dummy_hcd.c 49 KB

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