usb: Add support diag host driver
USB diag host driver will be used to connect diag interface
exposed by Qualcomm devices. This driver currently supports
usb qualcomm devices with product id: 9001 and vendor id:
0x5c6. Also add simple unit test driver to test diag host
driver.
Signed-off-by: Vamsi Krishna <vskrishn@codeaurora.org>
Signed-off-by: Jack Pham <jackp@codeaurora.org>
diff --git a/arch/arm/mach-msm/include/mach/diag_bridge.h b/arch/arm/mach-msm/include/mach/diag_bridge.h
new file mode 100644
index 0000000..92eb1b9
--- /dev/null
+++ b/arch/arm/mach-msm/include/mach/diag_bridge.h
@@ -0,0 +1,31 @@
+
+/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * 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_DIAG_BRIDGE_H__
+#define __LINUX_USB_DIAG_BRIDGE_H__
+
+struct diag_bridge_ops {
+ void *ctxt;
+ void (*read_complete_cb)(void *ctxt, char *buf,
+ size_t buf_size, size_t actual);
+ void (*write_complete_cb)(void *ctxt, char *buf,
+ size_t buf_size, size_t actual);
+};
+
+extern int diag_read(char *data, size_t size);
+extern int diag_write(char *data, size_t size);
+extern int diag_open(struct diag_bridge_ops *ops);
+extern void diag_close(void);
+
+#endif
diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index cf5d452..527dc85 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -259,3 +259,24 @@
To compile this driver as a module, choose M here: the
module will be called yurex.
+config USB_QCOM_DIAG
+ tristate "USB Qualcomm diag port driver"
+ depends on USB && !USB_SERIAL_QUALCOMM
+ default n
+ help
+ Say Y here if you want to use the diag ports provided
+ by many Qualcomm chipsets.
+
+ To compile this driver as a module, choose M here: the
+ module will be called qcaux. If unsure, choose N.
+
+config USB_QCOM_DIAG_TEST
+ tristate "USB Qualcomm diag port unit test driver"
+ depends on USB && USB_QCOM_DIAG
+ default n
+ help
+ Say Y here if you want to use unit test diag port provided
+ by many Qualcomm chipsets.
+
+ To compile this driver as a module, choose M here: the
+ module will be called qcaux. If unsure, choose N.
diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
index c8e777a..20ee62a 100644
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -28,3 +28,6 @@
obj-$(CONFIG_USB_YUREX) += yurex.o
obj-$(CONFIG_USB_SISUSBVGA) += sisusbvga/
+
+obj-$(CONFIG_USB_QCOM_DIAG) += diag_usb.o
+obj-$(CONFIG_USB_QCOM_DIAG_TEST) += diag_bridge_test.o
diff --git a/drivers/usb/misc/diag_bridge_test.c b/drivers/usb/misc/diag_bridge_test.c
new file mode 100644
index 0000000..234aa72
--- /dev/null
+++ b/drivers/usb/misc/diag_bridge_test.c
@@ -0,0 +1,204 @@
+/*
+ * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * 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/slab.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/debugfs.h>
+#include <linux/crc-ccitt.h>
+#include <mach/diag_bridge.h>
+
+#define RD_BUF_SIZE 2048
+#define DIAG_TEST_CONNECTED 0
+
+
+struct diag_test_dev {
+ char *read_buf;
+ struct work_struct read_w;
+ unsigned long flags;
+
+ struct diag_bridge_ops ops;
+};
+static struct diag_test_dev *__dev;
+static struct dentry *dent;
+
+static void
+diag_test_read_complete_cb(void *d, char *buf, size_t size, size_t actual)
+{
+ if (actual < 0) {
+ pr_err("%s: read complete err\n", __func__);
+ return;
+ }
+
+ print_hex_dump(KERN_INFO, "to_host:", 0, 1, 1, buf, actual, false);
+}
+static void diag_test_read_work(struct work_struct *w)
+{
+ struct diag_test_dev *dev =
+ container_of(w, struct diag_test_dev, read_w);
+
+ memset(dev->read_buf, 0, RD_BUF_SIZE);
+ diag_read(dev->read_buf, RD_BUF_SIZE);
+}
+
+static void
+diag_test_write_complete_cb(void *d, char *buf, size_t size, size_t actual)
+{
+ struct diag_test_dev *dev = d;
+
+ if (actual > 0)
+ schedule_work(&dev->read_w);
+}
+
+#if defined(CONFIG_DEBUG_FS)
+#define DEBUG_BUF_SIZE 1024
+static ssize_t send_ping_cmd(struct file *file, const char __user *ubuf,
+ size_t count, loff_t *ppos)
+{
+ struct diag_test_dev *dev = __dev;
+ unsigned char *buf;
+ int temp = sizeof(unsigned char) * 4;
+
+ if (!dev)
+ return -ENODEV;
+
+ buf = kmalloc(temp, GFP_KERNEL);
+ if (!buf) {
+ pr_err("%s: unable to allocate mem for ping cmd\n",
+ __func__);
+ return -ENOMEM;
+ }
+
+ /* hdlc encoded ping command */
+ buf[0] = 0x0C;
+ buf[1] = 0x14;
+ buf[2] = 0x3A;
+ buf[3] = 0x7E;
+
+ diag_write(buf, temp);
+
+ return count;
+}
+
+const struct file_operations diag_test_ping_ops = {
+ .write = send_ping_cmd,
+};
+
+static void diag_test_debug_init(void)
+{
+ struct dentry *dfile;
+
+ dent = debugfs_create_dir("diag_test", 0);
+ if (IS_ERR(dent))
+ return;
+
+ dfile = debugfs_create_file("send_ping", 0444, dent,
+ 0, &diag_test_ping_ops);
+ if (!dfile || IS_ERR(dfile))
+ debugfs_remove(dent);
+}
+#else
+static void diag_test_debug_init(void) { }
+#endif
+
+static int diag_test_remove(struct platform_device *pdev)
+{
+ diag_close();
+
+ if (dent) {
+ debugfs_remove_recursive(dent);
+ dent = NULL;
+ }
+
+ return 0;
+}
+
+static int diag_test_probe(struct platform_device *pdev)
+{
+ struct diag_test_dev *dev = __dev;
+ int ret = 0;
+
+ pr_info("%s:\n", __func__);
+
+ ret = diag_open(&dev->ops);
+ if (ret)
+ pr_err("diag open failed: %d", ret);
+
+
+ diag_test_debug_init();
+
+ return ret;
+}
+
+static struct platform_driver diag_test = {
+ .remove = diag_test_remove,
+ .probe = diag_test_probe,
+ .driver = {
+ .name = "diag_bridge",
+ .owner = THIS_MODULE,
+ },
+};
+
+static int __init diag_test_init(void)
+{
+ struct diag_test_dev *dev;
+ int ret = 0;
+
+ pr_info("%s\n", __func__);
+
+ dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+ if (!dev)
+ return -ENOMEM;
+
+ __dev = dev;
+
+ dev->ops.read_complete_cb = diag_test_read_complete_cb;
+ dev->ops.write_complete_cb = diag_test_write_complete_cb;
+ dev->read_buf = kmalloc(RD_BUF_SIZE, GFP_KERNEL);
+ if (!dev->read_buf) {
+ pr_err("%s: unable to allocate read buffer\n", __func__);
+ kfree(dev);
+ return -ENOMEM;
+ }
+
+ dev->ops.ctxt = dev;
+ INIT_WORK(&dev->read_w, diag_test_read_work);
+
+ ret = platform_driver_register(&diag_test);
+ if (ret)
+ pr_err("%s: platform driver %s register failed %d\n",
+ __func__, diag_test.driver.name, ret);
+
+ return ret;
+}
+
+static void __exit diag_test_exit(void)
+{
+ struct diag_test_dev *dev = __dev;
+
+ pr_info("%s:\n", __func__);
+
+ if (test_bit(DIAG_TEST_CONNECTED, &dev->flags))
+ diag_close();
+
+ kfree(dev->read_buf);
+ kfree(dev);
+
+}
+
+module_init(diag_test_init);
+module_exit(diag_test_exit);
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL V2");
diff --git a/drivers/usb/misc/diag_usb.c b/drivers/usb/misc/diag_usb.c
new file mode 100644
index 0000000..b3419f4
--- /dev/null
+++ b/drivers/usb/misc/diag_usb.c
@@ -0,0 +1,316 @@
+/*
+ * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * 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/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/kref.h>
+#include <linux/platform_device.h>
+#include <linux/uaccess.h>
+#include <linux/usb.h>
+#include <mach/diag_bridge.h>
+
+#define DRIVER_DESC "USB host diag bridge driver"
+#define DRIVER_VERSION "0.1"
+
+struct diag_bridge {
+ struct usb_device *udev;
+ struct usb_interface *ifc;
+ struct usb_anchor submitted;
+ __u8 in_epAddr;
+ __u8 out_epAddr;
+ struct kref kref;
+ struct diag_bridge_ops *ops;
+ struct platform_device *pdev;
+};
+struct diag_bridge *__dev;
+
+int diag_open(struct diag_bridge_ops *ops)
+{
+ struct diag_bridge *dev = __dev;
+
+ if (!dev) {
+ err("dev is null");
+ return -ENODEV;
+ }
+
+ dev->ops = ops;
+
+ return 0;
+}
+EXPORT_SYMBOL(diag_open);
+
+void diag_close(void)
+{
+ struct diag_bridge *dev = __dev;
+
+ dev_dbg(&dev->udev->dev, "%s:\n", __func__);
+
+ usb_kill_anchored_urbs(&dev->submitted);
+
+ dev->ops = 0;
+}
+EXPORT_SYMBOL(diag_close);
+
+static void diag_read_cb(struct urb *urb)
+{
+ struct diag_bridge *dev = urb->context;
+ struct diag_bridge_ops *cbs = dev->ops;
+
+ dev_dbg(&dev->udev->dev, "%s: status:%d actual:%d\n", __func__,
+ urb->status, urb->actual_length);
+ if (urb->status)
+ urb->actual_length = urb->status;
+
+ cbs->read_complete_cb(cbs->ctxt,
+ urb->transfer_buffer,
+ urb->transfer_buffer_length,
+ urb->actual_length);
+}
+
+int diag_read(char *data, size_t size)
+{
+ struct urb *urb = NULL;
+ unsigned int pipe;
+ struct diag_bridge *dev = __dev;
+ int ret;
+
+ dev_dbg(&dev->udev->dev, "%s:\n", __func__);
+
+ if (!size) {
+ dev_err(&dev->udev->dev, "invalid size:%d\n", size);
+ return -EINVAL;
+ }
+
+ if (!dev->ifc) {
+ dev_err(&dev->udev->dev, "device is disconnected\n");
+ return -ENODEV;
+ }
+
+ urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb) {
+ dev_err(&dev->udev->dev, "unable to allocate urb\n");
+ return -ENOMEM;
+ }
+
+ pipe = usb_rcvbulkpipe(dev->udev, dev->in_epAddr);
+ usb_fill_bulk_urb(urb, dev->udev, pipe, data, size,
+ diag_read_cb, dev);
+ usb_anchor_urb(urb, &dev->submitted);
+
+ ret = usb_submit_urb(urb, GFP_KERNEL);
+ if (ret) {
+ dev_err(&dev->udev->dev, "submitting urb failed err:%d\n", ret);
+ usb_unanchor_urb(urb);
+ usb_free_urb(urb);
+ return ret;
+ }
+
+ usb_free_urb(urb);
+
+ return 0;
+}
+EXPORT_SYMBOL(diag_read);
+
+static void diag_write_cb(struct urb *urb)
+{
+ struct diag_bridge *dev = urb->context;
+ struct diag_bridge_ops *cbs = dev->ops;
+
+ dev_dbg(&dev->udev->dev, "%s:\n", __func__);
+
+ if (urb->status)
+ urb->actual_length = urb->status;
+
+ cbs->write_complete_cb(cbs->ctxt,
+ urb->transfer_buffer,
+ urb->transfer_buffer_length,
+ urb->actual_length);
+}
+
+int diag_write(char *data, size_t size)
+{
+ struct urb *urb = NULL;
+ unsigned int pipe;
+ struct diag_bridge *dev = __dev;
+ int ret;
+
+ dev_dbg(&dev->udev->dev, "%s:\n", __func__);
+
+ if (!size) {
+ dev_err(&dev->udev->dev, "invalid size:%d\n", size);
+ return -EINVAL;
+ }
+
+ if (!dev->ifc) {
+ dev_err(&dev->udev->dev, "device is disconnected\n");
+ return -ENODEV;
+ }
+
+ urb = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb) {
+ err("unable to allocate urb");
+ return -ENOMEM;
+ }
+
+ pipe = usb_sndbulkpipe(dev->udev, dev->out_epAddr);
+ usb_fill_bulk_urb(urb, dev->udev, pipe, data, size,
+ diag_write_cb, dev);
+ usb_anchor_urb(urb, &dev->submitted);
+
+ ret = usb_submit_urb(urb, GFP_KERNEL);
+ if (ret) {
+ err("submitting urb failed err:%d", ret);
+ usb_unanchor_urb(urb);
+ usb_free_urb(urb);
+ return ret;
+ }
+
+ usb_free_urb(urb);
+
+ return 0;
+}
+EXPORT_SYMBOL(diag_write);
+
+static void diag_delete(struct kref *kref)
+{
+ struct diag_bridge *dev =
+ container_of(kref, struct diag_bridge, kref);
+
+ usb_put_dev(dev->udev);
+ __dev = 0;
+ kfree(dev);
+}
+
+static int
+diag_probe(struct usb_interface *ifc, const struct usb_device_id *id)
+{
+ struct diag_bridge *dev;
+ struct usb_host_interface *ifc_desc;
+ struct usb_endpoint_descriptor *ep_desc;
+ int i;
+ int ret = -ENOMEM;
+ __u8 ifc_num;
+
+ dbg("%s: id:%lu", __func__, id->driver_info);
+
+ ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
+
+ /* is this interface supported ? */
+ if (ifc_num != id->driver_info)
+ return -ENODEV;
+
+ dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+ if (!dev) {
+ pr_err("%s: unable to allocate dev\n", __func__);
+ return -ENOMEM;
+ }
+ dev->pdev = platform_device_alloc("diag_bridge", -1);
+ if (!dev->pdev) {
+ pr_err("%s: unable to allocate platform device\n", __func__);
+ kfree(dev);
+ return -ENOMEM;
+ }
+ __dev = dev;
+
+ dev->udev = usb_get_dev(interface_to_usbdev(ifc));
+ dev->ifc = ifc;
+ kref_init(&dev->kref);
+ init_usb_anchor(&dev->submitted);
+
+ ifc_desc = ifc->cur_altsetting;
+ for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
+ ep_desc = &ifc_desc->endpoint[i].desc;
+
+ if (!dev->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
+ dev->in_epAddr = ep_desc->bEndpointAddress;
+
+ if (!dev->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
+ dev->out_epAddr = ep_desc->bEndpointAddress;
+ }
+
+ if (!(dev->in_epAddr && dev->out_epAddr)) {
+ err("could not find bulk in and bulk out endpoints");
+ ret = -ENODEV;
+ goto error;
+ }
+
+ usb_set_intfdata(ifc, dev);
+
+ platform_device_add(dev->pdev);
+
+ dev_dbg(&dev->udev->dev, "%s: complete\n", __func__);
+
+ return 0;
+
+error:
+ if (dev)
+ kref_put(&dev->kref, diag_delete);
+
+ return ret;
+}
+
+static void diag_disconnect(struct usb_interface *ifc)
+{
+ struct diag_bridge *dev = usb_get_intfdata(ifc);
+
+ dev_dbg(&dev->udev->dev, "%s:\n", __func__);
+
+ platform_device_del(dev->pdev);
+ kref_put(&dev->kref, diag_delete);
+ usb_set_intfdata(ifc, NULL);
+}
+
+
+#define VALID_INTERFACE_NUM 0
+static const struct usb_device_id diag_ids[] = {
+ { USB_DEVICE(0x5c6, 0x9001),
+ .driver_info = VALID_INTERFACE_NUM, },
+
+ {} /* terminating entry */
+};
+MODULE_DEVICE_TABLE(usb, diag_ids);
+
+static struct usb_driver diag_driver = {
+ .name = "diag_qc",
+ .probe = diag_probe,
+ .disconnect = diag_disconnect,
+ .id_table = diag_ids,
+};
+
+static int __init diag_init(void)
+{
+ int ret;
+
+ ret = usb_register(&diag_driver);
+ if (ret) {
+ err("%s: unable to register diag driver",
+ __func__);
+ return ret;
+ }
+
+ return 0;
+}
+
+static void __exit diag_exit(void)
+{
+ usb_deregister(&diag_driver);
+}
+
+module_init(diag_init);
+module_exit(diag_exit);
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL V2");