xref: /linux/drivers/usb/gadget/configfs.c (revision 44f57d78)
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>
6 #include <linux/nls.h>
7 #include <linux/usb/composite.h>
8 #include <linux/usb/gadget_configfs.h>
9 #include "configfs.h"
10 #include "u_f.h"
11 #include "u_os_desc.h"
12 
13 int check_user_usb_string(const char *name,
14 		struct usb_gadget_strings *stringtab_dev)
15 {
16 	unsigned primary_lang;
17 	unsigned sub_lang;
18 	u16 num;
19 	int ret;
20 
21 	ret = kstrtou16(name, 0, &num);
22 	if (ret)
23 		return ret;
24 
25 	primary_lang = num & 0x3ff;
26 	sub_lang = num >> 10;
27 
28 	/* simple sanity check for valid langid */
29 	switch (primary_lang) {
30 	case 0:
31 	case 0x62 ... 0xfe:
32 	case 0x100 ... 0x3ff:
33 		return -EINVAL;
34 	}
35 	if (!sub_lang)
36 		return -EINVAL;
37 
38 	stringtab_dev->language = num;
39 	return 0;
40 }
41 
42 #define MAX_NAME_LEN	40
43 #define MAX_USB_STRING_LANGS 2
44 
45 static const struct usb_descriptor_header *otg_desc[2];
46 
47 struct gadget_info {
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;
53 
54 	struct mutex lock;
55 	struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
56 	struct list_head string_list;
57 	struct list_head available_func;
58 
59 	struct usb_composite_driver composite;
60 	struct usb_composite_dev cdev;
61 	bool use_os_desc;
62 	char b_vendor_code;
63 	char qw_sign[OS_STRING_QW_SIGN_LEN];
64 };
65 
66 static inline struct gadget_info *to_gadget_info(struct config_item *item)
67 {
68 	 return container_of(to_config_group(item), struct gadget_info, group);
69 }
70 
71 struct config_usb_cfg {
72 	struct config_group group;
73 	struct config_group strings_group;
74 	struct list_head string_list;
75 	struct usb_configuration c;
76 	struct list_head func_list;
77 	struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
78 };
79 
80 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
81 {
82 	return container_of(to_config_group(item), struct config_usb_cfg,
83 			group);
84 }
85 
86 struct gadget_strings {
87 	struct usb_gadget_strings stringtab_dev;
88 	struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
89 	char *manufacturer;
90 	char *product;
91 	char *serialnumber;
92 
93 	struct config_group group;
94 	struct list_head list;
95 };
96 
97 struct os_desc {
98 	struct config_group group;
99 };
100 
101 struct gadget_config_name {
102 	struct usb_gadget_strings stringtab_dev;
103 	struct usb_string strings;
104 	char *configuration;
105 
106 	struct config_group group;
107 	struct list_head list;
108 };
109 
110 static int usb_string_copy(const char *s, char **s_copy)
111 {
112 	int ret;
113 	char *str;
114 	char *copy = *s_copy;
115 	ret = strlen(s);
116 	if (ret > 126)
117 		return -EOVERFLOW;
118 
119 	str = kstrdup(s, GFP_KERNEL);
120 	if (!str)
121 		return -ENOMEM;
122 	if (str[ret - 1] == '\n')
123 		str[ret - 1] = '\0';
124 	kfree(copy);
125 	*s_copy = str;
126 	return 0;
127 }
128 
129 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name)	\
130 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
131 			char *page)	\
132 {	\
133 	return sprintf(page, "0x%02x\n", \
134 		to_gadget_info(item)->cdev.desc.__name); \
135 }
136 
137 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name)	\
138 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
139 			char *page)	\
140 {	\
141 	return sprintf(page, "0x%04x\n", \
142 		le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
143 }
144 
145 
146 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name)		\
147 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
148 		const char *page, size_t len)		\
149 {							\
150 	u8 val;						\
151 	int ret;					\
152 	ret = kstrtou8(page, 0, &val);			\
153 	if (ret)					\
154 		return ret;				\
155 	to_gadget_info(item)->cdev.desc._name = val;	\
156 	return len;					\
157 }
158 
159 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name)	\
160 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
161 		const char *page, size_t len)		\
162 {							\
163 	u16 val;					\
164 	int ret;					\
165 	ret = kstrtou16(page, 0, &val);			\
166 	if (ret)					\
167 		return ret;				\
168 	to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val);	\
169 	return len;					\
170 }
171 
172 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type)	\
173 	GI_DEVICE_DESC_SIMPLE_R_##_type(_name)	\
174 	GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
175 
176 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
177 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
178 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
179 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
180 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
181 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
182 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
183 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
184 
185 static ssize_t is_valid_bcd(u16 bcd_val)
186 {
187 	if ((bcd_val & 0xf) > 9)
188 		return -EINVAL;
189 	if (((bcd_val >> 4) & 0xf) > 9)
190 		return -EINVAL;
191 	if (((bcd_val >> 8) & 0xf) > 9)
192 		return -EINVAL;
193 	if (((bcd_val >> 12) & 0xf) > 9)
194 		return -EINVAL;
195 	return 0;
196 }
197 
198 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
199 		const char *page, size_t len)
200 {
201 	u16 bcdDevice;
202 	int ret;
203 
204 	ret = kstrtou16(page, 0, &bcdDevice);
205 	if (ret)
206 		return ret;
207 	ret = is_valid_bcd(bcdDevice);
208 	if (ret)
209 		return ret;
210 
211 	to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
212 	return len;
213 }
214 
215 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
216 		const char *page, size_t len)
217 {
218 	u16 bcdUSB;
219 	int ret;
220 
221 	ret = kstrtou16(page, 0, &bcdUSB);
222 	if (ret)
223 		return ret;
224 	ret = is_valid_bcd(bcdUSB);
225 	if (ret)
226 		return ret;
227 
228 	to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
229 	return len;
230 }
231 
232 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
233 {
234 	char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
235 
236 	return sprintf(page, "%s\n", udc_name ?: "");
237 }
238 
239 static int unregister_gadget(struct gadget_info *gi)
240 {
241 	int ret;
242 
243 	if (!gi->composite.gadget_driver.udc_name)
244 		return -ENODEV;
245 
246 	ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
247 	if (ret)
248 		return ret;
249 	kfree(gi->composite.gadget_driver.udc_name);
250 	gi->composite.gadget_driver.udc_name = NULL;
251 	return 0;
252 }
253 
254 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
255 		const char *page, size_t len)
256 {
257 	struct gadget_info *gi = to_gadget_info(item);
258 	char *name;
259 	int ret;
260 
261 	name = kstrdup(page, GFP_KERNEL);
262 	if (!name)
263 		return -ENOMEM;
264 	if (name[len - 1] == '\n')
265 		name[len - 1] = '\0';
266 
267 	mutex_lock(&gi->lock);
268 
269 	if (!strlen(name)) {
270 		ret = unregister_gadget(gi);
271 		if (ret)
272 			goto err;
273 		kfree(name);
274 	} else {
275 		if (gi->composite.gadget_driver.udc_name) {
276 			ret = -EBUSY;
277 			goto err;
278 		}
279 		gi->composite.gadget_driver.udc_name = name;
280 		ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
281 		if (ret) {
282 			gi->composite.gadget_driver.udc_name = NULL;
283 			goto err;
284 		}
285 	}
286 	mutex_unlock(&gi->lock);
287 	return len;
288 err:
289 	kfree(name);
290 	mutex_unlock(&gi->lock);
291 	return ret;
292 }
293 
294 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
295 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
296 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
297 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
298 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
299 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
300 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
301 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
302 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
303 
304 static struct configfs_attribute *gadget_root_attrs[] = {
305 	&gadget_dev_desc_attr_bDeviceClass,
306 	&gadget_dev_desc_attr_bDeviceSubClass,
307 	&gadget_dev_desc_attr_bDeviceProtocol,
308 	&gadget_dev_desc_attr_bMaxPacketSize0,
309 	&gadget_dev_desc_attr_idVendor,
310 	&gadget_dev_desc_attr_idProduct,
311 	&gadget_dev_desc_attr_bcdDevice,
312 	&gadget_dev_desc_attr_bcdUSB,
313 	&gadget_dev_desc_attr_UDC,
314 	NULL,
315 };
316 
317 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
318 {
319 	 return container_of(to_config_group(item), struct gadget_strings,
320 			 group);
321 }
322 
323 static inline struct gadget_config_name *to_gadget_config_name(
324 		struct config_item *item)
325 {
326 	 return container_of(to_config_group(item), struct gadget_config_name,
327 			 group);
328 }
329 
330 static inline struct usb_function_instance *to_usb_function_instance(
331 		struct config_item *item)
332 {
333 	 return container_of(to_config_group(item),
334 			 struct usb_function_instance, group);
335 }
336 
337 static void gadget_info_attr_release(struct config_item *item)
338 {
339 	struct gadget_info *gi = to_gadget_info(item);
340 
341 	WARN_ON(!list_empty(&gi->cdev.configs));
342 	WARN_ON(!list_empty(&gi->string_list));
343 	WARN_ON(!list_empty(&gi->available_func));
344 	kfree(gi->composite.gadget_driver.function);
345 	kfree(gi);
346 }
347 
348 static struct configfs_item_operations gadget_root_item_ops = {
349 	.release                = gadget_info_attr_release,
350 };
351 
352 static void gadget_config_attr_release(struct config_item *item)
353 {
354 	struct config_usb_cfg *cfg = to_config_usb_cfg(item);
355 
356 	WARN_ON(!list_empty(&cfg->c.functions));
357 	list_del(&cfg->c.list);
358 	kfree(cfg->c.label);
359 	kfree(cfg);
360 }
361 
362 static int config_usb_cfg_link(
363 	struct config_item *usb_cfg_ci,
364 	struct config_item *usb_func_ci)
365 {
366 	struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
367 	struct usb_composite_dev *cdev = cfg->c.cdev;
368 	struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
369 
370 	struct config_group *group = to_config_group(usb_func_ci);
371 	struct usb_function_instance *fi = container_of(group,
372 			struct usb_function_instance, group);
373 	struct usb_function_instance *a_fi;
374 	struct usb_function *f;
375 	int ret;
376 
377 	mutex_lock(&gi->lock);
378 	/*
379 	 * Make sure this function is from within our _this_ gadget and not
380 	 * from another gadget or a random directory.
381 	 * Also a function instance can only be linked once.
382 	 */
383 	list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
384 		if (a_fi == fi)
385 			break;
386 	}
387 	if (a_fi != fi) {
388 		ret = -EINVAL;
389 		goto out;
390 	}
391 
392 	list_for_each_entry(f, &cfg->func_list, list) {
393 		if (f->fi == fi) {
394 			ret = -EEXIST;
395 			goto out;
396 		}
397 	}
398 
399 	f = usb_get_function(fi);
400 	if (IS_ERR(f)) {
401 		ret = PTR_ERR(f);
402 		goto out;
403 	}
404 
405 	/* stash the function until we bind it to the gadget */
406 	list_add_tail(&f->list, &cfg->func_list);
407 	ret = 0;
408 out:
409 	mutex_unlock(&gi->lock);
410 	return ret;
411 }
412 
413 static void config_usb_cfg_unlink(
414 	struct config_item *usb_cfg_ci,
415 	struct config_item *usb_func_ci)
416 {
417 	struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
418 	struct usb_composite_dev *cdev = cfg->c.cdev;
419 	struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
420 
421 	struct config_group *group = to_config_group(usb_func_ci);
422 	struct usb_function_instance *fi = container_of(group,
423 			struct usb_function_instance, group);
424 	struct usb_function *f;
425 
426 	/*
427 	 * ideally I would like to forbid to unlink functions while a gadget is
428 	 * bound to an UDC. Since this isn't possible at the moment, we simply
429 	 * force an unbind, the function is available here and then we can
430 	 * remove the function.
431 	 */
432 	mutex_lock(&gi->lock);
433 	if (gi->composite.gadget_driver.udc_name)
434 		unregister_gadget(gi);
435 	WARN_ON(gi->composite.gadget_driver.udc_name);
436 
437 	list_for_each_entry(f, &cfg->func_list, list) {
438 		if (f->fi == fi) {
439 			list_del(&f->list);
440 			usb_put_function(f);
441 			mutex_unlock(&gi->lock);
442 			return;
443 		}
444 	}
445 	mutex_unlock(&gi->lock);
446 	WARN(1, "Unable to locate function to unbind\n");
447 }
448 
449 static struct configfs_item_operations gadget_config_item_ops = {
450 	.release                = gadget_config_attr_release,
451 	.allow_link             = config_usb_cfg_link,
452 	.drop_link              = config_usb_cfg_unlink,
453 };
454 
455 
456 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
457 		char *page)
458 {
459 	return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
460 }
461 
462 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
463 		const char *page, size_t len)
464 {
465 	u16 val;
466 	int ret;
467 	ret = kstrtou16(page, 0, &val);
468 	if (ret)
469 		return ret;
470 	if (DIV_ROUND_UP(val, 8) > 0xff)
471 		return -ERANGE;
472 	to_config_usb_cfg(item)->c.MaxPower = val;
473 	return len;
474 }
475 
476 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
477 		char *page)
478 {
479 	return sprintf(page, "0x%02x\n",
480 		to_config_usb_cfg(item)->c.bmAttributes);
481 }
482 
483 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
484 		const char *page, size_t len)
485 {
486 	u8 val;
487 	int ret;
488 	ret = kstrtou8(page, 0, &val);
489 	if (ret)
490 		return ret;
491 	if (!(val & USB_CONFIG_ATT_ONE))
492 		return -EINVAL;
493 	if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
494 				USB_CONFIG_ATT_WAKEUP))
495 		return -EINVAL;
496 	to_config_usb_cfg(item)->c.bmAttributes = val;
497 	return len;
498 }
499 
500 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
501 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
502 
503 static struct configfs_attribute *gadget_config_attrs[] = {
504 	&gadget_config_desc_attr_MaxPower,
505 	&gadget_config_desc_attr_bmAttributes,
506 	NULL,
507 };
508 
509 static const struct config_item_type gadget_config_type = {
510 	.ct_item_ops	= &gadget_config_item_ops,
511 	.ct_attrs	= gadget_config_attrs,
512 	.ct_owner	= THIS_MODULE,
513 };
514 
515 static const struct config_item_type gadget_root_type = {
516 	.ct_item_ops	= &gadget_root_item_ops,
517 	.ct_attrs	= gadget_root_attrs,
518 	.ct_owner	= THIS_MODULE,
519 };
520 
521 static void composite_init_dev(struct usb_composite_dev *cdev)
522 {
523 	spin_lock_init(&cdev->lock);
524 	INIT_LIST_HEAD(&cdev->configs);
525 	INIT_LIST_HEAD(&cdev->gstrings);
526 }
527 
528 static struct config_group *function_make(
529 		struct config_group *group,
530 		const char *name)
531 {
532 	struct gadget_info *gi;
533 	struct usb_function_instance *fi;
534 	char buf[MAX_NAME_LEN];
535 	char *func_name;
536 	char *instance_name;
537 	int ret;
538 
539 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
540 	if (ret >= MAX_NAME_LEN)
541 		return ERR_PTR(-ENAMETOOLONG);
542 
543 	func_name = buf;
544 	instance_name = strchr(func_name, '.');
545 	if (!instance_name) {
546 		pr_err("Unable to locate . in FUNC.INSTANCE\n");
547 		return ERR_PTR(-EINVAL);
548 	}
549 	*instance_name = '\0';
550 	instance_name++;
551 
552 	fi = usb_get_function_instance(func_name);
553 	if (IS_ERR(fi))
554 		return ERR_CAST(fi);
555 
556 	ret = config_item_set_name(&fi->group.cg_item, "%s", name);
557 	if (ret) {
558 		usb_put_function_instance(fi);
559 		return ERR_PTR(ret);
560 	}
561 	if (fi->set_inst_name) {
562 		ret = fi->set_inst_name(fi, instance_name);
563 		if (ret) {
564 			usb_put_function_instance(fi);
565 			return ERR_PTR(ret);
566 		}
567 	}
568 
569 	gi = container_of(group, struct gadget_info, functions_group);
570 
571 	mutex_lock(&gi->lock);
572 	list_add_tail(&fi->cfs_list, &gi->available_func);
573 	mutex_unlock(&gi->lock);
574 	return &fi->group;
575 }
576 
577 static void function_drop(
578 		struct config_group *group,
579 		struct config_item *item)
580 {
581 	struct usb_function_instance *fi = to_usb_function_instance(item);
582 	struct gadget_info *gi;
583 
584 	gi = container_of(group, struct gadget_info, functions_group);
585 
586 	mutex_lock(&gi->lock);
587 	list_del(&fi->cfs_list);
588 	mutex_unlock(&gi->lock);
589 	config_item_put(item);
590 }
591 
592 static struct configfs_group_operations functions_ops = {
593 	.make_group     = &function_make,
594 	.drop_item      = &function_drop,
595 };
596 
597 static const struct config_item_type functions_type = {
598 	.ct_group_ops   = &functions_ops,
599 	.ct_owner       = THIS_MODULE,
600 };
601 
602 GS_STRINGS_RW(gadget_config_name, configuration);
603 
604 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
605 	&gadget_config_name_attr_configuration,
606 	NULL,
607 };
608 
609 static void gadget_config_name_attr_release(struct config_item *item)
610 {
611 	struct gadget_config_name *cn = to_gadget_config_name(item);
612 
613 	kfree(cn->configuration);
614 
615 	list_del(&cn->list);
616 	kfree(cn);
617 }
618 
619 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
620 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
621 
622 static struct config_group *config_desc_make(
623 		struct config_group *group,
624 		const char *name)
625 {
626 	struct gadget_info *gi;
627 	struct config_usb_cfg *cfg;
628 	char buf[MAX_NAME_LEN];
629 	char *num_str;
630 	u8 num;
631 	int ret;
632 
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);
637 
638 	num_str = strchr(buf, '.');
639 	if (!num_str) {
640 		pr_err("Unable to locate . in name.bConfigurationValue\n");
641 		return ERR_PTR(-EINVAL);
642 	}
643 
644 	*num_str = '\0';
645 	num_str++;
646 
647 	if (!strlen(buf))
648 		return ERR_PTR(-EINVAL);
649 
650 	ret = kstrtou8(num_str, 0, &num);
651 	if (ret)
652 		return ERR_PTR(ret);
653 
654 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
655 	if (!cfg)
656 		return ERR_PTR(-ENOMEM);
657 	cfg->c.label = kstrdup(buf, GFP_KERNEL);
658 	if (!cfg->c.label) {
659 		ret = -ENOMEM;
660 		goto err;
661 	}
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);
667 
668 	config_group_init_type_name(&cfg->group, name,
669 				&gadget_config_type);
670 
671 	config_group_init_type_name(&cfg->strings_group, "strings",
672 			&gadget_config_name_strings_type);
673 	configfs_add_default_group(&cfg->strings_group, &cfg->group);
674 
675 	ret = usb_add_config_only(&gi->cdev, &cfg->c);
676 	if (ret)
677 		goto err;
678 
679 	return &cfg->group;
680 err:
681 	kfree(cfg->c.label);
682 	kfree(cfg);
683 	return ERR_PTR(ret);
684 }
685 
686 static void config_desc_drop(
687 		struct config_group *group,
688 		struct config_item *item)
689 {
690 	config_item_put(item);
691 }
692 
693 static struct configfs_group_operations config_desc_ops = {
694 	.make_group     = &config_desc_make,
695 	.drop_item      = &config_desc_drop,
696 };
697 
698 static const struct config_item_type config_desc_type = {
699 	.ct_group_ops   = &config_desc_ops,
700 	.ct_owner       = THIS_MODULE,
701 };
702 
703 GS_STRINGS_RW(gadget_strings, manufacturer);
704 GS_STRINGS_RW(gadget_strings, product);
705 GS_STRINGS_RW(gadget_strings, serialnumber);
706 
707 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
708 	&gadget_strings_attr_manufacturer,
709 	&gadget_strings_attr_product,
710 	&gadget_strings_attr_serialnumber,
711 	NULL,
712 };
713 
714 static void gadget_strings_attr_release(struct config_item *item)
715 {
716 	struct gadget_strings *gs = to_gadget_strings(item);
717 
718 	kfree(gs->manufacturer);
719 	kfree(gs->product);
720 	kfree(gs->serialnumber);
721 
722 	list_del(&gs->list);
723 	kfree(gs);
724 }
725 
726 USB_CONFIG_STRING_RW_OPS(gadget_strings);
727 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
728 
729 static inline struct os_desc *to_os_desc(struct config_item *item)
730 {
731 	return container_of(to_config_group(item), struct os_desc, group);
732 }
733 
734 static inline struct gadget_info *os_desc_item_to_gadget_info(
735 		struct config_item *item)
736 {
737 	return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
738 }
739 
740 static ssize_t os_desc_use_show(struct config_item *item, char *page)
741 {
742 	return sprintf(page, "%d\n",
743 			os_desc_item_to_gadget_info(item)->use_os_desc);
744 }
745 
746 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
747 				 size_t len)
748 {
749 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
750 	int ret;
751 	bool use;
752 
753 	mutex_lock(&gi->lock);
754 	ret = strtobool(page, &use);
755 	if (!ret) {
756 		gi->use_os_desc = use;
757 		ret = len;
758 	}
759 	mutex_unlock(&gi->lock);
760 
761 	return ret;
762 }
763 
764 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
765 {
766 	return sprintf(page, "0x%02x\n",
767 			os_desc_item_to_gadget_info(item)->b_vendor_code);
768 }
769 
770 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
771 					   const char *page, size_t len)
772 {
773 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
774 	int ret;
775 	u8 b_vendor_code;
776 
777 	mutex_lock(&gi->lock);
778 	ret = kstrtou8(page, 0, &b_vendor_code);
779 	if (!ret) {
780 		gi->b_vendor_code = b_vendor_code;
781 		ret = len;
782 	}
783 	mutex_unlock(&gi->lock);
784 
785 	return ret;
786 }
787 
788 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
789 {
790 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
791 	int res;
792 
793 	res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
794 			      UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
795 	page[res++] = '\n';
796 
797 	return res;
798 }
799 
800 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
801 				     size_t len)
802 {
803 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
804 	int res, l;
805 
806 	l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
807 	if (page[l - 1] == '\n')
808 		--l;
809 
810 	mutex_lock(&gi->lock);
811 	res = utf8s_to_utf16s(page, l,
812 			      UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
813 			      OS_STRING_QW_SIGN_LEN);
814 	if (res > 0)
815 		res = len;
816 	mutex_unlock(&gi->lock);
817 
818 	return res;
819 }
820 
821 CONFIGFS_ATTR(os_desc_, use);
822 CONFIGFS_ATTR(os_desc_, b_vendor_code);
823 CONFIGFS_ATTR(os_desc_, qw_sign);
824 
825 static struct configfs_attribute *os_desc_attrs[] = {
826 	&os_desc_attr_use,
827 	&os_desc_attr_b_vendor_code,
828 	&os_desc_attr_qw_sign,
829 	NULL,
830 };
831 
832 static void os_desc_attr_release(struct config_item *item)
833 {
834 	struct os_desc *os_desc = to_os_desc(item);
835 	kfree(os_desc);
836 }
837 
838 static int os_desc_link(struct config_item *os_desc_ci,
839 			struct config_item *usb_cfg_ci)
840 {
841 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
842 					struct gadget_info, os_desc_group);
843 	struct usb_composite_dev *cdev = &gi->cdev;
844 	struct config_usb_cfg *c_target =
845 		container_of(to_config_group(usb_cfg_ci),
846 			     struct config_usb_cfg, group);
847 	struct usb_configuration *c;
848 	int ret;
849 
850 	mutex_lock(&gi->lock);
851 	list_for_each_entry(c, &cdev->configs, list) {
852 		if (c == &c_target->c)
853 			break;
854 	}
855 	if (c != &c_target->c) {
856 		ret = -EINVAL;
857 		goto out;
858 	}
859 
860 	if (cdev->os_desc_config) {
861 		ret = -EBUSY;
862 		goto out;
863 	}
864 
865 	cdev->os_desc_config = &c_target->c;
866 	ret = 0;
867 
868 out:
869 	mutex_unlock(&gi->lock);
870 	return ret;
871 }
872 
873 static void os_desc_unlink(struct config_item *os_desc_ci,
874 			  struct config_item *usb_cfg_ci)
875 {
876 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
877 					struct gadget_info, os_desc_group);
878 	struct usb_composite_dev *cdev = &gi->cdev;
879 
880 	mutex_lock(&gi->lock);
881 	if (gi->composite.gadget_driver.udc_name)
882 		unregister_gadget(gi);
883 	cdev->os_desc_config = NULL;
884 	WARN_ON(gi->composite.gadget_driver.udc_name);
885 	mutex_unlock(&gi->lock);
886 }
887 
888 static struct configfs_item_operations os_desc_ops = {
889 	.release                = os_desc_attr_release,
890 	.allow_link		= os_desc_link,
891 	.drop_link		= os_desc_unlink,
892 };
893 
894 static struct config_item_type os_desc_type = {
895 	.ct_item_ops	= &os_desc_ops,
896 	.ct_attrs	= os_desc_attrs,
897 	.ct_owner	= THIS_MODULE,
898 };
899 
900 static inline struct usb_os_desc_ext_prop
901 *to_usb_os_desc_ext_prop(struct config_item *item)
902 {
903 	return container_of(item, struct usb_os_desc_ext_prop, item);
904 }
905 
906 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
907 {
908 	return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
909 }
910 
911 static ssize_t ext_prop_type_store(struct config_item *item,
912 				   const char *page, size_t len)
913 {
914 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
915 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
916 	u8 type;
917 	int ret;
918 
919 	if (desc->opts_mutex)
920 		mutex_lock(desc->opts_mutex);
921 	ret = kstrtou8(page, 0, &type);
922 	if (ret)
923 		goto end;
924 	if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
925 		ret = -EINVAL;
926 		goto end;
927 	}
928 
929 	if ((ext_prop->type == USB_EXT_PROP_BINARY ||
930 	    ext_prop->type == USB_EXT_PROP_LE32 ||
931 	    ext_prop->type == USB_EXT_PROP_BE32) &&
932 	    (type == USB_EXT_PROP_UNICODE ||
933 	    type == USB_EXT_PROP_UNICODE_ENV ||
934 	    type == USB_EXT_PROP_UNICODE_LINK))
935 		ext_prop->data_len <<= 1;
936 	else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
937 		   ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
938 		   ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
939 		   (type == USB_EXT_PROP_BINARY ||
940 		   type == USB_EXT_PROP_LE32 ||
941 		   type == USB_EXT_PROP_BE32))
942 		ext_prop->data_len >>= 1;
943 	ext_prop->type = type;
944 	ret = len;
945 
946 end:
947 	if (desc->opts_mutex)
948 		mutex_unlock(desc->opts_mutex);
949 	return ret;
950 }
951 
952 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
953 {
954 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
955 	int len = ext_prop->data_len;
956 
957 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
958 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
959 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
960 		len >>= 1;
961 	memcpy(page, ext_prop->data, len);
962 
963 	return len;
964 }
965 
966 static ssize_t ext_prop_data_store(struct config_item *item,
967 				   const char *page, size_t len)
968 {
969 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
970 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
971 	char *new_data;
972 	size_t ret_len = len;
973 
974 	if (page[len - 1] == '\n' || page[len - 1] == '\0')
975 		--len;
976 	new_data = kmemdup(page, len, GFP_KERNEL);
977 	if (!new_data)
978 		return -ENOMEM;
979 
980 	if (desc->opts_mutex)
981 		mutex_lock(desc->opts_mutex);
982 	kfree(ext_prop->data);
983 	ext_prop->data = new_data;
984 	desc->ext_prop_len -= ext_prop->data_len;
985 	ext_prop->data_len = len;
986 	desc->ext_prop_len += ext_prop->data_len;
987 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
988 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
989 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
990 		desc->ext_prop_len -= ext_prop->data_len;
991 		ext_prop->data_len <<= 1;
992 		ext_prop->data_len += 2;
993 		desc->ext_prop_len += ext_prop->data_len;
994 	}
995 	if (desc->opts_mutex)
996 		mutex_unlock(desc->opts_mutex);
997 	return ret_len;
998 }
999 
1000 CONFIGFS_ATTR(ext_prop_, type);
1001 CONFIGFS_ATTR(ext_prop_, data);
1002 
1003 static struct configfs_attribute *ext_prop_attrs[] = {
1004 	&ext_prop_attr_type,
1005 	&ext_prop_attr_data,
1006 	NULL,
1007 };
1008 
1009 static void usb_os_desc_ext_prop_release(struct config_item *item)
1010 {
1011 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1012 
1013 	kfree(ext_prop); /* frees a whole chunk */
1014 }
1015 
1016 static struct configfs_item_operations ext_prop_ops = {
1017 	.release		= usb_os_desc_ext_prop_release,
1018 };
1019 
1020 static struct config_item *ext_prop_make(
1021 		struct config_group *group,
1022 		const char *name)
1023 {
1024 	struct usb_os_desc_ext_prop *ext_prop;
1025 	struct config_item_type *ext_prop_type;
1026 	struct usb_os_desc *desc;
1027 	char *vlabuf;
1028 
1029 	vla_group(data_chunk);
1030 	vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1031 	vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1032 
1033 	vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1034 	if (!vlabuf)
1035 		return ERR_PTR(-ENOMEM);
1036 
1037 	ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1038 	ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1039 
1040 	desc = container_of(group, struct usb_os_desc, group);
1041 	ext_prop_type->ct_item_ops = &ext_prop_ops;
1042 	ext_prop_type->ct_attrs = ext_prop_attrs;
1043 	ext_prop_type->ct_owner = desc->owner;
1044 
1045 	config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1046 
1047 	ext_prop->name = kstrdup(name, GFP_KERNEL);
1048 	if (!ext_prop->name) {
1049 		kfree(vlabuf);
1050 		return ERR_PTR(-ENOMEM);
1051 	}
1052 	desc->ext_prop_len += 14;
1053 	ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1054 	if (desc->opts_mutex)
1055 		mutex_lock(desc->opts_mutex);
1056 	desc->ext_prop_len += ext_prop->name_len;
1057 	list_add_tail(&ext_prop->entry, &desc->ext_prop);
1058 	++desc->ext_prop_count;
1059 	if (desc->opts_mutex)
1060 		mutex_unlock(desc->opts_mutex);
1061 
1062 	return &ext_prop->item;
1063 }
1064 
1065 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1066 {
1067 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1068 	struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1069 
1070 	if (desc->opts_mutex)
1071 		mutex_lock(desc->opts_mutex);
1072 	list_del(&ext_prop->entry);
1073 	--desc->ext_prop_count;
1074 	kfree(ext_prop->name);
1075 	desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1076 	if (desc->opts_mutex)
1077 		mutex_unlock(desc->opts_mutex);
1078 	config_item_put(item);
1079 }
1080 
1081 static struct configfs_group_operations interf_grp_ops = {
1082 	.make_item	= &ext_prop_make,
1083 	.drop_item	= &ext_prop_drop,
1084 };
1085 
1086 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1087 					     char *page)
1088 {
1089 	memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1090 	return 8;
1091 }
1092 
1093 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1094 					      const char *page, size_t len)
1095 {
1096 	struct usb_os_desc *desc = to_usb_os_desc(item);
1097 	int l;
1098 
1099 	l = min_t(int, 8, len);
1100 	if (page[l - 1] == '\n')
1101 		--l;
1102 	if (desc->opts_mutex)
1103 		mutex_lock(desc->opts_mutex);
1104 	memcpy(desc->ext_compat_id, page, l);
1105 
1106 	if (desc->opts_mutex)
1107 		mutex_unlock(desc->opts_mutex);
1108 
1109 	return len;
1110 }
1111 
1112 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1113 						 char *page)
1114 {
1115 	memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1116 	return 8;
1117 }
1118 
1119 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1120 						  const char *page, size_t len)
1121 {
1122 	struct usb_os_desc *desc = to_usb_os_desc(item);
1123 	int l;
1124 
1125 	l = min_t(int, 8, len);
1126 	if (page[l - 1] == '\n')
1127 		--l;
1128 	if (desc->opts_mutex)
1129 		mutex_lock(desc->opts_mutex);
1130 	memcpy(desc->ext_compat_id + 8, page, l);
1131 
1132 	if (desc->opts_mutex)
1133 		mutex_unlock(desc->opts_mutex);
1134 
1135 	return len;
1136 }
1137 
1138 CONFIGFS_ATTR(interf_grp_, compatible_id);
1139 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1140 
1141 static struct configfs_attribute *interf_grp_attrs[] = {
1142 	&interf_grp_attr_compatible_id,
1143 	&interf_grp_attr_sub_compatible_id,
1144 	NULL
1145 };
1146 
1147 struct config_group *usb_os_desc_prepare_interf_dir(
1148 		struct config_group *parent,
1149 		int n_interf,
1150 		struct usb_os_desc **desc,
1151 		char **names,
1152 		struct module *owner)
1153 {
1154 	struct config_group *os_desc_group;
1155 	struct config_item_type *os_desc_type, *interface_type;
1156 
1157 	vla_group(data_chunk);
1158 	vla_item(data_chunk, struct config_group, os_desc_group, 1);
1159 	vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1160 	vla_item(data_chunk, struct config_item_type, interface_type, 1);
1161 
1162 	char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1163 	if (!vlabuf)
1164 		return ERR_PTR(-ENOMEM);
1165 
1166 	os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1167 	os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1168 	interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1169 
1170 	os_desc_type->ct_owner = owner;
1171 	config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1172 	configfs_add_default_group(os_desc_group, parent);
1173 
1174 	interface_type->ct_group_ops = &interf_grp_ops;
1175 	interface_type->ct_attrs = interf_grp_attrs;
1176 	interface_type->ct_owner = owner;
1177 
1178 	while (n_interf--) {
1179 		struct usb_os_desc *d;
1180 
1181 		d = desc[n_interf];
1182 		d->owner = owner;
1183 		config_group_init_type_name(&d->group, "", interface_type);
1184 		config_item_set_name(&d->group.cg_item, "interface.%s",
1185 				     names[n_interf]);
1186 		configfs_add_default_group(&d->group, os_desc_group);
1187 	}
1188 
1189 	return os_desc_group;
1190 }
1191 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1192 
1193 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1194 {
1195 	WARN_ON(1);
1196 	return -EINVAL;
1197 }
1198 
1199 int composite_dev_prepare(struct usb_composite_driver *composite,
1200 		struct usb_composite_dev *dev);
1201 
1202 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1203 				  struct usb_ep *ep0);
1204 
1205 static void purge_configs_funcs(struct gadget_info *gi)
1206 {
1207 	struct usb_configuration	*c;
1208 
1209 	list_for_each_entry(c, &gi->cdev.configs, list) {
1210 		struct usb_function *f, *tmp;
1211 		struct config_usb_cfg *cfg;
1212 
1213 		cfg = container_of(c, struct config_usb_cfg, c);
1214 
1215 		list_for_each_entry_safe(f, tmp, &c->functions, list) {
1216 
1217 			list_move_tail(&f->list, &cfg->func_list);
1218 			if (f->unbind) {
1219 				dev_dbg(&gi->cdev.gadget->dev,
1220 					"unbind function '%s'/%p\n",
1221 					f->name, f);
1222 				f->unbind(c, f);
1223 			}
1224 		}
1225 		c->next_interface_id = 0;
1226 		memset(c->interface, 0, sizeof(c->interface));
1227 		c->superspeed_plus = 0;
1228 		c->superspeed = 0;
1229 		c->highspeed = 0;
1230 		c->fullspeed = 0;
1231 	}
1232 }
1233 
1234 static int configfs_composite_bind(struct usb_gadget *gadget,
1235 		struct usb_gadget_driver *gdriver)
1236 {
1237 	struct usb_composite_driver     *composite = to_cdriver(gdriver);
1238 	struct gadget_info		*gi = container_of(composite,
1239 						struct gadget_info, composite);
1240 	struct usb_composite_dev	*cdev = &gi->cdev;
1241 	struct usb_configuration	*c;
1242 	struct usb_string		*s;
1243 	unsigned			i;
1244 	int				ret;
1245 
1246 	/* the gi->lock is hold by the caller */
1247 	cdev->gadget = gadget;
1248 	set_gadget_data(gadget, cdev);
1249 	ret = composite_dev_prepare(composite, cdev);
1250 	if (ret)
1251 		return ret;
1252 	/* and now the gadget bind */
1253 	ret = -EINVAL;
1254 
1255 	if (list_empty(&gi->cdev.configs)) {
1256 		pr_err("Need at least one configuration in %s.\n",
1257 				gi->composite.name);
1258 		goto err_comp_cleanup;
1259 	}
1260 
1261 
1262 	list_for_each_entry(c, &gi->cdev.configs, list) {
1263 		struct config_usb_cfg *cfg;
1264 
1265 		cfg = container_of(c, struct config_usb_cfg, c);
1266 		if (list_empty(&cfg->func_list)) {
1267 			pr_err("Config %s/%d of %s needs at least one function.\n",
1268 			      c->label, c->bConfigurationValue,
1269 			      gi->composite.name);
1270 			goto err_comp_cleanup;
1271 		}
1272 	}
1273 
1274 	/* init all strings */
1275 	if (!list_empty(&gi->string_list)) {
1276 		struct gadget_strings *gs;
1277 
1278 		i = 0;
1279 		list_for_each_entry(gs, &gi->string_list, list) {
1280 
1281 			gi->gstrings[i] = &gs->stringtab_dev;
1282 			gs->stringtab_dev.strings = gs->strings;
1283 			gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1284 				gs->manufacturer;
1285 			gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1286 			gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1287 			i++;
1288 		}
1289 		gi->gstrings[i] = NULL;
1290 		s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1291 				USB_GADGET_FIRST_AVAIL_IDX);
1292 		if (IS_ERR(s)) {
1293 			ret = PTR_ERR(s);
1294 			goto err_comp_cleanup;
1295 		}
1296 
1297 		gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1298 		gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1299 		gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1300 	}
1301 
1302 	if (gi->use_os_desc) {
1303 		cdev->use_os_string = true;
1304 		cdev->b_vendor_code = gi->b_vendor_code;
1305 		memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1306 	}
1307 
1308 	if (gadget_is_otg(gadget) && !otg_desc[0]) {
1309 		struct usb_descriptor_header *usb_desc;
1310 
1311 		usb_desc = usb_otg_descriptor_alloc(gadget);
1312 		if (!usb_desc) {
1313 			ret = -ENOMEM;
1314 			goto err_comp_cleanup;
1315 		}
1316 		usb_otg_descriptor_init(gadget, usb_desc);
1317 		otg_desc[0] = usb_desc;
1318 		otg_desc[1] = NULL;
1319 	}
1320 
1321 	/* Go through all configs, attach all functions */
1322 	list_for_each_entry(c, &gi->cdev.configs, list) {
1323 		struct config_usb_cfg *cfg;
1324 		struct usb_function *f;
1325 		struct usb_function *tmp;
1326 		struct gadget_config_name *cn;
1327 
1328 		if (gadget_is_otg(gadget))
1329 			c->descriptors = otg_desc;
1330 
1331 		cfg = container_of(c, struct config_usb_cfg, c);
1332 		if (!list_empty(&cfg->string_list)) {
1333 			i = 0;
1334 			list_for_each_entry(cn, &cfg->string_list, list) {
1335 				cfg->gstrings[i] = &cn->stringtab_dev;
1336 				cn->stringtab_dev.strings = &cn->strings;
1337 				cn->strings.s = cn->configuration;
1338 				i++;
1339 			}
1340 			cfg->gstrings[i] = NULL;
1341 			s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1342 			if (IS_ERR(s)) {
1343 				ret = PTR_ERR(s);
1344 				goto err_comp_cleanup;
1345 			}
1346 			c->iConfiguration = s[0].id;
1347 		}
1348 
1349 		list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1350 			list_del(&f->list);
1351 			ret = usb_add_function(c, f);
1352 			if (ret) {
1353 				list_add(&f->list, &cfg->func_list);
1354 				goto err_purge_funcs;
1355 			}
1356 		}
1357 		usb_ep_autoconfig_reset(cdev->gadget);
1358 	}
1359 	if (cdev->use_os_string) {
1360 		ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1361 		if (ret)
1362 			goto err_purge_funcs;
1363 	}
1364 
1365 	usb_ep_autoconfig_reset(cdev->gadget);
1366 	return 0;
1367 
1368 err_purge_funcs:
1369 	purge_configs_funcs(gi);
1370 err_comp_cleanup:
1371 	composite_dev_cleanup(cdev);
1372 	return ret;
1373 }
1374 
1375 static void configfs_composite_unbind(struct usb_gadget *gadget)
1376 {
1377 	struct usb_composite_dev	*cdev;
1378 	struct gadget_info		*gi;
1379 
1380 	/* the gi->lock is hold by the caller */
1381 
1382 	cdev = get_gadget_data(gadget);
1383 	gi = container_of(cdev, struct gadget_info, cdev);
1384 
1385 	kfree(otg_desc[0]);
1386 	otg_desc[0] = NULL;
1387 	purge_configs_funcs(gi);
1388 	composite_dev_cleanup(cdev);
1389 	usb_ep_autoconfig_reset(cdev->gadget);
1390 	cdev->gadget = NULL;
1391 	set_gadget_data(gadget, NULL);
1392 }
1393 
1394 static const struct usb_gadget_driver configfs_driver_template = {
1395 	.bind           = configfs_composite_bind,
1396 	.unbind         = configfs_composite_unbind,
1397 
1398 	.setup          = composite_setup,
1399 	.reset          = composite_disconnect,
1400 	.disconnect     = composite_disconnect,
1401 
1402 	.suspend	= composite_suspend,
1403 	.resume		= composite_resume,
1404 
1405 	.max_speed	= USB_SPEED_SUPER,
1406 	.driver = {
1407 		.owner          = THIS_MODULE,
1408 		.name		= "configfs-gadget",
1409 	},
1410 	.match_existing_only = 1,
1411 };
1412 
1413 static struct config_group *gadgets_make(
1414 		struct config_group *group,
1415 		const char *name)
1416 {
1417 	struct gadget_info *gi;
1418 
1419 	gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1420 	if (!gi)
1421 		return ERR_PTR(-ENOMEM);
1422 
1423 	config_group_init_type_name(&gi->group, name, &gadget_root_type);
1424 
1425 	config_group_init_type_name(&gi->functions_group, "functions",
1426 			&functions_type);
1427 	configfs_add_default_group(&gi->functions_group, &gi->group);
1428 
1429 	config_group_init_type_name(&gi->configs_group, "configs",
1430 			&config_desc_type);
1431 	configfs_add_default_group(&gi->configs_group, &gi->group);
1432 
1433 	config_group_init_type_name(&gi->strings_group, "strings",
1434 			&gadget_strings_strings_type);
1435 	configfs_add_default_group(&gi->strings_group, &gi->group);
1436 
1437 	config_group_init_type_name(&gi->os_desc_group, "os_desc",
1438 			&os_desc_type);
1439 	configfs_add_default_group(&gi->os_desc_group, &gi->group);
1440 
1441 	gi->composite.bind = configfs_do_nothing;
1442 	gi->composite.unbind = configfs_do_nothing;
1443 	gi->composite.suspend = NULL;
1444 	gi->composite.resume = NULL;
1445 	gi->composite.max_speed = USB_SPEED_SUPER;
1446 
1447 	mutex_init(&gi->lock);
1448 	INIT_LIST_HEAD(&gi->string_list);
1449 	INIT_LIST_HEAD(&gi->available_func);
1450 
1451 	composite_init_dev(&gi->cdev);
1452 	gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1453 	gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1454 	gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1455 
1456 	gi->composite.gadget_driver = configfs_driver_template;
1457 
1458 	gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1459 	gi->composite.name = gi->composite.gadget_driver.function;
1460 
1461 	if (!gi->composite.gadget_driver.function)
1462 		goto err;
1463 
1464 	return &gi->group;
1465 err:
1466 	kfree(gi);
1467 	return ERR_PTR(-ENOMEM);
1468 }
1469 
1470 static void gadgets_drop(struct config_group *group, struct config_item *item)
1471 {
1472 	config_item_put(item);
1473 }
1474 
1475 static struct configfs_group_operations gadgets_ops = {
1476 	.make_group     = &gadgets_make,
1477 	.drop_item      = &gadgets_drop,
1478 };
1479 
1480 static const struct config_item_type gadgets_type = {
1481 	.ct_group_ops   = &gadgets_ops,
1482 	.ct_owner       = THIS_MODULE,
1483 };
1484 
1485 static struct configfs_subsystem gadget_subsys = {
1486 	.su_group = {
1487 		.cg_item = {
1488 			.ci_namebuf = "usb_gadget",
1489 			.ci_type = &gadgets_type,
1490 		},
1491 	},
1492 	.su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1493 };
1494 
1495 void unregister_gadget_item(struct config_item *item)
1496 {
1497 	struct gadget_info *gi = to_gadget_info(item);
1498 
1499 	mutex_lock(&gi->lock);
1500 	unregister_gadget(gi);
1501 	mutex_unlock(&gi->lock);
1502 }
1503 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1504 
1505 static int __init gadget_cfs_init(void)
1506 {
1507 	int ret;
1508 
1509 	config_group_init(&gadget_subsys.su_group);
1510 
1511 	ret = configfs_register_subsystem(&gadget_subsys);
1512 	return ret;
1513 }
1514 module_init(gadget_cfs_init);
1515 
1516 static void __exit gadget_cfs_exit(void)
1517 {
1518 	configfs_unregister_subsystem(&gadget_subsys);
1519 }
1520 module_exit(gadget_cfs_exit);
1521