blob: 5d77899678f9e3d306cecda3d11599ce8d919a68 [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
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>
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050028#include <linux/platform_device.h>
29
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050030#include <linux/usb/ch9.h>
31#include <linux/usb/composite.h>
32#include <linux/usb/gadget.h>
33
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050034#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
Benoit Gobyaab96812011-04-19 20:37:33 -070048#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
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050059MODULE_AUTHOR("Mike Lockwood");
60MODULE_DESCRIPTION("Android Composite USB Driver");
61MODULE_LICENSE("GPL");
62MODULE_VERSION("1.0");
63
64static const char longname[] = "Gadget Android";
65
Benoit Gobyaab96812011-04-19 20:37:33 -070066/* Default vendor and product IDs, overridden by userspace */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050067#define VENDOR_ID 0x18D1
68#define PRODUCT_ID 0x0001
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050069
Benoit Gobyaab96812011-04-19 20:37:33 -070070struct android_usb_function {
71 char *name;
72 void *config;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050073
Benoit Gobyaab96812011-04-19 20:37:33 -070074 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 *);
Mike Lockwood686d33a2011-09-07 09:55:12 -070090 /* Optional: handle ctrl requests before the device is configured */
Benoit Gobyaab96812011-04-19 20:37:33 -070091 int (*ctrlrequest)(struct android_usb_function *,
92 struct usb_composite_dev *,
93 const struct usb_ctrlrequest *);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050094};
95
Benoit Gobyaab96812011-04-19 20:37:33 -070096struct 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
108static struct class *android_class;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500109static struct android_dev *_android_dev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700110static int android_bind_config(struct usb_configuration *c);
111static void android_unbind_config(struct usb_configuration *c);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500112
113/* string IDs are assigned dynamically */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500114#define STRING_MANUFACTURER_IDX 0
115#define STRING_PRODUCT_IDX 1
116#define STRING_SERIAL_IDX 2
117
Benoit Gobyaab96812011-04-19 20:37:33 -0700118static char manufacturer_string[256];
119static char product_string[256];
120static char serial_string[256];
121
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500122/* String Table */
123static struct usb_string strings_dev[] = {
Benoit Gobyaab96812011-04-19 20:37:33 -0700124 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
125 [STRING_PRODUCT_IDX].s = product_string,
126 [STRING_SERIAL_IDX].s = serial_string,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500127 { } /* end of list */
128};
129
130static struct usb_gadget_strings stringtab_dev = {
131 .language = 0x0409, /* en-us */
132 .strings = strings_dev,
133};
134
135static struct usb_gadget_strings *dev_strings[] = {
136 &stringtab_dev,
137 NULL,
138};
139
140static 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
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500151static struct usb_configuration android_config_driver = {
152 .label = "android",
Benoit Gobyaab96812011-04-19 20:37:33 -0700153 .unbind = android_unbind_config,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500154 .bConfigurationValue = 1,
155 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
156 .bMaxPower = 0xFA, /* 500ma */
157};
158
Benoit Gobyaab96812011-04-19 20:37:33 -0700159static 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 };
Dima Zavinfc753492011-09-14 11:52:45 -0700166 char **uevent_envp = NULL;
Benoit Gobyaab96812011-04-19 20:37:33 -0700167 unsigned long flags;
168
169 spin_lock_irqsave(&cdev->lock, flags);
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700170 if (cdev->config)
Dima Zavinfc753492011-09-14 11:52:45 -0700171 uevent_envp = configured;
Dima Zavinf85cf4f2011-09-14 15:12:45 -0700172 else if (dev->connected != dev->sw_connected)
173 uevent_envp = dev->connected ? connected : disconnected;
174 dev->sw_connected = dev->connected;
Dima Zavinfc753492011-09-14 11:52:45 -0700175 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]);
Benoit Gobyaab96812011-04-19 20:37:33 -0700180 } else {
Dima Zavinfc753492011-09-14 11:52:45 -0700181 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
182 dev->connected, dev->sw_connected, cdev->config);
Benoit Gobyaab96812011-04-19 20:37:33 -0700183 }
184}
185
186
187/*-------------------------------------------------------------------------*/
188/* Supported functions initialization */
189
190static int adb_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
191{
192 return adb_setup();
193}
194
195static void adb_function_cleanup(struct android_usb_function *f)
196{
197 adb_cleanup();
198}
199
200static int adb_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
201{
202 return adb_bind_config(c);
203}
204
205static 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
214struct acm_function_config {
215 int instances;
216};
217
218static 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
227static void acm_function_cleanup(struct android_usb_function *f)
228{
229 gserial_cleanup();
230 kfree(f->config);
231 f->config = NULL;
232}
233
234static int acm_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530235{
236 int i;
Benoit Gobyaab96812011-04-19 20:37:33 -0700237 int ret = 0;
238 struct acm_function_config *config = f->config;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530239
Benoit Gobyaab96812011-04-19 20:37:33 -0700240 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;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530245 }
246 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700247
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530248 return ret;
249}
250
Benoit Gobyaab96812011-04-19 20:37:33 -0700251static ssize_t acm_instances_show(struct device *dev,
252 struct device_attribute *attr, char *buf)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530253{
Benoit Gobyaab96812011-04-19 20:37:33 -0700254 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}
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530258
Benoit Gobyaab96812011-04-19 20:37:33 -0700259static 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
273static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show, acm_instances_store);
274static struct device_attribute *acm_function_attributes[] = { &dev_attr_instances, NULL };
275
276static 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
285static int mtp_function_init(struct android_usb_function *f, struct usb_composite_dev *cdev)
286{
287 return mtp_setup();
288}
289
290static void mtp_function_cleanup(struct android_usb_function *f)
291{
292 mtp_cleanup();
293}
294
295static int mtp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
296{
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400297 return mtp_bind_config(c, false);
298}
299
300static 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
306static void ptp_function_cleanup(struct android_usb_function *f)
307{
308 /* nothing to do - cleanup is handled by mtp_function_cleanup */
309}
310
311static int ptp_function_bind_config(struct android_usb_function *f, struct usb_configuration *c)
312{
313 return mtp_bind_config(c, true);
Benoit Gobyaab96812011-04-19 20:37:33 -0700314}
315
316static 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
323static 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
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400331/* PTP function is same as MTP with slightly different interface descriptor */
332static 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
Benoit Gobyaab96812011-04-19 20:37:33 -0700339
340struct rndis_function_config {
341 u8 ethaddr[ETH_ALEN];
342 u32 vendorID;
343 char manufacturer[256];
344 bool wceis;
345};
346
347static 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
355static void rndis_function_cleanup(struct android_usb_function *f)
356{
357 kfree(f->config);
358 f->config = NULL;
359}
360
361static 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
Mike Lockwood394bf632011-08-12 14:35:42 -0700376 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
Benoit Gobyaab96812011-04-19 20:37:33 -0700377 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
398static void rndis_function_unbind_config(struct android_usb_function *f,
399 struct usb_configuration *c)
400{
401 gether_cleanup();
402}
403
404static 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
412static 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
425static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
426 rndis_manufacturer_store);
427
428static 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
436static 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
450static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
451 rndis_wceis_store);
452
453static 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
463static 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
477static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
478 rndis_ethaddr_store);
479
480static 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
488static 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
502static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
503 rndis_vendorID_store);
504
505static 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
513static 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
523struct mass_storage_function_config {
524 struct fsg_config fsg;
525 struct fsg_common *common;
526};
527
528static 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
562static void mass_storage_function_cleanup(struct android_usb_function *f)
563{
564 kfree(f->config);
565 f->config = NULL;
566}
567
568static 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
575static 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
583static 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
595static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
596 mass_storage_inquiry_show,
597 mass_storage_inquiry_store);
598
599static struct device_attribute *mass_storage_function_attributes[] = {
600 &dev_attr_inquiry_string,
601 NULL
602};
603
604static 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
613static int accessory_function_init(struct android_usb_function *f,
614 struct usb_composite_dev *cdev)
615{
616 return acc_setup();
617}
618
619static void accessory_function_cleanup(struct android_usb_function *f)
620{
621 acc_cleanup();
622}
623
624static int accessory_function_bind_config(struct android_usb_function *f,
625 struct usb_configuration *c)
626{
627 return acc_bind_config(c);
628}
629
630static 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
637static 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
646static struct android_usb_function *supported_functions[] = {
647 &adb_function,
648 &acm_function,
649 &mtp_function,
Mike Lockwoodcf7addf2011-06-01 22:17:36 -0400650 &ptp_function,
Benoit Gobyaab96812011-04-19 20:37:33 -0700651 &rndis_function,
652 &mass_storage_function,
653 &accessory_function,
654 NULL
655};
656
657
658static 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
701err_out:
702 device_destroy(android_class, f->dev->devt);
703err_create:
704 kfree(f->dev_name);
705 return err;
706}
707
708static 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
725static int
726android_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 }
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530738 }
739 return 0;
740}
741
Benoit Gobyaab96812011-04-19 20:37:33 -0700742static void
743android_unbind_enabled_functions(struct android_dev *dev,
744 struct usb_configuration *c)
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530745{
Benoit Gobyaab96812011-04-19 20:37:33 -0700746 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
754static 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);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530761 return 0;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500762 }
763 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700764 return -EINVAL;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500765}
766
Benoit Gobyaab96812011-04-19 20:37:33 -0700767/*-------------------------------------------------------------------------*/
768/* /sys/class/android_usb/android%d/ interface */
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530769
Benoit Gobyaab96812011-04-19 20:37:33 -0700770static ssize_t
771functions_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
784static ssize_t
785functions_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);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530804 }
805 }
Benoit Gobyaab96812011-04-19 20:37:33 -0700806
807 return size;
808}
809
810static 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
817static 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) {
Benoit Goby6a4a3852011-11-28 18:01:03 -0800826 cdev->next_string_id = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700827 /* 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 usb_remove_config(cdev, &android_config_driver);
841 dev->enabled = false;
842 } else {
843 pr_err("android_usb: already %s\n",
844 dev->enabled ? "enabled" : "disabled");
845 }
846 return size;
847}
848
849static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
850 char *buf)
851{
852 struct android_dev *dev = dev_get_drvdata(pdev);
853 struct usb_composite_dev *cdev = dev->cdev;
854 char *state = "DISCONNECTED";
855 unsigned long flags;
856
857 if (!cdev)
858 goto out;
859
860 spin_lock_irqsave(&cdev->lock, flags);
861 if (cdev->config)
862 state = "CONFIGURED";
863 else if (dev->connected)
864 state = "CONNECTED";
865 spin_unlock_irqrestore(&cdev->lock, flags);
866out:
867 return sprintf(buf, "%s\n", state);
868}
869
870#define DESCRIPTOR_ATTR(field, format_string) \
871static ssize_t \
872field ## _show(struct device *dev, struct device_attribute *attr, \
873 char *buf) \
874{ \
875 return sprintf(buf, format_string, device_desc.field); \
876} \
877static ssize_t \
878field ## _store(struct device *dev, struct device_attribute *attr, \
879 const char *buf, size_t size) \
880{ \
881 int value; \
882 if (sscanf(buf, format_string, &value) == 1) { \
883 device_desc.field = value; \
884 return size; \
885 } \
886 return -1; \
887} \
888static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
889
890#define DESCRIPTOR_STRING_ATTR(field, buffer) \
891static ssize_t \
892field ## _show(struct device *dev, struct device_attribute *attr, \
893 char *buf) \
894{ \
895 return sprintf(buf, "%s", buffer); \
896} \
897static ssize_t \
898field ## _store(struct device *dev, struct device_attribute *attr, \
899 const char *buf, size_t size) \
900{ \
901 if (size >= sizeof(buffer)) return -EINVAL; \
902 if (sscanf(buf, "%s", buffer) == 1) { \
903 return size; \
904 } \
905 return -1; \
906} \
907static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
908
909
910DESCRIPTOR_ATTR(idVendor, "%04x\n")
911DESCRIPTOR_ATTR(idProduct, "%04x\n")
912DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
913DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
914DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
915DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
916DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
917DESCRIPTOR_STRING_ATTR(iProduct, product_string)
918DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
919
920static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show, functions_store);
921static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
922static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
923
924static struct device_attribute *android_usb_attributes[] = {
925 &dev_attr_idVendor,
926 &dev_attr_idProduct,
927 &dev_attr_bcdDevice,
928 &dev_attr_bDeviceClass,
929 &dev_attr_bDeviceSubClass,
930 &dev_attr_bDeviceProtocol,
931 &dev_attr_iManufacturer,
932 &dev_attr_iProduct,
933 &dev_attr_iSerial,
934 &dev_attr_functions,
935 &dev_attr_enable,
936 &dev_attr_state,
937 NULL
938};
939
940/*-------------------------------------------------------------------------*/
941/* Composite driver */
942
943static int android_bind_config(struct usb_configuration *c)
944{
945 struct android_dev *dev = _android_dev;
946 int ret = 0;
947
948 ret = android_bind_enabled_functions(dev, c);
949 if (ret)
950 return ret;
951
952 return 0;
953}
954
955static void android_unbind_config(struct usb_configuration *c)
956{
957 struct android_dev *dev = _android_dev;
958
959 android_unbind_enabled_functions(dev, c);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530960}
961
Dmitry Shmidt577e37a2010-07-02 12:46:34 -0700962static int android_bind(struct usb_composite_dev *cdev)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500963{
964 struct android_dev *dev = _android_dev;
965 struct usb_gadget *gadget = cdev->gadget;
Mike Lockwoodaecca432011-02-09 09:38:26 -0500966 int gcnum, id, ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500967
Benoit Gobyaab96812011-04-19 20:37:33 -0700968 usb_gadget_disconnect(gadget);
969
970 ret = android_init_functions(dev->functions, cdev);
971 if (ret)
972 return ret;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500973
974 /* Allocate string descriptor numbers ... note that string
975 * contents can be overridden by the composite_dev glue.
976 */
977 id = usb_string_id(cdev);
978 if (id < 0)
979 return id;
980 strings_dev[STRING_MANUFACTURER_IDX].id = id;
981 device_desc.iManufacturer = id;
982
983 id = usb_string_id(cdev);
984 if (id < 0)
985 return id;
986 strings_dev[STRING_PRODUCT_IDX].id = id;
987 device_desc.iProduct = id;
988
Benoit Gobyaab96812011-04-19 20:37:33 -0700989 /* Default strings - should be updated by userspace */
990 strncpy(manufacturer_string, "Android", sizeof(manufacturer_string) - 1);
991 strncpy(product_string, "Android", sizeof(product_string) - 1);
992 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
993
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500994 id = usb_string_id(cdev);
995 if (id < 0)
996 return id;
997 strings_dev[STRING_SERIAL_IDX].id = id;
998 device_desc.iSerialNumber = id;
999
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001000 gcnum = usb_gadget_controller_number(gadget);
1001 if (gcnum >= 0)
1002 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1003 else {
1004 /* gadget zero is so simple (for now, no altsettings) that
1005 * it SHOULD NOT have problems with bulk-capable hardware.
1006 * so just warn about unrcognized controllers -- don't panic.
1007 *
1008 * things like configuration and altsetting numbering
1009 * can need hardware-specific attention though.
1010 */
1011 pr_warning("%s: controller '%s' not recognized\n",
1012 longname, gadget->name);
1013 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1014 }
1015
1016 usb_gadget_set_selfpowered(gadget);
1017 dev->cdev = cdev;
1018
1019 return 0;
1020}
1021
Benoit Gobyaab96812011-04-19 20:37:33 -07001022static int android_usb_unbind(struct usb_composite_dev *cdev)
1023{
1024 struct android_dev *dev = _android_dev;
1025
1026 cancel_work_sync(&dev->work);
1027 android_cleanup_functions(dev->functions);
1028 return 0;
1029}
1030
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001031static struct usb_composite_driver android_usb_driver = {
1032 .name = "android_usb",
1033 .dev = &device_desc,
1034 .strings = dev_strings,
Benoit Gobyaab96812011-04-19 20:37:33 -07001035 .unbind = android_usb_unbind,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001036};
1037
Benoit Gobyaab96812011-04-19 20:37:33 -07001038static int
1039android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1040{
1041 struct android_dev *dev = _android_dev;
1042 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1043 struct usb_request *req = cdev->req;
Benoit Gobyaab96812011-04-19 20:37:33 -07001044 struct android_usb_function *f;
1045 int value = -EOPNOTSUPP;
1046 unsigned long flags;
1047
1048 req->zero = 0;
1049 req->complete = composite_setup_complete;
1050 req->length = 0;
1051 gadget->ep0->driver_data = cdev;
1052
Mike Lockwood6c7dd4b2011-08-02 11:13:48 -04001053 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
Benoit Gobyaab96812011-04-19 20:37:33 -07001054 if (f->ctrlrequest) {
1055 value = f->ctrlrequest(f, cdev, c);
1056 if (value >= 0)
1057 break;
1058 }
1059 }
1060
Mike Lockwood686d33a2011-09-07 09:55:12 -07001061 /* Special case the accessory function.
1062 * It needs to handle control requests before it is enabled.
1063 */
1064 if (value < 0)
1065 value = acc_ctrlrequest(cdev, c);
1066
Benoit Gobyaab96812011-04-19 20:37:33 -07001067 if (value < 0)
1068 value = composite_setup(gadget, c);
1069
1070 spin_lock_irqsave(&cdev->lock, flags);
1071 if (!dev->connected) {
1072 dev->connected = 1;
1073 schedule_work(&dev->work);
1074 }
1075 else if (c->bRequest == USB_REQ_SET_CONFIGURATION && cdev->config) {
1076 schedule_work(&dev->work);
1077 }
1078 spin_unlock_irqrestore(&cdev->lock, flags);
1079
1080 return value;
1081}
1082
1083static void android_disconnect(struct usb_gadget *gadget)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001084{
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301085 struct android_dev *dev = _android_dev;
Dima Zavin21bad752011-09-14 11:53:11 -07001086 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1087 unsigned long flags;
1088
1089 composite_disconnect(gadget);
1090
1091 spin_lock_irqsave(&cdev->lock, flags);
Benoit Gobyaab96812011-04-19 20:37:33 -07001092 dev->connected = 0;
1093 schedule_work(&dev->work);
Dima Zavin21bad752011-09-14 11:53:11 -07001094 spin_unlock_irqrestore(&cdev->lock, flags);
Krishna, Vamsi83814ea2009-02-11 21:07:20 +05301095}
1096
Benoit Gobyaab96812011-04-19 20:37:33 -07001097static int android_create_device(struct android_dev *dev)
John Michelaud5d2de62010-12-10 11:33:54 -06001098{
Benoit Gobyaab96812011-04-19 20:37:33 -07001099 struct device_attribute **attrs = android_usb_attributes;
1100 struct device_attribute *attr;
1101 int err;
John Michelaud5d2de62010-12-10 11:33:54 -06001102
Benoit Gobyaab96812011-04-19 20:37:33 -07001103 dev->dev = device_create(android_class, NULL,
1104 MKDEV(0, 0), NULL, "android0");
1105 if (IS_ERR(dev->dev))
1106 return PTR_ERR(dev->dev);
John Michelaud5d2de62010-12-10 11:33:54 -06001107
Benoit Gobyaab96812011-04-19 20:37:33 -07001108 dev_set_drvdata(dev->dev, dev);
1109
1110 while ((attr = *attrs++)) {
1111 err = device_create_file(dev->dev, attr);
1112 if (err) {
1113 device_destroy(android_class, dev->dev->devt);
1114 return err;
John Michelaud5d2de62010-12-10 11:33:54 -06001115 }
1116 }
Benoit Gobyaab96812011-04-19 20:37:33 -07001117 return 0;
John Michelaud5d2de62010-12-10 11:33:54 -06001118}
1119
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001120
1121static int __init init(void)
1122{
1123 struct android_dev *dev;
Benoit Gobyaab96812011-04-19 20:37:33 -07001124 int err;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001125
Benoit Gobyaab96812011-04-19 20:37:33 -07001126 android_class = class_create(THIS_MODULE, "android_usb");
1127 if (IS_ERR(android_class))
1128 return PTR_ERR(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001129
1130 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1131 if (!dev)
1132 return -ENOMEM;
1133
Benoit Gobyaab96812011-04-19 20:37:33 -07001134 dev->functions = supported_functions;
1135 INIT_LIST_HEAD(&dev->enabled_functions);
1136 INIT_WORK(&dev->work, android_work);
1137
1138 err = android_create_device(dev);
1139 if (err) {
1140 class_destroy(android_class);
1141 kfree(dev);
1142 return err;
1143 }
1144
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001145 _android_dev = dev;
1146
Benoit Gobyaab96812011-04-19 20:37:33 -07001147 /* Override composite driver functions */
1148 composite_driver.setup = android_setup;
1149 composite_driver.disconnect = android_disconnect;
1150
1151 return usb_composite_probe(&android_usb_driver, android_bind);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001152}
1153module_init(init);
1154
1155static void __exit cleanup(void)
1156{
1157 usb_composite_unregister(&android_usb_driver);
Benoit Gobyaab96812011-04-19 20:37:33 -07001158 class_destroy(android_class);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001159 kfree(_android_dev);
1160 _android_dev = NULL;
1161}
1162module_exit(cleanup);