V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / usb / gadget / zero.c
blobfcde5d9c87df11eb6d867f7ff4a3bee075c96bc4
1 /*
2 * zero.c -- Gadget Zero, for USB development
4 * Copyright (C) 2003-2007 David Brownell
5 * All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Gadget Zero only needs two bulk endpoints, and is an example of how you
25 * can write a hardware-agnostic gadget driver running inside a USB device.
27 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
28 * affect most of the driver.
30 * Use it with the Linux host/master side "usbtest" driver to get a basic
31 * functional test of your device-side usb stack, or with "usb-skeleton".
33 * It supports two similar configurations. One sinks whatever the usb host
34 * writes, and in return sources zeroes. The other loops whatever the host
35 * writes back, so the host can read it. Module options include:
37 * buflen=N default N=4096, buffer size used
38 * qlen=N default N=32, how many buffers in the loopback queue
39 * loopdefault default false, list loopback config first
41 * Many drivers will only have one configuration, letting them be much
42 * simpler if they also don't support high speed operation (like this
43 * driver does).
45 * Why is *this* driver using two configurations, rather than setting up
46 * two interfaces with different functions? To help verify that multiple
47 * configuration infrastucture is working correctly; also, so that it can
48 * work with low capability USB controllers without four bulk endpoints.
51 /* #define VERBOSE_DEBUG */
53 #include <linux/kernel.h>
54 #include <linux/utsname.h>
55 #include <linux/device.h>
57 #include <linux/usb/ch9.h>
58 #include <linux/usb/gadget.h>
60 #include "gadget_chips.h"
63 /*-------------------------------------------------------------------------*/
65 #define DRIVER_VERSION "Lughnasadh, 2007"
67 static const char shortname [] = "zero";
68 static const char longname [] = "Gadget Zero";
70 static const char source_sink [] = "source and sink data";
71 static const char loopback [] = "loop input to output";
73 /*-------------------------------------------------------------------------*/
76 * driver assumes self-powered hardware, and
77 * has no way for users to trigger remote wakeup.
79 * this version autoconfigures as much as possible,
80 * which is reasonable for most "bulk-only" drivers.
82 static const char *EP_IN_NAME; /* source */
83 static const char *EP_OUT_NAME; /* sink */
85 /*-------------------------------------------------------------------------*/
87 /* big enough to hold our biggest descriptor */
88 #define USB_BUFSIZ 256
90 struct zero_dev {
91 spinlock_t lock;
92 struct usb_gadget *gadget;
93 struct usb_request *req; /* for control responses */
95 /* when configured, we have one of two configs:
96 * - source data (in to host) and sink it (out from host)
97 * - or loop it back (out from host back in to host)
99 u8 config;
100 struct usb_ep *in_ep, *out_ep;
102 /* autoresume timer */
103 struct timer_list resume;
106 #define DBG(d, fmt, args...) \
107 dev_dbg(&(d)->gadget->dev , fmt , ## args)
108 #define VDBG(d, fmt, args...) \
109 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
110 #define ERROR(d, fmt, args...) \
111 dev_err(&(d)->gadget->dev , fmt , ## args)
112 #define WARN(d, fmt, args...) \
113 dev_warn(&(d)->gadget->dev , fmt , ## args)
114 #define INFO(d, fmt, args...) \
115 dev_info(&(d)->gadget->dev , fmt , ## args)
117 /*-------------------------------------------------------------------------*/
119 static unsigned buflen = 4096;
120 static unsigned qlen = 32;
121 static unsigned pattern = 0;
123 module_param (buflen, uint, S_IRUGO);
124 module_param (qlen, uint, S_IRUGO);
125 module_param (pattern, uint, S_IRUGO|S_IWUSR);
128 * if it's nonzero, autoresume says how many seconds to wait
129 * before trying to wake up the host after suspend.
131 static unsigned autoresume = 0;
132 module_param (autoresume, uint, 0);
135 * Normally the "loopback" configuration is second (index 1) so
136 * it's not the default. Here's where to change that order, to
137 * work better with hosts where config changes are problematic.
138 * Or controllers (like superh) that only support one config.
140 static int loopdefault = 0;
142 module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
144 /*-------------------------------------------------------------------------*/
146 /* Thanks to NetChip Technologies for donating this product ID.
148 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
149 * Instead: allocate your own, using normal USB-IF procedures.
151 #ifndef CONFIG_USB_ZERO_HNPTEST
152 #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
153 #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
154 #else
155 #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
156 #define DRIVER_PRODUCT_NUM 0xbadd
157 #endif
159 /*-------------------------------------------------------------------------*/
162 * DESCRIPTORS ... most are static, but strings and (full)
163 * configuration descriptors are built on demand.
166 #define STRING_MANUFACTURER 25
167 #define STRING_PRODUCT 42
168 #define STRING_SERIAL 101
169 #define STRING_SOURCE_SINK 250
170 #define STRING_LOOPBACK 251
173 * This device advertises two configurations; these numbers work
174 * on a pxa250 as well as more flexible hardware.
176 #define CONFIG_SOURCE_SINK 3
177 #define CONFIG_LOOPBACK 2
179 static struct usb_device_descriptor
180 device_desc = {
181 .bLength = sizeof device_desc,
182 .bDescriptorType = USB_DT_DEVICE,
184 .bcdUSB = __constant_cpu_to_le16 (0x0200),
185 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
187 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
188 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
189 .iManufacturer = STRING_MANUFACTURER,
190 .iProduct = STRING_PRODUCT,
191 .iSerialNumber = STRING_SERIAL,
192 .bNumConfigurations = 2,
195 static struct usb_config_descriptor
196 source_sink_config = {
197 .bLength = sizeof source_sink_config,
198 .bDescriptorType = USB_DT_CONFIG,
200 /* compute wTotalLength on the fly */
201 .bNumInterfaces = 1,
202 .bConfigurationValue = CONFIG_SOURCE_SINK,
203 .iConfiguration = STRING_SOURCE_SINK,
204 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
205 .bMaxPower = 1, /* self-powered */
208 static struct usb_config_descriptor
209 loopback_config = {
210 .bLength = sizeof loopback_config,
211 .bDescriptorType = USB_DT_CONFIG,
213 /* compute wTotalLength on the fly */
214 .bNumInterfaces = 1,
215 .bConfigurationValue = CONFIG_LOOPBACK,
216 .iConfiguration = STRING_LOOPBACK,
217 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
218 .bMaxPower = 1, /* self-powered */
221 static struct usb_otg_descriptor
222 otg_descriptor = {
223 .bLength = sizeof otg_descriptor,
224 .bDescriptorType = USB_DT_OTG,
226 .bmAttributes = USB_OTG_SRP,
229 /* one interface in each configuration */
231 static const struct usb_interface_descriptor
232 source_sink_intf = {
233 .bLength = sizeof source_sink_intf,
234 .bDescriptorType = USB_DT_INTERFACE,
236 .bNumEndpoints = 2,
237 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
238 .iInterface = STRING_SOURCE_SINK,
241 static const struct usb_interface_descriptor
242 loopback_intf = {
243 .bLength = sizeof loopback_intf,
244 .bDescriptorType = USB_DT_INTERFACE,
246 .bNumEndpoints = 2,
247 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
248 .iInterface = STRING_LOOPBACK,
251 /* two full speed bulk endpoints; their use is config-dependent */
253 static struct usb_endpoint_descriptor
254 fs_source_desc = {
255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
258 .bEndpointAddress = USB_DIR_IN,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
262 static struct usb_endpoint_descriptor
263 fs_sink_desc = {
264 .bLength = USB_DT_ENDPOINT_SIZE,
265 .bDescriptorType = USB_DT_ENDPOINT,
267 .bEndpointAddress = USB_DIR_OUT,
268 .bmAttributes = USB_ENDPOINT_XFER_BULK,
271 static const struct usb_descriptor_header *fs_source_sink_function [] = {
272 (struct usb_descriptor_header *) &otg_descriptor,
273 (struct usb_descriptor_header *) &source_sink_intf,
274 (struct usb_descriptor_header *) &fs_sink_desc,
275 (struct usb_descriptor_header *) &fs_source_desc,
276 NULL,
279 static const struct usb_descriptor_header *fs_loopback_function [] = {
280 (struct usb_descriptor_header *) &otg_descriptor,
281 (struct usb_descriptor_header *) &loopback_intf,
282 (struct usb_descriptor_header *) &fs_sink_desc,
283 (struct usb_descriptor_header *) &fs_source_desc,
284 NULL,
288 * usb 2.0 devices need to expose both high speed and full speed
289 * descriptors, unless they only run at full speed.
291 * that means alternate endpoint descriptors (bigger packets)
292 * and a "device qualifier" ... plus more construction options
293 * for the config descriptor.
296 static struct usb_endpoint_descriptor
297 hs_source_desc = {
298 .bLength = USB_DT_ENDPOINT_SIZE,
299 .bDescriptorType = USB_DT_ENDPOINT,
301 .bmAttributes = USB_ENDPOINT_XFER_BULK,
302 .wMaxPacketSize = __constant_cpu_to_le16 (512),
305 static struct usb_endpoint_descriptor
306 hs_sink_desc = {
307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
310 .bmAttributes = USB_ENDPOINT_XFER_BULK,
311 .wMaxPacketSize = __constant_cpu_to_le16 (512),
314 static struct usb_qualifier_descriptor
315 dev_qualifier = {
316 .bLength = sizeof dev_qualifier,
317 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
319 .bcdUSB = __constant_cpu_to_le16 (0x0200),
320 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
322 .bNumConfigurations = 2,
325 static const struct usb_descriptor_header *hs_source_sink_function [] = {
326 (struct usb_descriptor_header *) &otg_descriptor,
327 (struct usb_descriptor_header *) &source_sink_intf,
328 (struct usb_descriptor_header *) &hs_source_desc,
329 (struct usb_descriptor_header *) &hs_sink_desc,
330 NULL,
333 static const struct usb_descriptor_header *hs_loopback_function [] = {
334 (struct usb_descriptor_header *) &otg_descriptor,
335 (struct usb_descriptor_header *) &loopback_intf,
336 (struct usb_descriptor_header *) &hs_source_desc,
337 (struct usb_descriptor_header *) &hs_sink_desc,
338 NULL,
341 /* maxpacket and other transfer characteristics vary by speed. */
342 static inline struct usb_endpoint_descriptor *
343 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
344 struct usb_endpoint_descriptor *fs)
346 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
347 return hs;
348 return fs;
351 static char manufacturer[50];
353 /* default serial number takes at least two packets */
354 static char serial[] = "0123456789.0123456789.0123456789";
357 /* static strings, in UTF-8 */
358 static struct usb_string strings [] = {
359 { STRING_MANUFACTURER, manufacturer, },
360 { STRING_PRODUCT, longname, },
361 { STRING_SERIAL, serial, },
362 { STRING_LOOPBACK, loopback, },
363 { STRING_SOURCE_SINK, source_sink, },
364 { } /* end of list */
367 static struct usb_gadget_strings stringtab = {
368 .language = 0x0409, /* en-us */
369 .strings = strings,
373 * config descriptors are also handcrafted. these must agree with code
374 * that sets configurations, and with code managing interfaces and their
375 * altsettings. other complexity may come from:
377 * - high speed support, including "other speed config" rules
378 * - multiple configurations
379 * - interfaces with alternate settings
380 * - embedded class or vendor-specific descriptors
382 * this handles high speed, and has a second config that could as easily
383 * have been an alternate interface setting (on most hardware).
385 * NOTE: to demonstrate (and test) more USB capabilities, this driver
386 * should include an altsetting to test interrupt transfers, including
387 * high bandwidth modes at high speed. (Maybe work like Intel's test
388 * device?)
390 static int
391 config_buf (struct usb_gadget *gadget,
392 u8 *buf, u8 type, unsigned index)
394 int is_source_sink;
395 int len;
396 const struct usb_descriptor_header **function;
397 int hs = 0;
399 /* two configurations will always be index 0 and index 1 */
400 if (index > 1)
401 return -EINVAL;
402 is_source_sink = loopdefault ? (index == 1) : (index == 0);
404 if (gadget_is_dualspeed(gadget)) {
405 hs = (gadget->speed == USB_SPEED_HIGH);
406 if (type == USB_DT_OTHER_SPEED_CONFIG)
407 hs = !hs;
409 if (hs)
410 function = is_source_sink
411 ? hs_source_sink_function
412 : hs_loopback_function;
413 else
414 function = is_source_sink
415 ? fs_source_sink_function
416 : fs_loopback_function;
418 /* for now, don't advertise srp-only devices */
419 if (!gadget_is_otg(gadget))
420 function++;
422 len = usb_gadget_config_buf (is_source_sink
423 ? &source_sink_config
424 : &loopback_config,
425 buf, USB_BUFSIZ, function);
426 if (len < 0)
427 return len;
428 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
429 return len;
432 /*-------------------------------------------------------------------------*/
434 static struct usb_request *
435 alloc_ep_req (struct usb_ep *ep, unsigned length)
437 struct usb_request *req;
439 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
440 if (req) {
441 req->length = length;
442 req->buf = kmalloc(length, GFP_ATOMIC);
443 if (!req->buf) {
444 usb_ep_free_request (ep, req);
445 req = NULL;
448 return req;
451 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
453 kfree(req->buf);
454 usb_ep_free_request (ep, req);
457 /*-------------------------------------------------------------------------*/
460 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripherals,
461 * this just sinks bulk packets OUT to the peripheral and sources them IN
462 * to the host, optionally with specific data patterns.
464 * In terms of control messaging, this supports all the standard requests
465 * plus two that support control-OUT tests.
467 * Note that because this doesn't queue more than one request at a time,
468 * some other function must be used to test queueing logic. The network
469 * link (g_ether) is probably the best option for that.
472 /* optionally require specific source/sink data patterns */
474 static int
475 check_read_data (
476 struct zero_dev *dev,
477 struct usb_ep *ep,
478 struct usb_request *req
481 unsigned i;
482 u8 *buf = req->buf;
484 for (i = 0; i < req->actual; i++, buf++) {
485 switch (pattern) {
486 /* all-zeroes has no synchronization issues */
487 case 0:
488 if (*buf == 0)
489 continue;
490 break;
491 /* mod63 stays in sync with short-terminated transfers,
492 * or otherwise when host and gadget agree on how large
493 * each usb transfer request should be. resync is done
494 * with set_interface or set_config.
496 case 1:
497 if (*buf == (u8)(i % 63))
498 continue;
499 break;
501 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
502 usb_ep_set_halt (ep);
503 return -EINVAL;
505 return 0;
508 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
510 unsigned i;
511 u8 *buf = req->buf;
513 switch (pattern) {
514 case 0:
515 memset (req->buf, 0, req->length);
516 break;
517 case 1:
518 for (i = 0; i < req->length; i++)
519 *buf++ = (u8) (i % 63);
520 break;
524 /* if there is only one request in the queue, there'll always be an
525 * irq delay between end of one request and start of the next.
526 * that prevents using hardware dma queues.
528 static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
530 struct zero_dev *dev = ep->driver_data;
531 int status = req->status;
533 switch (status) {
535 case 0: /* normal completion? */
536 if (ep == dev->out_ep) {
537 check_read_data (dev, ep, req);
538 memset (req->buf, 0x55, req->length);
539 } else
540 reinit_write_data(ep, req);
541 break;
543 /* this endpoint is normally active while we're configured */
544 case -ECONNABORTED: /* hardware forced ep reset */
545 case -ECONNRESET: /* request dequeued */
546 case -ESHUTDOWN: /* disconnect from host */
547 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
548 req->actual, req->length);
549 if (ep == dev->out_ep)
550 check_read_data (dev, ep, req);
551 free_ep_req (ep, req);
552 return;
554 case -EOVERFLOW: /* buffer overrun on read means that
555 * we didn't provide a big enough
556 * buffer.
558 default:
559 #if 1
560 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
561 status, req->actual, req->length);
562 #endif
563 case -EREMOTEIO: /* short read */
564 break;
567 status = usb_ep_queue (ep, req, GFP_ATOMIC);
568 if (status) {
569 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
570 ep->name, req->length, status);
571 usb_ep_set_halt (ep);
572 /* FIXME recover later ... somehow */
576 static struct usb_request *source_sink_start_ep(struct usb_ep *ep)
578 struct usb_request *req;
579 int status;
581 req = alloc_ep_req (ep, buflen);
582 if (!req)
583 return NULL;
585 memset (req->buf, 0, req->length);
586 req->complete = source_sink_complete;
588 if (strcmp (ep->name, EP_IN_NAME) == 0)
589 reinit_write_data(ep, req);
590 else
591 memset (req->buf, 0x55, req->length);
593 status = usb_ep_queue(ep, req, GFP_ATOMIC);
594 if (status) {
595 struct zero_dev *dev = ep->driver_data;
597 ERROR (dev, "start %s --> %d\n", ep->name, status);
598 free_ep_req (ep, req);
599 req = NULL;
602 return req;
605 static int set_source_sink_config(struct zero_dev *dev)
607 int result = 0;
608 struct usb_ep *ep;
609 struct usb_gadget *gadget = dev->gadget;
611 gadget_for_each_ep (ep, gadget) {
612 const struct usb_endpoint_descriptor *d;
614 /* one endpoint writes (sources) zeroes in (to the host) */
615 if (strcmp (ep->name, EP_IN_NAME) == 0) {
616 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
617 result = usb_ep_enable (ep, d);
618 if (result == 0) {
619 ep->driver_data = dev;
620 if (source_sink_start_ep(ep) != NULL) {
621 dev->in_ep = ep;
622 continue;
624 usb_ep_disable (ep);
625 result = -EIO;
628 /* one endpoint reads (sinks) anything out (from the host) */
629 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
630 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
631 result = usb_ep_enable (ep, d);
632 if (result == 0) {
633 ep->driver_data = dev;
634 if (source_sink_start_ep(ep) != NULL) {
635 dev->out_ep = ep;
636 continue;
638 usb_ep_disable (ep);
639 result = -EIO;
642 /* ignore any other endpoints */
643 } else
644 continue;
646 /* stop on error */
647 ERROR (dev, "can't start %s, result %d\n", ep->name, result);
648 break;
650 if (result == 0)
651 DBG (dev, "buflen %d\n", buflen);
653 /* caller is responsible for cleanup on error */
654 return result;
657 /*-------------------------------------------------------------------------*/
659 static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
661 struct zero_dev *dev = ep->driver_data;
662 int status = req->status;
664 switch (status) {
666 case 0: /* normal completion? */
667 if (ep == dev->out_ep) {
668 /* loop this OUT packet back IN to the host */
669 req->zero = (req->actual < req->length);
670 req->length = req->actual;
671 status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
672 if (status == 0)
673 return;
675 /* "should never get here" */
676 ERROR (dev, "can't loop %s to %s: %d\n",
677 ep->name, dev->in_ep->name,
678 status);
681 /* queue the buffer for some later OUT packet */
682 req->length = buflen;
683 status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
684 if (status == 0)
685 return;
687 /* "should never get here" */
688 /* FALLTHROUGH */
690 default:
691 ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
692 status, req->actual, req->length);
693 /* FALLTHROUGH */
695 /* NOTE: since this driver doesn't maintain an explicit record
696 * of requests it submitted (just maintains qlen count), we
697 * rely on the hardware driver to clean up on disconnect or
698 * endpoint disable.
700 case -ECONNABORTED: /* hardware forced ep reset */
701 case -ECONNRESET: /* request dequeued */
702 case -ESHUTDOWN: /* disconnect from host */
703 free_ep_req (ep, req);
704 return;
708 static int set_loopback_config(struct zero_dev *dev)
710 int result = 0;
711 struct usb_ep *ep;
712 struct usb_gadget *gadget = dev->gadget;
714 gadget_for_each_ep (ep, gadget) {
715 const struct usb_endpoint_descriptor *d;
717 /* one endpoint writes data back IN to the host */
718 if (strcmp (ep->name, EP_IN_NAME) == 0) {
719 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
720 result = usb_ep_enable (ep, d);
721 if (result == 0) {
722 ep->driver_data = dev;
723 dev->in_ep = ep;
724 continue;
727 /* one endpoint just reads OUT packets */
728 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
729 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
730 result = usb_ep_enable (ep, d);
731 if (result == 0) {
732 ep->driver_data = dev;
733 dev->out_ep = ep;
734 continue;
737 /* ignore any other endpoints */
738 } else
739 continue;
741 /* stop on error */
742 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
743 break;
746 /* allocate a bunch of read buffers and queue them all at once.
747 * we buffer at most 'qlen' transfers; fewer if any need more
748 * than 'buflen' bytes each.
750 if (result == 0) {
751 struct usb_request *req;
752 unsigned i;
754 ep = dev->out_ep;
755 for (i = 0; i < qlen && result == 0; i++) {
756 req = alloc_ep_req (ep, buflen);
757 if (req) {
758 req->complete = loopback_complete;
759 result = usb_ep_queue (ep, req, GFP_ATOMIC);
760 if (result)
761 DBG (dev, "%s queue req --> %d\n",
762 ep->name, result);
763 } else
764 result = -ENOMEM;
767 if (result == 0)
768 DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
770 /* caller is responsible for cleanup on error */
771 return result;
774 /*-------------------------------------------------------------------------*/
776 static void zero_reset_config (struct zero_dev *dev)
778 if (dev->config == 0)
779 return;
781 DBG (dev, "reset config\n");
783 /* just disable endpoints, forcing completion of pending i/o.
784 * all our completion handlers free their requests in this case.
786 if (dev->in_ep) {
787 usb_ep_disable (dev->in_ep);
788 dev->in_ep = NULL;
790 if (dev->out_ep) {
791 usb_ep_disable (dev->out_ep);
792 dev->out_ep = NULL;
794 dev->config = 0;
795 del_timer (&dev->resume);
798 /* change our operational config. this code must agree with the code
799 * that returns config descriptors, and altsetting code.
801 * it's also responsible for power management interactions. some
802 * configurations might not work with our current power sources.
804 * note that some device controller hardware will constrain what this
805 * code can do, perhaps by disallowing more than one configuration or
806 * by limiting configuration choices (like the pxa2xx).
808 static int zero_set_config(struct zero_dev *dev, unsigned number)
810 int result = 0;
811 struct usb_gadget *gadget = dev->gadget;
813 if (number == dev->config)
814 return 0;
816 if (gadget_is_sa1100 (gadget) && dev->config) {
817 /* tx fifo is full, but we can't clear it...*/
818 ERROR(dev, "can't change configurations\n");
819 return -ESPIPE;
821 zero_reset_config (dev);
823 switch (number) {
824 case CONFIG_SOURCE_SINK:
825 result = set_source_sink_config(dev);
826 break;
827 case CONFIG_LOOPBACK:
828 result = set_loopback_config(dev);
829 break;
830 default:
831 result = -EINVAL;
832 /* FALL THROUGH */
833 case 0:
834 return result;
837 if (!result && (!dev->in_ep || !dev->out_ep))
838 result = -ENODEV;
839 if (result)
840 zero_reset_config (dev);
841 else {
842 char *speed;
844 switch (gadget->speed) {
845 case USB_SPEED_LOW: speed = "low"; break;
846 case USB_SPEED_FULL: speed = "full"; break;
847 case USB_SPEED_HIGH: speed = "high"; break;
848 default: speed = "?"; break;
851 dev->config = number;
852 INFO (dev, "%s speed config #%d: %s\n", speed, number,
853 (number == CONFIG_SOURCE_SINK)
854 ? source_sink : loopback);
856 return result;
859 /*-------------------------------------------------------------------------*/
861 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
863 if (req->status || req->actual != req->length)
864 DBG ((struct zero_dev *) ep->driver_data,
865 "setup complete --> %d, %d/%d\n",
866 req->status, req->actual, req->length);
870 * The setup() callback implements all the ep0 functionality that's
871 * not handled lower down, in hardware or the hardware driver (like
872 * device and endpoint feature flags, and their status). It's all
873 * housekeeping for the gadget function we're implementing. Most of
874 * the work is in config-specific setup.
876 static int
877 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
879 struct zero_dev *dev = get_gadget_data (gadget);
880 struct usb_request *req = dev->req;
881 int value = -EOPNOTSUPP;
882 u16 w_index = le16_to_cpu(ctrl->wIndex);
883 u16 w_value = le16_to_cpu(ctrl->wValue);
884 u16 w_length = le16_to_cpu(ctrl->wLength);
886 /* usually this stores reply data in the pre-allocated ep0 buffer,
887 * but config change events will reconfigure hardware.
889 req->zero = 0;
890 switch (ctrl->bRequest) {
892 case USB_REQ_GET_DESCRIPTOR:
893 if (ctrl->bRequestType != USB_DIR_IN)
894 goto unknown;
895 switch (w_value >> 8) {
897 case USB_DT_DEVICE:
898 value = min (w_length, (u16) sizeof device_desc);
899 memcpy (req->buf, &device_desc, value);
900 break;
901 case USB_DT_DEVICE_QUALIFIER:
902 if (!gadget_is_dualspeed(gadget))
903 break;
904 value = min (w_length, (u16) sizeof dev_qualifier);
905 memcpy (req->buf, &dev_qualifier, value);
906 break;
908 case USB_DT_OTHER_SPEED_CONFIG:
909 if (!gadget_is_dualspeed(gadget))
910 break;
911 // FALLTHROUGH
912 case USB_DT_CONFIG:
913 value = config_buf (gadget, req->buf,
914 w_value >> 8,
915 w_value & 0xff);
916 if (value >= 0)
917 value = min (w_length, (u16) value);
918 break;
920 case USB_DT_STRING:
921 /* wIndex == language code.
922 * this driver only handles one language, you can
923 * add string tables for other languages, using
924 * any UTF-8 characters
926 value = usb_gadget_get_string (&stringtab,
927 w_value & 0xff, req->buf);
928 if (value >= 0)
929 value = min (w_length, (u16) value);
930 break;
932 break;
934 /* currently two configs, two speeds */
935 case USB_REQ_SET_CONFIGURATION:
936 if (ctrl->bRequestType != 0)
937 goto unknown;
938 if (gadget->a_hnp_support)
939 DBG (dev, "HNP available\n");
940 else if (gadget->a_alt_hnp_support)
941 DBG (dev, "HNP needs a different root port\n");
942 else
943 VDBG (dev, "HNP inactive\n");
944 spin_lock (&dev->lock);
945 value = zero_set_config(dev, w_value);
946 spin_unlock (&dev->lock);
947 break;
948 case USB_REQ_GET_CONFIGURATION:
949 if (ctrl->bRequestType != USB_DIR_IN)
950 goto unknown;
951 *(u8 *)req->buf = dev->config;
952 value = min (w_length, (u16) 1);
953 break;
955 /* until we add altsetting support, or other interfaces,
956 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
957 * and already killed pending endpoint I/O.
959 case USB_REQ_SET_INTERFACE:
960 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
961 goto unknown;
962 spin_lock (&dev->lock);
963 if (dev->config && w_index == 0 && w_value == 0) {
964 u8 config = dev->config;
966 /* resets interface configuration, forgets about
967 * previous transaction state (queued bufs, etc)
968 * and re-inits endpoint state (toggle etc)
969 * no response queued, just zero status == success.
970 * if we had more than one interface we couldn't
971 * use this "reset the config" shortcut.
973 zero_reset_config (dev);
974 zero_set_config(dev, config);
975 value = 0;
977 spin_unlock (&dev->lock);
978 break;
979 case USB_REQ_GET_INTERFACE:
980 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
981 goto unknown;
982 if (!dev->config)
983 break;
984 if (w_index != 0) {
985 value = -EDOM;
986 break;
988 *(u8 *)req->buf = 0;
989 value = min (w_length, (u16) 1);
990 break;
993 * These are the same vendor-specific requests supported by
994 * Intel's USB 2.0 compliance test devices. We exceed that
995 * device spec by allowing multiple-packet requests.
997 case 0x5b: /* control WRITE test -- fill the buffer */
998 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
999 goto unknown;
1000 if (w_value || w_index)
1001 break;
1002 /* just read that many bytes into the buffer */
1003 if (w_length > USB_BUFSIZ)
1004 break;
1005 value = w_length;
1006 break;
1007 case 0x5c: /* control READ test -- return the buffer */
1008 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1009 goto unknown;
1010 if (w_value || w_index)
1011 break;
1012 /* expect those bytes are still in the buffer; send back */
1013 if (w_length > USB_BUFSIZ
1014 || w_length != req->length)
1015 break;
1016 value = w_length;
1017 break;
1019 default:
1020 unknown:
1021 VDBG (dev,
1022 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1023 ctrl->bRequestType, ctrl->bRequest,
1024 w_value, w_index, w_length);
1027 /* respond with data transfer before status phase? */
1028 if (value >= 0) {
1029 req->length = value;
1030 req->zero = value < w_length;
1031 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1032 if (value < 0) {
1033 DBG (dev, "ep_queue --> %d\n", value);
1034 req->status = 0;
1035 zero_setup_complete (gadget->ep0, req);
1039 /* device either stalls (value < 0) or reports success */
1040 return value;
1043 static void
1044 zero_disconnect (struct usb_gadget *gadget)
1046 struct zero_dev *dev = get_gadget_data (gadget);
1047 unsigned long flags;
1049 spin_lock_irqsave (&dev->lock, flags);
1050 zero_reset_config (dev);
1052 /* a more significant application might have some non-usb
1053 * activities to quiesce here, saving resources like power
1054 * or pushing the notification up a network stack.
1056 spin_unlock_irqrestore (&dev->lock, flags);
1058 /* next we may get setup() calls to enumerate new connections;
1059 * or an unbind() during shutdown (including removing module).
1063 static void
1064 zero_autoresume (unsigned long _dev)
1066 struct zero_dev *dev = (struct zero_dev *) _dev;
1067 int status;
1069 /* normally the host would be woken up for something
1070 * more significant than just a timer firing...
1072 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1073 status = usb_gadget_wakeup (dev->gadget);
1074 DBG (dev, "wakeup --> %d\n", status);
1078 /*-------------------------------------------------------------------------*/
1080 static void /* __init_or_exit */
1081 zero_unbind (struct usb_gadget *gadget)
1083 struct zero_dev *dev = get_gadget_data (gadget);
1085 DBG (dev, "unbind\n");
1087 /* we've already been disconnected ... no i/o is active */
1088 if (dev->req) {
1089 dev->req->length = USB_BUFSIZ;
1090 free_ep_req (gadget->ep0, dev->req);
1092 del_timer_sync (&dev->resume);
1093 kfree (dev);
1094 set_gadget_data (gadget, NULL);
1097 static int __init
1098 zero_bind (struct usb_gadget *gadget)
1100 struct zero_dev *dev;
1101 struct usb_ep *ep;
1102 int gcnum;
1104 /* FIXME this can't yet work right with SH ... it has only
1105 * one configuration, numbered one.
1107 if (gadget_is_sh(gadget))
1108 return -ENODEV;
1110 /* Bulk-only drivers like this one SHOULD be able to
1111 * autoconfigure on any sane usb controller driver,
1112 * but there may also be important quirks to address.
1114 usb_ep_autoconfig_reset (gadget);
1115 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1116 if (!ep) {
1117 autoconf_fail:
1118 printk (KERN_ERR "%s: can't autoconfigure on %s\n",
1119 shortname, gadget->name);
1120 return -ENODEV;
1122 EP_IN_NAME = ep->name;
1123 ep->driver_data = ep; /* claim */
1125 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1126 if (!ep)
1127 goto autoconf_fail;
1128 EP_OUT_NAME = ep->name;
1129 ep->driver_data = ep; /* claim */
1131 gcnum = usb_gadget_controller_number (gadget);
1132 if (gcnum >= 0)
1133 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
1134 else {
1135 /* gadget zero is so simple (for now, no altsettings) that
1136 * it SHOULD NOT have problems with bulk-capable hardware.
1137 * so warn about unrcognized controllers, don't panic.
1139 * things like configuration and altsetting numbering
1140 * can need hardware-specific attention though.
1142 printk (KERN_WARNING "%s: controller '%s' not recognized\n",
1143 shortname, gadget->name);
1144 device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
1148 /* ok, we made sense of the hardware ... */
1149 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1150 if (!dev)
1151 return -ENOMEM;
1152 spin_lock_init (&dev->lock);
1153 dev->gadget = gadget;
1154 set_gadget_data (gadget, dev);
1156 /* preallocate control response and buffer */
1157 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1158 if (!dev->req)
1159 goto enomem;
1160 dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1161 if (!dev->req->buf)
1162 goto enomem;
1164 dev->req->complete = zero_setup_complete;
1166 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1168 if (gadget_is_dualspeed(gadget)) {
1169 /* assume ep0 uses the same value for both speeds ... */
1170 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1172 /* and that all endpoints are dual-speed */
1173 hs_source_desc.bEndpointAddress =
1174 fs_source_desc.bEndpointAddress;
1175 hs_sink_desc.bEndpointAddress =
1176 fs_sink_desc.bEndpointAddress;
1179 if (gadget_is_otg(gadget)) {
1180 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1181 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1182 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1185 usb_gadget_set_selfpowered (gadget);
1187 init_timer (&dev->resume);
1188 dev->resume.function = zero_autoresume;
1189 dev->resume.data = (unsigned long) dev;
1190 if (autoresume) {
1191 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1192 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1195 gadget->ep0->driver_data = dev;
1197 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1198 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1199 EP_OUT_NAME, EP_IN_NAME);
1201 snprintf (manufacturer, sizeof manufacturer, "%s %s with %s",
1202 init_utsname()->sysname, init_utsname()->release,
1203 gadget->name);
1205 return 0;
1207 enomem:
1208 zero_unbind (gadget);
1209 return -ENOMEM;
1212 /*-------------------------------------------------------------------------*/
1214 static void
1215 zero_suspend (struct usb_gadget *gadget)
1217 struct zero_dev *dev = get_gadget_data (gadget);
1219 if (gadget->speed == USB_SPEED_UNKNOWN)
1220 return;
1222 if (autoresume) {
1223 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1224 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1225 } else
1226 DBG (dev, "suspend\n");
1229 static void
1230 zero_resume (struct usb_gadget *gadget)
1232 struct zero_dev *dev = get_gadget_data (gadget);
1234 DBG (dev, "resume\n");
1235 del_timer (&dev->resume);
1239 /*-------------------------------------------------------------------------*/
1241 static struct usb_gadget_driver zero_driver = {
1242 #ifdef CONFIG_USB_GADGET_DUALSPEED
1243 .speed = USB_SPEED_HIGH,
1244 #else
1245 .speed = USB_SPEED_FULL,
1246 #endif
1247 .function = (char *) longname,
1248 .bind = zero_bind,
1249 .unbind = __exit_p(zero_unbind),
1251 .setup = zero_setup,
1252 .disconnect = zero_disconnect,
1254 .suspend = zero_suspend,
1255 .resume = zero_resume,
1257 .driver = {
1258 .name = (char *) shortname,
1259 .owner = THIS_MODULE,
1263 MODULE_AUTHOR("David Brownell");
1264 MODULE_LICENSE("GPL");
1267 static int __init init (void)
1269 return usb_gadget_register_driver (&zero_driver);
1271 module_init (init);
1273 static void __exit cleanup (void)
1275 usb_gadget_unregister_driver (&zero_driver);
1277 module_exit (cleanup);