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. spin_unlock (&dum->lock);
  462. _req->actual = _req->length;
  463. _req->status = 0;
  464. _req->complete (_ep, _req);
  465. spin_lock (&dum->lock);
  466. }
  467. list_add_tail (&req->queue, &ep->queue);
  468. spin_unlock_irqrestore (&dum->lock, flags);
  469. /* real hardware would likely enable transfers here, in case
  470. * it'd been left NAKing.
  471. */
  472. return 0;
  473. }
  474. static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
  475. {
  476. struct dummy_ep *ep;
  477. struct dummy *dum;
  478. int retval = -EINVAL;
  479. unsigned long flags;
  480. struct dummy_request *req = NULL;
  481. if (!_ep || !_req)
  482. return retval;
  483. ep = usb_ep_to_dummy_ep (_ep);
  484. dum = ep_to_dummy (ep);
  485. if (!dum->driver)
  486. return -ESHUTDOWN;
  487. local_irq_save (flags);
  488. spin_lock (&dum->lock);
  489. list_for_each_entry (req, &ep->queue, queue) {
  490. if (&req->req == _req) {
  491. list_del_init (&req->queue);
  492. _req->status = -ECONNRESET;
  493. retval = 0;
  494. break;
  495. }
  496. }
  497. spin_unlock (&dum->lock);
  498. if (retval == 0) {
  499. dev_dbg (udc_dev(dum),
  500. "dequeued req %p from %s, len %d buf %p\n",
  501. req, _ep->name, _req->length, _req->buf);
  502. _req->complete (_ep, _req);
  503. }
  504. local_irq_restore (flags);
  505. return retval;
  506. }
  507. static int
  508. dummy_set_halt (struct usb_ep *_ep, int value)
  509. {
  510. struct dummy_ep *ep;
  511. struct dummy *dum;
  512. if (!_ep)
  513. return -EINVAL;
  514. ep = usb_ep_to_dummy_ep (_ep);
  515. dum = ep_to_dummy (ep);
  516. if (!dum->driver)
  517. return -ESHUTDOWN;
  518. if (!value)
  519. ep->halted = 0;
  520. else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  521. !list_empty (&ep->queue))
  522. return -EAGAIN;
  523. else
  524. ep->halted = 1;
  525. /* FIXME clear emulated data toggle too */
  526. return 0;
  527. }
  528. static const struct usb_ep_ops dummy_ep_ops = {
  529. .enable = dummy_enable,
  530. .disable = dummy_disable,
  531. .alloc_request = dummy_alloc_request,
  532. .free_request = dummy_free_request,
  533. .queue = dummy_queue,
  534. .dequeue = dummy_dequeue,
  535. .set_halt = dummy_set_halt,
  536. };
  537. /*-------------------------------------------------------------------------*/
  538. /* there are both host and device side versions of this call ... */
  539. static int dummy_g_get_frame (struct usb_gadget *_gadget)
  540. {
  541. struct timeval tv;
  542. do_gettimeofday (&tv);
  543. return tv.tv_usec / 1000;
  544. }
  545. static int dummy_wakeup (struct usb_gadget *_gadget)
  546. {
  547. struct dummy *dum;
  548. dum = gadget_to_dummy (_gadget);
  549. if (!(dum->devstatus & ( (1 << USB_DEVICE_B_HNP_ENABLE)
  550. | (1 << USB_DEVICE_REMOTE_WAKEUP))))
  551. return -EINVAL;
  552. if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0)
  553. return -ENOLINK;
  554. if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
  555. dum->rh_state != DUMMY_RH_SUSPENDED)
  556. return -EIO;
  557. /* FIXME: What if the root hub is suspended but the port isn't? */
  558. /* hub notices our request, issues downstream resume, etc */
  559. dum->resuming = 1;
  560. dum->re_timeout = jiffies + msecs_to_jiffies(20);
  561. mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
  562. return 0;
  563. }
  564. static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
  565. {
  566. struct dummy *dum;
  567. dum = gadget_to_dummy (_gadget);
  568. if (value)
  569. dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
  570. else
  571. dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  572. return 0;
  573. }
  574. static int dummy_pullup (struct usb_gadget *_gadget, int value)
  575. {
  576. struct dummy *dum;
  577. unsigned long flags;
  578. dum = gadget_to_dummy (_gadget);
  579. spin_lock_irqsave (&dum->lock, flags);
  580. dum->pullup = (value != 0);
  581. set_link_state (dum);
  582. spin_unlock_irqrestore (&dum->lock, flags);
  583. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  584. return 0;
  585. }
  586. static const struct usb_gadget_ops dummy_ops = {
  587. .get_frame = dummy_g_get_frame,
  588. .wakeup = dummy_wakeup,
  589. .set_selfpowered = dummy_set_selfpowered,
  590. .pullup = dummy_pullup,
  591. };
  592. /*-------------------------------------------------------------------------*/
  593. /* "function" sysfs attribute */
  594. static ssize_t
  595. show_function (struct device *dev, struct device_attribute *attr, char *buf)
  596. {
  597. struct dummy *dum = gadget_dev_to_dummy (dev);
  598. if (!dum->driver || !dum->driver->function)
  599. return 0;
  600. return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
  601. }
  602. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  603. /*-------------------------------------------------------------------------*/
  604. /*
  605. * Driver registration/unregistration.
  606. *
  607. * This is basically hardware-specific; there's usually only one real USB
  608. * device (not host) controller since that's how USB devices are intended
  609. * to work. So most implementations of these api calls will rely on the
  610. * fact that only one driver will ever bind to the hardware. But curious
  611. * hardware can be built with discrete components, so the gadget API doesn't
  612. * require that assumption.
  613. *
  614. * For this emulator, it might be convenient to create a usb slave device
  615. * for each driver that registers: just add to a big root hub.
  616. */
  617. int
  618. usb_gadget_register_driver (struct usb_gadget_driver *driver)
  619. {
  620. struct dummy *dum = the_controller;
  621. int retval, i;
  622. if (!dum)
  623. return -EINVAL;
  624. if (dum->driver)
  625. return -EBUSY;
  626. if (!driver->bind || !driver->setup
  627. || driver->speed == USB_SPEED_UNKNOWN)
  628. return -EINVAL;
  629. /*
  630. * SLAVE side init ... the layer above hardware, which
  631. * can't enumerate without help from the driver we're binding.
  632. */
  633. dum->devstatus = 0;
  634. INIT_LIST_HEAD (&dum->gadget.ep_list);
  635. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  636. struct dummy_ep *ep = &dum->ep [i];
  637. if (!ep_name [i])
  638. break;
  639. ep->ep.name = ep_name [i];
  640. ep->ep.ops = &dummy_ep_ops;
  641. list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
  642. ep->halted = ep->already_seen = ep->setup_stage = 0;
  643. ep->ep.maxpacket = ~0;
  644. ep->last_io = jiffies;
  645. ep->gadget = &dum->gadget;
  646. ep->desc = NULL;
  647. INIT_LIST_HEAD (&ep->queue);
  648. }
  649. dum->gadget.ep0 = &dum->ep [0].ep;
  650. dum->ep [0].ep.maxpacket = 64;
  651. list_del_init (&dum->ep [0].ep.ep_list);
  652. INIT_LIST_HEAD(&dum->fifo_req.queue);
  653. driver->driver.bus = NULL;
  654. dum->driver = driver;
  655. dum->gadget.dev.driver = &driver->driver;
  656. dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
  657. driver->driver.name);
  658. retval = driver->bind(&dum->gadget);
  659. if (retval) {
  660. dum->driver = NULL;
  661. dum->gadget.dev.driver = NULL;
  662. return retval;
  663. }
  664. /* khubd will enumerate this in a while */
  665. spin_lock_irq (&dum->lock);
  666. dum->pullup = 1;
  667. set_link_state (dum);
  668. spin_unlock_irq (&dum->lock);
  669. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  670. return 0;
  671. }
  672. EXPORT_SYMBOL (usb_gadget_register_driver);
  673. int
  674. usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  675. {
  676. struct dummy *dum = the_controller;
  677. unsigned long flags;
  678. if (!dum)
  679. return -ENODEV;
  680. if (!driver || driver != dum->driver || !driver->unbind)
  681. return -EINVAL;
  682. dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
  683. driver->driver.name);
  684. spin_lock_irqsave (&dum->lock, flags);
  685. dum->pullup = 0;
  686. set_link_state (dum);
  687. spin_unlock_irqrestore (&dum->lock, flags);
  688. driver->unbind (&dum->gadget);
  689. dum->gadget.dev.driver = NULL;
  690. dum->driver = NULL;
  691. spin_lock_irqsave (&dum->lock, flags);
  692. dum->pullup = 0;
  693. set_link_state (dum);
  694. spin_unlock_irqrestore (&dum->lock, flags);
  695. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  696. return 0;
  697. }
  698. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  699. #undef is_enabled
  700. /* just declare this in any driver that really need it */
  701. extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
  702. int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
  703. {
  704. return -ENOSYS;
  705. }
  706. EXPORT_SYMBOL (net2280_set_fifo_mode);
  707. /* The gadget structure is stored inside the hcd structure and will be
  708. * released along with it. */
  709. static void
  710. dummy_gadget_release (struct device *dev)
  711. {
  712. struct dummy *dum = gadget_dev_to_dummy (dev);
  713. usb_put_hcd (dummy_to_hcd (dum));
  714. }
  715. static int dummy_udc_probe (struct platform_device *pdev)
  716. {
  717. struct dummy *dum = the_controller;
  718. int rc;
  719. dum->gadget.name = gadget_name;
  720. dum->gadget.ops = &dummy_ops;
  721. dum->gadget.is_dualspeed = 1;
  722. /* maybe claim OTG support, though we won't complete HNP */
  723. dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
  724. strcpy (dum->gadget.dev.bus_id, "gadget");
  725. dum->gadget.dev.parent = &pdev->dev;
  726. dum->gadget.dev.release = dummy_gadget_release;
  727. rc = device_register (&dum->gadget.dev);
  728. if (rc < 0)
  729. return rc;
  730. usb_get_hcd (dummy_to_hcd (dum));
  731. platform_set_drvdata (pdev, dum);
  732. rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
  733. if (rc < 0)
  734. device_unregister (&dum->gadget.dev);
  735. return rc;
  736. }
  737. static int dummy_udc_remove (struct platform_device *pdev)
  738. {
  739. struct dummy *dum = platform_get_drvdata (pdev);
  740. platform_set_drvdata (pdev, NULL);
  741. device_remove_file (&dum->gadget.dev, &dev_attr_function);
  742. device_unregister (&dum->gadget.dev);
  743. return 0;
  744. }
  745. static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
  746. {
  747. struct dummy *dum = platform_get_drvdata(pdev);
  748. dev_dbg (&pdev->dev, "%s\n", __func__);
  749. spin_lock_irq (&dum->lock);
  750. dum->udc_suspended = 1;
  751. set_link_state (dum);
  752. spin_unlock_irq (&dum->lock);
  753. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  754. return 0;
  755. }
  756. static int dummy_udc_resume (struct platform_device *pdev)
  757. {
  758. struct dummy *dum = platform_get_drvdata(pdev);
  759. dev_dbg (&pdev->dev, "%s\n", __func__);
  760. spin_lock_irq (&dum->lock);
  761. dum->udc_suspended = 0;
  762. set_link_state (dum);
  763. spin_unlock_irq (&dum->lock);
  764. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  765. return 0;
  766. }
  767. static struct platform_driver dummy_udc_driver = {
  768. .probe = dummy_udc_probe,
  769. .remove = dummy_udc_remove,
  770. .suspend = dummy_udc_suspend,
  771. .resume = dummy_udc_resume,
  772. .driver = {
  773. .name = (char *) gadget_name,
  774. .owner = THIS_MODULE,
  775. },
  776. };
  777. /*-------------------------------------------------------------------------*/
  778. /* MASTER/HOST SIDE DRIVER
  779. *
  780. * this uses the hcd framework to hook up to host side drivers.
  781. * its root hub will only have one device, otherwise it acts like
  782. * a normal host controller.
  783. *
  784. * when urbs are queued, they're just stuck on a list that we
  785. * scan in a timer callback. that callback connects writes from
  786. * the host with reads from the device, and so on, based on the
  787. * usb 2.0 rules.
  788. */
  789. static int dummy_urb_enqueue (
  790. struct usb_hcd *hcd,
  791. struct urb *urb,
  792. gfp_t mem_flags
  793. ) {
  794. struct dummy *dum;
  795. struct urbp *urbp;
  796. unsigned long flags;
  797. int rc;
  798. if (!urb->transfer_buffer && urb->transfer_buffer_length)
  799. return -EINVAL;
  800. urbp = kmalloc (sizeof *urbp, mem_flags);
  801. if (!urbp)
  802. return -ENOMEM;
  803. urbp->urb = urb;
  804. dum = hcd_to_dummy (hcd);
  805. spin_lock_irqsave (&dum->lock, flags);
  806. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  807. if (rc) {
  808. kfree(urbp);
  809. goto done;
  810. }
  811. if (!dum->udev) {
  812. dum->udev = urb->dev;
  813. usb_get_dev (dum->udev);
  814. } else if (unlikely (dum->udev != urb->dev))
  815. dev_err (dummy_dev(dum), "usb_device address has changed!\n");
  816. list_add_tail (&urbp->urbp_list, &dum->urbp_list);
  817. urb->hcpriv = urbp;
  818. if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
  819. urb->error_count = 1; /* mark as a new urb */
  820. /* kick the scheduler, it'll do the rest */
  821. if (!timer_pending (&dum->timer))
  822. mod_timer (&dum->timer, jiffies + 1);
  823. done:
  824. spin_unlock_irqrestore(&dum->lock, flags);
  825. return rc;
  826. }
  827. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  828. {
  829. struct dummy *dum;
  830. unsigned long flags;
  831. int rc;
  832. /* giveback happens automatically in timer callback,
  833. * so make sure the callback happens */
  834. dum = hcd_to_dummy (hcd);
  835. spin_lock_irqsave (&dum->lock, flags);
  836. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  837. if (!rc && dum->rh_state != DUMMY_RH_RUNNING &&
  838. !list_empty(&dum->urbp_list))
  839. mod_timer (&dum->timer, jiffies);
  840. spin_unlock_irqrestore (&dum->lock, flags);
  841. return rc;
  842. }
  843. /* transfer up to a frame's worth; caller must own lock */
  844. static int
  845. transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
  846. int *status)
  847. {
  848. struct dummy_request *req;
  849. top:
  850. /* if there's no request queued, the device is NAKing; return */
  851. list_for_each_entry (req, &ep->queue, queue) {
  852. unsigned host_len, dev_len, len;
  853. int is_short, to_host;
  854. int rescan = 0;
  855. /* 1..N packets of ep->ep.maxpacket each ... the last one
  856. * may be short (including zero length).
  857. *
  858. * writer can send a zlp explicitly (length 0) or implicitly
  859. * (length mod maxpacket zero, and 'zero' flag); they always
  860. * terminate reads.
  861. */
  862. host_len = urb->transfer_buffer_length - urb->actual_length;
  863. dev_len = req->req.length - req->req.actual;
  864. len = min (host_len, dev_len);
  865. /* FIXME update emulated data toggle too */
  866. to_host = usb_pipein (urb->pipe);
  867. if (unlikely (len == 0))
  868. is_short = 1;
  869. else {
  870. char *ubuf, *rbuf;
  871. /* not enough bandwidth left? */
  872. if (limit < ep->ep.maxpacket && limit < len)
  873. break;
  874. len = min (len, (unsigned) limit);
  875. if (len == 0)
  876. break;
  877. /* use an extra pass for the final short packet */
  878. if (len > ep->ep.maxpacket) {
  879. rescan = 1;
  880. len -= (len % ep->ep.maxpacket);
  881. }
  882. is_short = (len % ep->ep.maxpacket) != 0;
  883. /* else transfer packet(s) */
  884. ubuf = urb->transfer_buffer + urb->actual_length;
  885. rbuf = req->req.buf + req->req.actual;
  886. if (to_host)
  887. memcpy (ubuf, rbuf, len);
  888. else
  889. memcpy (rbuf, ubuf, len);
  890. ep->last_io = jiffies;
  891. limit -= len;
  892. urb->actual_length += len;
  893. req->req.actual += len;
  894. }
  895. /* short packets terminate, maybe with overflow/underflow.
  896. * it's only really an error to write too much.
  897. *
  898. * partially filling a buffer optionally blocks queue advances
  899. * (so completion handlers can clean up the queue) but we don't
  900. * need to emulate such data-in-flight.
  901. */
  902. if (is_short) {
  903. if (host_len == dev_len) {
  904. req->req.status = 0;
  905. *status = 0;
  906. } else if (to_host) {
  907. req->req.status = 0;
  908. if (dev_len > host_len)
  909. *status = -EOVERFLOW;
  910. else
  911. *status = 0;
  912. } else if (!to_host) {
  913. *status = 0;
  914. if (host_len > dev_len)
  915. req->req.status = -EOVERFLOW;
  916. else
  917. req->req.status = 0;
  918. }
  919. /* many requests terminate without a short packet */
  920. } else {
  921. if (req->req.length == req->req.actual
  922. && !req->req.zero)
  923. req->req.status = 0;
  924. if (urb->transfer_buffer_length == urb->actual_length
  925. && !(urb->transfer_flags
  926. & URB_ZERO_PACKET))
  927. *status = 0;
  928. }
  929. /* device side completion --> continuable */
  930. if (req->req.status != -EINPROGRESS) {
  931. list_del_init (&req->queue);
  932. spin_unlock (&dum->lock);
  933. req->req.complete (&ep->ep, &req->req);
  934. spin_lock (&dum->lock);
  935. /* requests might have been unlinked... */
  936. rescan = 1;
  937. }
  938. /* host side completion --> terminate */
  939. if (*status != -EINPROGRESS)
  940. break;
  941. /* rescan to continue with any other queued i/o */
  942. if (rescan)
  943. goto top;
  944. }
  945. return limit;
  946. }
  947. static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
  948. {
  949. int limit = ep->ep.maxpacket;
  950. if (dum->gadget.speed == USB_SPEED_HIGH) {
  951. int tmp;
  952. /* high bandwidth mode */
  953. tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
  954. tmp = (tmp >> 11) & 0x03;
  955. tmp *= 8 /* applies to entire frame */;
  956. limit += limit * tmp;
  957. }
  958. return limit;
  959. }
  960. #define is_active(dum) ((dum->port_status & \
  961. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  962. USB_PORT_STAT_SUSPEND)) \
  963. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  964. static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
  965. {
  966. int i;
  967. if (!is_active (dum))
  968. return NULL;
  969. if ((address & ~USB_DIR_IN) == 0)
  970. return &dum->ep [0];
  971. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  972. struct dummy_ep *ep = &dum->ep [i];
  973. if (!ep->desc)
  974. continue;
  975. if (ep->desc->bEndpointAddress == address)
  976. return ep;
  977. }
  978. return NULL;
  979. }
  980. #undef is_active
  981. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  982. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  983. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  984. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  985. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  986. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  987. /* drive both sides of the transfers; looks like irq handlers to
  988. * both drivers except the callbacks aren't in_irq().
  989. */
  990. static void dummy_timer (unsigned long _dum)
  991. {
  992. struct dummy *dum = (struct dummy *) _dum;
  993. struct urbp *urbp, *tmp;
  994. unsigned long flags;
  995. int limit, total;
  996. int i;
  997. /* simplistic model for one frame's bandwidth */
  998. switch (dum->gadget.speed) {
  999. case USB_SPEED_LOW:
  1000. total = 8/*bytes*/ * 12/*packets*/;
  1001. break;
  1002. case USB_SPEED_FULL:
  1003. total = 64/*bytes*/ * 19/*packets*/;
  1004. break;
  1005. case USB_SPEED_HIGH:
  1006. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1007. break;
  1008. default:
  1009. dev_err (dummy_dev(dum), "bogus device speed\n");
  1010. return;
  1011. }
  1012. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1013. /* look at each urb queued by the host side driver */
  1014. spin_lock_irqsave (&dum->lock, flags);
  1015. if (!dum->udev) {
  1016. dev_err (dummy_dev(dum),
  1017. "timer fired with no URBs pending?\n");
  1018. spin_unlock_irqrestore (&dum->lock, flags);
  1019. return;
  1020. }
  1021. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1022. if (!ep_name [i])
  1023. break;
  1024. dum->ep [i].already_seen = 0;
  1025. }
  1026. restart:
  1027. list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
  1028. struct urb *urb;
  1029. struct dummy_request *req;
  1030. u8 address;
  1031. struct dummy_ep *ep = NULL;
  1032. int type;
  1033. int status = -EINPROGRESS;
  1034. urb = urbp->urb;
  1035. if (urb->unlinked)
  1036. goto return_urb;
  1037. else if (dum->rh_state != DUMMY_RH_RUNNING)
  1038. continue;
  1039. type = usb_pipetype (urb->pipe);
  1040. /* used up this frame's non-periodic bandwidth?
  1041. * FIXME there's infinite bandwidth for control and
  1042. * periodic transfers ... unrealistic.
  1043. */
  1044. if (total <= 0 && type == PIPE_BULK)
  1045. continue;
  1046. /* find the gadget's ep for this request (if configured) */
  1047. address = usb_pipeendpoint (urb->pipe);
  1048. if (usb_pipein (urb->pipe))
  1049. address |= USB_DIR_IN;
  1050. ep = find_endpoint(dum, address);
  1051. if (!ep) {
  1052. /* set_configuration() disagreement */
  1053. dev_dbg (dummy_dev(dum),
  1054. "no ep configured for urb %p\n",
  1055. urb);
  1056. status = -EPROTO;
  1057. goto return_urb;
  1058. }
  1059. if (ep->already_seen)
  1060. continue;
  1061. ep->already_seen = 1;
  1062. if (ep == &dum->ep [0] && urb->error_count) {
  1063. ep->setup_stage = 1; /* a new urb */
  1064. urb->error_count = 0;
  1065. }
  1066. if (ep->halted && !ep->setup_stage) {
  1067. /* NOTE: must not be iso! */
  1068. dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
  1069. ep->ep.name, urb);
  1070. status = -EPIPE;
  1071. goto return_urb;
  1072. }
  1073. /* FIXME make sure both ends agree on maxpacket */
  1074. /* handle control requests */
  1075. if (ep == &dum->ep [0] && ep->setup_stage) {
  1076. struct usb_ctrlrequest setup;
  1077. int value = 1;
  1078. struct dummy_ep *ep2;
  1079. unsigned w_index;
  1080. unsigned w_value;
  1081. setup = *(struct usb_ctrlrequest*) urb->setup_packet;
  1082. w_index = le16_to_cpu(setup.wIndex);
  1083. w_value = le16_to_cpu(setup.wValue);
  1084. if (le16_to_cpu(setup.wLength) !=
  1085. urb->transfer_buffer_length) {
  1086. status = -EOVERFLOW;
  1087. goto return_urb;
  1088. }
  1089. /* paranoia, in case of stale queued data */
  1090. list_for_each_entry (req, &ep->queue, queue) {
  1091. list_del_init (&req->queue);
  1092. req->req.status = -EOVERFLOW;
  1093. dev_dbg (udc_dev(dum), "stale req = %p\n",
  1094. req);
  1095. spin_unlock (&dum->lock);
  1096. req->req.complete (&ep->ep, &req->req);
  1097. spin_lock (&dum->lock);
  1098. ep->already_seen = 0;
  1099. goto restart;
  1100. }
  1101. /* gadget driver never sees set_address or operations
  1102. * on standard feature flags. some hardware doesn't
  1103. * even expose them.
  1104. */
  1105. ep->last_io = jiffies;
  1106. ep->setup_stage = 0;
  1107. ep->halted = 0;
  1108. switch (setup.bRequest) {
  1109. case USB_REQ_SET_ADDRESS:
  1110. if (setup.bRequestType != Dev_Request)
  1111. break;
  1112. dum->address = w_value;
  1113. status = 0;
  1114. dev_dbg (udc_dev(dum), "set_address = %d\n",
  1115. w_value);
  1116. value = 0;
  1117. break;
  1118. case USB_REQ_SET_FEATURE:
  1119. if (setup.bRequestType == Dev_Request) {
  1120. value = 0;
  1121. switch (w_value) {
  1122. case USB_DEVICE_REMOTE_WAKEUP:
  1123. break;
  1124. case USB_DEVICE_B_HNP_ENABLE:
  1125. dum->gadget.b_hnp_enable = 1;
  1126. break;
  1127. case USB_DEVICE_A_HNP_SUPPORT:
  1128. dum->gadget.a_hnp_support = 1;
  1129. break;
  1130. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1131. dum->gadget.a_alt_hnp_support
  1132. = 1;
  1133. break;
  1134. default:
  1135. value = -EOPNOTSUPP;
  1136. }
  1137. if (value == 0) {
  1138. dum->devstatus |=
  1139. (1 << w_value);
  1140. status = 0;
  1141. }
  1142. } else if (setup.bRequestType == Ep_Request) {
  1143. // endpoint halt
  1144. ep2 = find_endpoint (dum, w_index);
  1145. if (!ep2) {
  1146. value = -EOPNOTSUPP;
  1147. break;
  1148. }
  1149. ep2->halted = 1;
  1150. value = 0;
  1151. status = 0;
  1152. }
  1153. break;
  1154. case USB_REQ_CLEAR_FEATURE:
  1155. if (setup.bRequestType == Dev_Request) {
  1156. switch (w_value) {
  1157. case USB_DEVICE_REMOTE_WAKEUP:
  1158. dum->devstatus &= ~(1 <<
  1159. USB_DEVICE_REMOTE_WAKEUP);
  1160. value = 0;
  1161. status = 0;
  1162. break;
  1163. default:
  1164. value = -EOPNOTSUPP;
  1165. break;
  1166. }
  1167. } else if (setup.bRequestType == Ep_Request) {
  1168. // endpoint halt
  1169. ep2 = find_endpoint (dum, w_index);
  1170. if (!ep2) {
  1171. value = -EOPNOTSUPP;
  1172. break;
  1173. }
  1174. ep2->halted = 0;
  1175. value = 0;
  1176. status = 0;
  1177. }
  1178. break;
  1179. case USB_REQ_GET_STATUS:
  1180. if (setup.bRequestType == Dev_InRequest
  1181. || setup.bRequestType
  1182. == Intf_InRequest
  1183. || setup.bRequestType
  1184. == Ep_InRequest
  1185. ) {
  1186. char *buf;
  1187. // device: remote wakeup, selfpowered
  1188. // interface: nothing
  1189. // endpoint: halt
  1190. buf = (char *)urb->transfer_buffer;
  1191. if (urb->transfer_buffer_length > 0) {
  1192. if (setup.bRequestType ==
  1193. Ep_InRequest) {
  1194. ep2 = find_endpoint (dum, w_index);
  1195. if (!ep2) {
  1196. value = -EOPNOTSUPP;
  1197. break;
  1198. }
  1199. buf [0] = ep2->halted;
  1200. } else if (setup.bRequestType ==
  1201. Dev_InRequest) {
  1202. buf [0] = (u8)
  1203. dum->devstatus;
  1204. } else
  1205. buf [0] = 0;
  1206. }
  1207. if (urb->transfer_buffer_length > 1)
  1208. buf [1] = 0;
  1209. urb->actual_length = min (2,
  1210. urb->transfer_buffer_length);
  1211. value = 0;
  1212. status = 0;
  1213. }
  1214. break;
  1215. }
  1216. /* gadget driver handles all other requests. block
  1217. * until setup() returns; no reentrancy issues etc.
  1218. */
  1219. if (value > 0) {
  1220. spin_unlock (&dum->lock);
  1221. value = dum->driver->setup (&dum->gadget,
  1222. &setup);
  1223. spin_lock (&dum->lock);
  1224. if (value >= 0) {
  1225. /* no delays (max 64KB data stage) */
  1226. limit = 64*1024;
  1227. goto treat_control_like_bulk;
  1228. }
  1229. /* error, see below */
  1230. }
  1231. if (value < 0) {
  1232. if (value != -EOPNOTSUPP)
  1233. dev_dbg (udc_dev(dum),
  1234. "setup --> %d\n",
  1235. value);
  1236. status = -EPIPE;
  1237. urb->actual_length = 0;
  1238. }
  1239. goto return_urb;
  1240. }
  1241. /* non-control requests */
  1242. limit = total;
  1243. switch (usb_pipetype (urb->pipe)) {
  1244. case PIPE_ISOCHRONOUS:
  1245. /* FIXME is it urb->interval since the last xfer?
  1246. * use urb->iso_frame_desc[i].
  1247. * complete whether or not ep has requests queued.
  1248. * report random errors, to debug drivers.
  1249. */
  1250. limit = max (limit, periodic_bytes (dum, ep));
  1251. status = -ENOSYS;
  1252. break;
  1253. case PIPE_INTERRUPT:
  1254. /* FIXME is it urb->interval since the last xfer?
  1255. * this almost certainly polls too fast.
  1256. */
  1257. limit = max (limit, periodic_bytes (dum, ep));
  1258. /* FALLTHROUGH */
  1259. // case PIPE_BULK: case PIPE_CONTROL:
  1260. default:
  1261. treat_control_like_bulk:
  1262. ep->last_io = jiffies;
  1263. total = transfer(dum, urb, ep, limit, &status);
  1264. break;
  1265. }
  1266. /* incomplete transfer? */
  1267. if (status == -EINPROGRESS)
  1268. continue;
  1269. return_urb:
  1270. list_del (&urbp->urbp_list);
  1271. kfree (urbp);
  1272. if (ep)
  1273. ep->already_seen = ep->setup_stage = 0;
  1274. usb_hcd_unlink_urb_from_ep(dummy_to_hcd(dum), urb);
  1275. spin_unlock (&dum->lock);
  1276. usb_hcd_giveback_urb(dummy_to_hcd(dum), urb, status);
  1277. spin_lock (&dum->lock);
  1278. goto restart;
  1279. }
  1280. if (list_empty (&dum->urbp_list)) {
  1281. usb_put_dev (dum->udev);
  1282. dum->udev = NULL;
  1283. } else if (dum->rh_state == DUMMY_RH_RUNNING) {
  1284. /* want a 1 msec delay here */
  1285. mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
  1286. }
  1287. spin_unlock_irqrestore (&dum->lock, flags);
  1288. }
  1289. /*-------------------------------------------------------------------------*/
  1290. #define PORT_C_MASK \
  1291. ((USB_PORT_STAT_C_CONNECTION \
  1292. | USB_PORT_STAT_C_ENABLE \
  1293. | USB_PORT_STAT_C_SUSPEND \
  1294. | USB_PORT_STAT_C_OVERCURRENT \
  1295. | USB_PORT_STAT_C_RESET) << 16)
  1296. static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
  1297. {
  1298. struct dummy *dum;
  1299. unsigned long flags;
  1300. int retval = 0;
  1301. dum = hcd_to_dummy (hcd);
  1302. spin_lock_irqsave (&dum->lock, flags);
  1303. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
  1304. goto done;
  1305. if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
  1306. dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1307. dum->port_status &= ~USB_PORT_STAT_SUSPEND;
  1308. set_link_state (dum);
  1309. }
  1310. if ((dum->port_status & PORT_C_MASK) != 0) {
  1311. *buf = (1 << 1);
  1312. dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
  1313. dum->port_status);
  1314. retval = 1;
  1315. if (dum->rh_state == DUMMY_RH_SUSPENDED)
  1316. usb_hcd_resume_root_hub (hcd);
  1317. }
  1318. done:
  1319. spin_unlock_irqrestore (&dum->lock, flags);
  1320. return retval;
  1321. }
  1322. static inline void
  1323. hub_descriptor (struct usb_hub_descriptor *desc)
  1324. {
  1325. memset (desc, 0, sizeof *desc);
  1326. desc->bDescriptorType = 0x29;
  1327. desc->bDescLength = 9;
  1328. desc->wHubCharacteristics = (__force __u16)
  1329. (__constant_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, pdev->dev.bus_id);
  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);