1 #include <linux/configfs.h>
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 #include <linux/device.h>
5 #include <linux/usb/composite.h>
6 #include <linux/usb/gadget_configfs.h>
9 int check_user_usb_string(const char *name
,
10 struct usb_gadget_strings
*stringtab_dev
)
12 unsigned primary_lang
;
17 ret
= kstrtou16(name
, 0, &num
);
21 primary_lang
= num
& 0x3ff;
24 /* simple sanity check for valid langid */
25 switch (primary_lang
) {
34 stringtab_dev
->language
= num
;
38 #define MAX_NAME_LEN 40
39 #define MAX_USB_STRING_LANGS 2
42 struct config_group group
;
43 struct config_group functions_group
;
44 struct config_group configs_group
;
45 struct config_group strings_group
;
46 struct config_group
*default_groups
[4];
49 struct usb_gadget_strings
*gstrings
[MAX_USB_STRING_LANGS
+ 1];
50 struct list_head string_list
;
51 struct list_head available_func
;
55 struct usb_otg_descriptor otg
;
57 struct usb_composite_driver composite
;
58 struct usb_composite_dev cdev
;
61 struct config_usb_cfg
{
62 struct config_group group
;
63 struct config_group strings_group
;
64 struct config_group
*default_groups
[2];
65 struct list_head string_list
;
66 struct usb_configuration c
;
67 struct list_head func_list
;
68 struct usb_gadget_strings
*gstrings
[MAX_USB_STRING_LANGS
+ 1];
71 struct gadget_strings
{
72 struct usb_gadget_strings stringtab_dev
;
73 struct usb_string strings
[USB_GADGET_FIRST_AVAIL_IDX
];
78 struct config_group group
;
79 struct list_head list
;
82 struct gadget_config_name
{
83 struct usb_gadget_strings stringtab_dev
;
84 struct usb_string strings
;
87 struct config_group group
;
88 struct list_head list
;
91 static int usb_string_copy(const char *s
, char **s_copy
)
100 str
= kstrdup(s
, GFP_KERNEL
);
103 if (str
[ret
- 1] == '\n')
110 CONFIGFS_ATTR_STRUCT(gadget_info
);
111 CONFIGFS_ATTR_STRUCT(config_usb_cfg
);
113 #define GI_DEVICE_DESC_ITEM_ATTR(name) \
114 static struct gadget_info_attribute gadget_cdev_desc_##name = \
115 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
116 gadget_dev_desc_##name##_show, \
117 gadget_dev_desc_##name##_store)
119 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
120 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
123 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
126 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
127 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
130 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
134 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
135 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
136 const char *page, size_t len) \
140 ret = kstrtou8(page, 0, &val); \
143 gi->cdev.desc._name = val; \
147 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
148 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
149 const char *page, size_t len) \
153 ret = kstrtou16(page, 0, &val); \
156 gi->cdev.desc._name = cpu_to_le16p(&val); \
160 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
161 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
162 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
164 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB
);
165 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass
, u8
);
166 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass
, u8
);
167 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol
, u8
);
168 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0
, u8
);
169 GI_DEVICE_DESC_SIMPLE_RW(idVendor
, u16
);
170 GI_DEVICE_DESC_SIMPLE_RW(idProduct
, u16
);
171 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice
);
173 static ssize_t
is_valid_bcd(u16 bcd_val
)
175 if ((bcd_val
& 0xf) > 9)
177 if (((bcd_val
>> 4) & 0xf) > 9)
179 if (((bcd_val
>> 8) & 0xf) > 9)
181 if (((bcd_val
>> 12) & 0xf) > 9)
186 static ssize_t
gadget_dev_desc_bcdDevice_store(struct gadget_info
*gi
,
187 const char *page
, size_t len
)
192 ret
= kstrtou16(page
, 0, &bcdDevice
);
195 ret
= is_valid_bcd(bcdDevice
);
199 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
203 static ssize_t
gadget_dev_desc_bcdUSB_store(struct gadget_info
*gi
,
204 const char *page
, size_t len
)
209 ret
= kstrtou16(page
, 0, &bcdUSB
);
212 ret
= is_valid_bcd(bcdUSB
);
216 gi
->cdev
.desc
.bcdUSB
= cpu_to_le16(bcdUSB
);
220 static ssize_t
gadget_dev_desc_UDC_show(struct gadget_info
*gi
, char *page
)
222 return sprintf(page
, "%s\n", gi
->udc_name
?: "");
225 static int unregister_gadget(struct gadget_info
*gi
)
232 ret
= usb_gadget_unregister_driver(&gi
->composite
.gadget_driver
);
240 static ssize_t
gadget_dev_desc_UDC_store(struct gadget_info
*gi
,
241 const char *page
, size_t len
)
246 name
= kstrdup(page
, GFP_KERNEL
);
249 if (name
[len
- 1] == '\n')
250 name
[len
- 1] = '\0';
252 mutex_lock(&gi
->lock
);
255 ret
= unregister_gadget(gi
);
263 ret
= udc_attach_driver(name
, &gi
->composite
.gadget_driver
);
268 mutex_unlock(&gi
->lock
);
272 mutex_unlock(&gi
->lock
);
276 GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass
);
277 GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass
);
278 GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol
);
279 GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0
);
280 GI_DEVICE_DESC_ITEM_ATTR(idVendor
);
281 GI_DEVICE_DESC_ITEM_ATTR(idProduct
);
282 GI_DEVICE_DESC_ITEM_ATTR(bcdDevice
);
283 GI_DEVICE_DESC_ITEM_ATTR(bcdUSB
);
284 GI_DEVICE_DESC_ITEM_ATTR(UDC
);
286 static struct configfs_attribute
*gadget_root_attrs
[] = {
287 &gadget_cdev_desc_bDeviceClass
.attr
,
288 &gadget_cdev_desc_bDeviceSubClass
.attr
,
289 &gadget_cdev_desc_bDeviceProtocol
.attr
,
290 &gadget_cdev_desc_bMaxPacketSize0
.attr
,
291 &gadget_cdev_desc_idVendor
.attr
,
292 &gadget_cdev_desc_idProduct
.attr
,
293 &gadget_cdev_desc_bcdDevice
.attr
,
294 &gadget_cdev_desc_bcdUSB
.attr
,
295 &gadget_cdev_desc_UDC
.attr
,
299 static inline struct gadget_info
*to_gadget_info(struct config_item
*item
)
301 return container_of(to_config_group(item
), struct gadget_info
, group
);
304 static inline struct gadget_strings
*to_gadget_strings(struct config_item
*item
)
306 return container_of(to_config_group(item
), struct gadget_strings
,
310 static inline struct gadget_config_name
*to_gadget_config_name(
311 struct config_item
*item
)
313 return container_of(to_config_group(item
), struct gadget_config_name
,
317 static inline struct config_usb_cfg
*to_config_usb_cfg(struct config_item
*item
)
319 return container_of(to_config_group(item
), struct config_usb_cfg
,
323 static inline struct usb_function_instance
*to_usb_function_instance(
324 struct config_item
*item
)
326 return container_of(to_config_group(item
),
327 struct usb_function_instance
, group
);
330 static void gadget_info_attr_release(struct config_item
*item
)
332 struct gadget_info
*gi
= to_gadget_info(item
);
334 WARN_ON(!list_empty(&gi
->cdev
.configs
));
335 WARN_ON(!list_empty(&gi
->string_list
));
336 WARN_ON(!list_empty(&gi
->available_func
));
337 kfree(gi
->composite
.gadget_driver
.function
);
341 CONFIGFS_ATTR_OPS(gadget_info
);
343 static struct configfs_item_operations gadget_root_item_ops
= {
344 .release
= gadget_info_attr_release
,
345 .show_attribute
= gadget_info_attr_show
,
346 .store_attribute
= gadget_info_attr_store
,
349 static void gadget_config_attr_release(struct config_item
*item
)
351 struct config_usb_cfg
*cfg
= to_config_usb_cfg(item
);
353 WARN_ON(!list_empty(&cfg
->c
.functions
));
354 list_del(&cfg
->c
.list
);
359 static int config_usb_cfg_link(
360 struct config_item
*usb_cfg_ci
,
361 struct config_item
*usb_func_ci
)
363 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
364 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
365 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
367 struct config_group
*group
= to_config_group(usb_func_ci
);
368 struct usb_function_instance
*fi
= container_of(group
,
369 struct usb_function_instance
, group
);
370 struct usb_function_instance
*a_fi
;
371 struct usb_function
*f
;
374 mutex_lock(&gi
->lock
);
376 * Make sure this function is from within our _this_ gadget and not
377 * from another gadget or a random directory.
378 * Also a function instance can only be linked once.
380 list_for_each_entry(a_fi
, &gi
->available_func
, cfs_list
) {
389 list_for_each_entry(f
, &cfg
->func_list
, list
) {
396 f
= usb_get_function(fi
);
402 /* stash the function until we bind it to the gadget */
403 list_add_tail(&f
->list
, &cfg
->func_list
);
406 mutex_unlock(&gi
->lock
);
410 static int config_usb_cfg_unlink(
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
*f
;
424 * ideally I would like to forbid to unlink functions while a gadget is
425 * bound to an UDC. Since this isn't possible at the moment, we simply
426 * force an unbind, the function is available here and then we can
427 * remove the function.
429 mutex_lock(&gi
->lock
);
431 unregister_gadget(gi
);
432 WARN_ON(gi
->udc_name
);
434 list_for_each_entry(f
, &cfg
->func_list
, list
) {
438 mutex_unlock(&gi
->lock
);
442 mutex_unlock(&gi
->lock
);
443 WARN(1, "Unable to locate function to unbind\n");
447 CONFIGFS_ATTR_OPS(config_usb_cfg
);
449 static struct configfs_item_operations gadget_config_item_ops
= {
450 .release
= gadget_config_attr_release
,
451 .show_attribute
= config_usb_cfg_attr_show
,
452 .store_attribute
= config_usb_cfg_attr_store
,
453 .allow_link
= config_usb_cfg_link
,
454 .drop_link
= config_usb_cfg_unlink
,
458 static ssize_t
gadget_config_desc_MaxPower_show(struct config_usb_cfg
*cfg
,
461 return sprintf(page
, "%u\n", cfg
->c
.MaxPower
);
464 static ssize_t
gadget_config_desc_MaxPower_store(struct config_usb_cfg
*cfg
,
465 const char *page
, size_t len
)
469 ret
= kstrtou16(page
, 0, &val
);
472 if (DIV_ROUND_UP(val
, 8) > 0xff)
474 cfg
->c
.MaxPower
= val
;
478 static ssize_t
gadget_config_desc_bmAttributes_show(struct config_usb_cfg
*cfg
,
481 return sprintf(page
, "0x%02x\n", cfg
->c
.bmAttributes
);
484 static ssize_t
gadget_config_desc_bmAttributes_store(struct config_usb_cfg
*cfg
,
485 const char *page
, size_t len
)
489 ret
= kstrtou8(page
, 0, &val
);
492 if (!(val
& USB_CONFIG_ATT_ONE
))
494 if (val
& ~(USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
|
495 USB_CONFIG_ATT_WAKEUP
))
497 cfg
->c
.bmAttributes
= val
;
501 #define CFG_CONFIG_DESC_ITEM_ATTR(name) \
502 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
503 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
504 gadget_config_desc_##name##_show, \
505 gadget_config_desc_##name##_store)
507 CFG_CONFIG_DESC_ITEM_ATTR(MaxPower
);
508 CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes
);
510 static struct configfs_attribute
*gadget_config_attrs
[] = {
511 &gadget_usb_cfg_MaxPower
.attr
,
512 &gadget_usb_cfg_bmAttributes
.attr
,
516 static struct config_item_type gadget_config_type
= {
517 .ct_item_ops
= &gadget_config_item_ops
,
518 .ct_attrs
= gadget_config_attrs
,
519 .ct_owner
= THIS_MODULE
,
522 static struct config_item_type gadget_root_type
= {
523 .ct_item_ops
= &gadget_root_item_ops
,
524 .ct_attrs
= gadget_root_attrs
,
525 .ct_owner
= THIS_MODULE
,
528 static void composite_init_dev(struct usb_composite_dev
*cdev
)
530 spin_lock_init(&cdev
->lock
);
531 INIT_LIST_HEAD(&cdev
->configs
);
532 INIT_LIST_HEAD(&cdev
->gstrings
);
535 static struct config_group
*function_make(
536 struct config_group
*group
,
539 struct gadget_info
*gi
;
540 struct usb_function_instance
*fi
;
541 char buf
[MAX_NAME_LEN
];
546 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
547 if (ret
>= MAX_NAME_LEN
)
548 return ERR_PTR(-ENAMETOOLONG
);
551 instance_name
= strchr(func_name
, '.');
552 if (!instance_name
) {
553 pr_err("Unable to locate . in FUNC.INSTANCE\n");
554 return ERR_PTR(-EINVAL
);
556 *instance_name
= '\0';
559 fi
= usb_get_function_instance(func_name
);
563 ret
= config_item_set_name(&fi
->group
.cg_item
, name
);
565 usb_put_function_instance(fi
);
568 if (fi
->set_inst_name
) {
569 ret
= fi
->set_inst_name(fi
, instance_name
);
571 usb_put_function_instance(fi
);
576 gi
= container_of(group
, struct gadget_info
, functions_group
);
578 mutex_lock(&gi
->lock
);
579 list_add_tail(&fi
->cfs_list
, &gi
->available_func
);
580 mutex_unlock(&gi
->lock
);
584 static void function_drop(
585 struct config_group
*group
,
586 struct config_item
*item
)
588 struct usb_function_instance
*fi
= to_usb_function_instance(item
);
589 struct gadget_info
*gi
;
591 gi
= container_of(group
, struct gadget_info
, functions_group
);
593 mutex_lock(&gi
->lock
);
594 list_del(&fi
->cfs_list
);
595 mutex_unlock(&gi
->lock
);
596 config_item_put(item
);
599 static struct configfs_group_operations functions_ops
= {
600 .make_group
= &function_make
,
601 .drop_item
= &function_drop
,
604 static struct config_item_type functions_type
= {
605 .ct_group_ops
= &functions_ops
,
606 .ct_owner
= THIS_MODULE
,
609 CONFIGFS_ATTR_STRUCT(gadget_config_name
);
610 GS_STRINGS_RW(gadget_config_name
, configuration
);
612 static struct configfs_attribute
*gadget_config_name_langid_attrs
[] = {
613 &gadget_config_name_configuration
.attr
,
617 static void gadget_config_name_attr_release(struct config_item
*item
)
619 struct gadget_config_name
*cn
= to_gadget_config_name(item
);
621 kfree(cn
->configuration
);
627 USB_CONFIG_STRING_RW_OPS(gadget_config_name
);
628 USB_CONFIG_STRINGS_LANG(gadget_config_name
, config_usb_cfg
);
630 static struct config_group
*config_desc_make(
631 struct config_group
*group
,
634 struct gadget_info
*gi
;
635 struct config_usb_cfg
*cfg
;
636 char buf
[MAX_NAME_LEN
];
641 gi
= container_of(group
, struct gadget_info
, configs_group
);
642 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
643 if (ret
>= MAX_NAME_LEN
)
644 return ERR_PTR(-ENAMETOOLONG
);
646 num_str
= strchr(buf
, '.');
648 pr_err("Unable to locate . in name.bConfigurationValue\n");
649 return ERR_PTR(-EINVAL
);
656 return ERR_PTR(-EINVAL
);
658 ret
= kstrtou8(num_str
, 0, &num
);
662 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
664 return ERR_PTR(-ENOMEM
);
665 cfg
->c
.label
= kstrdup(buf
, GFP_KERNEL
);
670 cfg
->c
.bConfigurationValue
= num
;
671 cfg
->c
.MaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
;
672 cfg
->c
.bmAttributes
= USB_CONFIG_ATT_ONE
;
673 INIT_LIST_HEAD(&cfg
->string_list
);
674 INIT_LIST_HEAD(&cfg
->func_list
);
676 cfg
->group
.default_groups
= cfg
->default_groups
;
677 cfg
->default_groups
[0] = &cfg
->strings_group
;
679 config_group_init_type_name(&cfg
->group
, name
,
680 &gadget_config_type
);
681 config_group_init_type_name(&cfg
->strings_group
, "strings",
682 &gadget_config_name_strings_type
);
684 ret
= usb_add_config_only(&gi
->cdev
, &cfg
->c
);
695 static void config_desc_drop(
696 struct config_group
*group
,
697 struct config_item
*item
)
699 config_item_put(item
);
702 static struct configfs_group_operations config_desc_ops
= {
703 .make_group
= &config_desc_make
,
704 .drop_item
= &config_desc_drop
,
707 static struct config_item_type config_desc_type
= {
708 .ct_group_ops
= &config_desc_ops
,
709 .ct_owner
= THIS_MODULE
,
712 CONFIGFS_ATTR_STRUCT(gadget_strings
);
713 GS_STRINGS_RW(gadget_strings
, manufacturer
);
714 GS_STRINGS_RW(gadget_strings
, product
);
715 GS_STRINGS_RW(gadget_strings
, serialnumber
);
717 static struct configfs_attribute
*gadget_strings_langid_attrs
[] = {
718 &gadget_strings_manufacturer
.attr
,
719 &gadget_strings_product
.attr
,
720 &gadget_strings_serialnumber
.attr
,
724 static void gadget_strings_attr_release(struct config_item
*item
)
726 struct gadget_strings
*gs
= to_gadget_strings(item
);
728 kfree(gs
->manufacturer
);
730 kfree(gs
->serialnumber
);
736 USB_CONFIG_STRING_RW_OPS(gadget_strings
);
737 USB_CONFIG_STRINGS_LANG(gadget_strings
, gadget_info
);
739 static int configfs_do_nothing(struct usb_composite_dev
*cdev
)
745 int composite_dev_prepare(struct usb_composite_driver
*composite
,
746 struct usb_composite_dev
*dev
);
748 static void purge_configs_funcs(struct gadget_info
*gi
)
750 struct usb_configuration
*c
;
752 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
753 struct usb_function
*f
, *tmp
;
754 struct config_usb_cfg
*cfg
;
756 cfg
= container_of(c
, struct config_usb_cfg
, c
);
758 list_for_each_entry_safe(f
, tmp
, &c
->functions
, list
) {
760 list_move_tail(&f
->list
, &cfg
->func_list
);
762 dev_err(&gi
->cdev
.gadget
->dev
, "unbind function"
763 " '%s'/%p\n", f
->name
, f
);
767 c
->next_interface_id
= 0;
774 static int configfs_composite_bind(struct usb_gadget
*gadget
,
775 struct usb_gadget_driver
*gdriver
)
777 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
778 struct gadget_info
*gi
= container_of(composite
,
779 struct gadget_info
, composite
);
780 struct usb_composite_dev
*cdev
= &gi
->cdev
;
781 struct usb_configuration
*c
;
782 struct usb_string
*s
;
786 /* the gi->lock is hold by the caller */
787 cdev
->gadget
= gadget
;
788 set_gadget_data(gadget
, cdev
);
789 ret
= composite_dev_prepare(composite
, cdev
);
792 /* and now the gadget bind */
795 if (list_empty(&gi
->cdev
.configs
)) {
796 pr_err("Need atleast one configuration in %s.\n",
798 goto err_comp_cleanup
;
802 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
803 struct config_usb_cfg
*cfg
;
805 cfg
= container_of(c
, struct config_usb_cfg
, c
);
806 if (list_empty(&cfg
->func_list
)) {
807 pr_err("Config %s/%d of %s needs atleast one function.\n",
808 c
->label
, c
->bConfigurationValue
,
810 goto err_comp_cleanup
;
814 /* init all strings */
815 if (!list_empty(&gi
->string_list
)) {
816 struct gadget_strings
*gs
;
819 list_for_each_entry(gs
, &gi
->string_list
, list
) {
821 gi
->gstrings
[i
] = &gs
->stringtab_dev
;
822 gs
->stringtab_dev
.strings
= gs
->strings
;
823 gs
->strings
[USB_GADGET_MANUFACTURER_IDX
].s
=
825 gs
->strings
[USB_GADGET_PRODUCT_IDX
].s
= gs
->product
;
826 gs
->strings
[USB_GADGET_SERIAL_IDX
].s
= gs
->serialnumber
;
829 gi
->gstrings
[i
] = NULL
;
830 s
= usb_gstrings_attach(&gi
->cdev
, gi
->gstrings
,
831 USB_GADGET_FIRST_AVAIL_IDX
);
834 goto err_comp_cleanup
;
837 gi
->cdev
.desc
.iManufacturer
= s
[USB_GADGET_MANUFACTURER_IDX
].id
;
838 gi
->cdev
.desc
.iProduct
= s
[USB_GADGET_PRODUCT_IDX
].id
;
839 gi
->cdev
.desc
.iSerialNumber
= s
[USB_GADGET_SERIAL_IDX
].id
;
842 /* Go through all configs, attach all functions */
843 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
844 struct config_usb_cfg
*cfg
;
845 struct usb_function
*f
;
846 struct usb_function
*tmp
;
847 struct gadget_config_name
*cn
;
849 cfg
= container_of(c
, struct config_usb_cfg
, c
);
850 if (!list_empty(&cfg
->string_list
)) {
852 list_for_each_entry(cn
, &cfg
->string_list
, list
) {
853 cfg
->gstrings
[i
] = &cn
->stringtab_dev
;
854 cn
->stringtab_dev
.strings
= &cn
->strings
;
855 cn
->strings
.s
= cn
->configuration
;
858 cfg
->gstrings
[i
] = NULL
;
859 s
= usb_gstrings_attach(&gi
->cdev
, cfg
->gstrings
, 1);
862 goto err_comp_cleanup
;
864 c
->iConfiguration
= s
[0].id
;
867 list_for_each_entry_safe(f
, tmp
, &cfg
->func_list
, list
) {
869 ret
= usb_add_function(c
, f
);
871 list_add(&f
->list
, &cfg
->func_list
);
872 goto err_purge_funcs
;
875 usb_ep_autoconfig_reset(cdev
->gadget
);
877 usb_ep_autoconfig_reset(cdev
->gadget
);
881 purge_configs_funcs(gi
);
883 composite_dev_cleanup(cdev
);
887 static void configfs_composite_unbind(struct usb_gadget
*gadget
)
889 struct usb_composite_dev
*cdev
;
890 struct gadget_info
*gi
;
892 /* the gi->lock is hold by the caller */
894 cdev
= get_gadget_data(gadget
);
895 gi
= container_of(cdev
, struct gadget_info
, cdev
);
897 purge_configs_funcs(gi
);
898 composite_dev_cleanup(cdev
);
899 usb_ep_autoconfig_reset(cdev
->gadget
);
901 set_gadget_data(gadget
, NULL
);
904 static const struct usb_gadget_driver configfs_driver_template
= {
905 .bind
= configfs_composite_bind
,
906 .unbind
= configfs_composite_unbind
,
908 .setup
= composite_setup
,
909 .disconnect
= composite_disconnect
,
911 .max_speed
= USB_SPEED_SUPER
,
913 .owner
= THIS_MODULE
,
914 .name
= "configfs-gadget",
918 static struct config_group
*gadgets_make(
919 struct config_group
*group
,
922 struct gadget_info
*gi
;
924 gi
= kzalloc(sizeof(*gi
), GFP_KERNEL
);
926 return ERR_PTR(-ENOMEM
);
928 gi
->group
.default_groups
= gi
->default_groups
;
929 gi
->group
.default_groups
[0] = &gi
->functions_group
;
930 gi
->group
.default_groups
[1] = &gi
->configs_group
;
931 gi
->group
.default_groups
[2] = &gi
->strings_group
;
933 config_group_init_type_name(&gi
->functions_group
, "functions",
935 config_group_init_type_name(&gi
->configs_group
, "configs",
937 config_group_init_type_name(&gi
->strings_group
, "strings",
938 &gadget_strings_strings_type
);
940 gi
->composite
.bind
= configfs_do_nothing
;
941 gi
->composite
.unbind
= configfs_do_nothing
;
942 gi
->composite
.suspend
= NULL
;
943 gi
->composite
.resume
= NULL
;
944 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
946 mutex_init(&gi
->lock
);
947 INIT_LIST_HEAD(&gi
->string_list
);
948 INIT_LIST_HEAD(&gi
->available_func
);
950 composite_init_dev(&gi
->cdev
);
951 gi
->cdev
.desc
.bLength
= USB_DT_DEVICE_SIZE
;
952 gi
->cdev
.desc
.bDescriptorType
= USB_DT_DEVICE
;
953 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(get_default_bcdDevice());
955 gi
->composite
.gadget_driver
= configfs_driver_template
;
957 gi
->composite
.gadget_driver
.function
= kstrdup(name
, GFP_KERNEL
);
958 gi
->composite
.name
= gi
->composite
.gadget_driver
.function
;
960 if (!gi
->composite
.gadget_driver
.function
)
963 #ifdef CONFIG_USB_OTG
964 gi
->otg
.bLength
= sizeof(struct usb_otg_descriptor
);
965 gi
->otg
.bDescriptorType
= USB_DT_OTG
;
966 gi
->otg
.bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
;
969 config_group_init_type_name(&gi
->group
, name
,
974 return ERR_PTR(-ENOMEM
);
977 static void gadgets_drop(struct config_group
*group
, struct config_item
*item
)
979 config_item_put(item
);
982 static struct configfs_group_operations gadgets_ops
= {
983 .make_group
= &gadgets_make
,
984 .drop_item
= &gadgets_drop
,
987 static struct config_item_type gadgets_type
= {
988 .ct_group_ops
= &gadgets_ops
,
989 .ct_owner
= THIS_MODULE
,
992 static struct configfs_subsystem gadget_subsys
= {
995 .ci_namebuf
= "usb_gadget",
996 .ci_type
= &gadgets_type
,
999 .su_mutex
= __MUTEX_INITIALIZER(gadget_subsys
.su_mutex
),
1002 void unregister_gadget_item(struct config_item
*item
)
1004 struct gadget_info
*gi
= to_gadget_info(item
);
1006 unregister_gadget(gi
);
1008 EXPORT_SYMBOL(unregister_gadget_item
);
1010 static int __init
gadget_cfs_init(void)
1014 config_group_init(&gadget_subsys
.su_group
);
1016 ret
= configfs_register_subsystem(&gadget_subsys
);
1019 module_init(gadget_cfs_init
);
1021 static void __exit
gadget_cfs_exit(void)
1023 configfs_unregister_subsystem(&gadget_subsys
);
1025 module_exit(gadget_cfs_exit
);