1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/configfs.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/device.h>
7 #include <linux/usb/composite.h>
8 #include <linux/usb/gadget_configfs.h>
11 #include "u_os_desc.h"
13 int check_user_usb_string(const char *name
,
14 struct usb_gadget_strings
*stringtab_dev
)
16 unsigned primary_lang
;
21 ret
= kstrtou16(name
, 0, &num
);
25 primary_lang
= num
& 0x3ff;
28 /* simple sanity check for valid langid */
29 switch (primary_lang
) {
38 stringtab_dev
->language
= num
;
42 #define MAX_NAME_LEN 40
43 #define MAX_USB_STRING_LANGS 2
45 static const struct usb_descriptor_header
*otg_desc
[2];
48 struct config_group group
;
49 struct config_group functions_group
;
50 struct config_group configs_group
;
51 struct config_group strings_group
;
52 struct config_group os_desc_group
;
55 struct usb_gadget_strings
*gstrings
[MAX_USB_STRING_LANGS
+ 1];
56 struct list_head string_list
;
57 struct list_head available_func
;
59 struct usb_composite_driver composite
;
60 struct usb_composite_dev cdev
;
63 char qw_sign
[OS_STRING_QW_SIGN_LEN
];
68 static inline struct gadget_info
*to_gadget_info(struct config_item
*item
)
70 return container_of(to_config_group(item
), struct gadget_info
, group
);
73 struct config_usb_cfg
{
74 struct config_group group
;
75 struct config_group strings_group
;
76 struct list_head string_list
;
77 struct usb_configuration c
;
78 struct list_head func_list
;
79 struct usb_gadget_strings
*gstrings
[MAX_USB_STRING_LANGS
+ 1];
82 static inline struct config_usb_cfg
*to_config_usb_cfg(struct config_item
*item
)
84 return container_of(to_config_group(item
), struct config_usb_cfg
,
88 struct gadget_strings
{
89 struct usb_gadget_strings stringtab_dev
;
90 struct usb_string strings
[USB_GADGET_FIRST_AVAIL_IDX
];
95 struct config_group group
;
96 struct list_head list
;
100 struct config_group group
;
103 struct gadget_config_name
{
104 struct usb_gadget_strings stringtab_dev
;
105 struct usb_string strings
;
108 struct config_group group
;
109 struct list_head list
;
112 static int usb_string_copy(const char *s
, char **s_copy
)
116 char *copy
= *s_copy
;
121 str
= kstrdup(s
, GFP_KERNEL
);
124 if (str
[ret
- 1] == '\n')
131 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
132 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
135 return sprintf(page, "0x%02x\n", \
136 to_gadget_info(item)->cdev.desc.__name); \
139 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
140 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
143 return sprintf(page, "0x%04x\n", \
144 le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
148 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
149 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
150 const char *page, size_t len) \
154 ret = kstrtou8(page, 0, &val); \
157 to_gadget_info(item)->cdev.desc._name = val; \
161 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
162 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
163 const char *page, size_t len) \
167 ret = kstrtou16(page, 0, &val); \
170 to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
174 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
175 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
176 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
178 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB
);
179 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass
, u8
);
180 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass
, u8
);
181 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol
, u8
);
182 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0
, u8
);
183 GI_DEVICE_DESC_SIMPLE_RW(idVendor
, u16
);
184 GI_DEVICE_DESC_SIMPLE_RW(idProduct
, u16
);
185 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice
);
187 static ssize_t
is_valid_bcd(u16 bcd_val
)
189 if ((bcd_val
& 0xf) > 9)
191 if (((bcd_val
>> 4) & 0xf) > 9)
193 if (((bcd_val
>> 8) & 0xf) > 9)
195 if (((bcd_val
>> 12) & 0xf) > 9)
200 static ssize_t
gadget_dev_desc_bcdDevice_store(struct config_item
*item
,
201 const char *page
, size_t len
)
206 ret
= kstrtou16(page
, 0, &bcdDevice
);
209 ret
= is_valid_bcd(bcdDevice
);
213 to_gadget_info(item
)->cdev
.desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
217 static ssize_t
gadget_dev_desc_bcdUSB_store(struct config_item
*item
,
218 const char *page
, size_t len
)
223 ret
= kstrtou16(page
, 0, &bcdUSB
);
226 ret
= is_valid_bcd(bcdUSB
);
230 to_gadget_info(item
)->cdev
.desc
.bcdUSB
= cpu_to_le16(bcdUSB
);
234 static ssize_t
gadget_dev_desc_UDC_show(struct config_item
*item
, char *page
)
236 char *udc_name
= to_gadget_info(item
)->composite
.gadget_driver
.udc_name
;
238 return sprintf(page
, "%s\n", udc_name
?: "");
241 static int unregister_gadget(struct gadget_info
*gi
)
245 if (!gi
->composite
.gadget_driver
.udc_name
)
248 ret
= usb_gadget_unregister_driver(&gi
->composite
.gadget_driver
);
251 kfree(gi
->composite
.gadget_driver
.udc_name
);
252 gi
->composite
.gadget_driver
.udc_name
= NULL
;
256 static ssize_t
gadget_dev_desc_UDC_store(struct config_item
*item
,
257 const char *page
, size_t len
)
259 struct gadget_info
*gi
= to_gadget_info(item
);
263 if (strlen(page
) < len
)
266 name
= kstrdup(page
, GFP_KERNEL
);
269 if (name
[len
- 1] == '\n')
270 name
[len
- 1] = '\0';
272 mutex_lock(&gi
->lock
);
275 ret
= unregister_gadget(gi
);
280 if (gi
->composite
.gadget_driver
.udc_name
) {
284 gi
->composite
.gadget_driver
.udc_name
= name
;
285 ret
= usb_gadget_probe_driver(&gi
->composite
.gadget_driver
);
287 gi
->composite
.gadget_driver
.udc_name
= NULL
;
291 mutex_unlock(&gi
->lock
);
295 mutex_unlock(&gi
->lock
);
299 static ssize_t
gadget_dev_desc_max_speed_show(struct config_item
*item
,
302 enum usb_device_speed speed
= to_gadget_info(item
)->composite
.max_speed
;
304 return sprintf(page
, "%s\n", usb_speed_string(speed
));
307 static ssize_t
gadget_dev_desc_max_speed_store(struct config_item
*item
,
308 const char *page
, size_t len
)
310 struct gadget_info
*gi
= to_gadget_info(item
);
312 mutex_lock(&gi
->lock
);
314 /* Prevent changing of max_speed after the driver is binded */
315 if (gi
->composite
.gadget_driver
.udc_name
)
318 if (strncmp(page
, "super-speed-plus", 16) == 0)
319 gi
->composite
.max_speed
= USB_SPEED_SUPER_PLUS
;
320 else if (strncmp(page
, "super-speed", 11) == 0)
321 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
322 else if (strncmp(page
, "high-speed", 10) == 0)
323 gi
->composite
.max_speed
= USB_SPEED_HIGH
;
324 else if (strncmp(page
, "full-speed", 10) == 0)
325 gi
->composite
.max_speed
= USB_SPEED_FULL
;
326 else if (strncmp(page
, "low-speed", 9) == 0)
327 gi
->composite
.max_speed
= USB_SPEED_LOW
;
331 gi
->composite
.gadget_driver
.max_speed
= gi
->composite
.max_speed
;
333 mutex_unlock(&gi
->lock
);
336 mutex_unlock(&gi
->lock
);
340 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceClass
);
341 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceSubClass
);
342 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceProtocol
);
343 CONFIGFS_ATTR(gadget_dev_desc_
, bMaxPacketSize0
);
344 CONFIGFS_ATTR(gadget_dev_desc_
, idVendor
);
345 CONFIGFS_ATTR(gadget_dev_desc_
, idProduct
);
346 CONFIGFS_ATTR(gadget_dev_desc_
, bcdDevice
);
347 CONFIGFS_ATTR(gadget_dev_desc_
, bcdUSB
);
348 CONFIGFS_ATTR(gadget_dev_desc_
, UDC
);
349 CONFIGFS_ATTR(gadget_dev_desc_
, max_speed
);
351 static struct configfs_attribute
*gadget_root_attrs
[] = {
352 &gadget_dev_desc_attr_bDeviceClass
,
353 &gadget_dev_desc_attr_bDeviceSubClass
,
354 &gadget_dev_desc_attr_bDeviceProtocol
,
355 &gadget_dev_desc_attr_bMaxPacketSize0
,
356 &gadget_dev_desc_attr_idVendor
,
357 &gadget_dev_desc_attr_idProduct
,
358 &gadget_dev_desc_attr_bcdDevice
,
359 &gadget_dev_desc_attr_bcdUSB
,
360 &gadget_dev_desc_attr_UDC
,
361 &gadget_dev_desc_attr_max_speed
,
365 static inline struct gadget_strings
*to_gadget_strings(struct config_item
*item
)
367 return container_of(to_config_group(item
), struct gadget_strings
,
371 static inline struct gadget_config_name
*to_gadget_config_name(
372 struct config_item
*item
)
374 return container_of(to_config_group(item
), struct gadget_config_name
,
378 static inline struct usb_function_instance
*to_usb_function_instance(
379 struct config_item
*item
)
381 return container_of(to_config_group(item
),
382 struct usb_function_instance
, group
);
385 static void gadget_info_attr_release(struct config_item
*item
)
387 struct gadget_info
*gi
= to_gadget_info(item
);
389 WARN_ON(!list_empty(&gi
->cdev
.configs
));
390 WARN_ON(!list_empty(&gi
->string_list
));
391 WARN_ON(!list_empty(&gi
->available_func
));
392 kfree(gi
->composite
.gadget_driver
.function
);
396 static struct configfs_item_operations gadget_root_item_ops
= {
397 .release
= gadget_info_attr_release
,
400 static void gadget_config_attr_release(struct config_item
*item
)
402 struct config_usb_cfg
*cfg
= to_config_usb_cfg(item
);
404 WARN_ON(!list_empty(&cfg
->c
.functions
));
405 list_del(&cfg
->c
.list
);
410 static int config_usb_cfg_link(
411 struct config_item
*usb_cfg_ci
,
412 struct config_item
*usb_func_ci
)
414 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
415 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
416 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
418 struct config_group
*group
= to_config_group(usb_func_ci
);
419 struct usb_function_instance
*fi
= container_of(group
,
420 struct usb_function_instance
, group
);
421 struct usb_function_instance
*a_fi
;
422 struct usb_function
*f
;
425 mutex_lock(&gi
->lock
);
427 * Make sure this function is from within our _this_ gadget and not
428 * from another gadget or a random directory.
429 * Also a function instance can only be linked once.
431 list_for_each_entry(a_fi
, &gi
->available_func
, cfs_list
) {
440 list_for_each_entry(f
, &cfg
->func_list
, list
) {
447 f
= usb_get_function(fi
);
453 /* stash the function until we bind it to the gadget */
454 list_add_tail(&f
->list
, &cfg
->func_list
);
457 mutex_unlock(&gi
->lock
);
461 static void config_usb_cfg_unlink(
462 struct config_item
*usb_cfg_ci
,
463 struct config_item
*usb_func_ci
)
465 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
466 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
467 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
469 struct config_group
*group
= to_config_group(usb_func_ci
);
470 struct usb_function_instance
*fi
= container_of(group
,
471 struct usb_function_instance
, group
);
472 struct usb_function
*f
;
475 * ideally I would like to forbid to unlink functions while a gadget is
476 * bound to an UDC. Since this isn't possible at the moment, we simply
477 * force an unbind, the function is available here and then we can
478 * remove the function.
480 mutex_lock(&gi
->lock
);
481 if (gi
->composite
.gadget_driver
.udc_name
)
482 unregister_gadget(gi
);
483 WARN_ON(gi
->composite
.gadget_driver
.udc_name
);
485 list_for_each_entry(f
, &cfg
->func_list
, list
) {
489 mutex_unlock(&gi
->lock
);
493 mutex_unlock(&gi
->lock
);
494 WARN(1, "Unable to locate function to unbind\n");
497 static struct configfs_item_operations gadget_config_item_ops
= {
498 .release
= gadget_config_attr_release
,
499 .allow_link
= config_usb_cfg_link
,
500 .drop_link
= config_usb_cfg_unlink
,
504 static ssize_t
gadget_config_desc_MaxPower_show(struct config_item
*item
,
507 return sprintf(page
, "%u\n", to_config_usb_cfg(item
)->c
.MaxPower
);
510 static ssize_t
gadget_config_desc_MaxPower_store(struct config_item
*item
,
511 const char *page
, size_t len
)
515 ret
= kstrtou16(page
, 0, &val
);
518 if (DIV_ROUND_UP(val
, 8) > 0xff)
520 to_config_usb_cfg(item
)->c
.MaxPower
= val
;
524 static ssize_t
gadget_config_desc_bmAttributes_show(struct config_item
*item
,
527 return sprintf(page
, "0x%02x\n",
528 to_config_usb_cfg(item
)->c
.bmAttributes
);
531 static ssize_t
gadget_config_desc_bmAttributes_store(struct config_item
*item
,
532 const char *page
, size_t len
)
536 ret
= kstrtou8(page
, 0, &val
);
539 if (!(val
& USB_CONFIG_ATT_ONE
))
541 if (val
& ~(USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
|
542 USB_CONFIG_ATT_WAKEUP
))
544 to_config_usb_cfg(item
)->c
.bmAttributes
= val
;
548 CONFIGFS_ATTR(gadget_config_desc_
, MaxPower
);
549 CONFIGFS_ATTR(gadget_config_desc_
, bmAttributes
);
551 static struct configfs_attribute
*gadget_config_attrs
[] = {
552 &gadget_config_desc_attr_MaxPower
,
553 &gadget_config_desc_attr_bmAttributes
,
557 static const struct config_item_type gadget_config_type
= {
558 .ct_item_ops
= &gadget_config_item_ops
,
559 .ct_attrs
= gadget_config_attrs
,
560 .ct_owner
= THIS_MODULE
,
563 static const struct config_item_type gadget_root_type
= {
564 .ct_item_ops
= &gadget_root_item_ops
,
565 .ct_attrs
= gadget_root_attrs
,
566 .ct_owner
= THIS_MODULE
,
569 static void composite_init_dev(struct usb_composite_dev
*cdev
)
571 spin_lock_init(&cdev
->lock
);
572 INIT_LIST_HEAD(&cdev
->configs
);
573 INIT_LIST_HEAD(&cdev
->gstrings
);
576 static struct config_group
*function_make(
577 struct config_group
*group
,
580 struct gadget_info
*gi
;
581 struct usb_function_instance
*fi
;
582 char buf
[MAX_NAME_LEN
];
587 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
588 if (ret
>= MAX_NAME_LEN
)
589 return ERR_PTR(-ENAMETOOLONG
);
592 instance_name
= strchr(func_name
, '.');
593 if (!instance_name
) {
594 pr_err("Unable to locate . in FUNC.INSTANCE\n");
595 return ERR_PTR(-EINVAL
);
597 *instance_name
= '\0';
600 fi
= usb_get_function_instance(func_name
);
604 ret
= config_item_set_name(&fi
->group
.cg_item
, "%s", name
);
606 usb_put_function_instance(fi
);
609 if (fi
->set_inst_name
) {
610 ret
= fi
->set_inst_name(fi
, instance_name
);
612 usb_put_function_instance(fi
);
617 gi
= container_of(group
, struct gadget_info
, functions_group
);
619 mutex_lock(&gi
->lock
);
620 list_add_tail(&fi
->cfs_list
, &gi
->available_func
);
621 mutex_unlock(&gi
->lock
);
625 static void function_drop(
626 struct config_group
*group
,
627 struct config_item
*item
)
629 struct usb_function_instance
*fi
= to_usb_function_instance(item
);
630 struct gadget_info
*gi
;
632 gi
= container_of(group
, struct gadget_info
, functions_group
);
634 mutex_lock(&gi
->lock
);
635 list_del(&fi
->cfs_list
);
636 mutex_unlock(&gi
->lock
);
637 config_item_put(item
);
640 static struct configfs_group_operations functions_ops
= {
641 .make_group
= &function_make
,
642 .drop_item
= &function_drop
,
645 static const struct config_item_type functions_type
= {
646 .ct_group_ops
= &functions_ops
,
647 .ct_owner
= THIS_MODULE
,
650 GS_STRINGS_RW(gadget_config_name
, configuration
);
652 static struct configfs_attribute
*gadget_config_name_langid_attrs
[] = {
653 &gadget_config_name_attr_configuration
,
657 static void gadget_config_name_attr_release(struct config_item
*item
)
659 struct gadget_config_name
*cn
= to_gadget_config_name(item
);
661 kfree(cn
->configuration
);
667 USB_CONFIG_STRING_RW_OPS(gadget_config_name
);
668 USB_CONFIG_STRINGS_LANG(gadget_config_name
, config_usb_cfg
);
670 static struct config_group
*config_desc_make(
671 struct config_group
*group
,
674 struct gadget_info
*gi
;
675 struct config_usb_cfg
*cfg
;
676 char buf
[MAX_NAME_LEN
];
681 gi
= container_of(group
, struct gadget_info
, configs_group
);
682 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
683 if (ret
>= MAX_NAME_LEN
)
684 return ERR_PTR(-ENAMETOOLONG
);
686 num_str
= strchr(buf
, '.');
688 pr_err("Unable to locate . in name.bConfigurationValue\n");
689 return ERR_PTR(-EINVAL
);
696 return ERR_PTR(-EINVAL
);
698 ret
= kstrtou8(num_str
, 0, &num
);
702 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
704 return ERR_PTR(-ENOMEM
);
705 cfg
->c
.label
= kstrdup(buf
, GFP_KERNEL
);
710 cfg
->c
.bConfigurationValue
= num
;
711 cfg
->c
.MaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
;
712 cfg
->c
.bmAttributes
= USB_CONFIG_ATT_ONE
;
713 INIT_LIST_HEAD(&cfg
->string_list
);
714 INIT_LIST_HEAD(&cfg
->func_list
);
716 config_group_init_type_name(&cfg
->group
, name
,
717 &gadget_config_type
);
719 config_group_init_type_name(&cfg
->strings_group
, "strings",
720 &gadget_config_name_strings_type
);
721 configfs_add_default_group(&cfg
->strings_group
, &cfg
->group
);
723 ret
= usb_add_config_only(&gi
->cdev
, &cfg
->c
);
734 static void config_desc_drop(
735 struct config_group
*group
,
736 struct config_item
*item
)
738 config_item_put(item
);
741 static struct configfs_group_operations config_desc_ops
= {
742 .make_group
= &config_desc_make
,
743 .drop_item
= &config_desc_drop
,
746 static const struct config_item_type config_desc_type
= {
747 .ct_group_ops
= &config_desc_ops
,
748 .ct_owner
= THIS_MODULE
,
751 GS_STRINGS_RW(gadget_strings
, manufacturer
);
752 GS_STRINGS_RW(gadget_strings
, product
);
753 GS_STRINGS_RW(gadget_strings
, serialnumber
);
755 static struct configfs_attribute
*gadget_strings_langid_attrs
[] = {
756 &gadget_strings_attr_manufacturer
,
757 &gadget_strings_attr_product
,
758 &gadget_strings_attr_serialnumber
,
762 static void gadget_strings_attr_release(struct config_item
*item
)
764 struct gadget_strings
*gs
= to_gadget_strings(item
);
766 kfree(gs
->manufacturer
);
768 kfree(gs
->serialnumber
);
774 USB_CONFIG_STRING_RW_OPS(gadget_strings
);
775 USB_CONFIG_STRINGS_LANG(gadget_strings
, gadget_info
);
777 static inline struct os_desc
*to_os_desc(struct config_item
*item
)
779 return container_of(to_config_group(item
), struct os_desc
, group
);
782 static inline struct gadget_info
*os_desc_item_to_gadget_info(
783 struct config_item
*item
)
785 return to_gadget_info(to_os_desc(item
)->group
.cg_item
.ci_parent
);
788 static ssize_t
os_desc_use_show(struct config_item
*item
, char *page
)
790 return sprintf(page
, "%d\n",
791 os_desc_item_to_gadget_info(item
)->use_os_desc
);
794 static ssize_t
os_desc_use_store(struct config_item
*item
, const char *page
,
797 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
801 mutex_lock(&gi
->lock
);
802 ret
= strtobool(page
, &use
);
804 gi
->use_os_desc
= use
;
807 mutex_unlock(&gi
->lock
);
812 static ssize_t
os_desc_b_vendor_code_show(struct config_item
*item
, char *page
)
814 return sprintf(page
, "0x%02x\n",
815 os_desc_item_to_gadget_info(item
)->b_vendor_code
);
818 static ssize_t
os_desc_b_vendor_code_store(struct config_item
*item
,
819 const char *page
, size_t len
)
821 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
825 mutex_lock(&gi
->lock
);
826 ret
= kstrtou8(page
, 0, &b_vendor_code
);
828 gi
->b_vendor_code
= b_vendor_code
;
831 mutex_unlock(&gi
->lock
);
836 static ssize_t
os_desc_qw_sign_show(struct config_item
*item
, char *page
)
838 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
841 res
= utf16s_to_utf8s((wchar_t *) gi
->qw_sign
, OS_STRING_QW_SIGN_LEN
,
842 UTF16_LITTLE_ENDIAN
, page
, PAGE_SIZE
- 1);
848 static ssize_t
os_desc_qw_sign_store(struct config_item
*item
, const char *page
,
851 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
854 l
= min((int)len
, OS_STRING_QW_SIGN_LEN
>> 1);
855 if (page
[l
- 1] == '\n')
858 mutex_lock(&gi
->lock
);
859 res
= utf8s_to_utf16s(page
, l
,
860 UTF16_LITTLE_ENDIAN
, (wchar_t *) gi
->qw_sign
,
861 OS_STRING_QW_SIGN_LEN
);
864 mutex_unlock(&gi
->lock
);
869 CONFIGFS_ATTR(os_desc_
, use
);
870 CONFIGFS_ATTR(os_desc_
, b_vendor_code
);
871 CONFIGFS_ATTR(os_desc_
, qw_sign
);
873 static struct configfs_attribute
*os_desc_attrs
[] = {
875 &os_desc_attr_b_vendor_code
,
876 &os_desc_attr_qw_sign
,
880 static void os_desc_attr_release(struct config_item
*item
)
882 struct os_desc
*os_desc
= to_os_desc(item
);
886 static int os_desc_link(struct config_item
*os_desc_ci
,
887 struct config_item
*usb_cfg_ci
)
889 struct gadget_info
*gi
= container_of(to_config_group(os_desc_ci
),
890 struct gadget_info
, os_desc_group
);
891 struct usb_composite_dev
*cdev
= &gi
->cdev
;
892 struct config_usb_cfg
*c_target
=
893 container_of(to_config_group(usb_cfg_ci
),
894 struct config_usb_cfg
, group
);
895 struct usb_configuration
*c
;
898 mutex_lock(&gi
->lock
);
899 list_for_each_entry(c
, &cdev
->configs
, list
) {
900 if (c
== &c_target
->c
)
903 if (c
!= &c_target
->c
) {
908 if (cdev
->os_desc_config
) {
913 cdev
->os_desc_config
= &c_target
->c
;
917 mutex_unlock(&gi
->lock
);
921 static void os_desc_unlink(struct config_item
*os_desc_ci
,
922 struct config_item
*usb_cfg_ci
)
924 struct gadget_info
*gi
= container_of(to_config_group(os_desc_ci
),
925 struct gadget_info
, os_desc_group
);
926 struct usb_composite_dev
*cdev
= &gi
->cdev
;
928 mutex_lock(&gi
->lock
);
929 if (gi
->composite
.gadget_driver
.udc_name
)
930 unregister_gadget(gi
);
931 cdev
->os_desc_config
= NULL
;
932 WARN_ON(gi
->composite
.gadget_driver
.udc_name
);
933 mutex_unlock(&gi
->lock
);
936 static struct configfs_item_operations os_desc_ops
= {
937 .release
= os_desc_attr_release
,
938 .allow_link
= os_desc_link
,
939 .drop_link
= os_desc_unlink
,
942 static struct config_item_type os_desc_type
= {
943 .ct_item_ops
= &os_desc_ops
,
944 .ct_attrs
= os_desc_attrs
,
945 .ct_owner
= THIS_MODULE
,
948 static inline struct usb_os_desc_ext_prop
949 *to_usb_os_desc_ext_prop(struct config_item
*item
)
951 return container_of(item
, struct usb_os_desc_ext_prop
, item
);
954 static ssize_t
ext_prop_type_show(struct config_item
*item
, char *page
)
956 return sprintf(page
, "%d\n", to_usb_os_desc_ext_prop(item
)->type
);
959 static ssize_t
ext_prop_type_store(struct config_item
*item
,
960 const char *page
, size_t len
)
962 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
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 config_item
*item
, char *page
)
1002 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
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 config_item
*item
,
1015 const char *page
, size_t len
)
1017 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1018 struct usb_os_desc
*desc
= to_usb_os_desc(ext_prop
->item
.ci_parent
);
1020 size_t ret_len
= len
;
1022 if (page
[len
- 1] == '\n' || page
[len
- 1] == '\0')
1024 new_data
= kmemdup(page
, len
, GFP_KERNEL
);
1028 if (desc
->opts_mutex
)
1029 mutex_lock(desc
->opts_mutex
);
1030 kfree(ext_prop
->data
);
1031 ext_prop
->data
= new_data
;
1032 desc
->ext_prop_len
-= ext_prop
->data_len
;
1033 ext_prop
->data_len
= len
;
1034 desc
->ext_prop_len
+= ext_prop
->data_len
;
1035 if (ext_prop
->type
== USB_EXT_PROP_UNICODE
||
1036 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
1037 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
) {
1038 desc
->ext_prop_len
-= ext_prop
->data_len
;
1039 ext_prop
->data_len
<<= 1;
1040 ext_prop
->data_len
+= 2;
1041 desc
->ext_prop_len
+= ext_prop
->data_len
;
1043 if (desc
->opts_mutex
)
1044 mutex_unlock(desc
->opts_mutex
);
1048 CONFIGFS_ATTR(ext_prop_
, type
);
1049 CONFIGFS_ATTR(ext_prop_
, data
);
1051 static struct configfs_attribute
*ext_prop_attrs
[] = {
1052 &ext_prop_attr_type
,
1053 &ext_prop_attr_data
,
1057 static void usb_os_desc_ext_prop_release(struct config_item
*item
)
1059 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1061 kfree(ext_prop
); /* frees a whole chunk */
1064 static struct configfs_item_operations ext_prop_ops
= {
1065 .release
= usb_os_desc_ext_prop_release
,
1068 static struct config_item
*ext_prop_make(
1069 struct config_group
*group
,
1072 struct usb_os_desc_ext_prop
*ext_prop
;
1073 struct config_item_type
*ext_prop_type
;
1074 struct usb_os_desc
*desc
;
1077 vla_group(data_chunk
);
1078 vla_item(data_chunk
, struct usb_os_desc_ext_prop
, ext_prop
, 1);
1079 vla_item(data_chunk
, struct config_item_type
, ext_prop_type
, 1);
1081 vlabuf
= kzalloc(vla_group_size(data_chunk
), GFP_KERNEL
);
1083 return ERR_PTR(-ENOMEM
);
1085 ext_prop
= vla_ptr(vlabuf
, data_chunk
, ext_prop
);
1086 ext_prop_type
= vla_ptr(vlabuf
, data_chunk
, ext_prop_type
);
1088 desc
= container_of(group
, struct usb_os_desc
, group
);
1089 ext_prop_type
->ct_item_ops
= &ext_prop_ops
;
1090 ext_prop_type
->ct_attrs
= ext_prop_attrs
;
1091 ext_prop_type
->ct_owner
= desc
->owner
;
1093 config_item_init_type_name(&ext_prop
->item
, name
, ext_prop_type
);
1095 ext_prop
->name
= kstrdup(name
, GFP_KERNEL
);
1096 if (!ext_prop
->name
) {
1098 return ERR_PTR(-ENOMEM
);
1100 desc
->ext_prop_len
+= 14;
1101 ext_prop
->name_len
= 2 * strlen(ext_prop
->name
) + 2;
1102 if (desc
->opts_mutex
)
1103 mutex_lock(desc
->opts_mutex
);
1104 desc
->ext_prop_len
+= ext_prop
->name_len
;
1105 list_add_tail(&ext_prop
->entry
, &desc
->ext_prop
);
1106 ++desc
->ext_prop_count
;
1107 if (desc
->opts_mutex
)
1108 mutex_unlock(desc
->opts_mutex
);
1110 return &ext_prop
->item
;
1113 static void ext_prop_drop(struct config_group
*group
, struct config_item
*item
)
1115 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1116 struct usb_os_desc
*desc
= to_usb_os_desc(&group
->cg_item
);
1118 if (desc
->opts_mutex
)
1119 mutex_lock(desc
->opts_mutex
);
1120 list_del(&ext_prop
->entry
);
1121 --desc
->ext_prop_count
;
1122 kfree(ext_prop
->name
);
1123 desc
->ext_prop_len
-= (ext_prop
->name_len
+ ext_prop
->data_len
+ 14);
1124 if (desc
->opts_mutex
)
1125 mutex_unlock(desc
->opts_mutex
);
1126 config_item_put(item
);
1129 static struct configfs_group_operations interf_grp_ops
= {
1130 .make_item
= &ext_prop_make
,
1131 .drop_item
= &ext_prop_drop
,
1134 static ssize_t
interf_grp_compatible_id_show(struct config_item
*item
,
1137 memcpy(page
, to_usb_os_desc(item
)->ext_compat_id
, 8);
1141 static ssize_t
interf_grp_compatible_id_store(struct config_item
*item
,
1142 const char *page
, size_t len
)
1144 struct usb_os_desc
*desc
= to_usb_os_desc(item
);
1147 l
= min_t(int, 8, len
);
1148 if (page
[l
- 1] == '\n')
1150 if (desc
->opts_mutex
)
1151 mutex_lock(desc
->opts_mutex
);
1152 memcpy(desc
->ext_compat_id
, page
, l
);
1154 if (desc
->opts_mutex
)
1155 mutex_unlock(desc
->opts_mutex
);
1160 static ssize_t
interf_grp_sub_compatible_id_show(struct config_item
*item
,
1163 memcpy(page
, to_usb_os_desc(item
)->ext_compat_id
+ 8, 8);
1167 static ssize_t
interf_grp_sub_compatible_id_store(struct config_item
*item
,
1168 const char *page
, size_t len
)
1170 struct usb_os_desc
*desc
= to_usb_os_desc(item
);
1173 l
= min_t(int, 8, len
);
1174 if (page
[l
- 1] == '\n')
1176 if (desc
->opts_mutex
)
1177 mutex_lock(desc
->opts_mutex
);
1178 memcpy(desc
->ext_compat_id
+ 8, page
, l
);
1180 if (desc
->opts_mutex
)
1181 mutex_unlock(desc
->opts_mutex
);
1186 CONFIGFS_ATTR(interf_grp_
, compatible_id
);
1187 CONFIGFS_ATTR(interf_grp_
, sub_compatible_id
);
1189 static struct configfs_attribute
*interf_grp_attrs
[] = {
1190 &interf_grp_attr_compatible_id
,
1191 &interf_grp_attr_sub_compatible_id
,
1195 struct config_group
*usb_os_desc_prepare_interf_dir(
1196 struct config_group
*parent
,
1198 struct usb_os_desc
**desc
,
1200 struct module
*owner
)
1202 struct config_group
*os_desc_group
;
1203 struct config_item_type
*os_desc_type
, *interface_type
;
1205 vla_group(data_chunk
);
1206 vla_item(data_chunk
, struct config_group
, os_desc_group
, 1);
1207 vla_item(data_chunk
, struct config_item_type
, os_desc_type
, 1);
1208 vla_item(data_chunk
, struct config_item_type
, interface_type
, 1);
1210 char *vlabuf
= kzalloc(vla_group_size(data_chunk
), GFP_KERNEL
);
1212 return ERR_PTR(-ENOMEM
);
1214 os_desc_group
= vla_ptr(vlabuf
, data_chunk
, os_desc_group
);
1215 os_desc_type
= vla_ptr(vlabuf
, data_chunk
, os_desc_type
);
1216 interface_type
= vla_ptr(vlabuf
, data_chunk
, interface_type
);
1218 os_desc_type
->ct_owner
= owner
;
1219 config_group_init_type_name(os_desc_group
, "os_desc", os_desc_type
);
1220 configfs_add_default_group(os_desc_group
, parent
);
1222 interface_type
->ct_group_ops
= &interf_grp_ops
;
1223 interface_type
->ct_attrs
= interf_grp_attrs
;
1224 interface_type
->ct_owner
= owner
;
1226 while (n_interf
--) {
1227 struct usb_os_desc
*d
;
1231 config_group_init_type_name(&d
->group
, "", interface_type
);
1232 config_item_set_name(&d
->group
.cg_item
, "interface.%s",
1234 configfs_add_default_group(&d
->group
, os_desc_group
);
1237 return os_desc_group
;
1239 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir
);
1241 static int configfs_do_nothing(struct usb_composite_dev
*cdev
)
1247 int composite_dev_prepare(struct usb_composite_driver
*composite
,
1248 struct usb_composite_dev
*dev
);
1250 int composite_os_desc_req_prepare(struct usb_composite_dev
*cdev
,
1251 struct usb_ep
*ep0
);
1253 static void purge_configs_funcs(struct gadget_info
*gi
)
1255 struct usb_configuration
*c
;
1257 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1258 struct usb_function
*f
, *tmp
;
1259 struct config_usb_cfg
*cfg
;
1261 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1263 list_for_each_entry_safe(f
, tmp
, &c
->functions
, list
) {
1265 list_move_tail(&f
->list
, &cfg
->func_list
);
1267 dev_dbg(&gi
->cdev
.gadget
->dev
,
1268 "unbind function '%s'/%p\n",
1273 c
->next_interface_id
= 0;
1274 memset(c
->interface
, 0, sizeof(c
->interface
));
1275 c
->superspeed_plus
= 0;
1282 static int configfs_composite_bind(struct usb_gadget
*gadget
,
1283 struct usb_gadget_driver
*gdriver
)
1285 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1286 struct gadget_info
*gi
= container_of(composite
,
1287 struct gadget_info
, composite
);
1288 struct usb_composite_dev
*cdev
= &gi
->cdev
;
1289 struct usb_configuration
*c
;
1290 struct usb_string
*s
;
1294 /* the gi->lock is hold by the caller */
1296 cdev
->gadget
= gadget
;
1297 set_gadget_data(gadget
, cdev
);
1298 ret
= composite_dev_prepare(composite
, cdev
);
1301 /* and now the gadget bind */
1304 if (list_empty(&gi
->cdev
.configs
)) {
1305 pr_err("Need at least one configuration in %s.\n",
1306 gi
->composite
.name
);
1307 goto err_comp_cleanup
;
1311 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1312 struct config_usb_cfg
*cfg
;
1314 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1315 if (list_empty(&cfg
->func_list
)) {
1316 pr_err("Config %s/%d of %s needs at least one function.\n",
1317 c
->label
, c
->bConfigurationValue
,
1318 gi
->composite
.name
);
1319 goto err_comp_cleanup
;
1323 /* init all strings */
1324 if (!list_empty(&gi
->string_list
)) {
1325 struct gadget_strings
*gs
;
1328 list_for_each_entry(gs
, &gi
->string_list
, list
) {
1330 gi
->gstrings
[i
] = &gs
->stringtab_dev
;
1331 gs
->stringtab_dev
.strings
= gs
->strings
;
1332 gs
->strings
[USB_GADGET_MANUFACTURER_IDX
].s
=
1334 gs
->strings
[USB_GADGET_PRODUCT_IDX
].s
= gs
->product
;
1335 gs
->strings
[USB_GADGET_SERIAL_IDX
].s
= gs
->serialnumber
;
1338 gi
->gstrings
[i
] = NULL
;
1339 s
= usb_gstrings_attach(&gi
->cdev
, gi
->gstrings
,
1340 USB_GADGET_FIRST_AVAIL_IDX
);
1343 goto err_comp_cleanup
;
1346 gi
->cdev
.desc
.iManufacturer
= s
[USB_GADGET_MANUFACTURER_IDX
].id
;
1347 gi
->cdev
.desc
.iProduct
= s
[USB_GADGET_PRODUCT_IDX
].id
;
1348 gi
->cdev
.desc
.iSerialNumber
= s
[USB_GADGET_SERIAL_IDX
].id
;
1351 if (gi
->use_os_desc
) {
1352 cdev
->use_os_string
= true;
1353 cdev
->b_vendor_code
= gi
->b_vendor_code
;
1354 memcpy(cdev
->qw_sign
, gi
->qw_sign
, OS_STRING_QW_SIGN_LEN
);
1357 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
1358 struct usb_descriptor_header
*usb_desc
;
1360 usb_desc
= usb_otg_descriptor_alloc(gadget
);
1363 goto err_comp_cleanup
;
1365 usb_otg_descriptor_init(gadget
, usb_desc
);
1366 otg_desc
[0] = usb_desc
;
1370 /* Go through all configs, attach all functions */
1371 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1372 struct config_usb_cfg
*cfg
;
1373 struct usb_function
*f
;
1374 struct usb_function
*tmp
;
1375 struct gadget_config_name
*cn
;
1377 if (gadget_is_otg(gadget
))
1378 c
->descriptors
= otg_desc
;
1380 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1381 if (!list_empty(&cfg
->string_list
)) {
1383 list_for_each_entry(cn
, &cfg
->string_list
, list
) {
1384 cfg
->gstrings
[i
] = &cn
->stringtab_dev
;
1385 cn
->stringtab_dev
.strings
= &cn
->strings
;
1386 cn
->strings
.s
= cn
->configuration
;
1389 cfg
->gstrings
[i
] = NULL
;
1390 s
= usb_gstrings_attach(&gi
->cdev
, cfg
->gstrings
, 1);
1393 goto err_comp_cleanup
;
1395 c
->iConfiguration
= s
[0].id
;
1398 list_for_each_entry_safe(f
, tmp
, &cfg
->func_list
, list
) {
1400 ret
= usb_add_function(c
, f
);
1402 list_add(&f
->list
, &cfg
->func_list
);
1403 goto err_purge_funcs
;
1406 usb_ep_autoconfig_reset(cdev
->gadget
);
1408 if (cdev
->use_os_string
) {
1409 ret
= composite_os_desc_req_prepare(cdev
, gadget
->ep0
);
1411 goto err_purge_funcs
;
1414 usb_ep_autoconfig_reset(cdev
->gadget
);
1418 purge_configs_funcs(gi
);
1420 composite_dev_cleanup(cdev
);
1424 static void configfs_composite_unbind(struct usb_gadget
*gadget
)
1426 struct usb_composite_dev
*cdev
;
1427 struct gadget_info
*gi
;
1428 unsigned long flags
;
1430 /* the gi->lock is hold by the caller */
1432 cdev
= get_gadget_data(gadget
);
1433 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1434 spin_lock_irqsave(&gi
->spinlock
, flags
);
1436 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1440 purge_configs_funcs(gi
);
1441 composite_dev_cleanup(cdev
);
1442 usb_ep_autoconfig_reset(cdev
->gadget
);
1443 spin_lock_irqsave(&gi
->spinlock
, flags
);
1444 cdev
->gadget
= NULL
;
1445 set_gadget_data(gadget
, NULL
);
1446 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1449 static int configfs_composite_setup(struct usb_gadget
*gadget
,
1450 const struct usb_ctrlrequest
*ctrl
)
1452 struct usb_composite_dev
*cdev
;
1453 struct gadget_info
*gi
;
1454 unsigned long flags
;
1457 cdev
= get_gadget_data(gadget
);
1461 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1462 spin_lock_irqsave(&gi
->spinlock
, flags
);
1463 cdev
= get_gadget_data(gadget
);
1464 if (!cdev
|| gi
->unbind
) {
1465 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1469 ret
= composite_setup(gadget
, ctrl
);
1470 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1474 static void configfs_composite_disconnect(struct usb_gadget
*gadget
)
1476 struct usb_composite_dev
*cdev
;
1477 struct gadget_info
*gi
;
1478 unsigned long flags
;
1480 cdev
= get_gadget_data(gadget
);
1484 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1485 spin_lock_irqsave(&gi
->spinlock
, flags
);
1486 cdev
= get_gadget_data(gadget
);
1487 if (!cdev
|| gi
->unbind
) {
1488 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1492 composite_disconnect(gadget
);
1493 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1496 static void configfs_composite_suspend(struct usb_gadget
*gadget
)
1498 struct usb_composite_dev
*cdev
;
1499 struct gadget_info
*gi
;
1500 unsigned long flags
;
1502 cdev
= get_gadget_data(gadget
);
1506 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1507 spin_lock_irqsave(&gi
->spinlock
, flags
);
1508 cdev
= get_gadget_data(gadget
);
1509 if (!cdev
|| gi
->unbind
) {
1510 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1514 composite_suspend(gadget
);
1515 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1518 static void configfs_composite_resume(struct usb_gadget
*gadget
)
1520 struct usb_composite_dev
*cdev
;
1521 struct gadget_info
*gi
;
1522 unsigned long flags
;
1524 cdev
= get_gadget_data(gadget
);
1528 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1529 spin_lock_irqsave(&gi
->spinlock
, flags
);
1530 cdev
= get_gadget_data(gadget
);
1531 if (!cdev
|| gi
->unbind
) {
1532 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1536 composite_resume(gadget
);
1537 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1540 static const struct usb_gadget_driver configfs_driver_template
= {
1541 .bind
= configfs_composite_bind
,
1542 .unbind
= configfs_composite_unbind
,
1544 .setup
= configfs_composite_setup
,
1545 .reset
= configfs_composite_disconnect
,
1546 .disconnect
= configfs_composite_disconnect
,
1548 .suspend
= configfs_composite_suspend
,
1549 .resume
= configfs_composite_resume
,
1551 .max_speed
= USB_SPEED_SUPER
,
1553 .owner
= THIS_MODULE
,
1554 .name
= "configfs-gadget",
1556 .match_existing_only
= 1,
1559 static struct config_group
*gadgets_make(
1560 struct config_group
*group
,
1563 struct gadget_info
*gi
;
1565 gi
= kzalloc(sizeof(*gi
), GFP_KERNEL
);
1567 return ERR_PTR(-ENOMEM
);
1569 config_group_init_type_name(&gi
->group
, name
, &gadget_root_type
);
1571 config_group_init_type_name(&gi
->functions_group
, "functions",
1573 configfs_add_default_group(&gi
->functions_group
, &gi
->group
);
1575 config_group_init_type_name(&gi
->configs_group
, "configs",
1577 configfs_add_default_group(&gi
->configs_group
, &gi
->group
);
1579 config_group_init_type_name(&gi
->strings_group
, "strings",
1580 &gadget_strings_strings_type
);
1581 configfs_add_default_group(&gi
->strings_group
, &gi
->group
);
1583 config_group_init_type_name(&gi
->os_desc_group
, "os_desc",
1585 configfs_add_default_group(&gi
->os_desc_group
, &gi
->group
);
1587 gi
->composite
.bind
= configfs_do_nothing
;
1588 gi
->composite
.unbind
= configfs_do_nothing
;
1589 gi
->composite
.suspend
= NULL
;
1590 gi
->composite
.resume
= NULL
;
1591 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
1593 spin_lock_init(&gi
->spinlock
);
1594 mutex_init(&gi
->lock
);
1595 INIT_LIST_HEAD(&gi
->string_list
);
1596 INIT_LIST_HEAD(&gi
->available_func
);
1598 composite_init_dev(&gi
->cdev
);
1599 gi
->cdev
.desc
.bLength
= USB_DT_DEVICE_SIZE
;
1600 gi
->cdev
.desc
.bDescriptorType
= USB_DT_DEVICE
;
1601 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(get_default_bcdDevice());
1603 gi
->composite
.gadget_driver
= configfs_driver_template
;
1605 gi
->composite
.gadget_driver
.function
= kstrdup(name
, GFP_KERNEL
);
1606 gi
->composite
.name
= gi
->composite
.gadget_driver
.function
;
1608 if (!gi
->composite
.gadget_driver
.function
)
1614 return ERR_PTR(-ENOMEM
);
1617 static void gadgets_drop(struct config_group
*group
, struct config_item
*item
)
1619 config_item_put(item
);
1622 static struct configfs_group_operations gadgets_ops
= {
1623 .make_group
= &gadgets_make
,
1624 .drop_item
= &gadgets_drop
,
1627 static const struct config_item_type gadgets_type
= {
1628 .ct_group_ops
= &gadgets_ops
,
1629 .ct_owner
= THIS_MODULE
,
1632 static struct configfs_subsystem gadget_subsys
= {
1635 .ci_namebuf
= "usb_gadget",
1636 .ci_type
= &gadgets_type
,
1639 .su_mutex
= __MUTEX_INITIALIZER(gadget_subsys
.su_mutex
),
1642 void unregister_gadget_item(struct config_item
*item
)
1644 struct gadget_info
*gi
= to_gadget_info(item
);
1646 mutex_lock(&gi
->lock
);
1647 unregister_gadget(gi
);
1648 mutex_unlock(&gi
->lock
);
1650 EXPORT_SYMBOL_GPL(unregister_gadget_item
);
1652 static int __init
gadget_cfs_init(void)
1656 config_group_init(&gadget_subsys
.su_group
);
1658 ret
= configfs_register_subsystem(&gadget_subsys
);
1661 module_init(gadget_cfs_init
);
1663 static void __exit
gadget_cfs_exit(void)
1665 configfs_unregister_subsystem(&gadget_subsys
);
1667 module_exit(gadget_cfs_exit
);