dummy_hcd.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. /*
  2. * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * Copyright (C) 2003 David Brownell
  7. * Copyright (C) 2003-2005 Alan Stern
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. /*
  15. * This exposes a device side "USB gadget" API, driven by requests to a
  16. * Linux-USB host controller driver. USB traffic is simulated; there's
  17. * no need for USB hardware. Use this with two other drivers:
  18. *
  19. * - Gadget driver, responding to requests (slave);
  20. * - Host-side device driver, as already familiar in Linux.
  21. *
  22. * Having this all in one kernel can help some stages of development,
  23. * bypassing some hardware (and driver) issues. UML could help too.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/delay.h>
  28. #include <linux/ioport.h>
  29. #include <linux/slab.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/timer.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/usb.h>
  37. #include <linux/usb/gadget.h>
  38. #include <linux/usb/hcd.h>
  39. #include <asm/byteorder.h>
  40. #include <asm/io.h>
  41. #include <asm/irq.h>
  42. #include <asm/system.h>
  43. #include <asm/unaligned.h>
  44. #define DRIVER_DESC "USB Host+Gadget Emulator"
  45. #define DRIVER_VERSION "02 May 2005"
  46. #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
  47. static const char driver_name [] = "dummy_hcd";
  48. static const char driver_desc [] = "USB Host+Gadget Emulator";
  49. static const char gadget_name [] = "dummy_udc";
  50. MODULE_DESCRIPTION (DRIVER_DESC);
  51. MODULE_AUTHOR ("David Brownell");
  52. MODULE_LICENSE ("GPL");
  53. struct dummy_hcd_module_parameters {
  54. bool is_super_speed;
  55. bool is_high_speed;
  56. };
  57. static struct dummy_hcd_module_parameters mod_data = {
  58. .is_super_speed = false,
  59. .is_high_speed = true,
  60. };
  61. module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
  62. MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
  63. module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
  64. MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
  65. /*-------------------------------------------------------------------------*/
  66. /* gadget side driver data structres */
  67. struct dummy_ep {
  68. struct list_head queue;
  69. unsigned long last_io; /* jiffies timestamp */
  70. struct usb_gadget *gadget;
  71. const struct usb_endpoint_descriptor *desc;
  72. struct usb_ep ep;
  73. unsigned halted : 1;
  74. unsigned wedged : 1;
  75. unsigned already_seen : 1;
  76. unsigned setup_stage : 1;
  77. };
  78. struct dummy_request {
  79. struct list_head queue; /* ep's requests */
  80. struct usb_request req;
  81. };
  82. static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
  83. {
  84. return container_of (_ep, struct dummy_ep, ep);
  85. }
  86. static inline struct dummy_request *usb_request_to_dummy_request
  87. (struct usb_request *_req)
  88. {
  89. return container_of (_req, struct dummy_request, req);
  90. }
  91. /*-------------------------------------------------------------------------*/
  92. /*
  93. * Every device has ep0 for control requests, plus up to 30 more endpoints,
  94. * in one of two types:
  95. *
  96. * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
  97. * number can be changed. Names like "ep-a" are used for this type.
  98. *
  99. * - Fixed Function: in other cases. some characteristics may be mutable;
  100. * that'd be hardware-specific. Names like "ep12out-bulk" are used.
  101. *
  102. * Gadget drivers are responsible for not setting up conflicting endpoint
  103. * configurations, illegal or unsupported packet lengths, and so on.
  104. */
  105. static const char ep0name [] = "ep0";
  106. static const char *const ep_name [] = {
  107. ep0name, /* everyone has ep0 */
  108. /* act like a net2280: high speed, six configurable endpoints */
  109. "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
  110. /* or like pxa250: fifteen fixed function endpoints */
  111. "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
  112. "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
  113. "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
  114. "ep15in-int",
  115. /* or like sa1100: two fixed function endpoints */
  116. "ep1out-bulk", "ep2in-bulk",
  117. };
  118. #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
  119. /*-------------------------------------------------------------------------*/
  120. #define FIFO_SIZE 64
  121. struct urbp {
  122. struct urb *urb;
  123. struct list_head urbp_list;
  124. };
  125. enum dummy_rh_state {
  126. DUMMY_RH_RESET,
  127. DUMMY_RH_SUSPENDED,
  128. DUMMY_RH_RUNNING
  129. };
  130. struct dummy_hcd {
  131. struct dummy *dum;
  132. enum dummy_rh_state rh_state;
  133. struct timer_list timer;
  134. u32 port_status;
  135. u32 old_status;
  136. unsigned long re_timeout;
  137. struct usb_device *udev;
  138. struct list_head urbp_list;
  139. unsigned active:1;
  140. unsigned old_active:1;
  141. unsigned resuming:1;
  142. };
  143. struct dummy {
  144. spinlock_t lock;
  145. /*
  146. * SLAVE/GADGET side support
  147. */
  148. struct dummy_ep ep [DUMMY_ENDPOINTS];
  149. int address;
  150. struct usb_gadget gadget;
  151. struct usb_gadget_driver *driver;
  152. struct dummy_request fifo_req;
  153. u8 fifo_buf [FIFO_SIZE];
  154. u16 devstatus;
  155. unsigned udc_suspended:1;
  156. unsigned pullup:1;
  157. /*
  158. * MASTER/HOST side support
  159. */
  160. struct dummy_hcd *hs_hcd;
  161. struct dummy_hcd *ss_hcd;
  162. };
  163. static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
  164. {
  165. return (struct dummy_hcd *) (hcd->hcd_priv);
  166. }
  167. static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
  168. {
  169. return container_of((void *) dum, struct usb_hcd, hcd_priv);
  170. }
  171. static inline struct device *dummy_dev(struct dummy_hcd *dum)
  172. {
  173. return dummy_hcd_to_hcd(dum)->self.controller;
  174. }
  175. static inline struct device *udc_dev (struct dummy *dum)
  176. {
  177. return dum->gadget.dev.parent;
  178. }
  179. static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
  180. {
  181. return container_of (ep->gadget, struct dummy, gadget);
  182. }
  183. static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
  184. {
  185. struct dummy *dum = container_of(gadget, struct dummy, gadget);
  186. if (dum->gadget.speed == USB_SPEED_SUPER)
  187. return dum->ss_hcd;
  188. else
  189. return dum->hs_hcd;
  190. }
  191. static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
  192. {
  193. return container_of (dev, struct dummy, gadget.dev);
  194. }
  195. static struct dummy the_controller;
  196. /*-------------------------------------------------------------------------*/
  197. /* SLAVE/GADGET SIDE UTILITY ROUTINES */
  198. /* called with spinlock held */
  199. static void nuke (struct dummy *dum, struct dummy_ep *ep)
  200. {
  201. while (!list_empty (&ep->queue)) {
  202. struct dummy_request *req;
  203. req = list_entry (ep->queue.next, struct dummy_request, queue);
  204. list_del_init (&req->queue);
  205. req->req.status = -ESHUTDOWN;
  206. spin_unlock (&dum->lock);
  207. req->req.complete (&ep->ep, &req->req);
  208. spin_lock (&dum->lock);
  209. }
  210. }
  211. /* caller must hold lock */
  212. static void
  213. stop_activity (struct dummy *dum)
  214. {
  215. struct dummy_ep *ep;
  216. /* prevent any more requests */
  217. dum->address = 0;
  218. /* The timer is left running so that outstanding URBs can fail */
  219. /* nuke any pending requests first, so driver i/o is quiesced */
  220. list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
  221. nuke (dum, ep);
  222. /* driver now does any non-usb quiescing necessary */
  223. }
  224. /**
  225. * set_link_state_by_speed() - Sets the current state of the link according to
  226. * the hcd speed
  227. * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
  228. *
  229. * This function updates the port_status according to the link state and the
  230. * speed of the hcd.
  231. */
  232. static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
  233. {
  234. struct dummy *dum = dum_hcd->dum;
  235. if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
  236. if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
  237. dum_hcd->port_status = 0;
  238. } else if (!dum->pullup || dum->udc_suspended) {
  239. /* UDC suspend must cause a disconnect */
  240. dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
  241. USB_PORT_STAT_ENABLE);
  242. if ((dum_hcd->old_status &
  243. USB_PORT_STAT_CONNECTION) != 0)
  244. dum_hcd->port_status |=
  245. (USB_PORT_STAT_C_CONNECTION << 16);
  246. } else {
  247. /* device is connected and not suspended */
  248. dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
  249. USB_PORT_STAT_SPEED_5GBPS) ;
  250. if ((dum_hcd->old_status &
  251. USB_PORT_STAT_CONNECTION) == 0)
  252. dum_hcd->port_status |=
  253. (USB_PORT_STAT_C_CONNECTION << 16);
  254. if ((dum_hcd->port_status &
  255. USB_PORT_STAT_ENABLE) == 1 &&
  256. (dum_hcd->port_status &
  257. USB_SS_PORT_LS_U0) == 1 &&
  258. dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
  259. dum_hcd->active = 1;
  260. }
  261. } else {
  262. if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
  263. dum_hcd->port_status = 0;
  264. } else if (!dum->pullup || dum->udc_suspended) {
  265. /* UDC suspend must cause a disconnect */
  266. dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
  267. USB_PORT_STAT_ENABLE |
  268. USB_PORT_STAT_LOW_SPEED |
  269. USB_PORT_STAT_HIGH_SPEED |
  270. USB_PORT_STAT_SUSPEND);
  271. if ((dum_hcd->old_status &
  272. USB_PORT_STAT_CONNECTION) != 0)
  273. dum_hcd->port_status |=
  274. (USB_PORT_STAT_C_CONNECTION << 16);
  275. } else {
  276. dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
  277. if ((dum_hcd->old_status &
  278. USB_PORT_STAT_CONNECTION) == 0)
  279. dum_hcd->port_status |=
  280. (USB_PORT_STAT_C_CONNECTION << 16);
  281. if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
  282. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  283. else if ((dum_hcd->port_status &
  284. USB_PORT_STAT_SUSPEND) == 0 &&
  285. dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
  286. dum_hcd->active = 1;
  287. }
  288. }
  289. }
  290. /* caller must hold lock */
  291. static void set_link_state(struct dummy_hcd *dum_hcd)
  292. {
  293. struct dummy *dum = dum_hcd->dum;
  294. dum_hcd->active = 0;
  295. if (dum->pullup)
  296. if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
  297. dum->gadget.speed != USB_SPEED_SUPER) ||
  298. (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
  299. dum->gadget.speed == USB_SPEED_SUPER))
  300. return;
  301. set_link_state_by_speed(dum_hcd);
  302. if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
  303. dum_hcd->active)
  304. dum_hcd->resuming = 0;
  305. /* if !connected or reset */
  306. if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
  307. (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
  308. /*
  309. * We're connected and not reset (reset occurred now),
  310. * and driver attached - disconnect!
  311. */
  312. if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
  313. (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
  314. dum->driver) {
  315. stop_activity(dum);
  316. spin_unlock(&dum->lock);
  317. dum->driver->disconnect(&dum->gadget);
  318. spin_lock(&dum->lock);
  319. }
  320. } else if (dum_hcd->active != dum_hcd->old_active) {
  321. if (dum_hcd->old_active && dum->driver->suspend) {
  322. spin_unlock(&dum->lock);
  323. dum->driver->suspend(&dum->gadget);
  324. spin_lock(&dum->lock);
  325. } else if (!dum_hcd->old_active && dum->driver->resume) {
  326. spin_unlock(&dum->lock);
  327. dum->driver->resume(&dum->gadget);
  328. spin_lock(&dum->lock);
  329. }
  330. }
  331. dum_hcd->old_status = dum_hcd->port_status;
  332. dum_hcd->old_active = dum_hcd->active;
  333. }
  334. /*-------------------------------------------------------------------------*/
  335. /* SLAVE/GADGET SIDE DRIVER
  336. *
  337. * This only tracks gadget state. All the work is done when the host
  338. * side tries some (emulated) i/o operation. Real device controller
  339. * drivers would do real i/o using dma, fifos, irqs, timers, etc.
  340. */
  341. #define is_enabled(dum) \
  342. (dum->port_status & USB_PORT_STAT_ENABLE)
  343. static int
  344. dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  345. {
  346. struct dummy *dum;
  347. struct dummy_hcd *dum_hcd;
  348. struct dummy_ep *ep;
  349. unsigned max;
  350. int retval;
  351. ep = usb_ep_to_dummy_ep (_ep);
  352. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  353. || desc->bDescriptorType != USB_DT_ENDPOINT)
  354. return -EINVAL;
  355. dum = ep_to_dummy (ep);
  356. if (!dum->driver)
  357. return -ESHUTDOWN;
  358. dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  359. if (!is_enabled(dum_hcd))
  360. return -ESHUTDOWN;
  361. /*
  362. * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
  363. * maximum packet size.
  364. * For SS devices the wMaxPacketSize is limited by 1024.
  365. */
  366. max = usb_endpoint_maxp(desc) & 0x7ff;
  367. /* drivers must not request bad settings, since lower levels
  368. * (hardware or its drivers) may not check. some endpoints
  369. * can't do iso, many have maxpacket limitations, etc.
  370. *
  371. * since this "hardware" driver is here to help debugging, we
  372. * have some extra sanity checks. (there could be more though,
  373. * especially for "ep9out" style fixed function ones.)
  374. */
  375. retval = -EINVAL;
  376. switch (desc->bmAttributes & 0x03) {
  377. case USB_ENDPOINT_XFER_BULK:
  378. if (strstr (ep->ep.name, "-iso")
  379. || strstr (ep->ep.name, "-int")) {
  380. goto done;
  381. }
  382. switch (dum->gadget.speed) {
  383. case USB_SPEED_SUPER:
  384. if (max == 1024)
  385. break;
  386. goto done;
  387. case USB_SPEED_HIGH:
  388. if (max == 512)
  389. break;
  390. goto done;
  391. case USB_SPEED_FULL:
  392. if (max == 8 || max == 16 || max == 32 || max == 64)
  393. /* we'll fake any legal size */
  394. break;
  395. /* save a return statement */
  396. default:
  397. goto done;
  398. }
  399. break;
  400. case USB_ENDPOINT_XFER_INT:
  401. if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
  402. goto done;
  403. /* real hardware might not handle all packet sizes */
  404. switch (dum->gadget.speed) {
  405. case USB_SPEED_SUPER:
  406. case USB_SPEED_HIGH:
  407. if (max <= 1024)
  408. break;
  409. /* save a return statement */
  410. case USB_SPEED_FULL:
  411. if (max <= 64)
  412. break;
  413. /* save a return statement */
  414. default:
  415. if (max <= 8)
  416. break;
  417. goto done;
  418. }
  419. break;
  420. case USB_ENDPOINT_XFER_ISOC:
  421. if (strstr (ep->ep.name, "-bulk")
  422. || strstr (ep->ep.name, "-int"))
  423. goto done;
  424. /* real hardware might not handle all packet sizes */
  425. switch (dum->gadget.speed) {
  426. case USB_SPEED_SUPER:
  427. case USB_SPEED_HIGH:
  428. if (max <= 1024)
  429. break;
  430. /* save a return statement */
  431. case USB_SPEED_FULL:
  432. if (max <= 1023)
  433. break;
  434. /* save a return statement */
  435. default:
  436. goto done;
  437. }
  438. break;
  439. default:
  440. /* few chips support control except on ep0 */
  441. goto done;
  442. }
  443. _ep->maxpacket = max;
  444. ep->desc = desc;
  445. dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
  446. _ep->name,
  447. desc->bEndpointAddress & 0x0f,
  448. (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  449. ({ char *val;
  450. switch (desc->bmAttributes & 0x03) {
  451. case USB_ENDPOINT_XFER_BULK:
  452. val = "bulk";
  453. break;
  454. case USB_ENDPOINT_XFER_ISOC:
  455. val = "iso";
  456. break;
  457. case USB_ENDPOINT_XFER_INT:
  458. val = "intr";
  459. break;
  460. default:
  461. val = "ctrl";
  462. break;
  463. }; val; }),
  464. max);
  465. /* at this point real hardware should be NAKing transfers
  466. * to that endpoint, until a buffer is queued to it.
  467. */
  468. ep->halted = ep->wedged = 0;
  469. retval = 0;
  470. done:
  471. return retval;
  472. }
  473. static int dummy_disable (struct usb_ep *_ep)
  474. {
  475. struct dummy_ep *ep;
  476. struct dummy *dum;
  477. unsigned long flags;
  478. int retval;
  479. ep = usb_ep_to_dummy_ep (_ep);
  480. if (!_ep || !ep->desc || _ep->name == ep0name)
  481. return -EINVAL;
  482. dum = ep_to_dummy (ep);
  483. spin_lock_irqsave (&dum->lock, flags);
  484. ep->desc = NULL;
  485. retval = 0;
  486. nuke (dum, ep);
  487. spin_unlock_irqrestore (&dum->lock, flags);
  488. dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
  489. return retval;
  490. }
  491. static struct usb_request *
  492. dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
  493. {
  494. struct dummy_ep *ep;
  495. struct dummy_request *req;
  496. if (!_ep)
  497. return NULL;
  498. ep = usb_ep_to_dummy_ep (_ep);
  499. req = kzalloc(sizeof(*req), mem_flags);
  500. if (!req)
  501. return NULL;
  502. INIT_LIST_HEAD (&req->queue);
  503. return &req->req;
  504. }
  505. static void
  506. dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
  507. {
  508. struct dummy_ep *ep;
  509. struct dummy_request *req;
  510. ep = usb_ep_to_dummy_ep (_ep);
  511. if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
  512. return;
  513. req = usb_request_to_dummy_request (_req);
  514. WARN_ON (!list_empty (&req->queue));
  515. kfree (req);
  516. }
  517. static void
  518. fifo_complete (struct usb_ep *ep, struct usb_request *req)
  519. {
  520. }
  521. static int
  522. dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
  523. gfp_t mem_flags)
  524. {
  525. struct dummy_ep *ep;
  526. struct dummy_request *req;
  527. struct dummy *dum;
  528. struct dummy_hcd *dum_hcd;
  529. unsigned long flags;
  530. req = usb_request_to_dummy_request (_req);
  531. if (!_req || !list_empty (&req->queue) || !_req->complete)
  532. return -EINVAL;
  533. ep = usb_ep_to_dummy_ep (_ep);
  534. if (!_ep || (!ep->desc && _ep->name != ep0name))
  535. return -EINVAL;
  536. dum = ep_to_dummy (ep);
  537. dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  538. if (!dum->driver || !is_enabled(dum_hcd))
  539. return -ESHUTDOWN;
  540. #if 0
  541. dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
  542. ep, _req, _ep->name, _req->length, _req->buf);
  543. #endif
  544. _req->status = -EINPROGRESS;
  545. _req->actual = 0;
  546. spin_lock_irqsave (&dum->lock, flags);
  547. /* implement an emulated single-request FIFO */
  548. if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  549. list_empty (&dum->fifo_req.queue) &&
  550. list_empty (&ep->queue) &&
  551. _req->length <= FIFO_SIZE) {
  552. req = &dum->fifo_req;
  553. req->req = *_req;
  554. req->req.buf = dum->fifo_buf;
  555. memcpy (dum->fifo_buf, _req->buf, _req->length);
  556. req->req.context = dum;
  557. req->req.complete = fifo_complete;
  558. list_add_tail(&req->queue, &ep->queue);
  559. spin_unlock (&dum->lock);
  560. _req->actual = _req->length;
  561. _req->status = 0;
  562. _req->complete (_ep, _req);
  563. spin_lock (&dum->lock);
  564. } else
  565. list_add_tail(&req->queue, &ep->queue);
  566. spin_unlock_irqrestore (&dum->lock, flags);
  567. /* real hardware would likely enable transfers here, in case
  568. * it'd been left NAKing.
  569. */
  570. return 0;
  571. }
  572. static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
  573. {
  574. struct dummy_ep *ep;
  575. struct dummy *dum;
  576. int retval = -EINVAL;
  577. unsigned long flags;
  578. struct dummy_request *req = NULL;
  579. if (!_ep || !_req)
  580. return retval;
  581. ep = usb_ep_to_dummy_ep (_ep);
  582. dum = ep_to_dummy (ep);
  583. if (!dum->driver)
  584. return -ESHUTDOWN;
  585. local_irq_save (flags);
  586. spin_lock (&dum->lock);
  587. list_for_each_entry (req, &ep->queue, queue) {
  588. if (&req->req == _req) {
  589. list_del_init (&req->queue);
  590. _req->status = -ECONNRESET;
  591. retval = 0;
  592. break;
  593. }
  594. }
  595. spin_unlock (&dum->lock);
  596. if (retval == 0) {
  597. dev_dbg (udc_dev(dum),
  598. "dequeued req %p from %s, len %d buf %p\n",
  599. req, _ep->name, _req->length, _req->buf);
  600. _req->complete (_ep, _req);
  601. }
  602. local_irq_restore (flags);
  603. return retval;
  604. }
  605. static int
  606. dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
  607. {
  608. struct dummy_ep *ep;
  609. struct dummy *dum;
  610. if (!_ep)
  611. return -EINVAL;
  612. ep = usb_ep_to_dummy_ep (_ep);
  613. dum = ep_to_dummy (ep);
  614. if (!dum->driver)
  615. return -ESHUTDOWN;
  616. if (!value)
  617. ep->halted = ep->wedged = 0;
  618. else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  619. !list_empty (&ep->queue))
  620. return -EAGAIN;
  621. else {
  622. ep->halted = 1;
  623. if (wedged)
  624. ep->wedged = 1;
  625. }
  626. /* FIXME clear emulated data toggle too */
  627. return 0;
  628. }
  629. static int
  630. dummy_set_halt(struct usb_ep *_ep, int value)
  631. {
  632. return dummy_set_halt_and_wedge(_ep, value, 0);
  633. }
  634. static int dummy_set_wedge(struct usb_ep *_ep)
  635. {
  636. if (!_ep || _ep->name == ep0name)
  637. return -EINVAL;
  638. return dummy_set_halt_and_wedge(_ep, 1, 1);
  639. }
  640. static const struct usb_ep_ops dummy_ep_ops = {
  641. .enable = dummy_enable,
  642. .disable = dummy_disable,
  643. .alloc_request = dummy_alloc_request,
  644. .free_request = dummy_free_request,
  645. .queue = dummy_queue,
  646. .dequeue = dummy_dequeue,
  647. .set_halt = dummy_set_halt,
  648. .set_wedge = dummy_set_wedge,
  649. };
  650. /*-------------------------------------------------------------------------*/
  651. /* there are both host and device side versions of this call ... */
  652. static int dummy_g_get_frame (struct usb_gadget *_gadget)
  653. {
  654. struct timeval tv;
  655. do_gettimeofday (&tv);
  656. return tv.tv_usec / 1000;
  657. }
  658. static int dummy_wakeup (struct usb_gadget *_gadget)
  659. {
  660. struct dummy_hcd *dum_hcd;
  661. dum_hcd = gadget_to_dummy_hcd(_gadget);
  662. if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
  663. | (1 << USB_DEVICE_REMOTE_WAKEUP))))
  664. return -EINVAL;
  665. if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
  666. return -ENOLINK;
  667. if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
  668. dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
  669. return -EIO;
  670. /* FIXME: What if the root hub is suspended but the port isn't? */
  671. /* hub notices our request, issues downstream resume, etc */
  672. dum_hcd->resuming = 1;
  673. dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
  674. mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
  675. return 0;
  676. }
  677. static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
  678. {
  679. struct dummy *dum;
  680. dum = (gadget_to_dummy_hcd(_gadget))->dum;
  681. if (value)
  682. dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
  683. else
  684. dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  685. return 0;
  686. }
  687. static void dummy_udc_udpate_ep0(struct dummy *dum)
  688. {
  689. u32 i;
  690. if (dum->gadget.speed == USB_SPEED_SUPER) {
  691. for (i = 0; i < DUMMY_ENDPOINTS; i++)
  692. dum->ep[i].ep.max_streams = 0x10;
  693. dum->ep[0].ep.maxpacket = 9;
  694. } else {
  695. for (i = 0; i < DUMMY_ENDPOINTS; i++)
  696. dum->ep[i].ep.max_streams = 0;
  697. dum->ep[0].ep.maxpacket = 64;
  698. }
  699. }
  700. static int dummy_pullup (struct usb_gadget *_gadget, int value)
  701. {
  702. struct dummy_hcd *dum_hcd;
  703. struct dummy *dum;
  704. unsigned long flags;
  705. dum = gadget_dev_to_dummy(&_gadget->dev);
  706. if (value && dum->driver) {
  707. if (mod_data.is_super_speed)
  708. dum->gadget.speed = dum->driver->max_speed;
  709. else if (mod_data.is_high_speed)
  710. dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
  711. dum->driver->max_speed);
  712. else
  713. dum->gadget.speed = USB_SPEED_FULL;
  714. dummy_udc_udpate_ep0(dum);
  715. if (dum->gadget.speed < dum->driver->max_speed)
  716. dev_dbg(udc_dev(dum), "This device can perform faster"
  717. " if you connect it to a %s port...\n",
  718. usb_speed_string(dum->driver->max_speed));
  719. }
  720. dum_hcd = gadget_to_dummy_hcd(_gadget);
  721. spin_lock_irqsave (&dum->lock, flags);
  722. dum->pullup = (value != 0);
  723. set_link_state(dum_hcd);
  724. spin_unlock_irqrestore (&dum->lock, flags);
  725. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  726. return 0;
  727. }
  728. static int dummy_udc_start(struct usb_gadget *g,
  729. struct usb_gadget_driver *driver);
  730. static int dummy_udc_stop(struct usb_gadget *g,
  731. struct usb_gadget_driver *driver);
  732. static const struct usb_gadget_ops dummy_ops = {
  733. .get_frame = dummy_g_get_frame,
  734. .wakeup = dummy_wakeup,
  735. .set_selfpowered = dummy_set_selfpowered,
  736. .pullup = dummy_pullup,
  737. .udc_start = dummy_udc_start,
  738. .udc_stop = dummy_udc_stop,
  739. };
  740. /*-------------------------------------------------------------------------*/
  741. /* "function" sysfs attribute */
  742. static ssize_t
  743. show_function (struct device *dev, struct device_attribute *attr, char *buf)
  744. {
  745. struct dummy *dum = gadget_dev_to_dummy (dev);
  746. if (!dum->driver || !dum->driver->function)
  747. return 0;
  748. return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
  749. }
  750. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  751. /*-------------------------------------------------------------------------*/
  752. /*
  753. * Driver registration/unregistration.
  754. *
  755. * This is basically hardware-specific; there's usually only one real USB
  756. * device (not host) controller since that's how USB devices are intended
  757. * to work. So most implementations of these api calls will rely on the
  758. * fact that only one driver will ever bind to the hardware. But curious
  759. * hardware can be built with discrete components, so the gadget API doesn't
  760. * require that assumption.
  761. *
  762. * For this emulator, it might be convenient to create a usb slave device
  763. * for each driver that registers: just add to a big root hub.
  764. */
  765. static int dummy_udc_start(struct usb_gadget *g,
  766. struct usb_gadget_driver *driver)
  767. {
  768. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  769. struct dummy *dum = dum_hcd->dum;
  770. if (driver->max_speed == USB_SPEED_UNKNOWN)
  771. return -EINVAL;
  772. /*
  773. * SLAVE side init ... the layer above hardware, which
  774. * can't enumerate without help from the driver we're binding.
  775. */
  776. dum->devstatus = 0;
  777. dum->driver = driver;
  778. dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
  779. driver->driver.name);
  780. return 0;
  781. }
  782. static int dummy_udc_stop(struct usb_gadget *g,
  783. struct usb_gadget_driver *driver)
  784. {
  785. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  786. struct dummy *dum = dum_hcd->dum;
  787. dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
  788. driver->driver.name);
  789. dum->driver = NULL;
  790. dummy_pullup(&dum->gadget, 0);
  791. return 0;
  792. }
  793. #undef is_enabled
  794. /* The gadget structure is stored inside the hcd structure and will be
  795. * released along with it. */
  796. static void
  797. dummy_gadget_release (struct device *dev)
  798. {
  799. return;
  800. }
  801. static void init_dummy_udc_hw(struct dummy *dum)
  802. {
  803. int i;
  804. INIT_LIST_HEAD(&dum->gadget.ep_list);
  805. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  806. struct dummy_ep *ep = &dum->ep[i];
  807. if (!ep_name[i])
  808. break;
  809. ep->ep.name = ep_name[i];
  810. ep->ep.ops = &dummy_ep_ops;
  811. list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
  812. ep->halted = ep->wedged = ep->already_seen =
  813. ep->setup_stage = 0;
  814. ep->ep.maxpacket = ~0;
  815. ep->last_io = jiffies;
  816. ep->gadget = &dum->gadget;
  817. ep->desc = NULL;
  818. INIT_LIST_HEAD(&ep->queue);
  819. }
  820. dum->gadget.ep0 = &dum->ep[0].ep;
  821. list_del_init(&dum->ep[0].ep.ep_list);
  822. INIT_LIST_HEAD(&dum->fifo_req.queue);
  823. #ifdef CONFIG_USB_OTG
  824. dum->gadget.is_otg = 1;
  825. #endif
  826. }
  827. static int dummy_udc_probe (struct platform_device *pdev)
  828. {
  829. struct dummy *dum = &the_controller;
  830. int rc;
  831. dum->gadget.name = gadget_name;
  832. dum->gadget.ops = &dummy_ops;
  833. dum->gadget.max_speed = USB_SPEED_SUPER;
  834. dev_set_name(&dum->gadget.dev, "gadget");
  835. dum->gadget.dev.parent = &pdev->dev;
  836. dum->gadget.dev.release = dummy_gadget_release;
  837. rc = device_register (&dum->gadget.dev);
  838. if (rc < 0) {
  839. put_device(&dum->gadget.dev);
  840. return rc;
  841. }
  842. init_dummy_udc_hw(dum);
  843. rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
  844. if (rc < 0)
  845. goto err_udc;
  846. rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
  847. if (rc < 0)
  848. goto err_dev;
  849. platform_set_drvdata(pdev, dum);
  850. return rc;
  851. err_dev:
  852. usb_del_gadget_udc(&dum->gadget);
  853. err_udc:
  854. device_unregister(&dum->gadget.dev);
  855. return rc;
  856. }
  857. static int dummy_udc_remove (struct platform_device *pdev)
  858. {
  859. struct dummy *dum = platform_get_drvdata (pdev);
  860. usb_del_gadget_udc(&dum->gadget);
  861. platform_set_drvdata (pdev, NULL);
  862. device_remove_file (&dum->gadget.dev, &dev_attr_function);
  863. device_unregister (&dum->gadget.dev);
  864. return 0;
  865. }
  866. static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
  867. int suspend)
  868. {
  869. spin_lock_irq(&dum->lock);
  870. dum->udc_suspended = suspend;
  871. set_link_state(dum_hcd);
  872. spin_unlock_irq(&dum->lock);
  873. }
  874. static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
  875. {
  876. struct dummy *dum = platform_get_drvdata(pdev);
  877. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  878. dev_dbg(&pdev->dev, "%s\n", __func__);
  879. dummy_udc_pm(dum, dum_hcd, 1);
  880. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  881. return 0;
  882. }
  883. static int dummy_udc_resume(struct platform_device *pdev)
  884. {
  885. struct dummy *dum = platform_get_drvdata(pdev);
  886. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  887. dev_dbg(&pdev->dev, "%s\n", __func__);
  888. dummy_udc_pm(dum, dum_hcd, 0);
  889. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  890. return 0;
  891. }
  892. static struct platform_driver dummy_udc_driver = {
  893. .probe = dummy_udc_probe,
  894. .remove = dummy_udc_remove,
  895. .suspend = dummy_udc_suspend,
  896. .resume = dummy_udc_resume,
  897. .driver = {
  898. .name = (char *) gadget_name,
  899. .owner = THIS_MODULE,
  900. },
  901. };
  902. /*-------------------------------------------------------------------------*/
  903. /* MASTER/HOST SIDE DRIVER
  904. *
  905. * this uses the hcd framework to hook up to host side drivers.
  906. * its root hub will only have one device, otherwise it acts like
  907. * a normal host controller.
  908. *
  909. * when urbs are queued, they're just stuck on a list that we
  910. * scan in a timer callback. that callback connects writes from
  911. * the host with reads from the device, and so on, based on the
  912. * usb 2.0 rules.
  913. */
  914. static int dummy_urb_enqueue (
  915. struct usb_hcd *hcd,
  916. struct urb *urb,
  917. gfp_t mem_flags
  918. ) {
  919. struct dummy_hcd *dum_hcd;
  920. struct urbp *urbp;
  921. unsigned long flags;
  922. int rc;
  923. if (!urb->transfer_buffer && urb->transfer_buffer_length)
  924. return -EINVAL;
  925. urbp = kmalloc (sizeof *urbp, mem_flags);
  926. if (!urbp)
  927. return -ENOMEM;
  928. urbp->urb = urb;
  929. dum_hcd = hcd_to_dummy_hcd(hcd);
  930. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  931. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  932. if (rc) {
  933. kfree(urbp);
  934. goto done;
  935. }
  936. if (!dum_hcd->udev) {
  937. dum_hcd->udev = urb->dev;
  938. usb_get_dev(dum_hcd->udev);
  939. } else if (unlikely(dum_hcd->udev != urb->dev))
  940. dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
  941. list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
  942. urb->hcpriv = urbp;
  943. if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
  944. urb->error_count = 1; /* mark as a new urb */
  945. /* kick the scheduler, it'll do the rest */
  946. if (!timer_pending(&dum_hcd->timer))
  947. mod_timer(&dum_hcd->timer, jiffies + 1);
  948. done:
  949. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  950. return rc;
  951. }
  952. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  953. {
  954. struct dummy_hcd *dum_hcd;
  955. unsigned long flags;
  956. int rc;
  957. /* giveback happens automatically in timer callback,
  958. * so make sure the callback happens */
  959. dum_hcd = hcd_to_dummy_hcd(hcd);
  960. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  961. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  962. if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
  963. !list_empty(&dum_hcd->urbp_list))
  964. mod_timer(&dum_hcd->timer, jiffies);
  965. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  966. return rc;
  967. }
  968. /* transfer up to a frame's worth; caller must own lock */
  969. static int
  970. transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
  971. int *status)
  972. {
  973. struct dummy_request *req;
  974. top:
  975. /* if there's no request queued, the device is NAKing; return */
  976. list_for_each_entry (req, &ep->queue, queue) {
  977. unsigned host_len, dev_len, len;
  978. int is_short, to_host;
  979. int rescan = 0;
  980. /* 1..N packets of ep->ep.maxpacket each ... the last one
  981. * may be short (including zero length).
  982. *
  983. * writer can send a zlp explicitly (length 0) or implicitly
  984. * (length mod maxpacket zero, and 'zero' flag); they always
  985. * terminate reads.
  986. */
  987. host_len = urb->transfer_buffer_length - urb->actual_length;
  988. dev_len = req->req.length - req->req.actual;
  989. len = min (host_len, dev_len);
  990. /* FIXME update emulated data toggle too */
  991. to_host = usb_pipein (urb->pipe);
  992. if (unlikely (len == 0))
  993. is_short = 1;
  994. else {
  995. char *ubuf, *rbuf;
  996. /* not enough bandwidth left? */
  997. if (limit < ep->ep.maxpacket && limit < len)
  998. break;
  999. len = min (len, (unsigned) limit);
  1000. if (len == 0)
  1001. break;
  1002. /* use an extra pass for the final short packet */
  1003. if (len > ep->ep.maxpacket) {
  1004. rescan = 1;
  1005. len -= (len % ep->ep.maxpacket);
  1006. }
  1007. is_short = (len % ep->ep.maxpacket) != 0;
  1008. /* else transfer packet(s) */
  1009. ubuf = urb->transfer_buffer + urb->actual_length;
  1010. rbuf = req->req.buf + req->req.actual;
  1011. if (to_host)
  1012. memcpy (ubuf, rbuf, len);
  1013. else
  1014. memcpy (rbuf, ubuf, len);
  1015. ep->last_io = jiffies;
  1016. limit -= len;
  1017. urb->actual_length += len;
  1018. req->req.actual += len;
  1019. }
  1020. /* short packets terminate, maybe with overflow/underflow.
  1021. * it's only really an error to write too much.
  1022. *
  1023. * partially filling a buffer optionally blocks queue advances
  1024. * (so completion handlers can clean up the queue) but we don't
  1025. * need to emulate such data-in-flight.
  1026. */
  1027. if (is_short) {
  1028. if (host_len == dev_len) {
  1029. req->req.status = 0;
  1030. *status = 0;
  1031. } else if (to_host) {
  1032. req->req.status = 0;
  1033. if (dev_len > host_len)
  1034. *status = -EOVERFLOW;
  1035. else
  1036. *status = 0;
  1037. } else if (!to_host) {
  1038. *status = 0;
  1039. if (host_len > dev_len)
  1040. req->req.status = -EOVERFLOW;
  1041. else
  1042. req->req.status = 0;
  1043. }
  1044. /* many requests terminate without a short packet */
  1045. } else {
  1046. if (req->req.length == req->req.actual
  1047. && !req->req.zero)
  1048. req->req.status = 0;
  1049. if (urb->transfer_buffer_length == urb->actual_length
  1050. && !(urb->transfer_flags
  1051. & URB_ZERO_PACKET))
  1052. *status = 0;
  1053. }
  1054. /* device side completion --> continuable */
  1055. if (req->req.status != -EINPROGRESS) {
  1056. list_del_init (&req->queue);
  1057. spin_unlock (&dum->lock);
  1058. req->req.complete (&ep->ep, &req->req);
  1059. spin_lock (&dum->lock);
  1060. /* requests might have been unlinked... */
  1061. rescan = 1;
  1062. }
  1063. /* host side completion --> terminate */
  1064. if (*status != -EINPROGRESS)
  1065. break;
  1066. /* rescan to continue with any other queued i/o */
  1067. if (rescan)
  1068. goto top;
  1069. }
  1070. return limit;
  1071. }
  1072. static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
  1073. {
  1074. int limit = ep->ep.maxpacket;
  1075. if (dum->gadget.speed == USB_SPEED_HIGH) {
  1076. int tmp;
  1077. /* high bandwidth mode */
  1078. tmp = usb_endpoint_maxp(ep->desc);
  1079. tmp = (tmp >> 11) & 0x03;
  1080. tmp *= 8 /* applies to entire frame */;
  1081. limit += limit * tmp;
  1082. }
  1083. if (dum->gadget.speed == USB_SPEED_SUPER) {
  1084. switch (ep->desc->bmAttributes & 0x03) {
  1085. case USB_ENDPOINT_XFER_ISOC:
  1086. /* Sec. 4.4.8.2 USB3.0 Spec */
  1087. limit = 3 * 16 * 1024 * 8;
  1088. break;
  1089. case USB_ENDPOINT_XFER_INT:
  1090. /* Sec. 4.4.7.2 USB3.0 Spec */
  1091. limit = 3 * 1024 * 8;
  1092. break;
  1093. case USB_ENDPOINT_XFER_BULK:
  1094. default:
  1095. break;
  1096. }
  1097. }
  1098. return limit;
  1099. }
  1100. #define is_active(dum_hcd) ((dum_hcd->port_status & \
  1101. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  1102. USB_PORT_STAT_SUSPEND)) \
  1103. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  1104. static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
  1105. {
  1106. int i;
  1107. if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
  1108. dum->ss_hcd : dum->hs_hcd)))
  1109. return NULL;
  1110. if ((address & ~USB_DIR_IN) == 0)
  1111. return &dum->ep [0];
  1112. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  1113. struct dummy_ep *ep = &dum->ep [i];
  1114. if (!ep->desc)
  1115. continue;
  1116. if (ep->desc->bEndpointAddress == address)
  1117. return ep;
  1118. }
  1119. return NULL;
  1120. }
  1121. #undef is_active
  1122. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1123. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  1124. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  1125. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  1126. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  1127. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  1128. /**
  1129. * handle_control_request() - handles all control transfers
  1130. * @dum: pointer to dummy (the_controller)
  1131. * @urb: the urb request to handle
  1132. * @setup: pointer to the setup data for a USB device control
  1133. * request
  1134. * @status: pointer to request handling status
  1135. *
  1136. * Return 0 - if the request was handled
  1137. * 1 - if the request wasn't handles
  1138. * error code on error
  1139. */
  1140. static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
  1141. struct usb_ctrlrequest *setup,
  1142. int *status)
  1143. {
  1144. struct dummy_ep *ep2;
  1145. struct dummy *dum = dum_hcd->dum;
  1146. int ret_val = 1;
  1147. unsigned w_index;
  1148. unsigned w_value;
  1149. w_index = le16_to_cpu(setup->wIndex);
  1150. w_value = le16_to_cpu(setup->wValue);
  1151. switch (setup->bRequest) {
  1152. case USB_REQ_SET_ADDRESS:
  1153. if (setup->bRequestType != Dev_Request)
  1154. break;
  1155. dum->address = w_value;
  1156. *status = 0;
  1157. dev_dbg(udc_dev(dum), "set_address = %d\n",
  1158. w_value);
  1159. ret_val = 0;
  1160. break;
  1161. case USB_REQ_SET_FEATURE:
  1162. if (setup->bRequestType == Dev_Request) {
  1163. ret_val = 0;
  1164. switch (w_value) {
  1165. case USB_DEVICE_REMOTE_WAKEUP:
  1166. break;
  1167. case USB_DEVICE_B_HNP_ENABLE:
  1168. dum->gadget.b_hnp_enable = 1;
  1169. break;
  1170. case USB_DEVICE_A_HNP_SUPPORT:
  1171. dum->gadget.a_hnp_support = 1;
  1172. break;
  1173. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1174. dum->gadget.a_alt_hnp_support = 1;
  1175. break;
  1176. case USB_DEVICE_U1_ENABLE:
  1177. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1178. HCD_USB3)
  1179. w_value = USB_DEV_STAT_U1_ENABLED;
  1180. else
  1181. ret_val = -EOPNOTSUPP;
  1182. break;
  1183. case USB_DEVICE_U2_ENABLE:
  1184. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1185. HCD_USB3)
  1186. w_value = USB_DEV_STAT_U2_ENABLED;
  1187. else
  1188. ret_val = -EOPNOTSUPP;
  1189. break;
  1190. case USB_DEVICE_LTM_ENABLE:
  1191. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1192. HCD_USB3)
  1193. w_value = USB_DEV_STAT_LTM_ENABLED;
  1194. else
  1195. ret_val = -EOPNOTSUPP;
  1196. break;
  1197. default:
  1198. ret_val = -EOPNOTSUPP;
  1199. }
  1200. if (ret_val == 0) {
  1201. dum->devstatus |= (1 << w_value);
  1202. *status = 0;
  1203. }
  1204. } else if (setup->bRequestType == Ep_Request) {
  1205. /* endpoint halt */
  1206. ep2 = find_endpoint(dum, w_index);
  1207. if (!ep2 || ep2->ep.name == ep0name) {
  1208. ret_val = -EOPNOTSUPP;
  1209. break;
  1210. }
  1211. ep2->halted = 1;
  1212. ret_val = 0;
  1213. *status = 0;
  1214. }
  1215. break;
  1216. case USB_REQ_CLEAR_FEATURE:
  1217. if (setup->bRequestType == Dev_Request) {
  1218. ret_val = 0;
  1219. switch (w_value) {
  1220. case USB_DEVICE_REMOTE_WAKEUP:
  1221. w_value = USB_DEVICE_REMOTE_WAKEUP;
  1222. break;
  1223. case USB_DEVICE_U1_ENABLE:
  1224. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1225. HCD_USB3)
  1226. w_value = USB_DEV_STAT_U1_ENABLED;
  1227. else
  1228. ret_val = -EOPNOTSUPP;
  1229. break;
  1230. case USB_DEVICE_U2_ENABLE:
  1231. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1232. HCD_USB3)
  1233. w_value = USB_DEV_STAT_U2_ENABLED;
  1234. else
  1235. ret_val = -EOPNOTSUPP;
  1236. break;
  1237. case USB_DEVICE_LTM_ENABLE:
  1238. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1239. HCD_USB3)
  1240. w_value = USB_DEV_STAT_LTM_ENABLED;
  1241. else
  1242. ret_val = -EOPNOTSUPP;
  1243. break;
  1244. default:
  1245. ret_val = -EOPNOTSUPP;
  1246. break;
  1247. }
  1248. if (ret_val == 0) {
  1249. dum->devstatus &= ~(1 << w_value);
  1250. *status = 0;
  1251. }
  1252. } else if (setup->bRequestType == Ep_Request) {
  1253. /* endpoint halt */
  1254. ep2 = find_endpoint(dum, w_index);
  1255. if (!ep2) {
  1256. ret_val = -EOPNOTSUPP;
  1257. break;
  1258. }
  1259. if (!ep2->wedged)
  1260. ep2->halted = 0;
  1261. ret_val = 0;
  1262. *status = 0;
  1263. }
  1264. break;
  1265. case USB_REQ_GET_STATUS:
  1266. if (setup->bRequestType == Dev_InRequest
  1267. || setup->bRequestType == Intf_InRequest
  1268. || setup->bRequestType == Ep_InRequest) {
  1269. char *buf;
  1270. /*
  1271. * device: remote wakeup, selfpowered
  1272. * interface: nothing
  1273. * endpoint: halt
  1274. */
  1275. buf = (char *)urb->transfer_buffer;
  1276. if (urb->transfer_buffer_length > 0) {
  1277. if (setup->bRequestType == Ep_InRequest) {
  1278. ep2 = find_endpoint(dum, w_index);
  1279. if (!ep2) {
  1280. ret_val = -EOPNOTSUPP;
  1281. break;
  1282. }
  1283. buf[0] = ep2->halted;
  1284. } else if (setup->bRequestType ==
  1285. Dev_InRequest) {
  1286. buf[0] = (u8)dum->devstatus;
  1287. } else
  1288. buf[0] = 0;
  1289. }
  1290. if (urb->transfer_buffer_length > 1)
  1291. buf[1] = 0;
  1292. urb->actual_length = min_t(u32, 2,
  1293. urb->transfer_buffer_length);
  1294. ret_val = 0;
  1295. *status = 0;
  1296. }
  1297. break;
  1298. }
  1299. return ret_val;
  1300. }
  1301. /* drive both sides of the transfers; looks like irq handlers to
  1302. * both drivers except the callbacks aren't in_irq().
  1303. */
  1304. static void dummy_timer(unsigned long _dum_hcd)
  1305. {
  1306. struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
  1307. struct dummy *dum = dum_hcd->dum;
  1308. struct urbp *urbp, *tmp;
  1309. unsigned long flags;
  1310. int limit, total;
  1311. int i;
  1312. /* simplistic model for one frame's bandwidth */
  1313. switch (dum->gadget.speed) {
  1314. case USB_SPEED_LOW:
  1315. total = 8/*bytes*/ * 12/*packets*/;
  1316. break;
  1317. case USB_SPEED_FULL:
  1318. total = 64/*bytes*/ * 19/*packets*/;
  1319. break;
  1320. case USB_SPEED_HIGH:
  1321. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1322. break;
  1323. case USB_SPEED_SUPER:
  1324. /* Bus speed is 500000 bytes/ms, so use a little less */
  1325. total = 490000;
  1326. break;
  1327. default:
  1328. dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
  1329. return;
  1330. }
  1331. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1332. /* look at each urb queued by the host side driver */
  1333. spin_lock_irqsave (&dum->lock, flags);
  1334. if (!dum_hcd->udev) {
  1335. dev_err(dummy_dev(dum_hcd),
  1336. "timer fired with no URBs pending?\n");
  1337. spin_unlock_irqrestore (&dum->lock, flags);
  1338. return;
  1339. }
  1340. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1341. if (!ep_name [i])
  1342. break;
  1343. dum->ep [i].already_seen = 0;
  1344. }
  1345. restart:
  1346. list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
  1347. struct urb *urb;
  1348. struct dummy_request *req;
  1349. u8 address;
  1350. struct dummy_ep *ep = NULL;
  1351. int type;
  1352. int status = -EINPROGRESS;
  1353. urb = urbp->urb;
  1354. if (urb->unlinked)
  1355. goto return_urb;
  1356. else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
  1357. continue;
  1358. type = usb_pipetype (urb->pipe);
  1359. /* used up this frame's non-periodic bandwidth?
  1360. * FIXME there's infinite bandwidth for control and
  1361. * periodic transfers ... unrealistic.
  1362. */
  1363. if (total <= 0 && type == PIPE_BULK)
  1364. continue;
  1365. /* find the gadget's ep for this request (if configured) */
  1366. address = usb_pipeendpoint (urb->pipe);
  1367. if (usb_pipein (urb->pipe))
  1368. address |= USB_DIR_IN;
  1369. ep = find_endpoint(dum, address);
  1370. if (!ep) {
  1371. /* set_configuration() disagreement */
  1372. dev_dbg(dummy_dev(dum_hcd),
  1373. "no ep configured for urb %p\n",
  1374. urb);
  1375. status = -EPROTO;
  1376. goto return_urb;
  1377. }
  1378. if (ep->already_seen)
  1379. continue;
  1380. ep->already_seen = 1;
  1381. if (ep == &dum->ep [0] && urb->error_count) {
  1382. ep->setup_stage = 1; /* a new urb */
  1383. urb->error_count = 0;
  1384. }
  1385. if (ep->halted && !ep->setup_stage) {
  1386. /* NOTE: must not be iso! */
  1387. dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
  1388. ep->ep.name, urb);
  1389. status = -EPIPE;
  1390. goto return_urb;
  1391. }
  1392. /* FIXME make sure both ends agree on maxpacket */
  1393. /* handle control requests */
  1394. if (ep == &dum->ep [0] && ep->setup_stage) {
  1395. struct usb_ctrlrequest setup;
  1396. int value = 1;
  1397. setup = *(struct usb_ctrlrequest*) urb->setup_packet;
  1398. /* paranoia, in case of stale queued data */
  1399. list_for_each_entry (req, &ep->queue, queue) {
  1400. list_del_init (&req->queue);
  1401. req->req.status = -EOVERFLOW;
  1402. dev_dbg (udc_dev(dum), "stale req = %p\n",
  1403. req);
  1404. spin_unlock (&dum->lock);
  1405. req->req.complete (&ep->ep, &req->req);
  1406. spin_lock (&dum->lock);
  1407. ep->already_seen = 0;
  1408. goto restart;
  1409. }
  1410. /* gadget driver never sees set_address or operations
  1411. * on standard feature flags. some hardware doesn't
  1412. * even expose them.
  1413. */
  1414. ep->last_io = jiffies;
  1415. ep->setup_stage = 0;
  1416. ep->halted = 0;
  1417. value = handle_control_request(dum_hcd, urb, &setup,
  1418. &status);
  1419. /* gadget driver handles all other requests. block
  1420. * until setup() returns; no reentrancy issues etc.
  1421. */
  1422. if (value > 0) {
  1423. spin_unlock (&dum->lock);
  1424. value = dum->driver->setup (&dum->gadget,
  1425. &setup);
  1426. spin_lock (&dum->lock);
  1427. if (value >= 0) {
  1428. /* no delays (max 64KB data stage) */
  1429. limit = 64*1024;
  1430. goto treat_control_like_bulk;
  1431. }
  1432. /* error, see below */
  1433. }
  1434. if (value < 0) {
  1435. if (value != -EOPNOTSUPP)
  1436. dev_dbg (udc_dev(dum),
  1437. "setup --> %d\n",
  1438. value);
  1439. status = -EPIPE;
  1440. urb->actual_length = 0;
  1441. }
  1442. goto return_urb;
  1443. }
  1444. /* non-control requests */
  1445. limit = total;
  1446. switch (usb_pipetype (urb->pipe)) {
  1447. case PIPE_ISOCHRONOUS:
  1448. /* FIXME is it urb->interval since the last xfer?
  1449. * use urb->iso_frame_desc[i].
  1450. * complete whether or not ep has requests queued.
  1451. * report random errors, to debug drivers.
  1452. */
  1453. limit = max (limit, periodic_bytes (dum, ep));
  1454. status = -ENOSYS;
  1455. break;
  1456. case PIPE_INTERRUPT:
  1457. /* FIXME is it urb->interval since the last xfer?
  1458. * this almost certainly polls too fast.
  1459. */
  1460. limit = max (limit, periodic_bytes (dum, ep));
  1461. /* FALLTHROUGH */
  1462. // case PIPE_BULK: case PIPE_CONTROL:
  1463. default:
  1464. treat_control_like_bulk:
  1465. ep->last_io = jiffies;
  1466. total = transfer(dum, urb, ep, limit, &status);
  1467. break;
  1468. }
  1469. /* incomplete transfer? */
  1470. if (status == -EINPROGRESS)
  1471. continue;
  1472. return_urb:
  1473. list_del (&urbp->urbp_list);
  1474. kfree (urbp);
  1475. if (ep)
  1476. ep->already_seen = ep->setup_stage = 0;
  1477. usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
  1478. spin_unlock (&dum->lock);
  1479. usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
  1480. spin_lock (&dum->lock);
  1481. goto restart;
  1482. }
  1483. if (list_empty(&dum_hcd->urbp_list)) {
  1484. usb_put_dev(dum_hcd->udev);
  1485. dum_hcd->udev = NULL;
  1486. } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  1487. /* want a 1 msec delay here */
  1488. mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
  1489. }
  1490. spin_unlock_irqrestore (&dum->lock, flags);
  1491. }
  1492. /*-------------------------------------------------------------------------*/
  1493. #define PORT_C_MASK \
  1494. ((USB_PORT_STAT_C_CONNECTION \
  1495. | USB_PORT_STAT_C_ENABLE \
  1496. | USB_PORT_STAT_C_SUSPEND \
  1497. | USB_PORT_STAT_C_OVERCURRENT \
  1498. | USB_PORT_STAT_C_RESET) << 16)
  1499. static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
  1500. {
  1501. struct dummy_hcd *dum_hcd;
  1502. unsigned long flags;
  1503. int retval = 0;
  1504. dum_hcd = hcd_to_dummy_hcd(hcd);
  1505. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1506. if (!HCD_HW_ACCESSIBLE(hcd))
  1507. goto done;
  1508. if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1509. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1510. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1511. set_link_state(dum_hcd);
  1512. }
  1513. if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
  1514. *buf = (1 << 1);
  1515. dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
  1516. dum_hcd->port_status);
  1517. retval = 1;
  1518. if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
  1519. usb_hcd_resume_root_hub (hcd);
  1520. }
  1521. done:
  1522. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1523. return retval;
  1524. }
  1525. static inline void
  1526. ss_hub_descriptor(struct usb_hub_descriptor *desc)
  1527. {
  1528. memset(desc, 0, sizeof *desc);
  1529. desc->bDescriptorType = 0x2a;
  1530. desc->bDescLength = 12;
  1531. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1532. desc->bNbrPorts = 1;
  1533. desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
  1534. desc->u.ss.DeviceRemovable = 0xffff;
  1535. }
  1536. static inline void
  1537. hub_descriptor (struct usb_hub_descriptor *desc)
  1538. {
  1539. memset (desc, 0, sizeof *desc);
  1540. desc->bDescriptorType = 0x29;
  1541. desc->bDescLength = 9;
  1542. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1543. desc->bNbrPorts = 1;
  1544. desc->u.hs.DeviceRemovable[0] = 0xff;
  1545. desc->u.hs.DeviceRemovable[1] = 0xff;
  1546. }
  1547. static int dummy_hub_control (
  1548. struct usb_hcd *hcd,
  1549. u16 typeReq,
  1550. u16 wValue,
  1551. u16 wIndex,
  1552. char *buf,
  1553. u16 wLength
  1554. ) {
  1555. struct dummy_hcd *dum_hcd;
  1556. int retval = 0;
  1557. unsigned long flags;
  1558. if (!HCD_HW_ACCESSIBLE(hcd))
  1559. return -ETIMEDOUT;
  1560. dum_hcd = hcd_to_dummy_hcd(hcd);
  1561. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1562. switch (typeReq) {
  1563. case ClearHubFeature:
  1564. break;
  1565. case ClearPortFeature:
  1566. switch (wValue) {
  1567. case USB_PORT_FEAT_SUSPEND:
  1568. if (hcd->speed == HCD_USB3) {
  1569. dev_dbg(dummy_dev(dum_hcd),
  1570. "USB_PORT_FEAT_SUSPEND req not "
  1571. "supported for USB 3.0 roothub\n");
  1572. goto error;
  1573. }
  1574. if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
  1575. /* 20msec resume signaling */
  1576. dum_hcd->resuming = 1;
  1577. dum_hcd->re_timeout = jiffies +
  1578. msecs_to_jiffies(20);
  1579. }
  1580. break;
  1581. case USB_PORT_FEAT_POWER:
  1582. if (hcd->speed == HCD_USB3) {
  1583. if (dum_hcd->port_status & USB_PORT_STAT_POWER)
  1584. dev_dbg(dummy_dev(dum_hcd),
  1585. "power-off\n");
  1586. } else
  1587. if (dum_hcd->port_status &
  1588. USB_SS_PORT_STAT_POWER)
  1589. dev_dbg(dummy_dev(dum_hcd),
  1590. "power-off\n");
  1591. /* FALLS THROUGH */
  1592. default:
  1593. dum_hcd->port_status &= ~(1 << wValue);
  1594. set_link_state(dum_hcd);
  1595. }
  1596. break;
  1597. case GetHubDescriptor:
  1598. if (hcd->speed == HCD_USB3 &&
  1599. (wLength < USB_DT_SS_HUB_SIZE ||
  1600. wValue != (USB_DT_SS_HUB << 8))) {
  1601. dev_dbg(dummy_dev(dum_hcd),
  1602. "Wrong hub descriptor type for "
  1603. "USB 3.0 roothub.\n");
  1604. goto error;
  1605. }
  1606. if (hcd->speed == HCD_USB3)
  1607. ss_hub_descriptor((struct usb_hub_descriptor *) buf);
  1608. else
  1609. hub_descriptor((struct usb_hub_descriptor *) buf);
  1610. break;
  1611. case GetHubStatus:
  1612. *(__le32 *) buf = cpu_to_le32 (0);
  1613. break;
  1614. case GetPortStatus:
  1615. if (wIndex != 1)
  1616. retval = -EPIPE;
  1617. /* whoever resets or resumes must GetPortStatus to
  1618. * complete it!!
  1619. */
  1620. if (dum_hcd->resuming &&
  1621. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1622. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1623. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1624. }
  1625. if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
  1626. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1627. dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
  1628. dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
  1629. if (dum_hcd->dum->pullup) {
  1630. dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
  1631. if (hcd->speed < HCD_USB3) {
  1632. switch (dum_hcd->dum->gadget.speed) {
  1633. case USB_SPEED_HIGH:
  1634. dum_hcd->port_status |=
  1635. USB_PORT_STAT_HIGH_SPEED;
  1636. break;
  1637. case USB_SPEED_LOW:
  1638. dum_hcd->dum->gadget.ep0->
  1639. maxpacket = 8;
  1640. dum_hcd->port_status |=
  1641. USB_PORT_STAT_LOW_SPEED;
  1642. break;
  1643. default:
  1644. dum_hcd->dum->gadget.speed =
  1645. USB_SPEED_FULL;
  1646. break;
  1647. }
  1648. }
  1649. }
  1650. }
  1651. set_link_state(dum_hcd);
  1652. ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
  1653. ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
  1654. break;
  1655. case SetHubFeature:
  1656. retval = -EPIPE;
  1657. break;
  1658. case SetPortFeature:
  1659. switch (wValue) {
  1660. case USB_PORT_FEAT_LINK_STATE:
  1661. if (hcd->speed != HCD_USB3) {
  1662. dev_dbg(dummy_dev(dum_hcd),
  1663. "USB_PORT_FEAT_LINK_STATE req not "
  1664. "supported for USB 2.0 roothub\n");
  1665. goto error;
  1666. }
  1667. /*
  1668. * Since this is dummy we don't have an actual link so
  1669. * there is nothing to do for the SET_LINK_STATE cmd
  1670. */
  1671. break;
  1672. case USB_PORT_FEAT_U1_TIMEOUT:
  1673. case USB_PORT_FEAT_U2_TIMEOUT:
  1674. /* TODO: add suspend/resume support! */
  1675. if (hcd->speed != HCD_USB3) {
  1676. dev_dbg(dummy_dev(dum_hcd),
  1677. "USB_PORT_FEAT_U1/2_TIMEOUT req not "
  1678. "supported for USB 2.0 roothub\n");
  1679. goto error;
  1680. }
  1681. break;
  1682. case USB_PORT_FEAT_SUSPEND:
  1683. /* Applicable only for USB2.0 hub */
  1684. if (hcd->speed == HCD_USB3) {
  1685. dev_dbg(dummy_dev(dum_hcd),
  1686. "USB_PORT_FEAT_SUSPEND req not "
  1687. "supported for USB 3.0 roothub\n");
  1688. goto error;
  1689. }
  1690. if (dum_hcd->active) {
  1691. dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
  1692. /* HNP would happen here; for now we
  1693. * assume b_bus_req is always true.
  1694. */
  1695. set_link_state(dum_hcd);
  1696. if (((1 << USB_DEVICE_B_HNP_ENABLE)
  1697. & dum_hcd->dum->devstatus) != 0)
  1698. dev_dbg(dummy_dev(dum_hcd),
  1699. "no HNP yet!\n");
  1700. }
  1701. break;
  1702. case USB_PORT_FEAT_POWER:
  1703. if (hcd->speed == HCD_USB3)
  1704. dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
  1705. else
  1706. dum_hcd->port_status |= USB_PORT_STAT_POWER;
  1707. set_link_state(dum_hcd);
  1708. break;
  1709. case USB_PORT_FEAT_BH_PORT_RESET:
  1710. /* Applicable only for USB3.0 hub */
  1711. if (hcd->speed != HCD_USB3) {
  1712. dev_dbg(dummy_dev(dum_hcd),
  1713. "USB_PORT_FEAT_BH_PORT_RESET req not "
  1714. "supported for USB 2.0 roothub\n");
  1715. goto error;
  1716. }
  1717. /* FALLS THROUGH */
  1718. case USB_PORT_FEAT_RESET:
  1719. /* if it's already enabled, disable */
  1720. if (hcd->speed == HCD_USB3) {
  1721. dum_hcd->port_status = 0;
  1722. dum_hcd->port_status =
  1723. (USB_SS_PORT_STAT_POWER |
  1724. USB_PORT_STAT_CONNECTION |
  1725. USB_PORT_STAT_RESET);
  1726. } else
  1727. dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
  1728. | USB_PORT_STAT_LOW_SPEED
  1729. | USB_PORT_STAT_HIGH_SPEED);
  1730. /*
  1731. * We want to reset device status. All but the
  1732. * Self powered feature
  1733. */
  1734. dum_hcd->dum->devstatus &=
  1735. (1 << USB_DEVICE_SELF_POWERED);
  1736. /*
  1737. * FIXME USB3.0: what is the correct reset signaling
  1738. * interval? Is it still 50msec as for HS?
  1739. */
  1740. dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
  1741. /* FALLS THROUGH */
  1742. default:
  1743. if (hcd->speed == HCD_USB3) {
  1744. if ((dum_hcd->port_status &
  1745. USB_SS_PORT_STAT_POWER) != 0) {
  1746. dum_hcd->port_status |= (1 << wValue);
  1747. set_link_state(dum_hcd);
  1748. }
  1749. } else
  1750. if ((dum_hcd->port_status &
  1751. USB_PORT_STAT_POWER) != 0) {
  1752. dum_hcd->port_status |= (1 << wValue);
  1753. set_link_state(dum_hcd);
  1754. }
  1755. }
  1756. break;
  1757. case GetPortErrorCount:
  1758. if (hcd->speed != HCD_USB3) {
  1759. dev_dbg(dummy_dev(dum_hcd),
  1760. "GetPortErrorCount req not "
  1761. "supported for USB 2.0 roothub\n");
  1762. goto error;
  1763. }
  1764. /* We'll always return 0 since this is a dummy hub */
  1765. *(__le32 *) buf = cpu_to_le32(0);
  1766. break;
  1767. case SetHubDepth:
  1768. if (hcd->speed != HCD_USB3) {
  1769. dev_dbg(dummy_dev(dum_hcd),
  1770. "SetHubDepth req not supported for "
  1771. "USB 2.0 roothub\n");
  1772. goto error;
  1773. }
  1774. break;
  1775. default:
  1776. dev_dbg(dummy_dev(dum_hcd),
  1777. "hub control req%04x v%04x i%04x l%d\n",
  1778. typeReq, wValue, wIndex, wLength);
  1779. error:
  1780. /* "protocol stall" on error */
  1781. retval = -EPIPE;
  1782. }
  1783. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1784. if ((dum_hcd->port_status & PORT_C_MASK) != 0)
  1785. usb_hcd_poll_rh_status (hcd);
  1786. return retval;
  1787. }
  1788. static int dummy_bus_suspend (struct usb_hcd *hcd)
  1789. {
  1790. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1791. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1792. spin_lock_irq(&dum_hcd->dum->lock);
  1793. dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
  1794. set_link_state(dum_hcd);
  1795. hcd->state = HC_STATE_SUSPENDED;
  1796. spin_unlock_irq(&dum_hcd->dum->lock);
  1797. return 0;
  1798. }
  1799. static int dummy_bus_resume (struct usb_hcd *hcd)
  1800. {
  1801. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1802. int rc = 0;
  1803. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1804. spin_lock_irq(&dum_hcd->dum->lock);
  1805. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1806. rc = -ESHUTDOWN;
  1807. } else {
  1808. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1809. set_link_state(dum_hcd);
  1810. if (!list_empty(&dum_hcd->urbp_list))
  1811. mod_timer(&dum_hcd->timer, jiffies);
  1812. hcd->state = HC_STATE_RUNNING;
  1813. }
  1814. spin_unlock_irq(&dum_hcd->dum->lock);
  1815. return rc;
  1816. }
  1817. /*-------------------------------------------------------------------------*/
  1818. static inline ssize_t
  1819. show_urb (char *buf, size_t size, struct urb *urb)
  1820. {
  1821. int ep = usb_pipeendpoint (urb->pipe);
  1822. return snprintf (buf, size,
  1823. "urb/%p %s ep%d%s%s len %d/%d\n",
  1824. urb,
  1825. ({ char *s;
  1826. switch (urb->dev->speed) {
  1827. case USB_SPEED_LOW:
  1828. s = "ls";
  1829. break;
  1830. case USB_SPEED_FULL:
  1831. s = "fs";
  1832. break;
  1833. case USB_SPEED_HIGH:
  1834. s = "hs";
  1835. break;
  1836. case USB_SPEED_SUPER:
  1837. s = "ss";
  1838. break;
  1839. default:
  1840. s = "?";
  1841. break;
  1842. }; s; }),
  1843. ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
  1844. ({ char *s; \
  1845. switch (usb_pipetype (urb->pipe)) { \
  1846. case PIPE_CONTROL: \
  1847. s = ""; \
  1848. break; \
  1849. case PIPE_BULK: \
  1850. s = "-bulk"; \
  1851. break; \
  1852. case PIPE_INTERRUPT: \
  1853. s = "-int"; \
  1854. break; \
  1855. default: \
  1856. s = "-iso"; \
  1857. break; \
  1858. }; s;}),
  1859. urb->actual_length, urb->transfer_buffer_length);
  1860. }
  1861. static ssize_t
  1862. show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
  1863. {
  1864. struct usb_hcd *hcd = dev_get_drvdata (dev);
  1865. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1866. struct urbp *urbp;
  1867. size_t size = 0;
  1868. unsigned long flags;
  1869. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1870. list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
  1871. size_t temp;
  1872. temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
  1873. buf += temp;
  1874. size += temp;
  1875. }
  1876. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1877. return size;
  1878. }
  1879. static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
  1880. static int dummy_start_ss(struct dummy_hcd *dum_hcd)
  1881. {
  1882. init_timer(&dum_hcd->timer);
  1883. dum_hcd->timer.function = dummy_timer;
  1884. dum_hcd->timer.data = (unsigned long)dum_hcd;
  1885. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1886. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  1887. dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
  1888. dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
  1889. dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
  1890. #ifdef CONFIG_USB_OTG
  1891. dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
  1892. #endif
  1893. return 0;
  1894. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  1895. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  1896. }
  1897. static int dummy_start(struct usb_hcd *hcd)
  1898. {
  1899. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1900. /*
  1901. * MASTER side init ... we emulate a root hub that'll only ever
  1902. * talk to one device (the slave side). Also appears in sysfs,
  1903. * just like more familiar pci-based HCDs.
  1904. */
  1905. if (!usb_hcd_is_primary_hcd(hcd))
  1906. return dummy_start_ss(dum_hcd);
  1907. spin_lock_init(&dum_hcd->dum->lock);
  1908. init_timer(&dum_hcd->timer);
  1909. dum_hcd->timer.function = dummy_timer;
  1910. dum_hcd->timer.data = (unsigned long)dum_hcd;
  1911. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1912. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  1913. hcd->power_budget = POWER_BUDGET;
  1914. hcd->state = HC_STATE_RUNNING;
  1915. hcd->uses_new_polling = 1;
  1916. #ifdef CONFIG_USB_OTG
  1917. hcd->self.otg_port = 1;
  1918. #endif
  1919. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  1920. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  1921. }
  1922. static void dummy_stop (struct usb_hcd *hcd)
  1923. {
  1924. struct dummy *dum;
  1925. dum = (hcd_to_dummy_hcd(hcd))->dum;
  1926. device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
  1927. usb_gadget_unregister_driver(dum->driver);
  1928. dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
  1929. }
  1930. /*-------------------------------------------------------------------------*/
  1931. static int dummy_h_get_frame (struct usb_hcd *hcd)
  1932. {
  1933. return dummy_g_get_frame (NULL);
  1934. }
  1935. static int dummy_setup(struct usb_hcd *hcd)
  1936. {
  1937. if (usb_hcd_is_primary_hcd(hcd)) {
  1938. the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
  1939. the_controller.hs_hcd->dum = &the_controller;
  1940. /*
  1941. * Mark the first roothub as being USB 2.0.
  1942. * The USB 3.0 roothub will be registered later by
  1943. * dummy_hcd_probe()
  1944. */
  1945. hcd->speed = HCD_USB2;
  1946. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  1947. } else {
  1948. the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
  1949. the_controller.ss_hcd->dum = &the_controller;
  1950. hcd->speed = HCD_USB3;
  1951. hcd->self.root_hub->speed = USB_SPEED_SUPER;
  1952. }
  1953. return 0;
  1954. }
  1955. /* Change a group of bulk endpoints to support multiple stream IDs */
  1956. int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1957. struct usb_host_endpoint **eps, unsigned int num_eps,
  1958. unsigned int num_streams, gfp_t mem_flags)
  1959. {
  1960. if (hcd->speed != HCD_USB3)
  1961. dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
  1962. "%s() - ERROR! Not supported for USB2.0 roothub\n",
  1963. __func__);
  1964. return 0;
  1965. }
  1966. /* Reverts a group of bulk endpoints back to not using stream IDs. */
  1967. int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1968. struct usb_host_endpoint **eps, unsigned int num_eps,
  1969. gfp_t mem_flags)
  1970. {
  1971. if (hcd->speed != HCD_USB3)
  1972. dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
  1973. "%s() - ERROR! Not supported for USB2.0 roothub\n",
  1974. __func__);
  1975. return 0;
  1976. }
  1977. static struct hc_driver dummy_hcd = {
  1978. .description = (char *) driver_name,
  1979. .product_desc = "Dummy host controller",
  1980. .hcd_priv_size = sizeof(struct dummy_hcd),
  1981. .flags = HCD_USB3 | HCD_SHARED,
  1982. .reset = dummy_setup,
  1983. .start = dummy_start,
  1984. .stop = dummy_stop,
  1985. .urb_enqueue = dummy_urb_enqueue,
  1986. .urb_dequeue = dummy_urb_dequeue,
  1987. .get_frame_number = dummy_h_get_frame,
  1988. .hub_status_data = dummy_hub_status,
  1989. .hub_control = dummy_hub_control,
  1990. .bus_suspend = dummy_bus_suspend,
  1991. .bus_resume = dummy_bus_resume,
  1992. .alloc_streams = dummy_alloc_streams,
  1993. .free_streams = dummy_free_streams,
  1994. };
  1995. static int dummy_hcd_probe(struct platform_device *pdev)
  1996. {
  1997. struct usb_hcd *hs_hcd;
  1998. struct usb_hcd *ss_hcd;
  1999. int retval;
  2000. dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
  2001. if (!mod_data.is_super_speed)
  2002. dummy_hcd.flags = HCD_USB2;
  2003. hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
  2004. if (!hs_hcd)
  2005. return -ENOMEM;
  2006. hs_hcd->has_tt = 1;
  2007. retval = usb_add_hcd(hs_hcd, 0, 0);
  2008. if (retval != 0) {
  2009. usb_put_hcd(hs_hcd);
  2010. return retval;
  2011. }
  2012. if (mod_data.is_super_speed) {
  2013. ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
  2014. dev_name(&pdev->dev), hs_hcd);
  2015. if (!ss_hcd) {
  2016. retval = -ENOMEM;
  2017. goto dealloc_usb2_hcd;
  2018. }
  2019. retval = usb_add_hcd(ss_hcd, 0, 0);
  2020. if (retval)
  2021. goto put_usb3_hcd;
  2022. }
  2023. return 0;
  2024. put_usb3_hcd:
  2025. usb_put_hcd(ss_hcd);
  2026. dealloc_usb2_hcd:
  2027. usb_put_hcd(hs_hcd);
  2028. the_controller.hs_hcd = the_controller.ss_hcd = NULL;
  2029. return retval;
  2030. }
  2031. static int dummy_hcd_remove(struct platform_device *pdev)
  2032. {
  2033. struct dummy *dum;
  2034. dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
  2035. if (dum->ss_hcd) {
  2036. usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2037. usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2038. }
  2039. usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2040. usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2041. the_controller.hs_hcd = NULL;
  2042. the_controller.ss_hcd = NULL;
  2043. return 0;
  2044. }
  2045. static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
  2046. {
  2047. struct usb_hcd *hcd;
  2048. struct dummy_hcd *dum_hcd;
  2049. int rc = 0;
  2050. dev_dbg (&pdev->dev, "%s\n", __func__);
  2051. hcd = platform_get_drvdata (pdev);
  2052. dum_hcd = hcd_to_dummy_hcd(hcd);
  2053. if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  2054. dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
  2055. rc = -EBUSY;
  2056. } else
  2057. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2058. return rc;
  2059. }
  2060. static int dummy_hcd_resume (struct platform_device *pdev)
  2061. {
  2062. struct usb_hcd *hcd;
  2063. dev_dbg (&pdev->dev, "%s\n", __func__);
  2064. hcd = platform_get_drvdata (pdev);
  2065. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2066. usb_hcd_poll_rh_status (hcd);
  2067. return 0;
  2068. }
  2069. static struct platform_driver dummy_hcd_driver = {
  2070. .probe = dummy_hcd_probe,
  2071. .remove = dummy_hcd_remove,
  2072. .suspend = dummy_hcd_suspend,
  2073. .resume = dummy_hcd_resume,
  2074. .driver = {
  2075. .name = (char *) driver_name,
  2076. .owner = THIS_MODULE,
  2077. },
  2078. };
  2079. /*-------------------------------------------------------------------------*/
  2080. static struct platform_device *the_udc_pdev;
  2081. static struct platform_device *the_hcd_pdev;
  2082. static int __init init (void)
  2083. {
  2084. int retval = -ENOMEM;
  2085. if (usb_disabled ())
  2086. return -ENODEV;
  2087. if (!mod_data.is_high_speed && mod_data.is_super_speed)
  2088. return -EINVAL;
  2089. the_hcd_pdev = platform_device_alloc(driver_name, -1);
  2090. if (!the_hcd_pdev)
  2091. return retval;
  2092. the_udc_pdev = platform_device_alloc(gadget_name, -1);
  2093. if (!the_udc_pdev)
  2094. goto err_alloc_udc;
  2095. retval = platform_driver_register(&dummy_hcd_driver);
  2096. if (retval < 0)
  2097. goto err_register_hcd_driver;
  2098. retval = platform_driver_register(&dummy_udc_driver);
  2099. if (retval < 0)
  2100. goto err_register_udc_driver;
  2101. retval = platform_device_add(the_hcd_pdev);
  2102. if (retval < 0)
  2103. goto err_add_hcd;
  2104. if (!the_controller.hs_hcd ||
  2105. (!the_controller.ss_hcd && mod_data.is_super_speed)) {
  2106. /*
  2107. * The hcd was added successfully but its probe function failed
  2108. * for some reason.
  2109. */
  2110. retval = -EINVAL;
  2111. goto err_add_udc;
  2112. }
  2113. retval = platform_device_add(the_udc_pdev);
  2114. if (retval < 0)
  2115. goto err_add_udc;
  2116. if (!platform_get_drvdata(the_udc_pdev)) {
  2117. /*
  2118. * The udc was added successfully but its probe function failed
  2119. * for some reason.
  2120. */
  2121. retval = -EINVAL;
  2122. goto err_probe_udc;
  2123. }
  2124. return retval;
  2125. err_probe_udc:
  2126. platform_device_del(the_udc_pdev);
  2127. err_add_udc:
  2128. platform_device_del(the_hcd_pdev);
  2129. err_add_hcd:
  2130. platform_driver_unregister(&dummy_udc_driver);
  2131. err_register_udc_driver:
  2132. platform_driver_unregister(&dummy_hcd_driver);
  2133. err_register_hcd_driver:
  2134. platform_device_put(the_udc_pdev);
  2135. err_alloc_udc:
  2136. platform_device_put(the_hcd_pdev);
  2137. return retval;
  2138. }
  2139. module_init (init);
  2140. static void __exit cleanup (void)
  2141. {
  2142. platform_device_unregister(the_udc_pdev);
  2143. platform_device_unregister(the_hcd_pdev);
  2144. platform_driver_unregister(&dummy_udc_driver);
  2145. platform_driver_unregister(&dummy_hcd_driver);
  2146. }
  2147. module_exit (cleanup);