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