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