Merge branch 'iov_iter' into for-davem
[cascardo/linux.git] / net / bluetooth / hci_core.c
index 0171069..46b114c 100644 (file)
@@ -80,7 +80,7 @@ static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
        struct hci_dev *hdev = file->private_data;
        char buf[3];
 
-       buf[0] = test_bit(HCI_DUT_MODE, &hdev->dbg_flags) ? 'Y': 'N';
+       buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y': 'N';
        buf[1] = '\n';
        buf[2] = '\0';
        return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -106,7 +106,7 @@ static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
        if (strtobool(buf, &enable))
                return -EINVAL;
 
-       if (enable == test_bit(HCI_DUT_MODE, &hdev->dbg_flags))
+       if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
                return -EALREADY;
 
        hci_req_lock(hdev);
@@ -127,7 +127,7 @@ static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
        if (err < 0)
                return err;
 
-       change_bit(HCI_DUT_MODE, &hdev->dbg_flags);
+       hci_dev_change_flag(hdev, HCI_DUT_MODE);
 
        return count;
 }
@@ -141,13 +141,16 @@ static const struct file_operations dut_mode_fops = {
 
 /* ---- HCI requests ---- */
 
-static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode)
+static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
+                                 struct sk_buff *skb)
 {
        BT_DBG("%s result 0x%2.2x", hdev->name, result);
 
        if (hdev->req_status == HCI_REQ_PEND) {
                hdev->req_result = result;
                hdev->req_status = HCI_REQ_DONE;
+               if (skb)
+                       hdev->req_skb = skb_get(skb);
                wake_up_interruptible(&hdev->req_wait_q);
        }
 }
@@ -163,66 +166,12 @@ static void hci_req_cancel(struct hci_dev *hdev, int err)
        }
 }
 
-static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
-                                           u8 event)
-{
-       struct hci_ev_cmd_complete *ev;
-       struct hci_event_hdr *hdr;
-       struct sk_buff *skb;
-
-       hci_dev_lock(hdev);
-
-       skb = hdev->recv_evt;
-       hdev->recv_evt = NULL;
-
-       hci_dev_unlock(hdev);
-
-       if (!skb)
-               return ERR_PTR(-ENODATA);
-
-       if (skb->len < sizeof(*hdr)) {
-               BT_ERR("Too short HCI event");
-               goto failed;
-       }
-
-       hdr = (void *) skb->data;
-       skb_pull(skb, HCI_EVENT_HDR_SIZE);
-
-       if (event) {
-               if (hdr->evt != event)
-                       goto failed;
-               return skb;
-       }
-
-       if (hdr->evt != HCI_EV_CMD_COMPLETE) {
-               BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
-               goto failed;
-       }
-
-       if (skb->len < sizeof(*ev)) {
-               BT_ERR("Too short cmd_complete event");
-               goto failed;
-       }
-
-       ev = (void *) skb->data;
-       skb_pull(skb, sizeof(*ev));
-
-       if (opcode == __le16_to_cpu(ev->opcode))
-               return skb;
-
-       BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
-              __le16_to_cpu(ev->opcode));
-
-failed:
-       kfree_skb(skb);
-       return ERR_PTR(-ENODATA);
-}
-
 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
                                  const void *param, u8 event, u32 timeout)
 {
        DECLARE_WAITQUEUE(wait, current);
        struct hci_request req;
+       struct sk_buff *skb;
        int err = 0;
 
        BT_DBG("%s", hdev->name);
@@ -236,7 +185,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
        add_wait_queue(&hdev->req_wait_q, &wait);
        set_current_state(TASK_INTERRUPTIBLE);
 
-       err = hci_req_run(&req, hci_req_sync_complete);
+       err = hci_req_run_skb(&req, hci_req_sync_complete);
        if (err < 0) {
                remove_wait_queue(&hdev->req_wait_q, &wait);
                set_current_state(TASK_RUNNING);
@@ -265,13 +214,20 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
        }
 
        hdev->req_status = hdev->req_result = 0;
+       skb = hdev->req_skb;
+       hdev->req_skb = NULL;
 
        BT_DBG("%s end: err %d", hdev->name, err);
 
-       if (err < 0)
+       if (err < 0) {
+               kfree_skb(skb);
                return ERR_PTR(err);
+       }
+
+       if (!skb)
+               return ERR_PTR(-ENODATA);
 
-       return hci_get_cmd_complete(hdev, opcode, event);
+       return skb;
 }
 EXPORT_SYMBOL(__hci_cmd_sync_ev);
 
