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> |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 27 | #include <linux/utsname.h> |
Vikram Pandita | 8abaec8 | 2011-02-07 21:20:50 -0800 | [diff] [blame] | 28 | #include <linux/delay.h> |
| 29 | #include <linux/kdev_t.h> |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 30 | #include <linux/usb/composite.h> |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * The code in this file is utility code, used to build a gadget driver |
| 35 | * from one or more "function" drivers, one or more "configuration" |
| 36 | * objects, and a "usb_composite_driver" by gluing them together along |
| 37 | * with the relevant device-wide data. |
| 38 | */ |
| 39 | |
| 40 | /* big enough to hold our biggest descriptor */ |
Robert Lukassen | dd0543e | 2010-03-30 14:14:01 +0200 | [diff] [blame] | 41 | #define USB_BUFSIZ 1024 |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 42 | |
| 43 | static struct usb_composite_driver *composite; |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 44 | static int (*composite_gadget_bind)(struct usb_composite_dev *cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 45 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 46 | /* Some systems will need runtime overrides for the product identifiers |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 47 | * published in the device descriptor, either numbers or strings or both. |
| 48 | * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). |
| 49 | */ |
| 50 | |
| 51 | static ushort idVendor; |
| 52 | module_param(idVendor, ushort, 0); |
| 53 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); |
| 54 | |
| 55 | static ushort idProduct; |
| 56 | module_param(idProduct, ushort, 0); |
| 57 | MODULE_PARM_DESC(idProduct, "USB Product ID"); |
| 58 | |
| 59 | static ushort bcdDevice; |
| 60 | module_param(bcdDevice, ushort, 0); |
| 61 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); |
| 62 | |
| 63 | static char *iManufacturer; |
| 64 | module_param(iManufacturer, charp, 0); |
| 65 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); |
| 66 | |
| 67 | static char *iProduct; |
| 68 | module_param(iProduct, charp, 0); |
| 69 | MODULE_PARM_DESC(iProduct, "USB Product string"); |
| 70 | |
| 71 | static char *iSerialNumber; |
| 72 | module_param(iSerialNumber, charp, 0); |
| 73 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); |
| 74 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 75 | static char composite_manufacturer[50]; |
| 76 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 77 | /*-------------------------------------------------------------------------*/ |
| 78 | |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 79 | static ssize_t enable_show(struct device *dev, struct device_attribute *attr, |
| 80 | char *buf) |
| 81 | { |
| 82 | struct usb_function *f = dev_get_drvdata(dev); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 83 | return sprintf(buf, "%d\n", !f->disabled); |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static ssize_t enable_store( |
| 87 | struct device *dev, struct device_attribute *attr, |
| 88 | const char *buf, size_t size) |
| 89 | { |
| 90 | struct usb_function *f = dev_get_drvdata(dev); |
| 91 | struct usb_composite_driver *driver = f->config->cdev->driver; |
| 92 | int value; |
| 93 | |
| 94 | sscanf(buf, "%d", &value); |
| 95 | if (driver->enable_function) |
| 96 | driver->enable_function(f, value); |
| 97 | else |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 98 | usb_function_set_enabled(f, value); |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 99 | |
| 100 | return size; |
| 101 | } |
| 102 | |
| 103 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store); |
| 104 | |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 105 | void usb_function_set_enabled(struct usb_function *f, int enabled) |
| 106 | { |
| 107 | f->disabled = !enabled; |
| 108 | kobject_uevent(&f->dev->kobj, KOBJ_CHANGE); |
| 109 | } |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 110 | |
Mike Lockwood | 28acc1a | 2010-06-28 16:19:32 -0400 | [diff] [blame] | 111 | |
| 112 | void usb_composite_force_reset(struct usb_composite_dev *cdev) |
| 113 | { |
| 114 | unsigned long flags; |
| 115 | |
| 116 | spin_lock_irqsave(&cdev->lock, flags); |
| 117 | /* force reenumeration */ |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 118 | if (cdev && cdev->gadget && cdev->gadget->speed != USB_SPEED_UNKNOWN) { |
Mike Lockwood | 28acc1a | 2010-06-28 16:19:32 -0400 | [diff] [blame] | 119 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 120 | |
| 121 | usb_gadget_disconnect(cdev->gadget); |
| 122 | msleep(10); |
| 123 | usb_gadget_connect(cdev->gadget); |
| 124 | } else { |
| 125 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 126 | } |
| 127 | } |
| 128 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 129 | /** |
| 130 | * usb_add_function() - add a function to a configuration |
| 131 | * @config: the configuration |
| 132 | * @function: the function being added |
| 133 | * Context: single threaded during gadget setup |
| 134 | * |
| 135 | * After initialization, each configuration must have one or more |
| 136 | * functions added to it. Adding a function involves calling its @bind() |
| 137 | * method to allocate resources such as interface and string identifiers |
| 138 | * and endpoints. |
| 139 | * |
| 140 | * This function returns the value of the function's bind(), which is |
| 141 | * zero for success else a negative errno value. |
| 142 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 143 | int usb_add_function(struct usb_configuration *config, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 144 | struct usb_function *function) |
| 145 | { |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 146 | struct usb_composite_dev *cdev = config->cdev; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 147 | int value = -EINVAL; |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 148 | int index; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 149 | |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 150 | DBG(cdev, "adding '%s'/%p to config '%s'/%p\n", |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 151 | function->name, function, |
| 152 | config->label, config); |
| 153 | |
| 154 | if (!function->set_alt || !function->disable) |
| 155 | goto done; |
| 156 | |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 157 | index = atomic_inc_return(&cdev->driver->function_count); |
| 158 | function->dev = device_create(cdev->driver->class, NULL, |
| 159 | MKDEV(0, index), NULL, function->name); |
| 160 | if (IS_ERR(function->dev)) |
| 161 | return PTR_ERR(function->dev); |
| 162 | |
| 163 | value = device_create_file(function->dev, &dev_attr_enable); |
| 164 | if (value < 0) { |
| 165 | device_destroy(cdev->driver->class, MKDEV(0, index)); |
| 166 | return value; |
| 167 | } |
| 168 | dev_set_drvdata(function->dev, function); |
| 169 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 170 | function->config = config; |
| 171 | list_add_tail(&function->list, &config->functions); |
| 172 | |
| 173 | /* REVISIT *require* function->bind? */ |
| 174 | if (function->bind) { |
| 175 | value = function->bind(config, function); |
| 176 | if (value < 0) { |
| 177 | list_del(&function->list); |
| 178 | function->config = NULL; |
| 179 | } |
| 180 | } else |
| 181 | value = 0; |
| 182 | |
| 183 | /* We allow configurations that don't work at both speeds. |
| 184 | * If we run into a lowspeed Linux system, treat it the same |
| 185 | * as full speed ... it's the function drivers that will need |
| 186 | * to avoid bulk and ISO transfers. |
| 187 | */ |
| 188 | if (!config->fullspeed && function->descriptors) |
| 189 | config->fullspeed = true; |
| 190 | if (!config->highspeed && function->hs_descriptors) |
| 191 | config->highspeed = true; |
| 192 | |
| 193 | done: |
| 194 | if (value) |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 195 | DBG(cdev, "adding '%s'/%p --> %d\n", |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 196 | function->name, function, value); |
| 197 | return value; |
| 198 | } |
| 199 | |
| 200 | /** |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 201 | * usb_function_deactivate - prevent function and gadget enumeration |
| 202 | * @function: the function that isn't yet ready to respond |
| 203 | * |
| 204 | * Blocks response of the gadget driver to host enumeration by |
| 205 | * preventing the data line pullup from being activated. This is |
| 206 | * normally called during @bind() processing to change from the |
| 207 | * initial "ready to respond" state, or when a required resource |
| 208 | * becomes available. |
| 209 | * |
| 210 | * For example, drivers that serve as a passthrough to a userspace |
| 211 | * daemon can block enumeration unless that daemon (such as an OBEX, |
| 212 | * MTP, or print server) is ready to handle host requests. |
| 213 | * |
| 214 | * Not all systems support software control of their USB peripheral |
| 215 | * data pullups. |
| 216 | * |
| 217 | * Returns zero on success, else negative errno. |
| 218 | */ |
| 219 | int usb_function_deactivate(struct usb_function *function) |
| 220 | { |
| 221 | struct usb_composite_dev *cdev = function->config->cdev; |
Felipe Balbi | b2bdf3a | 2009-02-12 15:09:47 +0200 | [diff] [blame] | 222 | unsigned long flags; |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 223 | int status = 0; |
| 224 | |
Felipe Balbi | b2bdf3a | 2009-02-12 15:09:47 +0200 | [diff] [blame] | 225 | spin_lock_irqsave(&cdev->lock, flags); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 226 | |
| 227 | if (cdev->deactivations == 0) |
| 228 | status = usb_gadget_disconnect(cdev->gadget); |
| 229 | if (status == 0) |
| 230 | cdev->deactivations++; |
| 231 | |
Felipe Balbi | b2bdf3a | 2009-02-12 15:09:47 +0200 | [diff] [blame] | 232 | spin_unlock_irqrestore(&cdev->lock, flags); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 233 | return status; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * usb_function_activate - allow function and gadget enumeration |
| 238 | * @function: function on which usb_function_activate() was called |
| 239 | * |
| 240 | * Reverses effect of usb_function_deactivate(). If no more functions |
| 241 | * are delaying their activation, the gadget driver will respond to |
| 242 | * host enumeration procedures. |
| 243 | * |
| 244 | * Returns zero on success, else negative errno. |
| 245 | */ |
| 246 | int usb_function_activate(struct usb_function *function) |
| 247 | { |
| 248 | struct usb_composite_dev *cdev = function->config->cdev; |
| 249 | int status = 0; |
| 250 | |
| 251 | spin_lock(&cdev->lock); |
| 252 | |
| 253 | if (WARN_ON(cdev->deactivations == 0)) |
| 254 | status = -EINVAL; |
| 255 | else { |
| 256 | cdev->deactivations--; |
| 257 | if (cdev->deactivations == 0) |
| 258 | status = usb_gadget_connect(cdev->gadget); |
| 259 | } |
| 260 | |
| 261 | spin_unlock(&cdev->lock); |
| 262 | return status; |
| 263 | } |
| 264 | |
| 265 | /** |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 266 | * usb_interface_id() - allocate an unused interface ID |
| 267 | * @config: configuration associated with the interface |
| 268 | * @function: function handling the interface |
| 269 | * Context: single threaded during gadget setup |
| 270 | * |
| 271 | * usb_interface_id() is called from usb_function.bind() callbacks to |
| 272 | * allocate new interface IDs. The function driver will then store that |
| 273 | * ID in interface, association, CDC union, and other descriptors. It |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 274 | * will also handle any control requests targeted at that interface, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 275 | * particularly changing its altsetting via set_alt(). There may |
| 276 | * also be class-specific or vendor-specific requests to handle. |
| 277 | * |
| 278 | * All interface identifier should be allocated using this routine, to |
| 279 | * ensure that for example different functions don't wrongly assign |
| 280 | * different meanings to the same identifier. Note that since interface |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 281 | * identifiers are configuration-specific, functions used in more than |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 282 | * one configuration (or more than once in a given configuration) need |
| 283 | * multiple versions of the relevant descriptors. |
| 284 | * |
| 285 | * Returns the interface ID which was allocated; or -ENODEV if no |
| 286 | * more interface IDs can be allocated. |
| 287 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 288 | int usb_interface_id(struct usb_configuration *config, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 289 | struct usb_function *function) |
| 290 | { |
| 291 | unsigned id = config->next_interface_id; |
| 292 | |
| 293 | if (id < MAX_CONFIG_INTERFACES) { |
| 294 | config->interface[id] = function; |
| 295 | config->next_interface_id = id + 1; |
| 296 | return id; |
| 297 | } |
| 298 | return -ENODEV; |
| 299 | } |
| 300 | |
| 301 | static int config_buf(struct usb_configuration *config, |
| 302 | enum usb_device_speed speed, void *buf, u8 type) |
| 303 | { |
| 304 | struct usb_config_descriptor *c = buf; |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 305 | struct usb_interface_descriptor *intf; |
John Michelau | f71a394 | 2010-12-10 12:09:53 -0600 | [diff] [blame] | 306 | struct usb_interface_assoc_descriptor *iad = NULL; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 307 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 308 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 309 | struct usb_function *f; |
| 310 | int status; |
Jared Suttles | 646bb86 | 2009-07-30 16:13:27 -0500 | [diff] [blame] | 311 | int interfaceCount = 0; |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 312 | u8 *dest; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 313 | |
| 314 | /* write the config descriptor */ |
| 315 | c = buf; |
| 316 | c->bLength = USB_DT_CONFIG_SIZE; |
| 317 | c->bDescriptorType = type; |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 318 | /* wTotalLength and bNumInterfaces are written later */ |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 319 | c->bConfigurationValue = config->bConfigurationValue; |
| 320 | c->iConfiguration = config->iConfiguration; |
| 321 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
David Brownell | 36e893d | 2008-09-12 09:39:06 -0700 | [diff] [blame] | 322 | c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 323 | |
| 324 | /* There may be e.g. OTG descriptors */ |
| 325 | if (config->descriptors) { |
| 326 | status = usb_descriptor_fillbuf(next, len, |
| 327 | config->descriptors); |
| 328 | if (status < 0) |
| 329 | return status; |
| 330 | len -= status; |
| 331 | next += status; |
| 332 | } |
| 333 | |
| 334 | /* add each function's descriptors */ |
| 335 | list_for_each_entry(f, &config->functions, list) { |
| 336 | struct usb_descriptor_header **descriptors; |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 337 | struct usb_descriptor_header *descriptor; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 338 | |
| 339 | if (speed == USB_SPEED_HIGH) |
| 340 | descriptors = f->hs_descriptors; |
| 341 | else |
| 342 | descriptors = f->descriptors; |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 343 | if (f->disabled || !descriptors || descriptors[0] == NULL) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 344 | continue; |
| 345 | status = usb_descriptor_fillbuf(next, len, |
| 346 | (const struct usb_descriptor_header **) descriptors); |
| 347 | if (status < 0) |
| 348 | return status; |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 349 | |
| 350 | /* set interface numbers dynamically */ |
| 351 | dest = next; |
| 352 | while ((descriptor = *descriptors++) != NULL) { |
| 353 | intf = (struct usb_interface_descriptor *)dest; |
Mike Lockwood | 0f63be2 | 2010-02-26 09:34:19 -0500 | [diff] [blame] | 354 | if (intf->bDescriptorType == USB_DT_INTERFACE) { |
| 355 | /* don't increment bInterfaceNumber for alternate settings */ |
| 356 | if (intf->bAlternateSetting == 0) |
| 357 | intf->bInterfaceNumber = interfaceCount++; |
| 358 | else |
| 359 | intf->bInterfaceNumber = interfaceCount - 1; |
John Michelau | f71a394 | 2010-12-10 12:09:53 -0600 | [diff] [blame] | 360 | if (iad) { |
| 361 | iad->bFirstInterface = |
| 362 | intf->bInterfaceNumber; |
| 363 | iad = NULL; |
| 364 | } |
| 365 | } else if (intf->bDescriptorType == |
| 366 | USB_DT_INTERFACE_ASSOCIATION) { |
| 367 | /* This will be first if it exists. Save |
| 368 | * a pointer to it so we can properly set |
| 369 | * bFirstInterface when we process the first |
| 370 | * interface. |
| 371 | */ |
| 372 | iad = (struct usb_interface_assoc_descriptor *) |
| 373 | dest; |
Mike Lockwood | 0f63be2 | 2010-02-26 09:34:19 -0500 | [diff] [blame] | 374 | } |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 375 | dest += intf->bLength; |
| 376 | } |
| 377 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 378 | len -= status; |
| 379 | next += status; |
| 380 | } |
| 381 | |
| 382 | len = next - buf; |
| 383 | c->wTotalLength = cpu_to_le16(len); |
Mike Lockwood | c832ca8 | 2010-02-13 19:16:07 -0500 | [diff] [blame] | 384 | c->bNumInterfaces = interfaceCount; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 385 | return len; |
| 386 | } |
| 387 | |
| 388 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) |
| 389 | { |
| 390 | struct usb_gadget *gadget = cdev->gadget; |
| 391 | struct usb_configuration *c; |
| 392 | u8 type = w_value >> 8; |
| 393 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 394 | |
| 395 | if (gadget_is_dualspeed(gadget)) { |
| 396 | int hs = 0; |
| 397 | |
| 398 | if (gadget->speed == USB_SPEED_HIGH) |
| 399 | hs = 1; |
| 400 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 401 | hs = !hs; |
| 402 | if (hs) |
| 403 | speed = USB_SPEED_HIGH; |
| 404 | |
| 405 | } |
| 406 | |
| 407 | /* This is a lookup by config *INDEX* */ |
| 408 | w_value &= 0xff; |
| 409 | list_for_each_entry(c, &cdev->configs, list) { |
| 410 | /* ignore configs that won't work at this speed */ |
| 411 | if (speed == USB_SPEED_HIGH) { |
| 412 | if (!c->highspeed) |
| 413 | continue; |
| 414 | } else { |
| 415 | if (!c->fullspeed) |
| 416 | continue; |
| 417 | } |
| 418 | if (w_value == 0) |
| 419 | return config_buf(c, speed, cdev->req->buf, type); |
| 420 | w_value--; |
| 421 | } |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | |
| 425 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) |
| 426 | { |
| 427 | struct usb_gadget *gadget = cdev->gadget; |
| 428 | struct usb_configuration *c; |
| 429 | unsigned count = 0; |
| 430 | int hs = 0; |
| 431 | |
| 432 | if (gadget_is_dualspeed(gadget)) { |
| 433 | if (gadget->speed == USB_SPEED_HIGH) |
| 434 | hs = 1; |
| 435 | if (type == USB_DT_DEVICE_QUALIFIER) |
| 436 | hs = !hs; |
| 437 | } |
| 438 | list_for_each_entry(c, &cdev->configs, list) { |
| 439 | /* ignore configs that won't work at this speed */ |
| 440 | if (hs) { |
| 441 | if (!c->highspeed) |
| 442 | continue; |
| 443 | } else { |
| 444 | if (!c->fullspeed) |
| 445 | continue; |
| 446 | } |
| 447 | count++; |
| 448 | } |
| 449 | return count; |
| 450 | } |
| 451 | |
| 452 | static void device_qual(struct usb_composite_dev *cdev) |
| 453 | { |
| 454 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 455 | |
| 456 | qual->bLength = sizeof(*qual); |
| 457 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 458 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 459 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 460 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 461 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 462 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 463 | /* ASSUME same EP0 fifo size at both speeds */ |
| 464 | qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0; |
| 465 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
David Lopo | c24f422 | 2008-07-01 13:14:17 -0700 | [diff] [blame] | 466 | qual->bRESERVED = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /*-------------------------------------------------------------------------*/ |
| 470 | |
| 471 | static void reset_config(struct usb_composite_dev *cdev) |
| 472 | { |
| 473 | struct usb_function *f; |
| 474 | |
| 475 | DBG(cdev, "reset config\n"); |
| 476 | |
| 477 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 478 | if (f->disable) |
| 479 | f->disable(f); |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 480 | |
| 481 | bitmap_zero(f->endpoints, 32); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 482 | } |
| 483 | cdev->config = NULL; |
| 484 | } |
| 485 | |
| 486 | static int set_config(struct usb_composite_dev *cdev, |
| 487 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 488 | { |
| 489 | struct usb_gadget *gadget = cdev->gadget; |
| 490 | struct usb_configuration *c = NULL; |
| 491 | int result = -EINVAL; |
| 492 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 493 | int tmp; |
| 494 | |
| 495 | if (cdev->config) |
| 496 | reset_config(cdev); |
| 497 | |
| 498 | if (number) { |
| 499 | list_for_each_entry(c, &cdev->configs, list) { |
| 500 | if (c->bConfigurationValue == number) { |
| 501 | result = 0; |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | if (result < 0) |
| 506 | goto done; |
| 507 | } else |
| 508 | result = 0; |
| 509 | |
| 510 | INFO(cdev, "%s speed config #%d: %s\n", |
| 511 | ({ char *speed; |
| 512 | switch (gadget->speed) { |
| 513 | case USB_SPEED_LOW: speed = "low"; break; |
| 514 | case USB_SPEED_FULL: speed = "full"; break; |
| 515 | case USB_SPEED_HIGH: speed = "high"; break; |
| 516 | default: speed = "?"; break; |
| 517 | } ; speed; }), number, c ? c->label : "unconfigured"); |
| 518 | |
| 519 | if (!c) |
| 520 | goto done; |
| 521 | |
| 522 | cdev->config = c; |
| 523 | |
| 524 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 525 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 526 | struct usb_function *f = c->interface[tmp]; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 527 | struct usb_descriptor_header **descriptors; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 528 | |
| 529 | if (!f) |
| 530 | break; |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 531 | if (f->disabled) |
Mike Lockwood | 8c6a6b2 | 2010-02-26 09:30:01 -0500 | [diff] [blame] | 532 | continue; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 533 | |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 534 | /* |
| 535 | * Record which endpoints are used by the function. This is used |
| 536 | * to dispatch control requests targeted at that endpoint to the |
| 537 | * function's setup callback instead of the current |
| 538 | * configuration's setup callback. |
| 539 | */ |
| 540 | if (gadget->speed == USB_SPEED_HIGH) |
| 541 | descriptors = f->hs_descriptors; |
| 542 | else |
| 543 | descriptors = f->descriptors; |
| 544 | |
| 545 | for (; *descriptors; ++descriptors) { |
| 546 | struct usb_endpoint_descriptor *ep; |
| 547 | int addr; |
| 548 | |
| 549 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 550 | continue; |
| 551 | |
| 552 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 553 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 554 | | (ep->bEndpointAddress & 0x0f); |
| 555 | set_bit(addr, f->endpoints); |
| 556 | } |
| 557 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 558 | result = f->set_alt(f, tmp, 0); |
| 559 | if (result < 0) { |
| 560 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", |
| 561 | tmp, f->name, f, result); |
| 562 | |
| 563 | reset_config(cdev); |
| 564 | goto done; |
| 565 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 566 | |
| 567 | if (result == USB_GADGET_DELAYED_STATUS) { |
| 568 | DBG(cdev, |
| 569 | "%s: interface %d (%s) requested delayed status\n", |
| 570 | __func__, tmp, f->name); |
| 571 | cdev->delayed_status++; |
| 572 | DBG(cdev, "delayed_status count %d\n", |
| 573 | cdev->delayed_status); |
| 574 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* when we return, be sure our power usage is valid */ |
David Brownell | 36e893d | 2008-09-12 09:39:06 -0700 | [diff] [blame] | 578 | power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 579 | done: |
| 580 | usb_gadget_vbus_draw(gadget, power); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 581 | |
Mike Lockwood | 8da4cc8 | 2010-06-27 20:05:55 -0400 | [diff] [blame] | 582 | schedule_work(&cdev->switch_work); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 583 | |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 584 | if (result >= 0 && cdev->delayed_status) |
| 585 | result = USB_GADGET_DELAYED_STATUS; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 586 | return result; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * usb_add_config() - add a configuration to a device. |
| 591 | * @cdev: wraps the USB gadget |
| 592 | * @config: the configuration, with bConfigurationValue assigned |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 593 | * @bind: the configuration's bind function |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 594 | * Context: single threaded during gadget setup |
| 595 | * |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 596 | * One of the main tasks of a composite @bind() routine is to |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 597 | * add each of the configurations it supports, using this routine. |
| 598 | * |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 599 | * This function returns the value of the configuration's @bind(), which |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 600 | * is zero for success else a negative errno value. Binding configurations |
| 601 | * assigns global resources including string IDs, and per-configuration |
| 602 | * resources such as interface IDs and endpoints. |
| 603 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 604 | int usb_add_config(struct usb_composite_dev *cdev, |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 605 | struct usb_configuration *config, |
| 606 | int (*bind)(struct usb_configuration *)) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 607 | { |
| 608 | int status = -EINVAL; |
| 609 | struct usb_configuration *c; |
| 610 | |
| 611 | DBG(cdev, "adding config #%u '%s'/%p\n", |
| 612 | config->bConfigurationValue, |
| 613 | config->label, config); |
| 614 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 615 | if (!config->bConfigurationValue || !bind) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 616 | goto done; |
| 617 | |
| 618 | /* Prevent duplicate configuration identifiers */ |
| 619 | list_for_each_entry(c, &cdev->configs, list) { |
| 620 | if (c->bConfigurationValue == config->bConfigurationValue) { |
| 621 | status = -EBUSY; |
| 622 | goto done; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | config->cdev = cdev; |
| 627 | list_add_tail(&config->list, &cdev->configs); |
| 628 | |
| 629 | INIT_LIST_HEAD(&config->functions); |
| 630 | config->next_interface_id = 0; |
| 631 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 632 | status = bind(config); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 633 | if (status < 0) { |
| 634 | list_del(&config->list); |
| 635 | config->cdev = NULL; |
| 636 | } else { |
| 637 | unsigned i; |
| 638 | |
| 639 | DBG(cdev, "cfg %d/%p speeds:%s%s\n", |
| 640 | config->bConfigurationValue, config, |
| 641 | config->highspeed ? " high" : "", |
| 642 | config->fullspeed |
| 643 | ? (gadget_is_dualspeed(cdev->gadget) |
| 644 | ? " full" |
| 645 | : " full/low") |
| 646 | : ""); |
| 647 | |
| 648 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 649 | struct usb_function *f = config->interface[i]; |
| 650 | |
| 651 | if (!f) |
| 652 | continue; |
| 653 | DBG(cdev, " interface %d = %s/%p\n", |
| 654 | i, f->name, f); |
| 655 | } |
| 656 | } |
| 657 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 658 | /* set_alt(), or next bind(), sets up |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 659 | * ep->driver_data as needed. |
| 660 | */ |
| 661 | usb_ep_autoconfig_reset(cdev->gadget); |
| 662 | |
| 663 | done: |
| 664 | if (status) |
| 665 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, |
| 666 | config->bConfigurationValue, status); |
| 667 | return status; |
| 668 | } |
| 669 | |
| 670 | /*-------------------------------------------------------------------------*/ |
| 671 | |
| 672 | /* We support strings in multiple languages ... string descriptor zero |
| 673 | * says which languages are supported. The typical case will be that |
| 674 | * only one language (probably English) is used, with I18N handled on |
| 675 | * the host side. |
| 676 | */ |
| 677 | |
| 678 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) |
| 679 | { |
| 680 | const struct usb_gadget_strings *s; |
| 681 | u16 language; |
| 682 | __le16 *tmp; |
| 683 | |
| 684 | while (*sp) { |
| 685 | s = *sp; |
| 686 | language = cpu_to_le16(s->language); |
| 687 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { |
| 688 | if (*tmp == language) |
| 689 | goto repeat; |
| 690 | } |
| 691 | *tmp++ = language; |
| 692 | repeat: |
| 693 | sp++; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | static int lookup_string( |
| 698 | struct usb_gadget_strings **sp, |
| 699 | void *buf, |
| 700 | u16 language, |
| 701 | int id |
| 702 | ) |
| 703 | { |
| 704 | struct usb_gadget_strings *s; |
| 705 | int value; |
| 706 | |
| 707 | while (*sp) { |
| 708 | s = *sp++; |
| 709 | if (s->language != language) |
| 710 | continue; |
| 711 | value = usb_gadget_get_string(s, id, buf); |
| 712 | if (value > 0) |
| 713 | return value; |
| 714 | } |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | static int get_string(struct usb_composite_dev *cdev, |
| 719 | void *buf, u16 language, int id) |
| 720 | { |
| 721 | struct usb_configuration *c; |
| 722 | struct usb_function *f; |
| 723 | int len; |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 724 | const char *str; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 725 | |
| 726 | /* Yes, not only is USB's I18N support probably more than most |
| 727 | * folk will ever care about ... also, it's all supported here. |
| 728 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 729 | */ |
| 730 | |
| 731 | /* 0 == report all available language codes */ |
| 732 | if (id == 0) { |
| 733 | struct usb_string_descriptor *s = buf; |
| 734 | struct usb_gadget_strings **sp; |
| 735 | |
| 736 | memset(s, 0, 256); |
| 737 | s->bDescriptorType = USB_DT_STRING; |
| 738 | |
| 739 | sp = composite->strings; |
| 740 | if (sp) |
| 741 | collect_langs(sp, s->wData); |
| 742 | |
| 743 | list_for_each_entry(c, &cdev->configs, list) { |
| 744 | sp = c->strings; |
| 745 | if (sp) |
| 746 | collect_langs(sp, s->wData); |
| 747 | |
| 748 | list_for_each_entry(f, &c->functions, list) { |
| 749 | sp = f->strings; |
| 750 | if (sp) |
| 751 | collect_langs(sp, s->wData); |
| 752 | } |
| 753 | } |
| 754 | |
Roel Kluin | 417b57b | 2009-08-06 16:09:51 -0700 | [diff] [blame] | 755 | for (len = 0; len <= 126 && s->wData[len]; len++) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 756 | continue; |
| 757 | if (!len) |
| 758 | return -EINVAL; |
| 759 | |
| 760 | s->bLength = 2 * (len + 1); |
| 761 | return s->bLength; |
| 762 | } |
| 763 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 764 | /* Otherwise, look up and return a specified string. First |
| 765 | * check if the string has not been overridden. |
| 766 | */ |
| 767 | if (cdev->manufacturer_override == id) |
| 768 | str = iManufacturer ?: composite->iManufacturer ?: |
| 769 | composite_manufacturer; |
| 770 | else if (cdev->product_override == id) |
| 771 | str = iProduct ?: composite->iProduct; |
| 772 | else if (cdev->serial_override == id) |
| 773 | str = iSerialNumber; |
| 774 | else |
| 775 | str = NULL; |
| 776 | if (str) { |
| 777 | struct usb_gadget_strings strings = { |
| 778 | .language = language, |
| 779 | .strings = &(struct usb_string) { 0xff, str } |
| 780 | }; |
| 781 | return usb_gadget_get_string(&strings, 0xff, buf); |
| 782 | } |
| 783 | |
| 784 | /* String IDs are device-scoped, so we look up each string |
| 785 | * table we're told about. These lookups are infrequent; |
| 786 | * simpler-is-better here. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 787 | */ |
| 788 | if (composite->strings) { |
| 789 | len = lookup_string(composite->strings, buf, language, id); |
| 790 | if (len > 0) |
| 791 | return len; |
| 792 | } |
| 793 | list_for_each_entry(c, &cdev->configs, list) { |
| 794 | if (c->strings) { |
| 795 | len = lookup_string(c->strings, buf, language, id); |
| 796 | if (len > 0) |
| 797 | return len; |
| 798 | } |
| 799 | list_for_each_entry(f, &c->functions, list) { |
| 800 | if (!f->strings) |
| 801 | continue; |
| 802 | len = lookup_string(f->strings, buf, language, id); |
| 803 | if (len > 0) |
| 804 | return len; |
| 805 | } |
| 806 | } |
| 807 | return -EINVAL; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * usb_string_id() - allocate an unused string ID |
| 812 | * @cdev: the device whose string descriptor IDs are being allocated |
| 813 | * Context: single threaded during gadget setup |
| 814 | * |
| 815 | * @usb_string_id() is called from bind() callbacks to allocate |
| 816 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 817 | * then store that ID in the appropriate descriptors and string table. |
| 818 | * |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 819 | * All string identifier should be allocated using this, |
| 820 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 821 | * that for example different functions don't wrongly assign different |
| 822 | * meanings to the same identifier. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 823 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 824 | int usb_string_id(struct usb_composite_dev *cdev) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 825 | { |
| 826 | if (cdev->next_string_id < 254) { |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 827 | /* string id 0 is reserved by USB spec for list of |
| 828 | * supported languages */ |
| 829 | /* 255 reserved as well? -- mina86 */ |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 830 | cdev->next_string_id++; |
| 831 | return cdev->next_string_id; |
| 832 | } |
| 833 | return -ENODEV; |
| 834 | } |
| 835 | |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 836 | /** |
| 837 | * usb_string_ids() - allocate unused string IDs in batch |
| 838 | * @cdev: the device whose string descriptor IDs are being allocated |
| 839 | * @str: an array of usb_string objects to assign numbers to |
| 840 | * Context: single threaded during gadget setup |
| 841 | * |
| 842 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 843 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 844 | * then copy IDs from the string table to the appropriate descriptors |
| 845 | * and string table for other languages. |
| 846 | * |
| 847 | * All string identifier should be allocated using this, |
| 848 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 849 | * example different functions don't wrongly assign different meanings |
| 850 | * to the same identifier. |
| 851 | */ |
| 852 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 853 | { |
| 854 | int next = cdev->next_string_id; |
| 855 | |
| 856 | for (; str->s; ++str) { |
| 857 | if (unlikely(next >= 254)) |
| 858 | return -ENODEV; |
| 859 | str->id = ++next; |
| 860 | } |
| 861 | |
| 862 | cdev->next_string_id = next; |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * usb_string_ids_n() - allocate unused string IDs in batch |
Randy Dunlap | d187abb | 2010-08-11 12:07:13 -0700 | [diff] [blame] | 869 | * @c: the device whose string descriptor IDs are being allocated |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 870 | * @n: number of string IDs to allocate |
| 871 | * Context: single threaded during gadget setup |
| 872 | * |
| 873 | * Returns the first requested ID. This ID and next @n-1 IDs are now |
Randy Dunlap | d187abb | 2010-08-11 12:07:13 -0700 | [diff] [blame] | 874 | * valid IDs. At least provided that @n is non-zero because if it |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 875 | * is, returns last requested ID which is now very useful information. |
| 876 | * |
| 877 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 878 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 879 | * then store that ID in the appropriate descriptors and string table. |
| 880 | * |
| 881 | * All string identifier should be allocated using this, |
| 882 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 883 | * example different functions don't wrongly assign different meanings |
| 884 | * to the same identifier. |
| 885 | */ |
| 886 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 887 | { |
| 888 | unsigned next = c->next_string_id; |
| 889 | if (unlikely(n > 254 || (unsigned)next + n > 254)) |
| 890 | return -ENODEV; |
| 891 | c->next_string_id += n; |
| 892 | return next + 1; |
| 893 | } |
| 894 | |
| 895 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 896 | /*-------------------------------------------------------------------------*/ |
| 897 | |
| 898 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 899 | { |
| 900 | if (req->status || req->actual != req->length) |
| 901 | DBG((struct usb_composite_dev *) ep->driver_data, |
| 902 | "setup complete --> %d, %d/%d\n", |
| 903 | req->status, req->actual, req->length); |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * The setup() callback implements all the ep0 functionality that's |
| 908 | * not handled lower down, in hardware or the hardware driver(like |
| 909 | * device and endpoint feature flags, and their status). It's all |
| 910 | * housekeeping for the gadget function we're implementing. Most of |
| 911 | * the work is in config and function specific setup. |
| 912 | */ |
| 913 | static int |
| 914 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 915 | { |
| 916 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 917 | struct usb_request *req = cdev->req; |
| 918 | int value = -EOPNOTSUPP; |
| 919 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 920 | u8 intf = w_index & 0xFF; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 921 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 922 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 923 | struct usb_function *f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 924 | u8 endp; |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 925 | unsigned long flags; |
| 926 | |
| 927 | spin_lock_irqsave(&cdev->lock, flags); |
| 928 | if (!cdev->connected) { |
| 929 | cdev->connected = 1; |
| 930 | schedule_work(&cdev->switch_work); |
| 931 | } |
| 932 | spin_unlock_irqrestore(&cdev->lock, flags); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 933 | |
| 934 | /* partial re-init of the response message; the function or the |
| 935 | * gadget might need to intercept e.g. a control-OUT completion |
| 936 | * when we delegate to it. |
| 937 | */ |
| 938 | req->zero = 0; |
| 939 | req->complete = composite_setup_complete; |
Maulik Mankad | 2edb11c | 2011-02-22 19:08:42 +0530 | [diff] [blame] | 940 | req->length = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 941 | gadget->ep0->driver_data = cdev; |
| 942 | |
| 943 | switch (ctrl->bRequest) { |
| 944 | |
| 945 | /* we handle all standard USB descriptors */ |
| 946 | case USB_REQ_GET_DESCRIPTOR: |
| 947 | if (ctrl->bRequestType != USB_DIR_IN) |
| 948 | goto unknown; |
| 949 | switch (w_value >> 8) { |
| 950 | |
| 951 | case USB_DT_DEVICE: |
| 952 | cdev->desc.bNumConfigurations = |
| 953 | count_configs(cdev, USB_DT_DEVICE); |
| 954 | value = min(w_length, (u16) sizeof cdev->desc); |
| 955 | memcpy(req->buf, &cdev->desc, value); |
| 956 | break; |
| 957 | case USB_DT_DEVICE_QUALIFIER: |
| 958 | if (!gadget_is_dualspeed(gadget)) |
| 959 | break; |
| 960 | device_qual(cdev); |
| 961 | value = min_t(int, w_length, |
| 962 | sizeof(struct usb_qualifier_descriptor)); |
| 963 | break; |
| 964 | case USB_DT_OTHER_SPEED_CONFIG: |
| 965 | if (!gadget_is_dualspeed(gadget)) |
| 966 | break; |
| 967 | /* FALLTHROUGH */ |
| 968 | case USB_DT_CONFIG: |
| 969 | value = config_desc(cdev, w_value); |
| 970 | if (value >= 0) |
| 971 | value = min(w_length, (u16) value); |
| 972 | break; |
| 973 | case USB_DT_STRING: |
| 974 | value = get_string(cdev, req->buf, |
| 975 | w_index, w_value & 0xff); |
Mike Lockwood | fb52b00 | 2010-04-16 15:32:15 -0400 | [diff] [blame] | 976 | |
| 977 | /* Allow functions to handle USB_DT_STRING. |
| 978 | * This is required for MTP. |
| 979 | */ |
| 980 | if (value < 0) { |
| 981 | struct usb_configuration *cfg; |
| 982 | list_for_each_entry(cfg, &cdev->configs, list) { |
| 983 | if (cfg && cfg->setup) { |
| 984 | value = cfg->setup(cfg, ctrl); |
| 985 | if (value >= 0) |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 991 | if (value >= 0) |
| 992 | value = min(w_length, (u16) value); |
| 993 | break; |
| 994 | } |
| 995 | break; |
| 996 | |
| 997 | /* any number of configs can work */ |
| 998 | case USB_REQ_SET_CONFIGURATION: |
| 999 | if (ctrl->bRequestType != 0) |
| 1000 | goto unknown; |
| 1001 | if (gadget_is_otg(gadget)) { |
| 1002 | if (gadget->a_hnp_support) |
| 1003 | DBG(cdev, "HNP available\n"); |
| 1004 | else if (gadget->a_alt_hnp_support) |
| 1005 | DBG(cdev, "HNP on another port\n"); |
| 1006 | else |
| 1007 | VDBG(cdev, "HNP inactive\n"); |
| 1008 | } |
| 1009 | spin_lock(&cdev->lock); |
| 1010 | value = set_config(cdev, ctrl, w_value); |
| 1011 | spin_unlock(&cdev->lock); |
| 1012 | break; |
| 1013 | case USB_REQ_GET_CONFIGURATION: |
| 1014 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1015 | goto unknown; |
Oleg Matcovschi | e67dd16 | 2011-03-11 10:24:30 -0800 | [diff] [blame^] | 1016 | if (cdev->config) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1017 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
Oleg Matcovschi | e67dd16 | 2011-03-11 10:24:30 -0800 | [diff] [blame^] | 1018 | else |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1019 | *(u8 *)req->buf = 0; |
Oleg Matcovschi | e67dd16 | 2011-03-11 10:24:30 -0800 | [diff] [blame^] | 1020 | value = min(w_length, (u16) 1); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1021 | break; |
| 1022 | |
| 1023 | /* function drivers must handle get/set altsetting; if there's |
| 1024 | * no get() method, we know only altsetting zero works. |
| 1025 | */ |
| 1026 | case USB_REQ_SET_INTERFACE: |
| 1027 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 1028 | goto unknown; |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1029 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1030 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1031 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1032 | if (!f) |
| 1033 | break; |
Bryan Wu | dd4dff8 | 2009-01-08 00:21:18 +0800 | [diff] [blame] | 1034 | if (w_value && !f->set_alt) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1035 | break; |
| 1036 | value = f->set_alt(f, w_index, w_value); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1037 | if (value == USB_GADGET_DELAYED_STATUS) { |
| 1038 | DBG(cdev, |
| 1039 | "%s: interface %d (%s) requested delayed status\n", |
| 1040 | __func__, intf, f->name); |
| 1041 | cdev->delayed_status++; |
| 1042 | DBG(cdev, "delayed_status count %d\n", |
| 1043 | cdev->delayed_status); |
| 1044 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1045 | break; |
| 1046 | case USB_REQ_GET_INTERFACE: |
| 1047 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 1048 | goto unknown; |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1049 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1050 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1051 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1052 | if (!f) |
| 1053 | break; |
| 1054 | /* lots of interfaces only need altsetting zero... */ |
| 1055 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 1056 | if (value < 0) |
| 1057 | break; |
| 1058 | *((u8 *)req->buf) = value; |
| 1059 | value = min(w_length, (u16) 1); |
| 1060 | break; |
| 1061 | default: |
| 1062 | unknown: |
| 1063 | VDBG(cdev, |
| 1064 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 1065 | ctrl->bRequestType, ctrl->bRequest, |
| 1066 | w_value, w_index, w_length); |
| 1067 | |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1068 | /* functions always handle their interfaces and endpoints... |
| 1069 | * punt other recipients (other, WUSB, ...) to the current |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1070 | * configuration code. |
| 1071 | * |
| 1072 | * REVISIT it could make sense to let the composite device |
| 1073 | * take such requests too, if that's ever needed: to work |
| 1074 | * in config 0, etc. |
| 1075 | */ |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1076 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1077 | case USB_RECIP_INTERFACE: |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1078 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
Maulik Mankad | 3c47eb0 | 2011-01-13 18:19:56 +0530 | [diff] [blame] | 1079 | break; |
| 1080 | f = cdev->config->interface[intf]; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1081 | break; |
| 1082 | |
| 1083 | case USB_RECIP_ENDPOINT: |
| 1084 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 1085 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1086 | if (test_bit(endp, f->endpoints)) |
| 1087 | break; |
| 1088 | } |
| 1089 | if (&f->list == &cdev->config->functions) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1090 | f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1091 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1092 | } |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1093 | |
| 1094 | if (f && f->setup) |
| 1095 | value = f->setup(f, ctrl); |
| 1096 | else { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1097 | struct usb_configuration *c; |
| 1098 | |
| 1099 | c = cdev->config; |
| 1100 | if (c && c->setup) |
| 1101 | value = c->setup(c, ctrl); |
| 1102 | } |
| 1103 | |
Joe Swantek | 292b1bc | 2009-12-15 07:17:40 -0500 | [diff] [blame] | 1104 | /* If the vendor request is not processed (value < 0), |
| 1105 | * call all device registered configure setup callbacks |
| 1106 | * to process it. |
| 1107 | * This is used to handle the following cases: |
| 1108 | * - vendor request is for the device and arrives before |
| 1109 | * setconfiguration. |
| 1110 | * - Some devices are required to handle vendor request before |
| 1111 | * setconfiguration such as MTP, USBNET. |
| 1112 | */ |
| 1113 | |
| 1114 | if (value < 0) { |
| 1115 | struct usb_configuration *cfg; |
| 1116 | |
| 1117 | list_for_each_entry(cfg, &cdev->configs, list) { |
| 1118 | if (cfg && cfg->setup) |
| 1119 | value = cfg->setup(cfg, ctrl); |
| 1120 | } |
| 1121 | } |
| 1122 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1123 | goto done; |
| 1124 | } |
| 1125 | |
| 1126 | /* respond with data transfer before status phase? */ |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1127 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1128 | req->length = value; |
| 1129 | req->zero = value < w_length; |
| 1130 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 1131 | if (value < 0) { |
| 1132 | DBG(cdev, "ep_queue --> %d\n", value); |
| 1133 | req->status = 0; |
| 1134 | composite_setup_complete(gadget->ep0, req); |
| 1135 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1136 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
| 1137 | WARN(cdev, |
| 1138 | "%s: Delayed status not supported for w_length != 0", |
| 1139 | __func__); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | done: |
| 1143 | /* device either stalls (value < 0) or reports success */ |
| 1144 | return value; |
| 1145 | } |
| 1146 | |
| 1147 | static void composite_disconnect(struct usb_gadget *gadget) |
| 1148 | { |
| 1149 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1150 | unsigned long flags; |
| 1151 | |
| 1152 | /* REVISIT: should we have config and device level |
| 1153 | * disconnect callbacks? |
| 1154 | */ |
| 1155 | spin_lock_irqsave(&cdev->lock, flags); |
| 1156 | if (cdev->config) |
| 1157 | reset_config(cdev); |
Mike Lockwood | 28acc1a | 2010-06-28 16:19:32 -0400 | [diff] [blame] | 1158 | |
Michal Nazarewicz | 3f3e12d | 2010-06-21 13:57:08 +0200 | [diff] [blame] | 1159 | if (composite->disconnect) |
| 1160 | composite->disconnect(cdev); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 1161 | |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 1162 | cdev->connected = 0; |
| 1163 | schedule_work(&cdev->switch_work); |
Mike Lockwood | 28acc1a | 2010-06-28 16:19:32 -0400 | [diff] [blame] | 1164 | spin_unlock_irqrestore(&cdev->lock, flags); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | /*-------------------------------------------------------------------------*/ |
| 1168 | |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1169 | static ssize_t composite_show_suspended(struct device *dev, |
| 1170 | struct device_attribute *attr, |
| 1171 | char *buf) |
| 1172 | { |
| 1173 | struct usb_gadget *gadget = dev_to_usb_gadget(dev); |
| 1174 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1175 | |
| 1176 | return sprintf(buf, "%d\n", cdev->suspended); |
| 1177 | } |
| 1178 | |
| 1179 | static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL); |
| 1180 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1181 | static void |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1182 | composite_unbind(struct usb_gadget *gadget) |
| 1183 | { |
| 1184 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1185 | |
| 1186 | /* composite_disconnect() must already have been called |
| 1187 | * by the underlying peripheral controller driver! |
| 1188 | * so there's no i/o concurrency that could affect the |
| 1189 | * state protected by cdev->lock. |
| 1190 | */ |
| 1191 | WARN_ON(cdev->config); |
| 1192 | |
| 1193 | while (!list_empty(&cdev->configs)) { |
| 1194 | struct usb_configuration *c; |
| 1195 | |
| 1196 | c = list_first_entry(&cdev->configs, |
| 1197 | struct usb_configuration, list); |
| 1198 | while (!list_empty(&c->functions)) { |
| 1199 | struct usb_function *f; |
| 1200 | |
| 1201 | f = list_first_entry(&c->functions, |
| 1202 | struct usb_function, list); |
| 1203 | list_del(&f->list); |
| 1204 | if (f->unbind) { |
| 1205 | DBG(cdev, "unbind function '%s'/%p\n", |
| 1206 | f->name, f); |
| 1207 | f->unbind(c, f); |
| 1208 | /* may free memory for "f" */ |
| 1209 | } |
| 1210 | } |
| 1211 | list_del(&c->list); |
| 1212 | if (c->unbind) { |
| 1213 | DBG(cdev, "unbind config '%s'/%p\n", c->label, c); |
| 1214 | c->unbind(c); |
| 1215 | /* may free memory for "c" */ |
| 1216 | } |
| 1217 | } |
| 1218 | if (composite->unbind) |
| 1219 | composite->unbind(cdev); |
| 1220 | |
| 1221 | if (cdev->req) { |
| 1222 | kfree(cdev->req->buf); |
| 1223 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 1224 | } |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 1225 | switch_dev_unregister(&cdev->sw_connected); |
| 1226 | switch_dev_unregister(&cdev->sw_config); |
Pavankumar Kondeti | daba580 | 2010-12-16 14:32:25 +0530 | [diff] [blame] | 1227 | device_remove_file(&gadget->dev, &dev_attr_suspended); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1228 | kfree(cdev); |
| 1229 | set_gadget_data(gadget, NULL); |
| 1230 | composite = NULL; |
| 1231 | } |
| 1232 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1233 | static u8 override_id(struct usb_composite_dev *cdev, u8 *desc) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1234 | { |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1235 | if (!*desc) { |
| 1236 | int ret = usb_string_id(cdev); |
| 1237 | if (unlikely(ret < 0)) |
| 1238 | WARNING(cdev, "failed to override string ID\n"); |
| 1239 | else |
| 1240 | *desc = ret; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1241 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1242 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1243 | return *desc; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Mike Lockwood | 8da4cc8 | 2010-06-27 20:05:55 -0400 | [diff] [blame] | 1246 | static void |
| 1247 | composite_switch_work(struct work_struct *data) |
| 1248 | { |
| 1249 | struct usb_composite_dev *cdev = |
| 1250 | container_of(data, struct usb_composite_dev, switch_work); |
| 1251 | struct usb_configuration *config = cdev->config; |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 1252 | int connected; |
| 1253 | unsigned long flags; |
| 1254 | |
| 1255 | spin_lock_irqsave(&cdev->lock, flags); |
| 1256 | if (cdev->connected != cdev->sw_connected.state) { |
| 1257 | connected = cdev->connected; |
| 1258 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1259 | switch_set_state(&cdev->sw_connected, connected); |
| 1260 | } else { |
| 1261 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1262 | } |
Mike Lockwood | 8da4cc8 | 2010-06-27 20:05:55 -0400 | [diff] [blame] | 1263 | |
| 1264 | if (config) |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 1265 | switch_set_state(&cdev->sw_config, config->bConfigurationValue); |
Mike Lockwood | 8da4cc8 | 2010-06-27 20:05:55 -0400 | [diff] [blame] | 1266 | else |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 1267 | switch_set_state(&cdev->sw_config, 0); |
Mike Lockwood | 8da4cc8 | 2010-06-27 20:05:55 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1270 | static int composite_bind(struct usb_gadget *gadget) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1271 | { |
| 1272 | struct usb_composite_dev *cdev; |
| 1273 | int status = -ENOMEM; |
| 1274 | |
| 1275 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); |
| 1276 | if (!cdev) |
| 1277 | return status; |
| 1278 | |
| 1279 | spin_lock_init(&cdev->lock); |
| 1280 | cdev->gadget = gadget; |
| 1281 | set_gadget_data(gadget, cdev); |
| 1282 | INIT_LIST_HEAD(&cdev->configs); |
| 1283 | |
| 1284 | /* preallocate control response and buffer */ |
| 1285 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 1286 | if (!cdev->req) |
| 1287 | goto fail; |
| 1288 | cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); |
| 1289 | if (!cdev->req->buf) |
| 1290 | goto fail; |
| 1291 | cdev->req->complete = composite_setup_complete; |
| 1292 | gadget->ep0->driver_data = cdev; |
| 1293 | |
| 1294 | cdev->bufsiz = USB_BUFSIZ; |
| 1295 | cdev->driver = composite; |
| 1296 | |
Parirajan Muthalagu | 37b5801 | 2010-08-25 16:33:26 +0530 | [diff] [blame] | 1297 | /* |
| 1298 | * As per USB compliance update, a device that is actively drawing |
| 1299 | * more than 100mA from USB must report itself as bus-powered in |
| 1300 | * the GetStatus(DEVICE) call. |
| 1301 | */ |
| 1302 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 1303 | usb_gadget_set_selfpowered(gadget); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1304 | |
| 1305 | /* interface and string IDs start at zero via kzalloc. |
| 1306 | * we force endpoints to start unassigned; few controller |
| 1307 | * drivers will zero ep->driver_data. |
| 1308 | */ |
| 1309 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1310 | |
| 1311 | /* composite gadget needs to assign strings for whole device (like |
| 1312 | * serial number), register function drivers, potentially update |
| 1313 | * power state and consumption, etc |
| 1314 | */ |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1315 | status = composite_gadget_bind(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1316 | if (status < 0) |
| 1317 | goto fail; |
| 1318 | |
Mike Lockwood | e6be894 | 2010-12-10 16:30:15 -0800 | [diff] [blame] | 1319 | cdev->sw_connected.name = "usb_connected"; |
| 1320 | status = switch_dev_register(&cdev->sw_connected); |
| 1321 | if (status < 0) |
| 1322 | goto fail; |
| 1323 | cdev->sw_config.name = "usb_configuration"; |
| 1324 | status = switch_dev_register(&cdev->sw_config); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 1325 | if (status < 0) |
| 1326 | goto fail; |
Mike Lockwood | 8da4cc8 | 2010-06-27 20:05:55 -0400 | [diff] [blame] | 1327 | INIT_WORK(&cdev->switch_work, composite_switch_work); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 1328 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1329 | cdev->desc = *composite->dev; |
| 1330 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 1331 | |
Greg Kroah-Hartman | dbb442b | 2010-12-16 15:52:30 -0800 | [diff] [blame] | 1332 | /* standardized runtime overrides for device ID data */ |
| 1333 | if (idVendor) |
| 1334 | cdev->desc.idVendor = cpu_to_le16(idVendor); |
| 1335 | if (idProduct) |
| 1336 | cdev->desc.idProduct = cpu_to_le16(idProduct); |
| 1337 | if (bcdDevice) |
| 1338 | cdev->desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 1339 | |
Marek Belisko | 78bff3c | 2010-10-27 10:19:01 +0200 | [diff] [blame] | 1340 | /* string overrides */ |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1341 | if (iManufacturer || !cdev->desc.iManufacturer) { |
| 1342 | if (!iManufacturer && !composite->iManufacturer && |
| 1343 | !*composite_manufacturer) |
| 1344 | snprintf(composite_manufacturer, |
| 1345 | sizeof composite_manufacturer, |
| 1346 | "%s %s with %s", |
| 1347 | init_utsname()->sysname, |
| 1348 | init_utsname()->release, |
| 1349 | gadget->name); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1350 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1351 | cdev->manufacturer_override = |
| 1352 | override_id(cdev, &cdev->desc.iManufacturer); |
| 1353 | } |
| 1354 | |
| 1355 | if (iProduct || (!cdev->desc.iProduct && composite->iProduct)) |
| 1356 | cdev->product_override = |
| 1357 | override_id(cdev, &cdev->desc.iProduct); |
| 1358 | |
| 1359 | if (iSerialNumber) |
| 1360 | cdev->serial_override = |
| 1361 | override_id(cdev, &cdev->desc.iSerialNumber); |
| 1362 | |
| 1363 | /* has userspace failed to provide a serial number? */ |
| 1364 | if (composite->needs_serial && !cdev->desc.iSerialNumber) |
| 1365 | WARNING(cdev, "userspace failed to provide iSerialNumber\n"); |
| 1366 | |
| 1367 | /* finish up */ |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1368 | status = device_create_file(&gadget->dev, &dev_attr_suspended); |
| 1369 | if (status) |
| 1370 | goto fail; |
| 1371 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1372 | INFO(cdev, "%s ready\n", composite->name); |
| 1373 | return 0; |
| 1374 | |
| 1375 | fail: |
| 1376 | composite_unbind(gadget); |
| 1377 | return status; |
| 1378 | } |
| 1379 | |
| 1380 | /*-------------------------------------------------------------------------*/ |
| 1381 | |
| 1382 | static void |
| 1383 | composite_suspend(struct usb_gadget *gadget) |
| 1384 | { |
| 1385 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1386 | struct usb_function *f; |
| 1387 | |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1388 | /* REVISIT: should we have config level |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1389 | * suspend/resume callbacks? |
| 1390 | */ |
| 1391 | DBG(cdev, "suspend\n"); |
| 1392 | if (cdev->config) { |
| 1393 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1394 | if (f->suspend) |
| 1395 | f->suspend(f); |
| 1396 | } |
| 1397 | } |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1398 | if (composite->suspend) |
| 1399 | composite->suspend(cdev); |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1400 | |
| 1401 | cdev->suspended = 1; |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 1402 | |
| 1403 | usb_gadget_vbus_draw(gadget, 2); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | static void |
| 1407 | composite_resume(struct usb_gadget *gadget) |
| 1408 | { |
| 1409 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1410 | struct usb_function *f; |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 1411 | u8 maxpower; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1412 | |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1413 | /* REVISIT: should we have config level |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1414 | * suspend/resume callbacks? |
| 1415 | */ |
| 1416 | DBG(cdev, "resume\n"); |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1417 | if (composite->resume) |
| 1418 | composite->resume(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1419 | if (cdev->config) { |
| 1420 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1421 | if (f->resume) |
| 1422 | f->resume(f); |
| 1423 | } |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 1424 | |
| 1425 | maxpower = cdev->config->bMaxPower; |
| 1426 | |
| 1427 | usb_gadget_vbus_draw(gadget, maxpower ? |
| 1428 | (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1429 | } |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1430 | |
| 1431 | cdev->suspended = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 1434 | static int |
| 1435 | composite_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 1436 | { |
| 1437 | struct usb_function *f = dev_get_drvdata(dev); |
| 1438 | |
| 1439 | if (!f) { |
| 1440 | /* this happens when the device is first created */ |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | if (add_uevent_var(env, "FUNCTION=%s", f->name)) |
| 1445 | return -ENOMEM; |
| 1446 | if (add_uevent_var(env, "ENABLED=%d", !f->disabled)) |
| 1447 | return -ENOMEM; |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1451 | /*-------------------------------------------------------------------------*/ |
| 1452 | |
| 1453 | static struct usb_gadget_driver composite_driver = { |
| 1454 | .speed = USB_SPEED_HIGH, |
| 1455 | |
Michal Nazarewicz | 915c8be | 2009-11-09 14:15:25 +0100 | [diff] [blame] | 1456 | .unbind = composite_unbind, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1457 | |
| 1458 | .setup = composite_setup, |
| 1459 | .disconnect = composite_disconnect, |
| 1460 | |
| 1461 | .suspend = composite_suspend, |
| 1462 | .resume = composite_resume, |
| 1463 | |
| 1464 | .driver = { |
| 1465 | .owner = THIS_MODULE, |
| 1466 | }, |
| 1467 | }; |
| 1468 | |
| 1469 | /** |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1470 | * usb_composite_probe() - register a composite driver |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1471 | * @driver: the driver to register |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1472 | * @bind: the callback used to allocate resources that are shared across the |
| 1473 | * whole device, such as string IDs, and add its configurations using |
| 1474 | * @usb_add_config(). This may fail by returning a negative errno |
| 1475 | * value; it should return zero on successful initialization. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1476 | * Context: single threaded during gadget setup |
| 1477 | * |
| 1478 | * This function is used to register drivers using the composite driver |
| 1479 | * framework. The return value is zero, or a negative errno value. |
| 1480 | * Those values normally come from the driver's @bind method, which does |
| 1481 | * all the work of setting up the driver to match the hardware. |
| 1482 | * |
| 1483 | * On successful return, the gadget is ready to respond to requests from |
| 1484 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 1485 | * while it was binding. That would usually be done in order to wait for |
| 1486 | * some userspace participation. |
| 1487 | */ |
Jassi Brar | 05c3eeb | 2011-02-06 18:47:18 +0900 | [diff] [blame] | 1488 | int usb_composite_probe(struct usb_composite_driver *driver, |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1489 | int (*bind)(struct usb_composite_dev *cdev)) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1490 | { |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1491 | if (!driver || !driver->dev || !bind || composite) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1492 | return -EINVAL; |
| 1493 | |
| 1494 | if (!driver->name) |
| 1495 | driver->name = "composite"; |
Jassi Brar | 05c3eeb | 2011-02-06 18:47:18 +0900 | [diff] [blame] | 1496 | if (!driver->iProduct) |
| 1497 | driver->iProduct = driver->name; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1498 | composite_driver.function = (char *) driver->name; |
| 1499 | composite_driver.driver.name = driver->name; |
| 1500 | composite = driver; |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1501 | composite_gadget_bind = bind; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1502 | |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 1503 | driver->class = class_create(THIS_MODULE, "usb_composite"); |
| 1504 | if (IS_ERR(driver->class)) |
| 1505 | return PTR_ERR(driver->class); |
Mike Lockwood | e2dc503 | 2010-06-23 08:20:59 -0400 | [diff] [blame] | 1506 | driver->class->dev_uevent = composite_uevent; |
Mike Lockwood | f041ac6 | 2010-02-06 21:53:51 -0500 | [diff] [blame] | 1507 | |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 1508 | return usb_gadget_probe_driver(&composite_driver, composite_bind); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * usb_composite_unregister() - unregister a composite driver |
| 1513 | * @driver: the driver to unregister |
| 1514 | * |
| 1515 | * This function is used to unregister drivers using the composite |
| 1516 | * driver framework. |
| 1517 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1518 | void usb_composite_unregister(struct usb_composite_driver *driver) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1519 | { |
| 1520 | if (composite != driver) |
| 1521 | return; |
| 1522 | usb_gadget_unregister_driver(&composite_driver); |
| 1523 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1524 | |
| 1525 | /** |
| 1526 | * usb_composite_setup_continue() - Continue with the control transfer |
| 1527 | * @cdev: the composite device who's control transfer was kept waiting |
| 1528 | * |
| 1529 | * This function must be called by the USB function driver to continue |
| 1530 | * with the control transfer's data/status stage in case it had requested to |
| 1531 | * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) |
| 1532 | * can request the composite framework to delay the setup request's data/status |
| 1533 | * stages by returning USB_GADGET_DELAYED_STATUS. |
| 1534 | */ |
| 1535 | void usb_composite_setup_continue(struct usb_composite_dev *cdev) |
| 1536 | { |
| 1537 | int value; |
| 1538 | struct usb_request *req = cdev->req; |
| 1539 | unsigned long flags; |
| 1540 | |
| 1541 | DBG(cdev, "%s\n", __func__); |
| 1542 | spin_lock_irqsave(&cdev->lock, flags); |
| 1543 | |
| 1544 | if (cdev->delayed_status == 0) { |
| 1545 | WARN(cdev, "%s: Unexpected call\n", __func__); |
| 1546 | |
| 1547 | } else if (--cdev->delayed_status == 0) { |
| 1548 | DBG(cdev, "%s: Completing delayed status\n", __func__); |
| 1549 | req->length = 0; |
| 1550 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 1551 | if (value < 0) { |
| 1552 | DBG(cdev, "ep_queue --> %d\n", value); |
| 1553 | req->status = 0; |
| 1554 | composite_setup_complete(cdev->gadget->ep0, req); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1559 | } |
| 1560 | |