zero.c 35 KB

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