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