00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
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
00107
00108
00109
00110 while (len != 0 && (c = (u8) *s++) != 0) {
00111 if (unlikely(c & 0x80)) {
00112
00113
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
00124
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
00141 if (0xd800 <= uchar && uchar <= 0xdfff)
00142 goto fail;
00143
00144
00145
00146
00147
00148
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
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
00200 if (!s || !s->s)
00201 return -EINVAL;
00202
00203
00204 len = min ((size_t) 126, strlen (s->s));
00205 memset (buf + 2, 0, 2 * len);
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
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
00285 if (length < USB_DT_CONFIG_SIZE || !desc)
00286 return -EINVAL;
00287 *cp = *config;
00288
00289
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
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
00329
00330
00331
00332
00333
00334 static const char *EP_IN_NAME;
00335 static const char *EP_OUT_NAME;
00336
00337
00338
00339
00340 #define USB_BUFSIZ 512
00341
00342 struct zero_dev {
00343 spinlock_t lock;
00344 struct usb_gadget *gadget;
00345 struct usb_request *req;
00346
00347
00348
00349
00350
00351 u8 config;
00352 struct usb_ep *in_ep, *out_ep;
00353
00354
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
00368
00369 #ifdef VERBOSE
00370 #define VDBG DBG
00371 #else
00372 #define VDBG(dev,fmt,args...) \
00373 do { } while (0)
00374 #endif
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
00395
00396
00397 static unsigned autoresume = 0;
00398 module_param (autoresume, uint, 0);
00399
00400
00401
00402
00403
00404
00405
00406 static int loopdefault = 0;
00407
00408 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
00409
00410
00411
00412
00413
00414
00415
00416
00417 #ifndef CONFIG_USB_ZERO_HNPTEST
00418 #define DRIVER_VENDOR_NUM 0x0525
00419 #define DRIVER_PRODUCT_NUM 0xa4a0
00420 #else
00421 #define DRIVER_VENDOR_NUM 0x1a0a
00422 #define DRIVER_PRODUCT_NUM 0xbadd
00423 #endif
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
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
00446
00447
00448 #define CONFIG_SOURCE_SINK 3
00449 #define CONFIG_LOOPBACK 2
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
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
00492 .bNumInterfaces = 2,
00493 .bConfigurationValue = 1,
00494 .iConfiguration = 0,
00495 .bmAttributes = 0x40,
00496 .bMaxPower = 0,
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
00509 #ifdef CONFIG_USB_GADGET_DUALSPEED
00510
00511
00512
00513
00514
00515
00516
00517
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
00637 static char z_ac_interface_header_desc[] =
00638 { 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
00639
00640
00641 static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
00642 0x03, 0x00, 0x00, 0x00};
00643
00644 static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
00645 0x02, 0x00, 0x02, 0x00, 0x00};
00646
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
00758 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
00759
00760 #else
00761
00762
00763 #define ep_desc(g,hs,fs) fs
00764
00765 #endif
00766
00767 static char manufacturer [40];
00768
00769 static char serial [] = "Ser 00 em";
00770
00771
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 { }
00779 };
00780
00781 static struct usb_gadget_strings stringtab = {
00782 .language = 0x0409,
00783 .strings = strings,
00784 };
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
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
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
00862 case 0:
00863 if (*buf == 0)
00864 continue;
00865 break;
00866
00867
00868
00869
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
00893
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:
00919
00920 for (i=0, j=rbuf_start; i<req->actual; i++) {
00921
00922 rbuf[j] = ((__u8*)req->buf)[i];
00923 j++;
00924 if (j >= RBUF_LEN) j=0;
00925 }
00926 rbuf_start = j;
00927
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
00939 case -ECONNABORTED:
00940 case -ECONNRESET:
00941 case -ESHUTDOWN:
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:
00950
00951
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:
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
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
00996
00997
00998
00999
01000
01001
01002
01003
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;
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
01058
01059
01060
01061
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
01071
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
01096 #endif
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
01107
01108
01109
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
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
01137
01138
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
01149
01150
01151
01152
01153
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
01182
01183
01184
01185 case 0x5b:
01186 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
01187 goto unknown;
01188 if (ctrl->wValue || ctrl->wIndex)
01189 break;
01190
01191 if (ctrl->wLength > USB_BUFSIZ)
01192 break;
01193 value = ctrl->wLength;
01194 break;
01195 case 0x5c:
01196 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
01197 goto unknown;
01198 if (ctrl->wValue || ctrl->wIndex)
01199 break;
01200
01201 if (ctrl->wLength > USB_BUFSIZ
01202 || ctrl->wLength != req->length)
01203 break;
01204 value = ctrl->wLength;
01205 break;
01206
01207 case 0x01:
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
01227
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
01243
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
01261
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
01277
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
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
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
01321
01322
01323
01324 spin_unlock_irqrestore (&dev->lock, flags);
01325
01326
01327
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
01338
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
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
01368
01369 printk("binding\n");
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
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
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
01403 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
01404
01405
01406
01407
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
01481
01482
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
01502
01503
01504
01505
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);