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