blob: c70e79398923e822a53d7a178ca3d5142bbd4a47 [file] [log] [blame]
/*
* Gadget Driver for Android
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@android.com>
* Benoit Goby <benoit@android.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/platform_device.h>
#include <linux/usb/ch9.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include <linux/completion.h>
#include "./function/gadget_chips.h"
#include "./function/f_adb.c"
#ifndef CONFIG_USB_TELEPHONY
#include "./function/pxa910_u_serial.c"
#include "./function/pxa910_f_diag.c"
#include "./function/pxa910_f_modem.c"
#endif
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
#include "./function/f_fs.c"
#endif
#ifndef CONFIG_USB_TELEPHONY
#include "./function/pxa182x_serial_debug.c"
#include "./function/pxa182x_acm_debug.c"
#endif
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
#include "./function/f_midi.c"
#include "./function/f_audio_source.c"
#include "./function/f_accessory.c"
#endif
#define DEF_CDROM_PATH "/etc/cdrom/CDROM.iso"
#define APPLE_CDROM_PATH "/etc/cdrom/CDROM.iso"
#define LINUX_CDROM_PATH "/etc/cdrom/CDROM.iso"
#define WIN_CDROM_PATH "/etc/cdrom/CDROM.iso"
#include "./function/f_mass_storage.c"
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
#include "./function/f_mtp.c"
#endif
#define USB_ETH_RNDIS y
#include "./function/f_rndis.c"
#include "./function/rndis.c"
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
#include "./function/f_ecm.c"
#endif
#if defined(CONFIG_USB_ETH_RNDIS_ECM)
#include "./function/u_ether_rndis_ecm.c"
#elif defined(CONFIG_ASR_TOE)
#include "./function/u_ether_toe.c"
#else
#include "./function/u_ether.c"
#endif
#if defined (CONFIG_USB_G_MBIM)
#include "./function/f_mbim.c"
#endif
#if defined (CONFIG_USB_TELEPHONY)
#include "f_usbtel.c"
#endif
#if defined (CONFIG_ASR_UAC1)
#include "./function/u_asr_uac1.c"
#endif
MODULE_AUTHOR("Mike Lockwood");
MODULE_DESCRIPTION("Android Composite USB Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");
static const char longname[] = "Gadget Android";
#if defined (CONFIG_USB_G_MBIM)
static bool mbim_enabled = 0;
#endif
/* Default vendor and product IDs, overridden by userspace */
#define VENDOR_ID 0x18D1
#define PRODUCT_ID 0x0001
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
/* f_midi configuration */
#define MIDI_INPUT_PORTS 1
#define MIDI_OUTPUT_PORTS 1
#define MIDI_BUFFER_SIZE 256
#define MIDI_QUEUE_LENGTH 32
#endif
struct android_usb_function {
char *name;
void *config;
struct device *dev;
char *dev_name;
struct device_attribute **attributes;
/* for android_dev.enabled_functions */
struct list_head enabled_list;
/* Optional: initialization during gadget bind */
int (*init)(struct android_usb_function *, struct usb_composite_dev *);
/* Optional: cleanup during gadget unbind */
void (*cleanup)(struct android_usb_function *);
/* Optional: called when the function is added the list of
* enabled functions */
void (*enable)(struct android_usb_function *);
/* Optional: called when it is removed */
void (*disable)(struct android_usb_function *);
int (*bind_config)(struct android_usb_function *,
struct usb_configuration *);
/* Optional: called when the configuration is removed */
void (*unbind_config)(struct android_usb_function *,
struct usb_configuration *);
/* Optional: handle ctrl requests before the device is configured */
int (*ctrlrequest)(struct android_usb_function *,
struct usb_composite_dev *,
const struct usb_ctrlrequest *);
};
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct acm_dev_paramter {
int instances;
};
struct uac1_dev_paramter {
int instances;
};
struct rndis_dev_paramter {
u32 vendorID;
char manufacturer[256];
bool wceis;
};
struct gser_dev_paramter {
int instances;
};
#endif
struct android_dev {
struct android_usb_function **functions;
struct list_head enabled_functions;
struct usb_composite_dev *cdev;
struct device *dev;
void (*setup_complete)(struct usb_ep *ep,
struct usb_request *req);
bool enabled;
int disable_depth;
struct mutex mutex;
bool connected;
bool sw_connected;
struct work_struct work;
char ffs_aliases[256];
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct acm_dev_paramter acm_paramter;
#ifdef CONFIG_ASR_UAC1
struct uac1_dev_paramter uac1_paramter;
#endif
struct rndis_dev_paramter rndis_paramter;
struct gser_dev_paramter gser_paramter;
#endif
#ifdef CONFIG_USB_REMOTE_WAKEUP
bool remote_wakeup_enable;
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
char ethaddr[32];
#endif
struct list_head evt_list;
#endif
};
enum {
USB_DEV_DISCONNECT,
USB_DEV_CONNECT,
USB_DEV_CONFIGURE,
NR_USB_DEV_EVENT,
};
struct android_dev_event {
struct list_head list;
u32 event_code;
};
static struct class *android_class;
static struct android_dev *_android_dev;
static int android_bind_config(struct usb_configuration *c);
static void android_unbind_config(struct usb_configuration *c);
/* string IDs are assigned dynamically */
#define STRING_MANUFACTURER_IDX 0
#define STRING_PRODUCT_IDX 1
#define STRING_SERIAL_IDX 2
static char manufacturer_string[256];
static char product_string[256];
static char serial_string[256];
/* String Table */
static struct usb_string strings_dev[] = {
[STRING_MANUFACTURER_IDX].s = manufacturer_string,
[STRING_PRODUCT_IDX].s = product_string,
[STRING_SERIAL_IDX].s = serial_string,
{ } /* end of list */
};
static struct usb_gadget_strings stringtab_dev = {
.language = 0x0409, /* en-us */
.strings = strings_dev,
};
static struct usb_gadget_strings *dev_strings[] = {
&stringtab_dev,
NULL,
};
static struct usb_device_descriptor device_desc = {
.bLength = sizeof(device_desc),
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = __constant_cpu_to_le16(0x0200),
.bDeviceClass = USB_CLASS_MISC /* 0xEF */,
.bDeviceSubClass = 2,
.bDeviceProtocol = 1,
.idVendor = __constant_cpu_to_le16(VENDOR_ID),
.idProduct = __constant_cpu_to_le16(PRODUCT_ID),
.bcdDevice = __constant_cpu_to_le16(0xffff),
.bNumConfigurations = 1,
};
static struct usb_configuration android_config_driver = {
.label = "android",
.unbind = android_unbind_config,
.bConfigurationValue = 1,
};
#ifdef CONFIG_USB_ETH_RNDIS_ECM
static bool is_rndis_enabled = false;
#endif
static struct android_dev_event *create_dev_evt(u32 evt_code)
{
struct android_dev_event *evt;
evt = kzalloc(sizeof(*evt), GFP_ATOMIC);
if (unlikely(!evt)) {
pr_err("%s: failed to alloc dev evt\n", __func__);
return NULL;
}
evt->event_code = evt_code;
return evt;
}
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
static void save_net_mac(char *hostaddr)
{
struct android_dev *dev = _android_dev;
sprintf(dev->ethaddr , "USB_MAC=%pM" , hostaddr);
}
#endif
static void _android_work(struct android_dev *dev, u32 evt_code)
{
struct usb_composite_dev *cdev = dev->cdev;
char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
char *connected[2] = { "USB_STATE=CONNECTED", NULL };
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
char *configured[3] = { "USB_STATE=CONFIGURED", dev->ethaddr , NULL };
#else
char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
#endif
char **uevent_envp = NULL;
if (!cdev) {
pr_err("comp dev is NULL\n");
return;
}
if (USB_DEV_CONFIGURE == evt_code) {
uevent_envp = configured;
dev->sw_connected = true;
} else if (USB_DEV_DISCONNECT == evt_code) {
uevent_envp = disconnected;
dev->sw_connected = false;
} else {
uevent_envp = connected;
dev->sw_connected = true;
}
if (uevent_envp) {
#ifndef CONFIG_USB_TELEPHONY
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp);
pr_info("%s:%d: sent uevent %s %d:%p\n",
__func__, evt_code, uevent_envp[0],
dev->connected, cdev->config);
if (USB_DEV_DISCONNECT == evt_code)
msleep(100);
#endif
} else {
pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
dev->connected, dev->sw_connected, cdev->config);
}
}
static void android_work(struct work_struct *data)
{
struct android_dev *dev = container_of(data, struct android_dev, work);
struct usb_composite_dev *cdev = dev->cdev;
unsigned long flags;
struct android_dev_event *evt;
if (!cdev)
return;
spin_lock_irqsave(&cdev->lock, flags);
while (!list_empty(&dev->evt_list)) {
evt = list_entry(dev->evt_list.next, struct android_dev_event, list);
list_del_init(&evt->list);
spin_unlock_irqrestore(&cdev->lock, flags);
_android_work(dev, evt->event_code);
kfree(evt);
spin_lock_irqsave(&cdev->lock, flags);
}
spin_unlock_irqrestore(&cdev->lock, flags);
}
static void android_enable(struct android_dev *dev)
{
struct usb_composite_dev *cdev = dev->cdev;
if (WARN_ON(!dev->disable_depth))
return;
pr_err("USB_Debug Enter android_enable:%d\n", dev->disable_depth);
if (--dev->disable_depth == 0) {
usb_add_config(cdev, &android_config_driver,
android_bind_config);
usb_gadget_connect(cdev->gadget);
}
pr_debug("USB_Debug Exit android_enable:%d\n", dev->disable_depth);
}
extern bool dwc3_hwsulog_is_on(void);
#ifdef CONFIG_DWC3_HWSULOG
static void android_disconnect(struct usb_composite_dev *cdev);
#endif
static void android_disable(struct android_dev *dev)
{
struct usb_composite_dev *cdev = dev->cdev;
pr_err("USB_Debug Enter android_disable:%d\n", dev->disable_depth);
if (dev->disable_depth++ == 0) {
#ifdef CONFIG_DWC3_HWSULOG
if (dwc3_hwsulog_is_on()) {
android_disconnect(cdev);
mutex_unlock(&dev->mutex);
msleep(2000);
pr_err("hwsulog stop done\n");
mutex_lock(&dev->mutex);
pr_err("usb stop\n");
}
#endif
usb_gadget_disconnect(cdev->gadget);
/* Cancel pending control requests */
usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
usb_remove_config(cdev, &android_config_driver);
}
pr_debug("USB_Debug Exit android_disable:%d\n", dev->disable_depth);
}
/*-------------------------------------------------------------------------*/
/* Supported functions initialization */
struct adb_data {
bool opened;
bool enabled;
};
static int
adb_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return adb_setup();
}
static void adb_function_cleanup(struct android_usb_function *f)
{
adb_cleanup();
kfree(f->config);
}
static int
adb_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter adb_function_bind_config.\n");
ret = adb_bind_config(c);
pr_debug("USB_Debug Exit adb_function_bind_config.\n");
return ret;
}
static void adb_android_function_enable(struct android_usb_function *f)
{
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
struct android_dev *dev = _android_dev;
#endif
struct adb_data *data = f->config;
pr_debug("USB_Debug Enter adb_android_function_enable.\n");
data->enabled = true;
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
/* Disable the gadget until adbd is ready */
if (!data->opened)
android_disable(dev);
#endif
pr_debug("USB_Debug Exit adb_android_function_enable.\n");
}
static void adb_android_function_disable(struct android_usb_function *f)
{
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
struct android_dev *dev = _android_dev;
#endif
struct adb_data *data = f->config;
data->enabled = false;
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
/* Balance the disable that was called in closed_callback */
if (!data->opened)
android_enable(dev);
#endif
}
static struct android_usb_function adb_function = {
.name = "adb",
.enable = adb_android_function_enable,
.disable = adb_android_function_disable,
.init = adb_function_init,
.cleanup = adb_function_cleanup,
.bind_config = adb_function_bind_config,
};
static void adb_ready_callback(void)
{
struct android_dev *dev = _android_dev;
struct adb_data *data = adb_function.config;
mutex_lock(&dev->mutex);
data->opened = true;
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
if (data->enabled)
android_enable(dev);
#endif
mutex_unlock(&dev->mutex);
}
static void adb_closed_callback(void)
{
struct android_dev *dev = _android_dev;
struct adb_data *data = adb_function.config;
mutex_lock(&dev->mutex);
data->opened = false;
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
if (data->enabled)
android_disable(dev);
#endif
mutex_unlock(&dev->mutex);
}
#ifdef CONFIG_USB_GADGET_CHARGE_ONLY
static int charge_only_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
return 0;
}
static struct android_usb_function charge_only_function = {
.name = "charge_only",
.bind_config = charge_only_function_bind_config,
};
static int charge_only_connected;
static int charge_only_configured;
static int charge_only_disconnected;
void charge_only_send_uevent(int event)
{
struct android_dev *dev = _android_dev;
char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
char *connected[2] = { "USB_STATE=CONNECTED", NULL };
char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
if (!dev)
return;
switch (event) {
case 1:
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, connected);
charge_only_connected = 1;
charge_only_configured = 0;
charge_only_disconnected = 0;
break;
case 2:
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, configured);
charge_only_connected = 1;
charge_only_configured = 1;
charge_only_disconnected = 0;
break;
case 3:
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, disconnected);
charge_only_connected = 0;
charge_only_configured = 0;
charge_only_disconnected = 1;
break;
default:
charge_only_connected = 0;
charge_only_configured = 0;
charge_only_disconnected = 0;
break;
}
}
static int charge_only_mode;
int is_charge_only_mode(void)
{
return charge_only_mode;
}
#endif /* CONFIG_USB_GADGET_CHARGE_ONLY */
#ifndef CONFIG_USB_TELEPHONY
/* Marvell modem function initialization */
static int marvell_modem_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
return pxa910_modem_gserial_setup(cdev->gadget, 1);
}
static void marvell_modem_function_cleanup(struct android_usb_function *f)
{
pxa910_modem_gserial_cleanup();
}
int marvell_modem_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter marvell_modem_function_bind_config.\n");
ret = pxa910_acm_bind_config(c, 0);
pr_debug("USB_Debug Exit marvell_modem_function_bind_config.\n");
return ret;
}
static struct android_usb_function marvell_modem_function = {
.name = "marvell_modem",
.init = marvell_modem_function_init,
.cleanup = marvell_modem_function_cleanup,
.bind_config = marvell_modem_function_bind_config,
};
/* Marvell diag function initialization */
static int marvell_diag_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
return pxa910_diag_gserial_setup(cdev->gadget, 1);
}
static void marvell_diag_function_cleanup(struct android_usb_function *f)
{
pxa910_diag_gserial_cleanup();
}
int marvell_diag_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter marvell_diag_function_bind_config.\n");
ret = pxa910_diag_bind_config(c, 1);
pr_debug("USB_Debug Exit marvell_diag_function_bind_config:%d.\n", ret);
return ret;
}
static struct android_usb_function marvell_diag_function = {
.name = "marvell_diag",
.init = marvell_diag_function_init,
.cleanup = marvell_diag_function_cleanup,
.bind_config = marvell_diag_function_bind_config,
};
#endif
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
struct functionfs_config {
bool opened;
bool enabled;
struct ffs_data *data;
};
static int ffs_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct functionfs_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return functionfs_init();
}
static void ffs_function_cleanup(struct android_usb_function *f)
{
functionfs_cleanup();
kfree(f->config);
}
static void ffs_function_enable(struct android_usb_function *f)
{
struct android_dev *dev = _android_dev;
struct functionfs_config *config = f->config;
pr_debug("USB_Debug Enter ffs_function_enable.\n");
config->enabled = true;
/* Disable the gadget until the function is ready */
if (!config->opened)
android_disable(dev);
pr_debug("USB_Debug Exit ffs_function_enable.\n");
}
static void ffs_function_disable(struct android_usb_function *f)
{
struct android_dev *dev = _android_dev;
struct functionfs_config *config = f->config;
config->enabled = false;
/* Balance the disable that was called in closed_callback */
if (!config->opened)
android_enable(dev);
}
static int ffs_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct functionfs_config *config = f->config;
int ret;
pr_debug("USB_Debug Enter ffs_function_bind_config.\n");
ret = functionfs_bind_config(c->cdev, c, config->data);
pr_debug("USB_Debug Exit ffs_function_bind_config:%d.\n", ret);
return ret;
}
static ssize_t
ffs_aliases_show(struct device *pdev, struct device_attribute *attr, char *buf)
{
struct android_dev *dev = _android_dev;
int ret;
mutex_lock(&dev->mutex);
ret = sprintf(buf, "%s\n", dev->ffs_aliases);
mutex_unlock(&dev->mutex);
return ret;
}
static ssize_t
ffs_aliases_store(struct device *pdev, struct device_attribute *attr,
const char *buf, size_t size)
{
struct android_dev *dev = _android_dev;
char buff[256];
mutex_lock(&dev->mutex);
if (dev->enabled) {
mutex_unlock(&dev->mutex);
return -EBUSY;
}
strlcpy(buff, buf, sizeof(buff));
strlcpy(dev->ffs_aliases, strim(buff), sizeof(dev->ffs_aliases));
mutex_unlock(&dev->mutex);
return size;
}
static DEVICE_ATTR(aliases, S_IRUGO | S_IWUSR, ffs_aliases_show,
ffs_aliases_store);
static struct device_attribute *ffs_function_attributes[] = {
&dev_attr_aliases,
NULL
};
static struct android_usb_function ffs_function = {
.name = "ffs",
.init = ffs_function_init,
.enable = ffs_function_enable,
.disable = ffs_function_disable,
.cleanup = ffs_function_cleanup,
.bind_config = ffs_function_bind_config,
.attributes = ffs_function_attributes,
};
static int functionfs_ready_callback(struct ffs_data *ffs)
{
struct android_dev *dev = _android_dev;
struct functionfs_config *config = ffs_function.config;
int ret = 0;
mutex_lock(&dev->mutex);
ret = functionfs_bind(ffs, dev->cdev);
if (ret)
goto err;
config->data = ffs;
config->opened = true;
if (config->enabled)
android_enable(dev);
err:
mutex_unlock(&dev->mutex);
return ret;
}
static void functionfs_closed_callback(struct ffs_data *ffs)
{
struct android_dev *dev = _android_dev;
struct functionfs_config *config = ffs_function.config;
mutex_lock(&dev->mutex);
if (config->enabled)
android_disable(dev);
config->opened = false;
config->data = NULL;
functionfs_unbind(ffs);
mutex_unlock(&dev->mutex);
}
static void *functionfs_acquire_dev_callback(const char *dev_name)
{
return 0;
}
static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
{
}
#endif
#ifdef CONFIG_USB_MV_HSIC_UDC
#define MAX_ACM_INSTANCES 2
#else
#define MAX_ACM_INSTANCES 2
#endif
struct acm_function_config {
int instances;
int instances_on;
struct usb_function *f_acm[MAX_ACM_INSTANCES];
struct usb_function_instance *f_acm_inst[MAX_ACM_INSTANCES];
};
static int
acm_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
int i;
int ret;
struct acm_function_config *config;
config = kzalloc(sizeof(struct acm_function_config), GFP_KERNEL);
if (!config)
return -ENOMEM;
f->config = config;
for (i = 0; i < MAX_ACM_INSTANCES; i++) {
config->f_acm_inst[i] = usb_get_function_instance("acm");
if (IS_ERR(config->f_acm_inst[i])) {
ret = PTR_ERR(config->f_acm_inst[i]);
goto err_usb_get_function_instance;
}
config->f_acm[i] = usb_get_function(config->f_acm_inst[i]);
if (IS_ERR(config->f_acm[i])) {
ret = PTR_ERR(config->f_acm[i]);
goto err_usb_get_function;
}
}
return 0;
err_usb_get_function_instance:
while (i-- > 0) {
usb_put_function(config->f_acm[i]);
err_usb_get_function:
usb_put_function_instance(config->f_acm_inst[i]);
}
return ret;
}
static void acm_function_cleanup(struct android_usb_function *f)
{
int i;
struct acm_function_config *config = f->config;
for (i = 0; i < MAX_ACM_INSTANCES; i++) {
usb_put_function(config->f_acm[i]);
usb_put_function_instance(config->f_acm_inst[i]);
}
kfree(f->config);
f->config = NULL;
}
static int
acm_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int i;
int ret = 0;
struct acm_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *dev = _android_dev;
if (dev->acm_paramter.instances)
config->instances = dev->acm_paramter.instances;
#endif
pr_debug("USB_Debug Enter acm_function_bind_config.\n");
config->instances_on = config->instances;
for (i = 0; i < config->instances_on; i++) {
ret = usb_add_function(c, config->f_acm[i]);
if (ret) {
pr_err("Could not bind acm%u config\n", i);
goto err_usb_add_function;
}
}
pr_debug("USB_Debug Exit acm_function_bind_config.\n");
return 0;
err_usb_add_function:
while (i-- > 0)
usb_remove_function(c, config->f_acm[i]);
pr_debug("USB_Debug Exit acm_function_bind_config:%d.\n", ret);
return ret;
}
static ssize_t acm_instances_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct acm_function_config *config = f->config;
return sprintf(buf, "%d\n", config->instances);
}
static ssize_t acm_instances_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct acm_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
int value;
sscanf(buf, "%d", &value);
if (value > MAX_ACM_INSTANCES)
value = MAX_ACM_INSTANCES;
config->instances = value;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
_dev->acm_paramter.instances = value;
#endif
return size;
}
static DEVICE_ATTR(instances, S_IRUGO | S_IWUSR, acm_instances_show,
acm_instances_store);
static struct device_attribute *acm_function_attributes[] = {
&dev_attr_instances,
NULL
};
static struct android_usb_function acm_function = {
.name = "acm",
.init = acm_function_init,
.cleanup = acm_function_cleanup,
.bind_config = acm_function_bind_config,
.attributes = acm_function_attributes,
};
#ifndef CONFIG_USB_TELEPHONY
/* Marvell debug function initialization */
static int marvell_debug_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
return pxa182x_debug_gserial_setup(cdev->gadget, 1);
}
static void marvell_debug_function_cleanup(struct android_usb_function *f)
{
pxa182x_debug_gserial_cleanup();
}
int marvell_debug_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter marvell_debug_function_bind_config\n");
ret = pxa182x_debug_bind_config(c, 1);
pr_debug("USB_Debug Exit marvell_debug_function_bind_config:%d\n", ret);
return ret;
}
static struct android_usb_function marvell_debug_function = {
.name = "marvell_debug",
.init = marvell_debug_function_init,
.cleanup = marvell_debug_function_cleanup,
.bind_config = marvell_debug_function_bind_config,
};
#endif
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
static int
mtp_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
return mtp_setup();
}
static void mtp_function_cleanup(struct android_usb_function *f)
{
mtp_cleanup();
}
static int
mtp_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter mtp_function_bind_config.\n");
ret = mtp_bind_config(c, false);
pr_debug("USB_Debug Exit mtp_function_bind_config:%d.\n", ret);
return ret;
}
static int
ptp_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
/* nothing to do - initialization is handled by mtp_function_init */
return 0;
}
static void ptp_function_cleanup(struct android_usb_function *f)
{
/* nothing to do - cleanup is handled by mtp_function_cleanup */
}
static int
ptp_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter ptp_function_bind_config.\n");
ret = mtp_bind_config(c, true);
pr_debug("USB_Debug Exit ptp_function_bind_config:%d.\n", ret);
return ret;
}
static int mtp_function_ctrlrequest(struct android_usb_function *f,
struct usb_composite_dev *cdev,
const struct usb_ctrlrequest *c)
{
return mtp_ctrlrequest(cdev, c);
}
static int ptp_function_ctrlrequest(struct android_usb_function *f,
struct usb_composite_dev *cdev,
const struct usb_ctrlrequest *c)
{
return ptp_ctrlrequest(cdev, c);
}
static struct android_usb_function mtp_function = {
.name = "mtp",
.init = mtp_function_init,
.cleanup = mtp_function_cleanup,
.bind_config = mtp_function_bind_config,
.ctrlrequest = mtp_function_ctrlrequest,
};
/* PTP function is same as MTP with slightly different interface descriptor */
static struct android_usb_function ptp_function = {
.name = "ptp",
.init = ptp_function_init,
.cleanup = ptp_function_cleanup,
.bind_config = ptp_function_bind_config,
.ctrlrequest = ptp_function_ctrlrequest,
};
#endif
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
struct ecm_function_config {
char ethaddr[ETH_ALEN];
struct eth_dev *dev;
};
static ssize_t ecm_ethaddr_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct ecm_function_config *ecm = f->config;
return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
}
static ssize_t ecm_ethaddr_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct ecm_function_config *ecm = f->config;
if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
(int *)&ecm->ethaddr[0], (int *)&ecm->ethaddr[1],
(int *)&ecm->ethaddr[2], (int *)&ecm->ethaddr[3],
(int *)&ecm->ethaddr[4], (int *)&ecm->ethaddr[5]) == 6)
return size;
return -EINVAL;
}
static DEVICE_ATTR(ecm_ethaddr, S_IRUGO|S_IWUSR, ecm_ethaddr_show,
ecm_ethaddr_store);
static struct device_attribute *ecm_function_attributes[] = {
&dev_attr_ecm_ethaddr,
NULL
};
static int
ecm_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct ecm_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return 0;
}
static void
ecm_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
f->config = NULL;
}
static int
ecm_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
struct eth_dev *dev;
struct ecm_function_config *ecm = f->config;
if (!ecm) {
pr_err("%s: ecm_pdata\n", __func__);
return -EINVAL;
}
pr_debug("USB_Debug Enter ecm_function_bind_config.\n");
pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
ecm->ethaddr[0], ecm->ethaddr[1], ecm->ethaddr[2],
ecm->ethaddr[3], ecm->ethaddr[4], ecm->ethaddr[5]);
#ifdef CONFIG_USB_ETH_RNDIS_ECM
pr_info("%s: is_rndis_eanabled: %d\n", __func__, is_rndis_enabled);
save_ecm_usbnet_host_ethaddr(ecm->ethaddr);
if (is_rndis_enabled)
dev = gether_setup_name_ecm(c->cdev->gadget, ecm->ethaddr, "usbnet");
else
dev = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usbnet");
#else
save_usbnet_host_ethaddr(ecm->ethaddr);
dev = gether_setup_name(c->cdev->gadget, ecm->ethaddr, "usbnet");
#endif
if (IS_ERR(dev)) {
ret = PTR_ERR(dev);
pr_err("%s: gether_setup failed\n", __func__);
return ret;
}
ecm->dev = dev;
ret = ecm_bind_config(c, ecm->ethaddr, dev);
if (ret) {
pr_err("%s:ecm_bind_config failed\n", __func__);
gether_cleanup(ecm->dev);
}
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
save_net_mac(ecm->ethaddr);
#endif
pr_debug("USB_Debug Exit ecm_function_bind_config:%d.\n", ret);
return ret;
}
static void ecm_function_unbind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct ecm_function_config *ecm = f->config;
gether_cleanup(ecm->dev);
}
static struct android_usb_function ecm_function = {
.name = "ecm",
.init = ecm_function_init,
.cleanup = ecm_function_cleanup,
.bind_config = ecm_function_bind_config,
.unbind_config = ecm_function_unbind_config,
.attributes = ecm_function_attributes,
};
#endif
#if defined(CONFIG_CPU_ASR18XX) && defined(CONFIG_USB_TELEPHONY)
struct usbtel_function_config {
/*
* TODO: ADD needed params here
*/
int data;
};
static int
usbtel_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct usbtel_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return 0;
}
static void
usbtel_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
f->config = NULL;
}
static int
usbtel_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
struct usbtel_function_config *usbtel = f->config;
if (!usbtel) {
pr_err("%s: usbtel_pdata\n", __func__);
return -EINVAL;
}
pr_info("USB_Debug Enter usbtel_function_bind_config.\n");
ret = usbtel_bind_config(c);
if (ret) {
pr_err("%s:usbtel_bind_config failed\n", __func__);
}
pr_debug("USB_Debug Exit usbtel_function_bind_config:%d.\n", ret);
return ret;
}
static void usbtel_function_unbind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
/* TODO */
return;
}
static struct android_usb_function usbtel_function = {
.name = "usbtel",
.init = usbtel_function_init,
.cleanup = usbtel_function_cleanup,
.bind_config = usbtel_function_bind_config,
.unbind_config = usbtel_function_unbind_config,
};
#endif
#if defined(CONFIG_CPU_ASR18XX) && defined(CONFIG_ASR_UAC1)
#define MAX_UAC1_INSTANCES 1
struct uac1_function_config {
int instances;
int instances_on;
struct usb_function *f_uac1[MAX_UAC1_INSTANCES];
struct usb_function_instance *f_uac1_inst[MAX_UAC1_INSTANCES];
};
static int
uac1_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
int i;
int ret;
struct uac1_function_config *config;
config = kzalloc(sizeof(struct uac1_function_config), GFP_KERNEL);
if (!config)
return -ENOMEM;
f->config = config;
for (i = 0; i < MAX_UAC1_INSTANCES; i++) {
config->f_uac1_inst[i] = usb_get_function_instance("uac1");
if (IS_ERR(config->f_uac1_inst[i])) {
ret = PTR_ERR(config->f_uac1_inst[i]);
goto err_usb_get_function_instance;
}
config->f_uac1[i] = usb_get_function(config->f_uac1_inst[i]);
if (IS_ERR(config->f_uac1[i])) {
ret = PTR_ERR(config->f_uac1[i]);
goto err_usb_get_function;
}
}
return 0;
err_usb_get_function_instance:
while (i-- > 0) {
usb_put_function(config->f_uac1[i]);
err_usb_get_function:
usb_put_function_instance(config->f_uac1_inst[i]);
}
return ret;
}
static void uac1_function_cleanup(struct android_usb_function *f)
{
int i;
struct uac1_function_config *config = f->config;
for (i = 0; i < MAX_UAC1_INSTANCES; i++) {
usb_put_function(config->f_uac1[i]);
usb_put_function_instance(config->f_uac1_inst[i]);
}
kfree(f->config);
f->config = NULL;
}
static int
uac1_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int i;
int ret = 0;
struct uac1_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *dev = _android_dev;
if (dev->uac1_paramter.instances)
config->instances = dev->uac1_paramter.instances;
config->instances = MAX_UAC1_INSTANCES; //dev->uac1_paramter.instances;
#endif
pr_debug("USB_Debug Enter uac1_function_bind_config.\n");
config->instances_on = config->instances;
for (i = 0; i < config->instances_on; i++) {
ret = usb_add_function(c, config->f_uac1[i]);
if (ret) {
pr_err("Could not bind uac1_%u config\n", i);
goto err_usb_add_function;
}
}
pr_debug("USB_Debug Exit uac1_function_bind_config.\n");
return 0;
err_usb_add_function:
while (i-- > 0)
usb_remove_function(c, config->f_uac1[i]);
pr_debug("USB_Debug Exit uac1_function_bind_config:%d.\n", ret);
return ret;
}
static ssize_t uac1_instances_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct uac1_function_config *config = f->config;
return sprintf(buf, "%d\n", config->instances);
}
static ssize_t uac1_instances_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct uac1_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
int value;
sscanf(buf, "%d", &value);
if (value > MAX_UAC1_INSTANCES)
value = MAX_UAC1_INSTANCES;
config->instances = value;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
_dev->uac1_paramter.instances = value;
#endif
return size;
}
static DEVICE_ATTR(uacinstances, S_IRUGO | S_IWUSR, uac1_instances_show,
uac1_instances_store);
static struct device_attribute *uac1_function_attributes[] = {
&dev_attr_uacinstances,
NULL
};
static struct android_usb_function uac1_function = {
.name = "uac1",
.init = uac1_function_init,
.cleanup = uac1_function_cleanup,
.bind_config = uac1_function_bind_config,
.attributes = uac1_function_attributes,
};
#endif
#if defined(CONFIG_USB_F_SERIAL)
#define MAX_GSER_INSTANCES 2
struct gser_function_config {
int instances;
int instances_on;
struct usb_function *f_gser[MAX_GSER_INSTANCES];
struct usb_function_instance *f_gser_inst[MAX_GSER_INSTANCES];
};
static int
gser_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
int i;
int ret;
struct gser_function_config *config;
config = kzalloc(sizeof(struct gser_function_config), GFP_KERNEL);
if (!config)
return -ENOMEM;
f->config = config;
for (i = 0; i < MAX_GSER_INSTANCES; i++) {
config->f_gser_inst[i] = usb_get_function_instance("gser");
if (IS_ERR(config->f_gser_inst[i])) {
ret = PTR_ERR(config->f_gser_inst[i]);
goto err_usb_get_function_instance;
}
config->f_gser[i] = usb_get_function(config->f_gser_inst[i]);
if (IS_ERR(config->f_gser[i])) {
ret = PTR_ERR(config->f_gser[i]);
goto err_usb_get_function;
}
}
return 0;
err_usb_get_function_instance:
while (i-- > 0) {
usb_put_function(config->f_gser[i]);
err_usb_get_function:
usb_put_function_instance(config->f_gser_inst[i]);
}
return ret;
}
static void gser_function_cleanup(struct android_usb_function *f)
{
int i;
struct gser_function_config *config = f->config;
for (i = 0; i < MAX_GSER_INSTANCES; i++) {
usb_put_function(config->f_gser[i]);
usb_put_function_instance(config->f_gser_inst[i]);
}
kfree(f->config);
f->config = NULL;
}
static int
gser_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int i;
int ret = 0;
struct gser_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *dev = _android_dev;
if (dev->gser_paramter.instances)
config->instances = dev->gser_paramter.instances;
#endif
pr_debug("USB_Debug Enter gser_function_bind_config.\n");
config->instances_on = config->instances;
for (i = 0; i < config->instances_on; i++) {
ret = usb_add_function(c, config->f_gser[i]);
if (ret) {
pr_err("Could not bind gser%u config\n", i);
goto err_usb_add_function;
}
}
pr_debug("USB_Debug Exit gser_function_bind_config.\n");
return 0;
err_usb_add_function:
while (i-- > 0)
usb_remove_function(c, config->f_gser[i]);
pr_debug("USB_Debug Exit gser_function_bind_config:%d.\n", ret);
return ret;
}
static ssize_t gser_instances_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct gser_function_config *config = f->config;
return sprintf(buf, "%d\n", config->instances);
}
static ssize_t gser_instances_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct gser_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
int value;
sscanf(buf, "%d", &value);
if (value > MAX_GSER_INSTANCES)
value = MAX_GSER_INSTANCES;
config->instances = value;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
_dev->gser_paramter.instances = value;
#endif
return size;
}
static DEVICE_ATTR(gser_instances, S_IRUGO | S_IWUSR, gser_instances_show,
gser_instances_store);
static struct device_attribute *gser_function_attributes[] = {
&dev_attr_gser_instances,
NULL
};
static struct android_usb_function gser_function = {
.name = "gser",
.init = gser_function_init,
.cleanup = gser_function_cleanup,
.bind_config = gser_function_bind_config,
.attributes = gser_function_attributes,
};
#endif /* end of CONFIG_USB_F_SERIAL */
struct rndis_function_config {
u8 ethaddr[ETH_ALEN];
u32 vendorID;
char manufacturer[256];
/* "Wireless" RNDIS; auto-detected by Windows */
bool wceis;
struct eth_dev *dev;
};
static int
rndis_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct rndis_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return 0;
}
static void rndis_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
f->config = NULL;
}
static int
rndis_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
struct eth_dev *dev;
struct rndis_function_config *rndis = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
if (!rndis) {
pr_err("%s: rndis_pdata\n", __func__);
return -1;
}
pr_debug("USB_Debug Enter rndis_function_bind_config.\n");
pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
save_usbnet_host_ethaddr(rndis->ethaddr);
if (rndis->ethaddr[0])
dev = gether_setup_name(c->cdev->gadget, NULL, "usbnet");
else
dev = gether_setup_name(c->cdev->gadget, rndis->ethaddr,
"usbnet");
#ifdef CONFIG_USB_ETH_RNDIS_ECM
is_rndis_enabled = true;
#endif
if (IS_ERR(dev)) {
ret = PTR_ERR(dev);
pr_err("%s: gether_setup failed\n", __func__);
return ret;
}
rndis->dev = dev;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
strlcpy(rndis->manufacturer, _dev->rndis_paramter.manufacturer,
sizeof(_dev->rndis_paramter.manufacturer));
rndis->wceis = _dev->rndis_paramter.wceis;
rndis->vendorID = _dev->rndis_paramter.vendorID;
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
save_net_mac(rndis->ethaddr);
#endif
#endif
if (rndis->wceis) {
/* "Wireless" RNDIS; auto-detected by Windows */
rndis_iad_descriptor.bFunctionClass =
USB_CLASS_WIRELESS_CONTROLLER;
rndis_iad_descriptor.bFunctionSubClass = 0x01;
rndis_iad_descriptor.bFunctionProtocol = 0x03;
rndis_control_intf.bInterfaceClass =
USB_CLASS_WIRELESS_CONTROLLER;
rndis_control_intf.bInterfaceSubClass = 0x01;
rndis_control_intf.bInterfaceProtocol = 0x03;
}
ret = rndis_bind_config_vendor(c, rndis->ethaddr, rndis->vendorID,
rndis->manufacturer, rndis->dev);
pr_debug("USB_Debug Exit rndis_function_bind_config:%d.\n", ret);
return ret;
}
static void rndis_function_unbind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct rndis_function_config *rndis = f->config;
gether_cleanup(rndis->dev);
#ifdef CONFIG_USB_ETH_RNDIS_ECM
is_rndis_enabled = false;
#endif
}
static ssize_t rndis_manufacturer_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *config = f->config;
return sprintf(buf, "%s\n", config->manufacturer);
}
static ssize_t rndis_manufacturer_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *config = f->config;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
if (size >= sizeof(config->manufacturer))
return -EINVAL;
if (sscanf(buf, "%s", config->manufacturer) == 1) {
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
memset(_dev->rndis_paramter.manufacturer, 0,
sizeof(_dev->rndis_paramter.manufacturer));
strlcpy(_dev->rndis_paramter.manufacturer,
config->manufacturer , size);
#endif
return size;
}
return -1;
}
static DEVICE_ATTR(manufacturer, S_IRUGO | S_IWUSR, rndis_manufacturer_show,
rndis_manufacturer_store);
static ssize_t rndis_wceis_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *config = f->config;
return sprintf(buf, "%d\n", config->wceis);
}
static ssize_t rndis_wceis_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *config = f->config;
int value;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
if (sscanf(buf, "%d", &value) == 1) {
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
_dev->rndis_paramter.wceis = value;
#endif
config->wceis = value;
return size;
}
return -EINVAL;
}
static DEVICE_ATTR(wceis, S_IRUGO | S_IWUSR, rndis_wceis_show,
rndis_wceis_store);
static ssize_t rndis_ethaddr_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *rndis = f->config;
return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
rndis->ethaddr[0], rndis->ethaddr[1], rndis->ethaddr[2],
rndis->ethaddr[3], rndis->ethaddr[4], rndis->ethaddr[5]);
}
static ssize_t rndis_ethaddr_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *rndis = f->config;
if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
(int *)&rndis->ethaddr[0], (int *)&rndis->ethaddr[1],
(int *)&rndis->ethaddr[2], (int *)&rndis->ethaddr[3],
(int *)&rndis->ethaddr[4], (int *)&rndis->ethaddr[5]) == 6)
return size;
return -EINVAL;
}
static DEVICE_ATTR(ethaddr, S_IRUGO | S_IWUSR, rndis_ethaddr_show,
rndis_ethaddr_store);
static ssize_t rndis_vendorID_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *config = f->config;
return sprintf(buf, "%04x\n", config->vendorID);
}
static ssize_t rndis_vendorID_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct rndis_function_config *config = f->config;
int value;
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
struct android_dev *_dev = _android_dev;
#endif
if (sscanf(buf, "%04x", &value) == 1) {
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
_dev->rndis_paramter.vendorID = value;
#endif
config->vendorID = value;
return size;
}
return -EINVAL;
}
static DEVICE_ATTR(vendorID, S_IRUGO | S_IWUSR, rndis_vendorID_show,
rndis_vendorID_store);
static struct device_attribute *rndis_function_attributes[] = {
&dev_attr_manufacturer,
&dev_attr_wceis,
&dev_attr_ethaddr,
&dev_attr_vendorID,
NULL
};
static struct android_usb_function rndis_function = {
.name = "rndis",
.init = rndis_function_init,
.cleanup = rndis_function_cleanup,
.bind_config = rndis_function_bind_config,
.unbind_config = rndis_function_unbind_config,
.attributes = rndis_function_attributes,
};
#if defined (CONFIG_USB_G_MBIM)
struct mbim_function_config {
u8 ethaddr[ETH_ALEN];
u32 vendorID;
char manufacturer[256];
struct eth_dev *dev;
enum ncm_mbim_mode mode;
};
static int
mbim_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return mbim_init(1);
}
static int
ncm_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return 0;
}
static int
ncm_mbim_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct mbim_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return 0;
}
static int
mbim_handle_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c);
static int
ncm_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct mbim_function_config *ncm = f->config;
int ret;
if (!ncm) {
pr_err("%s: ncm_pdata\n", __func__);
return -1;
}
pr_debug("USB_Debug Enter ncm_function_bind_config.\n");
ncm->mode = MODE_NCM;
ret = mbim_handle_function_bind_config(f, c);
pr_debug("USB_Debug Exit ncm_function_bind_config:%d.\n", ret);
return ret;
}
static int
ncm_mbim_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct mbim_function_config *ncm_mbim = f->config;
int ret;
if (!ncm_mbim) {
pr_err("%s: ncm_mbim_pdata\n", __func__);
return -1;
}
pr_debug("USB_Debug Enter ncm_mbim_function_bind_config.\n");
ncm_mbim->mode = MODE_NCM_MBIM;
ret = mbim_handle_function_bind_config(f, c);
pr_debug("USB_Debug Exit ncm_mbim_function_bind_config:%d.\n", ret);
return ret;
}
static void ncm_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
f->config = NULL;
}
static void ncm_mbim_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
f->config = NULL;
}
static void mbim_function_cleanup(struct android_usb_function *f)
{
fmbim_cleanup();
kfree(f->config);
f->config = NULL;
}
static int
mbim_handle_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
struct eth_dev *dev;
struct mbim_function_config *mbim = f->config;
if (!mbim) {
pr_err("%s: mbim_pdata\n", __func__);
return -1;
}
pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
mbim->ethaddr[0], mbim->ethaddr[1], mbim->ethaddr[2],
mbim->ethaddr[3], mbim->ethaddr[4], mbim->ethaddr[5]);
save_usbnet_host_ethaddr(mbim->ethaddr);
if (mbim->ethaddr[0])
dev = gether_setup_name(c->cdev->gadget, NULL, "usbnet");
else
dev = gether_setup_name(c->cdev->gadget, mbim->ethaddr,
"usbnet");
if (IS_ERR(dev)) {
ret = PTR_ERR(dev);
pr_err("%s: gether_setup failed\n", __func__);
return ret;
}
mbim->dev = dev;
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
save_net_mac(mbim->ethaddr);
#endif
return mbim_bind_config(c, mbim->ethaddr, dev, mbim->mode);
}
static int
mbim_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct mbim_function_config *mbim = f->config;
int ret;
if (!mbim) {
pr_err("%s: mbim_pdata\n", __func__);
return -1;
}
pr_debug("USB_Debug Enter mbim_function_bind_config.\n");
mbim->mode = MODE_MBIM;
ret = mbim_handle_function_bind_config(f, c);
pr_debug("USB_Debug Exit mbim_function_bind_config:%d.\n", ret);
return ret;
}
static void mbim_function_unbind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct mbim_function_config *mbim = f->config;
gether_cleanup(mbim->dev);
}
static ssize_t mbim_manufacturer_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct mbim_function_config *config = f->config;
return sprintf(buf, "%s\n", config->manufacturer);
}
static ssize_t mbim_manufacturer_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct mbim_function_config *config = f->config;
if (size >= sizeof(config->manufacturer))
return -EINVAL;
if (sscanf(buf, "%s", config->manufacturer) == 1)
return size;
return -1;
}
static DEVICE_ATTR(mbim_manufacturer, S_IRUGO | S_IWUSR, mbim_manufacturer_show,
mbim_manufacturer_store);
static ssize_t mbim_ethaddr_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct mbim_function_config *mbim = f->config;
return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
mbim->ethaddr[0], mbim->ethaddr[1], mbim->ethaddr[2],
mbim->ethaddr[3], mbim->ethaddr[4], mbim->ethaddr[5]);
}
static ssize_t mbim_ethaddr_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct mbim_function_config *mbim = f->config;
if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
(int *)&mbim->ethaddr[0], (int *)&mbim->ethaddr[1],
(int *)&mbim->ethaddr[2], (int *)&mbim->ethaddr[3],
(int *)&mbim->ethaddr[4], (int *)&mbim->ethaddr[5]) == 6)
return size;
return -EINVAL;
}
static DEVICE_ATTR(mbim_ethaddr, S_IRUGO | S_IWUSR, mbim_ethaddr_show,
mbim_ethaddr_store);
static ssize_t mbim_vendorID_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct mbim_function_config *config = f->config;
return sprintf(buf, "%04x\n", config->vendorID);
}
static ssize_t mbim_vendorID_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct mbim_function_config *config = f->config;
int value;
if (sscanf(buf, "%04x", &value) == 1) {
config->vendorID = value;
return size;
}
return -EINVAL;
}
static DEVICE_ATTR(mbim_vendorID, S_IRUGO | S_IWUSR, mbim_vendorID_show,
mbim_vendorID_store);
static struct device_attribute *mbim_function_attributes[] = {
&dev_attr_mbim_manufacturer,
&dev_attr_mbim_ethaddr,
&dev_attr_mbim_vendorID,
NULL
};
static struct android_usb_function mbim_function = {
.name = "mbim",
.init = mbim_function_init,
.cleanup = mbim_function_cleanup,
.bind_config = mbim_function_bind_config,
.unbind_config = mbim_function_unbind_config,
.attributes = mbim_function_attributes,
};
static struct android_usb_function ncm_function = {
.name = "ncm",
.init = ncm_function_init,
.cleanup = ncm_function_cleanup,
.bind_config = ncm_function_bind_config,
.unbind_config = mbim_function_unbind_config,
.attributes = NULL,
};
static struct android_usb_function ncm_mbim_function = {
.name = "ncm_mbim",
.init = ncm_mbim_function_init,
.cleanup = ncm_mbim_function_cleanup,
.bind_config = ncm_mbim_function_bind_config,
.unbind_config = mbim_function_unbind_config,
.attributes = NULL,
};
#endif
#define MASS_STORAGE_INSTANCES 1
struct mass_storage_function_config {
int instances_on;
struct usb_function *f_mass_storage[MASS_STORAGE_INSTANCES];
struct usb_function_instance *f_mass_storage_inst[MASS_STORAGE_INSTANCES];
};
static int
mass_storage_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
int i;
int ret;
struct mass_storage_function_config *config;
config = kzalloc(sizeof(struct mass_storage_function_config), GFP_KERNEL);
if (!config)
return -ENOMEM;
f->config = config;
for (i = 0; i < MASS_STORAGE_INSTANCES; i++) {
config->f_mass_storage_inst[i] = usb_get_function_instance("mass_storage");
if (IS_ERR(config->f_mass_storage_inst[i])) {
ret = PTR_ERR(config->f_mass_storage_inst[i]);
goto err_usb_get_function_instance;
}
config->f_mass_storage[i] = usb_get_function(config->f_mass_storage_inst[i]);
if (IS_ERR(config->f_mass_storage[i])) {
ret = PTR_ERR(config->f_mass_storage[i]);
goto err_usb_get_function;
}
}
return 0;
err_usb_get_function_instance:
while (i-- > 0) {
usb_put_function(config->f_mass_storage[i]);
err_usb_get_function:
usb_put_function_instance(config->f_mass_storage_inst[i]);
}
return ret;
}
static void mass_storage_function_cleanup(struct android_usb_function *f)
{
int i;
struct mass_storage_function_config *config = f->config;
for (i = 0; i < MASS_STORAGE_INSTANCES; i++) {
usb_put_function(config->f_mass_storage[i]);
usb_put_function_instance(config->f_mass_storage_inst[i]);
}
kfree(f->config);
f->config = NULL;
}
static int
mass_storage_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int i;
int ret = 0;
struct mass_storage_function_config *config = f->config;
pr_info("USB_Debug Enter mass_storage_function_bind_config.\n");
config->instances_on = MASS_STORAGE_INSTANCES;
for (i = 0; i < config->instances_on; i++) {
ret = usb_add_function(c, config->f_mass_storage[i]);
if (ret) {
pr_err("Could not bind mass_storage%u config\n", i);
goto err_usb_add_function;
}
}
pr_info("USB_Debug Exit mass_storage_function_bind_config.\n");
return 0;
err_usb_add_function:
while (i-- > 0)
usb_remove_function(c, config->f_mass_storage[i]);
pr_debug("USB_Debug Exit mass_storage_function_bind_config:%d.\n", ret);
return ret;
}
static struct android_usb_function mass_storage_function = {
.name = "mass_storage",
.init = mass_storage_function_init,
.cleanup = mass_storage_function_cleanup,
.bind_config = mass_storage_function_bind_config,
};
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
static int accessory_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
return acc_setup();
}
static void accessory_function_cleanup(struct android_usb_function *f)
{
acc_cleanup();
}
static int accessory_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int ret;
pr_debug("USB_Debug Enter accessory_function_bind_config.\n");
ret = acc_bind_config(c);
pr_debug("USB_Debug Exit accessory_function_bind_config:%d.\n", ret);
return ret;
}
static int accessory_function_ctrlrequest(struct android_usb_function *f,
struct usb_composite_dev *cdev,
const struct usb_ctrlrequest *c)
{
return acc_ctrlrequest(cdev, c);
}
static struct android_usb_function accessory_function = {
.name = "accessory",
.init = accessory_function_init,
.cleanup = accessory_function_cleanup,
.bind_config = accessory_function_bind_config,
.ctrlrequest = accessory_function_ctrlrequest,
};
static int audio_source_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
struct audio_source_config *config;
config = kzalloc(sizeof(struct audio_source_config), GFP_KERNEL);
if (!config)
return -ENOMEM;
config->card = -1;
config->device = -1;
f->config = config;
return 0;
}
static void audio_source_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
}
static int audio_source_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct audio_source_config *config = f->config;
int ret;
pr_debug("USB_Debug Enter audio_source_function_bind_config.\n");
ret = audio_source_bind_config(c, config);
pr_debug("USB_Debug Exit audio_source_function_bind_config:%d\n", ret);
return ret;
}
static void audio_source_function_unbind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct audio_source_config *config = f->config;
config->card = -1;
config->device = -1;
}
static ssize_t audio_source_pcm_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct audio_source_config *config = f->config;
/* print PCM card and device numbers */
return sprintf(buf, "%d %d\n", config->card, config->device);
}
static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL);
static struct device_attribute *audio_source_function_attributes[] = {
&dev_attr_pcm,
NULL
};
static struct android_usb_function audio_source_function = {
.name = "audio_source",
.init = audio_source_function_init,
.cleanup = audio_source_function_cleanup,
.bind_config = audio_source_function_bind_config,
.unbind_config = audio_source_function_unbind_config,
.attributes = audio_source_function_attributes,
};
#define CD_ROM_PATH0 "/system/pcsuite/pcsuite.iso"
struct pcsuite_function_config {
struct fsg_config fsg;
struct fsg_common *common;
int nluns;
};
static int pcsuite_fsg_init(struct usb_composite_dev *cdev,
struct android_usb_function *f,
struct pcsuite_function_config *config)
{
struct fsg_common *common;
int nluns = config->nluns;
char *name;
int i, ret;
config->fsg.nluns = nluns;
for (i = 0; i < nluns; i++) {
config->fsg.luns[i].removable = 1;
config->fsg.luns[i].cdrom = 1;
}
common = fsg_common_init(NULL, cdev, &config->fsg);
if (IS_ERR(common))
return PTR_ERR(common);
/* Create symlinks */
for (i = 0; i < nluns; i++) {
name = get_lun_name(i, nluns);
ret = sysfs_create_link(&f->dev->kobj,
&common->luns[i].dev.kobj, name);
if (ret) {
while (i-- > 0) {
name = get_lun_name(i, nluns);
sysfs_remove_link(&f->dev->kobj, name);
}
goto err_create_symlink;
}
}
config->common = common;
return 0;
err_create_symlink:
fsg_common_release(&common->ref);
return ret;
}
static void pcsuite_fsg_release(struct android_usb_function *f)
{
struct pcsuite_function_config *config = f->config;
struct fsg_common *common = config->common;
int nluns = config->fsg.nluns;
char *name;
int i;
if (nluns < 1)
return;
/* Remove symlinks */
i = nluns - 1;
while (i >= 0) {
name = get_lun_name(i, nluns);
sysfs_remove_link(&f->dev->kobj, name);
i--;
}
fsg_common_release(&common->ref);
}
static int pcsuite_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
struct pcsuite_function_config *config;
config = kzalloc(sizeof(struct pcsuite_function_config), GFP_KERNEL);
if (!config)
return -ENOMEM;
/* Init nluns=1 by default */
config->nluns = 1;
config->fsg.luns[0].filename = CD_ROM_PATH0;
f->config = config;
return 0;
}
static void pcsuite_function_cleanup(struct android_usb_function *f)
{
pcsuite_fsg_release(f);
kfree(f->config);
f->config = NULL;
}
static int pcsuite_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct pcsuite_function_config *config = f->config;
int ret;
static int pcsuite_inited = 0;
printk("%s, filename is %s\n", __func__, config->fsg.luns[0].filename);
if (pcsuite_inited == 0) {
ret = pcsuite_fsg_init(c->cdev, f, config);
if (ret)
return ret;
pcsuite_inited = 1;
}
return fsg_bind_config(c->cdev, c, config->common);
}
static struct android_usb_function pcsuite_function = {
.name = "pcsuite",
.init = pcsuite_function_init,
.cleanup = pcsuite_function_cleanup,
.bind_config = pcsuite_function_bind_config,
};
static int midi_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
struct midi_alsa_config *config;
config = kzalloc(sizeof(struct midi_alsa_config), GFP_KERNEL);
f->config = config;
if (!config)
return -ENOMEM;
config->card = -1;
config->device = -1;
return 0;
}
static void midi_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
}
tatic int midi_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct midi_alsa_config *config = f->config;
return f_midi_bind_config(c, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
MIDI_INPUT_PORTS, MIDI_OUTPUT_PORTS, MIDI_BUFFER_SIZE,
MIDI_QUEUE_LENGTH, config);
}
static ssize_t midi_alsa_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct midi_alsa_config *config = f->config;
/* print ALSA card and device numbers */
return sprintf(buf, "%d %d\n", config->card, config->device);
}
static DEVICE_ATTR(alsa, S_IRUGO, midi_alsa_show, NULL);
static struct device_attribute *midi_function_attributes[] = {
&dev_attr_alsa,
NULL
};
static struct android_usb_function midi_function = {
.name = "midi",
.init = midi_function_init,
.cleanup = midi_function_cleanup,
.bind_config = midi_function_bind_config,
.attributes = midi_function_attributes,
};
#endif /* ifndef CONFIG_CPU_ASR18XX */
static struct android_usb_function *supported_functions[] = {
&adb_function,
#ifndef CONFIG_USB_TELEPHONY
&marvell_modem_function,
&marvell_diag_function,
#endif
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
&ffs_function,
#endif
&acm_function,
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
&mtp_function,
&ptp_function,
#endif
&rndis_function,
#ifndef CONFIG_USB_TELEPHONY
&marvell_debug_function,
#endif
#if defined (CONFIG_USB_G_MBIM)
&mbim_function,
&ncm_function,
&ncm_mbim_function,
#endif
&mass_storage_function,
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
&accessory_function,
&pcsuite_function,
&audio_source_function,
&midi_function,
#endif
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
&ecm_function,
#if defined(CONFIG_USB_TELEPHONY)
&usbtel_function,
#endif
#if defined(CONFIG_ASR_UAC1)
&uac1_function,
#endif
#ifdef CONFIG_USB_F_SERIAL
&gser_function,
#endif
#endif
#ifdef CONFIG_USB_GADGET_CHARGE_ONLY
&charge_only_function,
#endif /* CONFIG_USB_GADGET_CHARGE_ONLY */
NULL
};
static int android_init_functions(struct android_usb_function **functions,
struct usb_composite_dev *cdev)
{
struct android_dev *dev = _android_dev;
struct android_usb_function *f;
struct device_attribute **attrs;
struct device_attribute *attr;
int err;
// This index should be 1, not 0
int index = 1;
for (; (f = *functions++); index++) {
f->dev_name = kasprintf(GFP_KERNEL, "f_%s", f->name);
f->dev = device_create(android_class, dev->dev,
MKDEV(0, index), f, f->dev_name);
if (IS_ERR(f->dev)) {
pr_err("%s: Failed to create dev %s", __func__,
f->dev_name);
err = PTR_ERR(f->dev);
goto err_create;
}
if (f->init) {
err = f->init(f, cdev);
if (err) {
pr_err("%s: Failed to init %s", __func__,
f->name);
goto err_out;
}
}
attrs = f->attributes;
if (attrs) {
while ((attr = *attrs++) && !err)
err = device_create_file(f->dev, attr);
}
if (err) {
pr_err("%s: Failed to create function %s attributes",
__func__, f->name);
goto err_out;
}
}
return 0;
err_out:
device_destroy(android_class, f->dev->devt);
err_create:
kfree(f->dev_name);
return err;
}
static void android_cleanup_functions(struct android_usb_function **functions)
{
struct android_usb_function *f;
while (*functions) {
f = *functions++;
/* NOTE: must clean up first, then device_destroy */
if (f->cleanup)
f->cleanup(f);
if (f->dev) {
device_destroy(android_class, f->dev->devt);
kfree(f->dev_name);
}
}
}
static int
android_bind_enabled_functions(struct android_dev *dev,
struct usb_configuration *c)
{
struct android_usb_function *f;
int ret;
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
ret = f->bind_config(f, c);
if (ret) {
pr_err("%s: %s failed", __func__, f->name);
return ret;
}
}
return 0;
}
static void
android_unbind_enabled_functions(struct android_dev *dev,
struct usb_configuration *c)
{
struct android_usb_function *f;
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (f->unbind_config)
f->unbind_config(f, c);
}
}
static int android_enable_function(struct android_dev *dev, char *name)
{
struct android_usb_function **functions = dev->functions;
struct android_usb_function *f;
while ((f = *functions++)) {
if (!strcmp(name, f->name)) {
#if defined (CONFIG_USB_G_MBIM)
if (f == &mbim_function)
mbim_enabled = 1;
#endif
list_add_tail(&f->enabled_list,
&dev->enabled_functions);
return 0;
}
}
return -EINVAL;
}
/*-------------------------------------------------------------------------*/
/* /sys/class/android_usb/android%d/ interface */
static ssize_t
functions_show(struct device *pdev, struct device_attribute *attr, char *buf)
{
struct android_dev *dev = dev_get_drvdata(pdev);
struct android_usb_function *f;
char *buff = buf;
mutex_lock(&dev->mutex);
list_for_each_entry(f, &dev->enabled_functions, enabled_list)
buff += sprintf(buff, "%s,", f->name);
mutex_unlock(&dev->mutex);
if (buff != buf)
*(buff-1) = '\n';
return buff - buf;
}
static ssize_t
functions_store(struct device *pdev, struct device_attribute *attr,
const char *buff, size_t size)
{
struct android_dev *dev = dev_get_drvdata(pdev);
char *name;
char buf[256], *b;
char aliases[256], *a;
int err;
int is_ffs;
int ffs_enabled = 0;
mutex_lock(&dev->mutex);
if (dev->enabled) {
mutex_unlock(&dev->mutex);
return -EBUSY;
}
INIT_LIST_HEAD(&dev->enabled_functions);
strlcpy(buf, buff, sizeof(buf));
b = strim(buf);
pr_info("enabled functions: %s\n", b);
#if defined (CONFIG_USB_G_MBIM)
mbim_enabled = 0;
#endif
while (b) {
name = strsep(&b, ",");
if (!name)
continue;
is_ffs = 0;
strlcpy(aliases, dev->ffs_aliases, sizeof(aliases));
a = aliases;
while (a) {
char *alias = strsep(&a, ",");
if (alias && !strcmp(name, alias)) {
is_ffs = 1;
break;
}
}
if (is_ffs) {
if (ffs_enabled)
continue;
err = android_enable_function(dev, "ffs");
if (err)
pr_err("android_usb: Cannot enable ffs (%d)",
err);
else
ffs_enabled = 1;
continue;
}
err = android_enable_function(dev, name);
if (err)
pr_err("android_usb: Cannot enable '%s' (%d)",
name, err);
}
mutex_unlock(&dev->mutex);
return size;
}
static ssize_t enable_show(struct device *pdev, struct device_attribute *attr,
char *buf)
{
struct android_dev *dev = dev_get_drvdata(pdev);
return sprintf(buf, "%d\n", dev->enabled);
}
static ssize_t enable_store(struct device *pdev, struct device_attribute *attr,
const char *buff, size_t size)
{
struct android_dev *dev = dev_get_drvdata(pdev);
struct usb_composite_dev *cdev = dev->cdev;
struct android_usb_function *f;
int enabled = 0;
if (!cdev)
return -ENODEV;
mutex_lock(&dev->mutex);
sscanf(buff, "%d", &enabled);
#ifdef CONFIG_USB_GADGET_CHARGE_ONLY
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (strcmp(f->name, "charge_only") == 0) {
charge_only_mode = 1;
if (dev->enabled)
usb_gadget_disconnect(cdev->gadget);
if (enabled) {
dev->enabled = true;
usb_gadget_vbus_connect(cdev->gadget);
} else {
dev->enabled = false;
usb_gadget_vbus_disconnect(cdev->gadget);
}
pr_info("android_usb: charge_only mode %s\n",
enabled ? "enabled" : "disabled");
mutex_unlock(&dev->mutex);
return size;
}
}
charge_only_mode = 0;
#endif /* CONFIG_USB_GADGET_CHARGE_ONLY */
if (enabled && !dev->enabled) {
/*
* Update values in composite driver's copy of
* device descriptor.
*/
cdev->desc.idVendor = device_desc.idVendor;
cdev->desc.idProduct = device_desc.idProduct;
cdev->desc.bcdDevice = device_desc.bcdDevice;
cdev->desc.bDeviceClass = device_desc.bDeviceClass;
cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (f->enable)
f->enable(f);
}
android_enable(dev);
dev->enabled = true;
} else if (!enabled && dev->enabled) {
android_disable(dev);
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (f->disable)
f->disable(f);
}
dev->enabled = false;
} else {
pr_err("android_usb: already %s\n",
dev->enabled ? "enabled" : "disabled");
}
mutex_unlock(&dev->mutex);
return size;
}
static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
char *buf)
{
struct android_dev *dev = dev_get_drvdata(pdev);
struct usb_composite_dev *cdev = dev->cdev;
char *state = "DISCONNECTED";
unsigned long flags;
if (!cdev)
goto out;
spin_lock_irqsave(&cdev->lock, flags);
#ifdef CONFIG_USB_GADGET_CHARGE_ONLY
if (!charge_only_mode) {
if (cdev->config)
state = "CONFIGURED";
else if (dev->connected)
state = "CONNECTED";
} else {
if (charge_only_configured)
state = "CONFIGURED";
else if (charge_only_connected)
state = "CONNECTED";
}
#else
if (cdev->config)
state = "CONFIGURED";
else if (dev->connected)
state = "CONNECTED";
#endif
spin_unlock_irqrestore(&cdev->lock, flags);
out:
return sprintf(buf, "%s\n", state);
}
#define DESCRIPTOR_ATTR(field, format_string) \
static ssize_t \
field ## _show(struct device *dev, struct device_attribute *attr, \
char *buf) \
{ \
return sprintf(buf, format_string, device_desc.field); \
} \
static ssize_t \
field ## _store(struct device *dev, struct device_attribute *attr, \
const char *buf, size_t size) \
{ \
int value; \
if (sscanf(buf, format_string, &value) == 1) { \
device_desc.field = value; \
return size; \
} \
return -1; \
} \
static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
#define DESCRIPTOR_STRING_ATTR(field, buffer) \
static ssize_t \
field ## _show(struct device *dev, struct device_attribute *attr, \
char *buf) \
{ \
return sprintf(buf, "%s", buffer); \
} \
static ssize_t \
field ## _store(struct device *dev, struct device_attribute *attr, \
const char *buf, size_t size) \
{ \
if (size >= sizeof(buffer)) \
return -EINVAL; \
memset(buffer, 0, sizeof(buffer));\
/* @size is equal to the length of @buf
* NOTE: the string in @buf contains \n at last
* And we must remove the \n, otherwise Win8 will
* treat the string as illegal and will cause
* enumeration fail.
*/\
strncpy(buffer, buf, size - 1);\
return size; \
} \
static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
DESCRIPTOR_ATTR(idVendor, "%04x\n")
DESCRIPTOR_ATTR(idProduct, "%04x\n")
DESCRIPTOR_ATTR(bcdDevice, "%04x\n")
DESCRIPTOR_ATTR(bDeviceClass, "%d\n")
DESCRIPTOR_ATTR(bDeviceSubClass, "%d\n")
DESCRIPTOR_ATTR(bDeviceProtocol, "%d\n")
DESCRIPTOR_STRING_ATTR(iManufacturer, manufacturer_string)
DESCRIPTOR_STRING_ATTR(iProduct, product_string)
DESCRIPTOR_STRING_ATTR(iSerial, serial_string)
static DEVICE_ATTR(functions, S_IRUGO | S_IWUSR, functions_show,
functions_store);
static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
#ifdef CONFIG_USB_REMOTE_WAKEUP
static struct usb_otg_descriptor otg_descriptor = {
.bLength = sizeof(otg_descriptor),
.bDescriptorType = USB_DT_OTG,
/* REVISIT SRP-only hardware is possible, although
* it would not be called "OTG" ...
*/
.bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
};
static const struct usb_descriptor_header __maybe_unused *otg_desc[] = {
(struct usb_descriptor_header *) &otg_descriptor,
NULL,
};
static ssize_t
wakeup_enable_show(struct device *pdev,
struct device_attribute *attr, char *buf)
{
struct android_dev *dev = dev_get_drvdata(pdev);
return sprintf(buf , "%d\n" , dev->remote_wakeup_enable);
}
static ssize_t
wakeup_enable_store(struct device *pdev, struct device_attribute *attr,
const char *buff, size_t size)
{
struct android_dev *dev = dev_get_drvdata(pdev);
struct usb_composite_dev *cdev = dev->cdev;
int enabled;
mutex_lock(&dev->mutex);
sscanf(buff , "%d" , &enabled);
dev->remote_wakeup_enable = enabled;
if (enabled)
usb_gadget_wakeup(cdev->gadget);
mutex_unlock(&dev->mutex);
return size;
}
static DEVICE_ATTR(wakeup, S_IRUGO | S_IWUSR,
wakeup_enable_show, wakeup_enable_store);
#endif
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
static ssize_t hostEthaddr_show(struct device *pdev,
struct device_attribute *attr , char *buf)
{
struct android_dev *dev = dev_get_drvdata(pdev);
return sprintf(buf , "%s\n" , dev->ethaddr);
}
static DEVICE_ATTR(hostEthaddr , S_IRUGO, hostEthaddr_show , NULL);
#endif
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
static int force_usb_disconnect;
void enable_force_usb_disconnect(void)
{
force_usb_disconnect = 1;
}
void disable_force_usb_disconnect(void)
{
force_usb_disconnect = 0;
}
void force_send_usb_dis_event(struct android_dev *dev)
{
char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
if (force_usb_disconnect)
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, disconnected);
}
void android_dev_enable(uint8_t enabled)
{
struct android_dev *dev = _android_dev;
struct usb_composite_dev *cdev = dev->cdev;
struct android_usb_function *f;
if (!cdev) {
pr_info("cdev is null\n");
return;
}
/* send disconnect event to APP to close the file handle */
force_send_usb_dis_event(dev);
pr_err("USB_Debug Enter android_dev_enable:%d.\n", enabled);
mutex_lock(&dev->mutex);
if (enabled && !dev->enabled) {
/*
* Update values in composite driver's copy of
* device descriptor.
*/
cdev->desc.idVendor = device_desc.idVendor;
cdev->desc.idProduct = device_desc.idProduct;
cdev->desc.bcdDevice = device_desc.bcdDevice;
cdev->desc.bDeviceClass = device_desc.bDeviceClass;
cdev->desc.bDeviceSubClass = device_desc.bDeviceSubClass;
cdev->desc.bDeviceProtocol = device_desc.bDeviceProtocol;
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (f->enable)
f->enable(f);
}
android_enable(dev);
dev->enabled = true;
mutex_unlock(&dev->mutex);
} else if (!enabled && dev->enabled) {
/*
* check the netifd status before usb plug out instead of after
* plug in to avoid the corner case netifd is initiating itself
* while usb plug happens and confict with each other, here we
* we can wait for netifd init done and exec the plug routine
*/
#ifndef CONFIG_USB_TELEPHONY
wait_usbnet_br_up_and_brigded();
#endif
android_disable(dev);
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (f->disable)
f->disable(f);
}
dev->enabled = false;
mutex_unlock(&dev->mutex);
/*
* wait until usbnet IFF_DOWN is set when usb plugout routine
* finishes, together with wait_usbnet_br_up_and_brigded we
* garrantee following sequence,
* usb_plug_in->usbnet_up->usb_plug_out->usbnet_down->....
*/
#ifndef CONFIG_USB_TELEPHONY
wait_usbnet_if_down();
#endif
} else {
pr_err("android_usb: already %s\n",
dev->enabled ? "enabled" : "disabled");
mutex_unlock(&dev->mutex);
}
pr_err("USB_Debug Exit android_dev_enable:%d.\n", enabled);
}
#endif
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
#include "os_detect.c"
#endif
static struct device_attribute *android_usb_attributes[] = {
&dev_attr_idVendor,
&dev_attr_idProduct,
&dev_attr_bcdDevice,
&dev_attr_bDeviceClass,
&dev_attr_bDeviceSubClass,
&dev_attr_bDeviceProtocol,
&dev_attr_iManufacturer,
&dev_attr_iProduct,
&dev_attr_iSerial,
&dev_attr_functions,
&dev_attr_enable,
&dev_attr_state,
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
&dev_attr_win7,
&dev_attr_win8,
&dev_attr_apple,
&dev_attr_olinux,
&dev_attr_os,
&dev_attr_win7_s2,
&dev_attr_win8_s2,
&dev_attr_apple_s2,
&dev_attr_olinux_s2,
#endif
#ifdef CONFIG_USB_REMOTE_WAKEUP
&dev_attr_wakeup,
#endif
#if defined(CONFIG_CPU_ASR18XX) || defined(CONFIG_CPU_ASR1901)
&dev_attr_hostEthaddr,
#endif
NULL
};
/*-------------------------------------------------------------------------*/
/* Composite driver */
static int android_bind_config(struct usb_configuration *c)
{
struct android_dev *dev = _android_dev;
int ret = 0;
#ifdef CONFIG_USB_REMOTE_WAKEUP
c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
#endif
ret = android_bind_enabled_functions(dev, c);
if (ret)
return ret;
return 0;
}
static void android_unbind_config(struct usb_configuration *c)
{
struct android_dev *dev = _android_dev;
android_unbind_enabled_functions(dev, c);
}
static int android_bind(struct usb_composite_dev *cdev)
{
struct android_dev *dev = _android_dev;
struct usb_gadget *gadget = cdev->gadget;
int id, ret;
/* Save the default handler */
dev->setup_complete = cdev->req->complete;
/*
* Start disconnected. Userspace will connect the gadget once
* it is done configuring the functions.
*/
usb_gadget_disconnect(gadget);
ret = android_init_functions(dev->functions, cdev);
if (ret)
return ret;
/* Allocate string descriptor numbers ... note that string
* contents can be overridden by the composite_dev glue.
*/
id = usb_string_id(cdev);
if (id < 0)
return id;
strings_dev[STRING_MANUFACTURER_IDX].id = id;
device_desc.iManufacturer = id;
id = usb_string_id(cdev);
if (id < 0)
return id;
strings_dev[STRING_PRODUCT_IDX].id = id;
device_desc.iProduct = id;
/* Default strings - should be updated by userspace */
#if 0
strncpy(manufacturer_string, "Android", sizeof(manufacturer_string)-1);
strncpy(product_string, "Android", sizeof(product_string) - 1);
strncpy(serial_string, "0123456789ABCDEF", sizeof(serial_string) - 1);
#endif
id = usb_string_id(cdev);
if (id < 0)
return id;
strings_dev[STRING_SERIAL_IDX].id = id;
device_desc.iSerialNumber = id;
dev->cdev = cdev;
return 0;
}
static int android_usb_unbind(struct usb_composite_dev *cdev)
{
struct android_dev *dev = _android_dev;
cancel_work_sync(&dev->work);
dev->cdev = NULL;
android_cleanup_functions(dev->functions);
return 0;
}
/* HACK: android needs to override setup for accessory to work */
static int (*composite_setup_func)(struct usb_gadget *gadget, const struct usb_ctrlrequest *c);
static int
android_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *c)
{
struct android_dev *dev = _android_dev;
struct usb_composite_dev *cdev = get_gadget_data(gadget);
struct usb_request *req = cdev->req;
struct android_usb_function *f;
int value = -EOPNOTSUPP;
unsigned long flags;
struct android_dev_event *evt;
req->zero = 0;
req->length = 0;
req->complete = dev->setup_complete;
gadget->ep0->driver_data = cdev;
list_for_each_entry(f, &dev->enabled_functions, enabled_list) {
if (f->ctrlrequest) {
value = f->ctrlrequest(f, cdev, c);
if (value >= 0)
break;
}
}
/* Special case the accessory function.
* It needs to handle control requests before it is enabled.
*/
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
if (value < 0)
value = acc_ctrlrequest(cdev, c);
#endif
if (value < 0)
value = composite_setup_func(gadget, c);
spin_lock_irqsave(&cdev->lock, flags);
if (!dev->connected) {
dev->connected = true;
evt = create_dev_evt(USB_DEV_CONNECT);
/*spin lock is held */
if (likely(evt))
list_add_tail(&evt->list, &dev->evt_list);
schedule_work(&dev->work);
} else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
c->wValue && cdev->config) {
evt = create_dev_evt(USB_DEV_CONFIGURE);
/* spin lock is held */
if (likely(evt))
list_add_tail(&evt->list, &dev->evt_list);
schedule_work(&dev->work);
} else if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
(!c->wValue)) {
evt = create_dev_evt(USB_DEV_DISCONNECT);
/* spin lock is held */
if (likely(evt))
list_add_tail(&evt->list, &dev->evt_list);
schedule_work(&dev->work);
}
spin_unlock_irqrestore(&cdev->lock, flags);
return value;
}
static void android_disconnect(struct usb_composite_dev *cdev)
{
struct android_dev *dev = _android_dev;
struct android_dev_event *evt;
/* accessory HID support can be active while the
accessory function is not actually enabled,
so we need to inform it when we are disconnected.
*/
#if !defined(CONFIG_CPU_ASR18XX) && !defined(CONFIG_CPU_ASR1901)
acc_disconnect();
#endif
if (!in_interrupt()) {
pr_info("+preempt disable\n");
preempt_disable();
}
evt = create_dev_evt(USB_DEV_DISCONNECT);
/* spin lock is held */
if (likely(evt))
list_add_tail(&evt->list, &dev->evt_list);
if (!in_interrupt()) {
pr_info("-preempt enable\n");
preempt_enable();
}
dev->connected = false;
schedule_work(&dev->work);
}
static struct usb_composite_driver android_usb_driver = {
#ifdef CONFIG_USB_TELEPHONY
.name = "android_usbtel",
#else
.name = "android_usb",
#endif
.dev = &device_desc,
.strings = dev_strings,
.bind = android_bind,
.unbind = android_usb_unbind,
.disconnect = android_disconnect,
#if defined(CONFIG_CPU_ASR1901)
.max_speed = USB_SPEED_SUPER_PLUS,
#elif defined(CONFIG_CPU_ASR18XX)
.max_speed = USB_SPEED_SUPER,
#else
.max_speed = USB_SPEED_HIGH,
#endif
};
static int android_create_device(struct android_dev *dev)
{
struct device_attribute **attrs = android_usb_attributes;
struct device_attribute *attr;
int err;
dev->dev = device_create(android_class, NULL,
MKDEV(0, 0), NULL, "android0");
if (IS_ERR(dev->dev))
return PTR_ERR(dev->dev);
dev_set_drvdata(dev->dev, dev);
while ((attr = *attrs++)) {
err = device_create_file(dev->dev, attr);
if (err) {
device_destroy(android_class, dev->dev->devt);
return err;
}
}
return 0;
}
static int __init init(void)
{
struct android_dev *dev;
int err;
#ifdef CONFIG_CPU_ASR18XX
if (cpu_is_asr1803())
android_usb_driver.max_speed = USB_SPEED_HIGH;
#endif
#ifdef CONFIG_USB_TELEPHONY
android_class = class_create(THIS_MODULE, "android_usbtel");
#else
android_class = class_create(THIS_MODULE, "android_usb");
#endif
if (IS_ERR(android_class))
return PTR_ERR(android_class);
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev) {
err = -ENOMEM;
goto err_dev;
}
dev->disable_depth = 1;
dev->functions = supported_functions;
INIT_LIST_HEAD(&dev->enabled_functions);
INIT_WORK(&dev->work, android_work);
mutex_init(&dev->mutex);
err = android_create_device(dev);
if (err) {
pr_err("%s: failed to create android device %d", __func__, err);
goto err_create;
}
_android_dev = dev;
INIT_LIST_HEAD(&_android_dev->evt_list);
#ifdef CONFIG_USB_ANDROID_DETECT_HOST_OS
usb_os_detect_init();
#endif
err = usb_composite_probe(&android_usb_driver);
if (err) {
pr_err("%s: failed to probe driver %d", __func__, err);
goto err_probe;
}
/* HACK: exchange composite's setup with ours */
composite_setup_func = android_usb_driver.gadget_driver.setup;
android_usb_driver.gadget_driver.setup = android_setup;
return 0;
err_probe:
device_destroy(android_class, dev->dev->devt);
err_create:
kfree(dev);
err_dev:
class_destroy(android_class);
return err;
}
late_initcall(init);
static void __exit cleanup(void)
{
usb_composite_unregister(&android_usb_driver);
class_destroy(android_class);
kfree(_android_dev);
_android_dev = NULL;
}
module_exit(cleanup);