android_usb: Composite USB gadget driver for android.

Change-Id: I4101540916b73a5f4e48684727ff782f98b969c7
Signed-off-by: Mike Lockwood <lockwood@android.com>

USB: android gadget: add remote wakeup attribute to android function

Add remote wakeup attribute to configuration descriptor of android
function to advertise remote wakeup capability to host

Acked-by: Allam, Suresh Reddy <sallam@qualcomm.com>

Signed-off-by: Mike Lockwood <lockwood@android.com>

usb gadget: link fixes for android composite gadget

Signed-off-by: Mike Lockwood <lockwood@android.com>

usb gadget: Fix null pointer errors in android composite driver

Signed-off-by: Mike Lockwood <lockwood@android.com>

usb: gadget: android: Allow usb charging to draw up to 500mA instead of 250.

Signed-off-by: Rebecca Schultz Zavin <rebecca@android.com>

usb gadget: android: Add helper function for usb_gadget_connect and disconnect.

Signed-off-by: Mike Lockwood <lockwood@android.com>

drivers: usb: gadget: call switch_dev_unregister in mass storage unbind callback

This fixes a problem unloading the android gadget driver when built as a module

Signed-off-by: Mike Lockwood <lockwood@android.com>

usb: gadget: android: Add dependency on switch driver.

Signed-off-by: Mike Lockwood <lockwood@android.com>

USB: gadget: android: Fix USB WHQL Certification Issues

Submitted on behalf of RaviKumar Vembu <ravi.v@motorola.com>
Signed-off-by: Jared Suttles <jared.suttles@motorola.com>
Signed-off-by: Mike Lockwood <lockwood@android.com>

drivers: usb: gadget: Add "usb_mass_storage" platform driver.

This will be used for configuring vendor, product and release from board file.

Submitted on behalf of RaviKumar Vembu <ravi.v@motorola.com>
Signed-off-by: Jared Suttles <jared.suttles@motorola.com>
Signed-off-by: Mike Lockwood <lockwood@android.com>

drivers: usb: gadget: Use usb_mass_storage platform device as parent for lun

If a platform device is specified for the f_mass_storage function, use it as the
parent driver for the lun files in sysfs.
This allows a platform independent file path for controlling USB mass storage
from user space.

Signed-off-by: Mike Lockwood <lockwood@android.com>

drivers: usb: gadget: Add platform data struct for usb_mass_storage device

Signed-off-by: Mike Lockwood <lockwood@android.com>

usb: gadget: mass_storage: Fix Mass Storage Panic during PC reboot

Submitted on behalf of RaviKumar Vembu <ravi.v@motorola.com>
Signed-off-by: Jared Suttles <jared.suttles@motorola.com>
Signed-off-by: Mike Lockwood <lockwood@android.com>

usb: gadget: f_mass_storage: Handle setup request correctly

Signed-off-by: Mike Lockwood <lockwood@android.com>

usb: gadget: f_mass_storage: Clean up wakelocks on error paths

Signed-off-by: Rebecca Schultz Zavin <rebecca@android.com>
Signed-off-by: Mike Lockwood <lockwood@android.com>
This commit is contained in:
Mike Lockwood
2008-12-02 22:01:33 -05:00
committed by Colin Cross
parent 8f5ed00afe
commit 7f0d7bd310
8 changed files with 4132 additions and 0 deletions

View File

@@ -935,6 +935,15 @@ config USB_G_PRINTER
For more information, see Documentation/usb/gadget_printer.txt
which includes sample code for accessing the device file.
config USB_ANDROID
tristate "Android Gadget"
depends on SWITCH
help
The Android gadget provides mass storage and adb transport.
Say "y" to link the driver statically, or "m" to build a
dynamically linked module called "g_android".
config USB_CDC_COMPOSITE
tristate "CDC Composite Device (Ethernet and ACM)"
depends on NET

View File

@@ -49,6 +49,7 @@ g_dbgp-y := dbgp.o
g_nokia-y := nokia.o
g_webcam-y := webcam.o
g_ncm-y := ncm.o
g_android-y := android.o f_adb.o f_mass_storage_tmp.o
obj-$(CONFIG_USB_ZERO) += g_zero.o
obj-$(CONFIG_USB_AUDIO) += g_audio.o
@@ -67,3 +68,4 @@ obj-$(CONFIG_USB_G_MULTI) += g_multi.o
obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
obj-$(CONFIG_USB_G_NCM) += g_ncm.o
obj-$(CONFIG_USB_ANDROID) += g_android.o

