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 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceClass
);
300 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceSubClass
);
301 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceProtocol
);
302 CONFIGFS_ATTR(gadget_dev_desc_
, bMaxPacketSize0
);
303 CONFIGFS_ATTR(gadget_dev_desc_
, idVendor
);
304 CONFIGFS_ATTR(gadget_dev_desc_
, idProduct
);
305 CONFIGFS_ATTR(gadget_dev_desc_
, bcdDevice
);
306 CONFIGFS_ATTR(gadget_dev_desc_
, bcdUSB
);
307 CONFIGFS_ATTR(gadget_dev_desc_
, UDC
);
309 static struct configfs_attribute
*gadget_root_attrs
[] = {
310 &gadget_dev_desc_attr_bDeviceClass
,
311 &gadget_dev_desc_attr_bDeviceSubClass
,
312 &gadget_dev_desc_attr_bDeviceProtocol
,
313 &gadget_dev_desc_attr_bMaxPacketSize0
,
314 &gadget_dev_desc_attr_idVendor
,
315 &gadget_dev_desc_attr_idProduct
,
316 &gadget_dev_desc_attr_bcdDevice
,
317 &gadget_dev_desc_attr_bcdUSB
,
318 &gadget_dev_desc_attr_UDC
,
322 static inline struct gadget_strings
*to_gadget_strings(struct config_item
*item
)
324 return container_of(to_config_group(item
), struct gadget_strings
,
328 static inline struct gadget_config_name
*to_gadget_config_name(
329 struct config_item
*item
)
331 return container_of(to_config_group(item
), struct gadget_config_name
,
335 static inline struct usb_function_instance
*to_usb_function_instance(
336 struct config_item
*item
)
338 return container_of(to_config_group(item
),
339 struct usb_function_instance
, group
);
342 static void gadget_info_attr_release(struct config_item
*item
)
344 struct gadget_info
*gi
= to_gadget_info(item
);
346 WARN_ON(!list_empty(&gi
->cdev
.configs
));
347 WARN_ON(!list_empty(&gi
->string_list
));
348 WARN_ON(!list_empty(&gi
->available_func
));
349 kfree(gi
->composite
.gadget_driver
.function
);
353 static struct configfs_item_operations gadget_root_item_ops
= {
354 .release
= gadget_info_attr_release
,
357 static void gadget_config_attr_release(struct config_item
*item
)
359 struct config_usb_cfg
*cfg
= to_config_usb_cfg(item
);
361 WARN_ON(!list_empty(&cfg
->c
.functions
));
362 list_del(&cfg
->c
.list
);
367 static int config_usb_cfg_link(
368 struct config_item
*usb_cfg_ci
,
369 struct config_item
*usb_func_ci
)
371 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
372 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
373 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
375 struct config_group
*group
= to_config_group(usb_func_ci
);
376 struct usb_function_instance
*fi
= container_of(group
,
377 struct usb_function_instance
, group
);
378 struct usb_function_instance
*a_fi
;
379 struct usb_function
*f
;
382 mutex_lock(&gi
->lock
);
384 * Make sure this function is from within our _this_ gadget and not
385 * from another gadget or a random directory.
386 * Also a function instance can only be linked once.
388 list_for_each_entry(a_fi
, &gi
->available_func
, cfs_list
) {
397 list_for_each_entry(f
, &cfg
->func_list
, list
) {
404 f
= usb_get_function(fi
);
410 /* stash the function until we bind it to the gadget */
411 list_add_tail(&f
->list
, &cfg
->func_list
);
414 mutex_unlock(&gi
->lock
);
418 static void config_usb_cfg_unlink(
419 struct config_item
*usb_cfg_ci
,
420 struct config_item
*usb_func_ci
)
422 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
423 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
424 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
426 struct config_group
*group
= to_config_group(usb_func_ci
);
427 struct usb_function_instance
*fi
= container_of(group
,
428 struct usb_function_instance
, group
);
429 struct usb_function
*f
;
432 * ideally I would like to forbid to unlink functions while a gadget is
433 * bound to an UDC. Since this isn't possible at the moment, we simply
434 * force an unbind, the function is available here and then we can
435 * remove the function.
437 mutex_lock(&gi
->lock
);
438 if (gi
->composite
.gadget_driver
.udc_name
)
439 unregister_gadget(gi
);
440 WARN_ON(gi
->composite
.gadget_driver
.udc_name
);
442 list_for_each_entry(f
, &cfg
->func_list
, list
) {
446 mutex_unlock(&gi
->lock
);
450 mutex_unlock(&gi
->lock
);
451 WARN(1, "Unable to locate function to unbind\n");
454 static struct configfs_item_operations gadget_config_item_ops
= {
455 .release
= gadget_config_attr_release
,
456 .allow_link
= config_usb_cfg_link
,
457 .drop_link
= config_usb_cfg_unlink
,
461 static ssize_t
gadget_config_desc_MaxPower_show(struct config_item
*item
,
464 return sprintf(page
, "%u\n", to_config_usb_cfg(item
)->c
.MaxPower
);
467 static ssize_t
gadget_config_desc_MaxPower_store(struct config_item
*item
,
468 const char *page
, size_t len
)
472 ret
= kstrtou16(page
, 0, &val
);
475 if (DIV_ROUND_UP(val
, 8) > 0xff)
477 to_config_usb_cfg(item
)->c
.MaxPower
= val
;
481 static ssize_t
gadget_config_desc_bmAttributes_show(struct config_item
*item
,
484 return sprintf(page
, "0x%02x\n",
485 to_config_usb_cfg(item
)->c
.bmAttributes
);
488 static ssize_t
gadget_config_desc_bmAttributes_store(struct config_item
*item
,
489 const char *page
, size_t len
)
493 ret
= kstrtou8(page
, 0, &val
);
496 if (!(val
& USB_CONFIG_ATT_ONE
))
498 if (val
& ~(USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
|
499 USB_CONFIG_ATT_WAKEUP
))
501 to_config_usb_cfg(item
)->c
.bmAttributes
= val
;
505 CONFIGFS_ATTR(gadget_config_desc_
, MaxPower
);
506 CONFIGFS_ATTR(gadget_config_desc_
, bmAttributes
);
508 static struct configfs_attribute
*gadget_config_attrs
[] = {
509 &gadget_config_desc_attr_MaxPower
,
510 &gadget_config_desc_attr_bmAttributes
,
514 static const struct config_item_type gadget_config_type
= {
515 .ct_item_ops
= &gadget_config_item_ops
,
516 .ct_attrs
= gadget_config_attrs
,
517 .ct_owner
= THIS_MODULE
,
520 static const struct config_item_type gadget_root_type
= {
521 .ct_item_ops
= &gadget_root_item_ops
,
522 .ct_attrs
= gadget_root_attrs
,
523 .ct_owner
= THIS_MODULE
,
526 static void composite_init_dev(struct usb_composite_dev
*cdev
)
528 spin_lock_init(&cdev
->lock
);
529 INIT_LIST_HEAD(&cdev
->configs
);
530 INIT_LIST_HEAD(&cdev
->gstrings
);
533 static struct config_group
*function_make(
534 struct config_group
*group
,
537 struct gadget_info
*gi
;
538 struct usb_function_instance
*fi
;
539 char buf
[MAX_NAME_LEN
];
544 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
545 if (ret
>= MAX_NAME_LEN
)
546 return ERR_PTR(-ENAMETOOLONG
);
549 instance_name
= strchr(func_name
, '.');
550 if (!instance_name
) {
551 pr_err("Unable to locate . in FUNC.INSTANCE\n");
552 return ERR_PTR(-EINVAL
);
554 *instance_name
= '\0';
557 fi
= usb_get_function_instance(func_name
);
561 ret
= config_item_set_name(&fi
->group
.cg_item
, "%s", name
);
563 usb_put_function_instance(fi
);
566 if (fi
->set_inst_name
) {
567 ret
= fi
->set_inst_name(fi
, instance_name
);
569 usb_put_function_instance(fi
);
574 gi
= container_of(group
, struct gadget_info
, functions_group
);
576 mutex_lock(&gi
->lock
);
577 list_add_tail(&fi
->cfs_list
, &gi
->available_func
);
578 mutex_unlock(&gi
->lock
);
582 static void function_drop(
583 struct config_group
*group
,
584 struct config_item
*item
)
586 struct usb_function_instance
*fi
= to_usb_function_instance(item
);
587 struct gadget_info
*gi
;
589 gi
= container_of(group
, struct gadget_info
, functions_group
);
591 mutex_lock(&gi
->lock
);
592 list_del(&fi
->cfs_list
);
593 mutex_unlock(&gi
->lock
);
594 config_item_put(item
);
597 static struct configfs_group_operations functions_ops
= {
598 .make_group
= &function_make
,
599 .drop_item
= &function_drop
,
602 static const struct config_item_type functions_type
= {
603 .ct_group_ops
= &functions_ops
,
604 .ct_owner
= THIS_MODULE
,
607 GS_STRINGS_RW(gadget_config_name
, configuration
);
609 static struct configfs_attribute
*gadget_config_name_langid_attrs
[] = {
610 &gadget_config_name_attr_configuration
,
614 static void gadget_config_name_attr_release(struct config_item
*item
)
616 struct gadget_config_name
*cn
= to_gadget_config_name(item
);
618 kfree(cn
->configuration
);
624 USB_CONFIG_STRING_RW_OPS(gadget_config_name
);
625 USB_CONFIG_STRINGS_LANG(gadget_config_name
, config_usb_cfg
);
627 static struct config_group
*config_desc_make(
628 struct config_group
*group
,
631 struct gadget_info
*gi
;
632 struct config_usb_cfg
*cfg
;
633 char buf
[MAX_NAME_LEN
];
638 gi
= container_of(group
, struct gadget_info
, configs_group
);
639 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
640 if (ret
>= MAX_NAME_LEN
)
641 return ERR_PTR(-ENAMETOOLONG
);
643 num_str
= strchr(buf
, '.');
645 pr_err("Unable to locate . in name.bConfigurationValue\n");
646 return ERR_PTR(-EINVAL
);
653 return ERR_PTR(-EINVAL
);
655 ret
= kstrtou8(num_str
, 0, &num
);
659 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
661 return ERR_PTR(-ENOMEM
);
662 cfg
->c
.label
= kstrdup(buf
, GFP_KERNEL
);
667 cfg
->c
.bConfigurationValue
= num
;
668 cfg
->c
.MaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
;
669 cfg
->c
.bmAttributes
= USB_CONFIG_ATT_ONE
;
670 INIT_LIST_HEAD(&cfg
->string_list
);
671 INIT_LIST_HEAD(&cfg
->func_list
);
673 config_group_init_type_name(&cfg
->group
, name
,
674 &gadget_config_type
);
676 config_group_init_type_name(&cfg
->strings_group
, "strings",
677 &gadget_config_name_strings_type
);
678 configfs_add_default_group(&cfg
->strings_group
, &cfg
->group
);
680 ret
= usb_add_config_only(&gi
->cdev
, &cfg
->c
);
691 static void config_desc_drop(
692 struct config_group
*group
,
693 struct config_item
*item
)
695 config_item_put(item
);
698 static struct configfs_group_operations config_desc_ops
= {
699 .make_group
= &config_desc_make
,
700 .drop_item
= &config_desc_drop
,
703 static const struct config_item_type config_desc_type
= {
704 .ct_group_ops
= &config_desc_ops
,
705 .ct_owner
= THIS_MODULE
,
708 GS_STRINGS_RW(gadget_strings
, manufacturer
);
709 GS_STRINGS_RW(gadget_strings
, product
);
710 GS_STRINGS_RW(gadget_strings
, serialnumber
);
712 static struct configfs_attribute
*gadget_strings_langid_attrs
[] = {
713 &gadget_strings_attr_manufacturer
,
714 &gadget_strings_attr_product
,
715 &gadget_strings_attr_serialnumber
,
719 static void gadget_strings_attr_release(struct config_item
*item
)
721 struct gadget_strings
*gs
= to_gadget_strings(item
);
723 kfree(gs
->manufacturer
);
725 kfree(gs
->serialnumber
);
731 USB_CONFIG_STRING_RW_OPS(gadget_strings
);
732 USB_CONFIG_STRINGS_LANG(gadget_strings
, gadget_info
);
734 static inline struct os_desc
*to_os_desc(struct config_item
*item
)
736 return container_of(to_config_group(item
), struct os_desc
, group
);
739 static inline struct gadget_info
*os_desc_item_to_gadget_info(
740 struct config_item
*item
)
742 return to_gadget_info(to_os_desc(item
)->group
.cg_item
.ci_parent
);
745 static ssize_t
os_desc_use_show(struct config_item
*item
, char *page
)
747 return sprintf(page
, "%d\n",
748 os_desc_item_to_gadget_info(item
)->use_os_desc
);
751 static ssize_t
os_desc_use_store(struct config_item
*item
, const char *page
,
754 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
758 mutex_lock(&gi
->lock
);
759 ret
= strtobool(page
, &use
);
761 gi
->use_os_desc
= use
;
764 mutex_unlock(&gi
->lock
);
769 static ssize_t
os_desc_b_vendor_code_show(struct config_item
*item
, char *page
)
771 return sprintf(page
, "0x%02x\n",
772 os_desc_item_to_gadget_info(item
)->b_vendor_code
);
775 static ssize_t
os_desc_b_vendor_code_store(struct config_item
*item
,
776 const char *page
, size_t len
)
778 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
782 mutex_lock(&gi
->lock
);
783 ret
= kstrtou8(page
, 0, &b_vendor_code
);
785 gi
->b_vendor_code
= b_vendor_code
;
788 mutex_unlock(&gi
->lock
);
793 static ssize_t
os_desc_qw_sign_show(struct config_item
*item
, char *page
)
795 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
798 res
= utf16s_to_utf8s((wchar_t *) gi
->qw_sign
, OS_STRING_QW_SIGN_LEN
,
799 UTF16_LITTLE_ENDIAN
, page
, PAGE_SIZE
- 1);
805 static ssize_t
os_desc_qw_sign_store(struct config_item
*item
, const char *page
,
808 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
811 l
= min((int)len
, OS_STRING_QW_SIGN_LEN
>> 1);
812 if (page
[l
- 1] == '\n')
815 mutex_lock(&gi
->lock
);
816 res
= utf8s_to_utf16s(page
, l
,
817 UTF16_LITTLE_ENDIAN
, (wchar_t *) gi
->qw_sign
,
818 OS_STRING_QW_SIGN_LEN
);
821 mutex_unlock(&gi
->lock
);
826 CONFIGFS_ATTR(os_desc_
, use
);
827 CONFIGFS_ATTR(os_desc_
, b_vendor_code
);
828 CONFIGFS_ATTR(os_desc_
, qw_sign
);
830 static struct configfs_attribute
*os_desc_attrs
[] = {
832 &os_desc_attr_b_vendor_code
,
833 &os_desc_attr_qw_sign
,
837 static void os_desc_attr_release(struct config_item
*item
)
839 struct os_desc
*os_desc
= to_os_desc(item
);
843 static int os_desc_link(struct config_item
*os_desc_ci
,
844 struct config_item
*usb_cfg_ci
)
846 struct gadget_info
*gi
= container_of(to_config_group(os_desc_ci
),
847 struct gadget_info
, os_desc_group
);
848 struct usb_composite_dev
*cdev
= &gi
->cdev
;
849 struct config_usb_cfg
*c_target
=
850 container_of(to_config_group(usb_cfg_ci
),
851 struct config_usb_cfg
, group
);
852 struct usb_configuration
*c
;
855 mutex_lock(&gi
->lock
);
856 list_for_each_entry(c
, &cdev
->configs
, list
) {
857 if (c
== &c_target
->c
)
860 if (c
!= &c_target
->c
) {
865 if (cdev
->os_desc_config
) {
870 cdev
->os_desc_config
= &c_target
->c
;
874 mutex_unlock(&gi
->lock
);
878 static void os_desc_unlink(struct config_item
*os_desc_ci
,
879 struct config_item
*usb_cfg_ci
)
881 struct gadget_info
*gi
= container_of(to_config_group(os_desc_ci
),
882 struct gadget_info
, os_desc_group
);
883 struct usb_composite_dev
*cdev
= &gi
->cdev
;
885 mutex_lock(&gi
->lock
);
886 if (gi
->composite
.gadget_driver
.udc_name
)
887 unregister_gadget(gi
);
888 cdev
->os_desc_config
= NULL
;
889 WARN_ON(gi
->composite
.gadget_driver
.udc_name
);
890 mutex_unlock(&gi
->lock
);
893 static struct configfs_item_operations os_desc_ops
= {
894 .release
= os_desc_attr_release
,
895 .allow_link
= os_desc_link
,
896 .drop_link
= os_desc_unlink
,
899 static struct config_item_type os_desc_type
= {
900 .ct_item_ops
= &os_desc_ops
,
901 .ct_attrs
= os_desc_attrs
,
902 .ct_owner
= THIS_MODULE
,
905 static inline struct usb_os_desc_ext_prop
906 *to_usb_os_desc_ext_prop(struct config_item
*item
)
908 return container_of(item
, struct usb_os_desc_ext_prop
, item
);
911 static ssize_t
ext_prop_type_show(struct config_item
*item
, char *page
)
913 return sprintf(page
, "%d\n", to_usb_os_desc_ext_prop(item
)->type
);
916 static ssize_t
ext_prop_type_store(struct config_item
*item
,
917 const char *page
, size_t len
)
919 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
920 struct usb_os_desc
*desc
= to_usb_os_desc(ext_prop
->item
.ci_parent
);
924 if (desc
->opts_mutex
)
925 mutex_lock(desc
->opts_mutex
);
926 ret
= kstrtou8(page
, 0, &type
);
929 if (type
< USB_EXT_PROP_UNICODE
|| type
> USB_EXT_PROP_UNICODE_MULTI
) {
934 if ((ext_prop
->type
== USB_EXT_PROP_BINARY
||
935 ext_prop
->type
== USB_EXT_PROP_LE32
||
936 ext_prop
->type
== USB_EXT_PROP_BE32
) &&
937 (type
== USB_EXT_PROP_UNICODE
||
938 type
== USB_EXT_PROP_UNICODE_ENV
||
939 type
== USB_EXT_PROP_UNICODE_LINK
))
940 ext_prop
->data_len
<<= 1;
941 else if ((ext_prop
->type
== USB_EXT_PROP_UNICODE
||
942 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
943 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
) &&
944 (type
== USB_EXT_PROP_BINARY
||
945 type
== USB_EXT_PROP_LE32
||
946 type
== USB_EXT_PROP_BE32
))
947 ext_prop
->data_len
>>= 1;
948 ext_prop
->type
= type
;
952 if (desc
->opts_mutex
)
953 mutex_unlock(desc
->opts_mutex
);
957 static ssize_t
ext_prop_data_show(struct config_item
*item
, char *page
)
959 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
960 int len
= ext_prop
->data_len
;
962 if (ext_prop
->type
== USB_EXT_PROP_UNICODE
||
963 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
964 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
)
966 memcpy(page
, ext_prop
->data
, len
);
971 static ssize_t
ext_prop_data_store(struct config_item
*item
,
972 const char *page
, size_t len
)
974 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
975 struct usb_os_desc
*desc
= to_usb_os_desc(ext_prop
->item
.ci_parent
);
977 size_t ret_len
= len
;
979 if (page
[len
- 1] == '\n' || page
[len
- 1] == '\0')
981 new_data
= kmemdup(page
, len
, GFP_KERNEL
);
985 if (desc
->opts_mutex
)
986 mutex_lock(desc
->opts_mutex
);
987 kfree(ext_prop
->data
);
988 ext_prop
->data
= new_data
;
989 desc
->ext_prop_len
-= ext_prop
->data_len
;
990 ext_prop
->data_len
= len
;
991 desc
->ext_prop_len
+= ext_prop
->data_len
;
992 if (ext_prop
->type
== USB_EXT_PROP_UNICODE
||
993 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
994 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
) {
995 desc
->ext_prop_len
-= ext_prop
->data_len
;
996 ext_prop
->data_len
<<= 1;
997 ext_prop
->data_len
+= 2;
998 desc
->ext_prop_len
+= ext_prop
->data_len
;
1000 if (desc
->opts_mutex
)
1001 mutex_unlock(desc
->opts_mutex
);
1005 CONFIGFS_ATTR(ext_prop_
, type
);
1006 CONFIGFS_ATTR(ext_prop_
, data
);
1008 static struct configfs_attribute
*ext_prop_attrs
[] = {
1009 &ext_prop_attr_type
,
1010 &ext_prop_attr_data
,
1014 static void usb_os_desc_ext_prop_release(struct config_item
*item
)
1016 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1018 kfree(ext_prop
); /* frees a whole chunk */
1021 static struct configfs_item_operations ext_prop_ops
= {
1022 .release
= usb_os_desc_ext_prop_release
,
1025 static struct config_item
*ext_prop_make(
1026 struct config_group
*group
,
1029 struct usb_os_desc_ext_prop
*ext_prop
;
1030 struct config_item_type
*ext_prop_type
;
1031 struct usb_os_desc
*desc
;
1034 vla_group(data_chunk
);
1035 vla_item(data_chunk
, struct usb_os_desc_ext_prop
, ext_prop
, 1);
1036 vla_item(data_chunk
, struct config_item_type
, ext_prop_type
, 1);
1038 vlabuf
= kzalloc(vla_group_size(data_chunk
), GFP_KERNEL
);
1040 return ERR_PTR(-ENOMEM
);
1042 ext_prop
= vla_ptr(vlabuf
, data_chunk
, ext_prop
);
1043 ext_prop_type
= vla_ptr(vlabuf
, data_chunk
, ext_prop_type
);
1045 desc
= container_of(group
, struct usb_os_desc
, group
);
1046 ext_prop_type
->ct_item_ops
= &ext_prop_ops
;
1047 ext_prop_type
->ct_attrs
= ext_prop_attrs
;
1048 ext_prop_type
->ct_owner
= desc
->owner
;
1050 config_item_init_type_name(&ext_prop
->item
, name
, ext_prop_type
);
1052 ext_prop
->name
= kstrdup(name
, GFP_KERNEL
);
1053 if (!ext_prop
->name
) {
1055 return ERR_PTR(-ENOMEM
);
1057 desc
->ext_prop_len
+= 14;
1058 ext_prop
->name_len
= 2 * strlen(ext_prop
->name
) + 2;
1059 if (desc
->opts_mutex
)
1060 mutex_lock(desc
->opts_mutex
);
1061 desc
->ext_prop_len
+= ext_prop
->name_len
;
1062 list_add_tail(&ext_prop
->entry
, &desc
->ext_prop
);
1063 ++desc
->ext_prop_count
;
1064 if (desc
->opts_mutex
)
1065 mutex_unlock(desc
->opts_mutex
);
1067 return &ext_prop
->item
;
1070 static void ext_prop_drop(struct config_group
*group
, struct config_item
*item
)
1072 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1073 struct usb_os_desc
*desc
= to_usb_os_desc(&group
->cg_item
);
1075 if (desc
->opts_mutex
)
1076 mutex_lock(desc
->opts_mutex
);
1077 list_del(&ext_prop
->entry
);
1078 --desc
->ext_prop_count
;
1079 kfree(ext_prop
->name
);
1080 desc
->ext_prop_len
-= (ext_prop
->name_len
+ ext_prop
->data_len
+ 14);
1081 if (desc
->opts_mutex
)
1082 mutex_unlock(desc
->opts_mutex
);
1083 config_item_put(item
);
1086 static struct configfs_group_operations interf_grp_ops
= {
1087 .make_item
= &ext_prop_make
,
1088 .drop_item
= &ext_prop_drop
,
1091 static ssize_t
interf_grp_compatible_id_show(struct config_item
*item
,
1094 memcpy(page
, to_usb_os_desc(item
)->ext_compat_id
, 8);
1098 static ssize_t
interf_grp_compatible_id_store(struct config_item
*item
,
1099 const char *page
, size_t len
)
1101 struct usb_os_desc
*desc
= to_usb_os_desc(item
);
1104 l
= min_t(int, 8, len
);
1105 if (page
[l
- 1] == '\n')
1107 if (desc
->opts_mutex
)
1108 mutex_lock(desc
->opts_mutex
);
1109 memcpy(desc
->ext_compat_id
, page
, l
);
1111 if (desc
->opts_mutex
)
1112 mutex_unlock(desc
->opts_mutex
);
1117 static ssize_t
interf_grp_sub_compatible_id_show(struct config_item
*item
,
1120 memcpy(page
, to_usb_os_desc(item
)->ext_compat_id
+ 8, 8);
1124 static ssize_t
interf_grp_sub_compatible_id_store(struct config_item
*item
,
1125 const char *page
, size_t len
)
1127 struct usb_os_desc
*desc
= to_usb_os_desc(item
);
1130 l
= min_t(int, 8, len
);
1131 if (page
[l
- 1] == '\n')
1133 if (desc
->opts_mutex
)
1134 mutex_lock(desc
->opts_mutex
);
1135 memcpy(desc
->ext_compat_id
+ 8, page
, l
);
1137 if (desc
->opts_mutex
)
1138 mutex_unlock(desc
->opts_mutex
);
1143 CONFIGFS_ATTR(interf_grp_
, compatible_id
);
1144 CONFIGFS_ATTR(interf_grp_
, sub_compatible_id
);
1146 static struct configfs_attribute
*interf_grp_attrs
[] = {
1147 &interf_grp_attr_compatible_id
,
1148 &interf_grp_attr_sub_compatible_id
,
1152 struct config_group
*usb_os_desc_prepare_interf_dir(
1153 struct config_group
*parent
,
1155 struct usb_os_desc
**desc
,
1157 struct module
*owner
)
1159 struct config_group
*os_desc_group
;
1160 struct config_item_type
*os_desc_type
, *interface_type
;
1162 vla_group(data_chunk
);
1163 vla_item(data_chunk
, struct config_group
, os_desc_group
, 1);
1164 vla_item(data_chunk
, struct config_item_type
, os_desc_type
, 1);
1165 vla_item(data_chunk
, struct config_item_type
, interface_type
, 1);
1167 char *vlabuf
= kzalloc(vla_group_size(data_chunk
), GFP_KERNEL
);
1169 return ERR_PTR(-ENOMEM
);
1171 os_desc_group
= vla_ptr(vlabuf
, data_chunk
, os_desc_group
);
1172 os_desc_type
= vla_ptr(vlabuf
, data_chunk
, os_desc_type
);
1173 interface_type
= vla_ptr(vlabuf
, data_chunk
, interface_type
);
1175 os_desc_type
->ct_owner
= owner
;
1176 config_group_init_type_name(os_desc_group
, "os_desc", os_desc_type
);
1177 configfs_add_default_group(os_desc_group
, parent
);
1179 interface_type
->ct_group_ops
= &interf_grp_ops
;
1180 interface_type
->ct_attrs
= interf_grp_attrs
;
1181 interface_type
->ct_owner
= owner
;
1183 while (n_interf
--) {
1184 struct usb_os_desc
*d
;
1188 config_group_init_type_name(&d
->group
, "", interface_type
);
1189 config_item_set_name(&d
->group
.cg_item
, "interface.%s",
1191 configfs_add_default_group(&d
->group
, os_desc_group
);
1194 return os_desc_group
;
1196 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir
);
1198 static int configfs_do_nothing(struct usb_composite_dev
*cdev
)
1204 int composite_dev_prepare(struct usb_composite_driver
*composite
,
1205 struct usb_composite_dev
*dev
);
1207 int composite_os_desc_req_prepare(struct usb_composite_dev
*cdev
,
1208 struct usb_ep
*ep0
);
1210 static void purge_configs_funcs(struct gadget_info
*gi
)
1212 struct usb_configuration
*c
;
1214 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1215 struct usb_function
*f
, *tmp
;
1216 struct config_usb_cfg
*cfg
;
1218 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1220 list_for_each_entry_safe(f
, tmp
, &c
->functions
, list
) {
1222 list_move_tail(&f
->list
, &cfg
->func_list
);
1224 dev_dbg(&gi
->cdev
.gadget
->dev
,
1225 "unbind function '%s'/%p\n",
1230 c
->next_interface_id
= 0;
1231 memset(c
->interface
, 0, sizeof(c
->interface
));
1232 c
->superspeed_plus
= 0;
1239 static int configfs_composite_bind(struct usb_gadget
*gadget
,
1240 struct usb_gadget_driver
*gdriver
)
1242 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1243 struct gadget_info
*gi
= container_of(composite
,
1244 struct gadget_info
, composite
);
1245 struct usb_composite_dev
*cdev
= &gi
->cdev
;
1246 struct usb_configuration
*c
;
1247 struct usb_string
*s
;
1251 /* the gi->lock is hold by the caller */
1253 cdev
->gadget
= gadget
;
1254 set_gadget_data(gadget
, cdev
);
1255 ret
= composite_dev_prepare(composite
, cdev
);
1258 /* and now the gadget bind */
1261 if (list_empty(&gi
->cdev
.configs
)) {
1262 pr_err("Need at least one configuration in %s.\n",
1263 gi
->composite
.name
);
1264 goto err_comp_cleanup
;
1268 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1269 struct config_usb_cfg
*cfg
;
1271 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1272 if (list_empty(&cfg
->func_list
)) {
1273 pr_err("Config %s/%d of %s needs at least one function.\n",
1274 c
->label
, c
->bConfigurationValue
,
1275 gi
->composite
.name
);
1276 goto err_comp_cleanup
;
1280 /* init all strings */
1281 if (!list_empty(&gi
->string_list
)) {
1282 struct gadget_strings
*gs
;
1285 list_for_each_entry(gs
, &gi
->string_list
, list
) {
1287 gi
->gstrings
[i
] = &gs
->stringtab_dev
;
1288 gs
->stringtab_dev
.strings
= gs
->strings
;
1289 gs
->strings
[USB_GADGET_MANUFACTURER_IDX
].s
=
1291 gs
->strings
[USB_GADGET_PRODUCT_IDX
].s
= gs
->product
;
1292 gs
->strings
[USB_GADGET_SERIAL_IDX
].s
= gs
->serialnumber
;
1295 gi
->gstrings
[i
] = NULL
;
1296 s
= usb_gstrings_attach(&gi
->cdev
, gi
->gstrings
,
1297 USB_GADGET_FIRST_AVAIL_IDX
);
1300 goto err_comp_cleanup
;
1303 gi
->cdev
.desc
.iManufacturer
= s
[USB_GADGET_MANUFACTURER_IDX
].id
;
1304 gi
->cdev
.desc
.iProduct
= s
[USB_GADGET_PRODUCT_IDX
].id
;
1305 gi
->cdev
.desc
.iSerialNumber
= s
[USB_GADGET_SERIAL_IDX
].id
;
1308 if (gi
->use_os_desc
) {
1309 cdev
->use_os_string
= true;
1310 cdev
->b_vendor_code
= gi
->b_vendor_code
;
1311 memcpy(cdev
->qw_sign
, gi
->qw_sign
, OS_STRING_QW_SIGN_LEN
);
1314 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
1315 struct usb_descriptor_header
*usb_desc
;
1317 usb_desc
= usb_otg_descriptor_alloc(gadget
);
1320 goto err_comp_cleanup
;
1322 usb_otg_descriptor_init(gadget
, usb_desc
);
1323 otg_desc
[0] = usb_desc
;
1327 /* Go through all configs, attach all functions */
1328 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1329 struct config_usb_cfg
*cfg
;
1330 struct usb_function
*f
;
1331 struct usb_function
*tmp
;
1332 struct gadget_config_name
*cn
;
1334 if (gadget_is_otg(gadget
))
1335 c
->descriptors
= otg_desc
;
1337 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1338 if (!list_empty(&cfg
->string_list
)) {
1340 list_for_each_entry(cn
, &cfg
->string_list
, list
) {
1341 cfg
->gstrings
[i
] = &cn
->stringtab_dev
;
1342 cn
->stringtab_dev
.strings
= &cn
->strings
;
1343 cn
->strings
.s
= cn
->configuration
;
1346 cfg
->gstrings
[i
] = NULL
;
1347 s
= usb_gstrings_attach(&gi
->cdev
, cfg
->gstrings
, 1);
1350 goto err_comp_cleanup
;
1352 c
->iConfiguration
= s
[0].id
;
1355 list_for_each_entry_safe(f
, tmp
, &cfg
->func_list
, list
) {
1357 ret
= usb_add_function(c
, f
);
1359 list_add(&f
->list
, &cfg
->func_list
);
1360 goto err_purge_funcs
;
1363 usb_ep_autoconfig_reset(cdev
->gadget
);
1365 if (cdev
->use_os_string
) {
1366 ret
= composite_os_desc_req_prepare(cdev
, gadget
->ep0
);
1368 goto err_purge_funcs
;
1371 usb_ep_autoconfig_reset(cdev
->gadget
);
1375 purge_configs_funcs(gi
);
1377 composite_dev_cleanup(cdev
);
1381 static void configfs_composite_unbind(struct usb_gadget
*gadget
)
1383 struct usb_composite_dev
*cdev
;
1384 struct gadget_info
*gi
;
1385 unsigned long flags
;
1387 /* the gi->lock is hold by the caller */
1389 cdev
= get_gadget_data(gadget
);
1390 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1391 spin_lock_irqsave(&gi
->spinlock
, flags
);
1393 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1397 purge_configs_funcs(gi
);
1398 composite_dev_cleanup(cdev
);
1399 usb_ep_autoconfig_reset(cdev
->gadget
);
1400 spin_lock_irqsave(&gi
->spinlock
, flags
);
1401 cdev
->gadget
= NULL
;
1402 set_gadget_data(gadget
, NULL
);
1403 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1406 static int configfs_composite_setup(struct usb_gadget
*gadget
,
1407 const struct usb_ctrlrequest
*ctrl
)
1409 struct usb_composite_dev
*cdev
;
1410 struct gadget_info
*gi
;
1411 unsigned long flags
;
1414 cdev
= get_gadget_data(gadget
);
1418 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1419 spin_lock_irqsave(&gi
->spinlock
, flags
);
1420 cdev
= get_gadget_data(gadget
);
1421 if (!cdev
|| gi
->unbind
) {
1422 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1426 ret
= composite_setup(gadget
, ctrl
);
1427 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1431 static void configfs_composite_disconnect(struct usb_gadget
*gadget
)
1433 struct usb_composite_dev
*cdev
;
1434 struct gadget_info
*gi
;
1435 unsigned long flags
;
1437 cdev
= get_gadget_data(gadget
);
1441 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1442 spin_lock_irqsave(&gi
->spinlock
, flags
);
1443 cdev
= get_gadget_data(gadget
);
1444 if (!cdev
|| gi
->unbind
) {
1445 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1449 composite_disconnect(gadget
);
1450 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1453 static void configfs_composite_suspend(struct usb_gadget
*gadget
)
1455 struct usb_composite_dev
*cdev
;
1456 struct gadget_info
*gi
;
1457 unsigned long flags
;
1459 cdev
= get_gadget_data(gadget
);
1463 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1464 spin_lock_irqsave(&gi
->spinlock
, flags
);
1465 cdev
= get_gadget_data(gadget
);
1466 if (!cdev
|| gi
->unbind
) {
1467 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1471 composite_suspend(gadget
);
1472 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1475 static void configfs_composite_resume(struct usb_gadget
*gadget
)
1477 struct usb_composite_dev
*cdev
;
1478 struct gadget_info
*gi
;
1479 unsigned long flags
;
1481 cdev
= get_gadget_data(gadget
);
1485 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1486 spin_lock_irqsave(&gi
->spinlock
, flags
);
1487 cdev
= get_gadget_data(gadget
);
1488 if (!cdev
|| gi
->unbind
) {
1489 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1493 composite_resume(gadget
);
1494 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1497 static const struct usb_gadget_driver configfs_driver_template
= {
1498 .bind
= configfs_composite_bind
,
1499 .unbind
= configfs_composite_unbind
,
1501 .setup
= configfs_composite_setup
,
1502 .reset
= configfs_composite_disconnect
,
1503 .disconnect
= configfs_composite_disconnect
,
1505 .suspend
= configfs_composite_suspend
,
1506 .resume
= configfs_composite_resume
,
1508 .max_speed
= USB_SPEED_SUPER
,
1510 .owner
= THIS_MODULE
,
1511 .name
= "configfs-gadget",
1513 .match_existing_only
= 1,
1516 static struct config_group
*gadgets_make(
1517 struct config_group
*group
,
1520 struct gadget_info
*gi
;
1522 gi
= kzalloc(sizeof(*gi
), GFP_KERNEL
);
1524 return ERR_PTR(-ENOMEM
);
1526 config_group_init_type_name(&gi
->group
, name
, &gadget_root_type
);
1528 config_group_init_type_name(&gi
->functions_group
, "functions",
1530 configfs_add_default_group(&gi
->functions_group
, &gi
->group
);
1532 config_group_init_type_name(&gi
->configs_group
, "configs",
1534 configfs_add_default_group(&gi
->configs_group
, &gi
->group
);
1536 config_group_init_type_name(&gi
->strings_group
, "strings",
1537 &gadget_strings_strings_type
);
1538 configfs_add_default_group(&gi
->strings_group
, &gi
->group
);
1540 config_group_init_type_name(&gi
->os_desc_group
, "os_desc",
1542 configfs_add_default_group(&gi
->os_desc_group
, &gi
->group
);
1544 gi
->composite
.bind
= configfs_do_nothing
;
1545 gi
->composite
.unbind
= configfs_do_nothing
;
1546 gi
->composite
.suspend
= NULL
;
1547 gi
->composite
.resume
= NULL
;
1548 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
1550 spin_lock_init(&gi
->spinlock
);
1551 mutex_init(&gi
->lock
);
1552 INIT_LIST_HEAD(&gi
->string_list
);
1553 INIT_LIST_HEAD(&gi
->available_func
);
1555 composite_init_dev(&gi
->cdev
);
1556 gi
->cdev
.desc
.bLength
= USB_DT_DEVICE_SIZE
;
1557 gi
->cdev
.desc
.bDescriptorType
= USB_DT_DEVICE
;
1558 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(get_default_bcdDevice());
1560 gi
->composite
.gadget_driver
= configfs_driver_template
;
1562 gi
->composite
.gadget_driver
.function
= kstrdup(name
, GFP_KERNEL
);
1563 gi
->composite
.name
= gi
->composite
.gadget_driver
.function
;
1565 if (!gi
->composite
.gadget_driver
.function
)
1571 return ERR_PTR(-ENOMEM
);
1574 static void gadgets_drop(struct config_group
*group
, struct config_item
*item
)
1576 config_item_put(item
);
1579 static struct configfs_group_operations gadgets_ops
= {
1580 .make_group
= &gadgets_make
,
1581 .drop_item
= &gadgets_drop
,
1584 static const struct config_item_type gadgets_type
= {
1585 .ct_group_ops
= &gadgets_ops
,
1586 .ct_owner
= THIS_MODULE
,
1589 static struct configfs_subsystem gadget_subsys
= {
1592 .ci_namebuf
= "usb_gadget",
1593 .ci_type
= &gadgets_type
,
1596 .su_mutex
= __MUTEX_INITIALIZER(gadget_subsys
.su_mutex
),
1599 void unregister_gadget_item(struct config_item
*item
)
1601 struct gadget_info
*gi
= to_gadget_info(item
);
1603 mutex_lock(&gi
->lock
);
1604 unregister_gadget(gi
);
1605 mutex_unlock(&gi
->lock
);
1607 EXPORT_SYMBOL_GPL(unregister_gadget_item
);
1609 static int __init
gadget_cfs_init(void)
1613 config_group_init(&gadget_subsys
.su_group
);
1615 ret
= configfs_register_subsystem(&gadget_subsys
);
1618 module_init(gadget_cfs_init
);
1620 static void __exit
gadget_cfs_exit(void)
1622 configfs_unregister_subsystem(&gadget_subsys
);
1624 module_exit(gadget_cfs_exit
);