f1264747677ece818c56d4319a68043b4089d1a8
[firefly-linux-kernel-4.4.55.git] / drivers / usb / gadget / android.c
1 /*
2  * Gadget Driver for Android
3  *
4  * Copyright (C) 2008 Google, Inc.
5  * Author: Mike Lockwood <lockwood@android.com>
6  *         Benoit Goby <benoit@android.com>
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/fs.h>
22 #include <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/utsname.h>
25 #include <linux/platform_device.h>
26
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/composite.h>
29 #include <linux/usb/gadget.h>
30
31 #include "gadget_chips.h"
32
33 #include "f_fs.c"
34 #include "f_audio_source.c"
35 #include "f_midi.c"
36 #include "f_mass_storage.c"
37 #include "f_mtp.c"
38 #include "f_accessory.c"
39 #define USB_ETH_RNDIS y
40 #include "f_rndis.c"
41 #include "rndis.c"
42 #include "u_ether.c"
43
44 MODULE_AUTHOR("Mike Lockwood");
45 MODULE_DESCRIPTION("Android Composite USB Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION("1.0");
48
49 static const char longname[] = "Gadget Android";
50
51 /* Default vendor and product IDs, overridden by userspace */
52 #define VENDOR_ID               0x2207
53 #define PRODUCT_ID              0x2910
54
55 /* f_midi configuration */
56 #define MIDI_INPUT_PORTS    1
57 #define MIDI_OUTPUT_PORTS   1
58 #define MIDI_BUFFER_SIZE    256
59 #define MIDI_QUEUE_LENGTH   32
60
61 struct android_usb_function {
62         char *name;
63         void *config;
64
65         struct device *dev;
66         char *dev_name;
67         struct device_attribute **attributes;
68
69         /* for android_dev.enabled_functions */
70         struct list_head enabled_list;
71
72         /* Optional: initialization during gadget bind */
73         int (*init)(struct android_usb_function *, struct usb_composite_dev *);
74         /* Optional: cleanup during gadget unbind */
75         void (*cleanup)(struct android_usb_function *);
76         /* Optional: called when the function is added the list of
77          *              enabled functions */
78         void (*enable)(struct android_usb_function *);
79         /* Optional: called when it is removed */
80         void (*disable)(struct android_usb_function *);
81
82         int (*bind_config)(struct android_usb_function *,
83                            struct usb_configuration *);
84
85         /* Optional: called when the configuration is removed */
86         void (*unbind_config)(struct android_usb_function *,
87                               struct usb_configuration *);
88         /* Optional: handle ctrl requests before the device is configured */
89         int (*ctrlrequest)(struct android_usb_function *,
90                                         struct usb_composite_dev *,
91                                         const struct usb_ctrlrequest *);
92 };
93
94 struct android_dev {
95         struct android_usb_function **functions;
96         struct list_head enabled_functions;
97         struct usb_composite_dev *cdev;
98         struct device *dev;
99
100         void (*setup_complete)(struct usb_ep *ep,
101                                 struct usb_request *req);
102
103         bool enabled;
104         int disable_depth;
105         struct mutex mutex;
106         bool connected;
107         bool sw_connected;
108         struct work_struct work;
109         char ffs_aliases[256];
110 };
111
112 static struct class *android_class;
113 static struct android_dev *_android_dev;
114 static int android_bind_config(struct usb_configuration *c);
115 static void android_unbind_config(struct usb_configuration *c);
116
117 /* string IDs are assigned dynamically */
118 #define STRING_MANUFACTURER_IDX         0
119 #define STRING_PRODUCT_IDX              1
120 #define STRING_SERIAL_IDX               2
121
122 static char manufacturer_string[256];
123 static char product_string[256];
124 static char serial_string[256];
125
126 /* String Table */
127 static struct usb_string strings_dev[] = {
128         [STRING_MANUFACTURER_IDX].s = manufacturer_string,
129         [STRING_PRODUCT_IDX].s = product_string,
130         [STRING_SERIAL_IDX].s = serial_string,
131         {  }                    /* end of list */
132 };
133
134 static struct usb_gadget_strings stringtab_dev = {
135         .language       = 0x0409,       /* en-us */
136         .strings        = strings_dev,
137 };
138
139 static struct usb_gadget_strings *dev_strings[] = {
140         &stringtab_dev,
141         NULL,
142 };
143
144 static struct usb_device_descriptor device_desc = {
145         .bLength              = sizeof(device_desc),
146         .bDescriptorType      = USB_DT_DEVICE,
147         .bcdUSB               = __constant_cpu_to_le16(0x0200),
148         .bDeviceClass         = USB_CLASS_PER_INTERFACE,
149         .idVendor             = __constant_cpu_to_le16(VENDOR_ID),
150         .idProduct            = __constant_cpu_to_le16(PRODUCT_ID),
151         .bcdDevice            = __constant_cpu_to_le16(0xffff),
152         .bNumConfigurations   = 1,
153 };
154
155 static struct usb_configuration android_config_driver = {
156         .label          = "android",
157         .unbind         = android_unbind_config,
158         .bConfigurationValue = 1,
159         .bmAttributes   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
160         .MaxPower       = 500, /* 500ma */
161 };
162
163 static void android_work(struct work_struct *data)
164 {
165         struct android_dev *dev = container_of(data, struct android_dev, work);
166         struct usb_composite_dev *cdev = dev->cdev;
167         char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
168         char *connected[2]    = { "USB_STATE=CONNECTED", NULL };
169         char *configured[2]   = { "USB_STATE=CONFIGURED", NULL };
170         char **uevent_envp = NULL;
171         unsigned long flags;
172
173         spin_lock_irqsave(&cdev->lock, flags);
174         if (cdev->config)
175                 uevent_envp = configured;
176         else if (dev->connected != dev->sw_connected)
177                 uevent_envp = dev->connected ? connected : disconnected;
178         dev->sw_connected = dev->connected;
179         spin_unlock_irqrestore(&cdev->lock, flags);
180
181         if (uevent_envp) {
182                 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
183                 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
184         } else {
185                 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
186                          dev->connected, dev->sw_connected, cdev->config);
187         }
188 }
189
190 static void android_enable(struct android_dev *dev)
191 {
192         struct usb_composite_dev *cdev = dev->cdev;
193
194         if (WARN_ON(!dev->disable_depth))
195                 return;
196
197         if (--dev->disable_depth == 0) {
198                 usb_add_config(cdev, &android_config_driver,
199                                         android_bind_config);
200                 usb_gadget_connect(cdev->gadget);
201         }
202 }
203
204 static void android_disable(struct android_dev *dev)
205 {
206         struct usb_composite_dev *cdev = dev->cdev;
207
208         if (dev->disable_depth++ == 0) {
209                 usb_gadget_disconnect(cdev->gadget);
210                 /* Cancel pending control requests */
211                 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
212                 usb_remove_config(cdev, &android_config_driver);
213         }
214 }
215
216 /*-------------------------------------------------------------------------*/
217 /* Supported functions initialization */
218
219 struct functionfs_config {
220         bool opened;
221         bool enabled;
222         struct ffs_data *data;
223 };
224
225 static int ffs_function_init(struct android_usb_function *f,
226                              struct usb_composite_dev *cdev)
227 {
228         f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
229         if (!f->config)
230                 return -ENOMEM;
231
232         return functionfs_init();
233 }
234
235 static void ffs_function_cleanup(struct android_usb_function *f)
236 {
237         functionfs_cleanup();
238         kfree(f->config);
239 }
240
241 static void ffs_function_enable(struct android_usb_function *f)
242 {
243         struct android_dev *dev = _android_dev;
244         struct functionfs_config *config = f->config;
245
246         config->enabled = true;
247
248         /* Disable the gadget until the function is ready */
249         if (!config->opened)
250                 android_disable(dev);
251 }
252
253 static void ffs_function_disable(struct android_usb_function *f)
254 {
255         struct android_dev *dev = _android_dev;
256         struct functionfs_config *config = f->config;
257
258         config->enabled = false;
259
260         /* Balance the disable that was called in closed_callback */
261         if (!config->opened)
262                 android_enable(dev);
263 }
264
265 static int ffs_function_bind_config(struct android_usb_function *f,
266                                     struct usb_configuration *c)
267 {
268         struct functionfs_config *config = f->config;
269         return functionfs_bind_config(c->cdev, c, config->data);
270 }
271
272 static ssize_t
273 ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
274 {
275         struct android_dev *dev = _android_dev;
276         int ret;
277
278         mutex_lock(&dev->mutex);
279         ret = sprintf(buf, "%s\n", dev->ffs_aliases);
280         mutex_unlock(&dev->mutex);
281
282         return ret;
283 }
284
285 static ssize_t
286 ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
287                                         const char *buf, size_t size)
288 {
289         struct android_dev *dev = _android_dev;
290         char buff[256];
291
292         mutex_lock(&dev->mutex);
293
294         if (dev->enabled) {
295                 mutex_unlock(&dev->mutex);
296                 return -EBUSY;
297         }
298
299         strlcpy(buff, buf, sizeof(buff));
300         strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
301
302         mutex_unlock(&dev->mutex);
303
304         return size;
305 }
306
307 static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
308                                                ffs_aliases_store);
309 static struct device_attribute *ffs_function_attributes[] = {
310         &dev_attr_aliases,
311         NULL
312 };
313
314 static struct android_usb_function ffs_function = {
315         .name           = "ffs",
316         .init           = ffs_function_init,
317         .enable         = ffs_function_enable,
318         .disable        = ffs_function_disable,
319         .cleanup        = ffs_function_cleanup,
320         .bind_config    = ffs_function_bind_config,
321         .attributes     = ffs_function_attributes,
322 };
323
324 static int functionfs_ready_callback(struct ffs_data *ffs)
325 {
326         struct android_dev *dev = _android_dev;
327         struct functionfs_config *config = ffs_function.config;
328         int ret = 0;
329
330         mutex_lock(&dev->mutex);
331
332         ret = functionfs_bind(ffs, dev->cdev);
333         if (ret)
334                 goto err;
335
336         config->data = ffs;
337         config->opened = true;
338
339         if (config->enabled)
340                 android_enable(dev);
341
342 err:
343         mutex_unlock(&dev->mutex);
344         return ret;
345 }
346
347 static void functionfs_closed_callback(struct ffs_data *ffs)
348 {
349         struct android_dev *dev = _android_dev;
350         struct functionfs_config *config = ffs_function.config;
351
352         mutex_lock(&dev->mutex);
353
354         if (config->enabled)
355                 android_disable(dev);
356
357         config->opened = false;
358         config->data = NULL;
359
360         if (!WARN_ON(!ffs->gadget)) {
361                 dev->cdev->next_string_id -= ffs->strings_count;
362         }
363         functionfs_unbind(ffs);
364
365         mutex_unlock(&dev->mutex);
366 }
367
368 static void *functionfs_acquire_dev_callback(const char *dev_name)
369 {
370         return 0;
371 }
372
373 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
374 {
375 }
376
377 #define MAX_ACM_INSTANCES 4
378 struct acm_function_config {
379         int instances;
380         int instances_on;
381         struct usb_function *f_acm[MAX_ACM_INSTANCES];
382         struct usb_function_instance *f_acm_inst[MAX_ACM_INSTANCES];
383 };
384
385 static int
386 acm_function_init(struct android_usb_function *f,
387                 struct usb_composite_dev *cdev)
388 {
389         int i;
390         int ret;
391         struct acm_function_config *config;
392
393         config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
394         if (!config)
395                 return -ENOMEM;
396         f->config = config;
397
398         for (i = 0; i < MAX_ACM_INSTANCES; i++) {
399                 config->f_acm_inst[i] = usb_get_function_instance("acm");
400                 if (IS_ERR(config->f_acm_inst[i])) {
401                         ret = PTR_ERR(config->f_acm_inst[i]);
402                         goto err_usb_get_function_instance;
403                 }
404                 config->f_acm[i] = usb_get_function(config->f_acm_inst[i]);
405                 if (IS_ERR(config->f_acm[i])) {
406                         ret = PTR_ERR(config->f_acm[i]);
407                         goto err_usb_get_function;
408                 }
409         }
410         return 0;
411 err_usb_get_function_instance:
412         while (i-- > 0) {
413                 usb_put_function(config->f_acm[i]);
414 err_usb_get_function:
415                 usb_put_function_instance(config->f_acm_inst[i]);
416         }
417         return ret;
418 }
419
420 static void acm_function_cleanup(struct android_usb_function *f)
421 {
422         int i;
423         struct acm_function_config *config = f->config;
424
425         for (i = 0; i < MAX_ACM_INSTANCES; i++) {
426                 usb_put_function(config->f_acm[i]);
427                 usb_put_function_instance(config->f_acm_inst[i]);
428         }
429         kfree(f->config);
430         f->config = NULL;
431 }
432
433 static int
434 acm_function_bind_config(struct android_usb_function *f,
435                 struct usb_configuration *c)
436 {
437         int i;
438         int ret = 0;
439         struct acm_function_config *config = f->config;
440
441         config->instances_on = config->instances;
442         for (i = 0; i < config->instances_on; i++) {
443                 ret = usb_add_function(c, config->f_acm[i]);
444                 if (ret) {
445                         pr_err("Could not bind acm%u config\n", i);
446                         goto err_usb_add_function;
447                 }
448         }
449
450         return 0;
451
452 err_usb_add_function:
453         while (i-- > 0)
454                 usb_remove_function(c, config->f_acm[i]);
455         return ret;
456 }
457
458 static void acm_function_unbind_config(struct android_usb_function *f,
459                                        struct usb_configuration *c)
460 {
461         int i;
462         struct acm_function_config *config = f->config;
463
464         for (i = 0; i < config->instances_on; i++)
465                 usb_remove_function(c, config->f_acm[i]);
466 }
467
468 static ssize_t acm_instances_show(struct device *dev,
469                 struct device_attribute *attr, char *buf)
470 {
471         struct android_usb_function *f = dev_get_drvdata(dev);
472         struct acm_function_config *config = f->config;
473         return sprintf(buf, "%d\n", config->instances);
474 }
475
476 static ssize_t acm_instances_store(struct device *dev,
477                 struct device_attribute *attr, const char *buf, size_t size)
478 {
479         struct android_usb_function *f = dev_get_drvdata(dev);
480         struct acm_function_config *config = f->config;
481         int value;
482
483         sscanf(buf, "%d", &value);
484         if (value > MAX_ACM_INSTANCES)
485                 value = MAX_ACM_INSTANCES;
486         config->instances = value;
487         return size;
488 }
489
490 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
491                                                  acm_instances_store);
492 static struct device_attribute *acm_function_attributes[] = {
493         &dev_attr_instances,
494         NULL
495 };
496
497 static struct android_usb_function acm_function = {
498         .name           = "acm",
499         .init           = acm_function_init,
500         .cleanup        = acm_function_cleanup,
501         .bind_config    = acm_function_bind_config,
502         .unbind_config  = acm_function_unbind_config,
503         .attributes     = acm_function_attributes,
504 };
505
506
507 static int
508 mtp_function_init(struct android_usb_function *f,
509                 struct usb_composite_dev *cdev)
510 {
511         return mtp_setup();
512 }
513
514 static void mtp_function_cleanup(struct android_usb_function *f)
515 {
516         mtp_cleanup();
517 }
518
519 static int
520 mtp_function_bind_config(struct android_usb_function *f,
521                 struct usb_configuration *c)
522 {
523         return mtp_bind_config(c, false);
524 }
525
526 static int
527 ptp_function_init(struct android_usb_function *f,
528                 struct usb_composite_dev *cdev)
529 {
530         /* nothing to do - initialization is handled by mtp_function_init */
531         return 0;
532 }
533
534 static void ptp_function_cleanup(struct android_usb_function *f)
535 {
536         /* nothing to do - cleanup is handled by mtp_function_cleanup */
537 }
538
539 static int
540 ptp_function_bind_config(struct android_usb_function *f,
541                 struct usb_configuration *c)
542 {
543         return mtp_bind_config(c, true);
544 }
545
546 static int mtp_function_ctrlrequest(struct android_usb_function *f,
547                                         struct usb_composite_dev *cdev,
548                                         const struct usb_ctrlrequest *c)
549 {
550         return mtp_ctrlrequest(cdev, c);
551 }
552
553 static struct android_usb_function mtp_function = {
554         .name           = "mtp",
555         .init           = mtp_function_init,
556         .cleanup        = mtp_function_cleanup,
557         .bind_config    = mtp_function_bind_config,
558         .ctrlrequest    = mtp_function_ctrlrequest,
559 };
560
561 /* PTP function is same as MTP with slightly different interface descriptor */
562 static struct android_usb_function ptp_function = {
563         .name           = "ptp",
564         .init           = ptp_function_init,
565         .cleanup        = ptp_function_cleanup,
566         .bind_config    = ptp_function_bind_config,
567 };
568
569
570 struct rndis_function_config {
571         u8      ethaddr[ETH_ALEN];
572         u32     vendorID;
573         char    manufacturer[256];
574         /* "Wireless" RNDIS; auto-detected by Windows */
575         bool    wceis;
576         struct eth_dev *dev;
577 };
578
579 static int
580 rndis_function_init(struct android_usb_function *f,
581                 struct usb_composite_dev *cdev)
582 {
583         f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
584         if (!f->config)
585                 return -ENOMEM;
586         return 0;
587 }
588
589 static void rndis_function_cleanup(struct android_usb_function *f)
590 {
591         kfree(f->config);
592         f->config = NULL;
593 }
594
595 static int
596 rndis_function_bind_config(struct android_usb_function *f,
597                 struct usb_configuration *c)
598 {
599         int ret;
600         struct eth_dev *dev;
601         struct rndis_function_config *rndis = f->config;
602
603         if (!rndis) {
604                 pr_err("%s: rndis_pdata\n", __func__);
605                 return -1;
606         }
607
608         pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
609                 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
610                 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
611
612         dev = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
613         if (IS_ERR(dev)) {
614                 ret = PTR_ERR(dev);
615                 pr_err("%s: gether_setup failed\n", __func__);
616                 return ret;
617         }
618         rndis->dev = dev;
619
620         if (rndis->wceis) {
621                 /* "Wireless" RNDIS; auto-detected by Windows */
622                 rndis_iad_descriptor.bFunctionClass =
623                                                 USB_CLASS_WIRELESS_CONTROLLER;
624                 rndis_iad_descriptor.bFunctionSubClass = 0x01;
625                 rndis_iad_descriptor.bFunctionProtocol = 0x03;
626                 rndis_control_intf.bInterfaceClass =
627                                                 USB_CLASS_WIRELESS_CONTROLLER;
628                 rndis_control_intf.bInterfaceSubClass =  0x01;
629                 rndis_control_intf.bInterfaceProtocol =  0x03;
630         }
631
632         return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
633                                            rndis->manufacturer, rndis->dev);
634 }
635
636 static void rndis_function_unbind_config(struct android_usb_function *f,
637                                                 struct usb_configuration *c)
638 {
639         struct rndis_function_config *rndis = f->config;
640         gether_cleanup(rndis->dev);
641 }
642
643 static ssize_t rndis_manufacturer_show(struct device *dev,
644                 struct device_attribute *attr, char *buf)
645 {
646         struct android_usb_function *f = dev_get_drvdata(dev);
647         struct rndis_function_config *config = f->config;
648         return sprintf(buf, "%s\n", config->manufacturer);
649 }
650
651 static ssize_t rndis_manufacturer_store(struct device *dev,
652                 struct device_attribute *attr, const char *buf, size_t size)
653 {
654         struct android_usb_function *f = dev_get_drvdata(dev);
655         struct rndis_function_config *config = f->config;
656
657         if (size >= sizeof(config->manufacturer))
658                 return -EINVAL;
659         if (sscanf(buf, "%s", config->manufacturer) == 1)
660                 return size;
661         return -1;
662 }
663
664 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
665                                                     rndis_manufacturer_store);
666
667 static ssize_t rndis_wceis_show(struct device *dev,
668                 struct device_attribute *attr, char *buf)
669 {
670         struct android_usb_function *f = dev_get_drvdata(dev);
671         struct rndis_function_config *config = f->config;
672         return sprintf(buf, "%d\n", config->wceis);
673 }
674
675 static ssize_t rndis_wceis_store(struct device *dev,
676                 struct device_attribute *attr, const char *buf, size_t size)
677 {
678         struct android_usb_function *f = dev_get_drvdata(dev);
679         struct rndis_function_config *config = f->config;
680         int value;
681
682         if (sscanf(buf, "%d", &value) == 1) {
683                 config->wceis = value;
684                 return size;
685         }
686         return -EINVAL;
687 }
688
689 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
690                                              rndis_wceis_store);
691
692 static ssize_t rndis_ethaddr_show(struct device *dev,
693                 struct device_attribute *attr, char *buf)
694 {
695         struct android_usb_function *f = dev_get_drvdata(dev);
696         struct rndis_function_config *rndis = f->config;
697         return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
698                 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
699                 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
700 }
701
702 static ssize_t rndis_ethaddr_store(struct device *dev,
703                 struct device_attribute *attr, const char *buf, size_t size)
704 {
705         struct android_usb_function *f = dev_get_drvdata(dev);
706         struct rndis_function_config *rndis = f->config;
707
708         if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
709                     (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
710                     (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
711                     (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
712                 return size;
713         return -EINVAL;
714 }
715
716 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
717                                                rndis_ethaddr_store);
718
719 static ssize_t rndis_vendorID_show(struct device *dev,
720                 struct device_attribute *attr, char *buf)
721 {
722         struct android_usb_function *f = dev_get_drvdata(dev);
723         struct rndis_function_config *config = f->config;
724         return sprintf(buf, "%04x\n", config->vendorID);
725 }
726
727 static ssize_t rndis_vendorID_store(struct device *dev,
728                 struct device_attribute *attr, const char *buf, size_t size)
729 {
730         struct android_usb_function *f = dev_get_drvdata(dev);
731         struct rndis_function_config *config = f->config;
732         int value;
733
734         if (sscanf(buf, "%04x", &value) == 1) {
735                 config->vendorID = value;
736                 return size;
737         }
738         return -EINVAL;
739 }
740
741 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
742                                                 rndis_vendorID_store);
743
744 static struct device_attribute *rndis_function_attributes[] = {
745         &dev_attr_manufacturer,
746         &dev_attr_wceis,
747         &dev_attr_ethaddr,
748         &dev_attr_vendorID,
749         NULL
750 };
751
752 static struct android_usb_function rndis_function = {
753         .name           = "rndis",
754         .init           = rndis_function_init,
755         .cleanup        = rndis_function_cleanup,
756         .bind_config    = rndis_function_bind_config,
757         .unbind_config  = rndis_function_unbind_config,
758         .attributes     = rndis_function_attributes,
759 };
760
761
762 struct mass_storage_function_config {
763         struct fsg_config fsg;
764         struct fsg_common *common;
765 };
766
767 static int mass_storage_function_init(struct android_usb_function *f,
768                                         struct usb_composite_dev *cdev)
769 {
770         struct mass_storage_function_config *config;
771         struct fsg_common *common;
772         int err, i;
773         const char *name[2];
774
775         config = kzalloc(sizeof(struct mass_storage_function_config),
776                                                                 GFP_KERNEL);
777         if (!config)
778                 return -ENOMEM;
779
780         config->fsg.nluns = 2;
781         name[0] = "lun";
782         name[1] = "lun1";
783         for (i = 0; i < config->fsg.nluns; i++) {
784                 config->fsg.luns[i].removable = 1;
785                 config->fsg.luns[i].nofua = 1;
786         }
787
788         common = fsg_common_init(NULL, cdev, &config->fsg);
789         if (IS_ERR(common)) {
790                 kfree(config);
791                 return PTR_ERR(common);
792         }
793
794         for (i = 0; i < config->fsg.nluns; i++) {
795                 err = sysfs_create_link(&f->dev->kobj,
796                                         &common->luns[i].dev.kobj,
797                                         name[i]);
798                 if (err)
799                         goto error;
800         }
801
802         config->common = common;
803         f->config = config;
804         return 0;
805 error:
806         for (; i > 0 ; i--)
807                 sysfs_remove_link(&f->dev->kobj, name[i-1]);
808         fsg_common_release(&common->ref);
809         kfree(config);
810         return err;
811 }
812
813 static void mass_storage_function_cleanup(struct android_usb_function *f)
814 {
815         kfree(f->config);
816         f->config = NULL;
817 }
818
819 static int mass_storage_function_bind_config(struct android_usb_function *f,
820                                                 struct usb_configuration *c)
821 {
822         struct mass_storage_function_config *config = f->config;
823         return fsg_bind_config(c->cdev, c, config->common);
824 }
825
826 static ssize_t mass_storage_inquiry_show(struct device *dev,
827                                 struct device_attribute *attr, char *buf)
828 {
829         struct android_usb_function *f = dev_get_drvdata(dev);
830         struct mass_storage_function_config *config = f->config;
831         return sprintf(buf, "%s\n", config->common->inquiry_string);
832 }
833
834 static ssize_t mass_storage_inquiry_store(struct device *dev,
835                 struct device_attribute *attr, const char *buf, size_t size)
836 {
837         struct android_usb_function *f = dev_get_drvdata(dev);
838         struct mass_storage_function_config *config = f->config;
839         if (size >= sizeof(config->common->inquiry_string))
840                 return -EINVAL;
841         if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
842                 return -EINVAL;
843         return size;
844 }
845
846 static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
847                                         mass_storage_inquiry_show,
848                                         mass_storage_inquiry_store);
849
850 static struct device_attribute *mass_storage_function_attributes[] = {
851         &dev_attr_inquiry_string,
852         NULL
853 };
854
855 static struct android_usb_function mass_storage_function = {
856         .name           = "mass_storage",
857         .init           = mass_storage_function_init,
858         .cleanup        = mass_storage_function_cleanup,
859         .bind_config    = mass_storage_function_bind_config,
860         .attributes     = mass_storage_function_attributes,
861 };
862
863
864 static int accessory_function_init(struct android_usb_function *f,
865                                         struct usb_composite_dev *cdev)
866 {
867         return acc_setup();
868 }
869
870 static void accessory_function_cleanup(struct android_usb_function *f)
871 {
872         acc_cleanup();
873 }
874
875 static int accessory_function_bind_config(struct android_usb_function *f,
876                                                 struct usb_configuration *c)
877 {
878         return acc_bind_config(c);
879 }
880
881 static int accessory_function_ctrlrequest(struct android_usb_function *f,
882                                                 struct usb_composite_dev *cdev,
883                                                 const struct usb_ctrlrequest *c)
884 {
885         return acc_ctrlrequest(cdev, c);
886 }
887
888 static struct android_usb_function accessory_function = {
889         .name           = "accessory",
890         .init           = accessory_function_init,
891         .cleanup        = accessory_function_cleanup,
892         .bind_config    = accessory_function_bind_config,
893         .ctrlrequest    = accessory_function_ctrlrequest,
894 };
895
896 static int audio_source_function_init(struct android_usb_function *f,
897                         struct usb_composite_dev *cdev)
898 {
899         struct audio_source_config *config;
900
901         config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
902         if (!config)
903                 return -ENOMEM;
904         config->card = -1;
905         config->device = -1;
906         f->config = config;
907         return 0;
908 }
909
910 static void audio_source_function_cleanup(struct android_usb_function *f)
911 {
912         kfree(f->config);
913 }
914
915 static int audio_source_function_bind_config(struct android_usb_function *f,
916                                                 struct usb_configuration *c)
917 {
918         struct audio_source_config *config = f->config;
919
920         return audio_source_bind_config(c, config);
921 }
922
923 static void audio_source_function_unbind_config(struct android_usb_function *f,
924                                                 struct usb_configuration *c)
925 {
926         struct audio_source_config *config = f->config;
927
928         config->card = -1;
929         config->device = -1;
930 }
931
932 static ssize_t audio_source_pcm_show(struct device *dev,
933                 struct device_attribute *attr, char *buf)
934 {
935         struct android_usb_function *f = dev_get_drvdata(dev);
936         struct audio_source_config *config = f->config;
937
938         /* print PCM card and device numbers */
939         return sprintf(buf, "%d %d\n", config->card, config->device);
940 }
941
942 static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL);
943
944 static struct device_attribute *audio_source_function_attributes[] = {
945         &dev_attr_pcm,
946         NULL
947 };
948
949 static struct android_usb_function audio_source_function = {
950         .name           = "audio_source",
951         .init           = audio_source_function_init,
952         .cleanup        = audio_source_function_cleanup,
953         .bind_config    = audio_source_function_bind_config,
954         .unbind_config  = audio_source_function_unbind_config,
955         .attributes     = audio_source_function_attributes,
956 };
957
958 static int midi_function_init(struct android_usb_function *f,
959                                         struct usb_composite_dev *cdev)
960 {
961         struct midi_alsa_config *config;
962
963         config = kzalloc(sizeof(struct midi_alsa_config), GFP_KERNEL);
964         f->config = config;
965         if (!config)
966                 return -ENOMEM;
967         config->card = -1;
968         config->device = -1;
969         return 0;
970 }
971
972 static void midi_function_cleanup(struct android_usb_function *f)
973 {
974         kfree(f->config);
975 }
976
977 static int midi_function_bind_config(struct android_usb_function *f,
978                                                 struct usb_configuration *c)
979 {
980         struct midi_alsa_config *config = f->config;
981
982         return f_midi_bind_config(c, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
983                         MIDI_INPUT_PORTS, MIDI_OUTPUT_PORTS, MIDI_BUFFER_SIZE,
984                         MIDI_QUEUE_LENGTH, config);
985 }
986
987 static ssize_t midi_alsa_show(struct device *dev,
988                 struct device_attribute *attr, char *buf)
989 {
990         struct android_usb_function *f = dev_get_drvdata(dev);
991         struct midi_alsa_config *config = f->config;
992
993         /* print ALSA card and device numbers */
994         return sprintf(buf, "%d %d\n", config->card, config->device);
995 }
996
997 static DEVICE_ATTR(alsa, S_IRUGO, midi_alsa_show, NULL);
998
999 static struct device_attribute *midi_function_attributes[] = {
1000         &dev_attr_alsa,
1001         NULL
1002 };
1003
1004 static struct android_usb_function midi_function = {
1005         .name           = "midi",
1006         .init           = midi_function_init,
1007         .cleanup        = midi_function_cleanup,
1008         .bind_config    = midi_function_bind_config,
1009         .attributes     = midi_function_attributes,
1010 };
1011
1012 static struct android_usb_function *supported_functions[] = {
1013         &ffs_function,
1014         &acm_function,
1015         &mtp_function,
1016         &ptp_function,
1017         &rndis_function,
1018         &mass_storage_function,
1019         &accessory_function,
1020         &audio_source_function,
1021         &midi_function,
1022         NULL
1023 };
1024
1025 static int android_init_functions(struct android_usb_function **functions,
1026                                   struct usb_composite_dev *cdev)
1027 {
1028         struct android_dev *dev = _android_dev;
1029         struct android_usb_function *f;
1030         struct device_attribute **attrs;
1031         struct device_attribute *attr;
1032         int err;
1033         int index = 0;
1034
1035         for (; (f = *functions++); index++) {
1036                 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
1037                 f->dev = device_create(android_class, dev->dev,
1038                                 MKDEV(0, index), f, f->dev_name);
1039                 if (IS_ERR(f->dev)) {
1040                         pr_err("%s: Failed to create dev %s", __func__,
1041                                                         f->dev_name);
1042                         err = PTR_ERR(f->dev);
1043                         goto err_create;
1044                 }
1045
1046                 if (f->init) {
1047                         err = f->init(f, cdev);
1048                         if (err) {
1049                                 pr_err("%s: Failed to init %s", __func__,
1050                                                                 f->name);
1051                                 goto err_out;
1052                         }
1053                 }
1054
1055                 attrs = f->attributes;
1056                 if (attrs) {
1057                         while ((attr = *attrs++) && !err)
1058                                 err = device_create_file(f->dev, attr);
1059                 }
1060                 if (err) {
1061                         pr_err("%s: Failed to create function %s attributes",
1062                                         __func__, f->name);
1063                         goto err_out;
1064                 }
1065         }
1066         return 0;
1067
1068 err_out:
1069         device_destroy(android_class, f->dev->devt);
1070 err_create:
1071         kfree(f->dev_name);
1072         return err;
1073 }
1074
1075 static void android_cleanup_functions(struct android_usb_function **functions)
1076 {
1077         struct android_usb_function *f;
1078
1079         while (*functions) {
1080                 f = *functions++;
1081
1082                 if (f->dev) {
1083                         device_destroy(android_class, f->dev->devt);
1084                         kfree(f->dev_name);
1085                 }
1086
1087                 if (f->cleanup)
1088                         f->cleanup(f);
1089         }
1090 }
1091
1092 static int
1093 android_bind_enabled_functions(struct android_dev *dev,
1094                                struct usb_configuration *c)
1095 {
1096         struct android_usb_function *f;
1097         int ret;
1098
1099         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1100                 ret = f->bind_config(f, c);
1101                 if (ret) {
1102                         pr_err("%s: %s failed", __func__, f->name);
1103                         return ret;
1104                 }
1105         }
1106         return 0;
1107 }
1108
1109 static void
1110 android_unbind_enabled_functions(struct android_dev *dev,
1111                                struct usb_configuration *c)
1112 {
1113         struct android_usb_function *f;
1114
1115         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1116                 if (f->unbind_config)
1117                         f->unbind_config(f, c);
1118         }
1119 }
1120
1121 static int android_enable_function(struct android_dev *dev, char *name)
1122 {
1123         struct android_usb_function **functions = dev->functions;
1124         struct android_usb_function *f;
1125         while ((f = *functions++)) {
1126                 if (!strcmp(name, f->name)) {
1127                         list_add_tail(&f->enabled_list,
1128                                                 &dev->enabled_functions);
1129                         return 0;
1130                 }
1131         }
1132         return -EINVAL;
1133 }
1134
1135 /*-------------------------------------------------------------------------*/
1136 /* /sys/class/android_usb/android%d/ interface */
1137
1138 static ssize_t
1139 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1140 {
1141         struct android_dev *dev = dev_get_drvdata(pdev);
1142         struct android_usb_function *f;
1143         char *buff = buf;
1144
1145         mutex_lock(&dev->mutex);
1146
1147         list_for_each_entry(f, &dev->enabled_functions, enabled_list)
1148                 buff += sprintf(buff, "%s,", f->name);
1149
1150         mutex_unlock(&dev->mutex);
1151
1152         if (buff != buf)
1153                 *(buff-1) = '\n';
1154         return buff - buf;
1155 }
1156
1157 static ssize_t
1158 functions_store(struct device *pdev, struct device_attribute *attr,
1159                                const char *buff, size_t size)
1160 {
1161         struct android_dev *dev = dev_get_drvdata(pdev);
1162         char *name;
1163         char buf[256], *b;
1164         char aliases[256], *a;
1165         int err;
1166         int is_ffs;
1167         int ffs_enabled = 0;
1168
1169         mutex_lock(&dev->mutex);
1170
1171         if (dev->enabled) {
1172                 mutex_unlock(&dev->mutex);
1173                 return -EBUSY;
1174         }
1175
1176         INIT_LIST_HEAD(&dev->enabled_functions);
1177
1178         strlcpy(buf, buff, sizeof(buf));
1179         b = strim(buf);
1180
1181         while (b) {
1182                 name = strsep(&b, ",");
1183                 if (!name)
1184                         continue;
1185
1186                 is_ffs = 0;
1187                 strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
1188                 a = aliases;
1189
1190                 while (a) {
1191                         char *alias = strsep(&a, ",");
1192                         if (alias && !strcmp(name, alias)) {
1193                                 is_ffs = 1;
1194                                 break;
1195                         }
1196                 }
1197
1198                 if (is_ffs) {
1199                         if (ffs_enabled)
1200                                 continue;
1201                         err = android_enable_function(dev, "ffs");
1202                         if (err)
1203                                 pr_err("android_usb: Cannot enable ffs (%d)",
1204                                                                         err);
1205                         else
1206                                 ffs_enabled = 1;
1207                         continue;
1208                 }
1209
1210                 err = android_enable_function(dev, name);
1211                 if (err)
1212                         pr_err("android_usb: Cannot enable '%s' (%d)",
1213                                                            name, err);
1214         }
1215
1216         mutex_unlock(&dev->mutex);
1217
1218         return size;
1219 }
1220
1221 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1222                            char *buf)
1223 {
1224         struct android_dev *dev = dev_get_drvdata(pdev);
1225         return sprintf(buf, "%d\n", dev->enabled);
1226 }
1227
1228 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1229                             const char *buff, size_t size)
1230 {
1231         struct android_dev *dev = dev_get_drvdata(pdev);
1232         struct usb_composite_dev *cdev = dev->cdev;
1233         struct android_usb_function *f;
1234         int enabled = 0;
1235
1236
1237         if (!cdev)
1238                 return -ENODEV;
1239
1240         mutex_lock(&dev->mutex);
1241
1242         sscanf(buff, "%d", &enabled);
1243         if (enabled && !dev->enabled) {
1244                 /*
1245                  * Update values in composite driver's copy of
1246                  * device descriptor.
1247                  */
1248                 cdev->desc.idVendor = device_desc.idVendor;
1249                 cdev->desc.idProduct = device_desc.idProduct;
1250                 cdev->desc.bcdDevice = device_desc.bcdDevice;
1251                 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1252                 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1253                 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1254                 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1255                         if (f->enable)
1256                                 f->enable(f);
1257                 }
1258                 android_enable(dev);
1259                 dev->enabled = true;
1260         } else if (!enabled && dev->enabled) {
1261                 android_disable(dev);
1262                 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1263                         if (f->disable)
1264                                 f->disable(f);
1265                 }
1266                 dev->enabled = false;
1267         } else {
1268                 pr_err("android_usb: already %s\n",
1269                                 dev->enabled ? "enabled" : "disabled");
1270         }
1271
1272         mutex_unlock(&dev->mutex);
1273         return size;
1274 }
1275
1276 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1277                            char *buf)
1278 {
1279         struct android_dev *dev = dev_get_drvdata(pdev);
1280         struct usb_composite_dev *cdev = dev->cdev;
1281         char *state = "DISCONNECTED";
1282         unsigned long flags;
1283
1284         if (!cdev)
1285                 goto out;
1286
1287         spin_lock_irqsave(&cdev->lock, flags);
1288         if (cdev->config)
1289                 state = "CONFIGURED";
1290         else if (dev->connected)
1291                 state = "CONNECTED";
1292         spin_unlock_irqrestore(&cdev->lock, flags);
1293 out:
1294         return sprintf(buf, "%s\n", state);
1295 }
1296
1297 #define DESCRIPTOR_ATTR(field, format_string)                           \
1298 static ssize_t                                                          \
1299 field ## _show(struct device *dev, struct device_attribute *attr,       \
1300                 char *buf)                                              \
1301 {                                                                       \
1302         return sprintf(buf, format_string, device_desc.field);          \
1303 }                                                                       \
1304 static ssize_t                                                          \
1305 field ## _store(struct device *dev, struct device_attribute *attr,      \
1306                 const char *buf, size_t size)                           \
1307 {                                                                       \
1308         int value;                                                      \
1309         if (sscanf(buf, format_string, &value) == 1) {                  \
1310                 device_desc.field = value;                              \
1311                 return size;                                            \
1312         }                                                               \
1313         return -1;                                                      \
1314 }                                                                       \
1315 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1316
1317 #define DESCRIPTOR_STRING_ATTR(field, buffer)                           \
1318 static ssize_t                                                          \
1319 field ## _show(struct device *dev, struct device_attribute *attr,       \
1320                 char *buf)                                              \
1321 {                                                                       \
1322         return sprintf(buf, "%s", buffer);                              \
1323 }                                                                       \
1324 static ssize_t                                                          \
1325 field ## _store(struct device *dev, struct device_attribute *attr,      \
1326                 const char *buf, size_t size)                           \
1327 {                                                                       \
1328         if (size >= sizeof(buffer))                                     \
1329                 return -EINVAL;                                         \
1330         return strlcpy(buffer, buf, sizeof(buffer));                    \
1331 }                                                                       \
1332 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1333
1334
1335 DESCRIPTOR_ATTR(idVendor, "%04x\n")
1336 DESCRIPTOR_ATTR(idProduct, "%04x\n")
1337 DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1338 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1339 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1340 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1341 DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1342 DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1343 DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1344
1345 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1346                                                  functions_store);
1347 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1348 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1349
1350 static struct device_attribute *android_usb_attributes[] = {
1351         &dev_attr_idVendor,
1352         &dev_attr_idProduct,
1353         &dev_attr_bcdDevice,
1354         &dev_attr_bDeviceClass,
1355         &dev_attr_bDeviceSubClass,
1356         &dev_attr_bDeviceProtocol,
1357         &dev_attr_iManufacturer,
1358         &dev_attr_iProduct,
1359         &dev_attr_iSerial,
1360         &dev_attr_functions,
1361         &dev_attr_enable,
1362         &dev_attr_state,
1363         NULL
1364 };
1365
1366 /*-------------------------------------------------------------------------*/
1367 /* Composite driver */
1368
1369 static int android_bind_config(struct usb_configuration *c)
1370 {
1371         struct android_dev *dev = _android_dev;
1372         int ret = 0;
1373
1374         ret = android_bind_enabled_functions(dev, c);
1375         if (ret)
1376                 return ret;
1377
1378         return 0;
1379 }
1380
1381 static void android_unbind_config(struct usb_configuration *c)
1382 {
1383         struct android_dev *dev = _android_dev;
1384
1385         android_unbind_enabled_functions(dev, c);
1386 }
1387
1388 static int android_bind(struct usb_composite_dev *cdev)
1389 {
1390         struct android_dev *dev = _android_dev;
1391         struct usb_gadget       *gadget = cdev->gadget;
1392         int                     id, ret;
1393
1394         /* Save the default handler */
1395         dev->setup_complete = cdev->req->complete;
1396
1397         /*
1398          * Start disconnected. Userspace will connect the gadget once
1399          * it is done configuring the functions.
1400          */
1401         usb_gadget_disconnect(gadget);
1402
1403         ret = android_init_functions(dev->functions, cdev);
1404         if (ret)
1405                 return ret;
1406
1407         /* Allocate string descriptor numbers ... note that string
1408          * contents can be overridden by the composite_dev glue.
1409          */
1410         id = usb_string_id(cdev);
1411         if (id < 0)
1412                 return id;
1413         strings_dev[STRING_MANUFACTURER_IDX].id = id;
1414         device_desc.iManufacturer = id;
1415
1416         id = usb_string_id(cdev);
1417         if (id < 0)
1418                 return id;
1419         strings_dev[STRING_PRODUCT_IDX].id = id;
1420         device_desc.iProduct = id;
1421
1422         /* Default strings - should be updated by userspace */
1423         strncpy(manufacturer_string, "Android", sizeof(manufacturer_string)-1);
1424         strncpy(product_string, "Android", sizeof(product_string) - 1);
1425         strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
1426
1427         id = usb_string_id(cdev);
1428         if (id < 0)
1429                 return id;
1430         strings_dev[STRING_SERIAL_IDX].id = id;
1431         device_desc.iSerialNumber = id;
1432         device_desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1433
1434         usb_gadget_set_selfpowered(gadget);
1435         dev->cdev = cdev;
1436
1437         return 0;
1438 }
1439
1440 static int android_usb_unbind(struct usb_composite_dev *cdev)
1441 {
1442         struct android_dev *dev = _android_dev;
1443
1444         cancel_work_sync(&dev->work);
1445         android_cleanup_functions(dev->functions);
1446         return 0;
1447 }
1448
1449 /* HACK: android needs to override setup for accessory to work */
1450 static int (*composite_setup_func)(struct usb_gadget *gadget, const struct usb_ctrlrequest *c);
1451
1452 static int
1453 android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1454 {
1455         struct android_dev              *dev = _android_dev;
1456         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1457         struct usb_request              *req = cdev->req;
1458         struct android_usb_function     *f;
1459         int value = -EOPNOTSUPP;
1460         unsigned long flags;
1461
1462         req->zero = 0;
1463         req->length = 0;
1464         req->complete = dev->setup_complete;
1465         gadget->ep0->driver_data = cdev;
1466
1467         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1468                 if (f->ctrlrequest) {
1469                         value = f->ctrlrequest(f, cdev, c);
1470                         if (value >= 0)
1471                                 break;
1472                 }
1473         }
1474
1475         /* Special case the accessory function.
1476          * It needs to handle control requests before it is enabled.
1477          */
1478         if (value < 0)
1479                 value = acc_ctrlrequest(cdev, c);
1480
1481         if (value < 0)
1482                 value = composite_setup_func(gadget, c);
1483
1484         spin_lock_irqsave(&cdev->lock, flags);
1485         if (!dev->connected) {
1486                 dev->connected = 1;
1487                 schedule_work(&dev->work);
1488         } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1489                                                 cdev->config) {
1490                 schedule_work(&dev->work);
1491         }
1492         spin_unlock_irqrestore(&cdev->lock, flags);
1493
1494         return value;
1495 }
1496
1497 static void android_disconnect(struct usb_composite_dev *cdev)
1498 {
1499         struct android_dev *dev = _android_dev;
1500
1501         /* accessory HID support can be active while the
1502            accessory function is not actually enabled,
1503            so we need to inform it when we are disconnected.
1504          */
1505         acc_disconnect();
1506
1507         dev->connected = 0;
1508         schedule_work(&dev->work);
1509 }
1510
1511 static struct usb_composite_driver android_usb_driver = {
1512         .name           = "android_usb",
1513         .dev            = &device_desc,
1514         .strings        = dev_strings,
1515         .bind           = android_bind,
1516         .unbind         = android_usb_unbind,
1517         .disconnect     = android_disconnect,
1518         .max_speed      = USB_SPEED_HIGH,
1519 };
1520
1521 static int android_create_device(struct android_dev *dev)
1522 {
1523         struct device_attribute **attrs = android_usb_attributes;
1524         struct device_attribute *attr;
1525         int err;
1526
1527         dev->dev = device_create(android_class, NULL,
1528                                         MKDEV(0, 0), NULL, "android0");
1529         if (IS_ERR(dev->dev))
1530                 return PTR_ERR(dev->dev);
1531
1532         dev_set_drvdata(dev->dev, dev);
1533
1534         while ((attr = *attrs++)) {
1535                 err = device_create_file(dev->dev, attr);
1536                 if (err) {
1537                         device_destroy(android_class, dev->dev->devt);
1538                         return err;
1539                 }
1540         }
1541         return 0;
1542 }
1543
1544
1545 static int __init init(void)
1546 {
1547         struct android_dev *dev;
1548         int err;
1549
1550         android_class = class_create(THIS_MODULE, "android_usb");
1551         if (IS_ERR(android_class))
1552                 return PTR_ERR(android_class);
1553
1554         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1555         if (!dev) {
1556                 err = -ENOMEM;
1557                 goto err_dev;
1558         }
1559
1560         dev->disable_depth = 1;
1561         dev->functions = supported_functions;
1562         INIT_LIST_HEAD(&dev->enabled_functions);
1563         INIT_WORK(&dev->work, android_work);
1564         mutex_init(&dev->mutex);
1565
1566         err = android_create_device(dev);
1567         if (err) {
1568                 pr_err("%s: failed to create android device %d", __func__, err);
1569                 goto err_create;
1570         }
1571
1572         _android_dev = dev;
1573
1574         err = usb_composite_probe(&android_usb_driver);
1575         if (err) {
1576                 pr_err("%s: failed to probe driver %d", __func__, err);
1577                 goto err_probe;
1578         }
1579
1580         /* HACK: exchange composite's setup with ours */
1581         composite_setup_func = android_usb_driver.gadget_driver.setup;
1582         android_usb_driver.gadget_driver.setup = android_setup;
1583
1584         return 0;
1585
1586 err_probe:
1587         device_destroy(android_class, dev->dev->devt);
1588 err_create:
1589         kfree(dev);
1590 err_dev:
1591         class_destroy(android_class);
1592         return err;
1593 }
1594 late_initcall(init);
1595
1596 static void __exit cleanup(void)
1597 {
1598         usb_composite_unregister(&android_usb_driver);
1599         class_destroy(android_class);
1600         kfree(_android_dev);
1601         _android_dev = NULL;
1602 }
1603 module_exit(cleanup);