dummy_hcd.c 66 KB

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