dummy_hcd.c 44 KB

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