dummy_hcd.c 62 KB

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