View File

@@ -0,0 +1,351 @@
/*
* Gadget Driver for Android
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@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.
*
*/
/* #define DEBUG */
/* #define VERBOSE_DEBUG */
#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/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/usb/android.h>
#include <linux/usb/ch9.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include "f_mass_storage.h"
#include "f_adb.h"
#include "gadget_chips.h"
/*
* Kbuild is not very cooperative with respect to linking separately
* compiled library objects into one module. So for now we won't use
* separate compilation ... ensuring init/exit sections work to shrink
* the runtime footprint, and giving us at least some parts of what
* a "gcc --combine ... part1.c part2.c part3.c ... " build would.
*/
#include "usbstring.c"
#include "config.c"
#include "epautoconf.c"
#include "composite.c"
MODULE_AUTHOR("Mike Lockwood");
MODULE_DESCRIPTION("Android Composite USB Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");
static const char longname[] = "Gadget Android";
/* Default vendor and product IDs, overridden by platform data */
#define VENDOR_ID 0x18D1
#define PRODUCT_ID 0x0001
#define ADB_PRODUCT_ID 0x0002
struct android_dev {
struct usb_composite_dev *cdev;
int product_id;
int adb_product_id;
int version;
int adb_enabled;
int nluns;
};
static atomic_t adb_enable_excl;
static struct android_dev *_android_dev;
/* string IDs are assigned dynamically */
#define STRING_MANUFACTURER_IDX 0
#define STRING_PRODUCT_IDX 1
#define STRING_SERIAL_IDX 2
/* String Table */
static struct usb_string strings_dev[] = {
/* These dummy values should be overridden by platform data */
[STRING_MANUFACTURER_IDX].s = "Android",
[STRING_PRODUCT_IDX].s = "Android",
[STRING_SERIAL_IDX].s = "0123456789ABCDEF",
{ } /* 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_PER_INTERFACE,
.idVendor = __constant_cpu_to_le16(VENDOR_ID),
.idProduct = __constant_cpu_to_le16(PRODUCT_ID),
.bcdDevice = __constant_cpu_to_le16(0xffff),
.bNumConfigurations = 1,
};
void android_usb_set_connected(int connected)
{
if (_android_dev && _android_dev->cdev && _android_dev->cdev->gadget) {
if (connected)
usb_gadget_connect(_android_dev->cdev->gadget);
else
usb_gadget_disconnect(_android_dev->cdev->gadget);
}
}
static int __init android_bind_config(struct usb_configuration *c)
{
struct android_dev *dev = _android_dev;
int ret;
printk(KERN_DEBUG "android_bind_config\n");
ret = mass_storage_function_add(dev->cdev, c, dev->nluns);
if (ret)
return ret;
return adb_function_add(dev->cdev, c);
}
static struct usb_configuration android_config_driver = {
.label = "android",
.bind = android_bind_config,
.bConfigurationValue = 1,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 0xFA, /* 500ma */
};
static int __init android_bind(struct usb_composite_dev *cdev)
{
struct android_dev *dev = _android_dev;
struct usb_gadget *gadget = cdev->gadget;
int gcnum;
int id;
int ret;
printk(KERN_INFO "android_bind\n");
/* 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;
id = usb_string_id(cdev);
if (id < 0)
return id;
strings_dev[STRING_SERIAL_IDX].id = id;
device_desc.iSerialNumber = id;
/* register our configuration */
ret = usb_add_config(cdev, &android_config_driver);
if (ret) {
printk(KERN_ERR "usb_add_config failed\n");
return ret;
}
gcnum = usb_gadget_controller_number(gadget);
if (gcnum >= 0)
device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
else {
/* gadget zero is so simple (for now, no altsettings) that
* it SHOULD NOT have problems with bulk-capable hardware.
* so just warn about unrcognized controllers -- don't panic.
*
* things like configuration and altsetting numbering
* can need hardware-specific attention though.
*/
pr_warning("%s: controller '%s' not recognized\n",
longname, gadget->name);
device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
}
usb_gadget_set_selfpowered(gadget);
dev->cdev = cdev;
return 0;
}
static struct usb_composite_driver android_usb_driver = {
.name = "android_usb",
.dev = &device_desc,
.strings = dev_strings,
.bind = android_bind,
};
static void enable_adb(struct android_dev *dev, int enable)
{
if (enable != dev->adb_enabled) {
dev->adb_enabled = enable;
adb_function_enable(enable);
/* set product ID to the appropriate value */
if (enable)
device_desc.idProduct =
__constant_cpu_to_le16(dev->adb_product_id);
else
device_desc.idProduct =
__constant_cpu_to_le16(dev->product_id);
if (dev->cdev)
dev->cdev->desc.idProduct = device_desc.idProduct;
/* force reenumeration */
if (dev->cdev && dev->cdev->gadget &&
dev->cdev->gadget->speed != USB_SPEED_UNKNOWN) {
usb_gadget_disconnect(dev->cdev->gadget);
msleep(10);
usb_gadget_connect(dev->cdev->gadget);
}
}
}
static int adb_enable_open(struct inode *ip, struct file *fp)
{
if (atomic_inc_return(&adb_enable_excl) != 1) {
atomic_dec(&adb_enable_excl);
return -EBUSY;
}
printk(KERN_INFO "enabling adb\n");
enable_adb(_android_dev, 1);
return 0;
}
static int adb_enable_release(struct inode *ip, struct file *fp)
{
printk(KERN_INFO "disabling adb\n");
enable_adb(_android_dev, 0);
atomic_dec(&adb_enable_excl);
return 0;
}
static const struct file_operations adb_enable_fops = {
.owner = THIS_MODULE,
.open = adb_enable_open,
.release = adb_enable_release,
};
static struct miscdevice adb_enable_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "android_adb_enable",
.fops = &adb_enable_fops,
};
static int __init android_probe(struct platform_device *pdev)
{
struct android_usb_platform_data *pdata = pdev->dev.platform_data;
struct android_dev *dev = _android_dev;
printk(KERN_INFO "android_probe pdata: %p\n", pdata);
if (pdata) {
if (pdata->vendor_id)
device_desc.idVendor =
__constant_cpu_to_le16(pdata->vendor_id);
if (pdata->product_id) {
dev->product_id = pdata->product_id;
device_desc.idProduct =
__constant_cpu_to_le16(pdata->product_id);
}
if (pdata->adb_product_id)
dev->adb_product_id = pdata->adb_product_id;
if (pdata->version)
dev->version = pdata->version;
if (pdata->product_name)
strings_dev[STRING_PRODUCT_IDX].s = pdata->product_name;
if (pdata->manufacturer_name)
strings_dev[STRING_MANUFACTURER_IDX].s =
pdata->manufacturer_name;
if (pdata->serial_number)
strings_dev[STRING_SERIAL_IDX].s = pdata->serial_number;
dev->nluns = pdata->nluns;
}
return 0;
}
static struct platform_driver android_platform_driver = {
.driver = { .name = "android_usb", },
.probe = android_probe,
};
static int __init init(void)
{
struct android_dev *dev;
int ret;
printk(KERN_INFO "android init\n");
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
/* set default values, which should be overridden by platform data */
dev->product_id = PRODUCT_ID;
dev->adb_product_id = ADB_PRODUCT_ID;
_android_dev = dev;
ret = platform_driver_register(&android_platform_driver);
if (ret)
return ret;
ret = misc_register(&adb_enable_device);
if (ret) {
platform_driver_unregister(&android_platform_driver);
return ret;
}
ret = usb_composite_register(&android_usb_driver);
if (ret) {
misc_deregister(&adb_enable_device);
platform_driver_unregister(&android_platform_driver);
}
return ret;
}
module_init(init);
static void __exit cleanup(void)
{
usb_composite_unregister(&android_usb_driver);
misc_deregister(&adb_enable_device);
platform_driver_unregister(&android_platform_driver);
kfree(_android_dev);
_android_dev = NULL;
}
module_exit(cleanup);

