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