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