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];
66 static inline struct gadget_info *to_gadget_info(struct config_item *item)
68 return container_of(to_config_group(item), struct gadget_info, group);
71 struct config_usb_cfg {
72 struct config_group group;
73 struct config_group strings_group;
74 struct list_head string_list;
75 struct usb_configuration c;
76 struct list_head func_list;
77 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
80 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
82 return container_of(to_config_group(item), struct config_usb_cfg,
86 struct gadget_strings {
87 struct usb_gadget_strings stringtab_dev;
88 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
93 struct config_group group;
94 struct list_head list;
98 struct config_group group;
101 struct gadget_config_name {
102 struct usb_gadget_strings stringtab_dev;
103 struct usb_string strings;
106 struct config_group group;
107 struct list_head list;
110 static int usb_string_copy(const char *s, char **s_copy)
114 char *copy = *s_copy;
119 str = kstrdup(s, GFP_KERNEL);
122 if (str[ret - 1] == '\n')
129 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
130 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
133 return sprintf(page, "0x%02x\n", \
134 to_gadget_info(item)->cdev.desc.__name); \
137 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
138 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
141 return sprintf(page, "0x%04x\n", \
142 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
146 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
147 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
148 const char *page, size_t len) \
152 ret = kstrtou8(page, 0, &val); \
155 to_gadget_info(item)->cdev.desc._name = val; \
159 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
160 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
161 const char *page, size_t len) \
165 ret = kstrtou16(page, 0, &val); \
168 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
172 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
173 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
174 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
176 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
177 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
178 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
179 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
180 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
181 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
182 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
183 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
185 static ssize_t is_valid_bcd(u16 bcd_val)
187 if ((bcd_val & 0xf) > 9)
189 if (((bcd_val >> 4) & 0xf) > 9)
191 if (((bcd_val >> 8) & 0xf) > 9)
193 if (((bcd_val >> 12) & 0xf) > 9)
198 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
199 const char *page, size_t len)
204 ret = kstrtou16(page, 0, &bcdDevice);
207 ret = is_valid_bcd(bcdDevice);
211 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
215 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
216 const char *page, size_t len)
221 ret = kstrtou16(page, 0, &bcdUSB);
224 ret = is_valid_bcd(bcdUSB);
228 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
232 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
234 char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
236 return sprintf(page, "%s\n", udc_name ?: "");
239 static int unregister_gadget(struct gadget_info *gi)
243 if (!gi->composite.gadget_driver.udc_name)
246 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
249 kfree(gi->composite.gadget_driver.udc_name);
250 gi->composite.gadget_driver.udc_name = NULL;
254 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
255 const char *page, size_t len)
257 struct gadget_info *gi = to_gadget_info(item);
261 name = kstrdup(page, GFP_KERNEL);
264 if (name[len - 1] == '\n')
265 name[len - 1] = '\0';
267 mutex_lock(&gi->lock);
270 ret = unregister_gadget(gi);
275 if (gi->composite.gadget_driver.udc_name) {
279 gi->composite.gadget_driver.udc_name = name;
280 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
282 gi->composite.gadget_driver.udc_name = NULL;
286 mutex_unlock(&gi->lock);
290 mutex_unlock(&gi->lock);
294 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
295 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
296 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
297 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
298 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
299 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
300 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
301 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
302 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
304 static struct configfs_attribute *gadget_root_attrs[] = {
305 &gadget_dev_desc_attr_bDeviceClass,
306 &gadget_dev_desc_attr_bDeviceSubClass,
307 &gadget_dev_desc_attr_bDeviceProtocol,
308 &gadget_dev_desc_attr_bMaxPacketSize0,
309 &gadget_dev_desc_attr_idVendor,
310 &gadget_dev_desc_attr_idProduct,
311 &gadget_dev_desc_attr_bcdDevice,
312 &gadget_dev_desc_attr_bcdUSB,
313 &gadget_dev_desc_attr_UDC,
317 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
319 return container_of(to_config_group(item), struct gadget_strings,
323 static inline struct gadget_config_name *to_gadget_config_name(
324 struct config_item *item)
326 return container_of(to_config_group(item), struct gadget_config_name,
330 static inline struct usb_function_instance *to_usb_function_instance(
331 struct config_item *item)
333 return container_of(to_config_group(item),
334 struct usb_function_instance, group);
337 static void gadget_info_attr_release(struct config_item *item)
339 struct gadget_info *gi = to_gadget_info(item);
341 WARN_ON(!list_empty(&gi->cdev.configs));
342 WARN_ON(!list_empty(&gi->string_list));
343 WARN_ON(!list_empty(&gi->available_func));
344 kfree(gi->composite.gadget_driver.function);
348 static struct configfs_item_operations gadget_root_item_ops = {
349 .release = gadget_info_attr_release,
352 static void gadget_config_attr_release(struct config_item *item)
354 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
356 WARN_ON(!list_empty(&cfg->c.functions));
357 list_del(&cfg->c.list);
362 static int config_usb_cfg_link(
363 struct config_item *usb_cfg_ci,
364 struct config_item *usb_func_ci)
366 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
367 struct usb_composite_dev *cdev = cfg->c.cdev;
368 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
370 struct config_group *group = to_config_group(usb_func_ci);
371 struct usb_function_instance *fi = container_of(group,
372 struct usb_function_instance, group);
373 struct usb_function_instance *a_fi;
374 struct usb_function *f;
377 mutex_lock(&gi->lock);
379 * Make sure this function is from within our _this_ gadget and not
380 * from another gadget or a random directory.
381 * Also a function instance can only be linked once.
383 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
392 list_for_each_entry(f, &cfg->func_list, list) {
399 f = usb_get_function(fi);
405 /* stash the function until we bind it to the gadget */
406 list_add_tail(&f->list, &cfg->func_list);
409 mutex_unlock(&gi->lock);
413 static void config_usb_cfg_unlink(
414 struct config_item *usb_cfg_ci,
415 struct config_item *usb_func_ci)
417 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
418 struct usb_composite_dev *cdev = cfg->c.cdev;
419 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
421 struct config_group *group = to_config_group(usb_func_ci);
422 struct usb_function_instance *fi = container_of(group,
423 struct usb_function_instance, group);
424 struct usb_function *f;
427 * ideally I would like to forbid to unlink functions while a gadget is
428 * bound to an UDC. Since this isn't possible at the moment, we simply
429 * force an unbind, the function is available here and then we can
430 * remove the function.
432 mutex_lock(&gi->lock);
433 if (gi->composite.gadget_driver.udc_name)
434 unregister_gadget(gi);
435 WARN_ON(gi->composite.gadget_driver.udc_name);
437 list_for_each_entry(f, &cfg->func_list, list) {
441 mutex_unlock(&gi->lock);
445 mutex_unlock(&gi->lock);
446 WARN(1, "Unable to locate function to unbind\n");
449 static struct configfs_item_operations gadget_config_item_ops = {
450 .release = gadget_config_attr_release,
451 .allow_link = config_usb_cfg_link,
452 .drop_link = config_usb_cfg_unlink,
456 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
459 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
462 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
463 const char *page, size_t len)
467 ret = kstrtou16(page, 0, &val);
470 if (DIV_ROUND_UP(val, 8) > 0xff)
472 to_config_usb_cfg(item)->c.MaxPower = val;
476 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
479 return sprintf(page, "0x%02x\n",
480 to_config_usb_cfg(item)->c.bmAttributes);
483 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
484 const char *page, size_t len)
488 ret = kstrtou8(page, 0, &val);
491 if (!(val & USB_CONFIG_ATT_ONE))
493 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
494 USB_CONFIG_ATT_WAKEUP))
496 to_config_usb_cfg(item)->c.bmAttributes = val;
500 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
501 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
503 static struct configfs_attribute *gadget_config_attrs[] = {
504 &gadget_config_desc_attr_MaxPower,
505 &gadget_config_desc_attr_bmAttributes,
509 static const struct config_item_type gadget_config_type = {
510 .ct_item_ops = &gadget_config_item_ops,
511 .ct_attrs = gadget_config_attrs,
512 .ct_owner = THIS_MODULE,
515 static const struct config_item_type gadget_root_type = {
516 .ct_item_ops = &gadget_root_item_ops,
517 .ct_attrs = gadget_root_attrs,
518 .ct_owner = THIS_MODULE,
521 static void composite_init_dev(struct usb_composite_dev *cdev)
523 spin_lock_init(&cdev->lock);
524 INIT_LIST_HEAD(&cdev->configs);
525 INIT_LIST_HEAD(&cdev->gstrings);
528 static struct config_group *function_make(
529 struct config_group *group,
532 struct gadget_info *gi;
533 struct usb_function_instance *fi;
534 char buf[MAX_NAME_LEN];
539 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
540 if (ret >= MAX_NAME_LEN)
541 return ERR_PTR(-ENAMETOOLONG);
544 instance_name = strchr(func_name, '.');
545 if (!instance_name) {
546 pr_err("Unable to locate . in FUNC.INSTANCE\n");
547 return ERR_PTR(-EINVAL);
549 *instance_name = '\0';
552 fi = usb_get_function_instance(func_name);
556 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
558 usb_put_function_instance(fi);
561 if (fi->set_inst_name) {
562 ret = fi->set_inst_name(fi, instance_name);
564 usb_put_function_instance(fi);
569 gi = container_of(group, struct gadget_info, functions_group);
571 mutex_lock(&gi->lock);
572 list_add_tail(&fi->cfs_list, &gi->available_func);
573 mutex_unlock(&gi->lock);
577 static void function_drop(
578 struct config_group *group,
579 struct config_item *item)
581 struct usb_function_instance *fi = to_usb_function_instance(item);
582 struct gadget_info *gi;
584 gi = container_of(group, struct gadget_info, functions_group);
586 mutex_lock(&gi->lock);
587 list_del(&fi->cfs_list);
588 mutex_unlock(&gi->lock);
589 config_item_put(item);
592 static struct configfs_group_operations functions_ops = {
593 .make_group = &function_make,
594 .drop_item = &function_drop,
597 static const struct config_item_type functions_type = {
598 .ct_group_ops = &functions_ops,
599 .ct_owner = THIS_MODULE,
602 GS_STRINGS_RW(gadget_config_name, configuration);
604 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
605 &gadget_config_name_attr_configuration,
609 static void gadget_config_name_attr_release(struct config_item *item)
611 struct gadget_config_name *cn = to_gadget_config_name(item);
613 kfree(cn->configuration);
619 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
620 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
622 static struct config_group *config_desc_make(
623 struct config_group *group,
626 struct gadget_info *gi;
627 struct config_usb_cfg *cfg;
628 char buf[MAX_NAME_LEN];
633 gi = container_of(group, struct gadget_info, configs_group);
634 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
635 if (ret >= MAX_NAME_LEN)
636 return ERR_PTR(-ENAMETOOLONG);
638 num_str = strchr(buf, '.');
640 pr_err("Unable to locate . in name.bConfigurationValue\n");
641 return ERR_PTR(-EINVAL);
648 return ERR_PTR(-EINVAL);
650 ret = kstrtou8(num_str, 0, &num);
654 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
656 return ERR_PTR(-ENOMEM);
657 cfg->c.label = kstrdup(buf, GFP_KERNEL);
662 cfg->c.bConfigurationValue = num;
663 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
664 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
665 INIT_LIST_HEAD(&cfg->string_list);
666 INIT_LIST_HEAD(&cfg->func_list);
668 config_group_init_type_name(&cfg->group, name,
669 &gadget_config_type);
671 config_group_init_type_name(&cfg->strings_group, "strings",
672 &gadget_config_name_strings_type);
673 configfs_add_default_group(&cfg->strings_group, &cfg->group);
675 ret = usb_add_config_only(&gi->cdev, &cfg->c);
686 static void config_desc_drop(
687 struct config_group *group,
688 struct config_item *item)
690 config_item_put(item);
693 static struct configfs_group_operations config_desc_ops = {
694 .make_group = &config_desc_make,
695 .drop_item = &config_desc_drop,
698 static const struct config_item_type config_desc_type = {
699 .ct_group_ops = &config_desc_ops,
700 .ct_owner = THIS_MODULE,
703 GS_STRINGS_RW(gadget_strings, manufacturer);
704 GS_STRINGS_RW(gadget_strings, product);
705 GS_STRINGS_RW(gadget_strings, serialnumber);
707 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
708 &gadget_strings_attr_manufacturer,
709 &gadget_strings_attr_product,
710 &gadget_strings_attr_serialnumber,
714 static void gadget_strings_attr_release(struct config_item *item)
716 struct gadget_strings *gs = to_gadget_strings(item);
718 kfree(gs->manufacturer);
720 kfree(gs->serialnumber);
726 USB_CONFIG_STRING_RW_OPS(gadget_strings);
727 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
729 static inline struct os_desc *to_os_desc(struct config_item *item)
731 return container_of(to_config_group(item), struct os_desc, group);
734 static inline struct gadget_info *os_desc_item_to_gadget_info(
735 struct config_item *item)
737 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
740 static ssize_t os_desc_use_show(struct config_item *item, char *page)
742 return sprintf(page, "%d\n",
743 os_desc_item_to_gadget_info(item)->use_os_desc);
746 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
749 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
753 mutex_lock(&gi->lock);
754 ret = strtobool(page, &use);
756 gi->use_os_desc = use;
759 mutex_unlock(&gi->lock);
764 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
766 return sprintf(page, "0x%02x\n",
767 os_desc_item_to_gadget_info(item)->b_vendor_code);
770 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
771 const char *page, size_t len)
773 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
777 mutex_lock(&gi->lock);
778 ret = kstrtou8(page, 0, &b_vendor_code);
780 gi->b_vendor_code = b_vendor_code;
783 mutex_unlock(&gi->lock);
788 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
790 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
793 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
794 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
800 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
803 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
806 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
807 if (page[l - 1] == '\n')
810 mutex_lock(&gi->lock);
811 res = utf8s_to_utf16s(page, l,
812 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
813 OS_STRING_QW_SIGN_LEN);
816 mutex_unlock(&gi->lock);
821 CONFIGFS_ATTR(os_desc_, use);
822 CONFIGFS_ATTR(os_desc_, b_vendor_code);
823 CONFIGFS_ATTR(os_desc_, qw_sign);
825 static struct configfs_attribute *os_desc_attrs[] = {
827 &os_desc_attr_b_vendor_code,
828 &os_desc_attr_qw_sign,
832 static void os_desc_attr_release(struct config_item *item)
834 struct os_desc *os_desc = to_os_desc(item);
838 static int os_desc_link(struct config_item *os_desc_ci,
839 struct config_item *usb_cfg_ci)
841 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
842 struct gadget_info, os_desc_group);
843 struct usb_composite_dev *cdev = &gi->cdev;
844 struct config_usb_cfg *c_target =
845 container_of(to_config_group(usb_cfg_ci),
846 struct config_usb_cfg, group);
847 struct usb_configuration *c;
850 mutex_lock(&gi->lock);
851 list_for_each_entry(c, &cdev->configs, list) {
852 if (c == &c_target->c)
855 if (c != &c_target->c) {
860 if (cdev->os_desc_config) {
865 cdev->os_desc_config = &c_target->c;
869 mutex_unlock(&gi->lock);
873 static void os_desc_unlink(struct config_item *os_desc_ci,
874 struct config_item *usb_cfg_ci)
876 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
877 struct gadget_info, os_desc_group);
878 struct usb_composite_dev *cdev = &gi->cdev;
880 mutex_lock(&gi->lock);
881 if (gi->composite.gadget_driver.udc_name)
882 unregister_gadget(gi);
883 cdev->os_desc_config = NULL;
884 WARN_ON(gi->composite.gadget_driver.udc_name);
885 mutex_unlock(&gi->lock);
888 static struct configfs_item_operations os_desc_ops = {
889 .release = os_desc_attr_release,
890 .allow_link = os_desc_link,
891 .drop_link = os_desc_unlink,
894 static struct config_item_type os_desc_type = {
895 .ct_item_ops = &os_desc_ops,
896 .ct_attrs = os_desc_attrs,
897 .ct_owner = THIS_MODULE,
900 static inline struct usb_os_desc_ext_prop
901 *to_usb_os_desc_ext_prop(struct config_item *item)
903 return container_of(item, struct usb_os_desc_ext_prop, item);
906 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
908 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
911 static ssize_t ext_prop_type_store(struct config_item *item,
912 const char *page, size_t len)
914 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
915 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
919 if (desc->opts_mutex)
920 mutex_lock(desc->opts_mutex);
921 ret = kstrtou8(page, 0, &type);
924 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
929 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
930 ext_prop->type == USB_EXT_PROP_LE32 ||
931 ext_prop->type == USB_EXT_PROP_BE32) &&
932 (type == USB_EXT_PROP_UNICODE ||
933 type == USB_EXT_PROP_UNICODE_ENV ||
934 type == USB_EXT_PROP_UNICODE_LINK))
935 ext_prop->data_len <<= 1;
936 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
937 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
938 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
939 (type == USB_EXT_PROP_BINARY ||
940 type == USB_EXT_PROP_LE32 ||
941 type == USB_EXT_PROP_BE32))
942 ext_prop->data_len >>= 1;
943 ext_prop->type = type;
947 if (desc->opts_mutex)
948 mutex_unlock(desc->opts_mutex);
952 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
954 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
955 int len = ext_prop->data_len;
957 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
958 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
959 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
961 memcpy(page, ext_prop->data, len);
966 static ssize_t ext_prop_data_store(struct config_item *item,
967 const char *page, size_t len)
969 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
970 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
972 size_t ret_len = len;
974 if (page[len - 1] == '\n' || page[len - 1] == '\0')
976 new_data = kmemdup(page, len, GFP_KERNEL);
980 if (desc->opts_mutex)
981 mutex_lock(desc->opts_mutex);
982 kfree(ext_prop->data);
983 ext_prop->data = new_data;
984 desc->ext_prop_len -= ext_prop->data_len;
985 ext_prop->data_len = len;
986 desc->ext_prop_len += ext_prop->data_len;
987 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
988 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
989 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
990 desc->ext_prop_len -= ext_prop->data_len;
991 ext_prop->data_len <<= 1;
992 ext_prop->data_len += 2;
993 desc->ext_prop_len += ext_prop->data_len;
995 if (desc->opts_mutex)
996 mutex_unlock(desc->opts_mutex);
1000 CONFIGFS_ATTR(ext_prop_, type);
1001 CONFIGFS_ATTR(ext_prop_, data);
1003 static struct configfs_attribute *ext_prop_attrs[] = {
1004 &ext_prop_attr_type,
1005 &ext_prop_attr_data,
1009 static void usb_os_desc_ext_prop_release(struct config_item *item)
1011 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1013 kfree(ext_prop); /* frees a whole chunk */
1016 static struct configfs_item_operations ext_prop_ops = {
1017 .release = usb_os_desc_ext_prop_release,
1020 static struct config_item *ext_prop_make(
1021 struct config_group *group,
1024 struct usb_os_desc_ext_prop *ext_prop;
1025 struct config_item_type *ext_prop_type;
1026 struct usb_os_desc *desc;
1029 vla_group(data_chunk);
1030 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1031 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1033 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1035 return ERR_PTR(-ENOMEM);
1037 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1038 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1040 desc = container_of(group, struct usb_os_desc, group);
1041 ext_prop_type->ct_item_ops = &ext_prop_ops;
1042 ext_prop_type->ct_attrs = ext_prop_attrs;
1043 ext_prop_type->ct_owner = desc->owner;
1045 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1047 ext_prop->name = kstrdup(name, GFP_KERNEL);
1048 if (!ext_prop->name) {
1050 return ERR_PTR(-ENOMEM);
1052 desc->ext_prop_len += 14;
1053 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1054 if (desc->opts_mutex)
1055 mutex_lock(desc->opts_mutex);
1056 desc->ext_prop_len += ext_prop->name_len;
1057 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1058 ++desc->ext_prop_count;
1059 if (desc->opts_mutex)
1060 mutex_unlock(desc->opts_mutex);
1062 return &ext_prop->item;
1065 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1067 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1068 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1070 if (desc->opts_mutex)
1071 mutex_lock(desc->opts_mutex);
1072 list_del(&ext_prop->entry);
1073 --desc->ext_prop_count;
1074 kfree(ext_prop->name);
1075 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1076 if (desc->opts_mutex)
1077 mutex_unlock(desc->opts_mutex);
1078 config_item_put(item);
1081 static struct configfs_group_operations interf_grp_ops = {
1082 .make_item = &ext_prop_make,
1083 .drop_item = &ext_prop_drop,
1086 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1089 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1093 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1094 const char *page, size_t len)
1096 struct usb_os_desc *desc = to_usb_os_desc(item);
1099 l = min_t(int, 8, len);
1100 if (page[l - 1] == '\n')
1102 if (desc->opts_mutex)
1103 mutex_lock(desc->opts_mutex);
1104 memcpy(desc->ext_compat_id, page, l);
1106 if (desc->opts_mutex)
1107 mutex_unlock(desc->opts_mutex);
1112 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1115 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1119 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1120 const char *page, size_t len)
1122 struct usb_os_desc *desc = to_usb_os_desc(item);
1125 l = min_t(int, 8, len);
1126 if (page[l - 1] == '\n')
1128 if (desc->opts_mutex)
1129 mutex_lock(desc->opts_mutex);
1130 memcpy(desc->ext_compat_id + 8, page, l);
1132 if (desc->opts_mutex)
1133 mutex_unlock(desc->opts_mutex);
1138 CONFIGFS_ATTR(interf_grp_, compatible_id);
1139 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1141 static struct configfs_attribute *interf_grp_attrs[] = {
1142 &interf_grp_attr_compatible_id,
1143 &interf_grp_attr_sub_compatible_id,
1147 struct config_group *usb_os_desc_prepare_interf_dir(
1148 struct config_group *parent,
1150 struct usb_os_desc **desc,
1152 struct module *owner)
1154 struct config_group *os_desc_group;
1155 struct config_item_type *os_desc_type, *interface_type;
1157 vla_group(data_chunk);
1158 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1159 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1160 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1162 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1164 return ERR_PTR(-ENOMEM);
1166 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1167 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1168 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1170 os_desc_type->ct_owner = owner;
1171 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1172 configfs_add_default_group(os_desc_group, parent);
1174 interface_type->ct_group_ops = &interf_grp_ops;
1175 interface_type->ct_attrs = interf_grp_attrs;
1176 interface_type->ct_owner = owner;
1178 while (n_interf--) {
1179 struct usb_os_desc *d;
1183 config_group_init_type_name(&d->group, "", interface_type);
1184 config_item_set_name(&d->group.cg_item, "interface.%s",
1186 configfs_add_default_group(&d->group, os_desc_group);
1189 return os_desc_group;
1191 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1193 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1199 int composite_dev_prepare(struct usb_composite_driver *composite,
1200 struct usb_composite_dev *dev);
1202 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1203 struct usb_ep *ep0);
1205 static void purge_configs_funcs(struct gadget_info *gi)
1207 struct usb_configuration *c;
1209 list_for_each_entry(c, &gi->cdev.configs, list) {
1210 struct usb_function *f, *tmp;
1211 struct config_usb_cfg *cfg;
1213 cfg = container_of(c, struct config_usb_cfg, c);
1215 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1217 list_move_tail(&f->list, &cfg->func_list);
1219 dev_dbg(&gi->cdev.gadget->dev,
1220 "unbind function '%s'/%p\n",
1225 c->next_interface_id = 0;
1226 memset(c->interface, 0, sizeof(c->interface));
1227 c->superspeed_plus = 0;
1234 static int configfs_composite_bind(struct usb_gadget *gadget,
1235 struct usb_gadget_driver *gdriver)
1237 struct usb_composite_driver *composite = to_cdriver(gdriver);
1238 struct gadget_info *gi = container_of(composite,
1239 struct gadget_info, composite);
1240 struct usb_composite_dev *cdev = &gi->cdev;
1241 struct usb_configuration *c;
1242 struct usb_string *s;
1246 /* the gi->lock is hold by the caller */
1247 cdev->gadget = gadget;
1248 set_gadget_data(gadget, cdev);
1249 ret = composite_dev_prepare(composite, cdev);
1252 /* and now the gadget bind */
1255 if (list_empty(&gi->cdev.configs)) {
1256 pr_err("Need at least one configuration in %s.\n",
1257 gi->composite.name);
1258 goto err_comp_cleanup;
1262 list_for_each_entry(c, &gi->cdev.configs, list) {
1263 struct config_usb_cfg *cfg;
1265 cfg = container_of(c, struct config_usb_cfg, c);
1266 if (list_empty(&cfg->func_list)) {
1267 pr_err("Config %s/%d of %s needs at least one function.\n",
1268 c->label, c->bConfigurationValue,
1269 gi->composite.name);
1270 goto err_comp_cleanup;
1274 /* init all strings */
1275 if (!list_empty(&gi->string_list)) {
1276 struct gadget_strings *gs;
1279 list_for_each_entry(gs, &gi->string_list, list) {
1281 gi->gstrings[i] = &gs->stringtab_dev;
1282 gs->stringtab_dev.strings = gs->strings;
1283 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1285 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1286 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1289 gi->gstrings[i] = NULL;
1290 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1291 USB_GADGET_FIRST_AVAIL_IDX);
1294 goto err_comp_cleanup;
1297 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1298 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1299 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1302 if (gi->use_os_desc) {
1303 cdev->use_os_string = true;
1304 cdev->b_vendor_code = gi->b_vendor_code;
1305 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1308 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1309 struct usb_descriptor_header *usb_desc;
1311 usb_desc = usb_otg_descriptor_alloc(gadget);
1314 goto err_comp_cleanup;
1316 usb_otg_descriptor_init(gadget, usb_desc);
1317 otg_desc[0] = usb_desc;
1321 /* Go through all configs, attach all functions */
1322 list_for_each_entry(c, &gi->cdev.configs, list) {
1323 struct config_usb_cfg *cfg;
1324 struct usb_function *f;
1325 struct usb_function *tmp;
1326 struct gadget_config_name *cn;
1328 if (gadget_is_otg(gadget))
1329 c->descriptors = otg_desc;
1331 cfg = container_of(c, struct config_usb_cfg, c);
1332 if (!list_empty(&cfg->string_list)) {
1334 list_for_each_entry(cn, &cfg->string_list, list) {
1335 cfg->gstrings[i] = &cn->stringtab_dev;
1336 cn->stringtab_dev.strings = &cn->strings;
1337 cn->strings.s = cn->configuration;
1340 cfg->gstrings[i] = NULL;
1341 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1344 goto err_comp_cleanup;
1346 c->iConfiguration = s[0].id;
1349 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1351 ret = usb_add_function(c, f);
1353 list_add(&f->list, &cfg->func_list);
1354 goto err_purge_funcs;
1357 usb_ep_autoconfig_reset(cdev->gadget);
1359 if (cdev->use_os_string) {
1360 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1362 goto err_purge_funcs;
1365 usb_ep_autoconfig_reset(cdev->gadget);
1369 purge_configs_funcs(gi);
1371 composite_dev_cleanup(cdev);
1375 static void configfs_composite_unbind(struct usb_gadget *gadget)
1377 struct usb_composite_dev *cdev;
1378 struct gadget_info *gi;
1380 /* the gi->lock is hold by the caller */
1382 cdev = get_gadget_data(gadget);
1383 gi = container_of(cdev, struct gadget_info, cdev);
1387 purge_configs_funcs(gi);
1388 composite_dev_cleanup(cdev);
1389 usb_ep_autoconfig_reset(cdev->gadget);
1390 cdev->gadget = NULL;
1391 set_gadget_data(gadget, NULL);
1394 static const struct usb_gadget_driver configfs_driver_template = {
1395 .bind = configfs_composite_bind,
1396 .unbind = configfs_composite_unbind,
1398 .setup = composite_setup,
1399 .reset = composite_disconnect,
1400 .disconnect = composite_disconnect,
1402 .suspend = composite_suspend,
1403 .resume = composite_resume,
1405 .max_speed = USB_SPEED_SUPER,
1407 .owner = THIS_MODULE,
1408 .name = "configfs-gadget",
1410 .match_existing_only = 1,
1413 static struct config_group *gadgets_make(
1414 struct config_group *group,
1417 struct gadget_info *gi;
1419 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1421 return ERR_PTR(-ENOMEM);
1423 config_group_init_type_name(&gi->group, name, &gadget_root_type);
1425 config_group_init_type_name(&gi->functions_group, "functions",
1427 configfs_add_default_group(&gi->functions_group, &gi->group);
1429 config_group_init_type_name(&gi->configs_group, "configs",
1431 configfs_add_default_group(&gi->configs_group, &gi->group);
1433 config_group_init_type_name(&gi->strings_group, "strings",
1434 &gadget_strings_strings_type);
1435 configfs_add_default_group(&gi->strings_group, &gi->group);
1437 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1439 configfs_add_default_group(&gi->os_desc_group, &gi->group);
1441 gi->composite.bind = configfs_do_nothing;
1442 gi->composite.unbind = configfs_do_nothing;
1443 gi->composite.suspend = NULL;
1444 gi->composite.resume = NULL;
1445 gi->composite.max_speed = USB_SPEED_SUPER;
1447 mutex_init(&gi->lock);
1448 INIT_LIST_HEAD(&gi->string_list);
1449 INIT_LIST_HEAD(&gi->available_func);
1451 composite_init_dev(&gi->cdev);
1452 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1453 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1454 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1456 gi->composite.gadget_driver = configfs_driver_template;
1458 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1459 gi->composite.name = gi->composite.gadget_driver.function;
1461 if (!gi->composite.gadget_driver.function)
1467 return ERR_PTR(-ENOMEM);
1470 static void gadgets_drop(struct config_group *group, struct config_item *item)
1472 config_item_put(item);
1475 static struct configfs_group_operations gadgets_ops = {
1476 .make_group = &gadgets_make,
1477 .drop_item = &gadgets_drop,
1480 static const struct config_item_type gadgets_type = {
1481 .ct_group_ops = &gadgets_ops,
1482 .ct_owner = THIS_MODULE,
1485 static struct configfs_subsystem gadget_subsys = {
1488 .ci_namebuf = "usb_gadget",
1489 .ci_type = &gadgets_type,
1492 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1495 void unregister_gadget_item(struct config_item *item)
1497 struct gadget_info *gi = to_gadget_info(item);
1499 mutex_lock(&gi->lock);
1500 unregister_gadget(gi);
1501 mutex_unlock(&gi->lock);
1503 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1505 static int __init gadget_cfs_init(void)
1509 config_group_init(&gadget_subsys.su_group);
1511 ret = configfs_register_subsystem(&gadget_subsys);
1514 module_init(gadget_cfs_init);
1516 static void __exit gadget_cfs_exit(void)
1518 configfs_unregister_subsystem(&gadget_subsys);
1520 module_exit(gadget_cfs_exit);