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 |
| 518 | * and shall support LPM when operating in USB2.0 HS mode. |
| 519 | */ |
| 520 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 521 | bos->bNumDeviceCaps++; |
| 522 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE); |
| 523 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; |
| 524 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 525 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; |
| 526 | usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT); |
| 527 | |
| 528 | /* |
| 529 | * The Superspeed USB Capability descriptor shall be implemented by all |
| 530 | * SuperSpeed devices. |
| 531 | */ |
| 532 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 533 | bos->bNumDeviceCaps++; |
| 534 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); |
| 535 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; |
| 536 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 537 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; |
| 538 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ |
| 539 | ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | |
| 540 | USB_FULL_SPEED_OPERATION | |
| 541 | USB_HIGH_SPEED_OPERATION | |
| 542 | USB_5GBPS_OPERATION); |
| 543 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; |
| 544 | |
| 545 | /* Get Controller configuration */ |
| 546 | if (cdev->gadget->ops->get_config_params) |
| 547 | cdev->gadget->ops->get_config_params(&dcd_config_params); |
| 548 | else { |
Felipe Balbi | 089b837 | 2011-10-10 09:43:44 +0300 | [diff] [blame] | 549 | dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 550 | dcd_config_params.bU2DevExitLat = |
Felipe Balbi | 089b837 | 2011-10-10 09:43:44 +0300 | [diff] [blame] | 551 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 552 | } |
| 553 | ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; |
| 554 | ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; |
| 555 | |
| 556 | return le16_to_cpu(bos->wTotalLength); |
| 557 | } |
| 558 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 559 | static void device_qual(struct usb_composite_dev *cdev) |
| 560 | { |
| 561 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 562 | |
| 563 | qual->bLength = sizeof(*qual); |
| 564 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 565 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 566 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 567 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 568 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 569 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 570 | /* ASSUME same EP0 fifo size at both speeds */ |
Sebastian Andrzej Siewior | 765f5b8 | 2011-06-23 14:26:11 +0200 | [diff] [blame] | 571 | qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 572 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
David Lopo | c24f422 | 2008-07-01 13:14:17 -0700 | [diff] [blame] | 573 | qual->bRESERVED = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /*-------------------------------------------------------------------------*/ |
| 577 | |
| 578 | static void reset_config(struct usb_composite_dev *cdev) |
| 579 | { |
| 580 | struct usb_function *f; |
| 581 | |
| 582 | DBG(cdev, "reset config\n"); |
| 583 | |
| 584 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 585 | if (f->disable) |
| 586 | f->disable(f); |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 587 | |
| 588 | bitmap_zero(f->endpoints, 32); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 589 | } |
| 590 | cdev->config = NULL; |
| 591 | } |
| 592 | |
| 593 | static int set_config(struct usb_composite_dev *cdev, |
| 594 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 595 | { |
| 596 | struct usb_gadget *gadget = cdev->gadget; |
| 597 | struct usb_configuration *c = NULL; |
| 598 | int result = -EINVAL; |
| 599 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 600 | int tmp; |
| 601 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 602 | if (number) { |
| 603 | list_for_each_entry(c, &cdev->configs, list) { |
| 604 | if (c->bConfigurationValue == number) { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 605 | /* |
| 606 | * We disable the FDs of the previous |
| 607 | * configuration only if the new configuration |
| 608 | * is a valid one |
| 609 | */ |
| 610 | if (cdev->config) |
| 611 | reset_config(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 612 | result = 0; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | if (result < 0) |
| 617 | goto done; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 618 | } else { /* Zero configuration value - need to reset the config */ |
| 619 | if (cdev->config) |
| 620 | reset_config(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 621 | result = 0; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 622 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 623 | |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 624 | INFO(cdev, "%s config #%d: %s\n", |
| 625 | usb_speed_string(gadget->speed), |
| 626 | number, c ? c->label : "unconfigured"); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 627 | |
| 628 | if (!c) |
| 629 | goto done; |
| 630 | |
| 631 | cdev->config = c; |
| 632 | |
| 633 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 634 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 635 | struct usb_function *f = c->interface[tmp]; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 636 | struct usb_descriptor_header **descriptors; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 637 | |
| 638 | if (!f) |
| 639 | break; |
| 640 | |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Record which endpoints are used by the function. This is used |
| 643 | * to dispatch control requests targeted at that endpoint to the |
| 644 | * function's setup callback instead of the current |
| 645 | * configuration's setup callback. |
| 646 | */ |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 647 | switch (gadget->speed) { |
| 648 | case USB_SPEED_SUPER: |
| 649 | descriptors = f->ss_descriptors; |
| 650 | break; |
| 651 | case USB_SPEED_HIGH: |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 652 | descriptors = f->hs_descriptors; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 653 | break; |
| 654 | default: |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 655 | descriptors = f->descriptors; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 656 | } |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 657 | |
| 658 | for (; *descriptors; ++descriptors) { |
| 659 | struct usb_endpoint_descriptor *ep; |
| 660 | int addr; |
| 661 | |
| 662 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 663 | continue; |
| 664 | |
| 665 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 666 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 667 | | (ep->bEndpointAddress & 0x0f); |
| 668 | set_bit(addr, f->endpoints); |
| 669 | } |
| 670 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 671 | result = f->set_alt(f, tmp, 0); |
| 672 | if (result < 0) { |
| 673 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", |
| 674 | tmp, f->name, f, result); |
| 675 | |
| 676 | reset_config(cdev); |
| 677 | goto done; |
| 678 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 679 | |
| 680 | if (result == USB_GADGET_DELAYED_STATUS) { |
| 681 | DBG(cdev, |
| 682 | "%s: interface %d (%s) requested delayed status\n", |
| 683 | __func__, tmp, f->name); |
| 684 | cdev->delayed_status++; |
| 685 | DBG(cdev, "delayed_status count %d\n", |
| 686 | cdev->delayed_status); |
| 687 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* when we return, be sure our power usage is valid */ |
Ido Shayevitz | 2c2f267 | 2012-08-25 04:38:30 +0300 | [diff] [blame] | 691 | power = c->bMaxPower ? (cdev->vbus_draw_units * c->bMaxPower) : |
| 692 | CONFIG_USB_GADGET_VBUS_DRAW; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 693 | done: |
| 694 | usb_gadget_vbus_draw(gadget, power); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 695 | if (result >= 0 && cdev->delayed_status) |
| 696 | result = USB_GADGET_DELAYED_STATUS; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 697 | return result; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * usb_add_config() - add a configuration to a device. |
| 702 | * @cdev: wraps the USB gadget |
| 703 | * @config: the configuration, with bConfigurationValue assigned |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 704 | * @bind: the configuration's bind function |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 705 | * Context: single threaded during gadget setup |
| 706 | * |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 707 | * One of the main tasks of a composite @bind() routine is to |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 708 | * add each of the configurations it supports, using this routine. |
| 709 | * |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 710 | * This function returns the value of the configuration's @bind(), which |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 711 | * is zero for success else a negative errno value. Binding configurations |
| 712 | * assigns global resources including string IDs, and per-configuration |
| 713 | * resources such as interface IDs and endpoints. |
| 714 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 715 | int usb_add_config(struct usb_composite_dev *cdev, |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 716 | struct usb_configuration *config, |
| 717 | int (*bind)(struct usb_configuration *)) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 718 | { |
| 719 | int status = -EINVAL; |
| 720 | struct usb_configuration *c; |
| 721 | |
| 722 | DBG(cdev, "adding config #%u '%s'/%p\n", |
| 723 | config->bConfigurationValue, |
| 724 | config->label, config); |
| 725 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 726 | if (!config->bConfigurationValue || !bind) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 727 | goto done; |
| 728 | |
| 729 | /* Prevent duplicate configuration identifiers */ |
| 730 | list_for_each_entry(c, &cdev->configs, list) { |
| 731 | if (c->bConfigurationValue == config->bConfigurationValue) { |
| 732 | status = -EBUSY; |
| 733 | goto done; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | config->cdev = cdev; |
| 738 | list_add_tail(&config->list, &cdev->configs); |
| 739 | |
| 740 | INIT_LIST_HEAD(&config->functions); |
| 741 | config->next_interface_id = 0; |
Benoit Goby | 9c507c7 | 2011-12-13 16:01:41 -0800 | [diff] [blame] | 742 | memset(config->interface, 0, sizeof(config->interface)); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 743 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 744 | status = bind(config); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 745 | if (status < 0) { |
| 746 | list_del(&config->list); |
| 747 | config->cdev = NULL; |
| 748 | } else { |
| 749 | unsigned i; |
| 750 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 751 | DBG(cdev, "cfg %d/%p speeds:%s%s%s\n", |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 752 | config->bConfigurationValue, config, |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 753 | config->superspeed ? " super" : "", |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 754 | config->highspeed ? " high" : "", |
| 755 | config->fullspeed |
| 756 | ? (gadget_is_dualspeed(cdev->gadget) |
| 757 | ? " full" |
| 758 | : " full/low") |
| 759 | : ""); |
| 760 | |
| 761 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 762 | struct usb_function *f = config->interface[i]; |
| 763 | |
| 764 | if (!f) |
| 765 | continue; |
| 766 | DBG(cdev, " interface %d = %s/%p\n", |
| 767 | i, f->name, f); |
| 768 | } |
| 769 | } |
| 770 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 771 | /* set_alt(), or next bind(), sets up |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 772 | * ep->driver_data as needed. |
| 773 | */ |
| 774 | usb_ep_autoconfig_reset(cdev->gadget); |
| 775 | |
| 776 | done: |
| 777 | if (status) |
| 778 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, |
| 779 | config->bConfigurationValue, status); |
| 780 | return status; |
| 781 | } |
| 782 | |
Benoit Goby | be69452 | 2012-05-15 20:44:33 -0700 | [diff] [blame] | 783 | static int unbind_config(struct usb_composite_dev *cdev, |
Benoit Goby | 894eecb | 2011-12-13 16:16:31 -0800 | [diff] [blame] | 784 | struct usb_configuration *config) |
| 785 | { |
| 786 | while (!list_empty(&config->functions)) { |
| 787 | struct usb_function *f; |
| 788 | |
| 789 | f = list_first_entry(&config->functions, |
| 790 | struct usb_function, list); |
| 791 | list_del(&f->list); |
| 792 | if (f->unbind) { |
| 793 | DBG(cdev, "unbind function '%s'/%p\n", f->name, f); |
| 794 | f->unbind(config, f); |
| 795 | /* may free memory for "f" */ |
| 796 | } |
| 797 | } |
Benoit Goby | 894eecb | 2011-12-13 16:16:31 -0800 | [diff] [blame] | 798 | if (config->unbind) { |
| 799 | DBG(cdev, "unbind config '%s'/%p\n", config->label, config); |
| 800 | config->unbind(config); |
| 801 | /* may free memory for "c" */ |
| 802 | } |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * usb_remove_config() - remove a configuration from a device. |
| 808 | * @cdev: wraps the USB gadget |
| 809 | * @config: the configuration |
| 810 | * |
| 811 | * Drivers must call usb_gadget_disconnect before calling this function |
| 812 | * to disconnect the device from the host and make sure the host will not |
| 813 | * try to enumerate the device while we are changing the config list. |
| 814 | */ |
| 815 | int usb_remove_config(struct usb_composite_dev *cdev, |
| 816 | struct usb_configuration *config) |
| 817 | { |
| 818 | unsigned long flags; |
| 819 | |
| 820 | spin_lock_irqsave(&cdev->lock, flags); |
| 821 | |
| 822 | if (cdev->config == config) |
| 823 | reset_config(cdev); |
| 824 | |
Benoit Goby | be69452 | 2012-05-15 20:44:33 -0700 | [diff] [blame] | 825 | list_del(&config->list); |
| 826 | |
Benoit Goby | 894eecb | 2011-12-13 16:16:31 -0800 | [diff] [blame] | 827 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 828 | |
Benoit Goby | be69452 | 2012-05-15 20:44:33 -0700 | [diff] [blame] | 829 | return unbind_config(cdev, config); |
Benoit Goby | 894eecb | 2011-12-13 16:16:31 -0800 | [diff] [blame] | 830 | } |
| 831 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 832 | /*-------------------------------------------------------------------------*/ |
| 833 | |
| 834 | /* We support strings in multiple languages ... string descriptor zero |
| 835 | * says which languages are supported. The typical case will be that |
| 836 | * only one language (probably English) is used, with I18N handled on |
| 837 | * the host side. |
| 838 | */ |
| 839 | |
| 840 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) |
| 841 | { |
| 842 | const struct usb_gadget_strings *s; |
| 843 | u16 language; |
| 844 | __le16 *tmp; |
| 845 | |
| 846 | while (*sp) { |
| 847 | s = *sp; |
| 848 | language = cpu_to_le16(s->language); |
| 849 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { |
| 850 | if (*tmp == language) |
| 851 | goto repeat; |
| 852 | } |
| 853 | *tmp++ = language; |
| 854 | repeat: |
| 855 | sp++; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | static int lookup_string( |
| 860 | struct usb_gadget_strings **sp, |
| 861 | void *buf, |
| 862 | u16 language, |
| 863 | int id |
| 864 | ) |
| 865 | { |
| 866 | struct usb_gadget_strings *s; |
| 867 | int value; |
| 868 | |
| 869 | while (*sp) { |
| 870 | s = *sp++; |
| 871 | if (s->language != language) |
| 872 | continue; |
| 873 | value = usb_gadget_get_string(s, id, buf); |
| 874 | if (value > 0) |
| 875 | return value; |
| 876 | } |
| 877 | return -EINVAL; |
| 878 | } |
| 879 | |
| 880 | static int get_string(struct usb_composite_dev *cdev, |
| 881 | void *buf, u16 language, int id) |
| 882 | { |
| 883 | struct usb_configuration *c; |
| 884 | struct usb_function *f; |
| 885 | int len; |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 886 | const char *str; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 887 | |
| 888 | /* Yes, not only is USB's I18N support probably more than most |
| 889 | * folk will ever care about ... also, it's all supported here. |
| 890 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 891 | */ |
| 892 | |
| 893 | /* 0 == report all available language codes */ |
| 894 | if (id == 0) { |
| 895 | struct usb_string_descriptor *s = buf; |
| 896 | struct usb_gadget_strings **sp; |
| 897 | |
| 898 | memset(s, 0, 256); |
| 899 | s->bDescriptorType = USB_DT_STRING; |
| 900 | |
| 901 | sp = composite->strings; |
| 902 | if (sp) |
| 903 | collect_langs(sp, s->wData); |
| 904 | |
| 905 | list_for_each_entry(c, &cdev->configs, list) { |
| 906 | sp = c->strings; |
| 907 | if (sp) |
| 908 | collect_langs(sp, s->wData); |
| 909 | |
| 910 | list_for_each_entry(f, &c->functions, list) { |
| 911 | sp = f->strings; |
| 912 | if (sp) |
| 913 | collect_langs(sp, s->wData); |
| 914 | } |
| 915 | } |
| 916 | |
Roel Kluin | 417b57b | 2009-08-06 16:09:51 -0700 | [diff] [blame] | 917 | for (len = 0; len <= 126 && s->wData[len]; len++) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 918 | continue; |
| 919 | if (!len) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | s->bLength = 2 * (len + 1); |
| 923 | return s->bLength; |
| 924 | } |
| 925 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 926 | /* Otherwise, look up and return a specified string. First |
| 927 | * check if the string has not been overridden. |
| 928 | */ |
| 929 | if (cdev->manufacturer_override == id) |
| 930 | str = iManufacturer ?: composite->iManufacturer ?: |
| 931 | composite_manufacturer; |
| 932 | else if (cdev->product_override == id) |
| 933 | str = iProduct ?: composite->iProduct; |
| 934 | else if (cdev->serial_override == id) |
| 935 | str = iSerialNumber; |
| 936 | else |
| 937 | str = NULL; |
| 938 | if (str) { |
| 939 | struct usb_gadget_strings strings = { |
| 940 | .language = language, |
| 941 | .strings = &(struct usb_string) { 0xff, str } |
| 942 | }; |
| 943 | return usb_gadget_get_string(&strings, 0xff, buf); |
| 944 | } |
| 945 | |
| 946 | /* String IDs are device-scoped, so we look up each string |
| 947 | * table we're told about. These lookups are infrequent; |
| 948 | * simpler-is-better here. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 949 | */ |
| 950 | if (composite->strings) { |
| 951 | len = lookup_string(composite->strings, buf, language, id); |
| 952 | if (len > 0) |
| 953 | return len; |
| 954 | } |
| 955 | list_for_each_entry(c, &cdev->configs, list) { |
| 956 | if (c->strings) { |
| 957 | len = lookup_string(c->strings, buf, language, id); |
| 958 | if (len > 0) |
| 959 | return len; |
| 960 | } |
| 961 | list_for_each_entry(f, &c->functions, list) { |
| 962 | if (!f->strings) |
| 963 | continue; |
| 964 | len = lookup_string(f->strings, buf, language, id); |
| 965 | if (len > 0) |
| 966 | return len; |
| 967 | } |
| 968 | } |
| 969 | return -EINVAL; |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * usb_string_id() - allocate an unused string ID |
| 974 | * @cdev: the device whose string descriptor IDs are being allocated |
| 975 | * Context: single threaded during gadget setup |
| 976 | * |
| 977 | * @usb_string_id() is called from bind() callbacks to allocate |
| 978 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 979 | * then store that ID in the appropriate descriptors and string table. |
| 980 | * |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 981 | * All string identifier should be allocated using this, |
| 982 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 983 | * that for example different functions don't wrongly assign different |
| 984 | * meanings to the same identifier. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 985 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 986 | int usb_string_id(struct usb_composite_dev *cdev) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 987 | { |
| 988 | if (cdev->next_string_id < 254) { |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 989 | /* string id 0 is reserved by USB spec for list of |
| 990 | * supported languages */ |
| 991 | /* 255 reserved as well? -- mina86 */ |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 992 | cdev->next_string_id++; |
| 993 | return cdev->next_string_id; |
| 994 | } |
| 995 | return -ENODEV; |
| 996 | } |
| 997 | |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 998 | /** |
| 999 | * usb_string_ids() - allocate unused string IDs in batch |
| 1000 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1001 | * @str: an array of usb_string objects to assign numbers to |
| 1002 | * Context: single threaded during gadget setup |
| 1003 | * |
| 1004 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 1005 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1006 | * then copy IDs from the string table to the appropriate descriptors |
| 1007 | * and string table for other languages. |
| 1008 | * |
| 1009 | * All string identifier should be allocated using this, |
| 1010 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 1011 | * example different functions don't wrongly assign different meanings |
| 1012 | * to the same identifier. |
| 1013 | */ |
| 1014 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 1015 | { |
| 1016 | int next = cdev->next_string_id; |
| 1017 | |
| 1018 | for (; str->s; ++str) { |
| 1019 | if (unlikely(next >= 254)) |
| 1020 | return -ENODEV; |
| 1021 | str->id = ++next; |
| 1022 | } |
| 1023 | |
| 1024 | cdev->next_string_id = next; |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * usb_string_ids_n() - allocate unused string IDs in batch |
Randy Dunlap | d187abb | 2010-08-11 12:07:13 -0700 | [diff] [blame] | 1031 | * @c: the device whose string descriptor IDs are being allocated |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1032 | * @n: number of string IDs to allocate |
| 1033 | * Context: single threaded during gadget setup |
| 1034 | * |
| 1035 | * 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] | 1036 | * 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] | 1037 | * is, returns last requested ID which is now very useful information. |
| 1038 | * |
| 1039 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 1040 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1041 | * then store that ID in the appropriate descriptors and string table. |
| 1042 | * |
| 1043 | * All string identifier should be allocated using this, |
| 1044 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 1045 | * example different functions don't wrongly assign different meanings |
| 1046 | * to the same identifier. |
| 1047 | */ |
| 1048 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 1049 | { |
| 1050 | unsigned next = c->next_string_id; |
| 1051 | if (unlikely(n > 254 || (unsigned)next + n > 254)) |
| 1052 | return -ENODEV; |
| 1053 | c->next_string_id += n; |
| 1054 | return next + 1; |
| 1055 | } |
| 1056 | |
| 1057 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1058 | /*-------------------------------------------------------------------------*/ |
| 1059 | |
| 1060 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 1061 | { |
| 1062 | if (req->status || req->actual != req->length) |
| 1063 | DBG((struct usb_composite_dev *) ep->driver_data, |
| 1064 | "setup complete --> %d, %d/%d\n", |
| 1065 | req->status, req->actual, req->length); |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * The setup() callback implements all the ep0 functionality that's |
| 1070 | * not handled lower down, in hardware or the hardware driver(like |
| 1071 | * device and endpoint feature flags, and their status). It's all |
| 1072 | * housekeeping for the gadget function we're implementing. Most of |
| 1073 | * the work is in config and function specific setup. |
| 1074 | */ |
| 1075 | static int |
| 1076 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 1077 | { |
| 1078 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1079 | struct usb_request *req = cdev->req; |
| 1080 | int value = -EOPNOTSUPP; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1081 | int status = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1082 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1083 | u8 intf = w_index & 0xFF; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1084 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 1085 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 1086 | struct usb_function *f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1087 | u8 endp; |
Vijayavardhan Vennapusa | 95b650a | 2012-01-18 12:54:01 +0530 | [diff] [blame] | 1088 | struct usb_configuration *c; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1089 | |
Manu Gautam | 8d887a1 | 2011-10-12 17:13:52 +0530 | [diff] [blame] | 1090 | |
| 1091 | if (w_length > USB_BUFSIZ) |
| 1092 | return value; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1093 | |
| 1094 | /* partial re-init of the response message; the function or the |
| 1095 | * gadget might need to intercept e.g. a control-OUT completion |
| 1096 | * when we delegate to it. |
| 1097 | */ |
| 1098 | req->zero = 0; |
| 1099 | req->complete = composite_setup_complete; |
Maulik Mankad | 2edb11c | 2011-02-22 19:08:42 +0530 | [diff] [blame] | 1100 | req->length = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1101 | gadget->ep0->driver_data = cdev; |
| 1102 | |
| 1103 | switch (ctrl->bRequest) { |
| 1104 | |
| 1105 | /* we handle all standard USB descriptors */ |
| 1106 | case USB_REQ_GET_DESCRIPTOR: |
| 1107 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1108 | goto unknown; |
| 1109 | switch (w_value >> 8) { |
| 1110 | |
| 1111 | case USB_DT_DEVICE: |
| 1112 | cdev->desc.bNumConfigurations = |
| 1113 | count_configs(cdev, USB_DT_DEVICE); |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1114 | cdev->desc.bMaxPacketSize0 = |
| 1115 | cdev->gadget->ep0->maxpacket; |
Ido Shayevitz | 2c2f267 | 2012-08-25 04:38:30 +0300 | [diff] [blame] | 1116 | cdev->vbus_draw_units = 2; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1117 | if (gadget_is_superspeed(gadget)) { |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1118 | if (gadget->speed >= USB_SPEED_SUPER) { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1119 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1120 | cdev->desc.bMaxPacketSize0 = 9; |
Ido Shayevitz | 2c2f267 | 2012-08-25 04:38:30 +0300 | [diff] [blame] | 1121 | cdev->vbus_draw_units = 8; |
| 1122 | DBG(cdev, "Config SS device in SS\n"); |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1123 | } else { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1124 | cdev->desc.bcdUSB = cpu_to_le16(0x0210); |
Ido Shayevitz | 2c2f267 | 2012-08-25 04:38:30 +0300 | [diff] [blame] | 1125 | DBG(cdev, "Config SS device in HS\n"); |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1126 | } |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1127 | } |
| 1128 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1129 | value = min(w_length, (u16) sizeof cdev->desc); |
| 1130 | memcpy(req->buf, &cdev->desc, value); |
| 1131 | break; |
| 1132 | case USB_DT_DEVICE_QUALIFIER: |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1133 | if (!gadget_is_dualspeed(gadget) || |
| 1134 | gadget->speed >= USB_SPEED_SUPER) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1135 | break; |
| 1136 | device_qual(cdev); |
| 1137 | value = min_t(int, w_length, |
| 1138 | sizeof(struct usb_qualifier_descriptor)); |
| 1139 | break; |
| 1140 | case USB_DT_OTHER_SPEED_CONFIG: |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1141 | if (!gadget_is_dualspeed(gadget) || |
| 1142 | gadget->speed >= USB_SPEED_SUPER) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1143 | break; |
| 1144 | /* FALLTHROUGH */ |
| 1145 | case USB_DT_CONFIG: |
| 1146 | value = config_desc(cdev, w_value); |
| 1147 | if (value >= 0) |
| 1148 | value = min(w_length, (u16) value); |
| 1149 | break; |
Vijayavardhan Vennapusa | 95b650a | 2012-01-18 12:54:01 +0530 | [diff] [blame] | 1150 | case USB_DT_OTG: |
| 1151 | if (!gadget_is_otg(gadget)) |
| 1152 | break; |
| 1153 | c = list_first_entry(&cdev->configs, |
| 1154 | struct usb_configuration, list); |
| 1155 | if (c && c->descriptors) |
| 1156 | value = usb_find_descriptor_fillbuf(req->buf, |
| 1157 | USB_BUFSIZ, c->descriptors, |
| 1158 | USB_DT_OTG); |
| 1159 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1160 | case USB_DT_STRING: |
| 1161 | value = get_string(cdev, req->buf, |
| 1162 | w_index, w_value & 0xff); |
| 1163 | if (value >= 0) |
| 1164 | value = min(w_length, (u16) value); |
| 1165 | break; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1166 | case USB_DT_BOS: |
| 1167 | if (gadget_is_superspeed(gadget)) { |
| 1168 | value = bos_desc(cdev); |
| 1169 | value = min(w_length, (u16) value); |
| 1170 | } |
| 1171 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1172 | } |
| 1173 | break; |
| 1174 | |
| 1175 | /* any number of configs can work */ |
| 1176 | case USB_REQ_SET_CONFIGURATION: |
| 1177 | if (ctrl->bRequestType != 0) |
| 1178 | goto unknown; |
| 1179 | if (gadget_is_otg(gadget)) { |
| 1180 | if (gadget->a_hnp_support) |
| 1181 | DBG(cdev, "HNP available\n"); |
| 1182 | else if (gadget->a_alt_hnp_support) |
| 1183 | DBG(cdev, "HNP on another port\n"); |
| 1184 | else |
| 1185 | VDBG(cdev, "HNP inactive\n"); |
| 1186 | } |
| 1187 | spin_lock(&cdev->lock); |
| 1188 | value = set_config(cdev, ctrl, w_value); |
| 1189 | spin_unlock(&cdev->lock); |
| 1190 | break; |
| 1191 | case USB_REQ_GET_CONFIGURATION: |
| 1192 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1193 | goto unknown; |
| 1194 | if (cdev->config) |
| 1195 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 1196 | else |
| 1197 | *(u8 *)req->buf = 0; |
| 1198 | value = min(w_length, (u16) 1); |
| 1199 | break; |
| 1200 | |
| 1201 | /* function drivers must handle get/set altsetting; if there's |
| 1202 | * no get() method, we know only altsetting zero works. |
| 1203 | */ |
| 1204 | case USB_REQ_SET_INTERFACE: |
| 1205 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 1206 | goto unknown; |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1207 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1208 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1209 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1210 | if (!f) |
| 1211 | break; |
Bryan Wu | dd4dff8 | 2009-01-08 00:21:18 +0800 | [diff] [blame] | 1212 | if (w_value && !f->set_alt) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1213 | break; |
| 1214 | value = f->set_alt(f, w_index, w_value); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1215 | if (value == USB_GADGET_DELAYED_STATUS) { |
| 1216 | DBG(cdev, |
| 1217 | "%s: interface %d (%s) requested delayed status\n", |
| 1218 | __func__, intf, f->name); |
| 1219 | cdev->delayed_status++; |
| 1220 | DBG(cdev, "delayed_status count %d\n", |
| 1221 | cdev->delayed_status); |
| 1222 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1223 | break; |
| 1224 | case USB_REQ_GET_INTERFACE: |
| 1225 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 1226 | goto unknown; |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1227 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1228 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1229 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1230 | if (!f) |
| 1231 | break; |
| 1232 | /* lots of interfaces only need altsetting zero... */ |
| 1233 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 1234 | if (value < 0) |
| 1235 | break; |
| 1236 | *((u8 *)req->buf) = value; |
| 1237 | value = min(w_length, (u16) 1); |
| 1238 | break; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | * USB 3.0 additions: |
| 1242 | * Function driver should handle get_status request. If such cb |
| 1243 | * wasn't supplied we respond with default value = 0 |
| 1244 | * Note: function driver should supply such cb only for the first |
| 1245 | * interface of the function |
| 1246 | */ |
| 1247 | case USB_REQ_GET_STATUS: |
| 1248 | if (!gadget_is_superspeed(gadget)) |
| 1249 | goto unknown; |
| 1250 | if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) |
| 1251 | goto unknown; |
| 1252 | value = 2; /* This is the length of the get_status reply */ |
| 1253 | put_unaligned_le16(0, req->buf); |
| 1254 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 1255 | break; |
| 1256 | f = cdev->config->interface[intf]; |
| 1257 | if (!f) |
| 1258 | break; |
| 1259 | status = f->get_status ? f->get_status(f) : 0; |
| 1260 | if (status < 0) |
| 1261 | break; |
| 1262 | put_unaligned_le16(status & 0x0000ffff, req->buf); |
| 1263 | break; |
| 1264 | /* |
| 1265 | * Function drivers should handle SetFeature/ClearFeature |
| 1266 | * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied |
| 1267 | * only for the first interface of the function |
| 1268 | */ |
| 1269 | case USB_REQ_CLEAR_FEATURE: |
| 1270 | case USB_REQ_SET_FEATURE: |
| 1271 | if (!gadget_is_superspeed(gadget)) |
| 1272 | goto unknown; |
| 1273 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) |
| 1274 | goto unknown; |
| 1275 | switch (w_value) { |
| 1276 | case USB_INTRF_FUNC_SUSPEND: |
| 1277 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 1278 | break; |
| 1279 | f = cdev->config->interface[intf]; |
| 1280 | if (!f) |
| 1281 | break; |
| 1282 | value = 0; |
| 1283 | if (f->func_suspend) |
| 1284 | value = f->func_suspend(f, w_index >> 8); |
| 1285 | if (value < 0) { |
| 1286 | ERROR(cdev, |
| 1287 | "func_suspend() returned error %d\n", |
| 1288 | value); |
| 1289 | value = 0; |
| 1290 | } |
| 1291 | break; |
| 1292 | } |
| 1293 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1294 | default: |
| 1295 | unknown: |
| 1296 | VDBG(cdev, |
| 1297 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 1298 | ctrl->bRequestType, ctrl->bRequest, |
| 1299 | w_value, w_index, w_length); |
| 1300 | |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1301 | /* functions always handle their interfaces and endpoints... |
| 1302 | * punt other recipients (other, WUSB, ...) to the current |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1303 | * configuration code. |
| 1304 | * |
| 1305 | * REVISIT it could make sense to let the composite device |
| 1306 | * take such requests too, if that's ever needed: to work |
| 1307 | * in config 0, etc. |
| 1308 | */ |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1309 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1310 | case USB_RECIP_INTERFACE: |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1311 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
Maulik Mankad | 3c47eb0 | 2011-01-13 18:19:56 +0530 | [diff] [blame] | 1312 | break; |
| 1313 | f = cdev->config->interface[intf]; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1314 | break; |
| 1315 | |
| 1316 | case USB_RECIP_ENDPOINT: |
| 1317 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 1318 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1319 | if (test_bit(endp, f->endpoints)) |
| 1320 | break; |
| 1321 | } |
| 1322 | if (&f->list == &cdev->config->functions) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1323 | f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1324 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1325 | } |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1326 | |
| 1327 | if (f && f->setup) |
| 1328 | value = f->setup(f, ctrl); |
| 1329 | else { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1330 | struct usb_configuration *c; |
| 1331 | |
| 1332 | c = cdev->config; |
| 1333 | if (c && c->setup) |
| 1334 | value = c->setup(c, ctrl); |
| 1335 | } |
| 1336 | |
| 1337 | goto done; |
| 1338 | } |
| 1339 | |
| 1340 | /* respond with data transfer before status phase? */ |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1341 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1342 | req->length = value; |
| 1343 | req->zero = value < w_length; |
| 1344 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 1345 | if (value < 0) { |
| 1346 | DBG(cdev, "ep_queue --> %d\n", value); |
| 1347 | req->status = 0; |
| 1348 | composite_setup_complete(gadget->ep0, req); |
| 1349 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1350 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
| 1351 | WARN(cdev, |
| 1352 | "%s: Delayed status not supported for w_length != 0", |
| 1353 | __func__); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | done: |
| 1357 | /* device either stalls (value < 0) or reports success */ |
| 1358 | return value; |
| 1359 | } |
| 1360 | |
| 1361 | static void composite_disconnect(struct usb_gadget *gadget) |
| 1362 | { |
| 1363 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1364 | unsigned long flags; |
| 1365 | |
| 1366 | /* REVISIT: should we have config and device level |
| 1367 | * disconnect callbacks? |
| 1368 | */ |
| 1369 | spin_lock_irqsave(&cdev->lock, flags); |
| 1370 | if (cdev->config) |
| 1371 | reset_config(cdev); |
Michal Nazarewicz | 3f3e12d | 2010-06-21 13:57:08 +0200 | [diff] [blame] | 1372 | if (composite->disconnect) |
| 1373 | composite->disconnect(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1374 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1375 | } |
| 1376 | |
| 1377 | /*-------------------------------------------------------------------------*/ |
| 1378 | |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1379 | static ssize_t composite_show_suspended(struct device *dev, |
| 1380 | struct device_attribute *attr, |
| 1381 | char *buf) |
| 1382 | { |
| 1383 | struct usb_gadget *gadget = dev_to_usb_gadget(dev); |
| 1384 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1385 | |
| 1386 | return sprintf(buf, "%d\n", cdev->suspended); |
| 1387 | } |
| 1388 | |
| 1389 | static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL); |
| 1390 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1391 | static void |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1392 | composite_unbind(struct usb_gadget *gadget) |
| 1393 | { |
| 1394 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1395 | |
| 1396 | /* composite_disconnect() must already have been called |
| 1397 | * by the underlying peripheral controller driver! |
| 1398 | * so there's no i/o concurrency that could affect the |
| 1399 | * state protected by cdev->lock. |
| 1400 | */ |
| 1401 | WARN_ON(cdev->config); |
| 1402 | |
| 1403 | while (!list_empty(&cdev->configs)) { |
| 1404 | struct usb_configuration *c; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1405 | c = list_first_entry(&cdev->configs, |
| 1406 | struct usb_configuration, list); |
Benoit Goby | be69452 | 2012-05-15 20:44:33 -0700 | [diff] [blame] | 1407 | list_del(&c->list); |
| 1408 | unbind_config(cdev, c); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1409 | } |
| 1410 | if (composite->unbind) |
| 1411 | composite->unbind(cdev); |
| 1412 | |
| 1413 | if (cdev->req) { |
| 1414 | kfree(cdev->req->buf); |
| 1415 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 1416 | } |
Pavankumar Kondeti | daba580 | 2010-12-16 14:32:25 +0530 | [diff] [blame] | 1417 | device_remove_file(&gadget->dev, &dev_attr_suspended); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1418 | kfree(cdev); |
| 1419 | set_gadget_data(gadget, NULL); |
| 1420 | composite = NULL; |
| 1421 | } |
| 1422 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1423 | static u8 override_id(struct usb_composite_dev *cdev, u8 *desc) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1424 | { |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1425 | if (!*desc) { |
| 1426 | int ret = usb_string_id(cdev); |
| 1427 | if (unlikely(ret < 0)) |
| 1428 | WARNING(cdev, "failed to override string ID\n"); |
| 1429 | else |
| 1430 | *desc = ret; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1431 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1432 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1433 | return *desc; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1436 | static int composite_bind(struct usb_gadget *gadget) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1437 | { |
| 1438 | struct usb_composite_dev *cdev; |
| 1439 | int status = -ENOMEM; |
| 1440 | |
| 1441 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); |
| 1442 | if (!cdev) |
| 1443 | return status; |
| 1444 | |
| 1445 | spin_lock_init(&cdev->lock); |
| 1446 | cdev->gadget = gadget; |
| 1447 | set_gadget_data(gadget, cdev); |
| 1448 | INIT_LIST_HEAD(&cdev->configs); |
| 1449 | |
| 1450 | /* preallocate control response and buffer */ |
| 1451 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 1452 | if (!cdev->req) |
| 1453 | goto fail; |
| 1454 | cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); |
| 1455 | if (!cdev->req->buf) |
| 1456 | goto fail; |
| 1457 | cdev->req->complete = composite_setup_complete; |
| 1458 | gadget->ep0->driver_data = cdev; |
| 1459 | |
| 1460 | cdev->bufsiz = USB_BUFSIZ; |
| 1461 | cdev->driver = composite; |
| 1462 | |
Parirajan Muthalagu | 37b5801 | 2010-08-25 16:33:26 +0530 | [diff] [blame] | 1463 | /* |
| 1464 | * As per USB compliance update, a device that is actively drawing |
| 1465 | * more than 100mA from USB must report itself as bus-powered in |
| 1466 | * the GetStatus(DEVICE) call. |
| 1467 | */ |
| 1468 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 1469 | usb_gadget_set_selfpowered(gadget); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* interface and string IDs start at zero via kzalloc. |
| 1472 | * we force endpoints to start unassigned; few controller |
| 1473 | * drivers will zero ep->driver_data. |
| 1474 | */ |
| 1475 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1476 | |
| 1477 | /* composite gadget needs to assign strings for whole device (like |
| 1478 | * serial number), register function drivers, potentially update |
| 1479 | * power state and consumption, etc |
| 1480 | */ |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1481 | status = composite_gadget_bind(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1482 | if (status < 0) |
| 1483 | goto fail; |
| 1484 | |
| 1485 | cdev->desc = *composite->dev; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1486 | |
Greg Kroah-Hartman | dbb442b | 2010-12-16 15:52:30 -0800 | [diff] [blame] | 1487 | /* standardized runtime overrides for device ID data */ |
| 1488 | if (idVendor) |
| 1489 | cdev->desc.idVendor = cpu_to_le16(idVendor); |
| 1490 | if (idProduct) |
| 1491 | cdev->desc.idProduct = cpu_to_le16(idProduct); |
| 1492 | if (bcdDevice) |
| 1493 | cdev->desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 1494 | |
Marek Belisko | 78bff3c | 2010-10-27 10:19:01 +0200 | [diff] [blame] | 1495 | /* string overrides */ |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1496 | if (iManufacturer || !cdev->desc.iManufacturer) { |
| 1497 | if (!iManufacturer && !composite->iManufacturer && |
| 1498 | !*composite_manufacturer) |
| 1499 | snprintf(composite_manufacturer, |
| 1500 | sizeof composite_manufacturer, |
| 1501 | "%s %s with %s", |
| 1502 | init_utsname()->sysname, |
| 1503 | init_utsname()->release, |
| 1504 | gadget->name); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1505 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1506 | cdev->manufacturer_override = |
| 1507 | override_id(cdev, &cdev->desc.iManufacturer); |
| 1508 | } |
| 1509 | |
| 1510 | if (iProduct || (!cdev->desc.iProduct && composite->iProduct)) |
| 1511 | cdev->product_override = |
| 1512 | override_id(cdev, &cdev->desc.iProduct); |
| 1513 | |
| 1514 | if (iSerialNumber) |
| 1515 | cdev->serial_override = |
| 1516 | override_id(cdev, &cdev->desc.iSerialNumber); |
| 1517 | |
| 1518 | /* has userspace failed to provide a serial number? */ |
| 1519 | if (composite->needs_serial && !cdev->desc.iSerialNumber) |
| 1520 | WARNING(cdev, "userspace failed to provide iSerialNumber\n"); |
| 1521 | |
| 1522 | /* finish up */ |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1523 | status = device_create_file(&gadget->dev, &dev_attr_suspended); |
| 1524 | if (status) |
| 1525 | goto fail; |
| 1526 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1527 | INFO(cdev, "%s ready\n", composite->name); |
| 1528 | return 0; |
| 1529 | |
| 1530 | fail: |
| 1531 | composite_unbind(gadget); |
| 1532 | return status; |
| 1533 | } |
| 1534 | |
| 1535 | /*-------------------------------------------------------------------------*/ |
| 1536 | |
| 1537 | static void |
| 1538 | composite_suspend(struct usb_gadget *gadget) |
| 1539 | { |
| 1540 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1541 | struct usb_function *f; |
| 1542 | |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1543 | /* REVISIT: should we have config level |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1544 | * suspend/resume callbacks? |
| 1545 | */ |
| 1546 | DBG(cdev, "suspend\n"); |
| 1547 | if (cdev->config) { |
| 1548 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1549 | if (f->suspend) |
| 1550 | f->suspend(f); |
| 1551 | } |
| 1552 | } |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1553 | if (composite->suspend) |
| 1554 | composite->suspend(cdev); |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1555 | |
| 1556 | cdev->suspended = 1; |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 1557 | |
| 1558 | usb_gadget_vbus_draw(gadget, 2); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | static void |
| 1562 | composite_resume(struct usb_gadget *gadget) |
| 1563 | { |
| 1564 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1565 | struct usb_function *f; |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 1566 | u8 maxpower; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1567 | |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1568 | /* REVISIT: should we have config level |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1569 | * suspend/resume callbacks? |
| 1570 | */ |
| 1571 | DBG(cdev, "resume\n"); |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 1572 | if (composite->resume) |
| 1573 | composite->resume(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1574 | if (cdev->config) { |
| 1575 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1576 | if (f->resume) |
| 1577 | f->resume(f); |
| 1578 | } |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 1579 | |
| 1580 | maxpower = cdev->config->bMaxPower; |
| 1581 | |
| 1582 | usb_gadget_vbus_draw(gadget, maxpower ? |
Ido Shayevitz | 2c2f267 | 2012-08-25 04:38:30 +0300 | [diff] [blame] | 1583 | (cdev->vbus_draw_units * maxpower) : |
| 1584 | CONFIG_USB_GADGET_VBUS_DRAW); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1585 | } |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1586 | |
| 1587 | cdev->suspended = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /*-------------------------------------------------------------------------*/ |
| 1591 | |
| 1592 | static struct usb_gadget_driver composite_driver = { |
Michal Nazarewicz | 915c8be | 2009-11-09 14:15:25 +0100 | [diff] [blame] | 1593 | .unbind = composite_unbind, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1594 | |
| 1595 | .setup = composite_setup, |
| 1596 | .disconnect = composite_disconnect, |
| 1597 | |
| 1598 | .suspend = composite_suspend, |
| 1599 | .resume = composite_resume, |
| 1600 | |
| 1601 | .driver = { |
| 1602 | .owner = THIS_MODULE, |
| 1603 | }, |
| 1604 | }; |
| 1605 | |
| 1606 | /** |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1607 | * usb_composite_probe() - register a composite driver |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1608 | * @driver: the driver to register |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1609 | * @bind: the callback used to allocate resources that are shared across the |
| 1610 | * whole device, such as string IDs, and add its configurations using |
| 1611 | * @usb_add_config(). This may fail by returning a negative errno |
| 1612 | * value; it should return zero on successful initialization. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1613 | * Context: single threaded during gadget setup |
| 1614 | * |
| 1615 | * This function is used to register drivers using the composite driver |
| 1616 | * framework. The return value is zero, or a negative errno value. |
| 1617 | * Those values normally come from the driver's @bind method, which does |
| 1618 | * all the work of setting up the driver to match the hardware. |
| 1619 | * |
| 1620 | * On successful return, the gadget is ready to respond to requests from |
| 1621 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 1622 | * while it was binding. That would usually be done in order to wait for |
| 1623 | * some userspace participation. |
| 1624 | */ |
Jassi Brar | 05c3eeb | 2011-02-06 18:47:18 +0900 | [diff] [blame] | 1625 | int usb_composite_probe(struct usb_composite_driver *driver, |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1626 | int (*bind)(struct usb_composite_dev *cdev)) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1627 | { |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1628 | int retval; |
| 1629 | |
Ido Shayevitz | 23dc77c | 2012-07-18 16:16:06 +0300 | [diff] [blame] | 1630 | if (!driver || !driver->dev || !bind) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1631 | return -EINVAL; |
| 1632 | |
| 1633 | if (!driver->name) |
| 1634 | driver->name = "composite"; |
Jassi Brar | 05c3eeb | 2011-02-06 18:47:18 +0900 | [diff] [blame] | 1635 | if (!driver->iProduct) |
| 1636 | driver->iProduct = driver->name; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1637 | composite_driver.function = (char *) driver->name; |
| 1638 | composite_driver.driver.name = driver->name; |
Michal Nazarewicz | d1c3a13 | 2012-08-24 20:46:18 +0200 | [diff] [blame] | 1639 | composite_driver.max_speed = driver->max_speed; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1640 | composite = driver; |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 1641 | composite_gadget_bind = bind; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1642 | |
Lena Salman | d092f2d | 2012-03-12 17:27:24 +0200 | [diff] [blame] | 1643 | retval = usb_gadget_probe_driver(&composite_driver, composite_bind); |
| 1644 | if (retval) |
| 1645 | composite = NULL; |
| 1646 | return retval; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * usb_composite_unregister() - unregister a composite driver |
| 1651 | * @driver: the driver to unregister |
| 1652 | * |
| 1653 | * This function is used to unregister drivers using the composite |
| 1654 | * driver framework. |
| 1655 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1656 | void usb_composite_unregister(struct usb_composite_driver *driver) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1657 | { |
| 1658 | if (composite != driver) |
| 1659 | return; |
| 1660 | usb_gadget_unregister_driver(&composite_driver); |
| 1661 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1662 | |
| 1663 | /** |
| 1664 | * usb_composite_setup_continue() - Continue with the control transfer |
| 1665 | * @cdev: the composite device who's control transfer was kept waiting |
| 1666 | * |
| 1667 | * This function must be called by the USB function driver to continue |
| 1668 | * with the control transfer's data/status stage in case it had requested to |
| 1669 | * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) |
| 1670 | * can request the composite framework to delay the setup request's data/status |
| 1671 | * stages by returning USB_GADGET_DELAYED_STATUS. |
| 1672 | */ |
| 1673 | void usb_composite_setup_continue(struct usb_composite_dev *cdev) |
| 1674 | { |
| 1675 | int value; |
| 1676 | struct usb_request *req = cdev->req; |
| 1677 | unsigned long flags; |
| 1678 | |
| 1679 | DBG(cdev, "%s\n", __func__); |
| 1680 | spin_lock_irqsave(&cdev->lock, flags); |
| 1681 | |
| 1682 | if (cdev->delayed_status == 0) { |
| 1683 | WARN(cdev, "%s: Unexpected call\n", __func__); |
| 1684 | |
| 1685 | } else if (--cdev->delayed_status == 0) { |
| 1686 | DBG(cdev, "%s: Completing delayed status\n", __func__); |
| 1687 | req->length = 0; |
| 1688 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 1689 | if (value < 0) { |
| 1690 | DBG(cdev, "ep_queue --> %d\n", value); |
| 1691 | req->status = 0; |
| 1692 | composite_setup_complete(cdev->gadget->ep0, req); |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1697 | } |
| 1698 | |