dummy_hcd.c 50 KB

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