2 * zero.c -- Gadget Zero, for USB development
4 * Copyright (C) 2003-2004 David Brownell
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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.
40 * Gadget Zero only needs two bulk endpoints, and is an example of how you
41 * can write a hardware-agnostic gadget driver running inside a USB device.
43 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44 * affect most of the driver.
46 * Use it with the Linux host/master side "usbtest" driver to get a basic
47 * functional test of your device-side usb stack, or with "usb-skeleton".
49 * It supports two similar configurations. One sinks whatever the usb host
50 * writes, and in return sources zeroes. The other loops whatever the host
51 * writes back, so the host can read it. Module options include:
53 * buflen=N default N=4096, buffer size used
54 * qlen=N default N=32, how many buffers in the loopback queue
55 * loopdefault default false, list loopback config first
57 * Many drivers will only have one configuration, letting them be much
58 * simpler if they also don't support high speed operation (like this
62 #include <linux/config.h>
63 #include <linux/module.h>
64 #include <linux/kernel.h>
65 #include <linux/delay.h>
66 #include <linux/ioport.h>
67 #include <linux/sched.h>
68 #include <linux/slab.h>
69 #include <linux/smp_lock.h>
70 #include <linux/errno.h>
71 #include <linux/init.h>
72 #include <linux/timer.h>
73 #include <linux/list.h>
74 #include <linux/interrupt.h>
75 #include <linux/uts.h>
76 #include <linux/version.h>
77 #include <linux/device.h>
78 #include <linux/moduleparam.h>
79 #include <linux/proc_fs.h>
81 #include <asm/byteorder.h>
84 #include <asm/system.h>
85 #include <asm/unaligned.h>
87 #include <linux/usb_ch9.h>
88 #include <linux/usb_gadget.h>
91 /*-------------------------------------------------------------------------*/
92 /*-------------------------------------------------------------------------*/
95 static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
101 /* this insists on correct encodings, though not minimal ones.
102 * BUT it currently rejects legit 4-byte UTF-8 code points,
103 * which need surrogate pairs. (Unicode 3.1 can use them.)
105 while (len != 0 && (c = (u8) *s++) != 0) {
106 if (unlikely(c & 0x80)) {
108 // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
109 if ((c & 0xe0) == 0xc0) {
110 uchar = (c & 0x1f) << 6;
113 if ((c & 0xc0) != 0xc0)
118 // 3-byte sequence (most CJKV characters):
119 // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
120 } else if ((c & 0xf0) == 0xe0) {
121 uchar = (c & 0x0f) << 12;
124 if ((c & 0xc0) != 0xc0)
130 if ((c & 0xc0) != 0xc0)
135 /* no bogus surrogates */
136 if (0xd800 <= uchar && uchar <= 0xdfff)
139 // 4-byte sequence (surrogate pairs, currently rare):
140 // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
141 // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
142 // (uuuuu = wwww + 1)
143 // FIXME accept the surrogate code points (only)
149 put_unaligned (cpu_to_le16 (uchar), cp++);
160 * usb_gadget_get_string - fill out a string descriptor
161 * @table: of c strings encoded using UTF-8
162 * @id: string id, from low byte of wValue in get string descriptor
163 * @buf: at least 256 bytes
165 * Finds the UTF-8 string matching the ID, and converts it into a
166 * string descriptor in utf16-le.
167 * Returns length of descriptor (always even) or negative errno
169 * If your driver needs stings in multiple languages, you'll probably
170 * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
171 * using this routine after choosing which set of UTF-8 strings to use.
172 * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
173 * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
174 * characters (which are also widely used in C strings).
177 usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
179 struct usb_string *s;
182 /* descriptor 0 has the language id */
185 buf [1] = USB_DT_STRING;
186 buf [2] = (u8) table->language;
187 buf [3] = (u8) (table->language >> 8);
190 for (s = table->strings; s && s->s; s++)
194 /* unrecognized: stall. */
198 /* string descriptors have length, tag, then UTF16-LE text */
199 len = min ((size_t) 126, strlen (s->s));
200 memset (buf + 2, 0, 2 * len); /* zero all the bytes */
201 len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
204 buf [0] = (len + 1) * 2;
205 buf [1] = USB_DT_STRING;
210 /*-------------------------------------------------------------------------*/
211 /*-------------------------------------------------------------------------*/
215 * usb_descriptor_fillbuf - fill buffer with descriptors
216 * @buf: Buffer to be filled
217 * @buflen: Size of buf
218 * @src: Array of descriptor pointers, terminated by null pointer.
220 * Copies descriptors into the buffer, returning the length or a
221 * negative error code if they can't all be copied. Useful when
222 * assembling descriptors for an associated set of interfaces used
223 * as part of configuring a composite device; or in other cases where
224 * sets of descriptors need to be marshaled.
227 usb_descriptor_fillbuf(void *buf, unsigned buflen,
228 const struct usb_descriptor_header **src)
235 /* fill buffer from src[] until null descriptor ptr */
236 for (; 0 != *src; src++) {
237 unsigned len = (*src)->bLength;
241 memcpy(dest, *src, len);
245 return dest - (u8 *)buf;
250 * usb_gadget_config_buf - builts a complete configuration descriptor
251 * @config: Header for the descriptor, including characteristics such
252 * as power requirements and number of interfaces.
253 * @desc: Null-terminated vector of pointers to the descriptors (interface,
254 * endpoint, etc) defining all functions in this device configuration.
255 * @buf: Buffer for the resulting configuration descriptor.
256 * @length: Length of buffer. If this is not big enough to hold the
257 * entire configuration descriptor, an error code will be returned.
259 * This copies descriptors into the response buffer, building a descriptor
260 * for that configuration. It returns the buffer length or a negative
261 * status code. The config.wTotalLength field is set to match the length
262 * of the result, but other descriptor fields (including power usage and
263 * interface count) must be set by the caller.
265 * Gadget drivers could use this when constructing a config descriptor
266 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
267 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
269 int usb_gadget_config_buf(
270 const struct usb_config_descriptor *config,
273 const struct usb_descriptor_header **desc
276 struct usb_config_descriptor *cp = buf;
279 /* config descriptor first */
280 if (length < USB_DT_CONFIG_SIZE || !desc)
284 /* then interface/endpoint/class/vendor/... */
285 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
286 length - USB_DT_CONFIG_SIZE, desc);
289 len += USB_DT_CONFIG_SIZE;
293 /* patch up the config descriptor */
294 cp->bLength = USB_DT_CONFIG_SIZE;
295 cp->bDescriptorType = USB_DT_CONFIG;
296 cp->wTotalLength = cpu_to_le16(len);
297 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
301 /*-------------------------------------------------------------------------*/
302 /*-------------------------------------------------------------------------*/
305 #define RBUF_LEN (1024*1024)
306 static int rbuf_start;
308 static __u8 rbuf[RBUF_LEN];
310 /*-------------------------------------------------------------------------*/
312 #define DRIVER_VERSION "St Patrick's Day 2004"
314 static const char shortname [] = "zero";
315 static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
317 static const char source_sink [] = "source and sink data";
318 static const char loopback [] = "loop input to output";
320 /*-------------------------------------------------------------------------*/
323 * driver assumes self-powered hardware, and
324 * has no way for users to trigger remote wakeup.
326 * this version autoconfigures as much as possible,
327 * which is reasonable for most "bulk-only" drivers.
329 static const char *EP_IN_NAME; /* source */
330 static const char *EP_OUT_NAME; /* sink */
332 /*-------------------------------------------------------------------------*/
334 /* big enough to hold our biggest descriptor */
335 #define USB_BUFSIZ 512
339 struct usb_gadget *gadget;
340 struct usb_request *req; /* for control responses */
342 /* when configured, we have one of two configs:
343 * - source data (in to host) and sink it (out from host)
344 * - or loop it back (out from host back in to host)
347 struct usb_ep *in_ep, *out_ep;
349 /* autoresume timer */
350 struct timer_list resume;
353 #define xprintk(d,level,fmt,args...) \
354 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
357 #define DBG(dev,fmt,args...) \
358 xprintk(dev , KERN_DEBUG , fmt , ## args)
360 #define DBG(dev,fmt,args...) \
367 #define VDBG(dev,fmt,args...) \
371 #define ERROR(dev,fmt,args...) \
372 xprintk(dev , KERN_ERR , fmt , ## args)
373 #define WARN(dev,fmt,args...) \
374 xprintk(dev , KERN_WARNING , fmt , ## args)
375 #define INFO(dev,fmt,args...) \
376 xprintk(dev , KERN_INFO , fmt , ## args)
378 /*-------------------------------------------------------------------------*/
380 static unsigned buflen = 4096;
381 static unsigned qlen = 32;
382 static unsigned pattern = 0;
384 module_param (buflen, uint, S_IRUGO|S_IWUSR);
385 module_param (qlen, uint, S_IRUGO|S_IWUSR);
386 module_param (pattern, uint, S_IRUGO|S_IWUSR);
389 * if it's nonzero, autoresume says how many seconds to wait
390 * before trying to wake up the host after suspend.
392 static unsigned autoresume = 0;
393 module_param (autoresume, uint, 0);
396 * Normally the "loopback" configuration is second (index 1) so
397 * it's not the default. Here's where to change that order, to
398 * work better with hosts where config changes are problematic.
399 * Or controllers (like superh) that only support one config.
401 static int loopdefault = 0;
403 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
405 /*-------------------------------------------------------------------------*/
407 /* Thanks to NetChip Technologies for donating this product ID.
409 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
410 * Instead: allocate your own, using normal USB-IF procedures.
412 #ifndef CONFIG_USB_ZERO_HNPTEST
413 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
414 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
416 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
417 #define DRIVER_PRODUCT_NUM 0xbadd
420 /*-------------------------------------------------------------------------*/
423 * DESCRIPTORS ... most are static, but strings and (full)
424 * configuration descriptors are built on demand.
428 #define STRING_MANUFACTURER 25
429 #define STRING_PRODUCT 42
430 #define STRING_SERIAL 101
432 #define STRING_MANUFACTURER 1
433 #define STRING_PRODUCT 2
434 #define STRING_SERIAL 3
436 #define STRING_SOURCE_SINK 250
437 #define STRING_LOOPBACK 251
440 * This device advertises two configurations; these numbers work
441 * on a pxa250 as well as more flexible hardware.
443 #define CONFIG_SOURCE_SINK 3
444 #define CONFIG_LOOPBACK 2
447 static struct usb_device_descriptor
449 .bLength = sizeof device_desc,
450 .bDescriptorType = USB_DT_DEVICE,
452 .bcdUSB = __constant_cpu_to_le16 (0x0200),
453 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
455 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
456 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
457 .iManufacturer = STRING_MANUFACTURER,
458 .iProduct = STRING_PRODUCT,
459 .iSerialNumber = STRING_SERIAL,
460 .bNumConfigurations = 2,
463 static struct usb_device_descriptor
465 .bLength = sizeof device_desc,
466 .bDescriptorType = USB_DT_DEVICE,
467 .bcdUSB = __constant_cpu_to_le16 (0x0100),
468 .bDeviceClass = USB_CLASS_PER_INTERFACE,
469 .bDeviceSubClass = 0,
470 .bDeviceProtocol = 0,
471 .bMaxPacketSize0 = 64,
472 .bcdDevice = __constant_cpu_to_le16 (0x0100),
473 .idVendor = __constant_cpu_to_le16 (0x0499),
474 .idProduct = __constant_cpu_to_le16 (0x3002),
475 .iManufacturer = STRING_MANUFACTURER,
476 .iProduct = STRING_PRODUCT,
477 .iSerialNumber = STRING_SERIAL,
478 .bNumConfigurations = 1,
481 static struct usb_config_descriptor
483 .bLength = sizeof z_config,
484 .bDescriptorType = USB_DT_CONFIG,
486 /* compute wTotalLength on the fly */
488 .bConfigurationValue = 1,
490 .bmAttributes = 0x40,
491 .bMaxPower = 0, /* self-powered */
495 static struct usb_otg_descriptor
497 .bLength = sizeof otg_descriptor,
498 .bDescriptorType = USB_DT_OTG,
500 .bmAttributes = USB_OTG_SRP,
503 /* one interface in each configuration */
504 #ifdef CONFIG_USB_GADGET_DUALSPEED
507 * usb 2.0 devices need to expose both high speed and full speed
508 * descriptors, unless they only run at full speed.
510 * that means alternate endpoint descriptors (bigger packets)
511 * and a "device qualifier" ... plus more construction options
512 * for the config descriptor.
515 static struct usb_qualifier_descriptor
517 .bLength = sizeof dev_qualifier,
518 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
520 .bcdUSB = __constant_cpu_to_le16 (0x0200),
521 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
523 .bNumConfigurations = 2,
527 struct usb_cs_as_general_descriptor {
529 __u8 bDescriptorType;
531 __u8 bDescriptorSubType;
535 } __attribute__ ((packed));
537 struct usb_cs_as_format_descriptor {
539 __u8 bDescriptorType;
541 __u8 bDescriptorSubType;
547 __u8 tLowerSamFreq[3];
548 __u8 tUpperSamFreq[3];
549 } __attribute__ ((packed));
551 static const struct usb_interface_descriptor
552 z_audio_control_if_desc = {
553 .bLength = sizeof z_audio_control_if_desc,
554 .bDescriptorType = USB_DT_INTERFACE,
555 .bInterfaceNumber = 0,
556 .bAlternateSetting = 0,
558 .bInterfaceClass = USB_CLASS_AUDIO,
559 .bInterfaceSubClass = 0x1,
560 .bInterfaceProtocol = 0,
564 static const struct usb_interface_descriptor
566 .bLength = sizeof z_audio_if_desc,
567 .bDescriptorType = USB_DT_INTERFACE,
568 .bInterfaceNumber = 1,
569 .bAlternateSetting = 0,
571 .bInterfaceClass = USB_CLASS_AUDIO,
572 .bInterfaceSubClass = 0x2,
573 .bInterfaceProtocol = 0,
577 static const struct usb_interface_descriptor
579 .bLength = sizeof z_audio_if_desc,
580 .bDescriptorType = USB_DT_INTERFACE,
581 .bInterfaceNumber = 1,
582 .bAlternateSetting = 1,
584 .bInterfaceClass = USB_CLASS_AUDIO,
585 .bInterfaceSubClass = 0x2,
586 .bInterfaceProtocol = 0,
590 static const struct usb_cs_as_general_descriptor
591 z_audio_cs_as_if_desc = {
593 .bDescriptorType = 0x24,
595 .bDescriptorSubType = 0x01,
596 .bTerminalLink = 0x01,
598 .wFormatTag = __constant_cpu_to_le16 (0x0001)
602 static const struct usb_cs_as_format_descriptor
603 z_audio_cs_as_format_desc = {
605 .bDescriptorType = 0x24,
607 .bDescriptorSubType = 2,
613 .tLowerSamFreq = {0x7e, 0x13, 0x00},
614 .tUpperSamFreq = {0xe2, 0xd6, 0x00},
617 static const struct usb_endpoint_descriptor
620 .bDescriptorType = 0x05,
621 .bEndpointAddress = 0x04,
622 .bmAttributes = 0x09,
623 .wMaxPacketSize = 0x0038,
626 .bSynchAddress = 0x00,
629 static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
632 static char z_ac_interface_header_desc[] =
633 { 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
636 static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
637 0x03, 0x00, 0x00, 0x00};
639 static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
640 0x02, 0x00, 0x02, 0x00, 0x00};
642 static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
645 static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
648 static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
650 static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
651 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
653 static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
656 static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
658 static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
661 static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
663 static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
664 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
666 static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
669 static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
671 static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
674 static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
676 static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
677 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
679 static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
682 static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
684 static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
687 static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
689 static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
690 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
692 static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
695 static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
697 static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
700 static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
702 static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
703 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
705 static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
708 static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
712 static const struct usb_descriptor_header *z_function [] = {
713 (struct usb_descriptor_header *) &z_audio_control_if_desc,
714 (struct usb_descriptor_header *) &z_ac_interface_header_desc,
715 (struct usb_descriptor_header *) &z_0,
716 (struct usb_descriptor_header *) &z_1,
717 (struct usb_descriptor_header *) &z_2,
718 (struct usb_descriptor_header *) &z_audio_if_desc,
719 (struct usb_descriptor_header *) &z_audio_if_desc2,
720 (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
721 (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
722 (struct usb_descriptor_header *) &z_iso_ep,
723 (struct usb_descriptor_header *) &z_iso_ep2,
724 (struct usb_descriptor_header *) &za_0,
725 (struct usb_descriptor_header *) &za_1,
726 (struct usb_descriptor_header *) &za_2,
727 (struct usb_descriptor_header *) &za_3,
728 (struct usb_descriptor_header *) &za_4,
729 (struct usb_descriptor_header *) &za_5,
730 (struct usb_descriptor_header *) &za_6,
731 (struct usb_descriptor_header *) &za_7,
732 (struct usb_descriptor_header *) &za_8,
733 (struct usb_descriptor_header *) &za_9,
734 (struct usb_descriptor_header *) &za_10,
735 (struct usb_descriptor_header *) &za_11,
736 (struct usb_descriptor_header *) &za_12,
737 (struct usb_descriptor_header *) &za_13,
738 (struct usb_descriptor_header *) &za_14,
739 (struct usb_descriptor_header *) &za_15,
740 (struct usb_descriptor_header *) &za_16,
741 (struct usb_descriptor_header *) &za_17,
742 (struct usb_descriptor_header *) &za_18,
743 (struct usb_descriptor_header *) &za_19,
744 (struct usb_descriptor_header *) &za_20,
745 (struct usb_descriptor_header *) &za_21,
746 (struct usb_descriptor_header *) &za_22,
747 (struct usb_descriptor_header *) &za_23,
748 (struct usb_descriptor_header *) &za_24,
752 /* maxpacket and other transfer characteristics vary by speed. */
753 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
757 /* if there's no high speed support, maxpacket doesn't change. */
758 #define ep_desc(g,hs,fs) fs
760 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
762 static char manufacturer [40];
763 //static char serial [40];
764 static char serial [] = "Ser 00 em";
766 /* static strings, in UTF-8 */
767 static struct usb_string strings [] = {
768 { STRING_MANUFACTURER, manufacturer, },
769 { STRING_PRODUCT, longname, },
770 { STRING_SERIAL, serial, },
771 { STRING_LOOPBACK, loopback, },
772 { STRING_SOURCE_SINK, source_sink, },
773 { } /* end of list */
776 static struct usb_gadget_strings stringtab = {
777 .language = 0x0409, /* en-us */
782 * config descriptors are also handcrafted. these must agree with code
783 * that sets configurations, and with code managing interfaces and their
784 * altsettings. other complexity may come from:
786 * - high speed support, including "other speed config" rules
787 * - multiple configurations
788 * - interfaces with alternate settings
789 * - embedded class or vendor-specific descriptors
791 * this handles high speed, and has a second config that could as easily
792 * have been an alternate interface setting (on most hardware).
794 * NOTE: to demonstrate (and test) more USB capabilities, this driver
795 * should include an altsetting to test interrupt transfers, including
796 * high bandwidth modes at high speed. (Maybe work like Intel's test
800 config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
803 const struct usb_descriptor_header **function;
805 function = z_function;
806 len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
809 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
813 /*-------------------------------------------------------------------------*/
815 static struct usb_request *
816 alloc_ep_req (struct usb_ep *ep, unsigned length)
818 struct usb_request *req;
820 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
822 req->length = length;
823 req->buf = usb_ep_alloc_buffer (ep, length,
824 &req->dma, GFP_ATOMIC);
826 usb_ep_free_request (ep, req);
833 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
836 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
837 usb_ep_free_request (ep, req);
840 /*-------------------------------------------------------------------------*/
842 /* optionally require specific source/sink data patterns */
846 struct zero_dev *dev,
848 struct usb_request *req
854 for (i = 0; i < req->actual; i++, buf++) {
856 /* all-zeroes has no synchronization issues */
861 /* mod63 stays in sync with short-terminated transfers,
862 * or otherwise when host and gadget agree on how large
863 * each usb transfer request should be. resync is done
864 * with set_interface or set_config.
867 if (*buf == (u8)(i % 63))
871 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
872 usb_ep_set_halt (ep);
878 /*-------------------------------------------------------------------------*/
880 static void zero_reset_config (struct zero_dev *dev)
882 if (dev->config == 0)
885 DBG (dev, "reset config\n");
887 /* just disable endpoints, forcing completion of pending i/o.
888 * all our completion handlers free their requests in this case.
891 usb_ep_disable (dev->in_ep);
895 usb_ep_disable (dev->out_ep);
899 del_timer (&dev->resume);
902 #define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
905 zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
907 struct zero_dev *dev = ep->driver_data;
908 int status = req->status;
913 case 0: /* normal completion? */
914 //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
915 for (i=0, j=rbuf_start; i<req->actual; i++) {
916 //printk ("%02x ", ((__u8*)req->buf)[i]);
917 rbuf[j] = ((__u8*)req->buf)[i];
919 if (j >= RBUF_LEN) j=0;
924 if (rbuf_len < RBUF_LEN) {
925 rbuf_len += req->actual;
926 if (rbuf_len > RBUF_LEN) {
933 /* this endpoint is normally active while we're configured */
934 case -ECONNABORTED: /* hardware forced ep reset */
935 case -ECONNRESET: /* request dequeued */
936 case -ESHUTDOWN: /* disconnect from host */
937 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
938 req->actual, req->length);
939 if (ep == dev->out_ep)
940 check_read_data (dev, ep, req);
941 free_ep_req (ep, req);
944 case -EOVERFLOW: /* buffer overrun on read means that
945 * we didn't provide a big enough
950 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
951 status, req->actual, req->length);
953 case -EREMOTEIO: /* short read */
957 status = usb_ep_queue (ep, req, GFP_ATOMIC);
959 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
960 ep->name, req->length, status);
961 usb_ep_set_halt (ep);
962 /* FIXME recover later ... somehow */
966 static struct usb_request *
967 zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
969 struct usb_request *req;
972 req = alloc_ep_req (ep, 512);
976 req->complete = zero_isoc_complete;
978 status = usb_ep_queue (ep, req, gfp_flags);
980 struct zero_dev *dev = ep->driver_data;
982 ERROR (dev, "start %s --> %d\n", ep->name, status);
983 free_ep_req (ep, req);
990 /* change our operational config. this code must agree with the code
991 * that returns config descriptors, and altsetting code.
993 * it's also responsible for power management interactions. some
994 * configurations might not work with our current power sources.
996 * note that some device controller hardware will constrain what this
997 * code can do, perhaps by disallowing more than one configuration or
998 * by limiting configuration choices (like the pxa2xx).
1001 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
1004 struct usb_gadget *gadget = dev->gadget;
1005 const struct usb_endpoint_descriptor *d;
1008 if (number == dev->config)
1011 zero_reset_config (dev);
1013 gadget_for_each_ep (ep, gadget) {
1015 if (strcmp (ep->name, "ep4") == 0) {
1017 d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
1018 result = usb_ep_enable (ep, d);
1021 ep->driver_data = dev;
1024 if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
1030 usb_ep_disable (ep);
1037 dev->config = number;
1041 /*-------------------------------------------------------------------------*/
1043 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
1045 if (req->status || req->actual != req->length)
1046 DBG ((struct zero_dev *) ep->driver_data,
1047 "setup complete --> %d, %d/%d\n",
1048 req->status, req->actual, req->length);
1052 * The setup() callback implements all the ep0 functionality that's
1053 * not handled lower down, in hardware or the hardware driver (like
1054 * device and endpoint feature flags, and their status). It's all
1055 * housekeeping for the gadget function we're implementing. Most of
1056 * the work is in config-specific setup.
1059 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1061 struct zero_dev *dev = get_gadget_data (gadget);
1062 struct usb_request *req = dev->req;
1063 int value = -EOPNOTSUPP;
1065 /* usually this stores reply data in the pre-allocated ep0 buffer,
1066 * but config change events will reconfigure hardware.
1069 switch (ctrl->bRequest) {
1071 case USB_REQ_GET_DESCRIPTOR:
1073 switch (ctrl->wValue >> 8) {
1076 value = min (ctrl->wLength, (u16) sizeof device_desc);
1077 memcpy (req->buf, &device_desc, value);
1079 #ifdef CONFIG_USB_GADGET_DUALSPEED
1080 case USB_DT_DEVICE_QUALIFIER:
1081 if (!gadget->is_dualspeed)
1083 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1084 memcpy (req->buf, &dev_qualifier, value);
1087 case USB_DT_OTHER_SPEED_CONFIG:
1088 if (!gadget->is_dualspeed)
1091 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1093 value = config_buf (gadget, req->buf,
1095 ctrl->wValue & 0xff);
1097 value = min (ctrl->wLength, (u16) value);
1101 /* wIndex == language code.
1102 * this driver only handles one language, you can
1103 * add string tables for other languages, using
1104 * any UTF-8 characters
1106 value = usb_gadget_get_string (&stringtab,
1107 ctrl->wValue & 0xff, req->buf);
1109 value = min (ctrl->wLength, (u16) value);
1115 /* currently two configs, two speeds */
1116 case USB_REQ_SET_CONFIGURATION:
1117 if (ctrl->bRequestType != 0)
1120 spin_lock (&dev->lock);
1121 value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1122 spin_unlock (&dev->lock);
1124 case USB_REQ_GET_CONFIGURATION:
1125 if (ctrl->bRequestType != USB_DIR_IN)
1127 *(u8 *)req->buf = dev->config;
1128 value = min (ctrl->wLength, (u16) 1);
1131 /* until we add altsetting support, or other interfaces,
1132 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1133 * and already killed pending endpoint I/O.
1135 case USB_REQ_SET_INTERFACE:
1137 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1139 spin_lock (&dev->lock);
1141 u8 config = dev->config;
1143 /* resets interface configuration, forgets about
1144 * previous transaction state (queued bufs, etc)
1145 * and re-inits endpoint state (toggle etc)
1146 * no response queued, just zero status == success.
1147 * if we had more than one interface we couldn't
1148 * use this "reset the config" shortcut.
1150 zero_reset_config (dev);
1151 zero_set_config (dev, config, GFP_ATOMIC);
1154 spin_unlock (&dev->lock);
1156 case USB_REQ_GET_INTERFACE:
1157 if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
1158 value = ctrl->wLength;
1162 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1166 if (ctrl->wIndex != 0) {
1170 *(u8 *)req->buf = 0;
1171 value = min (ctrl->wLength, (u16) 1);
1176 * These are the same vendor-specific requests supported by
1177 * Intel's USB 2.0 compliance test devices. We exceed that
1178 * device spec by allowing multiple-packet requests.
1180 case 0x5b: /* control WRITE test -- fill the buffer */
1181 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1183 if (ctrl->wValue || ctrl->wIndex)
1185 /* just read that many bytes into the buffer */
1186 if (ctrl->wLength > USB_BUFSIZ)
1188 value = ctrl->wLength;
1190 case 0x5c: /* control READ test -- return the buffer */
1191 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1193 if (ctrl->wValue || ctrl->wIndex)
1195 /* expect those bytes are still in the buffer; send back */
1196 if (ctrl->wLength > USB_BUFSIZ
1197 || ctrl->wLength != req->length)
1199 value = ctrl->wLength;
1202 case 0x01: // SET_CUR
1207 value = ctrl->wLength;
1210 switch (ctrl->wValue) {
1213 ((u8*)req->buf)[0] = 0x00;
1214 ((u8*)req->buf)[1] = 0xe3;
1218 ((u8*)req->buf)[0] = 0x00;
1221 //((u8*)req->buf)[0] = 0x81;
1222 //((u8*)req->buf)[1] = 0x81;
1223 value = ctrl->wLength;
1226 switch (ctrl->wValue) {
1229 ((u8*)req->buf)[0] = 0x00;
1230 ((u8*)req->buf)[1] = 0xc3;
1234 ((u8*)req->buf)[0] = 0x00;
1237 //((u8*)req->buf)[0] = 0x82;
1238 //((u8*)req->buf)[1] = 0x82;
1239 value = ctrl->wLength;
1242 switch (ctrl->wValue) {
1245 ((u8*)req->buf)[0] = 0x00;
1246 ((u8*)req->buf)[1] = 0x00;
1249 ((u8*)req->buf)[0] = 0x60;
1252 ((u8*)req->buf)[0] = 0x18;
1255 //((u8*)req->buf)[0] = 0x83;
1256 //((u8*)req->buf)[1] = 0x83;
1257 value = ctrl->wLength;
1260 switch (ctrl->wValue) {
1263 ((u8*)req->buf)[0] = 0x00;
1264 ((u8*)req->buf)[1] = 0x01;
1268 ((u8*)req->buf)[0] = 0x08;
1271 //((u8*)req->buf)[0] = 0x84;
1272 //((u8*)req->buf)[1] = 0x84;
1273 value = ctrl->wLength;
1276 ((u8*)req->buf)[0] = 0x85;
1277 ((u8*)req->buf)[1] = 0x85;
1278 value = ctrl->wLength;
1284 printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
1285 ctrl->bRequestType, ctrl->bRequest,
1286 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1289 /* respond with data transfer before status phase? */
1291 req->length = value;
1292 req->zero = value < ctrl->wLength
1293 && (value % gadget->ep0->maxpacket) == 0;
1294 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1296 DBG (dev, "ep_queue < 0 --> %d\n", value);
1298 zero_setup_complete (gadget->ep0, req);
1302 /* device either stalls (value < 0) or reports success */
1307 zero_disconnect (struct usb_gadget *gadget)
1309 struct zero_dev *dev = get_gadget_data (gadget);
1310 unsigned long flags;
1312 spin_lock_irqsave (&dev->lock, flags);
1313 zero_reset_config (dev);
1315 /* a more significant application might have some non-usb
1316 * activities to quiesce here, saving resources like power
1317 * or pushing the notification up a network stack.
1319 spin_unlock_irqrestore (&dev->lock, flags);
1321 /* next we may get setup() calls to enumerate new connections;
1322 * or an unbind() during shutdown (including removing module).
1327 zero_autoresume (unsigned long _dev)
1329 struct zero_dev *dev = (struct zero_dev *) _dev;
1332 /* normally the host would be woken up for something
1333 * more significant than just a timer firing...
1335 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1336 status = usb_gadget_wakeup (dev->gadget);
1337 DBG (dev, "wakeup --> %d\n", status);
1341 /*-------------------------------------------------------------------------*/
1344 zero_unbind (struct usb_gadget *gadget)
1346 struct zero_dev *dev = get_gadget_data (gadget);
1348 DBG (dev, "unbind\n");
1350 /* we've already been disconnected ... no i/o is active */
1352 free_ep_req (gadget->ep0, dev->req);
1353 del_timer_sync (&dev->resume);
1355 set_gadget_data (gadget, NULL);
1359 zero_bind (struct usb_gadget *gadget)
1361 struct zero_dev *dev;
1362 //struct usb_ep *ep;
1364 printk("binding\n");
1366 * DRIVER POLICY CHOICE: you may want to do this differently.
1367 * One thing to avoid is reusing a bcdDevice revision code
1368 * with different host-visible configurations or behavior
1369 * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1371 //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
1374 /* ok, we made sense of the hardware ... */
1375 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1378 memset (dev, 0, sizeof *dev);
1379 spin_lock_init (&dev->lock);
1380 dev->gadget = gadget;
1381 set_gadget_data (gadget, dev);
1383 /* preallocate control response and buffer */
1384 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1387 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1388 &dev->req->dma, GFP_KERNEL);
1392 dev->req->complete = zero_setup_complete;
1394 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1396 #ifdef CONFIG_USB_GADGET_DUALSPEED
1397 /* assume ep0 uses the same value for both speeds ... */
1398 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1400 /* and that all endpoints are dual-speed */
1401 //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1402 //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1405 usb_gadget_set_selfpowered (gadget);
1407 init_timer (&dev->resume);
1408 dev->resume.function = zero_autoresume;
1409 dev->resume.data = (unsigned long) dev;
1411 gadget->ep0->driver_data = dev;
1413 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1414 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1415 EP_OUT_NAME, EP_IN_NAME);
1417 snprintf (manufacturer, sizeof manufacturer,
1418 UTS_SYSNAME " " UTS_RELEASE " with %s",
1424 zero_unbind (gadget);
1428 /*-------------------------------------------------------------------------*/
1431 zero_suspend (struct usb_gadget *gadget)
1433 struct zero_dev *dev = get_gadget_data (gadget);
1435 if (gadget->speed == USB_SPEED_UNKNOWN)
1439 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1440 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1442 DBG (dev, "suspend\n");
1446 zero_resume (struct usb_gadget *gadget)
1448 struct zero_dev *dev = get_gadget_data (gadget);
1450 DBG (dev, "resume\n");
1451 del_timer (&dev->resume);
1455 /*-------------------------------------------------------------------------*/
1457 static struct usb_gadget_driver zero_driver = {
1458 #ifdef CONFIG_USB_GADGET_DUALSPEED
1459 .speed = USB_SPEED_HIGH,
1461 .speed = USB_SPEED_FULL,
1463 .function = (char *) longname,
1465 .unbind = zero_unbind,
1467 .setup = zero_setup,
1468 .disconnect = zero_disconnect,
1470 .suspend = zero_suspend,
1471 .resume = zero_resume,
1474 .name = (char *) shortname,
1481 MODULE_AUTHOR ("David Brownell");
1482 MODULE_LICENSE ("Dual BSD/GPL");
1484 static struct proc_dir_entry *pdir, *pfile;
1486 static int isoc_read_data (char *page, char **start,
1487 off_t off, int count,
1488 int *eof, void *data)
1492 static int done = 0;
1496 printk ("\ncount: %d\n", count);
1497 printk ("rbuf_start: %d\n", rbuf_start);
1498 printk ("rbuf_len: %d\n", rbuf_len);
1499 printk ("off: %d\n", off);
1500 printk ("start: %p\n\n", *start);
1510 if (rbuf_len == RBUF_LEN)
1515 for (i=0; i<count && c<rbuf_len; i++, c++) {
1516 page[i] = rbuf[(c+s) % RBUF_LEN];
1520 if (c >= rbuf_len) {
1529 static int __init init (void)
1534 pdir = proc_mkdir("isoc_test", NULL);
1537 printk("Error creating dir\n");
1540 pdir->owner = THIS_MODULE;
1542 pfile = create_proc_read_entry("isoc_data",
1546 if (pfile == NULL) {
1548 printk("Error creating file\n");
1551 pfile->owner = THIS_MODULE;
1553 return usb_gadget_register_driver (&zero_driver);
1556 remove_proc_entry("isoc_data", NULL);
1562 static void __exit cleanup (void)
1565 usb_gadget_unregister_driver (&zero_driver);
1567 remove_proc_entry("isoc_data", pdir);
1568 remove_proc_entry("isoc_test", NULL);
1570 module_exit (cleanup);