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