034672ff5683e6b238a1d2dfe375a8f19bf5cb83
[linux-2.6.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  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 /* #define DEBUG */
19 /* #define VERBOSE_DEBUG */
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/fs.h>
24
25 #include <linux/delay.h>
26 #include <linux/kernel.h>
27 #include <linux/utsname.h>
28 #include <linux/platform_device.h>
29
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/composite.h>
32 #include <linux/usb/gadget.h>
33
34 #include "gadget_chips.h"
35
36 /*
37  * Kbuild is not very cooperative with respect to linking separately
38  * compiled library objects into one module.  So for now we won't use
39  * separate compilation ... ensuring init/exit sections work to shrink
40  * the runtime footprint, and giving us at least some parts of what
41  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
42  */
43 #include "usbstring.c"
44 #include "config.c"
45 #include "epautoconf.c"
46 #include "composite.c"
47
48 #include "f_mass_storage.c"
49 #include "u_serial.c"
50 #include "f_acm.c"
51 #include "f_adb.c"
52 #include "f_mtp.c"
53 #include "f_accessory.c"
54 #define USB_ETH_RNDIS y
55 #include "f_rndis.c"
56 #include "rndis.c"
57 #include "u_ether.c"
58
59 MODULE_AUTHOR("Mike Lockwood");
60 MODULE_DESCRIPTION("Android Composite USB Driver");
61 MODULE_LICENSE("GPL");
62 MODULE_VERSION("1.0");
63
64 static const char longname[] = "Gadget Android";
65
66 /* Default vendor and product IDs, overridden by userspace */
67 #define VENDOR_ID               0x18D1
68 #define PRODUCT_ID              0x0001
69
70 struct android_usb_function {
71         char *name;
72         void *config;
73
74         struct device *dev;
75         char *dev_name;
76         struct device_attribute **attributes;
77
78         /* for android_dev.enabled_functions */
79         struct list_head enabled_list;
80
81         /* Optional: initialization during gadget bind */
82         int (*init)(struct android_usb_function *, struct usb_composite_dev *);
83         /* Optional: cleanup during gadget unbind */
84         void (*cleanup)(struct android_usb_function *);
85
86         int (*bind_config)(struct android_usb_function *, struct usb_configuration *);
87
88         /* Optional: called when the configuration is removed */
89         void (*unbind_config)(struct android_usb_function *, struct usb_configuration *);
90         /* Optional: handle ctrl requests before the device is configured */
91         int (*ctrlrequest)(struct android_usb_function *,
92                                         struct usb_composite_dev *,
93                                         const struct usb_ctrlrequest *);
94 };
95
96 struct android_dev {
97         struct android_usb_function **functions;
98         struct list_head enabled_functions;
99         struct usb_composite_dev *cdev;
100         struct device *dev;
101
102         bool enabled;
103         bool connected;
104         bool sw_connected;
105         struct work_struct work;
106 };
107
108 static struct class *android_class;
109 static struct android_dev *_android_dev;
110 static int android_bind_config(struct usb_configuration *c);
111 static void android_unbind_config(struct usb_configuration *c);
112
113 /* string IDs are assigned dynamically */
114 #define STRING_MANUFACTURER_IDX         0
115 #define STRING_PRODUCT_IDX              1
116 #define STRING_SERIAL_IDX               2
117
118 static char manufacturer_string[256];
119 static char product_string[256];
120 static char serial_string[256];
121
122 /* String Table */
123 static struct usb_string strings_dev[] = {
124         [STRING_MANUFACTURER_IDX].s = manufacturer_string,
125         [STRING_PRODUCT_IDX].s = product_string,
126         [STRING_SERIAL_IDX].s = serial_string,
127         {  }                    /* end of list */
128 };
129
130 static struct usb_gadget_strings stringtab_dev = {
131         .language       = 0x0409,       /* en-us */
132         .strings        = strings_dev,
133 };
134
135 static struct usb_gadget_strings *dev_strings[] = {
136         &stringtab_dev,
137         NULL,
138 };
139
140 static struct usb_device_descriptor device_desc = {
141         .bLength              = sizeof(device_desc),
142         .bDescriptorType      = USB_DT_DEVICE,
143         .bcdUSB               = __constant_cpu_to_le16(0x0200),
144         .bDeviceClass         = USB_CLASS_PER_INTERFACE,
145         .idVendor             = __constant_cpu_to_le16(VENDOR_ID),
146         .idProduct            = __constant_cpu_to_le16(PRODUCT_ID),
147         .bcdDevice            = __constant_cpu_to_le16(0xffff),
148         .bNumConfigurations   = 1,
149 };
150
151 static struct usb_configuration android_config_driver = {
152         .label          = "android",
153         .unbind         = android_unbind_config,
154         .bConfigurationValue = 1,
155         .bmAttributes   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
156         .bMaxPower      = 0xFA, /* 500ma */
157 };
158
159 static void android_work(struct work_struct *data)
160 {
161         struct android_dev *dev = container_of(data, struct android_dev, work);
162         struct usb_composite_dev *cdev = dev->cdev;
163         char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
164         char *connected[2]    = { "USB_STATE=CONNECTED", NULL };
165         char *configured[2]   = { "USB_STATE=CONFIGURED", NULL };
166         char **uevent_envp = NULL;
167         unsigned long flags;
168
169         spin_lock_irqsave(&cdev->lock, flags);
170         if (cdev->config)
171                 uevent_envp = configured;
172         else if (dev->connected != dev->sw_connected)
173                 uevent_envp = dev->connected ? connected : disconnected;
174         dev->sw_connected = dev->connected;
175         spin_unlock_irqrestore(&cdev->lock, flags);
176
177         if (uevent_envp) {
178                 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
179                 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
180         } else {
181                 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
182                          dev->connected, dev->sw_connected, cdev->config);
183         }
184 }
185
186
187 /*-------------------------------------------------------------------------*/
188 /* Supported functions initialization */
189
190 static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
191 {
192         return adb_setup();
193 }
194
195 static void adb_function_cleanup(struct android_usb_function *f)
196 {
197         adb_cleanup();
198 }
199
200 static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
201 {
202         return adb_bind_config(c);
203 }
204
205 static struct android_usb_function adb_function = {
206         .name           = "adb",
207         .init           = adb_function_init,
208         .cleanup        = adb_function_cleanup,
209         .bind_config    = adb_function_bind_config,
210 };
211
212
213 #define MAX_ACM_INSTANCES 4
214 struct acm_function_config {
215         int instances;
216 };
217
218 static int acm_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
219 {
220         f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
221         if (!f->config)
222                 return -ENOMEM;
223
224         return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
225 }
226
227 static void acm_function_cleanup(struct android_usb_function *f)
228 {
229         gserial_cleanup();
230         kfree(f->config);
231         f->config = NULL;
232 }
233
234 static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
235 {
236         int i;
237         int ret = 0;
238         struct acm_function_config *config = f->config;
239
240         for (i = 0; i < config->instances; i++) {
241                 ret = acm_bind_config(c, i);
242                 if (ret) {
243                         pr_err("Could not bind acm%u config\n", i);
244                         break;
245                 }
246         }
247
248         return ret;
249 }
250
251 static ssize_t acm_instances_show(struct device *dev,
252                 struct device_attribute *attr, char *buf)
253 {
254         struct android_usb_function *f = dev_get_drvdata(dev);
255         struct acm_function_config *config = f->config;
256         return sprintf(buf, "%d\n", config->instances);
257 }
258
259 static ssize_t acm_instances_store(struct device *dev,
260                 struct device_attribute *attr, const char *buf, size_t size)
261 {
262         struct android_usb_function *f = dev_get_drvdata(dev);
263         struct acm_function_config *config = f->config;
264         int value;
265
266         sscanf(buf, "%d", &value);
267         if (value > MAX_ACM_INSTANCES)
268                 value = MAX_ACM_INSTANCES;
269         config->instances = value;
270         return size;
271 }
272
273 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
274 static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
275
276 static struct android_usb_function acm_function = {
277         .name           = "acm",
278         .init           = acm_function_init,
279         .cleanup        = acm_function_cleanup,
280         .bind_config    = acm_function_bind_config,
281         .attributes     = acm_function_attributes,
282 };
283
284
285 static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
286 {
287         return mtp_setup();
288 }
289
290 static void mtp_function_cleanup(struct android_usb_function *f)
291 {
292         mtp_cleanup();
293 }
294
295 static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
296 {
297         return mtp_bind_config(c, false);
298 }
299
300 static int ptp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
301 {
302         /* nothing to do - initialization is handled by mtp_function_init */
303         return 0;
304 }
305
306 static void ptp_function_cleanup(struct android_usb_function *f)
307 {
308         /* nothing to do - cleanup is handled by mtp_function_cleanup */
309 }
310
311 static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
312 {
313         return mtp_bind_config(c, true);
314 }
315
316 static int mtp_function_ctrlrequest(struct android_usb_function *f,
317                                                 struct usb_composite_dev *cdev,
318                                                 const struct usb_ctrlrequest *c)
319 {
320         return mtp_ctrlrequest(cdev, c);
321 }
322
323 static struct android_usb_function mtp_function = {
324         .name           = "mtp",
325         .init           = mtp_function_init,
326         .cleanup        = mtp_function_cleanup,
327         .bind_config    = mtp_function_bind_config,
328         .ctrlrequest    = mtp_function_ctrlrequest,
329 };
330
331 /* PTP function is same as MTP with slightly different interface descriptor */
332 static struct android_usb_function ptp_function = {
333         .name           = "ptp",
334         .init           = ptp_function_init,
335         .cleanup        = ptp_function_cleanup,
336         .bind_config    = ptp_function_bind_config,
337 };
338
339
340 struct rndis_function_config {
341         u8      ethaddr[ETH_ALEN];
342         u32     vendorID;
343         char    manufacturer[256];
344         bool    wceis;
345 };
346
347 static int rndis_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
348 {
349         f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
350         if (!f->config)
351                 return -ENOMEM;
352         return 0;
353 }
354
355 static void rndis_function_cleanup(struct android_usb_function *f)
356 {
357         kfree(f->config);
358         f->config = NULL;
359 }
360
361 static int rndis_function_bind_config(struct android_usb_function *f,
362                                         struct usb_configuration *c)
363 {
364         int ret;
365         struct rndis_function_config *rndis = f->config;
366
367         if (!rndis) {
368                 pr_err("%s: rndis_pdata\n", __func__);
369                 return -1;
370         }
371
372         pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
373                 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
374                 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
375
376         ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
377         if (ret) {
378                 pr_err("%s: gether_setup failed\n", __func__);
379                 return ret;
380         }
381
382         if (rndis->wceis) {
383                 /* "Wireless" RNDIS; auto-detected by Windows */
384                 rndis_iad_descriptor.bFunctionClass =
385                                                 USB_CLASS_WIRELESS_CONTROLLER;
386                 rndis_iad_descriptor.bFunctionSubClass = 0x01;
387                 rndis_iad_descriptor.bFunctionProtocol = 0x03;
388                 rndis_control_intf.bInterfaceClass =
389                                                 USB_CLASS_WIRELESS_CONTROLLER;
390                 rndis_control_intf.bInterfaceSubClass =  0x01;
391                 rndis_control_intf.bInterfaceProtocol =  0x03;
392         }
393
394         return rndis_bind_config(c, rndis->ethaddr, rndis->vendorID,
395                                     rndis->manufacturer);
396 }
397
398 static void rndis_function_unbind_config(struct android_usb_function *f,
399                                                 struct usb_configuration *c)
400 {
401         gether_cleanup();
402 }
403
404 static ssize_t rndis_manufacturer_show(struct device *dev,
405                 struct device_attribute *attr, char *buf)
406 {
407         struct android_usb_function *f = dev_get_drvdata(dev);
408         struct rndis_function_config *config = f->config;
409         return sprintf(buf, "%s\n", config->manufacturer);
410 }
411
412 static ssize_t rndis_manufacturer_store(struct device *dev,
413                 struct device_attribute *attr, const char *buf, size_t size)
414 {
415         struct android_usb_function *f = dev_get_drvdata(dev);
416         struct rndis_function_config *config = f->config;
417
418         if (size >= sizeof(config->manufacturer))
419                 return -EINVAL;
420         if (sscanf(buf, "%s", config->manufacturer) == 1)
421                 return size;
422         return -1;
423 }
424
425 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
426                                                     rndis_manufacturer_store);
427
428 static ssize_t rndis_wceis_show(struct device *dev,
429                 struct device_attribute *attr, char *buf)
430 {
431         struct android_usb_function *f = dev_get_drvdata(dev);
432         struct rndis_function_config *config = f->config;
433         return sprintf(buf, "%d\n", config->wceis);
434 }
435
436 static ssize_t rndis_wceis_store(struct device *dev,
437                 struct device_attribute *attr, const char *buf, size_t size)
438 {
439         struct android_usb_function *f = dev_get_drvdata(dev);
440         struct rndis_function_config *config = f->config;
441         int value;
442
443         if (sscanf(buf, "%d", &value) == 1) {
444                 config->wceis = value;
445                 return size;
446         }
447         return -EINVAL;
448 }
449
450 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
451                                              rndis_wceis_store);
452
453 static ssize_t rndis_ethaddr_show(struct device *dev,
454                 struct device_attribute *attr, char *buf)
455 {
456         struct android_usb_function *f = dev_get_drvdata(dev);
457         struct rndis_function_config *rndis = f->config;
458         return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
459                 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
460                 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
461 }
462
463 static ssize_t rndis_ethaddr_store(struct device *dev,
464                 struct device_attribute *attr, const char *buf, size_t size)
465 {
466         struct android_usb_function *f = dev_get_drvdata(dev);
467         struct rndis_function_config *rndis = f->config;
468
469         if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
470                     (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
471                     (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
472                     (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
473                 return size;
474         return -EINVAL;
475 }
476
477 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
478                                                rndis_ethaddr_store);
479
480 static ssize_t rndis_vendorID_show(struct device *dev,
481                 struct device_attribute *attr, char *buf)
482 {
483         struct android_usb_function *f = dev_get_drvdata(dev);
484         struct rndis_function_config *config = f->config;
485         return sprintf(buf, "%04x\n", config->vendorID);
486 }
487
488 static ssize_t rndis_vendorID_store(struct device *dev,
489                 struct device_attribute *attr, const char *buf, size_t size)
490 {
491         struct android_usb_function *f = dev_get_drvdata(dev);
492         struct rndis_function_config *config = f->config;
493         int value;
494
495         if (sscanf(buf, "%04x", &value) == 1) {
496                 config->vendorID = value;
497                 return size;
498         }
499         return -EINVAL;
500 }
501
502 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
503                                                 rndis_vendorID_store);
504
505 static struct device_attribute *rndis_function_attributes[] = {
506         &dev_attr_manufacturer,
507         &dev_attr_wceis,
508         &dev_attr_ethaddr,
509         &dev_attr_vendorID,
510         NULL
511 };
512
513 static struct android_usb_function rndis_function = {
514         .name           = "rndis",
515         .init           = rndis_function_init,
516         .cleanup        = rndis_function_cleanup,
517         .bind_config    = rndis_function_bind_config,
518         .unbind_config  = rndis_function_unbind_config,
519         .attributes     = rndis_function_attributes,
520 };
521
522
523 struct mass_storage_function_config {
524         struct fsg_config fsg;
525         struct fsg_common *common;
526 };
527
528 static int mass_storage_function_init(struct android_usb_function *f,
529                                         struct usb_composite_dev *cdev)
530 {
531         struct mass_storage_function_config *config;
532         struct fsg_common *common;
533         int err;
534
535         config = kzalloc(sizeof(struct mass_storage_function_config),
536                                                                 GFP_KERNEL);
537         if (!config)
538                 return -ENOMEM;
539
540         config->fsg.nluns = 1;
541         config->fsg.luns[0].removable = 1;
542
543         common = fsg_common_init(NULL, cdev, &config->fsg);
544         if (IS_ERR(common)) {
545                 kfree(config);
546                 return PTR_ERR(common);
547         }
548
549         err = sysfs_create_link(&f->dev->kobj,
550                                 &common->luns[0].dev.kobj,
551                                 "lun");
552         if (err) {
553                 kfree(config);
554                 return err;
555         }
556
557         config->common = common;
558         f->config = config;
559         return 0;
560 }
561
562 static void mass_storage_function_cleanup(struct android_usb_function *f)
563 {
564         kfree(f->config);
565         f->config = NULL;
566 }
567
568 static int mass_storage_function_bind_config(struct android_usb_function *f,
569                                                 struct usb_configuration *c)
570 {
571         struct mass_storage_function_config *config = f->config;
572         return fsg_bind_config(c->cdev, c, config->common);
573 }
574
575 static ssize_t mass_storage_inquiry_show(struct device *dev,
576                                 struct device_attribute *attr, char *buf)
577 {
578         struct android_usb_function *f = dev_get_drvdata(dev);
579         struct mass_storage_function_config *config = f->config;
580         return sprintf(buf, "%s\n", config->common->inquiry_string);
581 }
582
583 static ssize_t mass_storage_inquiry_store(struct device *dev,
584                 struct device_attribute *attr, const char *buf, size_t size)
585 {
586         struct android_usb_function *f = dev_get_drvdata(dev);
587         struct mass_storage_function_config *config = f->config;
588         if (size >= sizeof(config->common->inquiry_string))
589                 return -EINVAL;
590         if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
591                 return -EINVAL;
592         return size;
593 }
594
595 static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
596                                         mass_storage_inquiry_show,
597                                         mass_storage_inquiry_store);
598
599 static struct device_attribute *mass_storage_function_attributes[] = {
600         &dev_attr_inquiry_string,
601         NULL
602 };
603
604 static struct android_usb_function mass_storage_function = {
605         .name           = "mass_storage",
606         .init           = mass_storage_function_init,
607         .cleanup        = mass_storage_function_cleanup,
608         .bind_config    = mass_storage_function_bind_config,
609         .attributes     = mass_storage_function_attributes,
610 };
611
612
613 static int accessory_function_init(struct android_usb_function *f,
614                                         struct usb_composite_dev *cdev)
615 {
616         return acc_setup();
617 }
618
619 static void accessory_function_cleanup(struct android_usb_function *f)
620 {
621         acc_cleanup();
622 }
623
624 static int accessory_function_bind_config(struct android_usb_function *f,
625                                                 struct usb_configuration *c)
626 {
627         return acc_bind_config(c);
628 }
629
630 static int accessory_function_ctrlrequest(struct android_usb_function *f,
631                                                 struct usb_composite_dev *cdev,
632                                                 const struct usb_ctrlrequest *c)
633 {
634         return acc_ctrlrequest(cdev, c);
635 }
636
637 static struct android_usb_function accessory_function = {
638         .name           = "accessory",
639         .init           = accessory_function_init,
640         .cleanup        = accessory_function_cleanup,
641         .bind_config    = accessory_function_bind_config,
642         .ctrlrequest    = accessory_function_ctrlrequest,
643 };
644
645
646 static struct android_usb_function *supported_functions[] = {
647         &adb_function,
648         &acm_function,
649         &mtp_function,
650         &ptp_function,
651         &rndis_function,
652         &mass_storage_function,
653         &accessory_function,
654         NULL
655 };
656
657
658 static int android_init_functions(struct android_usb_function **functions,
659                                   struct usb_composite_dev *cdev)
660 {
661         struct android_dev *dev = _android_dev;
662         struct android_usb_function *f;
663         struct device_attribute **attrs;
664         struct device_attribute *attr;
665         int err;
666         int index = 0;
667
668         for (; (f = *functions++); index++) {
669                 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
670                 f->dev = device_create(android_class, dev->dev,
671                                 MKDEV(0, index), f, f->dev_name);
672                 if (IS_ERR(f->dev)) {
673                         pr_err("%s: Failed to create dev %s", __func__,
674                                                         f->dev_name);
675                         err = PTR_ERR(f->dev);
676                         goto err_create;
677                 }
678
679                 if (f->init) {
680                         err = f->init(f, cdev);
681                         if (err) {
682                                 pr_err("%s: Failed to init %s", __func__,
683                                                                 f->name);
684                                 goto err_out;
685                         }
686                 }
687
688                 attrs = f->attributes;
689                 if (attrs) {
690                         while ((attr = *attrs++) && !err)
691                                 err = device_create_file(f->dev, attr);
692                 }
693                 if (err) {
694                         pr_err("%s: Failed to create function %s attributes",
695                                         __func__, f->name);
696                         goto err_out;
697                 }
698         }
699         return 0;
700
701 err_out:
702         device_destroy(android_class, f->dev->devt);
703 err_create:
704         kfree(f->dev_name);
705         return err;
706 }
707
708 static void android_cleanup_functions(struct android_usb_function **functions)
709 {
710         struct android_usb_function *f;
711
712         while (*functions) {
713                 f = *functions++;
714
715                 if (f->dev) {
716                         device_destroy(android_class, f->dev->devt);
717                         kfree(f->dev_name);
718                 }
719
720                 if (f->cleanup)
721                         f->cleanup(f);
722         }
723 }
724
725 static int
726 android_bind_enabled_functions(struct android_dev *dev,
727                                struct usb_configuration *c)
728 {
729         struct android_usb_function *f;
730         int ret;
731
732         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
733                 ret = f->bind_config(f, c);
734                 if (ret) {
735                         pr_err("%s: %s failed", __func__, f->name);
736                         return ret;
737                 }
738         }
739         return 0;
740 }
741
742 static void
743 android_unbind_enabled_functions(struct android_dev *dev,
744                                struct usb_configuration *c)
745 {
746         struct android_usb_function *f;
747
748         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
749                 if (f->unbind_config)
750                         f->unbind_config(f, c);
751         }
752 }
753
754 static int android_enable_function(struct android_dev *dev, char *name)
755 {
756         struct android_usb_function **functions = dev->functions;
757         struct android_usb_function *f;
758         while ((f = *functions++)) {
759                 if (!strcmp(name, f->name)) {
760                         list_add_tail(&f->enabled_list, &dev->enabled_functions);
761                         return 0;
762                 }
763         }
764         return -EINVAL;
765 }
766
767 /*-------------------------------------------------------------------------*/
768 /* /sys/class/android_usb/android%d/ interface */
769
770 static ssize_t
771 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
772 {
773         struct android_dev *dev = dev_get_drvdata(pdev);
774         struct android_usb_function *f;
775         char *buff = buf;
776
777         list_for_each_entry(f, &dev->enabled_functions, enabled_list)
778                 buff += sprintf(buff, "%s,", f->name);
779         if (buff != buf)
780                 *(buff-1) = '\n';
781         return buff - buf;
782 }
783
784 static ssize_t
785 functions_store(struct device *pdev, struct device_attribute *attr,
786                                const char *buff, size_t size)
787 {
788         struct android_dev *dev = dev_get_drvdata(pdev);
789         char *name;
790         char buf[256], *b;
791         int err;
792
793         INIT_LIST_HEAD(&dev->enabled_functions);
794
795         strncpy(buf, buff, sizeof(buf));
796         b = strim(buf);
797
798         while (b) {
799                 name = strsep(&b, ",");
800                 if (name) {
801                         err = android_enable_function(dev, name);
802                         if (err)
803                                 pr_err("android_usb: Cannot enable '%s'", name);
804                 }
805         }
806
807         return size;
808 }
809
810 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
811                            char *buf)
812 {
813         struct android_dev *dev = dev_get_drvdata(pdev);
814         return sprintf(buf, "%d\n", dev->enabled);
815 }
816
817 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
818                             const char *buff, size_t size)
819 {
820         struct android_dev *dev = dev_get_drvdata(pdev);
821         struct usb_composite_dev *cdev = dev->cdev;
822         int enabled = 0;
823
824         sscanf(buff, "%d", &enabled);
825         if (enabled && !dev->enabled) {
826                 cdev->next_string_id = 0;
827                 /* update values in composite driver's copy of device descriptor */
828                 cdev->desc.idVendor = device_desc.idVendor;
829                 cdev->desc.idProduct = device_desc.idProduct;
830                 cdev->desc.bcdDevice = device_desc.bcdDevice;
831                 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
832                 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
833                 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
834                 usb_add_config(cdev, &android_config_driver,
835                                         android_bind_config);
836                 usb_gadget_connect(cdev->gadget);
837                 dev->enabled = true;
838         } else if (!enabled && dev->enabled) {
839                 usb_gadget_disconnect(cdev->gadget);
840                 /* Cancel pending control requests */
841                 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
842                 usb_remove_config(cdev, &android_config_driver);
843                 dev->enabled = false;
844         } else {
845                 pr_err("android_usb: already %s\n",
846                                 dev->enabled ? "enabled" : "disabled");
847         }
848         return size;
849 }
850
851 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
852                            char *buf)
853 {
854         struct android_dev *dev = dev_get_drvdata(pdev);
855         struct usb_composite_dev *cdev = dev->cdev;
856         char *state = "DISCONNECTED";
857         unsigned long flags;
858
859         if (!cdev)
860                 goto out;
861
862         spin_lock_irqsave(&cdev->lock, flags);
863         if (cdev->config)
864                 state = "CONFIGURED";
865         else if (dev->connected)
866                 state = "CONNECTED";
867         spin_unlock_irqrestore(&cdev->lock, flags);
868 out:
869         return sprintf(buf, "%s\n", state);
870 }
871
872 #define DESCRIPTOR_ATTR(field, format_string)                           \
873 static ssize_t                                                          \
874 field ## _show(struct device *dev, struct device_attribute *attr,       \
875                 char *buf)                                              \
876 {                                                                       \
877         return sprintf(buf, format_string, device_desc.field);          \
878 }                                                                       \
879 static ssize_t                                                          \
880 field ## _store(struct device *dev, struct device_attribute *attr,      \
881                 const char *buf, size_t size)                           \
882 {                                                                       \
883         int value;                                                      \
884         if (sscanf(buf, format_string, &value) == 1) {                  \
885                 device_desc.field = value;                              \
886                 return size;                                            \
887         }                                                               \
888         return -1;                                                      \
889 }                                                                       \
890 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
891
892 #define DESCRIPTOR_STRING_ATTR(field, buffer)                           \
893 static ssize_t                                                          \
894 field ## _show(struct device *dev, struct device_attribute *attr,       \
895                 char *buf)                                              \
896 {                                                                       \
897         return sprintf(buf, "%s", buffer);                              \
898 }                                                                       \
899 static ssize_t                                                          \
900 field ## _store(struct device *dev, struct device_attribute *attr,      \
901                 const char *buf, size_t size)                           \
902 {                                                                       \
903         if (size >= sizeof(buffer)) return -EINVAL;                     \
904         if (sscanf(buf, "%s", buffer) == 1) {                           \
905                 return size;                                            \
906         }                                                               \
907         return -1;                                                      \
908 }                                                                       \
909 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
910
911
912 DESCRIPTOR_ATTR(idVendor, "%04x\n")
913 DESCRIPTOR_ATTR(idProduct, "%04x\n")
914 DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
915 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
916 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
917 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
918 DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
919 DESCRIPTOR_STRING_ATTR(iProduct, product_string)
920 DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
921
922 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
923 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
924 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
925
926 static struct device_attribute *android_usb_attributes[] = {
927         &dev_attr_idVendor,
928         &dev_attr_idProduct,
929         &dev_attr_bcdDevice,
930         &dev_attr_bDeviceClass,
931         &dev_attr_bDeviceSubClass,
932         &dev_attr_bDeviceProtocol,
933         &dev_attr_iManufacturer,
934         &dev_attr_iProduct,
935         &dev_attr_iSerial,
936         &dev_attr_functions,
937         &dev_attr_enable,
938         &dev_attr_state,
939         NULL
940 };
941
942 /*-------------------------------------------------------------------------*/
943 /* Composite driver */
944
945 static int android_bind_config(struct usb_configuration *c)
946 {
947         struct android_dev *dev = _android_dev;
948         int ret = 0;
949
950         ret = android_bind_enabled_functions(dev, c);
951         if (ret)
952                 return ret;
953
954         return 0;
955 }
956
957 static void android_unbind_config(struct usb_configuration *c)
958 {
959         struct android_dev *dev = _android_dev;
960
961         android_unbind_enabled_functions(dev, c);
962 }
963
964 static int android_bind(struct usb_composite_dev *cdev)
965 {
966         struct android_dev *dev = _android_dev;
967         struct usb_gadget       *gadget = cdev->gadget;
968         int                     gcnum, id, ret;
969
970         usb_gadget_disconnect(gadget);
971
972         ret = android_init_functions(dev->functions, cdev);
973         if (ret)
974                 return ret;
975
976         /* Allocate string descriptor numbers ... note that string
977          * contents can be overridden by the composite_dev glue.
978          */
979         id = usb_string_id(cdev);
980         if (id < 0)
981                 return id;
982         strings_dev[STRING_MANUFACTURER_IDX].id = id;
983         device_desc.iManufacturer = id;
984
985         id = usb_string_id(cdev);
986         if (id < 0)
987                 return id;
988         strings_dev[STRING_PRODUCT_IDX].id = id;
989         device_desc.iProduct = id;
990
991         /* Default strings - should be updated by userspace */
992         strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1);
993         strncpy(product_string, "Android", sizeof(product_string) - 1);
994         strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
995
996         id = usb_string_id(cdev);
997         if (id < 0)
998                 return id;
999         strings_dev[STRING_SERIAL_IDX].id = id;
1000         device_desc.iSerialNumber = id;
1001
1002         gcnum = usb_gadget_controller_number(gadget);
1003         if (gcnum >= 0)
1004                 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1005         else {
1006                 /* gadget zero is so simple (for now, no altsettings) that
1007                  * it SHOULD NOT have problems with bulk-capable hardware.
1008                  * so just warn about unrcognized controllers -- don't panic.
1009                  *
1010                  * things like configuration and altsetting numbering
1011                  * can need hardware-specific attention though.
1012                  */
1013                 pr_warning("%s: controller '%s' not recognized\n",
1014                         longname, gadget->name);
1015                 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1016         }
1017
1018         usb_gadget_set_selfpowered(gadget);
1019         dev->cdev = cdev;
1020
1021         return 0;
1022 }
1023
1024 static int android_usb_unbind(struct usb_composite_dev *cdev)
1025 {
1026         struct android_dev *dev = _android_dev;
1027
1028         cancel_work_sync(&dev->work);
1029         android_cleanup_functions(dev->functions);
1030         return 0;
1031 }
1032
1033 static struct usb_composite_driver android_usb_driver = {
1034         .name           = "android_usb",
1035         .dev            = &device_desc,
1036         .strings        = dev_strings,
1037         .unbind         = android_usb_unbind,
1038         .max_speed      = USB_SPEED_HIGH,
1039 };
1040
1041 static int
1042 android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1043 {
1044         struct android_dev              *dev = _android_dev;
1045         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1046         struct usb_request              *req = cdev->req;
1047         struct android_usb_function     *f;
1048         int value = -EOPNOTSUPP;
1049         unsigned long flags;
1050
1051         req->zero = 0;
1052         req->complete = composite_setup_complete;
1053         req->length = 0;
1054         gadget->ep0->driver_data = cdev;
1055
1056         list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1057                 if (f->ctrlrequest) {
1058                         value = f->ctrlrequest(f, cdev, c);
1059                         if (value >= 0)
1060                                 break;
1061                 }
1062         }
1063
1064         /* Special case the accessory function.
1065          * It needs to handle control requests before it is enabled.
1066          */
1067         if (value < 0)
1068                 value = acc_ctrlrequest(cdev, c);
1069
1070         if (value < 0)
1071                 value = composite_setup(gadget, c);
1072
1073         spin_lock_irqsave(&cdev->lock, flags);
1074         if (!dev->connected) {
1075                 dev->connected = 1;
1076                 schedule_work(&dev->work);
1077         }
1078         else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1079                 schedule_work(&dev->work);
1080         }
1081         spin_unlock_irqrestore(&cdev->lock, flags);
1082
1083         return value;
1084 }
1085
1086 static void android_disconnect(struct usb_gadget *gadget)
1087 {
1088         struct android_dev *dev = _android_dev;
1089         struct usb_composite_dev *cdev = get_gadget_data(gadget);
1090         unsigned long flags;
1091
1092         composite_disconnect(gadget);
1093
1094         spin_lock_irqsave(&cdev->lock, flags);
1095         dev->connected = 0;
1096         schedule_work(&dev->work);
1097         spin_unlock_irqrestore(&cdev->lock, flags);
1098 }
1099
1100 static int android_create_device(struct android_dev *dev)
1101 {
1102         struct device_attribute **attrs = android_usb_attributes;
1103         struct device_attribute *attr;
1104         int err;
1105
1106         dev->dev = device_create(android_class, NULL,
1107                                         MKDEV(0, 0), NULL, "android0");
1108         if (IS_ERR(dev->dev))
1109                 return PTR_ERR(dev->dev);
1110
1111         dev_set_drvdata(dev->dev, dev);
1112
1113         while ((attr = *attrs++)) {
1114                 err = device_create_file(dev->dev, attr);
1115                 if (err) {
1116                         device_destroy(android_class, dev->dev->devt);
1117                         return err;
1118                 }
1119         }
1120         return 0;
1121 }
1122
1123
1124 static int __init init(void)
1125 {
1126         struct android_dev *dev;
1127         int err;
1128
1129         android_class = class_create(THIS_MODULE, "android_usb");
1130         if (IS_ERR(android_class))
1131                 return PTR_ERR(android_class);
1132
1133         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1134         if (!dev)
1135                 return -ENOMEM;
1136
1137         dev->functions = supported_functions;
1138         INIT_LIST_HEAD(&dev->enabled_functions);
1139         INIT_WORK(&dev->work, android_work);
1140
1141         err = android_create_device(dev);
1142         if (err) {
1143                 class_destroy(android_class);
1144                 kfree(dev);
1145                 return err;
1146         }
1147
1148         _android_dev = dev;
1149
1150         /* Override composite driver functions */
1151         composite_driver.setup = android_setup;
1152         composite_driver.disconnect = android_disconnect;
1153
1154         return usb_composite_probe(&android_usb_driver, android_bind);
1155 }
1156 module_init(init);
1157
1158 static void __exit cleanup(void)
1159 {
1160         usb_composite_unregister(&android_usb_driver);
1161         class_destroy(android_class);
1162         kfree(_android_dev);
1163         _android_dev = NULL;
1164 }
1165 module_exit(cleanup);