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