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