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