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 #ifdef CONFIG_USB_CONFIGFS_UEVENT
13 #include <linux/platform_device.h>
14 #include <linux/kdev_t.h>
15 #include <linux/usb/ch9.h>
18 #ifdef CONFIG_USB_CONFIGFS_F_ACC
19 extern int acc_ctrlrequest(struct usb_composite_dev *cdev,
20 const struct usb_ctrlrequest *ctrl);
21 void acc_disconnect(void);
23 static struct class *android_class;
26 int check_user_usb_string(const char *name,
27 struct usb_gadget_strings *stringtab_dev)
29 unsigned primary_lang;
34 ret = kstrtou16(name, 0, &num);
38 primary_lang = num & 0x3ff;
41 /* simple sanity check for valid langid */
42 switch (primary_lang) {
51 stringtab_dev->language = num;
55 #define MAX_NAME_LEN 40
56 #define MAX_USB_STRING_LANGS 2
58 static const struct usb_descriptor_header *otg_desc[2];
61 struct config_group group;
62 struct config_group functions_group;
63 struct config_group configs_group;
64 struct config_group strings_group;
65 struct config_group os_desc_group;
66 struct config_group *default_groups[5];
69 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
70 struct list_head string_list;
71 struct list_head available_func;
74 struct usb_composite_driver composite;
75 struct usb_composite_dev cdev;
78 char qw_sign[OS_STRING_QW_SIGN_LEN];
79 #ifdef CONFIG_USB_CONFIGFS_UEVENT
82 struct work_struct work;
87 static inline struct gadget_info *to_gadget_info(struct config_item *item)
89 return container_of(to_config_group(item), struct gadget_info, group);
92 struct config_usb_cfg {
93 struct config_group group;
94 struct config_group strings_group;
95 struct config_group *default_groups[2];
96 struct list_head string_list;
97 struct usb_configuration c;
98 struct list_head func_list;
99 struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
102 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
104 return container_of(to_config_group(item), struct config_usb_cfg,
108 struct gadget_strings {
109 struct usb_gadget_strings stringtab_dev;
110 struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
115 struct config_group group;
116 struct list_head list;
120 struct config_group group;
123 struct gadget_config_name {
124 struct usb_gadget_strings stringtab_dev;
125 struct usb_string strings;
128 struct config_group group;
129 struct list_head list;
132 static int usb_string_copy(const char *s, char **s_copy)
136 char *copy = *s_copy;
141 str = kstrdup(s, GFP_KERNEL);
144 if (str[ret - 1] == '\n')
151 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
152 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
155 return sprintf(page, "0x%02x\n", \
156 to_gadget_info(item)->cdev.desc.__name); \
159 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
160 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
163 return sprintf(page, "0x%04x\n", \
164 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
168 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
169 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
170 const char *page, size_t len) \
174 ret = kstrtou8(page, 0, &val); \
177 to_gadget_info(item)->cdev.desc._name = val; \
181 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
182 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
183 const char *page, size_t len) \
187 ret = kstrtou16(page, 0, &val); \
190 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
194 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
195 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
196 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
198 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
199 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
200 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
201 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
202 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
203 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
204 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
205 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
207 static ssize_t is_valid_bcd(u16 bcd_val)
209 if ((bcd_val & 0xf) > 9)
211 if (((bcd_val >> 4) & 0xf) > 9)
213 if (((bcd_val >> 8) & 0xf) > 9)
215 if (((bcd_val >> 12) & 0xf) > 9)
220 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
221 const char *page, size_t len)
226 ret = kstrtou16(page, 0, &bcdDevice);
229 ret = is_valid_bcd(bcdDevice);
233 to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
237 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
238 const char *page, size_t len)
243 ret = kstrtou16(page, 0, &bcdUSB);
246 ret = is_valid_bcd(bcdUSB);
250 to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
254 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
256 return sprintf(page, "%s\n", to_gadget_info(item)->udc_name ?: "");
259 static int unregister_gadget(struct gadget_info *gi)
266 ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
274 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
275 const char *page, size_t len)
277 struct gadget_info *gi = to_gadget_info(item);
281 name = kstrdup(page, GFP_KERNEL);
284 if (name[len - 1] == '\n')
285 name[len - 1] = '\0';
287 mutex_lock(&gi->lock);
289 if (!strlen(name) || strcmp(name, "none") == 0) {
290 ret = unregister_gadget(gi);
298 ret = usb_udc_attach_driver(name, &gi->composite.gadget_driver);
303 mutex_unlock(&gi->lock);
307 mutex_unlock(&gi->lock);
311 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
312 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
313 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
314 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
315 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
316 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
317 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
318 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
319 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
321 static struct configfs_attribute *gadget_root_attrs[] = {
322 &gadget_dev_desc_attr_bDeviceClass,
323 &gadget_dev_desc_attr_bDeviceSubClass,
324 &gadget_dev_desc_attr_bDeviceProtocol,
325 &gadget_dev_desc_attr_bMaxPacketSize0,
326 &gadget_dev_desc_attr_idVendor,
327 &gadget_dev_desc_attr_idProduct,
328 &gadget_dev_desc_attr_bcdDevice,
329 &gadget_dev_desc_attr_bcdUSB,
330 &gadget_dev_desc_attr_UDC,
334 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
336 return container_of(to_config_group(item), struct gadget_strings,
340 static inline struct gadget_config_name *to_gadget_config_name(
341 struct config_item *item)
343 return container_of(to_config_group(item), struct gadget_config_name,
347 static inline struct usb_function_instance *to_usb_function_instance(
348 struct config_item *item)
350 return container_of(to_config_group(item),
351 struct usb_function_instance, group);
354 static void gadget_info_attr_release(struct config_item *item)
356 struct gadget_info *gi = to_gadget_info(item);
358 WARN_ON(!list_empty(&gi->cdev.configs));
359 WARN_ON(!list_empty(&gi->string_list));
360 WARN_ON(!list_empty(&gi->available_func));
361 kfree(gi->composite.gadget_driver.function);
365 static struct configfs_item_operations gadget_root_item_ops = {
366 .release = gadget_info_attr_release,
369 static void gadget_config_attr_release(struct config_item *item)
371 struct config_usb_cfg *cfg = to_config_usb_cfg(item);
373 WARN_ON(!list_empty(&cfg->c.functions));
374 list_del(&cfg->c.list);
379 static int config_usb_cfg_link(
380 struct config_item *usb_cfg_ci,
381 struct config_item *usb_func_ci)
383 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
384 struct usb_composite_dev *cdev = cfg->c.cdev;
385 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
387 struct config_group *group = to_config_group(usb_func_ci);
388 struct usb_function_instance *fi = container_of(group,
389 struct usb_function_instance, group);
390 struct usb_function_instance *a_fi;
391 struct usb_function *f;
394 mutex_lock(&gi->lock);
396 * Make sure this function is from within our _this_ gadget and not
397 * from another gadget or a random directory.
398 * Also a function instance can only be linked once.
400 list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
409 list_for_each_entry(f, &cfg->func_list, list) {
416 f = usb_get_function(fi);
422 /* stash the function until we bind it to the gadget */
423 list_add_tail(&f->list, &cfg->func_list);
426 mutex_unlock(&gi->lock);
430 static int config_usb_cfg_unlink(
431 struct config_item *usb_cfg_ci,
432 struct config_item *usb_func_ci)
434 struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
435 struct usb_composite_dev *cdev = cfg->c.cdev;
436 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
438 struct config_group *group = to_config_group(usb_func_ci);
439 struct usb_function_instance *fi = container_of(group,
440 struct usb_function_instance, group);
441 struct usb_function *f;
444 * ideally I would like to forbid to unlink functions while a gadget is
445 * bound to an UDC. Since this isn't possible at the moment, we simply
446 * force an unbind, the function is available here and then we can
447 * remove the function.
449 mutex_lock(&gi->lock);
451 unregister_gadget(gi);
452 WARN_ON(gi->udc_name);
454 list_for_each_entry(f, &cfg->func_list, list) {
458 mutex_unlock(&gi->lock);
462 mutex_unlock(&gi->lock);
463 WARN(1, "Unable to locate function to unbind\n");
467 static struct configfs_item_operations gadget_config_item_ops = {
468 .release = gadget_config_attr_release,
469 .allow_link = config_usb_cfg_link,
470 .drop_link = config_usb_cfg_unlink,
474 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
477 return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
480 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
481 const char *page, size_t len)
485 ret = kstrtou16(page, 0, &val);
488 if (DIV_ROUND_UP(val, 8) > 0xff)
490 to_config_usb_cfg(item)->c.MaxPower = val;
494 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
497 return sprintf(page, "0x%02x\n",
498 to_config_usb_cfg(item)->c.bmAttributes);
501 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
502 const char *page, size_t len)
506 ret = kstrtou8(page, 0, &val);
509 if (!(val & USB_CONFIG_ATT_ONE))
511 if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
512 USB_CONFIG_ATT_WAKEUP))
514 to_config_usb_cfg(item)->c.bmAttributes = val;
518 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
519 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
521 static struct configfs_attribute *gadget_config_attrs[] = {
522 &gadget_config_desc_attr_MaxPower,
523 &gadget_config_desc_attr_bmAttributes,
527 static struct config_item_type gadget_config_type = {
528 .ct_item_ops = &gadget_config_item_ops,
529 .ct_attrs = gadget_config_attrs,
530 .ct_owner = THIS_MODULE,
533 static struct config_item_type gadget_root_type = {
534 .ct_item_ops = &gadget_root_item_ops,
535 .ct_attrs = gadget_root_attrs,
536 .ct_owner = THIS_MODULE,
539 static void composite_init_dev(struct usb_composite_dev *cdev)
541 spin_lock_init(&cdev->lock);
542 INIT_LIST_HEAD(&cdev->configs);
543 INIT_LIST_HEAD(&cdev->gstrings);
546 static struct config_group *function_make(
547 struct config_group *group,
550 struct gadget_info *gi;
551 struct usb_function_instance *fi;
552 char buf[MAX_NAME_LEN];
557 ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
558 if (ret >= MAX_NAME_LEN)
559 return ERR_PTR(-ENAMETOOLONG);
562 instance_name = strchr(func_name, '.');
563 if (!instance_name) {
564 pr_err("Unable to locate . in FUNC.INSTANCE\n");
565 return ERR_PTR(-EINVAL);
567 *instance_name = '\0';
570 fi = usb_get_function_instance(func_name);
574 ret = config_item_set_name(&fi->group.cg_item, "%s", name);
576 usb_put_function_instance(fi);
579 if (fi->set_inst_name) {
580 ret = fi->set_inst_name(fi, instance_name);
582 usb_put_function_instance(fi);
587 gi = container_of(group, struct gadget_info, functions_group);
589 mutex_lock(&gi->lock);
590 list_add_tail(&fi->cfs_list, &gi->available_func);
591 mutex_unlock(&gi->lock);
595 static void function_drop(
596 struct config_group *group,
597 struct config_item *item)
599 struct usb_function_instance *fi = to_usb_function_instance(item);
600 struct gadget_info *gi;
602 gi = container_of(group, struct gadget_info, functions_group);
604 mutex_lock(&gi->lock);
605 list_del(&fi->cfs_list);
606 mutex_unlock(&gi->lock);
607 config_item_put(item);
610 static struct configfs_group_operations functions_ops = {
611 .make_group = &function_make,
612 .drop_item = &function_drop,
615 static struct config_item_type functions_type = {
616 .ct_group_ops = &functions_ops,
617 .ct_owner = THIS_MODULE,
620 GS_STRINGS_RW(gadget_config_name, configuration);
622 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
623 &gadget_config_name_attr_configuration,
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 GS_STRINGS_RW(gadget_strings, manufacturer);
723 GS_STRINGS_RW(gadget_strings, product);
724 GS_STRINGS_RW(gadget_strings, serialnumber);
726 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
727 &gadget_strings_attr_manufacturer,
728 &gadget_strings_attr_product,
729 &gadget_strings_attr_serialnumber,
733 static void gadget_strings_attr_release(struct config_item *item)
735 struct gadget_strings *gs = to_gadget_strings(item);
737 kfree(gs->manufacturer);
739 kfree(gs->serialnumber);
745 USB_CONFIG_STRING_RW_OPS(gadget_strings);
746 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
748 static inline struct os_desc *to_os_desc(struct config_item *item)
750 return container_of(to_config_group(item), struct os_desc, group);
753 static inline struct gadget_info *os_desc_item_to_gadget_info(
754 struct config_item *item)
756 return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
759 static ssize_t os_desc_use_show(struct config_item *item, char *page)
761 return sprintf(page, "%d",
762 os_desc_item_to_gadget_info(item)->use_os_desc);
765 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
768 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
772 mutex_lock(&gi->lock);
773 ret = strtobool(page, &use);
775 gi->use_os_desc = use;
778 mutex_unlock(&gi->lock);
783 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
785 return sprintf(page, "%d",
786 os_desc_item_to_gadget_info(item)->b_vendor_code);
789 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
790 const char *page, size_t len)
792 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
796 mutex_lock(&gi->lock);
797 ret = kstrtou8(page, 0, &b_vendor_code);
799 gi->b_vendor_code = b_vendor_code;
802 mutex_unlock(&gi->lock);
807 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
809 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
811 memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
812 return OS_STRING_QW_SIGN_LEN;
815 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
818 struct gadget_info *gi = os_desc_item_to_gadget_info(item);
821 l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
822 if (page[l - 1] == '\n')
825 mutex_lock(&gi->lock);
826 res = utf8s_to_utf16s(page, l,
827 UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
828 OS_STRING_QW_SIGN_LEN);
831 mutex_unlock(&gi->lock);
836 CONFIGFS_ATTR(os_desc_, use);
837 CONFIGFS_ATTR(os_desc_, b_vendor_code);
838 CONFIGFS_ATTR(os_desc_, qw_sign);
840 static struct configfs_attribute *os_desc_attrs[] = {
842 &os_desc_attr_b_vendor_code,
843 &os_desc_attr_qw_sign,
847 static void os_desc_attr_release(struct config_item *item)
849 struct os_desc *os_desc = to_os_desc(item);
853 static int os_desc_link(struct config_item *os_desc_ci,
854 struct config_item *usb_cfg_ci)
856 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
857 struct gadget_info, os_desc_group);
858 struct usb_composite_dev *cdev = &gi->cdev;
859 struct config_usb_cfg *c_target =
860 container_of(to_config_group(usb_cfg_ci),
861 struct config_usb_cfg, group);
862 struct usb_configuration *c;
865 mutex_lock(&gi->lock);
866 list_for_each_entry(c, &cdev->configs, list) {
867 if (c == &c_target->c)
870 if (c != &c_target->c) {
875 if (cdev->os_desc_config) {
880 cdev->os_desc_config = &c_target->c;
884 mutex_unlock(&gi->lock);
888 static int os_desc_unlink(struct config_item *os_desc_ci,
889 struct config_item *usb_cfg_ci)
891 struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
892 struct gadget_info, os_desc_group);
893 struct usb_composite_dev *cdev = &gi->cdev;
895 mutex_lock(&gi->lock);
897 unregister_gadget(gi);
898 cdev->os_desc_config = NULL;
899 WARN_ON(gi->udc_name);
900 mutex_unlock(&gi->lock);
904 static struct configfs_item_operations os_desc_ops = {
905 .release = os_desc_attr_release,
906 .allow_link = os_desc_link,
907 .drop_link = os_desc_unlink,
910 static struct config_item_type os_desc_type = {
911 .ct_item_ops = &os_desc_ops,
912 .ct_attrs = os_desc_attrs,
913 .ct_owner = THIS_MODULE,
916 static inline struct usb_os_desc_ext_prop
917 *to_usb_os_desc_ext_prop(struct config_item *item)
919 return container_of(item, struct usb_os_desc_ext_prop, item);
922 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
924 return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
927 static ssize_t ext_prop_type_store(struct config_item *item,
928 const char *page, size_t len)
930 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
931 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
935 if (desc->opts_mutex)
936 mutex_lock(desc->opts_mutex);
937 ret = kstrtou8(page, 0, &type);
940 if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
945 if ((ext_prop->type == USB_EXT_PROP_BINARY ||
946 ext_prop->type == USB_EXT_PROP_LE32 ||
947 ext_prop->type == USB_EXT_PROP_BE32) &&
948 (type == USB_EXT_PROP_UNICODE ||
949 type == USB_EXT_PROP_UNICODE_ENV ||
950 type == USB_EXT_PROP_UNICODE_LINK))
951 ext_prop->data_len <<= 1;
952 else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
953 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
954 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
955 (type == USB_EXT_PROP_BINARY ||
956 type == USB_EXT_PROP_LE32 ||
957 type == USB_EXT_PROP_BE32))
958 ext_prop->data_len >>= 1;
959 ext_prop->type = type;
963 if (desc->opts_mutex)
964 mutex_unlock(desc->opts_mutex);
968 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
970 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
971 int len = ext_prop->data_len;
973 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
974 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
975 ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
977 memcpy(page, ext_prop->data, len);
982 static ssize_t ext_prop_data_store(struct config_item *item,
983 const char *page, size_t len)
985 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
986 struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
988 size_t ret_len = len;
990 if (page[len - 1] == '\n' || page[len - 1] == '\0')
992 new_data = kmemdup(page, len, GFP_KERNEL);
996 if (desc->opts_mutex)
997 mutex_lock(desc->opts_mutex);
998 kfree(ext_prop->data);
999 ext_prop->data = new_data;
1000 desc->ext_prop_len -= ext_prop->data_len;
1001 ext_prop->data_len = len;
1002 desc->ext_prop_len += ext_prop->data_len;
1003 if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1004 ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1005 ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1006 desc->ext_prop_len -= ext_prop->data_len;
1007 ext_prop->data_len <<= 1;
1008 ext_prop->data_len += 2;
1009 desc->ext_prop_len += ext_prop->data_len;
1011 if (desc->opts_mutex)
1012 mutex_unlock(desc->opts_mutex);
1016 CONFIGFS_ATTR(ext_prop_, type);
1017 CONFIGFS_ATTR(ext_prop_, data);
1019 static struct configfs_attribute *ext_prop_attrs[] = {
1020 &ext_prop_attr_type,
1021 &ext_prop_attr_data,
1025 static void usb_os_desc_ext_prop_release(struct config_item *item)
1027 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1029 kfree(ext_prop); /* frees a whole chunk */
1032 static struct configfs_item_operations ext_prop_ops = {
1033 .release = usb_os_desc_ext_prop_release,
1036 static struct config_item *ext_prop_make(
1037 struct config_group *group,
1040 struct usb_os_desc_ext_prop *ext_prop;
1041 struct config_item_type *ext_prop_type;
1042 struct usb_os_desc *desc;
1045 vla_group(data_chunk);
1046 vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1047 vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1049 vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1051 return ERR_PTR(-ENOMEM);
1053 ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1054 ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1056 desc = container_of(group, struct usb_os_desc, group);
1057 ext_prop_type->ct_item_ops = &ext_prop_ops;
1058 ext_prop_type->ct_attrs = ext_prop_attrs;
1059 ext_prop_type->ct_owner = desc->owner;
1061 config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1063 ext_prop->name = kstrdup(name, GFP_KERNEL);
1064 if (!ext_prop->name) {
1066 return ERR_PTR(-ENOMEM);
1068 desc->ext_prop_len += 14;
1069 ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1070 if (desc->opts_mutex)
1071 mutex_lock(desc->opts_mutex);
1072 desc->ext_prop_len += ext_prop->name_len;
1073 list_add_tail(&ext_prop->entry, &desc->ext_prop);
1074 ++desc->ext_prop_count;
1075 if (desc->opts_mutex)
1076 mutex_unlock(desc->opts_mutex);
1078 return &ext_prop->item;
1081 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1083 struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1084 struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1086 if (desc->opts_mutex)
1087 mutex_lock(desc->opts_mutex);
1088 list_del(&ext_prop->entry);
1089 --desc->ext_prop_count;
1090 kfree(ext_prop->name);
1091 desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1092 if (desc->opts_mutex)
1093 mutex_unlock(desc->opts_mutex);
1094 config_item_put(item);
1097 static struct configfs_group_operations interf_grp_ops = {
1098 .make_item = &ext_prop_make,
1099 .drop_item = &ext_prop_drop,
1102 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1105 memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1109 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1110 const char *page, size_t len)
1112 struct usb_os_desc *desc = to_usb_os_desc(item);
1115 l = min_t(int, 8, len);
1116 if (page[l - 1] == '\n')
1118 if (desc->opts_mutex)
1119 mutex_lock(desc->opts_mutex);
1120 memcpy(desc->ext_compat_id, page, l);
1122 if (desc->opts_mutex)
1123 mutex_unlock(desc->opts_mutex);
1128 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1131 memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1135 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1136 const char *page, size_t len)
1138 struct usb_os_desc *desc = to_usb_os_desc(item);
1141 l = min_t(int, 8, len);
1142 if (page[l - 1] == '\n')
1144 if (desc->opts_mutex)
1145 mutex_lock(desc->opts_mutex);
1146 memcpy(desc->ext_compat_id + 8, page, l);
1148 if (desc->opts_mutex)
1149 mutex_unlock(desc->opts_mutex);
1154 CONFIGFS_ATTR(interf_grp_, compatible_id);
1155 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1157 static struct configfs_attribute *interf_grp_attrs[] = {
1158 &interf_grp_attr_compatible_id,
1159 &interf_grp_attr_sub_compatible_id,
1163 int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1165 struct usb_os_desc **desc,
1167 struct module *owner)
1169 struct config_group **f_default_groups, *os_desc_group,
1171 struct config_item_type *os_desc_type, *interface_type;
1173 vla_group(data_chunk);
1174 vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1175 vla_item(data_chunk, struct config_group, os_desc_group, 1);
1176 vla_item(data_chunk, struct config_group *, interface_groups,
1178 vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1179 vla_item(data_chunk, struct config_item_type, interface_type, 1);
1181 char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1185 f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1186 os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1187 os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1188 interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1189 interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1191 parent->default_groups = f_default_groups;
1192 os_desc_type->ct_owner = owner;
1193 config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1194 f_default_groups[0] = os_desc_group;
1196 os_desc_group->default_groups = interface_groups;
1197 interface_type->ct_group_ops = &interf_grp_ops;
1198 interface_type->ct_attrs = interf_grp_attrs;
1199 interface_type->ct_owner = owner;
1201 while (n_interf--) {
1202 struct usb_os_desc *d;
1206 config_group_init_type_name(&d->group, "", interface_type);
1207 config_item_set_name(&d->group.cg_item, "interface.%s",
1209 interface_groups[n_interf] = &d->group;
1214 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1216 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1222 int composite_dev_prepare(struct usb_composite_driver *composite,
1223 struct usb_composite_dev *dev);
1225 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1226 struct usb_ep *ep0);
1228 static void purge_configs_funcs(struct gadget_info *gi)
1230 struct usb_configuration *c;
1232 list_for_each_entry(c, &gi->cdev.configs, list) {
1233 struct usb_function *f, *tmp;
1234 struct config_usb_cfg *cfg;
1236 cfg = container_of(c, struct config_usb_cfg, c);
1238 list_for_each_entry_safe(f, tmp, &c->functions, list) {
1240 list_move_tail(&f->list, &cfg->func_list);
1242 dev_err(&gi->cdev.gadget->dev, "unbind function"
1243 " '%s'/%p\n", f->name, f);
1247 c->next_interface_id = 0;
1248 memset(c->interface, 0, sizeof(c->interface));
1255 static int configfs_composite_bind(struct usb_gadget *gadget,
1256 struct usb_gadget_driver *gdriver)
1258 struct usb_composite_driver *composite = to_cdriver(gdriver);
1259 struct gadget_info *gi = container_of(composite,
1260 struct gadget_info, composite);
1261 struct usb_composite_dev *cdev = &gi->cdev;
1262 struct usb_configuration *c;
1263 struct usb_string *s;
1267 /* the gi->lock is hold by the caller */
1268 cdev->gadget = gadget;
1269 set_gadget_data(gadget, cdev);
1270 ret = composite_dev_prepare(composite, cdev);
1273 /* and now the gadget bind */
1276 if (list_empty(&gi->cdev.configs)) {
1277 pr_err("Need at least one configuration in %s.\n",
1278 gi->composite.name);
1279 goto err_comp_cleanup;
1283 list_for_each_entry(c, &gi->cdev.configs, list) {
1284 struct config_usb_cfg *cfg;
1286 cfg = container_of(c, struct config_usb_cfg, c);
1287 if (list_empty(&cfg->func_list)) {
1288 pr_err("Config %s/%d of %s needs at least one function.\n",
1289 c->label, c->bConfigurationValue,
1290 gi->composite.name);
1291 goto err_comp_cleanup;
1295 /* init all strings */
1296 if (!list_empty(&gi->string_list)) {
1297 struct gadget_strings *gs;
1300 list_for_each_entry(gs, &gi->string_list, list) {
1302 gi->gstrings[i] = &gs->stringtab_dev;
1303 gs->stringtab_dev.strings = gs->strings;
1304 gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1306 gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1307 gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1310 gi->gstrings[i] = NULL;
1311 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1312 USB_GADGET_FIRST_AVAIL_IDX);
1315 goto err_comp_cleanup;
1318 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1319 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1320 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1323 if (gi->use_os_desc) {
1324 cdev->use_os_string = true;
1325 cdev->b_vendor_code = gi->b_vendor_code;
1326 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1329 if (gadget_is_otg(gadget) && !otg_desc[0]) {
1330 struct usb_descriptor_header *usb_desc;
1332 usb_desc = usb_otg_descriptor_alloc(gadget);
1335 goto err_comp_cleanup;
1337 usb_otg_descriptor_init(gadget, usb_desc);
1338 otg_desc[0] = usb_desc;
1342 /* Go through all configs, attach all functions */
1343 list_for_each_entry(c, &gi->cdev.configs, list) {
1344 struct config_usb_cfg *cfg;
1345 struct usb_function *f;
1346 struct usb_function *tmp;
1347 struct gadget_config_name *cn;
1349 if (gadget_is_otg(gadget))
1350 c->descriptors = otg_desc;
1352 cfg = container_of(c, struct config_usb_cfg, c);
1353 if (!list_empty(&cfg->string_list)) {
1355 list_for_each_entry(cn, &cfg->string_list, list) {
1356 cfg->gstrings[i] = &cn->stringtab_dev;
1357 cn->stringtab_dev.strings = &cn->strings;
1358 cn->strings.s = cn->configuration;
1361 cfg->gstrings[i] = NULL;
1362 s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1365 goto err_comp_cleanup;
1367 c->iConfiguration = s[0].id;
1370 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1372 ret = usb_add_function(c, f);
1374 list_add(&f->list, &cfg->func_list);
1375 goto err_purge_funcs;
1378 usb_ep_autoconfig_reset(cdev->gadget);
1380 if (cdev->use_os_string) {
1381 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1383 goto err_purge_funcs;
1386 usb_ep_autoconfig_reset(cdev->gadget);
1390 purge_configs_funcs(gi);
1392 composite_dev_cleanup(cdev);
1396 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1397 static void android_work(struct work_struct *data)
1399 struct gadget_info *gi = container_of(data, struct gadget_info, work);
1400 struct usb_composite_dev *cdev = &gi->cdev;
1401 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
1402 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
1403 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
1404 /* 0-connected 1-configured 2-disconnected*/
1405 bool status[3] = { false, false, false };
1406 unsigned long flags;
1407 bool uevent_sent = false;
1409 spin_lock_irqsave(&cdev->lock, flags);
1413 if (gi->connected != gi->sw_connected) {
1418 gi->sw_connected = gi->connected;
1420 spin_unlock_irqrestore(&cdev->lock, flags);
1423 kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, connected);
1424 pr_info("%s: sent uevent %s\n", __func__, connected[0]);
1429 kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, configured);
1430 pr_info("%s: sent uevent %s\n", __func__, configured[0]);
1435 kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, disconnected);
1436 pr_info("%s: sent uevent %s\n", __func__, disconnected[0]);
1441 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
1442 gi->connected, gi->sw_connected, cdev->config);
1447 static void configfs_composite_unbind(struct usb_gadget *gadget)
1449 struct usb_composite_dev *cdev;
1450 struct gadget_info *gi;
1452 /* the gi->lock is hold by the caller */
1454 cdev = get_gadget_data(gadget);
1455 gi = container_of(cdev, struct gadget_info, cdev);
1459 purge_configs_funcs(gi);
1460 composite_dev_cleanup(cdev);
1461 usb_ep_autoconfig_reset(cdev->gadget);
1462 cdev->gadget = NULL;
1463 set_gadget_data(gadget, NULL);
1466 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1467 static int android_setup(struct usb_gadget *gadget,
1468 const struct usb_ctrlrequest *c)
1470 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1471 unsigned long flags;
1472 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
1473 int value = -EOPNOTSUPP;
1474 struct usb_function_instance *fi;
1476 spin_lock_irqsave(&cdev->lock, flags);
1477 if (!gi->connected) {
1479 schedule_work(&gi->work);
1481 spin_unlock_irqrestore(&cdev->lock, flags);
1482 list_for_each_entry(fi, &gi->available_func, cfs_list) {
1483 if (fi != NULL && fi->f != NULL && fi->f->setup != NULL) {
1484 value = fi->f->setup(fi->f, c);
1490 #ifdef CONFIG_USB_CONFIGFS_F_ACC
1492 value = acc_ctrlrequest(cdev, c);
1496 value = composite_setup(gadget, c);
1498 spin_lock_irqsave(&cdev->lock, flags);
1499 if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1501 schedule_work(&gi->work);
1503 spin_unlock_irqrestore(&cdev->lock, flags);
1508 static void android_disconnect(struct usb_gadget *gadget)
1510 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1511 struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
1513 /* accessory HID support can be active while the
1514 accessory function is not actually enabled,
1515 so we need to inform it when we are disconnected.
1518 #ifdef CONFIG_USB_CONFIGFS_F_ACC
1522 schedule_work(&gi->work);
1523 composite_disconnect(gadget);
1527 static const struct usb_gadget_driver configfs_driver_template = {
1528 .bind = configfs_composite_bind,
1529 .unbind = configfs_composite_unbind,
1530 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1531 .setup = android_setup,
1532 .disconnect = android_disconnect,
1534 .setup = composite_setup,
1535 .reset = composite_disconnect,
1536 .disconnect = composite_disconnect,
1538 .suspend = composite_suspend,
1539 .resume = composite_resume,
1541 .max_speed = USB_SPEED_SUPER,
1543 .owner = THIS_MODULE,
1544 .name = "configfs-gadget",
1548 static struct config_group *gadgets_make(
1549 struct config_group *group,
1552 struct gadget_info *gi;
1554 gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1556 return ERR_PTR(-ENOMEM);
1558 gi->group.default_groups = gi->default_groups;
1559 gi->group.default_groups[0] = &gi->functions_group;
1560 gi->group.default_groups[1] = &gi->configs_group;
1561 gi->group.default_groups[2] = &gi->strings_group;
1562 gi->group.default_groups[3] = &gi->os_desc_group;
1564 config_group_init_type_name(&gi->functions_group, "functions",
1566 config_group_init_type_name(&gi->configs_group, "configs",
1568 config_group_init_type_name(&gi->strings_group, "strings",
1569 &gadget_strings_strings_type);
1570 config_group_init_type_name(&gi->os_desc_group, "os_desc",
1573 gi->composite.bind = configfs_do_nothing;
1574 gi->composite.unbind = configfs_do_nothing;
1575 gi->composite.suspend = NULL;
1576 gi->composite.resume = NULL;
1577 gi->composite.max_speed = USB_SPEED_SUPER;
1579 mutex_init(&gi->lock);
1580 INIT_LIST_HEAD(&gi->string_list);
1581 INIT_LIST_HEAD(&gi->available_func);
1583 composite_init_dev(&gi->cdev);
1584 gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1585 gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1586 gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1588 gi->composite.gadget_driver = configfs_driver_template;
1590 gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1591 gi->composite.name = gi->composite.gadget_driver.function;
1593 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1594 INIT_WORK(&gi->work, android_work);
1595 gi->dev = device_create(android_class, NULL,
1596 MKDEV(0, 0), NULL, "android0");
1599 if (!gi->composite.gadget_driver.function)
1602 config_group_init_type_name(&gi->group, name,
1607 return ERR_PTR(-ENOMEM);
1610 static void gadgets_drop(struct config_group *group, struct config_item *item)
1612 config_item_put(item);
1615 static struct configfs_group_operations gadgets_ops = {
1616 .make_group = &gadgets_make,
1617 .drop_item = &gadgets_drop,
1620 static struct config_item_type gadgets_type = {
1621 .ct_group_ops = &gadgets_ops,
1622 .ct_owner = THIS_MODULE,
1625 static struct configfs_subsystem gadget_subsys = {
1628 .ci_namebuf = "usb_gadget",
1629 .ci_type = &gadgets_type,
1632 .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1635 void unregister_gadget_item(struct config_item *item)
1637 struct gadget_info *gi = to_gadget_info(item);
1639 unregister_gadget(gi);
1641 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1643 static int __init gadget_cfs_init(void)
1647 config_group_init(&gadget_subsys.su_group);
1649 ret = configfs_register_subsystem(&gadget_subsys);
1651 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1652 android_class = class_create(THIS_MODULE, "android_usb");
1653 if (IS_ERR(android_class))
1654 return PTR_ERR(android_class);
1659 module_init(gadget_cfs_init);
1661 static void __exit gadget_cfs_exit(void)
1663 configfs_unregister_subsystem(&gadget_subsys);
1665 module_exit(gadget_cfs_exit);