Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 2 | /* |
| 3 | * composite.c - infrastructure for Composite USB Gadgets |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 David Brownell |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 6 | * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com> |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 7 | */ |
| 8 | #undef DEBUG |
| 9 | |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 10 | #include <dm/devres.h> |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 11 | #include <linux/bitops.h> |
| 12 | #include <linux/usb/composite.h> |
| 13 | |
| 14 | #define USB_BUFSIZ 4096 |
| 15 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 16 | /* Helper type for accessing packed u16 pointers */ |
| 17 | typedef struct { __le16 val; } __packed __le16_packed; |
| 18 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 19 | static struct usb_composite_driver *composite; |
| 20 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 21 | static inline void le16_add_cpu_packed(__le16_packed *var, u16 val) |
| 22 | { |
| 23 | var->val = cpu_to_le16(le16_to_cpu(var->val) + val); |
| 24 | } |
| 25 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 26 | /** |
| 27 | * usb_add_function() - add a function to a configuration |
| 28 | * @config: the configuration |
| 29 | * @function: the function being added |
| 30 | * Context: single threaded during gadget setup |
| 31 | * |
| 32 | * After initialization, each configuration must have one or more |
| 33 | * functions added to it. Adding a function involves calling its @bind() |
| 34 | * method to allocate resources such as interface and string identifiers |
| 35 | * and endpoints. |
| 36 | * |
| 37 | * This function returns the value of the function's bind(), which is |
| 38 | * zero for success else a negative errno value. |
| 39 | */ |
| 40 | int usb_add_function(struct usb_configuration *config, |
| 41 | struct usb_function *function) |
| 42 | { |
| 43 | int value = -EINVAL; |
| 44 | |
| 45 | debug("adding '%s'/%p to config '%s'/%p\n", |
| 46 | function->name, function, |
| 47 | config->label, config); |
| 48 | |
| 49 | if (!function->set_alt || !function->disable) |
| 50 | goto done; |
| 51 | |
| 52 | function->config = config; |
| 53 | list_add_tail(&function->list, &config->functions); |
| 54 | |
| 55 | if (function->bind) { |
| 56 | value = function->bind(config, function); |
| 57 | if (value < 0) { |
| 58 | list_del(&function->list); |
| 59 | function->config = NULL; |
| 60 | } |
| 61 | } else |
| 62 | value = 0; |
| 63 | |
| 64 | if (!config->fullspeed && function->descriptors) |
| 65 | config->fullspeed = 1; |
| 66 | if (!config->highspeed && function->hs_descriptors) |
| 67 | config->highspeed = 1; |
| 68 | |
| 69 | done: |
| 70 | if (value) |
| 71 | debug("adding '%s'/%p --> %d\n", |
| 72 | function->name, function, value); |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * usb_function_deactivate - prevent function and gadget enumeration |
| 78 | * @function: the function that isn't yet ready to respond |
| 79 | * |
| 80 | * Blocks response of the gadget driver to host enumeration by |
| 81 | * preventing the data line pullup from being activated. This is |
| 82 | * normally called during @bind() processing to change from the |
| 83 | * initial "ready to respond" state, or when a required resource |
| 84 | * becomes available. |
| 85 | * |
| 86 | * For example, drivers that serve as a passthrough to a userspace |
| 87 | * daemon can block enumeration unless that daemon (such as an OBEX, |
| 88 | * MTP, or print server) is ready to handle host requests. |
| 89 | * |
| 90 | * Not all systems support software control of their USB peripheral |
| 91 | * data pullups. |
| 92 | * |
| 93 | * Returns zero on success, else negative errno. |
| 94 | */ |
| 95 | int usb_function_deactivate(struct usb_function *function) |
| 96 | { |
| 97 | struct usb_composite_dev *cdev = function->config->cdev; |
| 98 | int status = 0; |
| 99 | |
| 100 | if (cdev->deactivations == 0) |
| 101 | status = usb_gadget_disconnect(cdev->gadget); |
| 102 | if (status == 0) |
| 103 | cdev->deactivations++; |
| 104 | |
| 105 | return status; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * usb_function_activate - allow function and gadget enumeration |
| 110 | * @function: function on which usb_function_activate() was called |
| 111 | * |
| 112 | * Reverses effect of usb_function_deactivate(). If no more functions |
| 113 | * are delaying their activation, the gadget driver will respond to |
| 114 | * host enumeration procedures. |
| 115 | * |
| 116 | * Returns zero on success, else negative errno. |
| 117 | */ |
| 118 | int usb_function_activate(struct usb_function *function) |
| 119 | { |
| 120 | struct usb_composite_dev *cdev = function->config->cdev; |
| 121 | int status = 0; |
| 122 | |
| 123 | if (cdev->deactivations == 0) |
| 124 | status = -EINVAL; |
| 125 | else { |
| 126 | cdev->deactivations--; |
| 127 | if (cdev->deactivations == 0) |
| 128 | status = usb_gadget_connect(cdev->gadget); |
| 129 | } |
| 130 | |
| 131 | return status; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * usb_interface_id() - allocate an unused interface ID |
| 136 | * @config: configuration associated with the interface |
| 137 | * @function: function handling the interface |
| 138 | * Context: single threaded during gadget setup |
| 139 | * |
| 140 | * usb_interface_id() is called from usb_function.bind() callbacks to |
| 141 | * allocate new interface IDs. The function driver will then store that |
| 142 | * ID in interface, association, CDC union, and other descriptors. It |
| 143 | * will also handle any control requests targetted at that interface, |
| 144 | * particularly changing its altsetting via set_alt(). There may |
| 145 | * also be class-specific or vendor-specific requests to handle. |
| 146 | * |
| 147 | * All interface identifier should be allocated using this routine, to |
| 148 | * ensure that for example different functions don't wrongly assign |
| 149 | * different meanings to the same identifier. Note that since interface |
| 150 | * identifers are configuration-specific, functions used in more than |
| 151 | * one configuration (or more than once in a given configuration) need |
| 152 | * multiple versions of the relevant descriptors. |
| 153 | * |
| 154 | * Returns the interface ID which was allocated; or -ENODEV if no |
| 155 | * more interface IDs can be allocated. |
| 156 | */ |
| 157 | int usb_interface_id(struct usb_configuration *config, |
| 158 | struct usb_function *function) |
| 159 | { |
| 160 | unsigned char id = config->next_interface_id; |
| 161 | |
| 162 | if (id < MAX_CONFIG_INTERFACES) { |
| 163 | config->interface[id] = function; |
| 164 | config->next_interface_id = id + 1; |
| 165 | return id; |
| 166 | } |
| 167 | return -ENODEV; |
| 168 | } |
| 169 | |
| 170 | static int config_buf(struct usb_configuration *config, |
| 171 | enum usb_device_speed speed, void *buf, u8 type) |
| 172 | { |
| 173 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 174 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 175 | struct usb_descriptor_header **descriptors; |
Heinrich Schuchardt | 629c3b4 | 2018-03-18 13:05:58 +0100 | [diff] [blame] | 176 | struct usb_config_descriptor *c; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 177 | int status; |
| 178 | struct usb_function *f; |
| 179 | |
| 180 | /* write the config descriptor */ |
| 181 | c = buf; |
| 182 | c->bLength = USB_DT_CONFIG_SIZE; |
| 183 | c->bDescriptorType = type; |
| 184 | |
| 185 | c->bNumInterfaces = config->next_interface_id; |
| 186 | c->bConfigurationValue = config->bConfigurationValue; |
| 187 | c->iConfiguration = config->iConfiguration; |
| 188 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
| 189 | c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); |
| 190 | |
| 191 | /* There may be e.g. OTG descriptors */ |
| 192 | if (config->descriptors) { |
| 193 | status = usb_descriptor_fillbuf(next, len, |
| 194 | config->descriptors); |
| 195 | if (status < 0) |
| 196 | return status; |
| 197 | len -= status; |
| 198 | next += status; |
| 199 | } |
| 200 | |
| 201 | /* add each function's descriptors */ |
| 202 | list_for_each_entry(f, &config->functions, list) { |
| 203 | if (speed == USB_SPEED_HIGH) |
| 204 | descriptors = f->hs_descriptors; |
| 205 | else |
| 206 | descriptors = f->descriptors; |
| 207 | if (!descriptors) |
| 208 | continue; |
| 209 | status = usb_descriptor_fillbuf(next, len, |
| 210 | (const struct usb_descriptor_header **) descriptors); |
| 211 | if (status < 0) |
| 212 | return status; |
| 213 | len -= status; |
| 214 | next += status; |
| 215 | } |
| 216 | |
| 217 | len = next - buf; |
| 218 | c->wTotalLength = cpu_to_le16(len); |
| 219 | return len; |
| 220 | } |
| 221 | |
| 222 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) |
| 223 | { |
| 224 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 225 | struct usb_gadget *gadget = cdev->gadget; |
| 226 | u8 type = w_value >> 8; |
| 227 | int hs = 0; |
| 228 | struct usb_configuration *c; |
| 229 | |
| 230 | if (gadget_is_dualspeed(gadget)) { |
| 231 | if (gadget->speed == USB_SPEED_HIGH) |
| 232 | hs = 1; |
| 233 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 234 | hs = !hs; |
| 235 | if (hs) |
| 236 | speed = USB_SPEED_HIGH; |
| 237 | } |
| 238 | |
| 239 | w_value &= 0xff; |
| 240 | list_for_each_entry(c, &cdev->configs, list) { |
| 241 | if (speed == USB_SPEED_HIGH) { |
| 242 | if (!c->highspeed) |
| 243 | continue; |
| 244 | } else { |
| 245 | if (!c->fullspeed) |
| 246 | continue; |
| 247 | } |
| 248 | if (w_value == 0) |
| 249 | return config_buf(c, speed, cdev->req->buf, type); |
| 250 | w_value--; |
| 251 | } |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | |
| 255 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) |
| 256 | { |
| 257 | struct usb_gadget *gadget = cdev->gadget; |
| 258 | unsigned count = 0; |
| 259 | int hs = 0; |
| 260 | struct usb_configuration *c; |
| 261 | |
| 262 | if (gadget_is_dualspeed(gadget)) { |
| 263 | if (gadget->speed == USB_SPEED_HIGH) |
| 264 | hs = 1; |
| 265 | if (type == USB_DT_DEVICE_QUALIFIER) |
| 266 | hs = !hs; |
| 267 | } |
| 268 | list_for_each_entry(c, &cdev->configs, list) { |
| 269 | /* ignore configs that won't work at this speed */ |
| 270 | if (hs) { |
| 271 | if (!c->highspeed) |
| 272 | continue; |
| 273 | } else { |
| 274 | if (!c->fullspeed) |
| 275 | continue; |
| 276 | } |
| 277 | count++; |
| 278 | } |
| 279 | return count; |
| 280 | } |
| 281 | |
| 282 | static void device_qual(struct usb_composite_dev *cdev) |
| 283 | { |
| 284 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 285 | |
| 286 | qual->bLength = sizeof(*qual); |
| 287 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 288 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 289 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 290 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 291 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 292 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 293 | /* ASSUME same EP0 fifo size at both speeds */ |
Kishon Vijay Abraham I | 58d701e | 2015-02-23 18:40:17 +0530 | [diff] [blame] | 294 | qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 295 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
| 296 | qual->bRESERVED = 0; |
| 297 | } |
| 298 | |
| 299 | static void reset_config(struct usb_composite_dev *cdev) |
| 300 | { |
| 301 | struct usb_function *f; |
| 302 | |
| 303 | debug("%s:\n", __func__); |
| 304 | |
| 305 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 306 | if (f->disable) |
| 307 | f->disable(f); |
| 308 | |
| 309 | bitmap_zero(f->endpoints, 32); |
| 310 | } |
| 311 | cdev->config = NULL; |
| 312 | } |
| 313 | |
| 314 | static int set_config(struct usb_composite_dev *cdev, |
| 315 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 316 | { |
| 317 | struct usb_gadget *gadget = cdev->gadget; |
| 318 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 319 | struct usb_descriptor_header **descriptors; |
| 320 | int result = -EINVAL; |
| 321 | struct usb_endpoint_descriptor *ep; |
| 322 | struct usb_configuration *c = NULL; |
| 323 | int addr; |
| 324 | int tmp; |
| 325 | struct usb_function *f; |
| 326 | |
| 327 | if (cdev->config) |
| 328 | reset_config(cdev); |
| 329 | |
| 330 | if (number) { |
| 331 | list_for_each_entry(c, &cdev->configs, list) { |
| 332 | if (c->bConfigurationValue == number) { |
| 333 | result = 0; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | if (result < 0) |
| 338 | goto done; |
| 339 | } else |
| 340 | result = 0; |
| 341 | |
| 342 | debug("%s: %s speed config #%d: %s\n", __func__, |
| 343 | ({ char *speed; |
| 344 | switch (gadget->speed) { |
| 345 | case USB_SPEED_LOW: |
| 346 | speed = "low"; |
| 347 | break; |
| 348 | case USB_SPEED_FULL: |
| 349 | speed = "full"; |
| 350 | break; |
| 351 | case USB_SPEED_HIGH: |
| 352 | speed = "high"; |
| 353 | break; |
| 354 | default: |
| 355 | speed = "?"; |
| 356 | break; |
| 357 | }; |
| 358 | speed; |
| 359 | }), number, c ? c->label : "unconfigured"); |
| 360 | |
| 361 | if (!c) |
| 362 | goto done; |
| 363 | |
| 364 | cdev->config = c; |
| 365 | |
| 366 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 367 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 368 | f = c->interface[tmp]; |
| 369 | if (!f) |
| 370 | break; |
| 371 | |
| 372 | /* |
| 373 | * Record which endpoints are used by the function. This is used |
| 374 | * to dispatch control requests targeted at that endpoint to the |
| 375 | * function's setup callback instead of the current |
| 376 | * configuration's setup callback. |
| 377 | */ |
| 378 | if (gadget->speed == USB_SPEED_HIGH) |
| 379 | descriptors = f->hs_descriptors; |
| 380 | else |
| 381 | descriptors = f->descriptors; |
| 382 | |
| 383 | for (; *descriptors; ++descriptors) { |
| 384 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 385 | continue; |
| 386 | |
| 387 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 388 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 389 | | (ep->bEndpointAddress & 0x0f); |
Bryan O'Donoghue | 0b88876 | 2018-04-30 15:56:10 +0100 | [diff] [blame] | 390 | generic_set_bit(addr, f->endpoints); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | result = f->set_alt(f, tmp, 0); |
| 394 | if (result < 0) { |
| 395 | debug("interface %d (%s/%p) alt 0 --> %d\n", |
| 396 | tmp, f->name, f, result); |
| 397 | |
| 398 | reset_config(cdev); |
| 399 | goto done; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* when we return, be sure our power usage is valid */ |
| 404 | power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; |
| 405 | done: |
| 406 | usb_gadget_vbus_draw(gadget, power); |
| 407 | return result; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * usb_add_config() - add a configuration to a device. |
| 412 | * @cdev: wraps the USB gadget |
| 413 | * @config: the configuration, with bConfigurationValue assigned |
| 414 | * Context: single threaded during gadget setup |
| 415 | * |
| 416 | * One of the main tasks of a composite driver's bind() routine is to |
| 417 | * add each of the configurations it supports, using this routine. |
| 418 | * |
| 419 | * This function returns the value of the configuration's bind(), which |
| 420 | * is zero for success else a negative errno value. Binding configurations |
| 421 | * assigns global resources including string IDs, and per-configuration |
| 422 | * resources such as interface IDs and endpoints. |
| 423 | */ |
| 424 | int usb_add_config(struct usb_composite_dev *cdev, |
| 425 | struct usb_configuration *config) |
| 426 | { |
| 427 | int status = -EINVAL; |
| 428 | struct usb_configuration *c; |
| 429 | struct usb_function *f; |
| 430 | unsigned int i; |
| 431 | |
| 432 | debug("%s: adding config #%u '%s'/%p\n", __func__, |
| 433 | config->bConfigurationValue, |
| 434 | config->label, config); |
| 435 | |
| 436 | if (!config->bConfigurationValue || !config->bind) |
| 437 | goto done; |
| 438 | |
| 439 | /* Prevent duplicate configuration identifiers */ |
| 440 | list_for_each_entry(c, &cdev->configs, list) { |
| 441 | if (c->bConfigurationValue == config->bConfigurationValue) { |
| 442 | status = -EBUSY; |
| 443 | goto done; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | config->cdev = cdev; |
| 448 | list_add_tail(&config->list, &cdev->configs); |
| 449 | |
| 450 | INIT_LIST_HEAD(&config->functions); |
| 451 | config->next_interface_id = 0; |
| 452 | |
| 453 | status = config->bind(config); |
| 454 | if (status < 0) { |
| 455 | list_del(&config->list); |
| 456 | config->cdev = NULL; |
| 457 | } else { |
| 458 | debug("cfg %d/%p speeds:%s%s\n", |
| 459 | config->bConfigurationValue, config, |
| 460 | config->highspeed ? " high" : "", |
| 461 | config->fullspeed |
| 462 | ? (gadget_is_dualspeed(cdev->gadget) |
| 463 | ? " full" |
| 464 | : " full/low") |
| 465 | : ""); |
| 466 | |
| 467 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 468 | f = config->interface[i]; |
| 469 | if (!f) |
| 470 | continue; |
| 471 | debug("%s: interface %d = %s/%p\n", |
| 472 | __func__, i, f->name, f); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | usb_ep_autoconfig_reset(cdev->gadget); |
| 477 | |
| 478 | done: |
| 479 | if (status) |
| 480 | debug("added config '%s'/%u --> %d\n", config->label, |
| 481 | config->bConfigurationValue, status); |
| 482 | return status; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * We support strings in multiple languages ... string descriptor zero |
| 487 | * says which languages are supported. The typical case will be that |
| 488 | * only one language (probably English) is used, with I18N handled on |
| 489 | * the host side. |
| 490 | */ |
| 491 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 492 | static void collect_langs(struct usb_gadget_strings **sp, void *buf) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 493 | { |
| 494 | const struct usb_gadget_strings *s; |
| 495 | u16 language; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 496 | __le16_packed *tmp; |
| 497 | __le16_packed *end = (buf + 252); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 498 | |
| 499 | while (*sp) { |
| 500 | s = *sp; |
| 501 | language = cpu_to_le16(s->language); |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 502 | for (tmp = buf; tmp->val && tmp < end; tmp++) { |
| 503 | if (tmp->val == language) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 504 | goto repeat; |
| 505 | } |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 506 | tmp->val = language; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 507 | repeat: |
| 508 | sp++; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static int lookup_string( |
| 513 | struct usb_gadget_strings **sp, |
| 514 | void *buf, |
| 515 | u16 language, |
| 516 | int id |
| 517 | ) |
| 518 | { |
| 519 | int value; |
| 520 | struct usb_gadget_strings *s; |
| 521 | |
| 522 | while (*sp) { |
| 523 | s = *sp++; |
| 524 | if (s->language != language) |
| 525 | continue; |
| 526 | value = usb_gadget_get_string(s, id, buf); |
| 527 | if (value > 0) |
| 528 | return value; |
| 529 | } |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | static int get_string(struct usb_composite_dev *cdev, |
| 534 | void *buf, u16 language, int id) |
| 535 | { |
| 536 | struct usb_string_descriptor *s = buf; |
| 537 | struct usb_gadget_strings **sp; |
| 538 | int len; |
| 539 | struct usb_configuration *c; |
| 540 | struct usb_function *f; |
| 541 | |
| 542 | /* |
| 543 | * Yes, not only is USB's I18N support probably more than most |
| 544 | * folk will ever care about ... also, it's all supported here. |
| 545 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 546 | */ |
| 547 | |
| 548 | /* 0 == report all available language codes */ |
| 549 | if (id == 0) { |
| 550 | memset(s, 0, 256); |
| 551 | s->bDescriptorType = USB_DT_STRING; |
| 552 | |
| 553 | sp = composite->strings; |
| 554 | if (sp) |
| 555 | collect_langs(sp, s->wData); |
| 556 | |
| 557 | list_for_each_entry(c, &cdev->configs, list) { |
| 558 | sp = c->strings; |
| 559 | if (sp) |
| 560 | collect_langs(sp, s->wData); |
| 561 | |
| 562 | list_for_each_entry(f, &c->functions, list) { |
| 563 | sp = f->strings; |
| 564 | if (sp) |
| 565 | collect_langs(sp, s->wData); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | for (len = 0; len <= 126 && s->wData[len]; len++) |
| 570 | continue; |
| 571 | if (!len) |
| 572 | return -EINVAL; |
| 573 | |
| 574 | s->bLength = 2 * (len + 1); |
| 575 | return s->bLength; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Otherwise, look up and return a specified string. String IDs |
| 580 | * are device-scoped, so we look up each string table we're told |
| 581 | * about. These lookups are infrequent; simpler-is-better here. |
| 582 | */ |
| 583 | if (composite->strings) { |
| 584 | len = lookup_string(composite->strings, buf, language, id); |
| 585 | if (len > 0) |
| 586 | return len; |
| 587 | } |
| 588 | list_for_each_entry(c, &cdev->configs, list) { |
| 589 | if (c->strings) { |
| 590 | len = lookup_string(c->strings, buf, language, id); |
| 591 | if (len > 0) |
| 592 | return len; |
| 593 | } |
| 594 | list_for_each_entry(f, &c->functions, list) { |
| 595 | if (!f->strings) |
| 596 | continue; |
| 597 | len = lookup_string(f->strings, buf, language, id); |
| 598 | if (len > 0) |
| 599 | return len; |
| 600 | } |
| 601 | } |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * usb_string_id() - allocate an unused string ID |
| 607 | * @cdev: the device whose string descriptor IDs are being allocated |
| 608 | * Context: single threaded during gadget setup |
| 609 | * |
| 610 | * @usb_string_id() is called from bind() callbacks to allocate |
| 611 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 612 | * then store that ID in the appropriate descriptors and string table. |
| 613 | * |
| 614 | * All string identifier should be allocated using this, |
| 615 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 616 | * that for example different functions don't wrongly assign different |
| 617 | * meanings to the same identifier. |
| 618 | */ |
| 619 | int usb_string_id(struct usb_composite_dev *cdev) |
| 620 | { |
| 621 | if (cdev->next_string_id < 254) { |
| 622 | /* |
| 623 | * string id 0 is reserved by USB spec for list of |
| 624 | * supported languages |
| 625 | * 255 reserved as well? -- mina86 |
| 626 | */ |
| 627 | cdev->next_string_id++; |
| 628 | return cdev->next_string_id; |
| 629 | } |
| 630 | return -ENODEV; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * usb_string_ids() - allocate unused string IDs in batch |
| 635 | * @cdev: the device whose string descriptor IDs are being allocated |
| 636 | * @str: an array of usb_string objects to assign numbers to |
| 637 | * Context: single threaded during gadget setup |
| 638 | * |
| 639 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 640 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 641 | * then copy IDs from the string table to the appropriate descriptors |
| 642 | * and string table for other languages. |
| 643 | * |
| 644 | * All string identifier should be allocated using this, |
| 645 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 646 | * example different functions don't wrongly assign different meanings |
| 647 | * to the same identifier. |
| 648 | */ |
| 649 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 650 | { |
| 651 | u8 next = cdev->next_string_id; |
| 652 | |
| 653 | for (; str->s; ++str) { |
| 654 | if (next >= 254) |
| 655 | return -ENODEV; |
| 656 | str->id = ++next; |
| 657 | } |
| 658 | |
| 659 | cdev->next_string_id = next; |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * usb_string_ids_n() - allocate unused string IDs in batch |
| 666 | * @c: the device whose string descriptor IDs are being allocated |
| 667 | * @n: number of string IDs to allocate |
| 668 | * Context: single threaded during gadget setup |
| 669 | * |
| 670 | * Returns the first requested ID. This ID and next @n-1 IDs are now |
| 671 | * valid IDs. At least provided that @n is non-zero because if it |
| 672 | * is, returns last requested ID which is now very useful information. |
| 673 | * |
| 674 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 675 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 676 | * then store that ID in the appropriate descriptors and string table. |
| 677 | * |
| 678 | * All string identifier should be allocated using this, |
| 679 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 680 | * example different functions don't wrongly assign different meanings |
| 681 | * to the same identifier. |
| 682 | */ |
| 683 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 684 | { |
| 685 | u8 next = c->next_string_id; |
| 686 | |
| 687 | if (n > 254 || next + n > 254) |
| 688 | return -ENODEV; |
| 689 | |
| 690 | c->next_string_id += n; |
| 691 | return next + 1; |
| 692 | } |
| 693 | |
| 694 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 695 | { |
| 696 | if (req->status || req->actual != req->length) |
| 697 | debug("%s: setup complete --> %d, %d/%d\n", __func__, |
| 698 | req->status, req->actual, req->length); |
| 699 | } |
| 700 | |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 701 | static int bos_desc(struct usb_composite_dev *cdev) |
| 702 | { |
| 703 | struct usb_ext_cap_descriptor *usb_ext; |
| 704 | struct usb_bos_descriptor *bos = cdev->req->buf; |
| 705 | |
| 706 | bos->bLength = USB_DT_BOS_SIZE; |
| 707 | bos->bDescriptorType = USB_DT_BOS; |
| 708 | |
| 709 | bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); |
| 710 | bos->bNumDeviceCaps = 0; |
| 711 | |
| 712 | /* |
| 713 | * A SuperSpeed device shall include the USB2.0 extension descriptor |
| 714 | * and shall support LPM when operating in USB2.0 HS mode. |
| 715 | */ |
| 716 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 717 | bos->bNumDeviceCaps++; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 718 | le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, |
| 719 | USB_DT_USB_EXT_CAP_SIZE); |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 720 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; |
| 721 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 722 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; |
| 723 | usb_ext->bmAttributes = |
| 724 | cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT); |
| 725 | |
| 726 | /* |
| 727 | * The Superspeed USB Capability descriptor shall be implemented |
| 728 | * by all SuperSpeed devices. |
| 729 | */ |
| 730 | if (gadget_is_superspeed(cdev->gadget)) { |
| 731 | struct usb_ss_cap_descriptor *ss_cap; |
| 732 | |
| 733 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 734 | bos->bNumDeviceCaps++; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 735 | le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, |
| 736 | USB_DT_USB_SS_CAP_SIZE); |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 737 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; |
| 738 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 739 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; |
| 740 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ |
| 741 | ss_cap->wSpeedSupported = |
| 742 | cpu_to_le16(USB_LOW_SPEED_OPERATION | |
| 743 | USB_FULL_SPEED_OPERATION | |
| 744 | USB_HIGH_SPEED_OPERATION | |
| 745 | USB_5GBPS_OPERATION); |
| 746 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; |
| 747 | ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; |
| 748 | ss_cap->bU2DevExitLat = |
| 749 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
| 750 | } |
| 751 | return le16_to_cpu(bos->wTotalLength); |
| 752 | } |
| 753 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 754 | /* |
| 755 | * The setup() callback implements all the ep0 functionality that's |
| 756 | * not handled lower down, in hardware or the hardware driver(like |
| 757 | * device and endpoint feature flags, and their status). It's all |
| 758 | * housekeeping for the gadget function we're implementing. Most of |
| 759 | * the work is in config and function specific setup. |
| 760 | */ |
| 761 | static int |
| 762 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 763 | { |
| 764 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 765 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 766 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 767 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 768 | u8 intf = w_index & 0xFF; |
| 769 | int value = -EOPNOTSUPP; |
| 770 | struct usb_request *req = cdev->req; |
| 771 | struct usb_function *f = NULL; |
| 772 | int standard; |
| 773 | u8 endp; |
| 774 | struct usb_configuration *c; |
| 775 | |
| 776 | /* |
| 777 | * partial re-init of the response message; the function or the |
| 778 | * gadget might need to intercept e.g. a control-OUT completion |
| 779 | * when we delegate to it. |
| 780 | */ |
| 781 | req->zero = 0; |
| 782 | req->complete = composite_setup_complete; |
| 783 | req->length = USB_BUFSIZ; |
| 784 | gadget->ep0->driver_data = cdev; |
| 785 | standard = (ctrl->bRequestType & USB_TYPE_MASK) |
| 786 | == USB_TYPE_STANDARD; |
| 787 | if (!standard) |
| 788 | goto unknown; |
| 789 | |
| 790 | switch (ctrl->bRequest) { |
| 791 | |
| 792 | /* we handle all standard USB descriptors */ |
| 793 | case USB_REQ_GET_DESCRIPTOR: |
| 794 | if (ctrl->bRequestType != USB_DIR_IN) |
| 795 | goto unknown; |
| 796 | switch (w_value >> 8) { |
| 797 | |
| 798 | case USB_DT_DEVICE: |
| 799 | cdev->desc.bNumConfigurations = |
| 800 | count_configs(cdev, USB_DT_DEVICE); |
Siva Durga Prasad Paladugu | c7c0718 | 2018-12-13 15:16:36 +0530 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * If the speed is Super speed, then the supported |
| 804 | * max packet size is 512 and it should be sent as |
| 805 | * exponent of 2. So, 9(2^9=512) should be filled in |
| 806 | * bMaxPacketSize0. Also fill USB version as 3.0 |
| 807 | * if speed is Super speed. |
| 808 | */ |
| 809 | if (cdev->gadget->speed == USB_SPEED_SUPER) { |
| 810 | cdev->desc.bMaxPacketSize0 = 9; |
| 811 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
| 812 | } else { |
| 813 | cdev->desc.bMaxPacketSize0 = |
| 814 | cdev->gadget->ep0->maxpacket; |
| 815 | } |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 816 | value = min(w_length, (u16) sizeof cdev->desc); |
| 817 | memcpy(req->buf, &cdev->desc, value); |
| 818 | break; |
| 819 | case USB_DT_DEVICE_QUALIFIER: |
| 820 | if (!gadget_is_dualspeed(gadget)) |
| 821 | break; |
| 822 | device_qual(cdev); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 823 | value = min_t(int, w_length, |
| 824 | sizeof(struct usb_qualifier_descriptor)); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 825 | break; |
| 826 | case USB_DT_OTHER_SPEED_CONFIG: |
| 827 | if (!gadget_is_dualspeed(gadget)) |
| 828 | break; |
| 829 | |
| 830 | case USB_DT_CONFIG: |
| 831 | value = config_desc(cdev, w_value); |
| 832 | if (value >= 0) |
| 833 | value = min(w_length, (u16) value); |
| 834 | break; |
| 835 | case USB_DT_STRING: |
| 836 | value = get_string(cdev, req->buf, |
| 837 | w_index, w_value & 0xff); |
| 838 | if (value >= 0) |
| 839 | value = min(w_length, (u16) value); |
| 840 | break; |
Stefan Roese | a56fbcc | 2015-01-09 14:54:55 +0100 | [diff] [blame] | 841 | case USB_DT_BOS: |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 842 | if (gadget_is_superspeed(cdev->gadget)) |
| 843 | value = bos_desc(cdev); |
| 844 | if (value >= 0) |
| 845 | value = min(w_length, (u16)value); |
Stefan Roese | a56fbcc | 2015-01-09 14:54:55 +0100 | [diff] [blame] | 846 | break; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 847 | default: |
| 848 | goto unknown; |
| 849 | } |
| 850 | break; |
| 851 | |
| 852 | /* any number of configs can work */ |
| 853 | case USB_REQ_SET_CONFIGURATION: |
| 854 | if (ctrl->bRequestType != 0) |
| 855 | goto unknown; |
| 856 | if (gadget_is_otg(gadget)) { |
| 857 | if (gadget->a_hnp_support) |
| 858 | debug("HNP available\n"); |
| 859 | else if (gadget->a_alt_hnp_support) |
| 860 | debug("HNP on another port\n"); |
| 861 | else |
| 862 | debug("HNP inactive\n"); |
| 863 | } |
| 864 | |
| 865 | value = set_config(cdev, ctrl, w_value); |
| 866 | break; |
| 867 | case USB_REQ_GET_CONFIGURATION: |
| 868 | if (ctrl->bRequestType != USB_DIR_IN) |
| 869 | goto unknown; |
| 870 | if (cdev->config) |
| 871 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 872 | else |
| 873 | *(u8 *)req->buf = 0; |
| 874 | value = min(w_length, (u16) 1); |
| 875 | break; |
| 876 | |
| 877 | /* |
| 878 | * function drivers must handle get/set altsetting; if there's |
| 879 | * no get() method, we know only altsetting zero works. |
| 880 | */ |
| 881 | case USB_REQ_SET_INTERFACE: |
| 882 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 883 | goto unknown; |
| 884 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 885 | break; |
| 886 | f = cdev->config->interface[intf]; |
| 887 | if (!f) |
| 888 | break; |
| 889 | if (w_value && !f->set_alt) |
| 890 | break; |
| 891 | value = f->set_alt(f, w_index, w_value); |
| 892 | break; |
| 893 | case USB_REQ_GET_INTERFACE: |
| 894 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 895 | goto unknown; |
| 896 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 897 | break; |
| 898 | f = cdev->config->interface[intf]; |
| 899 | if (!f) |
| 900 | break; |
| 901 | /* lots of interfaces only need altsetting zero... */ |
| 902 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 903 | if (value < 0) |
| 904 | break; |
| 905 | *((u8 *)req->buf) = value; |
| 906 | value = min(w_length, (u16) 1); |
| 907 | break; |
| 908 | default: |
| 909 | unknown: |
| 910 | debug("non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 911 | ctrl->bRequestType, ctrl->bRequest, |
| 912 | w_value, w_index, w_length); |
| 913 | |
Christophe Kerello | 0c1b62c | 2018-03-15 09:34:17 +0100 | [diff] [blame] | 914 | if (!cdev->config) |
| 915 | goto done; |
| 916 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 917 | /* |
| 918 | * functions always handle their interfaces and endpoints... |
| 919 | * punt other recipients (other, WUSB, ...) to the current |
| 920 | * configuration code. |
| 921 | */ |
| 922 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 923 | case USB_RECIP_INTERFACE: |
| 924 | f = cdev->config->interface[intf]; |
| 925 | break; |
| 926 | |
| 927 | case USB_RECIP_ENDPOINT: |
| 928 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 929 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 930 | if (test_bit(endp, f->endpoints)) |
| 931 | break; |
| 932 | } |
| 933 | if (&f->list == &cdev->config->functions) |
| 934 | f = NULL; |
| 935 | break; |
Lukasz Majewski | 92d7fb8 | 2013-03-01 15:30:18 +0100 | [diff] [blame] | 936 | /* |
| 937 | * dfu-util (version 0.5) sets bmRequestType.Receipent = Device |
| 938 | * for non-standard request (w_value = 0x21, |
| 939 | * bRequest = GET_DESCRIPTOR in this case). |
| 940 | * When only one interface is registered (as it is done now), |
| 941 | * then this request shall be handled as it was requested for |
| 942 | * interface. |
| 943 | * |
| 944 | * In the below code it is checked if only one interface is |
| 945 | * present and proper function for it is extracted. Due to that |
| 946 | * function's setup (f->setup) is called to handle this |
| 947 | * special non-standard request. |
| 948 | */ |
| 949 | case USB_RECIP_DEVICE: |
| 950 | debug("cdev->config->next_interface_id: %d intf: %d\n", |
| 951 | cdev->config->next_interface_id, intf); |
| 952 | if (cdev->config->next_interface_id == 1) |
| 953 | f = cdev->config->interface[intf]; |
| 954 | break; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | if (f && f->setup) |
| 958 | value = f->setup(f, ctrl); |
| 959 | else { |
| 960 | c = cdev->config; |
Christophe Kerello | 0c1b62c | 2018-03-15 09:34:17 +0100 | [diff] [blame] | 961 | if (c->setup) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 962 | value = c->setup(c, ctrl); |
| 963 | } |
| 964 | |
| 965 | goto done; |
| 966 | } |
| 967 | |
| 968 | /* respond with data transfer before status phase? */ |
| 969 | if (value >= 0) { |
| 970 | req->length = value; |
| 971 | req->zero = value < w_length; |
| 972 | value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); |
| 973 | if (value < 0) { |
| 974 | debug("ep_queue --> %d\n", value); |
| 975 | req->status = 0; |
| 976 | composite_setup_complete(gadget->ep0, req); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | done: |
| 981 | /* device either stalls (value < 0) or reports success */ |
| 982 | return value; |
| 983 | } |
| 984 | |
| 985 | static void composite_disconnect(struct usb_gadget *gadget) |
| 986 | { |
| 987 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 988 | |
| 989 | if (cdev->config) |
| 990 | reset_config(cdev); |
| 991 | if (composite->disconnect) |
| 992 | composite->disconnect(cdev); |
| 993 | } |
| 994 | |
| 995 | static void composite_unbind(struct usb_gadget *gadget) |
| 996 | { |
| 997 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 998 | struct usb_configuration *c; |
| 999 | struct usb_function *f; |
| 1000 | |
| 1001 | /* |
| 1002 | * composite_disconnect() must already have been called |
| 1003 | * by the underlying peripheral controller driver! |
| 1004 | * so there's no i/o concurrency that could affect the |
| 1005 | * state protected by cdev->lock. |
| 1006 | */ |
Simon Glass | b18a960 | 2019-12-29 21:19:12 -0700 | [diff] [blame] | 1007 | #ifdef __UBOOT__ |
| 1008 | assert_noisy(!cdev->config); |
| 1009 | #else |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1010 | BUG_ON(cdev->config); |
Simon Glass | b18a960 | 2019-12-29 21:19:12 -0700 | [diff] [blame] | 1011 | #endif |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1012 | |
| 1013 | while (!list_empty(&cdev->configs)) { |
| 1014 | c = list_first_entry(&cdev->configs, |
| 1015 | struct usb_configuration, list); |
| 1016 | while (!list_empty(&c->functions)) { |
| 1017 | f = list_first_entry(&c->functions, |
| 1018 | struct usb_function, list); |
| 1019 | list_del(&f->list); |
| 1020 | if (f->unbind) { |
| 1021 | debug("unbind function '%s'/%p\n", |
| 1022 | f->name, f); |
| 1023 | f->unbind(c, f); |
| 1024 | } |
| 1025 | } |
| 1026 | list_del(&c->list); |
| 1027 | if (c->unbind) { |
| 1028 | debug("unbind config '%s'/%p\n", c->label, c); |
| 1029 | c->unbind(c); |
| 1030 | } |
Stephen Warren | 1c377dc | 2015-09-04 22:03:42 -0600 | [diff] [blame] | 1031 | free(c); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1032 | } |
| 1033 | if (composite->unbind) |
| 1034 | composite->unbind(cdev); |
| 1035 | |
| 1036 | if (cdev->req) { |
| 1037 | kfree(cdev->req->buf); |
| 1038 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 1039 | } |
| 1040 | kfree(cdev); |
| 1041 | set_gadget_data(gadget, NULL); |
| 1042 | |
| 1043 | composite = NULL; |
| 1044 | } |
| 1045 | |
| 1046 | static int composite_bind(struct usb_gadget *gadget) |
| 1047 | { |
| 1048 | int status = -ENOMEM; |
| 1049 | struct usb_composite_dev *cdev; |
| 1050 | |
| 1051 | cdev = calloc(sizeof *cdev, 1); |
| 1052 | if (!cdev) |
| 1053 | return status; |
| 1054 | |
| 1055 | cdev->gadget = gadget; |
| 1056 | set_gadget_data(gadget, cdev); |
| 1057 | INIT_LIST_HEAD(&cdev->configs); |
| 1058 | |
| 1059 | /* preallocate control response and buffer */ |
| 1060 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 1061 | if (!cdev->req) |
| 1062 | goto fail; |
| 1063 | cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ); |
| 1064 | if (!cdev->req->buf) |
| 1065 | goto fail; |
| 1066 | cdev->req->complete = composite_setup_complete; |
| 1067 | gadget->ep0->driver_data = cdev; |
| 1068 | |
| 1069 | cdev->bufsiz = USB_BUFSIZ; |
| 1070 | cdev->driver = composite; |
| 1071 | |
| 1072 | usb_gadget_set_selfpowered(gadget); |
| 1073 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1074 | |
| 1075 | status = composite->bind(cdev); |
| 1076 | if (status < 0) |
| 1077 | goto fail; |
| 1078 | |
Piotr Wilczek | 7db5de6 | 2013-04-10 14:07:51 +0200 | [diff] [blame] | 1079 | memcpy(&cdev->desc, composite->dev, |
| 1080 | sizeof(struct usb_device_descriptor)); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1081 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 1082 | |
| 1083 | debug("%s: ready\n", composite->name); |
| 1084 | return 0; |
| 1085 | |
| 1086 | fail: |
| 1087 | composite_unbind(gadget); |
| 1088 | return status; |
| 1089 | } |
| 1090 | |
| 1091 | static void |
| 1092 | composite_suspend(struct usb_gadget *gadget) |
| 1093 | { |
| 1094 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1095 | struct usb_function *f; |
| 1096 | |
| 1097 | debug("%s: suspend\n", __func__); |
| 1098 | if (cdev->config) { |
| 1099 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1100 | if (f->suspend) |
| 1101 | f->suspend(f); |
| 1102 | } |
| 1103 | } |
| 1104 | if (composite->suspend) |
| 1105 | composite->suspend(cdev); |
| 1106 | |
| 1107 | cdev->suspended = 1; |
| 1108 | } |
| 1109 | |
| 1110 | static void |
| 1111 | composite_resume(struct usb_gadget *gadget) |
| 1112 | { |
| 1113 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1114 | struct usb_function *f; |
| 1115 | |
| 1116 | debug("%s: resume\n", __func__); |
| 1117 | if (composite->resume) |
| 1118 | composite->resume(cdev); |
| 1119 | if (cdev->config) { |
| 1120 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1121 | if (f->resume) |
| 1122 | f->resume(f); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | cdev->suspended = 0; |
| 1127 | } |
| 1128 | |
| 1129 | static struct usb_gadget_driver composite_driver = { |
| 1130 | .speed = USB_SPEED_HIGH, |
| 1131 | |
| 1132 | .bind = composite_bind, |
| 1133 | .unbind = composite_unbind, |
| 1134 | |
| 1135 | .setup = composite_setup, |
Lukasz Majewski | d04a012 | 2015-03-03 17:32:07 +0100 | [diff] [blame] | 1136 | .reset = composite_disconnect, |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1137 | .disconnect = composite_disconnect, |
| 1138 | |
| 1139 | .suspend = composite_suspend, |
| 1140 | .resume = composite_resume, |
| 1141 | }; |
| 1142 | |
| 1143 | /** |
| 1144 | * usb_composite_register() - register a composite driver |
| 1145 | * @driver: the driver to register |
| 1146 | * Context: single threaded during gadget setup |
| 1147 | * |
| 1148 | * This function is used to register drivers using the composite driver |
| 1149 | * framework. The return value is zero, or a negative errno value. |
| 1150 | * Those values normally come from the driver's @bind method, which does |
| 1151 | * all the work of setting up the driver to match the hardware. |
| 1152 | * |
| 1153 | * On successful return, the gadget is ready to respond to requests from |
| 1154 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 1155 | * while it was binding. That would usually be done in order to wait for |
| 1156 | * some userspace participation. |
| 1157 | */ |
| 1158 | int usb_composite_register(struct usb_composite_driver *driver) |
| 1159 | { |
Sam Protsenko | 1257562 | 2016-02-16 19:59:19 +0200 | [diff] [blame] | 1160 | int res; |
| 1161 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1162 | if (!driver || !driver->dev || !driver->bind || composite) |
| 1163 | return -EINVAL; |
| 1164 | |
| 1165 | if (!driver->name) |
| 1166 | driver->name = "composite"; |
| 1167 | composite = driver; |
| 1168 | |
Sam Protsenko | 1257562 | 2016-02-16 19:59:19 +0200 | [diff] [blame] | 1169 | res = usb_gadget_register_driver(&composite_driver); |
| 1170 | if (res != 0) |
| 1171 | composite = NULL; |
| 1172 | |
| 1173 | return res; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * usb_composite_unregister() - unregister a composite driver |
| 1178 | * @driver: the driver to unregister |
| 1179 | * |
| 1180 | * This function is used to unregister drivers using the composite |
| 1181 | * driver framework. |
| 1182 | */ |
| 1183 | void usb_composite_unregister(struct usb_composite_driver *driver) |
| 1184 | { |
| 1185 | if (composite != driver) |
| 1186 | return; |
| 1187 | usb_gadget_unregister_driver(&composite_driver); |
Heiko Schocher | 9e37005 | 2013-06-04 11:21:32 +0200 | [diff] [blame] | 1188 | composite = NULL; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1189 | } |