dummy_hcd.c 43 KB

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