dummy_hcd.c 73 KB

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