dummy_hcd.c 69 KB

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