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