dummy_hcd.c 68 KB

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