cregit-Linux how code gets into the kernel

Release 4.7 drivers/bluetooth/hci_vhci.c

/*
 *
 *  Bluetooth virtual HCI driver
 *
 *  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
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/module.h>
#include <asm/unaligned.h>

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/poll.h>

#include <linux/skbuff.h>
#include <linux/miscdevice.h>

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>


#define VERSION "1.5"


static bool amp;


struct vhci_data {
	
struct hci_dev *hdev;

	
wait_queue_head_t read_wait;
	
struct sk_buff_head readq;

	
struct mutex open_mutex;
	
struct delayed_work open_timeout;
};


static int vhci_open_dev(struct hci_dev *hdev) { return 0; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds1285.71%133.33%
marcel holtmannmarcel holtmann17.14%133.33%
maksim krasnyanskiymaksim krasnyanskiy17.14%133.33%
Total14100.00%3100.00%


static int vhci_close_dev(struct hci_dev *hdev) { struct vhci_data *data = hci_get_drvdata(hdev); skb_queue_purge(&data->readq); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds2062.50%120.00%
marcel holtmannmarcel holtmann825.00%240.00%
david herrmanndavid herrmann39.38%120.00%
maksim krasnyanskiymaksim krasnyanskiy13.12%120.00%
Total32100.00%5100.00%


static int vhci_flush(struct hci_dev *hdev) { struct vhci_data *data = hci_get_drvdata(hdev); skb_queue_purge(&data->readq); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
maksim krasnyanskiymaksim krasnyanskiy1546.88%125.00%
marcel holtmannmarcel holtmann1443.75%250.00%
david herrmanndavid herrmann39.38%125.00%
Total32100.00%4100.00%


static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) { struct vhci_data *data = hci_get_drvdata(hdev); memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); skb_queue_tail(&data->readq, skb); wake_up_interruptible(&data->read_wait); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds4772.31%112.50%
marcel holtmannmarcel holtmann1726.15%675.00%
maksim krasnyanskiymaksim krasnyanskiy11.54%112.50%
Total65100.00%8100.00%


static int __vhci_create_device(struct vhci_data *data, __u8 opcode) { struct hci_dev *hdev; struct sk_buff *skb; __u8 dev_type; if (data->hdev) return -EBADFD; /* bits 0-1 are dev_type (BR/EDR or AMP) */ dev_type = opcode & 0x03; if (dev_type != HCI_BREDR && dev_type != HCI_AMP) return -EINVAL; /* bits 2-5 are reserved (must be zero) */ if (opcode & 0x3c) return -EINVAL; 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; /* bit 6 is for external configuration */ if (opcode & 0x40) set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks); /* bit 7 is for raw device */ if (opcode & 0x80) set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); 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; } hci_skb_pkt_type(skb) = HCI_VENDOR_PKT; *skb_put(skb, 1) = 0xff; *skb_put(skb, 1) = opcode; put_unaligned_le16(hdev->id, skb_put(skb, 2)); skb_queue_tail(&data->readq, skb); wake_up_interruptible(&data->read_wait); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
marcel holtmannmarcel holtmann27896.19%480.00%
takashi iwaitakashi iwai113.81%120.00%
Total289100.00%5100.00%


static int vhci_create_device(struct vhci_data *data, __u8 opcode) { int err; mutex_lock(&data->open_mutex); err = __vhci_create_device(data, opcode); mutex_unlock(&data->open_mutex); return err; }

Contributors

PersonTokensPropCommitsCommitProp
takashi iwaitakashi iwai45100.00%1100.00%
Total45100.00%1100.00%


static inline ssize_t vhci_get_user(struct vhci_data *data, struct iov_iter *from) { size_t len = iov_iter_count(from); struct sk_buff *skb; __u8 pkt_type, opcode; int ret; if (len < 2 || len > HCI_MAX_FRAME_SIZE) return -EINVAL; skb = bt_skb_alloc(len, GFP_KERNEL); if (!skb) return -ENOMEM; if (copy_from_iter(skb_put(skb, len), len, from) != len) { kfree_skb(skb); return -EFAULT; } pkt_type = *((__u8 *) skb->data); skb_pull(skb, 1); switch (pkt_type) { case HCI_EVENT_PKT: case HCI_ACLDATA_PKT: case HCI_SCODATA_PKT: if (!data->hdev) { kfree_skb(skb); return -ENODEV; } hci_skb_pkt_type(skb) = pkt_type; ret = hci_recv_frame(data->hdev, skb); break; case HCI_VENDOR_PKT: cancel_delayed_work_sync(&data->open_timeout); opcode = *((__u8 *) skb->data); skb_pull(skb, 1); if (skb->len > 0) { kfree_skb(skb); return -EINVAL; } kfree_skb(skb); ret = vhci_create_device(data, opcode); break; default: kfree_skb(skb); return -EINVAL; } return (ret < 0) ? ret : len; }

Contributors

PersonTokensPropCommitsCommitProp
marcel holtmannmarcel holtmann15761.09%753.85%
linus torvaldslinus torvalds7127.63%215.38%
al viroal viro103.89%17.69%
arnaldo carvalho de meloarnaldo carvalho de melo103.89%17.69%
jiri slabyjiri slaby83.11%17.69%
maksim krasnyanskiymaksim krasnyanskiy10.39%17.69%
Total257100.00%13100.00%


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; len = min_t(unsigned int, skb->len, count); if (copy_to_user(ptr, skb->data, len)) return -EFAULT; if (!data->hdev) return len; data->hdev->stat.byte_tx += len; switch (hci_skb_pkt_type(skb)) { case HCI_COMMAND_PKT: data->hdev->stat.cmd_tx++; break; case HCI_ACLDATA_PKT: data->hdev->stat.acl_tx++; break; case HCI_SCODATA_PKT: data->hdev->stat.sco_tx++; break; } return len; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds8563.43%19.09%
marcel holtmannmarcel holtmann3626.87%654.55%
arnaldo carvalho de meloarnaldo carvalho de melo75.22%19.09%
maksim krasnyanskiymaksim krasnyanskiy42.99%19.09%
al viroal viro10.75%19.09%
gustavo f. padovangustavo f. padovan10.75%19.09%
Total134100.00%11100.00%


