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