dummy_hcd.c 67 KB

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