dummy_hcd.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  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 int dummy_udc_start(struct usb_gadget_driver *driver,
  605. int (*bind)(struct usb_gadget *));
  606. static int dummy_udc_stop(struct usb_gadget_driver *driver);
  607. static const struct usb_gadget_ops dummy_ops = {
  608. .get_frame = dummy_g_get_frame,
  609. .wakeup = dummy_wakeup,
  610. .set_selfpowered = dummy_set_selfpowered,
  611. .pullup = dummy_pullup,
  612. .start = dummy_udc_start,
  613. .stop = dummy_udc_stop,
  614. };
  615. /*-------------------------------------------------------------------------*/
  616. /* "function" sysfs attribute */
  617. static ssize_t
  618. show_function (struct device *dev, struct device_attribute *attr, char *buf)
  619. {
  620. struct dummy *dum = gadget_dev_to_dummy (dev);
  621. if (!dum->driver || !dum->driver->function)
  622. return 0;
  623. return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
  624. }
  625. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  626. /*-------------------------------------------------------------------------*/
  627. /*
  628. * Driver registration/unregistration.
  629. *
  630. * This is basically hardware-specific; there's usually only one real USB
  631. * device (not host) controller since that's how USB devices are intended
  632. * to work. So most implementations of these api calls will rely on the
  633. * fact that only one driver will ever bind to the hardware. But curious
  634. * hardware can be built with discrete components, so the gadget API doesn't
  635. * require that assumption.
  636. *
  637. * For this emulator, it might be convenient to create a usb slave device
  638. * for each driver that registers: just add to a big root hub.
  639. */
  640. static int dummy_udc_start(struct usb_gadget_driver *driver,
  641. int (*bind)(struct usb_gadget *))
  642. {
  643. struct dummy *dum = the_controller;
  644. int retval, i;
  645. if (!dum)
  646. return -EINVAL;
  647. if (dum->driver)
  648. return -EBUSY;
  649. if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
  650. return -EINVAL;
  651. /*
  652. * SLAVE side init ... the layer above hardware, which
  653. * can't enumerate without help from the driver we're binding.
  654. */
  655. dum->devstatus = 0;
  656. INIT_LIST_HEAD (&dum->gadget.ep_list);
  657. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  658. struct dummy_ep *ep = &dum->ep [i];
  659. if (!ep_name [i])
  660. break;
  661. ep->ep.name = ep_name [i];
  662. ep->ep.ops = &dummy_ep_ops;
  663. list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
  664. ep->halted = ep->wedged = ep->already_seen =
  665. ep->setup_stage = 0;
  666. ep->ep.maxpacket = ~0;
  667. ep->last_io = jiffies;
  668. ep->gadget = &dum->gadget;
  669. ep->desc = NULL;
  670. INIT_LIST_HEAD (&ep->queue);
  671. }
  672. dum->gadget.ep0 = &dum->ep [0].ep;
  673. dum->ep [0].ep.maxpacket = 64;
  674. list_del_init (&dum->ep [0].ep.ep_list);
  675. INIT_LIST_HEAD(&dum->fifo_req.queue);
  676. driver->driver.bus = NULL;
  677. dum->driver = driver;
  678. dum->gadget.dev.driver = &driver->driver;
  679. dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
  680. driver->driver.name);
  681. retval = bind(&dum->gadget);
  682. if (retval) {
  683. dum->driver = NULL;
  684. dum->gadget.dev.driver = NULL;
  685. return retval;
  686. }
  687. /* khubd will enumerate this in a while */
  688. spin_lock_irq (&dum->lock);
  689. dum->pullup = 1;
  690. set_link_state (dum);
  691. spin_unlock_irq (&dum->lock);
  692. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  693. return 0;
  694. }
  695. static int dummy_udc_stop(struct usb_gadget_driver *driver)
  696. {
  697. struct dummy *dum = the_controller;
  698. unsigned long flags;
  699. if (!dum)
  700. return -ENODEV;
  701. if (!driver || driver != dum->driver || !driver->unbind)
  702. return -EINVAL;
  703. dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
  704. driver->driver.name);
  705. spin_lock_irqsave (&dum->lock, flags);
  706. dum->pullup = 0;
  707. set_link_state (dum);
  708. spin_unlock_irqrestore (&dum->lock, flags);
  709. driver->unbind (&dum->gadget);
  710. dum->gadget.dev.driver = NULL;
  711. dum->driver = NULL;
  712. spin_lock_irqsave (&dum->lock, flags);
  713. dum->pullup = 0;
  714. set_link_state (dum);
  715. spin_unlock_irqrestore (&dum->lock, flags);
  716. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  717. return 0;
  718. }
  719. #undef is_enabled
  720. /* just declare this in any driver that really need it */
  721. extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
  722. int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
  723. {
  724. return -ENOSYS;
  725. }
  726. EXPORT_SYMBOL (net2280_set_fifo_mode);
  727. /* The gadget structure is stored inside the hcd structure and will be
  728. * released along with it. */
  729. static void
  730. dummy_gadget_release (struct device *dev)
  731. {
  732. struct dummy *dum = gadget_dev_to_dummy (dev);
  733. usb_put_hcd (dummy_to_hcd (dum));
  734. }
  735. static int dummy_udc_probe (struct platform_device *pdev)
  736. {
  737. struct dummy *dum = the_controller;
  738. int rc;
  739. usb_get_hcd(dummy_to_hcd(dum));
  740. dum->gadget.name = gadget_name;
  741. dum->gadget.ops = &dummy_ops;
  742. dum->gadget.is_dualspeed = 1;
  743. /* maybe claim OTG support, though we won't complete HNP */
  744. dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
  745. dev_set_name(&dum->gadget.dev, "gadget");
  746. dum->gadget.dev.parent = &pdev->dev;
  747. dum->gadget.dev.release = dummy_gadget_release;
  748. rc = device_register (&dum->gadget.dev);
  749. if (rc < 0) {
  750. put_device(&dum->gadget.dev);
  751. return rc;
  752. }
  753. rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
  754. if (rc < 0)
  755. goto err_udc;
  756. rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
  757. if (rc < 0)
  758. goto err_dev;
  759. platform_set_drvdata(pdev, dum);
  760. return rc;
  761. err_dev:
  762. usb_del_gadget_udc(&dum->gadget);
  763. err_udc:
  764. device_unregister(&dum->gadget.dev);
  765. return rc;
  766. }
  767. static int dummy_udc_remove (struct platform_device *pdev)
  768. {
  769. struct dummy *dum = platform_get_drvdata (pdev);
  770. usb_del_gadget_udc(&dum->gadget);
  771. platform_set_drvdata (pdev, NULL);
  772. device_remove_file (&dum->gadget.dev, &dev_attr_function);
  773. device_unregister (&dum->gadget.dev);
  774. return 0;
  775. }
  776. static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
  777. {
  778. struct dummy *dum = platform_get_drvdata(pdev);
  779. dev_dbg (&pdev->dev, "%s\n", __func__);
  780. spin_lock_irq (&dum->lock);
  781. dum->udc_suspended = 1;
  782. set_link_state (dum);
  783. spin_unlock_irq (&dum->lock);
  784. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  785. return 0;
  786. }
  787. static int dummy_udc_resume (struct platform_device *pdev)
  788. {
  789. struct dummy *dum = platform_get_drvdata(pdev);
  790. dev_dbg (&pdev->dev, "%s\n", __func__);
  791. spin_lock_irq (&dum->lock);
  792. dum->udc_suspended = 0;
  793. set_link_state (dum);
  794. spin_unlock_irq (&dum->lock);
  795. usb_hcd_poll_rh_status (dummy_to_hcd (dum));
  796. return 0;
  797. }
  798. static struct platform_driver dummy_udc_driver = {
  799. .probe = dummy_udc_probe,
  800. .remove = dummy_udc_remove,
  801. .suspend = dummy_udc_suspend,
  802. .resume = dummy_udc_resume,
  803. .driver = {
  804. .name = (char *) gadget_name,
  805. .owner = THIS_MODULE,
  806. },
  807. };
  808. /*-------------------------------------------------------------------------*/
  809. /* MASTER/HOST SIDE DRIVER
  810. *
  811. * this uses the hcd framework to hook up to host side drivers.
  812. * its root hub will only have one device, otherwise it acts like
  813. * a normal host controller.
  814. *
  815. * when urbs are queued, they're just stuck on a list that we
  816. * scan in a timer callback. that callback connects writes from
  817. * the host with reads from the device, and so on, based on the
  818. * usb 2.0 rules.
  819. */
  820. static int dummy_urb_enqueue (
  821. struct usb_hcd *hcd,
  822. struct urb *urb,
  823. gfp_t mem_flags
  824. ) {
  825. struct dummy *dum;
  826. struct urbp *urbp;
  827. unsigned long flags;
  828. int rc;
  829. if (!urb->transfer_buffer && urb->transfer_buffer_length)
  830. return -EINVAL;
  831. urbp = kmalloc (sizeof *urbp, mem_flags);
  832. if (!urbp)
  833. return -ENOMEM;
  834. urbp->urb = urb;
  835. dum = hcd_to_dummy (hcd);
  836. spin_lock_irqsave (&dum->lock, flags);
  837. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  838. if (rc) {
  839. kfree(urbp);
  840. goto done;
  841. }
  842. if (!dum->udev) {
  843. dum->udev = urb->dev;
  844. usb_get_dev (dum->udev);
  845. } else if (unlikely (dum->udev != urb->dev))
  846. dev_err (dummy_dev(dum), "usb_device address has changed!\n");
  847. list_add_tail (&urbp->urbp_list, &dum->urbp_list);
  848. urb->hcpriv = urbp;
  849. if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
  850. urb->error_count = 1; /* mark as a new urb */
  851. /* kick the scheduler, it'll do the rest */
  852. if (!timer_pending (&dum->timer))
  853. mod_timer (&dum->timer, jiffies + 1);
  854. done:
  855. spin_unlock_irqrestore(&dum->lock, flags);
  856. return rc;
  857. }
  858. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  859. {
  860. struct dummy *dum;
  861. unsigned long flags;
  862. int rc;
  863. /* giveback happens automatically in timer callback,
  864. * so make sure the callback happens */
  865. dum = hcd_to_dummy (hcd);
  866. spin_lock_irqsave (&dum->lock, flags);
  867. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  868. if (!rc && dum->rh_state != DUMMY_RH_RUNNING &&
  869. !list_empty(&dum->urbp_list))
  870. mod_timer (&dum->timer, jiffies);
  871. spin_unlock_irqrestore (&dum->lock, flags);
  872. return rc;
  873. }
  874. /* transfer up to a frame's worth; caller must own lock */
  875. static int
  876. transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
  877. int *status)
  878. {
  879. struct dummy_request *req;
  880. top:
  881. /* if there's no request queued, the device is NAKing; return */
  882. list_for_each_entry (req, &ep->queue, queue) {
  883. unsigned host_len, dev_len, len;
  884. int is_short, to_host;
  885. int rescan = 0;
  886. /* 1..N packets of ep->ep.maxpacket each ... the last one
  887. * may be short (including zero length).
  888. *
  889. * writer can send a zlp explicitly (length 0) or implicitly
  890. * (length mod maxpacket zero, and 'zero' flag); they always
  891. * terminate reads.
  892. */
  893. host_len = urb->transfer_buffer_length - urb->actual_length;
  894. dev_len = req->req.length - req->req.actual;
  895. len = min (host_len, dev_len);
  896. /* FIXME update emulated data toggle too */
  897. to_host = usb_pipein (urb->pipe);
  898. if (unlikely (len == 0))
  899. is_short = 1;
  900. else {
  901. char *ubuf, *rbuf;
  902. /* not enough bandwidth left? */
  903. if (limit < ep->ep.maxpacket && limit < len)
  904. break;
  905. len = min (len, (unsigned) limit);
  906. if (len == 0)
  907. break;
  908. /* use an extra pass for the final short packet */
  909. if (len > ep->ep.maxpacket) {
  910. rescan = 1;
  911. len -= (len % ep->ep.maxpacket);
  912. }
  913. is_short = (len % ep->ep.maxpacket) != 0;
  914. /* else transfer packet(s) */
  915. ubuf = urb->transfer_buffer + urb->actual_length;
  916. rbuf = req->req.buf + req->req.actual;
  917. if (to_host)
  918. memcpy (ubuf, rbuf, len);
  919. else
  920. memcpy (rbuf, ubuf, len);
  921. ep->last_io = jiffies;
  922. limit -= len;
  923. urb->actual_length += len;
  924. req->req.actual += len;
  925. }
  926. /* short packets terminate, maybe with overflow/underflow.
  927. * it's only really an error to write too much.
  928. *
  929. * partially filling a buffer optionally blocks queue advances
  930. * (so completion handlers can clean up the queue) but we don't
  931. * need to emulate such data-in-flight.
  932. */
  933. if (is_short) {
  934. if (host_len == dev_len) {
  935. req->req.status = 0;
  936. *status = 0;
  937. } else if (to_host) {
  938. req->req.status = 0;
  939. if (dev_len > host_len)
  940. *status = -EOVERFLOW;
  941. else
  942. *status = 0;
  943. } else if (!to_host) {
  944. *status = 0;
  945. if (host_len > dev_len)
  946. req->req.status = -EOVERFLOW;
  947. else
  948. req->req.status = 0;
  949. }
  950. /* many requests terminate without a short packet */
  951. } else {
  952. if (req->req.length == req->req.actual
  953. && !req->req.zero)
  954. req->req.status = 0;
  955. if (urb->transfer_buffer_length == urb->actual_length
  956. && !(urb->transfer_flags
  957. & URB_ZERO_PACKET))
  958. *status = 0;
  959. }
  960. /* device side completion --> continuable */
  961. if (req->req.status != -EINPROGRESS) {
  962. list_del_init (&req->queue);
  963. spin_unlock (&dum->lock);
  964. req->req.complete (&ep->ep, &req->req);
  965. spin_lock (&dum->lock);
  966. /* requests might have been unlinked... */
  967. rescan = 1;
  968. }
  969. /* host side completion --> terminate */
  970. if (*status != -EINPROGRESS)
  971. break;
  972. /* rescan to continue with any other queued i/o */
  973. if (rescan)
  974. goto top;
  975. }
  976. return limit;
  977. }
  978. static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
  979. {
  980. int limit = ep->ep.maxpacket;
  981. if (dum->gadget.speed == USB_SPEED_HIGH) {
  982. int tmp;
  983. /* high bandwidth mode */
  984. tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
  985. tmp = (tmp >> 11) & 0x03;
  986. tmp *= 8 /* applies to entire frame */;
  987. limit += limit * tmp;
  988. }
  989. return limit;
  990. }
  991. #define is_active(dum) ((dum->port_status & \
  992. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  993. USB_PORT_STAT_SUSPEND)) \
  994. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  995. static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
  996. {
  997. int i;
  998. if (!is_active (dum))
  999. return NULL;
  1000. if ((address & ~USB_DIR_IN) == 0)
  1001. return &dum->ep [0];
  1002. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  1003. struct dummy_ep *ep = &dum->ep [i];
  1004. if (!ep->desc)
  1005. continue;
  1006. if (ep->desc->bEndpointAddress == address)
  1007. return ep;
  1008. }
  1009. return NULL;
  1010. }
  1011. #undef is_active
  1012. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1013. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  1014. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  1015. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  1016. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  1017. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  1018. /**
  1019. * handle_control_request() - handles all control transfers
  1020. * @dum: pointer to dummy (the_controller)
  1021. * @urb: the urb request to handle
  1022. * @setup: pointer to the setup data for a USB device control
  1023. * request
  1024. * @status: pointer to request handling status
  1025. *
  1026. * Return 0 - if the request was handled
  1027. * 1 - if the request wasn't handles
  1028. * error code on error
  1029. */
  1030. static int handle_control_request(struct dummy *dum, struct urb *urb,
  1031. struct usb_ctrlrequest *setup,
  1032. int *status)
  1033. {
  1034. struct dummy_ep *ep2;
  1035. int ret_val = 1;
  1036. unsigned w_index;
  1037. unsigned w_value;
  1038. w_index = le16_to_cpu(setup->wIndex);
  1039. w_value = le16_to_cpu(setup->wValue);
  1040. switch (setup->bRequest) {
  1041. case USB_REQ_SET_ADDRESS:
  1042. if (setup->bRequestType != Dev_Request)
  1043. break;
  1044. dum->address = w_value;
  1045. *status = 0;
  1046. dev_dbg(udc_dev(dum), "set_address = %d\n",
  1047. w_value);
  1048. ret_val = 0;
  1049. break;
  1050. case USB_REQ_SET_FEATURE:
  1051. if (setup->bRequestType == Dev_Request) {
  1052. ret_val = 0;
  1053. switch (w_value) {
  1054. case USB_DEVICE_REMOTE_WAKEUP:
  1055. break;
  1056. case USB_DEVICE_B_HNP_ENABLE:
  1057. dum->gadget.b_hnp_enable = 1;
  1058. break;
  1059. case USB_DEVICE_A_HNP_SUPPORT:
  1060. dum->gadget.a_hnp_support = 1;
  1061. break;
  1062. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1063. dum->gadget.a_alt_hnp_support = 1;
  1064. break;
  1065. default:
  1066. ret_val = -EOPNOTSUPP;
  1067. }
  1068. if (ret_val == 0) {
  1069. dum->devstatus |= (1 << w_value);
  1070. *status = 0;
  1071. }
  1072. } else if (setup->bRequestType == Ep_Request) {
  1073. /* endpoint halt */
  1074. ep2 = find_endpoint(dum, w_index);
  1075. if (!ep2 || ep2->ep.name == ep0name) {
  1076. ret_val = -EOPNOTSUPP;
  1077. break;
  1078. }
  1079. ep2->halted = 1;
  1080. ret_val = 0;
  1081. *status = 0;
  1082. }
  1083. break;
  1084. case USB_REQ_CLEAR_FEATURE:
  1085. if (setup->bRequestType == Dev_Request) {
  1086. ret_val = 0;
  1087. switch (w_value) {
  1088. case USB_DEVICE_REMOTE_WAKEUP:
  1089. w_value = USB_DEVICE_REMOTE_WAKEUP;
  1090. break;
  1091. default:
  1092. ret_val = -EOPNOTSUPP;
  1093. break;
  1094. }
  1095. if (ret_val == 0) {
  1096. dum->devstatus &= ~(1 << w_value);
  1097. *status = 0;
  1098. }
  1099. } else if (setup->bRequestType == Ep_Request) {
  1100. /* endpoint halt */
  1101. ep2 = find_endpoint(dum, w_index);
  1102. if (!ep2) {
  1103. ret_val = -EOPNOTSUPP;
  1104. break;
  1105. }
  1106. if (!ep2->wedged)
  1107. ep2->halted = 0;
  1108. ret_val = 0;
  1109. *status = 0;
  1110. }
  1111. break;
  1112. case USB_REQ_GET_STATUS:
  1113. if (setup->bRequestType == Dev_InRequest
  1114. || setup->bRequestType == Intf_InRequest
  1115. || setup->bRequestType == Ep_InRequest) {
  1116. char *buf;
  1117. /*
  1118. * device: remote wakeup, selfpowered
  1119. * interface: nothing
  1120. * endpoint: halt
  1121. */
  1122. buf = (char *)urb->transfer_buffer;
  1123. if (urb->transfer_buffer_length > 0) {
  1124. if (setup->bRequestType == Ep_InRequest) {
  1125. ep2 = find_endpoint(dum, w_index);
  1126. if (!ep2) {
  1127. ret_val = -EOPNOTSUPP;
  1128. break;
  1129. }
  1130. buf[0] = ep2->halted;
  1131. } else if (setup->bRequestType ==
  1132. Dev_InRequest) {
  1133. buf[0] = (u8)dum->devstatus;
  1134. } else
  1135. buf[0] = 0;
  1136. }
  1137. if (urb->transfer_buffer_length > 1)
  1138. buf[1] = 0;
  1139. urb->actual_length = min_t(u32, 2,
  1140. urb->transfer_buffer_length);
  1141. ret_val = 0;
  1142. *status = 0;
  1143. }
  1144. break;
  1145. }
  1146. return ret_val;
  1147. }
  1148. /* drive both sides of the transfers; looks like irq handlers to
  1149. * both drivers except the callbacks aren't in_irq().
  1150. */
  1151. static void dummy_timer (unsigned long _dum)
  1152. {
  1153. struct dummy *dum = (struct dummy *) _dum;
  1154. struct urbp *urbp, *tmp;
  1155. unsigned long flags;
  1156. int limit, total;
  1157. int i;
  1158. /* simplistic model for one frame's bandwidth */
  1159. switch (dum->gadget.speed) {
  1160. case USB_SPEED_LOW:
  1161. total = 8/*bytes*/ * 12/*packets*/;
  1162. break;
  1163. case USB_SPEED_FULL:
  1164. total = 64/*bytes*/ * 19/*packets*/;
  1165. break;
  1166. case USB_SPEED_HIGH:
  1167. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1168. break;
  1169. default:
  1170. dev_err (dummy_dev(dum), "bogus device speed\n");
  1171. return;
  1172. }
  1173. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1174. /* look at each urb queued by the host side driver */
  1175. spin_lock_irqsave (&dum->lock, flags);
  1176. if (!dum->udev) {
  1177. dev_err (dummy_dev(dum),
  1178. "timer fired with no URBs pending?\n");
  1179. spin_unlock_irqrestore (&dum->lock, flags);
  1180. return;
  1181. }
  1182. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1183. if (!ep_name [i])
  1184. break;
  1185. dum->ep [i].already_seen = 0;
  1186. }
  1187. restart:
  1188. list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
  1189. struct urb *urb;
  1190. struct dummy_request *req;
  1191. u8 address;
  1192. struct dummy_ep *ep = NULL;
  1193. int type;
  1194. int status = -EINPROGRESS;
  1195. urb = urbp->urb;
  1196. if (urb->unlinked)
  1197. goto return_urb;
  1198. else if (dum->rh_state != DUMMY_RH_RUNNING)
  1199. continue;
  1200. type = usb_pipetype (urb->pipe);
  1201. /* used up this frame's non-periodic bandwidth?
  1202. * FIXME there's infinite bandwidth for control and
  1203. * periodic transfers ... unrealistic.
  1204. */
  1205. if (total <= 0 && type == PIPE_BULK)
  1206. continue;
  1207. /* find the gadget's ep for this request (if configured) */
  1208. address = usb_pipeendpoint (urb->pipe);
  1209. if (usb_pipein (urb->pipe))
  1210. address |= USB_DIR_IN;
  1211. ep = find_endpoint(dum, address);
  1212. if (!ep) {
  1213. /* set_configuration() disagreement */
  1214. dev_dbg (dummy_dev(dum),
  1215. "no ep configured for urb %p\n",
  1216. urb);
  1217. status = -EPROTO;
  1218. goto return_urb;
  1219. }
  1220. if (ep->already_seen)
  1221. continue;
  1222. ep->already_seen = 1;
  1223. if (ep == &dum->ep [0] && urb->error_count) {
  1224. ep->setup_stage = 1; /* a new urb */
  1225. urb->error_count = 0;
  1226. }
  1227. if (ep->halted && !ep->setup_stage) {
  1228. /* NOTE: must not be iso! */
  1229. dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
  1230. ep->ep.name, urb);
  1231. status = -EPIPE;
  1232. goto return_urb;
  1233. }
  1234. /* FIXME make sure both ends agree on maxpacket */
  1235. /* handle control requests */
  1236. if (ep == &dum->ep [0] && ep->setup_stage) {
  1237. struct usb_ctrlrequest setup;
  1238. int value = 1;
  1239. setup = *(struct usb_ctrlrequest*) urb->setup_packet;
  1240. /* paranoia, in case of stale queued data */
  1241. list_for_each_entry (req, &ep->queue, queue) {
  1242. list_del_init (&req->queue);
  1243. req->req.status = -EOVERFLOW;
  1244. dev_dbg (udc_dev(dum), "stale req = %p\n",
  1245. req);
  1246. spin_unlock (&dum->lock);
  1247. req->req.complete (&ep->ep, &req->req);
  1248. spin_lock (&dum->lock);
  1249. ep->already_seen = 0;
  1250. goto restart;
  1251. }
  1252. /* gadget driver never sees set_address or operations
  1253. * on standard feature flags. some hardware doesn't
  1254. * even expose them.
  1255. */
  1256. ep->last_io = jiffies;
  1257. ep->setup_stage = 0;
  1258. ep->halted = 0;
  1259. value = handle_control_request(dum, urb, &setup,
  1260. &status);
  1261. /* gadget driver handles all other requests. block
  1262. * until setup() returns; no reentrancy issues etc.
  1263. */
  1264. if (value > 0) {
  1265. spin_unlock (&dum->lock);
  1266. value = dum->driver->setup (&dum->gadget,
  1267. &setup);
  1268. spin_lock (&dum->lock);
  1269. if (value >= 0) {
  1270. /* no delays (max 64KB data stage) */
  1271. limit = 64*1024;
  1272. goto treat_control_like_bulk;
  1273. }
  1274. /* error, see below */
  1275. }
  1276. if (value < 0) {
  1277. if (value != -EOPNOTSUPP)
  1278. dev_dbg (udc_dev(dum),
  1279. "setup --> %d\n",
  1280. value);
  1281. status = -EPIPE;
  1282. urb->actual_length = 0;
  1283. }
  1284. goto return_urb;
  1285. }
  1286. /* non-control requests */
  1287. limit = total;
  1288. switch (usb_pipetype (urb->pipe)) {
  1289. case PIPE_ISOCHRONOUS:
  1290. /* FIXME is it urb->interval since the last xfer?
  1291. * use urb->iso_frame_desc[i].
  1292. * complete whether or not ep has requests queued.
  1293. * report random errors, to debug drivers.
  1294. */
  1295. limit = max (limit, periodic_bytes (dum, ep));
  1296. status = -ENOSYS;
  1297. break;
  1298. case PIPE_INTERRUPT:
  1299. /* FIXME is it urb->interval since the last xfer?
  1300. * this almost certainly polls too fast.
  1301. */
  1302. limit = max (limit, periodic_bytes (dum, ep));
  1303. /* FALLTHROUGH */
  1304. // case PIPE_BULK: case PIPE_CONTROL:
  1305. default:
  1306. treat_control_like_bulk:
  1307. ep->last_io = jiffies;
  1308. total = transfer(dum, urb, ep, limit, &status);
  1309. break;
  1310. }
  1311. /* incomplete transfer? */
  1312. if (status == -EINPROGRESS)
  1313. continue;
  1314. return_urb:
  1315. list_del (&urbp->urbp_list);
  1316. kfree (urbp);
  1317. if (ep)
  1318. ep->already_seen = ep->setup_stage = 0;
  1319. usb_hcd_unlink_urb_from_ep(dummy_to_hcd(dum), urb);
  1320. spin_unlock (&dum->lock);
  1321. usb_hcd_giveback_urb(dummy_to_hcd(dum), urb, status);
  1322. spin_lock (&dum->lock);
  1323. goto restart;
  1324. }
  1325. if (list_empty (&dum->urbp_list)) {
  1326. usb_put_dev (dum->udev);
  1327. dum->udev = NULL;
  1328. } else if (dum->rh_state == DUMMY_RH_RUNNING) {
  1329. /* want a 1 msec delay here */
  1330. mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
  1331. }
  1332. spin_unlock_irqrestore (&dum->lock, flags);
  1333. }
  1334. /*-------------------------------------------------------------------------*/
  1335. #define PORT_C_MASK \
  1336. ((USB_PORT_STAT_C_CONNECTION \
  1337. | USB_PORT_STAT_C_ENABLE \
  1338. | USB_PORT_STAT_C_SUSPEND \
  1339. | USB_PORT_STAT_C_OVERCURRENT \
  1340. | USB_PORT_STAT_C_RESET) << 16)
  1341. static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
  1342. {
  1343. struct dummy *dum;
  1344. unsigned long flags;
  1345. int retval = 0;
  1346. dum = hcd_to_dummy (hcd);
  1347. spin_lock_irqsave (&dum->lock, flags);
  1348. if (!HCD_HW_ACCESSIBLE(hcd))
  1349. goto done;
  1350. if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
  1351. dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1352. dum->port_status &= ~USB_PORT_STAT_SUSPEND;
  1353. set_link_state (dum);
  1354. }
  1355. if ((dum->port_status & PORT_C_MASK) != 0) {
  1356. *buf = (1 << 1);
  1357. dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
  1358. dum->port_status);
  1359. retval = 1;
  1360. if (dum->rh_state == DUMMY_RH_SUSPENDED)
  1361. usb_hcd_resume_root_hub (hcd);
  1362. }
  1363. done:
  1364. spin_unlock_irqrestore (&dum->lock, flags);
  1365. return retval;
  1366. }
  1367. static inline void
  1368. hub_descriptor (struct usb_hub_descriptor *desc)
  1369. {
  1370. memset (desc, 0, sizeof *desc);
  1371. desc->bDescriptorType = 0x29;
  1372. desc->bDescLength = 9;
  1373. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1374. desc->bNbrPorts = 1;
  1375. desc->u.hs.DeviceRemovable[0] = 0xff;
  1376. desc->u.hs.DeviceRemovable[1] = 0xff;
  1377. }
  1378. static int dummy_hub_control (
  1379. struct usb_hcd *hcd,
  1380. u16 typeReq,
  1381. u16 wValue,
  1382. u16 wIndex,
  1383. char *buf,
  1384. u16 wLength
  1385. ) {
  1386. struct dummy *dum;
  1387. int retval = 0;
  1388. unsigned long flags;
  1389. if (!HCD_HW_ACCESSIBLE(hcd))
  1390. return -ETIMEDOUT;
  1391. dum = hcd_to_dummy (hcd);
  1392. spin_lock_irqsave (&dum->lock, flags);
  1393. switch (typeReq) {
  1394. case ClearHubFeature:
  1395. break;
  1396. case ClearPortFeature:
  1397. switch (wValue) {
  1398. case USB_PORT_FEAT_SUSPEND:
  1399. if (dum->port_status & USB_PORT_STAT_SUSPEND) {
  1400. /* 20msec resume signaling */
  1401. dum->resuming = 1;
  1402. dum->re_timeout = jiffies +
  1403. msecs_to_jiffies(20);
  1404. }
  1405. break;
  1406. case USB_PORT_FEAT_POWER:
  1407. if (dum->port_status & USB_PORT_STAT_POWER)
  1408. dev_dbg (dummy_dev(dum), "power-off\n");
  1409. /* FALLS THROUGH */
  1410. default:
  1411. dum->port_status &= ~(1 << wValue);
  1412. set_link_state (dum);
  1413. }
  1414. break;
  1415. case GetHubDescriptor:
  1416. hub_descriptor ((struct usb_hub_descriptor *) buf);
  1417. break;
  1418. case GetHubStatus:
  1419. *(__le32 *) buf = cpu_to_le32 (0);
  1420. break;
  1421. case GetPortStatus:
  1422. if (wIndex != 1)
  1423. retval = -EPIPE;
  1424. /* whoever resets or resumes must GetPortStatus to
  1425. * complete it!!
  1426. */
  1427. if (dum->resuming &&
  1428. time_after_eq (jiffies, dum->re_timeout)) {
  1429. dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1430. dum->port_status &= ~USB_PORT_STAT_SUSPEND;
  1431. }
  1432. if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
  1433. time_after_eq (jiffies, dum->re_timeout)) {
  1434. dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
  1435. dum->port_status &= ~USB_PORT_STAT_RESET;
  1436. if (dum->pullup) {
  1437. dum->port_status |= USB_PORT_STAT_ENABLE;
  1438. /* give it the best speed we agree on */
  1439. dum->gadget.speed = dum->driver->speed;
  1440. dum->gadget.ep0->maxpacket = 64;
  1441. switch (dum->gadget.speed) {
  1442. case USB_SPEED_HIGH:
  1443. dum->port_status |=
  1444. USB_PORT_STAT_HIGH_SPEED;
  1445. break;
  1446. case USB_SPEED_LOW:
  1447. dum->gadget.ep0->maxpacket = 8;
  1448. dum->port_status |=
  1449. USB_PORT_STAT_LOW_SPEED;
  1450. break;
  1451. default:
  1452. dum->gadget.speed = USB_SPEED_FULL;
  1453. break;
  1454. }
  1455. }
  1456. }
  1457. set_link_state (dum);
  1458. ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
  1459. ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
  1460. break;
  1461. case SetHubFeature:
  1462. retval = -EPIPE;
  1463. break;
  1464. case SetPortFeature:
  1465. switch (wValue) {
  1466. case USB_PORT_FEAT_SUSPEND:
  1467. if (dum->active) {
  1468. dum->port_status |= USB_PORT_STAT_SUSPEND;
  1469. /* HNP would happen here; for now we
  1470. * assume b_bus_req is always true.
  1471. */
  1472. set_link_state (dum);
  1473. if (((1 << USB_DEVICE_B_HNP_ENABLE)
  1474. & dum->devstatus) != 0)
  1475. dev_dbg (dummy_dev(dum),
  1476. "no HNP yet!\n");
  1477. }
  1478. break;
  1479. case USB_PORT_FEAT_POWER:
  1480. dum->port_status |= USB_PORT_STAT_POWER;
  1481. set_link_state (dum);
  1482. break;
  1483. case USB_PORT_FEAT_RESET:
  1484. /* if it's already enabled, disable */
  1485. dum->port_status &= ~(USB_PORT_STAT_ENABLE
  1486. | USB_PORT_STAT_LOW_SPEED
  1487. | USB_PORT_STAT_HIGH_SPEED);
  1488. dum->devstatus = 0;
  1489. /* 50msec reset signaling */
  1490. dum->re_timeout = jiffies + msecs_to_jiffies(50);
  1491. /* FALLS THROUGH */
  1492. default:
  1493. if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
  1494. dum->port_status |= (1 << wValue);
  1495. set_link_state (dum);
  1496. }
  1497. }
  1498. break;
  1499. default:
  1500. dev_dbg (dummy_dev(dum),
  1501. "hub control req%04x v%04x i%04x l%d\n",
  1502. typeReq, wValue, wIndex, wLength);
  1503. /* "protocol stall" on error */
  1504. retval = -EPIPE;
  1505. }
  1506. spin_unlock_irqrestore (&dum->lock, flags);
  1507. if ((dum->port_status & PORT_C_MASK) != 0)
  1508. usb_hcd_poll_rh_status (hcd);
  1509. return retval;
  1510. }
  1511. static int dummy_bus_suspend (struct usb_hcd *hcd)
  1512. {
  1513. struct dummy *dum = hcd_to_dummy (hcd);
  1514. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1515. spin_lock_irq (&dum->lock);
  1516. dum->rh_state = DUMMY_RH_SUSPENDED;
  1517. set_link_state (dum);
  1518. hcd->state = HC_STATE_SUSPENDED;
  1519. spin_unlock_irq (&dum->lock);
  1520. return 0;
  1521. }
  1522. static int dummy_bus_resume (struct usb_hcd *hcd)
  1523. {
  1524. struct dummy *dum = hcd_to_dummy (hcd);
  1525. int rc = 0;
  1526. dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
  1527. spin_lock_irq (&dum->lock);
  1528. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1529. rc = -ESHUTDOWN;
  1530. } else {
  1531. dum->rh_state = DUMMY_RH_RUNNING;
  1532. set_link_state (dum);
  1533. if (!list_empty(&dum->urbp_list))
  1534. mod_timer (&dum->timer, jiffies);
  1535. hcd->state = HC_STATE_RUNNING;
  1536. }
  1537. spin_unlock_irq (&dum->lock);
  1538. return rc;
  1539. }
  1540. /*-------------------------------------------------------------------------*/
  1541. static inline ssize_t
  1542. show_urb (char *buf, size_t size, struct urb *urb)
  1543. {
  1544. int ep = usb_pipeendpoint (urb->pipe);
  1545. return snprintf (buf, size,
  1546. "urb/%p %s ep%d%s%s len %d/%d\n",
  1547. urb,
  1548. ({ char *s;
  1549. switch (urb->dev->speed) {
  1550. case USB_SPEED_LOW: s = "ls"; break;
  1551. case USB_SPEED_FULL: s = "fs"; break;
  1552. case USB_SPEED_HIGH: s = "hs"; break;
  1553. default: s = "?"; break;
  1554. }; s; }),
  1555. ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
  1556. ({ char *s; \
  1557. switch (usb_pipetype (urb->pipe)) { \
  1558. case PIPE_CONTROL: s = ""; break; \
  1559. case PIPE_BULK: s = "-bulk"; break; \
  1560. case PIPE_INTERRUPT: s = "-int"; break; \
  1561. default: s = "-iso"; break; \
  1562. }; s;}),
  1563. urb->actual_length, urb->transfer_buffer_length);
  1564. }
  1565. static ssize_t
  1566. show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
  1567. {
  1568. struct usb_hcd *hcd = dev_get_drvdata (dev);
  1569. struct dummy *dum = hcd_to_dummy (hcd);
  1570. struct urbp *urbp;
  1571. size_t size = 0;
  1572. unsigned long flags;
  1573. spin_lock_irqsave (&dum->lock, flags);
  1574. list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
  1575. size_t temp;
  1576. temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
  1577. buf += temp;
  1578. size += temp;
  1579. }
  1580. spin_unlock_irqrestore (&dum->lock, flags);
  1581. return size;
  1582. }
  1583. static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
  1584. static int dummy_start (struct usb_hcd *hcd)
  1585. {
  1586. struct dummy *dum;
  1587. dum = hcd_to_dummy (hcd);
  1588. /*
  1589. * MASTER side init ... we emulate a root hub that'll only ever
  1590. * talk to one device (the slave side). Also appears in sysfs,
  1591. * just like more familiar pci-based HCDs.
  1592. */
  1593. spin_lock_init (&dum->lock);
  1594. init_timer (&dum->timer);
  1595. dum->timer.function = dummy_timer;
  1596. dum->timer.data = (unsigned long) dum;
  1597. dum->rh_state = DUMMY_RH_RUNNING;
  1598. INIT_LIST_HEAD (&dum->urbp_list);
  1599. hcd->power_budget = POWER_BUDGET;
  1600. hcd->state = HC_STATE_RUNNING;
  1601. hcd->uses_new_polling = 1;
  1602. #ifdef CONFIG_USB_OTG
  1603. hcd->self.otg_port = 1;
  1604. #endif
  1605. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  1606. return device_create_file (dummy_dev(dum), &dev_attr_urbs);
  1607. }
  1608. static void dummy_stop (struct usb_hcd *hcd)
  1609. {
  1610. struct dummy *dum;
  1611. dum = hcd_to_dummy (hcd);
  1612. device_remove_file (dummy_dev(dum), &dev_attr_urbs);
  1613. dev_info (dummy_dev(dum), "stopped\n");
  1614. }
  1615. /*-------------------------------------------------------------------------*/
  1616. static int dummy_h_get_frame (struct usb_hcd *hcd)
  1617. {
  1618. return dummy_g_get_frame (NULL);
  1619. }
  1620. static const struct hc_driver dummy_hcd = {
  1621. .description = (char *) driver_name,
  1622. .product_desc = "Dummy host controller",
  1623. .hcd_priv_size = sizeof(struct dummy),
  1624. .flags = HCD_USB2,
  1625. .start = dummy_start,
  1626. .stop = dummy_stop,
  1627. .urb_enqueue = dummy_urb_enqueue,
  1628. .urb_dequeue = dummy_urb_dequeue,
  1629. .get_frame_number = dummy_h_get_frame,
  1630. .hub_status_data = dummy_hub_status,
  1631. .hub_control = dummy_hub_control,
  1632. .bus_suspend = dummy_bus_suspend,
  1633. .bus_resume = dummy_bus_resume,
  1634. };
  1635. static int dummy_hcd_probe(struct platform_device *pdev)
  1636. {
  1637. struct usb_hcd *hcd;
  1638. int retval;
  1639. dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
  1640. hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
  1641. if (!hcd)
  1642. return -ENOMEM;
  1643. the_controller = hcd_to_dummy (hcd);
  1644. hcd->has_tt = 1;
  1645. retval = usb_add_hcd(hcd, 0, 0);
  1646. if (retval != 0) {
  1647. usb_put_hcd (hcd);
  1648. the_controller = NULL;
  1649. }
  1650. return retval;
  1651. }
  1652. static int dummy_hcd_remove (struct platform_device *pdev)
  1653. {
  1654. struct usb_hcd *hcd;
  1655. hcd = platform_get_drvdata (pdev);
  1656. usb_remove_hcd (hcd);
  1657. usb_put_hcd (hcd);
  1658. the_controller = NULL;
  1659. return 0;
  1660. }
  1661. static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
  1662. {
  1663. struct usb_hcd *hcd;
  1664. struct dummy *dum;
  1665. int rc = 0;
  1666. dev_dbg (&pdev->dev, "%s\n", __func__);
  1667. hcd = platform_get_drvdata (pdev);
  1668. dum = hcd_to_dummy (hcd);
  1669. if (dum->rh_state == DUMMY_RH_RUNNING) {
  1670. dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
  1671. rc = -EBUSY;
  1672. } else
  1673. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1674. return rc;
  1675. }
  1676. static int dummy_hcd_resume (struct platform_device *pdev)
  1677. {
  1678. struct usb_hcd *hcd;
  1679. dev_dbg (&pdev->dev, "%s\n", __func__);
  1680. hcd = platform_get_drvdata (pdev);
  1681. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1682. usb_hcd_poll_rh_status (hcd);
  1683. return 0;
  1684. }
  1685. static struct platform_driver dummy_hcd_driver = {
  1686. .probe = dummy_hcd_probe,
  1687. .remove = dummy_hcd_remove,
  1688. .suspend = dummy_hcd_suspend,
  1689. .resume = dummy_hcd_resume,
  1690. .driver = {
  1691. .name = (char *) driver_name,
  1692. .owner = THIS_MODULE,
  1693. },
  1694. };
  1695. /*-------------------------------------------------------------------------*/
  1696. static struct platform_device *the_udc_pdev;
  1697. static struct platform_device *the_hcd_pdev;
  1698. static int __init init (void)
  1699. {
  1700. int retval = -ENOMEM;
  1701. if (usb_disabled ())
  1702. return -ENODEV;
  1703. the_hcd_pdev = platform_device_alloc(driver_name, -1);
  1704. if (!the_hcd_pdev)
  1705. return retval;
  1706. the_udc_pdev = platform_device_alloc(gadget_name, -1);
  1707. if (!the_udc_pdev)
  1708. goto err_alloc_udc;
  1709. retval = platform_driver_register(&dummy_hcd_driver);
  1710. if (retval < 0)
  1711. goto err_register_hcd_driver;
  1712. retval = platform_driver_register(&dummy_udc_driver);
  1713. if (retval < 0)
  1714. goto err_register_udc_driver;
  1715. retval = platform_device_add(the_hcd_pdev);
  1716. if (retval < 0)
  1717. goto err_add_hcd;
  1718. if (!the_controller) {
  1719. /*
  1720. * The hcd was added successfully but its probe function failed
  1721. * for some reason.
  1722. */
  1723. retval = -EINVAL;
  1724. goto err_add_udc;
  1725. }
  1726. retval = platform_device_add(the_udc_pdev);
  1727. if (retval < 0)
  1728. goto err_add_udc;
  1729. if (!platform_get_drvdata(the_udc_pdev)) {
  1730. /*
  1731. * The udc was added successfully but its probe function failed
  1732. * for some reason.
  1733. */
  1734. retval = -EINVAL;
  1735. goto err_probe_udc;
  1736. }
  1737. return retval;
  1738. err_probe_udc:
  1739. platform_device_del(the_udc_pdev);
  1740. err_add_udc:
  1741. platform_device_del(the_hcd_pdev);
  1742. err_add_hcd:
  1743. platform_driver_unregister(&dummy_udc_driver);
  1744. err_register_udc_driver:
  1745. platform_driver_unregister(&dummy_hcd_driver);
  1746. err_register_hcd_driver:
  1747. platform_device_put(the_udc_pdev);
  1748. err_alloc_udc:
  1749. platform_device_put(the_hcd_pdev);
  1750. return retval;
  1751. }
  1752. module_init (init);
  1753. static void __exit cleanup (void)
  1754. {
  1755. platform_device_unregister(the_udc_pdev);
  1756. platform_device_unregister(the_hcd_pdev);
  1757. platform_driver_unregister(&dummy_udc_driver);
  1758. platform_driver_unregister(&dummy_hcd_driver);
  1759. }
  1760. module_exit (cleanup);