dummy_hcd.c 46 KB

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