dummy_hcd.c 71 KB

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