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 #include "f_audio_source.c"
36 #include "f_mass_storage.c"
38 #include "f_accessory.c"
39 #define USB_ETH_RNDIS y
44 MODULE_AUTHOR("Mike Lockwood");
45 MODULE_DESCRIPTION("Android Composite USB Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION("1.0");
49 static const char longname[] = "Gadget Android";
51 /* Default vendor and product IDs, overridden by userspace */
52 #define VENDOR_ID 0x2207
53 #define PRODUCT_ID 0x2910
55 /* f_midi configuration */
56 #define MIDI_INPUT_PORTS 1
57 #define MIDI_OUTPUT_PORTS 1
58 #define MIDI_BUFFER_SIZE 256
59 #define MIDI_QUEUE_LENGTH 32
61 struct android_usb_function {
67 struct device_attribute **attributes;
69 /* for android_dev.enabled_functions */
70 struct list_head enabled_list;
72 /* Optional: initialization during gadget bind */
73 int (*init)(struct android_usb_function *, struct usb_composite_dev *);
74 /* Optional: cleanup during gadget unbind */
75 void (*cleanup)(struct android_usb_function *);
76 /* Optional: called when the function is added the list of
77 * enabled functions */
78 void (*enable)(struct android_usb_function *);
79 /* Optional: called when it is removed */
80 void (*disable)(struct android_usb_function *);
82 int (*bind_config)(struct android_usb_function *,
83 struct usb_configuration *);
85 /* Optional: called when the configuration is removed */
86 void (*unbind_config)(struct android_usb_function *,
87 struct usb_configuration *);
88 /* Optional: handle ctrl requests before the device is configured */
89 int (*ctrlrequest)(struct android_usb_function *,
90 struct usb_composite_dev *,
91 const struct usb_ctrlrequest *);
95 struct android_usb_function **functions;
96 struct list_head enabled_functions;
97 struct usb_composite_dev *cdev;
100 void (*setup_complete)(struct usb_ep *ep,
101 struct usb_request *req);
108 struct work_struct work;
109 char ffs_aliases[256];
112 static struct class *android_class;
113 static struct android_dev *_android_dev;
114 static int android_bind_config(struct usb_configuration *c);
115 static void android_unbind_config(struct usb_configuration *c);
117 /* string IDs are assigned dynamically */
118 #define STRING_MANUFACTURER_IDX 0
119 #define STRING_PRODUCT_IDX 1
120 #define STRING_SERIAL_IDX 2
122 static char manufacturer_string[256];
123 static char product_string[256];
124 static char serial_string[256];
127 static struct usb_string strings_dev[] = {
128 [STRING_MANUFACTURER_IDX].s = manufacturer_string,
129 [STRING_PRODUCT_IDX].s = product_string,
130 [STRING_SERIAL_IDX].s = serial_string,
131 { } /* end of list */
134 static struct usb_gadget_strings stringtab_dev = {
135 .language = 0x0409, /* en-us */
136 .strings = strings_dev,
139 static struct usb_gadget_strings *dev_strings[] = {
144 static struct usb_device_descriptor device_desc = {
145 .bLength = sizeof(device_desc),
146 .bDescriptorType = USB_DT_DEVICE,
147 .bcdUSB = __constant_cpu_to_le16(0x0200),
148 .bDeviceClass = USB_CLASS_PER_INTERFACE,
149 .idVendor = __constant_cpu_to_le16(VENDOR_ID),
150 .idProduct = __constant_cpu_to_le16(PRODUCT_ID),
151 .bcdDevice = __constant_cpu_to_le16(0xffff),
152 .bNumConfigurations = 1,
155 static struct usb_configuration android_config_driver = {
157 .unbind = android_unbind_config,
158 .bConfigurationValue = 1,
159 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
160 .MaxPower = 500, /* 500ma */
163 static void android_work(struct work_struct *data)
165 struct android_dev *dev = container_of(data, struct android_dev, work);
166 struct usb_composite_dev *cdev = dev->cdev;
167 char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
168 char *connected[2] = { "USB_STATE=CONNECTED", NULL };
169 char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
170 char **uevent_envp = NULL;
173 spin_lock_irqsave(&cdev->lock, flags);
175 uevent_envp = configured;
176 else if (dev->connected != dev->sw_connected)
177 uevent_envp = dev->connected ? connected : disconnected;
178 dev->sw_connected = dev->connected;
179 spin_unlock_irqrestore(&cdev->lock, flags);
182 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
183 pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
185 pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
186 dev->connected, dev->sw_connected, cdev->config);
190 static void android_enable(struct android_dev *dev)
192 struct usb_composite_dev *cdev = dev->cdev;
194 if (WARN_ON(!dev->disable_depth))
197 if (--dev->disable_depth == 0) {
198 usb_add_config(cdev, &android_config_driver,
199 android_bind_config);
200 usb_gadget_connect(cdev->gadget);
204 static void android_disable(struct android_dev *dev)
206 struct usb_composite_dev *cdev = dev->cdev;
208 if (dev->disable_depth++ == 0) {
209 usb_gadget_disconnect(cdev->gadget);
210 /* Cancel pending control requests */
211 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
212 usb_remove_config(cdev, &android_config_driver);
216 /*-------------------------------------------------------------------------*/
217 /* Supported functions initialization */
219 struct functionfs_config {
222 struct ffs_data *data;
225 static int ffs_function_init(struct android_usb_function *f,
226 struct usb_composite_dev *cdev)
228 f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
232 return functionfs_init();
235 static void ffs_function_cleanup(struct android_usb_function *f)
237 functionfs_cleanup();
241 static void ffs_function_enable(struct android_usb_function *f)
243 struct android_dev *dev = _android_dev;
244 struct functionfs_config *config = f->config;
246 config->enabled = true;
248 /* Disable the gadget until the function is ready */
250 android_disable(dev);
253 static void ffs_function_disable(struct android_usb_function *f)
255 struct android_dev *dev = _android_dev;
256 struct functionfs_config *config = f->config;
258 config->enabled = false;
260 /* Balance the disable that was called in closed_callback */
265 static int ffs_function_bind_config(struct android_usb_function *f,
266 struct usb_configuration *c)
268 struct functionfs_config *config = f->config;
269 return functionfs_bind_config(c->cdev, c, config->data);
273 ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
275 struct android_dev *dev = _android_dev;
278 mutex_lock(&dev->mutex);
279 ret = sprintf(buf, "%s\n", dev->ffs_aliases);
280 mutex_unlock(&dev->mutex);
286 ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
287 const char *buf, size_t size)
289 struct android_dev *dev = _android_dev;
292 mutex_lock(&dev->mutex);
295 mutex_unlock(&dev->mutex);
299 strlcpy(buff, buf, sizeof(buff));
300 strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
302 mutex_unlock(&dev->mutex);
307 static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
309 static struct device_attribute *ffs_function_attributes[] = {
314 static struct android_usb_function ffs_function = {
316 .init = ffs_function_init,
317 .enable = ffs_function_enable,
318 .disable = ffs_function_disable,
319 .cleanup = ffs_function_cleanup,
320 .bind_config = ffs_function_bind_config,
321 .attributes = ffs_function_attributes,
324 static int functionfs_ready_callback(struct ffs_data *ffs)
326 struct android_dev *dev = _android_dev;
327 struct functionfs_config *config = ffs_function.config;
330 mutex_lock(&dev->mutex);
332 ret = functionfs_bind(ffs, dev->cdev);
337 config->opened = true;
343 mutex_unlock(&dev->mutex);
347 static void functionfs_closed_callback(struct ffs_data *ffs)
349 struct android_dev *dev = _android_dev;
350 struct functionfs_config *config = ffs_function.config;
352 mutex_lock(&dev->mutex);
355 android_disable(dev);
357 config->opened = false;
360 if (!WARN_ON(!ffs->gadget)) {
361 dev->cdev->next_string_id -= ffs->strings_count;
363 functionfs_unbind(ffs);
365 mutex_unlock(&dev->mutex);
368 static void *functionfs_acquire_dev_callback(const char *dev_name)
373 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
377 #define MAX_ACM_INSTANCES 4
378 struct acm_function_config {
381 struct usb_function *f_acm[MAX_ACM_INSTANCES];
382 struct usb_function_instance *f_acm_inst[MAX_ACM_INSTANCES];
386 acm_function_init(struct android_usb_function *f,
387 struct usb_composite_dev *cdev)
391 struct acm_function_config *config;
393 config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
398 for (i = 0; i < MAX_ACM_INSTANCES; i++) {
399 config->f_acm_inst[i] = usb_get_function_instance("acm");
400 if (IS_ERR(config->f_acm_inst[i])) {
401 ret = PTR_ERR(config->f_acm_inst[i]);
402 goto err_usb_get_function_instance;
404 config->f_acm[i] = usb_get_function(config->f_acm_inst[i]);
405 if (IS_ERR(config->f_acm[i])) {
406 ret = PTR_ERR(config->f_acm[i]);
407 goto err_usb_get_function;
411 err_usb_get_function_instance:
413 usb_put_function(config->f_acm[i]);
414 err_usb_get_function:
415 usb_put_function_instance(config->f_acm_inst[i]);
420 static void acm_function_cleanup(struct android_usb_function *f)
423 struct acm_function_config *config = f->config;
425 for (i = 0; i < MAX_ACM_INSTANCES; i++) {
426 usb_put_function(config->f_acm[i]);
427 usb_put_function_instance(config->f_acm_inst[i]);
434 acm_function_bind_config(struct android_usb_function *f,
435 struct usb_configuration *c)
439 struct acm_function_config *config = f->config;
441 config->instances_on = config->instances;
442 for (i = 0; i < config->instances_on; i++) {
443 ret = usb_add_function(c, config->f_acm[i]);
445 pr_err("Could not bind acm%u config\n", i);
446 goto err_usb_add_function;
452 err_usb_add_function:
454 usb_remove_function(c, config->f_acm[i]);
458 static void acm_function_unbind_config(struct android_usb_function *f,
459 struct usb_configuration *c)
462 struct acm_function_config *config = f->config;
464 for (i = 0; i < config->instances_on; i++)
465 usb_remove_function(c, config->f_acm[i]);
468 static ssize_t acm_instances_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct android_usb_function *f = dev_get_drvdata(dev);
472 struct acm_function_config *config = f->config;
473 return sprintf(buf, "%d\n", config->instances);
476 static ssize_t acm_instances_store(struct device *dev,
477 struct device_attribute *attr, const char *buf, size_t size)
479 struct android_usb_function *f = dev_get_drvdata(dev);
480 struct acm_function_config *config = f->config;
483 sscanf(buf, "%d", &value);
484 if (value > MAX_ACM_INSTANCES)
485 value = MAX_ACM_INSTANCES;
486 config->instances = value;
490 static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
491 acm_instances_store);
492 static struct device_attribute *acm_function_attributes[] = {
497 static struct android_usb_function acm_function = {
499 .init = acm_function_init,
500 .cleanup = acm_function_cleanup,
501 .bind_config = acm_function_bind_config,
502 .unbind_config = acm_function_unbind_config,
503 .attributes = acm_function_attributes,
508 mtp_function_init(struct android_usb_function *f,
509 struct usb_composite_dev *cdev)
514 static void mtp_function_cleanup(struct android_usb_function *f)
520 mtp_function_bind_config(struct android_usb_function *f,
521 struct usb_configuration *c)
523 return mtp_bind_config(c, false);
527 ptp_function_init(struct android_usb_function *f,
528 struct usb_composite_dev *cdev)
530 /* nothing to do - initialization is handled by mtp_function_init */
534 static void ptp_function_cleanup(struct android_usb_function *f)
536 /* nothing to do - cleanup is handled by mtp_function_cleanup */
540 ptp_function_bind_config(struct android_usb_function *f,
541 struct usb_configuration *c)
543 return mtp_bind_config(c, true);
546 static int mtp_function_ctrlrequest(struct android_usb_function *f,
547 struct usb_composite_dev *cdev,
548 const struct usb_ctrlrequest *c)
550 return mtp_ctrlrequest(cdev, c);
553 static struct android_usb_function mtp_function = {
555 .init = mtp_function_init,
556 .cleanup = mtp_function_cleanup,
557 .bind_config = mtp_function_bind_config,
558 .ctrlrequest = mtp_function_ctrlrequest,
561 /* PTP function is same as MTP with slightly different interface descriptor */
562 static struct android_usb_function ptp_function = {
564 .init = ptp_function_init,
565 .cleanup = ptp_function_cleanup,
566 .bind_config = ptp_function_bind_config,
570 struct rndis_function_config {
571 u8 ethaddr[ETH_ALEN];
573 char manufacturer[256];
574 /* "Wireless" RNDIS; auto-detected by Windows */
580 rndis_function_init(struct android_usb_function *f,
581 struct usb_composite_dev *cdev)
583 f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
589 static void rndis_function_cleanup(struct android_usb_function *f)
596 rndis_function_bind_config(struct android_usb_function *f,
597 struct usb_configuration *c)
601 struct rndis_function_config *rndis = f->config;
604 pr_err("%s: rndis_pdata\n", __func__);
608 pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
609 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
610 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
612 dev = gether_setup_name(c->cdev->gadget, rndis->ethaddr, "rndis");
615 pr_err("%s: gether_setup failed\n", __func__);
621 /* "Wireless" RNDIS; auto-detected by Windows */
622 rndis_iad_descriptor.bFunctionClass =
623 USB_CLASS_WIRELESS_CONTROLLER;
624 rndis_iad_descriptor.bFunctionSubClass = 0x01;
625 rndis_iad_descriptor.bFunctionProtocol = 0x03;
626 rndis_control_intf.bInterfaceClass =
627 USB_CLASS_WIRELESS_CONTROLLER;
628 rndis_control_intf.bInterfaceSubClass = 0x01;
629 rndis_control_intf.bInterfaceProtocol = 0x03;
632 return rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
633 rndis->manufacturer, rndis->dev);
636 static void rndis_function_unbind_config(struct android_usb_function *f,
637 struct usb_configuration *c)
639 struct rndis_function_config *rndis = f->config;
640 gether_cleanup(rndis->dev);
643 static ssize_t rndis_manufacturer_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
646 struct android_usb_function *f = dev_get_drvdata(dev);
647 struct rndis_function_config *config = f->config;
648 return sprintf(buf, "%s\n", config->manufacturer);
651 static ssize_t rndis_manufacturer_store(struct device *dev,
652 struct device_attribute *attr, const char *buf, size_t size)
654 struct android_usb_function *f = dev_get_drvdata(dev);
655 struct rndis_function_config *config = f->config;
657 if (size >= sizeof(config->manufacturer))
659 if (sscanf(buf, "%s", config->manufacturer) == 1)
664 static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
665 rndis_manufacturer_store);
667 static ssize_t rndis_wceis_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
670 struct android_usb_function *f = dev_get_drvdata(dev);
671 struct rndis_function_config *config = f->config;
672 return sprintf(buf, "%d\n", config->wceis);
675 static ssize_t rndis_wceis_store(struct device *dev,
676 struct device_attribute *attr, const char *buf, size_t size)
678 struct android_usb_function *f = dev_get_drvdata(dev);
679 struct rndis_function_config *config = f->config;
682 if (sscanf(buf, "%d", &value) == 1) {
683 config->wceis = value;
689 static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
692 static ssize_t rndis_ethaddr_show(struct device *dev,
693 struct device_attribute *attr, char *buf)
695 struct android_usb_function *f = dev_get_drvdata(dev);
696 struct rndis_function_config *rndis = f->config;
697 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
698 rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
699 rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
702 static ssize_t rndis_ethaddr_store(struct device *dev,
703 struct device_attribute *attr, const char *buf, size_t size)
705 struct android_usb_function *f = dev_get_drvdata(dev);
706 struct rndis_function_config *rndis = f->config;
708 if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
709 (int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
710 (int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
711 (int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
716 static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
717 rndis_ethaddr_store);
719 static ssize_t rndis_vendorID_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
722 struct android_usb_function *f = dev_get_drvdata(dev);
723 struct rndis_function_config *config = f->config;
724 return sprintf(buf, "%04x\n", config->vendorID);
727 static ssize_t rndis_vendorID_store(struct device *dev,
728 struct device_attribute *attr, const char *buf, size_t size)
730 struct android_usb_function *f = dev_get_drvdata(dev);
731 struct rndis_function_config *config = f->config;
734 if (sscanf(buf, "%04x", &value) == 1) {
735 config->vendorID = value;
741 static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
742 rndis_vendorID_store);
744 static struct device_attribute *rndis_function_attributes[] = {
745 &dev_attr_manufacturer,
752 static struct android_usb_function rndis_function = {
754 .init = rndis_function_init,
755 .cleanup = rndis_function_cleanup,
756 .bind_config = rndis_function_bind_config,
757 .unbind_config = rndis_function_unbind_config,
758 .attributes = rndis_function_attributes,
762 struct mass_storage_function_config {
763 struct fsg_config fsg;
764 struct fsg_common *common;
767 static int mass_storage_function_init(struct android_usb_function *f,
768 struct usb_composite_dev *cdev)
770 struct mass_storage_function_config *config;
771 struct fsg_common *common;
775 config = kzalloc(sizeof(struct mass_storage_function_config),
780 config->fsg.nluns = 2;
783 for (i = 0; i < config->fsg.nluns; i++) {
784 config->fsg.luns[i].removable = 1;
785 config->fsg.luns[i].nofua = 1;
788 common = fsg_common_init(NULL, cdev, &config->fsg);
789 if (IS_ERR(common)) {
791 return PTR_ERR(common);
794 for (i = 0; i < config->fsg.nluns; i++) {
795 err = sysfs_create_link(&f->dev->kobj,
796 &common->luns[i].dev.kobj,
802 config->common = common;
807 sysfs_remove_link(&f->dev->kobj, name[i-1]);
808 fsg_common_release(&common->ref);
813 static void mass_storage_function_cleanup(struct android_usb_function *f)
819 static int mass_storage_function_bind_config(struct android_usb_function *f,
820 struct usb_configuration *c)
822 struct mass_storage_function_config *config = f->config;
823 return fsg_bind_config(c->cdev, c, config->common);
826 static ssize_t mass_storage_inquiry_show(struct device *dev,
827 struct device_attribute *attr, char *buf)
829 struct android_usb_function *f = dev_get_drvdata(dev);
830 struct mass_storage_function_config *config = f->config;
831 return sprintf(buf, "%s\n", config->common->inquiry_string);
834 static ssize_t mass_storage_inquiry_store(struct device *dev,
835 struct device_attribute *attr, const char *buf, size_t size)
837 struct android_usb_function *f = dev_get_drvdata(dev);
838 struct mass_storage_function_config *config = f->config;
839 if (size >= sizeof(config->common->inquiry_string))
841 if (sscanf(buf, "%s", config->common->inquiry_string) != 1)
846 static DEVICE_ATTR(inquiry_string, S_IRUGO | S_IWUSR,
847 mass_storage_inquiry_show,
848 mass_storage_inquiry_store);
850 static struct device_attribute *mass_storage_function_attributes[] = {
851 &dev_attr_inquiry_string,
855 static struct android_usb_function mass_storage_function = {
856 .name = "mass_storage",
857 .init = mass_storage_function_init,
858 .cleanup = mass_storage_function_cleanup,
859 .bind_config = mass_storage_function_bind_config,
860 .attributes = mass_storage_function_attributes,
864 static int accessory_function_init(struct android_usb_function *f,
865 struct usb_composite_dev *cdev)
870 static void accessory_function_cleanup(struct android_usb_function *f)
875 static int accessory_function_bind_config(struct android_usb_function *f,
876 struct usb_configuration *c)
878 return acc_bind_config(c);
881 static int accessory_function_ctrlrequest(struct android_usb_function *f,
882 struct usb_composite_dev *cdev,
883 const struct usb_ctrlrequest *c)
885 return acc_ctrlrequest(cdev, c);
888 static struct android_usb_function accessory_function = {
890 .init = accessory_function_init,
891 .cleanup = accessory_function_cleanup,
892 .bind_config = accessory_function_bind_config,
893 .ctrlrequest = accessory_function_ctrlrequest,
896 static int audio_source_function_init(struct android_usb_function *f,
897 struct usb_composite_dev *cdev)
899 struct audio_source_config *config;
901 config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
910 static void audio_source_function_cleanup(struct android_usb_function *f)
915 static int audio_source_function_bind_config(struct android_usb_function *f,
916 struct usb_configuration *c)
918 struct audio_source_config *config = f->config;
920 return audio_source_bind_config(c, config);
923 static void audio_source_function_unbind_config(struct android_usb_function *f,
924 struct usb_configuration *c)
926 struct audio_source_config *config = f->config;
932 static ssize_t audio_source_pcm_show(struct device *dev,
933 struct device_attribute *attr, char *buf)
935 struct android_usb_function *f = dev_get_drvdata(dev);
936 struct audio_source_config *config = f->config;
938 /* print PCM card and device numbers */
939 return sprintf(buf, "%d %d\n", config->card, config->device);
942 static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL);
944 static struct device_attribute *audio_source_function_attributes[] = {
949 static struct android_usb_function audio_source_function = {
950 .name = "audio_source",
951 .init = audio_source_function_init,
952 .cleanup = audio_source_function_cleanup,
953 .bind_config = audio_source_function_bind_config,
954 .unbind_config = audio_source_function_unbind_config,
955 .attributes = audio_source_function_attributes,
958 static int midi_function_init(struct android_usb_function *f,
959 struct usb_composite_dev *cdev)
961 struct midi_alsa_config *config;
963 config = kzalloc(sizeof(struct midi_alsa_config), GFP_KERNEL);
972 static void midi_function_cleanup(struct android_usb_function *f)
977 static int midi_function_bind_config(struct android_usb_function *f,
978 struct usb_configuration *c)
980 struct midi_alsa_config *config = f->config;
982 return f_midi_bind_config(c, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
983 MIDI_INPUT_PORTS, MIDI_OUTPUT_PORTS, MIDI_BUFFER_SIZE,
984 MIDI_QUEUE_LENGTH, config);
987 static ssize_t midi_alsa_show(struct device *dev,
988 struct device_attribute *attr, char *buf)
990 struct android_usb_function *f = dev_get_drvdata(dev);
991 struct midi_alsa_config *config = f->config;
993 /* print ALSA card and device numbers */
994 return sprintf(buf, "%d %d\n", config->card, config->device);
997 static DEVICE_ATTR(alsa, S_IRUGO, midi_alsa_show, NULL);
999 static struct device_attribute *midi_function_attributes[] = {
1004 static struct android_usb_function midi_function = {
1006 .init = midi_function_init,
1007 .cleanup = midi_function_cleanup,
1008 .bind_config = midi_function_bind_config,
1009 .attributes = midi_function_attributes,
1012 static struct android_usb_function *supported_functions[] = {
1018 &mass_storage_function,
1019 &accessory_function,
1020 &audio_source_function,
1025 static int android_init_functions(struct android_usb_function **functions,
1026 struct usb_composite_dev *cdev)
1028 struct android_dev *dev = _android_dev;
1029 struct android_usb_function *f;
1030 struct device_attribute **attrs;
1031 struct device_attribute *attr;
1035 for (; (f = *functions++); index++) {
1036 f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
1037 f->dev = device_create(android_class, dev->dev,
1038 MKDEV(0, index), f, f->dev_name);
1039 if (IS_ERR(f->dev)) {
1040 pr_err("%s: Failed to create dev %s", __func__,
1042 err = PTR_ERR(f->dev);
1047 err = f->init(f, cdev);
1049 pr_err("%s: Failed to init %s", __func__,
1055 attrs = f->attributes;
1057 while ((attr = *attrs++) && !err)
1058 err = device_create_file(f->dev, attr);
1061 pr_err("%s: Failed to create function %s attributes",
1069 device_destroy(android_class, f->dev->devt);
1075 static void android_cleanup_functions(struct android_usb_function **functions)
1077 struct android_usb_function *f;
1079 while (*functions) {
1083 device_destroy(android_class, f->dev->devt);
1093 android_bind_enabled_functions(struct android_dev *dev,
1094 struct usb_configuration *c)
1096 struct android_usb_function *f;
1099 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1100 ret = f->bind_config(f, c);
1102 pr_err("%s: %s failed", __func__, f->name);
1110 android_unbind_enabled_functions(struct android_dev *dev,
1111 struct usb_configuration *c)
1113 struct android_usb_function *f;
1115 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1116 if (f->unbind_config)
1117 f->unbind_config(f, c);
1121 static int android_enable_function(struct android_dev *dev, char *name)
1123 struct android_usb_function **functions = dev->functions;
1124 struct android_usb_function *f;
1125 while ((f = *functions++)) {
1126 if (!strcmp(name, f->name)) {
1127 list_add_tail(&f->enabled_list,
1128 &dev->enabled_functions);
1135 /*-------------------------------------------------------------------------*/
1136 /* /sys/class/android_usb/android%d/ interface */
1139 functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
1141 struct android_dev *dev = dev_get_drvdata(pdev);
1142 struct android_usb_function *f;
1145 mutex_lock(&dev->mutex);
1147 list_for_each_entry(f, &dev->enabled_functions, enabled_list)
1148 buff += sprintf(buff, "%s,", f->name);
1150 mutex_unlock(&dev->mutex);
1158 functions_store(struct device *pdev, struct device_attribute *attr,
1159 const char *buff, size_t size)
1161 struct android_dev *dev = dev_get_drvdata(pdev);
1164 char aliases[256], *a;
1167 int ffs_enabled = 0;
1169 mutex_lock(&dev->mutex);
1172 mutex_unlock(&dev->mutex);
1176 INIT_LIST_HEAD(&dev->enabled_functions);
1178 strlcpy(buf, buff, sizeof(buf));
1182 name = strsep(&b, ",");
1187 strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
1191 char *alias = strsep(&a, ",");
1192 if (alias && !strcmp(name, alias)) {
1201 err = android_enable_function(dev, "ffs");
1203 pr_err("android_usb: Cannot enable ffs (%d)",
1210 err = android_enable_function(dev, name);
1212 pr_err("android_usb: Cannot enable '%s' (%d)",
1216 mutex_unlock(&dev->mutex);
1221 static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
1224 struct android_dev *dev = dev_get_drvdata(pdev);
1225 return sprintf(buf, "%d\n", dev->enabled);
1228 static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
1229 const char *buff, size_t size)
1231 struct android_dev *dev = dev_get_drvdata(pdev);
1232 struct usb_composite_dev *cdev = dev->cdev;
1233 struct android_usb_function *f;
1240 mutex_lock(&dev->mutex);
1242 sscanf(buff, "%d", &enabled);
1243 if (enabled && !dev->enabled) {
1245 * Update values in composite driver's copy of
1246 * device descriptor.
1248 cdev->desc.idVendor = device_desc.idVendor;
1249 cdev->desc.idProduct = device_desc.idProduct;
1250 cdev->desc.bcdDevice = device_desc.bcdDevice;
1251 cdev->desc.bDeviceClass = device_desc.bDeviceClass;
1252 cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
1253 cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
1254 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1258 android_enable(dev);
1259 dev->enabled = true;
1260 } else if (!enabled && dev->enabled) {
1261 android_disable(dev);
1262 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1266 dev->enabled = false;
1268 pr_err("android_usb: already %s\n",
1269 dev->enabled ? "enabled" : "disabled");
1272 mutex_unlock(&dev->mutex);
1276 static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
1279 struct android_dev *dev = dev_get_drvdata(pdev);
1280 struct usb_composite_dev *cdev = dev->cdev;
1281 char *state = "DISCONNECTED";
1282 unsigned long flags;
1287 spin_lock_irqsave(&cdev->lock, flags);
1289 state = "CONFIGURED";
1290 else if (dev->connected)
1291 state = "CONNECTED";
1292 spin_unlock_irqrestore(&cdev->lock, flags);
1294 return sprintf(buf, "%s\n", state);
1297 #define DESCRIPTOR_ATTR(field, format_string) \
1299 field ## _show(struct device *dev, struct device_attribute *attr, \
1302 return sprintf(buf, format_string, device_desc.field); \
1305 field ## _store(struct device *dev, struct device_attribute *attr, \
1306 const char *buf, size_t size) \
1309 if (sscanf(buf, format_string, &value) == 1) { \
1310 device_desc.field = value; \
1315 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1317 #define DESCRIPTOR_STRING_ATTR(field, buffer) \
1319 field ## _show(struct device *dev, struct device_attribute *attr, \
1322 return sprintf(buf, "%s", buffer); \
1325 field ## _store(struct device *dev, struct device_attribute *attr, \
1326 const char *buf, size_t size) \
1328 if (size >= sizeof(buffer)) \
1330 return strlcpy(buffer, buf, sizeof(buffer)); \
1332 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1335 DESCRIPTOR_ATTR(idVendor, "%04x\n")
1336 DESCRIPTOR_ATTR(idProduct, "%04x\n")
1337 DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
1338 DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
1339 DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
1340 DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
1341 DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
1342 DESCRIPTOR_STRING_ATTR(iProduct, product_string)
1343 DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
1345 static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
1347 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
1348 static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
1350 static struct device_attribute *android_usb_attributes[] = {
1352 &dev_attr_idProduct,
1353 &dev_attr_bcdDevice,
1354 &dev_attr_bDeviceClass,
1355 &dev_attr_bDeviceSubClass,
1356 &dev_attr_bDeviceProtocol,
1357 &dev_attr_iManufacturer,
1360 &dev_attr_functions,
1366 /*-------------------------------------------------------------------------*/
1367 /* Composite driver */
1369 static int android_bind_config(struct usb_configuration *c)
1371 struct android_dev *dev = _android_dev;
1374 ret = android_bind_enabled_functions(dev, c);
1381 static void android_unbind_config(struct usb_configuration *c)
1383 struct android_dev *dev = _android_dev;
1385 android_unbind_enabled_functions(dev, c);
1388 static int android_bind(struct usb_composite_dev *cdev)
1390 struct android_dev *dev = _android_dev;
1391 struct usb_gadget *gadget = cdev->gadget;
1394 /* Save the default handler */
1395 dev->setup_complete = cdev->req->complete;
1398 * Start disconnected. Userspace will connect the gadget once
1399 * it is done configuring the functions.
1401 usb_gadget_disconnect(gadget);
1403 ret = android_init_functions(dev->functions, cdev);
1407 /* Allocate string descriptor numbers ... note that string
1408 * contents can be overridden by the composite_dev glue.
1410 id = usb_string_id(cdev);
1413 strings_dev[STRING_MANUFACTURER_IDX].id = id;
1414 device_desc.iManufacturer = id;
1416 id = usb_string_id(cdev);
1419 strings_dev[STRING_PRODUCT_IDX].id = id;
1420 device_desc.iProduct = id;
1422 /* Default strings - should be updated by userspace */
1423 strncpy(manufacturer_string, "Android", sizeof(manufacturer_string)-1);
1424 strncpy(product_string, "Android", sizeof(product_string) - 1);
1425 strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
1427 id = usb_string_id(cdev);
1430 strings_dev[STRING_SERIAL_IDX].id = id;
1431 device_desc.iSerialNumber = id;
1432 device_desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1434 usb_gadget_set_selfpowered(gadget);
1440 static int android_usb_unbind(struct usb_composite_dev *cdev)
1442 struct android_dev *dev = _android_dev;
1444 cancel_work_sync(&dev->work);
1445 android_cleanup_functions(dev->functions);
1449 /* HACK: android needs to override setup for accessory to work */
1450 static int (*composite_setup_func)(struct usb_gadget *gadget, const struct usb_ctrlrequest *c);
1453 android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
1455 struct android_dev *dev = _android_dev;
1456 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1457 struct usb_request *req = cdev->req;
1458 struct android_usb_function *f;
1459 int value = -EOPNOTSUPP;
1460 unsigned long flags;
1464 req->complete = dev->setup_complete;
1465 gadget->ep0->driver_data = cdev;
1467 list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
1468 if (f->ctrlrequest) {
1469 value = f->ctrlrequest(f, cdev, c);
1475 /* Special case the accessory function.
1476 * It needs to handle control requests before it is enabled.
1479 value = acc_ctrlrequest(cdev, c);
1482 value = composite_setup_func(gadget, c);
1484 spin_lock_irqsave(&cdev->lock, flags);
1485 if (!dev->connected) {
1487 schedule_work(&dev->work);
1488 } else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
1490 schedule_work(&dev->work);
1492 spin_unlock_irqrestore(&cdev->lock, flags);
1497 static void android_disconnect(struct usb_composite_dev *cdev)
1499 struct android_dev *dev = _android_dev;
1501 /* accessory HID support can be active while the
1502 accessory function is not actually enabled,
1503 so we need to inform it when we are disconnected.
1508 schedule_work(&dev->work);
1511 static struct usb_composite_driver android_usb_driver = {
1512 .name = "android_usb",
1513 .dev = &device_desc,
1514 .strings = dev_strings,
1515 .bind = android_bind,
1516 .unbind = android_usb_unbind,
1517 .disconnect = android_disconnect,
1518 .max_speed = USB_SPEED_HIGH,
1521 static int android_create_device(struct android_dev *dev)
1523 struct device_attribute **attrs = android_usb_attributes;
1524 struct device_attribute *attr;
1527 dev->dev = device_create(android_class, NULL,
1528 MKDEV(0, 0), NULL, "android0");
1529 if (IS_ERR(dev->dev))
1530 return PTR_ERR(dev->dev);
1532 dev_set_drvdata(dev->dev, dev);
1534 while ((attr = *attrs++)) {
1535 err = device_create_file(dev->dev, attr);
1537 device_destroy(android_class, dev->dev->devt);
1545 static int __init init(void)
1547 struct android_dev *dev;
1550 android_class = class_create(THIS_MODULE, "android_usb");
1551 if (IS_ERR(android_class))
1552 return PTR_ERR(android_class);
1554 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1560 dev->disable_depth = 1;
1561 dev->functions = supported_functions;
1562 INIT_LIST_HEAD(&dev->enabled_functions);
1563 INIT_WORK(&dev->work, android_work);
1564 mutex_init(&dev->mutex);
1566 err = android_create_device(dev);
1568 pr_err("%s: failed to create android device %d", __func__, err);
1574 err = usb_composite_probe(&android_usb_driver);
1576 pr_err("%s: failed to probe driver %d", __func__, err);
1580 /* HACK: exchange composite's setup with ours */
1581 composite_setup_func = android_usb_driver.gadget_driver.setup;
1582 android_usb_driver.gadget_driver.setup = android_setup;
1587 device_destroy(android_class, dev->dev->devt);
1591 class_destroy(android_class);
1594 late_initcall(init);
1596 static void __exit cleanup(void)
1598 usb_composite_unregister(&android_usb_driver);
1599 class_destroy(android_class);
1600 kfree(_android_dev);
1601 _android_dev = NULL;
1603 module_exit(cleanup);