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