1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/configfs.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/device.h>
7 #include <linux/usb/composite.h>
8 #include <linux/usb/gadget_configfs.h>
11 #include "u_os_desc.h"
13 int check_user_usb_string(const char *name,
14 struct usb_gadget_strings *stringtab_dev)
19 ret = kstrtou16(name, 0, &num);
23 if (!usb_validate_langid(num))
26 stringtab_dev->language = num;
30 #define MAX_NAME_LEN 40
31 #define MAX_USB_STRING_LANGS 2
33 static const struct usb_descriptor_header *otg_desc[2];
36 struct config_group group;
37 struct config_group functions_group;
38 struct config_group configs_group;
39 struct config_group strings_group;
40 struct config_group os_desc_group;
43 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
44 struct list_head string_list;
45 struct list_head available_func;
47 struct usb_composite_driver composite;
48 struct usb_composite_dev cdev;
51 char qw_sign[OS_STRING_QW_SIGN_LEN];
56 static inline struct gadget_info *to_gadget_info(struct config_item *item)
58 return container_of(to_config_group(item), struct gadget_info, group);
61 struct config_usb_cfg {
62 struct config_group group;
63 struct config_group strings_group;
64 struct list_head string_list;
65 struct usb_configuration c;
66 struct list_head func_list;
67 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
70 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
72 return container_of(to_config_group(item), struct config_usb_cfg,
76 struct gadget_strings {
77 struct usb_gadget_strings stringtab_dev;
78 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
83 struct config_group group;
84 struct list_head list;
88 struct config_group group;
91 struct gadget_config_name {
92 struct usb_gadget_strings stringtab_dev;
93 struct usb_string strings;
96 struct config_group group;
97 struct list_head list;
100 static int usb_string_copy(const char *s, char **s_copy)
104 char *copy = *s_copy;
106 if (ret > USB_MAX_STRING_LEN)
109 str = kstrdup(s, GFP_KERNEL);
112 if (str[ret - 1] == '\n')
119 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
120 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
123 return sprintf(page, "0x%02x\n", \
124 to_gadget_info(item)->cdev.desc.__name); \
127 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
128 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
131 return sprintf(page, "0x%04x\n", \
132 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
136 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
137 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
138 const char *page, size_t len) \
142 ret = kstrtou8(page, 0, &val); \
145 to_gadget_info(item)->cdev.desc._name = val; \
149 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
150 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
151 const char *page, size_t len) \
155 ret = kstrtou16(page, 0, &val); \
158 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
162 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
163 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
164 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
166 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
167 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
168 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
169 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
170 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
171 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
172 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
173 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
175 static ssize_t is_valid_bcd(u16 bcd_val)
177 if ((bcd_val & 0xf) > 9)
179 if (((bcd_val >> 4) & 0xf) > 9)
181 if (((bcd_val >> 8) & 0xf) > 9)
183 if (((bcd_val >> 12) & 0xf) > 9)
188 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
189 const char *page, size_t len)
194 ret = kstrtou16(page, 0, &bcdDevice);
197 ret = is_valid_bcd(bcdDevice);
201 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
205 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
206 const char *page, size_t len)
211 ret = kstrtou16(page, 0, &bcdUSB);
214 ret = is_valid_bcd(bcdUSB);
218 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
222 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
224 struct gadget_info *gi = to_gadget_info(item);
228 mutex_lock(&gi->lock);
229 udc_name = gi->composite.gadget_driver.udc_name;
230 ret = sprintf(page, "%s\n", udc_name ?: "");
231 mutex_unlock(&gi->lock);
236 static int unregister_gadget(struct gadget_info *gi)
240 if (!gi->composite.gadget_driver.udc_name)
243 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
246 kfree(gi->composite.gadget_driver.udc_name);
247 gi->composite.gadget_driver.udc_name = NULL;
251 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
252 const char *page, size_t len)
254 struct gadget_info *gi = to_gadget_info(item);
258 if (strlen(page) < len)
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 static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
297 enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
299 return sprintf(page, "%s\n", usb_speed_string(speed));
302 static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
303 const char *page, size_t len)
305 struct gadget_info *gi = to_gadget_info(item);
307 mutex_lock(&gi->lock);
309 /* Prevent changing of max_speed after the driver is binded */
310 if (gi->composite.gadget_driver.udc_name)
313 if (strncmp(page, "super-speed-plus", 16) == 0)
314 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
315 else if (strncmp(page, "super-speed", 11) == 0)
316 gi->composite.max_speed = USB_SPEED_SUPER;
317 else if (strncmp(page, "high-speed", 10) == 0)
318 gi->composite.max_speed = USB_SPEED_HIGH;
319 else if (strncmp(page, "full-speed", 10) == 0)
320 gi->composite.max_speed = USB_SPEED_FULL;
321 else if (strncmp(page, "low-speed", 9) == 0)
322 gi->composite.max_speed = USB_SPEED_LOW;
326 gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
328 mutex_unlock(&gi->lock);
331 mutex_unlock(&gi->lock);
335 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
336 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
337 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
338 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
339 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
340 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
341 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
342 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
343 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
344 CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
346 static struct configfs_attribute *gadget_root_attrs[] = {
347 &gadget_dev_desc_attr_bDeviceClass,
348 &gadget_dev_desc_attr_bDeviceSubClass,
349 &gadget_dev_desc_attr_bDeviceProtocol,
350 &gadget_dev_desc_attr_bMaxPacketSize0,
351 &gadget_dev_desc_attr_idVendor,
352 &gadget_dev_desc_attr_idProduct,
353 &gadget_dev_desc_attr_bcdDevice,
354 &gadget_dev_desc_attr_bcdUSB,
355 &gadget_dev_desc_attr_UDC,
356 &gadget_dev_desc_attr_max_speed,
360 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
362 return container_of(to_config_group(item), struct gadget_strings,
366 static inline struct gadget_config_name *to_gadget_config_name(
367 struct config_item *item)
369 return container_of(to_config_group(item), struct gadget_config_name,
373 static inline struct usb_function_instance *to_usb_function_instance(
374 struct config_item *item)
376 return container_of(to_config_group(item),
377 struct usb_function_instance, group);
380 static void gadget_info_attr_release(struct config_item *item)
382 struct gadget_info *gi = to_gadget_info(item);
384 WARN_ON(!list_empty(&gi->cdev.configs));
385 WARN_ON(!list_empty(&gi->string_list));
386 WARN_ON(!list_empty(&gi->available_func));
387 kfree(gi->composite.gadget_driver.function);
391 static struct configfs_item_operations gadget_root_item_ops = {
392 .release = gadget_info_attr_release,
395 static void gadget_config_attr_release(struct config_item *item)
397 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
399 WARN_ON(!list_empty(&cfg->c.functions));
400 list_del(&cfg->c.list);
405 static int config_usb_cfg_link(
406 struct config_item *usb_cfg_ci,
407 struct config_item *usb_func_ci)
409 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
410 struct usb_composite_dev *cdev = cfg->c.cdev;
411 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
413 struct config_group *group = to_config_group(usb_func_ci);
414 struct usb_function_instance *fi = container_of(group,
415 struct usb_function_instance, group);
416 struct usb_function_instance *a_fi;
417 struct usb_function *f;
420 mutex_lock(&gi->lock);
422 * Make sure this function is from within our _this_ gadget and not
423 * from another gadget or a random directory.
424 * Also a function instance can only be linked once.
426 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
435 list_for_each_entry(f, &cfg->func_list, list) {
442 f = usb_get_function(fi);
448 /* stash the function until we bind it to the gadget */
449 list_add_tail(&f->list, &cfg->func_list);
452 mutex_unlock(&gi->lock);
456 static void config_usb_cfg_unlink(
457 struct config_item *usb_cfg_ci,
458 struct config_item *usb_func_ci)
460 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
461 struct usb_composite_dev *cdev = cfg->c.cdev;
462 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
464 struct config_group *group = to_config_group(usb_func_ci);
465 struct usb_function_instance *fi = container_of(group,
466 struct usb_function_instance, group);
467 struct usb_function *f;
470 * ideally I would like to forbid to unlink functions while a gadget is
471 * bound to an UDC. Since this isn't possible at the moment, we simply
472 * force an unbind, the function is available here and then we can
473 * remove the function.
475 mutex_lock(&gi->lock);
476 if (gi->composite.gadget_driver.udc_name)
477 unregister_gadget(gi);
478 WARN_ON(gi->composite.gadget_driver.udc_name);
480 list_for_each_entry(f, &cfg->func_list, list) {
484 mutex_unlock(&gi->lock);
488 mutex_unlock(&gi->lock);
489 WARN(1, "Unable to locate function to unbind\n");
492 static struct configfs_item_operations gadget_config_item_ops = {
493 .release = gadget_config_attr_release,
494 .allow_link = config_usb_cfg_link,
495 .drop_link = config_usb_cfg_unlink,
499 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
502 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
505 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
506 const char *page, size_t len)
510 ret = kstrtou16(page, 0, &val);
513 if (DIV_ROUND_UP(val, 8) > 0xff)
515 to_config_usb_cfg(item)->c.MaxPower = val;
519 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
522 return sprintf(page, "0x%02x\n",
523 to_config_usb_cfg(item)->c.bmAttributes);
526 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
527 const char *page, size_t len)
531 ret = kstrtou8(page, 0, &val);
534 if (!(val & USB_CONFIG_ATT_ONE))
536 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
537 USB_CONFIG_ATT_WAKEUP))
539 to_config_usb_cfg(item)->c.bmAttributes = val;
543 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
544 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
546 static struct configfs_attribute *gadget_config_attrs[] = {
547 &gadget_config_desc_attr_MaxPower,
548 &gadget_config_desc_attr_bmAttributes,
552 static const struct config_item_type gadget_config_type = {
553 .ct_item_ops = &gadget_config_item_ops,
554 .ct_attrs = gadget_config_attrs,
555 .ct_owner = THIS_MODULE,
558 static const struct config_item_type gadget_root_type = {
559 .ct_item_ops = &gadget_root_item_ops,
560 .ct_attrs = gadget_root_attrs,
561 .ct_owner = THIS_MODULE,
564 static void composite_init_dev(struct usb_composite_dev *cdev)
566 spin_lock_init(&cdev->lock);
567 INIT_LIST_HEAD(&cdev->configs);
568 INIT_LIST_HEAD(&cdev->gstrings);
571 static struct config_group *function_make(
572 struct config_group *group,
575 struct gadget_info *gi;
576 struct usb_function_instance *fi;
577 char buf[MAX_NAME_LEN];
582 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
583 if (ret >= MAX_NAME_LEN)
584 return ERR_PTR(-ENAMETOOLONG);
587 instance_name = strchr(func_name, '.');
588 if (!instance_name) {
589 pr_err("Unable to locate . in FUNC.INSTANCE\n");
590 return ERR_PTR(-EINVAL);
592 *instance_name = '\0';
595 fi = usb_get_function_instance(func_name);
599 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
601 usb_put_function_instance(fi);
604 if (fi->set_inst_name) {
605 ret = fi->set_inst_name(fi, instance_name);
607 usb_put_function_instance(fi);
612 gi = container_of(group, struct gadget_info, functions_group);
614 mutex_lock(&gi->lock);
615 list_add_tail(&fi->cfs_list, &gi->available_func);
616 mutex_unlock(&gi->lock);
620 static void function_drop(
621 struct config_group *group,
622 struct config_item *item)
624 struct usb_function_instance *fi = to_usb_function_instance(item);
625 struct gadget_info *gi;
627 gi = container_of(group, struct gadget_info, functions_group);
629 mutex_lock(&gi->lock);
630 list_del(&fi->cfs_list);
631 mutex_unlock(&gi->lock);
632 config_item_put(item);
635 static struct configfs_group_operations functions_ops = {
636 .make_group = &function_make,
637 .drop_item = &function_drop,
640 static const struct config_item_type functions_type = {
641 .ct_group_ops = &functions_ops,
642 .ct_owner = THIS_MODULE,
645 GS_STRINGS_RW(gadget_config_name, configuration);
647 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
648 &gadget_config_name_attr_configuration,
652 static void gadget_config_name_attr_release(struct config_item *item)
654 struct gadget_config_name *cn = to_gadget_config_name(item);
656 kfree(cn->configuration);
662 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
663 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
665 static struct config_group *config_desc_make(
666 struct config_group *group,
669 struct gadget_info *gi;
670 struct config_usb_cfg *cfg;
671 char buf[MAX_NAME_LEN];
676 gi = container_of(group, struct gadget_info, configs_group);
677 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
678 if (ret >= MAX_NAME_LEN)
679 return ERR_PTR(-ENAMETOOLONG);
681 num_str = strchr(buf, '.');
683 pr_err("Unable to locate . in name.bConfigurationValue\n");
684 return ERR_PTR(-EINVAL);
691 return ERR_PTR(-EINVAL);
693 ret = kstrtou8(num_str, 0, &num);
697 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
699 return ERR_PTR(-ENOMEM);
700 cfg->c.label = kstrdup(buf, GFP_KERNEL);
705 cfg->c.bConfigurationValue = num;
706 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
707 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
708 INIT_LIST_HEAD(&cfg->string_list);
709 INIT_LIST_HEAD(&cfg->func_list);
711 config_group_init_type_name(&cfg->group, name,
712 &gadget_config_type);
714 config_group_init_type_name(&cfg->strings_group, "strings",
715 &gadget_config_name_strings_type);
716 configfs_add_default_group(&cfg->strings_group, &cfg->group);
718 ret = usb_add_config_only(&gi->cdev, &cfg->c);
729 static void config_desc_drop(
730 struct config_group *group,
731 struct config_item *item)
733 config_item_put(item);
736 static struct configfs_group_operations config_desc_ops = {
737 .make_group = &config_desc_make,
738 .drop_item = &config_desc_drop,
741 static const struct config_item_type config_desc_type = {
742 .ct_group_ops = &config_desc_ops,
743 .ct_owner = THIS_MODULE,
746 GS_STRINGS_RW(gadget_strings, manufacturer);
747 GS_STRINGS_RW(gadget_strings, product);
748 GS_STRINGS_RW(gadget_strings, serialnumber);
750 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
751 &gadget_strings_attr_manufacturer,
752 &gadget_strings_attr_product,
753 &gadget_strings_attr_serialnumber,
757 static void gadget_strings_attr_release(struct config_item *item)
759 struct gadget_strings *gs = to_gadget_strings(item);
761 kfree(gs->manufacturer);
763 kfree(gs->serialnumber);
769 USB_CONFIG_STRING_RW_OPS(gadget_strings);
770 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
772 static inline struct os_desc *to_os_desc(struct config_item *item)
774 return container_of(to_config_group(item), struct os_desc, group);
777 static inline struct gadget_info *os_desc_item_to_gadget_info(
778 struct config_item *item)
780 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
783 static ssize_t os_desc_use_show(struct config_item *item, char *page)
785 return sprintf(page, "%d\n",
786 os_desc_item_to_gadget_info(item)->use_os_desc);
789 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
792 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
796 mutex_lock(&gi->lock);
797 ret = strtobool(page, &use);
799 gi->use_os_desc = use;
802 mutex_unlock(&gi->lock);
807 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
809 return sprintf(page, "0x%02x\n",
810 os_desc_item_to_gadget_info(item)->b_vendor_code);
813 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
814 const char *page, size_t len)
816 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
820 mutex_lock(&gi->lock);
821 ret = kstrtou8(page, 0, &b_vendor_code);
823 gi->b_vendor_code = b_vendor_code;
826 mutex_unlock(&gi->lock);
831 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
833 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
836 res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
837 UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
843 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
846 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
849 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
850 if (page[l - 1] == '\n')
853 mutex_lock(&gi->lock);
854 res = utf8s_to_utf16s(page, l,
855 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
856 OS_STRING_QW_SIGN_LEN);
859 mutex_unlock(&gi->lock);
864 CONFIGFS_ATTR(os_desc_, use);
865 CONFIGFS_ATTR(os_desc_, b_vendor_code);
866 CONFIGFS_ATTR(os_desc_, qw_sign);
868 static struct configfs_attribute *os_desc_attrs[] = {
870 &os_desc_attr_b_vendor_code,
871 &os_desc_attr_qw_sign,
875 static void os_desc_attr_release(struct config_item *item)
877 struct os_desc *os_desc = to_os_desc(item);
881 static int os_desc_link(struct config_item *os_desc_ci,
882 struct config_item *usb_cfg_ci)
884 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
885 struct gadget_info, os_desc_group);
886 struct usb_composite_dev *cdev = &gi->cdev;
887 struct config_usb_cfg *c_target =
888 container_of(to_config_group(usb_cfg_ci),
889 struct config_usb_cfg, group);
890 struct usb_configuration *c;
893 mutex_lock(&gi->lock);
894 list_for_each_entry(c, &cdev->configs, list) {
895 if (c == &c_target->c)
898 if (c != &c_target->c) {
903 if (cdev->os_desc_config) {
908 cdev->os_desc_config = &c_target->c;
912 mutex_unlock(&gi->lock);
916 static void os_desc_unlink(struct config_item *os_desc_ci,
917 struct config_item *usb_cfg_ci)
919 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
920 struct gadget_info, os_desc_group);
921 struct usb_composite_dev *cdev = &gi->cdev;
923 mutex_lock(&gi->lock);
924 if (gi->composite.gadget_driver.udc_name)
925 unregister_gadget(gi);
926 cdev->os_desc_config = NULL;
927 WARN_ON(gi->composite.gadget_driver.udc_name);
928 mutex_unlock(&gi->lock);
931 static struct configfs_item_operations os_desc_ops = {
932 .release = os_desc_attr_release,
933 .allow_link = os_desc_link,
934 .drop_link = os_desc_unlink,
937 static struct config_item_type os_desc_type = {
938 .ct_item_ops = &os_desc_ops,
939 .ct_attrs = os_desc_attrs,
940 .ct_owner = THIS_MODULE,
943 static inline struct usb_os_desc_ext_prop
944 *to_usb_os_desc_ext_prop(struct config_item *item)
946 return container_of(item, struct usb_os_desc_ext_prop, item);
949 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
951 return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
954 static ssize_t ext_prop_type_store(struct config_item *item,
955 const char *page, size_t len)
957 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
958 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
962 if (desc->opts_mutex)
963 mutex_lock(desc->opts_mutex);
964 ret = kstrtou8(page, 0, &type);
967 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
972 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
973 ext_prop->type == USB_EXT_PROP_LE32 ||
974 ext_prop->type == USB_EXT_PROP_BE32) &&
975 (type == USB_EXT_PROP_UNICODE ||
976 type == USB_EXT_PROP_UNICODE_ENV ||
977 type == USB_EXT_PROP_UNICODE_LINK))
978 ext_prop->data_len <<= 1;
979 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
980 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
981 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
982 (type == USB_EXT_PROP_BINARY ||
983 type == USB_EXT_PROP_LE32 ||
984 type == USB_EXT_PROP_BE32))
985 ext_prop->data_len >>= 1;
986 ext_prop->type = type;
990 if (desc->opts_mutex)
991 mutex_unlock(desc->opts_mutex);
995 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
997 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
998 int len = ext_prop->data_len;
1000 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1001 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1002 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1004 memcpy(page, ext_prop->data, len);
1009 static ssize_t ext_prop_data_store(struct config_item *item,
1010 const char *page, size_t len)
1012 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1013 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1015 size_t ret_len = len;
1017 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1019 new_data = kmemdup(page, len, GFP_KERNEL);
1023 if (desc->opts_mutex)
1024 mutex_lock(desc->opts_mutex);
1025 kfree(ext_prop->data);
1026 ext_prop->data = new_data;
1027 desc->ext_prop_len -= ext_prop->data_len;
1028 ext_prop->data_len = len;
1029 desc->ext_prop_len += ext_prop->data_len;
1030 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1031 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1032 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1033 desc->ext_prop_len -= ext_prop->data_len;
1034 ext_prop->data_len <<= 1;
1035 ext_prop->data_len += 2;
1036 desc->ext_prop_len += ext_prop->data_len;
1038 if (desc->opts_mutex)
1039 mutex_unlock(desc->opts_mutex);
1043 CONFIGFS_ATTR(ext_prop_, type);
1044 CONFIGFS_ATTR(ext_prop_, data);
1046 static struct configfs_attribute *ext_prop_attrs[] = {
1047 &ext_prop_attr_type,
1048 &ext_prop_attr_data,
1052 static void usb_os_desc_ext_prop_release(struct config_item *item)
1054 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1056 kfree(ext_prop); /* frees a whole chunk */
1059 static struct configfs_item_operations ext_prop_ops = {
1060 .release = usb_os_desc_ext_prop_release,
1063 static struct config_item *ext_prop_make(
1064 struct config_group *group,
1067 struct usb_os_desc_ext_prop *ext_prop;
1068 struct config_item_type *ext_prop_type;
1069 struct usb_os_desc *desc;
1072 vla_group(data_chunk);
1073 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1074 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1076 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1078 return ERR_PTR(-ENOMEM);
1080 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1081 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1083 desc = container_of(group, struct usb_os_desc, group);
1084 ext_prop_type->ct_item_ops = &ext_prop_ops;
1085 ext_prop_type->ct_attrs = ext_prop_attrs;
1086 ext_prop_type->ct_owner = desc->owner;
1088 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1090 ext_prop->name = kstrdup(name, GFP_KERNEL);
1091 if (!ext_prop->name) {
1093 return ERR_PTR(-ENOMEM);
1095 desc->ext_prop_len += 14;
1096 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1097 if (desc->opts_mutex)
1098 mutex_lock(desc->opts_mutex);
1099 desc->ext_prop_len += ext_prop->name_len;
1100 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1101 ++desc->ext_prop_count;
1102 if (desc->opts_mutex)
1103 mutex_unlock(desc->opts_mutex);
1105 return &ext_prop->item;
1108 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1110 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1111 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1113 if (desc->opts_mutex)
1114 mutex_lock(desc->opts_mutex);
1115 list_del(&ext_prop->entry);
1116 --desc->ext_prop_count;
1117 kfree(ext_prop->name);
1118 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1119 if (desc->opts_mutex)
1120 mutex_unlock(desc->opts_mutex);
1121 config_item_put(item);
1124 static struct configfs_group_operations interf_grp_ops = {
1125 .make_item = &ext_prop_make,
1126 .drop_item = &ext_prop_drop,
1129 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1132 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1136 static ssize_t interf_grp_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, page, l);
1149 if (desc->opts_mutex)
1150 mutex_unlock(desc->opts_mutex);
1155 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1158 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1162 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1163 const char *page, size_t len)
1165 struct usb_os_desc *desc = to_usb_os_desc(item);
1168 l = min_t(int, 8, len);
1169 if (page[l - 1] == '\n')
1171 if (desc->opts_mutex)
1172 mutex_lock(desc->opts_mutex);
1173 memcpy(desc->ext_compat_id + 8, page, l);
1175 if (desc->opts_mutex)
1176 mutex_unlock(desc->opts_mutex);
1181 CONFIGFS_ATTR(interf_grp_, compatible_id);
1182 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1184 static struct configfs_attribute *interf_grp_attrs[] = {
1185 &interf_grp_attr_compatible_id,
1186 &interf_grp_attr_sub_compatible_id,
1190 struct config_group *usb_os_desc_prepare_interf_dir(
1191 struct config_group *parent,
1193 struct usb_os_desc **desc,
1195 struct module *owner)
1197 struct config_group *os_desc_group;
1198 struct config_item_type *os_desc_type, *interface_type;
1200 vla_group(data_chunk);
1201 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1202 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1203 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1205 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1207 return ERR_PTR(-ENOMEM);
1209 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1210 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1211 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1213 os_desc_type->ct_owner = owner;
1214 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1215 configfs_add_default_group(os_desc_group, parent);
1217 interface_type->ct_group_ops = &interf_grp_ops;
1218 interface_type->ct_attrs = interf_grp_attrs;
1219 interface_type->ct_owner = owner;
1221 while (n_interf--) {
1222 struct usb_os_desc *d;
1226 config_group_init_type_name(&d->group, "", interface_type);
1227 config_item_set_name(&d->group.cg_item, "interface.%s",
1229 configfs_add_default_group(&d->group, os_desc_group);
1232 return os_desc_group;
1234 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1236 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1242 int composite_dev_prepare(struct usb_composite_driver *composite,
1243 struct usb_composite_dev *dev);
1245 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1246 struct usb_ep *ep0);
1248 static void purge_configs_funcs(struct gadget_info *gi)
1250 struct usb_configuration *c;
1252 list_for_each_entry(c, &gi->cdev.configs, list) {
1253 struct usb_function *f, *tmp;
1254 struct config_usb_cfg *cfg;
1256 cfg = container_of(c, struct config_usb_cfg, c);
1258 list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1260 list_move(&f->list, &cfg->func_list);
1262 dev_dbg(&gi->cdev.gadget->dev,
1263 "unbind function '%s'/%p\n",
1268 c->next_interface_id = 0;
1269 memset(c->interface, 0, sizeof(c->interface));
1270 c->superspeed_plus = 0;
1277 static int configfs_composite_bind(struct usb_gadget *gadget,
1278 struct usb_gadget_driver *gdriver)
1280 struct usb_composite_driver *composite = to_cdriver(gdriver);
1281 struct gadget_info *gi = container_of(composite,
1282 struct gadget_info, composite);
1283 struct usb_composite_dev *cdev = &gi->cdev;
1284 struct usb_configuration *c;
1285 struct usb_string *s;
1289 /* the gi->lock is hold by the caller */
1291 cdev->gadget = gadget;
1292 set_gadget_data(gadget, cdev);
1293 ret = composite_dev_prepare(composite, cdev);
1296 /* and now the gadget bind */
1299 if (list_empty(&gi->cdev.configs)) {
1300 pr_err("Need at least one configuration in %s.\n",
1301 gi->composite.name);
1302 goto err_comp_cleanup;
1306 list_for_each_entry(c, &gi->cdev.configs, list) {
1307 struct config_usb_cfg *cfg;
1309 cfg = container_of(c, struct config_usb_cfg, c);
1310 if (list_empty(&cfg->func_list)) {
1311 pr_err("Config %s/%d of %s needs at least one function.\n",
1312 c->label, c->bConfigurationValue,
1313 gi->composite.name);
1314 goto err_comp_cleanup;
1318 /* init all strings */
1319 if (!list_empty(&gi->string_list)) {
1320 struct gadget_strings *gs;
1323 list_for_each_entry(gs, &gi->string_list, list) {
1325 gi->gstrings[i] = &gs->stringtab_dev;
1326 gs->stringtab_dev.strings = gs->strings;
1327 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1329 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1330 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1333 gi->gstrings[i] = NULL;
1334 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1335 USB_GADGET_FIRST_AVAIL_IDX);
1338 goto err_comp_cleanup;
1341 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1342 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1343 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1346 if (gi->use_os_desc) {
1347 cdev->use_os_string = true;
1348 cdev->b_vendor_code = gi->b_vendor_code;
1349 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1352 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1353 struct usb_descriptor_header *usb_desc;
1355 usb_desc = usb_otg_descriptor_alloc(gadget);
1358 goto err_comp_cleanup;
1360 usb_otg_descriptor_init(gadget, usb_desc);
1361 otg_desc[0] = usb_desc;
1365 /* Go through all configs, attach all functions */
1366 list_for_each_entry(c, &gi->cdev.configs, list) {
1367 struct config_usb_cfg *cfg;
1368 struct usb_function *f;
1369 struct usb_function *tmp;
1370 struct gadget_config_name *cn;
1372 if (gadget_is_otg(gadget))
1373 c->descriptors = otg_desc;
1375 cfg = container_of(c, struct config_usb_cfg, c);
1376 if (!list_empty(&cfg->string_list)) {
1378 list_for_each_entry(cn, &cfg->string_list, list) {
1379 cfg->gstrings[i] = &cn->stringtab_dev;
1380 cn->stringtab_dev.strings = &cn->strings;
1381 cn->strings.s = cn->configuration;
1384 cfg->gstrings[i] = NULL;
1385 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1388 goto err_comp_cleanup;
1390 c->iConfiguration = s[0].id;
1393 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1395 ret = usb_add_function(c, f);
1397 list_add(&f->list, &cfg->func_list);
1398 goto err_purge_funcs;
1401 usb_ep_autoconfig_reset(cdev->gadget);
1403 if (cdev->use_os_string) {
1404 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1406 goto err_purge_funcs;
1409 usb_ep_autoconfig_reset(cdev->gadget);
1413 purge_configs_funcs(gi);
1415 composite_dev_cleanup(cdev);
1419 static void configfs_composite_unbind(struct usb_gadget *gadget)
1421 struct usb_composite_dev *cdev;
1422 struct gadget_info *gi;
1423 unsigned long flags;
1425 /* the gi->lock is hold by the caller */
1427 cdev = get_gadget_data(gadget);
1428 gi = container_of(cdev, struct gadget_info, cdev);
1429 spin_lock_irqsave(&gi->spinlock, flags);
1431 spin_unlock_irqrestore(&gi->spinlock, flags);
1435 purge_configs_funcs(gi);
1436 composite_dev_cleanup(cdev);
1437 usb_ep_autoconfig_reset(cdev->gadget);
1438 spin_lock_irqsave(&gi->spinlock, flags);
1439 cdev->gadget = NULL;
1440 set_gadget_data(gadget, NULL);
1441 spin_unlock_irqrestore(&gi->spinlock, flags);
1444 static int configfs_composite_setup(struct usb_gadget *gadget,
1445 const struct usb_ctrlrequest *ctrl)
1447 struct usb_composite_dev *cdev;
1448 struct gadget_info *gi;
1449 unsigned long flags;
1452 cdev = get_gadget_data(gadget);
1456 gi = container_of(cdev, struct gadget_info, cdev);
1457 spin_lock_irqsave(&gi->spinlock, flags);
1458 cdev = get_gadget_data(gadget);
1459 if (!cdev || gi->unbind) {
1460 spin_unlock_irqrestore(&gi->spinlock, flags);
1464 ret = composite_setup(gadget, ctrl);
1465 spin_unlock_irqrestore(&gi->spinlock, flags);
1469 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1471 struct usb_composite_dev *cdev;
1472 struct gadget_info *gi;
1473 unsigned long flags;
1475 cdev = get_gadget_data(gadget);
1479 gi = container_of(cdev, struct gadget_info, cdev);
1480 spin_lock_irqsave(&gi->spinlock, flags);
1481 cdev = get_gadget_data(gadget);
1482 if (!cdev || gi->unbind) {
1483 spin_unlock_irqrestore(&gi->spinlock, flags);
1487 composite_disconnect(gadget);
1488 spin_unlock_irqrestore(&gi->spinlock, flags);
1491 static void configfs_composite_reset(struct usb_gadget *gadget)
1493 struct usb_composite_dev *cdev;
1494 struct gadget_info *gi;
1495 unsigned long flags;
1497 cdev = get_gadget_data(gadget);
1501 gi = container_of(cdev, struct gadget_info, cdev);
1502 spin_lock_irqsave(&gi->spinlock, flags);
1503 cdev = get_gadget_data(gadget);
1504 if (!cdev || gi->unbind) {
1505 spin_unlock_irqrestore(&gi->spinlock, flags);
1509 composite_reset(gadget);
1510 spin_unlock_irqrestore(&gi->spinlock, flags);
1513 static void configfs_composite_suspend(struct usb_gadget *gadget)
1515 struct usb_composite_dev *cdev;
1516 struct gadget_info *gi;
1517 unsigned long flags;
1519 cdev = get_gadget_data(gadget);
1523 gi = container_of(cdev, struct gadget_info, cdev);
1524 spin_lock_irqsave(&gi->spinlock, flags);
1525 cdev = get_gadget_data(gadget);
1526 if (!cdev || gi->unbind) {
1527 spin_unlock_irqrestore(&gi->spinlock, flags);
1531 composite_suspend(gadget);
1532 spin_unlock_irqrestore(&gi->spinlock, flags);
1535 static void configfs_composite_resume(struct usb_gadget *gadget)
1537 struct usb_composite_dev *cdev;
1538 struct gadget_info *gi;
1539 unsigned long flags;
1541 cdev = get_gadget_data(gadget);
1545 gi = container_of(cdev, struct gadget_info, cdev);
1546 spin_lock_irqsave(&gi->spinlock, flags);
1547 cdev = get_gadget_data(gadget);
1548 if (!cdev || gi->unbind) {
1549 spin_unlock_irqrestore(&gi->spinlock, flags);
1553 composite_resume(gadget);
1554 spin_unlock_irqrestore(&gi->spinlock, flags);
1557 static const struct usb_gadget_driver configfs_driver_template = {
1558 .bind = configfs_composite_bind,
1559 .unbind = configfs_composite_unbind,
1561 .setup = configfs_composite_setup,
1562 .reset = configfs_composite_reset,
1563 .disconnect = configfs_composite_disconnect,
1565 .suspend = configfs_composite_suspend,
1566 .resume = configfs_composite_resume,
1568 .max_speed = USB_SPEED_SUPER_PLUS,
1570 .owner = THIS_MODULE,
1571 .name = "configfs-gadget",
1573 .match_existing_only = 1,
1576 static struct config_group *gadgets_make(
1577 struct config_group *group,
1580 struct gadget_info *gi;
1582 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1584 return ERR_PTR(-ENOMEM);
1586 config_group_init_type_name(&gi->group, name, &gadget_root_type);
1588 config_group_init_type_name(&gi->functions_group, "functions",
1590 configfs_add_default_group(&gi->functions_group, &gi->group);
1592 config_group_init_type_name(&gi->configs_group, "configs",
1594 configfs_add_default_group(&gi->configs_group, &gi->group);
1596 config_group_init_type_name(&gi->strings_group, "strings",
1597 &gadget_strings_strings_type);
1598 configfs_add_default_group(&gi->strings_group, &gi->group);
1600 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1602 configfs_add_default_group(&gi->os_desc_group, &gi->group);
1604 gi->composite.bind = configfs_do_nothing;
1605 gi->composite.unbind = configfs_do_nothing;
1606 gi->composite.suspend = NULL;
1607 gi->composite.resume = NULL;
1608 gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1610 spin_lock_init(&gi->spinlock);
1611 mutex_init(&gi->lock);
1612 INIT_LIST_HEAD(&gi->string_list);
1613 INIT_LIST_HEAD(&gi->available_func);
1615 composite_init_dev(&gi->cdev);
1616 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1617 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1618 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1620 gi->composite.gadget_driver = configfs_driver_template;
1622 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1623 gi->composite.name = gi->composite.gadget_driver.function;
1625 if (!gi->composite.gadget_driver.function)
1631 return ERR_PTR(-ENOMEM);
1634 static void gadgets_drop(struct config_group *group, struct config_item *item)
1636 config_item_put(item);
1639 static struct configfs_group_operations gadgets_ops = {
1640 .make_group = &gadgets_make,
1641 .drop_item = &gadgets_drop,
1644 static const struct config_item_type gadgets_type = {
1645 .ct_group_ops = &gadgets_ops,
1646 .ct_owner = THIS_MODULE,
1649 static struct configfs_subsystem gadget_subsys = {
1652 .ci_namebuf = "usb_gadget",
1653 .ci_type = &gadgets_type,
1656 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1659 void unregister_gadget_item(struct config_item *item)
1661 struct gadget_info *gi = to_gadget_info(item);
1663 mutex_lock(&gi->lock);
1664 unregister_gadget(gi);
1665 mutex_unlock(&gi->lock);
1667 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1669 static int __init gadget_cfs_init(void)
1673 config_group_init(&gadget_subsys.su_group);
1675 ret = configfs_register_subsystem(&gadget_subsys);
1678 module_init(gadget_cfs_init);
1680 static void __exit gadget_cfs_exit(void)
1682 configfs_unregister_subsystem(&gadget_subsys);
1684 module_exit(gadget_cfs_exit);