dummy_hcd.c 66 KB

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