dummy_hcd.c 63 KB

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