zero.c 35 KB

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