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 | |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 24 | #include "u_os_desc.h" |
| 25 | |
Andrzej Pietrasiewicz | 19824d5 | 2014-05-08 14:06:22 +0200 | [diff] [blame] | 26 | /** |
| 27 | * struct usb_os_string - represents OS String to be reported by a gadget |
| 28 | * @bLength: total length of the entire descritor, always 0x12 |
| 29 | * @bDescriptorType: USB_DT_STRING |
| 30 | * @qwSignature: the OS String proper |
| 31 | * @bMS_VendorCode: code used by the host for subsequent requests |
| 32 | * @bPad: not used, must be zero |
| 33 | */ |
| 34 | struct usb_os_string { |
| 35 | __u8 bLength; |
| 36 | __u8 bDescriptorType; |
| 37 | __u8 qwSignature[OS_STRING_QW_SIGN_LEN]; |
| 38 | __u8 bMS_VendorCode; |
| 39 | __u8 bPad; |
| 40 | } __packed; |
| 41 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 42 | /* |
| 43 | * The code in this file is utility code, used to build a gadget driver |
| 44 | * from one or more "function" drivers, one or more "configuration" |
| 45 | * objects, and a "usb_composite_driver" by gluing them together along |
| 46 | * with the relevant device-wide data. |
| 47 | */ |
| 48 | |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 49 | static struct usb_gadget_strings **get_containers_gs( |
| 50 | struct usb_gadget_string_container *uc) |
| 51 | { |
| 52 | return (struct usb_gadget_strings **)uc->stash; |
| 53 | } |
| 54 | |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 55 | /** |
| 56 | * next_ep_desc() - advance to the next EP descriptor |
| 57 | * @t: currect pointer within descriptor array |
| 58 | * |
| 59 | * Return: next EP descriptor or NULL |
| 60 | * |
| 61 | * Iterate over @t until either EP descriptor found or |
| 62 | * NULL (that indicates end of list) encountered |
| 63 | */ |
| 64 | static struct usb_descriptor_header** |
| 65 | next_ep_desc(struct usb_descriptor_header **t) |
| 66 | { |
| 67 | for (; *t; t++) { |
| 68 | if ((*t)->bDescriptorType == USB_DT_ENDPOINT) |
| 69 | return t; |
| 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * for_each_ep_desc()- iterate over endpoint descriptors in the |
| 76 | * descriptors list |
| 77 | * @start: pointer within descriptor array. |
| 78 | * @ep_desc: endpoint descriptor to use as the loop cursor |
| 79 | */ |
| 80 | #define for_each_ep_desc(start, ep_desc) \ |
| 81 | for (ep_desc = next_ep_desc(start); \ |
| 82 | ep_desc; ep_desc = next_ep_desc(ep_desc+1)) |
| 83 | |
| 84 | /** |
| 85 | * config_ep_by_speed() - configures the given endpoint |
| 86 | * according to gadget speed. |
| 87 | * @g: pointer to the gadget |
| 88 | * @f: usb function |
| 89 | * @_ep: the endpoint to configure |
| 90 | * |
| 91 | * Return: error code, 0 on success |
| 92 | * |
| 93 | * This function chooses the right descriptors for a given |
| 94 | * endpoint according to gadget speed and saves it in the |
| 95 | * endpoint desc field. If the endpoint already has a descriptor |
| 96 | * assigned to it - overwrites it with currently corresponding |
| 97 | * descriptor. The endpoint maxpacket field is updated according |
| 98 | * to the chosen descriptor. |
| 99 | * Note: the supplied function should hold all the descriptors |
| 100 | * for supported speeds |
| 101 | */ |
| 102 | int config_ep_by_speed(struct usb_gadget *g, |
| 103 | struct usb_function *f, |
| 104 | struct usb_ep *_ep) |
| 105 | { |
Felipe Balbi | b785ea7 | 2012-06-06 10:20:23 +0300 | [diff] [blame] | 106 | struct usb_composite_dev *cdev = get_gadget_data(g); |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 107 | struct usb_endpoint_descriptor *chosen_desc = NULL; |
| 108 | struct usb_descriptor_header **speed_desc = NULL; |
| 109 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 110 | struct usb_ss_ep_comp_descriptor *comp_desc = NULL; |
| 111 | int want_comp_desc = 0; |
| 112 | |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 113 | struct usb_descriptor_header **d_spd; /* cursor for speed desc */ |
| 114 | |
| 115 | if (!g || !f || !_ep) |
| 116 | return -EIO; |
| 117 | |
| 118 | /* select desired speed */ |
| 119 | switch (g->speed) { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 120 | case USB_SPEED_SUPER: |
| 121 | if (gadget_is_superspeed(g)) { |
| 122 | speed_desc = f->ss_descriptors; |
| 123 | want_comp_desc = 1; |
| 124 | break; |
| 125 | } |
| 126 | /* else: Fall trough */ |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 127 | case USB_SPEED_HIGH: |
| 128 | if (gadget_is_dualspeed(g)) { |
| 129 | speed_desc = f->hs_descriptors; |
| 130 | break; |
| 131 | } |
| 132 | /* else: fall through */ |
| 133 | default: |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 134 | speed_desc = f->fs_descriptors; |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 135 | } |
| 136 | /* find descriptors */ |
| 137 | for_each_ep_desc(speed_desc, d_spd) { |
| 138 | chosen_desc = (struct usb_endpoint_descriptor *)*d_spd; |
| 139 | if (chosen_desc->bEndpointAddress == _ep->address) |
| 140 | goto ep_found; |
| 141 | } |
| 142 | return -EIO; |
| 143 | |
| 144 | ep_found: |
| 145 | /* commit results */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 146 | _ep->maxpacket = usb_endpoint_maxp(chosen_desc); |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 147 | _ep->desc = chosen_desc; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 148 | _ep->comp_desc = NULL; |
| 149 | _ep->maxburst = 0; |
| 150 | _ep->mult = 0; |
| 151 | if (!want_comp_desc) |
| 152 | return 0; |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 153 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 154 | /* |
| 155 | * Companion descriptor should follow EP descriptor |
| 156 | * USB 3.0 spec, #9.6.7 |
| 157 | */ |
| 158 | comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd); |
| 159 | if (!comp_desc || |
| 160 | (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP)) |
| 161 | return -EIO; |
| 162 | _ep->comp_desc = comp_desc; |
| 163 | if (g->speed == USB_SPEED_SUPER) { |
| 164 | switch (usb_endpoint_type(_ep->desc)) { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 165 | case USB_ENDPOINT_XFER_ISOC: |
| 166 | /* mult: bits 1:0 of bmAttributes */ |
| 167 | _ep->mult = comp_desc->bmAttributes & 0x3; |
Paul Zimmerman | 9e878a6 | 2012-01-16 13:24:38 -0800 | [diff] [blame] | 168 | case USB_ENDPOINT_XFER_BULK: |
| 169 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | b785ea7 | 2012-06-06 10:20:23 +0300 | [diff] [blame] | 170 | _ep->maxburst = comp_desc->bMaxBurst + 1; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 171 | break; |
| 172 | default: |
Felipe Balbi | b785ea7 | 2012-06-06 10:20:23 +0300 | [diff] [blame] | 173 | if (comp_desc->bMaxBurst != 0) |
| 174 | ERROR(cdev, "ep0 bMaxBurst must be 0\n"); |
| 175 | _ep->maxburst = 1; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | } |
Tatyana Brokhman | 48767a4 | 2011-06-28 16:33:49 +0300 | [diff] [blame] | 179 | return 0; |
| 180 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(config_ep_by_speed); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * usb_add_function() - add a function to a configuration |
| 185 | * @config: the configuration |
| 186 | * @function: the function being added |
| 187 | * Context: single threaded during gadget setup |
| 188 | * |
| 189 | * After initialization, each configuration must have one or more |
| 190 | * functions added to it. Adding a function involves calling its @bind() |
| 191 | * method to allocate resources such as interface and string identifiers |
| 192 | * and endpoints. |
| 193 | * |
| 194 | * This function returns the value of the function's bind(), which is |
| 195 | * zero for success else a negative errno value. |
| 196 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 197 | int usb_add_function(struct usb_configuration *config, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 198 | struct usb_function *function) |
| 199 | { |
| 200 | int value = -EINVAL; |
| 201 | |
| 202 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", |
| 203 | function->name, function, |
| 204 | config->label, config); |
| 205 | |
| 206 | if (!function->set_alt || !function->disable) |
| 207 | goto done; |
| 208 | |
| 209 | function->config = config; |
| 210 | list_add_tail(&function->list, &config->functions); |
| 211 | |
| 212 | /* REVISIT *require* function->bind? */ |
| 213 | if (function->bind) { |
| 214 | value = function->bind(config, function); |
| 215 | if (value < 0) { |
| 216 | list_del(&function->list); |
| 217 | function->config = NULL; |
| 218 | } |
| 219 | } else |
| 220 | value = 0; |
| 221 | |
| 222 | /* We allow configurations that don't work at both speeds. |
| 223 | * If we run into a lowspeed Linux system, treat it the same |
| 224 | * as full speed ... it's the function drivers that will need |
| 225 | * to avoid bulk and ISO transfers. |
| 226 | */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 227 | if (!config->fullspeed && function->fs_descriptors) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 228 | config->fullspeed = true; |
| 229 | if (!config->highspeed && function->hs_descriptors) |
| 230 | config->highspeed = true; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 231 | if (!config->superspeed && function->ss_descriptors) |
| 232 | config->superspeed = true; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 233 | |
| 234 | done: |
| 235 | if (value) |
| 236 | DBG(config->cdev, "adding '%s'/%p --> %d\n", |
| 237 | function->name, function, value); |
| 238 | return value; |
| 239 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(usb_add_function); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 241 | |
Sebastian Andrzej Siewior | b4735778 | 2012-12-23 21:10:05 +0100 | [diff] [blame] | 242 | void usb_remove_function(struct usb_configuration *c, struct usb_function *f) |
| 243 | { |
| 244 | if (f->disable) |
| 245 | f->disable(f); |
| 246 | |
| 247 | bitmap_zero(f->endpoints, 32); |
| 248 | list_del(&f->list); |
| 249 | if (f->unbind) |
| 250 | f->unbind(c, f); |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(usb_remove_function); |
| 253 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 254 | /** |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 255 | * usb_function_deactivate - prevent function and gadget enumeration |
| 256 | * @function: the function that isn't yet ready to respond |
| 257 | * |
| 258 | * Blocks response of the gadget driver to host enumeration by |
| 259 | * preventing the data line pullup from being activated. This is |
| 260 | * normally called during @bind() processing to change from the |
| 261 | * initial "ready to respond" state, or when a required resource |
| 262 | * becomes available. |
| 263 | * |
| 264 | * For example, drivers that serve as a passthrough to a userspace |
| 265 | * daemon can block enumeration unless that daemon (such as an OBEX, |
| 266 | * MTP, or print server) is ready to handle host requests. |
| 267 | * |
| 268 | * Not all systems support software control of their USB peripheral |
| 269 | * data pullups. |
| 270 | * |
| 271 | * Returns zero on success, else negative errno. |
| 272 | */ |
| 273 | int usb_function_deactivate(struct usb_function *function) |
| 274 | { |
| 275 | struct usb_composite_dev *cdev = function->config->cdev; |
Felipe Balbi | b2bdf3a | 2009-02-12 15:09:47 +0200 | [diff] [blame] | 276 | unsigned long flags; |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 277 | int status = 0; |
| 278 | |
Felipe Balbi | b2bdf3a | 2009-02-12 15:09:47 +0200 | [diff] [blame] | 279 | spin_lock_irqsave(&cdev->lock, flags); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 280 | |
| 281 | if (cdev->deactivations == 0) |
| 282 | status = usb_gadget_disconnect(cdev->gadget); |
| 283 | if (status == 0) |
| 284 | cdev->deactivations++; |
| 285 | |
Felipe Balbi | b2bdf3a | 2009-02-12 15:09:47 +0200 | [diff] [blame] | 286 | spin_unlock_irqrestore(&cdev->lock, flags); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 287 | return status; |
| 288 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 289 | EXPORT_SYMBOL_GPL(usb_function_deactivate); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * usb_function_activate - allow function and gadget enumeration |
| 293 | * @function: function on which usb_function_activate() was called |
| 294 | * |
| 295 | * Reverses effect of usb_function_deactivate(). If no more functions |
| 296 | * are delaying their activation, the gadget driver will respond to |
| 297 | * host enumeration procedures. |
| 298 | * |
| 299 | * Returns zero on success, else negative errno. |
| 300 | */ |
| 301 | int usb_function_activate(struct usb_function *function) |
| 302 | { |
| 303 | struct usb_composite_dev *cdev = function->config->cdev; |
Michael Grzeschik | 4fefe9f | 2012-07-19 00:20:11 +0200 | [diff] [blame] | 304 | unsigned long flags; |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 305 | int status = 0; |
| 306 | |
Michael Grzeschik | 4fefe9f | 2012-07-19 00:20:11 +0200 | [diff] [blame] | 307 | spin_lock_irqsave(&cdev->lock, flags); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 308 | |
| 309 | if (WARN_ON(cdev->deactivations == 0)) |
| 310 | status = -EINVAL; |
| 311 | else { |
| 312 | cdev->deactivations--; |
| 313 | if (cdev->deactivations == 0) |
| 314 | status = usb_gadget_connect(cdev->gadget); |
| 315 | } |
| 316 | |
Michael Grzeschik | 4fefe9f | 2012-07-19 00:20:11 +0200 | [diff] [blame] | 317 | spin_unlock_irqrestore(&cdev->lock, flags); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 318 | return status; |
| 319 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 320 | EXPORT_SYMBOL_GPL(usb_function_activate); |
David Brownell | 60beed9 | 2008-08-18 17:38:22 -0700 | [diff] [blame] | 321 | |
| 322 | /** |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 323 | * usb_interface_id() - allocate an unused interface ID |
| 324 | * @config: configuration associated with the interface |
| 325 | * @function: function handling the interface |
| 326 | * Context: single threaded during gadget setup |
| 327 | * |
| 328 | * usb_interface_id() is called from usb_function.bind() callbacks to |
| 329 | * allocate new interface IDs. The function driver will then store that |
| 330 | * ID in interface, association, CDC union, and other descriptors. It |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 331 | * will also handle any control requests targeted at that interface, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 332 | * particularly changing its altsetting via set_alt(). There may |
| 333 | * also be class-specific or vendor-specific requests to handle. |
| 334 | * |
| 335 | * All interface identifier should be allocated using this routine, to |
| 336 | * ensure that for example different functions don't wrongly assign |
| 337 | * different meanings to the same identifier. Note that since interface |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 338 | * identifiers are configuration-specific, functions used in more than |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 339 | * one configuration (or more than once in a given configuration) need |
| 340 | * multiple versions of the relevant descriptors. |
| 341 | * |
| 342 | * Returns the interface ID which was allocated; or -ENODEV if no |
| 343 | * more interface IDs can be allocated. |
| 344 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 345 | int usb_interface_id(struct usb_configuration *config, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 346 | struct usb_function *function) |
| 347 | { |
| 348 | unsigned id = config->next_interface_id; |
| 349 | |
| 350 | if (id < MAX_CONFIG_INTERFACES) { |
| 351 | config->interface[id] = function; |
| 352 | config->next_interface_id = id + 1; |
| 353 | return id; |
| 354 | } |
| 355 | return -ENODEV; |
| 356 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 357 | EXPORT_SYMBOL_GPL(usb_interface_id); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 358 | |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 359 | static u8 encode_bMaxPower(enum usb_device_speed speed, |
| 360 | struct usb_configuration *c) |
| 361 | { |
| 362 | unsigned val; |
| 363 | |
| 364 | if (c->MaxPower) |
| 365 | val = c->MaxPower; |
| 366 | else |
| 367 | val = CONFIG_USB_GADGET_VBUS_DRAW; |
| 368 | if (!val) |
| 369 | return 0; |
| 370 | switch (speed) { |
| 371 | case USB_SPEED_SUPER: |
| 372 | return DIV_ROUND_UP(val, 8); |
| 373 | default: |
| 374 | return DIV_ROUND_UP(val, 2); |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 375 | } |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 376 | } |
| 377 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 378 | static int config_buf(struct usb_configuration *config, |
| 379 | enum usb_device_speed speed, void *buf, u8 type) |
| 380 | { |
| 381 | struct usb_config_descriptor *c = buf; |
| 382 | void *next = buf + USB_DT_CONFIG_SIZE; |
Sebastian Andrzej Siewior | e13f17f | 2012-09-10 15:01:51 +0200 | [diff] [blame] | 383 | int len; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 384 | struct usb_function *f; |
| 385 | int status; |
| 386 | |
Sebastian Andrzej Siewior | e13f17f | 2012-09-10 15:01:51 +0200 | [diff] [blame] | 387 | len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 388 | /* write the config descriptor */ |
| 389 | c = buf; |
| 390 | c->bLength = USB_DT_CONFIG_SIZE; |
| 391 | c->bDescriptorType = type; |
| 392 | /* wTotalLength is written later */ |
| 393 | c->bNumInterfaces = config->next_interface_id; |
| 394 | c->bConfigurationValue = config->bConfigurationValue; |
| 395 | c->iConfiguration = config->iConfiguration; |
| 396 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 397 | c->bMaxPower = encode_bMaxPower(speed, config); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 398 | |
| 399 | /* There may be e.g. OTG descriptors */ |
| 400 | if (config->descriptors) { |
| 401 | status = usb_descriptor_fillbuf(next, len, |
| 402 | config->descriptors); |
| 403 | if (status < 0) |
| 404 | return status; |
| 405 | len -= status; |
| 406 | next += status; |
| 407 | } |
| 408 | |
| 409 | /* add each function's descriptors */ |
| 410 | list_for_each_entry(f, &config->functions, list) { |
| 411 | struct usb_descriptor_header **descriptors; |
| 412 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 413 | switch (speed) { |
| 414 | case USB_SPEED_SUPER: |
| 415 | descriptors = f->ss_descriptors; |
| 416 | break; |
| 417 | case USB_SPEED_HIGH: |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 418 | descriptors = f->hs_descriptors; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 419 | break; |
| 420 | default: |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 421 | descriptors = f->fs_descriptors; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 422 | } |
| 423 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 424 | if (!descriptors) |
| 425 | continue; |
| 426 | status = usb_descriptor_fillbuf(next, len, |
| 427 | (const struct usb_descriptor_header **) descriptors); |
| 428 | if (status < 0) |
| 429 | return status; |
| 430 | len -= status; |
| 431 | next += status; |
| 432 | } |
| 433 | |
| 434 | len = next - buf; |
| 435 | c->wTotalLength = cpu_to_le16(len); |
| 436 | return len; |
| 437 | } |
| 438 | |
| 439 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) |
| 440 | { |
| 441 | struct usb_gadget *gadget = cdev->gadget; |
| 442 | struct usb_configuration *c; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 443 | struct list_head *pos; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 444 | u8 type = w_value >> 8; |
| 445 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 446 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 447 | if (gadget->speed == USB_SPEED_SUPER) |
| 448 | speed = gadget->speed; |
| 449 | else if (gadget_is_dualspeed(gadget)) { |
| 450 | int hs = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 451 | if (gadget->speed == USB_SPEED_HIGH) |
| 452 | hs = 1; |
| 453 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 454 | hs = !hs; |
| 455 | if (hs) |
| 456 | speed = USB_SPEED_HIGH; |
| 457 | |
| 458 | } |
| 459 | |
| 460 | /* This is a lookup by config *INDEX* */ |
| 461 | w_value &= 0xff; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 462 | |
| 463 | pos = &cdev->configs; |
| 464 | c = cdev->os_desc_config; |
| 465 | if (c) |
| 466 | goto check_config; |
| 467 | |
| 468 | while ((pos = pos->next) != &cdev->configs) { |
| 469 | c = list_entry(pos, typeof(*c), list); |
| 470 | |
| 471 | /* skip OS Descriptors config which is handled separately */ |
| 472 | if (c == cdev->os_desc_config) |
| 473 | continue; |
| 474 | |
| 475 | check_config: |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 476 | /* ignore configs that won't work at this speed */ |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 477 | switch (speed) { |
| 478 | case USB_SPEED_SUPER: |
| 479 | if (!c->superspeed) |
| 480 | continue; |
| 481 | break; |
| 482 | case USB_SPEED_HIGH: |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 483 | if (!c->highspeed) |
| 484 | continue; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 485 | break; |
| 486 | default: |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 487 | if (!c->fullspeed) |
| 488 | continue; |
| 489 | } |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 490 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 491 | if (w_value == 0) |
| 492 | return config_buf(c, speed, cdev->req->buf, type); |
| 493 | w_value--; |
| 494 | } |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) |
| 499 | { |
| 500 | struct usb_gadget *gadget = cdev->gadget; |
| 501 | struct usb_configuration *c; |
| 502 | unsigned count = 0; |
| 503 | int hs = 0; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 504 | int ss = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 505 | |
| 506 | if (gadget_is_dualspeed(gadget)) { |
| 507 | if (gadget->speed == USB_SPEED_HIGH) |
| 508 | hs = 1; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 509 | if (gadget->speed == USB_SPEED_SUPER) |
| 510 | ss = 1; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 511 | if (type == USB_DT_DEVICE_QUALIFIER) |
| 512 | hs = !hs; |
| 513 | } |
| 514 | list_for_each_entry(c, &cdev->configs, list) { |
| 515 | /* ignore configs that won't work at this speed */ |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 516 | if (ss) { |
| 517 | if (!c->superspeed) |
| 518 | continue; |
| 519 | } else if (hs) { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 520 | if (!c->highspeed) |
| 521 | continue; |
| 522 | } else { |
| 523 | if (!c->fullspeed) |
| 524 | continue; |
| 525 | } |
| 526 | count++; |
| 527 | } |
| 528 | return count; |
| 529 | } |
| 530 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 531 | /** |
| 532 | * bos_desc() - prepares the BOS descriptor. |
| 533 | * @cdev: pointer to usb_composite device to generate the bos |
| 534 | * descriptor for |
| 535 | * |
| 536 | * This function generates the BOS (Binary Device Object) |
| 537 | * descriptor and its device capabilities descriptors. The BOS |
| 538 | * descriptor should be supported by a SuperSpeed device. |
| 539 | */ |
| 540 | static int bos_desc(struct usb_composite_dev *cdev) |
| 541 | { |
| 542 | struct usb_ext_cap_descriptor *usb_ext; |
| 543 | struct usb_ss_cap_descriptor *ss_cap; |
| 544 | struct usb_dcd_config_params dcd_config_params; |
| 545 | struct usb_bos_descriptor *bos = cdev->req->buf; |
| 546 | |
| 547 | bos->bLength = USB_DT_BOS_SIZE; |
| 548 | bos->bDescriptorType = USB_DT_BOS; |
| 549 | |
| 550 | bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); |
| 551 | bos->bNumDeviceCaps = 0; |
| 552 | |
| 553 | /* |
| 554 | * A SuperSpeed device shall include the USB2.0 extension descriptor |
| 555 | * and shall support LPM when operating in USB2.0 HS mode. |
| 556 | */ |
| 557 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 558 | bos->bNumDeviceCaps++; |
| 559 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE); |
| 560 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; |
| 561 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 562 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; |
Felipe Balbi | a661593 | 2014-09-30 16:08:03 -0500 | [diff] [blame] | 563 | usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT); |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * The Superspeed USB Capability descriptor shall be implemented by all |
| 567 | * SuperSpeed devices. |
| 568 | */ |
| 569 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 570 | bos->bNumDeviceCaps++; |
| 571 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); |
| 572 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; |
| 573 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 574 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; |
| 575 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ |
| 576 | ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | |
| 577 | USB_FULL_SPEED_OPERATION | |
| 578 | USB_HIGH_SPEED_OPERATION | |
| 579 | USB_5GBPS_OPERATION); |
| 580 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; |
| 581 | |
| 582 | /* Get Controller configuration */ |
| 583 | if (cdev->gadget->ops->get_config_params) |
| 584 | cdev->gadget->ops->get_config_params(&dcd_config_params); |
| 585 | else { |
Felipe Balbi | 089b837 | 2011-10-10 09:43:44 +0300 | [diff] [blame] | 586 | dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 587 | dcd_config_params.bU2DevExitLat = |
Felipe Balbi | 089b837 | 2011-10-10 09:43:44 +0300 | [diff] [blame] | 588 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 589 | } |
| 590 | ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; |
| 591 | ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; |
| 592 | |
| 593 | return le16_to_cpu(bos->wTotalLength); |
| 594 | } |
| 595 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 596 | static void device_qual(struct usb_composite_dev *cdev) |
| 597 | { |
| 598 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 599 | |
| 600 | qual->bLength = sizeof(*qual); |
| 601 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 602 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 603 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 604 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 605 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 606 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 607 | /* ASSUME same EP0 fifo size at both speeds */ |
Sebastian Andrzej Siewior | 765f5b8 | 2011-06-23 14:26:11 +0200 | [diff] [blame] | 608 | qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 609 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
David Lopo | c24f422 | 2008-07-01 13:14:17 -0700 | [diff] [blame] | 610 | qual->bRESERVED = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /*-------------------------------------------------------------------------*/ |
| 614 | |
| 615 | static void reset_config(struct usb_composite_dev *cdev) |
| 616 | { |
| 617 | struct usb_function *f; |
| 618 | |
| 619 | DBG(cdev, "reset config\n"); |
| 620 | |
| 621 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 622 | if (f->disable) |
| 623 | f->disable(f); |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 624 | |
| 625 | bitmap_zero(f->endpoints, 32); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 626 | } |
| 627 | cdev->config = NULL; |
Michael Grzeschik | 2bac51a | 2013-11-11 23:43:32 +0100 | [diff] [blame] | 628 | cdev->delayed_status = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static int set_config(struct usb_composite_dev *cdev, |
| 632 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 633 | { |
| 634 | struct usb_gadget *gadget = cdev->gadget; |
| 635 | struct usb_configuration *c = NULL; |
| 636 | int result = -EINVAL; |
| 637 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 638 | int tmp; |
| 639 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 640 | if (number) { |
| 641 | list_for_each_entry(c, &cdev->configs, list) { |
| 642 | if (c->bConfigurationValue == number) { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 643 | /* |
| 644 | * We disable the FDs of the previous |
| 645 | * configuration only if the new configuration |
| 646 | * is a valid one |
| 647 | */ |
| 648 | if (cdev->config) |
| 649 | reset_config(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 650 | result = 0; |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | if (result < 0) |
| 655 | goto done; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 656 | } else { /* Zero configuration value - need to reset the config */ |
| 657 | if (cdev->config) |
| 658 | reset_config(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 659 | result = 0; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 660 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 661 | |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 662 | INFO(cdev, "%s config #%d: %s\n", |
| 663 | usb_speed_string(gadget->speed), |
| 664 | number, c ? c->label : "unconfigured"); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 665 | |
| 666 | if (!c) |
| 667 | goto done; |
| 668 | |
Peter Chen | 6027f31 | 2014-04-29 13:26:28 +0800 | [diff] [blame] | 669 | usb_gadget_set_state(gadget, USB_STATE_CONFIGURED); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 670 | cdev->config = c; |
| 671 | |
| 672 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 673 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 674 | struct usb_function *f = c->interface[tmp]; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 675 | struct usb_descriptor_header **descriptors; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 676 | |
| 677 | if (!f) |
| 678 | break; |
| 679 | |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 680 | /* |
| 681 | * Record which endpoints are used by the function. This is used |
| 682 | * to dispatch control requests targeted at that endpoint to the |
| 683 | * function's setup callback instead of the current |
| 684 | * configuration's setup callback. |
| 685 | */ |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 686 | switch (gadget->speed) { |
| 687 | case USB_SPEED_SUPER: |
| 688 | descriptors = f->ss_descriptors; |
| 689 | break; |
| 690 | case USB_SPEED_HIGH: |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 691 | descriptors = f->hs_descriptors; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 692 | break; |
| 693 | default: |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 694 | descriptors = f->fs_descriptors; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 695 | } |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 696 | |
| 697 | for (; *descriptors; ++descriptors) { |
| 698 | struct usb_endpoint_descriptor *ep; |
| 699 | int addr; |
| 700 | |
| 701 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 702 | continue; |
| 703 | |
| 704 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 705 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 706 | | (ep->bEndpointAddress & 0x0f); |
| 707 | set_bit(addr, f->endpoints); |
| 708 | } |
| 709 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 710 | result = f->set_alt(f, tmp, 0); |
| 711 | if (result < 0) { |
| 712 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", |
| 713 | tmp, f->name, f, result); |
| 714 | |
| 715 | reset_config(cdev); |
| 716 | goto done; |
| 717 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 718 | |
| 719 | if (result == USB_GADGET_DELAYED_STATUS) { |
| 720 | DBG(cdev, |
| 721 | "%s: interface %d (%s) requested delayed status\n", |
| 722 | __func__, tmp, f->name); |
| 723 | cdev->delayed_status++; |
| 724 | DBG(cdev, "delayed_status count %d\n", |
| 725 | cdev->delayed_status); |
| 726 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* when we return, be sure our power usage is valid */ |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 730 | power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 731 | done: |
| 732 | usb_gadget_vbus_draw(gadget, power); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 733 | if (result >= 0 && cdev->delayed_status) |
| 734 | result = USB_GADGET_DELAYED_STATUS; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 735 | return result; |
| 736 | } |
| 737 | |
Sebastian Andrzej Siewior | de53c25 | 2012-12-23 21:10:00 +0100 | [diff] [blame] | 738 | int usb_add_config_only(struct usb_composite_dev *cdev, |
| 739 | struct usb_configuration *config) |
| 740 | { |
| 741 | struct usb_configuration *c; |
| 742 | |
| 743 | if (!config->bConfigurationValue) |
| 744 | return -EINVAL; |
| 745 | |
| 746 | /* Prevent duplicate configuration identifiers */ |
| 747 | list_for_each_entry(c, &cdev->configs, list) { |
| 748 | if (c->bConfigurationValue == config->bConfigurationValue) |
| 749 | return -EBUSY; |
| 750 | } |
| 751 | |
| 752 | config->cdev = cdev; |
| 753 | list_add_tail(&config->list, &cdev->configs); |
| 754 | |
| 755 | INIT_LIST_HEAD(&config->functions); |
| 756 | config->next_interface_id = 0; |
| 757 | memset(config->interface, 0, sizeof(config->interface)); |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | EXPORT_SYMBOL_GPL(usb_add_config_only); |
| 762 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 763 | /** |
| 764 | * usb_add_config() - add a configuration to a device. |
| 765 | * @cdev: wraps the USB gadget |
| 766 | * @config: the configuration, with bConfigurationValue assigned |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 767 | * @bind: the configuration's bind function |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 768 | * Context: single threaded during gadget setup |
| 769 | * |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 770 | * One of the main tasks of a composite @bind() routine is to |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 771 | * add each of the configurations it supports, using this routine. |
| 772 | * |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 773 | * This function returns the value of the configuration's @bind(), which |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 774 | * is zero for success else a negative errno value. Binding configurations |
| 775 | * assigns global resources including string IDs, and per-configuration |
| 776 | * resources such as interface IDs and endpoints. |
| 777 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 778 | int usb_add_config(struct usb_composite_dev *cdev, |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 779 | struct usb_configuration *config, |
| 780 | int (*bind)(struct usb_configuration *)) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 781 | { |
| 782 | int status = -EINVAL; |
Sebastian Andrzej Siewior | de53c25 | 2012-12-23 21:10:00 +0100 | [diff] [blame] | 783 | |
| 784 | if (!bind) |
| 785 | goto done; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 786 | |
| 787 | DBG(cdev, "adding config #%u '%s'/%p\n", |
| 788 | config->bConfigurationValue, |
| 789 | config->label, config); |
| 790 | |
Sebastian Andrzej Siewior | de53c25 | 2012-12-23 21:10:00 +0100 | [diff] [blame] | 791 | status = usb_add_config_only(cdev, config); |
| 792 | if (status) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 793 | goto done; |
| 794 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 795 | status = bind(config); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 796 | if (status < 0) { |
Yongsul Oh | 124ef38 | 2012-03-20 10:38:38 +0900 | [diff] [blame] | 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", |
| 805 | f->name, f); |
| 806 | f->unbind(config, f); |
| 807 | /* may free memory for "f" */ |
| 808 | } |
| 809 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 810 | list_del(&config->list); |
| 811 | config->cdev = NULL; |
| 812 | } else { |
| 813 | unsigned i; |
| 814 | |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 815 | DBG(cdev, "cfg %d/%p speeds:%s%s%s\n", |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 816 | config->bConfigurationValue, config, |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 817 | config->superspeed ? " super" : "", |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 818 | config->highspeed ? " high" : "", |
| 819 | config->fullspeed |
| 820 | ? (gadget_is_dualspeed(cdev->gadget) |
| 821 | ? " full" |
| 822 | : " full/low") |
| 823 | : ""); |
| 824 | |
| 825 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 826 | struct usb_function *f = config->interface[i]; |
| 827 | |
| 828 | if (!f) |
| 829 | continue; |
| 830 | DBG(cdev, " interface %d = %s/%p\n", |
| 831 | i, f->name, f); |
| 832 | } |
| 833 | } |
| 834 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 835 | /* set_alt(), or next bind(), sets up |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 836 | * ep->driver_data as needed. |
| 837 | */ |
| 838 | usb_ep_autoconfig_reset(cdev->gadget); |
| 839 | |
| 840 | done: |
| 841 | if (status) |
| 842 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, |
| 843 | config->bConfigurationValue, status); |
| 844 | return status; |
| 845 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 846 | EXPORT_SYMBOL_GPL(usb_add_config); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 847 | |
Benoit Goby | 51cce6f | 2012-05-10 10:07:57 +0200 | [diff] [blame] | 848 | static void remove_config(struct usb_composite_dev *cdev, |
| 849 | struct usb_configuration *config) |
| 850 | { |
| 851 | while (!list_empty(&config->functions)) { |
| 852 | struct usb_function *f; |
| 853 | |
| 854 | f = list_first_entry(&config->functions, |
| 855 | struct usb_function, list); |
| 856 | list_del(&f->list); |
| 857 | if (f->unbind) { |
| 858 | DBG(cdev, "unbind function '%s'/%p\n", f->name, f); |
| 859 | f->unbind(config, f); |
| 860 | /* may free memory for "f" */ |
| 861 | } |
| 862 | } |
| 863 | list_del(&config->list); |
| 864 | if (config->unbind) { |
| 865 | DBG(cdev, "unbind config '%s'/%p\n", config->label, config); |
| 866 | config->unbind(config); |
| 867 | /* may free memory for "c" */ |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * usb_remove_config() - remove a configuration from a device. |
| 873 | * @cdev: wraps the USB gadget |
| 874 | * @config: the configuration |
| 875 | * |
| 876 | * Drivers must call usb_gadget_disconnect before calling this function |
| 877 | * to disconnect the device from the host and make sure the host will not |
| 878 | * try to enumerate the device while we are changing the config list. |
| 879 | */ |
| 880 | void usb_remove_config(struct usb_composite_dev *cdev, |
| 881 | struct usb_configuration *config) |
| 882 | { |
| 883 | unsigned long flags; |
| 884 | |
| 885 | spin_lock_irqsave(&cdev->lock, flags); |
| 886 | |
| 887 | if (cdev->config == config) |
| 888 | reset_config(cdev); |
| 889 | |
| 890 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 891 | |
| 892 | remove_config(cdev, config); |
| 893 | } |
| 894 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 895 | /*-------------------------------------------------------------------------*/ |
| 896 | |
| 897 | /* We support strings in multiple languages ... string descriptor zero |
| 898 | * says which languages are supported. The typical case will be that |
| 899 | * only one language (probably English) is used, with I18N handled on |
| 900 | * the host side. |
| 901 | */ |
| 902 | |
| 903 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) |
| 904 | { |
| 905 | const struct usb_gadget_strings *s; |
Dan Carpenter | 20c5e74 | 2012-04-17 09:30:22 +0300 | [diff] [blame] | 906 | __le16 language; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 907 | __le16 *tmp; |
| 908 | |
| 909 | while (*sp) { |
| 910 | s = *sp; |
| 911 | language = cpu_to_le16(s->language); |
| 912 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { |
| 913 | if (*tmp == language) |
| 914 | goto repeat; |
| 915 | } |
| 916 | *tmp++ = language; |
| 917 | repeat: |
| 918 | sp++; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | static int lookup_string( |
| 923 | struct usb_gadget_strings **sp, |
| 924 | void *buf, |
| 925 | u16 language, |
| 926 | int id |
| 927 | ) |
| 928 | { |
| 929 | struct usb_gadget_strings *s; |
| 930 | int value; |
| 931 | |
| 932 | while (*sp) { |
| 933 | s = *sp++; |
| 934 | if (s->language != language) |
| 935 | continue; |
| 936 | value = usb_gadget_get_string(s, id, buf); |
| 937 | if (value > 0) |
| 938 | return value; |
| 939 | } |
| 940 | return -EINVAL; |
| 941 | } |
| 942 | |
| 943 | static int get_string(struct usb_composite_dev *cdev, |
| 944 | void *buf, u16 language, int id) |
| 945 | { |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 946 | struct usb_composite_driver *composite = cdev->driver; |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 947 | struct usb_gadget_string_container *uc; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 948 | struct usb_configuration *c; |
| 949 | struct usb_function *f; |
| 950 | int len; |
| 951 | |
| 952 | /* Yes, not only is USB's I18N support probably more than most |
| 953 | * folk will ever care about ... also, it's all supported here. |
| 954 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 955 | */ |
| 956 | |
| 957 | /* 0 == report all available language codes */ |
| 958 | if (id == 0) { |
| 959 | struct usb_string_descriptor *s = buf; |
| 960 | struct usb_gadget_strings **sp; |
| 961 | |
| 962 | memset(s, 0, 256); |
| 963 | s->bDescriptorType = USB_DT_STRING; |
| 964 | |
| 965 | sp = composite->strings; |
| 966 | if (sp) |
| 967 | collect_langs(sp, s->wData); |
| 968 | |
| 969 | list_for_each_entry(c, &cdev->configs, list) { |
| 970 | sp = c->strings; |
| 971 | if (sp) |
| 972 | collect_langs(sp, s->wData); |
| 973 | |
| 974 | list_for_each_entry(f, &c->functions, list) { |
| 975 | sp = f->strings; |
| 976 | if (sp) |
| 977 | collect_langs(sp, s->wData); |
| 978 | } |
| 979 | } |
Sebastian Andrzej Siewior | 27a46633 | 2012-12-23 21:10:23 +0100 | [diff] [blame] | 980 | list_for_each_entry(uc, &cdev->gstrings, list) { |
| 981 | struct usb_gadget_strings **sp; |
| 982 | |
| 983 | sp = get_containers_gs(uc); |
| 984 | collect_langs(sp, s->wData); |
| 985 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 986 | |
Roel Kluin | 417b57b | 2009-08-06 16:09:51 -0700 | [diff] [blame] | 987 | for (len = 0; len <= 126 && s->wData[len]; len++) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 988 | continue; |
| 989 | if (!len) |
| 990 | return -EINVAL; |
| 991 | |
| 992 | s->bLength = 2 * (len + 1); |
| 993 | return s->bLength; |
| 994 | } |
| 995 | |
Andrzej Pietrasiewicz | 19824d5 | 2014-05-08 14:06:22 +0200 | [diff] [blame] | 996 | if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) { |
| 997 | struct usb_os_string *b = buf; |
| 998 | b->bLength = sizeof(*b); |
| 999 | b->bDescriptorType = USB_DT_STRING; |
| 1000 | compiletime_assert( |
| 1001 | sizeof(b->qwSignature) == sizeof(cdev->qw_sign), |
| 1002 | "qwSignature size must be equal to qw_sign"); |
| 1003 | memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature)); |
| 1004 | b->bMS_VendorCode = cdev->b_vendor_code; |
| 1005 | b->bPad = 0; |
| 1006 | return sizeof(*b); |
| 1007 | } |
| 1008 | |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 1009 | list_for_each_entry(uc, &cdev->gstrings, list) { |
| 1010 | struct usb_gadget_strings **sp; |
| 1011 | |
| 1012 | sp = get_containers_gs(uc); |
| 1013 | len = lookup_string(sp, buf, language, id); |
| 1014 | if (len > 0) |
| 1015 | return len; |
| 1016 | } |
| 1017 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 1018 | /* String IDs are device-scoped, so we look up each string |
| 1019 | * table we're told about. These lookups are infrequent; |
| 1020 | * simpler-is-better here. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1021 | */ |
| 1022 | if (composite->strings) { |
| 1023 | len = lookup_string(composite->strings, buf, language, id); |
| 1024 | if (len > 0) |
| 1025 | return len; |
| 1026 | } |
| 1027 | list_for_each_entry(c, &cdev->configs, list) { |
| 1028 | if (c->strings) { |
| 1029 | len = lookup_string(c->strings, buf, language, id); |
| 1030 | if (len > 0) |
| 1031 | return len; |
| 1032 | } |
| 1033 | list_for_each_entry(f, &c->functions, list) { |
| 1034 | if (!f->strings) |
| 1035 | continue; |
| 1036 | len = lookup_string(f->strings, buf, language, id); |
| 1037 | if (len > 0) |
| 1038 | return len; |
| 1039 | } |
| 1040 | } |
| 1041 | return -EINVAL; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * usb_string_id() - allocate an unused string ID |
| 1046 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1047 | * Context: single threaded during gadget setup |
| 1048 | * |
| 1049 | * @usb_string_id() is called from bind() callbacks to allocate |
| 1050 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1051 | * then store that ID in the appropriate descriptors and string table. |
| 1052 | * |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1053 | * All string identifier should be allocated using this, |
| 1054 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 1055 | * that for example different functions don't wrongly assign different |
| 1056 | * meanings to the same identifier. |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1057 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 1058 | int usb_string_id(struct usb_composite_dev *cdev) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1059 | { |
| 1060 | if (cdev->next_string_id < 254) { |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1061 | /* string id 0 is reserved by USB spec for list of |
| 1062 | * supported languages */ |
| 1063 | /* 255 reserved as well? -- mina86 */ |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1064 | cdev->next_string_id++; |
| 1065 | return cdev->next_string_id; |
| 1066 | } |
| 1067 | return -ENODEV; |
| 1068 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 1069 | EXPORT_SYMBOL_GPL(usb_string_id); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1070 | |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1071 | /** |
| 1072 | * usb_string_ids() - allocate unused string IDs in batch |
| 1073 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1074 | * @str: an array of usb_string objects to assign numbers to |
| 1075 | * Context: single threaded during gadget setup |
| 1076 | * |
| 1077 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 1078 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1079 | * then copy IDs from the string table to the appropriate descriptors |
| 1080 | * and string table for other languages. |
| 1081 | * |
| 1082 | * All string identifier should be allocated using this, |
| 1083 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 1084 | * example different functions don't wrongly assign different meanings |
| 1085 | * to the same identifier. |
| 1086 | */ |
| 1087 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 1088 | { |
| 1089 | int next = cdev->next_string_id; |
| 1090 | |
| 1091 | for (; str->s; ++str) { |
| 1092 | if (unlikely(next >= 254)) |
| 1093 | return -ENODEV; |
| 1094 | str->id = ++next; |
| 1095 | } |
| 1096 | |
| 1097 | cdev->next_string_id = next; |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 1101 | EXPORT_SYMBOL_GPL(usb_string_ids_tab); |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1102 | |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 1103 | static struct usb_gadget_string_container *copy_gadget_strings( |
| 1104 | struct usb_gadget_strings **sp, unsigned n_gstrings, |
| 1105 | unsigned n_strings) |
| 1106 | { |
| 1107 | struct usb_gadget_string_container *uc; |
| 1108 | struct usb_gadget_strings **gs_array; |
| 1109 | struct usb_gadget_strings *gs; |
| 1110 | struct usb_string *s; |
| 1111 | unsigned mem; |
| 1112 | unsigned n_gs; |
| 1113 | unsigned n_s; |
| 1114 | void *stash; |
| 1115 | |
| 1116 | mem = sizeof(*uc); |
| 1117 | mem += sizeof(void *) * (n_gstrings + 1); |
| 1118 | mem += sizeof(struct usb_gadget_strings) * n_gstrings; |
| 1119 | mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings); |
| 1120 | uc = kmalloc(mem, GFP_KERNEL); |
| 1121 | if (!uc) |
| 1122 | return ERR_PTR(-ENOMEM); |
| 1123 | gs_array = get_containers_gs(uc); |
| 1124 | stash = uc->stash; |
| 1125 | stash += sizeof(void *) * (n_gstrings + 1); |
| 1126 | for (n_gs = 0; n_gs < n_gstrings; n_gs++) { |
| 1127 | struct usb_string *org_s; |
| 1128 | |
| 1129 | gs_array[n_gs] = stash; |
| 1130 | gs = gs_array[n_gs]; |
| 1131 | stash += sizeof(struct usb_gadget_strings); |
| 1132 | gs->language = sp[n_gs]->language; |
| 1133 | gs->strings = stash; |
| 1134 | org_s = sp[n_gs]->strings; |
| 1135 | |
| 1136 | for (n_s = 0; n_s < n_strings; n_s++) { |
| 1137 | s = stash; |
| 1138 | stash += sizeof(struct usb_string); |
| 1139 | if (org_s->s) |
| 1140 | s->s = org_s->s; |
| 1141 | else |
| 1142 | s->s = ""; |
| 1143 | org_s++; |
| 1144 | } |
| 1145 | s = stash; |
| 1146 | s->s = NULL; |
| 1147 | stash += sizeof(struct usb_string); |
| 1148 | |
| 1149 | } |
| 1150 | gs_array[n_gs] = NULL; |
| 1151 | return uc; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids |
| 1156 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1157 | * and attached. |
| 1158 | * @sp: an array of usb_gadget_strings to attach. |
| 1159 | * @n_strings: number of entries in each usb_strings array (sp[]->strings) |
| 1160 | * |
| 1161 | * This function will create a deep copy of usb_gadget_strings and usb_string |
| 1162 | * and attach it to the cdev. The actual string (usb_string.s) will not be |
| 1163 | * copied but only a referenced will be made. The struct usb_gadget_strings |
Masanari Iida | 06ed0de | 2015-03-10 22:37:46 +0900 | [diff] [blame] | 1164 | * array may contain multiple languages and should be NULL terminated. |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 1165 | * The ->language pointer of each struct usb_gadget_strings has to contain the |
| 1166 | * same amount of entries. |
| 1167 | * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first |
Masanari Iida | 06ed0de | 2015-03-10 22:37:46 +0900 | [diff] [blame] | 1168 | * usb_string entry of es-ES contains the translation of the first usb_string |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 1169 | * entry of en-US. Therefore both entries become the same id assign. |
| 1170 | */ |
| 1171 | struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, |
| 1172 | struct usb_gadget_strings **sp, unsigned n_strings) |
| 1173 | { |
| 1174 | struct usb_gadget_string_container *uc; |
| 1175 | struct usb_gadget_strings **n_gs; |
| 1176 | unsigned n_gstrings = 0; |
| 1177 | unsigned i; |
| 1178 | int ret; |
| 1179 | |
| 1180 | for (i = 0; sp[i]; i++) |
| 1181 | n_gstrings++; |
| 1182 | |
| 1183 | if (!n_gstrings) |
| 1184 | return ERR_PTR(-EINVAL); |
| 1185 | |
| 1186 | uc = copy_gadget_strings(sp, n_gstrings, n_strings); |
| 1187 | if (IS_ERR(uc)) |
Felipe Balbi | dad4bab | 2014-03-10 13:30:56 -0500 | [diff] [blame] | 1188 | return ERR_CAST(uc); |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 1189 | |
| 1190 | n_gs = get_containers_gs(uc); |
| 1191 | ret = usb_string_ids_tab(cdev, n_gs[0]->strings); |
| 1192 | if (ret) |
| 1193 | goto err; |
| 1194 | |
| 1195 | for (i = 1; i < n_gstrings; i++) { |
| 1196 | struct usb_string *m_s; |
| 1197 | struct usb_string *s; |
| 1198 | unsigned n; |
| 1199 | |
| 1200 | m_s = n_gs[0]->strings; |
| 1201 | s = n_gs[i]->strings; |
| 1202 | for (n = 0; n < n_strings; n++) { |
| 1203 | s->id = m_s->id; |
| 1204 | s++; |
| 1205 | m_s++; |
| 1206 | } |
| 1207 | } |
| 1208 | list_add_tail(&uc->list, &cdev->gstrings); |
| 1209 | return n_gs[0]->strings; |
| 1210 | err: |
| 1211 | kfree(uc); |
| 1212 | return ERR_PTR(ret); |
| 1213 | } |
| 1214 | EXPORT_SYMBOL_GPL(usb_gstrings_attach); |
| 1215 | |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1216 | /** |
| 1217 | * usb_string_ids_n() - allocate unused string IDs in batch |
Randy Dunlap | d187abb | 2010-08-11 12:07:13 -0700 | [diff] [blame] | 1218 | * @c: the device whose string descriptor IDs are being allocated |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1219 | * @n: number of string IDs to allocate |
| 1220 | * Context: single threaded during gadget setup |
| 1221 | * |
| 1222 | * 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] | 1223 | * 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] | 1224 | * is, returns last requested ID which is now very useful information. |
| 1225 | * |
| 1226 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 1227 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1228 | * then store that ID in the appropriate descriptors and string table. |
| 1229 | * |
| 1230 | * All string identifier should be allocated using this, |
| 1231 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 1232 | * example different functions don't wrongly assign different meanings |
| 1233 | * to the same identifier. |
| 1234 | */ |
| 1235 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 1236 | { |
| 1237 | unsigned next = c->next_string_id; |
| 1238 | if (unlikely(n > 254 || (unsigned)next + n > 254)) |
| 1239 | return -ENODEV; |
| 1240 | c->next_string_id += n; |
| 1241 | return next + 1; |
| 1242 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 1243 | EXPORT_SYMBOL_GPL(usb_string_ids_n); |
Michal Nazarewicz | f2adc4f | 2010-06-16 12:07:59 +0200 | [diff] [blame] | 1244 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1245 | /*-------------------------------------------------------------------------*/ |
| 1246 | |
| 1247 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 1248 | { |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 1249 | struct usb_composite_dev *cdev; |
| 1250 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1251 | if (req->status || req->actual != req->length) |
| 1252 | DBG((struct usb_composite_dev *) ep->driver_data, |
| 1253 | "setup complete --> %d, %d/%d\n", |
| 1254 | req->status, req->actual, req->length); |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 1255 | |
| 1256 | /* |
| 1257 | * REVIST The same ep0 requests are shared with function drivers |
| 1258 | * so they don't have to maintain the same ->complete() stubs. |
| 1259 | * |
| 1260 | * Because of that, we need to check for the validity of ->context |
| 1261 | * here, even though we know we've set it to something useful. |
| 1262 | */ |
| 1263 | if (!req->context) |
| 1264 | return; |
| 1265 | |
| 1266 | cdev = req->context; |
| 1267 | |
| 1268 | if (cdev->req == req) |
| 1269 | cdev->setup_pending = false; |
| 1270 | else if (cdev->os_desc_req == req) |
| 1271 | cdev->os_desc_pending = false; |
| 1272 | else |
| 1273 | WARN(1, "unknown request %p\n", req); |
| 1274 | } |
| 1275 | |
| 1276 | static int composite_ep0_queue(struct usb_composite_dev *cdev, |
| 1277 | struct usb_request *req, gfp_t gfp_flags) |
| 1278 | { |
| 1279 | int ret; |
| 1280 | |
| 1281 | ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags); |
| 1282 | if (ret == 0) { |
| 1283 | if (cdev->req == req) |
| 1284 | cdev->setup_pending = true; |
| 1285 | else if (cdev->os_desc_req == req) |
| 1286 | cdev->os_desc_pending = true; |
| 1287 | else |
| 1288 | WARN(1, "unknown request %p\n", req); |
| 1289 | } |
| 1290 | |
| 1291 | return ret; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1294 | static int count_ext_compat(struct usb_configuration *c) |
| 1295 | { |
| 1296 | int i, res; |
| 1297 | |
| 1298 | res = 0; |
| 1299 | for (i = 0; i < c->next_interface_id; ++i) { |
| 1300 | struct usb_function *f; |
| 1301 | int j; |
| 1302 | |
| 1303 | f = c->interface[i]; |
| 1304 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1305 | struct usb_os_desc *d; |
| 1306 | |
| 1307 | if (i != f->os_desc_table[j].if_id) |
| 1308 | continue; |
| 1309 | d = f->os_desc_table[j].os_desc; |
| 1310 | if (d && d->ext_compat_id) |
| 1311 | ++res; |
| 1312 | } |
| 1313 | } |
| 1314 | BUG_ON(res > 255); |
| 1315 | return res; |
| 1316 | } |
| 1317 | |
| 1318 | static void fill_ext_compat(struct usb_configuration *c, u8 *buf) |
| 1319 | { |
| 1320 | int i, count; |
| 1321 | |
| 1322 | count = 16; |
| 1323 | for (i = 0; i < c->next_interface_id; ++i) { |
| 1324 | struct usb_function *f; |
| 1325 | int j; |
| 1326 | |
| 1327 | f = c->interface[i]; |
| 1328 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1329 | struct usb_os_desc *d; |
| 1330 | |
| 1331 | if (i != f->os_desc_table[j].if_id) |
| 1332 | continue; |
| 1333 | d = f->os_desc_table[j].os_desc; |
| 1334 | if (d && d->ext_compat_id) { |
| 1335 | *buf++ = i; |
| 1336 | *buf++ = 0x01; |
| 1337 | memcpy(buf, d->ext_compat_id, 16); |
| 1338 | buf += 22; |
| 1339 | } else { |
| 1340 | ++buf; |
| 1341 | *buf = 0x01; |
| 1342 | buf += 23; |
| 1343 | } |
| 1344 | count += 24; |
| 1345 | if (count >= 4096) |
| 1346 | return; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | static int count_ext_prop(struct usb_configuration *c, int interface) |
| 1352 | { |
| 1353 | struct usb_function *f; |
Julia Lawall | 849b133 | 2014-05-19 06:31:07 +0200 | [diff] [blame] | 1354 | int j; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1355 | |
| 1356 | f = c->interface[interface]; |
| 1357 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1358 | struct usb_os_desc *d; |
| 1359 | |
| 1360 | if (interface != f->os_desc_table[j].if_id) |
| 1361 | continue; |
| 1362 | d = f->os_desc_table[j].os_desc; |
| 1363 | if (d && d->ext_compat_id) |
| 1364 | return d->ext_prop_count; |
| 1365 | } |
Julia Lawall | 849b133 | 2014-05-19 06:31:07 +0200 | [diff] [blame] | 1366 | return 0; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static int len_ext_prop(struct usb_configuration *c, int interface) |
| 1370 | { |
| 1371 | struct usb_function *f; |
| 1372 | struct usb_os_desc *d; |
| 1373 | int j, res; |
| 1374 | |
| 1375 | res = 10; /* header length */ |
| 1376 | f = c->interface[interface]; |
| 1377 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1378 | if (interface != f->os_desc_table[j].if_id) |
| 1379 | continue; |
| 1380 | d = f->os_desc_table[j].os_desc; |
| 1381 | if (d) |
| 1382 | return min(res + d->ext_prop_len, 4096); |
| 1383 | } |
| 1384 | return res; |
| 1385 | } |
| 1386 | |
| 1387 | static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf) |
| 1388 | { |
| 1389 | struct usb_function *f; |
| 1390 | struct usb_os_desc *d; |
| 1391 | struct usb_os_desc_ext_prop *ext_prop; |
| 1392 | int j, count, n, ret; |
| 1393 | u8 *start = buf; |
| 1394 | |
| 1395 | f = c->interface[interface]; |
| 1396 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1397 | if (interface != f->os_desc_table[j].if_id) |
| 1398 | continue; |
| 1399 | d = f->os_desc_table[j].os_desc; |
| 1400 | if (d) |
| 1401 | list_for_each_entry(ext_prop, &d->ext_prop, entry) { |
| 1402 | /* 4kB minus header length */ |
| 1403 | n = buf - start; |
| 1404 | if (n >= 4086) |
| 1405 | return 0; |
| 1406 | |
| 1407 | count = ext_prop->data_len + |
| 1408 | ext_prop->name_len + 14; |
| 1409 | if (count > 4086 - n) |
| 1410 | return -EINVAL; |
| 1411 | usb_ext_prop_put_size(buf, count); |
| 1412 | usb_ext_prop_put_type(buf, ext_prop->type); |
| 1413 | ret = usb_ext_prop_put_name(buf, ext_prop->name, |
| 1414 | ext_prop->name_len); |
| 1415 | if (ret < 0) |
| 1416 | return ret; |
| 1417 | switch (ext_prop->type) { |
| 1418 | case USB_EXT_PROP_UNICODE: |
| 1419 | case USB_EXT_PROP_UNICODE_ENV: |
| 1420 | case USB_EXT_PROP_UNICODE_LINK: |
| 1421 | usb_ext_prop_put_unicode(buf, ret, |
| 1422 | ext_prop->data, |
| 1423 | ext_prop->data_len); |
| 1424 | break; |
| 1425 | case USB_EXT_PROP_BINARY: |
| 1426 | usb_ext_prop_put_binary(buf, ret, |
| 1427 | ext_prop->data, |
| 1428 | ext_prop->data_len); |
| 1429 | break; |
| 1430 | case USB_EXT_PROP_LE32: |
| 1431 | /* not implemented */ |
| 1432 | case USB_EXT_PROP_BE32: |
| 1433 | /* not implemented */ |
| 1434 | default: |
| 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | buf += count; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1444 | /* |
| 1445 | * The setup() callback implements all the ep0 functionality that's |
| 1446 | * not handled lower down, in hardware or the hardware driver(like |
| 1447 | * device and endpoint feature flags, and their status). It's all |
| 1448 | * housekeeping for the gadget function we're implementing. Most of |
| 1449 | * the work is in config and function specific setup. |
| 1450 | */ |
Sebastian Andrzej Siewior | 2d5a889 | 2012-12-23 21:10:21 +0100 | [diff] [blame] | 1451 | int |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1452 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 1453 | { |
| 1454 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1455 | struct usb_request *req = cdev->req; |
| 1456 | int value = -EOPNOTSUPP; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1457 | int status = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1458 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1459 | u8 intf = w_index & 0xFF; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1460 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 1461 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 1462 | struct usb_function *f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1463 | u8 endp; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1464 | |
| 1465 | /* partial re-init of the response message; the function or the |
| 1466 | * gadget might need to intercept e.g. a control-OUT completion |
| 1467 | * when we delegate to it. |
| 1468 | */ |
| 1469 | req->zero = 0; |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 1470 | req->context = cdev; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1471 | req->complete = composite_setup_complete; |
Maulik Mankad | 2edb11c | 2011-02-22 19:08:42 +0530 | [diff] [blame] | 1472 | req->length = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1473 | gadget->ep0->driver_data = cdev; |
| 1474 | |
Andrzej Pietrasiewicz | 232c010 | 2015-03-03 10:52:04 +0100 | [diff] [blame] | 1475 | /* |
| 1476 | * Don't let non-standard requests match any of the cases below |
| 1477 | * by accident. |
| 1478 | */ |
| 1479 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) |
| 1480 | goto unknown; |
| 1481 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1482 | switch (ctrl->bRequest) { |
| 1483 | |
| 1484 | /* we handle all standard USB descriptors */ |
| 1485 | case USB_REQ_GET_DESCRIPTOR: |
| 1486 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1487 | goto unknown; |
| 1488 | switch (w_value >> 8) { |
| 1489 | |
| 1490 | case USB_DT_DEVICE: |
| 1491 | cdev->desc.bNumConfigurations = |
| 1492 | count_configs(cdev, USB_DT_DEVICE); |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1493 | cdev->desc.bMaxPacketSize0 = |
| 1494 | cdev->gadget->ep0->maxpacket; |
| 1495 | if (gadget_is_superspeed(gadget)) { |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1496 | if (gadget->speed >= USB_SPEED_SUPER) { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1497 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1498 | cdev->desc.bMaxPacketSize0 = 9; |
| 1499 | } else { |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1500 | cdev->desc.bcdUSB = cpu_to_le16(0x0210); |
Sebastian Andrzej Siewior | a8f2115 | 2011-07-19 20:21:52 +0200 | [diff] [blame] | 1501 | } |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1502 | } |
| 1503 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1504 | value = min(w_length, (u16) sizeof cdev->desc); |
| 1505 | memcpy(req->buf, &cdev->desc, value); |
| 1506 | break; |
| 1507 | case USB_DT_DEVICE_QUALIFIER: |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1508 | if (!gadget_is_dualspeed(gadget) || |
| 1509 | gadget->speed >= USB_SPEED_SUPER) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1510 | break; |
| 1511 | device_qual(cdev); |
| 1512 | value = min_t(int, w_length, |
| 1513 | sizeof(struct usb_qualifier_descriptor)); |
| 1514 | break; |
| 1515 | case USB_DT_OTHER_SPEED_CONFIG: |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1516 | if (!gadget_is_dualspeed(gadget) || |
| 1517 | gadget->speed >= USB_SPEED_SUPER) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1518 | break; |
| 1519 | /* FALLTHROUGH */ |
| 1520 | case USB_DT_CONFIG: |
| 1521 | value = config_desc(cdev, w_value); |
| 1522 | if (value >= 0) |
| 1523 | value = min(w_length, (u16) value); |
| 1524 | break; |
| 1525 | case USB_DT_STRING: |
| 1526 | value = get_string(cdev, req->buf, |
| 1527 | w_index, w_value & 0xff); |
| 1528 | if (value >= 0) |
| 1529 | value = min(w_length, (u16) value); |
| 1530 | break; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1531 | case USB_DT_BOS: |
| 1532 | if (gadget_is_superspeed(gadget)) { |
| 1533 | value = bos_desc(cdev); |
| 1534 | value = min(w_length, (u16) value); |
| 1535 | } |
| 1536 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1537 | } |
| 1538 | break; |
| 1539 | |
| 1540 | /* any number of configs can work */ |
| 1541 | case USB_REQ_SET_CONFIGURATION: |
| 1542 | if (ctrl->bRequestType != 0) |
| 1543 | goto unknown; |
| 1544 | if (gadget_is_otg(gadget)) { |
| 1545 | if (gadget->a_hnp_support) |
| 1546 | DBG(cdev, "HNP available\n"); |
| 1547 | else if (gadget->a_alt_hnp_support) |
| 1548 | DBG(cdev, "HNP on another port\n"); |
| 1549 | else |
| 1550 | VDBG(cdev, "HNP inactive\n"); |
| 1551 | } |
| 1552 | spin_lock(&cdev->lock); |
| 1553 | value = set_config(cdev, ctrl, w_value); |
| 1554 | spin_unlock(&cdev->lock); |
| 1555 | break; |
| 1556 | case USB_REQ_GET_CONFIGURATION: |
| 1557 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1558 | goto unknown; |
| 1559 | if (cdev->config) |
| 1560 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 1561 | else |
| 1562 | *(u8 *)req->buf = 0; |
| 1563 | value = min(w_length, (u16) 1); |
| 1564 | break; |
| 1565 | |
| 1566 | /* function drivers must handle get/set altsetting; if there's |
| 1567 | * no get() method, we know only altsetting zero works. |
| 1568 | */ |
| 1569 | case USB_REQ_SET_INTERFACE: |
| 1570 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 1571 | goto unknown; |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1572 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1573 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1574 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1575 | if (!f) |
| 1576 | break; |
Bryan Wu | dd4dff8 | 2009-01-08 00:21:18 +0800 | [diff] [blame] | 1577 | if (w_value && !f->set_alt) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1578 | break; |
| 1579 | value = f->set_alt(f, w_index, w_value); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1580 | if (value == USB_GADGET_DELAYED_STATUS) { |
| 1581 | DBG(cdev, |
| 1582 | "%s: interface %d (%s) requested delayed status\n", |
| 1583 | __func__, intf, f->name); |
| 1584 | cdev->delayed_status++; |
| 1585 | DBG(cdev, "delayed_status count %d\n", |
| 1586 | cdev->delayed_status); |
| 1587 | } |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1588 | break; |
| 1589 | case USB_REQ_GET_INTERFACE: |
| 1590 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 1591 | goto unknown; |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1592 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1593 | break; |
Bryan Wu | 0888951 | 2009-01-08 00:21:19 +0800 | [diff] [blame] | 1594 | f = cdev->config->interface[intf]; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1595 | if (!f) |
| 1596 | break; |
| 1597 | /* lots of interfaces only need altsetting zero... */ |
| 1598 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 1599 | if (value < 0) |
| 1600 | break; |
| 1601 | *((u8 *)req->buf) = value; |
| 1602 | value = min(w_length, (u16) 1); |
| 1603 | break; |
Tatyana Brokhman | bdb64d7 | 2011-06-29 16:41:50 +0300 | [diff] [blame] | 1604 | |
| 1605 | /* |
| 1606 | * USB 3.0 additions: |
| 1607 | * Function driver should handle get_status request. If such cb |
| 1608 | * wasn't supplied we respond with default value = 0 |
| 1609 | * Note: function driver should supply such cb only for the first |
| 1610 | * interface of the function |
| 1611 | */ |
| 1612 | case USB_REQ_GET_STATUS: |
| 1613 | if (!gadget_is_superspeed(gadget)) |
| 1614 | goto unknown; |
| 1615 | if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) |
| 1616 | goto unknown; |
| 1617 | value = 2; /* This is the length of the get_status reply */ |
| 1618 | put_unaligned_le16(0, req->buf); |
| 1619 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 1620 | break; |
| 1621 | f = cdev->config->interface[intf]; |
| 1622 | if (!f) |
| 1623 | break; |
| 1624 | status = f->get_status ? f->get_status(f) : 0; |
| 1625 | if (status < 0) |
| 1626 | break; |
| 1627 | put_unaligned_le16(status & 0x0000ffff, req->buf); |
| 1628 | break; |
| 1629 | /* |
| 1630 | * Function drivers should handle SetFeature/ClearFeature |
| 1631 | * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied |
| 1632 | * only for the first interface of the function |
| 1633 | */ |
| 1634 | case USB_REQ_CLEAR_FEATURE: |
| 1635 | case USB_REQ_SET_FEATURE: |
| 1636 | if (!gadget_is_superspeed(gadget)) |
| 1637 | goto unknown; |
| 1638 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) |
| 1639 | goto unknown; |
| 1640 | switch (w_value) { |
| 1641 | case USB_INTRF_FUNC_SUSPEND: |
| 1642 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 1643 | break; |
| 1644 | f = cdev->config->interface[intf]; |
| 1645 | if (!f) |
| 1646 | break; |
| 1647 | value = 0; |
| 1648 | if (f->func_suspend) |
| 1649 | value = f->func_suspend(f, w_index >> 8); |
| 1650 | if (value < 0) { |
| 1651 | ERROR(cdev, |
| 1652 | "func_suspend() returned error %d\n", |
| 1653 | value); |
| 1654 | value = 0; |
| 1655 | } |
| 1656 | break; |
| 1657 | } |
| 1658 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1659 | default: |
| 1660 | unknown: |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1661 | /* |
| 1662 | * OS descriptors handling |
| 1663 | */ |
| 1664 | if (cdev->use_os_string && cdev->os_desc_config && |
Mario Schuknecht | df6738d | 2015-01-26 20:30:27 +0100 | [diff] [blame] | 1665 | (ctrl->bRequestType & USB_TYPE_VENDOR) && |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1666 | ctrl->bRequest == cdev->b_vendor_code) { |
| 1667 | struct usb_request *req; |
| 1668 | struct usb_configuration *os_desc_cfg; |
| 1669 | u8 *buf; |
| 1670 | int interface; |
| 1671 | int count = 0; |
| 1672 | |
| 1673 | req = cdev->os_desc_req; |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 1674 | req->context = cdev; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1675 | req->complete = composite_setup_complete; |
| 1676 | buf = req->buf; |
| 1677 | os_desc_cfg = cdev->os_desc_config; |
| 1678 | memset(buf, 0, w_length); |
| 1679 | buf[5] = 0x01; |
| 1680 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1681 | case USB_RECIP_DEVICE: |
| 1682 | if (w_index != 0x4 || (w_value >> 8)) |
| 1683 | break; |
| 1684 | buf[6] = w_index; |
| 1685 | if (w_length == 0x10) { |
| 1686 | /* Number of ext compat interfaces */ |
| 1687 | count = count_ext_compat(os_desc_cfg); |
| 1688 | buf[8] = count; |
| 1689 | count *= 24; /* 24 B/ext compat desc */ |
| 1690 | count += 16; /* header */ |
| 1691 | put_unaligned_le32(count, buf); |
| 1692 | value = w_length; |
| 1693 | } else { |
| 1694 | /* "extended compatibility ID"s */ |
| 1695 | count = count_ext_compat(os_desc_cfg); |
| 1696 | buf[8] = count; |
| 1697 | count *= 24; /* 24 B/ext compat desc */ |
| 1698 | count += 16; /* header */ |
| 1699 | put_unaligned_le32(count, buf); |
| 1700 | buf += 16; |
| 1701 | fill_ext_compat(os_desc_cfg, buf); |
| 1702 | value = w_length; |
| 1703 | } |
| 1704 | break; |
| 1705 | case USB_RECIP_INTERFACE: |
| 1706 | if (w_index != 0x5 || (w_value >> 8)) |
| 1707 | break; |
| 1708 | interface = w_value & 0xFF; |
| 1709 | buf[6] = w_index; |
| 1710 | if (w_length == 0x0A) { |
| 1711 | count = count_ext_prop(os_desc_cfg, |
| 1712 | interface); |
| 1713 | put_unaligned_le16(count, buf + 8); |
| 1714 | count = len_ext_prop(os_desc_cfg, |
| 1715 | interface); |
| 1716 | put_unaligned_le32(count, buf); |
| 1717 | |
| 1718 | value = w_length; |
| 1719 | } else { |
| 1720 | count = count_ext_prop(os_desc_cfg, |
| 1721 | interface); |
| 1722 | put_unaligned_le16(count, buf + 8); |
| 1723 | count = len_ext_prop(os_desc_cfg, |
| 1724 | interface); |
| 1725 | put_unaligned_le32(count, buf); |
| 1726 | buf += 10; |
| 1727 | value = fill_ext_prop(os_desc_cfg, |
| 1728 | interface, buf); |
| 1729 | if (value < 0) |
| 1730 | return value; |
| 1731 | |
| 1732 | value = w_length; |
| 1733 | } |
| 1734 | break; |
| 1735 | } |
| 1736 | req->length = value; |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 1737 | req->context = cdev; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1738 | req->zero = value < w_length; |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 1739 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1740 | if (value < 0) { |
| 1741 | DBG(cdev, "ep_queue --> %d\n", value); |
| 1742 | req->status = 0; |
| 1743 | composite_setup_complete(gadget->ep0, req); |
| 1744 | } |
| 1745 | return value; |
| 1746 | } |
| 1747 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1748 | VDBG(cdev, |
| 1749 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 1750 | ctrl->bRequestType, ctrl->bRequest, |
| 1751 | w_value, w_index, w_length); |
| 1752 | |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1753 | /* functions always handle their interfaces and endpoints... |
| 1754 | * punt other recipients (other, WUSB, ...) to the current |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1755 | * configuration code. |
| 1756 | * |
| 1757 | * REVISIT it could make sense to let the composite device |
| 1758 | * take such requests too, if that's ever needed: to work |
| 1759 | * in config 0, etc. |
| 1760 | */ |
Andrzej Pietrasiewicz | f563d23 | 2015-03-03 10:52:23 +0100 | [diff] [blame] | 1761 | list_for_each_entry(f, &cdev->config->functions, list) |
| 1762 | if (f->req_match && f->req_match(f, ctrl)) |
| 1763 | goto try_fun_setup; |
| 1764 | f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1765 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1766 | case USB_RECIP_INTERFACE: |
Jassi Brar | ff085de | 2011-02-06 17:39:17 +0900 | [diff] [blame] | 1767 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
Maulik Mankad | 3c47eb0 | 2011-01-13 18:19:56 +0530 | [diff] [blame] | 1768 | break; |
| 1769 | f = cdev->config->interface[intf]; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1770 | break; |
| 1771 | |
| 1772 | case USB_RECIP_ENDPOINT: |
| 1773 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 1774 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1775 | if (test_bit(endp, f->endpoints)) |
| 1776 | break; |
| 1777 | } |
| 1778 | if (&f->list == &cdev->config->functions) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1779 | f = NULL; |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1780 | break; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1781 | } |
Andrzej Pietrasiewicz | f563d23 | 2015-03-03 10:52:23 +0100 | [diff] [blame] | 1782 | try_fun_setup: |
Laurent Pinchart | 5242658 | 2009-10-21 00:03:38 +0200 | [diff] [blame] | 1783 | if (f && f->setup) |
| 1784 | value = f->setup(f, ctrl); |
| 1785 | else { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1786 | struct usb_configuration *c; |
| 1787 | |
| 1788 | c = cdev->config; |
Andrzej Pietrasiewicz | a01091e | 2013-11-07 08:41:25 +0100 | [diff] [blame] | 1789 | if (!c) |
| 1790 | goto done; |
| 1791 | |
| 1792 | /* try current config's setup */ |
| 1793 | if (c->setup) { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1794 | value = c->setup(c, ctrl); |
Andrzej Pietrasiewicz | a01091e | 2013-11-07 08:41:25 +0100 | [diff] [blame] | 1795 | goto done; |
| 1796 | } |
| 1797 | |
| 1798 | /* try the only function in the current config */ |
| 1799 | if (!list_is_singular(&c->functions)) |
| 1800 | goto done; |
| 1801 | f = list_first_entry(&c->functions, struct usb_function, |
| 1802 | list); |
| 1803 | if (f->setup) |
| 1804 | value = f->setup(f, ctrl); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | goto done; |
| 1808 | } |
| 1809 | |
| 1810 | /* respond with data transfer before status phase? */ |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1811 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1812 | req->length = value; |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 1813 | req->context = cdev; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1814 | req->zero = value < w_length; |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 1815 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1816 | if (value < 0) { |
| 1817 | DBG(cdev, "ep_queue --> %d\n", value); |
| 1818 | req->status = 0; |
| 1819 | composite_setup_complete(gadget->ep0, req); |
| 1820 | } |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 1821 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
| 1822 | WARN(cdev, |
| 1823 | "%s: Delayed status not supported for w_length != 0", |
| 1824 | __func__); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | done: |
| 1828 | /* device either stalls (value < 0) or reports success */ |
| 1829 | return value; |
| 1830 | } |
| 1831 | |
Sebastian Andrzej Siewior | 2d5a889 | 2012-12-23 21:10:21 +0100 | [diff] [blame] | 1832 | void composite_disconnect(struct usb_gadget *gadget) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1833 | { |
| 1834 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1835 | unsigned long flags; |
| 1836 | |
| 1837 | /* REVISIT: should we have config and device level |
| 1838 | * disconnect callbacks? |
| 1839 | */ |
| 1840 | spin_lock_irqsave(&cdev->lock, flags); |
| 1841 | if (cdev->config) |
| 1842 | reset_config(cdev); |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 1843 | if (cdev->driver->disconnect) |
| 1844 | cdev->driver->disconnect(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1845 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1846 | } |
| 1847 | |
| 1848 | /*-------------------------------------------------------------------------*/ |
| 1849 | |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 1850 | static ssize_t suspended_show(struct device *dev, struct device_attribute *attr, |
| 1851 | char *buf) |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1852 | { |
| 1853 | struct usb_gadget *gadget = dev_to_usb_gadget(dev); |
| 1854 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1855 | |
| 1856 | return sprintf(buf, "%d\n", cdev->suspended); |
| 1857 | } |
Greg Kroah-Hartman | ce26bd2 | 2013-08-23 16:34:43 -0700 | [diff] [blame] | 1858 | static DEVICE_ATTR_RO(suspended); |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 1859 | |
Sebastian Andrzej Siewior | 779d516 | 2012-12-23 21:09:55 +0100 | [diff] [blame] | 1860 | static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1861 | { |
| 1862 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1863 | |
| 1864 | /* composite_disconnect() must already have been called |
| 1865 | * by the underlying peripheral controller driver! |
| 1866 | * so there's no i/o concurrency that could affect the |
| 1867 | * state protected by cdev->lock. |
| 1868 | */ |
| 1869 | WARN_ON(cdev->config); |
| 1870 | |
| 1871 | while (!list_empty(&cdev->configs)) { |
| 1872 | struct usb_configuration *c; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1873 | c = list_first_entry(&cdev->configs, |
| 1874 | struct usb_configuration, list); |
Benoit Goby | 51cce6f | 2012-05-10 10:07:57 +0200 | [diff] [blame] | 1875 | remove_config(cdev, c); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1876 | } |
Sebastian Andrzej Siewior | 779d516 | 2012-12-23 21:09:55 +0100 | [diff] [blame] | 1877 | if (cdev->driver->unbind && unbind_driver) |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 1878 | cdev->driver->unbind(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1879 | |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 1880 | composite_dev_cleanup(cdev); |
| 1881 | |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 1882 | kfree(cdev->def_manufacturer); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1883 | kfree(cdev); |
| 1884 | set_gadget_data(gadget, NULL); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Sebastian Andrzej Siewior | 779d516 | 2012-12-23 21:09:55 +0100 | [diff] [blame] | 1887 | static void composite_unbind(struct usb_gadget *gadget) |
| 1888 | { |
| 1889 | __composite_unbind(gadget, true); |
| 1890 | } |
| 1891 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 1892 | static void update_unchanged_dev_desc(struct usb_device_descriptor *new, |
| 1893 | const struct usb_device_descriptor *old) |
| 1894 | { |
| 1895 | __le16 idVendor; |
| 1896 | __le16 idProduct; |
| 1897 | __le16 bcdDevice; |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 1898 | u8 iSerialNumber; |
Sebastian Andrzej Siewior | 03de9bf | 2012-09-10 15:01:55 +0200 | [diff] [blame] | 1899 | u8 iManufacturer; |
Sebastian Andrzej Siewior | 2d35ee4 | 2012-09-10 15:01:56 +0200 | [diff] [blame] | 1900 | u8 iProduct; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 1901 | |
| 1902 | /* |
| 1903 | * these variables may have been set in |
| 1904 | * usb_composite_overwrite_options() |
| 1905 | */ |
| 1906 | idVendor = new->idVendor; |
| 1907 | idProduct = new->idProduct; |
| 1908 | bcdDevice = new->bcdDevice; |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 1909 | iSerialNumber = new->iSerialNumber; |
Sebastian Andrzej Siewior | 03de9bf | 2012-09-10 15:01:55 +0200 | [diff] [blame] | 1910 | iManufacturer = new->iManufacturer; |
Sebastian Andrzej Siewior | 2d35ee4 | 2012-09-10 15:01:56 +0200 | [diff] [blame] | 1911 | iProduct = new->iProduct; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 1912 | |
| 1913 | *new = *old; |
| 1914 | if (idVendor) |
| 1915 | new->idVendor = idVendor; |
| 1916 | if (idProduct) |
| 1917 | new->idProduct = idProduct; |
| 1918 | if (bcdDevice) |
| 1919 | new->bcdDevice = bcdDevice; |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 1920 | else |
| 1921 | new->bcdDevice = cpu_to_le16(get_default_bcdDevice()); |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 1922 | if (iSerialNumber) |
| 1923 | new->iSerialNumber = iSerialNumber; |
Sebastian Andrzej Siewior | 03de9bf | 2012-09-10 15:01:55 +0200 | [diff] [blame] | 1924 | if (iManufacturer) |
| 1925 | new->iManufacturer = iManufacturer; |
Sebastian Andrzej Siewior | 2d35ee4 | 2012-09-10 15:01:56 +0200 | [diff] [blame] | 1926 | if (iProduct) |
| 1927 | new->iProduct = iProduct; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 1928 | } |
| 1929 | |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 1930 | int composite_dev_prepare(struct usb_composite_driver *composite, |
| 1931 | struct usb_composite_dev *cdev) |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 1932 | { |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 1933 | struct usb_gadget *gadget = cdev->gadget; |
| 1934 | int ret = -ENOMEM; |
| 1935 | |
| 1936 | /* preallocate control response and buffer */ |
| 1937 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 1938 | if (!cdev->req) |
| 1939 | return -ENOMEM; |
| 1940 | |
| 1941 | cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL); |
| 1942 | if (!cdev->req->buf) |
| 1943 | goto fail; |
| 1944 | |
| 1945 | ret = device_create_file(&gadget->dev, &dev_attr_suspended); |
| 1946 | if (ret) |
| 1947 | goto fail_dev; |
| 1948 | |
| 1949 | cdev->req->complete = composite_setup_complete; |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 1950 | cdev->req->context = cdev; |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 1951 | gadget->ep0->driver_data = cdev; |
| 1952 | |
| 1953 | cdev->driver = composite; |
| 1954 | |
| 1955 | /* |
| 1956 | * As per USB compliance update, a device that is actively drawing |
| 1957 | * more than 100mA from USB must report itself as bus-powered in |
| 1958 | * the GetStatus(DEVICE) call. |
| 1959 | */ |
| 1960 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 1961 | usb_gadget_set_selfpowered(gadget); |
| 1962 | |
| 1963 | /* interface and string IDs start at zero via kzalloc. |
| 1964 | * we force endpoints to start unassigned; few controller |
| 1965 | * drivers will zero ep->driver_data. |
| 1966 | */ |
| 1967 | usb_ep_autoconfig_reset(gadget); |
| 1968 | return 0; |
| 1969 | fail_dev: |
| 1970 | kfree(cdev->req->buf); |
| 1971 | fail: |
| 1972 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 1973 | cdev->req = NULL; |
| 1974 | return ret; |
| 1975 | } |
| 1976 | |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1977 | int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, |
| 1978 | struct usb_ep *ep0) |
| 1979 | { |
| 1980 | int ret = 0; |
| 1981 | |
| 1982 | cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL); |
| 1983 | if (!cdev->os_desc_req) { |
| 1984 | ret = PTR_ERR(cdev->os_desc_req); |
| 1985 | goto end; |
| 1986 | } |
| 1987 | |
| 1988 | /* OS feature descriptor length <= 4kB */ |
| 1989 | cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL); |
| 1990 | if (!cdev->os_desc_req->buf) { |
| 1991 | ret = PTR_ERR(cdev->os_desc_req->buf); |
| 1992 | kfree(cdev->os_desc_req); |
| 1993 | goto end; |
| 1994 | } |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 1995 | cdev->os_desc_req->context = cdev; |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 1996 | cdev->os_desc_req->complete = composite_setup_complete; |
| 1997 | end: |
| 1998 | return ret; |
| 1999 | } |
| 2000 | |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 2001 | void composite_dev_cleanup(struct usb_composite_dev *cdev) |
| 2002 | { |
Sebastian Andrzej Siewior | 27a46633 | 2012-12-23 21:10:23 +0100 | [diff] [blame] | 2003 | struct usb_gadget_string_container *uc, *tmp; |
| 2004 | |
| 2005 | list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) { |
| 2006 | list_del(&uc->list); |
| 2007 | kfree(uc); |
| 2008 | } |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 2009 | if (cdev->os_desc_req) { |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 2010 | if (cdev->os_desc_pending) |
| 2011 | usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req); |
| 2012 | |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 2013 | kfree(cdev->os_desc_req->buf); |
| 2014 | usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req); |
| 2015 | } |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 2016 | if (cdev->req) { |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 2017 | if (cdev->setup_pending) |
| 2018 | usb_ep_dequeue(cdev->gadget->ep0, cdev->req); |
| 2019 | |
Li Jun | be0a888 | 2014-08-28 21:44:11 +0800 | [diff] [blame] | 2020 | kfree(cdev->req->buf); |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 2021 | usb_ep_free_request(cdev->gadget->ep0, cdev->req); |
| 2022 | } |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 2023 | cdev->next_string_id = 0; |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 2024 | device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | static int composite_bind(struct usb_gadget *gadget, |
| 2028 | struct usb_gadget_driver *gdriver) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2029 | { |
| 2030 | struct usb_composite_dev *cdev; |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2031 | struct usb_composite_driver *composite = to_cdriver(gdriver); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2032 | int status = -ENOMEM; |
| 2033 | |
| 2034 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); |
| 2035 | if (!cdev) |
| 2036 | return status; |
| 2037 | |
| 2038 | spin_lock_init(&cdev->lock); |
| 2039 | cdev->gadget = gadget; |
| 2040 | set_gadget_data(gadget, cdev); |
| 2041 | INIT_LIST_HEAD(&cdev->configs); |
Sebastian Andrzej Siewior | 9bb2859 | 2012-12-23 21:10:22 +0100 | [diff] [blame] | 2042 | INIT_LIST_HEAD(&cdev->gstrings); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2043 | |
Sebastian Andrzej Siewior | a592334 | 2012-12-23 21:10:20 +0100 | [diff] [blame] | 2044 | status = composite_dev_prepare(composite, cdev); |
| 2045 | if (status) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2046 | goto fail; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2047 | |
| 2048 | /* composite gadget needs to assign strings for whole device (like |
| 2049 | * serial number), register function drivers, potentially update |
| 2050 | * power state and consumption, etc |
| 2051 | */ |
Sebastian Andrzej Siewior | fac3a43 | 2012-09-06 20:11:01 +0200 | [diff] [blame] | 2052 | status = composite->bind(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2053 | if (status < 0) |
| 2054 | goto fail; |
| 2055 | |
Andrzej Pietrasiewicz | 37a3a53 | 2014-05-08 14:06:23 +0200 | [diff] [blame] | 2056 | if (cdev->use_os_string) { |
| 2057 | status = composite_os_desc_req_prepare(cdev, gadget->ep0); |
| 2058 | if (status) |
| 2059 | goto fail; |
| 2060 | } |
| 2061 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 2062 | update_unchanged_dev_desc(&cdev->desc, composite->dev); |
Greg Kroah-Hartman | dbb442b | 2010-12-16 15:52:30 -0800 | [diff] [blame] | 2063 | |
Michal Nazarewicz | ad1a810 | 2010-08-12 17:43:46 +0200 | [diff] [blame] | 2064 | /* has userspace failed to provide a serial number? */ |
| 2065 | if (composite->needs_serial && !cdev->desc.iSerialNumber) |
| 2066 | WARNING(cdev, "userspace failed to provide iSerialNumber\n"); |
| 2067 | |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2068 | INFO(cdev, "%s ready\n", composite->name); |
| 2069 | return 0; |
| 2070 | |
| 2071 | fail: |
Sebastian Andrzej Siewior | 779d516 | 2012-12-23 21:09:55 +0100 | [diff] [blame] | 2072 | __composite_unbind(gadget, false); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2073 | return status; |
| 2074 | } |
| 2075 | |
| 2076 | /*-------------------------------------------------------------------------*/ |
| 2077 | |
Andrzej Pietrasiewicz | 3a57187 | 2014-10-08 12:03:36 +0200 | [diff] [blame] | 2078 | void composite_suspend(struct usb_gadget *gadget) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2079 | { |
| 2080 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2081 | struct usb_function *f; |
| 2082 | |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 2083 | /* REVISIT: should we have config level |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2084 | * suspend/resume callbacks? |
| 2085 | */ |
| 2086 | DBG(cdev, "suspend\n"); |
| 2087 | if (cdev->config) { |
| 2088 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 2089 | if (f->suspend) |
| 2090 | f->suspend(f); |
| 2091 | } |
| 2092 | } |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2093 | if (cdev->driver->suspend) |
| 2094 | cdev->driver->suspend(cdev); |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 2095 | |
| 2096 | cdev->suspended = 1; |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 2097 | |
| 2098 | usb_gadget_vbus_draw(gadget, 2); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Andrzej Pietrasiewicz | 3a57187 | 2014-10-08 12:03:36 +0200 | [diff] [blame] | 2101 | void composite_resume(struct usb_gadget *gadget) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2102 | { |
| 2103 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2104 | struct usb_function *f; |
Du, ChangbinX | 56b1b90 | 2013-12-17 11:47:42 +0000 | [diff] [blame] | 2105 | u16 maxpower; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2106 | |
David Brownell | 8942939 | 2009-03-19 14:14:17 -0700 | [diff] [blame] | 2107 | /* REVISIT: should we have config level |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2108 | * suspend/resume callbacks? |
| 2109 | */ |
| 2110 | DBG(cdev, "resume\n"); |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2111 | if (cdev->driver->resume) |
| 2112 | cdev->driver->resume(cdev); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2113 | if (cdev->config) { |
| 2114 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 2115 | if (f->resume) |
| 2116 | f->resume(f); |
| 2117 | } |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 2118 | |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 2119 | maxpower = cdev->config->MaxPower; |
Hao Wu | b23f2f9 | 2010-11-29 15:17:03 +0800 | [diff] [blame] | 2120 | |
| 2121 | usb_gadget_vbus_draw(gadget, maxpower ? |
Sebastian Andrzej Siewior | 8f900a9 | 2012-12-03 20:07:05 +0100 | [diff] [blame] | 2122 | maxpower : CONFIG_USB_GADGET_VBUS_DRAW); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2123 | } |
Fabien Chouteau | f48cf80 | 2010-04-23 14:21:26 +0200 | [diff] [blame] | 2124 | |
| 2125 | cdev->suspended = 0; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | /*-------------------------------------------------------------------------*/ |
| 2129 | |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2130 | static const struct usb_gadget_driver composite_driver_template = { |
Sebastian Andrzej Siewior | 9395295 | 2012-09-06 20:11:05 +0200 | [diff] [blame] | 2131 | .bind = composite_bind, |
Michal Nazarewicz | 915c8be | 2009-11-09 14:15:25 +0100 | [diff] [blame] | 2132 | .unbind = composite_unbind, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2133 | |
| 2134 | .setup = composite_setup, |
Peter Chen | d8a816f | 2014-09-09 08:56:49 +0800 | [diff] [blame] | 2135 | .reset = composite_disconnect, |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2136 | .disconnect = composite_disconnect, |
| 2137 | |
| 2138 | .suspend = composite_suspend, |
| 2139 | .resume = composite_resume, |
| 2140 | |
| 2141 | .driver = { |
| 2142 | .owner = THIS_MODULE, |
| 2143 | }, |
| 2144 | }; |
| 2145 | |
| 2146 | /** |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 2147 | * usb_composite_probe() - register a composite driver |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2148 | * @driver: the driver to register |
Nishanth Menon | 43febb2 | 2013-03-04 16:52:38 -0600 | [diff] [blame] | 2149 | * |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2150 | * Context: single threaded during gadget setup |
| 2151 | * |
| 2152 | * This function is used to register drivers using the composite driver |
| 2153 | * framework. The return value is zero, or a negative errno value. |
| 2154 | * Those values normally come from the driver's @bind method, which does |
| 2155 | * all the work of setting up the driver to match the hardware. |
| 2156 | * |
| 2157 | * On successful return, the gadget is ready to respond to requests from |
| 2158 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 2159 | * while it was binding. That would usually be done in order to wait for |
| 2160 | * some userspace participation. |
| 2161 | */ |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 2162 | int usb_composite_probe(struct usb_composite_driver *driver) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2163 | { |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2164 | struct usb_gadget_driver *gadget_driver; |
| 2165 | |
| 2166 | if (!driver || !driver->dev || !driver->bind) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2167 | return -EINVAL; |
| 2168 | |
| 2169 | if (!driver->name) |
| 2170 | driver->name = "composite"; |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2171 | |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2172 | driver->gadget_driver = composite_driver_template; |
| 2173 | gadget_driver = &driver->gadget_driver; |
| 2174 | |
| 2175 | gadget_driver->function = (char *) driver->name; |
| 2176 | gadget_driver->driver.name = driver->name; |
| 2177 | gadget_driver->max_speed = driver->max_speed; |
| 2178 | |
| 2179 | return usb_gadget_probe_driver(gadget_driver); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2180 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 2181 | EXPORT_SYMBOL_GPL(usb_composite_probe); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2182 | |
| 2183 | /** |
| 2184 | * usb_composite_unregister() - unregister a composite driver |
| 2185 | * @driver: the driver to unregister |
| 2186 | * |
| 2187 | * This function is used to unregister drivers using the composite |
| 2188 | * driver framework. |
| 2189 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 2190 | void usb_composite_unregister(struct usb_composite_driver *driver) |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2191 | { |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 2192 | usb_gadget_unregister_driver(&driver->gadget_driver); |
David Brownell | 40982be | 2008-06-19 17:52:58 -0700 | [diff] [blame] | 2193 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 2194 | EXPORT_SYMBOL_GPL(usb_composite_unregister); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 2195 | |
| 2196 | /** |
| 2197 | * usb_composite_setup_continue() - Continue with the control transfer |
| 2198 | * @cdev: the composite device who's control transfer was kept waiting |
| 2199 | * |
| 2200 | * This function must be called by the USB function driver to continue |
| 2201 | * with the control transfer's data/status stage in case it had requested to |
| 2202 | * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) |
| 2203 | * can request the composite framework to delay the setup request's data/status |
| 2204 | * stages by returning USB_GADGET_DELAYED_STATUS. |
| 2205 | */ |
| 2206 | void usb_composite_setup_continue(struct usb_composite_dev *cdev) |
| 2207 | { |
| 2208 | int value; |
| 2209 | struct usb_request *req = cdev->req; |
| 2210 | unsigned long flags; |
| 2211 | |
| 2212 | DBG(cdev, "%s\n", __func__); |
| 2213 | spin_lock_irqsave(&cdev->lock, flags); |
| 2214 | |
| 2215 | if (cdev->delayed_status == 0) { |
| 2216 | WARN(cdev, "%s: Unexpected call\n", __func__); |
| 2217 | |
| 2218 | } else if (--cdev->delayed_status == 0) { |
| 2219 | DBG(cdev, "%s: Completing delayed status\n", __func__); |
| 2220 | req->length = 0; |
Felipe Balbi | 5794371 | 2014-09-18 09:54:54 -0500 | [diff] [blame] | 2221 | req->context = cdev; |
Felipe Balbi | a7c12ea | 2014-09-18 10:01:55 -0500 | [diff] [blame] | 2222 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 2223 | if (value < 0) { |
| 2224 | DBG(cdev, "ep_queue --> %d\n", value); |
| 2225 | req->status = 0; |
| 2226 | composite_setup_complete(cdev->gadget->ep0, req); |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 2231 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 2232 | EXPORT_SYMBOL_GPL(usb_composite_setup_continue); |
Roger Quadros | 1b9ba00 | 2011-05-09 13:08:06 +0300 | [diff] [blame] | 2233 | |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 2234 | static char *composite_default_mfr(struct usb_gadget *gadget) |
| 2235 | { |
| 2236 | char *mfr; |
| 2237 | int len; |
| 2238 | |
| 2239 | len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname, |
| 2240 | init_utsname()->release, gadget->name); |
| 2241 | len++; |
| 2242 | mfr = kmalloc(len, GFP_KERNEL); |
| 2243 | if (!mfr) |
| 2244 | return NULL; |
| 2245 | snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname, |
| 2246 | init_utsname()->release, gadget->name); |
| 2247 | return mfr; |
| 2248 | } |
| 2249 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 2250 | void usb_composite_overwrite_options(struct usb_composite_dev *cdev, |
| 2251 | struct usb_composite_overwrite *covr) |
| 2252 | { |
| 2253 | struct usb_device_descriptor *desc = &cdev->desc; |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 2254 | struct usb_gadget_strings *gstr = cdev->driver->strings[0]; |
| 2255 | struct usb_string *dev_str = gstr->strings; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 2256 | |
| 2257 | if (covr->idVendor) |
| 2258 | desc->idVendor = cpu_to_le16(covr->idVendor); |
| 2259 | |
| 2260 | if (covr->idProduct) |
| 2261 | desc->idProduct = cpu_to_le16(covr->idProduct); |
| 2262 | |
| 2263 | if (covr->bcdDevice) |
| 2264 | desc->bcdDevice = cpu_to_le16(covr->bcdDevice); |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 2265 | |
| 2266 | if (covr->serial_number) { |
| 2267 | desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id; |
| 2268 | dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number; |
| 2269 | } |
Sebastian Andrzej Siewior | 03de9bf | 2012-09-10 15:01:55 +0200 | [diff] [blame] | 2270 | if (covr->manufacturer) { |
| 2271 | desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; |
| 2272 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer; |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 2273 | |
| 2274 | } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) { |
| 2275 | desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; |
| 2276 | cdev->def_manufacturer = composite_default_mfr(cdev->gadget); |
| 2277 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer; |
Sebastian Andrzej Siewior | 03de9bf | 2012-09-10 15:01:55 +0200 | [diff] [blame] | 2278 | } |
Sebastian Andrzej Siewior | 2d35ee4 | 2012-09-10 15:01:56 +0200 | [diff] [blame] | 2279 | |
| 2280 | if (covr->product) { |
| 2281 | desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id; |
| 2282 | dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product; |
| 2283 | } |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 2284 | } |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 2285 | EXPORT_SYMBOL_GPL(usb_composite_overwrite_options); |
Sebastian Andrzej Siewior | d80c304 | 2012-09-06 20:11:28 +0200 | [diff] [blame] | 2286 | |
| 2287 | MODULE_LICENSE("GPL"); |
| 2288 | MODULE_AUTHOR("David Brownell"); |