dummy_hcd.c 72 KB

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