xref: /openbmc/linux/drivers/usb/gadget/configfs.c (revision a8fe58ce)
1 #include <linux/configfs.h>
2 #include <linux/module.h>
3 #include <linux/slab.h>
4 #include <linux/device.h>
5 #include <linux/nls.h>
6 #include <linux/usb/composite.h>
7 #include <linux/usb/gadget_configfs.h>
8 #include "configfs.h"
9 #include "u_f.h"
10 #include "u_os_desc.h"
11 
12 int check_user_usb_string(const char *name,
13 		struct usb_gadget_strings *stringtab_dev)
14 {
15 	unsigned primary_lang;
16 	unsigned sub_lang;
17 	u16 num;
18 	int ret;
19 
20 	ret = kstrtou16(name, 0, &num);
21 	if (ret)
22 		return ret;
23 
24 	primary_lang = num & 0x3ff;
25 	sub_lang = num >> 10;
26 
27 	/* simple sanity check for valid langid */
28 	switch (primary_lang) {
29 	case 0:
30 	case 0x62 ... 0xfe:
31 	case 0x100 ... 0x3ff:
32 		return -EINVAL;
33 	}
34 	if (!sub_lang)
35 		return -EINVAL;
36 
37 	stringtab_dev->language = num;
38 	return 0;
39 }
40 
41 #define MAX_NAME_LEN	40
42 #define MAX_USB_STRING_LANGS 2
43 
44 static const struct usb_descriptor_header *otg_desc[2];
45 
46 struct gadget_info {
47 	struct config_group group;
48 	struct config_group functions_group;
49 	struct config_group configs_group;
50 	struct config_group strings_group;
51 	struct config_group os_desc_group;
52 	struct config_group *default_groups[5];
53 
54 	struct mutex lock;
55 	struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
56 	struct list_head string_list;
57 	struct list_head available_func;
58 
59 	struct usb_composite_driver composite;
60 	struct usb_composite_dev cdev;
61 	bool use_os_desc;
62 	char b_vendor_code;
63 	char qw_sign[OS_STRING_QW_SIGN_LEN];
64 };
65 
66 static inline struct gadget_info *to_gadget_info(struct config_item *item)
67 {
68 	 return container_of(to_config_group(item), struct gadget_info, group);
69 }
70 
71 struct config_usb_cfg {
72 	struct config_group group;
73 	struct config_group strings_group;
74 	struct config_group *default_groups[2];
75 	struct list_head string_list;
76 	struct usb_configuration c;
77 	struct list_head func_list;
78 	struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
79 };
80 
81 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
82 {
83 	return container_of(to_config_group(item), struct config_usb_cfg,
84 			group);
85 }
86 
87 struct gadget_strings {
88 	struct usb_gadget_strings stringtab_dev;
89 	struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
90 	char *manufacturer;
91 	char *product;
92 	char *serialnumber;
93 
94 	struct config_group group;
95 	struct list_head list;
96 };
97 
98 struct os_desc {
99 	struct config_group group;
100 };
101 
102 struct gadget_config_name {
103 	struct usb_gadget_strings stringtab_dev;
104 	struct usb_string strings;
105 	char *configuration;
106 
107 	struct config_group group;
108 	struct list_head list;
109 };
110 
111 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 > 126)
118 		return -EOVERFLOW;
119 
120 	str = kstrdup(s, GFP_KERNEL);
121 	if (!str)
122 		return -ENOMEM;
123 	if (str[ret - 1] == '\n')
124 		str[ret - 1] = '\0';
125 	kfree(copy);
126 	*s_copy = str;
127 	return 0;
128 }
129 
130 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name)	\
131 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
132 			char *page)	\
133 {	\
134 	return sprintf(page, "0x%02x\n", \
135 		to_gadget_info(item)->cdev.desc.__name); \
136 }
137 
138 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name)	\
139 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
140 			char *page)	\
141 {	\
142 	return sprintf(page, "0x%04x\n", \
143 		le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
144 }
145 
146 
147 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name)		\
148 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
149 		const char *page, size_t len)		\
150 {							\
151 	u8 val;						\
152 	int ret;					\
153 	ret = kstrtou8(page, 0, &val);			\
154 	if (ret)					\
155 		return ret;				\
156 	to_gadget_info(item)->cdev.desc._name = val;	\
157 	return len;					\
158 }
159 
160 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name)	\
161 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
162 		const char *page, size_t len)		\
163 {							\
164 	u16 val;					\
165 	int ret;					\
166 	ret = kstrtou16(page, 0, &val);			\
167 	if (ret)					\
168 		return ret;				\
169 	to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val);	\
170 	return len;					\
171 }
172 
173 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type)	\
174 	GI_DEVICE_DESC_SIMPLE_R_##_type(_name)	\
175 	GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
176 
177 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
178 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
179 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
180 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
181 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
182 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
183 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
184 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
185 
186 static ssize_t is_valid_bcd(u16 bcd_val)
187 {
188 	if ((bcd_val & 0xf) > 9)
189 		return -EINVAL;
190 	if (((bcd_val >> 4) & 0xf) > 9)
191 		return -EINVAL;
192 	if (((bcd_val >> 8) & 0xf) > 9)
193 		return -EINVAL;
194 	if (((bcd_val >> 12) & 0xf) > 9)
195 		return -EINVAL;
196 	return 0;
197 }
198 
199 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
200 		const char *page, size_t len)
201 {
202 	u16 bcdDevice;
203 	int ret;
204 
205 	ret = kstrtou16(page, 0, &bcdDevice);
206 	if (ret)
207 		return ret;
208 	ret = is_valid_bcd(bcdDevice);
209 	if (ret)
210 		return ret;
211 
212 	to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
213 	return len;
214 }
215 
216 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
217 		const char *page, size_t len)
218 {
219 	u16 bcdUSB;
220 	int ret;
221 
222 	ret = kstrtou16(page, 0, &bcdUSB);
223 	if (ret)
224 		return ret;
225 	ret = is_valid_bcd(bcdUSB);
226 	if (ret)
227 		return ret;
228 
229 	to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
230 	return len;
231 }
232 
233 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
234 {
235 	char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
236 
237 	return sprintf(page, "%s\n", udc_name ?: "");
238 }
239 
240 static int unregister_gadget(struct gadget_info *gi)
241 {
242 	int ret;
243 
244 	if (!gi->composite.gadget_driver.udc_name)
245 		return -ENODEV;
246 
247 	ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
248 	if (ret)
249 		return ret;
250 	kfree(gi->composite.gadget_driver.udc_name);
251 	gi->composite.gadget_driver.udc_name = NULL;
252 	return 0;
253 }
254 
255 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
256 		const char *page, size_t len)
257 {
258 	struct gadget_info *gi = to_gadget_info(item);
259 	char *name;
260 	int ret;
261 
262 	name = kstrdup(page, GFP_KERNEL);
263 	if (!name)
264 		return -ENOMEM;
265 	if (name[len - 1] == '\n')
266 		name[len - 1] = '\0';
267 
268 	mutex_lock(&gi->lock);
269 
270 	if (!strlen(name)) {
271 		ret = unregister_gadget(gi);
272 		if (ret)
273 			goto err;
274 	} else {
275 		if (gi->composite.gadget_driver.udc_name) {
276 			ret = -EBUSY;
277 			goto err;
278 		}
279 		gi->composite.gadget_driver.udc_name = name;
280 		ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
281 		if (ret) {
282 			gi->composite.gadget_driver.udc_name = NULL;
283 			goto err;
284 		}
285 	}
286 	mutex_unlock(&gi->lock);
287 	return len;
288 err:
289 	kfree(name);
290 	mutex_unlock(&gi->lock);
291 	return ret;
292 }
293 
294 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
295 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
296 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
297 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
298 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
299 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
300 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
301 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
302 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
303 
304 static struct configfs_attribute *gadget_root_attrs[] = {
305 	&gadget_dev_desc_attr_bDeviceClass,
306 	&gadget_dev_desc_attr_bDeviceSubClass,
307 	&gadget_dev_desc_attr_bDeviceProtocol,
308 	&gadget_dev_desc_attr_bMaxPacketSize0,
309 	&gadget_dev_desc_attr_idVendor,
310 	&gadget_dev_desc_attr_idProduct,
311 	&gadget_dev_desc_attr_bcdDevice,
312 	&gadget_dev_desc_attr_bcdUSB,
313 	&gadget_dev_desc_attr_UDC,
314 	NULL,
315 };
316 
317 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
318 {
319 	 return container_of(to_config_group(item), struct gadget_strings,
320 			 group);
321 }
322 
323 static inline struct gadget_config_name *to_gadget_config_name(
324 		struct config_item *item)
325 {
326 	 return container_of(to_config_group(item), struct gadget_config_name,
327 			 group);
328 }
329 
330 static inline struct usb_function_instance *to_usb_function_instance(
331 		struct config_item *item)
332 {
333 	 return container_of(to_config_group(item),
334 			 struct usb_function_instance, group);
335 }
336 
337 static void gadget_info_attr_release(struct config_item *item)
338 {
339 	struct gadget_info *gi = to_gadget_info(item);
340 
341 	WARN_ON(!list_empty(&gi->cdev.configs));
342 	WARN_ON(!list_empty(&gi->string_list));
343 	WARN_ON(!list_empty(&gi->available_func));
344 	kfree(gi->composite.gadget_driver.function);
345 	kfree(gi);
346 }
347 
348 static struct configfs_item_operations gadget_root_item_ops = {
349 	.release                = gadget_info_attr_release,
350 };
351 
352 static void gadget_config_attr_release(struct config_item *item)
353 {
354 	struct config_usb_cfg *cfg = to_config_usb_cfg(item);
355 
356 	WARN_ON(!list_empty(&cfg->c.functions));
357 	list_del(&cfg->c.list);
358 	kfree(cfg->c.label);
359 	kfree(cfg);
360 }
361 
362 static int config_usb_cfg_link(
363 	struct config_item *usb_cfg_ci,
364 	struct config_item *usb_func_ci)
365 {
366 	struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
367 	struct usb_composite_dev *cdev = cfg->c.cdev;
368 	struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
369 
370 	struct config_group *group = to_config_group(usb_func_ci);
371 	struct usb_function_instance *fi = container_of(group,
372 			struct usb_function_instance, group);
373 	struct usb_function_instance *a_fi;
374 	struct usb_function *f;
375 	int ret;
376 
377 	mutex_lock(&gi->lock);
378 	/*
379 	 * Make sure this function is from within our _this_ gadget and not
380 	 * from another gadget or a random directory.
381 	 * Also a function instance can only be linked once.
382 	 */
383 	list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
384 		if (a_fi == fi)
385 			break;
386 	}
387 	if (a_fi != fi) {
388 		ret = -EINVAL;
389 		goto out;
390 	}
391 
392 	list_for_each_entry(f, &cfg->func_list, list) {
393 		if (f->fi == fi) {
394 			ret = -EEXIST;
395 			goto out;
396 		}
397 	}
398 
399 	f = usb_get_function(fi);
400 	if (IS_ERR(f)) {
401 		ret = PTR_ERR(f);
402 		goto out;
403 	}
404 
405 	/* stash the function until we bind it to the gadget */
406 	list_add_tail(&f->list, &cfg->func_list);
407 	ret = 0;
408 out:
409 	mutex_unlock(&gi->lock);
410 	return ret;
411 }
412 
413 static int config_usb_cfg_unlink(
414 	struct config_item *usb_cfg_ci,
415 	struct config_item *usb_func_ci)
416 {
417 	struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
418 	struct usb_composite_dev *cdev = cfg->c.cdev;
419 	struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
420 
421 	struct config_group *group = to_config_group(usb_func_ci);
422 	struct usb_function_instance *fi = container_of(group,
423 			struct usb_function_instance, group);
424 	struct usb_function *f;
425 
426 	/*
427 	 * ideally I would like to forbid to unlink functions while a gadget is
428 	 * bound to an UDC. Since this isn't possible at the moment, we simply
429 	 * force an unbind, the function is available here and then we can
430 	 * remove the function.
431 	 */
432 	mutex_lock(&gi->lock);
433 	if (gi->composite.gadget_driver.udc_name)
434 		unregister_gadget(gi);
435 	WARN_ON(gi->composite.gadget_driver.udc_name);
436 
437 	list_for_each_entry(f, &cfg->func_list, list) {
438 		if (f->fi == fi) {
439 			list_del(&f->list);
440 			usb_put_function(f);
441 			mutex_unlock(&gi->lock);
442 			return 0;
443 		}
444 	}
445 	mutex_unlock(&gi->lock);
446 	WARN(1, "Unable to locate function to unbind\n");
447 	return 0;
448 }
449 
450 static struct configfs_item_operations gadget_config_item_ops = {
451 	.release                = gadget_config_attr_release,
452 	.allow_link             = config_usb_cfg_link,
453 	.drop_link              = config_usb_cfg_unlink,
454 };
455 
456 
457 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
458 		char *page)
459 {
460 	return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
461 }
462 
463 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
464 		const char *page, size_t len)
465 {
466 	u16 val;
467 	int ret;
468 	ret = kstrtou16(page, 0, &val);
469 	if (ret)
470 		return ret;
471 	if (DIV_ROUND_UP(val, 8) > 0xff)
472 		return -ERANGE;
473 	to_config_usb_cfg(item)->c.MaxPower = val;
474 	return len;
475 }
476 
477 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
478 		char *page)
479 {
480 	return sprintf(page, "0x%02x\n",
481 		to_config_usb_cfg(item)->c.bmAttributes);
482 }
483 
484 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
485 		const char *page, size_t len)
486 {
487 	u8 val;
488 	int ret;
489 	ret = kstrtou8(page, 0, &val);
490 	if (ret)
491 		return ret;
492 	if (!(val & USB_CONFIG_ATT_ONE))
493 		return -EINVAL;
494 	if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
495 				USB_CONFIG_ATT_WAKEUP))
496 		return -EINVAL;
497 	to_config_usb_cfg(item)->c.bmAttributes = val;
498 	return len;
499 }
500 
501 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
502 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
503 
504 static struct configfs_attribute *gadget_config_attrs[] = {
505 	&gadget_config_desc_attr_MaxPower,
506 	&gadget_config_desc_attr_bmAttributes,
507 	NULL,
508 };
509 
510 static struct config_item_type gadget_config_type = {
511 	.ct_item_ops	= &gadget_config_item_ops,
512 	.ct_attrs	= gadget_config_attrs,
513 	.ct_owner	= THIS_MODULE,
514 };
515 
516 static struct config_item_type gadget_root_type = {
517 	.ct_item_ops	= &gadget_root_item_ops,
518 	.ct_attrs	= gadget_root_attrs,
519 	.ct_owner	= THIS_MODULE,
520 };
521 
522 static void composite_init_dev(struct usb_composite_dev *cdev)
523 {
524 	spin_lock_init(&cdev->lock);
525 	INIT_LIST_HEAD(&cdev->configs);
526 	INIT_LIST_HEAD(&cdev->gstrings);
527 }
528 
529 static struct config_group *function_make(
530 		struct config_group *group,
531 		const char *name)
532 {
533 	struct gadget_info *gi;
534 	struct usb_function_instance *fi;
535 	char buf[MAX_NAME_LEN];
536 	char *func_name;
537 	char *instance_name;
538 	int ret;
539 
540 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
541 	if (ret >= MAX_NAME_LEN)
542 		return ERR_PTR(-ENAMETOOLONG);
543 
544 	func_name = buf;
545 	instance_name = strchr(func_name, '.');
546 	if (!instance_name) {
547 		pr_err("Unable to locate . in FUNC.INSTANCE\n");
548 		return ERR_PTR(-EINVAL);
549 	}
550 	*instance_name = '\0';
551 	instance_name++;
552 
553 	fi = usb_get_function_instance(func_name);
554 	if (IS_ERR(fi))
555 		return ERR_CAST(fi);
556 
557 	ret = config_item_set_name(&fi->group.cg_item, "%s", name);
558 	if (ret) {
559 		usb_put_function_instance(fi);
560 		return ERR_PTR(ret);
561 	}
562 	if (fi->set_inst_name) {
563 		ret = fi->set_inst_name(fi, instance_name);
564 		if (ret) {
565 			usb_put_function_instance(fi);
566 			return ERR_PTR(ret);
567 		}
568 	}
569 
570 	gi = container_of(group, struct gadget_info, functions_group);
571 
572 	mutex_lock(&gi->lock);
573 	list_add_tail(&fi->cfs_list, &gi->available_func);
574 	mutex_unlock(&gi->lock);
575 	return &fi->group;
576 }
577 
578 static void function_drop(
579 		struct config_group *group,
580 		struct config_item *item)
581 {
582 	struct usb_function_instance *fi = to_usb_function_instance(item);
583 	struct gadget_info *gi;
584 
585 	gi = container_of(group, struct gadget_info, functions_group);
586 
587 	mutex_lock(&gi->lock);
588 	list_del(&fi->cfs_list);
589 	mutex_unlock(&gi->lock);
590 	config_item_put(item);
591 }
592 
593 static struct configfs_group_operations functions_ops = {
594 	.make_group     = &function_make,
595 	.drop_item      = &function_drop,
596 };
597 
598 static struct config_item_type functions_type = {
599 	.ct_group_ops   = &functions_ops,
600 	.ct_owner       = THIS_MODULE,
601 };
602 
603 GS_STRINGS_RW(gadget_config_name, configuration);
604 
605 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
606 	&gadget_config_name_attr_configuration,
607 	NULL,
608 };
609 
610 static void gadget_config_name_attr_release(struct config_item *item)
611 {
612 	struct gadget_config_name *cn = to_gadget_config_name(item);
613 
614 	kfree(cn->configuration);
615 
616 	list_del(&cn->list);
617 	kfree(cn);
618 }
619 
620 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
621 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
622 
623 static struct config_group *config_desc_make(
624 		struct config_group *group,
625 		const char *name)
626 {
627 	struct gadget_info *gi;
628 	struct config_usb_cfg *cfg;
629 	char buf[MAX_NAME_LEN];
630 	char *num_str;
631 	u8 num;
632 	int ret;
633 
634 	gi = container_of(group, struct gadget_info, configs_group);
635 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
636 	if (ret >= MAX_NAME_LEN)
637 		return ERR_PTR(-ENAMETOOLONG);
638 
639 	num_str = strchr(buf, '.');
640 	if (!num_str) {
641 		pr_err("Unable to locate . in name.bConfigurationValue\n");
642 		return ERR_PTR(-EINVAL);
643 	}
644 
645 	*num_str = '\0';
646 	num_str++;
647 
648 	if (!strlen(buf))
649 		return ERR_PTR(-EINVAL);
650 
651 	ret = kstrtou8(num_str, 0, &num);
652 	if (ret)
653 		return ERR_PTR(ret);
654 
655 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
656 	if (!cfg)
657 		return ERR_PTR(-ENOMEM);
658 	cfg->c.label = kstrdup(buf, GFP_KERNEL);
659 	if (!cfg->c.label) {
660 		ret = -ENOMEM;
661 		goto err;
662 	}
663 	cfg->c.bConfigurationValue = num;
664 	cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
665 	cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
666 	INIT_LIST_HEAD(&cfg->string_list);
667 	INIT_LIST_HEAD(&cfg->func_list);
668 
669 	cfg->group.default_groups = cfg->default_groups;
670 	cfg->default_groups[0] = &cfg->strings_group;
671 
672 	config_group_init_type_name(&cfg->group, name,
673 				&gadget_config_type);
674 	config_group_init_type_name(&cfg->strings_group, "strings",
675 			&gadget_config_name_strings_type);
676 
677 	ret = usb_add_config_only(&gi->cdev, &cfg->c);
678 	if (ret)
679 		goto err;
680 
681 	return &cfg->group;
682 err:
683 	kfree(cfg->c.label);
684 	kfree(cfg);
685 	return ERR_PTR(ret);
686 }
687 
688 static void config_desc_drop(
689 		struct config_group *group,
690 		struct config_item *item)
691 {
692 	config_item_put(item);
693 }
694 
695 static struct configfs_group_operations config_desc_ops = {
696 	.make_group     = &config_desc_make,
697 	.drop_item      = &config_desc_drop,
698 };
699 
700 static struct config_item_type config_desc_type = {
701 	.ct_group_ops   = &config_desc_ops,
702 	.ct_owner       = THIS_MODULE,
703 };
704 
705 GS_STRINGS_RW(gadget_strings, manufacturer);
706 GS_STRINGS_RW(gadget_strings, product);
707 GS_STRINGS_RW(gadget_strings, serialnumber);
708 
709 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
710 	&gadget_strings_attr_manufacturer,
711 	&gadget_strings_attr_product,
712 	&gadget_strings_attr_serialnumber,
713 	NULL,
714 };
715 
716 static void gadget_strings_attr_release(struct config_item *item)
717 {
718 	struct gadget_strings *gs = to_gadget_strings(item);
719 
720 	kfree(gs->manufacturer);
721 	kfree(gs->product);
722 	kfree(gs->serialnumber);
723 
724 	list_del(&gs->list);
725 	kfree(gs);
726 }
727 
728 USB_CONFIG_STRING_RW_OPS(gadget_strings);
729 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
730 
731 static inline struct os_desc *to_os_desc(struct config_item *item)
732 {
733 	return container_of(to_config_group(item), struct os_desc, group);
734 }
735 
736 static inline struct gadget_info *os_desc_item_to_gadget_info(
737 		struct config_item *item)
738 {
739 	return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
740 }
741 
742 static ssize_t os_desc_use_show(struct config_item *item, char *page)
743 {
744 	return sprintf(page, "%d",
745 			os_desc_item_to_gadget_info(item)->use_os_desc);
746 }
747 
748 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
749 				 size_t len)
750 {
751 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
752 	int ret;
753 	bool use;
754 
755 	mutex_lock(&gi->lock);
756 	ret = strtobool(page, &use);
757 	if (!ret) {
758 		gi->use_os_desc = use;
759 		ret = len;
760 	}
761 	mutex_unlock(&gi->lock);
762 
763 	return ret;
764 }
765 
766 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
767 {
768 	return sprintf(page, "%d",
769 			os_desc_item_to_gadget_info(item)->b_vendor_code);
770 }
771 
772 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
773 					   const char *page, size_t len)
774 {
775 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
776 	int ret;
777 	u8 b_vendor_code;
778 
779 	mutex_lock(&gi->lock);
780 	ret = kstrtou8(page, 0, &b_vendor_code);
781 	if (!ret) {
782 		gi->b_vendor_code = b_vendor_code;
783 		ret = len;
784 	}
785 	mutex_unlock(&gi->lock);
786 
787 	return ret;
788 }
789 
790 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
791 {
792 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
793 
794 	memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
795 	return OS_STRING_QW_SIGN_LEN;
796 }
797 
798 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
799 				     size_t len)
800 {
801 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
802 	int res, l;
803 
804 	l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
805 	if (page[l - 1] == '\n')
806 		--l;
807 
808 	mutex_lock(&gi->lock);
809 	res = utf8s_to_utf16s(page, l,
810 			      UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
811 			      OS_STRING_QW_SIGN_LEN);
812 	if (res > 0)
813 		res = len;
814 	mutex_unlock(&gi->lock);
815 
816 	return res;
817 }
818 
819 CONFIGFS_ATTR(os_desc_, use);
820 CONFIGFS_ATTR(os_desc_, b_vendor_code);
821 CONFIGFS_ATTR(os_desc_, qw_sign);
822 
823 static struct configfs_attribute *os_desc_attrs[] = {
824 	&os_desc_attr_use,
825 	&os_desc_attr_b_vendor_code,
826 	&os_desc_attr_qw_sign,
827 	NULL,
828 };
829 
830 static void os_desc_attr_release(struct config_item *item)
831 {
832 	struct os_desc *os_desc = to_os_desc(item);
833 	kfree(os_desc);
834 }
835 
836 static int os_desc_link(struct config_item *os_desc_ci,
837 			struct config_item *usb_cfg_ci)
838 {
839 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
840 					struct gadget_info, os_desc_group);
841 	struct usb_composite_dev *cdev = &gi->cdev;
842 	struct config_usb_cfg *c_target =
843 		container_of(to_config_group(usb_cfg_ci),
844 			     struct config_usb_cfg, group);
845 	struct usb_configuration *c;
846 	int ret;
847 
848 	mutex_lock(&gi->lock);
849 	list_for_each_entry(c, &cdev->configs, list) {
850 		if (c == &c_target->c)
851 			break;
852 	}
853 	if (c != &c_target->c) {
854 		ret = -EINVAL;
855 		goto out;
856 	}
857 
858 	if (cdev->os_desc_config) {
859 		ret = -EBUSY;
860 		goto out;
861 	}
862 
863 	cdev->os_desc_config = &c_target->c;
864 	ret = 0;
865 
866 out:
867 	mutex_unlock(&gi->lock);
868 	return ret;
869 }
870 
871 static int os_desc_unlink(struct config_item *os_desc_ci,
872 			  struct config_item *usb_cfg_ci)
873 {
874 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
875 					struct gadget_info, os_desc_group);
876 	struct usb_composite_dev *cdev = &gi->cdev;
877 
878 	mutex_lock(&gi->lock);
879 	if (gi->composite.gadget_driver.udc_name)
880 		unregister_gadget(gi);
881 	cdev->os_desc_config = NULL;
882 	WARN_ON(gi->composite.gadget_driver.udc_name);
883 	mutex_unlock(&gi->lock);
884 	return 0;
885 }
886 
887 static struct configfs_item_operations os_desc_ops = {
888 	.release                = os_desc_attr_release,
889 	.allow_link		= os_desc_link,
890 	.drop_link		= os_desc_unlink,
891 };
892 
893 static struct config_item_type os_desc_type = {
894 	.ct_item_ops	= &os_desc_ops,
895 	.ct_attrs	= os_desc_attrs,
896 	.ct_owner	= THIS_MODULE,
897 };
898 
899 static inline struct usb_os_desc_ext_prop
900 *to_usb_os_desc_ext_prop(struct config_item *item)
901 {
902 	return container_of(item, struct usb_os_desc_ext_prop, item);
903 }
904 
905 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
906 {
907 	return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
908 }
909 
910 static ssize_t ext_prop_type_store(struct config_item *item,
911 				   const char *page, size_t len)
912 {
913 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
914 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
915 	u8 type;
916 	int ret;
917 
918 	if (desc->opts_mutex)
919 		mutex_lock(desc->opts_mutex);
920 	ret = kstrtou8(page, 0, &type);
921 	if (ret)
922 		goto end;
923 	if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
924 		ret = -EINVAL;
925 		goto end;
926 	}
927 
928 	if ((ext_prop->type == USB_EXT_PROP_BINARY ||
929 	    ext_prop->type == USB_EXT_PROP_LE32 ||
930 	    ext_prop->type == USB_EXT_PROP_BE32) &&
931 	    (type == USB_EXT_PROP_UNICODE ||
932 	    type == USB_EXT_PROP_UNICODE_ENV ||
933 	    type == USB_EXT_PROP_UNICODE_LINK))
934 		ext_prop->data_len <<= 1;
935 	else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
936 		   ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
937 		   ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
938 		   (type == USB_EXT_PROP_BINARY ||
939 		   type == USB_EXT_PROP_LE32 ||
940 		   type == USB_EXT_PROP_BE32))
941 		ext_prop->data_len >>= 1;
942 	ext_prop->type = type;
943 	ret = len;
944 
945 end:
946 	if (desc->opts_mutex)
947 		mutex_unlock(desc->opts_mutex);
948 	return ret;
949 }
950 
951 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
952 {
953 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
954 	int len = ext_prop->data_len;
955 
956 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
957 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
958 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
959 		len >>= 1;
960 	memcpy(page, ext_prop->data, len);
961 
962 	return len;
963 }
964 
965 static ssize_t ext_prop_data_store(struct config_item *item,
966 				   const char *page, size_t len)
967 {
968 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
969 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
970 	char *new_data;
971 	size_t ret_len = len;
972 
973 	if (page[len - 1] == '\n' || page[len - 1] == '\0')
974 		--len;
975 	new_data = kmemdup(page, len, GFP_KERNEL);
976 	if (!new_data)
977 		return -ENOMEM;
978 
979 	if (desc->opts_mutex)
980 		mutex_lock(desc->opts_mutex);
981 	kfree(ext_prop->data);
982 	ext_prop->data = new_data;
983 	desc->ext_prop_len -= ext_prop->data_len;
984 	ext_prop->data_len = len;
985 	desc->ext_prop_len += ext_prop->data_len;
986 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
987 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
988 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
989 		desc->ext_prop_len -= ext_prop->data_len;
990 		ext_prop->data_len <<= 1;
991 		ext_prop->data_len += 2;
992 		desc->ext_prop_len += ext_prop->data_len;
993 	}
994 	if (desc->opts_mutex)
995 		mutex_unlock(desc->opts_mutex);
996 	return ret_len;
997 }
998 
999 CONFIGFS_ATTR(ext_prop_, type);
1000 CONFIGFS_ATTR(ext_prop_, data);
1001 
1002 static struct configfs_attribute *ext_prop_attrs[] = {
1003 	&ext_prop_attr_type,
1004 	&ext_prop_attr_data,
1005 	NULL,
1006 };
1007 
1008 static void usb_os_desc_ext_prop_release(struct config_item *item)
1009 {
1010 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1011 
1012 	kfree(ext_prop); /* frees a whole chunk */
1013 }
1014 
1015 static struct configfs_item_operations ext_prop_ops = {
1016 	.release		= usb_os_desc_ext_prop_release,
1017 };
1018 
1019 static struct config_item *ext_prop_make(
1020 		struct config_group *group,
1021 		const char *name)
1022 {
1023 	struct usb_os_desc_ext_prop *ext_prop;
1024 	struct config_item_type *ext_prop_type;
1025 	struct usb_os_desc *desc;
1026 	char *vlabuf;
1027 
1028 	vla_group(data_chunk);
1029 	vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1030 	vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1031 
1032 	vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1033 	if (!vlabuf)
1034 		return ERR_PTR(-ENOMEM);
1035 
1036 	ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1037 	ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1038 
1039 	desc = container_of(group, struct usb_os_desc, group);
1040 	ext_prop_type->ct_item_ops = &ext_prop_ops;
1041 	ext_prop_type->ct_attrs = ext_prop_attrs;
1042 	ext_prop_type->ct_owner = desc->owner;
1043 
1044 	config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1045 
1046 	ext_prop->name = kstrdup(name, GFP_KERNEL);
1047 	if (!ext_prop->name) {
1048 		kfree(vlabuf);
1049 		return ERR_PTR(-ENOMEM);
1050 	}
1051 	desc->ext_prop_len += 14;
1052 	ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1053 	if (desc->opts_mutex)
1054 		mutex_lock(desc->opts_mutex);
1055 	desc->ext_prop_len += ext_prop->name_len;
1056 	list_add_tail(&ext_prop->entry, &desc->ext_prop);
1057 	++desc->ext_prop_count;
1058 	if (desc->opts_mutex)
1059 		mutex_unlock(desc->opts_mutex);
1060 
1061 	return &ext_prop->item;
1062 }
1063 
1064 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1065 {
1066 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1067 	struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1068 
1069 	if (desc->opts_mutex)
1070 		mutex_lock(desc->opts_mutex);
1071 	list_del(&ext_prop->entry);
1072 	--desc->ext_prop_count;
1073 	kfree(ext_prop->name);
1074 	desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1075 	if (desc->opts_mutex)
1076 		mutex_unlock(desc->opts_mutex);
1077 	config_item_put(item);
1078 }
1079 
1080 static struct configfs_group_operations interf_grp_ops = {
1081 	.make_item	= &ext_prop_make,
1082 	.drop_item	= &ext_prop_drop,
1083 };
1084 
1085 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1086 					     char *page)
1087 {
1088 	memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1089 	return 8;
1090 }
1091 
1092 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1093 					      const char *page, size_t len)
1094 {
1095 	struct usb_os_desc *desc = to_usb_os_desc(item);
1096 	int l;
1097 
1098 	l = min_t(int, 8, len);
1099 	if (page[l - 1] == '\n')
1100 		--l;
1101 	if (desc->opts_mutex)
1102 		mutex_lock(desc->opts_mutex);
1103 	memcpy(desc->ext_compat_id, page, l);
1104 
1105 	if (desc->opts_mutex)
1106 		mutex_unlock(desc->opts_mutex);
1107 
1108 	return len;
1109 }
1110 
1111 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1112 						 char *page)
1113 {
1114 	memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1115 	return 8;
1116 }
1117 
1118 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1119 						  const char *page, size_t len)
1120 {
1121 	struct usb_os_desc *desc = to_usb_os_desc(item);
1122 	int l;
1123 
1124 	l = min_t(int, 8, len);
1125 	if (page[l - 1] == '\n')
1126 		--l;
1127 	if (desc->opts_mutex)
1128 		mutex_lock(desc->opts_mutex);
1129 	memcpy(desc->ext_compat_id + 8, page, l);
1130 
1131 	if (desc->opts_mutex)
1132 		mutex_unlock(desc->opts_mutex);
1133 
1134 	return len;
1135 }
1136 
1137 CONFIGFS_ATTR(interf_grp_, compatible_id);
1138 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1139 
1140 static struct configfs_attribute *interf_grp_attrs[] = {
1141 	&interf_grp_attr_compatible_id,
1142 	&interf_grp_attr_sub_compatible_id,
1143 	NULL
1144 };
1145 
1146 int usb_os_desc_prepare_interf_dir(struct config_group *parent,
1147 				   int n_interf,
1148 				   struct usb_os_desc **desc,
1149 				   char **names,
1150 				   struct module *owner)
1151 {
1152 	struct config_group **f_default_groups, *os_desc_group,
1153 				**interface_groups;
1154 	struct config_item_type *os_desc_type, *interface_type;
1155 
1156 	vla_group(data_chunk);
1157 	vla_item(data_chunk, struct config_group *, f_default_groups, 2);
1158 	vla_item(data_chunk, struct config_group, os_desc_group, 1);
1159 	vla_item(data_chunk, struct config_group *, interface_groups,
1160 		 n_interf + 1);
1161 	vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1162 	vla_item(data_chunk, struct config_item_type, interface_type, 1);
1163 
1164 	char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1165 	if (!vlabuf)
1166 		return -ENOMEM;
1167 
1168 	f_default_groups = vla_ptr(vlabuf, data_chunk, f_default_groups);
1169 	os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1170 	os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1171 	interface_groups = vla_ptr(vlabuf, data_chunk, interface_groups);
1172 	interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1173 
1174 	parent->default_groups = f_default_groups;
1175 	os_desc_type->ct_owner = owner;
1176 	config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1177 	f_default_groups[0] = os_desc_group;
1178 
1179 	os_desc_group->default_groups = interface_groups;
1180 	interface_type->ct_group_ops = &interf_grp_ops;
1181 	interface_type->ct_attrs = interf_grp_attrs;
1182 	interface_type->ct_owner = owner;
1183 
1184 	while (n_interf--) {
1185 		struct usb_os_desc *d;
1186 
1187 		d = desc[n_interf];
1188 		d->owner = owner;
1189 		config_group_init_type_name(&d->group, "", interface_type);
1190 		config_item_set_name(&d->group.cg_item, "interface.%s",
1191 				     names[n_interf]);
1192 		interface_groups[n_interf] = &d->group;
1193 	}
1194 
1195 	return 0;
1196 }
1197 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1198 
1199 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1200 {
1201 	WARN_ON(1);
1202 	return -EINVAL;
1203 }
1204 
1205 int composite_dev_prepare(struct usb_composite_driver *composite,
1206 		struct usb_composite_dev *dev);
1207 
1208 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1209 				  struct usb_ep *ep0);
1210 
1211 static void purge_configs_funcs(struct gadget_info *gi)
1212 {
1213 	struct usb_configuration	*c;
1214 
1215 	list_for_each_entry(c, &gi->cdev.configs, list) {
1216 		struct usb_function *f, *tmp;
1217 		struct config_usb_cfg *cfg;
1218 
1219 		cfg = container_of(c, struct config_usb_cfg, c);
1220 
1221 		list_for_each_entry_safe(f, tmp, &c->functions, list) {
1222 
1223 			list_move_tail(&f->list, &cfg->func_list);
1224 			if (f->unbind) {
1225 				dev_err(&gi->cdev.gadget->dev, "unbind function"
1226 						" '%s'/%p\n", f->name, f);
1227 				f->unbind(c, f);
1228 			}
1229 		}
1230 		c->next_interface_id = 0;
1231 		memset(c->interface, 0, sizeof(c->interface));
1232 		c->superspeed = 0;
1233 		c->highspeed = 0;
1234 		c->fullspeed = 0;
1235 	}
1236 }
1237 
1238 static int configfs_composite_bind(struct usb_gadget *gadget,
1239 		struct usb_gadget_driver *gdriver)
1240 {
1241 	struct usb_composite_driver     *composite = to_cdriver(gdriver);
1242 	struct gadget_info		*gi = container_of(composite,
1243 						struct gadget_info, composite);
1244 	struct usb_composite_dev	*cdev = &gi->cdev;
1245 	struct usb_configuration	*c;
1246 	struct usb_string		*s;
1247 	unsigned			i;
1248 	int				ret;
1249 
1250 	/* the gi->lock is hold by the caller */
1251 	cdev->gadget = gadget;
1252 	set_gadget_data(gadget, cdev);
1253 	ret = composite_dev_prepare(composite, cdev);
1254 	if (ret)
1255 		return ret;
1256 	/* and now the gadget bind */
1257 	ret = -EINVAL;
1258 
1259 	if (list_empty(&gi->cdev.configs)) {
1260 		pr_err("Need at least one configuration in %s.\n",
1261 				gi->composite.name);
1262 		goto err_comp_cleanup;
1263 	}
1264 
1265 
1266 	list_for_each_entry(c, &gi->cdev.configs, list) {
1267 		struct config_usb_cfg *cfg;
1268 
1269 		cfg = container_of(c, struct config_usb_cfg, c);
1270 		if (list_empty(&cfg->func_list)) {
1271 			pr_err("Config %s/%d of %s needs at least one function.\n",
1272 			      c->label, c->bConfigurationValue,
1273 			      gi->composite.name);
1274 			goto err_comp_cleanup;
1275 		}
1276 	}
1277 
1278 	/* init all strings */
1279 	if (!list_empty(&gi->string_list)) {
1280 		struct gadget_strings *gs;
1281 
1282 		i = 0;
1283 		list_for_each_entry(gs, &gi->string_list, list) {
1284 
1285 			gi->gstrings[i] = &gs->stringtab_dev;
1286 			gs->stringtab_dev.strings = gs->strings;
1287 			gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1288 				gs->manufacturer;
1289 			gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1290 			gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1291 			i++;
1292 		}
1293 		gi->gstrings[i] = NULL;
1294 		s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1295 				USB_GADGET_FIRST_AVAIL_IDX);
1296 		if (IS_ERR(s)) {
1297 			ret = PTR_ERR(s);
1298 			goto err_comp_cleanup;
1299 		}
1300 
1301 		gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1302 		gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1303 		gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1304 	}
1305 
1306 	if (gi->use_os_desc) {
1307 		cdev->use_os_string = true;
1308 		cdev->b_vendor_code = gi->b_vendor_code;
1309 		memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1310 	}
1311 
1312 	if (gadget_is_otg(gadget) && !otg_desc[0]) {
1313 		struct usb_descriptor_header *usb_desc;
1314 
1315 		usb_desc = usb_otg_descriptor_alloc(gadget);
1316 		if (!usb_desc) {
1317 			ret = -ENOMEM;
1318 			goto err_comp_cleanup;
1319 		}
1320 		usb_otg_descriptor_init(gadget, usb_desc);
1321 		otg_desc[0] = usb_desc;
1322 		otg_desc[1] = NULL;
1323 	}
1324 
1325 	/* Go through all configs, attach all functions */
1326 	list_for_each_entry(c, &gi->cdev.configs, list) {
1327 		struct config_usb_cfg *cfg;
1328 		struct usb_function *f;
1329 		struct usb_function *tmp;
1330 		struct gadget_config_name *cn;
1331 
1332 		if (gadget_is_otg(gadget))
1333 			c->descriptors = otg_desc;
1334 
1335 		cfg = container_of(c, struct config_usb_cfg, c);
1336 		if (!list_empty(&cfg->string_list)) {
1337 			i = 0;
1338 			list_for_each_entry(cn, &cfg->string_list, list) {
1339 				cfg->gstrings[i] = &cn->stringtab_dev;
1340 				cn->stringtab_dev.strings = &cn->strings;
1341 				cn->strings.s = cn->configuration;
1342 				i++;
1343 			}
1344 			cfg->gstrings[i] = NULL;
1345 			s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1346 			if (IS_ERR(s)) {
1347 				ret = PTR_ERR(s);
1348 				goto err_comp_cleanup;
1349 			}
1350 			c->iConfiguration = s[0].id;
1351 		}
1352 
1353 		list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1354 			list_del(&f->list);
1355 			ret = usb_add_function(c, f);
1356 			if (ret) {
1357 				list_add(&f->list, &cfg->func_list);
1358 				goto err_purge_funcs;
1359 			}
1360 		}
1361 		usb_ep_autoconfig_reset(cdev->gadget);
1362 	}
1363 	if (cdev->use_os_string) {
1364 		ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1365 		if (ret)
1366 			goto err_purge_funcs;
1367 	}
1368 
1369 	usb_ep_autoconfig_reset(cdev->gadget);
1370 	return 0;
1371 
1372 err_purge_funcs:
1373 	purge_configs_funcs(gi);
1374 err_comp_cleanup:
1375 	composite_dev_cleanup(cdev);
1376 	return ret;
1377 }
1378 
1379 static void configfs_composite_unbind(struct usb_gadget *gadget)
1380 {
1381 	struct usb_composite_dev	*cdev;
1382 	struct gadget_info		*gi;
1383 
1384 	/* the gi->lock is hold by the caller */
1385 
1386 	cdev = get_gadget_data(gadget);
1387 	gi = container_of(cdev, struct gadget_info, cdev);
1388 
1389 	kfree(otg_desc[0]);
1390 	otg_desc[0] = NULL;
1391 	purge_configs_funcs(gi);
1392 	composite_dev_cleanup(cdev);
1393 	usb_ep_autoconfig_reset(cdev->gadget);
1394 	cdev->gadget = NULL;
1395 	set_gadget_data(gadget, NULL);
1396 }
1397 
1398 static const struct usb_gadget_driver configfs_driver_template = {
1399 	.bind           = configfs_composite_bind,
1400 	.unbind         = configfs_composite_unbind,
1401 
1402 	.setup          = composite_setup,
1403 	.reset          = composite_disconnect,
1404 	.disconnect     = composite_disconnect,
1405 
1406 	.suspend	= composite_suspend,
1407 	.resume		= composite_resume,
1408 
1409 	.max_speed	= USB_SPEED_SUPER,
1410 	.driver = {
1411 		.owner          = THIS_MODULE,
1412 		.name		= "configfs-gadget",
1413 	},
1414 };
1415 
1416 static struct config_group *gadgets_make(
1417 		struct config_group *group,
1418 		const char *name)
1419 {
1420 	struct gadget_info *gi;
1421 
1422 	gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1423 	if (!gi)
1424 		return ERR_PTR(-ENOMEM);
1425 
1426 	gi->group.default_groups = gi->default_groups;
1427 	gi->group.default_groups[0] = &gi->functions_group;
1428 	gi->group.default_groups[1] = &gi->configs_group;
1429 	gi->group.default_groups[2] = &gi->strings_group;
1430 	gi->group.default_groups[3] = &gi->os_desc_group;
1431 
1432 	config_group_init_type_name(&gi->functions_group, "functions",
1433 			&functions_type);
1434 	config_group_init_type_name(&gi->configs_group, "configs",
1435 			&config_desc_type);
1436 	config_group_init_type_name(&gi->strings_group, "strings",
1437 			&gadget_strings_strings_type);
1438 	config_group_init_type_name(&gi->os_desc_group, "os_desc",
1439 			&os_desc_type);
1440 
1441 	gi->composite.bind = configfs_do_nothing;
1442 	gi->composite.unbind = configfs_do_nothing;
1443 	gi->composite.suspend = NULL;
1444 	gi->composite.resume = NULL;
1445 	gi->composite.max_speed = USB_SPEED_SUPER;
1446 
1447 	mutex_init(&gi->lock);
1448 	INIT_LIST_HEAD(&gi->string_list);
1449 	INIT_LIST_HEAD(&gi->available_func);
1450 
1451 	composite_init_dev(&gi->cdev);
1452 	gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1453 	gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1454 	gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1455 
1456 	gi->composite.gadget_driver = configfs_driver_template;
1457 
1458 	gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1459 	gi->composite.name = gi->composite.gadget_driver.function;
1460 
1461 	if (!gi->composite.gadget_driver.function)
1462 		goto err;
1463 
1464 	config_group_init_type_name(&gi->group, name,
1465 				&gadget_root_type);
1466 	return &gi->group;
1467 err:
1468 	kfree(gi);
1469 	return ERR_PTR(-ENOMEM);
1470 }
1471 
1472 static void gadgets_drop(struct config_group *group, struct config_item *item)
1473 {
1474 	config_item_put(item);
1475 }
1476 
1477 static struct configfs_group_operations gadgets_ops = {
1478 	.make_group     = &gadgets_make,
1479 	.drop_item      = &gadgets_drop,
1480 };
1481 
1482 static struct config_item_type gadgets_type = {
1483 	.ct_group_ops   = &gadgets_ops,
1484 	.ct_owner       = THIS_MODULE,
1485 };
1486 
1487 static struct configfs_subsystem gadget_subsys = {
1488 	.su_group = {
1489 		.cg_item = {
1490 			.ci_namebuf = "usb_gadget",
1491 			.ci_type = &gadgets_type,
1492 		},
1493 	},
1494 	.su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1495 };
1496 
1497 void unregister_gadget_item(struct config_item *item)
1498 {
1499 	struct gadget_info *gi = to_gadget_info(item);
1500 
1501 	unregister_gadget(gi);
1502 }
1503 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1504 
1505 static int __init gadget_cfs_init(void)
1506 {
1507 	int ret;
1508 
1509 	config_group_init(&gadget_subsys.su_group);
1510 
1511 	ret = configfs_register_subsystem(&gadget_subsys);
1512 	return ret;
1513 }
1514 module_init(gadget_cfs_init);
1515 
1516 static void __exit gadget_cfs_exit(void)
1517 {
1518 	configfs_unregister_subsystem(&gadget_subsys);
1519 }
1520 module_exit(gadget_cfs_exit);
1521