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