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