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>
7 #include <linux/usb/composite.h>
8 #include <linux/usb/gadget_configfs.h>
11 #include "u_os_desc.h"
13 int check_user_usb_string(const char *name,
14 struct usb_gadget_strings *stringtab_dev)
19 ret = kstrtou16(name, 0, &num);
23 if (!usb_validate_langid(num))
26 stringtab_dev->language = num;
30 #define MAX_NAME_LEN 40
31 #define MAX_USB_STRING_LANGS 2
33 static const struct usb_descriptor_header *otg_desc[2];
36 struct config_group group;
37 struct config_group functions_group;
38 struct config_group configs_group;
39 struct config_group strings_group;
40 struct config_group os_desc_group;
43 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
44 struct list_head string_list;
45 struct list_head available_func;
47 struct usb_composite_driver composite;
48 struct usb_composite_dev cdev;
51 char qw_sign[OS_STRING_QW_SIGN_LEN];
56 static inline struct gadget_info *to_gadget_info(struct config_item *item)
58 return container_of(to_config_group(item), struct gadget_info, group);
61 struct config_usb_cfg {
62 struct config_group group;
63 struct config_group strings_group;
64 struct list_head string_list;
65 struct usb_configuration c;
66 struct list_head func_list;
67 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
70 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
72 return container_of(to_config_group(item), struct config_usb_cfg,
76 static inline struct gadget_info *cfg_to_gadget_info(struct config_usb_cfg *cfg)
78 return container_of(cfg->c.cdev, struct gadget_info, cdev);
81 struct gadget_strings {
82 struct usb_gadget_strings stringtab_dev;
83 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
88 struct config_group group;
89 struct list_head list;
92 struct gadget_config_name {
93 struct usb_gadget_strings stringtab_dev;
94 struct usb_string strings;
97 struct config_group group;
98 struct list_head list;
101 #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1)
103 static int usb_string_copy(const char *s, char **s_copy)
107 char *copy = *s_copy;
109 if (ret > USB_MAX_STRING_LEN)
115 str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
120 if (str[ret - 1] == '\n')
126 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
127 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
130 return sprintf(page, "0x%02x\n", \
131 to_gadget_info(item)->cdev.desc.__name); \
134 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
135 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
138 return sprintf(page, "0x%04x\n", \
139 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
143 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
144 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
145 const char *page, size_t len) \
149 ret = kstrtou8(page, 0, &val); \
152 to_gadget_info(item)->cdev.desc._name = val; \
156 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
157 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
158 const char *page, size_t len) \
162 ret = kstrtou16(page, 0, &val); \
165 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
169 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
170 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
171 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
173 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
174 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
175 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
176 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
177 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
178 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
179 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
180 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
182 static ssize_t is_valid_bcd(u16 bcd_val)
184 if ((bcd_val & 0xf) > 9)
186 if (((bcd_val >> 4) & 0xf) > 9)
188 if (((bcd_val >> 8) & 0xf) > 9)
190 if (((bcd_val >> 12) & 0xf) > 9)
195 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
196 const char *page, size_t len)
201 ret = kstrtou16(page, 0, &bcdDevice);
204 ret = is_valid_bcd(bcdDevice);
208 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
212 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
213 const char *page, size_t len)
218 ret = kstrtou16(page, 0, &bcdUSB);
221 ret = is_valid_bcd(bcdUSB);
225 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
229 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
231 struct gadget_info *gi = to_gadget_info(item);
235 mutex_lock(&gi->lock);
236 udc_name = gi->composite.gadget_driver.udc_name;
237 ret = sprintf(page, "%s\n", udc_name ?: "");
238 mutex_unlock(&gi->lock);
243 static int unregister_gadget(struct gadget_info *gi)
247 if (!gi->composite.gadget_driver.udc_name)
250 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
253 kfree(gi->composite.gadget_driver.udc_name);
254 gi->composite.gadget_driver.udc_name = NULL;
258 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
259 const char *page, size_t len)
261 struct gadget_info *gi = to_gadget_info(item);
265 if (strlen(page) < len)
268 name = kstrdup(page, GFP_KERNEL);
271 if (name[len - 1] == '\n')
272 name[len - 1] = '\0';
274 mutex_lock(&gi->lock);
277 ret = unregister_gadget(gi);
282 if (gi->composite.gadget_driver.udc_name) {
286 gi->composite.gadget_driver.udc_name = name;
287 ret = usb_gadget_register_driver(&gi->composite.gadget_driver);
289 gi->composite.gadget_driver.udc_name = NULL;
293 mutex_unlock(&gi->lock);
297 mutex_unlock(&gi->lock);
301 static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
304 enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
306 return sprintf(page, "%s\n", usb_speed_string(speed));
309 static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
310 const char *page, size_t len)
312 struct gadget_info *gi = to_gadget_info(item);
314 mutex_lock(&gi->lock);
316 /* Prevent changing of max_speed after the driver is binded */
317 if (gi->composite.gadget_driver.udc_name)
320 if (strncmp(page, "super-speed-plus", 16) == 0)
321 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
322 else if (strncmp(page, "super-speed", 11) == 0)
323 gi->composite.max_speed = USB_SPEED_SUPER;
324 else if (strncmp(page, "high-speed", 10) == 0)
325 gi->composite.max_speed = USB_SPEED_HIGH;
326 else if (strncmp(page, "full-speed", 10) == 0)
327 gi->composite.max_speed = USB_SPEED_FULL;
328 else if (strncmp(page, "low-speed", 9) == 0)
329 gi->composite.max_speed = USB_SPEED_LOW;
333 gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
335 mutex_unlock(&gi->lock);
338 mutex_unlock(&gi->lock);
342 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
343 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
344 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
345 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
346 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
347 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
348 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
349 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
350 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
351 CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
353 static struct configfs_attribute *gadget_root_attrs[] = {
354 &gadget_dev_desc_attr_bDeviceClass,
355 &gadget_dev_desc_attr_bDeviceSubClass,
356 &gadget_dev_desc_attr_bDeviceProtocol,
357 &gadget_dev_desc_attr_bMaxPacketSize0,
358 &gadget_dev_desc_attr_idVendor,
359 &gadget_dev_desc_attr_idProduct,
360 &gadget_dev_desc_attr_bcdDevice,
361 &gadget_dev_desc_attr_bcdUSB,
362 &gadget_dev_desc_attr_UDC,
363 &gadget_dev_desc_attr_max_speed,
367 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
369 return container_of(to_config_group(item), struct gadget_strings,
373 static inline struct gadget_config_name *to_gadget_config_name(
374 struct config_item *item)
376 return container_of(to_config_group(item), struct gadget_config_name,
380 static inline struct usb_function_instance *to_usb_function_instance(
381 struct config_item *item)
383 return container_of(to_config_group(item),
384 struct usb_function_instance, group);
387 static void gadget_info_attr_release(struct config_item *item)
389 struct gadget_info *gi = to_gadget_info(item);
391 WARN_ON(!list_empty(&gi->cdev.configs));
392 WARN_ON(!list_empty(&gi->string_list));
393 WARN_ON(!list_empty(&gi->available_func));
394 kfree(gi->composite.gadget_driver.function);
398 static struct configfs_item_operations gadget_root_item_ops = {
399 .release = gadget_info_attr_release,
402 static void gadget_config_attr_release(struct config_item *item)
404 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
406 WARN_ON(!list_empty(&cfg->c.functions));
407 list_del(&cfg->c.list);
412 static int config_usb_cfg_link(
413 struct config_item *usb_cfg_ci,
414 struct config_item *usb_func_ci)
416 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
417 struct gadget_info *gi = cfg_to_gadget_info(cfg);
419 struct usb_function_instance *fi =
420 to_usb_function_instance(usb_func_ci);
421 struct usb_function_instance *a_fi = NULL, *iter;
422 struct usb_function *f;
425 mutex_lock(&gi->lock);
427 * Make sure this function is from within our _this_ gadget and not
428 * from another gadget or a random directory.
429 * Also a function instance can only be linked once.
431 list_for_each_entry(iter, &gi->available_func, cfs_list) {
442 list_for_each_entry(f, &cfg->func_list, list) {
449 f = usb_get_function(fi);
455 /* stash the function until we bind it to the gadget */
456 list_add_tail(&f->list, &cfg->func_list);
459 mutex_unlock(&gi->lock);
463 static void config_usb_cfg_unlink(
464 struct config_item *usb_cfg_ci,
465 struct config_item *usb_func_ci)
467 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
468 struct gadget_info *gi = cfg_to_gadget_info(cfg);
470 struct usb_function_instance *fi =
471 to_usb_function_instance(usb_func_ci);
472 struct usb_function *f;
475 * ideally I would like to forbid to unlink functions while a gadget is
476 * bound to an UDC. Since this isn't possible at the moment, we simply
477 * force an unbind, the function is available here and then we can
478 * remove the function.
480 mutex_lock(&gi->lock);
481 if (gi->composite.gadget_driver.udc_name)
482 unregister_gadget(gi);
483 WARN_ON(gi->composite.gadget_driver.udc_name);
485 list_for_each_entry(f, &cfg->func_list, list) {
489 mutex_unlock(&gi->lock);
493 mutex_unlock(&gi->lock);
494 WARN(1, "Unable to locate function to unbind\n");
497 static struct configfs_item_operations gadget_config_item_ops = {
498 .release = gadget_config_attr_release,
499 .allow_link = config_usb_cfg_link,
500 .drop_link = config_usb_cfg_unlink,
504 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
507 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
509 return sprintf(page, "%u\n", cfg->c.MaxPower);
512 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
513 const char *page, size_t len)
515 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
518 ret = kstrtou16(page, 0, &val);
521 if (DIV_ROUND_UP(val, 8) > 0xff)
523 cfg->c.MaxPower = val;
527 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
530 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
532 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
535 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
536 const char *page, size_t len)
538 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
541 ret = kstrtou8(page, 0, &val);
544 if (!(val & USB_CONFIG_ATT_ONE))
546 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
547 USB_CONFIG_ATT_WAKEUP))
549 cfg->c.bmAttributes = val;
553 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
554 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
556 static struct configfs_attribute *gadget_config_attrs[] = {
557 &gadget_config_desc_attr_MaxPower,
558 &gadget_config_desc_attr_bmAttributes,
562 static const struct config_item_type gadget_config_type = {
563 .ct_item_ops = &gadget_config_item_ops,
564 .ct_attrs = gadget_config_attrs,
565 .ct_owner = THIS_MODULE,
568 static const struct config_item_type gadget_root_type = {
569 .ct_item_ops = &gadget_root_item_ops,
570 .ct_attrs = gadget_root_attrs,
571 .ct_owner = THIS_MODULE,
574 static void composite_init_dev(struct usb_composite_dev *cdev)
576 spin_lock_init(&cdev->lock);
577 INIT_LIST_HEAD(&cdev->configs);
578 INIT_LIST_HEAD(&cdev->gstrings);
581 static struct config_group *function_make(
582 struct config_group *group,
585 struct gadget_info *gi;
586 struct usb_function_instance *fi;
587 char buf[MAX_NAME_LEN];
592 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
593 if (ret >= MAX_NAME_LEN)
594 return ERR_PTR(-ENAMETOOLONG);
597 instance_name = strchr(func_name, '.');
598 if (!instance_name) {
599 pr_err("Unable to locate . in FUNC.INSTANCE\n");
600 return ERR_PTR(-EINVAL);
602 *instance_name = '\0';
605 fi = usb_get_function_instance(func_name);
609 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
611 usb_put_function_instance(fi);
614 if (fi->set_inst_name) {
615 ret = fi->set_inst_name(fi, instance_name);
617 usb_put_function_instance(fi);
622 gi = container_of(group, struct gadget_info, functions_group);
624 mutex_lock(&gi->lock);
625 list_add_tail(&fi->cfs_list, &gi->available_func);
626 mutex_unlock(&gi->lock);
630 static void function_drop(
631 struct config_group *group,
632 struct config_item *item)
634 struct usb_function_instance *fi = to_usb_function_instance(item);
635 struct gadget_info *gi;
637 gi = container_of(group, struct gadget_info, functions_group);
639 mutex_lock(&gi->lock);
640 list_del(&fi->cfs_list);
641 mutex_unlock(&gi->lock);
642 config_item_put(item);
645 static struct configfs_group_operations functions_ops = {
646 .make_group = &function_make,
647 .drop_item = &function_drop,
650 static const struct config_item_type functions_type = {
651 .ct_group_ops = &functions_ops,
652 .ct_owner = THIS_MODULE,
655 GS_STRINGS_RW(gadget_config_name, configuration);
657 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
658 &gadget_config_name_attr_configuration,
662 static void gadget_config_name_attr_release(struct config_item *item)
664 struct gadget_config_name *cn = to_gadget_config_name(item);
666 kfree(cn->configuration);
672 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
673 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
675 static struct config_group *config_desc_make(
676 struct config_group *group,
679 struct gadget_info *gi;
680 struct config_usb_cfg *cfg;
681 char buf[MAX_NAME_LEN];
686 gi = container_of(group, struct gadget_info, configs_group);
687 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
688 if (ret >= MAX_NAME_LEN)
689 return ERR_PTR(-ENAMETOOLONG);
691 num_str = strchr(buf, '.');
693 pr_err("Unable to locate . in name.bConfigurationValue\n");
694 return ERR_PTR(-EINVAL);
701 return ERR_PTR(-EINVAL);
703 ret = kstrtou8(num_str, 0, &num);
707 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
709 return ERR_PTR(-ENOMEM);
710 cfg->c.label = kstrdup(buf, GFP_KERNEL);
715 cfg->c.bConfigurationValue = num;
716 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
717 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
718 INIT_LIST_HEAD(&cfg->string_list);
719 INIT_LIST_HEAD(&cfg->func_list);
721 config_group_init_type_name(&cfg->group, name,
722 &gadget_config_type);
724 config_group_init_type_name(&cfg->strings_group, "strings",
725 &gadget_config_name_strings_type);
726 configfs_add_default_group(&cfg->strings_group, &cfg->group);
728 ret = usb_add_config_only(&gi->cdev, &cfg->c);
739 static void config_desc_drop(
740 struct config_group *group,
741 struct config_item *item)
743 config_item_put(item);
746 static struct configfs_group_operations config_desc_ops = {
747 .make_group = &config_desc_make,
748 .drop_item = &config_desc_drop,
751 static const struct config_item_type config_desc_type = {
752 .ct_group_ops = &config_desc_ops,
753 .ct_owner = THIS_MODULE,
756 GS_STRINGS_RW(gadget_strings, manufacturer);
757 GS_STRINGS_RW(gadget_strings, product);
758 GS_STRINGS_RW(gadget_strings, serialnumber);
760 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
761 &gadget_strings_attr_manufacturer,
762 &gadget_strings_attr_product,
763 &gadget_strings_attr_serialnumber,
767 static void gadget_strings_attr_release(struct config_item *item)
769 struct gadget_strings *gs = to_gadget_strings(item);
771 kfree(gs->manufacturer);
773 kfree(gs->serialnumber);
779 USB_CONFIG_STRING_RW_OPS(gadget_strings);
780 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
782 static inline struct gadget_info *os_desc_item_to_gadget_info(
783 struct config_item *item)
785 return container_of(to_config_group(item),
786 struct gadget_info, os_desc_group);
789 static ssize_t os_desc_use_show(struct config_item *item, char *page)
791 return sprintf(page, "%d\n",
792 os_desc_item_to_gadget_info(item)->use_os_desc);
795 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
798 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
802 mutex_lock(&gi->lock);
803 ret = strtobool(page, &use);
805 gi->use_os_desc = use;
808 mutex_unlock(&gi->lock);
813 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
815 return sprintf(page, "0x%02x\n",
816 os_desc_item_to_gadget_info(item)->b_vendor_code);
819 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
820 const char *page, size_t len)
822 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
826 mutex_lock(&gi->lock);
827 ret = kstrtou8(page, 0, &b_vendor_code);
829 gi->b_vendor_code = b_vendor_code;
832 mutex_unlock(&gi->lock);
837 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
839 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
842 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
843 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
849 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
852 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
855 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
856 if (page[l - 1] == '\n')
859 mutex_lock(&gi->lock);
860 res = utf8s_to_utf16s(page, l,
861 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
862 OS_STRING_QW_SIGN_LEN);
865 mutex_unlock(&gi->lock);
870 CONFIGFS_ATTR(os_desc_, use);
871 CONFIGFS_ATTR(os_desc_, b_vendor_code);
872 CONFIGFS_ATTR(os_desc_, qw_sign);
874 static struct configfs_attribute *os_desc_attrs[] = {
876 &os_desc_attr_b_vendor_code,
877 &os_desc_attr_qw_sign,
881 static int os_desc_link(struct config_item *os_desc_ci,
882 struct config_item *usb_cfg_ci)
884 struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci);
885 struct usb_composite_dev *cdev = &gi->cdev;
886 struct config_usb_cfg *c_target = to_config_usb_cfg(usb_cfg_ci);
887 struct usb_configuration *c = NULL, *iter;
890 mutex_lock(&gi->lock);
891 list_for_each_entry(iter, &cdev->configs, list) {
892 if (iter != &c_target->c)
902 if (cdev->os_desc_config) {
907 cdev->os_desc_config = &c_target->c;
911 mutex_unlock(&gi->lock);
915 static void os_desc_unlink(struct config_item *os_desc_ci,
916 struct config_item *usb_cfg_ci)
918 struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci);
919 struct usb_composite_dev *cdev = &gi->cdev;
921 mutex_lock(&gi->lock);
922 if (gi->composite.gadget_driver.udc_name)
923 unregister_gadget(gi);
924 cdev->os_desc_config = NULL;
925 WARN_ON(gi->composite.gadget_driver.udc_name);
926 mutex_unlock(&gi->lock);
929 static struct configfs_item_operations os_desc_ops = {
930 .allow_link = os_desc_link,
931 .drop_link = os_desc_unlink,
934 static struct config_item_type os_desc_type = {
935 .ct_item_ops = &os_desc_ops,
936 .ct_attrs = os_desc_attrs,
937 .ct_owner = THIS_MODULE,
940 static inline struct usb_os_desc_ext_prop
941 *to_usb_os_desc_ext_prop(struct config_item *item)
943 return container_of(item, struct usb_os_desc_ext_prop, item);
946 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
948 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
951 static ssize_t ext_prop_type_store(struct config_item *item,
952 const char *page, size_t len)
954 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
955 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
959 if (desc->opts_mutex)
960 mutex_lock(desc->opts_mutex);
961 ret = kstrtou8(page, 0, &type);
964 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
969 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
970 ext_prop->type == USB_EXT_PROP_LE32 ||
971 ext_prop->type == USB_EXT_PROP_BE32) &&
972 (type == USB_EXT_PROP_UNICODE ||
973 type == USB_EXT_PROP_UNICODE_ENV ||
974 type == USB_EXT_PROP_UNICODE_LINK))
975 ext_prop->data_len <<= 1;
976 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
977 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
978 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
979 (type == USB_EXT_PROP_BINARY ||
980 type == USB_EXT_PROP_LE32 ||
981 type == USB_EXT_PROP_BE32))
982 ext_prop->data_len >>= 1;
983 ext_prop->type = type;
987 if (desc->opts_mutex)
988 mutex_unlock(desc->opts_mutex);
992 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
994 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
995 int len = ext_prop->data_len;
997 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
998 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
999 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1001 memcpy(page, ext_prop->data, len);
1006 static ssize_t ext_prop_data_store(struct config_item *item,
1007 const char *page, size_t len)
1009 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1010 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1012 size_t ret_len = len;
1014 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1016 new_data = kmemdup(page, len, GFP_KERNEL);
1020 if (desc->opts_mutex)
1021 mutex_lock(desc->opts_mutex);
1022 kfree(ext_prop->data);
1023 ext_prop->data = new_data;
1024 desc->ext_prop_len -= ext_prop->data_len;
1025 ext_prop->data_len = len;
1026 desc->ext_prop_len += ext_prop->data_len;
1027 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1028 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1029 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1030 desc->ext_prop_len -= ext_prop->data_len;
1031 ext_prop->data_len <<= 1;
1032 ext_prop->data_len += 2;
1033 desc->ext_prop_len += ext_prop->data_len;
1035 if (desc->opts_mutex)
1036 mutex_unlock(desc->opts_mutex);
1040 CONFIGFS_ATTR(ext_prop_, type);
1041 CONFIGFS_ATTR(ext_prop_, data);
1043 static struct configfs_attribute *ext_prop_attrs[] = {
1044 &ext_prop_attr_type,
1045 &ext_prop_attr_data,
1049 static void usb_os_desc_ext_prop_release(struct config_item *item)
1051 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1053 kfree(ext_prop); /* frees a whole chunk */
1056 static struct configfs_item_operations ext_prop_ops = {
1057 .release = usb_os_desc_ext_prop_release,
1060 static struct config_item *ext_prop_make(
1061 struct config_group *group,
1064 struct usb_os_desc_ext_prop *ext_prop;
1065 struct config_item_type *ext_prop_type;
1066 struct usb_os_desc *desc;
1069 vla_group(data_chunk);
1070 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1071 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1073 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1075 return ERR_PTR(-ENOMEM);
1077 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1078 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1080 desc = container_of(group, struct usb_os_desc, group);
1081 ext_prop_type->ct_item_ops = &ext_prop_ops;
1082 ext_prop_type->ct_attrs = ext_prop_attrs;
1083 ext_prop_type->ct_owner = desc->owner;
1085 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1087 ext_prop->name = kstrdup(name, GFP_KERNEL);
1088 if (!ext_prop->name) {
1090 return ERR_PTR(-ENOMEM);
1092 desc->ext_prop_len += 14;
1093 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1094 if (desc->opts_mutex)
1095 mutex_lock(desc->opts_mutex);
1096 desc->ext_prop_len += ext_prop->name_len;
1097 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1098 ++desc->ext_prop_count;
1099 if (desc->opts_mutex)
1100 mutex_unlock(desc->opts_mutex);
1102 return &ext_prop->item;
1105 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1107 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1108 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1110 if (desc->opts_mutex)
1111 mutex_lock(desc->opts_mutex);
1112 list_del(&ext_prop->entry);
1113 --desc->ext_prop_count;
1114 kfree(ext_prop->name);
1115 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1116 if (desc->opts_mutex)
1117 mutex_unlock(desc->opts_mutex);
1118 config_item_put(item);
1121 static struct configfs_group_operations interf_grp_ops = {
1122 .make_item = &ext_prop_make,
1123 .drop_item = &ext_prop_drop,
1126 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1129 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1133 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1134 const char *page, size_t len)
1136 struct usb_os_desc *desc = to_usb_os_desc(item);
1139 l = min_t(int, 8, len);
1140 if (page[l - 1] == '\n')
1142 if (desc->opts_mutex)
1143 mutex_lock(desc->opts_mutex);
1144 memcpy(desc->ext_compat_id, page, l);
1146 if (desc->opts_mutex)
1147 mutex_unlock(desc->opts_mutex);
1152 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1155 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1159 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1160 const char *page, size_t len)
1162 struct usb_os_desc *desc = to_usb_os_desc(item);
1165 l = min_t(int, 8, len);
1166 if (page[l - 1] == '\n')
1168 if (desc->opts_mutex)
1169 mutex_lock(desc->opts_mutex);
1170 memcpy(desc->ext_compat_id + 8, page, l);
1172 if (desc->opts_mutex)
1173 mutex_unlock(desc->opts_mutex);
1178 CONFIGFS_ATTR(interf_grp_, compatible_id);
1179 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1181 static struct configfs_attribute *interf_grp_attrs[] = {
1182 &interf_grp_attr_compatible_id,
1183 &interf_grp_attr_sub_compatible_id,
1187 struct config_group *usb_os_desc_prepare_interf_dir(
1188 struct config_group *parent,
1190 struct usb_os_desc **desc,
1192 struct module *owner)
1194 struct config_group *os_desc_group;
1195 struct config_item_type *os_desc_type, *interface_type;
1197 vla_group(data_chunk);
1198 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1199 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1200 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1202 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1204 return ERR_PTR(-ENOMEM);
1206 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1207 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1208 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1210 os_desc_type->ct_owner = owner;
1211 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1212 configfs_add_default_group(os_desc_group, parent);
1214 interface_type->ct_group_ops = &interf_grp_ops;
1215 interface_type->ct_attrs = interf_grp_attrs;
1216 interface_type->ct_owner = owner;
1218 while (n_interf--) {
1219 struct usb_os_desc *d;
1223 config_group_init_type_name(&d->group, "", interface_type);
1224 config_item_set_name(&d->group.cg_item, "interface.%s",
1226 configfs_add_default_group(&d->group, os_desc_group);
1229 return os_desc_group;
1231 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1233 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1239 int composite_dev_prepare(struct usb_composite_driver *composite,
1240 struct usb_composite_dev *dev);
1242 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1243 struct usb_ep *ep0);
1245 static void purge_configs_funcs(struct gadget_info *gi)
1247 struct usb_configuration *c;
1249 list_for_each_entry(c, &gi->cdev.configs, list) {
1250 struct usb_function *f, *tmp;
1251 struct config_usb_cfg *cfg;
1253 cfg = container_of(c, struct config_usb_cfg, c);
1255 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1257 list_move(&f->list, &cfg->func_list);
1259 dev_dbg(&gi->cdev.gadget->dev,
1260 "unbind function '%s'/%p\n",
1265 c->next_interface_id = 0;
1266 memset(c->interface, 0, sizeof(c->interface));
1267 c->superspeed_plus = 0;
1274 static int configfs_composite_bind(struct usb_gadget *gadget,
1275 struct usb_gadget_driver *gdriver)
1277 struct usb_composite_driver *composite = to_cdriver(gdriver);
1278 struct gadget_info *gi = container_of(composite,
1279 struct gadget_info, composite);
1280 struct usb_composite_dev *cdev = &gi->cdev;
1281 struct usb_configuration *c;
1282 struct usb_string *s;
1286 /* the gi->lock is hold by the caller */
1288 cdev->gadget = gadget;
1289 set_gadget_data(gadget, cdev);
1290 ret = composite_dev_prepare(composite, cdev);
1293 /* and now the gadget bind */
1296 if (list_empty(&gi->cdev.configs)) {
1297 pr_err("Need at least one configuration in %s.\n",
1298 gi->composite.name);
1299 goto err_comp_cleanup;
1303 list_for_each_entry(c, &gi->cdev.configs, list) {
1304 struct config_usb_cfg *cfg;
1306 cfg = container_of(c, struct config_usb_cfg, c);
1307 if (list_empty(&cfg->func_list)) {
1308 pr_err("Config %s/%d of %s needs at least one function.\n",
1309 c->label, c->bConfigurationValue,
1310 gi->composite.name);
1311 goto err_comp_cleanup;
1315 /* init all strings */
1316 if (!list_empty(&gi->string_list)) {
1317 struct gadget_strings *gs;
1320 list_for_each_entry(gs, &gi->string_list, list) {
1322 gi->gstrings[i] = &gs->stringtab_dev;
1323 gs->stringtab_dev.strings = gs->strings;
1324 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1326 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1327 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1330 gi->gstrings[i] = NULL;
1331 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1332 USB_GADGET_FIRST_AVAIL_IDX);
1335 goto err_comp_cleanup;
1338 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1339 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1340 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1343 if (gi->use_os_desc) {
1344 cdev->use_os_string = true;
1345 cdev->b_vendor_code = gi->b_vendor_code;
1346 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1349 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1350 struct usb_descriptor_header *usb_desc;
1352 usb_desc = usb_otg_descriptor_alloc(gadget);
1355 goto err_comp_cleanup;
1357 usb_otg_descriptor_init(gadget, usb_desc);
1358 otg_desc[0] = usb_desc;
1362 /* Go through all configs, attach all functions */
1363 list_for_each_entry(c, &gi->cdev.configs, list) {
1364 struct config_usb_cfg *cfg;
1365 struct usb_function *f;
1366 struct usb_function *tmp;
1367 struct gadget_config_name *cn;
1369 if (gadget_is_otg(gadget))
1370 c->descriptors = otg_desc;
1372 cfg = container_of(c, struct config_usb_cfg, c);
1373 if (!list_empty(&cfg->string_list)) {
1375 list_for_each_entry(cn, &cfg->string_list, list) {
1376 cfg->gstrings[i] = &cn->stringtab_dev;
1377 cn->stringtab_dev.strings = &cn->strings;
1378 cn->strings.s = cn->configuration;
1381 cfg->gstrings[i] = NULL;
1382 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1385 goto err_comp_cleanup;
1387 c->iConfiguration = s[0].id;
1390 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1392 ret = usb_add_function(c, f);
1394 list_add(&f->list, &cfg->func_list);
1395 goto err_purge_funcs;
1398 ret = usb_gadget_check_config(cdev->gadget);
1400 goto err_purge_funcs;
1402 usb_ep_autoconfig_reset(cdev->gadget);
1404 if (cdev->use_os_string) {
1405 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1407 goto err_purge_funcs;
1410 usb_ep_autoconfig_reset(cdev->gadget);
1414 purge_configs_funcs(gi);
1416 composite_dev_cleanup(cdev);
1420 static void configfs_composite_unbind(struct usb_gadget *gadget)
1422 struct usb_composite_dev *cdev;
1423 struct gadget_info *gi;
1424 unsigned long flags;
1426 /* the gi->lock is hold by the caller */
1428 cdev = get_gadget_data(gadget);
1429 gi = container_of(cdev, struct gadget_info, cdev);
1430 spin_lock_irqsave(&gi->spinlock, flags);
1432 spin_unlock_irqrestore(&gi->spinlock, flags);
1436 purge_configs_funcs(gi);
1437 composite_dev_cleanup(cdev);
1438 usb_ep_autoconfig_reset(cdev->gadget);
1439 spin_lock_irqsave(&gi->spinlock, flags);
1440 cdev->gadget = NULL;
1441 cdev->deactivations = 0;
1442 gadget->deactivated = false;
1443 set_gadget_data(gadget, NULL);
1444 spin_unlock_irqrestore(&gi->spinlock, flags);
1447 static int configfs_composite_setup(struct usb_gadget *gadget,
1448 const struct usb_ctrlrequest *ctrl)
1450 struct usb_composite_dev *cdev;
1451 struct gadget_info *gi;
1452 unsigned long flags;
1455 cdev = get_gadget_data(gadget);
1459 gi = container_of(cdev, struct gadget_info, cdev);
1460 spin_lock_irqsave(&gi->spinlock, flags);
1461 cdev = get_gadget_data(gadget);
1462 if (!cdev || gi->unbind) {
1463 spin_unlock_irqrestore(&gi->spinlock, flags);
1467 ret = composite_setup(gadget, ctrl);
1468 spin_unlock_irqrestore(&gi->spinlock, flags);
1472 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1474 struct usb_composite_dev *cdev;
1475 struct gadget_info *gi;
1476 unsigned long flags;
1478 cdev = get_gadget_data(gadget);
1482 gi = container_of(cdev, struct gadget_info, cdev);
1483 spin_lock_irqsave(&gi->spinlock, flags);
1484 cdev = get_gadget_data(gadget);
1485 if (!cdev || gi->unbind) {
1486 spin_unlock_irqrestore(&gi->spinlock, flags);
1490 composite_disconnect(gadget);
1491 spin_unlock_irqrestore(&gi->spinlock, flags);
1494 static void configfs_composite_reset(struct usb_gadget *gadget)
1496 struct usb_composite_dev *cdev;
1497 struct gadget_info *gi;
1498 unsigned long flags;
1500 cdev = get_gadget_data(gadget);
1504 gi = container_of(cdev, struct gadget_info, cdev);
1505 spin_lock_irqsave(&gi->spinlock, flags);
1506 cdev = get_gadget_data(gadget);
1507 if (!cdev || gi->unbind) {
1508 spin_unlock_irqrestore(&gi->spinlock, flags);
1512 composite_reset(gadget);
1513 spin_unlock_irqrestore(&gi->spinlock, flags);
1516 static void configfs_composite_suspend(struct usb_gadget *gadget)
1518 struct usb_composite_dev *cdev;
1519 struct gadget_info *gi;
1520 unsigned long flags;
1522 cdev = get_gadget_data(gadget);
1526 gi = container_of(cdev, struct gadget_info, cdev);
1527 spin_lock_irqsave(&gi->spinlock, flags);
1528 cdev = get_gadget_data(gadget);
1529 if (!cdev || gi->unbind) {
1530 spin_unlock_irqrestore(&gi->spinlock, flags);
1534 composite_suspend(gadget);
1535 spin_unlock_irqrestore(&gi->spinlock, flags);
1538 static void configfs_composite_resume(struct usb_gadget *gadget)
1540 struct usb_composite_dev *cdev;
1541 struct gadget_info *gi;
1542 unsigned long flags;
1544 cdev = get_gadget_data(gadget);
1548 gi = container_of(cdev, struct gadget_info, cdev);
1549 spin_lock_irqsave(&gi->spinlock, flags);
1550 cdev = get_gadget_data(gadget);
1551 if (!cdev || gi->unbind) {
1552 spin_unlock_irqrestore(&gi->spinlock, flags);
1556 composite_resume(gadget);
1557 spin_unlock_irqrestore(&gi->spinlock, flags);
1560 static const struct usb_gadget_driver configfs_driver_template = {
1561 .bind = configfs_composite_bind,
1562 .unbind = configfs_composite_unbind,
1564 .setup = configfs_composite_setup,
1565 .reset = configfs_composite_reset,
1566 .disconnect = configfs_composite_disconnect,
1568 .suspend = configfs_composite_suspend,
1569 .resume = configfs_composite_resume,
1571 .max_speed = USB_SPEED_SUPER_PLUS,
1573 .owner = THIS_MODULE,
1574 .name = "configfs-gadget",
1576 .match_existing_only = 1,
1579 static struct config_group *gadgets_make(
1580 struct config_group *group,
1583 struct gadget_info *gi;
1585 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1587 return ERR_PTR(-ENOMEM);
1589 config_group_init_type_name(&gi->group, name, &gadget_root_type);
1591 config_group_init_type_name(&gi->functions_group, "functions",
1593 configfs_add_default_group(&gi->functions_group, &gi->group);
1595 config_group_init_type_name(&gi->configs_group, "configs",
1597 configfs_add_default_group(&gi->configs_group, &gi->group);
1599 config_group_init_type_name(&gi->strings_group, "strings",
1600 &gadget_strings_strings_type);
1601 configfs_add_default_group(&gi->strings_group, &gi->group);
1603 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1605 configfs_add_default_group(&gi->os_desc_group, &gi->group);
1607 gi->composite.bind = configfs_do_nothing;
1608 gi->composite.unbind = configfs_do_nothing;
1609 gi->composite.suspend = NULL;
1610 gi->composite.resume = NULL;
1611 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1613 spin_lock_init(&gi->spinlock);
1614 mutex_init(&gi->lock);
1615 INIT_LIST_HEAD(&gi->string_list);
1616 INIT_LIST_HEAD(&gi->available_func);
1618 composite_init_dev(&gi->cdev);
1619 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1620 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1621 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1623 gi->composite.gadget_driver = configfs_driver_template;
1625 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1626 gi->composite.name = gi->composite.gadget_driver.function;
1628 if (!gi->composite.gadget_driver.function)
1634 return ERR_PTR(-ENOMEM);
1637 static void gadgets_drop(struct config_group *group, struct config_item *item)
1639 config_item_put(item);
1642 static struct configfs_group_operations gadgets_ops = {
1643 .make_group = &gadgets_make,
1644 .drop_item = &gadgets_drop,
1647 static const struct config_item_type gadgets_type = {
1648 .ct_group_ops = &gadgets_ops,
1649 .ct_owner = THIS_MODULE,
1652 static struct configfs_subsystem gadget_subsys = {
1655 .ci_namebuf = "usb_gadget",
1656 .ci_type = &gadgets_type,
1659 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1662 void unregister_gadget_item(struct config_item *item)
1664 struct gadget_info *gi = to_gadget_info(item);
1666 mutex_lock(&gi->lock);
1667 unregister_gadget(gi);
1668 mutex_unlock(&gi->lock);
1670 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1672 static int __init gadget_cfs_init(void)
1676 config_group_init(&gadget_subsys.su_group);
1678 ret = configfs_register_subsystem(&gadget_subsys);
1681 module_init(gadget_cfs_init);
1683 static void __exit gadget_cfs_exit(void)
1685 configfs_unregister_subsystem(&gadget_subsys);
1687 module_exit(gadget_cfs_exit);