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>
11 int check_user_usb_string(const char *name,
12 struct usb_gadget_strings *stringtab_dev)
14 unsigned primary_lang;
19 ret = kstrtou16(name, 0, &num);
23 primary_lang = num & 0x3ff;
26 /* simple sanity check for valid langid */
27 switch (primary_lang) {
36 stringtab_dev->language = num;
40 #define MAX_NAME_LEN 40
41 #define MAX_USB_STRING_LANGS 2
44 struct config_group group;
45 struct config_group functions_group;
46 struct config_group configs_group;
47 struct config_group strings_group;
48 struct config_group os_desc_group;
49 struct config_group *default_groups[5];
52 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
53 struct list_head string_list;
54 struct list_head available_func;
58 struct usb_otg_descriptor otg;
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 = 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, 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);
944 static struct configfs_item_operations interf_item_ops = {
945 .show_attribute = usb_os_desc_attr_show,
946 .store_attribute = usb_os_desc_attr_store,
949 static ssize_t rndis_grp_compatible_id_show(struct usb_os_desc *desc,
952 memcpy(page, desc->ext_compat_id, 8);
956 static ssize_t rndis_grp_compatible_id_store(struct usb_os_desc *desc,
957 const char *page, size_t len)
961 l = min_t(int, 8, len);
962 if (page[l - 1] == '\n')
964 if (desc->opts_mutex)
965 mutex_lock(desc->opts_mutex);
966 memcpy(desc->ext_compat_id, page, l);
967 desc->ext_compat_id[l] = '\0';
969 if (desc->opts_mutex)
970 mutex_unlock(desc->opts_mutex);
975 static struct usb_os_desc_attribute rndis_grp_attr_compatible_id =
976 __CONFIGFS_ATTR(compatible_id, S_IRUGO | S_IWUSR,
977 rndis_grp_compatible_id_show,
978 rndis_grp_compatible_id_store);
980 static ssize_t rndis_grp_sub_compatible_id_show(struct usb_os_desc *desc,
983 memcpy(page, desc->ext_compat_id + 8, 8);
987 static ssize_t rndis_grp_sub_compatible_id_store(struct usb_os_desc *desc,
988 const char *page, size_t len)
992 l = min_t(int, 8, len);
993 if (page[l - 1] == '\n')
995 if (desc->opts_mutex)
996 mutex_lock(desc->opts_mutex);
997 memcpy(desc->ext_compat_id + 8, page, l);
998 desc->ext_compat_id[l + 8] = '\0';
1000 if (desc->opts_mutex)
1001 mutex_unlock(desc->opts_mutex);
1006 static struct usb_os_desc_attribute rndis_grp_attr_sub_compatible_id =
1007 __CONFIGFS_ATTR(sub_compatible_id, S_IRUGO | S_IWUSR,
1008 rndis_grp_sub_compatible_id_show,
1009 rndis_grp_sub_compatible_id_store);
1011 static struct configfs_attribute *interf_grp_attrs[] = {
1012 &rndis_grp_attr_compatible_id.attr,
1013 &rndis_grp_attr_sub_compatible_id.attr,
1017 int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1019 struct usb_os_desc **desc,
1020 struct module *owner)
1022 struct config_group **f_default_groups, *os_desc_group,
1024 struct config_item_type *os_desc_type, *interface_type;
1026 vla_group(data_chunk);
1027 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1028 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1029 vla_item(data_chunk, struct config_group *, interface_groups,
1031 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1032 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1034 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1038 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1039 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1040 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1041 interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1042 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1044 parent->default_groups = f_default_groups;
1045 os_desc_type->ct_owner = owner;
1046 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1047 f_default_groups[0] = os_desc_group;
1049 os_desc_group->default_groups = interface_groups;
1050 interface_type->ct_item_ops = &interf_item_ops;
1051 interface_type->ct_attrs = interf_grp_attrs;
1052 interface_type->ct_owner = owner;
1054 while (n_interf--) {
1055 struct usb_os_desc *d;
1058 config_group_init_type_name(&d->group, "", interface_type);
1059 config_item_set_name(&d->group.cg_item, "interface.%d",
1061 interface_groups[n_interf] = &d->group;
1066 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1068 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1074 int composite_dev_prepare(struct usb_composite_driver *composite,
1075 struct usb_composite_dev *dev);
1077 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1078 struct usb_ep *ep0);
1080 static void purge_configs_funcs(struct gadget_info *gi)
1082 struct usb_configuration *c;
1084 list_for_each_entry(c, &gi->cdev.configs, list) {
1085 struct usb_function *f, *tmp;
1086 struct config_usb_cfg *cfg;
1088 cfg = container_of(c, struct config_usb_cfg, c);
1090 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1092 list_move_tail(&f->list, &cfg->func_list);
1094 dev_err(&gi->cdev.gadget->dev, "unbind function"
1095 " '%s'/%p\n", f->name, f);
1099 c->next_interface_id = 0;
1106 static int configfs_composite_bind(struct usb_gadget *gadget,
1107 struct usb_gadget_driver *gdriver)
1109 struct usb_composite_driver *composite = to_cdriver(gdriver);
1110 struct gadget_info *gi = container_of(composite,
1111 struct gadget_info, composite);
1112 struct usb_composite_dev *cdev = &gi->cdev;
1113 struct usb_configuration *c;
1114 struct usb_string *s;
1118 /* the gi->lock is hold by the caller */
1119 cdev->gadget = gadget;
1120 set_gadget_data(gadget, cdev);
1121 ret = composite_dev_prepare(composite, cdev);
1124 /* and now the gadget bind */
1127 if (list_empty(&gi->cdev.configs)) {
1128 pr_err("Need at least one configuration in %s.\n",
1129 gi->composite.name);
1130 goto err_comp_cleanup;
1134 list_for_each_entry(c, &gi->cdev.configs, list) {
1135 struct config_usb_cfg *cfg;
1137 cfg = container_of(c, struct config_usb_cfg, c);
1138 if (list_empty(&cfg->func_list)) {
1139 pr_err("Config %s/%d of %s needs at least one function.\n",
1140 c->label, c->bConfigurationValue,
1141 gi->composite.name);
1142 goto err_comp_cleanup;
1146 /* init all strings */
1147 if (!list_empty(&gi->string_list)) {
1148 struct gadget_strings *gs;
1151 list_for_each_entry(gs, &gi->string_list, list) {
1153 gi->gstrings[i] = &gs->stringtab_dev;
1154 gs->stringtab_dev.strings = gs->strings;
1155 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1157 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1158 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1161 gi->gstrings[i] = NULL;
1162 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1163 USB_GADGET_FIRST_AVAIL_IDX);
1166 goto err_comp_cleanup;
1169 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1170 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1171 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1174 if (gi->use_os_desc) {
1175 cdev->use_os_string = true;
1176 cdev->b_vendor_code = gi->b_vendor_code;
1177 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1180 /* Go through all configs, attach all functions */
1181 list_for_each_entry(c, &gi->cdev.configs, list) {
1182 struct config_usb_cfg *cfg;
1183 struct usb_function *f;
1184 struct usb_function *tmp;
1185 struct gadget_config_name *cn;
1187 cfg = container_of(c, struct config_usb_cfg, c);
1188 if (!list_empty(&cfg->string_list)) {
1190 list_for_each_entry(cn, &cfg->string_list, list) {
1191 cfg->gstrings[i] = &cn->stringtab_dev;
1192 cn->stringtab_dev.strings = &cn->strings;
1193 cn->strings.s = cn->configuration;
1196 cfg->gstrings[i] = NULL;
1197 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1200 goto err_comp_cleanup;
1202 c->iConfiguration = s[0].id;
1205 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1207 ret = usb_add_function(c, f);
1209 list_add(&f->list, &cfg->func_list);
1210 goto err_purge_funcs;
1213 usb_ep_autoconfig_reset(cdev->gadget);
1215 if (cdev->use_os_string) {
1216 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1218 goto err_purge_funcs;
1221 usb_ep_autoconfig_reset(cdev->gadget);
1225 purge_configs_funcs(gi);
1227 composite_dev_cleanup(cdev);
1231 static void configfs_composite_unbind(struct usb_gadget *gadget)
1233 struct usb_composite_dev *cdev;
1234 struct gadget_info *gi;
1236 /* the gi->lock is hold by the caller */
1238 cdev = get_gadget_data(gadget);
1239 gi = container_of(cdev, struct gadget_info, cdev);
1241 purge_configs_funcs(gi);
1242 composite_dev_cleanup(cdev);
1243 usb_ep_autoconfig_reset(cdev->gadget);
1244 cdev->gadget = NULL;
1245 set_gadget_data(gadget, NULL);
1248 static const struct usb_gadget_driver configfs_driver_template = {
1249 .bind = configfs_composite_bind,
1250 .unbind = configfs_composite_unbind,
1252 .setup = composite_setup,
1253 .disconnect = composite_disconnect,
1255 .max_speed = USB_SPEED_SUPER,
1257 .owner = THIS_MODULE,
1258 .name = "configfs-gadget",
1262 static struct config_group *gadgets_make(
1263 struct config_group *group,
1266 struct gadget_info *gi;
1268 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1270 return ERR_PTR(-ENOMEM);
1272 gi->group.default_groups = gi->default_groups;
1273 gi->group.default_groups[0] = &gi->functions_group;
1274 gi->group.default_groups[1] = &gi->configs_group;
1275 gi->group.default_groups[2] = &gi->strings_group;
1276 gi->group.default_groups[3] = &gi->os_desc_group;
1278 config_group_init_type_name(&gi->functions_group, "functions",
1280 config_group_init_type_name(&gi->configs_group, "configs",
1282 config_group_init_type_name(&gi->strings_group, "strings",
1283 &gadget_strings_strings_type);
1284 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1287 gi->composite.bind = configfs_do_nothing;
1288 gi->composite.unbind = configfs_do_nothing;
1289 gi->composite.suspend = NULL;
1290 gi->composite.resume = NULL;
1291 gi->composite.max_speed = USB_SPEED_SUPER;
1293 mutex_init(&gi->lock);
1294 INIT_LIST_HEAD(&gi->string_list);
1295 INIT_LIST_HEAD(&gi->available_func);
1297 composite_init_dev(&gi->cdev);
1298 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1299 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1300 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1302 gi->composite.gadget_driver = configfs_driver_template;
1304 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1305 gi->composite.name = gi->composite.gadget_driver.function;
1307 if (!gi->composite.gadget_driver.function)
1310 #ifdef CONFIG_USB_OTG
1311 gi->otg.bLength = sizeof(struct usb_otg_descriptor);
1312 gi->otg.bDescriptorType = USB_DT_OTG;
1313 gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
1316 config_group_init_type_name(&gi->group, name,
1321 return ERR_PTR(-ENOMEM);
1324 static void gadgets_drop(struct config_group *group, struct config_item *item)
1326 config_item_put(item);
1329 static struct configfs_group_operations gadgets_ops = {
1330 .make_group = &gadgets_make,
1331 .drop_item = &gadgets_drop,
1334 static struct config_item_type gadgets_type = {
1335 .ct_group_ops = &gadgets_ops,
1336 .ct_owner = THIS_MODULE,
1339 static struct configfs_subsystem gadget_subsys = {
1342 .ci_namebuf = "usb_gadget",
1343 .ci_type = &gadgets_type,
1346 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1349 void unregister_gadget_item(struct config_item *item)
1351 struct gadget_info *gi = to_gadget_info(item);
1353 unregister_gadget(gi);
1355 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1357 static int __init gadget_cfs_init(void)
1361 config_group_init(&gadget_subsys.su_group);
1363 ret = configfs_register_subsystem(&gadget_subsys);
1366 module_init(gadget_cfs_init);
1368 static void __exit gadget_cfs_exit(void)
1370 configfs_unregister_subsystem(&gadget_subsys);
1372 module_exit(gadget_cfs_exit);