| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * composite.c - infrastructure for Composite USB Gadgets |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 David Brownell |
| 6 | */ |
| 7 | |
| 8 | /* #define VERBOSE_DEBUG */ |
| 9 | |
| 10 | #include <linux/kallsyms.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/utsname.h> |
| 16 | |
| 17 | #include <linux/usb/composite.h> |
| 18 | #include <linux/usb/otg.h> |
| 19 | #include <asm/unaligned.h> |
| 20 | |
| 21 | #include "u_os_desc.h" |
| 22 | |
| 23 | /** |
| 24 | * struct usb_os_string - represents OS String to be reported by a gadget |
| 25 | * @bLength: total length of the entire descritor, always 0x12 |
| 26 | * @bDescriptorType: USB_DT_STRING |
| 27 | * @qwSignature: the OS String proper |
| 28 | * @bMS_VendorCode: code used by the host for subsequent requests |
| 29 | * @bPad: not used, must be zero |
| 30 | */ |
| 31 | struct usb_os_string { |
| 32 | __u8 bLength; |
| 33 | __u8 bDescriptorType; |
| 34 | __u8 qwSignature[OS_STRING_QW_SIGN_LEN]; |
| 35 | __u8 bMS_VendorCode; |
| 36 | __u8 bPad; |
| 37 | } __packed; |
| 38 | |
| 39 | /* |
| 40 | * The code in this file is utility code, used to build a gadget driver |
| 41 | * from one or more "function" drivers, one or more "configuration" |
| 42 | * objects, and a "usb_composite_driver" by gluing them together along |
| 43 | * with the relevant device-wide data. |
| 44 | */ |
| 45 | |
| 46 | static struct usb_gadget_strings **get_containers_gs( |
| 47 | struct usb_gadget_string_container *uc) |
| 48 | { |
| 49 | return (struct usb_gadget_strings **)uc->stash; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * function_descriptors() - get function descriptors for speed |
| 54 | * @f: the function |
| 55 | * @speed: the speed |
| 56 | * |
| 57 | * Returns the descriptors or NULL if not set. |
| 58 | */ |
| 59 | static struct usb_descriptor_header ** |
| 60 | function_descriptors(struct usb_function *f, |
| 61 | enum usb_device_speed speed) |
| 62 | { |
| 63 | struct usb_descriptor_header **descriptors; |
| 64 | |
| 65 | /* |
| 66 | * NOTE: we try to help gadget drivers which might not be setting |
| 67 | * max_speed appropriately. |
| 68 | */ |
| 69 | |
| 70 | switch (speed) { |
| 71 | case USB_SPEED_SUPER_PLUS: |
| 72 | descriptors = f->ssp_descriptors; |
| 73 | if (descriptors) |
| 74 | break; |
| 75 | /* FALLTHROUGH */ |
| 76 | case USB_SPEED_SUPER: |
| 77 | descriptors = f->ss_descriptors; |
| 78 | if (descriptors) |
| 79 | break; |
| 80 | /* FALLTHROUGH */ |
| 81 | case USB_SPEED_HIGH: |
| 82 | descriptors = f->hs_descriptors; |
| 83 | if (descriptors) |
| 84 | break; |
| 85 | /* FALLTHROUGH */ |
| 86 | default: |
| 87 | descriptors = f->fs_descriptors; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * if we can't find any descriptors at all, then this gadget deserves to |
| 92 | * Oops with a NULL pointer dereference |
| 93 | */ |
| 94 | |
| 95 | return descriptors; |
| 96 | } |
| 97 | |
| 98 | #if 0 |
| 99 | static struct usb_descriptor_header** |
| 100 | next_ep_desc(struct usb_descriptor_header **t) |
| 101 | { |
| 102 | for (; *t; t++) { |
| 103 | if ((*t)->bDescriptorType == USB_DT_ENDPOINT) |
| 104 | return t; |
| 105 | } |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * for_each_ep_desc()- iterate over endpoint descriptors in the |
| 111 | * descriptors list |
| 112 | * @start: pointer within descriptor array. |
| 113 | * @ep_desc: endpoint descriptor to use as the loop cursor |
| 114 | */ |
| 115 | #define for_each_ep_desc(start, ep_desc) \ |
| 116 | for (ep_desc = next_ep_desc(start); \ |
| 117 | ep_desc; ep_desc = next_ep_desc(ep_desc+1)) |
| 118 | #endif |
| 119 | |
| 120 | /** |
| 121 | * next_desc() - advance to the next desc_type descriptor |
| 122 | * @t: currect pointer within descriptor array |
| 123 | * @desc_type: descriptor type |
| 124 | * |
| 125 | * Return: next desc_type descriptor or NULL |
| 126 | * |
| 127 | * Iterate over @t until either desc_type descriptor found or |
| 128 | * NULL (that indicates end of list) encountered |
| 129 | */ |
| 130 | static struct usb_descriptor_header** |
| 131 | next_desc(struct usb_descriptor_header **t, u8 desc_type) |
| 132 | { |
| 133 | for (; *t; t++) { |
| 134 | if ((*t)->bDescriptorType == desc_type) |
| 135 | return t; |
| 136 | } |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * for_each_desc() - iterate over desc_type descriptors in the |
| 142 | * descriptors list |
| 143 | * @start: pointer within descriptor array. |
| 144 | * @iter_desc: desc_type descriptor to use as the loop cursor |
| 145 | * @desc_type: wanted descriptr type |
| 146 | */ |
| 147 | #define for_each_desc(start, iter_desc, desc_type) \ |
| 148 | for (iter_desc = next_desc(start, desc_type); \ |
| 149 | iter_desc; iter_desc = next_desc(iter_desc + 1, desc_type)) |
| 150 | |
| 151 | /** |
| 152 | * config_ep_by_speed_and_alt() - configures the given endpoint |
| 153 | * according to gadget speed. |
| 154 | * @g: pointer to the gadget |
| 155 | * @f: usb function |
| 156 | * @_ep: the endpoint to configure |
| 157 | * @alt: alternate setting number |
| 158 | * |
| 159 | * Return: error code, 0 on success |
| 160 | * |
| 161 | * This function chooses the right descriptors for a given |
| 162 | * endpoint according to gadget speed and saves it in the |
| 163 | * endpoint desc field. If the endpoint already has a descriptor |
| 164 | * assigned to it - overwrites it with currently corresponding |
| 165 | * descriptor. The endpoint maxpacket field is updated according |
| 166 | * to the chosen descriptor. |
| 167 | * Note: the supplied function should hold all the descriptors |
| 168 | * for supported speeds |
| 169 | */ |
| 170 | int config_ep_by_speed_and_alt(struct usb_gadget *g, |
| 171 | struct usb_function *f, |
| 172 | struct usb_ep *_ep, |
| 173 | u8 alt) |
| 174 | { |
| 175 | struct usb_endpoint_descriptor *chosen_desc = NULL; |
| 176 | struct usb_interface_descriptor *int_desc = NULL; |
| 177 | struct usb_descriptor_header **speed_desc = NULL; |
| 178 | |
| 179 | struct usb_ss_ep_comp_descriptor *comp_desc = NULL; |
| 180 | int want_comp_desc = 0; |
| 181 | |
| 182 | struct usb_descriptor_header **d_spd; /* cursor for speed desc */ |
| 183 | |
| 184 | if (!g || !f || !_ep) |
| 185 | return -EIO; |
| 186 | |
| 187 | /* select desired speed */ |
| 188 | switch (g->speed) { |
| 189 | case USB_SPEED_SUPER_PLUS: |
| 190 | if (gadget_is_superspeed_plus(g)) { |
| 191 | speed_desc = f->ssp_descriptors; |
| 192 | want_comp_desc = 1; |
| 193 | break; |
| 194 | } |
| 195 | /* fall through */ |
| 196 | case USB_SPEED_SUPER: |
| 197 | if (gadget_is_superspeed(g)) { |
| 198 | speed_desc = f->ss_descriptors; |
| 199 | want_comp_desc = 1; |
| 200 | break; |
| 201 | } |
| 202 | /* fall through */ |
| 203 | case USB_SPEED_HIGH: |
| 204 | if (gadget_is_dualspeed(g)) { |
| 205 | speed_desc = f->hs_descriptors; |
| 206 | break; |
| 207 | } |
| 208 | /* fall through */ |
| 209 | default: |
| 210 | speed_desc = f->fs_descriptors; |
| 211 | } |
| 212 | |
| 213 | /* find correct alternate setting descriptor */ |
| 214 | for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) { |
| 215 | int_desc = (struct usb_interface_descriptor *)*d_spd; |
| 216 | |
| 217 | if (int_desc->bAlternateSetting == alt) { |
| 218 | speed_desc = d_spd; |
| 219 | goto intf_found; |
| 220 | } |
| 221 | } |
| 222 | return -EIO; |
| 223 | |
| 224 | intf_found: |
| 225 | /* find descriptors */ |
| 226 | for_each_desc(speed_desc, d_spd, USB_DT_ENDPOINT) { |
| 227 | chosen_desc = (struct usb_endpoint_descriptor *)*d_spd; |
| 228 | if (chosen_desc->bEndpointAddress == _ep->address) |
| 229 | goto ep_found; |
| 230 | } |
| 231 | return -EIO; |
| 232 | |
| 233 | ep_found: |
| 234 | /* commit results */ |
| 235 | _ep->maxpacket = usb_endpoint_maxp(chosen_desc); |
| 236 | _ep->desc = chosen_desc; |
| 237 | _ep->comp_desc = NULL; |
| 238 | _ep->maxburst = 0; |
| 239 | _ep->mult = 1; |
| 240 | |
| 241 | if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) || |
| 242 | usb_endpoint_xfer_int(_ep->desc))) |
| 243 | _ep->mult = usb_endpoint_maxp_mult(_ep->desc); |
| 244 | |
| 245 | if (!want_comp_desc) |
| 246 | return 0; |
| 247 | |
| 248 | /* |
| 249 | * Companion descriptor should follow EP descriptor |
| 250 | * USB 3.0 spec, #9.6.7 |
| 251 | */ |
| 252 | comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd); |
| 253 | if (!comp_desc || |
| 254 | (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP)) |
| 255 | return -EIO; |
| 256 | _ep->comp_desc = comp_desc; |
| 257 | if (g->speed >= USB_SPEED_SUPER) { |
| 258 | switch (usb_endpoint_type(_ep->desc)) { |
| 259 | case USB_ENDPOINT_XFER_ISOC: |
| 260 | /* mult: bits 1:0 of bmAttributes */ |
| 261 | _ep->mult = (comp_desc->bmAttributes & 0x3) + 1; |
| 262 | /* fall through */ |
| 263 | case USB_ENDPOINT_XFER_BULK: |
| 264 | case USB_ENDPOINT_XFER_INT: |
| 265 | _ep->maxburst = comp_desc->bMaxBurst + 1; |
| 266 | break; |
| 267 | default: |
| 268 | if (comp_desc->bMaxBurst != 0) { |
| 269 | struct usb_composite_dev *cdev; |
| 270 | |
| 271 | cdev = get_gadget_data(g); |
| 272 | ERROR(cdev, "ep0 bMaxBurst must be 0\n"); |
| 273 | } |
| 274 | _ep->maxburst = 1; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(config_ep_by_speed_and_alt); |
| 281 | |
| 282 | /** |
| 283 | * config_ep_by_speed() - configures the given endpoint |
| 284 | * according to gadget speed. |
| 285 | * @g: pointer to the gadget |
| 286 | * @f: usb function |
| 287 | * @_ep: the endpoint to configure |
| 288 | * |
| 289 | * Return: error code, 0 on success |
| 290 | * |
| 291 | * This function chooses the right descriptors for a given |
| 292 | * endpoint according to gadget speed and saves it in the |
| 293 | * endpoint desc field. If the endpoint already has a descriptor |
| 294 | * assigned to it - overwrites it with currently corresponding |
| 295 | * descriptor. The endpoint maxpacket field is updated according |
| 296 | * to the chosen descriptor. |
| 297 | * Note: the supplied function should hold all the descriptors |
| 298 | * for supported speeds |
| 299 | */ |
| 300 | int config_ep_by_speed(struct usb_gadget *g, |
| 301 | struct usb_function *f, |
| 302 | struct usb_ep *_ep) |
| 303 | { |
| 304 | return config_ep_by_speed_and_alt(g, f, _ep, 0); |
| 305 | } |
| 306 | EXPORT_SYMBOL_GPL(config_ep_by_speed); |
| 307 | |
| 308 | /** |
| 309 | * usb_add_function() - add a function to a configuration |
| 310 | * @config: the configuration |
| 311 | * @function: the function being added |
| 312 | * Context: single threaded during gadget setup |
| 313 | * |
| 314 | * After initialization, each configuration must have one or more |
| 315 | * functions added to it. Adding a function involves calling its @bind() |
| 316 | * method to allocate resources such as interface and string identifiers |
| 317 | * and endpoints. |
| 318 | * |
| 319 | * This function returns the value of the function's bind(), which is |
| 320 | * zero for success else a negative errno value. |
| 321 | */ |
| 322 | int usb_add_function(struct usb_configuration *config, |
| 323 | struct usb_function *function) |
| 324 | { |
| 325 | int value = -EINVAL; |
| 326 | |
| 327 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", |
| 328 | function->name, function, |
| 329 | config->label, config); |
| 330 | |
| 331 | if (!function->set_alt || !function->disable) |
| 332 | goto done; |
| 333 | |
| 334 | function->config = config; |
| 335 | list_add_tail(&function->list, &config->functions); |
| 336 | |
| 337 | if (function->bind_deactivated) { |
| 338 | value = usb_function_deactivate(function); |
| 339 | if (value) { |
| 340 | ERROR(config->cdev, "usb_function_deactivate failed %d\n", value); |
| 341 | goto done; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* REVISIT *require* function->bind? */ |
| 346 | if (function->bind) { |
| 347 | value = function->bind(config, function); |
| 348 | if (value < 0) { |
| 349 | ERROR(config->cdev, "func bind failed %d\n", value); |
| 350 | list_del(&function->list); |
| 351 | function->config = NULL; |
| 352 | } |
| 353 | } else |
| 354 | value = 0; |
| 355 | |
| 356 | /* We allow configurations that don't work at both speeds. |
| 357 | * If we run into a lowspeed Linux system, treat it the same |
| 358 | * as full speed ... it's the function drivers that will need |
| 359 | * to avoid bulk and ISO transfers. |
| 360 | */ |
| 361 | if (!config->fullspeed && function->fs_descriptors) |
| 362 | config->fullspeed = true; |
| 363 | if (!config->highspeed && function->hs_descriptors) |
| 364 | config->highspeed = true; |
| 365 | if (!config->superspeed && function->ss_descriptors) |
| 366 | config->superspeed = true; |
| 367 | if (!config->superspeed_plus && function->ssp_descriptors) |
| 368 | config->superspeed_plus = true; |
| 369 | |
| 370 | done: |
| 371 | if (value) |
| 372 | INFO(config->cdev, "adding '%s'/%p --> %d\n", |
| 373 | function->name, function, value); |
| 374 | return value; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(usb_add_function); |
| 377 | |
| 378 | void usb_remove_function(struct usb_configuration *c, struct usb_function *f) |
| 379 | { |
| 380 | if (f->disable) |
| 381 | f->disable(f); |
| 382 | |
| 383 | bitmap_zero(f->endpoints, 32); |
| 384 | list_del(&f->list); |
| 385 | if (f->unbind) |
| 386 | f->unbind(c, f); |
| 387 | #if 0 /* ASR private fix */ |
| 388 | if (f->bind_deactivated) |
| 389 | usb_function_activate(f); |
| 390 | #endif |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(usb_remove_function); |
| 393 | |
| 394 | /** |
| 395 | * usb_function_deactivate - prevent function and gadget enumeration |
| 396 | * @function: the function that isn't yet ready to respond |
| 397 | * |
| 398 | * Blocks response of the gadget driver to host enumeration by |
| 399 | * preventing the data line pullup from being activated. This is |
| 400 | * normally called during @bind() processing to change from the |
| 401 | * initial "ready to respond" state, or when a required resource |
| 402 | * becomes available. |
| 403 | * |
| 404 | * For example, drivers that serve as a passthrough to a userspace |
| 405 | * daemon can block enumeration unless that daemon (such as an OBEX, |
| 406 | * MTP, or print server) is ready to handle host requests. |
| 407 | * |
| 408 | * Not all systems support software control of their USB peripheral |
| 409 | * data pullups. |
| 410 | * |
| 411 | * Returns zero on success, else negative errno. |
| 412 | */ |
| 413 | int usb_function_deactivate(struct usb_function *function) |
| 414 | { |
| 415 | struct usb_composite_dev *cdev = function->config->cdev; |
| 416 | unsigned long flags; |
| 417 | int status = 0; |
| 418 | |
| 419 | spin_lock_irqsave(&cdev->lock, flags); |
| 420 | |
| 421 | if (cdev->deactivations == 0) { |
| 422 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 423 | status = usb_gadget_deactivate(cdev->gadget); |
| 424 | spin_lock_irqsave(&cdev->lock, flags); |
| 425 | } |
| 426 | if (status == 0) |
| 427 | cdev->deactivations++; |
| 428 | |
| 429 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 430 | return status; |
| 431 | } |
| 432 | EXPORT_SYMBOL_GPL(usb_function_deactivate); |
| 433 | |
| 434 | /** |
| 435 | * usb_function_activate - allow function and gadget enumeration |
| 436 | * @function: function on which usb_function_activate() was called |
| 437 | * |
| 438 | * Reverses effect of usb_function_deactivate(). If no more functions |
| 439 | * are delaying their activation, the gadget driver will respond to |
| 440 | * host enumeration procedures. |
| 441 | * |
| 442 | * Returns zero on success, else negative errno. |
| 443 | */ |
| 444 | int usb_function_activate(struct usb_function *function) |
| 445 | { |
| 446 | struct usb_composite_dev *cdev = function->config->cdev; |
| 447 | unsigned long flags; |
| 448 | int status = 0; |
| 449 | |
| 450 | spin_lock_irqsave(&cdev->lock, flags); |
| 451 | |
| 452 | if (WARN_ON(cdev->deactivations == 0)) |
| 453 | status = -EINVAL; |
| 454 | else { |
| 455 | cdev->deactivations--; |
| 456 | if (cdev->deactivations == 0) { |
| 457 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 458 | status = usb_gadget_activate(cdev->gadget); |
| 459 | spin_lock_irqsave(&cdev->lock, flags); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 464 | return status; |
| 465 | } |
| 466 | EXPORT_SYMBOL_GPL(usb_function_activate); |
| 467 | |
| 468 | /** |
| 469 | * usb_interface_id() - allocate an unused interface ID |
| 470 | * @config: configuration associated with the interface |
| 471 | * @function: function handling the interface |
| 472 | * Context: single threaded during gadget setup |
| 473 | * |
| 474 | * usb_interface_id() is called from usb_function.bind() callbacks to |
| 475 | * allocate new interface IDs. The function driver will then store that |
| 476 | * ID in interface, association, CDC union, and other descriptors. It |
| 477 | * will also handle any control requests targeted at that interface, |
| 478 | * particularly changing its altsetting via set_alt(). There may |
| 479 | * also be class-specific or vendor-specific requests to handle. |
| 480 | * |
| 481 | * All interface identifier should be allocated using this routine, to |
| 482 | * ensure that for example different functions don't wrongly assign |
| 483 | * different meanings to the same identifier. Note that since interface |
| 484 | * identifiers are configuration-specific, functions used in more than |
| 485 | * one configuration (or more than once in a given configuration) need |
| 486 | * multiple versions of the relevant descriptors. |
| 487 | * |
| 488 | * Returns the interface ID which was allocated; or -ENODEV if no |
| 489 | * more interface IDs can be allocated. |
| 490 | */ |
| 491 | int usb_interface_id(struct usb_configuration *config, |
| 492 | struct usb_function *function) |
| 493 | { |
| 494 | unsigned id = config->next_interface_id; |
| 495 | |
| 496 | if (id < MAX_CONFIG_INTERFACES) { |
| 497 | config->interface[id] = function; |
| 498 | config->next_interface_id = id + 1; |
| 499 | return id; |
| 500 | } |
| 501 | return -ENODEV; |
| 502 | } |
| 503 | EXPORT_SYMBOL_GPL(usb_interface_id); |
| 504 | |
| 505 | unsigned get_usb_interface_id(struct usb_configuration *config) |
| 506 | { |
| 507 | return config->next_interface_id; |
| 508 | } |
| 509 | EXPORT_SYMBOL_GPL(get_usb_interface_id); |
| 510 | |
| 511 | static u8 encode_bMaxPower(enum usb_device_speed speed, |
| 512 | struct usb_configuration *c) |
| 513 | { |
| 514 | unsigned val; |
| 515 | |
| 516 | if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER)) |
| 517 | val = c->MaxPower; |
| 518 | else |
| 519 | val = CONFIG_USB_GADGET_VBUS_DRAW; |
| 520 | if (!val) |
| 521 | return 0; |
| 522 | if (speed < USB_SPEED_SUPER) |
| 523 | return min(val, 500U) / 2; |
| 524 | else |
| 525 | /* |
| 526 | * USB 3.x supports up to 900mA, but since 900 isn't divisible |
| 527 | * by 8 the integral division will effectively cap to 896mA. |
| 528 | */ |
| 529 | return min(val, 900U) / 8; |
| 530 | } |
| 531 | |
| 532 | static int config_buf(struct usb_configuration *config, |
| 533 | enum usb_device_speed speed, void *buf, u8 type) |
| 534 | { |
| 535 | struct usb_config_descriptor *c = buf; |
| 536 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 537 | int len; |
| 538 | struct usb_function *f; |
| 539 | int status; |
| 540 | |
| 541 | len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 542 | /* write the config descriptor */ |
| 543 | c = buf; |
| 544 | c->bLength = USB_DT_CONFIG_SIZE; |
| 545 | c->bDescriptorType = type; |
| 546 | /* wTotalLength is written later */ |
| 547 | c->bNumInterfaces = config->next_interface_id; |
| 548 | c->bConfigurationValue = config->bConfigurationValue; |
| 549 | c->iConfiguration = config->iConfiguration; |
| 550 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
| 551 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 552 | c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER; |
| 553 | c->bMaxPower = encode_bMaxPower(speed, config); |
| 554 | |
| 555 | /* There may be e.g. OTG descriptors */ |
| 556 | if (config->descriptors) { |
| 557 | status = usb_descriptor_fillbuf(next, len, |
| 558 | config->descriptors); |
| 559 | if (status < 0) |
| 560 | return status; |
| 561 | len -= status; |
| 562 | next += status; |
| 563 | } |
| 564 | |
| 565 | /* add each function's descriptors */ |
| 566 | list_for_each_entry(f, &config->functions, list) { |
| 567 | struct usb_descriptor_header **descriptors; |
| 568 | |
| 569 | descriptors = function_descriptors(f, speed); |
| 570 | if (!descriptors) |
| 571 | continue; |
| 572 | status = usb_descriptor_fillbuf(next, len, |
| 573 | (const struct usb_descriptor_header **) descriptors); |
| 574 | if (status < 0) |
| 575 | return status; |
| 576 | len -= status; |
| 577 | next += status; |
| 578 | } |
| 579 | |
| 580 | len = next - buf; |
| 581 | c->wTotalLength = cpu_to_le16(len); |
| 582 | return len; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | #ifdef CONFIG_USB_MV_HSIC_UDC |
| 587 | static int config_buf_hsic(struct usb_configuration *config, |
| 588 | enum usb_device_speed speed, void *buf, u8 type) |
| 589 | { |
| 590 | struct usb_config_descriptor *c = buf; |
| 591 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 592 | int len; |
| 593 | struct usb_function *f; |
| 594 | int status; |
| 595 | |
| 596 | len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 597 | /* write the config descriptor */ |
| 598 | c = buf; |
| 599 | c->bLength = USB_DT_CONFIG_SIZE; |
| 600 | c->bDescriptorType = type; |
| 601 | /* wTotalLength is written later */ |
| 602 | c->bNumInterfaces = config->next_interface_id; |
| 603 | c->bConfigurationValue = config->bConfigurationValue; |
| 604 | c->iConfiguration = config->iConfiguration; |
| 605 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
| 606 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 607 | c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER; |
| 608 | c->bMaxPower = encode_bMaxPower(speed, config); |
| 609 | |
| 610 | /* There may be e.g. OTG descriptors */ |
| 611 | if (config->descriptors) { |
| 612 | status = usb_descriptor_fillbuf(next, len, |
| 613 | config->descriptors); |
| 614 | if (status < 0) |
| 615 | return status; |
| 616 | len -= status; |
| 617 | next += status; |
| 618 | } |
| 619 | |
| 620 | /* add each function's descriptors */ |
| 621 | list_for_each_entry(f, &config->functions, list) { |
| 622 | struct usb_descriptor_header **descriptors; |
| 623 | |
| 624 | switch (speed) { |
| 625 | case USB_SPEED_SUPER: |
| 626 | descriptors = f->ss_descriptors; |
| 627 | break; |
| 628 | case USB_SPEED_HIGH: |
| 629 | descriptors = f->hs_descriptors; |
| 630 | break; |
| 631 | default: |
| 632 | descriptors = f->fs_descriptors; |
| 633 | } |
| 634 | |
| 635 | if (!descriptors) |
| 636 | continue; |
| 637 | status = usb_descriptor_fillbuf(next, len, |
| 638 | (const struct usb_descriptor_header **) descriptors); |
| 639 | if (status < 0) |
| 640 | return status; |
| 641 | len -= status; |
| 642 | next += status; |
| 643 | } |
| 644 | |
| 645 | len = next - buf; |
| 646 | c->wTotalLength = cpu_to_le16(len); |
| 647 | return len; |
| 648 | } |
| 649 | |
| 650 | static int config_desc_hsic(struct usb_composite_dev *cdev, unsigned w_value) |
| 651 | { |
| 652 | struct usb_gadget *gadget = cdev->gadget; |
| 653 | struct usb_configuration *c; |
| 654 | u8 type = w_value >> 8; |
| 655 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 656 | |
| 657 | if (gadget->speed == USB_SPEED_SUPER) |
| 658 | speed = gadget->speed; |
| 659 | else if (gadget_is_dualspeed(gadget)) { |
| 660 | int hs = 0; |
| 661 | if (gadget->speed == USB_SPEED_HIGH) |
| 662 | hs = 1; |
| 663 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 664 | hs = !hs; |
| 665 | if (hs) |
| 666 | speed = USB_SPEED_HIGH; |
| 667 | |
| 668 | } |
| 669 | |
| 670 | /* This is a lookup by config *INDEX* */ |
| 671 | w_value &= 0xff; |
| 672 | list_for_each_entry(c, &cdev->configs, list) { |
| 673 | /* ignore configs that won't work at this speed */ |
| 674 | switch (speed) { |
| 675 | case USB_SPEED_SUPER: |
| 676 | if (!c->superspeed) |
| 677 | continue; |
| 678 | break; |
| 679 | case USB_SPEED_HIGH: |
| 680 | if (!c->highspeed) |
| 681 | continue; |
| 682 | break; |
| 683 | default: |
| 684 | if (!c->fullspeed) |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | if (w_value == 0) |
| 689 | return config_buf_hsic(c, speed, cdev->req->buf, type); |
| 690 | w_value--; |
| 691 | } |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | #endif |
| 695 | |
| 696 | #ifdef CONFIG_USB_TELEPHONY |
| 697 | static int config_buf_u3debug(struct usb_configuration *config, |
| 698 | enum usb_device_speed speed, void *buf, u8 type) |
| 699 | { |
| 700 | struct usb_config_descriptor *c = buf; |
| 701 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 702 | int len; |
| 703 | struct usb_function *f; |
| 704 | int status; |
| 705 | |
| 706 | len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 707 | /* write the config descriptor */ |
| 708 | c = buf; |
| 709 | c->bLength = USB_DT_CONFIG_SIZE; |
| 710 | c->bDescriptorType = type; |
| 711 | /* wTotalLength is written later */ |
| 712 | c->bNumInterfaces = config->next_interface_id; |
| 713 | c->bConfigurationValue = config->bConfigurationValue; |
| 714 | c->iConfiguration = config->iConfiguration; |
| 715 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
| 716 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 717 | c->bmAttributes |= USB_CONFIG_ATT_SELFPOWER; |
| 718 | c->bMaxPower = encode_bMaxPower(speed, config); |
| 719 | |
| 720 | /* There may be e.g. OTG descriptors */ |
| 721 | if (config->descriptors) { |
| 722 | status = usb_descriptor_fillbuf(next, len, |
| 723 | config->descriptors); |
| 724 | if (status < 0) |
| 725 | return status; |
| 726 | len -= status; |
| 727 | next += status; |
| 728 | } |
| 729 | |
| 730 | /* add each function's descriptors */ |
| 731 | list_for_each_entry(f, &config->functions, list) { |
| 732 | struct usb_descriptor_header **descriptors; |
| 733 | |
| 734 | switch (speed) { |
| 735 | case USB_SPEED_SUPER: |
| 736 | descriptors = f->ss_descriptors; |
| 737 | break; |
| 738 | case USB_SPEED_HIGH: |
| 739 | descriptors = f->hs_descriptors; |
| 740 | break; |
| 741 | default: |
| 742 | descriptors = f->fs_descriptors; |
| 743 | } |
| 744 | |
| 745 | if (!descriptors) |
| 746 | continue; |
| 747 | status = usb_descriptor_fillbuf(next, len, |
| 748 | (const struct usb_descriptor_header **) descriptors); |
| 749 | if (status < 0) |
| 750 | return status; |
| 751 | len -= status; |
| 752 | next += status; |
| 753 | } |
| 754 | |
| 755 | len = next - buf; |
| 756 | c->wTotalLength = cpu_to_le16(len); |
| 757 | return len; |
| 758 | } |
| 759 | |
| 760 | static int config_desc_u3debug(struct usb_composite_dev *cdev, unsigned w_value) |
| 761 | { |
| 762 | struct usb_gadget *gadget = cdev->gadget; |
| 763 | struct usb_configuration *c; |
| 764 | u8 type = w_value >> 8; |
| 765 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 766 | |
| 767 | if (gadget->speed == USB_SPEED_SUPER) |
| 768 | speed = gadget->speed; |
| 769 | else if (gadget_is_dualspeed(gadget)) { |
| 770 | int hs = 0; |
| 771 | if (gadget->speed == USB_SPEED_HIGH) |
| 772 | hs = 1; |
| 773 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 774 | hs = !hs; |
| 775 | if (hs) |
| 776 | speed = USB_SPEED_HIGH; |
| 777 | |
| 778 | } |
| 779 | |
| 780 | /* This is a lookup by config *INDEX* */ |
| 781 | w_value &= 0xff; |
| 782 | list_for_each_entry(c, &cdev->configs, list) { |
| 783 | /* ignore configs that won't work at this speed */ |
| 784 | switch (speed) { |
| 785 | case USB_SPEED_SUPER: |
| 786 | if (!c->superspeed) |
| 787 | continue; |
| 788 | break; |
| 789 | case USB_SPEED_HIGH: |
| 790 | if (!c->highspeed) |
| 791 | continue; |
| 792 | break; |
| 793 | default: |
| 794 | if (!c->fullspeed) |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | if (w_value == 0) |
| 799 | return config_buf_u3debug(c, speed, cdev->req->buf, type); |
| 800 | w_value--; |
| 801 | } |
| 802 | return -EINVAL; |
| 803 | } |
| 804 | #endif |
| 805 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) |
| 806 | { |
| 807 | struct usb_gadget *gadget = cdev->gadget; |
| 808 | struct usb_configuration *c; |
| 809 | struct list_head *pos; |
| 810 | u8 type = w_value >> 8; |
| 811 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 812 | |
| 813 | if (gadget->speed >= USB_SPEED_SUPER) |
| 814 | speed = gadget->speed; |
| 815 | else if (gadget_is_dualspeed(gadget)) { |
| 816 | int hs = 0; |
| 817 | if (gadget->speed == USB_SPEED_HIGH) |
| 818 | hs = 1; |
| 819 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 820 | hs = !hs; |
| 821 | if (hs) |
| 822 | speed = USB_SPEED_HIGH; |
| 823 | |
| 824 | } |
| 825 | |
| 826 | /* This is a lookup by config *INDEX* */ |
| 827 | w_value &= 0xff; |
| 828 | |
| 829 | pos = &cdev->configs; |
| 830 | c = cdev->os_desc_config; |
| 831 | if (c) |
| 832 | goto check_config; |
| 833 | |
| 834 | while ((pos = pos->next) != &cdev->configs) { |
| 835 | c = list_entry(pos, typeof(*c), list); |
| 836 | |
| 837 | /* skip OS Descriptors config which is handled separately */ |
| 838 | if (c == cdev->os_desc_config) |
| 839 | continue; |
| 840 | |
| 841 | check_config: |
| 842 | /* ignore configs that won't work at this speed */ |
| 843 | switch (speed) { |
| 844 | case USB_SPEED_SUPER_PLUS: |
| 845 | if (!c->superspeed_plus) |
| 846 | continue; |
| 847 | break; |
| 848 | case USB_SPEED_SUPER: |
| 849 | if (!c->superspeed) |
| 850 | continue; |
| 851 | break; |
| 852 | case USB_SPEED_HIGH: |
| 853 | if (!c->highspeed) |
| 854 | continue; |
| 855 | break; |
| 856 | default: |
| 857 | if (!c->fullspeed) |
| 858 | continue; |
| 859 | } |
| 860 | |
| 861 | if (w_value == 0) |
| 862 | return config_buf(c, speed, cdev->req->buf, type); |
| 863 | w_value--; |
| 864 | } |
| 865 | return -EINVAL; |
| 866 | } |
| 867 | |
| 868 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) |
| 869 | { |
| 870 | struct usb_gadget *gadget = cdev->gadget; |
| 871 | struct usb_configuration *c; |
| 872 | unsigned count = 0; |
| 873 | int hs = 0; |
| 874 | int ss = 0; |
| 875 | int ssp = 0; |
| 876 | |
| 877 | if (gadget_is_dualspeed(gadget)) { |
| 878 | if (gadget->speed == USB_SPEED_HIGH) |
| 879 | hs = 1; |
| 880 | if (gadget->speed == USB_SPEED_SUPER) |
| 881 | ss = 1; |
| 882 | if (gadget->speed == USB_SPEED_SUPER_PLUS) |
| 883 | ssp = 1; |
| 884 | if (type == USB_DT_DEVICE_QUALIFIER) |
| 885 | hs = !hs; |
| 886 | } |
| 887 | list_for_each_entry(c, &cdev->configs, list) { |
| 888 | /* ignore configs that won't work at this speed */ |
| 889 | if (ssp) { |
| 890 | if (!c->superspeed_plus) |
| 891 | continue; |
| 892 | } else if (ss) { |
| 893 | if (!c->superspeed) |
| 894 | continue; |
| 895 | } else if (hs) { |
| 896 | if (!c->highspeed) |
| 897 | continue; |
| 898 | } else { |
| 899 | if (!c->fullspeed) |
| 900 | continue; |
| 901 | } |
| 902 | count++; |
| 903 | } |
| 904 | return count; |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * bos_desc() - prepares the BOS descriptor. |
| 909 | * @cdev: pointer to usb_composite device to generate the bos |
| 910 | * descriptor for |
| 911 | * |
| 912 | * This function generates the BOS (Binary Device Object) |
| 913 | * descriptor and its device capabilities descriptors. The BOS |
| 914 | * descriptor should be supported by a SuperSpeed device. |
| 915 | */ |
| 916 | static int bos_desc(struct usb_composite_dev *cdev) |
| 917 | { |
| 918 | struct usb_ext_cap_descriptor *usb_ext; |
| 919 | struct usb_dcd_config_params dcd_config_params; |
| 920 | struct usb_bos_descriptor *bos = cdev->req->buf; |
| 921 | unsigned int besl = 0; |
| 922 | |
| 923 | bos->bLength = USB_DT_BOS_SIZE; |
| 924 | bos->bDescriptorType = USB_DT_BOS; |
| 925 | |
| 926 | bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); |
| 927 | bos->bNumDeviceCaps = 0; |
| 928 | |
| 929 | /* Get Controller configuration */ |
| 930 | if (cdev->gadget->ops->get_config_params) { |
| 931 | cdev->gadget->ops->get_config_params(cdev->gadget, |
| 932 | &dcd_config_params); |
| 933 | } else { |
| 934 | dcd_config_params.besl_baseline = |
| 935 | USB_DEFAULT_BESL_UNSPECIFIED; |
| 936 | dcd_config_params.besl_deep = |
| 937 | USB_DEFAULT_BESL_UNSPECIFIED; |
| 938 | dcd_config_params.bU1devExitLat = |
| 939 | USB_DEFAULT_U1_DEV_EXIT_LAT; |
| 940 | dcd_config_params.bU2DevExitLat = |
| 941 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
| 942 | } |
| 943 | |
| 944 | if (dcd_config_params.besl_baseline != USB_DEFAULT_BESL_UNSPECIFIED) |
| 945 | besl = USB_BESL_BASELINE_VALID | |
| 946 | USB_SET_BESL_BASELINE(dcd_config_params.besl_baseline); |
| 947 | |
| 948 | if (dcd_config_params.besl_deep != USB_DEFAULT_BESL_UNSPECIFIED) |
| 949 | besl |= USB_BESL_DEEP_VALID | |
| 950 | USB_SET_BESL_DEEP(dcd_config_params.besl_deep); |
| 951 | |
| 952 | /* |
| 953 | * A SuperSpeed device shall include the USB2.0 extension descriptor |
| 954 | * and shall support LPM when operating in USB2.0 HS mode. |
| 955 | */ |
| 956 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 957 | bos->bNumDeviceCaps++; |
| 958 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE); |
| 959 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; |
| 960 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 961 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; |
| 962 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 963 | if (cdev->gadget->speed == USB_SPEED_HIGH) |
| 964 | usb_ext->bmAttributes = 0x0; |
| 965 | else |
| 966 | #endif |
| 967 | usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT); |
| 968 | |
| 969 | /* |
| 970 | * The Superspeed USB Capability descriptor shall be implemented by all |
| 971 | * SuperSpeed devices. |
| 972 | */ |
| 973 | if (gadget_is_superspeed(cdev->gadget)) { |
| 974 | struct usb_ss_cap_descriptor *ss_cap; |
| 975 | |
| 976 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 977 | bos->bNumDeviceCaps++; |
| 978 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); |
| 979 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; |
| 980 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 981 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; |
| 982 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ |
| 983 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 984 | if (cdev->gadget->speed == USB_SPEED_HIGH) { |
| 985 | ss_cap->wSpeedSupported = cpu_to_le16(USB_HIGH_SPEED_OPERATION); |
| 986 | ss_cap->bFunctionalitySupport = USB_HIGH_SPEED_OPERATION; |
| 987 | } else { |
| 988 | #endif |
| 989 | ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | |
| 990 | USB_FULL_SPEED_OPERATION | |
| 991 | USB_HIGH_SPEED_OPERATION | |
| 992 | USB_5GBPS_OPERATION); |
| 993 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; |
| 994 | #ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS |
| 995 | } |
| 996 | #endif |
| 997 | ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; |
| 998 | ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; |
| 999 | } |
| 1000 | |
| 1001 | /* The SuperSpeedPlus USB Device Capability descriptor */ |
| 1002 | if (gadget_is_superspeed_plus(cdev->gadget)) { |
| 1003 | struct usb_ssp_cap_descriptor *ssp_cap; |
| 1004 | |
| 1005 | ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 1006 | bos->bNumDeviceCaps++; |
| 1007 | |
| 1008 | /* |
| 1009 | * Report typical values. |
| 1010 | */ |
| 1011 | |
| 1012 | le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1)); |
| 1013 | ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1); |
| 1014 | ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 1015 | ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE; |
| 1016 | ssp_cap->bReserved = 0; |
| 1017 | ssp_cap->wReserved = 0; |
| 1018 | |
| 1019 | /* SSAC = 1 (2 attributes) */ |
| 1020 | ssp_cap->bmAttributes = cpu_to_le32(1); |
| 1021 | |
| 1022 | /* Min RX/TX Lane Count = 1 */ |
| 1023 | ssp_cap->wFunctionalitySupport = |
| 1024 | cpu_to_le16((1 << 8) | (1 << 12)); |
| 1025 | |
| 1026 | /* |
| 1027 | * bmSublinkSpeedAttr[0]: |
| 1028 | * ST = Symmetric, RX |
| 1029 | * LSE = 3 (Gbps) |
| 1030 | * LP = 1 (SuperSpeedPlus) |
| 1031 | * LSM = 10 (10 Gbps) |
| 1032 | */ |
| 1033 | ssp_cap->bmSublinkSpeedAttr[0] = |
| 1034 | cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16)); |
| 1035 | /* |
| 1036 | * bmSublinkSpeedAttr[1] = |
| 1037 | * ST = Symmetric, TX |
| 1038 | * LSE = 3 (Gbps) |
| 1039 | * LP = 1 (SuperSpeedPlus) |
| 1040 | * LSM = 10 (10 Gbps) |
| 1041 | */ |
| 1042 | ssp_cap->bmSublinkSpeedAttr[1] = |
| 1043 | cpu_to_le32((3 << 4) | (1 << 14) | |
| 1044 | (0xa << 16) | (1 << 7)); |
| 1045 | } |
| 1046 | |
| 1047 | return le16_to_cpu(bos->wTotalLength); |
| 1048 | } |
| 1049 | |
| 1050 | static void device_qual(struct usb_composite_dev *cdev) |
| 1051 | { |
| 1052 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 1053 | |
| 1054 | qual->bLength = sizeof(*qual); |
| 1055 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 1056 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 1057 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 1058 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 1059 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 1060 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 1061 | /* ASSUME same EP0 fifo size at both speeds */ |
| 1062 | qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; |
| 1063 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
| 1064 | qual->bRESERVED = 0; |
| 1065 | } |
| 1066 | |
| 1067 | /*-------------------------------------------------------------------------*/ |
| 1068 | |
| 1069 | static void reset_config(struct usb_composite_dev *cdev) |
| 1070 | { |
| 1071 | struct usb_function *f; |
| 1072 | |
| 1073 | DBG(cdev, "reset config\n"); |
| 1074 | |
| 1075 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1076 | if (f->disable) |
| 1077 | f->disable(f); |
| 1078 | |
| 1079 | bitmap_zero(f->endpoints, 32); |
| 1080 | } |
| 1081 | cdev->config = NULL; |
| 1082 | cdev->delayed_status = 0; |
| 1083 | } |
| 1084 | |
| 1085 | static int set_config(struct usb_composite_dev *cdev, |
| 1086 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 1087 | { |
| 1088 | struct usb_gadget *gadget = cdev->gadget; |
| 1089 | struct usb_configuration *c = NULL; |
| 1090 | int result = -EINVAL; |
| 1091 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 1092 | int tmp; |
| 1093 | |
| 1094 | if (number) { |
| 1095 | list_for_each_entry(c, &cdev->configs, list) { |
| 1096 | if (c->bConfigurationValue == number) { |
| 1097 | /* |
| 1098 | * We disable the FDs of the previous |
| 1099 | * configuration only if the new configuration |
| 1100 | * is a valid one |
| 1101 | */ |
| 1102 | if (cdev->config) |
| 1103 | reset_config(cdev); |
| 1104 | result = 0; |
| 1105 | break; |
| 1106 | } |
| 1107 | } |
| 1108 | if (result < 0) |
| 1109 | goto done; |
| 1110 | } else { /* Zero configuration value - need to reset the config */ |
| 1111 | if (cdev->config) |
| 1112 | reset_config(cdev); |
| 1113 | result = 0; |
| 1114 | } |
| 1115 | |
| 1116 | INFO(cdev, "%s config #%d: %s\n", |
| 1117 | usb_speed_string(gadget->speed), |
| 1118 | number, c ? c->label : "unconfigured"); |
| 1119 | |
| 1120 | if (!c) |
| 1121 | goto done; |
| 1122 | |
| 1123 | usb_gadget_set_state(gadget, USB_STATE_CONFIGURED); |
| 1124 | cdev->config = c; |
| 1125 | |
| 1126 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 1127 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 1128 | struct usb_function *f = c->interface[tmp]; |
| 1129 | struct usb_descriptor_header **descriptors; |
| 1130 | |
| 1131 | if (!f) |
| 1132 | break; |
| 1133 | |
| 1134 | /* |
| 1135 | * Record which endpoints are used by the function. This is used |
| 1136 | * to dispatch control requests targeted at that endpoint to the |
| 1137 | * function's setup callback instead of the current |
| 1138 | * configuration's setup callback. |
| 1139 | */ |
| 1140 | descriptors = function_descriptors(f, gadget->speed); |
| 1141 | |
| 1142 | for (; *descriptors; ++descriptors) { |
| 1143 | struct usb_endpoint_descriptor *ep; |
| 1144 | int addr; |
| 1145 | |
| 1146 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 1147 | continue; |
| 1148 | |
| 1149 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 1150 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 1151 | | (ep->bEndpointAddress & 0x0f); |
| 1152 | set_bit(addr, f->endpoints); |
| 1153 | } |
| 1154 | |
| 1155 | result = f->set_alt(f, tmp, 0); |
| 1156 | if (result < 0) { |
| 1157 | INFO(cdev, "interface %d (%s/%p) alt 0 --> %d\n", |
| 1158 | tmp, f->name, f, result); |
| 1159 | |
| 1160 | reset_config(cdev); |
| 1161 | goto done; |
| 1162 | } |
| 1163 | |
| 1164 | if (result == USB_GADGET_DELAYED_STATUS) { |
| 1165 | DBG(cdev, |
| 1166 | "%s: interface %d (%s) requested delayed status\n", |
| 1167 | __func__, tmp, f->name); |
| 1168 | cdev->delayed_status++; |
| 1169 | DBG(cdev, "delayed_status count %d\n", |
| 1170 | cdev->delayed_status); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /* when we return, be sure our power usage is valid */ |
| 1175 | if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER)) |
| 1176 | power = c->MaxPower; |
| 1177 | else |
| 1178 | power = CONFIG_USB_GADGET_VBUS_DRAW; |
| 1179 | |
| 1180 | if (gadget->speed < USB_SPEED_SUPER) |
| 1181 | power = min(power, 500U); |
| 1182 | else |
| 1183 | power = min(power, 900U); |
| 1184 | done: |
| 1185 | if (power <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 1186 | usb_gadget_set_selfpowered(gadget); |
| 1187 | else |
| 1188 | usb_gadget_clear_selfpowered(gadget); |
| 1189 | |
| 1190 | usb_gadget_vbus_draw(gadget, power); |
| 1191 | if (result >= 0 && cdev->delayed_status) |
| 1192 | result = USB_GADGET_DELAYED_STATUS; |
| 1193 | return result; |
| 1194 | } |
| 1195 | |
| 1196 | int usb_add_config_only(struct usb_composite_dev *cdev, |
| 1197 | struct usb_configuration *config) |
| 1198 | { |
| 1199 | struct usb_configuration *c; |
| 1200 | |
| 1201 | if (!config->bConfigurationValue) |
| 1202 | return -EINVAL; |
| 1203 | |
| 1204 | /* Prevent duplicate configuration identifiers */ |
| 1205 | list_for_each_entry(c, &cdev->configs, list) { |
| 1206 | if (c->bConfigurationValue == config->bConfigurationValue) |
| 1207 | return -EBUSY; |
| 1208 | } |
| 1209 | |
| 1210 | config->cdev = cdev; |
| 1211 | list_add_tail(&config->list, &cdev->configs); |
| 1212 | |
| 1213 | INIT_LIST_HEAD(&config->functions); |
| 1214 | config->next_interface_id = 0; |
| 1215 | memset(config->interface, 0, sizeof(config->interface)); |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | EXPORT_SYMBOL_GPL(usb_add_config_only); |
| 1220 | |
| 1221 | /** |
| 1222 | * usb_add_config() - add a configuration to a device. |
| 1223 | * @cdev: wraps the USB gadget |
| 1224 | * @config: the configuration, with bConfigurationValue assigned |
| 1225 | * @bind: the configuration's bind function |
| 1226 | * Context: single threaded during gadget setup |
| 1227 | * |
| 1228 | * One of the main tasks of a composite @bind() routine is to |
| 1229 | * add each of the configurations it supports, using this routine. |
| 1230 | * |
| 1231 | * This function returns the value of the configuration's @bind(), which |
| 1232 | * is zero for success else a negative errno value. Binding configurations |
| 1233 | * assigns global resources including string IDs, and per-configuration |
| 1234 | * resources such as interface IDs and endpoints. |
| 1235 | */ |
| 1236 | int usb_add_config(struct usb_composite_dev *cdev, |
| 1237 | struct usb_configuration *config, |
| 1238 | int (*bind)(struct usb_configuration *)) |
| 1239 | { |
| 1240 | int status = -EINVAL; |
| 1241 | |
| 1242 | if (!bind) |
| 1243 | goto done; |
| 1244 | |
| 1245 | DBG(cdev, "adding config #%u '%s'/%p\n", |
| 1246 | config->bConfigurationValue, |
| 1247 | config->label, config); |
| 1248 | |
| 1249 | status = usb_add_config_only(cdev, config); |
| 1250 | if (status) |
| 1251 | goto done; |
| 1252 | |
| 1253 | status = bind(config); |
| 1254 | if (status < 0) { |
| 1255 | while (!list_empty(&config->functions)) { |
| 1256 | struct usb_function *f; |
| 1257 | |
| 1258 | f = list_first_entry(&config->functions, |
| 1259 | struct usb_function, list); |
| 1260 | list_del(&f->list); |
| 1261 | if (f->unbind) { |
| 1262 | INFO(cdev, "unbind function '%s'/%p\n", |
| 1263 | f->name, f); |
| 1264 | f->unbind(config, f); |
| 1265 | /* may free memory for "f" */ |
| 1266 | } |
| 1267 | } |
| 1268 | list_del(&config->list); |
| 1269 | config->cdev = NULL; |
| 1270 | } else { |
| 1271 | unsigned i; |
| 1272 | |
| 1273 | DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n", |
| 1274 | config->bConfigurationValue, config, |
| 1275 | config->superspeed_plus ? " superplus" : "", |
| 1276 | config->superspeed ? " super" : "", |
| 1277 | config->highspeed ? " high" : "", |
| 1278 | config->fullspeed |
| 1279 | ? (gadget_is_dualspeed(cdev->gadget) |
| 1280 | ? " full" |
| 1281 | : " full/low") |
| 1282 | : ""); |
| 1283 | |
| 1284 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 1285 | struct usb_function *f = config->interface[i]; |
| 1286 | |
| 1287 | if (!f) |
| 1288 | continue; |
| 1289 | DBG(cdev, " interface %d = %s/%p\n", |
| 1290 | i, f->name, f); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /* set_alt(), or next bind(), sets up ep->claimed as needed */ |
| 1295 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1296 | |
| 1297 | done: |
| 1298 | if (status) |
| 1299 | INFO(cdev, "added config '%s'/%u --> %d\n", config->label, |
| 1300 | config->bConfigurationValue, status); |
| 1301 | return status; |
| 1302 | } |
| 1303 | EXPORT_SYMBOL_GPL(usb_add_config); |
| 1304 | |
| 1305 | static void remove_config(struct usb_composite_dev *cdev, |
| 1306 | struct usb_configuration *config) |
| 1307 | { |
| 1308 | while (!list_empty(&config->functions)) { |
| 1309 | struct usb_function *f; |
| 1310 | |
| 1311 | f = list_first_entry(&config->functions, |
| 1312 | struct usb_function, list); |
| 1313 | |
| 1314 | usb_remove_function(config, f); |
| 1315 | } |
| 1316 | list_del(&config->list); |
| 1317 | if (config->unbind) { |
| 1318 | INFO(cdev, "unbind config '%s'/%p\n", config->label, config); |
| 1319 | config->unbind(config); |
| 1320 | /* may free memory for "c" */ |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * usb_remove_config() - remove a configuration from a device. |
| 1326 | * @cdev: wraps the USB gadget |
| 1327 | * @config: the configuration |
| 1328 | * |
| 1329 | * Drivers must call usb_gadget_disconnect before calling this function |
| 1330 | * to disconnect the device from the host and make sure the host will not |
| 1331 | * try to enumerate the device while we are changing the config list. |
| 1332 | */ |
| 1333 | void usb_remove_config(struct usb_composite_dev *cdev, |
| 1334 | struct usb_configuration *config) |
| 1335 | { |
| 1336 | unsigned long flags; |
| 1337 | |
| 1338 | spin_lock_irqsave(&cdev->lock, flags); |
| 1339 | |
| 1340 | if (cdev->config == config) |
| 1341 | reset_config(cdev); |
| 1342 | |
| 1343 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1344 | |
| 1345 | remove_config(cdev, config); |
| 1346 | } |
| 1347 | |
| 1348 | /*-------------------------------------------------------------------------*/ |
| 1349 | |
| 1350 | /* We support strings in multiple languages ... string descriptor zero |
| 1351 | * says which languages are supported. The typical case will be that |
| 1352 | * only one language (probably English) is used, with i18n handled on |
| 1353 | * the host side. |
| 1354 | */ |
| 1355 | |
| 1356 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) |
| 1357 | { |
| 1358 | const struct usb_gadget_strings *s; |
| 1359 | __le16 language; |
| 1360 | __le16 *tmp; |
| 1361 | |
| 1362 | while (*sp) { |
| 1363 | s = *sp; |
| 1364 | language = cpu_to_le16(s->language); |
| 1365 | for (tmp = buf; *tmp && tmp < &buf[USB_MAX_STRING_LEN]; tmp++) { |
| 1366 | if (*tmp == language) |
| 1367 | goto repeat; |
| 1368 | } |
| 1369 | *tmp++ = language; |
| 1370 | repeat: |
| 1371 | sp++; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | static int lookup_string( |
| 1376 | struct usb_gadget_strings **sp, |
| 1377 | void *buf, |
| 1378 | u16 language, |
| 1379 | int id |
| 1380 | ) |
| 1381 | { |
| 1382 | struct usb_gadget_strings *s; |
| 1383 | int value; |
| 1384 | |
| 1385 | while (*sp) { |
| 1386 | s = *sp++; |
| 1387 | if (s->language != language) |
| 1388 | continue; |
| 1389 | value = usb_gadget_get_string(s, id, buf); |
| 1390 | if (value > 0) |
| 1391 | return value; |
| 1392 | } |
| 1393 | return -EINVAL; |
| 1394 | } |
| 1395 | |
| 1396 | static int get_string(struct usb_composite_dev *cdev, |
| 1397 | void *buf, u16 language, int id) |
| 1398 | { |
| 1399 | struct usb_composite_driver *composite = cdev->driver; |
| 1400 | struct usb_gadget_string_container *uc; |
| 1401 | struct usb_configuration *c; |
| 1402 | struct usb_function *f; |
| 1403 | int len; |
| 1404 | |
| 1405 | /* Yes, not only is USB's i18n support probably more than most |
| 1406 | * folk will ever care about ... also, it's all supported here. |
| 1407 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 1408 | */ |
| 1409 | |
| 1410 | /* 0 == report all available language codes */ |
| 1411 | if (id == 0) { |
| 1412 | struct usb_string_descriptor *s = buf; |
| 1413 | struct usb_gadget_strings **sp; |
| 1414 | |
| 1415 | memset(s, 0, 256); |
| 1416 | s->bDescriptorType = USB_DT_STRING; |
| 1417 | |
| 1418 | sp = composite->strings; |
| 1419 | if (sp) |
| 1420 | collect_langs(sp, s->wData); |
| 1421 | |
| 1422 | list_for_each_entry(c, &cdev->configs, list) { |
| 1423 | sp = c->strings; |
| 1424 | if (sp) |
| 1425 | collect_langs(sp, s->wData); |
| 1426 | |
| 1427 | list_for_each_entry(f, &c->functions, list) { |
| 1428 | sp = f->strings; |
| 1429 | if (sp) |
| 1430 | collect_langs(sp, s->wData); |
| 1431 | } |
| 1432 | } |
| 1433 | list_for_each_entry(uc, &cdev->gstrings, list) { |
| 1434 | struct usb_gadget_strings **sp; |
| 1435 | |
| 1436 | sp = get_containers_gs(uc); |
| 1437 | collect_langs(sp, s->wData); |
| 1438 | } |
| 1439 | |
| 1440 | for (len = 0; len <= USB_MAX_STRING_LEN && s->wData[len]; len++) |
| 1441 | continue; |
| 1442 | if (!len) |
| 1443 | return -EINVAL; |
| 1444 | |
| 1445 | s->bLength = 2 * (len + 1); |
| 1446 | return s->bLength; |
| 1447 | } |
| 1448 | |
| 1449 | if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) { |
| 1450 | struct usb_os_string *b = buf; |
| 1451 | b->bLength = sizeof(*b); |
| 1452 | b->bDescriptorType = USB_DT_STRING; |
| 1453 | compiletime_assert( |
| 1454 | sizeof(b->qwSignature) == sizeof(cdev->qw_sign), |
| 1455 | "qwSignature size must be equal to qw_sign"); |
| 1456 | memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature)); |
| 1457 | b->bMS_VendorCode = cdev->b_vendor_code; |
| 1458 | b->bPad = 0; |
| 1459 | return sizeof(*b); |
| 1460 | } |
| 1461 | |
| 1462 | list_for_each_entry(uc, &cdev->gstrings, list) { |
| 1463 | struct usb_gadget_strings **sp; |
| 1464 | |
| 1465 | sp = get_containers_gs(uc); |
| 1466 | len = lookup_string(sp, buf, language, id); |
| 1467 | if (len > 0) |
| 1468 | return len; |
| 1469 | } |
| 1470 | |
| 1471 | /* String IDs are device-scoped, so we look up each string |
| 1472 | * table we're told about. These lookups are infrequent; |
| 1473 | * simpler-is-better here. |
| 1474 | */ |
| 1475 | if (composite->strings) { |
| 1476 | len = lookup_string(composite->strings, buf, language, id); |
| 1477 | if (len > 0) |
| 1478 | return len; |
| 1479 | } |
| 1480 | list_for_each_entry(c, &cdev->configs, list) { |
| 1481 | if (c->strings) { |
| 1482 | len = lookup_string(c->strings, buf, language, id); |
| 1483 | if (len > 0) |
| 1484 | return len; |
| 1485 | } |
| 1486 | list_for_each_entry(f, &c->functions, list) { |
| 1487 | if (!f->strings) |
| 1488 | continue; |
| 1489 | len = lookup_string(f->strings, buf, language, id); |
| 1490 | if (len > 0) |
| 1491 | return len; |
| 1492 | } |
| 1493 | } |
| 1494 | return -EINVAL; |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * usb_string_id() - allocate an unused string ID |
| 1499 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1500 | * Context: single threaded during gadget setup |
| 1501 | * |
| 1502 | * @usb_string_id() is called from bind() callbacks to allocate |
| 1503 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1504 | * then store that ID in the appropriate descriptors and string table. |
| 1505 | * |
| 1506 | * All string identifier should be allocated using this, |
| 1507 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 1508 | * that for example different functions don't wrongly assign different |
| 1509 | * meanings to the same identifier. |
| 1510 | */ |
| 1511 | int usb_string_id(struct usb_composite_dev *cdev) |
| 1512 | { |
| 1513 | if (cdev->next_string_id < 254) { |
| 1514 | /* string id 0 is reserved by USB spec for list of |
| 1515 | * supported languages */ |
| 1516 | /* 255 reserved as well? -- mina86 */ |
| 1517 | cdev->next_string_id++; |
| 1518 | return cdev->next_string_id; |
| 1519 | } |
| 1520 | return -ENODEV; |
| 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(usb_string_id); |
| 1523 | |
| 1524 | /** |
| 1525 | * usb_string_ids() - allocate unused string IDs in batch |
| 1526 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1527 | * @str: an array of usb_string objects to assign numbers to |
| 1528 | * Context: single threaded during gadget setup |
| 1529 | * |
| 1530 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 1531 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1532 | * then copy IDs from the string table to the appropriate descriptors |
| 1533 | * and string table for other languages. |
| 1534 | * |
| 1535 | * All string identifier should be allocated using this, |
| 1536 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 1537 | * example different functions don't wrongly assign different meanings |
| 1538 | * to the same identifier. |
| 1539 | */ |
| 1540 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 1541 | { |
| 1542 | int next = cdev->next_string_id; |
| 1543 | |
| 1544 | for (; str->s; ++str) { |
| 1545 | if (unlikely(next >= 254)) |
| 1546 | return -ENODEV; |
| 1547 | str->id = ++next; |
| 1548 | } |
| 1549 | |
| 1550 | cdev->next_string_id = next; |
| 1551 | |
| 1552 | return 0; |
| 1553 | } |
| 1554 | EXPORT_SYMBOL_GPL(usb_string_ids_tab); |
| 1555 | |
| 1556 | static struct usb_gadget_string_container *copy_gadget_strings( |
| 1557 | struct usb_gadget_strings **sp, unsigned n_gstrings, |
| 1558 | unsigned n_strings) |
| 1559 | { |
| 1560 | struct usb_gadget_string_container *uc; |
| 1561 | struct usb_gadget_strings **gs_array; |
| 1562 | struct usb_gadget_strings *gs; |
| 1563 | struct usb_string *s; |
| 1564 | unsigned mem; |
| 1565 | unsigned n_gs; |
| 1566 | unsigned n_s; |
| 1567 | void *stash; |
| 1568 | |
| 1569 | mem = sizeof(*uc); |
| 1570 | mem += sizeof(void *) * (n_gstrings + 1); |
| 1571 | mem += sizeof(struct usb_gadget_strings) * n_gstrings; |
| 1572 | mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings); |
| 1573 | uc = kmalloc(mem, GFP_KERNEL); |
| 1574 | if (!uc) |
| 1575 | return ERR_PTR(-ENOMEM); |
| 1576 | gs_array = get_containers_gs(uc); |
| 1577 | stash = uc->stash; |
| 1578 | stash += sizeof(void *) * (n_gstrings + 1); |
| 1579 | for (n_gs = 0; n_gs < n_gstrings; n_gs++) { |
| 1580 | struct usb_string *org_s; |
| 1581 | |
| 1582 | gs_array[n_gs] = stash; |
| 1583 | gs = gs_array[n_gs]; |
| 1584 | stash += sizeof(struct usb_gadget_strings); |
| 1585 | gs->language = sp[n_gs]->language; |
| 1586 | gs->strings = stash; |
| 1587 | org_s = sp[n_gs]->strings; |
| 1588 | |
| 1589 | for (n_s = 0; n_s < n_strings; n_s++) { |
| 1590 | s = stash; |
| 1591 | stash += sizeof(struct usb_string); |
| 1592 | if (org_s->s) |
| 1593 | s->s = org_s->s; |
| 1594 | else |
| 1595 | s->s = ""; |
| 1596 | org_s++; |
| 1597 | } |
| 1598 | s = stash; |
| 1599 | s->s = NULL; |
| 1600 | stash += sizeof(struct usb_string); |
| 1601 | |
| 1602 | } |
| 1603 | gs_array[n_gs] = NULL; |
| 1604 | return uc; |
| 1605 | } |
| 1606 | |
| 1607 | /** |
| 1608 | * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids |
| 1609 | * @cdev: the device whose string descriptor IDs are being allocated |
| 1610 | * and attached. |
| 1611 | * @sp: an array of usb_gadget_strings to attach. |
| 1612 | * @n_strings: number of entries in each usb_strings array (sp[]->strings) |
| 1613 | * |
| 1614 | * This function will create a deep copy of usb_gadget_strings and usb_string |
| 1615 | * and attach it to the cdev. The actual string (usb_string.s) will not be |
| 1616 | * copied but only a referenced will be made. The struct usb_gadget_strings |
| 1617 | * array may contain multiple languages and should be NULL terminated. |
| 1618 | * The ->language pointer of each struct usb_gadget_strings has to contain the |
| 1619 | * same amount of entries. |
| 1620 | * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first |
| 1621 | * usb_string entry of es-ES contains the translation of the first usb_string |
| 1622 | * entry of en-US. Therefore both entries become the same id assign. |
| 1623 | */ |
| 1624 | struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, |
| 1625 | struct usb_gadget_strings **sp, unsigned n_strings) |
| 1626 | { |
| 1627 | struct usb_gadget_string_container *uc; |
| 1628 | struct usb_gadget_strings **n_gs; |
| 1629 | unsigned n_gstrings = 0; |
| 1630 | unsigned i; |
| 1631 | int ret; |
| 1632 | |
| 1633 | for (i = 0; sp[i]; i++) |
| 1634 | n_gstrings++; |
| 1635 | |
| 1636 | if (!n_gstrings) |
| 1637 | return ERR_PTR(-EINVAL); |
| 1638 | |
| 1639 | uc = copy_gadget_strings(sp, n_gstrings, n_strings); |
| 1640 | if (IS_ERR(uc)) |
| 1641 | return ERR_CAST(uc); |
| 1642 | |
| 1643 | n_gs = get_containers_gs(uc); |
| 1644 | ret = usb_string_ids_tab(cdev, n_gs[0]->strings); |
| 1645 | if (ret) |
| 1646 | goto err; |
| 1647 | |
| 1648 | for (i = 1; i < n_gstrings; i++) { |
| 1649 | struct usb_string *m_s; |
| 1650 | struct usb_string *s; |
| 1651 | unsigned n; |
| 1652 | |
| 1653 | m_s = n_gs[0]->strings; |
| 1654 | s = n_gs[i]->strings; |
| 1655 | for (n = 0; n < n_strings; n++) { |
| 1656 | s->id = m_s->id; |
| 1657 | s++; |
| 1658 | m_s++; |
| 1659 | } |
| 1660 | } |
| 1661 | list_add_tail(&uc->list, &cdev->gstrings); |
| 1662 | return n_gs[0]->strings; |
| 1663 | err: |
| 1664 | kfree(uc); |
| 1665 | return ERR_PTR(ret); |
| 1666 | } |
| 1667 | EXPORT_SYMBOL_GPL(usb_gstrings_attach); |
| 1668 | |
| 1669 | /** |
| 1670 | * usb_string_ids_n() - allocate unused string IDs in batch |
| 1671 | * @c: the device whose string descriptor IDs are being allocated |
| 1672 | * @n: number of string IDs to allocate |
| 1673 | * Context: single threaded during gadget setup |
| 1674 | * |
| 1675 | * Returns the first requested ID. This ID and next @n-1 IDs are now |
| 1676 | * valid IDs. At least provided that @n is non-zero because if it |
| 1677 | * is, returns last requested ID which is now very useful information. |
| 1678 | * |
| 1679 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 1680 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 1681 | * then store that ID in the appropriate descriptors and string table. |
| 1682 | * |
| 1683 | * All string identifier should be allocated using this, |
| 1684 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 1685 | * example different functions don't wrongly assign different meanings |
| 1686 | * to the same identifier. |
| 1687 | */ |
| 1688 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 1689 | { |
| 1690 | unsigned next = c->next_string_id; |
| 1691 | if (unlikely(n > 254 || (unsigned)next + n > 254)) |
| 1692 | return -ENODEV; |
| 1693 | c->next_string_id += n; |
| 1694 | return next + 1; |
| 1695 | } |
| 1696 | EXPORT_SYMBOL_GPL(usb_string_ids_n); |
| 1697 | |
| 1698 | /*-------------------------------------------------------------------------*/ |
| 1699 | |
| 1700 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 1701 | { |
| 1702 | struct usb_composite_dev *cdev; |
| 1703 | |
| 1704 | if (req->status || req->actual != req->length) |
| 1705 | INFO((struct usb_composite_dev *) ep->driver_data, |
| 1706 | "setup complete --> %d, %d/%d\n", |
| 1707 | req->status, req->actual, req->length); |
| 1708 | |
| 1709 | /* |
| 1710 | * REVIST The same ep0 requests are shared with function drivers |
| 1711 | * so they don't have to maintain the same ->complete() stubs. |
| 1712 | * |
| 1713 | * Because of that, we need to check for the validity of ->context |
| 1714 | * here, even though we know we've set it to something useful. |
| 1715 | */ |
| 1716 | if (!req->context) |
| 1717 | return; |
| 1718 | |
| 1719 | cdev = req->context; |
| 1720 | |
| 1721 | if (cdev->req == req) |
| 1722 | cdev->setup_pending = false; |
| 1723 | else if (cdev->os_desc_req == req) |
| 1724 | cdev->os_desc_pending = false; |
| 1725 | else |
| 1726 | WARN(1, "unknown request %p\n", req); |
| 1727 | } |
| 1728 | |
| 1729 | static int composite_ep0_queue(struct usb_composite_dev *cdev, |
| 1730 | struct usb_request *req, gfp_t gfp_flags) |
| 1731 | { |
| 1732 | int ret; |
| 1733 | |
| 1734 | ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags); |
| 1735 | if (ret == 0) { |
| 1736 | if (cdev->req == req) |
| 1737 | cdev->setup_pending = true; |
| 1738 | else if (cdev->os_desc_req == req) |
| 1739 | cdev->os_desc_pending = true; |
| 1740 | else |
| 1741 | WARN(1, "unknown request %p\n", req); |
| 1742 | } |
| 1743 | |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | static int count_ext_compat(struct usb_configuration *c) |
| 1748 | { |
| 1749 | int i, res; |
| 1750 | |
| 1751 | res = 0; |
| 1752 | for (i = 0; i < c->next_interface_id; ++i) { |
| 1753 | struct usb_function *f; |
| 1754 | int j; |
| 1755 | |
| 1756 | f = c->interface[i]; |
| 1757 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1758 | struct usb_os_desc *d; |
| 1759 | |
| 1760 | if (i != f->os_desc_table[j].if_id) |
| 1761 | continue; |
| 1762 | d = f->os_desc_table[j].os_desc; |
| 1763 | if (d && d->ext_compat_id) |
| 1764 | ++res; |
| 1765 | } |
| 1766 | } |
| 1767 | BUG_ON(res > 255); |
| 1768 | return res; |
| 1769 | } |
| 1770 | |
| 1771 | static int fill_ext_compat(struct usb_configuration *c, u8 *buf) |
| 1772 | { |
| 1773 | int i, count; |
| 1774 | |
| 1775 | count = 16; |
| 1776 | buf += 16; |
| 1777 | for (i = 0; i < c->next_interface_id; ++i) { |
| 1778 | struct usb_function *f; |
| 1779 | int j; |
| 1780 | |
| 1781 | f = c->interface[i]; |
| 1782 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1783 | struct usb_os_desc *d; |
| 1784 | |
| 1785 | if (i != f->os_desc_table[j].if_id) |
| 1786 | continue; |
| 1787 | d = f->os_desc_table[j].os_desc; |
| 1788 | if (d && d->ext_compat_id) { |
| 1789 | *buf++ = i; |
| 1790 | *buf++ = 0x01; |
| 1791 | memcpy(buf, d->ext_compat_id, 16); |
| 1792 | buf += 22; |
| 1793 | } else { |
| 1794 | ++buf; |
| 1795 | *buf = 0x01; |
| 1796 | buf += 23; |
| 1797 | } |
| 1798 | count += 24; |
| 1799 | if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ) |
| 1800 | return count; |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | return count; |
| 1805 | } |
| 1806 | |
| 1807 | static int count_ext_prop(struct usb_configuration *c, int interface) |
| 1808 | { |
| 1809 | struct usb_function *f; |
| 1810 | int j; |
| 1811 | |
| 1812 | f = c->interface[interface]; |
| 1813 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1814 | struct usb_os_desc *d; |
| 1815 | |
| 1816 | if (interface != f->os_desc_table[j].if_id) |
| 1817 | continue; |
| 1818 | d = f->os_desc_table[j].os_desc; |
| 1819 | if (d && d->ext_compat_id) |
| 1820 | return d->ext_prop_count; |
| 1821 | } |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | static int len_ext_prop(struct usb_configuration *c, int interface) |
| 1826 | { |
| 1827 | struct usb_function *f; |
| 1828 | struct usb_os_desc *d; |
| 1829 | int j, res; |
| 1830 | |
| 1831 | res = 10; /* header length */ |
| 1832 | f = c->interface[interface]; |
| 1833 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1834 | if (interface != f->os_desc_table[j].if_id) |
| 1835 | continue; |
| 1836 | d = f->os_desc_table[j].os_desc; |
| 1837 | if (d) |
| 1838 | return min(res + d->ext_prop_len, 4096); |
| 1839 | } |
| 1840 | return res; |
| 1841 | } |
| 1842 | |
| 1843 | static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf) |
| 1844 | { |
| 1845 | struct usb_function *f; |
| 1846 | struct usb_os_desc *d; |
| 1847 | struct usb_os_desc_ext_prop *ext_prop; |
| 1848 | int j, count, n, ret; |
| 1849 | |
| 1850 | f = c->interface[interface]; |
| 1851 | count = 10; /* header length */ |
| 1852 | buf += 10; |
| 1853 | for (j = 0; j < f->os_desc_n; ++j) { |
| 1854 | if (interface != f->os_desc_table[j].if_id) |
| 1855 | continue; |
| 1856 | d = f->os_desc_table[j].os_desc; |
| 1857 | if (d) |
| 1858 | list_for_each_entry(ext_prop, &d->ext_prop, entry) { |
| 1859 | n = ext_prop->data_len + |
| 1860 | ext_prop->name_len + 14; |
| 1861 | if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ) |
| 1862 | return count; |
| 1863 | usb_ext_prop_put_size(buf, n); |
| 1864 | usb_ext_prop_put_type(buf, ext_prop->type); |
| 1865 | ret = usb_ext_prop_put_name(buf, ext_prop->name, |
| 1866 | ext_prop->name_len); |
| 1867 | if (ret < 0) |
| 1868 | return ret; |
| 1869 | switch (ext_prop->type) { |
| 1870 | case USB_EXT_PROP_UNICODE: |
| 1871 | case USB_EXT_PROP_UNICODE_ENV: |
| 1872 | case USB_EXT_PROP_UNICODE_LINK: |
| 1873 | usb_ext_prop_put_unicode(buf, ret, |
| 1874 | ext_prop->data, |
| 1875 | ext_prop->data_len); |
| 1876 | break; |
| 1877 | case USB_EXT_PROP_BINARY: |
| 1878 | usb_ext_prop_put_binary(buf, ret, |
| 1879 | ext_prop->data, |
| 1880 | ext_prop->data_len); |
| 1881 | break; |
| 1882 | case USB_EXT_PROP_LE32: |
| 1883 | /* not implemented */ |
| 1884 | case USB_EXT_PROP_BE32: |
| 1885 | /* not implemented */ |
| 1886 | default: |
| 1887 | return -EINVAL; |
| 1888 | } |
| 1889 | buf += n; |
| 1890 | count += n; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | return count; |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * The setup() callback implements all the ep0 functionality that's |
| 1899 | * not handled lower down, in hardware or the hardware driver(like |
| 1900 | * device and endpoint feature flags, and their status). It's all |
| 1901 | * housekeeping for the gadget function we're implementing. Most of |
| 1902 | * the work is in config and function specific setup. |
| 1903 | */ |
| 1904 | int |
| 1905 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 1906 | { |
| 1907 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1908 | struct usb_request *req = cdev->req; |
| 1909 | int value = -EOPNOTSUPP; |
| 1910 | int status = 0; |
| 1911 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 1912 | u8 intf = w_index & 0xFF; |
| 1913 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 1914 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 1915 | struct usb_function *f = NULL; |
| 1916 | u8 endp; |
| 1917 | |
| 1918 | if (w_length > USB_COMP_EP0_BUFSIZ) { |
| 1919 | if (ctrl->bRequestType & USB_DIR_IN) { |
| 1920 | /* Cast away the const, we are going to overwrite on purpose. */ |
| 1921 | __le16 *temp = (__le16 *)&ctrl->wLength; |
| 1922 | |
| 1923 | *temp = cpu_to_le16(USB_COMP_EP0_BUFSIZ); |
| 1924 | w_length = USB_COMP_EP0_BUFSIZ; |
| 1925 | } else { |
| 1926 | goto done; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | /* partial re-init of the response message; the function or the |
| 1931 | * gadget might need to intercept e.g. a control-OUT completion |
| 1932 | * when we delegate to it. |
| 1933 | */ |
| 1934 | req->zero = 0; |
| 1935 | req->context = cdev; |
| 1936 | req->complete = composite_setup_complete; |
| 1937 | req->length = 0; |
| 1938 | gadget->ep0->driver_data = cdev; |
| 1939 | |
| 1940 | #if defined(CONFIG_USB_ANDROID_DETECT_HOST_OS) && !defined(CONFIG_USB_TELEPHONY) |
| 1941 | usb_os_detect(cdev, ctrl); |
| 1942 | #else |
| 1943 | if (ctrl->bRequestType != 0x21 && ctrl->bRequestType != 0xa1) |
| 1944 | pr_info("Ctrl: 0x%x. 0x%x. 0x%x. 0x%x. 0x%x\n", |
| 1945 | ctrl->bRequestType, ctrl->bRequest, |
| 1946 | ctrl->wValue, ctrl->wIndex, ctrl->wLength); |
| 1947 | #endif |
| 1948 | /* |
| 1949 | * Don't let non-standard requests match any of the cases below |
| 1950 | * by accident. |
| 1951 | */ |
| 1952 | if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) |
| 1953 | goto unknown; |
| 1954 | |
| 1955 | switch (ctrl->bRequest) { |
| 1956 | |
| 1957 | /* we handle all standard USB descriptors */ |
| 1958 | case USB_REQ_GET_DESCRIPTOR: |
| 1959 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1960 | goto unknown; |
| 1961 | switch (w_value >> 8) { |
| 1962 | |
| 1963 | case USB_DT_DEVICE: |
| 1964 | cdev->desc.bNumConfigurations = |
| 1965 | count_configs(cdev, USB_DT_DEVICE); |
| 1966 | cdev->desc.bMaxPacketSize0 = |
| 1967 | cdev->gadget->ep0->maxpacket; |
| 1968 | if (gadget_is_superspeed(gadget)) { |
| 1969 | if (gadget->speed >= USB_SPEED_SUPER) { |
| 1970 | cdev->desc.bcdUSB = cpu_to_le16(0x0320); |
| 1971 | cdev->desc.bMaxPacketSize0 = 9; |
| 1972 | } else { |
| 1973 | cdev->desc.bcdUSB = cpu_to_le16(0x0210); |
| 1974 | } |
| 1975 | } else { |
| 1976 | if (gadget->lpm_capable) |
| 1977 | cdev->desc.bcdUSB = cpu_to_le16(0x0201); |
| 1978 | else |
| 1979 | cdev->desc.bcdUSB = cpu_to_le16(0x0200); |
| 1980 | } |
| 1981 | |
| 1982 | value = min(w_length, (u16) sizeof cdev->desc); |
| 1983 | memcpy(req->buf, &cdev->desc, value); |
| 1984 | break; |
| 1985 | case USB_DT_DEVICE_QUALIFIER: |
| 1986 | if (!gadget_is_dualspeed(gadget) || |
| 1987 | gadget->speed >= USB_SPEED_SUPER) |
| 1988 | break; |
| 1989 | device_qual(cdev); |
| 1990 | value = min_t(int, w_length, |
| 1991 | sizeof(struct usb_qualifier_descriptor)); |
| 1992 | break; |
| 1993 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1994 | if (!gadget_is_dualspeed(gadget) || |
| 1995 | gadget->speed >= USB_SPEED_SUPER) |
| 1996 | break; |
| 1997 | /* FALLTHROUGH */ |
| 1998 | case USB_DT_CONFIG: |
| 1999 | value = config_desc(cdev, w_value); |
| 2000 | if (value >= 0) |
| 2001 | value = min(w_length, (u16) value); |
| 2002 | break; |
| 2003 | case USB_DT_STRING: |
| 2004 | value = get_string(cdev, req->buf, |
| 2005 | w_index, w_value & 0xff); |
| 2006 | if (value >= 0) |
| 2007 | value = min(w_length, (u16) value); |
| 2008 | break; |
| 2009 | case USB_DT_BOS: |
| 2010 | if (gadget_is_superspeed(gadget) || |
| 2011 | gadget->lpm_capable) { |
| 2012 | value = bos_desc(cdev); |
| 2013 | value = min(w_length, (u16) value); |
| 2014 | } |
| 2015 | break; |
| 2016 | case USB_DT_OTG: |
| 2017 | if (gadget_is_otg(gadget)) { |
| 2018 | struct usb_configuration *config; |
| 2019 | int otg_desc_len = 0; |
| 2020 | |
| 2021 | if (cdev->config) |
| 2022 | config = cdev->config; |
| 2023 | else |
| 2024 | config = list_first_entry( |
| 2025 | &cdev->configs, |
| 2026 | struct usb_configuration, list); |
| 2027 | if (!config) |
| 2028 | goto done; |
| 2029 | |
| 2030 | if (gadget->otg_caps && |
| 2031 | (gadget->otg_caps->otg_rev >= 0x0200)) |
| 2032 | otg_desc_len += sizeof( |
| 2033 | struct usb_otg20_descriptor); |
| 2034 | else |
| 2035 | otg_desc_len += sizeof( |
| 2036 | struct usb_otg_descriptor); |
| 2037 | |
| 2038 | value = min_t(int, w_length, otg_desc_len); |
| 2039 | memcpy(req->buf, config->descriptors[0], value); |
| 2040 | } |
| 2041 | break; |
| 2042 | } |
| 2043 | break; |
| 2044 | |
| 2045 | /* any number of configs can work */ |
| 2046 | case USB_REQ_SET_CONFIGURATION: |
| 2047 | if (ctrl->bRequestType != 0) |
| 2048 | goto unknown; |
| 2049 | if (gadget_is_otg(gadget)) { |
| 2050 | if (gadget->a_hnp_support) |
| 2051 | INFO(cdev, "HNP available\n"); |
| 2052 | else if (gadget->a_alt_hnp_support) |
| 2053 | DBG(cdev, "HNP on another port\n"); |
| 2054 | else |
| 2055 | VDBG(cdev, "HNP inactive\n"); |
| 2056 | } |
| 2057 | spin_lock(&cdev->lock); |
| 2058 | value = set_config(cdev, ctrl, w_value); |
| 2059 | spin_unlock(&cdev->lock); |
| 2060 | break; |
| 2061 | case USB_REQ_GET_CONFIGURATION: |
| 2062 | if (ctrl->bRequestType != USB_DIR_IN) |
| 2063 | goto unknown; |
| 2064 | if (cdev->config) |
| 2065 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 2066 | else |
| 2067 | *(u8 *)req->buf = 0; |
| 2068 | value = min(w_length, (u16) 1); |
| 2069 | break; |
| 2070 | |
| 2071 | /* function drivers must handle get/set altsetting */ |
| 2072 | case USB_REQ_SET_INTERFACE: |
| 2073 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 2074 | goto unknown; |
| 2075 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2076 | break; |
| 2077 | f = cdev->config->interface[intf]; |
| 2078 | if (!f) |
| 2079 | break; |
| 2080 | |
| 2081 | /* |
| 2082 | * If there's no get_alt() method, we know only altsetting zero |
| 2083 | * works. There is no need to check if set_alt() is not NULL |
| 2084 | * as we check this in usb_add_function(). |
| 2085 | */ |
| 2086 | if (w_value && !f->get_alt) |
| 2087 | break; |
| 2088 | |
| 2089 | spin_lock(&cdev->lock); |
| 2090 | value = f->set_alt(f, w_index, w_value); |
| 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 | } |
| 2099 | spin_unlock(&cdev->lock); |
| 2100 | break; |
| 2101 | case USB_REQ_GET_INTERFACE: |
| 2102 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 2103 | goto unknown; |
| 2104 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2105 | break; |
| 2106 | f = cdev->config->interface[intf]; |
| 2107 | if (!f) |
| 2108 | break; |
| 2109 | /* lots of interfaces only need altsetting zero... */ |
| 2110 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 2111 | if (value < 0) |
| 2112 | break; |
| 2113 | *((u8 *)req->buf) = value; |
| 2114 | value = min(w_length, (u16) 1); |
| 2115 | break; |
| 2116 | case USB_REQ_GET_STATUS: |
| 2117 | if (gadget_is_otg(gadget) && gadget->hnp_polling_support && |
| 2118 | (w_index == OTG_STS_SELECTOR)) { |
| 2119 | if (ctrl->bRequestType != (USB_DIR_IN | |
| 2120 | USB_RECIP_DEVICE)) |
| 2121 | goto unknown; |
| 2122 | *((u8 *)req->buf) = gadget->host_request_flag; |
| 2123 | value = 1; |
| 2124 | break; |
| 2125 | } |
| 2126 | |
| 2127 | /* |
| 2128 | * USB 3.0 additions: |
| 2129 | * Function driver should handle get_status request. If such cb |
| 2130 | * wasn't supplied we respond with default value = 0 |
| 2131 | * Note: function driver should supply such cb only for the |
| 2132 | * first interface of the function |
| 2133 | */ |
| 2134 | if (!gadget_is_superspeed(gadget)) |
| 2135 | goto unknown; |
| 2136 | if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) |
| 2137 | goto unknown; |
| 2138 | value = 2; /* This is the length of the get_status reply */ |
| 2139 | put_unaligned_le16(0, req->buf); |
| 2140 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2141 | break; |
| 2142 | f = cdev->config->interface[intf]; |
| 2143 | if (!f) |
| 2144 | break; |
| 2145 | status = f->get_status ? f->get_status(f) : 0; |
| 2146 | if (status < 0) |
| 2147 | break; |
| 2148 | put_unaligned_le16(status & 0x0000ffff, req->buf); |
| 2149 | break; |
| 2150 | /* |
| 2151 | * Function drivers should handle SetFeature/ClearFeature |
| 2152 | * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied |
| 2153 | * only for the first interface of the function |
| 2154 | */ |
| 2155 | case USB_REQ_CLEAR_FEATURE: |
| 2156 | case USB_REQ_SET_FEATURE: |
| 2157 | if (!gadget_is_superspeed(gadget)) |
| 2158 | goto unknown; |
| 2159 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) |
| 2160 | goto unknown; |
| 2161 | switch (w_value) { |
| 2162 | case USB_INTRF_FUNC_SUSPEND: |
| 2163 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2164 | break; |
| 2165 | f = cdev->config->interface[intf]; |
| 2166 | if (!f) |
| 2167 | break; |
| 2168 | value = 0; |
| 2169 | if (f->func_suspend) |
| 2170 | value = f->func_suspend(f, w_index >> 8); |
| 2171 | if (value < 0) { |
| 2172 | ERROR(cdev, |
| 2173 | "func_suspend() returned error %d\n", |
| 2174 | value); |
| 2175 | value = 0; |
| 2176 | } |
| 2177 | break; |
| 2178 | } |
| 2179 | break; |
| 2180 | default: |
| 2181 | unknown: |
| 2182 | /* |
| 2183 | * OS descriptors handling |
| 2184 | */ |
| 2185 | if (cdev->use_os_string && cdev->os_desc_config && |
| 2186 | (ctrl->bRequestType & USB_TYPE_VENDOR) && |
| 2187 | ctrl->bRequest == cdev->b_vendor_code) { |
| 2188 | struct usb_configuration *os_desc_cfg; |
| 2189 | u8 *buf; |
| 2190 | int interface; |
| 2191 | int count = 0; |
| 2192 | |
| 2193 | req = cdev->os_desc_req; |
| 2194 | req->context = cdev; |
| 2195 | req->complete = composite_setup_complete; |
| 2196 | buf = req->buf; |
| 2197 | os_desc_cfg = cdev->os_desc_config; |
| 2198 | w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ); |
| 2199 | memset(buf, 0, w_length); |
| 2200 | buf[5] = 0x01; |
| 2201 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 2202 | /* |
| 2203 | * The Microsoft CompatID OS Descriptor Spec(w_index = 0x4) and |
| 2204 | * Extended Prop OS Desc Spec(w_index = 0x5) state that the |
| 2205 | * HighByte of wValue is the InterfaceNumber and the LowByte is |
| 2206 | * the PageNumber. This high/low byte ordering is incorrectly |
| 2207 | * documented in the Spec. USB analyzer output on the below |
| 2208 | * request packets show the high/low byte inverted i.e LowByte |
| 2209 | * is the InterfaceNumber and the HighByte is the PageNumber. |
| 2210 | * Since we dont support >64KB CompatID/ExtendedProp descriptors, |
| 2211 | * PageNumber is set to 0. Hence verify that the HighByte is 0 |
| 2212 | * for below two cases. |
| 2213 | */ |
| 2214 | case USB_RECIP_DEVICE: |
| 2215 | if (w_index != 0x4 || (w_value >> 8)) |
| 2216 | break; |
| 2217 | buf[6] = w_index; |
| 2218 | /* Number of ext compat interfaces */ |
| 2219 | count = count_ext_compat(os_desc_cfg); |
| 2220 | buf[8] = count; |
| 2221 | count *= 24; /* 24 B/ext compat desc */ |
| 2222 | count += 16; /* header */ |
| 2223 | put_unaligned_le32(count, buf); |
| 2224 | value = w_length; |
| 2225 | if (w_length > 0x10) { |
| 2226 | value = fill_ext_compat(os_desc_cfg, buf); |
| 2227 | value = min_t(u16, w_length, value); |
| 2228 | } |
| 2229 | break; |
| 2230 | case USB_RECIP_INTERFACE: |
| 2231 | if (w_index != 0x5 || (w_value >> 8)) |
| 2232 | break; |
| 2233 | interface = w_value & 0xFF; |
| 2234 | if (interface >= MAX_CONFIG_INTERFACES || |
| 2235 | !os_desc_cfg->interface[interface]) |
| 2236 | break; |
| 2237 | buf[6] = w_index; |
| 2238 | count = count_ext_prop(os_desc_cfg, |
| 2239 | interface); |
| 2240 | put_unaligned_le16(count, buf + 8); |
| 2241 | count = len_ext_prop(os_desc_cfg, |
| 2242 | interface); |
| 2243 | put_unaligned_le32(count, buf); |
| 2244 | value = w_length; |
| 2245 | if (w_length > 0x0A) { |
| 2246 | value = fill_ext_prop(os_desc_cfg, |
| 2247 | interface, buf); |
| 2248 | if (value >= 0) |
| 2249 | value = min_t(u16, w_length, value); |
| 2250 | } |
| 2251 | break; |
| 2252 | } |
| 2253 | |
| 2254 | goto check_value; |
| 2255 | } |
| 2256 | |
| 2257 | VDBG(cdev, |
| 2258 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 2259 | ctrl->bRequestType, ctrl->bRequest, |
| 2260 | w_value, w_index, w_length); |
| 2261 | |
| 2262 | /* functions always handle their interfaces and endpoints... |
| 2263 | * punt other recipients (other, WUSB, ...) to the current |
| 2264 | * configuration code. |
| 2265 | */ |
| 2266 | if (cdev->config) { |
| 2267 | list_for_each_entry(f, &cdev->config->functions, list) |
| 2268 | if (f->req_match && |
| 2269 | f->req_match(f, ctrl, false)) |
| 2270 | goto try_fun_setup; |
| 2271 | } else { |
| 2272 | struct usb_configuration *c; |
| 2273 | list_for_each_entry(c, &cdev->configs, list) |
| 2274 | list_for_each_entry(f, &c->functions, list) |
| 2275 | if (f->req_match && |
| 2276 | f->req_match(f, ctrl, true)) |
| 2277 | goto try_fun_setup; |
| 2278 | } |
| 2279 | f = NULL; |
| 2280 | |
| 2281 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 2282 | case USB_RECIP_INTERFACE: |
| 2283 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2284 | break; |
| 2285 | f = cdev->config->interface[intf]; |
| 2286 | break; |
| 2287 | |
| 2288 | case USB_RECIP_ENDPOINT: |
| 2289 | if (!cdev->config) |
| 2290 | break; |
| 2291 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 2292 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 2293 | if (test_bit(endp, f->endpoints)) |
| 2294 | break; |
| 2295 | } |
| 2296 | if (&f->list == &cdev->config->functions) |
| 2297 | f = NULL; |
| 2298 | break; |
| 2299 | } |
| 2300 | try_fun_setup: |
| 2301 | if (f && f->setup) |
| 2302 | value = f->setup(f, ctrl); |
| 2303 | else { |
| 2304 | struct usb_configuration *c; |
| 2305 | |
| 2306 | c = cdev->config; |
| 2307 | if (!c) |
| 2308 | goto done; |
| 2309 | |
| 2310 | /* try current config's setup */ |
| 2311 | if (c->setup) { |
| 2312 | value = c->setup(c, ctrl); |
| 2313 | goto done; |
| 2314 | } |
| 2315 | |
| 2316 | /* try the only function in the current config */ |
| 2317 | if (!list_is_singular(&c->functions)) |
| 2318 | goto done; |
| 2319 | f = list_first_entry(&c->functions, struct usb_function, |
| 2320 | list); |
| 2321 | if (f->setup) |
| 2322 | value = f->setup(f, ctrl); |
| 2323 | } |
| 2324 | |
| 2325 | goto done; |
| 2326 | } |
| 2327 | |
| 2328 | check_value: |
| 2329 | /* respond with data transfer before status phase? */ |
| 2330 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
| 2331 | req->length = value; |
| 2332 | req->context = cdev; |
| 2333 | req->zero = value < w_length; |
| 2334 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
| 2335 | if (value < 0) { |
| 2336 | INFO(cdev, "ep_queue --> %d\n", value); |
| 2337 | req->status = 0; |
| 2338 | composite_setup_complete(gadget->ep0, req); |
| 2339 | } |
| 2340 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
| 2341 | WARN(cdev, |
| 2342 | "%s: Delayed status not supported for w_length != 0", |
| 2343 | __func__); |
| 2344 | } |
| 2345 | |
| 2346 | done: |
| 2347 | /* device either stalls (value < 0) or reports success */ |
| 2348 | return value; |
| 2349 | } |
| 2350 | |
| 2351 | #ifdef CONFIG_USB_MV_HSIC_UDC |
| 2352 | |
| 2353 | /* |
| 2354 | * The setup() callback implements all the ep0 functionality that's |
| 2355 | * not handled lower down, in hardware or the hardware driver(like |
| 2356 | * device and endpoint feature flags, and their status). It's all |
| 2357 | * housekeeping for the gadget function we're implementing. Most of |
| 2358 | * the work is in config and function specific setup. |
| 2359 | */ |
| 2360 | int |
| 2361 | composite_setup_hsic(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 2362 | { |
| 2363 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2364 | struct usb_request *req = cdev->req; |
| 2365 | int value = -EOPNOTSUPP; |
| 2366 | int status = 0; |
| 2367 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 2368 | u8 intf = w_index & 0xFF; |
| 2369 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 2370 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 2371 | struct usb_function *f = NULL; |
| 2372 | u8 endp; |
| 2373 | |
| 2374 | /* partial re-init of the response message; the function or the |
| 2375 | * gadget might need to intercept e.g. a control-OUT completion |
| 2376 | * when we delegate to it. |
| 2377 | */ |
| 2378 | req->zero = 0; |
| 2379 | req->complete = composite_setup_complete; |
| 2380 | req->length = 0; |
| 2381 | gadget->ep0->driver_data = cdev; |
| 2382 | |
| 2383 | if (ctrl->bRequest == 0x06) |
| 2384 | pr_info("Ctrl: 0x%x. 0x%x. 0x%x. 0x%x. 0x%x\n", |
| 2385 | ctrl->bRequestType, ctrl->bRequest, |
| 2386 | ctrl->wValue, ctrl->wIndex, ctrl->wLength); |
| 2387 | |
| 2388 | switch (ctrl->bRequest) { |
| 2389 | |
| 2390 | /* we handle all standard USB descriptors */ |
| 2391 | case USB_REQ_GET_DESCRIPTOR: |
| 2392 | if (ctrl->bRequestType != USB_DIR_IN) |
| 2393 | goto unknown; |
| 2394 | |
| 2395 | switch (w_value >> 8) { |
| 2396 | |
| 2397 | case USB_DT_DEVICE: |
| 2398 | cdev->desc.bNumConfigurations = |
| 2399 | count_configs(cdev, USB_DT_DEVICE); |
| 2400 | cdev->desc.bMaxPacketSize0 = |
| 2401 | cdev->gadget->ep0->maxpacket; |
| 2402 | if (gadget_is_superspeed(gadget)) { |
| 2403 | if (gadget->speed >= USB_SPEED_SUPER) { |
| 2404 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
| 2405 | cdev->desc.bMaxPacketSize0 = 9; |
| 2406 | } else { |
| 2407 | cdev->desc.bcdUSB = cpu_to_le16(0x0210); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | value = min(w_length, (u16) sizeof cdev->desc); |
| 2412 | memcpy(req->buf, &cdev->desc, value); |
| 2413 | break; |
| 2414 | case USB_DT_DEVICE_QUALIFIER: |
| 2415 | if (!gadget_is_dualspeed(gadget) || |
| 2416 | gadget->speed >= USB_SPEED_SUPER) |
| 2417 | break; |
| 2418 | device_qual(cdev); |
| 2419 | value = min_t(int, w_length, |
| 2420 | sizeof(struct usb_qualifier_descriptor)); |
| 2421 | break; |
| 2422 | case USB_DT_OTHER_SPEED_CONFIG: |
| 2423 | if (!gadget_is_dualspeed(gadget) || |
| 2424 | gadget->speed >= USB_SPEED_SUPER) |
| 2425 | break; |
| 2426 | /* FALLTHROUGH */ |
| 2427 | case USB_DT_CONFIG: |
| 2428 | value = config_desc_hsic(cdev, w_value); |
| 2429 | if (value >= 0) |
| 2430 | value = min(w_length, (u16) value); |
| 2431 | break; |
| 2432 | case USB_DT_STRING: |
| 2433 | value = get_string(cdev, req->buf, |
| 2434 | w_index, w_value & 0xff); |
| 2435 | if (value >= 0) |
| 2436 | value = min(w_length, (u16) value); |
| 2437 | break; |
| 2438 | case USB_DT_BOS: |
| 2439 | if (gadget_is_superspeed(gadget)) { |
| 2440 | value = bos_desc(cdev); |
| 2441 | value = min(w_length, (u16) value); |
| 2442 | } |
| 2443 | break; |
| 2444 | } |
| 2445 | break; |
| 2446 | |
| 2447 | /* any number of configs can work */ |
| 2448 | case USB_REQ_SET_CONFIGURATION: |
| 2449 | if (ctrl->bRequestType != 0) |
| 2450 | goto unknown; |
| 2451 | if (gadget_is_otg(gadget)) { |
| 2452 | if (gadget->a_hnp_support) |
| 2453 | DBG(cdev, "HNP available\n"); |
| 2454 | else if (gadget->a_alt_hnp_support) |
| 2455 | DBG(cdev, "HNP on another port\n"); |
| 2456 | else |
| 2457 | VDBG(cdev, "HNP inactive\n"); |
| 2458 | } |
| 2459 | spin_lock(&cdev->lock); |
| 2460 | value = set_config_hsic(cdev, ctrl, w_value); |
| 2461 | spin_unlock(&cdev->lock); |
| 2462 | break; |
| 2463 | case USB_REQ_GET_CONFIGURATION: |
| 2464 | if (ctrl->bRequestType != USB_DIR_IN) |
| 2465 | goto unknown; |
| 2466 | if (cdev->config) |
| 2467 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 2468 | else |
| 2469 | *(u8 *)req->buf = 0; |
| 2470 | value = min(w_length, (u16) 1); |
| 2471 | break; |
| 2472 | |
| 2473 | /* function drivers must handle get/set altsetting; if there's |
| 2474 | * no get() method, we know only altsetting zero works. |
| 2475 | */ |
| 2476 | case USB_REQ_SET_INTERFACE: |
| 2477 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 2478 | goto unknown; |
| 2479 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2480 | break; |
| 2481 | |
| 2482 | f = cdev->config->interface[intf]; |
| 2483 | if (!f) |
| 2484 | break; |
| 2485 | if (w_value && !f->set_alt) |
| 2486 | break; |
| 2487 | value = f->set_alt(f, w_index, w_value); |
| 2488 | if (value == USB_GADGET_DELAYED_STATUS) { |
| 2489 | DBG(cdev, |
| 2490 | "%s: interface %d (%s) requested delayed status\n", |
| 2491 | __func__, intf, f->name); |
| 2492 | cdev->delayed_status++; |
| 2493 | DBG(cdev, "delayed_status count %d\n", |
| 2494 | cdev->delayed_status); |
| 2495 | } |
| 2496 | break; |
| 2497 | case USB_REQ_GET_INTERFACE: |
| 2498 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 2499 | goto unknown; |
| 2500 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2501 | break; |
| 2502 | f = cdev->config->interface[intf]; |
| 2503 | if (!f) |
| 2504 | break; |
| 2505 | /* lots of interfaces only need altsetting zero... */ |
| 2506 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 2507 | if (value < 0) |
| 2508 | break; |
| 2509 | *((u8 *)req->buf) = value; |
| 2510 | value = min(w_length, (u16) 1); |
| 2511 | break; |
| 2512 | |
| 2513 | /* |
| 2514 | * USB 3.0 additions: |
| 2515 | * Function driver should handle get_status request. If such cb |
| 2516 | * wasn't supplied we respond with default value = 0 |
| 2517 | * Note: function driver should supply such cb only for the first |
| 2518 | * interface of the function |
| 2519 | */ |
| 2520 | case USB_REQ_GET_STATUS: |
| 2521 | if (!gadget_is_superspeed(gadget)) |
| 2522 | goto unknown; |
| 2523 | if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) |
| 2524 | goto unknown; |
| 2525 | value = 2; /* This is the length of the get_status reply */ |
| 2526 | put_unaligned_le16(0, req->buf); |
| 2527 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2528 | break; |
| 2529 | f = cdev->config->interface[intf]; |
| 2530 | if (!f) |
| 2531 | break; |
| 2532 | status = f->get_status ? f->get_status(f) : 0; |
| 2533 | if (status < 0) |
| 2534 | break; |
| 2535 | put_unaligned_le16(status & 0x0000ffff, req->buf); |
| 2536 | break; |
| 2537 | /* |
| 2538 | * Function drivers should handle SetFeature/ClearFeature |
| 2539 | * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied |
| 2540 | * only for the first interface of the function |
| 2541 | */ |
| 2542 | case USB_REQ_CLEAR_FEATURE: |
| 2543 | case USB_REQ_SET_FEATURE: |
| 2544 | if (!gadget_is_superspeed(gadget)) |
| 2545 | goto unknown; |
| 2546 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) |
| 2547 | goto unknown; |
| 2548 | switch (w_value) { |
| 2549 | case USB_INTRF_FUNC_SUSPEND: |
| 2550 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2551 | break; |
| 2552 | f = cdev->config->interface[intf]; |
| 2553 | if (!f) |
| 2554 | break; |
| 2555 | value = 0; |
| 2556 | if (f->func_suspend) |
| 2557 | value = f->func_suspend(f, w_index >> 8); |
| 2558 | if (value < 0) { |
| 2559 | ERROR(cdev, |
| 2560 | "func_suspend() returned error %d\n", |
| 2561 | value); |
| 2562 | value = 0; |
| 2563 | } |
| 2564 | break; |
| 2565 | } |
| 2566 | break; |
| 2567 | default: |
| 2568 | unknown: |
| 2569 | VDBG(cdev, |
| 2570 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 2571 | ctrl->bRequestType, ctrl->bRequest, |
| 2572 | w_value, w_index, w_length); |
| 2573 | |
| 2574 | /* functions always handle their interfaces and endpoints... |
| 2575 | * punt other recipients (other, WUSB, ...) to the current |
| 2576 | * configuration code. |
| 2577 | * |
| 2578 | * REVISIT it could make sense to let the composite device |
| 2579 | * take such requests too, if that's ever needed: to work |
| 2580 | * in config 0, etc. |
| 2581 | */ |
| 2582 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 2583 | case USB_RECIP_INTERFACE: |
| 2584 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2585 | break; |
| 2586 | f = cdev->config->interface[intf]; |
| 2587 | break; |
| 2588 | |
| 2589 | case USB_RECIP_ENDPOINT: |
| 2590 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 2591 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 2592 | if (test_bit(endp, f->endpoints)) |
| 2593 | break; |
| 2594 | } |
| 2595 | if (&f->list == &cdev->config->functions) |
| 2596 | f = NULL; |
| 2597 | break; |
| 2598 | } |
| 2599 | |
| 2600 | if (f && f->setup) |
| 2601 | value = f->setup(f, ctrl); |
| 2602 | else { |
| 2603 | struct usb_configuration *c; |
| 2604 | |
| 2605 | c = cdev->config; |
| 2606 | if (c && c->setup) |
| 2607 | value = c->setup(c, ctrl); |
| 2608 | } |
| 2609 | |
| 2610 | goto done; |
| 2611 | } |
| 2612 | |
| 2613 | /* respond with data transfer before status phase? */ |
| 2614 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
| 2615 | req->length = value; |
| 2616 | req->zero = value < w_length; |
| 2617 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 2618 | if (value < 0) { |
| 2619 | DBG(cdev, "ep_queue --> %d\n", value); |
| 2620 | req->status = 0; |
| 2621 | composite_setup_complete(gadget->ep0, req); |
| 2622 | } |
| 2623 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
| 2624 | WARN(cdev, |
| 2625 | "%s: Delayed status not supported for w_length != 0", |
| 2626 | __func__); |
| 2627 | } |
| 2628 | |
| 2629 | done: |
| 2630 | /* device either stalls (value < 0) or reports success */ |
| 2631 | return value; |
| 2632 | } |
| 2633 | #endif |
| 2634 | |
| 2635 | #ifdef CONFIG_USB_TELEPHONY |
| 2636 | |
| 2637 | /* |
| 2638 | * The setup() callback implements all the ep0 functionality that's |
| 2639 | * not handled lower down, in hardware or the hardware driver(like |
| 2640 | * device and endpoint feature flags, and their status). It's all |
| 2641 | * housekeeping for the gadget function we're implementing. Most of |
| 2642 | * the work is in config and function specific setup. |
| 2643 | */ |
| 2644 | int |
| 2645 | composite_setup_u3debug(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 2646 | { |
| 2647 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2648 | struct usb_request *req = cdev->req; |
| 2649 | int value = -EOPNOTSUPP; |
| 2650 | int status = 0; |
| 2651 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 2652 | u8 intf = w_index & 0xFF; |
| 2653 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 2654 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 2655 | struct usb_function *f = NULL; |
| 2656 | u8 endp; |
| 2657 | |
| 2658 | /* partial re-init of the response message; the function or the |
| 2659 | * gadget might need to intercept e.g. a control-OUT completion |
| 2660 | * when we delegate to it. |
| 2661 | */ |
| 2662 | req->zero = 0; |
| 2663 | req->complete = composite_setup_complete; |
| 2664 | req->length = 0; |
| 2665 | gadget->ep0->driver_data = cdev; |
| 2666 | |
| 2667 | if (ctrl->bRequest == 0x06) |
| 2668 | pr_info("Ctrl: 0x%x. 0x%x. 0x%x. 0x%x. 0x%x\n", |
| 2669 | ctrl->bRequestType, ctrl->bRequest, |
| 2670 | ctrl->wValue, ctrl->wIndex, ctrl->wLength); |
| 2671 | |
| 2672 | switch (ctrl->bRequest) { |
| 2673 | |
| 2674 | /* we handle all standard USB descriptors */ |
| 2675 | case USB_REQ_GET_DESCRIPTOR: |
| 2676 | if (ctrl->bRequestType != USB_DIR_IN) |
| 2677 | goto unknown; |
| 2678 | |
| 2679 | switch (w_value >> 8) { |
| 2680 | |
| 2681 | case USB_DT_DEVICE: |
| 2682 | cdev->desc.bNumConfigurations = |
| 2683 | count_configs(cdev, USB_DT_DEVICE); |
| 2684 | cdev->desc.bMaxPacketSize0 = |
| 2685 | cdev->gadget->ep0->maxpacket; |
| 2686 | if (gadget_is_superspeed(gadget)) { |
| 2687 | if (gadget->speed >= USB_SPEED_SUPER) { |
| 2688 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
| 2689 | cdev->desc.bMaxPacketSize0 = 9; |
| 2690 | } else { |
| 2691 | cdev->desc.bcdUSB = cpu_to_le16(0x0210); |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | value = min(w_length, (u16) sizeof cdev->desc); |
| 2696 | memcpy(req->buf, &cdev->desc, value); |
| 2697 | break; |
| 2698 | case USB_DT_DEVICE_QUALIFIER: |
| 2699 | if (!gadget_is_dualspeed(gadget) || |
| 2700 | gadget->speed >= USB_SPEED_SUPER) |
| 2701 | break; |
| 2702 | device_qual(cdev); |
| 2703 | value = min_t(int, w_length, |
| 2704 | sizeof(struct usb_qualifier_descriptor)); |
| 2705 | break; |
| 2706 | case USB_DT_OTHER_SPEED_CONFIG: |
| 2707 | if (!gadget_is_dualspeed(gadget) || |
| 2708 | gadget->speed >= USB_SPEED_SUPER) |
| 2709 | break; |
| 2710 | /* FALLTHROUGH */ |
| 2711 | case USB_DT_CONFIG: |
| 2712 | value = config_desc_u3debug(cdev, w_value); |
| 2713 | if (value >= 0) |
| 2714 | value = min(w_length, (u16) value); |
| 2715 | break; |
| 2716 | case USB_DT_STRING: |
| 2717 | value = get_string(cdev, req->buf, |
| 2718 | w_index, w_value & 0xff); |
| 2719 | if (value >= 0) |
| 2720 | value = min(w_length, (u16) value); |
| 2721 | break; |
| 2722 | case USB_DT_BOS: |
| 2723 | if (gadget_is_superspeed(gadget)) { |
| 2724 | value = bos_desc(cdev); |
| 2725 | value = min(w_length, (u16) value); |
| 2726 | } |
| 2727 | break; |
| 2728 | } |
| 2729 | break; |
| 2730 | |
| 2731 | /* any number of configs can work */ |
| 2732 | case USB_REQ_SET_CONFIGURATION: |
| 2733 | if (ctrl->bRequestType != 0) |
| 2734 | goto unknown; |
| 2735 | if (gadget_is_otg(gadget)) { |
| 2736 | if (gadget->a_hnp_support) |
| 2737 | DBG(cdev, "HNP available\n"); |
| 2738 | else if (gadget->a_alt_hnp_support) |
| 2739 | DBG(cdev, "HNP on another port\n"); |
| 2740 | else |
| 2741 | VDBG(cdev, "HNP inactive\n"); |
| 2742 | } |
| 2743 | spin_lock(&cdev->lock); |
| 2744 | value = set_config_u3debug(cdev, ctrl, w_value); |
| 2745 | spin_unlock(&cdev->lock); |
| 2746 | break; |
| 2747 | case USB_REQ_GET_CONFIGURATION: |
| 2748 | if (ctrl->bRequestType != USB_DIR_IN) |
| 2749 | goto unknown; |
| 2750 | if (cdev->config) |
| 2751 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 2752 | else |
| 2753 | *(u8 *)req->buf = 0; |
| 2754 | value = min(w_length, (u16) 1); |
| 2755 | break; |
| 2756 | |
| 2757 | /* function drivers must handle get/set altsetting; if there's |
| 2758 | * no get() method, we know only altsetting zero works. |
| 2759 | */ |
| 2760 | case USB_REQ_SET_INTERFACE: |
| 2761 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 2762 | goto unknown; |
| 2763 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2764 | break; |
| 2765 | |
| 2766 | f = cdev->config->interface[intf]; |
| 2767 | if (!f) |
| 2768 | break; |
| 2769 | if (w_value && !f->set_alt) |
| 2770 | break; |
| 2771 | value = f->set_alt(f, w_index, w_value); |
| 2772 | if (value == USB_GADGET_DELAYED_STATUS) { |
| 2773 | DBG(cdev, |
| 2774 | "%s: interface %d (%s) requested delayed status\n", |
| 2775 | __func__, intf, f->name); |
| 2776 | cdev->delayed_status++; |
| 2777 | DBG(cdev, "delayed_status count %d\n", |
| 2778 | cdev->delayed_status); |
| 2779 | } |
| 2780 | break; |
| 2781 | case USB_REQ_GET_INTERFACE: |
| 2782 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 2783 | goto unknown; |
| 2784 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2785 | break; |
| 2786 | f = cdev->config->interface[intf]; |
| 2787 | if (!f) |
| 2788 | break; |
| 2789 | /* lots of interfaces only need altsetting zero... */ |
| 2790 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 2791 | if (value < 0) |
| 2792 | break; |
| 2793 | *((u8 *)req->buf) = value; |
| 2794 | value = min(w_length, (u16) 1); |
| 2795 | break; |
| 2796 | |
| 2797 | /* |
| 2798 | * USB 3.0 additions: |
| 2799 | * Function driver should handle get_status request. If such cb |
| 2800 | * wasn't supplied we respond with default value = 0 |
| 2801 | * Note: function driver should supply such cb only for the first |
| 2802 | * interface of the function |
| 2803 | */ |
| 2804 | case USB_REQ_GET_STATUS: |
| 2805 | if (!gadget_is_superspeed(gadget)) |
| 2806 | goto unknown; |
| 2807 | if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) |
| 2808 | goto unknown; |
| 2809 | value = 2; /* This is the length of the get_status reply */ |
| 2810 | put_unaligned_le16(0, req->buf); |
| 2811 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2812 | break; |
| 2813 | f = cdev->config->interface[intf]; |
| 2814 | if (!f) |
| 2815 | break; |
| 2816 | status = f->get_status ? f->get_status(f) : 0; |
| 2817 | if (status < 0) |
| 2818 | break; |
| 2819 | put_unaligned_le16(status & 0x0000ffff, req->buf); |
| 2820 | break; |
| 2821 | /* |
| 2822 | * Function drivers should handle SetFeature/ClearFeature |
| 2823 | * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied |
| 2824 | * only for the first interface of the function |
| 2825 | */ |
| 2826 | case USB_REQ_CLEAR_FEATURE: |
| 2827 | case USB_REQ_SET_FEATURE: |
| 2828 | if (!gadget_is_superspeed(gadget)) |
| 2829 | goto unknown; |
| 2830 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) |
| 2831 | goto unknown; |
| 2832 | switch (w_value) { |
| 2833 | case USB_INTRF_FUNC_SUSPEND: |
| 2834 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2835 | break; |
| 2836 | f = cdev->config->interface[intf]; |
| 2837 | if (!f) |
| 2838 | break; |
| 2839 | value = 0; |
| 2840 | if (f->func_suspend) |
| 2841 | value = f->func_suspend(f, w_index >> 8); |
| 2842 | if (value < 0) { |
| 2843 | ERROR(cdev, |
| 2844 | "func_suspend() returned error %d\n", |
| 2845 | value); |
| 2846 | value = 0; |
| 2847 | } |
| 2848 | break; |
| 2849 | } |
| 2850 | break; |
| 2851 | default: |
| 2852 | unknown: |
| 2853 | VDBG(cdev, |
| 2854 | "non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 2855 | ctrl->bRequestType, ctrl->bRequest, |
| 2856 | w_value, w_index, w_length); |
| 2857 | |
| 2858 | /* functions always handle their interfaces and endpoints... |
| 2859 | * punt other recipients (other, WUSB, ...) to the current |
| 2860 | * configuration code. |
| 2861 | * |
| 2862 | * REVISIT it could make sense to let the composite device |
| 2863 | * take such requests too, if that's ever needed: to work |
| 2864 | * in config 0, etc. |
| 2865 | */ |
| 2866 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 2867 | case USB_RECIP_INTERFACE: |
| 2868 | if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) |
| 2869 | break; |
| 2870 | f = cdev->config->interface[intf]; |
| 2871 | break; |
| 2872 | |
| 2873 | case USB_RECIP_ENDPOINT: |
| 2874 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 2875 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 2876 | if (test_bit(endp, f->endpoints)) |
| 2877 | break; |
| 2878 | } |
| 2879 | if (&f->list == &cdev->config->functions) |
| 2880 | f = NULL; |
| 2881 | break; |
| 2882 | } |
| 2883 | |
| 2884 | if (f && f->setup) |
| 2885 | value = f->setup(f, ctrl); |
| 2886 | else { |
| 2887 | struct usb_configuration *c; |
| 2888 | |
| 2889 | c = cdev->config; |
| 2890 | if (c && c->setup) |
| 2891 | value = c->setup(c, ctrl); |
| 2892 | } |
| 2893 | |
| 2894 | goto done; |
| 2895 | } |
| 2896 | |
| 2897 | /* respond with data transfer before status phase? */ |
| 2898 | if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { |
| 2899 | req->length = value; |
| 2900 | req->zero = value < w_length; |
| 2901 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 2902 | if (value < 0) { |
| 2903 | DBG(cdev, "ep_queue --> %d\n", value); |
| 2904 | req->status = 0; |
| 2905 | composite_setup_complete(gadget->ep0, req); |
| 2906 | } |
| 2907 | } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { |
| 2908 | WARN(cdev, |
| 2909 | "%s: Delayed status not supported for w_length != 0", |
| 2910 | __func__); |
| 2911 | } |
| 2912 | |
| 2913 | done: |
| 2914 | /* device either stalls (value < 0) or reports success */ |
| 2915 | return value; |
| 2916 | } |
| 2917 | #endif |
| 2918 | void composite_disconnect(struct usb_gadget *gadget) |
| 2919 | { |
| 2920 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2921 | unsigned long flags; |
| 2922 | |
| 2923 | /* REVISIT: should we have config and device level |
| 2924 | * disconnect callbacks? |
| 2925 | */ |
| 2926 | spin_lock_irqsave(&cdev->lock, flags); |
| 2927 | cdev->suspended = 0; |
| 2928 | if (cdev->config) |
| 2929 | reset_config(cdev); |
| 2930 | if (cdev->driver->disconnect) |
| 2931 | cdev->driver->disconnect(cdev); |
| 2932 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 2933 | } |
| 2934 | |
| 2935 | /*-------------------------------------------------------------------------*/ |
| 2936 | |
| 2937 | static ssize_t suspended_show(struct device *dev, struct device_attribute *attr, |
| 2938 | char *buf) |
| 2939 | { |
| 2940 | struct usb_gadget *gadget = dev_to_usb_gadget(dev); |
| 2941 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2942 | |
| 2943 | return sprintf(buf, "%d\n", cdev->suspended); |
| 2944 | } |
| 2945 | static DEVICE_ATTR_RO(suspended); |
| 2946 | |
| 2947 | static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver) |
| 2948 | { |
| 2949 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 2950 | struct usb_gadget_strings *gstr = cdev->driver->strings[0]; |
| 2951 | struct usb_string *dev_str = gstr->strings; |
| 2952 | |
| 2953 | /* composite_disconnect() must already have been called |
| 2954 | * by the underlying peripheral controller driver! |
| 2955 | * so there's no i/o concurrency that could affect the |
| 2956 | * state protected by cdev->lock. |
| 2957 | */ |
| 2958 | WARN_ON(cdev->config); |
| 2959 | |
| 2960 | while (!list_empty(&cdev->configs)) { |
| 2961 | struct usb_configuration *c; |
| 2962 | c = list_first_entry(&cdev->configs, |
| 2963 | struct usb_configuration, list); |
| 2964 | remove_config(cdev, c); |
| 2965 | } |
| 2966 | if (cdev->driver->unbind && unbind_driver) |
| 2967 | cdev->driver->unbind(cdev); |
| 2968 | |
| 2969 | composite_dev_cleanup(cdev); |
| 2970 | |
| 2971 | if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer) |
| 2972 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = ""; |
| 2973 | |
| 2974 | kfree(cdev->def_manufacturer); |
| 2975 | kfree(cdev); |
| 2976 | set_gadget_data(gadget, NULL); |
| 2977 | } |
| 2978 | |
| 2979 | static void composite_unbind(struct usb_gadget *gadget) |
| 2980 | { |
| 2981 | __composite_unbind(gadget, true); |
| 2982 | } |
| 2983 | |
| 2984 | static void update_unchanged_dev_desc(struct usb_device_descriptor *new, |
| 2985 | const struct usb_device_descriptor *old) |
| 2986 | { |
| 2987 | __le16 idVendor; |
| 2988 | __le16 idProduct; |
| 2989 | __le16 bcdDevice; |
| 2990 | u8 iSerialNumber; |
| 2991 | u8 iManufacturer; |
| 2992 | u8 iProduct; |
| 2993 | |
| 2994 | /* |
| 2995 | * these variables may have been set in |
| 2996 | * usb_composite_overwrite_options() |
| 2997 | */ |
| 2998 | idVendor = new->idVendor; |
| 2999 | idProduct = new->idProduct; |
| 3000 | bcdDevice = new->bcdDevice; |
| 3001 | iSerialNumber = new->iSerialNumber; |
| 3002 | iManufacturer = new->iManufacturer; |
| 3003 | iProduct = new->iProduct; |
| 3004 | |
| 3005 | *new = *old; |
| 3006 | if (idVendor) |
| 3007 | new->idVendor = idVendor; |
| 3008 | if (idProduct) |
| 3009 | new->idProduct = idProduct; |
| 3010 | if (bcdDevice) |
| 3011 | new->bcdDevice = bcdDevice; |
| 3012 | else |
| 3013 | new->bcdDevice = cpu_to_le16(get_default_bcdDevice()); |
| 3014 | if (iSerialNumber) |
| 3015 | new->iSerialNumber = iSerialNumber; |
| 3016 | if (iManufacturer) |
| 3017 | new->iManufacturer = iManufacturer; |
| 3018 | if (iProduct) |
| 3019 | new->iProduct = iProduct; |
| 3020 | } |
| 3021 | |
| 3022 | int composite_dev_prepare(struct usb_composite_driver *composite, |
| 3023 | struct usb_composite_dev *cdev) |
| 3024 | { |
| 3025 | struct usb_gadget *gadget = cdev->gadget; |
| 3026 | int ret = -ENOMEM; |
| 3027 | |
| 3028 | /* preallocate control response and buffer */ |
| 3029 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 3030 | if (!cdev->req) |
| 3031 | return -ENOMEM; |
| 3032 | |
| 3033 | cdev->req->buf = kzalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL); |
| 3034 | if (!cdev->req->buf) |
| 3035 | goto fail; |
| 3036 | |
| 3037 | ret = device_create_file(&gadget->dev, &dev_attr_suspended); |
| 3038 | if (ret) |
| 3039 | goto fail_dev; |
| 3040 | |
| 3041 | cdev->req->complete = composite_setup_complete; |
| 3042 | cdev->req->context = cdev; |
| 3043 | gadget->ep0->driver_data = cdev; |
| 3044 | |
| 3045 | cdev->driver = composite; |
| 3046 | |
| 3047 | /* |
| 3048 | * As per USB compliance update, a device that is actively drawing |
| 3049 | * more than 100mA from USB must report itself as bus-powered in |
| 3050 | * the GetStatus(DEVICE) call. |
| 3051 | */ |
| 3052 | if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) |
| 3053 | usb_gadget_set_selfpowered(gadget); |
| 3054 | |
| 3055 | /* interface and string IDs start at zero via kzalloc. |
| 3056 | * we force endpoints to start unassigned; few controller |
| 3057 | * drivers will zero ep->driver_data. |
| 3058 | */ |
| 3059 | usb_ep_autoconfig_reset(gadget); |
| 3060 | return 0; |
| 3061 | fail_dev: |
| 3062 | kfree(cdev->req->buf); |
| 3063 | fail: |
| 3064 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 3065 | cdev->req = NULL; |
| 3066 | return ret; |
| 3067 | } |
| 3068 | |
| 3069 | int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, |
| 3070 | struct usb_ep *ep0) |
| 3071 | { |
| 3072 | int ret = 0; |
| 3073 | |
| 3074 | cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL); |
| 3075 | if (!cdev->os_desc_req) { |
| 3076 | ret = -ENOMEM; |
| 3077 | goto end; |
| 3078 | } |
| 3079 | |
| 3080 | cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ, |
| 3081 | GFP_KERNEL); |
| 3082 | if (!cdev->os_desc_req->buf) { |
| 3083 | ret = -ENOMEM; |
| 3084 | usb_ep_free_request(ep0, cdev->os_desc_req); |
| 3085 | goto end; |
| 3086 | } |
| 3087 | cdev->os_desc_req->context = cdev; |
| 3088 | cdev->os_desc_req->complete = composite_setup_complete; |
| 3089 | end: |
| 3090 | return ret; |
| 3091 | } |
| 3092 | |
| 3093 | void composite_dev_cleanup(struct usb_composite_dev *cdev) |
| 3094 | { |
| 3095 | struct usb_gadget_string_container *uc, *tmp; |
| 3096 | struct usb_ep *ep, *tmp_ep; |
| 3097 | |
| 3098 | list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) { |
| 3099 | list_del(&uc->list); |
| 3100 | kfree(uc); |
| 3101 | } |
| 3102 | if (cdev->os_desc_req) { |
| 3103 | if (cdev->os_desc_pending) |
| 3104 | usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req); |
| 3105 | |
| 3106 | kfree(cdev->os_desc_req->buf); |
| 3107 | cdev->os_desc_req->buf = NULL; |
| 3108 | usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req); |
| 3109 | cdev->os_desc_req = NULL; |
| 3110 | } |
| 3111 | if (cdev->req) { |
| 3112 | if (cdev->setup_pending) |
| 3113 | usb_ep_dequeue(cdev->gadget->ep0, cdev->req); |
| 3114 | |
| 3115 | kfree(cdev->req->buf); |
| 3116 | cdev->req->buf = NULL; |
| 3117 | usb_ep_free_request(cdev->gadget->ep0, cdev->req); |
| 3118 | cdev->req = NULL; |
| 3119 | } |
| 3120 | cdev->next_string_id = 0; |
| 3121 | device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); |
| 3122 | |
| 3123 | /* |
| 3124 | * Some UDC backends have a dynamic EP allocation scheme. |
| 3125 | * |
| 3126 | * In that case, the dispose() callback is used to notify the |
| 3127 | * backend that the EPs are no longer in use. |
| 3128 | * |
| 3129 | * Note: The UDC backend can remove the EP from the ep_list as |
| 3130 | * a result, so we need to use the _safe list iterator. |
| 3131 | */ |
| 3132 | list_for_each_entry_safe(ep, tmp_ep, |
| 3133 | &cdev->gadget->ep_list, ep_list) { |
| 3134 | if (ep->ops->dispose) |
| 3135 | ep->ops->dispose(ep); |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | static int composite_bind(struct usb_gadget *gadget, |
| 3140 | struct usb_gadget_driver *gdriver) |
| 3141 | { |
| 3142 | struct usb_composite_dev *cdev; |
| 3143 | struct usb_composite_driver *composite = to_cdriver(gdriver); |
| 3144 | int status = -ENOMEM; |
| 3145 | |
| 3146 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); |
| 3147 | if (!cdev) |
| 3148 | return status; |
| 3149 | |
| 3150 | spin_lock_init(&cdev->lock); |
| 3151 | cdev->gadget = gadget; |
| 3152 | set_gadget_data(gadget, cdev); |
| 3153 | INIT_LIST_HEAD(&cdev->configs); |
| 3154 | INIT_LIST_HEAD(&cdev->gstrings); |
| 3155 | |
| 3156 | status = composite_dev_prepare(composite, cdev); |
| 3157 | if (status) |
| 3158 | goto fail; |
| 3159 | |
| 3160 | /* composite gadget needs to assign strings for whole device (like |
| 3161 | * serial number), register function drivers, potentially update |
| 3162 | * power state and consumption, etc |
| 3163 | */ |
| 3164 | status = composite->bind(cdev); |
| 3165 | if (status < 0) |
| 3166 | goto fail; |
| 3167 | |
| 3168 | if (cdev->use_os_string) { |
| 3169 | status = composite_os_desc_req_prepare(cdev, gadget->ep0); |
| 3170 | if (status) |
| 3171 | goto fail; |
| 3172 | } |
| 3173 | |
| 3174 | update_unchanged_dev_desc(&cdev->desc, composite->dev); |
| 3175 | |
| 3176 | /* has userspace failed to provide a serial number? */ |
| 3177 | if (composite->needs_serial && !cdev->desc.iSerialNumber) |
| 3178 | WARNING(cdev, "userspace failed to provide iSerialNumber\n"); |
| 3179 | |
| 3180 | INFO(cdev, "%s ready\n", composite->name); |
| 3181 | return 0; |
| 3182 | |
| 3183 | fail: |
| 3184 | __composite_unbind(gadget, false); |
| 3185 | return status; |
| 3186 | } |
| 3187 | |
| 3188 | /*-------------------------------------------------------------------------*/ |
| 3189 | |
| 3190 | void composite_suspend(struct usb_gadget *gadget) |
| 3191 | { |
| 3192 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 3193 | struct usb_function *f; |
| 3194 | |
| 3195 | /* REVISIT: should we have config level |
| 3196 | * suspend/resume callbacks? |
| 3197 | */ |
| 3198 | DBG(cdev, "suspend\n"); |
| 3199 | if (cdev->config) { |
| 3200 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 3201 | if (f->suspend) |
| 3202 | f->suspend(f); |
| 3203 | } |
| 3204 | } |
| 3205 | if (cdev->driver->suspend) |
| 3206 | cdev->driver->suspend(cdev); |
| 3207 | |
| 3208 | cdev->suspended = 1; |
| 3209 | |
| 3210 | usb_gadget_set_selfpowered(gadget); |
| 3211 | usb_gadget_vbus_draw(gadget, 2); |
| 3212 | } |
| 3213 | |
| 3214 | void composite_resume(struct usb_gadget *gadget) |
| 3215 | { |
| 3216 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 3217 | struct usb_function *f; |
| 3218 | unsigned maxpower; |
| 3219 | |
| 3220 | /* REVISIT: should we have config level |
| 3221 | * suspend/resume callbacks? |
| 3222 | */ |
| 3223 | DBG(cdev, "resume\n"); |
| 3224 | if (cdev->driver->resume) |
| 3225 | cdev->driver->resume(cdev); |
| 3226 | if (cdev->config) { |
| 3227 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 3228 | if (f->resume) |
| 3229 | f->resume(f); |
| 3230 | } |
| 3231 | |
| 3232 | maxpower = cdev->config->MaxPower ? |
| 3233 | cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW; |
| 3234 | if (gadget->speed < USB_SPEED_SUPER) |
| 3235 | maxpower = min(maxpower, 500U); |
| 3236 | else |
| 3237 | maxpower = min(maxpower, 900U); |
| 3238 | |
| 3239 | if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW) |
| 3240 | usb_gadget_clear_selfpowered(gadget); |
| 3241 | |
| 3242 | usb_gadget_vbus_draw(gadget, maxpower); |
| 3243 | } |
| 3244 | |
| 3245 | cdev->suspended = 0; |
| 3246 | } |
| 3247 | |
| 3248 | /*-------------------------------------------------------------------------*/ |
| 3249 | |
| 3250 | static const struct usb_gadget_driver composite_driver_template = { |
| 3251 | .bind = composite_bind, |
| 3252 | .unbind = composite_unbind, |
| 3253 | |
| 3254 | .setup = composite_setup, |
| 3255 | .reset = composite_disconnect, |
| 3256 | .disconnect = composite_disconnect, |
| 3257 | |
| 3258 | .suspend = composite_suspend, |
| 3259 | .resume = composite_resume, |
| 3260 | |
| 3261 | .driver = { |
| 3262 | .owner = THIS_MODULE, |
| 3263 | }, |
| 3264 | }; |
| 3265 | |
| 3266 | #ifdef CONFIG_USB_MV_HSIC_UDC |
| 3267 | static const struct usb_gadget_driver composite_hsic_driver_template = { |
| 3268 | .bind = composite_bind, |
| 3269 | .unbind = composite_unbind, |
| 3270 | |
| 3271 | .setup = composite_setup_hsic, |
| 3272 | .reset = composite_disconnect, |
| 3273 | .disconnect = composite_disconnect, |
| 3274 | |
| 3275 | .suspend = composite_suspend, |
| 3276 | .resume = composite_resume, |
| 3277 | |
| 3278 | .driver = { |
| 3279 | .owner = THIS_MODULE, |
| 3280 | }, |
| 3281 | .is_hsic = 1, |
| 3282 | }; |
| 3283 | #endif |
| 3284 | |
| 3285 | #ifdef CONFIG_USB_TELEPHONY |
| 3286 | static const struct usb_gadget_driver composite_u3debug_driver_template = { |
| 3287 | .bind = composite_bind, |
| 3288 | .unbind = composite_unbind, |
| 3289 | |
| 3290 | .setup = composite_setup_u3debug, |
| 3291 | .reset = composite_disconnect, |
| 3292 | .disconnect = composite_disconnect, |
| 3293 | |
| 3294 | .suspend = composite_suspend, |
| 3295 | .resume = composite_resume, |
| 3296 | |
| 3297 | .driver = { |
| 3298 | .owner = THIS_MODULE, |
| 3299 | }, |
| 3300 | .is_u3_drv = 1, |
| 3301 | }; |
| 3302 | #endif |
| 3303 | |
| 3304 | |
| 3305 | /** |
| 3306 | * usb_composite_probe() - register a composite driver |
| 3307 | * @driver: the driver to register |
| 3308 | * |
| 3309 | * Context: single threaded during gadget setup |
| 3310 | * |
| 3311 | * This function is used to register drivers using the composite driver |
| 3312 | * framework. The return value is zero, or a negative errno value. |
| 3313 | * Those values normally come from the driver's @bind method, which does |
| 3314 | * all the work of setting up the driver to match the hardware. |
| 3315 | * |
| 3316 | * On successful return, the gadget is ready to respond to requests from |
| 3317 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 3318 | * while it was binding. That would usually be done in order to wait for |
| 3319 | * some userspace participation. |
| 3320 | */ |
| 3321 | int usb_composite_probe(struct usb_composite_driver *driver) |
| 3322 | { |
| 3323 | struct usb_gadget_driver *gadget_driver; |
| 3324 | |
| 3325 | if (!driver || !driver->dev || !driver->bind) |
| 3326 | return -EINVAL; |
| 3327 | |
| 3328 | if (!driver->name) |
| 3329 | driver->name = "composite"; |
| 3330 | |
| 3331 | driver->gadget_driver = composite_driver_template; |
| 3332 | gadget_driver = &driver->gadget_driver; |
| 3333 | |
| 3334 | gadget_driver->function = (char *) driver->name; |
| 3335 | gadget_driver->driver.name = driver->name; |
| 3336 | gadget_driver->max_speed = driver->max_speed; |
| 3337 | |
| 3338 | return usb_gadget_probe_driver(gadget_driver); |
| 3339 | } |
| 3340 | EXPORT_SYMBOL_GPL(usb_composite_probe); |
| 3341 | |
| 3342 | #ifdef CONFIG_USB_MV_HSIC_UDC |
| 3343 | int usb_composite_probe_hsic(struct usb_composite_driver *driver) |
| 3344 | { |
| 3345 | struct usb_gadget_driver *gadget_driver; |
| 3346 | |
| 3347 | if (!driver || !driver->dev || !driver->bind) |
| 3348 | return -EINVAL; |
| 3349 | |
| 3350 | if (!driver->name) |
| 3351 | driver->name = "composite"; |
| 3352 | |
| 3353 | driver->gadget_driver = composite_hsic_driver_template; |
| 3354 | gadget_driver = &driver->gadget_driver; |
| 3355 | |
| 3356 | gadget_driver->function = (char *) driver->name; |
| 3357 | gadget_driver->driver.name = driver->name; |
| 3358 | gadget_driver->max_speed = driver->max_speed; |
| 3359 | |
| 3360 | return usb_gadget_probe_driver(gadget_driver); |
| 3361 | } |
| 3362 | EXPORT_SYMBOL_GPL(usb_composite_probe_hsic); |
| 3363 | #endif |
| 3364 | |
| 3365 | #ifdef CONFIG_USB_TELEPHONY |
| 3366 | int usb_composite_probe_u3debug(struct usb_composite_driver *driver) |
| 3367 | { |
| 3368 | struct usb_gadget_driver *gadget_driver; |
| 3369 | |
| 3370 | if (!driver || !driver->dev || !driver->bind) |
| 3371 | return -EINVAL; |
| 3372 | |
| 3373 | if (!driver->name) |
| 3374 | driver->name = "composite"; |
| 3375 | |
| 3376 | driver->gadget_driver = composite_u3debug_driver_template; |
| 3377 | gadget_driver = &driver->gadget_driver; |
| 3378 | |
| 3379 | gadget_driver->function = (char *) driver->name; |
| 3380 | gadget_driver->driver.name = driver->name; |
| 3381 | gadget_driver->max_speed = driver->max_speed; |
| 3382 | |
| 3383 | return usb_gadget_probe_driver(gadget_driver); |
| 3384 | } |
| 3385 | EXPORT_SYMBOL_GPL(usb_composite_probe_u3debug); |
| 3386 | #endif |
| 3387 | |
| 3388 | |
| 3389 | /** |
| 3390 | * usb_composite_unregister() - unregister a composite driver |
| 3391 | * @driver: the driver to unregister |
| 3392 | * |
| 3393 | * This function is used to unregister drivers using the composite |
| 3394 | * driver framework. |
| 3395 | */ |
| 3396 | void usb_composite_unregister(struct usb_composite_driver *driver) |
| 3397 | { |
| 3398 | usb_gadget_unregister_driver(&driver->gadget_driver); |
| 3399 | } |
| 3400 | EXPORT_SYMBOL_GPL(usb_composite_unregister); |
| 3401 | |
| 3402 | /** |
| 3403 | * usb_composite_setup_continue() - Continue with the control transfer |
| 3404 | * @cdev: the composite device who's control transfer was kept waiting |
| 3405 | * |
| 3406 | * This function must be called by the USB function driver to continue |
| 3407 | * with the control transfer's data/status stage in case it had requested to |
| 3408 | * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) |
| 3409 | * can request the composite framework to delay the setup request's data/status |
| 3410 | * stages by returning USB_GADGET_DELAYED_STATUS. |
| 3411 | */ |
| 3412 | void usb_composite_setup_continue(struct usb_composite_dev *cdev) |
| 3413 | { |
| 3414 | int value; |
| 3415 | struct usb_request *req = cdev->req; |
| 3416 | unsigned long flags; |
| 3417 | |
| 3418 | DBG(cdev, "%s\n", __func__); |
| 3419 | spin_lock_irqsave(&cdev->lock, flags); |
| 3420 | |
| 3421 | if (cdev->delayed_status == 0) { |
| 3422 | WARN(cdev, "%s: Unexpected call\n", __func__); |
| 3423 | |
| 3424 | } else if (--cdev->delayed_status == 0) { |
| 3425 | DBG(cdev, "%s: Completing delayed status\n", __func__); |
| 3426 | req->length = 0; |
| 3427 | req->context = cdev; |
| 3428 | value = composite_ep0_queue(cdev, req, GFP_ATOMIC); |
| 3429 | if (value < 0) { |
| 3430 | DBG(cdev, "ep_queue --> %d\n", value); |
| 3431 | req->status = 0; |
| 3432 | composite_setup_complete(cdev->gadget->ep0, req); |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 3437 | } |
| 3438 | EXPORT_SYMBOL_GPL(usb_composite_setup_continue); |
| 3439 | |
| 3440 | static char *composite_default_mfr(struct usb_gadget *gadget) |
| 3441 | { |
| 3442 | return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname, |
| 3443 | init_utsname()->release, gadget->name); |
| 3444 | } |
| 3445 | |
| 3446 | void usb_composite_overwrite_options(struct usb_composite_dev *cdev, |
| 3447 | struct usb_composite_overwrite *covr) |
| 3448 | { |
| 3449 | struct usb_device_descriptor *desc = &cdev->desc; |
| 3450 | struct usb_gadget_strings *gstr = cdev->driver->strings[0]; |
| 3451 | struct usb_string *dev_str = gstr->strings; |
| 3452 | |
| 3453 | if (covr->idVendor) |
| 3454 | desc->idVendor = cpu_to_le16(covr->idVendor); |
| 3455 | |
| 3456 | if (covr->idProduct) |
| 3457 | desc->idProduct = cpu_to_le16(covr->idProduct); |
| 3458 | |
| 3459 | if (covr->bcdDevice) |
| 3460 | desc->bcdDevice = cpu_to_le16(covr->bcdDevice); |
| 3461 | |
| 3462 | if (covr->serial_number) { |
| 3463 | desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id; |
| 3464 | dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number; |
| 3465 | } |
| 3466 | if (covr->manufacturer) { |
| 3467 | desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; |
| 3468 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer; |
| 3469 | |
| 3470 | } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) { |
| 3471 | desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; |
| 3472 | cdev->def_manufacturer = composite_default_mfr(cdev->gadget); |
| 3473 | dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer; |
| 3474 | } |
| 3475 | |
| 3476 | if (covr->product) { |
| 3477 | desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id; |
| 3478 | dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product; |
| 3479 | } |
| 3480 | } |
| 3481 | EXPORT_SYMBOL_GPL(usb_composite_overwrite_options); |
| 3482 | |
| 3483 | MODULE_LICENSE("GPL"); |
| 3484 | MODULE_AUTHOR("David Brownell"); |