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