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 name
= kstrdup(page
, GFP_KERNEL
);
266 if (name
[len
- 1] == '\n')
267 name
[len
- 1] = '\0';
269 mutex_lock(&gi
->lock
);
272 ret
= unregister_gadget(gi
);
277 if (gi
->composite
.gadget_driver
.udc_name
) {
281 gi
->composite
.gadget_driver
.udc_name
= name
;
282 ret
= usb_gadget_probe_driver(&gi
->composite
.gadget_driver
);
284 gi
->composite
.gadget_driver
.udc_name
= NULL
;
288 mutex_unlock(&gi
->lock
);
292 mutex_unlock(&gi
->lock
);
296 static ssize_t
gadget_dev_desc_max_speed_show(struct config_item
*item
,
299 enum usb_device_speed speed
= to_gadget_info(item
)->composite
.max_speed
;
301 return sprintf(page
, "%s\n", usb_speed_string(speed
));
304 static ssize_t
gadget_dev_desc_max_speed_store(struct config_item
*item
,
305 const char *page
, size_t len
)
307 struct gadget_info
*gi
= to_gadget_info(item
);
309 mutex_lock(&gi
->lock
);
311 /* Prevent changing of max_speed after the driver is binded */
312 if (gi
->composite
.gadget_driver
.udc_name
)
315 if (strncmp(page
, "super-speed-plus", 16) == 0)
316 gi
->composite
.max_speed
= USB_SPEED_SUPER_PLUS
;
317 else if (strncmp(page
, "super-speed", 11) == 0)
318 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
319 else if (strncmp(page
, "high-speed", 10) == 0)
320 gi
->composite
.max_speed
= USB_SPEED_HIGH
;
321 else if (strncmp(page
, "full-speed", 10) == 0)
322 gi
->composite
.max_speed
= USB_SPEED_FULL
;
323 else if (strncmp(page
, "low-speed", 9) == 0)
324 gi
->composite
.max_speed
= USB_SPEED_LOW
;
328 gi
->composite
.gadget_driver
.max_speed
= gi
->composite
.max_speed
;
330 mutex_unlock(&gi
->lock
);
333 mutex_unlock(&gi
->lock
);
337 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceClass
);
338 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceSubClass
);
339 CONFIGFS_ATTR(gadget_dev_desc_
, bDeviceProtocol
);
340 CONFIGFS_ATTR(gadget_dev_desc_
, bMaxPacketSize0
);
341 CONFIGFS_ATTR(gadget_dev_desc_
, idVendor
);
342 CONFIGFS_ATTR(gadget_dev_desc_
, idProduct
);
343 CONFIGFS_ATTR(gadget_dev_desc_
, bcdDevice
);
344 CONFIGFS_ATTR(gadget_dev_desc_
, bcdUSB
);
345 CONFIGFS_ATTR(gadget_dev_desc_
, UDC
);
346 CONFIGFS_ATTR(gadget_dev_desc_
, max_speed
);
348 static struct configfs_attribute
*gadget_root_attrs
[] = {
349 &gadget_dev_desc_attr_bDeviceClass
,
350 &gadget_dev_desc_attr_bDeviceSubClass
,
351 &gadget_dev_desc_attr_bDeviceProtocol
,
352 &gadget_dev_desc_attr_bMaxPacketSize0
,
353 &gadget_dev_desc_attr_idVendor
,
354 &gadget_dev_desc_attr_idProduct
,
355 &gadget_dev_desc_attr_bcdDevice
,
356 &gadget_dev_desc_attr_bcdUSB
,
357 &gadget_dev_desc_attr_UDC
,
358 &gadget_dev_desc_attr_max_speed
,
362 static inline struct gadget_strings
*to_gadget_strings(struct config_item
*item
)
364 return container_of(to_config_group(item
), struct gadget_strings
,
368 static inline struct gadget_config_name
*to_gadget_config_name(
369 struct config_item
*item
)
371 return container_of(to_config_group(item
), struct gadget_config_name
,
375 static inline struct usb_function_instance
*to_usb_function_instance(
376 struct config_item
*item
)
378 return container_of(to_config_group(item
),
379 struct usb_function_instance
, group
);
382 static void gadget_info_attr_release(struct config_item
*item
)
384 struct gadget_info
*gi
= to_gadget_info(item
);
386 WARN_ON(!list_empty(&gi
->cdev
.configs
));
387 WARN_ON(!list_empty(&gi
->string_list
));
388 WARN_ON(!list_empty(&gi
->available_func
));
389 kfree(gi
->composite
.gadget_driver
.function
);
393 static struct configfs_item_operations gadget_root_item_ops
= {
394 .release
= gadget_info_attr_release
,
397 static void gadget_config_attr_release(struct config_item
*item
)
399 struct config_usb_cfg
*cfg
= to_config_usb_cfg(item
);
401 WARN_ON(!list_empty(&cfg
->c
.functions
));
402 list_del(&cfg
->c
.list
);
407 static int config_usb_cfg_link(
408 struct config_item
*usb_cfg_ci
,
409 struct config_item
*usb_func_ci
)
411 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
412 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
413 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
415 struct config_group
*group
= to_config_group(usb_func_ci
);
416 struct usb_function_instance
*fi
= container_of(group
,
417 struct usb_function_instance
, group
);
418 struct usb_function_instance
*a_fi
;
419 struct usb_function
*f
;
422 mutex_lock(&gi
->lock
);
424 * Make sure this function is from within our _this_ gadget and not
425 * from another gadget or a random directory.
426 * Also a function instance can only be linked once.
428 list_for_each_entry(a_fi
, &gi
->available_func
, cfs_list
) {
437 list_for_each_entry(f
, &cfg
->func_list
, list
) {
444 f
= usb_get_function(fi
);
450 /* stash the function until we bind it to the gadget */
451 list_add_tail(&f
->list
, &cfg
->func_list
);
454 mutex_unlock(&gi
->lock
);
458 static void config_usb_cfg_unlink(
459 struct config_item
*usb_cfg_ci
,
460 struct config_item
*usb_func_ci
)
462 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
463 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
464 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
466 struct config_group
*group
= to_config_group(usb_func_ci
);
467 struct usb_function_instance
*fi
= container_of(group
,
468 struct usb_function_instance
, group
);
469 struct usb_function
*f
;
472 * ideally I would like to forbid to unlink functions while a gadget is
473 * bound to an UDC. Since this isn't possible at the moment, we simply
474 * force an unbind, the function is available here and then we can
475 * remove the function.
477 mutex_lock(&gi
->lock
);
478 if (gi
->composite
.gadget_driver
.udc_name
)
479 unregister_gadget(gi
);
480 WARN_ON(gi
->composite
.gadget_driver
.udc_name
);
482 list_for_each_entry(f
, &cfg
->func_list
, list
) {
486 mutex_unlock(&gi
->lock
);
490 mutex_unlock(&gi
->lock
);
491 WARN(1, "Unable to locate function to unbind\n");
494 static struct configfs_item_operations gadget_config_item_ops
= {
495 .release
= gadget_config_attr_release
,
496 .allow_link
= config_usb_cfg_link
,
497 .drop_link
= config_usb_cfg_unlink
,
501 static ssize_t
gadget_config_desc_MaxPower_show(struct config_item
*item
,
504 return sprintf(page
, "%u\n", to_config_usb_cfg(item
)->c
.MaxPower
);
507 static ssize_t
gadget_config_desc_MaxPower_store(struct config_item
*item
,
508 const char *page
, size_t len
)
512 ret
= kstrtou16(page
, 0, &val
);
515 if (DIV_ROUND_UP(val
, 8) > 0xff)
517 to_config_usb_cfg(item
)->c
.MaxPower
= val
;
521 static ssize_t
gadget_config_desc_bmAttributes_show(struct config_item
*item
,
524 return sprintf(page
, "0x%02x\n",
525 to_config_usb_cfg(item
)->c
.bmAttributes
);
528 static ssize_t
gadget_config_desc_bmAttributes_store(struct config_item
*item
,
529 const char *page
, size_t len
)
533 ret
= kstrtou8(page
, 0, &val
);
536 if (!(val
& USB_CONFIG_ATT_ONE
))
538 if (val
& ~(USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
|
539 USB_CONFIG_ATT_WAKEUP
))
541 to_config_usb_cfg(item
)->c
.bmAttributes
= val
;
545 CONFIGFS_ATTR(gadget_config_desc_
, MaxPower
);
546 CONFIGFS_ATTR(gadget_config_desc_
, bmAttributes
);
548 static struct configfs_attribute
*gadget_config_attrs
[] = {
549 &gadget_config_desc_attr_MaxPower
,
550 &gadget_config_desc_attr_bmAttributes
,
554 static const struct config_item_type gadget_config_type
= {
555 .ct_item_ops
= &gadget_config_item_ops
,
556 .ct_attrs
= gadget_config_attrs
,
557 .ct_owner
= THIS_MODULE
,
560 static const struct config_item_type gadget_root_type
= {
561 .ct_item_ops
= &gadget_root_item_ops
,
562 .ct_attrs
= gadget_root_attrs
,
563 .ct_owner
= THIS_MODULE
,
566 static void composite_init_dev(struct usb_composite_dev
*cdev
)
568 spin_lock_init(&cdev
->lock
);
569 INIT_LIST_HEAD(&cdev
->configs
);
570 INIT_LIST_HEAD(&cdev
->gstrings
);
573 static struct config_group
*function_make(
574 struct config_group
*group
,
577 struct gadget_info
*gi
;
578 struct usb_function_instance
*fi
;
579 char buf
[MAX_NAME_LEN
];
584 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
585 if (ret
>= MAX_NAME_LEN
)
586 return ERR_PTR(-ENAMETOOLONG
);
589 instance_name
= strchr(func_name
, '.');
590 if (!instance_name
) {
591 pr_err("Unable to locate . in FUNC.INSTANCE\n");
592 return ERR_PTR(-EINVAL
);
594 *instance_name
= '\0';
597 fi
= usb_get_function_instance(func_name
);
601 ret
= config_item_set_name(&fi
->group
.cg_item
, "%s", name
);
603 usb_put_function_instance(fi
);
606 if (fi
->set_inst_name
) {
607 ret
= fi
->set_inst_name(fi
, instance_name
);
609 usb_put_function_instance(fi
);
614 gi
= container_of(group
, struct gadget_info
, functions_group
);
616 mutex_lock(&gi
->lock
);
617 list_add_tail(&fi
->cfs_list
, &gi
->available_func
);
618 mutex_unlock(&gi
->lock
);
622 static void function_drop(
623 struct config_group
*group
,
624 struct config_item
*item
)
626 struct usb_function_instance
*fi
= to_usb_function_instance(item
);
627 struct gadget_info
*gi
;
629 gi
= container_of(group
, struct gadget_info
, functions_group
);
631 mutex_lock(&gi
->lock
);
632 list_del(&fi
->cfs_list
);
633 mutex_unlock(&gi
->lock
);
634 config_item_put(item
);
637 static struct configfs_group_operations functions_ops
= {
638 .make_group
= &function_make
,
639 .drop_item
= &function_drop
,
642 static const struct config_item_type functions_type
= {
643 .ct_group_ops
= &functions_ops
,
644 .ct_owner
= THIS_MODULE
,
647 GS_STRINGS_RW(gadget_config_name
, configuration
);
649 static struct configfs_attribute
*gadget_config_name_langid_attrs
[] = {
650 &gadget_config_name_attr_configuration
,
654 static void gadget_config_name_attr_release(struct config_item
*item
)
656 struct gadget_config_name
*cn
= to_gadget_config_name(item
);
658 kfree(cn
->configuration
);
664 USB_CONFIG_STRING_RW_OPS(gadget_config_name
);
665 USB_CONFIG_STRINGS_LANG(gadget_config_name
, config_usb_cfg
);
667 static struct config_group
*config_desc_make(
668 struct config_group
*group
,
671 struct gadget_info
*gi
;
672 struct config_usb_cfg
*cfg
;
673 char buf
[MAX_NAME_LEN
];
678 gi
= container_of(group
, struct gadget_info
, configs_group
);
679 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
680 if (ret
>= MAX_NAME_LEN
)
681 return ERR_PTR(-ENAMETOOLONG
);
683 num_str
= strchr(buf
, '.');
685 pr_err("Unable to locate . in name.bConfigurationValue\n");
686 return ERR_PTR(-EINVAL
);
693 return ERR_PTR(-EINVAL
);
695 ret
= kstrtou8(num_str
, 0, &num
);
699 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
701 return ERR_PTR(-ENOMEM
);
702 cfg
->c
.label
= kstrdup(buf
, GFP_KERNEL
);
707 cfg
->c
.bConfigurationValue
= num
;
708 cfg
->c
.MaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
;
709 cfg
->c
.bmAttributes
= USB_CONFIG_ATT_ONE
;
710 INIT_LIST_HEAD(&cfg
->string_list
);
711 INIT_LIST_HEAD(&cfg
->func_list
);
713 config_group_init_type_name(&cfg
->group
, name
,
714 &gadget_config_type
);
716 config_group_init_type_name(&cfg
->strings_group
, "strings",
717 &gadget_config_name_strings_type
);
718 configfs_add_default_group(&cfg
->strings_group
, &cfg
->group
);
720 ret
= usb_add_config_only(&gi
->cdev
, &cfg
->c
);
731 static void config_desc_drop(
732 struct config_group
*group
,
733 struct config_item
*item
)
735 config_item_put(item
);
738 static struct configfs_group_operations config_desc_ops
= {
739 .make_group
= &config_desc_make
,
740 .drop_item
= &config_desc_drop
,
743 static const struct config_item_type config_desc_type
= {
744 .ct_group_ops
= &config_desc_ops
,
745 .ct_owner
= THIS_MODULE
,
748 GS_STRINGS_RW(gadget_strings
, manufacturer
);
749 GS_STRINGS_RW(gadget_strings
, product
);
750 GS_STRINGS_RW(gadget_strings
, serialnumber
);
752 static struct configfs_attribute
*gadget_strings_langid_attrs
[] = {
753 &gadget_strings_attr_manufacturer
,
754 &gadget_strings_attr_product
,
755 &gadget_strings_attr_serialnumber
,
759 static void gadget_strings_attr_release(struct config_item
*item
)
761 struct gadget_strings
*gs
= to_gadget_strings(item
);
763 kfree(gs
->manufacturer
);
765 kfree(gs
->serialnumber
);
771 USB_CONFIG_STRING_RW_OPS(gadget_strings
);
772 USB_CONFIG_STRINGS_LANG(gadget_strings
, gadget_info
);
774 static inline struct os_desc
*to_os_desc(struct config_item
*item
)
776 return container_of(to_config_group(item
), struct os_desc
, group
);
779 static inline struct gadget_info
*os_desc_item_to_gadget_info(
780 struct config_item
*item
)
782 return to_gadget_info(to_os_desc(item
)->group
.cg_item
.ci_parent
);
785 static ssize_t
os_desc_use_show(struct config_item
*item
, char *page
)
787 return sprintf(page
, "%d\n",
788 os_desc_item_to_gadget_info(item
)->use_os_desc
);
791 static ssize_t
os_desc_use_store(struct config_item
*item
, const char *page
,
794 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
798 mutex_lock(&gi
->lock
);
799 ret
= strtobool(page
, &use
);
801 gi
->use_os_desc
= use
;
804 mutex_unlock(&gi
->lock
);
809 static ssize_t
os_desc_b_vendor_code_show(struct config_item
*item
, char *page
)
811 return sprintf(page
, "0x%02x\n",
812 os_desc_item_to_gadget_info(item
)->b_vendor_code
);
815 static ssize_t
os_desc_b_vendor_code_store(struct config_item
*item
,
816 const char *page
, size_t len
)
818 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
822 mutex_lock(&gi
->lock
);
823 ret
= kstrtou8(page
, 0, &b_vendor_code
);
825 gi
->b_vendor_code
= b_vendor_code
;
828 mutex_unlock(&gi
->lock
);
833 static ssize_t
os_desc_qw_sign_show(struct config_item
*item
, char *page
)
835 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
838 res
= utf16s_to_utf8s((wchar_t *) gi
->qw_sign
, OS_STRING_QW_SIGN_LEN
,
839 UTF16_LITTLE_ENDIAN
, page
, PAGE_SIZE
- 1);
845 static ssize_t
os_desc_qw_sign_store(struct config_item
*item
, const char *page
,
848 struct gadget_info
*gi
= os_desc_item_to_gadget_info(item
);
851 l
= min((int)len
, OS_STRING_QW_SIGN_LEN
>> 1);
852 if (page
[l
- 1] == '\n')
855 mutex_lock(&gi
->lock
);
856 res
= utf8s_to_utf16s(page
, l
,
857 UTF16_LITTLE_ENDIAN
, (wchar_t *) gi
->qw_sign
,
858 OS_STRING_QW_SIGN_LEN
);
861 mutex_unlock(&gi
->lock
);
866 CONFIGFS_ATTR(os_desc_
, use
);
867 CONFIGFS_ATTR(os_desc_
, b_vendor_code
);
868 CONFIGFS_ATTR(os_desc_
, qw_sign
);
870 static struct configfs_attribute
*os_desc_attrs
[] = {
872 &os_desc_attr_b_vendor_code
,
873 &os_desc_attr_qw_sign
,
877 static void os_desc_attr_release(struct config_item
*item
)
879 struct os_desc
*os_desc
= to_os_desc(item
);
883 static int os_desc_link(struct config_item
*os_desc_ci
,
884 struct config_item
*usb_cfg_ci
)
886 struct gadget_info
*gi
= container_of(to_config_group(os_desc_ci
),
887 struct gadget_info
, os_desc_group
);
888 struct usb_composite_dev
*cdev
= &gi
->cdev
;
889 struct config_usb_cfg
*c_target
=
890 container_of(to_config_group(usb_cfg_ci
),
891 struct config_usb_cfg
, group
);
892 struct usb_configuration
*c
;
895 mutex_lock(&gi
->lock
);
896 list_for_each_entry(c
, &cdev
->configs
, list
) {
897 if (c
== &c_target
->c
)
900 if (c
!= &c_target
->c
) {
905 if (cdev
->os_desc_config
) {
910 cdev
->os_desc_config
= &c_target
->c
;
914 mutex_unlock(&gi
->lock
);
918 static void os_desc_unlink(struct config_item
*os_desc_ci
,
919 struct config_item
*usb_cfg_ci
)
921 struct gadget_info
*gi
= container_of(to_config_group(os_desc_ci
),
922 struct gadget_info
, os_desc_group
);
923 struct usb_composite_dev
*cdev
= &gi
->cdev
;
925 mutex_lock(&gi
->lock
);
926 if (gi
->composite
.gadget_driver
.udc_name
)
927 unregister_gadget(gi
);
928 cdev
->os_desc_config
= NULL
;
929 WARN_ON(gi
->composite
.gadget_driver
.udc_name
);
930 mutex_unlock(&gi
->lock
);
933 static struct configfs_item_operations os_desc_ops
= {
934 .release
= os_desc_attr_release
,
935 .allow_link
= os_desc_link
,
936 .drop_link
= os_desc_unlink
,
939 static struct config_item_type os_desc_type
= {
940 .ct_item_ops
= &os_desc_ops
,
941 .ct_attrs
= os_desc_attrs
,
942 .ct_owner
= THIS_MODULE
,
945 static inline struct usb_os_desc_ext_prop
946 *to_usb_os_desc_ext_prop(struct config_item
*item
)
948 return container_of(item
, struct usb_os_desc_ext_prop
, item
);
951 static ssize_t
ext_prop_type_show(struct config_item
*item
, char *page
)
953 return sprintf(page
, "%d\n", to_usb_os_desc_ext_prop(item
)->type
);
956 static ssize_t
ext_prop_type_store(struct config_item
*item
,
957 const char *page
, size_t len
)
959 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
960 struct usb_os_desc
*desc
= to_usb_os_desc(ext_prop
->item
.ci_parent
);
964 if (desc
->opts_mutex
)
965 mutex_lock(desc
->opts_mutex
);
966 ret
= kstrtou8(page
, 0, &type
);
969 if (type
< USB_EXT_PROP_UNICODE
|| type
> USB_EXT_PROP_UNICODE_MULTI
) {
974 if ((ext_prop
->type
== USB_EXT_PROP_BINARY
||
975 ext_prop
->type
== USB_EXT_PROP_LE32
||
976 ext_prop
->type
== USB_EXT_PROP_BE32
) &&
977 (type
== USB_EXT_PROP_UNICODE
||
978 type
== USB_EXT_PROP_UNICODE_ENV
||
979 type
== USB_EXT_PROP_UNICODE_LINK
))
980 ext_prop
->data_len
<<= 1;
981 else if ((ext_prop
->type
== USB_EXT_PROP_UNICODE
||
982 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
983 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
) &&
984 (type
== USB_EXT_PROP_BINARY
||
985 type
== USB_EXT_PROP_LE32
||
986 type
== USB_EXT_PROP_BE32
))
987 ext_prop
->data_len
>>= 1;
988 ext_prop
->type
= type
;
992 if (desc
->opts_mutex
)
993 mutex_unlock(desc
->opts_mutex
);
997 static ssize_t
ext_prop_data_show(struct config_item
*item
, char *page
)
999 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1000 int len
= ext_prop
->data_len
;
1002 if (ext_prop
->type
== USB_EXT_PROP_UNICODE
||
1003 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
1004 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
)
1006 memcpy(page
, ext_prop
->data
, len
);
1011 static ssize_t
ext_prop_data_store(struct config_item
*item
,
1012 const char *page
, size_t len
)
1014 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1015 struct usb_os_desc
*desc
= to_usb_os_desc(ext_prop
->item
.ci_parent
);
1017 size_t ret_len
= len
;
1019 if (page
[len
- 1] == '\n' || page
[len
- 1] == '\0')
1021 new_data
= kmemdup(page
, len
, GFP_KERNEL
);
1025 if (desc
->opts_mutex
)
1026 mutex_lock(desc
->opts_mutex
);
1027 kfree(ext_prop
->data
);
1028 ext_prop
->data
= new_data
;
1029 desc
->ext_prop_len
-= ext_prop
->data_len
;
1030 ext_prop
->data_len
= len
;
1031 desc
->ext_prop_len
+= ext_prop
->data_len
;
1032 if (ext_prop
->type
== USB_EXT_PROP_UNICODE
||
1033 ext_prop
->type
== USB_EXT_PROP_UNICODE_ENV
||
1034 ext_prop
->type
== USB_EXT_PROP_UNICODE_LINK
) {
1035 desc
->ext_prop_len
-= ext_prop
->data_len
;
1036 ext_prop
->data_len
<<= 1;
1037 ext_prop
->data_len
+= 2;
1038 desc
->ext_prop_len
+= ext_prop
->data_len
;
1040 if (desc
->opts_mutex
)
1041 mutex_unlock(desc
->opts_mutex
);
1045 CONFIGFS_ATTR(ext_prop_
, type
);
1046 CONFIGFS_ATTR(ext_prop_
, data
);
1048 static struct configfs_attribute
*ext_prop_attrs
[] = {
1049 &ext_prop_attr_type
,
1050 &ext_prop_attr_data
,
1054 static void usb_os_desc_ext_prop_release(struct config_item
*item
)
1056 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1058 kfree(ext_prop
); /* frees a whole chunk */
1061 static struct configfs_item_operations ext_prop_ops
= {
1062 .release
= usb_os_desc_ext_prop_release
,
1065 static struct config_item
*ext_prop_make(
1066 struct config_group
*group
,
1069 struct usb_os_desc_ext_prop
*ext_prop
;
1070 struct config_item_type
*ext_prop_type
;
1071 struct usb_os_desc
*desc
;
1074 vla_group(data_chunk
);
1075 vla_item(data_chunk
, struct usb_os_desc_ext_prop
, ext_prop
, 1);
1076 vla_item(data_chunk
, struct config_item_type
, ext_prop_type
, 1);
1078 vlabuf
= kzalloc(vla_group_size(data_chunk
), GFP_KERNEL
);
1080 return ERR_PTR(-ENOMEM
);
1082 ext_prop
= vla_ptr(vlabuf
, data_chunk
, ext_prop
);
1083 ext_prop_type
= vla_ptr(vlabuf
, data_chunk
, ext_prop_type
);
1085 desc
= container_of(group
, struct usb_os_desc
, group
);
1086 ext_prop_type
->ct_item_ops
= &ext_prop_ops
;
1087 ext_prop_type
->ct_attrs
= ext_prop_attrs
;
1088 ext_prop_type
->ct_owner
= desc
->owner
;
1090 config_item_init_type_name(&ext_prop
->item
, name
, ext_prop_type
);
1092 ext_prop
->name
= kstrdup(name
, GFP_KERNEL
);
1093 if (!ext_prop
->name
) {
1095 return ERR_PTR(-ENOMEM
);
1097 desc
->ext_prop_len
+= 14;
1098 ext_prop
->name_len
= 2 * strlen(ext_prop
->name
) + 2;
1099 if (desc
->opts_mutex
)
1100 mutex_lock(desc
->opts_mutex
);
1101 desc
->ext_prop_len
+= ext_prop
->name_len
;
1102 list_add_tail(&ext_prop
->entry
, &desc
->ext_prop
);
1103 ++desc
->ext_prop_count
;
1104 if (desc
->opts_mutex
)
1105 mutex_unlock(desc
->opts_mutex
);
1107 return &ext_prop
->item
;
1110 static void ext_prop_drop(struct config_group
*group
, struct config_item
*item
)
1112 struct usb_os_desc_ext_prop
*ext_prop
= to_usb_os_desc_ext_prop(item
);
1113 struct usb_os_desc
*desc
= to_usb_os_desc(&group
->cg_item
);
1115 if (desc
->opts_mutex
)
1116 mutex_lock(desc
->opts_mutex
);
1117 list_del(&ext_prop
->entry
);
1118 --desc
->ext_prop_count
;
1119 kfree(ext_prop
->name
);
1120 desc
->ext_prop_len
-= (ext_prop
->name_len
+ ext_prop
->data_len
+ 14);
1121 if (desc
->opts_mutex
)
1122 mutex_unlock(desc
->opts_mutex
);
1123 config_item_put(item
);
1126 static struct configfs_group_operations interf_grp_ops
= {
1127 .make_item
= &ext_prop_make
,
1128 .drop_item
= &ext_prop_drop
,
1131 static ssize_t
interf_grp_compatible_id_show(struct config_item
*item
,
1134 memcpy(page
, to_usb_os_desc(item
)->ext_compat_id
, 8);
1138 static ssize_t
interf_grp_compatible_id_store(struct config_item
*item
,
1139 const char *page
, size_t len
)
1141 struct usb_os_desc
*desc
= to_usb_os_desc(item
);
1144 l
= min_t(int, 8, len
);
1145 if (page
[l
- 1] == '\n')
1147 if (desc
->opts_mutex
)
1148 mutex_lock(desc
->opts_mutex
);
1149 memcpy(desc
->ext_compat_id
, page
, l
);
1151 if (desc
->opts_mutex
)
1152 mutex_unlock(desc
->opts_mutex
);
1157 static ssize_t
interf_grp_sub_compatible_id_show(struct config_item
*item
,
1160 memcpy(page
, to_usb_os_desc(item
)->ext_compat_id
+ 8, 8);
1164 static ssize_t
interf_grp_sub_compatible_id_store(struct config_item
*item
,
1165 const char *page
, size_t len
)
1167 struct usb_os_desc
*desc
= to_usb_os_desc(item
);
1170 l
= min_t(int, 8, len
);
1171 if (page
[l
- 1] == '\n')
1173 if (desc
->opts_mutex
)
1174 mutex_lock(desc
->opts_mutex
);
1175 memcpy(desc
->ext_compat_id
+ 8, page
, l
);
1177 if (desc
->opts_mutex
)
1178 mutex_unlock(desc
->opts_mutex
);
1183 CONFIGFS_ATTR(interf_grp_
, compatible_id
);
1184 CONFIGFS_ATTR(interf_grp_
, sub_compatible_id
);
1186 static struct configfs_attribute
*interf_grp_attrs
[] = {
1187 &interf_grp_attr_compatible_id
,
1188 &interf_grp_attr_sub_compatible_id
,
1192 struct config_group
*usb_os_desc_prepare_interf_dir(
1193 struct config_group
*parent
,
1195 struct usb_os_desc
**desc
,
1197 struct module
*owner
)
1199 struct config_group
*os_desc_group
;
1200 struct config_item_type
*os_desc_type
, *interface_type
;
1202 vla_group(data_chunk
);
1203 vla_item(data_chunk
, struct config_group
, os_desc_group
, 1);
1204 vla_item(data_chunk
, struct config_item_type
, os_desc_type
, 1);
1205 vla_item(data_chunk
, struct config_item_type
, interface_type
, 1);
1207 char *vlabuf
= kzalloc(vla_group_size(data_chunk
), GFP_KERNEL
);
1209 return ERR_PTR(-ENOMEM
);
1211 os_desc_group
= vla_ptr(vlabuf
, data_chunk
, os_desc_group
);
1212 os_desc_type
= vla_ptr(vlabuf
, data_chunk
, os_desc_type
);
1213 interface_type
= vla_ptr(vlabuf
, data_chunk
, interface_type
);
1215 os_desc_type
->ct_owner
= owner
;
1216 config_group_init_type_name(os_desc_group
, "os_desc", os_desc_type
);
1217 configfs_add_default_group(os_desc_group
, parent
);
1219 interface_type
->ct_group_ops
= &interf_grp_ops
;
1220 interface_type
->ct_attrs
= interf_grp_attrs
;
1221 interface_type
->ct_owner
= owner
;
1223 while (n_interf
--) {
1224 struct usb_os_desc
*d
;
1228 config_group_init_type_name(&d
->group
, "", interface_type
);
1229 config_item_set_name(&d
->group
.cg_item
, "interface.%s",
1231 configfs_add_default_group(&d
->group
, os_desc_group
);
1234 return os_desc_group
;
1236 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir
);
1238 static int configfs_do_nothing(struct usb_composite_dev
*cdev
)
1244 int composite_dev_prepare(struct usb_composite_driver
*composite
,
1245 struct usb_composite_dev
*dev
);
1247 int composite_os_desc_req_prepare(struct usb_composite_dev
*cdev
,
1248 struct usb_ep
*ep0
);
1250 static void purge_configs_funcs(struct gadget_info
*gi
)
1252 struct usb_configuration
*c
;
1254 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1255 struct usb_function
*f
, *tmp
;
1256 struct config_usb_cfg
*cfg
;
1258 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1260 list_for_each_entry_safe(f
, tmp
, &c
->functions
, list
) {
1262 list_move_tail(&f
->list
, &cfg
->func_list
);
1264 dev_dbg(&gi
->cdev
.gadget
->dev
,
1265 "unbind function '%s'/%p\n",
1270 c
->next_interface_id
= 0;
1271 memset(c
->interface
, 0, sizeof(c
->interface
));
1272 c
->superspeed_plus
= 0;
1279 static int configfs_composite_bind(struct usb_gadget
*gadget
,
1280 struct usb_gadget_driver
*gdriver
)
1282 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1283 struct gadget_info
*gi
= container_of(composite
,
1284 struct gadget_info
, composite
);
1285 struct usb_composite_dev
*cdev
= &gi
->cdev
;
1286 struct usb_configuration
*c
;
1287 struct usb_string
*s
;
1291 /* the gi->lock is hold by the caller */
1293 cdev
->gadget
= gadget
;
1294 set_gadget_data(gadget
, cdev
);
1295 ret
= composite_dev_prepare(composite
, cdev
);
1298 /* and now the gadget bind */
1301 if (list_empty(&gi
->cdev
.configs
)) {
1302 pr_err("Need at least one configuration in %s.\n",
1303 gi
->composite
.name
);
1304 goto err_comp_cleanup
;
1308 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1309 struct config_usb_cfg
*cfg
;
1311 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1312 if (list_empty(&cfg
->func_list
)) {
1313 pr_err("Config %s/%d of %s needs at least one function.\n",
1314 c
->label
, c
->bConfigurationValue
,
1315 gi
->composite
.name
);
1316 goto err_comp_cleanup
;
1320 /* init all strings */
1321 if (!list_empty(&gi
->string_list
)) {
1322 struct gadget_strings
*gs
;
1325 list_for_each_entry(gs
, &gi
->string_list
, list
) {
1327 gi
->gstrings
[i
] = &gs
->stringtab_dev
;
1328 gs
->stringtab_dev
.strings
= gs
->strings
;
1329 gs
->strings
[USB_GADGET_MANUFACTURER_IDX
].s
=
1331 gs
->strings
[USB_GADGET_PRODUCT_IDX
].s
= gs
->product
;
1332 gs
->strings
[USB_GADGET_SERIAL_IDX
].s
= gs
->serialnumber
;
1335 gi
->gstrings
[i
] = NULL
;
1336 s
= usb_gstrings_attach(&gi
->cdev
, gi
->gstrings
,
1337 USB_GADGET_FIRST_AVAIL_IDX
);
1340 goto err_comp_cleanup
;
1343 gi
->cdev
.desc
.iManufacturer
= s
[USB_GADGET_MANUFACTURER_IDX
].id
;
1344 gi
->cdev
.desc
.iProduct
= s
[USB_GADGET_PRODUCT_IDX
].id
;
1345 gi
->cdev
.desc
.iSerialNumber
= s
[USB_GADGET_SERIAL_IDX
].id
;
1348 if (gi
->use_os_desc
) {
1349 cdev
->use_os_string
= true;
1350 cdev
->b_vendor_code
= gi
->b_vendor_code
;
1351 memcpy(cdev
->qw_sign
, gi
->qw_sign
, OS_STRING_QW_SIGN_LEN
);
1354 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
1355 struct usb_descriptor_header
*usb_desc
;
1357 usb_desc
= usb_otg_descriptor_alloc(gadget
);
1360 goto err_comp_cleanup
;
1362 usb_otg_descriptor_init(gadget
, usb_desc
);
1363 otg_desc
[0] = usb_desc
;
1367 /* Go through all configs, attach all functions */
1368 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
1369 struct config_usb_cfg
*cfg
;
1370 struct usb_function
*f
;
1371 struct usb_function
*tmp
;
1372 struct gadget_config_name
*cn
;
1374 if (gadget_is_otg(gadget
))
1375 c
->descriptors
= otg_desc
;
1377 cfg
= container_of(c
, struct config_usb_cfg
, c
);
1378 if (!list_empty(&cfg
->string_list
)) {
1380 list_for_each_entry(cn
, &cfg
->string_list
, list
) {
1381 cfg
->gstrings
[i
] = &cn
->stringtab_dev
;
1382 cn
->stringtab_dev
.strings
= &cn
->strings
;
1383 cn
->strings
.s
= cn
->configuration
;
1386 cfg
->gstrings
[i
] = NULL
;
1387 s
= usb_gstrings_attach(&gi
->cdev
, cfg
->gstrings
, 1);
1390 goto err_comp_cleanup
;
1392 c
->iConfiguration
= s
[0].id
;
1395 list_for_each_entry_safe(f
, tmp
, &cfg
->func_list
, list
) {
1397 ret
= usb_add_function(c
, f
);
1399 list_add(&f
->list
, &cfg
->func_list
);
1400 goto err_purge_funcs
;
1403 usb_ep_autoconfig_reset(cdev
->gadget
);
1405 if (cdev
->use_os_string
) {
1406 ret
= composite_os_desc_req_prepare(cdev
, gadget
->ep0
);
1408 goto err_purge_funcs
;
1411 usb_ep_autoconfig_reset(cdev
->gadget
);
1415 purge_configs_funcs(gi
);
1417 composite_dev_cleanup(cdev
);
1421 static void configfs_composite_unbind(struct usb_gadget
*gadget
)
1423 struct usb_composite_dev
*cdev
;
1424 struct gadget_info
*gi
;
1425 unsigned long flags
;
1427 /* the gi->lock is hold by the caller */
1429 cdev
= get_gadget_data(gadget
);
1430 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1431 spin_lock_irqsave(&gi
->spinlock
, flags
);
1433 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1437 purge_configs_funcs(gi
);
1438 composite_dev_cleanup(cdev
);
1439 usb_ep_autoconfig_reset(cdev
->gadget
);
1440 spin_lock_irqsave(&gi
->spinlock
, flags
);
1441 cdev
->gadget
= NULL
;
1442 set_gadget_data(gadget
, NULL
);
1443 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1446 static int configfs_composite_setup(struct usb_gadget
*gadget
,
1447 const struct usb_ctrlrequest
*ctrl
)
1449 struct usb_composite_dev
*cdev
;
1450 struct gadget_info
*gi
;
1451 unsigned long flags
;
1454 cdev
= get_gadget_data(gadget
);
1458 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1459 spin_lock_irqsave(&gi
->spinlock
, flags
);
1460 cdev
= get_gadget_data(gadget
);
1461 if (!cdev
|| gi
->unbind
) {
1462 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1466 ret
= composite_setup(gadget
, ctrl
);
1467 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1471 static void configfs_composite_disconnect(struct usb_gadget
*gadget
)
1473 struct usb_composite_dev
*cdev
;
1474 struct gadget_info
*gi
;
1475 unsigned long flags
;
1477 cdev
= get_gadget_data(gadget
);
1481 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1482 spin_lock_irqsave(&gi
->spinlock
, flags
);
1483 cdev
= get_gadget_data(gadget
);
1484 if (!cdev
|| gi
->unbind
) {
1485 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1489 composite_disconnect(gadget
);
1490 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1493 static void configfs_composite_suspend(struct usb_gadget
*gadget
)
1495 struct usb_composite_dev
*cdev
;
1496 struct gadget_info
*gi
;
1497 unsigned long flags
;
1499 cdev
= get_gadget_data(gadget
);
1503 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1504 spin_lock_irqsave(&gi
->spinlock
, flags
);
1505 cdev
= get_gadget_data(gadget
);
1506 if (!cdev
|| gi
->unbind
) {
1507 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1511 composite_suspend(gadget
);
1512 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1515 static void configfs_composite_resume(struct usb_gadget
*gadget
)
1517 struct usb_composite_dev
*cdev
;
1518 struct gadget_info
*gi
;
1519 unsigned long flags
;
1521 cdev
= get_gadget_data(gadget
);
1525 gi
= container_of(cdev
, struct gadget_info
, cdev
);
1526 spin_lock_irqsave(&gi
->spinlock
, flags
);
1527 cdev
= get_gadget_data(gadget
);
1528 if (!cdev
|| gi
->unbind
) {
1529 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1533 composite_resume(gadget
);
1534 spin_unlock_irqrestore(&gi
->spinlock
, flags
);
1537 static const struct usb_gadget_driver configfs_driver_template
= {
1538 .bind
= configfs_composite_bind
,
1539 .unbind
= configfs_composite_unbind
,
1541 .setup
= configfs_composite_setup
,
1542 .reset
= configfs_composite_disconnect
,
1543 .disconnect
= configfs_composite_disconnect
,
1545 .suspend
= configfs_composite_suspend
,
1546 .resume
= configfs_composite_resume
,
1548 .max_speed
= USB_SPEED_SUPER
,
1550 .owner
= THIS_MODULE
,
1551 .name
= "configfs-gadget",
1553 .match_existing_only
= 1,
1556 static struct config_group
*gadgets_make(
1557 struct config_group
*group
,
1560 struct gadget_info
*gi
;
1562 gi
= kzalloc(sizeof(*gi
), GFP_KERNEL
);
1564 return ERR_PTR(-ENOMEM
);
1566 config_group_init_type_name(&gi
->group
, name
, &gadget_root_type
);
1568 config_group_init_type_name(&gi
->functions_group
, "functions",
1570 configfs_add_default_group(&gi
->functions_group
, &gi
->group
);
1572 config_group_init_type_name(&gi
->configs_group
, "configs",
1574 configfs_add_default_group(&gi
->configs_group
, &gi
->group
);
1576 config_group_init_type_name(&gi
->strings_group
, "strings",
1577 &gadget_strings_strings_type
);
1578 configfs_add_default_group(&gi
->strings_group
, &gi
->group
);
1580 config_group_init_type_name(&gi
->os_desc_group
, "os_desc",
1582 configfs_add_default_group(&gi
->os_desc_group
, &gi
->group
);
1584 gi
->composite
.bind
= configfs_do_nothing
;
1585 gi
->composite
.unbind
= configfs_do_nothing
;
1586 gi
->composite
.suspend
= NULL
;
1587 gi
->composite
.resume
= NULL
;
1588 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
1590 spin_lock_init(&gi
->spinlock
);
1591 mutex_init(&gi
->lock
);
1592 INIT_LIST_HEAD(&gi
->string_list
);
1593 INIT_LIST_HEAD(&gi
->available_func
);
1595 composite_init_dev(&gi
->cdev
);
1596 gi
->cdev
.desc
.bLength
= USB_DT_DEVICE_SIZE
;
1597 gi
->cdev
.desc
.bDescriptorType
= USB_DT_DEVICE
;
1598 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(get_default_bcdDevice());
1600 gi
->composite
.gadget_driver
= configfs_driver_template
;
1602 gi
->composite
.gadget_driver
.function
= kstrdup(name
, GFP_KERNEL
);
1603 gi
->composite
.name
= gi
->composite
.gadget_driver
.function
;
1605 if (!gi
->composite
.gadget_driver
.function
)
1611 return ERR_PTR(-ENOMEM
);
1614 static void gadgets_drop(struct config_group
*group
, struct config_item
*item
)
1616 config_item_put(item
);
1619 static struct configfs_group_operations gadgets_ops
= {
1620 .make_group
= &gadgets_make
,
1621 .drop_item
= &gadgets_drop
,
1624 static const struct config_item_type gadgets_type
= {
1625 .ct_group_ops
= &gadgets_ops
,
1626 .ct_owner
= THIS_MODULE
,
1629 static struct configfs_subsystem gadget_subsys
= {
1632 .ci_namebuf
= "usb_gadget",
1633 .ci_type
= &gadgets_type
,
1636 .su_mutex
= __MUTEX_INITIALIZER(gadget_subsys
.su_mutex
),
1639 void unregister_gadget_item(struct config_item
*item
)
1641 struct gadget_info
*gi
= to_gadget_info(item
);
1643 mutex_lock(&gi
->lock
);
1644 unregister_gadget(gi
);
1645 mutex_unlock(&gi
->lock
);
1647 EXPORT_SYMBOL_GPL(unregister_gadget_item
);
1649 static int __init
gadget_cfs_init(void)
1653 config_group_init(&gadget_subsys
.su_group
);
1655 ret
= configfs_register_subsystem(&gadget_subsys
);
1658 module_init(gadget_cfs_init
);
1660 static void __exit
gadget_cfs_exit(void)
1662 configfs_unregister_subsystem(&gadget_subsys
);
1664 module_exit(gadget_cfs_exit
);