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> |
| 37 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 38 | #include "smp.h" |
| 39 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 40 | static void hci_rx_work(struct work_struct *work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 41 | static void hci_cmd_work(struct work_struct *work); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 42 | static void hci_tx_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* HCI device list */ |
| 45 | LIST_HEAD(hci_dev_list); |
| 46 | DEFINE_RWLOCK(hci_dev_list_lock); |
| 47 | |
| 48 | /* HCI callback list */ |
| 49 | LIST_HEAD(hci_cb_list); |
| 50 | DEFINE_RWLOCK(hci_cb_list_lock); |
| 51 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 52 | /* HCI ID Numbering */ |
| 53 | static DEFINE_IDA(hci_index_ida); |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* ---- HCI notifications ---- */ |
| 56 | |
Marcel Holtmann | 6516455 | 2005-10-28 19:20:48 +0200 | [diff] [blame] | 57 | static void hci_notify(struct hci_dev *hdev, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Marcel Holtmann | 040030e | 2012-02-20 14:50:37 +0100 | [diff] [blame] | 59 | hci_sock_dev_event(hdev, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 62 | /* ---- HCI debugfs entries ---- */ |
| 63 | |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 64 | static ssize_t dut_mode_read(struct file *file, char __user *user_buf, |
| 65 | size_t count, loff_t *ppos) |
| 66 | { |
| 67 | struct hci_dev *hdev = file->private_data; |
| 68 | char buf[3]; |
| 69 | |
| 70 | buf[0] = test_bit(HCI_DUT_MODE, &hdev->dev_flags) ? 'Y': 'N'; |
| 71 | buf[1] = '\n'; |
| 72 | buf[2] = '\0'; |
| 73 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 74 | } |
| 75 | |
| 76 | static ssize_t dut_mode_write(struct file *file, const char __user *user_buf, |
| 77 | size_t count, loff_t *ppos) |
| 78 | { |
| 79 | struct hci_dev *hdev = file->private_data; |
| 80 | struct sk_buff *skb; |
| 81 | char buf[32]; |
| 82 | size_t buf_size = min(count, (sizeof(buf)-1)); |
| 83 | bool enable; |
| 84 | int err; |
| 85 | |
| 86 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 87 | return -ENETDOWN; |
| 88 | |
| 89 | if (copy_from_user(buf, user_buf, buf_size)) |
| 90 | return -EFAULT; |
| 91 | |
| 92 | buf[buf_size] = '\0'; |
| 93 | if (strtobool(buf, &enable)) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | if (enable == test_bit(HCI_DUT_MODE, &hdev->dev_flags)) |
| 97 | return -EALREADY; |
| 98 | |
| 99 | hci_req_lock(hdev); |
| 100 | if (enable) |
| 101 | skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL, |
| 102 | HCI_CMD_TIMEOUT); |
| 103 | else |
| 104 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, |
| 105 | HCI_CMD_TIMEOUT); |
| 106 | hci_req_unlock(hdev); |
| 107 | |
| 108 | if (IS_ERR(skb)) |
| 109 | return PTR_ERR(skb); |
| 110 | |
| 111 | err = -bt_to_errno(skb->data[0]); |
| 112 | kfree_skb(skb); |
| 113 | |
| 114 | if (err < 0) |
| 115 | return err; |
| 116 | |
| 117 | change_bit(HCI_DUT_MODE, &hdev->dev_flags); |
| 118 | |
| 119 | return count; |
| 120 | } |
| 121 | |
| 122 | static const struct file_operations dut_mode_fops = { |
| 123 | .open = simple_open, |
| 124 | .read = dut_mode_read, |
| 125 | .write = dut_mode_write, |
| 126 | .llseek = default_llseek, |
| 127 | }; |
| 128 | |
Marcel Holtmann | dfb826a | 2013-10-18 12:04:46 -0700 | [diff] [blame] | 129 | static int features_show(struct seq_file *f, void *ptr) |
| 130 | { |
| 131 | struct hci_dev *hdev = f->private; |
| 132 | u8 p; |
| 133 | |
| 134 | hci_dev_lock(hdev); |
| 135 | for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { |
Marcel Holtmann | cfbb2b5 | 2013-10-19 02:25:33 -0700 | [diff] [blame] | 136 | seq_printf(f, "%2u: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x " |
Marcel Holtmann | dfb826a | 2013-10-18 12:04:46 -0700 | [diff] [blame] | 137 | "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", p, |
| 138 | hdev->features[p][0], hdev->features[p][1], |
| 139 | hdev->features[p][2], hdev->features[p][3], |
| 140 | hdev->features[p][4], hdev->features[p][5], |
| 141 | hdev->features[p][6], hdev->features[p][7]); |
| 142 | } |
Marcel Holtmann | cfbb2b5 | 2013-10-19 02:25:33 -0700 | [diff] [blame] | 143 | if (lmp_le_capable(hdev)) |
| 144 | seq_printf(f, "LE: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x " |
| 145 | "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", |
| 146 | hdev->le_features[0], hdev->le_features[1], |
| 147 | hdev->le_features[2], hdev->le_features[3], |
| 148 | hdev->le_features[4], hdev->le_features[5], |
| 149 | hdev->le_features[6], hdev->le_features[7]); |
Marcel Holtmann | dfb826a | 2013-10-18 12:04:46 -0700 | [diff] [blame] | 150 | hci_dev_unlock(hdev); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int features_open(struct inode *inode, struct file *file) |
| 156 | { |
| 157 | return single_open(file, features_show, inode->i_private); |
| 158 | } |
| 159 | |
| 160 | static const struct file_operations features_fops = { |
| 161 | .open = features_open, |
| 162 | .read = seq_read, |
| 163 | .llseek = seq_lseek, |
| 164 | .release = single_release, |
| 165 | }; |
| 166 | |
Marcel Holtmann | 70afe0b | 2013-10-17 17:24:14 -0700 | [diff] [blame] | 167 | static int blacklist_show(struct seq_file *f, void *p) |
| 168 | { |
| 169 | struct hci_dev *hdev = f->private; |
| 170 | struct bdaddr_list *b; |
| 171 | |
| 172 | hci_dev_lock(hdev); |
| 173 | list_for_each_entry(b, &hdev->blacklist, list) |
Marcel Holtmann | b25f078 | 2013-10-17 17:24:20 -0700 | [diff] [blame] | 174 | seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type); |
Marcel Holtmann | 70afe0b | 2013-10-17 17:24:14 -0700 | [diff] [blame] | 175 | hci_dev_unlock(hdev); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int blacklist_open(struct inode *inode, struct file *file) |
| 181 | { |
| 182 | return single_open(file, blacklist_show, inode->i_private); |
| 183 | } |
| 184 | |
| 185 | static const struct file_operations blacklist_fops = { |
| 186 | .open = blacklist_open, |
| 187 | .read = seq_read, |
| 188 | .llseek = seq_lseek, |
| 189 | .release = single_release, |
| 190 | }; |
| 191 | |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 192 | static int uuids_show(struct seq_file *f, void *p) |
| 193 | { |
| 194 | struct hci_dev *hdev = f->private; |
| 195 | struct bt_uuid *uuid; |
| 196 | |
| 197 | hci_dev_lock(hdev); |
| 198 | list_for_each_entry(uuid, &hdev->uuids, list) { |
Marcel Holtmann | 58f01aa | 2013-10-19 09:31:59 -0700 | [diff] [blame] | 199 | u8 i, val[16]; |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 200 | |
Marcel Holtmann | 58f01aa | 2013-10-19 09:31:59 -0700 | [diff] [blame] | 201 | /* The Bluetooth UUID values are stored in big endian, |
| 202 | * but with reversed byte order. So convert them into |
| 203 | * the right order for the %pUb modifier. |
| 204 | */ |
| 205 | for (i = 0; i < 16; i++) |
| 206 | val[i] = uuid->uuid[15 - i]; |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 207 | |
Marcel Holtmann | 58f01aa | 2013-10-19 09:31:59 -0700 | [diff] [blame] | 208 | seq_printf(f, "%pUb\n", val); |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 209 | } |
| 210 | hci_dev_unlock(hdev); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int uuids_open(struct inode *inode, struct file *file) |
| 216 | { |
| 217 | return single_open(file, uuids_show, inode->i_private); |
| 218 | } |
| 219 | |
| 220 | static const struct file_operations uuids_fops = { |
| 221 | .open = uuids_open, |
| 222 | .read = seq_read, |
| 223 | .llseek = seq_lseek, |
| 224 | .release = single_release, |
| 225 | }; |
| 226 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 227 | static int inquiry_cache_show(struct seq_file *f, void *p) |
| 228 | { |
| 229 | struct hci_dev *hdev = f->private; |
| 230 | struct discovery_state *cache = &hdev->discovery; |
| 231 | struct inquiry_entry *e; |
| 232 | |
| 233 | hci_dev_lock(hdev); |
| 234 | |
| 235 | list_for_each_entry(e, &cache->all, all) { |
| 236 | struct inquiry_data *data = &e->data; |
| 237 | seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n", |
| 238 | &data->bdaddr, |
| 239 | data->pscan_rep_mode, data->pscan_period_mode, |
| 240 | data->pscan_mode, data->dev_class[2], |
| 241 | data->dev_class[1], data->dev_class[0], |
| 242 | __le16_to_cpu(data->clock_offset), |
| 243 | data->rssi, data->ssp_mode, e->timestamp); |
| 244 | } |
| 245 | |
| 246 | hci_dev_unlock(hdev); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int inquiry_cache_open(struct inode *inode, struct file *file) |
| 252 | { |
| 253 | return single_open(file, inquiry_cache_show, inode->i_private); |
| 254 | } |
| 255 | |
| 256 | static const struct file_operations inquiry_cache_fops = { |
| 257 | .open = inquiry_cache_open, |
| 258 | .read = seq_read, |
| 259 | .llseek = seq_lseek, |
| 260 | .release = single_release, |
| 261 | }; |
| 262 | |
Marcel Holtmann | 02d08d1 | 2013-10-18 12:04:52 -0700 | [diff] [blame] | 263 | static int link_keys_show(struct seq_file *f, void *ptr) |
| 264 | { |
| 265 | struct hci_dev *hdev = f->private; |
| 266 | struct list_head *p, *n; |
| 267 | |
| 268 | hci_dev_lock(hdev); |
| 269 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 270 | struct link_key *key = list_entry(p, struct link_key, list); |
| 271 | seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type, |
| 272 | HCI_LINK_KEY_SIZE, key->val, key->pin_len); |
| 273 | } |
| 274 | hci_dev_unlock(hdev); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int link_keys_open(struct inode *inode, struct file *file) |
| 280 | { |
| 281 | return single_open(file, link_keys_show, inode->i_private); |
| 282 | } |
| 283 | |
| 284 | static const struct file_operations link_keys_fops = { |
| 285 | .open = link_keys_open, |
| 286 | .read = seq_read, |
| 287 | .llseek = seq_lseek, |
| 288 | .release = single_release, |
| 289 | }; |
| 290 | |
Marcel Holtmann | babdbb3 | 2013-10-18 12:04:51 -0700 | [diff] [blame] | 291 | static int dev_class_show(struct seq_file *f, void *ptr) |
| 292 | { |
| 293 | struct hci_dev *hdev = f->private; |
| 294 | |
| 295 | hci_dev_lock(hdev); |
| 296 | seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2], |
| 297 | hdev->dev_class[1], hdev->dev_class[0]); |
| 298 | hci_dev_unlock(hdev); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int dev_class_open(struct inode *inode, struct file *file) |
| 304 | { |
| 305 | return single_open(file, dev_class_show, inode->i_private); |
| 306 | } |
| 307 | |
| 308 | static const struct file_operations dev_class_fops = { |
| 309 | .open = dev_class_open, |
| 310 | .read = seq_read, |
| 311 | .llseek = seq_lseek, |
| 312 | .release = single_release, |
| 313 | }; |
| 314 | |
Marcel Holtmann | 041000b | 2013-10-17 12:02:31 -0700 | [diff] [blame] | 315 | static int voice_setting_get(void *data, u64 *val) |
| 316 | { |
| 317 | struct hci_dev *hdev = data; |
| 318 | |
| 319 | hci_dev_lock(hdev); |
| 320 | *val = hdev->voice_setting; |
| 321 | hci_dev_unlock(hdev); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | DEFINE_SIMPLE_ATTRIBUTE(voice_setting_fops, voice_setting_get, |
| 327 | NULL, "0x%4.4llx\n"); |
| 328 | |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame] | 329 | static int auto_accept_delay_set(void *data, u64 val) |
| 330 | { |
| 331 | struct hci_dev *hdev = data; |
| 332 | |
| 333 | hci_dev_lock(hdev); |
| 334 | hdev->auto_accept_delay = val; |
| 335 | hci_dev_unlock(hdev); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int auto_accept_delay_get(void *data, u64 *val) |
| 341 | { |
| 342 | struct hci_dev *hdev = data; |
| 343 | |
| 344 | hci_dev_lock(hdev); |
| 345 | *val = hdev->auto_accept_delay; |
| 346 | hci_dev_unlock(hdev); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get, |
| 352 | auto_accept_delay_set, "%llu\n"); |
| 353 | |
Marcel Holtmann | 06f5b77 | 2013-10-19 07:09:11 -0700 | [diff] [blame] | 354 | static int ssp_debug_mode_set(void *data, u64 val) |
| 355 | { |
| 356 | struct hci_dev *hdev = data; |
| 357 | struct sk_buff *skb; |
| 358 | __u8 mode; |
| 359 | int err; |
| 360 | |
| 361 | if (val != 0 && val != 1) |
| 362 | return -EINVAL; |
| 363 | |
| 364 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 365 | return -ENETDOWN; |
| 366 | |
| 367 | hci_req_lock(hdev); |
| 368 | mode = val; |
| 369 | skb = __hci_cmd_sync(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, sizeof(mode), |
| 370 | &mode, HCI_CMD_TIMEOUT); |
| 371 | hci_req_unlock(hdev); |
| 372 | |
| 373 | if (IS_ERR(skb)) |
| 374 | return PTR_ERR(skb); |
| 375 | |
| 376 | err = -bt_to_errno(skb->data[0]); |
| 377 | kfree_skb(skb); |
| 378 | |
| 379 | if (err < 0) |
| 380 | return err; |
| 381 | |
| 382 | hci_dev_lock(hdev); |
| 383 | hdev->ssp_debug_mode = val; |
| 384 | hci_dev_unlock(hdev); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int ssp_debug_mode_get(void *data, u64 *val) |
| 390 | { |
| 391 | struct hci_dev *hdev = data; |
| 392 | |
| 393 | hci_dev_lock(hdev); |
| 394 | *val = hdev->ssp_debug_mode; |
| 395 | hci_dev_unlock(hdev); |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | DEFINE_SIMPLE_ATTRIBUTE(ssp_debug_mode_fops, ssp_debug_mode_get, |
| 401 | ssp_debug_mode_set, "%llu\n"); |
| 402 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 403 | static ssize_t force_sc_support_read(struct file *file, char __user *user_buf, |
| 404 | size_t count, loff_t *ppos) |
| 405 | { |
| 406 | struct hci_dev *hdev = file->private_data; |
| 407 | char buf[3]; |
| 408 | |
| 409 | buf[0] = test_bit(HCI_FORCE_SC, &hdev->dev_flags) ? 'Y': 'N'; |
| 410 | buf[1] = '\n'; |
| 411 | buf[2] = '\0'; |
| 412 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 413 | } |
| 414 | |
| 415 | static ssize_t force_sc_support_write(struct file *file, |
| 416 | const char __user *user_buf, |
| 417 | size_t count, loff_t *ppos) |
| 418 | { |
| 419 | struct hci_dev *hdev = file->private_data; |
| 420 | char buf[32]; |
| 421 | size_t buf_size = min(count, (sizeof(buf)-1)); |
| 422 | bool enable; |
| 423 | |
| 424 | if (test_bit(HCI_UP, &hdev->flags)) |
| 425 | return -EBUSY; |
| 426 | |
| 427 | if (copy_from_user(buf, user_buf, buf_size)) |
| 428 | return -EFAULT; |
| 429 | |
| 430 | buf[buf_size] = '\0'; |
| 431 | if (strtobool(buf, &enable)) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | if (enable == test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
| 435 | return -EALREADY; |
| 436 | |
| 437 | change_bit(HCI_FORCE_SC, &hdev->dev_flags); |
| 438 | |
| 439 | return count; |
| 440 | } |
| 441 | |
| 442 | static const struct file_operations force_sc_support_fops = { |
| 443 | .open = simple_open, |
| 444 | .read = force_sc_support_read, |
| 445 | .write = force_sc_support_write, |
| 446 | .llseek = default_llseek, |
| 447 | }; |
| 448 | |
Marcel Holtmann | 134c2a8 | 2014-01-15 22:37:42 -0800 | [diff] [blame] | 449 | static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf, |
| 450 | size_t count, loff_t *ppos) |
| 451 | { |
| 452 | struct hci_dev *hdev = file->private_data; |
| 453 | char buf[3]; |
| 454 | |
| 455 | buf[0] = test_bit(HCI_SC_ONLY, &hdev->dev_flags) ? 'Y': 'N'; |
| 456 | buf[1] = '\n'; |
| 457 | buf[2] = '\0'; |
| 458 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 459 | } |
| 460 | |
| 461 | static const struct file_operations sc_only_mode_fops = { |
| 462 | .open = simple_open, |
| 463 | .read = sc_only_mode_read, |
| 464 | .llseek = default_llseek, |
| 465 | }; |
| 466 | |
Marcel Holtmann | 2bfa353 | 2013-10-17 19:16:02 -0700 | [diff] [blame] | 467 | static int idle_timeout_set(void *data, u64 val) |
| 468 | { |
| 469 | struct hci_dev *hdev = data; |
| 470 | |
| 471 | if (val != 0 && (val < 500 || val > 3600000)) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | hci_dev_lock(hdev); |
Marcel Holtmann | 2be48b6 | 2013-10-19 10:19:15 -0700 | [diff] [blame] | 475 | hdev->idle_timeout = val; |
Marcel Holtmann | 2bfa353 | 2013-10-17 19:16:02 -0700 | [diff] [blame] | 476 | hci_dev_unlock(hdev); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int idle_timeout_get(void *data, u64 *val) |
| 482 | { |
| 483 | struct hci_dev *hdev = data; |
| 484 | |
| 485 | hci_dev_lock(hdev); |
| 486 | *val = hdev->idle_timeout; |
| 487 | hci_dev_unlock(hdev); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get, |
| 493 | idle_timeout_set, "%llu\n"); |
| 494 | |
| 495 | static int sniff_min_interval_set(void *data, u64 val) |
| 496 | { |
| 497 | struct hci_dev *hdev = data; |
| 498 | |
| 499 | if (val == 0 || val % 2 || val > hdev->sniff_max_interval) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | hci_dev_lock(hdev); |
Marcel Holtmann | 2be48b6 | 2013-10-19 10:19:15 -0700 | [diff] [blame] | 503 | hdev->sniff_min_interval = val; |
Marcel Holtmann | 2bfa353 | 2013-10-17 19:16:02 -0700 | [diff] [blame] | 504 | hci_dev_unlock(hdev); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int sniff_min_interval_get(void *data, u64 *val) |
| 510 | { |
| 511 | struct hci_dev *hdev = data; |
| 512 | |
| 513 | hci_dev_lock(hdev); |
| 514 | *val = hdev->sniff_min_interval; |
| 515 | hci_dev_unlock(hdev); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | DEFINE_SIMPLE_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get, |
| 521 | sniff_min_interval_set, "%llu\n"); |
| 522 | |
| 523 | static int sniff_max_interval_set(void *data, u64 val) |
| 524 | { |
| 525 | struct hci_dev *hdev = data; |
| 526 | |
| 527 | if (val == 0 || val % 2 || val < hdev->sniff_min_interval) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | hci_dev_lock(hdev); |
Marcel Holtmann | 2be48b6 | 2013-10-19 10:19:15 -0700 | [diff] [blame] | 531 | hdev->sniff_max_interval = val; |
Marcel Holtmann | 2bfa353 | 2013-10-17 19:16:02 -0700 | [diff] [blame] | 532 | hci_dev_unlock(hdev); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int sniff_max_interval_get(void *data, u64 *val) |
| 538 | { |
| 539 | struct hci_dev *hdev = data; |
| 540 | |
| 541 | hci_dev_lock(hdev); |
| 542 | *val = hdev->sniff_max_interval; |
| 543 | hci_dev_unlock(hdev); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | DEFINE_SIMPLE_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get, |
| 549 | sniff_max_interval_set, "%llu\n"); |
| 550 | |
Marcel Holtmann | 7a4cd51 | 2014-02-19 19:52:13 -0800 | [diff] [blame] | 551 | static int random_address_show(struct seq_file *f, void *p) |
| 552 | { |
| 553 | struct hci_dev *hdev = f->private; |
| 554 | |
| 555 | hci_dev_lock(hdev); |
| 556 | seq_printf(f, "%pMR\n", &hdev->random_addr); |
| 557 | hci_dev_unlock(hdev); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static int random_address_open(struct inode *inode, struct file *file) |
| 563 | { |
| 564 | return single_open(file, random_address_show, inode->i_private); |
| 565 | } |
| 566 | |
| 567 | static const struct file_operations random_address_fops = { |
| 568 | .open = random_address_open, |
| 569 | .read = seq_read, |
| 570 | .llseek = seq_lseek, |
| 571 | .release = single_release, |
| 572 | }; |
| 573 | |
Marcel Holtmann | e7b8fc9 | 2013-10-17 11:45:09 -0700 | [diff] [blame] | 574 | static int static_address_show(struct seq_file *f, void *p) |
| 575 | { |
| 576 | struct hci_dev *hdev = f->private; |
| 577 | |
| 578 | hci_dev_lock(hdev); |
| 579 | seq_printf(f, "%pMR\n", &hdev->static_addr); |
| 580 | hci_dev_unlock(hdev); |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int static_address_open(struct inode *inode, struct file *file) |
| 586 | { |
| 587 | return single_open(file, static_address_show, inode->i_private); |
| 588 | } |
| 589 | |
| 590 | static const struct file_operations static_address_fops = { |
| 591 | .open = static_address_open, |
| 592 | .read = seq_read, |
| 593 | .llseek = seq_lseek, |
| 594 | .release = single_release, |
| 595 | }; |
| 596 | |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 597 | static ssize_t force_static_address_read(struct file *file, |
| 598 | char __user *user_buf, |
| 599 | size_t count, loff_t *ppos) |
Marcel Holtmann | 9220218 | 2013-10-18 16:38:10 -0700 | [diff] [blame] | 600 | { |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 601 | struct hci_dev *hdev = file->private_data; |
| 602 | char buf[3]; |
Marcel Holtmann | 9220218 | 2013-10-18 16:38:10 -0700 | [diff] [blame] | 603 | |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 604 | buf[0] = test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags) ? 'Y': 'N'; |
| 605 | buf[1] = '\n'; |
| 606 | buf[2] = '\0'; |
| 607 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 608 | } |
| 609 | |
| 610 | static ssize_t force_static_address_write(struct file *file, |
| 611 | const char __user *user_buf, |
| 612 | size_t count, loff_t *ppos) |
| 613 | { |
| 614 | struct hci_dev *hdev = file->private_data; |
| 615 | char buf[32]; |
| 616 | size_t buf_size = min(count, (sizeof(buf)-1)); |
| 617 | bool enable; |
| 618 | |
| 619 | if (test_bit(HCI_UP, &hdev->flags)) |
| 620 | return -EBUSY; |
| 621 | |
| 622 | if (copy_from_user(buf, user_buf, buf_size)) |
| 623 | return -EFAULT; |
| 624 | |
| 625 | buf[buf_size] = '\0'; |
| 626 | if (strtobool(buf, &enable)) |
Marcel Holtmann | 9220218 | 2013-10-18 16:38:10 -0700 | [diff] [blame] | 627 | return -EINVAL; |
| 628 | |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 629 | if (enable == test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags)) |
| 630 | return -EALREADY; |
Marcel Holtmann | 9220218 | 2013-10-18 16:38:10 -0700 | [diff] [blame] | 631 | |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 632 | change_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags); |
| 633 | |
| 634 | return count; |
Marcel Holtmann | 9220218 | 2013-10-18 16:38:10 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 637 | static const struct file_operations force_static_address_fops = { |
| 638 | .open = simple_open, |
| 639 | .read = force_static_address_read, |
| 640 | .write = force_static_address_write, |
| 641 | .llseek = default_llseek, |
| 642 | }; |
Marcel Holtmann | 9220218 | 2013-10-18 16:38:10 -0700 | [diff] [blame] | 643 | |
Marcel Holtmann | 3698d70 | 2014-02-18 21:54:49 -0800 | [diff] [blame] | 644 | static int identity_resolving_keys_show(struct seq_file *f, void *ptr) |
| 645 | { |
| 646 | struct hci_dev *hdev = f->private; |
| 647 | struct list_head *p, *n; |
| 648 | |
| 649 | hci_dev_lock(hdev); |
| 650 | list_for_each_safe(p, n, &hdev->identity_resolving_keys) { |
| 651 | struct smp_irk *irk = list_entry(p, struct smp_irk, list); |
| 652 | seq_printf(f, "%pMR (type %u) %*phN %pMR\n", |
| 653 | &irk->bdaddr, irk->addr_type, |
| 654 | 16, irk->val, &irk->rpa); |
| 655 | } |
| 656 | hci_dev_unlock(hdev); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int identity_resolving_keys_open(struct inode *inode, struct file *file) |
| 662 | { |
| 663 | return single_open(file, identity_resolving_keys_show, |
| 664 | inode->i_private); |
| 665 | } |
| 666 | |
| 667 | static const struct file_operations identity_resolving_keys_fops = { |
| 668 | .open = identity_resolving_keys_open, |
| 669 | .read = seq_read, |
| 670 | .llseek = seq_lseek, |
| 671 | .release = single_release, |
| 672 | }; |
| 673 | |
Marcel Holtmann | 8f8625c | 2013-10-18 15:56:57 -0700 | [diff] [blame] | 674 | static int long_term_keys_show(struct seq_file *f, void *ptr) |
| 675 | { |
| 676 | struct hci_dev *hdev = f->private; |
| 677 | struct list_head *p, *n; |
| 678 | |
| 679 | hci_dev_lock(hdev); |
Johan Hedberg | f813f1b | 2014-01-30 19:39:57 -0800 | [diff] [blame] | 680 | list_for_each_safe(p, n, &hdev->long_term_keys) { |
Marcel Holtmann | 8f8625c | 2013-10-18 15:56:57 -0700 | [diff] [blame] | 681 | struct smp_ltk *ltk = list_entry(p, struct smp_ltk, list); |
Johan Hedberg | f813f1b | 2014-01-30 19:39:57 -0800 | [diff] [blame] | 682 | seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %*phN %*phN\n", |
Marcel Holtmann | 8f8625c | 2013-10-18 15:56:57 -0700 | [diff] [blame] | 683 | <k->bdaddr, ltk->bdaddr_type, ltk->authenticated, |
| 684 | ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv), |
| 685 | 8, ltk->rand, 16, ltk->val); |
| 686 | } |
| 687 | hci_dev_unlock(hdev); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int long_term_keys_open(struct inode *inode, struct file *file) |
| 693 | { |
| 694 | return single_open(file, long_term_keys_show, inode->i_private); |
| 695 | } |
| 696 | |
| 697 | static const struct file_operations long_term_keys_fops = { |
| 698 | .open = long_term_keys_open, |
| 699 | .read = seq_read, |
| 700 | .llseek = seq_lseek, |
| 701 | .release = single_release, |
| 702 | }; |
| 703 | |
Marcel Holtmann | 4e70c7e | 2013-10-19 07:09:13 -0700 | [diff] [blame] | 704 | static int conn_min_interval_set(void *data, u64 val) |
| 705 | { |
| 706 | struct hci_dev *hdev = data; |
| 707 | |
| 708 | if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | hci_dev_lock(hdev); |
Marcel Holtmann | 2be48b6 | 2013-10-19 10:19:15 -0700 | [diff] [blame] | 712 | hdev->le_conn_min_interval = val; |
Marcel Holtmann | 4e70c7e | 2013-10-19 07:09:13 -0700 | [diff] [blame] | 713 | hci_dev_unlock(hdev); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static int conn_min_interval_get(void *data, u64 *val) |
| 719 | { |
| 720 | struct hci_dev *hdev = data; |
| 721 | |
| 722 | hci_dev_lock(hdev); |
| 723 | *val = hdev->le_conn_min_interval; |
| 724 | hci_dev_unlock(hdev); |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | DEFINE_SIMPLE_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get, |
| 730 | conn_min_interval_set, "%llu\n"); |
| 731 | |
| 732 | static int conn_max_interval_set(void *data, u64 val) |
| 733 | { |
| 734 | struct hci_dev *hdev = data; |
| 735 | |
| 736 | if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval) |
| 737 | return -EINVAL; |
| 738 | |
| 739 | hci_dev_lock(hdev); |
Marcel Holtmann | 2be48b6 | 2013-10-19 10:19:15 -0700 | [diff] [blame] | 740 | hdev->le_conn_max_interval = val; |
Marcel Holtmann | 4e70c7e | 2013-10-19 07:09:13 -0700 | [diff] [blame] | 741 | hci_dev_unlock(hdev); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static int conn_max_interval_get(void *data, u64 *val) |
| 747 | { |
| 748 | struct hci_dev *hdev = data; |
| 749 | |
| 750 | hci_dev_lock(hdev); |
| 751 | *val = hdev->le_conn_max_interval; |
| 752 | hci_dev_unlock(hdev); |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | DEFINE_SIMPLE_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get, |
| 758 | conn_max_interval_set, "%llu\n"); |
| 759 | |
Marcel Holtmann | 3f959d4 | 2014-02-20 11:55:56 -0800 | [diff] [blame] | 760 | static int adv_channel_map_set(void *data, u64 val) |
| 761 | { |
| 762 | struct hci_dev *hdev = data; |
| 763 | |
| 764 | if (val < 0x01 || val > 0x07) |
| 765 | return -EINVAL; |
| 766 | |
| 767 | hci_dev_lock(hdev); |
| 768 | hdev->le_adv_channel_map = val; |
| 769 | hci_dev_unlock(hdev); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int adv_channel_map_get(void *data, u64 *val) |
| 775 | { |
| 776 | struct hci_dev *hdev = data; |
| 777 | |
| 778 | hci_dev_lock(hdev); |
| 779 | *val = hdev->le_adv_channel_map; |
| 780 | hci_dev_unlock(hdev); |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | DEFINE_SIMPLE_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get, |
| 786 | adv_channel_map_set, "%llu\n"); |
| 787 | |
Jukka Rissanen | 8986310 | 2013-12-11 17:05:38 +0200 | [diff] [blame] | 788 | static ssize_t lowpan_read(struct file *file, char __user *user_buf, |
| 789 | size_t count, loff_t *ppos) |
| 790 | { |
| 791 | struct hci_dev *hdev = file->private_data; |
| 792 | char buf[3]; |
| 793 | |
| 794 | buf[0] = test_bit(HCI_6LOWPAN_ENABLED, &hdev->dev_flags) ? 'Y' : 'N'; |
| 795 | buf[1] = '\n'; |
| 796 | buf[2] = '\0'; |
| 797 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 798 | } |
| 799 | |
| 800 | static ssize_t lowpan_write(struct file *fp, const char __user *user_buffer, |
| 801 | size_t count, loff_t *position) |
| 802 | { |
| 803 | struct hci_dev *hdev = fp->private_data; |
| 804 | bool enable; |
| 805 | char buf[32]; |
| 806 | size_t buf_size = min(count, (sizeof(buf)-1)); |
| 807 | |
| 808 | if (copy_from_user(buf, user_buffer, buf_size)) |
| 809 | return -EFAULT; |
| 810 | |
| 811 | buf[buf_size] = '\0'; |
| 812 | |
| 813 | if (strtobool(buf, &enable) < 0) |
| 814 | return -EINVAL; |
| 815 | |
| 816 | if (enable == test_bit(HCI_6LOWPAN_ENABLED, &hdev->dev_flags)) |
| 817 | return -EALREADY; |
| 818 | |
| 819 | change_bit(HCI_6LOWPAN_ENABLED, &hdev->dev_flags); |
| 820 | |
| 821 | return count; |
| 822 | } |
| 823 | |
| 824 | static const struct file_operations lowpan_debugfs_fops = { |
| 825 | .open = simple_open, |
| 826 | .read = lowpan_read, |
| 827 | .write = lowpan_write, |
| 828 | .llseek = default_llseek, |
| 829 | }; |
| 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | /* ---- HCI requests ---- */ |
| 832 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 833 | static void hci_req_sync_complete(struct hci_dev *hdev, u8 result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 835 | BT_DBG("%s result 0x%2.2x", hdev->name, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
| 837 | if (hdev->req_status == HCI_REQ_PEND) { |
| 838 | hdev->req_result = result; |
| 839 | hdev->req_status = HCI_REQ_DONE; |
| 840 | wake_up_interruptible(&hdev->req_wait_q); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | static void hci_req_cancel(struct hci_dev *hdev, int err) |
| 845 | { |
| 846 | BT_DBG("%s err 0x%2.2x", hdev->name, err); |
| 847 | |
| 848 | if (hdev->req_status == HCI_REQ_PEND) { |
| 849 | hdev->req_result = err; |
| 850 | hdev->req_status = HCI_REQ_CANCELED; |
| 851 | wake_up_interruptible(&hdev->req_wait_q); |
| 852 | } |
| 853 | } |
| 854 | |
Fengguang Wu | 77a63e0 | 2013-04-20 16:24:31 +0300 | [diff] [blame] | 855 | static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode, |
| 856 | u8 event) |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 857 | { |
| 858 | struct hci_ev_cmd_complete *ev; |
| 859 | struct hci_event_hdr *hdr; |
| 860 | struct sk_buff *skb; |
| 861 | |
| 862 | hci_dev_lock(hdev); |
| 863 | |
| 864 | skb = hdev->recv_evt; |
| 865 | hdev->recv_evt = NULL; |
| 866 | |
| 867 | hci_dev_unlock(hdev); |
| 868 | |
| 869 | if (!skb) |
| 870 | return ERR_PTR(-ENODATA); |
| 871 | |
| 872 | if (skb->len < sizeof(*hdr)) { |
| 873 | BT_ERR("Too short HCI event"); |
| 874 | goto failed; |
| 875 | } |
| 876 | |
| 877 | hdr = (void *) skb->data; |
| 878 | skb_pull(skb, HCI_EVENT_HDR_SIZE); |
| 879 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 880 | if (event) { |
| 881 | if (hdr->evt != event) |
| 882 | goto failed; |
| 883 | return skb; |
| 884 | } |
| 885 | |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 886 | if (hdr->evt != HCI_EV_CMD_COMPLETE) { |
| 887 | BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt); |
| 888 | goto failed; |
| 889 | } |
| 890 | |
| 891 | if (skb->len < sizeof(*ev)) { |
| 892 | BT_ERR("Too short cmd_complete event"); |
| 893 | goto failed; |
| 894 | } |
| 895 | |
| 896 | ev = (void *) skb->data; |
| 897 | skb_pull(skb, sizeof(*ev)); |
| 898 | |
| 899 | if (opcode == __le16_to_cpu(ev->opcode)) |
| 900 | return skb; |
| 901 | |
| 902 | BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode, |
| 903 | __le16_to_cpu(ev->opcode)); |
| 904 | |
| 905 | failed: |
| 906 | kfree_skb(skb); |
| 907 | return ERR_PTR(-ENODATA); |
| 908 | } |
| 909 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 910 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 911 | const void *param, u8 event, u32 timeout) |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 912 | { |
| 913 | DECLARE_WAITQUEUE(wait, current); |
| 914 | struct hci_request req; |
| 915 | int err = 0; |
| 916 | |
| 917 | BT_DBG("%s", hdev->name); |
| 918 | |
| 919 | hci_req_init(&req, hdev); |
| 920 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 921 | hci_req_add_ev(&req, opcode, plen, param, event); |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 922 | |
| 923 | hdev->req_status = HCI_REQ_PEND; |
| 924 | |
| 925 | err = hci_req_run(&req, hci_req_sync_complete); |
| 926 | if (err < 0) |
| 927 | return ERR_PTR(err); |
| 928 | |
| 929 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 930 | set_current_state(TASK_INTERRUPTIBLE); |
| 931 | |
| 932 | schedule_timeout(timeout); |
| 933 | |
| 934 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 935 | |
| 936 | if (signal_pending(current)) |
| 937 | return ERR_PTR(-EINTR); |
| 938 | |
| 939 | switch (hdev->req_status) { |
| 940 | case HCI_REQ_DONE: |
| 941 | err = -bt_to_errno(hdev->req_result); |
| 942 | break; |
| 943 | |
| 944 | case HCI_REQ_CANCELED: |
| 945 | err = -hdev->req_result; |
| 946 | break; |
| 947 | |
| 948 | default: |
| 949 | err = -ETIMEDOUT; |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | hdev->req_status = hdev->req_result = 0; |
| 954 | |
| 955 | BT_DBG("%s end: err %d", hdev->name, err); |
| 956 | |
| 957 | if (err < 0) |
| 958 | return ERR_PTR(err); |
| 959 | |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 960 | return hci_get_cmd_complete(hdev, opcode, event); |
| 961 | } |
| 962 | EXPORT_SYMBOL(__hci_cmd_sync_ev); |
| 963 | |
| 964 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 965 | const void *param, u32 timeout) |
Johan Hedberg | 7b1abbb | 2013-04-03 21:54:47 +0300 | [diff] [blame] | 966 | { |
| 967 | return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); |
Johan Hedberg | 75e84b7 | 2013-04-02 13:35:04 +0300 | [diff] [blame] | 968 | } |
| 969 | EXPORT_SYMBOL(__hci_cmd_sync); |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | /* Execute request and wait for completion. */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 972 | static int __hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 973 | void (*func)(struct hci_request *req, |
| 974 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 975 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 977 | struct hci_request req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | DECLARE_WAITQUEUE(wait, current); |
| 979 | int err = 0; |
| 980 | |
| 981 | BT_DBG("%s start", hdev->name); |
| 982 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 983 | hci_req_init(&req, hdev); |
| 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | hdev->req_status = HCI_REQ_PEND; |
| 986 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 987 | func(&req, opt); |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 988 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 989 | err = hci_req_run(&req, hci_req_sync_complete); |
| 990 | if (err < 0) { |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 991 | hdev->req_status = 0; |
Andre Guedes | 920c830 | 2013-03-08 11:20:15 -0300 | [diff] [blame] | 992 | |
| 993 | /* ENODATA means the HCI request command queue is empty. |
| 994 | * This can happen when a request with conditionals doesn't |
| 995 | * trigger any commands to be sent. This is normal behavior |
| 996 | * and should not trigger an error return. |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 997 | */ |
Andre Guedes | 920c830 | 2013-03-08 11:20:15 -0300 | [diff] [blame] | 998 | if (err == -ENODATA) |
| 999 | return 0; |
| 1000 | |
| 1001 | return err; |
Johan Hedberg | 53cce22 | 2013-03-05 20:37:42 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
Andre Guedes | bc4445c | 2013-03-08 11:20:13 -0300 | [diff] [blame] | 1004 | add_wait_queue(&hdev->req_wait_q, &wait); |
| 1005 | set_current_state(TASK_INTERRUPTIBLE); |
| 1006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | schedule_timeout(timeout); |
| 1008 | |
| 1009 | remove_wait_queue(&hdev->req_wait_q, &wait); |
| 1010 | |
| 1011 | if (signal_pending(current)) |
| 1012 | return -EINTR; |
| 1013 | |
| 1014 | switch (hdev->req_status) { |
| 1015 | case HCI_REQ_DONE: |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1016 | err = -bt_to_errno(hdev->req_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | break; |
| 1018 | |
| 1019 | case HCI_REQ_CANCELED: |
| 1020 | err = -hdev->req_result; |
| 1021 | break; |
| 1022 | |
| 1023 | default: |
| 1024 | err = -ETIMEDOUT; |
| 1025 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Johan Hedberg | a5040ef | 2011-01-10 13:28:59 +0200 | [diff] [blame] | 1028 | hdev->req_status = hdev->req_result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | BT_DBG("%s end: err %d", hdev->name, err); |
| 1031 | |
| 1032 | return err; |
| 1033 | } |
| 1034 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1035 | static int hci_req_sync(struct hci_dev *hdev, |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1036 | void (*req)(struct hci_request *req, |
| 1037 | unsigned long opt), |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1038 | unsigned long opt, __u32 timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
| 1040 | int ret; |
| 1041 | |
Marcel Holtmann | 7c6a329 | 2008-09-12 03:11:54 +0200 | [diff] [blame] | 1042 | if (!test_bit(HCI_UP, &hdev->flags)) |
| 1043 | return -ENETDOWN; |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | /* Serialize all requests */ |
| 1046 | hci_req_lock(hdev); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1047 | ret = __hci_req_sync(hdev, req, opt, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | hci_req_unlock(hdev); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1053 | static void hci_reset_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1055 | BT_DBG("%s %ld", req->hdev->name, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* Reset device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1058 | set_bit(HCI_RESET, &req->hdev->flags); |
| 1059 | hci_req_add(req, HCI_OP_RESET, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1062 | static void bredr_init(struct hci_request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1064 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 1065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | /* Read Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1067 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Marcel Holtmann | 1143e5a | 2006-09-23 09:57:20 +0200 | [diff] [blame] | 1069 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1070 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1071 | |
| 1072 | /* Read BD Address */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1073 | hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1076 | static void amp_init(struct hci_request *req) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1077 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1078 | req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; |
Andrei Emeltchenko | 2455a3e | 2011-12-19 16:31:28 +0200 | [diff] [blame] | 1079 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1080 | /* Read Local Version */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1081 | hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 1082 | |
Marcel Holtmann | f6996cf | 2013-10-07 02:31:39 -0700 | [diff] [blame] | 1083 | /* Read Local Supported Commands */ |
| 1084 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
| 1085 | |
| 1086 | /* Read Local Supported Features */ |
| 1087 | hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); |
| 1088 | |
Andrei Emeltchenko | 6bcbc48 | 2012-03-28 16:31:24 +0300 | [diff] [blame] | 1089 | /* Read Local AMP Info */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1090 | hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
Andrei Emeltchenko | e71dfab | 2012-09-06 15:05:46 +0300 | [diff] [blame] | 1091 | |
| 1092 | /* Read Data Blk size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1093 | hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 1094 | |
Marcel Holtmann | f38ba94 | 2013-10-07 03:55:53 -0700 | [diff] [blame] | 1095 | /* Read Flow Control Mode */ |
| 1096 | hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); |
| 1097 | |
Marcel Holtmann | 7528ca1 | 2013-10-07 03:55:52 -0700 | [diff] [blame] | 1098 | /* Read Location Data */ |
| 1099 | hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1102 | static void hci_init1_req(struct hci_request *req, unsigned long opt) |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1103 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1104 | struct hci_dev *hdev = req->hdev; |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1105 | |
| 1106 | BT_DBG("%s %ld", hdev->name, opt); |
| 1107 | |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 1108 | /* Reset */ |
| 1109 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1110 | hci_reset_req(req, 0); |
Andrei Emeltchenko | 1177871 | 2012-06-11 11:13:10 +0300 | [diff] [blame] | 1111 | |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1112 | switch (hdev->dev_type) { |
| 1113 | case HCI_BREDR: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1114 | bredr_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1115 | break; |
| 1116 | |
| 1117 | case HCI_AMP: |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1118 | amp_init(req); |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1119 | break; |
| 1120 | |
| 1121 | default: |
| 1122 | BT_ERR("Unknown device type %d", hdev->dev_type); |
| 1123 | break; |
| 1124 | } |
Andrei Emeltchenko | e61ef499 | 2011-12-19 16:31:27 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1127 | static void bredr_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1128 | { |
Marcel Holtmann | 4ca048e | 2013-10-11 16:42:07 -0700 | [diff] [blame] | 1129 | struct hci_dev *hdev = req->hdev; |
| 1130 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1131 | __le16 param; |
| 1132 | __u8 flt_type; |
| 1133 | |
| 1134 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1135 | hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1136 | |
| 1137 | /* Read Class of Device */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1138 | hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1139 | |
| 1140 | /* Read Local Name */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1141 | hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1142 | |
| 1143 | /* Read Voice Setting */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1144 | hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1145 | |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 1146 | /* Read Number of Supported IAC */ |
| 1147 | hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); |
| 1148 | |
Marcel Holtmann | 4b836f3 | 2013-10-14 14:06:36 -0700 | [diff] [blame] | 1149 | /* Read Current IAC LAP */ |
| 1150 | hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); |
| 1151 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1152 | /* Clear Event Filters */ |
| 1153 | flt_type = HCI_FLT_CLEAR_ALL; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1154 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1155 | |
| 1156 | /* Connection accept timeout ~20 secs */ |
| 1157 | param = __constant_cpu_to_le16(0x7d00); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1158 | hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1159 | |
Marcel Holtmann | 4ca048e | 2013-10-11 16:42:07 -0700 | [diff] [blame] | 1160 | /* AVM Berlin (31), aka "BlueFRITZ!", reports version 1.2, |
| 1161 | * but it does not support page scan related HCI commands. |
| 1162 | */ |
| 1163 | if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) { |
Johan Hedberg | f332ec6 | 2013-03-15 17:07:11 -0500 | [diff] [blame] | 1164 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); |
| 1165 | hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); |
| 1166 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1169 | static void le_setup(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1170 | { |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1171 | struct hci_dev *hdev = req->hdev; |
| 1172 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1173 | /* Read LE Buffer Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1174 | hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1175 | |
| 1176 | /* Read LE Local Supported Features */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1177 | hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1178 | |
| 1179 | /* Read LE Advertising Channel TX Power */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1180 | hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1181 | |
| 1182 | /* Read LE White List Size */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1183 | hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1184 | |
| 1185 | /* Read LE Supported States */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1186 | hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1187 | |
| 1188 | /* LE-only controllers have LE implicitly enabled */ |
| 1189 | if (!lmp_bredr_capable(hdev)) |
| 1190 | set_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static u8 hci_get_inquiry_mode(struct hci_dev *hdev) |
| 1194 | { |
| 1195 | if (lmp_ext_inq_capable(hdev)) |
| 1196 | return 0x02; |
| 1197 | |
| 1198 | if (lmp_inq_rssi_capable(hdev)) |
| 1199 | return 0x01; |
| 1200 | |
| 1201 | if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 && |
| 1202 | hdev->lmp_subver == 0x0757) |
| 1203 | return 0x01; |
| 1204 | |
| 1205 | if (hdev->manufacturer == 15) { |
| 1206 | if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963) |
| 1207 | return 0x01; |
| 1208 | if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963) |
| 1209 | return 0x01; |
| 1210 | if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965) |
| 1211 | return 0x01; |
| 1212 | } |
| 1213 | |
| 1214 | if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 && |
| 1215 | hdev->lmp_subver == 0x1805) |
| 1216 | return 0x01; |
| 1217 | |
| 1218 | return 0x00; |
| 1219 | } |
| 1220 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1221 | static void hci_setup_inquiry_mode(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1222 | { |
| 1223 | u8 mode; |
| 1224 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1225 | mode = hci_get_inquiry_mode(req->hdev); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1226 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1227 | hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1228 | } |
| 1229 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1230 | static void hci_setup_event_mask(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1231 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1232 | struct hci_dev *hdev = req->hdev; |
| 1233 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1234 | /* The second byte is 0xff instead of 0x9f (two reserved bits |
| 1235 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the |
| 1236 | * command otherwise. |
| 1237 | */ |
| 1238 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 1239 | |
| 1240 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set |
| 1241 | * any event mask for pre 1.2 devices. |
| 1242 | */ |
| 1243 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1244 | return; |
| 1245 | |
| 1246 | if (lmp_bredr_capable(hdev)) { |
| 1247 | events[4] |= 0x01; /* Flow Specification Complete */ |
| 1248 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 1249 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ |
| 1250 | events[5] |= 0x08; /* Synchronous Connection Complete */ |
| 1251 | events[5] |= 0x10; /* Synchronous Connection Changed */ |
Marcel Holtmann | c7882cb | 2013-08-13 10:00:54 -0700 | [diff] [blame] | 1252 | } else { |
| 1253 | /* Use a different default for LE-only devices */ |
| 1254 | memset(events, 0, sizeof(events)); |
| 1255 | events[0] |= 0x10; /* Disconnection Complete */ |
| 1256 | events[0] |= 0x80; /* Encryption Change */ |
| 1257 | events[1] |= 0x08; /* Read Remote Version Information Complete */ |
| 1258 | events[1] |= 0x20; /* Command Complete */ |
| 1259 | events[1] |= 0x40; /* Command Status */ |
| 1260 | events[1] |= 0x80; /* Hardware Error */ |
| 1261 | events[2] |= 0x04; /* Number of Completed Packets */ |
| 1262 | events[3] |= 0x02; /* Data Buffer Overflow */ |
| 1263 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | if (lmp_inq_rssi_capable(hdev)) |
| 1267 | events[4] |= 0x02; /* Inquiry Result with RSSI */ |
| 1268 | |
| 1269 | if (lmp_sniffsubr_capable(hdev)) |
| 1270 | events[5] |= 0x20; /* Sniff Subrating */ |
| 1271 | |
| 1272 | if (lmp_pause_enc_capable(hdev)) |
| 1273 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ |
| 1274 | |
| 1275 | if (lmp_ext_inq_capable(hdev)) |
| 1276 | events[5] |= 0x40; /* Extended Inquiry Result */ |
| 1277 | |
| 1278 | if (lmp_no_flush_capable(hdev)) |
| 1279 | events[7] |= 0x01; /* Enhanced Flush Complete */ |
| 1280 | |
| 1281 | if (lmp_lsto_capable(hdev)) |
| 1282 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ |
| 1283 | |
| 1284 | if (lmp_ssp_capable(hdev)) { |
| 1285 | events[6] |= 0x01; /* IO Capability Request */ |
| 1286 | events[6] |= 0x02; /* IO Capability Response */ |
| 1287 | events[6] |= 0x04; /* User Confirmation Request */ |
| 1288 | events[6] |= 0x08; /* User Passkey Request */ |
| 1289 | events[6] |= 0x10; /* Remote OOB Data Request */ |
| 1290 | events[6] |= 0x20; /* Simple Pairing Complete */ |
| 1291 | events[7] |= 0x04; /* User Passkey Notification */ |
| 1292 | events[7] |= 0x08; /* Keypress Notification */ |
| 1293 | events[7] |= 0x10; /* Remote Host Supported |
| 1294 | * Features Notification |
| 1295 | */ |
| 1296 | } |
| 1297 | |
| 1298 | if (lmp_le_capable(hdev)) |
| 1299 | events[7] |= 0x20; /* LE Meta-Event */ |
| 1300 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1301 | hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1302 | |
| 1303 | if (lmp_le_capable(hdev)) { |
| 1304 | memset(events, 0, sizeof(events)); |
| 1305 | events[0] = 0x1f; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1306 | hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, |
| 1307 | sizeof(events), events); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1311 | static void hci_init2_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1312 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1313 | struct hci_dev *hdev = req->hdev; |
| 1314 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1315 | if (lmp_bredr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1316 | bredr_setup(req); |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1317 | else |
| 1318 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1319 | |
| 1320 | if (lmp_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1321 | le_setup(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1322 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1323 | hci_setup_event_mask(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1324 | |
Johan Hedberg | 3f8e2d7 | 2013-07-24 02:32:46 +0300 | [diff] [blame] | 1325 | /* AVM Berlin (31), aka "BlueFRITZ!", doesn't support the read |
| 1326 | * local supported commands HCI command. |
| 1327 | */ |
| 1328 | if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1329 | hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1330 | |
| 1331 | if (lmp_ssp_capable(hdev)) { |
Marcel Holtmann | 57af75a | 2013-10-18 12:04:47 -0700 | [diff] [blame] | 1332 | /* When SSP is available, then the host features page |
| 1333 | * should also be available as well. However some |
| 1334 | * controllers list the max_page as 0 as long as SSP |
| 1335 | * has not been enabled. To achieve proper debugging |
| 1336 | * output, force the minimum max_page to 1 at least. |
| 1337 | */ |
| 1338 | hdev->max_page = 0x01; |
| 1339 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1340 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 1341 | u8 mode = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1342 | hci_req_add(req, HCI_OP_WRITE_SSP_MODE, |
| 1343 | sizeof(mode), &mode); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1344 | } else { |
| 1345 | struct hci_cp_write_eir cp; |
| 1346 | |
| 1347 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 1348 | memset(&cp, 0, sizeof(cp)); |
| 1349 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1350 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | if (lmp_inq_rssi_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1355 | hci_setup_inquiry_mode(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1356 | |
| 1357 | if (lmp_inq_tx_pwr_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1358 | 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] | 1359 | |
| 1360 | if (lmp_ext_feat_capable(hdev)) { |
| 1361 | struct hci_cp_read_local_ext_features cp; |
| 1362 | |
| 1363 | cp.page = 0x01; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1364 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 1365 | sizeof(cp), &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) { |
| 1369 | u8 enable = 1; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1370 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), |
| 1371 | &enable); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1375 | static void hci_setup_link_policy(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1376 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1377 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1378 | struct hci_cp_write_def_link_policy cp; |
| 1379 | u16 link_policy = 0; |
| 1380 | |
| 1381 | if (lmp_rswitch_capable(hdev)) |
| 1382 | link_policy |= HCI_LP_RSWITCH; |
| 1383 | if (lmp_hold_capable(hdev)) |
| 1384 | link_policy |= HCI_LP_HOLD; |
| 1385 | if (lmp_sniff_capable(hdev)) |
| 1386 | link_policy |= HCI_LP_SNIFF; |
| 1387 | if (lmp_park_capable(hdev)) |
| 1388 | link_policy |= HCI_LP_PARK; |
| 1389 | |
| 1390 | cp.policy = cpu_to_le16(link_policy); |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1391 | 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] | 1392 | } |
| 1393 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1394 | static void hci_set_le_support(struct hci_request *req) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1395 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1396 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1397 | struct hci_cp_write_le_host_supported cp; |
| 1398 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1399 | /* LE-only devices do not support explicit enablement */ |
| 1400 | if (!lmp_bredr_capable(hdev)) |
| 1401 | return; |
| 1402 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1403 | memset(&cp, 0, sizeof(cp)); |
| 1404 | |
| 1405 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1406 | cp.le = 0x01; |
| 1407 | cp.simul = lmp_le_br_capable(hdev); |
| 1408 | } |
| 1409 | |
| 1410 | if (cp.le != lmp_host_le_capable(hdev)) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1411 | hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), |
| 1412 | &cp); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 1415 | static void hci_set_event_mask_page_2(struct hci_request *req) |
| 1416 | { |
| 1417 | struct hci_dev *hdev = req->hdev; |
| 1418 | u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 1419 | |
| 1420 | /* If Connectionless Slave Broadcast master role is supported |
| 1421 | * enable all necessary events for it. |
| 1422 | */ |
Marcel Holtmann | 53b834d2 | 2013-12-08 11:55:33 -0800 | [diff] [blame] | 1423 | if (lmp_csb_master_capable(hdev)) { |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 1424 | events[1] |= 0x40; /* Triggered Clock Capture */ |
| 1425 | events[1] |= 0x80; /* Synchronization Train Complete */ |
| 1426 | events[2] |= 0x10; /* Slave Page Response Timeout */ |
| 1427 | events[2] |= 0x20; /* CSB Channel Map Change */ |
| 1428 | } |
| 1429 | |
| 1430 | /* If Connectionless Slave Broadcast slave role is supported |
| 1431 | * enable all necessary events for it. |
| 1432 | */ |
Marcel Holtmann | 53b834d2 | 2013-12-08 11:55:33 -0800 | [diff] [blame] | 1433 | if (lmp_csb_slave_capable(hdev)) { |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 1434 | events[2] |= 0x01; /* Synchronization Train Received */ |
| 1435 | events[2] |= 0x02; /* CSB Receive */ |
| 1436 | events[2] |= 0x04; /* CSB Timeout */ |
| 1437 | events[2] |= 0x08; /* Truncated Page Complete */ |
| 1438 | } |
| 1439 | |
Marcel Holtmann | 40c59fc | 2014-01-10 02:07:21 -0800 | [diff] [blame] | 1440 | /* Enable Authenticated Payload Timeout Expired event if supported */ |
| 1441 | if (lmp_ping_capable(hdev)) |
| 1442 | events[2] |= 0x80; |
| 1443 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 1444 | hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); |
| 1445 | } |
| 1446 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1447 | static void hci_init3_req(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1448 | { |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1449 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 1450 | u8 p; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1451 | |
Gustavo Padovan | b8f4e06 | 2013-06-13 12:34:31 +0100 | [diff] [blame] | 1452 | /* Some Broadcom based Bluetooth controllers do not support the |
| 1453 | * Delete Stored Link Key command. They are clearly indicating its |
| 1454 | * absence in the bit mask of supported commands. |
| 1455 | * |
| 1456 | * Check the supported commands and only if the the command is marked |
| 1457 | * as supported send it. If not supported assume that the controller |
| 1458 | * does not have actual support for stored link keys which makes this |
| 1459 | * command redundant anyway. |
Marcel Holtmann | f9f462f | 2014-01-03 03:02:35 -0800 | [diff] [blame] | 1460 | * |
| 1461 | * Some controllers indicate that they support handling deleting |
| 1462 | * stored link keys, but they don't. The quirk lets a driver |
| 1463 | * just disable this command. |
Marcel Holtmann | 637b4ca | 2013-07-01 14:14:46 -0700 | [diff] [blame] | 1464 | */ |
Marcel Holtmann | f9f462f | 2014-01-03 03:02:35 -0800 | [diff] [blame] | 1465 | if (hdev->commands[6] & 0x80 && |
| 1466 | !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { |
Johan Hedberg | 59f45d5 | 2013-06-13 11:01:13 +0300 | [diff] [blame] | 1467 | struct hci_cp_delete_stored_link_key cp; |
| 1468 | |
| 1469 | bacpy(&cp.bdaddr, BDADDR_ANY); |
| 1470 | cp.delete_all = 0x01; |
| 1471 | hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, |
| 1472 | sizeof(cp), &cp); |
| 1473 | } |
| 1474 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1475 | if (hdev->commands[5] & 0x10) |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1476 | hci_setup_link_policy(req); |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1477 | |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 1478 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 1479 | /* If the controller has a public BD_ADDR, then by default |
| 1480 | * use that one. If this is a LE only controller without |
| 1481 | * a public address, default to the random address. |
| 1482 | * |
| 1483 | * For debugging purposes it is possible to force |
| 1484 | * controllers with a public address to use the |
| 1485 | * random address instead. |
| 1486 | */ |
| 1487 | if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags) || |
| 1488 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) |
| 1489 | hdev->own_addr_type = ADDR_LE_DEV_RANDOM; |
| 1490 | else |
| 1491 | hdev->own_addr_type = ADDR_LE_DEV_PUBLIC; |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 1492 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1493 | hci_set_le_support(req); |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 1494 | } |
Johan Hedberg | d2c5d77 | 2013-04-17 15:00:52 +0300 | [diff] [blame] | 1495 | |
| 1496 | /* Read features beyond page 1 if available */ |
| 1497 | for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { |
| 1498 | struct hci_cp_read_local_ext_features cp; |
| 1499 | |
| 1500 | cp.page = p; |
| 1501 | hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, |
| 1502 | sizeof(cp), &cp); |
| 1503 | } |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 1506 | static void hci_init4_req(struct hci_request *req, unsigned long opt) |
| 1507 | { |
| 1508 | struct hci_dev *hdev = req->hdev; |
| 1509 | |
Johan Hedberg | d62e6d6 | 2013-09-13 11:40:02 +0300 | [diff] [blame] | 1510 | /* Set event mask page 2 if the HCI command for it is supported */ |
| 1511 | if (hdev->commands[22] & 0x04) |
| 1512 | hci_set_event_mask_page_2(req); |
| 1513 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 1514 | /* Check for Synchronization Train support */ |
Marcel Holtmann | 53b834d2 | 2013-12-08 11:55:33 -0800 | [diff] [blame] | 1515 | if (lmp_sync_train_capable(hdev)) |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 1516 | hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); |
Marcel Holtmann | a6d0d69 | 2014-01-10 02:07:24 -0800 | [diff] [blame] | 1517 | |
| 1518 | /* Enable Secure Connections if supported and configured */ |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 1519 | if ((lmp_sc_capable(hdev) || |
| 1520 | test_bit(HCI_FORCE_SC, &hdev->dev_flags)) && |
Marcel Holtmann | a6d0d69 | 2014-01-10 02:07:24 -0800 | [diff] [blame] | 1521 | test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) { |
| 1522 | u8 support = 0x01; |
| 1523 | hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT, |
| 1524 | sizeof(support), &support); |
| 1525 | } |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 1526 | } |
| 1527 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1528 | static int __hci_init(struct hci_dev *hdev) |
| 1529 | { |
| 1530 | int err; |
| 1531 | |
| 1532 | err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); |
| 1533 | if (err < 0) |
| 1534 | return err; |
| 1535 | |
Marcel Holtmann | 4b4148e | 2013-10-19 07:09:12 -0700 | [diff] [blame] | 1536 | /* The Device Under Test (DUT) mode is special and available for |
| 1537 | * all controller types. So just create it early on. |
| 1538 | */ |
| 1539 | if (test_bit(HCI_SETUP, &hdev->dev_flags)) { |
| 1540 | debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev, |
| 1541 | &dut_mode_fops); |
| 1542 | } |
| 1543 | |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1544 | /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode |
| 1545 | * BR/EDR/LE type controllers. AMP controllers only need the |
| 1546 | * first stage init. |
| 1547 | */ |
| 1548 | if (hdev->dev_type != HCI_BREDR) |
| 1549 | return 0; |
| 1550 | |
| 1551 | err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); |
| 1552 | if (err < 0) |
| 1553 | return err; |
| 1554 | |
Johan Hedberg | 5d4e7e8 | 2013-09-13 11:40:01 +0300 | [diff] [blame] | 1555 | err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); |
| 1556 | if (err < 0) |
| 1557 | return err; |
| 1558 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 1559 | err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT); |
| 1560 | if (err < 0) |
| 1561 | return err; |
| 1562 | |
| 1563 | /* Only create debugfs entries during the initial setup |
| 1564 | * phase and not every time the controller gets powered on. |
| 1565 | */ |
| 1566 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 1567 | return 0; |
| 1568 | |
Marcel Holtmann | dfb826a | 2013-10-18 12:04:46 -0700 | [diff] [blame] | 1569 | debugfs_create_file("features", 0444, hdev->debugfs, hdev, |
| 1570 | &features_fops); |
Marcel Holtmann | ceeb3bc | 2013-10-18 12:04:49 -0700 | [diff] [blame] | 1571 | debugfs_create_u16("manufacturer", 0444, hdev->debugfs, |
| 1572 | &hdev->manufacturer); |
| 1573 | debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver); |
| 1574 | debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev); |
Marcel Holtmann | 70afe0b | 2013-10-17 17:24:14 -0700 | [diff] [blame] | 1575 | debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev, |
| 1576 | &blacklist_fops); |
Marcel Holtmann | 4721983 | 2013-10-17 17:24:15 -0700 | [diff] [blame] | 1577 | debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops); |
| 1578 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 1579 | if (lmp_bredr_capable(hdev)) { |
| 1580 | debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, |
| 1581 | hdev, &inquiry_cache_fops); |
Marcel Holtmann | 02d08d1 | 2013-10-18 12:04:52 -0700 | [diff] [blame] | 1582 | debugfs_create_file("link_keys", 0400, hdev->debugfs, |
| 1583 | hdev, &link_keys_fops); |
Marcel Holtmann | babdbb3 | 2013-10-18 12:04:51 -0700 | [diff] [blame] | 1584 | debugfs_create_file("dev_class", 0444, hdev->debugfs, |
| 1585 | hdev, &dev_class_fops); |
Marcel Holtmann | 041000b | 2013-10-17 12:02:31 -0700 | [diff] [blame] | 1586 | debugfs_create_file("voice_setting", 0444, hdev->debugfs, |
| 1587 | hdev, &voice_setting_fops); |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Marcel Holtmann | 06f5b77 | 2013-10-19 07:09:11 -0700 | [diff] [blame] | 1590 | if (lmp_ssp_capable(hdev)) { |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame] | 1591 | debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs, |
| 1592 | hdev, &auto_accept_delay_fops); |
Marcel Holtmann | 06f5b77 | 2013-10-19 07:09:11 -0700 | [diff] [blame] | 1593 | debugfs_create_file("ssp_debug_mode", 0644, hdev->debugfs, |
| 1594 | hdev, &ssp_debug_mode_fops); |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 1595 | debugfs_create_file("force_sc_support", 0644, hdev->debugfs, |
| 1596 | hdev, &force_sc_support_fops); |
Marcel Holtmann | 134c2a8 | 2014-01-15 22:37:42 -0800 | [diff] [blame] | 1597 | debugfs_create_file("sc_only_mode", 0444, hdev->debugfs, |
| 1598 | hdev, &sc_only_mode_fops); |
Marcel Holtmann | 06f5b77 | 2013-10-19 07:09:11 -0700 | [diff] [blame] | 1599 | } |
Marcel Holtmann | ebd1e33 | 2013-10-17 10:54:46 -0700 | [diff] [blame] | 1600 | |
Marcel Holtmann | 2bfa353 | 2013-10-17 19:16:02 -0700 | [diff] [blame] | 1601 | if (lmp_sniff_capable(hdev)) { |
| 1602 | debugfs_create_file("idle_timeout", 0644, hdev->debugfs, |
| 1603 | hdev, &idle_timeout_fops); |
| 1604 | debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs, |
| 1605 | hdev, &sniff_min_interval_fops); |
| 1606 | debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs, |
| 1607 | hdev, &sniff_max_interval_fops); |
| 1608 | } |
| 1609 | |
Marcel Holtmann | d0f729b | 2013-10-18 15:23:46 -0700 | [diff] [blame] | 1610 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 7a4cd51 | 2014-02-19 19:52:13 -0800 | [diff] [blame] | 1611 | debugfs_create_file("random_address", 0444, hdev->debugfs, |
| 1612 | hdev, &random_address_fops); |
Marcel Holtmann | b32bba6 | 2014-02-19 19:31:26 -0800 | [diff] [blame] | 1613 | debugfs_create_file("static_address", 0444, hdev->debugfs, |
| 1614 | hdev, &static_address_fops); |
| 1615 | |
| 1616 | /* For controllers with a public address, provide a debug |
| 1617 | * option to force the usage of the configured static |
| 1618 | * address. By default the public address is used. |
| 1619 | */ |
| 1620 | if (bacmp(&hdev->bdaddr, BDADDR_ANY)) |
| 1621 | debugfs_create_file("force_static_address", 0644, |
| 1622 | hdev->debugfs, hdev, |
| 1623 | &force_static_address_fops); |
| 1624 | |
Marcel Holtmann | d0f729b | 2013-10-18 15:23:46 -0700 | [diff] [blame] | 1625 | debugfs_create_u8("white_list_size", 0444, hdev->debugfs, |
| 1626 | &hdev->le_white_list_size); |
Marcel Holtmann | 3698d70 | 2014-02-18 21:54:49 -0800 | [diff] [blame] | 1627 | debugfs_create_file("identity_resolving_keys", 0400, |
| 1628 | hdev->debugfs, hdev, |
| 1629 | &identity_resolving_keys_fops); |
Marcel Holtmann | 8f8625c | 2013-10-18 15:56:57 -0700 | [diff] [blame] | 1630 | debugfs_create_file("long_term_keys", 0400, hdev->debugfs, |
| 1631 | hdev, &long_term_keys_fops); |
Marcel Holtmann | 4e70c7e | 2013-10-19 07:09:13 -0700 | [diff] [blame] | 1632 | debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, |
| 1633 | hdev, &conn_min_interval_fops); |
| 1634 | debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, |
| 1635 | hdev, &conn_max_interval_fops); |
Marcel Holtmann | 3f959d4 | 2014-02-20 11:55:56 -0800 | [diff] [blame] | 1636 | debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, |
| 1637 | hdev, &adv_channel_map_fops); |
Jukka Rissanen | 8986310 | 2013-12-11 17:05:38 +0200 | [diff] [blame] | 1638 | debugfs_create_file("6lowpan", 0644, hdev->debugfs, hdev, |
| 1639 | &lowpan_debugfs_fops); |
Marcel Holtmann | d0f729b | 2013-10-18 15:23:46 -0700 | [diff] [blame] | 1640 | } |
Marcel Holtmann | e7b8fc9 | 2013-10-17 11:45:09 -0700 | [diff] [blame] | 1641 | |
Marcel Holtmann | baf27f6 | 2013-10-16 03:28:55 -0700 | [diff] [blame] | 1642 | return 0; |
Johan Hedberg | 2177bab | 2013-03-05 20:37:43 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1645 | static void hci_scan_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | { |
| 1647 | __u8 scan = opt; |
| 1648 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1649 | BT_DBG("%s %x", req->hdev->name, scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | |
| 1651 | /* Inquiry and Page scans */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1652 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1655 | static void hci_auth_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | { |
| 1657 | __u8 auth = opt; |
| 1658 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1659 | BT_DBG("%s %x", req->hdev->name, auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
| 1661 | /* Authentication */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1662 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1665 | static void hci_encrypt_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | { |
| 1667 | __u8 encrypt = opt; |
| 1668 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1669 | BT_DBG("%s %x", req->hdev->name, encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1671 | /* Encryption */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1672 | hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1675 | static void hci_linkpol_req(struct hci_request *req, unsigned long opt) |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1676 | { |
| 1677 | __le16 policy = cpu_to_le16(opt); |
| 1678 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1679 | BT_DBG("%s %x", req->hdev->name, policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1680 | |
| 1681 | /* Default link policy */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1682 | hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1685 | /* Get HCI device by index. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | * Device is held on return. */ |
| 1687 | struct hci_dev *hci_dev_get(int index) |
| 1688 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1689 | struct hci_dev *hdev = NULL, *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
| 1691 | BT_DBG("%d", index); |
| 1692 | |
| 1693 | if (index < 0) |
| 1694 | return NULL; |
| 1695 | |
| 1696 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1697 | list_for_each_entry(d, &hci_dev_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | if (d->id == index) { |
| 1699 | hdev = hci_dev_hold(d); |
| 1700 | break; |
| 1701 | } |
| 1702 | } |
| 1703 | read_unlock(&hci_dev_list_lock); |
| 1704 | return hdev; |
| 1705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | |
| 1707 | /* ---- Inquiry support ---- */ |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1708 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1709 | bool hci_discovery_active(struct hci_dev *hdev) |
| 1710 | { |
| 1711 | struct discovery_state *discov = &hdev->discovery; |
| 1712 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 1713 | switch (discov->state) { |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 1714 | case DISCOVERY_FINDING: |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 1715 | case DISCOVERY_RESOLVING: |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1716 | return true; |
| 1717 | |
Andre Guedes | 6fbe195 | 2012-02-03 17:47:58 -0300 | [diff] [blame] | 1718 | default: |
| 1719 | return false; |
| 1720 | } |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1723 | void hci_discovery_set_state(struct hci_dev *hdev, int state) |
| 1724 | { |
| 1725 | BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); |
| 1726 | |
| 1727 | if (hdev->discovery.state == state) |
| 1728 | return; |
| 1729 | |
| 1730 | switch (state) { |
| 1731 | case DISCOVERY_STOPPED: |
Andre Guedes | 7b99b65 | 2012-02-13 15:41:02 -0300 | [diff] [blame] | 1732 | if (hdev->discovery.state != DISCOVERY_STARTING) |
| 1733 | mgmt_discovering(hdev, 0); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1734 | break; |
| 1735 | case DISCOVERY_STARTING: |
| 1736 | break; |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 1737 | case DISCOVERY_FINDING: |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1738 | mgmt_discovering(hdev, 1); |
| 1739 | break; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1740 | case DISCOVERY_RESOLVING: |
| 1741 | break; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 1742 | case DISCOVERY_STOPPING: |
| 1743 | break; |
| 1744 | } |
| 1745 | |
| 1746 | hdev->discovery.state = state; |
| 1747 | } |
| 1748 | |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1749 | void hci_inquiry_cache_flush(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1751 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1752 | struct inquiry_entry *p, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1754 | list_for_each_entry_safe(p, n, &cache->all, all) { |
| 1755 | list_del(&p->all); |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1756 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | } |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1758 | |
| 1759 | INIT_LIST_HEAD(&cache->unknown); |
| 1760 | INIT_LIST_HEAD(&cache->resolve); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1763 | struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, |
| 1764 | bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1766 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | struct inquiry_entry *e; |
| 1768 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1769 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1771 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1773 | return e; |
| 1774 | } |
| 1775 | |
| 1776 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1779 | 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] | 1780 | bdaddr_t *bdaddr) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1781 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1782 | struct discovery_state *cache = &hdev->discovery; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1783 | struct inquiry_entry *e; |
| 1784 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1785 | BT_DBG("cache %p, %pMR", cache, bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1786 | |
| 1787 | list_for_each_entry(e, &cache->unknown, list) { |
| 1788 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 1789 | return e; |
| 1790 | } |
| 1791 | |
| 1792 | return NULL; |
| 1793 | } |
| 1794 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1795 | 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] | 1796 | bdaddr_t *bdaddr, |
| 1797 | int state) |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1798 | { |
| 1799 | struct discovery_state *cache = &hdev->discovery; |
| 1800 | struct inquiry_entry *e; |
| 1801 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1802 | BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 1803 | |
| 1804 | list_for_each_entry(e, &cache->resolve, list) { |
| 1805 | if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) |
| 1806 | return e; |
| 1807 | if (!bacmp(&e->data.bdaddr, bdaddr)) |
| 1808 | return e; |
| 1809 | } |
| 1810 | |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1814 | void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1815 | struct inquiry_entry *ie) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1816 | { |
| 1817 | struct discovery_state *cache = &hdev->discovery; |
| 1818 | struct list_head *pos = &cache->resolve; |
| 1819 | struct inquiry_entry *p; |
| 1820 | |
| 1821 | list_del(&ie->list); |
| 1822 | |
| 1823 | list_for_each_entry(p, &cache->resolve, list) { |
| 1824 | if (p->name_state != NAME_PENDING && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1825 | abs(p->data.rssi) >= abs(ie->data.rssi)) |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1826 | break; |
| 1827 | pos = &p->list; |
| 1828 | } |
| 1829 | |
| 1830 | list_add(&ie->list, pos); |
| 1831 | } |
| 1832 | |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1833 | bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1834 | bool name_known, bool *ssp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1836 | struct discovery_state *cache = &hdev->discovery; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1837 | struct inquiry_entry *ie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1839 | BT_DBG("cache %p, %pMR", cache, &data->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Szymon Janc | 2b2fec4 | 2012-11-20 11:38:54 +0100 | [diff] [blame] | 1841 | hci_remove_remote_oob_data(hdev, &data->bdaddr); |
| 1842 | |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1843 | if (ssp) |
| 1844 | *ssp = data->ssp_mode; |
| 1845 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1846 | ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1847 | if (ie) { |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 1848 | if (ie->data.ssp_mode && ssp) |
| 1849 | *ssp = true; |
| 1850 | |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1851 | if (ie->name_state == NAME_NEEDED && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1852 | data->rssi != ie->data.rssi) { |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1853 | ie->data.rssi = data->rssi; |
| 1854 | hci_inquiry_cache_update_resolve(hdev, ie); |
| 1855 | } |
| 1856 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1857 | goto update; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 1858 | } |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1859 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1860 | /* Entry not in the cache. Add new one. */ |
| 1861 | ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); |
| 1862 | if (!ie) |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1863 | return false; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1864 | |
| 1865 | list_add(&ie->all, &cache->all); |
| 1866 | |
| 1867 | if (name_known) { |
| 1868 | ie->name_state = NAME_KNOWN; |
| 1869 | } else { |
| 1870 | ie->name_state = NAME_NOT_KNOWN; |
| 1871 | list_add(&ie->list, &cache->unknown); |
| 1872 | } |
| 1873 | |
| 1874 | update: |
| 1875 | if (name_known && ie->name_state != NAME_KNOWN && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1876 | ie->name_state != NAME_PENDING) { |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1877 | ie->name_state = NAME_KNOWN; |
| 1878 | list_del(&ie->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1881 | memcpy(&ie->data, data, sizeof(*data)); |
| 1882 | ie->timestamp = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | cache->timestamp = jiffies; |
Johan Hedberg | 3175405 | 2012-01-04 13:39:52 +0200 | [diff] [blame] | 1884 | |
| 1885 | if (ie->name_state == NAME_NOT_KNOWN) |
| 1886 | return false; |
| 1887 | |
| 1888 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) |
| 1892 | { |
Johan Hedberg | 3088351 | 2012-01-04 14:16:21 +0200 | [diff] [blame] | 1893 | struct discovery_state *cache = &hdev->discovery; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | struct inquiry_info *info = (struct inquiry_info *) buf; |
| 1895 | struct inquiry_entry *e; |
| 1896 | int copied = 0; |
| 1897 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 1898 | list_for_each_entry(e, &cache->all, all) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | struct inquiry_data *data = &e->data; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1900 | |
| 1901 | if (copied >= num) |
| 1902 | break; |
| 1903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | bacpy(&info->bdaddr, &data->bdaddr); |
| 1905 | info->pscan_rep_mode = data->pscan_rep_mode; |
| 1906 | info->pscan_period_mode = data->pscan_period_mode; |
| 1907 | info->pscan_mode = data->pscan_mode; |
| 1908 | memcpy(info->dev_class, data->dev_class, 3); |
| 1909 | info->clock_offset = data->clock_offset; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | info++; |
Johan Hedberg | b57c1a5 | 2012-01-03 16:03:00 +0200 | [diff] [blame] | 1912 | copied++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | BT_DBG("cache %p, copied %d", cache, copied); |
| 1916 | return copied; |
| 1917 | } |
| 1918 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1919 | static void hci_inq_req(struct hci_request *req, unsigned long opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | { |
| 1921 | struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1922 | struct hci_dev *hdev = req->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | struct hci_cp_inquiry cp; |
| 1924 | |
| 1925 | BT_DBG("%s", hdev->name); |
| 1926 | |
| 1927 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 1928 | return; |
| 1929 | |
| 1930 | /* Start Inquiry */ |
| 1931 | memcpy(&cp.lap, &ir->lap, 3); |
| 1932 | cp.length = ir->length; |
| 1933 | cp.num_rsp = ir->num_rsp; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 1934 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1937 | static int wait_inquiry(void *word) |
| 1938 | { |
| 1939 | schedule(); |
| 1940 | return signal_pending(current); |
| 1941 | } |
| 1942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | int hci_inquiry(void __user *arg) |
| 1944 | { |
| 1945 | __u8 __user *ptr = arg; |
| 1946 | struct hci_inquiry_req ir; |
| 1947 | struct hci_dev *hdev; |
| 1948 | int err = 0, do_inquiry = 0, max_rsp; |
| 1949 | long timeo; |
| 1950 | __u8 *buf; |
| 1951 | |
| 1952 | if (copy_from_user(&ir, ptr, sizeof(ir))) |
| 1953 | return -EFAULT; |
| 1954 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 1955 | hdev = hci_dev_get(ir.dev_id); |
| 1956 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | return -ENODEV; |
| 1958 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 1959 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 1960 | err = -EBUSY; |
| 1961 | goto done; |
| 1962 | } |
| 1963 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 1964 | if (hdev->dev_type != HCI_BREDR) { |
| 1965 | err = -EOPNOTSUPP; |
| 1966 | goto done; |
| 1967 | } |
| 1968 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1969 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1970 | err = -EOPNOTSUPP; |
| 1971 | goto done; |
| 1972 | } |
| 1973 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1974 | hci_dev_lock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1975 | if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 1976 | inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 1977 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | do_inquiry = 1; |
| 1979 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1980 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1982 | timeo = ir.length * msecs_to_jiffies(2000); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1983 | |
| 1984 | if (do_inquiry) { |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 1985 | err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, |
| 1986 | timeo); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1987 | if (err < 0) |
| 1988 | goto done; |
Andre Guedes | 3e13fa1 | 2013-03-27 20:04:56 -0300 | [diff] [blame] | 1989 | |
| 1990 | /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is |
| 1991 | * cleared). If it is interrupted by a signal, return -EINTR. |
| 1992 | */ |
| 1993 | if (wait_on_bit(&hdev->flags, HCI_INQUIRY, wait_inquiry, |
| 1994 | TASK_INTERRUPTIBLE)) |
| 1995 | return -EINTR; |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1998 | /* for unlimited number of responses we will use buffer with |
| 1999 | * 255 entries |
| 2000 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; |
| 2002 | |
| 2003 | /* cache_dump can't sleep. Therefore we allocate temp buffer and then |
| 2004 | * copy it to the user space. |
| 2005 | */ |
Szymon Janc | 01df8c3 | 2011-02-17 16:46:47 +0100 | [diff] [blame] | 2006 | buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2007 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | err = -ENOMEM; |
| 2009 | goto done; |
| 2010 | } |
| 2011 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2012 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2014 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
| 2016 | BT_DBG("num_rsp %d", ir.num_rsp); |
| 2017 | |
| 2018 | if (!copy_to_user(ptr, &ir, sizeof(ir))) { |
| 2019 | ptr += sizeof(ir); |
| 2020 | if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2021 | ir.num_rsp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | err = -EFAULT; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2023 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | err = -EFAULT; |
| 2025 | |
| 2026 | kfree(buf); |
| 2027 | |
| 2028 | done: |
| 2029 | hci_dev_put(hdev); |
| 2030 | return err; |
| 2031 | } |
| 2032 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 2033 | static int hci_dev_do_open(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | int ret = 0; |
| 2036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | BT_DBG("%s %p", hdev->name, hdev); |
| 2038 | |
| 2039 | hci_req_lock(hdev); |
| 2040 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 2041 | if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { |
| 2042 | ret = -ENODEV; |
| 2043 | goto done; |
| 2044 | } |
| 2045 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2046 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) { |
| 2047 | /* Check for rfkill but allow the HCI setup stage to |
| 2048 | * proceed (which in itself doesn't cause any RF activity). |
| 2049 | */ |
| 2050 | if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) { |
| 2051 | ret = -ERFKILL; |
| 2052 | goto done; |
| 2053 | } |
| 2054 | |
| 2055 | /* Check for valid public address or a configured static |
| 2056 | * random adddress, but let the HCI setup proceed to |
| 2057 | * be able to determine if there is a public address |
| 2058 | * or not. |
| 2059 | * |
Marcel Holtmann | c6beca0 | 2014-02-17 09:21:19 -0800 | [diff] [blame] | 2060 | * In case of user channel usage, it is not important |
| 2061 | * if a public address or static random address is |
| 2062 | * available. |
| 2063 | * |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2064 | * This check is only valid for BR/EDR controllers |
| 2065 | * since AMP controllers do not have an address. |
| 2066 | */ |
Marcel Holtmann | c6beca0 | 2014-02-17 09:21:19 -0800 | [diff] [blame] | 2067 | if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && |
| 2068 | hdev->dev_type == HCI_BREDR && |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2069 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 2070 | !bacmp(&hdev->static_addr, BDADDR_ANY)) { |
| 2071 | ret = -EADDRNOTAVAIL; |
| 2072 | goto done; |
| 2073 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 2077 | ret = -EALREADY; |
| 2078 | goto done; |
| 2079 | } |
| 2080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | if (hdev->open(hdev)) { |
| 2082 | ret = -EIO; |
| 2083 | goto done; |
| 2084 | } |
| 2085 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 2086 | atomic_set(&hdev->cmd_cnt, 1); |
| 2087 | set_bit(HCI_INIT, &hdev->flags); |
| 2088 | |
| 2089 | if (hdev->setup && test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 2090 | ret = hdev->setup(hdev); |
| 2091 | |
| 2092 | if (!ret) { |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 2093 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 2094 | set_bit(HCI_RAW, &hdev->flags); |
| 2095 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2096 | if (!test_bit(HCI_RAW, &hdev->flags) && |
| 2097 | !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 2098 | ret = __hci_init(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Marcel Holtmann | f41c70c | 2012-11-12 14:02:14 +0900 | [diff] [blame] | 2101 | clear_bit(HCI_INIT, &hdev->flags); |
| 2102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | if (!ret) { |
| 2104 | hci_dev_hold(hdev); |
| 2105 | set_bit(HCI_UP, &hdev->flags); |
| 2106 | hci_notify(hdev, HCI_DEV_UP); |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 2107 | if (!test_bit(HCI_SETUP, &hdev->dev_flags) && |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2108 | !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 2109 | hdev->dev_type == HCI_BREDR) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2110 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2111 | mgmt_powered(hdev, 1); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2112 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2113 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2114 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | /* Init failed, cleanup */ |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2116 | flush_work(&hdev->tx_work); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2117 | flush_work(&hdev->cmd_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2118 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | skb_queue_purge(&hdev->cmd_q); |
| 2121 | skb_queue_purge(&hdev->rx_q); |
| 2122 | |
| 2123 | if (hdev->flush) |
| 2124 | hdev->flush(hdev); |
| 2125 | |
| 2126 | if (hdev->sent_cmd) { |
| 2127 | kfree_skb(hdev->sent_cmd); |
| 2128 | hdev->sent_cmd = NULL; |
| 2129 | } |
| 2130 | |
| 2131 | hdev->close(hdev); |
| 2132 | hdev->flags = 0; |
| 2133 | } |
| 2134 | |
| 2135 | done: |
| 2136 | hci_req_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | return ret; |
| 2138 | } |
| 2139 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 2140 | /* ---- HCI ioctl helpers ---- */ |
| 2141 | |
| 2142 | int hci_dev_open(__u16 dev) |
| 2143 | { |
| 2144 | struct hci_dev *hdev; |
| 2145 | int err; |
| 2146 | |
| 2147 | hdev = hci_dev_get(dev); |
| 2148 | if (!hdev) |
| 2149 | return -ENODEV; |
| 2150 | |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 2151 | /* We need to ensure that no other power on/off work is pending |
| 2152 | * before proceeding to call hci_dev_do_open. This is |
| 2153 | * particularly important if the setup procedure has not yet |
| 2154 | * completed. |
| 2155 | */ |
| 2156 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 2157 | cancel_delayed_work(&hdev->power_off); |
| 2158 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2159 | /* After this call it is guaranteed that the setup procedure |
| 2160 | * has finished. This means that error conditions like RFKILL |
| 2161 | * or no valid public or static random address apply. |
| 2162 | */ |
Johan Hedberg | e1d08f4 | 2013-10-01 22:44:50 +0300 | [diff] [blame] | 2163 | flush_workqueue(hdev->req_workqueue); |
| 2164 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 2165 | err = hci_dev_do_open(hdev); |
| 2166 | |
| 2167 | hci_dev_put(hdev); |
| 2168 | |
| 2169 | return err; |
| 2170 | } |
| 2171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | static int hci_dev_do_close(struct hci_dev *hdev) |
| 2173 | { |
| 2174 | BT_DBG("%s %p", hdev->name, hdev); |
| 2175 | |
Vinicius Costa Gomes | 78c04c0 | 2012-09-14 16:34:46 -0300 | [diff] [blame] | 2176 | cancel_delayed_work(&hdev->power_off); |
| 2177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | hci_req_cancel(hdev, ENODEV); |
| 2179 | hci_req_lock(hdev); |
| 2180 | |
| 2181 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 2182 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | hci_req_unlock(hdev); |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 2187 | /* Flush RX and TX works */ |
| 2188 | flush_work(&hdev->tx_work); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 2189 | flush_work(&hdev->rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 2191 | if (hdev->discov_timeout > 0) { |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 2192 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 2193 | hdev->discov_timeout = 0; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2194 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 2195 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 2196 | } |
| 2197 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2198 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2199 | cancel_delayed_work(&hdev->service_cache); |
| 2200 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 2201 | cancel_delayed_work_sync(&hdev->le_scan_disable); |
| 2202 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2203 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 2204 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2206 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | |
| 2208 | hci_notify(hdev, HCI_DEV_DOWN); |
| 2209 | |
| 2210 | if (hdev->flush) |
| 2211 | hdev->flush(hdev); |
| 2212 | |
| 2213 | /* Reset device */ |
| 2214 | skb_queue_purge(&hdev->cmd_q); |
| 2215 | atomic_set(&hdev->cmd_cnt, 1); |
Johan Hedberg | 8af5946 | 2012-02-03 21:29:40 +0200 | [diff] [blame] | 2216 | if (!test_bit(HCI_RAW, &hdev->flags) && |
Marcel Holtmann | 3a6afbd | 2013-10-11 09:44:12 -0700 | [diff] [blame] | 2217 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags) && |
Szymon Janc | a6c511c | 2012-05-23 12:35:46 +0200 | [diff] [blame] | 2218 | test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | set_bit(HCI_INIT, &hdev->flags); |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2220 | __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | clear_bit(HCI_INIT, &hdev->flags); |
| 2222 | } |
| 2223 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2224 | /* flush cmd work */ |
| 2225 | flush_work(&hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | |
| 2227 | /* Drop queues */ |
| 2228 | skb_queue_purge(&hdev->rx_q); |
| 2229 | skb_queue_purge(&hdev->cmd_q); |
| 2230 | skb_queue_purge(&hdev->raw_q); |
| 2231 | |
| 2232 | /* Drop last sent command */ |
| 2233 | if (hdev->sent_cmd) { |
Vinicius Costa Gomes | b79f44c | 2011-04-11 18:46:55 -0300 | [diff] [blame] | 2234 | del_timer_sync(&hdev->cmd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | kfree_skb(hdev->sent_cmd); |
| 2236 | hdev->sent_cmd = NULL; |
| 2237 | } |
| 2238 | |
Johan Hedberg | b6ddb63 | 2013-04-02 13:34:31 +0300 | [diff] [blame] | 2239 | kfree_skb(hdev->recv_evt); |
| 2240 | hdev->recv_evt = NULL; |
| 2241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | /* After this point our queues are empty |
| 2243 | * and no tasks are scheduled. */ |
| 2244 | hdev->close(hdev); |
| 2245 | |
Johan Hedberg | 35b973c | 2013-03-15 17:06:59 -0500 | [diff] [blame] | 2246 | /* Clear flags */ |
| 2247 | hdev->flags = 0; |
| 2248 | hdev->dev_flags &= ~HCI_PERSISTENT_MASK; |
| 2249 | |
Marcel Holtmann | 93c311a | 2013-10-07 00:58:33 -0700 | [diff] [blame] | 2250 | if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 2251 | if (hdev->dev_type == HCI_BREDR) { |
| 2252 | hci_dev_lock(hdev); |
| 2253 | mgmt_powered(hdev, 0); |
| 2254 | hci_dev_unlock(hdev); |
| 2255 | } |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 2256 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2257 | |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 2258 | /* Controller radio is available but is currently powered down */ |
Marcel Holtmann | 536619e | 2013-10-05 11:47:45 -0700 | [diff] [blame] | 2259 | hdev->amp_status = AMP_STATUS_POWERED_DOWN; |
Andrei Emeltchenko | ced5c33 | 2012-11-28 17:59:42 +0200 | [diff] [blame] | 2260 | |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 2261 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
Johan Hedberg | 09b3c3f | 2012-02-22 22:01:41 +0200 | [diff] [blame] | 2262 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); |
Marcel Holtmann | 7a4cd51 | 2014-02-19 19:52:13 -0800 | [diff] [blame] | 2263 | bacpy(&hdev->random_addr, BDADDR_ANY); |
Johan Hedberg | e59fda8 | 2012-02-22 18:11:53 +0200 | [diff] [blame] | 2264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | hci_req_unlock(hdev); |
| 2266 | |
| 2267 | hci_dev_put(hdev); |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
| 2271 | int hci_dev_close(__u16 dev) |
| 2272 | { |
| 2273 | struct hci_dev *hdev; |
| 2274 | int err; |
| 2275 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2276 | hdev = hci_dev_get(dev); |
| 2277 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | return -ENODEV; |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 2279 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2280 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 2281 | err = -EBUSY; |
| 2282 | goto done; |
| 2283 | } |
| 2284 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 2285 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 2286 | cancel_delayed_work(&hdev->power_off); |
| 2287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | err = hci_dev_do_close(hdev); |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 2289 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2290 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | hci_dev_put(hdev); |
| 2292 | return err; |
| 2293 | } |
| 2294 | |
| 2295 | int hci_dev_reset(__u16 dev) |
| 2296 | { |
| 2297 | struct hci_dev *hdev; |
| 2298 | int ret = 0; |
| 2299 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2300 | hdev = hci_dev_get(dev); |
| 2301 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | return -ENODEV; |
| 2303 | |
| 2304 | hci_req_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Marcel Holtmann | 808a049 | 2013-08-26 20:57:58 -0700 | [diff] [blame] | 2306 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 2307 | ret = -ENETDOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | goto done; |
Marcel Holtmann | 808a049 | 2013-08-26 20:57:58 -0700 | [diff] [blame] | 2309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2311 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 2312 | ret = -EBUSY; |
| 2313 | goto done; |
| 2314 | } |
| 2315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | /* Drop queues */ |
| 2317 | skb_queue_purge(&hdev->rx_q); |
| 2318 | skb_queue_purge(&hdev->cmd_q); |
| 2319 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2320 | hci_dev_lock(hdev); |
Andre Guedes | 1f9b9a5 | 2013-04-30 15:29:27 -0300 | [diff] [blame] | 2321 | hci_inquiry_cache_flush(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | hci_conn_hash_flush(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2323 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
| 2325 | if (hdev->flush) |
| 2326 | hdev->flush(hdev); |
| 2327 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2328 | atomic_set(&hdev->cmd_cnt, 1); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 2329 | hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | |
| 2331 | if (!test_bit(HCI_RAW, &hdev->flags)) |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2332 | ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | |
| 2334 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | hci_req_unlock(hdev); |
| 2336 | hci_dev_put(hdev); |
| 2337 | return ret; |
| 2338 | } |
| 2339 | |
| 2340 | int hci_dev_reset_stat(__u16 dev) |
| 2341 | { |
| 2342 | struct hci_dev *hdev; |
| 2343 | int ret = 0; |
| 2344 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2345 | hdev = hci_dev_get(dev); |
| 2346 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | return -ENODEV; |
| 2348 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2349 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 2350 | ret = -EBUSY; |
| 2351 | goto done; |
| 2352 | } |
| 2353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); |
| 2355 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2356 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | hci_dev_put(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | return ret; |
| 2359 | } |
| 2360 | |
| 2361 | int hci_dev_cmd(unsigned int cmd, void __user *arg) |
| 2362 | { |
| 2363 | struct hci_dev *hdev; |
| 2364 | struct hci_dev_req dr; |
| 2365 | int err = 0; |
| 2366 | |
| 2367 | if (copy_from_user(&dr, arg, sizeof(dr))) |
| 2368 | return -EFAULT; |
| 2369 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2370 | hdev = hci_dev_get(dr.dev_id); |
| 2371 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | return -ENODEV; |
| 2373 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2374 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 2375 | err = -EBUSY; |
| 2376 | goto done; |
| 2377 | } |
| 2378 | |
Marcel Holtmann | 5b69bef5 | 2013-10-10 10:02:08 -0700 | [diff] [blame] | 2379 | if (hdev->dev_type != HCI_BREDR) { |
| 2380 | err = -EOPNOTSUPP; |
| 2381 | goto done; |
| 2382 | } |
| 2383 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2384 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 2385 | err = -EOPNOTSUPP; |
| 2386 | goto done; |
| 2387 | } |
| 2388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | switch (cmd) { |
| 2390 | case HCISETAUTH: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2391 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 2392 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | break; |
| 2394 | |
| 2395 | case HCISETENCRYPT: |
| 2396 | if (!lmp_encrypt_capable(hdev)) { |
| 2397 | err = -EOPNOTSUPP; |
| 2398 | break; |
| 2399 | } |
| 2400 | |
| 2401 | if (!test_bit(HCI_AUTH, &hdev->flags)) { |
| 2402 | /* Auth must be enabled first */ |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2403 | err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, |
| 2404 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | if (err) |
| 2406 | break; |
| 2407 | } |
| 2408 | |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2409 | err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, |
| 2410 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | break; |
| 2412 | |
| 2413 | case HCISETSCAN: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2414 | err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, |
| 2415 | HCI_INIT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | break; |
| 2417 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 2418 | case HCISETLINKPOL: |
Johan Hedberg | 01178cd | 2013-03-05 20:37:41 +0200 | [diff] [blame] | 2419 | err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, |
| 2420 | HCI_INIT_TIMEOUT); |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 2421 | break; |
| 2422 | |
| 2423 | case HCISETLINKMODE: |
| 2424 | hdev->link_mode = ((__u16) dr.dev_opt) & |
| 2425 | (HCI_LM_MASTER | HCI_LM_ACCEPT); |
| 2426 | break; |
| 2427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | case HCISETPTYPE: |
| 2429 | hdev->pkt_type = (__u16) dr.dev_opt; |
| 2430 | break; |
| 2431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | case HCISETACLMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 2433 | hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 2434 | hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | break; |
| 2436 | |
| 2437 | case HCISETSCOMTU: |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 2438 | hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); |
| 2439 | hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | break; |
| 2441 | |
| 2442 | default: |
| 2443 | err = -EINVAL; |
| 2444 | break; |
| 2445 | } |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 2446 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2447 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | hci_dev_put(hdev); |
| 2449 | return err; |
| 2450 | } |
| 2451 | |
| 2452 | int hci_get_dev_list(void __user *arg) |
| 2453 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2454 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | struct hci_dev_list_req *dl; |
| 2456 | struct hci_dev_req *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | int n = 0, size, err; |
| 2458 | __u16 dev_num; |
| 2459 | |
| 2460 | if (get_user(dev_num, (__u16 __user *) arg)) |
| 2461 | return -EFAULT; |
| 2462 | |
| 2463 | if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) |
| 2464 | return -EINVAL; |
| 2465 | |
| 2466 | size = sizeof(*dl) + dev_num * sizeof(*dr); |
| 2467 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2468 | dl = kzalloc(size, GFP_KERNEL); |
| 2469 | if (!dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | return -ENOMEM; |
| 2471 | |
| 2472 | dr = dl->dev_req; |
| 2473 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2474 | read_lock(&hci_dev_list_lock); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2475 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2476 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 2477 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2478 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2479 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 2480 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | (dr + n)->dev_id = hdev->id; |
| 2483 | (dr + n)->dev_opt = hdev->flags; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | if (++n >= dev_num) |
| 2486 | break; |
| 2487 | } |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 2488 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
| 2490 | dl->dev_num = n; |
| 2491 | size = sizeof(*dl) + n * sizeof(*dr); |
| 2492 | |
| 2493 | err = copy_to_user(arg, dl, size); |
| 2494 | kfree(dl); |
| 2495 | |
| 2496 | return err ? -EFAULT : 0; |
| 2497 | } |
| 2498 | |
| 2499 | int hci_get_dev_info(void __user *arg) |
| 2500 | { |
| 2501 | struct hci_dev *hdev; |
| 2502 | struct hci_dev_info di; |
| 2503 | int err = 0; |
| 2504 | |
| 2505 | if (copy_from_user(&di, arg, sizeof(di))) |
| 2506 | return -EFAULT; |
| 2507 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 2508 | hdev = hci_dev_get(di.dev_id); |
| 2509 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | return -ENODEV; |
| 2511 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2512 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 2513 | cancel_delayed_work_sync(&hdev->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2514 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2515 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 2516 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | strcpy(di.name, hdev->name); |
| 2519 | di.bdaddr = hdev->bdaddr; |
Marcel Holtmann | 60f2a3e | 2013-10-01 22:59:20 -0700 | [diff] [blame] | 2520 | di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | di.flags = hdev->flags; |
| 2522 | di.pkt_type = hdev->pkt_type; |
Johan Hedberg | 572c7f8 | 2012-10-19 20:57:46 +0300 | [diff] [blame] | 2523 | if (lmp_bredr_capable(hdev)) { |
| 2524 | di.acl_mtu = hdev->acl_mtu; |
| 2525 | di.acl_pkts = hdev->acl_pkts; |
| 2526 | di.sco_mtu = hdev->sco_mtu; |
| 2527 | di.sco_pkts = hdev->sco_pkts; |
| 2528 | } else { |
| 2529 | di.acl_mtu = hdev->le_mtu; |
| 2530 | di.acl_pkts = hdev->le_pkts; |
| 2531 | di.sco_mtu = 0; |
| 2532 | di.sco_pkts = 0; |
| 2533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | di.link_policy = hdev->link_policy; |
| 2535 | di.link_mode = hdev->link_mode; |
| 2536 | |
| 2537 | memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); |
| 2538 | memcpy(&di.features, &hdev->features, sizeof(di.features)); |
| 2539 | |
| 2540 | if (copy_to_user(arg, &di, sizeof(di))) |
| 2541 | err = -EFAULT; |
| 2542 | |
| 2543 | hci_dev_put(hdev); |
| 2544 | |
| 2545 | return err; |
| 2546 | } |
| 2547 | |
| 2548 | /* ---- Interface to HCI drivers ---- */ |
| 2549 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2550 | static int hci_rfkill_set_block(void *data, bool blocked) |
| 2551 | { |
| 2552 | struct hci_dev *hdev = data; |
| 2553 | |
| 2554 | BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); |
| 2555 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 2556 | if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) |
| 2557 | return -EBUSY; |
| 2558 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 2559 | if (blocked) { |
| 2560 | set_bit(HCI_RFKILLED, &hdev->dev_flags); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 2561 | if (!test_bit(HCI_SETUP, &hdev->dev_flags)) |
| 2562 | hci_dev_do_close(hdev); |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 2563 | } else { |
| 2564 | clear_bit(HCI_RFKILLED, &hdev->dev_flags); |
Gustavo Padovan | 1025c04 | 2013-09-27 11:56:14 -0300 | [diff] [blame] | 2565 | } |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 2566 | |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
| 2570 | static const struct rfkill_ops hci_rfkill_ops = { |
| 2571 | .set_block = hci_rfkill_set_block, |
| 2572 | }; |
| 2573 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2574 | static void hci_power_on(struct work_struct *work) |
| 2575 | { |
| 2576 | struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2577 | int err; |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2578 | |
| 2579 | BT_DBG("%s", hdev->name); |
| 2580 | |
Johan Hedberg | cbed0ca | 2013-10-01 22:44:49 +0300 | [diff] [blame] | 2581 | err = hci_dev_do_open(hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2582 | if (err < 0) { |
| 2583 | mgmt_set_powered_failed(hdev, err); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2584 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 2585 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2586 | |
Marcel Holtmann | a5c8f27 | 2013-10-06 01:08:57 -0700 | [diff] [blame] | 2587 | /* During the HCI setup phase, a few error conditions are |
| 2588 | * ignored and they need to be checked now. If they are still |
| 2589 | * valid, it is important to turn the device back off. |
| 2590 | */ |
| 2591 | if (test_bit(HCI_RFKILLED, &hdev->dev_flags) || |
| 2592 | (hdev->dev_type == HCI_BREDR && |
| 2593 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && |
| 2594 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 2595 | clear_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 2596 | hci_dev_do_close(hdev); |
| 2597 | } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 2598 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 2599 | HCI_AUTO_OFF_TIMEOUT); |
Johan Hedberg | bf54303 | 2013-09-13 08:58:18 +0300 | [diff] [blame] | 2600 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2601 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2602 | if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2603 | mgmt_index_added(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | static void hci_power_off(struct work_struct *work) |
| 2607 | { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 2608 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2609 | power_off.work); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2610 | |
| 2611 | BT_DBG("%s", hdev->name); |
| 2612 | |
Marcel Holtmann | 8ee5654 | 2012-02-21 12:33:48 +0100 | [diff] [blame] | 2613 | hci_dev_do_close(hdev); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 2614 | } |
| 2615 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 2616 | static void hci_discov_off(struct work_struct *work) |
| 2617 | { |
| 2618 | struct hci_dev *hdev; |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 2619 | |
| 2620 | hdev = container_of(work, struct hci_dev, discov_off.work); |
| 2621 | |
| 2622 | BT_DBG("%s", hdev->name); |
| 2623 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 2624 | mgmt_discoverable_timeout(hdev); |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2627 | void hci_uuids_clear(struct hci_dev *hdev) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2628 | { |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 2629 | struct bt_uuid *uuid, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2630 | |
Johan Hedberg | 4821002 | 2013-01-27 00:31:28 +0200 | [diff] [blame] | 2631 | list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { |
| 2632 | list_del(&uuid->list); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2633 | kfree(uuid); |
| 2634 | } |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2637 | void hci_link_keys_clear(struct hci_dev *hdev) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2638 | { |
| 2639 | struct list_head *p, *n; |
| 2640 | |
| 2641 | list_for_each_safe(p, n, &hdev->link_keys) { |
| 2642 | struct link_key *key; |
| 2643 | |
| 2644 | key = list_entry(p, struct link_key, list); |
| 2645 | |
| 2646 | list_del(p); |
| 2647 | kfree(key); |
| 2648 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2649 | } |
| 2650 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2651 | void hci_smp_ltks_clear(struct hci_dev *hdev) |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2652 | { |
| 2653 | struct smp_ltk *k, *tmp; |
| 2654 | |
| 2655 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 2656 | list_del(&k->list); |
| 2657 | kfree(k); |
| 2658 | } |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2659 | } |
| 2660 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2661 | void hci_smp_irks_clear(struct hci_dev *hdev) |
| 2662 | { |
| 2663 | struct smp_irk *k, *tmp; |
| 2664 | |
| 2665 | list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) { |
| 2666 | list_del(&k->list); |
| 2667 | kfree(k); |
| 2668 | } |
| 2669 | } |
| 2670 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2671 | struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2672 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2673 | struct link_key *k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2674 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2675 | list_for_each_entry(k, &hdev->link_keys, list) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2676 | if (bacmp(bdaddr, &k->bdaddr) == 0) |
| 2677 | return k; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2678 | |
| 2679 | return NULL; |
| 2680 | } |
| 2681 | |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2682 | 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] | 2683 | u8 key_type, u8 old_key_type) |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2684 | { |
| 2685 | /* Legacy key */ |
| 2686 | if (key_type < 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2687 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2688 | |
| 2689 | /* Debug keys are insecure so don't store them persistently */ |
| 2690 | if (key_type == HCI_LK_DEBUG_COMBINATION) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2691 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2692 | |
| 2693 | /* Changed combination key and there's no previous one */ |
| 2694 | if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2695 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2696 | |
| 2697 | /* Security mode 3 case */ |
| 2698 | if (!conn) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2699 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2700 | |
| 2701 | /* Neither local nor remote side had no-bonding as requirement */ |
| 2702 | if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2703 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2704 | |
| 2705 | /* Local side had dedicated bonding as requirement */ |
| 2706 | if (conn->auth_type == 0x02 || conn->auth_type == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2707 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2708 | |
| 2709 | /* Remote side had dedicated bonding as requirement */ |
| 2710 | if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2711 | return true; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2712 | |
| 2713 | /* If none of the above criteria match, then don't store the key |
| 2714 | * persistently */ |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2715 | return false; |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2718 | static bool ltk_type_master(u8 type) |
| 2719 | { |
| 2720 | if (type == HCI_SMP_STK || type == HCI_SMP_LTK) |
| 2721 | return true; |
| 2722 | |
| 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8], |
| 2727 | bool master) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2728 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2729 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2730 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2731 | list_for_each_entry(k, &hdev->long_term_keys, list) { |
| 2732 | if (k->ediv != ediv || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2733 | memcmp(rand, k->rand, sizeof(k->rand))) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2734 | continue; |
| 2735 | |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2736 | if (ltk_type_master(k->type) != master) |
| 2737 | continue; |
| 2738 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2739 | return k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | return NULL; |
| 2743 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2744 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2745 | struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2746 | u8 addr_type, bool master) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2747 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2748 | struct smp_ltk *k; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2749 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2750 | list_for_each_entry(k, &hdev->long_term_keys, list) |
| 2751 | if (addr_type == k->bdaddr_type && |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2752 | bacmp(bdaddr, &k->bdaddr) == 0 && |
| 2753 | ltk_type_master(k->type) == master) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2754 | return k; |
| 2755 | |
| 2756 | return NULL; |
| 2757 | } |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2758 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2759 | struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa) |
| 2760 | { |
| 2761 | struct smp_irk *irk; |
| 2762 | |
| 2763 | list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { |
| 2764 | if (!bacmp(&irk->rpa, rpa)) |
| 2765 | return irk; |
| 2766 | } |
| 2767 | |
| 2768 | list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { |
| 2769 | if (smp_irk_matches(hdev->tfm_aes, irk->val, rpa)) { |
| 2770 | bacpy(&irk->rpa, rpa); |
| 2771 | return irk; |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | return NULL; |
| 2776 | } |
| 2777 | |
| 2778 | struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2779 | u8 addr_type) |
| 2780 | { |
| 2781 | struct smp_irk *irk; |
| 2782 | |
Johan Hedberg | 6cfc998 | 2014-02-18 21:41:35 +0200 | [diff] [blame] | 2783 | /* Identity Address must be public or static random */ |
| 2784 | if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0) |
| 2785 | return NULL; |
| 2786 | |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2787 | list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { |
| 2788 | if (addr_type == irk->addr_type && |
| 2789 | bacmp(bdaddr, &irk->bdaddr) == 0) |
| 2790 | return irk; |
| 2791 | } |
| 2792 | |
| 2793 | return NULL; |
| 2794 | } |
| 2795 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2796 | int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2797 | bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2798 | { |
| 2799 | struct link_key *key, *old_key; |
Vishal Agarwal | 745c0ce | 2012-04-13 17:43:22 +0530 | [diff] [blame] | 2800 | u8 old_key_type; |
| 2801 | bool persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2802 | |
| 2803 | old_key = hci_find_link_key(hdev, bdaddr); |
| 2804 | if (old_key) { |
| 2805 | old_key_type = old_key->type; |
| 2806 | key = old_key; |
| 2807 | } else { |
Johan Hedberg | 12adcf3 | 2011-04-28 11:29:00 -0700 | [diff] [blame] | 2808 | old_key_type = conn ? conn->key_type : 0xff; |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 2809 | key = kzalloc(sizeof(*key), GFP_KERNEL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2810 | if (!key) |
| 2811 | return -ENOMEM; |
| 2812 | list_add(&key->list, &hdev->link_keys); |
| 2813 | } |
| 2814 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2815 | BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2816 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2817 | /* Some buggy controller combinations generate a changed |
| 2818 | * combination key for legacy pairing even when there's no |
| 2819 | * previous key */ |
| 2820 | if (type == HCI_LK_CHANGED_COMBINATION && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 2821 | (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2822 | type = HCI_LK_COMBINATION; |
Johan Hedberg | 655fe6e | 2011-04-28 11:29:01 -0700 | [diff] [blame] | 2823 | if (conn) |
| 2824 | conn->key_type = type; |
| 2825 | } |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 2826 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2827 | bacpy(&key->bdaddr, bdaddr); |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 2828 | memcpy(key->val, val, HCI_LINK_KEY_SIZE); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2829 | key->pin_len = pin_len; |
| 2830 | |
Waldemar Rymarkiewicz | b6020ba | 2011-04-28 12:07:53 +0200 | [diff] [blame] | 2831 | if (type == HCI_LK_CHANGED_COMBINATION) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2832 | key->type = old_key_type; |
Johan Hedberg | 4748fed | 2011-04-28 11:29:02 -0700 | [diff] [blame] | 2833 | else |
| 2834 | key->type = type; |
| 2835 | |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 2836 | if (!new_key) |
| 2837 | return 0; |
| 2838 | |
| 2839 | persistent = hci_persistent_key(hdev, conn, type, old_key_type); |
| 2840 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2841 | mgmt_new_link_key(hdev, key, persistent); |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 2842 | |
Vishal Agarwal | 6ec5bca | 2012-04-16 14:44:44 +0530 | [diff] [blame] | 2843 | if (conn) |
| 2844 | conn->flush_key = !persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2845 | |
| 2846 | return 0; |
| 2847 | } |
| 2848 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2849 | 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] | 2850 | u8 addr_type, u8 type, u8 authenticated, |
| 2851 | u8 tk[16], u8 enc_size, __le16 ediv, u8 rand[8]) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2852 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2853 | struct smp_ltk *key, *old_key; |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2854 | bool master = ltk_type_master(type); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2855 | |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 2856 | old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type, master); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2857 | if (old_key) |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2858 | key = old_key; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2859 | else { |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 2860 | key = kzalloc(sizeof(*key), GFP_KERNEL); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2861 | if (!key) |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2862 | return NULL; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2863 | list_add(&key->list, &hdev->long_term_keys); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2864 | } |
| 2865 | |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2866 | bacpy(&key->bdaddr, bdaddr); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2867 | key->bdaddr_type = addr_type; |
| 2868 | memcpy(key->val, tk, sizeof(key->val)); |
| 2869 | key->authenticated = authenticated; |
| 2870 | key->ediv = ediv; |
| 2871 | key->enc_size = enc_size; |
| 2872 | key->type = type; |
| 2873 | memcpy(key->rand, rand, sizeof(key->rand)); |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2874 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2875 | return key; |
Vinicius Costa Gomes | 75d262c | 2011-07-07 18:59:36 -0300 | [diff] [blame] | 2876 | } |
| 2877 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2878 | struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2879 | u8 addr_type, u8 val[16], bdaddr_t *rpa) |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2880 | { |
| 2881 | struct smp_irk *irk; |
| 2882 | |
| 2883 | irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type); |
| 2884 | if (!irk) { |
| 2885 | irk = kzalloc(sizeof(*irk), GFP_KERNEL); |
| 2886 | if (!irk) |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2887 | return NULL; |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2888 | |
| 2889 | bacpy(&irk->bdaddr, bdaddr); |
| 2890 | irk->addr_type = addr_type; |
| 2891 | |
| 2892 | list_add(&irk->list, &hdev->identity_resolving_keys); |
| 2893 | } |
| 2894 | |
| 2895 | memcpy(irk->val, val, 16); |
| 2896 | bacpy(&irk->rpa, rpa); |
| 2897 | |
Johan Hedberg | ca9142b | 2014-02-19 14:57:44 +0200 | [diff] [blame] | 2898 | return irk; |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2901 | int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2902 | { |
| 2903 | struct link_key *key; |
| 2904 | |
| 2905 | key = hci_find_link_key(hdev, bdaddr); |
| 2906 | if (!key) |
| 2907 | return -ENOENT; |
| 2908 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2909 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2910 | |
| 2911 | list_del(&key->list); |
| 2912 | kfree(key); |
| 2913 | |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2917 | 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] | 2918 | { |
| 2919 | struct smp_ltk *k, *tmp; |
Johan Hedberg | c51ffa0 | 2014-02-18 17:14:33 +0200 | [diff] [blame] | 2920 | int removed = 0; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2921 | |
| 2922 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2923 | if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type) |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2924 | continue; |
| 2925 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2926 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2927 | |
| 2928 | list_del(&k->list); |
| 2929 | kfree(k); |
Johan Hedberg | c51ffa0 | 2014-02-18 17:14:33 +0200 | [diff] [blame] | 2930 | removed++; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2931 | } |
| 2932 | |
Johan Hedberg | c51ffa0 | 2014-02-18 17:14:33 +0200 | [diff] [blame] | 2933 | return removed ? 0 : -ENOENT; |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 2934 | } |
| 2935 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2936 | void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type) |
| 2937 | { |
| 2938 | struct smp_irk *k, *tmp; |
| 2939 | |
| 2940 | list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { |
| 2941 | if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type) |
| 2942 | continue; |
| 2943 | |
| 2944 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
| 2945 | |
| 2946 | list_del(&k->list); |
| 2947 | kfree(k); |
| 2948 | } |
| 2949 | } |
| 2950 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2951 | /* HCI command timer function */ |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2952 | static void hci_cmd_timeout(unsigned long arg) |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2953 | { |
| 2954 | struct hci_dev *hdev = (void *) arg; |
| 2955 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 2956 | if (hdev->sent_cmd) { |
| 2957 | struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; |
| 2958 | u16 opcode = __le16_to_cpu(sent->opcode); |
| 2959 | |
| 2960 | BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); |
| 2961 | } else { |
| 2962 | BT_ERR("%s command tx timeout", hdev->name); |
| 2963 | } |
| 2964 | |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2965 | atomic_set(&hdev->cmd_cnt, 1); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 2966 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Ville Tervo | 6bd3232 | 2011-02-16 16:32:41 +0200 | [diff] [blame] | 2967 | } |
| 2968 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2969 | struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2970 | bdaddr_t *bdaddr) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2971 | { |
| 2972 | struct oob_data *data; |
| 2973 | |
| 2974 | list_for_each_entry(data, &hdev->remote_oob_data, list) |
| 2975 | if (bacmp(bdaddr, &data->bdaddr) == 0) |
| 2976 | return data; |
| 2977 | |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
| 2981 | int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) |
| 2982 | { |
| 2983 | struct oob_data *data; |
| 2984 | |
| 2985 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 2986 | if (!data) |
| 2987 | return -ENOENT; |
| 2988 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 2989 | BT_DBG("%s removing %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2990 | |
| 2991 | list_del(&data->list); |
| 2992 | kfree(data); |
| 2993 | |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2997 | void hci_remote_oob_data_clear(struct hci_dev *hdev) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2998 | { |
| 2999 | struct oob_data *data, *n; |
| 3000 | |
| 3001 | list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { |
| 3002 | list_del(&data->list); |
| 3003 | kfree(data); |
| 3004 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3005 | } |
| 3006 | |
Marcel Holtmann | 0798872 | 2014-01-10 02:07:29 -0800 | [diff] [blame] | 3007 | int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3008 | u8 *hash, u8 *randomizer) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3009 | { |
| 3010 | struct oob_data *data; |
| 3011 | |
| 3012 | data = hci_find_remote_oob_data(hdev, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3013 | if (!data) { |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 3014 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3015 | if (!data) |
| 3016 | return -ENOMEM; |
| 3017 | |
| 3018 | bacpy(&data->bdaddr, bdaddr); |
| 3019 | list_add(&data->list, &hdev->remote_oob_data); |
| 3020 | } |
| 3021 | |
Marcel Holtmann | 519ca9d | 2014-01-10 02:07:28 -0800 | [diff] [blame] | 3022 | memcpy(data->hash192, hash, sizeof(data->hash192)); |
| 3023 | memcpy(data->randomizer192, randomizer, sizeof(data->randomizer192)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3024 | |
Marcel Holtmann | 0798872 | 2014-01-10 02:07:29 -0800 | [diff] [blame] | 3025 | memset(data->hash256, 0, sizeof(data->hash256)); |
| 3026 | memset(data->randomizer256, 0, sizeof(data->randomizer256)); |
| 3027 | |
| 3028 | BT_DBG("%s for %pMR", hdev->name, bdaddr); |
| 3029 | |
| 3030 | return 0; |
| 3031 | } |
| 3032 | |
| 3033 | int hci_add_remote_oob_ext_data(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3034 | u8 *hash192, u8 *randomizer192, |
| 3035 | u8 *hash256, u8 *randomizer256) |
| 3036 | { |
| 3037 | struct oob_data *data; |
| 3038 | |
| 3039 | data = hci_find_remote_oob_data(hdev, bdaddr); |
| 3040 | if (!data) { |
Johan Hedberg | 0a14ab4 | 2014-02-19 14:57:43 +0200 | [diff] [blame] | 3041 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
Marcel Holtmann | 0798872 | 2014-01-10 02:07:29 -0800 | [diff] [blame] | 3042 | if (!data) |
| 3043 | return -ENOMEM; |
| 3044 | |
| 3045 | bacpy(&data->bdaddr, bdaddr); |
| 3046 | list_add(&data->list, &hdev->remote_oob_data); |
| 3047 | } |
| 3048 | |
| 3049 | memcpy(data->hash192, hash192, sizeof(data->hash192)); |
| 3050 | memcpy(data->randomizer192, randomizer192, sizeof(data->randomizer192)); |
| 3051 | |
| 3052 | memcpy(data->hash256, hash256, sizeof(data->hash256)); |
| 3053 | memcpy(data->randomizer256, randomizer256, sizeof(data->randomizer256)); |
| 3054 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 3055 | BT_DBG("%s for %pMR", hdev->name, bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3056 | |
| 3057 | return 0; |
| 3058 | } |
| 3059 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3060 | struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, |
| 3061 | bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3062 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3063 | struct bdaddr_list *b; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3064 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3065 | list_for_each_entry(b, &hdev->blacklist, list) { |
| 3066 | if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3067 | return b; |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3068 | } |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3069 | |
| 3070 | return NULL; |
| 3071 | } |
| 3072 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 3073 | void hci_blacklist_clear(struct hci_dev *hdev) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3074 | { |
| 3075 | struct list_head *p, *n; |
| 3076 | |
| 3077 | list_for_each_safe(p, n, &hdev->blacklist) { |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3078 | struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3079 | |
| 3080 | list_del(p); |
| 3081 | kfree(b); |
| 3082 | } |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3083 | } |
| 3084 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3085 | int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3086 | { |
| 3087 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3088 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3089 | if (!bacmp(bdaddr, BDADDR_ANY)) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3090 | return -EBADF; |
| 3091 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3092 | if (hci_blacklist_lookup(hdev, bdaddr, type)) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3093 | return -EEXIST; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3094 | |
| 3095 | entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3096 | if (!entry) |
| 3097 | return -ENOMEM; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3098 | |
| 3099 | bacpy(&entry->bdaddr, bdaddr); |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3100 | entry->bdaddr_type = type; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3101 | |
| 3102 | list_add(&entry->list, &hdev->blacklist); |
| 3103 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3104 | return mgmt_device_blocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3105 | } |
| 3106 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3107 | int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3108 | { |
| 3109 | struct bdaddr_list *entry; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3110 | |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 3111 | if (!bacmp(bdaddr, BDADDR_ANY)) { |
| 3112 | hci_blacklist_clear(hdev); |
| 3113 | return 0; |
| 3114 | } |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3115 | |
Marcel Holtmann | b9ee0a7 | 2013-10-17 17:24:13 -0700 | [diff] [blame] | 3116 | entry = hci_blacklist_lookup(hdev, bdaddr, type); |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 3117 | if (!entry) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3118 | return -ENOENT; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3119 | |
| 3120 | list_del(&entry->list); |
| 3121 | kfree(entry); |
| 3122 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3123 | return mgmt_device_unblocked(hdev, bdaddr, type); |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 3124 | } |
| 3125 | |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 3126 | /* This function requires the caller holds hdev->lock */ |
| 3127 | struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, |
| 3128 | bdaddr_t *addr, u8 addr_type) |
| 3129 | { |
| 3130 | struct hci_conn_params *params; |
| 3131 | |
| 3132 | list_for_each_entry(params, &hdev->le_conn_params, list) { |
| 3133 | if (bacmp(¶ms->addr, addr) == 0 && |
| 3134 | params->addr_type == addr_type) { |
| 3135 | return params; |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | |
| 3142 | /* This function requires the caller holds hdev->lock */ |
| 3143 | void hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type, |
| 3144 | u16 conn_min_interval, u16 conn_max_interval) |
| 3145 | { |
| 3146 | struct hci_conn_params *params; |
| 3147 | |
| 3148 | params = hci_conn_params_lookup(hdev, addr, addr_type); |
| 3149 | if (params) { |
| 3150 | params->conn_min_interval = conn_min_interval; |
| 3151 | params->conn_max_interval = conn_max_interval; |
| 3152 | return; |
| 3153 | } |
| 3154 | |
| 3155 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
| 3156 | if (!params) { |
| 3157 | BT_ERR("Out of memory"); |
| 3158 | return; |
| 3159 | } |
| 3160 | |
| 3161 | bacpy(¶ms->addr, addr); |
| 3162 | params->addr_type = addr_type; |
| 3163 | params->conn_min_interval = conn_min_interval; |
| 3164 | params->conn_max_interval = conn_max_interval; |
| 3165 | |
| 3166 | list_add(¶ms->list, &hdev->le_conn_params); |
| 3167 | |
| 3168 | BT_DBG("addr %pMR (type %u) conn_min_interval 0x%.4x " |
| 3169 | "conn_max_interval 0x%.4x", addr, addr_type, conn_min_interval, |
| 3170 | conn_max_interval); |
| 3171 | } |
| 3172 | |
| 3173 | /* This function requires the caller holds hdev->lock */ |
| 3174 | void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type) |
| 3175 | { |
| 3176 | struct hci_conn_params *params; |
| 3177 | |
| 3178 | params = hci_conn_params_lookup(hdev, addr, addr_type); |
| 3179 | if (!params) |
| 3180 | return; |
| 3181 | |
| 3182 | list_del(¶ms->list); |
| 3183 | kfree(params); |
| 3184 | |
| 3185 | BT_DBG("addr %pMR (type %u)", addr, addr_type); |
| 3186 | } |
| 3187 | |
| 3188 | /* This function requires the caller holds hdev->lock */ |
| 3189 | void hci_conn_params_clear(struct hci_dev *hdev) |
| 3190 | { |
| 3191 | struct hci_conn_params *params, *tmp; |
| 3192 | |
| 3193 | list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { |
| 3194 | list_del(¶ms->list); |
| 3195 | kfree(params); |
| 3196 | } |
| 3197 | |
| 3198 | BT_DBG("All LE connection parameters were removed"); |
| 3199 | } |
| 3200 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3201 | static void inquiry_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3202 | { |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3203 | if (status) { |
| 3204 | BT_ERR("Failed to start inquiry: status %d", status); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3205 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3206 | hci_dev_lock(hdev); |
| 3207 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3208 | hci_dev_unlock(hdev); |
| 3209 | return; |
| 3210 | } |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3211 | } |
| 3212 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3213 | static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3214 | { |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3215 | /* General inquiry access code (GIAC) */ |
| 3216 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 3217 | struct hci_request req; |
| 3218 | struct hci_cp_inquiry cp; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3219 | int err; |
| 3220 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3221 | if (status) { |
| 3222 | BT_ERR("Failed to disable LE scanning: status %d", status); |
| 3223 | return; |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 3224 | } |
| 3225 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3226 | switch (hdev->discovery.type) { |
| 3227 | case DISCOV_TYPE_LE: |
| 3228 | hci_dev_lock(hdev); |
| 3229 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3230 | hci_dev_unlock(hdev); |
| 3231 | break; |
| 3232 | |
| 3233 | case DISCOV_TYPE_INTERLEAVED: |
| 3234 | hci_req_init(&req, hdev); |
| 3235 | |
| 3236 | memset(&cp, 0, sizeof(cp)); |
| 3237 | memcpy(&cp.lap, lap, sizeof(cp.lap)); |
| 3238 | cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; |
| 3239 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 3240 | |
| 3241 | hci_dev_lock(hdev); |
| 3242 | |
| 3243 | hci_inquiry_cache_flush(hdev); |
| 3244 | |
| 3245 | err = hci_req_run(&req, inquiry_complete); |
| 3246 | if (err) { |
| 3247 | BT_ERR("Inquiry request failed: err %d", err); |
| 3248 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3249 | } |
| 3250 | |
| 3251 | hci_dev_unlock(hdev); |
| 3252 | break; |
| 3253 | } |
Andre Guedes | 7dbfac1 | 2012-03-15 16:52:07 -0300 | [diff] [blame] | 3254 | } |
| 3255 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3256 | static void le_scan_disable_work(struct work_struct *work) |
| 3257 | { |
| 3258 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3259 | le_scan_disable.work); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3260 | struct hci_cp_le_set_scan_enable cp; |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3261 | struct hci_request req; |
| 3262 | int err; |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3263 | |
| 3264 | BT_DBG("%s", hdev->name); |
| 3265 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3266 | hci_req_init(&req, hdev); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3267 | |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3268 | memset(&cp, 0, sizeof(cp)); |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3269 | cp.enable = LE_SCAN_DISABLE; |
| 3270 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
Andre Guedes | 7ba8b4b | 2012-02-03 17:47:59 -0300 | [diff] [blame] | 3271 | |
Andre Guedes | 4c87eaa | 2013-04-30 15:29:32 -0300 | [diff] [blame] | 3272 | err = hci_req_run(&req, le_scan_disable_work_complete); |
| 3273 | if (err) |
| 3274 | BT_ERR("Disable LE scanning request failed: err %d", err); |
Andre Guedes | 28b75a8 | 2012-02-03 17:48:00 -0300 | [diff] [blame] | 3275 | } |
| 3276 | |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 3277 | /* Alloc HCI device */ |
| 3278 | struct hci_dev *hci_alloc_dev(void) |
| 3279 | { |
| 3280 | struct hci_dev *hdev; |
| 3281 | |
| 3282 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); |
| 3283 | if (!hdev) |
| 3284 | return NULL; |
| 3285 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3286 | hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); |
| 3287 | hdev->esco_type = (ESCO_HV1); |
| 3288 | hdev->link_mode = (HCI_LM_ACCEPT); |
Marcel Holtmann | b4cb9fb | 2013-10-14 13:56:16 -0700 | [diff] [blame] | 3289 | hdev->num_iac = 0x01; /* One IAC support is mandatory */ |
| 3290 | hdev->io_capability = 0x03; /* No Input No Output */ |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 3291 | hdev->inq_tx_power = HCI_TX_POWER_INVALID; |
| 3292 | hdev->adv_tx_power = HCI_TX_POWER_INVALID; |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3293 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3294 | hdev->sniff_max_interval = 800; |
| 3295 | hdev->sniff_min_interval = 80; |
| 3296 | |
Marcel Holtmann | 3f959d4 | 2014-02-20 11:55:56 -0800 | [diff] [blame] | 3297 | hdev->le_adv_channel_map = 0x07; |
Marcel Holtmann | bef6473 | 2013-10-11 08:23:19 -0700 | [diff] [blame] | 3298 | hdev->le_scan_interval = 0x0060; |
| 3299 | hdev->le_scan_window = 0x0030; |
Marcel Holtmann | 4e70c7e | 2013-10-19 07:09:13 -0700 | [diff] [blame] | 3300 | hdev->le_conn_min_interval = 0x0028; |
| 3301 | hdev->le_conn_max_interval = 0x0038; |
Marcel Holtmann | bef6473 | 2013-10-11 08:23:19 -0700 | [diff] [blame] | 3302 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3303 | mutex_init(&hdev->lock); |
| 3304 | mutex_init(&hdev->req_lock); |
| 3305 | |
| 3306 | INIT_LIST_HEAD(&hdev->mgmt_pending); |
| 3307 | INIT_LIST_HEAD(&hdev->blacklist); |
| 3308 | INIT_LIST_HEAD(&hdev->uuids); |
| 3309 | INIT_LIST_HEAD(&hdev->link_keys); |
| 3310 | INIT_LIST_HEAD(&hdev->long_term_keys); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 3311 | INIT_LIST_HEAD(&hdev->identity_resolving_keys); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3312 | INIT_LIST_HEAD(&hdev->remote_oob_data); |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 3313 | INIT_LIST_HEAD(&hdev->le_conn_params); |
Andrei Emeltchenko | 6b536b5 | 2012-08-31 16:39:28 +0300 | [diff] [blame] | 3314 | INIT_LIST_HEAD(&hdev->conn_hash.list); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3315 | |
| 3316 | INIT_WORK(&hdev->rx_work, hci_rx_work); |
| 3317 | INIT_WORK(&hdev->cmd_work, hci_cmd_work); |
| 3318 | INIT_WORK(&hdev->tx_work, hci_tx_work); |
| 3319 | INIT_WORK(&hdev->power_on, hci_power_on); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3320 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3321 | INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); |
| 3322 | INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); |
| 3323 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); |
| 3324 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3325 | skb_queue_head_init(&hdev->rx_q); |
| 3326 | skb_queue_head_init(&hdev->cmd_q); |
| 3327 | skb_queue_head_init(&hdev->raw_q); |
| 3328 | |
| 3329 | init_waitqueue_head(&hdev->req_wait_q); |
| 3330 | |
Andrei Emeltchenko | bda4f23 | 2012-06-11 11:13:08 +0300 | [diff] [blame] | 3331 | setup_timer(&hdev->cmd_timer, hci_cmd_timeout, (unsigned long) hdev); |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3332 | |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3333 | hci_init_sysfs(hdev); |
| 3334 | discovery_init(hdev); |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 3335 | |
| 3336 | return hdev; |
| 3337 | } |
| 3338 | EXPORT_SYMBOL(hci_alloc_dev); |
| 3339 | |
| 3340 | /* Free HCI device */ |
| 3341 | void hci_free_dev(struct hci_dev *hdev) |
| 3342 | { |
David Herrmann | 9be0dab | 2012-04-22 14:39:57 +0200 | [diff] [blame] | 3343 | /* will free via device release */ |
| 3344 | put_device(&hdev->dev); |
| 3345 | } |
| 3346 | EXPORT_SYMBOL(hci_free_dev); |
| 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | /* Register HCI device */ |
| 3349 | int hci_register_dev(struct hci_dev *hdev) |
| 3350 | { |
David Herrmann | b1b813d | 2012-04-22 14:39:58 +0200 | [diff] [blame] | 3351 | int id, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | |
David Herrmann | 010666a | 2012-01-07 15:47:07 +0100 | [diff] [blame] | 3353 | if (!hdev->open || !hdev->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | return -EINVAL; |
| 3355 | |
Mat Martineau | 08add51 | 2011-11-02 16:18:36 -0700 | [diff] [blame] | 3356 | /* Do not allow HCI_AMP devices to register at index 0, |
| 3357 | * so the index can be used as the AMP controller ID. |
| 3358 | */ |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3359 | switch (hdev->dev_type) { |
| 3360 | case HCI_BREDR: |
| 3361 | id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); |
| 3362 | break; |
| 3363 | case HCI_AMP: |
| 3364 | id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); |
| 3365 | break; |
| 3366 | default: |
| 3367 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3369 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3370 | if (id < 0) |
| 3371 | return id; |
| 3372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | sprintf(hdev->name, "hci%d", id); |
| 3374 | hdev->id = id; |
Andrei Emeltchenko | 2d8b3a1 | 2012-04-16 16:32:04 +0300 | [diff] [blame] | 3375 | |
| 3376 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
| 3377 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 3378 | hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 3379 | WQ_MEM_RECLAIM, 1, hdev->name); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3380 | if (!hdev->workqueue) { |
| 3381 | error = -ENOMEM; |
| 3382 | goto err; |
| 3383 | } |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3384 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 3385 | hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | |
| 3386 | WQ_MEM_RECLAIM, 1, hdev->name); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 3387 | if (!hdev->req_workqueue) { |
| 3388 | destroy_workqueue(hdev->workqueue); |
| 3389 | error = -ENOMEM; |
| 3390 | goto err; |
| 3391 | } |
| 3392 | |
Marcel Holtmann | 0153e2e | 2013-10-17 17:24:17 -0700 | [diff] [blame] | 3393 | if (!IS_ERR_OR_NULL(bt_debugfs)) |
| 3394 | hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs); |
| 3395 | |
Marcel Holtmann | bdc3e0f | 2013-10-17 17:24:19 -0700 | [diff] [blame] | 3396 | dev_set_name(&hdev->dev, "%s", hdev->name); |
| 3397 | |
Johan Hedberg | 99780a7 | 2014-02-18 10:40:07 +0200 | [diff] [blame] | 3398 | hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, |
| 3399 | CRYPTO_ALG_ASYNC); |
| 3400 | if (IS_ERR(hdev->tfm_aes)) { |
| 3401 | BT_ERR("Unable to create crypto context"); |
| 3402 | error = PTR_ERR(hdev->tfm_aes); |
| 3403 | hdev->tfm_aes = NULL; |
| 3404 | goto err_wqueue; |
| 3405 | } |
| 3406 | |
Marcel Holtmann | bdc3e0f | 2013-10-17 17:24:19 -0700 | [diff] [blame] | 3407 | error = device_add(&hdev->dev); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3408 | if (error < 0) |
Johan Hedberg | 99780a7 | 2014-02-18 10:40:07 +0200 | [diff] [blame] | 3409 | goto err_tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 3411 | hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3412 | RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, |
| 3413 | hdev); |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 3414 | if (hdev->rfkill) { |
| 3415 | if (rfkill_register(hdev->rfkill) < 0) { |
| 3416 | rfkill_destroy(hdev->rfkill); |
| 3417 | hdev->rfkill = NULL; |
| 3418 | } |
| 3419 | } |
| 3420 | |
Johan Hedberg | 5e13036 | 2013-09-13 08:58:17 +0300 | [diff] [blame] | 3421 | if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) |
| 3422 | set_bit(HCI_RFKILLED, &hdev->dev_flags); |
| 3423 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 3424 | set_bit(HCI_SETUP, &hdev->dev_flags); |
Marcel Holtmann | 004b025 | 2013-10-07 00:58:32 -0700 | [diff] [blame] | 3425 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 3426 | |
Marcel Holtmann | 01cd340 | 2013-10-06 01:16:22 -0700 | [diff] [blame] | 3427 | if (hdev->dev_type == HCI_BREDR) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3428 | /* Assume BR/EDR support until proven otherwise (such as |
| 3429 | * through reading supported features during init. |
| 3430 | */ |
| 3431 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 3432 | } |
Andrei Emeltchenko | ce2be9a | 2012-06-29 15:07:00 +0300 | [diff] [blame] | 3433 | |
Gustavo Padovan | fcee337 | 2013-07-11 11:34:28 +0100 | [diff] [blame] | 3434 | write_lock(&hci_dev_list_lock); |
| 3435 | list_add(&hdev->list, &hci_dev_list); |
| 3436 | write_unlock(&hci_dev_list_lock); |
| 3437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | hci_notify(hdev, HCI_DEV_REG); |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 3439 | hci_dev_hold(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 3441 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Marcel Holtmann | fbe96d6 | 2012-10-30 01:35:40 -0700 | [diff] [blame] | 3442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | return id; |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3444 | |
Johan Hedberg | 99780a7 | 2014-02-18 10:40:07 +0200 | [diff] [blame] | 3445 | err_tfm: |
| 3446 | crypto_free_blkcipher(hdev->tfm_aes); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3447 | err_wqueue: |
| 3448 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 3449 | destroy_workqueue(hdev->req_workqueue); |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3450 | err: |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3451 | ida_simple_remove(&hci_index_ida, hdev->id); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3452 | |
David Herrmann | 33ca954 | 2011-10-08 14:58:49 +0200 | [diff] [blame] | 3453 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | } |
| 3455 | EXPORT_SYMBOL(hci_register_dev); |
| 3456 | |
| 3457 | /* Unregister HCI device */ |
David Herrmann | 5973563 | 2011-10-26 10:43:19 +0200 | [diff] [blame] | 3458 | void hci_unregister_dev(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | { |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3460 | int i, id; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3461 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 3462 | BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | |
Johan Hovold | 9432496 | 2012-03-15 14:48:41 +0100 | [diff] [blame] | 3464 | set_bit(HCI_UNREGISTER, &hdev->dev_flags); |
| 3465 | |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3466 | id = hdev->id; |
| 3467 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3468 | write_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | list_del(&hdev->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3470 | write_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | |
| 3472 | hci_dev_do_close(hdev); |
| 3473 | |
Suraj Sumangala | cd4c539 | 2010-07-14 13:02:16 +0530 | [diff] [blame] | 3474 | for (i = 0; i < NUM_REASSEMBLY; i++) |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3475 | kfree_skb(hdev->reassembly[i]); |
| 3476 | |
Gustavo Padovan | b9b5ef1 | 2012-11-21 00:50:21 -0200 | [diff] [blame] | 3477 | cancel_work_sync(&hdev->power_on); |
| 3478 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 3479 | if (!test_bit(HCI_INIT, &hdev->flags) && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3480 | !test_bit(HCI_SETUP, &hdev->dev_flags)) { |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3481 | hci_dev_lock(hdev); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3482 | mgmt_index_removed(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3483 | hci_dev_unlock(hdev); |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3484 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 3485 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3486 | /* mgmt_index_removed should take care of emptying the |
| 3487 | * pending list */ |
| 3488 | BUG_ON(!list_empty(&hdev->mgmt_pending)); |
| 3489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | hci_notify(hdev, HCI_DEV_UNREG); |
| 3491 | |
Marcel Holtmann | 611b30f | 2009-06-08 14:41:38 +0200 | [diff] [blame] | 3492 | if (hdev->rfkill) { |
| 3493 | rfkill_unregister(hdev->rfkill); |
| 3494 | rfkill_destroy(hdev->rfkill); |
| 3495 | } |
| 3496 | |
Johan Hedberg | 99780a7 | 2014-02-18 10:40:07 +0200 | [diff] [blame] | 3497 | if (hdev->tfm_aes) |
| 3498 | crypto_free_blkcipher(hdev->tfm_aes); |
| 3499 | |
Marcel Holtmann | bdc3e0f | 2013-10-17 17:24:19 -0700 | [diff] [blame] | 3500 | device_del(&hdev->dev); |
Dave Young | 147e2d5 | 2008-03-05 18:45:59 -0800 | [diff] [blame] | 3501 | |
Marcel Holtmann | 0153e2e | 2013-10-17 17:24:17 -0700 | [diff] [blame] | 3502 | debugfs_remove_recursive(hdev->debugfs); |
| 3503 | |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3504 | destroy_workqueue(hdev->workqueue); |
Johan Hedberg | 6ead1bb | 2013-01-14 22:33:50 +0200 | [diff] [blame] | 3505 | destroy_workqueue(hdev->req_workqueue); |
Marcel Holtmann | f48fd9c | 2010-03-20 15:20:04 +0100 | [diff] [blame] | 3506 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3507 | hci_dev_lock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 3508 | hci_blacklist_clear(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 3509 | hci_uuids_clear(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 3510 | hci_link_keys_clear(hdev); |
Vinicius Costa Gomes | b899efa | 2012-02-02 21:08:00 -0300 | [diff] [blame] | 3511 | hci_smp_ltks_clear(hdev); |
Johan Hedberg | 970c4e4 | 2014-02-18 10:19:33 +0200 | [diff] [blame] | 3512 | hci_smp_irks_clear(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3513 | hci_remote_oob_data_clear(hdev); |
Andre Guedes | 15819a7 | 2014-02-03 13:56:18 -0300 | [diff] [blame] | 3514 | hci_conn_params_clear(hdev); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3515 | hci_dev_unlock(hdev); |
Johan Hedberg | e2e0cac | 2011-01-04 12:08:50 +0200 | [diff] [blame] | 3516 | |
David Herrmann | dc946bd | 2012-01-07 15:47:24 +0100 | [diff] [blame] | 3517 | hci_dev_put(hdev); |
Sasha Levin | 3df92b3 | 2012-05-27 22:36:56 +0200 | [diff] [blame] | 3518 | |
| 3519 | ida_simple_remove(&hci_index_ida, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | } |
| 3521 | EXPORT_SYMBOL(hci_unregister_dev); |
| 3522 | |
| 3523 | /* Suspend HCI device */ |
| 3524 | int hci_suspend_dev(struct hci_dev *hdev) |
| 3525 | { |
| 3526 | hci_notify(hdev, HCI_DEV_SUSPEND); |
| 3527 | return 0; |
| 3528 | } |
| 3529 | EXPORT_SYMBOL(hci_suspend_dev); |
| 3530 | |
| 3531 | /* Resume HCI device */ |
| 3532 | int hci_resume_dev(struct hci_dev *hdev) |
| 3533 | { |
| 3534 | hci_notify(hdev, HCI_DEV_RESUME); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | EXPORT_SYMBOL(hci_resume_dev); |
| 3538 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3539 | /* Receive frame from HCI drivers */ |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 3540 | int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3541 | { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3542 | if (!hdev || (!test_bit(HCI_UP, &hdev->flags) |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3543 | && !test_bit(HCI_INIT, &hdev->flags))) { |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3544 | kfree_skb(skb); |
| 3545 | return -ENXIO; |
| 3546 | } |
| 3547 | |
Jorrit Schippers | d82603c | 2012-12-27 17:33:02 +0100 | [diff] [blame] | 3548 | /* Incoming skb */ |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3549 | bt_cb(skb)->incoming = 1; |
| 3550 | |
| 3551 | /* Time stamp */ |
| 3552 | __net_timestamp(skb); |
| 3553 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3554 | skb_queue_tail(&hdev->rx_q, skb); |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 3555 | queue_work(hdev->workqueue, &hdev->rx_work); |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 3556 | |
Marcel Holtmann | 76bca88 | 2009-11-18 00:40:39 +0100 | [diff] [blame] | 3557 | return 0; |
| 3558 | } |
| 3559 | EXPORT_SYMBOL(hci_recv_frame); |
| 3560 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3561 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3562 | int count, __u8 index) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3563 | { |
| 3564 | int len = 0; |
| 3565 | int hlen = 0; |
| 3566 | int remain = count; |
| 3567 | struct sk_buff *skb; |
| 3568 | struct bt_skb_cb *scb; |
| 3569 | |
| 3570 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3571 | index >= NUM_REASSEMBLY) |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3572 | return -EILSEQ; |
| 3573 | |
| 3574 | skb = hdev->reassembly[index]; |
| 3575 | |
| 3576 | if (!skb) { |
| 3577 | switch (type) { |
| 3578 | case HCI_ACLDATA_PKT: |
| 3579 | len = HCI_MAX_FRAME_SIZE; |
| 3580 | hlen = HCI_ACL_HDR_SIZE; |
| 3581 | break; |
| 3582 | case HCI_EVENT_PKT: |
| 3583 | len = HCI_MAX_EVENT_SIZE; |
| 3584 | hlen = HCI_EVENT_HDR_SIZE; |
| 3585 | break; |
| 3586 | case HCI_SCODATA_PKT: |
| 3587 | len = HCI_MAX_SCO_SIZE; |
| 3588 | hlen = HCI_SCO_HDR_SIZE; |
| 3589 | break; |
| 3590 | } |
| 3591 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 3592 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3593 | if (!skb) |
| 3594 | return -ENOMEM; |
| 3595 | |
| 3596 | scb = (void *) skb->cb; |
| 3597 | scb->expect = hlen; |
| 3598 | scb->pkt_type = type; |
| 3599 | |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3600 | hdev->reassembly[index] = skb; |
| 3601 | } |
| 3602 | |
| 3603 | while (count) { |
| 3604 | scb = (void *) skb->cb; |
Dan Carpenter | 89bb46d | 2012-02-28 09:57:59 +0300 | [diff] [blame] | 3605 | len = min_t(uint, scb->expect, count); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3606 | |
| 3607 | memcpy(skb_put(skb, len), data, len); |
| 3608 | |
| 3609 | count -= len; |
| 3610 | data += len; |
| 3611 | scb->expect -= len; |
| 3612 | remain = count; |
| 3613 | |
| 3614 | switch (type) { |
| 3615 | case HCI_EVENT_PKT: |
| 3616 | if (skb->len == HCI_EVENT_HDR_SIZE) { |
| 3617 | struct hci_event_hdr *h = hci_event_hdr(skb); |
| 3618 | scb->expect = h->plen; |
| 3619 | |
| 3620 | if (skb_tailroom(skb) < scb->expect) { |
| 3621 | kfree_skb(skb); |
| 3622 | hdev->reassembly[index] = NULL; |
| 3623 | return -ENOMEM; |
| 3624 | } |
| 3625 | } |
| 3626 | break; |
| 3627 | |
| 3628 | case HCI_ACLDATA_PKT: |
| 3629 | if (skb->len == HCI_ACL_HDR_SIZE) { |
| 3630 | struct hci_acl_hdr *h = hci_acl_hdr(skb); |
| 3631 | scb->expect = __le16_to_cpu(h->dlen); |
| 3632 | |
| 3633 | if (skb_tailroom(skb) < scb->expect) { |
| 3634 | kfree_skb(skb); |
| 3635 | hdev->reassembly[index] = NULL; |
| 3636 | return -ENOMEM; |
| 3637 | } |
| 3638 | } |
| 3639 | break; |
| 3640 | |
| 3641 | case HCI_SCODATA_PKT: |
| 3642 | if (skb->len == HCI_SCO_HDR_SIZE) { |
| 3643 | struct hci_sco_hdr *h = hci_sco_hdr(skb); |
| 3644 | scb->expect = h->dlen; |
| 3645 | |
| 3646 | if (skb_tailroom(skb) < scb->expect) { |
| 3647 | kfree_skb(skb); |
| 3648 | hdev->reassembly[index] = NULL; |
| 3649 | return -ENOMEM; |
| 3650 | } |
| 3651 | } |
| 3652 | break; |
| 3653 | } |
| 3654 | |
| 3655 | if (scb->expect == 0) { |
| 3656 | /* Complete frame */ |
| 3657 | |
| 3658 | bt_cb(skb)->pkt_type = type; |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 3659 | hci_recv_frame(hdev, skb); |
Suraj Sumangala | 33e882a | 2010-07-14 13:02:17 +0530 | [diff] [blame] | 3660 | |
| 3661 | hdev->reassembly[index] = NULL; |
| 3662 | return remain; |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | return remain; |
| 3667 | } |
| 3668 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3669 | int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) |
| 3670 | { |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 3671 | int rem = 0; |
| 3672 | |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3673 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) |
| 3674 | return -EILSEQ; |
| 3675 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 3676 | while (count) { |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 3677 | rem = hci_reassembly(hdev, type, data, count, type - 1); |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 3678 | if (rem < 0) |
| 3679 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3680 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 3681 | data += (count - rem); |
| 3682 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 3683 | } |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3684 | |
Suraj Sumangala | f39a3c0 | 2010-07-14 13:02:18 +0530 | [diff] [blame] | 3685 | return rem; |
Marcel Holtmann | ef22201 | 2007-07-11 06:42:04 +0200 | [diff] [blame] | 3686 | } |
| 3687 | EXPORT_SYMBOL(hci_recv_fragment); |
| 3688 | |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 3689 | #define STREAM_REASSEMBLY 0 |
| 3690 | |
| 3691 | int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) |
| 3692 | { |
| 3693 | int type; |
| 3694 | int rem = 0; |
| 3695 | |
Gustavo F. Padovan | da5f6c3 | 2010-07-24 01:34:54 -0300 | [diff] [blame] | 3696 | while (count) { |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 3697 | struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; |
| 3698 | |
| 3699 | if (!skb) { |
| 3700 | struct { char type; } *pkt; |
| 3701 | |
| 3702 | /* Start of the frame */ |
| 3703 | pkt = data; |
| 3704 | type = pkt->type; |
| 3705 | |
| 3706 | data++; |
| 3707 | count--; |
| 3708 | } else |
| 3709 | type = bt_cb(skb)->pkt_type; |
| 3710 | |
Gustavo F. Padovan | 1e429f3 | 2011-04-04 18:25:14 -0300 | [diff] [blame] | 3711 | rem = hci_reassembly(hdev, type, data, count, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 3712 | STREAM_REASSEMBLY); |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 3713 | if (rem < 0) |
| 3714 | return rem; |
| 3715 | |
| 3716 | data += (count - rem); |
| 3717 | count = rem; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 3718 | } |
Suraj Sumangala | 9981151 | 2010-07-14 13:02:19 +0530 | [diff] [blame] | 3719 | |
| 3720 | return rem; |
| 3721 | } |
| 3722 | EXPORT_SYMBOL(hci_recv_stream_fragment); |
| 3723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | /* ---- Interface to upper protocols ---- */ |
| 3725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | int hci_register_cb(struct hci_cb *cb) |
| 3727 | { |
| 3728 | BT_DBG("%p name %s", cb, cb->name); |
| 3729 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3730 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | list_add(&cb->list, &hci_cb_list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3732 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | |
| 3734 | return 0; |
| 3735 | } |
| 3736 | EXPORT_SYMBOL(hci_register_cb); |
| 3737 | |
| 3738 | int hci_unregister_cb(struct hci_cb *cb) |
| 3739 | { |
| 3740 | BT_DBG("%p name %s", cb, cb->name); |
| 3741 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3742 | write_lock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | list_del(&cb->list); |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 3744 | write_unlock(&hci_cb_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | |
| 3746 | return 0; |
| 3747 | } |
| 3748 | EXPORT_SYMBOL(hci_unregister_cb); |
| 3749 | |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 3750 | 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] | 3751 | { |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3752 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3754 | /* Time stamp */ |
| 3755 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 3757 | /* Send copy to monitor */ |
| 3758 | hci_send_to_monitor(hdev, skb); |
| 3759 | |
| 3760 | if (atomic_read(&hdev->promisc)) { |
| 3761 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 3762 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 3766 | skb_orphan(skb); |
| 3767 | |
Marcel Holtmann | 7bd8f09 | 2013-10-11 06:19:18 -0700 | [diff] [blame] | 3768 | if (hdev->send(hdev, skb) < 0) |
Marcel Holtmann | 5108699 | 2013-10-10 14:54:19 -0700 | [diff] [blame] | 3769 | BT_ERR("%s sending frame failed", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 3772 | void hci_req_init(struct hci_request *req, struct hci_dev *hdev) |
| 3773 | { |
| 3774 | skb_queue_head_init(&req->cmd_q); |
| 3775 | req->hdev = hdev; |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 3776 | req->err = 0; |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | int hci_req_run(struct hci_request *req, hci_req_complete_t complete) |
| 3780 | { |
| 3781 | struct hci_dev *hdev = req->hdev; |
| 3782 | struct sk_buff *skb; |
| 3783 | unsigned long flags; |
| 3784 | |
| 3785 | BT_DBG("length %u", skb_queue_len(&req->cmd_q)); |
| 3786 | |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 3787 | /* If an error occured during request building, remove all HCI |
| 3788 | * commands queued on the HCI request queue. |
| 3789 | */ |
| 3790 | if (req->err) { |
| 3791 | skb_queue_purge(&req->cmd_q); |
| 3792 | return req->err; |
| 3793 | } |
| 3794 | |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 3795 | /* Do not allow empty requests */ |
| 3796 | if (skb_queue_empty(&req->cmd_q)) |
Andre Guedes | 382b0c3 | 2013-03-08 11:20:14 -0300 | [diff] [blame] | 3797 | return -ENODATA; |
Johan Hedberg | 3119ae9 | 2013-03-05 20:37:44 +0200 | [diff] [blame] | 3798 | |
| 3799 | skb = skb_peek_tail(&req->cmd_q); |
| 3800 | bt_cb(skb)->req.complete = complete; |
| 3801 | |
| 3802 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 3803 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); |
| 3804 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 3805 | |
| 3806 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 3807 | |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 3811 | static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 3812 | u32 plen, const void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3813 | { |
| 3814 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 3815 | struct hci_command_hdr *hdr; |
| 3816 | struct sk_buff *skb; |
| 3817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 3819 | if (!skb) |
| 3820 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | |
| 3822 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 3823 | hdr->opcode = cpu_to_le16(opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | hdr->plen = plen; |
| 3825 | |
| 3826 | if (plen) |
| 3827 | memcpy(skb_put(skb, plen), param, plen); |
| 3828 | |
| 3829 | BT_DBG("skb len %d", skb->len); |
| 3830 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3831 | bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 3832 | |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 3833 | return skb; |
| 3834 | } |
| 3835 | |
| 3836 | /* Send HCI command */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 3837 | int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, |
| 3838 | const void *param) |
Johan Hedberg | 1ca3a9d | 2013-03-05 20:37:45 +0200 | [diff] [blame] | 3839 | { |
| 3840 | struct sk_buff *skb; |
| 3841 | |
| 3842 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 3843 | |
| 3844 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 3845 | if (!skb) { |
| 3846 | BT_ERR("%s no memory for command", hdev->name); |
| 3847 | return -ENOMEM; |
| 3848 | } |
| 3849 | |
Johan Hedberg | 11714b3 | 2013-03-05 20:37:47 +0200 | [diff] [blame] | 3850 | /* Stand-alone HCI commands must be flaged as |
| 3851 | * single-command requests. |
| 3852 | */ |
| 3853 | bt_cb(skb)->req.start = true; |
| 3854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 3856 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | |
| 3858 | return 0; |
| 3859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 3861 | /* Queue a command to an asynchronous HCI request */ |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 3862 | void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, |
| 3863 | const void *param, u8 event) |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 3864 | { |
| 3865 | struct hci_dev *hdev = req->hdev; |
| 3866 | struct sk_buff *skb; |
| 3867 | |
| 3868 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 3869 | |
Andre Guedes | 34739c1 | 2013-03-08 11:20:18 -0300 | [diff] [blame] | 3870 | /* If an error occured during request building, there is no point in |
| 3871 | * queueing the HCI command. We can simply return. |
| 3872 | */ |
| 3873 | if (req->err) |
| 3874 | return; |
| 3875 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 3876 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 3877 | if (!skb) { |
Andre Guedes | 5d73e03 | 2013-03-08 11:20:16 -0300 | [diff] [blame] | 3878 | BT_ERR("%s no memory for command (opcode 0x%4.4x)", |
| 3879 | hdev->name, opcode); |
| 3880 | req->err = -ENOMEM; |
Andre Guedes | e348fe6 | 2013-03-08 11:20:17 -0300 | [diff] [blame] | 3881 | return; |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | if (skb_queue_empty(&req->cmd_q)) |
| 3885 | bt_cb(skb)->req.start = true; |
| 3886 | |
Johan Hedberg | 02350a7 | 2013-04-03 21:50:29 +0300 | [diff] [blame] | 3887 | bt_cb(skb)->req.event = event; |
| 3888 | |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 3889 | skb_queue_tail(&req->cmd_q, skb); |
Johan Hedberg | 71c76a1 | 2013-03-05 20:37:46 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
Johan Hedberg | 07dc93d | 2013-04-19 10:14:51 +0300 | [diff] [blame] | 3892 | void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, |
| 3893 | const void *param) |
Johan Hedberg | 02350a7 | 2013-04-03 21:50:29 +0300 | [diff] [blame] | 3894 | { |
| 3895 | hci_req_add_ev(req, opcode, plen, param, 0); |
| 3896 | } |
| 3897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | /* Get data from the previously sent command */ |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 3899 | void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | { |
| 3901 | struct hci_command_hdr *hdr; |
| 3902 | |
| 3903 | if (!hdev->sent_cmd) |
| 3904 | return NULL; |
| 3905 | |
| 3906 | hdr = (void *) hdev->sent_cmd->data; |
| 3907 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 3908 | if (hdr->opcode != cpu_to_le16(opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3909 | return NULL; |
| 3910 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3911 | BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | |
| 3913 | return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; |
| 3914 | } |
| 3915 | |
| 3916 | /* Send ACL data */ |
| 3917 | static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) |
| 3918 | { |
| 3919 | struct hci_acl_hdr *hdr; |
| 3920 | int len = skb->len; |
| 3921 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 3922 | skb_push(skb, HCI_ACL_HDR_SIZE); |
| 3923 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 3924 | hdr = (struct hci_acl_hdr *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3925 | hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); |
| 3926 | hdr->dlen = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | } |
| 3928 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3929 | 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] | 3930 | struct sk_buff *skb, __u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3932 | struct hci_conn *conn = chan->conn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | struct hci_dev *hdev = conn->hdev; |
| 3934 | struct sk_buff *list; |
| 3935 | |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 3936 | skb->len = skb_headlen(skb); |
| 3937 | skb->data_len = 0; |
| 3938 | |
| 3939 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | 204a6e5 | 2012-10-15 11:58:39 +0300 | [diff] [blame] | 3940 | |
| 3941 | switch (hdev->dev_type) { |
| 3942 | case HCI_BREDR: |
| 3943 | hci_add_acl_hdr(skb, conn->handle, flags); |
| 3944 | break; |
| 3945 | case HCI_AMP: |
| 3946 | hci_add_acl_hdr(skb, chan->handle, flags); |
| 3947 | break; |
| 3948 | default: |
| 3949 | BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); |
| 3950 | return; |
| 3951 | } |
Gustavo Padovan | 087bfd9 | 2012-05-11 13:16:11 -0300 | [diff] [blame] | 3952 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 3953 | list = skb_shinfo(skb)->frag_list; |
| 3954 | if (!list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | /* Non fragmented */ |
| 3956 | BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); |
| 3957 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3958 | skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | } else { |
| 3960 | /* Fragmented */ |
| 3961 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 3962 | |
| 3963 | skb_shinfo(skb)->frag_list = NULL; |
| 3964 | |
| 3965 | /* Queue all fragments atomically */ |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 3966 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3968 | __skb_queue_tail(queue, skb); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 3969 | |
| 3970 | flags &= ~ACL_START; |
| 3971 | flags |= ACL_CONT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | do { |
| 3973 | skb = list; list = list->next; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3974 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 3975 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 3976 | hci_add_acl_hdr(skb, conn->handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | |
| 3978 | BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); |
| 3979 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3980 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | } while (list); |
| 3982 | |
Gustavo F. Padovan | af3e635 | 2011-12-22 16:35:05 -0200 | [diff] [blame] | 3983 | spin_unlock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) |
| 3988 | { |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3989 | struct hci_dev *hdev = chan->conn->hdev; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 3990 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 3991 | 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] | 3992 | |
Andrei Emeltchenko | ee22be7 | 2012-09-21 12:30:04 +0300 | [diff] [blame] | 3993 | hci_queue_acl(chan, &chan->data_q, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 3995 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | |
| 3998 | /* Send SCO data */ |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 3999 | void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | { |
| 4001 | struct hci_dev *hdev = conn->hdev; |
| 4002 | struct hci_sco_hdr hdr; |
| 4003 | |
| 4004 | BT_DBG("%s len %d", hdev->name, skb->len); |
| 4005 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 4006 | hdr.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | hdr.dlen = skb->len; |
| 4008 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 4009 | skb_push(skb, HCI_SCO_HDR_SIZE); |
| 4010 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 4011 | memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 4013 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; |
Marcel Holtmann | c78ae28 | 2009-11-18 01:02:54 +0100 | [diff] [blame] | 4014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | skb_queue_tail(&conn->data_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 4016 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | |
| 4019 | /* ---- HCI TX task (outgoing data) ---- */ |
| 4020 | |
| 4021 | /* HCI Connection scheduler */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4022 | static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, |
| 4023 | int *quote) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | { |
| 4025 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 4026 | struct hci_conn *conn = NULL, *c; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 4027 | unsigned int num = 0, min = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4029 | /* We don't have to lock device here. Connections are always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | * added and removed with TX task disabled. */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4031 | |
| 4032 | rcu_read_lock(); |
| 4033 | |
| 4034 | list_for_each_entry_rcu(c, &h->list, list) { |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 4035 | if (c->type != type || skb_queue_empty(&c->data_q)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4036 | continue; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 4037 | |
| 4038 | if (c->state != BT_CONNECTED && c->state != BT_CONFIG) |
| 4039 | continue; |
| 4040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | num++; |
| 4042 | |
| 4043 | if (c->sent < min) { |
| 4044 | min = c->sent; |
| 4045 | conn = c; |
| 4046 | } |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 4047 | |
| 4048 | if (hci_conn_num(hdev, type) == num) |
| 4049 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | } |
| 4051 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4052 | rcu_read_unlock(); |
| 4053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | if (conn) { |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4055 | int cnt, q; |
| 4056 | |
| 4057 | switch (conn->type) { |
| 4058 | case ACL_LINK: |
| 4059 | cnt = hdev->acl_cnt; |
| 4060 | break; |
| 4061 | case SCO_LINK: |
| 4062 | case ESCO_LINK: |
| 4063 | cnt = hdev->sco_cnt; |
| 4064 | break; |
| 4065 | case LE_LINK: |
| 4066 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 4067 | break; |
| 4068 | default: |
| 4069 | cnt = 0; |
| 4070 | BT_ERR("Unknown link type"); |
| 4071 | } |
| 4072 | |
| 4073 | q = cnt / num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | *quote = q ? q : 1; |
| 4075 | } else |
| 4076 | *quote = 0; |
| 4077 | |
| 4078 | BT_DBG("conn %p quote %d", conn, *quote); |
| 4079 | return conn; |
| 4080 | } |
| 4081 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4082 | static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | { |
| 4084 | struct hci_conn_hash *h = &hdev->conn_hash; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 4085 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 4087 | BT_ERR("%s link tx timeout", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4089 | rcu_read_lock(); |
| 4090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | /* Kill stalled connections */ |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4092 | list_for_each_entry_rcu(c, &h->list, list) { |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 4093 | if (c->type == type && c->sent) { |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 4094 | BT_ERR("%s killing stalled connection %pMR", |
| 4095 | hdev->name, &c->dst); |
Andre Guedes | bed7174 | 2013-01-30 11:50:56 -0300 | [diff] [blame] | 4096 | hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | } |
| 4098 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4099 | |
| 4100 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4103 | static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, |
| 4104 | int *quote) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4105 | { |
| 4106 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 4107 | struct hci_chan *chan = NULL; |
Mikel Astiz | abc5de8 | 2012-04-11 08:48:47 +0200 | [diff] [blame] | 4108 | unsigned int num = 0, min = ~0, cur_prio = 0; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4109 | struct hci_conn *conn; |
| 4110 | int cnt, q, conn_num = 0; |
| 4111 | |
| 4112 | BT_DBG("%s", hdev->name); |
| 4113 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4114 | rcu_read_lock(); |
| 4115 | |
| 4116 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4117 | struct hci_chan *tmp; |
| 4118 | |
| 4119 | if (conn->type != type) |
| 4120 | continue; |
| 4121 | |
| 4122 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 4123 | continue; |
| 4124 | |
| 4125 | conn_num++; |
| 4126 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 4127 | list_for_each_entry_rcu(tmp, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4128 | struct sk_buff *skb; |
| 4129 | |
| 4130 | if (skb_queue_empty(&tmp->data_q)) |
| 4131 | continue; |
| 4132 | |
| 4133 | skb = skb_peek(&tmp->data_q); |
| 4134 | if (skb->priority < cur_prio) |
| 4135 | continue; |
| 4136 | |
| 4137 | if (skb->priority > cur_prio) { |
| 4138 | num = 0; |
| 4139 | min = ~0; |
| 4140 | cur_prio = skb->priority; |
| 4141 | } |
| 4142 | |
| 4143 | num++; |
| 4144 | |
| 4145 | if (conn->sent < min) { |
| 4146 | min = conn->sent; |
| 4147 | chan = tmp; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | if (hci_conn_num(hdev, type) == conn_num) |
| 4152 | break; |
| 4153 | } |
| 4154 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4155 | rcu_read_unlock(); |
| 4156 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4157 | if (!chan) |
| 4158 | return NULL; |
| 4159 | |
| 4160 | switch (chan->conn->type) { |
| 4161 | case ACL_LINK: |
| 4162 | cnt = hdev->acl_cnt; |
| 4163 | break; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 4164 | case AMP_LINK: |
| 4165 | cnt = hdev->block_cnt; |
| 4166 | break; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4167 | case SCO_LINK: |
| 4168 | case ESCO_LINK: |
| 4169 | cnt = hdev->sco_cnt; |
| 4170 | break; |
| 4171 | case LE_LINK: |
| 4172 | cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; |
| 4173 | break; |
| 4174 | default: |
| 4175 | cnt = 0; |
| 4176 | BT_ERR("Unknown link type"); |
| 4177 | } |
| 4178 | |
| 4179 | q = cnt / num; |
| 4180 | *quote = q ? q : 1; |
| 4181 | BT_DBG("chan %p quote %d", chan, *quote); |
| 4182 | return chan; |
| 4183 | } |
| 4184 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4185 | static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) |
| 4186 | { |
| 4187 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 4188 | struct hci_conn *conn; |
| 4189 | int num = 0; |
| 4190 | |
| 4191 | BT_DBG("%s", hdev->name); |
| 4192 | |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4193 | rcu_read_lock(); |
| 4194 | |
| 4195 | list_for_each_entry_rcu(conn, &h->list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4196 | struct hci_chan *chan; |
| 4197 | |
| 4198 | if (conn->type != type) |
| 4199 | continue; |
| 4200 | |
| 4201 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 4202 | continue; |
| 4203 | |
| 4204 | num++; |
| 4205 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 4206 | list_for_each_entry_rcu(chan, &conn->chan_list, list) { |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4207 | struct sk_buff *skb; |
| 4208 | |
| 4209 | if (chan->sent) { |
| 4210 | chan->sent = 0; |
| 4211 | continue; |
| 4212 | } |
| 4213 | |
| 4214 | if (skb_queue_empty(&chan->data_q)) |
| 4215 | continue; |
| 4216 | |
| 4217 | skb = skb_peek(&chan->data_q); |
| 4218 | if (skb->priority >= HCI_PRIO_MAX - 1) |
| 4219 | continue; |
| 4220 | |
| 4221 | skb->priority = HCI_PRIO_MAX - 1; |
| 4222 | |
| 4223 | BT_DBG("chan %p skb %p promoted to %d", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4224 | skb->priority); |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | if (hci_conn_num(hdev, type) == num) |
| 4228 | break; |
| 4229 | } |
Gustavo F. Padovan | bf4c632 | 2011-12-14 22:54:12 -0200 | [diff] [blame] | 4230 | |
| 4231 | rcu_read_unlock(); |
| 4232 | |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4233 | } |
| 4234 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4235 | static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) |
| 4236 | { |
| 4237 | /* Calculate count of blocks used by this packet */ |
| 4238 | return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); |
| 4239 | } |
| 4240 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4241 | static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 4244 | /* ACL tx timeout must be longer than maximum |
| 4245 | * link supervision timeout (40.9 seconds) */ |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 4246 | if (!cnt && time_after(jiffies, hdev->acl_last_tx + |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 4247 | HCI_ACL_TX_TIMEOUT)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 4248 | hci_link_tx_to(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | } |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 4250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4252 | static void hci_sched_acl_pkt(struct hci_dev *hdev) |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 4253 | { |
| 4254 | unsigned int cnt = hdev->acl_cnt; |
| 4255 | struct hci_chan *chan; |
| 4256 | struct sk_buff *skb; |
| 4257 | int quote; |
| 4258 | |
| 4259 | __check_timeout(hdev, cnt); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 4260 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4261 | while (hdev->acl_cnt && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4262 | (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 4263 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 4264 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4265 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4266 | skb->len, skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4267 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 4268 | /* Stop if priority has changed */ |
| 4269 | if (skb->priority < priority) |
| 4270 | break; |
| 4271 | |
| 4272 | skb = skb_dequeue(&chan->data_q); |
| 4273 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4274 | hci_conn_enter_active_mode(chan->conn, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4275 | bt_cb(skb)->force_active); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 4276 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4277 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | hdev->acl_last_tx = jiffies; |
| 4279 | |
| 4280 | hdev->acl_cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4281 | chan->sent++; |
| 4282 | chan->conn->sent++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | } |
| 4284 | } |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4285 | |
| 4286 | if (cnt != hdev->acl_cnt) |
| 4287 | hci_prio_recalculate(hdev, ACL_LINK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4290 | static void hci_sched_acl_blk(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4291 | { |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 4292 | unsigned int cnt = hdev->block_cnt; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4293 | struct hci_chan *chan; |
| 4294 | struct sk_buff *skb; |
| 4295 | int quote; |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 4296 | u8 type; |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4297 | |
Andrei Emeltchenko | 63d2bc1 | 2012-02-03 16:27:55 +0200 | [diff] [blame] | 4298 | __check_timeout(hdev, cnt); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4299 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 4300 | BT_DBG("%s", hdev->name); |
| 4301 | |
| 4302 | if (hdev->dev_type == HCI_AMP) |
| 4303 | type = AMP_LINK; |
| 4304 | else |
| 4305 | type = ACL_LINK; |
| 4306 | |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4307 | while (hdev->block_cnt > 0 && |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 4308 | (chan = hci_chan_sent(hdev, type, "e))) { |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4309 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 4310 | while (quote > 0 && (skb = skb_peek(&chan->data_q))) { |
| 4311 | int blocks; |
| 4312 | |
| 4313 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4314 | skb->len, skb->priority); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4315 | |
| 4316 | /* Stop if priority has changed */ |
| 4317 | if (skb->priority < priority) |
| 4318 | break; |
| 4319 | |
| 4320 | skb = skb_dequeue(&chan->data_q); |
| 4321 | |
| 4322 | blocks = __get_blocks(hdev, skb); |
| 4323 | if (blocks > hdev->block_cnt) |
| 4324 | return; |
| 4325 | |
| 4326 | hci_conn_enter_active_mode(chan->conn, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4327 | bt_cb(skb)->force_active); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4328 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4329 | hci_send_frame(hdev, skb); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4330 | hdev->acl_last_tx = jiffies; |
| 4331 | |
| 4332 | hdev->block_cnt -= blocks; |
| 4333 | quote -= blocks; |
| 4334 | |
| 4335 | chan->sent += blocks; |
| 4336 | chan->conn->sent += blocks; |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | if (cnt != hdev->block_cnt) |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 4341 | hci_prio_recalculate(hdev, type); |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4342 | } |
| 4343 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4344 | static void hci_sched_acl(struct hci_dev *hdev) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4345 | { |
| 4346 | BT_DBG("%s", hdev->name); |
| 4347 | |
Andrei Emeltchenko | bd1eb66 | 2012-10-10 17:38:30 +0300 | [diff] [blame] | 4348 | /* No ACL link over BR/EDR controller */ |
| 4349 | if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) |
| 4350 | return; |
| 4351 | |
| 4352 | /* No AMP link over AMP controller */ |
| 4353 | if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) |
Andrei Emeltchenko | b71d385 | 2012-02-03 16:27:54 +0200 | [diff] [blame] | 4354 | return; |
| 4355 | |
| 4356 | switch (hdev->flow_ctl_mode) { |
| 4357 | case HCI_FLOW_CTL_MODE_PACKET_BASED: |
| 4358 | hci_sched_acl_pkt(hdev); |
| 4359 | break; |
| 4360 | |
| 4361 | case HCI_FLOW_CTL_MODE_BLOCK_BASED: |
| 4362 | hci_sched_acl_blk(hdev); |
| 4363 | break; |
| 4364 | } |
| 4365 | } |
| 4366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | /* Schedule SCO */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4368 | static void hci_sched_sco(struct hci_dev *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | { |
| 4370 | struct hci_conn *conn; |
| 4371 | struct sk_buff *skb; |
| 4372 | int quote; |
| 4373 | |
| 4374 | BT_DBG("%s", hdev->name); |
| 4375 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 4376 | if (!hci_conn_num(hdev, SCO_LINK)) |
| 4377 | return; |
| 4378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { |
| 4380 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 4381 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4382 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | |
| 4384 | conn->sent++; |
| 4385 | if (conn->sent == ~0) |
| 4386 | conn->sent = 0; |
| 4387 | } |
| 4388 | } |
| 4389 | } |
| 4390 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4391 | static void hci_sched_esco(struct hci_dev *hdev) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 4392 | { |
| 4393 | struct hci_conn *conn; |
| 4394 | struct sk_buff *skb; |
| 4395 | int quote; |
| 4396 | |
| 4397 | BT_DBG("%s", hdev->name); |
| 4398 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 4399 | if (!hci_conn_num(hdev, ESCO_LINK)) |
| 4400 | return; |
| 4401 | |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 4402 | while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, |
| 4403 | "e))) { |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 4404 | while (quote-- && (skb = skb_dequeue(&conn->data_q))) { |
| 4405 | BT_DBG("skb %p len %d", skb, skb->len); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4406 | hci_send_frame(hdev, skb); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 4407 | |
| 4408 | conn->sent++; |
| 4409 | if (conn->sent == ~0) |
| 4410 | conn->sent = 0; |
| 4411 | } |
| 4412 | } |
| 4413 | } |
| 4414 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4415 | static void hci_sched_le(struct hci_dev *hdev) |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4416 | { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4417 | struct hci_chan *chan; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4418 | struct sk_buff *skb; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4419 | int quote, cnt, tmp; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4420 | |
| 4421 | BT_DBG("%s", hdev->name); |
| 4422 | |
Luiz Augusto von Dentz | 52087a7 | 2011-08-17 16:23:00 +0300 | [diff] [blame] | 4423 | if (!hci_conn_num(hdev, LE_LINK)) |
| 4424 | return; |
| 4425 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4426 | if (!test_bit(HCI_RAW, &hdev->flags)) { |
| 4427 | /* LE tx timeout must be longer than maximum |
| 4428 | * link supervision timeout (40.9 seconds) */ |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 4429 | if (!hdev->le_cnt && hdev->le_pkts && |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4430 | time_after(jiffies, hdev->le_last_tx + HZ * 45)) |
Ville Tervo | bae1f5d9 | 2011-02-10 22:38:53 -0300 | [diff] [blame] | 4431 | hci_link_tx_to(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4435 | tmp = cnt; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4436 | while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 4437 | u32 priority = (skb_peek(&chan->data_q))->priority; |
| 4438 | while (quote-- && (skb = skb_peek(&chan->data_q))) { |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4439 | BT_DBG("chan %p skb %p len %d priority %u", chan, skb, |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4440 | skb->len, skb->priority); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4441 | |
Luiz Augusto von Dentz | ec1cce2 | 2011-11-02 15:52:02 +0200 | [diff] [blame] | 4442 | /* Stop if priority has changed */ |
| 4443 | if (skb->priority < priority) |
| 4444 | break; |
| 4445 | |
| 4446 | skb = skb_dequeue(&chan->data_q); |
| 4447 | |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4448 | hci_send_frame(hdev, skb); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4449 | hdev->le_last_tx = jiffies; |
| 4450 | |
| 4451 | cnt--; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4452 | chan->sent++; |
| 4453 | chan->conn->sent++; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4454 | } |
| 4455 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 4456 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4457 | if (hdev->le_pkts) |
| 4458 | hdev->le_cnt = cnt; |
| 4459 | else |
| 4460 | hdev->acl_cnt = cnt; |
Luiz Augusto von Dentz | 02b20f0 | 2011-11-02 15:52:03 +0200 | [diff] [blame] | 4461 | |
| 4462 | if (cnt != tmp) |
| 4463 | hci_prio_recalculate(hdev, LE_LINK); |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4464 | } |
| 4465 | |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 4466 | static void hci_tx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | { |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 4468 | struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | struct sk_buff *skb; |
| 4470 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4471 | 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] | 4472 | hdev->sco_cnt, hdev->le_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | |
Marcel Holtmann | 52de599 | 2013-09-03 18:08:38 -0700 | [diff] [blame] | 4474 | if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
| 4475 | /* Schedule queues and send stuff to HCI driver */ |
| 4476 | hci_sched_acl(hdev); |
| 4477 | hci_sched_sco(hdev); |
| 4478 | hci_sched_esco(hdev); |
| 4479 | hci_sched_le(hdev); |
| 4480 | } |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 4481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | /* Send next queued raw (unknown type) packet */ |
| 4483 | while ((skb = skb_dequeue(&hdev->raw_q))) |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4484 | hci_send_frame(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | } |
| 4486 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4487 | /* ----- HCI RX task (incoming data processing) ----- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | |
| 4489 | /* ACL data packet */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4490 | 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] | 4491 | { |
| 4492 | struct hci_acl_hdr *hdr = (void *) skb->data; |
| 4493 | struct hci_conn *conn; |
| 4494 | __u16 handle, flags; |
| 4495 | |
| 4496 | skb_pull(skb, HCI_ACL_HDR_SIZE); |
| 4497 | |
| 4498 | handle = __le16_to_cpu(hdr->handle); |
| 4499 | flags = hci_flags(handle); |
| 4500 | handle = hci_handle(handle); |
| 4501 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 4502 | 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] | 4503 | handle, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | |
| 4505 | hdev->stat.acl_rx++; |
| 4506 | |
| 4507 | hci_dev_lock(hdev); |
| 4508 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4509 | hci_dev_unlock(hdev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | if (conn) { |
Mat Martineau | 65983fc | 2011-12-13 15:06:02 -0800 | [diff] [blame] | 4512 | hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 4513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4515 | l2cap_recv_acldata(conn, skb, flags); |
| 4516 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4518 | BT_ERR("%s ACL packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4519 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | kfree_skb(skb); |
| 4523 | } |
| 4524 | |
| 4525 | /* SCO data packet */ |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 4526 | 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] | 4527 | { |
| 4528 | struct hci_sco_hdr *hdr = (void *) skb->data; |
| 4529 | struct hci_conn *conn; |
| 4530 | __u16 handle; |
| 4531 | |
| 4532 | skb_pull(skb, HCI_SCO_HDR_SIZE); |
| 4533 | |
| 4534 | handle = __le16_to_cpu(hdr->handle); |
| 4535 | |
Andrei Emeltchenko | f0e0951 | 2012-06-11 11:13:09 +0300 | [diff] [blame] | 4536 | 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] | 4537 | |
| 4538 | hdev->stat.sco_rx++; |
| 4539 | |
| 4540 | hci_dev_lock(hdev); |
| 4541 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4542 | hci_dev_unlock(hdev); |
| 4543 | |
| 4544 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | /* Send to upper protocol */ |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4546 | sco_recv_scodata(conn, skb); |
| 4547 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | } else { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4549 | BT_ERR("%s SCO packet for unknown connection handle %d", |
Gustavo Padovan | a8c5fb1 | 2012-05-17 00:36:26 -0300 | [diff] [blame] | 4550 | hdev->name, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | kfree_skb(skb); |
| 4554 | } |
| 4555 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4556 | static bool hci_req_is_complete(struct hci_dev *hdev) |
| 4557 | { |
| 4558 | struct sk_buff *skb; |
| 4559 | |
| 4560 | skb = skb_peek(&hdev->cmd_q); |
| 4561 | if (!skb) |
| 4562 | return true; |
| 4563 | |
| 4564 | return bt_cb(skb)->req.start; |
| 4565 | } |
| 4566 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4567 | static void hci_resend_last(struct hci_dev *hdev) |
| 4568 | { |
| 4569 | struct hci_command_hdr *sent; |
| 4570 | struct sk_buff *skb; |
| 4571 | u16 opcode; |
| 4572 | |
| 4573 | if (!hdev->sent_cmd) |
| 4574 | return; |
| 4575 | |
| 4576 | sent = (void *) hdev->sent_cmd->data; |
| 4577 | opcode = __le16_to_cpu(sent->opcode); |
| 4578 | if (opcode == HCI_OP_RESET) |
| 4579 | return; |
| 4580 | |
| 4581 | skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); |
| 4582 | if (!skb) |
| 4583 | return; |
| 4584 | |
| 4585 | skb_queue_head(&hdev->cmd_q, skb); |
| 4586 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 4587 | } |
| 4588 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4589 | void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status) |
| 4590 | { |
| 4591 | hci_req_complete_t req_complete = NULL; |
| 4592 | struct sk_buff *skb; |
| 4593 | unsigned long flags; |
| 4594 | |
| 4595 | BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); |
| 4596 | |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4597 | /* If the completed command doesn't match the last one that was |
| 4598 | * sent we need to do special handling of it. |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4599 | */ |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4600 | if (!hci_sent_cmd_data(hdev, opcode)) { |
| 4601 | /* Some CSR based controllers generate a spontaneous |
| 4602 | * reset complete event during init and any pending |
| 4603 | * command will never be completed. In such a case we |
| 4604 | * need to resend whatever was the last sent |
| 4605 | * command. |
| 4606 | */ |
| 4607 | if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) |
| 4608 | hci_resend_last(hdev); |
| 4609 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4610 | return; |
Johan Hedberg | 42c6b12 | 2013-03-05 20:37:49 +0200 | [diff] [blame] | 4611 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4612 | |
| 4613 | /* If the command succeeded and there's still more commands in |
| 4614 | * this request the request is not yet complete. |
| 4615 | */ |
| 4616 | if (!status && !hci_req_is_complete(hdev)) |
| 4617 | return; |
| 4618 | |
| 4619 | /* If this was the last command in a request the complete |
| 4620 | * callback would be found in hdev->sent_cmd instead of the |
| 4621 | * command queue (hdev->cmd_q). |
| 4622 | */ |
| 4623 | if (hdev->sent_cmd) { |
| 4624 | req_complete = bt_cb(hdev->sent_cmd)->req.complete; |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 4625 | |
| 4626 | if (req_complete) { |
| 4627 | /* We must set the complete callback to NULL to |
| 4628 | * avoid calling the callback more than once if |
| 4629 | * this function gets called again. |
| 4630 | */ |
| 4631 | bt_cb(hdev->sent_cmd)->req.complete = NULL; |
| 4632 | |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4633 | goto call_complete; |
Johan Hedberg | 53e21fb | 2013-07-27 14:11:14 -0500 | [diff] [blame] | 4634 | } |
Johan Hedberg | 9238f36 | 2013-03-05 20:37:48 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | /* Remove all pending commands belonging to this request */ |
| 4638 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 4639 | while ((skb = __skb_dequeue(&hdev->cmd_q))) { |
| 4640 | if (bt_cb(skb)->req.start) { |
| 4641 | __skb_queue_head(&hdev->cmd_q, skb); |
| 4642 | break; |
| 4643 | } |
| 4644 | |
| 4645 | req_complete = bt_cb(skb)->req.complete; |
| 4646 | kfree_skb(skb); |
| 4647 | } |
| 4648 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 4649 | |
| 4650 | call_complete: |
| 4651 | if (req_complete) |
| 4652 | req_complete(hdev, status); |
| 4653 | } |
| 4654 | |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 4655 | static void hci_rx_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | { |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 4657 | struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | struct sk_buff *skb; |
| 4659 | |
| 4660 | BT_DBG("%s", hdev->name); |
| 4661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | while ((skb = skb_dequeue(&hdev->rx_q))) { |
Marcel Holtmann | cd82e61 | 2012-02-20 20:34:38 +0100 | [diff] [blame] | 4663 | /* Send copy to monitor */ |
| 4664 | hci_send_to_monitor(hdev, skb); |
| 4665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | if (atomic_read(&hdev->promisc)) { |
| 4667 | /* Send copy to the sockets */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame] | 4668 | hci_send_to_sock(hdev, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4671 | if (test_bit(HCI_RAW, &hdev->flags) || |
| 4672 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | kfree_skb(skb); |
| 4674 | continue; |
| 4675 | } |
| 4676 | |
| 4677 | if (test_bit(HCI_INIT, &hdev->flags)) { |
| 4678 | /* Don't process data packets in this states. */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 4679 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | case HCI_ACLDATA_PKT: |
| 4681 | case HCI_SCODATA_PKT: |
| 4682 | kfree_skb(skb); |
| 4683 | continue; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | /* Process frame */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 4688 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | case HCI_EVENT_PKT: |
Marcel Holtmann | b78752c | 2010-08-08 23:06:53 -0400 | [diff] [blame] | 4690 | BT_DBG("%s Event packet", hdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | hci_event_packet(hdev, skb); |
| 4692 | break; |
| 4693 | |
| 4694 | case HCI_ACLDATA_PKT: |
| 4695 | BT_DBG("%s ACL data packet", hdev->name); |
| 4696 | hci_acldata_packet(hdev, skb); |
| 4697 | break; |
| 4698 | |
| 4699 | case HCI_SCODATA_PKT: |
| 4700 | BT_DBG("%s SCO data packet", hdev->name); |
| 4701 | hci_scodata_packet(hdev, skb); |
| 4702 | break; |
| 4703 | |
| 4704 | default: |
| 4705 | kfree_skb(skb); |
| 4706 | break; |
| 4707 | } |
| 4708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 4711 | static void hci_cmd_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | { |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 4713 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | struct sk_buff *skb; |
| 4715 | |
Andrei Emeltchenko | 2104786 | 2012-07-10 15:27:47 +0300 | [diff] [blame] | 4716 | BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, |
| 4717 | atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | /* Send queued commands */ |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4720 | if (atomic_read(&hdev->cmd_cnt)) { |
| 4721 | skb = skb_dequeue(&hdev->cmd_q); |
| 4722 | if (!skb) |
| 4723 | return; |
| 4724 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 4725 | kfree_skb(hdev->sent_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | |
Marcel Holtmann | a675d7f | 2013-09-03 18:11:07 -0700 | [diff] [blame] | 4727 | hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 4728 | if (hdev->sent_cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | atomic_dec(&hdev->cmd_cnt); |
Marcel Holtmann | 57d17d7 | 2013-10-10 14:54:17 -0700 | [diff] [blame] | 4730 | hci_send_frame(hdev, skb); |
Szymon Janc | 7bdb8a5 | 2011-07-26 22:46:54 +0200 | [diff] [blame] | 4731 | if (test_bit(HCI_RESET, &hdev->flags)) |
| 4732 | del_timer(&hdev->cmd_timer); |
| 4733 | else |
| 4734 | mod_timer(&hdev->cmd_timer, |
Andrei Emeltchenko | 5f246e8 | 2012-06-11 11:13:07 +0300 | [diff] [blame] | 4735 | jiffies + HCI_CMD_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | } else { |
| 4737 | skb_queue_head(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 4738 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | } |
| 4740 | } |
| 4741 | } |