zero.c 37 KB

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