dummy_hcd.c 50 KB

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