zero.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 = kmalloc(length, GFP_ATOMIC);
  403. if (!req->buf) {
  404. usb_ep_free_request (ep, req);
  405. req = NULL;
  406. }
  407. }
  408. return req;
  409. }
  410. static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  411. {
  412. kfree(req->buf);
  413. usb_ep_free_request (ep, req);
  414. }
  415. /*-------------------------------------------------------------------------*/
  416. /* optionally require specific source/sink data patterns */
  417. static int
  418. check_read_data (
  419. struct zero_dev *dev,
  420. struct usb_ep *ep,
  421. struct usb_request *req
  422. )
  423. {
  424. unsigned i;
  425. u8 *buf = req->buf;
  426. for (i = 0; i < req->actual; i++, buf++) {
  427. switch (pattern) {
  428. /* all-zeroes has no synchronization issues */
  429. case 0:
  430. if (*buf == 0)
  431. continue;
  432. break;
  433. /* mod63 stays in sync with short-terminated transfers,
  434. * or otherwise when host and gadget agree on how large
  435. * each usb transfer request should be. resync is done
  436. * with set_interface or set_config.
  437. */
  438. case 1:
  439. if (*buf == (u8)(i % 63))
  440. continue;
  441. break;
  442. }
  443. ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  444. usb_ep_set_halt (ep);
  445. return -EINVAL;
  446. }
  447. return 0;
  448. }
  449. static void
  450. reinit_write_data (
  451. struct zero_dev *dev,
  452. struct usb_ep *ep,
  453. struct usb_request *req
  454. )
  455. {
  456. unsigned i;
  457. u8 *buf = req->buf;
  458. switch (pattern) {
  459. case 0:
  460. memset (req->buf, 0, req->length);
  461. break;
  462. case 1:
  463. for (i = 0; i < req->length; i++)
  464. *buf++ = (u8) (i % 63);
  465. break;
  466. }
  467. }
  468. /* if there is only one request in the queue, there'll always be an
  469. * irq delay between end of one request and start of the next.
  470. * that prevents using hardware dma queues.
  471. */
  472. static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
  473. {
  474. struct zero_dev *dev = ep->driver_data;
  475. int status = req->status;
  476. switch (status) {
  477. case 0: /* normal completion? */
  478. if (ep == dev->out_ep) {
  479. check_read_data (dev, ep, req);
  480. memset (req->buf, 0x55, req->length);
  481. } else
  482. reinit_write_data (dev, ep, req);
  483. break;
  484. /* this endpoint is normally active while we're configured */
  485. case -ECONNABORTED: /* hardware forced ep reset */
  486. case -ECONNRESET: /* request dequeued */
  487. case -ESHUTDOWN: /* disconnect from host */
  488. VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  489. req->actual, req->length);
  490. if (ep == dev->out_ep)
  491. check_read_data (dev, ep, req);
  492. free_ep_req (ep, req);
  493. return;
  494. case -EOVERFLOW: /* buffer overrun on read means that
  495. * we didn't provide a big enough
  496. * buffer.
  497. */
  498. default:
  499. #if 1
  500. DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  501. status, req->actual, req->length);
  502. #endif
  503. case -EREMOTEIO: /* short read */
  504. break;
  505. }
  506. status = usb_ep_queue (ep, req, GFP_ATOMIC);
  507. if (status) {
  508. ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  509. ep->name, req->length, status);
  510. usb_ep_set_halt (ep);
  511. /* FIXME recover later ... somehow */
  512. }
  513. }
  514. static struct usb_request *
  515. source_sink_start_ep (struct usb_ep *ep, gfp_t gfp_flags)
  516. {
  517. struct usb_request *req;
  518. int status;
  519. req = alloc_ep_req (ep, buflen);
  520. if (!req)
  521. return NULL;
  522. memset (req->buf, 0, req->length);
  523. req->complete = source_sink_complete;
  524. if (strcmp (ep->name, EP_IN_NAME) == 0)
  525. reinit_write_data (ep->driver_data, ep, req);
  526. else
  527. memset (req->buf, 0x55, req->length);
  528. status = usb_ep_queue (ep, req, gfp_flags);
  529. if (status) {
  530. struct zero_dev *dev = ep->driver_data;
  531. ERROR (dev, "start %s --> %d\n", ep->name, status);
  532. free_ep_req (ep, req);
  533. req = NULL;
  534. }
  535. return req;
  536. }
  537. static int
  538. set_source_sink_config (struct zero_dev *dev, gfp_t gfp_flags)
  539. {
  540. int result = 0;
  541. struct usb_ep *ep;
  542. struct usb_gadget *gadget = dev->gadget;
  543. gadget_for_each_ep (ep, gadget) {
  544. const struct usb_endpoint_descriptor *d;
  545. /* one endpoint writes (sources) zeroes in (to the host) */
  546. if (strcmp (ep->name, EP_IN_NAME) == 0) {
  547. d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
  548. result = usb_ep_enable (ep, d);
  549. if (result == 0) {
  550. ep->driver_data = dev;
  551. if (source_sink_start_ep (ep, gfp_flags) != 0) {
  552. dev->in_ep = ep;
  553. continue;
  554. }
  555. usb_ep_disable (ep);
  556. result = -EIO;
  557. }
  558. /* one endpoint reads (sinks) anything out (from the host) */
  559. } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
  560. d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
  561. result = usb_ep_enable (ep, d);
  562. if (result == 0) {
  563. ep->driver_data = dev;
  564. if (source_sink_start_ep (ep, gfp_flags) != 0) {
  565. dev->out_ep = ep;
  566. continue;
  567. }
  568. usb_ep_disable (ep);
  569. result = -EIO;
  570. }
  571. /* ignore any other endpoints */
  572. } else
  573. continue;
  574. /* stop on error */
  575. ERROR (dev, "can't start %s, result %d\n", ep->name, result);
  576. break;
  577. }
  578. if (result == 0)
  579. DBG (dev, "buflen %d\n", buflen);
  580. /* caller is responsible for cleanup on error */
  581. return result;
  582. }
  583. /*-------------------------------------------------------------------------*/
  584. static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
  585. {
  586. struct zero_dev *dev = ep->driver_data;
  587. int status = req->status;
  588. switch (status) {
  589. case 0: /* normal completion? */
  590. if (ep == dev->out_ep) {
  591. /* loop this OUT packet back IN to the host */
  592. req->zero = (req->actual < req->length);
  593. req->length = req->actual;
  594. status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
  595. if (status == 0)
  596. return;
  597. /* "should never get here" */
  598. ERROR (dev, "can't loop %s to %s: %d\n",
  599. ep->name, dev->in_ep->name,
  600. status);
  601. }
  602. /* queue the buffer for some later OUT packet */
  603. req->length = buflen;
  604. status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
  605. if (status == 0)
  606. return;
  607. /* "should never get here" */
  608. /* FALLTHROUGH */
  609. default:
  610. ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
  611. status, req->actual, req->length);
  612. /* FALLTHROUGH */
  613. /* NOTE: since this driver doesn't maintain an explicit record
  614. * of requests it submitted (just maintains qlen count), we
  615. * rely on the hardware driver to clean up on disconnect or
  616. * endpoint disable.
  617. */
  618. case -ECONNABORTED: /* hardware forced ep reset */
  619. case -ECONNRESET: /* request dequeued */
  620. case -ESHUTDOWN: /* disconnect from host */
  621. free_ep_req (ep, req);
  622. return;
  623. }
  624. }
  625. static int
  626. set_loopback_config (struct zero_dev *dev, gfp_t gfp_flags)
  627. {
  628. int result = 0;
  629. struct usb_ep *ep;
  630. struct usb_gadget *gadget = dev->gadget;
  631. gadget_for_each_ep (ep, gadget) {
  632. const struct usb_endpoint_descriptor *d;
  633. /* one endpoint writes data back IN to the host */
  634. if (strcmp (ep->name, EP_IN_NAME) == 0) {
  635. d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
  636. result = usb_ep_enable (ep, d);
  637. if (result == 0) {
  638. ep->driver_data = dev;
  639. dev->in_ep = ep;
  640. continue;
  641. }
  642. /* one endpoint just reads OUT packets */
  643. } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
  644. d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
  645. result = usb_ep_enable (ep, d);
  646. if (result == 0) {
  647. ep->driver_data = dev;
  648. dev->out_ep = ep;
  649. continue;
  650. }
  651. /* ignore any other endpoints */
  652. } else
  653. continue;
  654. /* stop on error */
  655. ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
  656. break;
  657. }
  658. /* allocate a bunch of read buffers and queue them all at once.
  659. * we buffer at most 'qlen' transfers; fewer if any need more
  660. * than 'buflen' bytes each.
  661. */
  662. if (result == 0) {
  663. struct usb_request *req;
  664. unsigned i;
  665. ep = dev->out_ep;
  666. for (i = 0; i < qlen && result == 0; i++) {
  667. req = alloc_ep_req (ep, buflen);
  668. if (req) {
  669. req->complete = loopback_complete;
  670. result = usb_ep_queue (ep, req, GFP_ATOMIC);
  671. if (result)
  672. DBG (dev, "%s queue req --> %d\n",
  673. ep->name, result);
  674. } else
  675. result = -ENOMEM;
  676. }
  677. }
  678. if (result == 0)
  679. DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
  680. /* caller is responsible for cleanup on error */
  681. return result;
  682. }
  683. /*-------------------------------------------------------------------------*/
  684. static void zero_reset_config (struct zero_dev *dev)
  685. {
  686. if (dev->config == 0)
  687. return;
  688. DBG (dev, "reset config\n");
  689. /* just disable endpoints, forcing completion of pending i/o.
  690. * all our completion handlers free their requests in this case.
  691. */
  692. if (dev->in_ep) {
  693. usb_ep_disable (dev->in_ep);
  694. dev->in_ep = NULL;
  695. }
  696. if (dev->out_ep) {
  697. usb_ep_disable (dev->out_ep);
  698. dev->out_ep = NULL;
  699. }
  700. dev->config = 0;
  701. del_timer (&dev->resume);
  702. }
  703. /* change our operational config. this code must agree with the code
  704. * that returns config descriptors, and altsetting code.
  705. *
  706. * it's also responsible for power management interactions. some
  707. * configurations might not work with our current power sources.
  708. *
  709. * note that some device controller hardware will constrain what this
  710. * code can do, perhaps by disallowing more than one configuration or
  711. * by limiting configuration choices (like the pxa2xx).
  712. */
  713. static int
  714. zero_set_config (struct zero_dev *dev, unsigned number, gfp_t gfp_flags)
  715. {
  716. int result = 0;
  717. struct usb_gadget *gadget = dev->gadget;
  718. if (number == dev->config)
  719. return 0;
  720. if (gadget_is_sa1100 (gadget) && dev->config) {
  721. /* tx fifo is full, but we can't clear it...*/
  722. INFO (dev, "can't change configurations\n");
  723. return -ESPIPE;
  724. }
  725. zero_reset_config (dev);
  726. switch (number) {
  727. case CONFIG_SOURCE_SINK:
  728. result = set_source_sink_config (dev, gfp_flags);
  729. break;
  730. case CONFIG_LOOPBACK:
  731. result = set_loopback_config (dev, gfp_flags);
  732. break;
  733. default:
  734. result = -EINVAL;
  735. /* FALL THROUGH */
  736. case 0:
  737. return result;
  738. }
  739. if (!result && (!dev->in_ep || !dev->out_ep))
  740. result = -ENODEV;
  741. if (result)
  742. zero_reset_config (dev);
  743. else {
  744. char *speed;
  745. switch (gadget->speed) {
  746. case USB_SPEED_LOW: speed = "low"; break;
  747. case USB_SPEED_FULL: speed = "full"; break;
  748. case USB_SPEED_HIGH: speed = "high"; break;
  749. default: speed = "?"; break;
  750. }
  751. dev->config = number;
  752. INFO (dev, "%s speed config #%d: %s\n", speed, number,
  753. (number == CONFIG_SOURCE_SINK)
  754. ? source_sink : loopback);
  755. }
  756. return result;
  757. }
  758. /*-------------------------------------------------------------------------*/
  759. static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  760. {
  761. if (req->status || req->actual != req->length)
  762. DBG ((struct zero_dev *) ep->driver_data,
  763. "setup complete --> %d, %d/%d\n",
  764. req->status, req->actual, req->length);
  765. }
  766. /*
  767. * The setup() callback implements all the ep0 functionality that's
  768. * not handled lower down, in hardware or the hardware driver (like
  769. * device and endpoint feature flags, and their status). It's all
  770. * housekeeping for the gadget function we're implementing. Most of
  771. * the work is in config-specific setup.
  772. */
  773. static int
  774. zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  775. {
  776. struct zero_dev *dev = get_gadget_data (gadget);
  777. struct usb_request *req = dev->req;
  778. int value = -EOPNOTSUPP;
  779. u16 w_index = le16_to_cpu(ctrl->wIndex);
  780. u16 w_value = le16_to_cpu(ctrl->wValue);
  781. u16 w_length = le16_to_cpu(ctrl->wLength);
  782. /* usually this stores reply data in the pre-allocated ep0 buffer,
  783. * but config change events will reconfigure hardware.
  784. */
  785. req->zero = 0;
  786. switch (ctrl->bRequest) {
  787. case USB_REQ_GET_DESCRIPTOR:
  788. if (ctrl->bRequestType != USB_DIR_IN)
  789. goto unknown;
  790. switch (w_value >> 8) {
  791. case USB_DT_DEVICE:
  792. value = min (w_length, (u16) sizeof device_desc);
  793. memcpy (req->buf, &device_desc, value);
  794. break;
  795. #ifdef CONFIG_USB_GADGET_DUALSPEED
  796. case USB_DT_DEVICE_QUALIFIER:
  797. if (!gadget->is_dualspeed)
  798. break;
  799. value = min (w_length, (u16) sizeof dev_qualifier);
  800. memcpy (req->buf, &dev_qualifier, value);
  801. break;
  802. case USB_DT_OTHER_SPEED_CONFIG:
  803. if (!gadget->is_dualspeed)
  804. break;
  805. // FALLTHROUGH
  806. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  807. case USB_DT_CONFIG:
  808. value = config_buf (gadget, req->buf,
  809. w_value >> 8,
  810. w_value & 0xff);
  811. if (value >= 0)
  812. value = min (w_length, (u16) value);
  813. break;
  814. case USB_DT_STRING:
  815. /* wIndex == language code.
  816. * this driver only handles one language, you can
  817. * add string tables for other languages, using
  818. * any UTF-8 characters
  819. */
  820. value = usb_gadget_get_string (&stringtab,
  821. w_value & 0xff, req->buf);
  822. if (value >= 0)
  823. value = min (w_length, (u16) value);
  824. break;
  825. }
  826. break;
  827. /* currently two configs, two speeds */
  828. case USB_REQ_SET_CONFIGURATION:
  829. if (ctrl->bRequestType != 0)
  830. goto unknown;
  831. if (gadget->a_hnp_support)
  832. DBG (dev, "HNP available\n");
  833. else if (gadget->a_alt_hnp_support)
  834. DBG (dev, "HNP needs a different root port\n");
  835. else
  836. VDBG (dev, "HNP inactive\n");
  837. spin_lock (&dev->lock);
  838. value = zero_set_config (dev, w_value, GFP_ATOMIC);
  839. spin_unlock (&dev->lock);
  840. break;
  841. case USB_REQ_GET_CONFIGURATION:
  842. if (ctrl->bRequestType != USB_DIR_IN)
  843. goto unknown;
  844. *(u8 *)req->buf = dev->config;
  845. value = min (w_length, (u16) 1);
  846. break;
  847. /* until we add altsetting support, or other interfaces,
  848. * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  849. * and already killed pending endpoint I/O.
  850. */
  851. case USB_REQ_SET_INTERFACE:
  852. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  853. goto unknown;
  854. spin_lock (&dev->lock);
  855. if (dev->config && w_index == 0 && w_value == 0) {
  856. u8 config = dev->config;
  857. /* resets interface configuration, forgets about
  858. * previous transaction state (queued bufs, etc)
  859. * and re-inits endpoint state (toggle etc)
  860. * no response queued, just zero status == success.
  861. * if we had more than one interface we couldn't
  862. * use this "reset the config" shortcut.
  863. */
  864. zero_reset_config (dev);
  865. zero_set_config (dev, config, GFP_ATOMIC);
  866. value = 0;
  867. }
  868. spin_unlock (&dev->lock);
  869. break;
  870. case USB_REQ_GET_INTERFACE:
  871. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  872. goto unknown;
  873. if (!dev->config)
  874. break;
  875. if (w_index != 0) {
  876. value = -EDOM;
  877. break;
  878. }
  879. *(u8 *)req->buf = 0;
  880. value = min (w_length, (u16) 1);
  881. break;
  882. /*
  883. * These are the same vendor-specific requests supported by
  884. * Intel's USB 2.0 compliance test devices. We exceed that
  885. * device spec by allowing multiple-packet requests.
  886. */
  887. case 0x5b: /* control WRITE test -- fill the buffer */
  888. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  889. goto unknown;
  890. if (w_value || w_index)
  891. break;
  892. /* just read that many bytes into the buffer */
  893. if (w_length > USB_BUFSIZ)
  894. break;
  895. value = w_length;
  896. break;
  897. case 0x5c: /* control READ test -- return the buffer */
  898. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  899. goto unknown;
  900. if (w_value || w_index)
  901. break;
  902. /* expect those bytes are still in the buffer; send back */
  903. if (w_length > USB_BUFSIZ
  904. || w_length != req->length)
  905. break;
  906. value = w_length;
  907. break;
  908. default:
  909. unknown:
  910. VDBG (dev,
  911. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  912. ctrl->bRequestType, ctrl->bRequest,
  913. w_value, w_index, w_length);
  914. }
  915. /* respond with data transfer before status phase? */
  916. if (value >= 0) {
  917. req->length = value;
  918. req->zero = value < w_length;
  919. value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  920. if (value < 0) {
  921. DBG (dev, "ep_queue --> %d\n", value);
  922. req->status = 0;
  923. zero_setup_complete (gadget->ep0, req);
  924. }
  925. }
  926. /* device either stalls (value < 0) or reports success */
  927. return value;
  928. }
  929. static void
  930. zero_disconnect (struct usb_gadget *gadget)
  931. {
  932. struct zero_dev *dev = get_gadget_data (gadget);
  933. unsigned long flags;
  934. spin_lock_irqsave (&dev->lock, flags);
  935. zero_reset_config (dev);
  936. /* a more significant application might have some non-usb
  937. * activities to quiesce here, saving resources like power
  938. * or pushing the notification up a network stack.
  939. */
  940. spin_unlock_irqrestore (&dev->lock, flags);
  941. /* next we may get setup() calls to enumerate new connections;
  942. * or an unbind() during shutdown (including removing module).
  943. */
  944. }
  945. static void
  946. zero_autoresume (unsigned long _dev)
  947. {
  948. struct zero_dev *dev = (struct zero_dev *) _dev;
  949. int status;
  950. /* normally the host would be woken up for something
  951. * more significant than just a timer firing...
  952. */
  953. if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  954. status = usb_gadget_wakeup (dev->gadget);
  955. DBG (dev, "wakeup --> %d\n", status);
  956. }
  957. }
  958. /*-------------------------------------------------------------------------*/
  959. static void /* __init_or_exit */
  960. zero_unbind (struct usb_gadget *gadget)
  961. {
  962. struct zero_dev *dev = get_gadget_data (gadget);
  963. DBG (dev, "unbind\n");
  964. /* we've already been disconnected ... no i/o is active */
  965. if (dev->req) {
  966. dev->req->length = USB_BUFSIZ;
  967. free_ep_req (gadget->ep0, dev->req);
  968. }
  969. del_timer_sync (&dev->resume);
  970. kfree (dev);
  971. set_gadget_data (gadget, NULL);
  972. }
  973. static int __init
  974. zero_bind (struct usb_gadget *gadget)
  975. {
  976. struct zero_dev *dev;
  977. struct usb_ep *ep;
  978. int gcnum;
  979. /* FIXME this can't yet work right with SH ... it has only
  980. * one configuration, numbered one.
  981. */
  982. if (gadget_is_sh(gadget))
  983. return -ENODEV;
  984. /* Bulk-only drivers like this one SHOULD be able to
  985. * autoconfigure on any sane usb controller driver,
  986. * but there may also be important quirks to address.
  987. */
  988. usb_ep_autoconfig_reset (gadget);
  989. ep = usb_ep_autoconfig (gadget, &fs_source_desc);
  990. if (!ep) {
  991. autoconf_fail:
  992. printk (KERN_ERR "%s: can't autoconfigure on %s\n",
  993. shortname, gadget->name);
  994. return -ENODEV;
  995. }
  996. EP_IN_NAME = ep->name;
  997. ep->driver_data = ep; /* claim */
  998. ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
  999. if (!ep)
  1000. goto autoconf_fail;
  1001. EP_OUT_NAME = ep->name;
  1002. ep->driver_data = ep; /* claim */
  1003. gcnum = usb_gadget_controller_number (gadget);
  1004. if (gcnum >= 0)
  1005. device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
  1006. else {
  1007. /* gadget zero is so simple (for now, no altsettings) that
  1008. * it SHOULD NOT have problems with bulk-capable hardware.
  1009. * so warn about unrcognized controllers, don't panic.
  1010. *
  1011. * things like configuration and altsetting numbering
  1012. * can need hardware-specific attention though.
  1013. */
  1014. printk (KERN_WARNING "%s: controller '%s' not recognized\n",
  1015. shortname, gadget->name);
  1016. device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
  1017. }
  1018. /* ok, we made sense of the hardware ... */
  1019. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1020. if (!dev)
  1021. return -ENOMEM;
  1022. spin_lock_init (&dev->lock);
  1023. dev->gadget = gadget;
  1024. set_gadget_data (gadget, dev);
  1025. /* preallocate control response and buffer */
  1026. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1027. if (!dev->req)
  1028. goto enomem;
  1029. dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  1030. if (!dev->req->buf)
  1031. goto enomem;
  1032. dev->req->complete = zero_setup_complete;
  1033. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1034. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1035. /* assume ep0 uses the same value for both speeds ... */
  1036. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1037. /* and that all endpoints are dual-speed */
  1038. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  1039. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  1040. #endif
  1041. if (gadget->is_otg) {
  1042. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1043. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1044. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1045. }
  1046. usb_gadget_set_selfpowered (gadget);
  1047. init_timer (&dev->resume);
  1048. dev->resume.function = zero_autoresume;
  1049. dev->resume.data = (unsigned long) dev;
  1050. if (autoresume) {
  1051. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1052. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1053. }
  1054. gadget->ep0->driver_data = dev;
  1055. INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  1056. INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  1057. EP_OUT_NAME, EP_IN_NAME);
  1058. snprintf (manufacturer, sizeof manufacturer, "%s %s with %s",
  1059. init_utsname()->sysname, init_utsname()->release,
  1060. gadget->name);
  1061. return 0;
  1062. enomem:
  1063. zero_unbind (gadget);
  1064. return -ENOMEM;
  1065. }
  1066. /*-------------------------------------------------------------------------*/
  1067. static void
  1068. zero_suspend (struct usb_gadget *gadget)
  1069. {
  1070. struct zero_dev *dev = get_gadget_data (gadget);
  1071. if (gadget->speed == USB_SPEED_UNKNOWN)
  1072. return;
  1073. if (autoresume) {
  1074. mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  1075. DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  1076. } else
  1077. DBG (dev, "suspend\n");
  1078. }
  1079. static void
  1080. zero_resume (struct usb_gadget *gadget)
  1081. {
  1082. struct zero_dev *dev = get_gadget_data (gadget);
  1083. DBG (dev, "resume\n");
  1084. del_timer (&dev->resume);
  1085. }
  1086. /*-------------------------------------------------------------------------*/
  1087. static struct usb_gadget_driver zero_driver = {
  1088. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1089. .speed = USB_SPEED_HIGH,
  1090. #else
  1091. .speed = USB_SPEED_FULL,
  1092. #endif
  1093. .function = (char *) longname,
  1094. .bind = zero_bind,
  1095. .unbind = __exit_p(zero_unbind),
  1096. .setup = zero_setup,
  1097. .disconnect = zero_disconnect,
  1098. .suspend = zero_suspend,
  1099. .resume = zero_resume,
  1100. .driver = {
  1101. .name = (char *) shortname,
  1102. .owner = THIS_MODULE,
  1103. },
  1104. };
  1105. MODULE_AUTHOR ("David Brownell");
  1106. MODULE_LICENSE ("Dual BSD/GPL");
  1107. static int __init init (void)
  1108. {
  1109. /* a real value would likely come through some id prom
  1110. * or module option. this one takes at least two packets.
  1111. */
  1112. strlcpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
  1113. return usb_gadget_register_driver (&zero_driver);
  1114. }
  1115. module_init (init);
  1116. static void __exit cleanup (void)
  1117. {
  1118. usb_gadget_unregister_driver (&zero_driver);
  1119. }
  1120. module_exit (cleanup);