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