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