dummy_hcd.c 47 KB

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