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