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