668
drivers/usb/gadget/f_adb.c Normal file
View File

@@ -0,0 +1,668 @@
/*
* Gadget Driver for Android ADB
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@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.
*
*/
/* #define DEBUG */
/* #define VERBOSE_DEBUG */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/usb/ch9.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include "f_adb.h"
#define BULK_BUFFER_SIZE 4096
/* number of rx and tx requests to allocate */
#define RX_REQ_MAX 4
#define TX_REQ_MAX 4
static const char shortname[] = "android_adb";
struct adb_dev {
struct usb_function function;
struct usb_composite_dev *cdev;
spinlock_t lock;
struct usb_ep *ep_in;
struct usb_ep *ep_out;
int online;
int error;
atomic_t read_excl;
atomic_t write_excl;
atomic_t open_excl;
struct list_head tx_idle;
struct list_head rx_idle;
struct list_head rx_done;
wait_queue_head_t read_wq;
wait_queue_head_t write_wq;
/* the request we're currently reading from */
struct usb_request *read_req;
unsigned char *read_buf;
unsigned read_count;
};
static struct usb_interface_descriptor adb_interface_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0x42,
.bInterfaceProtocol = 1,
};
static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(512),
};
static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(512),
};
static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_descriptor_header *fs_adb_descs[] = {
(struct usb_descriptor_header *) &adb_interface_desc,
(struct usb_descriptor_header *) &adb_fullspeed_in_desc,
(struct usb_descriptor_header *) &adb_fullspeed_out_desc,
NULL,
};
static struct usb_descriptor_header *hs_adb_descs[] = {
(struct usb_descriptor_header *) &adb_interface_desc,
(struct usb_descriptor_header *) &adb_highspeed_in_desc,
(struct usb_descriptor_header *) &adb_highspeed_out_desc,
NULL,
};
/* used when adb function is disabled */
static struct usb_descriptor_header *null_adb_descs[] = {
NULL,
};
/* temporary variable used between adb_open() and adb_gadget_bind() */
static struct adb_dev *_adb_dev;
static inline struct adb_dev *func_to_dev(struct usb_function *f)
{
return container_of(f, struct adb_dev, function);
}
static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
{
struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
if (!req)
return NULL;
/* now allocate buffers for the requests */
req->buf = kmalloc(buffer_size, GFP_KERNEL);
if (!req->buf) {
usb_ep_free_request(ep, req);
return NULL;
}
return req;
}
static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
{
if (req) {
kfree(req->buf);
usb_ep_free_request(ep, req);
}
}
static inline int _lock(atomic_t *excl)
{
if (atomic_inc_return(excl) == 1) {
return 0;
} else {
atomic_dec(excl);
return -1;
}
}
static inline void _unlock(atomic_t *excl)
{
atomic_dec(excl);
}
/* add a request to the tail of a list */
void req_put(struct adb_dev *dev, struct list_head *head,
struct usb_request *req)
{
unsigned long flags;
spin_lock_irqsave(&dev->lock, flags);
list_add_tail(&req->list, head);
spin_unlock_irqrestore(&dev->lock, flags);
}
/* remove a request from the head of a list */
struct usb_request *req_get(struct adb_dev *dev, struct list_head *head)
{
unsigned long flags;
struct usb_request *req;
spin_lock_irqsave(&dev->lock, flags);
if (list_empty(head)) {
req = 0;
} else {
req = list_first_entry(head, struct usb_request, list);
list_del(&req->list);
}
spin_unlock_irqrestore(&dev->lock, flags);
return req;
}
static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
{
struct adb_dev *dev = _adb_dev;
if (req->status != 0)
dev->error = 1;
req_put(dev, &dev->tx_idle, req);
wake_up(&dev->write_wq);
}
static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
{
struct adb_dev *dev = _adb_dev;
if (req->status != 0) {
dev->error = 1;
req_put(dev, &dev->rx_idle, req);
} else {
req_put(dev, &dev->rx_done, req);
}
wake_up(&dev->read_wq);
}
static int __init create_bulk_endpoints(struct adb_dev *dev,
struct usb_endpoint_descriptor *in_desc,
struct usb_endpoint_descriptor *out_desc)
{
struct usb_composite_dev *cdev = dev->cdev;
struct usb_request *req;
struct usb_ep *ep;
int i;
DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
ep = usb_ep_autoconfig(cdev->gadget, in_desc);
if (!ep) {
DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
return -ENODEV;
}
DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
dev->ep_in = ep;
ep = usb_ep_autoconfig(cdev->gadget, out_desc);
if (!ep) {
DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
return -ENODEV;
}
DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
dev->ep_out = ep;
/* now allocate requests for our endpoints */
for (i = 0; i < RX_REQ_MAX; i++) {
req = adb_request_new(dev->ep_out, BULK_BUFFER_SIZE);
if (!req)
goto fail;
req->complete = adb_complete_out;
req_put(dev, &dev->rx_idle, req);
}
for (i = 0; i < TX_REQ_MAX; i++) {
req = adb_request_new(dev->ep_in, BULK_BUFFER_SIZE);
if (!req)
goto fail;
req->complete = adb_complete_in;
req_put(dev, &dev->tx_idle, req);
}
return 0;
fail:
printk(KERN_ERR "adb_bind() could not allocate requests\n");
return -1;
}
static ssize_t adb_read(struct file *fp, char __user *buf,
size_t count, loff_t *pos)
{
struct adb_dev *dev = fp->private_data;
struct usb_composite_dev *cdev = dev->cdev;
struct usb_request *req;
int r = count, xfer;
int ret;
DBG(cdev, "adb_read(%d)\n", count);
if (_lock(&dev->read_excl))
return -EBUSY;
/* we will block until we're online */
while (!(dev->online || dev->error)) {
DBG(cdev, "adb_read: waiting for online state\n");
ret = wait_event_interruptible(dev->read_wq,
(dev->online || dev->error));
if (ret < 0) {
_unlock(&dev->read_excl);
return ret;
}
}
while (count > 0) {
if (dev->error) {
DBG(cdev, "adb_read dev->error\n");
r = -EIO;
break;
}
/* if we have idle read requests, get them queued */
while ((req = req_get(dev, &dev->rx_idle))) {
requeue_req:
req->length = BULK_BUFFER_SIZE;
ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
if (ret < 0) {
r = -EIO;
dev->error = 1;
req_put(dev, &dev->rx_idle, req);
goto fail;
} else {
DBG(cdev, "rx %p queue\n", req);
}
}
/* if we have data pending, give it to userspace */
if (dev->read_count > 0) {
if (dev->read_count < count)
xfer = dev->read_count;
else
xfer = count;
if (copy_to_user(buf, dev->read_buf, xfer)) {
r = -EFAULT;
break;
}
dev->read_buf += xfer;
dev->read_count -= xfer;
buf += xfer;
count -= xfer;
/* if we've emptied the buffer, release the request */
if (dev->read_count == 0) {
req_put(dev, &dev->rx_idle, dev->read_req);
dev->read_req = 0;
}
continue;
}
/* wait for a request to complete */
req = 0;
ret = wait_event_interruptible(dev->read_wq,
((req = req_get(dev, &dev->rx_done)) || dev->error));
if (req != 0) {
/* if we got a 0-len one we need to put it back into
** service. if we made it the current read req we'd
** be stuck forever
*/
if (req->actual == 0)
goto requeue_req;
dev->read_req = req;
dev->read_count = req->actual;
dev->read_buf = req->buf;
DBG(cdev, "rx %p %d\n", req, req->actual);
}
if (ret < 0) {
r = ret;
break;
}
}
fail:
_unlock(&dev->read_excl);
DBG(cdev, "adb_read returning %d\n", r);
return r;
}
static ssize_t adb_write(struct file *fp, const char __user *buf,
size_t count, loff_t *pos)
{
struct adb_dev *dev = fp->private_data;
struct usb_composite_dev *cdev = dev->cdev;
struct usb_request *req = 0;
int r = count, xfer;
int ret;
DBG(cdev, "adb_write(%d)\n", count);
if (_lock(&dev->write_excl))
return -EBUSY;
while (count > 0) {
if (dev->error) {
DBG(cdev, "adb_write dev->error\n");
r = -EIO;
break;
}
/* get an idle tx request to use */
req = 0;
ret = wait_event_interruptible(dev->write_wq,
((req = req_get(dev, &dev->tx_idle)) || dev->error));
if (ret < 0) {
r = ret;
break;
}
if (req != 0) {
if (count > BULK_BUFFER_SIZE)
xfer = BULK_BUFFER_SIZE;
else
xfer = count;
if (copy_from_user(req->buf, buf, xfer)) {
r = -EFAULT;
break;
}
req->length = xfer;
ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
if (ret < 0) {
DBG(cdev, "adb_write: xfer error %d\n", ret);
dev->error = 1;
r = -EIO;
break;
}
buf += xfer;
count -= xfer;
/* zero this so we don't try to free it on error exit */
req = 0;
}
}
if (req)
req_put(dev, &dev->tx_idle, req);
_unlock(&dev->write_excl);
DBG(cdev, "adb_write returning %d\n", r);
return r;
}
static int adb_open(struct inode *ip, struct file *fp)
{
printk(KERN_INFO "adb_open\n");
if (_lock(&_adb_dev->open_excl))
return -EBUSY;
fp->private_data = _adb_dev;
/* clear the error latch */
_adb_dev->error = 0;
return 0;
}
static int adb_release(struct inode *ip, struct file *fp)
{
printk(KERN_INFO "adb_release\n");
_unlock(&_adb_dev->open_excl);
return 0;
}
/* file operations for ADB device /dev/android_adb */
static struct file_operations adb_fops = {
.owner = THIS_MODULE,
.read = adb_read,
.write = adb_write,
.open = adb_open,
.release = adb_release,
};
static struct miscdevice adb_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = shortname,
.fops = &adb_fops,
};
static int __init
adb_function_bind(struct usb_configuration *c, struct usb_function *f)
{
struct usb_composite_dev *cdev = c->cdev;
struct adb_dev *dev = func_to_dev(f);
int id;
int ret;
dev->cdev = cdev;
DBG(cdev, "adb_function_bind dev: %p\n", dev);
/* allocate interface ID(s) */
id = usb_interface_id(c, f);
if (id < 0)
return id;
adb_interface_desc.bInterfaceNumber = id;
/* allocate endpoints */
ret = create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
&adb_fullspeed_out_desc);
if (ret)
return ret;
/* support high speed hardware */
if (gadget_is_dualspeed(c->cdev->gadget)) {
adb_highspeed_in_desc.bEndpointAddress =
adb_fullspeed_in_desc.bEndpointAddress;
adb_highspeed_out_desc.bEndpointAddress =
adb_fullspeed_out_desc.bEndpointAddress;
}
DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
f->name, dev->ep_in->name, dev->ep_out->name);
return 0;
}
static void
adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
{
struct adb_dev *dev = func_to_dev(f);
struct usb_request *req;
spin_lock_irq(&dev->lock);
while ((req = req_get(dev, &dev->rx_idle)))
adb_request_free(req, dev->ep_out);
while ((req = req_get(dev, &dev->tx_idle)))
adb_request_free(req, dev->ep_in);
dev->online = 0;
dev->error = 1;
spin_unlock_irq(&dev->lock);
misc_deregister(&adb_device);
kfree(_adb_dev);
_adb_dev = NULL;
}
static int adb_function_set_alt(struct usb_function *f,
unsigned intf, unsigned alt)
{
struct adb_dev *dev = func_to_dev(f);
struct usb_composite_dev *cdev = f->config->cdev;
int ret;
DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
ret = usb_ep_enable(dev->ep_in,
ep_choose(cdev->gadget,
&adb_highspeed_in_desc,
&adb_fullspeed_in_desc));
if (ret)
return ret;
ret = usb_ep_enable(dev->ep_out,
ep_choose(cdev->gadget,
&adb_highspeed_out_desc,
&adb_fullspeed_out_desc));
if (ret) {
usb_ep_disable(dev->ep_in);
return ret;
}
dev->online = 1;
/* readers may be blocked waiting for us to go online */
wake_up(&dev->read_wq);
return 0;
}
static void adb_function_disable(struct usb_function *f)
{
struct adb_dev *dev = func_to_dev(f);
struct usb_composite_dev *cdev = dev->cdev;
DBG(cdev, "adb_function_disable\n");
dev->online = 0;
dev->error = 1;
usb_ep_disable(dev->ep_in);
usb_ep_disable(dev->ep_out);
/* readers may be blocked waiting for us to go online */
wake_up(&dev->read_wq);
VDBG(cdev, "%s disabled\n", dev->function.name);
}
int __init adb_function_add(struct usb_composite_dev *cdev,
struct usb_configuration *c)
{
struct adb_dev *dev;
int ret;
printk(KERN_INFO "adb_function_add\n");
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
spin_lock_init(&dev->lock);
init_waitqueue_head(&dev->read_wq);
init_waitqueue_head(&dev->write_wq);
atomic_set(&dev->open_excl, 0);
atomic_set(&dev->read_excl, 0);
atomic_set(&dev->write_excl, 0);
INIT_LIST_HEAD(&dev->rx_idle);
INIT_LIST_HEAD(&dev->rx_done);
INIT_LIST_HEAD(&dev->tx_idle);
dev->cdev = cdev;
dev->function.name = "adb";
dev->function.descriptors = null_adb_descs;
dev->function.hs_descriptors = null_adb_descs;
dev->function.bind = adb_function_bind;
dev->function.unbind = adb_function_unbind;
dev->function.set_alt = adb_function_set_alt;
dev->function.disable = adb_function_disable;
/* _adb_dev must be set before calling usb_gadget_register_driver */
_adb_dev = dev;
ret = misc_register(&adb_device);
if (ret)
goto err1;
ret = usb_add_function(c, &dev->function);
if (ret)
goto err2;
return 0;
err2:
misc_deregister(&adb_device);
err1:
kfree(dev);
printk(KERN_ERR "adb gadget driver failed to initialize\n");
return ret;
}
void adb_function_enable(int enable)
{
struct adb_dev *dev = _adb_dev;
if (dev) {
DBG(dev->cdev, "adb_function_enable(%s)\n",
enable ? "true" : "false");
if (enable) {
dev->function.descriptors = fs_adb_descs;
dev->function.hs_descriptors = hs_adb_descs;
} else {
dev->function.descriptors = null_adb_descs;
dev->function.hs_descriptors = null_adb_descs;
}
}
}

