[go: nahoru, domu]

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