GNU Linux-libre 4.9.309-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 int 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 0;
460                 }
461         }
462         mutex_unlock(&gi->lock);
463         WARN(1, "Unable to locate function to unbind\n");
464         return 0;
465 }
466
467 static struct configfs_item_operations gadget_config_item_ops = {
468         .release                = gadget_config_attr_release,
469         .allow_link             = config_usb_cfg_link,
470         .drop_link              = config_usb_cfg_unlink,
471 };
472
473
474 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
475                 char *page)
476 {
477         return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
478 }
479
480 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
481                 const char *page, size_t len)
482 {
483         u16 val;
484         int ret;
485         ret = kstrtou16(page, 0, &val);
486         if (ret)
487                 return ret;
488         if (DIV_ROUND_UP(val, 8) > 0xff)
489                 return -ERANGE;
490         to_config_usb_cfg(item)->c.MaxPower = val;
491         return len;
492 }
493
494 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
495                 char *page)
496 {
497         return sprintf(page, "0x%02x\n",
498                 to_config_usb_cfg(item)->c.bmAttributes);
499 }
500
501 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
502                 const char *page, size_t len)
503 {
504         u8 val;
505         int ret;
506         ret = kstrtou8(page, 0, &val);
507         if (ret)
508                 return ret;
509         if (!(val & USB_CONFIG_ATT_ONE))
510                 return -EINVAL;
511         if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
512                                 USB_CONFIG_ATT_WAKEUP))
513                 return -EINVAL;
514         to_config_usb_cfg(item)->c.bmAttributes = val;
515         return len;
516 }
517
518 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
519 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
520
521 static struct configfs_attribute *gadget_config_attrs[] = {
522         &gadget_config_desc_attr_MaxPower,
523         &gadget_config_desc_attr_bmAttributes,
524         NULL,
525 };
526
527 static struct config_item_type gadget_config_type = {
528         .ct_item_ops    = &gadget_config_item_ops,
529         .ct_attrs       = gadget_config_attrs,
530         .ct_owner       = THIS_MODULE,
531 };
532
533 static struct config_item_type gadget_root_type = {
534         .ct_item_ops    = &gadget_root_item_ops,
535         .ct_attrs       = gadget_root_attrs,
536         .ct_owner       = THIS_MODULE,
537 };
538
539 static void composite_init_dev(struct usb_composite_dev *cdev)
540 {
541         spin_lock_init(&cdev->lock);
542         INIT_LIST_HEAD(&cdev->configs);
543         INIT_LIST_HEAD(&cdev->gstrings);
544 }
545
546 static struct config_group *function_make(
547                 struct config_group *group,
548                 const char *name)
549 {
550         struct gadget_info *gi;
551         struct usb_function_instance *fi;
552         char buf[MAX_NAME_LEN];
553         char *func_name;
554         char *instance_name;
555         int ret;
556
557         ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
558         if (ret >= MAX_NAME_LEN)
559                 return ERR_PTR(-ENAMETOOLONG);
560
561         func_name = buf;
562         instance_name = strchr(func_name, '.');
563         if (!instance_name) {
564                 pr_err("Unable to locate . in FUNC.INSTANCE\n");
565                 return ERR_PTR(-EINVAL);
566         }
567         *instance_name = '\0';
568         instance_name++;
569
570         fi = usb_get_function_instance(func_name);
571         if (IS_ERR(fi))
572                 return ERR_CAST(fi);
573
574         ret = config_item_set_name(&fi->group.cg_item, "%s", name);
575         if (ret) {
576                 usb_put_function_instance(fi);
577                 return ERR_PTR(ret);
578         }
579         if (fi->set_inst_name) {
580                 ret = fi->set_inst_name(fi, instance_name);
581                 if (ret) {
582                         usb_put_function_instance(fi);
583                         return ERR_PTR(ret);
584                 }
585         }
586
587         gi = container_of(group, struct gadget_info, functions_group);
588
589         mutex_lock(&gi->lock);
590         list_add_tail(&fi->cfs_list, &gi->available_func);
591         mutex_unlock(&gi->lock);
592         return &fi->group;
593 }
594
595 static void function_drop(
596                 struct config_group *group,
597                 struct config_item *item)
598 {
599         struct usb_function_instance *fi = to_usb_function_instance(item);
600         struct gadget_info *gi;
601
602         gi = container_of(group, struct gadget_info, functions_group);
603
604         mutex_lock(&gi->lock);
605         list_del(&fi->cfs_list);
606         mutex_unlock(&gi->lock);
607         config_item_put(item);
608 }
609
610 static struct configfs_group_operations functions_ops = {
611         .make_group     = &function_make,
612         .drop_item      = &function_drop,
613 };
614
615 static struct config_item_type functions_type = {
616         .ct_group_ops   = &functions_ops,
617         .ct_owner       = THIS_MODULE,
618 };
619
620 GS_STRINGS_RW(gadget_config_name, configuration);
621
622 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
623         &gadget_config_name_attr_configuration,
624         NULL,
625 };
626
627 static void gadget_config_name_attr_release(struct config_item *item)
628 {
629         struct gadget_config_name *cn = to_gadget_config_name(item);
630
631         kfree(cn->configuration);
632
633         list_del(&cn->list);
634         kfree(cn);
635 }
636
637 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
638 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
639
640 static struct config_group *config_desc_make(
641                 struct config_group *group,
642                 const char *name)
643 {
644         struct gadget_info *gi;
645         struct config_usb_cfg *cfg;
646         char buf[MAX_NAME_LEN];
647         char *num_str;
648         u8 num;
649         int ret;
650
651         gi = container_of(group, struct gadget_info, configs_group);
652         ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
653         if (ret >= MAX_NAME_LEN)
654                 return ERR_PTR(-ENAMETOOLONG);
655
656         num_str = strchr(buf, '.');
657         if (!num_str) {
658                 pr_err("Unable to locate . in name.bConfigurationValue\n");
659                 return ERR_PTR(-EINVAL);
660         }
661
662         *num_str = '\0';
663         num_str++;
664
665         if (!strlen(buf))
666                 return ERR_PTR(-EINVAL);
667
668         ret = kstrtou8(num_str, 0, &num);
669         if (ret)
670                 return ERR_PTR(ret);
671
672         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
673         if (!cfg)
674                 return ERR_PTR(-ENOMEM);
675         cfg->c.label = kstrdup(buf, GFP_KERNEL);
676         if (!cfg->c.label) {
677                 ret = -ENOMEM;
678                 goto err;
679         }
680         cfg->c.bConfigurationValue = num;
681         cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
682         cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
683         INIT_LIST_HEAD(&cfg->string_list);
684         INIT_LIST_HEAD(&cfg->func_list);
685
686         config_group_init_type_name(&cfg->group, name,
687                                 &gadget_config_type);
688
689         config_group_init_type_name(&cfg->strings_group, "strings",
690                         &gadget_config_name_strings_type);
691         configfs_add_default_group(&cfg->strings_group, &cfg->group);
692
693         ret = usb_add_config_only(&gi->cdev, &cfg->c);
694         if (ret)
695                 goto err;
696
697         return &cfg->group;
698 err:
699         kfree(cfg->c.label);
700         kfree(cfg);
701         return ERR_PTR(ret);
702 }
703
704 static void config_desc_drop(
705                 struct config_group *group,
706                 struct config_item *item)
707 {
708         config_item_put(item);
709 }
710
711 static struct configfs_group_operations config_desc_ops = {
712         .make_group     = &config_desc_make,
713         .drop_item      = &config_desc_drop,
714 };
715
716 static struct config_item_type config_desc_type = {
717         .ct_group_ops   = &config_desc_ops,
718         .ct_owner       = THIS_MODULE,
719 };
720
721 GS_STRINGS_RW(gadget_strings, manufacturer);
722 GS_STRINGS_RW(gadget_strings, product);
723 GS_STRINGS_RW(gadget_strings, serialnumber);
724
725 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
726         &gadget_strings_attr_manufacturer,
727         &gadget_strings_attr_product,
728         &gadget_strings_attr_serialnumber,
729         NULL,
730 };
731
732 static void gadget_strings_attr_release(struct config_item *item)
733 {
734         struct gadget_strings *gs = to_gadget_strings(item);
735
736         kfree(gs->manufacturer);
737         kfree(gs->product);
738         kfree(gs->serialnumber);
739
740         list_del(&gs->list);
741         kfree(gs);
742 }
743
744 USB_CONFIG_STRING_RW_OPS(gadget_strings);
745 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
746
747 static inline struct os_desc *to_os_desc(struct config_item *item)
748 {
749         return container_of(to_config_group(item), struct os_desc, group);
750 }
751
752 static inline struct gadget_info *os_desc_item_to_gadget_info(
753                 struct config_item *item)
754 {
755         return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
756 }
757
758 static ssize_t os_desc_use_show(struct config_item *item, char *page)
759 {
760         return sprintf(page, "%d",
761                         os_desc_item_to_gadget_info(item)->use_os_desc);
762 }
763
764 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
765                                  size_t len)
766 {
767         struct gadget_info *gi = os_desc_item_to_gadget_info(item);
768         int ret;
769         bool use;
770
771         mutex_lock(&gi->lock);
772         ret = strtobool(page, &use);
773         if (!ret) {
774                 gi->use_os_desc = use;
775                 ret = len;
776         }
777         mutex_unlock(&gi->lock);
778
779         return ret;
780 }
781
782 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
783 {
784         return sprintf(page, "%d",
785                         os_desc_item_to_gadget_info(item)->b_vendor_code);
786 }
787
788 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
789                                            const char *page, size_t len)
790 {
791         struct gadget_info *gi = os_desc_item_to_gadget_info(item);
792         int ret;
793         u8 b_vendor_code;
794
795         mutex_lock(&gi->lock);
796         ret = kstrtou8(page, 0, &b_vendor_code);
797         if (!ret) {
798                 gi->b_vendor_code = b_vendor_code;
799                 ret = len;
800         }
801         mutex_unlock(&gi->lock);
802
803         return ret;
804 }
805
806 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
807 {
808         struct gadget_info *gi = os_desc_item_to_gadget_info(item);
809
810         memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
811         return OS_STRING_QW_SIGN_LEN;
812 }
813
814 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
815                                      size_t len)
816 {
817         struct gadget_info *gi = os_desc_item_to_gadget_info(item);
818         int res, l;
819
820         l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
821         if (page[l - 1] == '\n')
822                 --l;
823
824         mutex_lock(&gi->lock);
825         res = utf8s_to_utf16s(page, l,
826                               UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
827                               OS_STRING_QW_SIGN_LEN);
828         if (res > 0)
829                 res = len;
830         mutex_unlock(&gi->lock);
831
832         return res;
833 }
834
835 CONFIGFS_ATTR(os_desc_, use);
836 CONFIGFS_ATTR(os_desc_, b_vendor_code);
837 CONFIGFS_ATTR(os_desc_, qw_sign);
838
839 static struct configfs_attribute *os_desc_attrs[] = {
840         &os_desc_attr_use,
841         &os_desc_attr_b_vendor_code,
842         &os_desc_attr_qw_sign,
843         NULL,
844 };
845
846 static void os_desc_attr_release(struct config_item *item)
847 {
848         struct os_desc *os_desc = to_os_desc(item);
849         kfree(os_desc);
850 }
851
852 static int os_desc_link(struct config_item *os_desc_ci,
853                         struct config_item *usb_cfg_ci)
854 {
855         struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
856                                         struct gadget_info, os_desc_group);
857         struct usb_composite_dev *cdev = &gi->cdev;
858         struct config_usb_cfg *c_target =
859                 container_of(to_config_group(usb_cfg_ci),
860                              struct config_usb_cfg, group);
861         struct usb_configuration *c;
862         int ret;
863
864         mutex_lock(&gi->lock);
865         list_for_each_entry(c, &cdev->configs, list) {
866                 if (c == &c_target->c)
867                         break;
868         }
869         if (c != &c_target->c) {
870                 ret = -EINVAL;
871                 goto out;
872         }
873
874         if (cdev->os_desc_config) {
875                 ret = -EBUSY;
876                 goto out;
877         }
878
879         cdev->os_desc_config = &c_target->c;
880         ret = 0;
881
882 out:
883         mutex_unlock(&gi->lock);
884         return ret;
885 }
886
887 static int os_desc_unlink(struct config_item *os_desc_ci,
888                           struct config_item *usb_cfg_ci)
889 {
890         struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
891                                         struct gadget_info, os_desc_group);
892         struct usb_composite_dev *cdev = &gi->cdev;
893
894         mutex_lock(&gi->lock);
895         if (gi->composite.gadget_driver.udc_name)
896                 unregister_gadget(gi);
897         cdev->os_desc_config = NULL;
898         WARN_ON(gi->composite.gadget_driver.udc_name);
899         mutex_unlock(&gi->lock);
900         return 0;
901 }
902
903 static struct configfs_item_operations os_desc_ops = {
904         .release                = os_desc_attr_release,
905         .allow_link             = os_desc_link,
906         .drop_link              = os_desc_unlink,
907 };
908
909 static struct config_item_type os_desc_type = {
910         .ct_item_ops    = &os_desc_ops,
911         .ct_attrs       = os_desc_attrs,
912         .ct_owner       = THIS_MODULE,
913 };
914
915 static inline struct usb_os_desc_ext_prop
916 *to_usb_os_desc_ext_prop(struct config_item *item)
917 {
918         return container_of(item, struct usb_os_desc_ext_prop, item);
919 }
920
921 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
922 {
923         return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
924 }
925
926 static ssize_t ext_prop_type_store(struct config_item *item,
927                                    const char *page, size_t len)
928 {
929         struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
930         struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
931         u8 type;
932         int ret;
933
934         if (desc->opts_mutex)
935                 mutex_lock(desc->opts_mutex);
936         ret = kstrtou8(page, 0, &type);
937         if (ret)
938                 goto end;
939         if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
940                 ret = -EINVAL;
941                 goto end;
942         }
943
944         if ((ext_prop->type == USB_EXT_PROP_BINARY ||
945             ext_prop->type == USB_EXT_PROP_LE32 ||
946             ext_prop->type == USB_EXT_PROP_BE32) &&
947             (type == USB_EXT_PROP_UNICODE ||
948             type == USB_EXT_PROP_UNICODE_ENV ||
949             type == USB_EXT_PROP_UNICODE_LINK))
950                 ext_prop->data_len <<= 1;
951         else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
952                    ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
953                    ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
954                    (type == USB_EXT_PROP_BINARY ||
955                    type == USB_EXT_PROP_LE32 ||
956                    type == USB_EXT_PROP_BE32))
957                 ext_prop->data_len >>= 1;
958         ext_prop->type = type;
959         ret = len;
960
961 end:
962         if (desc->opts_mutex)
963                 mutex_unlock(desc->opts_mutex);
964         return ret;
965 }
966
967 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
968 {
969         struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
970         int len = ext_prop->data_len;
971
972         if (ext_prop->type == USB_EXT_PROP_UNICODE ||
973             ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
974             ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
975                 len >>= 1;
976         memcpy(page, ext_prop->data, len);
977
978         return len;
979 }
980
981 static ssize_t ext_prop_data_store(struct config_item *item,
982                                    const char *page, size_t len)
983 {
984         struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
985         struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
986         char *new_data;
987         size_t ret_len = len;
988
989         if (page[len - 1] == '\n' || page[len - 1] == '\0')
990                 --len;
991         new_data = kmemdup(page, len, GFP_KERNEL);
992         if (!new_data)
993                 return -ENOMEM;
994
995         if (desc->opts_mutex)
996                 mutex_lock(desc->opts_mutex);
997         kfree(ext_prop->data);
998         ext_prop->data = new_data;
999         desc->ext_prop_len -= ext_prop->data_len;
1000         ext_prop->data_len = len;
1001         desc->ext_prop_len += ext_prop->data_len;
1002         if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1003             ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1004             ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1005                 desc->ext_prop_len -= ext_prop->data_len;
1006                 ext_prop->data_len <<= 1;
1007                 ext_prop->data_len += 2;
1008                 desc->ext_prop_len += ext_prop->data_len;
1009         }
1010         if (desc->opts_mutex)
1011                 mutex_unlock(desc->opts_mutex);
1012         return ret_len;
1013 }
1014
1015 CONFIGFS_ATTR(ext_prop_, type);
1016 CONFIGFS_ATTR(ext_prop_, data);
1017
1018 static struct configfs_attribute *ext_prop_attrs[] = {
1019         &ext_prop_attr_type,
1020         &ext_prop_attr_data,
1021         NULL,
1022 };
1023
1024 static void usb_os_desc_ext_prop_release(struct config_item *item)
1025 {
1026         struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1027
1028         kfree(ext_prop); /* frees a whole chunk */
1029 }
1030
1031 static struct configfs_item_operations ext_prop_ops = {
1032         .release                = usb_os_desc_ext_prop_release,
1033 };
1034
1035 static struct config_item *ext_prop_make(
1036                 struct config_group *group,
1037                 const char *name)
1038 {
1039         struct usb_os_desc_ext_prop *ext_prop;
1040         struct config_item_type *ext_prop_type;
1041         struct usb_os_desc *desc;
1042         char *vlabuf;
1043
1044         vla_group(data_chunk);
1045         vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1046         vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1047
1048         vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1049         if (!vlabuf)
1050                 return ERR_PTR(-ENOMEM);
1051
1052         ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1053         ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1054
1055         desc = container_of(group, struct usb_os_desc, group);
1056         ext_prop_type->ct_item_ops = &ext_prop_ops;
1057         ext_prop_type->ct_attrs = ext_prop_attrs;
1058         ext_prop_type->ct_owner = desc->owner;
1059
1060         config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1061
1062         ext_prop->name = kstrdup(name, GFP_KERNEL);
1063         if (!ext_prop->name) {
1064                 kfree(vlabuf);
1065                 return ERR_PTR(-ENOMEM);
1066         }
1067         desc->ext_prop_len += 14;
1068         ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1069         if (desc->opts_mutex)
1070                 mutex_lock(desc->opts_mutex);
1071         desc->ext_prop_len += ext_prop->name_len;
1072         list_add_tail(&ext_prop->entry, &desc->ext_prop);
1073         ++desc->ext_prop_count;
1074         if (desc->opts_mutex)
1075                 mutex_unlock(desc->opts_mutex);
1076
1077         return &ext_prop->item;
1078 }
1079
1080 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1081 {
1082         struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1083         struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1084
1085         if (desc->opts_mutex)
1086                 mutex_lock(desc->opts_mutex);
1087         list_del(&ext_prop->entry);
1088         --desc->ext_prop_count;
1089         kfree(ext_prop->name);
1090         desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1091         if (desc->opts_mutex)
1092                 mutex_unlock(desc->opts_mutex);
1093         config_item_put(item);
1094 }
1095
1096 static struct configfs_group_operations interf_grp_ops = {
1097         .make_item      = &ext_prop_make,
1098         .drop_item      = &ext_prop_drop,
1099 };
1100
1101 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1102                                              char *page)
1103 {
1104         memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1105         return 8;
1106 }
1107
1108 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1109                                               const char *page, size_t len)
1110 {
1111         struct usb_os_desc *desc = to_usb_os_desc(item);
1112         int l;
1113
1114         l = min_t(int, 8, len);
1115         if (page[l - 1] == '\n')
1116                 --l;
1117         if (desc->opts_mutex)
1118                 mutex_lock(desc->opts_mutex);
1119         memcpy(desc->ext_compat_id, page, l);
1120
1121         if (desc->opts_mutex)
1122                 mutex_unlock(desc->opts_mutex);
1123
1124         return len;
1125 }
1126
1127 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1128                                                  char *page)
1129 {
1130         memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1131         return 8;
1132 }
1133
1134 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1135                                                   const char *page, size_t len)
1136 {
1137         struct usb_os_desc *desc = to_usb_os_desc(item);
1138         int l;
1139
1140         l = min_t(int, 8, len);
1141         if (page[l - 1] == '\n')
1142                 --l;
1143         if (desc->opts_mutex)
1144                 mutex_lock(desc->opts_mutex);
1145         memcpy(desc->ext_compat_id + 8, page, l);
1146
1147         if (desc->opts_mutex)
1148                 mutex_unlock(desc->opts_mutex);
1149
1150         return len;
1151 }
1152
1153 CONFIGFS_ATTR(interf_grp_, compatible_id);
1154 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1155
1156 static struct configfs_attribute *interf_grp_attrs[] = {
1157         &interf_grp_attr_compatible_id,
1158         &interf_grp_attr_sub_compatible_id,
1159         NULL
1160 };
1161
1162 struct config_group *usb_os_desc_prepare_interf_dir(
1163                 struct config_group *parent,
1164                 int n_interf,
1165                 struct usb_os_desc **desc,
1166                 char **names,
1167                 struct module *owner)
1168 {
1169         struct config_group *os_desc_group;
1170         struct config_item_type *os_desc_type, *interface_type;
1171
1172         vla_group(data_chunk);
1173         vla_item(data_chunk, struct config_group, os_desc_group, 1);
1174         vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1175         vla_item(data_chunk, struct config_item_type, interface_type, 1);
1176
1177         char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1178         if (!vlabuf)
1179                 return ERR_PTR(-ENOMEM);
1180
1181         os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1182         os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1183         interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1184
1185         os_desc_type->ct_owner = owner;
1186         config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1187         configfs_add_default_group(os_desc_group, parent);
1188
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                 configfs_add_default_group(&d->group, os_desc_group);
1202         }
1203
1204         return os_desc_group;
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_dbg(&gi->cdev.gadget->dev,
1235                                          "unbind function '%s'/%p\n",
1236                                          f->name, f);
1237                                 f->unbind(c, f);
1238                         }
1239                 }
1240                 c->next_interface_id = 0;
1241                 memset(c->interface, 0, sizeof(c->interface));
1242                 c->superspeed_plus = 0;
1243                 c->superspeed = 0;
1244                 c->highspeed = 0;
1245                 c->fullspeed = 0;
1246         }
1247 }
1248
1249 static int configfs_composite_bind(struct usb_gadget *gadget,
1250                 struct usb_gadget_driver *gdriver)
1251 {
1252         struct usb_composite_driver     *composite = to_cdriver(gdriver);
1253         struct gadget_info              *gi = container_of(composite,
1254                                                 struct gadget_info, composite);
1255         struct usb_composite_dev        *cdev = &gi->cdev;
1256         struct usb_configuration        *c;
1257         struct usb_string               *s;
1258         unsigned                        i;
1259         int                             ret;
1260
1261         /* the gi->lock is hold by the caller */
1262         gi->unbind = 0;
1263         cdev->gadget = gadget;
1264         set_gadget_data(gadget, cdev);
1265         ret = composite_dev_prepare(composite, cdev);
1266         if (ret)
1267                 return ret;
1268         /* and now the gadget bind */
1269         ret = -EINVAL;
1270
1271         if (list_empty(&gi->cdev.configs)) {
1272                 pr_err("Need at least one configuration in %s.\n",
1273                                 gi->composite.name);
1274                 goto err_comp_cleanup;
1275         }
1276
1277
1278         list_for_each_entry(c, &gi->cdev.configs, list) {
1279                 struct config_usb_cfg *cfg;
1280
1281                 cfg = container_of(c, struct config_usb_cfg, c);
1282                 if (list_empty(&cfg->func_list)) {
1283                         pr_err("Config %s/%d of %s needs at least one function.\n",
1284                               c->label, c->bConfigurationValue,
1285                               gi->composite.name);
1286                         goto err_comp_cleanup;
1287                 }
1288         }
1289
1290         /* init all strings */
1291         if (!list_empty(&gi->string_list)) {
1292                 struct gadget_strings *gs;
1293
1294                 i = 0;
1295                 list_for_each_entry(gs, &gi->string_list, list) {
1296
1297                         gi->gstrings[i] = &gs->stringtab_dev;
1298                         gs->stringtab_dev.strings = gs->strings;
1299                         gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1300                                 gs->manufacturer;
1301                         gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1302                         gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1303                         i++;
1304                 }
1305                 gi->gstrings[i] = NULL;
1306                 s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1307                                 USB_GADGET_FIRST_AVAIL_IDX);
1308                 if (IS_ERR(s)) {
1309                         ret = PTR_ERR(s);
1310                         goto err_comp_cleanup;
1311                 }
1312
1313                 gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1314                 gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1315                 gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1316         }
1317
1318         if (gi->use_os_desc) {
1319                 cdev->use_os_string = true;
1320                 cdev->b_vendor_code = gi->b_vendor_code;
1321                 memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1322         }
1323
1324         if (gadget_is_otg(gadget) && !otg_desc[0]) {
1325                 struct usb_descriptor_header *usb_desc;
1326
1327                 usb_desc = usb_otg_descriptor_alloc(gadget);
1328                 if (!usb_desc) {
1329                         ret = -ENOMEM;
1330                         goto err_comp_cleanup;
1331                 }
1332                 usb_otg_descriptor_init(gadget, usb_desc);
1333                 otg_desc[0] = usb_desc;
1334                 otg_desc[1] = NULL;
1335         }
1336
1337         /* Go through all configs, attach all functions */
1338         list_for_each_entry(c, &gi->cdev.configs, list) {
1339                 struct config_usb_cfg *cfg;
1340                 struct usb_function *f;
1341                 struct usb_function *tmp;
1342                 struct gadget_config_name *cn;
1343
1344                 if (gadget_is_otg(gadget))
1345                         c->descriptors = otg_desc;
1346
1347                 cfg = container_of(c, struct config_usb_cfg, c);
1348                 if (!list_empty(&cfg->string_list)) {
1349                         i = 0;
1350                         list_for_each_entry(cn, &cfg->string_list, list) {
1351                                 cfg->gstrings[i] = &cn->stringtab_dev;
1352                                 cn->stringtab_dev.strings = &cn->strings;
1353                                 cn->strings.s = cn->configuration;
1354                                 i++;
1355                         }
1356                         cfg->gstrings[i] = NULL;
1357                         s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1358                         if (IS_ERR(s)) {
1359                                 ret = PTR_ERR(s);
1360                                 goto err_comp_cleanup;
1361                         }
1362                         c->iConfiguration = s[0].id;
1363                 }
1364
1365                 list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1366                         list_del(&f->list);
1367                         ret = usb_add_function(c, f);
1368                         if (ret) {
1369                                 list_add(&f->list, &cfg->func_list);
1370                                 goto err_purge_funcs;
1371                         }
1372                 }
1373                 usb_ep_autoconfig_reset(cdev->gadget);
1374         }
1375         if (cdev->use_os_string) {
1376                 ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1377                 if (ret)
1378                         goto err_purge_funcs;
1379         }
1380
1381         usb_ep_autoconfig_reset(cdev->gadget);
1382         return 0;
1383
1384 err_purge_funcs:
1385         purge_configs_funcs(gi);
1386 err_comp_cleanup:
1387         composite_dev_cleanup(cdev);
1388         return ret;
1389 }
1390
1391 static void configfs_composite_unbind(struct usb_gadget *gadget)
1392 {
1393         struct usb_composite_dev        *cdev;
1394         struct gadget_info              *gi;
1395         unsigned long flags;
1396
1397         /* the gi->lock is hold by the caller */
1398
1399         cdev = get_gadget_data(gadget);
1400         gi = container_of(cdev, struct gadget_info, cdev);
1401         spin_lock_irqsave(&gi->spinlock, flags);
1402         gi->unbind = 1;
1403         spin_unlock_irqrestore(&gi->spinlock, flags);
1404
1405         kfree(otg_desc[0]);
1406         otg_desc[0] = NULL;
1407         purge_configs_funcs(gi);
1408         composite_dev_cleanup(cdev);
1409         usb_ep_autoconfig_reset(cdev->gadget);
1410         spin_lock_irqsave(&gi->spinlock, flags);
1411         cdev->gadget = NULL;
1412         set_gadget_data(gadget, NULL);
1413         spin_unlock_irqrestore(&gi->spinlock, flags);
1414 }
1415
1416 static int configfs_composite_setup(struct usb_gadget *gadget,
1417                 const struct usb_ctrlrequest *ctrl)
1418 {
1419         struct usb_composite_dev *cdev;
1420         struct gadget_info *gi;
1421         unsigned long flags;
1422         int ret;
1423
1424         cdev = get_gadget_data(gadget);
1425         if (!cdev)
1426                 return 0;
1427
1428         gi = container_of(cdev, struct gadget_info, cdev);
1429         spin_lock_irqsave(&gi->spinlock, flags);
1430         cdev = get_gadget_data(gadget);
1431         if (!cdev || gi->unbind) {
1432                 spin_unlock_irqrestore(&gi->spinlock, flags);
1433                 return 0;
1434         }
1435
1436         ret = composite_setup(gadget, ctrl);
1437         spin_unlock_irqrestore(&gi->spinlock, flags);
1438         return ret;
1439 }
1440
1441 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1442 {
1443         struct usb_composite_dev *cdev;
1444         struct gadget_info *gi;
1445         unsigned long flags;
1446
1447         cdev = get_gadget_data(gadget);
1448         if (!cdev)
1449                 return;
1450
1451         gi = container_of(cdev, struct gadget_info, cdev);
1452         spin_lock_irqsave(&gi->spinlock, flags);
1453         cdev = get_gadget_data(gadget);
1454         if (!cdev || gi->unbind) {
1455                 spin_unlock_irqrestore(&gi->spinlock, flags);
1456                 return;
1457         }
1458
1459         composite_disconnect(gadget);
1460         spin_unlock_irqrestore(&gi->spinlock, flags);
1461 }
1462
1463 static void configfs_composite_suspend(struct usb_gadget *gadget)
1464 {
1465         struct usb_composite_dev *cdev;
1466         struct gadget_info *gi;
1467         unsigned long flags;
1468
1469         cdev = get_gadget_data(gadget);
1470         if (!cdev)
1471                 return;
1472
1473         gi = container_of(cdev, struct gadget_info, cdev);
1474         spin_lock_irqsave(&gi->spinlock, flags);
1475         cdev = get_gadget_data(gadget);
1476         if (!cdev || gi->unbind) {
1477                 spin_unlock_irqrestore(&gi->spinlock, flags);
1478                 return;
1479         }
1480
1481         composite_suspend(gadget);
1482         spin_unlock_irqrestore(&gi->spinlock, flags);
1483 }
1484
1485 static void configfs_composite_resume(struct usb_gadget *gadget)
1486 {
1487         struct usb_composite_dev *cdev;
1488         struct gadget_info *gi;
1489         unsigned long flags;
1490
1491         cdev = get_gadget_data(gadget);
1492         if (!cdev)
1493                 return;
1494
1495         gi = container_of(cdev, struct gadget_info, cdev);
1496         spin_lock_irqsave(&gi->spinlock, flags);
1497         cdev = get_gadget_data(gadget);
1498         if (!cdev || gi->unbind) {
1499                 spin_unlock_irqrestore(&gi->spinlock, flags);
1500                 return;
1501         }
1502
1503         composite_resume(gadget);
1504         spin_unlock_irqrestore(&gi->spinlock, flags);
1505 }
1506
1507 static const struct usb_gadget_driver configfs_driver_template = {
1508         .bind           = configfs_composite_bind,
1509         .unbind         = configfs_composite_unbind,
1510
1511         .setup          = configfs_composite_setup,
1512         .reset          = configfs_composite_disconnect,
1513         .disconnect     = configfs_composite_disconnect,
1514
1515         .suspend        = configfs_composite_suspend,
1516         .resume         = configfs_composite_resume,
1517
1518         .max_speed      = USB_SPEED_SUPER_PLUS,
1519         .driver = {
1520                 .owner          = THIS_MODULE,
1521                 .name           = "configfs-gadget",
1522         },
1523         .match_existing_only = 1,
1524 };
1525
1526 static struct config_group *gadgets_make(
1527                 struct config_group *group,
1528                 const char *name)
1529 {
1530         struct gadget_info *gi;
1531
1532         gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1533         if (!gi)
1534                 return ERR_PTR(-ENOMEM);
1535
1536         config_group_init_type_name(&gi->group, name, &gadget_root_type);
1537
1538         config_group_init_type_name(&gi->functions_group, "functions",
1539                         &functions_type);
1540         configfs_add_default_group(&gi->functions_group, &gi->group);
1541
1542         config_group_init_type_name(&gi->configs_group, "configs",
1543                         &config_desc_type);
1544         configfs_add_default_group(&gi->configs_group, &gi->group);
1545
1546         config_group_init_type_name(&gi->strings_group, "strings",
1547                         &gadget_strings_strings_type);
1548         configfs_add_default_group(&gi->strings_group, &gi->group);
1549
1550         config_group_init_type_name(&gi->os_desc_group, "os_desc",
1551                         &os_desc_type);
1552         configfs_add_default_group(&gi->os_desc_group, &gi->group);
1553
1554         gi->composite.bind = configfs_do_nothing;
1555         gi->composite.unbind = configfs_do_nothing;
1556         gi->composite.suspend = NULL;
1557         gi->composite.resume = NULL;
1558         gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1559
1560         spin_lock_init(&gi->spinlock);
1561         mutex_init(&gi->lock);
1562         INIT_LIST_HEAD(&gi->string_list);
1563         INIT_LIST_HEAD(&gi->available_func);
1564
1565         composite_init_dev(&gi->cdev);
1566         gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1567         gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1568         gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1569
1570         gi->composite.gadget_driver = configfs_driver_template;
1571
1572         gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1573         gi->composite.name = gi->composite.gadget_driver.function;
1574
1575         if (!gi->composite.gadget_driver.function)
1576                 goto err;
1577
1578         return &gi->group;
1579 err:
1580         kfree(gi);
1581         return ERR_PTR(-ENOMEM);
1582 }
1583
1584 static void gadgets_drop(struct config_group *group, struct config_item *item)
1585 {
1586         config_item_put(item);
1587 }
1588
1589 static struct configfs_group_operations gadgets_ops = {
1590         .make_group     = &gadgets_make,
1591         .drop_item      = &gadgets_drop,
1592 };
1593
1594 static struct config_item_type gadgets_type = {
1595         .ct_group_ops   = &gadgets_ops,
1596         .ct_owner       = THIS_MODULE,
1597 };
1598
1599 static struct configfs_subsystem gadget_subsys = {
1600         .su_group = {
1601                 .cg_item = {
1602                         .ci_namebuf = "usb_gadget",
1603                         .ci_type = &gadgets_type,
1604                 },
1605         },
1606         .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1607 };
1608
1609 void unregister_gadget_item(struct config_item *item)
1610 {
1611         struct gadget_info *gi = to_gadget_info(item);
1612
1613         mutex_lock(&gi->lock);
1614         unregister_gadget(gi);
1615         mutex_unlock(&gi->lock);
1616 }
1617 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1618
1619 static int __init gadget_cfs_init(void)
1620 {
1621         int ret;
1622
1623         config_group_init(&gadget_subsys.su_group);
1624
1625         ret = configfs_register_subsystem(&gadget_subsys);
1626         return ret;
1627 }
1628 module_init(gadget_cfs_init);
1629
1630 static void __exit gadget_cfs_exit(void)
1631 {
1632         configfs_unregister_subsystem(&gadget_subsys);
1633 }
1634 module_exit(gadget_cfs_exit);