@@ -303,7 +259,7 @@ static int __hci_req_sync(struct hci_dev *hdev,
        add_wait_queue(&hdev->req_wait_q, &wait);
        set_current_state(TASK_INTERRUPTIBLE);
 
-       err = hci_req_run(&req, hci_req_sync_complete);
+       err = hci_req_run_skb(&req, hci_req_sync_complete);
        if (err < 0) {
                hdev->req_status = 0;
 
@@ -1554,7 +1510,7 @@ int hci_dev_open(__u16 dev)
         * particularly important if the setup procedure has not yet
         * completed.
         */
-       if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
+       if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF))
                cancel_delayed_work(&hdev->power_off);
 
        /* After this call it is guaranteed that the setup procedure
@@ -1629,7 +1585,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
        }
 
-       if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
+       if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
                cancel_delayed_work(&hdev->service_cache);
 
        cancel_delayed_work_sync(&hdev->le_scan_disable);
@@ -1647,7 +1603,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 
        hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 
-       if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
+       if (!hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
                if (hdev->dev_type == HCI_BREDR)
                        mgmt_powered(hdev, 0);
        }
@@ -1690,16 +1646,13 @@ static int hci_dev_do_close(struct hci_dev *hdev)
                hdev->sent_cmd = NULL;
        }
 
-       kfree_skb(hdev->recv_evt);
-       hdev->recv_evt = NULL;
-
        /* After this point our queues are empty
         * and no tasks are scheduled. */
        hdev->close(hdev);
 
        /* Clear flags */
        hdev->flags &= BIT(HCI_RAW);
-       hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
+       hci_dev_clear_volatile_flags(hdev);
 
        /* Controller radio is available but is currently powered down */
        hdev->amp_status = AMP_STATUS_POWERED_DOWN;
@@ -1728,7 +1681,7 @@ int hci_dev_close(__u16 dev)
                goto done;
        }
 
-       if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
+       if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF))
                cancel_delayed_work(&hdev->power_off);
 
        err = hci_dev_do_close(hdev);
@@ -1836,19 +1789,19 @@ static void hci_update_scan_state(struct hci_dev *hdev, u8 scan)
        BT_DBG("%s scan 0x%02x", hdev->name, scan);
 
        if ((scan & SCAN_PAGE))
-               conn_changed = !test_and_set_bit(HCI_CONNECTABLE,
-                                                &hdev->dev_flags);
+               conn_changed = !hci_dev_test_and_set_flag(hdev,
+                                                         HCI_CONNECTABLE);
        else
-               conn_changed = test_and_clear_bit(HCI_CONNECTABLE,
-                                                 &hdev->dev_flags);
+               conn_changed = hci_dev_test_and_clear_flag(hdev,
+                                                          HCI_CONNECTABLE);
 
        if ((scan & SCAN_INQUIRY)) {
-               discov_changed = !test_and_set_bit(HCI_DISCOVERABLE,
-                                                  &hdev->dev_flags);
+               discov_changed = !hci_dev_test_and_set_flag(hdev,
+                                                           HCI_DISCOVERABLE);
        } else {
                hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
-               discov_changed = test_and_clear_bit(HCI_DISCOVERABLE,
-                                                   &hdev->dev_flags);
+               discov_changed = hci_dev_test_and_clear_flag(hdev,
+                                                            HCI_DISCOVERABLE);
        }
 
        if (!hci_dev_test_flag(hdev, HCI_MGMT))
@@ -2128,7 +2081,7 @@ static void hci_power_on(struct work_struct *work)
                                   HCI_AUTO_OFF_TIMEOUT);
        }
 
-       if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) {
+       if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
                /* For unconfigured devices, set the HCI_RAW flag
                 * so that userspace can easily identify them.
                 */
@@ -2143,7 +2096,7 @@ static void hci_power_on(struct work_struct *work)
                 * and no event will be send.
                 */
                mgmt_index_added(hdev);
-       } else if (test_and_clear_bit(HCI_CONFIG, &hdev->dev_flags)) {
+       } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
                /* When the controller is now configured, then it
                 * is important to clear the HCI_RAW flag.
                 */
