dummy_hcd.c 62 KB

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