zero.c 34 KB

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