diff options
Diffstat (limited to 'drivers/bluetooth/hci_vhci.c')
| -rw-r--r-- | drivers/bluetooth/hci_vhci.c | 378 |
1 files changed, 191 insertions, 187 deletions
diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c index 52cbd45c308..add1c6a7206 100644 --- a/drivers/bluetooth/hci_vhci.c +++ b/drivers/bluetooth/hci_vhci.c @@ -2,9 +2,9 @@ * * Bluetooth virtual HCI driver * - * Copyright (C) 2000-2001 Qualcomm Incorporated - * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> - * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> + * Copyright (C) 2000-2001 Qualcomm Incorporated + * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> + * Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org> * * * This program is free software; you can redistribute it and/or modify @@ -23,8 +23,8 @@ * */ -#include <linux/config.h> #include <linux/module.h> +#include <asm/unaligned.h> #include <linux/kernel.h> #include <linux/init.h> @@ -40,30 +40,19 @@ #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/hci_core.h> -#ifndef CONFIG_BT_HCIVHCI_DEBUG -#undef BT_DBG -#define BT_DBG(D...) -#endif +#define VERSION "1.4" -#define VERSION "1.2" - -static int minor = MISC_DYNAMIC_MINOR; +static bool amp; struct vhci_data { struct hci_dev *hdev; - unsigned long flags; - wait_queue_head_t read_wait; struct sk_buff_head readq; - struct fasync_struct *fasync; + struct delayed_work open_timeout; }; -#define VHCI_FASYNC 0x0010 - -static struct miscdevice vhci_miscdev; - static int vhci_open_dev(struct hci_dev *hdev) { set_bit(HCI_RUNNING, &hdev->flags); @@ -73,312 +62,327 @@ static int vhci_open_dev(struct hci_dev *hdev) static int vhci_close_dev(struct hci_dev *hdev) { - struct vhci_data *vhci = hdev->driver_data; + struct vhci_data *data = hci_get_drvdata(hdev); if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) return 0; - skb_queue_purge(&vhci->readq); + skb_queue_purge(&data->readq); return 0; } static int vhci_flush(struct hci_dev *hdev) { - struct vhci_data *vhci = hdev->driver_data; + struct vhci_data *data = hci_get_drvdata(hdev); - skb_queue_purge(&vhci->readq); + skb_queue_purge(&data->readq); return 0; } -static int vhci_send_frame(struct sk_buff *skb) +static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) { - struct hci_dev* hdev = (struct hci_dev *) skb->dev; - struct vhci_data *vhci; - - if (!hdev) { - BT_ERR("Frame for unknown HCI device (hdev=NULL)"); - return -ENODEV; - } + struct vhci_data *data = hci_get_drvdata(hdev); if (!test_bit(HCI_RUNNING, &hdev->flags)) return -EBUSY; - vhci = hdev->driver_data; - memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); - skb_queue_tail(&vhci->readq, skb); - - if (vhci->flags & VHCI_FASYNC) - kill_fasync(&vhci->fasync, SIGIO, POLL_IN); - - wake_up_interruptible(&vhci->read_wait); + skb_queue_tail(&data->readq, skb); + wake_up_interruptible(&data->read_wait); return 0; } -static void vhci_destruct(struct hci_dev *hdev) +static int vhci_create_device(struct vhci_data *data, __u8 dev_type) { - kfree(hdev->driver_data); + struct hci_dev *hdev; + struct sk_buff *skb; + + skb = bt_skb_alloc(4, GFP_KERNEL); + if (!skb) + return -ENOMEM; + + hdev = hci_alloc_dev(); + if (!hdev) { + kfree_skb(skb); + return -ENOMEM; + } + + data->hdev = hdev; + + hdev->bus = HCI_VIRTUAL; + hdev->dev_type = dev_type; + hci_set_drvdata(hdev, data); + + hdev->open = vhci_open_dev; + hdev->close = vhci_close_dev; + hdev->flush = vhci_flush; + hdev->send = vhci_send_frame; + + if (hci_register_dev(hdev) < 0) { + BT_ERR("Can't register HCI device"); + hci_free_dev(hdev); + data->hdev = NULL; + kfree_skb(skb); + return -EBUSY; + } + + bt_cb(skb)->pkt_type = HCI_VENDOR_PKT; + + *skb_put(skb, 1) = 0xff; + *skb_put(skb, 1) = dev_type; + put_unaligned_le16(hdev->id, skb_put(skb, 2)); + skb_queue_tail(&data->readq, skb); + + wake_up_interruptible(&data->read_wait); + return 0; } -static inline ssize_t vhci_get_user(struct vhci_data *vhci, - const char __user *buf, size_t count) +static inline ssize_t vhci_get_user(struct vhci_data *data, + const struct iovec *iov, + unsigned long count) { + size_t len = iov_length(iov, count); struct sk_buff *skb; + __u8 pkt_type, dev_type; + unsigned long i; + int ret; - if (count > HCI_MAX_FRAME_SIZE) + if (len < 2 || len > HCI_MAX_FRAME_SIZE) return -EINVAL; - skb = bt_skb_alloc(count, GFP_KERNEL); + skb = bt_skb_alloc(len, GFP_KERNEL); if (!skb) return -ENOMEM; - if (copy_from_user(skb_put(skb, count), buf, count)) { - kfree_skb(skb); - return -EFAULT; + for (i = 0; i < count; i++) { + if (copy_from_user(skb_put(skb, iov[i].iov_len), + iov[i].iov_base, iov[i].iov_len)) { + kfree_skb(skb); + return -EFAULT; + } } - skb->dev = (void *) vhci->hdev; - bt_cb(skb)->pkt_type = *((__u8 *) skb->data); + pkt_type = *((__u8 *) skb->data); skb_pull(skb, 1); - hci_recv_frame(skb); + switch (pkt_type) { + case HCI_EVENT_PKT: + case HCI_ACLDATA_PKT: + case HCI_SCODATA_PKT: + if (!data->hdev) { + kfree_skb(skb); + return -ENODEV; + } + + bt_cb(skb)->pkt_type = pkt_type; + + ret = hci_recv_frame(data->hdev, skb); + break; + + case HCI_VENDOR_PKT: + if (data->hdev) { + kfree_skb(skb); + return -EBADFD; + } + + cancel_delayed_work_sync(&data->open_timeout); + + dev_type = *((__u8 *) skb->data); + skb_pull(skb, 1); - return count; + if (skb->len > 0) { + kfree_skb(skb); + return -EINVAL; + } + + kfree_skb(skb); + + if (dev_type != HCI_BREDR && dev_type != HCI_AMP) + return -EINVAL; + + ret = vhci_create_device(data, dev_type); + break; + + default: + kfree_skb(skb); + return -EINVAL; + } + + return (ret < 0) ? ret : len; } -static inline ssize_t vhci_put_user(struct vhci_data *vhci, - struct sk_buff *skb, char __user *buf, int count) +static inline ssize_t vhci_put_user(struct vhci_data *data, + struct sk_buff *skb, + char __user *buf, int count) { char __user *ptr = buf; - int len, total = 0; + int len; len = min_t(unsigned int, skb->len, count); if (copy_to_user(ptr, skb->data, len)) return -EFAULT; - total += len; + if (!data->hdev) + return len; - vhci->hdev->stat.byte_tx += len; + data->hdev->stat.byte_tx += len; switch (bt_cb(skb)->pkt_type) { case HCI_COMMAND_PKT: - vhci->hdev->stat.cmd_tx++; + data->hdev->stat.cmd_tx++; break; - case HCI_ACLDATA_PKT: - vhci->hdev->stat.acl_tx++; + data->hdev->stat.acl_tx++; break; - case HCI_SCODATA_PKT: - vhci->hdev->stat.cmd_tx++; + data->hdev->stat.sco_tx++; break; - }; + } - return total; + return len; } -static loff_t vhci_llseek(struct file * file, loff_t offset, int origin) +static ssize_t vhci_read(struct file *file, + char __user *buf, size_t count, loff_t *pos) { - return -ESPIPE; -} - -static ssize_t vhci_read(struct file * file, char __user * buf, size_t count, loff_t *pos) -{ - DECLARE_WAITQUEUE(wait, current); - struct vhci_data *vhci = file->private_data; + struct vhci_data *data = file->private_data; struct sk_buff *skb; ssize_t ret = 0; - add_wait_queue(&vhci->read_wait, &wait); while (count) { - set_current_state(TASK_INTERRUPTIBLE); - - skb = skb_dequeue(&vhci->readq); - if (!skb) { - if (file->f_flags & O_NONBLOCK) { - ret = -EAGAIN; - break; - } - - if (signal_pending(current)) { - ret = -ERESTARTSYS; - break; - } - - schedule(); - continue; + skb = skb_dequeue(&data->readq); + if (skb) { + ret = vhci_put_user(data, skb, buf, count); + if (ret < 0) + skb_queue_head(&data->readq, skb); + else + kfree_skb(skb); + break; } - if (access_ok(VERIFY_WRITE, buf, count)) - ret = vhci_put_user(vhci, skb, buf, count); - else - ret = -EFAULT; + if (file->f_flags & O_NONBLOCK) { + ret = -EAGAIN; + break; + } - kfree_skb(skb); - break; + ret = wait_event_interruptible(data->read_wait, + !skb_queue_empty(&data->readq)); + if (ret < 0) + break; } - set_current_state(TASK_RUNNING); - remove_wait_queue(&vhci->read_wait, &wait); return ret; } -static ssize_t vhci_write(struct file *file, - const char __user *buf, size_t count, loff_t *pos) +static ssize_t vhci_write(struct kiocb *iocb, const struct iovec *iov, + unsigned long count, loff_t pos) { - struct vhci_data *vhci = file->private_data; - - if (!access_ok(VERIFY_READ, buf, count)) - return -EFAULT; + struct file *file = iocb->ki_filp; + struct vhci_data *data = file->private_data; - return vhci_get_user(vhci, buf, count); + return vhci_get_user(data, iov, count); } static unsigned int vhci_poll(struct file *file, poll_table *wait) { - struct vhci_data *vhci = file->private_data; + struct vhci_data *data = file->private_data; - poll_wait(file, &vhci->read_wait, wait); + poll_wait(file, &data->read_wait, wait); - if (!skb_queue_empty(&vhci->readq)) + if (!skb_queue_empty(&data->readq)) return POLLIN | POLLRDNORM; return POLLOUT | POLLWRNORM; } -static int vhci_ioctl(struct inode *inode, struct file *file, - unsigned int cmd, unsigned long arg) +static void vhci_open_timeout(struct work_struct *work) { - return -EINVAL; + struct vhci_data *data = container_of(work, struct vhci_data, + open_timeout.work); + + vhci_create_device(data, amp ? HCI_AMP : HCI_BREDR); } static int vhci_open(struct inode *inode, struct file *file) { - struct vhci_data *vhci; - struct hci_dev *hdev; - - vhci = kmalloc(sizeof(struct vhci_data), GFP_KERNEL); - if (!vhci) - return -ENOMEM; - - memset(vhci, 0, sizeof(struct vhci_data)); - - skb_queue_head_init(&vhci->readq); - init_waitqueue_head(&vhci->read_wait); + struct vhci_data *data; - hdev = hci_alloc_dev(); - if (!hdev) { - kfree(vhci); + data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL); + if (!data) return -ENOMEM; - } - vhci->hdev = hdev; + skb_queue_head_init(&data->readq); + init_waitqueue_head(&data->read_wait); - hdev->type = HCI_VHCI; - hdev->driver_data = vhci; - SET_HCIDEV_DEV(hdev, vhci_miscdev.dev); + INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout); - hdev->open = vhci_open_dev; - hdev->close = vhci_close_dev; - hdev->flush = vhci_flush; - hdev->send = vhci_send_frame; - hdev->destruct = vhci_destruct; + file->private_data = data; + nonseekable_open(inode, file); - hdev->owner = THIS_MODULE; + schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000)); - if (hci_register_dev(hdev) < 0) { - BT_ERR("Can't register HCI device"); - kfree(vhci); - hci_free_dev(hdev); - return -EBUSY; - } - - file->private_data = vhci; - - return nonseekable_open(inode, file); + return 0; } static int vhci_release(struct inode *inode, struct file *file) { - struct vhci_data *vhci = file->private_data; - struct hci_dev *hdev = vhci->hdev; + struct vhci_data *data = file->private_data; + struct hci_dev *hdev = data->hdev; - if (hci_unregister_dev(hdev) < 0) { - BT_ERR("Can't unregister HCI device %s", hdev->name); - } + cancel_delayed_work_sync(&data->open_timeout); - hci_free_dev(hdev); + if (hdev) { + hci_unregister_dev(hdev); + hci_free_dev(hdev); + } file->private_data = NULL; + kfree(data); return 0; } -static int vhci_fasync(int fd, struct file *file, int on) -{ - struct vhci_data *vhci = file->private_data; - int err; - - err = fasync_helper(fd, file, on, &vhci->fasync); - if (err < 0) - return err; - - if (on) - vhci->flags |= VHCI_FASYNC; - else - vhci->flags &= ~VHCI_FASYNC; - - return 0; -} - -static struct file_operations vhci_fops = { +static const struct file_operations vhci_fops = { .owner = THIS_MODULE, - .llseek = vhci_llseek, .read = vhci_read, - .write = vhci_write, + .aio_write = vhci_write, .poll = vhci_poll, - .ioctl = vhci_ioctl, .open = vhci_open, .release = vhci_release, - .fasync = vhci_fasync, + .llseek = no_llseek, }; static struct miscdevice vhci_miscdev= { - .name = "vhci", - .fops = &vhci_fops, + .name = "vhci", + .fops = &vhci_fops, + .minor = VHCI_MINOR, }; static int __init vhci_init(void) { BT_INFO("Virtual HCI driver ver %s", VERSION); - vhci_miscdev.minor = minor; - - if (misc_register(&vhci_miscdev) < 0) { - BT_ERR("Can't register misc device with minor %d", minor); - return -EIO; - } - - return 0; + return misc_register(&vhci_miscdev); } static void __exit vhci_exit(void) { - if (misc_deregister(&vhci_miscdev) < 0) - BT_ERR("Can't unregister misc device with minor %d", minor); + misc_deregister(&vhci_miscdev); } module_init(vhci_init); module_exit(vhci_exit); -module_param(minor, int, 0444); -MODULE_PARM_DESC(minor, "Miscellaneous minor device number"); +module_param(amp, bool, 0644); +MODULE_PARM_DESC(amp, "Create AMP controller device"); -MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>"); +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION); MODULE_VERSION(VERSION); MODULE_LICENSE("GPL"); +MODULE_ALIAS("devname:vhci"); +MODULE_ALIAS_MISCDEV(VHCI_MINOR); |
