dummy_hcd.c 49 KB

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