dummy_hcd.c 63 KB

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