YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
Gustavo F. Padovan | 590051d | 2011-12-18 13:39:33 -0200 | [diff] [blame] | 4 | Copyright (C) 2011 ProFUSION Embedded Systems |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License version 2 as |
| 10 | published by the Free Software Foundation; |
| 11 | |
| 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 13 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 15 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 16 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 17 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 18 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 21 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 22 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | SOFTWARE IS DISCLAIMED. |
| 24 | */ |
| 25 | |
| 26 | /* Bluetooth HCI core. */ |
| 27 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 28 | #include <linux/export.h> |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 29 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 31 | #include <linux/rfkill.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <net/bluetooth/bluetooth.h> |
| 34 | #include <net/bluetooth/hci_core.h> |
| 35 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 36 | static void hci_rx_work(struct work_struct *work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 37 | static void hci_cmd_work(struct work_struct *work); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 38 | static void hci_tx_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* HCI device list */ |
| 41 | LIST_HEAD(hci_dev_list); |
| 42 | DEFINE_RWLOCK(hci_dev_list_lock); |
| 43 | |
| 44 | /* HCI callback list */ |
| 45 | LIST_HEAD(hci_cb_list); |
| 46 | DEFINE_RWLOCK(hci_cb_list_lock); |
| 47 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 48 | /* HCI ID Numbering */ |
| 49 | static DEFINE_IDA(hci_index_ida); |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* ---- HCI notifications ---- */ |
| 52 | |
Marcel Holtmann | 6516455 | 2005-10-28 19:20:48 +0200 | [diff] [blame] | 53 | static void hci_notify(struct hci_dev *hdev, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 55 | hci_sock_dev_event(hdev, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* ---- HCI requests ---- */ |
| 59 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 60 | static void hci_req_sync_complete(struct hci_dev *hdev, u8 result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 62 | BT_DBG("%s result 0x%2.2x", hdev->name, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | if (hdev->req_status == HCI_REQ_PEND) { |
| 65 | hdev->req_result = result; |
| 66 | hdev->req_status = HCI_REQ_DONE; |
| 67 | wake_up_interruptible(&hdev->req_wait_q); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void hci_req_cancel(struct hci_dev *hdev, int err) |
| 72 | { |
| 73 | BT_DBG("%s err 0x%2.2x", hdev->name, err); |
| 74 | |
| 75 | if (hdev->req_status == HCI_REQ_PEND) { |
| 76 | hdev->req_result = err; |
| 77 | hdev->req_status = HCI_REQ_CANCELED; |
| 78 | wake_up_interruptible(&hdev->req_wait_q); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* Execute request and wait for completion. */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 83 | static int __hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 84 | void (*func)(struct hci_request *req, |
| 85 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 86 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 88 | struct hci_request req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | DECLARE_WAITQUEUE(wait, current); |
| 90 | int err = 0; |
| 91 | |
| 92 | BT_DBG("%s start", hdev->name); |
| 93 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 94 | hci_req_init(&req, hdev); |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | hdev->req_status = HCI_REQ_PEND; |
| 97 | |
| 98 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 99 | set_current_state(TASK_INTERRUPTIBLE); |
| 100 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 101 | func(&req, opt); |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 102 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 103 | err = hci_req_run(&req, hci_req_sync_complete); |
| 104 | if (err < 0) { |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 105 | hdev->req_status = 0; |
| 106 | remove_wait_queue(&hdev->req_wait_q, &wait); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 107 | /* req_run will fail if the request did not add any |
| 108 | * commands to the queue, something that can happen when |
| 109 | * a request with conditionals doesn't trigger any |
| 110 | * commands to be sent. This is normal behavior and |
| 111 | * should not trigger an error return. |
| 112 | */ |
| 113 | return 0; |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | schedule_timeout(timeout); |
| 117 | |
| 118 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 119 | |
| 120 | if (signal_pending(current)) |
| 121 | return -EINTR; |
| 122 | |
| 123 | switch (hdev->req_status) { |
| 124 | case HCI_REQ_DONE: |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 125 | err = -bt_to_errno(hdev->req_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | break; |
| 127 | |
| 128 | case HCI_REQ_CANCELED: |
| 129 | err = -hdev->req_result; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | err = -ETIMEDOUT; |
| 134 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 137 | hdev->req_status = hdev->req_result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | BT_DBG("%s end: err %d", hdev->name, err); |
| 140 | |
| 141 | return err; |
| 142 | } |
| 143 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 144 | static int hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 145 | void (*req)(struct hci_request *req, |
| 146 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 147 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
| 149 | int ret; |
| 150 | |
Marcel Holtmann | 7c6a329 | 2008-09-12 03:11:54 +0200 | [diff] [blame] | 151 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 152 | return -ENETDOWN; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* Serialize all requests */ |
| 155 | hci_req_lock(hdev); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 156 | ret = __hci_req_sync(hdev, req, opt, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | hci_req_unlock(hdev); |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 162 | static void hci_reset_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 164 | BT_DBG("%s %ld", req->hdev->name, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* Reset device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 167 | set_bit(HCI_RESET, &req->hdev->flags); |
| 168 | hci_req_add(req, HCI_OP_RESET, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 171 | static void bredr_init(struct hci_request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 173 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* Read Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 176 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 178 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 179 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 180 | |
| 181 | /* Read BD Address */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 182 | hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 185 | static void amp_init(struct hci_request *req) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 186 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 187 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 188 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 189 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 190 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 191 | |
| 192 | /* Read Local AMP Info */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 193 | hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
Andrei Emeltchenko | e71dfab | 2012-09-06 15:05:46 +0300 | [diff] [blame] | 194 | |
| 195 | /* Read Data Blk size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 196 | hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 199 | static void hci_init1_req(struct hci_request *req, unsigned long opt) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 200 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 201 | struct hci_dev *hdev = req->hdev; |
| 202 | struct hci_request init_req; |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 203 | struct sk_buff *skb; |
| 204 | |
| 205 | BT_DBG("%s %ld", hdev->name, opt); |
| 206 | |
| 207 | /* Driver initialization */ |
| 208 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 209 | hci_req_init(&init_req, hdev); |
| 210 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 211 | /* Special commands */ |
| 212 | while ((skb = skb_dequeue(&hdev->driver_init))) { |
| 213 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
| 214 | skb->dev = (void *) hdev; |
| 215 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 216 | if (skb_queue_empty(&init_req.cmd_q)) |
| 217 | bt_cb(skb)->req.start = true; |
| 218 | |
| 219 | skb_queue_tail(&init_req.cmd_q, skb); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 220 | } |
| 221 | skb_queue_purge(&hdev->driver_init); |
| 222 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 223 | hci_req_run(&init_req, NULL); |
| 224 | |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 225 | /* Reset */ |
| 226 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 227 | hci_reset_req(req, 0); |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 228 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 229 | switch (hdev->dev_type) { |
| 230 | case HCI_BREDR: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 231 | bredr_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 232 | break; |
| 233 | |
| 234 | case HCI_AMP: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 235 | amp_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 236 | break; |
| 237 | |
| 238 | default: |
| 239 | BT_ERR("Unknown device type %d", hdev->dev_type); |
| 240 | break; |
| 241 | } |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 244 | static void bredr_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 245 | { |
| 246 | struct hci_cp_delete_stored_link_key cp; |
| 247 | __le16 param; |
| 248 | __u8 flt_type; |
| 249 | |
| 250 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 251 | hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 252 | |
| 253 | /* Read Class of Device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 254 | hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 255 | |
| 256 | /* Read Local Name */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 257 | hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 258 | |
| 259 | /* Read Voice Setting */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 260 | hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 261 | |
| 262 | /* Clear Event Filters */ |
| 263 | flt_type = HCI_FLT_CLEAR_ALL; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 264 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 265 | |
| 266 | /* Connection accept timeout ~20 secs */ |
| 267 | param = __constant_cpu_to_le16(0x7d00); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 268 | hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 269 | |
| 270 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 271 | cp.delete_all = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 272 | hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 275 | static void le_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 276 | { |
| 277 | /* Read LE Buffer Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 278 | hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 279 | |
| 280 | /* Read LE Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 281 | hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 282 | |
| 283 | /* Read LE Advertising Channel TX Power */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 284 | hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 285 | |
| 286 | /* Read LE White List Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 287 | hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 288 | |
| 289 | /* Read LE Supported States */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 290 | hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static u8 hci_get_inquiry_mode(struct hci_dev *hdev) |
| 294 | { |
| 295 | if (lmp_ext_inq_capable(hdev)) |
| 296 | return 0x02; |
| 297 | |
| 298 | if (lmp_inq_rssi_capable(hdev)) |
| 299 | return 0x01; |
| 300 | |
| 301 | if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 && |
| 302 | hdev->lmp_subver == 0x0757) |
| 303 | return 0x01; |
| 304 | |
| 305 | if (hdev->manufacturer == 15) { |
| 306 | if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963) |
| 307 | return 0x01; |
| 308 | if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963) |
| 309 | return 0x01; |
| 310 | if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965) |
| 311 | return 0x01; |
| 312 | } |
| 313 | |
| 314 | if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 && |
| 315 | hdev->lmp_subver == 0x1805) |
| 316 | return 0x01; |
| 317 | |
| 318 | return 0x00; |
| 319 | } |
| 320 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 321 | static void hci_setup_inquiry_mode(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 322 | { |
| 323 | u8 mode; |
| 324 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 325 | mode = hci_get_inquiry_mode(req->hdev); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 326 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 327 | hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 330 | static void hci_setup_event_mask(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 331 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 332 | struct hci_dev *hdev = req->hdev; |
| 333 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 334 | /* The second byte is 0xff instead of 0x9f (two reserved bits |
| 335 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the |
| 336 | * command otherwise. |
| 337 | */ |
| 338 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 339 | |
| 340 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set |
| 341 | * any event mask for pre 1.2 devices. |
| 342 | */ |
| 343 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 344 | return; |
| 345 | |
| 346 | if (lmp_bredr_capable(hdev)) { |
| 347 | events[4] |= 0x01; /* Flow Specification Complete */ |
| 348 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 349 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ |
| 350 | events[5] |= 0x08; /* Synchronous Connection Complete */ |
| 351 | events[5] |= 0x10; /* Synchronous Connection Changed */ |
| 352 | } |
| 353 | |
| 354 | if (lmp_inq_rssi_capable(hdev)) |
| 355 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 356 | |
| 357 | if (lmp_sniffsubr_capable(hdev)) |
| 358 | events[5] |= 0x20; /* Sniff Subrating */ |
| 359 | |
| 360 | if (lmp_pause_enc_capable(hdev)) |
| 361 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
| 362 | |
| 363 | if (lmp_ext_inq_capable(hdev)) |
| 364 | events[5] |= 0x40; /* Extended Inquiry Result */ |
| 365 | |
| 366 | if (lmp_no_flush_capable(hdev)) |
| 367 | events[7] |= 0x01; /* Enhanced Flush Complete */ |
| 368 | |
| 369 | if (lmp_lsto_capable(hdev)) |
| 370 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ |
| 371 | |
| 372 | if (lmp_ssp_capable(hdev)) { |
| 373 | events[6] |= 0x01; /* IO Capability Request */ |
| 374 | events[6] |= 0x02; /* IO Capability Response */ |
| 375 | events[6] |= 0x04; /* User Confirmation Request */ |
| 376 | events[6] |= 0x08; /* User Passkey Request */ |
| 377 | events[6] |= 0x10; /* Remote OOB Data Request */ |
| 378 | events[6] |= 0x20; /* Simple Pairing Complete */ |
| 379 | events[7] |= 0x04; /* User Passkey Notification */ |
| 380 | events[7] |= 0x08; /* Keypress Notification */ |
| 381 | events[7] |= 0x10; /* Remote Host Supported |
| 382 | * Features Notification |
| 383 | */ |
| 384 | } |
| 385 | |
| 386 | if (lmp_le_capable(hdev)) |
| 387 | events[7] |= 0x20; /* LE Meta-Event */ |
| 388 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 389 | hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 390 | |
| 391 | if (lmp_le_capable(hdev)) { |
| 392 | memset(events, 0, sizeof(events)); |
| 393 | events[0] = 0x1f; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 394 | hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, |
| 395 | sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 399 | static void hci_init2_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 400 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 401 | struct hci_dev *hdev = req->hdev; |
| 402 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 403 | if (lmp_bredr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 404 | bredr_setup(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 405 | |
| 406 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 407 | le_setup(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 408 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 409 | hci_setup_event_mask(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 410 | |
| 411 | if (hdev->hci_ver > BLUETOOTH_VER_1_1) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 412 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 413 | |
| 414 | if (lmp_ssp_capable(hdev)) { |
| 415 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 416 | u8 mode = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 417 | hci_req_add(req, HCI_OP_WRITE_SSP_MODE, |
| 418 | sizeof(mode), &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 419 | } else { |
| 420 | struct hci_cp_write_eir cp; |
| 421 | |
| 422 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 423 | memset(&cp, 0, sizeof(cp)); |
| 424 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 425 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | if (lmp_inq_rssi_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 430 | hci_setup_inquiry_mode(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 431 | |
| 432 | if (lmp_inq_tx_pwr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 433 | hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 434 | |
| 435 | if (lmp_ext_feat_capable(hdev)) { |
| 436 | struct hci_cp_read_local_ext_features cp; |
| 437 | |
| 438 | cp.page = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 439 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 440 | sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) { |
| 444 | u8 enable = 1; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 445 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), |
| 446 | &enable); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 450 | static void hci_setup_link_policy(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 451 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 452 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 453 | struct hci_cp_write_def_link_policy cp; |
| 454 | u16 link_policy = 0; |
| 455 | |
| 456 | if (lmp_rswitch_capable(hdev)) |
| 457 | link_policy |= HCI_LP_RSWITCH; |
| 458 | if (lmp_hold_capable(hdev)) |
| 459 | link_policy |= HCI_LP_HOLD; |
| 460 | if (lmp_sniff_capable(hdev)) |
| 461 | link_policy |= HCI_LP_SNIFF; |
| 462 | if (lmp_park_capable(hdev)) |
| 463 | link_policy |= HCI_LP_PARK; |
| 464 | |
| 465 | cp.policy = cpu_to_le16(link_policy); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 466 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 469 | static void hci_set_le_support(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 470 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 471 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 472 | struct hci_cp_write_le_host_supported cp; |
| 473 | |
| 474 | memset(&cp, 0, sizeof(cp)); |
| 475 | |
| 476 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 477 | cp.le = 0x01; |
| 478 | cp.simul = lmp_le_br_capable(hdev); |
| 479 | } |
| 480 | |
| 481 | if (cp.le != lmp_host_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 482 | hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), |
| 483 | &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 486 | static void hci_init3_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 487 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 488 | struct hci_dev *hdev = req->hdev; |
| 489 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 490 | if (hdev->commands[5] & 0x10) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 491 | hci_setup_link_policy(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 492 | |
| 493 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 494 | hci_set_le_support(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int __hci_init(struct hci_dev *hdev) |
| 498 | { |
| 499 | int err; |
| 500 | |
| 501 | err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); |
| 502 | if (err < 0) |
| 503 | return err; |
| 504 | |
| 505 | /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode |
| 506 | * BR/EDR/LE type controllers. AMP controllers only need the |
| 507 | * first stage init. |
| 508 | */ |
| 509 | if (hdev->dev_type != HCI_BREDR) |
| 510 | return 0; |
| 511 | |
| 512 | err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); |
| 513 | if (err < 0) |
| 514 | return err; |
| 515 | |
| 516 | return __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); |
| 517 | } |
| 518 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 519 | static void hci_scan_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
| 521 | __u8 scan = opt; |
| 522 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 523 | BT_DBG("%s %x", req->hdev->name, scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | /* Inquiry and Page scans */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 526 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 529 | static void hci_auth_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
| 531 | __u8 auth = opt; |
| 532 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 533 | BT_DBG("%s %x", req->hdev->name, auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
| 535 | /* Authentication */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 536 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 539 | static void hci_encrypt_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
| 541 | __u8 encrypt = opt; |
| 542 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 543 | BT_DBG("%s %x", req->hdev->name, encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 545 | /* Encryption */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 546 | hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 549 | static void hci_linkpol_req(struct hci_request *req, unsigned long opt) |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 550 | { |
| 551 | __le16 policy = cpu_to_le16(opt); |
| 552 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 553 | BT_DBG("%s %x", req->hdev->name, policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 554 | |
| 555 | /* Default link policy */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 556 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 557 | } |
| 558 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 559 | /* Get HCI device by index. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | * Device is held on return. */ |
| 561 | struct hci_dev *hci_dev_get(int index) |
| 562 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 563 | struct hci_dev *hdev = NULL, *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | BT_DBG("%d", index); |
| 566 | |
| 567 | if (index < 0) |
| 568 | return NULL; |
| 569 | |
| 570 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 571 | list_for_each_entry(d, &hci_dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | if (d->id == index) { |
| 573 | hdev = hci_dev_hold(d); |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | read_unlock(&hci_dev_list_lock); |
| 578 | return hdev; |
| 579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | /* ---- Inquiry support ---- */ |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 582 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 583 | bool hci_discovery_active(struct hci_dev *hdev) |
| 584 | { |
| 585 | struct discovery_state *discov = &hdev->discovery; |
| 586 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 587 | switch (discov->state) { |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 588 | case DISCOVERY_FINDING: |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 589 | case DISCOVERY_RESOLVING: |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 590 | return true; |
| 591 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 592 | default: |
| 593 | return false; |
| 594 | } |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 597 | void hci_discovery_set_state(struct hci_dev *hdev, int state) |
| 598 | { |
| 599 | BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); |
| 600 | |
| 601 | if (hdev->discovery.state == state) |
| 602 | return; |
| 603 | |
| 604 | switch (state) { |
| 605 | case DISCOVERY_STOPPED: |
Andre Guedes | 7b99b65 | 2012-02-13 15:41:02 -0300 | [diff] [blame] | 606 | if (hdev->discovery.state != DISCOVERY_STARTING) |
| 607 | mgmt_discovering(hdev, 0); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 608 | break; |
| 609 | case DISCOVERY_STARTING: |
| 610 | break; |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 611 | case DISCOVERY_FINDING: |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 612 | mgmt_discovering(hdev, 1); |
| 613 | break; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 614 | case DISCOVERY_RESOLVING: |
| 615 | break; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 616 | case DISCOVERY_STOPPING: |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | hdev->discovery.state = state; |
| 621 | } |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | static void inquiry_cache_flush(struct hci_dev *hdev) |
| 624 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 625 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 626 | struct inquiry_entry *p, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 628 | list_for_each_entry_safe(p, n, &cache->all, all) { |
| 629 | list_del(&p->all); |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 630 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 632 | |
| 633 | INIT_LIST_HEAD(&cache->unknown); |
| 634 | INIT_LIST_HEAD(&cache->resolve); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 637 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, |
| 638 | bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 640 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | struct inquiry_entry *e; |
| 642 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 643 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 645 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 647 | return e; |
| 648 | } |
| 649 | |
| 650 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 653 | struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 654 | bdaddr_t *bdaddr) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 655 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 656 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 657 | struct inquiry_entry *e; |
| 658 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 659 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 660 | |
| 661 | list_for_each_entry(e, &cache->unknown, list) { |
| 662 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 663 | return e; |
| 664 | } |
| 665 | |
| 666 | return NULL; |
| 667 | } |
| 668 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 669 | struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 670 | bdaddr_t *bdaddr, |
| 671 | int state) |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 672 | { |
| 673 | struct discovery_state *cache = &hdev->discovery; |
| 674 | struct inquiry_entry *e; |
| 675 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 676 | BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 677 | |
| 678 | list_for_each_entry(e, &cache->resolve, list) { |
| 679 | if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) |
| 680 | return e; |
| 681 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 682 | return e; |
| 683 | } |
| 684 | |
| 685 | return NULL; |
| 686 | } |
| 687 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 688 | void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 689 | struct inquiry_entry *ie) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 690 | { |
| 691 | struct discovery_state *cache = &hdev->discovery; |
| 692 | struct list_head *pos = &cache->resolve; |
| 693 | struct inquiry_entry *p; |
| 694 | |
| 695 | list_del(&ie->list); |
| 696 | |
| 697 | list_for_each_entry(p, &cache->resolve, list) { |
| 698 | if (p->name_state != NAME_PENDING && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 699 | abs(p->data.rssi) >= abs(ie->data.rssi)) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 700 | break; |
| 701 | pos = &p->list; |
| 702 | } |
| 703 | |
| 704 | list_add(&ie->list, pos); |
| 705 | } |
| 706 | |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 707 | bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 708 | bool name_known, bool *ssp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 710 | struct discovery_state *cache = &hdev->discovery; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 711 | struct inquiry_entry *ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 713 | BT_DBG("cache %p, %pMR", cache, &data->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Szymon Janc | 2b2fec4 | 2012-11-20 11:38:54 +0100 | [diff] [blame] | 715 | hci_remove_remote_oob_data(hdev, &data->bdaddr); |
| 716 | |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 717 | if (ssp) |
| 718 | *ssp = data->ssp_mode; |
| 719 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 720 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 721 | if (ie) { |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 722 | if (ie->data.ssp_mode && ssp) |
| 723 | *ssp = true; |
| 724 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 725 | if (ie->name_state == NAME_NEEDED && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 726 | data->rssi != ie->data.rssi) { |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 727 | ie->data.rssi = data->rssi; |
| 728 | hci_inquiry_cache_update_resolve(hdev, ie); |
| 729 | } |
| 730 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 731 | goto update; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 732 | } |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 733 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 734 | /* Entry not in the cache. Add new one. */ |
| 735 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
| 736 | if (!ie) |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 737 | return false; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 738 | |
| 739 | list_add(&ie->all, &cache->all); |
| 740 | |
| 741 | if (name_known) { |
| 742 | ie->name_state = NAME_KNOWN; |
| 743 | } else { |
| 744 | ie->name_state = NAME_NOT_KNOWN; |
| 745 | list_add(&ie->list, &cache->unknown); |
| 746 | } |
| 747 | |
| 748 | update: |
| 749 | if (name_known && ie->name_state != NAME_KNOWN && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 750 | ie->name_state != NAME_PENDING) { |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 751 | ie->name_state = NAME_KNOWN; |
| 752 | list_del(&ie->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 755 | memcpy(&ie->data, data, sizeof(*data)); |
| 756 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | cache->timestamp = jiffies; |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 758 | |
| 759 | if (ie->name_state == NAME_NOT_KNOWN) |
| 760 | return false; |
| 761 | |
| 762 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 766 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 767 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 769 | struct inquiry_entry *e; |
| 770 | int copied = 0; |
| 771 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 772 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | struct inquiry_data *data = &e->data; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 774 | |
| 775 | if (copied >= num) |
| 776 | break; |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | bacpy(&info->bdaddr, &data->bdaddr); |
| 779 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 780 | info->pscan_period_mode = data->pscan_period_mode; |
| 781 | info->pscan_mode = data->pscan_mode; |
| 782 | memcpy(info->dev_class, data->dev_class, 3); |
| 783 | info->clock_offset = data->clock_offset; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | info++; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 786 | copied++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | BT_DBG("cache %p, copied %d", cache, copied); |
| 790 | return copied; |
| 791 | } |
| 792 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 793 | static void hci_inq_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
| 795 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 796 | struct hci_dev *hdev = req->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | struct hci_cp_inquiry cp; |
| 798 | |
| 799 | BT_DBG("%s", hdev->name); |
| 800 | |
| 801 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 802 | return; |
| 803 | |
| 804 | /* Start Inquiry */ |
| 805 | memcpy(&cp.lap, &ir->lap, 3); |
| 806 | cp.length = ir->length; |
| 807 | cp.num_rsp = ir->num_rsp; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 808 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | int hci_inquiry(void __user *arg) |
| 812 | { |
| 813 | __u8 __user *ptr = arg; |
| 814 | struct hci_inquiry_req ir; |
| 815 | struct hci_dev *hdev; |
| 816 | int err = 0, do_inquiry = 0, max_rsp; |
| 817 | long timeo; |
| 818 | __u8 *buf; |
| 819 | |
| 820 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 821 | return -EFAULT; |
| 822 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 823 | hdev = hci_dev_get(ir.dev_id); |
| 824 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | return -ENODEV; |
| 826 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 827 | hci_dev_lock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 828 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 829 | inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | inquiry_cache_flush(hdev); |
| 831 | do_inquiry = 1; |
| 832 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 833 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 835 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 836 | |
| 837 | if (do_inquiry) { |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 838 | err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, |
| 839 | timeo); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 840 | if (err < 0) |
| 841 | goto done; |
| 842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 844 | /* for unlimited number of responses we will use buffer with |
| 845 | * 255 entries |
| 846 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 848 | |
| 849 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 850 | * copy it to the user space. |
| 851 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 852 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 853 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | err = -ENOMEM; |
| 855 | goto done; |
| 856 | } |
| 857 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 858 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 860 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 863 | |
| 864 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 865 | ptr += sizeof(ir); |
| 866 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 867 | ir.num_rsp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 869 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | err = -EFAULT; |
| 871 | |
| 872 | kfree(buf); |
| 873 | |
| 874 | done: |
| 875 | hci_dev_put(hdev); |
| 876 | return err; |
| 877 | } |
| 878 | |
Johan Hedberg | 3f0f524 | 2012-11-08 01:23:00 +0100 | [diff] [blame] | 879 | static u8 create_ad(struct hci_dev *hdev, u8 *ptr) |
| 880 | { |
| 881 | u8 ad_len = 0, flags = 0; |
| 882 | size_t name_len; |
| 883 | |
| 884 | if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags)) |
| 885 | flags |= LE_AD_GENERAL; |
| 886 | |
| 887 | if (!lmp_bredr_capable(hdev)) |
| 888 | flags |= LE_AD_NO_BREDR; |
| 889 | |
| 890 | if (lmp_le_br_capable(hdev)) |
| 891 | flags |= LE_AD_SIM_LE_BREDR_CTRL; |
| 892 | |
| 893 | if (lmp_host_le_br_capable(hdev)) |
| 894 | flags |= LE_AD_SIM_LE_BREDR_HOST; |
| 895 | |
| 896 | if (flags) { |
| 897 | BT_DBG("adv flags 0x%02x", flags); |
| 898 | |
| 899 | ptr[0] = 2; |
| 900 | ptr[1] = EIR_FLAGS; |
| 901 | ptr[2] = flags; |
| 902 | |
| 903 | ad_len += 3; |
| 904 | ptr += 3; |
| 905 | } |
| 906 | |
| 907 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 908 | ptr[0] = 2; |
| 909 | ptr[1] = EIR_TX_POWER; |
| 910 | ptr[2] = (u8) hdev->adv_tx_power; |
| 911 | |
| 912 | ad_len += 3; |
| 913 | ptr += 3; |
| 914 | } |
| 915 | |
| 916 | name_len = strlen(hdev->dev_name); |
| 917 | if (name_len > 0) { |
| 918 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 919 | |
| 920 | if (name_len > max_len) { |
| 921 | name_len = max_len; |
| 922 | ptr[1] = EIR_NAME_SHORT; |
| 923 | } else |
| 924 | ptr[1] = EIR_NAME_COMPLETE; |
| 925 | |
| 926 | ptr[0] = name_len + 1; |
| 927 | |
| 928 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 929 | |
| 930 | ad_len += (name_len + 2); |
| 931 | ptr += (name_len + 2); |
| 932 | } |
| 933 | |
| 934 | return ad_len; |
| 935 | } |
| 936 | |
| 937 | int hci_update_ad(struct hci_dev *hdev) |
| 938 | { |
| 939 | struct hci_cp_le_set_adv_data cp; |
| 940 | u8 len; |
| 941 | int err; |
| 942 | |
| 943 | hci_dev_lock(hdev); |
| 944 | |
| 945 | if (!lmp_le_capable(hdev)) { |
| 946 | err = -EINVAL; |
| 947 | goto unlock; |
| 948 | } |
| 949 | |
| 950 | memset(&cp, 0, sizeof(cp)); |
| 951 | |
| 952 | len = create_ad(hdev, cp.data); |
| 953 | |
| 954 | if (hdev->adv_data_len == len && |
| 955 | memcmp(cp.data, hdev->adv_data, len) == 0) { |
| 956 | err = 0; |
| 957 | goto unlock; |
| 958 | } |
| 959 | |
| 960 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 961 | hdev->adv_data_len = len; |
| 962 | |
| 963 | cp.length = len; |
| 964 | err = hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 965 | |
| 966 | unlock: |
| 967 | hci_dev_unlock(hdev); |
| 968 | |
| 969 | return err; |
| 970 | } |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /* ---- HCI ioctl helpers ---- */ |
| 973 | |
| 974 | int hci_dev_open(__u16 dev) |
| 975 | { |
| 976 | struct hci_dev *hdev; |
| 977 | int ret = 0; |
| 978 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 979 | hdev = hci_dev_get(dev); |
| 980 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | return -ENODEV; |
| 982 | |
| 983 | BT_DBG("%s %p", hdev->name, hdev); |
| 984 | |
| 985 | hci_req_lock(hdev); |
| 986 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 987 | if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { |
| 988 | ret = -ENODEV; |
| 989 | goto done; |
| 990 | } |
| 991 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 992 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) { |
| 993 | ret = -ERFKILL; |
| 994 | goto done; |
| 995 | } |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 998 | ret = -EALREADY; |
| 999 | goto done; |
| 1000 | } |
| 1001 | |
| 1002 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 1003 | set_bit(HCI_RAW, &hdev->flags); |
| 1004 | |
Andrei Emeltchenko | 07e3b94 | 2011-11-11 17:02:15 +0200 | [diff] [blame] | 1005 | /* Treat all non BR/EDR controllers as raw devices if |
| 1006 | enable_hs is not set */ |
| 1007 | if (hdev->dev_type != HCI_BREDR && !enable_hs) |
Marcel Holtmann | 943da25 | 2010-02-13 02:28:41 +0100 | [diff] [blame] | 1008 | set_bit(HCI_RAW, &hdev->flags); |
| 1009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (hdev->open(hdev)) { |
| 1011 | ret = -EIO; |
| 1012 | goto done; |
| 1013 | } |
| 1014 | |
| 1015 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 1016 | atomic_set(&hdev->cmd_cnt, 1); |
| 1017 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1018 | ret = __hci_init(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | clear_bit(HCI_INIT, &hdev->flags); |
| 1020 | } |
| 1021 | |
| 1022 | if (!ret) { |
| 1023 | hci_dev_hold(hdev); |
| 1024 | set_bit(HCI_UP, &hdev->flags); |
| 1025 | hci_notify(hdev, HCI_DEV_UP); |
Johan Hedberg | 3f0f524 | 2012-11-08 01:23:00 +0100 | [diff] [blame] | 1026 | hci_update_ad(hdev); |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 1027 | if (!test_bit(HCI_SETUP, &hdev->dev_flags) && |
| 1028 | mgmt_valid_hdev(hdev)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1029 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1030 | mgmt_powered(hdev, 1); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1031 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1032 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1033 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* Init failed, cleanup */ |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1035 | flush_work(&hdev->tx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1036 | flush_work(&hdev->cmd_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1037 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
| 1039 | skb_queue_purge(&hdev->cmd_q); |
| 1040 | skb_queue_purge(&hdev->rx_q); |
| 1041 | |
| 1042 | if (hdev->flush) |
| 1043 | hdev->flush(hdev); |
| 1044 | |
| 1045 | if (hdev->sent_cmd) { |
| 1046 | kfree_skb(hdev->sent_cmd); |
| 1047 | hdev->sent_cmd = NULL; |
| 1048 | } |
| 1049 | |
| 1050 | hdev->close(hdev); |
| 1051 | hdev->flags = 0; |
| 1052 | } |
| 1053 | |
| 1054 | done: |
| 1055 | hci_req_unlock(hdev); |
| 1056 | hci_dev_put(hdev); |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | static int hci_dev_do_close(struct hci_dev *hdev) |
| 1061 | { |
| 1062 | BT_DBG("%s %p", hdev->name, hdev); |
| 1063 | |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1064 | cancel_work_sync(&hdev->le_scan); |
| 1065 | |
Vinicius Costa Gomes | 78c04c0 | 2012-09-14 16:34:46 -0300 | [diff] [blame] | 1066 | cancel_delayed_work(&hdev->power_off); |
| 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | hci_req_cancel(hdev, ENODEV); |
| 1069 | hci_req_lock(hdev); |
| 1070 | |
| 1071 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 1072 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | hci_req_unlock(hdev); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1077 | /* Flush RX and TX works */ |
| 1078 | flush_work(&hdev->tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1079 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1081 | if (hdev->discov_timeout > 0) { |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 1082 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1083 | hdev->discov_timeout = 0; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1084 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1087 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1088 | cancel_delayed_work(&hdev->service_cache); |
| 1089 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1090 | cancel_delayed_work_sync(&hdev->le_scan_disable); |
| 1091 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1092 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | inquiry_cache_flush(hdev); |
| 1094 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1095 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | hci_notify(hdev, HCI_DEV_DOWN); |
| 1098 | |
| 1099 | if (hdev->flush) |
| 1100 | hdev->flush(hdev); |
| 1101 | |
| 1102 | /* Reset device */ |
| 1103 | skb_queue_purge(&hdev->cmd_q); |
| 1104 | atomic_set(&hdev->cmd_cnt, 1); |
Johan Hedberg | 8af5946 | 2012-02-03 21:29:40 +0200 | [diff] [blame] | 1105 | if (!test_bit(HCI_RAW, &hdev->flags) && |
Szymon Janc | a6c511c | 2012-05-23 12:35:46 +0200 | [diff] [blame] | 1106 | test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1108 | __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | clear_bit(HCI_INIT, &hdev->flags); |
| 1110 | } |
| 1111 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1112 | /* flush cmd work */ |
| 1113 | flush_work(&hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | /* Drop queues */ |
| 1116 | skb_queue_purge(&hdev->rx_q); |
| 1117 | skb_queue_purge(&hdev->cmd_q); |
| 1118 | skb_queue_purge(&hdev->raw_q); |
| 1119 | |
| 1120 | /* Drop last sent command */ |
| 1121 | if (hdev->sent_cmd) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 1122 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | kfree_skb(hdev->sent_cmd); |
| 1124 | hdev->sent_cmd = NULL; |
| 1125 | } |
| 1126 | |
| 1127 | /* After this point our queues are empty |
| 1128 | * and no tasks are scheduled. */ |
| 1129 | hdev->close(hdev); |
| 1130 | |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 1131 | if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags) && |
| 1132 | mgmt_valid_hdev(hdev)) { |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1133 | hci_dev_lock(hdev); |
| 1134 | mgmt_powered(hdev, 0); |
| 1135 | hci_dev_unlock(hdev); |
| 1136 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | /* Clear flags */ |
| 1139 | hdev->flags = 0; |
| 1140 | |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1141 | /* Controller radio is available but is currently powered down */ |
| 1142 | hdev->amp_status = 0; |
| 1143 | |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1144 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
Johan Hedberg | 09b3c3f | 2012-02-22 22:01:41 +0200 | [diff] [blame] | 1145 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | hci_req_unlock(hdev); |
| 1148 | |
| 1149 | hci_dev_put(hdev); |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | int hci_dev_close(__u16 dev) |
| 1154 | { |
| 1155 | struct hci_dev *hdev; |
| 1156 | int err; |
| 1157 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1158 | hdev = hci_dev_get(dev); |
| 1159 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | return -ENODEV; |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1161 | |
| 1162 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 1163 | cancel_delayed_work(&hdev->power_off); |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | err = hci_dev_do_close(hdev); |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | hci_dev_put(hdev); |
| 1168 | return err; |
| 1169 | } |
| 1170 | |
| 1171 | int hci_dev_reset(__u16 dev) |
| 1172 | { |
| 1173 | struct hci_dev *hdev; |
| 1174 | int ret = 0; |
| 1175 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1176 | hdev = hci_dev_get(dev); |
| 1177 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | return -ENODEV; |
| 1179 | |
| 1180 | hci_req_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
| 1182 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 1183 | goto done; |
| 1184 | |
| 1185 | /* Drop queues */ |
| 1186 | skb_queue_purge(&hdev->rx_q); |
| 1187 | skb_queue_purge(&hdev->cmd_q); |
| 1188 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1189 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | inquiry_cache_flush(hdev); |
| 1191 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1192 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
| 1194 | if (hdev->flush) |
| 1195 | hdev->flush(hdev); |
| 1196 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1197 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 1198 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | if (!test_bit(HCI_RAW, &hdev->flags)) |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1201 | ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | hci_req_unlock(hdev); |
| 1205 | hci_dev_put(hdev); |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | int hci_dev_reset_stat(__u16 dev) |
| 1210 | { |
| 1211 | struct hci_dev *hdev; |
| 1212 | int ret = 0; |
| 1213 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1214 | hdev = hci_dev_get(dev); |
| 1215 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | return -ENODEV; |
| 1217 | |
| 1218 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 1219 | |
| 1220 | hci_dev_put(hdev); |
| 1221 | |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 1226 | { |
| 1227 | struct hci_dev *hdev; |
| 1228 | struct hci_dev_req dr; |
| 1229 | int err = 0; |
| 1230 | |
| 1231 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 1232 | return -EFAULT; |
| 1233 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1234 | hdev = hci_dev_get(dr.dev_id); |
| 1235 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return -ENODEV; |
| 1237 | |
| 1238 | switch (cmd) { |
| 1239 | case HCISETAUTH: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1240 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 1241 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | break; |
| 1243 | |
| 1244 | case HCISETENCRYPT: |
| 1245 | if (!lmp_encrypt_capable(hdev)) { |
| 1246 | err = -EOPNOTSUPP; |
| 1247 | break; |
| 1248 | } |
| 1249 | |
| 1250 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 1251 | /* Auth must be enabled first */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1252 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 1253 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if (err) |
| 1255 | break; |
| 1256 | } |
| 1257 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1258 | err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, |
| 1259 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | break; |
| 1261 | |
| 1262 | case HCISETSCAN: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1263 | err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, |
| 1264 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | break; |
| 1266 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1267 | case HCISETLINKPOL: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1268 | err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, |
| 1269 | HCI_INIT_TIMEOUT); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1270 | break; |
| 1271 | |
| 1272 | case HCISETLINKMODE: |
| 1273 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 1274 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 1275 | break; |
| 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | case HCISETPTYPE: |
| 1278 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 1279 | break; |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1282 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1283 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | break; |
| 1285 | |
| 1286 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1287 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1288 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | break; |
| 1290 | |
| 1291 | default: |
| 1292 | err = -EINVAL; |
| 1293 | break; |
| 1294 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | hci_dev_put(hdev); |
| 1297 | return err; |
| 1298 | } |
| 1299 | |
| 1300 | int hci_get_dev_list(void __user *arg) |
| 1301 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1302 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | struct hci_dev_list_req *dl; |
| 1304 | struct hci_dev_req *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | int n = 0, size, err; |
| 1306 | __u16 dev_num; |
| 1307 | |
| 1308 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 1309 | return -EFAULT; |
| 1310 | |
| 1311 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 1312 | return -EINVAL; |
| 1313 | |
| 1314 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 1315 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1316 | dl = kzalloc(size, GFP_KERNEL); |
| 1317 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | return -ENOMEM; |
| 1319 | |
| 1320 | dr = dl->dev_req; |
| 1321 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1322 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1323 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1324 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 1325 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1326 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1327 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 1328 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | (dr + n)->dev_id = hdev->id; |
| 1331 | (dr + n)->dev_opt = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (++n >= dev_num) |
| 1334 | break; |
| 1335 | } |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1336 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | dl->dev_num = n; |
| 1339 | size = sizeof(*dl) + n * sizeof(*dr); |
| 1340 | |
| 1341 | err = copy_to_user(arg, dl, size); |
| 1342 | kfree(dl); |
| 1343 | |
| 1344 | return err ? -EFAULT : 0; |
| 1345 | } |
| 1346 | |
| 1347 | int hci_get_dev_info(void __user *arg) |
| 1348 | { |
| 1349 | struct hci_dev *hdev; |
| 1350 | struct hci_dev_info di; |
| 1351 | int err = 0; |
| 1352 | |
| 1353 | if (copy_from_user(&di, arg, sizeof(di))) |
| 1354 | return -EFAULT; |
| 1355 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1356 | hdev = hci_dev_get(di.dev_id); |
| 1357 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | return -ENODEV; |
| 1359 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1360 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1361 | cancel_delayed_work_sync(&hdev->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1362 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1363 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 1364 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | strcpy(di.name, hdev->name); |
| 1367 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 943da25 | 2010-02-13 02:28:41 +0100 | [diff] [blame] | 1368 | di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | di.flags = hdev->flags; |
| 1370 | di.pkt_type = hdev->pkt_type; |
Johan Hedberg | 572c7f8 | 2012-10-19 20:57:46 +0300 | [diff] [blame] | 1371 | if (lmp_bredr_capable(hdev)) { |
| 1372 | di.acl_mtu = hdev->acl_mtu; |
| 1373 | di.acl_pkts = hdev->acl_pkts; |
| 1374 | di.sco_mtu = hdev->sco_mtu; |
| 1375 | di.sco_pkts = hdev->sco_pkts; |
| 1376 | } else { |
| 1377 | di.acl_mtu = hdev->le_mtu; |
| 1378 | di.acl_pkts = hdev->le_pkts; |
| 1379 | di.sco_mtu = 0; |
| 1380 | di.sco_pkts = 0; |
| 1381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | di.link_policy = hdev->link_policy; |
| 1383 | di.link_mode = hdev->link_mode; |
| 1384 | |
| 1385 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 1386 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 1387 | |
| 1388 | if (copy_to_user(arg, &di, sizeof(di))) |
| 1389 | err = -EFAULT; |
| 1390 | |
| 1391 | hci_dev_put(hdev); |
| 1392 | |
| 1393 | return err; |
| 1394 | } |
| 1395 | |
| 1396 | /* ---- Interface to HCI drivers ---- */ |
| 1397 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1398 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 1399 | { |
| 1400 | struct hci_dev *hdev = data; |
| 1401 | |
| 1402 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 1403 | |
| 1404 | if (!blocked) |
| 1405 | return 0; |
| 1406 | |
| 1407 | hci_dev_do_close(hdev); |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static const struct rfkill_ops hci_rfkill_ops = { |
| 1413 | .set_block = hci_rfkill_set_block, |
| 1414 | }; |
| 1415 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1416 | static void hci_power_on(struct work_struct *work) |
| 1417 | { |
| 1418 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
| 1419 | |
| 1420 | BT_DBG("%s", hdev->name); |
| 1421 | |
| 1422 | if (hci_dev_open(hdev->id) < 0) |
| 1423 | return; |
| 1424 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1425 | if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1426 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1427 | HCI_AUTO_OFF_TIMEOUT); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1428 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1429 | if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1430 | mgmt_index_added(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | static void hci_power_off(struct work_struct *work) |
| 1434 | { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 1435 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1436 | power_off.work); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1437 | |
| 1438 | BT_DBG("%s", hdev->name); |
| 1439 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1440 | hci_dev_do_close(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1443 | static void hci_discov_off(struct work_struct *work) |
| 1444 | { |
| 1445 | struct hci_dev *hdev; |
| 1446 | u8 scan = SCAN_PAGE; |
| 1447 | |
| 1448 | hdev = container_of(work, struct hci_dev, discov_off.work); |
| 1449 | |
| 1450 | BT_DBG("%s", hdev->name); |
| 1451 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1452 | hci_dev_lock(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1453 | |
| 1454 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
| 1455 | |
| 1456 | hdev->discov_timeout = 0; |
| 1457 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1458 | hci_dev_unlock(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1461 | int hci_uuids_clear(struct hci_dev *hdev) |
| 1462 | { |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 1463 | struct bt_uuid *uuid, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1464 | |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 1465 | list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { |
| 1466 | list_del(&uuid->list); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1467 | kfree(uuid); |
| 1468 | } |
| 1469 | |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1473 | int hci_link_keys_clear(struct hci_dev *hdev) |
| 1474 | { |
| 1475 | struct list_head *p, *n; |
| 1476 | |
| 1477 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 1478 | struct link_key *key; |
| 1479 | |
| 1480 | key = list_entry(p, struct link_key, list); |
| 1481 | |
| 1482 | list_del(p); |
| 1483 | kfree(key); |
| 1484 | } |
| 1485 | |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 1489 | int hci_smp_ltks_clear(struct hci_dev *hdev) |
| 1490 | { |
| 1491 | struct smp_ltk *k, *tmp; |
| 1492 | |
| 1493 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 1494 | list_del(&k->list); |
| 1495 | kfree(k); |
| 1496 | } |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1501 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1502 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1503 | struct link_key *k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1504 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1505 | list_for_each_entry(k, &hdev->link_keys, list) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1506 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
| 1507 | return k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1508 | |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1512 | static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1513 | u8 key_type, u8 old_key_type) |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1514 | { |
| 1515 | /* Legacy key */ |
| 1516 | if (key_type < 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1517 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1518 | |
| 1519 | /* Debug keys are insecure so don't store them persistently */ |
| 1520 | if (key_type == HCI_LK_DEBUG_COMBINATION) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1521 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1522 | |
| 1523 | /* Changed combination key and there's no previous one */ |
| 1524 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1525 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1526 | |
| 1527 | /* Security mode 3 case */ |
| 1528 | if (!conn) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1529 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1530 | |
| 1531 | /* Neither local nor remote side had no-bonding as requirement */ |
| 1532 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1533 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1534 | |
| 1535 | /* Local side had dedicated bonding as requirement */ |
| 1536 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1537 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1538 | |
| 1539 | /* Remote side had dedicated bonding as requirement */ |
| 1540 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1541 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1542 | |
| 1543 | /* If none of the above criteria match, then don't store the key |
| 1544 | * persistently */ |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1545 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1548 | struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1549 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1550 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1551 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1552 | list_for_each_entry(k, &hdev->long_term_keys, list) { |
| 1553 | if (k->ediv != ediv || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1554 | memcmp(rand, k->rand, sizeof(k->rand))) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1555 | continue; |
| 1556 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1557 | return k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | return NULL; |
| 1561 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1562 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1563 | struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1564 | u8 addr_type) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1565 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1566 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1567 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1568 | list_for_each_entry(k, &hdev->long_term_keys, list) |
| 1569 | if (addr_type == k->bdaddr_type && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1570 | bacmp(bdaddr, &k->bdaddr) == 0) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1571 | return k; |
| 1572 | |
| 1573 | return NULL; |
| 1574 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1575 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1576 | int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1577 | bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1578 | { |
| 1579 | struct link_key *key, *old_key; |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 1580 | u8 old_key_type; |
| 1581 | bool persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1582 | |
| 1583 | old_key = hci_find_link_key(hdev, bdaddr); |
| 1584 | if (old_key) { |
| 1585 | old_key_type = old_key->type; |
| 1586 | key = old_key; |
| 1587 | } else { |
Johan Hedberg | 12adcf3 | 2011-04-28 11:29:00 -0700 | [diff] [blame] | 1588 | old_key_type = conn ? conn->key_type : 0xff; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1589 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
| 1590 | if (!key) |
| 1591 | return -ENOMEM; |
| 1592 | list_add(&key->list, &hdev->link_keys); |
| 1593 | } |
| 1594 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1595 | BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1596 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1597 | /* Some buggy controller combinations generate a changed |
| 1598 | * combination key for legacy pairing even when there's no |
| 1599 | * previous key */ |
| 1600 | if (type == HCI_LK_CHANGED_COMBINATION && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1601 | (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1602 | type = HCI_LK_COMBINATION; |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 1603 | if (conn) |
| 1604 | conn->key_type = type; |
| 1605 | } |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 1606 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1607 | bacpy(&key->bdaddr, bdaddr); |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 1608 | memcpy(key->val, val, HCI_LINK_KEY_SIZE); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1609 | key->pin_len = pin_len; |
| 1610 | |
Waldemar Rymarkiewicz | b6020ba | 2011-04-28 12:07:53 +0200 | [diff] [blame] | 1611 | if (type == HCI_LK_CHANGED_COMBINATION) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1612 | key->type = old_key_type; |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 1613 | else |
| 1614 | key->type = type; |
| 1615 | |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1616 | if (!new_key) |
| 1617 | return 0; |
| 1618 | |
| 1619 | persistent = hci_persistent_key(hdev, conn, type, old_key_type); |
| 1620 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 1621 | mgmt_new_link_key(hdev, key, persistent); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 1622 | |
Vishal Agarwal | 6ec5bca | 2012-04-16 14:44:44 +0530 | [diff] [blame] | 1623 | if (conn) |
| 1624 | conn->flush_key = !persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1625 | |
| 1626 | return 0; |
| 1627 | } |
| 1628 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1629 | int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type, |
Andrei Emeltchenko | 9a00665 | 2012-03-09 12:12:12 +0200 | [diff] [blame] | 1630 | int new_key, u8 authenticated, u8 tk[16], u8 enc_size, __le16 |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1631 | ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1632 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1633 | struct smp_ltk *key, *old_key; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1634 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1635 | if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK)) |
| 1636 | return 0; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1637 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1638 | old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type); |
| 1639 | if (old_key) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1640 | key = old_key; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1641 | else { |
| 1642 | key = kzalloc(sizeof(*key), GFP_ATOMIC); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1643 | if (!key) |
| 1644 | return -ENOMEM; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1645 | list_add(&key->list, &hdev->long_term_keys); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1646 | } |
| 1647 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1648 | bacpy(&key->bdaddr, bdaddr); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1649 | key->bdaddr_type = addr_type; |
| 1650 | memcpy(key->val, tk, sizeof(key->val)); |
| 1651 | key->authenticated = authenticated; |
| 1652 | key->ediv = ediv; |
| 1653 | key->enc_size = enc_size; |
| 1654 | key->type = type; |
| 1655 | memcpy(key->rand, rand, sizeof(key->rand)); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1656 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1657 | if (!new_key) |
| 1658 | return 0; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1659 | |
Vinicius Costa Gomes | 261cc5a | 2012-02-02 21:08:05 -0300 | [diff] [blame] | 1660 | if (type & HCI_SMP_LTK) |
| 1661 | mgmt_new_ltk(hdev, key, 1); |
| 1662 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 1663 | return 0; |
| 1664 | } |
| 1665 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1666 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1667 | { |
| 1668 | struct link_key *key; |
| 1669 | |
| 1670 | key = hci_find_link_key(hdev, bdaddr); |
| 1671 | if (!key) |
| 1672 | return -ENOENT; |
| 1673 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1674 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1675 | |
| 1676 | list_del(&key->list); |
| 1677 | kfree(key); |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 1682 | int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1683 | { |
| 1684 | struct smp_ltk *k, *tmp; |
| 1685 | |
| 1686 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 1687 | if (bacmp(bdaddr, &k->bdaddr)) |
| 1688 | continue; |
| 1689 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1690 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 1691 | |
| 1692 | list_del(&k->list); |
| 1693 | kfree(k); |
| 1694 | } |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1699 | /* HCI command timer function */ |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 1700 | static void hci_cmd_timeout(unsigned long arg) |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1701 | { |
| 1702 | struct hci_dev *hdev = (void *) arg; |
| 1703 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 1704 | if (hdev->sent_cmd) { |
| 1705 | struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; |
| 1706 | u16 opcode = __le16_to_cpu(sent->opcode); |
| 1707 | |
| 1708 | BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); |
| 1709 | } else { |
| 1710 | BT_ERR("%s command tx timeout", hdev->name); |
| 1711 | } |
| 1712 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1713 | atomic_set(&hdev->cmd_cnt, 1); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1714 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1717 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1718 | bdaddr_t *bdaddr) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1719 | { |
| 1720 | struct oob_data *data; |
| 1721 | |
| 1722 | list_for_each_entry(data, &hdev->remote_oob_data, list) |
| 1723 | if (bacmp(bdaddr, &data->bdaddr) == 0) |
| 1724 | return data; |
| 1725 | |
| 1726 | return NULL; |
| 1727 | } |
| 1728 | |
| 1729 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 1730 | { |
| 1731 | struct oob_data *data; |
| 1732 | |
| 1733 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 1734 | if (!data) |
| 1735 | return -ENOENT; |
| 1736 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1737 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1738 | |
| 1739 | list_del(&data->list); |
| 1740 | kfree(data); |
| 1741 | |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | int hci_remote_oob_data_clear(struct hci_dev *hdev) |
| 1746 | { |
| 1747 | struct oob_data *data, *n; |
| 1748 | |
| 1749 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 1750 | list_del(&data->list); |
| 1751 | kfree(data); |
| 1752 | } |
| 1753 | |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1758 | u8 *randomizer) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1759 | { |
| 1760 | struct oob_data *data; |
| 1761 | |
| 1762 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 1763 | |
| 1764 | if (!data) { |
| 1765 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 1766 | if (!data) |
| 1767 | return -ENOMEM; |
| 1768 | |
| 1769 | bacpy(&data->bdaddr, bdaddr); |
| 1770 | list_add(&data->list, &hdev->remote_oob_data); |
| 1771 | } |
| 1772 | |
| 1773 | memcpy(data->hash, hash, sizeof(data->hash)); |
| 1774 | memcpy(data->randomizer, randomizer, sizeof(data->randomizer)); |
| 1775 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1776 | BT_DBG("%s for %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1777 | |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1781 | struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1782 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1783 | struct bdaddr_list *b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1784 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1785 | list_for_each_entry(b, &hdev->blacklist, list) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1786 | if (bacmp(bdaddr, &b->bdaddr) == 0) |
| 1787 | return b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1788 | |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | |
| 1792 | int hci_blacklist_clear(struct hci_dev *hdev) |
| 1793 | { |
| 1794 | struct list_head *p, *n; |
| 1795 | |
| 1796 | list_for_each_safe(p, n, &hdev->blacklist) { |
| 1797 | struct bdaddr_list *b; |
| 1798 | |
| 1799 | b = list_entry(p, struct bdaddr_list, list); |
| 1800 | |
| 1801 | list_del(p); |
| 1802 | kfree(b); |
| 1803 | } |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 1808 | int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1809 | { |
| 1810 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1811 | |
| 1812 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
| 1813 | return -EBADF; |
| 1814 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1815 | if (hci_blacklist_lookup(hdev, bdaddr)) |
| 1816 | return -EEXIST; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1817 | |
| 1818 | entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1819 | if (!entry) |
| 1820 | return -ENOMEM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1821 | |
| 1822 | bacpy(&entry->bdaddr, bdaddr); |
| 1823 | |
| 1824 | list_add(&entry->list, &hdev->blacklist); |
| 1825 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 1826 | return mgmt_device_blocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1827 | } |
| 1828 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 1829 | int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1830 | { |
| 1831 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1832 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 1833 | if (bacmp(bdaddr, BDADDR_ANY) == 0) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1834 | return hci_blacklist_clear(hdev); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1835 | |
| 1836 | entry = hci_blacklist_lookup(hdev, bdaddr); |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 1837 | if (!entry) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1838 | return -ENOENT; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1839 | |
| 1840 | list_del(&entry->list); |
| 1841 | kfree(entry); |
| 1842 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 1843 | return mgmt_device_unblocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 1844 | } |
| 1845 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1846 | static void le_scan_param_req(struct hci_request *req, unsigned long opt) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1847 | { |
| 1848 | struct le_scan_params *param = (struct le_scan_params *) opt; |
| 1849 | struct hci_cp_le_set_scan_param cp; |
| 1850 | |
| 1851 | memset(&cp, 0, sizeof(cp)); |
| 1852 | cp.type = param->type; |
| 1853 | cp.interval = cpu_to_le16(param->interval); |
| 1854 | cp.window = cpu_to_le16(param->window); |
| 1855 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1856 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(cp), &cp); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1857 | } |
| 1858 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1859 | static void le_scan_enable_req(struct hci_request *req, unsigned long opt) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1860 | { |
| 1861 | struct hci_cp_le_set_scan_enable cp; |
| 1862 | |
| 1863 | memset(&cp, 0, sizeof(cp)); |
| 1864 | cp.enable = 1; |
Andre Guedes | 0431a43 | 2012-05-31 20:01:41 -0300 | [diff] [blame] | 1865 | cp.filter_dup = 1; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1866 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1867 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static int hci_do_le_scan(struct hci_dev *hdev, u8 type, u16 interval, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1871 | u16 window, int timeout) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1872 | { |
| 1873 | long timeo = msecs_to_jiffies(3000); |
| 1874 | struct le_scan_params param; |
| 1875 | int err; |
| 1876 | |
| 1877 | BT_DBG("%s", hdev->name); |
| 1878 | |
| 1879 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1880 | return -EINPROGRESS; |
| 1881 | |
| 1882 | param.type = type; |
| 1883 | param.interval = interval; |
| 1884 | param.window = window; |
| 1885 | |
| 1886 | hci_req_lock(hdev); |
| 1887 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1888 | err = __hci_req_sync(hdev, le_scan_param_req, (unsigned long) ¶m, |
| 1889 | timeo); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1890 | if (!err) |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1891 | err = __hci_req_sync(hdev, le_scan_enable_req, 0, timeo); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1892 | |
| 1893 | hci_req_unlock(hdev); |
| 1894 | |
| 1895 | if (err < 0) |
| 1896 | return err; |
| 1897 | |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 1898 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
| 1899 | msecs_to_jiffies(timeout)); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1900 | |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 1904 | int hci_cancel_le_scan(struct hci_dev *hdev) |
| 1905 | { |
| 1906 | BT_DBG("%s", hdev->name); |
| 1907 | |
| 1908 | if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1909 | return -EALREADY; |
| 1910 | |
| 1911 | if (cancel_delayed_work(&hdev->le_scan_disable)) { |
| 1912 | struct hci_cp_le_set_scan_enable cp; |
| 1913 | |
| 1914 | /* Send HCI command to disable LE Scan */ |
| 1915 | memset(&cp, 0, sizeof(cp)); |
| 1916 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
| 1917 | } |
| 1918 | |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1922 | static void le_scan_disable_work(struct work_struct *work) |
| 1923 | { |
| 1924 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1925 | le_scan_disable.work); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1926 | struct hci_cp_le_set_scan_enable cp; |
| 1927 | |
| 1928 | BT_DBG("%s", hdev->name); |
| 1929 | |
| 1930 | memset(&cp, 0, sizeof(cp)); |
| 1931 | |
| 1932 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
| 1933 | } |
| 1934 | |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1935 | static void le_scan_work(struct work_struct *work) |
| 1936 | { |
| 1937 | struct hci_dev *hdev = container_of(work, struct hci_dev, le_scan); |
| 1938 | struct le_scan_params *param = &hdev->le_scan_params; |
| 1939 | |
| 1940 | BT_DBG("%s", hdev->name); |
| 1941 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1942 | hci_do_le_scan(hdev, param->type, param->interval, param->window, |
| 1943 | param->timeout); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | int hci_le_scan(struct hci_dev *hdev, u8 type, u16 interval, u16 window, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1947 | int timeout) |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1948 | { |
| 1949 | struct le_scan_params *param = &hdev->le_scan_params; |
| 1950 | |
| 1951 | BT_DBG("%s", hdev->name); |
| 1952 | |
Johan Hedberg | f1550478 | 2012-10-24 21:12:03 +0300 | [diff] [blame] | 1953 | if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags)) |
| 1954 | return -ENOTSUPP; |
| 1955 | |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 1956 | if (work_busy(&hdev->le_scan)) |
| 1957 | return -EINPROGRESS; |
| 1958 | |
| 1959 | param->type = type; |
| 1960 | param->interval = interval; |
| 1961 | param->window = window; |
| 1962 | param->timeout = timeout; |
| 1963 | |
| 1964 | queue_work(system_long_wq, &hdev->le_scan); |
| 1965 | |
| 1966 | return 0; |
| 1967 | } |
| 1968 | |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 1969 | /* Alloc HCI device */ |
| 1970 | struct hci_dev *hci_alloc_dev(void) |
| 1971 | { |
| 1972 | struct hci_dev *hdev; |
| 1973 | |
| 1974 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
| 1975 | if (!hdev) |
| 1976 | return NULL; |
| 1977 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 1978 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
| 1979 | hdev->esco_type = (ESCO_HV1); |
| 1980 | hdev->link_mode = (HCI_LM_ACCEPT); |
| 1981 | hdev->io_capability = 0x03; /* No Input No Output */ |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 1982 | hdev->inq_tx_power = HCI_TX_POWER_INVALID; |
| 1983 | hdev->adv_tx_power = HCI_TX_POWER_INVALID; |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 1984 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 1985 | hdev->sniff_max_interval = 800; |
| 1986 | hdev->sniff_min_interval = 80; |
| 1987 | |
| 1988 | mutex_init(&hdev->lock); |
| 1989 | mutex_init(&hdev->req_lock); |
| 1990 | |
| 1991 | INIT_LIST_HEAD(&hdev->mgmt_pending); |
| 1992 | INIT_LIST_HEAD(&hdev->blacklist); |
| 1993 | INIT_LIST_HEAD(&hdev->uuids); |
| 1994 | INIT_LIST_HEAD(&hdev->link_keys); |
| 1995 | INIT_LIST_HEAD(&hdev->long_term_keys); |
| 1996 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
Andrei Emeltchenko | 6b536b5 | 2012-08-31 16:39:28 +0300 | [diff] [blame] | 1997 | INIT_LIST_HEAD(&hdev->conn_hash.list); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 1998 | |
| 1999 | INIT_WORK(&hdev->rx_work, hci_rx_work); |
| 2000 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); |
| 2001 | INIT_WORK(&hdev->tx_work, hci_tx_work); |
| 2002 | INIT_WORK(&hdev->power_on, hci_power_on); |
| 2003 | INIT_WORK(&hdev->le_scan, le_scan_work); |
| 2004 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2005 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); |
| 2006 | INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); |
| 2007 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); |
| 2008 | |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2009 | skb_queue_head_init(&hdev->driver_init); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2010 | skb_queue_head_init(&hdev->rx_q); |
| 2011 | skb_queue_head_init(&hdev->cmd_q); |
| 2012 | skb_queue_head_init(&hdev->raw_q); |
| 2013 | |
| 2014 | init_waitqueue_head(&hdev->req_wait_q); |
| 2015 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2016 | setup_timer(&hdev->cmd_timer, hci_cmd_timeout, (unsigned long) hdev); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2017 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2018 | hci_init_sysfs(hdev); |
| 2019 | discovery_init(hdev); |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2020 | |
| 2021 | return hdev; |
| 2022 | } |
| 2023 | EXPORT_SYMBOL(hci_alloc_dev); |
| 2024 | |
| 2025 | /* Free HCI device */ |
| 2026 | void hci_free_dev(struct hci_dev *hdev) |
| 2027 | { |
| 2028 | skb_queue_purge(&hdev->driver_init); |
| 2029 | |
| 2030 | /* will free via device release */ |
| 2031 | put_device(&hdev->dev); |
| 2032 | } |
| 2033 | EXPORT_SYMBOL(hci_free_dev); |
| 2034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | /* Register HCI device */ |
| 2036 | int hci_register_dev(struct hci_dev *hdev) |
| 2037 | { |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2038 | int id, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | |
David Herrmann | 010666a | 2012-01-07 15:47:07 +0100 | [diff] [blame] | 2040 | if (!hdev->open || !hdev->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | return -EINVAL; |
| 2042 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 2043 | /* Do not allow HCI_AMP devices to register at index 0, |
| 2044 | * so the index can be used as the AMP controller ID. |
| 2045 | */ |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2046 | switch (hdev->dev_type) { |
| 2047 | case HCI_BREDR: |
| 2048 | id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); |
| 2049 | break; |
| 2050 | case HCI_AMP: |
| 2051 | id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); |
| 2052 | break; |
| 2053 | default: |
| 2054 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2056 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2057 | if (id < 0) |
| 2058 | return id; |
| 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | sprintf(hdev->name, "hci%d", id); |
| 2061 | hdev->id = id; |
Andrei Emeltchenko | 2d8b3a1 | 2012-04-16 16:32:04 +0300 | [diff] [blame] | 2062 | |
| 2063 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
| 2064 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2065 | write_lock(&hci_dev_list_lock); |
| 2066 | list_add(&hdev->list, &hci_dev_list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2067 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Gustavo F. Padovan | 32845eb | 2011-12-17 17:47:30 -0200 | [diff] [blame] | 2069 | hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND | |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2070 | WQ_MEM_RECLAIM, 1); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2071 | if (!hdev->workqueue) { |
| 2072 | error = -ENOMEM; |
| 2073 | goto err; |
| 2074 | } |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2075 | |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2076 | hdev->req_workqueue = alloc_workqueue(hdev->name, |
| 2077 | WQ_HIGHPRI | WQ_UNBOUND | |
| 2078 | WQ_MEM_RECLAIM, 1); |
| 2079 | if (!hdev->req_workqueue) { |
| 2080 | destroy_workqueue(hdev->workqueue); |
| 2081 | error = -ENOMEM; |
| 2082 | goto err; |
| 2083 | } |
| 2084 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2085 | error = hci_add_sysfs(hdev); |
| 2086 | if (error < 0) |
| 2087 | goto err_wqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2089 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2090 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, |
| 2091 | hdev); |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2092 | if (hdev->rfkill) { |
| 2093 | if (rfkill_register(hdev->rfkill) < 0) { |
| 2094 | rfkill_destroy(hdev->rfkill); |
| 2095 | hdev->rfkill = NULL; |
| 2096 | } |
| 2097 | } |
| 2098 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2099 | set_bit(HCI_SETUP, &hdev->dev_flags); |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 2100 | |
| 2101 | if (hdev->dev_type != HCI_AMP) |
| 2102 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 2103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | hci_notify(hdev, HCI_DEV_REG); |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 2105 | hci_dev_hold(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 2107 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Marcel Holtmann | fbe96d6 | 2012-10-30 01:35:40 -0700 | [diff] [blame] | 2108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2110 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2111 | err_wqueue: |
| 2112 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2113 | destroy_workqueue(hdev->req_workqueue); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2114 | err: |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2115 | ida_simple_remove(&hci_index_ida, hdev->id); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2116 | write_lock(&hci_dev_list_lock); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2117 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2118 | write_unlock(&hci_dev_list_lock); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2119 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 2120 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | } |
| 2122 | EXPORT_SYMBOL(hci_register_dev); |
| 2123 | |
| 2124 | /* Unregister HCI device */ |
David Herrmann | 5973563 | 2011-10-26 10:43:19 +0200 | [diff] [blame] | 2125 | void hci_unregister_dev(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2127 | int i, id; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2128 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 2129 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 2131 | set_bit(HCI_UNREGISTER, &hdev->dev_flags); |
| 2132 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2133 | id = hdev->id; |
| 2134 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2135 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2137 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | |
| 2139 | hci_dev_do_close(hdev); |
| 2140 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 2141 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2142 | kfree_skb(hdev->reassembly[i]); |
| 2143 | |
Gustavo Padovan | b9b5ef1 | 2012-11-21 00:50:21 -0200 | [diff] [blame] | 2144 | cancel_work_sync(&hdev->power_on); |
| 2145 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2146 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2147 | !test_bit(HCI_SETUP, &hdev->dev_flags)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2148 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2149 | mgmt_index_removed(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2150 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2151 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2152 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2153 | /* mgmt_index_removed should take care of emptying the |
| 2154 | * pending list */ |
| 2155 | BUG_ON(!list_empty(&hdev->mgmt_pending)); |
| 2156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | hci_notify(hdev, HCI_DEV_UNREG); |
| 2158 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2159 | if (hdev->rfkill) { |
| 2160 | rfkill_unregister(hdev->rfkill); |
| 2161 | rfkill_destroy(hdev->rfkill); |
| 2162 | } |
| 2163 | |
David Herrmann | ce24297 | 2011-10-08 14:58:48 +0200 | [diff] [blame] | 2164 | hci_del_sysfs(hdev); |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 2165 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2166 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 2167 | destroy_workqueue(hdev->req_workqueue); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 2168 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2169 | hci_dev_lock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 2170 | hci_blacklist_clear(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2171 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2172 | hci_link_keys_clear(hdev); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2173 | hci_smp_ltks_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2174 | hci_remote_oob_data_clear(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2175 | hci_dev_unlock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 2176 | |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 2177 | hci_dev_put(hdev); |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 2178 | |
| 2179 | ida_simple_remove(&hci_index_ida, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | } |
| 2181 | EXPORT_SYMBOL(hci_unregister_dev); |
| 2182 | |
| 2183 | /* Suspend HCI device */ |
| 2184 | int hci_suspend_dev(struct hci_dev *hdev) |
| 2185 | { |
| 2186 | hci_notify(hdev, HCI_DEV_SUSPEND); |
| 2187 | return 0; |
| 2188 | } |
| 2189 | EXPORT_SYMBOL(hci_suspend_dev); |
| 2190 | |
| 2191 | /* Resume HCI device */ |
| 2192 | int hci_resume_dev(struct hci_dev *hdev) |
| 2193 | { |
| 2194 | hci_notify(hdev, HCI_DEV_RESUME); |
| 2195 | return 0; |
| 2196 | } |
| 2197 | EXPORT_SYMBOL(hci_resume_dev); |
| 2198 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2199 | /* Receive frame from HCI drivers */ |
| 2200 | int hci_recv_frame(struct sk_buff *skb) |
| 2201 | { |
| 2202 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 2203 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2204 | && !test_bit(HCI_INIT, &hdev->flags))) { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2205 | kfree_skb(skb); |
| 2206 | return -ENXIO; |
| 2207 | } |
| 2208 | |
Jorrit Schippers | d82603c | 2012-12-27 17:33:02 +0100 | [diff] [blame] | 2209 | /* Incoming skb */ |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2210 | bt_cb(skb)->incoming = 1; |
| 2211 | |
| 2212 | /* Time stamp */ |
| 2213 | __net_timestamp(skb); |
| 2214 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2215 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2216 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2217 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 2218 | return 0; |
| 2219 | } |
| 2220 | EXPORT_SYMBOL(hci_recv_frame); |
| 2221 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2222 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2223 | int count, __u8 index) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2224 | { |
| 2225 | int len = 0; |
| 2226 | int hlen = 0; |
| 2227 | int remain = count; |
| 2228 | struct sk_buff *skb; |
| 2229 | struct bt_skb_cb *scb; |
| 2230 | |
| 2231 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2232 | index >= NUM_REASSEMBLY) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2233 | return -EILSEQ; |
| 2234 | |
| 2235 | skb = hdev->reassembly[index]; |
| 2236 | |
| 2237 | if (!skb) { |
| 2238 | switch (type) { |
| 2239 | case HCI_ACLDATA_PKT: |
| 2240 | len = HCI_MAX_FRAME_SIZE; |
| 2241 | hlen = HCI_ACL_HDR_SIZE; |
| 2242 | break; |
| 2243 | case HCI_EVENT_PKT: |
| 2244 | len = HCI_MAX_EVENT_SIZE; |
| 2245 | hlen = HCI_EVENT_HDR_SIZE; |
| 2246 | break; |
| 2247 | case HCI_SCODATA_PKT: |
| 2248 | len = HCI_MAX_SCO_SIZE; |
| 2249 | hlen = HCI_SCO_HDR_SIZE; |
| 2250 | break; |
| 2251 | } |
| 2252 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2253 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2254 | if (!skb) |
| 2255 | return -ENOMEM; |
| 2256 | |
| 2257 | scb = (void *) skb->cb; |
| 2258 | scb->expect = hlen; |
| 2259 | scb->pkt_type = type; |
| 2260 | |
| 2261 | skb->dev = (void *) hdev; |
| 2262 | hdev->reassembly[index] = skb; |
| 2263 | } |
| 2264 | |
| 2265 | while (count) { |
| 2266 | scb = (void *) skb->cb; |
Dan Carpenter | 89bb46d | 2012-02-28 09:57:59 +0300 | [diff] [blame] | 2267 | len = min_t(uint, scb->expect, count); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 2268 | |
| 2269 | memcpy(skb_put(skb, len), data, len); |
| 2270 | |
| 2271 | count -= len; |
| 2272 | data += len; |
| 2273 | scb->expect -= len; |
| 2274 | remain = count; |
| 2275 | |
| 2276 | switch (type) { |
| 2277 | case HCI_EVENT_PKT: |
| 2278 | if (skb->len == HCI_EVENT_HDR_SIZE) { |
| 2279 | struct hci_event_hdr *h = hci_event_hdr(skb); |
| 2280 | scb->expect = h->plen; |
| 2281 | |
| 2282 | if (skb_tailroom(skb) < scb->expect) { |
| 2283 | kfree_skb(skb); |
| 2284 | hdev->reassembly[index] = NULL; |
| 2285 | return -ENOMEM; |
| 2286 | } |
| 2287 | } |
| 2288 | break; |
| 2289 | |
| 2290 | case HCI_ACLDATA_PKT: |
| 2291 | if (skb->len == HCI_ACL_HDR_SIZE) { |
| 2292 | struct hci_acl_hdr *h = hci_acl_hdr(skb); |
| 2293 | scb->expect = __le16_to_cpu(h->dlen); |
| 2294 | |
| 2295 | if (skb_tailroom(skb) < scb->expect) { |
| 2296 | kfree_skb(skb); |
| 2297 | hdev->reassembly[index] = NULL; |
| 2298 | return -ENOMEM; |
| 2299 | } |
| 2300 | } |
| 2301 | break; |
| 2302 | |
| 2303 | case HCI_SCODATA_PKT: |
| 2304 | if (skb->len == HCI_SCO_HDR_SIZE) { |
| 2305 | struct hci_sco_hdr *h = hci_sco_hdr(skb); |
| 2306 | scb->expect = h->dlen; |
| 2307 | |
| 2308 | if (skb_tailroom(skb) < scb->expect) { |
| 2309 | kfree_skb(skb); |
| 2310 | hdev->reassembly[index] = NULL; |
| 2311 | return -ENOMEM; |
| 2312 | } |
| 2313 | } |
| 2314 | break; |
| 2315 | } |
| 2316 | |
| 2317 | if (scb->expect == 0) { |
| 2318 | /* Complete frame */ |
| 2319 | |
| 2320 | bt_cb(skb)->pkt_type = type; |
| 2321 | hci_recv_frame(skb); |
| 2322 | |
| 2323 | hdev->reassembly[index] = NULL; |
| 2324 | return remain; |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | return remain; |
| 2329 | } |
| 2330 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2331 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
| 2332 | { |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2333 | int rem = 0; |
| 2334 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2335 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
| 2336 | return -EILSEQ; |
| 2337 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 2338 | while (count) { |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2339 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2340 | if (rem < 0) |
| 2341 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2342 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2343 | data += (count - rem); |
| 2344 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 2345 | } |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2346 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 2347 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 2348 | } |
| 2349 | EXPORT_SYMBOL(hci_recv_fragment); |
| 2350 | |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2351 | #define STREAM_REASSEMBLY 0 |
| 2352 | |
| 2353 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) |
| 2354 | { |
| 2355 | int type; |
| 2356 | int rem = 0; |
| 2357 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 2358 | while (count) { |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2359 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
| 2360 | |
| 2361 | if (!skb) { |
| 2362 | struct { char type; } *pkt; |
| 2363 | |
| 2364 | /* Start of the frame */ |
| 2365 | pkt = data; |
| 2366 | type = pkt->type; |
| 2367 | |
| 2368 | data++; |
| 2369 | count--; |
| 2370 | } else |
| 2371 | type = bt_cb(skb)->pkt_type; |
| 2372 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 2373 | rem = hci_reassembly(hdev, type, data, count, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2374 | STREAM_REASSEMBLY); |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2375 | if (rem < 0) |
| 2376 | return rem; |
| 2377 | |
| 2378 | data += (count - rem); |
| 2379 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 2380 | } |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 2381 | |
| 2382 | return rem; |
| 2383 | } |
| 2384 | EXPORT_SYMBOL(hci_recv_stream_fragment); |
| 2385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | /* ---- Interface to upper protocols ---- */ |
| 2387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | int hci_register_cb(struct hci_cb *cb) |
| 2389 | { |
| 2390 | BT_DBG("%p name %s", cb, cb->name); |
| 2391 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2392 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | list_add(&cb->list, &hci_cb_list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2394 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | |
| 2396 | return 0; |
| 2397 | } |
| 2398 | EXPORT_SYMBOL(hci_register_cb); |
| 2399 | |
| 2400 | int hci_unregister_cb(struct hci_cb *cb) |
| 2401 | { |
| 2402 | BT_DBG("%p name %s", cb, cb->name); |
| 2403 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2404 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | list_del(&cb->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2406 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | |
| 2408 | return 0; |
| 2409 | } |
| 2410 | EXPORT_SYMBOL(hci_unregister_cb); |
| 2411 | |
| 2412 | static int hci_send_frame(struct sk_buff *skb) |
| 2413 | { |
| 2414 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 2415 | |
| 2416 | if (!hdev) { |
| 2417 | kfree_skb(skb); |
| 2418 | return -ENODEV; |
| 2419 | } |
| 2420 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2421 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2423 | /* Time stamp */ |
| 2424 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 2426 | /* Send copy to monitor */ |
| 2427 | hci_send_to_monitor(hdev, skb); |
| 2428 | |
| 2429 | if (atomic_read(&hdev->promisc)) { |
| 2430 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 2431 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 2435 | skb_orphan(skb); |
| 2436 | |
| 2437 | return hdev->send(skb); |
| 2438 | } |
| 2439 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 2440 | void hci_req_init(struct hci_request *req, struct hci_dev *hdev) |
| 2441 | { |
| 2442 | skb_queue_head_init(&req->cmd_q); |
| 2443 | req->hdev = hdev; |
| 2444 | } |
| 2445 | |
| 2446 | int hci_req_run(struct hci_request *req, hci_req_complete_t complete) |
| 2447 | { |
| 2448 | struct hci_dev *hdev = req->hdev; |
| 2449 | struct sk_buff *skb; |
| 2450 | unsigned long flags; |
| 2451 | |
| 2452 | BT_DBG("length %u", skb_queue_len(&req->cmd_q)); |
| 2453 | |
| 2454 | /* Do not allow empty requests */ |
| 2455 | if (skb_queue_empty(&req->cmd_q)) |
| 2456 | return -EINVAL; |
| 2457 | |
| 2458 | skb = skb_peek_tail(&req->cmd_q); |
| 2459 | bt_cb(skb)->req.complete = complete; |
| 2460 | |
| 2461 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 2462 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); |
| 2463 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 2464 | |
| 2465 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 2466 | |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2470 | static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, |
| 2471 | u32 plen, void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | { |
| 2473 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 2474 | struct hci_command_hdr *hdr; |
| 2475 | struct sk_buff *skb; |
| 2476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2478 | if (!skb) |
| 2479 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | |
| 2481 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2482 | hdr->opcode = cpu_to_le16(opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | hdr->plen = plen; |
| 2484 | |
| 2485 | if (plen) |
| 2486 | memcpy(skb_put(skb, plen), param, plen); |
| 2487 | |
| 2488 | BT_DBG("skb len %d", skb->len); |
| 2489 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2490 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | skb->dev = (void *) hdev; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2492 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 2493 | return skb; |
| 2494 | } |
| 2495 | |
| 2496 | /* Send HCI command */ |
| 2497 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param) |
| 2498 | { |
| 2499 | struct sk_buff *skb; |
| 2500 | |
| 2501 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 2502 | |
| 2503 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 2504 | if (!skb) { |
| 2505 | BT_ERR("%s no memory for command", hdev->name); |
| 2506 | return -ENOMEM; |
| 2507 | } |
| 2508 | |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 2509 | /* Stand-alone HCI commands must be flaged as |
| 2510 | * single-command requests. |
| 2511 | */ |
| 2512 | bt_cb(skb)->req.start = true; |
| 2513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2515 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
| 2517 | return 0; |
| 2518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 2520 | /* Queue a command to an asynchronous HCI request */ |
| 2521 | int hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param) |
| 2522 | { |
| 2523 | struct hci_dev *hdev = req->hdev; |
| 2524 | struct sk_buff *skb; |
| 2525 | |
| 2526 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 2527 | |
| 2528 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 2529 | if (!skb) { |
| 2530 | BT_ERR("%s no memory for command", hdev->name); |
| 2531 | return -ENOMEM; |
| 2532 | } |
| 2533 | |
| 2534 | if (skb_queue_empty(&req->cmd_q)) |
| 2535 | bt_cb(skb)->req.start = true; |
| 2536 | |
| 2537 | skb_queue_tail(&req->cmd_q, skb); |
| 2538 | |
| 2539 | return 0; |
| 2540 | } |
| 2541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2543 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | { |
| 2545 | struct hci_command_hdr *hdr; |
| 2546 | |
| 2547 | if (!hdev->sent_cmd) |
| 2548 | return NULL; |
| 2549 | |
| 2550 | hdr = (void *) hdev->sent_cmd->data; |
| 2551 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 2552 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | return NULL; |
| 2554 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 2555 | BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
| 2557 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 2558 | } |
| 2559 | |
| 2560 | /* Send ACL data */ |
| 2561 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 2562 | { |
| 2563 | struct hci_acl_hdr *hdr; |
| 2564 | int len = skb->len; |
| 2565 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2566 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 2567 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2568 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2569 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 2570 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2573 | static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2574 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2576 | struct hci_conn *conn = chan->conn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | struct hci_dev *hdev = conn->hdev; |
| 2578 | struct sk_buff *list; |
| 2579 | |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 2580 | skb->len = skb_headlen(skb); |
| 2581 | skb->data_len = 0; |
| 2582 | |
| 2583 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | 204a6e5 | 2012-10-15 11:58:39 +0300 | [diff] [blame] | 2584 | |
| 2585 | switch (hdev->dev_type) { |
| 2586 | case HCI_BREDR: |
| 2587 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 2588 | break; |
| 2589 | case HCI_AMP: |
| 2590 | hci_add_acl_hdr(skb, chan->handle, flags); |
| 2591 | break; |
| 2592 | default: |
| 2593 | BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); |
| 2594 | return; |
| 2595 | } |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 2596 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2597 | list = skb_shinfo(skb)->frag_list; |
| 2598 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | /* Non fragmented */ |
| 2600 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 2601 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2602 | skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | } else { |
| 2604 | /* Fragmented */ |
| 2605 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 2606 | |
| 2607 | skb_shinfo(skb)->frag_list = NULL; |
| 2608 | |
| 2609 | /* Queue all fragments atomically */ |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 2610 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2612 | __skb_queue_tail(queue, skb); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 2613 | |
| 2614 | flags &= ~ACL_START; |
| 2615 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | do { |
| 2617 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2620 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 2621 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | |
| 2623 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 2624 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2625 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | } while (list); |
| 2627 | |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 2628 | spin_unlock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) |
| 2633 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2634 | struct hci_dev *hdev = chan->conn->hdev; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2635 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 2636 | BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2637 | |
| 2638 | skb->dev = (void *) hdev; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2639 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 2640 | hci_queue_acl(chan, &chan->data_q, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2642 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | |
| 2645 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 2646 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | { |
| 2648 | struct hci_dev *hdev = conn->hdev; |
| 2649 | struct hci_sco_hdr hdr; |
| 2650 | |
| 2651 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 2652 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2653 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | hdr.dlen = skb->len; |
| 2655 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 2656 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 2657 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2658 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | |
| 2660 | skb->dev = (void *) hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 2661 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 2662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | skb_queue_tail(&conn->data_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2664 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | |
| 2667 | /* ---- HCI TX task (outgoing data) ---- */ |
| 2668 | |
| 2669 | /* HCI Connection scheduler */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2670 | static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, |
| 2671 | int *quote) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | { |
| 2673 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2674 | struct hci_conn *conn = NULL, *c; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 2675 | unsigned int num = 0, min = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2677 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | * added and removed with TX task disabled. */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2679 | |
| 2680 | rcu_read_lock(); |
| 2681 | |
| 2682 | list_for_each_entry_rcu(c, &h->list, list) { |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2683 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 2685 | |
| 2686 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 2687 | continue; |
| 2688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | num++; |
| 2690 | |
| 2691 | if (c->sent < min) { |
| 2692 | min = c->sent; |
| 2693 | conn = c; |
| 2694 | } |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 2695 | |
| 2696 | if (hci_conn_num(hdev, type) == num) |
| 2697 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2700 | rcu_read_unlock(); |
| 2701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2703 | int cnt, q; |
| 2704 | |
| 2705 | switch (conn->type) { |
| 2706 | case ACL_LINK: |
| 2707 | cnt = hdev->acl_cnt; |
| 2708 | break; |
| 2709 | case SCO_LINK: |
| 2710 | case ESCO_LINK: |
| 2711 | cnt = hdev->sco_cnt; |
| 2712 | break; |
| 2713 | case LE_LINK: |
| 2714 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 2715 | break; |
| 2716 | default: |
| 2717 | cnt = 0; |
| 2718 | BT_ERR("Unknown link type"); |
| 2719 | } |
| 2720 | |
| 2721 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | *quote = q ? q : 1; |
| 2723 | } else |
| 2724 | *quote = 0; |
| 2725 | |
| 2726 | BT_DBG("conn %p quote %d", conn, *quote); |
| 2727 | return conn; |
| 2728 | } |
| 2729 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2730 | static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | { |
| 2732 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2733 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2735 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2737 | rcu_read_lock(); |
| 2738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | /* Kill stalled connections */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2740 | list_for_each_entry_rcu(c, &h->list, list) { |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2741 | if (c->type == type && c->sent) { |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2742 | BT_ERR("%s killing stalled connection %pMR", |
| 2743 | hdev->name, &c->dst); |
Andre Guedes | bed7174 | 2013-01-30 11:50:56 -0300 | [diff] [blame] | 2744 | hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | } |
| 2746 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2747 | |
| 2748 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2751 | static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
| 2752 | int *quote) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2753 | { |
| 2754 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 2755 | struct hci_chan *chan = NULL; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 2756 | unsigned int num = 0, min = ~0, cur_prio = 0; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2757 | struct hci_conn *conn; |
| 2758 | int cnt, q, conn_num = 0; |
| 2759 | |
| 2760 | BT_DBG("%s", hdev->name); |
| 2761 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2762 | rcu_read_lock(); |
| 2763 | |
| 2764 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2765 | struct hci_chan *tmp; |
| 2766 | |
| 2767 | if (conn->type != type) |
| 2768 | continue; |
| 2769 | |
| 2770 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 2771 | continue; |
| 2772 | |
| 2773 | conn_num++; |
| 2774 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 2775 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2776 | struct sk_buff *skb; |
| 2777 | |
| 2778 | if (skb_queue_empty(&tmp->data_q)) |
| 2779 | continue; |
| 2780 | |
| 2781 | skb = skb_peek(&tmp->data_q); |
| 2782 | if (skb->priority < cur_prio) |
| 2783 | continue; |
| 2784 | |
| 2785 | if (skb->priority > cur_prio) { |
| 2786 | num = 0; |
| 2787 | min = ~0; |
| 2788 | cur_prio = skb->priority; |
| 2789 | } |
| 2790 | |
| 2791 | num++; |
| 2792 | |
| 2793 | if (conn->sent < min) { |
| 2794 | min = conn->sent; |
| 2795 | chan = tmp; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | if (hci_conn_num(hdev, type) == conn_num) |
| 2800 | break; |
| 2801 | } |
| 2802 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2803 | rcu_read_unlock(); |
| 2804 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2805 | if (!chan) |
| 2806 | return NULL; |
| 2807 | |
| 2808 | switch (chan->conn->type) { |
| 2809 | case ACL_LINK: |
| 2810 | cnt = hdev->acl_cnt; |
| 2811 | break; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 2812 | case AMP_LINK: |
| 2813 | cnt = hdev->block_cnt; |
| 2814 | break; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2815 | case SCO_LINK: |
| 2816 | case ESCO_LINK: |
| 2817 | cnt = hdev->sco_cnt; |
| 2818 | break; |
| 2819 | case LE_LINK: |
| 2820 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 2821 | break; |
| 2822 | default: |
| 2823 | cnt = 0; |
| 2824 | BT_ERR("Unknown link type"); |
| 2825 | } |
| 2826 | |
| 2827 | q = cnt / num; |
| 2828 | *quote = q ? q : 1; |
| 2829 | BT_DBG("chan %p quote %d", chan, *quote); |
| 2830 | return chan; |
| 2831 | } |
| 2832 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2833 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
| 2834 | { |
| 2835 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 2836 | struct hci_conn *conn; |
| 2837 | int num = 0; |
| 2838 | |
| 2839 | BT_DBG("%s", hdev->name); |
| 2840 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2841 | rcu_read_lock(); |
| 2842 | |
| 2843 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2844 | struct hci_chan *chan; |
| 2845 | |
| 2846 | if (conn->type != type) |
| 2847 | continue; |
| 2848 | |
| 2849 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 2850 | continue; |
| 2851 | |
| 2852 | num++; |
| 2853 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 2854 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2855 | struct sk_buff *skb; |
| 2856 | |
| 2857 | if (chan->sent) { |
| 2858 | chan->sent = 0; |
| 2859 | continue; |
| 2860 | } |
| 2861 | |
| 2862 | if (skb_queue_empty(&chan->data_q)) |
| 2863 | continue; |
| 2864 | |
| 2865 | skb = skb_peek(&chan->data_q); |
| 2866 | if (skb->priority >= HCI_PRIO_MAX - 1) |
| 2867 | continue; |
| 2868 | |
| 2869 | skb->priority = HCI_PRIO_MAX - 1; |
| 2870 | |
| 2871 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2872 | skb->priority); |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | if (hci_conn_num(hdev, type) == num) |
| 2876 | break; |
| 2877 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 2878 | |
| 2879 | rcu_read_unlock(); |
| 2880 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2881 | } |
| 2882 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2883 | static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) |
| 2884 | { |
| 2885 | /* Calculate count of blocks used by this packet */ |
| 2886 | return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); |
| 2887 | } |
| 2888 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2889 | static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 2892 | /* ACL tx timeout must be longer than maximum |
| 2893 | * link supervision timeout (40.9 seconds) */ |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 2894 | if (!cnt && time_after(jiffies, hdev->acl_last_tx + |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 2895 | HCI_ACL_TX_TIMEOUT)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 2896 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | } |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 2898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2900 | static void hci_sched_acl_pkt(struct hci_dev *hdev) |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 2901 | { |
| 2902 | unsigned int cnt = hdev->acl_cnt; |
| 2903 | struct hci_chan *chan; |
| 2904 | struct sk_buff *skb; |
| 2905 | int quote; |
| 2906 | |
| 2907 | __check_timeout(hdev, cnt); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2908 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2909 | while (hdev->acl_cnt && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2910 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2911 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 2912 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2913 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2914 | skb->len, skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2915 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 2916 | /* Stop if priority has changed */ |
| 2917 | if (skb->priority < priority) |
| 2918 | break; |
| 2919 | |
| 2920 | skb = skb_dequeue(&chan->data_q); |
| 2921 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2922 | hci_conn_enter_active_mode(chan->conn, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2923 | bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 2924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | hci_send_frame(skb); |
| 2926 | hdev->acl_last_tx = jiffies; |
| 2927 | |
| 2928 | hdev->acl_cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 2929 | chan->sent++; |
| 2930 | chan->conn->sent++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | } |
| 2932 | } |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 2933 | |
| 2934 | if (cnt != hdev->acl_cnt) |
| 2935 | hci_prio_recalculate(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2938 | static void hci_sched_acl_blk(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2939 | { |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 2940 | unsigned int cnt = hdev->block_cnt; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2941 | struct hci_chan *chan; |
| 2942 | struct sk_buff *skb; |
| 2943 | int quote; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 2944 | u8 type; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2945 | |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 2946 | __check_timeout(hdev, cnt); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2947 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 2948 | BT_DBG("%s", hdev->name); |
| 2949 | |
| 2950 | if (hdev->dev_type == HCI_AMP) |
| 2951 | type = AMP_LINK; |
| 2952 | else |
| 2953 | type = ACL_LINK; |
| 2954 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2955 | while (hdev->block_cnt > 0 && |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 2956 | (chan = hci_chan_sent(hdev, type, "e))) { |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2957 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 2958 | while (quote > 0 && (skb = skb_peek(&chan->data_q))) { |
| 2959 | int blocks; |
| 2960 | |
| 2961 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2962 | skb->len, skb->priority); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2963 | |
| 2964 | /* Stop if priority has changed */ |
| 2965 | if (skb->priority < priority) |
| 2966 | break; |
| 2967 | |
| 2968 | skb = skb_dequeue(&chan->data_q); |
| 2969 | |
| 2970 | blocks = __get_blocks(hdev, skb); |
| 2971 | if (blocks > hdev->block_cnt) |
| 2972 | return; |
| 2973 | |
| 2974 | hci_conn_enter_active_mode(chan->conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2975 | bt_cb(skb)->force_active); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2976 | |
| 2977 | hci_send_frame(skb); |
| 2978 | hdev->acl_last_tx = jiffies; |
| 2979 | |
| 2980 | hdev->block_cnt -= blocks; |
| 2981 | quote -= blocks; |
| 2982 | |
| 2983 | chan->sent += blocks; |
| 2984 | chan->conn->sent += blocks; |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | if (cnt != hdev->block_cnt) |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 2989 | hci_prio_recalculate(hdev, type); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2992 | static void hci_sched_acl(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 2993 | { |
| 2994 | BT_DBG("%s", hdev->name); |
| 2995 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 2996 | /* No ACL link over BR/EDR controller */ |
| 2997 | if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) |
| 2998 | return; |
| 2999 | |
| 3000 | /* No AMP link over AMP controller */ |
| 3001 | if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3002 | return; |
| 3003 | |
| 3004 | switch (hdev->flow_ctl_mode) { |
| 3005 | case HCI_FLOW_CTL_MODE_PACKET_BASED: |
| 3006 | hci_sched_acl_pkt(hdev); |
| 3007 | break; |
| 3008 | |
| 3009 | case HCI_FLOW_CTL_MODE_BLOCK_BASED: |
| 3010 | hci_sched_acl_blk(hdev); |
| 3011 | break; |
| 3012 | } |
| 3013 | } |
| 3014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | /* Schedule SCO */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3016 | static void hci_sched_sco(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | { |
| 3018 | struct hci_conn *conn; |
| 3019 | struct sk_buff *skb; |
| 3020 | int quote; |
| 3021 | |
| 3022 | BT_DBG("%s", hdev->name); |
| 3023 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3024 | if (!hci_conn_num(hdev, SCO_LINK)) |
| 3025 | return; |
| 3026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 3028 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3029 | BT_DBG("skb %p len %d", skb, skb->len); |
| 3030 | hci_send_frame(skb); |
| 3031 | |
| 3032 | conn->sent++; |
| 3033 | if (conn->sent == ~0) |
| 3034 | conn->sent = 0; |
| 3035 | } |
| 3036 | } |
| 3037 | } |
| 3038 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3039 | static void hci_sched_esco(struct hci_dev *hdev) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3040 | { |
| 3041 | struct hci_conn *conn; |
| 3042 | struct sk_buff *skb; |
| 3043 | int quote; |
| 3044 | |
| 3045 | BT_DBG("%s", hdev->name); |
| 3046 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3047 | if (!hci_conn_num(hdev, ESCO_LINK)) |
| 3048 | return; |
| 3049 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3050 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, |
| 3051 | "e))) { |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3052 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3053 | BT_DBG("skb %p len %d", skb, skb->len); |
| 3054 | hci_send_frame(skb); |
| 3055 | |
| 3056 | conn->sent++; |
| 3057 | if (conn->sent == ~0) |
| 3058 | conn->sent = 0; |
| 3059 | } |
| 3060 | } |
| 3061 | } |
| 3062 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3063 | static void hci_sched_le(struct hci_dev *hdev) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3064 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3065 | struct hci_chan *chan; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3066 | struct sk_buff *skb; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3067 | int quote, cnt, tmp; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3068 | |
| 3069 | BT_DBG("%s", hdev->name); |
| 3070 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3071 | if (!hci_conn_num(hdev, LE_LINK)) |
| 3072 | return; |
| 3073 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3074 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 3075 | /* LE tx timeout must be longer than maximum |
| 3076 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3077 | if (!hdev->le_cnt && hdev->le_pkts && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3078 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3079 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3083 | tmp = cnt; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3084 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3085 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3086 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3087 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3088 | skb->len, skb->priority); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3089 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3090 | /* Stop if priority has changed */ |
| 3091 | if (skb->priority < priority) |
| 3092 | break; |
| 3093 | |
| 3094 | skb = skb_dequeue(&chan->data_q); |
| 3095 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3096 | hci_send_frame(skb); |
| 3097 | hdev->le_last_tx = jiffies; |
| 3098 | |
| 3099 | cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3100 | chan->sent++; |
| 3101 | chan->conn->sent++; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3102 | } |
| 3103 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3104 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3105 | if (hdev->le_pkts) |
| 3106 | hdev->le_cnt = cnt; |
| 3107 | else |
| 3108 | hdev->acl_cnt = cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3109 | |
| 3110 | if (cnt != tmp) |
| 3111 | hci_prio_recalculate(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3112 | } |
| 3113 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3114 | static void hci_tx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | { |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3116 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | struct sk_buff *skb; |
| 3118 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3119 | BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3120 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | |
| 3122 | /* Schedule queues and send stuff to HCI driver */ |
| 3123 | |
| 3124 | hci_sched_acl(hdev); |
| 3125 | |
| 3126 | hci_sched_sco(hdev); |
| 3127 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3128 | hci_sched_esco(hdev); |
| 3129 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3130 | hci_sched_le(hdev); |
| 3131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | /* Send next queued raw (unknown type) packet */ |
| 3133 | while ((skb = skb_dequeue(&hdev->raw_q))) |
| 3134 | hci_send_frame(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3137 | /* ----- HCI RX task (incoming data processing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | |
| 3139 | /* ACL data packet */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3140 | static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | { |
| 3142 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 3143 | struct hci_conn *conn; |
| 3144 | __u16 handle, flags; |
| 3145 | |
| 3146 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 3147 | |
| 3148 | handle = __le16_to_cpu(hdr->handle); |
| 3149 | flags = hci_flags(handle); |
| 3150 | handle = hci_handle(handle); |
| 3151 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3152 | BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3153 | handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
| 3155 | hdev->stat.acl_rx++; |
| 3156 | |
| 3157 | hci_dev_lock(hdev); |
| 3158 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3159 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | if (conn) { |
Mat Martineau | 65983fc | 2011-12-13 15:06:02 -0800 | [diff] [blame] | 3162 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3165 | l2cap_recv_acldata(conn, skb, flags); |
| 3166 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3168 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3169 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | kfree_skb(skb); |
| 3173 | } |
| 3174 | |
| 3175 | /* SCO data packet */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3176 | static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | { |
| 3178 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 3179 | struct hci_conn *conn; |
| 3180 | __u16 handle; |
| 3181 | |
| 3182 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 3183 | |
| 3184 | handle = __le16_to_cpu(hdr->handle); |
| 3185 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3186 | BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | |
| 3188 | hdev->stat.sco_rx++; |
| 3189 | |
| 3190 | hci_dev_lock(hdev); |
| 3191 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3192 | hci_dev_unlock(hdev); |
| 3193 | |
| 3194 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3196 | sco_recv_scodata(conn, skb); |
| 3197 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3199 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3200 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | kfree_skb(skb); |
| 3204 | } |
| 3205 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3206 | static bool hci_req_is_complete(struct hci_dev *hdev) |
| 3207 | { |
| 3208 | struct sk_buff *skb; |
| 3209 | |
| 3210 | skb = skb_peek(&hdev->cmd_q); |
| 3211 | if (!skb) |
| 3212 | return true; |
| 3213 | |
| 3214 | return bt_cb(skb)->req.start; |
| 3215 | } |
| 3216 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3217 | static void hci_resend_last(struct hci_dev *hdev) |
| 3218 | { |
| 3219 | struct hci_command_hdr *sent; |
| 3220 | struct sk_buff *skb; |
| 3221 | u16 opcode; |
| 3222 | |
| 3223 | if (!hdev->sent_cmd) |
| 3224 | return; |
| 3225 | |
| 3226 | sent = (void *) hdev->sent_cmd->data; |
| 3227 | opcode = __le16_to_cpu(sent->opcode); |
| 3228 | if (opcode == HCI_OP_RESET) |
| 3229 | return; |
| 3230 | |
| 3231 | skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); |
| 3232 | if (!skb) |
| 3233 | return; |
| 3234 | |
| 3235 | skb_queue_head(&hdev->cmd_q, skb); |
| 3236 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 3237 | } |
| 3238 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3239 | void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status) |
| 3240 | { |
| 3241 | hci_req_complete_t req_complete = NULL; |
| 3242 | struct sk_buff *skb; |
| 3243 | unsigned long flags; |
| 3244 | |
| 3245 | BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); |
| 3246 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3247 | /* If the completed command doesn't match the last one that was |
| 3248 | * sent we need to do special handling of it. |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3249 | */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3250 | if (!hci_sent_cmd_data(hdev, opcode)) { |
| 3251 | /* Some CSR based controllers generate a spontaneous |
| 3252 | * reset complete event during init and any pending |
| 3253 | * command will never be completed. In such a case we |
| 3254 | * need to resend whatever was the last sent |
| 3255 | * command. |
| 3256 | */ |
| 3257 | if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) |
| 3258 | hci_resend_last(hdev); |
| 3259 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3260 | return; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 3261 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 3262 | |
| 3263 | /* If the command succeeded and there's still more commands in |
| 3264 | * this request the request is not yet complete. |
| 3265 | */ |
| 3266 | if (!status && !hci_req_is_complete(hdev)) |
| 3267 | return; |
| 3268 | |
| 3269 | /* If this was the last command in a request the complete |
| 3270 | * callback would be found in hdev->sent_cmd instead of the |
| 3271 | * command queue (hdev->cmd_q). |
| 3272 | */ |
| 3273 | if (hdev->sent_cmd) { |
| 3274 | req_complete = bt_cb(hdev->sent_cmd)->req.complete; |
| 3275 | if (req_complete) |
| 3276 | goto call_complete; |
| 3277 | } |
| 3278 | |
| 3279 | /* Remove all pending commands belonging to this request */ |
| 3280 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 3281 | while ((skb = __skb_dequeue(&hdev->cmd_q))) { |
| 3282 | if (bt_cb(skb)->req.start) { |
| 3283 | __skb_queue_head(&hdev->cmd_q, skb); |
| 3284 | break; |
| 3285 | } |
| 3286 | |
| 3287 | req_complete = bt_cb(skb)->req.complete; |
| 3288 | kfree_skb(skb); |
| 3289 | } |
| 3290 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 3291 | |
| 3292 | call_complete: |
| 3293 | if (req_complete) |
| 3294 | req_complete(hdev, status); |
| 3295 | } |
| 3296 | |
| 3297 | void hci_req_cmd_status(struct hci_dev *hdev, u16 opcode, u8 status) |
| 3298 | { |
| 3299 | hci_req_complete_t req_complete = NULL; |
| 3300 | |
| 3301 | BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); |
| 3302 | |
| 3303 | if (status) { |
| 3304 | hci_req_cmd_complete(hdev, opcode, status); |
| 3305 | return; |
| 3306 | } |
| 3307 | |
| 3308 | /* No need to handle success status if there are more commands */ |
| 3309 | if (!hci_req_is_complete(hdev)) |
| 3310 | return; |
| 3311 | |
| 3312 | if (hdev->sent_cmd) |
| 3313 | req_complete = bt_cb(hdev->sent_cmd)->req.complete; |
| 3314 | |
| 3315 | /* If the request doesn't have a complete callback or there |
| 3316 | * are other commands/requests in the hdev queue we consider |
| 3317 | * this request as completed. |
| 3318 | */ |
| 3319 | if (!req_complete || !skb_queue_empty(&hdev->cmd_q)) |
| 3320 | hci_req_cmd_complete(hdev, opcode, status); |
| 3321 | } |
| 3322 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3323 | static void hci_rx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | { |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3325 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | struct sk_buff *skb; |
| 3327 | |
| 3328 | BT_DBG("%s", hdev->name); |
| 3329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3331 | /* Send copy to monitor */ |
| 3332 | hci_send_to_monitor(hdev, skb); |
| 3333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | if (atomic_read(&hdev->promisc)) { |
| 3335 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 3336 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | if (test_bit(HCI_RAW, &hdev->flags)) { |
| 3340 | kfree_skb(skb); |
| 3341 | continue; |
| 3342 | } |
| 3343 | |
| 3344 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 3345 | /* Don't process data packets in this states. */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3346 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | case HCI_ACLDATA_PKT: |
| 3348 | case HCI_SCODATA_PKT: |
| 3349 | kfree_skb(skb); |
| 3350 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | /* Process frame */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3355 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | case HCI_EVENT_PKT: |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3357 | BT_DBG("%s Event packet", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | hci_event_packet(hdev, skb); |
| 3359 | break; |
| 3360 | |
| 3361 | case HCI_ACLDATA_PKT: |
| 3362 | BT_DBG("%s ACL data packet", hdev->name); |
| 3363 | hci_acldata_packet(hdev, skb); |
| 3364 | break; |
| 3365 | |
| 3366 | case HCI_SCODATA_PKT: |
| 3367 | BT_DBG("%s SCO data packet", hdev->name); |
| 3368 | hci_scodata_packet(hdev, skb); |
| 3369 | break; |
| 3370 | |
| 3371 | default: |
| 3372 | kfree_skb(skb); |
| 3373 | break; |
| 3374 | } |
| 3375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3378 | static void hci_cmd_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | { |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3380 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | struct sk_buff *skb; |
| 3382 | |
Andrei Emeltchenko | 2104786 | 2012-07-10 15:27:47 +0300 | [diff] [blame] | 3383 | BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, |
| 3384 | atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 3387 | if (atomic_read(&hdev->cmd_cnt)) { |
| 3388 | skb = skb_dequeue(&hdev->cmd_q); |
| 3389 | if (!skb) |
| 3390 | return; |
| 3391 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 3392 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 3394 | hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC); |
| 3395 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | atomic_dec(&hdev->cmd_cnt); |
| 3397 | hci_send_frame(skb); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 3398 | if (test_bit(HCI_RESET, &hdev->flags)) |
| 3399 | del_timer(&hdev->cmd_timer); |
| 3400 | else |
| 3401 | mod_timer(&hdev->cmd_timer, |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 3402 | jiffies + HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | } else { |
| 3404 | skb_queue_head(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3405 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | } |
| 3407 | } |
| 3408 | } |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 3409 | |
| 3410 | int hci_do_inquiry(struct hci_dev *hdev, u8 length) |
| 3411 | { |
| 3412 | /* General inquiry access code (GIAC) */ |
| 3413 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 3414 | struct hci_cp_inquiry cp; |
| 3415 | |
| 3416 | BT_DBG("%s", hdev->name); |
| 3417 | |
| 3418 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 3419 | return -EINPROGRESS; |
| 3420 | |
Johan Hedberg | 4663262 | 2012-01-02 16:06:08 +0200 | [diff] [blame] | 3421 | inquiry_cache_flush(hdev); |
| 3422 | |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 3423 | memset(&cp, 0, sizeof(cp)); |
| 3424 | memcpy(&cp.lap, lap, sizeof(cp.lap)); |
| 3425 | cp.length = length; |
| 3426 | |
| 3427 | return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 3428 | } |
Andre Guedes | 023d5049 | 2011-11-04 14:16:52 -0300 | [diff] [blame] | 3429 | |
| 3430 | int hci_cancel_inquiry(struct hci_dev *hdev) |
| 3431 | { |
| 3432 | BT_DBG("%s", hdev->name); |
| 3433 | |
| 3434 | if (!test_bit(HCI_INQUIRY, &hdev->flags)) |
Andre Guedes | 7537e5c | 2012-03-20 00:13:38 -0300 | [diff] [blame] | 3435 | return -EALREADY; |
Andre Guedes | 023d5049 | 2011-11-04 14:16:52 -0300 | [diff] [blame] | 3436 | |
| 3437 | return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 3438 | } |
Andre Guedes | 31f7956 | 2012-04-24 21:02:53 -0300 | [diff] [blame] | 3439 | |
| 3440 | u8 bdaddr_to_le(u8 bdaddr_type) |
| 3441 | { |
| 3442 | switch (bdaddr_type) { |
| 3443 | case BDADDR_LE_PUBLIC: |
| 3444 | return ADDR_LE_DEV_PUBLIC; |
| 3445 | |
| 3446 | default: |
| 3447 | /* Fallback to LE Random address type */ |
| 3448 | return ADDR_LE_DEV_RANDOM; |
| 3449 | } |
| 3450 | } |