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