dummy_audio.c

00001 /*
00002  * zero.c -- Gadget Zero, for USB development
00003  *
00004  * Copyright (C) 2003-2004 David Brownell
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions, and the following disclaimer,
00012  *    without modification.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. The names of the above-listed copyright holders may not be used
00017  *    to endorse or promote products derived from this software without
00018  *    specific prior written permission.
00019  *
00020  * ALTERNATIVELY, this software may be distributed under the terms of the
00021  * GNU General Public License ("GPL") as published by the Free Software
00022  * Foundation, either version 2 of that License or (at your option) any
00023  * later version.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00026  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00027  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00029  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 
00039 /*
00040  * Gadget Zero only needs two bulk endpoints, and is an example of how you
00041  * can write a hardware-agnostic gadget driver running inside a USB device.
00042  *
00043  * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
00044  * affect most of the driver.
00045  *
00046  * Use it with the Linux host/master side "usbtest" driver to get a basic
00047  * functional test of your device-side usb stack, or with "usb-skeleton".
00048  *
00049  * It supports two similar configurations.  One sinks whatever the usb host
00050  * writes, and in return sources zeroes.  The other loops whatever the host
00051  * writes back, so the host can read it.  Module options include:
00052  *
00053  *   buflen=N           default N=4096, buffer size used
00054  *   qlen=N             default N=32, how many buffers in the loopback queue
00055  *   loopdefault        default false, list loopback config first
00056  *
00057  * Many drivers will only have one configuration, letting them be much
00058  * simpler if they also don't support high speed operation (like this
00059  * driver does).
00060  */
00061 
00062 #include <linux/config.h>
00063 #include <linux/module.h>
00064 #include <linux/kernel.h>
00065 #include <linux/delay.h>
00066 #include <linux/ioport.h>
00067 #include <linux/sched.h>
00068 #include <linux/slab.h>
00069 #include <linux/smp_lock.h>
00070 #include <linux/errno.h>
00071 #include <linux/init.h>
00072 #include <linux/timer.h>
00073 #include <linux/list.h>
00074 #include <linux/interrupt.h>
00075 #include <linux/uts.h>
00076 #include <linux/version.h>
00077 #include <linux/device.h>
00078 #include <linux/moduleparam.h>
00079 #include <linux/proc_fs.h>
00080 
00081 #include <asm/byteorder.h>
00082 #include <asm/io.h>
00083 #include <asm/irq.h>
00084 #include <asm/system.h>
00085 #include <asm/unaligned.h>
00086 
00087 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
00088 # include <linux/usb/ch9.h>
00089 #else
00090 # include <linux/usb_ch9.h>
00091 #endif
00092 
00093 #include <linux/usb_gadget.h>
00094 
00095 
00096 /*-------------------------------------------------------------------------*/
00097 /*-------------------------------------------------------------------------*/
00098 
00099 
00100 static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
00101 {
00102         int     count = 0;
00103         u8      c;
00104         u16     uchar;
00105 
00106         /* this insists on correct encodings, though not minimal ones.
00107          * BUT it currently rejects legit 4-byte UTF-8 code points,
00108          * which need surrogate pairs.  (Unicode 3.1 can use them.)
00109          */
00110         while (len != 0 && (c = (u8) *s++) != 0) {
00111                 if (unlikely(c & 0x80)) {
00112                         // 2-byte sequence:
00113                         // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
00114                         if ((c & 0xe0) == 0xc0) {
00115                                 uchar = (c & 0x1f) << 6;
00116 
00117                                 c = (u8) *s++;
00118                                 if ((c & 0xc0) != 0xc0)
00119                                         goto fail;
00120                                 c &= 0x3f;
00121                                 uchar |= c;
00122 
00123                         // 3-byte sequence (most CJKV characters):
00124                         // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
00125                         } else if ((c & 0xf0) == 0xe0) {
00126                                 uchar = (c & 0x0f) << 12;
00127 
00128                                 c = (u8) *s++;
00129                                 if ((c & 0xc0) != 0xc0)
00130                                         goto fail;
00131                                 c &= 0x3f;
00132                                 uchar |= c << 6;
00133 
00134                                 c = (u8) *s++;
00135                                 if ((c & 0xc0) != 0xc0)
00136                                         goto fail;
00137                                 c &= 0x3f;
00138                                 uchar |= c;
00139 
00140                                 /* no bogus surrogates */
00141                                 if (0xd800 <= uchar && uchar <= 0xdfff)
00142                                         goto fail;
00143 
00144                         // 4-byte sequence (surrogate pairs, currently rare):
00145                         // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
00146                         //     = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
00147                         // (uuuuu = wwww + 1)
00148                         // FIXME accept the surrogate code points (only)
00149 
00150                         } else
00151                                 goto fail;
00152                 } else
00153                         uchar = c;
00154                 put_unaligned (cpu_to_le16 (uchar), cp++);
00155                 count++;
00156                 len--;
00157         }
00158         return count;
00159 fail:
00160         return -1;
00161 }
00162 
00163 
00181 int
00182 usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
00183 {
00184         struct usb_string       *s;
00185         int                     len;
00186 
00187         /* descriptor 0 has the language id */
00188         if (id == 0) {
00189                 buf [0] = 4;
00190                 buf [1] = USB_DT_STRING;
00191                 buf [2] = (u8) table->language;
00192                 buf [3] = (u8) (table->language >> 8);
00193                 return 4;
00194         }
00195         for (s = table->strings; s && s->s; s++)
00196                 if (s->id == id)
00197                         break;
00198 
00199         /* unrecognized: stall. */
00200         if (!s || !s->s)
00201                 return -EINVAL;
00202 
00203         /* string descriptors have length, tag, then UTF16-LE text */
00204         len = min ((size_t) 126, strlen (s->s));
00205         memset (buf + 2, 0, 2 * len);   /* zero all the bytes */
00206         len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
00207         if (len < 0)
00208                 return -EINVAL;
00209         buf [0] = (len + 1) * 2;
00210         buf [1] = USB_DT_STRING;
00211         return buf [0];
00212 }
00213 
00214 
00215 /*-------------------------------------------------------------------------*/
00216 /*-------------------------------------------------------------------------*/
00217 
00218 
00231 int
00232 usb_descriptor_fillbuf(void *buf, unsigned buflen,
00233                 const struct usb_descriptor_header **src)
00234 {
00235         u8      *dest = buf;
00236 
00237         if (!src)
00238                 return -EINVAL;
00239 
00240         /* fill buffer from src[] until null descriptor ptr */
00241         for (; 0 != *src; src++) {
00242                 unsigned                len = (*src)->bLength;
00243 
00244                 if (len > buflen)
00245                         return -EINVAL;
00246                 memcpy(dest, *src, len);
00247                 buflen -= len;
00248                 dest += len;
00249         }
00250         return dest - (u8 *)buf;
00251 }
00252 
00253 
00274 int usb_gadget_config_buf(
00275         const struct usb_config_descriptor      *config,
00276         void                                    *buf,
00277         unsigned                                length,
00278         const struct usb_descriptor_header      **desc
00279 )
00280 {
00281         struct usb_config_descriptor            *cp = buf;
00282         int                                     len;
00283 
00284         /* config descriptor first */
00285         if (length < USB_DT_CONFIG_SIZE || !desc)
00286                 return -EINVAL;
00287         *cp = *config; 
00288 
00289         /* then interface/endpoint/class/vendor/... */
00290         len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
00291                         length - USB_DT_CONFIG_SIZE, desc);
00292         if (len < 0)
00293                 return len;
00294         len += USB_DT_CONFIG_SIZE;
00295         if (len > 0xffff)
00296                 return -EINVAL;
00297 
00298         /* patch up the config descriptor */
00299         cp->bLength = USB_DT_CONFIG_SIZE;
00300         cp->bDescriptorType = USB_DT_CONFIG;
00301         cp->wTotalLength = cpu_to_le16(len);
00302         cp->bmAttributes |= USB_CONFIG_ATT_ONE;
00303         return len;
00304 }
00305 
00306 /*-------------------------------------------------------------------------*/
00307 /*-------------------------------------------------------------------------*/
00308 
00309 
00310 #define RBUF_LEN (1024*1024)
00311 static int rbuf_start;
00312 static int rbuf_len;
00313 static __u8 rbuf[RBUF_LEN];
00314 
00315 /*-------------------------------------------------------------------------*/
00316 
00317 #define DRIVER_VERSION          "St Patrick's Day 2004"
00318 
00319 static const char shortname [] = "zero";
00320 static const char longname [] = "YAMAHA YST-MS35D USB Speaker  ";
00321 
00322 static const char source_sink [] = "source and sink data";
00323 static const char loopback [] = "loop input to output";
00324 
00325 /*-------------------------------------------------------------------------*/
00326 
00327 /*
00328  * driver assumes self-powered hardware, and
00329  * has no way for users to trigger remote wakeup.
00330  *
00331  * this version autoconfigures as much as possible,
00332  * which is reasonable for most "bulk-only" drivers.
00333  */
00334 static const char *EP_IN_NAME;          /* source */
00335 static const char *EP_OUT_NAME;         /* sink */
00336 
00337 /*-------------------------------------------------------------------------*/
00338 
00339 /* big enough to hold our biggest descriptor */
00340 #define USB_BUFSIZ      512
00341 
00342 struct zero_dev {
00343         spinlock_t              lock;
00344         struct usb_gadget       *gadget;
00345         struct usb_request      *req;           /* for control responses */
00346 
00347         /* when configured, we have one of two configs:
00348          * - source data (in to host) and sink it (out from host)
00349          * - or loop it back (out from host back in to host)
00350          */
00351         u8                      config;
00352         struct usb_ep           *in_ep, *out_ep;
00353 
00354         /* autoresume timer */
00355         struct timer_list       resume;
00356 };
00357 
00358 #define xprintk(d,level,fmt,args...) \
00359         dev_printk(level , &(d)->gadget->dev , fmt , ## args)
00360 
00361 #ifdef DEBUG
00362 #define DBG(dev,fmt,args...) \
00363         xprintk(dev , KERN_DEBUG , fmt , ## args)
00364 #else
00365 #define DBG(dev,fmt,args...) \
00366         do { } while (0)
00367 #endif /* DEBUG */
00368 
00369 #ifdef VERBOSE
00370 #define VDBG    DBG
00371 #else
00372 #define VDBG(dev,fmt,args...) \
00373         do { } while (0)
00374 #endif /* VERBOSE */
00375 
00376 #define ERROR(dev,fmt,args...) \
00377         xprintk(dev , KERN_ERR , fmt , ## args)
00378 #define WARN(dev,fmt,args...) \
00379         xprintk(dev , KERN_WARNING , fmt , ## args)
00380 #define INFO(dev,fmt,args...) \
00381         xprintk(dev , KERN_INFO , fmt , ## args)
00382 
00383 /*-------------------------------------------------------------------------*/
00384 
00385 static unsigned buflen = 4096;
00386 static unsigned qlen = 32;
00387 static unsigned pattern = 0;
00388 
00389 module_param (buflen, uint, S_IRUGO|S_IWUSR);
00390 module_param (qlen, uint, S_IRUGO|S_IWUSR);
00391 module_param (pattern, uint, S_IRUGO|S_IWUSR);
00392 
00393 /*
00394  * if it's nonzero, autoresume says how many seconds to wait
00395  * before trying to wake up the host after suspend.
00396  */
00397 static unsigned autoresume = 0;
00398 module_param (autoresume, uint, 0);
00399 
00400 /*
00401  * Normally the "loopback" configuration is second (index 1) so
00402  * it's not the default.  Here's where to change that order, to
00403  * work better with hosts where config changes are problematic.
00404  * Or controllers (like superh) that only support one config.
00405  */
00406 static int loopdefault = 0;
00407 
00408 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
00409 
00410 /*-------------------------------------------------------------------------*/
00411 
00412 /* Thanks to NetChip Technologies for donating this product ID.
00413  *
00414  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
00415  * Instead:  allocate your own, using normal USB-IF procedures.
00416  */
00417 #ifndef CONFIG_USB_ZERO_HNPTEST
00418 #define DRIVER_VENDOR_NUM       0x0525          /* NetChip */
00419 #define DRIVER_PRODUCT_NUM      0xa4a0          /* Linux-USB "Gadget Zero" */
00420 #else
00421 #define DRIVER_VENDOR_NUM       0x1a0a          /* OTG test device IDs */
00422 #define DRIVER_PRODUCT_NUM      0xbadd
00423 #endif
00424 
00425 /*-------------------------------------------------------------------------*/
00426 
00427 /*
00428  * DESCRIPTORS ... most are static, but strings and (full)
00429  * configuration descriptors are built on demand.
00430  */
00431 
00432 /*
00433 #define STRING_MANUFACTURER             25
00434 #define STRING_PRODUCT                  42
00435 #define STRING_SERIAL                   101
00436 */
00437 #define STRING_MANUFACTURER             1
00438 #define STRING_PRODUCT                  2
00439 #define STRING_SERIAL                   3
00440 
00441 #define STRING_SOURCE_SINK              250
00442 #define STRING_LOOPBACK                 251
00443 
00444 /*
00445  * This device advertises two configurations; these numbers work
00446  * on a pxa250 as well as more flexible hardware.
00447  */
00448 #define CONFIG_SOURCE_SINK      3
00449 #define CONFIG_LOOPBACK         2
00450 
00451 /*
00452 static struct usb_device_descriptor
00453 device_desc = {
00454         .bLength =              sizeof device_desc,
00455         .bDescriptorType =      USB_DT_DEVICE,
00456 
00457         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
00458         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
00459 
00460         .idVendor =             __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
00461         .idProduct =            __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
00462         .iManufacturer =        STRING_MANUFACTURER,
00463         .iProduct =             STRING_PRODUCT,
00464         .iSerialNumber =        STRING_SERIAL,
00465         .bNumConfigurations =   2,
00466 };
00467 */
00468 static struct usb_device_descriptor
00469 device_desc = {
00470         .bLength =              sizeof device_desc,
00471         .bDescriptorType =      USB_DT_DEVICE,
00472         .bcdUSB =               __constant_cpu_to_le16 (0x0100),
00473         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
00474         .bDeviceSubClass =      0,
00475         .bDeviceProtocol =      0,
00476         .bMaxPacketSize0 =      64,
00477         .bcdDevice =            __constant_cpu_to_le16 (0x0100),
00478         .idVendor =             __constant_cpu_to_le16 (0x0499),
00479         .idProduct =            __constant_cpu_to_le16 (0x3002),
00480         .iManufacturer =        STRING_MANUFACTURER,
00481         .iProduct =             STRING_PRODUCT,
00482         .iSerialNumber =        STRING_SERIAL,
00483         .bNumConfigurations =   1,
00484 };
00485 
00486 static struct usb_config_descriptor
00487 z_config = {
00488         .bLength =              sizeof z_config,
00489         .bDescriptorType =      USB_DT_CONFIG,
00490 
00491         /* compute wTotalLength on the fly */
00492         .bNumInterfaces =       2,
00493         .bConfigurationValue =  1,
00494         .iConfiguration =       0,
00495         .bmAttributes =         0x40,
00496         .bMaxPower =            0,      /* self-powered */
00497 };
00498 
00499 
00500 static struct usb_otg_descriptor
00501 otg_descriptor = {
00502         .bLength =              sizeof otg_descriptor,
00503         .bDescriptorType =      USB_DT_OTG,
00504 
00505         .bmAttributes =         USB_OTG_SRP,
00506 };
00507 
00508 /* one interface in each configuration */
00509 #ifdef  CONFIG_USB_GADGET_DUALSPEED
00510 
00511 /*
00512  * usb 2.0 devices need to expose both high speed and full speed
00513  * descriptors, unless they only run at full speed.
00514  *
00515  * that means alternate endpoint descriptors (bigger packets)
00516  * and a "device qualifier" ... plus more construction options
00517  * for the config descriptor.
00518  */
00519 
00520 static struct usb_qualifier_descriptor
00521 dev_qualifier = {
00522         .bLength =              sizeof dev_qualifier,
00523         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
00524 
00525         .bcdUSB =               __constant_cpu_to_le16 (0x0200),
00526         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
00527 
00528         .bNumConfigurations =   2,
00529 };
00530 
00531 
00532 struct usb_cs_as_general_descriptor {
00533         __u8  bLength;
00534         __u8  bDescriptorType;
00535 
00536         __u8  bDescriptorSubType;
00537         __u8  bTerminalLink;
00538         __u8  bDelay;
00539         __u16  wFormatTag;
00540 } __attribute__ ((packed));
00541 
00542 struct usb_cs_as_format_descriptor {
00543         __u8  bLength;
00544         __u8  bDescriptorType;
00545 
00546         __u8  bDescriptorSubType;
00547         __u8  bFormatType;
00548         __u8  bNrChannels;
00549         __u8  bSubframeSize;
00550         __u8  bBitResolution;
00551         __u8  bSamfreqType;
00552         __u8  tLowerSamFreq[3];
00553         __u8  tUpperSamFreq[3];
00554 } __attribute__ ((packed));
00555 
00556 static const struct usb_interface_descriptor
00557 z_audio_control_if_desc = {
00558         .bLength =              sizeof z_audio_control_if_desc,
00559         .bDescriptorType =      USB_DT_INTERFACE,
00560         .bInterfaceNumber = 0,
00561         .bAlternateSetting = 0,
00562         .bNumEndpoints = 0,
00563         .bInterfaceClass = USB_CLASS_AUDIO,
00564         .bInterfaceSubClass = 0x1,
00565         .bInterfaceProtocol = 0,
00566         .iInterface = 0,
00567 };
00568 
00569 static const struct usb_interface_descriptor
00570 z_audio_if_desc = {
00571         .bLength =              sizeof z_audio_if_desc,
00572         .bDescriptorType =      USB_DT_INTERFACE,
00573         .bInterfaceNumber = 1,
00574         .bAlternateSetting = 0,
00575         .bNumEndpoints = 0,
00576         .bInterfaceClass = USB_CLASS_AUDIO,
00577         .bInterfaceSubClass = 0x2,
00578         .bInterfaceProtocol = 0,
00579         .iInterface = 0,
00580 };
00581 
00582 static const struct usb_interface_descriptor
00583 z_audio_if_desc2 = {
00584         .bLength =              sizeof z_audio_if_desc,
00585         .bDescriptorType =      USB_DT_INTERFACE,
00586         .bInterfaceNumber = 1,
00587         .bAlternateSetting = 1,
00588         .bNumEndpoints = 1,
00589         .bInterfaceClass = USB_CLASS_AUDIO,
00590         .bInterfaceSubClass = 0x2,
00591         .bInterfaceProtocol = 0,
00592         .iInterface = 0,
00593 };
00594 
00595 static const struct usb_cs_as_general_descriptor
00596 z_audio_cs_as_if_desc = {
00597         .bLength = 7,
00598         .bDescriptorType = 0x24,
00599         
00600         .bDescriptorSubType = 0x01,
00601         .bTerminalLink = 0x01,
00602         .bDelay = 0x0,
00603         .wFormatTag = __constant_cpu_to_le16 (0x0001)
00604 };
00605 
00606 
00607 static const struct usb_cs_as_format_descriptor 
00608 z_audio_cs_as_format_desc = {
00609         .bLength = 0xe,
00610         .bDescriptorType = 0x24,
00611         
00612         .bDescriptorSubType = 2,
00613         .bFormatType = 1,
00614         .bNrChannels = 1,
00615         .bSubframeSize = 1,
00616         .bBitResolution = 8,
00617         .bSamfreqType = 0,
00618         .tLowerSamFreq = {0x7e, 0x13, 0x00},
00619         .tUpperSamFreq = {0xe2, 0xd6, 0x00},
00620 };
00621 
00622 static const struct usb_endpoint_descriptor 
00623 z_iso_ep = {
00624         .bLength = 0x09,
00625         .bDescriptorType = 0x05,
00626         .bEndpointAddress = 0x04,
00627         .bmAttributes = 0x09,
00628         .wMaxPacketSize = 0x0038,
00629         .bInterval = 0x01,
00630         .bRefresh = 0x00,
00631         .bSynchAddress = 0x00,  
00632 };
00633 
00634 static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
00635 
00636 // 9 bytes
00637 static char z_ac_interface_header_desc[] = 
00638 { 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
00639 
00640 // 12 bytes
00641 static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02, 
00642                      0x03, 0x00, 0x00, 0x00};
00643 // 13 bytes
00644 static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00, 
00645                      0x02, 0x00, 0x02, 0x00, 0x00};
00646 // 9 bytes
00647 static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02, 
00648                      0x00};
00649 
00650 static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00, 
00651                       0x00};
00652 
00653 static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
00654 
00655 static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00, 
00656                       0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
00657 
00658 static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
00659                       0x00};
00660 
00661 static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
00662 
00663 static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
00664                       0x00};
00665 
00666 static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
00667 
00668 static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
00669                       0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
00670 
00671 static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
00672                       0x00};
00673 
00674 static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
00675 
00676 static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
00677                        0x00};
00678 
00679 static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
00680 
00681 static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
00682                        0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
00683 
00684 static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
00685                        0x00};
00686 
00687 static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
00688 
00689 static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00, 
00690                        0x00};
00691 
00692 static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
00693 
00694 static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00, 
00695                        0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
00696 
00697 static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
00698                        0x00};
00699 
00700 static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
00701 
00702 static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
00703                        0x00};
00704 
00705 static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
00706 
00707 static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00, 
00708                        0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
00709 
00710 static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
00711                        0x00};
00712 
00713 static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
00714 
00715 
00716 
00717 static const struct usb_descriptor_header *z_function [] = {
00718         (struct usb_descriptor_header *) &z_audio_control_if_desc,
00719         (struct usb_descriptor_header *) &z_ac_interface_header_desc,
00720         (struct usb_descriptor_header *) &z_0,
00721         (struct usb_descriptor_header *) &z_1,
00722         (struct usb_descriptor_header *) &z_2,
00723         (struct usb_descriptor_header *) &z_audio_if_desc,
00724         (struct usb_descriptor_header *) &z_audio_if_desc2,
00725         (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
00726         (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
00727         (struct usb_descriptor_header *) &z_iso_ep,
00728         (struct usb_descriptor_header *) &z_iso_ep2,
00729         (struct usb_descriptor_header *) &za_0,
00730         (struct usb_descriptor_header *) &za_1,
00731         (struct usb_descriptor_header *) &za_2,
00732         (struct usb_descriptor_header *) &za_3,
00733         (struct usb_descriptor_header *) &za_4,
00734         (struct usb_descriptor_header *) &za_5,
00735         (struct usb_descriptor_header *) &za_6,
00736         (struct usb_descriptor_header *) &za_7,
00737         (struct usb_descriptor_header *) &za_8,
00738         (struct usb_descriptor_header *) &za_9,
00739         (struct usb_descriptor_header *) &za_10,
00740         (struct usb_descriptor_header *) &za_11,
00741         (struct usb_descriptor_header *) &za_12,
00742         (struct usb_descriptor_header *) &za_13,
00743         (struct usb_descriptor_header *) &za_14,
00744         (struct usb_descriptor_header *) &za_15,
00745         (struct usb_descriptor_header *) &za_16,
00746         (struct usb_descriptor_header *) &za_17,
00747         (struct usb_descriptor_header *) &za_18,
00748         (struct usb_descriptor_header *) &za_19,
00749         (struct usb_descriptor_header *) &za_20,
00750         (struct usb_descriptor_header *) &za_21,
00751         (struct usb_descriptor_header *) &za_22,
00752         (struct usb_descriptor_header *) &za_23,
00753         (struct usb_descriptor_header *) &za_24,
00754         NULL,
00755 };
00756 
00757 /* maxpacket and other transfer characteristics vary by speed. */
00758 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
00759 
00760 #else
00761 
00762 /* if there's no high speed support, maxpacket doesn't change. */
00763 #define ep_desc(g,hs,fs) fs
00764 
00765 #endif  /* !CONFIG_USB_GADGET_DUALSPEED */
00766 
00767 static char                             manufacturer [40];
00768 //static char                           serial [40];
00769 static char                             serial [] = "Ser 00 em";
00770 
00771 /* static strings, in UTF-8 */
00772 static struct usb_string                strings [] = {
00773         { STRING_MANUFACTURER, manufacturer, },
00774         { STRING_PRODUCT, longname, },
00775         { STRING_SERIAL, serial, },
00776         { STRING_LOOPBACK, loopback, },
00777         { STRING_SOURCE_SINK, source_sink, },
00778         {  }                    /* end of list */
00779 };
00780 
00781 static struct usb_gadget_strings        stringtab = {
00782         .language       = 0x0409,       /* en-us */
00783         .strings        = strings,
00784 };
00785 
00786 /*
00787  * config descriptors are also handcrafted.  these must agree with code
00788  * that sets configurations, and with code managing interfaces and their
00789  * altsettings.  other complexity may come from:
00790  *
00791  *  - high speed support, including "other speed config" rules
00792  *  - multiple configurations
00793  *  - interfaces with alternate settings
00794  *  - embedded class or vendor-specific descriptors
00795  *
00796  * this handles high speed, and has a second config that could as easily
00797  * have been an alternate interface setting (on most hardware).
00798  *
00799  * NOTE:  to demonstrate (and test) more USB capabilities, this driver
00800  * should include an altsetting to test interrupt transfers, including
00801  * high bandwidth modes at high speed.  (Maybe work like Intel's test
00802  * device?)
00803  */
00804 static int
00805 config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
00806 {
00807         int len;
00808         const struct usb_descriptor_header **function;
00809         
00810         function = z_function;
00811         len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
00812         if (len < 0)
00813                 return len;
00814         ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
00815         return len;
00816 }
00817 
00818 /*-------------------------------------------------------------------------*/
00819 
00820 static struct usb_request *
00821 alloc_ep_req (struct usb_ep *ep, unsigned length)
00822 {
00823         struct usb_request      *req;
00824 
00825         req = usb_ep_alloc_request (ep, GFP_ATOMIC);
00826         if (req) {
00827                 req->length = length;
00828                 req->buf = usb_ep_alloc_buffer (ep, length,
00829                                 &req->dma, GFP_ATOMIC);
00830                 if (!req->buf) {
00831                         usb_ep_free_request (ep, req);
00832                         req = NULL;
00833                 }
00834         }
00835         return req;
00836 }
00837 
00838 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
00839 {
00840         if (req->buf)
00841                 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
00842         usb_ep_free_request (ep, req);
00843 }
00844 
00845 /*-------------------------------------------------------------------------*/
00846 
00847 /* optionally require specific source/sink data patterns  */
00848 
00849 static int
00850 check_read_data (
00851         struct zero_dev         *dev,
00852         struct usb_ep           *ep,
00853         struct usb_request      *req
00854 )
00855 {
00856         unsigned        i;
00857         u8              *buf = req->buf;
00858 
00859         for (i = 0; i < req->actual; i++, buf++) {
00860                 switch (pattern) {
00861                 /* all-zeroes has no synchronization issues */
00862                 case 0:
00863                         if (*buf == 0)
00864                                 continue;
00865                         break;
00866                 /* mod63 stays in sync with short-terminated transfers,
00867                  * or otherwise when host and gadget agree on how large
00868                  * each usb transfer request should be.  resync is done
00869                  * with set_interface or set_config.
00870                  */
00871                 case 1:
00872                         if (*buf == (u8)(i % 63))
00873                                 continue;
00874                         break;
00875                 }
00876                 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
00877                 usb_ep_set_halt (ep);
00878                 return -EINVAL;
00879         }
00880         return 0;
00881 }
00882 
00883 /*-------------------------------------------------------------------------*/
00884 
00885 static void zero_reset_config (struct zero_dev *dev)
00886 {
00887         if (dev->config == 0)
00888                 return;
00889 
00890         DBG (dev, "reset config\n");
00891 
00892         /* just disable endpoints, forcing completion of pending i/o.
00893          * all our completion handlers free their requests in this case.
00894          */
00895         if (dev->in_ep) {
00896                 usb_ep_disable (dev->in_ep);
00897                 dev->in_ep = NULL;
00898         }
00899         if (dev->out_ep) {
00900                 usb_ep_disable (dev->out_ep);
00901                 dev->out_ep = NULL;
00902         }
00903         dev->config = 0;
00904         del_timer (&dev->resume);
00905 }
00906 
00907 #define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
00908 
00909 static void 
00910 zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
00911 {
00912         struct zero_dev *dev = ep->driver_data;
00913         int             status = req->status;
00914         int i, j;
00915 
00916         switch (status) {
00917 
00918         case 0:                         /* normal completion? */
00919                 //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
00920                 for (i=0, j=rbuf_start; i<req->actual; i++) {
00921                         //printk ("%02x ", ((__u8*)req->buf)[i]);
00922                         rbuf[j] = ((__u8*)req->buf)[i];
00923                         j++;
00924                         if (j >= RBUF_LEN) j=0;
00925                 }
00926                 rbuf_start = j;
00927                 //printk ("\n\n");
00928 
00929                 if (rbuf_len < RBUF_LEN) {
00930                         rbuf_len += req->actual;
00931                         if (rbuf_len > RBUF_LEN) {
00932                                 rbuf_len = RBUF_LEN;
00933                         }
00934                 }
00935 
00936                 break;
00937 
00938         /* this endpoint is normally active while we're configured */
00939         case -ECONNABORTED:             /* hardware forced ep reset */
00940         case -ECONNRESET:               /* request dequeued */
00941         case -ESHUTDOWN:                /* disconnect from host */
00942                 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
00943                                 req->actual, req->length);
00944                 if (ep == dev->out_ep)
00945                         check_read_data (dev, ep, req);
00946                 free_ep_req (ep, req);
00947                 return;
00948 
00949         case -EOVERFLOW:                /* buffer overrun on read means that
00950                                          * we didn't provide a big enough
00951                                          * buffer.
00952                                          */
00953         default:
00954 #if 1
00955                 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
00956                                 status, req->actual, req->length);
00957 #endif
00958         case -EREMOTEIO:                /* short read */
00959                 break;
00960         }
00961 
00962         status = usb_ep_queue (ep, req, GFP_ATOMIC);
00963         if (status) {
00964                 ERROR (dev, "kill %s:  resubmit %d bytes --> %d\n",
00965                                 ep->name, req->length, status);
00966                 usb_ep_set_halt (ep);
00967                 /* FIXME recover later ... somehow */
00968         }
00969 }
00970 
00971 static struct usb_request *
00972 zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
00973 {
00974         struct usb_request      *req;
00975         int                     status;
00976 
00977         req = alloc_ep_req (ep, 512);
00978         if (!req)
00979                 return NULL;
00980 
00981         req->complete = zero_isoc_complete;
00982 
00983         status = usb_ep_queue (ep, req, gfp_flags);
00984         if (status) {
00985                 struct zero_dev *dev = ep->driver_data;
00986 
00987                 ERROR (dev, "start %s --> %d\n", ep->name, status);
00988                 free_ep_req (ep, req);
00989                 req = NULL;
00990         }
00991 
00992         return req;
00993 }
00994 
00995 /* change our operational config.  this code must agree with the code
00996  * that returns config descriptors, and altsetting code.
00997  *
00998  * it's also responsible for power management interactions. some
00999  * configurations might not work with our current power sources.
01000  *
01001  * note that some device controller hardware will constrain what this
01002  * code can do, perhaps by disallowing more than one configuration or
01003  * by limiting configuration choices (like the pxa2xx).
01004  */
01005 static int
01006 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
01007 {
01008         int                     result = 0;
01009         struct usb_gadget       *gadget = dev->gadget;
01010         const struct usb_endpoint_descriptor    *d;
01011         struct usb_ep           *ep;
01012 
01013         if (number == dev->config)
01014                 return 0;
01015 
01016         zero_reset_config (dev);
01017 
01018         gadget_for_each_ep (ep, gadget) {
01019 
01020                 if (strcmp (ep->name, "ep4") == 0) {
01021 
01022                         d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
01023                         result = usb_ep_enable (ep, d);
01024 
01025                         if (result == 0) {
01026                                 ep->driver_data = dev;
01027                                 dev->in_ep = ep;
01028 
01029                                 if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
01030 
01031                                         dev->in_ep = ep;
01032                                         continue;
01033                                 }
01034 
01035                                 usb_ep_disable (ep);
01036                                 result = -EIO;
01037                         }
01038                 }
01039 
01040         }
01041 
01042         dev->config = number;
01043         return result;
01044 }
01045 
01046 /*-------------------------------------------------------------------------*/
01047 
01048 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
01049 {
01050         if (req->status || req->actual != req->length)
01051                 DBG ((struct zero_dev *) ep->driver_data,
01052                                 "setup complete --> %d, %d/%d\n",
01053                                 req->status, req->actual, req->length);
01054 }
01055 
01056 /*
01057  * The setup() callback implements all the ep0 functionality that's
01058  * not handled lower down, in hardware or the hardware driver (like
01059  * device and endpoint feature flags, and their status).  It's all
01060  * housekeeping for the gadget function we're implementing.  Most of
01061  * the work is in config-specific setup.
01062  */
01063 static int
01064 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
01065 {
01066         struct zero_dev         *dev = get_gadget_data (gadget);
01067         struct usb_request      *req = dev->req;
01068         int                     value = -EOPNOTSUPP;
01069 
01070         /* usually this stores reply data in the pre-allocated ep0 buffer,
01071          * but config change events will reconfigure hardware.
01072          */
01073         req->zero = 0;
01074         switch (ctrl->bRequest) {
01075 
01076         case USB_REQ_GET_DESCRIPTOR:
01077 
01078                 switch (ctrl->wValue >> 8) {
01079 
01080                 case USB_DT_DEVICE:
01081                         value = min (ctrl->wLength, (u16) sizeof device_desc);
01082                         memcpy (req->buf, &device_desc, value);
01083                         break;
01084 #ifdef CONFIG_USB_GADGET_DUALSPEED
01085                 case USB_DT_DEVICE_QUALIFIER:
01086                         if (!gadget->is_dualspeed)
01087                                 break;
01088                         value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
01089                         memcpy (req->buf, &dev_qualifier, value);
01090                         break;
01091 
01092                 case USB_DT_OTHER_SPEED_CONFIG:
01093                         if (!gadget->is_dualspeed)
01094                                 break;
01095                         // FALLTHROUGH
01096 #endif /* CONFIG_USB_GADGET_DUALSPEED */
01097                 case USB_DT_CONFIG:
01098                         value = config_buf (gadget, req->buf,
01099                                         ctrl->wValue >> 8,
01100                                         ctrl->wValue & 0xff);
01101                         if (value >= 0)
01102                                 value = min (ctrl->wLength, (u16) value);
01103                         break;
01104 
01105                 case USB_DT_STRING:
01106                         /* wIndex == language code.
01107                          * this driver only handles one language, you can
01108                          * add string tables for other languages, using
01109                          * any UTF-8 characters
01110                          */
01111                         value = usb_gadget_get_string (&stringtab,
01112                                         ctrl->wValue & 0xff, req->buf);
01113                         if (value >= 0) {
01114                                 value = min (ctrl->wLength, (u16) value);
01115                         }
01116                         break;
01117                 }
01118                 break;
01119 
01120         /* currently two configs, two speeds */
01121         case USB_REQ_SET_CONFIGURATION:
01122                 if (ctrl->bRequestType != 0)
01123                         goto unknown;
01124 
01125                 spin_lock (&dev->lock);
01126                 value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
01127                 spin_unlock (&dev->lock);
01128                 break;
01129         case USB_REQ_GET_CONFIGURATION:
01130                 if (ctrl->bRequestType != USB_DIR_IN)
01131                         goto unknown;
01132                 *(u8 *)req->buf = dev->config;
01133                 value = min (ctrl->wLength, (u16) 1);
01134                 break;
01135 
01136         /* until we add altsetting support, or other interfaces,
01137          * only 0/0 are possible.  pxa2xx only supports 0/0 (poorly)
01138          * and already killed pending endpoint I/O.
01139          */
01140         case USB_REQ_SET_INTERFACE:
01141 
01142                 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
01143                         goto unknown;
01144                 spin_lock (&dev->lock);
01145                 if (dev->config) {
01146                         u8              config = dev->config;
01147 
01148                         /* resets interface configuration, forgets about
01149                          * previous transaction state (queued bufs, etc)
01150                          * and re-inits endpoint state (toggle etc)
01151                          * no response queued, just zero status == success.
01152                          * if we had more than one interface we couldn't
01153                          * use this "reset the config" shortcut.
01154                          */
01155                         zero_reset_config (dev);
01156                         zero_set_config (dev, config, GFP_ATOMIC);
01157                         value = 0;
01158                 }
01159                 spin_unlock (&dev->lock);
01160                 break;
01161         case USB_REQ_GET_INTERFACE:
01162                 if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
01163                         value = ctrl->wLength;
01164                         break;
01165                 }
01166                 else {
01167                         if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
01168                                 goto unknown;
01169                         if (!dev->config)
01170                                 break;
01171                         if (ctrl->wIndex != 0) {
01172                                 value = -EDOM;
01173                                 break;
01174                         }
01175                         *(u8 *)req->buf = 0;
01176                         value = min (ctrl->wLength, (u16) 1);
01177                 }
01178                 break;
01179 
01180         /*
01181          * These are the same vendor-specific requests supported by
01182          * Intel's USB 2.0 compliance test devices.  We exceed that
01183          * device spec by allowing multiple-packet requests.
01184          */
01185         case 0x5b:      /* control WRITE test -- fill the buffer */
01186                 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
01187                         goto unknown;
01188                 if (ctrl->wValue || ctrl->wIndex)
01189                         break;
01190                 /* just read that many bytes into the buffer */
01191                 if (ctrl->wLength > USB_BUFSIZ)
01192                         break;
01193                 value = ctrl->wLength;
01194                 break;
01195         case 0x5c:      /* control READ test -- return the buffer */
01196                 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
01197                         goto unknown;
01198                 if (ctrl->wValue || ctrl->wIndex)
01199                         break;
01200                 /* expect those bytes are still in the buffer; send back */
01201                 if (ctrl->wLength > USB_BUFSIZ
01202                                 || ctrl->wLength != req->length)
01203                         break;
01204                 value = ctrl->wLength;
01205                 break;
01206 
01207         case 0x01: // SET_CUR
01208         case 0x02:
01209         case 0x03:
01210         case 0x04:
01211         case 0x05:
01212                 value = ctrl->wLength;
01213                 break;
01214         case 0x81:
01215                 switch (ctrl->wValue) {
01216                 case 0x0201:
01217                 case 0x0202:
01218                         ((u8*)req->buf)[0] = 0x00;
01219                         ((u8*)req->buf)[1] = 0xe3;
01220                         break;
01221                 case 0x0300:
01222                 case 0x0500:
01223                         ((u8*)req->buf)[0] = 0x00;
01224                         break;
01225                 }
01226                 //((u8*)req->buf)[0] = 0x81;
01227                 //((u8*)req->buf)[1] = 0x81;
01228                 value = ctrl->wLength;
01229                 break;
01230         case 0x82:
01231                 switch (ctrl->wValue) {
01232                 case 0x0201:
01233                 case 0x0202:
01234                         ((u8*)req->buf)[0] = 0x00;
01235                         ((u8*)req->buf)[1] = 0xc3;
01236                         break;
01237                 case 0x0300:
01238                 case 0x0500:
01239                         ((u8*)req->buf)[0] = 0x00;
01240                         break;
01241                 }
01242                 //((u8*)req->buf)[0] = 0x82;
01243                 //((u8*)req->buf)[1] = 0x82;
01244                 value = ctrl->wLength;
01245                 break;
01246         case 0x83:
01247                 switch (ctrl->wValue) {
01248                 case 0x0201:
01249                 case 0x0202:
01250                         ((u8*)req->buf)[0] = 0x00;
01251                         ((u8*)req->buf)[1] = 0x00;
01252                         break;
01253                 case 0x0300:
01254                         ((u8*)req->buf)[0] = 0x60;
01255                         break;
01256                 case 0x0500:    
01257                         ((u8*)req->buf)[0] = 0x18;
01258                         break;
01259                 }
01260                 //((u8*)req->buf)[0] = 0x83;
01261                 //((u8*)req->buf)[1] = 0x83;
01262                 value = ctrl->wLength;
01263                 break;
01264         case 0x84:
01265                 switch (ctrl->wValue) {
01266                 case 0x0201:
01267                 case 0x0202:
01268                         ((u8*)req->buf)[0] = 0x00;
01269                         ((u8*)req->buf)[1] = 0x01;
01270                         break;
01271                 case 0x0300:
01272                 case 0x0500:
01273                         ((u8*)req->buf)[0] = 0x08;
01274                         break;
01275                 }
01276                 //((u8*)req->buf)[0] = 0x84;
01277                 //((u8*)req->buf)[1] = 0x84;
01278                 value = ctrl->wLength;
01279                 break;
01280         case 0x85:
01281                 ((u8*)req->buf)[0] = 0x85;
01282                 ((u8*)req->buf)[1] = 0x85;
01283                 value = ctrl->wLength;
01284                 break;
01285 
01286         
01287         default:
01288 unknown:
01289                 printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
01290                         ctrl->bRequestType, ctrl->bRequest,
01291                         ctrl->wValue, ctrl->wIndex, ctrl->wLength);
01292         }
01293 
01294         /* respond with data transfer before status phase? */
01295         if (value >= 0) {
01296                 req->length = value;
01297                 req->zero = value < ctrl->wLength
01298                                 && (value % gadget->ep0->maxpacket) == 0;
01299                 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
01300                 if (value < 0) {
01301                         DBG (dev, "ep_queue < 0 --> %d\n", value);
01302                         req->status = 0;
01303                         zero_setup_complete (gadget->ep0, req);
01304                 }
01305         }
01306 
01307         /* device either stalls (value < 0) or reports success */
01308         return value;
01309 }
01310 
01311 static void
01312 zero_disconnect (struct usb_gadget *gadget)
01313 {
01314         struct zero_dev         *dev = get_gadget_data (gadget);
01315         unsigned long           flags;
01316 
01317         spin_lock_irqsave (&dev->lock, flags);
01318         zero_reset_config (dev);
01319 
01320         /* a more significant application might have some non-usb
01321          * activities to quiesce here, saving resources like power
01322          * or pushing the notification up a network stack.
01323          */
01324         spin_unlock_irqrestore (&dev->lock, flags);
01325 
01326         /* next we may get setup() calls to enumerate new connections;
01327          * or an unbind() during shutdown (including removing module).
01328          */
01329 }
01330 
01331 static void
01332 zero_autoresume (unsigned long _dev)
01333 {
01334         struct zero_dev *dev = (struct zero_dev *) _dev;
01335         int             status;
01336 
01337         /* normally the host would be woken up for something
01338          * more significant than just a timer firing...
01339          */
01340         if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
01341                 status = usb_gadget_wakeup (dev->gadget);
01342                 DBG (dev, "wakeup --> %d\n", status);
01343         }
01344 }
01345 
01346 /*-------------------------------------------------------------------------*/
01347 
01348 static void
01349 zero_unbind (struct usb_gadget *gadget)
01350 {
01351         struct zero_dev         *dev = get_gadget_data (gadget);
01352 
01353         DBG (dev, "unbind\n");
01354 
01355         /* we've already been disconnected ... no i/o is active */
01356         if (dev->req)
01357                 free_ep_req (gadget->ep0, dev->req);
01358         del_timer_sync (&dev->resume);
01359         kfree (dev);
01360         set_gadget_data (gadget, NULL);
01361 }
01362 
01363 static int
01364 zero_bind (struct usb_gadget *gadget)
01365 {
01366         struct zero_dev         *dev;
01367         //struct usb_ep         *ep;
01368 
01369         printk("binding\n");
01370         /*
01371          * DRIVER POLICY CHOICE:  you may want to do this differently.
01372          * One thing to avoid is reusing a bcdDevice revision code
01373          * with different host-visible configurations or behavior
01374          * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
01375          */
01376         //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
01377 
01378 
01379         /* ok, we made sense of the hardware ... */
01380         dev = kmalloc (sizeof *dev, SLAB_KERNEL);
01381         if (!dev)
01382                 return -ENOMEM;
01383         memset (dev, 0, sizeof *dev);
01384         spin_lock_init (&dev->lock);
01385         dev->gadget = gadget;
01386         set_gadget_data (gadget, dev);
01387 
01388         /* preallocate control response and buffer */
01389         dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
01390         if (!dev->req)
01391                 goto enomem;
01392         dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
01393                                 &dev->req->dma, GFP_KERNEL);
01394         if (!dev->req->buf)
01395                 goto enomem;
01396 
01397         dev->req->complete = zero_setup_complete;
01398 
01399         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
01400 
01401 #ifdef CONFIG_USB_GADGET_DUALSPEED
01402         /* assume ep0 uses the same value for both speeds ... */
01403         dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
01404 
01405         /* and that all endpoints are dual-speed */
01406         //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
01407         //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
01408 #endif
01409 
01410         usb_gadget_set_selfpowered (gadget);
01411 
01412         init_timer (&dev->resume);
01413         dev->resume.function = zero_autoresume;
01414         dev->resume.data = (unsigned long) dev;
01415 
01416         gadget->ep0->driver_data = dev;
01417 
01418         INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
01419         INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
01420                 EP_OUT_NAME, EP_IN_NAME);
01421 
01422         snprintf (manufacturer, sizeof manufacturer,
01423                 UTS_SYSNAME " " UTS_RELEASE " with %s",
01424                 gadget->name);
01425 
01426         return 0;
01427 
01428 enomem:
01429         zero_unbind (gadget);
01430         return -ENOMEM;
01431 }
01432 
01433 /*-------------------------------------------------------------------------*/
01434 
01435 static void
01436 zero_suspend (struct usb_gadget *gadget)
01437 {
01438         struct zero_dev         *dev = get_gadget_data (gadget);
01439 
01440         if (gadget->speed == USB_SPEED_UNKNOWN)
01441                 return;
01442 
01443         if (autoresume) {
01444                 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
01445                 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
01446         } else
01447                 DBG (dev, "suspend\n");
01448 }
01449 
01450 static void
01451 zero_resume (struct usb_gadget *gadget)
01452 {
01453         struct zero_dev         *dev = get_gadget_data (gadget);
01454 
01455         DBG (dev, "resume\n");
01456         del_timer (&dev->resume);
01457 }
01458 
01459 
01460 /*-------------------------------------------------------------------------*/
01461 
01462 static struct usb_gadget_driver zero_driver = {
01463 #ifdef CONFIG_USB_GADGET_DUALSPEED
01464         .speed          = USB_SPEED_HIGH,
01465 #else
01466         .speed          = USB_SPEED_FULL,
01467 #endif
01468         .function       = (char *) longname,
01469         .bind           = zero_bind,
01470         .unbind         = zero_unbind,
01471 
01472         .setup          = zero_setup,
01473         .disconnect     = zero_disconnect,
01474 
01475         .suspend        = zero_suspend,
01476         .resume         = zero_resume,
01477 
01478         .driver         = {
01479                 .name           = (char *) shortname,
01480                 // .shutdown = ...
01481                 // .suspend = ...
01482                 // .resume = ...
01483         },
01484 };
01485 
01486 MODULE_AUTHOR ("David Brownell");
01487 MODULE_LICENSE ("Dual BSD/GPL");
01488 
01489 static struct proc_dir_entry *pdir, *pfile;
01490 
01491 static int isoc_read_data (char *page, char **start,
01492                            off_t off, int count,
01493                            int *eof, void *data)
01494 {
01495         int i;
01496         static int c = 0;
01497         static int done = 0;
01498         static int s = 0;
01499 
01500 /*
01501         printk ("\ncount: %d\n", count);
01502         printk ("rbuf_start: %d\n", rbuf_start);
01503         printk ("rbuf_len: %d\n", rbuf_len);
01504         printk ("off: %d\n", off);
01505         printk ("start: %p\n\n", *start);
01506 */
01507         if (done) {
01508                 c = 0;
01509                 done = 0;
01510                 *eof = 1;
01511                 return 0;
01512         }
01513 
01514         if (c == 0) {
01515                 if (rbuf_len == RBUF_LEN)
01516                         s = rbuf_start;
01517                 else s = 0;
01518         }
01519 
01520         for (i=0; i<count && c<rbuf_len; i++, c++) {
01521                 page[i] = rbuf[(c+s) % RBUF_LEN];
01522         }
01523         *start = page;
01524         
01525         if (c >= rbuf_len) {
01526                 *eof = 1;
01527                 done = 1;
01528         }
01529 
01530 
01531         return i;
01532 }
01533 
01534 static int __init init (void)
01535 {
01536 
01537         int retval = 0;
01538 
01539         pdir = proc_mkdir("isoc_test", NULL);
01540         if(pdir == NULL) {
01541                 retval = -ENOMEM;
01542                 printk("Error creating dir\n");
01543                 goto done;
01544         }
01545         pdir->owner = THIS_MODULE;
01546 
01547         pfile = create_proc_read_entry("isoc_data",
01548                                        0444, pdir,
01549                                        isoc_read_data,
01550                                        NULL);
01551         if (pfile == NULL) {
01552                 retval = -ENOMEM;
01553                 printk("Error creating file\n");
01554                 goto no_file;
01555         }
01556         pfile->owner = THIS_MODULE;
01557 
01558         return usb_gadget_register_driver (&zero_driver);
01559 
01560  no_file:
01561         remove_proc_entry("isoc_data", NULL);
01562  done:
01563         return retval;
01564 }
01565 module_init (init);
01566 
01567 static void __exit cleanup (void)
01568 {
01569 
01570         usb_gadget_unregister_driver (&zero_driver);
01571         
01572         remove_proc_entry("isoc_data", pdir);
01573         remove_proc_entry("isoc_test", NULL);
01574 }
01575 module_exit (cleanup);

Generated on Tue May 5 02:22:48 2009 for DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver by  doxygen 1.4.7