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