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/errno.h>
71 #include <linux/init.h>
72 #include <linux/timer.h>
73 #include <linux/list.h>
74 #include <linux/interrupt.h>
75 #include <linux/utsname.h>
76 #include <linux/device.h>
77 #include <linux/moduleparam.h>
79 #include <asm/byteorder.h>
82 #include <asm/system.h>
83 #include <asm/unaligned.h>
85 #include <linux/usb/ch9.h>
86 #include <linux/usb_gadget.h>
88 #include "gadget_chips.h"
91 /*-------------------------------------------------------------------------*/
93 #define DRIVER_VERSION "St Patrick's Day 2004"
95 static const char shortname [] = "zero";
96 static const char longname [] = "Gadget Zero";
98 static const char source_sink [] = "source and sink data";
99 static const char loopback [] = "loop input to output";
101 /*-------------------------------------------------------------------------*/
104 * driver assumes self-powered hardware, and
105 * has no way for users to trigger remote wakeup.
107 * this version autoconfigures as much as possible,
108 * which is reasonable for most "bulk-only" drivers.
110 static const char *EP_IN_NAME; /* source */
111 static const char *EP_OUT_NAME; /* sink */
113 /*-------------------------------------------------------------------------*/
115 /* big enough to hold our biggest descriptor */
116 #define USB_BUFSIZ 256
120 struct usb_gadget *gadget;
121 struct usb_request *req; /* for control responses */
123 /* when configured, we have one of two configs:
124 * - source data (in to host) and sink it (out from host)
125 * - or loop it back (out from host back in to host)
128 struct usb_ep *in_ep, *out_ep;
130 /* autoresume timer */
131 struct timer_list resume;
134 #define xprintk(d,level,fmt,args...) \
135 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
138 #define DBG(dev,fmt,args...) \
139 xprintk(dev , KERN_DEBUG , fmt , ## args)
141 #define DBG(dev,fmt,args...) \
148 #define VDBG(dev,fmt,args...) \
152 #define ERROR(dev,fmt,args...) \
153 xprintk(dev , KERN_ERR , fmt , ## args)
154 #define WARN(dev,fmt,args...) \
155 xprintk(dev , KERN_WARNING , fmt , ## args)
156 #define INFO(dev,fmt,args...) \
157 xprintk(dev , KERN_INFO , fmt , ## args)
159 /*-------------------------------------------------------------------------*/
161 static unsigned buflen = 4096;
162 static unsigned qlen = 32;
163 static unsigned pattern = 0;
165 module_param (buflen, uint, S_IRUGO);
166 module_param (qlen, uint, S_IRUGO);
167 module_param (pattern, uint, S_IRUGO|S_IWUSR);
170 * if it's nonzero, autoresume says how many seconds to wait
171 * before trying to wake up the host after suspend.
173 static unsigned autoresume = 0;
174 module_param (autoresume, uint, 0);
177 * Normally the "loopback" configuration is second (index 1) so
178 * it's not the default. Here's where to change that order, to
179 * work better with hosts where config changes are problematic.
180 * Or controllers (like superh) that only support one config.
182 static int loopdefault = 0;
184 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
186 /*-------------------------------------------------------------------------*/
188 /* Thanks to NetChip Technologies for donating this product ID.
190 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
191 * Instead: allocate your own, using normal USB-IF procedures.
193 #ifndef CONFIG_USB_ZERO_HNPTEST
194 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
195 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
197 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
198 #define DRIVER_PRODUCT_NUM 0xbadd
201 /*-------------------------------------------------------------------------*/
204 * DESCRIPTORS ... most are static, but strings and (full)
205 * configuration descriptors are built on demand.
208 #define STRING_MANUFACTURER 25
209 #define STRING_PRODUCT 42
210 #define STRING_SERIAL 101
211 #define STRING_SOURCE_SINK 250
212 #define STRING_LOOPBACK 251
215 * This device advertises two configurations; these numbers work
216 * on a pxa250 as well as more flexible hardware.
218 #define CONFIG_SOURCE_SINK 3
219 #define CONFIG_LOOPBACK 2
221 static struct usb_device_descriptor
223 .bLength = sizeof device_desc,
224 .bDescriptorType = USB_DT_DEVICE,
226 .bcdUSB = __constant_cpu_to_le16 (0x0200),
227 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
229 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
230 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
231 .iManufacturer = STRING_MANUFACTURER,
232 .iProduct = STRING_PRODUCT,
233 .iSerialNumber = STRING_SERIAL,
234 .bNumConfigurations = 2,
237 static struct usb_config_descriptor
238 source_sink_config = {
239 .bLength = sizeof source_sink_config,
240 .bDescriptorType = USB_DT_CONFIG,
242 /* compute wTotalLength on the fly */
244 .bConfigurationValue = CONFIG_SOURCE_SINK,
245 .iConfiguration = STRING_SOURCE_SINK,
246 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
247 .bMaxPower = 1, /* self-powered */
250 static struct usb_config_descriptor
252 .bLength = sizeof loopback_config,
253 .bDescriptorType = USB_DT_CONFIG,
255 /* compute wTotalLength on the fly */
257 .bConfigurationValue = CONFIG_LOOPBACK,
258 .iConfiguration = STRING_LOOPBACK,
259 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
260 .bMaxPower = 1, /* self-powered */
263 static struct usb_otg_descriptor
265 .bLength = sizeof otg_descriptor,
266 .bDescriptorType = USB_DT_OTG,
268 .bmAttributes = USB_OTG_SRP,
271 /* one interface in each configuration */
273 static const struct usb_interface_descriptor
275 .bLength = sizeof source_sink_intf,
276 .bDescriptorType = USB_DT_INTERFACE,
279 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
280 .iInterface = STRING_SOURCE_SINK,
283 static const struct usb_interface_descriptor
285 .bLength = sizeof loopback_intf,
286 .bDescriptorType = USB_DT_INTERFACE,
289 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
290 .iInterface = STRING_LOOPBACK,
293 /* two full speed bulk endpoints; their use is config-dependent */
295 static struct usb_endpoint_descriptor
297 .bLength = USB_DT_ENDPOINT_SIZE,
298 .bDescriptorType = USB_DT_ENDPOINT,
300 .bEndpointAddress = USB_DIR_IN,
301 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 static struct usb_endpoint_descriptor
306 .bLength = USB_DT_ENDPOINT_SIZE,
307 .bDescriptorType = USB_DT_ENDPOINT,
309 .bEndpointAddress = USB_DIR_OUT,
310 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 static const struct usb_descriptor_header *fs_source_sink_function [] = {
314 (struct usb_descriptor_header *) &otg_descriptor,
315 (struct usb_descriptor_header *) &source_sink_intf,
316 (struct usb_descriptor_header *) &fs_sink_desc,
317 (struct usb_descriptor_header *) &fs_source_desc,
321 static const struct usb_descriptor_header *fs_loopback_function [] = {
322 (struct usb_descriptor_header *) &otg_descriptor,
323 (struct usb_descriptor_header *) &loopback_intf,
324 (struct usb_descriptor_header *) &fs_sink_desc,
325 (struct usb_descriptor_header *) &fs_source_desc,
329 #ifdef CONFIG_USB_GADGET_DUALSPEED
332 * usb 2.0 devices need to expose both high speed and full speed
333 * descriptors, unless they only run at full speed.
335 * that means alternate endpoint descriptors (bigger packets)
336 * and a "device qualifier" ... plus more construction options
337 * for the config descriptor.
340 static struct usb_endpoint_descriptor
342 .bLength = USB_DT_ENDPOINT_SIZE,
343 .bDescriptorType = USB_DT_ENDPOINT,
345 .bmAttributes = USB_ENDPOINT_XFER_BULK,
346 .wMaxPacketSize = __constant_cpu_to_le16 (512),
349 static struct usb_endpoint_descriptor
351 .bLength = USB_DT_ENDPOINT_SIZE,
352 .bDescriptorType = USB_DT_ENDPOINT,
354 .bmAttributes = USB_ENDPOINT_XFER_BULK,
355 .wMaxPacketSize = __constant_cpu_to_le16 (512),
358 static struct usb_qualifier_descriptor
360 .bLength = sizeof dev_qualifier,
361 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
363 .bcdUSB = __constant_cpu_to_le16 (0x0200),
364 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
366 .bNumConfigurations = 2,
369 static const struct usb_descriptor_header *hs_source_sink_function [] = {
370 (struct usb_descriptor_header *) &otg_descriptor,
371 (struct usb_descriptor_header *) &source_sink_intf,
372 (struct usb_descriptor_header *) &hs_source_desc,
373 (struct usb_descriptor_header *) &hs_sink_desc,
377 static const struct usb_descriptor_header *hs_loopback_function [] = {
378 (struct usb_descriptor_header *) &otg_descriptor,
379 (struct usb_descriptor_header *) &loopback_intf,
380 (struct usb_descriptor_header *) &hs_source_desc,
381 (struct usb_descriptor_header *) &hs_sink_desc,
385 /* maxpacket and other transfer characteristics vary by speed. */
386 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
390 /* if there's no high speed support, maxpacket doesn't change. */
391 #define ep_desc(g,hs,fs) fs
393 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
395 static char manufacturer [50];
396 static char serial [40];
398 /* static strings, in UTF-8 */
399 static struct usb_string strings [] = {
400 { STRING_MANUFACTURER, manufacturer, },
401 { STRING_PRODUCT, longname, },
402 { STRING_SERIAL, serial, },
403 { STRING_LOOPBACK, loopback, },
404 { STRING_SOURCE_SINK, source_sink, },
405 { } /* end of list */
408 static struct usb_gadget_strings stringtab = {
409 .language = 0x0409, /* en-us */
414 * config descriptors are also handcrafted. these must agree with code
415 * that sets configurations, and with code managing interfaces and their
416 * altsettings. other complexity may come from:
418 * - high speed support, including "other speed config" rules
419 * - multiple configurations
420 * - interfaces with alternate settings
421 * - embedded class or vendor-specific descriptors
423 * this handles high speed, and has a second config that could as easily
424 * have been an alternate interface setting (on most hardware).
426 * NOTE: to demonstrate (and test) more USB capabilities, this driver
427 * should include an altsetting to test interrupt transfers, including
428 * high bandwidth modes at high speed. (Maybe work like Intel's test
432 config_buf (struct usb_gadget *gadget,
433 u8 *buf, u8 type, unsigned index)
437 const struct usb_descriptor_header **function;
438 #ifdef CONFIG_USB_GADGET_DUALSPEED
439 int hs = (gadget->speed == USB_SPEED_HIGH);
442 /* two configurations will always be index 0 and index 1 */
445 is_source_sink = loopdefault ? (index == 1) : (index == 0);
447 #ifdef CONFIG_USB_GADGET_DUALSPEED
448 if (type == USB_DT_OTHER_SPEED_CONFIG)
451 function = is_source_sink
452 ? hs_source_sink_function
453 : hs_loopback_function;
456 function = is_source_sink
457 ? fs_source_sink_function
458 : fs_loopback_function;
460 /* for now, don't advertise srp-only devices */
464 len = usb_gadget_config_buf (is_source_sink
465 ? &source_sink_config
467 buf, USB_BUFSIZ, function);
470 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
474 /*-------------------------------------------------------------------------*/
476 static struct usb_request *
477 alloc_ep_req (struct usb_ep *ep, unsigned length)
479 struct usb_request *req;
481 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
483 req->length = length;
484 req->buf = kmalloc(length, GFP_ATOMIC);
486 usb_ep_free_request (ep, req);
493 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
496 usb_ep_free_request (ep, req);
499 /*-------------------------------------------------------------------------*/
501 /* optionally require specific source/sink data patterns */
505 struct zero_dev *dev,
507 struct usb_request *req
513 for (i = 0; i < req->actual; i++, buf++) {
515 /* all-zeroes has no synchronization issues */
520 /* mod63 stays in sync with short-terminated transfers,
521 * or otherwise when host and gadget agree on how large
522 * each usb transfer request should be. resync is done
523 * with set_interface or set_config.
526 if (*buf == (u8)(i % 63))
530 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
531 usb_ep_set_halt (ep);
539 struct zero_dev *dev,
541 struct usb_request *req
549 memset (req->buf, 0, req->length);
552 for (i = 0; i < req->length; i++)
553 *buf++ = (u8) (i % 63);
558 /* if there is only one request in the queue, there'll always be an
559 * irq delay between end of one request and start of the next.
560 * that prevents using hardware dma queues.
562 static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
564 struct zero_dev *dev = ep->driver_data;
565 int status = req->status;
569 case 0: /* normal completion? */
570 if (ep == dev->out_ep) {
571 check_read_data (dev, ep, req);
572 memset (req->buf, 0x55, req->length);
574 reinit_write_data (dev, ep, req);
577 /* this endpoint is normally active while we're configured */
578 case -ECONNABORTED: /* hardware forced ep reset */
579 case -ECONNRESET: /* request dequeued */
580 case -ESHUTDOWN: /* disconnect from host */
581 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
582 req->actual, req->length);
583 if (ep == dev->out_ep)
584 check_read_data (dev, ep, req);
585 free_ep_req (ep, req);
588 case -EOVERFLOW: /* buffer overrun on read means that
589 * we didn't provide a big enough
594 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
595 status, req->actual, req->length);
597 case -EREMOTEIO: /* short read */
601 status = usb_ep_queue (ep, req, GFP_ATOMIC);
603 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
604 ep->name, req->length, status);
605 usb_ep_set_halt (ep);
606 /* FIXME recover later ... somehow */
610 static struct usb_request *
611 source_sink_start_ep (struct usb_ep *ep, gfp_t gfp_flags)
613 struct usb_request *req;
616 req = alloc_ep_req (ep, buflen);
620 memset (req->buf, 0, req->length);
621 req->complete = source_sink_complete;
623 if (strcmp (ep->name, EP_IN_NAME) == 0)
624 reinit_write_data (ep->driver_data, ep, req);
626 memset (req->buf, 0x55, req->length);
628 status = usb_ep_queue (ep, req, gfp_flags);
630 struct zero_dev *dev = ep->driver_data;
632 ERROR (dev, "start %s --> %d\n", ep->name, status);
633 free_ep_req (ep, req);
641 set_source_sink_config (struct zero_dev *dev, gfp_t gfp_flags)
645 struct usb_gadget *gadget = dev->gadget;
647 gadget_for_each_ep (ep, gadget) {
648 const struct usb_endpoint_descriptor *d;
650 /* one endpoint writes (sources) zeroes in (to the host) */
651 if (strcmp (ep->name, EP_IN_NAME) == 0) {
652 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
653 result = usb_ep_enable (ep, d);
655 ep->driver_data = dev;
656 if (source_sink_start_ep (ep, gfp_flags) != 0) {
664 /* one endpoint reads (sinks) anything out (from the host) */
665 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
666 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
667 result = usb_ep_enable (ep, d);
669 ep->driver_data = dev;
670 if (source_sink_start_ep (ep, gfp_flags) != 0) {
678 /* ignore any other endpoints */
683 ERROR (dev, "can't start %s, result %d\n", ep->name, result);
687 DBG (dev, "buflen %d\n", buflen);
689 /* caller is responsible for cleanup on error */
693 /*-------------------------------------------------------------------------*/
695 static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
697 struct zero_dev *dev = ep->driver_data;
698 int status = req->status;
702 case 0: /* normal completion? */
703 if (ep == dev->out_ep) {
704 /* loop this OUT packet back IN to the host */
705 req->zero = (req->actual < req->length);
706 req->length = req->actual;
707 status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
711 /* "should never get here" */
712 ERROR (dev, "can't loop %s to %s: %d\n",
713 ep->name, dev->in_ep->name,
717 /* queue the buffer for some later OUT packet */
718 req->length = buflen;
719 status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
723 /* "should never get here" */
727 ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
728 status, req->actual, req->length);
731 /* NOTE: since this driver doesn't maintain an explicit record
732 * of requests it submitted (just maintains qlen count), we
733 * rely on the hardware driver to clean up on disconnect or
736 case -ECONNABORTED: /* hardware forced ep reset */
737 case -ECONNRESET: /* request dequeued */
738 case -ESHUTDOWN: /* disconnect from host */
739 free_ep_req (ep, req);
745 set_loopback_config (struct zero_dev *dev, gfp_t gfp_flags)
749 struct usb_gadget *gadget = dev->gadget;
751 gadget_for_each_ep (ep, gadget) {
752 const struct usb_endpoint_descriptor *d;
754 /* one endpoint writes data back IN to the host */
755 if (strcmp (ep->name, EP_IN_NAME) == 0) {
756 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
757 result = usb_ep_enable (ep, d);
759 ep->driver_data = dev;
764 /* one endpoint just reads OUT packets */
765 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
766 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
767 result = usb_ep_enable (ep, d);
769 ep->driver_data = dev;
774 /* ignore any other endpoints */
779 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
783 /* allocate a bunch of read buffers and queue them all at once.
784 * we buffer at most 'qlen' transfers; fewer if any need more
785 * than 'buflen' bytes each.
788 struct usb_request *req;
792 for (i = 0; i < qlen && result == 0; i++) {
793 req = alloc_ep_req (ep, buflen);
795 req->complete = loopback_complete;
796 result = usb_ep_queue (ep, req, GFP_ATOMIC);
798 DBG (dev, "%s queue req --> %d\n",
805 DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
807 /* caller is responsible for cleanup on error */
811 /*-------------------------------------------------------------------------*/
813 static void zero_reset_config (struct zero_dev *dev)
815 if (dev->config == 0)
818 DBG (dev, "reset config\n");
820 /* just disable endpoints, forcing completion of pending i/o.
821 * all our completion handlers free their requests in this case.
824 usb_ep_disable (dev->in_ep);
828 usb_ep_disable (dev->out_ep);
832 del_timer (&dev->resume);
835 /* change our operational config. this code must agree with the code
836 * that returns config descriptors, and altsetting code.
838 * it's also responsible for power management interactions. some
839 * configurations might not work with our current power sources.
841 * note that some device controller hardware will constrain what this
842 * code can do, perhaps by disallowing more than one configuration or
843 * by limiting configuration choices (like the pxa2xx).
846 zero_set_config (struct zero_dev *dev, unsigned number, gfp_t gfp_flags)
849 struct usb_gadget *gadget = dev->gadget;
851 if (number == dev->config)
854 if (gadget_is_sa1100 (gadget) && dev->config) {
855 /* tx fifo is full, but we can't clear it...*/
856 INFO (dev, "can't change configurations\n");
859 zero_reset_config (dev);
862 case CONFIG_SOURCE_SINK:
863 result = set_source_sink_config (dev, gfp_flags);
865 case CONFIG_LOOPBACK:
866 result = set_loopback_config (dev, gfp_flags);
875 if (!result && (!dev->in_ep || !dev->out_ep))
878 zero_reset_config (dev);
882 switch (gadget->speed) {
883 case USB_SPEED_LOW: speed = "low"; break;
884 case USB_SPEED_FULL: speed = "full"; break;
885 case USB_SPEED_HIGH: speed = "high"; break;
886 default: speed = "?"; break;
889 dev->config = number;
890 INFO (dev, "%s speed config #%d: %s\n", speed, number,
891 (number == CONFIG_SOURCE_SINK)
892 ? source_sink : loopback);
897 /*-------------------------------------------------------------------------*/
899 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
901 if (req->status || req->actual != req->length)
902 DBG ((struct zero_dev *) ep->driver_data,
903 "setup complete --> %d, %d/%d\n",
904 req->status, req->actual, req->length);
908 * The setup() callback implements all the ep0 functionality that's
909 * not handled lower down, in hardware or the hardware driver (like
910 * device and endpoint feature flags, and their status). It's all
911 * housekeeping for the gadget function we're implementing. Most of
912 * the work is in config-specific setup.
915 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
917 struct zero_dev *dev = get_gadget_data (gadget);
918 struct usb_request *req = dev->req;
919 int value = -EOPNOTSUPP;
920 u16 w_index = le16_to_cpu(ctrl->wIndex);
921 u16 w_value = le16_to_cpu(ctrl->wValue);
922 u16 w_length = le16_to_cpu(ctrl->wLength);
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 (w_value >> 8) {
936 value = min (w_length, (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 (w_length, (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,
957 value = min (w_length, (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 w_value & 0xff, req->buf);
969 value = min (w_length, (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, w_value, 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 (w_length, (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 && w_index == 0 && w_value == 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))
1028 *(u8 *)req->buf = 0;
1029 value = min (w_length, (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 (w_value || w_index)
1042 /* just read that many bytes into the buffer */
1043 if (w_length > USB_BUFSIZ)
1047 case 0x5c: /* control READ test -- return the buffer */
1048 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1050 if (w_value || w_index)
1052 /* expect those bytes are still in the buffer; send back */
1053 if (w_length > USB_BUFSIZ
1054 || w_length != req->length)
1062 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1063 ctrl->bRequestType, ctrl->bRequest,
1064 w_value, w_index, w_length);
1067 /* respond with data transfer before status phase? */
1069 req->length = value;
1070 req->zero = value < w_length;
1071 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1073 DBG (dev, "ep_queue --> %d\n", value);
1075 zero_setup_complete (gadget->ep0, req);
1079 /* device either stalls (value < 0) or reports success */
1084 zero_disconnect (struct usb_gadget *gadget)
1086 struct zero_dev *dev = get_gadget_data (gadget);
1087 unsigned long flags;
1089 spin_lock_irqsave (&dev->lock, flags);
1090 zero_reset_config (dev);
1092 /* a more significant application might have some non-usb
1093 * activities to quiesce here, saving resources like power
1094 * or pushing the notification up a network stack.
1096 spin_unlock_irqrestore (&dev->lock, flags);
1098 /* next we may get setup() calls to enumerate new connections;
1099 * or an unbind() during shutdown (including removing module).
1104 zero_autoresume (unsigned long _dev)
1106 struct zero_dev *dev = (struct zero_dev *) _dev;
1109 /* normally the host would be woken up for something
1110 * more significant than just a timer firing...
1112 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1113 status = usb_gadget_wakeup (dev->gadget);
1114 DBG (dev, "wakeup --> %d\n", status);
1118 /*-------------------------------------------------------------------------*/
1120 static void /* __init_or_exit */
1121 zero_unbind (struct usb_gadget *gadget)
1123 struct zero_dev *dev = get_gadget_data (gadget);
1125 DBG (dev, "unbind\n");
1127 /* we've already been disconnected ... no i/o is active */
1129 dev->req->length = USB_BUFSIZ;
1130 free_ep_req (gadget->ep0, dev->req);
1132 del_timer_sync (&dev->resume);
1134 set_gadget_data (gadget, NULL);
1138 zero_bind (struct usb_gadget *gadget)
1140 struct zero_dev *dev;
1144 /* FIXME this can't yet work right with SH ... it has only
1145 * one configuration, numbered one.
1147 if (gadget_is_sh(gadget))
1150 /* Bulk-only drivers like this one SHOULD be able to
1151 * autoconfigure on any sane usb controller driver,
1152 * but there may also be important quirks to address.
1154 usb_ep_autoconfig_reset (gadget);
1155 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1158 printk (KERN_ERR "%s: can't autoconfigure on %s\n",
1159 shortname, gadget->name);
1162 EP_IN_NAME = ep->name;
1163 ep->driver_data = ep; /* claim */
1165 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1168 EP_OUT_NAME = ep->name;
1169 ep->driver_data = ep; /* claim */
1171 gcnum = usb_gadget_controller_number (gadget);
1173 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
1175 /* gadget zero is so simple (for now, no altsettings) that
1176 * it SHOULD NOT have problems with bulk-capable hardware.
1177 * so warn about unrcognized controllers, don't panic.
1179 * things like configuration and altsetting numbering
1180 * can need hardware-specific attention though.
1182 printk (KERN_WARNING "%s: controller '%s' not recognized\n",
1183 shortname, gadget->name);
1184 device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
1188 /* ok, we made sense of the hardware ... */
1189 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1192 spin_lock_init (&dev->lock);
1193 dev->gadget = gadget;
1194 set_gadget_data (gadget, dev);
1196 /* preallocate control response and buffer */
1197 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1200 dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1204 dev->req->complete = zero_setup_complete;
1206 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1208 #ifdef CONFIG_USB_GADGET_DUALSPEED
1209 /* assume ep0 uses the same value for both speeds ... */
1210 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1212 /* and that all endpoints are dual-speed */
1213 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1214 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1217 if (gadget->is_otg) {
1218 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1219 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1220 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1223 usb_gadget_set_selfpowered (gadget);
1225 init_timer (&dev->resume);
1226 dev->resume.function = zero_autoresume;
1227 dev->resume.data = (unsigned long) dev;
1229 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1230 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1233 gadget->ep0->driver_data = dev;
1235 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1236 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1237 EP_OUT_NAME, EP_IN_NAME);
1239 snprintf (manufacturer, sizeof manufacturer, "%s %s with %s",
1240 init_utsname()->sysname, init_utsname()->release,
1246 zero_unbind (gadget);
1250 /*-------------------------------------------------------------------------*/
1253 zero_suspend (struct usb_gadget *gadget)
1255 struct zero_dev *dev = get_gadget_data (gadget);
1257 if (gadget->speed == USB_SPEED_UNKNOWN)
1261 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1262 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1264 DBG (dev, "suspend\n");
1268 zero_resume (struct usb_gadget *gadget)
1270 struct zero_dev *dev = get_gadget_data (gadget);
1272 DBG (dev, "resume\n");
1273 del_timer (&dev->resume);
1277 /*-------------------------------------------------------------------------*/
1279 static struct usb_gadget_driver zero_driver = {
1280 #ifdef CONFIG_USB_GADGET_DUALSPEED
1281 .speed = USB_SPEED_HIGH,
1283 .speed = USB_SPEED_FULL,
1285 .function = (char *) longname,
1287 .unbind = __exit_p(zero_unbind),
1289 .setup = zero_setup,
1290 .disconnect = zero_disconnect,
1292 .suspend = zero_suspend,
1293 .resume = zero_resume,
1296 .name = (char *) shortname,
1297 .owner = THIS_MODULE,
1301 MODULE_AUTHOR ("David Brownell");
1302 MODULE_LICENSE ("Dual BSD/GPL");
1305 static int __init init (void)
1307 /* a real value would likely come through some id prom
1308 * or module option. this one takes at least two packets.
1310 strlcpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
1312 return usb_gadget_register_driver (&zero_driver);
1316 static void __exit cleanup (void)
1318 usb_gadget_unregister_driver (&zero_driver);
1320 module_exit (cleanup);