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