dummy_hcd.c 51 KB

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