1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/configfs.h> 3 #include <linux/module.h> 4 #include <linux/slab.h> 5 #include <linux/device.h> 6 #include <linux/nls.h> 7 #include <linux/usb/composite.h> 8 #include <linux/usb/gadget_configfs.h> 9 #include "configfs.h" 10 #include "u_f.h" 11 #include "u_os_desc.h" 12 13 int check_user_usb_string(const char *name, 14 struct usb_gadget_strings *stringtab_dev) 15 { 16 u16 num; 17 int ret; 18 19 ret = kstrtou16(name, 0, &num); 20 if (ret) 21 return ret; 22 23 if (!usb_validate_langid(num)) 24 return -EINVAL; 25 26 stringtab_dev->language = num; 27 return 0; 28 } 29 30 #define MAX_NAME_LEN 40 31 #define MAX_USB_STRING_LANGS 2 32 33 static const struct usb_descriptor_header *otg_desc[2]; 34 35 struct gadget_info { 36 struct config_group group; 37 struct config_group functions_group; 38 struct config_group configs_group; 39 struct config_group strings_group; 40 struct config_group os_desc_group; 41 42 struct mutex lock; 43 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; 44 struct list_head string_list; 45 struct list_head available_func; 46 47 struct usb_composite_driver composite; 48 struct usb_composite_dev cdev; 49 bool use_os_desc; 50 char b_vendor_code; 51 char qw_sign[OS_STRING_QW_SIGN_LEN]; 52 spinlock_t spinlock; 53 bool unbind; 54 }; 55 56 static inline struct gadget_info *to_gadget_info(struct config_item *item) 57 { 58 return container_of(to_config_group(item), struct gadget_info, group); 59 } 60 61 struct config_usb_cfg { 62 struct config_group group; 63 struct config_group strings_group; 64 struct list_head string_list; 65 struct usb_configuration c; 66 struct list_head func_list; 67 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; 68 }; 69 70 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) 71 { 72 return container_of(to_config_group(item), struct config_usb_cfg, 73 group); 74 } 75 76 static inline struct gadget_info *cfg_to_gadget_info(struct config_usb_cfg *cfg) 77 { 78 return container_of(cfg->c.cdev, struct gadget_info, cdev); 79 } 80 81 struct gadget_strings { 82 struct usb_gadget_strings stringtab_dev; 83 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; 84 char *manufacturer; 85 char *product; 86 char *serialnumber; 87 88 struct config_group group; 89 struct list_head list; 90 }; 91 92 struct gadget_config_name { 93 struct usb_gadget_strings stringtab_dev; 94 struct usb_string strings; 95 char *configuration; 96 97 struct config_group group; 98 struct list_head list; 99 }; 100 101 #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1) 102 103 static int usb_string_copy(const char *s, char **s_copy) 104 { 105 int ret; 106 char *str; 107 char *copy = *s_copy; 108 ret = strlen(s); 109 if (ret > USB_MAX_STRING_LEN) 110 return -EOVERFLOW; 111 112 if (copy) { 113 str = copy; 114 } else { 115 str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL); 116 if (!str) 117 return -ENOMEM; 118 } 119 strcpy(str, s); 120 if (str[ret - 1] == '\n') 121 str[ret - 1] = '\0'; 122 *s_copy = str; 123 return 0; 124 } 125 126 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ 127 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ 128 char *page) \ 129 { \ 130 return sprintf(page, "0x%02x\n", \ 131 to_gadget_info(item)->cdev.desc.__name); \ 132 } 133 134 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ 135 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ 136 char *page) \ 137 { \ 138 return sprintf(page, "0x%04x\n", \ 139 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \ 140 } 141 142 143 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ 144 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ 145 const char *page, size_t len) \ 146 { \ 147 u8 val; \ 148 int ret; \ 149 ret = kstrtou8(page, 0, &val); \ 150 if (ret) \ 151 return ret; \ 152 to_gadget_info(item)->cdev.desc._name = val; \ 153 return len; \ 154 } 155 156 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ 157 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ 158 const char *page, size_t len) \ 159 { \ 160 u16 val; \ 161 int ret; \ 162 ret = kstrtou16(page, 0, &val); \ 163 if (ret) \ 164 return ret; \ 165 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \ 166 return len; \ 167 } 168 169 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ 170 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ 171 GI_DEVICE_DESC_SIMPLE_W_##_type(_name) 172 173 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); 174 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); 175 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); 176 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); 177 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); 178 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); 179 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); 180 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); 181 182 static ssize_t is_valid_bcd(u16 bcd_val) 183 { 184 if ((bcd_val & 0xf) > 9) 185 return -EINVAL; 186 if (((bcd_val >> 4) & 0xf) > 9) 187 return -EINVAL; 188 if (((bcd_val >> 8) & 0xf) > 9) 189 return -EINVAL; 190 if (((bcd_val >> 12) & 0xf) > 9) 191 return -EINVAL; 192 return 0; 193 } 194 195 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item, 196 const char *page, size_t len) 197 { 198 u16 bcdDevice; 199 int ret; 200 201 ret = kstrtou16(page, 0, &bcdDevice); 202 if (ret) 203 return ret; 204 ret = is_valid_bcd(bcdDevice); 205 if (ret) 206 return ret; 207 208 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); 209 return len; 210 } 211 212 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item, 213 const char *page, size_t len) 214 { 215 u16 bcdUSB; 216 int ret; 217 218 ret = kstrtou16(page, 0, &bcdUSB); 219 if (ret) 220 return ret; 221 ret = is_valid_bcd(bcdUSB); 222 if (ret) 223 return ret; 224 225 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); 226 return len; 227 } 228 229 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page) 230 { 231 struct gadget_info *gi = to_gadget_info(item); 232 char *udc_name; 233 int ret; 234 235 mutex_lock(&gi->lock); 236 udc_name = gi->composite.gadget_driver.udc_name; 237 ret = sprintf(page, "%s\n", udc_name ?: ""); 238 mutex_unlock(&gi->lock); 239 240 return ret; 241 } 242 243 static int unregister_gadget(struct gadget_info *gi) 244 { 245 int ret; 246 247 if (!gi->composite.gadget_driver.udc_name) 248 return -ENODEV; 249 250 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); 251 if (ret) 252 return ret; 253 kfree(gi->composite.gadget_driver.udc_name); 254 gi->composite.gadget_driver.udc_name = NULL; 255 return 0; 256 } 257 258 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item, 259 const char *page, size_t len) 260 { 261 struct gadget_info *gi = to_gadget_info(item); 262 char *name; 263 int ret; 264 265 if (strlen(page) < len) 266 return -EOVERFLOW; 267 268 name = kstrdup(page, GFP_KERNEL); 269 if (!name) 270 return -ENOMEM; 271 if (name[len - 1] == '\n') 272 name[len - 1] = '\0'; 273 274 mutex_lock(&gi->lock); 275 276 if (!strlen(name)) { 277 ret = unregister_gadget(gi); 278 if (ret) 279 goto err; 280 kfree(name); 281 } else { 282 if (gi->composite.gadget_driver.udc_name) { 283 ret = -EBUSY; 284 goto err; 285 } 286 gi->composite.gadget_driver.udc_name = name; 287 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver); 288 if (ret) { 289 gi->composite.gadget_driver.udc_name = NULL; 290 goto err; 291 } 292 } 293 mutex_unlock(&gi->lock); 294 return len; 295 err: 296 kfree(name); 297 mutex_unlock(&gi->lock); 298 return ret; 299 } 300 301 static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item, 302 char *page) 303 { 304 enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed; 305 306 return sprintf(page, "%s\n", usb_speed_string(speed)); 307 } 308 309 static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item, 310 const char *page, size_t len) 311 { 312 struct gadget_info *gi = to_gadget_info(item); 313 314 mutex_lock(&gi->lock); 315 316 /* Prevent changing of max_speed after the driver is binded */ 317 if (gi->composite.gadget_driver.udc_name) 318 goto err; 319 320 if (strncmp(page, "super-speed-plus", 16) == 0) 321 gi->composite.max_speed = USB_SPEED_SUPER_PLUS; 322 else if (strncmp(page, "super-speed", 11) == 0) 323 gi->composite.max_speed = USB_SPEED_SUPER; 324 else if (strncmp(page, "high-speed", 10) == 0) 325 gi->composite.max_speed = USB_SPEED_HIGH; 326 else if (strncmp(page, "full-speed", 10) == 0) 327 gi->composite.max_speed = USB_SPEED_FULL; 328 else if (strncmp(page, "low-speed", 9) == 0) 329 gi->composite.max_speed = USB_SPEED_LOW; 330 else 331 goto err; 332 333 gi->composite.gadget_driver.max_speed = gi->composite.max_speed; 334 335 mutex_unlock(&gi->lock); 336 return len; 337 err: 338 mutex_unlock(&gi->lock); 339 return -EINVAL; 340 } 341 342 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass); 343 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass); 344 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol); 345 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0); 346 CONFIGFS_ATTR(gadget_dev_desc_, idVendor); 347 CONFIGFS_ATTR(gadget_dev_desc_, idProduct); 348 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice); 349 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB); 350 CONFIGFS_ATTR(gadget_dev_desc_, UDC); 351 CONFIGFS_ATTR(gadget_dev_desc_, max_speed); 352 353 static struct configfs_attribute *gadget_root_attrs[] = { 354 &gadget_dev_desc_attr_bDeviceClass, 355 &gadget_dev_desc_attr_bDeviceSubClass, 356 &gadget_dev_desc_attr_bDeviceProtocol, 357 &gadget_dev_desc_attr_bMaxPacketSize0, 358 &gadget_dev_desc_attr_idVendor, 359 &gadget_dev_desc_attr_idProduct, 360 &gadget_dev_desc_attr_bcdDevice, 361 &gadget_dev_desc_attr_bcdUSB, 362 &gadget_dev_desc_attr_UDC, 363 &gadget_dev_desc_attr_max_speed, 364 NULL, 365 }; 366 367 static inline struct gadget_strings *to_gadget_strings(struct config_item *item) 368 { 369 return container_of(to_config_group(item), struct gadget_strings, 370 group); 371 } 372 373 static inline struct gadget_config_name *to_gadget_config_name( 374 struct config_item *item) 375 { 376 return container_of(to_config_group(item), struct gadget_config_name, 377 group); 378 } 379 380 static inline struct usb_function_instance *to_usb_function_instance( 381 struct config_item *item) 382 { 383 return container_of(to_config_group(item), 384 struct usb_function_instance, group); 385 } 386 387 static void gadget_info_attr_release(struct config_item *item) 388 { 389 struct gadget_info *gi = to_gadget_info(item); 390 391 WARN_ON(!list_empty(&gi->cdev.configs)); 392 WARN_ON(!list_empty(&gi->string_list)); 393 WARN_ON(!list_empty(&gi->available_func)); 394 kfree(gi->composite.gadget_driver.function); 395 kfree(gi); 396 } 397 398 static struct configfs_item_operations gadget_root_item_ops = { 399 .release = gadget_info_attr_release, 400 }; 401 402 static void gadget_config_attr_release(struct config_item *item) 403 { 404 struct config_usb_cfg *cfg = to_config_usb_cfg(item); 405 406 WARN_ON(!list_empty(&cfg->c.functions)); 407 list_del(&cfg->c.list); 408 kfree(cfg->c.label); 409 kfree(cfg); 410 } 411 412 static int config_usb_cfg_link( 413 struct config_item *usb_cfg_ci, 414 struct config_item *usb_func_ci) 415 { 416 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); 417 struct gadget_info *gi = cfg_to_gadget_info(cfg); 418 419 struct config_group *group = to_config_group(usb_func_ci); 420 struct usb_function_instance *fi = container_of(group, 421 struct usb_function_instance, group); 422 struct usb_function_instance *a_fi; 423 struct usb_function *f; 424 int ret; 425 426 mutex_lock(&gi->lock); 427 /* 428 * Make sure this function is from within our _this_ gadget and not 429 * from another gadget or a random directory. 430 * Also a function instance can only be linked once. 431 */ 432 list_for_each_entry(a_fi, &gi->available_func, cfs_list) { 433 if (a_fi == fi) 434 break; 435 } 436 if (a_fi != fi) { 437 ret = -EINVAL; 438 goto out; 439 } 440 441 list_for_each_entry(f, &cfg->func_list, list) { 442 if (f->fi == fi) { 443 ret = -EEXIST; 444 goto out; 445 } 446 } 447 448 f = usb_get_function(fi); 449 if (IS_ERR(f)) { 450 ret = PTR_ERR(f); 451 goto out; 452 } 453 454 /* stash the function until we bind it to the gadget */ 455 list_add_tail(&f->list, &cfg->func_list); 456 ret = 0; 457 out: 458 mutex_unlock(&gi->lock); 459 return ret; 460 } 461 462 static void config_usb_cfg_unlink( 463 struct config_item *usb_cfg_ci, 464 struct config_item *usb_func_ci) 465 { 466 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); 467 struct gadget_info *gi = cfg_to_gadget_info(cfg); 468 469 struct config_group *group = to_config_group(usb_func_ci); 470 struct usb_function_instance *fi = container_of(group, 471 struct usb_function_instance, group); 472 struct usb_function *f; 473 474 /* 475 * ideally I would like to forbid to unlink functions while a gadget is 476 * bound to an UDC. Since this isn't possible at the moment, we simply 477 * force an unbind, the function is available here and then we can 478 * remove the function. 479 */ 480 mutex_lock(&gi->lock); 481 if (gi->composite.gadget_driver.udc_name) 482 unregister_gadget(gi); 483 WARN_ON(gi->composite.gadget_driver.udc_name); 484 485 list_for_each_entry(f, &cfg->func_list, list) { 486 if (f->fi == fi) { 487 list_del(&f->list); 488 usb_put_function(f); 489 mutex_unlock(&gi->lock); 490 return; 491 } 492 } 493 mutex_unlock(&gi->lock); 494 WARN(1, "Unable to locate function to unbind\n"); 495 } 496 497 static struct configfs_item_operations gadget_config_item_ops = { 498 .release = gadget_config_attr_release, 499 .allow_link = config_usb_cfg_link, 500 .drop_link = config_usb_cfg_unlink, 501 }; 502 503 504 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item, 505 char *page) 506 { 507 struct config_usb_cfg *cfg = to_config_usb_cfg(item); 508 509 return sprintf(page, "%u\n", cfg->c.MaxPower); 510 } 511 512 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item, 513 const char *page, size_t len) 514 { 515 struct config_usb_cfg *cfg = to_config_usb_cfg(item); 516 u16 val; 517 int ret; 518 ret = kstrtou16(page, 0, &val); 519 if (ret) 520 return ret; 521 if (DIV_ROUND_UP(val, 8) > 0xff) 522 return -ERANGE; 523 cfg->c.MaxPower = val; 524 return len; 525 } 526 527 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item, 528 char *page) 529 { 530 struct config_usb_cfg *cfg = to_config_usb_cfg(item); 531 532 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes); 533 } 534 535 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item, 536 const char *page, size_t len) 537 { 538 struct config_usb_cfg *cfg = to_config_usb_cfg(item); 539 u8 val; 540 int ret; 541 ret = kstrtou8(page, 0, &val); 542 if (ret) 543 return ret; 544 if (!(val & USB_CONFIG_ATT_ONE)) 545 return -EINVAL; 546 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | 547 USB_CONFIG_ATT_WAKEUP)) 548 return -EINVAL; 549 cfg->c.bmAttributes = val; 550 return len; 551 } 552 553 CONFIGFS_ATTR(gadget_config_desc_, MaxPower); 554 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes); 555 556 static struct configfs_attribute *gadget_config_attrs[] = { 557 &gadget_config_desc_attr_MaxPower, 558 &gadget_config_desc_attr_bmAttributes, 559 NULL, 560 }; 561 562 static const struct config_item_type gadget_config_type = { 563 .ct_item_ops = &gadget_config_item_ops, 564 .ct_attrs = gadget_config_attrs, 565 .ct_owner = THIS_MODULE, 566 }; 567 568 static const struct config_item_type gadget_root_type = { 569 .ct_item_ops = &gadget_root_item_ops, 570 .ct_attrs = gadget_root_attrs, 571 .ct_owner = THIS_MODULE, 572 }; 573 574 static void composite_init_dev(struct usb_composite_dev *cdev) 575 { 576 spin_lock_init(&cdev->lock); 577 INIT_LIST_HEAD(&cdev->configs); 578 INIT_LIST_HEAD(&cdev->gstrings); 579 } 580 581 static struct config_group *function_make( 582 struct config_group *group, 583 const char *name) 584 { 585 struct gadget_info *gi; 586 struct usb_function_instance *fi; 587 char buf[MAX_NAME_LEN]; 588 char *func_name; 589 char *instance_name; 590 int ret; 591 592 ret = snprintf(buf, MAX_NAME_LEN, "%s", name); 593 if (ret >= MAX_NAME_LEN) 594 return ERR_PTR(-ENAMETOOLONG); 595 596 func_name = buf; 597 instance_name = strchr(func_name, '.'); 598 if (!instance_name) { 599 pr_err("Unable to locate . in FUNC.INSTANCE\n"); 600 return ERR_PTR(-EINVAL); 601 } 602 *instance_name = '\0'; 603 instance_name++; 604 605 fi = usb_get_function_instance(func_name); 606 if (IS_ERR(fi)) 607 return ERR_CAST(fi); 608 609 ret = config_item_set_name(&fi->group.cg_item, "%s", name); 610 if (ret) { 611 usb_put_function_instance(fi); 612 return ERR_PTR(ret); 613 } 614 if (fi->set_inst_name) { 615 ret = fi->set_inst_name(fi, instance_name); 616 if (ret) { 617 usb_put_function_instance(fi); 618 return ERR_PTR(ret); 619 } 620 } 621 622 gi = container_of(group, struct gadget_info, functions_group); 623 624 mutex_lock(&gi->lock); 625 list_add_tail(&fi->cfs_list, &gi->available_func); 626 mutex_unlock(&gi->lock); 627 return &fi->group; 628 } 629 630 static void function_drop( 631 struct config_group *group, 632 struct config_item *item) 633 { 634 struct usb_function_instance *fi = to_usb_function_instance(item); 635 struct gadget_info *gi; 636 637 gi = container_of(group, struct gadget_info, functions_group); 638 639 mutex_lock(&gi->lock); 640 list_del(&fi->cfs_list); 641 mutex_unlock(&gi->lock); 642 config_item_put(item); 643 } 644 645 static struct configfs_group_operations functions_ops = { 646 .make_group = &function_make, 647 .drop_item = &function_drop, 648 }; 649 650 static const struct config_item_type functions_type = { 651 .ct_group_ops = &functions_ops, 652 .ct_owner = THIS_MODULE, 653 }; 654 655 GS_STRINGS_RW(gadget_config_name, configuration); 656 657 static struct configfs_attribute *gadget_config_name_langid_attrs[] = { 658 &gadget_config_name_attr_configuration, 659 NULL, 660 }; 661 662 static void gadget_config_name_attr_release(struct config_item *item) 663 { 664 struct gadget_config_name *cn = to_gadget_config_name(item); 665 666 kfree(cn->configuration); 667 668 list_del(&cn->list); 669 kfree(cn); 670 } 671 672 USB_CONFIG_STRING_RW_OPS(gadget_config_name); 673 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); 674 675 static struct config_group *config_desc_make( 676 struct config_group *group, 677 const char *name) 678 { 679 struct gadget_info *gi; 680 struct config_usb_cfg *cfg; 681 char buf[MAX_NAME_LEN]; 682 char *num_str; 683 u8 num; 684 int ret; 685 686 gi = container_of(group, struct gadget_info, configs_group); 687 ret = snprintf(buf, MAX_NAME_LEN, "%s", name); 688 if (ret >= MAX_NAME_LEN) 689 return ERR_PTR(-ENAMETOOLONG); 690 691 num_str = strchr(buf, '.'); 692 if (!num_str) { 693 pr_err("Unable to locate . in name.bConfigurationValue\n"); 694 return ERR_PTR(-EINVAL); 695 } 696 697 *num_str = '\0'; 698 num_str++; 699 700 if (!strlen(buf)) 701 return ERR_PTR(-EINVAL); 702 703 ret = kstrtou8(num_str, 0, &num); 704 if (ret) 705 return ERR_PTR(ret); 706 707 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 708 if (!cfg) 709 return ERR_PTR(-ENOMEM); 710 cfg->c.label = kstrdup(buf, GFP_KERNEL); 711 if (!cfg->c.label) { 712 ret = -ENOMEM; 713 goto err; 714 } 715 cfg->c.bConfigurationValue = num; 716 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; 717 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; 718 INIT_LIST_HEAD(&cfg->string_list); 719 INIT_LIST_HEAD(&cfg->func_list); 720 721 config_group_init_type_name(&cfg->group, name, 722 &gadget_config_type); 723 724 config_group_init_type_name(&cfg->strings_group, "strings", 725 &gadget_config_name_strings_type); 726 configfs_add_default_group(&cfg->strings_group, &cfg->group); 727 728 ret = usb_add_config_only(&gi->cdev, &cfg->c); 729 if (ret) 730 goto err; 731 732 return &cfg->group; 733 err: 734 kfree(cfg->c.label); 735 kfree(cfg); 736 return ERR_PTR(ret); 737 } 738 739 static void config_desc_drop( 740 struct config_group *group, 741 struct config_item *item) 742 { 743 config_item_put(item); 744 } 745 746 static struct configfs_group_operations config_desc_ops = { 747 .make_group = &config_desc_make, 748 .drop_item = &config_desc_drop, 749 }; 750 751 static const struct config_item_type config_desc_type = { 752 .ct_group_ops = &config_desc_ops, 753 .ct_owner = THIS_MODULE, 754 }; 755 756 GS_STRINGS_RW(gadget_strings, manufacturer); 757 GS_STRINGS_RW(gadget_strings, product); 758 GS_STRINGS_RW(gadget_strings, serialnumber); 759 760 static struct configfs_attribute *gadget_strings_langid_attrs[] = { 761 &gadget_strings_attr_manufacturer, 762 &gadget_strings_attr_product, 763 &gadget_strings_attr_serialnumber, 764 NULL, 765 }; 766 767 static void gadget_strings_attr_release(struct config_item *item) 768 { 769 struct gadget_strings *gs = to_gadget_strings(item); 770 771 kfree(gs->manufacturer); 772 kfree(gs->product); 773 kfree(gs->serialnumber); 774 775 list_del(&gs->list); 776 kfree(gs); 777 } 778 779 USB_CONFIG_STRING_RW_OPS(gadget_strings); 780 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info); 781 782 static inline struct gadget_info *os_desc_item_to_gadget_info( 783 struct config_item *item) 784 { 785 return container_of(to_config_group(item), 786 struct gadget_info, os_desc_group); 787 } 788 789 static ssize_t os_desc_use_show(struct config_item *item, char *page) 790 { 791 return sprintf(page, "%d\n", 792 os_desc_item_to_gadget_info(item)->use_os_desc); 793 } 794 795 static ssize_t os_desc_use_store(struct config_item *item, const char *page, 796 size_t len) 797 { 798 struct gadget_info *gi = os_desc_item_to_gadget_info(item); 799 int ret; 800 bool use; 801 802 mutex_lock(&gi->lock); 803 ret = strtobool(page, &use); 804 if (!ret) { 805 gi->use_os_desc = use; 806 ret = len; 807 } 808 mutex_unlock(&gi->lock); 809 810 return ret; 811 } 812 813 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page) 814 { 815 return sprintf(page, "0x%02x\n", 816 os_desc_item_to_gadget_info(item)->b_vendor_code); 817 } 818 819 static ssize_t os_desc_b_vendor_code_store(struct config_item *item, 820 const char *page, size_t len) 821 { 822 struct gadget_info *gi = os_desc_item_to_gadget_info(item); 823 int ret; 824 u8 b_vendor_code; 825 826 mutex_lock(&gi->lock); 827 ret = kstrtou8(page, 0, &b_vendor_code); 828 if (!ret) { 829 gi->b_vendor_code = b_vendor_code; 830 ret = len; 831 } 832 mutex_unlock(&gi->lock); 833 834 return ret; 835 } 836 837 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page) 838 { 839 struct gadget_info *gi = os_desc_item_to_gadget_info(item); 840 int res; 841 842 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN, 843 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1); 844 page[res++] = '\n'; 845 846 return res; 847 } 848 849 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page, 850 size_t len) 851 { 852 struct gadget_info *gi = os_desc_item_to_gadget_info(item); 853 int res, l; 854 855 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1); 856 if (page[l - 1] == '\n') 857 --l; 858 859 mutex_lock(&gi->lock); 860 res = utf8s_to_utf16s(page, l, 861 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign, 862 OS_STRING_QW_SIGN_LEN); 863 if (res > 0) 864 res = len; 865 mutex_unlock(&gi->lock); 866 867 return res; 868 } 869 870 CONFIGFS_ATTR(os_desc_, use); 871 CONFIGFS_ATTR(os_desc_, b_vendor_code); 872 CONFIGFS_ATTR(os_desc_, qw_sign); 873 874 static struct configfs_attribute *os_desc_attrs[] = { 875 &os_desc_attr_use, 876 &os_desc_attr_b_vendor_code, 877 &os_desc_attr_qw_sign, 878 NULL, 879 }; 880 881 static int os_desc_link(struct config_item *os_desc_ci, 882 struct config_item *usb_cfg_ci) 883 { 884 struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci); 885 struct usb_composite_dev *cdev = &gi->cdev; 886 struct config_usb_cfg *c_target = to_config_usb_cfg(usb_cfg_ci); 887 struct usb_configuration *c; 888 int ret; 889 890 mutex_lock(&gi->lock); 891 list_for_each_entry(c, &cdev->configs, list) { 892 if (c == &c_target->c) 893 break; 894 } 895 if (c != &c_target->c) { 896 ret = -EINVAL; 897 goto out; 898 } 899 900 if (cdev->os_desc_config) { 901 ret = -EBUSY; 902 goto out; 903 } 904 905 cdev->os_desc_config = &c_target->c; 906 ret = 0; 907 908 out: 909 mutex_unlock(&gi->lock); 910 return ret; 911 } 912 913 static void os_desc_unlink(struct config_item *os_desc_ci, 914 struct config_item *usb_cfg_ci) 915 { 916 struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci); 917 struct usb_composite_dev *cdev = &gi->cdev; 918 919 mutex_lock(&gi->lock); 920 if (gi->composite.gadget_driver.udc_name) 921 unregister_gadget(gi); 922 cdev->os_desc_config = NULL; 923 WARN_ON(gi->composite.gadget_driver.udc_name); 924 mutex_unlock(&gi->lock); 925 } 926 927 static struct configfs_item_operations os_desc_ops = { 928 .allow_link = os_desc_link, 929 .drop_link = os_desc_unlink, 930 }; 931 932 static struct config_item_type os_desc_type = { 933 .ct_item_ops = &os_desc_ops, 934 .ct_attrs = os_desc_attrs, 935 .ct_owner = THIS_MODULE, 936 }; 937 938 static inline struct usb_os_desc_ext_prop 939 *to_usb_os_desc_ext_prop(struct config_item *item) 940 { 941 return container_of(item, struct usb_os_desc_ext_prop, item); 942 } 943 944 static ssize_t ext_prop_type_show(struct config_item *item, char *page) 945 { 946 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type); 947 } 948 949 static ssize_t ext_prop_type_store(struct config_item *item, 950 const char *page, size_t len) 951 { 952 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 953 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); 954 u8 type; 955 int ret; 956 957 if (desc->opts_mutex) 958 mutex_lock(desc->opts_mutex); 959 ret = kstrtou8(page, 0, &type); 960 if (ret) 961 goto end; 962 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) { 963 ret = -EINVAL; 964 goto end; 965 } 966 967 if ((ext_prop->type == USB_EXT_PROP_BINARY || 968 ext_prop->type == USB_EXT_PROP_LE32 || 969 ext_prop->type == USB_EXT_PROP_BE32) && 970 (type == USB_EXT_PROP_UNICODE || 971 type == USB_EXT_PROP_UNICODE_ENV || 972 type == USB_EXT_PROP_UNICODE_LINK)) 973 ext_prop->data_len <<= 1; 974 else if ((ext_prop->type == USB_EXT_PROP_UNICODE || 975 ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 976 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) && 977 (type == USB_EXT_PROP_BINARY || 978 type == USB_EXT_PROP_LE32 || 979 type == USB_EXT_PROP_BE32)) 980 ext_prop->data_len >>= 1; 981 ext_prop->type = type; 982 ret = len; 983 984 end: 985 if (desc->opts_mutex) 986 mutex_unlock(desc->opts_mutex); 987 return ret; 988 } 989 990 static ssize_t ext_prop_data_show(struct config_item *item, char *page) 991 { 992 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 993 int len = ext_prop->data_len; 994 995 if (ext_prop->type == USB_EXT_PROP_UNICODE || 996 ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 997 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) 998 len >>= 1; 999 memcpy(page, ext_prop->data, len); 1000 1001 return len; 1002 } 1003 1004 static ssize_t ext_prop_data_store(struct config_item *item, 1005 const char *page, size_t len) 1006 { 1007 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 1008 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); 1009 char *new_data; 1010 size_t ret_len = len; 1011 1012 if (page[len - 1] == '\n' || page[len - 1] == '\0') 1013 --len; 1014 new_data = kmemdup(page, len, GFP_KERNEL); 1015 if (!new_data) 1016 return -ENOMEM; 1017 1018 if (desc->opts_mutex) 1019 mutex_lock(desc->opts_mutex); 1020 kfree(ext_prop->data); 1021 ext_prop->data = new_data; 1022 desc->ext_prop_len -= ext_prop->data_len; 1023 ext_prop->data_len = len; 1024 desc->ext_prop_len += ext_prop->data_len; 1025 if (ext_prop->type == USB_EXT_PROP_UNICODE || 1026 ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 1027 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) { 1028 desc->ext_prop_len -= ext_prop->data_len; 1029 ext_prop->data_len <<= 1; 1030 ext_prop->data_len += 2; 1031 desc->ext_prop_len += ext_prop->data_len; 1032 } 1033 if (desc->opts_mutex) 1034 mutex_unlock(desc->opts_mutex); 1035 return ret_len; 1036 } 1037 1038 CONFIGFS_ATTR(ext_prop_, type); 1039 CONFIGFS_ATTR(ext_prop_, data); 1040 1041 static struct configfs_attribute *ext_prop_attrs[] = { 1042 &ext_prop_attr_type, 1043 &ext_prop_attr_data, 1044 NULL, 1045 }; 1046 1047 static void usb_os_desc_ext_prop_release(struct config_item *item) 1048 { 1049 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 1050 1051 kfree(ext_prop); /* frees a whole chunk */ 1052 } 1053 1054 static struct configfs_item_operations ext_prop_ops = { 1055 .release = usb_os_desc_ext_prop_release, 1056 }; 1057 1058 static struct config_item *ext_prop_make( 1059 struct config_group *group, 1060 const char *name) 1061 { 1062 struct usb_os_desc_ext_prop *ext_prop; 1063 struct config_item_type *ext_prop_type; 1064 struct usb_os_desc *desc; 1065 char *vlabuf; 1066 1067 vla_group(data_chunk); 1068 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1); 1069 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1); 1070 1071 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); 1072 if (!vlabuf) 1073 return ERR_PTR(-ENOMEM); 1074 1075 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop); 1076 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type); 1077 1078 desc = container_of(group, struct usb_os_desc, group); 1079 ext_prop_type->ct_item_ops = &ext_prop_ops; 1080 ext_prop_type->ct_attrs = ext_prop_attrs; 1081 ext_prop_type->ct_owner = desc->owner; 1082 1083 config_item_init_type_name(&ext_prop->item, name, ext_prop_type); 1084 1085 ext_prop->name = kstrdup(name, GFP_KERNEL); 1086 if (!ext_prop->name) { 1087 kfree(vlabuf); 1088 return ERR_PTR(-ENOMEM); 1089 } 1090 desc->ext_prop_len += 14; 1091 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2; 1092 if (desc->opts_mutex) 1093 mutex_lock(desc->opts_mutex); 1094 desc->ext_prop_len += ext_prop->name_len; 1095 list_add_tail(&ext_prop->entry, &desc->ext_prop); 1096 ++desc->ext_prop_count; 1097 if (desc->opts_mutex) 1098 mutex_unlock(desc->opts_mutex); 1099 1100 return &ext_prop->item; 1101 } 1102 1103 static void ext_prop_drop(struct config_group *group, struct config_item *item) 1104 { 1105 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 1106 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item); 1107 1108 if (desc->opts_mutex) 1109 mutex_lock(desc->opts_mutex); 1110 list_del(&ext_prop->entry); 1111 --desc->ext_prop_count; 1112 kfree(ext_prop->name); 1113 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14); 1114 if (desc->opts_mutex) 1115 mutex_unlock(desc->opts_mutex); 1116 config_item_put(item); 1117 } 1118 1119 static struct configfs_group_operations interf_grp_ops = { 1120 .make_item = &ext_prop_make, 1121 .drop_item = &ext_prop_drop, 1122 }; 1123 1124 static ssize_t interf_grp_compatible_id_show(struct config_item *item, 1125 char *page) 1126 { 1127 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8); 1128 return 8; 1129 } 1130 1131 static ssize_t interf_grp_compatible_id_store(struct config_item *item, 1132 const char *page, size_t len) 1133 { 1134 struct usb_os_desc *desc = to_usb_os_desc(item); 1135 int l; 1136 1137 l = min_t(int, 8, len); 1138 if (page[l - 1] == '\n') 1139 --l; 1140 if (desc->opts_mutex) 1141 mutex_lock(desc->opts_mutex); 1142 memcpy(desc->ext_compat_id, page, l); 1143 1144 if (desc->opts_mutex) 1145 mutex_unlock(desc->opts_mutex); 1146 1147 return len; 1148 } 1149 1150 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item, 1151 char *page) 1152 { 1153 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8); 1154 return 8; 1155 } 1156 1157 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item, 1158 const char *page, size_t len) 1159 { 1160 struct usb_os_desc *desc = to_usb_os_desc(item); 1161 int l; 1162 1163 l = min_t(int, 8, len); 1164 if (page[l - 1] == '\n') 1165 --l; 1166 if (desc->opts_mutex) 1167 mutex_lock(desc->opts_mutex); 1168 memcpy(desc->ext_compat_id + 8, page, l); 1169 1170 if (desc->opts_mutex) 1171 mutex_unlock(desc->opts_mutex); 1172 1173 return len; 1174 } 1175 1176 CONFIGFS_ATTR(interf_grp_, compatible_id); 1177 CONFIGFS_ATTR(interf_grp_, sub_compatible_id); 1178 1179 static struct configfs_attribute *interf_grp_attrs[] = { 1180 &interf_grp_attr_compatible_id, 1181 &interf_grp_attr_sub_compatible_id, 1182 NULL 1183 }; 1184 1185 struct config_group *usb_os_desc_prepare_interf_dir( 1186 struct config_group *parent, 1187 int n_interf, 1188 struct usb_os_desc **desc, 1189 char **names, 1190 struct module *owner) 1191 { 1192 struct config_group *os_desc_group; 1193 struct config_item_type *os_desc_type, *interface_type; 1194 1195 vla_group(data_chunk); 1196 vla_item(data_chunk, struct config_group, os_desc_group, 1); 1197 vla_item(data_chunk, struct config_item_type, os_desc_type, 1); 1198 vla_item(data_chunk, struct config_item_type, interface_type, 1); 1199 1200 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); 1201 if (!vlabuf) 1202 return ERR_PTR(-ENOMEM); 1203 1204 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group); 1205 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type); 1206 interface_type = vla_ptr(vlabuf, data_chunk, interface_type); 1207 1208 os_desc_type->ct_owner = owner; 1209 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type); 1210 configfs_add_default_group(os_desc_group, parent); 1211 1212 interface_type->ct_group_ops = &interf_grp_ops; 1213 interface_type->ct_attrs = interf_grp_attrs; 1214 interface_type->ct_owner = owner; 1215 1216 while (n_interf--) { 1217 struct usb_os_desc *d; 1218 1219 d = desc[n_interf]; 1220 d->owner = owner; 1221 config_group_init_type_name(&d->group, "", interface_type); 1222 config_item_set_name(&d->group.cg_item, "interface.%s", 1223 names[n_interf]); 1224 configfs_add_default_group(&d->group, os_desc_group); 1225 } 1226 1227 return os_desc_group; 1228 } 1229 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir); 1230 1231 static int configfs_do_nothing(struct usb_composite_dev *cdev) 1232 { 1233 WARN_ON(1); 1234 return -EINVAL; 1235 } 1236 1237 int composite_dev_prepare(struct usb_composite_driver *composite, 1238 struct usb_composite_dev *dev); 1239 1240 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, 1241 struct usb_ep *ep0); 1242 1243 static void purge_configs_funcs(struct gadget_info *gi) 1244 { 1245 struct usb_configuration *c; 1246 1247 list_for_each_entry(c, &gi->cdev.configs, list) { 1248 struct usb_function *f, *tmp; 1249 struct config_usb_cfg *cfg; 1250 1251 cfg = container_of(c, struct config_usb_cfg, c); 1252 1253 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) { 1254 1255 list_move(&f->list, &cfg->func_list); 1256 if (f->unbind) { 1257 dev_dbg(&gi->cdev.gadget->dev, 1258 "unbind function '%s'/%p\n", 1259 f->name, f); 1260 f->unbind(c, f); 1261 } 1262 } 1263 c->next_interface_id = 0; 1264 memset(c->interface, 0, sizeof(c->interface)); 1265 c->superspeed_plus = 0; 1266 c->superspeed = 0; 1267 c->highspeed = 0; 1268 c->fullspeed = 0; 1269 } 1270 } 1271 1272 static int configfs_composite_bind(struct usb_gadget *gadget, 1273 struct usb_gadget_driver *gdriver) 1274 { 1275 struct usb_composite_driver *composite = to_cdriver(gdriver); 1276 struct gadget_info *gi = container_of(composite, 1277 struct gadget_info, composite); 1278 struct usb_composite_dev *cdev = &gi->cdev; 1279 struct usb_configuration *c; 1280 struct usb_string *s; 1281 unsigned i; 1282 int ret; 1283 1284 /* the gi->lock is hold by the caller */ 1285 gi->unbind = 0; 1286 cdev->gadget = gadget; 1287 set_gadget_data(gadget, cdev); 1288 ret = composite_dev_prepare(composite, cdev); 1289 if (ret) 1290 return ret; 1291 /* and now the gadget bind */ 1292 ret = -EINVAL; 1293 1294 if (list_empty(&gi->cdev.configs)) { 1295 pr_err("Need at least one configuration in %s.\n", 1296 gi->composite.name); 1297 goto err_comp_cleanup; 1298 } 1299 1300 1301 list_for_each_entry(c, &gi->cdev.configs, list) { 1302 struct config_usb_cfg *cfg; 1303 1304 cfg = container_of(c, struct config_usb_cfg, c); 1305 if (list_empty(&cfg->func_list)) { 1306 pr_err("Config %s/%d of %s needs at least one function.\n", 1307 c->label, c->bConfigurationValue, 1308 gi->composite.name); 1309 goto err_comp_cleanup; 1310 } 1311 } 1312 1313 /* init all strings */ 1314 if (!list_empty(&gi->string_list)) { 1315 struct gadget_strings *gs; 1316 1317 i = 0; 1318 list_for_each_entry(gs, &gi->string_list, list) { 1319 1320 gi->gstrings[i] = &gs->stringtab_dev; 1321 gs->stringtab_dev.strings = gs->strings; 1322 gs->strings[USB_GADGET_MANUFACTURER_IDX].s = 1323 gs->manufacturer; 1324 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; 1325 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; 1326 i++; 1327 } 1328 gi->gstrings[i] = NULL; 1329 s = usb_gstrings_attach(&gi->cdev, gi->gstrings, 1330 USB_GADGET_FIRST_AVAIL_IDX); 1331 if (IS_ERR(s)) { 1332 ret = PTR_ERR(s); 1333 goto err_comp_cleanup; 1334 } 1335 1336 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; 1337 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; 1338 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; 1339 } 1340 1341 if (gi->use_os_desc) { 1342 cdev->use_os_string = true; 1343 cdev->b_vendor_code = gi->b_vendor_code; 1344 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN); 1345 } 1346 1347 if (gadget_is_otg(gadget) && !otg_desc[0]) { 1348 struct usb_descriptor_header *usb_desc; 1349 1350 usb_desc = usb_otg_descriptor_alloc(gadget); 1351 if (!usb_desc) { 1352 ret = -ENOMEM; 1353 goto err_comp_cleanup; 1354 } 1355 usb_otg_descriptor_init(gadget, usb_desc); 1356 otg_desc[0] = usb_desc; 1357 otg_desc[1] = NULL; 1358 } 1359 1360 /* Go through all configs, attach all functions */ 1361 list_for_each_entry(c, &gi->cdev.configs, list) { 1362 struct config_usb_cfg *cfg; 1363 struct usb_function *f; 1364 struct usb_function *tmp; 1365 struct gadget_config_name *cn; 1366 1367 if (gadget_is_otg(gadget)) 1368 c->descriptors = otg_desc; 1369 1370 cfg = container_of(c, struct config_usb_cfg, c); 1371 if (!list_empty(&cfg->string_list)) { 1372 i = 0; 1373 list_for_each_entry(cn, &cfg->string_list, list) { 1374 cfg->gstrings[i] = &cn->stringtab_dev; 1375 cn->stringtab_dev.strings = &cn->strings; 1376 cn->strings.s = cn->configuration; 1377 i++; 1378 } 1379 cfg->gstrings[i] = NULL; 1380 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); 1381 if (IS_ERR(s)) { 1382 ret = PTR_ERR(s); 1383 goto err_comp_cleanup; 1384 } 1385 c->iConfiguration = s[0].id; 1386 } 1387 1388 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { 1389 list_del(&f->list); 1390 ret = usb_add_function(c, f); 1391 if (ret) { 1392 list_add(&f->list, &cfg->func_list); 1393 goto err_purge_funcs; 1394 } 1395 } 1396 ret = usb_gadget_check_config(cdev->gadget); 1397 if (ret) 1398 goto err_purge_funcs; 1399 1400 usb_ep_autoconfig_reset(cdev->gadget); 1401 } 1402 if (cdev->use_os_string) { 1403 ret = composite_os_desc_req_prepare(cdev, gadget->ep0); 1404 if (ret) 1405 goto err_purge_funcs; 1406 } 1407 1408 usb_ep_autoconfig_reset(cdev->gadget); 1409 return 0; 1410 1411 err_purge_funcs: 1412 purge_configs_funcs(gi); 1413 err_comp_cleanup: 1414 composite_dev_cleanup(cdev); 1415 return ret; 1416 } 1417 1418 static void configfs_composite_unbind(struct usb_gadget *gadget) 1419 { 1420 struct usb_composite_dev *cdev; 1421 struct gadget_info *gi; 1422 unsigned long flags; 1423 1424 /* the gi->lock is hold by the caller */ 1425 1426 cdev = get_gadget_data(gadget); 1427 gi = container_of(cdev, struct gadget_info, cdev); 1428 spin_lock_irqsave(&gi->spinlock, flags); 1429 gi->unbind = 1; 1430 spin_unlock_irqrestore(&gi->spinlock, flags); 1431 1432 kfree(otg_desc[0]); 1433 otg_desc[0] = NULL; 1434 purge_configs_funcs(gi); 1435 composite_dev_cleanup(cdev); 1436 usb_ep_autoconfig_reset(cdev->gadget); 1437 spin_lock_irqsave(&gi->spinlock, flags); 1438 cdev->gadget = NULL; 1439 set_gadget_data(gadget, NULL); 1440 spin_unlock_irqrestore(&gi->spinlock, flags); 1441 } 1442 1443 static int configfs_composite_setup(struct usb_gadget *gadget, 1444 const struct usb_ctrlrequest *ctrl) 1445 { 1446 struct usb_composite_dev *cdev; 1447 struct gadget_info *gi; 1448 unsigned long flags; 1449 int ret; 1450 1451 cdev = get_gadget_data(gadget); 1452 if (!cdev) 1453 return 0; 1454 1455 gi = container_of(cdev, struct gadget_info, cdev); 1456 spin_lock_irqsave(&gi->spinlock, flags); 1457 cdev = get_gadget_data(gadget); 1458 if (!cdev || gi->unbind) { 1459 spin_unlock_irqrestore(&gi->spinlock, flags); 1460 return 0; 1461 } 1462 1463 ret = composite_setup(gadget, ctrl); 1464 spin_unlock_irqrestore(&gi->spinlock, flags); 1465 return ret; 1466 } 1467 1468 static void configfs_composite_disconnect(struct usb_gadget *gadget) 1469 { 1470 struct usb_composite_dev *cdev; 1471 struct gadget_info *gi; 1472 unsigned long flags; 1473 1474 cdev = get_gadget_data(gadget); 1475 if (!cdev) 1476 return; 1477 1478 gi = container_of(cdev, struct gadget_info, cdev); 1479 spin_lock_irqsave(&gi->spinlock, flags); 1480 cdev = get_gadget_data(gadget); 1481 if (!cdev || gi->unbind) { 1482 spin_unlock_irqrestore(&gi->spinlock, flags); 1483 return; 1484 } 1485 1486 composite_disconnect(gadget); 1487 spin_unlock_irqrestore(&gi->spinlock, flags); 1488 } 1489 1490 static void configfs_composite_reset(struct usb_gadget *gadget) 1491 { 1492 struct usb_composite_dev *cdev; 1493 struct gadget_info *gi; 1494 unsigned long flags; 1495 1496 cdev = get_gadget_data(gadget); 1497 if (!cdev) 1498 return; 1499 1500 gi = container_of(cdev, struct gadget_info, cdev); 1501 spin_lock_irqsave(&gi->spinlock, flags); 1502 cdev = get_gadget_data(gadget); 1503 if (!cdev || gi->unbind) { 1504 spin_unlock_irqrestore(&gi->spinlock, flags); 1505 return; 1506 } 1507 1508 composite_reset(gadget); 1509 spin_unlock_irqrestore(&gi->spinlock, flags); 1510 } 1511 1512 static void configfs_composite_suspend(struct usb_gadget *gadget) 1513 { 1514 struct usb_composite_dev *cdev; 1515 struct gadget_info *gi; 1516 unsigned long flags; 1517 1518 cdev = get_gadget_data(gadget); 1519 if (!cdev) 1520 return; 1521 1522 gi = container_of(cdev, struct gadget_info, cdev); 1523 spin_lock_irqsave(&gi->spinlock, flags); 1524 cdev = get_gadget_data(gadget); 1525 if (!cdev || gi->unbind) { 1526 spin_unlock_irqrestore(&gi->spinlock, flags); 1527 return; 1528 } 1529 1530 composite_suspend(gadget); 1531 spin_unlock_irqrestore(&gi->spinlock, flags); 1532 } 1533 1534 static void configfs_composite_resume(struct usb_gadget *gadget) 1535 { 1536 struct usb_composite_dev *cdev; 1537 struct gadget_info *gi; 1538 unsigned long flags; 1539 1540 cdev = get_gadget_data(gadget); 1541 if (!cdev) 1542 return; 1543 1544 gi = container_of(cdev, struct gadget_info, cdev); 1545 spin_lock_irqsave(&gi->spinlock, flags); 1546 cdev = get_gadget_data(gadget); 1547 if (!cdev || gi->unbind) { 1548 spin_unlock_irqrestore(&gi->spinlock, flags); 1549 return; 1550 } 1551 1552 composite_resume(gadget); 1553 spin_unlock_irqrestore(&gi->spinlock, flags); 1554 } 1555 1556 static const struct usb_gadget_driver configfs_driver_template = { 1557 .bind = configfs_composite_bind, 1558 .unbind = configfs_composite_unbind, 1559 1560 .setup = configfs_composite_setup, 1561 .reset = configfs_composite_reset, 1562 .disconnect = configfs_composite_disconnect, 1563 1564 .suspend = configfs_composite_suspend, 1565 .resume = configfs_composite_resume, 1566 1567 .max_speed = USB_SPEED_SUPER_PLUS, 1568 .driver = { 1569 .owner = THIS_MODULE, 1570 .name = "configfs-gadget", 1571 }, 1572 .match_existing_only = 1, 1573 }; 1574 1575 static struct config_group *gadgets_make( 1576 struct config_group *group, 1577 const char *name) 1578 { 1579 struct gadget_info *gi; 1580 1581 gi = kzalloc(sizeof(*gi), GFP_KERNEL); 1582 if (!gi) 1583 return ERR_PTR(-ENOMEM); 1584 1585 config_group_init_type_name(&gi->group, name, &gadget_root_type); 1586 1587 config_group_init_type_name(&gi->functions_group, "functions", 1588 &functions_type); 1589 configfs_add_default_group(&gi->functions_group, &gi->group); 1590 1591 config_group_init_type_name(&gi->configs_group, "configs", 1592 &config_desc_type); 1593 configfs_add_default_group(&gi->configs_group, &gi->group); 1594 1595 config_group_init_type_name(&gi->strings_group, "strings", 1596 &gadget_strings_strings_type); 1597 configfs_add_default_group(&gi->strings_group, &gi->group); 1598 1599 config_group_init_type_name(&gi->os_desc_group, "os_desc", 1600 &os_desc_type); 1601 configfs_add_default_group(&gi->os_desc_group, &gi->group); 1602 1603 gi->composite.bind = configfs_do_nothing; 1604 gi->composite.unbind = configfs_do_nothing; 1605 gi->composite.suspend = NULL; 1606 gi->composite.resume = NULL; 1607 gi->composite.max_speed = USB_SPEED_SUPER_PLUS; 1608 1609 spin_lock_init(&gi->spinlock); 1610 mutex_init(&gi->lock); 1611 INIT_LIST_HEAD(&gi->string_list); 1612 INIT_LIST_HEAD(&gi->available_func); 1613 1614 composite_init_dev(&gi->cdev); 1615 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; 1616 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; 1617 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); 1618 1619 gi->composite.gadget_driver = configfs_driver_template; 1620 1621 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); 1622 gi->composite.name = gi->composite.gadget_driver.function; 1623 1624 if (!gi->composite.gadget_driver.function) 1625 goto err; 1626 1627 return &gi->group; 1628 err: 1629 kfree(gi); 1630 return ERR_PTR(-ENOMEM); 1631 } 1632 1633 static void gadgets_drop(struct config_group *group, struct config_item *item) 1634 { 1635 config_item_put(item); 1636 } 1637 1638 static struct configfs_group_operations gadgets_ops = { 1639 .make_group = &gadgets_make, 1640 .drop_item = &gadgets_drop, 1641 }; 1642 1643 static const struct config_item_type gadgets_type = { 1644 .ct_group_ops = &gadgets_ops, 1645 .ct_owner = THIS_MODULE, 1646 }; 1647 1648 static struct configfs_subsystem gadget_subsys = { 1649 .su_group = { 1650 .cg_item = { 1651 .ci_namebuf = "usb_gadget", 1652 .ci_type = &gadgets_type, 1653 }, 1654 }, 1655 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), 1656 }; 1657 1658 void unregister_gadget_item(struct config_item *item) 1659 { 1660 struct gadget_info *gi = to_gadget_info(item); 1661 1662 mutex_lock(&gi->lock); 1663 unregister_gadget(gi); 1664 mutex_unlock(&gi->lock); 1665 } 1666 EXPORT_SYMBOL_GPL(unregister_gadget_item); 1667 1668 static int __init gadget_cfs_init(void) 1669 { 1670 int ret; 1671 1672 config_group_init(&gadget_subsys.su_group); 1673 1674 ret = configfs_register_subsystem(&gadget_subsys); 1675 return ret; 1676 } 1677 module_init(gadget_cfs_init); 1678 1679 static void __exit gadget_cfs_exit(void) 1680 { 1681 configfs_unregister_subsystem(&gadget_subsys); 1682 } 1683 module_exit(gadget_cfs_exit); 1684