dummy_hcd.c 50 KB

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