dummy_hcd.c 50 KB

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