static ssize_t vhci_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { struct vhci_data *data = file->private_data; struct sk_buff *skb; ssize_t ret = 0; while (count) { 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 (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; break; } ret = wait_event_interruptible(data->read_wait, !skb_queue_empty(&data->readq)); if (ret < 0) break; } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds7954.86%233.33%
marcel holtmannmarcel holtmann6444.44%350.00%
al viroal viro10.69%116.67%
Total144100.00%6100.00%


static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from) { struct file *file = iocb->ki_filp; struct vhci_data *data = file->private_data; return vhci_get_user(data, from); }

Contributors

PersonTokensPropCommitsCommitProp
marcel holtmannmarcel holtmann2764.29%350.00%
maksim krasnyanskiymaksim krasnyanskiy716.67%116.67%
linus torvaldslinus torvalds511.90%116.67%
al viroal viro37.14%116.67%
Total42100.00%6100.00%


static unsigned int vhci_poll(struct file *file, poll_table *wait) { struct vhci_data *data = file->private_data; poll_wait(file, &data->read_wait, wait); if (!skb_queue_empty(&data->readq)) return POLLIN | POLLRDNORM; return POLLOUT | POLLWRNORM; }

Contributors

PersonTokensPropCommitsCommitProp
marcel holtmannmarcel holtmann3051.72%266.67%
linus torvaldslinus torvalds2848.28%133.33%
Total58100.00%3100.00%


static void vhci_open_timeout(struct work_struct *work) { struct vhci_data *data = container_of(work, struct vhci_data, open_timeout.work); vhci_create_device(data, amp ? HCI_AMP : HCI_BREDR); }

Contributors

PersonTokensPropCommitsCommitProp
marcel holtmannmarcel holtmann39100.00%1100.00%
Total39100.00%1100.00%


static int vhci_open(struct inode *inode, struct file *file) { struct vhci_data *data; data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL); if (!data) return -ENOMEM; skb_queue_head_init(&data->readq); init_waitqueue_head(&data->read_wait); mutex_init(&data->open_mutex); INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout); file->private_data = data; nonseekable_open(inode, file); schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds5955.66%220.00%
marcel holtmannmarcel holtmann3432.08%440.00%
takashi iwaitakashi iwai87.55%110.00%
david herrmanndavid herrmann43.77%220.00%
deepak saxenadeepak saxena10.94%110.00%
Total106100.00%10100.00%


static int vhci_release(struct inode *inode, struct file *file) { struct vhci_data *data = file->private_data; struct hci_dev *hdev; cancel_delayed_work_sync(&data->open_timeout); hdev = data->hdev; if (hdev) { hci_unregister_dev(hdev); hci_free_dev(hdev); } skb_queue_purge(&data->readq); file->private_data = NULL; kfree(data); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds3643.90%111.11%
marcel holtmannmarcel holtmann2732.93%555.56%
jiri slabyjiri slaby1417.07%222.22%
david herrmanndavid herrmann56.10%111.11%
Total82100.00%9100.00%

static const struct file_operations vhci_fops = { .owner = THIS_MODULE, .read = vhci_read, .write_iter = vhci_write, .poll = vhci_poll, .open = vhci_open, .release = vhci_release, .llseek = no_llseek, }; static struct miscdevice vhci_miscdev = { .name = "vhci", .fops = &vhci_fops, .minor = VHCI_MINOR, };
static int __init vhci_init(void) { BT_INFO("Virtual HCI driver ver %s", VERSION); return misc_register(&vhci_miscdev); }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds1773.91%233.33%
marcel holtmannmarcel holtmann521.74%350.00%
maksim krasnyanskiymaksim krasnyanskiy14.35%116.67%
Total23100.00%6100.00%


static void __exit vhci_exit(void) { misc_deregister(&vhci_miscdev); }

Contributors

PersonTokensPropCommitsCommitProp
linus torvaldslinus torvalds1066.67%133.33%
marcel holtmannmarcel holtmann533.33%266.67%
Total15100.00%3100.00%

module_init(vhci_init); module_exit(vhci_exit); module_param(amp, bool, 0644); MODULE_PARM_DESC(amp, "Create AMP controller device"); 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);

Overall Contributors

PersonTokensPropCommitsCommitProp
marcel holtmannmarcel holtmann81852.00%2047.62%
linus torvaldslinus torvalds54234.46%37.14%
takashi iwaitakashi iwai684.32%12.38%
maksim krasnyanskiymaksim krasnyanskiy311.97%37.14%
jiri slabyjiri slaby221.40%24.76%
andrei emeltchenkoandrei emeltchenko201.27%12.38%
arnaldo carvalho de meloarnaldo carvalho de melo171.08%12.38%
al viroal viro161.02%24.76%
david herrmanndavid herrmann150.95%37.14%
rusty russellrusty russell100.64%12.38%
lucas de marchilucas de marchi60.38%12.38%
arnd bergmannarnd bergmann50.32%12.38%
deepak saxenadeepak saxena10.06%12.38%
arjan van de venarjan van de ven10.06%12.38%
gustavo f. padovangustavo f. padovan10.06%12.38%
Total1573100.00%42100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}