OSDN Git Service

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