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