David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * composite.c - infrastructure for Composite USB Gadgets |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 David Brownell |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | /* #define VERBOSE_DEBUG */ |
| 22 | |
| 23 | #include <linux/kallsyms.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/device.h> |
| 27 | |
| 28 | #include <linux/usb/composite.h> |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * The code in this file is utility code, used to build a gadget driver |
| 33 | * from one or more "function" drivers, one or more "configuration" |
| 34 | * objects, and a "usb_composite_driver" by gluing them together along |
| 35 | * with the relevant device-wide data. |
| 36 | */ |
| 37 | |
| 38 | /* big enough to hold our biggest descriptor */ |
| 39 | #define USB_BUFSIZ 512 |
| 40 | |
| 41 | static struct usb_composite_driver *composite; |
| 42 | |
| 43 | /* Some systems will need runtime overrides for the product identifers |
| 44 | * published in the device descriptor, either numbers or strings or both. |
| 45 | * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). |
| 46 | */ |
| 47 | |
| 48 | static ushort idVendor; |
| 49 | module_param(idVendor, ushort, 0); |
| 50 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); |
| 51 | |
| 52 | static ushort idProduct; |
| 53 | module_param(idProduct, ushort, 0); |
| 54 | MODULE_PARM_DESC(idProduct, "USB Product ID"); |
| 55 | |
| 56 | static ushort bcdDevice; |
| 57 | module_param(bcdDevice, ushort, 0); |
| 58 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); |
| 59 | |
| 60 | static char *iManufacturer; |
| 61 | module_param(iManufacturer, charp, 0); |
| 62 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); |
| 63 | |
| 64 | static char *iProduct; |
| 65 | module_param(iProduct, charp, 0); |
| 66 | MODULE_PARM_DESC(iProduct, "USB Product string"); |
| 67 | |
| 68 | static char *iSerialNumber; |
| 69 | module_param(iSerialNumber, charp, 0); |
| 70 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); |
| 71 | |
| 72 | /*-------------------------------------------------------------------------*/ |
| 73 | |
| 74 | /** |
| 75 | * usb_add_function() - add a function to a configuration |
| 76 | * @config: the configuration |
| 77 | * @function: the function being added |
| 78 | * Context: single threaded during gadget setup |
| 79 | * |
| 80 | * After initialization, each configuration must have one or more |
| 81 | * functions added to it. Adding a function involves calling its @bind() |
| 82 | * method to allocate resources such as interface and string identifiers |
| 83 | * and endpoints. |
| 84 | * |
| 85 | * This function returns the value of the function's bind(), which is |
| 86 | * zero for success else a negative errno value. |
| 87 | */ |
| 88 | int __init usb_add_function(struct usb_configuration *config, |
| 89 | struct usb_function *function) |
| 90 | { |
| 91 | int value = -EINVAL; |
| 92 | |
| 93 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", |
| 94 | function->name, function, |
| 95 | config->label, config); |
| 96 | |
| 97 | if (!function->set_alt || !function->disable) |
| 98 | goto done; |
| 99 | |
| 100 | function->config = config; |
| 101 | list_add_tail(&function->list, &config->functions); |
| 102 | |
| 103 | /* REVISIT *require* function->bind? */ |
| 104 | if (function->bind) { |
| 105 | value = function->bind(config, function); |
| 106 | if (value < 0) { |
| 107 | list_del(&function->list); |
| 108 | function->config = NULL; |
| 109 | } |
| 110 | } else |
| 111 | value = 0; |
| 112 | |
| 113 | /* We allow configurations that don't work at both speeds. |
| 114 | * If we run into a lowspeed Linux system, treat it the same |
| 115 | * as full speed ... it's the function drivers that will need |
| 116 | * to avoid bulk and ISO transfers. |
| 117 | */ |
| 118 | if (!config->fullspeed && function->descriptors) |
| 119 | config->fullspeed = true; |
| 120 | if (!config->highspeed && function->hs_descriptors) |
| 121 | config->highspeed = true; |
| 122 | |
| 123 | done: |
| 124 | if (value) |
| 125 | DBG(config->cdev, "adding '%s'/%p --> %d\n", |
| 126 | function->name, function, value); |
| 127 | return value; |
| 128 | } |
| 129 | |
| 130 | /** |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 131 | * usb_function_deactivate - prevent function and gadget enumeration |
| 132 | * @function: the function that isn't yet ready to respond |
| 133 | * |
| 134 | * Blocks response of the gadget driver to host enumeration by |
| 135 | * preventing the data line pullup from being activated. This is |
| 136 | * normally called during @bind() processing to change from the |
| 137 | * initial "ready to respond" state, or when a required resource |
| 138 | * becomes available. |
| 139 | * |
| 140 | * For example, drivers that serve as a passthrough to a userspace |
| 141 | * daemon can block enumeration unless that daemon (such as an OBEX, |
| 142 | * MTP, or print server) is ready to handle host requests. |
| 143 | * |
| 144 | * Not all systems support software control of their USB peripheral |
| 145 | * data pullups. |
| 146 | * |
| 147 | * Returns zero on success, else negative errno. |
| 148 | */ |
| 149 | int usb_function_deactivate(struct usb_function *function) |
| 150 | { |
| 151 | struct usb_composite_dev *cdev = function->config->cdev; |
| 152 | int status = 0; |
| 153 | |
| 154 | spin_lock(&cdev->lock); |
| 155 | |
| 156 | if (cdev->deactivations == 0) |
| 157 | status = usb_gadget_disconnect(cdev->gadget); |
| 158 | if (status == 0) |
| 159 | cdev->deactivations++; |
| 160 | |
| 161 | spin_unlock(&cdev->lock); |
| 162 | return status; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * usb_function_activate - allow function and gadget enumeration |
| 167 | * @function: function on which usb_function_activate() was called |
| 168 | * |
| 169 | * Reverses effect of usb_function_deactivate(). If no more functions |
| 170 | * are delaying their activation, the gadget driver will respond to |
| 171 | * host enumeration procedures. |
| 172 | * |
| 173 | * Returns zero on success, else negative errno. |
| 174 | */ |
| 175 | int usb_function_activate(struct usb_function *function) |
| 176 | { |
| 177 | struct usb_composite_dev *cdev = function->config->cdev; |
| 178 | int status = 0; |
| 179 | |
| 180 | spin_lock(&cdev->lock); |
| 181 | |
| 182 | if (WARN_ON(cdev->deactivations == 0)) |
| 183 | status = -EINVAL; |
| 184 | else { |
| 185 | cdev->deactivations--; |
| 186 | if (cdev->deactivations == 0) |
| 187 | status = usb_gadget_connect(cdev->gadget); |
| 188 | } |
| 189 | |
| 190 | spin_unlock(&cdev->lock); |
| 191 | return status; |
| 192 | } |
| 193 | |
| 194 | /** |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 195 | * usb_interface_id() - allocate an unused interface ID |
| 196 | * @config: configuration associated with the interface |
| 197 | * @function: function handling the interface |
| 198 | * Context: single threaded during gadget setup |
| 199 | * |
| 200 | * usb_interface_id() is called from usb_function.bind() callbacks to |
| 201 | * allocate new interface IDs. The function driver will then store that |
| 202 | * ID in interface, association, CDC union, and other descriptors. It |
| 203 | * will also handle any control requests targetted at that interface, |
| 204 | * particularly changing its altsetting via set_alt(). There may |
| 205 | * also be class-specific or vendor-specific requests to handle. |
| 206 | * |
| 207 | * All interface identifier should be allocated using this routine, to |
| 208 | * ensure that for example different functions don't wrongly assign |
| 209 | * different meanings to the same identifier. Note that since interface |
| 210 | * identifers are configuration-specific, functions used in more than |
| 211 | * one configuration (or more than once in a given configuration) need |
| 212 | * multiple versions of the relevant descriptors. |
| 213 | * |
| 214 | * Returns the interface ID which was allocated; or -ENODEV if no |
| 215 | * more interface IDs can be allocated. |
| 216 | */ |
| 217 | int __init usb_interface_id(struct usb_configuration *config, |
| 218 | struct usb_function *function) |
| 219 | { |
| 220 | unsigned id = config->next_interface_id; |
| 221 | |
| 222 | if (id < MAX_CONFIG_INTERFACES) { |
| 223 | config->interface[id] = function; |
| 224 | config->next_interface_id = id + 1; |
| 225 | return id; |
| 226 | } |
| 227 | return -ENODEV; |
| 228 | } |
| 229 | |
| 230 | static int config_buf(struct usb_configuration *config, |
| 231 | enum usb_device_speed speed, void *buf, u8 type) |
| 232 | { |
| 233 | struct usb_config_descriptor *c = buf; |
| 234 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 235 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 236 | struct usb_function *f; |
| 237 | int status; |
| 238 | |
| 239 | /* write the config descriptor */ |
| 240 | c = buf; |
| 241 | c->bLength = USB_DT_CONFIG_SIZE; |
| 242 | c->bDescriptorType = type; |
| 243 | /* wTotalLength is written later */ |
| 244 | c->bNumInterfaces = config->next_interface_id; |
| 245 | c->bConfigurationValue = config->bConfigurationValue; |
| 246 | c->iConfiguration = config->iConfiguration; |
| 247 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
David Brownell | 36e893d | 2008-09-12 09:39:06 -0700 | [diff] [blame] | 248 | c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 249 | |
| 250 | /* There may be e.g. OTG descriptors */ |
| 251 | if (config->descriptors) { |
| 252 | status = usb_descriptor_fillbuf(next, len, |
| 253 | config->descriptors); |
| 254 | if (status < 0) |
| 255 | return status; |
| 256 | len -= status; |
| 257 | next += status; |
| 258 | } |
| 259 | |
| 260 | /* add each function's descriptors */ |
| 261 | list_for_each_entry(f, &config->functions, list) { |
| 262 | struct usb_descriptor_header **descriptors; |
| 263 | |
| 264 | if (speed == USB_SPEED_HIGH) |
| 265 | descriptors = f->hs_descriptors; |
| 266 | else |
| 267 | descriptors = f->descriptors; |
| 268 | if (!descriptors) |
| 269 | continue; |
| 270 | status = usb_descriptor_fillbuf(next, len, |
| 271 | (const struct usb_descriptor_header **) descriptors); |
| 272 | if (status < 0) |
| 273 | return status; |
| 274 | len -= status; |
| 275 | next += status; |
| 276 | } |
| 277 | |
| 278 | len = next - buf; |
| 279 | c->wTotalLength = cpu_to_le16(len); |
| 280 | return len; |
| 281 | } |
| 282 | |
| 283 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) |
| 284 | { |
| 285 | struct usb_gadget *gadget = cdev->gadget; |
| 286 | struct usb_configuration *c; |
| 287 | u8 type = w_value >> 8; |
| 288 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 289 | |
| 290 | if (gadget_is_dualspeed(gadget)) { |
| 291 | int hs = 0; |
| 292 | |
| 293 | if (gadget->speed == USB_SPEED_HIGH) |
| 294 | hs = 1; |
| 295 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 296 | hs = !hs; |
| 297 | if (hs) |
| 298 | speed = USB_SPEED_HIGH; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | /* This is a lookup by config *INDEX* */ |
| 303 | w_value &= 0xff; |
| 304 | list_for_each_entry(c, &cdev->configs, list) { |
| 305 | /* ignore configs that won't work at this speed */ |
| 306 | if (speed == USB_SPEED_HIGH) { |
| 307 | if (!c->highspeed) |
| 308 | continue; |
| 309 | } else { |
| 310 | if (!c->fullspeed) |
| 311 | continue; |
| 312 | } |
| 313 | if (w_value == 0) |
| 314 | return config_buf(c, speed, cdev->req->buf, type); |
| 315 | w_value--; |
| 316 | } |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) |
| 321 | { |
| 322 | struct usb_gadget *gadget = cdev->gadget; |
| 323 | struct usb_configuration *c; |
| 324 | unsigned count = 0; |
| 325 | int hs = 0; |
| 326 | |
| 327 | if (gadget_is_dualspeed(gadget)) { |
| 328 | if (gadget->speed == USB_SPEED_HIGH) |
| 329 | hs = 1; |
| 330 | if (type == USB_DT_DEVICE_QUALIFIER) |
| 331 | hs = !hs; |
| 332 | } |
| 333 | list_for_each_entry(c, &cdev->configs, list) { |
| 334 | /* ignore configs that won't work at this speed */ |
| 335 | if (hs) { |
| 336 | if (!c->highspeed) |
| 337 | continue; |
| 338 | } else { |
| 339 | if (!c->fullspeed) |
| 340 | continue; |
| 341 | } |
| 342 | count++; |
| 343 | } |
| 344 | return count; |
| 345 | } |
| 346 | |
| 347 | static void device_qual(struct usb_composite_dev *cdev) |
| 348 | { |
| 349 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 350 | |
| 351 | qual->bLength = sizeof(*qual); |
| 352 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 353 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 354 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 355 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 356 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 357 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 358 | /* ASSUME same EP0 fifo size at both speeds */ |
| 359 | qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0; |
| 360 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
David Lopo | c24f422 | 2008-07-01 13:14:17 -0700 | [diff] [blame] | 361 | qual->bRESERVED = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /*-------------------------------------------------------------------------*/ |
| 365 | |
| 366 | static void reset_config(struct usb_composite_dev *cdev) |
| 367 | { |
| 368 | struct usb_function *f; |
| 369 | |
| 370 | DBG(cdev, "reset config\n"); |
| 371 | |
| 372 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 373 | if (f->disable) |
| 374 | f->disable(f); |
| 375 | } |
| 376 | cdev->config = NULL; |
| 377 | } |
| 378 | |
| 379 | static int set_config(struct usb_composite_dev *cdev, |
| 380 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 381 | { |
| 382 | struct usb_gadget *gadget = cdev->gadget; |
| 383 | struct usb_configuration *c = NULL; |
| 384 | int result = -EINVAL; |
| 385 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 386 | int tmp; |
| 387 | |
| 388 | if (cdev->config) |
| 389 | reset_config(cdev); |
| 390 | |
| 391 | if (number) { |
| 392 | list_for_each_entry(c, &cdev->configs, list) { |
| 393 | if (c->bConfigurationValue == number) { |
| 394 | result = 0; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | if (result < 0) |
| 399 | goto done; |
| 400 | } else |
| 401 | result = 0; |
| 402 | |
| 403 | INFO(cdev, "%s speed config #%d: %s\n", |
| 404 | ({ char *speed; |
| 405 | switch (gadget->speed) { |
| 406 | case USB_SPEED_LOW: speed = "low"; break; |
| 407 | case USB_SPEED_FULL: speed = "full"; break; |
| 408 | case USB_SPEED_HIGH: speed = "high"; break; |
| 409 | default: speed = "?"; break; |
| 410 | } ; speed; }), number, c ? c->label : "unconfigured"); |
| 411 | |
| 412 | if (!c) |
| 413 | goto done; |
| 414 | |
| 415 | cdev->config = c; |
| 416 | |
| 417 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 418 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 419 | struct usb_function *f = c->interface[tmp]; |
| 420 | |
| 421 | if (!f) |
| 422 | break; |
| 423 | |
| 424 | result = f->set_alt(f, tmp, 0); |
| 425 | if (result < 0) { |
| 426 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", |
| 427 | tmp, f->name, f, result); |
| 428 | |
| 429 | reset_config(cdev); |
| 430 | goto done; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* when we return, be sure our power usage is valid */ |
David Brownell | 36e893d | 2008-09-12 09:39:06 -0700 | [diff] [blame] | 435 | power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 436 | done: |
| 437 | usb_gadget_vbus_draw(gadget, power); |
| 438 | return result; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * usb_add_config() - add a configuration to a device. |
| 443 | * @cdev: wraps the USB gadget |
| 444 | * @config: the configuration, with bConfigurationValue assigned |
| 445 | * Context: single threaded during gadget setup |
| 446 | * |
| 447 | * One of the main tasks of a composite driver's bind() routine is to |
| 448 | * add each of the configurations it supports, using this routine. |
| 449 | * |
| 450 | * This function returns the value of the configuration's bind(), which |
| 451 | * is zero for success else a negative errno value. Binding configurations |
| 452 | * assigns global resources including string IDs, and per-configuration |
| 453 | * resources such as interface IDs and endpoints. |
| 454 | */ |
| 455 | int __init usb_add_config(struct usb_composite_dev *cdev, |
| 456 | struct usb_configuration *config) |
| 457 | { |
| 458 | int status = -EINVAL; |
| 459 | struct usb_configuration *c; |
| 460 | |
| 461 | DBG(cdev, "adding config #%u '%s'/%p\n", |
| 462 | config->bConfigurationValue, |
| 463 | config->label, config); |
| 464 | |
| 465 | if (!config->bConfigurationValue || !config->bind) |
| 466 | goto done; |
| 467 | |
| 468 | /* Prevent duplicate configuration identifiers */ |
| 469 | list_for_each_entry(c, &cdev->configs, list) { |
| 470 | if (c->bConfigurationValue == config->bConfigurationValue) { |
| 471 | status = -EBUSY; |
| 472 | goto done; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | config->cdev = cdev; |
| 477 | list_add_tail(&config->list, &cdev->configs); |
| 478 | |
| 479 | INIT_LIST_HEAD(&config->functions); |
| 480 | config->next_interface_id = 0; |
| 481 | |
| 482 | status = config->bind(config); |
| 483 | if (status < 0) { |
| 484 | list_del(&config->list); |
| 485 | config->cdev = NULL; |
| 486 | } else { |
| 487 | unsigned i; |
| 488 | |
| 489 | DBG(cdev, "cfg %d/%p speeds:%s%s\n", |
| 490 | config->bConfigurationValue, config, |
| 491 | config->highspeed ? " high" : "", |
| 492 | config->fullspeed |
| 493 | ? (gadget_is_dualspeed(cdev->gadget) |
| 494 | ? " full" |
| 495 | : " full/low") |
| 496 | : ""); |
| 497 | |
| 498 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 499 | struct usb_function *f = config->interface[i]; |
| 500 | |
| 501 | if (!f) |
| 502 | continue; |
| 503 | DBG(cdev, " interface %d = %s/%p\n", |
| 504 | i, f->name, f); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* set_alt(), or next config->bind(), sets up |
| 509 | * ep->driver_data as needed. |
| 510 | */ |
| 511 | usb_ep_autoconfig_reset(cdev->gadget); |
| 512 | |
| 513 | done: |
| 514 | if (status) |
| 515 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, |
| 516 | config->bConfigurationValue, status); |
| 517 | return status; |
| 518 | } |
| 519 | |
| 520 | /*-------------------------------------------------------------------------*/ |
| 521 | |
| 522 | /* We support strings in multiple languages ... string descriptor zero |
| 523 | * says which languages are supported. The typical case will be that |
| 524 | * only one language (probably English) is used, with I18N handled on |
| 525 | * the host side. |
| 526 | */ |
| 527 | |
| 528 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) |
| 529 | { |
| 530 | const struct usb_gadget_strings *s; |
| 531 | u16 language; |
| 532 | __le16 *tmp; |
| 533 | |
| 534 | while (*sp) { |
| 535 | s = *sp; |
| 536 | language = cpu_to_le16(s->language); |
| 537 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { |
| 538 | if (*tmp == language) |
| 539 | goto repeat; |
| 540 | } |
| 541 | *tmp++ = language; |
| 542 | repeat: |
| 543 | sp++; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | static int lookup_string( |
| 548 | struct usb_gadget_strings **sp, |
| 549 | void *buf, |
| 550 | u16 language, |
| 551 | int id |
| 552 | ) |
| 553 | { |
| 554 | struct usb_gadget_strings *s; |
| 555 | int value; |
| 556 | |
| 557 | while (*sp) { |
| 558 | s = *sp++; |
| 559 | if (s->language != language) |
| 560 | continue; |
| 561 | value = usb_gadget_get_string(s, id, buf); |
| 562 | if (value > 0) |
| 563 | return value; |
| 564 | } |
| 565 | return -EINVAL; |
| 566 | } |
| 567 | |
| 568 | static int get_string(struct usb_composite_dev *cdev, |
| 569 | void *buf, u16 language, int id) |
| 570 | { |
| 571 | struct usb_configuration *c; |
| 572 | struct usb_function *f; |
| 573 | int len; |
| 574 | |
| 575 | /* Yes, not only is USB's I18N support probably more than most |
| 576 | * folk will ever care about ... also, it's all supported here. |
| 577 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 578 | */ |
| 579 | |
| 580 | /* 0 == report all available language codes */ |
| 581 | if (id == 0) { |
| 582 | struct usb_string_descriptor *s = buf; |
| 583 | struct usb_gadget_strings **sp; |
| 584 | |
| 585 | memset(s, 0, 256); |
| 586 | s->bDescriptorType = USB_DT_STRING; |
| 587 | |
| 588 | sp = composite->strings; |
| 589 | if (sp) |
| 590 | collect_langs(sp, s->wData); |
| 591 | |
| 592 | list_for_each_entry(c, &cdev->configs, list) { |
| 593 | sp = c->strings; |
| 594 | if (sp) |
| 595 | collect_langs(sp, s->wData); |
| 596 | |
| 597 | list_for_each_entry(f, &c->functions, list) { |
| 598 | sp = f->strings; |
| 599 | if (sp) |
| 600 | collect_langs(sp, s->wData); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | for (len = 0; s->wData[len] && len <= 126; len++) |
| 605 | continue; |
| 606 | if (!len) |
| 607 | return -EINVAL; |
| 608 | |
| 609 | s->bLength = 2 * (len + 1); |
| 610 | return s->bLength; |
| 611 | } |
| 612 | |
| 613 | /* Otherwise, look up and return a specified string. String IDs |
| 614 | * are device-scoped, so we look up each string table we're told |
| 615 | * about. These lookups are infrequent; simpler-is-better here. |
| 616 | */ |
| 617 | if (composite->strings) { |
| 618 | len = lookup_string(composite->strings, buf, language, id); |
| 619 | if (len > 0) |
| 620 | return len; |
| 621 | } |
| 622 | list_for_each_entry(c, &cdev->configs, list) { |
| 623 | if (c->strings) { |
| 624 | len = lookup_string(c->strings, buf, language, id); |
| 625 | if (len > 0) |
| 626 | return len; |
| 627 | } |
| 628 | list_for_each_entry(f, &c->functions, list) { |
| 629 | if (!f->strings) |
| 630 | continue; |
| 631 | len = lookup_string(f->strings, buf, language, id); |
| 632 | if (len > 0) |
| 633 | return len; |
| 634 | } |
| 635 | } |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * usb_string_id() - allocate an unused string ID |
| 641 | * @cdev: the device whose string descriptor IDs are being allocated |
| 642 | * Context: single threaded during gadget setup |
| 643 | * |
| 644 | * @usb_string_id() is called from bind() callbacks to allocate |
| 645 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 646 | * then store that ID in the appropriate descriptors and string table. |
| 647 | * |
| 648 | * All string identifier should be allocated using this routine, to |
| 649 | * ensure that for example different functions don't wrongly assign |
| 650 | * different meanings to the same identifier. |
| 651 | */ |
| 652 | int __init usb_string_id(struct usb_composite_dev *cdev) |
| 653 | { |
| 654 | if (cdev->next_string_id < 254) { |
| 655 | /* string id 0 is reserved */ |
| 656 | cdev->next_string_id++; |
| 657 | return cdev->next_string_id; |
| 658 | } |
| 659 | return -ENODEV; |
| 660 | } |
| 661 | |
| 662 | /*-------------------------------------------------------------------------*/ |
| 663 | |
| 664 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 665 | { |
| 666 | if (req->status || req->actual != req->length) |
| 667 | DBG((struct usb_composite_dev *) ep->driver_data, |
| 668 | "setup complete --> %d, %d/%d\n", |
| 669 | req->status, req->actual, req->length); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * The setup() callback implements all the ep0 functionality that's |
| 674 | * not handled lower down, in hardware or the hardware driver(like |
| 675 | * device and endpoint feature flags, and their status). It's all |
| 676 | * housekeeping for the gadget function we're implementing. Most of |
| 677 | * the work is in config and function specific setup. |
| 678 | */ |
| 679 | static int |
| 680 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 681 | { |
| 682 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 683 | struct usb_request *req = cdev->req; |
| 684 | int value = -EOPNOTSUPP; |
| 685 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 686 | u8 intf = w_index & 0xFF; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 687 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 688 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 689 | struct usb_function *f = NULL; |
| 690 | |
| 691 | /* partial re-init of the response message; the function or the |
| 692 | * gadget might need to intercept e.g. a control-OUT completion |
| 693 | * when we delegate to it. |
| 694 | */ |
| 695 | req->zero = 0; |
| 696 | req->complete = composite_setup_complete; |
| 697 | req->length = USB_BUFSIZ; |
| 698 | gadget->ep0->driver_data = cdev; |
| 699 | |
| 700 | switch (ctrl->bRequest) { |
| 701 | |
| 702 | /* we handle all standard USB descriptors */ |
| 703 | case USB_REQ_GET_DESCRIPTOR: |
| 704 | if (ctrl->bRequestType != USB_DIR_IN) |
| 705 | goto unknown; |
| 706 | switch (w_value >> 8) { |
| 707 | |
| 708 | case USB_DT_DEVICE: |
| 709 | cdev->desc.bNumConfigurations = |
| 710 | count_configs(cdev, USB_DT_DEVICE); |
| 711 | value = min(w_length, (u16) sizeof cdev->desc); |
| 712 | memcpy(req->buf, &cdev->desc, value); |
| 713 | break; |
| 714 | case USB_DT_DEVICE_QUALIFIER: |
| 715 | if (!gadget_is_dualspeed(gadget)) |
| 716 | break; |
| 717 | device_qual(cdev); |
| 718 | value = min_t(int, w_length, |
| 719 | sizeof(struct usb_qualifier_descriptor)); |
| 720 | break; |
| 721 | case USB_DT_OTHER_SPEED_CONFIG: |
| 722 | if (!gadget_is_dualspeed(gadget)) |
| 723 | break; |
| 724 | /* FALLTHROUGH */ |
| 725 | case USB_DT_CONFIG: |
| 726 | value = config_desc(cdev, w_value); |
| 727 | if (value >= 0) |
| 728 | value = min(w_length, (u16) value); |
| 729 | break; |
| 730 | case USB_DT_STRING: |
| 731 | value = get_string(cdev, req->buf, |
| 732 | w_index, w_value & 0xff); |
| 733 | if (value >= 0) |
| 734 | value = min(w_length, (u16) value); |
| 735 | break; |
| 736 | } |
| 737 | break; |
| 738 | |
| 739 | /* any number of configs can work */ |
| 740 | case USB_REQ_SET_CONFIGURATION: |
| 741 | if (ctrl->bRequestType != 0) |
| 742 | goto unknown; |
| 743 | if (gadget_is_otg(gadget)) { |
| 744 | if (gadget->a_hnp_support) |
| 745 | DBG(cdev, "HNP available\n"); |
| 746 | else if (gadget->a_alt_hnp_support) |
| 747 | DBG(cdev, "HNP on another port\n"); |
| 748 | else |
| 749 | VDBG(cdev, "HNP inactive\n"); |
| 750 | } |
| 751 | spin_lock(&cdev->lock); |
| 752 | value = set_config(cdev, ctrl, w_value); |
| 753 | spin_unlock(&cdev->lock); |
| 754 | break; |
| 755 | case USB_REQ_GET_CONFIGURATION: |
| 756 | if (ctrl->bRequestType != USB_DIR_IN) |
| 757 | goto unknown; |
| 758 | if (cdev->config) |
| 759 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 760 | else |
| 761 | *(u8 *)req->buf = 0; |
| 762 | value = min(w_length, (u16) 1); |
| 763 | break; |
| 764 | |
| 765 | /* function drivers must handle get/set altsetting; if there's |
| 766 | * no get() method, we know only altsetting zero works. |
| 767 | */ |
| 768 | case USB_REQ_SET_INTERFACE: |
| 769 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 770 | goto unknown; |
| 771 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 772 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 773 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 774 | if (!f) |
| 775 | break; |
Bryan Wu | dd4dff8 | 2009-01-08 00:21:18 +0800 | [diff] [blame] | 776 | if (w_value && !f->set_alt) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 777 | break; |
| 778 | value = f->set_alt(f, w_index, w_value); |
| 779 | break; |
| 780 | case USB_REQ_GET_INTERFACE: |
| 781 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 782 | goto unknown; |
| 783 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 784 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 785 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 786 | if (!f) |
| 787 | break; |
| 788 | /* lots of interfaces only need altsetting zero... */ |
| 789 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 790 | if (value < 0) |
| 791 | break; |
| 792 | *((u8 *)req->buf) = value; |
| 793 | value = min(w_length, (u16) 1); |
| 794 | break; |
| 795 | default: |
| 796 | unknown: |
| 797 | VDBG(cdev, |
| 798 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 799 | ctrl->bRequestType, ctrl->bRequest, |
| 800 | w_value, w_index, w_length); |
| 801 | |
| 802 | /* functions always handle their interfaces ... punt other |
| 803 | * recipients (endpoint, other, WUSB, ...) to the current |
| 804 | * configuration code. |
| 805 | * |
| 806 | * REVISIT it could make sense to let the composite device |
| 807 | * take such requests too, if that's ever needed: to work |
| 808 | * in config 0, etc. |
| 809 | */ |
| 810 | if ((ctrl->bRequestType & USB_RECIP_MASK) |
| 811 | == USB_RECIP_INTERFACE) { |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 812 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 813 | if (f && f->setup) |
| 814 | value = f->setup(f, ctrl); |
| 815 | else |
| 816 | f = NULL; |
| 817 | } |
| 818 | if (value < 0 && !f) { |
| 819 | struct usb_configuration *c; |
| 820 | |
| 821 | c = cdev->config; |
| 822 | if (c && c->setup) |
| 823 | value = c->setup(c, ctrl); |
| 824 | } |
| 825 | |
| 826 | goto done; |
| 827 | } |
| 828 | |
| 829 | /* respond with data transfer before status phase? */ |
| 830 | if (value >= 0) { |
| 831 | req->length = value; |
| 832 | req->zero = value < w_length; |
| 833 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 834 | if (value < 0) { |
| 835 | DBG(cdev, "ep_queue --> %d\n", value); |
| 836 | req->status = 0; |
| 837 | composite_setup_complete(gadget->ep0, req); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | done: |
| 842 | /* device either stalls (value < 0) or reports success */ |
| 843 | return value; |
| 844 | } |
| 845 | |
| 846 | static void composite_disconnect(struct usb_gadget *gadget) |
| 847 | { |
| 848 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 849 | unsigned long flags; |
| 850 | |
| 851 | /* REVISIT: should we have config and device level |
| 852 | * disconnect callbacks? |
| 853 | */ |
| 854 | spin_lock_irqsave(&cdev->lock, flags); |
| 855 | if (cdev->config) |
| 856 | reset_config(cdev); |
| 857 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 858 | } |
| 859 | |
| 860 | /*-------------------------------------------------------------------------*/ |
| 861 | |
| 862 | static void /* __init_or_exit */ |
| 863 | composite_unbind(struct usb_gadget *gadget) |
| 864 | { |
| 865 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 866 | |
| 867 | /* composite_disconnect() must already have been called |
| 868 | * by the underlying peripheral controller driver! |
| 869 | * so there's no i/o concurrency that could affect the |
| 870 | * state protected by cdev->lock. |
| 871 | */ |
| 872 | WARN_ON(cdev->config); |
| 873 | |
| 874 | while (!list_empty(&cdev->configs)) { |
| 875 | struct usb_configuration *c; |
| 876 | |
| 877 | c = list_first_entry(&cdev->configs, |
| 878 | struct usb_configuration, list); |
| 879 | while (!list_empty(&c->functions)) { |
| 880 | struct usb_function *f; |
| 881 | |
| 882 | f = list_first_entry(&c->functions, |
| 883 | struct usb_function, list); |
| 884 | list_del(&f->list); |
| 885 | if (f->unbind) { |
| 886 | DBG(cdev, "unbind function '%s'/%p\n", |
| 887 | f->name, f); |
| 888 | f->unbind(c, f); |
| 889 | /* may free memory for "f" */ |
| 890 | } |
| 891 | } |
| 892 | list_del(&c->list); |
| 893 | if (c->unbind) { |
| 894 | DBG(cdev, "unbind config '%s'/%p\n", c->label, c); |
| 895 | c->unbind(c); |
| 896 | /* may free memory for "c" */ |
| 897 | } |
| 898 | } |
| 899 | if (composite->unbind) |
| 900 | composite->unbind(cdev); |
| 901 | |
| 902 | if (cdev->req) { |
| 903 | kfree(cdev->req->buf); |
| 904 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 905 | } |
| 906 | kfree(cdev); |
| 907 | set_gadget_data(gadget, NULL); |
| 908 | composite = NULL; |
| 909 | } |
| 910 | |
| 911 | static void __init |
| 912 | string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s) |
| 913 | { |
| 914 | struct usb_string *str = tab->strings; |
| 915 | |
| 916 | for (str = tab->strings; str->s; str++) { |
| 917 | if (str->id == id) { |
| 918 | str->s = s; |
| 919 | return; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | static void __init |
| 925 | string_override(struct usb_gadget_strings **tab, u8 id, const char *s) |
| 926 | { |
| 927 | while (*tab) { |
| 928 | string_override_one(*tab, id, s); |
| 929 | tab++; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | static int __init composite_bind(struct usb_gadget *gadget) |
| 934 | { |
| 935 | struct usb_composite_dev *cdev; |
| 936 | int status = -ENOMEM; |
| 937 | |
| 938 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); |
| 939 | if (!cdev) |
| 940 | return status; |
| 941 | |
| 942 | spin_lock_init(&cdev->lock); |
| 943 | cdev->gadget = gadget; |
| 944 | set_gadget_data(gadget, cdev); |
| 945 | INIT_LIST_HEAD(&cdev->configs); |
| 946 | |
| 947 | /* preallocate control response and buffer */ |
| 948 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 949 | if (!cdev->req) |
| 950 | goto fail; |
| 951 | cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); |
| 952 | if (!cdev->req->buf) |
| 953 | goto fail; |
| 954 | cdev->req->complete = composite_setup_complete; |
| 955 | gadget->ep0->driver_data = cdev; |
| 956 | |
| 957 | cdev->bufsiz = USB_BUFSIZ; |
| 958 | cdev->driver = composite; |
| 959 | |
| 960 | usb_gadget_set_selfpowered(gadget); |
| 961 | |
| 962 | /* interface and string IDs start at zero via kzalloc. |
| 963 | * we force endpoints to start unassigned; few controller |
| 964 | * drivers will zero ep->driver_data. |
| 965 | */ |
| 966 | usb_ep_autoconfig_reset(cdev->gadget); |
| 967 | |
| 968 | /* composite gadget needs to assign strings for whole device (like |
| 969 | * serial number), register function drivers, potentially update |
| 970 | * power state and consumption, etc |
| 971 | */ |
| 972 | status = composite->bind(cdev); |
| 973 | if (status < 0) |
| 974 | goto fail; |
| 975 | |
| 976 | cdev->desc = *composite->dev; |
| 977 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 978 | |
| 979 | /* standardized runtime overrides for device ID data */ |
| 980 | if (idVendor) |
| 981 | cdev->desc.idVendor = cpu_to_le16(idVendor); |
| 982 | if (idProduct) |
| 983 | cdev->desc.idProduct = cpu_to_le16(idProduct); |
| 984 | if (bcdDevice) |
| 985 | cdev->desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 986 | |
| 987 | /* strings can't be assigned before bind() allocates the |
| 988 | * releavnt identifiers |
| 989 | */ |
| 990 | if (cdev->desc.iManufacturer && iManufacturer) |
| 991 | string_override(composite->strings, |
| 992 | cdev->desc.iManufacturer, iManufacturer); |
| 993 | if (cdev->desc.iProduct && iProduct) |
| 994 | string_override(composite->strings, |
| 995 | cdev->desc.iProduct, iProduct); |
| 996 | if (cdev->desc.iSerialNumber && iSerialNumber) |
| 997 | string_override(composite->strings, |
| 998 | cdev->desc.iSerialNumber, iSerialNumber); |
| 999 | |
| 1000 | INFO(cdev, "%s ready\n", composite->name); |
| 1001 | return 0; |
| 1002 | |
| 1003 | fail: |
| 1004 | composite_unbind(gadget); |
| 1005 | return status; |
| 1006 | } |
| 1007 | |
| 1008 | /*-------------------------------------------------------------------------*/ |
| 1009 | |
| 1010 | static void |
| 1011 | composite_suspend(struct usb_gadget *gadget) |
| 1012 | { |
| 1013 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1014 | struct usb_function *f; |
| 1015 | |
| 1016 | /* REVISIT: should we have config and device level |
| 1017 | * suspend/resume callbacks? |
| 1018 | */ |
| 1019 | DBG(cdev, "suspend\n"); |
| 1020 | if (cdev->config) { |
| 1021 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1022 | if (f->suspend) |
| 1023 | f->suspend(f); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | static void |
| 1029 | composite_resume(struct usb_gadget *gadget) |
| 1030 | { |
| 1031 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1032 | struct usb_function *f; |
| 1033 | |
| 1034 | /* REVISIT: should we have config and device level |
| 1035 | * suspend/resume callbacks? |
| 1036 | */ |
| 1037 | DBG(cdev, "resume\n"); |
| 1038 | if (cdev->config) { |
| 1039 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1040 | if (f->resume) |
| 1041 | f->resume(f); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /*-------------------------------------------------------------------------*/ |
| 1047 | |
| 1048 | static struct usb_gadget_driver composite_driver = { |
| 1049 | .speed = USB_SPEED_HIGH, |
| 1050 | |
| 1051 | .bind = composite_bind, |
| 1052 | .unbind = __exit_p(composite_unbind), |
| 1053 | |
| 1054 | .setup = composite_setup, |
| 1055 | .disconnect = composite_disconnect, |
| 1056 | |
| 1057 | .suspend = composite_suspend, |
| 1058 | .resume = composite_resume, |
| 1059 | |
| 1060 | .driver = { |
| 1061 | .owner = THIS_MODULE, |
| 1062 | }, |
| 1063 | }; |
| 1064 | |
| 1065 | /** |
| 1066 | * usb_composite_register() - register a composite driver |
| 1067 | * @driver: the driver to register |
| 1068 | * Context: single threaded during gadget setup |
| 1069 | * |
| 1070 | * This function is used to register drivers using the composite driver |
| 1071 | * framework. The return value is zero, or a negative errno value. |
| 1072 | * Those values normally come from the driver's @bind method, which does |
| 1073 | * all the work of setting up the driver to match the hardware. |
| 1074 | * |
| 1075 | * On successful return, the gadget is ready to respond to requests from |
| 1076 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 1077 | * while it was binding. That would usually be done in order to wait for |
| 1078 | * some userspace participation. |
| 1079 | */ |
| 1080 | int __init usb_composite_register(struct usb_composite_driver *driver) |
| 1081 | { |
| 1082 | if (!driver || !driver->dev || !driver->bind || composite) |
| 1083 | return -EINVAL; |
| 1084 | |
| 1085 | if (!driver->name) |
| 1086 | driver->name = "composite"; |
| 1087 | composite_driver.function = (char *) driver->name; |
| 1088 | composite_driver.driver.name = driver->name; |
| 1089 | composite = driver; |
| 1090 | |
| 1091 | return usb_gadget_register_driver(&composite_driver); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * usb_composite_unregister() - unregister a composite driver |
| 1096 | * @driver: the driver to unregister |
| 1097 | * |
| 1098 | * This function is used to unregister drivers using the composite |
| 1099 | * driver framework. |
| 1100 | */ |
| 1101 | void __exit usb_composite_unregister(struct usb_composite_driver *driver) |
| 1102 | { |
| 1103 | if (composite != driver) |
| 1104 | return; |
| 1105 | usb_gadget_unregister_driver(&composite_driver); |
| 1106 | } |