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