dummy_hcd.c 71 KB

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