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