OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [gadget/] [zero.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * zero.c -- Gadget Zero, for USB development
3
 *
4
 * Copyright (C) 2003-2004 David Brownell
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions, and the following disclaimer,
12
 *    without modification.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The names of the above-listed copyright holders may not be used
17
 *    to endorse or promote products derived from this software without
18
 *    specific prior written permission.
19
 *
20
 * ALTERNATIVELY, this software may be distributed under the terms of the
21
 * GNU General Public License ("GPL") as published by the Free Software
22
 * Foundation, either version 2 of that License or (at your option) any
23
 * later version.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 */
37
 
38
 
39
/*
40
 * Gadget Zero only needs two bulk endpoints, and is an example of how you
41
 * can write a hardware-agnostic gadget driver running inside a USB device.
42
 *
43
 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44
 * affect most of the driver.
45
 *
46
 * Use it with the Linux host/master side "usbtest" driver to get a basic
47
 * functional test of your device-side usb stack, or with "usb-skeleton".
48
 *
49
 * It supports two similar configurations.  One sinks whatever the usb host
50
 * writes, and in return sources zeroes.  The other loops whatever the host
51
 * writes back, so the host can read it.  Module options include:
52
 *
53
 *   buflen=N           default N=4096, buffer size used
54
 *   qlen=N             default N=32, how many buffers in the loopback queue
55
 *   loopdefault        default false, list loopback config first
56
 *
57
 * Many drivers will only have one configuration, letting them be much
58
 * simpler if they also don't support high speed operation (like this
59
 * driver does).
60
 */
61
 
62
#define DEBUG 1
63
// #define VERBOSE
64
 
65
#include <linux/config.h>
66
#include <linux/kernel.h>
67
#include <linux/module.h>
68
#include <linux/delay.h>
69
#include <linux/ioport.h>
70
#include <linux/sched.h>
71
#include <linux/slab.h>
72
#include <linux/smp_lock.h>
73
#include <linux/errno.h>
74
#include <linux/init.h>
75
#include <linux/timer.h>
76
#include <linux/list.h>
77
#include <linux/interrupt.h>
78
#include <linux/uts.h>
79
#include <linux/version.h>
80
 
81
#include <asm/byteorder.h>
82
#include <asm/io.h>
83
#include <asm/irq.h>
84
#include <asm/system.h>
85
#include <asm/unaligned.h>
86
 
87
#include <linux/usb_ch9.h>
88
#include <linux/usb_gadget.h>
89
 
90
 
91
/*-------------------------------------------------------------------------*/
92
 
93
#define DRIVER_VERSION          "Bastille Day 2003"
94
 
95
static const char shortname [] = "zero";
96
static const char longname [] = "Gadget Zero";
97
 
98
static const char source_sink [] = "source and sink data";
99
static const char loopback [] = "loop input to output";
100
 
101
/*-------------------------------------------------------------------------*/
102
 
103
/*
104
 * driver assumes self-powered hardware, and
105
 * has no way for users to trigger remote wakeup.
106
 */
107
 
108
/*
109
 * hardware-specific configuration, controlled by which device
110
 * controller driver was configured.
111
 *
112
 * CHIP ... hardware identifier
113
 * DRIVER_VERSION_NUM ... alerts the host side driver to differences
114
 * EP_*_NAME ... which endpoints do we use for which purpose?
115
 * EP_*_NUM ... numbers for them (often limited by hardware)
116
 *
117
 * add other defines for other portability issues, like hardware that
118
 * for some reason doesn't handle full speed bulk maxpacket of 64.
119
 */
120
 
121
/*
122
 * DRIVER_VERSION_NUM 0x0000 (?):  Martin Diehl's ezusb an21/fx code
123
 */
124
 
125
/*
126
 * NetChip 2280, PCI based.
127
 *
128
 * This has half a dozen configurable endpoints, four with dedicated
129
 * DMA channels to manage their FIFOs.  It supports high speed.
130
 * Those endpoints can be arranged in any desired configuration.
131
 */
132
#ifdef  CONFIG_USB_GADGET_NET2280
133
#define CHIP                    "net2280"
134
#define DRIVER_VERSION_NUM      0x0111
135
static const char EP_OUT_NAME [] = "ep-a";
136
#define EP_OUT_NUM      2
137
static const char EP_IN_NAME [] = "ep-b";
138
#define EP_IN_NUM       2
139
#endif
140
 
141
/*
142
 * PXA-2xx UDC:  widely used in second gen Linux-capable PDAs.
143
 *
144
 * This has fifteen fixed-function full speed endpoints, and it
145
 * can support all USB transfer types.
146
 *
147
 * These supports three or four configurations, with fixed numbers.
148
 * The hardware interprets SET_INTERFACE, net effect is that you
149
 * can't use altsettings or reset the interfaces independently.
150
 * So stick to a single interface.
151
 */
152
#ifdef  CONFIG_USB_GADGET_PXA2XX
153
#define CHIP                    "pxa2xx"
154
#define DRIVER_VERSION_NUM      0x0113
155
static const char EP_OUT_NAME [] = "ep12out-bulk";
156
#define EP_OUT_NUM      12
157
static const char EP_IN_NAME [] = "ep11in-bulk";
158
#define EP_IN_NUM       11
159
#endif
160
 
161
/*
162
 * SA-1100 UDC:  widely used in first gen Linux-capable PDAs.
163
 *
164
 * This has only two fixed function endpoints, which can only
165
 * be used for bulk (or interrupt) transfers.  (Plus control.)
166
 *
167
 * Since it can't flush its TX fifos without disabling the UDC,
168
 * the current configuration or altsettings can't change except
169
 * in special situations.  So this is a case of "choose it right
170
 * during enumeration" ...
171
 */
172
#ifdef  CONFIG_USB_GADGET_SA1100
173
#define CHIP                    "sa1100"
174
#define DRIVER_VERSION_NUM      0x0115
175
static const char EP_OUT_NAME [] = "ep1out-bulk";
176
#define EP_OUT_NUM      1
177
static const char EP_IN_NAME [] = "ep2in-bulk";
178
#define EP_IN_NUM       2
179
#endif
180
 
181
/*
182
 * Toshiba TC86C001 ("Goku-S") UDC
183
 *
184
 * This has three semi-configurable full speed bulk/interrupt endpoints.
185
 */
186
#ifdef  CONFIG_USB_GADGET_GOKU
187
#define CHIP                    "goku"
188
#define DRIVER_VERSION_NUM      0x0116
189
static const char EP_OUT_NAME [] = "ep1-bulk";
190
#define EP_OUT_NUM      1
191
static const char EP_IN_NAME [] = "ep2-bulk";
192
#define EP_IN_NUM       2
193
#endif
194
 
195
/*-------------------------------------------------------------------------*/
196
 
197
#ifndef EP_OUT_NUM
198
#       error Configure some USB peripheral controller driver!
199
#endif
200
 
201
/*-------------------------------------------------------------------------*/
202
 
203
/* big enough to hold our biggest descriptor */
204
#define USB_BUFSIZ      256
205
 
206
struct zero_dev {
207
        spinlock_t              lock;
208
        struct usb_gadget       *gadget;
209
        struct usb_request      *req;           /* for control responses */
210
 
211
        /* when configured, we have one of two configs:
212
         * - source data (in to host) and sink it (out from host)
213
         * - or loop it back (out from host back in to host)
214
         */
215
        u8                      config;
216
        struct usb_ep           *in_ep, *out_ep;
217
};
218
 
219
#define xprintk(d,level,fmt,args...) \
220
        printk(level "%s %s: " fmt , shortname , (d)->gadget->dev.bus_id , \
221
                ## args)
222
 
223
#ifdef DEBUG
224
#undef DEBUG
225
#define DEBUG(dev,fmt,args...) \
226
        xprintk(dev , KERN_DEBUG , fmt , ## args)
227
#else
228
#define DEBUG(dev,fmt,args...) \
229
        do { } while (0)
230
#endif /* DEBUG */
231
 
232
#ifdef VERBOSE
233
#define VDEBUG  DEBUG
234
#else
235
#define VDEBUG(dev,fmt,args...) \
236
        do { } while (0)
237
#endif /* DEBUG */
238
 
239
#define ERROR(dev,fmt,args...) \
240
        xprintk(dev , KERN_ERR , fmt , ## args)
241
#define WARN(dev,fmt,args...) \
242
        xprintk(dev , KERN_WARNING , fmt , ## args)
243
#define INFO(dev,fmt,args...) \
244
        xprintk(dev , KERN_INFO , fmt , ## args)
245
 
246
/*-------------------------------------------------------------------------*/
247
 
248
static unsigned buflen = 4096;
249
static unsigned qlen = 32;
250
static unsigned pattern = 0;
251
 
252
/*
253
 * Normally the "loopback" configuration is second (index 1) so
254
 * it's not the default.  Here's where to change that order, to
255
 * work better with hosts where config changes are problematic.
256
 * Or controllers (like superh) that only support one config.
257
 */
258
static int loopdefault = 0;
259
 
260
 
261
MODULE_PARM (buflen, "i");
262
MODULE_PARM_DESC (buflen, "size of i/o buffers");
263
 
264
MODULE_PARM (qlen, "i");
265
MODULE_PARM_DESC (qlen, "depth of loopback buffering");
266
 
267
MODULE_PARM (pattern, "i");
268
MODULE_PARM_DESC (pattern, "0 for default all-zeroes, 1 for mod63");
269
 
270
MODULE_PARM (loopdefault, "b");
271
MODULE_PARM_DESC (loopdefault, "true to have default config be loopback");
272
 
273
/*-------------------------------------------------------------------------*/
274
 
275
/* Thanks to NetChip Technologies for donating this product ID.
276
 *
277
 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
278
 * Instead:  allocate your own, using normal USB-IF procedures.
279
 */
280
#define DRIVER_VENDOR_NUM       0x0525          /* NetChip */
281
#define DRIVER_PRODUCT_NUM      0xa4a0          /* Linux-USB "Gadget Zero" */
282
 
283
/*-------------------------------------------------------------------------*/
284
 
285
/*
286
 * DESCRIPTORS ... most are static, but strings and (full)
287
 * configuration descriptors are built on demand.
288
 */
289
 
290
#define STRING_MANUFACTURER             25
291
#define STRING_PRODUCT                  42
292
#define STRING_SERIAL                   101
293
#define STRING_SOURCE_SINK              250
294
#define STRING_LOOPBACK                 251
295
 
296
/*
297
 * This device advertises two configurations; these numbers work
298
 * on a pxa250 as well as more flexible hardware.
299
 */
300
#define CONFIG_SOURCE_SINK      3
301
#define CONFIG_LOOPBACK         2
302
 
303
static struct usb_device_descriptor
304
device_desc = {
305
        .bLength =              sizeof device_desc,
306
        .bDescriptorType =      USB_DT_DEVICE,
307
 
308
        .bcdUSB =               __constant_cpu_to_le16 (0x0200),
309
        .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
310
 
311
        .idVendor =             __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
312
        .idProduct =            __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
313
        .bcdDevice =            __constant_cpu_to_le16 (DRIVER_VERSION_NUM),
314
        .iManufacturer =        STRING_MANUFACTURER,
315
        .iProduct =             STRING_PRODUCT,
316
        .iSerialNumber =        STRING_SERIAL,
317
        .bNumConfigurations =   2,
318
};
319
 
320
static const struct usb_config_descriptor
321
source_sink_config = {
322
        .bLength =              sizeof source_sink_config,
323
        .bDescriptorType =      USB_DT_CONFIG,
324
 
325
        /* compute wTotalLength on the fly */
326
        .bNumInterfaces =       1,
327
        .bConfigurationValue =  CONFIG_SOURCE_SINK,
328
        .iConfiguration =       STRING_SOURCE_SINK,
329
        .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
330
        .bMaxPower =            1,      /* self-powered */
331
};
332
 
333
static const struct usb_config_descriptor
334
loopback_config = {
335
        .bLength =              sizeof loopback_config,
336
        .bDescriptorType =      USB_DT_CONFIG,
337
 
338
        /* compute wTotalLength on the fly */
339
        .bNumInterfaces =       1,
340
        .bConfigurationValue =  CONFIG_LOOPBACK,
341
        .iConfiguration =       STRING_LOOPBACK,
342
        .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
343
        .bMaxPower =            1,      /* self-powered */
344
};
345
 
346
/* one interface in each configuration */
347
 
348
static const struct usb_interface_descriptor
349
source_sink_intf = {
350
        .bLength =              sizeof source_sink_intf,
351
        .bDescriptorType =      USB_DT_INTERFACE,
352
 
353
        .bNumEndpoints =        2,
354
        .bInterfaceClass =      USB_CLASS_VENDOR_SPEC,
355
        .iInterface =           STRING_SOURCE_SINK,
356
};
357
 
358
static const struct usb_interface_descriptor
359
loopback_intf = {
360
        .bLength =              sizeof loopback_intf,
361
        .bDescriptorType =      USB_DT_INTERFACE,
362
 
363
        .bNumEndpoints =        2,
364
        .bInterfaceClass =      USB_CLASS_VENDOR_SPEC,
365
        .iInterface =           STRING_LOOPBACK,
366
};
367
 
368
/* two full speed bulk endpoints; their use is config-dependent */
369
 
370
static const struct usb_endpoint_descriptor
371
fs_source_desc = {
372
        .bLength =              USB_DT_ENDPOINT_SIZE,
373
        .bDescriptorType =      USB_DT_ENDPOINT,
374
 
375
        .bEndpointAddress =     EP_IN_NUM | USB_DIR_IN,
376
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
377
        .wMaxPacketSize =       __constant_cpu_to_le16 (64),
378
};
379
 
380
static const struct usb_endpoint_descriptor
381
fs_sink_desc = {
382
        .bLength =              USB_DT_ENDPOINT_SIZE,
383
        .bDescriptorType =      USB_DT_ENDPOINT,
384
 
385
        .bEndpointAddress =     EP_OUT_NUM,
386
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
387
        .wMaxPacketSize =       __constant_cpu_to_le16 (64),
388
};
389
 
390
static const struct usb_descriptor_header *fs_source_sink_function [] = {
391
        (struct usb_descriptor_header *) &source_sink_intf,
392
        (struct usb_descriptor_header *) &fs_sink_desc,
393
        (struct usb_descriptor_header *) &fs_source_desc,
394
        0,
395
};
396
 
397
static const struct usb_descriptor_header *fs_loopback_function [] = {
398
        (struct usb_descriptor_header *) &loopback_intf,
399
        (struct usb_descriptor_header *) &fs_sink_desc,
400
        (struct usb_descriptor_header *) &fs_source_desc,
401
        0,
402
};
403
 
404
#ifdef  CONFIG_USB_GADGET_DUALSPEED
405
 
406
/*
407
 * usb 2.0 devices need to expose both high speed and full speed
408
 * descriptors, unless they only run at full speed.
409
 *
410
 * that means alternate endpoint descriptors (bigger packets)
411
 * and a "device qualifier" ... plus more construction options
412
 * for the config descriptor.
413
 */
414
 
415
static struct usb_endpoint_descriptor
416
hs_source_desc = {
417
        .bLength =              USB_DT_ENDPOINT_SIZE,
418
        .bDescriptorType =      USB_DT_ENDPOINT,
419
 
420
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
421
        .wMaxPacketSize =       __constant_cpu_to_le16 (512),
422
};
423
 
424
static struct usb_endpoint_descriptor
425
hs_sink_desc = {
426
        .bLength =              USB_DT_ENDPOINT_SIZE,
427
        .bDescriptorType =      USB_DT_ENDPOINT,
428
 
429
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
430
        .wMaxPacketSize =       __constant_cpu_to_le16 (512),
431
};
432
 
433
static struct usb_qualifier_descriptor
434
dev_qualifier = {
435
        .bLength =              sizeof dev_qualifier,
436
        .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
437
 
438
        .bcdUSB =               __constant_cpu_to_le16 (0x0200),
439
        .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
440
 
441
        .bNumConfigurations =   2,
442
};
443
 
444
static const struct usb_descriptor_header *hs_source_sink_function [] = {
445
        (struct usb_descriptor_header *) &source_sink_intf,
446
        (struct usb_descriptor_header *) &hs_source_desc,
447
        (struct usb_descriptor_header *) &hs_sink_desc,
448
        0,
449
};
450
 
451
static const struct usb_descriptor_header *hs_loopback_function [] = {
452
        (struct usb_descriptor_header *) &loopback_intf,
453
        (struct usb_descriptor_header *) &hs_source_desc,
454
        (struct usb_descriptor_header *) &hs_sink_desc,
455
        0,
456
};
457
 
458
/* maxpacket and other transfer characteristics vary by speed. */
459
#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
460
 
461
#else
462
 
463
/* if there's no high speed support, maxpacket doesn't change. */
464
#define ep_desc(g,hs,fs) fs
465
 
466
#endif  /* !CONFIG_USB_GADGET_DUALSPEED */
467
 
468
static char                             manufacturer [40];
469
static char                             serial [40];
470
 
471
/* static strings, in iso 8859/1 */
472
static struct usb_string                strings [] = {
473
        { STRING_MANUFACTURER, manufacturer, },
474
        { STRING_PRODUCT, longname, },
475
        { STRING_SERIAL, serial, },
476
        { STRING_LOOPBACK, loopback, },
477
        { STRING_SOURCE_SINK, source_sink, },
478
        {  }                    /* end of list */
479
};
480
 
481
static struct usb_gadget_strings        stringtab = {
482
        .language       = 0x0409,       /* en-us */
483
        .strings        = strings,
484
};
485
 
486
/*
487
 * config descriptors are also handcrafted.  these must agree with code
488
 * that sets configurations, and with code managing interfaces and their
489
 * altsettings.  other complexity may come from:
490
 *
491
 *  - high speed support, including "other speed config" rules
492
 *  - multiple configurations
493
 *  - interfaces with alternate settings
494
 *  - embedded class or vendor-specific descriptors
495
 *
496
 * this handles high speed, and has a second config that could as easily
497
 * have been an alternate interface setting (on most hardware).
498
 *
499
 * NOTE:  to demonstrate (and test) more USB capabilities, this driver
500
 * should include an altsetting to test interrupt transfers, including
501
 * high bandwidth modes at high speed.  (Maybe work like Intel's test
502
 * device?)
503
 */
504
static int
505
config_buf (struct usb_gadget *gadget,
506
                u8 *buf, u8 type, unsigned index)
507
{
508
        int                             is_source_sink;
509
        int                             len;
510
        const struct usb_descriptor_header **function;
511
#ifdef CONFIG_USB_GADGET_DUALSPEED
512
        int                             hs = (gadget->speed == USB_SPEED_HIGH);
513
#endif
514
 
515
        /* two configurations will always be index 0 and index 1 */
516
        if (index > 1)
517
                return -EINVAL;
518
        is_source_sink = loopdefault ? (index == 1) : (index == 0);
519
 
520
#ifdef CONFIG_USB_GADGET_DUALSPEED
521
        if (type == USB_DT_OTHER_SPEED_CONFIG)
522
                hs = !hs;
523
        if (hs)
524
                function = is_source_sink
525
                        ? hs_source_sink_function
526
                        : hs_loopback_function;
527
        else
528
#endif
529
                function = is_source_sink
530
                        ? fs_source_sink_function
531
                        : fs_loopback_function;
532
 
533
        len = usb_gadget_config_buf (is_source_sink
534
                                        ? &source_sink_config
535
                                        : &loopback_config,
536
                        buf, USB_BUFSIZ, function);
537
        if (len < 0)
538
                return len;
539
        ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
540
        return len;
541
}
542
 
543
/*-------------------------------------------------------------------------*/
544
 
545
static struct usb_request *
546
alloc_ep_req (struct usb_ep *ep, unsigned length)
547
{
548
        struct usb_request      *req;
549
 
550
        req = usb_ep_alloc_request (ep, GFP_ATOMIC);
551
        if (req) {
552
                req->length = length;
553
                req->buf = usb_ep_alloc_buffer (ep, length,
554
                                &req->dma, GFP_ATOMIC);
555
                if (!req->buf) {
556
                        usb_ep_free_request (ep, req);
557
                        req = 0;
558
                }
559
        }
560
        return req;
561
}
562
 
563
static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
564
{
565
        if (req->buf)
566
                usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
567
        usb_ep_free_request (ep, req);
568
}
569
 
570
/*-------------------------------------------------------------------------*/
571
 
572
/* optionally require specific source/sink data patterns  */
573
 
574
static inline int
575
check_read_data (
576
        struct zero_dev         *dev,
577
        struct usb_ep           *ep,
578
        struct usb_request      *req
579
)
580
{
581
        unsigned        i;
582
        u8              *buf = req->buf;
583
 
584
        for (i = 0; i < req->actual; i++, buf++) {
585
                switch (pattern) {
586
                /* all-zeroes has no synchronization issues */
587
                case 0:
588
                        if (*buf == 0)
589
                                continue;
590
                        break;
591
                /* mod63 stays in sync with short-terminated transfers,
592
                 * or otherwise when host and gadget agree on how large
593
                 * each usb transfer request should be.  resync is done
594
                 * with set_interface or set_config.
595
                 */
596
                case 1:
597
                        if (*buf == (u8)(i % 63))
598
                                continue;
599
                        break;
600
                }
601
                ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
602
                usb_ep_set_halt (ep);
603
                return -EINVAL;
604
        }
605
        return 0;
606
}
607
 
608
static inline void
609
reinit_write_data (
610
        struct zero_dev         *dev,
611
        struct usb_ep           *ep,
612
        struct usb_request      *req
613
)
614
{
615
        unsigned        i;
616
        u8              *buf = req->buf;
617
 
618
        switch (pattern) {
619
        case 0:
620
                memset (req->buf, 0, req->length);
621
                break;
622
        case 1:
623
                for  (i = 0; i < req->length; i++)
624
                        *buf++ = (u8) (i % 63);
625
                break;
626
        }
627
}
628
 
629
/* if there is only one request in the queue, there'll always be an
630
 * irq delay between end of one request and start of the next.
631
 * that prevents using hardware dma queues.
632
 */
633
static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
634
{
635
        struct zero_dev *dev = ep->driver_data;
636
        int             status = req->status;
637
 
638
        switch (status) {
639
 
640
        case 0:                  /* normal completion? */
641
                if (ep == dev->out_ep)
642
                        check_read_data (dev, ep, req);
643
                else
644
                        reinit_write_data (dev, ep, req);
645
                break;
646
 
647
        /* this endpoint is normally active while we're configured */
648
        case -ECONNABORTED:             /* hardware forced ep reset */
649
        case -ECONNRESET:               /* request dequeued */
650
        case -ESHUTDOWN:                /* disconnect from host */
651
                VDEBUG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
652
                                req->actual, req->length);
653
                if (ep == dev->out_ep)
654
                        check_read_data (dev, ep, req);
655
                free_ep_req (ep, req);
656
                return;
657
 
658
        case -EOVERFLOW:                /* buffer overrun on read means that
659
                                         * we didn't provide a big enough
660
                                         * buffer.
661
                                         */
662
        default:
663
#if 1
664
                DEBUG (dev, "%s complete --> %d, %d/%d\n", ep->name,
665
                                status, req->actual, req->length);
666
#endif
667
        case -EREMOTEIO:                /* short read */
668
                break;
669
        }
670
 
671
        status = usb_ep_queue (ep, req, GFP_ATOMIC);
672
        if (status) {
673
                ERROR (dev, "kill %s:  resubmit %d bytes --> %d\n",
674
                                ep->name, req->length, status);
675
                usb_ep_set_halt (ep);
676
                /* FIXME recover later ... somehow */
677
        }
678
}
679
 
680
static struct usb_request *
681
source_sink_start_ep (struct usb_ep *ep, int gfp_flags)
682
{
683
        struct usb_request      *req;
684
        int                     status;
685
 
686
        req = alloc_ep_req (ep, buflen);
687
        if (!req)
688
                return 0;
689
 
690
        memset (req->buf, 0, req->length);
691
        req->complete = source_sink_complete;
692
 
693
        if (strcmp (ep->name, EP_IN_NAME) == 0)
694
                reinit_write_data (ep->driver_data, ep, req);
695
 
696
        status = usb_ep_queue (ep, req, gfp_flags);
697
        if (status) {
698
                struct zero_dev *dev = ep->driver_data;
699
 
700
                ERROR (dev, "start %s --> %d\n", ep->name, status);
701
                free_ep_req (ep, req);
702
                req = 0;
703
        }
704
 
705
        return req;
706
}
707
 
708
static int
709
set_source_sink_config (struct zero_dev *dev, int gfp_flags)
710
{
711
        int                     result = 0;
712
        struct usb_ep           *ep;
713
        struct usb_gadget       *gadget = dev->gadget;
714
 
715
        gadget_for_each_ep (ep, gadget) {
716
                const struct usb_endpoint_descriptor    *d;
717
 
718
                /* one endpoint writes (sources) zeroes in (to the host) */
719
                if (strcmp (ep->name, EP_IN_NAME) == 0) {
720
                        d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
721
                        result = usb_ep_enable (ep, d);
722
                        if (result == 0) {
723
                                ep->driver_data = dev;
724
                                if (source_sink_start_ep (ep, gfp_flags) != 0) {
725
                                        dev->in_ep = ep;
726
                                        continue;
727
                                }
728
                                usb_ep_disable (ep);
729
                                result = -EIO;
730
                        }
731
 
732
                /* one endpoint reads (sinks) anything out (from the host) */
733
                } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
734
                        d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
735
                        result = usb_ep_enable (ep, d);
736
                        if (result == 0) {
737
                                ep->driver_data = dev;
738
                                if (source_sink_start_ep (ep, gfp_flags) != 0) {
739
                                        dev->out_ep = ep;
740
                                        continue;
741
                                }
742
                                usb_ep_disable (ep);
743
                                result = -EIO;
744
                        }
745
 
746
                /* ignore any other endpoints */
747
                } else
748
                        continue;
749
 
750
                /* stop on error */
751
                ERROR (dev, "can't start %s, result %d\n", ep->name, result);
752
                break;
753
        }
754
        if (result == 0)
755
                DEBUG (dev, "buflen %d\n", buflen);
756
 
757
        /* caller is responsible for cleanup on error */
758
        return result;
759
}
760
 
761
/*-------------------------------------------------------------------------*/
762
 
763
static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
764
{
765
        struct zero_dev *dev = ep->driver_data;
766
        int             status = req->status;
767
 
768
        switch (status) {
769
 
770
        case 0:                  /* normal completion? */
771
                if (ep == dev->out_ep) {
772
                        /* loop this OUT packet back IN to the host */
773
                        req->zero = (req->actual < req->length);
774
                        req->length = req->actual;
775
                        status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
776
                        if (status == 0)
777
                                return;
778
 
779
                        /* "should never get here" */
780
                        ERROR (dev, "can't loop %s to %s: %d\n",
781
                                ep->name, dev->in_ep->name,
782
                                status);
783
                }
784
 
785
                /* queue the buffer for some later OUT packet */
786
                req->length = buflen;
787
                status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
788
                if (status == 0)
789
                        return;
790
 
791
                /* "should never get here" */
792
                /* FALLTHROUGH */
793
 
794
        default:
795
                ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
796
                                status, req->actual, req->length);
797
                /* FALLTHROUGH */
798
 
799
        /* NOTE:  since this driver doesn't maintain an explicit record
800
         * of requests it submitted (just maintains qlen count), we
801
         * rely on the hardware driver to clean up on disconnect or
802
         * endpoint disable.
803
         */
804
        case -ECONNABORTED:             /* hardware forced ep reset */
805
        case -ECONNRESET:               /* request dequeued */
806
        case -ESHUTDOWN:                /* disconnect from host */
807
                free_ep_req (ep, req);
808
                return;
809
        }
810
}
811
 
812
static int
813
set_loopback_config (struct zero_dev *dev, int gfp_flags)
814
{
815
        int                     result = 0;
816
        struct usb_ep           *ep;
817
        struct usb_gadget       *gadget = dev->gadget;
818
 
819
        gadget_for_each_ep (ep, gadget) {
820
                const struct usb_endpoint_descriptor    *d;
821
 
822
                /* one endpoint writes data back IN to the host */
823
                if (strcmp (ep->name, EP_IN_NAME) == 0) {
824
                        d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
825
                        result = usb_ep_enable (ep, d);
826
                        if (result == 0) {
827
                                ep->driver_data = dev;
828
                                dev->in_ep = ep;
829
                                continue;
830
                        }
831
 
832
                /* one endpoint just reads OUT packets */
833
                } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
834
                        d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
835
                        result = usb_ep_enable (ep, d);
836
                        if (result == 0) {
837
                                ep->driver_data = dev;
838
                                dev->out_ep = ep;
839
                                continue;
840
                        }
841
 
842
                /* ignore any other endpoints */
843
                } else
844
                        continue;
845
 
846
                /* stop on error */
847
                ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
848
                break;
849
        }
850
 
851
        /* allocate a bunch of read buffers and queue them all at once.
852
         * we buffer at most 'qlen' transfers; fewer if any need more
853
         * than 'buflen' bytes each.
854
         */
855
        if (result == 0) {
856
                struct usb_request      *req;
857
                unsigned                i;
858
 
859
                ep = dev->out_ep;
860
                for (i = 0; i < qlen && result == 0; i++) {
861
                        req = alloc_ep_req (ep, buflen);
862
                        if (req) {
863
                                req->complete = loopback_complete;
864
                                result = usb_ep_queue (ep, req, GFP_ATOMIC);
865
                                if (result)
866
                                        DEBUG (dev, "%s queue req --> %d\n",
867
                                                        ep->name, result);
868
                        } else
869
                                result = -ENOMEM;
870
                }
871
        }
872
        if (result == 0)
873
                DEBUG (dev, "qlen %d, buflen %d\n", qlen, buflen);
874
 
875
        /* caller is responsible for cleanup on error */
876
        return result;
877
}
878
 
879
/*-------------------------------------------------------------------------*/
880
 
881
static void zero_reset_config (struct zero_dev *dev)
882
{
883
        if (dev->config == 0)
884
                return;
885
 
886
        DEBUG (dev, "reset config\n");
887
 
888
        /* just disable endpoints, forcing completion of pending i/o.
889
         * all our completion handlers free their requests in this case.
890
         */
891
        if (dev->in_ep) {
892
                usb_ep_disable (dev->in_ep);
893
                dev->in_ep = 0;
894
        }
895
        if (dev->out_ep) {
896
                usb_ep_disable (dev->out_ep);
897
                dev->out_ep = 0;
898
        }
899
        dev->config = 0;
900
}
901
 
902
/* change our operational config.  this code must agree with the code
903
 * that returns config descriptors, and altsetting code.
904
 *
905
 * it's also responsible for power management interactions. some
906
 * configurations might not work with our current power sources.
907
 *
908
 * note that some device controller hardware will constrain what this
909
 * code can do, perhaps by disallowing more than one configuration or
910
 * by limiting configuration choices (like the pxa2xx).
911
 */
912
static int
913
zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
914
{
915
        int                     result = 0;
916
        struct usb_gadget       *gadget = dev->gadget;
917
 
918
        if (number == dev->config)
919
                return 0;
920
 
921
#ifdef CONFIG_USB_GADGET_SA1100
922
        if (dev->config) {
923
                /* tx fifo is full, but we can't clear it...*/
924
                INFO (dev, "can't change configurations\n");
925
                return -ESPIPE;
926
        }
927
#endif
928
        zero_reset_config (dev);
929
 
930
        switch (number) {
931
        case CONFIG_SOURCE_SINK:
932
                result = set_source_sink_config (dev, gfp_flags);
933
                break;
934
        case CONFIG_LOOPBACK:
935
                result = set_loopback_config (dev, gfp_flags);
936
                break;
937
        default:
938
                result = -EINVAL;
939
                /* FALL THROUGH */
940
        case 0:
941
                return result;
942
        }
943
 
944
        if (!result && (!dev->in_ep || !dev->out_ep))
945
                result = -ENODEV;
946
        if (result)
947
                zero_reset_config (dev);
948
        else {
949
                char *speed;
950
 
951
                switch (gadget->speed) {
952
                case USB_SPEED_LOW:     speed = "low"; break;
953
                case USB_SPEED_FULL:    speed = "full"; break;
954
                case USB_SPEED_HIGH:    speed = "high"; break;
955
                default:                speed = "?"; break;
956
                }
957
 
958
                dev->config = number;
959
                INFO (dev, "%s speed config #%d: %s\n", speed, number,
960
                                (number == CONFIG_SOURCE_SINK)
961
                                        ? source_sink : loopback);
962
        }
963
        return result;
964
}
965
 
966
/*-------------------------------------------------------------------------*/
967
 
968
static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
969
{
970
        if (req->status || req->actual != req->length)
971
                DEBUG ((struct zero_dev *) ep->driver_data,
972
                                "setup complete --> %d, %d/%d\n",
973
                                req->status, req->actual, req->length);
974
}
975
 
976
/*
977
 * The setup() callback implements all the ep0 functionality that's
978
 * not handled lower down, in hardware or the hardware driver (like
979
 * device and endpoint feature flags, and their status).  It's all
980
 * housekeeping for the gadget function we're implementing.  Most of
981
 * the work is in config-specific setup.
982
 */
983
static int
984
zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
985
{
986
        struct zero_dev         *dev = get_gadget_data (gadget);
987
        struct usb_request      *req = dev->req;
988
        int                     value = -EOPNOTSUPP;
989
 
990
        /* usually this stores reply data in the pre-allocated ep0 buffer,
991
         * but config change events will reconfigure hardware.
992
         */
993
        switch (ctrl->bRequest) {
994
 
995
        case USB_REQ_GET_DESCRIPTOR:
996
                if (ctrl->bRequestType != USB_DIR_IN)
997
                        goto unknown;
998
                switch (ctrl->wValue >> 8) {
999
 
1000
                case USB_DT_DEVICE:
1001
                        value = min (ctrl->wLength, (u16) sizeof device_desc);
1002
                        memcpy (req->buf, &device_desc, value);
1003
                        break;
1004
#ifdef CONFIG_USB_GADGET_DUALSPEED
1005
                case USB_DT_DEVICE_QUALIFIER:
1006
                        if (!gadget->is_dualspeed)
1007
                                break;
1008
                        value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
1009
                        memcpy (req->buf, &dev_qualifier, value);
1010
                        break;
1011
 
1012
                case USB_DT_OTHER_SPEED_CONFIG:
1013
                        if (!gadget->is_dualspeed)
1014
                                break;
1015
                        // FALLTHROUGH
1016
#endif /* CONFIG_USB_GADGET_DUALSPEED */
1017
                case USB_DT_CONFIG:
1018
                        value = config_buf (gadget, req->buf,
1019
                                        ctrl->wValue >> 8,
1020
                                        ctrl->wValue & 0xff);
1021
                        if (value >= 0)
1022
                                value = min (ctrl->wLength, (u16) value);
1023
                        break;
1024
 
1025
                case USB_DT_STRING:
1026
                        /* wIndex == language code.
1027
                         * this driver only handles one language, you can
1028
                         * add others even if they don't use iso8859/1
1029
                         */
1030
                        value = usb_gadget_get_string (&stringtab,
1031
                                        ctrl->wValue & 0xff, req->buf);
1032
                        if (value >= 0)
1033
                                value = min (ctrl->wLength, (u16) value);
1034
                        break;
1035
                }
1036
                break;
1037
 
1038
        /* currently two configs, two speeds */
1039
        case USB_REQ_SET_CONFIGURATION:
1040
                if (ctrl->bRequestType != 0)
1041
                        goto unknown;
1042
                spin_lock (&dev->lock);
1043
                value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
1044
                spin_unlock (&dev->lock);
1045
                break;
1046
        case USB_REQ_GET_CONFIGURATION:
1047
                if (ctrl->bRequestType != USB_DIR_IN)
1048
                        goto unknown;
1049
                *(u8 *)req->buf = dev->config;
1050
                value = min (ctrl->wLength, (u16) 1);
1051
                break;
1052
 
1053
        /* until we add altsetting support, or other interfaces,
1054
         * only 0/0 are possible.  pxa2xx only supports 0/0 (poorly)
1055
         * and already killed pending endpoint I/O.
1056
         */
1057
        case USB_REQ_SET_INTERFACE:
1058
                if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1059
                        goto unknown;
1060
                spin_lock (&dev->lock);
1061
                if (dev->config && ctrl->wIndex == 0 && ctrl->wValue == 0) {
1062
                        u8              config = dev->config;
1063
 
1064
                        /* resets interface configuration, forgets about
1065
                         * previous transaction state (queued bufs, etc)
1066
                         * and re-inits endpoint state (toggle etc)
1067
                         * no response queued, just zero status == success.
1068
                         * if we had more than one interface we couldn't
1069
                         * use this "reset the config" shortcut.
1070
                         */
1071
                        zero_reset_config (dev);
1072
                        zero_set_config (dev, config, GFP_ATOMIC);
1073
                        value = 0;
1074
                }
1075
                spin_unlock (&dev->lock);
1076
                break;
1077
        case USB_REQ_GET_INTERFACE:
1078
                if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1079
                        goto unknown;
1080
                if (!dev->config)
1081
                        break;
1082
                if (ctrl->wIndex != 0) {
1083
                        value = -EDOM;
1084
                        break;
1085
                }
1086
                *(u8 *)req->buf = 0;
1087
                value = min (ctrl->wLength, (u16) 1);
1088
                break;
1089
 
1090
        /*
1091
         * These are the same vendor-specific requests supported by
1092
         * Intel's USB 2.0 compliance test devices.  We exceed that
1093
         * device spec by allowing multiple-packet requests.
1094
         */
1095
        case 0x5b:      /* control WRITE test -- fill the buffer */
1096
                if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1097
                        goto unknown;
1098
                if (ctrl->wValue || ctrl->wIndex)
1099
                        break;
1100
                /* just read that many bytes into the buffer */
1101
                if (ctrl->wLength > USB_BUFSIZ)
1102
                        break;
1103
                value = ctrl->wLength;
1104
                break;
1105
        case 0x5c:      /* control READ test -- return the buffer */
1106
                if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1107
                        goto unknown;
1108
                if (ctrl->wValue || ctrl->wIndex)
1109
                        break;
1110
                /* expect those bytes are still in the buffer; send back */
1111
                if (ctrl->wLength > USB_BUFSIZ
1112
                                || ctrl->wLength != req->length)
1113
                        break;
1114
                value = ctrl->wLength;
1115
                break;
1116
 
1117
        default:
1118
unknown:
1119
                VDEBUG (dev,
1120
                        "unknown control req%02x.%02x v%04x i%04x l%d\n",
1121
                        ctrl->bRequestType, ctrl->bRequest,
1122
                        ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1123
        }
1124
 
1125
        /* respond with data transfer before status phase? */
1126
        if (value >= 0) {
1127
                req->length = value;
1128
                value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1129
                if (value < 0) {
1130
                        DEBUG (dev, "ep_queue --> %d\n", value);
1131
                        req->status = 0;
1132
                        zero_setup_complete (gadget->ep0, req);
1133
                }
1134
        }
1135
 
1136
        /* device either stalls (value < 0) or reports success */
1137
        return value;
1138
}
1139
 
1140
static void
1141
zero_disconnect (struct usb_gadget *gadget)
1142
{
1143
        struct zero_dev         *dev = get_gadget_data (gadget);
1144
        unsigned long           flags;
1145
 
1146
        spin_lock_irqsave (&dev->lock, flags);
1147
        zero_reset_config (dev);
1148
 
1149
        /* a more significant application might have some non-usb
1150
         * activities to quiesce here, saving resources like power
1151
         * or pushing the notification up a network stack.
1152
         */
1153
        spin_unlock_irqrestore (&dev->lock, flags);
1154
 
1155
        /* next we may get setup() calls to enumerate new connections;
1156
         * or an unbind() during shutdown (including removing module).
1157
         */
1158
}
1159
 
1160
/*-------------------------------------------------------------------------*/
1161
 
1162
static void
1163
zero_unbind (struct usb_gadget *gadget)
1164
{
1165
        struct zero_dev         *dev = get_gadget_data (gadget);
1166
 
1167
        DEBUG (dev, "unbind\n");
1168
 
1169
        /* we've already been disconnected ... no i/o is active */
1170
        if (dev->req)
1171
                free_ep_req (gadget->ep0, dev->req);
1172
        kfree (dev);
1173
        set_gadget_data (gadget, 0);
1174
}
1175
 
1176
static int
1177
zero_bind (struct usb_gadget *gadget)
1178
{
1179
        struct zero_dev         *dev;
1180
 
1181
        dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1182
        if (!dev)
1183
                return -ENOMEM;
1184
        memset (dev, 0, sizeof *dev);
1185
        spin_lock_init (&dev->lock);
1186
        dev->gadget = gadget;
1187
        set_gadget_data (gadget, dev);
1188
 
1189
        /* preallocate control response and buffer */
1190
        dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1191
        if (!dev->req)
1192
                goto enomem;
1193
        dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1194
                                &dev->req->dma, GFP_KERNEL);
1195
        if (!dev->req->buf)
1196
                goto enomem;
1197
 
1198
        dev->req->complete = zero_setup_complete;
1199
 
1200
        device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1201
 
1202
#ifdef CONFIG_USB_GADGET_DUALSPEED
1203
        /* assume ep0 uses the same value for both speeds ... */
1204
        dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1205
 
1206
        /* and that all endpoints are dual-speed */
1207
        hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1208
        hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1209
#endif
1210
 
1211
        gadget->ep0->driver_data = dev;
1212
 
1213
        INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1214
        INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1215
                EP_OUT_NAME, EP_IN_NAME);
1216
 
1217
        snprintf (manufacturer, sizeof manufacturer,
1218
                UTS_SYSNAME " " UTS_RELEASE " with %s",
1219
                gadget->name);
1220
 
1221
        return 0;
1222
 
1223
enomem:
1224
        zero_unbind (gadget);
1225
        return -ENOMEM;
1226
}
1227
 
1228
/*-------------------------------------------------------------------------*/
1229
 
1230
static struct usb_gadget_driver zero_driver = {
1231
#ifdef CONFIG_USB_GADGET_DUALSPEED
1232
        .speed          = USB_SPEED_HIGH,
1233
#else
1234
        .speed          = USB_SPEED_FULL,
1235
#endif
1236
        .function       = (char *) longname,
1237
        .bind           = zero_bind,
1238
        .unbind         = zero_unbind,
1239
 
1240
        .setup          = zero_setup,
1241
        .disconnect     = zero_disconnect,
1242
 
1243
        .driver         = {
1244
                .name           = (char *) shortname,
1245
                // .shutdown = ...
1246
                // .suspend = ...
1247
                // .resume = ...
1248
        },
1249
};
1250
 
1251
MODULE_AUTHOR ("David Brownell");
1252
MODULE_LICENSE ("Dual BSD/GPL");
1253
 
1254
 
1255
static int __init init (void)
1256
{
1257
        /* a real value would likely come through some id prom
1258
         * or module option.  this one takes at least two packets.
1259
         */
1260
        strncpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
1261
        serial [sizeof serial - 1] = 0;
1262
 
1263
        return usb_gadget_register_driver (&zero_driver);
1264
}
1265
module_init (init);
1266
 
1267
static void __exit cleanup (void)
1268
{
1269
        usb_gadget_unregister_driver (&zero_driver);
1270
}
1271
module_exit (cleanup);
1272
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.