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