2 * zero.c -- Gadget Zero, for USB development
4 * Copyright (C) 2003-2004 David Brownell
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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.
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.
43 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44 * affect most of the driver.
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".
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:
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
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
65 #include <linux/config.h>
66 #include <linux/module.h>
67 #include <linux/kernel.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 #include <linux/device.h>
81 #include <linux/moduleparam.h>
83 #include <asm/byteorder.h>
86 #include <asm/system.h>
87 #include <asm/unaligned.h>
89 #include <linux/usb_ch9.h>
90 #include <linux/usb_gadget.h>
92 #include "gadget_chips.h"
95 /*-------------------------------------------------------------------------*/
97 #define DRIVER_VERSION "St Patrick's Day 2004"
99 static const char shortname
[] = "zero";
100 static const char longname
[] = "Gadget Zero";
102 static const char source_sink
[] = "source and sink data";
103 static const char loopback
[] = "loop input to output";
105 /*-------------------------------------------------------------------------*/
108 * driver assumes self-powered hardware, and
109 * has no way for users to trigger remote wakeup.
111 * this version autoconfigures as much as possible,
112 * which is reasonable for most "bulk-only" drivers.
114 static const char *EP_IN_NAME
; /* source */
115 static const char *EP_OUT_NAME
; /* sink */
117 /*-------------------------------------------------------------------------*/
119 /* big enough to hold our biggest descriptor */
120 #define USB_BUFSIZ 256
124 struct usb_gadget
*gadget
;
125 struct usb_request
*req
; /* for control responses */
127 /* when configured, we have one of two configs:
128 * - source data (in to host) and sink it (out from host)
129 * - or loop it back (out from host back in to host)
132 struct usb_ep
*in_ep
, *out_ep
;
134 /* autoresume timer */
135 struct timer_list resume
;
138 #define xprintk(d,level,fmt,args...) \
139 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
142 #define DBG(dev,fmt,args...) \
143 xprintk(dev , KERN_DEBUG , fmt , ## args)
145 #define DBG(dev,fmt,args...) \
152 #define VDBG(dev,fmt,args...) \
156 #define ERROR(dev,fmt,args...) \
157 xprintk(dev , KERN_ERR , fmt , ## args)
158 #define WARN(dev,fmt,args...) \
159 xprintk(dev , KERN_WARNING , fmt , ## args)
160 #define INFO(dev,fmt,args...) \
161 xprintk(dev , KERN_INFO , fmt , ## args)
163 /*-------------------------------------------------------------------------*/
165 static unsigned buflen
= 4096;
166 static unsigned qlen
= 32;
167 static unsigned pattern
= 0;
169 module_param (buflen
, uint
, S_IRUGO
|S_IWUSR
);
170 module_param (qlen
, uint
, S_IRUGO
|S_IWUSR
);
171 module_param (pattern
, uint
, S_IRUGO
|S_IWUSR
);
174 * if it's nonzero, autoresume says how many seconds to wait
175 * before trying to wake up the host after suspend.
177 static unsigned autoresume
= 0;
178 module_param (autoresume
, uint
, 0);
181 * Normally the "loopback" configuration is second (index 1) so
182 * it's not the default. Here's where to change that order, to
183 * work better with hosts where config changes are problematic.
184 * Or controllers (like superh) that only support one config.
186 static int loopdefault
= 0;
188 module_param (loopdefault
, bool, S_IRUGO
|S_IWUSR
);
190 /*-------------------------------------------------------------------------*/
192 /* Thanks to NetChip Technologies for donating this product ID.
194 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
195 * Instead: allocate your own, using normal USB-IF procedures.
197 #ifndef CONFIG_USB_ZERO_HNPTEST
198 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
199 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
201 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
202 #define DRIVER_PRODUCT_NUM 0xbadd
205 /*-------------------------------------------------------------------------*/
208 * DESCRIPTORS ... most are static, but strings and (full)
209 * configuration descriptors are built on demand.
212 #define STRING_MANUFACTURER 25
213 #define STRING_PRODUCT 42
214 #define STRING_SERIAL 101
215 #define STRING_SOURCE_SINK 250
216 #define STRING_LOOPBACK 251
219 * This device advertises two configurations; these numbers work
220 * on a pxa250 as well as more flexible hardware.
222 #define CONFIG_SOURCE_SINK 3
223 #define CONFIG_LOOPBACK 2
225 static struct usb_device_descriptor
227 .bLength
= sizeof device_desc
,
228 .bDescriptorType
= USB_DT_DEVICE
,
230 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
231 .bDeviceClass
= USB_CLASS_VENDOR_SPEC
,
233 .idVendor
= __constant_cpu_to_le16 (DRIVER_VENDOR_NUM
),
234 .idProduct
= __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM
),
235 .iManufacturer
= STRING_MANUFACTURER
,
236 .iProduct
= STRING_PRODUCT
,
237 .iSerialNumber
= STRING_SERIAL
,
238 .bNumConfigurations
= 2,
241 static struct usb_config_descriptor
242 source_sink_config
= {
243 .bLength
= sizeof source_sink_config
,
244 .bDescriptorType
= USB_DT_CONFIG
,
246 /* compute wTotalLength on the fly */
248 .bConfigurationValue
= CONFIG_SOURCE_SINK
,
249 .iConfiguration
= STRING_SOURCE_SINK
,
250 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
251 .bMaxPower
= 1, /* self-powered */
254 static struct usb_config_descriptor
256 .bLength
= sizeof loopback_config
,
257 .bDescriptorType
= USB_DT_CONFIG
,
259 /* compute wTotalLength on the fly */
261 .bConfigurationValue
= CONFIG_LOOPBACK
,
262 .iConfiguration
= STRING_LOOPBACK
,
263 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
264 .bMaxPower
= 1, /* self-powered */
267 static struct usb_otg_descriptor
269 .bLength
= sizeof otg_descriptor
,
270 .bDescriptorType
= USB_DT_OTG
,
272 .bmAttributes
= USB_OTG_SRP
,
275 /* one interface in each configuration */
277 static const struct usb_interface_descriptor
279 .bLength
= sizeof source_sink_intf
,
280 .bDescriptorType
= USB_DT_INTERFACE
,
283 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
284 .iInterface
= STRING_SOURCE_SINK
,
287 static const struct usb_interface_descriptor
289 .bLength
= sizeof loopback_intf
,
290 .bDescriptorType
= USB_DT_INTERFACE
,
293 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
294 .iInterface
= STRING_LOOPBACK
,
297 /* two full speed bulk endpoints; their use is config-dependent */
299 static struct usb_endpoint_descriptor
301 .bLength
= USB_DT_ENDPOINT_SIZE
,
302 .bDescriptorType
= USB_DT_ENDPOINT
,
304 .bEndpointAddress
= USB_DIR_IN
,
305 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
308 static struct usb_endpoint_descriptor
310 .bLength
= USB_DT_ENDPOINT_SIZE
,
311 .bDescriptorType
= USB_DT_ENDPOINT
,
313 .bEndpointAddress
= USB_DIR_OUT
,
314 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
317 static const struct usb_descriptor_header
*fs_source_sink_function
[] = {
318 (struct usb_descriptor_header
*) &otg_descriptor
,
319 (struct usb_descriptor_header
*) &source_sink_intf
,
320 (struct usb_descriptor_header
*) &fs_sink_desc
,
321 (struct usb_descriptor_header
*) &fs_source_desc
,
325 static const struct usb_descriptor_header
*fs_loopback_function
[] = {
326 (struct usb_descriptor_header
*) &otg_descriptor
,
327 (struct usb_descriptor_header
*) &loopback_intf
,
328 (struct usb_descriptor_header
*) &fs_sink_desc
,
329 (struct usb_descriptor_header
*) &fs_source_desc
,
333 #ifdef CONFIG_USB_GADGET_DUALSPEED
336 * usb 2.0 devices need to expose both high speed and full speed
337 * descriptors, unless they only run at full speed.
339 * that means alternate endpoint descriptors (bigger packets)
340 * and a "device qualifier" ... plus more construction options
341 * for the config descriptor.
344 static struct usb_endpoint_descriptor
346 .bLength
= USB_DT_ENDPOINT_SIZE
,
347 .bDescriptorType
= USB_DT_ENDPOINT
,
349 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
350 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
353 static struct usb_endpoint_descriptor
355 .bLength
= USB_DT_ENDPOINT_SIZE
,
356 .bDescriptorType
= USB_DT_ENDPOINT
,
358 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
359 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
362 static struct usb_qualifier_descriptor
364 .bLength
= sizeof dev_qualifier
,
365 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
367 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
368 .bDeviceClass
= USB_CLASS_VENDOR_SPEC
,
370 .bNumConfigurations
= 2,
373 static const struct usb_descriptor_header
*hs_source_sink_function
[] = {
374 (struct usb_descriptor_header
*) &otg_descriptor
,
375 (struct usb_descriptor_header
*) &source_sink_intf
,
376 (struct usb_descriptor_header
*) &hs_source_desc
,
377 (struct usb_descriptor_header
*) &hs_sink_desc
,
381 static const struct usb_descriptor_header
*hs_loopback_function
[] = {
382 (struct usb_descriptor_header
*) &otg_descriptor
,
383 (struct usb_descriptor_header
*) &loopback_intf
,
384 (struct usb_descriptor_header
*) &hs_source_desc
,
385 (struct usb_descriptor_header
*) &hs_sink_desc
,
389 /* maxpacket and other transfer characteristics vary by speed. */
390 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
394 /* if there's no high speed support, maxpacket doesn't change. */
395 #define ep_desc(g,hs,fs) fs
397 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
399 static char manufacturer
[40];
400 static char serial
[40];
402 /* static strings, in UTF-8 */
403 static struct usb_string strings
[] = {
404 { STRING_MANUFACTURER
, manufacturer
, },
405 { STRING_PRODUCT
, longname
, },
406 { STRING_SERIAL
, serial
, },
407 { STRING_LOOPBACK
, loopback
, },
408 { STRING_SOURCE_SINK
, source_sink
, },
409 { } /* end of list */
412 static struct usb_gadget_strings stringtab
= {
413 .language
= 0x0409, /* en-us */
418 * config descriptors are also handcrafted. these must agree with code
419 * that sets configurations, and with code managing interfaces and their
420 * altsettings. other complexity may come from:
422 * - high speed support, including "other speed config" rules
423 * - multiple configurations
424 * - interfaces with alternate settings
425 * - embedded class or vendor-specific descriptors
427 * this handles high speed, and has a second config that could as easily
428 * have been an alternate interface setting (on most hardware).
430 * NOTE: to demonstrate (and test) more USB capabilities, this driver
431 * should include an altsetting to test interrupt transfers, including
432 * high bandwidth modes at high speed. (Maybe work like Intel's test
436 config_buf (struct usb_gadget
*gadget
,
437 u8
*buf
, u8 type
, unsigned index
)
441 const struct usb_descriptor_header
**function
;
442 #ifdef CONFIG_USB_GADGET_DUALSPEED
443 int hs
= (gadget
->speed
== USB_SPEED_HIGH
);
446 /* two configurations will always be index 0 and index 1 */
449 is_source_sink
= loopdefault
? (index
== 1) : (index
== 0);
451 #ifdef CONFIG_USB_GADGET_DUALSPEED
452 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
455 function
= is_source_sink
456 ? hs_source_sink_function
457 : hs_loopback_function
;
460 function
= is_source_sink
461 ? fs_source_sink_function
462 : fs_loopback_function
;
464 /* for now, don't advertise srp-only devices */
468 len
= usb_gadget_config_buf (is_source_sink
469 ? &source_sink_config
471 buf
, USB_BUFSIZ
, function
);
474 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
478 /*-------------------------------------------------------------------------*/
480 static struct usb_request
*
481 alloc_ep_req (struct usb_ep
*ep
, unsigned length
)
483 struct usb_request
*req
;
485 req
= usb_ep_alloc_request (ep
, GFP_ATOMIC
);
487 req
->length
= length
;
488 req
->buf
= usb_ep_alloc_buffer (ep
, length
,
489 &req
->dma
, GFP_ATOMIC
);
491 usb_ep_free_request (ep
, req
);
498 static void free_ep_req (struct usb_ep
*ep
, struct usb_request
*req
)
501 usb_ep_free_buffer (ep
, req
->buf
, req
->dma
, req
->length
);
502 usb_ep_free_request (ep
, req
);
505 /*-------------------------------------------------------------------------*/
507 /* optionally require specific source/sink data patterns */
511 struct zero_dev
*dev
,
513 struct usb_request
*req
519 for (i
= 0; i
< req
->actual
; i
++, buf
++) {
521 /* all-zeroes has no synchronization issues */
526 /* mod63 stays in sync with short-terminated transfers,
527 * or otherwise when host and gadget agree on how large
528 * each usb transfer request should be. resync is done
529 * with set_interface or set_config.
532 if (*buf
== (u8
)(i
% 63))
536 ERROR (dev
, "bad OUT byte, buf [%d] = %d\n", i
, *buf
);
537 usb_ep_set_halt (ep
);
545 struct zero_dev
*dev
,
547 struct usb_request
*req
555 memset (req
->buf
, 0, req
->length
);
558 for (i
= 0; i
< req
->length
; i
++)
559 *buf
++ = (u8
) (i
% 63);
564 /* if there is only one request in the queue, there'll always be an
565 * irq delay between end of one request and start of the next.
566 * that prevents using hardware dma queues.
568 static void source_sink_complete (struct usb_ep
*ep
, struct usb_request
*req
)
570 struct zero_dev
*dev
= ep
->driver_data
;
571 int status
= req
->status
;
575 case 0: /* normal completion? */
576 if (ep
== dev
->out_ep
)
577 check_read_data (dev
, ep
, req
);
579 reinit_write_data (dev
, ep
, req
);
582 /* this endpoint is normally active while we're configured */
583 case -ECONNABORTED
: /* hardware forced ep reset */
584 case -ECONNRESET
: /* request dequeued */
585 case -ESHUTDOWN
: /* disconnect from host */
586 VDBG (dev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
587 req
->actual
, req
->length
);
588 if (ep
== dev
->out_ep
)
589 check_read_data (dev
, ep
, req
);
590 free_ep_req (ep
, req
);
593 case -EOVERFLOW
: /* buffer overrun on read means that
594 * we didn't provide a big enough
599 DBG (dev
, "%s complete --> %d, %d/%d\n", ep
->name
,
600 status
, req
->actual
, req
->length
);
602 case -EREMOTEIO
: /* short read */
606 status
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
608 ERROR (dev
, "kill %s: resubmit %d bytes --> %d\n",
609 ep
->name
, req
->length
, status
);
610 usb_ep_set_halt (ep
);
611 /* FIXME recover later ... somehow */
615 static struct usb_request
*
616 source_sink_start_ep (struct usb_ep
*ep
, int gfp_flags
)
618 struct usb_request
*req
;
621 req
= alloc_ep_req (ep
, buflen
);
625 memset (req
->buf
, 0, req
->length
);
626 req
->complete
= source_sink_complete
;
628 if (strcmp (ep
->name
, EP_IN_NAME
) == 0)
629 reinit_write_data (ep
->driver_data
, ep
, req
);
631 status
= usb_ep_queue (ep
, req
, gfp_flags
);
633 struct zero_dev
*dev
= ep
->driver_data
;
635 ERROR (dev
, "start %s --> %d\n", ep
->name
, status
);
636 free_ep_req (ep
, req
);
644 set_source_sink_config (struct zero_dev
*dev
, int gfp_flags
)
648 struct usb_gadget
*gadget
= dev
->gadget
;
650 gadget_for_each_ep (ep
, gadget
) {
651 const struct usb_endpoint_descriptor
*d
;
653 /* one endpoint writes (sources) zeroes in (to the host) */
654 if (strcmp (ep
->name
, EP_IN_NAME
) == 0) {
655 d
= ep_desc (gadget
, &hs_source_desc
, &fs_source_desc
);
656 result
= usb_ep_enable (ep
, d
);
658 ep
->driver_data
= dev
;
659 if (source_sink_start_ep (ep
, gfp_flags
) != 0) {
667 /* one endpoint reads (sinks) anything out (from the host) */
668 } else if (strcmp (ep
->name
, EP_OUT_NAME
) == 0) {
669 d
= ep_desc (gadget
, &hs_sink_desc
, &fs_sink_desc
);
670 result
= usb_ep_enable (ep
, d
);
672 ep
->driver_data
= dev
;
673 if (source_sink_start_ep (ep
, gfp_flags
) != 0) {
681 /* ignore any other endpoints */
686 ERROR (dev
, "can't start %s, result %d\n", ep
->name
, result
);
690 DBG (dev
, "buflen %d\n", buflen
);
692 /* caller is responsible for cleanup on error */
696 /*-------------------------------------------------------------------------*/
698 static void loopback_complete (struct usb_ep
*ep
, struct usb_request
*req
)
700 struct zero_dev
*dev
= ep
->driver_data
;
701 int status
= req
->status
;
705 case 0: /* normal completion? */
706 if (ep
== dev
->out_ep
) {
707 /* loop this OUT packet back IN to the host */
708 req
->zero
= (req
->actual
< req
->length
);
709 req
->length
= req
->actual
;
710 status
= usb_ep_queue (dev
->in_ep
, req
, GFP_ATOMIC
);
714 /* "should never get here" */
715 ERROR (dev
, "can't loop %s to %s: %d\n",
716 ep
->name
, dev
->in_ep
->name
,
720 /* queue the buffer for some later OUT packet */
721 req
->length
= buflen
;
722 status
= usb_ep_queue (dev
->out_ep
, req
, GFP_ATOMIC
);
726 /* "should never get here" */
730 ERROR (dev
, "%s loop complete --> %d, %d/%d\n", ep
->name
,
731 status
, req
->actual
, req
->length
);
734 /* NOTE: since this driver doesn't maintain an explicit record
735 * of requests it submitted (just maintains qlen count), we
736 * rely on the hardware driver to clean up on disconnect or
739 case -ECONNABORTED
: /* hardware forced ep reset */
740 case -ECONNRESET
: /* request dequeued */
741 case -ESHUTDOWN
: /* disconnect from host */
742 free_ep_req (ep
, req
);
748 set_loopback_config (struct zero_dev
*dev
, int gfp_flags
)
752 struct usb_gadget
*gadget
= dev
->gadget
;
754 gadget_for_each_ep (ep
, gadget
) {
755 const struct usb_endpoint_descriptor
*d
;
757 /* one endpoint writes data back IN to the host */
758 if (strcmp (ep
->name
, EP_IN_NAME
) == 0) {
759 d
= ep_desc (gadget
, &hs_source_desc
, &fs_source_desc
);
760 result
= usb_ep_enable (ep
, d
);
762 ep
->driver_data
= dev
;
767 /* one endpoint just reads OUT packets */
768 } else if (strcmp (ep
->name
, EP_OUT_NAME
) == 0) {
769 d
= ep_desc (gadget
, &hs_sink_desc
, &fs_sink_desc
);
770 result
= usb_ep_enable (ep
, d
);
772 ep
->driver_data
= dev
;
777 /* ignore any other endpoints */
782 ERROR (dev
, "can't enable %s, result %d\n", ep
->name
, result
);
786 /* allocate a bunch of read buffers and queue them all at once.
787 * we buffer at most 'qlen' transfers; fewer if any need more
788 * than 'buflen' bytes each.
791 struct usb_request
*req
;
795 for (i
= 0; i
< qlen
&& result
== 0; i
++) {
796 req
= alloc_ep_req (ep
, buflen
);
798 req
->complete
= loopback_complete
;
799 result
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
801 DBG (dev
, "%s queue req --> %d\n",
808 DBG (dev
, "qlen %d, buflen %d\n", qlen
, buflen
);
810 /* caller is responsible for cleanup on error */
814 /*-------------------------------------------------------------------------*/
816 static void zero_reset_config (struct zero_dev
*dev
)
818 if (dev
->config
== 0)
821 DBG (dev
, "reset config\n");
823 /* just disable endpoints, forcing completion of pending i/o.
824 * all our completion handlers free their requests in this case.
827 usb_ep_disable (dev
->in_ep
);
831 usb_ep_disable (dev
->out_ep
);
835 del_timer (&dev
->resume
);
838 /* change our operational config. this code must agree with the code
839 * that returns config descriptors, and altsetting code.
841 * it's also responsible for power management interactions. some
842 * configurations might not work with our current power sources.
844 * note that some device controller hardware will constrain what this
845 * code can do, perhaps by disallowing more than one configuration or
846 * by limiting configuration choices (like the pxa2xx).
849 zero_set_config (struct zero_dev
*dev
, unsigned number
, int gfp_flags
)
852 struct usb_gadget
*gadget
= dev
->gadget
;
854 if (number
== dev
->config
)
857 if (gadget_is_sa1100 (gadget
) && dev
->config
) {
858 /* tx fifo is full, but we can't clear it...*/
859 INFO (dev
, "can't change configurations\n");
862 zero_reset_config (dev
);
865 case CONFIG_SOURCE_SINK
:
866 result
= set_source_sink_config (dev
, gfp_flags
);
868 case CONFIG_LOOPBACK
:
869 result
= set_loopback_config (dev
, gfp_flags
);
878 if (!result
&& (!dev
->in_ep
|| !dev
->out_ep
))
881 zero_reset_config (dev
);
885 switch (gadget
->speed
) {
886 case USB_SPEED_LOW
: speed
= "low"; break;
887 case USB_SPEED_FULL
: speed
= "full"; break;
888 case USB_SPEED_HIGH
: speed
= "high"; break;
889 default: speed
= "?"; break;
892 dev
->config
= number
;
893 INFO (dev
, "%s speed config #%d: %s\n", speed
, number
,
894 (number
== CONFIG_SOURCE_SINK
)
895 ? source_sink
: loopback
);
900 /*-------------------------------------------------------------------------*/
902 static void zero_setup_complete (struct usb_ep
*ep
, struct usb_request
*req
)
904 if (req
->status
|| req
->actual
!= req
->length
)
905 DBG ((struct zero_dev
*) ep
->driver_data
,
906 "setup complete --> %d, %d/%d\n",
907 req
->status
, req
->actual
, req
->length
);
911 * The setup() callback implements all the ep0 functionality that's
912 * not handled lower down, in hardware or the hardware driver (like
913 * device and endpoint feature flags, and their status). It's all
914 * housekeeping for the gadget function we're implementing. Most of
915 * the work is in config-specific setup.
918 zero_setup (struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
920 struct zero_dev
*dev
= get_gadget_data (gadget
);
921 struct usb_request
*req
= dev
->req
;
922 int value
= -EOPNOTSUPP
;
924 /* usually this stores reply data in the pre-allocated ep0 buffer,
925 * but config change events will reconfigure hardware.
928 switch (ctrl
->bRequest
) {
930 case USB_REQ_GET_DESCRIPTOR
:
931 if (ctrl
->bRequestType
!= USB_DIR_IN
)
933 switch (ctrl
->wValue
>> 8) {
936 value
= min (ctrl
->wLength
, (u16
) sizeof device_desc
);
937 memcpy (req
->buf
, &device_desc
, value
);
939 #ifdef CONFIG_USB_GADGET_DUALSPEED
940 case USB_DT_DEVICE_QUALIFIER
:
941 if (!gadget
->is_dualspeed
)
943 value
= min (ctrl
->wLength
, (u16
) sizeof dev_qualifier
);
944 memcpy (req
->buf
, &dev_qualifier
, value
);
947 case USB_DT_OTHER_SPEED_CONFIG
:
948 if (!gadget
->is_dualspeed
)
951 #endif /* CONFIG_USB_GADGET_DUALSPEED */
953 value
= config_buf (gadget
, req
->buf
,
955 ctrl
->wValue
& 0xff);
957 value
= min (ctrl
->wLength
, (u16
) value
);
961 /* wIndex == language code.
962 * this driver only handles one language, you can
963 * add string tables for other languages, using
964 * any UTF-8 characters
966 value
= usb_gadget_get_string (&stringtab
,
967 ctrl
->wValue
& 0xff, req
->buf
);
969 value
= min (ctrl
->wLength
, (u16
) value
);
974 /* currently two configs, two speeds */
975 case USB_REQ_SET_CONFIGURATION
:
976 if (ctrl
->bRequestType
!= 0)
978 if (gadget
->a_hnp_support
)
979 DBG (dev
, "HNP available\n");
980 else if (gadget
->a_alt_hnp_support
)
981 DBG (dev
, "HNP needs a different root port\n");
983 VDBG (dev
, "HNP inactive\n");
984 spin_lock (&dev
->lock
);
985 value
= zero_set_config (dev
, ctrl
->wValue
, GFP_ATOMIC
);
986 spin_unlock (&dev
->lock
);
988 case USB_REQ_GET_CONFIGURATION
:
989 if (ctrl
->bRequestType
!= USB_DIR_IN
)
991 *(u8
*)req
->buf
= dev
->config
;
992 value
= min (ctrl
->wLength
, (u16
) 1);
995 /* until we add altsetting support, or other interfaces,
996 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
997 * and already killed pending endpoint I/O.
999 case USB_REQ_SET_INTERFACE
:
1000 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1002 spin_lock (&dev
->lock
);
1003 if (dev
->config
&& ctrl
->wIndex
== 0 && ctrl
->wValue
== 0) {
1004 u8 config
= dev
->config
;
1006 /* resets interface configuration, forgets about
1007 * previous transaction state (queued bufs, etc)
1008 * and re-inits endpoint state (toggle etc)
1009 * no response queued, just zero status == success.
1010 * if we had more than one interface we couldn't
1011 * use this "reset the config" shortcut.
1013 zero_reset_config (dev
);
1014 zero_set_config (dev
, config
, GFP_ATOMIC
);
1017 spin_unlock (&dev
->lock
);
1019 case USB_REQ_GET_INTERFACE
:
1020 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1024 if (ctrl
->wIndex
!= 0) {
1028 *(u8
*)req
->buf
= 0;
1029 value
= min (ctrl
->wLength
, (u16
) 1);
1033 * These are the same vendor-specific requests supported by
1034 * Intel's USB 2.0 compliance test devices. We exceed that
1035 * device spec by allowing multiple-packet requests.
1037 case 0x5b: /* control WRITE test -- fill the buffer */
1038 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|USB_TYPE_VENDOR
))
1040 if (ctrl
->wValue
|| ctrl
->wIndex
)
1042 /* just read that many bytes into the buffer */
1043 if (ctrl
->wLength
> USB_BUFSIZ
)
1045 value
= ctrl
->wLength
;
1047 case 0x5c: /* control READ test -- return the buffer */
1048 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_TYPE_VENDOR
))
1050 if (ctrl
->wValue
|| ctrl
->wIndex
)
1052 /* expect those bytes are still in the buffer; send back */
1053 if (ctrl
->wLength
> USB_BUFSIZ
1054 || ctrl
->wLength
!= req
->length
)
1056 value
= ctrl
->wLength
;
1062 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1063 ctrl
->bRequestType
, ctrl
->bRequest
,
1064 ctrl
->wValue
, ctrl
->wIndex
, ctrl
->wLength
);
1067 /* respond with data transfer before status phase? */
1069 req
->length
= value
;
1070 req
->zero
= value
< ctrl
->wLength
1071 && (value
% gadget
->ep0
->maxpacket
) == 0;
1072 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1074 DBG (dev
, "ep_queue --> %d\n", value
);
1076 zero_setup_complete (gadget
->ep0
, req
);
1080 /* device either stalls (value < 0) or reports success */
1085 zero_disconnect (struct usb_gadget
*gadget
)
1087 struct zero_dev
*dev
= get_gadget_data (gadget
);
1088 unsigned long flags
;
1090 spin_lock_irqsave (&dev
->lock
, flags
);
1091 zero_reset_config (dev
);
1093 /* a more significant application might have some non-usb
1094 * activities to quiesce here, saving resources like power
1095 * or pushing the notification up a network stack.
1097 spin_unlock_irqrestore (&dev
->lock
, flags
);
1099 /* next we may get setup() calls to enumerate new connections;
1100 * or an unbind() during shutdown (including removing module).
1105 zero_autoresume (unsigned long _dev
)
1107 struct zero_dev
*dev
= (struct zero_dev
*) _dev
;
1110 /* normally the host would be woken up for something
1111 * more significant than just a timer firing...
1113 if (dev
->gadget
->speed
!= USB_SPEED_UNKNOWN
) {
1114 status
= usb_gadget_wakeup (dev
->gadget
);
1115 DBG (dev
, "wakeup --> %d\n", status
);
1119 /*-------------------------------------------------------------------------*/
1122 zero_unbind (struct usb_gadget
*gadget
)
1124 struct zero_dev
*dev
= get_gadget_data (gadget
);
1126 DBG (dev
, "unbind\n");
1128 /* we've already been disconnected ... no i/o is active */
1130 free_ep_req (gadget
->ep0
, dev
->req
);
1131 del_timer_sync (&dev
->resume
);
1133 set_gadget_data (gadget
, NULL
);
1137 zero_bind (struct usb_gadget
*gadget
)
1139 struct zero_dev
*dev
;
1142 /* Bulk-only drivers like this one SHOULD be able to
1143 * autoconfigure on any sane usb controller driver,
1144 * but there may also be important quirks to address.
1146 usb_ep_autoconfig_reset (gadget
);
1147 ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
1150 printk (KERN_ERR
"%s: can't autoconfigure on %s\n",
1151 shortname
, gadget
->name
);
1154 EP_IN_NAME
= ep
->name
;
1155 ep
->driver_data
= ep
; /* claim */
1157 ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
1160 EP_OUT_NAME
= ep
->name
;
1161 ep
->driver_data
= ep
; /* claim */
1165 * DRIVER POLICY CHOICE: you may want to do this differently.
1166 * One thing to avoid is reusing a bcdDevice revision code
1167 * with different host-visible configurations or behavior
1168 * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1170 if (gadget_is_net2280 (gadget
)) {
1171 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0201);
1172 } else if (gadget_is_pxa (gadget
)) {
1173 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0203);
1175 } else if (gadget_is_sh(gadget
)) {
1176 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0204);
1177 /* SH has only one configuration; see "loopdefault" */
1178 device_desc
.bNumConfigurations
= 1;
1179 /* FIXME make 1 == default.bConfigurationValue */
1181 } else if (gadget_is_sa1100 (gadget
)) {
1182 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0205);
1183 } else if (gadget_is_goku (gadget
)) {
1184 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0206);
1185 } else if (gadget_is_mq11xx (gadget
)) {
1186 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0207);
1187 } else if (gadget_is_omap (gadget
)) {
1188 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0208);
1189 } else if (gadget_is_lh7a40x(gadget
)) {
1190 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x0209);
1192 /* gadget zero is so simple (for now, no altsettings) that
1193 * it SHOULD NOT have problems with bulk-capable hardware.
1194 * so warn about unrcognized controllers, don't panic.
1196 * things like configuration and altsetting numbering
1197 * can need hardware-specific attention though.
1199 printk (KERN_WARNING
"%s: controller '%s' not recognized\n",
1200 shortname
, gadget
->name
);
1201 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x9999);
1205 /* ok, we made sense of the hardware ... */
1206 dev
= kmalloc (sizeof *dev
, SLAB_KERNEL
);
1209 memset (dev
, 0, sizeof *dev
);
1210 spin_lock_init (&dev
->lock
);
1211 dev
->gadget
= gadget
;
1212 set_gadget_data (gadget
, dev
);
1214 /* preallocate control response and buffer */
1215 dev
->req
= usb_ep_alloc_request (gadget
->ep0
, GFP_KERNEL
);
1218 dev
->req
->buf
= usb_ep_alloc_buffer (gadget
->ep0
, USB_BUFSIZ
,
1219 &dev
->req
->dma
, GFP_KERNEL
);
1223 dev
->req
->complete
= zero_setup_complete
;
1225 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
1227 #ifdef CONFIG_USB_GADGET_DUALSPEED
1228 /* assume ep0 uses the same value for both speeds ... */
1229 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
1231 /* and that all endpoints are dual-speed */
1232 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
1233 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
1236 if (gadget
->is_otg
) {
1237 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
1238 source_sink_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1239 loopback_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1242 if (gadget
->is_otg
) {
1243 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
1244 source_sink_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1245 loopback_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1248 usb_gadget_set_selfpowered (gadget
);
1250 init_timer (&dev
->resume
);
1251 dev
->resume
.function
= zero_autoresume
;
1252 dev
->resume
.data
= (unsigned long) dev
;
1254 source_sink_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1255 loopback_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1258 gadget
->ep0
->driver_data
= dev
;
1260 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", longname
);
1261 INFO (dev
, "using %s, OUT %s IN %s\n", gadget
->name
,
1262 EP_OUT_NAME
, EP_IN_NAME
);
1264 snprintf (manufacturer
, sizeof manufacturer
,
1265 UTS_SYSNAME
" " UTS_RELEASE
" with %s",
1271 zero_unbind (gadget
);
1275 /*-------------------------------------------------------------------------*/
1278 zero_suspend (struct usb_gadget
*gadget
)
1280 struct zero_dev
*dev
= get_gadget_data (gadget
);
1282 if (gadget
->speed
== USB_SPEED_UNKNOWN
)
1286 mod_timer (&dev
->resume
, jiffies
+ (HZ
* autoresume
));
1287 DBG (dev
, "suspend, wakeup in %d seconds\n", autoresume
);
1289 DBG (dev
, "suspend\n");
1293 zero_resume (struct usb_gadget
*gadget
)
1295 struct zero_dev
*dev
= get_gadget_data (gadget
);
1297 DBG (dev
, "resume\n");
1298 del_timer (&dev
->resume
);
1302 /*-------------------------------------------------------------------------*/
1304 static struct usb_gadget_driver zero_driver
= {
1305 #ifdef CONFIG_USB_GADGET_DUALSPEED
1306 .speed
= USB_SPEED_HIGH
,
1308 .speed
= USB_SPEED_FULL
,
1310 .function
= (char *) longname
,
1312 .unbind
= zero_unbind
,
1314 .setup
= zero_setup
,
1315 .disconnect
= zero_disconnect
,
1317 .suspend
= zero_suspend
,
1318 .resume
= zero_resume
,
1321 .name
= (char *) shortname
,
1328 MODULE_AUTHOR ("David Brownell");
1329 MODULE_LICENSE ("Dual BSD/GPL");
1332 static int __init
init (void)
1334 /* a real value would likely come through some id prom
1335 * or module option. this one takes at least two packets.
1337 strlcpy (serial
, "0123456789.0123456789.0123456789", sizeof serial
);
1339 return usb_gadget_register_driver (&zero_driver
);
1343 static void __exit
cleanup (void)
1345 usb_gadget_unregister_driver (&zero_driver
);
1347 module_exit (cleanup
);