]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ccg/ccg.c
Merge branch 'pl022' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[karo-tx-linux.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 <linux/usb/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 "../../usb/gadget/usbstring.c"
48 #include "../../usb/gadget/config.c"
49 #include "../../usb/gadget/epautoconf.c"
50 #include "../../usb/gadget/composite.c"
51
52 #include "../../usb/gadget/f_mass_storage.c"
53 #include "../../usb/gadget/u_serial.c"
54 #include "../../usb/gadget/f_acm.c"
55 #define USB_ETH_RNDIS y
56 #include "../../usb/gadget/f_rndis.c"
57 #include "../../usb/gadget/rndis.c"
58 #include "../../usb/gadget/u_ether.c"
59 #include "../../usb/gadget/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: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
568                 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
569                 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
570
571         ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
572         if (ret) {
573                 pr_err("%s: gether_setup failed\n", __func__);
574                 return ret;
575         }
576
577         if (rndis->wceis) {
578                 /* "Wireless" RNDIS; auto-detected by Windows */
579                 rndis_iad_descriptor.bFunctionClass =
580                                                 USB_CLASS_WIRELESS_CONTROLLER;
581                 rndis_iad_descriptor.bFunctionSubClass = 0x01;
582                 rndis_iad_descriptor.bFunctionProtocol = 0x03;
583                 rndis_control_intf.bInterfaceClass =
584                                                 USB_CLASS_WIRELESS_CONTROLLER;
585                 rndis_control_intf.bInterfaceSubClass =  0x01;
586                 rndis_control_intf.bInterfaceProtocol =  0x03;
587         }
588
589         return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
590                                            rndis->manufacturer);
591 }
592
593 static void rndis_function_unbind_config(struct ccg_usb_function *f,
594                                                 struct usb_configuration *c)
595 {
596         gether_cleanup();
597 }
598
599 static ssize_t rndis_manufacturer_show(struct device *dev,
600                 struct device_attribute *attr, char *buf)
601 {
602         struct ccg_usb_function *f = dev_get_drvdata(dev);
603         struct rndis_function_config *config = f->config;
604         return sprintf(buf, "%s\n", config->manufacturer);
605 }
606
607 static ssize_t rndis_manufacturer_store(struct device *dev,
608                 struct device_attribute *attr, const char *buf, size_t size)
609 {
610         struct ccg_usb_function *f = dev_get_drvdata(dev);
611         struct rndis_function_config *config = f->config;
612
613         if (size >= sizeof(config->manufacturer))
614                 return -EINVAL;
615         memcpy(config->manufacturer, buf, size);
616         config->manufacturer[size] = 0;
617
618         return size;
619 }
620
621 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
622                                                     rndis_manufacturer_store);
623
624 static ssize_t rndis_wceis_show(struct device *dev,
625                 struct device_attribute *attr, char *buf)
626 {
627         struct ccg_usb_function *f = dev_get_drvdata(dev);
628         struct rndis_function_config *config = f->config;
629         return sprintf(buf, "%d\n", config->wceis);
630 }
631
632 static ssize_t rndis_wceis_store(struct device *dev,
633                 struct device_attribute *attr, const char *buf, size_t size)
634 {
635         struct ccg_usb_function *f = dev_get_drvdata(dev);
636         struct rndis_function_config *config = f->config;
637         int value;
638         int ret;
639
640         ret = kstrtoint(buf, 10, &value);
641         if (ret)
642                 return ret;
643
644         config->wceis = value;
645
646         return size;
647 }
648
649 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
650                                              rndis_wceis_store);
651
652 static ssize_t rndis_ethaddr_show(struct device *dev,
653                 struct device_attribute *attr, char *buf)
654 {
655         struct ccg_usb_function *f = dev_get_drvdata(dev);
656         struct rndis_function_config *rndis = f->config;
657         return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
658                 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
659                 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
660 }
661
662 static ssize_t rndis_ethaddr_store(struct device *dev,
663                 struct device_attribute *attr, const char *buf, size_t size)
664 {
665         struct ccg_usb_function *f = dev_get_drvdata(dev);
666         struct rndis_function_config *rndis = f->config;
667         unsigned char tmp[6];
668
669         if (sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
670                    tmp + 0, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5) !=
671             ETH_ALEN)
672                 return -EINVAL;
673
674         memcpy(rndis->ethaddr, tmp, ETH_ALEN);
675
676         return ETH_ALEN;
677
678 }
679
680 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
681                                                rndis_ethaddr_store);
682
683 static ssize_t rndis_vendorID_show(struct device *dev,
684                 struct device_attribute *attr, char *buf)
685 {
686         struct ccg_usb_function *f = dev_get_drvdata(dev);
687         struct rndis_function_config *config = f->config;
688         return sprintf(buf, "%04x\n", config->vendorID);
689 }
690
691 static ssize_t rndis_vendorID_store(struct device *dev,
692                 struct device_attribute *attr, const char *buf, size_t size)
693 {
694         struct ccg_usb_function *f = dev_get_drvdata(dev);
695         struct rndis_function_config *config = f->config;
696         int value;
697         int ret;
698
699         ret = kstrtou32(buf, 16, &value);
700         if (ret)
701                 return ret;
702
703         config->vendorID = value;
704
705         return size;
706 }
707
708 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
709                                                 rndis_vendorID_store);
710
711 static struct device_attribute *rndis_function_attributes[] = {
712         &dev_attr_manufacturer,
713         &dev_attr_wceis,
714         &dev_attr_ethaddr,
715         &dev_attr_vendorID,
716         NULL
717 };
718
719 static struct ccg_usb_function rndis_function = {
720         .name           = "rndis",
721         .init           = rndis_function_init,
722         .cleanup        = rndis_function_cleanup,
723         .bind_config    = rndis_function_bind_config,
724         .unbind_config  = rndis_function_unbind_config,
725         .attributes     = rndis_function_attributes,
726 };
727
728 static int mass_storage_function_init(struct ccg_usb_function *f,
729                                         struct usb_composite_dev *cdev)
730 {
731         struct fsg_config fsg;
732         struct fsg_common *common;
733         int err;
734
735         memset(&fsg, 0, sizeof fsg);
736         fsg.nluns = 1;
737         fsg.luns[0].removable = 1;
738         fsg.vendor_name = iManufacturer;
739         fsg.product_name = iProduct;
740
741         common = fsg_common_init(NULL, cdev, &fsg);
742         if (IS_ERR(common))
743                 return PTR_ERR(common);
744
745         err = sysfs_create_link(&f->dev->kobj,
746                                 &common->luns[0].dev.kobj,
747                                 "lun");
748         if (err) {
749                 fsg_common_put(common);
750                 return err;
751         }
752
753         f->config = common;
754         return 0;
755 }
756
757 static void mass_storage_function_cleanup(struct ccg_usb_function *f)
758 {
759         fsg_common_put(f->config);
760         f->config = NULL;
761 }
762
763 static int mass_storage_function_bind_config(struct ccg_usb_function *f,
764                                              struct usb_configuration *c)
765 {
766         struct fsg_common *common = f->config;
767         return fsg_bind_config(c->cdev, c, common);
768 }
769
770 static struct ccg_usb_function mass_storage_function = {
771         .name           = "mass_storage",
772         .init           = mass_storage_function_init,
773         .cleanup        = mass_storage_function_cleanup,
774         .bind_config    = mass_storage_function_bind_config,
775 };
776
777 static struct ccg_usb_function *supported_functions[] = {
778         &functionfs_function,
779         &acm_function,
780         &rndis_function,
781         &mass_storage_function,
782         NULL
783 };
784
785
786 static int ccg_init_functions(struct ccg_usb_function **functions,
787                                   struct usb_composite_dev *cdev)
788 {
789         struct ccg_dev *dev = _ccg_dev;
790         struct ccg_usb_function *f;
791         struct device_attribute **attrs;
792         struct device_attribute *attr;
793         int err;
794         int index = 0;
795
796         for (; (f = *functions++); index++) {
797                 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
798                 if (!f->dev_name) {
799                         pr_err("%s: Failed to alloc name %s", __func__,
800                                f->name);
801                         err = -ENOMEM;
802                         goto err_alloc;
803                 }
804                 f->dev = device_create(ccg_class, dev->dev,
805                                 MKDEV(0, index), f, f->dev_name);
806                 if (IS_ERR(f->dev)) {
807                         pr_err("%s: Failed to create dev %s", __func__,
808                                                         f->dev_name);
809                         err = PTR_ERR(f->dev);
810                         f->dev = NULL;
811                         goto err_create;
812                 }
813
814                 if (f->init) {
815                         err = f->init(f, cdev);
816                         if (err) {
817                                 pr_err("%s: Failed to init %s", __func__,
818                                                                 f->name);
819                                 goto err_out;
820                         }
821                 }
822
823                 attrs = f->attributes;
824                 if (attrs) {
825                         while ((attr = *attrs++) && !err)
826                                 err = device_create_file(f->dev, attr);
827                 }
828                 if (err) {
829                         pr_err("%s: Failed to create function %s attributes",
830                                         __func__, f->name);
831                         goto err_uninit;
832                 }
833         }
834         return 0;
835
836 err_uninit:
837         if (f->cleanup)
838                 f->cleanup(f);
839 err_out:
840         device_destroy(ccg_class, f->dev->devt);
841         f->dev = NULL;
842 err_create:
843         kfree(f->dev_name);
844 err_alloc:
845         return err;
846 }
847
848 static void ccg_cleanup_functions(struct ccg_usb_function **functions)
849 {
850         struct ccg_usb_function *f;
851
852         while (*functions) {
853                 f = *functions++;
854
855                 if (f->dev) {
856                         if (f->cleanup)
857                                 f->cleanup(f);
858                         device_destroy(ccg_class, f->dev->devt);
859                         kfree(f->dev_name);
860                 }
861         }
862 }
863
864 static int ccg_bind_enabled_functions(struct ccg_dev *dev,
865                                       struct usb_configuration *c)
866 {
867         struct ccg_usb_function *f;
868         int ret;
869
870         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
871                 ret = f->bind_config(f, c);
872                 if (ret) {
873                         pr_err("%s: %s failed", __func__, f->name);
874                         return ret;
875                 }
876         }
877         return 0;
878 }
879
880 static void ccg_unbind_enabled_functions(struct ccg_dev *dev,
881                                          struct usb_configuration *c)
882 {
883         struct ccg_usb_function *f;
884
885         list_for_each_entry(f, &dev->enabled_functions, enabled_list)
886                 if (f->unbind_config)
887                         f->unbind_config(f, c);
888 }
889
890 static int ccg_enable_function(struct ccg_dev *dev, char *name)
891 {
892         struct ccg_usb_function **functions = dev->functions;
893         struct ccg_usb_function *f;
894         while ((f = *functions++)) {
895                 if (!strcmp(name, f->name)) {
896                         list_add_tail(&f->enabled_list,
897                                                 &dev->enabled_functions);
898                         return 0;
899                 }
900         }
901         return -EINVAL;
902 }
903
904 /*-------------------------------------------------------------------------*/
905 /* /sys/class/ccg_usb/ccg%d/ interface */
906
907 static ssize_t
908 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
909 {
910         struct ccg_dev *dev = dev_get_drvdata(pdev);
911         struct ccg_usb_function *f;
912         char *buff = buf;
913         int i;
914
915         mutex_lock(&dev->mutex);
916
917         list_for_each_entry(f, &dev->enabled_functions, enabled_list)
918                 buff += sprintf(buff, "%s,", f->name);
919         for (i = 0; i < dev->max_func_num; i++)
920                 if (dev->ffs_tab[i].used)
921                         buff += sprintf(buff, "%s", dev->ffs_tab[i].name);
922
923         mutex_unlock(&dev->mutex);
924
925         if (buff != buf)
926                 *(buff-1) = '\n';
927         return buff - buf;
928 }
929
930 static ssize_t
931 functions_store(struct device *pdev, struct device_attribute *attr,
932                                const char *buff, size_t size)
933 {
934         struct ccg_dev *dev = dev_get_drvdata(pdev);
935         char *name;
936         char buf[256], *b;
937         int err, i;
938         bool functionfs_enabled;
939
940         buff = skip_spaces(buff);
941         if (!*buff)
942                 return -EINVAL;
943
944         mutex_lock(&dev->mutex);
945
946         if (dev->enabled) {
947                 mutex_unlock(&dev->mutex);
948                 return -EBUSY;
949         }
950
951         INIT_LIST_HEAD(&dev->enabled_functions);
952         functionfs_enabled = false;
953         for (i = 0; i < dev->max_func_num; i++)
954                 dev->ffs_tab[i].used = false;
955
956         strlcpy(buf, buff, sizeof(buf));
957         b = strim(buf);
958
959         while (b) {
960                 struct ffs_obj *user_func;
961
962                 name = strsep(&b, ",");
963                 /* handle FunctionFS implicitly */
964                 if (!strcmp(name, functionfs_function.name)) {
965                         pr_err("ccg_usb: Cannot explicitly enable '%s'", name);
966                         continue;
967                 }
968                 user_func = functionfs_find_dev(dev, name);
969                 if (user_func)
970                         name = functionfs_function.name;
971                 err = 0;
972                 if (!user_func || !functionfs_enabled)
973                         err = ccg_enable_function(dev, name);
974                 if (err)
975                         pr_err("ccg_usb: Cannot enable '%s'", name);
976                 else if (user_func) {
977                         user_func->used = true;
978                         dev->func_num++;
979                         functionfs_enabled = true;
980                 }
981         }
982
983         mutex_unlock(&dev->mutex);
984
985         return size;
986 }
987
988 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
989                            char *buf)
990 {
991         struct ccg_dev *dev = dev_get_drvdata(pdev);
992         return sprintf(buf, "%d\n", dev->enabled);
993 }
994
995 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
996                             const char *buff, size_t size)
997 {
998         struct ccg_dev *dev = dev_get_drvdata(pdev);
999         struct usb_composite_dev *cdev = dev->cdev;
1000         int enabled = 0;
1001
1002         mutex_lock(&dev->mutex);
1003         sscanf(buff, "%d", &enabled);
1004         if (enabled && dev->func_num && !functionfs_all_ready(dev)) {
1005                 mutex_unlock(&dev->mutex);
1006                 return -ENODEV;
1007         }
1008
1009         if (enabled && !dev->enabled) {
1010                 int ret;
1011
1012                 cdev->next_string_id = 0;
1013                 /*
1014                  * Update values in composite driver's copy of
1015                  * device descriptor.
1016                  */
1017                 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1018                 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1019                 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1020                 cdev->desc.idVendor = idVendor;
1021                 cdev->desc.idProduct = idProduct;
1022                 cdev->desc.bcdDevice = bcdDevice;
1023
1024                 usb_add_config(cdev, &ccg_config_driver, ccg_bind_config);
1025                 dev->enabled = true;
1026                 ret = usb_gadget_connect(cdev->gadget);
1027                 if (ret) {
1028                         dev->enabled = false;
1029                         usb_remove_config(cdev, &ccg_config_driver);
1030                 }
1031         } else if (!enabled && dev->enabled) {
1032                 reset_usb(dev);
1033         } else {
1034                 pr_err("ccg_usb: already %s\n",
1035                         dev->enabled ? "enabled" : "disabled");
1036         }
1037
1038         mutex_unlock(&dev->mutex);
1039         return size;
1040 }
1041
1042 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1043                            char *buf)
1044 {
1045         struct ccg_dev *dev = dev_get_drvdata(pdev);
1046         struct usb_composite_dev *cdev = dev->cdev;
1047         char *state = "DISCONNECTED";
1048         unsigned long flags;
1049
1050         if (!cdev)
1051                 goto out;
1052
1053         spin_lock_irqsave(&cdev->lock, flags);
1054         if (cdev->config)
1055                 state = "CONFIGURED";
1056         else if (dev->connected)
1057                 state = "CONNECTED";
1058         spin_unlock_irqrestore(&cdev->lock, flags);
1059 out:
1060         return sprintf(buf, "%s\n", state);
1061 }
1062
1063 #define DESCRIPTOR_ATTR(field, format_string)                           \
1064 static ssize_t                                                          \
1065 field ## _show(struct device *dev, struct device_attribute *attr,       \
1066                 char *buf)                                              \
1067 {                                                                       \
1068         return sprintf(buf, format_string, device_desc.field);          \
1069 }                                                                       \
1070 static ssize_t                                                          \
1071 field ## _store(struct device *dev, struct device_attribute *attr,      \
1072                 const char *buf, size_t size)                           \
1073 {                                                                       \
1074         int value;                                                      \
1075         if (sscanf(buf, format_string, &value) == 1) {                  \
1076                 device_desc.field = value;                              \
1077                 return size;                                            \
1078         }                                                               \
1079         return -1;                                                      \
1080 }                                                                       \
1081 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1082
1083 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1084 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1085 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1086
1087 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1088                                                  functions_store);
1089 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1090 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1091
1092 static struct device_attribute *ccg_usb_attributes[] = {
1093         &dev_attr_bDeviceClass,
1094         &dev_attr_bDeviceSubClass,
1095         &dev_attr_bDeviceProtocol,
1096         &dev_attr_functions,
1097         &dev_attr_enable,
1098         &dev_attr_state,
1099         NULL
1100 };
1101
1102 /*-------------------------------------------------------------------------*/
1103 /* Composite driver */
1104
1105 static int ccg_bind_config(struct usb_configuration *c)
1106 {
1107         struct ccg_dev *dev = _ccg_dev;
1108         int ret = 0;
1109
1110         ret = ccg_bind_enabled_functions(dev, c);
1111         if (ret)
1112                 return ret;
1113
1114         return 0;
1115 }
1116
1117 static void ccg_unbind_config(struct usb_configuration *c)
1118 {
1119         struct ccg_dev *dev = _ccg_dev;
1120
1121         ccg_unbind_enabled_functions(dev, c);
1122
1123         usb_ep_autoconfig_reset(dev->cdev->gadget);
1124 }
1125
1126 static int ccg_bind(struct usb_composite_dev *cdev)
1127 {
1128         struct ccg_dev *dev = _ccg_dev;
1129         struct usb_gadget       *gadget = cdev->gadget;
1130         int                     gcnum, ret;
1131
1132         /*
1133          * Start disconnected. Userspace will connect the gadget once
1134          * it is done configuring the functions.
1135          */
1136         usb_gadget_disconnect(gadget);
1137
1138         ret = ccg_init_functions(dev->functions, cdev);
1139         if (ret)
1140                 return ret;
1141
1142         gcnum = usb_gadget_controller_number(gadget);
1143         if (gcnum >= 0)
1144                 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1145         else {
1146                 pr_warning("%s: controller '%s' not recognized\n",
1147                         longname, gadget->name);
1148                 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1149         }
1150
1151         usb_gadget_set_selfpowered(gadget);
1152         dev->cdev = cdev;
1153
1154         return 0;
1155 }
1156
1157 static int ccg_usb_unbind(struct usb_composite_dev *cdev)
1158 {
1159         struct ccg_dev *dev = _ccg_dev;
1160
1161         cancel_work_sync(&dev->work);
1162         ccg_cleanup_functions(dev->functions);
1163         return 0;
1164 }
1165
1166 static struct usb_composite_driver ccg_usb_driver = {
1167         .name           = "configurable_usb",
1168         .dev            = &device_desc,
1169         .unbind         = ccg_usb_unbind,
1170         .needs_serial   = true,
1171         .iManufacturer  = "Linux Foundation",
1172         .iProduct       = longname,
1173         .iSerialNumber  = "1234567890123456",
1174 };
1175
1176 static int ccg_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1177 {
1178         struct ccg_dev          *dev = _ccg_dev;
1179         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1180         struct usb_request              *req = cdev->req;
1181         struct ccg_usb_function *f;
1182         int value = -EOPNOTSUPP;
1183         unsigned long flags;
1184
1185         req->zero = 0;
1186         req->complete = composite_setup_complete;
1187         req->length = 0;
1188         gadget->ep0->driver_data = cdev;
1189
1190         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1191                 if (f->ctrlrequest) {
1192                         value = f->ctrlrequest(f, cdev, c);
1193                         if (value >= 0)
1194                                 break;
1195                 }
1196         }
1197
1198         if (value < 0)
1199                 value = composite_setup(gadget, c);
1200
1201         spin_lock_irqsave(&cdev->lock, flags);
1202         if (!dev->connected) {
1203                 dev->connected = 1;
1204                 schedule_work(&dev->work);
1205         } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1206                                                 cdev->config) {
1207                 schedule_work(&dev->work);
1208         }
1209         spin_unlock_irqrestore(&cdev->lock, flags);
1210
1211         return value;
1212 }
1213
1214 static void ccg_disconnect(struct usb_gadget *gadget)
1215 {
1216         struct ccg_dev *dev = _ccg_dev;
1217         struct usb_composite_dev *cdev = get_gadget_data(gadget);
1218         unsigned long flags;
1219
1220         composite_disconnect(gadget);
1221
1222         spin_lock_irqsave(&cdev->lock, flags);
1223         dev->connected = 0;
1224         schedule_work(&dev->work);
1225         spin_unlock_irqrestore(&cdev->lock, flags);
1226 }
1227
1228 static int ccg_create_device(struct ccg_dev *dev)
1229 {
1230         struct device_attribute **attrs = ccg_usb_attributes;
1231         struct device_attribute *attr;
1232         int err;
1233
1234         dev->dev = device_create(ccg_class, NULL, MKDEV(0, 0), NULL, "ccg0");
1235         if (IS_ERR(dev->dev))
1236                 return PTR_ERR(dev->dev);
1237
1238         dev_set_drvdata(dev->dev, dev);
1239
1240         while ((attr = *attrs++)) {
1241                 err = device_create_file(dev->dev, attr);
1242                 if (err) {
1243                         device_destroy(ccg_class, dev->dev->devt);
1244                         return err;
1245                 }
1246         }
1247         return 0;
1248 }
1249
1250
1251 static int __init init(void)
1252 {
1253         struct ccg_dev *dev;
1254         int err;
1255
1256         ccg_class = class_create(THIS_MODULE, "ccg_usb");
1257         if (IS_ERR(ccg_class))
1258                 return PTR_ERR(ccg_class);
1259
1260         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1261         if (!dev)
1262                 return -ENOMEM;
1263
1264         dev->functions = supported_functions;
1265         INIT_LIST_HEAD(&dev->enabled_functions);
1266         INIT_WORK(&dev->work, ccg_work);
1267         mutex_init(&dev->mutex);
1268
1269         err = ccg_create_device(dev);
1270         if (err) {
1271                 class_destroy(ccg_class);
1272                 kfree(dev);
1273                 return err;
1274         }
1275
1276         _ccg_dev = dev;
1277
1278         /* Override composite driver functions */
1279         composite_driver.setup = ccg_setup;
1280         composite_driver.disconnect = ccg_disconnect;
1281
1282         err = usb_composite_probe(&ccg_usb_driver, ccg_bind);
1283         if (err) {
1284                 class_destroy(ccg_class);
1285                 kfree(dev);
1286         }
1287
1288         return err;
1289 }
1290 module_init(init);
1291
1292 static void __exit cleanup(void)
1293 {
1294         usb_composite_unregister(&ccg_usb_driver);
1295         class_destroy(ccg_class);
1296         kfree(_ccg_dev);
1297         _ccg_dev = NULL;
1298 }
1299 module_exit(cleanup);