dummy_hcd.c 71 KB

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