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