(no commit message)
[firefly-linux-kernel-4.4.55.git] / 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 #include <linux/usb_ch9.h>
88 #include <linux/usb_gadget.h>
89
90
91 /*-------------------------------------------------------------------------*/
92 /*-------------------------------------------------------------------------*/
93
94
95 static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
96 {
97         int     count = 0;
98         u8      c;
99         u16     uchar;
100
101         /* this insists on correct encodings, though not minimal ones.
102          * BUT it currently rejects legit 4-byte UTF-8 code points,
103          * which need surrogate pairs.  (Unicode 3.1 can use them.)
104          */
105         while (len != 0 && (c = (u8) *s++) != 0) {
106                 if (unlikely(c & 0x80)) {
107                         // 2-byte sequence:
108                         // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
109                         if ((c & 0xe0) == 0xc0) {
110                                 uchar = (c & 0x1f) << 6;
111
112                                 c = (u8) *s++;
113                                 if ((c & 0xc0) != 0xc0)
114                                         goto fail;
115                                 c &= 0x3f;
116                                 uchar |= c;
117
118                         // 3-byte sequence (most CJKV characters):
119                         // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
120                         } else if ((c & 0xf0) == 0xe0) {
121                                 uchar = (c & 0x0f) << 12;
122
123                                 c = (u8) *s++;
124                                 if ((c & 0xc0) != 0xc0)
125                                         goto fail;
126                                 c &= 0x3f;
127                                 uchar |= c << 6;
128
129                                 c = (u8) *s++;
130                                 if ((c & 0xc0) != 0xc0)
131                                         goto fail;
132                                 c &= 0x3f;
133                                 uchar |= c;
134
135                                 /* no bogus surrogates */
136                                 if (0xd800 <= uchar && uchar <= 0xdfff)
137                                         goto fail;
138
139                         // 4-byte sequence (surrogate pairs, currently rare):
140                         // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
141                         //     = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
142                         // (uuuuu = wwww + 1)
143                         // FIXME accept the surrogate code points (only)
144
145                         } else
146                                 goto fail;
147                 } else
148                         uchar = c;
149                 put_unaligned (cpu_to_le16 (uchar), cp++);
150                 count++;
151                 len--;
152         }
153         return count;
154 fail:
155         return -1;
156 }
157
158
159 /**
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
164  *
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
168  *
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).
175  */
176 int
177 usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
178 {
179         struct usb_string       *s;
180         int                     len;
181
182         /* descriptor 0 has the language id */
183         if (id == 0) {
184                 buf [0] = 4;
185                 buf [1] = USB_DT_STRING;
186                 buf [2] = (u8) table->language;
187                 buf [3] = (u8) (table->language >> 8);
188                 return 4;
189         }
190         for (s = table->strings; s && s->s; s++)
191                 if (s->id == id)
192                         break;
193
194         /* unrecognized: stall. */
195         if (!s || !s->s)
196                 return -EINVAL;
197
198         /* string descriptors have length, tag, then UTF16-LE text */
199         len = min ((size_t) 126, strlen (s->s));
200         memset (buf + 2, 0, 2 * len);   /* zero all the bytes */
201         len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
202         if (len < 0)
203                 return -EINVAL;
204         buf [0] = (len + 1) * 2;
205         buf [1] = USB_DT_STRING;
206         return buf [0];
207 }
208
209
210 /*-------------------------------------------------------------------------*/
211 /*-------------------------------------------------------------------------*/
212
213
214 /**
215  * usb_descriptor_fillbuf - fill buffer with descriptors
216  * @buf: Buffer to be filled
217  * @buflen: Size of buf
218  * @src: Array of descriptor pointers, terminated by null pointer.
219  *
220  * Copies descriptors into the buffer, returning the length or a
221  * negative error code if they can't all be copied.  Useful when
222  * assembling descriptors for an associated set of interfaces used
223  * as part of configuring a composite device; or in other cases where
224  * sets of descriptors need to be marshaled.
225  */
226 int
227 usb_descriptor_fillbuf(void *buf, unsigned buflen,
228                 const struct usb_descriptor_header **src)
229 {
230         u8      *dest = buf;
231
232         if (!src)
233                 return -EINVAL;
234
235         /* fill buffer from src[] until null descriptor ptr */
236         for (; 0 != *src; src++) {
237                 unsigned                len = (*src)->bLength;
238
239                 if (len > buflen)
240                         return -EINVAL;
241                 memcpy(dest, *src, len);
242                 buflen -= len;
243                 dest += len;
244         }
245         return dest - (u8 *)buf;
246 }
247
248
249 /**
250  * usb_gadget_config_buf - builts a complete configuration descriptor
251  * @config: Header for the descriptor, including characteristics such
252  *      as power requirements and number of interfaces.
253  * @desc: Null-terminated vector of pointers to the descriptors (interface,
254  *      endpoint, etc) defining all functions in this device configuration.
255  * @buf: Buffer for the resulting configuration descriptor.
256  * @length: Length of buffer.  If this is not big enough to hold the
257  *      entire configuration descriptor, an error code will be returned.
258  *
259  * This copies descriptors into the response buffer, building a descriptor
260  * for that configuration.  It returns the buffer length or a negative
261  * status code.  The config.wTotalLength field is set to match the length
262  * of the result, but other descriptor fields (including power usage and
263  * interface count) must be set by the caller.
264  *
265  * Gadget drivers could use this when constructing a config descriptor
266  * in response to USB_REQ_GET_DESCRIPTOR.  They will need to patch the
267  * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
268  */
269 int usb_gadget_config_buf(
270         const struct usb_config_descriptor      *config,
271         void                                    *buf,
272         unsigned                                length,
273         const struct usb_descriptor_header      **desc
274 )
275 {
276         struct usb_config_descriptor            *cp = buf;
277         int                                     len;
278
279         /* config descriptor first */
280         if (length < USB_DT_CONFIG_SIZE || !desc)
281                 return -EINVAL;
282         *cp = *config; 
283
284         /* then interface/endpoint/class/vendor/... */
285         len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
286                         length - USB_DT_CONFIG_SIZE, desc);
287         if (len < 0)
288                 return len;
289         len += USB_DT_CONFIG_SIZE;
290         if (len > 0xffff)
291                 return -EINVAL;
292
293         /* patch up the config descriptor */
294         cp->bLength = USB_DT_CONFIG_SIZE;
295         cp->bDescriptorType = USB_DT_CONFIG;
296         cp->wTotalLength = cpu_to_le16(len);
297         cp->bmAttributes |= USB_CONFIG_ATT_ONE;
298         return len;
299 }
300
301 /*-------------------------------------------------------------------------*/
302 /*-------------------------------------------------------------------------*/
303
304
305 #define RBUF_LEN (1024*1024)
306 static int rbuf_start;
307 static int rbuf_len;
308 static __u8 rbuf[RBUF_LEN];
309
310 /*-------------------------------------------------------------------------*/
311
312 #define DRIVER_VERSION          "St Patrick's Day 2004"
313
314 static const char shortname [] = "zero";
315 static const char longname [] = "YAMAHA YST-MS35D USB Speaker  ";
316
317 static const char source_sink [] = "source and sink data";
318 static const char loopback [] = "loop input to output";
319
320 /*-------------------------------------------------------------------------*/
321
322 /*
323  * driver assumes self-powered hardware, and
324  * has no way for users to trigger remote wakeup.
325  *
326  * this version autoconfigures as much as possible,
327  * which is reasonable for most "bulk-only" drivers.
328  */
329 static const char *EP_IN_NAME;          /* source */
330 static const char *EP_OUT_NAME;         /* sink */
331
332 /*-------------------------------------------------------------------------*/
333
334 /* big enough to hold our biggest descriptor */
335 #define USB_BUFSIZ      512
336
337 struct zero_dev {
338         spinlock_t              lock;
339         struct usb_gadget       *gadget;
340         struct usb_request      *req;           /* for control responses */
341
342         /* when configured, we have one of two configs:
343          * - source data (in to host) and sink it (out from host)
344          * - or loop it back (out from host back in to host)
345          */
346         u8                      config;
347         struct usb_ep           *in_ep, *out_ep;
348
349         /* autoresume timer */
350         struct timer_list       resume;
351 };
352
353 #define xprintk(d,level,fmt,args...) \
354         dev_printk(level , &(d)->gadget->dev , fmt , ## args)
355
356 #ifdef DEBUG
357 #define DBG(dev,fmt,args...) \
358         xprintk(dev , KERN_DEBUG , fmt , ## args)
359 #else
360 #define DBG(dev,fmt,args...) \
361         do { } while (0)
362 #endif /* DEBUG */
363
364 #ifdef VERBOSE
365 #define VDBG    DBG
366 #else
367 #define VDBG(dev,fmt,args...) \
368         do { } while (0)
369 #endif /* VERBOSE */
370
371 #define ERROR(dev,fmt,args...) \
372         xprintk(dev , KERN_ERR , fmt , ## args)
373 #define WARN(dev,fmt,args...) \
374         xprintk(dev , KERN_WARNING , fmt , ## args)
375 #define INFO(dev,fmt,args...) \
376         xprintk(dev , KERN_INFO , fmt , ## args)
377
378 /*-------------------------------------------------------------------------*/
379
380 static unsigned buflen = 4096;
381 static unsigned qlen = 32;
382 static unsigned pattern = 0;
383
384 module_param (buflen, uint, S_IRUGO|S_IWUSR);
385 module_param (qlen, uint, S_IRUGO|S_IWUSR);
386 module_param (pattern, uint, S_IRUGO|S_IWUSR);
387
388 /*
389  * if it's nonzero, autoresume says how many seconds to wait
390  * before trying to wake up the host after suspend.
391  */
392 static unsigned autoresume = 0;
393 module_param (autoresume, uint, 0);
394
395 /*
396  * Normally the "loopback" configuration is second (index 1) so
397  * it's not the default.  Here's where to change that order, to
398  * work better with hosts where config changes are problematic.
399  * Or controllers (like superh) that only support one config.
400  */
401 static int loopdefault = 0;
402
403 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
404
405 /*-------------------------------------------------------------------------*/
406
407 /* Thanks to NetChip Technologies for donating this product ID.
408  *
409  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
410  * Instead:  allocate your own, using normal USB-IF procedures.
411  */
412 #ifndef CONFIG_USB_ZERO_HNPTEST
413 #define DRIVER_VENDOR_NUM       0x0525          /* NetChip */
414 #define DRIVER_PRODUCT_NUM      0xa4a0          /* Linux-USB "Gadget Zero" */
415 #else
416 #define DRIVER_VENDOR_NUM       0x1a0a          /* OTG test device IDs */
417 #define DRIVER_PRODUCT_NUM      0xbadd
418 #endif
419
420 /*-------------------------------------------------------------------------*/
421
422 /*
423  * DESCRIPTORS ... most are static, but strings and (full)
424  * configuration descriptors are built on demand.
425  */
426
427 /*
428 #define STRING_MANUFACTURER             25
429 #define STRING_PRODUCT                  42
430 #define STRING_SERIAL                   101
431 */
432 #define STRING_MANUFACTURER             1
433 #define STRING_PRODUCT                  2
434 #define STRING_SERIAL                   3
435
436 #define STRING_SOURCE_SINK              250
437 #define STRING_LOOPBACK                 251
438
439 /*
440  * This device advertises two configurations; these numbers work
441  * on a pxa250 as well as more flexible hardware.
442  */
443 #define CONFIG_SOURCE_SINK      3
444 #define CONFIG_LOOPBACK         2
445
446 /*
447 static struct usb_device_descriptor
448 device_desc = {
449         .bLength =              sizeof device_desc,
450         .bDescriptorType =      USB_DT_DEVICE,
451
452         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
453         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
454
455         .idVendor =             __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
456         .idProduct =            __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
457         .iManufacturer =        STRING_MANUFACTURER,
458         .iProduct =             STRING_PRODUCT,
459         .iSerialNumber =        STRING_SERIAL,
460         .bNumConfigurations =   2,
461 };
462 */
463 static struct usb_device_descriptor
464 device_desc = {
465         .bLength =              sizeof device_desc,
466         .bDescriptorType =      USB_DT_DEVICE,
467         .bcdUSB =               __constant_cpu_to_le16 (0x0100),
468         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
469         .bDeviceSubClass =      0,
470         .bDeviceProtocol =      0,
471         .bMaxPacketSize0 =      64,
472         .bcdDevice =            __constant_cpu_to_le16 (0x0100),
473         .idVendor =             __constant_cpu_to_le16 (0x0499),
474         .idProduct =            __constant_cpu_to_le16 (0x3002),
475         .iManufacturer =        STRING_MANUFACTURER,
476         .iProduct =             STRING_PRODUCT,
477         .iSerialNumber =        STRING_SERIAL,
478         .bNumConfigurations =   1,
479 };
480
481 static struct usb_config_descriptor
482 z_config = {
483         .bLength =              sizeof z_config,
484         .bDescriptorType =      USB_DT_CONFIG,
485
486         /* compute wTotalLength on the fly */
487         .bNumInterfaces =       2,
488         .bConfigurationValue =  1,
489         .iConfiguration =       0,
490         .bmAttributes =         0x40,
491         .bMaxPower =            0,      /* self-powered */
492 };
493
494
495 static struct usb_otg_descriptor
496 otg_descriptor = {
497         .bLength =              sizeof otg_descriptor,
498         .bDescriptorType =      USB_DT_OTG,
499
500         .bmAttributes =         USB_OTG_SRP,
501 };
502
503 /* one interface in each configuration */
504 #ifdef  CONFIG_USB_GADGET_DUALSPEED
505
506 /*
507  * usb 2.0 devices need to expose both high speed and full speed
508  * descriptors, unless they only run at full speed.
509  *
510  * that means alternate endpoint descriptors (bigger packets)
511  * and a "device qualifier" ... plus more construction options
512  * for the config descriptor.
513  */
514
515 static struct usb_qualifier_descriptor
516 dev_qualifier = {
517         .bLength =              sizeof dev_qualifier,
518         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
519
520         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
521         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
522
523         .bNumConfigurations =   2,
524 };
525
526
527 struct usb_cs_as_general_descriptor {
528         __u8  bLength;
529         __u8  bDescriptorType;
530
531         __u8  bDescriptorSubType;
532         __u8  bTerminalLink;
533         __u8  bDelay;
534         __u16  wFormatTag;
535 } __attribute__ ((packed));
536
537 struct usb_cs_as_format_descriptor {
538         __u8  bLength;
539         __u8  bDescriptorType;
540
541         __u8  bDescriptorSubType;
542         __u8  bFormatType;
543         __u8  bNrChannels;
544         __u8  bSubframeSize;
545         __u8  bBitResolution;
546         __u8  bSamfreqType;
547         __u8  tLowerSamFreq[3];
548         __u8  tUpperSamFreq[3];
549 } __attribute__ ((packed));
550
551 static const struct usb_interface_descriptor
552 z_audio_control_if_desc = {
553         .bLength =              sizeof z_audio_control_if_desc,
554         .bDescriptorType =      USB_DT_INTERFACE,
555         .bInterfaceNumber = 0,
556         .bAlternateSetting = 0,
557         .bNumEndpoints = 0,
558         .bInterfaceClass = USB_CLASS_AUDIO,
559         .bInterfaceSubClass = 0x1,
560         .bInterfaceProtocol = 0,
561         .iInterface = 0,
562 };
563
564 static const struct usb_interface_descriptor
565 z_audio_if_desc = {
566         .bLength =              sizeof z_audio_if_desc,
567         .bDescriptorType =      USB_DT_INTERFACE,
568         .bInterfaceNumber = 1,
569         .bAlternateSetting = 0,
570         .bNumEndpoints = 0,
571         .bInterfaceClass = USB_CLASS_AUDIO,
572         .bInterfaceSubClass = 0x2,
573         .bInterfaceProtocol = 0,
574         .iInterface = 0,
575 };
576
577 static const struct usb_interface_descriptor
578 z_audio_if_desc2 = {
579         .bLength =              sizeof z_audio_if_desc,
580         .bDescriptorType =      USB_DT_INTERFACE,
581         .bInterfaceNumber = 1,
582         .bAlternateSetting = 1,
583         .bNumEndpoints = 1,
584         .bInterfaceClass = USB_CLASS_AUDIO,
585         .bInterfaceSubClass = 0x2,
586         .bInterfaceProtocol = 0,
587         .iInterface = 0,
588 };
589
590 static const struct usb_cs_as_general_descriptor
591 z_audio_cs_as_if_desc = {
592         .bLength = 7,
593         .bDescriptorType = 0x24,
594         
595         .bDescriptorSubType = 0x01,
596         .bTerminalLink = 0x01,
597         .bDelay = 0x0,
598         .wFormatTag = __constant_cpu_to_le16 (0x0001)
599 };
600
601
602 static const struct usb_cs_as_format_descriptor 
603 z_audio_cs_as_format_desc = {
604         .bLength = 0xe,
605         .bDescriptorType = 0x24,
606         
607         .bDescriptorSubType = 2,
608         .bFormatType = 1,
609         .bNrChannels = 1,
610         .bSubframeSize = 1,
611         .bBitResolution = 8,
612         .bSamfreqType = 0,
613         .tLowerSamFreq = {0x7e, 0x13, 0x00},
614         .tUpperSamFreq = {0xe2, 0xd6, 0x00},
615 };
616
617 static const struct usb_endpoint_descriptor 
618 z_iso_ep = {
619         .bLength = 0x09,
620         .bDescriptorType = 0x05,
621         .bEndpointAddress = 0x04,
622         .bmAttributes = 0x09,
623         .wMaxPacketSize = 0x0038,
624         .bInterval = 0x01,
625         .bRefresh = 0x00,
626         .bSynchAddress = 0x00,  
627 };
628
629 static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
630
631 // 9 bytes
632 static char z_ac_interface_header_desc[] = 
633 { 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
634
635 // 12 bytes
636 static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02, 
637                      0x03, 0x00, 0x00, 0x00};
638 // 13 bytes
639 static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00, 
640                      0x02, 0x00, 0x02, 0x00, 0x00};
641 // 9 bytes
642 static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02, 
643                      0x00};
644
645 static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00, 
646                       0x00};
647
648 static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
649
650 static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00, 
651                       0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
652
653 static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
654                       0x00};
655
656 static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
657
658 static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
659                       0x00};
660
661 static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
662
663 static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
664                       0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
665
666 static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
667                       0x00};
668
669 static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
670
671 static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
672                        0x00};
673
674 static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
675
676 static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
677                        0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
678
679 static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
680                        0x00};
681
682 static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
683
684 static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00, 
685                        0x00};
686
687 static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
688
689 static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00, 
690                        0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
691
692 static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
693                        0x00};
694
695 static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
696
697 static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
698                        0x00};
699
700 static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
701
702 static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00, 
703                        0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
704
705 static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
706                        0x00};
707
708 static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
709
710
711
712 static const struct usb_descriptor_header *z_function [] = {
713         (struct usb_descriptor_header *) &z_audio_control_if_desc,
714         (struct usb_descriptor_header *) &z_ac_interface_header_desc,
715         (struct usb_descriptor_header *) &z_0,
716         (struct usb_descriptor_header *) &z_1,
717         (struct usb_descriptor_header *) &z_2,
718         (struct usb_descriptor_header *) &z_audio_if_desc,
719         (struct usb_descriptor_header *) &z_audio_if_desc2,
720         (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
721         (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
722         (struct usb_descriptor_header *) &z_iso_ep,
723         (struct usb_descriptor_header *) &z_iso_ep2,
724         (struct usb_descriptor_header *) &za_0,
725         (struct usb_descriptor_header *) &za_1,
726         (struct usb_descriptor_header *) &za_2,
727         (struct usb_descriptor_header *) &za_3,
728         (struct usb_descriptor_header *) &za_4,
729         (struct usb_descriptor_header *) &za_5,
730         (struct usb_descriptor_header *) &za_6,
731         (struct usb_descriptor_header *) &za_7,
732         (struct usb_descriptor_header *) &za_8,
733         (struct usb_descriptor_header *) &za_9,
734         (struct usb_descriptor_header *) &za_10,
735         (struct usb_descriptor_header *) &za_11,
736         (struct usb_descriptor_header *) &za_12,
737         (struct usb_descriptor_header *) &za_13,
738         (struct usb_descriptor_header *) &za_14,
739         (struct usb_descriptor_header *) &za_15,
740         (struct usb_descriptor_header *) &za_16,
741         (struct usb_descriptor_header *) &za_17,
742         (struct usb_descriptor_header *) &za_18,
743         (struct usb_descriptor_header *) &za_19,
744         (struct usb_descriptor_header *) &za_20,
745         (struct usb_descriptor_header *) &za_21,
746         (struct usb_descriptor_header *) &za_22,
747         (struct usb_descriptor_header *) &za_23,
748         (struct usb_descriptor_header *) &za_24,
749         NULL,
750 };
751
752 /* maxpacket and other transfer characteristics vary by speed. */
753 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
754
755 #else
756
757 /* if there's no high speed support, maxpacket doesn't change. */
758 #define ep_desc(g,hs,fs) fs
759
760 #endif  /* !CONFIG_USB_GADGET_DUALSPEED */
761
762 static char                             manufacturer [40];
763 //static char                           serial [40];
764 static char                             serial [] = "Ser 00 em";
765
766 /* static strings, in UTF-8 */
767 static struct usb_string                strings [] = {
768         { STRING_MANUFACTURER, manufacturer, },
769         { STRING_PRODUCT, longname, },
770         { STRING_SERIAL, serial, },
771         { STRING_LOOPBACK, loopback, },
772         { STRING_SOURCE_SINK, source_sink, },
773         {  }                    /* end of list */
774 };
775
776 static struct usb_gadget_strings        stringtab = {
777         .language       = 0x0409,       /* en-us */
778         .strings        = strings,
779 };
780
781 /*
782  * config descriptors are also handcrafted.  these must agree with code
783  * that sets configurations, and with code managing interfaces and their
784  * altsettings.  other complexity may come from:
785  *
786  *  - high speed support, including "other speed config" rules
787  *  - multiple configurations
788  *  - interfaces with alternate settings
789  *  - embedded class or vendor-specific descriptors
790  *
791  * this handles high speed, and has a second config that could as easily
792  * have been an alternate interface setting (on most hardware).
793  *
794  * NOTE:  to demonstrate (and test) more USB capabilities, this driver
795  * should include an altsetting to test interrupt transfers, including
796  * high bandwidth modes at high speed.  (Maybe work like Intel's test
797  * device?)
798  */
799 static int
800 config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
801 {
802         int len;
803         const struct usb_descriptor_header **function;
804         
805         function = z_function;
806         len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
807         if (len < 0)
808                 return len;
809         ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
810         return len;
811 }
812
813 /*-------------------------------------------------------------------------*/
814
815 static struct usb_request *
816 alloc_ep_req (struct usb_ep *ep, unsigned length)
817 {
818         struct usb_request      *req;
819
820         req = usb_ep_alloc_request (ep, GFP_ATOMIC);
821         if (req) {
822                 req->length = length;
823                 req->buf = usb_ep_alloc_buffer (ep, length,
824                                 &req->dma, GFP_ATOMIC);
825                 if (!req->buf) {
826                         usb_ep_free_request (ep, req);
827                         req = NULL;
828                 }
829         }
830         return req;
831 }
832
833 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
834 {
835         if (req->buf)
836                 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
837         usb_ep_free_request (ep, req);
838 }
839
840 /*-------------------------------------------------------------------------*/
841
842 /* optionally require specific source/sink data patterns  */
843
844 static int
845 check_read_data (
846         struct zero_dev         *dev,
847         struct usb_ep           *ep,
848         struct usb_request      *req
849 )
850 {
851         unsigned        i;
852         u8              *buf = req->buf;
853
854         for (i = 0; i < req->actual; i++, buf++) {
855                 switch (pattern) {
856                 /* all-zeroes has no synchronization issues */
857                 case 0:
858                         if (*buf == 0)
859                                 continue;
860                         break;
861                 /* mod63 stays in sync with short-terminated transfers,
862                  * or otherwise when host and gadget agree on how large
863                  * each usb transfer request should be.  resync is done
864                  * with set_interface or set_config.
865                  */
866                 case 1:
867                         if (*buf == (u8)(i % 63))
868                                 continue;
869                         break;
870                 }
871                 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
872                 usb_ep_set_halt (ep);
873                 return -EINVAL;
874         }
875         return 0;
876 }
877
878 /*-------------------------------------------------------------------------*/
879
880 static void zero_reset_config (struct zero_dev *dev)
881 {
882         if (dev->config == 0)
883                 return;
884
885         DBG (dev, "reset config\n");
886
887         /* just disable endpoints, forcing completion of pending i/o.
888          * all our completion handlers free their requests in this case.
889          */
890         if (dev->in_ep) {
891                 usb_ep_disable (dev->in_ep);
892                 dev->in_ep = NULL;
893         }
894         if (dev->out_ep) {
895                 usb_ep_disable (dev->out_ep);
896                 dev->out_ep = NULL;
897         }
898         dev->config = 0;
899         del_timer (&dev->resume);
900 }
901
902 #define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
903
904 static void 
905 zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
906 {
907         struct zero_dev *dev = ep->driver_data;
908         int             status = req->status;
909         int i, j;
910
911         switch (status) {
912
913         case 0:                         /* normal completion? */
914                 //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
915                 for (i=0, j=rbuf_start; i<req->actual; i++) {
916                         //printk ("%02x ", ((__u8*)req->buf)[i]);
917                         rbuf[j] = ((__u8*)req->buf)[i];
918                         j++;
919                         if (j >= RBUF_LEN) j=0;
920                 }
921                 rbuf_start = j;
922                 //printk ("\n\n");
923
924                 if (rbuf_len < RBUF_LEN) {
925                         rbuf_len += req->actual;
926                         if (rbuf_len > RBUF_LEN) {
927                                 rbuf_len = RBUF_LEN;
928                         }
929                 }
930
931                 break;
932
933         /* this endpoint is normally active while we're configured */
934         case -ECONNABORTED:             /* hardware forced ep reset */
935         case -ECONNRESET:               /* request dequeued */
936         case -ESHUTDOWN:                /* disconnect from host */
937                 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
938                                 req->actual, req->length);
939                 if (ep == dev->out_ep)
940                         check_read_data (dev, ep, req);
941                 free_ep_req (ep, req);
942                 return;
943
944         case -EOVERFLOW:                /* buffer overrun on read means that
945                                          * we didn't provide a big enough
946                                          * buffer.
947                                          */
948         default:
949 #if 1
950                 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
951                                 status, req->actual, req->length);
952 #endif
953         case -EREMOTEIO:                /* short read */
954                 break;
955         }
956
957         status = usb_ep_queue (ep, req, GFP_ATOMIC);
958         if (status) {
959                 ERROR (dev, "kill %s:  resubmit %d bytes --> %d\n",
960                                 ep->name, req->length, status);
961                 usb_ep_set_halt (ep);
962                 /* FIXME recover later ... somehow */
963         }
964 }
965
966 static struct usb_request *
967 zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
968 {
969         struct usb_request      *req;
970         int                     status;
971
972         req = alloc_ep_req (ep, 512);
973         if (!req)
974                 return NULL;
975
976         req->complete = zero_isoc_complete;
977
978         status = usb_ep_queue (ep, req, gfp_flags);
979         if (status) {
980                 struct zero_dev *dev = ep->driver_data;
981
982                 ERROR (dev, "start %s --> %d\n", ep->name, status);
983                 free_ep_req (ep, req);
984                 req = NULL;
985         }
986
987         return req;
988 }
989
990 /* change our operational config.  this code must agree with the code
991  * that returns config descriptors, and altsetting code.
992  *
993  * it's also responsible for power management interactions. some
994  * configurations might not work with our current power sources.
995  *
996  * note that some device controller hardware will constrain what this
997  * code can do, perhaps by disallowing more than one configuration or
998  * by limiting configuration choices (like the pxa2xx).
999  */
1000 static int
1001 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
1002 {
1003         int                     result = 0;
1004         struct usb_gadget       *gadget = dev->gadget;
1005         const struct usb_endpoint_descriptor    *d;
1006         struct usb_ep           *ep;
1007
1008         if (number == dev->config)
1009                 return 0;
1010
1011         zero_reset_config (dev);
1012
1013         gadget_for_each_ep (ep, gadget) {
1014
1015                 if (strcmp (ep->name, "ep4") == 0) {
1016
1017                         d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
1018                         result = usb_ep_enable (ep, d);
1019
1020                         if (result == 0) {
1021                                 ep->driver_data = dev;
1022                                 dev->in_ep = ep;
1023
1024                                 if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
1025
1026                                         dev->in_ep = ep;
1027                                         continue;
1028                                 }
1029
1030                                 usb_ep_disable (ep);
1031                                 result = -EIO;
1032                         }
1033                 }
1034
1035         }
1036
1037         dev->config = number;
1038         return result;
1039 }
1040
1041 /*-------------------------------------------------------------------------*/
1042
1043 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
1044 {
1045         if (req->status || req->actual != req->length)
1046                 DBG ((struct zero_dev *) ep->driver_data,
1047                                 "setup complete --> %d, %d/%d\n",
1048                                 req->status, req->actual, req->length);
1049 }
1050
1051 /*
1052  * The setup() callback implements all the ep0 functionality that's
1053  * not handled lower down, in hardware or the hardware driver (like
1054  * device and endpoint feature flags, and their status).  It's all
1055  * housekeeping for the gadget function we're implementing.  Most of
1056  * the work is in config-specific setup.
1057  */
1058 static int
1059 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1060 {
1061         struct zero_dev         *dev = get_gadget_data (gadget);
1062         struct usb_request      *req = dev->req;
1063         int                     value = -EOPNOTSUPP;
1064
1065         /* usually this stores reply data in the pre-allocated ep0 buffer,
1066          * but config change events will reconfigure hardware.
1067          */
1068         req->zero = 0;
1069         switch (ctrl->bRequest) {
1070
1071         case USB_REQ_GET_DESCRIPTOR:
1072
1073                 switch (ctrl->wValue >> 8) {
1074
1075                 case USB_DT_DEVICE:
1076                         value = min (ctrl->wLength, (u16) sizeof device_desc);
1077                         memcpy (req->buf, &device_desc, value);
1078                         break;
1079 #ifdef CONFIG_USB_GADGET_DUALSPEED
1080                 case USB_DT_DEVICE_QUALIFIER:
1081                         if (!gadget->is_dualspeed)
1082                                 break;
1083                         value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1084                         memcpy (req->buf, &dev_qualifier, value);
1085                         break;
1086
1087                 case USB_DT_OTHER_SPEED_CONFIG:
1088                         if (!gadget->is_dualspeed)
1089                                 break;
1090                         // FALLTHROUGH
1091 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1092                 case USB_DT_CONFIG:
1093                         value = config_buf (gadget, req->buf,
1094                                         ctrl->wValue >> 8,
1095                                         ctrl->wValue & 0xff);
1096                         if (value >= 0)
1097                                 value = min (ctrl->wLength, (u16) value);
1098                         break;
1099
1100                 case USB_DT_STRING:
1101                         /* wIndex == language code.
1102                          * this driver only handles one language, you can
1103                          * add string tables for other languages, using
1104                          * any UTF-8 characters
1105                          */
1106                         value = usb_gadget_get_string (&stringtab,
1107                                         ctrl->wValue & 0xff, req->buf);
1108                         if (value >= 0) {
1109                                 value = min (ctrl->wLength, (u16) value);
1110                         }
1111                         break;
1112                 }
1113                 break;
1114
1115         /* currently two configs, two speeds */
1116         case USB_REQ_SET_CONFIGURATION:
1117                 if (ctrl->bRequestType != 0)
1118                         goto unknown;
1119
1120                 spin_lock (&dev->lock);
1121                 value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1122                 spin_unlock (&dev->lock);
1123                 break;
1124         case USB_REQ_GET_CONFIGURATION:
1125                 if (ctrl->bRequestType != USB_DIR_IN)
1126                         goto unknown;
1127                 *(u8 *)req->buf = dev->config;
1128                 value = min (ctrl->wLength, (u16) 1);
1129                 break;
1130
1131         /* until we add altsetting support, or other interfaces,
1132          * only 0/0 are possible.  pxa2xx only supports 0/0 (poorly)
1133          * and already killed pending endpoint I/O.
1134          */
1135         case USB_REQ_SET_INTERFACE:
1136
1137                 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1138                         goto unknown;
1139                 spin_lock (&dev->lock);
1140                 if (dev->config) {
1141                         u8              config = dev->config;
1142
1143                         /* resets interface configuration, forgets about
1144                          * previous transaction state (queued bufs, etc)
1145                          * and re-inits endpoint state (toggle etc)
1146                          * no response queued, just zero status == success.
1147                          * if we had more than one interface we couldn't
1148                          * use this "reset the config" shortcut.
1149                          */
1150                         zero_reset_config (dev);
1151                         zero_set_config (dev, config, GFP_ATOMIC);
1152                         value = 0;
1153                 }
1154                 spin_unlock (&dev->lock);
1155                 break;
1156         case USB_REQ_GET_INTERFACE:
1157                 if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
1158                         value = ctrl->wLength;
1159                         break;
1160                 }
1161                 else {
1162                         if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1163                                 goto unknown;
1164                         if (!dev->config)
1165                                 break;
1166                         if (ctrl->wIndex != 0) {
1167                                 value = -EDOM;
1168                                 break;
1169                         }
1170                         *(u8 *)req->buf = 0;
1171                         value = min (ctrl->wLength, (u16) 1);
1172                 }
1173                 break;
1174
1175         /*
1176          * These are the same vendor-specific requests supported by
1177          * Intel's USB 2.0 compliance test devices.  We exceed that
1178          * device spec by allowing multiple-packet requests.
1179          */
1180         case 0x5b:      /* control WRITE test -- fill the buffer */
1181                 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1182                         goto unknown;
1183                 if (ctrl->wValue || ctrl->wIndex)
1184                         break;
1185                 /* just read that many bytes into the buffer */
1186                 if (ctrl->wLength > USB_BUFSIZ)
1187                         break;
1188                 value = ctrl->wLength;
1189                 break;
1190         case 0x5c:      /* control READ test -- return the buffer */
1191                 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1192                         goto unknown;
1193                 if (ctrl->wValue || ctrl->wIndex)
1194                         break;
1195                 /* expect those bytes are still in the buffer; send back */
1196                 if (ctrl->wLength > USB_BUFSIZ
1197                                 || ctrl->wLength != req->length)
1198                         break;
1199                 value = ctrl->wLength;
1200                 break;
1201
1202         case 0x01: // SET_CUR
1203         case 0x02:
1204         case 0x03:
1205         case 0x04:
1206         case 0x05:
1207                 value = ctrl->wLength;
1208                 break;
1209         case 0x81:
1210                 switch (ctrl->wValue) {
1211                 case 0x0201:
1212                 case 0x0202:
1213                         ((u8*)req->buf)[0] = 0x00;
1214                         ((u8*)req->buf)[1] = 0xe3;
1215                         break;
1216                 case 0x0300:
1217                 case 0x0500:
1218                         ((u8*)req->buf)[0] = 0x00;
1219                         break;
1220                 }
1221                 //((u8*)req->buf)[0] = 0x81;
1222                 //((u8*)req->buf)[1] = 0x81;
1223                 value = ctrl->wLength;
1224                 break;
1225         case 0x82:
1226                 switch (ctrl->wValue) {
1227                 case 0x0201:
1228                 case 0x0202:
1229                         ((u8*)req->buf)[0] = 0x00;
1230                         ((u8*)req->buf)[1] = 0xc3;
1231                         break;
1232                 case 0x0300:
1233                 case 0x0500:
1234                         ((u8*)req->buf)[0] = 0x00;
1235                         break;
1236                 }
1237                 //((u8*)req->buf)[0] = 0x82;
1238                 //((u8*)req->buf)[1] = 0x82;
1239                 value = ctrl->wLength;
1240                 break;
1241         case 0x83:
1242                 switch (ctrl->wValue) {
1243                 case 0x0201:
1244                 case 0x0202:
1245                         ((u8*)req->buf)[0] = 0x00;
1246                         ((u8*)req->buf)[1] = 0x00;
1247                         break;
1248                 case 0x0300:
1249                         ((u8*)req->buf)[0] = 0x60;
1250                         break;
1251                 case 0x0500:    
1252                         ((u8*)req->buf)[0] = 0x18;
1253                         break;
1254                 }
1255                 //((u8*)req->buf)[0] = 0x83;
1256                 //((u8*)req->buf)[1] = 0x83;
1257                 value = ctrl->wLength;
1258                 break;
1259         case 0x84:
1260                 switch (ctrl->wValue) {
1261                 case 0x0201:
1262                 case 0x0202:
1263                         ((u8*)req->buf)[0] = 0x00;
1264                         ((u8*)req->buf)[1] = 0x01;
1265                         break;
1266                 case 0x0300:
1267                 case 0x0500:
1268                         ((u8*)req->buf)[0] = 0x08;
1269                         break;
1270                 }
1271                 //((u8*)req->buf)[0] = 0x84;
1272                 //((u8*)req->buf)[1] = 0x84;
1273                 value = ctrl->wLength;
1274                 break;
1275         case 0x85:
1276                 ((u8*)req->buf)[0] = 0x85;
1277                 ((u8*)req->buf)[1] = 0x85;
1278                 value = ctrl->wLength;
1279                 break;
1280
1281         
1282         default:
1283 unknown:
1284                 printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
1285                         ctrl->bRequestType, ctrl->bRequest,
1286                         ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1287         }
1288
1289         /* respond with data transfer before status phase? */
1290         if (value >= 0) {
1291                 req->length = value;
1292                 req->zero = value < ctrl->wLength
1293                                 && (value % gadget->ep0->maxpacket) == 0;
1294                 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1295                 if (value < 0) {
1296                         DBG (dev, "ep_queue < 0 --> %d\n", value);
1297                         req->status = 0;
1298                         zero_setup_complete (gadget->ep0, req);
1299                 }
1300         }
1301
1302         /* device either stalls (value < 0) or reports success */
1303         return value;
1304 }
1305
1306 static void
1307 zero_disconnect (struct usb_gadget *gadget)
1308 {
1309         struct zero_dev         *dev = get_gadget_data (gadget);
1310         unsigned long           flags;
1311
1312         spin_lock_irqsave (&dev->lock, flags);
1313         zero_reset_config (dev);
1314
1315         /* a more significant application might have some non-usb
1316          * activities to quiesce here, saving resources like power
1317          * or pushing the notification up a network stack.
1318          */
1319         spin_unlock_irqrestore (&dev->lock, flags);
1320
1321         /* next we may get setup() calls to enumerate new connections;
1322          * or an unbind() during shutdown (including removing module).
1323          */
1324 }
1325
1326 static void
1327 zero_autoresume (unsigned long _dev)
1328 {
1329         struct zero_dev *dev = (struct zero_dev *) _dev;
1330         int             status;
1331
1332         /* normally the host would be woken up for something
1333          * more significant than just a timer firing...
1334          */
1335         if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1336                 status = usb_gadget_wakeup (dev->gadget);
1337                 DBG (dev, "wakeup --> %d\n", status);
1338         }
1339 }
1340
1341 /*-------------------------------------------------------------------------*/
1342
1343 static void
1344 zero_unbind (struct usb_gadget *gadget)
1345 {
1346         struct zero_dev         *dev = get_gadget_data (gadget);
1347
1348         DBG (dev, "unbind\n");
1349
1350         /* we've already been disconnected ... no i/o is active */
1351         if (dev->req)
1352                 free_ep_req (gadget->ep0, dev->req);
1353         del_timer_sync (&dev->resume);
1354         kfree (dev);
1355         set_gadget_data (gadget, NULL);
1356 }
1357
1358 static int
1359 zero_bind (struct usb_gadget *gadget)
1360 {
1361         struct zero_dev         *dev;
1362         //struct usb_ep         *ep;
1363
1364         printk("binding\n");
1365         /*
1366          * DRIVER POLICY CHOICE:  you may want to do this differently.
1367          * One thing to avoid is reusing a bcdDevice revision code
1368          * with different host-visible configurations or behavior
1369          * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1370          */
1371         //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
1372
1373
1374         /* ok, we made sense of the hardware ... */
1375         dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1376         if (!dev)
1377                 return -ENOMEM;
1378         memset (dev, 0, sizeof *dev);
1379         spin_lock_init (&dev->lock);
1380         dev->gadget = gadget;
1381         set_gadget_data (gadget, dev);
1382
1383         /* preallocate control response and buffer */
1384         dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1385         if (!dev->req)
1386                 goto enomem;
1387         dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1388                                 &dev->req->dma, GFP_KERNEL);
1389         if (!dev->req->buf)
1390                 goto enomem;
1391
1392         dev->req->complete = zero_setup_complete;
1393
1394         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1395
1396 #ifdef CONFIG_USB_GADGET_DUALSPEED
1397         /* assume ep0 uses the same value for both speeds ... */
1398         dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1399
1400         /* and that all endpoints are dual-speed */
1401         //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1402         //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1403 #endif
1404
1405         usb_gadget_set_selfpowered (gadget);
1406
1407         init_timer (&dev->resume);
1408         dev->resume.function = zero_autoresume;
1409         dev->resume.data = (unsigned long) dev;
1410
1411         gadget->ep0->driver_data = dev;
1412
1413         INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1414         INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1415                 EP_OUT_NAME, EP_IN_NAME);
1416
1417         snprintf (manufacturer, sizeof manufacturer,
1418                 UTS_SYSNAME " " UTS_RELEASE " with %s",
1419                 gadget->name);
1420
1421         return 0;
1422
1423 enomem:
1424         zero_unbind (gadget);
1425         return -ENOMEM;
1426 }
1427
1428 /*-------------------------------------------------------------------------*/
1429
1430 static void
1431 zero_suspend (struct usb_gadget *gadget)
1432 {
1433         struct zero_dev         *dev = get_gadget_data (gadget);
1434
1435         if (gadget->speed == USB_SPEED_UNKNOWN)
1436                 return;
1437
1438         if (autoresume) {
1439                 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1440                 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1441         } else
1442                 DBG (dev, "suspend\n");
1443 }
1444
1445 static void
1446 zero_resume (struct usb_gadget *gadget)
1447 {
1448         struct zero_dev         *dev = get_gadget_data (gadget);
1449
1450         DBG (dev, "resume\n");
1451         del_timer (&dev->resume);
1452 }
1453
1454
1455 /*-------------------------------------------------------------------------*/
1456
1457 static struct usb_gadget_driver zero_driver = {
1458 #ifdef CONFIG_USB_GADGET_DUALSPEED
1459         .speed          = USB_SPEED_HIGH,
1460 #else
1461         .speed          = USB_SPEED_FULL,
1462 #endif
1463         .function       = (char *) longname,
1464         .bind           = zero_bind,
1465         .unbind         = zero_unbind,
1466
1467         .setup          = zero_setup,
1468         .disconnect     = zero_disconnect,
1469
1470         .suspend        = zero_suspend,
1471         .resume         = zero_resume,
1472
1473         .driver         = {
1474                 .name           = (char *) shortname,
1475                 // .shutdown = ...
1476                 // .suspend = ...
1477                 // .resume = ...
1478         },
1479 };
1480
1481 MODULE_AUTHOR ("David Brownell");
1482 MODULE_LICENSE ("Dual BSD/GPL");
1483
1484 static struct proc_dir_entry *pdir, *pfile;
1485
1486 static int isoc_read_data (char *page, char **start,
1487                            off_t off, int count,
1488                            int *eof, void *data)
1489 {
1490         int i;
1491         static int c = 0;
1492         static int done = 0;
1493         static int s = 0;
1494
1495 /*
1496         printk ("\ncount: %d\n", count);
1497         printk ("rbuf_start: %d\n", rbuf_start);
1498         printk ("rbuf_len: %d\n", rbuf_len);
1499         printk ("off: %d\n", off);
1500         printk ("start: %p\n\n", *start);
1501 */
1502         if (done) {
1503                 c = 0;
1504                 done = 0;
1505                 *eof = 1;
1506                 return 0;
1507         }
1508
1509         if (c == 0) {
1510                 if (rbuf_len == RBUF_LEN)
1511                         s = rbuf_start;
1512                 else s = 0;
1513         }
1514
1515         for (i=0; i<count && c<rbuf_len; i++, c++) {
1516                 page[i] = rbuf[(c+s) % RBUF_LEN];
1517         }
1518         *start = page;
1519         
1520         if (c >= rbuf_len) {
1521                 *eof = 1;
1522                 done = 1;
1523         }
1524
1525
1526         return i;
1527 }
1528
1529 static int __init init (void)
1530 {
1531
1532         int retval = 0;
1533
1534         pdir = proc_mkdir("isoc_test", NULL);
1535         if(pdir == NULL) {
1536                 retval = -ENOMEM;
1537                 printk("Error creating dir\n");
1538                 goto done;
1539         }
1540         pdir->owner = THIS_MODULE;
1541
1542         pfile = create_proc_read_entry("isoc_data",
1543                                        0444, pdir,
1544                                        isoc_read_data,
1545                                        NULL);
1546         if (pfile == NULL) {
1547                 retval = -ENOMEM;
1548                 printk("Error creating file\n");
1549                 goto no_file;
1550         }
1551         pfile->owner = THIS_MODULE;
1552
1553         return usb_gadget_register_driver (&zero_driver);
1554
1555  no_file:
1556         remove_proc_entry("isoc_data", NULL);
1557  done:
1558         return retval;
1559 }
1560 module_init (init);
1561
1562 static void __exit cleanup (void)
1563 {
1564
1565         usb_gadget_unregister_driver (&zero_driver);
1566         
1567         remove_proc_entry("isoc_data", pdir);
1568         remove_proc_entry("isoc_test", NULL);
1569 }
1570 module_exit (cleanup);