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/module.h>
66 #include <linux/kernel.h>
67 #include <linux/delay.h>
68 #include <linux/ioport.h>
69 #include <linux/slab.h>
70 #include <linux/smp_lock.h>
71 #include <linux/errno.h>
72 #include <linux/init.h>
73 #include <linux/timer.h>
74 #include <linux/list.h>
75 #include <linux/interrupt.h>
76 #include <linux/utsname.h>
77 #include <linux/device.h>
78 #include <linux/moduleparam.h>
80 #include <asm/byteorder.h>
83 #include <asm/system.h>
84 #include <asm/unaligned.h>
86 #include <linux/usb/ch9.h>
87 #include <linux/usb_gadget.h>
89 #include "gadget_chips.h"
92 /*-------------------------------------------------------------------------*/
94 #define DRIVER_VERSION "St Patrick's Day 2004"
96 static const char shortname
[] = "zero";
97 static const char longname
[] = "Gadget Zero";
99 static const char source_sink
[] = "source and sink data";
100 static const char loopback
[] = "loop input to output";
102 /*-------------------------------------------------------------------------*/
105 * driver assumes self-powered hardware, and
106 * has no way for users to trigger remote wakeup.
108 * this version autoconfigures as much as possible,
109 * which is reasonable for most "bulk-only" drivers.
111 static const char *EP_IN_NAME
; /* source */
112 static const char *EP_OUT_NAME
; /* sink */
114 /*-------------------------------------------------------------------------*/
116 /* big enough to hold our biggest descriptor */
117 #define USB_BUFSIZ 256
121 struct usb_gadget
*gadget
;
122 struct usb_request
*req
; /* for control responses */
124 /* when configured, we have one of two configs:
125 * - source data (in to host) and sink it (out from host)
126 * - or loop it back (out from host back in to host)
129 struct usb_ep
*in_ep
, *out_ep
;
131 /* autoresume timer */
132 struct timer_list resume
;
135 #define xprintk(d,level,fmt,args...) \
136 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
139 #define DBG(dev,fmt,args...) \
140 xprintk(dev , KERN_DEBUG , fmt , ## args)
142 #define DBG(dev,fmt,args...) \
149 #define VDBG(dev,fmt,args...) \
153 #define ERROR(dev,fmt,args...) \
154 xprintk(dev , KERN_ERR , fmt , ## args)
155 #define WARN(dev,fmt,args...) \
156 xprintk(dev , KERN_WARNING , fmt , ## args)
157 #define INFO(dev,fmt,args...) \
158 xprintk(dev , KERN_INFO , fmt , ## args)
160 /*-------------------------------------------------------------------------*/
162 static unsigned buflen
= 4096;
163 static unsigned qlen
= 32;
164 static unsigned pattern
= 0;
166 module_param (buflen
, uint
, S_IRUGO
);
167 module_param (qlen
, uint
, S_IRUGO
);
168 module_param (pattern
, uint
, S_IRUGO
|S_IWUSR
);
171 * if it's nonzero, autoresume says how many seconds to wait
172 * before trying to wake up the host after suspend.
174 static unsigned autoresume
= 0;
175 module_param (autoresume
, uint
, 0);
178 * Normally the "loopback" configuration is second (index 1) so
179 * it's not the default. Here's where to change that order, to
180 * work better with hosts where config changes are problematic.
181 * Or controllers (like superh) that only support one config.
183 static int loopdefault
= 0;
185 module_param (loopdefault
, bool, S_IRUGO
|S_IWUSR
);
187 /*-------------------------------------------------------------------------*/
189 /* Thanks to NetChip Technologies for donating this product ID.
191 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
192 * Instead: allocate your own, using normal USB-IF procedures.
194 #ifndef CONFIG_USB_ZERO_HNPTEST
195 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
196 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
198 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
199 #define DRIVER_PRODUCT_NUM 0xbadd
202 /*-------------------------------------------------------------------------*/
205 * DESCRIPTORS ... most are static, but strings and (full)
206 * configuration descriptors are built on demand.
209 #define STRING_MANUFACTURER 25
210 #define STRING_PRODUCT 42
211 #define STRING_SERIAL 101
212 #define STRING_SOURCE_SINK 250
213 #define STRING_LOOPBACK 251
216 * This device advertises two configurations; these numbers work
217 * on a pxa250 as well as more flexible hardware.
219 #define CONFIG_SOURCE_SINK 3
220 #define CONFIG_LOOPBACK 2
222 static struct usb_device_descriptor
224 .bLength
= sizeof device_desc
,
225 .bDescriptorType
= USB_DT_DEVICE
,
227 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
228 .bDeviceClass
= USB_CLASS_VENDOR_SPEC
,
230 .idVendor
= __constant_cpu_to_le16 (DRIVER_VENDOR_NUM
),
231 .idProduct
= __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM
),
232 .iManufacturer
= STRING_MANUFACTURER
,
233 .iProduct
= STRING_PRODUCT
,
234 .iSerialNumber
= STRING_SERIAL
,
235 .bNumConfigurations
= 2,
238 static struct usb_config_descriptor
239 source_sink_config
= {
240 .bLength
= sizeof source_sink_config
,
241 .bDescriptorType
= USB_DT_CONFIG
,
243 /* compute wTotalLength on the fly */
245 .bConfigurationValue
= CONFIG_SOURCE_SINK
,
246 .iConfiguration
= STRING_SOURCE_SINK
,
247 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
248 .bMaxPower
= 1, /* self-powered */
251 static struct usb_config_descriptor
253 .bLength
= sizeof loopback_config
,
254 .bDescriptorType
= USB_DT_CONFIG
,
256 /* compute wTotalLength on the fly */
258 .bConfigurationValue
= CONFIG_LOOPBACK
,
259 .iConfiguration
= STRING_LOOPBACK
,
260 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
261 .bMaxPower
= 1, /* self-powered */
264 static struct usb_otg_descriptor
266 .bLength
= sizeof otg_descriptor
,
267 .bDescriptorType
= USB_DT_OTG
,
269 .bmAttributes
= USB_OTG_SRP
,
272 /* one interface in each configuration */
274 static const struct usb_interface_descriptor
276 .bLength
= sizeof source_sink_intf
,
277 .bDescriptorType
= USB_DT_INTERFACE
,
280 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
281 .iInterface
= STRING_SOURCE_SINK
,
284 static const struct usb_interface_descriptor
286 .bLength
= sizeof loopback_intf
,
287 .bDescriptorType
= USB_DT_INTERFACE
,
290 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
291 .iInterface
= STRING_LOOPBACK
,
294 /* two full speed bulk endpoints; their use is config-dependent */
296 static struct usb_endpoint_descriptor
298 .bLength
= USB_DT_ENDPOINT_SIZE
,
299 .bDescriptorType
= USB_DT_ENDPOINT
,
301 .bEndpointAddress
= USB_DIR_IN
,
302 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
305 static struct usb_endpoint_descriptor
307 .bLength
= USB_DT_ENDPOINT_SIZE
,
308 .bDescriptorType
= USB_DT_ENDPOINT
,
310 .bEndpointAddress
= USB_DIR_OUT
,
311 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
314 static const struct usb_descriptor_header
*fs_source_sink_function
[] = {
315 (struct usb_descriptor_header
*) &otg_descriptor
,
316 (struct usb_descriptor_header
*) &source_sink_intf
,
317 (struct usb_descriptor_header
*) &fs_sink_desc
,
318 (struct usb_descriptor_header
*) &fs_source_desc
,
322 static const struct usb_descriptor_header
*fs_loopback_function
[] = {
323 (struct usb_descriptor_header
*) &otg_descriptor
,
324 (struct usb_descriptor_header
*) &loopback_intf
,
325 (struct usb_descriptor_header
*) &fs_sink_desc
,
326 (struct usb_descriptor_header
*) &fs_source_desc
,
330 #ifdef CONFIG_USB_GADGET_DUALSPEED
333 * usb 2.0 devices need to expose both high speed and full speed
334 * descriptors, unless they only run at full speed.
336 * that means alternate endpoint descriptors (bigger packets)
337 * and a "device qualifier" ... plus more construction options
338 * for the config descriptor.
341 static struct usb_endpoint_descriptor
343 .bLength
= USB_DT_ENDPOINT_SIZE
,
344 .bDescriptorType
= USB_DT_ENDPOINT
,
346 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
347 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
350 static struct usb_endpoint_descriptor
352 .bLength
= USB_DT_ENDPOINT_SIZE
,
353 .bDescriptorType
= USB_DT_ENDPOINT
,
355 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
356 .wMaxPacketSize
= __constant_cpu_to_le16 (512),
359 static struct usb_qualifier_descriptor
361 .bLength
= sizeof dev_qualifier
,
362 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
364 .bcdUSB
= __constant_cpu_to_le16 (0x0200),
365 .bDeviceClass
= USB_CLASS_VENDOR_SPEC
,
367 .bNumConfigurations
= 2,
370 static const struct usb_descriptor_header
*hs_source_sink_function
[] = {
371 (struct usb_descriptor_header
*) &otg_descriptor
,
372 (struct usb_descriptor_header
*) &source_sink_intf
,
373 (struct usb_descriptor_header
*) &hs_source_desc
,
374 (struct usb_descriptor_header
*) &hs_sink_desc
,
378 static const struct usb_descriptor_header
*hs_loopback_function
[] = {
379 (struct usb_descriptor_header
*) &otg_descriptor
,
380 (struct usb_descriptor_header
*) &loopback_intf
,
381 (struct usb_descriptor_header
*) &hs_source_desc
,
382 (struct usb_descriptor_header
*) &hs_sink_desc
,
386 /* maxpacket and other transfer characteristics vary by speed. */
387 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
391 /* if there's no high speed support, maxpacket doesn't change. */
392 #define ep_desc(g,hs,fs) fs
394 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
396 static char manufacturer
[50];
397 static char serial
[40];
399 /* static strings, in UTF-8 */
400 static struct usb_string strings
[] = {
401 { STRING_MANUFACTURER
, manufacturer
, },
402 { STRING_PRODUCT
, longname
, },
403 { STRING_SERIAL
, serial
, },
404 { STRING_LOOPBACK
, loopback
, },
405 { STRING_SOURCE_SINK
, source_sink
, },
406 { } /* end of list */
409 static struct usb_gadget_strings stringtab
= {
410 .language
= 0x0409, /* en-us */
415 * config descriptors are also handcrafted. these must agree with code
416 * that sets configurations, and with code managing interfaces and their
417 * altsettings. other complexity may come from:
419 * - high speed support, including "other speed config" rules
420 * - multiple configurations
421 * - interfaces with alternate settings
422 * - embedded class or vendor-specific descriptors
424 * this handles high speed, and has a second config that could as easily
425 * have been an alternate interface setting (on most hardware).
427 * NOTE: to demonstrate (and test) more USB capabilities, this driver
428 * should include an altsetting to test interrupt transfers, including
429 * high bandwidth modes at high speed. (Maybe work like Intel's test
433 config_buf (struct usb_gadget
*gadget
,
434 u8
*buf
, u8 type
, unsigned index
)
438 const struct usb_descriptor_header
**function
;
439 #ifdef CONFIG_USB_GADGET_DUALSPEED
440 int hs
= (gadget
->speed
== USB_SPEED_HIGH
);
443 /* two configurations will always be index 0 and index 1 */
446 is_source_sink
= loopdefault
? (index
== 1) : (index
== 0);
448 #ifdef CONFIG_USB_GADGET_DUALSPEED
449 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
452 function
= is_source_sink
453 ? hs_source_sink_function
454 : hs_loopback_function
;
457 function
= is_source_sink
458 ? fs_source_sink_function
459 : fs_loopback_function
;
461 /* for now, don't advertise srp-only devices */
465 len
= usb_gadget_config_buf (is_source_sink
466 ? &source_sink_config
468 buf
, USB_BUFSIZ
, function
);
471 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
475 /*-------------------------------------------------------------------------*/
477 static struct usb_request
*
478 alloc_ep_req (struct usb_ep
*ep
, unsigned length
)
480 struct usb_request
*req
;
482 req
= usb_ep_alloc_request (ep
, GFP_ATOMIC
);
484 req
->length
= length
;
485 req
->buf
= usb_ep_alloc_buffer (ep
, length
,
486 &req
->dma
, GFP_ATOMIC
);
488 usb_ep_free_request (ep
, req
);
495 static void free_ep_req (struct usb_ep
*ep
, struct usb_request
*req
)
498 usb_ep_free_buffer (ep
, req
->buf
, req
->dma
, req
->length
);
499 usb_ep_free_request (ep
, req
);
502 /*-------------------------------------------------------------------------*/
504 /* optionally require specific source/sink data patterns */
508 struct zero_dev
*dev
,
510 struct usb_request
*req
516 for (i
= 0; i
< req
->actual
; i
++, buf
++) {
518 /* all-zeroes has no synchronization issues */
523 /* mod63 stays in sync with short-terminated transfers,
524 * or otherwise when host and gadget agree on how large
525 * each usb transfer request should be. resync is done
526 * with set_interface or set_config.
529 if (*buf
== (u8
)(i
% 63))
533 ERROR (dev
, "bad OUT byte, buf [%d] = %d\n", i
, *buf
);
534 usb_ep_set_halt (ep
);
542 struct zero_dev
*dev
,
544 struct usb_request
*req
552 memset (req
->buf
, 0, req
->length
);
555 for (i
= 0; i
< req
->length
; i
++)
556 *buf
++ = (u8
) (i
% 63);
561 /* if there is only one request in the queue, there'll always be an
562 * irq delay between end of one request and start of the next.
563 * that prevents using hardware dma queues.
565 static void source_sink_complete (struct usb_ep
*ep
, struct usb_request
*req
)
567 struct zero_dev
*dev
= ep
->driver_data
;
568 int status
= req
->status
;
572 case 0: /* normal completion? */
573 if (ep
== dev
->out_ep
) {
574 check_read_data (dev
, ep
, req
);
575 memset (req
->buf
, 0x55, req
->length
);
577 reinit_write_data (dev
, ep
, req
);
580 /* this endpoint is normally active while we're configured */
581 case -ECONNABORTED
: /* hardware forced ep reset */
582 case -ECONNRESET
: /* request dequeued */
583 case -ESHUTDOWN
: /* disconnect from host */
584 VDBG (dev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
585 req
->actual
, req
->length
);
586 if (ep
== dev
->out_ep
)
587 check_read_data (dev
, ep
, req
);
588 free_ep_req (ep
, req
);
591 case -EOVERFLOW
: /* buffer overrun on read means that
592 * we didn't provide a big enough
597 DBG (dev
, "%s complete --> %d, %d/%d\n", ep
->name
,
598 status
, req
->actual
, req
->length
);
600 case -EREMOTEIO
: /* short read */
604 status
= usb_ep_queue (ep
, req
, GFP_ATOMIC
);
606 ERROR (dev
, "kill %s: resubmit %d bytes --> %d\n",
607 ep
->name
, req
->length
, status
);
608 usb_ep_set_halt (ep
);
609 /* FIXME recover later ... somehow */
613 static struct usb_request
*
614 source_sink_start_ep (struct usb_ep
*ep
, gfp_t gfp_flags
)
616 struct usb_request
*req
;
619 req
= alloc_ep_req (ep
, buflen
);
623 memset (req
->buf
, 0, req
->length
);
624 req
->complete
= source_sink_complete
;
626 if (strcmp (ep
->name
, EP_IN_NAME
) == 0)
627 reinit_write_data (ep
->driver_data
, ep
, req
);
629 memset (req
->buf
, 0x55, req
->length
);
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
, gfp_t 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
, gfp_t 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
, gfp_t 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
;
923 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
924 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
925 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
927 /* usually this stores reply data in the pre-allocated ep0 buffer,
928 * but config change events will reconfigure hardware.
931 switch (ctrl
->bRequest
) {
933 case USB_REQ_GET_DESCRIPTOR
:
934 if (ctrl
->bRequestType
!= USB_DIR_IN
)
936 switch (w_value
>> 8) {
939 value
= min (w_length
, (u16
) sizeof device_desc
);
940 memcpy (req
->buf
, &device_desc
, value
);
942 #ifdef CONFIG_USB_GADGET_DUALSPEED
943 case USB_DT_DEVICE_QUALIFIER
:
944 if (!gadget
->is_dualspeed
)
946 value
= min (w_length
, (u16
) sizeof dev_qualifier
);
947 memcpy (req
->buf
, &dev_qualifier
, value
);
950 case USB_DT_OTHER_SPEED_CONFIG
:
951 if (!gadget
->is_dualspeed
)
954 #endif /* CONFIG_USB_GADGET_DUALSPEED */
956 value
= config_buf (gadget
, req
->buf
,
960 value
= min (w_length
, (u16
) value
);
964 /* wIndex == language code.
965 * this driver only handles one language, you can
966 * add string tables for other languages, using
967 * any UTF-8 characters
969 value
= usb_gadget_get_string (&stringtab
,
970 w_value
& 0xff, req
->buf
);
972 value
= min (w_length
, (u16
) value
);
977 /* currently two configs, two speeds */
978 case USB_REQ_SET_CONFIGURATION
:
979 if (ctrl
->bRequestType
!= 0)
981 if (gadget
->a_hnp_support
)
982 DBG (dev
, "HNP available\n");
983 else if (gadget
->a_alt_hnp_support
)
984 DBG (dev
, "HNP needs a different root port\n");
986 VDBG (dev
, "HNP inactive\n");
987 spin_lock (&dev
->lock
);
988 value
= zero_set_config (dev
, w_value
, GFP_ATOMIC
);
989 spin_unlock (&dev
->lock
);
991 case USB_REQ_GET_CONFIGURATION
:
992 if (ctrl
->bRequestType
!= USB_DIR_IN
)
994 *(u8
*)req
->buf
= dev
->config
;
995 value
= min (w_length
, (u16
) 1);
998 /* until we add altsetting support, or other interfaces,
999 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1000 * and already killed pending endpoint I/O.
1002 case USB_REQ_SET_INTERFACE
:
1003 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1005 spin_lock (&dev
->lock
);
1006 if (dev
->config
&& w_index
== 0 && w_value
== 0) {
1007 u8 config
= dev
->config
;
1009 /* resets interface configuration, forgets about
1010 * previous transaction state (queued bufs, etc)
1011 * and re-inits endpoint state (toggle etc)
1012 * no response queued, just zero status == success.
1013 * if we had more than one interface we couldn't
1014 * use this "reset the config" shortcut.
1016 zero_reset_config (dev
);
1017 zero_set_config (dev
, config
, GFP_ATOMIC
);
1020 spin_unlock (&dev
->lock
);
1022 case USB_REQ_GET_INTERFACE
:
1023 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1031 *(u8
*)req
->buf
= 0;
1032 value
= min (w_length
, (u16
) 1);
1036 * These are the same vendor-specific requests supported by
1037 * Intel's USB 2.0 compliance test devices. We exceed that
1038 * device spec by allowing multiple-packet requests.
1040 case 0x5b: /* control WRITE test -- fill the buffer */
1041 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|USB_TYPE_VENDOR
))
1043 if (w_value
|| w_index
)
1045 /* just read that many bytes into the buffer */
1046 if (w_length
> USB_BUFSIZ
)
1050 case 0x5c: /* control READ test -- return the buffer */
1051 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_TYPE_VENDOR
))
1053 if (w_value
|| w_index
)
1055 /* expect those bytes are still in the buffer; send back */
1056 if (w_length
> USB_BUFSIZ
1057 || w_length
!= req
->length
)
1065 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1066 ctrl
->bRequestType
, ctrl
->bRequest
,
1067 w_value
, w_index
, w_length
);
1070 /* respond with data transfer before status phase? */
1072 req
->length
= value
;
1073 req
->zero
= value
< w_length
;
1074 value
= usb_ep_queue (gadget
->ep0
, req
, GFP_ATOMIC
);
1076 DBG (dev
, "ep_queue --> %d\n", value
);
1078 zero_setup_complete (gadget
->ep0
, req
);
1082 /* device either stalls (value < 0) or reports success */
1087 zero_disconnect (struct usb_gadget
*gadget
)
1089 struct zero_dev
*dev
= get_gadget_data (gadget
);
1090 unsigned long flags
;
1092 spin_lock_irqsave (&dev
->lock
, flags
);
1093 zero_reset_config (dev
);
1095 /* a more significant application might have some non-usb
1096 * activities to quiesce here, saving resources like power
1097 * or pushing the notification up a network stack.
1099 spin_unlock_irqrestore (&dev
->lock
, flags
);
1101 /* next we may get setup() calls to enumerate new connections;
1102 * or an unbind() during shutdown (including removing module).
1107 zero_autoresume (unsigned long _dev
)
1109 struct zero_dev
*dev
= (struct zero_dev
*) _dev
;
1112 /* normally the host would be woken up for something
1113 * more significant than just a timer firing...
1115 if (dev
->gadget
->speed
!= USB_SPEED_UNKNOWN
) {
1116 status
= usb_gadget_wakeup (dev
->gadget
);
1117 DBG (dev
, "wakeup --> %d\n", status
);
1121 /*-------------------------------------------------------------------------*/
1123 static void /* __init_or_exit */
1124 zero_unbind (struct usb_gadget
*gadget
)
1126 struct zero_dev
*dev
= get_gadget_data (gadget
);
1128 DBG (dev
, "unbind\n");
1130 /* we've already been disconnected ... no i/o is active */
1132 dev
->req
->length
= USB_BUFSIZ
;
1133 free_ep_req (gadget
->ep0
, dev
->req
);
1135 del_timer_sync (&dev
->resume
);
1137 set_gadget_data (gadget
, NULL
);
1141 zero_bind (struct usb_gadget
*gadget
)
1143 struct zero_dev
*dev
;
1147 /* FIXME this can't yet work right with SH ... it has only
1148 * one configuration, numbered one.
1150 if (gadget_is_sh(gadget
))
1153 /* Bulk-only drivers like this one SHOULD be able to
1154 * autoconfigure on any sane usb controller driver,
1155 * but there may also be important quirks to address.
1157 usb_ep_autoconfig_reset (gadget
);
1158 ep
= usb_ep_autoconfig (gadget
, &fs_source_desc
);
1161 printk (KERN_ERR
"%s: can't autoconfigure on %s\n",
1162 shortname
, gadget
->name
);
1165 EP_IN_NAME
= ep
->name
;
1166 ep
->driver_data
= ep
; /* claim */
1168 ep
= usb_ep_autoconfig (gadget
, &fs_sink_desc
);
1171 EP_OUT_NAME
= ep
->name
;
1172 ep
->driver_data
= ep
; /* claim */
1174 gcnum
= usb_gadget_controller_number (gadget
);
1176 device_desc
.bcdDevice
= cpu_to_le16 (0x0200 + gcnum
);
1178 /* gadget zero is so simple (for now, no altsettings) that
1179 * it SHOULD NOT have problems with bulk-capable hardware.
1180 * so warn about unrcognized controllers, don't panic.
1182 * things like configuration and altsetting numbering
1183 * can need hardware-specific attention though.
1185 printk (KERN_WARNING
"%s: controller '%s' not recognized\n",
1186 shortname
, gadget
->name
);
1187 device_desc
.bcdDevice
= __constant_cpu_to_le16 (0x9999);
1191 /* ok, we made sense of the hardware ... */
1192 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1195 spin_lock_init (&dev
->lock
);
1196 dev
->gadget
= gadget
;
1197 set_gadget_data (gadget
, dev
);
1199 /* preallocate control response and buffer */
1200 dev
->req
= usb_ep_alloc_request (gadget
->ep0
, GFP_KERNEL
);
1203 dev
->req
->buf
= usb_ep_alloc_buffer (gadget
->ep0
, USB_BUFSIZ
,
1204 &dev
->req
->dma
, GFP_KERNEL
);
1208 dev
->req
->complete
= zero_setup_complete
;
1210 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
1212 #ifdef CONFIG_USB_GADGET_DUALSPEED
1213 /* assume ep0 uses the same value for both speeds ... */
1214 dev_qualifier
.bMaxPacketSize0
= device_desc
.bMaxPacketSize0
;
1216 /* and that all endpoints are dual-speed */
1217 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
1218 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
1221 if (gadget
->is_otg
) {
1222 otg_descriptor
.bmAttributes
|= USB_OTG_HNP
,
1223 source_sink_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1224 loopback_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1227 usb_gadget_set_selfpowered (gadget
);
1229 init_timer (&dev
->resume
);
1230 dev
->resume
.function
= zero_autoresume
;
1231 dev
->resume
.data
= (unsigned long) dev
;
1233 source_sink_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1234 loopback_config
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
1237 gadget
->ep0
->driver_data
= dev
;
1239 INFO (dev
, "%s, version: " DRIVER_VERSION
"\n", longname
);
1240 INFO (dev
, "using %s, OUT %s IN %s\n", gadget
->name
,
1241 EP_OUT_NAME
, EP_IN_NAME
);
1243 snprintf (manufacturer
, sizeof manufacturer
, "%s %s with %s",
1244 init_utsname()->sysname
, init_utsname()->release
,
1250 zero_unbind (gadget
);
1254 /*-------------------------------------------------------------------------*/
1257 zero_suspend (struct usb_gadget
*gadget
)
1259 struct zero_dev
*dev
= get_gadget_data (gadget
);
1261 if (gadget
->speed
== USB_SPEED_UNKNOWN
)
1265 mod_timer (&dev
->resume
, jiffies
+ (HZ
* autoresume
));
1266 DBG (dev
, "suspend, wakeup in %d seconds\n", autoresume
);
1268 DBG (dev
, "suspend\n");
1272 zero_resume (struct usb_gadget
*gadget
)
1274 struct zero_dev
*dev
= get_gadget_data (gadget
);
1276 DBG (dev
, "resume\n");
1277 del_timer (&dev
->resume
);
1281 /*-------------------------------------------------------------------------*/
1283 static struct usb_gadget_driver zero_driver
= {
1284 #ifdef CONFIG_USB_GADGET_DUALSPEED
1285 .speed
= USB_SPEED_HIGH
,
1287 .speed
= USB_SPEED_FULL
,
1289 .function
= (char *) longname
,
1291 .unbind
= __exit_p(zero_unbind
),
1293 .setup
= zero_setup
,
1294 .disconnect
= zero_disconnect
,
1296 .suspend
= zero_suspend
,
1297 .resume
= zero_resume
,
1300 .name
= (char *) shortname
,
1301 .owner
= THIS_MODULE
,
1305 MODULE_AUTHOR ("David Brownell");
1306 MODULE_LICENSE ("Dual BSD/GPL");
1309 static int __init
init (void)
1311 /* a real value would likely come through some id prom
1312 * or module option. this one takes at least two packets.
1314 strlcpy (serial
, "0123456789.0123456789.0123456789", sizeof serial
);
1316 return usb_gadget_register_driver (&zero_driver
);
1320 static void __exit
cleanup (void)
1322 usb_gadget_unregister_driver (&zero_driver
);
1324 module_exit (cleanup
);