zero.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * zero.c -- Gadget Zero, for USB development
  3. *
  4. * Copyright (C) 2003-2007 David Brownell
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * Gadget Zero only needs two bulk endpoints, and is an example of how you
  23. * can write a hardware-agnostic gadget driver running inside a USB device.
  24. * Some hardware details are visible, but don't affect most of the driver.
  25. *
  26. * Use it with the Linux host/master side "usbtest" driver to get a basic
  27. * functional test of your device-side usb stack, or with "usb-skeleton".
  28. *
  29. * It supports two similar configurations. One sinks whatever the usb host
  30. * writes, and in return sources zeroes. The other loops whatever the host
  31. * writes back, so the host can read it. Module options include:
  32. *
  33. * buflen=N default N=4096, buffer size used
  34. * qlen=N default N=32, how many buffers in the loopback queue
  35. * loopdefault default false, list loopback config first
  36. * autoresume=N default N=0, seconds before triggering remote wakeup
  37. *
  38. * Many drivers will only have one configuration, letting them be much
  39. * simpler if they also don't support high speed operation (like this
  40. * driver does).
  41. *
  42. * Why is *this* driver using two configurations, rather than setting up
  43. * two interfaces with different functions? To help verify that multiple
  44. * configuration infrastucture is working correctly; also, so that it can
  45. * work with low capability USB controllers without four bulk endpoints.
  46. */
  47. /* #define VERBOSE_DEBUG */
  48. #include <linux/kernel.h>
  49. #include <linux/utsname.h>
  50. #include <linux/device.h>
  51. #include <linux/usb/ch9.h>
  52. #include <linux/usb/gadget.h>
  53. #include "gadget_chips.h"
  54. /*-------------------------------------------------------------------------*/
  55. #define DRIVER_VERSION "Earth Day 2008"
  56. static const char shortname[] = "zero";
  57. static const char longname[] = "Gadget Zero";
  58. static const char source_sink[] = "source and sink data";
  59. static const char loopback[] = "loop input to output";
  60. /*-------------------------------------------------------------------------*/
  61. /*
  62. * driver assumes self-powered hardware, and
  63. * has no way for users to trigger remote wakeup.
  64. *
  65. * this version autoconfigures as much as possible,
  66. * which is reasonable for most "bulk-only" drivers.
  67. */
  68. static const char *EP_IN_NAME; /* source */
  69. static const char *EP_OUT_NAME; /* sink */
  70. /*-------------------------------------------------------------------------*/
  71. /* big enough to hold our biggest descriptor */
  72. #define USB_BUFSIZ 256
  73. struct zero_dev {
  74. spinlock_t lock;
  75. struct usb_gadget *gadget;
  76. struct usb_request *req; /* for control responses */
  77. /* when configured, we have one of two configs:
  78. * - source data (in to host) and sink it (out from host)
  79. * - or loop it back (out from host back in to host)
  80. */
  81. u8 config;
  82. struct usb_ep *in_ep, *out_ep;
  83. /* autoresume timer */
  84. struct timer_list resume;
  85. };
  86. #define DBG(d, fmt, args...) \
  87. dev_dbg(&(d)->gadget->dev , fmt , ## args)
  88. #define VDBG(d, fmt, args...) \
  89. dev_vdbg(&(d)->gadget->dev , fmt , ## args)
  90. #define ERROR(d, fmt, args...) \
  91. dev_err(&(d)->gadget->dev , fmt , ## args)
  92. #define WARN(d, fmt, args...) \
  93. dev_warn(&(d)->gadget->dev , fmt , ## args)
  94. #define INFO(d, fmt, args...) \
  95. dev_info(&(d)->gadget->dev , fmt , ## args)
  96. /*-------------------------------------------------------------------------*/
  97. static unsigned buflen = 4096;
  98. static unsigned qlen = 32;
  99. static unsigned pattern = 0;
  100. module_param(buflen, uint, S_IRUGO);
  101. module_param(qlen, uint, S_IRUGO);
  102. module_param(pattern, uint, S_IRUGO|S_IWUSR);
  103. /*
  104. * if it's nonzero, autoresume says how many seconds to wait
  105. * before trying to wake up the host after suspend.
  106. */
  107. static unsigned autoresume = 0;
  108. module_param(autoresume, uint, 0);
  109. /*
  110. * Normally the "loopback" configuration is second (index 1) so
  111. * it's not the default. Here's where to change that order, to
  112. * work better with hosts where config changes are problematic.
  113. * Or controllers (like superh) that only support one config.
  114. */
  115. static int loopdefault = 0;
  116. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  117. /*-------------------------------------------------------------------------*/
  118. /* Thanks to NetChip Technologies for donating this product ID.
  119. *
  120. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  121. * Instead: allocate your own, using normal USB-IF procedures.
  122. */
  123. #ifndef CONFIG_USB_ZERO_HNPTEST
  124. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  125. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  126. #else
  127. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  128. #define DRIVER_PRODUCT_NUM 0xbadd
  129. #endif
  130. /*-------------------------------------------------------------------------*/
  131. /*
  132. * DESCRIPTORS ... most are static, but strings and (full)
  133. * configuration descriptors are built on demand.
  134. */
  135. #define STRING_MANUFACTURER 25
  136. #define STRING_PRODUCT 42
  137. #define STRING_SERIAL 101
  138. #define STRING_SOURCE_SINK 250
  139. #define STRING_LOOPBACK 251
  140. /*
  141. * This device advertises two configurations; these numbers work
  142. * on a pxa250 as well as more flexible hardware.
  143. */
  144. #define CONFIG_SOURCE_SINK 3
  145. #define CONFIG_LOOPBACK 2
  146. static struct usb_device_descriptor device_desc = {
  147. .bLength = sizeof device_desc,
  148. .bDescriptorType = USB_DT_DEVICE,
  149. .bcdUSB = __constant_cpu_to_le16(0x0200),
  150. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  151. .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
  152. .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
  153. .iManufacturer = STRING_MANUFACTURER,
  154. .iProduct = STRING_PRODUCT,
  155. .iSerialNumber = STRING_SERIAL,
  156. .bNumConfigurations = 2,
  157. };
  158. static struct usb_config_descriptor source_sink_config = {
  159. .bLength = sizeof source_sink_config,
  160. .bDescriptorType = USB_DT_CONFIG,
  161. /* compute wTotalLength on the fly */
  162. .bNumInterfaces = 1,
  163. .bConfigurationValue = CONFIG_SOURCE_SINK,
  164. .iConfiguration = STRING_SOURCE_SINK,
  165. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  166. .bMaxPower = 1, /* self-powered */
  167. };
  168. static struct usb_config_descriptor loopback_config = {
  169. .bLength = sizeof loopback_config,
  170. .bDescriptorType = USB_DT_CONFIG,
  171. /* compute wTotalLength on the fly */
  172. .bNumInterfaces = 1,
  173. .bConfigurationValue = CONFIG_LOOPBACK,
  174. .iConfiguration = STRING_LOOPBACK,
  175. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  176. .bMaxPower = 1, /* self-powered */
  177. };
  178. static struct usb_otg_descriptor otg_descriptor = {
  179. .bLength = sizeof otg_descriptor,
  180. .bDescriptorType = USB_DT_OTG,
  181. .bmAttributes = USB_OTG_SRP,
  182. };
  183. /* one interface in each configuration */
  184. static const struct usb_interface_descriptor source_sink_intf = {
  185. .bLength = sizeof source_sink_intf,
  186. .bDescriptorType = USB_DT_INTERFACE,
  187. .bNumEndpoints = 2,
  188. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  189. .iInterface = STRING_SOURCE_SINK,
  190. };
  191. static const struct usb_interface_descriptor loopback_intf = {
  192. .bLength = sizeof loopback_intf,
  193. .bDescriptorType = USB_DT_INTERFACE,
  194. .bNumEndpoints = 2,
  195. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  196. .iInterface = STRING_LOOPBACK,
  197. };
  198. /* two full speed bulk endpoints; their use is config-dependent */
  199. static struct usb_endpoint_descriptor fs_source_desc = {
  200. .bLength = USB_DT_ENDPOINT_SIZE,
  201. .bDescriptorType = USB_DT_ENDPOINT,
  202. .bEndpointAddress = USB_DIR_IN,
  203. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  204. };
  205. static struct usb_endpoint_descriptor fs_sink_desc = {
  206. .bLength = USB_DT_ENDPOINT_SIZE,
  207. .bDescriptorType = USB_DT_ENDPOINT,
  208. .bEndpointAddress = USB_DIR_OUT,
  209. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  210. };
  211. static const struct usb_descriptor_header *fs_source_sink_function[] = {
  212. (struct usb_descriptor_header *) &otg_descriptor,
  213. (struct usb_descriptor_header *) &source_sink_intf,
  214. (struct usb_descriptor_header *) &fs_sink_desc,
  215. (struct usb_descriptor_header *) &fs_source_desc,
  216. NULL,
  217. };
  218. static const struct usb_descriptor_header *fs_loopback_function[] = {
  219. (struct usb_descriptor_header *) &otg_descriptor,
  220. (struct usb_descriptor_header *) &loopback_intf,
  221. (struct usb_descriptor_header *) &fs_sink_desc,
  222. (struct usb_descriptor_header *) &fs_source_desc,
  223. NULL,
  224. };
  225. /*
  226. * usb 2.0 devices need to expose both high speed and full speed
  227. * descriptors, unless they only run at full speed.
  228. *
  229. * that means alternate endpoint descriptors (bigger packets)
  230. * and a "device qualifier" ... plus more construction options
  231. * for the config descriptor.
  232. */
  233. static struct usb_endpoint_descriptor hs_source_desc = {
  234. .bLength = USB_DT_ENDPOINT_SIZE,
  235. .bDescriptorType = USB_DT_ENDPOINT,
  236. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  237. .wMaxPacketSize = __constant_cpu_to_le16(512),
  238. };
  239. static struct usb_endpoint_descriptor hs_sink_desc = {
  240. .bLength = USB_DT_ENDPOINT_SIZE,
  241. .bDescriptorType = USB_DT_ENDPOINT,
  242. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  243. .wMaxPacketSize = __constant_cpu_to_le16(512),
  244. };
  245. static struct usb_qualifier_descriptor dev_qualifier = {
  246. .bLength = sizeof dev_qualifier,
  247. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  248. .bcdUSB = __constant_cpu_to_le16(0x0200),
  249. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  250. .bNumConfigurations = 2,
  251. };
  252. static const struct usb_descriptor_header *hs_source_sink_function[] = {
  253. (struct usb_descriptor_header *) &otg_descriptor,
  254. (struct usb_descriptor_header *) &source_sink_intf,
  255. (struct usb_descriptor_header *) &hs_source_desc,
  256. (struct usb_descriptor_header *) &hs_sink_desc,
  257. NULL,
  258. };
  259. static const struct usb_descriptor_header *hs_loopback_function[] = {
  260. (struct usb_descriptor_header *) &otg_descriptor,
  261. (struct usb_descriptor_header *) &loopback_intf,
  262. (struct usb_descriptor_header *) &hs_source_desc,
  263. (struct usb_descriptor_header *) &hs_sink_desc,
  264. NULL,
  265. };
  266. /* maxpacket and other transfer characteristics vary by speed. */
  267. static inline struct usb_endpoint_descriptor *
  268. ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
  269. struct usb_endpoint_descriptor *fs)
  270. {
  271. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  272. return hs;
  273. return fs;
  274. }
  275. static char manufacturer[50];
  276. /* default serial number takes at least two packets */
  277. static char serial[] = "0123456789.0123456789.0123456789";
  278. /* static strings, in UTF-8 */
  279. static struct usb_string strings[] = {
  280. { STRING_MANUFACTURER, manufacturer, },
  281. { STRING_PRODUCT, longname, },
  282. { STRING_SERIAL, serial, },
  283. { STRING_LOOPBACK, loopback, },
  284. { STRING_SOURCE_SINK, source_sink, },
  285. { } /* end of list */
  286. };
  287. static struct usb_gadget_strings stringtab = {
  288. .language = 0x0409, /* en-us */
  289. .strings = strings,
  290. };
  291. /*
  292. * config descriptors are also handcrafted. these must agree with code
  293. * that sets configurations, and with code managing interfaces and their
  294. * altsettings. other complexity may come from:
  295. *
  296. * - high speed support, including "other speed config" rules
  297. * - multiple configurations
  298. * - interfaces with alternate settings
  299. * - embedded class or vendor-specific descriptors
  300. *
  301. * this handles high speed, and has a second config that could as easily
  302. * have been an alternate interface setting (on most hardware).
  303. *
  304. * NOTE: to demonstrate (and test) more USB capabilities, this driver
  305. * should include an altsetting to test interrupt transfers, including
  306. * high bandwidth modes at high speed. (Maybe work like Intel's test
  307. * device?)
  308. */
  309. static int config_buf(struct usb_gadget *gadget,
  310. u8 *buf, u8 type, unsigned index)
  311. {
  312. int is_source_sink;
  313. int len;
  314. const struct usb_descriptor_header **function;
  315. int hs = 0;
  316. /* two configurations will always be index 0 and index 1 */
  317. if (index > 1)
  318. return -EINVAL;
  319. is_source_sink = loopdefault ? (index == 1) : (index == 0);
  320. if (gadget_is_dualspeed(gadget)) {
  321. hs = (gadget->speed == USB_SPEED_HIGH);
  322. if (type == USB_DT_OTHER_SPEED_CONFIG)
  323. hs = !hs;
  324. }
  325. if (hs)
  326. function = is_source_sink
  327. ? hs_source_sink_function
  328. : hs_loopback_function;
  329. else
  330. function = is_source_sink
  331. ? fs_source_sink_function
  332. : fs_loopback_function;
  333. /* for now, don't advertise srp-only devices */
  334. if (!gadget_is_otg(gadget))
  335. function++;
  336. len = usb_gadget_config_buf(is_source_sink
  337. ? &source_sink_config
  338. : &loopback_config,
  339. buf, USB_BUFSIZ, function);
  340. if (len < 0)
  341. return len;
  342. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  343. return len;
  344. }
  345. /*-------------------------------------------------------------------------*/
  346. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  347. {
  348. struct usb_request *req;
  349. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  350. if (req) {
  351. req->length = length;
  352. req->buf = kmalloc(length, GFP_ATOMIC);
  353. if (!req->buf) {
  354. usb_ep_free_request(ep, req);
  355. req = NULL;
  356. }
  357. }
  358. return req;
  359. }
  360. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  361. {
  362. kfree(req->buf);
  363. usb_ep_free_request(ep, req);
  364. }
  365. /*-------------------------------------------------------------------------*/
  366. /*
  367. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripherals,
  368. * this just sinks bulk packets OUT to the peripheral and sources them IN
  369. * to the host, optionally with specific data patterns.
  370. *
  371. * In terms of control messaging, this supports all the standard requests
  372. * plus two that support control-OUT tests.
  373. *
  374. * Note that because this doesn't queue more than one request at a time,
  375. * some other function must be used to test queueing logic. The network
  376. * link (g_ether) is probably the best option for that.
  377. */
  378. /* optionally require specific source/sink data patterns */
  379. static int
  380. check_read_data(
  381. struct zero_dev *dev,
  382. struct usb_ep *ep,
  383. struct usb_request *req
  384. )
  385. {
  386. unsigned i;
  387. u8 *buf = req->buf;
  388. for (i = 0; i < req->actual; i++, buf++) {
  389. switch (pattern) {
  390. /* all-zeroes has no synchronization issues */
  391. case 0:
  392. if (*buf == 0)
  393. continue;
  394. break;
  395. /* mod63 stays in sync with short-terminated transfers,
  396. * or otherwise when host and gadget agree on how large
  397. * each usb transfer request should be. resync is done
  398. * with set_interface or set_config.
  399. */
  400. case 1:
  401. if (*buf == (u8)(i % 63))
  402. continue;
  403. break;
  404. }
  405. ERROR(dev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  406. usb_ep_set_halt(ep);
  407. return -EINVAL;
  408. }
  409. return 0;
  410. }
  411. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  412. {
  413. unsigned i;
  414. u8 *buf = req->buf;
  415. switch (pattern) {
  416. case 0:
  417. memset(req->buf, 0, req->length);
  418. break;
  419. case 1:
  420. for (i = 0; i < req->length; i++)
  421. *buf++ = (u8) (i % 63);
  422. break;
  423. }
  424. }
  425. /* if there is only one request in the queue, there'll always be an
  426. * irq delay between end of one request and start of the next.
  427. * that prevents using hardware dma queues.
  428. */
  429. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  430. {
  431. struct zero_dev *dev = ep->driver_data;
  432. int status = req->status;
  433. switch (status) {
  434. case 0: /* normal completion? */
  435. if (ep == dev->out_ep) {
  436. check_read_data(dev, ep, req);
  437. memset(req->buf, 0x55, req->length);
  438. } else
  439. reinit_write_data(ep, req);
  440. break;
  441. /* this endpoint is normally active while we're configured */
  442. case -ECONNABORTED: /* hardware forced ep reset */
  443. case -ECONNRESET: /* request dequeued */
  444. case -ESHUTDOWN: /* disconnect from host */
  445. VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
  446. req->actual, req->length);
  447. if (ep == dev->out_ep)
  448. check_read_data(dev, ep, req);
  449. free_ep_req(ep, req);
  450. return;
  451. case -EOVERFLOW: /* buffer overrun on read means that
  452. * we didn't provide a big enough
  453. * buffer.
  454. */
  455. default:
  456. #if 1
  457. DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
  458. status, req->actual, req->length);
  459. #endif
  460. case -EREMOTEIO: /* short read */
  461. break;
  462. }
  463. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  464. if (status) {
  465. ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
  466. ep->name, req->length, status);
  467. usb_ep_set_halt(ep);
  468. /* FIXME recover later ... somehow */
  469. }
  470. }
  471. static struct usb_request *source_sink_start_ep(struct usb_ep *ep)
  472. {
  473. struct usb_request *req;
  474. int status;
  475. req = alloc_ep_req(ep, buflen);
  476. if (!req)
  477. return NULL;
  478. memset(req->buf, 0, req->length);
  479. req->complete = source_sink_complete;
  480. if (strcmp(ep->name, EP_IN_NAME) == 0)
  481. reinit_write_data(ep, req);
  482. else
  483. memset(req->buf, 0x55, req->length);
  484. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  485. if (status) {
  486. struct zero_dev *dev = ep->driver_data;
  487. ERROR(dev, "start %s --> %d\n", ep->name, status);
  488. free_ep_req(ep, req);
  489. req = NULL;
  490. }
  491. return req;
  492. }
  493. static int set_source_sink_config(struct zero_dev *dev)
  494. {
  495. int result = 0;
  496. struct usb_ep *ep;
  497. struct usb_gadget *gadget = dev->gadget;
  498. gadget_for_each_ep(ep, gadget) {
  499. const struct usb_endpoint_descriptor *d;
  500. /* one endpoint writes (sources) zeroes in (to the host) */
  501. if (strcmp(ep->name, EP_IN_NAME) == 0) {
  502. d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
  503. result = usb_ep_enable(ep, d);
  504. if (result == 0) {
  505. ep->driver_data = dev;
  506. if (source_sink_start_ep(ep) != NULL) {
  507. dev->in_ep = ep;
  508. continue;
  509. }
  510. usb_ep_disable(ep);
  511. result = -EIO;
  512. }
  513. /* one endpoint reads (sinks) anything out (from the host) */
  514. } else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
  515. d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
  516. result = usb_ep_enable(ep, d);
  517. if (result == 0) {
  518. ep->driver_data = dev;
  519. if (source_sink_start_ep(ep) != NULL) {
  520. dev->out_ep = ep;
  521. continue;
  522. }
  523. usb_ep_disable(ep);
  524. result = -EIO;
  525. }
  526. /* ignore any other endpoints */
  527. } else
  528. continue;
  529. /* stop on error */
  530. ERROR(dev, "can't start %s, result %d\n", ep->name, result);
  531. break;
  532. }
  533. if (result == 0)
  534. DBG(dev, "buflen %d\n", buflen);
  535. /* caller is responsible for cleanup on error */
  536. return result;
  537. }
  538. /*-------------------------------------------------------------------------*/
  539. static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
  540. {
  541. struct zero_dev *dev = ep->driver_data;
  542. int status = req->status;
  543. switch (status) {
  544. case 0: /* normal completion? */
  545. if (ep == dev->out_ep) {
  546. /* loop this OUT packet back IN to the host */
  547. req->zero = (req->actual < req->length);
  548. req->length = req->actual;
  549. status = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
  550. if (status == 0)
  551. return;
  552. /* "should never get here" */
  553. ERROR(dev, "can't loop %s to %s: %d\n",
  554. ep->name, dev->in_ep->name,
  555. status);
  556. }
  557. /* queue the buffer for some later OUT packet */
  558. req->length = buflen;
  559. status = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
  560. if (status == 0)
  561. return;
  562. /* "should never get here" */
  563. /* FALLTHROUGH */
  564. default:
  565. ERROR(dev, "%s loop complete --> %d, %d/%d\n", ep->name,
  566. status, req->actual, req->length);
  567. /* FALLTHROUGH */
  568. /* NOTE: since this driver doesn't maintain an explicit record
  569. * of requests it submitted (just maintains qlen count), we
  570. * rely on the hardware driver to clean up on disconnect or
  571. * endpoint disable.
  572. */
  573. case -ECONNABORTED: /* hardware forced ep reset */
  574. case -ECONNRESET: /* request dequeued */
  575. case -ESHUTDOWN: /* disconnect from host */
  576. free_ep_req(ep, req);
  577. return;
  578. }
  579. }
  580. static int set_loopback_config(struct zero_dev *dev)
  581. {
  582. int result = 0;
  583. struct usb_ep *ep;
  584. struct usb_gadget *gadget = dev->gadget;
  585. gadget_for_each_ep(ep, gadget) {
  586. const struct usb_endpoint_descriptor *d;
  587. /* one endpoint writes data back IN to the host */
  588. if (strcmp(ep->name, EP_IN_NAME) == 0) {
  589. d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
  590. result = usb_ep_enable(ep, d);
  591. if (result == 0) {
  592. ep->driver_data = dev;
  593. dev->in_ep = ep;
  594. continue;
  595. }
  596. /* one endpoint just reads OUT packets */
  597. } else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
  598. d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
  599. result = usb_ep_enable(ep, d);
  600. if (result == 0) {
  601. ep->driver_data = dev;
  602. dev->out_ep = ep;
  603. continue;
  604. }
  605. /* ignore any other endpoints */
  606. } else
  607. continue;
  608. /* stop on error */
  609. ERROR(dev, "can't enable %s, result %d\n", ep->name, result);
  610. break;
  611. }
  612. /* allocate a bunch of read buffers and queue them all at once.
  613. * we buffer at most 'qlen' transfers; fewer if any need more
  614. * than 'buflen' bytes each.
  615. */
  616. if (result == 0) {
  617. struct usb_request *req;
  618. unsigned i;
  619. ep = dev->out_ep;
  620. for (i = 0; i < qlen && result == 0; i++) {
  621. req = alloc_ep_req(ep, buflen);
  622. if (req) {
  623. req->complete = loopback_complete;
  624. result = usb_ep_queue(ep, req, GFP_ATOMIC);
  625. if (result)
  626. DBG(dev, "%s queue req --> %d\n",
  627. ep->name, result);
  628. } else
  629. result = -ENOMEM;
  630. }
  631. }
  632. if (result == 0)
  633. DBG(dev, "qlen %d, buflen %d\n", qlen, buflen);
  634. /* caller is responsible for cleanup on error */
  635. return result;
  636. }
  637. /*-------------------------------------------------------------------------*/
  638. static void zero_reset_config(struct zero_dev *dev)
  639. {
  640. if (dev->config == 0)
  641. return;
  642. DBG(dev, "reset config\n");
  643. /* just disable endpoints, forcing completion of pending i/o.
  644. * all our completion handlers free their requests in this case.
  645. */
  646. if (dev->in_ep) {
  647. usb_ep_disable(dev->in_ep);
  648. dev->in_ep = NULL;
  649. }
  650. if (dev->out_ep) {
  651. usb_ep_disable(dev->out_ep);
  652. dev->out_ep = NULL;
  653. }
  654. dev->config = 0;
  655. del_timer(&dev->resume);
  656. }
  657. /* change our operational config. this code must agree with the code
  658. * that returns config descriptors, and altsetting code.
  659. *
  660. * it's also responsible for power management interactions. some
  661. * configurations might not work with our current power sources.
  662. *
  663. * note that some device controller hardware will constrain what this
  664. * code can do, perhaps by disallowing more than one configuration or
  665. * by limiting configuration choices (like the pxa2xx).
  666. */
  667. static int zero_set_config(struct zero_dev *dev, unsigned number)
  668. {
  669. int result = 0;
  670. struct usb_gadget *gadget = dev->gadget;
  671. if (number == dev->config)
  672. return 0;
  673. if (gadget_is_sa1100(gadget) && dev->config) {
  674. /* tx fifo is full, but we can't clear it...*/
  675. ERROR(dev, "can't change configurations\n");
  676. return -ESPIPE;
  677. }
  678. zero_reset_config(dev);
  679. switch (number) {
  680. case CONFIG_SOURCE_SINK:
  681. result = set_source_sink_config(dev);
  682. break;
  683. case CONFIG_LOOPBACK:
  684. result = set_loopback_config(dev);
  685. break;
  686. default:
  687. result = -EINVAL;
  688. /* FALL THROUGH */
  689. case 0:
  690. return result;
  691. }
  692. if (!result && (!dev->in_ep || !dev->out_ep))
  693. result = -ENODEV;
  694. if (result)
  695. zero_reset_config(dev);
  696. else {
  697. char *speed;
  698. switch (gadget->speed) {
  699. case USB_SPEED_LOW: speed = "low"; break;
  700. case USB_SPEED_FULL: speed = "full"; break;
  701. case USB_SPEED_HIGH: speed = "high"; break;
  702. default: speed = "?"; break;
  703. }
  704. dev->config = number;
  705. INFO(dev, "%s speed config #%d: %s\n", speed, number,
  706. (number == CONFIG_SOURCE_SINK)
  707. ? source_sink : loopback);
  708. }
  709. return result;
  710. }
  711. /*-------------------------------------------------------------------------*/
  712. static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req)
  713. {
  714. if (req->status || req->actual != req->length)
  715. DBG((struct zero_dev *) ep->driver_data,
  716. "setup complete --> %d, %d/%d\n",
  717. req->status, req->actual, req->length);
  718. }
  719. /*
  720. * The setup() callback implements all the ep0 functionality that's
  721. * not handled lower down, in hardware or the hardware driver (like
  722. * device and endpoint feature flags, and their status). It's all
  723. * housekeeping for the gadget function we're implementing. Most of
  724. * the work is in config-specific setup.
  725. */
  726. static int
  727. zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  728. {
  729. struct zero_dev *dev = get_gadget_data(gadget);
  730. struct usb_request *req = dev->req;
  731. int value = -EOPNOTSUPP;
  732. u16 w_index = le16_to_cpu(ctrl->wIndex);
  733. u16 w_value = le16_to_cpu(ctrl->wValue);
  734. u16 w_length = le16_to_cpu(ctrl->wLength);
  735. /* usually this stores reply data in the pre-allocated ep0 buffer,
  736. * but config change events will reconfigure hardware.
  737. */
  738. req->zero = 0;
  739. switch (ctrl->bRequest) {
  740. case USB_REQ_GET_DESCRIPTOR:
  741. if (ctrl->bRequestType != USB_DIR_IN)
  742. goto unknown;
  743. switch (w_value >> 8) {
  744. case USB_DT_DEVICE:
  745. value = min(w_length, (u16) sizeof device_desc);
  746. memcpy(req->buf, &device_desc, value);
  747. break;
  748. case USB_DT_DEVICE_QUALIFIER:
  749. if (!gadget_is_dualspeed(gadget))
  750. break;
  751. value = min(w_length, (u16) sizeof dev_qualifier);
  752. memcpy(req->buf, &dev_qualifier, value);
  753. break;
  754. case USB_DT_OTHER_SPEED_CONFIG:
  755. if (!gadget_is_dualspeed(gadget))
  756. break;
  757. // FALLTHROUGH
  758. case USB_DT_CONFIG:
  759. value = config_buf(gadget, req->buf,
  760. w_value >> 8,
  761. w_value & 0xff);
  762. if (value >= 0)
  763. value = min(w_length, (u16) value);
  764. break;
  765. case USB_DT_STRING:
  766. /* wIndex == language code.
  767. * this driver only handles one language, you can
  768. * add string tables for other languages, using
  769. * any UTF-8 characters
  770. */
  771. value = usb_gadget_get_string(&stringtab,
  772. w_value & 0xff, req->buf);
  773. if (value >= 0)
  774. value = min(w_length, (u16) value);
  775. break;
  776. }
  777. break;
  778. /* currently two configs, two speeds */
  779. case USB_REQ_SET_CONFIGURATION:
  780. if (ctrl->bRequestType != 0)
  781. goto unknown;
  782. if (gadget->a_hnp_support)
  783. DBG(dev, "HNP available\n");
  784. else if (gadget->a_alt_hnp_support)
  785. DBG(dev, "HNP needs a different root port\n");
  786. else
  787. VDBG(dev, "HNP inactive\n");
  788. spin_lock(&dev->lock);
  789. value = zero_set_config(dev, w_value);
  790. spin_unlock(&dev->lock);
  791. break;
  792. case USB_REQ_GET_CONFIGURATION:
  793. if (ctrl->bRequestType != USB_DIR_IN)
  794. goto unknown;
  795. *(u8 *)req->buf = dev->config;
  796. value = min(w_length, (u16) 1);
  797. break;
  798. /* until we add altsetting support, or other interfaces,
  799. * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  800. * and already killed pending endpoint I/O.
  801. */
  802. case USB_REQ_SET_INTERFACE:
  803. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  804. goto unknown;
  805. spin_lock(&dev->lock);
  806. if (dev->config && w_index == 0 && w_value == 0) {
  807. u8 config = dev->config;
  808. /* resets interface configuration, forgets about
  809. * previous transaction state (queued bufs, etc)
  810. * and re-inits endpoint state (toggle etc)
  811. * no response queued, just zero status == success.
  812. * if we had more than one interface we couldn't
  813. * use this "reset the config" shortcut.
  814. */
  815. zero_reset_config(dev);
  816. zero_set_config(dev, config);
  817. value = 0;
  818. }
  819. spin_unlock(&dev->lock);
  820. break;
  821. case USB_REQ_GET_INTERFACE:
  822. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  823. goto unknown;
  824. if (!dev->config)
  825. break;
  826. if (w_index != 0) {
  827. value = -EDOM;
  828. break;
  829. }
  830. *(u8 *)req->buf = 0;
  831. value = min(w_length, (u16) 1);
  832. break;
  833. /*
  834. * These are the same vendor-specific requests supported by
  835. * Intel's USB 2.0 compliance test devices. We exceed that
  836. * device spec by allowing multiple-packet requests.
  837. */
  838. case 0x5b: /* control WRITE test -- fill the buffer */
  839. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  840. goto unknown;
  841. if (w_value || w_index)
  842. break;
  843. /* just read that many bytes into the buffer */
  844. if (w_length > USB_BUFSIZ)
  845. break;
  846. value = w_length;
  847. break;
  848. case 0x5c: /* control READ test -- return the buffer */
  849. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  850. goto unknown;
  851. if (w_value || w_index)
  852. break;
  853. /* expect those bytes are still in the buffer; send back */
  854. if (w_length > USB_BUFSIZ
  855. || w_length != req->length)
  856. break;
  857. value = w_length;
  858. break;
  859. default:
  860. unknown:
  861. VDBG(dev,
  862. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  863. ctrl->bRequestType, ctrl->bRequest,
  864. w_value, w_index, w_length);
  865. }
  866. /* respond with data transfer before status phase? */
  867. if (value >= 0) {
  868. req->length = value;
  869. req->zero = value < w_length;
  870. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  871. if (value < 0) {
  872. DBG(dev, "ep_queue --> %d\n", value);
  873. req->status = 0;
  874. zero_setup_complete(gadget->ep0, req);
  875. }
  876. }
  877. /* device either stalls (value < 0) or reports success */
  878. return value;
  879. }
  880. static void zero_disconnect(struct usb_gadget *gadget)
  881. {
  882. struct zero_dev *dev = get_gadget_data(gadget);
  883. unsigned long flags;
  884. spin_lock_irqsave(&dev->lock, flags);
  885. zero_reset_config(dev);
  886. /* a more significant application might have some non-usb
  887. * activities to quiesce here, saving resources like power
  888. * or pushing the notification up a network stack.
  889. */
  890. spin_unlock_irqrestore(&dev->lock, flags);
  891. /* next we may get setup() calls to enumerate new connections;
  892. * or an unbind() during shutdown (including removing module).
  893. */
  894. }
  895. static void zero_autoresume(unsigned long _dev)
  896. {
  897. struct zero_dev *dev = (struct zero_dev *) _dev;
  898. int status;
  899. /* normally the host would be woken up for something
  900. * more significant than just a timer firing...
  901. */
  902. if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  903. status = usb_gadget_wakeup(dev->gadget);
  904. DBG(dev, "wakeup --> %d\n", status);
  905. }
  906. }
  907. /*-------------------------------------------------------------------------*/
  908. static void zero_unbind(struct usb_gadget *gadget)
  909. {
  910. struct zero_dev *dev = get_gadget_data(gadget);
  911. DBG(dev, "unbind\n");
  912. /* we've already been disconnected ... no i/o is active */
  913. if (dev->req) {
  914. dev->req->length = USB_BUFSIZ;
  915. free_ep_req(gadget->ep0, dev->req);
  916. }
  917. del_timer_sync(&dev->resume);
  918. kfree(dev);
  919. set_gadget_data(gadget, NULL);
  920. }
  921. static int __init zero_bind(struct usb_gadget *gadget)
  922. {
  923. struct zero_dev *dev;
  924. struct usb_ep *ep;
  925. int gcnum;
  926. /* FIXME this can't yet work right with SH ... it has only
  927. * one configuration, numbered one.
  928. */
  929. if (gadget_is_sh(gadget))
  930. return -ENODEV;
  931. /* Bulk-only drivers like this one SHOULD be able to
  932. * autoconfigure on any sane usb controller driver,
  933. * but there may also be important quirks to address.
  934. */
  935. usb_ep_autoconfig_reset(gadget);
  936. ep = usb_ep_autoconfig(gadget, &fs_source_desc);
  937. if (!ep) {
  938. autoconf_fail:
  939. pr_err("%s: can't autoconfigure on %s\n",
  940. shortname, gadget->name);
  941. return -ENODEV;
  942. }
  943. EP_IN_NAME = ep->name;
  944. ep->driver_data = ep; /* claim */
  945. ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
  946. if (!ep)
  947. goto autoconf_fail;
  948. EP_OUT_NAME = ep->name;
  949. ep->driver_data = ep; /* claim */
  950. gcnum = usb_gadget_controller_number(gadget);
  951. if (gcnum >= 0)
  952. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  953. else {
  954. /* gadget zero is so simple (for now, no altsettings) that
  955. * it SHOULD NOT have problems with bulk-capable hardware.
  956. * so warn about unrcognized controllers, don't panic.
  957. *
  958. * things like configuration and altsetting numbering
  959. * can need hardware-specific attention though.
  960. */
  961. pr_warning("%s: controller '%s' not recognized\n",
  962. shortname, gadget->name);
  963. device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
  964. }
  965. /* ok, we made sense of the hardware ... */
  966. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  967. if (!dev)
  968. return -ENOMEM;
  969. spin_lock_init(&dev->lock);
  970. dev->gadget = gadget;
  971. set_gadget_data(gadget, dev);
  972. init_timer(&dev->resume);
  973. dev->resume.function = zero_autoresume;
  974. dev->resume.data = (unsigned long) dev;
  975. /* preallocate control response and buffer */
  976. dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  977. if (!dev->req)
  978. goto enomem;
  979. dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  980. if (!dev->req->buf)
  981. goto enomem;
  982. dev->req->complete = zero_setup_complete;
  983. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  984. if (gadget_is_dualspeed(gadget)) {
  985. /* assume ep0 uses the same value for both speeds ... */
  986. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  987. /* and that all endpoints are dual-speed */
  988. hs_source_desc.bEndpointAddress =
  989. fs_source_desc.bEndpointAddress;
  990. hs_sink_desc.bEndpointAddress =
  991. fs_sink_desc.bEndpointAddress;
  992. }
  993. if (gadget_is_otg(gadget)) {
  994. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  995. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  996. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  997. }
  998. usb_gadget_set_selfpowered(gadget);
  999. if (autoresume) {
  1000. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1001. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1002. }
  1003. gadget->ep0->driver_data = dev;
  1004. INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
  1005. INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
  1006. EP_OUT_NAME, EP_IN_NAME);
  1007. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  1008. init_utsname()->sysname, init_utsname()->release,
  1009. gadget->name);
  1010. return 0;
  1011. enomem:
  1012. zero_unbind(gadget);
  1013. return -ENOMEM;
  1014. }
  1015. /*-------------------------------------------------------------------------*/
  1016. static void zero_suspend(struct usb_gadget *gadget)
  1017. {
  1018. struct zero_dev *dev = get_gadget_data(gadget);
  1019. if (gadget->speed == USB_SPEED_UNKNOWN)
  1020. return;
  1021. if (autoresume) {
  1022. mod_timer(&dev->resume, jiffies + (HZ * autoresume));
  1023. DBG(dev, "suspend, wakeup in %d seconds\n", autoresume);
  1024. } else
  1025. DBG(dev, "suspend\n");
  1026. }
  1027. static void zero_resume(struct usb_gadget *gadget)
  1028. {
  1029. struct zero_dev *dev = get_gadget_data(gadget);
  1030. DBG(dev, "resume\n");
  1031. del_timer(&dev->resume);
  1032. }
  1033. /*-------------------------------------------------------------------------*/
  1034. static struct usb_gadget_driver zero_driver = {
  1035. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1036. .speed = USB_SPEED_HIGH,
  1037. #else
  1038. .speed = USB_SPEED_FULL,
  1039. #endif
  1040. .function = (char *) longname,
  1041. .bind = zero_bind,
  1042. .unbind = __exit_p(zero_unbind),
  1043. .setup = zero_setup,
  1044. .disconnect = zero_disconnect,
  1045. .suspend = zero_suspend,
  1046. .resume = zero_resume,
  1047. .driver = {
  1048. .name = (char *) shortname,
  1049. .owner = THIS_MODULE,
  1050. },
  1051. };
  1052. MODULE_AUTHOR("David Brownell");
  1053. MODULE_LICENSE("GPL");
  1054. static int __init init(void)
  1055. {
  1056. return usb_gadget_register_driver(&zero_driver);
  1057. }
  1058. module_init(init);
  1059. static void __exit cleanup(void)
  1060. {
  1061. usb_gadget_unregister_driver(&zero_driver);
  1062. }
  1063. module_exit(cleanup);