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