xref: /openbmc/linux/drivers/usb/gadget/configfs.c (revision 09717af7)
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 	struct config_usb_cfg *cfg = to_config_usb_cfg(item);
512 
513 	return sprintf(page, "%u\n", cfg->c.MaxPower);
514 }
515 
516 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
517 		const char *page, size_t len)
518 {
519 	struct config_usb_cfg *cfg = to_config_usb_cfg(item);
520 	u16 val;
521 	int ret;
522 	ret = kstrtou16(page, 0, &val);
523 	if (ret)
524 		return ret;
525 	if (DIV_ROUND_UP(val, 8) > 0xff)
526 		return -ERANGE;
527 	cfg->c.MaxPower = val;
528 	return len;
529 }
530 
531 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
532 		char *page)
533 {
534 	struct config_usb_cfg *cfg = to_config_usb_cfg(item);
535 
536 	return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
537 }
538 
539 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
540 		const char *page, size_t len)
541 {
542 	struct config_usb_cfg *cfg = to_config_usb_cfg(item);
543 	u8 val;
544 	int ret;
545 	ret = kstrtou8(page, 0, &val);
546 	if (ret)
547 		return ret;
548 	if (!(val & USB_CONFIG_ATT_ONE))
549 		return -EINVAL;
550 	if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
551 				USB_CONFIG_ATT_WAKEUP))
552 		return -EINVAL;
553 	cfg->c.bmAttributes = val;
554 	return len;
555 }
556 
557 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
558 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
559 
560 static struct configfs_attribute *gadget_config_attrs[] = {
561 	&gadget_config_desc_attr_MaxPower,
562 	&gadget_config_desc_attr_bmAttributes,
563 	NULL,
564 };
565 
566 static const struct config_item_type gadget_config_type = {
567 	.ct_item_ops	= &gadget_config_item_ops,
568 	.ct_attrs	= gadget_config_attrs,
569 	.ct_owner	= THIS_MODULE,
570 };
571 
572 static const struct config_item_type gadget_root_type = {
573 	.ct_item_ops	= &gadget_root_item_ops,
574 	.ct_attrs	= gadget_root_attrs,
575 	.ct_owner	= THIS_MODULE,
576 };
577 
578 static void composite_init_dev(struct usb_composite_dev *cdev)
579 {
580 	spin_lock_init(&cdev->lock);
581 	INIT_LIST_HEAD(&cdev->configs);
582 	INIT_LIST_HEAD(&cdev->gstrings);
583 }
584 
585 static struct config_group *function_make(
586 		struct config_group *group,
587 		const char *name)
588 {
589 	struct gadget_info *gi;
590 	struct usb_function_instance *fi;
591 	char buf[MAX_NAME_LEN];
592 	char *func_name;
593 	char *instance_name;
594 	int ret;
595 
596 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
597 	if (ret >= MAX_NAME_LEN)
598 		return ERR_PTR(-ENAMETOOLONG);
599 
600 	func_name = buf;
601 	instance_name = strchr(func_name, '.');
602 	if (!instance_name) {
603 		pr_err("Unable to locate . in FUNC.INSTANCE\n");
604 		return ERR_PTR(-EINVAL);
605 	}
606 	*instance_name = '\0';
607 	instance_name++;
608 
609 	fi = usb_get_function_instance(func_name);
610 	if (IS_ERR(fi))
611 		return ERR_CAST(fi);
612 
613 	ret = config_item_set_name(&fi->group.cg_item, "%s", name);
614 	if (ret) {
615 		usb_put_function_instance(fi);
616 		return ERR_PTR(ret);
617 	}
618 	if (fi->set_inst_name) {
619 		ret = fi->set_inst_name(fi, instance_name);
620 		if (ret) {
621 			usb_put_function_instance(fi);
622 			return ERR_PTR(ret);
623 		}
624 	}
625 
626 	gi = container_of(group, struct gadget_info, functions_group);
627 
628 	mutex_lock(&gi->lock);
629 	list_add_tail(&fi->cfs_list, &gi->available_func);
630 	mutex_unlock(&gi->lock);
631 	return &fi->group;
632 }
633 
634 static void function_drop(
635 		struct config_group *group,
636 		struct config_item *item)
637 {
638 	struct usb_function_instance *fi = to_usb_function_instance(item);
639 	struct gadget_info *gi;
640 
641 	gi = container_of(group, struct gadget_info, functions_group);
642 
643 	mutex_lock(&gi->lock);
644 	list_del(&fi->cfs_list);
645 	mutex_unlock(&gi->lock);
646 	config_item_put(item);
647 }
648 
649 static struct configfs_group_operations functions_ops = {
650 	.make_group     = &function_make,
651 	.drop_item      = &function_drop,
652 };
653 
654 static const struct config_item_type functions_type = {
655 	.ct_group_ops   = &functions_ops,
656 	.ct_owner       = THIS_MODULE,
657 };
658 
659 GS_STRINGS_RW(gadget_config_name, configuration);
660 
661 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
662 	&gadget_config_name_attr_configuration,
663 	NULL,
664 };
665 
666 static void gadget_config_name_attr_release(struct config_item *item)
667 {
668 	struct gadget_config_name *cn = to_gadget_config_name(item);
669 
670 	kfree(cn->configuration);
671 
672 	list_del(&cn->list);
673 	kfree(cn);
674 }
675 
676 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
677 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
678 
679 static struct config_group *config_desc_make(
680 		struct config_group *group,
681 		const char *name)
682 {
683 	struct gadget_info *gi;
684 	struct config_usb_cfg *cfg;
685 	char buf[MAX_NAME_LEN];
686 	char *num_str;
687 	u8 num;
688 	int ret;
689 
690 	gi = container_of(group, struct gadget_info, configs_group);
691 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
692 	if (ret >= MAX_NAME_LEN)
693 		return ERR_PTR(-ENAMETOOLONG);
694 
695 	num_str = strchr(buf, '.');
696 	if (!num_str) {
697 		pr_err("Unable to locate . in name.bConfigurationValue\n");
698 		return ERR_PTR(-EINVAL);
699 	}
700 
701 	*num_str = '\0';
702 	num_str++;
703 
704 	if (!strlen(buf))
705 		return ERR_PTR(-EINVAL);
706 
707 	ret = kstrtou8(num_str, 0, &num);
708 	if (ret)
709 		return ERR_PTR(ret);
710 
711 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
712 	if (!cfg)
713 		return ERR_PTR(-ENOMEM);
714 	cfg->c.label = kstrdup(buf, GFP_KERNEL);
715 	if (!cfg->c.label) {
716 		ret = -ENOMEM;
717 		goto err;
718 	}
719 	cfg->c.bConfigurationValue = num;
720 	cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
721 	cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
722 	INIT_LIST_HEAD(&cfg->string_list);
723 	INIT_LIST_HEAD(&cfg->func_list);
724 
725 	config_group_init_type_name(&cfg->group, name,
726 				&gadget_config_type);
727 
728 	config_group_init_type_name(&cfg->strings_group, "strings",
729 			&gadget_config_name_strings_type);
730 	configfs_add_default_group(&cfg->strings_group, &cfg->group);
731 
732 	ret = usb_add_config_only(&gi->cdev, &cfg->c);
733 	if (ret)
734 		goto err;
735 
736 	return &cfg->group;
737 err:
738 	kfree(cfg->c.label);
739 	kfree(cfg);
740 	return ERR_PTR(ret);
741 }
742 
743 static void config_desc_drop(
744 		struct config_group *group,
745 		struct config_item *item)
746 {
747 	config_item_put(item);
748 }
749 
750 static struct configfs_group_operations config_desc_ops = {
751 	.make_group     = &config_desc_make,
752 	.drop_item      = &config_desc_drop,
753 };
754 
755 static const struct config_item_type config_desc_type = {
756 	.ct_group_ops   = &config_desc_ops,
757 	.ct_owner       = THIS_MODULE,
758 };
759 
760 GS_STRINGS_RW(gadget_strings, manufacturer);
761 GS_STRINGS_RW(gadget_strings, product);
762 GS_STRINGS_RW(gadget_strings, serialnumber);
763 
764 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
765 	&gadget_strings_attr_manufacturer,
766 	&gadget_strings_attr_product,
767 	&gadget_strings_attr_serialnumber,
768 	NULL,
769 };
770 
771 static void gadget_strings_attr_release(struct config_item *item)
772 {
773 	struct gadget_strings *gs = to_gadget_strings(item);
774 
775 	kfree(gs->manufacturer);
776 	kfree(gs->product);
777 	kfree(gs->serialnumber);
778 
779 	list_del(&gs->list);
780 	kfree(gs);
781 }
782 
783 USB_CONFIG_STRING_RW_OPS(gadget_strings);
784 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
785 
786 static inline struct os_desc *to_os_desc(struct config_item *item)
787 {
788 	return container_of(to_config_group(item), struct os_desc, group);
789 }
790 
791 static inline struct gadget_info *os_desc_item_to_gadget_info(
792 		struct config_item *item)
793 {
794 	return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
795 }
796 
797 static ssize_t os_desc_use_show(struct config_item *item, char *page)
798 {
799 	return sprintf(page, "%d\n",
800 			os_desc_item_to_gadget_info(item)->use_os_desc);
801 }
802 
803 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
804 				 size_t len)
805 {
806 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
807 	int ret;
808 	bool use;
809 
810 	mutex_lock(&gi->lock);
811 	ret = strtobool(page, &use);
812 	if (!ret) {
813 		gi->use_os_desc = use;
814 		ret = len;
815 	}
816 	mutex_unlock(&gi->lock);
817 
818 	return ret;
819 }
820 
821 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
822 {
823 	return sprintf(page, "0x%02x\n",
824 			os_desc_item_to_gadget_info(item)->b_vendor_code);
825 }
826 
827 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
828 					   const char *page, size_t len)
829 {
830 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
831 	int ret;
832 	u8 b_vendor_code;
833 
834 	mutex_lock(&gi->lock);
835 	ret = kstrtou8(page, 0, &b_vendor_code);
836 	if (!ret) {
837 		gi->b_vendor_code = b_vendor_code;
838 		ret = len;
839 	}
840 	mutex_unlock(&gi->lock);
841 
842 	return ret;
843 }
844 
845 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
846 {
847 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
848 	int res;
849 
850 	res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
851 			      UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
852 	page[res++] = '\n';
853 
854 	return res;
855 }
856 
857 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
858 				     size_t len)
859 {
860 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
861 	int res, l;
862 
863 	l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
864 	if (page[l - 1] == '\n')
865 		--l;
866 
867 	mutex_lock(&gi->lock);
868 	res = utf8s_to_utf16s(page, l,
869 			      UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
870 			      OS_STRING_QW_SIGN_LEN);
871 	if (res > 0)
872 		res = len;
873 	mutex_unlock(&gi->lock);
874 
875 	return res;
876 }
877 
878 CONFIGFS_ATTR(os_desc_, use);
879 CONFIGFS_ATTR(os_desc_, b_vendor_code);
880 CONFIGFS_ATTR(os_desc_, qw_sign);
881 
882 static struct configfs_attribute *os_desc_attrs[] = {
883 	&os_desc_attr_use,
884 	&os_desc_attr_b_vendor_code,
885 	&os_desc_attr_qw_sign,
886 	NULL,
887 };
888 
889 static void os_desc_attr_release(struct config_item *item)
890 {
891 	struct os_desc *os_desc = to_os_desc(item);
892 	kfree(os_desc);
893 }
894 
895 static int os_desc_link(struct config_item *os_desc_ci,
896 			struct config_item *usb_cfg_ci)
897 {
898 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
899 					struct gadget_info, os_desc_group);
900 	struct usb_composite_dev *cdev = &gi->cdev;
901 	struct config_usb_cfg *c_target =
902 		container_of(to_config_group(usb_cfg_ci),
903 			     struct config_usb_cfg, group);
904 	struct usb_configuration *c;
905 	int ret;
906 
907 	mutex_lock(&gi->lock);
908 	list_for_each_entry(c, &cdev->configs, list) {
909 		if (c == &c_target->c)
910 			break;
911 	}
912 	if (c != &c_target->c) {
913 		ret = -EINVAL;
914 		goto out;
915 	}
916 
917 	if (cdev->os_desc_config) {
918 		ret = -EBUSY;
919 		goto out;
920 	}
921 
922 	cdev->os_desc_config = &c_target->c;
923 	ret = 0;
924 
925 out:
926 	mutex_unlock(&gi->lock);
927 	return ret;
928 }
929 
930 static void os_desc_unlink(struct config_item *os_desc_ci,
931 			  struct config_item *usb_cfg_ci)
932 {
933 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
934 					struct gadget_info, os_desc_group);
935 	struct usb_composite_dev *cdev = &gi->cdev;
936 
937 	mutex_lock(&gi->lock);
938 	if (gi->composite.gadget_driver.udc_name)
939 		unregister_gadget(gi);
940 	cdev->os_desc_config = NULL;
941 	WARN_ON(gi->composite.gadget_driver.udc_name);
942 	mutex_unlock(&gi->lock);
943 }
944 
945 static struct configfs_item_operations os_desc_ops = {
946 	.release                = os_desc_attr_release,
947 	.allow_link		= os_desc_link,
948 	.drop_link		= os_desc_unlink,
949 };
950 
951 static struct config_item_type os_desc_type = {
952 	.ct_item_ops	= &os_desc_ops,
953 	.ct_attrs	= os_desc_attrs,
954 	.ct_owner	= THIS_MODULE,
955 };
956 
957 static inline struct usb_os_desc_ext_prop
958 *to_usb_os_desc_ext_prop(struct config_item *item)
959 {
960 	return container_of(item, struct usb_os_desc_ext_prop, item);
961 }
962 
963 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
964 {
965 	return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
966 }
967 
968 static ssize_t ext_prop_type_store(struct config_item *item,
969 				   const char *page, size_t len)
970 {
971 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
972 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
973 	u8 type;
974 	int ret;
975 
976 	if (desc->opts_mutex)
977 		mutex_lock(desc->opts_mutex);
978 	ret = kstrtou8(page, 0, &type);
979 	if (ret)
980 		goto end;
981 	if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
982 		ret = -EINVAL;
983 		goto end;
984 	}
985 
986 	if ((ext_prop->type == USB_EXT_PROP_BINARY ||
987 	    ext_prop->type == USB_EXT_PROP_LE32 ||
988 	    ext_prop->type == USB_EXT_PROP_BE32) &&
989 	    (type == USB_EXT_PROP_UNICODE ||
990 	    type == USB_EXT_PROP_UNICODE_ENV ||
991 	    type == USB_EXT_PROP_UNICODE_LINK))
992 		ext_prop->data_len <<= 1;
993 	else 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 		   (type == USB_EXT_PROP_BINARY ||
997 		   type == USB_EXT_PROP_LE32 ||
998 		   type == USB_EXT_PROP_BE32))
999 		ext_prop->data_len >>= 1;
1000 	ext_prop->type = type;
1001 	ret = len;
1002 
1003 end:
1004 	if (desc->opts_mutex)
1005 		mutex_unlock(desc->opts_mutex);
1006 	return ret;
1007 }
1008 
1009 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
1010 {
1011 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1012 	int len = ext_prop->data_len;
1013 
1014 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1015 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1016 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1017 		len >>= 1;
1018 	memcpy(page, ext_prop->data, len);
1019 
1020 	return len;
1021 }
1022 
1023 static ssize_t ext_prop_data_store(struct config_item *item,
1024 				   const char *page, size_t len)
1025 {
1026 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1027 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1028 	char *new_data;
1029 	size_t ret_len = len;
1030 
1031 	if (page[len - 1] == '\n' || page[len - 1] == '\0')
1032 		--len;
1033 	new_data = kmemdup(page, len, GFP_KERNEL);
1034 	if (!new_data)
1035 		return -ENOMEM;
1036 
1037 	if (desc->opts_mutex)
1038 		mutex_lock(desc->opts_mutex);
1039 	kfree(ext_prop->data);
1040 	ext_prop->data = new_data;
1041 	desc->ext_prop_len -= ext_prop->data_len;
1042 	ext_prop->data_len = len;
1043 	desc->ext_prop_len += ext_prop->data_len;
1044 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1045 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1046 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1047 		desc->ext_prop_len -= ext_prop->data_len;
1048 		ext_prop->data_len <<= 1;
1049 		ext_prop->data_len += 2;
1050 		desc->ext_prop_len += ext_prop->data_len;
1051 	}
1052 	if (desc->opts_mutex)
1053 		mutex_unlock(desc->opts_mutex);
1054 	return ret_len;
1055 }
1056 
1057 CONFIGFS_ATTR(ext_prop_, type);
1058 CONFIGFS_ATTR(ext_prop_, data);
1059 
1060 static struct configfs_attribute *ext_prop_attrs[] = {
1061 	&ext_prop_attr_type,
1062 	&ext_prop_attr_data,
1063 	NULL,
1064 };
1065 
1066 static void usb_os_desc_ext_prop_release(struct config_item *item)
1067 {
1068 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1069 
1070 	kfree(ext_prop); /* frees a whole chunk */
1071 }
1072 
1073 static struct configfs_item_operations ext_prop_ops = {
1074 	.release		= usb_os_desc_ext_prop_release,
1075 };
1076 
1077 static struct config_item *ext_prop_make(
1078 		struct config_group *group,
1079 		const char *name)
1080 {
1081 	struct usb_os_desc_ext_prop *ext_prop;
1082 	struct config_item_type *ext_prop_type;
1083 	struct usb_os_desc *desc;
1084 	char *vlabuf;
1085 
1086 	vla_group(data_chunk);
1087 	vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1088 	vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1089 
1090 	vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1091 	if (!vlabuf)
1092 		return ERR_PTR(-ENOMEM);
1093 
1094 	ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1095 	ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1096 
1097 	desc = container_of(group, struct usb_os_desc, group);
1098 	ext_prop_type->ct_item_ops = &ext_prop_ops;
1099 	ext_prop_type->ct_attrs = ext_prop_attrs;
1100 	ext_prop_type->ct_owner = desc->owner;
1101 
1102 	config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1103 
1104 	ext_prop->name = kstrdup(name, GFP_KERNEL);
1105 	if (!ext_prop->name) {
1106 		kfree(vlabuf);
1107 		return ERR_PTR(-ENOMEM);
1108 	}
1109 	desc->ext_prop_len += 14;
1110 	ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1111 	if (desc->opts_mutex)
1112 		mutex_lock(desc->opts_mutex);
1113 	desc->ext_prop_len += ext_prop->name_len;
1114 	list_add_tail(&ext_prop->entry, &desc->ext_prop);
1115 	++desc->ext_prop_count;
1116 	if (desc->opts_mutex)
1117 		mutex_unlock(desc->opts_mutex);
1118 
1119 	return &ext_prop->item;
1120 }
1121 
1122 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1123 {
1124 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1125 	struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1126 
1127 	if (desc->opts_mutex)
1128 		mutex_lock(desc->opts_mutex);
1129 	list_del(&ext_prop->entry);
1130 	--desc->ext_prop_count;
1131 	kfree(ext_prop->name);
1132 	desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1133 	if (desc->opts_mutex)
1134 		mutex_unlock(desc->opts_mutex);
1135 	config_item_put(item);
1136 }
1137 
1138 static struct configfs_group_operations interf_grp_ops = {
1139 	.make_item	= &ext_prop_make,
1140 	.drop_item	= &ext_prop_drop,
1141 };
1142 
1143 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1144 					     char *page)
1145 {
1146 	memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1147 	return 8;
1148 }
1149 
1150 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1151 					      const char *page, size_t len)
1152 {
1153 	struct usb_os_desc *desc = to_usb_os_desc(item);
1154 	int l;
1155 
1156 	l = min_t(int, 8, len);
1157 	if (page[l - 1] == '\n')
1158 		--l;
1159 	if (desc->opts_mutex)
1160 		mutex_lock(desc->opts_mutex);
1161 	memcpy(desc->ext_compat_id, page, l);
1162 
1163 	if (desc->opts_mutex)
1164 		mutex_unlock(desc->opts_mutex);
1165 
1166 	return len;
1167 }
1168 
1169 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1170 						 char *page)
1171 {
1172 	memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1173 	return 8;
1174 }
1175 
1176 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1177 						  const char *page, size_t len)
1178 {
1179 	struct usb_os_desc *desc = to_usb_os_desc(item);
1180 	int l;
1181 
1182 	l = min_t(int, 8, len);
1183 	if (page[l - 1] == '\n')
1184 		--l;
1185 	if (desc->opts_mutex)
1186 		mutex_lock(desc->opts_mutex);
1187 	memcpy(desc->ext_compat_id + 8, page, l);
1188 
1189 	if (desc->opts_mutex)
1190 		mutex_unlock(desc->opts_mutex);
1191 
1192 	return len;
1193 }
1194 
1195 CONFIGFS_ATTR(interf_grp_, compatible_id);
1196 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1197 
1198 static struct configfs_attribute *interf_grp_attrs[] = {
1199 	&interf_grp_attr_compatible_id,
1200 	&interf_grp_attr_sub_compatible_id,
1201 	NULL
1202 };
1203 
1204 struct config_group *usb_os_desc_prepare_interf_dir(
1205 		struct config_group *parent,
1206 		int n_interf,
1207 		struct usb_os_desc **desc,
1208 		char **names,
1209 		struct module *owner)
1210 {
1211 	struct config_group *os_desc_group;
1212 	struct config_item_type *os_desc_type, *interface_type;
1213 
1214 	vla_group(data_chunk);
1215 	vla_item(data_chunk, struct config_group, os_desc_group, 1);
1216 	vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1217 	vla_item(data_chunk, struct config_item_type, interface_type, 1);
1218 
1219 	char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1220 	if (!vlabuf)
1221 		return ERR_PTR(-ENOMEM);
1222 
1223 	os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1224 	os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1225 	interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1226 
1227 	os_desc_type->ct_owner = owner;
1228 	config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1229 	configfs_add_default_group(os_desc_group, parent);
1230 
1231 	interface_type->ct_group_ops = &interf_grp_ops;
1232 	interface_type->ct_attrs = interf_grp_attrs;
1233 	interface_type->ct_owner = owner;
1234 
1235 	while (n_interf--) {
1236 		struct usb_os_desc *d;
1237 
1238 		d = desc[n_interf];
1239 		d->owner = owner;
1240 		config_group_init_type_name(&d->group, "", interface_type);
1241 		config_item_set_name(&d->group.cg_item, "interface.%s",
1242 				     names[n_interf]);
1243 		configfs_add_default_group(&d->group, os_desc_group);
1244 	}
1245 
1246 	return os_desc_group;
1247 }
1248 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1249 
1250 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1251 {
1252 	WARN_ON(1);
1253 	return -EINVAL;
1254 }
1255 
1256 int composite_dev_prepare(struct usb_composite_driver *composite,
1257 		struct usb_composite_dev *dev);
1258 
1259 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1260 				  struct usb_ep *ep0);
1261 
1262 static void purge_configs_funcs(struct gadget_info *gi)
1263 {
1264 	struct usb_configuration	*c;
1265 
1266 	list_for_each_entry(c, &gi->cdev.configs, list) {
1267 		struct usb_function *f, *tmp;
1268 		struct config_usb_cfg *cfg;
1269 
1270 		cfg = container_of(c, struct config_usb_cfg, c);
1271 
1272 		list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1273 
1274 			list_move(&f->list, &cfg->func_list);
1275 			if (f->unbind) {
1276 				dev_dbg(&gi->cdev.gadget->dev,
1277 					"unbind function '%s'/%p\n",
1278 					f->name, f);
1279 				f->unbind(c, f);
1280 			}
1281 		}
1282 		c->next_interface_id = 0;
1283 		memset(c->interface, 0, sizeof(c->interface));
1284 		c->superspeed_plus = 0;
1285 		c->superspeed = 0;
1286 		c->highspeed = 0;
1287 		c->fullspeed = 0;
1288 	}
1289 }
1290 
1291 static int configfs_composite_bind(struct usb_gadget *gadget,
1292 		struct usb_gadget_driver *gdriver)
1293 {
1294 	struct usb_composite_driver     *composite = to_cdriver(gdriver);
1295 	struct gadget_info		*gi = container_of(composite,
1296 						struct gadget_info, composite);
1297 	struct usb_composite_dev	*cdev = &gi->cdev;
1298 	struct usb_configuration	*c;
1299 	struct usb_string		*s;
1300 	unsigned			i;
1301 	int				ret;
1302 
1303 	/* the gi->lock is hold by the caller */
1304 	gi->unbind = 0;
1305 	cdev->gadget = gadget;
1306 	set_gadget_data(gadget, cdev);
1307 	ret = composite_dev_prepare(composite, cdev);
1308 	if (ret)
1309 		return ret;
1310 	/* and now the gadget bind */
1311 	ret = -EINVAL;
1312 
1313 	if (list_empty(&gi->cdev.configs)) {
1314 		pr_err("Need at least one configuration in %s.\n",
1315 				gi->composite.name);
1316 		goto err_comp_cleanup;
1317 	}
1318 
1319 
1320 	list_for_each_entry(c, &gi->cdev.configs, list) {
1321 		struct config_usb_cfg *cfg;
1322 
1323 		cfg = container_of(c, struct config_usb_cfg, c);
1324 		if (list_empty(&cfg->func_list)) {
1325 			pr_err("Config %s/%d of %s needs at least one function.\n",
1326 			      c->label, c->bConfigurationValue,
1327 			      gi->composite.name);
1328 			goto err_comp_cleanup;
1329 		}
1330 	}
1331 
1332 	/* init all strings */
1333 	if (!list_empty(&gi->string_list)) {
1334 		struct gadget_strings *gs;
1335 
1336 		i = 0;
1337 		list_for_each_entry(gs, &gi->string_list, list) {
1338 
1339 			gi->gstrings[i] = &gs->stringtab_dev;
1340 			gs->stringtab_dev.strings = gs->strings;
1341 			gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1342 				gs->manufacturer;
1343 			gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1344 			gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1345 			i++;
1346 		}
1347 		gi->gstrings[i] = NULL;
1348 		s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1349 				USB_GADGET_FIRST_AVAIL_IDX);
1350 		if (IS_ERR(s)) {
1351 			ret = PTR_ERR(s);
1352 			goto err_comp_cleanup;
1353 		}
1354 
1355 		gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1356 		gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1357 		gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1358 	}
1359 
1360 	if (gi->use_os_desc) {
1361 		cdev->use_os_string = true;
1362 		cdev->b_vendor_code = gi->b_vendor_code;
1363 		memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1364 	}
1365 
1366 	if (gadget_is_otg(gadget) && !otg_desc[0]) {
1367 		struct usb_descriptor_header *usb_desc;
1368 
1369 		usb_desc = usb_otg_descriptor_alloc(gadget);
1370 		if (!usb_desc) {
1371 			ret = -ENOMEM;
1372 			goto err_comp_cleanup;
1373 		}
1374 		usb_otg_descriptor_init(gadget, usb_desc);
1375 		otg_desc[0] = usb_desc;
1376 		otg_desc[1] = NULL;
1377 	}
1378 
1379 	/* Go through all configs, attach all functions */
1380 	list_for_each_entry(c, &gi->cdev.configs, list) {
1381 		struct config_usb_cfg *cfg;
1382 		struct usb_function *f;
1383 		struct usb_function *tmp;
1384 		struct gadget_config_name *cn;
1385 
1386 		if (gadget_is_otg(gadget))
1387 			c->descriptors = otg_desc;
1388 
1389 		cfg = container_of(c, struct config_usb_cfg, c);
1390 		if (!list_empty(&cfg->string_list)) {
1391 			i = 0;
1392 			list_for_each_entry(cn, &cfg->string_list, list) {
1393 				cfg->gstrings[i] = &cn->stringtab_dev;
1394 				cn->stringtab_dev.strings = &cn->strings;
1395 				cn->strings.s = cn->configuration;
1396 				i++;
1397 			}
1398 			cfg->gstrings[i] = NULL;
1399 			s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1400 			if (IS_ERR(s)) {
1401 				ret = PTR_ERR(s);
1402 				goto err_comp_cleanup;
1403 			}
1404 			c->iConfiguration = s[0].id;
1405 		}
1406 
1407 		list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1408 			list_del(&f->list);
1409 			ret = usb_add_function(c, f);
1410 			if (ret) {
1411 				list_add(&f->list, &cfg->func_list);
1412 				goto err_purge_funcs;
1413 			}
1414 		}
1415 		ret = usb_gadget_check_config(cdev->gadget);
1416 		if (ret)
1417 			goto err_purge_funcs;
1418 
1419 		usb_ep_autoconfig_reset(cdev->gadget);
1420 	}
1421 	if (cdev->use_os_string) {
1422 		ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1423 		if (ret)
1424 			goto err_purge_funcs;
1425 	}
1426 
1427 	usb_ep_autoconfig_reset(cdev->gadget);
1428 	return 0;
1429 
1430 err_purge_funcs:
1431 	purge_configs_funcs(gi);
1432 err_comp_cleanup:
1433 	composite_dev_cleanup(cdev);
1434 	return ret;
1435 }
1436 
1437 static void configfs_composite_unbind(struct usb_gadget *gadget)
1438 {
1439 	struct usb_composite_dev	*cdev;
1440 	struct gadget_info		*gi;
1441 	unsigned long flags;
1442 
1443 	/* the gi->lock is hold by the caller */
1444 
1445 	cdev = get_gadget_data(gadget);
1446 	gi = container_of(cdev, struct gadget_info, cdev);
1447 	spin_lock_irqsave(&gi->spinlock, flags);
1448 	gi->unbind = 1;
1449 	spin_unlock_irqrestore(&gi->spinlock, flags);
1450 
1451 	kfree(otg_desc[0]);
1452 	otg_desc[0] = NULL;
1453 	purge_configs_funcs(gi);
1454 	composite_dev_cleanup(cdev);
1455 	usb_ep_autoconfig_reset(cdev->gadget);
1456 	spin_lock_irqsave(&gi->spinlock, flags);
1457 	cdev->gadget = NULL;
1458 	set_gadget_data(gadget, NULL);
1459 	spin_unlock_irqrestore(&gi->spinlock, flags);
1460 }
1461 
1462 static int configfs_composite_setup(struct usb_gadget *gadget,
1463 		const struct usb_ctrlrequest *ctrl)
1464 {
1465 	struct usb_composite_dev *cdev;
1466 	struct gadget_info *gi;
1467 	unsigned long flags;
1468 	int ret;
1469 
1470 	cdev = get_gadget_data(gadget);
1471 	if (!cdev)
1472 		return 0;
1473 
1474 	gi = container_of(cdev, struct gadget_info, cdev);
1475 	spin_lock_irqsave(&gi->spinlock, flags);
1476 	cdev = get_gadget_data(gadget);
1477 	if (!cdev || gi->unbind) {
1478 		spin_unlock_irqrestore(&gi->spinlock, flags);
1479 		return 0;
1480 	}
1481 
1482 	ret = composite_setup(gadget, ctrl);
1483 	spin_unlock_irqrestore(&gi->spinlock, flags);
1484 	return ret;
1485 }
1486 
1487 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1488 {
1489 	struct usb_composite_dev *cdev;
1490 	struct gadget_info *gi;
1491 	unsigned long flags;
1492 
1493 	cdev = get_gadget_data(gadget);
1494 	if (!cdev)
1495 		return;
1496 
1497 	gi = container_of(cdev, struct gadget_info, cdev);
1498 	spin_lock_irqsave(&gi->spinlock, flags);
1499 	cdev = get_gadget_data(gadget);
1500 	if (!cdev || gi->unbind) {
1501 		spin_unlock_irqrestore(&gi->spinlock, flags);
1502 		return;
1503 	}
1504 
1505 	composite_disconnect(gadget);
1506 	spin_unlock_irqrestore(&gi->spinlock, flags);
1507 }
1508 
1509 static void configfs_composite_reset(struct usb_gadget *gadget)
1510 {
1511 	struct usb_composite_dev *cdev;
1512 	struct gadget_info *gi;
1513 	unsigned long flags;
1514 
1515 	cdev = get_gadget_data(gadget);
1516 	if (!cdev)
1517 		return;
1518 
1519 	gi = container_of(cdev, struct gadget_info, cdev);
1520 	spin_lock_irqsave(&gi->spinlock, flags);
1521 	cdev = get_gadget_data(gadget);
1522 	if (!cdev || gi->unbind) {
1523 		spin_unlock_irqrestore(&gi->spinlock, flags);
1524 		return;
1525 	}
1526 
1527 	composite_reset(gadget);
1528 	spin_unlock_irqrestore(&gi->spinlock, flags);
1529 }
1530 
1531 static void configfs_composite_suspend(struct usb_gadget *gadget)
1532 {
1533 	struct usb_composite_dev *cdev;
1534 	struct gadget_info *gi;
1535 	unsigned long flags;
1536 
1537 	cdev = get_gadget_data(gadget);
1538 	if (!cdev)
1539 		return;
1540 
1541 	gi = container_of(cdev, struct gadget_info, cdev);
1542 	spin_lock_irqsave(&gi->spinlock, flags);
1543 	cdev = get_gadget_data(gadget);
1544 	if (!cdev || gi->unbind) {
1545 		spin_unlock_irqrestore(&gi->spinlock, flags);
1546 		return;
1547 	}
1548 
1549 	composite_suspend(gadget);
1550 	spin_unlock_irqrestore(&gi->spinlock, flags);
1551 }
1552 
1553 static void configfs_composite_resume(struct usb_gadget *gadget)
1554 {
1555 	struct usb_composite_dev *cdev;
1556 	struct gadget_info *gi;
1557 	unsigned long flags;
1558 
1559 	cdev = get_gadget_data(gadget);
1560 	if (!cdev)
1561 		return;
1562 
1563 	gi = container_of(cdev, struct gadget_info, cdev);
1564 	spin_lock_irqsave(&gi->spinlock, flags);
1565 	cdev = get_gadget_data(gadget);
1566 	if (!cdev || gi->unbind) {
1567 		spin_unlock_irqrestore(&gi->spinlock, flags);
1568 		return;
1569 	}
1570 
1571 	composite_resume(gadget);
1572 	spin_unlock_irqrestore(&gi->spinlock, flags);
1573 }
1574 
1575 static const struct usb_gadget_driver configfs_driver_template = {
1576 	.bind           = configfs_composite_bind,
1577 	.unbind         = configfs_composite_unbind,
1578 
1579 	.setup          = configfs_composite_setup,
1580 	.reset          = configfs_composite_reset,
1581 	.disconnect     = configfs_composite_disconnect,
1582 
1583 	.suspend	= configfs_composite_suspend,
1584 	.resume		= configfs_composite_resume,
1585 
1586 	.max_speed	= USB_SPEED_SUPER_PLUS,
1587 	.driver = {
1588 		.owner          = THIS_MODULE,
1589 		.name		= "configfs-gadget",
1590 	},
1591 	.match_existing_only = 1,
1592 };
1593 
1594 static struct config_group *gadgets_make(
1595 		struct config_group *group,
1596 		const char *name)
1597 {
1598 	struct gadget_info *gi;
1599 
1600 	gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1601 	if (!gi)
1602 		return ERR_PTR(-ENOMEM);
1603 
1604 	config_group_init_type_name(&gi->group, name, &gadget_root_type);
1605 
1606 	config_group_init_type_name(&gi->functions_group, "functions",
1607 			&functions_type);
1608 	configfs_add_default_group(&gi->functions_group, &gi->group);
1609 
1610 	config_group_init_type_name(&gi->configs_group, "configs",
1611 			&config_desc_type);
1612 	configfs_add_default_group(&gi->configs_group, &gi->group);
1613 
1614 	config_group_init_type_name(&gi->strings_group, "strings",
1615 			&gadget_strings_strings_type);
1616 	configfs_add_default_group(&gi->strings_group, &gi->group);
1617 
1618 	config_group_init_type_name(&gi->os_desc_group, "os_desc",
1619 			&os_desc_type);
1620 	configfs_add_default_group(&gi->os_desc_group, &gi->group);
1621 
1622 	gi->composite.bind = configfs_do_nothing;
1623 	gi->composite.unbind = configfs_do_nothing;
1624 	gi->composite.suspend = NULL;
1625 	gi->composite.resume = NULL;
1626 	gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1627 
1628 	spin_lock_init(&gi->spinlock);
1629 	mutex_init(&gi->lock);
1630 	INIT_LIST_HEAD(&gi->string_list);
1631 	INIT_LIST_HEAD(&gi->available_func);
1632 
1633 	composite_init_dev(&gi->cdev);
1634 	gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1635 	gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1636 	gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1637 
1638 	gi->composite.gadget_driver = configfs_driver_template;
1639 
1640 	gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1641 	gi->composite.name = gi->composite.gadget_driver.function;
1642 
1643 	if (!gi->composite.gadget_driver.function)
1644 		goto err;
1645 
1646 	return &gi->group;
1647 err:
1648 	kfree(gi);
1649 	return ERR_PTR(-ENOMEM);
1650 }
1651 
1652 static void gadgets_drop(struct config_group *group, struct config_item *item)
1653 {
1654 	config_item_put(item);
1655 }
1656 
1657 static struct configfs_group_operations gadgets_ops = {
1658 	.make_group     = &gadgets_make,
1659 	.drop_item      = &gadgets_drop,
1660 };
1661 
1662 static const struct config_item_type gadgets_type = {
1663 	.ct_group_ops   = &gadgets_ops,
1664 	.ct_owner       = THIS_MODULE,
1665 };
1666 
1667 static struct configfs_subsystem gadget_subsys = {
1668 	.su_group = {
1669 		.cg_item = {
1670 			.ci_namebuf = "usb_gadget",
1671 			.ci_type = &gadgets_type,
1672 		},
1673 	},
1674 	.su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1675 };
1676 
1677 void unregister_gadget_item(struct config_item *item)
1678 {
1679 	struct gadget_info *gi = to_gadget_info(item);
1680 
1681 	mutex_lock(&gi->lock);
1682 	unregister_gadget(gi);
1683 	mutex_unlock(&gi->lock);
1684 }
1685 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1686 
1687 static int __init gadget_cfs_init(void)
1688 {
1689 	int ret;
1690 
1691 	config_group_init(&gadget_subsys.su_group);
1692 
1693 	ret = configfs_register_subsystem(&gadget_subsys);
1694 	return ret;
1695 }
1696 module_init(gadget_cfs_init);
1697 
1698 static void __exit gadget_cfs_exit(void)
1699 {
1700 	configfs_unregister_subsystem(&gadget_subsys);
1701 }
1702 module_exit(gadget_cfs_exit);
1703