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