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