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.
39 * Gadget Zero only needs two bulk endpoints, and is an example of how you
40 * can write a hardware-agnostic gadget driver running inside a USB device.
42 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
43 * affect most of the driver.
45 * Use it with the Linux host/master side "usbtest" driver to get a basic
46 * functional test of your device-side usb stack, or with "usb-skeleton".
48 * It supports two similar configurations. One sinks whatever the usb host
49 * writes, and in return sources zeroes. The other loops whatever the host
50 * writes back, so the host can read it. Module options include:
52 * buflen=N default N=4096, buffer size used
53 * qlen=N default N=32, how many buffers in the loopback queue
54 * loopdefault default false, list loopback config first
56 * Many drivers will only have one configuration, letting them be much
57 * simpler if they also don't support high speed operation (like this
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/uts.h>
75 #include <linux/version.h>
76 #include <linux/device.h>
77 #include <linux/moduleparam.h>
78 #include <linux/proc_fs.h>
80 #include <asm/byteorder.h>
83 #include <asm/system.h>
84 #include <asm/unaligned.h>
86 #include <linux/usb_ch9.h>
87 #include <linux/usb_gadget.h>
89 /*-------------------------------------------------------------------------*/
90 /*-------------------------------------------------------------------------*/
92 static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
98 /* this insists on correct encodings, though not minimal ones.
99 * BUT it currently rejects legit 4-byte UTF-8 code points,
100 * which need surrogate pairs. (Unicode 3.1 can use them.)
102 while (len != 0 && (c = (u8)*s++) != 0) {
103 if (unlikely(c & 0x80)) {
105 * 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
107 if ((c & 0xe0) == 0xc0) {
108 uchar = (c & 0x1f) << 6;
111 if ((c & 0xc0) != 0xc0)
116 /* 3-byte sequence (most CJKV characters):
117 * zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
119 } else if ((c & 0xf0) == 0xe0) {
120 uchar = (c & 0x0f) << 12;
123 if ((c & 0xc0) != 0xc0)
129 if ((c & 0xc0) != 0xc0)
134 /* no bogus surrogates */
135 if (0xd800 <= uchar && uchar <= 0xdfff)
139 * (surrogate pairs, currently rare):
140 * 11101110wwwwzzzzyy + 110111yyyyxxxxxx
141 * = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
143 * FIXME accept the surrogate code points (only)
150 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).
176 int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf)
178 struct usb_string *s;
181 /* descriptor 0 has the language id */
184 buf[1] = USB_DT_STRING;
185 buf[2] = (u8) table->language;
186 buf[3] = (u8) (table->language >> 8);
189 for (s = table->strings; s && s->s; s++)
193 /* unrecognized: stall. */
197 /* string descriptors have length, tag, then UTF16-LE text */
198 len = min((size_t) 126, strlen(s->s));
199 memset(buf + 2, 0, 2 * len); /* zero all the bytes */
200 len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
203 buf[0] = (len + 1) * 2;
204 buf[1] = USB_DT_STRING;
208 /*-------------------------------------------------------------------------*/
209 /*-------------------------------------------------------------------------*/
212 * usb_descriptor_fillbuf - fill buffer with descriptors
213 * @buf: Buffer to be filled
214 * @buflen: Size of buf
215 * @src: Array of descriptor pointers, terminated by null pointer.
217 * Copies descriptors into the buffer, returning the length or a
218 * negative error code if they can't all be copied. Useful when
219 * assembling descriptors for an associated set of interfaces used
220 * as part of configuring a composite device; or in other cases where
221 * sets of descriptors need to be marshaled.
224 usb_descriptor_fillbuf(void *buf, unsigned buflen,
225 const struct usb_descriptor_header **src)
232 /* fill buffer from src[] until null descriptor ptr */
233 for (; 0 != *src; src++) {
234 unsigned len = (*src)->bLength;
238 memcpy(dest, *src, len);
242 return dest - (u8 *) buf;
246 * usb_gadget_config_buf - builts a complete configuration descriptor
247 * @config: Header for the descriptor, including characteristics such
248 * as power requirements and number of interfaces.
249 * @desc: Null-terminated vector of pointers to the descriptors (interface,
250 * endpoint, etc) defining all functions in this device configuration.
251 * @buf: Buffer for the resulting configuration descriptor.
252 * @length: Length of buffer. If this is not big enough to hold the
253 * entire configuration descriptor, an error code will be returned.
255 * This copies descriptors into the response buffer, building a descriptor
256 * for that configuration. It returns the buffer length or a negative
257 * status code. The config.wTotalLength field is set to match the length
258 * of the result, but other descriptor fields (including power usage and
259 * interface count) must be set by the caller.
261 * Gadget drivers could use this when constructing a config descriptor
262 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
263 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
265 int usb_gadget_config_buf(const struct usb_config_descriptor *config,
268 const struct usb_descriptor_header **desc)
270 struct usb_config_descriptor *cp = buf;
273 /* config descriptor first */
274 if (length < USB_DT_CONFIG_SIZE || !desc)
278 /* then interface/endpoint/class/vendor/... */
279 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *) buf,
280 length - USB_DT_CONFIG_SIZE, desc);
283 len += USB_DT_CONFIG_SIZE;
287 /* patch up the config descriptor */
288 cp->bLength = USB_DT_CONFIG_SIZE;
289 cp->bDescriptorType = USB_DT_CONFIG;
290 cp->wTotalLength = cpu_to_le16(len);
291 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
295 /*-------------------------------------------------------------------------*/
296 /*-------------------------------------------------------------------------*/
298 #define RBUF_LEN (1024*1024)
299 static int rbuf_start;
301 static __u8 rbuf[RBUF_LEN];
303 /*-------------------------------------------------------------------------*/
305 #define DRIVER_VERSION "St Patrick's Day 2004"
307 static const char shortname[] = "zero";
308 static const char longname[] = "YAMAHA YST-MS35D USB Speaker ";
310 static const char source_sink[] = "source and sink data";
311 static const char loopback[] = "loop input to output";
313 /*-------------------------------------------------------------------------*/
316 * driver assumes self-powered hardware, and
317 * has no way for users to trigger remote wakeup.
319 * this version autoconfigures as much as possible,
320 * which is reasonable for most "bulk-only" drivers.
322 static const char *EP_IN_NAME; /* source */
323 static const char *EP_OUT_NAME; /* sink */
325 /*-------------------------------------------------------------------------*/
327 /* big enough to hold our biggest descriptor */
328 #define USB_BUFSIZ 512
332 struct usb_gadget *gadget;
333 struct usb_request *req; /* for control responses */
335 /* when configured, we have one of two configs:
336 * - source data (in to host) and sink it (out from host)
337 * - or loop it back (out from host back in to host)
340 struct usb_ep *in_ep, *out_ep;
342 /* autoresume timer */
343 struct timer_list resume;
346 #define xprintk(d, level, fmt, args...) \
347 dev_printk(level, &(d)->gadget->dev, fmt, ## args)
350 #define DBG(dev, fmt, args...) \
351 xprintk(dev, KERN_DEBUG, fmt, ## args)
353 #define DBG(dev, fmt, args...) \
360 #define VDBG(dev, fmt, args...) \
364 #define ERROR(dev, fmt, args...) \
365 xprintk(dev, KERN_ERR, fmt, ## args)
366 #define WARN(dev, fmt, args...) \
367 xprintk(dev, KERN_WARNING, fmt, ## args)
368 #define INFO(dev, fmt, args...) \
369 xprintk(dev, KERN_INFO, fmt, ## args)
371 /*-------------------------------------------------------------------------*/
373 static unsigned buflen = 4096;
374 static unsigned qlen = 32;
375 static unsigned pattern;
377 module_param(buflen, uint, S_IRUGO | S_IWUSR);
378 module_param(qlen, uint, S_IRUGO | S_IWUSR);
379 module_param(pattern, uint, S_IRUGO | S_IWUSR);
382 * if it's nonzero, autoresume says how many seconds to wait
383 * before trying to wake up the host after suspend.
385 static unsigned autoresume;
386 module_param(autoresume, uint, 0);
389 * Normally the "loopback" configuration is second (index 1) so
390 * it's not the default. Here's where to change that order, to
391 * work better with hosts where config changes are problematic.
392 * Or controllers (like superh) that only support one config.
394 static int loopdefault0;
396 module_param(loopdefault, bool, S_IRUGO | S_IWUSR);
398 /*-------------------------------------------------------------------------*/
400 /* Thanks to NetChip Technologies for donating this product ID.
402 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
403 * Instead: allocate your own, using normal USB-IF procedures.
405 #ifndef CONFIG_USB_ZERO_HNPTEST
406 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
407 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
409 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
410 #define DRIVER_PRODUCT_NUM 0xbadd
413 /*-------------------------------------------------------------------------*/
416 * DESCRIPTORS ... most are static, but strings and (full)
417 * configuration descriptors are built on demand.
421 #define STRING_MANUFACTURER 25
422 #define STRING_PRODUCT 42
423 #define STRING_SERIAL 101
425 #define STRING_MANUFACTURER 1
426 #define STRING_PRODUCT 2
427 #define STRING_SERIAL 3
429 #define STRING_SOURCE_SINK 250
430 #define STRING_LOOPBACK 251
433 * This device advertises two configurations; these numbers work
434 * on a pxa250 as well as more flexible hardware.
436 #define CONFIG_SOURCE_SINK 3
437 #define CONFIG_LOOPBACK 2
440 static struct usb_device_descriptor
442 .bLength = sizeof device_desc,
443 .bDescriptorType = USB_DT_DEVICE,
445 .bcdUSB = __constant_cpu_to_le16 (0x0200),
446 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
448 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
449 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
450 .iManufacturer = STRING_MANUFACTURER,
451 .iProduct = STRING_PRODUCT,
452 .iSerialNumber = STRING_SERIAL,
453 .bNumConfigurations = 2,
456 static struct usb_device_descriptor
458 .bLength = sizeof(device_desc),
459 .bDescriptorType = USB_DT_DEVICE,
460 .bcdUSB = __constant_cpu_to_le16(0x0100),
461 .bDeviceClass = USB_CLASS_PER_INTERFACE,
462 .bDeviceSubClass = 0,
463 .bDeviceProtocol = 0,
464 .bMaxPacketSize0 = 64,
465 .bcdDevice = __constant_cpu_to_le16(0x0100),
466 .idVendor = __constant_cpu_to_le16(0x0499),
467 .idProduct = __constant_cpu_to_le16(0x3002),
468 .iManufacturer = STRING_MANUFACTURER,
469 .iProduct = STRING_PRODUCT,
470 .iSerialNumber = STRING_SERIAL,
471 .bNumConfigurations = 1,
474 static struct usb_config_descriptor
476 .bLength = sizeof(z_config),
477 .bDescriptorType = USB_DT_CONFIG,
479 /* compute wTotalLength on the fly */
481 .bConfigurationValue = 1,
483 .bmAttributes = 0x40,
484 .bMaxPower = 0, /* self-powered */
487 static struct usb_otg_descriptor
489 .bLength = sizeof(otg_descriptor),
490 .bDescriptorType = USB_DT_OTG,
492 .bmAttributes = USB_OTG_SRP,
495 /* one interface in each configuration */
496 #ifdef CONFIG_USB_GADGET_DUALSPEED
499 * usb 2.0 devices need to expose both high speed and full speed
500 * descriptors, unless they only run at full speed.
502 * that means alternate endpoint descriptors (bigger packets)
503 * and a "device qualifier" ... plus more construction options
504 * for the config descriptor.
507 static struct usb_qualifier_descriptor
509 .bLength = sizeof(dev_qualifier),
510 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
512 .bcdUSB = __constant_cpu_to_le16(0x0200),
513 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
515 .bNumConfigurations = 2,
518 struct usb_cs_as_general_descriptor {
520 __u8 bDescriptorType;
522 __u8 bDescriptorSubType;
526 } __attribute__((packed));
528 struct usb_cs_as_format_descriptor {
530 __u8 bDescriptorType;
532 __u8 bDescriptorSubType;
538 __u8 tLowerSamFreq[3];
539 __u8 tUpperSamFreq[3];
540 } __attribute__((packed));
542 static const struct usb_interface_descriptor
543 z_audio_control_if_desc = {
544 .bLength = sizeof(z_audio_control_if_desc),
545 .bDescriptorType = USB_DT_INTERFACE,
546 .bInterfaceNumber = 0,
547 .bAlternateSetting = 0,
549 .bInterfaceClass = USB_CLASS_AUDIO,
550 .bInterfaceSubClass = 0x1,
551 .bInterfaceProtocol = 0,
555 static const struct usb_interface_descriptor
557 .bLength = sizeof(z_audio_if_desc),
558 .bDescriptorType = USB_DT_INTERFACE,
559 .bInterfaceNumber = 1,
560 .bAlternateSetting = 0,
562 .bInterfaceClass = USB_CLASS_AUDIO,
563 .bInterfaceSubClass = 0x2,
564 .bInterfaceProtocol = 0,
568 static const struct usb_interface_descriptor
570 .bLength = sizeof(z_audio_if_desc),
571 .bDescriptorType = USB_DT_INTERFACE,
572 .bInterfaceNumber = 1,
573 .bAlternateSetting = 1,
575 .bInterfaceClass = USB_CLASS_AUDIO,
576 .bInterfaceSubClass = 0x2,
577 .bInterfaceProtocol = 0,
581 static const struct usb_cs_as_general_descriptor
582 z_audio_cs_as_if_desc = {
584 .bDescriptorType = 0x24,
586 .bDescriptorSubType = 0x01,
587 .bTerminalLink = 0x01,
589 .wFormatTag = __constant_cpu_to_le16(0x0001)
592 static const struct usb_cs_as_format_descriptor
593 z_audio_cs_as_format_desc = {
595 .bDescriptorType = 0x24,
597 .bDescriptorSubType = 2,
603 .tLowerSamFreq = {0x7e, 0x13, 0x00},
604 .tUpperSamFreq = {0xe2, 0xd6, 0x00},
607 static const struct usb_endpoint_descriptor
610 .bDescriptorType = 0x05,
611 .bEndpointAddress = 0x04,
612 .bmAttributes = 0x09,
613 .wMaxPacketSize = 0x0038,
616 .bSynchAddress = 0x00,
619 static char z_iso_ep2[] = { 0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02 };
622 static char z_ac_interface_header_desc[] = {
623 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01
627 static char z_0[] = { 0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
628 0x03, 0x00, 0x00, 0x00
632 static char z_1[] = { 0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
633 0x02, 0x00, 0x02, 0x00, 0x00
637 static char z_2[] = { 0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
641 static char za_0[] = { 0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
645 static char za_1[] = { 0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00 };
647 static char za_2[] = { 0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
648 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00
651 static char za_3[] = { 0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
655 static char za_4[] = { 0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02 };
657 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
667 static char za_8[] = { 0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
671 static char za_9[] = { 0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02 };
673 static char za_10[] = { 0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
677 static char za_11[] = { 0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00 };
679 static char za_12[] = { 0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
680 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00
683 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,
693 static char za_16[] = { 0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00 };
695 static char za_17[] = { 0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
696 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00
699 static char za_18[] = { 0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
703 static char za_19[] = { 0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02 };
705 static char za_20[] = { 0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
709 static char za_21[] = { 0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00 };
711 static char za_22[] = { 0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
712 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00
715 static char za_23[] = { 0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
719 static char za_24[] = { 0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02 };
721 static const struct usb_descriptor_header *z_function[] = {
722 (struct usb_descriptor_header *)&z_audio_control_if_desc,
723 (struct usb_descriptor_header *)&z_ac_interface_header_desc,
724 (struct usb_descriptor_header *)&z_0,
725 (struct usb_descriptor_header *)&z_1,
726 (struct usb_descriptor_header *)&z_2,
727 (struct usb_descriptor_header *)&z_audio_if_desc,
728 (struct usb_descriptor_header *)&z_audio_if_desc2,
729 (struct usb_descriptor_header *)&z_audio_cs_as_if_desc,
730 (struct usb_descriptor_header *)&z_audio_cs_as_format_desc,
731 (struct usb_descriptor_header *)&z_iso_ep,
732 (struct usb_descriptor_header *)&z_iso_ep2,
733 (struct usb_descriptor_header *)&za_0,
734 (struct usb_descriptor_header *)&za_1,
735 (struct usb_descriptor_header *)&za_2,
736 (struct usb_descriptor_header *)&za_3,
737 (struct usb_descriptor_header *)&za_4,
738 (struct usb_descriptor_header *)&za_5,
739 (struct usb_descriptor_header *)&za_6,
740 (struct usb_descriptor_header *)&za_7,
741 (struct usb_descriptor_header *)&za_8,
742 (struct usb_descriptor_header *)&za_9,
743 (struct usb_descriptor_header *)&za_10,
744 (struct usb_descriptor_header *)&za_11,
745 (struct usb_descriptor_header *)&za_12,
746 (struct usb_descriptor_header *)&za_13,
747 (struct usb_descriptor_header *)&za_14,
748 (struct usb_descriptor_header *)&za_15,
749 (struct usb_descriptor_header *)&za_16,
750 (struct usb_descriptor_header *)&za_17,
751 (struct usb_descriptor_header *)&za_18,
752 (struct usb_descriptor_header *)&za_19,
753 (struct usb_descriptor_header *)&za_20,
754 (struct usb_descriptor_header *)&za_21,
755 (struct usb_descriptor_header *)&za_22,
756 (struct usb_descriptor_header *)&za_23,
757 (struct usb_descriptor_header *)&za_24,
761 /* maxpacket and other transfer characteristics vary by speed. */
762 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH) ? (hs) : (fs))
766 /* if there's no high speed support, maxpacket doesn't change. */
767 #define ep_desc(g, hs, fs) fs
769 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
771 static char manufacturer[40];
772 /* static char serial [40]; */
773 static char serial[] = "Ser 00 em";
775 /* static strings, in UTF-8 */
776 static struct usb_string strings[] = {
777 {STRING_MANUFACTURER, manufacturer,},
778 {STRING_PRODUCT, longname,},
779 {STRING_SERIAL, serial,},
780 {STRING_LOOPBACK, loopback,},
781 {STRING_SOURCE_SINK, source_sink,},
785 static struct usb_gadget_strings stringtab = {
786 .language = 0x0409, /* en-us */
791 * config descriptors are also handcrafted. these must agree with code
792 * that sets configurations, and with code managing interfaces and their
793 * altsettings. other complexity may come from:
795 * - high speed support, including "other speed config" rules
796 * - multiple configurations
797 * - interfaces with alternate settings
798 * - embedded class or vendor-specific descriptors
800 * this handles high speed, and has a second config that could as easily
801 * have been an alternate interface setting (on most hardware).
803 * NOTE: to demonstrate (and test) more USB capabilities, this driver
804 * should include an altsetting to test interrupt transfers, including
805 * high bandwidth modes at high speed. (Maybe work like Intel's test
809 config_buf(struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
812 const struct usb_descriptor_header **function;
814 function = z_function;
815 len = usb_gadget_config_buf(&z_config, buf, USB_BUFSIZ, function);
818 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
822 /*-------------------------------------------------------------------------*/
824 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
826 struct usb_request *req;
828 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
830 req->length = length;
831 req->buf = usb_ep_alloc_buffer(ep, length,
832 &req->dma, GFP_ATOMIC);
834 usb_ep_free_request(ep, req);
841 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
844 usb_ep_free_buffer(ep, req->buf, req->dma, req->length);
845 usb_ep_free_request(ep, req);
848 /*-------------------------------------------------------------------------*/
850 /* optionally require specific source/sink data patterns */
853 check_read_data(struct zero_dev *dev,
854 struct usb_ep *ep, 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);
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))
909 static void zero_isoc_complete(struct usb_ep *ep, struct usb_request *req)
911 struct zero_dev *dev = ep->driver_data;
912 int status = req->status;
917 case 0: /* normal completion? */
918 /* printk ("\nzero ---------------> isoc normal completion %d bytes\n",
921 for (i = 0, j = rbuf_start; i < req->actual; i++) {
922 /* printk ("%02x ", ((__u8*)req->buf)[i]); */
923 rbuf[j] = ((__u8 *) req->buf)[i];
929 /* printk ("\n\n"); */
931 if (rbuf_len < RBUF_LEN) {
932 rbuf_len += req->actual;
933 if (rbuf_len > RBUF_LEN)
939 /* this endpoint is normally active while we're configured */
940 case -ECONNABORTED: /* hardware forced ep reset */
941 case -ECONNRESET: /* request dequeued */
942 case -ESHUTDOWN: /* disconnect from host */
943 VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
944 req->actual, req->length);
945 if (ep == dev->out_ep)
946 check_read_data(dev, ep, req);
947 free_ep_req(ep, req);
950 case -EOVERFLOW: /* buffer overrun on read means that
951 * we didn't provide a big enough
956 DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
957 status, req->actual, req->length);
959 case -EREMOTEIO: /* short read */
963 status = usb_ep_queue(ep, req, GFP_ATOMIC);
965 ERROR(dev, "kill %s: resubmit %d bytes --> %d\n",
966 ep->name, req->length, status);
968 /* FIXME recover later ... somehow */
972 static struct usb_request *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).
1005 static int zero_set_config(struct zero_dev *dev, unsigned number, int gfp_flags)
1008 struct usb_gadget *gadget = dev->gadget;
1009 const struct usb_endpoint_descriptor *d;
1012 if (number == dev->config)
1015 zero_reset_config(dev);
1017 gadget_for_each_ep(ep, gadget) {
1019 if (strcmp(ep->name, "ep4") == 0) {
1020 /* isoc ep desc for audio i/f alt setting 6 */
1021 d = (struct usb_endpoint_descripter *)&za_23;
1022 result = usb_ep_enable(ep, d);
1025 ep->driver_data = dev;
1028 if (zero_start_isoc_ep(ep, gfp_flags) != 0) {
1041 dev->config = number;
1045 /*-------------------------------------------------------------------------*/
1047 static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req)
1049 if (req->status || req->actual != req->length)
1050 DBG((struct zero_dev *)ep->driver_data,
1051 "setup complete --> %d, %d/%d\n",
1052 req->status, req->actual, req->length);
1056 * The setup() callback implements all the ep0 functionality that's
1057 * not handled lower down, in hardware or the hardware driver (like
1058 * device and endpoint feature flags, and their status). It's all
1059 * housekeeping for the gadget function we're implementing. Most of
1060 * the work is in config-specific setup.
1063 zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1065 struct zero_dev *dev = get_gadget_data(gadget);
1066 struct usb_request *req = dev->req;
1067 int value = -EOPNOTSUPP;
1069 /* usually this stores reply data in the pre-allocated ep0 buffer,
1070 * but config change events will reconfigure hardware.
1073 switch (ctrl->bRequest) {
1075 case USB_REQ_GET_DESCRIPTOR:
1077 switch (ctrl->wValue >> 8) {
1080 value = min(ctrl->wLength, (u16) sizeof(device_desc));
1081 memcpy(req->buf, &device_desc, value);
1083 #ifdef CONFIG_USB_GADGET_DUALSPEED
1084 case USB_DT_DEVICE_QUALIFIER:
1085 if (!gadget->is_dualspeed)
1087 value = min(ctrl->wLength, (u16) sizeof(dev_qualifier));
1088 memcpy(req->buf, &dev_qualifier, value);
1091 case USB_DT_OTHER_SPEED_CONFIG:
1092 if (!gadget->is_dualspeed)
1095 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1097 value = config_buf(gadget, req->buf,
1099 ctrl->wValue & 0xff);
1101 value = min(ctrl->wLength, (u16) value);
1105 /* wIndex == language code.
1106 * this driver only handles one language, you can
1107 * add string tables for other languages, using
1108 * any UTF-8 characters
1110 value = usb_gadget_get_string(&stringtab,
1111 ctrl->wValue & 0xff,
1114 value = min(ctrl->wLength, (u16) value);
1119 /* currently two configs, two speeds */
1120 case USB_REQ_SET_CONFIGURATION:
1121 if (ctrl->bRequestType != 0)
1124 spin_lock(&dev->lock);
1125 value = zero_set_config(dev, ctrl->wValue, GFP_ATOMIC);
1126 spin_unlock(&dev->lock);
1128 case USB_REQ_GET_CONFIGURATION:
1129 if (ctrl->bRequestType != USB_DIR_IN)
1131 *(u8 *) req->buf = dev->config;
1132 value = min(ctrl->wLength, (u16) 1);
1135 /* until we add altsetting support, or other interfaces,
1136 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1137 * and already killed pending endpoint I/O.
1139 case USB_REQ_SET_INTERFACE:
1141 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1143 spin_lock(&dev->lock);
1145 u8 config = dev->config;
1147 /* resets interface configuration, forgets about
1148 * previous transaction state (queued bufs, etc)
1149 * and re-inits endpoint state (toggle etc)
1150 * no response queued, just zero status == success.
1151 * if we had more than one interface we couldn't
1152 * use this "reset the config" shortcut.
1154 zero_reset_config(dev);
1155 zero_set_config(dev, config, GFP_ATOMIC);
1158 spin_unlock(&dev->lock);
1160 case USB_REQ_GET_INTERFACE:
1161 if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
1162 value = ctrl->wLength;
1165 if (ctrl->bRequestType !=
1166 (USB_DIR_IN | USB_RECIP_INTERFACE))
1170 if (ctrl->wIndex != 0) {
1174 *(u8 *) req->buf = 0;
1175 value = min(ctrl->wLength, (u16) 1);
1180 * These are the same vendor-specific requests supported by
1181 * Intel's USB 2.0 compliance test devices. We exceed that
1182 * device spec by allowing multiple-packet requests.
1184 case 0x5b: /* control WRITE test -- fill the buffer */
1185 if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_VENDOR))
1187 if (ctrl->wValue || ctrl->wIndex)
1189 /* just read that many bytes into the buffer */
1190 if (ctrl->wLength > USB_BUFSIZ)
1192 value = ctrl->wLength;
1194 case 0x5c: /* control READ test -- return the buffer */
1195 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_VENDOR))
1197 if (ctrl->wValue || ctrl->wIndex)
1199 /* expect those bytes are still in the buffer; send back */
1200 if (ctrl->wLength > USB_BUFSIZ || ctrl->wLength != req->length)
1202 value = ctrl->wLength;
1205 case 0x01: /* SET_CUR */
1210 value = ctrl->wLength;
1213 switch (ctrl->wValue) {
1216 ((u8 *) req->buf)[0] = 0x00;
1217 ((u8 *) req->buf)[1] = 0xe3;
1221 ((u8 *) req->buf)[0] = 0x00;
1224 /* ((u8*)req->buf)[0] = 0x81; */
1225 /* ((u8*)req->buf)[1] = 0x81; */
1226 value = ctrl->wLength;
1229 switch (ctrl->wValue) {
1232 ((u8 *) req->buf)[0] = 0x00;
1233 ((u8 *) req->buf)[1] = 0xc3;
1237 ((u8 *) req->buf)[0] = 0x00;
1240 /* ((u8*)req->buf)[0] = 0x82; */
1241 /* ((u8*)req->buf)[1] = 0x82; */
1242 value = ctrl->wLength;
1245 switch (ctrl->wValue) {
1248 ((u8 *) req->buf)[0] = 0x00;
1249 ((u8 *) req->buf)[1] = 0x00;
1252 ((u8 *) req->buf)[0] = 0x60;
1255 ((u8 *) req->buf)[0] = 0x18;
1258 /* ((u8*)req->buf)[0] = 0x83; */
1259 /* ((u8*)req->buf)[1] = 0x83; */
1260 value = ctrl->wLength;
1263 switch (ctrl->wValue) {
1266 ((u8 *) req->buf)[0] = 0x00;
1267 ((u8 *) req->buf)[1] = 0x01;
1271 ((u8 *) req->buf)[0] = 0x08;
1274 /* ((u8*)req->buf)[0] = 0x84; */
1275 /* ((u8*)req->buf)[1] = 0x84; */
1276 value = ctrl->wLength;
1279 ((u8 *) req->buf)[0] = 0x85;
1280 ((u8 *) req->buf)[1] = 0x85;
1281 value = ctrl->wLength;
1286 printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
1287 ctrl->bRequestType, ctrl->bRequest,
1288 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1291 /* respond with data transfer before status phase? */
1293 req->length = value;
1294 req->zero = value < ctrl->wLength
1295 && (value % gadget->ep0->maxpacket) == 0;
1296 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1298 DBG(dev, "ep_queue < 0 --> %d\n", value);
1300 zero_setup_complete(gadget->ep0, req);
1304 /* device either stalls (value < 0) or reports success */
1308 static void zero_disconnect(struct usb_gadget *gadget)
1310 struct zero_dev *dev = get_gadget_data(gadget);
1311 unsigned long flags;
1313 spin_lock_irqsave(&dev->lock, flags);
1314 zero_reset_config(dev);
1316 /* a more significant application might have some non-usb
1317 * activities to quiesce here, saving resources like power
1318 * or pushing the notification up a network stack.
1320 spin_unlock_irqrestore(&dev->lock, flags);
1322 /* next we may get setup() calls to enumerate new connections;
1323 * or an unbind() during shutdown (including removing module).
1327 static void 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 /*-------------------------------------------------------------------------*/
1343 static void zero_unbind(struct usb_gadget *gadget)
1345 struct zero_dev *dev = get_gadget_data(gadget);
1347 DBG(dev, "unbind\n");
1349 /* we've already been disconnected ... no i/o is active */
1351 free_ep_req(gadget->ep0, dev->req);
1352 del_timer_sync(&dev->resume);
1354 set_gadget_data(gadget, NULL);
1357 static int zero_bind(struct usb_gadget *gadget)
1359 struct zero_dev *dev;
1360 /* struct usb_ep *ep; */
1362 printk("binding\n");
1364 * DRIVER POLICY CHOICE: you may want to do this differently.
1365 * One thing to avoid is reusing a bcdDevice revision code
1366 * with different host-visible configurations or behavior
1367 * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1369 /* device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201); */
1371 /* ok, we made sense of the hardware ... */
1372 dev = kmalloc(sizeof(*dev), SLAB_KERNEL);
1375 memset(dev, 0, sizeof(*dev));
1376 spin_lock_init(&dev->lock);
1377 dev->gadget = gadget;
1378 set_gadget_data(gadget, dev);
1380 /* preallocate control response and buffer */
1381 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1384 dev->req->buf = usb_ep_alloc_buffer(gadget->ep0, USB_BUFSIZ,
1385 &dev->req->dma, GFP_KERNEL);
1389 dev->req->complete = zero_setup_complete;
1391 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1393 #ifdef CONFIG_USB_GADGET_DUALSPEED
1394 /* assume ep0 uses the same value for both speeds ... */
1395 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1397 /* and that all endpoints are dual-speed */
1398 /* hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; */
1399 /* hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; */
1402 usb_gadget_set_selfpowered(gadget);
1404 init_timer(&dev->resume);
1405 dev->resume.function = zero_autoresume;
1406 dev->resume.data = (unsigned long)dev;
1408 gadget->ep0->driver_data = dev;
1410 INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
1411 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
1412 EP_OUT_NAME, EP_IN_NAME);
1414 snprintf(manufacturer, sizeof(manufacturer),
1415 UTS_SYSNAME " " UTS_RELEASE " with %s", gadget->name);
1420 zero_unbind(gadget);
1424 /*-------------------------------------------------------------------------*/
1426 static void zero_suspend(struct usb_gadget *gadget)
1428 struct zero_dev *dev = get_gadget_data(gadget);
1430 if (gadget->speed == USB_SPEED_UNKNOWN)
1434 mod_timer(&dev->resume, jiffies + (HZ * autoresume));
1435 DBG(dev, "suspend, wakeup in %d seconds\n", autoresume);
1437 DBG(dev, "suspend\n");
1440 static void zero_resume(struct usb_gadget *gadget)
1442 struct zero_dev *dev = get_gadget_data(gadget);
1444 DBG(dev, "resume\n");
1445 del_timer(&dev->resume);
1448 /*-------------------------------------------------------------------------*/
1450 static struct usb_gadget_driver zero_driver = {
1451 #ifdef CONFIG_USB_GADGET_DUALSPEED
1452 .speed = USB_SPEED_HIGH,
1454 .speed = USB_SPEED_FULL,
1456 .function = (char *)longname,
1458 .unbind = zero_unbind,
1460 .setup = zero_setup,
1461 .disconnect = zero_disconnect,
1463 .suspend = zero_suspend,
1464 .resume = zero_resume,
1467 .name = (char *)shortname,
1468 /* .shutdown = ... */
1469 /* .suspend = ... */
1474 MODULE_AUTHOR("David Brownell");
1475 MODULE_LICENSE("Dual BSD/GPL");
1477 static struct proc_dir_entry *pdir, *pfile;
1479 static int isoc_read_data(char *page, char **start,
1480 off_t off, int count, int *eof, void *data)
1488 printk ("\ncount: %d\n", count);
1489 printk ("rbuf_start: %d\n", rbuf_start);
1490 printk ("rbuf_len: %d\n", rbuf_len);
1491 printk ("off: %d\n", off);
1492 printk ("start: %p\n\n", *start);
1502 if (rbuf_len == RBUF_LEN)
1508 for (i = 0; i < count && c < rbuf_len; i++, c++)
1509 page[i] = rbuf[(c + s) % RBUF_LEN];
1513 if (c >= rbuf_len) {
1521 static int __init init(void)
1526 pdir = proc_mkdir("isoc_test", NULL);
1529 printk("Error creating dir\n");
1532 pdir->owner = THIS_MODULE;
1534 pfile = create_proc_read_entry("isoc_data",
1535 0444, pdir, isoc_read_data, NULL);
1536 if (pfile == NULL) {
1538 printk("Error creating file\n");
1541 pfile->owner = THIS_MODULE;
1543 return usb_gadget_register_driver(&zero_driver);
1546 remove_proc_entry("isoc_data", NULL);
1553 static void __exit cleanup(void)
1556 usb_gadget_unregister_driver(&zero_driver);
1558 remove_proc_entry("isoc_data", pdir);
1559 remove_proc_entry("isoc_test", NULL);
1562 module_exit(cleanup);