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