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