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