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