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