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