blob: 68e3d8f5da8947cb8d28cf17c2056b8829d04190 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * zero.c -- Gadget Zero, for USB development
3 *
4 * Copyright (C) 2003-2004 David Brownell
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
19 *
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
23 * later version.
24 *
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.
36 */
37
38
39/*
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.
42 *
43 * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44 * affect most of the driver.
45 *
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".
48 *
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:
52 *
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
56 *
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
59 * driver does).
60 */
61
62#define DEBUG 1
63// #define VERBOSE
64
65#include <linux/config.h>
66#include <linux/module.h>
67#include <linux/kernel.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/utsname.h>
79#include <linux/device.h>
80#include <linux/moduleparam.h>
81
82#include <asm/byteorder.h>
83#include <asm/io.h>
84#include <asm/irq.h>
85#include <asm/system.h>
86#include <asm/unaligned.h>
87
88#include <linux/usb_ch9.h>
89#include <linux/usb_gadget.h>
90
91#include "gadget_chips.h"
92
93
94/*-------------------------------------------------------------------------*/
95
96#define DRIVER_VERSION "St Patrick's Day 2004"
97
98static const char shortname [] = "zero";
99static const char longname [] = "Gadget Zero";
100
101static const char source_sink [] = "source and sink data";
102static const char loopback [] = "loop input to output";
103
104/*-------------------------------------------------------------------------*/
105
106/*
107 * driver assumes self-powered hardware, and
108 * has no way for users to trigger remote wakeup.
109 *
110 * this version autoconfigures as much as possible,
111 * which is reasonable for most "bulk-only" drivers.
112 */
113static const char *EP_IN_NAME; /* source */
114static const char *EP_OUT_NAME; /* sink */
115
116/*-------------------------------------------------------------------------*/
117
118/* big enough to hold our biggest descriptor */
119#define USB_BUFSIZ 256
120
121struct zero_dev {
122 spinlock_t lock;
123 struct usb_gadget *gadget;
124 struct usb_request *req; /* for control responses */
125
126 /* when configured, we have one of two configs:
127 * - source data (in to host) and sink it (out from host)
128 * - or loop it back (out from host back in to host)
129 */
130 u8 config;
131 struct usb_ep *in_ep, *out_ep;
132
133 /* autoresume timer */
134 struct timer_list resume;
135};
136
137#define xprintk(d,level,fmt,args...) \
138 dev_printk(level , &(d)->gadget->dev , fmt , ## args)
139
140#ifdef DEBUG
141#define DBG(dev,fmt,args...) \
142 xprintk(dev , KERN_DEBUG , fmt , ## args)
143#else
144#define DBG(dev,fmt,args...) \
145 do { } while (0)
146#endif /* DEBUG */
147
148#ifdef VERBOSE
149#define VDBG DBG
150#else
151#define VDBG(dev,fmt,args...) \
152 do { } while (0)
153#endif /* VERBOSE */
154
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)
161
162/*-------------------------------------------------------------------------*/
163
164static unsigned buflen = 4096;
165static unsigned qlen = 32;
166static unsigned pattern = 0;
167
David Brownell69396dc2006-01-20 14:38:49 -0800168module_param (buflen, uint, S_IRUGO);
169module_param (qlen, uint, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170module_param (pattern, uint, S_IRUGO|S_IWUSR);
171
172/*
173 * if it's nonzero, autoresume says how many seconds to wait
174 * before trying to wake up the host after suspend.
175 */
176static unsigned autoresume = 0;
177module_param (autoresume, uint, 0);
178
179/*
180 * Normally the "loopback" configuration is second (index 1) so
181 * it's not the default. Here's where to change that order, to
182 * work better with hosts where config changes are problematic.
183 * Or controllers (like superh) that only support one config.
184 */
185static int loopdefault = 0;
186
187module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
188
189/*-------------------------------------------------------------------------*/
190
191/* Thanks to NetChip Technologies for donating this product ID.
192 *
193 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
194 * Instead: allocate your own, using normal USB-IF procedures.
195 */
196#ifndef CONFIG_USB_ZERO_HNPTEST
197#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
198#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
199#else
200#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
201#define DRIVER_PRODUCT_NUM 0xbadd
202#endif
203
204/*-------------------------------------------------------------------------*/
205
206/*
207 * DESCRIPTORS ... most are static, but strings and (full)
208 * configuration descriptors are built on demand.
209 */
210
211#define STRING_MANUFACTURER 25
212#define STRING_PRODUCT 42
213#define STRING_SERIAL 101
214#define STRING_SOURCE_SINK 250
215#define STRING_LOOPBACK 251
216
217/*
218 * This device advertises two configurations; these numbers work
219 * on a pxa250 as well as more flexible hardware.
220 */
221#define CONFIG_SOURCE_SINK 3
222#define CONFIG_LOOPBACK 2
223
224static struct usb_device_descriptor
225device_desc = {
226 .bLength = sizeof device_desc,
227 .bDescriptorType = USB_DT_DEVICE,
228
229 .bcdUSB = __constant_cpu_to_le16 (0x0200),
230 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
231
232 .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
233 .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
234 .iManufacturer = STRING_MANUFACTURER,
235 .iProduct = STRING_PRODUCT,
236 .iSerialNumber = STRING_SERIAL,
237 .bNumConfigurations = 2,
238};
239
240static struct usb_config_descriptor
241source_sink_config = {
242 .bLength = sizeof source_sink_config,
243 .bDescriptorType = USB_DT_CONFIG,
244
245 /* compute wTotalLength on the fly */
246 .bNumInterfaces = 1,
247 .bConfigurationValue = CONFIG_SOURCE_SINK,
248 .iConfiguration = STRING_SOURCE_SINK,
249 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
250 .bMaxPower = 1, /* self-powered */
251};
252
253static struct usb_config_descriptor
254loopback_config = {
255 .bLength = sizeof loopback_config,
256 .bDescriptorType = USB_DT_CONFIG,
257
258 /* compute wTotalLength on the fly */
259 .bNumInterfaces = 1,
260 .bConfigurationValue = CONFIG_LOOPBACK,
261 .iConfiguration = STRING_LOOPBACK,
262 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
263 .bMaxPower = 1, /* self-powered */
264};
265
266static struct usb_otg_descriptor
267otg_descriptor = {
268 .bLength = sizeof otg_descriptor,
269 .bDescriptorType = USB_DT_OTG,
270
271 .bmAttributes = USB_OTG_SRP,
272};
273
274/* one interface in each configuration */
275
276static const struct usb_interface_descriptor
277source_sink_intf = {
278 .bLength = sizeof source_sink_intf,
279 .bDescriptorType = USB_DT_INTERFACE,
280
281 .bNumEndpoints = 2,
282 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
283 .iInterface = STRING_SOURCE_SINK,
284};
285
286static const struct usb_interface_descriptor
287loopback_intf = {
288 .bLength = sizeof loopback_intf,
289 .bDescriptorType = USB_DT_INTERFACE,
290
291 .bNumEndpoints = 2,
292 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
293 .iInterface = STRING_LOOPBACK,
294};
295
296/* two full speed bulk endpoints; their use is config-dependent */
297
298static struct usb_endpoint_descriptor
299fs_source_desc = {
300 .bLength = USB_DT_ENDPOINT_SIZE,
301 .bDescriptorType = USB_DT_ENDPOINT,
302
303 .bEndpointAddress = USB_DIR_IN,
304 .bmAttributes = USB_ENDPOINT_XFER_BULK,
305};
306
307static struct usb_endpoint_descriptor
308fs_sink_desc = {
309 .bLength = USB_DT_ENDPOINT_SIZE,
310 .bDescriptorType = USB_DT_ENDPOINT,
311
312 .bEndpointAddress = USB_DIR_OUT,
313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
314};
315
316static const struct usb_descriptor_header *fs_source_sink_function [] = {
317 (struct usb_descriptor_header *) &otg_descriptor,
318 (struct usb_descriptor_header *) &source_sink_intf,
319 (struct usb_descriptor_header *) &fs_sink_desc,
320 (struct usb_descriptor_header *) &fs_source_desc,
321 NULL,
322};
323
324static const struct usb_descriptor_header *fs_loopback_function [] = {
325 (struct usb_descriptor_header *) &otg_descriptor,
326 (struct usb_descriptor_header *) &loopback_intf,
327 (struct usb_descriptor_header *) &fs_sink_desc,
328 (struct usb_descriptor_header *) &fs_source_desc,
329 NULL,
330};
331
332#ifdef CONFIG_USB_GADGET_DUALSPEED
333
334/*
335 * usb 2.0 devices need to expose both high speed and full speed
336 * descriptors, unless they only run at full speed.
337 *
338 * that means alternate endpoint descriptors (bigger packets)
339 * and a "device qualifier" ... plus more construction options
340 * for the config descriptor.
341 */
342
343static struct usb_endpoint_descriptor
344hs_source_desc = {
345 .bLength = USB_DT_ENDPOINT_SIZE,
346 .bDescriptorType = USB_DT_ENDPOINT,
347
348 .bmAttributes = USB_ENDPOINT_XFER_BULK,
349 .wMaxPacketSize = __constant_cpu_to_le16 (512),
350};
351
352static struct usb_endpoint_descriptor
353hs_sink_desc = {
354 .bLength = USB_DT_ENDPOINT_SIZE,
355 .bDescriptorType = USB_DT_ENDPOINT,
356
357 .bmAttributes = USB_ENDPOINT_XFER_BULK,
358 .wMaxPacketSize = __constant_cpu_to_le16 (512),
359};
360
361static struct usb_qualifier_descriptor
362dev_qualifier = {
363 .bLength = sizeof dev_qualifier,
364 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
365
366 .bcdUSB = __constant_cpu_to_le16 (0x0200),
367 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
368
369 .bNumConfigurations = 2,
370};
371
372static const struct usb_descriptor_header *hs_source_sink_function [] = {
373 (struct usb_descriptor_header *) &otg_descriptor,
374 (struct usb_descriptor_header *) &source_sink_intf,
375 (struct usb_descriptor_header *) &hs_source_desc,
376 (struct usb_descriptor_header *) &hs_sink_desc,
377 NULL,
378};
379
380static const struct usb_descriptor_header *hs_loopback_function [] = {
381 (struct usb_descriptor_header *) &otg_descriptor,
382 (struct usb_descriptor_header *) &loopback_intf,
383 (struct usb_descriptor_header *) &hs_source_desc,
384 (struct usb_descriptor_header *) &hs_sink_desc,
385 NULL,
386};
387
388/* maxpacket and other transfer characteristics vary by speed. */
389#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
390
391#else
392
393/* if there's no high speed support, maxpacket doesn't change. */
394#define ep_desc(g,hs,fs) fs
395
396#endif /* !CONFIG_USB_GADGET_DUALSPEED */
397
398static char manufacturer [50];
399static char serial [40];
400
401/* static strings, in UTF-8 */
402static struct usb_string strings [] = {
403 { STRING_MANUFACTURER, manufacturer, },
404 { STRING_PRODUCT, longname, },
405 { STRING_SERIAL, serial, },
406 { STRING_LOOPBACK, loopback, },
407 { STRING_SOURCE_SINK, source_sink, },
408 { } /* end of list */
409};
410
411static struct usb_gadget_strings stringtab = {
412 .language = 0x0409, /* en-us */
413 .strings = strings,
414};
415
416/*
417 * config descriptors are also handcrafted. these must agree with code
418 * that sets configurations, and with code managing interfaces and their
419 * altsettings. other complexity may come from:
420 *
421 * - high speed support, including "other speed config" rules
422 * - multiple configurations
423 * - interfaces with alternate settings
424 * - embedded class or vendor-specific descriptors
425 *
426 * this handles high speed, and has a second config that could as easily
427 * have been an alternate interface setting (on most hardware).
428 *
429 * NOTE: to demonstrate (and test) more USB capabilities, this driver
430 * should include an altsetting to test interrupt transfers, including
431 * high bandwidth modes at high speed. (Maybe work like Intel's test
432 * device?)
433 */
434static int
435config_buf (struct usb_gadget *gadget,
436 u8 *buf, u8 type, unsigned index)
437{
438 int is_source_sink;
439 int len;
440 const struct usb_descriptor_header **function;
441#ifdef CONFIG_USB_GADGET_DUALSPEED
442 int hs = (gadget->speed == USB_SPEED_HIGH);
443#endif
444
445 /* two configurations will always be index 0 and index 1 */
446 if (index > 1)
447 return -EINVAL;
448 is_source_sink = loopdefault ? (index == 1) : (index == 0);
449
450#ifdef CONFIG_USB_GADGET_DUALSPEED
451 if (type == USB_DT_OTHER_SPEED_CONFIG)
452 hs = !hs;
453 if (hs)
454 function = is_source_sink
455 ? hs_source_sink_function
456 : hs_loopback_function;
457 else
458#endif
459 function = is_source_sink
460 ? fs_source_sink_function
461 : fs_loopback_function;
462
463 /* for now, don't advertise srp-only devices */
464 if (!gadget->is_otg)
465 function++;
466
467 len = usb_gadget_config_buf (is_source_sink
468 ? &source_sink_config
469 : &loopback_config,
470 buf, USB_BUFSIZ, function);
471 if (len < 0)
472 return len;
473 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
474 return len;
475}
476
477/*-------------------------------------------------------------------------*/
478
479static struct usb_request *
480alloc_ep_req (struct usb_ep *ep, unsigned length)
481{
482 struct usb_request *req;
483
484 req = usb_ep_alloc_request (ep, GFP_ATOMIC);
485 if (req) {
486 req->length = length;
487 req->buf = usb_ep_alloc_buffer (ep, length,
488 &req->dma, GFP_ATOMIC);
489 if (!req->buf) {
490 usb_ep_free_request (ep, req);
491 req = NULL;
492 }
493 }
494 return req;
495}
496
497static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
498{
499 if (req->buf)
500 usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
501 usb_ep_free_request (ep, req);
502}
503
504/*-------------------------------------------------------------------------*/
505
506/* optionally require specific source/sink data patterns */
507
508static int
509check_read_data (
510 struct zero_dev *dev,
511 struct usb_ep *ep,
512 struct usb_request *req
513)
514{
515 unsigned i;
516 u8 *buf = req->buf;
517
518 for (i = 0; i < req->actual; i++, buf++) {
519 switch (pattern) {
520 /* all-zeroes has no synchronization issues */
521 case 0:
522 if (*buf == 0)
523 continue;
524 break;
525 /* mod63 stays in sync with short-terminated transfers,
526 * or otherwise when host and gadget agree on how large
527 * each usb transfer request should be. resync is done
528 * with set_interface or set_config.
529 */
530 case 1:
531 if (*buf == (u8)(i % 63))
532 continue;
533 break;
534 }
535 ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
536 usb_ep_set_halt (ep);
537 return -EINVAL;
538 }
539 return 0;
540}
541
542static void
543reinit_write_data (
544 struct zero_dev *dev,
545 struct usb_ep *ep,
546 struct usb_request *req
547)
548{
549 unsigned i;
550 u8 *buf = req->buf;
551
552 switch (pattern) {
553 case 0:
554 memset (req->buf, 0, req->length);
555 break;
556 case 1:
557 for (i = 0; i < req->length; i++)
558 *buf++ = (u8) (i % 63);
559 break;
560 }
561}
562
563/* if there is only one request in the queue, there'll always be an
564 * irq delay between end of one request and start of the next.
565 * that prevents using hardware dma queues.
566 */
567static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
568{
569 struct zero_dev *dev = ep->driver_data;
570 int status = req->status;
571
572 switch (status) {
573
574 case 0: /* normal completion? */
David Brownell35fcca42006-04-02 10:19:43 -0800575 if (ep == dev->out_ep) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 check_read_data (dev, ep, req);
David Brownell35fcca42006-04-02 10:19:43 -0800577 memset (req->buf, 0x55, req->length);
578 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 reinit_write_data (dev, ep, req);
580 break;
581
582 /* this endpoint is normally active while we're configured */
583 case -ECONNABORTED: /* hardware forced ep reset */
584 case -ECONNRESET: /* request dequeued */
585 case -ESHUTDOWN: /* disconnect from host */
586 VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
587 req->actual, req->length);
588 if (ep == dev->out_ep)
589 check_read_data (dev, ep, req);
590 free_ep_req (ep, req);
591 return;
592
593 case -EOVERFLOW: /* buffer overrun on read means that
594 * we didn't provide a big enough
595 * buffer.
596 */
597 default:
598#if 1
599 DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
600 status, req->actual, req->length);
601#endif
602 case -EREMOTEIO: /* short read */
603 break;
604 }
605
606 status = usb_ep_queue (ep, req, GFP_ATOMIC);
607 if (status) {
608 ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
609 ep->name, req->length, status);
610 usb_ep_set_halt (ep);
611 /* FIXME recover later ... somehow */
612 }
613}
614
615static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400616source_sink_start_ep (struct usb_ep *ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct usb_request *req;
619 int status;
620
621 req = alloc_ep_req (ep, buflen);
622 if (!req)
623 return NULL;
624
625 memset (req->buf, 0, req->length);
626 req->complete = source_sink_complete;
627
628 if (strcmp (ep->name, EP_IN_NAME) == 0)
629 reinit_write_data (ep->driver_data, ep, req);
David Brownell35fcca42006-04-02 10:19:43 -0800630 else
631 memset (req->buf, 0x55, req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 status = usb_ep_queue (ep, req, gfp_flags);
634 if (status) {
635 struct zero_dev *dev = ep->driver_data;
636
637 ERROR (dev, "start %s --> %d\n", ep->name, status);
638 free_ep_req (ep, req);
639 req = NULL;
640 }
641
642 return req;
643}
644
645static int
Al Viro55016f12005-10-21 03:21:58 -0400646set_source_sink_config (struct zero_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int result = 0;
649 struct usb_ep *ep;
650 struct usb_gadget *gadget = dev->gadget;
651
652 gadget_for_each_ep (ep, gadget) {
653 const struct usb_endpoint_descriptor *d;
654
655 /* one endpoint writes (sources) zeroes in (to the host) */
656 if (strcmp (ep->name, EP_IN_NAME) == 0) {
657 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
658 result = usb_ep_enable (ep, d);
659 if (result == 0) {
660 ep->driver_data = dev;
661 if (source_sink_start_ep (ep, gfp_flags) != 0) {
662 dev->in_ep = ep;
663 continue;
664 }
665 usb_ep_disable (ep);
666 result = -EIO;
667 }
668
669 /* one endpoint reads (sinks) anything out (from the host) */
670 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
671 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
672 result = usb_ep_enable (ep, d);
673 if (result == 0) {
674 ep->driver_data = dev;
675 if (source_sink_start_ep (ep, gfp_flags) != 0) {
676 dev->out_ep = ep;
677 continue;
678 }
679 usb_ep_disable (ep);
680 result = -EIO;
681 }
682
683 /* ignore any other endpoints */
684 } else
685 continue;
686
687 /* stop on error */
688 ERROR (dev, "can't start %s, result %d\n", ep->name, result);
689 break;
690 }
691 if (result == 0)
692 DBG (dev, "buflen %d\n", buflen);
693
694 /* caller is responsible for cleanup on error */
695 return result;
696}
697
698/*-------------------------------------------------------------------------*/
699
700static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
701{
702 struct zero_dev *dev = ep->driver_data;
703 int status = req->status;
704
705 switch (status) {
706
707 case 0: /* normal completion? */
708 if (ep == dev->out_ep) {
709 /* loop this OUT packet back IN to the host */
710 req->zero = (req->actual < req->length);
711 req->length = req->actual;
712 status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
713 if (status == 0)
714 return;
715
716 /* "should never get here" */
717 ERROR (dev, "can't loop %s to %s: %d\n",
718 ep->name, dev->in_ep->name,
719 status);
720 }
721
722 /* queue the buffer for some later OUT packet */
723 req->length = buflen;
724 status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
725 if (status == 0)
726 return;
727
728 /* "should never get here" */
729 /* FALLTHROUGH */
730
731 default:
732 ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
733 status, req->actual, req->length);
734 /* FALLTHROUGH */
735
736 /* NOTE: since this driver doesn't maintain an explicit record
737 * of requests it submitted (just maintains qlen count), we
738 * rely on the hardware driver to clean up on disconnect or
739 * endpoint disable.
740 */
741 case -ECONNABORTED: /* hardware forced ep reset */
742 case -ECONNRESET: /* request dequeued */
743 case -ESHUTDOWN: /* disconnect from host */
744 free_ep_req (ep, req);
745 return;
746 }
747}
748
749static int
Al Viro55016f12005-10-21 03:21:58 -0400750set_loopback_config (struct zero_dev *dev, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 int result = 0;
753 struct usb_ep *ep;
754 struct usb_gadget *gadget = dev->gadget;
755
756 gadget_for_each_ep (ep, gadget) {
757 const struct usb_endpoint_descriptor *d;
758
759 /* one endpoint writes data back IN to the host */
760 if (strcmp (ep->name, EP_IN_NAME) == 0) {
761 d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
762 result = usb_ep_enable (ep, d);
763 if (result == 0) {
764 ep->driver_data = dev;
765 dev->in_ep = ep;
766 continue;
767 }
768
769 /* one endpoint just reads OUT packets */
770 } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
771 d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
772 result = usb_ep_enable (ep, d);
773 if (result == 0) {
774 ep->driver_data = dev;
775 dev->out_ep = ep;
776 continue;
777 }
778
779 /* ignore any other endpoints */
780 } else
781 continue;
782
783 /* stop on error */
784 ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
785 break;
786 }
787
788 /* allocate a bunch of read buffers and queue them all at once.
789 * we buffer at most 'qlen' transfers; fewer if any need more
790 * than 'buflen' bytes each.
791 */
792 if (result == 0) {
793 struct usb_request *req;
794 unsigned i;
795
796 ep = dev->out_ep;
797 for (i = 0; i < qlen && result == 0; i++) {
798 req = alloc_ep_req (ep, buflen);
799 if (req) {
800 req->complete = loopback_complete;
801 result = usb_ep_queue (ep, req, GFP_ATOMIC);
802 if (result)
803 DBG (dev, "%s queue req --> %d\n",
804 ep->name, result);
805 } else
806 result = -ENOMEM;
807 }
808 }
809 if (result == 0)
810 DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
811
812 /* caller is responsible for cleanup on error */
813 return result;
814}
815
816/*-------------------------------------------------------------------------*/
817
818static void zero_reset_config (struct zero_dev *dev)
819{
820 if (dev->config == 0)
821 return;
822
823 DBG (dev, "reset config\n");
824
825 /* just disable endpoints, forcing completion of pending i/o.
826 * all our completion handlers free their requests in this case.
827 */
828 if (dev->in_ep) {
829 usb_ep_disable (dev->in_ep);
830 dev->in_ep = NULL;
831 }
832 if (dev->out_ep) {
833 usb_ep_disable (dev->out_ep);
834 dev->out_ep = NULL;
835 }
836 dev->config = 0;
837 del_timer (&dev->resume);
838}
839
840/* change our operational config. this code must agree with the code
841 * that returns config descriptors, and altsetting code.
842 *
843 * it's also responsible for power management interactions. some
844 * configurations might not work with our current power sources.
845 *
846 * note that some device controller hardware will constrain what this
847 * code can do, perhaps by disallowing more than one configuration or
848 * by limiting configuration choices (like the pxa2xx).
849 */
850static int
Al Viro55016f12005-10-21 03:21:58 -0400851zero_set_config (struct zero_dev *dev, unsigned number, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int result = 0;
854 struct usb_gadget *gadget = dev->gadget;
855
856 if (number == dev->config)
857 return 0;
858
859 if (gadget_is_sa1100 (gadget) && dev->config) {
860 /* tx fifo is full, but we can't clear it...*/
861 INFO (dev, "can't change configurations\n");
862 return -ESPIPE;
863 }
864 zero_reset_config (dev);
865
866 switch (number) {
867 case CONFIG_SOURCE_SINK:
868 result = set_source_sink_config (dev, gfp_flags);
869 break;
870 case CONFIG_LOOPBACK:
871 result = set_loopback_config (dev, gfp_flags);
872 break;
873 default:
874 result = -EINVAL;
875 /* FALL THROUGH */
876 case 0:
877 return result;
878 }
879
880 if (!result && (!dev->in_ep || !dev->out_ep))
881 result = -ENODEV;
882 if (result)
883 zero_reset_config (dev);
884 else {
885 char *speed;
886
887 switch (gadget->speed) {
888 case USB_SPEED_LOW: speed = "low"; break;
889 case USB_SPEED_FULL: speed = "full"; break;
890 case USB_SPEED_HIGH: speed = "high"; break;
891 default: speed = "?"; break;
892 }
893
894 dev->config = number;
895 INFO (dev, "%s speed config #%d: %s\n", speed, number,
896 (number == CONFIG_SOURCE_SINK)
897 ? source_sink : loopback);
898 }
899 return result;
900}
901
902/*-------------------------------------------------------------------------*/
903
904static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
905{
906 if (req->status || req->actual != req->length)
907 DBG ((struct zero_dev *) ep->driver_data,
908 "setup complete --> %d, %d/%d\n",
909 req->status, req->actual, req->length);
910}
911
912/*
913 * The setup() callback implements all the ep0 functionality that's
914 * not handled lower down, in hardware or the hardware driver (like
915 * device and endpoint feature flags, and their status). It's all
916 * housekeeping for the gadget function we're implementing. Most of
917 * the work is in config-specific setup.
918 */
919static int
920zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
921{
922 struct zero_dev *dev = get_gadget_data (gadget);
923 struct usb_request *req = dev->req;
924 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -0700925 u16 w_index = le16_to_cpu(ctrl->wIndex);
926 u16 w_value = le16_to_cpu(ctrl->wValue);
927 u16 w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* usually this stores reply data in the pre-allocated ep0 buffer,
930 * but config change events will reconfigure hardware.
931 */
932 req->zero = 0;
933 switch (ctrl->bRequest) {
934
935 case USB_REQ_GET_DESCRIPTOR:
936 if (ctrl->bRequestType != USB_DIR_IN)
937 goto unknown;
938 switch (w_value >> 8) {
939
940 case USB_DT_DEVICE:
941 value = min (w_length, (u16) sizeof device_desc);
942 memcpy (req->buf, &device_desc, value);
943 break;
944#ifdef CONFIG_USB_GADGET_DUALSPEED
945 case USB_DT_DEVICE_QUALIFIER:
946 if (!gadget->is_dualspeed)
947 break;
948 value = min (w_length, (u16) sizeof dev_qualifier);
949 memcpy (req->buf, &dev_qualifier, value);
950 break;
951
952 case USB_DT_OTHER_SPEED_CONFIG:
953 if (!gadget->is_dualspeed)
954 break;
955 // FALLTHROUGH
956#endif /* CONFIG_USB_GADGET_DUALSPEED */
957 case USB_DT_CONFIG:
958 value = config_buf (gadget, req->buf,
959 w_value >> 8,
960 w_value & 0xff);
961 if (value >= 0)
962 value = min (w_length, (u16) value);
963 break;
964
965 case USB_DT_STRING:
966 /* wIndex == language code.
967 * this driver only handles one language, you can
968 * add string tables for other languages, using
969 * any UTF-8 characters
970 */
971 value = usb_gadget_get_string (&stringtab,
972 w_value & 0xff, req->buf);
973 if (value >= 0)
974 value = min (w_length, (u16) value);
975 break;
976 }
977 break;
978
979 /* currently two configs, two speeds */
980 case USB_REQ_SET_CONFIGURATION:
981 if (ctrl->bRequestType != 0)
982 goto unknown;
983 if (gadget->a_hnp_support)
984 DBG (dev, "HNP available\n");
985 else if (gadget->a_alt_hnp_support)
986 DBG (dev, "HNP needs a different root port\n");
987 else
988 VDBG (dev, "HNP inactive\n");
989 spin_lock (&dev->lock);
990 value = zero_set_config (dev, w_value, GFP_ATOMIC);
991 spin_unlock (&dev->lock);
992 break;
993 case USB_REQ_GET_CONFIGURATION:
994 if (ctrl->bRequestType != USB_DIR_IN)
995 goto unknown;
996 *(u8 *)req->buf = dev->config;
997 value = min (w_length, (u16) 1);
998 break;
999
1000 /* until we add altsetting support, or other interfaces,
1001 * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
1002 * and already killed pending endpoint I/O.
1003 */
1004 case USB_REQ_SET_INTERFACE:
1005 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1006 goto unknown;
1007 spin_lock (&dev->lock);
1008 if (dev->config && w_index == 0 && w_value == 0) {
1009 u8 config = dev->config;
1010
1011 /* resets interface configuration, forgets about
1012 * previous transaction state (queued bufs, etc)
1013 * and re-inits endpoint state (toggle etc)
1014 * no response queued, just zero status == success.
1015 * if we had more than one interface we couldn't
1016 * use this "reset the config" shortcut.
1017 */
1018 zero_reset_config (dev);
1019 zero_set_config (dev, config, GFP_ATOMIC);
1020 value = 0;
1021 }
1022 spin_unlock (&dev->lock);
1023 break;
1024 case USB_REQ_GET_INTERFACE:
1025 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1026 goto unknown;
1027 if (!dev->config)
1028 break;
1029 if (w_index != 0) {
1030 value = -EDOM;
1031 break;
1032 }
1033 *(u8 *)req->buf = 0;
1034 value = min (w_length, (u16) 1);
1035 break;
1036
1037 /*
1038 * These are the same vendor-specific requests supported by
1039 * Intel's USB 2.0 compliance test devices. We exceed that
1040 * device spec by allowing multiple-packet requests.
1041 */
1042 case 0x5b: /* control WRITE test -- fill the buffer */
1043 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1044 goto unknown;
1045 if (w_value || w_index)
1046 break;
1047 /* just read that many bytes into the buffer */
1048 if (w_length > USB_BUFSIZ)
1049 break;
1050 value = w_length;
1051 break;
1052 case 0x5c: /* control READ test -- return the buffer */
1053 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1054 goto unknown;
1055 if (w_value || w_index)
1056 break;
1057 /* expect those bytes are still in the buffer; send back */
1058 if (w_length > USB_BUFSIZ
1059 || w_length != req->length)
1060 break;
1061 value = w_length;
1062 break;
1063
1064 default:
1065unknown:
1066 VDBG (dev,
1067 "unknown control req%02x.%02x v%04x i%04x l%d\n",
1068 ctrl->bRequestType, ctrl->bRequest,
1069 w_value, w_index, w_length);
1070 }
1071
1072 /* respond with data transfer before status phase? */
1073 if (value >= 0) {
1074 req->length = value;
1075 req->zero = value < w_length;
1076 value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1077 if (value < 0) {
1078 DBG (dev, "ep_queue --> %d\n", value);
1079 req->status = 0;
1080 zero_setup_complete (gadget->ep0, req);
1081 }
1082 }
1083
1084 /* device either stalls (value < 0) or reports success */
1085 return value;
1086}
1087
1088static void
1089zero_disconnect (struct usb_gadget *gadget)
1090{
1091 struct zero_dev *dev = get_gadget_data (gadget);
1092 unsigned long flags;
1093
1094 spin_lock_irqsave (&dev->lock, flags);
1095 zero_reset_config (dev);
1096
1097 /* a more significant application might have some non-usb
1098 * activities to quiesce here, saving resources like power
1099 * or pushing the notification up a network stack.
1100 */
1101 spin_unlock_irqrestore (&dev->lock, flags);
1102
1103 /* next we may get setup() calls to enumerate new connections;
1104 * or an unbind() during shutdown (including removing module).
1105 */
1106}
1107
1108static void
1109zero_autoresume (unsigned long _dev)
1110{
1111 struct zero_dev *dev = (struct zero_dev *) _dev;
1112 int status;
1113
1114 /* normally the host would be woken up for something
1115 * more significant than just a timer firing...
1116 */
1117 if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1118 status = usb_gadget_wakeup (dev->gadget);
1119 DBG (dev, "wakeup --> %d\n", status);
1120 }
1121}
1122
1123/*-------------------------------------------------------------------------*/
1124
David Brownell329af282006-02-18 12:31:05 -08001125static void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126zero_unbind (struct usb_gadget *gadget)
1127{
1128 struct zero_dev *dev = get_gadget_data (gadget);
1129
1130 DBG (dev, "unbind\n");
1131
1132 /* we've already been disconnected ... no i/o is active */
David Brownell69396dc2006-01-20 14:38:49 -08001133 if (dev->req) {
1134 dev->req->length = USB_BUFSIZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 free_ep_req (gadget->ep0, dev->req);
David Brownell69396dc2006-01-20 14:38:49 -08001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 del_timer_sync (&dev->resume);
1138 kfree (dev);
1139 set_gadget_data (gadget, NULL);
1140}
1141
David Brownell329af282006-02-18 12:31:05 -08001142static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143zero_bind (struct usb_gadget *gadget)
1144{
1145 struct zero_dev *dev;
1146 struct usb_ep *ep;
David Brownell91e79c92005-07-13 15:18:30 -07001147 int gcnum;
1148
1149 /* FIXME this can't yet work right with SH ... it has only
1150 * one configuration, numbered one.
1151 */
1152 if (gadget_is_sh(gadget))
1153 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /* Bulk-only drivers like this one SHOULD be able to
1156 * autoconfigure on any sane usb controller driver,
1157 * but there may also be important quirks to address.
1158 */
1159 usb_ep_autoconfig_reset (gadget);
1160 ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1161 if (!ep) {
1162autoconf_fail:
1163 printk (KERN_ERR "%s: can't autoconfigure on %s\n",
1164 shortname, gadget->name);
1165 return -ENODEV;
1166 }
1167 EP_IN_NAME = ep->name;
1168 ep->driver_data = ep; /* claim */
1169
1170 ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1171 if (!ep)
1172 goto autoconf_fail;
1173 EP_OUT_NAME = ep->name;
1174 ep->driver_data = ep; /* claim */
1175
David Brownell91e79c92005-07-13 15:18:30 -07001176 gcnum = usb_gadget_controller_number (gadget);
1177 if (gcnum >= 0)
1178 device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
1179 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* gadget zero is so simple (for now, no altsettings) that
1181 * it SHOULD NOT have problems with bulk-capable hardware.
1182 * so warn about unrcognized controllers, don't panic.
1183 *
1184 * things like configuration and altsetting numbering
1185 * can need hardware-specific attention though.
1186 */
1187 printk (KERN_WARNING "%s: controller '%s' not recognized\n",
1188 shortname, gadget->name);
1189 device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
1190 }
1191
1192
1193 /* ok, we made sense of the hardware ... */
Eric Sesterhenn7039f422006-02-27 13:34:10 -08001194 dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (!dev)
1196 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 spin_lock_init (&dev->lock);
1198 dev->gadget = gadget;
1199 set_gadget_data (gadget, dev);
1200
1201 /* preallocate control response and buffer */
1202 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1203 if (!dev->req)
1204 goto enomem;
1205 dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1206 &dev->req->dma, GFP_KERNEL);
1207 if (!dev->req->buf)
1208 goto enomem;
1209
1210 dev->req->complete = zero_setup_complete;
1211
1212 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1213
1214#ifdef CONFIG_USB_GADGET_DUALSPEED
1215 /* assume ep0 uses the same value for both speeds ... */
1216 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1217
1218 /* and that all endpoints are dual-speed */
1219 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1220 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1221#endif
1222
1223 if (gadget->is_otg) {
1224 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1225 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1226 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1227 }
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 usb_gadget_set_selfpowered (gadget);
1230
1231 init_timer (&dev->resume);
1232 dev->resume.function = zero_autoresume;
1233 dev->resume.data = (unsigned long) dev;
1234 if (autoresume) {
1235 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1236 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1237 }
1238
1239 gadget->ep0->driver_data = dev;
1240
1241 INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1242 INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1243 EP_OUT_NAME, EP_IN_NAME);
1244
1245 snprintf (manufacturer, sizeof manufacturer, "%s %s with %s",
1246 system_utsname.sysname, system_utsname.release,
1247 gadget->name);
1248
1249 return 0;
1250
1251enomem:
1252 zero_unbind (gadget);
1253 return -ENOMEM;
1254}
1255
1256/*-------------------------------------------------------------------------*/
1257
1258static void
1259zero_suspend (struct usb_gadget *gadget)
1260{
1261 struct zero_dev *dev = get_gadget_data (gadget);
1262
1263 if (gadget->speed == USB_SPEED_UNKNOWN)
1264 return;
1265
1266 if (autoresume) {
1267 mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1268 DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1269 } else
1270 DBG (dev, "suspend\n");
1271}
1272
1273static void
1274zero_resume (struct usb_gadget *gadget)
1275{
1276 struct zero_dev *dev = get_gadget_data (gadget);
1277
1278 DBG (dev, "resume\n");
1279 del_timer (&dev->resume);
1280}
1281
1282
1283/*-------------------------------------------------------------------------*/
1284
1285static struct usb_gadget_driver zero_driver = {
1286#ifdef CONFIG_USB_GADGET_DUALSPEED
1287 .speed = USB_SPEED_HIGH,
1288#else
1289 .speed = USB_SPEED_FULL,
1290#endif
1291 .function = (char *) longname,
1292 .bind = zero_bind,
David Brownell329af282006-02-18 12:31:05 -08001293 .unbind = __exit_p(zero_unbind),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 .setup = zero_setup,
1296 .disconnect = zero_disconnect,
1297
1298 .suspend = zero_suspend,
1299 .resume = zero_resume,
1300
1301 .driver = {
1302 .name = (char *) shortname,
Ben Dooksd0d50492005-10-10 10:52:33 +01001303 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 },
1305};
1306
1307MODULE_AUTHOR ("David Brownell");
1308MODULE_LICENSE ("Dual BSD/GPL");
1309
1310
1311static int __init init (void)
1312{
1313 /* a real value would likely come through some id prom
1314 * or module option. this one takes at least two packets.
1315 */
1316 strlcpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
1317
1318 return usb_gadget_register_driver (&zero_driver);
1319}
1320module_init (init);
1321
1322static void __exit cleanup (void)
1323{
1324 usb_gadget_unregister_driver (&zero_driver);
1325}
1326module_exit (cleanup);
1327