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/sched.h>
70 #include <linux/slab.h>
71 #include <linux/smp_lock.h>
72 #include <linux/errno.h>
73 #include <linux/init.h>
74 #include <linux/timer.h>
75 #include <linux/list.h>
76 #include <linux/interrupt.h>
77 #include <linux/utsname.h>
78 #include <linux/device.h>
79 #include <linux/moduleparam.h>
81 #include <asm/byteorder.h>
84 #include <asm/system.h>
85 #include <asm/unaligned.h>
87 #include <linux/usb_ch9.h>
88 #include <linux/usb_gadget.h>
90 #include "gadget_chips.h"
93 /*-------------------------------------------------------------------------*/
95 #define DRIVER_VERSION "St Patrick's Day 2004"
97 static const char shortname [] = "zero";
98 static const char longname [] = "Gadget Zero";
100 static const char source_sink [] = "source and sink data";
101 static const char loopback [] = "loop input to output";
103 /*-------------------------------------------------------------------------*/
106 * driver assumes self-powered hardware, and
107 * has no way for users to trigger remote wakeup.
109 * this version autoconfigures as much as possible,
110 * which is reasonable for most "bulk-only" drivers.
112 static const char *EP_IN_NAME; /* source */
113 static const char *EP_OUT_NAME; /* sink */
115 /*-------------------------------------------------------------------------*/
117 /* big enough to hold our biggest descriptor */
118 #define USB_BUFSIZ 256
122 struct usb_gadget *gadget;
123 struct usb_request *req; /* for control responses */
125 /* when configured, we have one of two configs:
126 * - source data (in to host) and sink it (out from host)
127 * - or loop it back (out from host back in to host)
130 struct usb_ep *in_ep, *out_ep;
132 /* autoresume timer */
133 struct timer_list resume;
136 #define xprintk(d,level,fmt,args...) \
137 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
140 #define DBG(dev,fmt,args...) \
141 xprintk(dev , KERN_DEBUG , fmt , ## args)
143 #define DBG(dev,fmt,args...) \
150 #define VDBG(dev,fmt,args...) \
154 #define ERROR(dev,fmt,args...) \
155 xprintk(dev , KERN_ERR , fmt , ## args)
156 #define WARN(dev,fmt,args...) \
157 xprintk(dev , KERN_WARNING , fmt , ## args)
158 #define INFO(dev,fmt,args...) \
159 xprintk(dev , KERN_INFO , fmt , ## args)
161 /*-------------------------------------------------------------------------*/
163 static unsigned buflen = 4096;
164 static unsigned qlen = 32;
165 static unsigned pattern = 0;
167 module_param (buflen, uint, S_IRUGO);
168 module_param (qlen, uint, S_IRUGO);
169 module_param (pattern, uint, S_IRUGO|S_IWUSR);
172 * if it's nonzero, autoresume says how many seconds to wait
173 * before trying to wake up the host after suspend.
175 static unsigned autoresume = 0;
176 module_param (autoresume, uint, 0);
179 * Normally the "loopback" configuration is second (index 1) so
180 * it's not the default. Here's where to change that order, to
181 * work better with hosts where config changes are problematic.
182 * Or controllers (like superh) that only support one config.
184 static int loopdefault = 0;
186 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
188 /*-------------------------------------------------------------------------*/
190 /* Thanks to NetChip Technologies for donating this product ID.
192 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
193 * Instead: allocate your own, using normal USB-IF procedures.
195 #ifndef CONFIG_USB_ZERO_HNPTEST
196 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
197 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
199 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
200 #define DRIVER_PRODUCT_NUM 0xbadd
203 /*-------------------------------------------------------------------------*/
206 * DESCRIPTORS ... most are static, but strings and (full)
207 * configuration descriptors are built on demand.
210 #define STRING_MANUFACTURER 25
211 #define STRING_PRODUCT 42
212 #define STRING_SERIAL 101
213 #define STRING_SOURCE_SINK 250
214 #define STRING_LOOPBACK 251
217 * This device advertises two configurations; these numbers work
218 * on a pxa250 as well as more flexible hardware.
220 #define CONFIG_SOURCE_SINK 3
221 #define CONFIG_LOOPBACK 2
223 static struct usb_device_descriptor
225 .bLength = sizeof device_desc,
226 .bDescriptorType = USB_DT_DEVICE,
228 .bcdUSB = __constant_cpu_to_le16 (0x0200),
229 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
231 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
232 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
233 .iManufacturer = STRING_MANUFACTURER,
234 .iProduct = STRING_PRODUCT,
235 .iSerialNumber = STRING_SERIAL,
236 .bNumConfigurations = 2,
239 static struct usb_config_descriptor
240 source_sink_config = {
241 .bLength = sizeof source_sink_config,
242 .bDescriptorType = USB_DT_CONFIG,
244 /* compute wTotalLength on the fly */
246 .bConfigurationValue = CONFIG_SOURCE_SINK,
247 .iConfiguration = STRING_SOURCE_SINK,
248 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
249 .bMaxPower = 1, /* self-powered */
252 static struct usb_config_descriptor
254 .bLength = sizeof loopback_config,
255 .bDescriptorType = USB_DT_CONFIG,
257 /* compute wTotalLength on the fly */
259 .bConfigurationValue = CONFIG_LOOPBACK,
260 .iConfiguration = STRING_LOOPBACK,
261 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
262 .bMaxPower = 1, /* self-powered */
265 static struct usb_otg_descriptor
267 .bLength = sizeof otg_descriptor,
268 .bDescriptorType = USB_DT_OTG,
270 .bmAttributes = USB_OTG_SRP,
273 /* one interface in each configuration */
275 static const struct usb_interface_descriptor
277 .bLength = sizeof source_sink_intf,
278 .bDescriptorType = USB_DT_INTERFACE,
281 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
282 .iInterface = STRING_SOURCE_SINK,
285 static const struct usb_interface_descriptor
287 .bLength = sizeof loopback_intf,
288 .bDescriptorType = USB_DT_INTERFACE,
291 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
292 .iInterface = STRING_LOOPBACK,
295 /* two full speed bulk endpoints; their use is config-dependent */
297 static struct usb_endpoint_descriptor
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
306 static struct usb_endpoint_descriptor
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
311 .bEndpointAddress = USB_DIR_OUT,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
315 static const struct usb_descriptor_header *fs_source_sink_function [] = {
316 (struct usb_descriptor_header *) &otg_descriptor,
317 (struct usb_descriptor_header *) &source_sink_intf,
318 (struct usb_descriptor_header *) &fs_sink_desc,
319 (struct usb_descriptor_header *) &fs_source_desc,
323 static const struct usb_descriptor_header *fs_loopback_function [] = {
324 (struct usb_descriptor_header *) &otg_descriptor,
325 (struct usb_descriptor_header *) &loopback_intf,
326 (struct usb_descriptor_header *) &fs_sink_desc,
327 (struct usb_descriptor_header *) &fs_source_desc,
331 #ifdef CONFIG_USB_GADGET_DUALSPEED
334 * usb 2.0 devices need to expose both high speed and full speed
335 * descriptors, unless they only run at full speed.
337 * that means alternate endpoint descriptors (bigger packets)
338 * and a "device qualifier" ... plus more construction options
339 * for the config descriptor.
342 static struct usb_endpoint_descriptor
344 .bLength = USB_DT_ENDPOINT_SIZE,
345 .bDescriptorType = USB_DT_ENDPOINT,
347 .bmAttributes = USB_ENDPOINT_XFER_BULK,
348 .wMaxPacketSize = __constant_cpu_to_le16 (512),
351 static struct usb_endpoint_descriptor
353 .bLength = USB_DT_ENDPOINT_SIZE,
354 .bDescriptorType = USB_DT_ENDPOINT,
356 .bmAttributes = USB_ENDPOINT_XFER_BULK,
357 .wMaxPacketSize = __constant_cpu_to_le16 (512),
360 static struct usb_qualifier_descriptor
362 .bLength = sizeof dev_qualifier,
363 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
365 .bcdUSB = __constant_cpu_to_le16 (0x0200),
366 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
368 .bNumConfigurations = 2,
371 static const struct usb_descriptor_header *hs_source_sink_function [] = {
372 (struct usb_descriptor_header *) &otg_descriptor,
373 (struct usb_descriptor_header *) &source_sink_intf,
374 (struct usb_descriptor_header *) &hs_source_desc,
375 (struct usb_descriptor_header *) &hs_sink_desc,
379 static const struct usb_descriptor_header *hs_loopback_function [] = {
380 (struct usb_descriptor_header *) &otg_descriptor,
381 (struct usb_descriptor_header *) &loopback_intf,
382 (struct usb_descriptor_header *) &hs_source_desc,
383 (struct usb_descriptor_header *) &hs_sink_desc,
387 /* maxpacket and other transfer characteristics vary by speed. */
388 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
392 /* if there's no high speed support, maxpacket doesn't change. */
393 #define ep_desc(g,hs,fs) fs
395 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
397 static char manufacturer [50];
398 static char serial [40];
400 /* static strings, in UTF-8 */
401 static struct usb_string strings [] = {
402 { STRING_MANUFACTURER, manufacturer, },
403 { STRING_PRODUCT, longname, },
404 { STRING_SERIAL, serial, },
405 { STRING_LOOPBACK, loopback, },
406 { STRING_SOURCE_SINK, source_sink, },
407 { } /* end of list */
410 static struct usb_gadget_strings stringtab = {
411 .language = 0x0409, /* en-us */
416 * config descriptors are also handcrafted. these must agree with code
417 * that sets configurations, and with code managing interfaces and their
418 * altsettings. other complexity may come from:
420 * - high speed support, including "other speed config" rules
421 * - multiple configurations
422 * - interfaces with alternate settings
423 * - embedded class or vendor-specific descriptors
425 * this handles high speed, and has a second config that could as easily
426 * have been an alternate interface setting (on most hardware).
428 * NOTE: to demonstrate (and test) more USB capabilities, this driver
429 * should include an altsetting to test interrupt transfers, including
430 * high bandwidth modes at high speed. (Maybe work like Intel's test
434 config_buf (struct usb_gadget *gadget,
435 u8 *buf, u8 type, unsigned index)
439 const struct usb_descriptor_header **function;
440 #ifdef CONFIG_USB_GADGET_DUALSPEED
441 int hs = (gadget->speed == USB_SPEED_HIGH);
444 /* two configurations will always be index 0 and index 1 */
447 is_source_sink = loopdefault ? (index == 1) : (index == 0);
449 #ifdef CONFIG_USB_GADGET_DUALSPEED
450 if (type == USB_DT_OTHER_SPEED_CONFIG)
453 function = is_source_sink
454 ? hs_source_sink_function
455 : hs_loopback_function;
458 function = is_source_sink
459 ? fs_source_sink_function
460 : fs_loopback_function;
462 /* for now, don't advertise srp-only devices */
466 len = usb_gadget_config_buf (is_source_sink
467 ? &source_sink_config
469 buf, USB_BUFSIZ, function);
472 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
476 /*-------------------------------------------------------------------------*/
478 static struct usb_request *
479 alloc_ep_req (struct usb_ep *ep, unsigned length)
481 struct usb_request *req;
483 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
485 req->length = length;
486 req->buf = usb_ep_alloc_buffer (ep, length,
487 &req->dma, GFP_ATOMIC);
489 usb_ep_free_request (ep, req);
496 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
499 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
500 usb_ep_free_request (ep, req);
503 /*-------------------------------------------------------------------------*/
505 /* optionally require specific source/sink data patterns */
509 struct zero_dev *dev,
511 struct usb_request *req
517 for (i = 0; i < req->actual; i++, buf++) {
519 /* all-zeroes has no synchronization issues */
524 /* mod63 stays in sync with short-terminated transfers,
525 * or otherwise when host and gadget agree on how large
526 * each usb transfer request should be. resync is done
527 * with set_interface or set_config.
530 if (*buf == (u8)(i % 63))
534 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
535 usb_ep_set_halt (ep);
543 struct zero_dev *dev,
545 struct usb_request *req
553 memset (req->buf, 0, req->length);
556 for (i = 0; i < req->length; i++)
557 *buf++ = (u8) (i % 63);
562 /* if there is only one request in the queue, there'll always be an
563 * irq delay between end of one request and start of the next.
564 * that prevents using hardware dma queues.
566 static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
568 struct zero_dev *dev = ep->driver_data;
569 int status = req->status;
573 case 0: /* normal completion? */
574 if (ep == dev->out_ep) {
575 check_read_data (dev, ep, req);
576 memset (req->buf, 0x55, req->length);
578 reinit_write_data (dev, ep, req);
581 /* this endpoint is normally active while we're configured */
582 case -ECONNABORTED: /* hardware forced ep reset */
583 case -ECONNRESET: /* request dequeued */
584 case -ESHUTDOWN: /* disconnect from host */
585 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
586 req->actual, req->length);
587 if (ep == dev->out_ep)
588 check_read_data (dev, ep, req);
589 free_ep_req (ep, req);
592 case -EOVERFLOW: /* buffer overrun on read means that
593 * we didn't provide a big enough
598 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
599 status, req->actual, req->length);
601 case -EREMOTEIO: /* short read */
605 status = usb_ep_queue (ep, req, GFP_ATOMIC);
607 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
608 ep->name, req->length, status);
609 usb_ep_set_halt (ep);
610 /* FIXME recover later ... somehow */
614 static struct usb_request *
615 source_sink_start_ep (struct usb_ep *ep, gfp_t gfp_flags)
617 struct usb_request *req;
620 req = alloc_ep_req (ep, buflen);
624 memset (req->buf, 0, req->length);
625 req->complete = source_sink_complete;
627 if (strcmp (ep->name, EP_IN_NAME) == 0)
628 reinit_write_data (ep->driver_data, ep, req);
630 memset (req->buf, 0x55, req->length);
632 status = usb_ep_queue (ep, req, gfp_flags);
634 struct zero_dev *dev = ep->driver_data;
636 ERROR (dev, "start %s --> %d\n", ep->name, status);
637 free_ep_req (ep, req);
645 set_source_sink_config (struct zero_dev *dev, gfp_t gfp_flags)
649 struct usb_gadget *gadget = dev->gadget;
651 gadget_for_each_ep (ep, gadget) {
652 const struct usb_endpoint_descriptor *d;
654 /* one endpoint writes (sources) zeroes in (to the host) */
655 if (strcmp (ep->name, EP_IN_NAME) == 0) {
656 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
657 result = usb_ep_enable (ep, d);
659 ep->driver_data = dev;
660 if (source_sink_start_ep (ep, gfp_flags) != 0) {
668 /* one endpoint reads (sinks) anything out (from the host) */
669 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
670 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
671 result = usb_ep_enable (ep, d);
673 ep->driver_data = dev;
674 if (source_sink_start_ep (ep, gfp_flags) != 0) {
682 /* ignore any other endpoints */
687 ERROR (dev, "can't start %s, result %d\n", ep->name, result);
691 DBG (dev, "buflen %d\n", buflen);
693 /* caller is responsible for cleanup on error */
697 /*-------------------------------------------------------------------------*/
699 static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
701 struct zero_dev *dev = ep->driver_data;
702 int status = req->status;
706 case 0: /* normal completion? */
707 if (ep == dev->out_ep) {
708 /* loop this OUT packet back IN to the host */
709 req->zero = (req->actual < req->length);
710 req->length = req->actual;
711 status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
715 /* "should never get here" */
716 ERROR (dev, "can't loop %s to %s: %d\n",
717 ep->name, dev->in_ep->name,
721 /* queue the buffer for some later OUT packet */
722 req->length = buflen;
723 status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
727 /* "should never get here" */
731 ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
732 status, req->actual, req->length);
735 /* NOTE: since this driver doesn't maintain an explicit record
736 * of requests it submitted (just maintains qlen count), we
737 * rely on the hardware driver to clean up on disconnect or
740 case -ECONNABORTED: /* hardware forced ep reset */
741 case -ECONNRESET: /* request dequeued */
742 case -ESHUTDOWN: /* disconnect from host */
743 free_ep_req (ep, req);
749 set_loopback_config (struct zero_dev *dev, gfp_t gfp_flags)
753 struct usb_gadget *gadget = dev->gadget;
755 gadget_for_each_ep (ep, gadget) {
756 const struct usb_endpoint_descriptor *d;
758 /* one endpoint writes data back IN to the host */
759 if (strcmp (ep->name, EP_IN_NAME) == 0) {
760 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
761 result = usb_ep_enable (ep, d);
763 ep->driver_data = dev;
768 /* one endpoint just reads OUT packets */
769 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
770 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
771 result = usb_ep_enable (ep, d);
773 ep->driver_data = dev;
778 /* ignore any other endpoints */
783 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
787 /* allocate a bunch of read buffers and queue them all at once.
788 * we buffer at most 'qlen' transfers; fewer if any need more
789 * than 'buflen' bytes each.
792 struct usb_request *req;
796 for (i = 0; i < qlen && result == 0; i++) {
797 req = alloc_ep_req (ep, buflen);
799 req->complete = loopback_complete;
800 result = usb_ep_queue (ep, req, GFP_ATOMIC);
802 DBG (dev, "%s queue req --> %d\n",
809 DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
811 /* caller is responsible for cleanup on error */
815 /*-------------------------------------------------------------------------*/
817 static void zero_reset_config (struct zero_dev *dev)
819 if (dev->config == 0)
822 DBG (dev, "reset config\n");
824 /* just disable endpoints, forcing completion of pending i/o.
825 * all our completion handlers free their requests in this case.
828 usb_ep_disable (dev->in_ep);
832 usb_ep_disable (dev->out_ep);
836 del_timer (&dev->resume);
839 /* change our operational config. this code must agree with the code
840 * that returns config descriptors, and altsetting code.
842 * it's also responsible for power management interactions. some
843 * configurations might not work with our current power sources.
845 * note that some device controller hardware will constrain what this
846 * code can do, perhaps by disallowing more than one configuration or
847 * by limiting configuration choices (like the pxa2xx).
850 zero_set_config (struct zero_dev *dev, unsigned number, gfp_t gfp_flags)
853 struct usb_gadget *gadget = dev->gadget;
855 if (number == dev->config)
858 if (gadget_is_sa1100 (gadget) && dev->config) {
859 /* tx fifo is full, but we can't clear it...*/
860 INFO (dev, "can't change configurations\n");
863 zero_reset_config (dev);
866 case CONFIG_SOURCE_SINK:
867 result = set_source_sink_config (dev, gfp_flags);
869 case CONFIG_LOOPBACK:
870 result = set_loopback_config (dev, gfp_flags);
879 if (!result && (!dev->in_ep || !dev->out_ep))
882 zero_reset_config (dev);
886 switch (gadget->speed) {
887 case USB_SPEED_LOW: speed = "low"; break;
888 case USB_SPEED_FULL: speed = "full"; break;
889 case USB_SPEED_HIGH: speed = "high"; break;
890 default: speed = "?"; break;
893 dev->config = number;
894 INFO (dev, "%s speed config #%d: %s\n", speed, number,
895 (number == CONFIG_SOURCE_SINK)
896 ? source_sink : loopback);
901 /*-------------------------------------------------------------------------*/
903 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
905 if (req->status || req->actual != req->length)
906 DBG ((struct zero_dev *) ep->driver_data,
907 "setup complete --> %d, %d/%d\n",
908 req->status, req->actual, req->length);
912 * The setup() callback implements all the ep0 functionality that's
913 * not handled lower down, in hardware or the hardware driver (like
914 * device and endpoint feature flags, and their status). It's all
915 * housekeeping for the gadget function we're implementing. Most of
916 * the work is in config-specific setup.
919 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
921 struct zero_dev *dev = get_gadget_data (gadget);
922 struct usb_request *req = dev->req;
923 int value = -EOPNOTSUPP;
924 u16 w_index = le16_to_cpu(ctrl->wIndex);
925 u16 w_value = le16_to_cpu(ctrl->wValue);
926 u16 w_length = le16_to_cpu(ctrl->wLength);
928 /* usually this stores reply data in the pre-allocated ep0 buffer,
929 * but config change events will reconfigure hardware.
932 switch (ctrl->bRequest) {
934 case USB_REQ_GET_DESCRIPTOR:
935 if (ctrl->bRequestType != USB_DIR_IN)
937 switch (w_value >> 8) {
940 value = min (w_length, (u16) sizeof device_desc);
941 memcpy (req->buf, &device_desc, value);
943 #ifdef CONFIG_USB_GADGET_DUALSPEED
944 case USB_DT_DEVICE_QUALIFIER:
945 if (!gadget->is_dualspeed)
947 value = min (w_length, (u16) sizeof dev_qualifier);
948 memcpy (req->buf, &dev_qualifier, value);
951 case USB_DT_OTHER_SPEED_CONFIG:
952 if (!gadget->is_dualspeed)
955 #endif /* CONFIG_USB_GADGET_DUALSPEED */
957 value = config_buf (gadget, req->buf,
961 value = min (w_length, (u16) value);
965 /* wIndex == language code.
966 * this driver only handles one language, you can
967 * add string tables for other languages, using
968 * any UTF-8 characters
970 value = usb_gadget_get_string (&stringtab,
971 w_value & 0xff, req->buf);
973 value = min (w_length, (u16) value);
978 /* currently two configs, two speeds */
979 case USB_REQ_SET_CONFIGURATION:
980 if (ctrl->bRequestType != 0)
982 if (gadget->a_hnp_support)
983 DBG (dev, "HNP available\n");
984 else if (gadget->a_alt_hnp_support)
985 DBG (dev, "HNP needs a different root port\n");
987 VDBG (dev, "HNP inactive\n");
988 spin_lock (&dev->lock);
989 value = zero_set_config (dev, w_value, GFP_ATOMIC);
990 spin_unlock (&dev->lock);
992 case USB_REQ_GET_CONFIGURATION:
993 if (ctrl->bRequestType != USB_DIR_IN)
995 *(u8 *)req->buf = dev->config;
996 value = min (w_length, (u16) 1);
999 /* until we add altsetting support, or other interfaces,
1000 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1001 * and already killed pending endpoint I/O.
1003 case USB_REQ_SET_INTERFACE:
1004 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1006 spin_lock (&dev->lock);
1007 if (dev->config && w_index == 0 && w_value == 0) {
1008 u8 config = dev->config;
1010 /* resets interface configuration, forgets about
1011 * previous transaction state (queued bufs, etc)
1012 * and re-inits endpoint state (toggle etc)
1013 * no response queued, just zero status == success.
1014 * if we had more than one interface we couldn't
1015 * use this "reset the config" shortcut.
1017 zero_reset_config (dev);
1018 zero_set_config (dev, config, GFP_ATOMIC);
1021 spin_unlock (&dev->lock);
1023 case USB_REQ_GET_INTERFACE:
1024 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1032 *(u8 *)req->buf = 0;
1033 value = min (w_length, (u16) 1);
1037 * These are the same vendor-specific requests supported by
1038 * Intel's USB 2.0 compliance test devices. We exceed that
1039 * device spec by allowing multiple-packet requests.
1041 case 0x5b: /* control WRITE test -- fill the buffer */
1042 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1044 if (w_value || w_index)
1046 /* just read that many bytes into the buffer */
1047 if (w_length > USB_BUFSIZ)
1051 case 0x5c: /* control READ test -- return the buffer */
1052 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1054 if (w_value || w_index)
1056 /* expect those bytes are still in the buffer; send back */
1057 if (w_length > USB_BUFSIZ
1058 || w_length != req->length)
1066 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1067 ctrl->bRequestType, ctrl->bRequest,
1068 w_value, w_index, w_length);
1071 /* respond with data transfer before status phase? */
1073 req->length = value;
1074 req->zero = value < w_length;
1075 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1077 DBG (dev, "ep_queue --> %d\n", value);
1079 zero_setup_complete (gadget->ep0, req);
1083 /* device either stalls (value < 0) or reports success */
1088 zero_disconnect (struct usb_gadget *gadget)
1090 struct zero_dev *dev = get_gadget_data (gadget);
1091 unsigned long flags;
1093 spin_lock_irqsave (&dev->lock, flags);
1094 zero_reset_config (dev);
1096 /* a more significant application might have some non-usb
1097 * activities to quiesce here, saving resources like power
1098 * or pushing the notification up a network stack.
1100 spin_unlock_irqrestore (&dev->lock, flags);
1102 /* next we may get setup() calls to enumerate new connections;
1103 * or an unbind() during shutdown (including removing module).
1108 zero_autoresume (unsigned long _dev)
1110 struct zero_dev *dev = (struct zero_dev *) _dev;
1113 /* normally the host would be woken up for something
1114 * more significant than just a timer firing...
1116 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1117 status = usb_gadget_wakeup (dev->gadget);
1118 DBG (dev, "wakeup --> %d\n", status);
1122 /*-------------------------------------------------------------------------*/
1124 static void /* __init_or_exit */
1125 zero_unbind (struct usb_gadget *gadget)
1127 struct zero_dev *dev = get_gadget_data (gadget);
1129 DBG (dev, "unbind\n");
1131 /* we've already been disconnected ... no i/o is active */
1133 dev->req->length = USB_BUFSIZ;
1134 free_ep_req (gadget->ep0, dev->req);
1136 del_timer_sync (&dev->resume);
1138 set_gadget_data (gadget, NULL);
1142 zero_bind (struct usb_gadget *gadget)
1144 struct zero_dev *dev;
1148 /* FIXME this can't yet work right with SH ... it has only
1149 * one configuration, numbered one.
1151 if (gadget_is_sh(gadget))
1154 /* Bulk-only drivers like this one SHOULD be able to
1155 * autoconfigure on any sane usb controller driver,
1156 * but there may also be important quirks to address.
1158 usb_ep_autoconfig_reset (gadget);
1159 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1162 printk (KERN_ERR "%s: can't autoconfigure on %s\n",
1163 shortname, gadget->name);
1166 EP_IN_NAME = ep->name;
1167 ep->driver_data = ep; /* claim */
1169 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1172 EP_OUT_NAME = ep->name;
1173 ep->driver_data = ep; /* claim */
1175 gcnum = usb_gadget_controller_number (gadget);
1177 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
1179 /* gadget zero is so simple (for now, no altsettings) that
1180 * it SHOULD NOT have problems with bulk-capable hardware.
1181 * so warn about unrcognized controllers, don't panic.
1183 * things like configuration and altsetting numbering
1184 * can need hardware-specific attention though.
1186 printk (KERN_WARNING "%s: controller '%s' not recognized\n",
1187 shortname, gadget->name);
1188 device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
1192 /* ok, we made sense of the hardware ... */
1193 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1196 spin_lock_init (&dev->lock);
1197 dev->gadget = gadget;
1198 set_gadget_data (gadget, dev);
1200 /* preallocate control response and buffer */
1201 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1204 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1205 &dev->req->dma, GFP_KERNEL);
1209 dev->req->complete = zero_setup_complete;
1211 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1213 #ifdef CONFIG_USB_GADGET_DUALSPEED
1214 /* assume ep0 uses the same value for both speeds ... */
1215 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1217 /* and that all endpoints are dual-speed */
1218 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1219 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1222 if (gadget->is_otg) {
1223 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1224 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1225 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1228 usb_gadget_set_selfpowered (gadget);
1230 init_timer (&dev->resume);
1231 dev->resume.function = zero_autoresume;
1232 dev->resume.data = (unsigned long) dev;
1234 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1235 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1238 gadget->ep0->driver_data = dev;
1240 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1241 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1242 EP_OUT_NAME, EP_IN_NAME);
1244 snprintf (manufacturer, sizeof manufacturer, "%s %s with %s",
1245 init_utsname()->sysname, init_utsname()->release,
1251 zero_unbind (gadget);
1255 /*-------------------------------------------------------------------------*/
1258 zero_suspend (struct usb_gadget *gadget)
1260 struct zero_dev *dev = get_gadget_data (gadget);
1262 if (gadget->speed == USB_SPEED_UNKNOWN)
1266 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1267 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1269 DBG (dev, "suspend\n");
1273 zero_resume (struct usb_gadget *gadget)
1275 struct zero_dev *dev = get_gadget_data (gadget);
1277 DBG (dev, "resume\n");
1278 del_timer (&dev->resume);
1282 /*-------------------------------------------------------------------------*/
1284 static struct usb_gadget_driver zero_driver = {
1285 #ifdef CONFIG_USB_GADGET_DUALSPEED
1286 .speed = USB_SPEED_HIGH,
1288 .speed = USB_SPEED_FULL,
1290 .function = (char *) longname,
1292 .unbind = __exit_p(zero_unbind),
1294 .setup = zero_setup,
1295 .disconnect = zero_disconnect,
1297 .suspend = zero_suspend,
1298 .resume = zero_resume,
1301 .name = (char *) shortname,
1302 .owner = THIS_MODULE,
1306 MODULE_AUTHOR ("David Brownell");
1307 MODULE_LICENSE ("Dual BSD/GPL");
1310 static int __init init (void)
1312 /* a real value would likely come through some id prom
1313 * or module option. this one takes at least two packets.
1315 strlcpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
1317 return usb_gadget_register_driver (&zero_driver);
1321 static void __exit cleanup (void)
1323 usb_gadget_unregister_driver (&zero_driver);
1325 module_exit (cleanup);