@@ -2874,7 +2827,6 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status,
 {
        /* General inquiry access code (GIAC) */
        u8 lap[3] = { 0x33, 0x8b, 0x9e };
-       struct hci_request req;
        struct hci_cp_inquiry cp;
        int err;
 
@@ -2893,21 +2845,37 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status,
                break;
 
        case DISCOV_TYPE_INTERLEAVED:
-               hci_req_init(&req, hdev);
+               hci_dev_lock(hdev);
 
-               memset(&cp, 0, sizeof(cp));
-               memcpy(&cp.lap, lap, sizeof(cp.lap));
-               cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN;
-               hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp);
+               if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
+                            &hdev->quirks)) {
+                       /* If we were running LE only scan, change discovery
+                        * state. If we were running both LE and BR/EDR inquiry
+                        * simultaneously, and BR/EDR inquiry is already
+                        * finished, stop discovery, otherwise BR/EDR inquiry
+                        * will stop discovery when finished.
+                        */
+                       if (!test_bit(HCI_INQUIRY, &hdev->flags))
+                               hci_discovery_set_state(hdev,
+                                                       DISCOVERY_STOPPED);
+               } else {
+                       struct hci_request req;
 
-               hci_dev_lock(hdev);
+                       hci_inquiry_cache_flush(hdev);
 
-               hci_inquiry_cache_flush(hdev);
+                       hci_req_init(&req, hdev);
 
-               err = hci_req_run(&req, inquiry_complete);
-               if (err) {
-                       BT_ERR("Inquiry request failed: err %d", err);
-                       hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
+                       memset(&cp, 0, sizeof(cp));
+                       memcpy(&cp.lap, lap, sizeof(cp.lap));
+                       cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN;
+                       hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp);
+
+                       err = hci_req_run(&req, inquiry_complete);
+                       if (err) {
+                               BT_ERR("Inquiry request failed: err %d", err);
+                               hci_discovery_set_state(hdev,
+                                                       DISCOVERY_STOPPED);
+                       }
                }
 
                hci_dev_unlock(hdev);
@@ -3019,7 +2987,7 @@ static void le_scan_restart_work(struct work_struct *work)
 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
                               u8 *bdaddr_type)
 {
-       if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ||
+       if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
            !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
            (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
             bacmp(&hdev->static_addr, BDADDR_ANY))) {
@@ -3111,6 +3079,7 @@ struct hci_dev *hci_alloc_dev(void)
 
        hci_init_sysfs(hdev);
        discovery_init(hdev);
+       adv_info_init(hdev);
 
        return hdev;
 }
@@ -3547,11 +3516,6 @@ static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
        }
 }
 
-bool hci_req_pending(struct hci_dev *hdev)
-{
-       return (hdev->req_status == HCI_REQ_PEND);
-}
-
 /* Send HCI command */
 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
                 const void *param)
@@ -3569,7 +3533,7 @@ int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
        /* Stand-alone HCI commands must be flagged as
         * single-command requests.
         */
-       bt_cb(skb)->req_start = 1;
+       bt_cb(skb)->req.start = true;
 
        skb_queue_tail(&hdev->cmd_q, skb);
        queue_work(hdev->workqueue, &hdev->cmd_work);
@@ -4247,7 +4211,7 @@ static bool hci_req_is_complete(struct hci_dev *hdev)
        if (!skb)
                return true;
 
-       return bt_cb(skb)->req_start;
+       return bt_cb(skb)->req.start;
 }
 
 static void hci_resend_last(struct hci_dev *hdev)
@@ -4272,9 +4236,10 @@ static void hci_resend_last(struct hci_dev *hdev)
        queue_work(hdev->workqueue, &hdev->cmd_work);
 }
 
-void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
+void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
+                         hci_req_complete_t *req_complete,
+                         hci_req_complete_skb_t *req_complete_skb)
 {
-       hci_req_complete_t req_complete = NULL;
        struct sk_buff *skb;
        unsigned long flags;
 
@@ -4306,36 +4271,29 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
         * callback would be found in hdev->sent_cmd instead of the
         * command queue (hdev->cmd_q).
         */
-       if (hdev->sent_cmd) {
-               req_complete = bt_cb(hdev->sent_cmd)->req_complete;
-
-               if (req_complete) {
-                       /* We must set the complete callback to NULL to
-                        * avoid calling the callback more than once if
-                        * this function gets called again.
-                        */
-                       bt_cb(hdev->sent_cmd)->req_complete = NULL;
+       if (bt_cb(hdev->sent_cmd)->req.complete) {
+               *req_complete = bt_cb(hdev->sent_cmd)->req.complete;
+               return;
+       }
 
-                       goto call_complete;
-               }
+       if (bt_cb(hdev->sent_cmd)->req.complete_skb) {
+               *req_complete_skb = bt_cb(hdev->sent_cmd)->req.complete_skb;
+               return;
        }
 
        /* Remove all pending commands belonging to this request */
        spin_lock_irqsave(&hdev->cmd_q.lock, flags);
        while ((skb = __skb_dequeue(&hdev->cmd_q))) {
-               if (bt_cb(skb)->req_start) {
+               if (bt_cb(skb)->req.start) {
                        __skb_queue_head(&hdev->cmd_q, skb);
                        break;
                }
 
-               req_complete = bt_cb(skb)->req_complete;
+               *req_complete = bt_cb(skb)->req.complete;
+               *req_complete_skb = bt_cb(skb)->req.complete_skb;
                kfree_skb(skb);
        }
        spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
-
-call_complete:
-       if (req_complete)
-               req_complete(hdev, status, status ? opcode : HCI_OP_NOP);
 }
 
 static void hci_rx_work(struct work_struct *work)