dummy_hcd.c 49 KB

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