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>
8 int check_user_usb_string(const char *name
,
9 struct usb_gadget_strings
*stringtab_dev
)
11 unsigned primary_lang
;
16 ret
= kstrtou16(name
, 0, &num
);
20 primary_lang
= num
& 0x3ff;
23 /* simple sanity check for valid langid */
24 switch (primary_lang
) {
33 stringtab_dev
->language
= num
;
37 #define MAX_NAME_LEN 40
38 #define MAX_USB_STRING_LANGS 2
41 struct config_group group
;
42 struct config_group functions_group
;
43 struct config_group configs_group
;
44 struct config_group strings_group
;
45 struct config_group
*default_groups
[4];
48 struct usb_gadget_strings
*gstrings
[MAX_USB_STRING_LANGS
+ 1];
49 struct list_head string_list
;
50 struct list_head available_func
;
54 struct usb_otg_descriptor otg
;
56 struct usb_composite_driver composite
;
57 struct usb_composite_dev cdev
;
60 struct config_usb_cfg
{
61 struct config_group group
;
62 struct config_group strings_group
;
63 struct config_group
*default_groups
[2];
64 struct list_head string_list
;
65 struct usb_configuration c
;
66 struct list_head func_list
;
67 struct usb_gadget_strings
*gstrings
[MAX_USB_STRING_LANGS
+ 1];
70 struct gadget_strings
{
71 struct usb_gadget_strings stringtab_dev
;
72 struct usb_string strings
[USB_GADGET_FIRST_AVAIL_IDX
];
77 struct config_group group
;
78 struct list_head list
;
81 struct gadget_config_name
{
82 struct usb_gadget_strings stringtab_dev
;
83 struct usb_string strings
;
86 struct config_group group
;
87 struct list_head list
;
90 static int usb_string_copy(const char *s
, char **s_copy
)
99 str
= kstrdup(s
, GFP_KERNEL
);
102 if (str
[ret
- 1] == '\n')
109 CONFIGFS_ATTR_STRUCT(gadget_info
);
110 CONFIGFS_ATTR_STRUCT(config_usb_cfg
);
112 #define GI_DEVICE_DESC_ITEM_ATTR(name) \
113 static struct gadget_info_attribute gadget_cdev_desc_##name = \
114 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
115 gadget_dev_desc_##name##_show, \
116 gadget_dev_desc_##name##_store)
118 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
119 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
122 return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
125 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
126 static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
129 return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
133 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
134 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
135 const char *page, size_t len) \
139 ret = kstrtou8(page, 0, &val); \
142 gi->cdev.desc._name = val; \
146 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
147 static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
148 const char *page, size_t len) \
152 ret = kstrtou16(page, 0, &val); \
155 gi->cdev.desc._name = cpu_to_le16p(&val); \
159 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
160 GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
161 GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
163 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB
);
164 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass
, u8
);
165 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass
, u8
);
166 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol
, u8
);
167 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0
, u8
);
168 GI_DEVICE_DESC_SIMPLE_RW(idVendor
, u16
);
169 GI_DEVICE_DESC_SIMPLE_RW(idProduct
, u16
);
170 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice
);
172 static ssize_t
is_valid_bcd(u16 bcd_val
)
174 if ((bcd_val
& 0xf) > 9)
176 if (((bcd_val
>> 4) & 0xf) > 9)
178 if (((bcd_val
>> 8) & 0xf) > 9)
180 if (((bcd_val
>> 12) & 0xf) > 9)
185 static ssize_t
gadget_dev_desc_bcdDevice_store(struct gadget_info
*gi
,
186 const char *page
, size_t len
)
191 ret
= kstrtou16(page
, 0, &bcdDevice
);
194 ret
= is_valid_bcd(bcdDevice
);
198 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
202 static ssize_t
gadget_dev_desc_bcdUSB_store(struct gadget_info
*gi
,
203 const char *page
, size_t len
)
208 ret
= kstrtou16(page
, 0, &bcdUSB
);
211 ret
= is_valid_bcd(bcdUSB
);
215 gi
->cdev
.desc
.bcdUSB
= cpu_to_le16(bcdUSB
);
219 static ssize_t
gadget_dev_desc_UDC_show(struct gadget_info
*gi
, char *page
)
221 return sprintf(page
, "%s\n", gi
->udc_name
?: "");
224 static int unregister_gadget(struct gadget_info
*gi
)
231 ret
= usb_gadget_unregister_driver(&gi
->composite
.gadget_driver
);
239 static ssize_t
gadget_dev_desc_UDC_store(struct gadget_info
*gi
,
240 const char *page
, size_t len
)
245 name
= kstrdup(page
, GFP_KERNEL
);
248 if (name
[len
- 1] == '\n')
249 name
[len
- 1] = '\0';
251 mutex_lock(&gi
->lock
);
254 ret
= unregister_gadget(gi
);
262 ret
= udc_attach_driver(name
, &gi
->composite
.gadget_driver
);
267 mutex_unlock(&gi
->lock
);
271 mutex_unlock(&gi
->lock
);
275 GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass
);
276 GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass
);
277 GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol
);
278 GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0
);
279 GI_DEVICE_DESC_ITEM_ATTR(idVendor
);
280 GI_DEVICE_DESC_ITEM_ATTR(idProduct
);
281 GI_DEVICE_DESC_ITEM_ATTR(bcdDevice
);
282 GI_DEVICE_DESC_ITEM_ATTR(bcdUSB
);
283 GI_DEVICE_DESC_ITEM_ATTR(UDC
);
285 static struct configfs_attribute
*gadget_root_attrs
[] = {
286 &gadget_cdev_desc_bDeviceClass
.attr
,
287 &gadget_cdev_desc_bDeviceSubClass
.attr
,
288 &gadget_cdev_desc_bDeviceProtocol
.attr
,
289 &gadget_cdev_desc_bMaxPacketSize0
.attr
,
290 &gadget_cdev_desc_idVendor
.attr
,
291 &gadget_cdev_desc_idProduct
.attr
,
292 &gadget_cdev_desc_bcdDevice
.attr
,
293 &gadget_cdev_desc_bcdUSB
.attr
,
294 &gadget_cdev_desc_UDC
.attr
,
298 static inline struct gadget_info
*to_gadget_info(struct config_item
*item
)
300 return container_of(to_config_group(item
), struct gadget_info
, group
);
303 static inline struct gadget_strings
*to_gadget_strings(struct config_item
*item
)
305 return container_of(to_config_group(item
), struct gadget_strings
,
309 static inline struct gadget_config_name
*to_gadget_config_name(
310 struct config_item
*item
)
312 return container_of(to_config_group(item
), struct gadget_config_name
,
316 static inline struct config_usb_cfg
*to_config_usb_cfg(struct config_item
*item
)
318 return container_of(to_config_group(item
), struct config_usb_cfg
,
322 static inline struct usb_function_instance
*to_usb_function_instance(
323 struct config_item
*item
)
325 return container_of(to_config_group(item
),
326 struct usb_function_instance
, group
);
329 static void gadget_info_attr_release(struct config_item
*item
)
331 struct gadget_info
*gi
= to_gadget_info(item
);
333 WARN_ON(!list_empty(&gi
->cdev
.configs
));
334 WARN_ON(!list_empty(&gi
->string_list
));
335 WARN_ON(!list_empty(&gi
->available_func
));
336 kfree(gi
->composite
.gadget_driver
.function
);
340 CONFIGFS_ATTR_OPS(gadget_info
);
342 static struct configfs_item_operations gadget_root_item_ops
= {
343 .release
= gadget_info_attr_release
,
344 .show_attribute
= gadget_info_attr_show
,
345 .store_attribute
= gadget_info_attr_store
,
348 static void gadget_config_attr_release(struct config_item
*item
)
350 struct config_usb_cfg
*cfg
= to_config_usb_cfg(item
);
352 WARN_ON(!list_empty(&cfg
->c
.functions
));
353 list_del(&cfg
->c
.list
);
358 static int config_usb_cfg_link(
359 struct config_item
*usb_cfg_ci
,
360 struct config_item
*usb_func_ci
)
362 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
363 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
364 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
366 struct config_group
*group
= to_config_group(usb_func_ci
);
367 struct usb_function_instance
*fi
= container_of(group
,
368 struct usb_function_instance
, group
);
369 struct usb_function_instance
*a_fi
;
370 struct usb_function
*f
;
373 mutex_lock(&gi
->lock
);
375 * Make sure this function is from within our _this_ gadget and not
376 * from another gadget or a random directory.
377 * Also a function instance can only be linked once.
379 list_for_each_entry(a_fi
, &gi
->available_func
, cfs_list
) {
388 list_for_each_entry(f
, &cfg
->func_list
, list
) {
395 f
= usb_get_function(fi
);
401 /* stash the function until we bind it to the gadget */
402 list_add_tail(&f
->list
, &cfg
->func_list
);
405 mutex_unlock(&gi
->lock
);
409 static int config_usb_cfg_unlink(
410 struct config_item
*usb_cfg_ci
,
411 struct config_item
*usb_func_ci
)
413 struct config_usb_cfg
*cfg
= to_config_usb_cfg(usb_cfg_ci
);
414 struct usb_composite_dev
*cdev
= cfg
->c
.cdev
;
415 struct gadget_info
*gi
= container_of(cdev
, struct gadget_info
, cdev
);
417 struct config_group
*group
= to_config_group(usb_func_ci
);
418 struct usb_function_instance
*fi
= container_of(group
,
419 struct usb_function_instance
, group
);
420 struct usb_function
*f
;
423 * ideally I would like to forbid to unlink functions while a gadget is
424 * bound to an UDC. Since this isn't possible at the moment, we simply
425 * force an unbind, the function is available here and then we can
426 * remove the function.
428 mutex_lock(&gi
->lock
);
430 unregister_gadget(gi
);
431 WARN_ON(gi
->udc_name
);
433 list_for_each_entry(f
, &cfg
->func_list
, list
) {
437 mutex_unlock(&gi
->lock
);
441 mutex_unlock(&gi
->lock
);
442 WARN(1, "Unable to locate function to unbind\n");
446 CONFIGFS_ATTR_OPS(config_usb_cfg
);
448 static struct configfs_item_operations gadget_config_item_ops
= {
449 .release
= gadget_config_attr_release
,
450 .show_attribute
= config_usb_cfg_attr_show
,
451 .store_attribute
= config_usb_cfg_attr_store
,
452 .allow_link
= config_usb_cfg_link
,
453 .drop_link
= config_usb_cfg_unlink
,
457 static ssize_t
gadget_config_desc_MaxPower_show(struct config_usb_cfg
*cfg
,
460 return sprintf(page
, "%u\n", cfg
->c
.MaxPower
);
463 static ssize_t
gadget_config_desc_MaxPower_store(struct config_usb_cfg
*cfg
,
464 const char *page
, size_t len
)
468 ret
= kstrtou16(page
, 0, &val
);
471 if (DIV_ROUND_UP(val
, 8) > 0xff)
473 cfg
->c
.MaxPower
= val
;
477 static ssize_t
gadget_config_desc_bmAttributes_show(struct config_usb_cfg
*cfg
,
480 return sprintf(page
, "0x%02x\n", cfg
->c
.bmAttributes
);
483 static ssize_t
gadget_config_desc_bmAttributes_store(struct config_usb_cfg
*cfg
,
484 const char *page
, size_t len
)
488 ret
= kstrtou8(page
, 0, &val
);
491 if (!(val
& USB_CONFIG_ATT_ONE
))
493 if (val
& ~(USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
|
494 USB_CONFIG_ATT_WAKEUP
))
496 cfg
->c
.bmAttributes
= val
;
500 #define CFG_CONFIG_DESC_ITEM_ATTR(name) \
501 static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
502 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
503 gadget_config_desc_##name##_show, \
504 gadget_config_desc_##name##_store)
506 CFG_CONFIG_DESC_ITEM_ATTR(MaxPower
);
507 CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes
);
509 static struct configfs_attribute
*gadget_config_attrs
[] = {
510 &gadget_usb_cfg_MaxPower
.attr
,
511 &gadget_usb_cfg_bmAttributes
.attr
,
515 static struct config_item_type gadget_config_type
= {
516 .ct_item_ops
= &gadget_config_item_ops
,
517 .ct_attrs
= gadget_config_attrs
,
518 .ct_owner
= THIS_MODULE
,
521 static struct config_item_type gadget_root_type
= {
522 .ct_item_ops
= &gadget_root_item_ops
,
523 .ct_attrs
= gadget_root_attrs
,
524 .ct_owner
= THIS_MODULE
,
527 static void composite_init_dev(struct usb_composite_dev
*cdev
)
529 spin_lock_init(&cdev
->lock
);
530 INIT_LIST_HEAD(&cdev
->configs
);
531 INIT_LIST_HEAD(&cdev
->gstrings
);
534 static struct config_group
*function_make(
535 struct config_group
*group
,
538 struct gadget_info
*gi
;
539 struct usb_function_instance
*fi
;
540 char buf
[MAX_NAME_LEN
];
545 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
546 if (ret
>= MAX_NAME_LEN
)
547 return ERR_PTR(-ENAMETOOLONG
);
550 instance_name
= strchr(func_name
, '.');
551 if (!instance_name
) {
552 pr_err("Unable to locate . in FUNC.INSTANCE\n");
553 return ERR_PTR(-EINVAL
);
555 *instance_name
= '\0';
558 fi
= usb_get_function_instance(func_name
);
560 return ERR_PTR(PTR_ERR(fi
));
562 ret
= config_item_set_name(&fi
->group
.cg_item
, name
);
564 usb_put_function_instance(fi
);
568 gi
= container_of(group
, struct gadget_info
, functions_group
);
570 mutex_lock(&gi
->lock
);
571 list_add_tail(&fi
->cfs_list
, &gi
->available_func
);
572 mutex_unlock(&gi
->lock
);
576 static void function_drop(
577 struct config_group
*group
,
578 struct config_item
*item
)
580 struct usb_function_instance
*fi
= to_usb_function_instance(item
);
581 struct gadget_info
*gi
;
583 gi
= container_of(group
, struct gadget_info
, functions_group
);
585 mutex_lock(&gi
->lock
);
586 list_del(&fi
->cfs_list
);
587 mutex_unlock(&gi
->lock
);
588 config_item_put(item
);
591 static struct configfs_group_operations functions_ops
= {
592 .make_group
= &function_make
,
593 .drop_item
= &function_drop
,
596 static struct config_item_type functions_type
= {
597 .ct_group_ops
= &functions_ops
,
598 .ct_owner
= THIS_MODULE
,
601 CONFIGFS_ATTR_STRUCT(gadget_config_name
);
602 GS_STRINGS_RW(gadget_config_name
, configuration
);
604 static struct configfs_attribute
*gadget_config_name_langid_attrs
[] = {
605 &gadget_config_name_configuration
.attr
,
609 static void gadget_config_name_attr_release(struct config_item
*item
)
611 struct gadget_config_name
*cn
= to_gadget_config_name(item
);
613 kfree(cn
->configuration
);
619 USB_CONFIG_STRING_RW_OPS(gadget_config_name
);
620 USB_CONFIG_STRINGS_LANG(gadget_config_name
, config_usb_cfg
);
622 static struct config_group
*config_desc_make(
623 struct config_group
*group
,
626 struct gadget_info
*gi
;
627 struct config_usb_cfg
*cfg
;
628 char buf
[MAX_NAME_LEN
];
633 gi
= container_of(group
, struct gadget_info
, configs_group
);
634 ret
= snprintf(buf
, MAX_NAME_LEN
, "%s", name
);
635 if (ret
>= MAX_NAME_LEN
)
636 return ERR_PTR(-ENAMETOOLONG
);
638 num_str
= strchr(buf
, '.');
640 pr_err("Unable to locate . in name.bConfigurationValue\n");
641 return ERR_PTR(-EINVAL
);
648 return ERR_PTR(-EINVAL
);
650 ret
= kstrtou8(num_str
, 0, &num
);
654 cfg
= kzalloc(sizeof(*cfg
), GFP_KERNEL
);
656 return ERR_PTR(-ENOMEM
);
657 cfg
->c
.label
= kstrdup(buf
, GFP_KERNEL
);
662 cfg
->c
.bConfigurationValue
= num
;
663 cfg
->c
.MaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
;
664 cfg
->c
.bmAttributes
= USB_CONFIG_ATT_ONE
;
665 INIT_LIST_HEAD(&cfg
->string_list
);
666 INIT_LIST_HEAD(&cfg
->func_list
);
668 cfg
->group
.default_groups
= cfg
->default_groups
;
669 cfg
->default_groups
[0] = &cfg
->strings_group
;
671 config_group_init_type_name(&cfg
->group
, name
,
672 &gadget_config_type
);
673 config_group_init_type_name(&cfg
->strings_group
, "strings",
674 &gadget_config_name_strings_type
);
676 ret
= usb_add_config_only(&gi
->cdev
, &cfg
->c
);
687 static void config_desc_drop(
688 struct config_group
*group
,
689 struct config_item
*item
)
691 config_item_put(item
);
694 static struct configfs_group_operations config_desc_ops
= {
695 .make_group
= &config_desc_make
,
696 .drop_item
= &config_desc_drop
,
699 static struct config_item_type config_desc_type
= {
700 .ct_group_ops
= &config_desc_ops
,
701 .ct_owner
= THIS_MODULE
,
704 CONFIGFS_ATTR_STRUCT(gadget_strings
);
705 GS_STRINGS_RW(gadget_strings
, manufacturer
);
706 GS_STRINGS_RW(gadget_strings
, product
);
707 GS_STRINGS_RW(gadget_strings
, serialnumber
);
709 static struct configfs_attribute
*gadget_strings_langid_attrs
[] = {
710 &gadget_strings_manufacturer
.attr
,
711 &gadget_strings_product
.attr
,
712 &gadget_strings_serialnumber
.attr
,
716 static void gadget_strings_attr_release(struct config_item
*item
)
718 struct gadget_strings
*gs
= to_gadget_strings(item
);
720 kfree(gs
->manufacturer
);
722 kfree(gs
->serialnumber
);
728 USB_CONFIG_STRING_RW_OPS(gadget_strings
);
729 USB_CONFIG_STRINGS_LANG(gadget_strings
, gadget_info
);
731 static int configfs_do_nothing(struct usb_composite_dev
*cdev
)
737 int composite_dev_prepare(struct usb_composite_driver
*composite
,
738 struct usb_composite_dev
*dev
);
740 static void purge_configs_funcs(struct gadget_info
*gi
)
742 struct usb_configuration
*c
;
744 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
745 struct usb_function
*f
, *tmp
;
746 struct config_usb_cfg
*cfg
;
748 cfg
= container_of(c
, struct config_usb_cfg
, c
);
750 list_for_each_entry_safe(f
, tmp
, &c
->functions
, list
) {
752 list_move_tail(&f
->list
, &cfg
->func_list
);
754 dev_err(&gi
->cdev
.gadget
->dev
, "unbind function"
755 " '%s'/%p\n", f
->name
, f
);
759 c
->next_interface_id
= 0;
766 static int configfs_composite_bind(struct usb_gadget
*gadget
,
767 struct usb_gadget_driver
*gdriver
)
769 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
770 struct gadget_info
*gi
= container_of(composite
,
771 struct gadget_info
, composite
);
772 struct usb_composite_dev
*cdev
= &gi
->cdev
;
773 struct usb_configuration
*c
;
774 struct usb_string
*s
;
778 /* the gi->lock is hold by the caller */
779 cdev
->gadget
= gadget
;
780 set_gadget_data(gadget
, cdev
);
781 ret
= composite_dev_prepare(composite
, cdev
);
784 /* and now the gadget bind */
787 if (list_empty(&gi
->cdev
.configs
)) {
788 pr_err("Need atleast one configuration in %s.\n",
790 goto err_comp_cleanup
;
794 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
795 struct config_usb_cfg
*cfg
;
797 cfg
= container_of(c
, struct config_usb_cfg
, c
);
798 if (list_empty(&cfg
->func_list
)) {
799 pr_err("Config %s/%d of %s needs atleast one function.\n",
800 c
->label
, c
->bConfigurationValue
,
802 goto err_comp_cleanup
;
806 /* init all strings */
807 if (!list_empty(&gi
->string_list
)) {
808 struct gadget_strings
*gs
;
811 list_for_each_entry(gs
, &gi
->string_list
, list
) {
813 gi
->gstrings
[i
] = &gs
->stringtab_dev
;
814 gs
->stringtab_dev
.strings
= gs
->strings
;
815 gs
->strings
[USB_GADGET_MANUFACTURER_IDX
].s
=
817 gs
->strings
[USB_GADGET_PRODUCT_IDX
].s
= gs
->product
;
818 gs
->strings
[USB_GADGET_SERIAL_IDX
].s
= gs
->serialnumber
;
821 gi
->gstrings
[i
] = NULL
;
822 s
= usb_gstrings_attach(&gi
->cdev
, gi
->gstrings
,
823 USB_GADGET_FIRST_AVAIL_IDX
);
826 goto err_comp_cleanup
;
829 gi
->cdev
.desc
.iManufacturer
= s
[USB_GADGET_MANUFACTURER_IDX
].id
;
830 gi
->cdev
.desc
.iProduct
= s
[USB_GADGET_PRODUCT_IDX
].id
;
831 gi
->cdev
.desc
.iSerialNumber
= s
[USB_GADGET_SERIAL_IDX
].id
;
834 /* Go through all configs, attach all functions */
835 list_for_each_entry(c
, &gi
->cdev
.configs
, list
) {
836 struct config_usb_cfg
*cfg
;
837 struct usb_function
*f
;
838 struct usb_function
*tmp
;
839 struct gadget_config_name
*cn
;
841 cfg
= container_of(c
, struct config_usb_cfg
, c
);
842 if (!list_empty(&cfg
->string_list
)) {
844 list_for_each_entry(cn
, &cfg
->string_list
, list
) {
845 cfg
->gstrings
[i
] = &cn
->stringtab_dev
;
846 cn
->stringtab_dev
.strings
= &cn
->strings
;
847 cn
->strings
.s
= cn
->configuration
;
850 cfg
->gstrings
[i
] = NULL
;
851 s
= usb_gstrings_attach(&gi
->cdev
, cfg
->gstrings
, 1);
854 goto err_comp_cleanup
;
856 c
->iConfiguration
= s
[0].id
;
859 list_for_each_entry_safe(f
, tmp
, &cfg
->func_list
, list
) {
861 ret
= usb_add_function(c
, f
);
863 list_add(&f
->list
, &cfg
->func_list
);
864 goto err_purge_funcs
;
867 usb_ep_autoconfig_reset(cdev
->gadget
);
869 usb_ep_autoconfig_reset(cdev
->gadget
);
873 purge_configs_funcs(gi
);
875 composite_dev_cleanup(cdev
);
879 static void configfs_composite_unbind(struct usb_gadget
*gadget
)
881 struct usb_composite_dev
*cdev
;
882 struct gadget_info
*gi
;
884 /* the gi->lock is hold by the caller */
886 cdev
= get_gadget_data(gadget
);
887 gi
= container_of(cdev
, struct gadget_info
, cdev
);
889 purge_configs_funcs(gi
);
890 composite_dev_cleanup(cdev
);
891 usb_ep_autoconfig_reset(cdev
->gadget
);
893 set_gadget_data(gadget
, NULL
);
896 static const struct usb_gadget_driver configfs_driver_template
= {
897 .bind
= configfs_composite_bind
,
898 .unbind
= configfs_composite_unbind
,
900 .setup
= composite_setup
,
901 .disconnect
= composite_disconnect
,
903 .max_speed
= USB_SPEED_SUPER
,
905 .owner
= THIS_MODULE
,
906 .name
= "configfs-gadget",
910 static struct config_group
*gadgets_make(
911 struct config_group
*group
,
914 struct gadget_info
*gi
;
916 gi
= kzalloc(sizeof(*gi
), GFP_KERNEL
);
918 return ERR_PTR(-ENOMEM
);
920 gi
->group
.default_groups
= gi
->default_groups
;
921 gi
->group
.default_groups
[0] = &gi
->functions_group
;
922 gi
->group
.default_groups
[1] = &gi
->configs_group
;
923 gi
->group
.default_groups
[2] = &gi
->strings_group
;
925 config_group_init_type_name(&gi
->functions_group
, "functions",
927 config_group_init_type_name(&gi
->configs_group
, "configs",
929 config_group_init_type_name(&gi
->strings_group
, "strings",
930 &gadget_strings_strings_type
);
932 gi
->composite
.bind
= configfs_do_nothing
;
933 gi
->composite
.unbind
= configfs_do_nothing
;
934 gi
->composite
.suspend
= NULL
;
935 gi
->composite
.resume
= NULL
;
936 gi
->composite
.max_speed
= USB_SPEED_SUPER
;
938 mutex_init(&gi
->lock
);
939 INIT_LIST_HEAD(&gi
->string_list
);
940 INIT_LIST_HEAD(&gi
->available_func
);
942 composite_init_dev(&gi
->cdev
);
943 gi
->cdev
.desc
.bLength
= USB_DT_DEVICE_SIZE
;
944 gi
->cdev
.desc
.bDescriptorType
= USB_DT_DEVICE
;
945 gi
->cdev
.desc
.bcdDevice
= cpu_to_le16(get_default_bcdDevice());
947 gi
->composite
.gadget_driver
= configfs_driver_template
;
949 gi
->composite
.gadget_driver
.function
= kstrdup(name
, GFP_KERNEL
);
950 gi
->composite
.name
= gi
->composite
.gadget_driver
.function
;
952 if (!gi
->composite
.gadget_driver
.function
)
955 #ifdef CONFIG_USB_OTG
956 gi
->otg
.bLength
= sizeof(struct usb_otg_descriptor
);
957 gi
->otg
.bDescriptorType
= USB_DT_OTG
;
958 gi
->otg
.bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
;
961 config_group_init_type_name(&gi
->group
, name
,
966 return ERR_PTR(-ENOMEM
);
969 static void gadgets_drop(struct config_group
*group
, struct config_item
*item
)
971 config_item_put(item
);
974 static struct configfs_group_operations gadgets_ops
= {
975 .make_group
= &gadgets_make
,
976 .drop_item
= &gadgets_drop
,
979 static struct config_item_type gadgets_type
= {
980 .ct_group_ops
= &gadgets_ops
,
981 .ct_owner
= THIS_MODULE
,
984 static struct configfs_subsystem gadget_subsys
= {
987 .ci_namebuf
= "usb_gadget",
988 .ci_type
= &gadgets_type
,
991 .su_mutex
= __MUTEX_INITIALIZER(gadget_subsys
.su_mutex
),
994 static int __init
gadget_cfs_init(void)
998 config_group_init(&gadget_subsys
.su_group
);
1000 ret
= configfs_register_subsystem(&gadget_subsys
);
1003 module_init(gadget_cfs_init
);
1005 static void __exit
gadget_cfs_exit(void)
1007 configfs_unregister_subsystem(&gadget_subsys
);
1009 module_exit(gadget_cfs_exit
);