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