dummy_hcd.c 62 KB

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