View File

@@ -0,0 +1,25 @@
/*
* Gadget Driver for Android ADB
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@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.
*
*/
#ifndef __F_ADB_H
#define __F_ADB_H
int adb_function_add(struct usb_composite_dev *cdev,
struct usb_configuration *c);
void adb_function_enable(int enable);
#endif /* __F_ADB_H */

View File

@@ -0,0 +1,52 @@
/*
* drivers/usb/gadget/f_mass_storage.h
*
* Function Driver for USB Mass Storage
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@android.com>
*
* Based heavily on the file_storage gadget driver in
* drivers/usb/gadget/file_storage.c and licensed under the same terms:
*
* Copyright (C) 2003-2007 Alan Stern
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the above-listed copyright holders may not be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* ALTERNATIVELY, this software may be distributed under the terms of the
* GNU General Public License ("GPL") as published by the Free Software
* Foundation, either version 2 of that License or (at your option) any
* later version.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __F_MASS_STORAGE_H
#define __F_MASS_STORAGE_H
int mass_storage_function_add(struct usb_composite_dev *cdev,
struct usb_configuration *c, int nluns);
#endif /* __F_MASS_STORAGE_H */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,50 @@
/*
* Platform data for Android USB
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@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.
*
*/
#ifndef __LINUX_USB_ANDROID_H
#define __LINUX_USB_ANDROID_H
struct android_usb_platform_data {
/* USB device descriptor fields */
__u16 vendor_id;
/* Default product ID. */
__u16 product_id;
/* Product ID when adb is enabled. */
__u16 adb_product_id;
__u16 version;
char *product_name;
char *manufacturer_name;
char *serial_number;
/* number of LUNS for mass storage function */
int nluns;
};
/* Platform data for "usb_mass_storage" driver.
* Contains values for the SC_INQUIRY SCSI command. */
struct usb_mass_storage_platform_data {
char *vendor;
char *product;
int release;
};
extern void android_usb_set_connected(int on);
#endif /* __LINUX_USB_ANDROID_H */