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