ozhcd.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  1. /* -----------------------------------------------------------------------------
  2. * Copyright (c) 2011 Ozmo Inc
  3. * Released under the GNU General Public License Version 2 (GPLv2).
  4. *
  5. * This file provides the implementation of a USB host controller device that
  6. * does not have any associated hardware. Instead the virtual device is
  7. * connected to the WiFi network and emulates the operation of a USB hcd by
  8. * receiving and sending network frames.
  9. * Note:
  10. * We take great pains to reduce the amount of code where interrupts need to be
  11. * disabled and in this respect we are different from standard HCD's. In
  12. * particular we don't want in_irq() code bleeding over to the protocol side of
  13. * the driver.
  14. * The troublesome functions are the urb enqueue and dequeue functions both of
  15. * which can be called in_irq(). So for these functions we put the urbs into a
  16. * queue and request a tasklet to process them. This means that a spinlock with
  17. * interrupts disabled must be held for insertion and removal but most code is
  18. * is in tasklet or soft irq context. The lock that protects this list is called
  19. * the tasklet lock and serves the purpose of the 'HCD lock' which must be held
  20. * when calling the following functions.
  21. * usb_hcd_link_urb_to_ep()
  22. * usb_hcd_unlink_urb_from_ep()
  23. * usb_hcd_flush_endpoint()
  24. * usb_hcd_check_unlink_urb()
  25. * -----------------------------------------------------------------------------
  26. */
  27. #include <linux/platform_device.h>
  28. #include <linux/usb.h>
  29. #include <linux/slab.h>
  30. #include <linux/export.h>
  31. #include "linux/usb/hcd.h"
  32. #include <asm/unaligned.h>
  33. #include "ozdbg.h"
  34. #include "ozusbif.h"
  35. #include "ozurbparanoia.h"
  36. #include "ozhcd.h"
  37. /*
  38. * Number of units of buffering to capture for an isochronous IN endpoint before
  39. * allowing data to be indicated up.
  40. */
  41. #define OZ_IN_BUFFERING_UNITS 100
  42. /* Name of our platform device.
  43. */
  44. #define OZ_PLAT_DEV_NAME "ozwpan"
  45. /*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
  46. */
  47. #define EP0_TIMEOUT_COUNTER 13
  48. /* Debounce time HCD driver should wait before unregistering.
  49. */
  50. #define OZ_HUB_DEBOUNCE_TIMEOUT 1500
  51. /*
  52. * Used to link urbs together and also store some status information for each
  53. * urb.
  54. * A cache of these are kept in a pool to reduce number of calls to kmalloc.
  55. */
  56. struct oz_urb_link {
  57. struct list_head link;
  58. struct urb *urb;
  59. struct oz_port *port;
  60. u8 req_id;
  61. u8 ep_num;
  62. unsigned submit_counter;
  63. };
  64. static struct kmem_cache *oz_urb_link_cache;
  65. /* Holds state information about a USB endpoint.
  66. */
  67. #define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
  68. #define OZ_EP_BUFFER_SIZE_INT 512
  69. struct oz_endpoint {
  70. struct list_head urb_list; /* List of oz_urb_link items. */
  71. struct list_head link; /* For isoc ep, links in to isoc
  72. lists of oz_port. */
  73. struct timespec timestamp;
  74. int credit;
  75. int credit_ceiling;
  76. u8 ep_num;
  77. u8 attrib;
  78. u8 *buffer;
  79. int buffer_size;
  80. int in_ix;
  81. int out_ix;
  82. int buffered_units;
  83. unsigned flags;
  84. int start_frame;
  85. };
  86. /* Bits in the flags field. */
  87. #define OZ_F_EP_BUFFERING 0x1
  88. #define OZ_F_EP_HAVE_STREAM 0x2
  89. /* Holds state information about a USB interface.
  90. */
  91. struct oz_interface {
  92. unsigned ep_mask;
  93. u8 alt;
  94. };
  95. /* Holds state information about an hcd port.
  96. */
  97. #define OZ_NB_ENDPOINTS 16
  98. struct oz_port {
  99. unsigned flags;
  100. unsigned status;
  101. void *hpd;
  102. struct oz_hcd *ozhcd;
  103. spinlock_t port_lock;
  104. u8 bus_addr;
  105. u8 next_req_id;
  106. u8 config_num;
  107. int num_iface;
  108. struct oz_interface *iface;
  109. struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
  110. struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
  111. struct list_head isoc_out_ep;
  112. struct list_head isoc_in_ep;
  113. };
  114. #define OZ_PORT_F_PRESENT 0x1
  115. #define OZ_PORT_F_CHANGED 0x2
  116. #define OZ_PORT_F_DYING 0x4
  117. /* Data structure in the private context area of struct usb_hcd.
  118. */
  119. #define OZ_NB_PORTS 8
  120. struct oz_hcd {
  121. spinlock_t hcd_lock;
  122. struct list_head urb_pending_list;
  123. struct list_head urb_cancel_list;
  124. struct list_head orphanage;
  125. int conn_port; /* Port that is currently connecting, -1 if none.*/
  126. struct oz_port ports[OZ_NB_PORTS];
  127. uint flags;
  128. struct usb_hcd *hcd;
  129. };
  130. /* Bits in flags field.
  131. */
  132. #define OZ_HDC_F_SUSPENDED 0x1
  133. /*
  134. * Static function prototypes.
  135. */
  136. static int oz_hcd_start(struct usb_hcd *hcd);
  137. static void oz_hcd_stop(struct usb_hcd *hcd);
  138. static void oz_hcd_shutdown(struct usb_hcd *hcd);
  139. static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  140. gfp_t mem_flags);
  141. static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
  142. static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
  143. struct usb_host_endpoint *ep);
  144. static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
  145. struct usb_host_endpoint *ep);
  146. static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
  147. static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
  148. static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
  149. u16 windex, char *buf, u16 wlength);
  150. static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
  151. static int oz_hcd_bus_resume(struct usb_hcd *hcd);
  152. static int oz_plat_probe(struct platform_device *dev);
  153. static int oz_plat_remove(struct platform_device *dev);
  154. static void oz_plat_shutdown(struct platform_device *dev);
  155. static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
  156. static int oz_plat_resume(struct platform_device *dev);
  157. static void oz_urb_process_tasklet(unsigned long unused);
  158. static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
  159. struct oz_port *port, struct usb_host_config *config,
  160. gfp_t mem_flags);
  161. static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
  162. struct oz_port *port);
  163. static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
  164. struct oz_port *port,
  165. struct usb_host_interface *intf, gfp_t mem_flags);
  166. static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
  167. struct oz_port *port, int if_ix);
  168. static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
  169. gfp_t mem_flags);
  170. static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
  171. struct urb *urb);
  172. static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
  173. /*
  174. * Static external variables.
  175. */
  176. static struct platform_device *g_plat_dev;
  177. static struct oz_hcd *g_ozhcd;
  178. static DEFINE_SPINLOCK(g_hcdlock); /* Guards g_ozhcd. */
  179. static const char g_hcd_name[] = "Ozmo WPAN";
  180. static DEFINE_SPINLOCK(g_tasklet_lock);
  181. static struct tasklet_struct g_urb_process_tasklet;
  182. static struct tasklet_struct g_urb_cancel_tasklet;
  183. static atomic_t g_pending_urbs = ATOMIC_INIT(0);
  184. static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
  185. static const struct hc_driver g_oz_hc_drv = {
  186. .description = g_hcd_name,
  187. .product_desc = "Ozmo Devices WPAN",
  188. .hcd_priv_size = sizeof(struct oz_hcd),
  189. .flags = HCD_USB11,
  190. .start = oz_hcd_start,
  191. .stop = oz_hcd_stop,
  192. .shutdown = oz_hcd_shutdown,
  193. .urb_enqueue = oz_hcd_urb_enqueue,
  194. .urb_dequeue = oz_hcd_urb_dequeue,
  195. .endpoint_disable = oz_hcd_endpoint_disable,
  196. .endpoint_reset = oz_hcd_endpoint_reset,
  197. .get_frame_number = oz_hcd_get_frame_number,
  198. .hub_status_data = oz_hcd_hub_status_data,
  199. .hub_control = oz_hcd_hub_control,
  200. .bus_suspend = oz_hcd_bus_suspend,
  201. .bus_resume = oz_hcd_bus_resume,
  202. };
  203. static struct platform_driver g_oz_plat_drv = {
  204. .probe = oz_plat_probe,
  205. .remove = oz_plat_remove,
  206. .shutdown = oz_plat_shutdown,
  207. .suspend = oz_plat_suspend,
  208. .resume = oz_plat_resume,
  209. .driver = {
  210. .name = OZ_PLAT_DEV_NAME,
  211. .owner = THIS_MODULE,
  212. },
  213. };
  214. /*
  215. * Gets our private context area (which is of type struct oz_hcd) from the
  216. * usb_hcd structure.
  217. * Context: any
  218. */
  219. static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
  220. {
  221. return (struct oz_hcd *)hcd->hcd_priv;
  222. }
  223. /*
  224. * Searches list of ports to find the index of the one with a specified USB
  225. * bus address. If none of the ports has the bus address then the connection
  226. * port is returned, if there is one or -1 otherwise.
  227. * Context: any
  228. */
  229. static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
  230. {
  231. int i;
  232. for (i = 0; i < OZ_NB_PORTS; i++) {
  233. if (ozhcd->ports[i].bus_addr == bus_addr)
  234. return i;
  235. }
  236. return ozhcd->conn_port;
  237. }
  238. /*
  239. * Context: any
  240. */
  241. static struct oz_urb_link *oz_alloc_urb_link(void)
  242. {
  243. return kmem_cache_alloc(oz_urb_link_cache, GFP_ATOMIC);
  244. }
  245. /*
  246. * Context: any
  247. */
  248. static void oz_free_urb_link(struct oz_urb_link *urbl)
  249. {
  250. if (!urbl)
  251. return;
  252. kmem_cache_free(oz_urb_link_cache, urbl);
  253. }
  254. /*
  255. * Allocates endpoint structure and optionally a buffer. If a buffer is
  256. * allocated it immediately follows the endpoint structure.
  257. * Context: softirq
  258. */
  259. static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
  260. {
  261. struct oz_endpoint *ep =
  262. kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
  263. if (ep) {
  264. INIT_LIST_HEAD(&ep->urb_list);
  265. INIT_LIST_HEAD(&ep->link);
  266. ep->credit = -1;
  267. if (buffer_size) {
  268. ep->buffer_size = buffer_size;
  269. ep->buffer = (u8 *)(ep+1);
  270. }
  271. }
  272. return ep;
  273. }
  274. /*
  275. * Pre-condition: Must be called with g_tasklet_lock held and interrupts
  276. * disabled.
  277. * Context: softirq or process
  278. */
  279. static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
  280. struct urb *urb)
  281. {
  282. struct oz_urb_link *urbl;
  283. list_for_each_entry(urbl, &ozhcd->urb_cancel_list, link) {
  284. if (urb == urbl->urb) {
  285. list_del_init(&urbl->link);
  286. return urbl;
  287. }
  288. }
  289. return NULL;
  290. }
  291. /*
  292. * This is called when we have finished processing an urb. It unlinks it from
  293. * the ep and returns it to the core.
  294. * Context: softirq or process
  295. */
  296. static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
  297. int status)
  298. {
  299. struct oz_hcd *ozhcd = oz_hcd_private(hcd);
  300. unsigned long irq_state;
  301. struct oz_urb_link *cancel_urbl;
  302. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  303. usb_hcd_unlink_urb_from_ep(hcd, urb);
  304. /* Clear hcpriv which will prevent it being put in the cancel list
  305. * in the event that an attempt is made to cancel it.
  306. */
  307. urb->hcpriv = NULL;
  308. /* Walk the cancel list in case the urb is already sitting there.
  309. * Since we process the cancel list in a tasklet rather than in
  310. * the dequeue function this could happen.
  311. */
  312. cancel_urbl = oz_uncancel_urb(ozhcd, urb);
  313. /* Note: we release lock but do not enable local irqs.
  314. * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
  315. * or at least other host controllers disable interrupts at this point
  316. * so we do the same. We must, however, release the lock otherwise a
  317. * deadlock will occur if an urb is submitted to our driver in the urb
  318. * completion function. Because we disable interrupts it is possible
  319. * that the urb_enqueue function can be called with them disabled.
  320. */
  321. spin_unlock(&g_tasklet_lock);
  322. if (oz_forget_urb(urb)) {
  323. oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
  324. } else {
  325. atomic_dec(&g_pending_urbs);
  326. usb_hcd_giveback_urb(hcd, urb, status);
  327. }
  328. spin_lock(&g_tasklet_lock);
  329. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  330. if (cancel_urbl)
  331. oz_free_urb_link(cancel_urbl);
  332. }
  333. /*
  334. * Deallocates an endpoint including deallocating any associated stream and
  335. * returning any queued urbs to the core.
  336. * Context: softirq
  337. */
  338. static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
  339. {
  340. if (port) {
  341. LIST_HEAD(list);
  342. struct oz_hcd *ozhcd = port->ozhcd;
  343. if (ep->flags & OZ_F_EP_HAVE_STREAM)
  344. oz_usb_stream_delete(port->hpd, ep->ep_num);
  345. /* Transfer URBs to the orphanage while we hold the lock. */
  346. spin_lock_bh(&ozhcd->hcd_lock);
  347. /* Note: this works even if ep->urb_list is empty.*/
  348. list_replace_init(&ep->urb_list, &list);
  349. /* Put the URBs in the orphanage. */
  350. list_splice_tail(&list, &ozhcd->orphanage);
  351. spin_unlock_bh(&ozhcd->hcd_lock);
  352. }
  353. oz_dbg(ON, "Freeing endpoint memory\n");
  354. kfree(ep);
  355. }
  356. /*
  357. * Context: softirq
  358. */
  359. static void oz_complete_buffered_urb(struct oz_port *port,
  360. struct oz_endpoint *ep,
  361. struct urb *urb)
  362. {
  363. int data_len, available_space, copy_len;
  364. data_len = ep->buffer[ep->out_ix];
  365. if (data_len <= urb->transfer_buffer_length)
  366. available_space = data_len;
  367. else
  368. available_space = urb->transfer_buffer_length;
  369. if (++ep->out_ix == ep->buffer_size)
  370. ep->out_ix = 0;
  371. copy_len = ep->buffer_size - ep->out_ix;
  372. if (copy_len >= available_space)
  373. copy_len = available_space;
  374. memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
  375. if (copy_len < available_space) {
  376. memcpy((urb->transfer_buffer + copy_len), ep->buffer,
  377. (available_space - copy_len));
  378. ep->out_ix = available_space - copy_len;
  379. } else {
  380. ep->out_ix += copy_len;
  381. }
  382. urb->actual_length = available_space;
  383. if (ep->out_ix == ep->buffer_size)
  384. ep->out_ix = 0;
  385. ep->buffered_units--;
  386. oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
  387. available_space);
  388. oz_complete_urb(port->ozhcd->hcd, urb, 0);
  389. }
  390. /*
  391. * Context: softirq
  392. */
  393. static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
  394. struct urb *urb, u8 req_id)
  395. {
  396. struct oz_urb_link *urbl;
  397. struct oz_endpoint *ep = NULL;
  398. int err = 0;
  399. if (ep_addr >= OZ_NB_ENDPOINTS) {
  400. oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
  401. return -EINVAL;
  402. }
  403. urbl = oz_alloc_urb_link();
  404. if (!urbl)
  405. return -ENOMEM;
  406. urbl->submit_counter = 0;
  407. urbl->urb = urb;
  408. urbl->req_id = req_id;
  409. urbl->ep_num = ep_addr;
  410. /* Hold lock while we insert the URB into the list within the
  411. * endpoint structure.
  412. */
  413. spin_lock_bh(&port->ozhcd->hcd_lock);
  414. /* If the urb has been unlinked while out of any list then
  415. * complete it now.
  416. */
  417. if (urb->unlinked) {
  418. spin_unlock_bh(&port->ozhcd->hcd_lock);
  419. oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
  420. oz_complete_urb(port->ozhcd->hcd, urb, 0);
  421. oz_free_urb_link(urbl);
  422. return 0;
  423. }
  424. if (in_dir)
  425. ep = port->in_ep[ep_addr];
  426. else
  427. ep = port->out_ep[ep_addr];
  428. if (!ep) {
  429. err = -ENOMEM;
  430. goto out;
  431. }
  432. /*For interrupt endpoint check for buffered data
  433. * & complete urb
  434. */
  435. if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
  436. && ep->buffered_units > 0) {
  437. oz_free_urb_link(urbl);
  438. spin_unlock_bh(&port->ozhcd->hcd_lock);
  439. oz_complete_buffered_urb(port, ep, urb);
  440. return 0;
  441. }
  442. if (port->hpd) {
  443. list_add_tail(&urbl->link, &ep->urb_list);
  444. if (!in_dir && ep_addr && (ep->credit < 0)) {
  445. getrawmonotonic(&ep->timestamp);
  446. ep->credit = 0;
  447. }
  448. } else {
  449. err = -EPIPE;
  450. }
  451. out:
  452. spin_unlock_bh(&port->ozhcd->hcd_lock);
  453. if (err)
  454. oz_free_urb_link(urbl);
  455. return err;
  456. }
  457. /*
  458. * Removes an urb from the queue in the endpoint.
  459. * Returns 0 if it is found and -EIDRM otherwise.
  460. * Context: softirq
  461. */
  462. static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
  463. struct urb *urb)
  464. {
  465. struct oz_urb_link *urbl = NULL;
  466. struct oz_endpoint *ep;
  467. spin_lock_bh(&port->ozhcd->hcd_lock);
  468. if (in_dir)
  469. ep = port->in_ep[ep_addr];
  470. else
  471. ep = port->out_ep[ep_addr];
  472. if (ep) {
  473. struct list_head *e;
  474. list_for_each(e, &ep->urb_list) {
  475. urbl = list_entry(e, struct oz_urb_link, link);
  476. if (urbl->urb == urb) {
  477. list_del_init(e);
  478. break;
  479. }
  480. urbl = NULL;
  481. }
  482. }
  483. spin_unlock_bh(&port->ozhcd->hcd_lock);
  484. if (urbl)
  485. oz_free_urb_link(urbl);
  486. return urbl ? 0 : -EIDRM;
  487. }
  488. /*
  489. * Finds an urb given its request id.
  490. * Context: softirq
  491. */
  492. static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
  493. u8 req_id)
  494. {
  495. struct oz_hcd *ozhcd = port->ozhcd;
  496. struct urb *urb = NULL;
  497. struct oz_urb_link *urbl;
  498. struct oz_endpoint *ep;
  499. spin_lock_bh(&ozhcd->hcd_lock);
  500. ep = port->out_ep[ep_ix];
  501. if (ep) {
  502. struct list_head *e;
  503. list_for_each(e, &ep->urb_list) {
  504. urbl = list_entry(e, struct oz_urb_link, link);
  505. if (urbl->req_id == req_id) {
  506. urb = urbl->urb;
  507. list_del_init(e);
  508. break;
  509. }
  510. }
  511. }
  512. spin_unlock_bh(&ozhcd->hcd_lock);
  513. /* If urb is non-zero then we we must have an urb link to delete.
  514. */
  515. if (urb)
  516. oz_free_urb_link(urbl);
  517. return urb;
  518. }
  519. /*
  520. * Pre-condition: Port lock must be held.
  521. * Context: softirq
  522. */
  523. static void oz_acquire_port(struct oz_port *port, void *hpd)
  524. {
  525. INIT_LIST_HEAD(&port->isoc_out_ep);
  526. INIT_LIST_HEAD(&port->isoc_in_ep);
  527. port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
  528. port->status |= USB_PORT_STAT_CONNECTION |
  529. (USB_PORT_STAT_C_CONNECTION << 16);
  530. oz_usb_get(hpd);
  531. port->hpd = hpd;
  532. }
  533. /*
  534. * Context: softirq
  535. */
  536. static struct oz_hcd *oz_hcd_claim(void)
  537. {
  538. struct oz_hcd *ozhcd;
  539. spin_lock_bh(&g_hcdlock);
  540. ozhcd = g_ozhcd;
  541. if (ozhcd)
  542. usb_get_hcd(ozhcd->hcd);
  543. spin_unlock_bh(&g_hcdlock);
  544. return ozhcd;
  545. }
  546. /*
  547. * Context: softirq
  548. */
  549. static inline void oz_hcd_put(struct oz_hcd *ozhcd)
  550. {
  551. if (ozhcd)
  552. usb_put_hcd(ozhcd->hcd);
  553. }
  554. /*
  555. * This is called by the protocol handler to notify that a PD has arrived.
  556. * We allocate a port to associate with the PD and create a structure for
  557. * endpoint 0. This port is made the connection port.
  558. * In the event that one of the other port is already a connection port then
  559. * we fail.
  560. * TODO We should be able to do better than fail and should be able remember
  561. * that this port needs configuring and make it the connection port once the
  562. * current connection port has been assigned an address. Collisions here are
  563. * probably very rare indeed.
  564. * Context: softirq
  565. */
  566. struct oz_port *oz_hcd_pd_arrived(void *hpd)
  567. {
  568. int i;
  569. struct oz_port *hport;
  570. struct oz_hcd *ozhcd;
  571. struct oz_endpoint *ep;
  572. ozhcd = oz_hcd_claim();
  573. if (!ozhcd)
  574. return NULL;
  575. /* Allocate an endpoint object in advance (before holding hcd lock) to
  576. * use for out endpoint 0.
  577. */
  578. ep = oz_ep_alloc(0, GFP_ATOMIC);
  579. if (!ep)
  580. goto err_put;
  581. spin_lock_bh(&ozhcd->hcd_lock);
  582. if (ozhcd->conn_port >= 0)
  583. goto err_unlock;
  584. for (i = 0; i < OZ_NB_PORTS; i++) {
  585. struct oz_port *port = &ozhcd->ports[i];
  586. spin_lock(&port->port_lock);
  587. if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
  588. oz_acquire_port(port, hpd);
  589. spin_unlock(&port->port_lock);
  590. break;
  591. }
  592. spin_unlock(&port->port_lock);
  593. }
  594. if (i == OZ_NB_PORTS)
  595. goto err_unlock;
  596. ozhcd->conn_port = i;
  597. hport = &ozhcd->ports[i];
  598. hport->out_ep[0] = ep;
  599. spin_unlock_bh(&ozhcd->hcd_lock);
  600. if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
  601. usb_hcd_resume_root_hub(ozhcd->hcd);
  602. usb_hcd_poll_rh_status(ozhcd->hcd);
  603. oz_hcd_put(ozhcd);
  604. return hport;
  605. err_unlock:
  606. spin_unlock_bh(&ozhcd->hcd_lock);
  607. oz_ep_free(NULL, ep);
  608. err_put:
  609. oz_hcd_put(ozhcd);
  610. return NULL;
  611. }
  612. /*
  613. * This is called by the protocol handler to notify that the PD has gone away.
  614. * We need to deallocate all resources and then request that the root hub is
  615. * polled. We release the reference we hold on the PD.
  616. * Context: softirq
  617. */
  618. void oz_hcd_pd_departed(struct oz_port *port)
  619. {
  620. struct oz_hcd *ozhcd;
  621. void *hpd;
  622. struct oz_endpoint *ep = NULL;
  623. if (port == NULL) {
  624. oz_dbg(ON, "%s: port = 0\n", __func__);
  625. return;
  626. }
  627. ozhcd = port->ozhcd;
  628. if (ozhcd == NULL)
  629. return;
  630. /* Check if this is the connection port - if so clear it.
  631. */
  632. spin_lock_bh(&ozhcd->hcd_lock);
  633. if ((ozhcd->conn_port >= 0) &&
  634. (port == &ozhcd->ports[ozhcd->conn_port])) {
  635. oz_dbg(ON, "Clearing conn_port\n");
  636. ozhcd->conn_port = -1;
  637. }
  638. spin_lock(&port->port_lock);
  639. port->flags |= OZ_PORT_F_DYING;
  640. spin_unlock(&port->port_lock);
  641. spin_unlock_bh(&ozhcd->hcd_lock);
  642. oz_clean_endpoints_for_config(ozhcd->hcd, port);
  643. spin_lock_bh(&port->port_lock);
  644. hpd = port->hpd;
  645. port->hpd = NULL;
  646. port->bus_addr = 0xff;
  647. port->config_num = 0;
  648. port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
  649. port->flags |= OZ_PORT_F_CHANGED;
  650. port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
  651. port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
  652. /* If there is an endpont 0 then clear the pointer while we hold
  653. * the spinlock be we deallocate it after releasing the lock.
  654. */
  655. if (port->out_ep[0]) {
  656. ep = port->out_ep[0];
  657. port->out_ep[0] = NULL;
  658. }
  659. spin_unlock_bh(&port->port_lock);
  660. if (ep)
  661. oz_ep_free(port, ep);
  662. usb_hcd_poll_rh_status(ozhcd->hcd);
  663. oz_usb_put(hpd);
  664. }
  665. /*
  666. * Context: softirq
  667. */
  668. void oz_hcd_pd_reset(void *hpd, void *hport)
  669. {
  670. /* Cleanup the current configuration and report reset to the core.
  671. */
  672. struct oz_port *port = (struct oz_port *)hport;
  673. struct oz_hcd *ozhcd = port->ozhcd;
  674. oz_dbg(ON, "PD Reset\n");
  675. spin_lock_bh(&port->port_lock);
  676. port->flags |= OZ_PORT_F_CHANGED;
  677. port->status |= USB_PORT_STAT_RESET;
  678. port->status |= (USB_PORT_STAT_C_RESET << 16);
  679. spin_unlock_bh(&port->port_lock);
  680. oz_clean_endpoints_for_config(ozhcd->hcd, port);
  681. usb_hcd_poll_rh_status(ozhcd->hcd);
  682. }
  683. /*
  684. * Context: softirq
  685. */
  686. void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
  687. int length, int offset, int total_size)
  688. {
  689. struct oz_port *port = (struct oz_port *)hport;
  690. struct urb *urb;
  691. int err = 0;
  692. oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
  693. length, offset, total_size);
  694. urb = oz_find_urb_by_id(port, 0, req_id);
  695. if (!urb)
  696. return;
  697. if (status == 0) {
  698. int copy_len;
  699. int required_size = urb->transfer_buffer_length;
  700. if (required_size > total_size)
  701. required_size = total_size;
  702. copy_len = required_size-offset;
  703. if (length <= copy_len)
  704. copy_len = length;
  705. memcpy(urb->transfer_buffer+offset, desc, copy_len);
  706. offset += copy_len;
  707. if (offset < required_size) {
  708. struct usb_ctrlrequest *setup =
  709. (struct usb_ctrlrequest *)urb->setup_packet;
  710. unsigned wvalue = le16_to_cpu(setup->wValue);
  711. if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
  712. err = -ENOMEM;
  713. else if (oz_usb_get_desc_req(port->hpd, req_id,
  714. setup->bRequestType, (u8)(wvalue>>8),
  715. (u8)wvalue, setup->wIndex, offset,
  716. required_size-offset)) {
  717. oz_dequeue_ep_urb(port, 0, 0, urb);
  718. err = -ENOMEM;
  719. }
  720. if (err == 0)
  721. return;
  722. }
  723. }
  724. urb->actual_length = total_size;
  725. oz_complete_urb(port->ozhcd->hcd, urb, 0);
  726. }
  727. /*
  728. * Context: softirq
  729. */
  730. static void oz_display_conf_type(u8 t)
  731. {
  732. switch (t) {
  733. case USB_REQ_GET_STATUS:
  734. oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
  735. break;
  736. case USB_REQ_CLEAR_FEATURE:
  737. oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
  738. break;
  739. case USB_REQ_SET_FEATURE:
  740. oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
  741. break;
  742. case USB_REQ_SET_ADDRESS:
  743. oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
  744. break;
  745. case USB_REQ_GET_DESCRIPTOR:
  746. oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
  747. break;
  748. case USB_REQ_SET_DESCRIPTOR:
  749. oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
  750. break;
  751. case USB_REQ_GET_CONFIGURATION:
  752. oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
  753. break;
  754. case USB_REQ_SET_CONFIGURATION:
  755. oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
  756. break;
  757. case USB_REQ_GET_INTERFACE:
  758. oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
  759. break;
  760. case USB_REQ_SET_INTERFACE:
  761. oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
  762. break;
  763. case USB_REQ_SYNCH_FRAME:
  764. oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
  765. break;
  766. }
  767. }
  768. /*
  769. * Context: softirq
  770. */
  771. static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
  772. u8 rcode, u8 config_num)
  773. {
  774. int rc = 0;
  775. struct usb_hcd *hcd = port->ozhcd->hcd;
  776. if (rcode == 0) {
  777. port->config_num = config_num;
  778. oz_clean_endpoints_for_config(hcd, port);
  779. if (oz_build_endpoints_for_config(hcd, port,
  780. &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
  781. rc = -ENOMEM;
  782. }
  783. } else {
  784. rc = -ENOMEM;
  785. }
  786. oz_complete_urb(hcd, urb, rc);
  787. }
  788. /*
  789. * Context: softirq
  790. */
  791. static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
  792. u8 rcode, u8 if_num, u8 alt)
  793. {
  794. struct usb_hcd *hcd = port->ozhcd->hcd;
  795. int rc = 0;
  796. if ((rcode == 0) && (port->config_num > 0)) {
  797. struct usb_host_config *config;
  798. struct usb_host_interface *intf;
  799. oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
  800. oz_clean_endpoints_for_interface(hcd, port, if_num);
  801. config = &urb->dev->config[port->config_num-1];
  802. intf = &config->intf_cache[if_num]->altsetting[alt];
  803. if (oz_build_endpoints_for_interface(hcd, port, intf,
  804. GFP_ATOMIC))
  805. rc = -ENOMEM;
  806. else
  807. port->iface[if_num].alt = alt;
  808. } else {
  809. rc = -ENOMEM;
  810. }
  811. oz_complete_urb(hcd, urb, rc);
  812. }
  813. /*
  814. * Context: softirq
  815. */
  816. void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
  817. int data_len)
  818. {
  819. struct oz_port *port = (struct oz_port *)hport;
  820. struct urb *urb;
  821. struct usb_ctrlrequest *setup;
  822. struct usb_hcd *hcd = port->ozhcd->hcd;
  823. unsigned windex;
  824. unsigned wvalue;
  825. oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
  826. urb = oz_find_urb_by_id(port, 0, req_id);
  827. if (!urb) {
  828. oz_dbg(ON, "URB not found\n");
  829. return;
  830. }
  831. setup = (struct usb_ctrlrequest *)urb->setup_packet;
  832. windex = le16_to_cpu(setup->wIndex);
  833. wvalue = le16_to_cpu(setup->wValue);
  834. if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  835. /* Standard requests */
  836. oz_display_conf_type(setup->bRequest);
  837. switch (setup->bRequest) {
  838. case USB_REQ_SET_CONFIGURATION:
  839. oz_hcd_complete_set_config(port, urb, rcode,
  840. (u8)wvalue);
  841. break;
  842. case USB_REQ_SET_INTERFACE:
  843. oz_hcd_complete_set_interface(port, urb, rcode,
  844. (u8)windex, (u8)wvalue);
  845. break;
  846. default:
  847. oz_complete_urb(hcd, urb, 0);
  848. }
  849. } else {
  850. int copy_len;
  851. oz_dbg(ON, "VENDOR-CLASS - cnf\n");
  852. if (data_len) {
  853. if (data_len <= urb->transfer_buffer_length)
  854. copy_len = data_len;
  855. else
  856. copy_len = urb->transfer_buffer_length;
  857. memcpy(urb->transfer_buffer, data, copy_len);
  858. urb->actual_length = copy_len;
  859. }
  860. oz_complete_urb(hcd, urb, 0);
  861. }
  862. }
  863. /*
  864. * Context: softirq-serialized
  865. */
  866. static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
  867. int data_len)
  868. {
  869. int space;
  870. int copy_len;
  871. if (!ep->buffer)
  872. return -1;
  873. space = ep->out_ix-ep->in_ix-1;
  874. if (space < 0)
  875. space += ep->buffer_size;
  876. if (space < (data_len+1)) {
  877. oz_dbg(ON, "Buffer full\n");
  878. return -1;
  879. }
  880. ep->buffer[ep->in_ix] = (u8)data_len;
  881. if (++ep->in_ix == ep->buffer_size)
  882. ep->in_ix = 0;
  883. copy_len = ep->buffer_size - ep->in_ix;
  884. if (copy_len > data_len)
  885. copy_len = data_len;
  886. memcpy(&ep->buffer[ep->in_ix], data, copy_len);
  887. if (copy_len < data_len) {
  888. memcpy(ep->buffer, data+copy_len, data_len-copy_len);
  889. ep->in_ix = data_len-copy_len;
  890. } else {
  891. ep->in_ix += copy_len;
  892. }
  893. if (ep->in_ix == ep->buffer_size)
  894. ep->in_ix = 0;
  895. ep->buffered_units++;
  896. return 0;
  897. }
  898. /*
  899. * Context: softirq-serialized
  900. */
  901. void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
  902. {
  903. struct oz_port *port = (struct oz_port *)hport;
  904. struct oz_endpoint *ep;
  905. struct oz_hcd *ozhcd = port->ozhcd;
  906. spin_lock_bh(&ozhcd->hcd_lock);
  907. ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
  908. if (ep == NULL)
  909. goto done;
  910. switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
  911. case USB_ENDPOINT_XFER_INT:
  912. case USB_ENDPOINT_XFER_BULK:
  913. if (!list_empty(&ep->urb_list)) {
  914. struct oz_urb_link *urbl =
  915. list_first_entry(&ep->urb_list,
  916. struct oz_urb_link, link);
  917. struct urb *urb;
  918. int copy_len;
  919. list_del_init(&urbl->link);
  920. spin_unlock_bh(&ozhcd->hcd_lock);
  921. urb = urbl->urb;
  922. oz_free_urb_link(urbl);
  923. if (data_len <= urb->transfer_buffer_length)
  924. copy_len = data_len;
  925. else
  926. copy_len = urb->transfer_buffer_length;
  927. memcpy(urb->transfer_buffer, data, copy_len);
  928. urb->actual_length = copy_len;
  929. oz_complete_urb(port->ozhcd->hcd, urb, 0);
  930. return;
  931. }
  932. oz_dbg(ON, "buffering frame as URB is not available\n");
  933. oz_hcd_buffer_data(ep, data, data_len);
  934. break;
  935. case USB_ENDPOINT_XFER_ISOC:
  936. oz_hcd_buffer_data(ep, data, data_len);
  937. break;
  938. }
  939. done:
  940. spin_unlock_bh(&ozhcd->hcd_lock);
  941. }
  942. /*
  943. * Context: unknown
  944. */
  945. static inline int oz_usb_get_frame_number(void)
  946. {
  947. return atomic_inc_return(&g_usb_frame_number);
  948. }
  949. /*
  950. * Context: softirq
  951. */
  952. int oz_hcd_heartbeat(void *hport)
  953. {
  954. int rc = 0;
  955. struct oz_port *port = (struct oz_port *)hport;
  956. struct oz_hcd *ozhcd = port->ozhcd;
  957. struct oz_urb_link *urbl, *n;
  958. LIST_HEAD(xfr_list);
  959. struct urb *urb;
  960. struct oz_endpoint *ep;
  961. struct timespec ts, delta;
  962. getrawmonotonic(&ts);
  963. /* Check the OUT isoc endpoints to see if any URB data can be sent.
  964. */
  965. spin_lock_bh(&ozhcd->hcd_lock);
  966. list_for_each_entry(ep, &port->isoc_out_ep, link) {
  967. if (ep->credit < 0)
  968. continue;
  969. delta = timespec_sub(ts, ep->timestamp);
  970. ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
  971. if (ep->credit > ep->credit_ceiling)
  972. ep->credit = ep->credit_ceiling;
  973. ep->timestamp = ts;
  974. while (ep->credit && !list_empty(&ep->urb_list)) {
  975. urbl = list_first_entry(&ep->urb_list,
  976. struct oz_urb_link, link);
  977. urb = urbl->urb;
  978. if ((ep->credit + 1) < urb->number_of_packets)
  979. break;
  980. ep->credit -= urb->number_of_packets;
  981. if (ep->credit < 0)
  982. ep->credit = 0;
  983. list_move_tail(&urbl->link, &xfr_list);
  984. }
  985. }
  986. spin_unlock_bh(&ozhcd->hcd_lock);
  987. /* Send to PD and complete URBs.
  988. */
  989. list_for_each_entry_safe(urbl, n, &xfr_list, link) {
  990. urb = urbl->urb;
  991. list_del_init(&urbl->link);
  992. urb->error_count = 0;
  993. urb->start_frame = oz_usb_get_frame_number();
  994. oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
  995. oz_free_urb_link(urbl);
  996. oz_complete_urb(port->ozhcd->hcd, urb, 0);
  997. }
  998. /* Check the IN isoc endpoints to see if any URBs can be completed.
  999. */
  1000. spin_lock_bh(&ozhcd->hcd_lock);
  1001. list_for_each_entry(ep, &port->isoc_in_ep, link) {
  1002. if (ep->flags & OZ_F_EP_BUFFERING) {
  1003. if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
  1004. ep->flags &= ~OZ_F_EP_BUFFERING;
  1005. ep->credit = 0;
  1006. ep->timestamp = ts;
  1007. ep->start_frame = 0;
  1008. }
  1009. continue;
  1010. }
  1011. delta = timespec_sub(ts, ep->timestamp);
  1012. ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
  1013. ep->timestamp = ts;
  1014. list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
  1015. struct urb *urb = urbl->urb;
  1016. int len = 0;
  1017. int copy_len;
  1018. int i;
  1019. if (ep->credit < urb->number_of_packets)
  1020. break;
  1021. if (ep->buffered_units < urb->number_of_packets)
  1022. break;
  1023. urb->actual_length = 0;
  1024. for (i = 0; i < urb->number_of_packets; i++) {
  1025. len = ep->buffer[ep->out_ix];
  1026. if (++ep->out_ix == ep->buffer_size)
  1027. ep->out_ix = 0;
  1028. copy_len = ep->buffer_size - ep->out_ix;
  1029. if (copy_len > len)
  1030. copy_len = len;
  1031. memcpy(urb->transfer_buffer,
  1032. &ep->buffer[ep->out_ix], copy_len);
  1033. if (copy_len < len) {
  1034. memcpy(urb->transfer_buffer+copy_len,
  1035. ep->buffer, len-copy_len);
  1036. ep->out_ix = len-copy_len;
  1037. } else
  1038. ep->out_ix += copy_len;
  1039. if (ep->out_ix == ep->buffer_size)
  1040. ep->out_ix = 0;
  1041. urb->iso_frame_desc[i].offset =
  1042. urb->actual_length;
  1043. urb->actual_length += len;
  1044. urb->iso_frame_desc[i].actual_length = len;
  1045. urb->iso_frame_desc[i].status = 0;
  1046. }
  1047. ep->buffered_units -= urb->number_of_packets;
  1048. urb->error_count = 0;
  1049. urb->start_frame = ep->start_frame;
  1050. ep->start_frame += urb->number_of_packets;
  1051. list_move_tail(&urbl->link, &xfr_list);
  1052. ep->credit -= urb->number_of_packets;
  1053. }
  1054. }
  1055. if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
  1056. rc = 1;
  1057. spin_unlock_bh(&ozhcd->hcd_lock);
  1058. /* Complete the filled URBs.
  1059. */
  1060. list_for_each_entry_safe(urbl, n, &xfr_list, link) {
  1061. urb = urbl->urb;
  1062. list_del_init(&urbl->link);
  1063. oz_free_urb_link(urbl);
  1064. oz_complete_urb(port->ozhcd->hcd, urb, 0);
  1065. }
  1066. /* Check if there are any ep0 requests that have timed out.
  1067. * If so resent to PD.
  1068. */
  1069. ep = port->out_ep[0];
  1070. if (ep) {
  1071. spin_lock_bh(&ozhcd->hcd_lock);
  1072. list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
  1073. if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
  1074. oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
  1075. list_move_tail(&urbl->link, &xfr_list);
  1076. urbl->submit_counter = 0;
  1077. } else {
  1078. urbl->submit_counter++;
  1079. }
  1080. }
  1081. if (!list_empty(&ep->urb_list))
  1082. rc = 1;
  1083. spin_unlock_bh(&ozhcd->hcd_lock);
  1084. list_for_each_entry_safe(urbl, n, &xfr_list, link) {
  1085. oz_dbg(ON, "Resending request to PD\n");
  1086. oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
  1087. oz_free_urb_link(urbl);
  1088. }
  1089. }
  1090. return rc;
  1091. }
  1092. /*
  1093. * Context: softirq
  1094. */
  1095. static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
  1096. struct oz_port *port,
  1097. struct usb_host_interface *intf, gfp_t mem_flags)
  1098. {
  1099. struct oz_hcd *ozhcd = port->ozhcd;
  1100. int i;
  1101. int if_ix = intf->desc.bInterfaceNumber;
  1102. int request_heartbeat = 0;
  1103. oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
  1104. if (if_ix >= port->num_iface || port->iface == NULL)
  1105. return -ENOMEM;
  1106. for (i = 0; i < intf->desc.bNumEndpoints; i++) {
  1107. struct usb_host_endpoint *hep = &intf->endpoint[i];
  1108. u8 ep_addr = hep->desc.bEndpointAddress;
  1109. u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  1110. struct oz_endpoint *ep;
  1111. int buffer_size = 0;
  1112. oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
  1113. if (ep_addr & USB_ENDPOINT_DIR_MASK) {
  1114. switch (hep->desc.bmAttributes &
  1115. USB_ENDPOINT_XFERTYPE_MASK) {
  1116. case USB_ENDPOINT_XFER_ISOC:
  1117. buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
  1118. break;
  1119. case USB_ENDPOINT_XFER_INT:
  1120. buffer_size = OZ_EP_BUFFER_SIZE_INT;
  1121. break;
  1122. }
  1123. }
  1124. ep = oz_ep_alloc(buffer_size, mem_flags);
  1125. if (!ep) {
  1126. oz_clean_endpoints_for_interface(hcd, port, if_ix);
  1127. return -ENOMEM;
  1128. }
  1129. ep->attrib = hep->desc.bmAttributes;
  1130. ep->ep_num = ep_num;
  1131. if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
  1132. == USB_ENDPOINT_XFER_ISOC) {
  1133. oz_dbg(ON, "wMaxPacketSize = %d\n",
  1134. usb_endpoint_maxp(&hep->desc));
  1135. ep->credit_ceiling = 200;
  1136. if (ep_addr & USB_ENDPOINT_DIR_MASK) {
  1137. ep->flags |= OZ_F_EP_BUFFERING;
  1138. } else {
  1139. ep->flags |= OZ_F_EP_HAVE_STREAM;
  1140. if (oz_usb_stream_create(port->hpd, ep_num))
  1141. ep->flags &= ~OZ_F_EP_HAVE_STREAM;
  1142. }
  1143. }
  1144. spin_lock_bh(&ozhcd->hcd_lock);
  1145. if (ep_addr & USB_ENDPOINT_DIR_MASK) {
  1146. port->in_ep[ep_num] = ep;
  1147. port->iface[if_ix].ep_mask |=
  1148. (1<<(ep_num+OZ_NB_ENDPOINTS));
  1149. if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
  1150. == USB_ENDPOINT_XFER_ISOC) {
  1151. list_add_tail(&ep->link, &port->isoc_in_ep);
  1152. request_heartbeat = 1;
  1153. }
  1154. } else {
  1155. port->out_ep[ep_num] = ep;
  1156. port->iface[if_ix].ep_mask |= (1<<ep_num);
  1157. if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
  1158. == USB_ENDPOINT_XFER_ISOC) {
  1159. list_add_tail(&ep->link, &port->isoc_out_ep);
  1160. request_heartbeat = 1;
  1161. }
  1162. }
  1163. spin_unlock_bh(&ozhcd->hcd_lock);
  1164. if (request_heartbeat && port->hpd)
  1165. oz_usb_request_heartbeat(port->hpd);
  1166. }
  1167. return 0;
  1168. }
  1169. /*
  1170. * Context: softirq
  1171. */
  1172. static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
  1173. struct oz_port *port, int if_ix)
  1174. {
  1175. struct oz_hcd *ozhcd = port->ozhcd;
  1176. unsigned mask;
  1177. int i;
  1178. LIST_HEAD(ep_list);
  1179. struct oz_endpoint *ep, *n;
  1180. oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
  1181. if (if_ix >= port->num_iface)
  1182. return;
  1183. spin_lock_bh(&ozhcd->hcd_lock);
  1184. mask = port->iface[if_ix].ep_mask;
  1185. port->iface[if_ix].ep_mask = 0;
  1186. for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
  1187. struct list_head *e;
  1188. /* Gather OUT endpoints.
  1189. */
  1190. if ((mask & (1<<i)) && port->out_ep[i]) {
  1191. e = &port->out_ep[i]->link;
  1192. port->out_ep[i] = NULL;
  1193. /* Remove from isoc list if present.
  1194. */
  1195. list_move_tail(e, &ep_list);
  1196. }
  1197. /* Gather IN endpoints.
  1198. */
  1199. if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
  1200. e = &port->in_ep[i]->link;
  1201. port->in_ep[i] = NULL;
  1202. list_move_tail(e, &ep_list);
  1203. }
  1204. }
  1205. spin_unlock_bh(&ozhcd->hcd_lock);
  1206. list_for_each_entry_safe(ep, n, &ep_list, link) {
  1207. list_del_init(&ep->link);
  1208. oz_ep_free(port, ep);
  1209. }
  1210. }
  1211. /*
  1212. * Context: softirq
  1213. */
  1214. static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
  1215. struct oz_port *port, struct usb_host_config *config,
  1216. gfp_t mem_flags)
  1217. {
  1218. struct oz_hcd *ozhcd = port->ozhcd;
  1219. int i;
  1220. int num_iface = config->desc.bNumInterfaces;
  1221. if (num_iface) {
  1222. struct oz_interface *iface;
  1223. iface = kmalloc_array(num_iface, sizeof(struct oz_interface),
  1224. mem_flags | __GFP_ZERO);
  1225. if (!iface)
  1226. return -ENOMEM;
  1227. spin_lock_bh(&ozhcd->hcd_lock);
  1228. port->iface = iface;
  1229. port->num_iface = num_iface;
  1230. spin_unlock_bh(&ozhcd->hcd_lock);
  1231. }
  1232. for (i = 0; i < num_iface; i++) {
  1233. struct usb_host_interface *intf =
  1234. &config->intf_cache[i]->altsetting[0];
  1235. if (oz_build_endpoints_for_interface(hcd, port, intf,
  1236. mem_flags))
  1237. goto fail;
  1238. }
  1239. return 0;
  1240. fail:
  1241. oz_clean_endpoints_for_config(hcd, port);
  1242. return -1;
  1243. }
  1244. /*
  1245. * Context: softirq
  1246. */
  1247. static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
  1248. struct oz_port *port)
  1249. {
  1250. struct oz_hcd *ozhcd = port->ozhcd;
  1251. int i;
  1252. oz_dbg(ON, "Deleting endpoints for configuration\n");
  1253. for (i = 0; i < port->num_iface; i++)
  1254. oz_clean_endpoints_for_interface(hcd, port, i);
  1255. spin_lock_bh(&ozhcd->hcd_lock);
  1256. if (port->iface) {
  1257. oz_dbg(ON, "Freeing interfaces object\n");
  1258. kfree(port->iface);
  1259. port->iface = NULL;
  1260. }
  1261. port->num_iface = 0;
  1262. spin_unlock_bh(&ozhcd->hcd_lock);
  1263. }
  1264. /*
  1265. * Context: tasklet
  1266. */
  1267. static void *oz_claim_hpd(struct oz_port *port)
  1268. {
  1269. void *hpd;
  1270. struct oz_hcd *ozhcd = port->ozhcd;
  1271. spin_lock_bh(&ozhcd->hcd_lock);
  1272. hpd = port->hpd;
  1273. if (hpd)
  1274. oz_usb_get(hpd);
  1275. spin_unlock_bh(&ozhcd->hcd_lock);
  1276. return hpd;
  1277. }
  1278. /*
  1279. * Context: tasklet
  1280. */
  1281. static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
  1282. gfp_t mem_flags)
  1283. {
  1284. struct usb_ctrlrequest *setup;
  1285. unsigned windex;
  1286. unsigned wvalue;
  1287. unsigned wlength;
  1288. void *hpd;
  1289. u8 req_id;
  1290. int rc = 0;
  1291. unsigned complete = 0;
  1292. int port_ix = -1;
  1293. struct oz_port *port = NULL;
  1294. oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
  1295. port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
  1296. if (port_ix < 0) {
  1297. rc = -EPIPE;
  1298. goto out;
  1299. }
  1300. port = &ozhcd->ports[port_ix];
  1301. if (((port->flags & OZ_PORT_F_PRESENT) == 0)
  1302. || (port->flags & OZ_PORT_F_DYING)) {
  1303. oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
  1304. port_ix, urb->dev->devnum);
  1305. rc = -EPIPE;
  1306. goto out;
  1307. }
  1308. /* Store port in private context data.
  1309. */
  1310. urb->hcpriv = port;
  1311. setup = (struct usb_ctrlrequest *)urb->setup_packet;
  1312. windex = le16_to_cpu(setup->wIndex);
  1313. wvalue = le16_to_cpu(setup->wValue);
  1314. wlength = le16_to_cpu(setup->wLength);
  1315. oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
  1316. oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
  1317. oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
  1318. oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
  1319. oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
  1320. req_id = port->next_req_id++;
  1321. hpd = oz_claim_hpd(port);
  1322. if (hpd == NULL) {
  1323. oz_dbg(ON, "Cannot claim port\n");
  1324. rc = -EPIPE;
  1325. goto out;
  1326. }
  1327. if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1328. /* Standard requests
  1329. */
  1330. switch (setup->bRequest) {
  1331. case USB_REQ_GET_DESCRIPTOR:
  1332. oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
  1333. break;
  1334. case USB_REQ_SET_ADDRESS:
  1335. oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
  1336. oz_dbg(ON, "Port %d address is 0x%x\n",
  1337. ozhcd->conn_port,
  1338. (u8)le16_to_cpu(setup->wValue));
  1339. spin_lock_bh(&ozhcd->hcd_lock);
  1340. if (ozhcd->conn_port >= 0) {
  1341. ozhcd->ports[ozhcd->conn_port].bus_addr =
  1342. (u8)le16_to_cpu(setup->wValue);
  1343. oz_dbg(ON, "Clearing conn_port\n");
  1344. ozhcd->conn_port = -1;
  1345. }
  1346. spin_unlock_bh(&ozhcd->hcd_lock);
  1347. complete = 1;
  1348. break;
  1349. case USB_REQ_SET_CONFIGURATION:
  1350. oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
  1351. break;
  1352. case USB_REQ_GET_CONFIGURATION:
  1353. /* We short circuit this case and reply directly since
  1354. * we have the selected configuration number cached.
  1355. */
  1356. oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
  1357. if (urb->transfer_buffer_length >= 1) {
  1358. urb->actual_length = 1;
  1359. *((u8 *)urb->transfer_buffer) =
  1360. port->config_num;
  1361. complete = 1;
  1362. } else {
  1363. rc = -EPIPE;
  1364. }
  1365. break;
  1366. case USB_REQ_GET_INTERFACE:
  1367. /* We short circuit this case and reply directly since
  1368. * we have the selected interface alternative cached.
  1369. */
  1370. oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
  1371. if (urb->transfer_buffer_length >= 1) {
  1372. urb->actual_length = 1;
  1373. *((u8 *)urb->transfer_buffer) =
  1374. port->iface[(u8)windex].alt;
  1375. oz_dbg(ON, "interface = %d alt = %d\n",
  1376. windex, port->iface[(u8)windex].alt);
  1377. complete = 1;
  1378. } else {
  1379. rc = -EPIPE;
  1380. }
  1381. break;
  1382. case USB_REQ_SET_INTERFACE:
  1383. oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
  1384. break;
  1385. }
  1386. }
  1387. if (!rc && !complete) {
  1388. int data_len = 0;
  1389. if ((setup->bRequestType & USB_DIR_IN) == 0)
  1390. data_len = wlength;
  1391. urb->actual_length = data_len;
  1392. if (oz_usb_control_req(port->hpd, req_id, setup,
  1393. urb->transfer_buffer, data_len)) {
  1394. rc = -ENOMEM;
  1395. } else {
  1396. /* Note: we are queuing the request after we have
  1397. * submitted it to be transmitted. If the request were
  1398. * to complete before we queued it then it would not
  1399. * be found in the queue. It seems impossible for
  1400. * this to happen but if it did the request would
  1401. * be resubmitted so the problem would hopefully
  1402. * resolve itself. Putting the request into the
  1403. * queue before it has been sent is worse since the
  1404. * urb could be cancelled while we are using it
  1405. * to build the request.
  1406. */
  1407. if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
  1408. rc = -ENOMEM;
  1409. }
  1410. }
  1411. oz_usb_put(hpd);
  1412. out:
  1413. if (rc || complete) {
  1414. oz_dbg(ON, "Completing request locally\n");
  1415. oz_complete_urb(ozhcd->hcd, urb, rc);
  1416. } else {
  1417. oz_usb_request_heartbeat(port->hpd);
  1418. }
  1419. }
  1420. /*
  1421. * Context: tasklet
  1422. */
  1423. static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
  1424. {
  1425. int rc = 0;
  1426. struct oz_port *port = urb->hcpriv;
  1427. u8 ep_addr;
  1428. /* When we are paranoid we keep a list of urbs which we check against
  1429. * before handing one back. This is just for debugging during
  1430. * development and should be turned off in the released driver.
  1431. */
  1432. oz_remember_urb(urb);
  1433. /* Check buffer is valid.
  1434. */
  1435. if (!urb->transfer_buffer && urb->transfer_buffer_length)
  1436. return -EINVAL;
  1437. /* Check if there is a device at the port - refuse if not.
  1438. */
  1439. if ((port->flags & OZ_PORT_F_PRESENT) == 0)
  1440. return -EPIPE;
  1441. ep_addr = usb_pipeendpoint(urb->pipe);
  1442. if (ep_addr) {
  1443. /* If the request is not for EP0 then queue it.
  1444. */
  1445. if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
  1446. urb, 0))
  1447. rc = -EPIPE;
  1448. } else {
  1449. oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
  1450. }
  1451. return rc;
  1452. }
  1453. /*
  1454. * Context: tasklet
  1455. */
  1456. static void oz_urb_process_tasklet(unsigned long unused)
  1457. {
  1458. unsigned long irq_state;
  1459. struct urb *urb;
  1460. struct oz_hcd *ozhcd = oz_hcd_claim();
  1461. struct oz_urb_link *urbl, *n;
  1462. int rc = 0;
  1463. if (ozhcd == NULL)
  1464. return;
  1465. /* This is called from a tasklet so is in softirq context but the urb
  1466. * list is filled from any context so we need to lock
  1467. * appropriately while removing urbs.
  1468. */
  1469. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1470. list_for_each_entry_safe(urbl, n, &ozhcd->urb_pending_list, link) {
  1471. list_del_init(&urbl->link);
  1472. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1473. urb = urbl->urb;
  1474. oz_free_urb_link(urbl);
  1475. rc = oz_urb_process(ozhcd, urb);
  1476. if (rc)
  1477. oz_complete_urb(ozhcd->hcd, urb, rc);
  1478. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1479. }
  1480. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1481. oz_hcd_put(ozhcd);
  1482. }
  1483. /*
  1484. * This function searches for the urb in any of the lists it could be in.
  1485. * If it is found it is removed from the list and completed. If the urb is
  1486. * being processed then it won't be in a list so won't be found. However, the
  1487. * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
  1488. * to a non-zero value. When an attempt is made to put the urb back in a list
  1489. * the unlinked field will be checked and the urb will then be completed.
  1490. * Context: tasklet
  1491. */
  1492. static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
  1493. {
  1494. struct oz_urb_link *urbl = NULL;
  1495. struct list_head *e;
  1496. struct oz_hcd *ozhcd;
  1497. unsigned long irq_state;
  1498. u8 ix;
  1499. if (port == NULL) {
  1500. oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
  1501. return;
  1502. }
  1503. ozhcd = port->ozhcd;
  1504. if (ozhcd == NULL) {
  1505. oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
  1506. return;
  1507. }
  1508. /* Look in the tasklet queue.
  1509. */
  1510. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1511. list_for_each(e, &ozhcd->urb_cancel_list) {
  1512. urbl = list_entry(e, struct oz_urb_link, link);
  1513. if (urb == urbl->urb) {
  1514. list_del_init(e);
  1515. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1516. goto out2;
  1517. }
  1518. }
  1519. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1520. urbl = NULL;
  1521. /* Look in the orphanage.
  1522. */
  1523. spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
  1524. list_for_each(e, &ozhcd->orphanage) {
  1525. urbl = list_entry(e, struct oz_urb_link, link);
  1526. if (urbl->urb == urb) {
  1527. list_del(e);
  1528. oz_dbg(ON, "Found urb in orphanage\n");
  1529. goto out;
  1530. }
  1531. }
  1532. ix = (ep_num & 0xf);
  1533. urbl = NULL;
  1534. if ((ep_num & USB_DIR_IN) && ix)
  1535. urbl = oz_remove_urb(port->in_ep[ix], urb);
  1536. else
  1537. urbl = oz_remove_urb(port->out_ep[ix], urb);
  1538. out:
  1539. spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
  1540. out2:
  1541. if (urbl) {
  1542. urb->actual_length = 0;
  1543. oz_free_urb_link(urbl);
  1544. oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
  1545. }
  1546. }
  1547. /*
  1548. * Context: tasklet
  1549. */
  1550. static void oz_urb_cancel_tasklet(unsigned long unused)
  1551. {
  1552. unsigned long irq_state;
  1553. struct urb *urb;
  1554. struct oz_urb_link *urbl, *n;
  1555. struct oz_hcd *ozhcd = oz_hcd_claim();
  1556. if (ozhcd == NULL)
  1557. return;
  1558. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1559. list_for_each_entry_safe(urbl, n, &ozhcd->urb_cancel_list, link) {
  1560. list_del_init(&urbl->link);
  1561. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1562. urb = urbl->urb;
  1563. if (urb->unlinked)
  1564. oz_urb_cancel(urbl->port, urbl->ep_num, urb);
  1565. oz_free_urb_link(urbl);
  1566. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1567. }
  1568. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1569. oz_hcd_put(ozhcd);
  1570. }
  1571. /*
  1572. * Context: unknown
  1573. */
  1574. static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
  1575. {
  1576. if (ozhcd) {
  1577. struct oz_urb_link *urbl, *n;
  1578. list_for_each_entry_safe(urbl, n, &ozhcd->orphanage, link) {
  1579. list_del(&urbl->link);
  1580. oz_complete_urb(ozhcd->hcd, urbl->urb, status);
  1581. oz_free_urb_link(urbl);
  1582. }
  1583. }
  1584. }
  1585. /*
  1586. * Context: unknown
  1587. */
  1588. static int oz_hcd_start(struct usb_hcd *hcd)
  1589. {
  1590. hcd->power_budget = 200;
  1591. hcd->state = HC_STATE_RUNNING;
  1592. hcd->uses_new_polling = 1;
  1593. return 0;
  1594. }
  1595. /*
  1596. * Context: unknown
  1597. */
  1598. static void oz_hcd_stop(struct usb_hcd *hcd)
  1599. {
  1600. }
  1601. /*
  1602. * Context: unknown
  1603. */
  1604. static void oz_hcd_shutdown(struct usb_hcd *hcd)
  1605. {
  1606. }
  1607. /*
  1608. * Called to queue an urb for the device.
  1609. * This function should return a non-zero error code if it fails the urb but
  1610. * should not call usb_hcd_giveback_urb().
  1611. * Context: any
  1612. */
  1613. static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  1614. gfp_t mem_flags)
  1615. {
  1616. struct oz_hcd *ozhcd = oz_hcd_private(hcd);
  1617. int rc;
  1618. int port_ix;
  1619. struct oz_port *port;
  1620. unsigned long irq_state;
  1621. struct oz_urb_link *urbl;
  1622. oz_dbg(URB, "%s: (%p)\n", __func__, urb);
  1623. if (unlikely(ozhcd == NULL)) {
  1624. oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
  1625. return -EPIPE;
  1626. }
  1627. if (unlikely(hcd->state != HC_STATE_RUNNING)) {
  1628. oz_dbg(URB, "Refused urb(%p) not running\n", urb);
  1629. return -EPIPE;
  1630. }
  1631. port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
  1632. if (port_ix < 0)
  1633. return -EPIPE;
  1634. port = &ozhcd->ports[port_ix];
  1635. if (port == NULL)
  1636. return -EPIPE;
  1637. if (!(port->flags & OZ_PORT_F_PRESENT) ||
  1638. (port->flags & OZ_PORT_F_CHANGED)) {
  1639. oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
  1640. port_ix, urb->dev->devnum);
  1641. return -EPIPE;
  1642. }
  1643. urb->hcpriv = port;
  1644. /* Put request in queue for processing by tasklet.
  1645. */
  1646. urbl = oz_alloc_urb_link();
  1647. if (unlikely(urbl == NULL))
  1648. return -ENOMEM;
  1649. urbl->urb = urb;
  1650. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1651. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  1652. if (unlikely(rc)) {
  1653. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1654. oz_free_urb_link(urbl);
  1655. return rc;
  1656. }
  1657. list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
  1658. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1659. tasklet_schedule(&g_urb_process_tasklet);
  1660. atomic_inc(&g_pending_urbs);
  1661. return 0;
  1662. }
  1663. /*
  1664. * Context: tasklet
  1665. */
  1666. static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
  1667. struct urb *urb)
  1668. {
  1669. struct oz_urb_link *urbl;
  1670. if (unlikely(ep == NULL))
  1671. return NULL;
  1672. list_for_each_entry(urbl, &ep->urb_list, link) {
  1673. if (urbl->urb == urb) {
  1674. list_del_init(&urbl->link);
  1675. if (usb_pipeisoc(urb->pipe)) {
  1676. ep->credit -= urb->number_of_packets;
  1677. if (ep->credit < 0)
  1678. ep->credit = 0;
  1679. }
  1680. return urbl;
  1681. }
  1682. }
  1683. return NULL;
  1684. }
  1685. /*
  1686. * Called to dequeue a previously submitted urb for the device.
  1687. * Context: any
  1688. */
  1689. static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1690. {
  1691. struct oz_hcd *ozhcd = oz_hcd_private(hcd);
  1692. struct oz_urb_link *urbl;
  1693. int rc;
  1694. unsigned long irq_state;
  1695. oz_dbg(URB, "%s: (%p)\n", __func__, urb);
  1696. urbl = oz_alloc_urb_link();
  1697. if (unlikely(urbl == NULL))
  1698. return -ENOMEM;
  1699. spin_lock_irqsave(&g_tasklet_lock, irq_state);
  1700. /* The following function checks the urb is still in the queue
  1701. * maintained by the core and that the unlinked field is zero.
  1702. * If both are true the function sets the unlinked field and returns
  1703. * zero. Otherwise it returns an error.
  1704. */
  1705. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1706. /* We have to check we haven't completed the urb or are about
  1707. * to complete it. When we do we set hcpriv to 0 so if this has
  1708. * already happened we don't put the urb in the cancel queue.
  1709. */
  1710. if ((rc == 0) && urb->hcpriv) {
  1711. urbl->urb = urb;
  1712. urbl->port = (struct oz_port *)urb->hcpriv;
  1713. urbl->ep_num = usb_pipeendpoint(urb->pipe);
  1714. if (usb_pipein(urb->pipe))
  1715. urbl->ep_num |= USB_DIR_IN;
  1716. list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
  1717. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1718. tasklet_schedule(&g_urb_cancel_tasklet);
  1719. } else {
  1720. spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
  1721. oz_free_urb_link(urbl);
  1722. }
  1723. return rc;
  1724. }
  1725. /*
  1726. * Context: unknown
  1727. */
  1728. static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
  1729. struct usb_host_endpoint *ep)
  1730. {
  1731. }
  1732. /*
  1733. * Context: unknown
  1734. */
  1735. static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
  1736. struct usb_host_endpoint *ep)
  1737. {
  1738. }
  1739. /*
  1740. * Context: unknown
  1741. */
  1742. static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
  1743. {
  1744. oz_dbg(ON, "oz_hcd_get_frame_number\n");
  1745. return oz_usb_get_frame_number();
  1746. }
  1747. /*
  1748. * Context: softirq
  1749. * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
  1750. * always do that in softirq context.
  1751. */
  1752. static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  1753. {
  1754. struct oz_hcd *ozhcd = oz_hcd_private(hcd);
  1755. int i;
  1756. buf[0] = 0;
  1757. buf[1] = 0;
  1758. spin_lock_bh(&ozhcd->hcd_lock);
  1759. for (i = 0; i < OZ_NB_PORTS; i++) {
  1760. if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
  1761. oz_dbg(HUB, "Port %d changed\n", i);
  1762. ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
  1763. if (i < 7)
  1764. buf[0] |= 1 << (i + 1);
  1765. else
  1766. buf[1] |= 1 << (i - 7);
  1767. }
  1768. }
  1769. spin_unlock_bh(&ozhcd->hcd_lock);
  1770. if (buf[0] != 0 || buf[1] != 0)
  1771. return 2;
  1772. return 0;
  1773. }
  1774. /*
  1775. * Context: process
  1776. */
  1777. static void oz_get_hub_descriptor(struct usb_hcd *hcd,
  1778. struct usb_hub_descriptor *desc)
  1779. {
  1780. memset(desc, 0, sizeof(*desc));
  1781. desc->bDescriptorType = 0x29;
  1782. desc->bDescLength = 9;
  1783. desc->wHubCharacteristics = (__force __u16)cpu_to_le16(0x0001);
  1784. desc->bNbrPorts = OZ_NB_PORTS;
  1785. }
  1786. /*
  1787. * Context: process
  1788. */
  1789. static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
  1790. {
  1791. struct oz_port *port;
  1792. u8 port_id = (u8)windex;
  1793. struct oz_hcd *ozhcd = oz_hcd_private(hcd);
  1794. unsigned set_bits = 0;
  1795. unsigned clear_bits = 0;
  1796. if ((port_id < 1) || (port_id > OZ_NB_PORTS))
  1797. return -EPIPE;
  1798. port = &ozhcd->ports[port_id-1];
  1799. switch (wvalue) {
  1800. case USB_PORT_FEAT_CONNECTION:
  1801. oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
  1802. break;
  1803. case USB_PORT_FEAT_ENABLE:
  1804. oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
  1805. break;
  1806. case USB_PORT_FEAT_SUSPEND:
  1807. oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
  1808. break;
  1809. case USB_PORT_FEAT_OVER_CURRENT:
  1810. oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
  1811. break;
  1812. case USB_PORT_FEAT_RESET:
  1813. oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
  1814. set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
  1815. clear_bits = USB_PORT_STAT_RESET;
  1816. ozhcd->ports[port_id-1].bus_addr = 0;
  1817. break;
  1818. case USB_PORT_FEAT_POWER:
  1819. oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
  1820. set_bits |= USB_PORT_STAT_POWER;
  1821. break;
  1822. case USB_PORT_FEAT_LOWSPEED:
  1823. oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
  1824. break;
  1825. case USB_PORT_FEAT_C_CONNECTION:
  1826. oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
  1827. break;
  1828. case USB_PORT_FEAT_C_ENABLE:
  1829. oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
  1830. break;
  1831. case USB_PORT_FEAT_C_SUSPEND:
  1832. oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
  1833. break;
  1834. case USB_PORT_FEAT_C_OVER_CURRENT:
  1835. oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
  1836. break;
  1837. case USB_PORT_FEAT_C_RESET:
  1838. oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
  1839. break;
  1840. case USB_PORT_FEAT_TEST:
  1841. oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
  1842. break;
  1843. case USB_PORT_FEAT_INDICATOR:
  1844. oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
  1845. break;
  1846. default:
  1847. oz_dbg(HUB, "Other %d\n", wvalue);
  1848. break;
  1849. }
  1850. if (set_bits || clear_bits) {
  1851. spin_lock_bh(&port->port_lock);
  1852. port->status &= ~clear_bits;
  1853. port->status |= set_bits;
  1854. spin_unlock_bh(&port->port_lock);
  1855. }
  1856. oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
  1857. return 0;
  1858. }
  1859. /*
  1860. * Context: process
  1861. */
  1862. static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
  1863. {
  1864. struct oz_port *port;
  1865. u8 port_id = (u8)windex;
  1866. struct oz_hcd *ozhcd = oz_hcd_private(hcd);
  1867. unsigned clear_bits = 0;
  1868. if ((port_id < 1) || (port_id > OZ_NB_PORTS))
  1869. return -EPIPE;
  1870. port = &ozhcd->ports[port_id-1];
  1871. switch (wvalue) {
  1872. case USB_PORT_FEAT_CONNECTION:
  1873. oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
  1874. break;
  1875. case USB_PORT_FEAT_ENABLE:
  1876. oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
  1877. clear_bits = USB_PORT_STAT_ENABLE;
  1878. break;
  1879. case USB_PORT_FEAT_SUSPEND:
  1880. oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
  1881. break;
  1882. case USB_PORT_FEAT_OVER_CURRENT:
  1883. oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
  1884. break;
  1885. case USB_PORT_FEAT_RESET:
  1886. oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
  1887. break;
  1888. case USB_PORT_FEAT_POWER:
  1889. oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
  1890. clear_bits |= USB_PORT_STAT_POWER;
  1891. break;
  1892. case USB_PORT_FEAT_LOWSPEED:
  1893. oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
  1894. break;
  1895. case USB_PORT_FEAT_C_CONNECTION:
  1896. oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
  1897. clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
  1898. break;
  1899. case USB_PORT_FEAT_C_ENABLE:
  1900. oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
  1901. clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
  1902. break;
  1903. case USB_PORT_FEAT_C_SUSPEND:
  1904. oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
  1905. break;
  1906. case USB_PORT_FEAT_C_OVER_CURRENT:
  1907. oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
  1908. break;
  1909. case USB_PORT_FEAT_C_RESET:
  1910. oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
  1911. clear_bits = (USB_PORT_FEAT_C_RESET << 16);
  1912. break;
  1913. case USB_PORT_FEAT_TEST:
  1914. oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
  1915. break;
  1916. case USB_PORT_FEAT_INDICATOR:
  1917. oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
  1918. break;
  1919. default:
  1920. oz_dbg(HUB, "Other %d\n", wvalue);
  1921. break;
  1922. }
  1923. if (clear_bits) {
  1924. spin_lock_bh(&port->port_lock);
  1925. port->status &= ~clear_bits;
  1926. spin_unlock_bh(&port->port_lock);
  1927. }
  1928. oz_dbg(HUB, "Port[%d] status = 0x%x\n",
  1929. port_id, ozhcd->ports[port_id-1].status);
  1930. return 0;
  1931. }
  1932. /*
  1933. * Context: process
  1934. */
  1935. static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
  1936. {
  1937. struct oz_hcd *ozhcd;
  1938. u32 status;
  1939. if ((windex < 1) || (windex > OZ_NB_PORTS))
  1940. return -EPIPE;
  1941. ozhcd = oz_hcd_private(hcd);
  1942. oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
  1943. status = ozhcd->ports[windex-1].status;
  1944. put_unaligned(cpu_to_le32(status), (__le32 *)buf);
  1945. oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
  1946. return 0;
  1947. }
  1948. /*
  1949. * Context: process
  1950. */
  1951. static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
  1952. u16 windex, char *buf, u16 wlength)
  1953. {
  1954. int err = 0;
  1955. switch (req_type) {
  1956. case ClearHubFeature:
  1957. oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
  1958. break;
  1959. case ClearPortFeature:
  1960. err = oz_clear_port_feature(hcd, wvalue, windex);
  1961. break;
  1962. case GetHubDescriptor:
  1963. oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
  1964. break;
  1965. case GetHubStatus:
  1966. oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
  1967. put_unaligned(cpu_to_le32(0), (__le32 *)buf);
  1968. break;
  1969. case GetPortStatus:
  1970. err = oz_get_port_status(hcd, windex, buf);
  1971. break;
  1972. case SetHubFeature:
  1973. oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
  1974. break;
  1975. case SetPortFeature:
  1976. err = oz_set_port_feature(hcd, wvalue, windex);
  1977. break;
  1978. default:
  1979. oz_dbg(HUB, "Other: %d\n", req_type);
  1980. break;
  1981. }
  1982. return err;
  1983. }
  1984. /*
  1985. * Context: process
  1986. */
  1987. static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
  1988. {
  1989. struct oz_hcd *ozhcd;
  1990. ozhcd = oz_hcd_private(hcd);
  1991. spin_lock_bh(&ozhcd->hcd_lock);
  1992. hcd->state = HC_STATE_SUSPENDED;
  1993. ozhcd->flags |= OZ_HDC_F_SUSPENDED;
  1994. spin_unlock_bh(&ozhcd->hcd_lock);
  1995. return 0;
  1996. }
  1997. /*
  1998. * Context: process
  1999. */
  2000. static int oz_hcd_bus_resume(struct usb_hcd *hcd)
  2001. {
  2002. struct oz_hcd *ozhcd;
  2003. ozhcd = oz_hcd_private(hcd);
  2004. spin_lock_bh(&ozhcd->hcd_lock);
  2005. ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
  2006. hcd->state = HC_STATE_RUNNING;
  2007. spin_unlock_bh(&ozhcd->hcd_lock);
  2008. return 0;
  2009. }
  2010. static void oz_plat_shutdown(struct platform_device *dev)
  2011. {
  2012. }
  2013. /*
  2014. * Context: process
  2015. */
  2016. static int oz_plat_probe(struct platform_device *dev)
  2017. {
  2018. int i;
  2019. int err;
  2020. struct usb_hcd *hcd;
  2021. struct oz_hcd *ozhcd;
  2022. hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
  2023. if (hcd == NULL) {
  2024. oz_dbg(ON, "Failed to created hcd object OK\n");
  2025. return -ENOMEM;
  2026. }
  2027. ozhcd = oz_hcd_private(hcd);
  2028. memset(ozhcd, 0, sizeof(*ozhcd));
  2029. INIT_LIST_HEAD(&ozhcd->urb_pending_list);
  2030. INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
  2031. INIT_LIST_HEAD(&ozhcd->orphanage);
  2032. ozhcd->hcd = hcd;
  2033. ozhcd->conn_port = -1;
  2034. spin_lock_init(&ozhcd->hcd_lock);
  2035. for (i = 0; i < OZ_NB_PORTS; i++) {
  2036. struct oz_port *port = &ozhcd->ports[i];
  2037. port->ozhcd = ozhcd;
  2038. port->flags = 0;
  2039. port->status = 0;
  2040. port->bus_addr = 0xff;
  2041. spin_lock_init(&port->port_lock);
  2042. }
  2043. err = usb_add_hcd(hcd, 0, 0);
  2044. if (err) {
  2045. oz_dbg(ON, "Failed to add hcd object OK\n");
  2046. usb_put_hcd(hcd);
  2047. return -1;
  2048. }
  2049. device_wakeup_enable(hcd->self.controller);
  2050. spin_lock_bh(&g_hcdlock);
  2051. g_ozhcd = ozhcd;
  2052. spin_unlock_bh(&g_hcdlock);
  2053. return 0;
  2054. }
  2055. /*
  2056. * Context: unknown
  2057. */
  2058. static int oz_plat_remove(struct platform_device *dev)
  2059. {
  2060. struct usb_hcd *hcd = platform_get_drvdata(dev);
  2061. struct oz_hcd *ozhcd;
  2062. if (hcd == NULL)
  2063. return -1;
  2064. ozhcd = oz_hcd_private(hcd);
  2065. spin_lock_bh(&g_hcdlock);
  2066. if (ozhcd == g_ozhcd)
  2067. g_ozhcd = NULL;
  2068. spin_unlock_bh(&g_hcdlock);
  2069. oz_dbg(ON, "Clearing orphanage\n");
  2070. oz_hcd_clear_orphanage(ozhcd, -EPIPE);
  2071. oz_dbg(ON, "Removing hcd\n");
  2072. usb_remove_hcd(hcd);
  2073. usb_put_hcd(hcd);
  2074. return 0;
  2075. }
  2076. /*
  2077. * Context: unknown
  2078. */
  2079. static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
  2080. {
  2081. return 0;
  2082. }
  2083. /*
  2084. * Context: unknown
  2085. */
  2086. static int oz_plat_resume(struct platform_device *dev)
  2087. {
  2088. return 0;
  2089. }
  2090. /*
  2091. * Context: process
  2092. */
  2093. int oz_hcd_init(void)
  2094. {
  2095. int err;
  2096. if (usb_disabled())
  2097. return -ENODEV;
  2098. oz_urb_link_cache = KMEM_CACHE(oz_urb_link, 0);
  2099. if (!oz_urb_link_cache)
  2100. return -ENOMEM;
  2101. tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
  2102. tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
  2103. err = platform_driver_register(&g_oz_plat_drv);
  2104. oz_dbg(ON, "platform_driver_register() returned %d\n", err);
  2105. if (err)
  2106. goto error;
  2107. g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
  2108. if (g_plat_dev == NULL) {
  2109. err = -ENOMEM;
  2110. goto error1;
  2111. }
  2112. oz_dbg(ON, "platform_device_alloc() succeeded\n");
  2113. err = platform_device_add(g_plat_dev);
  2114. if (err)
  2115. goto error2;
  2116. oz_dbg(ON, "platform_device_add() succeeded\n");
  2117. return 0;
  2118. error2:
  2119. platform_device_put(g_plat_dev);
  2120. error1:
  2121. platform_driver_unregister(&g_oz_plat_drv);
  2122. error:
  2123. tasklet_disable(&g_urb_process_tasklet);
  2124. tasklet_disable(&g_urb_cancel_tasklet);
  2125. oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
  2126. return err;
  2127. }
  2128. /*
  2129. * Context: process
  2130. */
  2131. void oz_hcd_term(void)
  2132. {
  2133. msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
  2134. tasklet_kill(&g_urb_process_tasklet);
  2135. tasklet_kill(&g_urb_cancel_tasklet);
  2136. platform_device_unregister(g_plat_dev);
  2137. platform_driver_unregister(&g_oz_plat_drv);
  2138. oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
  2139. kmem_cache_destroy(oz_urb_link_cache);
  2140. }