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