dummy_hcd.c 62 KB

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