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