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