dummy_hcd.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  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. if (dum->gadget.speed == USB_SPEED_SUPER)
  690. dum->ep[0].ep.maxpacket = 9;
  691. else
  692. dum->ep[0].ep.maxpacket = 64;
  693. }
  694. static int dummy_pullup (struct usb_gadget *_gadget, int value)
  695. {
  696. struct dummy_hcd *dum_hcd;
  697. struct dummy *dum;
  698. unsigned long flags;
  699. dum = gadget_dev_to_dummy(&_gadget->dev);
  700. if (value && dum->driver) {
  701. if (mod_data.is_super_speed)
  702. dum->gadget.speed = dum->driver->max_speed;
  703. else if (mod_data.is_high_speed)
  704. dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
  705. dum->driver->max_speed);
  706. else
  707. dum->gadget.speed = USB_SPEED_FULL;
  708. dummy_udc_udpate_ep0(dum);
  709. if (dum->gadget.speed < dum->driver->max_speed)
  710. dev_dbg(udc_dev(dum), "This device can perform faster"
  711. " if you connect it to a %s port...\n",
  712. usb_speed_string(dum->driver->max_speed));
  713. }
  714. dum_hcd = gadget_to_dummy_hcd(_gadget);
  715. spin_lock_irqsave (&dum->lock, flags);
  716. dum->pullup = (value != 0);
  717. set_link_state(dum_hcd);
  718. spin_unlock_irqrestore (&dum->lock, flags);
  719. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  720. return 0;
  721. }
  722. static int dummy_udc_start(struct usb_gadget *g,
  723. struct usb_gadget_driver *driver);
  724. static int dummy_udc_stop(struct usb_gadget *g,
  725. struct usb_gadget_driver *driver);
  726. static const struct usb_gadget_ops dummy_ops = {
  727. .get_frame = dummy_g_get_frame,
  728. .wakeup = dummy_wakeup,
  729. .set_selfpowered = dummy_set_selfpowered,
  730. .pullup = dummy_pullup,
  731. .udc_start = dummy_udc_start,
  732. .udc_stop = dummy_udc_stop,
  733. };
  734. /*-------------------------------------------------------------------------*/
  735. /* "function" sysfs attribute */
  736. static ssize_t
  737. show_function (struct device *dev, struct device_attribute *attr, char *buf)
  738. {
  739. struct dummy *dum = gadget_dev_to_dummy (dev);
  740. if (!dum->driver || !dum->driver->function)
  741. return 0;
  742. return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
  743. }
  744. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  745. /*-------------------------------------------------------------------------*/
  746. /*
  747. * Driver registration/unregistration.
  748. *
  749. * This is basically hardware-specific; there's usually only one real USB
  750. * device (not host) controller since that's how USB devices are intended
  751. * to work. So most implementations of these api calls will rely on the
  752. * fact that only one driver will ever bind to the hardware. But curious
  753. * hardware can be built with discrete components, so the gadget API doesn't
  754. * require that assumption.
  755. *
  756. * For this emulator, it might be convenient to create a usb slave device
  757. * for each driver that registers: just add to a big root hub.
  758. */
  759. static int dummy_udc_start(struct usb_gadget *g,
  760. struct usb_gadget_driver *driver)
  761. {
  762. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  763. struct dummy *dum = dum_hcd->dum;
  764. if (driver->max_speed == USB_SPEED_UNKNOWN)
  765. return -EINVAL;
  766. /*
  767. * SLAVE side init ... the layer above hardware, which
  768. * can't enumerate without help from the driver we're binding.
  769. */
  770. dum->devstatus = 0;
  771. dum->driver = driver;
  772. dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
  773. driver->driver.name);
  774. return 0;
  775. }
  776. static int dummy_udc_stop(struct usb_gadget *g,
  777. struct usb_gadget_driver *driver)
  778. {
  779. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  780. struct dummy *dum = dum_hcd->dum;
  781. dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
  782. driver->driver.name);
  783. dum->driver = NULL;
  784. dummy_pullup(&dum->gadget, 0);
  785. return 0;
  786. }
  787. #undef is_enabled
  788. /* The gadget structure is stored inside the hcd structure and will be
  789. * released along with it. */
  790. static void
  791. dummy_gadget_release (struct device *dev)
  792. {
  793. return;
  794. }
  795. static void init_dummy_udc_hw(struct dummy *dum)
  796. {
  797. int i;
  798. INIT_LIST_HEAD(&dum->gadget.ep_list);
  799. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  800. struct dummy_ep *ep = &dum->ep[i];
  801. if (!ep_name[i])
  802. break;
  803. ep->ep.name = ep_name[i];
  804. ep->ep.ops = &dummy_ep_ops;
  805. list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
  806. ep->halted = ep->wedged = ep->already_seen =
  807. ep->setup_stage = 0;
  808. ep->ep.maxpacket = ~0;
  809. ep->ep.max_streams = 16;
  810. ep->last_io = jiffies;
  811. ep->gadget = &dum->gadget;
  812. ep->desc = NULL;
  813. INIT_LIST_HEAD(&ep->queue);
  814. }
  815. dum->gadget.ep0 = &dum->ep[0].ep;
  816. list_del_init(&dum->ep[0].ep.ep_list);
  817. INIT_LIST_HEAD(&dum->fifo_req.queue);
  818. #ifdef CONFIG_USB_OTG
  819. dum->gadget.is_otg = 1;
  820. #endif
  821. }
  822. static int dummy_udc_probe (struct platform_device *pdev)
  823. {
  824. struct dummy *dum = &the_controller;
  825. int rc;
  826. dum->gadget.name = gadget_name;
  827. dum->gadget.ops = &dummy_ops;
  828. dum->gadget.max_speed = USB_SPEED_SUPER;
  829. dev_set_name(&dum->gadget.dev, "gadget");
  830. dum->gadget.dev.parent = &pdev->dev;
  831. dum->gadget.dev.release = dummy_gadget_release;
  832. rc = device_register (&dum->gadget.dev);
  833. if (rc < 0) {
  834. put_device(&dum->gadget.dev);
  835. return rc;
  836. }
  837. init_dummy_udc_hw(dum);
  838. rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
  839. if (rc < 0)
  840. goto err_udc;
  841. rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
  842. if (rc < 0)
  843. goto err_dev;
  844. platform_set_drvdata(pdev, dum);
  845. return rc;
  846. err_dev:
  847. usb_del_gadget_udc(&dum->gadget);
  848. err_udc:
  849. device_unregister(&dum->gadget.dev);
  850. return rc;
  851. }
  852. static int dummy_udc_remove (struct platform_device *pdev)
  853. {
  854. struct dummy *dum = platform_get_drvdata (pdev);
  855. usb_del_gadget_udc(&dum->gadget);
  856. platform_set_drvdata (pdev, NULL);
  857. device_remove_file (&dum->gadget.dev, &dev_attr_function);
  858. device_unregister (&dum->gadget.dev);
  859. return 0;
  860. }
  861. static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
  862. int suspend)
  863. {
  864. spin_lock_irq(&dum->lock);
  865. dum->udc_suspended = suspend;
  866. set_link_state(dum_hcd);
  867. spin_unlock_irq(&dum->lock);
  868. }
  869. static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
  870. {
  871. struct dummy *dum = platform_get_drvdata(pdev);
  872. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  873. dev_dbg(&pdev->dev, "%s\n", __func__);
  874. dummy_udc_pm(dum, dum_hcd, 1);
  875. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  876. return 0;
  877. }
  878. static int dummy_udc_resume(struct platform_device *pdev)
  879. {
  880. struct dummy *dum = platform_get_drvdata(pdev);
  881. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  882. dev_dbg(&pdev->dev, "%s\n", __func__);
  883. dummy_udc_pm(dum, dum_hcd, 0);
  884. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  885. return 0;
  886. }
  887. static struct platform_driver dummy_udc_driver = {
  888. .probe = dummy_udc_probe,
  889. .remove = dummy_udc_remove,
  890. .suspend = dummy_udc_suspend,
  891. .resume = dummy_udc_resume,
  892. .driver = {
  893. .name = (char *) gadget_name,
  894. .owner = THIS_MODULE,
  895. },
  896. };
  897. /*-------------------------------------------------------------------------*/
  898. /* MASTER/HOST SIDE DRIVER
  899. *
  900. * this uses the hcd framework to hook up to host side drivers.
  901. * its root hub will only have one device, otherwise it acts like
  902. * a normal host controller.
  903. *
  904. * when urbs are queued, they're just stuck on a list that we
  905. * scan in a timer callback. that callback connects writes from
  906. * the host with reads from the device, and so on, based on the
  907. * usb 2.0 rules.
  908. */
  909. static int dummy_urb_enqueue (
  910. struct usb_hcd *hcd,
  911. struct urb *urb,
  912. gfp_t mem_flags
  913. ) {
  914. struct dummy_hcd *dum_hcd;
  915. struct urbp *urbp;
  916. unsigned long flags;
  917. int rc;
  918. if (!urb->transfer_buffer && urb->transfer_buffer_length)
  919. return -EINVAL;
  920. urbp = kmalloc (sizeof *urbp, mem_flags);
  921. if (!urbp)
  922. return -ENOMEM;
  923. urbp->urb = urb;
  924. dum_hcd = hcd_to_dummy_hcd(hcd);
  925. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  926. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  927. if (rc) {
  928. kfree(urbp);
  929. goto done;
  930. }
  931. if (!dum_hcd->udev) {
  932. dum_hcd->udev = urb->dev;
  933. usb_get_dev(dum_hcd->udev);
  934. } else if (unlikely(dum_hcd->udev != urb->dev))
  935. dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
  936. list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
  937. urb->hcpriv = urbp;
  938. if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
  939. urb->error_count = 1; /* mark as a new urb */
  940. /* kick the scheduler, it'll do the rest */
  941. if (!timer_pending(&dum_hcd->timer))
  942. mod_timer(&dum_hcd->timer, jiffies + 1);
  943. done:
  944. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  945. return rc;
  946. }
  947. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  948. {
  949. struct dummy_hcd *dum_hcd;
  950. unsigned long flags;
  951. int rc;
  952. /* giveback happens automatically in timer callback,
  953. * so make sure the callback happens */
  954. dum_hcd = hcd_to_dummy_hcd(hcd);
  955. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  956. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  957. if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
  958. !list_empty(&dum_hcd->urbp_list))
  959. mod_timer(&dum_hcd->timer, jiffies);
  960. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  961. return rc;
  962. }
  963. /* transfer up to a frame's worth; caller must own lock */
  964. static int
  965. transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
  966. int *status)
  967. {
  968. struct dummy_request *req;
  969. top:
  970. /* if there's no request queued, the device is NAKing; return */
  971. list_for_each_entry (req, &ep->queue, queue) {
  972. unsigned host_len, dev_len, len;
  973. int is_short, to_host;
  974. int rescan = 0;
  975. /* 1..N packets of ep->ep.maxpacket each ... the last one
  976. * may be short (including zero length).
  977. *
  978. * writer can send a zlp explicitly (length 0) or implicitly
  979. * (length mod maxpacket zero, and 'zero' flag); they always
  980. * terminate reads.
  981. */
  982. host_len = urb->transfer_buffer_length - urb->actual_length;
  983. dev_len = req->req.length - req->req.actual;
  984. len = min (host_len, dev_len);
  985. /* FIXME update emulated data toggle too */
  986. to_host = usb_pipein (urb->pipe);
  987. if (unlikely (len == 0))
  988. is_short = 1;
  989. else {
  990. char *ubuf, *rbuf;
  991. /* not enough bandwidth left? */
  992. if (limit < ep->ep.maxpacket && limit < len)
  993. break;
  994. len = min (len, (unsigned) limit);
  995. if (len == 0)
  996. break;
  997. /* use an extra pass for the final short packet */
  998. if (len > ep->ep.maxpacket) {
  999. rescan = 1;
  1000. len -= (len % ep->ep.maxpacket);
  1001. }
  1002. is_short = (len % ep->ep.maxpacket) != 0;
  1003. /* else transfer packet(s) */
  1004. ubuf = urb->transfer_buffer + urb->actual_length;
  1005. rbuf = req->req.buf + req->req.actual;
  1006. if (to_host)
  1007. memcpy (ubuf, rbuf, len);
  1008. else
  1009. memcpy (rbuf, ubuf, len);
  1010. ep->last_io = jiffies;
  1011. limit -= len;
  1012. urb->actual_length += len;
  1013. req->req.actual += len;
  1014. }
  1015. /* short packets terminate, maybe with overflow/underflow.
  1016. * it's only really an error to write too much.
  1017. *
  1018. * partially filling a buffer optionally blocks queue advances
  1019. * (so completion handlers can clean up the queue) but we don't
  1020. * need to emulate such data-in-flight.
  1021. */
  1022. if (is_short) {
  1023. if (host_len == dev_len) {
  1024. req->req.status = 0;
  1025. *status = 0;
  1026. } else if (to_host) {
  1027. req->req.status = 0;
  1028. if (dev_len > host_len)
  1029. *status = -EOVERFLOW;
  1030. else
  1031. *status = 0;
  1032. } else if (!to_host) {
  1033. *status = 0;
  1034. if (host_len > dev_len)
  1035. req->req.status = -EOVERFLOW;
  1036. else
  1037. req->req.status = 0;
  1038. }
  1039. /* many requests terminate without a short packet */
  1040. } else {
  1041. if (req->req.length == req->req.actual
  1042. && !req->req.zero)
  1043. req->req.status = 0;
  1044. if (urb->transfer_buffer_length == urb->actual_length
  1045. && !(urb->transfer_flags
  1046. & URB_ZERO_PACKET))
  1047. *status = 0;
  1048. }
  1049. /* device side completion --> continuable */
  1050. if (req->req.status != -EINPROGRESS) {
  1051. list_del_init (&req->queue);
  1052. spin_unlock (&dum->lock);
  1053. req->req.complete (&ep->ep, &req->req);
  1054. spin_lock (&dum->lock);
  1055. /* requests might have been unlinked... */
  1056. rescan = 1;
  1057. }
  1058. /* host side completion --> terminate */
  1059. if (*status != -EINPROGRESS)
  1060. break;
  1061. /* rescan to continue with any other queued i/o */
  1062. if (rescan)
  1063. goto top;
  1064. }
  1065. return limit;
  1066. }
  1067. static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
  1068. {
  1069. int limit = ep->ep.maxpacket;
  1070. if (dum->gadget.speed == USB_SPEED_HIGH) {
  1071. int tmp;
  1072. /* high bandwidth mode */
  1073. tmp = usb_endpoint_maxp(ep->desc);
  1074. tmp = (tmp >> 11) & 0x03;
  1075. tmp *= 8 /* applies to entire frame */;
  1076. limit += limit * tmp;
  1077. }
  1078. if (dum->gadget.speed == USB_SPEED_SUPER) {
  1079. switch (ep->desc->bmAttributes & 0x03) {
  1080. case USB_ENDPOINT_XFER_ISOC:
  1081. /* Sec. 4.4.8.2 USB3.0 Spec */
  1082. limit = 3 * 16 * 1024 * 8;
  1083. break;
  1084. case USB_ENDPOINT_XFER_INT:
  1085. /* Sec. 4.4.7.2 USB3.0 Spec */
  1086. limit = 3 * 1024 * 8;
  1087. break;
  1088. case USB_ENDPOINT_XFER_BULK:
  1089. default:
  1090. break;
  1091. }
  1092. }
  1093. return limit;
  1094. }
  1095. #define is_active(dum_hcd) ((dum_hcd->port_status & \
  1096. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  1097. USB_PORT_STAT_SUSPEND)) \
  1098. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  1099. static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
  1100. {
  1101. int i;
  1102. if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
  1103. dum->ss_hcd : dum->hs_hcd)))
  1104. return NULL;
  1105. if ((address & ~USB_DIR_IN) == 0)
  1106. return &dum->ep [0];
  1107. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  1108. struct dummy_ep *ep = &dum->ep [i];
  1109. if (!ep->desc)
  1110. continue;
  1111. if (ep->desc->bEndpointAddress == address)
  1112. return ep;
  1113. }
  1114. return NULL;
  1115. }
  1116. #undef is_active
  1117. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1118. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  1119. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  1120. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  1121. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  1122. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  1123. /**
  1124. * handle_control_request() - handles all control transfers
  1125. * @dum: pointer to dummy (the_controller)
  1126. * @urb: the urb request to handle
  1127. * @setup: pointer to the setup data for a USB device control
  1128. * request
  1129. * @status: pointer to request handling status
  1130. *
  1131. * Return 0 - if the request was handled
  1132. * 1 - if the request wasn't handles
  1133. * error code on error
  1134. */
  1135. static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
  1136. struct usb_ctrlrequest *setup,
  1137. int *status)
  1138. {
  1139. struct dummy_ep *ep2;
  1140. struct dummy *dum = dum_hcd->dum;
  1141. int ret_val = 1;
  1142. unsigned w_index;
  1143. unsigned w_value;
  1144. w_index = le16_to_cpu(setup->wIndex);
  1145. w_value = le16_to_cpu(setup->wValue);
  1146. switch (setup->bRequest) {
  1147. case USB_REQ_SET_ADDRESS:
  1148. if (setup->bRequestType != Dev_Request)
  1149. break;
  1150. dum->address = w_value;
  1151. *status = 0;
  1152. dev_dbg(udc_dev(dum), "set_address = %d\n",
  1153. w_value);
  1154. ret_val = 0;
  1155. break;
  1156. case USB_REQ_SET_FEATURE:
  1157. if (setup->bRequestType == Dev_Request) {
  1158. ret_val = 0;
  1159. switch (w_value) {
  1160. case USB_DEVICE_REMOTE_WAKEUP:
  1161. break;
  1162. case USB_DEVICE_B_HNP_ENABLE:
  1163. dum->gadget.b_hnp_enable = 1;
  1164. break;
  1165. case USB_DEVICE_A_HNP_SUPPORT:
  1166. dum->gadget.a_hnp_support = 1;
  1167. break;
  1168. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1169. dum->gadget.a_alt_hnp_support = 1;
  1170. break;
  1171. case USB_DEVICE_U1_ENABLE:
  1172. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1173. HCD_USB3)
  1174. w_value = USB_DEV_STAT_U1_ENABLED;
  1175. else
  1176. ret_val = -EOPNOTSUPP;
  1177. break;
  1178. case USB_DEVICE_U2_ENABLE:
  1179. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1180. HCD_USB3)
  1181. w_value = USB_DEV_STAT_U2_ENABLED;
  1182. else
  1183. ret_val = -EOPNOTSUPP;
  1184. break;
  1185. case USB_DEVICE_LTM_ENABLE:
  1186. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1187. HCD_USB3)
  1188. w_value = USB_DEV_STAT_LTM_ENABLED;
  1189. else
  1190. ret_val = -EOPNOTSUPP;
  1191. break;
  1192. default:
  1193. ret_val = -EOPNOTSUPP;
  1194. }
  1195. if (ret_val == 0) {
  1196. dum->devstatus |= (1 << w_value);
  1197. *status = 0;
  1198. }
  1199. } else if (setup->bRequestType == Ep_Request) {
  1200. /* endpoint halt */
  1201. ep2 = find_endpoint(dum, w_index);
  1202. if (!ep2 || ep2->ep.name == ep0name) {
  1203. ret_val = -EOPNOTSUPP;
  1204. break;
  1205. }
  1206. ep2->halted = 1;
  1207. ret_val = 0;
  1208. *status = 0;
  1209. }
  1210. break;
  1211. case USB_REQ_CLEAR_FEATURE:
  1212. if (setup->bRequestType == Dev_Request) {
  1213. ret_val = 0;
  1214. switch (w_value) {
  1215. case USB_DEVICE_REMOTE_WAKEUP:
  1216. w_value = USB_DEVICE_REMOTE_WAKEUP;
  1217. break;
  1218. case USB_DEVICE_U1_ENABLE:
  1219. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1220. HCD_USB3)
  1221. w_value = USB_DEV_STAT_U1_ENABLED;
  1222. else
  1223. ret_val = -EOPNOTSUPP;
  1224. break;
  1225. case USB_DEVICE_U2_ENABLE:
  1226. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1227. HCD_USB3)
  1228. w_value = USB_DEV_STAT_U2_ENABLED;
  1229. else
  1230. ret_val = -EOPNOTSUPP;
  1231. break;
  1232. case USB_DEVICE_LTM_ENABLE:
  1233. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1234. HCD_USB3)
  1235. w_value = USB_DEV_STAT_LTM_ENABLED;
  1236. else
  1237. ret_val = -EOPNOTSUPP;
  1238. break;
  1239. default:
  1240. ret_val = -EOPNOTSUPP;
  1241. break;
  1242. }
  1243. if (ret_val == 0) {
  1244. dum->devstatus &= ~(1 << w_value);
  1245. *status = 0;
  1246. }
  1247. } else if (setup->bRequestType == Ep_Request) {
  1248. /* endpoint halt */
  1249. ep2 = find_endpoint(dum, w_index);
  1250. if (!ep2) {
  1251. ret_val = -EOPNOTSUPP;
  1252. break;
  1253. }
  1254. if (!ep2->wedged)
  1255. ep2->halted = 0;
  1256. ret_val = 0;
  1257. *status = 0;
  1258. }
  1259. break;
  1260. case USB_REQ_GET_STATUS:
  1261. if (setup->bRequestType == Dev_InRequest
  1262. || setup->bRequestType == Intf_InRequest
  1263. || setup->bRequestType == Ep_InRequest) {
  1264. char *buf;
  1265. /*
  1266. * device: remote wakeup, selfpowered
  1267. * interface: nothing
  1268. * endpoint: halt
  1269. */
  1270. buf = (char *)urb->transfer_buffer;
  1271. if (urb->transfer_buffer_length > 0) {
  1272. if (setup->bRequestType == Ep_InRequest) {
  1273. ep2 = find_endpoint(dum, w_index);
  1274. if (!ep2) {
  1275. ret_val = -EOPNOTSUPP;
  1276. break;
  1277. }
  1278. buf[0] = ep2->halted;
  1279. } else if (setup->bRequestType ==
  1280. Dev_InRequest) {
  1281. buf[0] = (u8)dum->devstatus;
  1282. } else
  1283. buf[0] = 0;
  1284. }
  1285. if (urb->transfer_buffer_length > 1)
  1286. buf[1] = 0;
  1287. urb->actual_length = min_t(u32, 2,
  1288. urb->transfer_buffer_length);
  1289. ret_val = 0;
  1290. *status = 0;
  1291. }
  1292. break;
  1293. }
  1294. return ret_val;
  1295. }
  1296. /* drive both sides of the transfers; looks like irq handlers to
  1297. * both drivers except the callbacks aren't in_irq().
  1298. */
  1299. static void dummy_timer(unsigned long _dum_hcd)
  1300. {
  1301. struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
  1302. struct dummy *dum = dum_hcd->dum;
  1303. struct urbp *urbp, *tmp;
  1304. unsigned long flags;
  1305. int limit, total;
  1306. int i;
  1307. /* simplistic model for one frame's bandwidth */
  1308. switch (dum->gadget.speed) {
  1309. case USB_SPEED_LOW:
  1310. total = 8/*bytes*/ * 12/*packets*/;
  1311. break;
  1312. case USB_SPEED_FULL:
  1313. total = 64/*bytes*/ * 19/*packets*/;
  1314. break;
  1315. case USB_SPEED_HIGH:
  1316. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1317. break;
  1318. case USB_SPEED_SUPER:
  1319. /* Bus speed is 500000 bytes/ms, so use a little less */
  1320. total = 490000;
  1321. break;
  1322. default:
  1323. dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
  1324. return;
  1325. }
  1326. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1327. /* look at each urb queued by the host side driver */
  1328. spin_lock_irqsave (&dum->lock, flags);
  1329. if (!dum_hcd->udev) {
  1330. dev_err(dummy_dev(dum_hcd),
  1331. "timer fired with no URBs pending?\n");
  1332. spin_unlock_irqrestore (&dum->lock, flags);
  1333. return;
  1334. }
  1335. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1336. if (!ep_name [i])
  1337. break;
  1338. dum->ep [i].already_seen = 0;
  1339. }
  1340. restart:
  1341. list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
  1342. struct urb *urb;
  1343. struct dummy_request *req;
  1344. u8 address;
  1345. struct dummy_ep *ep = NULL;
  1346. int type;
  1347. int status = -EINPROGRESS;
  1348. urb = urbp->urb;
  1349. if (urb->unlinked)
  1350. goto return_urb;
  1351. else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
  1352. continue;
  1353. type = usb_pipetype (urb->pipe);
  1354. /* used up this frame's non-periodic bandwidth?
  1355. * FIXME there's infinite bandwidth for control and
  1356. * periodic transfers ... unrealistic.
  1357. */
  1358. if (total <= 0 && type == PIPE_BULK)
  1359. continue;
  1360. /* find the gadget's ep for this request (if configured) */
  1361. address = usb_pipeendpoint (urb->pipe);
  1362. if (usb_pipein (urb->pipe))
  1363. address |= USB_DIR_IN;
  1364. ep = find_endpoint(dum, address);
  1365. if (!ep) {
  1366. /* set_configuration() disagreement */
  1367. dev_dbg(dummy_dev(dum_hcd),
  1368. "no ep configured for urb %p\n",
  1369. urb);
  1370. status = -EPROTO;
  1371. goto return_urb;
  1372. }
  1373. if (ep->already_seen)
  1374. continue;
  1375. ep->already_seen = 1;
  1376. if (ep == &dum->ep [0] && urb->error_count) {
  1377. ep->setup_stage = 1; /* a new urb */
  1378. urb->error_count = 0;
  1379. }
  1380. if (ep->halted && !ep->setup_stage) {
  1381. /* NOTE: must not be iso! */
  1382. dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
  1383. ep->ep.name, urb);
  1384. status = -EPIPE;
  1385. goto return_urb;
  1386. }
  1387. /* FIXME make sure both ends agree on maxpacket */
  1388. /* handle control requests */
  1389. if (ep == &dum->ep [0] && ep->setup_stage) {
  1390. struct usb_ctrlrequest setup;
  1391. int value = 1;
  1392. setup = *(struct usb_ctrlrequest*) urb->setup_packet;
  1393. /* paranoia, in case of stale queued data */
  1394. list_for_each_entry (req, &ep->queue, queue) {
  1395. list_del_init (&req->queue);
  1396. req->req.status = -EOVERFLOW;
  1397. dev_dbg (udc_dev(dum), "stale req = %p\n",
  1398. req);
  1399. spin_unlock (&dum->lock);
  1400. req->req.complete (&ep->ep, &req->req);
  1401. spin_lock (&dum->lock);
  1402. ep->already_seen = 0;
  1403. goto restart;
  1404. }
  1405. /* gadget driver never sees set_address or operations
  1406. * on standard feature flags. some hardware doesn't
  1407. * even expose them.
  1408. */
  1409. ep->last_io = jiffies;
  1410. ep->setup_stage = 0;
  1411. ep->halted = 0;
  1412. value = handle_control_request(dum_hcd, urb, &setup,
  1413. &status);
  1414. /* gadget driver handles all other requests. block
  1415. * until setup() returns; no reentrancy issues etc.
  1416. */
  1417. if (value > 0) {
  1418. spin_unlock (&dum->lock);
  1419. value = dum->driver->setup (&dum->gadget,
  1420. &setup);
  1421. spin_lock (&dum->lock);
  1422. if (value >= 0) {
  1423. /* no delays (max 64KB data stage) */
  1424. limit = 64*1024;
  1425. goto treat_control_like_bulk;
  1426. }
  1427. /* error, see below */
  1428. }
  1429. if (value < 0) {
  1430. if (value != -EOPNOTSUPP)
  1431. dev_dbg (udc_dev(dum),
  1432. "setup --> %d\n",
  1433. value);
  1434. status = -EPIPE;
  1435. urb->actual_length = 0;
  1436. }
  1437. goto return_urb;
  1438. }
  1439. /* non-control requests */
  1440. limit = total;
  1441. switch (usb_pipetype (urb->pipe)) {
  1442. case PIPE_ISOCHRONOUS:
  1443. /* FIXME is it urb->interval since the last xfer?
  1444. * use urb->iso_frame_desc[i].
  1445. * complete whether or not ep has requests queued.
  1446. * report random errors, to debug drivers.
  1447. */
  1448. limit = max (limit, periodic_bytes (dum, ep));
  1449. status = -ENOSYS;
  1450. break;
  1451. case PIPE_INTERRUPT:
  1452. /* FIXME is it urb->interval since the last xfer?
  1453. * this almost certainly polls too fast.
  1454. */
  1455. limit = max (limit, periodic_bytes (dum, ep));
  1456. /* FALLTHROUGH */
  1457. // case PIPE_BULK: case PIPE_CONTROL:
  1458. default:
  1459. treat_control_like_bulk:
  1460. ep->last_io = jiffies;
  1461. total = transfer(dum, urb, ep, limit, &status);
  1462. break;
  1463. }
  1464. /* incomplete transfer? */
  1465. if (status == -EINPROGRESS)
  1466. continue;
  1467. return_urb:
  1468. list_del (&urbp->urbp_list);
  1469. kfree (urbp);
  1470. if (ep)
  1471. ep->already_seen = ep->setup_stage = 0;
  1472. usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
  1473. spin_unlock (&dum->lock);
  1474. usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
  1475. spin_lock (&dum->lock);
  1476. goto restart;
  1477. }
  1478. if (list_empty(&dum_hcd->urbp_list)) {
  1479. usb_put_dev(dum_hcd->udev);
  1480. dum_hcd->udev = NULL;
  1481. } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  1482. /* want a 1 msec delay here */
  1483. mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
  1484. }
  1485. spin_unlock_irqrestore (&dum->lock, flags);
  1486. }
  1487. /*-------------------------------------------------------------------------*/
  1488. #define PORT_C_MASK \
  1489. ((USB_PORT_STAT_C_CONNECTION \
  1490. | USB_PORT_STAT_C_ENABLE \
  1491. | USB_PORT_STAT_C_SUSPEND \
  1492. | USB_PORT_STAT_C_OVERCURRENT \
  1493. | USB_PORT_STAT_C_RESET) << 16)
  1494. static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
  1495. {
  1496. struct dummy_hcd *dum_hcd;
  1497. unsigned long flags;
  1498. int retval = 0;
  1499. dum_hcd = hcd_to_dummy_hcd(hcd);
  1500. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1501. if (!HCD_HW_ACCESSIBLE(hcd))
  1502. goto done;
  1503. if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1504. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1505. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1506. set_link_state(dum_hcd);
  1507. }
  1508. if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
  1509. *buf = (1 << 1);
  1510. dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
  1511. dum_hcd->port_status);
  1512. retval = 1;
  1513. if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
  1514. usb_hcd_resume_root_hub (hcd);
  1515. }
  1516. done:
  1517. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1518. return retval;
  1519. }
  1520. static inline void
  1521. ss_hub_descriptor(struct usb_hub_descriptor *desc)
  1522. {
  1523. memset(desc, 0, sizeof *desc);
  1524. desc->bDescriptorType = 0x2a;
  1525. desc->bDescLength = 12;
  1526. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1527. desc->bNbrPorts = 1;
  1528. desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
  1529. desc->u.ss.DeviceRemovable = 0xffff;
  1530. }
  1531. static inline void
  1532. hub_descriptor (struct usb_hub_descriptor *desc)
  1533. {
  1534. memset (desc, 0, sizeof *desc);
  1535. desc->bDescriptorType = 0x29;
  1536. desc->bDescLength = 9;
  1537. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1538. desc->bNbrPorts = 1;
  1539. desc->u.hs.DeviceRemovable[0] = 0xff;
  1540. desc->u.hs.DeviceRemovable[1] = 0xff;
  1541. }
  1542. static int dummy_hub_control (
  1543. struct usb_hcd *hcd,
  1544. u16 typeReq,
  1545. u16 wValue,
  1546. u16 wIndex,
  1547. char *buf,
  1548. u16 wLength
  1549. ) {
  1550. struct dummy_hcd *dum_hcd;
  1551. int retval = 0;
  1552. unsigned long flags;
  1553. if (!HCD_HW_ACCESSIBLE(hcd))
  1554. return -ETIMEDOUT;
  1555. dum_hcd = hcd_to_dummy_hcd(hcd);
  1556. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1557. switch (typeReq) {
  1558. case ClearHubFeature:
  1559. break;
  1560. case ClearPortFeature:
  1561. switch (wValue) {
  1562. case USB_PORT_FEAT_SUSPEND:
  1563. if (hcd->speed == HCD_USB3) {
  1564. dev_dbg(dummy_dev(dum_hcd),
  1565. "USB_PORT_FEAT_SUSPEND req not "
  1566. "supported for USB 3.0 roothub\n");
  1567. goto error;
  1568. }
  1569. if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
  1570. /* 20msec resume signaling */
  1571. dum_hcd->resuming = 1;
  1572. dum_hcd->re_timeout = jiffies +
  1573. msecs_to_jiffies(20);
  1574. }
  1575. break;
  1576. case USB_PORT_FEAT_POWER:
  1577. if (hcd->speed == HCD_USB3) {
  1578. if (dum_hcd->port_status & USB_PORT_STAT_POWER)
  1579. dev_dbg(dummy_dev(dum_hcd),
  1580. "power-off\n");
  1581. } else
  1582. if (dum_hcd->port_status &
  1583. USB_SS_PORT_STAT_POWER)
  1584. dev_dbg(dummy_dev(dum_hcd),
  1585. "power-off\n");
  1586. /* FALLS THROUGH */
  1587. default:
  1588. dum_hcd->port_status &= ~(1 << wValue);
  1589. set_link_state(dum_hcd);
  1590. }
  1591. break;
  1592. case GetHubDescriptor:
  1593. if (hcd->speed == HCD_USB3 &&
  1594. (wLength < USB_DT_SS_HUB_SIZE ||
  1595. wValue != (USB_DT_SS_HUB << 8))) {
  1596. dev_dbg(dummy_dev(dum_hcd),
  1597. "Wrong hub descriptor type for "
  1598. "USB 3.0 roothub.\n");
  1599. goto error;
  1600. }
  1601. if (hcd->speed == HCD_USB3)
  1602. ss_hub_descriptor((struct usb_hub_descriptor *) buf);
  1603. else
  1604. hub_descriptor((struct usb_hub_descriptor *) buf);
  1605. break;
  1606. case GetHubStatus:
  1607. *(__le32 *) buf = cpu_to_le32 (0);
  1608. break;
  1609. case GetPortStatus:
  1610. if (wIndex != 1)
  1611. retval = -EPIPE;
  1612. /* whoever resets or resumes must GetPortStatus to
  1613. * complete it!!
  1614. */
  1615. if (dum_hcd->resuming &&
  1616. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1617. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1618. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1619. }
  1620. if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
  1621. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1622. dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
  1623. dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
  1624. if (dum_hcd->dum->pullup) {
  1625. dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
  1626. if (hcd->speed < HCD_USB3) {
  1627. switch (dum_hcd->dum->gadget.speed) {
  1628. case USB_SPEED_HIGH:
  1629. dum_hcd->port_status |=
  1630. USB_PORT_STAT_HIGH_SPEED;
  1631. break;
  1632. case USB_SPEED_LOW:
  1633. dum_hcd->dum->gadget.ep0->
  1634. maxpacket = 8;
  1635. dum_hcd->port_status |=
  1636. USB_PORT_STAT_LOW_SPEED;
  1637. break;
  1638. default:
  1639. dum_hcd->dum->gadget.speed =
  1640. USB_SPEED_FULL;
  1641. break;
  1642. }
  1643. }
  1644. }
  1645. }
  1646. set_link_state(dum_hcd);
  1647. ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
  1648. ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
  1649. break;
  1650. case SetHubFeature:
  1651. retval = -EPIPE;
  1652. break;
  1653. case SetPortFeature:
  1654. switch (wValue) {
  1655. case USB_PORT_FEAT_LINK_STATE:
  1656. if (hcd->speed != HCD_USB3) {
  1657. dev_dbg(dummy_dev(dum_hcd),
  1658. "USB_PORT_FEAT_LINK_STATE req not "
  1659. "supported for USB 2.0 roothub\n");
  1660. goto error;
  1661. }
  1662. /*
  1663. * Since this is dummy we don't have an actual link so
  1664. * there is nothing to do for the SET_LINK_STATE cmd
  1665. */
  1666. break;
  1667. case USB_PORT_FEAT_U1_TIMEOUT:
  1668. case USB_PORT_FEAT_U2_TIMEOUT:
  1669. /* TODO: add suspend/resume support! */
  1670. if (hcd->speed != HCD_USB3) {
  1671. dev_dbg(dummy_dev(dum_hcd),
  1672. "USB_PORT_FEAT_U1/2_TIMEOUT req not "
  1673. "supported for USB 2.0 roothub\n");
  1674. goto error;
  1675. }
  1676. break;
  1677. case USB_PORT_FEAT_SUSPEND:
  1678. /* Applicable only for USB2.0 hub */
  1679. if (hcd->speed == HCD_USB3) {
  1680. dev_dbg(dummy_dev(dum_hcd),
  1681. "USB_PORT_FEAT_SUSPEND req not "
  1682. "supported for USB 3.0 roothub\n");
  1683. goto error;
  1684. }
  1685. if (dum_hcd->active) {
  1686. dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
  1687. /* HNP would happen here; for now we
  1688. * assume b_bus_req is always true.
  1689. */
  1690. set_link_state(dum_hcd);
  1691. if (((1 << USB_DEVICE_B_HNP_ENABLE)
  1692. & dum_hcd->dum->devstatus) != 0)
  1693. dev_dbg(dummy_dev(dum_hcd),
  1694. "no HNP yet!\n");
  1695. }
  1696. break;
  1697. case USB_PORT_FEAT_POWER:
  1698. if (hcd->speed == HCD_USB3)
  1699. dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
  1700. else
  1701. dum_hcd->port_status |= USB_PORT_STAT_POWER;
  1702. set_link_state(dum_hcd);
  1703. break;
  1704. case USB_PORT_FEAT_BH_PORT_RESET:
  1705. /* Applicable only for USB3.0 hub */
  1706. if (hcd->speed != HCD_USB3) {
  1707. dev_dbg(dummy_dev(dum_hcd),
  1708. "USB_PORT_FEAT_BH_PORT_RESET req not "
  1709. "supported for USB 2.0 roothub\n");
  1710. goto error;
  1711. }
  1712. /* FALLS THROUGH */
  1713. case USB_PORT_FEAT_RESET:
  1714. /* if it's already enabled, disable */
  1715. if (hcd->speed == HCD_USB3) {
  1716. dum_hcd->port_status = 0;
  1717. dum_hcd->port_status =
  1718. (USB_SS_PORT_STAT_POWER |
  1719. USB_PORT_STAT_CONNECTION |
  1720. USB_PORT_STAT_RESET);
  1721. } else
  1722. dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
  1723. | USB_PORT_STAT_LOW_SPEED
  1724. | USB_PORT_STAT_HIGH_SPEED);
  1725. /*
  1726. * We want to reset device status. All but the
  1727. * Self powered feature
  1728. */
  1729. dum_hcd->dum->devstatus &=
  1730. (1 << USB_DEVICE_SELF_POWERED);
  1731. /*
  1732. * FIXME USB3.0: what is the correct reset signaling
  1733. * interval? Is it still 50msec as for HS?
  1734. */
  1735. dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
  1736. /* FALLS THROUGH */
  1737. default:
  1738. if (hcd->speed == HCD_USB3) {
  1739. if ((dum_hcd->port_status &
  1740. USB_SS_PORT_STAT_POWER) != 0) {
  1741. dum_hcd->port_status |= (1 << wValue);
  1742. set_link_state(dum_hcd);
  1743. }
  1744. } else
  1745. if ((dum_hcd->port_status &
  1746. USB_PORT_STAT_POWER) != 0) {
  1747. dum_hcd->port_status |= (1 << wValue);
  1748. set_link_state(dum_hcd);
  1749. }
  1750. }
  1751. break;
  1752. case GetPortErrorCount:
  1753. if (hcd->speed != HCD_USB3) {
  1754. dev_dbg(dummy_dev(dum_hcd),
  1755. "GetPortErrorCount req not "
  1756. "supported for USB 2.0 roothub\n");
  1757. goto error;
  1758. }
  1759. /* We'll always return 0 since this is a dummy hub */
  1760. *(__le32 *) buf = cpu_to_le32(0);
  1761. break;
  1762. case SetHubDepth:
  1763. if (hcd->speed != HCD_USB3) {
  1764. dev_dbg(dummy_dev(dum_hcd),
  1765. "SetHubDepth req not supported for "
  1766. "USB 2.0 roothub\n");
  1767. goto error;
  1768. }
  1769. break;
  1770. default:
  1771. dev_dbg(dummy_dev(dum_hcd),
  1772. "hub control req%04x v%04x i%04x l%d\n",
  1773. typeReq, wValue, wIndex, wLength);
  1774. error:
  1775. /* "protocol stall" on error */
  1776. retval = -EPIPE;
  1777. }
  1778. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1779. if ((dum_hcd->port_status & PORT_C_MASK) != 0)
  1780. usb_hcd_poll_rh_status (hcd);
  1781. return retval;
  1782. }
  1783. static int dummy_bus_suspend (struct usb_hcd *hcd)
  1784. {
  1785. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1786. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1787. spin_lock_irq(&dum_hcd->dum->lock);
  1788. dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
  1789. set_link_state(dum_hcd);
  1790. hcd->state = HC_STATE_SUSPENDED;
  1791. spin_unlock_irq(&dum_hcd->dum->lock);
  1792. return 0;
  1793. }
  1794. static int dummy_bus_resume (struct usb_hcd *hcd)
  1795. {
  1796. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1797. int rc = 0;
  1798. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1799. spin_lock_irq(&dum_hcd->dum->lock);
  1800. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1801. rc = -ESHUTDOWN;
  1802. } else {
  1803. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1804. set_link_state(dum_hcd);
  1805. if (!list_empty(&dum_hcd->urbp_list))
  1806. mod_timer(&dum_hcd->timer, jiffies);
  1807. hcd->state = HC_STATE_RUNNING;
  1808. }
  1809. spin_unlock_irq(&dum_hcd->dum->lock);
  1810. return rc;
  1811. }
  1812. /*-------------------------------------------------------------------------*/
  1813. static inline ssize_t
  1814. show_urb (char *buf, size_t size, struct urb *urb)
  1815. {
  1816. int ep = usb_pipeendpoint (urb->pipe);
  1817. return snprintf (buf, size,
  1818. "urb/%p %s ep%d%s%s len %d/%d\n",
  1819. urb,
  1820. ({ char *s;
  1821. switch (urb->dev->speed) {
  1822. case USB_SPEED_LOW:
  1823. s = "ls";
  1824. break;
  1825. case USB_SPEED_FULL:
  1826. s = "fs";
  1827. break;
  1828. case USB_SPEED_HIGH:
  1829. s = "hs";
  1830. break;
  1831. case USB_SPEED_SUPER:
  1832. s = "ss";
  1833. break;
  1834. default:
  1835. s = "?";
  1836. break;
  1837. }; s; }),
  1838. ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
  1839. ({ char *s; \
  1840. switch (usb_pipetype (urb->pipe)) { \
  1841. case PIPE_CONTROL: \
  1842. s = ""; \
  1843. break; \
  1844. case PIPE_BULK: \
  1845. s = "-bulk"; \
  1846. break; \
  1847. case PIPE_INTERRUPT: \
  1848. s = "-int"; \
  1849. break; \
  1850. default: \
  1851. s = "-iso"; \
  1852. break; \
  1853. }; s;}),
  1854. urb->actual_length, urb->transfer_buffer_length);
  1855. }
  1856. static ssize_t
  1857. show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
  1858. {
  1859. struct usb_hcd *hcd = dev_get_drvdata (dev);
  1860. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1861. struct urbp *urbp;
  1862. size_t size = 0;
  1863. unsigned long flags;
  1864. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1865. list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
  1866. size_t temp;
  1867. temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
  1868. buf += temp;
  1869. size += temp;
  1870. }
  1871. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1872. return size;
  1873. }
  1874. static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
  1875. static int dummy_start_ss(struct dummy_hcd *dum_hcd)
  1876. {
  1877. init_timer(&dum_hcd->timer);
  1878. dum_hcd->timer.function = dummy_timer;
  1879. dum_hcd->timer.data = (unsigned long)dum_hcd;
  1880. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1881. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  1882. dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
  1883. dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
  1884. dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
  1885. #ifdef CONFIG_USB_OTG
  1886. dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
  1887. #endif
  1888. return 0;
  1889. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  1890. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  1891. }
  1892. static int dummy_start(struct usb_hcd *hcd)
  1893. {
  1894. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1895. /*
  1896. * MASTER side init ... we emulate a root hub that'll only ever
  1897. * talk to one device (the slave side). Also appears in sysfs,
  1898. * just like more familiar pci-based HCDs.
  1899. */
  1900. if (!usb_hcd_is_primary_hcd(hcd))
  1901. return dummy_start_ss(dum_hcd);
  1902. spin_lock_init(&dum_hcd->dum->lock);
  1903. init_timer(&dum_hcd->timer);
  1904. dum_hcd->timer.function = dummy_timer;
  1905. dum_hcd->timer.data = (unsigned long)dum_hcd;
  1906. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1907. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  1908. hcd->power_budget = POWER_BUDGET;
  1909. hcd->state = HC_STATE_RUNNING;
  1910. hcd->uses_new_polling = 1;
  1911. #ifdef CONFIG_USB_OTG
  1912. hcd->self.otg_port = 1;
  1913. #endif
  1914. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  1915. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  1916. }
  1917. static void dummy_stop (struct usb_hcd *hcd)
  1918. {
  1919. struct dummy *dum;
  1920. dum = (hcd_to_dummy_hcd(hcd))->dum;
  1921. device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
  1922. usb_gadget_unregister_driver(dum->driver);
  1923. dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
  1924. }
  1925. /*-------------------------------------------------------------------------*/
  1926. static int dummy_h_get_frame (struct usb_hcd *hcd)
  1927. {
  1928. return dummy_g_get_frame (NULL);
  1929. }
  1930. static int dummy_setup(struct usb_hcd *hcd)
  1931. {
  1932. if (usb_hcd_is_primary_hcd(hcd)) {
  1933. the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
  1934. the_controller.hs_hcd->dum = &the_controller;
  1935. /*
  1936. * Mark the first roothub as being USB 2.0.
  1937. * The USB 3.0 roothub will be registered later by
  1938. * dummy_hcd_probe()
  1939. */
  1940. hcd->speed = HCD_USB2;
  1941. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  1942. } else {
  1943. the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
  1944. the_controller.ss_hcd->dum = &the_controller;
  1945. hcd->speed = HCD_USB3;
  1946. hcd->self.root_hub->speed = USB_SPEED_SUPER;
  1947. }
  1948. return 0;
  1949. }
  1950. /* Change a group of bulk endpoints to support multiple stream IDs */
  1951. int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1952. struct usb_host_endpoint **eps, unsigned int num_eps,
  1953. unsigned int num_streams, gfp_t mem_flags)
  1954. {
  1955. if (hcd->speed != HCD_USB3)
  1956. dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
  1957. "%s() - ERROR! Not supported for USB2.0 roothub\n",
  1958. __func__);
  1959. return 0;
  1960. }
  1961. /* Reverts a group of bulk endpoints back to not using stream IDs. */
  1962. int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1963. struct usb_host_endpoint **eps, unsigned int num_eps,
  1964. gfp_t mem_flags)
  1965. {
  1966. if (hcd->speed != HCD_USB3)
  1967. dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
  1968. "%s() - ERROR! Not supported for USB2.0 roothub\n",
  1969. __func__);
  1970. return 0;
  1971. }
  1972. static struct hc_driver dummy_hcd = {
  1973. .description = (char *) driver_name,
  1974. .product_desc = "Dummy host controller",
  1975. .hcd_priv_size = sizeof(struct dummy_hcd),
  1976. .flags = HCD_USB3 | HCD_SHARED,
  1977. .reset = dummy_setup,
  1978. .start = dummy_start,
  1979. .stop = dummy_stop,
  1980. .urb_enqueue = dummy_urb_enqueue,
  1981. .urb_dequeue = dummy_urb_dequeue,
  1982. .get_frame_number = dummy_h_get_frame,
  1983. .hub_status_data = dummy_hub_status,
  1984. .hub_control = dummy_hub_control,
  1985. .bus_suspend = dummy_bus_suspend,
  1986. .bus_resume = dummy_bus_resume,
  1987. .alloc_streams = dummy_alloc_streams,
  1988. .free_streams = dummy_free_streams,
  1989. };
  1990. static int dummy_hcd_probe(struct platform_device *pdev)
  1991. {
  1992. struct usb_hcd *hs_hcd;
  1993. struct usb_hcd *ss_hcd;
  1994. int retval;
  1995. dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
  1996. if (!mod_data.is_super_speed)
  1997. dummy_hcd.flags = HCD_USB2;
  1998. hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
  1999. if (!hs_hcd)
  2000. return -ENOMEM;
  2001. hs_hcd->has_tt = 1;
  2002. retval = usb_add_hcd(hs_hcd, 0, 0);
  2003. if (retval != 0) {
  2004. usb_put_hcd(hs_hcd);
  2005. return retval;
  2006. }
  2007. if (mod_data.is_super_speed) {
  2008. ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
  2009. dev_name(&pdev->dev), hs_hcd);
  2010. if (!ss_hcd) {
  2011. retval = -ENOMEM;
  2012. goto dealloc_usb2_hcd;
  2013. }
  2014. retval = usb_add_hcd(ss_hcd, 0, 0);
  2015. if (retval)
  2016. goto put_usb3_hcd;
  2017. }
  2018. return 0;
  2019. put_usb3_hcd:
  2020. usb_put_hcd(ss_hcd);
  2021. dealloc_usb2_hcd:
  2022. usb_put_hcd(hs_hcd);
  2023. the_controller.hs_hcd = the_controller.ss_hcd = NULL;
  2024. return retval;
  2025. }
  2026. static int dummy_hcd_remove(struct platform_device *pdev)
  2027. {
  2028. struct dummy *dum;
  2029. dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
  2030. if (dum->ss_hcd) {
  2031. usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2032. usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2033. }
  2034. usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2035. usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2036. the_controller.hs_hcd = NULL;
  2037. the_controller.ss_hcd = NULL;
  2038. return 0;
  2039. }
  2040. static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
  2041. {
  2042. struct usb_hcd *hcd;
  2043. struct dummy_hcd *dum_hcd;
  2044. int rc = 0;
  2045. dev_dbg (&pdev->dev, "%s\n", __func__);
  2046. hcd = platform_get_drvdata (pdev);
  2047. dum_hcd = hcd_to_dummy_hcd(hcd);
  2048. if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  2049. dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
  2050. rc = -EBUSY;
  2051. } else
  2052. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2053. return rc;
  2054. }
  2055. static int dummy_hcd_resume (struct platform_device *pdev)
  2056. {
  2057. struct usb_hcd *hcd;
  2058. dev_dbg (&pdev->dev, "%s\n", __func__);
  2059. hcd = platform_get_drvdata (pdev);
  2060. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2061. usb_hcd_poll_rh_status (hcd);
  2062. return 0;
  2063. }
  2064. static struct platform_driver dummy_hcd_driver = {
  2065. .probe = dummy_hcd_probe,
  2066. .remove = dummy_hcd_remove,
  2067. .suspend = dummy_hcd_suspend,
  2068. .resume = dummy_hcd_resume,
  2069. .driver = {
  2070. .name = (char *) driver_name,
  2071. .owner = THIS_MODULE,
  2072. },
  2073. };
  2074. /*-------------------------------------------------------------------------*/
  2075. static struct platform_device *the_udc_pdev;
  2076. static struct platform_device *the_hcd_pdev;
  2077. static int __init init (void)
  2078. {
  2079. int retval = -ENOMEM;
  2080. if (usb_disabled ())
  2081. return -ENODEV;
  2082. if (!mod_data.is_high_speed && mod_data.is_super_speed)
  2083. return -EINVAL;
  2084. the_hcd_pdev = platform_device_alloc(driver_name, -1);
  2085. if (!the_hcd_pdev)
  2086. return retval;
  2087. the_udc_pdev = platform_device_alloc(gadget_name, -1);
  2088. if (!the_udc_pdev)
  2089. goto err_alloc_udc;
  2090. retval = platform_driver_register(&dummy_hcd_driver);
  2091. if (retval < 0)
  2092. goto err_register_hcd_driver;
  2093. retval = platform_driver_register(&dummy_udc_driver);
  2094. if (retval < 0)
  2095. goto err_register_udc_driver;
  2096. retval = platform_device_add(the_hcd_pdev);
  2097. if (retval < 0)
  2098. goto err_add_hcd;
  2099. if (!the_controller.hs_hcd ||
  2100. (!the_controller.ss_hcd && mod_data.is_super_speed)) {
  2101. /*
  2102. * The hcd was added successfully but its probe function failed
  2103. * for some reason.
  2104. */
  2105. retval = -EINVAL;
  2106. goto err_add_udc;
  2107. }
  2108. retval = platform_device_add(the_udc_pdev);
  2109. if (retval < 0)
  2110. goto err_add_udc;
  2111. if (!platform_get_drvdata(the_udc_pdev)) {
  2112. /*
  2113. * The udc was added successfully but its probe function failed
  2114. * for some reason.
  2115. */
  2116. retval = -EINVAL;
  2117. goto err_probe_udc;
  2118. }
  2119. return retval;
  2120. err_probe_udc:
  2121. platform_device_del(the_udc_pdev);
  2122. err_add_udc:
  2123. platform_device_del(the_hcd_pdev);
  2124. err_add_hcd:
  2125. platform_driver_unregister(&dummy_udc_driver);
  2126. err_register_udc_driver:
  2127. platform_driver_unregister(&dummy_hcd_driver);
  2128. err_register_hcd_driver:
  2129. platform_device_put(the_udc_pdev);
  2130. err_alloc_udc:
  2131. platform_device_put(the_hcd_pdev);
  2132. return retval;
  2133. }
  2134. module_init (init);
  2135. static void __exit cleanup (void)
  2136. {
  2137. platform_device_unregister(the_udc_pdev);
  2138. platform_device_unregister(the_hcd_pdev);
  2139. platform_driver_unregister(&dummy_udc_driver);
  2140. platform_driver_unregister(&dummy_hcd_driver);
  2141. }
  2142. module_exit (cleanup);