Merge tag 'usb-3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[pandora-kernel.git] / drivers / staging / ccg / ccg.c
1 /*
2  * Configurable Composite Gadget
3  *
4  * Initially contributed as "Android Composite Gdaget" by:
5  *
6  * Copyright (C) 2008 Google, Inc.
7  * Author: Mike Lockwood <lockwood@android.com>
8  *         Benoit Goby <benoit@android.com>
9  *
10  * Tailoring it to become a generic Configurable Composite Gadget is
11  *
12  * Copyright (C) 2012 Samsung Electronics
13  * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
14  *
15  * This software is licensed under the terms of the GNU General Public
16  * License version 2, as published by the Free Software Foundation, and
17  * may be copied, distributed, and modified under those terms.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  */
25
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/fs.h>
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/utsname.h>
32 #include <linux/platform_device.h>
33
34 #include <linux/usb/ch9.h>
35 #include "composite.h"
36 #include <linux/usb/gadget.h>
37
38 #include "gadget_chips.h"
39
40 /*
41  * Kbuild is not very cooperative with respect to linking separately
42  * compiled library objects into one module.  So for now we won't use
43  * separate compilation ... ensuring init/exit sections work to shrink
44  * the runtime footprint, and giving us at least some parts of what
45  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
46  */
47 #include "usbstring.c"
48 #include "config.c"
49 #include "epautoconf.c"
50 #include "composite.c"
51
52 #include "f_mass_storage.c"
53 #include "u_serial.c"
54 #include "f_acm.c"
55 #define USB_ETH_RNDIS y
56 #include "f_rndis.c"
57 #include "rndis.c"
58 #include "u_ether.c"
59 #include "f_fs.c"
60
61 MODULE_AUTHOR("Mike Lockwood, Andrzej Pietrasiewicz");
62 MODULE_DESCRIPTION("Configurable Composite USB Gadget");
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION("1.0");
65
66 static const char longname[] = "Configurable Composite Gadget";
67
68 /* Default vendor and product IDs, overridden by userspace */
69 #define VENDOR_ID               0x1d6b /* Linux Foundation */
70 #define PRODUCT_ID              0x0107
71 #define GFS_MAX_DEVS            10
72
73 struct ccg_usb_function {
74         char *name;
75         void *config;
76
77         struct device *dev;
78         char *dev_name;
79         struct device_attribute **attributes;
80
81         /* for ccg_dev.enabled_functions */
82         struct list_head enabled_list;
83
84         /* Optional: initialization during gadget bind */
85         int (*init)(struct ccg_usb_function *, struct usb_composite_dev *);
86         /* Optional: cleanup during gadget unbind */
87         void (*cleanup)(struct ccg_usb_function *);
88
89         int (*bind_config)(struct ccg_usb_function *,
90                            struct usb_configuration *);
91
92         /* Optional: called when the configuration is removed */
93         void (*unbind_config)(struct ccg_usb_function *,
94                               struct usb_configuration *);
95         /* Optional: handle ctrl requests before the device is configured */
96         int (*ctrlrequest)(struct ccg_usb_function *,
97                            struct usb_composite_dev *,
98                            const struct usb_ctrlrequest *);
99 };
100
101 struct ffs_obj {
102         const char *name;
103         bool mounted;
104         bool desc_ready;
105         bool used;
106         struct ffs_data *ffs_data;
107 };
108
109 struct ccg_dev {
110         struct ccg_usb_function **functions;
111         struct list_head enabled_functions;
112         struct usb_composite_dev *cdev;
113         struct device *dev;
114
115         bool enabled;
116         struct mutex mutex;
117         bool connected;
118         bool sw_connected;
119         struct work_struct work;
120
121         unsigned int max_func_num;
122         unsigned int func_num;
123         struct ffs_obj ffs_tab[GFS_MAX_DEVS];
124 };
125
126 static struct class *ccg_class;
127 static struct ccg_dev *_ccg_dev;
128 static int ccg_bind_config(struct usb_configuration *c);
129 static void ccg_unbind_config(struct usb_configuration *c);
130
131 static char func_names_buf[256];
132
133 static struct usb_device_descriptor device_desc = {
134         .bLength              = sizeof(device_desc),
135         .bDescriptorType      = USB_DT_DEVICE,
136         .bcdUSB               = __constant_cpu_to_le16(0x0200),
137         .bDeviceClass         = USB_CLASS_PER_INTERFACE,
138         .idVendor             = __constant_cpu_to_le16(VENDOR_ID),
139         .idProduct            = __constant_cpu_to_le16(PRODUCT_ID),
140         .bcdDevice            = __constant_cpu_to_le16(0xffff),
141         .bNumConfigurations   = 1,
142 };
143
144 static struct usb_configuration ccg_config_driver = {
145         .label          = "ccg",
146         .unbind         = ccg_unbind_config,
147         .bConfigurationValue = 1,
148         .bmAttributes   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
149         .bMaxPower      = 0xFA, /* 500ma */
150 };
151
152 static void ccg_work(struct work_struct *data)
153 {
154         struct ccg_dev *dev = container_of(data, struct ccg_dev, work);
155         struct usb_composite_dev *cdev = dev->cdev;
156         static char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
157         static char *connected[2]    = { "USB_STATE=CONNECTED", NULL };
158         static char *configured[2]   = { "USB_STATE=CONFIGURED", NULL };
159         char **uevent_envp = NULL;
160         unsigned long flags;
161
162         spin_lock_irqsave(&cdev->lock, flags);
163         if (cdev->config)
164                 uevent_envp = configured;
165         else if (dev->connected != dev->sw_connected)
166                 uevent_envp = dev->connected ? connected : disconnected;
167         dev->sw_connected = dev->connected;
168         spin_unlock_irqrestore(&cdev->lock, flags);
169
170         if (uevent_envp) {
171                 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
172                 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
173         } else {
174                 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
175                          dev->connected, dev->sw_connected, cdev->config);
176         }
177 }
178
179
180 /*-------------------------------------------------------------------------*/
181 /* Supported functions initialization */
182
183 static struct ffs_obj *functionfs_find_dev(struct ccg_dev *dev,
184                                            const char *dev_name)
185 {
186         int i;
187
188         for (i = 0; i < dev->max_func_num; i++)
189                 if (strcmp(dev->ffs_tab[i].name, dev_name) == 0)
190                         return &dev->ffs_tab[i];
191
192         return NULL;
193 }
194
195 static bool functionfs_all_ready(struct ccg_dev *dev)
196 {
197         int i;
198
199         for (i = 0; i < dev->max_func_num; i++)
200                 if (dev->ffs_tab[i].used && !dev->ffs_tab[i].desc_ready)
201                         return false;
202
203         return true;
204 }
205
206 static int functionfs_ready_callback(struct ffs_data *ffs)
207 {
208         struct ffs_obj *ffs_obj;
209         int ret;
210
211         mutex_lock(&_ccg_dev->mutex);
212
213         ffs_obj = ffs->private_data;
214         if (!ffs_obj) {
215                 ret = -EINVAL;
216                 goto done;
217         }
218         if (WARN_ON(ffs_obj->desc_ready)) {
219                 ret = -EBUSY;
220                 goto done;
221         }
222         ffs_obj->ffs_data = ffs;
223
224         if (functionfs_all_ready(_ccg_dev)) {
225                 ret = -EBUSY;
226                 goto done;
227         }
228         ffs_obj->desc_ready = true;
229
230 done:
231         mutex_unlock(&_ccg_dev->mutex);
232         return ret;
233 }
234
235 static void reset_usb(struct ccg_dev *dev)
236 {
237         /* Cancel pending control requests */
238         usb_ep_dequeue(dev->cdev->gadget->ep0, dev->cdev->req);
239         usb_remove_config(dev->cdev, &ccg_config_driver);
240         dev->enabled = false;
241         usb_gadget_disconnect(dev->cdev->gadget);
242 }
243
244 static void functionfs_closed_callback(struct ffs_data *ffs)
245 {
246         struct ffs_obj *ffs_obj;
247
248         mutex_lock(&_ccg_dev->mutex);
249
250         ffs_obj = ffs->private_data;
251         if (!ffs_obj)
252                 goto done;
253
254         ffs_obj->desc_ready = false;
255
256         if (_ccg_dev->enabled)
257                 reset_usb(_ccg_dev);
258
259 done:
260         mutex_unlock(&_ccg_dev->mutex);
261 }
262
263 static void *functionfs_acquire_dev_callback(const char *dev_name)
264 {
265         struct ffs_obj *ffs_dev;
266
267         mutex_lock(&_ccg_dev->mutex);
268
269         ffs_dev = functionfs_find_dev(_ccg_dev, dev_name);
270         if (!ffs_dev) {
271                 ffs_dev = ERR_PTR(-ENODEV);
272                 goto done;
273         }
274
275         if (ffs_dev->mounted) {
276                 ffs_dev = ERR_PTR(-EBUSY);
277                 goto done;
278         }
279         ffs_dev->mounted = true;
280
281 done:
282         mutex_unlock(&_ccg_dev->mutex);
283         return ffs_dev;
284 }
285
286 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
287 {
288         struct ffs_obj *ffs_dev;
289
290         mutex_lock(&_ccg_dev->mutex);
291
292         ffs_dev = ffs_data->private_data;
293         if (ffs_dev)
294                 ffs_dev->mounted = false;
295
296         mutex_unlock(&_ccg_dev->mutex);
297 }
298
299 static int functionfs_function_init(struct ccg_usb_function *f,
300                                 struct usb_composite_dev *cdev)
301 {
302         return functionfs_init();
303 }
304
305 static void functionfs_function_cleanup(struct ccg_usb_function *f)
306 {
307         functionfs_cleanup();
308 }
309
310 static int functionfs_function_bind_config(struct ccg_usb_function *f,
311                                            struct usb_configuration *c)
312 {
313         struct ccg_dev *dev = _ccg_dev;
314         int i, ret;
315
316         for (i = dev->max_func_num; i--; ) {
317                 if (!dev->ffs_tab[i].used)
318                         continue;
319                 ret = functionfs_bind(dev->ffs_tab[i].ffs_data, c->cdev);
320                 if (unlikely(ret < 0)) {
321                         while (++i < dev->max_func_num)
322                                 functionfs_unbind(dev->ffs_tab[i].ffs_data);
323                         return ret;
324                 }
325         }
326
327         for (i = dev->max_func_num; i--; ) {
328                 if (!dev->ffs_tab[i].used)
329                         continue;
330                 ret = functionfs_bind_config(c->cdev, c,
331                                              dev->ffs_tab[i].ffs_data);
332                 if (unlikely(ret < 0))
333                         return ret;
334         }
335
336         return 0;
337 }
338
339 static void functionfs_function_unbind_config(struct ccg_usb_function *f,
340                                               struct usb_configuration *c)
341 {
342         struct ccg_dev *dev = _ccg_dev;
343         int i;
344
345         for (i = dev->max_func_num; i--; )
346                 if (dev->ffs_tab[i].ffs_data)
347                         functionfs_unbind(dev->ffs_tab[i].ffs_data);
348 }
349
350 static ssize_t functionfs_user_functions_show(struct device *_dev,
351                                               struct device_attribute *attr,
352                                               char *buf)
353 {
354         struct ccg_dev *dev = _ccg_dev;
355         char *buff = buf;
356         int i;
357
358         mutex_lock(&dev->mutex);
359
360         for (i = 0; i < dev->max_func_num; i++)
361                 buff += snprintf(buff, PAGE_SIZE + buf - buff, "%s,",
362                                  dev->ffs_tab[i].name);
363
364         mutex_unlock(&dev->mutex);
365
366         if (buff != buf)
367                 *(buff - 1) = '\n';
368         return buff - buf;
369 }
370
371 static ssize_t functionfs_user_functions_store(struct device *_dev,
372                                                struct device_attribute *attr,
373                                                const char *buff, size_t size)
374 {
375         struct ccg_dev *dev = _ccg_dev;
376         char *name, *b;
377         ssize_t ret = size;
378         int i;
379
380         buff = skip_spaces(buff);
381         if (!*buff)
382                 return -EINVAL;
383
384         mutex_lock(&dev->mutex);
385
386         if (dev->enabled) {
387                 ret = -EBUSY;
388                 goto end;
389         }
390
391         for (i = 0; i < dev->max_func_num; i++)
392                 if (dev->ffs_tab[i].mounted) {
393                         ret = -EBUSY;
394                         goto end;
395                 }
396
397         strlcpy(func_names_buf, buff, sizeof(func_names_buf));
398         b = strim(func_names_buf);
399
400         /* replace the list of functions */
401         dev->max_func_num = 0;
402         while (b) {
403                 name = strsep(&b, ",");
404                 if (dev->max_func_num == GFS_MAX_DEVS) {
405                         ret = -ENOSPC;
406                         goto end;
407                 }
408                 if (functionfs_find_dev(dev, name)) {
409                         ret = -EEXIST;
410                         continue;
411                 }
412                 dev->ffs_tab[dev->max_func_num++].name = name;
413         }
414
415 end:
416         mutex_unlock(&dev->mutex);
417         return ret;
418 }
419
420 static DEVICE_ATTR(user_functions, S_IRUGO | S_IWUSR,
421                    functionfs_user_functions_show,
422                    functionfs_user_functions_store);
423
424 static ssize_t functionfs_max_user_functions_show(struct device *_dev,
425                                                   struct device_attribute *attr,
426                                                   char *buf)
427 {
428         return sprintf(buf, "%d", GFS_MAX_DEVS);
429 }
430
431 static DEVICE_ATTR(max_user_functions, S_IRUGO,
432                    functionfs_max_user_functions_show, NULL);
433
434 static struct device_attribute *functionfs_function_attributes[] = {
435         &dev_attr_user_functions,
436         &dev_attr_max_user_functions,
437         NULL
438 };
439
440 static struct ccg_usb_function functionfs_function = {
441         .name           = "fs",
442         .init           = functionfs_function_init,
443         .cleanup        = functionfs_function_cleanup,
444         .bind_config    = functionfs_function_bind_config,
445         .unbind_config  = functionfs_function_unbind_config,
446         .attributes     = functionfs_function_attributes,
447 };
448
449 #define MAX_ACM_INSTANCES 4
450 struct acm_function_config {
451         int instances;
452 };
453
454 static int
455 acm_function_init(struct ccg_usb_function *f, struct usb_composite_dev *cdev)
456 {
457         f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
458         if (!f->config)
459                 return -ENOMEM;
460
461         return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
462 }
463
464 static void acm_function_cleanup(struct ccg_usb_function *f)
465 {
466         gserial_cleanup();
467         kfree(f->config);
468         f->config = NULL;
469 }
470
471 static int
472 acm_function_bind_config(struct ccg_usb_function *f,
473                 struct usb_configuration *c)
474 {
475         int i;
476         int ret = 0;
477         struct acm_function_config *config = f->config;
478
479         for (i = 0; i < config->instances; i++) {
480                 ret = acm_bind_config(c, i);
481                 if (ret) {
482                         pr_err("Could not bind acm%u config\n", i);
483                         break;
484                 }
485         }
486
487         return ret;
488 }
489
490 static ssize_t acm_instances_show(struct device *dev,
491                 struct device_attribute *attr, char *buf)
492 {
493         struct ccg_usb_function *f = dev_get_drvdata(dev);
494         struct acm_function_config *config = f->config;
495         return sprintf(buf, "%d\n", config->instances);
496 }
497
498 static ssize_t acm_instances_store(struct device *dev,
499                 struct device_attribute *attr, const char *buf, size_t size)
500 {
501         struct ccg_usb_function *f = dev_get_drvdata(dev);
502         struct acm_function_config *config = f->config;
503         int value;
504         int ret = 0;
505
506         ret = kstrtoint(buf, 10, &value);
507         if (ret)
508                 return ret;
509
510         if (value > MAX_ACM_INSTANCES)
511                 return -EINVAL;
512
513         config->instances = value;
514
515         return size;
516 }
517
518 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
519                                                  acm_instances_store);
520 static struct device_attribute *acm_function_attributes[] = {
521         &dev_attr_instances,
522         NULL
523 };
524
525 static struct ccg_usb_function acm_function = {
526         .name           = "acm",
527         .init           = acm_function_init,
528         .cleanup        = acm_function_cleanup,
529         .bind_config    = acm_function_bind_config,
530         .attributes     = acm_function_attributes,
531 };
532
533 struct rndis_function_config {
534         u8      ethaddr[ETH_ALEN];
535         u32     vendorID;
536         char    manufacturer[256];
537         /* "Wireless" RNDIS; auto-detected by Windows */
538         bool    wceis;
539 };
540
541 static int rndis_function_init(struct ccg_usb_function *f,
542                                struct usb_composite_dev *cdev)
543 {
544         f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
545         if (!f->config)
546                 return -ENOMEM;
547         return 0;
548 }
549
550 static void rndis_function_cleanup(struct ccg_usb_function *f)
551 {
552         kfree(f->config);
553         f->config = NULL;
554 }
555
556 static int rndis_function_bind_config(struct ccg_usb_function *f,
557                                       struct usb_configuration *c)
558 {
559         int ret;
560         struct rndis_function_config *rndis = f->config;
561
562         if (!rndis) {
563                 pr_err("%s: rndis_pdata\n", __func__);
564                 return -1;
565         }
566
567         pr_info("%s MAC: %pM\n", __func__, rndis->ethaddr);
568
569         ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
570         if (ret) {
571                 pr_err("%s: gether_setup failed\n", __func__);
572                 return ret;
573         }
574
575         if (rndis->wceis) {
576                 /* "Wireless" RNDIS; auto-detected by Windows */
577                 rndis_iad_descriptor.bFunctionClass =
578                                                 USB_CLASS_WIRELESS_CONTROLLER;
579                 rndis_iad_descriptor.bFunctionSubClass = 0x01;
580                 rndis_iad_descriptor.bFunctionProtocol = 0x03;
581                 rndis_control_intf.bInterfaceClass =
582                                                 USB_CLASS_WIRELESS_CONTROLLER;
583                 rndis_control_intf.bInterfaceSubClass =  0x01;
584                 rndis_control_intf.bInterfaceProtocol =  0x03;
585         }
586
587         return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
588                                            rndis->manufacturer);
589 }
590
591 static void rndis_function_unbind_config(struct ccg_usb_function *f,
592                                                 struct usb_configuration *c)
593 {
594         gether_cleanup();
595 }
596
597 static ssize_t rndis_manufacturer_show(struct device *dev,
598                 struct device_attribute *attr, char *buf)
599 {
600         struct ccg_usb_function *f = dev_get_drvdata(dev);
601         struct rndis_function_config *config = f->config;
602         return sprintf(buf, "%s\n", config->manufacturer);
603 }
604
605 static ssize_t rndis_manufacturer_store(struct device *dev,
606                 struct device_attribute *attr, const char *buf, size_t size)
607 {
608         struct ccg_usb_function *f = dev_get_drvdata(dev);
609         struct rndis_function_config *config = f->config;
610
611         if (size >= sizeof(config->manufacturer))
612                 return -EINVAL;
613         memcpy(config->manufacturer, buf, size);
614         config->manufacturer[size] = 0;
615
616         return size;
617 }
618
619 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
620                                                     rndis_manufacturer_store);
621
622 static ssize_t rndis_wceis_show(struct device *dev,
623                 struct device_attribute *attr, char *buf)
624 {
625         struct ccg_usb_function *f = dev_get_drvdata(dev);
626         struct rndis_function_config *config = f->config;
627         return sprintf(buf, "%d\n", config->wceis);
628 }
629
630 static ssize_t rndis_wceis_store(struct device *dev,
631                 struct device_attribute *attr, const char *buf, size_t size)
632 {
633         struct ccg_usb_function *f = dev_get_drvdata(dev);
634         struct rndis_function_config *config = f->config;
635         int value;
636         int ret;
637
638         ret = kstrtoint(buf, 10, &value);
639         if (ret)
640                 return ret;
641
642         config->wceis = value;
643
644         return size;
645 }
646
647 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
648                                              rndis_wceis_store);
649
650 static ssize_t rndis_ethaddr_show(struct device *dev,
651                 struct device_attribute *attr, char *buf)
652 {
653         struct ccg_usb_function *f = dev_get_drvdata(dev);
654         struct rndis_function_config *rndis = f->config;
655         return sprintf(buf, "%pM\n", rndis->ethaddr);
656 }
657
658 static ssize_t rndis_ethaddr_store(struct device *dev,
659                 struct device_attribute *attr, const char *buf, size_t size)
660 {
661         struct ccg_usb_function *f = dev_get_drvdata(dev);
662         struct rndis_function_config *rndis = f->config;
663         unsigned char tmp[6];
664
665         if (sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
666                    tmp + 0, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5) !=
667             ETH_ALEN)
668                 return -EINVAL;
669
670         memcpy(rndis->ethaddr, tmp, ETH_ALEN);
671
672         return ETH_ALEN;
673
674 }
675
676 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
677                                                rndis_ethaddr_store);
678
679 static ssize_t rndis_vendorID_show(struct device *dev,
680                 struct device_attribute *attr, char *buf)
681 {
682         struct ccg_usb_function *f = dev_get_drvdata(dev);
683         struct rndis_function_config *config = f->config;
684         return sprintf(buf, "%04x\n", config->vendorID);
685 }
686
687 static ssize_t rndis_vendorID_store(struct device *dev,
688                 struct device_attribute *attr, const char *buf, size_t size)
689 {
690         struct ccg_usb_function *f = dev_get_drvdata(dev);
691         struct rndis_function_config *config = f->config;
692         int value;
693         int ret;
694
695         ret = kstrtou32(buf, 16, &value);
696         if (ret)
697                 return ret;
698
699         config->vendorID = value;
700
701         return size;
702 }
703
704 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
705                                                 rndis_vendorID_store);
706
707 static struct device_attribute *rndis_function_attributes[] = {
708         &dev_attr_manufacturer,
709         &dev_attr_wceis,
710         &dev_attr_ethaddr,
711         &dev_attr_vendorID,
712         NULL
713 };
714
715 static struct ccg_usb_function rndis_function = {
716         .name           = "rndis",
717         .init           = rndis_function_init,
718         .cleanup        = rndis_function_cleanup,
719         .bind_config    = rndis_function_bind_config,
720         .unbind_config  = rndis_function_unbind_config,
721         .attributes     = rndis_function_attributes,
722 };
723
724 static int mass_storage_function_init(struct ccg_usb_function *f,
725                                         struct usb_composite_dev *cdev)
726 {
727         struct fsg_config fsg;
728         struct fsg_common *common;
729         int err;
730
731         memset(&fsg, 0, sizeof(fsg));
732         fsg.nluns = 1;
733         fsg.luns[0].removable = 1;
734         fsg.vendor_name = iManufacturer;
735         fsg.product_name = iProduct;
736
737         common = fsg_common_init(NULL, cdev, &fsg);
738         if (IS_ERR(common))
739                 return PTR_ERR(common);
740
741         err = sysfs_create_link(&f->dev->kobj,
742                                 &common->luns[0].dev.kobj,
743                                 "lun");
744         if (err) {
745                 fsg_common_put(common);
746                 return err;
747         }
748
749         f->config = common;
750         return 0;
751 }
752
753 static void mass_storage_function_cleanup(struct ccg_usb_function *f)
754 {
755         fsg_common_put(f->config);
756         f->config = NULL;
757 }
758
759 static int mass_storage_function_bind_config(struct ccg_usb_function *f,
760                                              struct usb_configuration *c)
761 {
762         struct fsg_common *common = f->config;
763         return fsg_bind_config(c->cdev, c, common);
764 }
765
766 static struct ccg_usb_function mass_storage_function = {
767         .name           = "mass_storage",
768         .init           = mass_storage_function_init,
769         .cleanup        = mass_storage_function_cleanup,
770         .bind_config    = mass_storage_function_bind_config,
771 };
772
773 static struct ccg_usb_function *supported_functions[] = {
774         &functionfs_function,
775         &acm_function,
776         &rndis_function,
777         &mass_storage_function,
778         NULL
779 };
780
781
782 static int ccg_init_functions(struct ccg_usb_function **functions,
783                                   struct usb_composite_dev *cdev)
784 {
785         struct ccg_dev *dev = _ccg_dev;
786         struct ccg_usb_function *f;
787         struct device_attribute **attrs;
788         struct device_attribute *attr;
789         int err;
790         int index = 0;
791
792         for (; (f = *functions++); index++) {
793                 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
794                 if (!f->dev_name) {
795                         pr_err("%s: Failed to alloc name %s", __func__,
796                                f->name);
797                         err = -ENOMEM;
798                         goto err_alloc;
799                 }
800                 f->dev = device_create(ccg_class, dev->dev,
801                                 MKDEV(0, index), f, f->dev_name);
802                 if (IS_ERR(f->dev)) {
803                         pr_err("%s: Failed to create dev %s", __func__,
804                                                         f->dev_name);
805                         err = PTR_ERR(f->dev);
806                         f->dev = NULL;
807                         goto err_create;
808                 }
809
810                 if (f->init) {
811                         err = f->init(f, cdev);
812                         if (err) {
813                                 pr_err("%s: Failed to init %s", __func__,
814                                                                 f->name);
815                                 goto err_out;
816                         }
817                 }
818
819                 attrs = f->attributes;
820                 if (attrs) {
821                         while ((attr = *attrs++) && !err)
822                                 err = device_create_file(f->dev, attr);
823                 }
824                 if (err) {
825                         pr_err("%s: Failed to create function %s attributes",
826                                         __func__, f->name);
827                         goto err_uninit;
828                 }
829         }
830         return 0;
831
832 err_uninit:
833         if (f->cleanup)
834                 f->cleanup(f);
835 err_out:
836         device_destroy(ccg_class, f->dev->devt);
837         f->dev = NULL;
838 err_create:
839         kfree(f->dev_name);
840 err_alloc:
841         return err;
842 }
843
844 static void ccg_cleanup_functions(struct ccg_usb_function **functions)
845 {
846         struct ccg_usb_function *f;
847
848         while (*functions) {
849                 f = *functions++;
850
851                 if (f->dev) {
852                         if (f->cleanup)
853                                 f->cleanup(f);
854                         device_destroy(ccg_class, f->dev->devt);
855                         kfree(f->dev_name);
856                 }
857         }
858 }
859
860 static int ccg_bind_enabled_functions(struct ccg_dev *dev,
861                                       struct usb_configuration *c)
862 {
863         struct ccg_usb_function *f;
864         int ret;
865
866         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
867                 ret = f->bind_config(f, c);
868                 if (ret) {
869                         pr_err("%s: %s failed", __func__, f->name);
870                         return ret;
871                 }
872         }
873         return 0;
874 }
875
876 static void ccg_unbind_enabled_functions(struct ccg_dev *dev,
877                                          struct usb_configuration *c)
878 {
879         struct ccg_usb_function *f;
880
881         list_for_each_entry(f, &dev->enabled_functions, enabled_list)
882                 if (f->unbind_config)
883                         f->unbind_config(f, c);
884 }
885
886 static int ccg_enable_function(struct ccg_dev *dev, char *name)
887 {
888         struct ccg_usb_function **functions = dev->functions;
889         struct ccg_usb_function *f;
890         while ((f = *functions++)) {
891                 if (!strcmp(name, f->name)) {
892                         list_add_tail(&f->enabled_list,
893                                                 &dev->enabled_functions);
894                         return 0;
895                 }
896         }
897         return -EINVAL;
898 }
899
900 /*-------------------------------------------------------------------------*/
901 /* /sys/class/ccg_usb/ccg%d/ interface */
902
903 static ssize_t
904 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
905 {
906         struct ccg_dev *dev = dev_get_drvdata(pdev);
907         struct ccg_usb_function *f;
908         char *buff = buf;
909         int i;
910
911         mutex_lock(&dev->mutex);
912
913         list_for_each_entry(f, &dev->enabled_functions, enabled_list)
914                 buff += sprintf(buff, "%s,", f->name);
915         for (i = 0; i < dev->max_func_num; i++)
916                 if (dev->ffs_tab[i].used)
917                         buff += sprintf(buff, "%s", dev->ffs_tab[i].name);
918
919         mutex_unlock(&dev->mutex);
920
921         if (buff != buf)
922                 *(buff-1) = '\n';
923         return buff - buf;
924 }
925
926 static ssize_t
927 functions_store(struct device *pdev, struct device_attribute *attr,
928                                const char *buff, size_t size)
929 {
930         struct ccg_dev *dev = dev_get_drvdata(pdev);
931         char *name;
932         char buf[256], *b;
933         int err, i;
934         bool functionfs_enabled;
935
936         buff = skip_spaces(buff);
937         if (!*buff)
938                 return -EINVAL;
939
940         mutex_lock(&dev->mutex);
941
942         if (dev->enabled) {
943                 mutex_unlock(&dev->mutex);
944                 return -EBUSY;
945         }
946
947         INIT_LIST_HEAD(&dev->enabled_functions);
948         functionfs_enabled = false;
949         for (i = 0; i < dev->max_func_num; i++)
950                 dev->ffs_tab[i].used = false;
951
952         strlcpy(buf, buff, sizeof(buf));
953         b = strim(buf);
954
955         while (b) {
956                 struct ffs_obj *user_func;
957
958                 name = strsep(&b, ",");
959                 /* handle FunctionFS implicitly */
960                 if (!strcmp(name, functionfs_function.name)) {
961                         pr_err("ccg_usb: Cannot explicitly enable '%s'", name);
962                         continue;
963                 }
964                 user_func = functionfs_find_dev(dev, name);
965                 if (user_func)
966                         name = functionfs_function.name;
967                 err = 0;
968                 if (!user_func || !functionfs_enabled)
969                         err = ccg_enable_function(dev, name);
970                 if (err)
971                         pr_err("ccg_usb: Cannot enable '%s'", name);
972                 else if (user_func) {
973                         user_func->used = true;
974                         dev->func_num++;
975                         functionfs_enabled = true;
976                 }
977         }
978
979         mutex_unlock(&dev->mutex);
980
981         return size;
982 }
983
984 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
985                            char *buf)
986 {
987         struct ccg_dev *dev = dev_get_drvdata(pdev);
988         return sprintf(buf, "%d\n", dev->enabled);
989 }
990
991 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
992                             const char *buff, size_t size)
993 {
994         struct ccg_dev *dev = dev_get_drvdata(pdev);
995         struct usb_composite_dev *cdev = dev->cdev;
996         int enabled = 0;
997
998         mutex_lock(&dev->mutex);
999         sscanf(buff, "%d", &enabled);
1000         if (enabled && dev->func_num && !functionfs_all_ready(dev)) {
1001                 mutex_unlock(&dev->mutex);
1002                 return -ENODEV;
1003         }
1004
1005         if (enabled && !dev->enabled) {
1006                 int ret;
1007
1008                 cdev->next_string_id = 0;
1009                 /*
1010                  * Update values in composite driver's copy of
1011                  * device descriptor.
1012                  */
1013                 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1014                 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1015                 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1016                 cdev->desc.idVendor = idVendor;
1017                 cdev->desc.idProduct = idProduct;
1018                 cdev->desc.bcdDevice = bcdDevice;
1019
1020                 usb_add_config(cdev, &ccg_config_driver, ccg_bind_config);
1021                 dev->enabled = true;
1022                 ret = usb_gadget_connect(cdev->gadget);
1023                 if (ret) {
1024                         dev->enabled = false;
1025                         usb_remove_config(cdev, &ccg_config_driver);
1026                 }
1027         } else if (!enabled && dev->enabled) {
1028                 reset_usb(dev);
1029         } else {
1030                 pr_err("ccg_usb: already %s\n",
1031                         dev->enabled ? "enabled" : "disabled");
1032         }
1033
1034         mutex_unlock(&dev->mutex);
1035         return size;
1036 }
1037
1038 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1039                            char *buf)
1040 {
1041         struct ccg_dev *dev = dev_get_drvdata(pdev);
1042         struct usb_composite_dev *cdev = dev->cdev;
1043         char *state = "DISCONNECTED";
1044         unsigned long flags;
1045
1046         if (!cdev)
1047                 goto out;
1048
1049         spin_lock_irqsave(&cdev->lock, flags);
1050         if (cdev->config)
1051                 state = "CONFIGURED";
1052         else if (dev->connected)
1053                 state = "CONNECTED";
1054         spin_unlock_irqrestore(&cdev->lock, flags);
1055 out:
1056         return sprintf(buf, "%s\n", state);
1057 }
1058
1059 #define DESCRIPTOR_ATTR(field, format_string)                           \
1060 static ssize_t                                                          \
1061 field ## _show(struct device *dev, struct device_attribute *attr,       \
1062                 char *buf)                                              \
1063 {                                                                       \
1064         return sprintf(buf, format_string, device_desc.field);          \
1065 }                                                                       \
1066 static ssize_t                                                          \
1067 field ## _store(struct device *dev, struct device_attribute *attr,      \
1068                 const char *buf, size_t size)                           \
1069 {                                                                       \
1070         int value;                                                      \
1071         if (sscanf(buf, format_string, &value) == 1) {                  \
1072                 device_desc.field = value;                              \
1073                 return size;                                            \
1074         }                                                               \
1075         return -1;                                                      \
1076 }                                                                       \
1077 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1078
1079 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1080 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1081 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1082
1083 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1084                                                  functions_store);
1085 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1086 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1087
1088 static struct device_attribute *ccg_usb_attributes[] = {
1089         &dev_attr_bDeviceClass,
1090         &dev_attr_bDeviceSubClass,
1091         &dev_attr_bDeviceProtocol,
1092         &dev_attr_functions,
1093         &dev_attr_enable,
1094         &dev_attr_state,
1095         NULL
1096 };
1097
1098 /*-------------------------------------------------------------------------*/
1099 /* Composite driver */
1100
1101 static int ccg_bind_config(struct usb_configuration *c)
1102 {
1103         struct ccg_dev *dev = _ccg_dev;
1104         return ccg_bind_enabled_functions(dev, c);
1105 }
1106
1107 static void ccg_unbind_config(struct usb_configuration *c)
1108 {
1109         struct ccg_dev *dev = _ccg_dev;
1110
1111         ccg_unbind_enabled_functions(dev, c);
1112
1113         usb_ep_autoconfig_reset(dev->cdev->gadget);
1114 }
1115
1116 static int ccg_bind(struct usb_composite_dev *cdev)
1117 {
1118         struct ccg_dev *dev = _ccg_dev;
1119         struct usb_gadget       *gadget = cdev->gadget;
1120         int                     gcnum, ret;
1121
1122         /*
1123          * Start disconnected. Userspace will connect the gadget once
1124          * it is done configuring the functions.
1125          */
1126         usb_gadget_disconnect(gadget);
1127
1128         ret = ccg_init_functions(dev->functions, cdev);
1129         if (ret)
1130                 return ret;
1131
1132         gcnum = usb_gadget_controller_number(gadget);
1133         if (gcnum >= 0)
1134                 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1135         else {
1136                 pr_warn("%s: controller '%s' not recognized\n",
1137                         longname, gadget->name);
1138                 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1139         }
1140
1141         usb_gadget_set_selfpowered(gadget);
1142         dev->cdev = cdev;
1143
1144         return 0;
1145 }
1146
1147 static int ccg_usb_unbind(struct usb_composite_dev *cdev)
1148 {
1149         struct ccg_dev *dev = _ccg_dev;
1150
1151         cancel_work_sync(&dev->work);
1152         ccg_cleanup_functions(dev->functions);
1153         return 0;
1154 }
1155
1156 static struct usb_composite_driver ccg_usb_driver = {
1157         .name           = "configurable_usb",
1158         .dev            = &device_desc,
1159         .bind           = ccg_bind,
1160         .unbind         = ccg_usb_unbind,
1161         .needs_serial   = true,
1162         .iManufacturer  = "Linux Foundation",
1163         .iProduct       = longname,
1164         .iSerialNumber  = "1234567890123456",
1165 };
1166
1167 static int ccg_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1168 {
1169         struct ccg_dev          *dev = _ccg_dev;
1170         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1171         struct usb_request              *req = cdev->req;
1172         struct ccg_usb_function *f;
1173         int value = -EOPNOTSUPP;
1174         unsigned long flags;
1175
1176         req->zero = 0;
1177         req->complete = composite_setup_complete;
1178         req->length = 0;
1179         gadget->ep0->driver_data = cdev;
1180
1181         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1182                 if (f->ctrlrequest) {
1183                         value = f->ctrlrequest(f, cdev, c);
1184                         if (value >= 0)
1185                                 break;
1186                 }
1187         }
1188
1189         if (value < 0)
1190                 value = composite_setup(gadget, c);
1191
1192         spin_lock_irqsave(&cdev->lock, flags);
1193         if (!dev->connected) {
1194                 dev->connected = 1;
1195                 schedule_work(&dev->work);
1196         } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1197                                                 cdev->config) {
1198                 schedule_work(&dev->work);
1199         }
1200         spin_unlock_irqrestore(&cdev->lock, flags);
1201
1202         return value;
1203 }
1204
1205 static void ccg_disconnect(struct usb_gadget *gadget)
1206 {
1207         struct ccg_dev *dev = _ccg_dev;
1208         struct usb_composite_dev *cdev = get_gadget_data(gadget);
1209         unsigned long flags;
1210
1211         composite_disconnect(gadget);
1212
1213         spin_lock_irqsave(&cdev->lock, flags);
1214         dev->connected = 0;
1215         schedule_work(&dev->work);
1216         spin_unlock_irqrestore(&cdev->lock, flags);
1217 }
1218
1219 static int ccg_create_device(struct ccg_dev *dev)
1220 {
1221         struct device_attribute **attrs = ccg_usb_attributes;
1222         struct device_attribute *attr;
1223         int err;
1224
1225         dev->dev = device_create(ccg_class, NULL, MKDEV(0, 0), NULL, "ccg0");
1226         if (IS_ERR(dev->dev))
1227                 return PTR_ERR(dev->dev);
1228
1229         dev_set_drvdata(dev->dev, dev);
1230
1231         while ((attr = *attrs++)) {
1232                 err = device_create_file(dev->dev, attr);
1233                 if (err) {
1234                         device_destroy(ccg_class, dev->dev->devt);
1235                         return err;
1236                 }
1237         }
1238         return 0;
1239 }
1240
1241
1242 static int __init init(void)
1243 {
1244         struct ccg_dev *dev;
1245         int err;
1246
1247         ccg_class = class_create(THIS_MODULE, "ccg_usb");
1248         if (IS_ERR(ccg_class))
1249                 return PTR_ERR(ccg_class);
1250
1251         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1252         if (!dev) {
1253                 class_destroy(ccg_class);
1254                 return -ENOMEM;
1255         }
1256
1257         dev->functions = supported_functions;
1258         INIT_LIST_HEAD(&dev->enabled_functions);
1259         INIT_WORK(&dev->work, ccg_work);
1260         mutex_init(&dev->mutex);
1261
1262         err = ccg_create_device(dev);
1263         if (err) {
1264                 class_destroy(ccg_class);
1265                 kfree(dev);
1266                 return err;
1267         }
1268
1269         _ccg_dev = dev;
1270
1271         /* Override composite driver functions */
1272         composite_driver.setup = ccg_setup;
1273         composite_driver.disconnect = ccg_disconnect;
1274
1275         err = usb_composite_probe(&ccg_usb_driver);
1276         if (err) {
1277                 class_destroy(ccg_class);
1278                 kfree(dev);
1279         }
1280
1281         return err;
1282 }
1283 module_init(init);
1284
1285 static void __exit cleanup(void)
1286 {
1287         usb_composite_unregister(&ccg_usb_driver);
1288         class_destroy(ccg_class);
1289         kfree(_ccg_dev);
1290         _ccg_dev = NULL;
1291 }
1292 module_exit(cleanup);