2 * Gadget Driver for Android
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 * Benoit Goby <benoit@android.com>
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.
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.
19 #include <linux/init.h>
20 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/utsname.h>
25 #include <linux/platform_device.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/composite.h>
29 #include <linux/usb/gadget.h>
31 #include "gadget_chips.h"
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.
40 #include "usbstring.c"
42 #include "epautoconf.c"
43 #include "composite.c"
46 #include "f_mass_storage.c"
51 #include "f_accessory.c"
52 #define USB_ETH_RNDIS y
57 MODULE_AUTHOR("Mike Lockwood");
58 MODULE_DESCRIPTION("Android Composite USB Driver");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION("1.0");
62 static const char longname[] = "Gadget Android";
64 /* Default vendor and product IDs, overridden by userspace */
65 #define VENDOR_ID 0x18D1
66 #define PRODUCT_ID 0x0001
68 struct android_usb_function {
74 struct device_attribute **attributes;
76 /* for android_dev.enabled_functions */
77 struct list_head enabled_list;
79 /* Optional: initialization during gadget bind */
80 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
81 /* Optional: cleanup during gadget unbind */
82 void (*cleanup)(struct android_usb_function *);
83 /* Optional: called when the function is added the list of
84 * enabled functions */
85 void (*enable)(struct android_usb_function *);
86 /* Optional: called when it is removed */
87 void (*disable)(struct android_usb_function *);
89 int (*bind_config)(struct android_usb_function *,
90 struct usb_configuration *);
92 /* Optional: called when the configuration is removed */
93 void (*unbind_config)(struct android_usb_function *,
94 struct usb_configuration *);
95 /* Optional: handle ctrl requests before the device is configured */
96 int (*ctrlrequest)(struct android_usb_function *,
97 struct usb_composite_dev *,
98 const struct usb_ctrlrequest *);
102 struct android_usb_function **functions;
103 struct list_head enabled_functions;
104 struct usb_composite_dev *cdev;
112 struct work_struct work;
113 char ffs_aliases[256];
116 static struct class *android_class;
117 static struct android_dev *_android_dev;
118 static int android_bind_config(struct usb_configuration *c);
119 static void android_unbind_config(struct usb_configuration *c);
121 /* string IDs are assigned dynamically */
122 #define STRING_MANUFACTURER_IDX 0
123 #define STRING_PRODUCT_IDX 1
124 #define STRING_SERIAL_IDX 2
126 static char manufacturer_string[256];
127 static char product_string[256];
128 static char serial_string[256];
131 static struct usb_string strings_dev[] = {
132 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
133 [STRING_PRODUCT_IDX].s = product_string,
134 [STRING_SERIAL_IDX].s = serial_string,
135 { } /* end of list */
138 static struct usb_gadget_strings stringtab_dev = {
139 .language = 0x0409, /* en-us */
140 .strings = strings_dev,
143 static struct usb_gadget_strings *dev_strings[] = {
148 static struct usb_device_descriptor device_desc = {
149 .bLength = sizeof(device_desc),
150 .bDescriptorType = USB_DT_DEVICE,
151 .bcdUSB = __constant_cpu_to_le16(0x0200),
152 .bDeviceClass = USB_CLASS_PER_INTERFACE,
153 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
154 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
155 .bcdDevice = __constant_cpu_to_le16(0xffff),
156 .bNumConfigurations = 1,
159 static struct usb_configuration android_config_driver = {
161 .unbind = android_unbind_config,
162 .bConfigurationValue = 1,
163 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
164 .bMaxPower = 0xFA, /* 500ma */
167 static void android_work(struct work_struct *data)
169 struct android_dev *dev = container_of(data, struct android_dev, work);
170 struct usb_composite_dev *cdev = dev->cdev;
171 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
172 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
173 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
174 char **uevent_envp = NULL;
177 spin_lock_irqsave(&cdev->lock, flags);
179 uevent_envp = configured;
180 else if (dev->connected != dev->sw_connected)
181 uevent_envp = dev->connected ? connected : disconnected;
182 dev->sw_connected = dev->connected;
183 spin_unlock_irqrestore(&cdev->lock, flags);
186 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
187 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
189 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
190 dev->connected, dev->sw_connected, cdev->config);
194 static void android_enable(struct android_dev *dev)
196 struct usb_composite_dev *cdev = dev->cdev;
198 if (WARN_ON(!dev->disable_depth))
201 if (--dev->disable_depth == 0) {
202 usb_add_config(cdev, &android_config_driver,
203 android_bind_config);
204 usb_gadget_connect(cdev->gadget);
208 static void android_disable(struct android_dev *dev)
210 struct usb_composite_dev *cdev = dev->cdev;
212 if (dev->disable_depth++ == 0) {
213 usb_gadget_disconnect(cdev->gadget);
214 /* Cancel pending control requests */
215 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
216 usb_remove_config(cdev, &android_config_driver);
220 /*-------------------------------------------------------------------------*/
221 /* Supported functions initialization */
223 struct functionfs_config {
226 struct ffs_data *data;
229 static int ffs_function_init(struct android_usb_function *f,
230 struct usb_composite_dev *cdev)
232 f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
236 return functionfs_init();
239 static void ffs_function_cleanup(struct android_usb_function *f)
241 functionfs_cleanup();
245 static void ffs_function_enable(struct android_usb_function *f)
247 struct android_dev *dev = _android_dev;
248 struct functionfs_config *config = f->config;
250 config->enabled = true;
252 /* Disable the gadget until the function is ready */
254 android_disable(dev);
257 static void ffs_function_disable(struct android_usb_function *f)
259 struct android_dev *dev = _android_dev;
260 struct functionfs_config *config = f->config;
262 config->enabled = false;
264 /* Balance the disable that was called in closed_callback */
269 static int ffs_function_bind_config(struct android_usb_function *f,
270 struct usb_configuration *c)
272 struct functionfs_config *config = f->config;
273 return functionfs_bind_config(c->cdev, c, config->data);
277 ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
279 struct android_dev *dev = _android_dev;
282 mutex_lock(&dev->mutex);
283 ret = sprintf(buf, "%s\n", dev->ffs_aliases);
284 mutex_unlock(&dev->mutex);
290 ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
291 const char *buf, size_t size)
293 struct android_dev *dev = _android_dev;
296 mutex_lock(&dev->mutex);
299 mutex_unlock(&dev->mutex);
303 strlcpy(buff, buf, sizeof(buff));
304 strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
306 mutex_unlock(&dev->mutex);
311 static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
313 static struct device_attribute *ffs_function_attributes[] = {
318 static struct android_usb_function ffs_function = {
320 .init = ffs_function_init,
321 .enable = ffs_function_enable,
322 .disable = ffs_function_disable,
323 .cleanup = ffs_function_cleanup,
324 .bind_config = ffs_function_bind_config,
325 .attributes = ffs_function_attributes,
328 static int functionfs_ready_callback(struct ffs_data *ffs)
330 struct android_dev *dev = _android_dev;
331 struct functionfs_config *config = ffs_function.config;
334 mutex_lock(&dev->mutex);
336 ret = functionfs_bind(ffs, dev->cdev);
341 config->opened = true;
347 mutex_unlock(&dev->mutex);
351 static void functionfs_closed_callback(struct ffs_data *ffs)
353 struct android_dev *dev = _android_dev;
354 struct functionfs_config *config = ffs_function.config;
356 mutex_lock(&dev->mutex);
359 android_disable(dev);
361 config->opened = false;
364 functionfs_unbind(ffs);
366 mutex_unlock(&dev->mutex);
369 static int functionfs_check_dev_callback(const char *dev_name)
381 adb_function_init(struct android_usb_function *f,
382 struct usb_composite_dev *cdev)
384 f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
391 static void adb_function_cleanup(struct android_usb_function *f)
398 adb_function_bind_config(struct android_usb_function *f,
399 struct usb_configuration *c)
401 return adb_bind_config(c);
404 static void adb_android_function_enable(struct android_usb_function *f)
406 struct android_dev *dev = _android_dev;
407 struct adb_data *data = f->config;
409 data->enabled = true;
411 /* Disable the gadget until adbd is ready */
413 android_disable(dev);
416 static void adb_android_function_disable(struct android_usb_function *f)
418 struct android_dev *dev = _android_dev;
419 struct adb_data *data = f->config;
421 data->enabled = false;
423 /* Balance the disable that was called in closed_callback */
428 static struct android_usb_function adb_function = {
430 .enable = adb_android_function_enable,
431 .disable = adb_android_function_disable,
432 .init = adb_function_init,
433 .cleanup = adb_function_cleanup,
434 .bind_config = adb_function_bind_config,
437 static void adb_ready_callback(void)
439 struct android_dev *dev = _android_dev;
440 struct adb_data *data = adb_function.config;
442 mutex_lock(&dev->mutex);
449 mutex_unlock(&dev->mutex);
452 static void adb_closed_callback(void)
454 struct android_dev *dev = _android_dev;
455 struct adb_data *data = adb_function.config;
457 mutex_lock(&dev->mutex);
459 data->opened = false;
462 android_disable(dev);
464 mutex_unlock(&dev->mutex);
468 #define MAX_ACM_INSTANCES 4
469 struct acm_function_config {
474 acm_function_init(struct android_usb_function *f,
475 struct usb_composite_dev *cdev)
477 f->config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
481 return gserial_setup(cdev->gadget, MAX_ACM_INSTANCES);
484 static void acm_function_cleanup(struct android_usb_function *f)
492 acm_function_bind_config(struct android_usb_function *f,
493 struct usb_configuration *c)
497 struct acm_function_config *config = f->config;
499 for (i = 0; i < config->instances; i++) {
500 ret = acm_bind_config(c, i);
502 pr_err("Could not bind acm%u config\n", i);
510 static ssize_t acm_instances_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 struct android_usb_function *f = dev_get_drvdata(dev);
514 struct acm_function_config *config = f->config;
515 return sprintf(buf, "%d\n", config->instances);
518 static ssize_t acm_instances_store(struct device *dev,
519 struct device_attribute *attr, const char *buf, size_t size)
521 struct android_usb_function *f = dev_get_drvdata(dev);
522 struct acm_function_config *config = f->config;
525 sscanf(buf, "%d", &value);
526 if (value > MAX_ACM_INSTANCES)
527 value = MAX_ACM_INSTANCES;
528 config->instances = value;
532 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
533 acm_instances_store);
534 static struct device_attribute *acm_function_attributes[] = {
539 static struct android_usb_function acm_function = {
541 .init = acm_function_init,
542 .cleanup = acm_function_cleanup,
543 .bind_config = acm_function_bind_config,
544 .attributes = acm_function_attributes,
549 mtp_function_init(struct android_usb_function *f,
550 struct usb_composite_dev *cdev)
555 static void mtp_function_cleanup(struct android_usb_function *f)
561 mtp_function_bind_config(struct android_usb_function *f,
562 struct usb_configuration *c)
564 return mtp_bind_config(c, false);
568 ptp_function_init(struct android_usb_function *f,
569 struct usb_composite_dev *cdev)
571 /* nothing to do - initialization is handled by mtp_function_init */
575 static void ptp_function_cleanup(struct android_usb_function *f)
577 /* nothing to do - cleanup is handled by mtp_function_cleanup */
581 ptp_function_bind_config(struct android_usb_function *f,
582 struct usb_configuration *c)
584 return mtp_bind_config(c, true);
587 static int mtp_function_ctrlrequest(struct android_usb_function *f,
588 struct usb_composite_dev *cdev,
589 const struct usb_ctrlrequest *c)
591 return mtp_ctrlrequest(cdev, c);
594 static struct android_usb_function mtp_function = {
596 .init = mtp_function_init,
597 .cleanup = mtp_function_cleanup,
598 .bind_config = mtp_function_bind_config,
599 .ctrlrequest = mtp_function_ctrlrequest,
602 /* PTP function is same as MTP with slightly different interface descriptor */
603 static struct android_usb_function ptp_function = {
605 .init = ptp_function_init,
606 .cleanup = ptp_function_cleanup,
607 .bind_config = ptp_function_bind_config,
611 struct rndis_function_config {
612 u8 ethaddr[ETH_ALEN];
614 char manufacturer[256];
615 /* "Wireless" RNDIS; auto-detected by Windows */
620 rndis_function_init(struct android_usb_function *f,
621 struct usb_composite_dev *cdev)
623 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
629 static void rndis_function_cleanup(struct android_usb_function *f)
636 rndis_function_bind_config(struct android_usb_function *f,
637 struct usb_configuration *c)
640 struct rndis_function_config *rndis = f->config;
643 pr_err("%s: rndis_pdata\n", __func__);
647 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
648 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
649 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
651 ret = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
653 pr_err("%s: gether_setup failed\n", __func__);
658 /* "Wireless" RNDIS; auto-detected by Windows */
659 rndis_iad_descriptor.bFunctionClass =
660 USB_CLASS_WIRELESS_CONTROLLER;
661 rndis_iad_descriptor.bFunctionSubClass = 0x01;
662 rndis_iad_descriptor.bFunctionProtocol = 0x03;
663 rndis_control_intf.bInterfaceClass =
664 USB_CLASS_WIRELESS_CONTROLLER;
665 rndis_control_intf.bInterfaceSubClass = 0x01;
666 rndis_control_intf.bInterfaceProtocol = 0x03;
669 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
670 rndis->manufacturer);
673 static void rndis_function_unbind_config(struct android_usb_function *f,
674 struct usb_configuration *c)
679 static ssize_t rndis_manufacturer_show(struct device *dev,
680 struct device_attribute *attr, char *buf)
682 struct android_usb_function *f = dev_get_drvdata(dev);
683 struct rndis_function_config *config = f->config;
684 return sprintf(buf, "%s\n", config->manufacturer);
687 static ssize_t rndis_manufacturer_store(struct device *dev,
688 struct device_attribute *attr, const char *buf, size_t size)
690 struct android_usb_function *f = dev_get_drvdata(dev);
691 struct rndis_function_config *config = f->config;
693 if (size >= sizeof(config->manufacturer))
695 if (sscanf(buf, "%s", config->manufacturer) == 1)
700 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
701 rndis_manufacturer_store);
703 static ssize_t rndis_wceis_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
706 struct android_usb_function *f = dev_get_drvdata(dev);
707 struct rndis_function_config *config = f->config;
708 return sprintf(buf, "%d\n", config->wceis);
711 static ssize_t rndis_wceis_store(struct device *dev,
712 struct device_attribute *attr, const char *buf, size_t size)
714 struct android_usb_function *f = dev_get_drvdata(dev);
715 struct rndis_function_config *config = f->config;
718 if (sscanf(buf, "%d", &value) == 1) {
719 config->wceis = value;
725 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
728 static ssize_t rndis_ethaddr_show(struct device *dev,
729 struct device_attribute *attr, char *buf)
731 struct android_usb_function *f = dev_get_drvdata(dev);
732 struct rndis_function_config *rndis = f->config;
733 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
734 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
735 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
738 static ssize_t rndis_ethaddr_store(struct device *dev,
739 struct device_attribute *attr, const char *buf, size_t size)
741 struct android_usb_function *f = dev_get_drvdata(dev);
742 struct rndis_function_config *rndis = f->config;
744 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
745 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
746 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
747 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
752 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
753 rndis_ethaddr_store);
755 static ssize_t rndis_vendorID_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
758 struct android_usb_function *f = dev_get_drvdata(dev);
759 struct rndis_function_config *config = f->config;
760 return sprintf(buf, "%04x\n", config->vendorID);
763 static ssize_t rndis_vendorID_store(struct device *dev,
764 struct device_attribute *attr, const char *buf, size_t size)
766 struct android_usb_function *f = dev_get_drvdata(dev);
767 struct rndis_function_config *config = f->config;
770 if (sscanf(buf, "%04x", &value) == 1) {
771 config->vendorID = value;
777 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
778 rndis_vendorID_store);
780 static struct device_attribute *rndis_function_attributes[] = {
781 &dev_attr_manufacturer,
788 static struct android_usb_function rndis_function = {
790 .init = rndis_function_init,
791 .cleanup = rndis_function_cleanup,
792 .bind_config = rndis_function_bind_config,
793 .unbind_config = rndis_function_unbind_config,
794 .attributes = rndis_function_attributes,
798 struct mass_storage_function_config {
799 struct fsg_config fsg;
800 struct fsg_common *common;
803 static int mass_storage_function_init(struct android_usb_function *f,
804 struct usb_composite_dev *cdev)
806 struct mass_storage_function_config *config;
807 struct fsg_common *common;
810 config = kzalloc(sizeof(struct mass_storage_function_config),
815 config->fsg.nluns = 1;
816 config->fsg.luns[0].removable = 1;
818 common = fsg_common_init(NULL, cdev, &config->fsg);
819 if (IS_ERR(common)) {
821 return PTR_ERR(common);
824 err = sysfs_create_link(&f->dev->kobj,
825 &common->luns[0].dev.kobj,
832 config->common = common;
837 static void mass_storage_function_cleanup(struct android_usb_function *f)
843 static int mass_storage_function_bind_config(struct android_usb_function *f,
844 struct usb_configuration *c)
846 struct mass_storage_function_config *config = f->config;
847 return fsg_bind_config(c->cdev, c, config->common);
850 static ssize_t mass_storage_inquiry_show(struct device *dev,
851 struct device_attribute *attr, char *buf)
853 struct android_usb_function *f = dev_get_drvdata(dev);
854 struct mass_storage_function_config *config = f->config;
855 return sprintf(buf, "%s\n", config->common->inquiry_string);
858 static ssize_t mass_storage_inquiry_store(struct device *dev,
859 struct device_attribute *attr, const char *buf, size_t size)
861 struct android_usb_function *f = dev_get_drvdata(dev);
862 struct mass_storage_function_config *config = f->config;
863 if (size >= sizeof(config->common->inquiry_string))
865 if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
870 static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
871 mass_storage_inquiry_show,
872 mass_storage_inquiry_store);
874 static struct device_attribute *mass_storage_function_attributes[] = {
875 &dev_attr_inquiry_string,
879 static struct android_usb_function mass_storage_function = {
880 .name = "mass_storage",
881 .init = mass_storage_function_init,
882 .cleanup = mass_storage_function_cleanup,
883 .bind_config = mass_storage_function_bind_config,
884 .attributes = mass_storage_function_attributes,
888 static int accessory_function_init(struct android_usb_function *f,
889 struct usb_composite_dev *cdev)
894 static void accessory_function_cleanup(struct android_usb_function *f)
899 static int accessory_function_bind_config(struct android_usb_function *f,
900 struct usb_configuration *c)
902 return acc_bind_config(c);
905 static int accessory_function_ctrlrequest(struct android_usb_function *f,
906 struct usb_composite_dev *cdev,
907 const struct usb_ctrlrequest *c)
909 return acc_ctrlrequest(cdev, c);
912 static struct android_usb_function accessory_function = {
914 .init = accessory_function_init,
915 .cleanup = accessory_function_cleanup,
916 .bind_config = accessory_function_bind_config,
917 .ctrlrequest = accessory_function_ctrlrequest,
921 static struct android_usb_function *supported_functions[] = {
928 &mass_storage_function,
934 static int android_init_functions(struct android_usb_function **functions,
935 struct usb_composite_dev *cdev)
937 struct android_dev *dev = _android_dev;
938 struct android_usb_function *f;
939 struct device_attribute **attrs;
940 struct device_attribute *attr;
944 for (; (f = *functions++); index++) {
945 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
946 f->dev = device_create(android_class, dev->dev,
947 MKDEV(0, index), f, f->dev_name);
948 if (IS_ERR(f->dev)) {
949 pr_err("%s: Failed to create dev %s", __func__,
951 err = PTR_ERR(f->dev);
956 err = f->init(f, cdev);
958 pr_err("%s: Failed to init %s", __func__,
964 attrs = f->attributes;
966 while ((attr = *attrs++) && !err)
967 err = device_create_file(f->dev, attr);
970 pr_err("%s: Failed to create function %s attributes",
978 device_destroy(android_class, f->dev->devt);
984 static void android_cleanup_functions(struct android_usb_function **functions)
986 struct android_usb_function *f;
992 device_destroy(android_class, f->dev->devt);
1002 android_bind_enabled_functions(struct android_dev *dev,
1003 struct usb_configuration *c)
1005 struct android_usb_function *f;
1008 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1009 ret = f->bind_config(f, c);
1011 pr_err("%s: %s failed", __func__, f->name);
1019 android_unbind_enabled_functions(struct android_dev *dev,
1020 struct usb_configuration *c)
1022 struct android_usb_function *f;
1024 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1025 if (f->unbind_config)
1026 f->unbind_config(f, c);
1030 static int android_enable_function(struct android_dev *dev, char *name)
1032 struct android_usb_function **functions = dev->functions;
1033 struct android_usb_function *f;
1034 while ((f = *functions++)) {
1035 if (!strcmp(name, f->name)) {
1036 list_add_tail(&f->enabled_list,
1037 &dev->enabled_functions);
1044 /*-------------------------------------------------------------------------*/
1045 /* /sys/class/android_usb/android%d/ interface */
1048 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1050 struct android_dev *dev = dev_get_drvdata(pdev);
1051 struct android_usb_function *f;
1054 mutex_lock(&dev->mutex);
1056 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
1057 buff += sprintf(buff, "%s,", f->name);
1059 mutex_unlock(&dev->mutex);
1067 functions_store(struct device *pdev, struct device_attribute *attr,
1068 const char *buff, size_t size)
1070 struct android_dev *dev = dev_get_drvdata(pdev);
1073 char aliases[256], *a;
1076 int ffs_enabled = 0;
1078 mutex_lock(&dev->mutex);
1081 mutex_unlock(&dev->mutex);
1085 INIT_LIST_HEAD(&dev->enabled_functions);
1087 strlcpy(buf, buff, sizeof(buf));
1091 name = strsep(&b, ",");
1096 strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
1100 char *alias = strsep(&a, ",");
1101 if (alias && !strcmp(name, alias)) {
1110 err = android_enable_function(dev, "ffs");
1112 pr_err("android_usb: Cannot enable ffs (%d)",
1119 err = android_enable_function(dev, name);
1121 pr_err("android_usb: Cannot enable '%s' (%d)",
1125 mutex_unlock(&dev->mutex);
1130 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1133 struct android_dev *dev = dev_get_drvdata(pdev);
1134 return sprintf(buf, "%d\n", dev->enabled);
1137 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1138 const char *buff, size_t size)
1140 struct android_dev *dev = dev_get_drvdata(pdev);
1141 struct usb_composite_dev *cdev = dev->cdev;
1142 struct android_usb_function *f;
1149 mutex_lock(&dev->mutex);
1151 sscanf(buff, "%d", &enabled);
1152 if (enabled && !dev->enabled) {
1154 * Update values in composite driver's copy of
1155 * device descriptor.
1157 cdev->desc.idVendor = device_desc.idVendor;
1158 cdev->desc.idProduct = device_desc.idProduct;
1159 cdev->desc.bcdDevice = device_desc.bcdDevice;
1160 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1161 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1162 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1163 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1167 android_enable(dev);
1168 dev->enabled = true;
1169 } else if (!enabled && dev->enabled) {
1170 android_disable(dev);
1171 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1175 dev->enabled = false;
1177 pr_err("android_usb: already %s\n",
1178 dev->enabled ? "enabled" : "disabled");
1181 mutex_unlock(&dev->mutex);
1185 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1188 struct android_dev *dev = dev_get_drvdata(pdev);
1189 struct usb_composite_dev *cdev = dev->cdev;
1190 char *state = "DISCONNECTED";
1191 unsigned long flags;
1196 spin_lock_irqsave(&cdev->lock, flags);
1198 state = "CONFIGURED";
1199 else if (dev->connected)
1200 state = "CONNECTED";
1201 spin_unlock_irqrestore(&cdev->lock, flags);
1203 return sprintf(buf, "%s\n", state);
1206 #define DESCRIPTOR_ATTR(field, format_string) \
1208 field ## _show(struct device *dev, struct device_attribute *attr, \
1211 return sprintf(buf, format_string, device_desc.field); \
1214 field ## _store(struct device *dev, struct device_attribute *attr, \
1215 const char *buf, size_t size) \
1218 if (sscanf(buf, format_string, &value) == 1) { \
1219 device_desc.field = value; \
1224 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1226 #define DESCRIPTOR_STRING_ATTR(field, buffer) \
1228 field ## _show(struct device *dev, struct device_attribute *attr, \
1231 return sprintf(buf, "%s", buffer); \
1234 field ## _store(struct device *dev, struct device_attribute *attr, \
1235 const char *buf, size_t size) \
1237 if (size >= sizeof(buffer)) \
1239 return strlcpy(buffer, buf, sizeof(buffer)); \
1241 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1244 DESCRIPTOR_ATTR(idVendor, "%04x\n")
1245 DESCRIPTOR_ATTR(idProduct, "%04x\n")
1246 DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1247 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1248 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1249 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1250 DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1251 DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1252 DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1254 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1256 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1257 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1259 static struct device_attribute *android_usb_attributes[] = {
1261 &dev_attr_idProduct,
1262 &dev_attr_bcdDevice,
1263 &dev_attr_bDeviceClass,
1264 &dev_attr_bDeviceSubClass,
1265 &dev_attr_bDeviceProtocol,
1266 &dev_attr_iManufacturer,
1269 &dev_attr_functions,
1275 /*-------------------------------------------------------------------------*/
1276 /* Composite driver */
1278 static int android_bind_config(struct usb_configuration *c)
1280 struct android_dev *dev = _android_dev;
1283 ret = android_bind_enabled_functions(dev, c);
1290 static void android_unbind_config(struct usb_configuration *c)
1292 struct android_dev *dev = _android_dev;
1294 android_unbind_enabled_functions(dev, c);
1297 static int android_bind(struct usb_composite_dev *cdev)
1299 struct android_dev *dev = _android_dev;
1300 struct usb_gadget *gadget = cdev->gadget;
1304 * Start disconnected. Userspace will connect the gadget once
1305 * it is done configuring the functions.
1307 usb_gadget_disconnect(gadget);
1309 ret = android_init_functions(dev->functions, cdev);
1313 /* Allocate string descriptor numbers ... note that string
1314 * contents can be overridden by the composite_dev glue.
1316 id = usb_string_id(cdev);
1319 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1320 device_desc.iManufacturer = id;
1322 id = usb_string_id(cdev);
1325 strings_dev[STRING_PRODUCT_IDX].id = id;
1326 device_desc.iProduct = id;
1328 /* Default strings - should be updated by userspace */
1329 strncpy(manufacturer_string, "Android", sizeof(manufacturer_string)-1);
1330 strncpy(product_string, "Android", sizeof(product_string) - 1);
1331 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
1333 id = usb_string_id(cdev);
1336 strings_dev[STRING_SERIAL_IDX].id = id;
1337 device_desc.iSerialNumber = id;
1339 gcnum = usb_gadget_controller_number(gadget);
1341 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1343 pr_warning("%s: controller '%s' not recognized\n",
1344 longname, gadget->name);
1345 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
1348 usb_gadget_set_selfpowered(gadget);
1354 static int android_usb_unbind(struct usb_composite_dev *cdev)
1356 struct android_dev *dev = _android_dev;
1358 cancel_work_sync(&dev->work);
1359 android_cleanup_functions(dev->functions);
1363 static struct usb_composite_driver android_usb_driver = {
1364 .name = "android_usb",
1365 .dev = &device_desc,
1366 .strings = dev_strings,
1367 .unbind = android_usb_unbind,
1368 .max_speed = USB_SPEED_HIGH,
1372 android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1374 struct android_dev *dev = _android_dev;
1375 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1376 struct usb_request *req = cdev->req;
1377 struct android_usb_function *f;
1378 int value = -EOPNOTSUPP;
1379 unsigned long flags;
1382 req->complete = composite_setup_complete;
1384 gadget->ep0->driver_data = cdev;
1386 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1387 if (f->ctrlrequest) {
1388 value = f->ctrlrequest(f, cdev, c);
1394 /* Special case the accessory function.
1395 * It needs to handle control requests before it is enabled.
1398 value = acc_ctrlrequest(cdev, c);
1401 value = composite_setup(gadget, c);
1403 spin_lock_irqsave(&cdev->lock, flags);
1404 if (!dev->connected) {
1406 schedule_work(&dev->work);
1407 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1409 schedule_work(&dev->work);
1411 spin_unlock_irqrestore(&cdev->lock, flags);
1416 static void android_disconnect(struct usb_gadget *gadget)
1418 struct android_dev *dev = _android_dev;
1419 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1420 unsigned long flags;
1422 composite_disconnect(gadget);
1424 spin_lock_irqsave(&cdev->lock, flags);
1426 schedule_work(&dev->work);
1427 spin_unlock_irqrestore(&cdev->lock, flags);
1430 static int android_create_device(struct android_dev *dev)
1432 struct device_attribute **attrs = android_usb_attributes;
1433 struct device_attribute *attr;
1436 dev->dev = device_create(android_class, NULL,
1437 MKDEV(0, 0), NULL, "android0");
1438 if (IS_ERR(dev->dev))
1439 return PTR_ERR(dev->dev);
1441 dev_set_drvdata(dev->dev, dev);
1443 while ((attr = *attrs++)) {
1444 err = device_create_file(dev->dev, attr);
1446 device_destroy(android_class, dev->dev->devt);
1454 static int __init init(void)
1456 struct android_dev *dev;
1459 android_class = class_create(THIS_MODULE, "android_usb");
1460 if (IS_ERR(android_class))
1461 return PTR_ERR(android_class);
1463 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1467 dev->disable_depth = 1;
1468 dev->functions = supported_functions;
1469 INIT_LIST_HEAD(&dev->enabled_functions);
1470 INIT_WORK(&dev->work, android_work);
1471 mutex_init(&dev->mutex);
1473 err = android_create_device(dev);
1475 class_destroy(android_class);
1482 /* Override composite driver functions */
1483 composite_driver.setup = android_setup;
1484 composite_driver.disconnect = android_disconnect;
1486 return usb_composite_probe(&android_usb_driver, android_bind);
1490 static void __exit cleanup(void)
1492 usb_composite_unregister(&android_usb_driver);
1493 class_destroy(android_class);
1494 kfree(_android_dev);
1495 _android_dev = NULL;
1497 module_exit(cleanup);