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