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