dummy_hcd.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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 <asm/byteorder.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <asm/system.h>
  51. #include <asm/unaligned.h>
  52. #include "../core/hcd.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. /*-------------------------------------------------------------------------*/
  63. /* gadget side driver data structres */
  64. struct dummy_ep {
  65. struct list_head queue;
  66. unsigned long last_io; /* jiffies timestamp */
  67. struct usb_gadget *gadget;
  68. const struct usb_endpoint_descriptor *desc;
  69. struct usb_ep ep;
  70. unsigned halted : 1;
  71. unsigned already_seen : 1;
  72. unsigned setup_stage : 1;
  73. };
  74. struct dummy_request {
  75. struct list_head queue; /* ep's requests */
  76. struct usb_request req;
  77. };
  78. static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
  79. {
  80. return container_of (_ep, struct dummy_ep, ep);
  81. }
  82. static inline struct dummy_request *usb_request_to_dummy_request
  83. (struct usb_request *_req)
  84. {
  85. return container_of (_req, struct dummy_request, req);
  86. }
  87. /*-------------------------------------------------------------------------*/
  88. /*
  89. * Every device has ep0 for control requests, plus up to 30 more endpoints,
  90. * in one of two types:
  91. *
  92. * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
  93. * number can be changed. Names like "ep-a" are used for this type.
  94. *
  95. * - Fixed Function: in other cases. some characteristics may be mutable;
  96. * that'd be hardware-specific. Names like "ep12out-bulk" are used.
  97. *
  98. * Gadget drivers are responsible for not setting up conflicting endpoint
  99. * configurations, illegal or unsupported packet lengths, and so on.
  100. */
  101. static const char ep0name [] = "ep0";
  102. static const char *const ep_name [] = {
  103. ep0name, /* everyone has ep0 */
  104. /* act like a net2280: high speed, six configurable endpoints */
  105. "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
  106. /* or like pxa250: fifteen fixed function endpoints */
  107. "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
  108. "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
  109. "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
  110. "ep15in-int",
  111. /* or like sa1100: two fixed function endpoints */
  112. "ep1out-bulk", "ep2in-bulk",
  113. };
  114. #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
  115. /*-------------------------------------------------------------------------*/
  116. #define FIFO_SIZE 64
  117. struct urbp {
  118. struct urb *urb;
  119. struct list_head urbp_list;
  120. };
  121. enum dummy_rh_state {
  122. DUMMY_RH_RESET,
  123. DUMMY_RH_SUSPENDED,
  124. DUMMY_RH_RUNNING
  125. };
  126. struct dummy {
  127. spinlock_t lock;
  128. /*
  129. * SLAVE/GADGET side support
  130. */
  131. struct dummy_ep ep [DUMMY_ENDPOINTS];
  132. int address;
  133. struct usb_gadget gadget;
  134. struct usb_gadget_driver *driver;
  135. struct dummy_request fifo_req;
  136. u8 fifo_buf [FIFO_SIZE];
  137. u16 devstatus;
  138. unsigned udc_suspended:1;
  139. unsigned pullup:1;
  140. unsigned active:1;
  141. unsigned old_active:1;
  142. /*
  143. * MASTER/HOST side support
  144. */
  145. enum dummy_rh_state rh_state;
  146. struct timer_list timer;
  147. u32 port_status;
  148. u32 old_status;
  149. unsigned resuming:1;
  150. unsigned long re_timeout;
  151. struct usb_device *udev;
  152. struct list_head urbp_list;
  153. };
  154. static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
  155. {
  156. return (struct dummy *) (hcd->hcd_priv);
  157. }
  158. static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
  159. {
  160. return container_of((void *) dum, struct usb_hcd, hcd_priv);
  161. }
  162. static inline struct device *dummy_dev (struct dummy *dum)
  163. {
  164. return dummy_to_hcd(dum)->self.controller;
  165. }
  166. static inline struct device *udc_dev (struct dummy *dum)
  167. {
  168. return dum->gadget.dev.parent;
  169. }
  170. static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
  171. {
  172. return container_of (ep->gadget, struct dummy, gadget);
  173. }
  174. static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
  175. {
  176. return container_of (gadget, struct dummy, gadget);
  177. }
  178. static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
  179. {
  180. return container_of (dev, struct dummy, gadget.dev);
  181. }
  182. static struct dummy *the_controller;
  183. /*-------------------------------------------------------------------------*/
  184. /* SLAVE/GADGET SIDE UTILITY ROUTINES */
  185. /* called with spinlock held */
  186. static void nuke (struct dummy *dum, struct dummy_ep *ep)
  187. {
  188. while (!list_empty (&ep->queue)) {
  189. struct dummy_request *req;
  190. req = list_entry (ep->queue.next, struct dummy_request, queue);
  191. list_del_init (&req->queue);
  192. req->req.status = -ESHUTDOWN;
  193. spin_unlock (&dum->lock);
  194. req->req.complete (&ep->ep, &req->req);
  195. spin_lock (&dum->lock);
  196. }
  197. }
  198. /* caller must hold lock */
  199. static void
  200. stop_activity (struct dummy *dum)
  201. {
  202. struct dummy_ep *ep;
  203. /* prevent any more requests */
  204. dum->address = 0;
  205. /* The timer is left running so that outstanding URBs can fail */
  206. /* nuke any pending requests first, so driver i/o is quiesced */
  207. list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
  208. nuke (dum, ep);
  209. /* driver now does any non-usb quiescing necessary */
  210. }
  211. /* caller must hold lock */
  212. static void
  213. set_link_state (struct dummy *dum)
  214. {
  215. dum->active = 0;
  216. if ((dum->port_status & USB_PORT_STAT_POWER) == 0)
  217. dum->port_status = 0;
  218. /* UDC suspend must cause a disconnect */
  219. else if (!dum->pullup || dum->udc_suspended) {
  220. dum->port_status &= ~(USB_PORT_STAT_CONNECTION |
  221. USB_PORT_STAT_ENABLE |
  222. USB_PORT_STAT_LOW_SPEED |
  223. USB_PORT_STAT_HIGH_SPEED |
  224. USB_PORT_STAT_SUSPEND);
  225. if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0)
  226. dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
  227. } else {
  228. dum->port_status |= USB_PORT_STAT_CONNECTION;
  229. if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0)
  230. dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
  231. if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0)
  232. dum->port_status &= ~USB_PORT_STAT_SUSPEND;
  233. else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
  234. dum->rh_state != DUMMY_RH_SUSPENDED)
  235. dum->active = 1;
  236. }
  237. if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active)
  238. dum->resuming = 0;
  239. if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
  240. (dum->port_status & USB_PORT_STAT_RESET) != 0) {
  241. if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
  242. (dum->old_status & USB_PORT_STAT_RESET) == 0 &&
  243. dum->driver) {
  244. stop_activity (dum);
  245. spin_unlock (&dum->lock);
  246. dum->driver->disconnect (&dum->gadget);
  247. spin_lock (&dum->lock);
  248. }
  249. } else if (dum->active != dum->old_active) {
  250. if (dum->old_active && dum->driver->suspend) {
  251. spin_unlock (&dum->lock);
  252. dum->driver->suspend (&dum->gadget);
  253. spin_lock (&dum->lock);
  254. } else if (!dum->old_active && dum->driver->resume) {
  255. spin_unlock (&dum->lock);
  256. dum->driver->resume (&dum->gadget);
  257. spin_lock (&dum->lock);
  258. }
  259. }
  260. dum->old_status = dum->port_status;
  261. dum->old_active = dum->active;
  262. }
  263. /*-------------------------------------------------------------------------*/
  264. /* SLAVE/GADGET SIDE DRIVER
  265. *
  266. * This only tracks gadget state. All the work is done when the host
  267. * side tries some (emulated) i/o operation. Real device controller
  268. * drivers would do real i/o using dma, fifos, irqs, timers, etc.
  269. */
  270. #define is_enabled(dum) \
  271. (dum->port_status & USB_PORT_STAT_ENABLE)
  272. static int
  273. dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  274. {
  275. struct dummy *dum;
  276. struct dummy_ep *ep;
  277. unsigned max;
  278. int retval;
  279. ep = usb_ep_to_dummy_ep (_ep);
  280. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  281. || desc->bDescriptorType != USB_DT_ENDPOINT)
  282. return -EINVAL;
  283. dum = ep_to_dummy (ep);
  284. if (!dum->driver || !is_enabled (dum))
  285. return -ESHUTDOWN;
  286. max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
  287. /* drivers must not request bad settings, since lower levels
  288. * (hardware or its drivers) may not check. some endpoints
  289. * can't do iso, many have maxpacket limitations, etc.
  290. *
  291. * since this "hardware" driver is here to help debugging, we
  292. * have some extra sanity checks. (there could be more though,
  293. * especially for "ep9out" style fixed function ones.)
  294. */
  295. retval = -EINVAL;
  296. switch (desc->bmAttributes & 0x03) {
  297. case USB_ENDPOINT_XFER_BULK:
  298. if (strstr (ep->ep.name, "-iso")
  299. || strstr (ep->ep.name, "-int")) {
  300. goto done;
  301. }
  302. switch (dum->gadget.speed) {
  303. case USB_SPEED_HIGH:
  304. if (max == 512)
  305. break;
  306. goto done;
  307. case USB_SPEED_FULL:
  308. if (max == 8 || max == 16 || max == 32 || max == 64)
  309. /* we'll fake any legal size */
  310. break;
  311. /* save a return statement */
  312. default:
  313. goto done;
  314. }
  315. break;
  316. case USB_ENDPOINT_XFER_INT:
  317. if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
  318. goto done;
  319. /* real hardware might not handle all packet sizes */
  320. switch (dum->gadget.speed) {
  321. case USB_SPEED_HIGH:
  322. if (max <= 1024)
  323. break;
  324. /* save a return statement */
  325. case USB_SPEED_FULL:
  326. if (max <= 64)
  327. break;
  328. /* save a return statement */
  329. default:
  330. if (max <= 8)
  331. break;
  332. goto done;
  333. }
  334. break;
  335. case USB_ENDPOINT_XFER_ISOC:
  336. if (strstr (ep->ep.name, "-bulk")
  337. || strstr (ep->ep.name, "-int"))
  338. goto done;
  339. /* real hardware might not handle all packet sizes */
  340. switch (dum->gadget.speed) {
  341. case USB_SPEED_HIGH:
  342. if (max <= 1024)
  343. break;
  344. /* save a return statement */
  345. case USB_SPEED_FULL:
  346. if (max <= 1023)
  347. break;
  348. /* save a return statement */
  349. default:
  350. goto done;
  351. }
  352. break;
  353. default:
  354. /* few chips support control except on ep0 */
  355. goto done;
  356. }
  357. _ep->maxpacket = max;
  358. ep->desc = desc;
  359. dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
  360. _ep->name,
  361. desc->bEndpointAddress & 0x0f,
  362. (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  363. ({ char *val;
  364. switch (desc->bmAttributes & 0x03) {
  365. case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
  366. case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
  367. case USB_ENDPOINT_XFER_INT: val = "intr"; break;
  368. default: val = "ctrl"; break;
  369. }; val; }),
  370. max);
  371. /* at this point real hardware should be NAKing transfers
  372. * to that endpoint, until a buffer is queued to it.
  373. */
  374. retval = 0;
  375. done:
  376. return retval;
  377. }
  378. static int dummy_disable (struct usb_ep *_ep)
  379. {
  380. struct dummy_ep *ep;
  381. struct dummy *dum;
  382. unsigned long flags;
  383. int retval;
  384. ep = usb_ep_to_dummy_ep (_ep);
  385. if (!_ep || !ep->desc || _ep->name == ep0name)
  386. return -EINVAL;
  387. dum = ep_to_dummy (ep);
  388. spin_lock_irqsave (&dum->lock, flags);
  389. ep->desc = NULL;
  390. retval = 0;
  391. nuke (dum, ep);
  392. spin_unlock_irqrestore (&dum->lock, flags);
  393. dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
  394. return retval;
  395. }
  396. static struct usb_request *
  397. dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
  398. {
  399. struct dummy_ep *ep;
  400. struct dummy_request *req;
  401. if (!_ep)
  402. return NULL;
  403. ep = usb_ep_to_dummy_ep (_ep);
  404. req = kzalloc(sizeof(*req), mem_flags);
  405. if (!req)
  406. return NULL;
  407. INIT_LIST_HEAD (&req->queue);
  408. return &req->req;
  409. }
  410. static void
  411. dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
  412. {
  413. struct dummy_ep *ep;
  414. struct dummy_request *req;
  415. ep = usb_ep_to_dummy_ep (_ep);
  416. if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
  417. return;
  418. req = usb_request_to_dummy_request (_req);
  419. WARN_ON (!list_empty (&req->queue));
  420. kfree (req);
  421. }
  422. static void
  423. fifo_complete (struct usb_ep *ep, struct usb_request *req)
  424. {
  425. }
  426. static int
  427. dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
  428. gfp_t mem_flags)
  429. {
  430. struct dummy_ep *ep;
  431. struct dummy_request *req;
  432. struct dummy *dum;
  433. unsigned long flags;
  434. req = usb_request_to_dummy_request (_req);
  435. if (!_req || !list_empty (&req->queue) || !_req->complete)
  436. return -EINVAL;
  437. ep = usb_ep_to_dummy_ep (_ep);
  438. if (!_ep || (!ep->desc && _ep->name != ep0name))
  439. return -EINVAL;
  440. dum = ep_to_dummy (ep);
  441. if (!dum->driver || !is_enabled (dum))
  442. return -ESHUTDOWN;
  443. #if 0
  444. dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
  445. ep, _req, _ep->name, _req->length, _req->buf);
  446. #endif
  447. _req->status = -EINPROGRESS;
  448. _req->actual = 0;
  449. spin_lock_irqsave (&dum->lock, flags);
  450. /* implement an emulated single-request FIFO */
  451. if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  452. list_empty (&dum->fifo_req.queue) &&
  453. list_empty (&ep->queue) &&
  454. _req->length <= FIFO_SIZE) {
  455. req = &dum->fifo_req;
  456. req->req = *_req;
  457. req->req.buf = dum->fifo_buf;
  458. memcpy (dum->fifo_buf, _req->buf, _req->length);
  459. req->req.context = dum;
  460. req->req.complete = fifo_complete;
  461. list_add_tail(&req->queue, &ep->queue);
  462. spin_unlock (&dum->lock);
  463. _req->actual = _req->length;
  464. _req->status = 0;
  465. _req->complete (_ep, _req);
  466. spin_lock (&dum->lock);
  467. } else
  468. list_add_tail(&req->queue, &ep->queue);
  469. spin_unlock_irqrestore (&dum->lock, flags);
  470. /* real hardware would likely enable transfers here, in case
  471. * it'd been left NAKing.
  472. */
  473. return 0;
  474. }
  475. static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
  476. {
  477. struct dummy_ep *ep;
  478. struct dummy *dum;
  479. int retval = -EINVAL;
  480. unsigned long flags;
  481. struct dummy_request *req = NULL;
  482. if (!_ep || !_req)
  483. return retval;
  484. ep = usb_ep_to_dummy_ep (_ep);
  485. dum = ep_to_dummy (ep);
  486. if (!dum->driver)
  487. return -ESHUTDOWN;
  488. local_irq_save (flags);
  489. spin_lock (&dum->lock);
  490. list_for_each_entry (req, &ep->queue, queue) {
  491. if (&req->req == _req) {
  492. list_del_init (&req->queue);
  493. _req->status = -ECONNRESET;
  494. retval = 0;
  495. break;
  496. }
  497. }
  498. spin_unlock (&dum->lock);
  499. if (retval == 0) {
  500. dev_dbg (udc_dev(dum),
  501. "dequeued req %p from %s, len %d buf %p\n",
  502. req, _ep->name, _req->length, _req->buf);
  503. _req->complete (_ep, _req);
  504. }
  505. local_irq_restore (flags);
  506. return retval;
  507. }
  508. static int
  509. dummy_set_halt (struct usb_ep *_ep, int value)
  510. {
  511. struct dummy_ep *ep;
  512. struct dummy *dum;
  513. if (!_ep)
  514. return -EINVAL;
  515. ep = usb_ep_to_dummy_ep (_ep);
  516. dum = ep_to_dummy (ep);
  517. if (!dum->driver)
  518. return -ESHUTDOWN;
  519. if (!value)
  520. ep->halted = 0;
  521. else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  522. !list_empty (&ep->queue))
  523. return -EAGAIN;
  524. else
  525. ep->halted = 1;
  526. /* FIXME clear emulated data toggle too */
  527. return 0;
  528. }
  529. static const struct usb_ep_ops dummy_ep_ops = {
  530. .enable = dummy_enable,
  531. .disable = dummy_disable,
  532. .alloc_request = dummy_alloc_request,
  533. .free_request = dummy_free_request,
  534. .queue = dummy_queue,
  535. .dequeue = dummy_dequeue,
  536. .set_halt = dummy_set_halt,
  537. };
  538. /*-------------------------------------------------------------------------*/
  539. /* there are both host and device side versions of this call ... */
  540. static int dummy_g_get_frame (struct usb_gadget *_gadget)
  541. {
  542. struct timeval tv;
  543. do_gettimeofday (&tv);
  544. return tv.tv_usec / 1000;
  545. }
  546. static int dummy_wakeup (struct usb_gadget *_gadget)
  547. {
  548. struct dummy *dum;
  549. dum = gadget_to_dummy (_gadget);
  550. if (!(dum->devstatus & ( (1 << USB_DEVICE_B_HNP_ENABLE)
  551. | (1 << USB_DEVICE_REMOTE_WAKEUP))))
  552. return -EINVAL;
  553. if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0)
  554. return -ENOLINK;
  555. if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
  556. dum->rh_state != DUMMY_RH_SUSPENDED)
  557. return -EIO;
  558. /* FIXME: What if the root hub is suspended but the port isn't? */
  559. /* hub notices our request, issues downstream resume, etc */
  560. dum->resuming = 1;
  561. dum->re_timeout = jiffies + msecs_to_jiffies(20);
  562. mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
  563. return 0;
  564. }
  565. static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
  566. {
  567. struct dummy *dum;
  568. dum = gadget_to_dummy (_gadget);
  569. if (value)
  570. dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
  571. else
  572. dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  573. return 0;
  574. }
  575. static int dummy_pullup (struct usb_gadget *_gadget, int value)
  576. {
  577. struct dummy *dum;
  578. unsigned long flags;
  579. dum = gadget_to_dummy (_gadget);
  580. spin_lock_irqsave (&dum->lock, flags);
  581. dum->pullup = (value != 0);
  582. set_link_state (dum);
  583. spin_unlock_irqrestore (&dum->lock, flags);
  584. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  585. return 0;
  586. }
  587. static const struct usb_gadget_ops dummy_ops = {
  588. .get_frame = dummy_g_get_frame,
  589. .wakeup = dummy_wakeup,
  590. .set_selfpowered = dummy_set_selfpowered,
  591. .pullup = dummy_pullup,
  592. };
  593. /*-------------------------------------------------------------------------*/
  594. /* "function" sysfs attribute */
  595. static ssize_t
  596. show_function (struct device *dev, struct device_attribute *attr, char *buf)
  597. {
  598. struct dummy *dum = gadget_dev_to_dummy (dev);
  599. if (!dum->driver || !dum->driver->function)
  600. return 0;
  601. return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
  602. }
  603. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  604. /*-------------------------------------------------------------------------*/
  605. /*
  606. * Driver registration/unregistration.
  607. *
  608. * This is basically hardware-specific; there's usually only one real USB
  609. * device (not host) controller since that's how USB devices are intended
  610. * to work. So most implementations of these api calls will rely on the
  611. * fact that only one driver will ever bind to the hardware. But curious
  612. * hardware can be built with discrete components, so the gadget API doesn't
  613. * require that assumption.
  614. *
  615. * For this emulator, it might be convenient to create a usb slave device
  616. * for each driver that registers: just add to a big root hub.
  617. */
  618. int
  619. usb_gadget_register_driver (struct usb_gadget_driver *driver)
  620. {
  621. struct dummy *dum = the_controller;
  622. int retval, i;
  623. if (!dum)
  624. return -EINVAL;
  625. if (dum->driver)
  626. return -EBUSY;
  627. if (!driver->bind || !driver->setup
  628. || driver->speed == USB_SPEED_UNKNOWN)
  629. return -EINVAL;
  630. /*
  631. * SLAVE side init ... the layer above hardware, which
  632. * can't enumerate without help from the driver we're binding.
  633. */
  634. dum->devstatus = 0;
  635. INIT_LIST_HEAD (&dum->gadget.ep_list);
  636. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  637. struct dummy_ep *ep = &dum->ep [i];
  638. if (!ep_name [i])
  639. break;
  640. ep->ep.name = ep_name [i];
  641. ep->ep.ops = &dummy_ep_ops;
  642. list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
  643. ep->halted = ep->already_seen = ep->setup_stage = 0;
  644. ep->ep.maxpacket = ~0;
  645. ep->last_io = jiffies;
  646. ep->gadget = &dum->gadget;
  647. ep->desc = NULL;
  648. INIT_LIST_HEAD (&ep->queue);
  649. }
  650. dum->gadget.ep0 = &dum->ep [0].ep;
  651. dum->ep [0].ep.maxpacket = 64;
  652. list_del_init (&dum->ep [0].ep.ep_list);
  653. INIT_LIST_HEAD(&dum->fifo_req.queue);
  654. driver->driver.bus = NULL;
  655. dum->driver = driver;
  656. dum->gadget.dev.driver = &driver->driver;
  657. dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
  658. driver->driver.name);
  659. retval = driver->bind(&dum->gadget);
  660. if (retval) {
  661. dum->driver = NULL;
  662. dum->gadget.dev.driver = NULL;
  663. return retval;
  664. }
  665. /* khubd will enumerate this in a while */
  666. spin_lock_irq (&dum->lock);
  667. dum->pullup = 1;
  668. set_link_state (dum);
  669. spin_unlock_irq (&dum->lock);
  670. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  671. return 0;
  672. }
  673. EXPORT_SYMBOL (usb_gadget_register_driver);
  674. int
  675. usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  676. {
  677. struct dummy *dum = the_controller;
  678. unsigned long flags;
  679. if (!dum)
  680. return -ENODEV;
  681. if (!driver || driver != dum->driver || !driver->unbind)
  682. return -EINVAL;
  683. dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
  684. driver->driver.name);
  685. spin_lock_irqsave (&dum->lock, flags);
  686. dum->pullup = 0;
  687. set_link_state (dum);
  688. spin_unlock_irqrestore (&dum->lock, flags);
  689. driver->unbind (&dum->gadget);
  690. dum->gadget.dev.driver = NULL;
  691. dum->driver = NULL;
  692. spin_lock_irqsave (&dum->lock, flags);
  693. dum->pullup = 0;
  694. set_link_state (dum);
  695. spin_unlock_irqrestore (&dum->lock, flags);
  696. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  697. return 0;
  698. }
  699. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  700. #undef is_enabled
  701. /* just declare this in any driver that really need it */
  702. extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
  703. int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
  704. {
  705. return -ENOSYS;
  706. }
  707. EXPORT_SYMBOL (net2280_set_fifo_mode);
  708. /* The gadget structure is stored inside the hcd structure and will be
  709. * released along with it. */
  710. static void
  711. dummy_gadget_release (struct device *dev)
  712. {
  713. struct dummy *dum = gadget_dev_to_dummy (dev);
  714. usb_put_hcd (dummy_to_hcd (dum));
  715. }
  716. static int dummy_udc_probe (struct platform_device *pdev)
  717. {
  718. struct dummy *dum = the_controller;
  719. int rc;
  720. dum->gadget.name = gadget_name;
  721. dum->gadget.ops = &dummy_ops;
  722. dum->gadget.is_dualspeed = 1;
  723. /* maybe claim OTG support, though we won't complete HNP */
  724. dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
  725. dev_set_name(&dum->gadget.dev, "gadget");
  726. dum->gadget.dev.parent = &pdev->dev;
  727. dum->gadget.dev.release = dummy_gadget_release;
  728. rc = device_register (&dum->gadget.dev);
  729. if (rc < 0)
  730. return rc;
  731. usb_get_hcd (dummy_to_hcd (dum));
  732. platform_set_drvdata (pdev, dum);
  733. rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
  734. if (rc < 0)
  735. device_unregister (&dum->gadget.dev);
  736. return rc;
  737. }
  738. static int dummy_udc_remove (struct platform_device *pdev)
  739. {
  740. struct dummy *dum = platform_get_drvdata (pdev);
  741. platform_set_drvdata (pdev, NULL);
  742. device_remove_file (&dum->gadget.dev, &dev_attr_function);
  743. device_unregister (&dum->gadget.dev);
  744. return 0;
  745. }
  746. static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
  747. {
  748. struct dummy *dum = platform_get_drvdata(pdev);
  749. dev_dbg (&pdev->dev, "%s\n", __func__);
  750. spin_lock_irq (&dum->lock);
  751. dum->udc_suspended = 1;
  752. set_link_state (dum);
  753. spin_unlock_irq (&dum->lock);
  754. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  755. return 0;
  756. }
  757. static int dummy_udc_resume (struct platform_device *pdev)
  758. {
  759. struct dummy *dum = platform_get_drvdata(pdev);
  760. dev_dbg (&pdev->dev, "%s\n", __func__);
  761. spin_lock_irq (&dum->lock);
  762. dum->udc_suspended = 0;
  763. set_link_state (dum);
  764. spin_unlock_irq (&dum->lock);
  765. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  766. return 0;
  767. }
  768. static struct platform_driver dummy_udc_driver = {
  769. .probe = dummy_udc_probe,
  770. .remove = dummy_udc_remove,
  771. .suspend = dummy_udc_suspend,
  772. .resume = dummy_udc_resume,
  773. .driver = {
  774. .name = (char *) gadget_name,
  775. .owner = THIS_MODULE,
  776. },
  777. };
  778. /*-------------------------------------------------------------------------*/
  779. /* MASTER/HOST SIDE DRIVER
  780. *
  781. * this uses the hcd framework to hook up to host side drivers.
  782. * its root hub will only have one device, otherwise it acts like
  783. * a normal host controller.
  784. *
  785. * when urbs are queued, they're just stuck on a list that we
  786. * scan in a timer callback. that callback connects writes from
  787. * the host with reads from the device, and so on, based on the
  788. * usb 2.0 rules.
  789. */
  790. static int dummy_urb_enqueue (
  791. struct usb_hcd *hcd,
  792. struct urb *urb,
  793. gfp_t mem_flags
  794. ) {
  795. struct dummy *dum;
  796. struct urbp *urbp;
  797. unsigned long flags;
  798. int rc;
  799. if (!urb->transfer_buffer && urb->transfer_buffer_length)
  800. return -EINVAL;
  801. urbp = kmalloc (sizeof *urbp, mem_flags);
  802. if (!urbp)
  803. return -ENOMEM;
  804. urbp->urb = urb;
  805. dum = hcd_to_dummy (hcd);
  806. spin_lock_irqsave (&dum->lock, flags);
  807. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  808. if (rc) {
  809. kfree(urbp);
  810. goto done;
  811. }
  812. if (!dum->udev) {
  813. dum->udev = urb->dev;
  814. usb_get_dev (dum->udev);
  815. } else if (unlikely (dum->udev != urb->dev))
  816. dev_err (dummy_dev(dum), "usb_device address has changed!\n");
  817. list_add_tail (&urbp->urbp_list, &dum->urbp_list);
  818. urb->hcpriv = urbp;
  819. if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
  820. urb->error_count = 1; /* mark as a new urb */
  821. /* kick the scheduler, it'll do the rest */
  822. if (!timer_pending (&dum->timer))
  823. mod_timer (&dum->timer, jiffies + 1);
  824. done:
  825. spin_unlock_irqrestore(&dum->lock, flags);
  826. return rc;
  827. }
  828. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  829. {
  830. struct dummy *dum;
  831. unsigned long flags;
  832. int rc;
  833. /* giveback happens automatically in timer callback,
  834. * so make sure the callback happens */
  835. dum = hcd_to_dummy (hcd);
  836. spin_lock_irqsave (&dum->lock, flags);
  837. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  838. if (!rc && dum->rh_state != DUMMY_RH_RUNNING &&
  839. !list_empty(&dum->urbp_list))
  840. mod_timer (&dum->timer, jiffies);
  841. spin_unlock_irqrestore (&dum->lock, flags);
  842. return rc;
  843. }
  844. /* transfer up to a frame's worth; caller must own lock */
  845. static int
  846. transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
  847. int *status)
  848. {
  849. struct dummy_request *req;
  850. top:
  851. /* if there's no request queued, the device is NAKing; return */
  852. list_for_each_entry (req, &ep->queue, queue) {
  853. unsigned host_len, dev_len, len;
  854. int is_short, to_host;
  855. int rescan = 0;
  856. /* 1..N packets of ep->ep.maxpacket each ... the last one
  857. * may be short (including zero length).
  858. *
  859. * writer can send a zlp explicitly (length 0) or implicitly
  860. * (length mod maxpacket zero, and 'zero' flag); they always
  861. * terminate reads.
  862. */
  863. host_len = urb->transfer_buffer_length - urb->actual_length;
  864. dev_len = req->req.length - req->req.actual;
  865. len = min (host_len, dev_len);
  866. /* FIXME update emulated data toggle too */
  867. to_host = usb_pipein (urb->pipe);
  868. if (unlikely (len == 0))
  869. is_short = 1;
  870. else {
  871. char *ubuf, *rbuf;
  872. /* not enough bandwidth left? */
  873. if (limit < ep->ep.maxpacket && limit < len)
  874. break;
  875. len = min (len, (unsigned) limit);
  876. if (len == 0)
  877. break;
  878. /* use an extra pass for the final short packet */
  879. if (len > ep->ep.maxpacket) {
  880. rescan = 1;
  881. len -= (len % ep->ep.maxpacket);
  882. }
  883. is_short = (len % ep->ep.maxpacket) != 0;
  884. /* else transfer packet(s) */
  885. ubuf = urb->transfer_buffer + urb->actual_length;
  886. rbuf = req->req.buf + req->req.actual;
  887. if (to_host)
  888. memcpy (ubuf, rbuf, len);
  889. else
  890. memcpy (rbuf, ubuf, len);
  891. ep->last_io = jiffies;
  892. limit -= len;
  893. urb->actual_length += len;
  894. req->req.actual += len;
  895. }
  896. /* short packets terminate, maybe with overflow/underflow.
  897. * it's only really an error to write too much.
  898. *
  899. * partially filling a buffer optionally blocks queue advances
  900. * (so completion handlers can clean up the queue) but we don't
  901. * need to emulate such data-in-flight.
  902. */
  903. if (is_short) {
  904. if (host_len == dev_len) {
  905. req->req.status = 0;
  906. *status = 0;
  907. } else if (to_host) {
  908. req->req.status = 0;
  909. if (dev_len > host_len)
  910. *status = -EOVERFLOW;
  911. else
  912. *status = 0;
  913. } else if (!to_host) {
  914. *status = 0;
  915. if (host_len > dev_len)
  916. req->req.status = -EOVERFLOW;
  917. else
  918. req->req.status = 0;
  919. }
  920. /* many requests terminate without a short packet */
  921. } else {
  922. if (req->req.length == req->req.actual
  923. && !req->req.zero)
  924. req->req.status = 0;
  925. if (urb->transfer_buffer_length == urb->actual_length
  926. && !(urb->transfer_flags
  927. & URB_ZERO_PACKET))
  928. *status = 0;
  929. }
  930. /* device side completion --> continuable */
  931. if (req->req.status != -EINPROGRESS) {
  932. list_del_init (&req->queue);
  933. spin_unlock (&dum->lock);
  934. req->req.complete (&ep->ep, &req->req);
  935. spin_lock (&dum->lock);
  936. /* requests might have been unlinked... */
  937. rescan = 1;
  938. }
  939. /* host side completion --> terminate */
  940. if (*status != -EINPROGRESS)
  941. break;
  942. /* rescan to continue with any other queued i/o */
  943. if (rescan)
  944. goto top;
  945. }
  946. return limit;
  947. }
  948. static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
  949. {
  950. int limit = ep->ep.maxpacket;
  951. if (dum->gadget.speed == USB_SPEED_HIGH) {
  952. int tmp;
  953. /* high bandwidth mode */
  954. tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
  955. tmp = (tmp >> 11) & 0x03;
  956. tmp *= 8 /* applies to entire frame */;
  957. limit += limit * tmp;
  958. }
  959. return limit;
  960. }
  961. #define is_active(dum) ((dum->port_status & \
  962. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  963. USB_PORT_STAT_SUSPEND)) \
  964. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  965. static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
  966. {
  967. int i;
  968. if (!is_active (dum))
  969. return NULL;
  970. if ((address & ~USB_DIR_IN) == 0)
  971. return &dum->ep [0];
  972. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  973. struct dummy_ep *ep = &dum->ep [i];
  974. if (!ep->desc)
  975. continue;
  976. if (ep->desc->bEndpointAddress == address)
  977. return ep;
  978. }
  979. return NULL;
  980. }
  981. #undef is_active
  982. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  983. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  984. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  985. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  986. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  987. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  988. /* drive both sides of the transfers; looks like irq handlers to
  989. * both drivers except the callbacks aren't in_irq().
  990. */
  991. static void dummy_timer (unsigned long _dum)
  992. {
  993. struct dummy *dum = (struct dummy *) _dum;
  994. struct urbp *urbp, *tmp;
  995. unsigned long flags;
  996. int limit, total;
  997. int i;
  998. /* simplistic model for one frame's bandwidth */
  999. switch (dum->gadget.speed) {
  1000. case USB_SPEED_LOW:
  1001. total = 8/*bytes*/ * 12/*packets*/;
  1002. break;
  1003. case USB_SPEED_FULL:
  1004. total = 64/*bytes*/ * 19/*packets*/;
  1005. break;
  1006. case USB_SPEED_HIGH:
  1007. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1008. break;
  1009. default:
  1010. dev_err (dummy_dev(dum), "bogus device speed\n");
  1011. return;
  1012. }
  1013. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1014. /* look at each urb queued by the host side driver */
  1015. spin_lock_irqsave (&dum->lock, flags);
  1016. if (!dum->udev) {
  1017. dev_err (dummy_dev(dum),
  1018. "timer fired with no URBs pending?\n");
  1019. spin_unlock_irqrestore (&dum->lock, flags);
  1020. return;
  1021. }
  1022. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1023. if (!ep_name [i])
  1024. break;
  1025. dum->ep [i].already_seen = 0;
  1026. }
  1027. restart:
  1028. list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
  1029. struct urb *urb;
  1030. struct dummy_request *req;
  1031. u8 address;
  1032. struct dummy_ep *ep = NULL;
  1033. int type;
  1034. int status = -EINPROGRESS;
  1035. urb = urbp->urb;
  1036. if (urb->unlinked)
  1037. goto return_urb;
  1038. else if (dum->rh_state != DUMMY_RH_RUNNING)
  1039. continue;
  1040. type = usb_pipetype (urb->pipe);
  1041. /* used up this frame's non-periodic bandwidth?
  1042. * FIXME there's infinite bandwidth for control and
  1043. * periodic transfers ... unrealistic.
  1044. */
  1045. if (total <= 0 && type == PIPE_BULK)
  1046. continue;
  1047. /* find the gadget's ep for this request (if configured) */
  1048. address = usb_pipeendpoint (urb->pipe);
  1049. if (usb_pipein (urb->pipe))
  1050. address |= USB_DIR_IN;
  1051. ep = find_endpoint(dum, address);
  1052. if (!ep) {
  1053. /* set_configuration() disagreement */
  1054. dev_dbg (dummy_dev(dum),
  1055. "no ep configured for urb %p\n",
  1056. urb);
  1057. status = -EPROTO;
  1058. goto return_urb;
  1059. }
  1060. if (ep->already_seen)
  1061. continue;
  1062. ep->already_seen = 1;
  1063. if (ep == &dum->ep [0] && urb->error_count) {
  1064. ep->setup_stage = 1; /* a new urb */
  1065. urb->error_count = 0;
  1066. }
  1067. if (ep->halted && !ep->setup_stage) {
  1068. /* NOTE: must not be iso! */
  1069. dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
  1070. ep->ep.name, urb);
  1071. status = -EPIPE;
  1072. goto return_urb;
  1073. }
  1074. /* FIXME make sure both ends agree on maxpacket */
  1075. /* handle control requests */
  1076. if (ep == &dum->ep [0] && ep->setup_stage) {
  1077. struct usb_ctrlrequest setup;
  1078. int value = 1;
  1079. struct dummy_ep *ep2;
  1080. unsigned w_index;
  1081. unsigned w_value;
  1082. setup = *(struct usb_ctrlrequest*) urb->setup_packet;
  1083. w_index = le16_to_cpu(setup.wIndex);
  1084. w_value = le16_to_cpu(setup.wValue);
  1085. if (le16_to_cpu(setup.wLength) !=
  1086. urb->transfer_buffer_length) {
  1087. status = -EOVERFLOW;
  1088. goto return_urb;
  1089. }
  1090. /* paranoia, in case of stale queued data */
  1091. list_for_each_entry (req, &ep->queue, queue) {
  1092. list_del_init (&req->queue);
  1093. req->req.status = -EOVERFLOW;
  1094. dev_dbg (udc_dev(dum), "stale req = %p\n",
  1095. req);
  1096. spin_unlock (&dum->lock);
  1097. req->req.complete (&ep->ep, &req->req);
  1098. spin_lock (&dum->lock);
  1099. ep->already_seen = 0;
  1100. goto restart;
  1101. }
  1102. /* gadget driver never sees set_address or operations
  1103. * on standard feature flags. some hardware doesn't
  1104. * even expose them.
  1105. */
  1106. ep->last_io = jiffies;
  1107. ep->setup_stage = 0;
  1108. ep->halted = 0;
  1109. switch (setup.bRequest) {
  1110. case USB_REQ_SET_ADDRESS:
  1111. if (setup.bRequestType != Dev_Request)
  1112. break;
  1113. dum->address = w_value;
  1114. status = 0;
  1115. dev_dbg (udc_dev(dum), "set_address = %d\n",
  1116. w_value);
  1117. value = 0;
  1118. break;
  1119. case USB_REQ_SET_FEATURE:
  1120. if (setup.bRequestType == Dev_Request) {
  1121. value = 0;
  1122. switch (w_value) {
  1123. case USB_DEVICE_REMOTE_WAKEUP:
  1124. break;
  1125. case USB_DEVICE_B_HNP_ENABLE:
  1126. dum->gadget.b_hnp_enable = 1;
  1127. break;
  1128. case USB_DEVICE_A_HNP_SUPPORT:
  1129. dum->gadget.a_hnp_support = 1;
  1130. break;
  1131. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1132. dum->gadget.a_alt_hnp_support
  1133. = 1;
  1134. break;
  1135. default:
  1136. value = -EOPNOTSUPP;
  1137. }
  1138. if (value == 0) {
  1139. dum->devstatus |=
  1140. (1 << w_value);
  1141. status = 0;
  1142. }
  1143. } else if (setup.bRequestType == Ep_Request) {
  1144. // endpoint halt
  1145. ep2 = find_endpoint (dum, w_index);
  1146. if (!ep2) {
  1147. value = -EOPNOTSUPP;
  1148. break;
  1149. }
  1150. ep2->halted = 1;
  1151. value = 0;
  1152. status = 0;
  1153. }
  1154. break;
  1155. case USB_REQ_CLEAR_FEATURE:
  1156. if (setup.bRequestType == Dev_Request) {
  1157. switch (w_value) {
  1158. case USB_DEVICE_REMOTE_WAKEUP:
  1159. dum->devstatus &= ~(1 <<
  1160. USB_DEVICE_REMOTE_WAKEUP);
  1161. value = 0;
  1162. status = 0;
  1163. break;
  1164. default:
  1165. value = -EOPNOTSUPP;
  1166. break;
  1167. }
  1168. } else if (setup.bRequestType == Ep_Request) {
  1169. // endpoint halt
  1170. ep2 = find_endpoint (dum, w_index);
  1171. if (!ep2) {
  1172. value = -EOPNOTSUPP;
  1173. break;
  1174. }
  1175. ep2->halted = 0;
  1176. value = 0;
  1177. status = 0;
  1178. }
  1179. break;
  1180. case USB_REQ_GET_STATUS:
  1181. if (setup.bRequestType == Dev_InRequest
  1182. || setup.bRequestType
  1183. == Intf_InRequest
  1184. || setup.bRequestType
  1185. == Ep_InRequest
  1186. ) {
  1187. char *buf;
  1188. // device: remote wakeup, selfpowered
  1189. // interface: nothing
  1190. // endpoint: halt
  1191. buf = (char *)urb->transfer_buffer;
  1192. if (urb->transfer_buffer_length > 0) {
  1193. if (setup.bRequestType ==
  1194. Ep_InRequest) {
  1195. ep2 = find_endpoint (dum, w_index);
  1196. if (!ep2) {
  1197. value = -EOPNOTSUPP;
  1198. break;
  1199. }
  1200. buf [0] = ep2->halted;
  1201. } else if (setup.bRequestType ==
  1202. Dev_InRequest) {
  1203. buf [0] = (u8)
  1204. dum->devstatus;
  1205. } else
  1206. buf [0] = 0;
  1207. }
  1208. if (urb->transfer_buffer_length > 1)
  1209. buf [1] = 0;
  1210. urb->actual_length = min (2,
  1211. urb->transfer_buffer_length);
  1212. value = 0;
  1213. status = 0;
  1214. }
  1215. break;
  1216. }
  1217. /* gadget driver handles all other requests. block
  1218. * until setup() returns; no reentrancy issues etc.
  1219. */
  1220. if (value > 0) {
  1221. spin_unlock (&dum->lock);
  1222. value = dum->driver->setup (&dum->gadget,
  1223. &setup);
  1224. spin_lock (&dum->lock);
  1225. if (value >= 0) {
  1226. /* no delays (max 64KB data stage) */
  1227. limit = 64*1024;
  1228. goto treat_control_like_bulk;
  1229. }
  1230. /* error, see below */
  1231. }
  1232. if (value < 0) {
  1233. if (value != -EOPNOTSUPP)
  1234. dev_dbg (udc_dev(dum),
  1235. "setup --> %d\n",
  1236. value);
  1237. status = -EPIPE;
  1238. urb->actual_length = 0;
  1239. }
  1240. goto return_urb;
  1241. }
  1242. /* non-control requests */
  1243. limit = total;
  1244. switch (usb_pipetype (urb->pipe)) {
  1245. case PIPE_ISOCHRONOUS:
  1246. /* FIXME is it urb->interval since the last xfer?
  1247. * use urb->iso_frame_desc[i].
  1248. * complete whether or not ep has requests queued.
  1249. * report random errors, to debug drivers.
  1250. */
  1251. limit = max (limit, periodic_bytes (dum, ep));
  1252. status = -ENOSYS;
  1253. break;
  1254. case PIPE_INTERRUPT:
  1255. /* FIXME is it urb->interval since the last xfer?
  1256. * this almost certainly polls too fast.
  1257. */
  1258. limit = max (limit, periodic_bytes (dum, ep));
  1259. /* FALLTHROUGH */
  1260. // case PIPE_BULK: case PIPE_CONTROL:
  1261. default:
  1262. treat_control_like_bulk:
  1263. ep->last_io = jiffies;
  1264. total = transfer(dum, urb, ep, limit, &status);
  1265. break;
  1266. }
  1267. /* incomplete transfer? */
  1268. if (status == -EINPROGRESS)
  1269. continue;
  1270. return_urb:
  1271. list_del (&urbp->urbp_list);
  1272. kfree (urbp);
  1273. if (ep)
  1274. ep->already_seen = ep->setup_stage = 0;
  1275. usb_hcd_unlink_urb_from_ep(dummy_to_hcd(dum), urb);
  1276. spin_unlock (&dum->lock);
  1277. usb_hcd_giveback_urb(dummy_to_hcd(dum), urb, status);
  1278. spin_lock (&dum->lock);
  1279. goto restart;
  1280. }
  1281. if (list_empty (&dum->urbp_list)) {
  1282. usb_put_dev (dum->udev);
  1283. dum->udev = NULL;
  1284. } else if (dum->rh_state == DUMMY_RH_RUNNING) {
  1285. /* want a 1 msec delay here */
  1286. mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
  1287. }
  1288. spin_unlock_irqrestore (&dum->lock, flags);
  1289. }
  1290. /*-------------------------------------------------------------------------*/
  1291. #define PORT_C_MASK \
  1292. ((USB_PORT_STAT_C_CONNECTION \
  1293. | USB_PORT_STAT_C_ENABLE \
  1294. | USB_PORT_STAT_C_SUSPEND \
  1295. | USB_PORT_STAT_C_OVERCURRENT \
  1296. | USB_PORT_STAT_C_RESET) << 16)
  1297. static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
  1298. {
  1299. struct dummy *dum;
  1300. unsigned long flags;
  1301. int retval = 0;
  1302. dum = hcd_to_dummy (hcd);
  1303. spin_lock_irqsave (&dum->lock, flags);
  1304. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
  1305. goto done;
  1306. if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
  1307. dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1308. dum->port_status &= ~USB_PORT_STAT_SUSPEND;
  1309. set_link_state (dum);
  1310. }
  1311. if ((dum->port_status & PORT_C_MASK) != 0) {
  1312. *buf = (1 << 1);
  1313. dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
  1314. dum->port_status);
  1315. retval = 1;
  1316. if (dum->rh_state == DUMMY_RH_SUSPENDED)
  1317. usb_hcd_resume_root_hub (hcd);
  1318. }
  1319. done:
  1320. spin_unlock_irqrestore (&dum->lock, flags);
  1321. return retval;
  1322. }
  1323. static inline void
  1324. hub_descriptor (struct usb_hub_descriptor *desc)
  1325. {
  1326. memset (desc, 0, sizeof *desc);
  1327. desc->bDescriptorType = 0x29;
  1328. desc->bDescLength = 9;
  1329. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1330. desc->bNbrPorts = 1;
  1331. desc->bitmap [0] = 0xff;
  1332. desc->bitmap [1] = 0xff;
  1333. }
  1334. static int dummy_hub_control (
  1335. struct usb_hcd *hcd,
  1336. u16 typeReq,
  1337. u16 wValue,
  1338. u16 wIndex,
  1339. char *buf,
  1340. u16 wLength
  1341. ) {
  1342. struct dummy *dum;
  1343. int retval = 0;
  1344. unsigned long flags;
  1345. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
  1346. return -ETIMEDOUT;
  1347. dum = hcd_to_dummy (hcd);
  1348. spin_lock_irqsave (&dum->lock, flags);
  1349. switch (typeReq) {
  1350. case ClearHubFeature:
  1351. break;
  1352. case ClearPortFeature:
  1353. switch (wValue) {
  1354. case USB_PORT_FEAT_SUSPEND:
  1355. if (dum->port_status & USB_PORT_STAT_SUSPEND) {
  1356. /* 20msec resume signaling */
  1357. dum->resuming = 1;
  1358. dum->re_timeout = jiffies +
  1359. msecs_to_jiffies(20);
  1360. }
  1361. break;
  1362. case USB_PORT_FEAT_POWER:
  1363. if (dum->port_status & USB_PORT_STAT_POWER)
  1364. dev_dbg (dummy_dev(dum), "power-off\n");
  1365. /* FALLS THROUGH */
  1366. default:
  1367. dum->port_status &= ~(1 << wValue);
  1368. set_link_state (dum);
  1369. }
  1370. break;
  1371. case GetHubDescriptor:
  1372. hub_descriptor ((struct usb_hub_descriptor *) buf);
  1373. break;
  1374. case GetHubStatus:
  1375. *(__le32 *) buf = __constant_cpu_to_le32 (0);
  1376. break;
  1377. case GetPortStatus:
  1378. if (wIndex != 1)
  1379. retval = -EPIPE;
  1380. /* whoever resets or resumes must GetPortStatus to
  1381. * complete it!!
  1382. */
  1383. if (dum->resuming &&
  1384. time_after_eq (jiffies, dum->re_timeout)) {
  1385. dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1386. dum->port_status &= ~USB_PORT_STAT_SUSPEND;
  1387. }
  1388. if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
  1389. time_after_eq (jiffies, dum->re_timeout)) {
  1390. dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
  1391. dum->port_status &= ~USB_PORT_STAT_RESET;
  1392. if (dum->pullup) {
  1393. dum->port_status |= USB_PORT_STAT_ENABLE;
  1394. /* give it the best speed we agree on */
  1395. dum->gadget.speed = dum->driver->speed;
  1396. dum->gadget.ep0->maxpacket = 64;
  1397. switch (dum->gadget.speed) {
  1398. case USB_SPEED_HIGH:
  1399. dum->port_status |=
  1400. USB_PORT_STAT_HIGH_SPEED;
  1401. break;
  1402. case USB_SPEED_LOW:
  1403. dum->gadget.ep0->maxpacket = 8;
  1404. dum->port_status |=
  1405. USB_PORT_STAT_LOW_SPEED;
  1406. break;
  1407. default:
  1408. dum->gadget.speed = USB_SPEED_FULL;
  1409. break;
  1410. }
  1411. }
  1412. }
  1413. set_link_state (dum);
  1414. ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
  1415. ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
  1416. break;
  1417. case SetHubFeature:
  1418. retval = -EPIPE;
  1419. break;
  1420. case SetPortFeature:
  1421. switch (wValue) {
  1422. case USB_PORT_FEAT_SUSPEND:
  1423. if (dum->active) {
  1424. dum->port_status |= USB_PORT_STAT_SUSPEND;
  1425. /* HNP would happen here; for now we
  1426. * assume b_bus_req is always true.
  1427. */
  1428. set_link_state (dum);
  1429. if (((1 << USB_DEVICE_B_HNP_ENABLE)
  1430. & dum->devstatus) != 0)
  1431. dev_dbg (dummy_dev(dum),
  1432. "no HNP yet!\n");
  1433. }
  1434. break;
  1435. case USB_PORT_FEAT_POWER:
  1436. dum->port_status |= USB_PORT_STAT_POWER;
  1437. set_link_state (dum);
  1438. break;
  1439. case USB_PORT_FEAT_RESET:
  1440. /* if it's already enabled, disable */
  1441. dum->port_status &= ~(USB_PORT_STAT_ENABLE
  1442. | USB_PORT_STAT_LOW_SPEED
  1443. | USB_PORT_STAT_HIGH_SPEED);
  1444. dum->devstatus = 0;
  1445. /* 50msec reset signaling */
  1446. dum->re_timeout = jiffies + msecs_to_jiffies(50);
  1447. /* FALLS THROUGH */
  1448. default:
  1449. if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
  1450. dum->port_status |= (1 << wValue);
  1451. set_link_state (dum);
  1452. }
  1453. }
  1454. break;
  1455. default:
  1456. dev_dbg (dummy_dev(dum),
  1457. "hub control req%04x v%04x i%04x l%d\n",
  1458. typeReq, wValue, wIndex, wLength);
  1459. /* "protocol stall" on error */
  1460. retval = -EPIPE;
  1461. }
  1462. spin_unlock_irqrestore (&dum->lock, flags);
  1463. if ((dum->port_status & PORT_C_MASK) != 0)
  1464. usb_hcd_poll_rh_status (hcd);
  1465. return retval;
  1466. }
  1467. static int dummy_bus_suspend (struct usb_hcd *hcd)
  1468. {
  1469. struct dummy *dum = hcd_to_dummy (hcd);
  1470. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1471. spin_lock_irq (&dum->lock);
  1472. dum->rh_state = DUMMY_RH_SUSPENDED;
  1473. set_link_state (dum);
  1474. hcd->state = HC_STATE_SUSPENDED;
  1475. spin_unlock_irq (&dum->lock);
  1476. return 0;
  1477. }
  1478. static int dummy_bus_resume (struct usb_hcd *hcd)
  1479. {
  1480. struct dummy *dum = hcd_to_dummy (hcd);
  1481. int rc = 0;
  1482. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1483. spin_lock_irq (&dum->lock);
  1484. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  1485. rc = -ESHUTDOWN;
  1486. } else {
  1487. dum->rh_state = DUMMY_RH_RUNNING;
  1488. set_link_state (dum);
  1489. if (!list_empty(&dum->urbp_list))
  1490. mod_timer (&dum->timer, jiffies);
  1491. hcd->state = HC_STATE_RUNNING;
  1492. }
  1493. spin_unlock_irq (&dum->lock);
  1494. return rc;
  1495. }
  1496. /*-------------------------------------------------------------------------*/
  1497. static inline ssize_t
  1498. show_urb (char *buf, size_t size, struct urb *urb)
  1499. {
  1500. int ep = usb_pipeendpoint (urb->pipe);
  1501. return snprintf (buf, size,
  1502. "urb/%p %s ep%d%s%s len %d/%d\n",
  1503. urb,
  1504. ({ char *s;
  1505. switch (urb->dev->speed) {
  1506. case USB_SPEED_LOW: s = "ls"; break;
  1507. case USB_SPEED_FULL: s = "fs"; break;
  1508. case USB_SPEED_HIGH: s = "hs"; break;
  1509. default: s = "?"; break;
  1510. }; s; }),
  1511. ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
  1512. ({ char *s; \
  1513. switch (usb_pipetype (urb->pipe)) { \
  1514. case PIPE_CONTROL: s = ""; break; \
  1515. case PIPE_BULK: s = "-bulk"; break; \
  1516. case PIPE_INTERRUPT: s = "-int"; break; \
  1517. default: s = "-iso"; break; \
  1518. }; s;}),
  1519. urb->actual_length, urb->transfer_buffer_length);
  1520. }
  1521. static ssize_t
  1522. show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
  1523. {
  1524. struct usb_hcd *hcd = dev_get_drvdata (dev);
  1525. struct dummy *dum = hcd_to_dummy (hcd);
  1526. struct urbp *urbp;
  1527. size_t size = 0;
  1528. unsigned long flags;
  1529. spin_lock_irqsave (&dum->lock, flags);
  1530. list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
  1531. size_t temp;
  1532. temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
  1533. buf += temp;
  1534. size += temp;
  1535. }
  1536. spin_unlock_irqrestore (&dum->lock, flags);
  1537. return size;
  1538. }
  1539. static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
  1540. static int dummy_start (struct usb_hcd *hcd)
  1541. {
  1542. struct dummy *dum;
  1543. dum = hcd_to_dummy (hcd);
  1544. /*
  1545. * MASTER side init ... we emulate a root hub that'll only ever
  1546. * talk to one device (the slave side). Also appears in sysfs,
  1547. * just like more familiar pci-based HCDs.
  1548. */
  1549. spin_lock_init (&dum->lock);
  1550. init_timer (&dum->timer);
  1551. dum->timer.function = dummy_timer;
  1552. dum->timer.data = (unsigned long) dum;
  1553. dum->rh_state = DUMMY_RH_RUNNING;
  1554. INIT_LIST_HEAD (&dum->urbp_list);
  1555. hcd->power_budget = POWER_BUDGET;
  1556. hcd->state = HC_STATE_RUNNING;
  1557. hcd->uses_new_polling = 1;
  1558. #ifdef CONFIG_USB_OTG
  1559. hcd->self.otg_port = 1;
  1560. #endif
  1561. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  1562. return device_create_file (dummy_dev(dum), &dev_attr_urbs);
  1563. }
  1564. static void dummy_stop (struct usb_hcd *hcd)
  1565. {
  1566. struct dummy *dum;
  1567. dum = hcd_to_dummy (hcd);
  1568. device_remove_file (dummy_dev(dum), &dev_attr_urbs);
  1569. usb_gadget_unregister_driver (dum->driver);
  1570. dev_info (dummy_dev(dum), "stopped\n");
  1571. }
  1572. /*-------------------------------------------------------------------------*/
  1573. static int dummy_h_get_frame (struct usb_hcd *hcd)
  1574. {
  1575. return dummy_g_get_frame (NULL);
  1576. }
  1577. static const struct hc_driver dummy_hcd = {
  1578. .description = (char *) driver_name,
  1579. .product_desc = "Dummy host controller",
  1580. .hcd_priv_size = sizeof(struct dummy),
  1581. .flags = HCD_USB2,
  1582. .start = dummy_start,
  1583. .stop = dummy_stop,
  1584. .urb_enqueue = dummy_urb_enqueue,
  1585. .urb_dequeue = dummy_urb_dequeue,
  1586. .get_frame_number = dummy_h_get_frame,
  1587. .hub_status_data = dummy_hub_status,
  1588. .hub_control = dummy_hub_control,
  1589. .bus_suspend = dummy_bus_suspend,
  1590. .bus_resume = dummy_bus_resume,
  1591. };
  1592. static int dummy_hcd_probe(struct platform_device *pdev)
  1593. {
  1594. struct usb_hcd *hcd;
  1595. int retval;
  1596. dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
  1597. hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
  1598. if (!hcd)
  1599. return -ENOMEM;
  1600. the_controller = hcd_to_dummy (hcd);
  1601. retval = usb_add_hcd(hcd, 0, 0);
  1602. if (retval != 0) {
  1603. usb_put_hcd (hcd);
  1604. the_controller = NULL;
  1605. }
  1606. return retval;
  1607. }
  1608. static int dummy_hcd_remove (struct platform_device *pdev)
  1609. {
  1610. struct usb_hcd *hcd;
  1611. hcd = platform_get_drvdata (pdev);
  1612. usb_remove_hcd (hcd);
  1613. usb_put_hcd (hcd);
  1614. the_controller = NULL;
  1615. return 0;
  1616. }
  1617. static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
  1618. {
  1619. struct usb_hcd *hcd;
  1620. struct dummy *dum;
  1621. int rc = 0;
  1622. dev_dbg (&pdev->dev, "%s\n", __func__);
  1623. hcd = platform_get_drvdata (pdev);
  1624. dum = hcd_to_dummy (hcd);
  1625. if (dum->rh_state == DUMMY_RH_RUNNING) {
  1626. dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
  1627. rc = -EBUSY;
  1628. } else
  1629. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1630. return rc;
  1631. }
  1632. static int dummy_hcd_resume (struct platform_device *pdev)
  1633. {
  1634. struct usb_hcd *hcd;
  1635. dev_dbg (&pdev->dev, "%s\n", __func__);
  1636. hcd = platform_get_drvdata (pdev);
  1637. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1638. usb_hcd_poll_rh_status (hcd);
  1639. return 0;
  1640. }
  1641. static struct platform_driver dummy_hcd_driver = {
  1642. .probe = dummy_hcd_probe,
  1643. .remove = dummy_hcd_remove,
  1644. .suspend = dummy_hcd_suspend,
  1645. .resume = dummy_hcd_resume,
  1646. .driver = {
  1647. .name = (char *) driver_name,
  1648. .owner = THIS_MODULE,
  1649. },
  1650. };
  1651. /*-------------------------------------------------------------------------*/
  1652. static struct platform_device *the_udc_pdev;
  1653. static struct platform_device *the_hcd_pdev;
  1654. static int __init init (void)
  1655. {
  1656. int retval = -ENOMEM;
  1657. if (usb_disabled ())
  1658. return -ENODEV;
  1659. the_hcd_pdev = platform_device_alloc(driver_name, -1);
  1660. if (!the_hcd_pdev)
  1661. return retval;
  1662. the_udc_pdev = platform_device_alloc(gadget_name, -1);
  1663. if (!the_udc_pdev)
  1664. goto err_alloc_udc;
  1665. retval = platform_driver_register(&dummy_hcd_driver);
  1666. if (retval < 0)
  1667. goto err_register_hcd_driver;
  1668. retval = platform_driver_register(&dummy_udc_driver);
  1669. if (retval < 0)
  1670. goto err_register_udc_driver;
  1671. retval = platform_device_add(the_hcd_pdev);
  1672. if (retval < 0)
  1673. goto err_add_hcd;
  1674. retval = platform_device_add(the_udc_pdev);
  1675. if (retval < 0)
  1676. goto err_add_udc;
  1677. return retval;
  1678. err_add_udc:
  1679. platform_device_del(the_hcd_pdev);
  1680. err_add_hcd:
  1681. platform_driver_unregister(&dummy_udc_driver);
  1682. err_register_udc_driver:
  1683. platform_driver_unregister(&dummy_hcd_driver);
  1684. err_register_hcd_driver:
  1685. platform_device_put(the_udc_pdev);
  1686. err_alloc_udc:
  1687. platform_device_put(the_hcd_pdev);
  1688. return retval;
  1689. }
  1690. module_init (init);
  1691. static void __exit cleanup (void)
  1692. {
  1693. platform_device_unregister(the_udc_pdev);
  1694. platform_device_unregister(the_hcd_pdev);
  1695. platform_driver_unregister(&dummy_udc_driver);
  1696. platform_driver_unregister(&dummy_hcd_driver);
  1697. }
  1698. module_exit (cleanup);