1 #include <linux/configfs.h>
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 #include <linux/device.h>
6 #include <linux/usb/composite.h>
7 #include <linux/usb/gadget_configfs.h>
10 #include "u_os_desc.h"
12 int check_user_usb_string(const char *name,
13 struct usb_gadget_strings *stringtab_dev)
15 unsigned primary_lang;
20 ret = kstrtou16(name, 0, &num);
24 primary_lang = num & 0x3ff;
27 /* simple sanity check for valid langid */
28 switch (primary_lang) {
37 stringtab_dev->language = num;
41 #define MAX_NAME_LEN 40
42 #define MAX_USB_STRING_LANGS 2
44 static const struct usb_descriptor_header *otg_desc[2];
47 struct config_group group;
48 struct config_group functions_group;
49 struct config_group configs_group;
50 struct config_group strings_group;
51 struct config_group os_desc_group;
54 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
55 struct list_head string_list;
56 struct list_head available_func;
58 struct usb_composite_driver composite;
59 struct usb_composite_dev cdev;
62 char qw_sign[OS_STRING_QW_SIGN_LEN];
67 static inline struct gadget_info *to_gadget_info(struct config_item *item)
69 return container_of(to_config_group(item), struct gadget_info, group);
72 struct config_usb_cfg {
73 struct config_group group;
74 struct config_group strings_group;
75 struct list_head string_list;
76 struct usb_configuration c;
77 struct list_head func_list;
78 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
81 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
83 return container_of(to_config_group(item), struct config_usb_cfg,
87 struct gadget_strings {
88 struct usb_gadget_strings stringtab_dev;
89 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
94 struct config_group group;
95 struct list_head list;
99 struct config_group group;
102 struct gadget_config_name {
103 struct usb_gadget_strings stringtab_dev;
104 struct usb_string strings;
107 struct config_group group;
108 struct list_head list;
111 #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1)
113 static int usb_string_copy(const char *s, char **s_copy)
117 char *copy = *s_copy;
119 if (ret > USB_MAX_STRING_LEN)
125 str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
130 if (str[ret - 1] == '\n')
136 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
137 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
140 return sprintf(page, "0x%02x\n", \
141 to_gadget_info(item)->cdev.desc.__name); \
144 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
145 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
148 return sprintf(page, "0x%04x\n", \
149 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
153 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
154 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
155 const char *page, size_t len) \
159 ret = kstrtou8(page, 0, &val); \
162 to_gadget_info(item)->cdev.desc._name = val; \
166 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
167 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
168 const char *page, size_t len) \
172 ret = kstrtou16(page, 0, &val); \
175 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
179 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
180 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
181 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
183 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
184 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
185 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
186 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
187 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
188 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
189 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
190 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
192 static ssize_t is_valid_bcd(u16 bcd_val)
194 if ((bcd_val & 0xf) > 9)
196 if (((bcd_val >> 4) & 0xf) > 9)
198 if (((bcd_val >> 8) & 0xf) > 9)
200 if (((bcd_val >> 12) & 0xf) > 9)
205 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
206 const char *page, size_t len)
211 ret = kstrtou16(page, 0, &bcdDevice);
214 ret = is_valid_bcd(bcdDevice);
218 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
222 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
223 const char *page, size_t len)
228 ret = kstrtou16(page, 0, &bcdUSB);
231 ret = is_valid_bcd(bcdUSB);
235 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
239 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
241 struct gadget_info *gi = to_gadget_info(item);
245 mutex_lock(&gi->lock);
246 udc_name = gi->composite.gadget_driver.udc_name;
247 ret = sprintf(page, "%s\n", udc_name ?: "");
248 mutex_unlock(&gi->lock);
253 static int unregister_gadget(struct gadget_info *gi)
257 if (!gi->composite.gadget_driver.udc_name)
260 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
263 kfree(gi->composite.gadget_driver.udc_name);
264 gi->composite.gadget_driver.udc_name = NULL;
268 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
269 const char *page, size_t len)
271 struct gadget_info *gi = to_gadget_info(item);
275 if (strlen(page) < len)
278 name = kstrdup(page, GFP_KERNEL);
281 if (name[len - 1] == '\n')
282 name[len - 1] = '\0';
284 mutex_lock(&gi->lock);
287 ret = unregister_gadget(gi);
292 if (gi->composite.gadget_driver.udc_name) {
296 gi->composite.gadget_driver.udc_name = name;
297 ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
299 gi->composite.gadget_driver.udc_name = NULL;
303 mutex_unlock(&gi->lock);
307 mutex_unlock(&gi->lock);
311 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
312 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
313 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
314 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
315 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
316 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
317 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
318 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
319 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
321 static struct configfs_attribute *gadget_root_attrs[] = {
322 &gadget_dev_desc_attr_bDeviceClass,
323 &gadget_dev_desc_attr_bDeviceSubClass,
324 &gadget_dev_desc_attr_bDeviceProtocol,
325 &gadget_dev_desc_attr_bMaxPacketSize0,
326 &gadget_dev_desc_attr_idVendor,
327 &gadget_dev_desc_attr_idProduct,
328 &gadget_dev_desc_attr_bcdDevice,
329 &gadget_dev_desc_attr_bcdUSB,
330 &gadget_dev_desc_attr_UDC,
334 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
336 return container_of(to_config_group(item), struct gadget_strings,
340 static inline struct gadget_config_name *to_gadget_config_name(
341 struct config_item *item)
343 return container_of(to_config_group(item), struct gadget_config_name,
347 static inline struct usb_function_instance *to_usb_function_instance(
348 struct config_item *item)
350 return container_of(to_config_group(item),
351 struct usb_function_instance, group);
354 static void gadget_info_attr_release(struct config_item *item)
356 struct gadget_info *gi = to_gadget_info(item);
358 WARN_ON(!list_empty(&gi->cdev.configs));
359 WARN_ON(!list_empty(&gi->string_list));
360 WARN_ON(!list_empty(&gi->available_func));
361 kfree(gi->composite.gadget_driver.function);
365 static struct configfs_item_operations gadget_root_item_ops = {
366 .release = gadget_info_attr_release,
369 static void gadget_config_attr_release(struct config_item *item)
371 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
373 WARN_ON(!list_empty(&cfg->c.functions));
374 list_del(&cfg->c.list);
379 static int config_usb_cfg_link(
380 struct config_item *usb_cfg_ci,
381 struct config_item *usb_func_ci)
383 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
384 struct usb_composite_dev *cdev = cfg->c.cdev;
385 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
387 struct config_group *group = to_config_group(usb_func_ci);
388 struct usb_function_instance *fi = container_of(group,
389 struct usb_function_instance, group);
390 struct usb_function_instance *a_fi;
391 struct usb_function *f;
394 mutex_lock(&gi->lock);
396 * Make sure this function is from within our _this_ gadget and not
397 * from another gadget or a random directory.
398 * Also a function instance can only be linked once.
400 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
409 list_for_each_entry(f, &cfg->func_list, list) {
416 f = usb_get_function(fi);
422 /* stash the function until we bind it to the gadget */
423 list_add_tail(&f->list, &cfg->func_list);
426 mutex_unlock(&gi->lock);
430 static void config_usb_cfg_unlink(
431 struct config_item *usb_cfg_ci,
432 struct config_item *usb_func_ci)
434 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
435 struct usb_composite_dev *cdev = cfg->c.cdev;
436 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
438 struct config_group *group = to_config_group(usb_func_ci);
439 struct usb_function_instance *fi = container_of(group,
440 struct usb_function_instance, group);
441 struct usb_function *f;
444 * ideally I would like to forbid to unlink functions while a gadget is
445 * bound to an UDC. Since this isn't possible at the moment, we simply
446 * force an unbind, the function is available here and then we can
447 * remove the function.
449 mutex_lock(&gi->lock);
450 if (gi->composite.gadget_driver.udc_name)
451 unregister_gadget(gi);
452 WARN_ON(gi->composite.gadget_driver.udc_name);
454 list_for_each_entry(f, &cfg->func_list, list) {
458 mutex_unlock(&gi->lock);
462 mutex_unlock(&gi->lock);
463 WARN(1, "Unable to locate function to unbind\n");
466 static struct configfs_item_operations gadget_config_item_ops = {
467 .release = gadget_config_attr_release,
468 .allow_link = config_usb_cfg_link,
469 .drop_link = config_usb_cfg_unlink,
473 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
476 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
479 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
480 const char *page, size_t len)
484 ret = kstrtou16(page, 0, &val);
487 if (DIV_ROUND_UP(val, 8) > 0xff)
489 to_config_usb_cfg(item)->c.MaxPower = val;
493 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
496 return sprintf(page, "0x%02x\n",
497 to_config_usb_cfg(item)->c.bmAttributes);
500 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
501 const char *page, size_t len)
505 ret = kstrtou8(page, 0, &val);
508 if (!(val & USB_CONFIG_ATT_ONE))
510 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
511 USB_CONFIG_ATT_WAKEUP))
513 to_config_usb_cfg(item)->c.bmAttributes = val;
517 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
518 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
520 static struct configfs_attribute *gadget_config_attrs[] = {
521 &gadget_config_desc_attr_MaxPower,
522 &gadget_config_desc_attr_bmAttributes,
526 static struct config_item_type gadget_config_type = {
527 .ct_item_ops = &gadget_config_item_ops,
528 .ct_attrs = gadget_config_attrs,
529 .ct_owner = THIS_MODULE,
532 static struct config_item_type gadget_root_type = {
533 .ct_item_ops = &gadget_root_item_ops,
534 .ct_attrs = gadget_root_attrs,
535 .ct_owner = THIS_MODULE,
538 static void composite_init_dev(struct usb_composite_dev *cdev)
540 spin_lock_init(&cdev->lock);
541 INIT_LIST_HEAD(&cdev->configs);
542 INIT_LIST_HEAD(&cdev->gstrings);
545 static struct config_group *function_make(
546 struct config_group *group,
549 struct gadget_info *gi;
550 struct usb_function_instance *fi;
551 char buf[MAX_NAME_LEN];
556 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
557 if (ret >= MAX_NAME_LEN)
558 return ERR_PTR(-ENAMETOOLONG);
561 instance_name = strchr(func_name, '.');
562 if (!instance_name) {
563 pr_err("Unable to locate . in FUNC.INSTANCE\n");
564 return ERR_PTR(-EINVAL);
566 *instance_name = '\0';
569 fi = usb_get_function_instance(func_name);
573 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
575 usb_put_function_instance(fi);
578 if (fi->set_inst_name) {
579 ret = fi->set_inst_name(fi, instance_name);
581 usb_put_function_instance(fi);
586 gi = container_of(group, struct gadget_info, functions_group);
588 mutex_lock(&gi->lock);
589 list_add_tail(&fi->cfs_list, &gi->available_func);
590 mutex_unlock(&gi->lock);
594 static void function_drop(
595 struct config_group *group,
596 struct config_item *item)
598 struct usb_function_instance *fi = to_usb_function_instance(item);
599 struct gadget_info *gi;
601 gi = container_of(group, struct gadget_info, functions_group);
603 mutex_lock(&gi->lock);
604 list_del(&fi->cfs_list);
605 mutex_unlock(&gi->lock);
606 config_item_put(item);
609 static struct configfs_group_operations functions_ops = {
610 .make_group = &function_make,
611 .drop_item = &function_drop,
614 static struct config_item_type functions_type = {
615 .ct_group_ops = &functions_ops,
616 .ct_owner = THIS_MODULE,
619 GS_STRINGS_RW(gadget_config_name, configuration);
621 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
622 &gadget_config_name_attr_configuration,
626 static void gadget_config_name_attr_release(struct config_item *item)
628 struct gadget_config_name *cn = to_gadget_config_name(item);
630 kfree(cn->configuration);
636 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
637 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
639 static struct config_group *config_desc_make(
640 struct config_group *group,
643 struct gadget_info *gi;
644 struct config_usb_cfg *cfg;
645 char buf[MAX_NAME_LEN];
650 gi = container_of(group, struct gadget_info, configs_group);
651 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
652 if (ret >= MAX_NAME_LEN)
653 return ERR_PTR(-ENAMETOOLONG);
655 num_str = strchr(buf, '.');
657 pr_err("Unable to locate . in name.bConfigurationValue\n");
658 return ERR_PTR(-EINVAL);
665 return ERR_PTR(-EINVAL);
667 ret = kstrtou8(num_str, 0, &num);
671 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
673 return ERR_PTR(-ENOMEM);
674 cfg->c.label = kstrdup(buf, GFP_KERNEL);
679 cfg->c.bConfigurationValue = num;
680 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
681 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
682 INIT_LIST_HEAD(&cfg->string_list);
683 INIT_LIST_HEAD(&cfg->func_list);
685 config_group_init_type_name(&cfg->group, name,
686 &gadget_config_type);
688 config_group_init_type_name(&cfg->strings_group, "strings",
689 &gadget_config_name_strings_type);
690 configfs_add_default_group(&cfg->strings_group, &cfg->group);
692 ret = usb_add_config_only(&gi->cdev, &cfg->c);
703 static void config_desc_drop(
704 struct config_group *group,
705 struct config_item *item)
707 config_item_put(item);
710 static struct configfs_group_operations config_desc_ops = {
711 .make_group = &config_desc_make,
712 .drop_item = &config_desc_drop,
715 static struct config_item_type config_desc_type = {
716 .ct_group_ops = &config_desc_ops,
717 .ct_owner = THIS_MODULE,
720 GS_STRINGS_RW(gadget_strings, manufacturer);
721 GS_STRINGS_RW(gadget_strings, product);
722 GS_STRINGS_RW(gadget_strings, serialnumber);
724 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
725 &gadget_strings_attr_manufacturer,
726 &gadget_strings_attr_product,
727 &gadget_strings_attr_serialnumber,
731 static void gadget_strings_attr_release(struct config_item *item)
733 struct gadget_strings *gs = to_gadget_strings(item);
735 kfree(gs->manufacturer);
737 kfree(gs->serialnumber);
743 USB_CONFIG_STRING_RW_OPS(gadget_strings);
744 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
746 static inline struct os_desc *to_os_desc(struct config_item *item)
748 return container_of(to_config_group(item), struct os_desc, group);
751 static inline struct gadget_info *os_desc_item_to_gadget_info(
752 struct config_item *item)
754 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
757 static ssize_t os_desc_use_show(struct config_item *item, char *page)
759 return sprintf(page, "%d\n",
760 os_desc_item_to_gadget_info(item)->use_os_desc);
763 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
766 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
770 mutex_lock(&gi->lock);
771 ret = strtobool(page, &use);
773 gi->use_os_desc = use;
776 mutex_unlock(&gi->lock);
781 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
783 return sprintf(page, "0x%02x\n",
784 os_desc_item_to_gadget_info(item)->b_vendor_code);
787 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
788 const char *page, size_t len)
790 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
794 mutex_lock(&gi->lock);
795 ret = kstrtou8(page, 0, &b_vendor_code);
797 gi->b_vendor_code = b_vendor_code;
800 mutex_unlock(&gi->lock);
805 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
807 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
810 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
811 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
817 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
820 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
823 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
824 if (page[l - 1] == '\n')
827 mutex_lock(&gi->lock);
828 res = utf8s_to_utf16s(page, l,
829 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
830 OS_STRING_QW_SIGN_LEN);
833 mutex_unlock(&gi->lock);
838 CONFIGFS_ATTR(os_desc_, use);
839 CONFIGFS_ATTR(os_desc_, b_vendor_code);
840 CONFIGFS_ATTR(os_desc_, qw_sign);
842 static struct configfs_attribute *os_desc_attrs[] = {
844 &os_desc_attr_b_vendor_code,
845 &os_desc_attr_qw_sign,
849 static void os_desc_attr_release(struct config_item *item)
851 struct os_desc *os_desc = to_os_desc(item);
855 static int os_desc_link(struct config_item *os_desc_ci,
856 struct config_item *usb_cfg_ci)
858 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
859 struct gadget_info, os_desc_group);
860 struct usb_composite_dev *cdev = &gi->cdev;
861 struct config_usb_cfg *c_target =
862 container_of(to_config_group(usb_cfg_ci),
863 struct config_usb_cfg, group);
864 struct usb_configuration *c;
867 mutex_lock(&gi->lock);
868 list_for_each_entry(c, &cdev->configs, list) {
869 if (c == &c_target->c)
872 if (c != &c_target->c) {
877 if (cdev->os_desc_config) {
882 cdev->os_desc_config = &c_target->c;
886 mutex_unlock(&gi->lock);
890 static void os_desc_unlink(struct config_item *os_desc_ci,
891 struct config_item *usb_cfg_ci)
893 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
894 struct gadget_info, os_desc_group);
895 struct usb_composite_dev *cdev = &gi->cdev;
897 mutex_lock(&gi->lock);
898 if (gi->composite.gadget_driver.udc_name)
899 unregister_gadget(gi);
900 cdev->os_desc_config = NULL;
901 WARN_ON(gi->composite.gadget_driver.udc_name);
902 mutex_unlock(&gi->lock);
905 static struct configfs_item_operations os_desc_ops = {
906 .release = os_desc_attr_release,
907 .allow_link = os_desc_link,
908 .drop_link = os_desc_unlink,
911 static struct config_item_type os_desc_type = {
912 .ct_item_ops = &os_desc_ops,
913 .ct_attrs = os_desc_attrs,
914 .ct_owner = THIS_MODULE,
917 static inline struct usb_os_desc_ext_prop
918 *to_usb_os_desc_ext_prop(struct config_item *item)
920 return container_of(item, struct usb_os_desc_ext_prop, item);
923 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
925 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
928 static ssize_t ext_prop_type_store(struct config_item *item,
929 const char *page, size_t len)
931 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
932 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
936 if (desc->opts_mutex)
937 mutex_lock(desc->opts_mutex);
938 ret = kstrtou8(page, 0, &type);
941 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
946 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
947 ext_prop->type == USB_EXT_PROP_LE32 ||
948 ext_prop->type == USB_EXT_PROP_BE32) &&
949 (type == USB_EXT_PROP_UNICODE ||
950 type == USB_EXT_PROP_UNICODE_ENV ||
951 type == USB_EXT_PROP_UNICODE_LINK))
952 ext_prop->data_len <<= 1;
953 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
954 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
955 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
956 (type == USB_EXT_PROP_BINARY ||
957 type == USB_EXT_PROP_LE32 ||
958 type == USB_EXT_PROP_BE32))
959 ext_prop->data_len >>= 1;
960 ext_prop->type = type;
964 if (desc->opts_mutex)
965 mutex_unlock(desc->opts_mutex);
969 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
971 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
972 int len = ext_prop->data_len;
974 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
975 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
976 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
978 memcpy(page, ext_prop->data, len);
983 static ssize_t ext_prop_data_store(struct config_item *item,
984 const char *page, size_t len)
986 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
987 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
989 size_t ret_len = len;
991 if (page[len - 1] == '\n' || page[len - 1] == '\0')
993 new_data = kmemdup(page, len, GFP_KERNEL);
997 if (desc->opts_mutex)
998 mutex_lock(desc->opts_mutex);
999 kfree(ext_prop->data);
1000 ext_prop->data = new_data;
1001 desc->ext_prop_len -= ext_prop->data_len;
1002 ext_prop->data_len = len;
1003 desc->ext_prop_len += ext_prop->data_len;
1004 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1005 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1006 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1007 desc->ext_prop_len -= ext_prop->data_len;
1008 ext_prop->data_len <<= 1;
1009 ext_prop->data_len += 2;
1010 desc->ext_prop_len += ext_prop->data_len;
1012 if (desc->opts_mutex)
1013 mutex_unlock(desc->opts_mutex);
1017 CONFIGFS_ATTR(ext_prop_, type);
1018 CONFIGFS_ATTR(ext_prop_, data);
1020 static struct configfs_attribute *ext_prop_attrs[] = {
1021 &ext_prop_attr_type,
1022 &ext_prop_attr_data,
1026 static void usb_os_desc_ext_prop_release(struct config_item *item)
1028 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1030 kfree(ext_prop); /* frees a whole chunk */
1033 static struct configfs_item_operations ext_prop_ops = {
1034 .release = usb_os_desc_ext_prop_release,
1037 static struct config_item *ext_prop_make(
1038 struct config_group *group,
1041 struct usb_os_desc_ext_prop *ext_prop;
1042 struct config_item_type *ext_prop_type;
1043 struct usb_os_desc *desc;
1046 vla_group(data_chunk);
1047 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1048 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1050 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1052 return ERR_PTR(-ENOMEM);
1054 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1055 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1057 desc = container_of(group, struct usb_os_desc, group);
1058 ext_prop_type->ct_item_ops = &ext_prop_ops;
1059 ext_prop_type->ct_attrs = ext_prop_attrs;
1060 ext_prop_type->ct_owner = desc->owner;
1062 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1064 ext_prop->name = kstrdup(name, GFP_KERNEL);
1065 if (!ext_prop->name) {
1067 return ERR_PTR(-ENOMEM);
1069 desc->ext_prop_len += 14;
1070 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1071 if (desc->opts_mutex)
1072 mutex_lock(desc->opts_mutex);
1073 desc->ext_prop_len += ext_prop->name_len;
1074 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1075 ++desc->ext_prop_count;
1076 if (desc->opts_mutex)
1077 mutex_unlock(desc->opts_mutex);
1079 return &ext_prop->item;
1082 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1084 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1085 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1087 if (desc->opts_mutex)
1088 mutex_lock(desc->opts_mutex);
1089 list_del(&ext_prop->entry);
1090 --desc->ext_prop_count;
1091 kfree(ext_prop->name);
1092 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1093 if (desc->opts_mutex)
1094 mutex_unlock(desc->opts_mutex);
1095 config_item_put(item);
1098 static struct configfs_group_operations interf_grp_ops = {
1099 .make_item = &ext_prop_make,
1100 .drop_item = &ext_prop_drop,
1103 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1106 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1110 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1111 const char *page, size_t len)
1113 struct usb_os_desc *desc = to_usb_os_desc(item);
1116 l = min_t(int, 8, len);
1117 if (page[l - 1] == '\n')
1119 if (desc->opts_mutex)
1120 mutex_lock(desc->opts_mutex);
1121 memcpy(desc->ext_compat_id, page, l);
1123 if (desc->opts_mutex)
1124 mutex_unlock(desc->opts_mutex);
1129 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1132 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1136 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1137 const char *page, size_t len)
1139 struct usb_os_desc *desc = to_usb_os_desc(item);
1142 l = min_t(int, 8, len);
1143 if (page[l - 1] == '\n')
1145 if (desc->opts_mutex)
1146 mutex_lock(desc->opts_mutex);
1147 memcpy(desc->ext_compat_id + 8, page, l);
1149 if (desc->opts_mutex)
1150 mutex_unlock(desc->opts_mutex);
1155 CONFIGFS_ATTR(interf_grp_, compatible_id);
1156 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1158 static struct configfs_attribute *interf_grp_attrs[] = {
1159 &interf_grp_attr_compatible_id,
1160 &interf_grp_attr_sub_compatible_id,
1164 struct config_group *usb_os_desc_prepare_interf_dir(
1165 struct config_group *parent,
1167 struct usb_os_desc **desc,
1169 struct module *owner)
1171 struct config_group *os_desc_group;
1172 struct config_item_type *os_desc_type, *interface_type;
1174 vla_group(data_chunk);
1175 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1176 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1177 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1179 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1181 return ERR_PTR(-ENOMEM);
1183 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1184 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1185 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1187 os_desc_type->ct_owner = owner;
1188 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1189 configfs_add_default_group(os_desc_group, parent);
1191 interface_type->ct_group_ops = &interf_grp_ops;
1192 interface_type->ct_attrs = interf_grp_attrs;
1193 interface_type->ct_owner = owner;
1195 while (n_interf--) {
1196 struct usb_os_desc *d;
1200 config_group_init_type_name(&d->group, "", interface_type);
1201 config_item_set_name(&d->group.cg_item, "interface.%s",
1203 configfs_add_default_group(&d->group, os_desc_group);
1206 return os_desc_group;
1208 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1210 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1216 int composite_dev_prepare(struct usb_composite_driver *composite,
1217 struct usb_composite_dev *dev);
1219 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1220 struct usb_ep *ep0);
1222 static void purge_configs_funcs(struct gadget_info *gi)
1224 struct usb_configuration *c;
1226 list_for_each_entry(c, &gi->cdev.configs, list) {
1227 struct usb_function *f, *tmp;
1228 struct config_usb_cfg *cfg;
1230 cfg = container_of(c, struct config_usb_cfg, c);
1232 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1234 list_move(&f->list, &cfg->func_list);
1236 dev_dbg(&gi->cdev.gadget->dev,
1237 "unbind function '%s'/%p\n",
1242 c->next_interface_id = 0;
1243 memset(c->interface, 0, sizeof(c->interface));
1244 c->superspeed_plus = 0;
1251 static int configfs_composite_bind(struct usb_gadget *gadget,
1252 struct usb_gadget_driver *gdriver)
1254 struct usb_composite_driver *composite = to_cdriver(gdriver);
1255 struct gadget_info *gi = container_of(composite,
1256 struct gadget_info, composite);
1257 struct usb_composite_dev *cdev = &gi->cdev;
1258 struct usb_configuration *c;
1259 struct usb_string *s;
1263 /* the gi->lock is hold by the caller */
1265 cdev->gadget = gadget;
1266 set_gadget_data(gadget, cdev);
1267 ret = composite_dev_prepare(composite, cdev);
1270 /* and now the gadget bind */
1273 if (list_empty(&gi->cdev.configs)) {
1274 pr_err("Need at least one configuration in %s.\n",
1275 gi->composite.name);
1276 goto err_comp_cleanup;
1280 list_for_each_entry(c, &gi->cdev.configs, list) {
1281 struct config_usb_cfg *cfg;
1283 cfg = container_of(c, struct config_usb_cfg, c);
1284 if (list_empty(&cfg->func_list)) {
1285 pr_err("Config %s/%d of %s needs at least one function.\n",
1286 c->label, c->bConfigurationValue,
1287 gi->composite.name);
1288 goto err_comp_cleanup;
1292 /* init all strings */
1293 if (!list_empty(&gi->string_list)) {
1294 struct gadget_strings *gs;
1297 list_for_each_entry(gs, &gi->string_list, list) {
1299 gi->gstrings[i] = &gs->stringtab_dev;
1300 gs->stringtab_dev.strings = gs->strings;
1301 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1303 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1304 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1307 gi->gstrings[i] = NULL;
1308 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1309 USB_GADGET_FIRST_AVAIL_IDX);
1312 goto err_comp_cleanup;
1315 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1316 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1317 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1320 if (gi->use_os_desc) {
1321 cdev->use_os_string = true;
1322 cdev->b_vendor_code = gi->b_vendor_code;
1323 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1326 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1327 struct usb_descriptor_header *usb_desc;
1329 usb_desc = usb_otg_descriptor_alloc(gadget);
1332 goto err_comp_cleanup;
1334 usb_otg_descriptor_init(gadget, usb_desc);
1335 otg_desc[0] = usb_desc;
1339 /* Go through all configs, attach all functions */
1340 list_for_each_entry(c, &gi->cdev.configs, list) {
1341 struct config_usb_cfg *cfg;
1342 struct usb_function *f;
1343 struct usb_function *tmp;
1344 struct gadget_config_name *cn;
1346 if (gadget_is_otg(gadget))
1347 c->descriptors = otg_desc;
1349 cfg = container_of(c, struct config_usb_cfg, c);
1350 if (!list_empty(&cfg->string_list)) {
1352 list_for_each_entry(cn, &cfg->string_list, list) {
1353 cfg->gstrings[i] = &cn->stringtab_dev;
1354 cn->stringtab_dev.strings = &cn->strings;
1355 cn->strings.s = cn->configuration;
1358 cfg->gstrings[i] = NULL;
1359 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1362 goto err_comp_cleanup;
1364 c->iConfiguration = s[0].id;
1367 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1369 ret = usb_add_function(c, f);
1371 list_add(&f->list, &cfg->func_list);
1372 goto err_purge_funcs;
1375 usb_ep_autoconfig_reset(cdev->gadget);
1377 if (cdev->use_os_string) {
1378 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1380 goto err_purge_funcs;
1383 usb_ep_autoconfig_reset(cdev->gadget);
1387 purge_configs_funcs(gi);
1389 composite_dev_cleanup(cdev);
1393 static void configfs_composite_unbind(struct usb_gadget *gadget)
1395 struct usb_composite_dev *cdev;
1396 struct gadget_info *gi;
1397 unsigned long flags;
1399 /* the gi->lock is hold by the caller */
1401 cdev = get_gadget_data(gadget);
1402 gi = container_of(cdev, struct gadget_info, cdev);
1403 spin_lock_irqsave(&gi->spinlock, flags);
1405 spin_unlock_irqrestore(&gi->spinlock, flags);
1409 purge_configs_funcs(gi);
1410 composite_dev_cleanup(cdev);
1411 usb_ep_autoconfig_reset(cdev->gadget);
1412 spin_lock_irqsave(&gi->spinlock, flags);
1413 cdev->gadget = NULL;
1414 set_gadget_data(gadget, NULL);
1415 spin_unlock_irqrestore(&gi->spinlock, flags);
1418 static int configfs_composite_setup(struct usb_gadget *gadget,
1419 const struct usb_ctrlrequest *ctrl)
1421 struct usb_composite_dev *cdev;
1422 struct gadget_info *gi;
1423 unsigned long flags;
1426 cdev = get_gadget_data(gadget);
1430 gi = container_of(cdev, struct gadget_info, cdev);
1431 spin_lock_irqsave(&gi->spinlock, flags);
1432 cdev = get_gadget_data(gadget);
1433 if (!cdev || gi->unbind) {
1434 spin_unlock_irqrestore(&gi->spinlock, flags);
1438 ret = composite_setup(gadget, ctrl);
1439 spin_unlock_irqrestore(&gi->spinlock, flags);
1443 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1445 struct usb_composite_dev *cdev;
1446 struct gadget_info *gi;
1447 unsigned long flags;
1449 cdev = get_gadget_data(gadget);
1453 gi = container_of(cdev, struct gadget_info, cdev);
1454 spin_lock_irqsave(&gi->spinlock, flags);
1455 cdev = get_gadget_data(gadget);
1456 if (!cdev || gi->unbind) {
1457 spin_unlock_irqrestore(&gi->spinlock, flags);
1461 composite_disconnect(gadget);
1462 spin_unlock_irqrestore(&gi->spinlock, flags);
1465 static void configfs_composite_suspend(struct usb_gadget *gadget)
1467 struct usb_composite_dev *cdev;
1468 struct gadget_info *gi;
1469 unsigned long flags;
1471 cdev = get_gadget_data(gadget);
1475 gi = container_of(cdev, struct gadget_info, cdev);
1476 spin_lock_irqsave(&gi->spinlock, flags);
1477 cdev = get_gadget_data(gadget);
1478 if (!cdev || gi->unbind) {
1479 spin_unlock_irqrestore(&gi->spinlock, flags);
1483 composite_suspend(gadget);
1484 spin_unlock_irqrestore(&gi->spinlock, flags);
1487 static void configfs_composite_resume(struct usb_gadget *gadget)
1489 struct usb_composite_dev *cdev;
1490 struct gadget_info *gi;
1491 unsigned long flags;
1493 cdev = get_gadget_data(gadget);
1497 gi = container_of(cdev, struct gadget_info, cdev);
1498 spin_lock_irqsave(&gi->spinlock, flags);
1499 cdev = get_gadget_data(gadget);
1500 if (!cdev || gi->unbind) {
1501 spin_unlock_irqrestore(&gi->spinlock, flags);
1505 composite_resume(gadget);
1506 spin_unlock_irqrestore(&gi->spinlock, flags);
1509 static const struct usb_gadget_driver configfs_driver_template = {
1510 .bind = configfs_composite_bind,
1511 .unbind = configfs_composite_unbind,
1513 .setup = configfs_composite_setup,
1514 .reset = configfs_composite_disconnect,
1515 .disconnect = configfs_composite_disconnect,
1517 .suspend = configfs_composite_suspend,
1518 .resume = configfs_composite_resume,
1520 .max_speed = USB_SPEED_SUPER_PLUS,
1522 .owner = THIS_MODULE,
1523 .name = "configfs-gadget",
1525 .match_existing_only = 1,
1528 static struct config_group *gadgets_make(
1529 struct config_group *group,
1532 struct gadget_info *gi;
1534 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1536 return ERR_PTR(-ENOMEM);
1538 config_group_init_type_name(&gi->group, name, &gadget_root_type);
1540 config_group_init_type_name(&gi->functions_group, "functions",
1542 configfs_add_default_group(&gi->functions_group, &gi->group);
1544 config_group_init_type_name(&gi->configs_group, "configs",
1546 configfs_add_default_group(&gi->configs_group, &gi->group);
1548 config_group_init_type_name(&gi->strings_group, "strings",
1549 &gadget_strings_strings_type);
1550 configfs_add_default_group(&gi->strings_group, &gi->group);
1552 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1554 configfs_add_default_group(&gi->os_desc_group, &gi->group);
1556 gi->composite.bind = configfs_do_nothing;
1557 gi->composite.unbind = configfs_do_nothing;
1558 gi->composite.suspend = NULL;
1559 gi->composite.resume = NULL;
1560 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1562 spin_lock_init(&gi->spinlock);
1563 mutex_init(&gi->lock);
1564 INIT_LIST_HEAD(&gi->string_list);
1565 INIT_LIST_HEAD(&gi->available_func);
1567 composite_init_dev(&gi->cdev);
1568 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1569 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1570 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1572 gi->composite.gadget_driver = configfs_driver_template;
1574 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1575 gi->composite.name = gi->composite.gadget_driver.function;
1577 if (!gi->composite.gadget_driver.function)
1583 return ERR_PTR(-ENOMEM);
1586 static void gadgets_drop(struct config_group *group, struct config_item *item)
1588 config_item_put(item);
1591 static struct configfs_group_operations gadgets_ops = {
1592 .make_group = &gadgets_make,
1593 .drop_item = &gadgets_drop,
1596 static struct config_item_type gadgets_type = {
1597 .ct_group_ops = &gadgets_ops,
1598 .ct_owner = THIS_MODULE,
1601 static struct configfs_subsystem gadget_subsys = {
1604 .ci_namebuf = "usb_gadget",
1605 .ci_type = &gadgets_type,
1608 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1611 void unregister_gadget_item(struct config_item *item)
1613 struct gadget_info *gi = to_gadget_info(item);
1615 mutex_lock(&gi->lock);
1616 unregister_gadget(gi);
1617 mutex_unlock(&gi->lock);
1619 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1621 static int __init gadget_cfs_init(void)
1625 config_group_init(&gadget_subsys.su_group);
1627 ret = configfs_register_subsystem(&gadget_subsys);
1630 module_init(gadget_cfs_init);
1632 static void __exit gadget_cfs_exit(void)
1634 configfs_unregister_subsystem(&gadget_subsys);
1636 module_exit(gadget_cfs_exit);