2 * zero.c -- Gadget Zero, for USB development
4 * Copyright (C) 2003-2004 David Brownell
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation, either version 2 of that License or (at your option) any
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Gadget Zero only needs two bulk endpoints, and is an example of how you
41 * can write a hardware-agnostic gadget driver running inside a USB device.
43 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44 * affect most of the driver.
46 * Use it with the Linux host/master side "usbtest" driver to get a basic
47 * functional test of your device-side usb stack, or with "usb-skeleton".
49 * It supports two similar configurations. One sinks whatever the usb host
50 * writes, and in return sources zeroes. The other loops whatever the host
51 * writes back, so the host can read it. Module options include:
53 * buflen=N default N=4096, buffer size used
54 * qlen=N default N=32, how many buffers in the loopback queue
55 * loopdefault default false, list loopback config first
57 * Many drivers will only have one configuration, letting them be much
58 * simpler if they also don't support high speed operation (like this
65 #include <linux/config.h>
66 #include <linux/kernel.h>
67 #include <linux/module.h>
68 #include <linux/delay.h>
69 #include <linux/ioport.h>
70 #include <linux/sched.h>
71 #include <linux/slab.h>
72 #include <linux/smp_lock.h>
73 #include <linux/errno.h>
74 #include <linux/init.h>
75 #include <linux/timer.h>
76 #include <linux/list.h>
77 #include <linux/interrupt.h>
78 #include <linux/uts.h>
79 #include <linux/version.h>
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 printk(level "%s %s: " fmt , shortname , (d)->gadget->dev.bus_id , \
141 #define DBG(dev,fmt,args...) \
142 xprintk(dev , KERN_DEBUG , fmt , ## args)
144 #define DBG(dev,fmt,args...) \
151 #define VDBG(dev,fmt,args...) \
155 #define ERROR(dev,fmt,args...) \
156 xprintk(dev , KERN_ERR , fmt , ## args)
157 #define WARN(dev,fmt,args...) \
158 xprintk(dev , KERN_WARNING , fmt , ## args)
159 #define INFO(dev,fmt,args...) \
160 xprintk(dev , KERN_INFO , fmt , ## args)
162 /*-------------------------------------------------------------------------*/
164 static unsigned buflen = 4096;
165 static unsigned qlen = 32;
166 static unsigned pattern = 0;
169 * Normally the "loopback" configuration is second (index 1) so
170 * it's not the default. Here's where to change that order, to
171 * work better with hosts where config changes are problematic.
172 * Or controllers (like superh) that only support one config.
174 static int loopdefault = 0;
177 MODULE_PARM (buflen, "i");
178 MODULE_PARM_DESC (buflen, "size of i/o buffers");
180 MODULE_PARM (qlen, "i");
181 MODULE_PARM_DESC (qlen, "depth of loopback buffering");
183 MODULE_PARM (pattern, "i");
184 MODULE_PARM_DESC (pattern, "0 for default all-zeroes, 1 for mod63");
186 MODULE_PARM (loopdefault, "b");
187 MODULE_PARM_DESC (loopdefault, "true to have default config be loopback");
190 * if it's nonzero, autoresume says how many seconds to wait
191 * before trying to wake up the host after suspend.
193 static unsigned autoresume = 0;
194 MODULE_PARM (autoresume, "i");
196 /*-------------------------------------------------------------------------*/
198 /* Thanks to NetChip Technologies for donating this product ID.
200 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
201 * Instead: allocate your own, using normal USB-IF procedures.
203 #ifndef CONFIG_USB_ZERO_HNPTEST
204 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
205 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
207 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
208 #define DRIVER_PRODUCT_NUM 0xbadd
211 /*-------------------------------------------------------------------------*/
214 * DESCRIPTORS ... most are static, but strings and (full)
215 * configuration descriptors are built on demand.
218 #define STRING_MANUFACTURER 25
219 #define STRING_PRODUCT 42
220 #define STRING_SERIAL 101
221 #define STRING_SOURCE_SINK 250
222 #define STRING_LOOPBACK 251
225 * This device advertises two configurations; these numbers work
226 * on a pxa250 as well as more flexible hardware.
228 #define CONFIG_SOURCE_SINK 3
229 #define CONFIG_LOOPBACK 2
231 static struct usb_device_descriptor
233 .bLength = sizeof device_desc,
234 .bDescriptorType = USB_DT_DEVICE,
236 .bcdUSB = __constant_cpu_to_le16 (0x0200),
237 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
239 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
240 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
241 .iManufacturer = STRING_MANUFACTURER,
242 .iProduct = STRING_PRODUCT,
243 .iSerialNumber = STRING_SERIAL,
244 .bNumConfigurations = 2,
247 static struct usb_config_descriptor
248 source_sink_config = {
249 .bLength = sizeof source_sink_config,
250 .bDescriptorType = USB_DT_CONFIG,
252 /* compute wTotalLength on the fly */
254 .bConfigurationValue = CONFIG_SOURCE_SINK,
255 .iConfiguration = STRING_SOURCE_SINK,
256 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
257 .bMaxPower = 1, /* self-powered */
260 static struct usb_config_descriptor
262 .bLength = sizeof loopback_config,
263 .bDescriptorType = USB_DT_CONFIG,
265 /* compute wTotalLength on the fly */
267 .bConfigurationValue = CONFIG_LOOPBACK,
268 .iConfiguration = STRING_LOOPBACK,
269 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
270 .bMaxPower = 1, /* self-powered */
273 static struct usb_otg_descriptor
275 .bLength = sizeof otg_descriptor,
276 .bDescriptorType = USB_DT_OTG,
278 .bmAttributes = USB_OTG_SRP,
281 /* one interface in each configuration */
283 static const struct usb_interface_descriptor
285 .bLength = sizeof source_sink_intf,
286 .bDescriptorType = USB_DT_INTERFACE,
289 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
290 .iInterface = STRING_SOURCE_SINK,
293 static const struct usb_interface_descriptor
295 .bLength = sizeof loopback_intf,
296 .bDescriptorType = USB_DT_INTERFACE,
299 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
300 .iInterface = STRING_LOOPBACK,
303 /* two full speed bulk endpoints; their use is config-dependent */
305 static struct usb_endpoint_descriptor
307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
310 .bEndpointAddress = USB_DIR_IN,
311 .bmAttributes = USB_ENDPOINT_XFER_BULK,
314 static struct usb_endpoint_descriptor
316 .bLength = USB_DT_ENDPOINT_SIZE,
317 .bDescriptorType = USB_DT_ENDPOINT,
319 .bEndpointAddress = USB_DIR_OUT,
320 .bmAttributes = USB_ENDPOINT_XFER_BULK,
323 static const struct usb_descriptor_header *fs_source_sink_function [] = {
324 (struct usb_descriptor_header *) &otg_descriptor,
325 (struct usb_descriptor_header *) &source_sink_intf,
326 (struct usb_descriptor_header *) &fs_sink_desc,
327 (struct usb_descriptor_header *) &fs_source_desc,
331 static const struct usb_descriptor_header *fs_loopback_function [] = {
332 (struct usb_descriptor_header *) &otg_descriptor,
333 (struct usb_descriptor_header *) &loopback_intf,
334 (struct usb_descriptor_header *) &fs_sink_desc,
335 (struct usb_descriptor_header *) &fs_source_desc,
339 #ifdef CONFIG_USB_GADGET_DUALSPEED
342 * usb 2.0 devices need to expose both high speed and full speed
343 * descriptors, unless they only run at full speed.
345 * that means alternate endpoint descriptors (bigger packets)
346 * and a "device qualifier" ... plus more construction options
347 * for the config descriptor.
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_endpoint_descriptor
361 .bLength = USB_DT_ENDPOINT_SIZE,
362 .bDescriptorType = USB_DT_ENDPOINT,
364 .bmAttributes = USB_ENDPOINT_XFER_BULK,
365 .wMaxPacketSize = __constant_cpu_to_le16 (512),
368 static struct usb_qualifier_descriptor
370 .bLength = sizeof dev_qualifier,
371 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
373 .bcdUSB = __constant_cpu_to_le16 (0x0200),
374 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
376 .bNumConfigurations = 2,
379 static const struct usb_descriptor_header *hs_source_sink_function [] = {
380 (struct usb_descriptor_header *) &otg_descriptor,
381 (struct usb_descriptor_header *) &source_sink_intf,
382 (struct usb_descriptor_header *) &hs_source_desc,
383 (struct usb_descriptor_header *) &hs_sink_desc,
387 static const struct usb_descriptor_header *hs_loopback_function [] = {
388 (struct usb_descriptor_header *) &otg_descriptor,
389 (struct usb_descriptor_header *) &loopback_intf,
390 (struct usb_descriptor_header *) &hs_source_desc,
391 (struct usb_descriptor_header *) &hs_sink_desc,
395 /* maxpacket and other transfer characteristics vary by speed. */
396 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
400 /* if there's no high speed support, maxpacket doesn't change. */
401 #define ep_desc(g,hs,fs) fs
403 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
405 static char manufacturer [50];
406 static char serial [40];
408 /* static strings, in UTF-8 */
409 static struct usb_string strings [] = {
410 { STRING_MANUFACTURER, manufacturer, },
411 { STRING_PRODUCT, longname, },
412 { STRING_SERIAL, serial, },
413 { STRING_LOOPBACK, loopback, },
414 { STRING_SOURCE_SINK, source_sink, },
415 { } /* end of list */
418 static struct usb_gadget_strings stringtab = {
419 .language = 0x0409, /* en-us */
424 * config descriptors are also handcrafted. these must agree with code
425 * that sets configurations, and with code managing interfaces and their
426 * altsettings. other complexity may come from:
428 * - high speed support, including "other speed config" rules
429 * - multiple configurations
430 * - interfaces with alternate settings
431 * - embedded class or vendor-specific descriptors
433 * this handles high speed, and has a second config that could as easily
434 * have been an alternate interface setting (on most hardware).
436 * NOTE: to demonstrate (and test) more USB capabilities, this driver
437 * should include an altsetting to test interrupt transfers, including
438 * high bandwidth modes at high speed. (Maybe work like Intel's test
442 config_buf (struct usb_gadget *gadget,
443 u8 *buf, u8 type, unsigned index)
447 const struct usb_descriptor_header **function;
448 #ifdef CONFIG_USB_GADGET_DUALSPEED
449 int hs = (gadget->speed == USB_SPEED_HIGH);
452 /* two configurations will always be index 0 and index 1 */
455 is_source_sink = loopdefault ? (index == 1) : (index == 0);
457 #ifdef CONFIG_USB_GADGET_DUALSPEED
458 if (type == USB_DT_OTHER_SPEED_CONFIG)
461 function = is_source_sink
462 ? hs_source_sink_function
463 : hs_loopback_function;
466 function = is_source_sink
467 ? fs_source_sink_function
468 : fs_loopback_function;
470 /* for now, don't advertise srp-only devices */
474 len = usb_gadget_config_buf (is_source_sink
475 ? &source_sink_config
477 buf, USB_BUFSIZ, function);
480 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
484 /*-------------------------------------------------------------------------*/
486 static struct usb_request *
487 alloc_ep_req (struct usb_ep *ep, unsigned length)
489 struct usb_request *req;
491 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
493 req->length = length;
494 req->buf = usb_ep_alloc_buffer (ep, length,
495 &req->dma, GFP_ATOMIC);
497 usb_ep_free_request (ep, req);
504 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
507 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
508 usb_ep_free_request (ep, req);
511 /*-------------------------------------------------------------------------*/
513 /* optionally require specific source/sink data patterns */
517 struct zero_dev *dev,
519 struct usb_request *req
525 for (i = 0; i < req->actual; i++, buf++) {
527 /* all-zeroes has no synchronization issues */
532 /* mod63 stays in sync with short-terminated transfers,
533 * or otherwise when host and gadget agree on how large
534 * each usb transfer request should be. resync is done
535 * with set_interface or set_config.
538 if (*buf == (u8)(i % 63))
542 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
543 usb_ep_set_halt (ep);
551 struct zero_dev *dev,
553 struct usb_request *req
561 memset (req->buf, 0, req->length);
564 for (i = 0; i < req->length; i++)
565 *buf++ = (u8) (i % 63);
570 /* if there is only one request in the queue, there'll always be an
571 * irq delay between end of one request and start of the next.
572 * that prevents using hardware dma queues.
574 static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
576 struct zero_dev *dev = ep->driver_data;
577 int status = req->status;
581 case 0: /* normal completion? */
582 if (ep == dev->out_ep)
583 check_read_data (dev, ep, req);
585 reinit_write_data (dev, ep, req);
588 /* this endpoint is normally active while we're configured */
589 case -ECONNABORTED: /* hardware forced ep reset */
590 case -ECONNRESET: /* request dequeued */
591 case -ESHUTDOWN: /* disconnect from host */
592 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
593 req->actual, req->length);
594 if (ep == dev->out_ep)
595 check_read_data (dev, ep, req);
596 free_ep_req (ep, req);
599 case -EOVERFLOW: /* buffer overrun on read means that
600 * we didn't provide a big enough
605 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
606 status, req->actual, req->length);
608 case -EREMOTEIO: /* short read */
612 status = usb_ep_queue (ep, req, GFP_ATOMIC);
614 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
615 ep->name, req->length, status);
616 usb_ep_set_halt (ep);
617 /* FIXME recover later ... somehow */
621 static struct usb_request *
622 source_sink_start_ep (struct usb_ep *ep, int gfp_flags)
624 struct usb_request *req;
627 req = alloc_ep_req (ep, buflen);
631 memset (req->buf, 0, req->length);
632 req->complete = source_sink_complete;
634 if (strcmp (ep->name, EP_IN_NAME) == 0)
635 reinit_write_data (ep->driver_data, ep, req);
637 status = usb_ep_queue (ep, req, gfp_flags);
639 struct zero_dev *dev = ep->driver_data;
641 ERROR (dev, "start %s --> %d\n", ep->name, status);
642 free_ep_req (ep, req);
650 set_source_sink_config (struct zero_dev *dev, int gfp_flags)
654 struct usb_gadget *gadget = dev->gadget;
656 gadget_for_each_ep (ep, gadget) {
657 const struct usb_endpoint_descriptor *d;
659 /* one endpoint writes (sources) zeroes in (to the host) */
660 if (strcmp (ep->name, EP_IN_NAME) == 0) {
661 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
662 result = usb_ep_enable (ep, d);
664 ep->driver_data = dev;
665 if (source_sink_start_ep (ep, gfp_flags) != 0) {
673 /* one endpoint reads (sinks) anything out (from the host) */
674 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
675 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
676 result = usb_ep_enable (ep, d);
678 ep->driver_data = dev;
679 if (source_sink_start_ep (ep, gfp_flags) != 0) {
687 /* ignore any other endpoints */
692 ERROR (dev, "can't start %s, result %d\n", ep->name, result);
696 DBG (dev, "buflen %d\n", buflen);
698 /* caller is responsible for cleanup on error */
702 /*-------------------------------------------------------------------------*/
704 static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
706 struct zero_dev *dev = ep->driver_data;
707 int status = req->status;
711 case 0: /* normal completion? */
712 if (ep == dev->out_ep) {
713 /* loop this OUT packet back IN to the host */
714 req->zero = (req->actual < req->length);
715 req->length = req->actual;
716 status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
720 /* "should never get here" */
721 ERROR (dev, "can't loop %s to %s: %d\n",
722 ep->name, dev->in_ep->name,
726 /* queue the buffer for some later OUT packet */
727 req->length = buflen;
728 status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
732 /* "should never get here" */
736 ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
737 status, req->actual, req->length);
740 /* NOTE: since this driver doesn't maintain an explicit record
741 * of requests it submitted (just maintains qlen count), we
742 * rely on the hardware driver to clean up on disconnect or
745 case -ECONNABORTED: /* hardware forced ep reset */
746 case -ECONNRESET: /* request dequeued */
747 case -ESHUTDOWN: /* disconnect from host */
748 free_ep_req (ep, req);
754 set_loopback_config (struct zero_dev *dev, int gfp_flags)
758 struct usb_gadget *gadget = dev->gadget;
760 gadget_for_each_ep (ep, gadget) {
761 const struct usb_endpoint_descriptor *d;
763 /* one endpoint writes data back IN to the host */
764 if (strcmp (ep->name, EP_IN_NAME) == 0) {
765 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
766 result = usb_ep_enable (ep, d);
768 ep->driver_data = dev;
773 /* one endpoint just reads OUT packets */
774 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
775 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
776 result = usb_ep_enable (ep, d);
778 ep->driver_data = dev;
783 /* ignore any other endpoints */
788 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
792 /* allocate a bunch of read buffers and queue them all at once.
793 * we buffer at most 'qlen' transfers; fewer if any need more
794 * than 'buflen' bytes each.
797 struct usb_request *req;
801 for (i = 0; i < qlen && result == 0; i++) {
802 req = alloc_ep_req (ep, buflen);
804 req->complete = loopback_complete;
805 result = usb_ep_queue (ep, req, GFP_ATOMIC);
807 DBG (dev, "%s queue req --> %d\n",
814 DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
816 /* caller is responsible for cleanup on error */
820 /*-------------------------------------------------------------------------*/
822 static void zero_reset_config (struct zero_dev *dev)
824 if (dev->config == 0)
827 DBG (dev, "reset config\n");
829 /* just disable endpoints, forcing completion of pending i/o.
830 * all our completion handlers free their requests in this case.
833 usb_ep_disable (dev->in_ep);
837 usb_ep_disable (dev->out_ep);
841 del_timer (&dev->resume);
844 /* change our operational config. this code must agree with the code
845 * that returns config descriptors, and altsetting code.
847 * it's also responsible for power management interactions. some
848 * configurations might not work with our current power sources.
850 * note that some device controller hardware will constrain what this
851 * code can do, perhaps by disallowing more than one configuration or
852 * by limiting configuration choices (like the pxa2xx).
855 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
858 struct usb_gadget *gadget = dev->gadget;
860 if (number == dev->config)
863 if (gadget_is_sa1100 (gadget) && dev->config) {
864 /* tx fifo is full, but we can't clear it...*/
865 INFO (dev, "can't change configurations\n");
868 zero_reset_config (dev);
871 case CONFIG_SOURCE_SINK:
872 result = set_source_sink_config (dev, gfp_flags);
874 case CONFIG_LOOPBACK:
875 result = set_loopback_config (dev, gfp_flags);
884 if (!result && (!dev->in_ep || !dev->out_ep))
887 zero_reset_config (dev);
891 switch (gadget->speed) {
892 case USB_SPEED_LOW: speed = "low"; break;
893 case USB_SPEED_FULL: speed = "full"; break;
894 case USB_SPEED_HIGH: speed = "high"; break;
895 default: speed = "?"; break;
898 dev->config = number;
899 INFO (dev, "%s speed config #%d: %s\n", speed, number,
900 (number == CONFIG_SOURCE_SINK)
901 ? source_sink : loopback);
906 /*-------------------------------------------------------------------------*/
908 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
910 if (req->status || req->actual != req->length)
911 DBG ((struct zero_dev *) ep->driver_data,
912 "setup complete --> %d, %d/%d\n",
913 req->status, req->actual, req->length);
917 * The setup() callback implements all the ep0 functionality that's
918 * not handled lower down, in hardware or the hardware driver (like
919 * device and endpoint feature flags, and their status). It's all
920 * housekeeping for the gadget function we're implementing. Most of
921 * the work is in config-specific setup.
924 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
926 struct zero_dev *dev = get_gadget_data (gadget);
927 struct usb_request *req = dev->req;
928 int value = -EOPNOTSUPP;
930 /* usually this stores reply data in the pre-allocated ep0 buffer,
931 * but config change events will reconfigure hardware.
934 switch (ctrl->bRequest) {
936 case USB_REQ_GET_DESCRIPTOR:
937 if (ctrl->bRequestType != USB_DIR_IN)
939 switch (ctrl->wValue >> 8) {
942 value = min (ctrl->wLength, (u16) sizeof device_desc);
943 memcpy (req->buf, &device_desc, value);
945 #ifdef CONFIG_USB_GADGET_DUALSPEED
946 case USB_DT_DEVICE_QUALIFIER:
947 if (!gadget->is_dualspeed)
949 value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
950 memcpy (req->buf, &dev_qualifier, value);
953 case USB_DT_OTHER_SPEED_CONFIG:
954 if (!gadget->is_dualspeed)
957 #endif /* CONFIG_USB_GADGET_DUALSPEED */
959 value = config_buf (gadget, req->buf,
961 ctrl->wValue & 0xff);
963 value = min (ctrl->wLength, (u16) value);
967 /* wIndex == language code.
968 * this driver only handles one language, you can
969 * add string tables for other languages, using
970 * any UTF-8 characters
972 value = usb_gadget_get_string (&stringtab,
973 ctrl->wValue & 0xff, req->buf);
975 value = min (ctrl->wLength, (u16) value);
980 /* currently two configs, two speeds */
981 case USB_REQ_SET_CONFIGURATION:
982 if (ctrl->bRequestType != 0)
984 if (gadget->a_hnp_support)
985 DBG (dev, "HNP available\n");
986 else if (gadget->a_alt_hnp_support)
987 DBG (dev, "HNP needs a different root port\n");
989 VDBG (dev, "HNP inactive\n");
990 spin_lock (&dev->lock);
991 value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
992 spin_unlock (&dev->lock);
994 case USB_REQ_GET_CONFIGURATION:
995 if (ctrl->bRequestType != USB_DIR_IN)
997 *(u8 *)req->buf = dev->config;
998 value = min (ctrl->wLength, (u16) 1);
1001 /* until we add altsetting support, or other interfaces,
1002 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1003 * and already killed pending endpoint I/O.
1005 case USB_REQ_SET_INTERFACE:
1006 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1008 spin_lock (&dev->lock);
1009 if (dev->config && ctrl->wIndex == 0 && ctrl->wValue == 0) {
1010 u8 config = dev->config;
1012 /* resets interface configuration, forgets about
1013 * previous transaction state (queued bufs, etc)
1014 * and re-inits endpoint state (toggle etc)
1015 * no response queued, just zero status == success.
1016 * if we had more than one interface we couldn't
1017 * use this "reset the config" shortcut.
1019 zero_reset_config (dev);
1020 zero_set_config (dev, config, GFP_ATOMIC);
1023 spin_unlock (&dev->lock);
1025 case USB_REQ_GET_INTERFACE:
1026 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1030 if (ctrl->wIndex != 0) {
1034 *(u8 *)req->buf = 0;
1035 value = min (ctrl->wLength, (u16) 1);
1039 * These are the same vendor-specific requests supported by
1040 * Intel's USB 2.0 compliance test devices. We exceed that
1041 * device spec by allowing multiple-packet requests.
1043 case 0x5b: /* control WRITE test -- fill the buffer */
1044 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1046 if (ctrl->wValue || ctrl->wIndex)
1048 /* just read that many bytes into the buffer */
1049 if (ctrl->wLength > USB_BUFSIZ)
1051 value = ctrl->wLength;
1053 case 0x5c: /* control READ test -- return the buffer */
1054 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1056 if (ctrl->wValue || ctrl->wIndex)
1058 /* expect those bytes are still in the buffer; send back */
1059 if (ctrl->wLength > USB_BUFSIZ
1060 || ctrl->wLength != req->length)
1062 value = ctrl->wLength;
1068 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1069 ctrl->bRequestType, ctrl->bRequest,
1070 ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1073 /* respond with data transfer before status phase? */
1075 req->length = value;
1076 req->zero = value < ctrl->wLength
1077 && (value % gadget->ep0->maxpacket) == 0;
1078 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1080 DBG (dev, "ep_queue --> %d\n", value);
1082 zero_setup_complete (gadget->ep0, req);
1086 /* device either stalls (value < 0) or reports success */
1091 zero_disconnect (struct usb_gadget *gadget)
1093 struct zero_dev *dev = get_gadget_data (gadget);
1094 unsigned long flags;
1096 spin_lock_irqsave (&dev->lock, flags);
1097 zero_reset_config (dev);
1099 /* a more significant application might have some non-usb
1100 * activities to quiesce here, saving resources like power
1101 * or pushing the notification up a network stack.
1103 spin_unlock_irqrestore (&dev->lock, flags);
1105 /* next we may get setup() calls to enumerate new connections;
1106 * or an unbind() during shutdown (including removing module).
1111 zero_autoresume (unsigned long _dev)
1113 struct zero_dev *dev = (struct zero_dev *) _dev;
1116 /* normally the host would be woken up for something
1117 * more significant than just a timer firing...
1119 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1120 status = usb_gadget_wakeup (dev->gadget);
1121 DBG (dev, "wakeup --> %d\n", status);
1125 /*-------------------------------------------------------------------------*/
1128 zero_unbind (struct usb_gadget *gadget)
1130 struct zero_dev *dev = get_gadget_data (gadget);
1132 DBG (dev, "unbind\n");
1134 /* we've already been disconnected ... no i/o is active */
1136 free_ep_req (gadget->ep0, dev->req);
1137 del_timer_sync (&dev->resume);
1139 set_gadget_data (gadget, NULL);
1143 zero_bind (struct usb_gadget *gadget)
1145 struct zero_dev *dev;
1148 /* Bulk-only drivers like this one SHOULD be able to
1149 * autoconfigure on any sane usb controller driver,
1150 * but there may also be important quirks to address.
1152 usb_ep_autoconfig_reset (gadget);
1153 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1156 printk (KERN_ERR "%s: can't autoconfigure on %s\n",
1157 shortname, gadget->name);
1160 EP_IN_NAME = ep->name;
1161 ep->driver_data = ep; /* claim */
1163 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1166 EP_OUT_NAME = ep->name;
1167 ep->driver_data = ep; /* claim */
1171 * DRIVER POLICY CHOICE: you may want to do this differently.
1172 * One thing to avoid is reusing a bcdDevice revision code
1173 * with different host-visible configurations or behavior
1174 * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1176 if (gadget_is_net2280 (gadget)) {
1177 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
1178 } else if (gadget_is_pxa (gadget)) {
1179 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
1181 } else if (gadget_is_sh(gadget)) {
1182 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
1183 /* SH has only one configuration; see "loopdefault" */
1184 device_desc.bNumConfigurations = 1;
1185 /* FIXME make 1 == default.bConfigurationValue */
1187 } else if (gadget_is_sa1100 (gadget)) {
1188 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
1189 } else if (gadget_is_goku (gadget)) {
1190 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
1191 } else if (gadget_is_mq11xx (gadget)) {
1192 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
1193 } else if (gadget_is_omap (gadget)) {
1194 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
1195 } else if (gadget_is_lh7a40x(gadget)) {
1196 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0209);
1197 } else if (gadget_is_n9604(gadget)) {
1198 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0210);
1199 } else if (gadget_is_pxa27x(gadget)) {
1200 device_desc.bcdDevice = __constant_cpu_to_le16 (0x0211);
1202 /* gadget zero is so simple (for now, no altsettings) that
1203 * it SHOULD NOT have problems with bulk-capable hardware.
1204 * so warn about unrcognized controllers, don't panic.
1206 * things like configuration and altsetting numbering
1207 * can need hardware-specific attention though.
1209 printk (KERN_WARNING "%s: controller '%s' not recognized\n",
1210 shortname, gadget->name);
1211 device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
1215 /* ok, we made sense of the hardware ... */
1216 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1219 memset (dev, 0, sizeof *dev);
1220 spin_lock_init (&dev->lock);
1221 dev->gadget = gadget;
1222 set_gadget_data (gadget, dev);
1224 /* preallocate control response and buffer */
1225 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1228 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1229 &dev->req->dma, GFP_KERNEL);
1233 dev->req->complete = zero_setup_complete;
1235 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1237 #ifdef CONFIG_USB_GADGET_DUALSPEED
1238 /* assume ep0 uses the same value for both speeds ... */
1239 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1241 /* and that all endpoints are dual-speed */
1242 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1243 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1246 if (gadget->is_otg) {
1247 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1248 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1249 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1252 if (gadget->is_otg) {
1253 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1254 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1255 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1258 usb_gadget_set_selfpowered (gadget);
1260 init_timer (&dev->resume);
1261 dev->resume.function = zero_autoresume;
1262 dev->resume.data = (unsigned long) dev;
1264 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1265 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1268 gadget->ep0->driver_data = dev;
1270 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1271 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1272 EP_OUT_NAME, EP_IN_NAME);
1274 snprintf (manufacturer, sizeof manufacturer,
1275 UTS_SYSNAME " " UTS_RELEASE " with %s",
1281 zero_unbind (gadget);
1285 /*-------------------------------------------------------------------------*/
1288 zero_suspend (struct usb_gadget *gadget)
1290 struct zero_dev *dev = get_gadget_data (gadget);
1292 if (gadget->speed == USB_SPEED_UNKNOWN)
1296 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1297 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1299 DBG (dev, "suspend\n");
1303 zero_resume (struct usb_gadget *gadget)
1305 struct zero_dev *dev = get_gadget_data (gadget);
1307 DBG (dev, "resume\n");
1308 del_timer (&dev->resume);
1312 /*-------------------------------------------------------------------------*/
1314 static struct usb_gadget_driver zero_driver = {
1315 #ifdef CONFIG_USB_GADGET_DUALSPEED
1316 .speed = USB_SPEED_HIGH,
1318 .speed = USB_SPEED_FULL,
1320 .function = (char *) longname,
1322 .unbind = zero_unbind,
1324 .setup = zero_setup,
1325 .disconnect = zero_disconnect,
1327 .suspend = zero_suspend,
1328 .resume = zero_resume,
1331 .name = (char *) shortname,
1338 MODULE_AUTHOR ("David Brownell");
1339 MODULE_LICENSE ("Dual BSD/GPL");
1342 static int __init init (void)
1344 /* a real value would likely come through some id prom
1345 * or module option. this one takes at least two packets.
1347 strncpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
1348 serial [sizeof serial - 1] = 0;
1350 return usb_gadget_register_driver (&zero_driver);
1354 static void __exit cleanup (void)
1356 usb_gadget_unregister_driver (&zero_driver);
1358 module_exit (cleanup);