dummy_hcd.c 72 KB

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