dummy_hcd.c 43 KB

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