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