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> |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 30 | #include <linux/rfkill.h> |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 31 | #include <linux/debugfs.h> |
Johan Hedberg | 99780a7 | 2014-02-18 10:40:07 +0200 | [diff] [blame] | 32 | #include <linux/crypto.h> |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 33 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <net/bluetooth/bluetooth.h> |
| 36 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 37 | #include <net/bluetooth/l2cap.h> |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 38 | #include <net/bluetooth/mgmt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 40 | #include "hci_request.h" |
Marcel Holtmann | 60c5f5f | 2014-12-20 16:05:13 +0100 | [diff] [blame] | 41 | #include "hci_debugfs.h" |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 42 | #include "smp.h" |
| 43 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 44 | static void hci_rx_work(struct work_struct *work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 45 | static void hci_cmd_work(struct work_struct *work); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 46 | static void hci_tx_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* HCI device list */ |
| 49 | LIST_HEAD(hci_dev_list); |
| 50 | DEFINE_RWLOCK(hci_dev_list_lock); |
| 51 | |
| 52 | /* HCI callback list */ |
| 53 | LIST_HEAD(hci_cb_list); |
Johan Hedberg | fba7ecf | 2015-02-18 14:53:55 +0200 | [diff] [blame] | 54 | DEFINE_MUTEX(hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 56 | /* HCI ID Numbering */ |
| 57 | static DEFINE_IDA(hci_index_ida); |
| 58 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 59 | /* ---- HCI debugfs entries ---- */ |
| 60 | |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 61 | static ssize_t dut_mode_read(struct file *file, char __user *user_buf, |
| 62 | size_t count, loff_t *ppos) |
| 63 | { |
| 64 | struct hci_dev *hdev = file->private_data; |
| 65 | char buf[3]; |
| 66 | |
Prasanna Karthik | 74b93e9 | 2015-11-18 12:38:41 +0000 | [diff] [blame] | 67 | buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y' : 'N'; |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 68 | buf[1] = '\n'; |
| 69 | buf[2] = '\0'; |
| 70 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 71 | } |
| 72 | |
| 73 | static ssize_t dut_mode_write(struct file *file, const char __user *user_buf, |
| 74 | size_t count, loff_t *ppos) |
| 75 | { |
| 76 | struct hci_dev *hdev = file->private_data; |
| 77 | struct sk_buff *skb; |
| 78 | char buf[32]; |
| 79 | size_t buf_size = min(count, (sizeof(buf)-1)); |
| 80 | bool enable; |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 81 | |
| 82 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 83 | return -ENETDOWN; |
| 84 | |
| 85 | if (copy_from_user(buf, user_buf, buf_size)) |
| 86 | return -EFAULT; |
| 87 | |
| 88 | buf[buf_size] = '\0'; |
| 89 | if (strtobool(buf, &enable)) |
| 90 | return -EINVAL; |
| 91 | |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 92 | if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE)) |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 93 | return -EALREADY; |
| 94 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 95 | hci_req_sync_lock(hdev); |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 96 | if (enable) |
| 97 | skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL, |
| 98 | HCI_CMD_TIMEOUT); |
| 99 | else |
| 100 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, |
| 101 | HCI_CMD_TIMEOUT); |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 102 | hci_req_sync_unlock(hdev); |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 103 | |
| 104 | if (IS_ERR(skb)) |
| 105 | return PTR_ERR(skb); |
| 106 | |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 107 | kfree_skb(skb); |
| 108 | |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 109 | hci_dev_change_flag(hdev, HCI_DUT_MODE); |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 110 | |
| 111 | return count; |
| 112 | } |
| 113 | |
| 114 | static const struct file_operations dut_mode_fops = { |
| 115 | .open = simple_open, |
| 116 | .read = dut_mode_read, |
| 117 | .write = dut_mode_write, |
| 118 | .llseek = default_llseek, |
| 119 | }; |
| 120 | |
Marcel Holtmann | 4b4113d | 2015-10-07 19:52:35 +0200 | [diff] [blame] | 121 | static ssize_t vendor_diag_read(struct file *file, char __user *user_buf, |
| 122 | size_t count, loff_t *ppos) |
| 123 | { |
| 124 | struct hci_dev *hdev = file->private_data; |
| 125 | char buf[3]; |
| 126 | |
Prasanna Karthik | 74b93e9 | 2015-11-18 12:38:41 +0000 | [diff] [blame] | 127 | buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y' : 'N'; |
Marcel Holtmann | 4b4113d | 2015-10-07 19:52:35 +0200 | [diff] [blame] | 128 | buf[1] = '\n'; |
| 129 | buf[2] = '\0'; |
| 130 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 131 | } |
| 132 | |
| 133 | static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf, |
| 134 | size_t count, loff_t *ppos) |
| 135 | { |
| 136 | struct hci_dev *hdev = file->private_data; |
| 137 | char buf[32]; |
| 138 | size_t buf_size = min(count, (sizeof(buf)-1)); |
| 139 | bool enable; |
| 140 | int err; |
| 141 | |
| 142 | if (copy_from_user(buf, user_buf, buf_size)) |
| 143 | return -EFAULT; |
| 144 | |
| 145 | buf[buf_size] = '\0'; |
| 146 | if (strtobool(buf, &enable)) |
| 147 | return -EINVAL; |
| 148 | |
Marcel Holtmann | 7e995b9 | 2015-10-17 16:00:26 +0200 | [diff] [blame] | 149 | /* When the diagnostic flags are not persistent and the transport |
| 150 | * is not active, then there is no need for the vendor callback. |
| 151 | * |
| 152 | * Instead just store the desired value. If needed the setting |
| 153 | * will be programmed when the controller gets powered on. |
| 154 | */ |
| 155 | if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && |
| 156 | !test_bit(HCI_RUNNING, &hdev->flags)) |
| 157 | goto done; |
| 158 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 159 | hci_req_sync_lock(hdev); |
Marcel Holtmann | 4b4113d | 2015-10-07 19:52:35 +0200 | [diff] [blame] | 160 | err = hdev->set_diag(hdev, enable); |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 161 | hci_req_sync_unlock(hdev); |
Marcel Holtmann | 4b4113d | 2015-10-07 19:52:35 +0200 | [diff] [blame] | 162 | |
| 163 | if (err < 0) |
| 164 | return err; |
| 165 | |
Marcel Holtmann | 7e995b9 | 2015-10-17 16:00:26 +0200 | [diff] [blame] | 166 | done: |
Marcel Holtmann | 4b4113d | 2015-10-07 19:52:35 +0200 | [diff] [blame] | 167 | if (enable) |
| 168 | hci_dev_set_flag(hdev, HCI_VENDOR_DIAG); |
| 169 | else |
| 170 | hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG); |
| 171 | |
| 172 | return count; |
| 173 | } |
| 174 | |
| 175 | static const struct file_operations vendor_diag_fops = { |
| 176 | .open = simple_open, |
| 177 | .read = vendor_diag_read, |
| 178 | .write = vendor_diag_write, |
| 179 | .llseek = default_llseek, |
| 180 | }; |
| 181 | |
Marcel Holtmann | f640ee9 | 2015-10-08 12:35:42 +0200 | [diff] [blame] | 182 | static void hci_debugfs_create_basic(struct hci_dev *hdev) |
| 183 | { |
| 184 | debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev, |
| 185 | &dut_mode_fops); |
| 186 | |
| 187 | if (hdev->set_diag) |
| 188 | debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev, |
| 189 | &vendor_diag_fops); |
| 190 | } |
| 191 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 192 | static int hci_reset_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 194 | BT_DBG("%s %ld", req->hdev->name, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | /* Reset device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 197 | set_bit(HCI_RESET, &req->hdev->flags); |
| 198 | hci_req_add(req, HCI_OP_RESET, 0, NULL); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 199 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 202 | static void bredr_init(struct hci_request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 204 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* Read Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 207 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 209 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 210 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 211 | |
| 212 | /* Read BD Address */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 213 | hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Johan Hedberg | 0af801b | 2015-02-17 15:05:21 +0200 | [diff] [blame] | 216 | static void amp_init1(struct hci_request *req) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 217 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 218 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 219 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 220 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 221 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 222 | |
Marcel Holtmann | f6996cf | 2013-10-07 02:31:39 -0700 | [diff] [blame] | 223 | /* Read Local Supported Commands */ |
| 224 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
| 225 | |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 226 | /* Read Local AMP Info */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 227 | hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
Andrei Emeltchenko | e71dfab | 2012-09-06 15:05:46 +0300 | [diff] [blame] | 228 | |
| 229 | /* Read Data Blk size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 230 | hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 231 | |
Marcel Holtmann | f38ba94 | 2013-10-07 03:55:53 -0700 | [diff] [blame] | 232 | /* Read Flow Control Mode */ |
| 233 | hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); |
| 234 | |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 235 | /* Read Location Data */ |
| 236 | hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 239 | static int amp_init2(struct hci_request *req) |
Johan Hedberg | 0af801b | 2015-02-17 15:05:21 +0200 | [diff] [blame] | 240 | { |
| 241 | /* Read Local Supported Features. Not all AMP controllers |
| 242 | * support this so it's placed conditionally in the second |
| 243 | * stage init. |
| 244 | */ |
| 245 | if (req->hdev->commands[14] & 0x20) |
| 246 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 247 | |
| 248 | return 0; |
Johan Hedberg | 0af801b | 2015-02-17 15:05:21 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 251 | static int hci_init1_req(struct hci_request *req, unsigned long opt) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 252 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 253 | struct hci_dev *hdev = req->hdev; |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 254 | |
| 255 | BT_DBG("%s %ld", hdev->name, opt); |
| 256 | |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 257 | /* Reset */ |
| 258 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 259 | hci_reset_req(req, 0); |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 260 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 261 | switch (hdev->dev_type) { |
| 262 | case HCI_BREDR: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 263 | bredr_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | case HCI_AMP: |
Johan Hedberg | 0af801b | 2015-02-17 15:05:21 +0200 | [diff] [blame] | 267 | amp_init1(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | default: |
| 271 | BT_ERR("Unknown device type %d", hdev->dev_type); |
| 272 | break; |
| 273 | } |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 274 | |
| 275 | return 0; |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 278 | static void bredr_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 279 | { |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 280 | __le16 param; |
| 281 | __u8 flt_type; |
| 282 | |
| 283 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 284 | hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 285 | |
| 286 | /* Read Class of Device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 287 | hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 288 | |
| 289 | /* Read Local Name */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 290 | hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 291 | |
| 292 | /* Read Voice Setting */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 293 | hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 294 | |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 295 | /* Read Number of Supported IAC */ |
| 296 | hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); |
| 297 | |
Marcel Holtmann | 4b836f3 | 2013-10-14 14:06:36 -0700 | [diff] [blame] | 298 | /* Read Current IAC LAP */ |
| 299 | hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); |
| 300 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 301 | /* Clear Event Filters */ |
| 302 | flt_type = HCI_FLT_CLEAR_ALL; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 303 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 304 | |
| 305 | /* Connection accept timeout ~20 secs */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 306 | param = cpu_to_le16(0x7d00); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 307 | hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 310 | static void le_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 311 | { |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 312 | struct hci_dev *hdev = req->hdev; |
| 313 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 314 | /* Read LE Buffer Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 315 | hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 316 | |
| 317 | /* Read LE Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 318 | hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 319 | |
Marcel Holtmann | 747d3f0 | 2014-02-27 20:37:29 -0800 | [diff] [blame] | 320 | /* Read LE Supported States */ |
| 321 | hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); |
| 322 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 323 | /* LE-only controllers have LE implicitly enabled */ |
| 324 | if (!lmp_bredr_capable(hdev)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 325 | hci_dev_set_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 328 | static void hci_setup_event_mask(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 329 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 330 | struct hci_dev *hdev = req->hdev; |
| 331 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 332 | /* The second byte is 0xff instead of 0x9f (two reserved bits |
| 333 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the |
| 334 | * command otherwise. |
| 335 | */ |
| 336 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 337 | |
| 338 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set |
| 339 | * any event mask for pre 1.2 devices. |
| 340 | */ |
| 341 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 342 | return; |
| 343 | |
| 344 | if (lmp_bredr_capable(hdev)) { |
| 345 | events[4] |= 0x01; /* Flow Specification Complete */ |
Marcel Holtmann | c7882cb | 2013-08-13 10:00:54 -0700 | [diff] [blame] | 346 | } else { |
| 347 | /* Use a different default for LE-only devices */ |
| 348 | memset(events, 0, sizeof(events)); |
Marcel Holtmann | c7882cb | 2013-08-13 10:00:54 -0700 | [diff] [blame] | 349 | events[1] |= 0x20; /* Command Complete */ |
| 350 | events[1] |= 0x40; /* Command Status */ |
| 351 | events[1] |= 0x80; /* Hardware Error */ |
Marcel Holtmann | 5c3d3b4 | 2015-11-04 07:17:23 +0100 | [diff] [blame] | 352 | |
| 353 | /* If the controller supports the Disconnect command, enable |
| 354 | * the corresponding event. In addition enable packet flow |
| 355 | * control related events. |
| 356 | */ |
| 357 | if (hdev->commands[0] & 0x20) { |
| 358 | events[0] |= 0x10; /* Disconnection Complete */ |
| 359 | events[2] |= 0x04; /* Number of Completed Packets */ |
| 360 | events[3] |= 0x02; /* Data Buffer Overflow */ |
| 361 | } |
| 362 | |
| 363 | /* If the controller supports the Read Remote Version |
| 364 | * Information command, enable the corresponding event. |
| 365 | */ |
| 366 | if (hdev->commands[2] & 0x80) |
| 367 | events[1] |= 0x08; /* Read Remote Version Information |
| 368 | * Complete |
| 369 | */ |
Marcel Holtmann | 0da71f1 | 2014-07-12 23:36:16 +0200 | [diff] [blame] | 370 | |
| 371 | if (hdev->le_features[0] & HCI_LE_ENCRYPTION) { |
| 372 | events[0] |= 0x80; /* Encryption Change */ |
| 373 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
| 374 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Marcel Holtmann | 9fe759c | 2015-11-01 09:45:22 +0100 | [diff] [blame] | 377 | if (lmp_inq_rssi_capable(hdev) || |
| 378 | test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 379 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 380 | |
Marcel Holtmann | 70f56aa | 2015-11-01 09:39:49 +0100 | [diff] [blame] | 381 | if (lmp_ext_feat_capable(hdev)) |
| 382 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ |
| 383 | |
| 384 | if (lmp_esco_capable(hdev)) { |
| 385 | events[5] |= 0x08; /* Synchronous Connection Complete */ |
| 386 | events[5] |= 0x10; /* Synchronous Connection Changed */ |
| 387 | } |
| 388 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 389 | if (lmp_sniffsubr_capable(hdev)) |
| 390 | events[5] |= 0x20; /* Sniff Subrating */ |
| 391 | |
| 392 | if (lmp_pause_enc_capable(hdev)) |
| 393 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
| 394 | |
| 395 | if (lmp_ext_inq_capable(hdev)) |
| 396 | events[5] |= 0x40; /* Extended Inquiry Result */ |
| 397 | |
| 398 | if (lmp_no_flush_capable(hdev)) |
| 399 | events[7] |= 0x01; /* Enhanced Flush Complete */ |
| 400 | |
| 401 | if (lmp_lsto_capable(hdev)) |
| 402 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ |
| 403 | |
| 404 | if (lmp_ssp_capable(hdev)) { |
| 405 | events[6] |= 0x01; /* IO Capability Request */ |
| 406 | events[6] |= 0x02; /* IO Capability Response */ |
| 407 | events[6] |= 0x04; /* User Confirmation Request */ |
| 408 | events[6] |= 0x08; /* User Passkey Request */ |
| 409 | events[6] |= 0x10; /* Remote OOB Data Request */ |
| 410 | events[6] |= 0x20; /* Simple Pairing Complete */ |
| 411 | events[7] |= 0x04; /* User Passkey Notification */ |
| 412 | events[7] |= 0x08; /* Keypress Notification */ |
| 413 | events[7] |= 0x10; /* Remote Host Supported |
| 414 | * Features Notification |
| 415 | */ |
| 416 | } |
| 417 | |
| 418 | if (lmp_le_capable(hdev)) |
| 419 | events[7] |= 0x20; /* LE Meta-Event */ |
| 420 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 421 | hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 424 | static int hci_init2_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 425 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 426 | struct hci_dev *hdev = req->hdev; |
| 427 | |
Johan Hedberg | 0af801b | 2015-02-17 15:05:21 +0200 | [diff] [blame] | 428 | if (hdev->dev_type == HCI_AMP) |
| 429 | return amp_init2(req); |
| 430 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 431 | if (lmp_bredr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 432 | bredr_setup(req); |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 433 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 434 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 435 | |
| 436 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 437 | le_setup(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 438 | |
Marcel Holtmann | 0f3adea | 2014-12-26 04:42:34 +0100 | [diff] [blame] | 439 | /* All Bluetooth 1.2 and later controllers should support the |
| 440 | * HCI command for reading the local supported commands. |
| 441 | * |
| 442 | * Unfortunately some controllers indicate Bluetooth 1.2 support, |
| 443 | * but do not have support for this command. If that is the case, |
| 444 | * the driver can quirk the behavior and skip reading the local |
| 445 | * supported commands. |
Johan Hedberg | 3f8e2d7 | 2013-07-24 02:32:46 +0300 | [diff] [blame] | 446 | */ |
Marcel Holtmann | 0f3adea | 2014-12-26 04:42:34 +0100 | [diff] [blame] | 447 | if (hdev->hci_ver > BLUETOOTH_VER_1_1 && |
| 448 | !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 449 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 450 | |
| 451 | if (lmp_ssp_capable(hdev)) { |
Marcel Holtmann | 57af75a | 2013-10-18 12:04:47 -0700 | [diff] [blame] | 452 | /* When SSP is available, then the host features page |
| 453 | * should also be available as well. However some |
| 454 | * controllers list the max_page as 0 as long as SSP |
| 455 | * has not been enabled. To achieve proper debugging |
| 456 | * output, force the minimum max_page to 1 at least. |
| 457 | */ |
| 458 | hdev->max_page = 0x01; |
| 459 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 460 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 461 | u8 mode = 0x01; |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 462 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 463 | hci_req_add(req, HCI_OP_WRITE_SSP_MODE, |
| 464 | sizeof(mode), &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 465 | } else { |
| 466 | struct hci_cp_write_eir cp; |
| 467 | |
| 468 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 469 | memset(&cp, 0, sizeof(cp)); |
| 470 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 471 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Marcel Holtmann | 043ec9b | 2015-01-02 23:35:19 -0800 | [diff] [blame] | 475 | if (lmp_inq_rssi_capable(hdev) || |
| 476 | test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) { |
Marcel Holtmann | 04422da | 2015-01-02 23:35:18 -0800 | [diff] [blame] | 477 | u8 mode; |
| 478 | |
| 479 | /* If Extended Inquiry Result events are supported, then |
| 480 | * they are clearly preferred over Inquiry Result with RSSI |
| 481 | * events. |
| 482 | */ |
| 483 | mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; |
| 484 | |
| 485 | hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); |
| 486 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 487 | |
| 488 | if (lmp_inq_tx_pwr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 489 | 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] | 490 | |
| 491 | if (lmp_ext_feat_capable(hdev)) { |
| 492 | struct hci_cp_read_local_ext_features cp; |
| 493 | |
| 494 | cp.page = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 495 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 496 | sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 499 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 500 | u8 enable = 1; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 501 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), |
| 502 | &enable); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 503 | } |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 504 | |
| 505 | return 0; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 508 | static void hci_setup_link_policy(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 509 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 510 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 511 | struct hci_cp_write_def_link_policy cp; |
| 512 | u16 link_policy = 0; |
| 513 | |
| 514 | if (lmp_rswitch_capable(hdev)) |
| 515 | link_policy |= HCI_LP_RSWITCH; |
| 516 | if (lmp_hold_capable(hdev)) |
| 517 | link_policy |= HCI_LP_HOLD; |
| 518 | if (lmp_sniff_capable(hdev)) |
| 519 | link_policy |= HCI_LP_SNIFF; |
| 520 | if (lmp_park_capable(hdev)) |
| 521 | link_policy |= HCI_LP_PARK; |
| 522 | |
| 523 | cp.policy = cpu_to_le16(link_policy); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 524 | 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] | 525 | } |
| 526 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 527 | static void hci_set_le_support(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 528 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 529 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 530 | struct hci_cp_write_le_host_supported cp; |
| 531 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 532 | /* LE-only devices do not support explicit enablement */ |
| 533 | if (!lmp_bredr_capable(hdev)) |
| 534 | return; |
| 535 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 536 | memset(&cp, 0, sizeof(cp)); |
| 537 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 538 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 539 | cp.le = 0x01; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 540 | cp.simul = 0x00; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | if (cp.le != lmp_host_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 544 | hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), |
| 545 | &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 548 | static void hci_set_event_mask_page_2(struct hci_request *req) |
| 549 | { |
| 550 | struct hci_dev *hdev = req->hdev; |
| 551 | u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 552 | |
| 553 | /* If Connectionless Slave Broadcast master role is supported |
| 554 | * enable all necessary events for it. |
| 555 | */ |
Marcel Holtmann | 53b834d2 | 2013-12-08 11:55:33 -0800 | [diff] [blame] | 556 | if (lmp_csb_master_capable(hdev)) { |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 557 | events[1] |= 0x40; /* Triggered Clock Capture */ |
| 558 | events[1] |= 0x80; /* Synchronization Train Complete */ |
| 559 | events[2] |= 0x10; /* Slave Page Response Timeout */ |
| 560 | events[2] |= 0x20; /* CSB Channel Map Change */ |
| 561 | } |
| 562 | |
| 563 | /* If Connectionless Slave Broadcast slave role is supported |
| 564 | * enable all necessary events for it. |
| 565 | */ |
Marcel Holtmann | 53b834d2 | 2013-12-08 11:55:33 -0800 | [diff] [blame] | 566 | if (lmp_csb_slave_capable(hdev)) { |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 567 | events[2] |= 0x01; /* Synchronization Train Received */ |
| 568 | events[2] |= 0x02; /* CSB Receive */ |
| 569 | events[2] |= 0x04; /* CSB Timeout */ |
| 570 | events[2] |= 0x08; /* Truncated Page Complete */ |
| 571 | } |
| 572 | |
Marcel Holtmann | 40c59fc | 2014-01-10 02:07:21 -0800 | [diff] [blame] | 573 | /* Enable Authenticated Payload Timeout Expired event if supported */ |
Marcel Holtmann | cd7ca0e | 2014-07-09 09:49:05 +0200 | [diff] [blame] | 574 | if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) |
Marcel Holtmann | 40c59fc | 2014-01-10 02:07:21 -0800 | [diff] [blame] | 575 | events[2] |= 0x80; |
| 576 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 577 | hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); |
| 578 | } |
| 579 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 580 | static int hci_init3_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 581 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 582 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 583 | u8 p; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 584 | |
Marcel Holtmann | 0da71f1 | 2014-07-12 23:36:16 +0200 | [diff] [blame] | 585 | hci_setup_event_mask(req); |
| 586 | |
Johan Hedberg | e81be90 | 2015-08-30 21:47:20 +0300 | [diff] [blame] | 587 | if (hdev->commands[6] & 0x20 && |
| 588 | !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { |
Marcel Holtmann | 48ce62c | 2015-01-12 09:21:26 -0800 | [diff] [blame] | 589 | struct hci_cp_read_stored_link_key cp; |
| 590 | |
| 591 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 592 | cp.read_all = 0x01; |
| 593 | hci_req_add(req, HCI_OP_READ_STORED_LINK_KEY, sizeof(cp), &cp); |
| 594 | } |
| 595 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 596 | if (hdev->commands[5] & 0x10) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 597 | hci_setup_link_policy(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 598 | |
Marcel Holtmann | 417287d | 2014-12-11 20:21:54 +0100 | [diff] [blame] | 599 | if (hdev->commands[8] & 0x01) |
| 600 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); |
| 601 | |
| 602 | /* Some older Broadcom based Bluetooth 1.2 controllers do not |
| 603 | * support the Read Page Scan Type command. Check support for |
| 604 | * this command in the bit mask of supported commands. |
| 605 | */ |
| 606 | if (hdev->commands[13] & 0x01) |
| 607 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); |
| 608 | |
Andre Guedes | 9193c6e | 2014-07-01 18:10:09 -0300 | [diff] [blame] | 609 | if (lmp_le_capable(hdev)) { |
| 610 | u8 events[8]; |
| 611 | |
| 612 | memset(events, 0, sizeof(events)); |
Marcel Holtmann | 4d6c705 | 2014-07-13 00:29:22 +0200 | [diff] [blame] | 613 | |
| 614 | if (hdev->le_features[0] & HCI_LE_ENCRYPTION) |
| 615 | events[0] |= 0x10; /* LE Long Term Key Request */ |
Andre Guedes | 662bc2e | 2014-07-01 18:10:10 -0300 | [diff] [blame] | 616 | |
| 617 | /* If controller supports the Connection Parameters Request |
| 618 | * Link Layer Procedure, enable the corresponding event. |
| 619 | */ |
| 620 | if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) |
| 621 | events[0] |= 0x20; /* LE Remote Connection |
| 622 | * Parameter Request |
| 623 | */ |
| 624 | |
Marcel Holtmann | a9f6068 | 2014-12-20 16:28:39 +0100 | [diff] [blame] | 625 | /* If the controller supports the Data Length Extension |
| 626 | * feature, enable the corresponding event. |
| 627 | */ |
| 628 | if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) |
| 629 | events[0] |= 0x40; /* LE Data Length Change */ |
| 630 | |
Marcel Holtmann | 4b71bba | 2014-12-05 16:20:12 +0100 | [diff] [blame] | 631 | /* If the controller supports Extended Scanner Filter |
| 632 | * Policies, enable the correspondig event. |
| 633 | */ |
| 634 | if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY) |
| 635 | events[1] |= 0x04; /* LE Direct Advertising |
| 636 | * Report |
| 637 | */ |
| 638 | |
Marcel Holtmann | 7d26f5c | 2015-11-01 09:39:51 +0100 | [diff] [blame] | 639 | /* If the controller supports the LE Set Scan Enable command, |
| 640 | * enable the corresponding advertising report event. |
| 641 | */ |
| 642 | if (hdev->commands[26] & 0x08) |
| 643 | events[0] |= 0x02; /* LE Advertising Report */ |
| 644 | |
| 645 | /* If the controller supports the LE Create Connection |
| 646 | * command, enable the corresponding event. |
| 647 | */ |
| 648 | if (hdev->commands[26] & 0x10) |
| 649 | events[0] |= 0x01; /* LE Connection Complete */ |
| 650 | |
| 651 | /* If the controller supports the LE Connection Update |
| 652 | * command, enable the corresponding event. |
| 653 | */ |
| 654 | if (hdev->commands[27] & 0x04) |
| 655 | events[0] |= 0x04; /* LE Connection Update |
| 656 | * Complete |
| 657 | */ |
| 658 | |
| 659 | /* If the controller supports the LE Read Remote Used Features |
| 660 | * command, enable the corresponding event. |
| 661 | */ |
| 662 | if (hdev->commands[27] & 0x20) |
| 663 | events[0] |= 0x08; /* LE Read Remote Used |
| 664 | * Features Complete |
| 665 | */ |
| 666 | |
Marcel Holtmann | 5a34bd5 | 2014-12-05 16:20:15 +0100 | [diff] [blame] | 667 | /* If the controller supports the LE Read Local P-256 |
| 668 | * Public Key command, enable the corresponding event. |
| 669 | */ |
| 670 | if (hdev->commands[34] & 0x02) |
| 671 | events[0] |= 0x80; /* LE Read Local P-256 |
| 672 | * Public Key Complete |
| 673 | */ |
| 674 | |
| 675 | /* If the controller supports the LE Generate DHKey |
| 676 | * command, enable the corresponding event. |
| 677 | */ |
| 678 | if (hdev->commands[34] & 0x04) |
| 679 | events[1] |= 0x01; /* LE Generate DHKey Complete */ |
| 680 | |
Andre Guedes | 9193c6e | 2014-07-01 18:10:09 -0300 | [diff] [blame] | 681 | hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events), |
| 682 | events); |
| 683 | |
Marcel Holtmann | 15a49cc | 2014-07-12 23:20:50 +0200 | [diff] [blame] | 684 | if (hdev->commands[25] & 0x40) { |
| 685 | /* Read LE Advertising Channel TX Power */ |
| 686 | hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); |
| 687 | } |
| 688 | |
Marcel Holtmann | 2ab216a | 2015-11-01 09:39:48 +0100 | [diff] [blame] | 689 | if (hdev->commands[26] & 0x40) { |
| 690 | /* Read LE White List Size */ |
| 691 | hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, |
| 692 | 0, NULL); |
| 693 | } |
| 694 | |
| 695 | if (hdev->commands[26] & 0x80) { |
| 696 | /* Clear LE White List */ |
| 697 | hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL); |
| 698 | } |
| 699 | |
Marcel Holtmann | a9f6068 | 2014-12-20 16:28:39 +0100 | [diff] [blame] | 700 | if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) { |
| 701 | /* Read LE Maximum Data Length */ |
| 702 | hci_req_add(req, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL); |
| 703 | |
| 704 | /* Read LE Suggested Default Data Length */ |
| 705 | hci_req_add(req, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL); |
| 706 | } |
| 707 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 708 | hci_set_le_support(req); |
Andre Guedes | 9193c6e | 2014-07-01 18:10:09 -0300 | [diff] [blame] | 709 | } |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 710 | |
| 711 | /* Read features beyond page 1 if available */ |
| 712 | for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { |
| 713 | struct hci_cp_read_local_ext_features cp; |
| 714 | |
| 715 | cp.page = p; |
| 716 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 717 | sizeof(cp), &cp); |
| 718 | } |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 719 | |
| 720 | return 0; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 723 | static int hci_init4_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 724 | { |
| 725 | struct hci_dev *hdev = req->hdev; |
| 726 | |
Marcel Holtmann | 36f260c | 2015-01-12 22:47:22 -0800 | [diff] [blame] | 727 | /* Some Broadcom based Bluetooth controllers do not support the |
| 728 | * Delete Stored Link Key command. They are clearly indicating its |
| 729 | * absence in the bit mask of supported commands. |
| 730 | * |
| 731 | * Check the supported commands and only if the the command is marked |
| 732 | * as supported send it. If not supported assume that the controller |
| 733 | * does not have actual support for stored link keys which makes this |
| 734 | * command redundant anyway. |
| 735 | * |
| 736 | * Some controllers indicate that they support handling deleting |
| 737 | * stored link keys, but they don't. The quirk lets a driver |
| 738 | * just disable this command. |
| 739 | */ |
| 740 | if (hdev->commands[6] & 0x80 && |
| 741 | !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { |
| 742 | struct hci_cp_delete_stored_link_key cp; |
| 743 | |
| 744 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 745 | cp.delete_all = 0x01; |
| 746 | hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, |
| 747 | sizeof(cp), &cp); |
| 748 | } |
| 749 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 750 | /* Set event mask page 2 if the HCI command for it is supported */ |
| 751 | if (hdev->commands[22] & 0x04) |
| 752 | hci_set_event_mask_page_2(req); |
| 753 | |
Marcel Holtmann | 109e319 | 2014-07-23 19:24:56 +0200 | [diff] [blame] | 754 | /* Read local codec list if the HCI command is supported */ |
| 755 | if (hdev->commands[29] & 0x20) |
| 756 | hci_req_add(req, HCI_OP_READ_LOCAL_CODECS, 0, NULL); |
| 757 | |
Marcel Holtmann | f4fe73e | 2014-07-23 19:24:57 +0200 | [diff] [blame] | 758 | /* Get MWS transport configuration if the HCI command is supported */ |
| 759 | if (hdev->commands[30] & 0x08) |
| 760 | hci_req_add(req, HCI_OP_GET_MWS_TRANSPORT_CONFIG, 0, NULL); |
| 761 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 762 | /* Check for Synchronization Train support */ |
Marcel Holtmann | 53b834d2 | 2013-12-08 11:55:33 -0800 | [diff] [blame] | 763 | if (lmp_sync_train_capable(hdev)) |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 764 | hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); |
Marcel Holtmann | a6d0d69 | 2014-01-10 02:07:24 -0800 | [diff] [blame] | 765 | |
| 766 | /* Enable Secure Connections if supported and configured */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 767 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 768 | bredr_sc_enabled(hdev)) { |
Marcel Holtmann | a6d0d69 | 2014-01-10 02:07:24 -0800 | [diff] [blame] | 769 | u8 support = 0x01; |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 770 | |
Marcel Holtmann | a6d0d69 | 2014-01-10 02:07:24 -0800 | [diff] [blame] | 771 | hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT, |
| 772 | sizeof(support), &support); |
| 773 | } |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 774 | |
| 775 | return 0; |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 776 | } |
| 777 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 778 | static int __hci_init(struct hci_dev *hdev) |
| 779 | { |
| 780 | int err; |
| 781 | |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 782 | err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 783 | if (err < 0) |
| 784 | return err; |
| 785 | |
Marcel Holtmann | f640ee9 | 2015-10-08 12:35:42 +0200 | [diff] [blame] | 786 | if (hci_dev_test_flag(hdev, HCI_SETUP)) |
| 787 | hci_debugfs_create_basic(hdev); |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 788 | |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 789 | err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 790 | if (err < 0) |
| 791 | return err; |
| 792 | |
Johan Hedberg | 0af801b | 2015-02-17 15:05:21 +0200 | [diff] [blame] | 793 | /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode |
| 794 | * BR/EDR/LE type controllers. AMP controllers only need the |
| 795 | * first two stages of init. |
| 796 | */ |
| 797 | if (hdev->dev_type != HCI_BREDR) |
| 798 | return 0; |
| 799 | |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 800 | err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT, NULL); |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 801 | if (err < 0) |
| 802 | return err; |
| 803 | |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 804 | err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT, NULL); |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 805 | if (err < 0) |
| 806 | return err; |
| 807 | |
Marcel Holtmann | ec6cef9 | 2015-01-01 02:05:16 -0800 | [diff] [blame] | 808 | /* This function is only called when the controller is actually in |
| 809 | * configured state. When the controller is marked as unconfigured, |
| 810 | * this initialization procedure is not run. |
| 811 | * |
| 812 | * It means that it is possible that a controller runs through its |
| 813 | * setup phase and then discovers missing settings. If that is the |
| 814 | * case, then this function will not be called. It then will only |
| 815 | * be called during the config phase. |
| 816 | * |
| 817 | * So only when in setup phase or config phase, create the debugfs |
| 818 | * entries and register the SMP channels. |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 819 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 820 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && |
| 821 | !hci_dev_test_flag(hdev, HCI_CONFIG)) |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 822 | return 0; |
| 823 | |
Marcel Holtmann | 60c5f5f | 2014-12-20 16:05:13 +0100 | [diff] [blame] | 824 | hci_debugfs_create_common(hdev); |
| 825 | |
Marcel Holtmann | 71c3b60 | 2014-12-20 16:05:15 +0100 | [diff] [blame] | 826 | if (lmp_bredr_capable(hdev)) |
Marcel Holtmann | 60c5f5f | 2014-12-20 16:05:13 +0100 | [diff] [blame] | 827 | hci_debugfs_create_bredr(hdev); |
Marcel Holtmann | 2bfa353 | 2013-10-17 19:16:02 -0700 | [diff] [blame] | 828 | |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 829 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 60c5f5f | 2014-12-20 16:05:13 +0100 | [diff] [blame] | 830 | hci_debugfs_create_le(hdev); |
Marcel Holtmann | e7b8fc9 | 2013-10-17 11:45:09 -0700 | [diff] [blame] | 831 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 832 | return 0; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 835 | static int hci_init0_req(struct hci_request *req, unsigned long opt) |
Marcel Holtmann | 0ebca7d | 2014-07-05 10:48:02 +0200 | [diff] [blame] | 836 | { |
| 837 | struct hci_dev *hdev = req->hdev; |
| 838 | |
| 839 | BT_DBG("%s %ld", hdev->name, opt); |
| 840 | |
| 841 | /* Reset */ |
| 842 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) |
| 843 | hci_reset_req(req, 0); |
| 844 | |
| 845 | /* Read Local Version */ |
| 846 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
| 847 | |
| 848 | /* Read BD Address */ |
| 849 | if (hdev->set_bdaddr) |
| 850 | hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 851 | |
| 852 | return 0; |
Marcel Holtmann | 0ebca7d | 2014-07-05 10:48:02 +0200 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static int __hci_unconf_init(struct hci_dev *hdev) |
| 856 | { |
| 857 | int err; |
| 858 | |
Marcel Holtmann | cc78b44 | 2014-07-06 13:43:20 +0200 | [diff] [blame] | 859 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 860 | return 0; |
| 861 | |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 862 | err = __hci_req_sync(hdev, hci_init0_req, 0, HCI_INIT_TIMEOUT, NULL); |
Marcel Holtmann | 0ebca7d | 2014-07-05 10:48:02 +0200 | [diff] [blame] | 863 | if (err < 0) |
| 864 | return err; |
| 865 | |
Marcel Holtmann | f640ee9 | 2015-10-08 12:35:42 +0200 | [diff] [blame] | 866 | if (hci_dev_test_flag(hdev, HCI_SETUP)) |
| 867 | hci_debugfs_create_basic(hdev); |
| 868 | |
Marcel Holtmann | 0ebca7d | 2014-07-05 10:48:02 +0200 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 872 | static int hci_scan_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
| 874 | __u8 scan = opt; |
| 875 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 876 | BT_DBG("%s %x", req->hdev->name, scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | /* Inquiry and Page scans */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 879 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 880 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 883 | static int hci_auth_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | { |
| 885 | __u8 auth = opt; |
| 886 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 887 | BT_DBG("%s %x", req->hdev->name, auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | /* Authentication */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 890 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 891 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 894 | static int hci_encrypt_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
| 896 | __u8 encrypt = opt; |
| 897 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 898 | BT_DBG("%s %x", req->hdev->name, encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 900 | /* Encryption */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 901 | hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 902 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 905 | static int hci_linkpol_req(struct hci_request *req, unsigned long opt) |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 906 | { |
| 907 | __le16 policy = cpu_to_le16(opt); |
| 908 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 909 | BT_DBG("%s %x", req->hdev->name, policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 910 | |
| 911 | /* Default link policy */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 912 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 913 | return 0; |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 914 | } |
| 915 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 916 | /* Get HCI device by index. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | * Device is held on return. */ |
| 918 | struct hci_dev *hci_dev_get(int index) |
| 919 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 920 | struct hci_dev *hdev = NULL, *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
| 922 | BT_DBG("%d", index); |
| 923 | |
| 924 | if (index < 0) |
| 925 | return NULL; |
| 926 | |
| 927 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 928 | list_for_each_entry(d, &hci_dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | if (d->id == index) { |
| 930 | hdev = hci_dev_hold(d); |
| 931 | break; |
| 932 | } |
| 933 | } |
| 934 | read_unlock(&hci_dev_list_lock); |
| 935 | return hdev; |
| 936 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | /* ---- Inquiry support ---- */ |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 939 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 940 | bool hci_discovery_active(struct hci_dev *hdev) |
| 941 | { |
| 942 | struct discovery_state *discov = &hdev->discovery; |
| 943 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 944 | switch (discov->state) { |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 945 | case DISCOVERY_FINDING: |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 946 | case DISCOVERY_RESOLVING: |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 947 | return true; |
| 948 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 949 | default: |
| 950 | return false; |
| 951 | } |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 954 | void hci_discovery_set_state(struct hci_dev *hdev, int state) |
| 955 | { |
Johan Hedberg | bb3e0a3 | 2014-07-07 13:24:58 +0300 | [diff] [blame] | 956 | int old_state = hdev->discovery.state; |
| 957 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 958 | BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); |
| 959 | |
Johan Hedberg | bb3e0a3 | 2014-07-07 13:24:58 +0300 | [diff] [blame] | 960 | if (old_state == state) |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 961 | return; |
| 962 | |
Johan Hedberg | bb3e0a3 | 2014-07-07 13:24:58 +0300 | [diff] [blame] | 963 | hdev->discovery.state = state; |
| 964 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 965 | switch (state) { |
| 966 | case DISCOVERY_STOPPED: |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 967 | hci_update_background_scan(hdev); |
| 968 | |
Johan Hedberg | bb3e0a3 | 2014-07-07 13:24:58 +0300 | [diff] [blame] | 969 | if (old_state != DISCOVERY_STARTING) |
Andre Guedes | 7b99b65 | 2012-02-13 15:41:02 -0300 | [diff] [blame] | 970 | mgmt_discovering(hdev, 0); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 971 | break; |
| 972 | case DISCOVERY_STARTING: |
| 973 | break; |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 974 | case DISCOVERY_FINDING: |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 975 | mgmt_discovering(hdev, 1); |
| 976 | break; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 977 | case DISCOVERY_RESOLVING: |
| 978 | break; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 979 | case DISCOVERY_STOPPING: |
| 980 | break; |
| 981 | } |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 984 | void hci_inquiry_cache_flush(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 986 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 987 | struct inquiry_entry *p, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 989 | list_for_each_entry_safe(p, n, &cache->all, all) { |
| 990 | list_del(&p->all); |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 991 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 993 | |
| 994 | INIT_LIST_HEAD(&cache->unknown); |
| 995 | INIT_LIST_HEAD(&cache->resolve); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 998 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, |
| 999 | bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1001 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | struct inquiry_entry *e; |
| 1003 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1004 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1006 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1008 | return e; |
| 1009 | } |
| 1010 | |
| 1011 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1014 | 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] | 1015 | bdaddr_t *bdaddr) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1016 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1017 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1018 | struct inquiry_entry *e; |
| 1019 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1020 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1021 | |
| 1022 | list_for_each_entry(e, &cache->unknown, list) { |
| 1023 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 1024 | return e; |
| 1025 | } |
| 1026 | |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1030 | 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] | 1031 | bdaddr_t *bdaddr, |
| 1032 | int state) |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1033 | { |
| 1034 | struct discovery_state *cache = &hdev->discovery; |
| 1035 | struct inquiry_entry *e; |
| 1036 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1037 | BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1038 | |
| 1039 | list_for_each_entry(e, &cache->resolve, list) { |
| 1040 | if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) |
| 1041 | return e; |
| 1042 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 1043 | return e; |
| 1044 | } |
| 1045 | |
| 1046 | return NULL; |
| 1047 | } |
| 1048 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1049 | void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1050 | struct inquiry_entry *ie) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1051 | { |
| 1052 | struct discovery_state *cache = &hdev->discovery; |
| 1053 | struct list_head *pos = &cache->resolve; |
| 1054 | struct inquiry_entry *p; |
| 1055 | |
| 1056 | list_del(&ie->list); |
| 1057 | |
| 1058 | list_for_each_entry(p, &cache->resolve, list) { |
| 1059 | if (p->name_state != NAME_PENDING && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1060 | abs(p->data.rssi) >= abs(ie->data.rssi)) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1061 | break; |
| 1062 | pos = &p->list; |
| 1063 | } |
| 1064 | |
| 1065 | list_add(&ie->list, pos); |
| 1066 | } |
| 1067 | |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1068 | u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, |
| 1069 | bool name_known) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1071 | struct discovery_state *cache = &hdev->discovery; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1072 | struct inquiry_entry *ie; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1073 | u32 flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1075 | BT_DBG("cache %p, %pMR", cache, &data->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 1077 | hci_remove_remote_oob_data(hdev, &data->bdaddr, BDADDR_BREDR); |
Szymon Janc | 2b2fec4 | 2012-11-20 11:38:54 +0100 | [diff] [blame] | 1078 | |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1079 | if (!data->ssp_mode) |
| 1080 | flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1081 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1082 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1083 | if (ie) { |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1084 | if (!ie->data.ssp_mode) |
| 1085 | flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1086 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1087 | if (ie->name_state == NAME_NEEDED && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1088 | data->rssi != ie->data.rssi) { |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1089 | ie->data.rssi = data->rssi; |
| 1090 | hci_inquiry_cache_update_resolve(hdev, ie); |
| 1091 | } |
| 1092 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1093 | goto update; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1094 | } |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1095 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1096 | /* Entry not in the cache. Add new one. */ |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 1097 | ie = kzalloc(sizeof(*ie), GFP_KERNEL); |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1098 | if (!ie) { |
| 1099 | flags |= MGMT_DEV_FOUND_CONFIRM_NAME; |
| 1100 | goto done; |
| 1101 | } |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1102 | |
| 1103 | list_add(&ie->all, &cache->all); |
| 1104 | |
| 1105 | if (name_known) { |
| 1106 | ie->name_state = NAME_KNOWN; |
| 1107 | } else { |
| 1108 | ie->name_state = NAME_NOT_KNOWN; |
| 1109 | list_add(&ie->list, &cache->unknown); |
| 1110 | } |
| 1111 | |
| 1112 | update: |
| 1113 | if (name_known && ie->name_state != NAME_KNOWN && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1114 | ie->name_state != NAME_PENDING) { |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1115 | ie->name_state = NAME_KNOWN; |
| 1116 | list_del(&ie->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1119 | memcpy(&ie->data, data, sizeof(*data)); |
| 1120 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | cache->timestamp = jiffies; |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1122 | |
| 1123 | if (ie->name_state == NAME_NOT_KNOWN) |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1124 | flags |= MGMT_DEV_FOUND_CONFIRM_NAME; |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1125 | |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 1126 | done: |
| 1127 | return flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 1131 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1132 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 1134 | struct inquiry_entry *e; |
| 1135 | int copied = 0; |
| 1136 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1137 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | struct inquiry_data *data = &e->data; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1139 | |
| 1140 | if (copied >= num) |
| 1141 | break; |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | bacpy(&info->bdaddr, &data->bdaddr); |
| 1144 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 1145 | info->pscan_period_mode = data->pscan_period_mode; |
| 1146 | info->pscan_mode = data->pscan_mode; |
| 1147 | memcpy(info->dev_class, data->dev_class, 3); |
| 1148 | info->clock_offset = data->clock_offset; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | info++; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1151 | copied++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | BT_DBG("cache %p, copied %d", cache, copied); |
| 1155 | return copied; |
| 1156 | } |
| 1157 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 1158 | static int hci_inq_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | { |
| 1160 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1161 | struct hci_dev *hdev = req->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | struct hci_cp_inquiry cp; |
| 1163 | |
| 1164 | BT_DBG("%s", hdev->name); |
| 1165 | |
| 1166 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 1167 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
| 1169 | /* Start Inquiry */ |
| 1170 | memcpy(&cp.lap, &ir->lap, 3); |
| 1171 | cp.length = ir->length; |
| 1172 | cp.num_rsp = ir->num_rsp; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1173 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 1174 | |
| 1175 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | int hci_inquiry(void __user *arg) |
| 1179 | { |
| 1180 | __u8 __user *ptr = arg; |
| 1181 | struct hci_inquiry_req ir; |
| 1182 | struct hci_dev *hdev; |
| 1183 | int err = 0, do_inquiry = 0, max_rsp; |
| 1184 | long timeo; |
| 1185 | __u8 *buf; |
| 1186 | |
| 1187 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 1188 | return -EFAULT; |
| 1189 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 1190 | hdev = hci_dev_get(ir.dev_id); |
| 1191 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | return -ENODEV; |
| 1193 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1194 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1195 | err = -EBUSY; |
| 1196 | goto done; |
| 1197 | } |
| 1198 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1199 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1200 | err = -EOPNOTSUPP; |
| 1201 | goto done; |
| 1202 | } |
| 1203 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1204 | if (hdev->dev_type != HCI_BREDR) { |
| 1205 | err = -EOPNOTSUPP; |
| 1206 | goto done; |
| 1207 | } |
| 1208 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1209 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1210 | err = -EOPNOTSUPP; |
| 1211 | goto done; |
| 1212 | } |
| 1213 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1214 | hci_dev_lock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1215 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1216 | inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1217 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | do_inquiry = 1; |
| 1219 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1220 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1222 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1223 | |
| 1224 | if (do_inquiry) { |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1225 | err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1226 | timeo, NULL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1227 | if (err < 0) |
| 1228 | goto done; |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1229 | |
| 1230 | /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is |
| 1231 | * cleared). If it is interrupted by a signal, return -EINTR. |
| 1232 | */ |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1233 | if (wait_on_bit(&hdev->flags, HCI_INQUIRY, |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1234 | TASK_INTERRUPTIBLE)) |
| 1235 | return -EINTR; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1238 | /* for unlimited number of responses we will use buffer with |
| 1239 | * 255 entries |
| 1240 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 1242 | |
| 1243 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 1244 | * copy it to the user space. |
| 1245 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 1246 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1247 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | err = -ENOMEM; |
| 1249 | goto done; |
| 1250 | } |
| 1251 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1252 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1254 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
| 1256 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 1257 | |
| 1258 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 1259 | ptr += sizeof(ir); |
| 1260 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1261 | ir.num_rsp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1263 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | err = -EFAULT; |
| 1265 | |
| 1266 | kfree(buf); |
| 1267 | |
| 1268 | done: |
| 1269 | hci_dev_put(hdev); |
| 1270 | return err; |
| 1271 | } |
| 1272 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1273 | static int hci_dev_do_open(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | int ret = 0; |
| 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | BT_DBG("%s %p", hdev->name, hdev); |
| 1278 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1279 | hci_req_sync_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1281 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 1282 | ret = -ENODEV; |
| 1283 | goto done; |
| 1284 | } |
| 1285 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1286 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && |
| 1287 | !hci_dev_test_flag(hdev, HCI_CONFIG)) { |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1288 | /* Check for rfkill but allow the HCI setup stage to |
| 1289 | * proceed (which in itself doesn't cause any RF activity). |
| 1290 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1291 | if (hci_dev_test_flag(hdev, HCI_RFKILLED)) { |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1292 | ret = -ERFKILL; |
| 1293 | goto done; |
| 1294 | } |
| 1295 | |
| 1296 | /* Check for valid public address or a configured static |
| 1297 | * random adddress, but let the HCI setup proceed to |
| 1298 | * be able to determine if there is a public address |
| 1299 | * or not. |
| 1300 | * |
Marcel Holtmann | c6beca0 | 2014-02-17 09:21:19 -0800 | [diff] [blame] | 1301 | * In case of user channel usage, it is not important |
| 1302 | * if a public address or static random address is |
| 1303 | * available. |
| 1304 | * |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1305 | * This check is only valid for BR/EDR controllers |
| 1306 | * since AMP controllers do not have an address. |
| 1307 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1308 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && |
Marcel Holtmann | c6beca0 | 2014-02-17 09:21:19 -0800 | [diff] [blame] | 1309 | hdev->dev_type == HCI_BREDR && |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1310 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 1311 | !bacmp(&hdev->static_addr, BDADDR_ANY)) { |
| 1312 | ret = -EADDRNOTAVAIL; |
| 1313 | goto done; |
| 1314 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 1318 | ret = -EALREADY; |
| 1319 | goto done; |
| 1320 | } |
| 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | if (hdev->open(hdev)) { |
| 1323 | ret = -EIO; |
| 1324 | goto done; |
| 1325 | } |
| 1326 | |
Marcel Holtmann | e9ca8bf | 2015-10-04 23:34:02 +0200 | [diff] [blame] | 1327 | set_bit(HCI_RUNNING, &hdev->flags); |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 1328 | hci_sock_dev_event(hdev, HCI_DEV_OPEN); |
Marcel Holtmann | 4a3f95b | 2015-10-04 23:34:00 +0200 | [diff] [blame] | 1329 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1330 | atomic_set(&hdev->cmd_cnt, 1); |
| 1331 | set_bit(HCI_INIT, &hdev->flags); |
| 1332 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1333 | if (hci_dev_test_flag(hdev, HCI_SETUP)) { |
Marcel Holtmann | e131d74 | 2015-10-20 02:30:47 +0200 | [diff] [blame] | 1334 | hci_sock_dev_event(hdev, HCI_DEV_SETUP); |
| 1335 | |
Marcel Holtmann | af202f8 | 2014-07-04 17:23:34 +0200 | [diff] [blame] | 1336 | if (hdev->setup) |
| 1337 | ret = hdev->setup(hdev); |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1338 | |
Marcel Holtmann | af202f8 | 2014-07-04 17:23:34 +0200 | [diff] [blame] | 1339 | /* The transport driver can set these quirks before |
| 1340 | * creating the HCI device or in its setup callback. |
| 1341 | * |
| 1342 | * In case any of them is set, the controller has to |
| 1343 | * start up as unconfigured. |
| 1344 | */ |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 1345 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 1346 | test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1347 | hci_dev_set_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 0ebca7d | 2014-07-05 10:48:02 +0200 | [diff] [blame] | 1348 | |
| 1349 | /* For an unconfigured controller it is required to |
| 1350 | * read at least the version information provided by |
| 1351 | * the Read Local Version Information command. |
| 1352 | * |
| 1353 | * If the set_bdaddr driver callback is provided, then |
| 1354 | * also the original Bluetooth public device address |
| 1355 | * will be read using the Read BD Address command. |
| 1356 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1357 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 0ebca7d | 2014-07-05 10:48:02 +0200 | [diff] [blame] | 1358 | ret = __hci_unconf_init(hdev); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1361 | if (hci_dev_test_flag(hdev, HCI_CONFIG)) { |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 1362 | /* If public address change is configured, ensure that |
| 1363 | * the address gets programmed. If the driver does not |
| 1364 | * support changing the public address, fail the power |
| 1365 | * on procedure. |
| 1366 | */ |
| 1367 | if (bacmp(&hdev->public_addr, BDADDR_ANY) && |
| 1368 | hdev->set_bdaddr) |
Marcel Holtmann | 24c457e | 2014-07-02 00:53:47 +0200 | [diff] [blame] | 1369 | ret = hdev->set_bdaddr(hdev, &hdev->public_addr); |
| 1370 | else |
| 1371 | ret = -EADDRNOTAVAIL; |
| 1372 | } |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1373 | |
| 1374 | if (!ret) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1375 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && |
Marcel Holtmann | 98a63aa | 2015-10-20 23:25:42 +0200 | [diff] [blame] | 1376 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1377 | ret = __hci_init(hdev); |
Marcel Holtmann | 98a63aa | 2015-10-20 23:25:42 +0200 | [diff] [blame] | 1378 | if (!ret && hdev->post_init) |
| 1379 | ret = hdev->post_init(hdev); |
| 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Marcel Holtmann | 7e995b9 | 2015-10-17 16:00:26 +0200 | [diff] [blame] | 1383 | /* If the HCI Reset command is clearing all diagnostic settings, |
| 1384 | * then they need to be reprogrammed after the init procedure |
| 1385 | * completed. |
| 1386 | */ |
| 1387 | if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && |
| 1388 | hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag) |
| 1389 | ret = hdev->set_diag(hdev, true); |
| 1390 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 1391 | clear_bit(HCI_INIT, &hdev->flags); |
| 1392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | if (!ret) { |
| 1394 | hci_dev_hold(hdev); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1395 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | set_bit(HCI_UP, &hdev->flags); |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 1397 | hci_sock_dev_event(hdev, HCI_DEV_UP); |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1398 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && |
| 1399 | !hci_dev_test_flag(hdev, HCI_CONFIG) && |
| 1400 | !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && |
| 1401 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 1402 | hci_dev_test_flag(hdev, HCI_MGMT) && |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 1403 | hdev->dev_type == HCI_BREDR) { |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 1404 | ret = __hci_req_hci_power_on(hdev); |
| 1405 | mgmt_power_on(hdev, ret); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1406 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1407 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | /* Init failed, cleanup */ |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1409 | flush_work(&hdev->tx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1410 | flush_work(&hdev->cmd_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1411 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | skb_queue_purge(&hdev->cmd_q); |
| 1414 | skb_queue_purge(&hdev->rx_q); |
| 1415 | |
| 1416 | if (hdev->flush) |
| 1417 | hdev->flush(hdev); |
| 1418 | |
| 1419 | if (hdev->sent_cmd) { |
| 1420 | kfree_skb(hdev->sent_cmd); |
| 1421 | hdev->sent_cmd = NULL; |
| 1422 | } |
| 1423 | |
Marcel Holtmann | e9ca8bf | 2015-10-04 23:34:02 +0200 | [diff] [blame] | 1424 | clear_bit(HCI_RUNNING, &hdev->flags); |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 1425 | hci_sock_dev_event(hdev, HCI_DEV_CLOSE); |
Marcel Holtmann | 4a3f95b | 2015-10-04 23:34:00 +0200 | [diff] [blame] | 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | hdev->close(hdev); |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1428 | hdev->flags &= BIT(HCI_RAW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | done: |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1432 | hci_req_sync_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | return ret; |
| 1434 | } |
| 1435 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1436 | /* ---- HCI ioctl helpers ---- */ |
| 1437 | |
| 1438 | int hci_dev_open(__u16 dev) |
| 1439 | { |
| 1440 | struct hci_dev *hdev; |
| 1441 | int err; |
| 1442 | |
| 1443 | hdev = hci_dev_get(dev); |
| 1444 | if (!hdev) |
| 1445 | return -ENODEV; |
| 1446 | |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 1447 | /* Devices that are marked as unconfigured can only be powered |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1448 | * up as user channel. Trying to bring them up as normal devices |
| 1449 | * will result into a failure. Only user channel operation is |
| 1450 | * possible. |
| 1451 | * |
| 1452 | * When this function is called for a user channel, the flag |
| 1453 | * HCI_USER_CHANNEL will be set first before attempting to |
| 1454 | * open the device. |
| 1455 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1456 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && |
| 1457 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1458 | err = -EOPNOTSUPP; |
| 1459 | goto done; |
| 1460 | } |
| 1461 | |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1462 | /* We need to ensure that no other power on/off work is pending |
| 1463 | * before proceeding to call hci_dev_do_open. This is |
| 1464 | * particularly important if the setup procedure has not yet |
| 1465 | * completed. |
| 1466 | */ |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1467 | if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1468 | cancel_delayed_work(&hdev->power_off); |
| 1469 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 1470 | /* After this call it is guaranteed that the setup procedure |
| 1471 | * has finished. This means that error conditions like RFKILL |
| 1472 | * or no valid public or static random address apply. |
| 1473 | */ |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 1474 | flush_workqueue(hdev->req_workqueue); |
| 1475 | |
Marcel Holtmann | 12aa4f0 | 2014-07-10 15:25:22 +0200 | [diff] [blame] | 1476 | /* For controllers not using the management interface and that |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1477 | * are brought up using legacy ioctl, set the HCI_BONDABLE bit |
Marcel Holtmann | 12aa4f0 | 2014-07-10 15:25:22 +0200 | [diff] [blame] | 1478 | * so that pairing works for them. Once the management interface |
| 1479 | * is in use this bit will be cleared again and userspace has |
| 1480 | * to explicitly enable it. |
| 1481 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1482 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && |
| 1483 | !hci_dev_test_flag(hdev, HCI_MGMT)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1484 | hci_dev_set_flag(hdev, HCI_BONDABLE); |
Marcel Holtmann | 12aa4f0 | 2014-07-10 15:25:22 +0200 | [diff] [blame] | 1485 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1486 | err = hci_dev_do_open(hdev); |
| 1487 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1488 | done: |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1489 | hci_dev_put(hdev); |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 1490 | return err; |
| 1491 | } |
| 1492 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 1493 | /* This function requires the caller holds hdev->lock */ |
| 1494 | static void hci_pend_le_actions_clear(struct hci_dev *hdev) |
| 1495 | { |
| 1496 | struct hci_conn_params *p; |
| 1497 | |
Johan Hedberg | f161dd4 | 2014-08-15 21:06:54 +0300 | [diff] [blame] | 1498 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 1499 | if (p->conn) { |
| 1500 | hci_conn_drop(p->conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 1501 | hci_conn_put(p->conn); |
Johan Hedberg | f161dd4 | 2014-08-15 21:06:54 +0300 | [diff] [blame] | 1502 | p->conn = NULL; |
| 1503 | } |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 1504 | list_del_init(&p->action); |
Johan Hedberg | f161dd4 | 2014-08-15 21:06:54 +0300 | [diff] [blame] | 1505 | } |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 1506 | |
| 1507 | BT_DBG("All LE pending actions cleared"); |
| 1508 | } |
| 1509 | |
Simon Fels | 6b3cc1d | 2015-09-02 12:10:12 +0200 | [diff] [blame] | 1510 | int hci_dev_do_close(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | { |
Marcel Holtmann | acc649c | 2015-10-08 01:53:55 +0200 | [diff] [blame] | 1512 | bool auto_off; |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | BT_DBG("%s %p", hdev->name, hdev); |
| 1515 | |
Gabriele Mazzotta | d24d814 | 2015-04-26 20:51:50 +0200 | [diff] [blame] | 1516 | if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && |
Loic Poulain | 867146a | 2015-06-09 11:46:30 +0200 | [diff] [blame] | 1517 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && |
Gabriele Mazzotta | d24d814 | 2015-04-26 20:51:50 +0200 | [diff] [blame] | 1518 | test_bit(HCI_UP, &hdev->flags)) { |
Tedd Ho-Jeong An | a44fecb | 2015-02-13 09:20:50 -0800 | [diff] [blame] | 1519 | /* Execute vendor specific shutdown routine */ |
| 1520 | if (hdev->shutdown) |
| 1521 | hdev->shutdown(hdev); |
| 1522 | } |
| 1523 | |
Vinicius Costa Gomes | 78c04c0 | 2012-09-14 16:34:46 -0300 | [diff] [blame] | 1524 | cancel_delayed_work(&hdev->power_off); |
| 1525 | |
Johan Hedberg | 7df0f73 | 2015-11-12 15:15:00 +0200 | [diff] [blame] | 1526 | hci_request_cancel_all(hdev); |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1527 | hci_req_sync_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
| 1529 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 1530 | cancel_delayed_work_sync(&hdev->cmd_timer); |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1531 | hci_req_sync_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | return 0; |
| 1533 | } |
| 1534 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 1535 | /* Flush RX and TX works */ |
| 1536 | flush_work(&hdev->tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 1537 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1539 | if (hdev->discov_timeout > 0) { |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1540 | hdev->discov_timeout = 0; |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1541 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1542 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1545 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1546 | cancel_delayed_work(&hdev->service_cache); |
| 1547 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1548 | if (hci_dev_test_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 4518bb0 | 2014-02-24 20:35:07 +0200 | [diff] [blame] | 1549 | cancel_delayed_work_sync(&hdev->rpa_expired); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 1550 | |
Johan Hedberg | 76727c0 | 2014-11-18 09:00:14 +0200 | [diff] [blame] | 1551 | /* Avoid potential lockdep warnings from the *_flush() calls by |
| 1552 | * ensuring the workqueue is empty up front. |
| 1553 | */ |
| 1554 | drain_workqueue(hdev->workqueue); |
| 1555 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1556 | hci_dev_lock(hdev); |
Johan Hedberg | 1aeb9c6 | 2014-12-11 21:45:46 +0200 | [diff] [blame] | 1557 | |
Johan Hedberg | 8f502f8 | 2015-01-28 19:56:02 +0200 | [diff] [blame] | 1558 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 1559 | |
Marcel Holtmann | acc649c | 2015-10-08 01:53:55 +0200 | [diff] [blame] | 1560 | auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); |
| 1561 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 1562 | if (!auto_off && hdev->dev_type == HCI_BREDR && |
| 1563 | hci_dev_test_flag(hdev, HCI_MGMT)) |
| 1564 | __mgmt_power_off(hdev); |
Johan Hedberg | 1aeb9c6 | 2014-12-11 21:45:46 +0200 | [diff] [blame] | 1565 | |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1566 | hci_inquiry_cache_flush(hdev); |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 1567 | hci_pend_le_actions_clear(hdev); |
Johan Hedberg | f161dd4 | 2014-08-15 21:06:54 +0300 | [diff] [blame] | 1568 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1569 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
Marcel Holtmann | 64dae96 | 2015-01-28 14:10:28 -0800 | [diff] [blame] | 1571 | smp_unregister(hdev); |
| 1572 | |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 1573 | hci_sock_dev_event(hdev, HCI_DEV_DOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
| 1575 | if (hdev->flush) |
| 1576 | hdev->flush(hdev); |
| 1577 | |
| 1578 | /* Reset device */ |
| 1579 | skb_queue_purge(&hdev->cmd_q); |
| 1580 | atomic_set(&hdev->cmd_cnt, 1); |
Marcel Holtmann | acc649c | 2015-10-08 01:53:55 +0200 | [diff] [blame] | 1581 | if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) && |
| 1582 | !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1584 | __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | clear_bit(HCI_INIT, &hdev->flags); |
| 1586 | } |
| 1587 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 1588 | /* flush cmd work */ |
| 1589 | flush_work(&hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
| 1591 | /* Drop queues */ |
| 1592 | skb_queue_purge(&hdev->rx_q); |
| 1593 | skb_queue_purge(&hdev->cmd_q); |
| 1594 | skb_queue_purge(&hdev->raw_q); |
| 1595 | |
| 1596 | /* Drop last sent command */ |
| 1597 | if (hdev->sent_cmd) { |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 1598 | cancel_delayed_work_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | kfree_skb(hdev->sent_cmd); |
| 1600 | hdev->sent_cmd = NULL; |
| 1601 | } |
| 1602 | |
Marcel Holtmann | e9ca8bf | 2015-10-04 23:34:02 +0200 | [diff] [blame] | 1603 | clear_bit(HCI_RUNNING, &hdev->flags); |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 1604 | hci_sock_dev_event(hdev, HCI_DEV_CLOSE); |
Marcel Holtmann | 4a3f95b | 2015-10-04 23:34:00 +0200 | [diff] [blame] | 1605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | /* After this point our queues are empty |
| 1607 | * and no tasks are scheduled. */ |
| 1608 | hdev->close(hdev); |
| 1609 | |
Johan Hedberg | 35b973c | 2013-03-15 17:06:59 -0500 | [diff] [blame] | 1610 | /* Clear flags */ |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1611 | hdev->flags &= BIT(HCI_RAW); |
Marcel Holtmann | eacb44d | 2015-03-13 09:04:17 -0700 | [diff] [blame] | 1612 | hci_dev_clear_volatile_flags(hdev); |
Johan Hedberg | 35b973c | 2013-03-15 17:06:59 -0500 | [diff] [blame] | 1613 | |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1614 | /* Controller radio is available but is currently powered down */ |
Marcel Holtmann | 536619e | 2013-10-05 11:47:45 -0700 | [diff] [blame] | 1615 | hdev->amp_status = AMP_STATUS_POWERED_DOWN; |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 1616 | |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1617 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
Johan Hedberg | 09b3c3f | 2012-02-22 22:01:41 +0200 | [diff] [blame] | 1618 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); |
Marcel Holtmann | 7a4cd51 | 2014-02-19 19:52:13 -0800 | [diff] [blame] | 1619 | bacpy(&hdev->random_addr, BDADDR_ANY); |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 1620 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1621 | hci_req_sync_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | hci_dev_put(hdev); |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | int hci_dev_close(__u16 dev) |
| 1628 | { |
| 1629 | struct hci_dev *hdev; |
| 1630 | int err; |
| 1631 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1632 | hdev = hci_dev_get(dev); |
| 1633 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | return -ENODEV; |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1635 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1636 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1637 | err = -EBUSY; |
| 1638 | goto done; |
| 1639 | } |
| 1640 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1641 | if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1642 | cancel_delayed_work(&hdev->power_off); |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | err = hci_dev_do_close(hdev); |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 1645 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1646 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | hci_dev_put(hdev); |
| 1648 | return err; |
| 1649 | } |
| 1650 | |
Marcel Holtmann | 5c91249 | 2015-01-28 11:53:05 -0800 | [diff] [blame] | 1651 | static int hci_dev_do_reset(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | { |
Marcel Holtmann | 5c91249 | 2015-01-28 11:53:05 -0800 | [diff] [blame] | 1653 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
Marcel Holtmann | 5c91249 | 2015-01-28 11:53:05 -0800 | [diff] [blame] | 1655 | BT_DBG("%s %p", hdev->name, hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1657 | hci_req_sync_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | /* Drop queues */ |
| 1660 | skb_queue_purge(&hdev->rx_q); |
| 1661 | skb_queue_purge(&hdev->cmd_q); |
| 1662 | |
Johan Hedberg | 76727c0 | 2014-11-18 09:00:14 +0200 | [diff] [blame] | 1663 | /* Avoid potential lockdep warnings from the *_flush() calls by |
| 1664 | * ensuring the workqueue is empty up front. |
| 1665 | */ |
| 1666 | drain_workqueue(hdev->workqueue); |
| 1667 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1668 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1669 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1671 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
| 1673 | if (hdev->flush) |
| 1674 | hdev->flush(hdev); |
| 1675 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1676 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 1677 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1679 | ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 1681 | hci_req_sync_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | return ret; |
| 1683 | } |
| 1684 | |
Marcel Holtmann | 5c91249 | 2015-01-28 11:53:05 -0800 | [diff] [blame] | 1685 | int hci_dev_reset(__u16 dev) |
| 1686 | { |
| 1687 | struct hci_dev *hdev; |
| 1688 | int err; |
| 1689 | |
| 1690 | hdev = hci_dev_get(dev); |
| 1691 | if (!hdev) |
| 1692 | return -ENODEV; |
| 1693 | |
| 1694 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1695 | err = -ENETDOWN; |
| 1696 | goto done; |
| 1697 | } |
| 1698 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1699 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 5c91249 | 2015-01-28 11:53:05 -0800 | [diff] [blame] | 1700 | err = -EBUSY; |
| 1701 | goto done; |
| 1702 | } |
| 1703 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1704 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 5c91249 | 2015-01-28 11:53:05 -0800 | [diff] [blame] | 1705 | err = -EOPNOTSUPP; |
| 1706 | goto done; |
| 1707 | } |
| 1708 | |
| 1709 | err = hci_dev_do_reset(hdev); |
| 1710 | |
| 1711 | done: |
| 1712 | hci_dev_put(hdev); |
| 1713 | return err; |
| 1714 | } |
| 1715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | int hci_dev_reset_stat(__u16 dev) |
| 1717 | { |
| 1718 | struct hci_dev *hdev; |
| 1719 | int ret = 0; |
| 1720 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1721 | hdev = hci_dev_get(dev); |
| 1722 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return -ENODEV; |
| 1724 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1725 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1726 | ret = -EBUSY; |
| 1727 | goto done; |
| 1728 | } |
| 1729 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1730 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1731 | ret = -EOPNOTSUPP; |
| 1732 | goto done; |
| 1733 | } |
| 1734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 1736 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1737 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | hci_dev_put(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | return ret; |
| 1740 | } |
| 1741 | |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1742 | static void hci_update_scan_state(struct hci_dev *hdev, u8 scan) |
| 1743 | { |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1744 | bool conn_changed, discov_changed; |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1745 | |
| 1746 | BT_DBG("%s scan 0x%02x", hdev->name, scan); |
| 1747 | |
| 1748 | if ((scan & SCAN_PAGE)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1749 | conn_changed = !hci_dev_test_and_set_flag(hdev, |
| 1750 | HCI_CONNECTABLE); |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1751 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1752 | conn_changed = hci_dev_test_and_clear_flag(hdev, |
| 1753 | HCI_CONNECTABLE); |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1754 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1755 | if ((scan & SCAN_INQUIRY)) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1756 | discov_changed = !hci_dev_test_and_set_flag(hdev, |
| 1757 | HCI_DISCOVERABLE); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1758 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1759 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1760 | discov_changed = hci_dev_test_and_clear_flag(hdev, |
| 1761 | HCI_DISCOVERABLE); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1762 | } |
| 1763 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1764 | if (!hci_dev_test_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1765 | return; |
| 1766 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1767 | if (conn_changed || discov_changed) { |
| 1768 | /* In case this was disabled through mgmt */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1769 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1770 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1771 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1772 | hci_req_update_adv_data(hdev, hdev->cur_adv_instance); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1773 | |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1774 | mgmt_new_settings(hdev); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1775 | } |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1776 | } |
| 1777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 1779 | { |
| 1780 | struct hci_dev *hdev; |
| 1781 | struct hci_dev_req dr; |
| 1782 | int err = 0; |
| 1783 | |
| 1784 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 1785 | return -EFAULT; |
| 1786 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1787 | hdev = hci_dev_get(dr.dev_id); |
| 1788 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | return -ENODEV; |
| 1790 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1791 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1792 | err = -EBUSY; |
| 1793 | goto done; |
| 1794 | } |
| 1795 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1796 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 1797 | err = -EOPNOTSUPP; |
| 1798 | goto done; |
| 1799 | } |
| 1800 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1801 | if (hdev->dev_type != HCI_BREDR) { |
| 1802 | err = -EOPNOTSUPP; |
| 1803 | goto done; |
| 1804 | } |
| 1805 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1806 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1807 | err = -EOPNOTSUPP; |
| 1808 | goto done; |
| 1809 | } |
| 1810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | switch (cmd) { |
| 1812 | case HCISETAUTH: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1813 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1814 | HCI_INIT_TIMEOUT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | break; |
| 1816 | |
| 1817 | case HCISETENCRYPT: |
| 1818 | if (!lmp_encrypt_capable(hdev)) { |
| 1819 | err = -EOPNOTSUPP; |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 1824 | /* Auth must be enabled first */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1825 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1826 | HCI_INIT_TIMEOUT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | if (err) |
| 1828 | break; |
| 1829 | } |
| 1830 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1831 | err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1832 | HCI_INIT_TIMEOUT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | break; |
| 1834 | |
| 1835 | case HCISETSCAN: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1836 | err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1837 | HCI_INIT_TIMEOUT, NULL); |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1838 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1839 | /* Ensure that the connectable and discoverable states |
| 1840 | * get correctly modified as this was a non-mgmt change. |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1841 | */ |
Johan Hedberg | 123abc0 | 2014-07-10 12:09:07 +0300 | [diff] [blame] | 1842 | if (!err) |
| 1843 | hci_update_scan_state(hdev, dr.dev_opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | break; |
| 1845 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1846 | case HCISETLINKPOL: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1847 | err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 1848 | HCI_INIT_TIMEOUT, NULL); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1849 | break; |
| 1850 | |
| 1851 | case HCISETLINKMODE: |
| 1852 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 1853 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 1854 | break; |
| 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | case HCISETPTYPE: |
| 1857 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 1858 | break; |
| 1859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1861 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1862 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | break; |
| 1864 | |
| 1865 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1866 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 1867 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | break; |
| 1869 | |
| 1870 | default: |
| 1871 | err = -EINVAL; |
| 1872 | break; |
| 1873 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1874 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1875 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | hci_dev_put(hdev); |
| 1877 | return err; |
| 1878 | } |
| 1879 | |
| 1880 | int hci_get_dev_list(void __user *arg) |
| 1881 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1882 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | struct hci_dev_list_req *dl; |
| 1884 | struct hci_dev_req *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | int n = 0, size, err; |
| 1886 | __u16 dev_num; |
| 1887 | |
| 1888 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 1889 | return -EFAULT; |
| 1890 | |
| 1891 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 1892 | return -EINVAL; |
| 1893 | |
| 1894 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 1895 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1896 | dl = kzalloc(size, GFP_KERNEL); |
| 1897 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | return -ENOMEM; |
| 1899 | |
| 1900 | dr = dl->dev_req; |
| 1901 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1902 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1903 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1904 | unsigned long flags = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1905 | |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1906 | /* When the auto-off is configured it means the transport |
| 1907 | * is running, but in that case still indicate that the |
| 1908 | * device is actually down. |
| 1909 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1910 | if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1911 | flags &= ~BIT(HCI_UP); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | (dr + n)->dev_id = hdev->id; |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1914 | (dr + n)->dev_opt = flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | if (++n >= dev_num) |
| 1917 | break; |
| 1918 | } |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 1919 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
| 1921 | dl->dev_num = n; |
| 1922 | size = sizeof(*dl) + n * sizeof(*dr); |
| 1923 | |
| 1924 | err = copy_to_user(arg, dl, size); |
| 1925 | kfree(dl); |
| 1926 | |
| 1927 | return err ? -EFAULT : 0; |
| 1928 | } |
| 1929 | |
| 1930 | int hci_get_dev_info(void __user *arg) |
| 1931 | { |
| 1932 | struct hci_dev *hdev; |
| 1933 | struct hci_dev_info di; |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1934 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | int err = 0; |
| 1936 | |
| 1937 | if (copy_from_user(&di, arg, sizeof(di))) |
| 1938 | return -EFAULT; |
| 1939 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1940 | hdev = hci_dev_get(di.dev_id); |
| 1941 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | return -ENODEV; |
| 1943 | |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1944 | /* When the auto-off is configured it means the transport |
| 1945 | * is running, but in that case still indicate that the |
| 1946 | * device is actually down. |
| 1947 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1948 | if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1949 | flags = hdev->flags & ~BIT(HCI_UP); |
| 1950 | else |
| 1951 | flags = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | strcpy(di.name, hdev->name); |
| 1954 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 60f2a3e | 2013-10-01 22:59:20 -0700 | [diff] [blame] | 1955 | di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); |
Marcel Holtmann | 2e84d8d | 2014-07-10 13:17:37 +0200 | [diff] [blame] | 1956 | di.flags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | di.pkt_type = hdev->pkt_type; |
Johan Hedberg | 572c7f8 | 2012-10-19 20:57:46 +0300 | [diff] [blame] | 1958 | if (lmp_bredr_capable(hdev)) { |
| 1959 | di.acl_mtu = hdev->acl_mtu; |
| 1960 | di.acl_pkts = hdev->acl_pkts; |
| 1961 | di.sco_mtu = hdev->sco_mtu; |
| 1962 | di.sco_pkts = hdev->sco_pkts; |
| 1963 | } else { |
| 1964 | di.acl_mtu = hdev->le_mtu; |
| 1965 | di.acl_pkts = hdev->le_pkts; |
| 1966 | di.sco_mtu = 0; |
| 1967 | di.sco_pkts = 0; |
| 1968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | di.link_policy = hdev->link_policy; |
| 1970 | di.link_mode = hdev->link_mode; |
| 1971 | |
| 1972 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 1973 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 1974 | |
| 1975 | if (copy_to_user(arg, &di, sizeof(di))) |
| 1976 | err = -EFAULT; |
| 1977 | |
| 1978 | hci_dev_put(hdev); |
| 1979 | |
| 1980 | return err; |
| 1981 | } |
| 1982 | |
| 1983 | /* ---- Interface to HCI drivers ---- */ |
| 1984 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 1985 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 1986 | { |
| 1987 | struct hci_dev *hdev = data; |
| 1988 | |
| 1989 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 1990 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1991 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1992 | return -EBUSY; |
| 1993 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 1994 | if (blocked) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1995 | hci_dev_set_flag(hdev, HCI_RFKILLED); |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1996 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && |
| 1997 | !hci_dev_test_flag(hdev, HCI_CONFIG)) |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 1998 | hci_dev_do_close(hdev); |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 1999 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2000 | hci_dev_clear_flag(hdev, HCI_RFKILLED); |
Gustavo Padovan | 1025c04 | 2013-09-27 11:56:14 -0300 | [diff] [blame] | 2001 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
| 2006 | static const struct rfkill_ops hci_rfkill_ops = { |
| 2007 | .set_block = hci_rfkill_set_block, |
| 2008 | }; |
| 2009 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2010 | static void hci_power_on(struct work_struct *work) |
| 2011 | { |
| 2012 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2013 | int err; |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2014 | |
| 2015 | BT_DBG("%s", hdev->name); |
| 2016 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 2017 | if (test_bit(HCI_UP, &hdev->flags) && |
| 2018 | hci_dev_test_flag(hdev, HCI_MGMT) && |
| 2019 | hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { |
| 2020 | hci_req_sync_lock(hdev); |
| 2021 | err = __hci_req_hci_power_on(hdev); |
| 2022 | hci_req_sync_unlock(hdev); |
| 2023 | mgmt_power_on(hdev, err); |
| 2024 | return; |
| 2025 | } |
| 2026 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 2027 | err = hci_dev_do_open(hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2028 | if (err < 0) { |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2029 | hci_dev_lock(hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2030 | mgmt_set_powered_failed(hdev, err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2031 | hci_dev_unlock(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2032 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2033 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2034 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2035 | /* During the HCI setup phase, a few error conditions are |
| 2036 | * ignored and they need to be checked now. If they are still |
| 2037 | * valid, it is important to turn the device back off. |
| 2038 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2039 | if (hci_dev_test_flag(hdev, HCI_RFKILLED) || |
| 2040 | hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2041 | (hdev->dev_type == HCI_BREDR && |
| 2042 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 2043 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2044 | hci_dev_clear_flag(hdev, HCI_AUTO_OFF); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 2045 | hci_dev_do_close(hdev); |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2046 | } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 2047 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 2048 | HCI_AUTO_OFF_TIMEOUT); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 2049 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2050 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2051 | if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 2052 | /* For unconfigured devices, set the HCI_RAW flag |
| 2053 | * so that userspace can easily identify them. |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 2054 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2055 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 2056 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 2057 | |
| 2058 | /* For fully configured devices, this will send |
| 2059 | * the Index Added event. For unconfigured devices, |
| 2060 | * it will send Unconfigued Index Added event. |
| 2061 | * |
| 2062 | * Devices with HCI_QUIRK_RAW_DEVICE are ignored |
| 2063 | * and no event will be send. |
| 2064 | */ |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2065 | mgmt_index_added(hdev); |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2066 | } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 2067 | /* When the controller is now configured, then it |
| 2068 | * is important to clear the HCI_RAW flag. |
| 2069 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2070 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 2071 | clear_bit(HCI_RAW, &hdev->flags); |
| 2072 | |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 2073 | /* Powering on the controller with HCI_CONFIG set only |
| 2074 | * happens with the transition from unconfigured to |
| 2075 | * configured. This will send the Index Added event. |
| 2076 | */ |
| 2077 | mgmt_index_added(hdev); |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 2078 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | static void hci_power_off(struct work_struct *work) |
| 2082 | { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 2083 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2084 | power_off.work); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2085 | |
| 2086 | BT_DBG("%s", hdev->name); |
| 2087 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 2088 | hci_dev_do_close(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2089 | } |
| 2090 | |
Marcel Holtmann | c7741d1 | 2015-01-28 11:09:55 -0800 | [diff] [blame] | 2091 | static void hci_error_reset(struct work_struct *work) |
| 2092 | { |
| 2093 | struct hci_dev *hdev = container_of(work, struct hci_dev, error_reset); |
| 2094 | |
| 2095 | BT_DBG("%s", hdev->name); |
| 2096 | |
| 2097 | if (hdev->hw_error) |
| 2098 | hdev->hw_error(hdev, hdev->hw_error_code); |
| 2099 | else |
| 2100 | BT_ERR("%s hardware error 0x%2.2x", hdev->name, |
| 2101 | hdev->hw_error_code); |
| 2102 | |
| 2103 | if (hci_dev_do_close(hdev)) |
| 2104 | return; |
| 2105 | |
Marcel Holtmann | c7741d1 | 2015-01-28 11:09:55 -0800 | [diff] [blame] | 2106 | hci_dev_do_open(hdev); |
| 2107 | } |
| 2108 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2109 | void hci_uuids_clear(struct hci_dev *hdev) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2110 | { |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 2111 | struct bt_uuid *uuid, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2112 | |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 2113 | list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { |
| 2114 | list_del(&uuid->list); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2115 | kfree(uuid); |
| 2116 | } |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2117 | } |
| 2118 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2119 | void hci_link_keys_clear(struct hci_dev *hdev) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2120 | { |
Johan Hedberg | 0378b59 | 2014-11-19 15:22:22 +0200 | [diff] [blame] | 2121 | struct link_key *key; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2122 | |
Johan Hedberg | 0378b59 | 2014-11-19 15:22:22 +0200 | [diff] [blame] | 2123 | list_for_each_entry_rcu(key, &hdev->link_keys, list) { |
| 2124 | list_del_rcu(&key->list); |
| 2125 | kfree_rcu(key, rcu); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2126 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2127 | } |
| 2128 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2129 | void hci_smp_ltks_clear(struct hci_dev *hdev) |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2130 | { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2131 | struct smp_ltk *k; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2132 | |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2133 | list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { |
| 2134 | list_del_rcu(&k->list); |
| 2135 | kfree_rcu(k, rcu); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2136 | } |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2137 | } |
| 2138 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2139 | void hci_smp_irks_clear(struct hci_dev *hdev) |
| 2140 | { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2141 | struct smp_irk *k; |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2142 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2143 | list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) { |
| 2144 | list_del_rcu(&k->list); |
| 2145 | kfree_rcu(k, rcu); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2149 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2150 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2151 | struct link_key *k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2152 | |
Johan Hedberg | 0378b59 | 2014-11-19 15:22:22 +0200 | [diff] [blame] | 2153 | rcu_read_lock(); |
| 2154 | list_for_each_entry_rcu(k, &hdev->link_keys, list) { |
| 2155 | if (bacmp(bdaddr, &k->bdaddr) == 0) { |
| 2156 | rcu_read_unlock(); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2157 | return k; |
Johan Hedberg | 0378b59 | 2014-11-19 15:22:22 +0200 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | rcu_read_unlock(); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2161 | |
| 2162 | return NULL; |
| 2163 | } |
| 2164 | |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2165 | 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] | 2166 | u8 key_type, u8 old_key_type) |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2167 | { |
| 2168 | /* Legacy key */ |
| 2169 | if (key_type < 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2170 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2171 | |
| 2172 | /* Debug keys are insecure so don't store them persistently */ |
| 2173 | if (key_type == HCI_LK_DEBUG_COMBINATION) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2174 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2175 | |
| 2176 | /* Changed combination key and there's no previous one */ |
| 2177 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2178 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2179 | |
| 2180 | /* Security mode 3 case */ |
| 2181 | if (!conn) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2182 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2183 | |
Johan Hedberg | e3befab | 2014-06-01 16:33:39 +0300 | [diff] [blame] | 2184 | /* BR/EDR key derived using SC from an LE link */ |
| 2185 | if (conn->type == LE_LINK) |
| 2186 | return true; |
| 2187 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2188 | /* Neither local nor remote side had no-bonding as requirement */ |
| 2189 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2190 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2191 | |
| 2192 | /* Local side had dedicated bonding as requirement */ |
| 2193 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2194 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2195 | |
| 2196 | /* Remote side had dedicated bonding as requirement */ |
| 2197 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2198 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2199 | |
| 2200 | /* If none of the above criteria match, then don't store the key |
| 2201 | * persistently */ |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2202 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 2205 | static u8 ltk_role(u8 type) |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2206 | { |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 2207 | if (type == SMP_LTK) |
| 2208 | return HCI_ROLE_MASTER; |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2209 | |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 2210 | return HCI_ROLE_SLAVE; |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2211 | } |
| 2212 | |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 2213 | struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2214 | u8 addr_type, u8 role) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2215 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2216 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2217 | |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2218 | rcu_read_lock(); |
| 2219 | list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { |
Johan Hedberg | 5378bc5 | 2014-05-29 14:00:39 +0300 | [diff] [blame] | 2220 | if (addr_type != k->bdaddr_type || bacmp(bdaddr, &k->bdaddr)) |
| 2221 | continue; |
| 2222 | |
Johan Hedberg | 923e241 | 2014-12-03 12:43:39 +0200 | [diff] [blame] | 2223 | if (smp_ltk_is_sc(k) || ltk_role(k->type) == role) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2224 | rcu_read_unlock(); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2225 | return k; |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | rcu_read_unlock(); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2229 | |
| 2230 | return NULL; |
| 2231 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2232 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2233 | struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa) |
| 2234 | { |
| 2235 | struct smp_irk *irk; |
| 2236 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2237 | rcu_read_lock(); |
| 2238 | list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { |
| 2239 | if (!bacmp(&irk->rpa, rpa)) { |
| 2240 | rcu_read_unlock(); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2241 | return irk; |
| 2242 | } |
| 2243 | } |
| 2244 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2245 | list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { |
| 2246 | if (smp_irk_matches(hdev, irk->val, rpa)) { |
| 2247 | bacpy(&irk->rpa, rpa); |
| 2248 | rcu_read_unlock(); |
| 2249 | return irk; |
| 2250 | } |
| 2251 | } |
| 2252 | rcu_read_unlock(); |
| 2253 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2254 | return NULL; |
| 2255 | } |
| 2256 | |
| 2257 | struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2258 | u8 addr_type) |
| 2259 | { |
| 2260 | struct smp_irk *irk; |
| 2261 | |
Johan Hedberg | 6cfc998 | 2014-02-18 21:41:35 +0200 | [diff] [blame] | 2262 | /* Identity Address must be public or static random */ |
| 2263 | if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0) |
| 2264 | return NULL; |
| 2265 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2266 | rcu_read_lock(); |
| 2267 | list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2268 | if (addr_type == irk->addr_type && |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2269 | bacmp(bdaddr, &irk->bdaddr) == 0) { |
| 2270 | rcu_read_unlock(); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2271 | return irk; |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2272 | } |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2273 | } |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2274 | rcu_read_unlock(); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2275 | |
| 2276 | return NULL; |
| 2277 | } |
| 2278 | |
Johan Hedberg | 567fa2a | 2014-06-24 13:15:48 +0300 | [diff] [blame] | 2279 | struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2280 | bdaddr_t *bdaddr, u8 *val, u8 type, |
| 2281 | u8 pin_len, bool *persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2282 | { |
| 2283 | struct link_key *key, *old_key; |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2284 | u8 old_key_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2285 | |
| 2286 | old_key = hci_find_link_key(hdev, bdaddr); |
| 2287 | if (old_key) { |
| 2288 | old_key_type = old_key->type; |
| 2289 | key = old_key; |
| 2290 | } else { |
Johan Hedberg | 12adcf3 | 2011-04-28 11:29:00 -0700 | [diff] [blame] | 2291 | old_key_type = conn ? conn->key_type : 0xff; |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 2292 | key = kzalloc(sizeof(*key), GFP_KERNEL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2293 | if (!key) |
Johan Hedberg | 567fa2a | 2014-06-24 13:15:48 +0300 | [diff] [blame] | 2294 | return NULL; |
Johan Hedberg | 0378b59 | 2014-11-19 15:22:22 +0200 | [diff] [blame] | 2295 | list_add_rcu(&key->list, &hdev->link_keys); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2296 | } |
| 2297 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2298 | BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2299 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2300 | /* Some buggy controller combinations generate a changed |
| 2301 | * combination key for legacy pairing even when there's no |
| 2302 | * previous key */ |
| 2303 | if (type == HCI_LK_CHANGED_COMBINATION && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2304 | (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2305 | type = HCI_LK_COMBINATION; |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 2306 | if (conn) |
| 2307 | conn->key_type = type; |
| 2308 | } |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2309 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2310 | bacpy(&key->bdaddr, bdaddr); |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 2311 | memcpy(key->val, val, HCI_LINK_KEY_SIZE); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2312 | key->pin_len = pin_len; |
| 2313 | |
Waldemar Rymarkiewicz | b6020ba | 2011-04-28 12:07:53 +0200 | [diff] [blame] | 2314 | if (type == HCI_LK_CHANGED_COMBINATION) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2315 | key->type = old_key_type; |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 2316 | else |
| 2317 | key->type = type; |
| 2318 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2319 | if (persistent) |
| 2320 | *persistent = hci_persistent_key(hdev, conn, type, |
| 2321 | old_key_type); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 2322 | |
Johan Hedberg | 567fa2a | 2014-06-24 13:15:48 +0300 | [diff] [blame] | 2323 | return key; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2324 | } |
| 2325 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2326 | struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 2327 | u8 addr_type, u8 type, u8 authenticated, |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 2328 | u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2329 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2330 | struct smp_ltk *key, *old_key; |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 2331 | u8 role = ltk_role(type); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2332 | |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 2333 | old_key = hci_find_ltk(hdev, bdaddr, addr_type, role); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2334 | if (old_key) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2335 | key = old_key; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2336 | else { |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 2337 | key = kzalloc(sizeof(*key), GFP_KERNEL); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2338 | if (!key) |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2339 | return NULL; |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2340 | list_add_rcu(&key->list, &hdev->long_term_keys); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2341 | } |
| 2342 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2343 | bacpy(&key->bdaddr, bdaddr); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2344 | key->bdaddr_type = addr_type; |
| 2345 | memcpy(key->val, tk, sizeof(key->val)); |
| 2346 | key->authenticated = authenticated; |
| 2347 | key->ediv = ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 2348 | key->rand = rand; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2349 | key->enc_size = enc_size; |
| 2350 | key->type = type; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2351 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2352 | return key; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2353 | } |
| 2354 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2355 | struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2356 | u8 addr_type, u8 val[16], bdaddr_t *rpa) |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2357 | { |
| 2358 | struct smp_irk *irk; |
| 2359 | |
| 2360 | irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type); |
| 2361 | if (!irk) { |
| 2362 | irk = kzalloc(sizeof(*irk), GFP_KERNEL); |
| 2363 | if (!irk) |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2364 | return NULL; |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2365 | |
| 2366 | bacpy(&irk->bdaddr, bdaddr); |
| 2367 | irk->addr_type = addr_type; |
| 2368 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2369 | list_add_rcu(&irk->list, &hdev->identity_resolving_keys); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | memcpy(irk->val, val, 16); |
| 2373 | bacpy(&irk->rpa, rpa); |
| 2374 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2375 | return irk; |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2376 | } |
| 2377 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2378 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2379 | { |
| 2380 | struct link_key *key; |
| 2381 | |
| 2382 | key = hci_find_link_key(hdev, bdaddr); |
| 2383 | if (!key) |
| 2384 | return -ENOENT; |
| 2385 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2386 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2387 | |
Johan Hedberg | 0378b59 | 2014-11-19 15:22:22 +0200 | [diff] [blame] | 2388 | list_del_rcu(&key->list); |
| 2389 | kfree_rcu(key, rcu); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2390 | |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2394 | int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type) |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2395 | { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2396 | struct smp_ltk *k; |
Johan Hedberg | c51ffa0 | 2014-02-18 17:14:33 +0200 | [diff] [blame] | 2397 | int removed = 0; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2398 | |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2399 | list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2400 | if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type) |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2401 | continue; |
| 2402 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2403 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2404 | |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 2405 | list_del_rcu(&k->list); |
| 2406 | kfree_rcu(k, rcu); |
Johan Hedberg | c51ffa0 | 2014-02-18 17:14:33 +0200 | [diff] [blame] | 2407 | removed++; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2408 | } |
| 2409 | |
Johan Hedberg | c51ffa0 | 2014-02-18 17:14:33 +0200 | [diff] [blame] | 2410 | return removed ? 0 : -ENOENT; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2411 | } |
| 2412 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2413 | void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type) |
| 2414 | { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2415 | struct smp_irk *k; |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2416 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2417 | list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) { |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2418 | if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type) |
| 2419 | continue; |
| 2420 | |
| 2421 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
| 2422 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2423 | list_del_rcu(&k->list); |
| 2424 | kfree_rcu(k, rcu); |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2425 | } |
| 2426 | } |
| 2427 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2428 | bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
| 2429 | { |
| 2430 | struct smp_ltk *k; |
Johan Hedberg | 4ba9faf | 2015-03-11 10:52:08 +0200 | [diff] [blame] | 2431 | struct smp_irk *irk; |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2432 | u8 addr_type; |
| 2433 | |
| 2434 | if (type == BDADDR_BREDR) { |
| 2435 | if (hci_find_link_key(hdev, bdaddr)) |
| 2436 | return true; |
| 2437 | return false; |
| 2438 | } |
| 2439 | |
| 2440 | /* Convert to HCI addr type which struct smp_ltk uses */ |
| 2441 | if (type == BDADDR_LE_PUBLIC) |
| 2442 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2443 | else |
| 2444 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2445 | |
Johan Hedberg | 4ba9faf | 2015-03-11 10:52:08 +0200 | [diff] [blame] | 2446 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 2447 | if (irk) { |
| 2448 | bdaddr = &irk->bdaddr; |
| 2449 | addr_type = irk->addr_type; |
| 2450 | } |
| 2451 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2452 | rcu_read_lock(); |
| 2453 | list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { |
Johan Hedberg | 87c8b28 | 2015-03-11 08:55:51 +0200 | [diff] [blame] | 2454 | if (k->bdaddr_type == addr_type && !bacmp(bdaddr, &k->bdaddr)) { |
| 2455 | rcu_read_unlock(); |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2456 | return true; |
Johan Hedberg | 87c8b28 | 2015-03-11 08:55:51 +0200 | [diff] [blame] | 2457 | } |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2458 | } |
| 2459 | rcu_read_unlock(); |
| 2460 | |
| 2461 | return false; |
| 2462 | } |
| 2463 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2464 | /* HCI command timer function */ |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 2465 | static void hci_cmd_timeout(struct work_struct *work) |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2466 | { |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 2467 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 2468 | cmd_timer.work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2469 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2470 | if (hdev->sent_cmd) { |
| 2471 | struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; |
| 2472 | u16 opcode = __le16_to_cpu(sent->opcode); |
| 2473 | |
| 2474 | BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); |
| 2475 | } else { |
| 2476 | BT_ERR("%s command tx timeout", hdev->name); |
| 2477 | } |
| 2478 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2479 | atomic_set(&hdev->cmd_cnt, 1); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2480 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2481 | } |
| 2482 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2483 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2484 | bdaddr_t *bdaddr, u8 bdaddr_type) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2485 | { |
| 2486 | struct oob_data *data; |
| 2487 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2488 | list_for_each_entry(data, &hdev->remote_oob_data, list) { |
| 2489 | if (bacmp(bdaddr, &data->bdaddr) != 0) |
| 2490 | continue; |
| 2491 | if (data->bdaddr_type != bdaddr_type) |
| 2492 | continue; |
| 2493 | return data; |
| 2494 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2495 | |
| 2496 | return NULL; |
| 2497 | } |
| 2498 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2499 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2500 | u8 bdaddr_type) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2501 | { |
| 2502 | struct oob_data *data; |
| 2503 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2504 | data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2505 | if (!data) |
| 2506 | return -ENOENT; |
| 2507 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2508 | BT_DBG("%s removing %pMR (%u)", hdev->name, bdaddr, bdaddr_type); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2509 | |
| 2510 | list_del(&data->list); |
| 2511 | kfree(data); |
| 2512 | |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2516 | void hci_remote_oob_data_clear(struct hci_dev *hdev) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2517 | { |
| 2518 | struct oob_data *data, *n; |
| 2519 | |
| 2520 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 2521 | list_del(&data->list); |
| 2522 | kfree(data); |
| 2523 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2524 | } |
| 2525 | |
Marcel Holtmann | 0798872 | 2014-01-10 02:07:29 -0800 | [diff] [blame] | 2526 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2527 | u8 bdaddr_type, u8 *hash192, u8 *rand192, |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 2528 | u8 *hash256, u8 *rand256) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2529 | { |
| 2530 | struct oob_data *data; |
| 2531 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2532 | data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2533 | if (!data) { |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 2534 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2535 | if (!data) |
| 2536 | return -ENOMEM; |
| 2537 | |
| 2538 | bacpy(&data->bdaddr, bdaddr); |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 2539 | data->bdaddr_type = bdaddr_type; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2540 | list_add(&data->list, &hdev->remote_oob_data); |
| 2541 | } |
| 2542 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 2543 | if (hash192 && rand192) { |
| 2544 | memcpy(data->hash192, hash192, sizeof(data->hash192)); |
| 2545 | memcpy(data->rand192, rand192, sizeof(data->rand192)); |
Marcel Holtmann | f7697b1 | 2015-01-30 23:20:55 -0800 | [diff] [blame] | 2546 | if (hash256 && rand256) |
| 2547 | data->present = 0x03; |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 2548 | } else { |
| 2549 | memset(data->hash192, 0, sizeof(data->hash192)); |
| 2550 | memset(data->rand192, 0, sizeof(data->rand192)); |
Marcel Holtmann | f7697b1 | 2015-01-30 23:20:55 -0800 | [diff] [blame] | 2551 | if (hash256 && rand256) |
| 2552 | data->present = 0x02; |
| 2553 | else |
| 2554 | data->present = 0x00; |
Marcel Holtmann | 0798872 | 2014-01-10 02:07:29 -0800 | [diff] [blame] | 2555 | } |
| 2556 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 2557 | if (hash256 && rand256) { |
| 2558 | memcpy(data->hash256, hash256, sizeof(data->hash256)); |
| 2559 | memcpy(data->rand256, rand256, sizeof(data->rand256)); |
| 2560 | } else { |
| 2561 | memset(data->hash256, 0, sizeof(data->hash256)); |
| 2562 | memset(data->rand256, 0, sizeof(data->rand256)); |
Marcel Holtmann | f7697b1 | 2015-01-30 23:20:55 -0800 | [diff] [blame] | 2563 | if (hash192 && rand192) |
| 2564 | data->present = 0x01; |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 2565 | } |
Marcel Holtmann | 0798872 | 2014-01-10 02:07:29 -0800 | [diff] [blame] | 2566 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2567 | BT_DBG("%s for %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2568 | |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2572 | /* This function requires the caller holds hdev->lock */ |
| 2573 | struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance) |
| 2574 | { |
| 2575 | struct adv_info *adv_instance; |
| 2576 | |
| 2577 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 2578 | if (adv_instance->instance == instance) |
| 2579 | return adv_instance; |
| 2580 | } |
| 2581 | |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | |
| 2585 | /* This function requires the caller holds hdev->lock */ |
Prasanna Karthik | 74b93e9 | 2015-11-18 12:38:41 +0000 | [diff] [blame] | 2586 | struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance) |
| 2587 | { |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2588 | struct adv_info *cur_instance; |
| 2589 | |
| 2590 | cur_instance = hci_find_adv_instance(hdev, instance); |
| 2591 | if (!cur_instance) |
| 2592 | return NULL; |
| 2593 | |
| 2594 | if (cur_instance == list_last_entry(&hdev->adv_instances, |
| 2595 | struct adv_info, list)) |
| 2596 | return list_first_entry(&hdev->adv_instances, |
| 2597 | struct adv_info, list); |
| 2598 | else |
| 2599 | return list_next_entry(cur_instance, list); |
| 2600 | } |
| 2601 | |
| 2602 | /* This function requires the caller holds hdev->lock */ |
| 2603 | int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance) |
| 2604 | { |
| 2605 | struct adv_info *adv_instance; |
| 2606 | |
| 2607 | adv_instance = hci_find_adv_instance(hdev, instance); |
| 2608 | if (!adv_instance) |
| 2609 | return -ENOENT; |
| 2610 | |
| 2611 | BT_DBG("%s removing %dMR", hdev->name, instance); |
| 2612 | |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 2613 | if (hdev->cur_adv_instance == instance) { |
| 2614 | if (hdev->adv_instance_timeout) { |
| 2615 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 2616 | hdev->adv_instance_timeout = 0; |
| 2617 | } |
| 2618 | hdev->cur_adv_instance = 0x00; |
Florian Grandel | 5d900e4 | 2015-06-18 03:16:35 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2621 | list_del(&adv_instance->list); |
| 2622 | kfree(adv_instance); |
| 2623 | |
| 2624 | hdev->adv_instance_cnt--; |
| 2625 | |
| 2626 | return 0; |
| 2627 | } |
| 2628 | |
| 2629 | /* This function requires the caller holds hdev->lock */ |
| 2630 | void hci_adv_instances_clear(struct hci_dev *hdev) |
| 2631 | { |
| 2632 | struct adv_info *adv_instance, *n; |
| 2633 | |
Florian Grandel | 5d900e4 | 2015-06-18 03:16:35 +0200 | [diff] [blame] | 2634 | if (hdev->adv_instance_timeout) { |
| 2635 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 2636 | hdev->adv_instance_timeout = 0; |
| 2637 | } |
| 2638 | |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2639 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 2640 | list_del(&adv_instance->list); |
| 2641 | kfree(adv_instance); |
| 2642 | } |
| 2643 | |
| 2644 | hdev->adv_instance_cnt = 0; |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 2645 | hdev->cur_adv_instance = 0x00; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | /* This function requires the caller holds hdev->lock */ |
| 2649 | int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags, |
| 2650 | u16 adv_data_len, u8 *adv_data, |
| 2651 | u16 scan_rsp_len, u8 *scan_rsp_data, |
| 2652 | u16 timeout, u16 duration) |
| 2653 | { |
| 2654 | struct adv_info *adv_instance; |
| 2655 | |
| 2656 | adv_instance = hci_find_adv_instance(hdev, instance); |
| 2657 | if (adv_instance) { |
| 2658 | memset(adv_instance->adv_data, 0, |
| 2659 | sizeof(adv_instance->adv_data)); |
| 2660 | memset(adv_instance->scan_rsp_data, 0, |
| 2661 | sizeof(adv_instance->scan_rsp_data)); |
| 2662 | } else { |
| 2663 | if (hdev->adv_instance_cnt >= HCI_MAX_ADV_INSTANCES || |
| 2664 | instance < 1 || instance > HCI_MAX_ADV_INSTANCES) |
| 2665 | return -EOVERFLOW; |
| 2666 | |
Johan Hedberg | 39ecfad | 2015-06-18 20:50:08 +0300 | [diff] [blame] | 2667 | adv_instance = kzalloc(sizeof(*adv_instance), GFP_KERNEL); |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2668 | if (!adv_instance) |
| 2669 | return -ENOMEM; |
| 2670 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 2671 | adv_instance->pending = true; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2672 | adv_instance->instance = instance; |
| 2673 | list_add(&adv_instance->list, &hdev->adv_instances); |
| 2674 | hdev->adv_instance_cnt++; |
| 2675 | } |
| 2676 | |
| 2677 | adv_instance->flags = flags; |
| 2678 | adv_instance->adv_data_len = adv_data_len; |
| 2679 | adv_instance->scan_rsp_len = scan_rsp_len; |
| 2680 | |
| 2681 | if (adv_data_len) |
| 2682 | memcpy(adv_instance->adv_data, adv_data, adv_data_len); |
| 2683 | |
| 2684 | if (scan_rsp_len) |
| 2685 | memcpy(adv_instance->scan_rsp_data, |
| 2686 | scan_rsp_data, scan_rsp_len); |
| 2687 | |
| 2688 | adv_instance->timeout = timeout; |
Florian Grandel | 5d900e4 | 2015-06-18 03:16:35 +0200 | [diff] [blame] | 2689 | adv_instance->remaining_time = timeout; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2690 | |
| 2691 | if (duration == 0) |
| 2692 | adv_instance->duration = HCI_DEFAULT_ADV_DURATION; |
| 2693 | else |
| 2694 | adv_instance->duration = duration; |
| 2695 | |
| 2696 | BT_DBG("%s for %dMR", hdev->name, instance); |
| 2697 | |
| 2698 | return 0; |
| 2699 | } |
| 2700 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2701 | struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list, |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2702 | bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2703 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2704 | struct bdaddr_list *b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2705 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2706 | list_for_each_entry(b, bdaddr_list, list) { |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2707 | if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2708 | return b; |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2709 | } |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2710 | |
| 2711 | return NULL; |
| 2712 | } |
| 2713 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2714 | void hci_bdaddr_list_clear(struct list_head *bdaddr_list) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2715 | { |
Geliang Tang | 7eb7404 | 2015-12-18 23:33:25 +0800 | [diff] [blame] | 2716 | struct bdaddr_list *b, *n; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2717 | |
Geliang Tang | 7eb7404 | 2015-12-18 23:33:25 +0800 | [diff] [blame] | 2718 | list_for_each_entry_safe(b, n, bdaddr_list, list) { |
| 2719 | list_del(&b->list); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2720 | kfree(b); |
| 2721 | } |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2722 | } |
| 2723 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2724 | int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2725 | { |
| 2726 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2727 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2728 | if (!bacmp(bdaddr, BDADDR_ANY)) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2729 | return -EBADF; |
| 2730 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2731 | if (hci_bdaddr_list_lookup(list, bdaddr, type)) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2732 | return -EEXIST; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2733 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 2734 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2735 | if (!entry) |
| 2736 | return -ENOMEM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2737 | |
| 2738 | bacpy(&entry->bdaddr, bdaddr); |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 2739 | entry->bdaddr_type = type; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 2740 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2741 | list_add(&entry->list, list); |
Marcel Holtmann | d2ab0ac | 2014-02-27 20:37:30 -0800 | [diff] [blame] | 2742 | |
| 2743 | return 0; |
| 2744 | } |
| 2745 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2746 | int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type) |
Marcel Holtmann | d2ab0ac | 2014-02-27 20:37:30 -0800 | [diff] [blame] | 2747 | { |
| 2748 | struct bdaddr_list *entry; |
| 2749 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2750 | if (!bacmp(bdaddr, BDADDR_ANY)) { |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2751 | hci_bdaddr_list_clear(list); |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2752 | return 0; |
| 2753 | } |
Marcel Holtmann | d2ab0ac | 2014-02-27 20:37:30 -0800 | [diff] [blame] | 2754 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 2755 | entry = hci_bdaddr_list_lookup(list, bdaddr, type); |
Marcel Holtmann | d2ab0ac | 2014-02-27 20:37:30 -0800 | [diff] [blame] | 2756 | if (!entry) |
| 2757 | return -ENOENT; |
| 2758 | |
| 2759 | list_del(&entry->list); |
| 2760 | kfree(entry); |
| 2761 | |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2765 | /* This function requires the caller holds hdev->lock */ |
| 2766 | struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, |
| 2767 | bdaddr_t *addr, u8 addr_type) |
| 2768 | { |
| 2769 | struct hci_conn_params *params; |
| 2770 | |
| 2771 | list_for_each_entry(params, &hdev->le_conn_params, list) { |
| 2772 | if (bacmp(¶ms->addr, addr) == 0 && |
| 2773 | params->addr_type == addr_type) { |
| 2774 | return params; |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
| 2781 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 501f882 | 2014-07-04 12:37:26 +0300 | [diff] [blame] | 2782 | struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list, |
| 2783 | bdaddr_t *addr, u8 addr_type) |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2784 | { |
Johan Hedberg | 912b42e | 2014-07-03 19:33:49 +0300 | [diff] [blame] | 2785 | struct hci_conn_params *param; |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2786 | |
Johan Hedberg | 501f882 | 2014-07-04 12:37:26 +0300 | [diff] [blame] | 2787 | list_for_each_entry(param, list, action) { |
Johan Hedberg | 912b42e | 2014-07-03 19:33:49 +0300 | [diff] [blame] | 2788 | if (bacmp(¶m->addr, addr) == 0 && |
| 2789 | param->addr_type == addr_type) |
| 2790 | return param; |
Marcel Holtmann | 4b10966 | 2014-06-29 13:41:49 +0200 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | return NULL; |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /* This function requires the caller holds hdev->lock */ |
Marcel Holtmann | 51d167c | 2014-07-01 12:11:04 +0200 | [diff] [blame] | 2797 | struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev, |
| 2798 | bdaddr_t *addr, u8 addr_type) |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2799 | { |
| 2800 | struct hci_conn_params *params; |
| 2801 | |
| 2802 | params = hci_conn_params_lookup(hdev, addr, addr_type); |
Andre Guedes | cef952c | 2014-02-26 20:21:49 -0300 | [diff] [blame] | 2803 | if (params) |
Marcel Holtmann | 51d167c | 2014-07-01 12:11:04 +0200 | [diff] [blame] | 2804 | return params; |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2805 | |
| 2806 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
| 2807 | if (!params) { |
| 2808 | BT_ERR("Out of memory"); |
Marcel Holtmann | 51d167c | 2014-07-01 12:11:04 +0200 | [diff] [blame] | 2809 | return NULL; |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | bacpy(¶ms->addr, addr); |
| 2813 | params->addr_type = addr_type; |
Andre Guedes | cef952c | 2014-02-26 20:21:49 -0300 | [diff] [blame] | 2814 | |
| 2815 | list_add(¶ms->list, &hdev->le_conn_params); |
Johan Hedberg | 93450c7 | 2014-07-04 12:37:17 +0300 | [diff] [blame] | 2816 | INIT_LIST_HEAD(¶ms->action); |
Andre Guedes | cef952c | 2014-02-26 20:21:49 -0300 | [diff] [blame] | 2817 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 2818 | params->conn_min_interval = hdev->le_conn_min_interval; |
| 2819 | params->conn_max_interval = hdev->le_conn_max_interval; |
| 2820 | params->conn_latency = hdev->le_conn_latency; |
| 2821 | params->supervision_timeout = hdev->le_supv_timeout; |
| 2822 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2823 | |
| 2824 | BT_DBG("addr %pMR (type %u)", addr, addr_type); |
| 2825 | |
Marcel Holtmann | 51d167c | 2014-07-01 12:11:04 +0200 | [diff] [blame] | 2826 | return params; |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 2827 | } |
| 2828 | |
Johan Hedberg | f6c6324 | 2014-08-15 21:06:59 +0300 | [diff] [blame] | 2829 | static void hci_conn_params_free(struct hci_conn_params *params) |
| 2830 | { |
| 2831 | if (params->conn) { |
| 2832 | hci_conn_drop(params->conn); |
| 2833 | hci_conn_put(params->conn); |
| 2834 | } |
| 2835 | |
| 2836 | list_del(¶ms->action); |
| 2837 | list_del(¶ms->list); |
| 2838 | kfree(params); |
| 2839 | } |
| 2840 | |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2841 | /* This function requires the caller holds hdev->lock */ |
| 2842 | void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type) |
| 2843 | { |
| 2844 | struct hci_conn_params *params; |
| 2845 | |
| 2846 | params = hci_conn_params_lookup(hdev, addr, addr_type); |
| 2847 | if (!params) |
| 2848 | return; |
| 2849 | |
Johan Hedberg | f6c6324 | 2014-08-15 21:06:59 +0300 | [diff] [blame] | 2850 | hci_conn_params_free(params); |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2851 | |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 2852 | hci_update_background_scan(hdev); |
| 2853 | |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2854 | BT_DBG("addr %pMR (type %u)", addr, addr_type); |
| 2855 | } |
| 2856 | |
| 2857 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 55af49a8 | 2014-07-02 17:37:26 +0300 | [diff] [blame] | 2858 | void hci_conn_params_clear_disabled(struct hci_dev *hdev) |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2859 | { |
| 2860 | struct hci_conn_params *params, *tmp; |
| 2861 | |
| 2862 | list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { |
Johan Hedberg | 55af49a8 | 2014-07-02 17:37:26 +0300 | [diff] [blame] | 2863 | if (params->auto_connect != HCI_AUTO_CONN_DISABLED) |
| 2864 | continue; |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 2865 | |
| 2866 | /* If trying to estabilish one time connection to disabled |
| 2867 | * device, leave the params, but mark them as just once. |
| 2868 | */ |
| 2869 | if (params->explicit_connect) { |
| 2870 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2871 | continue; |
| 2872 | } |
| 2873 | |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2874 | list_del(¶ms->list); |
| 2875 | kfree(params); |
| 2876 | } |
| 2877 | |
Johan Hedberg | 55af49a8 | 2014-07-02 17:37:26 +0300 | [diff] [blame] | 2878 | BT_DBG("All LE disabled connection parameters were removed"); |
| 2879 | } |
| 2880 | |
| 2881 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 030e7f8 | 2015-11-10 09:44:53 +0200 | [diff] [blame] | 2882 | static void hci_conn_params_clear_all(struct hci_dev *hdev) |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2883 | { |
| 2884 | struct hci_conn_params *params, *tmp; |
| 2885 | |
Johan Hedberg | f6c6324 | 2014-08-15 21:06:59 +0300 | [diff] [blame] | 2886 | list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) |
| 2887 | hci_conn_params_free(params); |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2888 | |
| 2889 | BT_DBG("All LE connection parameters were removed"); |
| 2890 | } |
| 2891 | |
Johan Hedberg | a1f4c31 | 2014-02-27 14:05:41 +0200 | [diff] [blame] | 2892 | /* Copy the Identity Address of the controller. |
| 2893 | * |
| 2894 | * If the controller has a public BD_ADDR, then by default use that one. |
| 2895 | * If this is a LE only controller without a public address, default to |
| 2896 | * the static random address. |
| 2897 | * |
| 2898 | * For debugging purposes it is possible to force controllers with a |
| 2899 | * public address to use the static random address instead. |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 2900 | * |
| 2901 | * In case BR/EDR has been disabled on a dual-mode controller and |
| 2902 | * userspace has configured a static address, then that address |
| 2903 | * becomes the identity address instead of the public BR/EDR address. |
Johan Hedberg | a1f4c31 | 2014-02-27 14:05:41 +0200 | [diff] [blame] | 2904 | */ |
| 2905 | void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2906 | u8 *bdaddr_type) |
| 2907 | { |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 2908 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 2909 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2910 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 2911 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Johan Hedberg | a1f4c31 | 2014-02-27 14:05:41 +0200 | [diff] [blame] | 2912 | bacpy(bdaddr, &hdev->static_addr); |
| 2913 | *bdaddr_type = ADDR_LE_DEV_RANDOM; |
| 2914 | } else { |
| 2915 | bacpy(bdaddr, &hdev->bdaddr); |
| 2916 | *bdaddr_type = ADDR_LE_DEV_PUBLIC; |
| 2917 | } |
| 2918 | } |
| 2919 | |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2920 | /* Alloc HCI device */ |
| 2921 | struct hci_dev *hci_alloc_dev(void) |
| 2922 | { |
| 2923 | struct hci_dev *hdev; |
| 2924 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 2925 | hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 2926 | if (!hdev) |
| 2927 | return NULL; |
| 2928 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2929 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
| 2930 | hdev->esco_type = (ESCO_HV1); |
| 2931 | hdev->link_mode = (HCI_LM_ACCEPT); |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 2932 | hdev->num_iac = 0x01; /* One IAC support is mandatory */ |
| 2933 | hdev->io_capability = 0x03; /* No Input No Output */ |
Marcel Holtmann | 96c2103 | 2014-07-02 11:30:51 +0200 | [diff] [blame] | 2934 | hdev->manufacturer = 0xffff; /* Default to internal use */ |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 2935 | hdev->inq_tx_power = HCI_TX_POWER_INVALID; |
| 2936 | hdev->adv_tx_power = HCI_TX_POWER_INVALID; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2937 | hdev->adv_instance_cnt = 0; |
| 2938 | hdev->cur_adv_instance = 0x00; |
Florian Grandel | 5d900e4 | 2015-06-18 03:16:35 +0200 | [diff] [blame] | 2939 | hdev->adv_instance_timeout = 0; |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2940 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2941 | hdev->sniff_max_interval = 800; |
| 2942 | hdev->sniff_min_interval = 80; |
| 2943 | |
Marcel Holtmann | 3f959d4 | 2014-02-20 11:55:56 -0800 | [diff] [blame] | 2944 | hdev->le_adv_channel_map = 0x07; |
Georg Lukas | 628531c | 2014-07-26 13:59:57 +0200 | [diff] [blame] | 2945 | hdev->le_adv_min_interval = 0x0800; |
| 2946 | hdev->le_adv_max_interval = 0x0800; |
Marcel Holtmann | bef6473 | 2013-10-11 08:23:19 -0700 | [diff] [blame] | 2947 | hdev->le_scan_interval = 0x0060; |
| 2948 | hdev->le_scan_window = 0x0030; |
Marcel Holtmann | 4e70c7e | 2013-10-19 07:09:13 -0700 | [diff] [blame] | 2949 | hdev->le_conn_min_interval = 0x0028; |
| 2950 | hdev->le_conn_max_interval = 0x0038; |
Marcel Holtmann | 04fb7d9 | 2014-06-30 12:34:36 +0200 | [diff] [blame] | 2951 | hdev->le_conn_latency = 0x0000; |
| 2952 | hdev->le_supv_timeout = 0x002a; |
Marcel Holtmann | a8e1bfa | 2014-12-20 16:28:40 +0100 | [diff] [blame] | 2953 | hdev->le_def_tx_len = 0x001b; |
| 2954 | hdev->le_def_tx_time = 0x0148; |
| 2955 | hdev->le_max_tx_len = 0x001b; |
| 2956 | hdev->le_max_tx_time = 0x0148; |
| 2957 | hdev->le_max_rx_len = 0x001b; |
| 2958 | hdev->le_max_rx_time = 0x0148; |
Marcel Holtmann | bef6473 | 2013-10-11 08:23:19 -0700 | [diff] [blame] | 2959 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 2960 | hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT; |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 2961 | hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT; |
Andrzej Kaczmarek | 31ad169 | 2014-05-14 13:43:02 +0200 | [diff] [blame] | 2962 | hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE; |
| 2963 | hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE; |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 2964 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2965 | mutex_init(&hdev->lock); |
| 2966 | mutex_init(&hdev->req_lock); |
| 2967 | |
| 2968 | INIT_LIST_HEAD(&hdev->mgmt_pending); |
| 2969 | INIT_LIST_HEAD(&hdev->blacklist); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 2970 | INIT_LIST_HEAD(&hdev->whitelist); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2971 | INIT_LIST_HEAD(&hdev->uuids); |
| 2972 | INIT_LIST_HEAD(&hdev->link_keys); |
| 2973 | INIT_LIST_HEAD(&hdev->long_term_keys); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2974 | INIT_LIST_HEAD(&hdev->identity_resolving_keys); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2975 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
Marcel Holtmann | d2ab0ac | 2014-02-27 20:37:30 -0800 | [diff] [blame] | 2976 | INIT_LIST_HEAD(&hdev->le_white_list); |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 2977 | INIT_LIST_HEAD(&hdev->le_conn_params); |
Andre Guedes | 77a77a3 | 2014-02-26 20:21:46 -0300 | [diff] [blame] | 2978 | INIT_LIST_HEAD(&hdev->pend_le_conns); |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 2979 | INIT_LIST_HEAD(&hdev->pend_le_reports); |
Andrei Emeltchenko | 6b536b5 | 2012-08-31 16:39:28 +0300 | [diff] [blame] | 2980 | INIT_LIST_HEAD(&hdev->conn_hash.list); |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 2981 | INIT_LIST_HEAD(&hdev->adv_instances); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2982 | |
| 2983 | INIT_WORK(&hdev->rx_work, hci_rx_work); |
| 2984 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); |
| 2985 | INIT_WORK(&hdev->tx_work, hci_tx_work); |
| 2986 | INIT_WORK(&hdev->power_on, hci_power_on); |
Marcel Holtmann | c7741d1 | 2015-01-28 11:09:55 -0800 | [diff] [blame] | 2987 | INIT_WORK(&hdev->error_reset, hci_error_reset); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2988 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2989 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2990 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2991 | skb_queue_head_init(&hdev->rx_q); |
| 2992 | skb_queue_head_init(&hdev->cmd_q); |
| 2993 | skb_queue_head_init(&hdev->raw_q); |
| 2994 | |
| 2995 | init_waitqueue_head(&hdev->req_wait_q); |
| 2996 | |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 2997 | INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 2998 | |
Johan Hedberg | 5fc16cc | 2015-11-11 08:11:16 +0200 | [diff] [blame] | 2999 | hci_request_setup(hdev); |
| 3000 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3001 | hci_init_sysfs(hdev); |
| 3002 | discovery_init(hdev); |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 3003 | |
| 3004 | return hdev; |
| 3005 | } |
| 3006 | EXPORT_SYMBOL(hci_alloc_dev); |
| 3007 | |
| 3008 | /* Free HCI device */ |
| 3009 | void hci_free_dev(struct hci_dev *hdev) |
| 3010 | { |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 3011 | /* will free via device release */ |
| 3012 | put_device(&hdev->dev); |
| 3013 | } |
| 3014 | EXPORT_SYMBOL(hci_free_dev); |
| 3015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | /* Register HCI device */ |
| 3017 | int hci_register_dev(struct hci_dev *hdev) |
| 3018 | { |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3019 | int id, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | |
Marcel Holtmann | 74292d5 | 2014-07-06 15:50:27 +0200 | [diff] [blame] | 3021 | if (!hdev->open || !hdev->close || !hdev->send) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | return -EINVAL; |
| 3023 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 3024 | /* Do not allow HCI_AMP devices to register at index 0, |
| 3025 | * so the index can be used as the AMP controller ID. |
| 3026 | */ |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3027 | switch (hdev->dev_type) { |
| 3028 | case HCI_BREDR: |
| 3029 | id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); |
| 3030 | break; |
| 3031 | case HCI_AMP: |
| 3032 | id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); |
| 3033 | break; |
| 3034 | default: |
| 3035 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3037 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3038 | if (id < 0) |
| 3039 | return id; |
| 3040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | sprintf(hdev->name, "hci%d", id); |
| 3042 | hdev->id = id; |
Andrei Emeltchenko | 2d8b3a1 | 2012-04-16 16:32:04 +0300 | [diff] [blame] | 3043 | |
| 3044 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
| 3045 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 3046 | hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 3047 | WQ_MEM_RECLAIM, 1, hdev->name); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3048 | if (!hdev->workqueue) { |
| 3049 | error = -ENOMEM; |
| 3050 | goto err; |
| 3051 | } |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3052 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 3053 | hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 3054 | WQ_MEM_RECLAIM, 1, hdev->name); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 3055 | if (!hdev->req_workqueue) { |
| 3056 | destroy_workqueue(hdev->workqueue); |
| 3057 | error = -ENOMEM; |
| 3058 | goto err; |
| 3059 | } |
| 3060 | |
Marcel Holtmann | 0153e2e | 2013-10-17 17:24:17 -0700 | [diff] [blame] | 3061 | if (!IS_ERR_OR_NULL(bt_debugfs)) |
| 3062 | hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs); |
| 3063 | |
Marcel Holtmann | bdc3e0f | 2013-10-17 17:24:19 -0700 | [diff] [blame] | 3064 | dev_set_name(&hdev->dev, "%s", hdev->name); |
| 3065 | |
| 3066 | error = device_add(&hdev->dev); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3067 | if (error < 0) |
Johan Hedberg | 5450691 | 2014-08-08 09:32:51 +0300 | [diff] [blame] | 3068 | goto err_wqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 3070 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3071 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, |
| 3072 | hdev); |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 3073 | if (hdev->rfkill) { |
| 3074 | if (rfkill_register(hdev->rfkill) < 0) { |
| 3075 | rfkill_destroy(hdev->rfkill); |
| 3076 | hdev->rfkill = NULL; |
| 3077 | } |
| 3078 | } |
| 3079 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 3080 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3081 | hci_dev_set_flag(hdev, HCI_RFKILLED); |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 3082 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3083 | hci_dev_set_flag(hdev, HCI_SETUP); |
| 3084 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 3085 | |
Marcel Holtmann | 01cd340 | 2013-10-06 01:16:22 -0700 | [diff] [blame] | 3086 | if (hdev->dev_type == HCI_BREDR) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3087 | /* Assume BR/EDR support until proven otherwise (such as |
| 3088 | * through reading supported features during init. |
| 3089 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3090 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3091 | } |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 3092 | |
Gustavo Padovan | fcee337 | 2013-07-11 11:34:28 +0100 | [diff] [blame] | 3093 | write_lock(&hci_dev_list_lock); |
| 3094 | list_add(&hdev->list, &hci_dev_list); |
| 3095 | write_unlock(&hci_dev_list_lock); |
| 3096 | |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 3097 | /* Devices that are marked for raw-only usage are unconfigured |
| 3098 | * and should not be included in normal operation. |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 3099 | */ |
| 3100 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3101 | hci_dev_set_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 3102 | |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 3103 | hci_sock_dev_event(hdev, HCI_DEV_REG); |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 3104 | hci_dev_hold(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 3106 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Marcel Holtmann | fbe96d6 | 2012-10-30 01:35:40 -0700 | [diff] [blame] | 3107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3109 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3110 | err_wqueue: |
| 3111 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 3112 | destroy_workqueue(hdev->req_workqueue); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3113 | err: |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3114 | ida_simple_remove(&hci_index_ida, hdev->id); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3115 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3116 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | } |
| 3118 | EXPORT_SYMBOL(hci_register_dev); |
| 3119 | |
| 3120 | /* Unregister HCI device */ |
David Herrmann | 5973563 | 2011-10-26 10:43:19 +0200 | [diff] [blame] | 3121 | void hci_unregister_dev(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | { |
Marcel Holtmann | 2d7cc19 | 2015-04-04 21:59:27 -0700 | [diff] [blame] | 3123 | int id; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3124 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 3125 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3127 | hci_dev_set_flag(hdev, HCI_UNREGISTER); |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 3128 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3129 | id = hdev->id; |
| 3130 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3131 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3133 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | |
| 3135 | hci_dev_do_close(hdev); |
| 3136 | |
Gustavo Padovan | b9b5ef1 | 2012-11-21 00:50:21 -0200 | [diff] [blame] | 3137 | cancel_work_sync(&hdev->power_on); |
| 3138 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 3139 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3140 | !hci_dev_test_flag(hdev, HCI_SETUP) && |
| 3141 | !hci_dev_test_flag(hdev, HCI_CONFIG)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3142 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3143 | mgmt_index_removed(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3144 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3145 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 3146 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3147 | /* mgmt_index_removed should take care of emptying the |
| 3148 | * pending list */ |
| 3149 | BUG_ON(!list_empty(&hdev->mgmt_pending)); |
| 3150 | |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 3151 | hci_sock_dev_event(hdev, HCI_DEV_UNREG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 3153 | if (hdev->rfkill) { |
| 3154 | rfkill_unregister(hdev->rfkill); |
| 3155 | rfkill_destroy(hdev->rfkill); |
| 3156 | } |
| 3157 | |
Marcel Holtmann | bdc3e0f | 2013-10-17 17:24:19 -0700 | [diff] [blame] | 3158 | device_del(&hdev->dev); |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 3159 | |
Marcel Holtmann | 0153e2e | 2013-10-17 17:24:17 -0700 | [diff] [blame] | 3160 | debugfs_remove_recursive(hdev->debugfs); |
| 3161 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3162 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 3163 | destroy_workqueue(hdev->req_workqueue); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3164 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3165 | hci_dev_lock(hdev); |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3166 | hci_bdaddr_list_clear(&hdev->blacklist); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 3167 | hci_bdaddr_list_clear(&hdev->whitelist); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 3168 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 3169 | hci_link_keys_clear(hdev); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 3170 | hci_smp_ltks_clear(hdev); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 3171 | hci_smp_irks_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3172 | hci_remote_oob_data_clear(hdev); |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 3173 | hci_adv_instances_clear(hdev); |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3174 | hci_bdaddr_list_clear(&hdev->le_white_list); |
Johan Hedberg | 373110c | 2014-07-02 17:37:25 +0300 | [diff] [blame] | 3175 | hci_conn_params_clear_all(hdev); |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3176 | hci_discovery_filter_clear(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3177 | hci_dev_unlock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 3178 | |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 3179 | hci_dev_put(hdev); |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3180 | |
| 3181 | ida_simple_remove(&hci_index_ida, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | } |
| 3183 | EXPORT_SYMBOL(hci_unregister_dev); |
| 3184 | |
| 3185 | /* Suspend HCI device */ |
| 3186 | int hci_suspend_dev(struct hci_dev *hdev) |
| 3187 | { |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 3188 | hci_sock_dev_event(hdev, HCI_DEV_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | return 0; |
| 3190 | } |
| 3191 | EXPORT_SYMBOL(hci_suspend_dev); |
| 3192 | |
| 3193 | /* Resume HCI device */ |
| 3194 | int hci_resume_dev(struct hci_dev *hdev) |
| 3195 | { |
Marcel Holtmann | 05fcd4c | 2015-10-25 23:29:22 +0100 | [diff] [blame] | 3196 | hci_sock_dev_event(hdev, HCI_DEV_RESUME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | return 0; |
| 3198 | } |
| 3199 | EXPORT_SYMBOL(hci_resume_dev); |
| 3200 | |
Marcel Holtmann | 75e0569 | 2014-11-02 08:15:38 +0100 | [diff] [blame] | 3201 | /* Reset HCI device */ |
| 3202 | int hci_reset_dev(struct hci_dev *hdev) |
| 3203 | { |
| 3204 | const u8 hw_err[] = { HCI_EV_HARDWARE_ERROR, 0x01, 0x00 }; |
| 3205 | struct sk_buff *skb; |
| 3206 | |
| 3207 | skb = bt_skb_alloc(3, GFP_ATOMIC); |
| 3208 | if (!skb) |
| 3209 | return -ENOMEM; |
| 3210 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3211 | hci_skb_pkt_type(skb) = HCI_EVENT_PKT; |
Marcel Holtmann | 75e0569 | 2014-11-02 08:15:38 +0100 | [diff] [blame] | 3212 | memcpy(skb_put(skb, 3), hw_err, 3); |
| 3213 | |
| 3214 | /* Send Hardware Error to upper stack */ |
| 3215 | return hci_recv_frame(hdev, skb); |
| 3216 | } |
| 3217 | EXPORT_SYMBOL(hci_reset_dev); |
| 3218 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3219 | /* Receive frame from HCI drivers */ |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 3220 | int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3221 | { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3222 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3223 | && !test_bit(HCI_INIT, &hdev->flags))) { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3224 | kfree_skb(skb); |
| 3225 | return -ENXIO; |
| 3226 | } |
| 3227 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3228 | if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT && |
| 3229 | hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT && |
| 3230 | hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) { |
Marcel Holtmann | fe806dc | 2015-10-08 03:14:28 +0200 | [diff] [blame] | 3231 | kfree_skb(skb); |
| 3232 | return -EINVAL; |
| 3233 | } |
| 3234 | |
Jorrit Schippers | d82603c | 2012-12-27 17:33:02 +0100 | [diff] [blame] | 3235 | /* Incoming skb */ |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3236 | bt_cb(skb)->incoming = 1; |
| 3237 | |
| 3238 | /* Time stamp */ |
| 3239 | __net_timestamp(skb); |
| 3240 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3241 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3242 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 3243 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3244 | return 0; |
| 3245 | } |
| 3246 | EXPORT_SYMBOL(hci_recv_frame); |
| 3247 | |
Marcel Holtmann | e875ff8 | 2015-10-07 16:38:35 +0200 | [diff] [blame] | 3248 | /* Receive diagnostic message from HCI drivers */ |
| 3249 | int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb) |
| 3250 | { |
Marcel Holtmann | 581d6fd | 2015-10-09 16:13:51 +0200 | [diff] [blame] | 3251 | /* Mark as diagnostic packet */ |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3252 | hci_skb_pkt_type(skb) = HCI_DIAG_PKT; |
Marcel Holtmann | 581d6fd | 2015-10-09 16:13:51 +0200 | [diff] [blame] | 3253 | |
Marcel Holtmann | e875ff8 | 2015-10-07 16:38:35 +0200 | [diff] [blame] | 3254 | /* Time stamp */ |
| 3255 | __net_timestamp(skb); |
| 3256 | |
Marcel Holtmann | 581d6fd | 2015-10-09 16:13:51 +0200 | [diff] [blame] | 3257 | skb_queue_tail(&hdev->rx_q, skb); |
| 3258 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | e875ff8 | 2015-10-07 16:38:35 +0200 | [diff] [blame] | 3259 | |
Marcel Holtmann | e875ff8 | 2015-10-07 16:38:35 +0200 | [diff] [blame] | 3260 | return 0; |
| 3261 | } |
| 3262 | EXPORT_SYMBOL(hci_recv_diag); |
| 3263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | /* ---- Interface to upper protocols ---- */ |
| 3265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | int hci_register_cb(struct hci_cb *cb) |
| 3267 | { |
| 3268 | BT_DBG("%p name %s", cb, cb->name); |
| 3269 | |
Johan Hedberg | fba7ecf | 2015-02-18 14:53:55 +0200 | [diff] [blame] | 3270 | mutex_lock(&hci_cb_list_lock); |
Johan Hedberg | 00629e0 | 2015-02-18 14:53:54 +0200 | [diff] [blame] | 3271 | list_add_tail(&cb->list, &hci_cb_list); |
Johan Hedberg | fba7ecf | 2015-02-18 14:53:55 +0200 | [diff] [blame] | 3272 | mutex_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
| 3274 | return 0; |
| 3275 | } |
| 3276 | EXPORT_SYMBOL(hci_register_cb); |
| 3277 | |
| 3278 | int hci_unregister_cb(struct hci_cb *cb) |
| 3279 | { |
| 3280 | BT_DBG("%p name %s", cb, cb->name); |
| 3281 | |
Johan Hedberg | fba7ecf | 2015-02-18 14:53:55 +0200 | [diff] [blame] | 3282 | mutex_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | list_del(&cb->list); |
Johan Hedberg | fba7ecf | 2015-02-18 14:53:55 +0200 | [diff] [blame] | 3284 | mutex_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | |
| 3286 | return 0; |
| 3287 | } |
| 3288 | EXPORT_SYMBOL(hci_unregister_cb); |
| 3289 | |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 3290 | static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | { |
Marcel Holtmann | cdc52fa | 2014-07-06 15:36:15 +0200 | [diff] [blame] | 3292 | int err; |
| 3293 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3294 | BT_DBG("%s type %d len %d", hdev->name, hci_skb_pkt_type(skb), |
| 3295 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3297 | /* Time stamp */ |
| 3298 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3300 | /* Send copy to monitor */ |
| 3301 | hci_send_to_monitor(hdev, skb); |
| 3302 | |
| 3303 | if (atomic_read(&hdev->promisc)) { |
| 3304 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 3305 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 3309 | skb_orphan(skb); |
| 3310 | |
Marcel Holtmann | 73d0d3c | 2015-10-04 23:34:01 +0200 | [diff] [blame] | 3311 | if (!test_bit(HCI_RUNNING, &hdev->flags)) { |
| 3312 | kfree_skb(skb); |
| 3313 | return; |
| 3314 | } |
| 3315 | |
Marcel Holtmann | cdc52fa | 2014-07-06 15:36:15 +0200 | [diff] [blame] | 3316 | err = hdev->send(hdev, skb); |
| 3317 | if (err < 0) { |
| 3318 | BT_ERR("%s sending frame failed (%d)", hdev->name, err); |
| 3319 | kfree_skb(skb); |
| 3320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 3323 | /* Send HCI command */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 3324 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, |
| 3325 | const void *param) |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 3326 | { |
| 3327 | struct sk_buff *skb; |
| 3328 | |
| 3329 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 3330 | |
| 3331 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 3332 | if (!skb) { |
| 3333 | BT_ERR("%s no memory for command", hdev->name); |
| 3334 | return -ENOMEM; |
| 3335 | } |
| 3336 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 3337 | /* Stand-alone HCI commands must be flagged as |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 3338 | * single-command requests. |
| 3339 | */ |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 3340 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 3341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3343 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | |
| 3345 | return 0; |
| 3346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | |
| 3348 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 3349 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | { |
| 3351 | struct hci_command_hdr *hdr; |
| 3352 | |
| 3353 | if (!hdev->sent_cmd) |
| 3354 | return NULL; |
| 3355 | |
| 3356 | hdr = (void *) hdev->sent_cmd->data; |
| 3357 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 3358 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | return NULL; |
| 3360 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3361 | BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
| 3363 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 3364 | } |
| 3365 | |
Loic Poulain | fbef168 | 2015-09-29 15:05:44 +0200 | [diff] [blame] | 3366 | /* Send HCI command and wait for command commplete event */ |
| 3367 | struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, |
| 3368 | const void *param, u32 timeout) |
| 3369 | { |
| 3370 | struct sk_buff *skb; |
| 3371 | |
| 3372 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 3373 | return ERR_PTR(-ENETDOWN); |
| 3374 | |
| 3375 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); |
| 3376 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 3377 | hci_req_sync_lock(hdev); |
Loic Poulain | fbef168 | 2015-09-29 15:05:44 +0200 | [diff] [blame] | 3378 | skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 3379 | hci_req_sync_unlock(hdev); |
Loic Poulain | fbef168 | 2015-09-29 15:05:44 +0200 | [diff] [blame] | 3380 | |
| 3381 | return skb; |
| 3382 | } |
| 3383 | EXPORT_SYMBOL(hci_cmd_sync); |
| 3384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | /* Send ACL data */ |
| 3386 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 3387 | { |
| 3388 | struct hci_acl_hdr *hdr; |
| 3389 | int len = skb->len; |
| 3390 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 3391 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 3392 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 3393 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3394 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 3395 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3398 | 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] | 3399 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3401 | struct hci_conn *conn = chan->conn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | struct hci_dev *hdev = conn->hdev; |
| 3403 | struct sk_buff *list; |
| 3404 | |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 3405 | skb->len = skb_headlen(skb); |
| 3406 | skb->data_len = 0; |
| 3407 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3408 | hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | 204a6e5 | 2012-10-15 11:58:39 +0300 | [diff] [blame] | 3409 | |
| 3410 | switch (hdev->dev_type) { |
| 3411 | case HCI_BREDR: |
| 3412 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 3413 | break; |
| 3414 | case HCI_AMP: |
| 3415 | hci_add_acl_hdr(skb, chan->handle, flags); |
| 3416 | break; |
| 3417 | default: |
| 3418 | BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); |
| 3419 | return; |
| 3420 | } |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 3421 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 3422 | list = skb_shinfo(skb)->frag_list; |
| 3423 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | /* Non fragmented */ |
| 3425 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 3426 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3427 | skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | } else { |
| 3429 | /* Fragmented */ |
| 3430 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 3431 | |
| 3432 | skb_shinfo(skb)->frag_list = NULL; |
| 3433 | |
Jukka Rissanen | 9cfd5a2 | 2014-10-29 10:16:00 +0200 | [diff] [blame] | 3434 | /* Queue all fragments atomically. We need to use spin_lock_bh |
| 3435 | * here because of 6LoWPAN links, as there this function is |
| 3436 | * called from softirq and using normal spin lock could cause |
| 3437 | * deadlocks. |
| 3438 | */ |
| 3439 | spin_lock_bh(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3441 | __skb_queue_tail(queue, skb); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 3442 | |
| 3443 | flags &= ~ACL_START; |
| 3444 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | do { |
| 3446 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3447 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3448 | hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 3449 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | |
| 3451 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 3452 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3453 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | } while (list); |
| 3455 | |
Jukka Rissanen | 9cfd5a2 | 2014-10-29 10:16:00 +0200 | [diff] [blame] | 3456 | spin_unlock_bh(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) |
| 3461 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3462 | struct hci_dev *hdev = chan->conn->hdev; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3463 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3464 | 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] | 3465 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3466 | hci_queue_acl(chan, &chan->data_q, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3468 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | |
| 3471 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 3472 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | { |
| 3474 | struct hci_dev *hdev = conn->hdev; |
| 3475 | struct hci_sco_hdr hdr; |
| 3476 | |
| 3477 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 3478 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3479 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3480 | hdr.dlen = skb->len; |
| 3481 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 3482 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 3483 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 3484 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 3486 | hci_skb_pkt_type(skb) = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 3487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | skb_queue_tail(&conn->data_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3489 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | |
| 3492 | /* ---- HCI TX task (outgoing data) ---- */ |
| 3493 | |
| 3494 | /* HCI Connection scheduler */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3495 | static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, |
| 3496 | int *quote) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | { |
| 3498 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3499 | struct hci_conn *conn = NULL, *c; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 3500 | unsigned int num = 0, min = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3502 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | * added and removed with TX task disabled. */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3504 | |
| 3505 | rcu_read_lock(); |
| 3506 | |
| 3507 | list_for_each_entry_rcu(c, &h->list, list) { |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 3508 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 3510 | |
| 3511 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 3512 | continue; |
| 3513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | num++; |
| 3515 | |
| 3516 | if (c->sent < min) { |
| 3517 | min = c->sent; |
| 3518 | conn = c; |
| 3519 | } |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3520 | |
| 3521 | if (hci_conn_num(hdev, type) == num) |
| 3522 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3525 | rcu_read_unlock(); |
| 3526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3528 | int cnt, q; |
| 3529 | |
| 3530 | switch (conn->type) { |
| 3531 | case ACL_LINK: |
| 3532 | cnt = hdev->acl_cnt; |
| 3533 | break; |
| 3534 | case SCO_LINK: |
| 3535 | case ESCO_LINK: |
| 3536 | cnt = hdev->sco_cnt; |
| 3537 | break; |
| 3538 | case LE_LINK: |
| 3539 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 3540 | break; |
| 3541 | default: |
| 3542 | cnt = 0; |
| 3543 | BT_ERR("Unknown link type"); |
| 3544 | } |
| 3545 | |
| 3546 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | *quote = q ? q : 1; |
| 3548 | } else |
| 3549 | *quote = 0; |
| 3550 | |
| 3551 | BT_DBG("conn %p quote %d", conn, *quote); |
| 3552 | return conn; |
| 3553 | } |
| 3554 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3555 | static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | { |
| 3557 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3558 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3560 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3562 | rcu_read_lock(); |
| 3563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | /* Kill stalled connections */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3565 | list_for_each_entry_rcu(c, &h->list, list) { |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3566 | if (c->type == type && c->sent) { |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 3567 | BT_ERR("%s killing stalled connection %pMR", |
| 3568 | hdev->name, &c->dst); |
Andre Guedes | bed7174 | 2013-01-30 11:50:56 -0300 | [diff] [blame] | 3569 | hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | } |
| 3571 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3572 | |
| 3573 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | } |
| 3575 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3576 | static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
| 3577 | int *quote) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3578 | { |
| 3579 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 3580 | struct hci_chan *chan = NULL; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 3581 | unsigned int num = 0, min = ~0, cur_prio = 0; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3582 | struct hci_conn *conn; |
| 3583 | int cnt, q, conn_num = 0; |
| 3584 | |
| 3585 | BT_DBG("%s", hdev->name); |
| 3586 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3587 | rcu_read_lock(); |
| 3588 | |
| 3589 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3590 | struct hci_chan *tmp; |
| 3591 | |
| 3592 | if (conn->type != type) |
| 3593 | continue; |
| 3594 | |
| 3595 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 3596 | continue; |
| 3597 | |
| 3598 | conn_num++; |
| 3599 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 3600 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3601 | struct sk_buff *skb; |
| 3602 | |
| 3603 | if (skb_queue_empty(&tmp->data_q)) |
| 3604 | continue; |
| 3605 | |
| 3606 | skb = skb_peek(&tmp->data_q); |
| 3607 | if (skb->priority < cur_prio) |
| 3608 | continue; |
| 3609 | |
| 3610 | if (skb->priority > cur_prio) { |
| 3611 | num = 0; |
| 3612 | min = ~0; |
| 3613 | cur_prio = skb->priority; |
| 3614 | } |
| 3615 | |
| 3616 | num++; |
| 3617 | |
| 3618 | if (conn->sent < min) { |
| 3619 | min = conn->sent; |
| 3620 | chan = tmp; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | if (hci_conn_num(hdev, type) == conn_num) |
| 3625 | break; |
| 3626 | } |
| 3627 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3628 | rcu_read_unlock(); |
| 3629 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3630 | if (!chan) |
| 3631 | return NULL; |
| 3632 | |
| 3633 | switch (chan->conn->type) { |
| 3634 | case ACL_LINK: |
| 3635 | cnt = hdev->acl_cnt; |
| 3636 | break; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3637 | case AMP_LINK: |
| 3638 | cnt = hdev->block_cnt; |
| 3639 | break; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3640 | case SCO_LINK: |
| 3641 | case ESCO_LINK: |
| 3642 | cnt = hdev->sco_cnt; |
| 3643 | break; |
| 3644 | case LE_LINK: |
| 3645 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 3646 | break; |
| 3647 | default: |
| 3648 | cnt = 0; |
| 3649 | BT_ERR("Unknown link type"); |
| 3650 | } |
| 3651 | |
| 3652 | q = cnt / num; |
| 3653 | *quote = q ? q : 1; |
| 3654 | BT_DBG("chan %p quote %d", chan, *quote); |
| 3655 | return chan; |
| 3656 | } |
| 3657 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3658 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
| 3659 | { |
| 3660 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 3661 | struct hci_conn *conn; |
| 3662 | int num = 0; |
| 3663 | |
| 3664 | BT_DBG("%s", hdev->name); |
| 3665 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3666 | rcu_read_lock(); |
| 3667 | |
| 3668 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3669 | struct hci_chan *chan; |
| 3670 | |
| 3671 | if (conn->type != type) |
| 3672 | continue; |
| 3673 | |
| 3674 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 3675 | continue; |
| 3676 | |
| 3677 | num++; |
| 3678 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 3679 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3680 | struct sk_buff *skb; |
| 3681 | |
| 3682 | if (chan->sent) { |
| 3683 | chan->sent = 0; |
| 3684 | continue; |
| 3685 | } |
| 3686 | |
| 3687 | if (skb_queue_empty(&chan->data_q)) |
| 3688 | continue; |
| 3689 | |
| 3690 | skb = skb_peek(&chan->data_q); |
| 3691 | if (skb->priority >= HCI_PRIO_MAX - 1) |
| 3692 | continue; |
| 3693 | |
| 3694 | skb->priority = HCI_PRIO_MAX - 1; |
| 3695 | |
| 3696 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3697 | skb->priority); |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | if (hci_conn_num(hdev, type) == num) |
| 3701 | break; |
| 3702 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 3703 | |
| 3704 | rcu_read_unlock(); |
| 3705 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3706 | } |
| 3707 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3708 | static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) |
| 3709 | { |
| 3710 | /* Calculate count of blocks used by this packet */ |
| 3711 | return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); |
| 3712 | } |
| 3713 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3714 | static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3716 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | /* ACL tx timeout must be longer than maximum |
| 3718 | * link supervision timeout (40.9 seconds) */ |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3719 | if (!cnt && time_after(jiffies, hdev->acl_last_tx + |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 3720 | HCI_ACL_TX_TIMEOUT)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3721 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | } |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3725 | static void hci_sched_acl_pkt(struct hci_dev *hdev) |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3726 | { |
| 3727 | unsigned int cnt = hdev->acl_cnt; |
| 3728 | struct hci_chan *chan; |
| 3729 | struct sk_buff *skb; |
| 3730 | int quote; |
| 3731 | |
| 3732 | __check_timeout(hdev, cnt); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3733 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3734 | while (hdev->acl_cnt && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3735 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3736 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3737 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3738 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3739 | skb->len, skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3740 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3741 | /* Stop if priority has changed */ |
| 3742 | if (skb->priority < priority) |
| 3743 | break; |
| 3744 | |
| 3745 | skb = skb_dequeue(&chan->data_q); |
| 3746 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3747 | hci_conn_enter_active_mode(chan->conn, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3748 | bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3749 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3750 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | hdev->acl_last_tx = jiffies; |
| 3752 | |
| 3753 | hdev->acl_cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3754 | chan->sent++; |
| 3755 | chan->conn->sent++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | } |
| 3757 | } |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3758 | |
| 3759 | if (cnt != hdev->acl_cnt) |
| 3760 | hci_prio_recalculate(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | } |
| 3762 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3763 | static void hci_sched_acl_blk(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3764 | { |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3765 | unsigned int cnt = hdev->block_cnt; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3766 | struct hci_chan *chan; |
| 3767 | struct sk_buff *skb; |
| 3768 | int quote; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3769 | u8 type; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3770 | |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 3771 | __check_timeout(hdev, cnt); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3772 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3773 | BT_DBG("%s", hdev->name); |
| 3774 | |
| 3775 | if (hdev->dev_type == HCI_AMP) |
| 3776 | type = AMP_LINK; |
| 3777 | else |
| 3778 | type = ACL_LINK; |
| 3779 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3780 | while (hdev->block_cnt > 0 && |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3781 | (chan = hci_chan_sent(hdev, type, "e))) { |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3782 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3783 | while (quote > 0 && (skb = skb_peek(&chan->data_q))) { |
| 3784 | int blocks; |
| 3785 | |
| 3786 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3787 | skb->len, skb->priority); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3788 | |
| 3789 | /* Stop if priority has changed */ |
| 3790 | if (skb->priority < priority) |
| 3791 | break; |
| 3792 | |
| 3793 | skb = skb_dequeue(&chan->data_q); |
| 3794 | |
| 3795 | blocks = __get_blocks(hdev, skb); |
| 3796 | if (blocks > hdev->block_cnt) |
| 3797 | return; |
| 3798 | |
| 3799 | hci_conn_enter_active_mode(chan->conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3800 | bt_cb(skb)->force_active); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3801 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3802 | hci_send_frame(hdev, skb); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3803 | hdev->acl_last_tx = jiffies; |
| 3804 | |
| 3805 | hdev->block_cnt -= blocks; |
| 3806 | quote -= blocks; |
| 3807 | |
| 3808 | chan->sent += blocks; |
| 3809 | chan->conn->sent += blocks; |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | if (cnt != hdev->block_cnt) |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3814 | hci_prio_recalculate(hdev, type); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3815 | } |
| 3816 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3817 | static void hci_sched_acl(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3818 | { |
| 3819 | BT_DBG("%s", hdev->name); |
| 3820 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 3821 | /* No ACL link over BR/EDR controller */ |
| 3822 | if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) |
| 3823 | return; |
| 3824 | |
| 3825 | /* No AMP link over AMP controller */ |
| 3826 | if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 3827 | return; |
| 3828 | |
| 3829 | switch (hdev->flow_ctl_mode) { |
| 3830 | case HCI_FLOW_CTL_MODE_PACKET_BASED: |
| 3831 | hci_sched_acl_pkt(hdev); |
| 3832 | break; |
| 3833 | |
| 3834 | case HCI_FLOW_CTL_MODE_BLOCK_BASED: |
| 3835 | hci_sched_acl_blk(hdev); |
| 3836 | break; |
| 3837 | } |
| 3838 | } |
| 3839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | /* Schedule SCO */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3841 | static void hci_sched_sco(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | { |
| 3843 | struct hci_conn *conn; |
| 3844 | struct sk_buff *skb; |
| 3845 | int quote; |
| 3846 | |
| 3847 | BT_DBG("%s", hdev->name); |
| 3848 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3849 | if (!hci_conn_num(hdev, SCO_LINK)) |
| 3850 | return; |
| 3851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 3853 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3854 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3855 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | |
| 3857 | conn->sent++; |
| 3858 | if (conn->sent == ~0) |
| 3859 | conn->sent = 0; |
| 3860 | } |
| 3861 | } |
| 3862 | } |
| 3863 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3864 | static void hci_sched_esco(struct hci_dev *hdev) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3865 | { |
| 3866 | struct hci_conn *conn; |
| 3867 | struct sk_buff *skb; |
| 3868 | int quote; |
| 3869 | |
| 3870 | BT_DBG("%s", hdev->name); |
| 3871 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3872 | if (!hci_conn_num(hdev, ESCO_LINK)) |
| 3873 | return; |
| 3874 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3875 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, |
| 3876 | "e))) { |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3877 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 3878 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3879 | hci_send_frame(hdev, skb); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 3880 | |
| 3881 | conn->sent++; |
| 3882 | if (conn->sent == ~0) |
| 3883 | conn->sent = 0; |
| 3884 | } |
| 3885 | } |
| 3886 | } |
| 3887 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3888 | static void hci_sched_le(struct hci_dev *hdev) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3889 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3890 | struct hci_chan *chan; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3891 | struct sk_buff *skb; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3892 | int quote, cnt, tmp; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3893 | |
| 3894 | BT_DBG("%s", hdev->name); |
| 3895 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 3896 | if (!hci_conn_num(hdev, LE_LINK)) |
| 3897 | return; |
| 3898 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3899 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3900 | /* LE tx timeout must be longer than maximum |
| 3901 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3902 | if (!hdev->le_cnt && hdev->le_pkts && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3903 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 3904 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3908 | tmp = cnt; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3909 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3910 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 3911 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3912 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3913 | skb->len, skb->priority); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3914 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 3915 | /* Stop if priority has changed */ |
| 3916 | if (skb->priority < priority) |
| 3917 | break; |
| 3918 | |
| 3919 | skb = skb_dequeue(&chan->data_q); |
| 3920 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3921 | hci_send_frame(hdev, skb); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3922 | hdev->le_last_tx = jiffies; |
| 3923 | |
| 3924 | cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3925 | chan->sent++; |
| 3926 | chan->conn->sent++; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3927 | } |
| 3928 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3929 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3930 | if (hdev->le_pkts) |
| 3931 | hdev->le_cnt = cnt; |
| 3932 | else |
| 3933 | hdev->acl_cnt = cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 3934 | |
| 3935 | if (cnt != tmp) |
| 3936 | hci_prio_recalculate(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3937 | } |
| 3938 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3939 | static void hci_tx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | { |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3941 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | struct sk_buff *skb; |
| 3943 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3944 | 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] | 3945 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3947 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Marcel Holtmann | 52de599 | 2013-09-03 18:08:38 -0700 | [diff] [blame] | 3948 | /* Schedule queues and send stuff to HCI driver */ |
| 3949 | hci_sched_acl(hdev); |
| 3950 | hci_sched_sco(hdev); |
| 3951 | hci_sched_esco(hdev); |
| 3952 | hci_sched_le(hdev); |
| 3953 | } |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 3954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | /* Send next queued raw (unknown type) packet */ |
| 3956 | while ((skb = skb_dequeue(&hdev->raw_q))) |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 3957 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | } |
| 3959 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3960 | /* ----- HCI RX task (incoming data processing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | |
| 3962 | /* ACL data packet */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3963 | 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] | 3964 | { |
| 3965 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 3966 | struct hci_conn *conn; |
| 3967 | __u16 handle, flags; |
| 3968 | |
| 3969 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 3970 | |
| 3971 | handle = __le16_to_cpu(hdr->handle); |
| 3972 | flags = hci_flags(handle); |
| 3973 | handle = hci_handle(handle); |
| 3974 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3975 | 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] | 3976 | handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | |
| 3978 | hdev->stat.acl_rx++; |
| 3979 | |
| 3980 | hci_dev_lock(hdev); |
| 3981 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 3982 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | if (conn) { |
Mat Martineau | 65983fc | 2011-12-13 15:06:02 -0800 | [diff] [blame] | 3985 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 3986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 3988 | l2cap_recv_acldata(conn, skb, flags); |
| 3989 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3991 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3992 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | kfree_skb(skb); |
| 3996 | } |
| 3997 | |
| 3998 | /* SCO data packet */ |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3999 | 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] | 4000 | { |
| 4001 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 4002 | struct hci_conn *conn; |
| 4003 | __u16 handle; |
| 4004 | |
| 4005 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 4006 | |
| 4007 | handle = __le16_to_cpu(hdr->handle); |
| 4008 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 4009 | 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] | 4010 | |
| 4011 | hdev->stat.sco_rx++; |
| 4012 | |
| 4013 | hci_dev_lock(hdev); |
| 4014 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4015 | hci_dev_unlock(hdev); |
| 4016 | |
| 4017 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4019 | sco_recv_scodata(conn, skb); |
| 4020 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4022 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4023 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | kfree_skb(skb); |
| 4027 | } |
| 4028 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4029 | static bool hci_req_is_complete(struct hci_dev *hdev) |
| 4030 | { |
| 4031 | struct sk_buff *skb; |
| 4032 | |
| 4033 | skb = skb_peek(&hdev->cmd_q); |
| 4034 | if (!skb) |
| 4035 | return true; |
| 4036 | |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 4037 | return (bt_cb(skb)->hci.req_flags & HCI_REQ_START); |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4038 | } |
| 4039 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4040 | static void hci_resend_last(struct hci_dev *hdev) |
| 4041 | { |
| 4042 | struct hci_command_hdr *sent; |
| 4043 | struct sk_buff *skb; |
| 4044 | u16 opcode; |
| 4045 | |
| 4046 | if (!hdev->sent_cmd) |
| 4047 | return; |
| 4048 | |
| 4049 | sent = (void *) hdev->sent_cmd->data; |
| 4050 | opcode = __le16_to_cpu(sent->opcode); |
| 4051 | if (opcode == HCI_OP_RESET) |
| 4052 | return; |
| 4053 | |
| 4054 | skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); |
| 4055 | if (!skb) |
| 4056 | return; |
| 4057 | |
| 4058 | skb_queue_head(&hdev->cmd_q, skb); |
| 4059 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 4060 | } |
| 4061 | |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 4062 | void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status, |
| 4063 | hci_req_complete_t *req_complete, |
| 4064 | hci_req_complete_skb_t *req_complete_skb) |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4065 | { |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4066 | struct sk_buff *skb; |
| 4067 | unsigned long flags; |
| 4068 | |
| 4069 | BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); |
| 4070 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4071 | /* If the completed command doesn't match the last one that was |
| 4072 | * sent we need to do special handling of it. |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4073 | */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4074 | if (!hci_sent_cmd_data(hdev, opcode)) { |
| 4075 | /* Some CSR based controllers generate a spontaneous |
| 4076 | * reset complete event during init and any pending |
| 4077 | * command will never be completed. In such a case we |
| 4078 | * need to resend whatever was the last sent |
| 4079 | * command. |
| 4080 | */ |
| 4081 | if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) |
| 4082 | hci_resend_last(hdev); |
| 4083 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4084 | return; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4085 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4086 | |
| 4087 | /* If the command succeeded and there's still more commands in |
| 4088 | * this request the request is not yet complete. |
| 4089 | */ |
| 4090 | if (!status && !hci_req_is_complete(hdev)) |
| 4091 | return; |
| 4092 | |
| 4093 | /* If this was the last command in a request the complete |
| 4094 | * callback would be found in hdev->sent_cmd instead of the |
| 4095 | * command queue (hdev->cmd_q). |
| 4096 | */ |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 4097 | if (bt_cb(hdev->sent_cmd)->hci.req_flags & HCI_REQ_SKB) { |
| 4098 | *req_complete_skb = bt_cb(hdev->sent_cmd)->hci.req_complete_skb; |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 4099 | return; |
| 4100 | } |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 4101 | |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 4102 | if (bt_cb(hdev->sent_cmd)->hci.req_complete) { |
| 4103 | *req_complete = bt_cb(hdev->sent_cmd)->hci.req_complete; |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 4104 | return; |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | /* Remove all pending commands belonging to this request */ |
| 4108 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 4109 | while ((skb = __skb_dequeue(&hdev->cmd_q))) { |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 4110 | if (bt_cb(skb)->hci.req_flags & HCI_REQ_START) { |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4111 | __skb_queue_head(&hdev->cmd_q, skb); |
| 4112 | break; |
| 4113 | } |
| 4114 | |
Douglas Anderson | 3bd7594 | 2016-02-19 14:25:21 -0800 | [diff] [blame] | 4115 | if (bt_cb(skb)->hci.req_flags & HCI_REQ_SKB) |
| 4116 | *req_complete_skb = bt_cb(skb)->hci.req_complete_skb; |
| 4117 | else |
| 4118 | *req_complete = bt_cb(skb)->hci.req_complete; |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4119 | kfree_skb(skb); |
| 4120 | } |
| 4121 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4122 | } |
| 4123 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 4124 | static void hci_rx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | { |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 4126 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | struct sk_buff *skb; |
| 4128 | |
| 4129 | BT_DBG("%s", hdev->name); |
| 4130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4131 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 4132 | /* Send copy to monitor */ |
| 4133 | hci_send_to_monitor(hdev, skb); |
| 4134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | if (atomic_read(&hdev->promisc)) { |
| 4136 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 4137 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | } |
| 4139 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4140 | if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4141 | kfree_skb(skb); |
| 4142 | continue; |
| 4143 | } |
| 4144 | |
| 4145 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 4146 | /* Don't process data packets in this states. */ |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 4147 | switch (hci_skb_pkt_type(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | case HCI_ACLDATA_PKT: |
| 4149 | case HCI_SCODATA_PKT: |
| 4150 | kfree_skb(skb); |
| 4151 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | /* Process frame */ |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 4156 | switch (hci_skb_pkt_type(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | case HCI_EVENT_PKT: |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 4158 | BT_DBG("%s Event packet", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | hci_event_packet(hdev, skb); |
| 4160 | break; |
| 4161 | |
| 4162 | case HCI_ACLDATA_PKT: |
| 4163 | BT_DBG("%s ACL data packet", hdev->name); |
| 4164 | hci_acldata_packet(hdev, skb); |
| 4165 | break; |
| 4166 | |
| 4167 | case HCI_SCODATA_PKT: |
| 4168 | BT_DBG("%s SCO data packet", hdev->name); |
| 4169 | hci_scodata_packet(hdev, skb); |
| 4170 | break; |
| 4171 | |
| 4172 | default: |
| 4173 | kfree_skb(skb); |
| 4174 | break; |
| 4175 | } |
| 4176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 4179 | static void hci_cmd_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | { |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 4181 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | struct sk_buff *skb; |
| 4183 | |
Andrei Emeltchenko | 2104786 | 2012-07-10 15:27:47 +0300 | [diff] [blame] | 4184 | BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, |
| 4185 | atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4188 | if (atomic_read(&hdev->cmd_cnt)) { |
| 4189 | skb = skb_dequeue(&hdev->cmd_q); |
| 4190 | if (!skb) |
| 4191 | return; |
| 4192 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 4193 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | |
Marcel Holtmann | a675d7f | 2013-09-03 18:11:07 -0700 | [diff] [blame] | 4195 | hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 4196 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | atomic_dec(&hdev->cmd_cnt); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4198 | hci_send_frame(hdev, skb); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 4199 | if (test_bit(HCI_RESET, &hdev->flags)) |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 4200 | cancel_delayed_work(&hdev->cmd_timer); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 4201 | else |
Marcel Holtmann | 65cc2b4 | 2014-06-16 12:30:56 +0200 | [diff] [blame] | 4202 | schedule_delayed_work(&hdev->cmd_timer, |
| 4203 | HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | } else { |
| 4205 | skb_queue_head(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 4206 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | } |
| 4208 | } |
| 4209 | } |