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