zero.c 35 KB

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