dummy_hcd.c 49 KB

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