11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds BlueZ - Bluetooth protocol stack for Linux 31da177e4SLinus Torvalds Copyright (C) 2000-2001 Qualcomm Incorporated 4590051deSGustavo F. Padovan Copyright (C) 2011 ProFUSION Embedded Systems 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 91da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as 101da177e4SLinus Torvalds published by the Free Software Foundation; 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 131da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 141da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 151da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 161da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 171da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 181da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 191da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 221da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 231da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds /* Bluetooth HCI core. */ 271da177e4SLinus Torvalds 288c520a59SGustavo Padovan #include <linux/export.h> 293df92b31SSasha Levin #include <linux/idr.h> 30611b30f7SMarcel Holtmann #include <linux/rfkill.h> 31baf27f6eSMarcel Holtmann #include <linux/debugfs.h> 3299780a7bSJohan Hedberg #include <linux/crypto.h> 3347219839SMarcel Holtmann #include <asm/unaligned.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h> 361da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h> 374bc58f51SJohan Hedberg #include <net/bluetooth/l2cap.h> 38af58925cSMarcel Holtmann #include <net/bluetooth/mgmt.h> 391da177e4SLinus Torvalds 40970c4e46SJohan Hedberg #include "smp.h" 41970c4e46SJohan Hedberg 42b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work); 43c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work); 443eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work); 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* HCI device list */ 471da177e4SLinus Torvalds LIST_HEAD(hci_dev_list); 481da177e4SLinus Torvalds DEFINE_RWLOCK(hci_dev_list_lock); 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* HCI callback list */ 511da177e4SLinus Torvalds LIST_HEAD(hci_cb_list); 521da177e4SLinus Torvalds DEFINE_RWLOCK(hci_cb_list_lock); 531da177e4SLinus Torvalds 543df92b31SSasha Levin /* HCI ID Numbering */ 553df92b31SSasha Levin static DEFINE_IDA(hci_index_ida); 563df92b31SSasha Levin 571da177e4SLinus Torvalds /* ---- HCI notifications ---- */ 581da177e4SLinus Torvalds 596516455dSMarcel Holtmann static void hci_notify(struct hci_dev *hdev, int event) 601da177e4SLinus Torvalds { 61040030efSMarcel Holtmann hci_sock_dev_event(hdev, event); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 64baf27f6eSMarcel Holtmann /* ---- HCI debugfs entries ---- */ 65baf27f6eSMarcel Holtmann 664b4148e9SMarcel Holtmann static ssize_t dut_mode_read(struct file *file, char __user *user_buf, 674b4148e9SMarcel Holtmann size_t count, loff_t *ppos) 684b4148e9SMarcel Holtmann { 694b4148e9SMarcel Holtmann struct hci_dev *hdev = file->private_data; 704b4148e9SMarcel Holtmann char buf[3]; 714b4148e9SMarcel Holtmann 72111902f7SMarcel Holtmann buf[0] = test_bit(HCI_DUT_MODE, &hdev->dbg_flags) ? 'Y': 'N'; 734b4148e9SMarcel Holtmann buf[1] = '\n'; 744b4148e9SMarcel Holtmann buf[2] = '\0'; 754b4148e9SMarcel Holtmann return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 764b4148e9SMarcel Holtmann } 774b4148e9SMarcel Holtmann 784b4148e9SMarcel Holtmann static ssize_t dut_mode_write(struct file *file, const char __user *user_buf, 794b4148e9SMarcel Holtmann size_t count, loff_t *ppos) 804b4148e9SMarcel Holtmann { 814b4148e9SMarcel Holtmann struct hci_dev *hdev = file->private_data; 824b4148e9SMarcel Holtmann struct sk_buff *skb; 834b4148e9SMarcel Holtmann char buf[32]; 844b4148e9SMarcel Holtmann size_t buf_size = min(count, (sizeof(buf)-1)); 854b4148e9SMarcel Holtmann bool enable; 864b4148e9SMarcel Holtmann int err; 874b4148e9SMarcel Holtmann 884b4148e9SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) 894b4148e9SMarcel Holtmann return -ENETDOWN; 904b4148e9SMarcel Holtmann 914b4148e9SMarcel Holtmann if (copy_from_user(buf, user_buf, buf_size)) 924b4148e9SMarcel Holtmann return -EFAULT; 934b4148e9SMarcel Holtmann 944b4148e9SMarcel Holtmann buf[buf_size] = '\0'; 954b4148e9SMarcel Holtmann if (strtobool(buf, &enable)) 964b4148e9SMarcel Holtmann return -EINVAL; 974b4148e9SMarcel Holtmann 98111902f7SMarcel Holtmann if (enable == test_bit(HCI_DUT_MODE, &hdev->dbg_flags)) 994b4148e9SMarcel Holtmann return -EALREADY; 1004b4148e9SMarcel Holtmann 1014b4148e9SMarcel Holtmann hci_req_lock(hdev); 1024b4148e9SMarcel Holtmann if (enable) 1034b4148e9SMarcel Holtmann skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL, 1044b4148e9SMarcel Holtmann HCI_CMD_TIMEOUT); 1054b4148e9SMarcel Holtmann else 1064b4148e9SMarcel Holtmann skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, 1074b4148e9SMarcel Holtmann HCI_CMD_TIMEOUT); 1084b4148e9SMarcel Holtmann hci_req_unlock(hdev); 1094b4148e9SMarcel Holtmann 1104b4148e9SMarcel Holtmann if (IS_ERR(skb)) 1114b4148e9SMarcel Holtmann return PTR_ERR(skb); 1124b4148e9SMarcel Holtmann 1134b4148e9SMarcel Holtmann err = -bt_to_errno(skb->data[0]); 1144b4148e9SMarcel Holtmann kfree_skb(skb); 1154b4148e9SMarcel Holtmann 1164b4148e9SMarcel Holtmann if (err < 0) 1174b4148e9SMarcel Holtmann return err; 1184b4148e9SMarcel Holtmann 119111902f7SMarcel Holtmann change_bit(HCI_DUT_MODE, &hdev->dbg_flags); 1204b4148e9SMarcel Holtmann 1214b4148e9SMarcel Holtmann return count; 1224b4148e9SMarcel Holtmann } 1234b4148e9SMarcel Holtmann 1244b4148e9SMarcel Holtmann static const struct file_operations dut_mode_fops = { 1254b4148e9SMarcel Holtmann .open = simple_open, 1264b4148e9SMarcel Holtmann .read = dut_mode_read, 1274b4148e9SMarcel Holtmann .write = dut_mode_write, 1284b4148e9SMarcel Holtmann .llseek = default_llseek, 1294b4148e9SMarcel Holtmann }; 1304b4148e9SMarcel Holtmann 131dfb826a8SMarcel Holtmann static int features_show(struct seq_file *f, void *ptr) 132dfb826a8SMarcel Holtmann { 133dfb826a8SMarcel Holtmann struct hci_dev *hdev = f->private; 134dfb826a8SMarcel Holtmann u8 p; 135dfb826a8SMarcel Holtmann 136dfb826a8SMarcel Holtmann hci_dev_lock(hdev); 137dfb826a8SMarcel Holtmann for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { 138cfbb2b5bSMarcel Holtmann seq_printf(f, "%2u: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x " 139dfb826a8SMarcel Holtmann "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", p, 140dfb826a8SMarcel Holtmann hdev->features[p][0], hdev->features[p][1], 141dfb826a8SMarcel Holtmann hdev->features[p][2], hdev->features[p][3], 142dfb826a8SMarcel Holtmann hdev->features[p][4], hdev->features[p][5], 143dfb826a8SMarcel Holtmann hdev->features[p][6], hdev->features[p][7]); 144dfb826a8SMarcel Holtmann } 145cfbb2b5bSMarcel Holtmann if (lmp_le_capable(hdev)) 146cfbb2b5bSMarcel Holtmann seq_printf(f, "LE: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x " 147cfbb2b5bSMarcel Holtmann "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", 148cfbb2b5bSMarcel Holtmann hdev->le_features[0], hdev->le_features[1], 149cfbb2b5bSMarcel Holtmann hdev->le_features[2], hdev->le_features[3], 150cfbb2b5bSMarcel Holtmann hdev->le_features[4], hdev->le_features[5], 151cfbb2b5bSMarcel Holtmann hdev->le_features[6], hdev->le_features[7]); 152dfb826a8SMarcel Holtmann hci_dev_unlock(hdev); 153dfb826a8SMarcel Holtmann 154dfb826a8SMarcel Holtmann return 0; 155dfb826a8SMarcel Holtmann } 156dfb826a8SMarcel Holtmann 157dfb826a8SMarcel Holtmann static int features_open(struct inode *inode, struct file *file) 158dfb826a8SMarcel Holtmann { 159dfb826a8SMarcel Holtmann return single_open(file, features_show, inode->i_private); 160dfb826a8SMarcel Holtmann } 161dfb826a8SMarcel Holtmann 162dfb826a8SMarcel Holtmann static const struct file_operations features_fops = { 163dfb826a8SMarcel Holtmann .open = features_open, 164dfb826a8SMarcel Holtmann .read = seq_read, 165dfb826a8SMarcel Holtmann .llseek = seq_lseek, 166dfb826a8SMarcel Holtmann .release = single_release, 167dfb826a8SMarcel Holtmann }; 168dfb826a8SMarcel Holtmann 16970afe0b8SMarcel Holtmann static int blacklist_show(struct seq_file *f, void *p) 17070afe0b8SMarcel Holtmann { 17170afe0b8SMarcel Holtmann struct hci_dev *hdev = f->private; 17270afe0b8SMarcel Holtmann struct bdaddr_list *b; 17370afe0b8SMarcel Holtmann 17470afe0b8SMarcel Holtmann hci_dev_lock(hdev); 17570afe0b8SMarcel Holtmann list_for_each_entry(b, &hdev->blacklist, list) 176b25f0785SMarcel Holtmann seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type); 17770afe0b8SMarcel Holtmann hci_dev_unlock(hdev); 17870afe0b8SMarcel Holtmann 17970afe0b8SMarcel Holtmann return 0; 18070afe0b8SMarcel Holtmann } 18170afe0b8SMarcel Holtmann 18270afe0b8SMarcel Holtmann static int blacklist_open(struct inode *inode, struct file *file) 18370afe0b8SMarcel Holtmann { 18470afe0b8SMarcel Holtmann return single_open(file, blacklist_show, inode->i_private); 18570afe0b8SMarcel Holtmann } 18670afe0b8SMarcel Holtmann 18770afe0b8SMarcel Holtmann static const struct file_operations blacklist_fops = { 18870afe0b8SMarcel Holtmann .open = blacklist_open, 18970afe0b8SMarcel Holtmann .read = seq_read, 19070afe0b8SMarcel Holtmann .llseek = seq_lseek, 19170afe0b8SMarcel Holtmann .release = single_release, 19270afe0b8SMarcel Holtmann }; 19370afe0b8SMarcel Holtmann 19447219839SMarcel Holtmann static int uuids_show(struct seq_file *f, void *p) 19547219839SMarcel Holtmann { 19647219839SMarcel Holtmann struct hci_dev *hdev = f->private; 19747219839SMarcel Holtmann struct bt_uuid *uuid; 19847219839SMarcel Holtmann 19947219839SMarcel Holtmann hci_dev_lock(hdev); 20047219839SMarcel Holtmann list_for_each_entry(uuid, &hdev->uuids, list) { 20158f01aa9SMarcel Holtmann u8 i, val[16]; 20247219839SMarcel Holtmann 20358f01aa9SMarcel Holtmann /* The Bluetooth UUID values are stored in big endian, 20458f01aa9SMarcel Holtmann * but with reversed byte order. So convert them into 20558f01aa9SMarcel Holtmann * the right order for the %pUb modifier. 20658f01aa9SMarcel Holtmann */ 20758f01aa9SMarcel Holtmann for (i = 0; i < 16; i++) 20858f01aa9SMarcel Holtmann val[i] = uuid->uuid[15 - i]; 20947219839SMarcel Holtmann 21058f01aa9SMarcel Holtmann seq_printf(f, "%pUb\n", val); 21147219839SMarcel Holtmann } 21247219839SMarcel Holtmann hci_dev_unlock(hdev); 21347219839SMarcel Holtmann 21447219839SMarcel Holtmann return 0; 21547219839SMarcel Holtmann } 21647219839SMarcel Holtmann 21747219839SMarcel Holtmann static int uuids_open(struct inode *inode, struct file *file) 21847219839SMarcel Holtmann { 21947219839SMarcel Holtmann return single_open(file, uuids_show, inode->i_private); 22047219839SMarcel Holtmann } 22147219839SMarcel Holtmann 22247219839SMarcel Holtmann static const struct file_operations uuids_fops = { 22347219839SMarcel Holtmann .open = uuids_open, 22447219839SMarcel Holtmann .read = seq_read, 22547219839SMarcel Holtmann .llseek = seq_lseek, 22647219839SMarcel Holtmann .release = single_release, 22747219839SMarcel Holtmann }; 22847219839SMarcel Holtmann 229baf27f6eSMarcel Holtmann static int inquiry_cache_show(struct seq_file *f, void *p) 230baf27f6eSMarcel Holtmann { 231baf27f6eSMarcel Holtmann struct hci_dev *hdev = f->private; 232baf27f6eSMarcel Holtmann struct discovery_state *cache = &hdev->discovery; 233baf27f6eSMarcel Holtmann struct inquiry_entry *e; 234baf27f6eSMarcel Holtmann 235baf27f6eSMarcel Holtmann hci_dev_lock(hdev); 236baf27f6eSMarcel Holtmann 237baf27f6eSMarcel Holtmann list_for_each_entry(e, &cache->all, all) { 238baf27f6eSMarcel Holtmann struct inquiry_data *data = &e->data; 239baf27f6eSMarcel Holtmann seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n", 240baf27f6eSMarcel Holtmann &data->bdaddr, 241baf27f6eSMarcel Holtmann data->pscan_rep_mode, data->pscan_period_mode, 242baf27f6eSMarcel Holtmann data->pscan_mode, data->dev_class[2], 243baf27f6eSMarcel Holtmann data->dev_class[1], data->dev_class[0], 244baf27f6eSMarcel Holtmann __le16_to_cpu(data->clock_offset), 245baf27f6eSMarcel Holtmann data->rssi, data->ssp_mode, e->timestamp); 246baf27f6eSMarcel Holtmann } 247baf27f6eSMarcel Holtmann 248baf27f6eSMarcel Holtmann hci_dev_unlock(hdev); 249baf27f6eSMarcel Holtmann 250baf27f6eSMarcel Holtmann return 0; 251baf27f6eSMarcel Holtmann } 252baf27f6eSMarcel Holtmann 253baf27f6eSMarcel Holtmann static int inquiry_cache_open(struct inode *inode, struct file *file) 254baf27f6eSMarcel Holtmann { 255baf27f6eSMarcel Holtmann return single_open(file, inquiry_cache_show, inode->i_private); 256baf27f6eSMarcel Holtmann } 257baf27f6eSMarcel Holtmann 258baf27f6eSMarcel Holtmann static const struct file_operations inquiry_cache_fops = { 259baf27f6eSMarcel Holtmann .open = inquiry_cache_open, 260baf27f6eSMarcel Holtmann .read = seq_read, 261baf27f6eSMarcel Holtmann .llseek = seq_lseek, 262baf27f6eSMarcel Holtmann .release = single_release, 263baf27f6eSMarcel Holtmann }; 264baf27f6eSMarcel Holtmann 26502d08d15SMarcel Holtmann static int link_keys_show(struct seq_file *f, void *ptr) 26602d08d15SMarcel Holtmann { 26702d08d15SMarcel Holtmann struct hci_dev *hdev = f->private; 26802d08d15SMarcel Holtmann struct list_head *p, *n; 26902d08d15SMarcel Holtmann 27002d08d15SMarcel Holtmann hci_dev_lock(hdev); 27102d08d15SMarcel Holtmann list_for_each_safe(p, n, &hdev->link_keys) { 27202d08d15SMarcel Holtmann struct link_key *key = list_entry(p, struct link_key, list); 27302d08d15SMarcel Holtmann seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type, 27402d08d15SMarcel Holtmann HCI_LINK_KEY_SIZE, key->val, key->pin_len); 27502d08d15SMarcel Holtmann } 27602d08d15SMarcel Holtmann hci_dev_unlock(hdev); 27702d08d15SMarcel Holtmann 27802d08d15SMarcel Holtmann return 0; 27902d08d15SMarcel Holtmann } 28002d08d15SMarcel Holtmann 28102d08d15SMarcel Holtmann static int link_keys_open(struct inode *inode, struct file *file) 28202d08d15SMarcel Holtmann { 28302d08d15SMarcel Holtmann return single_open(file, link_keys_show, inode->i_private); 28402d08d15SMarcel Holtmann } 28502d08d15SMarcel Holtmann 28602d08d15SMarcel Holtmann static const struct file_operations link_keys_fops = { 28702d08d15SMarcel Holtmann .open = link_keys_open, 28802d08d15SMarcel Holtmann .read = seq_read, 28902d08d15SMarcel Holtmann .llseek = seq_lseek, 29002d08d15SMarcel Holtmann .release = single_release, 29102d08d15SMarcel Holtmann }; 29202d08d15SMarcel Holtmann 293babdbb3cSMarcel Holtmann static int dev_class_show(struct seq_file *f, void *ptr) 294babdbb3cSMarcel Holtmann { 295babdbb3cSMarcel Holtmann struct hci_dev *hdev = f->private; 296babdbb3cSMarcel Holtmann 297babdbb3cSMarcel Holtmann hci_dev_lock(hdev); 298babdbb3cSMarcel Holtmann seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2], 299babdbb3cSMarcel Holtmann hdev->dev_class[1], hdev->dev_class[0]); 300babdbb3cSMarcel Holtmann hci_dev_unlock(hdev); 301babdbb3cSMarcel Holtmann 302babdbb3cSMarcel Holtmann return 0; 303babdbb3cSMarcel Holtmann } 304babdbb3cSMarcel Holtmann 305babdbb3cSMarcel Holtmann static int dev_class_open(struct inode *inode, struct file *file) 306babdbb3cSMarcel Holtmann { 307babdbb3cSMarcel Holtmann return single_open(file, dev_class_show, inode->i_private); 308babdbb3cSMarcel Holtmann } 309babdbb3cSMarcel Holtmann 310babdbb3cSMarcel Holtmann static const struct file_operations dev_class_fops = { 311babdbb3cSMarcel Holtmann .open = dev_class_open, 312babdbb3cSMarcel Holtmann .read = seq_read, 313babdbb3cSMarcel Holtmann .llseek = seq_lseek, 314babdbb3cSMarcel Holtmann .release = single_release, 315babdbb3cSMarcel Holtmann }; 316babdbb3cSMarcel Holtmann 317041000b9SMarcel Holtmann static int voice_setting_get(void *data, u64 *val) 318041000b9SMarcel Holtmann { 319041000b9SMarcel Holtmann struct hci_dev *hdev = data; 320041000b9SMarcel Holtmann 321041000b9SMarcel Holtmann hci_dev_lock(hdev); 322041000b9SMarcel Holtmann *val = hdev->voice_setting; 323041000b9SMarcel Holtmann hci_dev_unlock(hdev); 324041000b9SMarcel Holtmann 325041000b9SMarcel Holtmann return 0; 326041000b9SMarcel Holtmann } 327041000b9SMarcel Holtmann 328041000b9SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(voice_setting_fops, voice_setting_get, 329041000b9SMarcel Holtmann NULL, "0x%4.4llx\n"); 330041000b9SMarcel Holtmann 331ebd1e33bSMarcel Holtmann static int auto_accept_delay_set(void *data, u64 val) 332ebd1e33bSMarcel Holtmann { 333ebd1e33bSMarcel Holtmann struct hci_dev *hdev = data; 334ebd1e33bSMarcel Holtmann 335ebd1e33bSMarcel Holtmann hci_dev_lock(hdev); 336ebd1e33bSMarcel Holtmann hdev->auto_accept_delay = val; 337ebd1e33bSMarcel Holtmann hci_dev_unlock(hdev); 338ebd1e33bSMarcel Holtmann 339ebd1e33bSMarcel Holtmann return 0; 340ebd1e33bSMarcel Holtmann } 341ebd1e33bSMarcel Holtmann 342ebd1e33bSMarcel Holtmann static int auto_accept_delay_get(void *data, u64 *val) 343ebd1e33bSMarcel Holtmann { 344ebd1e33bSMarcel Holtmann struct hci_dev *hdev = data; 345ebd1e33bSMarcel Holtmann 346ebd1e33bSMarcel Holtmann hci_dev_lock(hdev); 347ebd1e33bSMarcel Holtmann *val = hdev->auto_accept_delay; 348ebd1e33bSMarcel Holtmann hci_dev_unlock(hdev); 349ebd1e33bSMarcel Holtmann 350ebd1e33bSMarcel Holtmann return 0; 351ebd1e33bSMarcel Holtmann } 352ebd1e33bSMarcel Holtmann 353ebd1e33bSMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get, 354ebd1e33bSMarcel Holtmann auto_accept_delay_set, "%llu\n"); 355ebd1e33bSMarcel Holtmann 3565afeac14SMarcel Holtmann static ssize_t force_sc_support_read(struct file *file, char __user *user_buf, 3575afeac14SMarcel Holtmann size_t count, loff_t *ppos) 3585afeac14SMarcel Holtmann { 3595afeac14SMarcel Holtmann struct hci_dev *hdev = file->private_data; 3605afeac14SMarcel Holtmann char buf[3]; 3615afeac14SMarcel Holtmann 362111902f7SMarcel Holtmann buf[0] = test_bit(HCI_FORCE_SC, &hdev->dbg_flags) ? 'Y': 'N'; 3635afeac14SMarcel Holtmann buf[1] = '\n'; 3645afeac14SMarcel Holtmann buf[2] = '\0'; 3655afeac14SMarcel Holtmann return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 3665afeac14SMarcel Holtmann } 3675afeac14SMarcel Holtmann 3685afeac14SMarcel Holtmann static ssize_t force_sc_support_write(struct file *file, 3695afeac14SMarcel Holtmann const char __user *user_buf, 3705afeac14SMarcel Holtmann size_t count, loff_t *ppos) 3715afeac14SMarcel Holtmann { 3725afeac14SMarcel Holtmann struct hci_dev *hdev = file->private_data; 3735afeac14SMarcel Holtmann char buf[32]; 3745afeac14SMarcel Holtmann size_t buf_size = min(count, (sizeof(buf)-1)); 3755afeac14SMarcel Holtmann bool enable; 3765afeac14SMarcel Holtmann 3775afeac14SMarcel Holtmann if (test_bit(HCI_UP, &hdev->flags)) 3785afeac14SMarcel Holtmann return -EBUSY; 3795afeac14SMarcel Holtmann 3805afeac14SMarcel Holtmann if (copy_from_user(buf, user_buf, buf_size)) 3815afeac14SMarcel Holtmann return -EFAULT; 3825afeac14SMarcel Holtmann 3835afeac14SMarcel Holtmann buf[buf_size] = '\0'; 3845afeac14SMarcel Holtmann if (strtobool(buf, &enable)) 3855afeac14SMarcel Holtmann return -EINVAL; 3865afeac14SMarcel Holtmann 387111902f7SMarcel Holtmann if (enable == test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) 3885afeac14SMarcel Holtmann return -EALREADY; 3895afeac14SMarcel Holtmann 390111902f7SMarcel Holtmann change_bit(HCI_FORCE_SC, &hdev->dbg_flags); 3915afeac14SMarcel Holtmann 3925afeac14SMarcel Holtmann return count; 3935afeac14SMarcel Holtmann } 3945afeac14SMarcel Holtmann 3955afeac14SMarcel Holtmann static const struct file_operations force_sc_support_fops = { 3965afeac14SMarcel Holtmann .open = simple_open, 3975afeac14SMarcel Holtmann .read = force_sc_support_read, 3985afeac14SMarcel Holtmann .write = force_sc_support_write, 3995afeac14SMarcel Holtmann .llseek = default_llseek, 4005afeac14SMarcel Holtmann }; 4015afeac14SMarcel Holtmann 402134c2a89SMarcel Holtmann static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf, 403134c2a89SMarcel Holtmann size_t count, loff_t *ppos) 404134c2a89SMarcel Holtmann { 405134c2a89SMarcel Holtmann struct hci_dev *hdev = file->private_data; 406134c2a89SMarcel Holtmann char buf[3]; 407134c2a89SMarcel Holtmann 408134c2a89SMarcel Holtmann buf[0] = test_bit(HCI_SC_ONLY, &hdev->dev_flags) ? 'Y': 'N'; 409134c2a89SMarcel Holtmann buf[1] = '\n'; 410134c2a89SMarcel Holtmann buf[2] = '\0'; 411134c2a89SMarcel Holtmann return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 412134c2a89SMarcel Holtmann } 413134c2a89SMarcel Holtmann 414134c2a89SMarcel Holtmann static const struct file_operations sc_only_mode_fops = { 415134c2a89SMarcel Holtmann .open = simple_open, 416134c2a89SMarcel Holtmann .read = sc_only_mode_read, 417134c2a89SMarcel Holtmann .llseek = default_llseek, 418134c2a89SMarcel Holtmann }; 419134c2a89SMarcel Holtmann 4202bfa3531SMarcel Holtmann static int idle_timeout_set(void *data, u64 val) 4212bfa3531SMarcel Holtmann { 4222bfa3531SMarcel Holtmann struct hci_dev *hdev = data; 4232bfa3531SMarcel Holtmann 4242bfa3531SMarcel Holtmann if (val != 0 && (val < 500 || val > 3600000)) 4252bfa3531SMarcel Holtmann return -EINVAL; 4262bfa3531SMarcel Holtmann 4272bfa3531SMarcel Holtmann hci_dev_lock(hdev); 4282bfa3531SMarcel Holtmann hdev->idle_timeout = val; 4292bfa3531SMarcel Holtmann hci_dev_unlock(hdev); 4302bfa3531SMarcel Holtmann 4312bfa3531SMarcel Holtmann return 0; 4322bfa3531SMarcel Holtmann } 4332bfa3531SMarcel Holtmann 4342bfa3531SMarcel Holtmann static int idle_timeout_get(void *data, u64 *val) 4352bfa3531SMarcel Holtmann { 4362bfa3531SMarcel Holtmann struct hci_dev *hdev = data; 4372bfa3531SMarcel Holtmann 4382bfa3531SMarcel Holtmann hci_dev_lock(hdev); 4392bfa3531SMarcel Holtmann *val = hdev->idle_timeout; 4402bfa3531SMarcel Holtmann hci_dev_unlock(hdev); 4412bfa3531SMarcel Holtmann 4422bfa3531SMarcel Holtmann return 0; 4432bfa3531SMarcel Holtmann } 4442bfa3531SMarcel Holtmann 4452bfa3531SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get, 4462bfa3531SMarcel Holtmann idle_timeout_set, "%llu\n"); 4472bfa3531SMarcel Holtmann 448c982b2eaSJohan Hedberg static int rpa_timeout_set(void *data, u64 val) 449c982b2eaSJohan Hedberg { 450c982b2eaSJohan Hedberg struct hci_dev *hdev = data; 451c982b2eaSJohan Hedberg 452c982b2eaSJohan Hedberg /* Require the RPA timeout to be at least 30 seconds and at most 453c982b2eaSJohan Hedberg * 24 hours. 454c982b2eaSJohan Hedberg */ 455c982b2eaSJohan Hedberg if (val < 30 || val > (60 * 60 * 24)) 456c982b2eaSJohan Hedberg return -EINVAL; 457c982b2eaSJohan Hedberg 458c982b2eaSJohan Hedberg hci_dev_lock(hdev); 459c982b2eaSJohan Hedberg hdev->rpa_timeout = val; 460c982b2eaSJohan Hedberg hci_dev_unlock(hdev); 461c982b2eaSJohan Hedberg 462c982b2eaSJohan Hedberg return 0; 463c982b2eaSJohan Hedberg } 464c982b2eaSJohan Hedberg 465c982b2eaSJohan Hedberg static int rpa_timeout_get(void *data, u64 *val) 466c982b2eaSJohan Hedberg { 467c982b2eaSJohan Hedberg struct hci_dev *hdev = data; 468c982b2eaSJohan Hedberg 469c982b2eaSJohan Hedberg hci_dev_lock(hdev); 470c982b2eaSJohan Hedberg *val = hdev->rpa_timeout; 471c982b2eaSJohan Hedberg hci_dev_unlock(hdev); 472c982b2eaSJohan Hedberg 473c982b2eaSJohan Hedberg return 0; 474c982b2eaSJohan Hedberg } 475c982b2eaSJohan Hedberg 476c982b2eaSJohan Hedberg DEFINE_SIMPLE_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get, 477c982b2eaSJohan Hedberg rpa_timeout_set, "%llu\n"); 478c982b2eaSJohan Hedberg 4792bfa3531SMarcel Holtmann static int sniff_min_interval_set(void *data, u64 val) 4802bfa3531SMarcel Holtmann { 4812bfa3531SMarcel Holtmann struct hci_dev *hdev = data; 4822bfa3531SMarcel Holtmann 4832bfa3531SMarcel Holtmann if (val == 0 || val % 2 || val > hdev->sniff_max_interval) 4842bfa3531SMarcel Holtmann return -EINVAL; 4852bfa3531SMarcel Holtmann 4862bfa3531SMarcel Holtmann hci_dev_lock(hdev); 4872bfa3531SMarcel Holtmann hdev->sniff_min_interval = val; 4882bfa3531SMarcel Holtmann hci_dev_unlock(hdev); 4892bfa3531SMarcel Holtmann 4902bfa3531SMarcel Holtmann return 0; 4912bfa3531SMarcel Holtmann } 4922bfa3531SMarcel Holtmann 4932bfa3531SMarcel Holtmann static int sniff_min_interval_get(void *data, u64 *val) 4942bfa3531SMarcel Holtmann { 4952bfa3531SMarcel Holtmann struct hci_dev *hdev = data; 4962bfa3531SMarcel Holtmann 4972bfa3531SMarcel Holtmann hci_dev_lock(hdev); 4982bfa3531SMarcel Holtmann *val = hdev->sniff_min_interval; 4992bfa3531SMarcel Holtmann hci_dev_unlock(hdev); 5002bfa3531SMarcel Holtmann 5012bfa3531SMarcel Holtmann return 0; 5022bfa3531SMarcel Holtmann } 5032bfa3531SMarcel Holtmann 5042bfa3531SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get, 5052bfa3531SMarcel Holtmann sniff_min_interval_set, "%llu\n"); 5062bfa3531SMarcel Holtmann 5072bfa3531SMarcel Holtmann static int sniff_max_interval_set(void *data, u64 val) 5082bfa3531SMarcel Holtmann { 5092bfa3531SMarcel Holtmann struct hci_dev *hdev = data; 5102bfa3531SMarcel Holtmann 5112bfa3531SMarcel Holtmann if (val == 0 || val % 2 || val < hdev->sniff_min_interval) 5122bfa3531SMarcel Holtmann return -EINVAL; 5132bfa3531SMarcel Holtmann 5142bfa3531SMarcel Holtmann hci_dev_lock(hdev); 5152bfa3531SMarcel Holtmann hdev->sniff_max_interval = val; 5162bfa3531SMarcel Holtmann hci_dev_unlock(hdev); 5172bfa3531SMarcel Holtmann 5182bfa3531SMarcel Holtmann return 0; 5192bfa3531SMarcel Holtmann } 5202bfa3531SMarcel Holtmann 5212bfa3531SMarcel Holtmann static int sniff_max_interval_get(void *data, u64 *val) 5222bfa3531SMarcel Holtmann { 5232bfa3531SMarcel Holtmann struct hci_dev *hdev = data; 5242bfa3531SMarcel Holtmann 5252bfa3531SMarcel Holtmann hci_dev_lock(hdev); 5262bfa3531SMarcel Holtmann *val = hdev->sniff_max_interval; 5272bfa3531SMarcel Holtmann hci_dev_unlock(hdev); 5282bfa3531SMarcel Holtmann 5292bfa3531SMarcel Holtmann return 0; 5302bfa3531SMarcel Holtmann } 5312bfa3531SMarcel Holtmann 5322bfa3531SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get, 5332bfa3531SMarcel Holtmann sniff_max_interval_set, "%llu\n"); 5342bfa3531SMarcel Holtmann 53531ad1691SAndrzej Kaczmarek static int conn_info_min_age_set(void *data, u64 val) 53631ad1691SAndrzej Kaczmarek { 53731ad1691SAndrzej Kaczmarek struct hci_dev *hdev = data; 53831ad1691SAndrzej Kaczmarek 53931ad1691SAndrzej Kaczmarek if (val == 0 || val > hdev->conn_info_max_age) 54031ad1691SAndrzej Kaczmarek return -EINVAL; 54131ad1691SAndrzej Kaczmarek 54231ad1691SAndrzej Kaczmarek hci_dev_lock(hdev); 54331ad1691SAndrzej Kaczmarek hdev->conn_info_min_age = val; 54431ad1691SAndrzej Kaczmarek hci_dev_unlock(hdev); 54531ad1691SAndrzej Kaczmarek 54631ad1691SAndrzej Kaczmarek return 0; 54731ad1691SAndrzej Kaczmarek } 54831ad1691SAndrzej Kaczmarek 54931ad1691SAndrzej Kaczmarek static int conn_info_min_age_get(void *data, u64 *val) 55031ad1691SAndrzej Kaczmarek { 55131ad1691SAndrzej Kaczmarek struct hci_dev *hdev = data; 55231ad1691SAndrzej Kaczmarek 55331ad1691SAndrzej Kaczmarek hci_dev_lock(hdev); 55431ad1691SAndrzej Kaczmarek *val = hdev->conn_info_min_age; 55531ad1691SAndrzej Kaczmarek hci_dev_unlock(hdev); 55631ad1691SAndrzej Kaczmarek 55731ad1691SAndrzej Kaczmarek return 0; 55831ad1691SAndrzej Kaczmarek } 55931ad1691SAndrzej Kaczmarek 56031ad1691SAndrzej Kaczmarek DEFINE_SIMPLE_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get, 56131ad1691SAndrzej Kaczmarek conn_info_min_age_set, "%llu\n"); 56231ad1691SAndrzej Kaczmarek 56331ad1691SAndrzej Kaczmarek static int conn_info_max_age_set(void *data, u64 val) 56431ad1691SAndrzej Kaczmarek { 56531ad1691SAndrzej Kaczmarek struct hci_dev *hdev = data; 56631ad1691SAndrzej Kaczmarek 56731ad1691SAndrzej Kaczmarek if (val == 0 || val < hdev->conn_info_min_age) 56831ad1691SAndrzej Kaczmarek return -EINVAL; 56931ad1691SAndrzej Kaczmarek 57031ad1691SAndrzej Kaczmarek hci_dev_lock(hdev); 57131ad1691SAndrzej Kaczmarek hdev->conn_info_max_age = val; 57231ad1691SAndrzej Kaczmarek hci_dev_unlock(hdev); 57331ad1691SAndrzej Kaczmarek 57431ad1691SAndrzej Kaczmarek return 0; 57531ad1691SAndrzej Kaczmarek } 57631ad1691SAndrzej Kaczmarek 57731ad1691SAndrzej Kaczmarek static int conn_info_max_age_get(void *data, u64 *val) 57831ad1691SAndrzej Kaczmarek { 57931ad1691SAndrzej Kaczmarek struct hci_dev *hdev = data; 58031ad1691SAndrzej Kaczmarek 58131ad1691SAndrzej Kaczmarek hci_dev_lock(hdev); 58231ad1691SAndrzej Kaczmarek *val = hdev->conn_info_max_age; 58331ad1691SAndrzej Kaczmarek hci_dev_unlock(hdev); 58431ad1691SAndrzej Kaczmarek 58531ad1691SAndrzej Kaczmarek return 0; 58631ad1691SAndrzej Kaczmarek } 58731ad1691SAndrzej Kaczmarek 58831ad1691SAndrzej Kaczmarek DEFINE_SIMPLE_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get, 58931ad1691SAndrzej Kaczmarek conn_info_max_age_set, "%llu\n"); 59031ad1691SAndrzej Kaczmarek 591ac345813SMarcel Holtmann static int identity_show(struct seq_file *f, void *p) 592ac345813SMarcel Holtmann { 593ac345813SMarcel Holtmann struct hci_dev *hdev = f->private; 594a1f4c318SJohan Hedberg bdaddr_t addr; 595ac345813SMarcel Holtmann u8 addr_type; 596ac345813SMarcel Holtmann 597ac345813SMarcel Holtmann hci_dev_lock(hdev); 598ac345813SMarcel Holtmann 599a1f4c318SJohan Hedberg hci_copy_identity_address(hdev, &addr, &addr_type); 600ac345813SMarcel Holtmann 601a1f4c318SJohan Hedberg seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type, 602473deef2SMarcel Holtmann 16, hdev->irk, &hdev->rpa); 603ac345813SMarcel Holtmann 604ac345813SMarcel Holtmann hci_dev_unlock(hdev); 605ac345813SMarcel Holtmann 606ac345813SMarcel Holtmann return 0; 607ac345813SMarcel Holtmann } 608ac345813SMarcel Holtmann 609ac345813SMarcel Holtmann static int identity_open(struct inode *inode, struct file *file) 610ac345813SMarcel Holtmann { 611ac345813SMarcel Holtmann return single_open(file, identity_show, inode->i_private); 612ac345813SMarcel Holtmann } 613ac345813SMarcel Holtmann 614ac345813SMarcel Holtmann static const struct file_operations identity_fops = { 615ac345813SMarcel Holtmann .open = identity_open, 616ac345813SMarcel Holtmann .read = seq_read, 617ac345813SMarcel Holtmann .llseek = seq_lseek, 618ac345813SMarcel Holtmann .release = single_release, 619ac345813SMarcel Holtmann }; 620ac345813SMarcel Holtmann 6217a4cd51dSMarcel Holtmann static int random_address_show(struct seq_file *f, void *p) 6227a4cd51dSMarcel Holtmann { 6237a4cd51dSMarcel Holtmann struct hci_dev *hdev = f->private; 6247a4cd51dSMarcel Holtmann 6257a4cd51dSMarcel Holtmann hci_dev_lock(hdev); 6267a4cd51dSMarcel Holtmann seq_printf(f, "%pMR\n", &hdev->random_addr); 6277a4cd51dSMarcel Holtmann hci_dev_unlock(hdev); 6287a4cd51dSMarcel Holtmann 6297a4cd51dSMarcel Holtmann return 0; 6307a4cd51dSMarcel Holtmann } 6317a4cd51dSMarcel Holtmann 6327a4cd51dSMarcel Holtmann static int random_address_open(struct inode *inode, struct file *file) 6337a4cd51dSMarcel Holtmann { 6347a4cd51dSMarcel Holtmann return single_open(file, random_address_show, inode->i_private); 6357a4cd51dSMarcel Holtmann } 6367a4cd51dSMarcel Holtmann 6377a4cd51dSMarcel Holtmann static const struct file_operations random_address_fops = { 6387a4cd51dSMarcel Holtmann .open = random_address_open, 6397a4cd51dSMarcel Holtmann .read = seq_read, 6407a4cd51dSMarcel Holtmann .llseek = seq_lseek, 6417a4cd51dSMarcel Holtmann .release = single_release, 6427a4cd51dSMarcel Holtmann }; 6437a4cd51dSMarcel Holtmann 644e7b8fc92SMarcel Holtmann static int static_address_show(struct seq_file *f, void *p) 645e7b8fc92SMarcel Holtmann { 646e7b8fc92SMarcel Holtmann struct hci_dev *hdev = f->private; 647e7b8fc92SMarcel Holtmann 648e7b8fc92SMarcel Holtmann hci_dev_lock(hdev); 649e7b8fc92SMarcel Holtmann seq_printf(f, "%pMR\n", &hdev->static_addr); 650e7b8fc92SMarcel Holtmann hci_dev_unlock(hdev); 651e7b8fc92SMarcel Holtmann 652e7b8fc92SMarcel Holtmann return 0; 653e7b8fc92SMarcel Holtmann } 654e7b8fc92SMarcel Holtmann 655e7b8fc92SMarcel Holtmann static int static_address_open(struct inode *inode, struct file *file) 656e7b8fc92SMarcel Holtmann { 657e7b8fc92SMarcel Holtmann return single_open(file, static_address_show, inode->i_private); 658e7b8fc92SMarcel Holtmann } 659e7b8fc92SMarcel Holtmann 660e7b8fc92SMarcel Holtmann static const struct file_operations static_address_fops = { 661e7b8fc92SMarcel Holtmann .open = static_address_open, 662e7b8fc92SMarcel Holtmann .read = seq_read, 663e7b8fc92SMarcel Holtmann .llseek = seq_lseek, 664e7b8fc92SMarcel Holtmann .release = single_release, 665e7b8fc92SMarcel Holtmann }; 666e7b8fc92SMarcel Holtmann 667b32bba6cSMarcel Holtmann static ssize_t force_static_address_read(struct file *file, 668b32bba6cSMarcel Holtmann char __user *user_buf, 669b32bba6cSMarcel Holtmann size_t count, loff_t *ppos) 67092202185SMarcel Holtmann { 671b32bba6cSMarcel Holtmann struct hci_dev *hdev = file->private_data; 672b32bba6cSMarcel Holtmann char buf[3]; 67392202185SMarcel Holtmann 674111902f7SMarcel Holtmann buf[0] = test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ? 'Y': 'N'; 675b32bba6cSMarcel Holtmann buf[1] = '\n'; 676b32bba6cSMarcel Holtmann buf[2] = '\0'; 677b32bba6cSMarcel Holtmann return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 678b32bba6cSMarcel Holtmann } 679b32bba6cSMarcel Holtmann 680b32bba6cSMarcel Holtmann static ssize_t force_static_address_write(struct file *file, 681b32bba6cSMarcel Holtmann const char __user *user_buf, 682b32bba6cSMarcel Holtmann size_t count, loff_t *ppos) 683b32bba6cSMarcel Holtmann { 684b32bba6cSMarcel Holtmann struct hci_dev *hdev = file->private_data; 685b32bba6cSMarcel Holtmann char buf[32]; 686b32bba6cSMarcel Holtmann size_t buf_size = min(count, (sizeof(buf)-1)); 687b32bba6cSMarcel Holtmann bool enable; 688b32bba6cSMarcel Holtmann 689b32bba6cSMarcel Holtmann if (test_bit(HCI_UP, &hdev->flags)) 690b32bba6cSMarcel Holtmann return -EBUSY; 691b32bba6cSMarcel Holtmann 692b32bba6cSMarcel Holtmann if (copy_from_user(buf, user_buf, buf_size)) 693b32bba6cSMarcel Holtmann return -EFAULT; 694b32bba6cSMarcel Holtmann 695b32bba6cSMarcel Holtmann buf[buf_size] = '\0'; 696b32bba6cSMarcel Holtmann if (strtobool(buf, &enable)) 69792202185SMarcel Holtmann return -EINVAL; 69892202185SMarcel Holtmann 699111902f7SMarcel Holtmann if (enable == test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags)) 700b32bba6cSMarcel Holtmann return -EALREADY; 70192202185SMarcel Holtmann 702111902f7SMarcel Holtmann change_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags); 703b32bba6cSMarcel Holtmann 704b32bba6cSMarcel Holtmann return count; 70592202185SMarcel Holtmann } 70692202185SMarcel Holtmann 707b32bba6cSMarcel Holtmann static const struct file_operations force_static_address_fops = { 708b32bba6cSMarcel Holtmann .open = simple_open, 709b32bba6cSMarcel Holtmann .read = force_static_address_read, 710b32bba6cSMarcel Holtmann .write = force_static_address_write, 711b32bba6cSMarcel Holtmann .llseek = default_llseek, 712b32bba6cSMarcel Holtmann }; 71392202185SMarcel Holtmann 714d2ab0ac1SMarcel Holtmann static int white_list_show(struct seq_file *f, void *ptr) 715d2ab0ac1SMarcel Holtmann { 716d2ab0ac1SMarcel Holtmann struct hci_dev *hdev = f->private; 717d2ab0ac1SMarcel Holtmann struct bdaddr_list *b; 718d2ab0ac1SMarcel Holtmann 719d2ab0ac1SMarcel Holtmann hci_dev_lock(hdev); 720d2ab0ac1SMarcel Holtmann list_for_each_entry(b, &hdev->le_white_list, list) 721d2ab0ac1SMarcel Holtmann seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type); 722d2ab0ac1SMarcel Holtmann hci_dev_unlock(hdev); 723d2ab0ac1SMarcel Holtmann 724d2ab0ac1SMarcel Holtmann return 0; 725d2ab0ac1SMarcel Holtmann } 726d2ab0ac1SMarcel Holtmann 727d2ab0ac1SMarcel Holtmann static int white_list_open(struct inode *inode, struct file *file) 728d2ab0ac1SMarcel Holtmann { 729d2ab0ac1SMarcel Holtmann return single_open(file, white_list_show, inode->i_private); 730d2ab0ac1SMarcel Holtmann } 731d2ab0ac1SMarcel Holtmann 732d2ab0ac1SMarcel Holtmann static const struct file_operations white_list_fops = { 733d2ab0ac1SMarcel Holtmann .open = white_list_open, 734d2ab0ac1SMarcel Holtmann .read = seq_read, 735d2ab0ac1SMarcel Holtmann .llseek = seq_lseek, 736d2ab0ac1SMarcel Holtmann .release = single_release, 737d2ab0ac1SMarcel Holtmann }; 738d2ab0ac1SMarcel Holtmann 7393698d704SMarcel Holtmann static int identity_resolving_keys_show(struct seq_file *f, void *ptr) 7403698d704SMarcel Holtmann { 7413698d704SMarcel Holtmann struct hci_dev *hdev = f->private; 7423698d704SMarcel Holtmann struct list_head *p, *n; 7433698d704SMarcel Holtmann 7443698d704SMarcel Holtmann hci_dev_lock(hdev); 7453698d704SMarcel Holtmann list_for_each_safe(p, n, &hdev->identity_resolving_keys) { 7463698d704SMarcel Holtmann struct smp_irk *irk = list_entry(p, struct smp_irk, list); 7473698d704SMarcel Holtmann seq_printf(f, "%pMR (type %u) %*phN %pMR\n", 7483698d704SMarcel Holtmann &irk->bdaddr, irk->addr_type, 7493698d704SMarcel Holtmann 16, irk->val, &irk->rpa); 7503698d704SMarcel Holtmann } 7513698d704SMarcel Holtmann hci_dev_unlock(hdev); 7523698d704SMarcel Holtmann 7533698d704SMarcel Holtmann return 0; 7543698d704SMarcel Holtmann } 7553698d704SMarcel Holtmann 7563698d704SMarcel Holtmann static int identity_resolving_keys_open(struct inode *inode, struct file *file) 7573698d704SMarcel Holtmann { 7583698d704SMarcel Holtmann return single_open(file, identity_resolving_keys_show, 7593698d704SMarcel Holtmann inode->i_private); 7603698d704SMarcel Holtmann } 7613698d704SMarcel Holtmann 7623698d704SMarcel Holtmann static const struct file_operations identity_resolving_keys_fops = { 7633698d704SMarcel Holtmann .open = identity_resolving_keys_open, 7643698d704SMarcel Holtmann .read = seq_read, 7653698d704SMarcel Holtmann .llseek = seq_lseek, 7663698d704SMarcel Holtmann .release = single_release, 7673698d704SMarcel Holtmann }; 7683698d704SMarcel Holtmann 7698f8625cdSMarcel Holtmann static int long_term_keys_show(struct seq_file *f, void *ptr) 7708f8625cdSMarcel Holtmann { 7718f8625cdSMarcel Holtmann struct hci_dev *hdev = f->private; 7728f8625cdSMarcel Holtmann struct list_head *p, *n; 7738f8625cdSMarcel Holtmann 7748f8625cdSMarcel Holtmann hci_dev_lock(hdev); 775f813f1beSJohan Hedberg list_for_each_safe(p, n, &hdev->long_term_keys) { 7768f8625cdSMarcel Holtmann struct smp_ltk *ltk = list_entry(p, struct smp_ltk, list); 777fe39c7b2SMarcel Holtmann seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n", 7788f8625cdSMarcel Holtmann <k->bdaddr, ltk->bdaddr_type, ltk->authenticated, 7798f8625cdSMarcel Holtmann ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv), 780fe39c7b2SMarcel Holtmann __le64_to_cpu(ltk->rand), 16, ltk->val); 7818f8625cdSMarcel Holtmann } 7828f8625cdSMarcel Holtmann hci_dev_unlock(hdev); 7838f8625cdSMarcel Holtmann 7848f8625cdSMarcel Holtmann return 0; 7858f8625cdSMarcel Holtmann } 7868f8625cdSMarcel Holtmann 7878f8625cdSMarcel Holtmann static int long_term_keys_open(struct inode *inode, struct file *file) 7888f8625cdSMarcel Holtmann { 7898f8625cdSMarcel Holtmann return single_open(file, long_term_keys_show, inode->i_private); 7908f8625cdSMarcel Holtmann } 7918f8625cdSMarcel Holtmann 7928f8625cdSMarcel Holtmann static const struct file_operations long_term_keys_fops = { 7938f8625cdSMarcel Holtmann .open = long_term_keys_open, 7948f8625cdSMarcel Holtmann .read = seq_read, 7958f8625cdSMarcel Holtmann .llseek = seq_lseek, 7968f8625cdSMarcel Holtmann .release = single_release, 7978f8625cdSMarcel Holtmann }; 7988f8625cdSMarcel Holtmann 7994e70c7e7SMarcel Holtmann static int conn_min_interval_set(void *data, u64 val) 8004e70c7e7SMarcel Holtmann { 8014e70c7e7SMarcel Holtmann struct hci_dev *hdev = data; 8024e70c7e7SMarcel Holtmann 8034e70c7e7SMarcel Holtmann if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval) 8044e70c7e7SMarcel Holtmann return -EINVAL; 8054e70c7e7SMarcel Holtmann 8064e70c7e7SMarcel Holtmann hci_dev_lock(hdev); 8074e70c7e7SMarcel Holtmann hdev->le_conn_min_interval = val; 8084e70c7e7SMarcel Holtmann hci_dev_unlock(hdev); 8094e70c7e7SMarcel Holtmann 8104e70c7e7SMarcel Holtmann return 0; 8114e70c7e7SMarcel Holtmann } 8124e70c7e7SMarcel Holtmann 8134e70c7e7SMarcel Holtmann static int conn_min_interval_get(void *data, u64 *val) 8144e70c7e7SMarcel Holtmann { 8154e70c7e7SMarcel Holtmann struct hci_dev *hdev = data; 8164e70c7e7SMarcel Holtmann 8174e70c7e7SMarcel Holtmann hci_dev_lock(hdev); 8184e70c7e7SMarcel Holtmann *val = hdev->le_conn_min_interval; 8194e70c7e7SMarcel Holtmann hci_dev_unlock(hdev); 8204e70c7e7SMarcel Holtmann 8214e70c7e7SMarcel Holtmann return 0; 8224e70c7e7SMarcel Holtmann } 8234e70c7e7SMarcel Holtmann 8244e70c7e7SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get, 8254e70c7e7SMarcel Holtmann conn_min_interval_set, "%llu\n"); 8264e70c7e7SMarcel Holtmann 8274e70c7e7SMarcel Holtmann static int conn_max_interval_set(void *data, u64 val) 8284e70c7e7SMarcel Holtmann { 8294e70c7e7SMarcel Holtmann struct hci_dev *hdev = data; 8304e70c7e7SMarcel Holtmann 8314e70c7e7SMarcel Holtmann if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval) 8324e70c7e7SMarcel Holtmann return -EINVAL; 8334e70c7e7SMarcel Holtmann 8344e70c7e7SMarcel Holtmann hci_dev_lock(hdev); 8354e70c7e7SMarcel Holtmann hdev->le_conn_max_interval = val; 8364e70c7e7SMarcel Holtmann hci_dev_unlock(hdev); 8374e70c7e7SMarcel Holtmann 8384e70c7e7SMarcel Holtmann return 0; 8394e70c7e7SMarcel Holtmann } 8404e70c7e7SMarcel Holtmann 8414e70c7e7SMarcel Holtmann static int conn_max_interval_get(void *data, u64 *val) 8424e70c7e7SMarcel Holtmann { 8434e70c7e7SMarcel Holtmann struct hci_dev *hdev = data; 8444e70c7e7SMarcel Holtmann 8454e70c7e7SMarcel Holtmann hci_dev_lock(hdev); 8464e70c7e7SMarcel Holtmann *val = hdev->le_conn_max_interval; 8474e70c7e7SMarcel Holtmann hci_dev_unlock(hdev); 8484e70c7e7SMarcel Holtmann 8494e70c7e7SMarcel Holtmann return 0; 8504e70c7e7SMarcel Holtmann } 8514e70c7e7SMarcel Holtmann 8524e70c7e7SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get, 8534e70c7e7SMarcel Holtmann conn_max_interval_set, "%llu\n"); 8544e70c7e7SMarcel Holtmann 855816a93d1SMarcel Holtmann static int conn_latency_set(void *data, u64 val) 856816a93d1SMarcel Holtmann { 857816a93d1SMarcel Holtmann struct hci_dev *hdev = data; 858816a93d1SMarcel Holtmann 859816a93d1SMarcel Holtmann if (val > 0x01f3) 860816a93d1SMarcel Holtmann return -EINVAL; 861816a93d1SMarcel Holtmann 862816a93d1SMarcel Holtmann hci_dev_lock(hdev); 863816a93d1SMarcel Holtmann hdev->le_conn_latency = val; 864816a93d1SMarcel Holtmann hci_dev_unlock(hdev); 865816a93d1SMarcel Holtmann 866816a93d1SMarcel Holtmann return 0; 867816a93d1SMarcel Holtmann } 868816a93d1SMarcel Holtmann 869816a93d1SMarcel Holtmann static int conn_latency_get(void *data, u64 *val) 870816a93d1SMarcel Holtmann { 871816a93d1SMarcel Holtmann struct hci_dev *hdev = data; 872816a93d1SMarcel Holtmann 873816a93d1SMarcel Holtmann hci_dev_lock(hdev); 874816a93d1SMarcel Holtmann *val = hdev->le_conn_latency; 875816a93d1SMarcel Holtmann hci_dev_unlock(hdev); 876816a93d1SMarcel Holtmann 877816a93d1SMarcel Holtmann return 0; 878816a93d1SMarcel Holtmann } 879816a93d1SMarcel Holtmann 880816a93d1SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(conn_latency_fops, conn_latency_get, 881816a93d1SMarcel Holtmann conn_latency_set, "%llu\n"); 882816a93d1SMarcel Holtmann 883f1649577SMarcel Holtmann static int supervision_timeout_set(void *data, u64 val) 884f1649577SMarcel Holtmann { 885f1649577SMarcel Holtmann struct hci_dev *hdev = data; 886f1649577SMarcel Holtmann 887f1649577SMarcel Holtmann if (val < 0x000a || val > 0x0c80) 888f1649577SMarcel Holtmann return -EINVAL; 889f1649577SMarcel Holtmann 890f1649577SMarcel Holtmann hci_dev_lock(hdev); 891f1649577SMarcel Holtmann hdev->le_supv_timeout = val; 892f1649577SMarcel Holtmann hci_dev_unlock(hdev); 893f1649577SMarcel Holtmann 894f1649577SMarcel Holtmann return 0; 895f1649577SMarcel Holtmann } 896f1649577SMarcel Holtmann 897f1649577SMarcel Holtmann static int supervision_timeout_get(void *data, u64 *val) 898f1649577SMarcel Holtmann { 899f1649577SMarcel Holtmann struct hci_dev *hdev = data; 900f1649577SMarcel Holtmann 901f1649577SMarcel Holtmann hci_dev_lock(hdev); 902f1649577SMarcel Holtmann *val = hdev->le_supv_timeout; 903f1649577SMarcel Holtmann hci_dev_unlock(hdev); 904f1649577SMarcel Holtmann 905f1649577SMarcel Holtmann return 0; 906f1649577SMarcel Holtmann } 907f1649577SMarcel Holtmann 908f1649577SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get, 909f1649577SMarcel Holtmann supervision_timeout_set, "%llu\n"); 910f1649577SMarcel Holtmann 9113f959d46SMarcel Holtmann static int adv_channel_map_set(void *data, u64 val) 9123f959d46SMarcel Holtmann { 9133f959d46SMarcel Holtmann struct hci_dev *hdev = data; 9143f959d46SMarcel Holtmann 9153f959d46SMarcel Holtmann if (val < 0x01 || val > 0x07) 9163f959d46SMarcel Holtmann return -EINVAL; 9173f959d46SMarcel Holtmann 9183f959d46SMarcel Holtmann hci_dev_lock(hdev); 9193f959d46SMarcel Holtmann hdev->le_adv_channel_map = val; 9203f959d46SMarcel Holtmann hci_dev_unlock(hdev); 9213f959d46SMarcel Holtmann 9223f959d46SMarcel Holtmann return 0; 9233f959d46SMarcel Holtmann } 9243f959d46SMarcel Holtmann 9253f959d46SMarcel Holtmann static int adv_channel_map_get(void *data, u64 *val) 9263f959d46SMarcel Holtmann { 9273f959d46SMarcel Holtmann struct hci_dev *hdev = data; 9283f959d46SMarcel Holtmann 9293f959d46SMarcel Holtmann hci_dev_lock(hdev); 9303f959d46SMarcel Holtmann *val = hdev->le_adv_channel_map; 9313f959d46SMarcel Holtmann hci_dev_unlock(hdev); 9323f959d46SMarcel Holtmann 9333f959d46SMarcel Holtmann return 0; 9343f959d46SMarcel Holtmann } 9353f959d46SMarcel Holtmann 9363f959d46SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get, 9373f959d46SMarcel Holtmann adv_channel_map_set, "%llu\n"); 9383f959d46SMarcel Holtmann 9390b3c7d37SMarcel Holtmann static int device_list_show(struct seq_file *f, void *ptr) 9407d474e06SAndre Guedes { 9410b3c7d37SMarcel Holtmann struct hci_dev *hdev = f->private; 9427d474e06SAndre Guedes struct hci_conn_params *p; 9437d474e06SAndre Guedes 9447d474e06SAndre Guedes hci_dev_lock(hdev); 9457d474e06SAndre Guedes list_for_each_entry(p, &hdev->le_conn_params, list) { 9460b3c7d37SMarcel Holtmann seq_printf(f, "%pMR %u %u\n", &p->addr, p->addr_type, 9477d474e06SAndre Guedes p->auto_connect); 9487d474e06SAndre Guedes } 9497d474e06SAndre Guedes hci_dev_unlock(hdev); 9507d474e06SAndre Guedes 9517d474e06SAndre Guedes return 0; 9527d474e06SAndre Guedes } 9537d474e06SAndre Guedes 9540b3c7d37SMarcel Holtmann static int device_list_open(struct inode *inode, struct file *file) 9557d474e06SAndre Guedes { 9560b3c7d37SMarcel Holtmann return single_open(file, device_list_show, inode->i_private); 9577d474e06SAndre Guedes } 9587d474e06SAndre Guedes 9590b3c7d37SMarcel Holtmann static const struct file_operations device_list_fops = { 9600b3c7d37SMarcel Holtmann .open = device_list_open, 9617d474e06SAndre Guedes .read = seq_read, 9627d474e06SAndre Guedes .llseek = seq_lseek, 9637d474e06SAndre Guedes .release = single_release, 9647d474e06SAndre Guedes }; 9657d474e06SAndre Guedes 9661da177e4SLinus Torvalds /* ---- HCI requests ---- */ 9671da177e4SLinus Torvalds 96842c6b129SJohan Hedberg static void hci_req_sync_complete(struct hci_dev *hdev, u8 result) 9691da177e4SLinus Torvalds { 97042c6b129SJohan Hedberg BT_DBG("%s result 0x%2.2x", hdev->name, result); 97175fb0e32SJohan Hedberg 9721da177e4SLinus Torvalds if (hdev->req_status == HCI_REQ_PEND) { 9731da177e4SLinus Torvalds hdev->req_result = result; 9741da177e4SLinus Torvalds hdev->req_status = HCI_REQ_DONE; 9751da177e4SLinus Torvalds wake_up_interruptible(&hdev->req_wait_q); 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds static void hci_req_cancel(struct hci_dev *hdev, int err) 9801da177e4SLinus Torvalds { 9811da177e4SLinus Torvalds BT_DBG("%s err 0x%2.2x", hdev->name, err); 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds if (hdev->req_status == HCI_REQ_PEND) { 9841da177e4SLinus Torvalds hdev->req_result = err; 9851da177e4SLinus Torvalds hdev->req_status = HCI_REQ_CANCELED; 9861da177e4SLinus Torvalds wake_up_interruptible(&hdev->req_wait_q); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 99077a63e0aSFengguang Wu static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode, 99177a63e0aSFengguang Wu u8 event) 99275e84b7cSJohan Hedberg { 99375e84b7cSJohan Hedberg struct hci_ev_cmd_complete *ev; 99475e84b7cSJohan Hedberg struct hci_event_hdr *hdr; 99575e84b7cSJohan Hedberg struct sk_buff *skb; 99675e84b7cSJohan Hedberg 99775e84b7cSJohan Hedberg hci_dev_lock(hdev); 99875e84b7cSJohan Hedberg 99975e84b7cSJohan Hedberg skb = hdev->recv_evt; 100075e84b7cSJohan Hedberg hdev->recv_evt = NULL; 100175e84b7cSJohan Hedberg 100275e84b7cSJohan Hedberg hci_dev_unlock(hdev); 100375e84b7cSJohan Hedberg 100475e84b7cSJohan Hedberg if (!skb) 100575e84b7cSJohan Hedberg return ERR_PTR(-ENODATA); 100675e84b7cSJohan Hedberg 100775e84b7cSJohan Hedberg if (skb->len < sizeof(*hdr)) { 100875e84b7cSJohan Hedberg BT_ERR("Too short HCI event"); 100975e84b7cSJohan Hedberg goto failed; 101075e84b7cSJohan Hedberg } 101175e84b7cSJohan Hedberg 101275e84b7cSJohan Hedberg hdr = (void *) skb->data; 101375e84b7cSJohan Hedberg skb_pull(skb, HCI_EVENT_HDR_SIZE); 101475e84b7cSJohan Hedberg 10157b1abbbeSJohan Hedberg if (event) { 10167b1abbbeSJohan Hedberg if (hdr->evt != event) 10177b1abbbeSJohan Hedberg goto failed; 10187b1abbbeSJohan Hedberg return skb; 10197b1abbbeSJohan Hedberg } 10207b1abbbeSJohan Hedberg 102175e84b7cSJohan Hedberg if (hdr->evt != HCI_EV_CMD_COMPLETE) { 102275e84b7cSJohan Hedberg BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt); 102375e84b7cSJohan Hedberg goto failed; 102475e84b7cSJohan Hedberg } 102575e84b7cSJohan Hedberg 102675e84b7cSJohan Hedberg if (skb->len < sizeof(*ev)) { 102775e84b7cSJohan Hedberg BT_ERR("Too short cmd_complete event"); 102875e84b7cSJohan Hedberg goto failed; 102975e84b7cSJohan Hedberg } 103075e84b7cSJohan Hedberg 103175e84b7cSJohan Hedberg ev = (void *) skb->data; 103275e84b7cSJohan Hedberg skb_pull(skb, sizeof(*ev)); 103375e84b7cSJohan Hedberg 103475e84b7cSJohan Hedberg if (opcode == __le16_to_cpu(ev->opcode)) 103575e84b7cSJohan Hedberg return skb; 103675e84b7cSJohan Hedberg 103775e84b7cSJohan Hedberg BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode, 103875e84b7cSJohan Hedberg __le16_to_cpu(ev->opcode)); 103975e84b7cSJohan Hedberg 104075e84b7cSJohan Hedberg failed: 104175e84b7cSJohan Hedberg kfree_skb(skb); 104275e84b7cSJohan Hedberg return ERR_PTR(-ENODATA); 104375e84b7cSJohan Hedberg } 104475e84b7cSJohan Hedberg 10457b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, 104607dc93ddSJohan Hedberg const void *param, u8 event, u32 timeout) 104775e84b7cSJohan Hedberg { 104875e84b7cSJohan Hedberg DECLARE_WAITQUEUE(wait, current); 104975e84b7cSJohan Hedberg struct hci_request req; 105075e84b7cSJohan Hedberg int err = 0; 105175e84b7cSJohan Hedberg 105275e84b7cSJohan Hedberg BT_DBG("%s", hdev->name); 105375e84b7cSJohan Hedberg 105475e84b7cSJohan Hedberg hci_req_init(&req, hdev); 105575e84b7cSJohan Hedberg 10567b1abbbeSJohan Hedberg hci_req_add_ev(&req, opcode, plen, param, event); 105775e84b7cSJohan Hedberg 105875e84b7cSJohan Hedberg hdev->req_status = HCI_REQ_PEND; 105975e84b7cSJohan Hedberg 106075e84b7cSJohan Hedberg err = hci_req_run(&req, hci_req_sync_complete); 106175e84b7cSJohan Hedberg if (err < 0) 106275e84b7cSJohan Hedberg return ERR_PTR(err); 106375e84b7cSJohan Hedberg 106475e84b7cSJohan Hedberg add_wait_queue(&hdev->req_wait_q, &wait); 106575e84b7cSJohan Hedberg set_current_state(TASK_INTERRUPTIBLE); 106675e84b7cSJohan Hedberg 106775e84b7cSJohan Hedberg schedule_timeout(timeout); 106875e84b7cSJohan Hedberg 106975e84b7cSJohan Hedberg remove_wait_queue(&hdev->req_wait_q, &wait); 107075e84b7cSJohan Hedberg 107175e84b7cSJohan Hedberg if (signal_pending(current)) 107275e84b7cSJohan Hedberg return ERR_PTR(-EINTR); 107375e84b7cSJohan Hedberg 107475e84b7cSJohan Hedberg switch (hdev->req_status) { 107575e84b7cSJohan Hedberg case HCI_REQ_DONE: 107675e84b7cSJohan Hedberg err = -bt_to_errno(hdev->req_result); 107775e84b7cSJohan Hedberg break; 107875e84b7cSJohan Hedberg 107975e84b7cSJohan Hedberg case HCI_REQ_CANCELED: 108075e84b7cSJohan Hedberg err = -hdev->req_result; 108175e84b7cSJohan Hedberg break; 108275e84b7cSJohan Hedberg 108375e84b7cSJohan Hedberg default: 108475e84b7cSJohan Hedberg err = -ETIMEDOUT; 108575e84b7cSJohan Hedberg break; 108675e84b7cSJohan Hedberg } 108775e84b7cSJohan Hedberg 108875e84b7cSJohan Hedberg hdev->req_status = hdev->req_result = 0; 108975e84b7cSJohan Hedberg 109075e84b7cSJohan Hedberg BT_DBG("%s end: err %d", hdev->name, err); 109175e84b7cSJohan Hedberg 109275e84b7cSJohan Hedberg if (err < 0) 109375e84b7cSJohan Hedberg return ERR_PTR(err); 109475e84b7cSJohan Hedberg 10957b1abbbeSJohan Hedberg return hci_get_cmd_complete(hdev, opcode, event); 10967b1abbbeSJohan Hedberg } 10977b1abbbeSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync_ev); 10987b1abbbeSJohan Hedberg 10997b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 110007dc93ddSJohan Hedberg const void *param, u32 timeout) 11017b1abbbeSJohan Hedberg { 11027b1abbbeSJohan Hedberg return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); 110375e84b7cSJohan Hedberg } 110475e84b7cSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync); 110575e84b7cSJohan Hedberg 11061da177e4SLinus Torvalds /* Execute request and wait for completion. */ 110701178cd4SJohan Hedberg static int __hci_req_sync(struct hci_dev *hdev, 110842c6b129SJohan Hedberg void (*func)(struct hci_request *req, 110942c6b129SJohan Hedberg unsigned long opt), 11101da177e4SLinus Torvalds unsigned long opt, __u32 timeout) 11111da177e4SLinus Torvalds { 111242c6b129SJohan Hedberg struct hci_request req; 11131da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 11141da177e4SLinus Torvalds int err = 0; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds BT_DBG("%s start", hdev->name); 11171da177e4SLinus Torvalds 111842c6b129SJohan Hedberg hci_req_init(&req, hdev); 111942c6b129SJohan Hedberg 11201da177e4SLinus Torvalds hdev->req_status = HCI_REQ_PEND; 11211da177e4SLinus Torvalds 112242c6b129SJohan Hedberg func(&req, opt); 112353cce22dSJohan Hedberg 112442c6b129SJohan Hedberg err = hci_req_run(&req, hci_req_sync_complete); 112542c6b129SJohan Hedberg if (err < 0) { 112653cce22dSJohan Hedberg hdev->req_status = 0; 1127920c8300SAndre Guedes 1128920c8300SAndre Guedes /* ENODATA means the HCI request command queue is empty. 1129920c8300SAndre Guedes * This can happen when a request with conditionals doesn't 1130920c8300SAndre Guedes * trigger any commands to be sent. This is normal behavior 1131920c8300SAndre Guedes * and should not trigger an error return. 113242c6b129SJohan Hedberg */ 1133920c8300SAndre Guedes if (err == -ENODATA) 113442c6b129SJohan Hedberg return 0; 1135920c8300SAndre Guedes 1136920c8300SAndre Guedes return err; 113753cce22dSJohan Hedberg } 113853cce22dSJohan Hedberg 1139bc4445c7SAndre Guedes add_wait_queue(&hdev->req_wait_q, &wait); 1140bc4445c7SAndre Guedes set_current_state(TASK_INTERRUPTIBLE); 1141bc4445c7SAndre Guedes 11421da177e4SLinus Torvalds schedule_timeout(timeout); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds remove_wait_queue(&hdev->req_wait_q, &wait); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds if (signal_pending(current)) 11471da177e4SLinus Torvalds return -EINTR; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds switch (hdev->req_status) { 11501da177e4SLinus Torvalds case HCI_REQ_DONE: 1151e175072fSJoe Perches err = -bt_to_errno(hdev->req_result); 11521da177e4SLinus Torvalds break; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds case HCI_REQ_CANCELED: 11551da177e4SLinus Torvalds err = -hdev->req_result; 11561da177e4SLinus Torvalds break; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds default: 11591da177e4SLinus Torvalds err = -ETIMEDOUT; 11601da177e4SLinus Torvalds break; 11613ff50b79SStephen Hemminger } 11621da177e4SLinus Torvalds 1163a5040efaSJohan Hedberg hdev->req_status = hdev->req_result = 0; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds BT_DBG("%s end: err %d", hdev->name, err); 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds return err; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 117001178cd4SJohan Hedberg static int hci_req_sync(struct hci_dev *hdev, 117142c6b129SJohan Hedberg void (*req)(struct hci_request *req, 117242c6b129SJohan Hedberg unsigned long opt), 11731da177e4SLinus Torvalds unsigned long opt, __u32 timeout) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds int ret; 11761da177e4SLinus Torvalds 11777c6a329eSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) 11787c6a329eSMarcel Holtmann return -ENETDOWN; 11797c6a329eSMarcel Holtmann 11801da177e4SLinus Torvalds /* Serialize all requests */ 11811da177e4SLinus Torvalds hci_req_lock(hdev); 118201178cd4SJohan Hedberg ret = __hci_req_sync(hdev, req, opt, timeout); 11831da177e4SLinus Torvalds hci_req_unlock(hdev); 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds return ret; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 118842c6b129SJohan Hedberg static void hci_reset_req(struct hci_request *req, unsigned long opt) 11891da177e4SLinus Torvalds { 119042c6b129SJohan Hedberg BT_DBG("%s %ld", req->hdev->name, opt); 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds /* Reset device */ 119342c6b129SJohan Hedberg set_bit(HCI_RESET, &req->hdev->flags); 119442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_RESET, 0, NULL); 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 119742c6b129SJohan Hedberg static void bredr_init(struct hci_request *req) 11981da177e4SLinus Torvalds { 119942c6b129SJohan Hedberg req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; 12002455a3eaSAndrei Emeltchenko 12011da177e4SLinus Torvalds /* Read Local Supported Features */ 120242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); 12031da177e4SLinus Torvalds 12041143e5a6SMarcel Holtmann /* Read Local Version */ 120542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 12062177bab5SJohan Hedberg 12072177bab5SJohan Hedberg /* Read BD Address */ 120842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 121142c6b129SJohan Hedberg static void amp_init(struct hci_request *req) 1212e61ef499SAndrei Emeltchenko { 121342c6b129SJohan Hedberg req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; 12142455a3eaSAndrei Emeltchenko 1215e61ef499SAndrei Emeltchenko /* Read Local Version */ 121642c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 12176bcbc489SAndrei Emeltchenko 1218f6996cfeSMarcel Holtmann /* Read Local Supported Commands */ 1219f6996cfeSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 1220f6996cfeSMarcel Holtmann 1221f6996cfeSMarcel Holtmann /* Read Local Supported Features */ 1222f6996cfeSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); 1223f6996cfeSMarcel Holtmann 12246bcbc489SAndrei Emeltchenko /* Read Local AMP Info */ 122542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); 1226e71dfabaSAndrei Emeltchenko 1227e71dfabaSAndrei Emeltchenko /* Read Data Blk size */ 122842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); 12297528ca1cSMarcel Holtmann 1230f38ba941SMarcel Holtmann /* Read Flow Control Mode */ 1231f38ba941SMarcel Holtmann hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); 1232f38ba941SMarcel Holtmann 12337528ca1cSMarcel Holtmann /* Read Location Data */ 12347528ca1cSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); 1235e61ef499SAndrei Emeltchenko } 1236e61ef499SAndrei Emeltchenko 123742c6b129SJohan Hedberg static void hci_init1_req(struct hci_request *req, unsigned long opt) 1238e61ef499SAndrei Emeltchenko { 123942c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 1240e61ef499SAndrei Emeltchenko 1241e61ef499SAndrei Emeltchenko BT_DBG("%s %ld", hdev->name, opt); 1242e61ef499SAndrei Emeltchenko 124311778716SAndrei Emeltchenko /* Reset */ 124411778716SAndrei Emeltchenko if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) 124542c6b129SJohan Hedberg hci_reset_req(req, 0); 124611778716SAndrei Emeltchenko 1247e61ef499SAndrei Emeltchenko switch (hdev->dev_type) { 1248e61ef499SAndrei Emeltchenko case HCI_BREDR: 124942c6b129SJohan Hedberg bredr_init(req); 1250e61ef499SAndrei Emeltchenko break; 1251e61ef499SAndrei Emeltchenko 1252e61ef499SAndrei Emeltchenko case HCI_AMP: 125342c6b129SJohan Hedberg amp_init(req); 1254e61ef499SAndrei Emeltchenko break; 1255e61ef499SAndrei Emeltchenko 1256e61ef499SAndrei Emeltchenko default: 1257e61ef499SAndrei Emeltchenko BT_ERR("Unknown device type %d", hdev->dev_type); 1258e61ef499SAndrei Emeltchenko break; 1259e61ef499SAndrei Emeltchenko } 1260e61ef499SAndrei Emeltchenko } 1261e61ef499SAndrei Emeltchenko 126242c6b129SJohan Hedberg static void bredr_setup(struct hci_request *req) 12632177bab5SJohan Hedberg { 12644ca048e3SMarcel Holtmann struct hci_dev *hdev = req->hdev; 12654ca048e3SMarcel Holtmann 12662177bab5SJohan Hedberg __le16 param; 12672177bab5SJohan Hedberg __u8 flt_type; 12682177bab5SJohan Hedberg 12692177bab5SJohan Hedberg /* Read Buffer Size (ACL mtu, max pkt, etc.) */ 127042c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); 12712177bab5SJohan Hedberg 12722177bab5SJohan Hedberg /* Read Class of Device */ 127342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); 12742177bab5SJohan Hedberg 12752177bab5SJohan Hedberg /* Read Local Name */ 127642c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); 12772177bab5SJohan Hedberg 12782177bab5SJohan Hedberg /* Read Voice Setting */ 127942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); 12802177bab5SJohan Hedberg 1281b4cb9fb2SMarcel Holtmann /* Read Number of Supported IAC */ 1282b4cb9fb2SMarcel Holtmann hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); 1283b4cb9fb2SMarcel Holtmann 12844b836f39SMarcel Holtmann /* Read Current IAC LAP */ 12854b836f39SMarcel Holtmann hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); 12864b836f39SMarcel Holtmann 12872177bab5SJohan Hedberg /* Clear Event Filters */ 12882177bab5SJohan Hedberg flt_type = HCI_FLT_CLEAR_ALL; 128942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); 12902177bab5SJohan Hedberg 12912177bab5SJohan Hedberg /* Connection accept timeout ~20 secs */ 1292dcf4adbfSJoe Perches param = cpu_to_le16(0x7d00); 129342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); 12942177bab5SJohan Hedberg 12954ca048e3SMarcel Holtmann /* AVM Berlin (31), aka "BlueFRITZ!", reports version 1.2, 12964ca048e3SMarcel Holtmann * but it does not support page scan related HCI commands. 12974ca048e3SMarcel Holtmann */ 12984ca048e3SMarcel Holtmann if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) { 1299f332ec66SJohan Hedberg hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); 1300f332ec66SJohan Hedberg hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); 1301f332ec66SJohan Hedberg } 13022177bab5SJohan Hedberg } 13032177bab5SJohan Hedberg 130442c6b129SJohan Hedberg static void le_setup(struct hci_request *req) 13052177bab5SJohan Hedberg { 1306c73eee91SJohan Hedberg struct hci_dev *hdev = req->hdev; 1307c73eee91SJohan Hedberg 13082177bab5SJohan Hedberg /* Read LE Buffer Size */ 130942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); 13102177bab5SJohan Hedberg 13112177bab5SJohan Hedberg /* Read LE Local Supported Features */ 131242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); 13132177bab5SJohan Hedberg 1314747d3f03SMarcel Holtmann /* Read LE Supported States */ 1315747d3f03SMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); 1316747d3f03SMarcel Holtmann 13172177bab5SJohan Hedberg /* Read LE Advertising Channel TX Power */ 131842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); 13192177bab5SJohan Hedberg 13202177bab5SJohan Hedberg /* Read LE White List Size */ 132142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); 13222177bab5SJohan Hedberg 1323747d3f03SMarcel Holtmann /* Clear LE White List */ 1324747d3f03SMarcel Holtmann hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL); 1325c73eee91SJohan Hedberg 1326c73eee91SJohan Hedberg /* LE-only controllers have LE implicitly enabled */ 1327c73eee91SJohan Hedberg if (!lmp_bredr_capable(hdev)) 1328c73eee91SJohan Hedberg set_bit(HCI_LE_ENABLED, &hdev->dev_flags); 13292177bab5SJohan Hedberg } 13302177bab5SJohan Hedberg 13312177bab5SJohan Hedberg static u8 hci_get_inquiry_mode(struct hci_dev *hdev) 13322177bab5SJohan Hedberg { 13332177bab5SJohan Hedberg if (lmp_ext_inq_capable(hdev)) 13342177bab5SJohan Hedberg return 0x02; 13352177bab5SJohan Hedberg 13362177bab5SJohan Hedberg if (lmp_inq_rssi_capable(hdev)) 13372177bab5SJohan Hedberg return 0x01; 13382177bab5SJohan Hedberg 13392177bab5SJohan Hedberg if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 && 13402177bab5SJohan Hedberg hdev->lmp_subver == 0x0757) 13412177bab5SJohan Hedberg return 0x01; 13422177bab5SJohan Hedberg 13432177bab5SJohan Hedberg if (hdev->manufacturer == 15) { 13442177bab5SJohan Hedberg if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963) 13452177bab5SJohan Hedberg return 0x01; 13462177bab5SJohan Hedberg if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963) 13472177bab5SJohan Hedberg return 0x01; 13482177bab5SJohan Hedberg if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965) 13492177bab5SJohan Hedberg return 0x01; 13502177bab5SJohan Hedberg } 13512177bab5SJohan Hedberg 13522177bab5SJohan Hedberg if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 && 13532177bab5SJohan Hedberg hdev->lmp_subver == 0x1805) 13542177bab5SJohan Hedberg return 0x01; 13552177bab5SJohan Hedberg 13562177bab5SJohan Hedberg return 0x00; 13572177bab5SJohan Hedberg } 13582177bab5SJohan Hedberg 135942c6b129SJohan Hedberg static void hci_setup_inquiry_mode(struct hci_request *req) 13602177bab5SJohan Hedberg { 13612177bab5SJohan Hedberg u8 mode; 13622177bab5SJohan Hedberg 136342c6b129SJohan Hedberg mode = hci_get_inquiry_mode(req->hdev); 13642177bab5SJohan Hedberg 136542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); 13662177bab5SJohan Hedberg } 13672177bab5SJohan Hedberg 136842c6b129SJohan Hedberg static void hci_setup_event_mask(struct hci_request *req) 13692177bab5SJohan Hedberg { 137042c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 137142c6b129SJohan Hedberg 13722177bab5SJohan Hedberg /* The second byte is 0xff instead of 0x9f (two reserved bits 13732177bab5SJohan Hedberg * disabled) since a Broadcom 1.2 dongle doesn't respond to the 13742177bab5SJohan Hedberg * command otherwise. 13752177bab5SJohan Hedberg */ 13762177bab5SJohan Hedberg u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; 13772177bab5SJohan Hedberg 13782177bab5SJohan Hedberg /* CSR 1.1 dongles does not accept any bitfield so don't try to set 13792177bab5SJohan Hedberg * any event mask for pre 1.2 devices. 13802177bab5SJohan Hedberg */ 13812177bab5SJohan Hedberg if (hdev->hci_ver < BLUETOOTH_VER_1_2) 13822177bab5SJohan Hedberg return; 13832177bab5SJohan Hedberg 13842177bab5SJohan Hedberg if (lmp_bredr_capable(hdev)) { 13852177bab5SJohan Hedberg events[4] |= 0x01; /* Flow Specification Complete */ 13862177bab5SJohan Hedberg events[4] |= 0x02; /* Inquiry Result with RSSI */ 13872177bab5SJohan Hedberg events[4] |= 0x04; /* Read Remote Extended Features Complete */ 13882177bab5SJohan Hedberg events[5] |= 0x08; /* Synchronous Connection Complete */ 13892177bab5SJohan Hedberg events[5] |= 0x10; /* Synchronous Connection Changed */ 1390c7882cbdSMarcel Holtmann } else { 1391c7882cbdSMarcel Holtmann /* Use a different default for LE-only devices */ 1392c7882cbdSMarcel Holtmann memset(events, 0, sizeof(events)); 1393c7882cbdSMarcel Holtmann events[0] |= 0x10; /* Disconnection Complete */ 1394c7882cbdSMarcel Holtmann events[0] |= 0x80; /* Encryption Change */ 1395c7882cbdSMarcel Holtmann events[1] |= 0x08; /* Read Remote Version Information Complete */ 1396c7882cbdSMarcel Holtmann events[1] |= 0x20; /* Command Complete */ 1397c7882cbdSMarcel Holtmann events[1] |= 0x40; /* Command Status */ 1398c7882cbdSMarcel Holtmann events[1] |= 0x80; /* Hardware Error */ 1399c7882cbdSMarcel Holtmann events[2] |= 0x04; /* Number of Completed Packets */ 1400c7882cbdSMarcel Holtmann events[3] |= 0x02; /* Data Buffer Overflow */ 1401c7882cbdSMarcel Holtmann events[5] |= 0x80; /* Encryption Key Refresh Complete */ 14022177bab5SJohan Hedberg } 14032177bab5SJohan Hedberg 14042177bab5SJohan Hedberg if (lmp_inq_rssi_capable(hdev)) 14052177bab5SJohan Hedberg events[4] |= 0x02; /* Inquiry Result with RSSI */ 14062177bab5SJohan Hedberg 14072177bab5SJohan Hedberg if (lmp_sniffsubr_capable(hdev)) 14082177bab5SJohan Hedberg events[5] |= 0x20; /* Sniff Subrating */ 14092177bab5SJohan Hedberg 14102177bab5SJohan Hedberg if (lmp_pause_enc_capable(hdev)) 14112177bab5SJohan Hedberg events[5] |= 0x80; /* Encryption Key Refresh Complete */ 14122177bab5SJohan Hedberg 14132177bab5SJohan Hedberg if (lmp_ext_inq_capable(hdev)) 14142177bab5SJohan Hedberg events[5] |= 0x40; /* Extended Inquiry Result */ 14152177bab5SJohan Hedberg 14162177bab5SJohan Hedberg if (lmp_no_flush_capable(hdev)) 14172177bab5SJohan Hedberg events[7] |= 0x01; /* Enhanced Flush Complete */ 14182177bab5SJohan Hedberg 14192177bab5SJohan Hedberg if (lmp_lsto_capable(hdev)) 14202177bab5SJohan Hedberg events[6] |= 0x80; /* Link Supervision Timeout Changed */ 14212177bab5SJohan Hedberg 14222177bab5SJohan Hedberg if (lmp_ssp_capable(hdev)) { 14232177bab5SJohan Hedberg events[6] |= 0x01; /* IO Capability Request */ 14242177bab5SJohan Hedberg events[6] |= 0x02; /* IO Capability Response */ 14252177bab5SJohan Hedberg events[6] |= 0x04; /* User Confirmation Request */ 14262177bab5SJohan Hedberg events[6] |= 0x08; /* User Passkey Request */ 14272177bab5SJohan Hedberg events[6] |= 0x10; /* Remote OOB Data Request */ 14282177bab5SJohan Hedberg events[6] |= 0x20; /* Simple Pairing Complete */ 14292177bab5SJohan Hedberg events[7] |= 0x04; /* User Passkey Notification */ 14302177bab5SJohan Hedberg events[7] |= 0x08; /* Keypress Notification */ 14312177bab5SJohan Hedberg events[7] |= 0x10; /* Remote Host Supported 14322177bab5SJohan Hedberg * Features Notification 14332177bab5SJohan Hedberg */ 14342177bab5SJohan Hedberg } 14352177bab5SJohan Hedberg 14362177bab5SJohan Hedberg if (lmp_le_capable(hdev)) 14372177bab5SJohan Hedberg events[7] |= 0x20; /* LE Meta-Event */ 14382177bab5SJohan Hedberg 143942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); 14402177bab5SJohan Hedberg } 14412177bab5SJohan Hedberg 144242c6b129SJohan Hedberg static void hci_init2_req(struct hci_request *req, unsigned long opt) 14432177bab5SJohan Hedberg { 144442c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 144542c6b129SJohan Hedberg 14462177bab5SJohan Hedberg if (lmp_bredr_capable(hdev)) 144742c6b129SJohan Hedberg bredr_setup(req); 144856f87901SJohan Hedberg else 144956f87901SJohan Hedberg clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 14502177bab5SJohan Hedberg 14512177bab5SJohan Hedberg if (lmp_le_capable(hdev)) 145242c6b129SJohan Hedberg le_setup(req); 14532177bab5SJohan Hedberg 145442c6b129SJohan Hedberg hci_setup_event_mask(req); 14552177bab5SJohan Hedberg 14563f8e2d75SJohan Hedberg /* AVM Berlin (31), aka "BlueFRITZ!", doesn't support the read 14573f8e2d75SJohan Hedberg * local supported commands HCI command. 14583f8e2d75SJohan Hedberg */ 14593f8e2d75SJohan Hedberg if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) 146042c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 14612177bab5SJohan Hedberg 14622177bab5SJohan Hedberg if (lmp_ssp_capable(hdev)) { 146357af75a8SMarcel Holtmann /* When SSP is available, then the host features page 146457af75a8SMarcel Holtmann * should also be available as well. However some 146557af75a8SMarcel Holtmann * controllers list the max_page as 0 as long as SSP 146657af75a8SMarcel Holtmann * has not been enabled. To achieve proper debugging 146757af75a8SMarcel Holtmann * output, force the minimum max_page to 1 at least. 146857af75a8SMarcel Holtmann */ 146957af75a8SMarcel Holtmann hdev->max_page = 0x01; 147057af75a8SMarcel Holtmann 14712177bab5SJohan Hedberg if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 14722177bab5SJohan Hedberg u8 mode = 0x01; 147342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_SSP_MODE, 14742177bab5SJohan Hedberg sizeof(mode), &mode); 14752177bab5SJohan Hedberg } else { 14762177bab5SJohan Hedberg struct hci_cp_write_eir cp; 14772177bab5SJohan Hedberg 14782177bab5SJohan Hedberg memset(hdev->eir, 0, sizeof(hdev->eir)); 14792177bab5SJohan Hedberg memset(&cp, 0, sizeof(cp)); 14802177bab5SJohan Hedberg 148142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); 14822177bab5SJohan Hedberg } 14832177bab5SJohan Hedberg } 14842177bab5SJohan Hedberg 14852177bab5SJohan Hedberg if (lmp_inq_rssi_capable(hdev)) 148642c6b129SJohan Hedberg hci_setup_inquiry_mode(req); 14872177bab5SJohan Hedberg 14882177bab5SJohan Hedberg if (lmp_inq_tx_pwr_capable(hdev)) 148942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL); 14902177bab5SJohan Hedberg 14912177bab5SJohan Hedberg if (lmp_ext_feat_capable(hdev)) { 14922177bab5SJohan Hedberg struct hci_cp_read_local_ext_features cp; 14932177bab5SJohan Hedberg 14942177bab5SJohan Hedberg cp.page = 0x01; 149542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, 149642c6b129SJohan Hedberg sizeof(cp), &cp); 14972177bab5SJohan Hedberg } 14982177bab5SJohan Hedberg 14992177bab5SJohan Hedberg if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) { 15002177bab5SJohan Hedberg u8 enable = 1; 150142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), 15022177bab5SJohan Hedberg &enable); 15032177bab5SJohan Hedberg } 15042177bab5SJohan Hedberg } 15052177bab5SJohan Hedberg 150642c6b129SJohan Hedberg static void hci_setup_link_policy(struct hci_request *req) 15072177bab5SJohan Hedberg { 150842c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 15092177bab5SJohan Hedberg struct hci_cp_write_def_link_policy cp; 15102177bab5SJohan Hedberg u16 link_policy = 0; 15112177bab5SJohan Hedberg 15122177bab5SJohan Hedberg if (lmp_rswitch_capable(hdev)) 15132177bab5SJohan Hedberg link_policy |= HCI_LP_RSWITCH; 15142177bab5SJohan Hedberg if (lmp_hold_capable(hdev)) 15152177bab5SJohan Hedberg link_policy |= HCI_LP_HOLD; 15162177bab5SJohan Hedberg if (lmp_sniff_capable(hdev)) 15172177bab5SJohan Hedberg link_policy |= HCI_LP_SNIFF; 15182177bab5SJohan Hedberg if (lmp_park_capable(hdev)) 15192177bab5SJohan Hedberg link_policy |= HCI_LP_PARK; 15202177bab5SJohan Hedberg 15212177bab5SJohan Hedberg cp.policy = cpu_to_le16(link_policy); 152242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp); 15232177bab5SJohan Hedberg } 15242177bab5SJohan Hedberg 152542c6b129SJohan Hedberg static void hci_set_le_support(struct hci_request *req) 15262177bab5SJohan Hedberg { 152742c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 15282177bab5SJohan Hedberg struct hci_cp_write_le_host_supported cp; 15292177bab5SJohan Hedberg 1530c73eee91SJohan Hedberg /* LE-only devices do not support explicit enablement */ 1531c73eee91SJohan Hedberg if (!lmp_bredr_capable(hdev)) 1532c73eee91SJohan Hedberg return; 1533c73eee91SJohan Hedberg 15342177bab5SJohan Hedberg memset(&cp, 0, sizeof(cp)); 15352177bab5SJohan Hedberg 15362177bab5SJohan Hedberg if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { 15372177bab5SJohan Hedberg cp.le = 0x01; 15382177bab5SJohan Hedberg cp.simul = lmp_le_br_capable(hdev); 15392177bab5SJohan Hedberg } 15402177bab5SJohan Hedberg 15412177bab5SJohan Hedberg if (cp.le != lmp_host_le_capable(hdev)) 154242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), 15432177bab5SJohan Hedberg &cp); 15442177bab5SJohan Hedberg } 15452177bab5SJohan Hedberg 1546d62e6d67SJohan Hedberg static void hci_set_event_mask_page_2(struct hci_request *req) 1547d62e6d67SJohan Hedberg { 1548d62e6d67SJohan Hedberg struct hci_dev *hdev = req->hdev; 1549d62e6d67SJohan Hedberg u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 1550d62e6d67SJohan Hedberg 1551d62e6d67SJohan Hedberg /* If Connectionless Slave Broadcast master role is supported 1552d62e6d67SJohan Hedberg * enable all necessary events for it. 1553d62e6d67SJohan Hedberg */ 155453b834d2SMarcel Holtmann if (lmp_csb_master_capable(hdev)) { 1555d62e6d67SJohan Hedberg events[1] |= 0x40; /* Triggered Clock Capture */ 1556d62e6d67SJohan Hedberg events[1] |= 0x80; /* Synchronization Train Complete */ 1557d62e6d67SJohan Hedberg events[2] |= 0x10; /* Slave Page Response Timeout */ 1558d62e6d67SJohan Hedberg events[2] |= 0x20; /* CSB Channel Map Change */ 1559d62e6d67SJohan Hedberg } 1560d62e6d67SJohan Hedberg 1561d62e6d67SJohan Hedberg /* If Connectionless Slave Broadcast slave role is supported 1562d62e6d67SJohan Hedberg * enable all necessary events for it. 1563d62e6d67SJohan Hedberg */ 156453b834d2SMarcel Holtmann if (lmp_csb_slave_capable(hdev)) { 1565d62e6d67SJohan Hedberg events[2] |= 0x01; /* Synchronization Train Received */ 1566d62e6d67SJohan Hedberg events[2] |= 0x02; /* CSB Receive */ 1567d62e6d67SJohan Hedberg events[2] |= 0x04; /* CSB Timeout */ 1568d62e6d67SJohan Hedberg events[2] |= 0x08; /* Truncated Page Complete */ 1569d62e6d67SJohan Hedberg } 1570d62e6d67SJohan Hedberg 157140c59fcbSMarcel Holtmann /* Enable Authenticated Payload Timeout Expired event if supported */ 157240c59fcbSMarcel Holtmann if (lmp_ping_capable(hdev)) 157340c59fcbSMarcel Holtmann events[2] |= 0x80; 157440c59fcbSMarcel Holtmann 1575d62e6d67SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); 1576d62e6d67SJohan Hedberg } 1577d62e6d67SJohan Hedberg 157842c6b129SJohan Hedberg static void hci_init3_req(struct hci_request *req, unsigned long opt) 15792177bab5SJohan Hedberg { 158042c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 1581d2c5d77fSJohan Hedberg u8 p; 158242c6b129SJohan Hedberg 1583b8f4e068SGustavo Padovan /* Some Broadcom based Bluetooth controllers do not support the 1584b8f4e068SGustavo Padovan * Delete Stored Link Key command. They are clearly indicating its 1585b8f4e068SGustavo Padovan * absence in the bit mask of supported commands. 1586b8f4e068SGustavo Padovan * 1587b8f4e068SGustavo Padovan * Check the supported commands and only if the the command is marked 1588b8f4e068SGustavo Padovan * as supported send it. If not supported assume that the controller 1589b8f4e068SGustavo Padovan * does not have actual support for stored link keys which makes this 1590b8f4e068SGustavo Padovan * command redundant anyway. 1591f9f462faSMarcel Holtmann * 1592f9f462faSMarcel Holtmann * Some controllers indicate that they support handling deleting 1593f9f462faSMarcel Holtmann * stored link keys, but they don't. The quirk lets a driver 1594f9f462faSMarcel Holtmann * just disable this command. 1595b8f4e068SGustavo Padovan */ 1596f9f462faSMarcel Holtmann if (hdev->commands[6] & 0x80 && 1597f9f462faSMarcel Holtmann !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { 159859f45d57SJohan Hedberg struct hci_cp_delete_stored_link_key cp; 159959f45d57SJohan Hedberg 160059f45d57SJohan Hedberg bacpy(&cp.bdaddr, BDADDR_ANY); 160159f45d57SJohan Hedberg cp.delete_all = 0x01; 160259f45d57SJohan Hedberg hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, 160359f45d57SJohan Hedberg sizeof(cp), &cp); 160459f45d57SJohan Hedberg } 160559f45d57SJohan Hedberg 16062177bab5SJohan Hedberg if (hdev->commands[5] & 0x10) 160742c6b129SJohan Hedberg hci_setup_link_policy(req); 16082177bab5SJohan Hedberg 16099193c6e8SAndre Guedes if (lmp_le_capable(hdev)) { 16109193c6e8SAndre Guedes u8 events[8]; 16119193c6e8SAndre Guedes 16129193c6e8SAndre Guedes memset(events, 0, sizeof(events)); 16139193c6e8SAndre Guedes events[0] = 0x1f; 1614662bc2e6SAndre Guedes 1615662bc2e6SAndre Guedes /* If controller supports the Connection Parameters Request 1616662bc2e6SAndre Guedes * Link Layer Procedure, enable the corresponding event. 1617662bc2e6SAndre Guedes */ 1618662bc2e6SAndre Guedes if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) 1619662bc2e6SAndre Guedes events[0] |= 0x20; /* LE Remote Connection 1620662bc2e6SAndre Guedes * Parameter Request 1621662bc2e6SAndre Guedes */ 1622662bc2e6SAndre Guedes 16239193c6e8SAndre Guedes hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events), 16249193c6e8SAndre Guedes events); 16259193c6e8SAndre Guedes 162642c6b129SJohan Hedberg hci_set_le_support(req); 16279193c6e8SAndre Guedes } 1628d2c5d77fSJohan Hedberg 1629d2c5d77fSJohan Hedberg /* Read features beyond page 1 if available */ 1630d2c5d77fSJohan Hedberg for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { 1631d2c5d77fSJohan Hedberg struct hci_cp_read_local_ext_features cp; 1632d2c5d77fSJohan Hedberg 1633d2c5d77fSJohan Hedberg cp.page = p; 1634d2c5d77fSJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, 1635d2c5d77fSJohan Hedberg sizeof(cp), &cp); 1636d2c5d77fSJohan Hedberg } 16372177bab5SJohan Hedberg } 16382177bab5SJohan Hedberg 16395d4e7e8dSJohan Hedberg static void hci_init4_req(struct hci_request *req, unsigned long opt) 16405d4e7e8dSJohan Hedberg { 16415d4e7e8dSJohan Hedberg struct hci_dev *hdev = req->hdev; 16425d4e7e8dSJohan Hedberg 1643d62e6d67SJohan Hedberg /* Set event mask page 2 if the HCI command for it is supported */ 1644d62e6d67SJohan Hedberg if (hdev->commands[22] & 0x04) 1645d62e6d67SJohan Hedberg hci_set_event_mask_page_2(req); 1646d62e6d67SJohan Hedberg 16475d4e7e8dSJohan Hedberg /* Check for Synchronization Train support */ 164853b834d2SMarcel Holtmann if (lmp_sync_train_capable(hdev)) 16495d4e7e8dSJohan Hedberg hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); 1650a6d0d690SMarcel Holtmann 1651a6d0d690SMarcel Holtmann /* Enable Secure Connections if supported and configured */ 16525afeac14SMarcel Holtmann if ((lmp_sc_capable(hdev) || 1653111902f7SMarcel Holtmann test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) && 1654a6d0d690SMarcel Holtmann test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) { 1655a6d0d690SMarcel Holtmann u8 support = 0x01; 1656a6d0d690SMarcel Holtmann hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT, 1657a6d0d690SMarcel Holtmann sizeof(support), &support); 1658a6d0d690SMarcel Holtmann } 16595d4e7e8dSJohan Hedberg } 16605d4e7e8dSJohan Hedberg 16612177bab5SJohan Hedberg static int __hci_init(struct hci_dev *hdev) 16622177bab5SJohan Hedberg { 16632177bab5SJohan Hedberg int err; 16642177bab5SJohan Hedberg 16652177bab5SJohan Hedberg err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); 16662177bab5SJohan Hedberg if (err < 0) 16672177bab5SJohan Hedberg return err; 16682177bab5SJohan Hedberg 16694b4148e9SMarcel Holtmann /* The Device Under Test (DUT) mode is special and available for 16704b4148e9SMarcel Holtmann * all controller types. So just create it early on. 16714b4148e9SMarcel Holtmann */ 16724b4148e9SMarcel Holtmann if (test_bit(HCI_SETUP, &hdev->dev_flags)) { 16734b4148e9SMarcel Holtmann debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev, 16744b4148e9SMarcel Holtmann &dut_mode_fops); 16754b4148e9SMarcel Holtmann } 16764b4148e9SMarcel Holtmann 16772177bab5SJohan Hedberg /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode 16782177bab5SJohan Hedberg * BR/EDR/LE type controllers. AMP controllers only need the 16792177bab5SJohan Hedberg * first stage init. 16802177bab5SJohan Hedberg */ 16812177bab5SJohan Hedberg if (hdev->dev_type != HCI_BREDR) 16822177bab5SJohan Hedberg return 0; 16832177bab5SJohan Hedberg 16842177bab5SJohan Hedberg err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); 16852177bab5SJohan Hedberg if (err < 0) 16862177bab5SJohan Hedberg return err; 16872177bab5SJohan Hedberg 16885d4e7e8dSJohan Hedberg err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); 16895d4e7e8dSJohan Hedberg if (err < 0) 16905d4e7e8dSJohan Hedberg return err; 16915d4e7e8dSJohan Hedberg 1692baf27f6eSMarcel Holtmann err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT); 1693baf27f6eSMarcel Holtmann if (err < 0) 1694baf27f6eSMarcel Holtmann return err; 1695baf27f6eSMarcel Holtmann 1696baf27f6eSMarcel Holtmann /* Only create debugfs entries during the initial setup 1697baf27f6eSMarcel Holtmann * phase and not every time the controller gets powered on. 1698baf27f6eSMarcel Holtmann */ 1699baf27f6eSMarcel Holtmann if (!test_bit(HCI_SETUP, &hdev->dev_flags)) 1700baf27f6eSMarcel Holtmann return 0; 1701baf27f6eSMarcel Holtmann 1702dfb826a8SMarcel Holtmann debugfs_create_file("features", 0444, hdev->debugfs, hdev, 1703dfb826a8SMarcel Holtmann &features_fops); 1704ceeb3bc0SMarcel Holtmann debugfs_create_u16("manufacturer", 0444, hdev->debugfs, 1705ceeb3bc0SMarcel Holtmann &hdev->manufacturer); 1706ceeb3bc0SMarcel Holtmann debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver); 1707ceeb3bc0SMarcel Holtmann debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev); 170870afe0b8SMarcel Holtmann debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev, 170970afe0b8SMarcel Holtmann &blacklist_fops); 171047219839SMarcel Holtmann debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops); 171147219839SMarcel Holtmann 171231ad1691SAndrzej Kaczmarek debugfs_create_file("conn_info_min_age", 0644, hdev->debugfs, hdev, 171331ad1691SAndrzej Kaczmarek &conn_info_min_age_fops); 171431ad1691SAndrzej Kaczmarek debugfs_create_file("conn_info_max_age", 0644, hdev->debugfs, hdev, 171531ad1691SAndrzej Kaczmarek &conn_info_max_age_fops); 171631ad1691SAndrzej Kaczmarek 1717baf27f6eSMarcel Holtmann if (lmp_bredr_capable(hdev)) { 1718baf27f6eSMarcel Holtmann debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, 1719baf27f6eSMarcel Holtmann hdev, &inquiry_cache_fops); 172002d08d15SMarcel Holtmann debugfs_create_file("link_keys", 0400, hdev->debugfs, 172102d08d15SMarcel Holtmann hdev, &link_keys_fops); 1722babdbb3cSMarcel Holtmann debugfs_create_file("dev_class", 0444, hdev->debugfs, 1723babdbb3cSMarcel Holtmann hdev, &dev_class_fops); 1724041000b9SMarcel Holtmann debugfs_create_file("voice_setting", 0444, hdev->debugfs, 1725041000b9SMarcel Holtmann hdev, &voice_setting_fops); 1726baf27f6eSMarcel Holtmann } 1727baf27f6eSMarcel Holtmann 172806f5b778SMarcel Holtmann if (lmp_ssp_capable(hdev)) { 1729ebd1e33bSMarcel Holtmann debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs, 1730ebd1e33bSMarcel Holtmann hdev, &auto_accept_delay_fops); 17315afeac14SMarcel Holtmann debugfs_create_file("force_sc_support", 0644, hdev->debugfs, 17325afeac14SMarcel Holtmann hdev, &force_sc_support_fops); 1733134c2a89SMarcel Holtmann debugfs_create_file("sc_only_mode", 0444, hdev->debugfs, 1734134c2a89SMarcel Holtmann hdev, &sc_only_mode_fops); 173506f5b778SMarcel Holtmann } 1736ebd1e33bSMarcel Holtmann 17372bfa3531SMarcel Holtmann if (lmp_sniff_capable(hdev)) { 17382bfa3531SMarcel Holtmann debugfs_create_file("idle_timeout", 0644, hdev->debugfs, 17392bfa3531SMarcel Holtmann hdev, &idle_timeout_fops); 17402bfa3531SMarcel Holtmann debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs, 17412bfa3531SMarcel Holtmann hdev, &sniff_min_interval_fops); 17422bfa3531SMarcel Holtmann debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs, 17432bfa3531SMarcel Holtmann hdev, &sniff_max_interval_fops); 17442bfa3531SMarcel Holtmann } 17452bfa3531SMarcel Holtmann 1746d0f729b8SMarcel Holtmann if (lmp_le_capable(hdev)) { 1747ac345813SMarcel Holtmann debugfs_create_file("identity", 0400, hdev->debugfs, 1748ac345813SMarcel Holtmann hdev, &identity_fops); 1749ac345813SMarcel Holtmann debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, 1750ac345813SMarcel Holtmann hdev, &rpa_timeout_fops); 17517a4cd51dSMarcel Holtmann debugfs_create_file("random_address", 0444, hdev->debugfs, 17527a4cd51dSMarcel Holtmann hdev, &random_address_fops); 1753e7b8fc92SMarcel Holtmann debugfs_create_file("static_address", 0444, hdev->debugfs, 1754e7b8fc92SMarcel Holtmann hdev, &static_address_fops); 1755b32bba6cSMarcel Holtmann 1756b32bba6cSMarcel Holtmann /* For controllers with a public address, provide a debug 1757b32bba6cSMarcel Holtmann * option to force the usage of the configured static 1758b32bba6cSMarcel Holtmann * address. By default the public address is used. 1759b32bba6cSMarcel Holtmann */ 1760b32bba6cSMarcel Holtmann if (bacmp(&hdev->bdaddr, BDADDR_ANY)) 1761b32bba6cSMarcel Holtmann debugfs_create_file("force_static_address", 0644, 1762b32bba6cSMarcel Holtmann hdev->debugfs, hdev, 1763b32bba6cSMarcel Holtmann &force_static_address_fops); 1764b32bba6cSMarcel Holtmann 1765b32bba6cSMarcel Holtmann debugfs_create_u8("white_list_size", 0444, hdev->debugfs, 1766b32bba6cSMarcel Holtmann &hdev->le_white_list_size); 1767d2ab0ac1SMarcel Holtmann debugfs_create_file("white_list", 0444, hdev->debugfs, hdev, 1768d2ab0ac1SMarcel Holtmann &white_list_fops); 17693698d704SMarcel Holtmann debugfs_create_file("identity_resolving_keys", 0400, 17703698d704SMarcel Holtmann hdev->debugfs, hdev, 17713698d704SMarcel Holtmann &identity_resolving_keys_fops); 17728f8625cdSMarcel Holtmann debugfs_create_file("long_term_keys", 0400, hdev->debugfs, 17738f8625cdSMarcel Holtmann hdev, &long_term_keys_fops); 17744e70c7e7SMarcel Holtmann debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, 17754e70c7e7SMarcel Holtmann hdev, &conn_min_interval_fops); 17764e70c7e7SMarcel Holtmann debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, 17774e70c7e7SMarcel Holtmann hdev, &conn_max_interval_fops); 1778816a93d1SMarcel Holtmann debugfs_create_file("conn_latency", 0644, hdev->debugfs, 1779816a93d1SMarcel Holtmann hdev, &conn_latency_fops); 1780f1649577SMarcel Holtmann debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, 1781f1649577SMarcel Holtmann hdev, &supervision_timeout_fops); 17823f959d46SMarcel Holtmann debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, 17833f959d46SMarcel Holtmann hdev, &adv_channel_map_fops); 17840b3c7d37SMarcel Holtmann debugfs_create_file("device_list", 0444, hdev->debugfs, hdev, 17850b3c7d37SMarcel Holtmann &device_list_fops); 1786b9a7a61eSLukasz Rymanowski debugfs_create_u16("discov_interleaved_timeout", 0644, 1787b9a7a61eSLukasz Rymanowski hdev->debugfs, 1788b9a7a61eSLukasz Rymanowski &hdev->discov_interleaved_timeout); 1789d0f729b8SMarcel Holtmann } 1790e7b8fc92SMarcel Holtmann 1791baf27f6eSMarcel Holtmann return 0; 17922177bab5SJohan Hedberg } 17932177bab5SJohan Hedberg 179442c6b129SJohan Hedberg static void hci_scan_req(struct hci_request *req, unsigned long opt) 17951da177e4SLinus Torvalds { 17961da177e4SLinus Torvalds __u8 scan = opt; 17971da177e4SLinus Torvalds 179842c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, scan); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /* Inquiry and Page scans */ 180142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 180442c6b129SJohan Hedberg static void hci_auth_req(struct hci_request *req, unsigned long opt) 18051da177e4SLinus Torvalds { 18061da177e4SLinus Torvalds __u8 auth = opt; 18071da177e4SLinus Torvalds 180842c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, auth); 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds /* Authentication */ 181142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds 181442c6b129SJohan Hedberg static void hci_encrypt_req(struct hci_request *req, unsigned long opt) 18151da177e4SLinus Torvalds { 18161da177e4SLinus Torvalds __u8 encrypt = opt; 18171da177e4SLinus Torvalds 181842c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, encrypt); 18191da177e4SLinus Torvalds 1820e4e8e37cSMarcel Holtmann /* Encryption */ 182142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 182442c6b129SJohan Hedberg static void hci_linkpol_req(struct hci_request *req, unsigned long opt) 1825e4e8e37cSMarcel Holtmann { 1826e4e8e37cSMarcel Holtmann __le16 policy = cpu_to_le16(opt); 1827e4e8e37cSMarcel Holtmann 182842c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, policy); 1829e4e8e37cSMarcel Holtmann 1830e4e8e37cSMarcel Holtmann /* Default link policy */ 183142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); 1832e4e8e37cSMarcel Holtmann } 1833e4e8e37cSMarcel Holtmann 18341da177e4SLinus Torvalds /* Get HCI device by index. 18351da177e4SLinus Torvalds * Device is held on return. */ 18361da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index) 18371da177e4SLinus Torvalds { 18388035ded4SLuiz Augusto von Dentz struct hci_dev *hdev = NULL, *d; 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds BT_DBG("%d", index); 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds if (index < 0) 18431da177e4SLinus Torvalds return NULL; 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds read_lock(&hci_dev_list_lock); 18468035ded4SLuiz Augusto von Dentz list_for_each_entry(d, &hci_dev_list, list) { 18471da177e4SLinus Torvalds if (d->id == index) { 18481da177e4SLinus Torvalds hdev = hci_dev_hold(d); 18491da177e4SLinus Torvalds break; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds read_unlock(&hci_dev_list_lock); 18531da177e4SLinus Torvalds return hdev; 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds /* ---- Inquiry support ---- */ 1857ff9ef578SJohan Hedberg 185830dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev) 185930dc78e1SJohan Hedberg { 186030dc78e1SJohan Hedberg struct discovery_state *discov = &hdev->discovery; 186130dc78e1SJohan Hedberg 18626fbe195dSAndre Guedes switch (discov->state) { 1863343f935bSAndre Guedes case DISCOVERY_FINDING: 18646fbe195dSAndre Guedes case DISCOVERY_RESOLVING: 186530dc78e1SJohan Hedberg return true; 186630dc78e1SJohan Hedberg 18676fbe195dSAndre Guedes default: 186830dc78e1SJohan Hedberg return false; 186930dc78e1SJohan Hedberg } 18706fbe195dSAndre Guedes } 187130dc78e1SJohan Hedberg 1872ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state) 1873ff9ef578SJohan Hedberg { 1874ff9ef578SJohan Hedberg BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); 1875ff9ef578SJohan Hedberg 1876ff9ef578SJohan Hedberg if (hdev->discovery.state == state) 1877ff9ef578SJohan Hedberg return; 1878ff9ef578SJohan Hedberg 1879ff9ef578SJohan Hedberg switch (state) { 1880ff9ef578SJohan Hedberg case DISCOVERY_STOPPED: 1881c54c3860SAndre Guedes hci_update_background_scan(hdev); 1882c54c3860SAndre Guedes 18837b99b659SAndre Guedes if (hdev->discovery.state != DISCOVERY_STARTING) 1884ff9ef578SJohan Hedberg mgmt_discovering(hdev, 0); 1885ff9ef578SJohan Hedberg break; 1886ff9ef578SJohan Hedberg case DISCOVERY_STARTING: 1887ff9ef578SJohan Hedberg break; 1888343f935bSAndre Guedes case DISCOVERY_FINDING: 1889ff9ef578SJohan Hedberg mgmt_discovering(hdev, 1); 1890ff9ef578SJohan Hedberg break; 189130dc78e1SJohan Hedberg case DISCOVERY_RESOLVING: 189230dc78e1SJohan Hedberg break; 1893ff9ef578SJohan Hedberg case DISCOVERY_STOPPING: 1894ff9ef578SJohan Hedberg break; 1895ff9ef578SJohan Hedberg } 1896ff9ef578SJohan Hedberg 1897ff9ef578SJohan Hedberg hdev->discovery.state = state; 1898ff9ef578SJohan Hedberg } 1899ff9ef578SJohan Hedberg 19001f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev) 19011da177e4SLinus Torvalds { 190230883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1903b57c1a56SJohan Hedberg struct inquiry_entry *p, *n; 19041da177e4SLinus Torvalds 1905561aafbcSJohan Hedberg list_for_each_entry_safe(p, n, &cache->all, all) { 1906561aafbcSJohan Hedberg list_del(&p->all); 1907b57c1a56SJohan Hedberg kfree(p); 19081da177e4SLinus Torvalds } 1909561aafbcSJohan Hedberg 1910561aafbcSJohan Hedberg INIT_LIST_HEAD(&cache->unknown); 1911561aafbcSJohan Hedberg INIT_LIST_HEAD(&cache->resolve); 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 1914a8c5fb1aSGustavo Padovan struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, 1915a8c5fb1aSGustavo Padovan bdaddr_t *bdaddr) 19161da177e4SLinus Torvalds { 191730883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 19181da177e4SLinus Torvalds struct inquiry_entry *e; 19191da177e4SLinus Torvalds 19206ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, bdaddr); 19211da177e4SLinus Torvalds 1922561aafbcSJohan Hedberg list_for_each_entry(e, &cache->all, all) { 19231da177e4SLinus Torvalds if (!bacmp(&e->data.bdaddr, bdaddr)) 19241da177e4SLinus Torvalds return e; 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds 1927b57c1a56SJohan Hedberg return NULL; 1928b57c1a56SJohan Hedberg } 1929b57c1a56SJohan Hedberg 1930561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, 1931561aafbcSJohan Hedberg bdaddr_t *bdaddr) 1932561aafbcSJohan Hedberg { 193330883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1934561aafbcSJohan Hedberg struct inquiry_entry *e; 1935561aafbcSJohan Hedberg 19366ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, bdaddr); 1937561aafbcSJohan Hedberg 1938561aafbcSJohan Hedberg list_for_each_entry(e, &cache->unknown, list) { 1939561aafbcSJohan Hedberg if (!bacmp(&e->data.bdaddr, bdaddr)) 1940561aafbcSJohan Hedberg return e; 1941561aafbcSJohan Hedberg } 1942561aafbcSJohan Hedberg 1943561aafbcSJohan Hedberg return NULL; 1944561aafbcSJohan Hedberg } 1945561aafbcSJohan Hedberg 194630dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, 194730dc78e1SJohan Hedberg bdaddr_t *bdaddr, 194830dc78e1SJohan Hedberg int state) 194930dc78e1SJohan Hedberg { 195030dc78e1SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 195130dc78e1SJohan Hedberg struct inquiry_entry *e; 195230dc78e1SJohan Hedberg 19536ed93dc6SAndrei Emeltchenko BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); 195430dc78e1SJohan Hedberg 195530dc78e1SJohan Hedberg list_for_each_entry(e, &cache->resolve, list) { 195630dc78e1SJohan Hedberg if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) 195730dc78e1SJohan Hedberg return e; 195830dc78e1SJohan Hedberg if (!bacmp(&e->data.bdaddr, bdaddr)) 195930dc78e1SJohan Hedberg return e; 196030dc78e1SJohan Hedberg } 196130dc78e1SJohan Hedberg 196230dc78e1SJohan Hedberg return NULL; 196330dc78e1SJohan Hedberg } 196430dc78e1SJohan Hedberg 1965a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, 1966a3d4e20aSJohan Hedberg struct inquiry_entry *ie) 1967a3d4e20aSJohan Hedberg { 1968a3d4e20aSJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1969a3d4e20aSJohan Hedberg struct list_head *pos = &cache->resolve; 1970a3d4e20aSJohan Hedberg struct inquiry_entry *p; 1971a3d4e20aSJohan Hedberg 1972a3d4e20aSJohan Hedberg list_del(&ie->list); 1973a3d4e20aSJohan Hedberg 1974a3d4e20aSJohan Hedberg list_for_each_entry(p, &cache->resolve, list) { 1975a3d4e20aSJohan Hedberg if (p->name_state != NAME_PENDING && 1976a3d4e20aSJohan Hedberg abs(p->data.rssi) >= abs(ie->data.rssi)) 1977a3d4e20aSJohan Hedberg break; 1978a3d4e20aSJohan Hedberg pos = &p->list; 1979a3d4e20aSJohan Hedberg } 1980a3d4e20aSJohan Hedberg 1981a3d4e20aSJohan Hedberg list_add(&ie->list, pos); 1982a3d4e20aSJohan Hedberg } 1983a3d4e20aSJohan Hedberg 1984af58925cSMarcel Holtmann u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, 1985af58925cSMarcel Holtmann bool name_known) 19861da177e4SLinus Torvalds { 198730883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 198870f23020SAndrei Emeltchenko struct inquiry_entry *ie; 1989af58925cSMarcel Holtmann u32 flags = 0; 19901da177e4SLinus Torvalds 19916ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, &data->bdaddr); 19921da177e4SLinus Torvalds 19932b2fec4dSSzymon Janc hci_remove_remote_oob_data(hdev, &data->bdaddr); 19942b2fec4dSSzymon Janc 1995af58925cSMarcel Holtmann if (!data->ssp_mode) 1996af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; 1997388fc8faSJohan Hedberg 199870f23020SAndrei Emeltchenko ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); 1999a3d4e20aSJohan Hedberg if (ie) { 2000af58925cSMarcel Holtmann if (!ie->data.ssp_mode) 2001af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; 2002388fc8faSJohan Hedberg 2003a3d4e20aSJohan Hedberg if (ie->name_state == NAME_NEEDED && 2004a3d4e20aSJohan Hedberg data->rssi != ie->data.rssi) { 2005a3d4e20aSJohan Hedberg ie->data.rssi = data->rssi; 2006a3d4e20aSJohan Hedberg hci_inquiry_cache_update_resolve(hdev, ie); 2007a3d4e20aSJohan Hedberg } 2008a3d4e20aSJohan Hedberg 2009561aafbcSJohan Hedberg goto update; 2010a3d4e20aSJohan Hedberg } 2011561aafbcSJohan Hedberg 20121da177e4SLinus Torvalds /* Entry not in the cache. Add new one. */ 201370f23020SAndrei Emeltchenko ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC); 2014af58925cSMarcel Holtmann if (!ie) { 2015af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_CONFIRM_NAME; 2016af58925cSMarcel Holtmann goto done; 2017af58925cSMarcel Holtmann } 201870f23020SAndrei Emeltchenko 2019561aafbcSJohan Hedberg list_add(&ie->all, &cache->all); 2020561aafbcSJohan Hedberg 2021561aafbcSJohan Hedberg if (name_known) { 2022561aafbcSJohan Hedberg ie->name_state = NAME_KNOWN; 2023561aafbcSJohan Hedberg } else { 2024561aafbcSJohan Hedberg ie->name_state = NAME_NOT_KNOWN; 2025561aafbcSJohan Hedberg list_add(&ie->list, &cache->unknown); 2026561aafbcSJohan Hedberg } 2027561aafbcSJohan Hedberg 2028561aafbcSJohan Hedberg update: 2029561aafbcSJohan Hedberg if (name_known && ie->name_state != NAME_KNOWN && 2030561aafbcSJohan Hedberg ie->name_state != NAME_PENDING) { 2031561aafbcSJohan Hedberg ie->name_state = NAME_KNOWN; 2032561aafbcSJohan Hedberg list_del(&ie->list); 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds 203570f23020SAndrei Emeltchenko memcpy(&ie->data, data, sizeof(*data)); 203670f23020SAndrei Emeltchenko ie->timestamp = jiffies; 20371da177e4SLinus Torvalds cache->timestamp = jiffies; 20383175405bSJohan Hedberg 20393175405bSJohan Hedberg if (ie->name_state == NAME_NOT_KNOWN) 2040af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_CONFIRM_NAME; 20413175405bSJohan Hedberg 2042af58925cSMarcel Holtmann done: 2043af58925cSMarcel Holtmann return flags; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) 20471da177e4SLinus Torvalds { 204830883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 20491da177e4SLinus Torvalds struct inquiry_info *info = (struct inquiry_info *) buf; 20501da177e4SLinus Torvalds struct inquiry_entry *e; 20511da177e4SLinus Torvalds int copied = 0; 20521da177e4SLinus Torvalds 2053561aafbcSJohan Hedberg list_for_each_entry(e, &cache->all, all) { 20541da177e4SLinus Torvalds struct inquiry_data *data = &e->data; 2055b57c1a56SJohan Hedberg 2056b57c1a56SJohan Hedberg if (copied >= num) 2057b57c1a56SJohan Hedberg break; 2058b57c1a56SJohan Hedberg 20591da177e4SLinus Torvalds bacpy(&info->bdaddr, &data->bdaddr); 20601da177e4SLinus Torvalds info->pscan_rep_mode = data->pscan_rep_mode; 20611da177e4SLinus Torvalds info->pscan_period_mode = data->pscan_period_mode; 20621da177e4SLinus Torvalds info->pscan_mode = data->pscan_mode; 20631da177e4SLinus Torvalds memcpy(info->dev_class, data->dev_class, 3); 20641da177e4SLinus Torvalds info->clock_offset = data->clock_offset; 2065b57c1a56SJohan Hedberg 20661da177e4SLinus Torvalds info++; 2067b57c1a56SJohan Hedberg copied++; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds BT_DBG("cache %p, copied %d", cache, copied); 20711da177e4SLinus Torvalds return copied; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 207442c6b129SJohan Hedberg static void hci_inq_req(struct hci_request *req, unsigned long opt) 20751da177e4SLinus Torvalds { 20761da177e4SLinus Torvalds struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; 207742c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 20781da177e4SLinus Torvalds struct hci_cp_inquiry cp; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds if (test_bit(HCI_INQUIRY, &hdev->flags)) 20831da177e4SLinus Torvalds return; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds /* Start Inquiry */ 20861da177e4SLinus Torvalds memcpy(&cp.lap, &ir->lap, 3); 20871da177e4SLinus Torvalds cp.length = ir->length; 20881da177e4SLinus Torvalds cp.num_rsp = ir->num_rsp; 208942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 20923e13fa1eSAndre Guedes static int wait_inquiry(void *word) 20933e13fa1eSAndre Guedes { 20943e13fa1eSAndre Guedes schedule(); 20953e13fa1eSAndre Guedes return signal_pending(current); 20963e13fa1eSAndre Guedes } 20973e13fa1eSAndre Guedes 20981da177e4SLinus Torvalds int hci_inquiry(void __user *arg) 20991da177e4SLinus Torvalds { 21001da177e4SLinus Torvalds __u8 __user *ptr = arg; 21011da177e4SLinus Torvalds struct hci_inquiry_req ir; 21021da177e4SLinus Torvalds struct hci_dev *hdev; 21031da177e4SLinus Torvalds int err = 0, do_inquiry = 0, max_rsp; 21041da177e4SLinus Torvalds long timeo; 21051da177e4SLinus Torvalds __u8 *buf; 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds if (copy_from_user(&ir, ptr, sizeof(ir))) 21081da177e4SLinus Torvalds return -EFAULT; 21091da177e4SLinus Torvalds 21105a08ecceSAndrei Emeltchenko hdev = hci_dev_get(ir.dev_id); 21115a08ecceSAndrei Emeltchenko if (!hdev) 21121da177e4SLinus Torvalds return -ENODEV; 21131da177e4SLinus Torvalds 21140736cfa8SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 21150736cfa8SMarcel Holtmann err = -EBUSY; 21160736cfa8SMarcel Holtmann goto done; 21170736cfa8SMarcel Holtmann } 21180736cfa8SMarcel Holtmann 21194a964404SMarcel Holtmann if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 2120fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 2121fee746b0SMarcel Holtmann goto done; 2122fee746b0SMarcel Holtmann } 2123fee746b0SMarcel Holtmann 21245b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) { 21255b69bef5SMarcel Holtmann err = -EOPNOTSUPP; 21265b69bef5SMarcel Holtmann goto done; 21275b69bef5SMarcel Holtmann } 21285b69bef5SMarcel Holtmann 212956f87901SJohan Hedberg if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 213056f87901SJohan Hedberg err = -EOPNOTSUPP; 213156f87901SJohan Hedberg goto done; 213256f87901SJohan Hedberg } 213356f87901SJohan Hedberg 213409fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 21351da177e4SLinus Torvalds if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 2136a8c5fb1aSGustavo Padovan inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { 21371f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 21381da177e4SLinus Torvalds do_inquiry = 1; 21391da177e4SLinus Torvalds } 214009fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 21411da177e4SLinus Torvalds 214204837f64SMarcel Holtmann timeo = ir.length * msecs_to_jiffies(2000); 214370f23020SAndrei Emeltchenko 214470f23020SAndrei Emeltchenko if (do_inquiry) { 214501178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, 214601178cd4SJohan Hedberg timeo); 214770f23020SAndrei Emeltchenko if (err < 0) 21481da177e4SLinus Torvalds goto done; 21493e13fa1eSAndre Guedes 21503e13fa1eSAndre Guedes /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is 21513e13fa1eSAndre Guedes * cleared). If it is interrupted by a signal, return -EINTR. 21523e13fa1eSAndre Guedes */ 21533e13fa1eSAndre Guedes if (wait_on_bit(&hdev->flags, HCI_INQUIRY, wait_inquiry, 21543e13fa1eSAndre Guedes TASK_INTERRUPTIBLE)) 21553e13fa1eSAndre Guedes return -EINTR; 215670f23020SAndrei Emeltchenko } 21571da177e4SLinus Torvalds 21588fc9ced3SGustavo Padovan /* for unlimited number of responses we will use buffer with 21598fc9ced3SGustavo Padovan * 255 entries 21608fc9ced3SGustavo Padovan */ 21611da177e4SLinus Torvalds max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds /* cache_dump can't sleep. Therefore we allocate temp buffer and then 21641da177e4SLinus Torvalds * copy it to the user space. 21651da177e4SLinus Torvalds */ 216670f23020SAndrei Emeltchenko buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); 216770f23020SAndrei Emeltchenko if (!buf) { 21681da177e4SLinus Torvalds err = -ENOMEM; 21691da177e4SLinus Torvalds goto done; 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds 217209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 21731da177e4SLinus Torvalds ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); 217409fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds BT_DBG("num_rsp %d", ir.num_rsp); 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (!copy_to_user(ptr, &ir, sizeof(ir))) { 21791da177e4SLinus Torvalds ptr += sizeof(ir); 21801da177e4SLinus Torvalds if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * 21811da177e4SLinus Torvalds ir.num_rsp)) 21821da177e4SLinus Torvalds err = -EFAULT; 21831da177e4SLinus Torvalds } else 21841da177e4SLinus Torvalds err = -EFAULT; 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds kfree(buf); 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds done: 21891da177e4SLinus Torvalds hci_dev_put(hdev); 21901da177e4SLinus Torvalds return err; 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds 2193cbed0ca1SJohan Hedberg static int hci_dev_do_open(struct hci_dev *hdev) 21941da177e4SLinus Torvalds { 21951da177e4SLinus Torvalds int ret = 0; 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds BT_DBG("%s %p", hdev->name, hdev); 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds hci_req_lock(hdev); 22001da177e4SLinus Torvalds 220194324962SJohan Hovold if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { 220294324962SJohan Hovold ret = -ENODEV; 220394324962SJohan Hovold goto done; 220494324962SJohan Hovold } 220594324962SJohan Hovold 2206a5c8f270SMarcel Holtmann if (!test_bit(HCI_SETUP, &hdev->dev_flags)) { 2207a5c8f270SMarcel Holtmann /* Check for rfkill but allow the HCI setup stage to 2208a5c8f270SMarcel Holtmann * proceed (which in itself doesn't cause any RF activity). 2209bf543036SJohan Hedberg */ 2210a5c8f270SMarcel Holtmann if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) { 2211611b30f7SMarcel Holtmann ret = -ERFKILL; 2212611b30f7SMarcel Holtmann goto done; 2213611b30f7SMarcel Holtmann } 2214611b30f7SMarcel Holtmann 2215a5c8f270SMarcel Holtmann /* Check for valid public address or a configured static 2216a5c8f270SMarcel Holtmann * random adddress, but let the HCI setup proceed to 2217a5c8f270SMarcel Holtmann * be able to determine if there is a public address 2218a5c8f270SMarcel Holtmann * or not. 2219a5c8f270SMarcel Holtmann * 2220c6beca0eSMarcel Holtmann * In case of user channel usage, it is not important 2221c6beca0eSMarcel Holtmann * if a public address or static random address is 2222c6beca0eSMarcel Holtmann * available. 2223c6beca0eSMarcel Holtmann * 2224a5c8f270SMarcel Holtmann * This check is only valid for BR/EDR controllers 2225a5c8f270SMarcel Holtmann * since AMP controllers do not have an address. 2226a5c8f270SMarcel Holtmann */ 2227c6beca0eSMarcel Holtmann if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && 2228c6beca0eSMarcel Holtmann hdev->dev_type == HCI_BREDR && 2229a5c8f270SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) && 2230a5c8f270SMarcel Holtmann !bacmp(&hdev->static_addr, BDADDR_ANY)) { 2231a5c8f270SMarcel Holtmann ret = -EADDRNOTAVAIL; 2232a5c8f270SMarcel Holtmann goto done; 2233a5c8f270SMarcel Holtmann } 2234a5c8f270SMarcel Holtmann } 2235a5c8f270SMarcel Holtmann 22361da177e4SLinus Torvalds if (test_bit(HCI_UP, &hdev->flags)) { 22371da177e4SLinus Torvalds ret = -EALREADY; 22381da177e4SLinus Torvalds goto done; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds if (hdev->open(hdev)) { 22421da177e4SLinus Torvalds ret = -EIO; 22431da177e4SLinus Torvalds goto done; 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 22471da177e4SLinus Torvalds set_bit(HCI_INIT, &hdev->flags); 2248f41c70c4SMarcel Holtmann 2249f41c70c4SMarcel Holtmann if (hdev->setup && test_bit(HCI_SETUP, &hdev->dev_flags)) 2250f41c70c4SMarcel Holtmann ret = hdev->setup(hdev); 2251f41c70c4SMarcel Holtmann 225224c457e2SMarcel Holtmann /* If public address change is configured, ensure that the 225324c457e2SMarcel Holtmann * address gets programmed. If the driver does not support 225424c457e2SMarcel Holtmann * changing the public address, fail the power on procedure. 225524c457e2SMarcel Holtmann */ 225624c457e2SMarcel Holtmann if (!ret && bacmp(&hdev->public_addr, BDADDR_ANY)) { 225724c457e2SMarcel Holtmann if (hdev->set_bdaddr) 225824c457e2SMarcel Holtmann ret = hdev->set_bdaddr(hdev, &hdev->public_addr); 225924c457e2SMarcel Holtmann else 226024c457e2SMarcel Holtmann ret = -EADDRNOTAVAIL; 226124c457e2SMarcel Holtmann } 226224c457e2SMarcel Holtmann 2263f41c70c4SMarcel Holtmann if (!ret) { 22644a964404SMarcel Holtmann if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 22650736cfa8SMarcel Holtmann !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) 22662177bab5SJohan Hedberg ret = __hci_init(hdev); 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 2269f41c70c4SMarcel Holtmann clear_bit(HCI_INIT, &hdev->flags); 2270f41c70c4SMarcel Holtmann 22711da177e4SLinus Torvalds if (!ret) { 22721da177e4SLinus Torvalds hci_dev_hold(hdev); 2273d6bfd59cSJohan Hedberg set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 22741da177e4SLinus Torvalds set_bit(HCI_UP, &hdev->flags); 22751da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_UP); 2276bb4b2a9aSAndrei Emeltchenko if (!test_bit(HCI_SETUP, &hdev->dev_flags) && 22774a964404SMarcel Holtmann !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 22780736cfa8SMarcel Holtmann !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && 22791514b892SMarcel Holtmann hdev->dev_type == HCI_BREDR) { 228009fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 2281744cf19eSJohan Hedberg mgmt_powered(hdev, 1); 228209fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 228356e5cb86SJohan Hedberg } 22841da177e4SLinus Torvalds } else { 22851da177e4SLinus Torvalds /* Init failed, cleanup */ 22863eff45eaSGustavo F. Padovan flush_work(&hdev->tx_work); 2287c347b765SGustavo F. Padovan flush_work(&hdev->cmd_work); 2288b78752ccSMarcel Holtmann flush_work(&hdev->rx_work); 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 22911da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds if (hdev->flush) 22941da177e4SLinus Torvalds hdev->flush(hdev); 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds if (hdev->sent_cmd) { 22971da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 22981da177e4SLinus Torvalds hdev->sent_cmd = NULL; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds hdev->close(hdev); 2302fee746b0SMarcel Holtmann hdev->flags &= BIT(HCI_RAW); 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds done: 23061da177e4SLinus Torvalds hci_req_unlock(hdev); 23071da177e4SLinus Torvalds return ret; 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds 2310cbed0ca1SJohan Hedberg /* ---- HCI ioctl helpers ---- */ 2311cbed0ca1SJohan Hedberg 2312cbed0ca1SJohan Hedberg int hci_dev_open(__u16 dev) 2313cbed0ca1SJohan Hedberg { 2314cbed0ca1SJohan Hedberg struct hci_dev *hdev; 2315cbed0ca1SJohan Hedberg int err; 2316cbed0ca1SJohan Hedberg 2317cbed0ca1SJohan Hedberg hdev = hci_dev_get(dev); 2318cbed0ca1SJohan Hedberg if (!hdev) 2319cbed0ca1SJohan Hedberg return -ENODEV; 2320cbed0ca1SJohan Hedberg 23214a964404SMarcel Holtmann /* Devices that are marked as unconfigured can only be powered 2322fee746b0SMarcel Holtmann * up as user channel. Trying to bring them up as normal devices 2323fee746b0SMarcel Holtmann * will result into a failure. Only user channel operation is 2324fee746b0SMarcel Holtmann * possible. 2325fee746b0SMarcel Holtmann * 2326fee746b0SMarcel Holtmann * When this function is called for a user channel, the flag 2327fee746b0SMarcel Holtmann * HCI_USER_CHANNEL will be set first before attempting to 2328fee746b0SMarcel Holtmann * open the device. 2329fee746b0SMarcel Holtmann */ 23304a964404SMarcel Holtmann if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 2331fee746b0SMarcel Holtmann !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 2332fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 2333fee746b0SMarcel Holtmann goto done; 2334fee746b0SMarcel Holtmann } 2335fee746b0SMarcel Holtmann 2336e1d08f40SJohan Hedberg /* We need to ensure that no other power on/off work is pending 2337e1d08f40SJohan Hedberg * before proceeding to call hci_dev_do_open. This is 2338e1d08f40SJohan Hedberg * particularly important if the setup procedure has not yet 2339e1d08f40SJohan Hedberg * completed. 2340e1d08f40SJohan Hedberg */ 2341e1d08f40SJohan Hedberg if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 2342e1d08f40SJohan Hedberg cancel_delayed_work(&hdev->power_off); 2343e1d08f40SJohan Hedberg 2344a5c8f270SMarcel Holtmann /* After this call it is guaranteed that the setup procedure 2345a5c8f270SMarcel Holtmann * has finished. This means that error conditions like RFKILL 2346a5c8f270SMarcel Holtmann * or no valid public or static random address apply. 2347a5c8f270SMarcel Holtmann */ 2348e1d08f40SJohan Hedberg flush_workqueue(hdev->req_workqueue); 2349e1d08f40SJohan Hedberg 2350cbed0ca1SJohan Hedberg err = hci_dev_do_open(hdev); 2351cbed0ca1SJohan Hedberg 2352fee746b0SMarcel Holtmann done: 2353cbed0ca1SJohan Hedberg hci_dev_put(hdev); 2354cbed0ca1SJohan Hedberg return err; 2355cbed0ca1SJohan Hedberg } 2356cbed0ca1SJohan Hedberg 2357d7347f3cSJohan Hedberg /* This function requires the caller holds hdev->lock */ 2358d7347f3cSJohan Hedberg static void hci_pend_le_actions_clear(struct hci_dev *hdev) 2359d7347f3cSJohan Hedberg { 2360d7347f3cSJohan Hedberg struct hci_conn_params *p; 2361d7347f3cSJohan Hedberg 2362d7347f3cSJohan Hedberg list_for_each_entry(p, &hdev->le_conn_params, list) 2363d7347f3cSJohan Hedberg list_del_init(&p->action); 2364d7347f3cSJohan Hedberg 2365d7347f3cSJohan Hedberg BT_DBG("All LE pending actions cleared"); 2366d7347f3cSJohan Hedberg } 2367d7347f3cSJohan Hedberg 23681da177e4SLinus Torvalds static int hci_dev_do_close(struct hci_dev *hdev) 23691da177e4SLinus Torvalds { 23701da177e4SLinus Torvalds BT_DBG("%s %p", hdev->name, hdev); 23711da177e4SLinus Torvalds 237278c04c0bSVinicius Costa Gomes cancel_delayed_work(&hdev->power_off); 237378c04c0bSVinicius Costa Gomes 23741da177e4SLinus Torvalds hci_req_cancel(hdev, ENODEV); 23751da177e4SLinus Torvalds hci_req_lock(hdev); 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { 237865cc2b49SMarcel Holtmann cancel_delayed_work_sync(&hdev->cmd_timer); 23791da177e4SLinus Torvalds hci_req_unlock(hdev); 23801da177e4SLinus Torvalds return 0; 23811da177e4SLinus Torvalds } 23821da177e4SLinus Torvalds 23833eff45eaSGustavo F. Padovan /* Flush RX and TX works */ 23843eff45eaSGustavo F. Padovan flush_work(&hdev->tx_work); 2385b78752ccSMarcel Holtmann flush_work(&hdev->rx_work); 23861da177e4SLinus Torvalds 238716ab91abSJohan Hedberg if (hdev->discov_timeout > 0) { 2388e0f9309fSJohan Hedberg cancel_delayed_work(&hdev->discov_off); 238916ab91abSJohan Hedberg hdev->discov_timeout = 0; 23905e5282bbSJohan Hedberg clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 2391310a3d48SMarcel Holtmann clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 239216ab91abSJohan Hedberg } 239316ab91abSJohan Hedberg 2394a8b2d5c2SJohan Hedberg if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 23957d78525dSJohan Hedberg cancel_delayed_work(&hdev->service_cache); 23967d78525dSJohan Hedberg 23977ba8b4beSAndre Guedes cancel_delayed_work_sync(&hdev->le_scan_disable); 23984518bb0fSJohan Hedberg 23994518bb0fSJohan Hedberg if (test_bit(HCI_MGMT, &hdev->dev_flags)) 2400d6bfd59cSJohan Hedberg cancel_delayed_work_sync(&hdev->rpa_expired); 24017ba8b4beSAndre Guedes 240209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 24031f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 24041da177e4SLinus Torvalds hci_conn_hash_flush(hdev); 2405d7347f3cSJohan Hedberg hci_pend_le_actions_clear(hdev); 240609fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_DOWN); 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds if (hdev->flush) 24111da177e4SLinus Torvalds hdev->flush(hdev); 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds /* Reset device */ 24141da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 24151da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 24164a964404SMarcel Holtmann if (!test_bit(HCI_AUTO_OFF, &hdev->dev_flags) && 24174a964404SMarcel Holtmann !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 2418a6c511c6SSzymon Janc test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 24191da177e4SLinus Torvalds set_bit(HCI_INIT, &hdev->flags); 242001178cd4SJohan Hedberg __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); 24211da177e4SLinus Torvalds clear_bit(HCI_INIT, &hdev->flags); 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 2424c347b765SGustavo F. Padovan /* flush cmd work */ 2425c347b765SGustavo F. Padovan flush_work(&hdev->cmd_work); 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds /* Drop queues */ 24281da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 24291da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 24301da177e4SLinus Torvalds skb_queue_purge(&hdev->raw_q); 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds /* Drop last sent command */ 24331da177e4SLinus Torvalds if (hdev->sent_cmd) { 243465cc2b49SMarcel Holtmann cancel_delayed_work_sync(&hdev->cmd_timer); 24351da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 24361da177e4SLinus Torvalds hdev->sent_cmd = NULL; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds 2439b6ddb638SJohan Hedberg kfree_skb(hdev->recv_evt); 2440b6ddb638SJohan Hedberg hdev->recv_evt = NULL; 2441b6ddb638SJohan Hedberg 24421da177e4SLinus Torvalds /* After this point our queues are empty 24431da177e4SLinus Torvalds * and no tasks are scheduled. */ 24441da177e4SLinus Torvalds hdev->close(hdev); 24451da177e4SLinus Torvalds 244635b973c9SJohan Hedberg /* Clear flags */ 2447fee746b0SMarcel Holtmann hdev->flags &= BIT(HCI_RAW); 244835b973c9SJohan Hedberg hdev->dev_flags &= ~HCI_PERSISTENT_MASK; 244935b973c9SJohan Hedberg 245093c311a0SMarcel Holtmann if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 245193c311a0SMarcel Holtmann if (hdev->dev_type == HCI_BREDR) { 245209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 2453744cf19eSJohan Hedberg mgmt_powered(hdev, 0); 245409fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 24558ee56540SMarcel Holtmann } 245693c311a0SMarcel Holtmann } 24575add6af8SJohan Hedberg 2458ced5c338SAndrei Emeltchenko /* Controller radio is available but is currently powered down */ 2459536619e8SMarcel Holtmann hdev->amp_status = AMP_STATUS_POWERED_DOWN; 2460ced5c338SAndrei Emeltchenko 2461e59fda8dSJohan Hedberg memset(hdev->eir, 0, sizeof(hdev->eir)); 246209b3c3fbSJohan Hedberg memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); 24637a4cd51dSMarcel Holtmann bacpy(&hdev->random_addr, BDADDR_ANY); 2464e59fda8dSJohan Hedberg 24651da177e4SLinus Torvalds hci_req_unlock(hdev); 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds hci_dev_put(hdev); 24681da177e4SLinus Torvalds return 0; 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds int hci_dev_close(__u16 dev) 24721da177e4SLinus Torvalds { 24731da177e4SLinus Torvalds struct hci_dev *hdev; 24741da177e4SLinus Torvalds int err; 24751da177e4SLinus Torvalds 247670f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 247770f23020SAndrei Emeltchenko if (!hdev) 24781da177e4SLinus Torvalds return -ENODEV; 24798ee56540SMarcel Holtmann 24800736cfa8SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 24810736cfa8SMarcel Holtmann err = -EBUSY; 24820736cfa8SMarcel Holtmann goto done; 24830736cfa8SMarcel Holtmann } 24840736cfa8SMarcel Holtmann 24858ee56540SMarcel Holtmann if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 24868ee56540SMarcel Holtmann cancel_delayed_work(&hdev->power_off); 24878ee56540SMarcel Holtmann 24881da177e4SLinus Torvalds err = hci_dev_do_close(hdev); 24898ee56540SMarcel Holtmann 24900736cfa8SMarcel Holtmann done: 24911da177e4SLinus Torvalds hci_dev_put(hdev); 24921da177e4SLinus Torvalds return err; 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds int hci_dev_reset(__u16 dev) 24961da177e4SLinus Torvalds { 24971da177e4SLinus Torvalds struct hci_dev *hdev; 24981da177e4SLinus Torvalds int ret = 0; 24991da177e4SLinus Torvalds 250070f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 250170f23020SAndrei Emeltchenko if (!hdev) 25021da177e4SLinus Torvalds return -ENODEV; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds hci_req_lock(hdev); 25051da177e4SLinus Torvalds 2506808a049eSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 2507808a049eSMarcel Holtmann ret = -ENETDOWN; 25081da177e4SLinus Torvalds goto done; 2509808a049eSMarcel Holtmann } 25101da177e4SLinus Torvalds 25110736cfa8SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 25120736cfa8SMarcel Holtmann ret = -EBUSY; 25130736cfa8SMarcel Holtmann goto done; 25140736cfa8SMarcel Holtmann } 25150736cfa8SMarcel Holtmann 25164a964404SMarcel Holtmann if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 2517fee746b0SMarcel Holtmann ret = -EOPNOTSUPP; 2518fee746b0SMarcel Holtmann goto done; 2519fee746b0SMarcel Holtmann } 2520fee746b0SMarcel Holtmann 25211da177e4SLinus Torvalds /* Drop queues */ 25221da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 25231da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 25241da177e4SLinus Torvalds 252509fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 25261f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 25271da177e4SLinus Torvalds hci_conn_hash_flush(hdev); 252809fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds if (hdev->flush) 25311da177e4SLinus Torvalds hdev->flush(hdev); 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 25346ed58ec5SVille Tervo hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; 25351da177e4SLinus Torvalds 253601178cd4SJohan Hedberg ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds done: 25391da177e4SLinus Torvalds hci_req_unlock(hdev); 25401da177e4SLinus Torvalds hci_dev_put(hdev); 25411da177e4SLinus Torvalds return ret; 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev) 25451da177e4SLinus Torvalds { 25461da177e4SLinus Torvalds struct hci_dev *hdev; 25471da177e4SLinus Torvalds int ret = 0; 25481da177e4SLinus Torvalds 254970f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 255070f23020SAndrei Emeltchenko if (!hdev) 25511da177e4SLinus Torvalds return -ENODEV; 25521da177e4SLinus Torvalds 25530736cfa8SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 25540736cfa8SMarcel Holtmann ret = -EBUSY; 25550736cfa8SMarcel Holtmann goto done; 25560736cfa8SMarcel Holtmann } 25570736cfa8SMarcel Holtmann 25584a964404SMarcel Holtmann if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 2559fee746b0SMarcel Holtmann ret = -EOPNOTSUPP; 2560fee746b0SMarcel Holtmann goto done; 2561fee746b0SMarcel Holtmann } 2562fee746b0SMarcel Holtmann 25631da177e4SLinus Torvalds memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); 25641da177e4SLinus Torvalds 25650736cfa8SMarcel Holtmann done: 25661da177e4SLinus Torvalds hci_dev_put(hdev); 25671da177e4SLinus Torvalds return ret; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg) 25711da177e4SLinus Torvalds { 25721da177e4SLinus Torvalds struct hci_dev *hdev; 25731da177e4SLinus Torvalds struct hci_dev_req dr; 25741da177e4SLinus Torvalds int err = 0; 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds if (copy_from_user(&dr, arg, sizeof(dr))) 25771da177e4SLinus Torvalds return -EFAULT; 25781da177e4SLinus Torvalds 257970f23020SAndrei Emeltchenko hdev = hci_dev_get(dr.dev_id); 258070f23020SAndrei Emeltchenko if (!hdev) 25811da177e4SLinus Torvalds return -ENODEV; 25821da177e4SLinus Torvalds 25830736cfa8SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 25840736cfa8SMarcel Holtmann err = -EBUSY; 25850736cfa8SMarcel Holtmann goto done; 25860736cfa8SMarcel Holtmann } 25870736cfa8SMarcel Holtmann 25884a964404SMarcel Holtmann if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 2589fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 2590fee746b0SMarcel Holtmann goto done; 2591fee746b0SMarcel Holtmann } 2592fee746b0SMarcel Holtmann 25935b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) { 25945b69bef5SMarcel Holtmann err = -EOPNOTSUPP; 25955b69bef5SMarcel Holtmann goto done; 25965b69bef5SMarcel Holtmann } 25975b69bef5SMarcel Holtmann 259856f87901SJohan Hedberg if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 259956f87901SJohan Hedberg err = -EOPNOTSUPP; 260056f87901SJohan Hedberg goto done; 260156f87901SJohan Hedberg } 260256f87901SJohan Hedberg 26031da177e4SLinus Torvalds switch (cmd) { 26041da177e4SLinus Torvalds case HCISETAUTH: 260501178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 26065f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 26071da177e4SLinus Torvalds break; 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds case HCISETENCRYPT: 26101da177e4SLinus Torvalds if (!lmp_encrypt_capable(hdev)) { 26111da177e4SLinus Torvalds err = -EOPNOTSUPP; 26121da177e4SLinus Torvalds break; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds if (!test_bit(HCI_AUTH, &hdev->flags)) { 26161da177e4SLinus Torvalds /* Auth must be enabled first */ 261701178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 26185f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 26191da177e4SLinus Torvalds if (err) 26201da177e4SLinus Torvalds break; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 262301178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, 26245f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 26251da177e4SLinus Torvalds break; 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds case HCISETSCAN: 262801178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, 26295f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 26301da177e4SLinus Torvalds break; 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds case HCISETLINKPOL: 263301178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, 26345f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 26351da177e4SLinus Torvalds break; 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds case HCISETLINKMODE: 2638e4e8e37cSMarcel Holtmann hdev->link_mode = ((__u16) dr.dev_opt) & 2639e4e8e37cSMarcel Holtmann (HCI_LM_MASTER | HCI_LM_ACCEPT); 2640e4e8e37cSMarcel Holtmann break; 2641e4e8e37cSMarcel Holtmann 2642e4e8e37cSMarcel Holtmann case HCISETPTYPE: 2643e4e8e37cSMarcel Holtmann hdev->pkt_type = (__u16) dr.dev_opt; 26441da177e4SLinus Torvalds break; 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds case HCISETACLMTU: 26471da177e4SLinus Torvalds hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); 26481da177e4SLinus Torvalds hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); 26491da177e4SLinus Torvalds break; 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds case HCISETSCOMTU: 26521da177e4SLinus Torvalds hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); 26531da177e4SLinus Torvalds hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); 26541da177e4SLinus Torvalds break; 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds default: 26571da177e4SLinus Torvalds err = -EINVAL; 26581da177e4SLinus Torvalds break; 26591da177e4SLinus Torvalds } 2660e4e8e37cSMarcel Holtmann 26610736cfa8SMarcel Holtmann done: 26621da177e4SLinus Torvalds hci_dev_put(hdev); 26631da177e4SLinus Torvalds return err; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg) 26671da177e4SLinus Torvalds { 26688035ded4SLuiz Augusto von Dentz struct hci_dev *hdev; 26691da177e4SLinus Torvalds struct hci_dev_list_req *dl; 26701da177e4SLinus Torvalds struct hci_dev_req *dr; 26711da177e4SLinus Torvalds int n = 0, size, err; 26721da177e4SLinus Torvalds __u16 dev_num; 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds if (get_user(dev_num, (__u16 __user *) arg)) 26751da177e4SLinus Torvalds return -EFAULT; 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) 26781da177e4SLinus Torvalds return -EINVAL; 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds size = sizeof(*dl) + dev_num * sizeof(*dr); 26811da177e4SLinus Torvalds 268270f23020SAndrei Emeltchenko dl = kzalloc(size, GFP_KERNEL); 268370f23020SAndrei Emeltchenko if (!dl) 26841da177e4SLinus Torvalds return -ENOMEM; 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds dr = dl->dev_req; 26871da177e4SLinus Torvalds 2688f20d09d5SGustavo F. Padovan read_lock(&hci_dev_list_lock); 26898035ded4SLuiz Augusto von Dentz list_for_each_entry(hdev, &hci_dev_list, list) { 2690a8b2d5c2SJohan Hedberg if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 2691e0f9309fSJohan Hedberg cancel_delayed_work(&hdev->power_off); 2692c542a06cSJohan Hedberg 2693a8b2d5c2SJohan Hedberg if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 2694a8b2d5c2SJohan Hedberg set_bit(HCI_PAIRABLE, &hdev->dev_flags); 2695c542a06cSJohan Hedberg 26961da177e4SLinus Torvalds (dr + n)->dev_id = hdev->id; 26971da177e4SLinus Torvalds (dr + n)->dev_opt = hdev->flags; 2698c542a06cSJohan Hedberg 26991da177e4SLinus Torvalds if (++n >= dev_num) 27001da177e4SLinus Torvalds break; 27011da177e4SLinus Torvalds } 2702f20d09d5SGustavo F. Padovan read_unlock(&hci_dev_list_lock); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds dl->dev_num = n; 27051da177e4SLinus Torvalds size = sizeof(*dl) + n * sizeof(*dr); 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds err = copy_to_user(arg, dl, size); 27081da177e4SLinus Torvalds kfree(dl); 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds return err ? -EFAULT : 0; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg) 27141da177e4SLinus Torvalds { 27151da177e4SLinus Torvalds struct hci_dev *hdev; 27161da177e4SLinus Torvalds struct hci_dev_info di; 27171da177e4SLinus Torvalds int err = 0; 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds if (copy_from_user(&di, arg, sizeof(di))) 27201da177e4SLinus Torvalds return -EFAULT; 27211da177e4SLinus Torvalds 272270f23020SAndrei Emeltchenko hdev = hci_dev_get(di.dev_id); 272370f23020SAndrei Emeltchenko if (!hdev) 27241da177e4SLinus Torvalds return -ENODEV; 27251da177e4SLinus Torvalds 2726a8b2d5c2SJohan Hedberg if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 27273243553fSJohan Hedberg cancel_delayed_work_sync(&hdev->power_off); 2728ab81cbf9SJohan Hedberg 2729a8b2d5c2SJohan Hedberg if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 2730a8b2d5c2SJohan Hedberg set_bit(HCI_PAIRABLE, &hdev->dev_flags); 2731c542a06cSJohan Hedberg 27321da177e4SLinus Torvalds strcpy(di.name, hdev->name); 27331da177e4SLinus Torvalds di.bdaddr = hdev->bdaddr; 273460f2a3edSMarcel Holtmann di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); 27351da177e4SLinus Torvalds di.flags = hdev->flags; 27361da177e4SLinus Torvalds di.pkt_type = hdev->pkt_type; 2737572c7f84SJohan Hedberg if (lmp_bredr_capable(hdev)) { 27381da177e4SLinus Torvalds di.acl_mtu = hdev->acl_mtu; 27391da177e4SLinus Torvalds di.acl_pkts = hdev->acl_pkts; 27401da177e4SLinus Torvalds di.sco_mtu = hdev->sco_mtu; 27411da177e4SLinus Torvalds di.sco_pkts = hdev->sco_pkts; 2742572c7f84SJohan Hedberg } else { 2743572c7f84SJohan Hedberg di.acl_mtu = hdev->le_mtu; 2744572c7f84SJohan Hedberg di.acl_pkts = hdev->le_pkts; 2745572c7f84SJohan Hedberg di.sco_mtu = 0; 2746572c7f84SJohan Hedberg di.sco_pkts = 0; 2747572c7f84SJohan Hedberg } 27481da177e4SLinus Torvalds di.link_policy = hdev->link_policy; 27491da177e4SLinus Torvalds di.link_mode = hdev->link_mode; 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); 27521da177e4SLinus Torvalds memcpy(&di.features, &hdev->features, sizeof(di.features)); 27531da177e4SLinus Torvalds 27541da177e4SLinus Torvalds if (copy_to_user(arg, &di, sizeof(di))) 27551da177e4SLinus Torvalds err = -EFAULT; 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds hci_dev_put(hdev); 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds return err; 27601da177e4SLinus Torvalds } 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds /* ---- Interface to HCI drivers ---- */ 27631da177e4SLinus Torvalds 2764611b30f7SMarcel Holtmann static int hci_rfkill_set_block(void *data, bool blocked) 2765611b30f7SMarcel Holtmann { 2766611b30f7SMarcel Holtmann struct hci_dev *hdev = data; 2767611b30f7SMarcel Holtmann 2768611b30f7SMarcel Holtmann BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); 2769611b30f7SMarcel Holtmann 27700736cfa8SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) 27710736cfa8SMarcel Holtmann return -EBUSY; 27720736cfa8SMarcel Holtmann 27735e130367SJohan Hedberg if (blocked) { 27745e130367SJohan Hedberg set_bit(HCI_RFKILLED, &hdev->dev_flags); 2775bf543036SJohan Hedberg if (!test_bit(HCI_SETUP, &hdev->dev_flags)) 2776611b30f7SMarcel Holtmann hci_dev_do_close(hdev); 27775e130367SJohan Hedberg } else { 27785e130367SJohan Hedberg clear_bit(HCI_RFKILLED, &hdev->dev_flags); 27795e130367SJohan Hedberg } 2780611b30f7SMarcel Holtmann 2781611b30f7SMarcel Holtmann return 0; 2782611b30f7SMarcel Holtmann } 2783611b30f7SMarcel Holtmann 2784611b30f7SMarcel Holtmann static const struct rfkill_ops hci_rfkill_ops = { 2785611b30f7SMarcel Holtmann .set_block = hci_rfkill_set_block, 2786611b30f7SMarcel Holtmann }; 2787611b30f7SMarcel Holtmann 2788ab81cbf9SJohan Hedberg static void hci_power_on(struct work_struct *work) 2789ab81cbf9SJohan Hedberg { 2790ab81cbf9SJohan Hedberg struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); 279196570ffcSJohan Hedberg int err; 2792ab81cbf9SJohan Hedberg 2793ab81cbf9SJohan Hedberg BT_DBG("%s", hdev->name); 2794ab81cbf9SJohan Hedberg 2795cbed0ca1SJohan Hedberg err = hci_dev_do_open(hdev); 279696570ffcSJohan Hedberg if (err < 0) { 279796570ffcSJohan Hedberg mgmt_set_powered_failed(hdev, err); 2798ab81cbf9SJohan Hedberg return; 279996570ffcSJohan Hedberg } 2800ab81cbf9SJohan Hedberg 2801a5c8f270SMarcel Holtmann /* During the HCI setup phase, a few error conditions are 2802a5c8f270SMarcel Holtmann * ignored and they need to be checked now. If they are still 2803a5c8f270SMarcel Holtmann * valid, it is important to turn the device back off. 2804a5c8f270SMarcel Holtmann */ 2805a5c8f270SMarcel Holtmann if (test_bit(HCI_RFKILLED, &hdev->dev_flags) || 28064a964404SMarcel Holtmann test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) || 2807a5c8f270SMarcel Holtmann (hdev->dev_type == HCI_BREDR && 2808a5c8f270SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) && 2809a5c8f270SMarcel Holtmann !bacmp(&hdev->static_addr, BDADDR_ANY))) { 2810bf543036SJohan Hedberg clear_bit(HCI_AUTO_OFF, &hdev->dev_flags); 2811bf543036SJohan Hedberg hci_dev_do_close(hdev); 2812bf543036SJohan Hedberg } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 281319202573SJohan Hedberg queue_delayed_work(hdev->req_workqueue, &hdev->power_off, 281419202573SJohan Hedberg HCI_AUTO_OFF_TIMEOUT); 2815bf543036SJohan Hedberg } 2816ab81cbf9SJohan Hedberg 2817fee746b0SMarcel Holtmann if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) { 28184a964404SMarcel Holtmann /* For unconfigured devices, set the HCI_RAW flag 28194a964404SMarcel Holtmann * so that userspace can easily identify them. 28204a964404SMarcel Holtmann */ 28214a964404SMarcel Holtmann if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 28224a964404SMarcel Holtmann set_bit(HCI_RAW, &hdev->flags); 28230602a8adSMarcel Holtmann 28240602a8adSMarcel Holtmann /* For fully configured devices, this will send 28250602a8adSMarcel Holtmann * the Index Added event. For unconfigured devices, 28260602a8adSMarcel Holtmann * it will send Unconfigued Index Added event. 28270602a8adSMarcel Holtmann * 28280602a8adSMarcel Holtmann * Devices with HCI_QUIRK_RAW_DEVICE are ignored 28290602a8adSMarcel Holtmann * and no event will be send. 28300602a8adSMarcel Holtmann */ 2831744cf19eSJohan Hedberg mgmt_index_added(hdev); 2832ab81cbf9SJohan Hedberg } 2833fee746b0SMarcel Holtmann } 2834ab81cbf9SJohan Hedberg 2835ab81cbf9SJohan Hedberg static void hci_power_off(struct work_struct *work) 2836ab81cbf9SJohan Hedberg { 28373243553fSJohan Hedberg struct hci_dev *hdev = container_of(work, struct hci_dev, 28383243553fSJohan Hedberg power_off.work); 2839ab81cbf9SJohan Hedberg 2840ab81cbf9SJohan Hedberg BT_DBG("%s", hdev->name); 2841ab81cbf9SJohan Hedberg 28428ee56540SMarcel Holtmann hci_dev_do_close(hdev); 2843ab81cbf9SJohan Hedberg } 2844ab81cbf9SJohan Hedberg 284516ab91abSJohan Hedberg static void hci_discov_off(struct work_struct *work) 284616ab91abSJohan Hedberg { 284716ab91abSJohan Hedberg struct hci_dev *hdev; 284816ab91abSJohan Hedberg 284916ab91abSJohan Hedberg hdev = container_of(work, struct hci_dev, discov_off.work); 285016ab91abSJohan Hedberg 285116ab91abSJohan Hedberg BT_DBG("%s", hdev->name); 285216ab91abSJohan Hedberg 2853d1967ff8SMarcel Holtmann mgmt_discoverable_timeout(hdev); 285416ab91abSJohan Hedberg } 285516ab91abSJohan Hedberg 285635f7498aSJohan Hedberg void hci_uuids_clear(struct hci_dev *hdev) 28572aeb9a1aSJohan Hedberg { 28584821002cSJohan Hedberg struct bt_uuid *uuid, *tmp; 28592aeb9a1aSJohan Hedberg 28604821002cSJohan Hedberg list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { 28614821002cSJohan Hedberg list_del(&uuid->list); 28622aeb9a1aSJohan Hedberg kfree(uuid); 28632aeb9a1aSJohan Hedberg } 28642aeb9a1aSJohan Hedberg } 28652aeb9a1aSJohan Hedberg 286635f7498aSJohan Hedberg void hci_link_keys_clear(struct hci_dev *hdev) 286755ed8ca1SJohan Hedberg { 286855ed8ca1SJohan Hedberg struct list_head *p, *n; 286955ed8ca1SJohan Hedberg 287055ed8ca1SJohan Hedberg list_for_each_safe(p, n, &hdev->link_keys) { 287155ed8ca1SJohan Hedberg struct link_key *key; 287255ed8ca1SJohan Hedberg 287355ed8ca1SJohan Hedberg key = list_entry(p, struct link_key, list); 287455ed8ca1SJohan Hedberg 287555ed8ca1SJohan Hedberg list_del(p); 287655ed8ca1SJohan Hedberg kfree(key); 287755ed8ca1SJohan Hedberg } 287855ed8ca1SJohan Hedberg } 287955ed8ca1SJohan Hedberg 288035f7498aSJohan Hedberg void hci_smp_ltks_clear(struct hci_dev *hdev) 2881b899efafSVinicius Costa Gomes { 2882b899efafSVinicius Costa Gomes struct smp_ltk *k, *tmp; 2883b899efafSVinicius Costa Gomes 2884b899efafSVinicius Costa Gomes list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { 2885b899efafSVinicius Costa Gomes list_del(&k->list); 2886b899efafSVinicius Costa Gomes kfree(k); 2887b899efafSVinicius Costa Gomes } 2888b899efafSVinicius Costa Gomes } 2889b899efafSVinicius Costa Gomes 2890970c4e46SJohan Hedberg void hci_smp_irks_clear(struct hci_dev *hdev) 2891970c4e46SJohan Hedberg { 2892970c4e46SJohan Hedberg struct smp_irk *k, *tmp; 2893970c4e46SJohan Hedberg 2894970c4e46SJohan Hedberg list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) { 2895970c4e46SJohan Hedberg list_del(&k->list); 2896970c4e46SJohan Hedberg kfree(k); 2897970c4e46SJohan Hedberg } 2898970c4e46SJohan Hedberg } 2899970c4e46SJohan Hedberg 290055ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) 290155ed8ca1SJohan Hedberg { 290255ed8ca1SJohan Hedberg struct link_key *k; 290355ed8ca1SJohan Hedberg 29048035ded4SLuiz Augusto von Dentz list_for_each_entry(k, &hdev->link_keys, list) 290555ed8ca1SJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) == 0) 290655ed8ca1SJohan Hedberg return k; 290755ed8ca1SJohan Hedberg 290855ed8ca1SJohan Hedberg return NULL; 290955ed8ca1SJohan Hedberg } 291055ed8ca1SJohan Hedberg 2911745c0ce3SVishal Agarwal static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, 2912d25e28abSJohan Hedberg u8 key_type, u8 old_key_type) 2913d25e28abSJohan Hedberg { 2914d25e28abSJohan Hedberg /* Legacy key */ 2915d25e28abSJohan Hedberg if (key_type < 0x03) 2916745c0ce3SVishal Agarwal return true; 2917d25e28abSJohan Hedberg 2918d25e28abSJohan Hedberg /* Debug keys are insecure so don't store them persistently */ 2919d25e28abSJohan Hedberg if (key_type == HCI_LK_DEBUG_COMBINATION) 2920745c0ce3SVishal Agarwal return false; 2921d25e28abSJohan Hedberg 2922d25e28abSJohan Hedberg /* Changed combination key and there's no previous one */ 2923d25e28abSJohan Hedberg if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) 2924745c0ce3SVishal Agarwal return false; 2925d25e28abSJohan Hedberg 2926d25e28abSJohan Hedberg /* Security mode 3 case */ 2927d25e28abSJohan Hedberg if (!conn) 2928745c0ce3SVishal Agarwal return true; 2929d25e28abSJohan Hedberg 2930d25e28abSJohan Hedberg /* Neither local nor remote side had no-bonding as requirement */ 2931d25e28abSJohan Hedberg if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) 2932745c0ce3SVishal Agarwal return true; 2933d25e28abSJohan Hedberg 2934d25e28abSJohan Hedberg /* Local side had dedicated bonding as requirement */ 2935d25e28abSJohan Hedberg if (conn->auth_type == 0x02 || conn->auth_type == 0x03) 2936745c0ce3SVishal Agarwal return true; 2937d25e28abSJohan Hedberg 2938d25e28abSJohan Hedberg /* Remote side had dedicated bonding as requirement */ 2939d25e28abSJohan Hedberg if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) 2940745c0ce3SVishal Agarwal return true; 2941d25e28abSJohan Hedberg 2942d25e28abSJohan Hedberg /* If none of the above criteria match, then don't store the key 2943d25e28abSJohan Hedberg * persistently */ 2944745c0ce3SVishal Agarwal return false; 2945d25e28abSJohan Hedberg } 2946d25e28abSJohan Hedberg 294798a0b845SJohan Hedberg static bool ltk_type_master(u8 type) 294898a0b845SJohan Hedberg { 2949d97c9fb0SJohan Hedberg return (type == SMP_LTK); 295098a0b845SJohan Hedberg } 295198a0b845SJohan Hedberg 2952fe39c7b2SMarcel Holtmann struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand, 295398a0b845SJohan Hedberg bool master) 295475d262c2SVinicius Costa Gomes { 2955c9839a11SVinicius Costa Gomes struct smp_ltk *k; 295675d262c2SVinicius Costa Gomes 2957c9839a11SVinicius Costa Gomes list_for_each_entry(k, &hdev->long_term_keys, list) { 2958fe39c7b2SMarcel Holtmann if (k->ediv != ediv || k->rand != rand) 295975d262c2SVinicius Costa Gomes continue; 296075d262c2SVinicius Costa Gomes 296198a0b845SJohan Hedberg if (ltk_type_master(k->type) != master) 296298a0b845SJohan Hedberg continue; 296398a0b845SJohan Hedberg 296475d262c2SVinicius Costa Gomes return k; 296575d262c2SVinicius Costa Gomes } 296675d262c2SVinicius Costa Gomes 296775d262c2SVinicius Costa Gomes return NULL; 296875d262c2SVinicius Costa Gomes } 296975d262c2SVinicius Costa Gomes 2970c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, 297198a0b845SJohan Hedberg u8 addr_type, bool master) 297275d262c2SVinicius Costa Gomes { 2973c9839a11SVinicius Costa Gomes struct smp_ltk *k; 297475d262c2SVinicius Costa Gomes 2975c9839a11SVinicius Costa Gomes list_for_each_entry(k, &hdev->long_term_keys, list) 2976c9839a11SVinicius Costa Gomes if (addr_type == k->bdaddr_type && 297798a0b845SJohan Hedberg bacmp(bdaddr, &k->bdaddr) == 0 && 297898a0b845SJohan Hedberg ltk_type_master(k->type) == master) 297975d262c2SVinicius Costa Gomes return k; 298075d262c2SVinicius Costa Gomes 298175d262c2SVinicius Costa Gomes return NULL; 298275d262c2SVinicius Costa Gomes } 298375d262c2SVinicius Costa Gomes 2984970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa) 2985970c4e46SJohan Hedberg { 2986970c4e46SJohan Hedberg struct smp_irk *irk; 2987970c4e46SJohan Hedberg 2988970c4e46SJohan Hedberg list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { 2989970c4e46SJohan Hedberg if (!bacmp(&irk->rpa, rpa)) 2990970c4e46SJohan Hedberg return irk; 2991970c4e46SJohan Hedberg } 2992970c4e46SJohan Hedberg 2993970c4e46SJohan Hedberg list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { 2994970c4e46SJohan Hedberg if (smp_irk_matches(hdev->tfm_aes, irk->val, rpa)) { 2995970c4e46SJohan Hedberg bacpy(&irk->rpa, rpa); 2996970c4e46SJohan Hedberg return irk; 2997970c4e46SJohan Hedberg } 2998970c4e46SJohan Hedberg } 2999970c4e46SJohan Hedberg 3000970c4e46SJohan Hedberg return NULL; 3001970c4e46SJohan Hedberg } 3002970c4e46SJohan Hedberg 3003970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, 3004970c4e46SJohan Hedberg u8 addr_type) 3005970c4e46SJohan Hedberg { 3006970c4e46SJohan Hedberg struct smp_irk *irk; 3007970c4e46SJohan Hedberg 30086cfc9988SJohan Hedberg /* Identity Address must be public or static random */ 30096cfc9988SJohan Hedberg if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0) 30106cfc9988SJohan Hedberg return NULL; 30116cfc9988SJohan Hedberg 3012970c4e46SJohan Hedberg list_for_each_entry(irk, &hdev->identity_resolving_keys, list) { 3013970c4e46SJohan Hedberg if (addr_type == irk->addr_type && 3014970c4e46SJohan Hedberg bacmp(bdaddr, &irk->bdaddr) == 0) 3015970c4e46SJohan Hedberg return irk; 3016970c4e46SJohan Hedberg } 3017970c4e46SJohan Hedberg 3018970c4e46SJohan Hedberg return NULL; 3019970c4e46SJohan Hedberg } 3020970c4e46SJohan Hedberg 3021567fa2aaSJohan Hedberg struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, 30227652ff6aSJohan Hedberg bdaddr_t *bdaddr, u8 *val, u8 type, 30237652ff6aSJohan Hedberg u8 pin_len, bool *persistent) 302455ed8ca1SJohan Hedberg { 302555ed8ca1SJohan Hedberg struct link_key *key, *old_key; 3026745c0ce3SVishal Agarwal u8 old_key_type; 302755ed8ca1SJohan Hedberg 302855ed8ca1SJohan Hedberg old_key = hci_find_link_key(hdev, bdaddr); 302955ed8ca1SJohan Hedberg if (old_key) { 303055ed8ca1SJohan Hedberg old_key_type = old_key->type; 303155ed8ca1SJohan Hedberg key = old_key; 303255ed8ca1SJohan Hedberg } else { 303312adcf3aSJohan Hedberg old_key_type = conn ? conn->key_type : 0xff; 30340a14ab41SJohan Hedberg key = kzalloc(sizeof(*key), GFP_KERNEL); 303555ed8ca1SJohan Hedberg if (!key) 3036567fa2aaSJohan Hedberg return NULL; 303755ed8ca1SJohan Hedberg list_add(&key->list, &hdev->link_keys); 303855ed8ca1SJohan Hedberg } 303955ed8ca1SJohan Hedberg 30406ed93dc6SAndrei Emeltchenko BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); 304155ed8ca1SJohan Hedberg 3042d25e28abSJohan Hedberg /* Some buggy controller combinations generate a changed 3043d25e28abSJohan Hedberg * combination key for legacy pairing even when there's no 3044d25e28abSJohan Hedberg * previous key */ 3045d25e28abSJohan Hedberg if (type == HCI_LK_CHANGED_COMBINATION && 3046a8c5fb1aSGustavo Padovan (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { 3047d25e28abSJohan Hedberg type = HCI_LK_COMBINATION; 3048655fe6ecSJohan Hedberg if (conn) 3049655fe6ecSJohan Hedberg conn->key_type = type; 3050655fe6ecSJohan Hedberg } 3051d25e28abSJohan Hedberg 305255ed8ca1SJohan Hedberg bacpy(&key->bdaddr, bdaddr); 30539b3b4460SAndrei Emeltchenko memcpy(key->val, val, HCI_LINK_KEY_SIZE); 305455ed8ca1SJohan Hedberg key->pin_len = pin_len; 305555ed8ca1SJohan Hedberg 3056b6020ba0SWaldemar Rymarkiewicz if (type == HCI_LK_CHANGED_COMBINATION) 305755ed8ca1SJohan Hedberg key->type = old_key_type; 30584748fed2SJohan Hedberg else 30594748fed2SJohan Hedberg key->type = type; 30604748fed2SJohan Hedberg 30617652ff6aSJohan Hedberg if (persistent) 30627652ff6aSJohan Hedberg *persistent = hci_persistent_key(hdev, conn, type, 30637652ff6aSJohan Hedberg old_key_type); 306455ed8ca1SJohan Hedberg 3065567fa2aaSJohan Hedberg return key; 306655ed8ca1SJohan Hedberg } 306755ed8ca1SJohan Hedberg 3068ca9142b8SJohan Hedberg struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, 306935d70271SJohan Hedberg u8 addr_type, u8 type, u8 authenticated, 3070fe39c7b2SMarcel Holtmann u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand) 307175d262c2SVinicius Costa Gomes { 3072c9839a11SVinicius Costa Gomes struct smp_ltk *key, *old_key; 307398a0b845SJohan Hedberg bool master = ltk_type_master(type); 307475d262c2SVinicius Costa Gomes 307598a0b845SJohan Hedberg old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type, master); 3076c9839a11SVinicius Costa Gomes if (old_key) 307775d262c2SVinicius Costa Gomes key = old_key; 3078c9839a11SVinicius Costa Gomes else { 30790a14ab41SJohan Hedberg key = kzalloc(sizeof(*key), GFP_KERNEL); 308075d262c2SVinicius Costa Gomes if (!key) 3081ca9142b8SJohan Hedberg return NULL; 3082c9839a11SVinicius Costa Gomes list_add(&key->list, &hdev->long_term_keys); 308375d262c2SVinicius Costa Gomes } 308475d262c2SVinicius Costa Gomes 308575d262c2SVinicius Costa Gomes bacpy(&key->bdaddr, bdaddr); 3086c9839a11SVinicius Costa Gomes key->bdaddr_type = addr_type; 3087c9839a11SVinicius Costa Gomes memcpy(key->val, tk, sizeof(key->val)); 3088c9839a11SVinicius Costa Gomes key->authenticated = authenticated; 3089c9839a11SVinicius Costa Gomes key->ediv = ediv; 3090fe39c7b2SMarcel Holtmann key->rand = rand; 3091c9839a11SVinicius Costa Gomes key->enc_size = enc_size; 3092c9839a11SVinicius Costa Gomes key->type = type; 309375d262c2SVinicius Costa Gomes 3094ca9142b8SJohan Hedberg return key; 309575d262c2SVinicius Costa Gomes } 309675d262c2SVinicius Costa Gomes 3097ca9142b8SJohan Hedberg struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, 3098ca9142b8SJohan Hedberg u8 addr_type, u8 val[16], bdaddr_t *rpa) 3099970c4e46SJohan Hedberg { 3100970c4e46SJohan Hedberg struct smp_irk *irk; 3101970c4e46SJohan Hedberg 3102970c4e46SJohan Hedberg irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type); 3103970c4e46SJohan Hedberg if (!irk) { 3104970c4e46SJohan Hedberg irk = kzalloc(sizeof(*irk), GFP_KERNEL); 3105970c4e46SJohan Hedberg if (!irk) 3106ca9142b8SJohan Hedberg return NULL; 3107970c4e46SJohan Hedberg 3108970c4e46SJohan Hedberg bacpy(&irk->bdaddr, bdaddr); 3109970c4e46SJohan Hedberg irk->addr_type = addr_type; 3110970c4e46SJohan Hedberg 3111970c4e46SJohan Hedberg list_add(&irk->list, &hdev->identity_resolving_keys); 3112970c4e46SJohan Hedberg } 3113970c4e46SJohan Hedberg 3114970c4e46SJohan Hedberg memcpy(irk->val, val, 16); 3115970c4e46SJohan Hedberg bacpy(&irk->rpa, rpa); 3116970c4e46SJohan Hedberg 3117ca9142b8SJohan Hedberg return irk; 3118970c4e46SJohan Hedberg } 3119970c4e46SJohan Hedberg 312055ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) 312155ed8ca1SJohan Hedberg { 312255ed8ca1SJohan Hedberg struct link_key *key; 312355ed8ca1SJohan Hedberg 312455ed8ca1SJohan Hedberg key = hci_find_link_key(hdev, bdaddr); 312555ed8ca1SJohan Hedberg if (!key) 312655ed8ca1SJohan Hedberg return -ENOENT; 312755ed8ca1SJohan Hedberg 31286ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 312955ed8ca1SJohan Hedberg 313055ed8ca1SJohan Hedberg list_del(&key->list); 313155ed8ca1SJohan Hedberg kfree(key); 313255ed8ca1SJohan Hedberg 313355ed8ca1SJohan Hedberg return 0; 313455ed8ca1SJohan Hedberg } 313555ed8ca1SJohan Hedberg 3136e0b2b27eSJohan Hedberg int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type) 3137b899efafSVinicius Costa Gomes { 3138b899efafSVinicius Costa Gomes struct smp_ltk *k, *tmp; 3139c51ffa0bSJohan Hedberg int removed = 0; 3140b899efafSVinicius Costa Gomes 3141b899efafSVinicius Costa Gomes list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) { 3142e0b2b27eSJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type) 3143b899efafSVinicius Costa Gomes continue; 3144b899efafSVinicius Costa Gomes 31456ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 3146b899efafSVinicius Costa Gomes 3147b899efafSVinicius Costa Gomes list_del(&k->list); 3148b899efafSVinicius Costa Gomes kfree(k); 3149c51ffa0bSJohan Hedberg removed++; 3150b899efafSVinicius Costa Gomes } 3151b899efafSVinicius Costa Gomes 3152c51ffa0bSJohan Hedberg return removed ? 0 : -ENOENT; 3153b899efafSVinicius Costa Gomes } 3154b899efafSVinicius Costa Gomes 3155a7ec7338SJohan Hedberg void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type) 3156a7ec7338SJohan Hedberg { 3157a7ec7338SJohan Hedberg struct smp_irk *k, *tmp; 3158a7ec7338SJohan Hedberg 3159668b7b19SJohan Hedberg list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) { 3160a7ec7338SJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type) 3161a7ec7338SJohan Hedberg continue; 3162a7ec7338SJohan Hedberg 3163a7ec7338SJohan Hedberg BT_DBG("%s removing %pMR", hdev->name, bdaddr); 3164a7ec7338SJohan Hedberg 3165a7ec7338SJohan Hedberg list_del(&k->list); 3166a7ec7338SJohan Hedberg kfree(k); 3167a7ec7338SJohan Hedberg } 3168a7ec7338SJohan Hedberg } 3169a7ec7338SJohan Hedberg 31706bd32326SVille Tervo /* HCI command timer function */ 317165cc2b49SMarcel Holtmann static void hci_cmd_timeout(struct work_struct *work) 31726bd32326SVille Tervo { 317365cc2b49SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, 317465cc2b49SMarcel Holtmann cmd_timer.work); 31756bd32326SVille Tervo 3176bda4f23aSAndrei Emeltchenko if (hdev->sent_cmd) { 3177bda4f23aSAndrei Emeltchenko struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; 3178bda4f23aSAndrei Emeltchenko u16 opcode = __le16_to_cpu(sent->opcode); 3179bda4f23aSAndrei Emeltchenko 3180bda4f23aSAndrei Emeltchenko BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); 3181bda4f23aSAndrei Emeltchenko } else { 31826bd32326SVille Tervo BT_ERR("%s command tx timeout", hdev->name); 3183bda4f23aSAndrei Emeltchenko } 3184bda4f23aSAndrei Emeltchenko 31856bd32326SVille Tervo atomic_set(&hdev->cmd_cnt, 1); 3186c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 31876bd32326SVille Tervo } 31886bd32326SVille Tervo 31892763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, 31902763eda6SSzymon Janc bdaddr_t *bdaddr) 31912763eda6SSzymon Janc { 31922763eda6SSzymon Janc struct oob_data *data; 31932763eda6SSzymon Janc 31942763eda6SSzymon Janc list_for_each_entry(data, &hdev->remote_oob_data, list) 31952763eda6SSzymon Janc if (bacmp(bdaddr, &data->bdaddr) == 0) 31962763eda6SSzymon Janc return data; 31972763eda6SSzymon Janc 31982763eda6SSzymon Janc return NULL; 31992763eda6SSzymon Janc } 32002763eda6SSzymon Janc 32012763eda6SSzymon Janc int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr) 32022763eda6SSzymon Janc { 32032763eda6SSzymon Janc struct oob_data *data; 32042763eda6SSzymon Janc 32052763eda6SSzymon Janc data = hci_find_remote_oob_data(hdev, bdaddr); 32062763eda6SSzymon Janc if (!data) 32072763eda6SSzymon Janc return -ENOENT; 32082763eda6SSzymon Janc 32096ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 32102763eda6SSzymon Janc 32112763eda6SSzymon Janc list_del(&data->list); 32122763eda6SSzymon Janc kfree(data); 32132763eda6SSzymon Janc 32142763eda6SSzymon Janc return 0; 32152763eda6SSzymon Janc } 32162763eda6SSzymon Janc 321735f7498aSJohan Hedberg void hci_remote_oob_data_clear(struct hci_dev *hdev) 32182763eda6SSzymon Janc { 32192763eda6SSzymon Janc struct oob_data *data, *n; 32202763eda6SSzymon Janc 32212763eda6SSzymon Janc list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { 32222763eda6SSzymon Janc list_del(&data->list); 32232763eda6SSzymon Janc kfree(data); 32242763eda6SSzymon Janc } 32252763eda6SSzymon Janc } 32262763eda6SSzymon Janc 32270798872eSMarcel Holtmann int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, 32280798872eSMarcel Holtmann u8 *hash, u8 *randomizer) 32292763eda6SSzymon Janc { 32302763eda6SSzymon Janc struct oob_data *data; 32312763eda6SSzymon Janc 32322763eda6SSzymon Janc data = hci_find_remote_oob_data(hdev, bdaddr); 32332763eda6SSzymon Janc if (!data) { 32340a14ab41SJohan Hedberg data = kmalloc(sizeof(*data), GFP_KERNEL); 32352763eda6SSzymon Janc if (!data) 32362763eda6SSzymon Janc return -ENOMEM; 32372763eda6SSzymon Janc 32382763eda6SSzymon Janc bacpy(&data->bdaddr, bdaddr); 32392763eda6SSzymon Janc list_add(&data->list, &hdev->remote_oob_data); 32402763eda6SSzymon Janc } 32412763eda6SSzymon Janc 3242519ca9d0SMarcel Holtmann memcpy(data->hash192, hash, sizeof(data->hash192)); 3243519ca9d0SMarcel Holtmann memcpy(data->randomizer192, randomizer, sizeof(data->randomizer192)); 32442763eda6SSzymon Janc 32450798872eSMarcel Holtmann memset(data->hash256, 0, sizeof(data->hash256)); 32460798872eSMarcel Holtmann memset(data->randomizer256, 0, sizeof(data->randomizer256)); 32470798872eSMarcel Holtmann 32480798872eSMarcel Holtmann BT_DBG("%s for %pMR", hdev->name, bdaddr); 32490798872eSMarcel Holtmann 32500798872eSMarcel Holtmann return 0; 32510798872eSMarcel Holtmann } 32520798872eSMarcel Holtmann 32530798872eSMarcel Holtmann int hci_add_remote_oob_ext_data(struct hci_dev *hdev, bdaddr_t *bdaddr, 32540798872eSMarcel Holtmann u8 *hash192, u8 *randomizer192, 32550798872eSMarcel Holtmann u8 *hash256, u8 *randomizer256) 32560798872eSMarcel Holtmann { 32570798872eSMarcel Holtmann struct oob_data *data; 32580798872eSMarcel Holtmann 32590798872eSMarcel Holtmann data = hci_find_remote_oob_data(hdev, bdaddr); 32600798872eSMarcel Holtmann if (!data) { 32610a14ab41SJohan Hedberg data = kmalloc(sizeof(*data), GFP_KERNEL); 32620798872eSMarcel Holtmann if (!data) 32630798872eSMarcel Holtmann return -ENOMEM; 32640798872eSMarcel Holtmann 32650798872eSMarcel Holtmann bacpy(&data->bdaddr, bdaddr); 32660798872eSMarcel Holtmann list_add(&data->list, &hdev->remote_oob_data); 32670798872eSMarcel Holtmann } 32680798872eSMarcel Holtmann 32690798872eSMarcel Holtmann memcpy(data->hash192, hash192, sizeof(data->hash192)); 32700798872eSMarcel Holtmann memcpy(data->randomizer192, randomizer192, sizeof(data->randomizer192)); 32710798872eSMarcel Holtmann 32720798872eSMarcel Holtmann memcpy(data->hash256, hash256, sizeof(data->hash256)); 32730798872eSMarcel Holtmann memcpy(data->randomizer256, randomizer256, sizeof(data->randomizer256)); 32740798872eSMarcel Holtmann 32756ed93dc6SAndrei Emeltchenko BT_DBG("%s for %pMR", hdev->name, bdaddr); 32762763eda6SSzymon Janc 32772763eda6SSzymon Janc return 0; 32782763eda6SSzymon Janc } 32792763eda6SSzymon Janc 3280b9ee0a78SMarcel Holtmann struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, 3281b9ee0a78SMarcel Holtmann bdaddr_t *bdaddr, u8 type) 3282b2a66aadSAntti Julku { 3283b2a66aadSAntti Julku struct bdaddr_list *b; 3284b2a66aadSAntti Julku 3285b9ee0a78SMarcel Holtmann list_for_each_entry(b, &hdev->blacklist, list) { 3286b9ee0a78SMarcel Holtmann if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) 3287b2a66aadSAntti Julku return b; 3288b9ee0a78SMarcel Holtmann } 3289b2a66aadSAntti Julku 3290b2a66aadSAntti Julku return NULL; 3291b2a66aadSAntti Julku } 3292b2a66aadSAntti Julku 3293c9507490SMarcel Holtmann static void hci_blacklist_clear(struct hci_dev *hdev) 3294b2a66aadSAntti Julku { 3295b2a66aadSAntti Julku struct list_head *p, *n; 3296b2a66aadSAntti Julku 3297b2a66aadSAntti Julku list_for_each_safe(p, n, &hdev->blacklist) { 3298b9ee0a78SMarcel Holtmann struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list); 3299b2a66aadSAntti Julku 3300b2a66aadSAntti Julku list_del(p); 3301b2a66aadSAntti Julku kfree(b); 3302b2a66aadSAntti Julku } 3303b2a66aadSAntti Julku } 3304b2a66aadSAntti Julku 330588c1fe4bSJohan Hedberg int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 3306b2a66aadSAntti Julku { 3307b2a66aadSAntti Julku struct bdaddr_list *entry; 3308b2a66aadSAntti Julku 3309b9ee0a78SMarcel Holtmann if (!bacmp(bdaddr, BDADDR_ANY)) 3310b2a66aadSAntti Julku return -EBADF; 3311b2a66aadSAntti Julku 3312b9ee0a78SMarcel Holtmann if (hci_blacklist_lookup(hdev, bdaddr, type)) 33135e762444SAntti Julku return -EEXIST; 3314b2a66aadSAntti Julku 3315b2a66aadSAntti Julku entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); 33165e762444SAntti Julku if (!entry) 33175e762444SAntti Julku return -ENOMEM; 3318b2a66aadSAntti Julku 3319b2a66aadSAntti Julku bacpy(&entry->bdaddr, bdaddr); 3320b9ee0a78SMarcel Holtmann entry->bdaddr_type = type; 3321b2a66aadSAntti Julku 3322b2a66aadSAntti Julku list_add(&entry->list, &hdev->blacklist); 3323b2a66aadSAntti Julku 33242a8357f2SJohan Hedberg return 0; 3325b2a66aadSAntti Julku } 3326b2a66aadSAntti Julku 332788c1fe4bSJohan Hedberg int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 3328b2a66aadSAntti Julku { 3329b2a66aadSAntti Julku struct bdaddr_list *entry; 3330b2a66aadSAntti Julku 333135f7498aSJohan Hedberg if (!bacmp(bdaddr, BDADDR_ANY)) { 333235f7498aSJohan Hedberg hci_blacklist_clear(hdev); 333335f7498aSJohan Hedberg return 0; 333435f7498aSJohan Hedberg } 3335b2a66aadSAntti Julku 3336b9ee0a78SMarcel Holtmann entry = hci_blacklist_lookup(hdev, bdaddr, type); 33371ec918ceSSzymon Janc if (!entry) 33385e762444SAntti Julku return -ENOENT; 3339b2a66aadSAntti Julku 3340b2a66aadSAntti Julku list_del(&entry->list); 3341b2a66aadSAntti Julku kfree(entry); 3342b2a66aadSAntti Julku 33432a8357f2SJohan Hedberg return 0; 3344b2a66aadSAntti Julku } 3345b2a66aadSAntti Julku 3346d2ab0ac1SMarcel Holtmann struct bdaddr_list *hci_white_list_lookup(struct hci_dev *hdev, 3347d2ab0ac1SMarcel Holtmann bdaddr_t *bdaddr, u8 type) 3348d2ab0ac1SMarcel Holtmann { 3349d2ab0ac1SMarcel Holtmann struct bdaddr_list *b; 3350d2ab0ac1SMarcel Holtmann 3351d2ab0ac1SMarcel Holtmann list_for_each_entry(b, &hdev->le_white_list, list) { 3352d2ab0ac1SMarcel Holtmann if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) 3353d2ab0ac1SMarcel Holtmann return b; 3354d2ab0ac1SMarcel Holtmann } 3355d2ab0ac1SMarcel Holtmann 3356d2ab0ac1SMarcel Holtmann return NULL; 3357d2ab0ac1SMarcel Holtmann } 3358d2ab0ac1SMarcel Holtmann 3359d2ab0ac1SMarcel Holtmann void hci_white_list_clear(struct hci_dev *hdev) 3360d2ab0ac1SMarcel Holtmann { 3361d2ab0ac1SMarcel Holtmann struct list_head *p, *n; 3362d2ab0ac1SMarcel Holtmann 3363d2ab0ac1SMarcel Holtmann list_for_each_safe(p, n, &hdev->le_white_list) { 3364d2ab0ac1SMarcel Holtmann struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list); 3365d2ab0ac1SMarcel Holtmann 3366d2ab0ac1SMarcel Holtmann list_del(p); 3367d2ab0ac1SMarcel Holtmann kfree(b); 3368d2ab0ac1SMarcel Holtmann } 3369d2ab0ac1SMarcel Holtmann } 3370d2ab0ac1SMarcel Holtmann 3371d2ab0ac1SMarcel Holtmann int hci_white_list_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 3372d2ab0ac1SMarcel Holtmann { 3373d2ab0ac1SMarcel Holtmann struct bdaddr_list *entry; 3374d2ab0ac1SMarcel Holtmann 3375d2ab0ac1SMarcel Holtmann if (!bacmp(bdaddr, BDADDR_ANY)) 3376d2ab0ac1SMarcel Holtmann return -EBADF; 3377d2ab0ac1SMarcel Holtmann 3378d2ab0ac1SMarcel Holtmann entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); 3379d2ab0ac1SMarcel Holtmann if (!entry) 3380d2ab0ac1SMarcel Holtmann return -ENOMEM; 3381d2ab0ac1SMarcel Holtmann 3382d2ab0ac1SMarcel Holtmann bacpy(&entry->bdaddr, bdaddr); 3383d2ab0ac1SMarcel Holtmann entry->bdaddr_type = type; 3384d2ab0ac1SMarcel Holtmann 3385d2ab0ac1SMarcel Holtmann list_add(&entry->list, &hdev->le_white_list); 3386d2ab0ac1SMarcel Holtmann 3387d2ab0ac1SMarcel Holtmann return 0; 3388d2ab0ac1SMarcel Holtmann } 3389d2ab0ac1SMarcel Holtmann 3390d2ab0ac1SMarcel Holtmann int hci_white_list_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 3391d2ab0ac1SMarcel Holtmann { 3392d2ab0ac1SMarcel Holtmann struct bdaddr_list *entry; 3393d2ab0ac1SMarcel Holtmann 3394d2ab0ac1SMarcel Holtmann if (!bacmp(bdaddr, BDADDR_ANY)) 3395d2ab0ac1SMarcel Holtmann return -EBADF; 3396d2ab0ac1SMarcel Holtmann 3397d2ab0ac1SMarcel Holtmann entry = hci_white_list_lookup(hdev, bdaddr, type); 3398d2ab0ac1SMarcel Holtmann if (!entry) 3399d2ab0ac1SMarcel Holtmann return -ENOENT; 3400d2ab0ac1SMarcel Holtmann 3401d2ab0ac1SMarcel Holtmann list_del(&entry->list); 3402d2ab0ac1SMarcel Holtmann kfree(entry); 3403d2ab0ac1SMarcel Holtmann 3404d2ab0ac1SMarcel Holtmann return 0; 3405d2ab0ac1SMarcel Holtmann } 3406d2ab0ac1SMarcel Holtmann 340715819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 340815819a70SAndre Guedes struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, 340915819a70SAndre Guedes bdaddr_t *addr, u8 addr_type) 341015819a70SAndre Guedes { 341115819a70SAndre Guedes struct hci_conn_params *params; 341215819a70SAndre Guedes 3413738f6185SJohan Hedberg /* The conn params list only contains identity addresses */ 3414738f6185SJohan Hedberg if (!hci_is_identity_address(addr, addr_type)) 3415738f6185SJohan Hedberg return NULL; 3416738f6185SJohan Hedberg 341715819a70SAndre Guedes list_for_each_entry(params, &hdev->le_conn_params, list) { 341815819a70SAndre Guedes if (bacmp(¶ms->addr, addr) == 0 && 341915819a70SAndre Guedes params->addr_type == addr_type) { 342015819a70SAndre Guedes return params; 342115819a70SAndre Guedes } 342215819a70SAndre Guedes } 342315819a70SAndre Guedes 342415819a70SAndre Guedes return NULL; 342515819a70SAndre Guedes } 342615819a70SAndre Guedes 3427cef952ceSAndre Guedes static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) 3428cef952ceSAndre Guedes { 3429cef952ceSAndre Guedes struct hci_conn *conn; 3430cef952ceSAndre Guedes 3431cef952ceSAndre Guedes conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); 3432cef952ceSAndre Guedes if (!conn) 3433cef952ceSAndre Guedes return false; 3434cef952ceSAndre Guedes 3435cef952ceSAndre Guedes if (conn->dst_type != type) 3436cef952ceSAndre Guedes return false; 3437cef952ceSAndre Guedes 3438cef952ceSAndre Guedes if (conn->state != BT_CONNECTED) 3439cef952ceSAndre Guedes return false; 3440cef952ceSAndre Guedes 3441cef952ceSAndre Guedes return true; 3442cef952ceSAndre Guedes } 3443cef952ceSAndre Guedes 344415819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 3445912b42efSJohan Hedberg struct hci_conn_params *hci_pend_le_conn_lookup(struct hci_dev *hdev, 34464b10966fSMarcel Holtmann bdaddr_t *addr, u8 addr_type) 34474b10966fSMarcel Holtmann { 3448912b42efSJohan Hedberg struct hci_conn_params *param; 34494b10966fSMarcel Holtmann 3450738f6185SJohan Hedberg /* The list only contains identity addresses */ 3451738f6185SJohan Hedberg if (!hci_is_identity_address(addr, addr_type)) 3452738f6185SJohan Hedberg return NULL; 3453738f6185SJohan Hedberg 345493450c75SJohan Hedberg list_for_each_entry(param, &hdev->pend_le_conns, action) { 3455912b42efSJohan Hedberg if (bacmp(¶m->addr, addr) == 0 && 3456912b42efSJohan Hedberg param->addr_type == addr_type) 3457912b42efSJohan Hedberg return param; 34584b10966fSMarcel Holtmann } 34594b10966fSMarcel Holtmann 34604b10966fSMarcel Holtmann return NULL; 34614b10966fSMarcel Holtmann } 34624b10966fSMarcel Holtmann 34634b10966fSMarcel Holtmann /* This function requires the caller holds hdev->lock */ 3464912b42efSJohan Hedberg void hci_pend_le_conn_add(struct hci_dev *hdev, struct hci_conn_params *params) 34654b10966fSMarcel Holtmann { 346693450c75SJohan Hedberg list_del_init(¶ms->action); 346793450c75SJohan Hedberg list_add(¶ms->action, &hdev->pend_le_conns); 34684b10966fSMarcel Holtmann 3469912b42efSJohan Hedberg BT_DBG("addr %pMR (type %u)", ¶ms->addr, params->addr_type); 34704b10966fSMarcel Holtmann 34714b10966fSMarcel Holtmann hci_update_background_scan(hdev); 34724b10966fSMarcel Holtmann } 34734b10966fSMarcel Holtmann 347451d167c0SMarcel Holtmann struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev, 347551d167c0SMarcel Holtmann bdaddr_t *addr, u8 addr_type) 3476bf5b3c8bSMarcel Holtmann { 3477bf5b3c8bSMarcel Holtmann struct hci_conn_params *params; 3478bf5b3c8bSMarcel Holtmann 3479c46245b3SJohan Hedberg if (!hci_is_identity_address(addr, addr_type)) 348051d167c0SMarcel Holtmann return NULL; 3481bf5b3c8bSMarcel Holtmann 3482bf5b3c8bSMarcel Holtmann params = hci_conn_params_lookup(hdev, addr, addr_type); 3483bf5b3c8bSMarcel Holtmann if (params) 348451d167c0SMarcel Holtmann return params; 3485bf5b3c8bSMarcel Holtmann 3486bf5b3c8bSMarcel Holtmann params = kzalloc(sizeof(*params), GFP_KERNEL); 3487bf5b3c8bSMarcel Holtmann if (!params) { 3488bf5b3c8bSMarcel Holtmann BT_ERR("Out of memory"); 348951d167c0SMarcel Holtmann return NULL; 3490bf5b3c8bSMarcel Holtmann } 3491bf5b3c8bSMarcel Holtmann 3492bf5b3c8bSMarcel Holtmann bacpy(¶ms->addr, addr); 3493bf5b3c8bSMarcel Holtmann params->addr_type = addr_type; 3494bf5b3c8bSMarcel Holtmann 3495bf5b3c8bSMarcel Holtmann list_add(¶ms->list, &hdev->le_conn_params); 349693450c75SJohan Hedberg INIT_LIST_HEAD(¶ms->action); 3497bf5b3c8bSMarcel Holtmann 3498bf5b3c8bSMarcel Holtmann params->conn_min_interval = hdev->le_conn_min_interval; 3499bf5b3c8bSMarcel Holtmann params->conn_max_interval = hdev->le_conn_max_interval; 3500bf5b3c8bSMarcel Holtmann params->conn_latency = hdev->le_conn_latency; 3501bf5b3c8bSMarcel Holtmann params->supervision_timeout = hdev->le_supv_timeout; 3502bf5b3c8bSMarcel Holtmann params->auto_connect = HCI_AUTO_CONN_DISABLED; 3503bf5b3c8bSMarcel Holtmann 3504bf5b3c8bSMarcel Holtmann BT_DBG("addr %pMR (type %u)", addr, addr_type); 3505bf5b3c8bSMarcel Holtmann 350651d167c0SMarcel Holtmann return params; 3507bf5b3c8bSMarcel Holtmann } 3508bf5b3c8bSMarcel Holtmann 3509bf5b3c8bSMarcel Holtmann /* This function requires the caller holds hdev->lock */ 3510bf5b3c8bSMarcel Holtmann int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type, 3511d06b50ceSMarcel Holtmann u8 auto_connect) 351215819a70SAndre Guedes { 351315819a70SAndre Guedes struct hci_conn_params *params; 351415819a70SAndre Guedes 35158c87aae1SMarcel Holtmann params = hci_conn_params_add(hdev, addr, addr_type); 35168c87aae1SMarcel Holtmann if (!params) 35178c87aae1SMarcel Holtmann return -EIO; 3518a9b0a04cSAndre Guedes 351942ce26deSJohan Hedberg if (params->auto_connect == auto_connect) 352042ce26deSJohan Hedberg return 0; 352142ce26deSJohan Hedberg 352266f8455aSJohan Hedberg list_del_init(¶ms->action); 352315819a70SAndre Guedes 3524cef952ceSAndre Guedes switch (auto_connect) { 3525cef952ceSAndre Guedes case HCI_AUTO_CONN_DISABLED: 3526cef952ceSAndre Guedes case HCI_AUTO_CONN_LINK_LOSS: 352795305baaSJohan Hedberg hci_update_background_scan(hdev); 3528cef952ceSAndre Guedes break; 3529851efca8SJohan Hedberg case HCI_AUTO_CONN_REPORT: 353095305baaSJohan Hedberg list_add(¶ms->action, &hdev->pend_le_reports); 353195305baaSJohan Hedberg hci_update_background_scan(hdev); 3532851efca8SJohan Hedberg break; 3533cef952ceSAndre Guedes case HCI_AUTO_CONN_ALWAYS: 353495305baaSJohan Hedberg if (!is_connected(hdev, addr, addr_type)) { 353595305baaSJohan Hedberg list_add(¶ms->action, &hdev->pend_le_conns); 353695305baaSJohan Hedberg hci_update_background_scan(hdev); 353795305baaSJohan Hedberg } 3538cef952ceSAndre Guedes break; 3539cef952ceSAndre Guedes } 354015819a70SAndre Guedes 3541851efca8SJohan Hedberg params->auto_connect = auto_connect; 3542851efca8SJohan Hedberg 3543d06b50ceSMarcel Holtmann BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, 3544d06b50ceSMarcel Holtmann auto_connect); 3545a9b0a04cSAndre Guedes 3546a9b0a04cSAndre Guedes return 0; 354715819a70SAndre Guedes } 354815819a70SAndre Guedes 354915819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 355015819a70SAndre Guedes void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type) 355115819a70SAndre Guedes { 355215819a70SAndre Guedes struct hci_conn_params *params; 355315819a70SAndre Guedes 355415819a70SAndre Guedes params = hci_conn_params_lookup(hdev, addr, addr_type); 355515819a70SAndre Guedes if (!params) 355615819a70SAndre Guedes return; 355715819a70SAndre Guedes 355895305baaSJohan Hedberg list_del(¶ms->action); 355915819a70SAndre Guedes list_del(¶ms->list); 356015819a70SAndre Guedes kfree(params); 356115819a70SAndre Guedes 356295305baaSJohan Hedberg hci_update_background_scan(hdev); 356395305baaSJohan Hedberg 356415819a70SAndre Guedes BT_DBG("addr %pMR (type %u)", addr, addr_type); 356515819a70SAndre Guedes } 356615819a70SAndre Guedes 356715819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 356855af49a8SJohan Hedberg void hci_conn_params_clear_disabled(struct hci_dev *hdev) 356955af49a8SJohan Hedberg { 357055af49a8SJohan Hedberg struct hci_conn_params *params, *tmp; 357155af49a8SJohan Hedberg 357255af49a8SJohan Hedberg list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { 357355af49a8SJohan Hedberg if (params->auto_connect != HCI_AUTO_CONN_DISABLED) 357455af49a8SJohan Hedberg continue; 357555af49a8SJohan Hedberg list_del(¶ms->list); 357655af49a8SJohan Hedberg kfree(params); 357755af49a8SJohan Hedberg } 357855af49a8SJohan Hedberg 357955af49a8SJohan Hedberg BT_DBG("All LE disabled connection parameters were removed"); 358055af49a8SJohan Hedberg } 358155af49a8SJohan Hedberg 358255af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */ 358355af49a8SJohan Hedberg void hci_conn_params_clear_enabled(struct hci_dev *hdev) 358455af49a8SJohan Hedberg { 358555af49a8SJohan Hedberg struct hci_conn_params *params, *tmp; 358655af49a8SJohan Hedberg 358755af49a8SJohan Hedberg list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { 358855af49a8SJohan Hedberg if (params->auto_connect == HCI_AUTO_CONN_DISABLED) 358955af49a8SJohan Hedberg continue; 3590a2f41a8fSJohan Hedberg list_del(¶ms->action); 359155af49a8SJohan Hedberg list_del(¶ms->list); 359255af49a8SJohan Hedberg kfree(params); 359355af49a8SJohan Hedberg } 359455af49a8SJohan Hedberg 3595a2f41a8fSJohan Hedberg hci_update_background_scan(hdev); 359655af49a8SJohan Hedberg 359755af49a8SJohan Hedberg BT_DBG("All enabled LE connection parameters were removed"); 359855af49a8SJohan Hedberg } 359955af49a8SJohan Hedberg 360055af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */ 3601373110c5SJohan Hedberg void hci_conn_params_clear_all(struct hci_dev *hdev) 360215819a70SAndre Guedes { 360315819a70SAndre Guedes struct hci_conn_params *params, *tmp; 360415819a70SAndre Guedes 360515819a70SAndre Guedes list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { 3606a2f41a8fSJohan Hedberg list_del(¶ms->action); 360715819a70SAndre Guedes list_del(¶ms->list); 360815819a70SAndre Guedes kfree(params); 360915819a70SAndre Guedes } 361015819a70SAndre Guedes 3611a2f41a8fSJohan Hedberg hci_update_background_scan(hdev); 36121089b67dSMarcel Holtmann 361315819a70SAndre Guedes BT_DBG("All LE connection parameters were removed"); 361415819a70SAndre Guedes } 361515819a70SAndre Guedes 36164c87eaabSAndre Guedes static void inquiry_complete(struct hci_dev *hdev, u8 status) 36177ba8b4beSAndre Guedes { 36184c87eaabSAndre Guedes if (status) { 36194c87eaabSAndre Guedes BT_ERR("Failed to start inquiry: status %d", status); 36207ba8b4beSAndre Guedes 36214c87eaabSAndre Guedes hci_dev_lock(hdev); 36224c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 36234c87eaabSAndre Guedes hci_dev_unlock(hdev); 36244c87eaabSAndre Guedes return; 36254c87eaabSAndre Guedes } 36267ba8b4beSAndre Guedes } 36277ba8b4beSAndre Guedes 36284c87eaabSAndre Guedes static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status) 36297ba8b4beSAndre Guedes { 36304c87eaabSAndre Guedes /* General inquiry access code (GIAC) */ 36314c87eaabSAndre Guedes u8 lap[3] = { 0x33, 0x8b, 0x9e }; 36324c87eaabSAndre Guedes struct hci_request req; 36334c87eaabSAndre Guedes struct hci_cp_inquiry cp; 36347ba8b4beSAndre Guedes int err; 36357ba8b4beSAndre Guedes 36364c87eaabSAndre Guedes if (status) { 36374c87eaabSAndre Guedes BT_ERR("Failed to disable LE scanning: status %d", status); 36384c87eaabSAndre Guedes return; 36397ba8b4beSAndre Guedes } 36407ba8b4beSAndre Guedes 36414c87eaabSAndre Guedes switch (hdev->discovery.type) { 36424c87eaabSAndre Guedes case DISCOV_TYPE_LE: 36434c87eaabSAndre Guedes hci_dev_lock(hdev); 36444c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 36454c87eaabSAndre Guedes hci_dev_unlock(hdev); 36464c87eaabSAndre Guedes break; 36477dbfac1dSAndre Guedes 36484c87eaabSAndre Guedes case DISCOV_TYPE_INTERLEAVED: 36494c87eaabSAndre Guedes hci_req_init(&req, hdev); 36507dbfac1dSAndre Guedes 36517dbfac1dSAndre Guedes memset(&cp, 0, sizeof(cp)); 36524c87eaabSAndre Guedes memcpy(&cp.lap, lap, sizeof(cp.lap)); 36534c87eaabSAndre Guedes cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; 36544c87eaabSAndre Guedes hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); 36554c87eaabSAndre Guedes 36564c87eaabSAndre Guedes hci_dev_lock(hdev); 36574c87eaabSAndre Guedes 36584c87eaabSAndre Guedes hci_inquiry_cache_flush(hdev); 36594c87eaabSAndre Guedes 36604c87eaabSAndre Guedes err = hci_req_run(&req, inquiry_complete); 36614c87eaabSAndre Guedes if (err) { 36624c87eaabSAndre Guedes BT_ERR("Inquiry request failed: err %d", err); 36634c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 36647dbfac1dSAndre Guedes } 36657dbfac1dSAndre Guedes 36664c87eaabSAndre Guedes hci_dev_unlock(hdev); 36674c87eaabSAndre Guedes break; 36684c87eaabSAndre Guedes } 36697dbfac1dSAndre Guedes } 36707dbfac1dSAndre Guedes 36717ba8b4beSAndre Guedes static void le_scan_disable_work(struct work_struct *work) 36727ba8b4beSAndre Guedes { 36737ba8b4beSAndre Guedes struct hci_dev *hdev = container_of(work, struct hci_dev, 36747ba8b4beSAndre Guedes le_scan_disable.work); 36754c87eaabSAndre Guedes struct hci_request req; 36764c87eaabSAndre Guedes int err; 36777ba8b4beSAndre Guedes 36787ba8b4beSAndre Guedes BT_DBG("%s", hdev->name); 36797ba8b4beSAndre Guedes 36804c87eaabSAndre Guedes hci_req_init(&req, hdev); 36817ba8b4beSAndre Guedes 3682b1efcc28SAndre Guedes hci_req_add_le_scan_disable(&req); 36837ba8b4beSAndre Guedes 36844c87eaabSAndre Guedes err = hci_req_run(&req, le_scan_disable_work_complete); 36854c87eaabSAndre Guedes if (err) 36864c87eaabSAndre Guedes BT_ERR("Disable LE scanning request failed: err %d", err); 368728b75a89SAndre Guedes } 368828b75a89SAndre Guedes 36898d97250eSJohan Hedberg static void set_random_addr(struct hci_request *req, bdaddr_t *rpa) 36908d97250eSJohan Hedberg { 36918d97250eSJohan Hedberg struct hci_dev *hdev = req->hdev; 36928d97250eSJohan Hedberg 36938d97250eSJohan Hedberg /* If we're advertising or initiating an LE connection we can't 36948d97250eSJohan Hedberg * go ahead and change the random address at this time. This is 36958d97250eSJohan Hedberg * because the eventual initiator address used for the 36968d97250eSJohan Hedberg * subsequently created connection will be undefined (some 36978d97250eSJohan Hedberg * controllers use the new address and others the one we had 36988d97250eSJohan Hedberg * when the operation started). 36998d97250eSJohan Hedberg * 37008d97250eSJohan Hedberg * In this kind of scenario skip the update and let the random 37018d97250eSJohan Hedberg * address be updated at the next cycle. 37028d97250eSJohan Hedberg */ 37038d97250eSJohan Hedberg if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) || 37048d97250eSJohan Hedberg hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) { 37058d97250eSJohan Hedberg BT_DBG("Deferring random address update"); 37068d97250eSJohan Hedberg return; 37078d97250eSJohan Hedberg } 37088d97250eSJohan Hedberg 37098d97250eSJohan Hedberg hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa); 37108d97250eSJohan Hedberg } 37118d97250eSJohan Hedberg 371294b1fc92SMarcel Holtmann int hci_update_random_address(struct hci_request *req, bool require_privacy, 371394b1fc92SMarcel Holtmann u8 *own_addr_type) 3714ebd3a747SJohan Hedberg { 3715ebd3a747SJohan Hedberg struct hci_dev *hdev = req->hdev; 3716ebd3a747SJohan Hedberg int err; 3717ebd3a747SJohan Hedberg 3718ebd3a747SJohan Hedberg /* If privacy is enabled use a resolvable private address. If 37192b5224dcSMarcel Holtmann * current RPA has expired or there is something else than 37202b5224dcSMarcel Holtmann * the current RPA in use, then generate a new one. 3721ebd3a747SJohan Hedberg */ 3722ebd3a747SJohan Hedberg if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) { 3723ebd3a747SJohan Hedberg int to; 3724ebd3a747SJohan Hedberg 3725ebd3a747SJohan Hedberg *own_addr_type = ADDR_LE_DEV_RANDOM; 3726ebd3a747SJohan Hedberg 3727ebd3a747SJohan Hedberg if (!test_and_clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags) && 37282b5224dcSMarcel Holtmann !bacmp(&hdev->random_addr, &hdev->rpa)) 3729ebd3a747SJohan Hedberg return 0; 3730ebd3a747SJohan Hedberg 37312b5224dcSMarcel Holtmann err = smp_generate_rpa(hdev->tfm_aes, hdev->irk, &hdev->rpa); 3732ebd3a747SJohan Hedberg if (err < 0) { 3733ebd3a747SJohan Hedberg BT_ERR("%s failed to generate new RPA", hdev->name); 3734ebd3a747SJohan Hedberg return err; 3735ebd3a747SJohan Hedberg } 3736ebd3a747SJohan Hedberg 37378d97250eSJohan Hedberg set_random_addr(req, &hdev->rpa); 3738ebd3a747SJohan Hedberg 3739ebd3a747SJohan Hedberg to = msecs_to_jiffies(hdev->rpa_timeout * 1000); 3740ebd3a747SJohan Hedberg queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to); 3741ebd3a747SJohan Hedberg 3742ebd3a747SJohan Hedberg return 0; 3743ebd3a747SJohan Hedberg } 3744ebd3a747SJohan Hedberg 374594b1fc92SMarcel Holtmann /* In case of required privacy without resolvable private address, 374694b1fc92SMarcel Holtmann * use an unresolvable private address. This is useful for active 374794b1fc92SMarcel Holtmann * scanning and non-connectable advertising. 374894b1fc92SMarcel Holtmann */ 374994b1fc92SMarcel Holtmann if (require_privacy) { 375094b1fc92SMarcel Holtmann bdaddr_t urpa; 375194b1fc92SMarcel Holtmann 375294b1fc92SMarcel Holtmann get_random_bytes(&urpa, 6); 375394b1fc92SMarcel Holtmann urpa.b[5] &= 0x3f; /* Clear two most significant bits */ 375494b1fc92SMarcel Holtmann 375594b1fc92SMarcel Holtmann *own_addr_type = ADDR_LE_DEV_RANDOM; 37568d97250eSJohan Hedberg set_random_addr(req, &urpa); 375794b1fc92SMarcel Holtmann return 0; 375894b1fc92SMarcel Holtmann } 375994b1fc92SMarcel Holtmann 3760ebd3a747SJohan Hedberg /* If forcing static address is in use or there is no public 3761ebd3a747SJohan Hedberg * address use the static address as random address (but skip 3762ebd3a747SJohan Hedberg * the HCI command if the current random address is already the 3763ebd3a747SJohan Hedberg * static one. 3764ebd3a747SJohan Hedberg */ 3765111902f7SMarcel Holtmann if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) || 3766ebd3a747SJohan Hedberg !bacmp(&hdev->bdaddr, BDADDR_ANY)) { 3767ebd3a747SJohan Hedberg *own_addr_type = ADDR_LE_DEV_RANDOM; 3768ebd3a747SJohan Hedberg if (bacmp(&hdev->static_addr, &hdev->random_addr)) 3769ebd3a747SJohan Hedberg hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, 3770ebd3a747SJohan Hedberg &hdev->static_addr); 3771ebd3a747SJohan Hedberg return 0; 3772ebd3a747SJohan Hedberg } 3773ebd3a747SJohan Hedberg 3774ebd3a747SJohan Hedberg /* Neither privacy nor static address is being used so use a 3775ebd3a747SJohan Hedberg * public address. 3776ebd3a747SJohan Hedberg */ 3777ebd3a747SJohan Hedberg *own_addr_type = ADDR_LE_DEV_PUBLIC; 3778ebd3a747SJohan Hedberg 3779ebd3a747SJohan Hedberg return 0; 3780ebd3a747SJohan Hedberg } 3781ebd3a747SJohan Hedberg 3782a1f4c318SJohan Hedberg /* Copy the Identity Address of the controller. 3783a1f4c318SJohan Hedberg * 3784a1f4c318SJohan Hedberg * If the controller has a public BD_ADDR, then by default use that one. 3785a1f4c318SJohan Hedberg * If this is a LE only controller without a public address, default to 3786a1f4c318SJohan Hedberg * the static random address. 3787a1f4c318SJohan Hedberg * 3788a1f4c318SJohan Hedberg * For debugging purposes it is possible to force controllers with a 3789a1f4c318SJohan Hedberg * public address to use the static random address instead. 3790a1f4c318SJohan Hedberg */ 3791a1f4c318SJohan Hedberg void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, 3792a1f4c318SJohan Hedberg u8 *bdaddr_type) 3793a1f4c318SJohan Hedberg { 3794111902f7SMarcel Holtmann if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) || 3795a1f4c318SJohan Hedberg !bacmp(&hdev->bdaddr, BDADDR_ANY)) { 3796a1f4c318SJohan Hedberg bacpy(bdaddr, &hdev->static_addr); 3797a1f4c318SJohan Hedberg *bdaddr_type = ADDR_LE_DEV_RANDOM; 3798a1f4c318SJohan Hedberg } else { 3799a1f4c318SJohan Hedberg bacpy(bdaddr, &hdev->bdaddr); 3800a1f4c318SJohan Hedberg *bdaddr_type = ADDR_LE_DEV_PUBLIC; 3801a1f4c318SJohan Hedberg } 3802a1f4c318SJohan Hedberg } 3803a1f4c318SJohan Hedberg 38049be0dab7SDavid Herrmann /* Alloc HCI device */ 38059be0dab7SDavid Herrmann struct hci_dev *hci_alloc_dev(void) 38069be0dab7SDavid Herrmann { 38079be0dab7SDavid Herrmann struct hci_dev *hdev; 38089be0dab7SDavid Herrmann 38099be0dab7SDavid Herrmann hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); 38109be0dab7SDavid Herrmann if (!hdev) 38119be0dab7SDavid Herrmann return NULL; 38129be0dab7SDavid Herrmann 3813b1b813d4SDavid Herrmann hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); 3814b1b813d4SDavid Herrmann hdev->esco_type = (ESCO_HV1); 3815b1b813d4SDavid Herrmann hdev->link_mode = (HCI_LM_ACCEPT); 3816b4cb9fb2SMarcel Holtmann hdev->num_iac = 0x01; /* One IAC support is mandatory */ 3817b1b813d4SDavid Herrmann hdev->io_capability = 0x03; /* No Input No Output */ 381896c2103aSMarcel Holtmann hdev->manufacturer = 0xffff; /* Default to internal use */ 3819bbaf444aSJohan Hedberg hdev->inq_tx_power = HCI_TX_POWER_INVALID; 3820bbaf444aSJohan Hedberg hdev->adv_tx_power = HCI_TX_POWER_INVALID; 3821b1b813d4SDavid Herrmann 3822b1b813d4SDavid Herrmann hdev->sniff_max_interval = 800; 3823b1b813d4SDavid Herrmann hdev->sniff_min_interval = 80; 3824b1b813d4SDavid Herrmann 38253f959d46SMarcel Holtmann hdev->le_adv_channel_map = 0x07; 3826bef64738SMarcel Holtmann hdev->le_scan_interval = 0x0060; 3827bef64738SMarcel Holtmann hdev->le_scan_window = 0x0030; 38284e70c7e7SMarcel Holtmann hdev->le_conn_min_interval = 0x0028; 38294e70c7e7SMarcel Holtmann hdev->le_conn_max_interval = 0x0038; 383004fb7d90SMarcel Holtmann hdev->le_conn_latency = 0x0000; 383104fb7d90SMarcel Holtmann hdev->le_supv_timeout = 0x002a; 3832bef64738SMarcel Holtmann 3833d6bfd59cSJohan Hedberg hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT; 3834b9a7a61eSLukasz Rymanowski hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT; 383531ad1691SAndrzej Kaczmarek hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE; 383631ad1691SAndrzej Kaczmarek hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE; 3837d6bfd59cSJohan Hedberg 3838b1b813d4SDavid Herrmann mutex_init(&hdev->lock); 3839b1b813d4SDavid Herrmann mutex_init(&hdev->req_lock); 3840b1b813d4SDavid Herrmann 3841b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->mgmt_pending); 3842b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->blacklist); 3843b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->uuids); 3844b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->link_keys); 3845b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->long_term_keys); 3846970c4e46SJohan Hedberg INIT_LIST_HEAD(&hdev->identity_resolving_keys); 3847b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->remote_oob_data); 3848d2ab0ac1SMarcel Holtmann INIT_LIST_HEAD(&hdev->le_white_list); 384915819a70SAndre Guedes INIT_LIST_HEAD(&hdev->le_conn_params); 385077a77a30SAndre Guedes INIT_LIST_HEAD(&hdev->pend_le_conns); 385166f8455aSJohan Hedberg INIT_LIST_HEAD(&hdev->pend_le_reports); 38526b536b5eSAndrei Emeltchenko INIT_LIST_HEAD(&hdev->conn_hash.list); 3853b1b813d4SDavid Herrmann 3854b1b813d4SDavid Herrmann INIT_WORK(&hdev->rx_work, hci_rx_work); 3855b1b813d4SDavid Herrmann INIT_WORK(&hdev->cmd_work, hci_cmd_work); 3856b1b813d4SDavid Herrmann INIT_WORK(&hdev->tx_work, hci_tx_work); 3857b1b813d4SDavid Herrmann INIT_WORK(&hdev->power_on, hci_power_on); 3858b1b813d4SDavid Herrmann 3859b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); 3860b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); 3861b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); 3862b1b813d4SDavid Herrmann 3863b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->rx_q); 3864b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->cmd_q); 3865b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->raw_q); 3866b1b813d4SDavid Herrmann 3867b1b813d4SDavid Herrmann init_waitqueue_head(&hdev->req_wait_q); 3868b1b813d4SDavid Herrmann 386965cc2b49SMarcel Holtmann INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout); 3870b1b813d4SDavid Herrmann 3871b1b813d4SDavid Herrmann hci_init_sysfs(hdev); 3872b1b813d4SDavid Herrmann discovery_init(hdev); 38739be0dab7SDavid Herrmann 38749be0dab7SDavid Herrmann return hdev; 38759be0dab7SDavid Herrmann } 38769be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_alloc_dev); 38779be0dab7SDavid Herrmann 38789be0dab7SDavid Herrmann /* Free HCI device */ 38799be0dab7SDavid Herrmann void hci_free_dev(struct hci_dev *hdev) 38809be0dab7SDavid Herrmann { 38819be0dab7SDavid Herrmann /* will free via device release */ 38829be0dab7SDavid Herrmann put_device(&hdev->dev); 38839be0dab7SDavid Herrmann } 38849be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_free_dev); 38859be0dab7SDavid Herrmann 38861da177e4SLinus Torvalds /* Register HCI device */ 38871da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev) 38881da177e4SLinus Torvalds { 3889b1b813d4SDavid Herrmann int id, error; 38901da177e4SLinus Torvalds 3891010666a1SDavid Herrmann if (!hdev->open || !hdev->close) 38921da177e4SLinus Torvalds return -EINVAL; 38931da177e4SLinus Torvalds 389408add513SMat Martineau /* Do not allow HCI_AMP devices to register at index 0, 389508add513SMat Martineau * so the index can be used as the AMP controller ID. 389608add513SMat Martineau */ 38973df92b31SSasha Levin switch (hdev->dev_type) { 38983df92b31SSasha Levin case HCI_BREDR: 38993df92b31SSasha Levin id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); 39001da177e4SLinus Torvalds break; 39013df92b31SSasha Levin case HCI_AMP: 39023df92b31SSasha Levin id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); 39033df92b31SSasha Levin break; 39043df92b31SSasha Levin default: 39053df92b31SSasha Levin return -EINVAL; 39061da177e4SLinus Torvalds } 39071da177e4SLinus Torvalds 39083df92b31SSasha Levin if (id < 0) 39093df92b31SSasha Levin return id; 39103df92b31SSasha Levin 39111da177e4SLinus Torvalds sprintf(hdev->name, "hci%d", id); 39121da177e4SLinus Torvalds hdev->id = id; 39132d8b3a11SAndrei Emeltchenko 39142d8b3a11SAndrei Emeltchenko BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 39152d8b3a11SAndrei Emeltchenko 3916d8537548SKees Cook hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | 3917d8537548SKees Cook WQ_MEM_RECLAIM, 1, hdev->name); 391833ca954dSDavid Herrmann if (!hdev->workqueue) { 391933ca954dSDavid Herrmann error = -ENOMEM; 392033ca954dSDavid Herrmann goto err; 392133ca954dSDavid Herrmann } 3922f48fd9c8SMarcel Holtmann 3923d8537548SKees Cook hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | 3924d8537548SKees Cook WQ_MEM_RECLAIM, 1, hdev->name); 39256ead1bbcSJohan Hedberg if (!hdev->req_workqueue) { 39266ead1bbcSJohan Hedberg destroy_workqueue(hdev->workqueue); 39276ead1bbcSJohan Hedberg error = -ENOMEM; 39286ead1bbcSJohan Hedberg goto err; 39296ead1bbcSJohan Hedberg } 39306ead1bbcSJohan Hedberg 39310153e2ecSMarcel Holtmann if (!IS_ERR_OR_NULL(bt_debugfs)) 39320153e2ecSMarcel Holtmann hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs); 39330153e2ecSMarcel Holtmann 3934bdc3e0f1SMarcel Holtmann dev_set_name(&hdev->dev, "%s", hdev->name); 3935bdc3e0f1SMarcel Holtmann 393699780a7bSJohan Hedberg hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 393799780a7bSJohan Hedberg CRYPTO_ALG_ASYNC); 393899780a7bSJohan Hedberg if (IS_ERR(hdev->tfm_aes)) { 393999780a7bSJohan Hedberg BT_ERR("Unable to create crypto context"); 394099780a7bSJohan Hedberg error = PTR_ERR(hdev->tfm_aes); 394199780a7bSJohan Hedberg hdev->tfm_aes = NULL; 394299780a7bSJohan Hedberg goto err_wqueue; 394399780a7bSJohan Hedberg } 394499780a7bSJohan Hedberg 3945bdc3e0f1SMarcel Holtmann error = device_add(&hdev->dev); 394633ca954dSDavid Herrmann if (error < 0) 394799780a7bSJohan Hedberg goto err_tfm; 39481da177e4SLinus Torvalds 3949611b30f7SMarcel Holtmann hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, 3950a8c5fb1aSGustavo Padovan RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, 3951a8c5fb1aSGustavo Padovan hdev); 3952611b30f7SMarcel Holtmann if (hdev->rfkill) { 3953611b30f7SMarcel Holtmann if (rfkill_register(hdev->rfkill) < 0) { 3954611b30f7SMarcel Holtmann rfkill_destroy(hdev->rfkill); 3955611b30f7SMarcel Holtmann hdev->rfkill = NULL; 3956611b30f7SMarcel Holtmann } 3957611b30f7SMarcel Holtmann } 3958611b30f7SMarcel Holtmann 39595e130367SJohan Hedberg if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) 39605e130367SJohan Hedberg set_bit(HCI_RFKILLED, &hdev->dev_flags); 39615e130367SJohan Hedberg 3962a8b2d5c2SJohan Hedberg set_bit(HCI_SETUP, &hdev->dev_flags); 3963004b0258SMarcel Holtmann set_bit(HCI_AUTO_OFF, &hdev->dev_flags); 3964ce2be9acSAndrei Emeltchenko 396501cd3404SMarcel Holtmann if (hdev->dev_type == HCI_BREDR) { 396656f87901SJohan Hedberg /* Assume BR/EDR support until proven otherwise (such as 396756f87901SJohan Hedberg * through reading supported features during init. 396856f87901SJohan Hedberg */ 396956f87901SJohan Hedberg set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 397056f87901SJohan Hedberg } 3971ce2be9acSAndrei Emeltchenko 3972fcee3377SGustavo Padovan write_lock(&hci_dev_list_lock); 3973fcee3377SGustavo Padovan list_add(&hdev->list, &hci_dev_list); 3974fcee3377SGustavo Padovan write_unlock(&hci_dev_list_lock); 3975fcee3377SGustavo Padovan 39764a964404SMarcel Holtmann /* Devices that are marked for raw-only usage are unconfigured 39774a964404SMarcel Holtmann * and should not be included in normal operation. 3978fee746b0SMarcel Holtmann */ 3979fee746b0SMarcel Holtmann if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 39804a964404SMarcel Holtmann set_bit(HCI_UNCONFIGURED, &hdev->dev_flags); 3981fee746b0SMarcel Holtmann 39821da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_REG); 3983dc946bd8SDavid Herrmann hci_dev_hold(hdev); 39841da177e4SLinus Torvalds 398519202573SJohan Hedberg queue_work(hdev->req_workqueue, &hdev->power_on); 3986fbe96d6fSMarcel Holtmann 39871da177e4SLinus Torvalds return id; 3988f48fd9c8SMarcel Holtmann 398999780a7bSJohan Hedberg err_tfm: 399099780a7bSJohan Hedberg crypto_free_blkcipher(hdev->tfm_aes); 399133ca954dSDavid Herrmann err_wqueue: 399233ca954dSDavid Herrmann destroy_workqueue(hdev->workqueue); 39936ead1bbcSJohan Hedberg destroy_workqueue(hdev->req_workqueue); 399433ca954dSDavid Herrmann err: 39953df92b31SSasha Levin ida_simple_remove(&hci_index_ida, hdev->id); 3996f48fd9c8SMarcel Holtmann 399733ca954dSDavid Herrmann return error; 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_dev); 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds /* Unregister HCI device */ 400259735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev) 40031da177e4SLinus Torvalds { 40043df92b31SSasha Levin int i, id; 4005ef222013SMarcel Holtmann 4006c13854ceSMarcel Holtmann BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 40071da177e4SLinus Torvalds 400894324962SJohan Hovold set_bit(HCI_UNREGISTER, &hdev->dev_flags); 400994324962SJohan Hovold 40103df92b31SSasha Levin id = hdev->id; 40113df92b31SSasha Levin 4012f20d09d5SGustavo F. Padovan write_lock(&hci_dev_list_lock); 40131da177e4SLinus Torvalds list_del(&hdev->list); 4014f20d09d5SGustavo F. Padovan write_unlock(&hci_dev_list_lock); 40151da177e4SLinus Torvalds 40161da177e4SLinus Torvalds hci_dev_do_close(hdev); 40171da177e4SLinus Torvalds 4018cd4c5391SSuraj Sumangala for (i = 0; i < NUM_REASSEMBLY; i++) 4019ef222013SMarcel Holtmann kfree_skb(hdev->reassembly[i]); 4020ef222013SMarcel Holtmann 4021b9b5ef18SGustavo Padovan cancel_work_sync(&hdev->power_on); 4022b9b5ef18SGustavo Padovan 4023ab81cbf9SJohan Hedberg if (!test_bit(HCI_INIT, &hdev->flags) && 40240602a8adSMarcel Holtmann !test_bit(HCI_SETUP, &hdev->dev_flags)) { 402509fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 4026744cf19eSJohan Hedberg mgmt_index_removed(hdev); 402709fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 402856e5cb86SJohan Hedberg } 4029ab81cbf9SJohan Hedberg 40302e58ef3eSJohan Hedberg /* mgmt_index_removed should take care of emptying the 40312e58ef3eSJohan Hedberg * pending list */ 40322e58ef3eSJohan Hedberg BUG_ON(!list_empty(&hdev->mgmt_pending)); 40332e58ef3eSJohan Hedberg 40341da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_UNREG); 40351da177e4SLinus Torvalds 4036611b30f7SMarcel Holtmann if (hdev->rfkill) { 4037611b30f7SMarcel Holtmann rfkill_unregister(hdev->rfkill); 4038611b30f7SMarcel Holtmann rfkill_destroy(hdev->rfkill); 4039611b30f7SMarcel Holtmann } 4040611b30f7SMarcel Holtmann 404199780a7bSJohan Hedberg if (hdev->tfm_aes) 404299780a7bSJohan Hedberg crypto_free_blkcipher(hdev->tfm_aes); 404399780a7bSJohan Hedberg 4044bdc3e0f1SMarcel Holtmann device_del(&hdev->dev); 4045147e2d59SDave Young 40460153e2ecSMarcel Holtmann debugfs_remove_recursive(hdev->debugfs); 40470153e2ecSMarcel Holtmann 4048f48fd9c8SMarcel Holtmann destroy_workqueue(hdev->workqueue); 40496ead1bbcSJohan Hedberg destroy_workqueue(hdev->req_workqueue); 4050f48fd9c8SMarcel Holtmann 405109fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 4052e2e0cacbSJohan Hedberg hci_blacklist_clear(hdev); 40532aeb9a1aSJohan Hedberg hci_uuids_clear(hdev); 405455ed8ca1SJohan Hedberg hci_link_keys_clear(hdev); 4055b899efafSVinicius Costa Gomes hci_smp_ltks_clear(hdev); 4056970c4e46SJohan Hedberg hci_smp_irks_clear(hdev); 40572763eda6SSzymon Janc hci_remote_oob_data_clear(hdev); 4058d2ab0ac1SMarcel Holtmann hci_white_list_clear(hdev); 4059373110c5SJohan Hedberg hci_conn_params_clear_all(hdev); 406009fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 4061e2e0cacbSJohan Hedberg 4062dc946bd8SDavid Herrmann hci_dev_put(hdev); 40633df92b31SSasha Levin 40643df92b31SSasha Levin ida_simple_remove(&hci_index_ida, id); 40651da177e4SLinus Torvalds } 40661da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_dev); 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds /* Suspend HCI device */ 40691da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev) 40701da177e4SLinus Torvalds { 40711da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_SUSPEND); 40721da177e4SLinus Torvalds return 0; 40731da177e4SLinus Torvalds } 40741da177e4SLinus Torvalds EXPORT_SYMBOL(hci_suspend_dev); 40751da177e4SLinus Torvalds 40761da177e4SLinus Torvalds /* Resume HCI device */ 40771da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev) 40781da177e4SLinus Torvalds { 40791da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_RESUME); 40801da177e4SLinus Torvalds return 0; 40811da177e4SLinus Torvalds } 40821da177e4SLinus Torvalds EXPORT_SYMBOL(hci_resume_dev); 40831da177e4SLinus Torvalds 408476bca880SMarcel Holtmann /* Receive frame from HCI drivers */ 4085e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) 408676bca880SMarcel Holtmann { 408776bca880SMarcel Holtmann if (!hdev || (!test_bit(HCI_UP, &hdev->flags) 408876bca880SMarcel Holtmann && !test_bit(HCI_INIT, &hdev->flags))) { 408976bca880SMarcel Holtmann kfree_skb(skb); 409076bca880SMarcel Holtmann return -ENXIO; 409176bca880SMarcel Holtmann } 409276bca880SMarcel Holtmann 4093d82603c6SJorrit Schippers /* Incoming skb */ 409476bca880SMarcel Holtmann bt_cb(skb)->incoming = 1; 409576bca880SMarcel Holtmann 409676bca880SMarcel Holtmann /* Time stamp */ 409776bca880SMarcel Holtmann __net_timestamp(skb); 409876bca880SMarcel Holtmann 409976bca880SMarcel Holtmann skb_queue_tail(&hdev->rx_q, skb); 4100b78752ccSMarcel Holtmann queue_work(hdev->workqueue, &hdev->rx_work); 4101c78ae283SMarcel Holtmann 410276bca880SMarcel Holtmann return 0; 410376bca880SMarcel Holtmann } 410476bca880SMarcel Holtmann EXPORT_SYMBOL(hci_recv_frame); 410576bca880SMarcel Holtmann 410633e882a5SSuraj Sumangala static int hci_reassembly(struct hci_dev *hdev, int type, void *data, 41071e429f38SGustavo F. Padovan int count, __u8 index) 410833e882a5SSuraj Sumangala { 410933e882a5SSuraj Sumangala int len = 0; 411033e882a5SSuraj Sumangala int hlen = 0; 411133e882a5SSuraj Sumangala int remain = count; 411233e882a5SSuraj Sumangala struct sk_buff *skb; 411333e882a5SSuraj Sumangala struct bt_skb_cb *scb; 411433e882a5SSuraj Sumangala 411533e882a5SSuraj Sumangala if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || 411633e882a5SSuraj Sumangala index >= NUM_REASSEMBLY) 411733e882a5SSuraj Sumangala return -EILSEQ; 411833e882a5SSuraj Sumangala 411933e882a5SSuraj Sumangala skb = hdev->reassembly[index]; 412033e882a5SSuraj Sumangala 412133e882a5SSuraj Sumangala if (!skb) { 412233e882a5SSuraj Sumangala switch (type) { 412333e882a5SSuraj Sumangala case HCI_ACLDATA_PKT: 412433e882a5SSuraj Sumangala len = HCI_MAX_FRAME_SIZE; 412533e882a5SSuraj Sumangala hlen = HCI_ACL_HDR_SIZE; 412633e882a5SSuraj Sumangala break; 412733e882a5SSuraj Sumangala case HCI_EVENT_PKT: 412833e882a5SSuraj Sumangala len = HCI_MAX_EVENT_SIZE; 412933e882a5SSuraj Sumangala hlen = HCI_EVENT_HDR_SIZE; 413033e882a5SSuraj Sumangala break; 413133e882a5SSuraj Sumangala case HCI_SCODATA_PKT: 413233e882a5SSuraj Sumangala len = HCI_MAX_SCO_SIZE; 413333e882a5SSuraj Sumangala hlen = HCI_SCO_HDR_SIZE; 413433e882a5SSuraj Sumangala break; 413533e882a5SSuraj Sumangala } 413633e882a5SSuraj Sumangala 41371e429f38SGustavo F. Padovan skb = bt_skb_alloc(len, GFP_ATOMIC); 413833e882a5SSuraj Sumangala if (!skb) 413933e882a5SSuraj Sumangala return -ENOMEM; 414033e882a5SSuraj Sumangala 414133e882a5SSuraj Sumangala scb = (void *) skb->cb; 414233e882a5SSuraj Sumangala scb->expect = hlen; 414333e882a5SSuraj Sumangala scb->pkt_type = type; 414433e882a5SSuraj Sumangala 414533e882a5SSuraj Sumangala hdev->reassembly[index] = skb; 414633e882a5SSuraj Sumangala } 414733e882a5SSuraj Sumangala 414833e882a5SSuraj Sumangala while (count) { 414933e882a5SSuraj Sumangala scb = (void *) skb->cb; 415089bb46d0SDan Carpenter len = min_t(uint, scb->expect, count); 415133e882a5SSuraj Sumangala 415233e882a5SSuraj Sumangala memcpy(skb_put(skb, len), data, len); 415333e882a5SSuraj Sumangala 415433e882a5SSuraj Sumangala count -= len; 415533e882a5SSuraj Sumangala data += len; 415633e882a5SSuraj Sumangala scb->expect -= len; 415733e882a5SSuraj Sumangala remain = count; 415833e882a5SSuraj Sumangala 415933e882a5SSuraj Sumangala switch (type) { 416033e882a5SSuraj Sumangala case HCI_EVENT_PKT: 416133e882a5SSuraj Sumangala if (skb->len == HCI_EVENT_HDR_SIZE) { 416233e882a5SSuraj Sumangala struct hci_event_hdr *h = hci_event_hdr(skb); 416333e882a5SSuraj Sumangala scb->expect = h->plen; 416433e882a5SSuraj Sumangala 416533e882a5SSuraj Sumangala if (skb_tailroom(skb) < scb->expect) { 416633e882a5SSuraj Sumangala kfree_skb(skb); 416733e882a5SSuraj Sumangala hdev->reassembly[index] = NULL; 416833e882a5SSuraj Sumangala return -ENOMEM; 416933e882a5SSuraj Sumangala } 417033e882a5SSuraj Sumangala } 417133e882a5SSuraj Sumangala break; 417233e882a5SSuraj Sumangala 417333e882a5SSuraj Sumangala case HCI_ACLDATA_PKT: 417433e882a5SSuraj Sumangala if (skb->len == HCI_ACL_HDR_SIZE) { 417533e882a5SSuraj Sumangala struct hci_acl_hdr *h = hci_acl_hdr(skb); 417633e882a5SSuraj Sumangala scb->expect = __le16_to_cpu(h->dlen); 417733e882a5SSuraj Sumangala 417833e882a5SSuraj Sumangala if (skb_tailroom(skb) < scb->expect) { 417933e882a5SSuraj Sumangala kfree_skb(skb); 418033e882a5SSuraj Sumangala hdev->reassembly[index] = NULL; 418133e882a5SSuraj Sumangala return -ENOMEM; 418233e882a5SSuraj Sumangala } 418333e882a5SSuraj Sumangala } 418433e882a5SSuraj Sumangala break; 418533e882a5SSuraj Sumangala 418633e882a5SSuraj Sumangala case HCI_SCODATA_PKT: 418733e882a5SSuraj Sumangala if (skb->len == HCI_SCO_HDR_SIZE) { 418833e882a5SSuraj Sumangala struct hci_sco_hdr *h = hci_sco_hdr(skb); 418933e882a5SSuraj Sumangala scb->expect = h->dlen; 419033e882a5SSuraj Sumangala 419133e882a5SSuraj Sumangala if (skb_tailroom(skb) < scb->expect) { 419233e882a5SSuraj Sumangala kfree_skb(skb); 419333e882a5SSuraj Sumangala hdev->reassembly[index] = NULL; 419433e882a5SSuraj Sumangala return -ENOMEM; 419533e882a5SSuraj Sumangala } 419633e882a5SSuraj Sumangala } 419733e882a5SSuraj Sumangala break; 419833e882a5SSuraj Sumangala } 419933e882a5SSuraj Sumangala 420033e882a5SSuraj Sumangala if (scb->expect == 0) { 420133e882a5SSuraj Sumangala /* Complete frame */ 420233e882a5SSuraj Sumangala 420333e882a5SSuraj Sumangala bt_cb(skb)->pkt_type = type; 4204e1a26170SMarcel Holtmann hci_recv_frame(hdev, skb); 420533e882a5SSuraj Sumangala 420633e882a5SSuraj Sumangala hdev->reassembly[index] = NULL; 420733e882a5SSuraj Sumangala return remain; 420833e882a5SSuraj Sumangala } 420933e882a5SSuraj Sumangala } 421033e882a5SSuraj Sumangala 421133e882a5SSuraj Sumangala return remain; 421233e882a5SSuraj Sumangala } 421333e882a5SSuraj Sumangala 4214ef222013SMarcel Holtmann int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) 4215ef222013SMarcel Holtmann { 4216f39a3c06SSuraj Sumangala int rem = 0; 4217f39a3c06SSuraj Sumangala 4218ef222013SMarcel Holtmann if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) 4219ef222013SMarcel Holtmann return -EILSEQ; 4220ef222013SMarcel Holtmann 4221da5f6c37SGustavo F. Padovan while (count) { 42221e429f38SGustavo F. Padovan rem = hci_reassembly(hdev, type, data, count, type - 1); 4223f39a3c06SSuraj Sumangala if (rem < 0) 4224f39a3c06SSuraj Sumangala return rem; 4225ef222013SMarcel Holtmann 4226f39a3c06SSuraj Sumangala data += (count - rem); 4227f39a3c06SSuraj Sumangala count = rem; 4228f81c6224SJoe Perches } 4229ef222013SMarcel Holtmann 4230f39a3c06SSuraj Sumangala return rem; 4231ef222013SMarcel Holtmann } 4232ef222013SMarcel Holtmann EXPORT_SYMBOL(hci_recv_fragment); 4233ef222013SMarcel Holtmann 423499811510SSuraj Sumangala #define STREAM_REASSEMBLY 0 423599811510SSuraj Sumangala 423699811510SSuraj Sumangala int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count) 423799811510SSuraj Sumangala { 423899811510SSuraj Sumangala int type; 423999811510SSuraj Sumangala int rem = 0; 424099811510SSuraj Sumangala 4241da5f6c37SGustavo F. Padovan while (count) { 424299811510SSuraj Sumangala struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY]; 424399811510SSuraj Sumangala 424499811510SSuraj Sumangala if (!skb) { 424599811510SSuraj Sumangala struct { char type; } *pkt; 424699811510SSuraj Sumangala 424799811510SSuraj Sumangala /* Start of the frame */ 424899811510SSuraj Sumangala pkt = data; 424999811510SSuraj Sumangala type = pkt->type; 425099811510SSuraj Sumangala 425199811510SSuraj Sumangala data++; 425299811510SSuraj Sumangala count--; 425399811510SSuraj Sumangala } else 425499811510SSuraj Sumangala type = bt_cb(skb)->pkt_type; 425599811510SSuraj Sumangala 42561e429f38SGustavo F. Padovan rem = hci_reassembly(hdev, type, data, count, 42571e429f38SGustavo F. Padovan STREAM_REASSEMBLY); 425899811510SSuraj Sumangala if (rem < 0) 425999811510SSuraj Sumangala return rem; 426099811510SSuraj Sumangala 426199811510SSuraj Sumangala data += (count - rem); 426299811510SSuraj Sumangala count = rem; 4263f81c6224SJoe Perches } 426499811510SSuraj Sumangala 426599811510SSuraj Sumangala return rem; 426699811510SSuraj Sumangala } 426799811510SSuraj Sumangala EXPORT_SYMBOL(hci_recv_stream_fragment); 426899811510SSuraj Sumangala 42691da177e4SLinus Torvalds /* ---- Interface to upper protocols ---- */ 42701da177e4SLinus Torvalds 42711da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *cb) 42721da177e4SLinus Torvalds { 42731da177e4SLinus Torvalds BT_DBG("%p name %s", cb, cb->name); 42741da177e4SLinus Torvalds 4275f20d09d5SGustavo F. Padovan write_lock(&hci_cb_list_lock); 42761da177e4SLinus Torvalds list_add(&cb->list, &hci_cb_list); 4277f20d09d5SGustavo F. Padovan write_unlock(&hci_cb_list_lock); 42781da177e4SLinus Torvalds 42791da177e4SLinus Torvalds return 0; 42801da177e4SLinus Torvalds } 42811da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_cb); 42821da177e4SLinus Torvalds 42831da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *cb) 42841da177e4SLinus Torvalds { 42851da177e4SLinus Torvalds BT_DBG("%p name %s", cb, cb->name); 42861da177e4SLinus Torvalds 4287f20d09d5SGustavo F. Padovan write_lock(&hci_cb_list_lock); 42881da177e4SLinus Torvalds list_del(&cb->list); 4289f20d09d5SGustavo F. Padovan write_unlock(&hci_cb_list_lock); 42901da177e4SLinus Torvalds 42911da177e4SLinus Torvalds return 0; 42921da177e4SLinus Torvalds } 42931da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_cb); 42941da177e4SLinus Torvalds 429551086991SMarcel Holtmann static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) 42961da177e4SLinus Torvalds { 42970d48d939SMarcel Holtmann BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); 42981da177e4SLinus Torvalds 42991da177e4SLinus Torvalds /* Time stamp */ 4300a61bbcf2SPatrick McHardy __net_timestamp(skb); 43011da177e4SLinus Torvalds 4302cd82e61cSMarcel Holtmann /* Send copy to monitor */ 4303cd82e61cSMarcel Holtmann hci_send_to_monitor(hdev, skb); 4304cd82e61cSMarcel Holtmann 4305cd82e61cSMarcel Holtmann if (atomic_read(&hdev->promisc)) { 4306cd82e61cSMarcel Holtmann /* Send copy to the sockets */ 4307470fe1b5SMarcel Holtmann hci_send_to_sock(hdev, skb); 43081da177e4SLinus Torvalds } 43091da177e4SLinus Torvalds 43101da177e4SLinus Torvalds /* Get rid of skb owner, prior to sending to the driver. */ 43111da177e4SLinus Torvalds skb_orphan(skb); 43121da177e4SLinus Torvalds 43137bd8f09fSMarcel Holtmann if (hdev->send(hdev, skb) < 0) 431451086991SMarcel Holtmann BT_ERR("%s sending frame failed", hdev->name); 43151da177e4SLinus Torvalds } 43161da177e4SLinus Torvalds 43173119ae95SJohan Hedberg void hci_req_init(struct hci_request *req, struct hci_dev *hdev) 43183119ae95SJohan Hedberg { 43193119ae95SJohan Hedberg skb_queue_head_init(&req->cmd_q); 43203119ae95SJohan Hedberg req->hdev = hdev; 43215d73e034SAndre Guedes req->err = 0; 43223119ae95SJohan Hedberg } 43233119ae95SJohan Hedberg 43243119ae95SJohan Hedberg int hci_req_run(struct hci_request *req, hci_req_complete_t complete) 43253119ae95SJohan Hedberg { 43263119ae95SJohan Hedberg struct hci_dev *hdev = req->hdev; 43273119ae95SJohan Hedberg struct sk_buff *skb; 43283119ae95SJohan Hedberg unsigned long flags; 43293119ae95SJohan Hedberg 43303119ae95SJohan Hedberg BT_DBG("length %u", skb_queue_len(&req->cmd_q)); 43313119ae95SJohan Hedberg 43325d73e034SAndre Guedes /* If an error occured during request building, remove all HCI 43335d73e034SAndre Guedes * commands queued on the HCI request queue. 43345d73e034SAndre Guedes */ 43355d73e034SAndre Guedes if (req->err) { 43365d73e034SAndre Guedes skb_queue_purge(&req->cmd_q); 43375d73e034SAndre Guedes return req->err; 43385d73e034SAndre Guedes } 43395d73e034SAndre Guedes 43403119ae95SJohan Hedberg /* Do not allow empty requests */ 43413119ae95SJohan Hedberg if (skb_queue_empty(&req->cmd_q)) 4342382b0c39SAndre Guedes return -ENODATA; 43433119ae95SJohan Hedberg 43443119ae95SJohan Hedberg skb = skb_peek_tail(&req->cmd_q); 43453119ae95SJohan Hedberg bt_cb(skb)->req.complete = complete; 43463119ae95SJohan Hedberg 43473119ae95SJohan Hedberg spin_lock_irqsave(&hdev->cmd_q.lock, flags); 43483119ae95SJohan Hedberg skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); 43493119ae95SJohan Hedberg spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); 43503119ae95SJohan Hedberg 43513119ae95SJohan Hedberg queue_work(hdev->workqueue, &hdev->cmd_work); 43523119ae95SJohan Hedberg 43533119ae95SJohan Hedberg return 0; 43543119ae95SJohan Hedberg } 43553119ae95SJohan Hedberg 43561ca3a9d0SJohan Hedberg static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, 435707dc93ddSJohan Hedberg u32 plen, const void *param) 43581da177e4SLinus Torvalds { 43591da177e4SLinus Torvalds int len = HCI_COMMAND_HDR_SIZE + plen; 43601da177e4SLinus Torvalds struct hci_command_hdr *hdr; 43611da177e4SLinus Torvalds struct sk_buff *skb; 43621da177e4SLinus Torvalds 43631da177e4SLinus Torvalds skb = bt_skb_alloc(len, GFP_ATOMIC); 43641ca3a9d0SJohan Hedberg if (!skb) 43651ca3a9d0SJohan Hedberg return NULL; 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); 4368a9de9248SMarcel Holtmann hdr->opcode = cpu_to_le16(opcode); 43691da177e4SLinus Torvalds hdr->plen = plen; 43701da177e4SLinus Torvalds 43711da177e4SLinus Torvalds if (plen) 43721da177e4SLinus Torvalds memcpy(skb_put(skb, plen), param, plen); 43731da177e4SLinus Torvalds 43741da177e4SLinus Torvalds BT_DBG("skb len %d", skb->len); 43751da177e4SLinus Torvalds 43760d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; 4377c78ae283SMarcel Holtmann 43781ca3a9d0SJohan Hedberg return skb; 43791ca3a9d0SJohan Hedberg } 43801ca3a9d0SJohan Hedberg 43811ca3a9d0SJohan Hedberg /* Send HCI command */ 438207dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, 438307dc93ddSJohan Hedberg const void *param) 43841ca3a9d0SJohan Hedberg { 43851ca3a9d0SJohan Hedberg struct sk_buff *skb; 43861ca3a9d0SJohan Hedberg 43871ca3a9d0SJohan Hedberg BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); 43881ca3a9d0SJohan Hedberg 43891ca3a9d0SJohan Hedberg skb = hci_prepare_cmd(hdev, opcode, plen, param); 43901ca3a9d0SJohan Hedberg if (!skb) { 43911ca3a9d0SJohan Hedberg BT_ERR("%s no memory for command", hdev->name); 43921ca3a9d0SJohan Hedberg return -ENOMEM; 43931ca3a9d0SJohan Hedberg } 43941ca3a9d0SJohan Hedberg 439511714b3dSJohan Hedberg /* Stand-alone HCI commands must be flaged as 439611714b3dSJohan Hedberg * single-command requests. 439711714b3dSJohan Hedberg */ 439811714b3dSJohan Hedberg bt_cb(skb)->req.start = true; 439911714b3dSJohan Hedberg 44001da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 4401c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 44021da177e4SLinus Torvalds 44031da177e4SLinus Torvalds return 0; 44041da177e4SLinus Torvalds } 44051da177e4SLinus Torvalds 440671c76a17SJohan Hedberg /* Queue a command to an asynchronous HCI request */ 440707dc93ddSJohan Hedberg void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, 440807dc93ddSJohan Hedberg const void *param, u8 event) 440971c76a17SJohan Hedberg { 441071c76a17SJohan Hedberg struct hci_dev *hdev = req->hdev; 441171c76a17SJohan Hedberg struct sk_buff *skb; 441271c76a17SJohan Hedberg 441371c76a17SJohan Hedberg BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); 441471c76a17SJohan Hedberg 441534739c1eSAndre Guedes /* If an error occured during request building, there is no point in 441634739c1eSAndre Guedes * queueing the HCI command. We can simply return. 441734739c1eSAndre Guedes */ 441834739c1eSAndre Guedes if (req->err) 441934739c1eSAndre Guedes return; 442034739c1eSAndre Guedes 442171c76a17SJohan Hedberg skb = hci_prepare_cmd(hdev, opcode, plen, param); 442271c76a17SJohan Hedberg if (!skb) { 44235d73e034SAndre Guedes BT_ERR("%s no memory for command (opcode 0x%4.4x)", 44245d73e034SAndre Guedes hdev->name, opcode); 44255d73e034SAndre Guedes req->err = -ENOMEM; 4426e348fe6bSAndre Guedes return; 442771c76a17SJohan Hedberg } 442871c76a17SJohan Hedberg 442971c76a17SJohan Hedberg if (skb_queue_empty(&req->cmd_q)) 443071c76a17SJohan Hedberg bt_cb(skb)->req.start = true; 443171c76a17SJohan Hedberg 443202350a72SJohan Hedberg bt_cb(skb)->req.event = event; 443302350a72SJohan Hedberg 443471c76a17SJohan Hedberg skb_queue_tail(&req->cmd_q, skb); 443571c76a17SJohan Hedberg } 443671c76a17SJohan Hedberg 443707dc93ddSJohan Hedberg void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, 443807dc93ddSJohan Hedberg const void *param) 443902350a72SJohan Hedberg { 444002350a72SJohan Hedberg hci_req_add_ev(req, opcode, plen, param, 0); 444102350a72SJohan Hedberg } 444202350a72SJohan Hedberg 44431da177e4SLinus Torvalds /* Get data from the previously sent command */ 4444a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) 44451da177e4SLinus Torvalds { 44461da177e4SLinus Torvalds struct hci_command_hdr *hdr; 44471da177e4SLinus Torvalds 44481da177e4SLinus Torvalds if (!hdev->sent_cmd) 44491da177e4SLinus Torvalds return NULL; 44501da177e4SLinus Torvalds 44511da177e4SLinus Torvalds hdr = (void *) hdev->sent_cmd->data; 44521da177e4SLinus Torvalds 4453a9de9248SMarcel Holtmann if (hdr->opcode != cpu_to_le16(opcode)) 44541da177e4SLinus Torvalds return NULL; 44551da177e4SLinus Torvalds 4456f0e09510SAndrei Emeltchenko BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); 44571da177e4SLinus Torvalds 44581da177e4SLinus Torvalds return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; 44591da177e4SLinus Torvalds } 44601da177e4SLinus Torvalds 44611da177e4SLinus Torvalds /* Send ACL data */ 44621da177e4SLinus Torvalds static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) 44631da177e4SLinus Torvalds { 44641da177e4SLinus Torvalds struct hci_acl_hdr *hdr; 44651da177e4SLinus Torvalds int len = skb->len; 44661da177e4SLinus Torvalds 4467badff6d0SArnaldo Carvalho de Melo skb_push(skb, HCI_ACL_HDR_SIZE); 4468badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 44699c70220bSArnaldo Carvalho de Melo hdr = (struct hci_acl_hdr *)skb_transport_header(skb); 4470aca3192cSYOSHIFUJI Hideaki hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); 4471aca3192cSYOSHIFUJI Hideaki hdr->dlen = cpu_to_le16(len); 44721da177e4SLinus Torvalds } 44731da177e4SLinus Torvalds 4474ee22be7eSAndrei Emeltchenko static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue, 447573d80debSLuiz Augusto von Dentz struct sk_buff *skb, __u16 flags) 44761da177e4SLinus Torvalds { 4477ee22be7eSAndrei Emeltchenko struct hci_conn *conn = chan->conn; 44781da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 44791da177e4SLinus Torvalds struct sk_buff *list; 44801da177e4SLinus Torvalds 4481087bfd99SGustavo Padovan skb->len = skb_headlen(skb); 4482087bfd99SGustavo Padovan skb->data_len = 0; 4483087bfd99SGustavo Padovan 4484087bfd99SGustavo Padovan bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 4485204a6e54SAndrei Emeltchenko 4486204a6e54SAndrei Emeltchenko switch (hdev->dev_type) { 4487204a6e54SAndrei Emeltchenko case HCI_BREDR: 4488087bfd99SGustavo Padovan hci_add_acl_hdr(skb, conn->handle, flags); 4489204a6e54SAndrei Emeltchenko break; 4490204a6e54SAndrei Emeltchenko case HCI_AMP: 4491204a6e54SAndrei Emeltchenko hci_add_acl_hdr(skb, chan->handle, flags); 4492204a6e54SAndrei Emeltchenko break; 4493204a6e54SAndrei Emeltchenko default: 4494204a6e54SAndrei Emeltchenko BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); 4495204a6e54SAndrei Emeltchenko return; 4496204a6e54SAndrei Emeltchenko } 4497087bfd99SGustavo Padovan 449870f23020SAndrei Emeltchenko list = skb_shinfo(skb)->frag_list; 449970f23020SAndrei Emeltchenko if (!list) { 45001da177e4SLinus Torvalds /* Non fragmented */ 45011da177e4SLinus Torvalds BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); 45021da177e4SLinus Torvalds 450373d80debSLuiz Augusto von Dentz skb_queue_tail(queue, skb); 45041da177e4SLinus Torvalds } else { 45051da177e4SLinus Torvalds /* Fragmented */ 45061da177e4SLinus Torvalds BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); 45071da177e4SLinus Torvalds 45081da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = NULL; 45091da177e4SLinus Torvalds 45101da177e4SLinus Torvalds /* Queue all fragments atomically */ 4511af3e6359SGustavo F. Padovan spin_lock(&queue->lock); 45121da177e4SLinus Torvalds 451373d80debSLuiz Augusto von Dentz __skb_queue_tail(queue, skb); 4514e702112fSAndrei Emeltchenko 4515e702112fSAndrei Emeltchenko flags &= ~ACL_START; 4516e702112fSAndrei Emeltchenko flags |= ACL_CONT; 45171da177e4SLinus Torvalds do { 45181da177e4SLinus Torvalds skb = list; list = list->next; 45191da177e4SLinus Torvalds 45200d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 4521e702112fSAndrei Emeltchenko hci_add_acl_hdr(skb, conn->handle, flags); 45221da177e4SLinus Torvalds 45231da177e4SLinus Torvalds BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); 45241da177e4SLinus Torvalds 452573d80debSLuiz Augusto von Dentz __skb_queue_tail(queue, skb); 45261da177e4SLinus Torvalds } while (list); 45271da177e4SLinus Torvalds 4528af3e6359SGustavo F. Padovan spin_unlock(&queue->lock); 45291da177e4SLinus Torvalds } 453073d80debSLuiz Augusto von Dentz } 453173d80debSLuiz Augusto von Dentz 453273d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) 453373d80debSLuiz Augusto von Dentz { 4534ee22be7eSAndrei Emeltchenko struct hci_dev *hdev = chan->conn->hdev; 453573d80debSLuiz Augusto von Dentz 4536f0e09510SAndrei Emeltchenko BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); 453773d80debSLuiz Augusto von Dentz 4538ee22be7eSAndrei Emeltchenko hci_queue_acl(chan, &chan->data_q, skb, flags); 45391da177e4SLinus Torvalds 45403eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 45411da177e4SLinus Torvalds } 45421da177e4SLinus Torvalds 45431da177e4SLinus Torvalds /* Send SCO data */ 45440d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) 45451da177e4SLinus Torvalds { 45461da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 45471da177e4SLinus Torvalds struct hci_sco_hdr hdr; 45481da177e4SLinus Torvalds 45491da177e4SLinus Torvalds BT_DBG("%s len %d", hdev->name, skb->len); 45501da177e4SLinus Torvalds 4551aca3192cSYOSHIFUJI Hideaki hdr.handle = cpu_to_le16(conn->handle); 45521da177e4SLinus Torvalds hdr.dlen = skb->len; 45531da177e4SLinus Torvalds 4554badff6d0SArnaldo Carvalho de Melo skb_push(skb, HCI_SCO_HDR_SIZE); 4555badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 45569c70220bSArnaldo Carvalho de Melo memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); 45571da177e4SLinus Torvalds 45580d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 4559c78ae283SMarcel Holtmann 45601da177e4SLinus Torvalds skb_queue_tail(&conn->data_q, skb); 45613eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 45621da177e4SLinus Torvalds } 45631da177e4SLinus Torvalds 45641da177e4SLinus Torvalds /* ---- HCI TX task (outgoing data) ---- */ 45651da177e4SLinus Torvalds 45661da177e4SLinus Torvalds /* HCI Connection scheduler */ 45676039aa73SGustavo Padovan static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, 4568a8c5fb1aSGustavo Padovan int *quote) 45691da177e4SLinus Torvalds { 45701da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 45718035ded4SLuiz Augusto von Dentz struct hci_conn *conn = NULL, *c; 4572abc5de8fSMikel Astiz unsigned int num = 0, min = ~0; 45731da177e4SLinus Torvalds 45741da177e4SLinus Torvalds /* We don't have to lock device here. Connections are always 45751da177e4SLinus Torvalds * added and removed with TX task disabled. */ 4576bf4c6325SGustavo F. Padovan 4577bf4c6325SGustavo F. Padovan rcu_read_lock(); 4578bf4c6325SGustavo F. Padovan 4579bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(c, &h->list, list) { 4580769be974SMarcel Holtmann if (c->type != type || skb_queue_empty(&c->data_q)) 45811da177e4SLinus Torvalds continue; 4582769be974SMarcel Holtmann 4583769be974SMarcel Holtmann if (c->state != BT_CONNECTED && c->state != BT_CONFIG) 4584769be974SMarcel Holtmann continue; 4585769be974SMarcel Holtmann 45861da177e4SLinus Torvalds num++; 45871da177e4SLinus Torvalds 45881da177e4SLinus Torvalds if (c->sent < min) { 45891da177e4SLinus Torvalds min = c->sent; 45901da177e4SLinus Torvalds conn = c; 45911da177e4SLinus Torvalds } 459252087a79SLuiz Augusto von Dentz 459352087a79SLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == num) 459452087a79SLuiz Augusto von Dentz break; 45951da177e4SLinus Torvalds } 45961da177e4SLinus Torvalds 4597bf4c6325SGustavo F. Padovan rcu_read_unlock(); 4598bf4c6325SGustavo F. Padovan 45991da177e4SLinus Torvalds if (conn) { 46006ed58ec5SVille Tervo int cnt, q; 46016ed58ec5SVille Tervo 46026ed58ec5SVille Tervo switch (conn->type) { 46036ed58ec5SVille Tervo case ACL_LINK: 46046ed58ec5SVille Tervo cnt = hdev->acl_cnt; 46056ed58ec5SVille Tervo break; 46066ed58ec5SVille Tervo case SCO_LINK: 46076ed58ec5SVille Tervo case ESCO_LINK: 46086ed58ec5SVille Tervo cnt = hdev->sco_cnt; 46096ed58ec5SVille Tervo break; 46106ed58ec5SVille Tervo case LE_LINK: 46116ed58ec5SVille Tervo cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; 46126ed58ec5SVille Tervo break; 46136ed58ec5SVille Tervo default: 46146ed58ec5SVille Tervo cnt = 0; 46156ed58ec5SVille Tervo BT_ERR("Unknown link type"); 46166ed58ec5SVille Tervo } 46176ed58ec5SVille Tervo 46186ed58ec5SVille Tervo q = cnt / num; 46191da177e4SLinus Torvalds *quote = q ? q : 1; 46201da177e4SLinus Torvalds } else 46211da177e4SLinus Torvalds *quote = 0; 46221da177e4SLinus Torvalds 46231da177e4SLinus Torvalds BT_DBG("conn %p quote %d", conn, *quote); 46241da177e4SLinus Torvalds return conn; 46251da177e4SLinus Torvalds } 46261da177e4SLinus Torvalds 46276039aa73SGustavo Padovan static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) 46281da177e4SLinus Torvalds { 46291da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 46301da177e4SLinus Torvalds struct hci_conn *c; 46311da177e4SLinus Torvalds 4632bae1f5d9SVille Tervo BT_ERR("%s link tx timeout", hdev->name); 46331da177e4SLinus Torvalds 4634bf4c6325SGustavo F. Padovan rcu_read_lock(); 4635bf4c6325SGustavo F. Padovan 46361da177e4SLinus Torvalds /* Kill stalled connections */ 4637bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(c, &h->list, list) { 4638bae1f5d9SVille Tervo if (c->type == type && c->sent) { 46396ed93dc6SAndrei Emeltchenko BT_ERR("%s killing stalled connection %pMR", 46406ed93dc6SAndrei Emeltchenko hdev->name, &c->dst); 4641bed71748SAndre Guedes hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); 46421da177e4SLinus Torvalds } 46431da177e4SLinus Torvalds } 4644bf4c6325SGustavo F. Padovan 4645bf4c6325SGustavo F. Padovan rcu_read_unlock(); 46461da177e4SLinus Torvalds } 46471da177e4SLinus Torvalds 46486039aa73SGustavo Padovan static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, 464973d80debSLuiz Augusto von Dentz int *quote) 465073d80debSLuiz Augusto von Dentz { 465173d80debSLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 465273d80debSLuiz Augusto von Dentz struct hci_chan *chan = NULL; 4653abc5de8fSMikel Astiz unsigned int num = 0, min = ~0, cur_prio = 0; 465473d80debSLuiz Augusto von Dentz struct hci_conn *conn; 465573d80debSLuiz Augusto von Dentz int cnt, q, conn_num = 0; 465673d80debSLuiz Augusto von Dentz 465773d80debSLuiz Augusto von Dentz BT_DBG("%s", hdev->name); 465873d80debSLuiz Augusto von Dentz 4659bf4c6325SGustavo F. Padovan rcu_read_lock(); 4660bf4c6325SGustavo F. Padovan 4661bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(conn, &h->list, list) { 466273d80debSLuiz Augusto von Dentz struct hci_chan *tmp; 466373d80debSLuiz Augusto von Dentz 466473d80debSLuiz Augusto von Dentz if (conn->type != type) 466573d80debSLuiz Augusto von Dentz continue; 466673d80debSLuiz Augusto von Dentz 466773d80debSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 466873d80debSLuiz Augusto von Dentz continue; 466973d80debSLuiz Augusto von Dentz 467073d80debSLuiz Augusto von Dentz conn_num++; 467173d80debSLuiz Augusto von Dentz 46728192edefSGustavo F. Padovan list_for_each_entry_rcu(tmp, &conn->chan_list, list) { 467373d80debSLuiz Augusto von Dentz struct sk_buff *skb; 467473d80debSLuiz Augusto von Dentz 467573d80debSLuiz Augusto von Dentz if (skb_queue_empty(&tmp->data_q)) 467673d80debSLuiz Augusto von Dentz continue; 467773d80debSLuiz Augusto von Dentz 467873d80debSLuiz Augusto von Dentz skb = skb_peek(&tmp->data_q); 467973d80debSLuiz Augusto von Dentz if (skb->priority < cur_prio) 468073d80debSLuiz Augusto von Dentz continue; 468173d80debSLuiz Augusto von Dentz 468273d80debSLuiz Augusto von Dentz if (skb->priority > cur_prio) { 468373d80debSLuiz Augusto von Dentz num = 0; 468473d80debSLuiz Augusto von Dentz min = ~0; 468573d80debSLuiz Augusto von Dentz cur_prio = skb->priority; 468673d80debSLuiz Augusto von Dentz } 468773d80debSLuiz Augusto von Dentz 468873d80debSLuiz Augusto von Dentz num++; 468973d80debSLuiz Augusto von Dentz 469073d80debSLuiz Augusto von Dentz if (conn->sent < min) { 469173d80debSLuiz Augusto von Dentz min = conn->sent; 469273d80debSLuiz Augusto von Dentz chan = tmp; 469373d80debSLuiz Augusto von Dentz } 469473d80debSLuiz Augusto von Dentz } 469573d80debSLuiz Augusto von Dentz 469673d80debSLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == conn_num) 469773d80debSLuiz Augusto von Dentz break; 469873d80debSLuiz Augusto von Dentz } 469973d80debSLuiz Augusto von Dentz 4700bf4c6325SGustavo F. Padovan rcu_read_unlock(); 4701bf4c6325SGustavo F. Padovan 470273d80debSLuiz Augusto von Dentz if (!chan) 470373d80debSLuiz Augusto von Dentz return NULL; 470473d80debSLuiz Augusto von Dentz 470573d80debSLuiz Augusto von Dentz switch (chan->conn->type) { 470673d80debSLuiz Augusto von Dentz case ACL_LINK: 470773d80debSLuiz Augusto von Dentz cnt = hdev->acl_cnt; 470873d80debSLuiz Augusto von Dentz break; 4709bd1eb66bSAndrei Emeltchenko case AMP_LINK: 4710bd1eb66bSAndrei Emeltchenko cnt = hdev->block_cnt; 4711bd1eb66bSAndrei Emeltchenko break; 471273d80debSLuiz Augusto von Dentz case SCO_LINK: 471373d80debSLuiz Augusto von Dentz case ESCO_LINK: 471473d80debSLuiz Augusto von Dentz cnt = hdev->sco_cnt; 471573d80debSLuiz Augusto von Dentz break; 471673d80debSLuiz Augusto von Dentz case LE_LINK: 471773d80debSLuiz Augusto von Dentz cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; 471873d80debSLuiz Augusto von Dentz break; 471973d80debSLuiz Augusto von Dentz default: 472073d80debSLuiz Augusto von Dentz cnt = 0; 472173d80debSLuiz Augusto von Dentz BT_ERR("Unknown link type"); 472273d80debSLuiz Augusto von Dentz } 472373d80debSLuiz Augusto von Dentz 472473d80debSLuiz Augusto von Dentz q = cnt / num; 472573d80debSLuiz Augusto von Dentz *quote = q ? q : 1; 472673d80debSLuiz Augusto von Dentz BT_DBG("chan %p quote %d", chan, *quote); 472773d80debSLuiz Augusto von Dentz return chan; 472873d80debSLuiz Augusto von Dentz } 472973d80debSLuiz Augusto von Dentz 473002b20f0bSLuiz Augusto von Dentz static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) 473102b20f0bSLuiz Augusto von Dentz { 473202b20f0bSLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 473302b20f0bSLuiz Augusto von Dentz struct hci_conn *conn; 473402b20f0bSLuiz Augusto von Dentz int num = 0; 473502b20f0bSLuiz Augusto von Dentz 473602b20f0bSLuiz Augusto von Dentz BT_DBG("%s", hdev->name); 473702b20f0bSLuiz Augusto von Dentz 4738bf4c6325SGustavo F. Padovan rcu_read_lock(); 4739bf4c6325SGustavo F. Padovan 4740bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(conn, &h->list, list) { 474102b20f0bSLuiz Augusto von Dentz struct hci_chan *chan; 474202b20f0bSLuiz Augusto von Dentz 474302b20f0bSLuiz Augusto von Dentz if (conn->type != type) 474402b20f0bSLuiz Augusto von Dentz continue; 474502b20f0bSLuiz Augusto von Dentz 474602b20f0bSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 474702b20f0bSLuiz Augusto von Dentz continue; 474802b20f0bSLuiz Augusto von Dentz 474902b20f0bSLuiz Augusto von Dentz num++; 475002b20f0bSLuiz Augusto von Dentz 47518192edefSGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_list, list) { 475202b20f0bSLuiz Augusto von Dentz struct sk_buff *skb; 475302b20f0bSLuiz Augusto von Dentz 475402b20f0bSLuiz Augusto von Dentz if (chan->sent) { 475502b20f0bSLuiz Augusto von Dentz chan->sent = 0; 475602b20f0bSLuiz Augusto von Dentz continue; 475702b20f0bSLuiz Augusto von Dentz } 475802b20f0bSLuiz Augusto von Dentz 475902b20f0bSLuiz Augusto von Dentz if (skb_queue_empty(&chan->data_q)) 476002b20f0bSLuiz Augusto von Dentz continue; 476102b20f0bSLuiz Augusto von Dentz 476202b20f0bSLuiz Augusto von Dentz skb = skb_peek(&chan->data_q); 476302b20f0bSLuiz Augusto von Dentz if (skb->priority >= HCI_PRIO_MAX - 1) 476402b20f0bSLuiz Augusto von Dentz continue; 476502b20f0bSLuiz Augusto von Dentz 476602b20f0bSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX - 1; 476702b20f0bSLuiz Augusto von Dentz 476802b20f0bSLuiz Augusto von Dentz BT_DBG("chan %p skb %p promoted to %d", chan, skb, 476902b20f0bSLuiz Augusto von Dentz skb->priority); 477002b20f0bSLuiz Augusto von Dentz } 477102b20f0bSLuiz Augusto von Dentz 477202b20f0bSLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == num) 477302b20f0bSLuiz Augusto von Dentz break; 477402b20f0bSLuiz Augusto von Dentz } 4775bf4c6325SGustavo F. Padovan 4776bf4c6325SGustavo F. Padovan rcu_read_unlock(); 4777bf4c6325SGustavo F. Padovan 477802b20f0bSLuiz Augusto von Dentz } 477902b20f0bSLuiz Augusto von Dentz 4780b71d385aSAndrei Emeltchenko static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) 4781b71d385aSAndrei Emeltchenko { 4782b71d385aSAndrei Emeltchenko /* Calculate count of blocks used by this packet */ 4783b71d385aSAndrei Emeltchenko return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); 4784b71d385aSAndrei Emeltchenko } 4785b71d385aSAndrei Emeltchenko 47866039aa73SGustavo Padovan static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) 47871da177e4SLinus Torvalds { 47884a964404SMarcel Holtmann if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 47891da177e4SLinus Torvalds /* ACL tx timeout must be longer than maximum 47901da177e4SLinus Torvalds * link supervision timeout (40.9 seconds) */ 479163d2bc1bSAndrei Emeltchenko if (!cnt && time_after(jiffies, hdev->acl_last_tx + 47925f246e89SAndrei Emeltchenko HCI_ACL_TX_TIMEOUT)) 4793bae1f5d9SVille Tervo hci_link_tx_to(hdev, ACL_LINK); 47941da177e4SLinus Torvalds } 479563d2bc1bSAndrei Emeltchenko } 47961da177e4SLinus Torvalds 47976039aa73SGustavo Padovan static void hci_sched_acl_pkt(struct hci_dev *hdev) 479863d2bc1bSAndrei Emeltchenko { 479963d2bc1bSAndrei Emeltchenko unsigned int cnt = hdev->acl_cnt; 480063d2bc1bSAndrei Emeltchenko struct hci_chan *chan; 480163d2bc1bSAndrei Emeltchenko struct sk_buff *skb; 480263d2bc1bSAndrei Emeltchenko int quote; 480363d2bc1bSAndrei Emeltchenko 480463d2bc1bSAndrei Emeltchenko __check_timeout(hdev, cnt); 480504837f64SMarcel Holtmann 480673d80debSLuiz Augusto von Dentz while (hdev->acl_cnt && 480773d80debSLuiz Augusto von Dentz (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { 4808ec1cce24SLuiz Augusto von Dentz u32 priority = (skb_peek(&chan->data_q))->priority; 4809ec1cce24SLuiz Augusto von Dentz while (quote-- && (skb = skb_peek(&chan->data_q))) { 481073d80debSLuiz Augusto von Dentz BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 481173d80debSLuiz Augusto von Dentz skb->len, skb->priority); 481273d80debSLuiz Augusto von Dentz 4813ec1cce24SLuiz Augusto von Dentz /* Stop if priority has changed */ 4814ec1cce24SLuiz Augusto von Dentz if (skb->priority < priority) 4815ec1cce24SLuiz Augusto von Dentz break; 4816ec1cce24SLuiz Augusto von Dentz 4817ec1cce24SLuiz Augusto von Dentz skb = skb_dequeue(&chan->data_q); 4818ec1cce24SLuiz Augusto von Dentz 481973d80debSLuiz Augusto von Dentz hci_conn_enter_active_mode(chan->conn, 482073d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active); 482104837f64SMarcel Holtmann 482257d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 48231da177e4SLinus Torvalds hdev->acl_last_tx = jiffies; 48241da177e4SLinus Torvalds 48251da177e4SLinus Torvalds hdev->acl_cnt--; 482673d80debSLuiz Augusto von Dentz chan->sent++; 482773d80debSLuiz Augusto von Dentz chan->conn->sent++; 48281da177e4SLinus Torvalds } 48291da177e4SLinus Torvalds } 483002b20f0bSLuiz Augusto von Dentz 483102b20f0bSLuiz Augusto von Dentz if (cnt != hdev->acl_cnt) 483202b20f0bSLuiz Augusto von Dentz hci_prio_recalculate(hdev, ACL_LINK); 48331da177e4SLinus Torvalds } 48341da177e4SLinus Torvalds 48356039aa73SGustavo Padovan static void hci_sched_acl_blk(struct hci_dev *hdev) 4836b71d385aSAndrei Emeltchenko { 483763d2bc1bSAndrei Emeltchenko unsigned int cnt = hdev->block_cnt; 4838b71d385aSAndrei Emeltchenko struct hci_chan *chan; 4839b71d385aSAndrei Emeltchenko struct sk_buff *skb; 4840b71d385aSAndrei Emeltchenko int quote; 4841bd1eb66bSAndrei Emeltchenko u8 type; 4842b71d385aSAndrei Emeltchenko 484363d2bc1bSAndrei Emeltchenko __check_timeout(hdev, cnt); 4844b71d385aSAndrei Emeltchenko 4845bd1eb66bSAndrei Emeltchenko BT_DBG("%s", hdev->name); 4846bd1eb66bSAndrei Emeltchenko 4847bd1eb66bSAndrei Emeltchenko if (hdev->dev_type == HCI_AMP) 4848bd1eb66bSAndrei Emeltchenko type = AMP_LINK; 4849bd1eb66bSAndrei Emeltchenko else 4850bd1eb66bSAndrei Emeltchenko type = ACL_LINK; 4851bd1eb66bSAndrei Emeltchenko 4852b71d385aSAndrei Emeltchenko while (hdev->block_cnt > 0 && 4853bd1eb66bSAndrei Emeltchenko (chan = hci_chan_sent(hdev, type, "e))) { 4854b71d385aSAndrei Emeltchenko u32 priority = (skb_peek(&chan->data_q))->priority; 4855b71d385aSAndrei Emeltchenko while (quote > 0 && (skb = skb_peek(&chan->data_q))) { 4856b71d385aSAndrei Emeltchenko int blocks; 4857b71d385aSAndrei Emeltchenko 4858b71d385aSAndrei Emeltchenko BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 4859b71d385aSAndrei Emeltchenko skb->len, skb->priority); 4860b71d385aSAndrei Emeltchenko 4861b71d385aSAndrei Emeltchenko /* Stop if priority has changed */ 4862b71d385aSAndrei Emeltchenko if (skb->priority < priority) 4863b71d385aSAndrei Emeltchenko break; 4864b71d385aSAndrei Emeltchenko 4865b71d385aSAndrei Emeltchenko skb = skb_dequeue(&chan->data_q); 4866b71d385aSAndrei Emeltchenko 4867b71d385aSAndrei Emeltchenko blocks = __get_blocks(hdev, skb); 4868b71d385aSAndrei Emeltchenko if (blocks > hdev->block_cnt) 4869b71d385aSAndrei Emeltchenko return; 4870b71d385aSAndrei Emeltchenko 4871b71d385aSAndrei Emeltchenko hci_conn_enter_active_mode(chan->conn, 4872b71d385aSAndrei Emeltchenko bt_cb(skb)->force_active); 4873b71d385aSAndrei Emeltchenko 487457d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 4875b71d385aSAndrei Emeltchenko hdev->acl_last_tx = jiffies; 4876b71d385aSAndrei Emeltchenko 4877b71d385aSAndrei Emeltchenko hdev->block_cnt -= blocks; 4878b71d385aSAndrei Emeltchenko quote -= blocks; 4879b71d385aSAndrei Emeltchenko 4880b71d385aSAndrei Emeltchenko chan->sent += blocks; 4881b71d385aSAndrei Emeltchenko chan->conn->sent += blocks; 4882b71d385aSAndrei Emeltchenko } 4883b71d385aSAndrei Emeltchenko } 4884b71d385aSAndrei Emeltchenko 4885b71d385aSAndrei Emeltchenko if (cnt != hdev->block_cnt) 4886bd1eb66bSAndrei Emeltchenko hci_prio_recalculate(hdev, type); 4887b71d385aSAndrei Emeltchenko } 4888b71d385aSAndrei Emeltchenko 48896039aa73SGustavo Padovan static void hci_sched_acl(struct hci_dev *hdev) 4890b71d385aSAndrei Emeltchenko { 4891b71d385aSAndrei Emeltchenko BT_DBG("%s", hdev->name); 4892b71d385aSAndrei Emeltchenko 4893bd1eb66bSAndrei Emeltchenko /* No ACL link over BR/EDR controller */ 4894bd1eb66bSAndrei Emeltchenko if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) 4895bd1eb66bSAndrei Emeltchenko return; 4896bd1eb66bSAndrei Emeltchenko 4897bd1eb66bSAndrei Emeltchenko /* No AMP link over AMP controller */ 4898bd1eb66bSAndrei Emeltchenko if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) 4899b71d385aSAndrei Emeltchenko return; 4900b71d385aSAndrei Emeltchenko 4901b71d385aSAndrei Emeltchenko switch (hdev->flow_ctl_mode) { 4902b71d385aSAndrei Emeltchenko case HCI_FLOW_CTL_MODE_PACKET_BASED: 4903b71d385aSAndrei Emeltchenko hci_sched_acl_pkt(hdev); 4904b71d385aSAndrei Emeltchenko break; 4905b71d385aSAndrei Emeltchenko 4906b71d385aSAndrei Emeltchenko case HCI_FLOW_CTL_MODE_BLOCK_BASED: 4907b71d385aSAndrei Emeltchenko hci_sched_acl_blk(hdev); 4908b71d385aSAndrei Emeltchenko break; 4909b71d385aSAndrei Emeltchenko } 4910b71d385aSAndrei Emeltchenko } 4911b71d385aSAndrei Emeltchenko 49121da177e4SLinus Torvalds /* Schedule SCO */ 49136039aa73SGustavo Padovan static void hci_sched_sco(struct hci_dev *hdev) 49141da177e4SLinus Torvalds { 49151da177e4SLinus Torvalds struct hci_conn *conn; 49161da177e4SLinus Torvalds struct sk_buff *skb; 49171da177e4SLinus Torvalds int quote; 49181da177e4SLinus Torvalds 49191da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 49201da177e4SLinus Torvalds 492152087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, SCO_LINK)) 492252087a79SLuiz Augusto von Dentz return; 492352087a79SLuiz Augusto von Dentz 49241da177e4SLinus Torvalds while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { 49251da177e4SLinus Torvalds while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 49261da177e4SLinus Torvalds BT_DBG("skb %p len %d", skb, skb->len); 492757d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 49281da177e4SLinus Torvalds 49291da177e4SLinus Torvalds conn->sent++; 49301da177e4SLinus Torvalds if (conn->sent == ~0) 49311da177e4SLinus Torvalds conn->sent = 0; 49321da177e4SLinus Torvalds } 49331da177e4SLinus Torvalds } 49341da177e4SLinus Torvalds } 49351da177e4SLinus Torvalds 49366039aa73SGustavo Padovan static void hci_sched_esco(struct hci_dev *hdev) 4937b6a0dc82SMarcel Holtmann { 4938b6a0dc82SMarcel Holtmann struct hci_conn *conn; 4939b6a0dc82SMarcel Holtmann struct sk_buff *skb; 4940b6a0dc82SMarcel Holtmann int quote; 4941b6a0dc82SMarcel Holtmann 4942b6a0dc82SMarcel Holtmann BT_DBG("%s", hdev->name); 4943b6a0dc82SMarcel Holtmann 494452087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, ESCO_LINK)) 494552087a79SLuiz Augusto von Dentz return; 494652087a79SLuiz Augusto von Dentz 49478fc9ced3SGustavo Padovan while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, 49488fc9ced3SGustavo Padovan "e))) { 4949b6a0dc82SMarcel Holtmann while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 4950b6a0dc82SMarcel Holtmann BT_DBG("skb %p len %d", skb, skb->len); 495157d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 4952b6a0dc82SMarcel Holtmann 4953b6a0dc82SMarcel Holtmann conn->sent++; 4954b6a0dc82SMarcel Holtmann if (conn->sent == ~0) 4955b6a0dc82SMarcel Holtmann conn->sent = 0; 4956b6a0dc82SMarcel Holtmann } 4957b6a0dc82SMarcel Holtmann } 4958b6a0dc82SMarcel Holtmann } 4959b6a0dc82SMarcel Holtmann 49606039aa73SGustavo Padovan static void hci_sched_le(struct hci_dev *hdev) 49616ed58ec5SVille Tervo { 496273d80debSLuiz Augusto von Dentz struct hci_chan *chan; 49636ed58ec5SVille Tervo struct sk_buff *skb; 496402b20f0bSLuiz Augusto von Dentz int quote, cnt, tmp; 49656ed58ec5SVille Tervo 49666ed58ec5SVille Tervo BT_DBG("%s", hdev->name); 49676ed58ec5SVille Tervo 496852087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, LE_LINK)) 496952087a79SLuiz Augusto von Dentz return; 497052087a79SLuiz Augusto von Dentz 49714a964404SMarcel Holtmann if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 49726ed58ec5SVille Tervo /* LE tx timeout must be longer than maximum 49736ed58ec5SVille Tervo * link supervision timeout (40.9 seconds) */ 4974bae1f5d9SVille Tervo if (!hdev->le_cnt && hdev->le_pkts && 49756ed58ec5SVille Tervo time_after(jiffies, hdev->le_last_tx + HZ * 45)) 4976bae1f5d9SVille Tervo hci_link_tx_to(hdev, LE_LINK); 49776ed58ec5SVille Tervo } 49786ed58ec5SVille Tervo 49796ed58ec5SVille Tervo cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; 498002b20f0bSLuiz Augusto von Dentz tmp = cnt; 498173d80debSLuiz Augusto von Dentz while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { 4982ec1cce24SLuiz Augusto von Dentz u32 priority = (skb_peek(&chan->data_q))->priority; 4983ec1cce24SLuiz Augusto von Dentz while (quote-- && (skb = skb_peek(&chan->data_q))) { 498473d80debSLuiz Augusto von Dentz BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 498573d80debSLuiz Augusto von Dentz skb->len, skb->priority); 49866ed58ec5SVille Tervo 4987ec1cce24SLuiz Augusto von Dentz /* Stop if priority has changed */ 4988ec1cce24SLuiz Augusto von Dentz if (skb->priority < priority) 4989ec1cce24SLuiz Augusto von Dentz break; 4990ec1cce24SLuiz Augusto von Dentz 4991ec1cce24SLuiz Augusto von Dentz skb = skb_dequeue(&chan->data_q); 4992ec1cce24SLuiz Augusto von Dentz 499357d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 49946ed58ec5SVille Tervo hdev->le_last_tx = jiffies; 49956ed58ec5SVille Tervo 49966ed58ec5SVille Tervo cnt--; 499773d80debSLuiz Augusto von Dentz chan->sent++; 499873d80debSLuiz Augusto von Dentz chan->conn->sent++; 49996ed58ec5SVille Tervo } 50006ed58ec5SVille Tervo } 500173d80debSLuiz Augusto von Dentz 50026ed58ec5SVille Tervo if (hdev->le_pkts) 50036ed58ec5SVille Tervo hdev->le_cnt = cnt; 50046ed58ec5SVille Tervo else 50056ed58ec5SVille Tervo hdev->acl_cnt = cnt; 500602b20f0bSLuiz Augusto von Dentz 500702b20f0bSLuiz Augusto von Dentz if (cnt != tmp) 500802b20f0bSLuiz Augusto von Dentz hci_prio_recalculate(hdev, LE_LINK); 50096ed58ec5SVille Tervo } 50106ed58ec5SVille Tervo 50113eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work) 50121da177e4SLinus Torvalds { 50133eff45eaSGustavo F. Padovan struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); 50141da177e4SLinus Torvalds struct sk_buff *skb; 50151da177e4SLinus Torvalds 50166ed58ec5SVille Tervo BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, 50176ed58ec5SVille Tervo hdev->sco_cnt, hdev->le_cnt); 50181da177e4SLinus Torvalds 501952de599eSMarcel Holtmann if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 50201da177e4SLinus Torvalds /* Schedule queues and send stuff to HCI driver */ 50211da177e4SLinus Torvalds hci_sched_acl(hdev); 50221da177e4SLinus Torvalds hci_sched_sco(hdev); 5023b6a0dc82SMarcel Holtmann hci_sched_esco(hdev); 50246ed58ec5SVille Tervo hci_sched_le(hdev); 502552de599eSMarcel Holtmann } 50266ed58ec5SVille Tervo 50271da177e4SLinus Torvalds /* Send next queued raw (unknown type) packet */ 50281da177e4SLinus Torvalds while ((skb = skb_dequeue(&hdev->raw_q))) 502957d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 50301da177e4SLinus Torvalds } 50311da177e4SLinus Torvalds 503225985edcSLucas De Marchi /* ----- HCI RX task (incoming data processing) ----- */ 50331da177e4SLinus Torvalds 50341da177e4SLinus Torvalds /* ACL data packet */ 50356039aa73SGustavo Padovan static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) 50361da177e4SLinus Torvalds { 50371da177e4SLinus Torvalds struct hci_acl_hdr *hdr = (void *) skb->data; 50381da177e4SLinus Torvalds struct hci_conn *conn; 50391da177e4SLinus Torvalds __u16 handle, flags; 50401da177e4SLinus Torvalds 50411da177e4SLinus Torvalds skb_pull(skb, HCI_ACL_HDR_SIZE); 50421da177e4SLinus Torvalds 50431da177e4SLinus Torvalds handle = __le16_to_cpu(hdr->handle); 50441da177e4SLinus Torvalds flags = hci_flags(handle); 50451da177e4SLinus Torvalds handle = hci_handle(handle); 50461da177e4SLinus Torvalds 5047f0e09510SAndrei Emeltchenko BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len, 5048a8c5fb1aSGustavo Padovan handle, flags); 50491da177e4SLinus Torvalds 50501da177e4SLinus Torvalds hdev->stat.acl_rx++; 50511da177e4SLinus Torvalds 50521da177e4SLinus Torvalds hci_dev_lock(hdev); 50531da177e4SLinus Torvalds conn = hci_conn_hash_lookup_handle(hdev, handle); 50541da177e4SLinus Torvalds hci_dev_unlock(hdev); 50551da177e4SLinus Torvalds 50561da177e4SLinus Torvalds if (conn) { 505765983fc7SMat Martineau hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); 505804837f64SMarcel Holtmann 50591da177e4SLinus Torvalds /* Send to upper protocol */ 5060686ebf28SUlisses Furquim l2cap_recv_acldata(conn, skb, flags); 50611da177e4SLinus Torvalds return; 50621da177e4SLinus Torvalds } else { 50631da177e4SLinus Torvalds BT_ERR("%s ACL packet for unknown connection handle %d", 50641da177e4SLinus Torvalds hdev->name, handle); 50651da177e4SLinus Torvalds } 50661da177e4SLinus Torvalds 50671da177e4SLinus Torvalds kfree_skb(skb); 50681da177e4SLinus Torvalds } 50691da177e4SLinus Torvalds 50701da177e4SLinus Torvalds /* SCO data packet */ 50716039aa73SGustavo Padovan static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) 50721da177e4SLinus Torvalds { 50731da177e4SLinus Torvalds struct hci_sco_hdr *hdr = (void *) skb->data; 50741da177e4SLinus Torvalds struct hci_conn *conn; 50751da177e4SLinus Torvalds __u16 handle; 50761da177e4SLinus Torvalds 50771da177e4SLinus Torvalds skb_pull(skb, HCI_SCO_HDR_SIZE); 50781da177e4SLinus Torvalds 50791da177e4SLinus Torvalds handle = __le16_to_cpu(hdr->handle); 50801da177e4SLinus Torvalds 5081f0e09510SAndrei Emeltchenko BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle); 50821da177e4SLinus Torvalds 50831da177e4SLinus Torvalds hdev->stat.sco_rx++; 50841da177e4SLinus Torvalds 50851da177e4SLinus Torvalds hci_dev_lock(hdev); 50861da177e4SLinus Torvalds conn = hci_conn_hash_lookup_handle(hdev, handle); 50871da177e4SLinus Torvalds hci_dev_unlock(hdev); 50881da177e4SLinus Torvalds 50891da177e4SLinus Torvalds if (conn) { 50901da177e4SLinus Torvalds /* Send to upper protocol */ 5091686ebf28SUlisses Furquim sco_recv_scodata(conn, skb); 50921da177e4SLinus Torvalds return; 50931da177e4SLinus Torvalds } else { 50941da177e4SLinus Torvalds BT_ERR("%s SCO packet for unknown connection handle %d", 50951da177e4SLinus Torvalds hdev->name, handle); 50961da177e4SLinus Torvalds } 50971da177e4SLinus Torvalds 50981da177e4SLinus Torvalds kfree_skb(skb); 50991da177e4SLinus Torvalds } 51001da177e4SLinus Torvalds 51019238f36aSJohan Hedberg static bool hci_req_is_complete(struct hci_dev *hdev) 51029238f36aSJohan Hedberg { 51039238f36aSJohan Hedberg struct sk_buff *skb; 51049238f36aSJohan Hedberg 51059238f36aSJohan Hedberg skb = skb_peek(&hdev->cmd_q); 51069238f36aSJohan Hedberg if (!skb) 51079238f36aSJohan Hedberg return true; 51089238f36aSJohan Hedberg 51099238f36aSJohan Hedberg return bt_cb(skb)->req.start; 51109238f36aSJohan Hedberg } 51119238f36aSJohan Hedberg 511242c6b129SJohan Hedberg static void hci_resend_last(struct hci_dev *hdev) 511342c6b129SJohan Hedberg { 511442c6b129SJohan Hedberg struct hci_command_hdr *sent; 511542c6b129SJohan Hedberg struct sk_buff *skb; 511642c6b129SJohan Hedberg u16 opcode; 511742c6b129SJohan Hedberg 511842c6b129SJohan Hedberg if (!hdev->sent_cmd) 511942c6b129SJohan Hedberg return; 512042c6b129SJohan Hedberg 512142c6b129SJohan Hedberg sent = (void *) hdev->sent_cmd->data; 512242c6b129SJohan Hedberg opcode = __le16_to_cpu(sent->opcode); 512342c6b129SJohan Hedberg if (opcode == HCI_OP_RESET) 512442c6b129SJohan Hedberg return; 512542c6b129SJohan Hedberg 512642c6b129SJohan Hedberg skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); 512742c6b129SJohan Hedberg if (!skb) 512842c6b129SJohan Hedberg return; 512942c6b129SJohan Hedberg 513042c6b129SJohan Hedberg skb_queue_head(&hdev->cmd_q, skb); 513142c6b129SJohan Hedberg queue_work(hdev->workqueue, &hdev->cmd_work); 513242c6b129SJohan Hedberg } 513342c6b129SJohan Hedberg 51349238f36aSJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status) 51359238f36aSJohan Hedberg { 51369238f36aSJohan Hedberg hci_req_complete_t req_complete = NULL; 51379238f36aSJohan Hedberg struct sk_buff *skb; 51389238f36aSJohan Hedberg unsigned long flags; 51399238f36aSJohan Hedberg 51409238f36aSJohan Hedberg BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); 51419238f36aSJohan Hedberg 514242c6b129SJohan Hedberg /* If the completed command doesn't match the last one that was 514342c6b129SJohan Hedberg * sent we need to do special handling of it. 51449238f36aSJohan Hedberg */ 514542c6b129SJohan Hedberg if (!hci_sent_cmd_data(hdev, opcode)) { 514642c6b129SJohan Hedberg /* Some CSR based controllers generate a spontaneous 514742c6b129SJohan Hedberg * reset complete event during init and any pending 514842c6b129SJohan Hedberg * command will never be completed. In such a case we 514942c6b129SJohan Hedberg * need to resend whatever was the last sent 515042c6b129SJohan Hedberg * command. 515142c6b129SJohan Hedberg */ 515242c6b129SJohan Hedberg if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) 515342c6b129SJohan Hedberg hci_resend_last(hdev); 515442c6b129SJohan Hedberg 51559238f36aSJohan Hedberg return; 515642c6b129SJohan Hedberg } 51579238f36aSJohan Hedberg 51589238f36aSJohan Hedberg /* If the command succeeded and there's still more commands in 51599238f36aSJohan Hedberg * this request the request is not yet complete. 51609238f36aSJohan Hedberg */ 51619238f36aSJohan Hedberg if (!status && !hci_req_is_complete(hdev)) 51629238f36aSJohan Hedberg return; 51639238f36aSJohan Hedberg 51649238f36aSJohan Hedberg /* If this was the last command in a request the complete 51659238f36aSJohan Hedberg * callback would be found in hdev->sent_cmd instead of the 51669238f36aSJohan Hedberg * command queue (hdev->cmd_q). 51679238f36aSJohan Hedberg */ 51689238f36aSJohan Hedberg if (hdev->sent_cmd) { 51699238f36aSJohan Hedberg req_complete = bt_cb(hdev->sent_cmd)->req.complete; 517053e21fbcSJohan Hedberg 517153e21fbcSJohan Hedberg if (req_complete) { 517253e21fbcSJohan Hedberg /* We must set the complete callback to NULL to 517353e21fbcSJohan Hedberg * avoid calling the callback more than once if 517453e21fbcSJohan Hedberg * this function gets called again. 517553e21fbcSJohan Hedberg */ 517653e21fbcSJohan Hedberg bt_cb(hdev->sent_cmd)->req.complete = NULL; 517753e21fbcSJohan Hedberg 51789238f36aSJohan Hedberg goto call_complete; 51799238f36aSJohan Hedberg } 518053e21fbcSJohan Hedberg } 51819238f36aSJohan Hedberg 51829238f36aSJohan Hedberg /* Remove all pending commands belonging to this request */ 51839238f36aSJohan Hedberg spin_lock_irqsave(&hdev->cmd_q.lock, flags); 51849238f36aSJohan Hedberg while ((skb = __skb_dequeue(&hdev->cmd_q))) { 51859238f36aSJohan Hedberg if (bt_cb(skb)->req.start) { 51869238f36aSJohan Hedberg __skb_queue_head(&hdev->cmd_q, skb); 51879238f36aSJohan Hedberg break; 51889238f36aSJohan Hedberg } 51899238f36aSJohan Hedberg 51909238f36aSJohan Hedberg req_complete = bt_cb(skb)->req.complete; 51919238f36aSJohan Hedberg kfree_skb(skb); 51929238f36aSJohan Hedberg } 51939238f36aSJohan Hedberg spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); 51949238f36aSJohan Hedberg 51959238f36aSJohan Hedberg call_complete: 51969238f36aSJohan Hedberg if (req_complete) 51979238f36aSJohan Hedberg req_complete(hdev, status); 51989238f36aSJohan Hedberg } 51999238f36aSJohan Hedberg 5200b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work) 52011da177e4SLinus Torvalds { 5202b78752ccSMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); 52031da177e4SLinus Torvalds struct sk_buff *skb; 52041da177e4SLinus Torvalds 52051da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 52061da177e4SLinus Torvalds 52071da177e4SLinus Torvalds while ((skb = skb_dequeue(&hdev->rx_q))) { 5208cd82e61cSMarcel Holtmann /* Send copy to monitor */ 5209cd82e61cSMarcel Holtmann hci_send_to_monitor(hdev, skb); 5210cd82e61cSMarcel Holtmann 52111da177e4SLinus Torvalds if (atomic_read(&hdev->promisc)) { 52121da177e4SLinus Torvalds /* Send copy to the sockets */ 5213470fe1b5SMarcel Holtmann hci_send_to_sock(hdev, skb); 52141da177e4SLinus Torvalds } 52151da177e4SLinus Torvalds 5216fee746b0SMarcel Holtmann if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 52171da177e4SLinus Torvalds kfree_skb(skb); 52181da177e4SLinus Torvalds continue; 52191da177e4SLinus Torvalds } 52201da177e4SLinus Torvalds 52211da177e4SLinus Torvalds if (test_bit(HCI_INIT, &hdev->flags)) { 52221da177e4SLinus Torvalds /* Don't process data packets in this states. */ 52230d48d939SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 52241da177e4SLinus Torvalds case HCI_ACLDATA_PKT: 52251da177e4SLinus Torvalds case HCI_SCODATA_PKT: 52261da177e4SLinus Torvalds kfree_skb(skb); 52271da177e4SLinus Torvalds continue; 52283ff50b79SStephen Hemminger } 52291da177e4SLinus Torvalds } 52301da177e4SLinus Torvalds 52311da177e4SLinus Torvalds /* Process frame */ 52320d48d939SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 52331da177e4SLinus Torvalds case HCI_EVENT_PKT: 5234b78752ccSMarcel Holtmann BT_DBG("%s Event packet", hdev->name); 52351da177e4SLinus Torvalds hci_event_packet(hdev, skb); 52361da177e4SLinus Torvalds break; 52371da177e4SLinus Torvalds 52381da177e4SLinus Torvalds case HCI_ACLDATA_PKT: 52391da177e4SLinus Torvalds BT_DBG("%s ACL data packet", hdev->name); 52401da177e4SLinus Torvalds hci_acldata_packet(hdev, skb); 52411da177e4SLinus Torvalds break; 52421da177e4SLinus Torvalds 52431da177e4SLinus Torvalds case HCI_SCODATA_PKT: 52441da177e4SLinus Torvalds BT_DBG("%s SCO data packet", hdev->name); 52451da177e4SLinus Torvalds hci_scodata_packet(hdev, skb); 52461da177e4SLinus Torvalds break; 52471da177e4SLinus Torvalds 52481da177e4SLinus Torvalds default: 52491da177e4SLinus Torvalds kfree_skb(skb); 52501da177e4SLinus Torvalds break; 52511da177e4SLinus Torvalds } 52521da177e4SLinus Torvalds } 52531da177e4SLinus Torvalds } 52541da177e4SLinus Torvalds 5255c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work) 52561da177e4SLinus Torvalds { 5257c347b765SGustavo F. Padovan struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); 52581da177e4SLinus Torvalds struct sk_buff *skb; 52591da177e4SLinus Torvalds 52602104786bSAndrei Emeltchenko BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, 52612104786bSAndrei Emeltchenko atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); 52621da177e4SLinus Torvalds 52631da177e4SLinus Torvalds /* Send queued commands */ 52645a08ecceSAndrei Emeltchenko if (atomic_read(&hdev->cmd_cnt)) { 52655a08ecceSAndrei Emeltchenko skb = skb_dequeue(&hdev->cmd_q); 52665a08ecceSAndrei Emeltchenko if (!skb) 52675a08ecceSAndrei Emeltchenko return; 52685a08ecceSAndrei Emeltchenko 52691da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 52701da177e4SLinus Torvalds 5271a675d7f1SMarcel Holtmann hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); 527270f23020SAndrei Emeltchenko if (hdev->sent_cmd) { 52731da177e4SLinus Torvalds atomic_dec(&hdev->cmd_cnt); 527457d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 52757bdb8a5cSSzymon Janc if (test_bit(HCI_RESET, &hdev->flags)) 527665cc2b49SMarcel Holtmann cancel_delayed_work(&hdev->cmd_timer); 52777bdb8a5cSSzymon Janc else 527865cc2b49SMarcel Holtmann schedule_delayed_work(&hdev->cmd_timer, 527965cc2b49SMarcel Holtmann HCI_CMD_TIMEOUT); 52801da177e4SLinus Torvalds } else { 52811da177e4SLinus Torvalds skb_queue_head(&hdev->cmd_q, skb); 5282c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 52831da177e4SLinus Torvalds } 52841da177e4SLinus Torvalds } 52851da177e4SLinus Torvalds } 5286b1efcc28SAndre Guedes 5287b1efcc28SAndre Guedes void hci_req_add_le_scan_disable(struct hci_request *req) 5288b1efcc28SAndre Guedes { 5289b1efcc28SAndre Guedes struct hci_cp_le_set_scan_enable cp; 5290b1efcc28SAndre Guedes 5291b1efcc28SAndre Guedes memset(&cp, 0, sizeof(cp)); 5292b1efcc28SAndre Guedes cp.enable = LE_SCAN_DISABLE; 5293b1efcc28SAndre Guedes hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); 5294b1efcc28SAndre Guedes } 5295a4790dbdSAndre Guedes 52968ef30fd3SAndre Guedes void hci_req_add_le_passive_scan(struct hci_request *req) 52978ef30fd3SAndre Guedes { 52988ef30fd3SAndre Guedes struct hci_cp_le_set_scan_param param_cp; 52998ef30fd3SAndre Guedes struct hci_cp_le_set_scan_enable enable_cp; 53008ef30fd3SAndre Guedes struct hci_dev *hdev = req->hdev; 53018ef30fd3SAndre Guedes u8 own_addr_type; 53028ef30fd3SAndre Guedes 53036ab535a7SMarcel Holtmann /* Set require_privacy to false since no SCAN_REQ are send 53046ab535a7SMarcel Holtmann * during passive scanning. Not using an unresolvable address 53056ab535a7SMarcel Holtmann * here is important so that peer devices using direct 53066ab535a7SMarcel Holtmann * advertising with our address will be correctly reported 53076ab535a7SMarcel Holtmann * by the controller. 53088ef30fd3SAndre Guedes */ 53096ab535a7SMarcel Holtmann if (hci_update_random_address(req, false, &own_addr_type)) 53108ef30fd3SAndre Guedes return; 53118ef30fd3SAndre Guedes 53128ef30fd3SAndre Guedes memset(¶m_cp, 0, sizeof(param_cp)); 53138ef30fd3SAndre Guedes param_cp.type = LE_SCAN_PASSIVE; 53148ef30fd3SAndre Guedes param_cp.interval = cpu_to_le16(hdev->le_scan_interval); 53158ef30fd3SAndre Guedes param_cp.window = cpu_to_le16(hdev->le_scan_window); 53168ef30fd3SAndre Guedes param_cp.own_address_type = own_addr_type; 53178ef30fd3SAndre Guedes hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), 53188ef30fd3SAndre Guedes ¶m_cp); 53198ef30fd3SAndre Guedes 53208ef30fd3SAndre Guedes memset(&enable_cp, 0, sizeof(enable_cp)); 53218ef30fd3SAndre Guedes enable_cp.enable = LE_SCAN_ENABLE; 53224340a124SAndre Guedes enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; 53238ef30fd3SAndre Guedes hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), 53248ef30fd3SAndre Guedes &enable_cp); 53258ef30fd3SAndre Guedes } 53268ef30fd3SAndre Guedes 5327a4790dbdSAndre Guedes static void update_background_scan_complete(struct hci_dev *hdev, u8 status) 5328a4790dbdSAndre Guedes { 5329a4790dbdSAndre Guedes if (status) 5330a4790dbdSAndre Guedes BT_DBG("HCI request failed to update background scanning: " 5331a4790dbdSAndre Guedes "status 0x%2.2x", status); 5332a4790dbdSAndre Guedes } 5333a4790dbdSAndre Guedes 5334a4790dbdSAndre Guedes /* This function controls the background scanning based on hdev->pend_le_conns 5335a4790dbdSAndre Guedes * list. If there are pending LE connection we start the background scanning, 5336a4790dbdSAndre Guedes * otherwise we stop it. 5337a4790dbdSAndre Guedes * 5338a4790dbdSAndre Guedes * This function requires the caller holds hdev->lock. 5339a4790dbdSAndre Guedes */ 5340a4790dbdSAndre Guedes void hci_update_background_scan(struct hci_dev *hdev) 5341a4790dbdSAndre Guedes { 5342a4790dbdSAndre Guedes struct hci_request req; 5343a4790dbdSAndre Guedes struct hci_conn *conn; 5344a4790dbdSAndre Guedes int err; 5345a4790dbdSAndre Guedes 5346c20c02d5SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags) || 5347c20c02d5SMarcel Holtmann test_bit(HCI_INIT, &hdev->flags) || 5348c20c02d5SMarcel Holtmann test_bit(HCI_SETUP, &hdev->dev_flags) || 5349b8221770SMarcel Holtmann test_bit(HCI_AUTO_OFF, &hdev->dev_flags) || 5350c20c02d5SMarcel Holtmann test_bit(HCI_UNREGISTER, &hdev->dev_flags)) 53511c1697c0SMarcel Holtmann return; 53521c1697c0SMarcel Holtmann 5353a4790dbdSAndre Guedes hci_req_init(&req, hdev); 5354a4790dbdSAndre Guedes 535566f8455aSJohan Hedberg if (list_empty(&hdev->pend_le_conns) && 535666f8455aSJohan Hedberg list_empty(&hdev->pend_le_reports)) { 53570d2bf134SJohan Hedberg /* If there is no pending LE connections or devices 53580d2bf134SJohan Hedberg * to be scanned for, we should stop the background 53590d2bf134SJohan Hedberg * scanning. 5360a4790dbdSAndre Guedes */ 5361a4790dbdSAndre Guedes 5362a4790dbdSAndre Guedes /* If controller is not scanning we are done. */ 5363a4790dbdSAndre Guedes if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags)) 5364a4790dbdSAndre Guedes return; 5365a4790dbdSAndre Guedes 5366a4790dbdSAndre Guedes hci_req_add_le_scan_disable(&req); 5367a4790dbdSAndre Guedes 5368a4790dbdSAndre Guedes BT_DBG("%s stopping background scanning", hdev->name); 5369a4790dbdSAndre Guedes } else { 5370a4790dbdSAndre Guedes /* If there is at least one pending LE connection, we should 5371a4790dbdSAndre Guedes * keep the background scan running. 5372a4790dbdSAndre Guedes */ 5373a4790dbdSAndre Guedes 5374a4790dbdSAndre Guedes /* If controller is connecting, we should not start scanning 5375a4790dbdSAndre Guedes * since some controllers are not able to scan and connect at 5376a4790dbdSAndre Guedes * the same time. 5377a4790dbdSAndre Guedes */ 5378a4790dbdSAndre Guedes conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); 5379a4790dbdSAndre Guedes if (conn) 5380a4790dbdSAndre Guedes return; 5381a4790dbdSAndre Guedes 53824340a124SAndre Guedes /* If controller is currently scanning, we stop it to ensure we 53834340a124SAndre Guedes * don't miss any advertising (due to duplicates filter). 53844340a124SAndre Guedes */ 53854340a124SAndre Guedes if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) 53864340a124SAndre Guedes hci_req_add_le_scan_disable(&req); 53874340a124SAndre Guedes 53888ef30fd3SAndre Guedes hci_req_add_le_passive_scan(&req); 5389a4790dbdSAndre Guedes 5390a4790dbdSAndre Guedes BT_DBG("%s starting background scanning", hdev->name); 5391a4790dbdSAndre Guedes } 5392a4790dbdSAndre Guedes 5393a4790dbdSAndre Guedes err = hci_req_run(&req, update_background_scan_complete); 5394a4790dbdSAndre Guedes if (err) 5395a4790dbdSAndre Guedes BT_ERR("Failed to run HCI request: err %d", err); 5396a4790dbdSAndre Guedes } 5397