dummy_hcd.c 69 KB

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