Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / bluetooth / hci_vhci.c
index 6653473..597b2d1 100644 (file)
@@ -50,13 +50,12 @@ struct vhci_data {
        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)
 {
-       set_bit(HCI_RUNNING, &hdev->flags);
-
        return 0;
 }
 
@@ -64,9 +63,6 @@ static int vhci_close_dev(struct hci_dev *hdev)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);
 
-       if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
-               return 0;
-
        skb_queue_purge(&data->readq);
 
        return 0;
@@ -85,9 +81,6 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);
 
-       if (!test_bit(HCI_RUNNING, &hdev->flags))
-               return -EBUSY;
-
        memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
        skb_queue_tail(&data->readq, skb);
 
@@ -95,12 +88,15 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
        return 0;
 }
 
-static int vhci_create_device(struct vhci_data *data, __u8 opcode)
+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;
 
@@ -159,6 +155,17 @@ static int vhci_create_device(struct vhci_data *data, __u8 opcode)
        return 0;
 }
 
+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;
+}
+
 static inline ssize_t vhci_get_user(struct vhci_data *data,
                                    struct iov_iter *from)
 {
@@ -197,11 +204,6 @@ static inline ssize_t vhci_get_user(struct vhci_data *data,
                break;
 
        case HCI_VENDOR_PKT:
-               if (data->hdev) {
-                       kfree_skb(skb);
-                       return -EBADFD;
-               }
-
                cancel_delayed_work_sync(&data->open_timeout);
 
                opcode = *((__u8 *) skb->data);
@@ -328,6 +330,7 @@ static int vhci_open(struct inode *inode, struct file *file)
        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;
@@ -341,15 +344,18 @@ static int vhci_open(struct inode *inode, struct file *file)
 static int vhci_release(struct inode *inode, struct file *file)
 {
        struct vhci_data *data = file->private_data;
-       struct hci_dev *hdev = data->hdev;
+       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);
 
@@ -366,7 +372,7 @@ static const struct file_operations vhci_fops = {
        .llseek         = no_llseek,
 };
 
-static struct miscdevice vhci_miscdev= {
+static struct miscdevice vhci_miscdev = {
        .name   = "vhci",
        .fops   = &vhci_fops,
        .minor  = VHCI_MINOR,