[ramips] move files to files-3.7
[15.05/openwrt.git] / target / linux / ramips / files-3.7 / drivers / usb / dwc_otg / dummy_audio.c
1 /*
2  * zero.c -- Gadget Zero, for USB development
3  *
4  * Copyright (C) 2003-2004 David Brownell
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The names of the above-listed copyright holders may not be used
17  *    to endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * ALTERNATIVELY, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") as published by the Free Software
22  * Foundation, either version 2 of that License or (at your option) any
23  * later version.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38
39 /*
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.
42  *
43  * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44  * affect most of the driver.
45  *
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".
48  *
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:
52  *
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
56  *
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
59  * driver does).
60  */
61
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>
80
81 #include <asm/byteorder.h>
82 #include <asm/io.h>
83 #include <asm/irq.h>
84 #include <asm/system.h>
85 #include <asm/unaligned.h>
86
87 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
88 # include <linux/usb/ch9.h>
89 #else
90 # include <linux/usb_ch9.h>
91 #endif
92
93 #include <linux/usb_gadget.h>
94
95
96 /*-------------------------------------------------------------------------*/
97 /*-------------------------------------------------------------------------*/
98
99
100 static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
101 {
102         int     count = 0;
103         u8      c;
104         u16     uchar;
105
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.)
109          */
110         while (len != 0 && (c = (u8) *s++) != 0) {
111                 if (unlikely(c & 0x80)) {
112                         // 2-byte sequence:
113                         // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
114                         if ((c & 0xe0) == 0xc0) {
115                                 uchar = (c & 0x1f) << 6;
116
117                                 c = (u8) *s++;
118                                 if ((c & 0xc0) != 0xc0)
119                                         goto fail;
120                                 c &= 0x3f;
121                                 uchar |= c;
122
123                         // 3-byte sequence (most CJKV characters):
124                         // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
125                         } else if ((c & 0xf0) == 0xe0) {
126                                 uchar = (c & 0x0f) << 12;
127
128                                 c = (u8) *s++;
129                                 if ((c & 0xc0) != 0xc0)
130                                         goto fail;
131                                 c &= 0x3f;
132                                 uchar |= c << 6;
133
134                                 c = (u8) *s++;
135                                 if ((c & 0xc0) != 0xc0)
136                                         goto fail;
137                                 c &= 0x3f;
138                                 uchar |= c;
139
140                                 /* no bogus surrogates */
141                                 if (0xd800 <= uchar && uchar <= 0xdfff)
142                                         goto fail;
143
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)
149
150                         } else
151                                 goto fail;
152                 } else
153                         uchar = c;
154                 put_unaligned (cpu_to_le16 (uchar), cp++);
155                 count++;
156                 len--;
157         }
158         return count;
159 fail:
160         return -1;
161 }
162
163
164 /**
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
169  *
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
173  *
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).
180  */
181 int
182 usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
183 {
184         struct usb_string       *s;
185         int                     len;
186
187         /* descriptor 0 has the language id */
188         if (id == 0) {
189                 buf [0] = 4;
190                 buf [1] = USB_DT_STRING;
191                 buf [2] = (u8) table->language;
192                 buf [3] = (u8) (table->language >> 8);
193                 return 4;
194         }
195         for (s = table->strings; s && s->s; s++)
196                 if (s->id == id)
197                         break;
198
199         /* unrecognized: stall. */
200         if (!s || !s->s)
201                 return -EINVAL;
202
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);
207         if (len < 0)
208                 return -EINVAL;
209         buf [0] = (len + 1) * 2;
210         buf [1] = USB_DT_STRING;
211         return buf [0];
212 }
213
214
215 /*-------------------------------------------------------------------------*/
216 /*-------------------------------------------------------------------------*/
217
218
219 /**
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.
224  *
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.
230  */
231 int
232 usb_descriptor_fillbuf(void *buf, unsigned buflen,
233                 const struct usb_descriptor_header **src)
234 {
235         u8      *dest = buf;
236
237         if (!src)
238                 return -EINVAL;
239
240         /* fill buffer from src[] until null descriptor ptr */
241         for (; 0 != *src; src++) {
242                 unsigned                len = (*src)->bLength;
243
244                 if (len > buflen)
245                         return -EINVAL;
246                 memcpy(dest, *src, len);
247                 buflen -= len;
248                 dest += len;
249         }
250         return dest - (u8 *)buf;
251 }
252
253
254 /**
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.
263  *
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.
269  *
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.
273  */
274 int usb_gadget_config_buf(
275         const struct usb_config_descriptor      *config,
276         void                                    *buf,
277         unsigned                                length,
278         const struct usb_descriptor_header      **desc
279 )
280 {
281         struct usb_config_descriptor            *cp = buf;
282         int                                     len;
283
284         /* config descriptor first */
285         if (length < USB_DT_CONFIG_SIZE || !desc)
286                 return -EINVAL;
287         *cp = *config;
288
289         /* then interface/endpoint/class/vendor/... */
290         len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
291                         length - USB_DT_CONFIG_SIZE, desc);
292         if (len < 0)
293                 return len;
294         len += USB_DT_CONFIG_SIZE;
295         if (len > 0xffff)
296                 return -EINVAL;
297
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;
303         return len;
304 }
305
306 /*-------------------------------------------------------------------------*/
307 /*-------------------------------------------------------------------------*/
308
309
310 #define RBUF_LEN (1024*1024)
311 static int rbuf_start;
312 static int rbuf_len;
313 static __u8 rbuf[RBUF_LEN];
314
315 /*-------------------------------------------------------------------------*/
316
317 #define DRIVER_VERSION          "St Patrick's Day 2004"
318
319 static const char shortname [] = "zero";
320 static const char longname [] = "YAMAHA YST-MS35D USB Speaker  ";
321
322 static const char source_sink [] = "source and sink data";
323 static const char loopback [] = "loop input to output";
324
325 /*-------------------------------------------------------------------------*/
326
327 /*
328  * driver assumes self-powered hardware, and
329  * has no way for users to trigger remote wakeup.
330  *
331  * this version autoconfigures as much as possible,
332  * which is reasonable for most "bulk-only" drivers.
333  */
334 static const char *EP_IN_NAME;          /* source */
335 static const char *EP_OUT_NAME;         /* sink */
336
337 /*-------------------------------------------------------------------------*/
338
339 /* big enough to hold our biggest descriptor */
340 #define USB_BUFSIZ      512
341
342 struct zero_dev {
343         spinlock_t              lock;
344         struct usb_gadget       *gadget;
345         struct usb_request      *req;           /* for control responses */
346
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)
350          */
351         u8                      config;
352         struct usb_ep           *in_ep, *out_ep;
353
354         /* autoresume timer */
355         struct timer_list       resume;
356 };
357
358 #define xprintk(d,level,fmt,args...) \
359         dev_printk(level , &(d)->gadget->dev , fmt , ## args)
360
361 #ifdef DEBUG
362 #define DBG(dev,fmt,args...) \
363         xprintk(dev , KERN_DEBUG , fmt , ## args)
364 #else
365 #define DBG(dev,fmt,args...) \
366         do { } while (0)
367 #endif /* DEBUG */
368
369 #ifdef VERBOSE
370 #define VDBG    DBG
371 #else
372 #define VDBG(dev,fmt,args...) \
373         do { } while (0)
374 #endif /* VERBOSE */
375
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)
382
383 /*-------------------------------------------------------------------------*/
384
385 static unsigned buflen = 4096;
386 static unsigned qlen = 32;
387 static unsigned pattern = 0;
388
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);
392
393 /*
394  * if it's nonzero, autoresume says how many seconds to wait
395  * before trying to wake up the host after suspend.
396  */
397 static unsigned autoresume = 0;
398 module_param (autoresume, uint, 0);
399
400 /*
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.
405  */
406 static int loopdefault = 0;
407
408 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
409
410 /*-------------------------------------------------------------------------*/
411
412 /* Thanks to NetChip Technologies for donating this product ID.
413  *
414  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
415  * Instead:  allocate your own, using normal USB-IF procedures.
416  */
417 #ifndef CONFIG_USB_ZERO_HNPTEST
418 #define DRIVER_VENDOR_NUM       0x0525          /* NetChip */
419 #define DRIVER_PRODUCT_NUM      0xa4a0          /* Linux-USB "Gadget Zero" */
420 #else
421 #define DRIVER_VENDOR_NUM       0x1a0a          /* OTG test device IDs */
422 #define DRIVER_PRODUCT_NUM      0xbadd
423 #endif
424
425 /*-------------------------------------------------------------------------*/
426
427 /*
428  * DESCRIPTORS ... most are static, but strings and (full)
429  * configuration descriptors are built on demand.
430  */
431
432 /*
433 #define STRING_MANUFACTURER             25
434 #define STRING_PRODUCT                  42
435 #define STRING_SERIAL                   101
436 */
437 #define STRING_MANUFACTURER             1
438 #define STRING_PRODUCT                  2
439 #define STRING_SERIAL                   3
440
441 #define STRING_SOURCE_SINK              250
442 #define STRING_LOOPBACK                 251
443
444 /*
445  * This device advertises two configurations; these numbers work
446  * on a pxa250 as well as more flexible hardware.
447  */
448 #define CONFIG_SOURCE_SINK      3
449 #define CONFIG_LOOPBACK         2
450
451 /*
452 static struct usb_device_descriptor
453 device_desc = {
454         .bLength =              sizeof device_desc,
455         .bDescriptorType =      USB_DT_DEVICE,
456
457         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
458         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
459
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,
466 };
467 */
468 static struct usb_device_descriptor
469 device_desc = {
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,
484 };
485
486 static struct usb_config_descriptor
487 z_config = {
488         .bLength =              sizeof z_config,
489         .bDescriptorType =      USB_DT_CONFIG,
490
491         /* compute wTotalLength on the fly */
492         .bNumInterfaces =       2,
493         .bConfigurationValue =  1,
494         .iConfiguration =       0,
495         .bmAttributes =         0x40,
496         .bMaxPower =            0,      /* self-powered */
497 };
498
499
500 static struct usb_otg_descriptor
501 otg_descriptor = {
502         .bLength =              sizeof otg_descriptor,
503         .bDescriptorType =      USB_DT_OTG,
504
505         .bmAttributes =         USB_OTG_SRP,
506 };
507
508 /* one interface in each configuration */
509 #ifdef  CONFIG_USB_GADGET_DUALSPEED
510
511 /*
512  * usb 2.0 devices need to expose both high speed and full speed
513  * descriptors, unless they only run at full speed.
514  *
515  * that means alternate endpoint descriptors (bigger packets)
516  * and a "device qualifier" ... plus more construction options
517  * for the config descriptor.
518  */
519
520 static struct usb_qualifier_descriptor
521 dev_qualifier = {
522         .bLength =              sizeof dev_qualifier,
523         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
524
525         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
526         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
527
528         .bNumConfigurations =   2,
529 };
530
531
532 struct usb_cs_as_general_descriptor {
533         __u8  bLength;
534         __u8  bDescriptorType;
535
536         __u8  bDescriptorSubType;
537         __u8  bTerminalLink;
538         __u8  bDelay;
539         __u16  wFormatTag;
540 } __attribute__ ((packed));
541
542 struct usb_cs_as_format_descriptor {
543         __u8  bLength;
544         __u8  bDescriptorType;
545
546         __u8  bDescriptorSubType;
547         __u8  bFormatType;
548         __u8  bNrChannels;
549         __u8  bSubframeSize;
550         __u8  bBitResolution;
551         __u8  bSamfreqType;
552         __u8  tLowerSamFreq[3];
553         __u8  tUpperSamFreq[3];
554 } __attribute__ ((packed));
555
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,
562         .bNumEndpoints = 0,
563         .bInterfaceClass = USB_CLASS_AUDIO,
564         .bInterfaceSubClass = 0x1,
565         .bInterfaceProtocol = 0,
566         .iInterface = 0,
567 };
568
569 static const struct usb_interface_descriptor
570 z_audio_if_desc = {
571         .bLength =              sizeof z_audio_if_desc,
572         .bDescriptorType =      USB_DT_INTERFACE,
573         .bInterfaceNumber = 1,
574         .bAlternateSetting = 0,
575         .bNumEndpoints = 0,
576         .bInterfaceClass = USB_CLASS_AUDIO,
577         .bInterfaceSubClass = 0x2,
578         .bInterfaceProtocol = 0,
579         .iInterface = 0,
580 };
581
582 static const struct usb_interface_descriptor
583 z_audio_if_desc2 = {
584         .bLength =              sizeof z_audio_if_desc,
585         .bDescriptorType =      USB_DT_INTERFACE,
586         .bInterfaceNumber = 1,
587         .bAlternateSetting = 1,
588         .bNumEndpoints = 1,
589         .bInterfaceClass = USB_CLASS_AUDIO,
590         .bInterfaceSubClass = 0x2,
591         .bInterfaceProtocol = 0,
592         .iInterface = 0,
593 };
594
595 static const struct usb_cs_as_general_descriptor
596 z_audio_cs_as_if_desc = {
597         .bLength = 7,
598         .bDescriptorType = 0x24,
599
600         .bDescriptorSubType = 0x01,
601         .bTerminalLink = 0x01,
602         .bDelay = 0x0,
603         .wFormatTag = __constant_cpu_to_le16 (0x0001)
604 };
605
606
607 static const struct usb_cs_as_format_descriptor
608 z_audio_cs_as_format_desc = {
609         .bLength = 0xe,
610         .bDescriptorType = 0x24,
611
612         .bDescriptorSubType = 2,
613         .bFormatType = 1,
614         .bNrChannels = 1,
615         .bSubframeSize = 1,
616         .bBitResolution = 8,
617         .bSamfreqType = 0,
618         .tLowerSamFreq = {0x7e, 0x13, 0x00},
619         .tUpperSamFreq = {0xe2, 0xd6, 0x00},
620 };
621
622 static const struct usb_endpoint_descriptor
623 z_iso_ep = {
624         .bLength = 0x09,
625         .bDescriptorType = 0x05,
626         .bEndpointAddress = 0x04,
627         .bmAttributes = 0x09,
628         .wMaxPacketSize = 0x0038,
629         .bInterval = 0x01,
630         .bRefresh = 0x00,
631         .bSynchAddress = 0x00,
632 };
633
634 static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
635
636 // 9 bytes
637 static char z_ac_interface_header_desc[] =
638 { 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
639
640 // 12 bytes
641 static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
642                      0x03, 0x00, 0x00, 0x00};
643 // 13 bytes
644 static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
645                      0x02, 0x00, 0x02, 0x00, 0x00};
646 // 9 bytes
647 static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
648                      0x00};
649
650 static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
651                       0x00};
652
653 static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
654
655 static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
656                       0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
657
658 static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
659                       0x00};
660
661 static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
662
663 static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
664                       0x00};
665
666 static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
667
668 static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
669                       0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
670
671 static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
672                       0x00};
673
674 static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
675
676 static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
677                        0x00};
678
679 static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
680
681 static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
682                        0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
683
684 static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
685                        0x00};
686
687 static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
688
689 static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
690                        0x00};
691
692 static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
693
694 static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
695                        0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
696
697 static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
698                        0x00};
699
700 static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
701
702 static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
703                        0x00};
704
705 static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
706
707 static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
708                        0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
709
710 static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
711                        0x00};
712
713 static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
714
715
716
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,
754         NULL,
755 };
756
757 /* maxpacket and other transfer characteristics vary by speed. */
758 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
759
760 #else
761
762 /* if there's no high speed support, maxpacket doesn't change. */
763 #define ep_desc(g,hs,fs) fs
764
765 #endif  /* !CONFIG_USB_GADGET_DUALSPEED */
766
767 static char                             manufacturer [40];
768 //static char                           serial [40];
769 static char                             serial [] = "Ser 00 em";
770
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 */
779 };
780
781 static struct usb_gadget_strings        stringtab = {
782         .language       = 0x0409,       /* en-us */
783         .strings        = strings,
784 };
785
786 /*
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:
790  *
791  *  - high speed support, including "other speed config" rules
792  *  - multiple configurations
793  *  - interfaces with alternate settings
794  *  - embedded class or vendor-specific descriptors
795  *
796  * this handles high speed, and has a second config that could as easily
797  * have been an alternate interface setting (on most hardware).
798  *
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
802  * device?)
803  */
804 static int
805 config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
806 {
807         int len;
808         const struct usb_descriptor_header **function;
809
810         function = z_function;
811         len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
812         if (len < 0)
813                 return len;
814         ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
815         return len;
816 }
817
818 /*-------------------------------------------------------------------------*/
819
820 static struct usb_request *
821 alloc_ep_req (struct usb_ep *ep, unsigned length)
822 {
823         struct usb_request      *req;
824
825         req = usb_ep_alloc_request (ep, GFP_ATOMIC);
826         if (req) {
827                 req->length = length;
828                 req->buf = usb_ep_alloc_buffer (ep, length,
829                                 &req->dma, GFP_ATOMIC);
830                 if (!req->buf) {
831                         usb_ep_free_request (ep, req);
832                         req = NULL;
833                 }
834         }
835         return req;
836 }
837
838 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
839 {
840         if (req->buf)
841                 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
842         usb_ep_free_request (ep, req);
843 }
844
845 /*-------------------------------------------------------------------------*/
846
847 /* optionally require specific source/sink data patterns  */
848
849 static int
850 check_read_data (
851         struct zero_dev         *dev,
852         struct usb_ep           *ep,
853         struct usb_request      *req
854 )
855 {
856         unsigned        i;
857         u8              *buf = req->buf;
858
859         for (i = 0; i < req->actual; i++, buf++) {
860                 switch (pattern) {
861                 /* all-zeroes has no synchronization issues */
862                 case 0:
863                         if (*buf == 0)
864                                 continue;
865                         break;
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.
870                  */
871                 case 1:
872                         if (*buf == (u8)(i % 63))
873                                 continue;
874                         break;
875                 }
876                 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
877                 usb_ep_set_halt (ep);
878                 return -EINVAL;
879         }
880         return 0;
881 }
882
883 /*-------------------------------------------------------------------------*/
884
885 static void zero_reset_config (struct zero_dev *dev)
886 {
887         if (dev->config == 0)
888                 return;
889
890         DBG (dev, "reset config\n");
891
892         /* just disable endpoints, forcing completion of pending i/o.
893          * all our completion handlers free their requests in this case.
894          */
895         if (dev->in_ep) {
896                 usb_ep_disable (dev->in_ep);
897                 dev->in_ep = NULL;
898         }
899         if (dev->out_ep) {
900                 usb_ep_disable (dev->out_ep);
901                 dev->out_ep = NULL;
902         }
903         dev->config = 0;
904         del_timer (&dev->resume);
905 }
906
907 #define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
908
909 static void
910 zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
911 {
912         struct zero_dev *dev = ep->driver_data;
913         int             status = req->status;
914         int i, j;
915
916         switch (status) {
917
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];
923                         j++;
924                         if (j >= RBUF_LEN) j=0;
925                 }
926                 rbuf_start = j;
927                 //printk ("\n\n");
928
929                 if (rbuf_len < RBUF_LEN) {
930                         rbuf_len += req->actual;
931                         if (rbuf_len > RBUF_LEN) {
932                                 rbuf_len = RBUF_LEN;
933                         }
934                 }
935
936                 break;
937
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);
947                 return;
948
949         case -EOVERFLOW:                /* buffer overrun on read means that
950                                          * we didn't provide a big enough
951                                          * buffer.
952                                          */
953         default:
954 #if 1
955                 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
956                                 status, req->actual, req->length);
957 #endif
958         case -EREMOTEIO:                /* short read */
959                 break;
960         }
961
962         status = usb_ep_queue (ep, req, GFP_ATOMIC);
963         if (status) {
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 */
968         }
969 }
970
971 static struct usb_request *
972 zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
973 {
974         struct usb_request      *req;
975         int                     status;
976
977         req = alloc_ep_req (ep, 512);
978         if (!req)
979                 return NULL;
980
981         req->complete = zero_isoc_complete;
982
983         status = usb_ep_queue (ep, req, gfp_flags);
984         if (status) {
985                 struct zero_dev *dev = ep->driver_data;
986
987                 ERROR (dev, "start %s --> %d\n", ep->name, status);
988                 free_ep_req (ep, req);
989                 req = NULL;
990         }
991
992         return req;
993 }
994
995 /* change our operational config.  this code must agree with the code
996  * that returns config descriptors, and altsetting code.
997  *
998  * it's also responsible for power management interactions. some
999  * configurations might not work with our current power sources.
1000  *
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).
1004  */
1005 static int
1006 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
1007 {
1008         int                     result = 0;
1009         struct usb_gadget       *gadget = dev->gadget;
1010         const struct usb_endpoint_descriptor    *d;
1011         struct usb_ep           *ep;
1012
1013         if (number == dev->config)
1014                 return 0;
1015
1016         zero_reset_config (dev);
1017
1018         gadget_for_each_ep (ep, gadget) {
1019
1020                 if (strcmp (ep->name, "ep4") == 0) {
1021
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);
1024
1025                         if (result == 0) {
1026                                 ep->driver_data = dev;
1027                                 dev->in_ep = ep;
1028
1029                                 if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
1030
1031                                         dev->in_ep = ep;
1032                                         continue;
1033                                 }
1034
1035                                 usb_ep_disable (ep);
1036                                 result = -EIO;
1037                         }
1038                 }
1039
1040         }
1041
1042         dev->config = number;
1043         return result;
1044 }
1045
1046 /*-------------------------------------------------------------------------*/
1047
1048 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
1049 {
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);
1054 }
1055
1056 /*
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.
1062  */
1063 static int
1064 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1065 {
1066         struct zero_dev         *dev = get_gadget_data (gadget);
1067         struct usb_request      *req = dev->req;
1068         int                     value = -EOPNOTSUPP;
1069
1070         /* usually this stores reply data in the pre-allocated ep0 buffer,
1071          * but config change events will reconfigure hardware.
1072          */
1073         req->zero = 0;
1074         switch (ctrl->bRequest) {
1075
1076         case USB_REQ_GET_DESCRIPTOR:
1077
1078                 switch (ctrl->wValue >> 8) {
1079
1080                 case USB_DT_DEVICE:
1081                         value = min (ctrl->wLength, (u16) sizeof device_desc);
1082                         memcpy (req->buf, &device_desc, value);
1083                         break;
1084 #ifdef CONFIG_USB_GADGET_DUALSPEED
1085                 case USB_DT_DEVICE_QUALIFIER:
1086                         if (!gadget->is_dualspeed)
1087                                 break;
1088                         value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1089                         memcpy (req->buf, &dev_qualifier, value);
1090                         break;
1091
1092                 case USB_DT_OTHER_SPEED_CONFIG:
1093                         if (!gadget->is_dualspeed)
1094                                 break;
1095                         // FALLTHROUGH
1096 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1097                 case USB_DT_CONFIG:
1098                         value = config_buf (gadget, req->buf,
1099                                         ctrl->wValue >> 8,
1100                                         ctrl->wValue & 0xff);
1101                         if (value >= 0)
1102                                 value = min (ctrl->wLength, (u16) value);
1103                         break;
1104
1105                 case USB_DT_STRING:
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
1110                          */
1111                         value = usb_gadget_get_string (&stringtab,
1112                                         ctrl->wValue & 0xff, req->buf);
1113                         if (value >= 0) {
1114                                 value = min (ctrl->wLength, (u16) value);
1115                         }
1116                         break;
1117                 }
1118                 break;
1119
1120         /* currently two configs, two speeds */
1121         case USB_REQ_SET_CONFIGURATION:
1122                 if (ctrl->bRequestType != 0)
1123                         goto unknown;
1124
1125                 spin_lock (&dev->lock);
1126                 value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1127                 spin_unlock (&dev->lock);
1128                 break;
1129         case USB_REQ_GET_CONFIGURATION:
1130                 if (ctrl->bRequestType != USB_DIR_IN)
1131                         goto unknown;
1132                 *(u8 *)req->buf = dev->config;
1133                 value = min (ctrl->wLength, (u16) 1);
1134                 break;
1135
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.
1139          */
1140         case USB_REQ_SET_INTERFACE:
1141
1142                 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1143                         goto unknown;
1144                 spin_lock (&dev->lock);
1145                 if (dev->config) {
1146                         u8              config = dev->config;
1147
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.
1154                          */
1155                         zero_reset_config (dev);
1156                         zero_set_config (dev, config, GFP_ATOMIC);
1157                         value = 0;
1158                 }
1159                 spin_unlock (&dev->lock);
1160                 break;
1161         case USB_REQ_GET_INTERFACE:
1162                 if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
1163                         value = ctrl->wLength;
1164                         break;
1165                 }
1166                 else {
1167                         if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1168                                 goto unknown;
1169                         if (!dev->config)
1170                                 break;
1171                         if (ctrl->wIndex != 0) {
1172                                 value = -EDOM;
1173                                 break;
1174                         }
1175                         *(u8 *)req->buf = 0;
1176                         value = min (ctrl->wLength, (u16) 1);
1177                 }
1178                 break;
1179
1180         /*
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.
1184          */
1185         case 0x5b:      /* control WRITE test -- fill the buffer */
1186                 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1187                         goto unknown;
1188                 if (ctrl->wValue || ctrl->wIndex)
1189                         break;
1190                 /* just read that many bytes into the buffer */
1191                 if (ctrl->wLength > USB_BUFSIZ)
1192                         break;
1193                 value = ctrl->wLength;
1194                 break;
1195         case 0x5c:      /* control READ test -- return the buffer */
1196                 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1197                         goto unknown;
1198                 if (ctrl->wValue || ctrl->wIndex)
1199                         break;
1200                 /* expect those bytes are still in the buffer; send back */
1201                 if (ctrl->wLength > USB_BUFSIZ
1202                                 || ctrl->wLength != req->length)
1203                         break;
1204                 value = ctrl->wLength;
1205                 break;
1206
1207         case 0x01: // SET_CUR
1208         case 0x02:
1209         case 0x03:
1210         case 0x04:
1211         case 0x05:
1212                 value = ctrl->wLength;
1213                 break;
1214         case 0x81:
1215                 switch (ctrl->wValue) {
1216                 case 0x0201:
1217                 case 0x0202:
1218                         ((u8*)req->buf)[0] = 0x00;
1219                         ((u8*)req->buf)[1] = 0xe3;
1220                         break;
1221                 case 0x0300:
1222                 case 0x0500:
1223                         ((u8*)req->buf)[0] = 0x00;
1224                         break;
1225                 }
1226                 //((u8*)req->buf)[0] = 0x81;
1227                 //((u8*)req->buf)[1] = 0x81;
1228                 value = ctrl->wLength;
1229                 break;
1230         case 0x82:
1231                 switch (ctrl->wValue) {
1232                 case 0x0201:
1233                 case 0x0202:
1234                         ((u8*)req->buf)[0] = 0x00;
1235                         ((u8*)req->buf)[1] = 0xc3;
1236                         break;
1237                 case 0x0300:
1238                 case 0x0500:
1239                         ((u8*)req->buf)[0] = 0x00;
1240                         break;
1241                 }
1242                 //((u8*)req->buf)[0] = 0x82;
1243                 //((u8*)req->buf)[1] = 0x82;
1244                 value = ctrl->wLength;
1245                 break;
1246         case 0x83:
1247                 switch (ctrl->wValue) {
1248                 case 0x0201:
1249                 case 0x0202:
1250                         ((u8*)req->buf)[0] = 0x00;
1251                         ((u8*)req->buf)[1] = 0x00;
1252                         break;
1253                 case 0x0300:
1254                         ((u8*)req->buf)[0] = 0x60;
1255                         break;
1256                 case 0x0500:
1257                         ((u8*)req->buf)[0] = 0x18;
1258                         break;
1259                 }
1260                 //((u8*)req->buf)[0] = 0x83;
1261                 //((u8*)req->buf)[1] = 0x83;
1262                 value = ctrl->wLength;
1263                 break;
1264         case 0x84:
1265                 switch (ctrl->wValue) {
1266                 case 0x0201:
1267                 case 0x0202:
1268                         ((u8*)req->buf)[0] = 0x00;
1269                         ((u8*)req->buf)[1] = 0x01;
1270                         break;
1271                 case 0x0300:
1272                 case 0x0500:
1273                         ((u8*)req->buf)[0] = 0x08;
1274                         break;
1275                 }
1276                 //((u8*)req->buf)[0] = 0x84;
1277                 //((u8*)req->buf)[1] = 0x84;
1278                 value = ctrl->wLength;
1279                 break;
1280         case 0x85:
1281                 ((u8*)req->buf)[0] = 0x85;
1282                 ((u8*)req->buf)[1] = 0x85;
1283                 value = ctrl->wLength;
1284                 break;
1285
1286
1287         default:
1288 unknown:
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);
1292         }
1293
1294         /* respond with data transfer before status phase? */
1295         if (value >= 0) {
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);
1300                 if (value < 0) {
1301                         DBG (dev, "ep_queue < 0 --> %d\n", value);
1302                         req->status = 0;
1303                         zero_setup_complete (gadget->ep0, req);
1304                 }
1305         }
1306
1307         /* device either stalls (value < 0) or reports success */
1308         return value;
1309 }
1310
1311 static void
1312 zero_disconnect (struct usb_gadget *gadget)
1313 {
1314         struct zero_dev         *dev = get_gadget_data (gadget);
1315         unsigned long           flags;
1316
1317         spin_lock_irqsave (&dev->lock, flags);
1318         zero_reset_config (dev);
1319
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.
1323          */
1324         spin_unlock_irqrestore (&dev->lock, flags);
1325
1326         /* next we may get setup() calls to enumerate new connections;
1327          * or an unbind() during shutdown (including removing module).
1328          */
1329 }
1330
1331 static void
1332 zero_autoresume (unsigned long _dev)
1333 {
1334         struct zero_dev *dev = (struct zero_dev *) _dev;
1335         int             status;
1336
1337         /* normally the host would be woken up for something
1338          * more significant than just a timer firing...
1339          */
1340         if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1341                 status = usb_gadget_wakeup (dev->gadget);
1342                 DBG (dev, "wakeup --> %d\n", status);
1343         }
1344 }
1345
1346 /*-------------------------------------------------------------------------*/
1347
1348 static void
1349 zero_unbind (struct usb_gadget *gadget)
1350 {
1351         struct zero_dev         *dev = get_gadget_data (gadget);
1352
1353         DBG (dev, "unbind\n");
1354
1355         /* we've already been disconnected ... no i/o is active */
1356         if (dev->req)
1357                 free_ep_req (gadget->ep0, dev->req);
1358         del_timer_sync (&dev->resume);
1359         kfree (dev);
1360         set_gadget_data (gadget, NULL);
1361 }
1362
1363 static int
1364 zero_bind (struct usb_gadget *gadget)
1365 {
1366         struct zero_dev         *dev;
1367         //struct usb_ep         *ep;
1368
1369         printk("binding\n");
1370         /*
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
1375          */
1376         //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
1377
1378
1379         /* ok, we made sense of the hardware ... */
1380         dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1381         if (!dev)
1382                 return -ENOMEM;
1383         memset (dev, 0, sizeof *dev);
1384         spin_lock_init (&dev->lock);
1385         dev->gadget = gadget;
1386         set_gadget_data (gadget, dev);
1387
1388         /* preallocate control response and buffer */
1389         dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1390         if (!dev->req)
1391                 goto enomem;
1392         dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1393                                 &dev->req->dma, GFP_KERNEL);
1394         if (!dev->req->buf)
1395                 goto enomem;
1396
1397         dev->req->complete = zero_setup_complete;
1398
1399         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1400
1401 #ifdef CONFIG_USB_GADGET_DUALSPEED
1402         /* assume ep0 uses the same value for both speeds ... */
1403         dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1404
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;
1408 #endif
1409
1410         usb_gadget_set_selfpowered (gadget);
1411
1412         init_timer (&dev->resume);
1413         dev->resume.function = zero_autoresume;
1414         dev->resume.data = (unsigned long) dev;
1415
1416         gadget->ep0->driver_data = dev;
1417
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);
1421
1422         snprintf (manufacturer, sizeof manufacturer,
1423                 UTS_SYSNAME " " UTS_RELEASE " with %s",
1424                 gadget->name);
1425
1426         return 0;
1427
1428 enomem:
1429         zero_unbind (gadget);
1430         return -ENOMEM;
1431 }
1432
1433 /*-------------------------------------------------------------------------*/
1434
1435 static void
1436 zero_suspend (struct usb_gadget *gadget)
1437 {
1438         struct zero_dev         *dev = get_gadget_data (gadget);
1439
1440         if (gadget->speed == USB_SPEED_UNKNOWN)
1441                 return;
1442
1443         if (autoresume) {
1444                 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1445                 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1446         } else
1447                 DBG (dev, "suspend\n");
1448 }
1449
1450 static void
1451 zero_resume (struct usb_gadget *gadget)
1452 {
1453         struct zero_dev         *dev = get_gadget_data (gadget);
1454
1455         DBG (dev, "resume\n");
1456         del_timer (&dev->resume);
1457 }
1458
1459
1460 /*-------------------------------------------------------------------------*/
1461
1462 static struct usb_gadget_driver zero_driver = {
1463 #ifdef CONFIG_USB_GADGET_DUALSPEED
1464         .speed          = USB_SPEED_HIGH,
1465 #else
1466         .speed          = USB_SPEED_FULL,
1467 #endif
1468         .function       = (char *) longname,
1469         .bind           = zero_bind,
1470         .unbind         = zero_unbind,
1471
1472         .setup          = zero_setup,
1473         .disconnect     = zero_disconnect,
1474
1475         .suspend        = zero_suspend,
1476         .resume         = zero_resume,
1477
1478         .driver         = {
1479                 .name           = (char *) shortname,
1480                 // .shutdown = ...
1481                 // .suspend = ...
1482                 // .resume = ...
1483         },
1484 };
1485
1486 MODULE_AUTHOR ("David Brownell");
1487 MODULE_LICENSE ("Dual BSD/GPL");
1488
1489 static struct proc_dir_entry *pdir, *pfile;
1490
1491 static int isoc_read_data (char *page, char **start,
1492                            off_t off, int count,
1493                            int *eof, void *data)
1494 {
1495         int i;
1496         static int c = 0;
1497         static int done = 0;
1498         static int s = 0;
1499
1500 /*
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);
1506 */
1507         if (done) {
1508                 c = 0;
1509                 done = 0;
1510                 *eof = 1;
1511                 return 0;
1512         }
1513
1514         if (c == 0) {
1515                 if (rbuf_len == RBUF_LEN)
1516                         s = rbuf_start;
1517                 else s = 0;
1518         }
1519
1520         for (i=0; i<count && c<rbuf_len; i++, c++) {
1521                 page[i] = rbuf[(c+s) % RBUF_LEN];
1522         }
1523         *start = page;
1524
1525         if (c >= rbuf_len) {
1526                 *eof = 1;
1527                 done = 1;
1528         }
1529
1530
1531         return i;
1532 }
1533
1534 static int __init init (void)
1535 {
1536
1537         int retval = 0;
1538
1539         pdir = proc_mkdir("isoc_test", NULL);
1540         if(pdir == NULL) {
1541                 retval = -ENOMEM;
1542                 printk("Error creating dir\n");
1543                 goto done;
1544         }
1545         pdir->owner = THIS_MODULE;
1546
1547         pfile = create_proc_read_entry("isoc_data",
1548                                        0444, pdir,
1549                                        isoc_read_data,
1550                                        NULL);
1551         if (pfile == NULL) {
1552                 retval = -ENOMEM;
1553                 printk("Error creating file\n");
1554                 goto no_file;
1555         }
1556         pfile->owner = THIS_MODULE;
1557
1558         return usb_gadget_register_driver (&zero_driver);
1559
1560  no_file:
1561         remove_proc_entry("isoc_data", NULL);
1562  done:
1563         return retval;
1564 }
1565 module_init (init);
1566
1567 static void __exit cleanup (void)
1568 {
1569
1570         usb_gadget_unregister_driver (&zero_driver);
1571
1572         remove_proc_entry("isoc_data", pdir);
1573         remove_proc_entry("isoc_test", NULL);
1574 }
1575 module_exit (cleanup);