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