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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
88 # include <linux/usb/ch9.h>
90 # include <linux/usb_ch9.h>
93 #include <linux/usb_gadget.h>
96 /*-------------------------------------------------------------------------*/
97 /*-------------------------------------------------------------------------*/
100 static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
106 /* this insists on correct encodings, though not minimal ones.
107 * BUT it currently rejects legit 4-byte UTF-8 code points,
108 * which need surrogate pairs. (Unicode 3.1 can use them.)
110 while (len != 0 && (c = (u8) *s++) != 0) {
111 if (unlikely(c & 0x80)) {
113 // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
114 if ((c & 0xe0) == 0xc0) {
115 uchar = (c & 0x1f) << 6;
118 if ((c & 0xc0) != 0xc0)
123 // 3-byte sequence (most CJKV characters):
124 // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
125 } else if ((c & 0xf0) == 0xe0) {
126 uchar = (c & 0x0f) << 12;
129 if ((c & 0xc0) != 0xc0)
135 if ((c & 0xc0) != 0xc0)
140 /* no bogus surrogates */
141 if (0xd800 <= uchar && uchar <= 0xdfff)
144 // 4-byte sequence (surrogate pairs, currently rare):
145 // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
146 // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
147 // (uuuuu = wwww + 1)
148 // FIXME accept the surrogate code points (only)
154 put_unaligned (cpu_to_le16 (uchar), cp++);
165 * usb_gadget_get_string - fill out a string descriptor
166 * @table: of c strings encoded using UTF-8
167 * @id: string id, from low byte of wValue in get string descriptor
168 * @buf: at least 256 bytes
170 * Finds the UTF-8 string matching the ID, and converts it into a
171 * string descriptor in utf16-le.
172 * Returns length of descriptor (always even) or negative errno
174 * If your driver needs stings in multiple languages, you'll probably
175 * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
176 * using this routine after choosing which set of UTF-8 strings to use.
177 * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
178 * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
179 * characters (which are also widely used in C strings).
182 usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
184 struct usb_string *s;
187 /* descriptor 0 has the language id */
190 buf [1] = USB_DT_STRING;
191 buf [2] = (u8) table->language;
192 buf [3] = (u8) (table->language >> 8);
195 for (s = table->strings; s && s->s; s++)
199 /* unrecognized: stall. */
203 /* string descriptors have length, tag, then UTF16-LE text */
204 len = min ((size_t) 126, strlen (s->s));
205 memset (buf + 2, 0, 2 * len); /* zero all the bytes */
206 len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
209 buf [0] = (len + 1) * 2;
210 buf [1] = USB_DT_STRING;
215 /*-------------------------------------------------------------------------*/
216 /*-------------------------------------------------------------------------*/
220 * usb_descriptor_fillbuf - fill buffer with descriptors
221 * @buf: Buffer to be filled
222 * @buflen: Size of buf
223 * @src: Array of descriptor pointers, terminated by null pointer.
225 * Copies descriptors into the buffer, returning the length or a
226 * negative error code if they can't all be copied. Useful when
227 * assembling descriptors for an associated set of interfaces used
228 * as part of configuring a composite device; or in other cases where
229 * sets of descriptors need to be marshaled.
232 usb_descriptor_fillbuf(void *buf, unsigned buflen,
233 const struct usb_descriptor_header **src)
240 /* fill buffer from src[] until null descriptor ptr */
241 for (; 0 != *src; src++) {
242 unsigned len = (*src)->bLength;
246 memcpy(dest, *src, len);
250 return dest - (u8 *)buf;
255 * usb_gadget_config_buf - builts a complete configuration descriptor
256 * @config: Header for the descriptor, including characteristics such
257 * as power requirements and number of interfaces.
258 * @desc: Null-terminated vector of pointers to the descriptors (interface,
259 * endpoint, etc) defining all functions in this device configuration.
260 * @buf: Buffer for the resulting configuration descriptor.
261 * @length: Length of buffer. If this is not big enough to hold the
262 * entire configuration descriptor, an error code will be returned.
264 * This copies descriptors into the response buffer, building a descriptor
265 * for that configuration. It returns the buffer length or a negative
266 * status code. The config.wTotalLength field is set to match the length
267 * of the result, but other descriptor fields (including power usage and
268 * interface count) must be set by the caller.
270 * Gadget drivers could use this when constructing a config descriptor
271 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
272 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
274 int usb_gadget_config_buf(
275 const struct usb_config_descriptor *config,
278 const struct usb_descriptor_header **desc
281 struct usb_config_descriptor *cp = buf;
284 /* config descriptor first */
285 if (length < USB_DT_CONFIG_SIZE || !desc)
289 /* then interface/endpoint/class/vendor/... */
290 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
291 length - USB_DT_CONFIG_SIZE, desc);
294 len += USB_DT_CONFIG_SIZE;
298 /* patch up the config descriptor */
299 cp->bLength = USB_DT_CONFIG_SIZE;
300 cp->bDescriptorType = USB_DT_CONFIG;
301 cp->wTotalLength = cpu_to_le16(len);
302 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
306 /*-------------------------------------------------------------------------*/
307 /*-------------------------------------------------------------------------*/
310 #define RBUF_LEN (1024*1024)
311 static int rbuf_start;
313 static __u8 rbuf[RBUF_LEN];
315 /*-------------------------------------------------------------------------*/
317 #define DRIVER_VERSION "St Patrick's Day 2004"
319 static const char shortname [] = "zero";
320 static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
322 static const char source_sink [] = "source and sink data";
323 static const char loopback [] = "loop input to output";
325 /*-------------------------------------------------------------------------*/
328 * driver assumes self-powered hardware, and
329 * has no way for users to trigger remote wakeup.
331 * this version autoconfigures as much as possible,
332 * which is reasonable for most "bulk-only" drivers.
334 static const char *EP_IN_NAME; /* source */
335 static const char *EP_OUT_NAME; /* sink */
337 /*-------------------------------------------------------------------------*/
339 /* big enough to hold our biggest descriptor */
340 #define USB_BUFSIZ 512
344 struct usb_gadget *gadget;
345 struct usb_request *req; /* for control responses */
347 /* when configured, we have one of two configs:
348 * - source data (in to host) and sink it (out from host)
349 * - or loop it back (out from host back in to host)
352 struct usb_ep *in_ep, *out_ep;
354 /* autoresume timer */
355 struct timer_list resume;
358 #define xprintk(d,level,fmt,args...) \
359 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
362 #define DBG(dev,fmt,args...) \
363 xprintk(dev , KERN_DEBUG , fmt , ## args)
365 #define DBG(dev,fmt,args...) \
372 #define VDBG(dev,fmt,args...) \
376 #define ERROR(dev,fmt,args...) \
377 xprintk(dev , KERN_ERR , fmt , ## args)
378 #define WARN(dev,fmt,args...) \
379 xprintk(dev , KERN_WARNING , fmt , ## args)
380 #define INFO(dev,fmt,args...) \
381 xprintk(dev , KERN_INFO , fmt , ## args)
383 /*-------------------------------------------------------------------------*/
385 static unsigned buflen = 4096;
386 static unsigned qlen = 32;
387 static unsigned pattern = 0;
389 module_param (buflen, uint, S_IRUGO|S_IWUSR);
390 module_param (qlen, uint, S_IRUGO|S_IWUSR);
391 module_param (pattern, uint, S_IRUGO|S_IWUSR);
394 * if it's nonzero, autoresume says how many seconds to wait
395 * before trying to wake up the host after suspend.
397 static unsigned autoresume = 0;
398 module_param (autoresume, uint, 0);
401 * Normally the "loopback" configuration is second (index 1) so
402 * it's not the default. Here's where to change that order, to
403 * work better with hosts where config changes are problematic.
404 * Or controllers (like superh) that only support one config.
406 static int loopdefault = 0;
408 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
410 /*-------------------------------------------------------------------------*/
412 /* Thanks to NetChip Technologies for donating this product ID.
414 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
415 * Instead: allocate your own, using normal USB-IF procedures.
417 #ifndef CONFIG_USB_ZERO_HNPTEST
418 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
419 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
421 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
422 #define DRIVER_PRODUCT_NUM 0xbadd
425 /*-------------------------------------------------------------------------*/
428 * DESCRIPTORS ... most are static, but strings and (full)
429 * configuration descriptors are built on demand.
433 #define STRING_MANUFACTURER 25
434 #define STRING_PRODUCT 42
435 #define STRING_SERIAL 101
437 #define STRING_MANUFACTURER 1
438 #define STRING_PRODUCT 2
439 #define STRING_SERIAL 3
441 #define STRING_SOURCE_SINK 250
442 #define STRING_LOOPBACK 251
445 * This device advertises two configurations; these numbers work
446 * on a pxa250 as well as more flexible hardware.
448 #define CONFIG_SOURCE_SINK 3
449 #define CONFIG_LOOPBACK 2
452 static struct usb_device_descriptor
454 .bLength = sizeof device_desc,
455 .bDescriptorType = USB_DT_DEVICE,
457 .bcdUSB = __constant_cpu_to_le16 (0x0200),
458 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
460 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
461 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
462 .iManufacturer = STRING_MANUFACTURER,
463 .iProduct = STRING_PRODUCT,
464 .iSerialNumber = STRING_SERIAL,
465 .bNumConfigurations = 2,
468 static struct usb_device_descriptor
470 .bLength = sizeof device_desc,
471 .bDescriptorType = USB_DT_DEVICE,
472 .bcdUSB = __constant_cpu_to_le16 (0x0100),
473 .bDeviceClass = USB_CLASS_PER_INTERFACE,
474 .bDeviceSubClass = 0,
475 .bDeviceProtocol = 0,
476 .bMaxPacketSize0 = 64,
477 .bcdDevice = __constant_cpu_to_le16 (0x0100),
478 .idVendor = __constant_cpu_to_le16 (0x0499),
479 .idProduct = __constant_cpu_to_le16 (0x3002),
480 .iManufacturer = STRING_MANUFACTURER,
481 .iProduct = STRING_PRODUCT,
482 .iSerialNumber = STRING_SERIAL,
483 .bNumConfigurations = 1,
486 static struct usb_config_descriptor
488 .bLength = sizeof z_config,
489 .bDescriptorType = USB_DT_CONFIG,
491 /* compute wTotalLength on the fly */
493 .bConfigurationValue = 1,
495 .bmAttributes = 0x40,
496 .bMaxPower = 0, /* self-powered */
500 static struct usb_otg_descriptor
502 .bLength = sizeof otg_descriptor,
503 .bDescriptorType = USB_DT_OTG,
505 .bmAttributes = USB_OTG_SRP,
508 /* one interface in each configuration */
509 #ifdef CONFIG_USB_GADGET_DUALSPEED
512 * usb 2.0 devices need to expose both high speed and full speed
513 * descriptors, unless they only run at full speed.
515 * that means alternate endpoint descriptors (bigger packets)
516 * and a "device qualifier" ... plus more construction options
517 * for the config descriptor.
520 static struct usb_qualifier_descriptor
522 .bLength = sizeof dev_qualifier,
523 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
525 .bcdUSB = __constant_cpu_to_le16 (0x0200),
526 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
528 .bNumConfigurations = 2,
532 struct usb_cs_as_general_descriptor {
534 __u8 bDescriptorType;
536 __u8 bDescriptorSubType;
540 } __attribute__ ((packed));
542 struct usb_cs_as_format_descriptor {
544 __u8 bDescriptorType;
546 __u8 bDescriptorSubType;
552 __u8 tLowerSamFreq[3];
553 __u8 tUpperSamFreq[3];
554 } __attribute__ ((packed));
556 static const struct usb_interface_descriptor
557 z_audio_control_if_desc = {
558 .bLength = sizeof z_audio_control_if_desc,
559 .bDescriptorType = USB_DT_INTERFACE,
560 .bInterfaceNumber = 0,
561 .bAlternateSetting = 0,
563 .bInterfaceClass = USB_CLASS_AUDIO,
564 .bInterfaceSubClass = 0x1,
565 .bInterfaceProtocol = 0,
569 static const struct usb_interface_descriptor
571 .bLength = sizeof z_audio_if_desc,
572 .bDescriptorType = USB_DT_INTERFACE,
573 .bInterfaceNumber = 1,
574 .bAlternateSetting = 0,
576 .bInterfaceClass = USB_CLASS_AUDIO,
577 .bInterfaceSubClass = 0x2,
578 .bInterfaceProtocol = 0,
582 static const struct usb_interface_descriptor
584 .bLength = sizeof z_audio_if_desc,
585 .bDescriptorType = USB_DT_INTERFACE,
586 .bInterfaceNumber = 1,
587 .bAlternateSetting = 1,
589 .bInterfaceClass = USB_CLASS_AUDIO,
590 .bInterfaceSubClass = 0x2,
591 .bInterfaceProtocol = 0,
595 static const struct usb_cs_as_general_descriptor
596 z_audio_cs_as_if_desc = {
598 .bDescriptorType = 0x24,
600 .bDescriptorSubType = 0x01,
601 .bTerminalLink = 0x01,
603 .wFormatTag = __constant_cpu_to_le16 (0x0001)
607 static const struct usb_cs_as_format_descriptor
608 z_audio_cs_as_format_desc = {
610 .bDescriptorType = 0x24,
612 .bDescriptorSubType = 2,
618 .tLowerSamFreq = {0x7e, 0x13, 0x00},
619 .tUpperSamFreq = {0xe2, 0xd6, 0x00},
622 static const struct usb_endpoint_descriptor
625 .bDescriptorType = 0x05,
626 .bEndpointAddress = 0x04,
627 .bmAttributes = 0x09,
628 .wMaxPacketSize = 0x0038,
631 .bSynchAddress = 0x00,
634 static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
637 static char z_ac_interface_header_desc[] =
638 { 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
641 static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
642 0x03, 0x00, 0x00, 0x00};
644 static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
645 0x02, 0x00, 0x02, 0x00, 0x00};
647 static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
650 static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
653 static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
655 static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
656 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
658 static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
661 static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
663 static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
666 static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
668 static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
669 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
671 static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
674 static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
676 static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
679 static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
681 static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
682 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
684 static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
687 static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
689 static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
692 static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
694 static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
695 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
697 static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
700 static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
702 static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
705 static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
707 static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
708 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
710 static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
713 static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
717 static const struct usb_descriptor_header *z_function [] = {
718 (struct usb_descriptor_header *) &z_audio_control_if_desc,
719 (struct usb_descriptor_header *) &z_ac_interface_header_desc,
720 (struct usb_descriptor_header *) &z_0,
721 (struct usb_descriptor_header *) &z_1,
722 (struct usb_descriptor_header *) &z_2,
723 (struct usb_descriptor_header *) &z_audio_if_desc,
724 (struct usb_descriptor_header *) &z_audio_if_desc2,
725 (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
726 (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
727 (struct usb_descriptor_header *) &z_iso_ep,
728 (struct usb_descriptor_header *) &z_iso_ep2,
729 (struct usb_descriptor_header *) &za_0,
730 (struct usb_descriptor_header *) &za_1,
731 (struct usb_descriptor_header *) &za_2,
732 (struct usb_descriptor_header *) &za_3,
733 (struct usb_descriptor_header *) &za_4,
734 (struct usb_descriptor_header *) &za_5,
735 (struct usb_descriptor_header *) &za_6,
736 (struct usb_descriptor_header *) &za_7,
737 (struct usb_descriptor_header *) &za_8,
738 (struct usb_descriptor_header *) &za_9,
739 (struct usb_descriptor_header *) &za_10,
740 (struct usb_descriptor_header *) &za_11,
741 (struct usb_descriptor_header *) &za_12,
742 (struct usb_descriptor_header *) &za_13,
743 (struct usb_descriptor_header *) &za_14,
744 (struct usb_descriptor_header *) &za_15,
745 (struct usb_descriptor_header *) &za_16,
746 (struct usb_descriptor_header *) &za_17,
747 (struct usb_descriptor_header *) &za_18,
748 (struct usb_descriptor_header *) &za_19,
749 (struct usb_descriptor_header *) &za_20,
750 (struct usb_descriptor_header *) &za_21,
751 (struct usb_descriptor_header *) &za_22,
752 (struct usb_descriptor_header *) &za_23,
753 (struct usb_descriptor_header *) &za_24,
757 /* maxpacket and other transfer characteristics vary by speed. */
758 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
762 /* if there's no high speed support, maxpacket doesn't change. */
763 #define ep_desc(g,hs,fs) fs
765 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
767 static char manufacturer [40];
768 //static char serial [40];
769 static char serial [] = "Ser 00 em";
771 /* static strings, in UTF-8 */
772 static struct usb_string strings [] = {
773 { STRING_MANUFACTURER, manufacturer, },
774 { STRING_PRODUCT, longname, },
775 { STRING_SERIAL, serial, },
776 { STRING_LOOPBACK, loopback, },
777 { STRING_SOURCE_SINK, source_sink, },
778 { } /* end of list */
781 static struct usb_gadget_strings stringtab = {
782 .language = 0x0409, /* en-us */
787 * config descriptors are also handcrafted. these must agree with code
788 * that sets configurations, and with code managing interfaces and their
789 * altsettings. other complexity may come from:
791 * - high speed support, including "other speed config" rules
792 * - multiple configurations
793 * - interfaces with alternate settings
794 * - embedded class or vendor-specific descriptors
796 * this handles high speed, and has a second config that could as easily
797 * have been an alternate interface setting (on most hardware).
799 * NOTE: to demonstrate (and test) more USB capabilities, this driver
800 * should include an altsetting to test interrupt transfers, including
801 * high bandwidth modes at high speed. (Maybe work like Intel's test
805 config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
808 const struct usb_descriptor_header **function;
810 function = z_function;
811 len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
814 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
818 /*-------------------------------------------------------------------------*/
820 static struct usb_request *
821 alloc_ep_req (struct usb_ep *ep, unsigned length)
823 struct usb_request *req;
825 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
827 req->length = length;
828 req->buf = usb_ep_alloc_buffer (ep, length,
829 &req->dma, GFP_ATOMIC);
831 usb_ep_free_request (ep, req);
838 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
841 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
842 usb_ep_free_request (ep, req);
845 /*-------------------------------------------------------------------------*/
847 /* optionally require specific source/sink data patterns */
851 struct zero_dev *dev,
853 struct usb_request *req
859 for (i = 0; i < req->actual; i++, buf++) {
861 /* all-zeroes has no synchronization issues */
866 /* mod63 stays in sync with short-terminated transfers,
867 * or otherwise when host and gadget agree on how large
868 * each usb transfer request should be. resync is done
869 * with set_interface or set_config.
872 if (*buf == (u8)(i % 63))
876 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
877 usb_ep_set_halt (ep);
883 /*-------------------------------------------------------------------------*/
885 static void zero_reset_config (struct zero_dev *dev)
887 if (dev->config == 0)
890 DBG (dev, "reset config\n");
892 /* just disable endpoints, forcing completion of pending i/o.
893 * all our completion handlers free their requests in this case.
896 usb_ep_disable (dev->in_ep);
900 usb_ep_disable (dev->out_ep);
904 del_timer (&dev->resume);
907 #define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
910 zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
912 struct zero_dev *dev = ep->driver_data;
913 int status = req->status;
918 case 0: /* normal completion? */
919 //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
920 for (i=0, j=rbuf_start; i<req->actual; i++) {
921 //printk ("%02x ", ((__u8*)req->buf)[i]);
922 rbuf[j] = ((__u8*)req->buf)[i];
924 if (j >= RBUF_LEN) j=0;
929 if (rbuf_len < RBUF_LEN) {
930 rbuf_len += req->actual;
931 if (rbuf_len > RBUF_LEN) {
938 /* this endpoint is normally active while we're configured */
939 case -ECONNABORTED: /* hardware forced ep reset */
940 case -ECONNRESET: /* request dequeued */
941 case -ESHUTDOWN: /* disconnect from host */
942 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
943 req->actual, req->length);
944 if (ep == dev->out_ep)
945 check_read_data (dev, ep, req);
946 free_ep_req (ep, req);
949 case -EOVERFLOW: /* buffer overrun on read means that
950 * we didn't provide a big enough
955 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
956 status, req->actual, req->length);
958 case -EREMOTEIO: /* short read */
962 status = usb_ep_queue (ep, req, GFP_ATOMIC);
964 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
965 ep->name, req->length, status);
966 usb_ep_set_halt (ep);
967 /* FIXME recover later ... somehow */
971 static struct usb_request *
972 zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
974 struct usb_request *req;
977 req = alloc_ep_req (ep, 512);
981 req->complete = zero_isoc_complete;
983 status = usb_ep_queue (ep, req, gfp_flags);
985 struct zero_dev *dev = ep->driver_data;
987 ERROR (dev, "start %s --> %d\n", ep->name, status);
988 free_ep_req (ep, req);
995 /* change our operational config. this code must agree with the code
996 * that returns config descriptors, and altsetting code.
998 * it's also responsible for power management interactions. some
999 * configurations might not work with our current power sources.
1001 * note that some device controller hardware will constrain what this
1002 * code can do, perhaps by disallowing more than one configuration or
1003 * by limiting configuration choices (like the pxa2xx).
1006 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
1009 struct usb_gadget *gadget = dev->gadget;
1010 const struct usb_endpoint_descriptor *d;
1013 if (number == dev->config)
1016 zero_reset_config (dev);
1018 gadget_for_each_ep (ep, gadget) {
1020 if (strcmp (ep->name, "ep4") == 0) {
1022 d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
1023 result = usb_ep_enable (ep, d);
1026 ep->driver_data = dev;
1029 if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
1035 usb_ep_disable (ep);
1042 dev->config = number;
1046 /*-------------------------------------------------------------------------*/
1048 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
1050 if (req->status || req->actual != req->length)
1051 DBG ((struct zero_dev *) ep->driver_data,
1052 "setup complete --> %d, %d/%d\n",
1053 req->status, req->actual, req->length);
1057 * The setup() callback implements all the ep0 functionality that's
1058 * not handled lower down, in hardware or the hardware driver (like
1059 * device and endpoint feature flags, and their status). It's all
1060 * housekeeping for the gadget function we're implementing. Most of
1061 * the work is in config-specific setup.
1064 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1066 struct zero_dev *dev = get_gadget_data (gadget);
1067 struct usb_request *req = dev->req;
1068 int value = -EOPNOTSUPP;
1070 /* usually this stores reply data in the pre-allocated ep0 buffer,
1071 * but config change events will reconfigure hardware.
1074 switch (ctrl->bRequest) {
1076 case USB_REQ_GET_DESCRIPTOR:
1078 switch (ctrl->wValue >> 8) {
1081 value = min (ctrl->wLength, (u16) sizeof device_desc);
1082 memcpy (req->buf, &device_desc, value);
1084 #ifdef CONFIG_USB_GADGET_DUALSPEED
1085 case USB_DT_DEVICE_QUALIFIER:
1086 if (!gadget->is_dualspeed)
1088 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1089 memcpy (req->buf, &dev_qualifier, value);
1092 case USB_DT_OTHER_SPEED_CONFIG:
1093 if (!gadget->is_dualspeed)
1096 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1098 value = config_buf (gadget, req->buf,
1100 ctrl->wValue & 0xff);
1102 value = min (ctrl->wLength, (u16) value);
1106 /* wIndex == language code.
1107 * this driver only handles one language, you can
1108 * add string tables for other languages, using
1109 * any UTF-8 characters
1111 value = usb_gadget_get_string (&stringtab,
1112 ctrl->wValue & 0xff, req->buf);
1114 value = min (ctrl->wLength, (u16) value);
1120 /* currently two configs, two speeds */
1121 case USB_REQ_SET_CONFIGURATION:
1122 if (ctrl->bRequestType != 0)
1125 spin_lock (&dev->lock);
1126 value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1127 spin_unlock (&dev->lock);
1129 case USB_REQ_GET_CONFIGURATION:
1130 if (ctrl->bRequestType != USB_DIR_IN)
1132 *(u8 *)req->buf = dev->config;
1133 value = min (ctrl->wLength, (u16) 1);
1136 /* until we add altsetting support, or other interfaces,
1137 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1138 * and already killed pending endpoint I/O.
1140 case USB_REQ_SET_INTERFACE:
1142 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1144 spin_lock (&dev->lock);
1146 u8 config = dev->config;
1148 /* resets interface configuration, forgets about
1149 * previous transaction state (queued bufs, etc)
1150 * and re-inits endpoint state (toggle etc)
1151 * no response queued, just zero status == success.
1152 * if we had more than one interface we couldn't
1153 * use this "reset the config" shortcut.
1155 zero_reset_config (dev);
1156 zero_set_config (dev, config, GFP_ATOMIC);
1159 spin_unlock (&dev->lock);
1161 case USB_REQ_GET_INTERFACE:
1162 if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
1163 value = ctrl->wLength;
1167 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1171 if (ctrl->wIndex != 0) {
1175 *(u8 *)req->buf = 0;
1176 value = min (ctrl->wLength, (u16) 1);
1181 * These are the same vendor-specific requests supported by
1182 * Intel's USB 2.0 compliance test devices. We exceed that
1183 * device spec by allowing multiple-packet requests.
1185 case 0x5b: /* control WRITE test -- fill the buffer */
1186 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1188 if (ctrl->wValue || ctrl->wIndex)
1190 /* just read that many bytes into the buffer */
1191 if (ctrl->wLength > USB_BUFSIZ)
1193 value = ctrl->wLength;
1195 case 0x5c: /* control READ test -- return the buffer */
1196 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1198 if (ctrl->wValue || ctrl->wIndex)
1200 /* expect those bytes are still in the buffer; send back */
1201 if (ctrl->wLength > USB_BUFSIZ
1202 || ctrl->wLength != req->length)
1204 value = ctrl->wLength;
1207 case 0x01: // SET_CUR
1212 value = ctrl->wLength;
1215 switch (ctrl->wValue) {
1218 ((u8*)req->buf)[0] = 0x00;
1219 ((u8*)req->buf)[1] = 0xe3;
1223 ((u8*)req->buf)[0] = 0x00;
1226 //((u8*)req->buf)[0] = 0x81;
1227 //((u8*)req->buf)[1] = 0x81;
1228 value = ctrl->wLength;
1231 switch (ctrl->wValue) {
1234 ((u8*)req->buf)[0] = 0x00;
1235 ((u8*)req->buf)[1] = 0xc3;
1239 ((u8*)req->buf)[0] = 0x00;
1242 //((u8*)req->buf)[0] = 0x82;
1243 //((u8*)req->buf)[1] = 0x82;
1244 value = ctrl->wLength;
1247 switch (ctrl->wValue) {
1250 ((u8*)req->buf)[0] = 0x00;
1251 ((u8*)req->buf)[1] = 0x00;
1254 ((u8*)req->buf)[0] = 0x60;
1257 ((u8*)req->buf)[0] = 0x18;
1260 //((u8*)req->buf)[0] = 0x83;
1261 //((u8*)req->buf)[1] = 0x83;
1262 value = ctrl->wLength;
1265 switch (ctrl->wValue) {
1268 ((u8*)req->buf)[0] = 0x00;
1269 ((u8*)req->buf)[1] = 0x01;
1273 ((u8*)req->buf)[0] = 0x08;
1276 //((u8*)req->buf)[0] = 0x84;
1277 //((u8*)req->buf)[1] = 0x84;
1278 value = ctrl->wLength;
1281 ((u8*)req->buf)[0] = 0x85;
1282 ((u8*)req->buf)[1] = 0x85;
1283 value = ctrl->wLength;
1289 printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
1290 ctrl->bRequestType, ctrl->bRequest,
1291 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1294 /* respond with data transfer before status phase? */
1296 req->length = value;
1297 req->zero = value < ctrl->wLength
1298 && (value % gadget->ep0->maxpacket) == 0;
1299 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1301 DBG (dev, "ep_queue < 0 --> %d\n", value);
1303 zero_setup_complete (gadget->ep0, req);
1307 /* device either stalls (value < 0) or reports success */
1312 zero_disconnect (struct usb_gadget *gadget)
1314 struct zero_dev *dev = get_gadget_data (gadget);
1315 unsigned long flags;
1317 spin_lock_irqsave (&dev->lock, flags);
1318 zero_reset_config (dev);
1320 /* a more significant application might have some non-usb
1321 * activities to quiesce here, saving resources like power
1322 * or pushing the notification up a network stack.
1324 spin_unlock_irqrestore (&dev->lock, flags);
1326 /* next we may get setup() calls to enumerate new connections;
1327 * or an unbind() during shutdown (including removing module).
1332 zero_autoresume (unsigned long _dev)
1334 struct zero_dev *dev = (struct zero_dev *) _dev;
1337 /* normally the host would be woken up for something
1338 * more significant than just a timer firing...
1340 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1341 status = usb_gadget_wakeup (dev->gadget);
1342 DBG (dev, "wakeup --> %d\n", status);
1346 /*-------------------------------------------------------------------------*/
1349 zero_unbind (struct usb_gadget *gadget)
1351 struct zero_dev *dev = get_gadget_data (gadget);
1353 DBG (dev, "unbind\n");
1355 /* we've already been disconnected ... no i/o is active */
1357 free_ep_req (gadget->ep0, dev->req);
1358 del_timer_sync (&dev->resume);
1360 set_gadget_data (gadget, NULL);
1364 zero_bind (struct usb_gadget *gadget)
1366 struct zero_dev *dev;
1367 //struct usb_ep *ep;
1369 printk("binding\n");
1371 * DRIVER POLICY CHOICE: you may want to do this differently.
1372 * One thing to avoid is reusing a bcdDevice revision code
1373 * with different host-visible configurations or behavior
1374 * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1376 //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
1379 /* ok, we made sense of the hardware ... */
1380 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1383 memset (dev, 0, sizeof *dev);
1384 spin_lock_init (&dev->lock);
1385 dev->gadget = gadget;
1386 set_gadget_data (gadget, dev);
1388 /* preallocate control response and buffer */
1389 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1392 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1393 &dev->req->dma, GFP_KERNEL);
1397 dev->req->complete = zero_setup_complete;
1399 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1401 #ifdef CONFIG_USB_GADGET_DUALSPEED
1402 /* assume ep0 uses the same value for both speeds ... */
1403 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1405 /* and that all endpoints are dual-speed */
1406 //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1407 //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1410 usb_gadget_set_selfpowered (gadget);
1412 init_timer (&dev->resume);
1413 dev->resume.function = zero_autoresume;
1414 dev->resume.data = (unsigned long) dev;
1416 gadget->ep0->driver_data = dev;
1418 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1419 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1420 EP_OUT_NAME, EP_IN_NAME);
1422 snprintf (manufacturer, sizeof manufacturer,
1423 UTS_SYSNAME " " UTS_RELEASE " with %s",
1429 zero_unbind (gadget);
1433 /*-------------------------------------------------------------------------*/
1436 zero_suspend (struct usb_gadget *gadget)
1438 struct zero_dev *dev = get_gadget_data (gadget);
1440 if (gadget->speed == USB_SPEED_UNKNOWN)
1444 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1445 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1447 DBG (dev, "suspend\n");
1451 zero_resume (struct usb_gadget *gadget)
1453 struct zero_dev *dev = get_gadget_data (gadget);
1455 DBG (dev, "resume\n");
1456 del_timer (&dev->resume);
1460 /*-------------------------------------------------------------------------*/
1462 static struct usb_gadget_driver zero_driver = {
1463 #ifdef CONFIG_USB_GADGET_DUALSPEED
1464 .speed = USB_SPEED_HIGH,
1466 .speed = USB_SPEED_FULL,
1468 .function = (char *) longname,
1470 .unbind = zero_unbind,
1472 .setup = zero_setup,
1473 .disconnect = zero_disconnect,
1475 .suspend = zero_suspend,
1476 .resume = zero_resume,
1479 .name = (char *) shortname,
1486 MODULE_AUTHOR ("David Brownell");
1487 MODULE_LICENSE ("Dual BSD/GPL");
1489 static struct proc_dir_entry *pdir, *pfile;
1491 static int isoc_read_data (char *page, char **start,
1492 off_t off, int count,
1493 int *eof, void *data)
1497 static int done = 0;
1501 printk ("\ncount: %d\n", count);
1502 printk ("rbuf_start: %d\n", rbuf_start);
1503 printk ("rbuf_len: %d\n", rbuf_len);
1504 printk ("off: %d\n", off);
1505 printk ("start: %p\n\n", *start);
1515 if (rbuf_len == RBUF_LEN)
1520 for (i=0; i<count && c<rbuf_len; i++, c++) {
1521 page[i] = rbuf[(c+s) % RBUF_LEN];
1525 if (c >= rbuf_len) {
1534 static int __init init (void)
1539 pdir = proc_mkdir("isoc_test", NULL);
1542 printk("Error creating dir\n");
1545 pdir->owner = THIS_MODULE;
1547 pfile = create_proc_read_entry("isoc_data",
1551 if (pfile == NULL) {
1553 printk("Error creating file\n");
1556 pfile->owner = THIS_MODULE;
1558 return usb_gadget_register_driver (&zero_driver);
1561 remove_proc_entry("isoc_data", NULL);
1567 static void __exit cleanup (void)
1570 usb_gadget_unregister_driver (&zero_driver);
1572 remove_proc_entry("isoc_data", pdir);
1573 remove_proc_entry("isoc_test", NULL);
1575 module_exit (cleanup);