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)
16 unsigned primary_lang;
21 ret = kstrtou16(name, 0, &num);
25 primary_lang = num & 0x3ff;
28 /* simple sanity check for valid langid */
29 switch (primary_lang) {
38 stringtab_dev->language = num;
42 #define MAX_NAME_LEN 40
43 #define MAX_USB_STRING_LANGS 2
45 static const struct usb_descriptor_header *otg_desc[2];
48 struct config_group group;
49 struct config_group functions_group;
50 struct config_group configs_group;
51 struct config_group strings_group;
52 struct config_group os_desc_group;
55 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
56 struct list_head string_list;
57 struct list_head available_func;
59 struct usb_composite_driver composite;
60 struct usb_composite_dev cdev;
63 char qw_sign[OS_STRING_QW_SIGN_LEN];
68 static inline struct gadget_info *to_gadget_info(struct config_item *item)
70 return container_of(to_config_group(item), struct gadget_info, group);
73 struct config_usb_cfg {
74 struct config_group group;
75 struct config_group strings_group;
76 struct list_head string_list;
77 struct usb_configuration c;
78 struct list_head func_list;
79 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
82 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
84 return container_of(to_config_group(item), struct config_usb_cfg,
88 struct gadget_strings {
89 struct usb_gadget_strings stringtab_dev;
90 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
95 struct config_group group;
96 struct list_head list;
100 struct config_group group;
103 struct gadget_config_name {
104 struct usb_gadget_strings stringtab_dev;
105 struct usb_string strings;
108 struct config_group group;
109 struct list_head list;
112 #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1)
114 static int usb_string_copy(const char *s, char **s_copy)
118 char *copy = *s_copy;
120 if (ret > USB_MAX_STRING_LEN)
126 str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
131 if (str[ret - 1] == '\n')
137 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
138 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
141 return sprintf(page, "0x%02x\n", \
142 to_gadget_info(item)->cdev.desc.__name); \
145 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
146 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
149 return sprintf(page, "0x%04x\n", \
150 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
154 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
155 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
156 const char *page, size_t len) \
160 ret = kstrtou8(page, 0, &val); \
163 to_gadget_info(item)->cdev.desc._name = val; \
167 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
168 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
169 const char *page, size_t len) \
173 ret = kstrtou16(page, 0, &val); \
176 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
180 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
181 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
182 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
184 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
185 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
186 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
187 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
188 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
189 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
190 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
191 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
193 static ssize_t is_valid_bcd(u16 bcd_val)
195 if ((bcd_val & 0xf) > 9)
197 if (((bcd_val >> 4) & 0xf) > 9)
199 if (((bcd_val >> 8) & 0xf) > 9)
201 if (((bcd_val >> 12) & 0xf) > 9)
206 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
207 const char *page, size_t len)
212 ret = kstrtou16(page, 0, &bcdDevice);
215 ret = is_valid_bcd(bcdDevice);
219 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
223 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
224 const char *page, size_t len)
229 ret = kstrtou16(page, 0, &bcdUSB);
232 ret = is_valid_bcd(bcdUSB);
236 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
240 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
242 struct gadget_info *gi = to_gadget_info(item);
246 mutex_lock(&gi->lock);
247 udc_name = gi->composite.gadget_driver.udc_name;
248 ret = sprintf(page, "%s\n", udc_name ?: "");
249 mutex_unlock(&gi->lock);
254 static int unregister_gadget(struct gadget_info *gi)
258 if (!gi->composite.gadget_driver.udc_name)
261 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
264 kfree(gi->composite.gadget_driver.udc_name);
265 gi->composite.gadget_driver.udc_name = NULL;
269 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
270 const char *page, size_t len)
272 struct gadget_info *gi = to_gadget_info(item);
276 if (strlen(page) < len)
279 name = kstrdup(page, GFP_KERNEL);
282 if (name[len - 1] == '\n')
283 name[len - 1] = '\0';
285 mutex_lock(&gi->lock);
288 ret = unregister_gadget(gi);
293 if (gi->composite.gadget_driver.udc_name) {
297 gi->composite.gadget_driver.udc_name = name;
298 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
300 gi->composite.gadget_driver.udc_name = NULL;
304 mutex_unlock(&gi->lock);
308 mutex_unlock(&gi->lock);
312 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
313 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
314 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
315 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
316 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
317 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
318 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
319 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
320 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
322 static struct configfs_attribute *gadget_root_attrs[] = {
323 &gadget_dev_desc_attr_bDeviceClass,
324 &gadget_dev_desc_attr_bDeviceSubClass,
325 &gadget_dev_desc_attr_bDeviceProtocol,
326 &gadget_dev_desc_attr_bMaxPacketSize0,
327 &gadget_dev_desc_attr_idVendor,
328 &gadget_dev_desc_attr_idProduct,
329 &gadget_dev_desc_attr_bcdDevice,
330 &gadget_dev_desc_attr_bcdUSB,
331 &gadget_dev_desc_attr_UDC,
335 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
337 return container_of(to_config_group(item), struct gadget_strings,
341 static inline struct gadget_config_name *to_gadget_config_name(
342 struct config_item *item)
344 return container_of(to_config_group(item), struct gadget_config_name,
348 static inline struct usb_function_instance *to_usb_function_instance(
349 struct config_item *item)
351 return container_of(to_config_group(item),
352 struct usb_function_instance, group);
355 static void gadget_info_attr_release(struct config_item *item)
357 struct gadget_info *gi = to_gadget_info(item);
359 WARN_ON(!list_empty(&gi->cdev.configs));
360 WARN_ON(!list_empty(&gi->string_list));
361 WARN_ON(!list_empty(&gi->available_func));
362 kfree(gi->composite.gadget_driver.function);
366 static struct configfs_item_operations gadget_root_item_ops = {
367 .release = gadget_info_attr_release,
370 static void gadget_config_attr_release(struct config_item *item)
372 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
374 WARN_ON(!list_empty(&cfg->c.functions));
375 list_del(&cfg->c.list);
380 static int config_usb_cfg_link(
381 struct config_item *usb_cfg_ci,
382 struct config_item *usb_func_ci)
384 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
385 struct usb_composite_dev *cdev = cfg->c.cdev;
386 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
388 struct config_group *group = to_config_group(usb_func_ci);
389 struct usb_function_instance *fi = container_of(group,
390 struct usb_function_instance, group);
391 struct usb_function_instance *a_fi;
392 struct usb_function *f;
395 mutex_lock(&gi->lock);
397 * Make sure this function is from within our _this_ gadget and not
398 * from another gadget or a random directory.
399 * Also a function instance can only be linked once.
401 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
410 list_for_each_entry(f, &cfg->func_list, list) {
417 f = usb_get_function(fi);
423 /* stash the function until we bind it to the gadget */
424 list_add_tail(&f->list, &cfg->func_list);
427 mutex_unlock(&gi->lock);
431 static void config_usb_cfg_unlink(
432 struct config_item *usb_cfg_ci,
433 struct config_item *usb_func_ci)
435 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
436 struct usb_composite_dev *cdev = cfg->c.cdev;
437 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
439 struct config_group *group = to_config_group(usb_func_ci);
440 struct usb_function_instance *fi = container_of(group,
441 struct usb_function_instance, group);
442 struct usb_function *f;
445 * ideally I would like to forbid to unlink functions while a gadget is
446 * bound to an UDC. Since this isn't possible at the moment, we simply
447 * force an unbind, the function is available here and then we can
448 * remove the function.
450 mutex_lock(&gi->lock);
451 if (gi->composite.gadget_driver.udc_name)
452 unregister_gadget(gi);
453 WARN_ON(gi->composite.gadget_driver.udc_name);
455 list_for_each_entry(f, &cfg->func_list, list) {
459 mutex_unlock(&gi->lock);
463 mutex_unlock(&gi->lock);
464 WARN(1, "Unable to locate function to unbind\n");
467 static struct configfs_item_operations gadget_config_item_ops = {
468 .release = gadget_config_attr_release,
469 .allow_link = config_usb_cfg_link,
470 .drop_link = config_usb_cfg_unlink,
474 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
477 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
480 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
481 const char *page, size_t len)
485 ret = kstrtou16(page, 0, &val);
488 if (DIV_ROUND_UP(val, 8) > 0xff)
490 to_config_usb_cfg(item)->c.MaxPower = val;
494 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
497 return sprintf(page, "0x%02x\n",
498 to_config_usb_cfg(item)->c.bmAttributes);
501 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
502 const char *page, size_t len)
506 ret = kstrtou8(page, 0, &val);
509 if (!(val & USB_CONFIG_ATT_ONE))
511 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
512 USB_CONFIG_ATT_WAKEUP))
514 to_config_usb_cfg(item)->c.bmAttributes = val;
518 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
519 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
521 static struct configfs_attribute *gadget_config_attrs[] = {
522 &gadget_config_desc_attr_MaxPower,
523 &gadget_config_desc_attr_bmAttributes,
527 static const struct config_item_type gadget_config_type = {
528 .ct_item_ops = &gadget_config_item_ops,
529 .ct_attrs = gadget_config_attrs,
530 .ct_owner = THIS_MODULE,
533 static const struct config_item_type gadget_root_type = {
534 .ct_item_ops = &gadget_root_item_ops,
535 .ct_attrs = gadget_root_attrs,
536 .ct_owner = THIS_MODULE,
539 static void composite_init_dev(struct usb_composite_dev *cdev)
541 spin_lock_init(&cdev->lock);
542 INIT_LIST_HEAD(&cdev->configs);
543 INIT_LIST_HEAD(&cdev->gstrings);
546 static struct config_group *function_make(
547 struct config_group *group,
550 struct gadget_info *gi;
551 struct usb_function_instance *fi;
552 char buf[MAX_NAME_LEN];
557 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
558 if (ret >= MAX_NAME_LEN)
559 return ERR_PTR(-ENAMETOOLONG);
562 instance_name = strchr(func_name, '.');
563 if (!instance_name) {
564 pr_err("Unable to locate . in FUNC.INSTANCE\n");
565 return ERR_PTR(-EINVAL);
567 *instance_name = '\0';
570 fi = usb_get_function_instance(func_name);
574 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
576 usb_put_function_instance(fi);
579 if (fi->set_inst_name) {
580 ret = fi->set_inst_name(fi, instance_name);
582 usb_put_function_instance(fi);
587 gi = container_of(group, struct gadget_info, functions_group);
589 mutex_lock(&gi->lock);
590 list_add_tail(&fi->cfs_list, &gi->available_func);
591 mutex_unlock(&gi->lock);
595 static void function_drop(
596 struct config_group *group,
597 struct config_item *item)
599 struct usb_function_instance *fi = to_usb_function_instance(item);
600 struct gadget_info *gi;
602 gi = container_of(group, struct gadget_info, functions_group);
604 mutex_lock(&gi->lock);
605 list_del(&fi->cfs_list);
606 mutex_unlock(&gi->lock);
607 config_item_put(item);
610 static struct configfs_group_operations functions_ops = {
611 .make_group = &function_make,
612 .drop_item = &function_drop,
615 static const struct config_item_type functions_type = {
616 .ct_group_ops = &functions_ops,
617 .ct_owner = THIS_MODULE,
620 GS_STRINGS_RW(gadget_config_name, configuration);
622 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
623 &gadget_config_name_attr_configuration,
627 static void gadget_config_name_attr_release(struct config_item *item)
629 struct gadget_config_name *cn = to_gadget_config_name(item);
631 kfree(cn->configuration);
637 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
638 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
640 static struct config_group *config_desc_make(
641 struct config_group *group,
644 struct gadget_info *gi;
645 struct config_usb_cfg *cfg;
646 char buf[MAX_NAME_LEN];
651 gi = container_of(group, struct gadget_info, configs_group);
652 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
653 if (ret >= MAX_NAME_LEN)
654 return ERR_PTR(-ENAMETOOLONG);
656 num_str = strchr(buf, '.');
658 pr_err("Unable to locate . in name.bConfigurationValue\n");
659 return ERR_PTR(-EINVAL);
666 return ERR_PTR(-EINVAL);
668 ret = kstrtou8(num_str, 0, &num);
672 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
674 return ERR_PTR(-ENOMEM);
675 cfg->c.label = kstrdup(buf, GFP_KERNEL);
680 cfg->c.bConfigurationValue = num;
681 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
682 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
683 INIT_LIST_HEAD(&cfg->string_list);
684 INIT_LIST_HEAD(&cfg->func_list);
686 config_group_init_type_name(&cfg->group, name,
687 &gadget_config_type);
689 config_group_init_type_name(&cfg->strings_group, "strings",
690 &gadget_config_name_strings_type);
691 configfs_add_default_group(&cfg->strings_group, &cfg->group);
693 ret = usb_add_config_only(&gi->cdev, &cfg->c);
704 static void config_desc_drop(
705 struct config_group *group,
706 struct config_item *item)
708 config_item_put(item);
711 static struct configfs_group_operations config_desc_ops = {
712 .make_group = &config_desc_make,
713 .drop_item = &config_desc_drop,
716 static const struct config_item_type config_desc_type = {
717 .ct_group_ops = &config_desc_ops,
718 .ct_owner = THIS_MODULE,
721 GS_STRINGS_RW(gadget_strings, manufacturer);
722 GS_STRINGS_RW(gadget_strings, product);
723 GS_STRINGS_RW(gadget_strings, serialnumber);
725 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
726 &gadget_strings_attr_manufacturer,
727 &gadget_strings_attr_product,
728 &gadget_strings_attr_serialnumber,
732 static void gadget_strings_attr_release(struct config_item *item)
734 struct gadget_strings *gs = to_gadget_strings(item);
736 kfree(gs->manufacturer);
738 kfree(gs->serialnumber);
744 USB_CONFIG_STRING_RW_OPS(gadget_strings);
745 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
747 static inline struct os_desc *to_os_desc(struct config_item *item)
749 return container_of(to_config_group(item), struct os_desc, group);
752 static inline struct gadget_info *os_desc_item_to_gadget_info(
753 struct config_item *item)
755 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
758 static ssize_t os_desc_use_show(struct config_item *item, char *page)
760 return sprintf(page, "%d\n",
761 os_desc_item_to_gadget_info(item)->use_os_desc);
764 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
767 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
771 mutex_lock(&gi->lock);
772 ret = strtobool(page, &use);
774 gi->use_os_desc = use;
777 mutex_unlock(&gi->lock);
782 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
784 return sprintf(page, "0x%02x\n",
785 os_desc_item_to_gadget_info(item)->b_vendor_code);
788 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
789 const char *page, size_t len)
791 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
795 mutex_lock(&gi->lock);
796 ret = kstrtou8(page, 0, &b_vendor_code);
798 gi->b_vendor_code = b_vendor_code;
801 mutex_unlock(&gi->lock);
806 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
808 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
811 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
812 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
818 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
821 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
824 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
825 if (page[l - 1] == '\n')
828 mutex_lock(&gi->lock);
829 res = utf8s_to_utf16s(page, l,
830 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
831 OS_STRING_QW_SIGN_LEN);
834 mutex_unlock(&gi->lock);
839 CONFIGFS_ATTR(os_desc_, use);
840 CONFIGFS_ATTR(os_desc_, b_vendor_code);
841 CONFIGFS_ATTR(os_desc_, qw_sign);
843 static struct configfs_attribute *os_desc_attrs[] = {
845 &os_desc_attr_b_vendor_code,
846 &os_desc_attr_qw_sign,
850 static void os_desc_attr_release(struct config_item *item)
852 struct os_desc *os_desc = to_os_desc(item);
856 static int os_desc_link(struct config_item *os_desc_ci,
857 struct config_item *usb_cfg_ci)
859 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
860 struct gadget_info, os_desc_group);
861 struct usb_composite_dev *cdev = &gi->cdev;
862 struct config_usb_cfg *c_target =
863 container_of(to_config_group(usb_cfg_ci),
864 struct config_usb_cfg, group);
865 struct usb_configuration *c;
868 mutex_lock(&gi->lock);
869 list_for_each_entry(c, &cdev->configs, list) {
870 if (c == &c_target->c)
873 if (c != &c_target->c) {
878 if (cdev->os_desc_config) {
883 cdev->os_desc_config = &c_target->c;
887 mutex_unlock(&gi->lock);
891 static void os_desc_unlink(struct config_item *os_desc_ci,
892 struct config_item *usb_cfg_ci)
894 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
895 struct gadget_info, os_desc_group);
896 struct usb_composite_dev *cdev = &gi->cdev;
898 mutex_lock(&gi->lock);
899 if (gi->composite.gadget_driver.udc_name)
900 unregister_gadget(gi);
901 cdev->os_desc_config = NULL;
902 WARN_ON(gi->composite.gadget_driver.udc_name);
903 mutex_unlock(&gi->lock);
906 static struct configfs_item_operations os_desc_ops = {
907 .release = os_desc_attr_release,
908 .allow_link = os_desc_link,
909 .drop_link = os_desc_unlink,
912 static struct config_item_type os_desc_type = {
913 .ct_item_ops = &os_desc_ops,
914 .ct_attrs = os_desc_attrs,
915 .ct_owner = THIS_MODULE,
918 static inline struct usb_os_desc_ext_prop
919 *to_usb_os_desc_ext_prop(struct config_item *item)
921 return container_of(item, struct usb_os_desc_ext_prop, item);
924 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
926 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
929 static ssize_t ext_prop_type_store(struct config_item *item,
930 const char *page, size_t len)
932 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
933 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
937 if (desc->opts_mutex)
938 mutex_lock(desc->opts_mutex);
939 ret = kstrtou8(page, 0, &type);
942 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
947 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
948 ext_prop->type == USB_EXT_PROP_LE32 ||
949 ext_prop->type == USB_EXT_PROP_BE32) &&
950 (type == USB_EXT_PROP_UNICODE ||
951 type == USB_EXT_PROP_UNICODE_ENV ||
952 type == USB_EXT_PROP_UNICODE_LINK))
953 ext_prop->data_len <<= 1;
954 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
955 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
956 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
957 (type == USB_EXT_PROP_BINARY ||
958 type == USB_EXT_PROP_LE32 ||
959 type == USB_EXT_PROP_BE32))
960 ext_prop->data_len >>= 1;
961 ext_prop->type = type;
965 if (desc->opts_mutex)
966 mutex_unlock(desc->opts_mutex);
970 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
972 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
973 int len = ext_prop->data_len;
975 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
976 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
977 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
979 memcpy(page, ext_prop->data, len);
984 static ssize_t ext_prop_data_store(struct config_item *item,
985 const char *page, size_t len)
987 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
988 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
990 size_t ret_len = len;
992 if (page[len - 1] == '\n' || page[len - 1] == '\0')
994 new_data = kmemdup(page, len, GFP_KERNEL);
998 if (desc->opts_mutex)
999 mutex_lock(desc->opts_mutex);
1000 kfree(ext_prop->data);
1001 ext_prop->data = new_data;
1002 desc->ext_prop_len -= ext_prop->data_len;
1003 ext_prop->data_len = len;
1004 desc->ext_prop_len += ext_prop->data_len;
1005 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1006 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1007 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1008 desc->ext_prop_len -= ext_prop->data_len;
1009 ext_prop->data_len <<= 1;
1010 ext_prop->data_len += 2;
1011 desc->ext_prop_len += ext_prop->data_len;
1013 if (desc->opts_mutex)
1014 mutex_unlock(desc->opts_mutex);
1018 CONFIGFS_ATTR(ext_prop_, type);
1019 CONFIGFS_ATTR(ext_prop_, data);
1021 static struct configfs_attribute *ext_prop_attrs[] = {
1022 &ext_prop_attr_type,
1023 &ext_prop_attr_data,
1027 static void usb_os_desc_ext_prop_release(struct config_item *item)
1029 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1031 kfree(ext_prop); /* frees a whole chunk */
1034 static struct configfs_item_operations ext_prop_ops = {
1035 .release = usb_os_desc_ext_prop_release,
1038 static struct config_item *ext_prop_make(
1039 struct config_group *group,
1042 struct usb_os_desc_ext_prop *ext_prop;
1043 struct config_item_type *ext_prop_type;
1044 struct usb_os_desc *desc;
1047 vla_group(data_chunk);
1048 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1049 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1051 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1053 return ERR_PTR(-ENOMEM);
1055 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1056 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1058 desc = container_of(group, struct usb_os_desc, group);
1059 ext_prop_type->ct_item_ops = &ext_prop_ops;
1060 ext_prop_type->ct_attrs = ext_prop_attrs;
1061 ext_prop_type->ct_owner = desc->owner;
1063 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1065 ext_prop->name = kstrdup(name, GFP_KERNEL);
1066 if (!ext_prop->name) {
1068 return ERR_PTR(-ENOMEM);
1070 desc->ext_prop_len += 14;
1071 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1072 if (desc->opts_mutex)
1073 mutex_lock(desc->opts_mutex);
1074 desc->ext_prop_len += ext_prop->name_len;
1075 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1076 ++desc->ext_prop_count;
1077 if (desc->opts_mutex)
1078 mutex_unlock(desc->opts_mutex);
1080 return &ext_prop->item;
1083 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1085 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1086 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1088 if (desc->opts_mutex)
1089 mutex_lock(desc->opts_mutex);
1090 list_del(&ext_prop->entry);
1091 --desc->ext_prop_count;
1092 kfree(ext_prop->name);
1093 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1094 if (desc->opts_mutex)
1095 mutex_unlock(desc->opts_mutex);
1096 config_item_put(item);
1099 static struct configfs_group_operations interf_grp_ops = {
1100 .make_item = &ext_prop_make,
1101 .drop_item = &ext_prop_drop,
1104 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1107 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1111 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1112 const char *page, size_t len)
1114 struct usb_os_desc *desc = to_usb_os_desc(item);
1117 l = min_t(int, 8, len);
1118 if (page[l - 1] == '\n')
1120 if (desc->opts_mutex)
1121 mutex_lock(desc->opts_mutex);
1122 memcpy(desc->ext_compat_id, page, l);
1124 if (desc->opts_mutex)
1125 mutex_unlock(desc->opts_mutex);
1130 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1133 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1137 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1138 const char *page, size_t len)
1140 struct usb_os_desc *desc = to_usb_os_desc(item);
1143 l = min_t(int, 8, len);
1144 if (page[l - 1] == '\n')
1146 if (desc->opts_mutex)
1147 mutex_lock(desc->opts_mutex);
1148 memcpy(desc->ext_compat_id + 8, page, l);
1150 if (desc->opts_mutex)
1151 mutex_unlock(desc->opts_mutex);
1156 CONFIGFS_ATTR(interf_grp_, compatible_id);
1157 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1159 static struct configfs_attribute *interf_grp_attrs[] = {
1160 &interf_grp_attr_compatible_id,
1161 &interf_grp_attr_sub_compatible_id,
1165 struct config_group *usb_os_desc_prepare_interf_dir(
1166 struct config_group *parent,
1168 struct usb_os_desc **desc,
1170 struct module *owner)
1172 struct config_group *os_desc_group;
1173 struct config_item_type *os_desc_type, *interface_type;
1175 vla_group(data_chunk);
1176 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1177 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1178 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1180 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1182 return ERR_PTR(-ENOMEM);
1184 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1185 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1186 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1188 os_desc_type->ct_owner = owner;
1189 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1190 configfs_add_default_group(os_desc_group, parent);
1192 interface_type->ct_group_ops = &interf_grp_ops;
1193 interface_type->ct_attrs = interf_grp_attrs;
1194 interface_type->ct_owner = owner;
1196 while (n_interf--) {
1197 struct usb_os_desc *d;
1201 config_group_init_type_name(&d->group, "", interface_type);
1202 config_item_set_name(&d->group.cg_item, "interface.%s",
1204 configfs_add_default_group(&d->group, os_desc_group);
1207 return os_desc_group;
1209 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1211 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1217 int composite_dev_prepare(struct usb_composite_driver *composite,
1218 struct usb_composite_dev *dev);
1220 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1221 struct usb_ep *ep0);
1223 static void purge_configs_funcs(struct gadget_info *gi)
1225 struct usb_configuration *c;
1227 list_for_each_entry(c, &gi->cdev.configs, list) {
1228 struct usb_function *f, *tmp;
1229 struct config_usb_cfg *cfg;
1231 cfg = container_of(c, struct config_usb_cfg, c);
1233 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1235 list_move(&f->list, &cfg->func_list);
1237 dev_dbg(&gi->cdev.gadget->dev,
1238 "unbind function '%s'/%p\n",
1243 c->next_interface_id = 0;
1244 memset(c->interface, 0, sizeof(c->interface));
1245 c->superspeed_plus = 0;
1252 static int configfs_composite_bind(struct usb_gadget *gadget,
1253 struct usb_gadget_driver *gdriver)
1255 struct usb_composite_driver *composite = to_cdriver(gdriver);
1256 struct gadget_info *gi = container_of(composite,
1257 struct gadget_info, composite);
1258 struct usb_composite_dev *cdev = &gi->cdev;
1259 struct usb_configuration *c;
1260 struct usb_string *s;
1264 /* the gi->lock is hold by the caller */
1266 cdev->gadget = gadget;
1267 set_gadget_data(gadget, cdev);
1268 ret = composite_dev_prepare(composite, cdev);
1271 /* and now the gadget bind */
1274 if (list_empty(&gi->cdev.configs)) {
1275 pr_err("Need at least one configuration in %s.\n",
1276 gi->composite.name);
1277 goto err_comp_cleanup;
1281 list_for_each_entry(c, &gi->cdev.configs, list) {
1282 struct config_usb_cfg *cfg;
1284 cfg = container_of(c, struct config_usb_cfg, c);
1285 if (list_empty(&cfg->func_list)) {
1286 pr_err("Config %s/%d of %s needs at least one function.\n",
1287 c->label, c->bConfigurationValue,
1288 gi->composite.name);
1289 goto err_comp_cleanup;
1293 /* init all strings */
1294 if (!list_empty(&gi->string_list)) {
1295 struct gadget_strings *gs;
1298 list_for_each_entry(gs, &gi->string_list, list) {
1300 gi->gstrings[i] = &gs->stringtab_dev;
1301 gs->stringtab_dev.strings = gs->strings;
1302 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1304 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1305 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1308 gi->gstrings[i] = NULL;
1309 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1310 USB_GADGET_FIRST_AVAIL_IDX);
1313 goto err_comp_cleanup;
1316 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1317 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1318 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1321 if (gi->use_os_desc) {
1322 cdev->use_os_string = true;
1323 cdev->b_vendor_code = gi->b_vendor_code;
1324 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1327 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1328 struct usb_descriptor_header *usb_desc;
1330 usb_desc = usb_otg_descriptor_alloc(gadget);
1333 goto err_comp_cleanup;
1335 usb_otg_descriptor_init(gadget, usb_desc);
1336 otg_desc[0] = usb_desc;
1340 /* Go through all configs, attach all functions */
1341 list_for_each_entry(c, &gi->cdev.configs, list) {
1342 struct config_usb_cfg *cfg;
1343 struct usb_function *f;
1344 struct usb_function *tmp;
1345 struct gadget_config_name *cn;
1347 if (gadget_is_otg(gadget))
1348 c->descriptors = otg_desc;
1350 cfg = container_of(c, struct config_usb_cfg, c);
1351 if (!list_empty(&cfg->string_list)) {
1353 list_for_each_entry(cn, &cfg->string_list, list) {
1354 cfg->gstrings[i] = &cn->stringtab_dev;
1355 cn->stringtab_dev.strings = &cn->strings;
1356 cn->strings.s = cn->configuration;
1359 cfg->gstrings[i] = NULL;
1360 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1363 goto err_comp_cleanup;
1365 c->iConfiguration = s[0].id;
1368 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1370 ret = usb_add_function(c, f);
1372 list_add(&f->list, &cfg->func_list);
1373 goto err_purge_funcs;
1376 usb_ep_autoconfig_reset(cdev->gadget);
1378 if (cdev->use_os_string) {
1379 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1381 goto err_purge_funcs;
1384 usb_ep_autoconfig_reset(cdev->gadget);
1388 purge_configs_funcs(gi);
1390 composite_dev_cleanup(cdev);
1394 static void configfs_composite_unbind(struct usb_gadget *gadget)
1396 struct usb_composite_dev *cdev;
1397 struct gadget_info *gi;
1398 unsigned long flags;
1400 /* the gi->lock is hold by the caller */
1402 cdev = get_gadget_data(gadget);
1403 gi = container_of(cdev, struct gadget_info, cdev);
1404 spin_lock_irqsave(&gi->spinlock, flags);
1406 spin_unlock_irqrestore(&gi->spinlock, flags);
1410 purge_configs_funcs(gi);
1411 composite_dev_cleanup(cdev);
1412 usb_ep_autoconfig_reset(cdev->gadget);
1413 spin_lock_irqsave(&gi->spinlock, flags);
1414 cdev->gadget = NULL;
1415 set_gadget_data(gadget, NULL);
1416 spin_unlock_irqrestore(&gi->spinlock, flags);
1419 static int configfs_composite_setup(struct usb_gadget *gadget,
1420 const struct usb_ctrlrequest *ctrl)
1422 struct usb_composite_dev *cdev;
1423 struct gadget_info *gi;
1424 unsigned long flags;
1427 cdev = get_gadget_data(gadget);
1431 gi = container_of(cdev, struct gadget_info, cdev);
1432 spin_lock_irqsave(&gi->spinlock, flags);
1433 cdev = get_gadget_data(gadget);
1434 if (!cdev || gi->unbind) {
1435 spin_unlock_irqrestore(&gi->spinlock, flags);
1439 ret = composite_setup(gadget, ctrl);
1440 spin_unlock_irqrestore(&gi->spinlock, flags);
1444 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1446 struct usb_composite_dev *cdev;
1447 struct gadget_info *gi;
1448 unsigned long flags;
1450 cdev = get_gadget_data(gadget);
1454 gi = container_of(cdev, struct gadget_info, cdev);
1455 spin_lock_irqsave(&gi->spinlock, flags);
1456 cdev = get_gadget_data(gadget);
1457 if (!cdev || gi->unbind) {
1458 spin_unlock_irqrestore(&gi->spinlock, flags);
1462 composite_disconnect(gadget);
1463 spin_unlock_irqrestore(&gi->spinlock, flags);
1466 static void configfs_composite_suspend(struct usb_gadget *gadget)
1468 struct usb_composite_dev *cdev;
1469 struct gadget_info *gi;
1470 unsigned long flags;
1472 cdev = get_gadget_data(gadget);
1476 gi = container_of(cdev, struct gadget_info, cdev);
1477 spin_lock_irqsave(&gi->spinlock, flags);
1478 cdev = get_gadget_data(gadget);
1479 if (!cdev || gi->unbind) {
1480 spin_unlock_irqrestore(&gi->spinlock, flags);
1484 composite_suspend(gadget);
1485 spin_unlock_irqrestore(&gi->spinlock, flags);
1488 static void configfs_composite_resume(struct usb_gadget *gadget)
1490 struct usb_composite_dev *cdev;
1491 struct gadget_info *gi;
1492 unsigned long flags;
1494 cdev = get_gadget_data(gadget);
1498 gi = container_of(cdev, struct gadget_info, cdev);
1499 spin_lock_irqsave(&gi->spinlock, flags);
1500 cdev = get_gadget_data(gadget);
1501 if (!cdev || gi->unbind) {
1502 spin_unlock_irqrestore(&gi->spinlock, flags);
1506 composite_resume(gadget);
1507 spin_unlock_irqrestore(&gi->spinlock, flags);
1510 static const struct usb_gadget_driver configfs_driver_template = {
1511 .bind = configfs_composite_bind,
1512 .unbind = configfs_composite_unbind,
1514 .setup = configfs_composite_setup,
1515 .reset = configfs_composite_disconnect,
1516 .disconnect = configfs_composite_disconnect,
1518 .suspend = configfs_composite_suspend,
1519 .resume = configfs_composite_resume,
1521 .max_speed = USB_SPEED_SUPER_PLUS,
1523 .owner = THIS_MODULE,
1524 .name = "configfs-gadget",
1526 .match_existing_only = 1,
1529 static struct config_group *gadgets_make(
1530 struct config_group *group,
1533 struct gadget_info *gi;
1535 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1537 return ERR_PTR(-ENOMEM);
1539 config_group_init_type_name(&gi->group, name, &gadget_root_type);
1541 config_group_init_type_name(&gi->functions_group, "functions",
1543 configfs_add_default_group(&gi->functions_group, &gi->group);
1545 config_group_init_type_name(&gi->configs_group, "configs",
1547 configfs_add_default_group(&gi->configs_group, &gi->group);
1549 config_group_init_type_name(&gi->strings_group, "strings",
1550 &gadget_strings_strings_type);
1551 configfs_add_default_group(&gi->strings_group, &gi->group);
1553 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1555 configfs_add_default_group(&gi->os_desc_group, &gi->group);
1557 gi->composite.bind = configfs_do_nothing;
1558 gi->composite.unbind = configfs_do_nothing;
1559 gi->composite.suspend = NULL;
1560 gi->composite.resume = NULL;
1561 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1563 spin_lock_init(&gi->spinlock);
1564 mutex_init(&gi->lock);
1565 INIT_LIST_HEAD(&gi->string_list);
1566 INIT_LIST_HEAD(&gi->available_func);
1568 composite_init_dev(&gi->cdev);
1569 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1570 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1571 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1573 gi->composite.gadget_driver = configfs_driver_template;
1575 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1576 gi->composite.name = gi->composite.gadget_driver.function;
1578 if (!gi->composite.gadget_driver.function)
1584 return ERR_PTR(-ENOMEM);
1587 static void gadgets_drop(struct config_group *group, struct config_item *item)
1589 config_item_put(item);
1592 static struct configfs_group_operations gadgets_ops = {
1593 .make_group = &gadgets_make,
1594 .drop_item = &gadgets_drop,
1597 static const struct config_item_type gadgets_type = {
1598 .ct_group_ops = &gadgets_ops,
1599 .ct_owner = THIS_MODULE,
1602 static struct configfs_subsystem gadget_subsys = {
1605 .ci_namebuf = "usb_gadget",
1606 .ci_type = &gadgets_type,
1609 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1612 void unregister_gadget_item(struct config_item *item)
1614 struct gadget_info *gi = to_gadget_info(item);
1616 mutex_lock(&gi->lock);
1617 unregister_gadget(gi);
1618 mutex_unlock(&gi->lock);
1620 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1622 static int __init gadget_cfs_init(void)
1626 config_group_init(&gadget_subsys.su_group);
1628 ret = configfs_register_subsystem(&gadget_subsys);
1631 module_init(gadget_cfs_init);
1633 static void __exit gadget_cfs_exit(void)
1635 configfs_unregister_subsystem(&gadget_subsys);
1637 module_exit(gadget_cfs_exit);