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