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;
52 struct config_group *default_groups[5];
55 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
56 struct list_head string_list;
57 struct list_head available_func;
60 struct usb_composite_driver composite;
61 struct usb_composite_dev cdev;
64 char qw_sign[OS_STRING_QW_SIGN_LEN];
67 struct config_usb_cfg {
68 struct config_group group;
69 struct config_group strings_group;
70 struct config_group *default_groups[2];
71 struct list_head string_list;
72 struct usb_configuration c;
73 struct list_head func_list;
74 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
77 struct gadget_strings {
78 struct usb_gadget_strings stringtab_dev;
79 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
84 struct config_group group;
85 struct list_head list;
89 struct config_group group;
92 struct gadget_config_name {
93 struct usb_gadget_strings stringtab_dev;
94 struct usb_string strings;
97 struct config_group group;
98 struct list_head list;
101 static int usb_string_copy(const char *s, char **s_copy)
105 char *copy = *s_copy;
110 str = kstrdup(s, GFP_KERNEL);
113 if (str[ret - 1] == '\n')
120 CONFIGFS_ATTR_STRUCT(gadget_info);
121 CONFIGFS_ATTR_STRUCT(config_usb_cfg);
123 #define GI_DEVICE_DESC_ITEM_ATTR(name) \
124 static struct gadget_info_attribute gadget_cdev_desc_##name = \
125 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
126 gadget_dev_desc_##name##_show, \
127 gadget_dev_desc_##name##_store)
129 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
130 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
133 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
136 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
137 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
140 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
144 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
145 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
146 const char *page, size_t len) \
150 ret = kstrtou8(page, 0, &val); \
153 gi->cdev.desc._name = val; \
157 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
158 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
159 const char *page, size_t len) \
163 ret = kstrtou16(page, 0, &val); \
166 gi->cdev.desc._name = cpu_to_le16p(&val); \
170 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
171 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
172 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
174 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
175 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
176 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
177 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
178 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
179 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
180 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
181 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
183 static ssize_t is_valid_bcd(u16 bcd_val)
185 if ((bcd_val & 0xf) > 9)
187 if (((bcd_val >> 4) & 0xf) > 9)
189 if (((bcd_val >> 8) & 0xf) > 9)
191 if (((bcd_val >> 12) & 0xf) > 9)
196 static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
197 const char *page, size_t len)
202 ret = kstrtou16(page, 0, &bcdDevice);
205 ret = is_valid_bcd(bcdDevice);
209 gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
213 static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
214 const char *page, size_t len)
219 ret = kstrtou16(page, 0, &bcdUSB);
222 ret = is_valid_bcd(bcdUSB);
226 gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
230 static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
232 return sprintf(page, "%s\n", gi->udc_name ?: "");
235 static int unregister_gadget(struct gadget_info *gi)
242 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
250 static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
251 const char *page, size_t len)
256 name = kstrdup(page, GFP_KERNEL);
259 if (name[len - 1] == '\n')
260 name[len - 1] = '\0';
262 mutex_lock(&gi->lock);
265 ret = unregister_gadget(gi);
273 ret = usb_udc_attach_driver(name, &gi->composite.gadget_driver);
278 mutex_unlock(&gi->lock);
282 mutex_unlock(&gi->lock);
286 GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
287 GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
288 GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
289 GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
290 GI_DEVICE_DESC_ITEM_ATTR(idVendor);
291 GI_DEVICE_DESC_ITEM_ATTR(idProduct);
292 GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
293 GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
294 GI_DEVICE_DESC_ITEM_ATTR(UDC);
296 static struct configfs_attribute *gadget_root_attrs[] = {
297 &gadget_cdev_desc_bDeviceClass.attr,
298 &gadget_cdev_desc_bDeviceSubClass.attr,
299 &gadget_cdev_desc_bDeviceProtocol.attr,
300 &gadget_cdev_desc_bMaxPacketSize0.attr,
301 &gadget_cdev_desc_idVendor.attr,
302 &gadget_cdev_desc_idProduct.attr,
303 &gadget_cdev_desc_bcdDevice.attr,
304 &gadget_cdev_desc_bcdUSB.attr,
305 &gadget_cdev_desc_UDC.attr,
309 static inline struct gadget_info *to_gadget_info(struct config_item *item)
311 return container_of(to_config_group(item), struct gadget_info, group);
314 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
316 return container_of(to_config_group(item), struct gadget_strings,
320 static inline struct gadget_config_name *to_gadget_config_name(
321 struct config_item *item)
323 return container_of(to_config_group(item), struct gadget_config_name,
327 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
329 return container_of(to_config_group(item), struct config_usb_cfg,
333 static inline struct usb_function_instance *to_usb_function_instance(
334 struct config_item *item)
336 return container_of(to_config_group(item),
337 struct usb_function_instance, group);
340 static void gadget_info_attr_release(struct config_item *item)
342 struct gadget_info *gi = to_gadget_info(item);
344 WARN_ON(!list_empty(&gi->cdev.configs));
345 WARN_ON(!list_empty(&gi->string_list));
346 WARN_ON(!list_empty(&gi->available_func));
347 kfree(gi->composite.gadget_driver.function);
351 CONFIGFS_ATTR_OPS(gadget_info);
353 static struct configfs_item_operations gadget_root_item_ops = {
354 .release = gadget_info_attr_release,
355 .show_attribute = gadget_info_attr_show,
356 .store_attribute = gadget_info_attr_store,
359 static void gadget_config_attr_release(struct config_item *item)
361 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
363 WARN_ON(!list_empty(&cfg->c.functions));
364 list_del(&cfg->c.list);
369 static int config_usb_cfg_link(
370 struct config_item *usb_cfg_ci,
371 struct config_item *usb_func_ci)
373 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
374 struct usb_composite_dev *cdev = cfg->c.cdev;
375 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
377 struct config_group *group = to_config_group(usb_func_ci);
378 struct usb_function_instance *fi = container_of(group,
379 struct usb_function_instance, group);
380 struct usb_function_instance *a_fi;
381 struct usb_function *f;
384 mutex_lock(&gi->lock);
386 * Make sure this function is from within our _this_ gadget and not
387 * from another gadget or a random directory.
388 * Also a function instance can only be linked once.
390 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
399 list_for_each_entry(f, &cfg->func_list, list) {
406 f = usb_get_function(fi);
412 /* stash the function until we bind it to the gadget */
413 list_add_tail(&f->list, &cfg->func_list);
416 mutex_unlock(&gi->lock);
420 static int config_usb_cfg_unlink(
421 struct config_item *usb_cfg_ci,
422 struct config_item *usb_func_ci)
424 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
425 struct usb_composite_dev *cdev = cfg->c.cdev;
426 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
428 struct config_group *group = to_config_group(usb_func_ci);
429 struct usb_function_instance *fi = container_of(group,
430 struct usb_function_instance, group);
431 struct usb_function *f;
434 * ideally I would like to forbid to unlink functions while a gadget is
435 * bound to an UDC. Since this isn't possible at the moment, we simply
436 * force an unbind, the function is available here and then we can
437 * remove the function.
439 mutex_lock(&gi->lock);
441 unregister_gadget(gi);
442 WARN_ON(gi->udc_name);
444 list_for_each_entry(f, &cfg->func_list, list) {
448 mutex_unlock(&gi->lock);
452 mutex_unlock(&gi->lock);
453 WARN(1, "Unable to locate function to unbind\n");
457 CONFIGFS_ATTR_OPS(config_usb_cfg);
459 static struct configfs_item_operations gadget_config_item_ops = {
460 .release = gadget_config_attr_release,
461 .show_attribute = config_usb_cfg_attr_show,
462 .store_attribute = config_usb_cfg_attr_store,
463 .allow_link = config_usb_cfg_link,
464 .drop_link = config_usb_cfg_unlink,
468 static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
471 return sprintf(page, "%u\n", cfg->c.MaxPower);
474 static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
475 const char *page, size_t len)
479 ret = kstrtou16(page, 0, &val);
482 if (DIV_ROUND_UP(val, 8) > 0xff)
484 cfg->c.MaxPower = val;
488 static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
491 return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
494 static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
495 const char *page, size_t len)
499 ret = kstrtou8(page, 0, &val);
502 if (!(val & USB_CONFIG_ATT_ONE))
504 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
505 USB_CONFIG_ATT_WAKEUP))
507 cfg->c.bmAttributes = val;
511 #define CFG_CONFIG_DESC_ITEM_ATTR(name) \
512 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
513 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
514 gadget_config_desc_##name##_show, \
515 gadget_config_desc_##name##_store)
517 CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
518 CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
520 static struct configfs_attribute *gadget_config_attrs[] = {
521 &gadget_usb_cfg_MaxPower.attr,
522 &gadget_usb_cfg_bmAttributes.attr,
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 CONFIGFS_ATTR_STRUCT(gadget_config_name);
620 GS_STRINGS_RW(gadget_config_name, configuration);
622 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
623 &gadget_config_name_configuration.attr,
627 static void gadget_config_name_attr_release(struct config_item *item)
629 struct gadget_config_name *cn = to_gadget_config_name(item);
631 kfree(cn->configuration);
637 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
638 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
640 static struct config_group *config_desc_make(
641 struct config_group *group,
644 struct gadget_info *gi;
645 struct config_usb_cfg *cfg;
646 char buf[MAX_NAME_LEN];
651 gi = container_of(group, struct gadget_info, configs_group);
652 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
653 if (ret >= MAX_NAME_LEN)
654 return ERR_PTR(-ENAMETOOLONG);
656 num_str = strchr(buf, '.');
658 pr_err("Unable to locate . in name.bConfigurationValue\n");
659 return ERR_PTR(-EINVAL);
666 return ERR_PTR(-EINVAL);
668 ret = kstrtou8(num_str, 0, &num);
672 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
674 return ERR_PTR(-ENOMEM);
675 cfg->c.label = kstrdup(buf, GFP_KERNEL);
680 cfg->c.bConfigurationValue = num;
681 cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
682 cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
683 INIT_LIST_HEAD(&cfg->string_list);
684 INIT_LIST_HEAD(&cfg->func_list);
686 cfg->group.default_groups = cfg->default_groups;
687 cfg->default_groups[0] = &cfg->strings_group;
689 config_group_init_type_name(&cfg->group, name,
690 &gadget_config_type);
691 config_group_init_type_name(&cfg->strings_group, "strings",
692 &gadget_config_name_strings_type);
694 ret = usb_add_config_only(&gi->cdev, &cfg->c);
705 static void config_desc_drop(
706 struct config_group *group,
707 struct config_item *item)
709 config_item_put(item);
712 static struct configfs_group_operations config_desc_ops = {
713 .make_group = &config_desc_make,
714 .drop_item = &config_desc_drop,
717 static struct config_item_type config_desc_type = {
718 .ct_group_ops = &config_desc_ops,
719 .ct_owner = THIS_MODULE,
722 CONFIGFS_ATTR_STRUCT(gadget_strings);
723 GS_STRINGS_RW(gadget_strings, manufacturer);
724 GS_STRINGS_RW(gadget_strings, product);
725 GS_STRINGS_RW(gadget_strings, serialnumber);
727 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
728 &gadget_strings_manufacturer.attr,
729 &gadget_strings_product.attr,
730 &gadget_strings_serialnumber.attr,
734 static void gadget_strings_attr_release(struct config_item *item)
736 struct gadget_strings *gs = to_gadget_strings(item);
738 kfree(gs->manufacturer);
740 kfree(gs->serialnumber);
746 USB_CONFIG_STRING_RW_OPS(gadget_strings);
747 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
749 static inline struct os_desc *to_os_desc(struct config_item *item)
751 return container_of(to_config_group(item), struct os_desc, group);
754 CONFIGFS_ATTR_STRUCT(os_desc);
755 CONFIGFS_ATTR_OPS(os_desc);
757 static ssize_t os_desc_use_show(struct os_desc *os_desc, char *page)
759 struct gadget_info *gi;
761 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
763 return sprintf(page, "%d", gi->use_os_desc);
766 static ssize_t os_desc_use_store(struct os_desc *os_desc, const char *page,
769 struct gadget_info *gi;
773 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
775 mutex_lock(&gi->lock);
776 ret = strtobool(page, &use);
778 gi->use_os_desc = use;
781 mutex_unlock(&gi->lock);
786 static struct os_desc_attribute os_desc_use =
787 __CONFIGFS_ATTR(use, S_IRUGO | S_IWUSR,
791 static ssize_t os_desc_b_vendor_code_show(struct os_desc *os_desc, char *page)
793 struct gadget_info *gi;
795 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
797 return sprintf(page, "%d", gi->b_vendor_code);
800 static ssize_t os_desc_b_vendor_code_store(struct os_desc *os_desc,
801 const char *page, size_t len)
803 struct gadget_info *gi;
807 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
809 mutex_lock(&gi->lock);
810 ret = kstrtou8(page, 0, &b_vendor_code);
812 gi->b_vendor_code = b_vendor_code;
815 mutex_unlock(&gi->lock);
820 static struct os_desc_attribute os_desc_b_vendor_code =
821 __CONFIGFS_ATTR(b_vendor_code, S_IRUGO | S_IWUSR,
822 os_desc_b_vendor_code_show,
823 os_desc_b_vendor_code_store);
825 static ssize_t os_desc_qw_sign_show(struct os_desc *os_desc, char *page)
827 struct gadget_info *gi;
829 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
831 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
833 return OS_STRING_QW_SIGN_LEN;
836 static ssize_t os_desc_qw_sign_store(struct os_desc *os_desc, const char *page,
839 struct gadget_info *gi;
842 gi = to_gadget_info(os_desc->group.cg_item.ci_parent);
843 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
844 if (page[l - 1] == '\n')
847 mutex_lock(&gi->lock);
848 res = utf8s_to_utf16s(page, l,
849 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
850 OS_STRING_QW_SIGN_LEN);
853 mutex_unlock(&gi->lock);
858 static struct os_desc_attribute os_desc_qw_sign =
859 __CONFIGFS_ATTR(qw_sign, S_IRUGO | S_IWUSR,
860 os_desc_qw_sign_show,
861 os_desc_qw_sign_store);
863 static struct configfs_attribute *os_desc_attrs[] = {
865 &os_desc_b_vendor_code.attr,
866 &os_desc_qw_sign.attr,
870 static void os_desc_attr_release(struct config_item *item)
872 struct os_desc *os_desc = to_os_desc(item);
876 static int os_desc_link(struct config_item *os_desc_ci,
877 struct config_item *usb_cfg_ci)
879 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
880 struct gadget_info, os_desc_group);
881 struct usb_composite_dev *cdev = &gi->cdev;
882 struct config_usb_cfg *c_target =
883 container_of(to_config_group(usb_cfg_ci),
884 struct config_usb_cfg, group);
885 struct usb_configuration *c;
888 mutex_lock(&gi->lock);
889 list_for_each_entry(c, &cdev->configs, list) {
890 if (c == &c_target->c)
893 if (c != &c_target->c) {
898 if (cdev->os_desc_config) {
903 cdev->os_desc_config = &c_target->c;
907 mutex_unlock(&gi->lock);
911 static int os_desc_unlink(struct config_item *os_desc_ci,
912 struct config_item *usb_cfg_ci)
914 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
915 struct gadget_info, os_desc_group);
916 struct usb_composite_dev *cdev = &gi->cdev;
918 mutex_lock(&gi->lock);
920 unregister_gadget(gi);
921 cdev->os_desc_config = NULL;
922 WARN_ON(gi->udc_name);
923 mutex_unlock(&gi->lock);
927 static struct configfs_item_operations os_desc_ops = {
928 .release = os_desc_attr_release,
929 .show_attribute = os_desc_attr_show,
930 .store_attribute = os_desc_attr_store,
931 .allow_link = os_desc_link,
932 .drop_link = os_desc_unlink,
935 static struct config_item_type os_desc_type = {
936 .ct_item_ops = &os_desc_ops,
937 .ct_attrs = os_desc_attrs,
938 .ct_owner = THIS_MODULE,
941 CONFIGFS_ATTR_STRUCT(usb_os_desc);
942 CONFIGFS_ATTR_OPS(usb_os_desc);
945 static inline struct usb_os_desc_ext_prop
946 *to_usb_os_desc_ext_prop(struct config_item *item)
948 return container_of(item, struct usb_os_desc_ext_prop, item);
951 CONFIGFS_ATTR_STRUCT(usb_os_desc_ext_prop);
952 CONFIGFS_ATTR_OPS(usb_os_desc_ext_prop);
954 static ssize_t ext_prop_type_show(struct usb_os_desc_ext_prop *ext_prop,
957 return sprintf(page, "%d", ext_prop->type);
960 static ssize_t ext_prop_type_store(struct usb_os_desc_ext_prop *ext_prop,
961 const char *page, size_t len)
963 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
967 if (desc->opts_mutex)
968 mutex_lock(desc->opts_mutex);
969 ret = kstrtou8(page, 0, &type);
972 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
977 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
978 ext_prop->type == USB_EXT_PROP_LE32 ||
979 ext_prop->type == USB_EXT_PROP_BE32) &&
980 (type == USB_EXT_PROP_UNICODE ||
981 type == USB_EXT_PROP_UNICODE_ENV ||
982 type == USB_EXT_PROP_UNICODE_LINK))
983 ext_prop->data_len <<= 1;
984 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
985 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
986 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
987 (type == USB_EXT_PROP_BINARY ||
988 type == USB_EXT_PROP_LE32 ||
989 type == USB_EXT_PROP_BE32))
990 ext_prop->data_len >>= 1;
991 ext_prop->type = type;
995 if (desc->opts_mutex)
996 mutex_unlock(desc->opts_mutex);
1000 static ssize_t ext_prop_data_show(struct usb_os_desc_ext_prop *ext_prop,
1003 int len = ext_prop->data_len;
1005 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1006 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1007 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1009 memcpy(page, ext_prop->data, len);
1014 static ssize_t ext_prop_data_store(struct usb_os_desc_ext_prop *ext_prop,
1015 const char *page, size_t len)
1017 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1019 size_t ret_len = len;
1021 if (page[len - 1] == '\n' || page[len - 1] == '\0')
1023 new_data = kmemdup(page, len, GFP_KERNEL);
1027 if (desc->opts_mutex)
1028 mutex_lock(desc->opts_mutex);
1029 kfree(ext_prop->data);
1030 ext_prop->data = new_data;
1031 desc->ext_prop_len -= ext_prop->data_len;
1032 ext_prop->data_len = len;
1033 desc->ext_prop_len += ext_prop->data_len;
1034 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1035 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1036 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1037 desc->ext_prop_len -= ext_prop->data_len;
1038 ext_prop->data_len <<= 1;
1039 ext_prop->data_len += 2;
1040 desc->ext_prop_len += ext_prop->data_len;
1042 if (desc->opts_mutex)
1043 mutex_unlock(desc->opts_mutex);
1047 static struct usb_os_desc_ext_prop_attribute ext_prop_type =
1048 __CONFIGFS_ATTR(type, S_IRUGO | S_IWUSR,
1049 ext_prop_type_show, ext_prop_type_store);
1051 static struct usb_os_desc_ext_prop_attribute ext_prop_data =
1052 __CONFIGFS_ATTR(data, S_IRUGO | S_IWUSR,
1053 ext_prop_data_show, ext_prop_data_store);
1055 static struct configfs_attribute *ext_prop_attrs[] = {
1056 &ext_prop_type.attr,
1057 &ext_prop_data.attr,
1061 static void usb_os_desc_ext_prop_release(struct config_item *item)
1063 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1065 kfree(ext_prop); /* frees a whole chunk */
1068 static struct configfs_item_operations ext_prop_ops = {
1069 .release = usb_os_desc_ext_prop_release,
1070 .show_attribute = usb_os_desc_ext_prop_attr_show,
1071 .store_attribute = usb_os_desc_ext_prop_attr_store,
1074 static struct config_item *ext_prop_make(
1075 struct config_group *group,
1078 struct usb_os_desc_ext_prop *ext_prop;
1079 struct config_item_type *ext_prop_type;
1080 struct usb_os_desc *desc;
1083 vla_group(data_chunk);
1084 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1085 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1087 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1089 return ERR_PTR(-ENOMEM);
1091 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1092 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1094 desc = container_of(group, struct usb_os_desc, group);
1095 ext_prop_type->ct_item_ops = &ext_prop_ops;
1096 ext_prop_type->ct_attrs = ext_prop_attrs;
1097 ext_prop_type->ct_owner = desc->owner;
1099 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1101 ext_prop->name = kstrdup(name, GFP_KERNEL);
1102 if (!ext_prop->name) {
1104 return ERR_PTR(-ENOMEM);
1106 desc->ext_prop_len += 14;
1107 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1108 if (desc->opts_mutex)
1109 mutex_lock(desc->opts_mutex);
1110 desc->ext_prop_len += ext_prop->name_len;
1111 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1112 ++desc->ext_prop_count;
1113 if (desc->opts_mutex)
1114 mutex_unlock(desc->opts_mutex);
1116 return &ext_prop->item;
1119 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1121 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1122 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1124 if (desc->opts_mutex)
1125 mutex_lock(desc->opts_mutex);
1126 list_del(&ext_prop->entry);
1127 --desc->ext_prop_count;
1128 kfree(ext_prop->name);
1129 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1130 if (desc->opts_mutex)
1131 mutex_unlock(desc->opts_mutex);
1132 config_item_put(item);
1135 static struct configfs_group_operations interf_grp_ops = {
1136 .make_item = &ext_prop_make,
1137 .drop_item = &ext_prop_drop,
1140 static struct configfs_item_operations interf_item_ops = {
1141 .show_attribute = usb_os_desc_attr_show,
1142 .store_attribute = usb_os_desc_attr_store,
1145 static ssize_t interf_grp_compatible_id_show(struct usb_os_desc *desc,
1148 memcpy(page, desc->ext_compat_id, 8);
1152 static ssize_t interf_grp_compatible_id_store(struct usb_os_desc *desc,
1153 const char *page, size_t len)
1157 l = min_t(int, 8, len);
1158 if (page[l - 1] == '\n')
1160 if (desc->opts_mutex)
1161 mutex_lock(desc->opts_mutex);
1162 memcpy(desc->ext_compat_id, page, l);
1164 if (desc->opts_mutex)
1165 mutex_unlock(desc->opts_mutex);
1170 static struct usb_os_desc_attribute interf_grp_attr_compatible_id =
1171 __CONFIGFS_ATTR(compatible_id, S_IRUGO | S_IWUSR,
1172 interf_grp_compatible_id_show,
1173 interf_grp_compatible_id_store);
1175 static ssize_t interf_grp_sub_compatible_id_show(struct usb_os_desc *desc,
1178 memcpy(page, desc->ext_compat_id + 8, 8);
1182 static ssize_t interf_grp_sub_compatible_id_store(struct usb_os_desc *desc,
1183 const char *page, size_t len)
1187 l = min_t(int, 8, len);
1188 if (page[l - 1] == '\n')
1190 if (desc->opts_mutex)
1191 mutex_lock(desc->opts_mutex);
1192 memcpy(desc->ext_compat_id + 8, page, l);
1194 if (desc->opts_mutex)
1195 mutex_unlock(desc->opts_mutex);
1200 static struct usb_os_desc_attribute interf_grp_attr_sub_compatible_id =
1201 __CONFIGFS_ATTR(sub_compatible_id, S_IRUGO | S_IWUSR,
1202 interf_grp_sub_compatible_id_show,
1203 interf_grp_sub_compatible_id_store);
1205 static struct configfs_attribute *interf_grp_attrs[] = {
1206 &interf_grp_attr_compatible_id.attr,
1207 &interf_grp_attr_sub_compatible_id.attr,
1211 int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1213 struct usb_os_desc **desc,
1215 struct module *owner)
1217 struct config_group **f_default_groups, *os_desc_group,
1219 struct config_item_type *os_desc_type, *interface_type;
1221 vla_group(data_chunk);
1222 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1223 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1224 vla_item(data_chunk, struct config_group *, interface_groups,
1226 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1227 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1229 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1233 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1234 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1235 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1236 interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1237 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1239 parent->default_groups = f_default_groups;
1240 os_desc_type->ct_owner = owner;
1241 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1242 f_default_groups[0] = os_desc_group;
1244 os_desc_group->default_groups = interface_groups;
1245 interface_type->ct_item_ops = &interf_item_ops;
1246 interface_type->ct_group_ops = &interf_grp_ops;
1247 interface_type->ct_attrs = interf_grp_attrs;
1248 interface_type->ct_owner = owner;
1250 while (n_interf--) {
1251 struct usb_os_desc *d;
1255 config_group_init_type_name(&d->group, "", interface_type);
1256 config_item_set_name(&d->group.cg_item, "interface.%s",
1258 interface_groups[n_interf] = &d->group;
1263 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1265 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1271 int composite_dev_prepare(struct usb_composite_driver *composite,
1272 struct usb_composite_dev *dev);
1274 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1275 struct usb_ep *ep0);
1277 static void purge_configs_funcs(struct gadget_info *gi)
1279 struct usb_configuration *c;
1281 list_for_each_entry(c, &gi->cdev.configs, list) {
1282 struct usb_function *f, *tmp;
1283 struct config_usb_cfg *cfg;
1285 cfg = container_of(c, struct config_usb_cfg, c);
1287 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1289 list_move_tail(&f->list, &cfg->func_list);
1291 dev_err(&gi->cdev.gadget->dev, "unbind function"
1292 " '%s'/%p\n", f->name, f);
1296 c->next_interface_id = 0;
1297 memset(c->interface, 0, sizeof(c->interface));
1304 static int configfs_composite_bind(struct usb_gadget *gadget,
1305 struct usb_gadget_driver *gdriver)
1307 struct usb_composite_driver *composite = to_cdriver(gdriver);
1308 struct gadget_info *gi = container_of(composite,
1309 struct gadget_info, composite);
1310 struct usb_composite_dev *cdev = &gi->cdev;
1311 struct usb_configuration *c;
1312 struct usb_string *s;
1316 /* the gi->lock is hold by the caller */
1317 cdev->gadget = gadget;
1318 set_gadget_data(gadget, cdev);
1319 ret = composite_dev_prepare(composite, cdev);
1322 /* and now the gadget bind */
1325 if (list_empty(&gi->cdev.configs)) {
1326 pr_err("Need at least one configuration in %s.\n",
1327 gi->composite.name);
1328 goto err_comp_cleanup;
1332 list_for_each_entry(c, &gi->cdev.configs, list) {
1333 struct config_usb_cfg *cfg;
1335 cfg = container_of(c, struct config_usb_cfg, c);
1336 if (list_empty(&cfg->func_list)) {
1337 pr_err("Config %s/%d of %s needs at least one function.\n",
1338 c->label, c->bConfigurationValue,
1339 gi->composite.name);
1340 goto err_comp_cleanup;
1344 /* init all strings */
1345 if (!list_empty(&gi->string_list)) {
1346 struct gadget_strings *gs;
1349 list_for_each_entry(gs, &gi->string_list, list) {
1351 gi->gstrings[i] = &gs->stringtab_dev;
1352 gs->stringtab_dev.strings = gs->strings;
1353 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1355 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1356 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1359 gi->gstrings[i] = NULL;
1360 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1361 USB_GADGET_FIRST_AVAIL_IDX);
1364 goto err_comp_cleanup;
1367 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1368 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1369 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1372 if (gi->use_os_desc) {
1373 cdev->use_os_string = true;
1374 cdev->b_vendor_code = gi->b_vendor_code;
1375 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1378 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1379 struct usb_descriptor_header *usb_desc;
1381 usb_desc = usb_otg_descriptor_alloc(gadget);
1384 goto err_comp_cleanup;
1386 usb_otg_descriptor_init(gadget, usb_desc);
1387 otg_desc[0] = usb_desc;
1391 /* Go through all configs, attach all functions */
1392 list_for_each_entry(c, &gi->cdev.configs, list) {
1393 struct config_usb_cfg *cfg;
1394 struct usb_function *f;
1395 struct usb_function *tmp;
1396 struct gadget_config_name *cn;
1398 if (gadget_is_otg(gadget))
1399 c->descriptors = otg_desc;
1401 cfg = container_of(c, struct config_usb_cfg, c);
1402 if (!list_empty(&cfg->string_list)) {
1404 list_for_each_entry(cn, &cfg->string_list, list) {
1405 cfg->gstrings[i] = &cn->stringtab_dev;
1406 cn->stringtab_dev.strings = &cn->strings;
1407 cn->strings.s = cn->configuration;
1410 cfg->gstrings[i] = NULL;
1411 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1414 goto err_comp_cleanup;
1416 c->iConfiguration = s[0].id;
1419 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1421 ret = usb_add_function(c, f);
1423 list_add(&f->list, &cfg->func_list);
1424 goto err_purge_funcs;
1427 usb_ep_autoconfig_reset(cdev->gadget);
1429 if (cdev->use_os_string) {
1430 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1432 goto err_purge_funcs;
1435 usb_ep_autoconfig_reset(cdev->gadget);
1439 purge_configs_funcs(gi);
1441 composite_dev_cleanup(cdev);
1445 static void configfs_composite_unbind(struct usb_gadget *gadget)
1447 struct usb_composite_dev *cdev;
1448 struct gadget_info *gi;
1450 /* the gi->lock is hold by the caller */
1452 cdev = get_gadget_data(gadget);
1453 gi = container_of(cdev, struct gadget_info, cdev);
1457 purge_configs_funcs(gi);
1458 composite_dev_cleanup(cdev);
1459 usb_ep_autoconfig_reset(cdev->gadget);
1460 cdev->gadget = NULL;
1461 set_gadget_data(gadget, NULL);
1464 static const struct usb_gadget_driver configfs_driver_template = {
1465 .bind = configfs_composite_bind,
1466 .unbind = configfs_composite_unbind,
1468 .setup = composite_setup,
1469 .reset = composite_disconnect,
1470 .disconnect = composite_disconnect,
1472 .suspend = composite_suspend,
1473 .resume = composite_resume,
1475 .max_speed = USB_SPEED_SUPER,
1477 .owner = THIS_MODULE,
1478 .name = "configfs-gadget",
1482 static struct config_group *gadgets_make(
1483 struct config_group *group,
1486 struct gadget_info *gi;
1488 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1490 return ERR_PTR(-ENOMEM);
1492 gi->group.default_groups = gi->default_groups;
1493 gi->group.default_groups[0] = &gi->functions_group;
1494 gi->group.default_groups[1] = &gi->configs_group;
1495 gi->group.default_groups[2] = &gi->strings_group;
1496 gi->group.default_groups[3] = &gi->os_desc_group;
1498 config_group_init_type_name(&gi->functions_group, "functions",
1500 config_group_init_type_name(&gi->configs_group, "configs",
1502 config_group_init_type_name(&gi->strings_group, "strings",
1503 &gadget_strings_strings_type);
1504 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1507 gi->composite.bind = configfs_do_nothing;
1508 gi->composite.unbind = configfs_do_nothing;
1509 gi->composite.suspend = NULL;
1510 gi->composite.resume = NULL;
1511 gi->composite.max_speed = USB_SPEED_SUPER;
1513 mutex_init(&gi->lock);
1514 INIT_LIST_HEAD(&gi->string_list);
1515 INIT_LIST_HEAD(&gi->available_func);
1517 composite_init_dev(&gi->cdev);
1518 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1519 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1520 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1522 gi->composite.gadget_driver = configfs_driver_template;
1524 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1525 gi->composite.name = gi->composite.gadget_driver.function;
1527 if (!gi->composite.gadget_driver.function)
1530 config_group_init_type_name(&gi->group, name,
1535 return ERR_PTR(-ENOMEM);
1538 static void gadgets_drop(struct config_group *group, struct config_item *item)
1540 config_item_put(item);
1543 static struct configfs_group_operations gadgets_ops = {
1544 .make_group = &gadgets_make,
1545 .drop_item = &gadgets_drop,
1548 static struct config_item_type gadgets_type = {
1549 .ct_group_ops = &gadgets_ops,
1550 .ct_owner = THIS_MODULE,
1553 static struct configfs_subsystem gadget_subsys = {
1556 .ci_namebuf = "usb_gadget",
1557 .ci_type = &gadgets_type,
1560 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1563 void unregister_gadget_item(struct config_item *item)
1565 struct gadget_info *gi = to_gadget_info(item);
1567 unregister_gadget(gi);
1569 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1571 static int __init gadget_cfs_init(void)
1575 config_group_init(&gadget_subsys.su_group);
1577 ret = configfs_register_subsystem(&gadget_subsys);
1580 module_init(gadget_cfs_init);
1582 static void __exit gadget_cfs_exit(void)
1584 configfs_unregister_subsystem(&gadget_subsys);
1586 module_exit(gadget_cfs_exit);