• 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 
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 
465 	if (gi->composite.gadget_driver.udc_name) {
466 		ret = -EINVAL;
467 		goto out;
468 	}
469 
470 	list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
471 		if (a_fi == fi)
472 			break;
473 	}
474 	if (a_fi != fi) {
475 		ret = -EINVAL;
476 		goto out;
477 	}
478 
479 	list_for_each_entry(f, &cfg->func_list, list) {
480 		if (f->fi == fi) {
481 			ret = -EEXIST;
482 			goto out;
483 		}
484 	}
485 
486 	f = usb_get_function(fi);
487 	if (IS_ERR(f)) {
488 		ret = PTR_ERR(f);
489 		goto out;
490 	}
491 
492 	/* stash the function until we bind it to the gadget */
493 	list_add_tail(&f->list, &cfg->func_list);
494 	ret = 0;
495 out:
496 	mutex_unlock(&gi->lock);
497 	return ret;
498 }
499 
config_usb_cfg_unlink(struct config_item * usb_cfg_ci,struct config_item * usb_func_ci)500 static void config_usb_cfg_unlink(
501 	struct config_item *usb_cfg_ci,
502 	struct config_item *usb_func_ci)
503 {
504 	struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
505 	struct usb_composite_dev *cdev = cfg->c.cdev;
506 	struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
507 
508 	struct config_group *group = to_config_group(usb_func_ci);
509 	struct usb_function_instance *fi = container_of(group,
510 			struct usb_function_instance, group);
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 	return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
547 }
548 
gadget_config_desc_MaxPower_store(struct config_item * item,const char * page,size_t len)549 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
550 		const char *page, size_t len)
551 {
552 	u16 val;
553 	int ret;
554 	ret = kstrtou16(page, 0, &val);
555 	if (ret)
556 		return ret;
557 	if (DIV_ROUND_UP(val, 8) > 0xff)
558 		return -ERANGE;
559 	to_config_usb_cfg(item)->c.MaxPower = val;
560 	return len;
561 }
562 
gadget_config_desc_bmAttributes_show(struct config_item * item,char * page)563 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
564 		char *page)
565 {
566 	return sprintf(page, "0x%02x\n",
567 		to_config_usb_cfg(item)->c.bmAttributes);
568 }
569 
gadget_config_desc_bmAttributes_store(struct config_item * item,const char * page,size_t len)570 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
571 		const char *page, size_t len)
572 {
573 	u8 val;
574 	int ret;
575 	ret = kstrtou8(page, 0, &val);
576 	if (ret)
577 		return ret;
578 	if (!(val & USB_CONFIG_ATT_ONE))
579 		return -EINVAL;
580 	if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
581 				USB_CONFIG_ATT_WAKEUP))
582 		return -EINVAL;
583 	to_config_usb_cfg(item)->c.bmAttributes = val;
584 	return len;
585 }
586 
587 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
588 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
589 
590 static struct configfs_attribute *gadget_config_attrs[] = {
591 	&gadget_config_desc_attr_MaxPower,
592 	&gadget_config_desc_attr_bmAttributes,
593 	NULL,
594 };
595 
596 static const struct config_item_type gadget_config_type = {
597 	.ct_item_ops	= &gadget_config_item_ops,
598 	.ct_attrs	= gadget_config_attrs,
599 	.ct_owner	= THIS_MODULE,
600 };
601 
602 static const struct config_item_type gadget_root_type = {
603 	.ct_item_ops	= &gadget_root_item_ops,
604 	.ct_attrs	= gadget_root_attrs,
605 	.ct_owner	= THIS_MODULE,
606 };
607 
composite_init_dev(struct usb_composite_dev * cdev)608 static void composite_init_dev(struct usb_composite_dev *cdev)
609 {
610 	spin_lock_init(&cdev->lock);
611 	INIT_LIST_HEAD(&cdev->configs);
612 	INIT_LIST_HEAD(&cdev->gstrings);
613 }
614 
function_make(struct config_group * group,const char * name)615 static struct config_group *function_make(
616 		struct config_group *group,
617 		const char *name)
618 {
619 	struct gadget_info *gi;
620 	struct usb_function_instance *fi;
621 	char buf[MAX_NAME_LEN];
622 	char *func_name;
623 	char *instance_name;
624 	int ret;
625 
626 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
627 	if (ret >= MAX_NAME_LEN)
628 		return ERR_PTR(-ENAMETOOLONG);
629 
630 	func_name = buf;
631 	instance_name = strchr(func_name, '.');
632 	if (!instance_name) {
633 		pr_err("Unable to locate . in FUNC.INSTANCE\n");
634 		return ERR_PTR(-EINVAL);
635 	}
636 	*instance_name = '\0';
637 	instance_name++;
638 
639 	fi = usb_get_function_instance(func_name);
640 	if (IS_ERR(fi))
641 		return ERR_CAST(fi);
642 
643 	ret = config_item_set_name(&fi->group.cg_item, "%s", name);
644 	if (ret) {
645 		usb_put_function_instance(fi);
646 		return ERR_PTR(ret);
647 	}
648 	if (fi->set_inst_name) {
649 		ret = fi->set_inst_name(fi, instance_name);
650 		if (ret) {
651 			usb_put_function_instance(fi);
652 			return ERR_PTR(ret);
653 		}
654 	}
655 
656 	gi = container_of(group, struct gadget_info, functions_group);
657 
658 	mutex_lock(&gi->lock);
659 	list_add_tail(&fi->cfs_list, &gi->available_func);
660 	mutex_unlock(&gi->lock);
661 	return &fi->group;
662 }
663 
function_drop(struct config_group * group,struct config_item * item)664 static void function_drop(
665 		struct config_group *group,
666 		struct config_item *item)
667 {
668 	struct usb_function_instance *fi = to_usb_function_instance(item);
669 	struct gadget_info *gi;
670 
671 	gi = container_of(group, struct gadget_info, functions_group);
672 
673 	mutex_lock(&gi->lock);
674 	list_del(&fi->cfs_list);
675 	mutex_unlock(&gi->lock);
676 	config_item_put(item);
677 }
678 
679 static struct configfs_group_operations functions_ops = {
680 	.make_group     = &function_make,
681 	.drop_item      = &function_drop,
682 };
683 
684 static const struct config_item_type functions_type = {
685 	.ct_group_ops   = &functions_ops,
686 	.ct_owner       = THIS_MODULE,
687 };
688 
689 GS_STRINGS_RW(gadget_config_name, configuration);
690 
691 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
692 	&gadget_config_name_attr_configuration,
693 	NULL,
694 };
695 
gadget_config_name_attr_release(struct config_item * item)696 static void gadget_config_name_attr_release(struct config_item *item)
697 {
698 	struct gadget_config_name *cn = to_gadget_config_name(item);
699 
700 	kfree(cn->configuration);
701 
702 	list_del(&cn->list);
703 	kfree(cn);
704 }
705 
706 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
707 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
708 
config_desc_make(struct config_group * group,const char * name)709 static struct config_group *config_desc_make(
710 		struct config_group *group,
711 		const char *name)
712 {
713 	struct gadget_info *gi;
714 	struct config_usb_cfg *cfg;
715 	char buf[MAX_NAME_LEN];
716 	char *num_str;
717 	u8 num;
718 	int ret;
719 
720 	gi = container_of(group, struct gadget_info, configs_group);
721 	ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
722 	if (ret >= MAX_NAME_LEN)
723 		return ERR_PTR(-ENAMETOOLONG);
724 
725 	num_str = strchr(buf, '.');
726 	if (!num_str) {
727 		pr_err("Unable to locate . in name.bConfigurationValue\n");
728 		return ERR_PTR(-EINVAL);
729 	}
730 
731 	*num_str = '\0';
732 	num_str++;
733 
734 	if (!strlen(buf))
735 		return ERR_PTR(-EINVAL);
736 
737 	ret = kstrtou8(num_str, 0, &num);
738 	if (ret)
739 		return ERR_PTR(ret);
740 
741 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
742 	if (!cfg)
743 		return ERR_PTR(-ENOMEM);
744 	cfg->c.label = kstrdup(buf, GFP_KERNEL);
745 	if (!cfg->c.label) {
746 		ret = -ENOMEM;
747 		goto err;
748 	}
749 	cfg->c.bConfigurationValue = num;
750 	cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
751 	cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
752 	INIT_LIST_HEAD(&cfg->string_list);
753 	INIT_LIST_HEAD(&cfg->func_list);
754 
755 	config_group_init_type_name(&cfg->group, name,
756 				&gadget_config_type);
757 
758 	config_group_init_type_name(&cfg->strings_group, "strings",
759 			&gadget_config_name_strings_type);
760 	configfs_add_default_group(&cfg->strings_group, &cfg->group);
761 
762 	ret = usb_add_config_only(&gi->cdev, &cfg->c);
763 	if (ret)
764 		goto err;
765 
766 	return &cfg->group;
767 err:
768 	kfree(cfg->c.label);
769 	kfree(cfg);
770 	return ERR_PTR(ret);
771 }
772 
config_desc_drop(struct config_group * group,struct config_item * item)773 static void config_desc_drop(
774 		struct config_group *group,
775 		struct config_item *item)
776 {
777 	config_item_put(item);
778 }
779 
780 static struct configfs_group_operations config_desc_ops = {
781 	.make_group     = &config_desc_make,
782 	.drop_item      = &config_desc_drop,
783 };
784 
785 static const struct config_item_type config_desc_type = {
786 	.ct_group_ops   = &config_desc_ops,
787 	.ct_owner       = THIS_MODULE,
788 };
789 
790 GS_STRINGS_RW(gadget_strings, manufacturer);
791 GS_STRINGS_RW(gadget_strings, product);
792 GS_STRINGS_RW(gadget_strings, serialnumber);
793 
794 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
795 	&gadget_strings_attr_manufacturer,
796 	&gadget_strings_attr_product,
797 	&gadget_strings_attr_serialnumber,
798 	NULL,
799 };
800 
gadget_strings_attr_release(struct config_item * item)801 static void gadget_strings_attr_release(struct config_item *item)
802 {
803 	struct gadget_strings *gs = to_gadget_strings(item);
804 
805 	kfree(gs->manufacturer);
806 	kfree(gs->product);
807 	kfree(gs->serialnumber);
808 
809 	list_del(&gs->list);
810 	kfree(gs);
811 }
812 
813 USB_CONFIG_STRING_RW_OPS(gadget_strings);
814 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
815 
to_os_desc(struct config_item * item)816 static inline struct os_desc *to_os_desc(struct config_item *item)
817 {
818 	return container_of(to_config_group(item), struct os_desc, group);
819 }
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 to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
825 }
826 
os_desc_use_show(struct config_item * item,char * page)827 static ssize_t os_desc_use_show(struct config_item *item, char *page)
828 {
829 	return sprintf(page, "%d\n",
830 			os_desc_item_to_gadget_info(item)->use_os_desc);
831 }
832 
os_desc_use_store(struct config_item * item,const char * page,size_t len)833 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
834 				 size_t len)
835 {
836 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
837 	int ret;
838 	bool use;
839 
840 	mutex_lock(&gi->lock);
841 	ret = strtobool(page, &use);
842 	if (!ret) {
843 		gi->use_os_desc = use;
844 		ret = len;
845 	}
846 	mutex_unlock(&gi->lock);
847 
848 	return ret;
849 }
850 
os_desc_b_vendor_code_show(struct config_item * item,char * page)851 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
852 {
853 	return sprintf(page, "0x%02x\n",
854 			os_desc_item_to_gadget_info(item)->b_vendor_code);
855 }
856 
os_desc_b_vendor_code_store(struct config_item * item,const char * page,size_t len)857 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
858 					   const char *page, size_t len)
859 {
860 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
861 	int ret;
862 	u8 b_vendor_code;
863 
864 	mutex_lock(&gi->lock);
865 	ret = kstrtou8(page, 0, &b_vendor_code);
866 	if (!ret) {
867 		gi->b_vendor_code = b_vendor_code;
868 		ret = len;
869 	}
870 	mutex_unlock(&gi->lock);
871 
872 	return ret;
873 }
874 
os_desc_qw_sign_show(struct config_item * item,char * page)875 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
876 {
877 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
878 	int res;
879 
880 	res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
881 			      UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
882 	page[res++] = '\n';
883 
884 	return res;
885 }
886 
os_desc_qw_sign_store(struct config_item * item,const char * page,size_t len)887 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
888 				     size_t len)
889 {
890 	struct gadget_info *gi = os_desc_item_to_gadget_info(item);
891 	int res, l;
892 
893 	l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
894 	if (page[l - 1] == '\n')
895 		--l;
896 
897 	mutex_lock(&gi->lock);
898 	res = utf8s_to_utf16s(page, l,
899 			      UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
900 			      OS_STRING_QW_SIGN_LEN);
901 	if (res > 0)
902 		res = len;
903 	mutex_unlock(&gi->lock);
904 
905 	return res;
906 }
907 
908 CONFIGFS_ATTR(os_desc_, use);
909 CONFIGFS_ATTR(os_desc_, b_vendor_code);
910 CONFIGFS_ATTR(os_desc_, qw_sign);
911 
912 static struct configfs_attribute *os_desc_attrs[] = {
913 	&os_desc_attr_use,
914 	&os_desc_attr_b_vendor_code,
915 	&os_desc_attr_qw_sign,
916 	NULL,
917 };
918 
os_desc_attr_release(struct config_item * item)919 static void os_desc_attr_release(struct config_item *item)
920 {
921 	struct os_desc *os_desc = to_os_desc(item);
922 	kfree(os_desc);
923 }
924 
os_desc_link(struct config_item * os_desc_ci,struct config_item * usb_cfg_ci)925 static int os_desc_link(struct config_item *os_desc_ci,
926 			struct config_item *usb_cfg_ci)
927 {
928 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
929 					struct gadget_info, os_desc_group);
930 	struct usb_composite_dev *cdev = &gi->cdev;
931 	struct config_usb_cfg *c_target =
932 		container_of(to_config_group(usb_cfg_ci),
933 			     struct config_usb_cfg, group);
934 	struct usb_configuration *c;
935 	int ret;
936 
937 	mutex_lock(&gi->lock);
938 	list_for_each_entry(c, &cdev->configs, list) {
939 		if (c == &c_target->c)
940 			break;
941 	}
942 	if (c != &c_target->c) {
943 		ret = -EINVAL;
944 		goto out;
945 	}
946 
947 	if (cdev->os_desc_config) {
948 		ret = -EBUSY;
949 		goto out;
950 	}
951 
952 	cdev->os_desc_config = &c_target->c;
953 	ret = 0;
954 
955 out:
956 	mutex_unlock(&gi->lock);
957 	return ret;
958 }
959 
os_desc_unlink(struct config_item * os_desc_ci,struct config_item * usb_cfg_ci)960 static void os_desc_unlink(struct config_item *os_desc_ci,
961 			  struct config_item *usb_cfg_ci)
962 {
963 	struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
964 					struct gadget_info, os_desc_group);
965 	struct usb_composite_dev *cdev = &gi->cdev;
966 
967 	mutex_lock(&gi->lock);
968 	if (gi->composite.gadget_driver.udc_name)
969 		unregister_gadget(gi);
970 	cdev->os_desc_config = NULL;
971 	WARN_ON(gi->composite.gadget_driver.udc_name);
972 	mutex_unlock(&gi->lock);
973 }
974 
975 static struct configfs_item_operations os_desc_ops = {
976 	.release                = os_desc_attr_release,
977 	.allow_link		= os_desc_link,
978 	.drop_link		= os_desc_unlink,
979 };
980 
981 static struct config_item_type os_desc_type = {
982 	.ct_item_ops	= &os_desc_ops,
983 	.ct_attrs	= os_desc_attrs,
984 	.ct_owner	= THIS_MODULE,
985 };
986 
987 static inline struct usb_os_desc_ext_prop
to_usb_os_desc_ext_prop(struct config_item * item)988 *to_usb_os_desc_ext_prop(struct config_item *item)
989 {
990 	return container_of(item, struct usb_os_desc_ext_prop, item);
991 }
992 
ext_prop_type_show(struct config_item * item,char * page)993 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
994 {
995 	return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
996 }
997 
ext_prop_type_store(struct config_item * item,const char * page,size_t len)998 static ssize_t ext_prop_type_store(struct config_item *item,
999 				   const char *page, size_t len)
1000 {
1001 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1002 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1003 	u8 type;
1004 	int ret;
1005 
1006 	if (desc->opts_mutex)
1007 		mutex_lock(desc->opts_mutex);
1008 	ret = kstrtou8(page, 0, &type);
1009 	if (ret)
1010 		goto end;
1011 	if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
1012 		ret = -EINVAL;
1013 		goto end;
1014 	}
1015 
1016 	if ((ext_prop->type == USB_EXT_PROP_BINARY ||
1017 	    ext_prop->type == USB_EXT_PROP_LE32 ||
1018 	    ext_prop->type == USB_EXT_PROP_BE32) &&
1019 	    (type == USB_EXT_PROP_UNICODE ||
1020 	    type == USB_EXT_PROP_UNICODE_ENV ||
1021 	    type == USB_EXT_PROP_UNICODE_LINK))
1022 		ext_prop->data_len <<= 1;
1023 	else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
1024 		   ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1025 		   ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
1026 		   (type == USB_EXT_PROP_BINARY ||
1027 		   type == USB_EXT_PROP_LE32 ||
1028 		   type == USB_EXT_PROP_BE32))
1029 		ext_prop->data_len >>= 1;
1030 	ext_prop->type = type;
1031 	ret = len;
1032 
1033 end:
1034 	if (desc->opts_mutex)
1035 		mutex_unlock(desc->opts_mutex);
1036 	return ret;
1037 }
1038 
ext_prop_data_show(struct config_item * item,char * page)1039 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
1040 {
1041 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1042 	int len = ext_prop->data_len;
1043 
1044 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1045 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1046 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1047 		len >>= 1;
1048 	memcpy(page, ext_prop->data, len);
1049 
1050 	return len;
1051 }
1052 
ext_prop_data_store(struct config_item * item,const char * page,size_t len)1053 static ssize_t ext_prop_data_store(struct config_item *item,
1054 				   const char *page, size_t len)
1055 {
1056 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1057 	struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1058 	char *new_data;
1059 	size_t ret_len = len;
1060 
1061 	if (page[len - 1] == '\n' || page[len - 1] == '\0')
1062 		--len;
1063 	new_data = kmemdup(page, len, GFP_KERNEL);
1064 	if (!new_data)
1065 		return -ENOMEM;
1066 
1067 	if (desc->opts_mutex)
1068 		mutex_lock(desc->opts_mutex);
1069 	kfree(ext_prop->data);
1070 	ext_prop->data = new_data;
1071 	desc->ext_prop_len -= ext_prop->data_len;
1072 	ext_prop->data_len = len;
1073 	desc->ext_prop_len += ext_prop->data_len;
1074 	if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1075 	    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1076 	    ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1077 		desc->ext_prop_len -= ext_prop->data_len;
1078 		ext_prop->data_len <<= 1;
1079 		ext_prop->data_len += 2;
1080 		desc->ext_prop_len += ext_prop->data_len;
1081 	}
1082 	if (desc->opts_mutex)
1083 		mutex_unlock(desc->opts_mutex);
1084 	return ret_len;
1085 }
1086 
1087 CONFIGFS_ATTR(ext_prop_, type);
1088 CONFIGFS_ATTR(ext_prop_, data);
1089 
1090 static struct configfs_attribute *ext_prop_attrs[] = {
1091 	&ext_prop_attr_type,
1092 	&ext_prop_attr_data,
1093 	NULL,
1094 };
1095 
usb_os_desc_ext_prop_release(struct config_item * item)1096 static void usb_os_desc_ext_prop_release(struct config_item *item)
1097 {
1098 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1099 
1100 	kfree(ext_prop); /* frees a whole chunk */
1101 }
1102 
1103 static struct configfs_item_operations ext_prop_ops = {
1104 	.release		= usb_os_desc_ext_prop_release,
1105 };
1106 
ext_prop_make(struct config_group * group,const char * name)1107 static struct config_item *ext_prop_make(
1108 		struct config_group *group,
1109 		const char *name)
1110 {
1111 	struct usb_os_desc_ext_prop *ext_prop;
1112 	struct config_item_type *ext_prop_type;
1113 	struct usb_os_desc *desc;
1114 	char *vlabuf;
1115 
1116 	vla_group(data_chunk);
1117 	vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1118 	vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1119 
1120 	vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1121 	if (!vlabuf)
1122 		return ERR_PTR(-ENOMEM);
1123 
1124 	ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1125 	ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1126 
1127 	desc = container_of(group, struct usb_os_desc, group);
1128 	ext_prop_type->ct_item_ops = &ext_prop_ops;
1129 	ext_prop_type->ct_attrs = ext_prop_attrs;
1130 	ext_prop_type->ct_owner = desc->owner;
1131 
1132 	config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1133 
1134 	ext_prop->name = kstrdup(name, GFP_KERNEL);
1135 	if (!ext_prop->name) {
1136 		kfree(vlabuf);
1137 		return ERR_PTR(-ENOMEM);
1138 	}
1139 	desc->ext_prop_len += 14;
1140 	ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1141 	if (desc->opts_mutex)
1142 		mutex_lock(desc->opts_mutex);
1143 	desc->ext_prop_len += ext_prop->name_len;
1144 	list_add_tail(&ext_prop->entry, &desc->ext_prop);
1145 	++desc->ext_prop_count;
1146 	if (desc->opts_mutex)
1147 		mutex_unlock(desc->opts_mutex);
1148 
1149 	return &ext_prop->item;
1150 }
1151 
ext_prop_drop(struct config_group * group,struct config_item * item)1152 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1153 {
1154 	struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1155 	struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1156 
1157 	if (desc->opts_mutex)
1158 		mutex_lock(desc->opts_mutex);
1159 	list_del(&ext_prop->entry);
1160 	--desc->ext_prop_count;
1161 	kfree(ext_prop->name);
1162 	desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1163 	if (desc->opts_mutex)
1164 		mutex_unlock(desc->opts_mutex);
1165 	config_item_put(item);
1166 }
1167 
1168 static struct configfs_group_operations interf_grp_ops = {
1169 	.make_item	= &ext_prop_make,
1170 	.drop_item	= &ext_prop_drop,
1171 };
1172 
interf_grp_compatible_id_show(struct config_item * item,char * page)1173 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1174 					     char *page)
1175 {
1176 	memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1177 	return 8;
1178 }
1179 
interf_grp_compatible_id_store(struct config_item * item,const char * page,size_t len)1180 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1181 					      const char *page, size_t len)
1182 {
1183 	struct usb_os_desc *desc = to_usb_os_desc(item);
1184 	int l;
1185 
1186 	l = min_t(int, 8, len);
1187 	if (page[l - 1] == '\n')
1188 		--l;
1189 	if (desc->opts_mutex)
1190 		mutex_lock(desc->opts_mutex);
1191 	memcpy(desc->ext_compat_id, page, l);
1192 
1193 	if (desc->opts_mutex)
1194 		mutex_unlock(desc->opts_mutex);
1195 
1196 	return len;
1197 }
1198 
interf_grp_sub_compatible_id_show(struct config_item * item,char * page)1199 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1200 						 char *page)
1201 {
1202 	memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1203 	return 8;
1204 }
1205 
interf_grp_sub_compatible_id_store(struct config_item * item,const char * page,size_t len)1206 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1207 						  const char *page, size_t len)
1208 {
1209 	struct usb_os_desc *desc = to_usb_os_desc(item);
1210 	int l;
1211 
1212 	l = min_t(int, 8, len);
1213 	if (page[l - 1] == '\n')
1214 		--l;
1215 	if (desc->opts_mutex)
1216 		mutex_lock(desc->opts_mutex);
1217 	memcpy(desc->ext_compat_id + 8, page, l);
1218 
1219 	if (desc->opts_mutex)
1220 		mutex_unlock(desc->opts_mutex);
1221 
1222 	return len;
1223 }
1224 
1225 CONFIGFS_ATTR(interf_grp_, compatible_id);
1226 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1227 
1228 static struct configfs_attribute *interf_grp_attrs[] = {
1229 	&interf_grp_attr_compatible_id,
1230 	&interf_grp_attr_sub_compatible_id,
1231 	NULL
1232 };
1233 
usb_os_desc_prepare_interf_dir(struct config_group * parent,int n_interf,struct usb_os_desc ** desc,char ** names,struct module * owner)1234 struct config_group *usb_os_desc_prepare_interf_dir(
1235 		struct config_group *parent,
1236 		int n_interf,
1237 		struct usb_os_desc **desc,
1238 		char **names,
1239 		struct module *owner)
1240 {
1241 	struct config_group *os_desc_group;
1242 	struct config_item_type *os_desc_type, *interface_type;
1243 
1244 	vla_group(data_chunk);
1245 	vla_item(data_chunk, struct config_group, os_desc_group, 1);
1246 	vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1247 	vla_item(data_chunk, struct config_item_type, interface_type, 1);
1248 
1249 	char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1250 	if (!vlabuf)
1251 		return ERR_PTR(-ENOMEM);
1252 
1253 	os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1254 	os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1255 	interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1256 
1257 	os_desc_type->ct_owner = owner;
1258 	config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1259 	configfs_add_default_group(os_desc_group, parent);
1260 
1261 	interface_type->ct_group_ops = &interf_grp_ops;
1262 	interface_type->ct_attrs = interf_grp_attrs;
1263 	interface_type->ct_owner = owner;
1264 
1265 	while (n_interf--) {
1266 		struct usb_os_desc *d;
1267 
1268 		d = desc[n_interf];
1269 		d->owner = owner;
1270 		config_group_init_type_name(&d->group, "", interface_type);
1271 		config_item_set_name(&d->group.cg_item, "interface.%s",
1272 				     names[n_interf]);
1273 		configfs_add_default_group(&d->group, os_desc_group);
1274 	}
1275 
1276 	return os_desc_group;
1277 }
1278 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1279 
configfs_do_nothing(struct usb_composite_dev * cdev)1280 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1281 {
1282 	WARN_ON(1);
1283 	return -EINVAL;
1284 }
1285 
1286 int composite_dev_prepare(struct usb_composite_driver *composite,
1287 		struct usb_composite_dev *dev);
1288 
1289 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1290 				  struct usb_ep *ep0);
1291 
purge_configs_funcs(struct gadget_info * gi)1292 static void purge_configs_funcs(struct gadget_info *gi)
1293 {
1294 	struct usb_configuration	*c;
1295 
1296 	list_for_each_entry(c, &gi->cdev.configs, list) {
1297 		struct usb_function *f, *tmp;
1298 		struct config_usb_cfg *cfg;
1299 
1300 		cfg = container_of(c, struct config_usb_cfg, c);
1301 
1302 		list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1303 
1304 			list_move(&f->list, &cfg->func_list);
1305 			if (f->unbind) {
1306 				dev_dbg(&gi->cdev.gadget->dev,
1307 					"unbind function '%s'/%p\n",
1308 					f->name, f);
1309 				f->unbind(c, f);
1310 			}
1311 		}
1312 		c->next_interface_id = 0;
1313 		memset(c->interface, 0, sizeof(c->interface));
1314 		c->superspeed_plus = 0;
1315 		c->superspeed = 0;
1316 		c->highspeed = 0;
1317 		c->fullspeed = 0;
1318 	}
1319 }
1320 
configfs_composite_bind(struct usb_gadget * gadget,struct usb_gadget_driver * gdriver)1321 static int configfs_composite_bind(struct usb_gadget *gadget,
1322 		struct usb_gadget_driver *gdriver)
1323 {
1324 	struct usb_composite_driver     *composite = to_cdriver(gdriver);
1325 	struct gadget_info		*gi = container_of(composite,
1326 						struct gadget_info, composite);
1327 	struct usb_composite_dev	*cdev = &gi->cdev;
1328 	struct usb_configuration	*c;
1329 	struct usb_string		*s;
1330 	unsigned			i;
1331 	int				ret;
1332 
1333 	/* the gi->lock is hold by the caller */
1334 	gi->unbind = 0;
1335 	cdev->gadget = gadget;
1336 	set_gadget_data(gadget, cdev);
1337 	ret = composite_dev_prepare(composite, cdev);
1338 	if (ret)
1339 		return ret;
1340 	/* and now the gadget bind */
1341 	ret = -EINVAL;
1342 
1343 	if (list_empty(&gi->cdev.configs)) {
1344 		pr_err("Need at least one configuration in %s.\n",
1345 				gi->composite.name);
1346 		goto err_comp_cleanup;
1347 	}
1348 
1349 
1350 	list_for_each_entry(c, &gi->cdev.configs, list) {
1351 		struct config_usb_cfg *cfg;
1352 
1353 		cfg = container_of(c, struct config_usb_cfg, c);
1354 		if (list_empty(&cfg->func_list)) {
1355 			pr_err("Config %s/%d of %s needs at least one function.\n",
1356 			      c->label, c->bConfigurationValue,
1357 			      gi->composite.name);
1358 			goto err_comp_cleanup;
1359 		}
1360 	}
1361 
1362 	/* init all strings */
1363 	if (!list_empty(&gi->string_list)) {
1364 		struct gadget_strings *gs;
1365 
1366 		i = 0;
1367 		list_for_each_entry(gs, &gi->string_list, list) {
1368 
1369 			gi->gstrings[i] = &gs->stringtab_dev;
1370 			gs->stringtab_dev.strings = gs->strings;
1371 			gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1372 				gs->manufacturer;
1373 			gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1374 			gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1375 			i++;
1376 		}
1377 		gi->gstrings[i] = NULL;
1378 		s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1379 				USB_GADGET_FIRST_AVAIL_IDX);
1380 		if (IS_ERR(s)) {
1381 			ret = PTR_ERR(s);
1382 			goto err_comp_cleanup;
1383 		}
1384 
1385 		gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1386 		gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1387 		gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1388 	}
1389 
1390 	if (gi->use_os_desc) {
1391 		cdev->use_os_string = true;
1392 		cdev->b_vendor_code = gi->b_vendor_code;
1393 		memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1394 	}
1395 
1396 	if (gadget_is_otg(gadget) && !otg_desc[0]) {
1397 		struct usb_descriptor_header *usb_desc;
1398 
1399 		usb_desc = usb_otg_descriptor_alloc(gadget);
1400 		if (!usb_desc) {
1401 			ret = -ENOMEM;
1402 			goto err_comp_cleanup;
1403 		}
1404 		usb_otg_descriptor_init(gadget, usb_desc);
1405 		otg_desc[0] = usb_desc;
1406 		otg_desc[1] = NULL;
1407 	}
1408 
1409 	/* Go through all configs, attach all functions */
1410 	list_for_each_entry(c, &gi->cdev.configs, list) {
1411 		struct config_usb_cfg *cfg;
1412 		struct usb_function *f;
1413 		struct usb_function *tmp;
1414 		struct gadget_config_name *cn;
1415 
1416 		if (gadget_is_otg(gadget))
1417 			c->descriptors = otg_desc;
1418 
1419 		cfg = container_of(c, struct config_usb_cfg, c);
1420 		if (!list_empty(&cfg->string_list)) {
1421 			i = 0;
1422 			list_for_each_entry(cn, &cfg->string_list, list) {
1423 				cfg->gstrings[i] = &cn->stringtab_dev;
1424 				cn->stringtab_dev.strings = &cn->strings;
1425 				cn->strings.s = cn->configuration;
1426 				i++;
1427 			}
1428 			cfg->gstrings[i] = NULL;
1429 			s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1430 			if (IS_ERR(s)) {
1431 				ret = PTR_ERR(s);
1432 				goto err_comp_cleanup;
1433 			}
1434 			c->iConfiguration = s[0].id;
1435 		}
1436 
1437 		list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1438 			list_del(&f->list);
1439 			ret = usb_add_function(c, f);
1440 			if (ret) {
1441 				list_add(&f->list, &cfg->func_list);
1442 				goto err_purge_funcs;
1443 			}
1444 		}
1445 		ret = usb_gadget_check_config(cdev->gadget);
1446 		if (ret)
1447 			goto err_purge_funcs;
1448 
1449 		usb_ep_autoconfig_reset(cdev->gadget);
1450 	}
1451 	if (cdev->use_os_string) {
1452 		ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1453 		if (ret)
1454 			goto err_purge_funcs;
1455 	}
1456 
1457 	usb_ep_autoconfig_reset(cdev->gadget);
1458 	return 0;
1459 
1460 err_purge_funcs:
1461 	purge_configs_funcs(gi);
1462 err_comp_cleanup:
1463 	composite_dev_cleanup(cdev);
1464 	return ret;
1465 }
1466 
1467 #ifdef CONFIG_USB_CONFIGFS_UEVENT
android_work(struct work_struct * data)1468 static void android_work(struct work_struct *data)
1469 {
1470 	struct gadget_info *gi = container_of(data, struct gadget_info, work);
1471 	struct usb_composite_dev *cdev = &gi->cdev;
1472 	char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
1473 	char *connected[2]    = { "USB_STATE=CONNECTED", NULL };
1474 	char *configured[2]   = { "USB_STATE=CONFIGURED", NULL };
1475 	/* 0-connected 1-configured 2-disconnected*/
1476 	bool status[3] = { false, false, false };
1477 	unsigned long flags;
1478 	bool uevent_sent = false;
1479 
1480 	spin_lock_irqsave(&cdev->lock, flags);
1481 	if (cdev->config)
1482 		status[1] = true;
1483 
1484 	if (gi->connected != gi->sw_connected) {
1485 		if (gi->connected)
1486 			status[0] = true;
1487 		else
1488 			status[2] = true;
1489 		gi->sw_connected = gi->connected;
1490 	}
1491 	spin_unlock_irqrestore(&cdev->lock, flags);
1492 
1493 	if (status[0]) {
1494 		kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, connected);
1495 		pr_info("%s: sent uevent %s\n", __func__, connected[0]);
1496 		uevent_sent = true;
1497 	}
1498 
1499 	if (status[1]) {
1500 		kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, configured);
1501 		pr_info("%s: sent uevent %s\n", __func__, configured[0]);
1502 		uevent_sent = true;
1503 	}
1504 
1505 	if (status[2]) {
1506 		kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, disconnected);
1507 		pr_info("%s: sent uevent %s\n", __func__, disconnected[0]);
1508 		uevent_sent = true;
1509 	}
1510 
1511 	if (!uevent_sent) {
1512 		pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
1513 			gi->connected, gi->sw_connected, cdev->config);
1514 	}
1515 }
1516 #endif
1517 
configfs_composite_unbind(struct usb_gadget * gadget)1518 static void configfs_composite_unbind(struct usb_gadget *gadget)
1519 {
1520 	struct usb_composite_dev	*cdev;
1521 	struct gadget_info		*gi;
1522 	unsigned long flags;
1523 
1524 	/* the gi->lock is hold by the caller */
1525 
1526 	cdev = get_gadget_data(gadget);
1527 	gi = container_of(cdev, struct gadget_info, cdev);
1528 	spin_lock_irqsave(&gi->spinlock, flags);
1529 	gi->unbind = 1;
1530 	spin_unlock_irqrestore(&gi->spinlock, flags);
1531 
1532 	kfree(otg_desc[0]);
1533 	otg_desc[0] = NULL;
1534 	purge_configs_funcs(gi);
1535 	composite_dev_cleanup(cdev);
1536 	usb_ep_autoconfig_reset(cdev->gadget);
1537 	spin_lock_irqsave(&gi->spinlock, flags);
1538 	cdev->gadget = NULL;
1539 	cdev->deactivations = 0;
1540 	gadget->deactivated = false;
1541 	set_gadget_data(gadget, NULL);
1542 	spin_unlock_irqrestore(&gi->spinlock, flags);
1543 }
1544 
1545 #ifdef CONFIG_USB_CONFIGFS_UEVENT
android_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * c)1546 static int android_setup(struct usb_gadget *gadget,
1547 			const struct usb_ctrlrequest *c)
1548 {
1549 	struct usb_composite_dev *cdev;
1550 	unsigned long flags;
1551 	struct gadget_info *gi;
1552 	int value = -EOPNOTSUPP;
1553 	struct usb_function_instance *fi;
1554 
1555 	if (!android_device)
1556 		return 0;
1557 
1558 	gi = dev_get_drvdata(android_device);
1559 	spin_lock_irqsave(&gi->spinlock, flags);
1560 	cdev = get_gadget_data(gadget);
1561 	if (!cdev || gi->unbind) {
1562 		spin_unlock_irqrestore(&gi->spinlock, flags);
1563 		return 0;
1564 	}
1565 
1566 	if (!gi->connected) {
1567 		gi->connected = 1;
1568 		schedule_work(&gi->work);
1569 	}
1570 
1571 	list_for_each_entry(fi, &gi->available_func, cfs_list) {
1572 		if (fi != NULL && fi->f != NULL && fi->f->setup != NULL) {
1573 			value = fi->f->setup(fi->f, c);
1574 			if (value >= 0)
1575 				break;
1576 		}
1577 	}
1578 
1579 #ifdef CONFIG_USB_CONFIGFS_F_ACC
1580 	if (value < 0)
1581 		value = acc_ctrlrequest_composite(cdev, c);
1582 #endif
1583 
1584 	if (value < 0)
1585 		value = composite_setup(gadget, c);
1586 
1587 	if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1588 						cdev->config) {
1589 		schedule_work(&gi->work);
1590 	}
1591 	spin_unlock_irqrestore(&gi->spinlock, flags);
1592 
1593 	return value;
1594 }
1595 
1596 #else // CONFIG_USB_CONFIGFS_UEVENT
1597 
configfs_composite_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)1598 static int configfs_composite_setup(struct usb_gadget *gadget,
1599 		const struct usb_ctrlrequest *ctrl)
1600 {
1601 	struct usb_composite_dev *cdev;
1602 	struct gadget_info *gi;
1603 	unsigned long flags;
1604 	int ret;
1605 
1606 	cdev = get_gadget_data(gadget);
1607 	if (!cdev)
1608 		return 0;
1609 
1610 	gi = container_of(cdev, struct gadget_info, cdev);
1611 	spin_lock_irqsave(&gi->spinlock, flags);
1612 	cdev = get_gadget_data(gadget);
1613 	if (!cdev || gi->unbind) {
1614 		spin_unlock_irqrestore(&gi->spinlock, flags);
1615 		return 0;
1616 	}
1617 
1618 	ret = composite_setup(gadget, ctrl);
1619 	spin_unlock_irqrestore(&gi->spinlock, flags);
1620 	return ret;
1621 }
1622 
1623 #endif // CONFIG_USB_CONFIGFS_UEVENT
1624 
configfs_composite_disconnect(struct usb_gadget * gadget)1625 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1626 {
1627 	struct usb_composite_dev *cdev;
1628 	struct gadget_info *gi;
1629 	unsigned long flags;
1630 
1631 	cdev = get_gadget_data(gadget);
1632 	if (!cdev)
1633 		return;
1634 
1635 #ifdef CONFIG_USB_CONFIGFS_F_ACC
1636 	/*
1637 	 * accessory HID support can be active while the
1638 	 * accessory function is not actually enabled,
1639 	 * so we need to inform it when we are disconnected.
1640 	 */
1641 	acc_disconnect();
1642 #endif
1643 	gi = container_of(cdev, struct gadget_info, cdev);
1644 	spin_lock_irqsave(&gi->spinlock, flags);
1645 	cdev = get_gadget_data(gadget);
1646 	if (!cdev || gi->unbind) {
1647 		spin_unlock_irqrestore(&gi->spinlock, flags);
1648 		return;
1649 	}
1650 
1651 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1652 	gi->connected = 0;
1653 	schedule_work(&gi->work);
1654 #endif
1655 	composite_disconnect(gadget);
1656 	spin_unlock_irqrestore(&gi->spinlock, flags);
1657 }
1658 
configfs_composite_reset(struct usb_gadget * gadget)1659 static void configfs_composite_reset(struct usb_gadget *gadget)
1660 {
1661 	struct usb_composite_dev *cdev;
1662 	struct gadget_info *gi;
1663 	unsigned long flags;
1664 
1665 	cdev = get_gadget_data(gadget);
1666 	if (!cdev)
1667 		return;
1668 
1669 	gi = container_of(cdev, struct gadget_info, cdev);
1670 	spin_lock_irqsave(&gi->spinlock, flags);
1671 	cdev = get_gadget_data(gadget);
1672 	if (!cdev || gi->unbind) {
1673 		spin_unlock_irqrestore(&gi->spinlock, flags);
1674 		return;
1675 	}
1676 
1677 	composite_reset(gadget);
1678 	spin_unlock_irqrestore(&gi->spinlock, flags);
1679 }
1680 
configfs_composite_suspend(struct usb_gadget * gadget)1681 static void configfs_composite_suspend(struct usb_gadget *gadget)
1682 {
1683 	struct usb_composite_dev *cdev;
1684 	struct gadget_info *gi;
1685 	unsigned long flags;
1686 
1687 	cdev = get_gadget_data(gadget);
1688 	if (!cdev)
1689 		return;
1690 
1691 	gi = container_of(cdev, struct gadget_info, cdev);
1692 	spin_lock_irqsave(&gi->spinlock, flags);
1693 	cdev = get_gadget_data(gadget);
1694 	if (!cdev || gi->unbind) {
1695 		spin_unlock_irqrestore(&gi->spinlock, flags);
1696 		return;
1697 	}
1698 
1699 	composite_suspend(gadget);
1700 	spin_unlock_irqrestore(&gi->spinlock, flags);
1701 }
1702 
configfs_composite_resume(struct usb_gadget * gadget)1703 static void configfs_composite_resume(struct usb_gadget *gadget)
1704 {
1705 	struct usb_composite_dev *cdev;
1706 	struct gadget_info *gi;
1707 	unsigned long flags;
1708 
1709 	cdev = get_gadget_data(gadget);
1710 	if (!cdev)
1711 		return;
1712 
1713 	gi = container_of(cdev, struct gadget_info, cdev);
1714 	spin_lock_irqsave(&gi->spinlock, flags);
1715 	cdev = get_gadget_data(gadget);
1716 	if (!cdev || gi->unbind) {
1717 		spin_unlock_irqrestore(&gi->spinlock, flags);
1718 		return;
1719 	}
1720 
1721 	composite_resume(gadget);
1722 	spin_unlock_irqrestore(&gi->spinlock, flags);
1723 }
1724 
1725 static const struct usb_gadget_driver configfs_driver_template = {
1726 	.bind           = configfs_composite_bind,
1727 	.unbind         = configfs_composite_unbind,
1728 
1729 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1730 	.setup          = android_setup,
1731 #else
1732 	.setup          = configfs_composite_setup,
1733 #endif
1734 	.reset          = configfs_composite_reset,
1735 	.disconnect     = configfs_composite_disconnect,
1736 	.suspend	= configfs_composite_suspend,
1737 	.resume		= configfs_composite_resume,
1738 
1739 	.max_speed	= USB_SPEED_SUPER_PLUS,
1740 	.driver = {
1741 		.owner          = THIS_MODULE,
1742 		.name		= "configfs-gadget",
1743 	},
1744 	.match_existing_only = 1,
1745 };
1746 
1747 #ifdef CONFIG_USB_CONFIGFS_UEVENT
state_show(struct device * pdev,struct device_attribute * attr,char * buf)1748 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1749 			char *buf)
1750 {
1751 	struct gadget_info *dev = dev_get_drvdata(pdev);
1752 	struct usb_composite_dev *cdev;
1753 	char *state = "DISCONNECTED";
1754 	unsigned long flags;
1755 
1756 	if (!dev)
1757 		goto out;
1758 
1759 	cdev = &dev->cdev;
1760 
1761 	if (!cdev)
1762 		goto out;
1763 
1764 	spin_lock_irqsave(&cdev->lock, flags);
1765 	if (cdev->config)
1766 		state = "CONFIGURED";
1767 	else if (dev->connected)
1768 		state = "CONNECTED";
1769 	spin_unlock_irqrestore(&cdev->lock, flags);
1770 out:
1771 	return sprintf(buf, "%s\n", state);
1772 }
1773 
1774 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1775 
1776 static struct device_attribute *android_usb_attributes[] = {
1777 	&dev_attr_state,
1778 	NULL
1779 };
1780 
android_device_create(struct gadget_info * gi)1781 static int android_device_create(struct gadget_info *gi)
1782 {
1783 	struct device_attribute **attrs;
1784 	struct device_attribute *attr;
1785 
1786 	INIT_WORK(&gi->work, android_work);
1787 	gi->dev = device_create(android_class, NULL,
1788 			MKDEV(0, 0), NULL, "android%d", gadget_index++);
1789 	if (IS_ERR(gi->dev))
1790 		return PTR_ERR(gi->dev);
1791 
1792 	dev_set_drvdata(gi->dev, gi);
1793 	if (!android_device)
1794 		android_device = gi->dev;
1795 
1796 	attrs = android_usb_attributes;
1797 	while ((attr = *attrs++)) {
1798 		int err;
1799 
1800 		err = device_create_file(gi->dev, attr);
1801 		if (err) {
1802 			device_destroy(gi->dev->class,
1803 				       gi->dev->devt);
1804 			return err;
1805 		}
1806 	}
1807 
1808 	return 0;
1809 }
1810 
android_device_destroy(struct gadget_info * gi)1811 static void android_device_destroy(struct gadget_info *gi)
1812 {
1813 	struct device_attribute **attrs;
1814 	struct device_attribute *attr;
1815 
1816 	attrs = android_usb_attributes;
1817 	while ((attr = *attrs++))
1818 		device_remove_file(gi->dev, attr);
1819 	device_destroy(gi->dev->class, gi->dev->devt);
1820 }
1821 #else
android_device_create(struct gadget_info * gi)1822 static inline int android_device_create(struct gadget_info *gi)
1823 {
1824 	return 0;
1825 }
1826 
android_device_destroy(struct gadget_info * gi)1827 static inline void android_device_destroy(struct gadget_info *gi)
1828 {
1829 }
1830 #endif
1831 
gadgets_make(struct config_group * group,const char * name)1832 static struct config_group *gadgets_make(
1833 		struct config_group *group,
1834 		const char *name)
1835 {
1836 	struct gadget_info *gi;
1837 
1838 	gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1839 	if (!gi)
1840 		return ERR_PTR(-ENOMEM);
1841 
1842 	config_group_init_type_name(&gi->group, name, &gadget_root_type);
1843 
1844 	config_group_init_type_name(&gi->functions_group, "functions",
1845 			&functions_type);
1846 	configfs_add_default_group(&gi->functions_group, &gi->group);
1847 
1848 	config_group_init_type_name(&gi->configs_group, "configs",
1849 			&config_desc_type);
1850 	configfs_add_default_group(&gi->configs_group, &gi->group);
1851 
1852 	config_group_init_type_name(&gi->strings_group, "strings",
1853 			&gadget_strings_strings_type);
1854 	configfs_add_default_group(&gi->strings_group, &gi->group);
1855 
1856 	config_group_init_type_name(&gi->os_desc_group, "os_desc",
1857 			&os_desc_type);
1858 	configfs_add_default_group(&gi->os_desc_group, &gi->group);
1859 
1860 	gi->composite.bind = configfs_do_nothing;
1861 	gi->composite.unbind = configfs_do_nothing;
1862 	gi->composite.suspend = NULL;
1863 	gi->composite.resume = NULL;
1864 	gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1865 
1866 	spin_lock_init(&gi->spinlock);
1867 	mutex_init(&gi->lock);
1868 	INIT_LIST_HEAD(&gi->string_list);
1869 	INIT_LIST_HEAD(&gi->available_func);
1870 
1871 	composite_init_dev(&gi->cdev);
1872 	gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1873 	gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1874 	gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1875 
1876 	gi->composite.gadget_driver = configfs_driver_template;
1877 
1878 	gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1879 	gi->composite.name = gi->composite.gadget_driver.function;
1880 
1881 	if (!gi->composite.gadget_driver.function)
1882 		goto err;
1883 
1884 	if (android_device_create(gi) < 0)
1885 		goto err;
1886 
1887 	return &gi->group;
1888 
1889 err:
1890 	kfree(gi);
1891 	return ERR_PTR(-ENOMEM);
1892 }
1893 
gadgets_drop(struct config_group * group,struct config_item * item)1894 static void gadgets_drop(struct config_group *group, struct config_item *item)
1895 {
1896 	struct gadget_info *gi;
1897 
1898 	gi = container_of(to_config_group(item), struct gadget_info, group);
1899 	config_item_put(item);
1900 	android_device_destroy(gi);
1901 }
1902 
1903 static struct configfs_group_operations gadgets_ops = {
1904 	.make_group     = &gadgets_make,
1905 	.drop_item      = &gadgets_drop,
1906 };
1907 
1908 static const struct config_item_type gadgets_type = {
1909 	.ct_group_ops   = &gadgets_ops,
1910 	.ct_owner       = THIS_MODULE,
1911 };
1912 
1913 static struct configfs_subsystem gadget_subsys = {
1914 	.su_group = {
1915 		.cg_item = {
1916 			.ci_namebuf = "usb_gadget",
1917 			.ci_type = &gadgets_type,
1918 		},
1919 	},
1920 	.su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1921 };
1922 
unregister_gadget_item(struct config_item * item)1923 void unregister_gadget_item(struct config_item *item)
1924 {
1925 	struct gadget_info *gi = to_gadget_info(item);
1926 
1927 	mutex_lock(&gi->lock);
1928 	unregister_gadget(gi);
1929 	mutex_unlock(&gi->lock);
1930 }
1931 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1932 
gadget_cfs_init(void)1933 static int __init gadget_cfs_init(void)
1934 {
1935 	int ret;
1936 
1937 	config_group_init(&gadget_subsys.su_group);
1938 
1939 	ret = configfs_register_subsystem(&gadget_subsys);
1940 
1941 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1942 	android_class = class_create(THIS_MODULE, "android_usb");
1943 	if (IS_ERR(android_class))
1944 		return PTR_ERR(android_class);
1945 #endif
1946 
1947 	return ret;
1948 }
1949 module_init(gadget_cfs_init);
1950 
gadget_cfs_exit(void)1951 static void __exit gadget_cfs_exit(void)
1952 {
1953 	configfs_unregister_subsystem(&gadget_subsys);
1954 #ifdef CONFIG_USB_CONFIGFS_UEVENT
1955 	if (!IS_ERR(android_class))
1956 		class_destroy(android_class);
1957 #endif
1958 
1959 }
1960 module_exit(gadget_cfs_exit);
1961