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 400857dd3bSJohan Hedberg #include "hci_request.h" 4160c5f5fbSMarcel Holtmann #include "hci_debugfs.h" 42970c4e46SJohan Hedberg #include "smp.h" 43970c4e46SJohan Hedberg 44b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work); 45c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work); 463eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work); 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* HCI device list */ 491da177e4SLinus Torvalds LIST_HEAD(hci_dev_list); 501da177e4SLinus Torvalds DEFINE_RWLOCK(hci_dev_list_lock); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* HCI callback list */ 531da177e4SLinus Torvalds LIST_HEAD(hci_cb_list); 54fba7ecf0SJohan Hedberg DEFINE_MUTEX(hci_cb_list_lock); 551da177e4SLinus Torvalds 563df92b31SSasha Levin /* HCI ID Numbering */ 573df92b31SSasha Levin static DEFINE_IDA(hci_index_ida); 583df92b31SSasha Levin 59899de765SMarcel Holtmann /* ----- HCI requests ----- */ 60899de765SMarcel Holtmann 61899de765SMarcel Holtmann #define HCI_REQ_DONE 0 62899de765SMarcel Holtmann #define HCI_REQ_PEND 1 63899de765SMarcel Holtmann #define HCI_REQ_CANCELED 2 64899de765SMarcel Holtmann 65899de765SMarcel Holtmann #define hci_req_lock(d) mutex_lock(&d->req_lock) 66899de765SMarcel Holtmann #define hci_req_unlock(d) mutex_unlock(&d->req_lock) 67899de765SMarcel Holtmann 681da177e4SLinus Torvalds /* ---- HCI notifications ---- */ 691da177e4SLinus Torvalds 706516455dSMarcel Holtmann static void hci_notify(struct hci_dev *hdev, int event) 711da177e4SLinus Torvalds { 72040030efSMarcel Holtmann hci_sock_dev_event(hdev, event); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 75baf27f6eSMarcel Holtmann /* ---- HCI debugfs entries ---- */ 76baf27f6eSMarcel Holtmann 774b4148e9SMarcel Holtmann static ssize_t dut_mode_read(struct file *file, char __user *user_buf, 784b4148e9SMarcel Holtmann size_t count, loff_t *ppos) 794b4148e9SMarcel Holtmann { 804b4148e9SMarcel Holtmann struct hci_dev *hdev = file->private_data; 814b4148e9SMarcel Holtmann char buf[3]; 824b4148e9SMarcel Holtmann 83b7cb93e5SMarcel Holtmann buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y': 'N'; 844b4148e9SMarcel Holtmann buf[1] = '\n'; 854b4148e9SMarcel Holtmann buf[2] = '\0'; 864b4148e9SMarcel Holtmann return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 874b4148e9SMarcel Holtmann } 884b4148e9SMarcel Holtmann 894b4148e9SMarcel Holtmann static ssize_t dut_mode_write(struct file *file, const char __user *user_buf, 904b4148e9SMarcel Holtmann size_t count, loff_t *ppos) 914b4148e9SMarcel Holtmann { 924b4148e9SMarcel Holtmann struct hci_dev *hdev = file->private_data; 934b4148e9SMarcel Holtmann struct sk_buff *skb; 944b4148e9SMarcel Holtmann char buf[32]; 954b4148e9SMarcel Holtmann size_t buf_size = min(count, (sizeof(buf)-1)); 964b4148e9SMarcel Holtmann bool enable; 974b4148e9SMarcel Holtmann 984b4148e9SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) 994b4148e9SMarcel Holtmann return -ENETDOWN; 1004b4148e9SMarcel Holtmann 1014b4148e9SMarcel Holtmann if (copy_from_user(buf, user_buf, buf_size)) 1024b4148e9SMarcel Holtmann return -EFAULT; 1034b4148e9SMarcel Holtmann 1044b4148e9SMarcel Holtmann buf[buf_size] = '\0'; 1054b4148e9SMarcel Holtmann if (strtobool(buf, &enable)) 1064b4148e9SMarcel Holtmann return -EINVAL; 1074b4148e9SMarcel Holtmann 108b7cb93e5SMarcel Holtmann if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE)) 1094b4148e9SMarcel Holtmann return -EALREADY; 1104b4148e9SMarcel Holtmann 1114b4148e9SMarcel Holtmann hci_req_lock(hdev); 1124b4148e9SMarcel Holtmann if (enable) 1134b4148e9SMarcel Holtmann skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL, 1144b4148e9SMarcel Holtmann HCI_CMD_TIMEOUT); 1154b4148e9SMarcel Holtmann else 1164b4148e9SMarcel Holtmann skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, 1174b4148e9SMarcel Holtmann HCI_CMD_TIMEOUT); 1184b4148e9SMarcel Holtmann hci_req_unlock(hdev); 1194b4148e9SMarcel Holtmann 1204b4148e9SMarcel Holtmann if (IS_ERR(skb)) 1214b4148e9SMarcel Holtmann return PTR_ERR(skb); 1224b4148e9SMarcel Holtmann 1234b4148e9SMarcel Holtmann kfree_skb(skb); 1244b4148e9SMarcel Holtmann 125b7cb93e5SMarcel Holtmann hci_dev_change_flag(hdev, HCI_DUT_MODE); 1264b4148e9SMarcel Holtmann 1274b4148e9SMarcel Holtmann return count; 1284b4148e9SMarcel Holtmann } 1294b4148e9SMarcel Holtmann 1304b4148e9SMarcel Holtmann static const struct file_operations dut_mode_fops = { 1314b4148e9SMarcel Holtmann .open = simple_open, 1324b4148e9SMarcel Holtmann .read = dut_mode_read, 1334b4148e9SMarcel Holtmann .write = dut_mode_write, 1344b4148e9SMarcel Holtmann .llseek = default_llseek, 1354b4148e9SMarcel Holtmann }; 1364b4148e9SMarcel Holtmann 1374b4113d6SMarcel Holtmann static ssize_t vendor_diag_read(struct file *file, char __user *user_buf, 1384b4113d6SMarcel Holtmann size_t count, loff_t *ppos) 1394b4113d6SMarcel Holtmann { 1404b4113d6SMarcel Holtmann struct hci_dev *hdev = file->private_data; 1414b4113d6SMarcel Holtmann char buf[3]; 1424b4113d6SMarcel Holtmann 1434b4113d6SMarcel Holtmann buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y': 'N'; 1444b4113d6SMarcel Holtmann buf[1] = '\n'; 1454b4113d6SMarcel Holtmann buf[2] = '\0'; 1464b4113d6SMarcel Holtmann return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 1474b4113d6SMarcel Holtmann } 1484b4113d6SMarcel Holtmann 1494b4113d6SMarcel Holtmann static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf, 1504b4113d6SMarcel Holtmann size_t count, loff_t *ppos) 1514b4113d6SMarcel Holtmann { 1524b4113d6SMarcel Holtmann struct hci_dev *hdev = file->private_data; 1534b4113d6SMarcel Holtmann char buf[32]; 1544b4113d6SMarcel Holtmann size_t buf_size = min(count, (sizeof(buf)-1)); 1554b4113d6SMarcel Holtmann bool enable; 1564b4113d6SMarcel Holtmann int err; 1574b4113d6SMarcel Holtmann 1584b4113d6SMarcel Holtmann if (copy_from_user(buf, user_buf, buf_size)) 1594b4113d6SMarcel Holtmann return -EFAULT; 1604b4113d6SMarcel Holtmann 1614b4113d6SMarcel Holtmann buf[buf_size] = '\0'; 1624b4113d6SMarcel Holtmann if (strtobool(buf, &enable)) 1634b4113d6SMarcel Holtmann return -EINVAL; 1644b4113d6SMarcel Holtmann 1657e995b9eSMarcel Holtmann /* When the diagnostic flags are not persistent and the transport 1667e995b9eSMarcel Holtmann * is not active, then there is no need for the vendor callback. 1677e995b9eSMarcel Holtmann * 1687e995b9eSMarcel Holtmann * Instead just store the desired value. If needed the setting 1697e995b9eSMarcel Holtmann * will be programmed when the controller gets powered on. 1707e995b9eSMarcel Holtmann */ 1717e995b9eSMarcel Holtmann if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && 1727e995b9eSMarcel Holtmann !test_bit(HCI_RUNNING, &hdev->flags)) 1737e995b9eSMarcel Holtmann goto done; 1747e995b9eSMarcel Holtmann 1754b4113d6SMarcel Holtmann hci_req_lock(hdev); 1764b4113d6SMarcel Holtmann err = hdev->set_diag(hdev, enable); 1774b4113d6SMarcel Holtmann hci_req_unlock(hdev); 1784b4113d6SMarcel Holtmann 1794b4113d6SMarcel Holtmann if (err < 0) 1804b4113d6SMarcel Holtmann return err; 1814b4113d6SMarcel Holtmann 1827e995b9eSMarcel Holtmann done: 1834b4113d6SMarcel Holtmann if (enable) 1844b4113d6SMarcel Holtmann hci_dev_set_flag(hdev, HCI_VENDOR_DIAG); 1854b4113d6SMarcel Holtmann else 1864b4113d6SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG); 1874b4113d6SMarcel Holtmann 1884b4113d6SMarcel Holtmann return count; 1894b4113d6SMarcel Holtmann } 1904b4113d6SMarcel Holtmann 1914b4113d6SMarcel Holtmann static const struct file_operations vendor_diag_fops = { 1924b4113d6SMarcel Holtmann .open = simple_open, 1934b4113d6SMarcel Holtmann .read = vendor_diag_read, 1944b4113d6SMarcel Holtmann .write = vendor_diag_write, 1954b4113d6SMarcel Holtmann .llseek = default_llseek, 1964b4113d6SMarcel Holtmann }; 1974b4113d6SMarcel Holtmann 198f640ee98SMarcel Holtmann static void hci_debugfs_create_basic(struct hci_dev *hdev) 199f640ee98SMarcel Holtmann { 200f640ee98SMarcel Holtmann debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev, 201f640ee98SMarcel Holtmann &dut_mode_fops); 202f640ee98SMarcel Holtmann 203f640ee98SMarcel Holtmann if (hdev->set_diag) 204f640ee98SMarcel Holtmann debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev, 205f640ee98SMarcel Holtmann &vendor_diag_fops); 206f640ee98SMarcel Holtmann } 207f640ee98SMarcel Holtmann 2081da177e4SLinus Torvalds /* ---- HCI requests ---- */ 2091da177e4SLinus Torvalds 210f60cb305SJohan Hedberg static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, 211f60cb305SJohan Hedberg struct sk_buff *skb) 2121da177e4SLinus Torvalds { 21342c6b129SJohan Hedberg BT_DBG("%s result 0x%2.2x", hdev->name, result); 21475fb0e32SJohan Hedberg 2151da177e4SLinus Torvalds if (hdev->req_status == HCI_REQ_PEND) { 2161da177e4SLinus Torvalds hdev->req_result = result; 2171da177e4SLinus Torvalds hdev->req_status = HCI_REQ_DONE; 218f60cb305SJohan Hedberg if (skb) 219f60cb305SJohan Hedberg hdev->req_skb = skb_get(skb); 2201da177e4SLinus Torvalds wake_up_interruptible(&hdev->req_wait_q); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds static void hci_req_cancel(struct hci_dev *hdev, int err) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds BT_DBG("%s err 0x%2.2x", hdev->name, err); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds if (hdev->req_status == HCI_REQ_PEND) { 2291da177e4SLinus Torvalds hdev->req_result = err; 2301da177e4SLinus Torvalds hdev->req_status = HCI_REQ_CANCELED; 2311da177e4SLinus Torvalds wake_up_interruptible(&hdev->req_wait_q); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2357b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, 23607dc93ddSJohan Hedberg const void *param, u8 event, u32 timeout) 23775e84b7cSJohan Hedberg { 23875e84b7cSJohan Hedberg DECLARE_WAITQUEUE(wait, current); 23975e84b7cSJohan Hedberg struct hci_request req; 240f60cb305SJohan Hedberg struct sk_buff *skb; 24175e84b7cSJohan Hedberg int err = 0; 24275e84b7cSJohan Hedberg 24375e84b7cSJohan Hedberg BT_DBG("%s", hdev->name); 24475e84b7cSJohan Hedberg 24575e84b7cSJohan Hedberg hci_req_init(&req, hdev); 24675e84b7cSJohan Hedberg 2477b1abbbeSJohan Hedberg hci_req_add_ev(&req, opcode, plen, param, event); 24875e84b7cSJohan Hedberg 24975e84b7cSJohan Hedberg hdev->req_status = HCI_REQ_PEND; 25075e84b7cSJohan Hedberg 25175e84b7cSJohan Hedberg add_wait_queue(&hdev->req_wait_q, &wait); 25275e84b7cSJohan Hedberg set_current_state(TASK_INTERRUPTIBLE); 25375e84b7cSJohan Hedberg 254f60cb305SJohan Hedberg err = hci_req_run_skb(&req, hci_req_sync_complete); 255039fada5SChan-yeol Park if (err < 0) { 256039fada5SChan-yeol Park remove_wait_queue(&hdev->req_wait_q, &wait); 25722a3ceabSJohan Hedberg set_current_state(TASK_RUNNING); 258039fada5SChan-yeol Park return ERR_PTR(err); 259039fada5SChan-yeol Park } 260039fada5SChan-yeol Park 26175e84b7cSJohan Hedberg schedule_timeout(timeout); 26275e84b7cSJohan Hedberg 26375e84b7cSJohan Hedberg remove_wait_queue(&hdev->req_wait_q, &wait); 26475e84b7cSJohan Hedberg 26575e84b7cSJohan Hedberg if (signal_pending(current)) 26675e84b7cSJohan Hedberg return ERR_PTR(-EINTR); 26775e84b7cSJohan Hedberg 26875e84b7cSJohan Hedberg switch (hdev->req_status) { 26975e84b7cSJohan Hedberg case HCI_REQ_DONE: 27075e84b7cSJohan Hedberg err = -bt_to_errno(hdev->req_result); 27175e84b7cSJohan Hedberg break; 27275e84b7cSJohan Hedberg 27375e84b7cSJohan Hedberg case HCI_REQ_CANCELED: 27475e84b7cSJohan Hedberg err = -hdev->req_result; 27575e84b7cSJohan Hedberg break; 27675e84b7cSJohan Hedberg 27775e84b7cSJohan Hedberg default: 27875e84b7cSJohan Hedberg err = -ETIMEDOUT; 27975e84b7cSJohan Hedberg break; 28075e84b7cSJohan Hedberg } 28175e84b7cSJohan Hedberg 28275e84b7cSJohan Hedberg hdev->req_status = hdev->req_result = 0; 283f60cb305SJohan Hedberg skb = hdev->req_skb; 284f60cb305SJohan Hedberg hdev->req_skb = NULL; 28575e84b7cSJohan Hedberg 28675e84b7cSJohan Hedberg BT_DBG("%s end: err %d", hdev->name, err); 28775e84b7cSJohan Hedberg 288f60cb305SJohan Hedberg if (err < 0) { 289f60cb305SJohan Hedberg kfree_skb(skb); 29075e84b7cSJohan Hedberg return ERR_PTR(err); 291f60cb305SJohan Hedberg } 29275e84b7cSJohan Hedberg 293757aa0b5SJohan Hedberg if (!skb) 294757aa0b5SJohan Hedberg return ERR_PTR(-ENODATA); 295757aa0b5SJohan Hedberg 296757aa0b5SJohan Hedberg return skb; 2977b1abbbeSJohan Hedberg } 2987b1abbbeSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync_ev); 2997b1abbbeSJohan Hedberg 3007b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 30107dc93ddSJohan Hedberg const void *param, u32 timeout) 3027b1abbbeSJohan Hedberg { 3037b1abbbeSJohan Hedberg return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); 30475e84b7cSJohan Hedberg } 30575e84b7cSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync); 30675e84b7cSJohan Hedberg 3071da177e4SLinus Torvalds /* Execute request and wait for completion. */ 30801178cd4SJohan Hedberg static int __hci_req_sync(struct hci_dev *hdev, 30942c6b129SJohan Hedberg void (*func)(struct hci_request *req, 31042c6b129SJohan Hedberg unsigned long opt), 3111da177e4SLinus Torvalds unsigned long opt, __u32 timeout) 3121da177e4SLinus Torvalds { 31342c6b129SJohan Hedberg struct hci_request req; 3141da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 3151da177e4SLinus Torvalds int err = 0; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds BT_DBG("%s start", hdev->name); 3181da177e4SLinus Torvalds 31942c6b129SJohan Hedberg hci_req_init(&req, hdev); 32042c6b129SJohan Hedberg 3211da177e4SLinus Torvalds hdev->req_status = HCI_REQ_PEND; 3221da177e4SLinus Torvalds 32342c6b129SJohan Hedberg func(&req, opt); 32453cce22dSJohan Hedberg 325039fada5SChan-yeol Park add_wait_queue(&hdev->req_wait_q, &wait); 326039fada5SChan-yeol Park set_current_state(TASK_INTERRUPTIBLE); 327039fada5SChan-yeol Park 328f60cb305SJohan Hedberg err = hci_req_run_skb(&req, hci_req_sync_complete); 32942c6b129SJohan Hedberg if (err < 0) { 33053cce22dSJohan Hedberg hdev->req_status = 0; 331920c8300SAndre Guedes 332039fada5SChan-yeol Park remove_wait_queue(&hdev->req_wait_q, &wait); 33322a3ceabSJohan Hedberg set_current_state(TASK_RUNNING); 334039fada5SChan-yeol Park 335920c8300SAndre Guedes /* ENODATA means the HCI request command queue is empty. 336920c8300SAndre Guedes * This can happen when a request with conditionals doesn't 337920c8300SAndre Guedes * trigger any commands to be sent. This is normal behavior 338920c8300SAndre Guedes * and should not trigger an error return. 33942c6b129SJohan Hedberg */ 340920c8300SAndre Guedes if (err == -ENODATA) 34142c6b129SJohan Hedberg return 0; 342920c8300SAndre Guedes 343920c8300SAndre Guedes return err; 34453cce22dSJohan Hedberg } 34553cce22dSJohan Hedberg 3461da177e4SLinus Torvalds schedule_timeout(timeout); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds remove_wait_queue(&hdev->req_wait_q, &wait); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds if (signal_pending(current)) 3511da177e4SLinus Torvalds return -EINTR; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds switch (hdev->req_status) { 3541da177e4SLinus Torvalds case HCI_REQ_DONE: 355e175072fSJoe Perches err = -bt_to_errno(hdev->req_result); 3561da177e4SLinus Torvalds break; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds case HCI_REQ_CANCELED: 3591da177e4SLinus Torvalds err = -hdev->req_result; 3601da177e4SLinus Torvalds break; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds default: 3631da177e4SLinus Torvalds err = -ETIMEDOUT; 3641da177e4SLinus Torvalds break; 3653ff50b79SStephen Hemminger } 3661da177e4SLinus Torvalds 367a5040efaSJohan Hedberg hdev->req_status = hdev->req_result = 0; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds BT_DBG("%s end: err %d", hdev->name, err); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds return err; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 37401178cd4SJohan Hedberg static int hci_req_sync(struct hci_dev *hdev, 37542c6b129SJohan Hedberg void (*req)(struct hci_request *req, 37642c6b129SJohan Hedberg unsigned long opt), 3771da177e4SLinus Torvalds unsigned long opt, __u32 timeout) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds int ret; 3801da177e4SLinus Torvalds 3817c6a329eSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) 3827c6a329eSMarcel Holtmann return -ENETDOWN; 3837c6a329eSMarcel Holtmann 3841da177e4SLinus Torvalds /* Serialize all requests */ 3851da177e4SLinus Torvalds hci_req_lock(hdev); 38601178cd4SJohan Hedberg ret = __hci_req_sync(hdev, req, opt, timeout); 3871da177e4SLinus Torvalds hci_req_unlock(hdev); 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds return ret; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 39242c6b129SJohan Hedberg static void hci_reset_req(struct hci_request *req, unsigned long opt) 3931da177e4SLinus Torvalds { 39442c6b129SJohan Hedberg BT_DBG("%s %ld", req->hdev->name, opt); 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* Reset device */ 39742c6b129SJohan Hedberg set_bit(HCI_RESET, &req->hdev->flags); 39842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_RESET, 0, NULL); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 40142c6b129SJohan Hedberg static void bredr_init(struct hci_request *req) 4021da177e4SLinus Torvalds { 40342c6b129SJohan Hedberg req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; 4042455a3eaSAndrei Emeltchenko 4051da177e4SLinus Torvalds /* Read Local Supported Features */ 40642c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); 4071da177e4SLinus Torvalds 4081143e5a6SMarcel Holtmann /* Read Local Version */ 40942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 4102177bab5SJohan Hedberg 4112177bab5SJohan Hedberg /* Read BD Address */ 41242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4150af801b9SJohan Hedberg static void amp_init1(struct hci_request *req) 416e61ef499SAndrei Emeltchenko { 41742c6b129SJohan Hedberg req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; 4182455a3eaSAndrei Emeltchenko 419e61ef499SAndrei Emeltchenko /* Read Local Version */ 42042c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 4216bcbc489SAndrei Emeltchenko 422f6996cfeSMarcel Holtmann /* Read Local Supported Commands */ 423f6996cfeSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 424f6996cfeSMarcel Holtmann 4256bcbc489SAndrei Emeltchenko /* Read Local AMP Info */ 42642c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); 427e71dfabaSAndrei Emeltchenko 428e71dfabaSAndrei Emeltchenko /* Read Data Blk size */ 42942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); 4307528ca1cSMarcel Holtmann 431f38ba941SMarcel Holtmann /* Read Flow Control Mode */ 432f38ba941SMarcel Holtmann hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); 433f38ba941SMarcel Holtmann 4347528ca1cSMarcel Holtmann /* Read Location Data */ 4357528ca1cSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); 436e61ef499SAndrei Emeltchenko } 437e61ef499SAndrei Emeltchenko 4380af801b9SJohan Hedberg static void amp_init2(struct hci_request *req) 4390af801b9SJohan Hedberg { 4400af801b9SJohan Hedberg /* Read Local Supported Features. Not all AMP controllers 4410af801b9SJohan Hedberg * support this so it's placed conditionally in the second 4420af801b9SJohan Hedberg * stage init. 4430af801b9SJohan Hedberg */ 4440af801b9SJohan Hedberg if (req->hdev->commands[14] & 0x20) 4450af801b9SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); 4460af801b9SJohan Hedberg } 4470af801b9SJohan Hedberg 44842c6b129SJohan Hedberg static void hci_init1_req(struct hci_request *req, unsigned long opt) 449e61ef499SAndrei Emeltchenko { 45042c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 451e61ef499SAndrei Emeltchenko 452e61ef499SAndrei Emeltchenko BT_DBG("%s %ld", hdev->name, opt); 453e61ef499SAndrei Emeltchenko 45411778716SAndrei Emeltchenko /* Reset */ 45511778716SAndrei Emeltchenko if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) 45642c6b129SJohan Hedberg hci_reset_req(req, 0); 45711778716SAndrei Emeltchenko 458e61ef499SAndrei Emeltchenko switch (hdev->dev_type) { 459e61ef499SAndrei Emeltchenko case HCI_BREDR: 46042c6b129SJohan Hedberg bredr_init(req); 461e61ef499SAndrei Emeltchenko break; 462e61ef499SAndrei Emeltchenko 463e61ef499SAndrei Emeltchenko case HCI_AMP: 4640af801b9SJohan Hedberg amp_init1(req); 465e61ef499SAndrei Emeltchenko break; 466e61ef499SAndrei Emeltchenko 467e61ef499SAndrei Emeltchenko default: 468e61ef499SAndrei Emeltchenko BT_ERR("Unknown device type %d", hdev->dev_type); 469e61ef499SAndrei Emeltchenko break; 470e61ef499SAndrei Emeltchenko } 471e61ef499SAndrei Emeltchenko } 472e61ef499SAndrei Emeltchenko 47342c6b129SJohan Hedberg static void bredr_setup(struct hci_request *req) 4742177bab5SJohan Hedberg { 4752177bab5SJohan Hedberg __le16 param; 4762177bab5SJohan Hedberg __u8 flt_type; 4772177bab5SJohan Hedberg 4782177bab5SJohan Hedberg /* Read Buffer Size (ACL mtu, max pkt, etc.) */ 47942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); 4802177bab5SJohan Hedberg 4812177bab5SJohan Hedberg /* Read Class of Device */ 48242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); 4832177bab5SJohan Hedberg 4842177bab5SJohan Hedberg /* Read Local Name */ 48542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); 4862177bab5SJohan Hedberg 4872177bab5SJohan Hedberg /* Read Voice Setting */ 48842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); 4892177bab5SJohan Hedberg 490b4cb9fb2SMarcel Holtmann /* Read Number of Supported IAC */ 491b4cb9fb2SMarcel Holtmann hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); 492b4cb9fb2SMarcel Holtmann 4934b836f39SMarcel Holtmann /* Read Current IAC LAP */ 4944b836f39SMarcel Holtmann hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); 4954b836f39SMarcel Holtmann 4962177bab5SJohan Hedberg /* Clear Event Filters */ 4972177bab5SJohan Hedberg flt_type = HCI_FLT_CLEAR_ALL; 49842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); 4992177bab5SJohan Hedberg 5002177bab5SJohan Hedberg /* Connection accept timeout ~20 secs */ 501dcf4adbfSJoe Perches param = cpu_to_le16(0x7d00); 50242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); 5032177bab5SJohan Hedberg } 5042177bab5SJohan Hedberg 50542c6b129SJohan Hedberg static void le_setup(struct hci_request *req) 5062177bab5SJohan Hedberg { 507c73eee91SJohan Hedberg struct hci_dev *hdev = req->hdev; 508c73eee91SJohan Hedberg 5092177bab5SJohan Hedberg /* Read LE Buffer Size */ 51042c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); 5112177bab5SJohan Hedberg 5122177bab5SJohan Hedberg /* Read LE Local Supported Features */ 51342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); 5142177bab5SJohan Hedberg 515747d3f03SMarcel Holtmann /* Read LE Supported States */ 516747d3f03SMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); 517747d3f03SMarcel Holtmann 5182177bab5SJohan Hedberg /* Read LE White List Size */ 51942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); 5202177bab5SJohan Hedberg 521747d3f03SMarcel Holtmann /* Clear LE White List */ 522747d3f03SMarcel Holtmann hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL); 523c73eee91SJohan Hedberg 524c73eee91SJohan Hedberg /* LE-only controllers have LE implicitly enabled */ 525c73eee91SJohan Hedberg if (!lmp_bredr_capable(hdev)) 526a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_LE_ENABLED); 5272177bab5SJohan Hedberg } 5282177bab5SJohan Hedberg 52942c6b129SJohan Hedberg static void hci_setup_event_mask(struct hci_request *req) 5302177bab5SJohan Hedberg { 53142c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 53242c6b129SJohan Hedberg 5332177bab5SJohan Hedberg /* The second byte is 0xff instead of 0x9f (two reserved bits 5342177bab5SJohan Hedberg * disabled) since a Broadcom 1.2 dongle doesn't respond to the 5352177bab5SJohan Hedberg * command otherwise. 5362177bab5SJohan Hedberg */ 5372177bab5SJohan Hedberg u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; 5382177bab5SJohan Hedberg 5392177bab5SJohan Hedberg /* CSR 1.1 dongles does not accept any bitfield so don't try to set 5402177bab5SJohan Hedberg * any event mask for pre 1.2 devices. 5412177bab5SJohan Hedberg */ 5422177bab5SJohan Hedberg if (hdev->hci_ver < BLUETOOTH_VER_1_2) 5432177bab5SJohan Hedberg return; 5442177bab5SJohan Hedberg 5452177bab5SJohan Hedberg if (lmp_bredr_capable(hdev)) { 5462177bab5SJohan Hedberg events[4] |= 0x01; /* Flow Specification Complete */ 5472177bab5SJohan Hedberg events[4] |= 0x02; /* Inquiry Result with RSSI */ 5482177bab5SJohan Hedberg events[4] |= 0x04; /* Read Remote Extended Features Complete */ 5492177bab5SJohan Hedberg events[5] |= 0x08; /* Synchronous Connection Complete */ 5502177bab5SJohan Hedberg events[5] |= 0x10; /* Synchronous Connection Changed */ 551c7882cbdSMarcel Holtmann } else { 552c7882cbdSMarcel Holtmann /* Use a different default for LE-only devices */ 553c7882cbdSMarcel Holtmann memset(events, 0, sizeof(events)); 554c7882cbdSMarcel Holtmann events[0] |= 0x10; /* Disconnection Complete */ 555c7882cbdSMarcel Holtmann events[1] |= 0x08; /* Read Remote Version Information Complete */ 556c7882cbdSMarcel Holtmann events[1] |= 0x20; /* Command Complete */ 557c7882cbdSMarcel Holtmann events[1] |= 0x40; /* Command Status */ 558c7882cbdSMarcel Holtmann events[1] |= 0x80; /* Hardware Error */ 559c7882cbdSMarcel Holtmann events[2] |= 0x04; /* Number of Completed Packets */ 560c7882cbdSMarcel Holtmann events[3] |= 0x02; /* Data Buffer Overflow */ 5610da71f1bSMarcel Holtmann 5620da71f1bSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_ENCRYPTION) { 5630da71f1bSMarcel Holtmann events[0] |= 0x80; /* Encryption Change */ 564c7882cbdSMarcel Holtmann events[5] |= 0x80; /* Encryption Key Refresh Complete */ 5652177bab5SJohan Hedberg } 5660da71f1bSMarcel Holtmann } 5672177bab5SJohan Hedberg 5682177bab5SJohan Hedberg if (lmp_inq_rssi_capable(hdev)) 5692177bab5SJohan Hedberg events[4] |= 0x02; /* Inquiry Result with RSSI */ 5702177bab5SJohan Hedberg 5712177bab5SJohan Hedberg if (lmp_sniffsubr_capable(hdev)) 5722177bab5SJohan Hedberg events[5] |= 0x20; /* Sniff Subrating */ 5732177bab5SJohan Hedberg 5742177bab5SJohan Hedberg if (lmp_pause_enc_capable(hdev)) 5752177bab5SJohan Hedberg events[5] |= 0x80; /* Encryption Key Refresh Complete */ 5762177bab5SJohan Hedberg 5772177bab5SJohan Hedberg if (lmp_ext_inq_capable(hdev)) 5782177bab5SJohan Hedberg events[5] |= 0x40; /* Extended Inquiry Result */ 5792177bab5SJohan Hedberg 5802177bab5SJohan Hedberg if (lmp_no_flush_capable(hdev)) 5812177bab5SJohan Hedberg events[7] |= 0x01; /* Enhanced Flush Complete */ 5822177bab5SJohan Hedberg 5832177bab5SJohan Hedberg if (lmp_lsto_capable(hdev)) 5842177bab5SJohan Hedberg events[6] |= 0x80; /* Link Supervision Timeout Changed */ 5852177bab5SJohan Hedberg 5862177bab5SJohan Hedberg if (lmp_ssp_capable(hdev)) { 5872177bab5SJohan Hedberg events[6] |= 0x01; /* IO Capability Request */ 5882177bab5SJohan Hedberg events[6] |= 0x02; /* IO Capability Response */ 5892177bab5SJohan Hedberg events[6] |= 0x04; /* User Confirmation Request */ 5902177bab5SJohan Hedberg events[6] |= 0x08; /* User Passkey Request */ 5912177bab5SJohan Hedberg events[6] |= 0x10; /* Remote OOB Data Request */ 5922177bab5SJohan Hedberg events[6] |= 0x20; /* Simple Pairing Complete */ 5932177bab5SJohan Hedberg events[7] |= 0x04; /* User Passkey Notification */ 5942177bab5SJohan Hedberg events[7] |= 0x08; /* Keypress Notification */ 5952177bab5SJohan Hedberg events[7] |= 0x10; /* Remote Host Supported 5962177bab5SJohan Hedberg * Features Notification 5972177bab5SJohan Hedberg */ 5982177bab5SJohan Hedberg } 5992177bab5SJohan Hedberg 6002177bab5SJohan Hedberg if (lmp_le_capable(hdev)) 6012177bab5SJohan Hedberg events[7] |= 0x20; /* LE Meta-Event */ 6022177bab5SJohan Hedberg 60342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); 6042177bab5SJohan Hedberg } 6052177bab5SJohan Hedberg 60642c6b129SJohan Hedberg static void hci_init2_req(struct hci_request *req, unsigned long opt) 6072177bab5SJohan Hedberg { 60842c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 60942c6b129SJohan Hedberg 6100af801b9SJohan Hedberg if (hdev->dev_type == HCI_AMP) 6110af801b9SJohan Hedberg return amp_init2(req); 6120af801b9SJohan Hedberg 6132177bab5SJohan Hedberg if (lmp_bredr_capable(hdev)) 61442c6b129SJohan Hedberg bredr_setup(req); 61556f87901SJohan Hedberg else 616a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); 6172177bab5SJohan Hedberg 6182177bab5SJohan Hedberg if (lmp_le_capable(hdev)) 61942c6b129SJohan Hedberg le_setup(req); 6202177bab5SJohan Hedberg 6210f3adeaeSMarcel Holtmann /* All Bluetooth 1.2 and later controllers should support the 6220f3adeaeSMarcel Holtmann * HCI command for reading the local supported commands. 6230f3adeaeSMarcel Holtmann * 6240f3adeaeSMarcel Holtmann * Unfortunately some controllers indicate Bluetooth 1.2 support, 6250f3adeaeSMarcel Holtmann * but do not have support for this command. If that is the case, 6260f3adeaeSMarcel Holtmann * the driver can quirk the behavior and skip reading the local 6270f3adeaeSMarcel Holtmann * supported commands. 6283f8e2d75SJohan Hedberg */ 6290f3adeaeSMarcel Holtmann if (hdev->hci_ver > BLUETOOTH_VER_1_1 && 6300f3adeaeSMarcel Holtmann !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) 63142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 6322177bab5SJohan Hedberg 6332177bab5SJohan Hedberg if (lmp_ssp_capable(hdev)) { 63457af75a8SMarcel Holtmann /* When SSP is available, then the host features page 63557af75a8SMarcel Holtmann * should also be available as well. However some 63657af75a8SMarcel Holtmann * controllers list the max_page as 0 as long as SSP 63757af75a8SMarcel Holtmann * has not been enabled. To achieve proper debugging 63857af75a8SMarcel Holtmann * output, force the minimum max_page to 1 at least. 63957af75a8SMarcel Holtmann */ 64057af75a8SMarcel Holtmann hdev->max_page = 0x01; 64157af75a8SMarcel Holtmann 642d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { 6432177bab5SJohan Hedberg u8 mode = 0x01; 644574ea3c7SMarcel Holtmann 64542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_SSP_MODE, 6462177bab5SJohan Hedberg sizeof(mode), &mode); 6472177bab5SJohan Hedberg } else { 6482177bab5SJohan Hedberg struct hci_cp_write_eir cp; 6492177bab5SJohan Hedberg 6502177bab5SJohan Hedberg memset(hdev->eir, 0, sizeof(hdev->eir)); 6512177bab5SJohan Hedberg memset(&cp, 0, sizeof(cp)); 6522177bab5SJohan Hedberg 65342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); 6542177bab5SJohan Hedberg } 6552177bab5SJohan Hedberg } 6562177bab5SJohan Hedberg 657043ec9bfSMarcel Holtmann if (lmp_inq_rssi_capable(hdev) || 658043ec9bfSMarcel Holtmann test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) { 65904422da9SMarcel Holtmann u8 mode; 66004422da9SMarcel Holtmann 66104422da9SMarcel Holtmann /* If Extended Inquiry Result events are supported, then 66204422da9SMarcel Holtmann * they are clearly preferred over Inquiry Result with RSSI 66304422da9SMarcel Holtmann * events. 66404422da9SMarcel Holtmann */ 66504422da9SMarcel Holtmann mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; 66604422da9SMarcel Holtmann 66704422da9SMarcel Holtmann hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); 66804422da9SMarcel Holtmann } 6692177bab5SJohan Hedberg 6702177bab5SJohan Hedberg if (lmp_inq_tx_pwr_capable(hdev)) 67142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL); 6722177bab5SJohan Hedberg 6732177bab5SJohan Hedberg if (lmp_ext_feat_capable(hdev)) { 6742177bab5SJohan Hedberg struct hci_cp_read_local_ext_features cp; 6752177bab5SJohan Hedberg 6762177bab5SJohan Hedberg cp.page = 0x01; 67742c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, 67842c6b129SJohan Hedberg sizeof(cp), &cp); 6792177bab5SJohan Hedberg } 6802177bab5SJohan Hedberg 681d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { 6822177bab5SJohan Hedberg u8 enable = 1; 68342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), 6842177bab5SJohan Hedberg &enable); 6852177bab5SJohan Hedberg } 6862177bab5SJohan Hedberg } 6872177bab5SJohan Hedberg 68842c6b129SJohan Hedberg static void hci_setup_link_policy(struct hci_request *req) 6892177bab5SJohan Hedberg { 69042c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 6912177bab5SJohan Hedberg struct hci_cp_write_def_link_policy cp; 6922177bab5SJohan Hedberg u16 link_policy = 0; 6932177bab5SJohan Hedberg 6942177bab5SJohan Hedberg if (lmp_rswitch_capable(hdev)) 6952177bab5SJohan Hedberg link_policy |= HCI_LP_RSWITCH; 6962177bab5SJohan Hedberg if (lmp_hold_capable(hdev)) 6972177bab5SJohan Hedberg link_policy |= HCI_LP_HOLD; 6982177bab5SJohan Hedberg if (lmp_sniff_capable(hdev)) 6992177bab5SJohan Hedberg link_policy |= HCI_LP_SNIFF; 7002177bab5SJohan Hedberg if (lmp_park_capable(hdev)) 7012177bab5SJohan Hedberg link_policy |= HCI_LP_PARK; 7022177bab5SJohan Hedberg 7032177bab5SJohan Hedberg cp.policy = cpu_to_le16(link_policy); 70442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp); 7052177bab5SJohan Hedberg } 7062177bab5SJohan Hedberg 70742c6b129SJohan Hedberg static void hci_set_le_support(struct hci_request *req) 7082177bab5SJohan Hedberg { 70942c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 7102177bab5SJohan Hedberg struct hci_cp_write_le_host_supported cp; 7112177bab5SJohan Hedberg 712c73eee91SJohan Hedberg /* LE-only devices do not support explicit enablement */ 713c73eee91SJohan Hedberg if (!lmp_bredr_capable(hdev)) 714c73eee91SJohan Hedberg return; 715c73eee91SJohan Hedberg 7162177bab5SJohan Hedberg memset(&cp, 0, sizeof(cp)); 7172177bab5SJohan Hedberg 718d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 7192177bab5SJohan Hedberg cp.le = 0x01; 72032226e4fSMarcel Holtmann cp.simul = 0x00; 7212177bab5SJohan Hedberg } 7222177bab5SJohan Hedberg 7232177bab5SJohan Hedberg if (cp.le != lmp_host_le_capable(hdev)) 72442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), 7252177bab5SJohan Hedberg &cp); 7262177bab5SJohan Hedberg } 7272177bab5SJohan Hedberg 728d62e6d67SJohan Hedberg static void hci_set_event_mask_page_2(struct hci_request *req) 729d62e6d67SJohan Hedberg { 730d62e6d67SJohan Hedberg struct hci_dev *hdev = req->hdev; 731d62e6d67SJohan Hedberg u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 732d62e6d67SJohan Hedberg 733d62e6d67SJohan Hedberg /* If Connectionless Slave Broadcast master role is supported 734d62e6d67SJohan Hedberg * enable all necessary events for it. 735d62e6d67SJohan Hedberg */ 73653b834d2SMarcel Holtmann if (lmp_csb_master_capable(hdev)) { 737d62e6d67SJohan Hedberg events[1] |= 0x40; /* Triggered Clock Capture */ 738d62e6d67SJohan Hedberg events[1] |= 0x80; /* Synchronization Train Complete */ 739d62e6d67SJohan Hedberg events[2] |= 0x10; /* Slave Page Response Timeout */ 740d62e6d67SJohan Hedberg events[2] |= 0x20; /* CSB Channel Map Change */ 741d62e6d67SJohan Hedberg } 742d62e6d67SJohan Hedberg 743d62e6d67SJohan Hedberg /* If Connectionless Slave Broadcast slave role is supported 744d62e6d67SJohan Hedberg * enable all necessary events for it. 745d62e6d67SJohan Hedberg */ 74653b834d2SMarcel Holtmann if (lmp_csb_slave_capable(hdev)) { 747d62e6d67SJohan Hedberg events[2] |= 0x01; /* Synchronization Train Received */ 748d62e6d67SJohan Hedberg events[2] |= 0x02; /* CSB Receive */ 749d62e6d67SJohan Hedberg events[2] |= 0x04; /* CSB Timeout */ 750d62e6d67SJohan Hedberg events[2] |= 0x08; /* Truncated Page Complete */ 751d62e6d67SJohan Hedberg } 752d62e6d67SJohan Hedberg 75340c59fcbSMarcel Holtmann /* Enable Authenticated Payload Timeout Expired event if supported */ 754cd7ca0ecSMarcel Holtmann if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) 75540c59fcbSMarcel Holtmann events[2] |= 0x80; 75640c59fcbSMarcel Holtmann 757d62e6d67SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); 758d62e6d67SJohan Hedberg } 759d62e6d67SJohan Hedberg 76042c6b129SJohan Hedberg static void hci_init3_req(struct hci_request *req, unsigned long opt) 7612177bab5SJohan Hedberg { 76242c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 763d2c5d77fSJohan Hedberg u8 p; 76442c6b129SJohan Hedberg 7650da71f1bSMarcel Holtmann hci_setup_event_mask(req); 7660da71f1bSMarcel Holtmann 767e81be90bSJohan Hedberg if (hdev->commands[6] & 0x20 && 768e81be90bSJohan Hedberg !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { 76948ce62c4SMarcel Holtmann struct hci_cp_read_stored_link_key cp; 77048ce62c4SMarcel Holtmann 77148ce62c4SMarcel Holtmann bacpy(&cp.bdaddr, BDADDR_ANY); 77248ce62c4SMarcel Holtmann cp.read_all = 0x01; 77348ce62c4SMarcel Holtmann hci_req_add(req, HCI_OP_READ_STORED_LINK_KEY, sizeof(cp), &cp); 77448ce62c4SMarcel Holtmann } 77548ce62c4SMarcel Holtmann 7762177bab5SJohan Hedberg if (hdev->commands[5] & 0x10) 77742c6b129SJohan Hedberg hci_setup_link_policy(req); 7782177bab5SJohan Hedberg 779417287deSMarcel Holtmann if (hdev->commands[8] & 0x01) 780417287deSMarcel Holtmann hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); 781417287deSMarcel Holtmann 782417287deSMarcel Holtmann /* Some older Broadcom based Bluetooth 1.2 controllers do not 783417287deSMarcel Holtmann * support the Read Page Scan Type command. Check support for 784417287deSMarcel Holtmann * this command in the bit mask of supported commands. 785417287deSMarcel Holtmann */ 786417287deSMarcel Holtmann if (hdev->commands[13] & 0x01) 787417287deSMarcel Holtmann hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); 788417287deSMarcel Holtmann 7899193c6e8SAndre Guedes if (lmp_le_capable(hdev)) { 7909193c6e8SAndre Guedes u8 events[8]; 7919193c6e8SAndre Guedes 7929193c6e8SAndre Guedes memset(events, 0, sizeof(events)); 7934d6c705bSMarcel Holtmann events[0] = 0x0f; 7944d6c705bSMarcel Holtmann 7954d6c705bSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_ENCRYPTION) 7964d6c705bSMarcel Holtmann events[0] |= 0x10; /* LE Long Term Key Request */ 797662bc2e6SAndre Guedes 798662bc2e6SAndre Guedes /* If controller supports the Connection Parameters Request 799662bc2e6SAndre Guedes * Link Layer Procedure, enable the corresponding event. 800662bc2e6SAndre Guedes */ 801662bc2e6SAndre Guedes if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) 802662bc2e6SAndre Guedes events[0] |= 0x20; /* LE Remote Connection 803662bc2e6SAndre Guedes * Parameter Request 804662bc2e6SAndre Guedes */ 805662bc2e6SAndre Guedes 806a9f6068eSMarcel Holtmann /* If the controller supports the Data Length Extension 807a9f6068eSMarcel Holtmann * feature, enable the corresponding event. 808a9f6068eSMarcel Holtmann */ 809a9f6068eSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) 810a9f6068eSMarcel Holtmann events[0] |= 0x40; /* LE Data Length Change */ 811a9f6068eSMarcel Holtmann 8124b71bba4SMarcel Holtmann /* If the controller supports Extended Scanner Filter 8134b71bba4SMarcel Holtmann * Policies, enable the correspondig event. 8144b71bba4SMarcel Holtmann */ 8154b71bba4SMarcel Holtmann if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY) 8164b71bba4SMarcel Holtmann events[1] |= 0x04; /* LE Direct Advertising 8174b71bba4SMarcel Holtmann * Report 8184b71bba4SMarcel Holtmann */ 8194b71bba4SMarcel Holtmann 8205a34bd5fSMarcel Holtmann /* If the controller supports the LE Read Local P-256 8215a34bd5fSMarcel Holtmann * Public Key command, enable the corresponding event. 8225a34bd5fSMarcel Holtmann */ 8235a34bd5fSMarcel Holtmann if (hdev->commands[34] & 0x02) 8245a34bd5fSMarcel Holtmann events[0] |= 0x80; /* LE Read Local P-256 8255a34bd5fSMarcel Holtmann * Public Key Complete 8265a34bd5fSMarcel Holtmann */ 8275a34bd5fSMarcel Holtmann 8285a34bd5fSMarcel Holtmann /* If the controller supports the LE Generate DHKey 8295a34bd5fSMarcel Holtmann * command, enable the corresponding event. 8305a34bd5fSMarcel Holtmann */ 8315a34bd5fSMarcel Holtmann if (hdev->commands[34] & 0x04) 8325a34bd5fSMarcel Holtmann events[1] |= 0x01; /* LE Generate DHKey Complete */ 8335a34bd5fSMarcel Holtmann 8349193c6e8SAndre Guedes hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events), 8359193c6e8SAndre Guedes events); 8369193c6e8SAndre Guedes 83715a49ccaSMarcel Holtmann if (hdev->commands[25] & 0x40) { 83815a49ccaSMarcel Holtmann /* Read LE Advertising Channel TX Power */ 83915a49ccaSMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); 84015a49ccaSMarcel Holtmann } 84115a49ccaSMarcel Holtmann 842a9f6068eSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) { 843a9f6068eSMarcel Holtmann /* Read LE Maximum Data Length */ 844a9f6068eSMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL); 845a9f6068eSMarcel Holtmann 846a9f6068eSMarcel Holtmann /* Read LE Suggested Default Data Length */ 847a9f6068eSMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL); 848a9f6068eSMarcel Holtmann } 849a9f6068eSMarcel Holtmann 85042c6b129SJohan Hedberg hci_set_le_support(req); 8519193c6e8SAndre Guedes } 852d2c5d77fSJohan Hedberg 853d2c5d77fSJohan Hedberg /* Read features beyond page 1 if available */ 854d2c5d77fSJohan Hedberg for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { 855d2c5d77fSJohan Hedberg struct hci_cp_read_local_ext_features cp; 856d2c5d77fSJohan Hedberg 857d2c5d77fSJohan Hedberg cp.page = p; 858d2c5d77fSJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, 859d2c5d77fSJohan Hedberg sizeof(cp), &cp); 860d2c5d77fSJohan Hedberg } 8612177bab5SJohan Hedberg } 8622177bab5SJohan Hedberg 8635d4e7e8dSJohan Hedberg static void hci_init4_req(struct hci_request *req, unsigned long opt) 8645d4e7e8dSJohan Hedberg { 8655d4e7e8dSJohan Hedberg struct hci_dev *hdev = req->hdev; 8665d4e7e8dSJohan Hedberg 86736f260ceSMarcel Holtmann /* Some Broadcom based Bluetooth controllers do not support the 86836f260ceSMarcel Holtmann * Delete Stored Link Key command. They are clearly indicating its 86936f260ceSMarcel Holtmann * absence in the bit mask of supported commands. 87036f260ceSMarcel Holtmann * 87136f260ceSMarcel Holtmann * Check the supported commands and only if the the command is marked 87236f260ceSMarcel Holtmann * as supported send it. If not supported assume that the controller 87336f260ceSMarcel Holtmann * does not have actual support for stored link keys which makes this 87436f260ceSMarcel Holtmann * command redundant anyway. 87536f260ceSMarcel Holtmann * 87636f260ceSMarcel Holtmann * Some controllers indicate that they support handling deleting 87736f260ceSMarcel Holtmann * stored link keys, but they don't. The quirk lets a driver 87836f260ceSMarcel Holtmann * just disable this command. 87936f260ceSMarcel Holtmann */ 88036f260ceSMarcel Holtmann if (hdev->commands[6] & 0x80 && 88136f260ceSMarcel Holtmann !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { 88236f260ceSMarcel Holtmann struct hci_cp_delete_stored_link_key cp; 88336f260ceSMarcel Holtmann 88436f260ceSMarcel Holtmann bacpy(&cp.bdaddr, BDADDR_ANY); 88536f260ceSMarcel Holtmann cp.delete_all = 0x01; 88636f260ceSMarcel Holtmann hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, 88736f260ceSMarcel Holtmann sizeof(cp), &cp); 88836f260ceSMarcel Holtmann } 88936f260ceSMarcel Holtmann 890d62e6d67SJohan Hedberg /* Set event mask page 2 if the HCI command for it is supported */ 891d62e6d67SJohan Hedberg if (hdev->commands[22] & 0x04) 892d62e6d67SJohan Hedberg hci_set_event_mask_page_2(req); 893d62e6d67SJohan Hedberg 894109e3191SMarcel Holtmann /* Read local codec list if the HCI command is supported */ 895109e3191SMarcel Holtmann if (hdev->commands[29] & 0x20) 896109e3191SMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_CODECS, 0, NULL); 897109e3191SMarcel Holtmann 898f4fe73edSMarcel Holtmann /* Get MWS transport configuration if the HCI command is supported */ 899f4fe73edSMarcel Holtmann if (hdev->commands[30] & 0x08) 900f4fe73edSMarcel Holtmann hci_req_add(req, HCI_OP_GET_MWS_TRANSPORT_CONFIG, 0, NULL); 901f4fe73edSMarcel Holtmann 9025d4e7e8dSJohan Hedberg /* Check for Synchronization Train support */ 90353b834d2SMarcel Holtmann if (lmp_sync_train_capable(hdev)) 9045d4e7e8dSJohan Hedberg hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); 905a6d0d690SMarcel Holtmann 906a6d0d690SMarcel Holtmann /* Enable Secure Connections if supported and configured */ 907d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && 908574ea3c7SMarcel Holtmann bredr_sc_enabled(hdev)) { 909a6d0d690SMarcel Holtmann u8 support = 0x01; 910574ea3c7SMarcel Holtmann 911a6d0d690SMarcel Holtmann hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT, 912a6d0d690SMarcel Holtmann sizeof(support), &support); 913a6d0d690SMarcel Holtmann } 9145d4e7e8dSJohan Hedberg } 9155d4e7e8dSJohan Hedberg 9162177bab5SJohan Hedberg static int __hci_init(struct hci_dev *hdev) 9172177bab5SJohan Hedberg { 9182177bab5SJohan Hedberg int err; 9192177bab5SJohan Hedberg 9202177bab5SJohan Hedberg err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); 9212177bab5SJohan Hedberg if (err < 0) 9222177bab5SJohan Hedberg return err; 9232177bab5SJohan Hedberg 924f640ee98SMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SETUP)) 925f640ee98SMarcel Holtmann hci_debugfs_create_basic(hdev); 9264b4148e9SMarcel Holtmann 9272177bab5SJohan Hedberg err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); 9282177bab5SJohan Hedberg if (err < 0) 9292177bab5SJohan Hedberg return err; 9302177bab5SJohan Hedberg 9310af801b9SJohan Hedberg /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode 9320af801b9SJohan Hedberg * BR/EDR/LE type controllers. AMP controllers only need the 9330af801b9SJohan Hedberg * first two stages of init. 9340af801b9SJohan Hedberg */ 9350af801b9SJohan Hedberg if (hdev->dev_type != HCI_BREDR) 9360af801b9SJohan Hedberg return 0; 9370af801b9SJohan Hedberg 9385d4e7e8dSJohan Hedberg err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); 9395d4e7e8dSJohan Hedberg if (err < 0) 9405d4e7e8dSJohan Hedberg return err; 9415d4e7e8dSJohan Hedberg 942baf27f6eSMarcel Holtmann err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT); 943baf27f6eSMarcel Holtmann if (err < 0) 944baf27f6eSMarcel Holtmann return err; 945baf27f6eSMarcel Holtmann 946ec6cef9cSMarcel Holtmann /* This function is only called when the controller is actually in 947ec6cef9cSMarcel Holtmann * configured state. When the controller is marked as unconfigured, 948ec6cef9cSMarcel Holtmann * this initialization procedure is not run. 949ec6cef9cSMarcel Holtmann * 950ec6cef9cSMarcel Holtmann * It means that it is possible that a controller runs through its 951ec6cef9cSMarcel Holtmann * setup phase and then discovers missing settings. If that is the 952ec6cef9cSMarcel Holtmann * case, then this function will not be called. It then will only 953ec6cef9cSMarcel Holtmann * be called during the config phase. 954ec6cef9cSMarcel Holtmann * 955ec6cef9cSMarcel Holtmann * So only when in setup phase or config phase, create the debugfs 956ec6cef9cSMarcel Holtmann * entries and register the SMP channels. 957baf27f6eSMarcel Holtmann */ 958d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 959d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) 960baf27f6eSMarcel Holtmann return 0; 961baf27f6eSMarcel Holtmann 96260c5f5fbSMarcel Holtmann hci_debugfs_create_common(hdev); 96360c5f5fbSMarcel Holtmann 96471c3b60eSMarcel Holtmann if (lmp_bredr_capable(hdev)) 96560c5f5fbSMarcel Holtmann hci_debugfs_create_bredr(hdev); 9662bfa3531SMarcel Holtmann 967162a3bacSMarcel Holtmann if (lmp_le_capable(hdev)) 96860c5f5fbSMarcel Holtmann hci_debugfs_create_le(hdev); 969e7b8fc92SMarcel Holtmann 970baf27f6eSMarcel Holtmann return 0; 9712177bab5SJohan Hedberg } 9722177bab5SJohan Hedberg 9730ebca7d6SMarcel Holtmann static void hci_init0_req(struct hci_request *req, unsigned long opt) 9740ebca7d6SMarcel Holtmann { 9750ebca7d6SMarcel Holtmann struct hci_dev *hdev = req->hdev; 9760ebca7d6SMarcel Holtmann 9770ebca7d6SMarcel Holtmann BT_DBG("%s %ld", hdev->name, opt); 9780ebca7d6SMarcel Holtmann 9790ebca7d6SMarcel Holtmann /* Reset */ 9800ebca7d6SMarcel Holtmann if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) 9810ebca7d6SMarcel Holtmann hci_reset_req(req, 0); 9820ebca7d6SMarcel Holtmann 9830ebca7d6SMarcel Holtmann /* Read Local Version */ 9840ebca7d6SMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 9850ebca7d6SMarcel Holtmann 9860ebca7d6SMarcel Holtmann /* Read BD Address */ 9870ebca7d6SMarcel Holtmann if (hdev->set_bdaddr) 9880ebca7d6SMarcel Holtmann hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); 9890ebca7d6SMarcel Holtmann } 9900ebca7d6SMarcel Holtmann 9910ebca7d6SMarcel Holtmann static int __hci_unconf_init(struct hci_dev *hdev) 9920ebca7d6SMarcel Holtmann { 9930ebca7d6SMarcel Holtmann int err; 9940ebca7d6SMarcel Holtmann 995cc78b44bSMarcel Holtmann if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 996cc78b44bSMarcel Holtmann return 0; 997cc78b44bSMarcel Holtmann 9980ebca7d6SMarcel Holtmann err = __hci_req_sync(hdev, hci_init0_req, 0, HCI_INIT_TIMEOUT); 9990ebca7d6SMarcel Holtmann if (err < 0) 10000ebca7d6SMarcel Holtmann return err; 10010ebca7d6SMarcel Holtmann 1002f640ee98SMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SETUP)) 1003f640ee98SMarcel Holtmann hci_debugfs_create_basic(hdev); 1004f640ee98SMarcel Holtmann 10050ebca7d6SMarcel Holtmann return 0; 10060ebca7d6SMarcel Holtmann } 10070ebca7d6SMarcel Holtmann 100842c6b129SJohan Hedberg static void hci_scan_req(struct hci_request *req, unsigned long opt) 10091da177e4SLinus Torvalds { 10101da177e4SLinus Torvalds __u8 scan = opt; 10111da177e4SLinus Torvalds 101242c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, scan); 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds /* Inquiry and Page scans */ 101542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 101842c6b129SJohan Hedberg static void hci_auth_req(struct hci_request *req, unsigned long opt) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds __u8 auth = opt; 10211da177e4SLinus Torvalds 102242c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, auth); 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* Authentication */ 102542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 102842c6b129SJohan Hedberg static void hci_encrypt_req(struct hci_request *req, unsigned long opt) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds __u8 encrypt = opt; 10311da177e4SLinus Torvalds 103242c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, encrypt); 10331da177e4SLinus Torvalds 1034e4e8e37cSMarcel Holtmann /* Encryption */ 103542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 103842c6b129SJohan Hedberg static void hci_linkpol_req(struct hci_request *req, unsigned long opt) 1039e4e8e37cSMarcel Holtmann { 1040e4e8e37cSMarcel Holtmann __le16 policy = cpu_to_le16(opt); 1041e4e8e37cSMarcel Holtmann 104242c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, policy); 1043e4e8e37cSMarcel Holtmann 1044e4e8e37cSMarcel Holtmann /* Default link policy */ 104542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); 1046e4e8e37cSMarcel Holtmann } 1047e4e8e37cSMarcel Holtmann 10481da177e4SLinus Torvalds /* Get HCI device by index. 10491da177e4SLinus Torvalds * Device is held on return. */ 10501da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index) 10511da177e4SLinus Torvalds { 10528035ded4SLuiz Augusto von Dentz struct hci_dev *hdev = NULL, *d; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds BT_DBG("%d", index); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (index < 0) 10571da177e4SLinus Torvalds return NULL; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds read_lock(&hci_dev_list_lock); 10608035ded4SLuiz Augusto von Dentz list_for_each_entry(d, &hci_dev_list, list) { 10611da177e4SLinus Torvalds if (d->id == index) { 10621da177e4SLinus Torvalds hdev = hci_dev_hold(d); 10631da177e4SLinus Torvalds break; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds read_unlock(&hci_dev_list_lock); 10671da177e4SLinus Torvalds return hdev; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* ---- Inquiry support ---- */ 1071ff9ef578SJohan Hedberg 107230dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev) 107330dc78e1SJohan Hedberg { 107430dc78e1SJohan Hedberg struct discovery_state *discov = &hdev->discovery; 107530dc78e1SJohan Hedberg 10766fbe195dSAndre Guedes switch (discov->state) { 1077343f935bSAndre Guedes case DISCOVERY_FINDING: 10786fbe195dSAndre Guedes case DISCOVERY_RESOLVING: 107930dc78e1SJohan Hedberg return true; 108030dc78e1SJohan Hedberg 10816fbe195dSAndre Guedes default: 108230dc78e1SJohan Hedberg return false; 108330dc78e1SJohan Hedberg } 10846fbe195dSAndre Guedes } 108530dc78e1SJohan Hedberg 1086ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state) 1087ff9ef578SJohan Hedberg { 1088bb3e0a33SJohan Hedberg int old_state = hdev->discovery.state; 1089bb3e0a33SJohan Hedberg 1090ff9ef578SJohan Hedberg BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); 1091ff9ef578SJohan Hedberg 1092bb3e0a33SJohan Hedberg if (old_state == state) 1093ff9ef578SJohan Hedberg return; 1094ff9ef578SJohan Hedberg 1095bb3e0a33SJohan Hedberg hdev->discovery.state = state; 1096bb3e0a33SJohan Hedberg 1097ff9ef578SJohan Hedberg switch (state) { 1098ff9ef578SJohan Hedberg case DISCOVERY_STOPPED: 1099c54c3860SAndre Guedes hci_update_background_scan(hdev); 1100c54c3860SAndre Guedes 1101bb3e0a33SJohan Hedberg if (old_state != DISCOVERY_STARTING) 1102ff9ef578SJohan Hedberg mgmt_discovering(hdev, 0); 1103ff9ef578SJohan Hedberg break; 1104ff9ef578SJohan Hedberg case DISCOVERY_STARTING: 1105ff9ef578SJohan Hedberg break; 1106343f935bSAndre Guedes case DISCOVERY_FINDING: 1107ff9ef578SJohan Hedberg mgmt_discovering(hdev, 1); 1108ff9ef578SJohan Hedberg break; 110930dc78e1SJohan Hedberg case DISCOVERY_RESOLVING: 111030dc78e1SJohan Hedberg break; 1111ff9ef578SJohan Hedberg case DISCOVERY_STOPPING: 1112ff9ef578SJohan Hedberg break; 1113ff9ef578SJohan Hedberg } 1114ff9ef578SJohan Hedberg } 1115ff9ef578SJohan Hedberg 11161f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev) 11171da177e4SLinus Torvalds { 111830883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1119b57c1a56SJohan Hedberg struct inquiry_entry *p, *n; 11201da177e4SLinus Torvalds 1121561aafbcSJohan Hedberg list_for_each_entry_safe(p, n, &cache->all, all) { 1122561aafbcSJohan Hedberg list_del(&p->all); 1123b57c1a56SJohan Hedberg kfree(p); 11241da177e4SLinus Torvalds } 1125561aafbcSJohan Hedberg 1126561aafbcSJohan Hedberg INIT_LIST_HEAD(&cache->unknown); 1127561aafbcSJohan Hedberg INIT_LIST_HEAD(&cache->resolve); 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 1130a8c5fb1aSGustavo Padovan struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, 1131a8c5fb1aSGustavo Padovan bdaddr_t *bdaddr) 11321da177e4SLinus Torvalds { 113330883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 11341da177e4SLinus Torvalds struct inquiry_entry *e; 11351da177e4SLinus Torvalds 11366ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, bdaddr); 11371da177e4SLinus Torvalds 1138561aafbcSJohan Hedberg list_for_each_entry(e, &cache->all, all) { 11391da177e4SLinus Torvalds if (!bacmp(&e->data.bdaddr, bdaddr)) 11401da177e4SLinus Torvalds return e; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 1143b57c1a56SJohan Hedberg return NULL; 1144b57c1a56SJohan Hedberg } 1145b57c1a56SJohan Hedberg 1146561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, 1147561aafbcSJohan Hedberg bdaddr_t *bdaddr) 1148561aafbcSJohan Hedberg { 114930883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1150561aafbcSJohan Hedberg struct inquiry_entry *e; 1151561aafbcSJohan Hedberg 11526ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, bdaddr); 1153561aafbcSJohan Hedberg 1154561aafbcSJohan Hedberg list_for_each_entry(e, &cache->unknown, list) { 1155561aafbcSJohan Hedberg if (!bacmp(&e->data.bdaddr, bdaddr)) 1156561aafbcSJohan Hedberg return e; 1157561aafbcSJohan Hedberg } 1158561aafbcSJohan Hedberg 1159561aafbcSJohan Hedberg return NULL; 1160561aafbcSJohan Hedberg } 1161561aafbcSJohan Hedberg 116230dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, 116330dc78e1SJohan Hedberg bdaddr_t *bdaddr, 116430dc78e1SJohan Hedberg int state) 116530dc78e1SJohan Hedberg { 116630dc78e1SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 116730dc78e1SJohan Hedberg struct inquiry_entry *e; 116830dc78e1SJohan Hedberg 11696ed93dc6SAndrei Emeltchenko BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); 117030dc78e1SJohan Hedberg 117130dc78e1SJohan Hedberg list_for_each_entry(e, &cache->resolve, list) { 117230dc78e1SJohan Hedberg if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) 117330dc78e1SJohan Hedberg return e; 117430dc78e1SJohan Hedberg if (!bacmp(&e->data.bdaddr, bdaddr)) 117530dc78e1SJohan Hedberg return e; 117630dc78e1SJohan Hedberg } 117730dc78e1SJohan Hedberg 117830dc78e1SJohan Hedberg return NULL; 117930dc78e1SJohan Hedberg } 118030dc78e1SJohan Hedberg 1181a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, 1182a3d4e20aSJohan Hedberg struct inquiry_entry *ie) 1183a3d4e20aSJohan Hedberg { 1184a3d4e20aSJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1185a3d4e20aSJohan Hedberg struct list_head *pos = &cache->resolve; 1186a3d4e20aSJohan Hedberg struct inquiry_entry *p; 1187a3d4e20aSJohan Hedberg 1188a3d4e20aSJohan Hedberg list_del(&ie->list); 1189a3d4e20aSJohan Hedberg 1190a3d4e20aSJohan Hedberg list_for_each_entry(p, &cache->resolve, list) { 1191a3d4e20aSJohan Hedberg if (p->name_state != NAME_PENDING && 1192a3d4e20aSJohan Hedberg abs(p->data.rssi) >= abs(ie->data.rssi)) 1193a3d4e20aSJohan Hedberg break; 1194a3d4e20aSJohan Hedberg pos = &p->list; 1195a3d4e20aSJohan Hedberg } 1196a3d4e20aSJohan Hedberg 1197a3d4e20aSJohan Hedberg list_add(&ie->list, pos); 1198a3d4e20aSJohan Hedberg } 1199a3d4e20aSJohan Hedberg 1200af58925cSMarcel Holtmann u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, 1201af58925cSMarcel Holtmann bool name_known) 12021da177e4SLinus Torvalds { 120330883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 120470f23020SAndrei Emeltchenko struct inquiry_entry *ie; 1205af58925cSMarcel Holtmann u32 flags = 0; 12061da177e4SLinus Torvalds 12076ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, &data->bdaddr); 12081da177e4SLinus Torvalds 12096928a924SJohan Hedberg hci_remove_remote_oob_data(hdev, &data->bdaddr, BDADDR_BREDR); 12102b2fec4dSSzymon Janc 1211af58925cSMarcel Holtmann if (!data->ssp_mode) 1212af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; 1213388fc8faSJohan Hedberg 121470f23020SAndrei Emeltchenko ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); 1215a3d4e20aSJohan Hedberg if (ie) { 1216af58925cSMarcel Holtmann if (!ie->data.ssp_mode) 1217af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; 1218388fc8faSJohan Hedberg 1219a3d4e20aSJohan Hedberg if (ie->name_state == NAME_NEEDED && 1220a3d4e20aSJohan Hedberg data->rssi != ie->data.rssi) { 1221a3d4e20aSJohan Hedberg ie->data.rssi = data->rssi; 1222a3d4e20aSJohan Hedberg hci_inquiry_cache_update_resolve(hdev, ie); 1223a3d4e20aSJohan Hedberg } 1224a3d4e20aSJohan Hedberg 1225561aafbcSJohan Hedberg goto update; 1226a3d4e20aSJohan Hedberg } 1227561aafbcSJohan Hedberg 12281da177e4SLinus Torvalds /* Entry not in the cache. Add new one. */ 122927f70f3eSJohan Hedberg ie = kzalloc(sizeof(*ie), GFP_KERNEL); 1230af58925cSMarcel Holtmann if (!ie) { 1231af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_CONFIRM_NAME; 1232af58925cSMarcel Holtmann goto done; 1233af58925cSMarcel Holtmann } 123470f23020SAndrei Emeltchenko 1235561aafbcSJohan Hedberg list_add(&ie->all, &cache->all); 1236561aafbcSJohan Hedberg 1237561aafbcSJohan Hedberg if (name_known) { 1238561aafbcSJohan Hedberg ie->name_state = NAME_KNOWN; 1239561aafbcSJohan Hedberg } else { 1240561aafbcSJohan Hedberg ie->name_state = NAME_NOT_KNOWN; 1241561aafbcSJohan Hedberg list_add(&ie->list, &cache->unknown); 1242561aafbcSJohan Hedberg } 1243561aafbcSJohan Hedberg 1244561aafbcSJohan Hedberg update: 1245561aafbcSJohan Hedberg if (name_known && ie->name_state != NAME_KNOWN && 1246561aafbcSJohan Hedberg ie->name_state != NAME_PENDING) { 1247561aafbcSJohan Hedberg ie->name_state = NAME_KNOWN; 1248561aafbcSJohan Hedberg list_del(&ie->list); 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 125170f23020SAndrei Emeltchenko memcpy(&ie->data, data, sizeof(*data)); 125270f23020SAndrei Emeltchenko ie->timestamp = jiffies; 12531da177e4SLinus Torvalds cache->timestamp = jiffies; 12543175405bSJohan Hedberg 12553175405bSJohan Hedberg if (ie->name_state == NAME_NOT_KNOWN) 1256af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_CONFIRM_NAME; 12573175405bSJohan Hedberg 1258af58925cSMarcel Holtmann done: 1259af58925cSMarcel Holtmann return flags; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) 12631da177e4SLinus Torvalds { 126430883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 12651da177e4SLinus Torvalds struct inquiry_info *info = (struct inquiry_info *) buf; 12661da177e4SLinus Torvalds struct inquiry_entry *e; 12671da177e4SLinus Torvalds int copied = 0; 12681da177e4SLinus Torvalds 1269561aafbcSJohan Hedberg list_for_each_entry(e, &cache->all, all) { 12701da177e4SLinus Torvalds struct inquiry_data *data = &e->data; 1271b57c1a56SJohan Hedberg 1272b57c1a56SJohan Hedberg if (copied >= num) 1273b57c1a56SJohan Hedberg break; 1274b57c1a56SJohan Hedberg 12751da177e4SLinus Torvalds bacpy(&info->bdaddr, &data->bdaddr); 12761da177e4SLinus Torvalds info->pscan_rep_mode = data->pscan_rep_mode; 12771da177e4SLinus Torvalds info->pscan_period_mode = data->pscan_period_mode; 12781da177e4SLinus Torvalds info->pscan_mode = data->pscan_mode; 12791da177e4SLinus Torvalds memcpy(info->dev_class, data->dev_class, 3); 12801da177e4SLinus Torvalds info->clock_offset = data->clock_offset; 1281b57c1a56SJohan Hedberg 12821da177e4SLinus Torvalds info++; 1283b57c1a56SJohan Hedberg copied++; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds BT_DBG("cache %p, copied %d", cache, copied); 12871da177e4SLinus Torvalds return copied; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 129042c6b129SJohan Hedberg static void hci_inq_req(struct hci_request *req, unsigned long opt) 12911da177e4SLinus Torvalds { 12921da177e4SLinus Torvalds struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; 129342c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 12941da177e4SLinus Torvalds struct hci_cp_inquiry cp; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds if (test_bit(HCI_INQUIRY, &hdev->flags)) 12991da177e4SLinus Torvalds return; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds /* Start Inquiry */ 13021da177e4SLinus Torvalds memcpy(&cp.lap, &ir->lap, 3); 13031da177e4SLinus Torvalds cp.length = ir->length; 13041da177e4SLinus Torvalds cp.num_rsp = ir->num_rsp; 130542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds int hci_inquiry(void __user *arg) 13091da177e4SLinus Torvalds { 13101da177e4SLinus Torvalds __u8 __user *ptr = arg; 13111da177e4SLinus Torvalds struct hci_inquiry_req ir; 13121da177e4SLinus Torvalds struct hci_dev *hdev; 13131da177e4SLinus Torvalds int err = 0, do_inquiry = 0, max_rsp; 13141da177e4SLinus Torvalds long timeo; 13151da177e4SLinus Torvalds __u8 *buf; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds if (copy_from_user(&ir, ptr, sizeof(ir))) 13181da177e4SLinus Torvalds return -EFAULT; 13191da177e4SLinus Torvalds 13205a08ecceSAndrei Emeltchenko hdev = hci_dev_get(ir.dev_id); 13215a08ecceSAndrei Emeltchenko if (!hdev) 13221da177e4SLinus Torvalds return -ENODEV; 13231da177e4SLinus Torvalds 1324d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 13250736cfa8SMarcel Holtmann err = -EBUSY; 13260736cfa8SMarcel Holtmann goto done; 13270736cfa8SMarcel Holtmann } 13280736cfa8SMarcel Holtmann 1329d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 1330fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 1331fee746b0SMarcel Holtmann goto done; 1332fee746b0SMarcel Holtmann } 1333fee746b0SMarcel Holtmann 13345b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) { 13355b69bef5SMarcel Holtmann err = -EOPNOTSUPP; 13365b69bef5SMarcel Holtmann goto done; 13375b69bef5SMarcel Holtmann } 13385b69bef5SMarcel Holtmann 1339d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 134056f87901SJohan Hedberg err = -EOPNOTSUPP; 134156f87901SJohan Hedberg goto done; 134256f87901SJohan Hedberg } 134356f87901SJohan Hedberg 134409fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 13451da177e4SLinus Torvalds if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 1346a8c5fb1aSGustavo Padovan inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { 13471f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 13481da177e4SLinus Torvalds do_inquiry = 1; 13491da177e4SLinus Torvalds } 135009fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 13511da177e4SLinus Torvalds 135204837f64SMarcel Holtmann timeo = ir.length * msecs_to_jiffies(2000); 135370f23020SAndrei Emeltchenko 135470f23020SAndrei Emeltchenko if (do_inquiry) { 135501178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, 135601178cd4SJohan Hedberg timeo); 135770f23020SAndrei Emeltchenko if (err < 0) 13581da177e4SLinus Torvalds goto done; 13593e13fa1eSAndre Guedes 13603e13fa1eSAndre Guedes /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is 13613e13fa1eSAndre Guedes * cleared). If it is interrupted by a signal, return -EINTR. 13623e13fa1eSAndre Guedes */ 136374316201SNeilBrown if (wait_on_bit(&hdev->flags, HCI_INQUIRY, 13643e13fa1eSAndre Guedes TASK_INTERRUPTIBLE)) 13653e13fa1eSAndre Guedes return -EINTR; 136670f23020SAndrei Emeltchenko } 13671da177e4SLinus Torvalds 13688fc9ced3SGustavo Padovan /* for unlimited number of responses we will use buffer with 13698fc9ced3SGustavo Padovan * 255 entries 13708fc9ced3SGustavo Padovan */ 13711da177e4SLinus Torvalds max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds /* cache_dump can't sleep. Therefore we allocate temp buffer and then 13741da177e4SLinus Torvalds * copy it to the user space. 13751da177e4SLinus Torvalds */ 137670f23020SAndrei Emeltchenko buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); 137770f23020SAndrei Emeltchenko if (!buf) { 13781da177e4SLinus Torvalds err = -ENOMEM; 13791da177e4SLinus Torvalds goto done; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 138209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 13831da177e4SLinus Torvalds ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); 138409fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds BT_DBG("num_rsp %d", ir.num_rsp); 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (!copy_to_user(ptr, &ir, sizeof(ir))) { 13891da177e4SLinus Torvalds ptr += sizeof(ir); 13901da177e4SLinus Torvalds if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * 13911da177e4SLinus Torvalds ir.num_rsp)) 13921da177e4SLinus Torvalds err = -EFAULT; 13931da177e4SLinus Torvalds } else 13941da177e4SLinus Torvalds err = -EFAULT; 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds kfree(buf); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds done: 13991da177e4SLinus Torvalds hci_dev_put(hdev); 14001da177e4SLinus Torvalds return err; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 1403cbed0ca1SJohan Hedberg static int hci_dev_do_open(struct hci_dev *hdev) 14041da177e4SLinus Torvalds { 14051da177e4SLinus Torvalds int ret = 0; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds BT_DBG("%s %p", hdev->name, hdev); 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds hci_req_lock(hdev); 14101da177e4SLinus Torvalds 1411d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 141294324962SJohan Hovold ret = -ENODEV; 141394324962SJohan Hovold goto done; 141494324962SJohan Hovold } 141594324962SJohan Hovold 1416d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 1417d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) { 1418a5c8f270SMarcel Holtmann /* Check for rfkill but allow the HCI setup stage to 1419a5c8f270SMarcel Holtmann * proceed (which in itself doesn't cause any RF activity). 1420bf543036SJohan Hedberg */ 1421d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_RFKILLED)) { 1422611b30f7SMarcel Holtmann ret = -ERFKILL; 1423611b30f7SMarcel Holtmann goto done; 1424611b30f7SMarcel Holtmann } 1425611b30f7SMarcel Holtmann 1426a5c8f270SMarcel Holtmann /* Check for valid public address or a configured static 1427a5c8f270SMarcel Holtmann * random adddress, but let the HCI setup proceed to 1428a5c8f270SMarcel Holtmann * be able to determine if there is a public address 1429a5c8f270SMarcel Holtmann * or not. 1430a5c8f270SMarcel Holtmann * 1431c6beca0eSMarcel Holtmann * In case of user channel usage, it is not important 1432c6beca0eSMarcel Holtmann * if a public address or static random address is 1433c6beca0eSMarcel Holtmann * available. 1434c6beca0eSMarcel Holtmann * 1435a5c8f270SMarcel Holtmann * This check is only valid for BR/EDR controllers 1436a5c8f270SMarcel Holtmann * since AMP controllers do not have an address. 1437a5c8f270SMarcel Holtmann */ 1438d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 1439c6beca0eSMarcel Holtmann hdev->dev_type == HCI_BREDR && 1440a5c8f270SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) && 1441a5c8f270SMarcel Holtmann !bacmp(&hdev->static_addr, BDADDR_ANY)) { 1442a5c8f270SMarcel Holtmann ret = -EADDRNOTAVAIL; 1443a5c8f270SMarcel Holtmann goto done; 1444a5c8f270SMarcel Holtmann } 1445a5c8f270SMarcel Holtmann } 1446a5c8f270SMarcel Holtmann 14471da177e4SLinus Torvalds if (test_bit(HCI_UP, &hdev->flags)) { 14481da177e4SLinus Torvalds ret = -EALREADY; 14491da177e4SLinus Torvalds goto done; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds if (hdev->open(hdev)) { 14531da177e4SLinus Torvalds ret = -EIO; 14541da177e4SLinus Torvalds goto done; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 1457e9ca8bf1SMarcel Holtmann set_bit(HCI_RUNNING, &hdev->flags); 14584a3f95b7SMarcel Holtmann hci_notify(hdev, HCI_DEV_OPEN); 14594a3f95b7SMarcel Holtmann 14601da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 14611da177e4SLinus Torvalds set_bit(HCI_INIT, &hdev->flags); 1462f41c70c4SMarcel Holtmann 1463d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SETUP)) { 1464e131d74aSMarcel Holtmann hci_sock_dev_event(hdev, HCI_DEV_SETUP); 1465e131d74aSMarcel Holtmann 1466af202f84SMarcel Holtmann if (hdev->setup) 1467f41c70c4SMarcel Holtmann ret = hdev->setup(hdev); 1468f41c70c4SMarcel Holtmann 1469af202f84SMarcel Holtmann /* The transport driver can set these quirks before 1470af202f84SMarcel Holtmann * creating the HCI device or in its setup callback. 1471af202f84SMarcel Holtmann * 1472af202f84SMarcel Holtmann * In case any of them is set, the controller has to 1473af202f84SMarcel Holtmann * start up as unconfigured. 1474af202f84SMarcel Holtmann */ 1475eb1904f4SMarcel Holtmann if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || 1476eb1904f4SMarcel Holtmann test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks)) 1477a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_UNCONFIGURED); 1478f41c70c4SMarcel Holtmann 14790ebca7d6SMarcel Holtmann /* For an unconfigured controller it is required to 14800ebca7d6SMarcel Holtmann * read at least the version information provided by 14810ebca7d6SMarcel Holtmann * the Read Local Version Information command. 14820ebca7d6SMarcel Holtmann * 14830ebca7d6SMarcel Holtmann * If the set_bdaddr driver callback is provided, then 14840ebca7d6SMarcel Holtmann * also the original Bluetooth public device address 14850ebca7d6SMarcel Holtmann * will be read using the Read BD Address command. 14860ebca7d6SMarcel Holtmann */ 1487d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 14880ebca7d6SMarcel Holtmann ret = __hci_unconf_init(hdev); 148989bc22d2SMarcel Holtmann } 149089bc22d2SMarcel Holtmann 1491d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_CONFIG)) { 14929713c17bSMarcel Holtmann /* If public address change is configured, ensure that 14939713c17bSMarcel Holtmann * the address gets programmed. If the driver does not 14949713c17bSMarcel Holtmann * support changing the public address, fail the power 14959713c17bSMarcel Holtmann * on procedure. 149624c457e2SMarcel Holtmann */ 14979713c17bSMarcel Holtmann if (bacmp(&hdev->public_addr, BDADDR_ANY) && 14989713c17bSMarcel Holtmann hdev->set_bdaddr) 149924c457e2SMarcel Holtmann ret = hdev->set_bdaddr(hdev, &hdev->public_addr); 150024c457e2SMarcel Holtmann else 150124c457e2SMarcel Holtmann ret = -EADDRNOTAVAIL; 150224c457e2SMarcel Holtmann } 150324c457e2SMarcel Holtmann 1504f41c70c4SMarcel Holtmann if (!ret) { 1505d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 150698a63aafSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 15072177bab5SJohan Hedberg ret = __hci_init(hdev); 150898a63aafSMarcel Holtmann if (!ret && hdev->post_init) 150998a63aafSMarcel Holtmann ret = hdev->post_init(hdev); 151098a63aafSMarcel Holtmann } 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15137e995b9eSMarcel Holtmann /* If the HCI Reset command is clearing all diagnostic settings, 15147e995b9eSMarcel Holtmann * then they need to be reprogrammed after the init procedure 15157e995b9eSMarcel Holtmann * completed. 15167e995b9eSMarcel Holtmann */ 15177e995b9eSMarcel Holtmann if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && 15187e995b9eSMarcel Holtmann hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag) 15197e995b9eSMarcel Holtmann ret = hdev->set_diag(hdev, true); 15207e995b9eSMarcel Holtmann 1521f41c70c4SMarcel Holtmann clear_bit(HCI_INIT, &hdev->flags); 1522f41c70c4SMarcel Holtmann 15231da177e4SLinus Torvalds if (!ret) { 15241da177e4SLinus Torvalds hci_dev_hold(hdev); 1525a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 15261da177e4SLinus Torvalds set_bit(HCI_UP, &hdev->flags); 15271da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_UP); 1528d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 1529d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG) && 1530d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1531d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 15321514b892SMarcel Holtmann hdev->dev_type == HCI_BREDR) { 153309fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 1534744cf19eSJohan Hedberg mgmt_powered(hdev, 1); 153509fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 153656e5cb86SJohan Hedberg } 15371da177e4SLinus Torvalds } else { 15381da177e4SLinus Torvalds /* Init failed, cleanup */ 15393eff45eaSGustavo F. Padovan flush_work(&hdev->tx_work); 1540c347b765SGustavo F. Padovan flush_work(&hdev->cmd_work); 1541b78752ccSMarcel Holtmann flush_work(&hdev->rx_work); 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 15441da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds if (hdev->flush) 15471da177e4SLinus Torvalds hdev->flush(hdev); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds if (hdev->sent_cmd) { 15501da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 15511da177e4SLinus Torvalds hdev->sent_cmd = NULL; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds 1554e9ca8bf1SMarcel Holtmann clear_bit(HCI_RUNNING, &hdev->flags); 15554a3f95b7SMarcel Holtmann hci_notify(hdev, HCI_DEV_CLOSE); 15564a3f95b7SMarcel Holtmann 15571da177e4SLinus Torvalds hdev->close(hdev); 1558fee746b0SMarcel Holtmann hdev->flags &= BIT(HCI_RAW); 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds done: 15621da177e4SLinus Torvalds hci_req_unlock(hdev); 15631da177e4SLinus Torvalds return ret; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 1566cbed0ca1SJohan Hedberg /* ---- HCI ioctl helpers ---- */ 1567cbed0ca1SJohan Hedberg 1568cbed0ca1SJohan Hedberg int hci_dev_open(__u16 dev) 1569cbed0ca1SJohan Hedberg { 1570cbed0ca1SJohan Hedberg struct hci_dev *hdev; 1571cbed0ca1SJohan Hedberg int err; 1572cbed0ca1SJohan Hedberg 1573cbed0ca1SJohan Hedberg hdev = hci_dev_get(dev); 1574cbed0ca1SJohan Hedberg if (!hdev) 1575cbed0ca1SJohan Hedberg return -ENODEV; 1576cbed0ca1SJohan Hedberg 15774a964404SMarcel Holtmann /* Devices that are marked as unconfigured can only be powered 1578fee746b0SMarcel Holtmann * up as user channel. Trying to bring them up as normal devices 1579fee746b0SMarcel Holtmann * will result into a failure. Only user channel operation is 1580fee746b0SMarcel Holtmann * possible. 1581fee746b0SMarcel Holtmann * 1582fee746b0SMarcel Holtmann * When this function is called for a user channel, the flag 1583fee746b0SMarcel Holtmann * HCI_USER_CHANNEL will be set first before attempting to 1584fee746b0SMarcel Holtmann * open the device. 1585fee746b0SMarcel Holtmann */ 1586d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1587d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 1588fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 1589fee746b0SMarcel Holtmann goto done; 1590fee746b0SMarcel Holtmann } 1591fee746b0SMarcel Holtmann 1592e1d08f40SJohan Hedberg /* We need to ensure that no other power on/off work is pending 1593e1d08f40SJohan Hedberg * before proceeding to call hci_dev_do_open. This is 1594e1d08f40SJohan Hedberg * particularly important if the setup procedure has not yet 1595e1d08f40SJohan Hedberg * completed. 1596e1d08f40SJohan Hedberg */ 1597a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) 1598e1d08f40SJohan Hedberg cancel_delayed_work(&hdev->power_off); 1599e1d08f40SJohan Hedberg 1600a5c8f270SMarcel Holtmann /* After this call it is guaranteed that the setup procedure 1601a5c8f270SMarcel Holtmann * has finished. This means that error conditions like RFKILL 1602a5c8f270SMarcel Holtmann * or no valid public or static random address apply. 1603a5c8f270SMarcel Holtmann */ 1604e1d08f40SJohan Hedberg flush_workqueue(hdev->req_workqueue); 1605e1d08f40SJohan Hedberg 160612aa4f0aSMarcel Holtmann /* For controllers not using the management interface and that 1607b6ae8457SJohan Hedberg * are brought up using legacy ioctl, set the HCI_BONDABLE bit 160812aa4f0aSMarcel Holtmann * so that pairing works for them. Once the management interface 160912aa4f0aSMarcel Holtmann * is in use this bit will be cleared again and userspace has 161012aa4f0aSMarcel Holtmann * to explicitly enable it. 161112aa4f0aSMarcel Holtmann */ 1612d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 1613d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_MGMT)) 1614a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_BONDABLE); 161512aa4f0aSMarcel Holtmann 1616cbed0ca1SJohan Hedberg err = hci_dev_do_open(hdev); 1617cbed0ca1SJohan Hedberg 1618fee746b0SMarcel Holtmann done: 1619cbed0ca1SJohan Hedberg hci_dev_put(hdev); 1620cbed0ca1SJohan Hedberg return err; 1621cbed0ca1SJohan Hedberg } 1622cbed0ca1SJohan Hedberg 1623d7347f3cSJohan Hedberg /* This function requires the caller holds hdev->lock */ 1624d7347f3cSJohan Hedberg static void hci_pend_le_actions_clear(struct hci_dev *hdev) 1625d7347f3cSJohan Hedberg { 1626d7347f3cSJohan Hedberg struct hci_conn_params *p; 1627d7347f3cSJohan Hedberg 1628f161dd41SJohan Hedberg list_for_each_entry(p, &hdev->le_conn_params, list) { 1629f161dd41SJohan Hedberg if (p->conn) { 1630f161dd41SJohan Hedberg hci_conn_drop(p->conn); 1631f8aaf9b6SJohan Hedberg hci_conn_put(p->conn); 1632f161dd41SJohan Hedberg p->conn = NULL; 1633f161dd41SJohan Hedberg } 1634d7347f3cSJohan Hedberg list_del_init(&p->action); 1635f161dd41SJohan Hedberg } 1636d7347f3cSJohan Hedberg 1637d7347f3cSJohan Hedberg BT_DBG("All LE pending actions cleared"); 1638d7347f3cSJohan Hedberg } 1639d7347f3cSJohan Hedberg 16406b3cc1dbSSimon Fels int hci_dev_do_close(struct hci_dev *hdev) 16411da177e4SLinus Torvalds { 1642acc649c6SMarcel Holtmann bool auto_off; 1643acc649c6SMarcel Holtmann 16441da177e4SLinus Torvalds BT_DBG("%s %p", hdev->name, hdev); 16451da177e4SLinus Torvalds 1646d24d8144SGabriele Mazzotta if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && 1647867146a0SLoic Poulain !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 1648d24d8144SGabriele Mazzotta test_bit(HCI_UP, &hdev->flags)) { 1649a44fecbdSTedd Ho-Jeong An /* Execute vendor specific shutdown routine */ 1650a44fecbdSTedd Ho-Jeong An if (hdev->shutdown) 1651a44fecbdSTedd Ho-Jeong An hdev->shutdown(hdev); 1652a44fecbdSTedd Ho-Jeong An } 1653a44fecbdSTedd Ho-Jeong An 165478c04c0bSVinicius Costa Gomes cancel_delayed_work(&hdev->power_off); 165578c04c0bSVinicius Costa Gomes 16561da177e4SLinus Torvalds hci_req_cancel(hdev, ENODEV); 16571da177e4SLinus Torvalds hci_req_lock(hdev); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { 166065cc2b49SMarcel Holtmann cancel_delayed_work_sync(&hdev->cmd_timer); 16611da177e4SLinus Torvalds hci_req_unlock(hdev); 16621da177e4SLinus Torvalds return 0; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16653eff45eaSGustavo F. Padovan /* Flush RX and TX works */ 16663eff45eaSGustavo F. Padovan flush_work(&hdev->tx_work); 1667b78752ccSMarcel Holtmann flush_work(&hdev->rx_work); 16681da177e4SLinus Torvalds 166916ab91abSJohan Hedberg if (hdev->discov_timeout > 0) { 1670e0f9309fSJohan Hedberg cancel_delayed_work(&hdev->discov_off); 167116ab91abSJohan Hedberg hdev->discov_timeout = 0; 1672a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 1673a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 167416ab91abSJohan Hedberg } 167516ab91abSJohan Hedberg 1676a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) 16777d78525dSJohan Hedberg cancel_delayed_work(&hdev->service_cache); 16787d78525dSJohan Hedberg 16797ba8b4beSAndre Guedes cancel_delayed_work_sync(&hdev->le_scan_disable); 16802d28cfe7SJakub Pawlowski cancel_delayed_work_sync(&hdev->le_scan_restart); 16814518bb0fSJohan Hedberg 1682d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_MGMT)) 1683d6bfd59cSJohan Hedberg cancel_delayed_work_sync(&hdev->rpa_expired); 16847ba8b4beSAndre Guedes 16855d900e46SFlorian Grandel if (hdev->adv_instance_timeout) { 16865d900e46SFlorian Grandel cancel_delayed_work_sync(&hdev->adv_instance_expire); 16875d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 16885d900e46SFlorian Grandel } 16895d900e46SFlorian Grandel 169076727c02SJohan Hedberg /* Avoid potential lockdep warnings from the *_flush() calls by 169176727c02SJohan Hedberg * ensuring the workqueue is empty up front. 169276727c02SJohan Hedberg */ 169376727c02SJohan Hedberg drain_workqueue(hdev->workqueue); 169476727c02SJohan Hedberg 169509fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 16961aeb9c65SJohan Hedberg 16978f502f84SJohan Hedberg hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 16988f502f84SJohan Hedberg 1699acc649c6SMarcel Holtmann auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); 1700acc649c6SMarcel Holtmann 1701acc649c6SMarcel Holtmann if (!auto_off && hdev->dev_type == HCI_BREDR) 17021aeb9c65SJohan Hedberg mgmt_powered(hdev, 0); 17031aeb9c65SJohan Hedberg 17041f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 1705d7347f3cSJohan Hedberg hci_pend_le_actions_clear(hdev); 1706f161dd41SJohan Hedberg hci_conn_hash_flush(hdev); 170709fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 17081da177e4SLinus Torvalds 170964dae967SMarcel Holtmann smp_unregister(hdev); 171064dae967SMarcel Holtmann 17111da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_DOWN); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds if (hdev->flush) 17141da177e4SLinus Torvalds hdev->flush(hdev); 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* Reset device */ 17171da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 17181da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 1719acc649c6SMarcel Holtmann if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) && 1720acc649c6SMarcel Holtmann !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 17211da177e4SLinus Torvalds set_bit(HCI_INIT, &hdev->flags); 172201178cd4SJohan Hedberg __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); 17231da177e4SLinus Torvalds clear_bit(HCI_INIT, &hdev->flags); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 1726c347b765SGustavo F. Padovan /* flush cmd work */ 1727c347b765SGustavo F. Padovan flush_work(&hdev->cmd_work); 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds /* Drop queues */ 17301da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 17311da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 17321da177e4SLinus Torvalds skb_queue_purge(&hdev->raw_q); 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds /* Drop last sent command */ 17351da177e4SLinus Torvalds if (hdev->sent_cmd) { 173665cc2b49SMarcel Holtmann cancel_delayed_work_sync(&hdev->cmd_timer); 17371da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 17381da177e4SLinus Torvalds hdev->sent_cmd = NULL; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 1741e9ca8bf1SMarcel Holtmann clear_bit(HCI_RUNNING, &hdev->flags); 17424a3f95b7SMarcel Holtmann hci_notify(hdev, HCI_DEV_CLOSE); 17434a3f95b7SMarcel Holtmann 17441da177e4SLinus Torvalds /* After this point our queues are empty 17451da177e4SLinus Torvalds * and no tasks are scheduled. */ 17461da177e4SLinus Torvalds hdev->close(hdev); 17471da177e4SLinus Torvalds 174835b973c9SJohan Hedberg /* Clear flags */ 1749fee746b0SMarcel Holtmann hdev->flags &= BIT(HCI_RAW); 1750eacb44dfSMarcel Holtmann hci_dev_clear_volatile_flags(hdev); 175135b973c9SJohan Hedberg 1752ced5c338SAndrei Emeltchenko /* Controller radio is available but is currently powered down */ 1753536619e8SMarcel Holtmann hdev->amp_status = AMP_STATUS_POWERED_DOWN; 1754ced5c338SAndrei Emeltchenko 1755e59fda8dSJohan Hedberg memset(hdev->eir, 0, sizeof(hdev->eir)); 175609b3c3fbSJohan Hedberg memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); 17577a4cd51dSMarcel Holtmann bacpy(&hdev->random_addr, BDADDR_ANY); 1758e59fda8dSJohan Hedberg 17591da177e4SLinus Torvalds hci_req_unlock(hdev); 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds hci_dev_put(hdev); 17621da177e4SLinus Torvalds return 0; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds int hci_dev_close(__u16 dev) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds struct hci_dev *hdev; 17681da177e4SLinus Torvalds int err; 17691da177e4SLinus Torvalds 177070f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 177170f23020SAndrei Emeltchenko if (!hdev) 17721da177e4SLinus Torvalds return -ENODEV; 17738ee56540SMarcel Holtmann 1774d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 17750736cfa8SMarcel Holtmann err = -EBUSY; 17760736cfa8SMarcel Holtmann goto done; 17770736cfa8SMarcel Holtmann } 17780736cfa8SMarcel Holtmann 1779a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) 17808ee56540SMarcel Holtmann cancel_delayed_work(&hdev->power_off); 17818ee56540SMarcel Holtmann 17821da177e4SLinus Torvalds err = hci_dev_do_close(hdev); 17838ee56540SMarcel Holtmann 17840736cfa8SMarcel Holtmann done: 17851da177e4SLinus Torvalds hci_dev_put(hdev); 17861da177e4SLinus Torvalds return err; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 17895c912495SMarcel Holtmann static int hci_dev_do_reset(struct hci_dev *hdev) 17901da177e4SLinus Torvalds { 17915c912495SMarcel Holtmann int ret; 17921da177e4SLinus Torvalds 17935c912495SMarcel Holtmann BT_DBG("%s %p", hdev->name, hdev); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds hci_req_lock(hdev); 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds /* Drop queues */ 17981da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 17991da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 18001da177e4SLinus Torvalds 180176727c02SJohan Hedberg /* Avoid potential lockdep warnings from the *_flush() calls by 180276727c02SJohan Hedberg * ensuring the workqueue is empty up front. 180376727c02SJohan Hedberg */ 180476727c02SJohan Hedberg drain_workqueue(hdev->workqueue); 180576727c02SJohan Hedberg 180609fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 18071f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 18081da177e4SLinus Torvalds hci_conn_hash_flush(hdev); 180909fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds if (hdev->flush) 18121da177e4SLinus Torvalds hdev->flush(hdev); 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 18156ed58ec5SVille Tervo hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; 18161da177e4SLinus Torvalds 181701178cd4SJohan Hedberg ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds hci_req_unlock(hdev); 18201da177e4SLinus Torvalds return ret; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18235c912495SMarcel Holtmann int hci_dev_reset(__u16 dev) 18245c912495SMarcel Holtmann { 18255c912495SMarcel Holtmann struct hci_dev *hdev; 18265c912495SMarcel Holtmann int err; 18275c912495SMarcel Holtmann 18285c912495SMarcel Holtmann hdev = hci_dev_get(dev); 18295c912495SMarcel Holtmann if (!hdev) 18305c912495SMarcel Holtmann return -ENODEV; 18315c912495SMarcel Holtmann 18325c912495SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 18335c912495SMarcel Holtmann err = -ENETDOWN; 18345c912495SMarcel Holtmann goto done; 18355c912495SMarcel Holtmann } 18365c912495SMarcel Holtmann 1837d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 18385c912495SMarcel Holtmann err = -EBUSY; 18395c912495SMarcel Holtmann goto done; 18405c912495SMarcel Holtmann } 18415c912495SMarcel Holtmann 1842d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 18435c912495SMarcel Holtmann err = -EOPNOTSUPP; 18445c912495SMarcel Holtmann goto done; 18455c912495SMarcel Holtmann } 18465c912495SMarcel Holtmann 18475c912495SMarcel Holtmann err = hci_dev_do_reset(hdev); 18485c912495SMarcel Holtmann 18495c912495SMarcel Holtmann done: 18505c912495SMarcel Holtmann hci_dev_put(hdev); 18515c912495SMarcel Holtmann return err; 18525c912495SMarcel Holtmann } 18535c912495SMarcel Holtmann 18541da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev) 18551da177e4SLinus Torvalds { 18561da177e4SLinus Torvalds struct hci_dev *hdev; 18571da177e4SLinus Torvalds int ret = 0; 18581da177e4SLinus Torvalds 185970f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 186070f23020SAndrei Emeltchenko if (!hdev) 18611da177e4SLinus Torvalds return -ENODEV; 18621da177e4SLinus Torvalds 1863d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 18640736cfa8SMarcel Holtmann ret = -EBUSY; 18650736cfa8SMarcel Holtmann goto done; 18660736cfa8SMarcel Holtmann } 18670736cfa8SMarcel Holtmann 1868d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 1869fee746b0SMarcel Holtmann ret = -EOPNOTSUPP; 1870fee746b0SMarcel Holtmann goto done; 1871fee746b0SMarcel Holtmann } 1872fee746b0SMarcel Holtmann 18731da177e4SLinus Torvalds memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); 18741da177e4SLinus Torvalds 18750736cfa8SMarcel Holtmann done: 18761da177e4SLinus Torvalds hci_dev_put(hdev); 18771da177e4SLinus Torvalds return ret; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 1880123abc08SJohan Hedberg static void hci_update_scan_state(struct hci_dev *hdev, u8 scan) 1881123abc08SJohan Hedberg { 1882bc6d2d04SJohan Hedberg bool conn_changed, discov_changed; 1883123abc08SJohan Hedberg 1884123abc08SJohan Hedberg BT_DBG("%s scan 0x%02x", hdev->name, scan); 1885123abc08SJohan Hedberg 1886123abc08SJohan Hedberg if ((scan & SCAN_PAGE)) 1887238be788SMarcel Holtmann conn_changed = !hci_dev_test_and_set_flag(hdev, 1888238be788SMarcel Holtmann HCI_CONNECTABLE); 1889123abc08SJohan Hedberg else 1890a69d8927SMarcel Holtmann conn_changed = hci_dev_test_and_clear_flag(hdev, 1891a69d8927SMarcel Holtmann HCI_CONNECTABLE); 1892123abc08SJohan Hedberg 1893bc6d2d04SJohan Hedberg if ((scan & SCAN_INQUIRY)) { 1894238be788SMarcel Holtmann discov_changed = !hci_dev_test_and_set_flag(hdev, 1895238be788SMarcel Holtmann HCI_DISCOVERABLE); 1896bc6d2d04SJohan Hedberg } else { 1897a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1898a69d8927SMarcel Holtmann discov_changed = hci_dev_test_and_clear_flag(hdev, 1899a69d8927SMarcel Holtmann HCI_DISCOVERABLE); 1900bc6d2d04SJohan Hedberg } 1901bc6d2d04SJohan Hedberg 1902d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_MGMT)) 1903123abc08SJohan Hedberg return; 1904123abc08SJohan Hedberg 1905bc6d2d04SJohan Hedberg if (conn_changed || discov_changed) { 1906bc6d2d04SJohan Hedberg /* In case this was disabled through mgmt */ 1907a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); 1908bc6d2d04SJohan Hedberg 1909d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1910bc6d2d04SJohan Hedberg mgmt_update_adv_data(hdev); 1911bc6d2d04SJohan Hedberg 1912123abc08SJohan Hedberg mgmt_new_settings(hdev); 1913123abc08SJohan Hedberg } 1914bc6d2d04SJohan Hedberg } 1915123abc08SJohan Hedberg 19161da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg) 19171da177e4SLinus Torvalds { 19181da177e4SLinus Torvalds struct hci_dev *hdev; 19191da177e4SLinus Torvalds struct hci_dev_req dr; 19201da177e4SLinus Torvalds int err = 0; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds if (copy_from_user(&dr, arg, sizeof(dr))) 19231da177e4SLinus Torvalds return -EFAULT; 19241da177e4SLinus Torvalds 192570f23020SAndrei Emeltchenko hdev = hci_dev_get(dr.dev_id); 192670f23020SAndrei Emeltchenko if (!hdev) 19271da177e4SLinus Torvalds return -ENODEV; 19281da177e4SLinus Torvalds 1929d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 19300736cfa8SMarcel Holtmann err = -EBUSY; 19310736cfa8SMarcel Holtmann goto done; 19320736cfa8SMarcel Holtmann } 19330736cfa8SMarcel Holtmann 1934d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 1935fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 1936fee746b0SMarcel Holtmann goto done; 1937fee746b0SMarcel Holtmann } 1938fee746b0SMarcel Holtmann 19395b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) { 19405b69bef5SMarcel Holtmann err = -EOPNOTSUPP; 19415b69bef5SMarcel Holtmann goto done; 19425b69bef5SMarcel Holtmann } 19435b69bef5SMarcel Holtmann 1944d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 194556f87901SJohan Hedberg err = -EOPNOTSUPP; 194656f87901SJohan Hedberg goto done; 194756f87901SJohan Hedberg } 194856f87901SJohan Hedberg 19491da177e4SLinus Torvalds switch (cmd) { 19501da177e4SLinus Torvalds case HCISETAUTH: 195101178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 19525f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19531da177e4SLinus Torvalds break; 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds case HCISETENCRYPT: 19561da177e4SLinus Torvalds if (!lmp_encrypt_capable(hdev)) { 19571da177e4SLinus Torvalds err = -EOPNOTSUPP; 19581da177e4SLinus Torvalds break; 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds if (!test_bit(HCI_AUTH, &hdev->flags)) { 19621da177e4SLinus Torvalds /* Auth must be enabled first */ 196301178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 19645f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19651da177e4SLinus Torvalds if (err) 19661da177e4SLinus Torvalds break; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 196901178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, 19705f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19711da177e4SLinus Torvalds break; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds case HCISETSCAN: 197401178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, 19755f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 197691a668b0SJohan Hedberg 1977bc6d2d04SJohan Hedberg /* Ensure that the connectable and discoverable states 1978bc6d2d04SJohan Hedberg * get correctly modified as this was a non-mgmt change. 197991a668b0SJohan Hedberg */ 1980123abc08SJohan Hedberg if (!err) 1981123abc08SJohan Hedberg hci_update_scan_state(hdev, dr.dev_opt); 19821da177e4SLinus Torvalds break; 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds case HCISETLINKPOL: 198501178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, 19865f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19871da177e4SLinus Torvalds break; 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds case HCISETLINKMODE: 1990e4e8e37cSMarcel Holtmann hdev->link_mode = ((__u16) dr.dev_opt) & 1991e4e8e37cSMarcel Holtmann (HCI_LM_MASTER | HCI_LM_ACCEPT); 1992e4e8e37cSMarcel Holtmann break; 1993e4e8e37cSMarcel Holtmann 1994e4e8e37cSMarcel Holtmann case HCISETPTYPE: 1995e4e8e37cSMarcel Holtmann hdev->pkt_type = (__u16) dr.dev_opt; 19961da177e4SLinus Torvalds break; 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds case HCISETACLMTU: 19991da177e4SLinus Torvalds hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); 20001da177e4SLinus Torvalds hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); 20011da177e4SLinus Torvalds break; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds case HCISETSCOMTU: 20041da177e4SLinus Torvalds hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); 20051da177e4SLinus Torvalds hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); 20061da177e4SLinus Torvalds break; 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds default: 20091da177e4SLinus Torvalds err = -EINVAL; 20101da177e4SLinus Torvalds break; 20111da177e4SLinus Torvalds } 2012e4e8e37cSMarcel Holtmann 20130736cfa8SMarcel Holtmann done: 20141da177e4SLinus Torvalds hci_dev_put(hdev); 20151da177e4SLinus Torvalds return err; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg) 20191da177e4SLinus Torvalds { 20208035ded4SLuiz Augusto von Dentz struct hci_dev *hdev; 20211da177e4SLinus Torvalds struct hci_dev_list_req *dl; 20221da177e4SLinus Torvalds struct hci_dev_req *dr; 20231da177e4SLinus Torvalds int n = 0, size, err; 20241da177e4SLinus Torvalds __u16 dev_num; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds if (get_user(dev_num, (__u16 __user *) arg)) 20271da177e4SLinus Torvalds return -EFAULT; 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) 20301da177e4SLinus Torvalds return -EINVAL; 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds size = sizeof(*dl) + dev_num * sizeof(*dr); 20331da177e4SLinus Torvalds 203470f23020SAndrei Emeltchenko dl = kzalloc(size, GFP_KERNEL); 203570f23020SAndrei Emeltchenko if (!dl) 20361da177e4SLinus Torvalds return -ENOMEM; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds dr = dl->dev_req; 20391da177e4SLinus Torvalds 2040f20d09d5SGustavo F. Padovan read_lock(&hci_dev_list_lock); 20418035ded4SLuiz Augusto von Dentz list_for_each_entry(hdev, &hci_dev_list, list) { 20422e84d8dbSMarcel Holtmann unsigned long flags = hdev->flags; 2043c542a06cSJohan Hedberg 20442e84d8dbSMarcel Holtmann /* When the auto-off is configured it means the transport 20452e84d8dbSMarcel Holtmann * is running, but in that case still indicate that the 20462e84d8dbSMarcel Holtmann * device is actually down. 20472e84d8dbSMarcel Holtmann */ 2048d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) 20492e84d8dbSMarcel Holtmann flags &= ~BIT(HCI_UP); 2050c542a06cSJohan Hedberg 20511da177e4SLinus Torvalds (dr + n)->dev_id = hdev->id; 20522e84d8dbSMarcel Holtmann (dr + n)->dev_opt = flags; 2053c542a06cSJohan Hedberg 20541da177e4SLinus Torvalds if (++n >= dev_num) 20551da177e4SLinus Torvalds break; 20561da177e4SLinus Torvalds } 2057f20d09d5SGustavo F. Padovan read_unlock(&hci_dev_list_lock); 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds dl->dev_num = n; 20601da177e4SLinus Torvalds size = sizeof(*dl) + n * sizeof(*dr); 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds err = copy_to_user(arg, dl, size); 20631da177e4SLinus Torvalds kfree(dl); 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds return err ? -EFAULT : 0; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg) 20691da177e4SLinus Torvalds { 20701da177e4SLinus Torvalds struct hci_dev *hdev; 20711da177e4SLinus Torvalds struct hci_dev_info di; 20722e84d8dbSMarcel Holtmann unsigned long flags; 20731da177e4SLinus Torvalds int err = 0; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds if (copy_from_user(&di, arg, sizeof(di))) 20761da177e4SLinus Torvalds return -EFAULT; 20771da177e4SLinus Torvalds 207870f23020SAndrei Emeltchenko hdev = hci_dev_get(di.dev_id); 207970f23020SAndrei Emeltchenko if (!hdev) 20801da177e4SLinus Torvalds return -ENODEV; 20811da177e4SLinus Torvalds 20822e84d8dbSMarcel Holtmann /* When the auto-off is configured it means the transport 20832e84d8dbSMarcel Holtmann * is running, but in that case still indicate that the 20842e84d8dbSMarcel Holtmann * device is actually down. 20852e84d8dbSMarcel Holtmann */ 2086d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) 20872e84d8dbSMarcel Holtmann flags = hdev->flags & ~BIT(HCI_UP); 20882e84d8dbSMarcel Holtmann else 20892e84d8dbSMarcel Holtmann flags = hdev->flags; 2090c542a06cSJohan Hedberg 20911da177e4SLinus Torvalds strcpy(di.name, hdev->name); 20921da177e4SLinus Torvalds di.bdaddr = hdev->bdaddr; 209360f2a3edSMarcel Holtmann di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); 20942e84d8dbSMarcel Holtmann di.flags = flags; 20951da177e4SLinus Torvalds di.pkt_type = hdev->pkt_type; 2096572c7f84SJohan Hedberg if (lmp_bredr_capable(hdev)) { 20971da177e4SLinus Torvalds di.acl_mtu = hdev->acl_mtu; 20981da177e4SLinus Torvalds di.acl_pkts = hdev->acl_pkts; 20991da177e4SLinus Torvalds di.sco_mtu = hdev->sco_mtu; 21001da177e4SLinus Torvalds di.sco_pkts = hdev->sco_pkts; 2101572c7f84SJohan Hedberg } else { 2102572c7f84SJohan Hedberg di.acl_mtu = hdev->le_mtu; 2103572c7f84SJohan Hedberg di.acl_pkts = hdev->le_pkts; 2104572c7f84SJohan Hedberg di.sco_mtu = 0; 2105572c7f84SJohan Hedberg di.sco_pkts = 0; 2106572c7f84SJohan Hedberg } 21071da177e4SLinus Torvalds di.link_policy = hdev->link_policy; 21081da177e4SLinus Torvalds di.link_mode = hdev->link_mode; 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); 21111da177e4SLinus Torvalds memcpy(&di.features, &hdev->features, sizeof(di.features)); 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds if (copy_to_user(arg, &di, sizeof(di))) 21141da177e4SLinus Torvalds err = -EFAULT; 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds hci_dev_put(hdev); 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds return err; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* ---- Interface to HCI drivers ---- */ 21221da177e4SLinus Torvalds 2123611b30f7SMarcel Holtmann static int hci_rfkill_set_block(void *data, bool blocked) 2124611b30f7SMarcel Holtmann { 2125611b30f7SMarcel Holtmann struct hci_dev *hdev = data; 2126611b30f7SMarcel Holtmann 2127611b30f7SMarcel Holtmann BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); 2128611b30f7SMarcel Holtmann 2129d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 21300736cfa8SMarcel Holtmann return -EBUSY; 21310736cfa8SMarcel Holtmann 21325e130367SJohan Hedberg if (blocked) { 2133a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_RFKILLED); 2134d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 2135d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) 2136611b30f7SMarcel Holtmann hci_dev_do_close(hdev); 21375e130367SJohan Hedberg } else { 2138a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_RFKILLED); 21395e130367SJohan Hedberg } 2140611b30f7SMarcel Holtmann 2141611b30f7SMarcel Holtmann return 0; 2142611b30f7SMarcel Holtmann } 2143611b30f7SMarcel Holtmann 2144611b30f7SMarcel Holtmann static const struct rfkill_ops hci_rfkill_ops = { 2145611b30f7SMarcel Holtmann .set_block = hci_rfkill_set_block, 2146611b30f7SMarcel Holtmann }; 2147611b30f7SMarcel Holtmann 2148ab81cbf9SJohan Hedberg static void hci_power_on(struct work_struct *work) 2149ab81cbf9SJohan Hedberg { 2150ab81cbf9SJohan Hedberg struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); 215196570ffcSJohan Hedberg int err; 2152ab81cbf9SJohan Hedberg 2153ab81cbf9SJohan Hedberg BT_DBG("%s", hdev->name); 2154ab81cbf9SJohan Hedberg 2155cbed0ca1SJohan Hedberg err = hci_dev_do_open(hdev); 215696570ffcSJohan Hedberg if (err < 0) { 21573ad67582SJaganath Kanakkassery hci_dev_lock(hdev); 215896570ffcSJohan Hedberg mgmt_set_powered_failed(hdev, err); 21593ad67582SJaganath Kanakkassery hci_dev_unlock(hdev); 2160ab81cbf9SJohan Hedberg return; 216196570ffcSJohan Hedberg } 2162ab81cbf9SJohan Hedberg 2163a5c8f270SMarcel Holtmann /* During the HCI setup phase, a few error conditions are 2164a5c8f270SMarcel Holtmann * ignored and they need to be checked now. If they are still 2165a5c8f270SMarcel Holtmann * valid, it is important to turn the device back off. 2166a5c8f270SMarcel Holtmann */ 2167d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_RFKILLED) || 2168d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || 2169a5c8f270SMarcel Holtmann (hdev->dev_type == HCI_BREDR && 2170a5c8f270SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) && 2171a5c8f270SMarcel Holtmann !bacmp(&hdev->static_addr, BDADDR_ANY))) { 2172a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_AUTO_OFF); 2173bf543036SJohan Hedberg hci_dev_do_close(hdev); 2174d7a5a11dSMarcel Holtmann } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { 217519202573SJohan Hedberg queue_delayed_work(hdev->req_workqueue, &hdev->power_off, 217619202573SJohan Hedberg HCI_AUTO_OFF_TIMEOUT); 2177bf543036SJohan Hedberg } 2178ab81cbf9SJohan Hedberg 2179a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { 21804a964404SMarcel Holtmann /* For unconfigured devices, set the HCI_RAW flag 21814a964404SMarcel Holtmann * so that userspace can easily identify them. 21824a964404SMarcel Holtmann */ 2183d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 21844a964404SMarcel Holtmann set_bit(HCI_RAW, &hdev->flags); 21850602a8adSMarcel Holtmann 21860602a8adSMarcel Holtmann /* For fully configured devices, this will send 21870602a8adSMarcel Holtmann * the Index Added event. For unconfigured devices, 21880602a8adSMarcel Holtmann * it will send Unconfigued Index Added event. 21890602a8adSMarcel Holtmann * 21900602a8adSMarcel Holtmann * Devices with HCI_QUIRK_RAW_DEVICE are ignored 21910602a8adSMarcel Holtmann * and no event will be send. 21920602a8adSMarcel Holtmann */ 2193744cf19eSJohan Hedberg mgmt_index_added(hdev); 2194a69d8927SMarcel Holtmann } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { 21955ea234d3SMarcel Holtmann /* When the controller is now configured, then it 21965ea234d3SMarcel Holtmann * is important to clear the HCI_RAW flag. 21975ea234d3SMarcel Holtmann */ 2198d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 21995ea234d3SMarcel Holtmann clear_bit(HCI_RAW, &hdev->flags); 22005ea234d3SMarcel Holtmann 2201d603b76bSMarcel Holtmann /* Powering on the controller with HCI_CONFIG set only 2202d603b76bSMarcel Holtmann * happens with the transition from unconfigured to 2203d603b76bSMarcel Holtmann * configured. This will send the Index Added event. 2204d603b76bSMarcel Holtmann */ 2205d603b76bSMarcel Holtmann mgmt_index_added(hdev); 2206ab81cbf9SJohan Hedberg } 2207ab81cbf9SJohan Hedberg } 2208ab81cbf9SJohan Hedberg 2209ab81cbf9SJohan Hedberg static void hci_power_off(struct work_struct *work) 2210ab81cbf9SJohan Hedberg { 22113243553fSJohan Hedberg struct hci_dev *hdev = container_of(work, struct hci_dev, 22123243553fSJohan Hedberg power_off.work); 2213ab81cbf9SJohan Hedberg 2214ab81cbf9SJohan Hedberg BT_DBG("%s", hdev->name); 2215ab81cbf9SJohan Hedberg 22168ee56540SMarcel Holtmann hci_dev_do_close(hdev); 2217ab81cbf9SJohan Hedberg } 2218ab81cbf9SJohan Hedberg 2219c7741d16SMarcel Holtmann static void hci_error_reset(struct work_struct *work) 2220c7741d16SMarcel Holtmann { 2221c7741d16SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, error_reset); 2222c7741d16SMarcel Holtmann 2223c7741d16SMarcel Holtmann BT_DBG("%s", hdev->name); 2224c7741d16SMarcel Holtmann 2225c7741d16SMarcel Holtmann if (hdev->hw_error) 2226c7741d16SMarcel Holtmann hdev->hw_error(hdev, hdev->hw_error_code); 2227c7741d16SMarcel Holtmann else 2228c7741d16SMarcel Holtmann BT_ERR("%s hardware error 0x%2.2x", hdev->name, 2229c7741d16SMarcel Holtmann hdev->hw_error_code); 2230c7741d16SMarcel Holtmann 2231c7741d16SMarcel Holtmann if (hci_dev_do_close(hdev)) 2232c7741d16SMarcel Holtmann return; 2233c7741d16SMarcel Holtmann 2234c7741d16SMarcel Holtmann hci_dev_do_open(hdev); 2235c7741d16SMarcel Holtmann } 2236c7741d16SMarcel Holtmann 223716ab91abSJohan Hedberg static void hci_discov_off(struct work_struct *work) 223816ab91abSJohan Hedberg { 223916ab91abSJohan Hedberg struct hci_dev *hdev; 224016ab91abSJohan Hedberg 224116ab91abSJohan Hedberg hdev = container_of(work, struct hci_dev, discov_off.work); 224216ab91abSJohan Hedberg 224316ab91abSJohan Hedberg BT_DBG("%s", hdev->name); 224416ab91abSJohan Hedberg 2245d1967ff8SMarcel Holtmann mgmt_discoverable_timeout(hdev); 224616ab91abSJohan Hedberg } 224716ab91abSJohan Hedberg 22485d900e46SFlorian Grandel static void hci_adv_timeout_expire(struct work_struct *work) 22495d900e46SFlorian Grandel { 22505d900e46SFlorian Grandel struct hci_dev *hdev; 22515d900e46SFlorian Grandel 22525d900e46SFlorian Grandel hdev = container_of(work, struct hci_dev, adv_instance_expire.work); 22535d900e46SFlorian Grandel 22545d900e46SFlorian Grandel BT_DBG("%s", hdev->name); 22555d900e46SFlorian Grandel 22565d900e46SFlorian Grandel mgmt_adv_timeout_expired(hdev); 22575d900e46SFlorian Grandel } 22585d900e46SFlorian Grandel 225935f7498aSJohan Hedberg void hci_uuids_clear(struct hci_dev *hdev) 22602aeb9a1aSJohan Hedberg { 22614821002cSJohan Hedberg struct bt_uuid *uuid, *tmp; 22622aeb9a1aSJohan Hedberg 22634821002cSJohan Hedberg list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { 22644821002cSJohan Hedberg list_del(&uuid->list); 22652aeb9a1aSJohan Hedberg kfree(uuid); 22662aeb9a1aSJohan Hedberg } 22672aeb9a1aSJohan Hedberg } 22682aeb9a1aSJohan Hedberg 226935f7498aSJohan Hedberg void hci_link_keys_clear(struct hci_dev *hdev) 227055ed8ca1SJohan Hedberg { 227155ed8ca1SJohan Hedberg struct link_key *key; 227255ed8ca1SJohan Hedberg 22730378b597SJohan Hedberg list_for_each_entry_rcu(key, &hdev->link_keys, list) { 22740378b597SJohan Hedberg list_del_rcu(&key->list); 22750378b597SJohan Hedberg kfree_rcu(key, rcu); 227655ed8ca1SJohan Hedberg } 227755ed8ca1SJohan Hedberg } 227855ed8ca1SJohan Hedberg 227935f7498aSJohan Hedberg void hci_smp_ltks_clear(struct hci_dev *hdev) 2280b899efafSVinicius Costa Gomes { 2281970d0f1bSJohan Hedberg struct smp_ltk *k; 2282b899efafSVinicius Costa Gomes 2283970d0f1bSJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 2284970d0f1bSJohan Hedberg list_del_rcu(&k->list); 2285970d0f1bSJohan Hedberg kfree_rcu(k, rcu); 2286b899efafSVinicius Costa Gomes } 2287b899efafSVinicius Costa Gomes } 2288b899efafSVinicius Costa Gomes 2289970c4e46SJohan Hedberg void hci_smp_irks_clear(struct hci_dev *hdev) 2290970c4e46SJohan Hedberg { 2291adae20cbSJohan Hedberg struct smp_irk *k; 2292970c4e46SJohan Hedberg 2293adae20cbSJohan Hedberg list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) { 2294adae20cbSJohan Hedberg list_del_rcu(&k->list); 2295adae20cbSJohan Hedberg kfree_rcu(k, rcu); 2296970c4e46SJohan Hedberg } 2297970c4e46SJohan Hedberg } 2298970c4e46SJohan Hedberg 229955ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) 230055ed8ca1SJohan Hedberg { 230155ed8ca1SJohan Hedberg struct link_key *k; 230255ed8ca1SJohan Hedberg 23030378b597SJohan Hedberg rcu_read_lock(); 23040378b597SJohan Hedberg list_for_each_entry_rcu(k, &hdev->link_keys, list) { 23050378b597SJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) == 0) { 23060378b597SJohan Hedberg rcu_read_unlock(); 230755ed8ca1SJohan Hedberg return k; 23080378b597SJohan Hedberg } 23090378b597SJohan Hedberg } 23100378b597SJohan Hedberg rcu_read_unlock(); 231155ed8ca1SJohan Hedberg 231255ed8ca1SJohan Hedberg return NULL; 231355ed8ca1SJohan Hedberg } 231455ed8ca1SJohan Hedberg 2315745c0ce3SVishal Agarwal static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, 2316d25e28abSJohan Hedberg u8 key_type, u8 old_key_type) 2317d25e28abSJohan Hedberg { 2318d25e28abSJohan Hedberg /* Legacy key */ 2319d25e28abSJohan Hedberg if (key_type < 0x03) 2320745c0ce3SVishal Agarwal return true; 2321d25e28abSJohan Hedberg 2322d25e28abSJohan Hedberg /* Debug keys are insecure so don't store them persistently */ 2323d25e28abSJohan Hedberg if (key_type == HCI_LK_DEBUG_COMBINATION) 2324745c0ce3SVishal Agarwal return false; 2325d25e28abSJohan Hedberg 2326d25e28abSJohan Hedberg /* Changed combination key and there's no previous one */ 2327d25e28abSJohan Hedberg if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) 2328745c0ce3SVishal Agarwal return false; 2329d25e28abSJohan Hedberg 2330d25e28abSJohan Hedberg /* Security mode 3 case */ 2331d25e28abSJohan Hedberg if (!conn) 2332745c0ce3SVishal Agarwal return true; 2333d25e28abSJohan Hedberg 2334e3befab9SJohan Hedberg /* BR/EDR key derived using SC from an LE link */ 2335e3befab9SJohan Hedberg if (conn->type == LE_LINK) 2336e3befab9SJohan Hedberg return true; 2337e3befab9SJohan Hedberg 2338d25e28abSJohan Hedberg /* Neither local nor remote side had no-bonding as requirement */ 2339d25e28abSJohan Hedberg if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) 2340745c0ce3SVishal Agarwal return true; 2341d25e28abSJohan Hedberg 2342d25e28abSJohan Hedberg /* Local side had dedicated bonding as requirement */ 2343d25e28abSJohan Hedberg if (conn->auth_type == 0x02 || conn->auth_type == 0x03) 2344745c0ce3SVishal Agarwal return true; 2345d25e28abSJohan Hedberg 2346d25e28abSJohan Hedberg /* Remote side had dedicated bonding as requirement */ 2347d25e28abSJohan Hedberg if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) 2348745c0ce3SVishal Agarwal return true; 2349d25e28abSJohan Hedberg 2350d25e28abSJohan Hedberg /* If none of the above criteria match, then don't store the key 2351d25e28abSJohan Hedberg * persistently */ 2352745c0ce3SVishal Agarwal return false; 2353d25e28abSJohan Hedberg } 2354d25e28abSJohan Hedberg 2355e804d25dSJohan Hedberg static u8 ltk_role(u8 type) 235698a0b845SJohan Hedberg { 2357e804d25dSJohan Hedberg if (type == SMP_LTK) 2358e804d25dSJohan Hedberg return HCI_ROLE_MASTER; 235998a0b845SJohan Hedberg 2360e804d25dSJohan Hedberg return HCI_ROLE_SLAVE; 236198a0b845SJohan Hedberg } 236298a0b845SJohan Hedberg 2363f3a73d97SJohan Hedberg struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, 2364e804d25dSJohan Hedberg u8 addr_type, u8 role) 236575d262c2SVinicius Costa Gomes { 2366c9839a11SVinicius Costa Gomes struct smp_ltk *k; 236775d262c2SVinicius Costa Gomes 2368970d0f1bSJohan Hedberg rcu_read_lock(); 2369970d0f1bSJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 23705378bc56SJohan Hedberg if (addr_type != k->bdaddr_type || bacmp(bdaddr, &k->bdaddr)) 23715378bc56SJohan Hedberg continue; 23725378bc56SJohan Hedberg 2373923e2414SJohan Hedberg if (smp_ltk_is_sc(k) || ltk_role(k->type) == role) { 2374970d0f1bSJohan Hedberg rcu_read_unlock(); 237575d262c2SVinicius Costa Gomes return k; 2376970d0f1bSJohan Hedberg } 2377970d0f1bSJohan Hedberg } 2378970d0f1bSJohan Hedberg rcu_read_unlock(); 237975d262c2SVinicius Costa Gomes 238075d262c2SVinicius Costa Gomes return NULL; 238175d262c2SVinicius Costa Gomes } 238275d262c2SVinicius Costa Gomes 2383970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa) 2384970c4e46SJohan Hedberg { 2385970c4e46SJohan Hedberg struct smp_irk *irk; 2386970c4e46SJohan Hedberg 2387adae20cbSJohan Hedberg rcu_read_lock(); 2388adae20cbSJohan Hedberg list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { 2389adae20cbSJohan Hedberg if (!bacmp(&irk->rpa, rpa)) { 2390adae20cbSJohan Hedberg rcu_read_unlock(); 2391970c4e46SJohan Hedberg return irk; 2392970c4e46SJohan Hedberg } 2393adae20cbSJohan Hedberg } 2394970c4e46SJohan Hedberg 2395adae20cbSJohan Hedberg list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { 2396defce9e8SJohan Hedberg if (smp_irk_matches(hdev, irk->val, rpa)) { 2397970c4e46SJohan Hedberg bacpy(&irk->rpa, rpa); 2398adae20cbSJohan Hedberg rcu_read_unlock(); 2399970c4e46SJohan Hedberg return irk; 2400970c4e46SJohan Hedberg } 2401970c4e46SJohan Hedberg } 2402adae20cbSJohan Hedberg rcu_read_unlock(); 2403970c4e46SJohan Hedberg 2404970c4e46SJohan Hedberg return NULL; 2405970c4e46SJohan Hedberg } 2406970c4e46SJohan Hedberg 2407970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, 2408970c4e46SJohan Hedberg u8 addr_type) 2409970c4e46SJohan Hedberg { 2410970c4e46SJohan Hedberg struct smp_irk *irk; 2411970c4e46SJohan Hedberg 24126cfc9988SJohan Hedberg /* Identity Address must be public or static random */ 24136cfc9988SJohan Hedberg if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0) 24146cfc9988SJohan Hedberg return NULL; 24156cfc9988SJohan Hedberg 2416adae20cbSJohan Hedberg rcu_read_lock(); 2417adae20cbSJohan Hedberg list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { 2418970c4e46SJohan Hedberg if (addr_type == irk->addr_type && 2419adae20cbSJohan Hedberg bacmp(bdaddr, &irk->bdaddr) == 0) { 2420adae20cbSJohan Hedberg rcu_read_unlock(); 2421970c4e46SJohan Hedberg return irk; 2422970c4e46SJohan Hedberg } 2423adae20cbSJohan Hedberg } 2424adae20cbSJohan Hedberg rcu_read_unlock(); 2425970c4e46SJohan Hedberg 2426970c4e46SJohan Hedberg return NULL; 2427970c4e46SJohan Hedberg } 2428970c4e46SJohan Hedberg 2429567fa2aaSJohan Hedberg struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, 24307652ff6aSJohan Hedberg bdaddr_t *bdaddr, u8 *val, u8 type, 24317652ff6aSJohan Hedberg u8 pin_len, bool *persistent) 243255ed8ca1SJohan Hedberg { 243355ed8ca1SJohan Hedberg struct link_key *key, *old_key; 2434745c0ce3SVishal Agarwal u8 old_key_type; 243555ed8ca1SJohan Hedberg 243655ed8ca1SJohan Hedberg old_key = hci_find_link_key(hdev, bdaddr); 243755ed8ca1SJohan Hedberg if (old_key) { 243855ed8ca1SJohan Hedberg old_key_type = old_key->type; 243955ed8ca1SJohan Hedberg key = old_key; 244055ed8ca1SJohan Hedberg } else { 244112adcf3aSJohan Hedberg old_key_type = conn ? conn->key_type : 0xff; 24420a14ab41SJohan Hedberg key = kzalloc(sizeof(*key), GFP_KERNEL); 244355ed8ca1SJohan Hedberg if (!key) 2444567fa2aaSJohan Hedberg return NULL; 24450378b597SJohan Hedberg list_add_rcu(&key->list, &hdev->link_keys); 244655ed8ca1SJohan Hedberg } 244755ed8ca1SJohan Hedberg 24486ed93dc6SAndrei Emeltchenko BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); 244955ed8ca1SJohan Hedberg 2450d25e28abSJohan Hedberg /* Some buggy controller combinations generate a changed 2451d25e28abSJohan Hedberg * combination key for legacy pairing even when there's no 2452d25e28abSJohan Hedberg * previous key */ 2453d25e28abSJohan Hedberg if (type == HCI_LK_CHANGED_COMBINATION && 2454a8c5fb1aSGustavo Padovan (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { 2455d25e28abSJohan Hedberg type = HCI_LK_COMBINATION; 2456655fe6ecSJohan Hedberg if (conn) 2457655fe6ecSJohan Hedberg conn->key_type = type; 2458655fe6ecSJohan Hedberg } 2459d25e28abSJohan Hedberg 246055ed8ca1SJohan Hedberg bacpy(&key->bdaddr, bdaddr); 24619b3b4460SAndrei Emeltchenko memcpy(key->val, val, HCI_LINK_KEY_SIZE); 246255ed8ca1SJohan Hedberg key->pin_len = pin_len; 246355ed8ca1SJohan Hedberg 2464b6020ba0SWaldemar Rymarkiewicz if (type == HCI_LK_CHANGED_COMBINATION) 246555ed8ca1SJohan Hedberg key->type = old_key_type; 24664748fed2SJohan Hedberg else 24674748fed2SJohan Hedberg key->type = type; 24684748fed2SJohan Hedberg 24697652ff6aSJohan Hedberg if (persistent) 24707652ff6aSJohan Hedberg *persistent = hci_persistent_key(hdev, conn, type, 24717652ff6aSJohan Hedberg old_key_type); 24724df378a1SJohan Hedberg 2473567fa2aaSJohan Hedberg return key; 247455ed8ca1SJohan Hedberg } 247555ed8ca1SJohan Hedberg 2476ca9142b8SJohan Hedberg struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, 247735d70271SJohan Hedberg u8 addr_type, u8 type, u8 authenticated, 2478fe39c7b2SMarcel Holtmann u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand) 247975d262c2SVinicius Costa Gomes { 2480c9839a11SVinicius Costa Gomes struct smp_ltk *key, *old_key; 2481e804d25dSJohan Hedberg u8 role = ltk_role(type); 248275d262c2SVinicius Costa Gomes 2483f3a73d97SJohan Hedberg old_key = hci_find_ltk(hdev, bdaddr, addr_type, role); 2484c9839a11SVinicius Costa Gomes if (old_key) 248575d262c2SVinicius Costa Gomes key = old_key; 2486c9839a11SVinicius Costa Gomes else { 24870a14ab41SJohan Hedberg key = kzalloc(sizeof(*key), GFP_KERNEL); 248875d262c2SVinicius Costa Gomes if (!key) 2489ca9142b8SJohan Hedberg return NULL; 2490970d0f1bSJohan Hedberg list_add_rcu(&key->list, &hdev->long_term_keys); 249175d262c2SVinicius Costa Gomes } 249275d262c2SVinicius Costa Gomes 249375d262c2SVinicius Costa Gomes bacpy(&key->bdaddr, bdaddr); 2494c9839a11SVinicius Costa Gomes key->bdaddr_type = addr_type; 2495c9839a11SVinicius Costa Gomes memcpy(key->val, tk, sizeof(key->val)); 2496c9839a11SVinicius Costa Gomes key->authenticated = authenticated; 2497c9839a11SVinicius Costa Gomes key->ediv = ediv; 2498fe39c7b2SMarcel Holtmann key->rand = rand; 2499c9839a11SVinicius Costa Gomes key->enc_size = enc_size; 2500c9839a11SVinicius Costa Gomes key->type = type; 250175d262c2SVinicius Costa Gomes 2502ca9142b8SJohan Hedberg return key; 250375d262c2SVinicius Costa Gomes } 250475d262c2SVinicius Costa Gomes 2505ca9142b8SJohan Hedberg struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, 2506ca9142b8SJohan Hedberg u8 addr_type, u8 val[16], bdaddr_t *rpa) 2507970c4e46SJohan Hedberg { 2508970c4e46SJohan Hedberg struct smp_irk *irk; 2509970c4e46SJohan Hedberg 2510970c4e46SJohan Hedberg irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type); 2511970c4e46SJohan Hedberg if (!irk) { 2512970c4e46SJohan Hedberg irk = kzalloc(sizeof(*irk), GFP_KERNEL); 2513970c4e46SJohan Hedberg if (!irk) 2514ca9142b8SJohan Hedberg return NULL; 2515970c4e46SJohan Hedberg 2516970c4e46SJohan Hedberg bacpy(&irk->bdaddr, bdaddr); 2517970c4e46SJohan Hedberg irk->addr_type = addr_type; 2518970c4e46SJohan Hedberg 2519adae20cbSJohan Hedberg list_add_rcu(&irk->list, &hdev->identity_resolving_keys); 2520970c4e46SJohan Hedberg } 2521970c4e46SJohan Hedberg 2522970c4e46SJohan Hedberg memcpy(irk->val, val, 16); 2523970c4e46SJohan Hedberg bacpy(&irk->rpa, rpa); 2524970c4e46SJohan Hedberg 2525ca9142b8SJohan Hedberg return irk; 2526970c4e46SJohan Hedberg } 2527970c4e46SJohan Hedberg 252855ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) 252955ed8ca1SJohan Hedberg { 253055ed8ca1SJohan Hedberg struct link_key *key; 253155ed8ca1SJohan Hedberg 253255ed8ca1SJohan Hedberg key = hci_find_link_key(hdev, bdaddr); 253355ed8ca1SJohan Hedberg if (!key) 253455ed8ca1SJohan Hedberg return -ENOENT; 253555ed8ca1SJohan Hedberg 25366ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 253755ed8ca1SJohan Hedberg 25380378b597SJohan Hedberg list_del_rcu(&key->list); 25390378b597SJohan Hedberg kfree_rcu(key, rcu); 254055ed8ca1SJohan Hedberg 254155ed8ca1SJohan Hedberg return 0; 254255ed8ca1SJohan Hedberg } 254355ed8ca1SJohan Hedberg 2544e0b2b27eSJohan Hedberg int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type) 2545b899efafSVinicius Costa Gomes { 2546970d0f1bSJohan Hedberg struct smp_ltk *k; 2547c51ffa0bSJohan Hedberg int removed = 0; 2548b899efafSVinicius Costa Gomes 2549970d0f1bSJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 2550e0b2b27eSJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type) 2551b899efafSVinicius Costa Gomes continue; 2552b899efafSVinicius Costa Gomes 25536ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 2554b899efafSVinicius Costa Gomes 2555970d0f1bSJohan Hedberg list_del_rcu(&k->list); 2556970d0f1bSJohan Hedberg kfree_rcu(k, rcu); 2557c51ffa0bSJohan Hedberg removed++; 2558b899efafSVinicius Costa Gomes } 2559b899efafSVinicius Costa Gomes 2560c51ffa0bSJohan Hedberg return removed ? 0 : -ENOENT; 2561b899efafSVinicius Costa Gomes } 2562b899efafSVinicius Costa Gomes 2563a7ec7338SJohan Hedberg void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type) 2564a7ec7338SJohan Hedberg { 2565adae20cbSJohan Hedberg struct smp_irk *k; 2566a7ec7338SJohan Hedberg 2567adae20cbSJohan Hedberg list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) { 2568a7ec7338SJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type) 2569a7ec7338SJohan Hedberg continue; 2570a7ec7338SJohan Hedberg 2571a7ec7338SJohan Hedberg BT_DBG("%s removing %pMR", hdev->name, bdaddr); 2572a7ec7338SJohan Hedberg 2573adae20cbSJohan Hedberg list_del_rcu(&k->list); 2574adae20cbSJohan Hedberg kfree_rcu(k, rcu); 2575a7ec7338SJohan Hedberg } 2576a7ec7338SJohan Hedberg } 2577a7ec7338SJohan Hedberg 257855e76b38SJohan Hedberg bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 257955e76b38SJohan Hedberg { 258055e76b38SJohan Hedberg struct smp_ltk *k; 25814ba9faf3SJohan Hedberg struct smp_irk *irk; 258255e76b38SJohan Hedberg u8 addr_type; 258355e76b38SJohan Hedberg 258455e76b38SJohan Hedberg if (type == BDADDR_BREDR) { 258555e76b38SJohan Hedberg if (hci_find_link_key(hdev, bdaddr)) 258655e76b38SJohan Hedberg return true; 258755e76b38SJohan Hedberg return false; 258855e76b38SJohan Hedberg } 258955e76b38SJohan Hedberg 259055e76b38SJohan Hedberg /* Convert to HCI addr type which struct smp_ltk uses */ 259155e76b38SJohan Hedberg if (type == BDADDR_LE_PUBLIC) 259255e76b38SJohan Hedberg addr_type = ADDR_LE_DEV_PUBLIC; 259355e76b38SJohan Hedberg else 259455e76b38SJohan Hedberg addr_type = ADDR_LE_DEV_RANDOM; 259555e76b38SJohan Hedberg 25964ba9faf3SJohan Hedberg irk = hci_get_irk(hdev, bdaddr, addr_type); 25974ba9faf3SJohan Hedberg if (irk) { 25984ba9faf3SJohan Hedberg bdaddr = &irk->bdaddr; 25994ba9faf3SJohan Hedberg addr_type = irk->addr_type; 26004ba9faf3SJohan Hedberg } 26014ba9faf3SJohan Hedberg 260255e76b38SJohan Hedberg rcu_read_lock(); 260355e76b38SJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 260487c8b28dSJohan Hedberg if (k->bdaddr_type == addr_type && !bacmp(bdaddr, &k->bdaddr)) { 260587c8b28dSJohan Hedberg rcu_read_unlock(); 260655e76b38SJohan Hedberg return true; 260755e76b38SJohan Hedberg } 260887c8b28dSJohan Hedberg } 260955e76b38SJohan Hedberg rcu_read_unlock(); 261055e76b38SJohan Hedberg 261155e76b38SJohan Hedberg return false; 261255e76b38SJohan Hedberg } 261355e76b38SJohan Hedberg 26146bd32326SVille Tervo /* HCI command timer function */ 261565cc2b49SMarcel Holtmann static void hci_cmd_timeout(struct work_struct *work) 26166bd32326SVille Tervo { 261765cc2b49SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, 261865cc2b49SMarcel Holtmann cmd_timer.work); 26196bd32326SVille Tervo 2620bda4f23aSAndrei Emeltchenko if (hdev->sent_cmd) { 2621bda4f23aSAndrei Emeltchenko struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; 2622bda4f23aSAndrei Emeltchenko u16 opcode = __le16_to_cpu(sent->opcode); 2623bda4f23aSAndrei Emeltchenko 2624bda4f23aSAndrei Emeltchenko BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); 2625bda4f23aSAndrei Emeltchenko } else { 26266bd32326SVille Tervo BT_ERR("%s command tx timeout", hdev->name); 2627bda4f23aSAndrei Emeltchenko } 2628bda4f23aSAndrei Emeltchenko 26296bd32326SVille Tervo atomic_set(&hdev->cmd_cnt, 1); 2630c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 26316bd32326SVille Tervo } 26326bd32326SVille Tervo 26332763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, 26346928a924SJohan Hedberg bdaddr_t *bdaddr, u8 bdaddr_type) 26352763eda6SSzymon Janc { 26362763eda6SSzymon Janc struct oob_data *data; 26372763eda6SSzymon Janc 26386928a924SJohan Hedberg list_for_each_entry(data, &hdev->remote_oob_data, list) { 26396928a924SJohan Hedberg if (bacmp(bdaddr, &data->bdaddr) != 0) 26406928a924SJohan Hedberg continue; 26416928a924SJohan Hedberg if (data->bdaddr_type != bdaddr_type) 26426928a924SJohan Hedberg continue; 26432763eda6SSzymon Janc return data; 26446928a924SJohan Hedberg } 26452763eda6SSzymon Janc 26462763eda6SSzymon Janc return NULL; 26472763eda6SSzymon Janc } 26482763eda6SSzymon Janc 26496928a924SJohan Hedberg int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, 26506928a924SJohan Hedberg u8 bdaddr_type) 26512763eda6SSzymon Janc { 26522763eda6SSzymon Janc struct oob_data *data; 26532763eda6SSzymon Janc 26546928a924SJohan Hedberg data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type); 26552763eda6SSzymon Janc if (!data) 26562763eda6SSzymon Janc return -ENOENT; 26572763eda6SSzymon Janc 26586928a924SJohan Hedberg BT_DBG("%s removing %pMR (%u)", hdev->name, bdaddr, bdaddr_type); 26592763eda6SSzymon Janc 26602763eda6SSzymon Janc list_del(&data->list); 26612763eda6SSzymon Janc kfree(data); 26622763eda6SSzymon Janc 26632763eda6SSzymon Janc return 0; 26642763eda6SSzymon Janc } 26652763eda6SSzymon Janc 266635f7498aSJohan Hedberg void hci_remote_oob_data_clear(struct hci_dev *hdev) 26672763eda6SSzymon Janc { 26682763eda6SSzymon Janc struct oob_data *data, *n; 26692763eda6SSzymon Janc 26702763eda6SSzymon Janc list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { 26712763eda6SSzymon Janc list_del(&data->list); 26722763eda6SSzymon Janc kfree(data); 26732763eda6SSzymon Janc } 26742763eda6SSzymon Janc } 26752763eda6SSzymon Janc 26760798872eSMarcel Holtmann int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, 26776928a924SJohan Hedberg u8 bdaddr_type, u8 *hash192, u8 *rand192, 267838da1703SJohan Hedberg u8 *hash256, u8 *rand256) 26790798872eSMarcel Holtmann { 26800798872eSMarcel Holtmann struct oob_data *data; 26810798872eSMarcel Holtmann 26826928a924SJohan Hedberg data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type); 26830798872eSMarcel Holtmann if (!data) { 26840a14ab41SJohan Hedberg data = kmalloc(sizeof(*data), GFP_KERNEL); 26850798872eSMarcel Holtmann if (!data) 26860798872eSMarcel Holtmann return -ENOMEM; 26870798872eSMarcel Holtmann 26880798872eSMarcel Holtmann bacpy(&data->bdaddr, bdaddr); 26896928a924SJohan Hedberg data->bdaddr_type = bdaddr_type; 26900798872eSMarcel Holtmann list_add(&data->list, &hdev->remote_oob_data); 26910798872eSMarcel Holtmann } 26920798872eSMarcel Holtmann 269381328d5cSJohan Hedberg if (hash192 && rand192) { 26940798872eSMarcel Holtmann memcpy(data->hash192, hash192, sizeof(data->hash192)); 269538da1703SJohan Hedberg memcpy(data->rand192, rand192, sizeof(data->rand192)); 2696f7697b16SMarcel Holtmann if (hash256 && rand256) 2697f7697b16SMarcel Holtmann data->present = 0x03; 269881328d5cSJohan Hedberg } else { 269981328d5cSJohan Hedberg memset(data->hash192, 0, sizeof(data->hash192)); 270081328d5cSJohan Hedberg memset(data->rand192, 0, sizeof(data->rand192)); 2701f7697b16SMarcel Holtmann if (hash256 && rand256) 2702f7697b16SMarcel Holtmann data->present = 0x02; 2703f7697b16SMarcel Holtmann else 2704f7697b16SMarcel Holtmann data->present = 0x00; 270581328d5cSJohan Hedberg } 27060798872eSMarcel Holtmann 270781328d5cSJohan Hedberg if (hash256 && rand256) { 27080798872eSMarcel Holtmann memcpy(data->hash256, hash256, sizeof(data->hash256)); 270938da1703SJohan Hedberg memcpy(data->rand256, rand256, sizeof(data->rand256)); 271081328d5cSJohan Hedberg } else { 271181328d5cSJohan Hedberg memset(data->hash256, 0, sizeof(data->hash256)); 271281328d5cSJohan Hedberg memset(data->rand256, 0, sizeof(data->rand256)); 2713f7697b16SMarcel Holtmann if (hash192 && rand192) 2714f7697b16SMarcel Holtmann data->present = 0x01; 271581328d5cSJohan Hedberg } 27160798872eSMarcel Holtmann 27176ed93dc6SAndrei Emeltchenko BT_DBG("%s for %pMR", hdev->name, bdaddr); 27182763eda6SSzymon Janc 27192763eda6SSzymon Janc return 0; 27202763eda6SSzymon Janc } 27212763eda6SSzymon Janc 2722d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2723d2609b34SFlorian Grandel struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance) 2724d2609b34SFlorian Grandel { 2725d2609b34SFlorian Grandel struct adv_info *adv_instance; 2726d2609b34SFlorian Grandel 2727d2609b34SFlorian Grandel list_for_each_entry(adv_instance, &hdev->adv_instances, list) { 2728d2609b34SFlorian Grandel if (adv_instance->instance == instance) 2729d2609b34SFlorian Grandel return adv_instance; 2730d2609b34SFlorian Grandel } 2731d2609b34SFlorian Grandel 2732d2609b34SFlorian Grandel return NULL; 2733d2609b34SFlorian Grandel } 2734d2609b34SFlorian Grandel 2735d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2736d2609b34SFlorian Grandel struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance) { 2737d2609b34SFlorian Grandel struct adv_info *cur_instance; 2738d2609b34SFlorian Grandel 2739d2609b34SFlorian Grandel cur_instance = hci_find_adv_instance(hdev, instance); 2740d2609b34SFlorian Grandel if (!cur_instance) 2741d2609b34SFlorian Grandel return NULL; 2742d2609b34SFlorian Grandel 2743d2609b34SFlorian Grandel if (cur_instance == list_last_entry(&hdev->adv_instances, 2744d2609b34SFlorian Grandel struct adv_info, list)) 2745d2609b34SFlorian Grandel return list_first_entry(&hdev->adv_instances, 2746d2609b34SFlorian Grandel struct adv_info, list); 2747d2609b34SFlorian Grandel else 2748d2609b34SFlorian Grandel return list_next_entry(cur_instance, list); 2749d2609b34SFlorian Grandel } 2750d2609b34SFlorian Grandel 2751d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2752d2609b34SFlorian Grandel int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance) 2753d2609b34SFlorian Grandel { 2754d2609b34SFlorian Grandel struct adv_info *adv_instance; 2755d2609b34SFlorian Grandel 2756d2609b34SFlorian Grandel adv_instance = hci_find_adv_instance(hdev, instance); 2757d2609b34SFlorian Grandel if (!adv_instance) 2758d2609b34SFlorian Grandel return -ENOENT; 2759d2609b34SFlorian Grandel 2760d2609b34SFlorian Grandel BT_DBG("%s removing %dMR", hdev->name, instance); 2761d2609b34SFlorian Grandel 27625d900e46SFlorian Grandel if (hdev->cur_adv_instance == instance && hdev->adv_instance_timeout) { 27635d900e46SFlorian Grandel cancel_delayed_work(&hdev->adv_instance_expire); 27645d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 27655d900e46SFlorian Grandel } 27665d900e46SFlorian Grandel 2767d2609b34SFlorian Grandel list_del(&adv_instance->list); 2768d2609b34SFlorian Grandel kfree(adv_instance); 2769d2609b34SFlorian Grandel 2770d2609b34SFlorian Grandel hdev->adv_instance_cnt--; 2771d2609b34SFlorian Grandel 2772d2609b34SFlorian Grandel return 0; 2773d2609b34SFlorian Grandel } 2774d2609b34SFlorian Grandel 2775d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2776d2609b34SFlorian Grandel void hci_adv_instances_clear(struct hci_dev *hdev) 2777d2609b34SFlorian Grandel { 2778d2609b34SFlorian Grandel struct adv_info *adv_instance, *n; 2779d2609b34SFlorian Grandel 27805d900e46SFlorian Grandel if (hdev->adv_instance_timeout) { 27815d900e46SFlorian Grandel cancel_delayed_work(&hdev->adv_instance_expire); 27825d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 27835d900e46SFlorian Grandel } 27845d900e46SFlorian Grandel 2785d2609b34SFlorian Grandel list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { 2786d2609b34SFlorian Grandel list_del(&adv_instance->list); 2787d2609b34SFlorian Grandel kfree(adv_instance); 2788d2609b34SFlorian Grandel } 2789d2609b34SFlorian Grandel 2790d2609b34SFlorian Grandel hdev->adv_instance_cnt = 0; 2791d2609b34SFlorian Grandel } 2792d2609b34SFlorian Grandel 2793d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2794d2609b34SFlorian Grandel int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags, 2795d2609b34SFlorian Grandel u16 adv_data_len, u8 *adv_data, 2796d2609b34SFlorian Grandel u16 scan_rsp_len, u8 *scan_rsp_data, 2797d2609b34SFlorian Grandel u16 timeout, u16 duration) 2798d2609b34SFlorian Grandel { 2799d2609b34SFlorian Grandel struct adv_info *adv_instance; 2800d2609b34SFlorian Grandel 2801d2609b34SFlorian Grandel adv_instance = hci_find_adv_instance(hdev, instance); 2802d2609b34SFlorian Grandel if (adv_instance) { 2803d2609b34SFlorian Grandel memset(adv_instance->adv_data, 0, 2804d2609b34SFlorian Grandel sizeof(adv_instance->adv_data)); 2805d2609b34SFlorian Grandel memset(adv_instance->scan_rsp_data, 0, 2806d2609b34SFlorian Grandel sizeof(adv_instance->scan_rsp_data)); 2807d2609b34SFlorian Grandel } else { 2808d2609b34SFlorian Grandel if (hdev->adv_instance_cnt >= HCI_MAX_ADV_INSTANCES || 2809d2609b34SFlorian Grandel instance < 1 || instance > HCI_MAX_ADV_INSTANCES) 2810d2609b34SFlorian Grandel return -EOVERFLOW; 2811d2609b34SFlorian Grandel 281239ecfad6SJohan Hedberg adv_instance = kzalloc(sizeof(*adv_instance), GFP_KERNEL); 2813d2609b34SFlorian Grandel if (!adv_instance) 2814d2609b34SFlorian Grandel return -ENOMEM; 2815d2609b34SFlorian Grandel 2816fffd38bcSFlorian Grandel adv_instance->pending = true; 2817d2609b34SFlorian Grandel adv_instance->instance = instance; 2818d2609b34SFlorian Grandel list_add(&adv_instance->list, &hdev->adv_instances); 2819d2609b34SFlorian Grandel hdev->adv_instance_cnt++; 2820d2609b34SFlorian Grandel } 2821d2609b34SFlorian Grandel 2822d2609b34SFlorian Grandel adv_instance->flags = flags; 2823d2609b34SFlorian Grandel adv_instance->adv_data_len = adv_data_len; 2824d2609b34SFlorian Grandel adv_instance->scan_rsp_len = scan_rsp_len; 2825d2609b34SFlorian Grandel 2826d2609b34SFlorian Grandel if (adv_data_len) 2827d2609b34SFlorian Grandel memcpy(adv_instance->adv_data, adv_data, adv_data_len); 2828d2609b34SFlorian Grandel 2829d2609b34SFlorian Grandel if (scan_rsp_len) 2830d2609b34SFlorian Grandel memcpy(adv_instance->scan_rsp_data, 2831d2609b34SFlorian Grandel scan_rsp_data, scan_rsp_len); 2832d2609b34SFlorian Grandel 2833d2609b34SFlorian Grandel adv_instance->timeout = timeout; 28345d900e46SFlorian Grandel adv_instance->remaining_time = timeout; 2835d2609b34SFlorian Grandel 2836d2609b34SFlorian Grandel if (duration == 0) 2837d2609b34SFlorian Grandel adv_instance->duration = HCI_DEFAULT_ADV_DURATION; 2838d2609b34SFlorian Grandel else 2839d2609b34SFlorian Grandel adv_instance->duration = duration; 2840d2609b34SFlorian Grandel 2841d2609b34SFlorian Grandel BT_DBG("%s for %dMR", hdev->name, instance); 2842d2609b34SFlorian Grandel 2843d2609b34SFlorian Grandel return 0; 2844d2609b34SFlorian Grandel } 2845d2609b34SFlorian Grandel 2846dcc36c16SJohan Hedberg struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list, 2847b9ee0a78SMarcel Holtmann bdaddr_t *bdaddr, u8 type) 2848b2a66aadSAntti Julku { 2849b2a66aadSAntti Julku struct bdaddr_list *b; 2850b2a66aadSAntti Julku 2851dcc36c16SJohan Hedberg list_for_each_entry(b, bdaddr_list, list) { 2852b9ee0a78SMarcel Holtmann if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) 2853b2a66aadSAntti Julku return b; 2854b9ee0a78SMarcel Holtmann } 2855b2a66aadSAntti Julku 2856b2a66aadSAntti Julku return NULL; 2857b2a66aadSAntti Julku } 2858b2a66aadSAntti Julku 2859dcc36c16SJohan Hedberg void hci_bdaddr_list_clear(struct list_head *bdaddr_list) 2860b2a66aadSAntti Julku { 2861b2a66aadSAntti Julku struct list_head *p, *n; 2862b2a66aadSAntti Julku 2863dcc36c16SJohan Hedberg list_for_each_safe(p, n, bdaddr_list) { 2864b9ee0a78SMarcel Holtmann struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list); 2865b2a66aadSAntti Julku 2866b2a66aadSAntti Julku list_del(p); 2867b2a66aadSAntti Julku kfree(b); 2868b2a66aadSAntti Julku } 2869b2a66aadSAntti Julku } 2870b2a66aadSAntti Julku 2871dcc36c16SJohan Hedberg int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type) 2872b2a66aadSAntti Julku { 2873b2a66aadSAntti Julku struct bdaddr_list *entry; 2874b2a66aadSAntti Julku 2875b9ee0a78SMarcel Holtmann if (!bacmp(bdaddr, BDADDR_ANY)) 2876b2a66aadSAntti Julku return -EBADF; 2877b2a66aadSAntti Julku 2878dcc36c16SJohan Hedberg if (hci_bdaddr_list_lookup(list, bdaddr, type)) 28795e762444SAntti Julku return -EEXIST; 2880b2a66aadSAntti Julku 288127f70f3eSJohan Hedberg entry = kzalloc(sizeof(*entry), GFP_KERNEL); 28825e762444SAntti Julku if (!entry) 28835e762444SAntti Julku return -ENOMEM; 2884b2a66aadSAntti Julku 2885b2a66aadSAntti Julku bacpy(&entry->bdaddr, bdaddr); 2886b9ee0a78SMarcel Holtmann entry->bdaddr_type = type; 2887b2a66aadSAntti Julku 2888dcc36c16SJohan Hedberg list_add(&entry->list, list); 2889b2a66aadSAntti Julku 28902a8357f2SJohan Hedberg return 0; 2891b2a66aadSAntti Julku } 2892b2a66aadSAntti Julku 2893dcc36c16SJohan Hedberg int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type) 2894b2a66aadSAntti Julku { 2895b2a66aadSAntti Julku struct bdaddr_list *entry; 2896b2a66aadSAntti Julku 289735f7498aSJohan Hedberg if (!bacmp(bdaddr, BDADDR_ANY)) { 2898dcc36c16SJohan Hedberg hci_bdaddr_list_clear(list); 289935f7498aSJohan Hedberg return 0; 290035f7498aSJohan Hedberg } 2901b2a66aadSAntti Julku 2902dcc36c16SJohan Hedberg entry = hci_bdaddr_list_lookup(list, bdaddr, type); 2903d2ab0ac1SMarcel Holtmann if (!entry) 2904d2ab0ac1SMarcel Holtmann return -ENOENT; 2905d2ab0ac1SMarcel Holtmann 2906d2ab0ac1SMarcel Holtmann list_del(&entry->list); 2907d2ab0ac1SMarcel Holtmann kfree(entry); 2908d2ab0ac1SMarcel Holtmann 2909d2ab0ac1SMarcel Holtmann return 0; 2910d2ab0ac1SMarcel Holtmann } 2911d2ab0ac1SMarcel Holtmann 291215819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 291315819a70SAndre Guedes struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, 291415819a70SAndre Guedes bdaddr_t *addr, u8 addr_type) 291515819a70SAndre Guedes { 291615819a70SAndre Guedes struct hci_conn_params *params; 291715819a70SAndre Guedes 291815819a70SAndre Guedes list_for_each_entry(params, &hdev->le_conn_params, list) { 291915819a70SAndre Guedes if (bacmp(¶ms->addr, addr) == 0 && 292015819a70SAndre Guedes params->addr_type == addr_type) { 292115819a70SAndre Guedes return params; 292215819a70SAndre Guedes } 292315819a70SAndre Guedes } 292415819a70SAndre Guedes 292515819a70SAndre Guedes return NULL; 292615819a70SAndre Guedes } 292715819a70SAndre Guedes 292815819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 2929501f8827SJohan Hedberg struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list, 29304b10966fSMarcel Holtmann bdaddr_t *addr, u8 addr_type) 293115819a70SAndre Guedes { 2932912b42efSJohan Hedberg struct hci_conn_params *param; 293315819a70SAndre Guedes 2934501f8827SJohan Hedberg list_for_each_entry(param, list, action) { 2935912b42efSJohan Hedberg if (bacmp(¶m->addr, addr) == 0 && 2936912b42efSJohan Hedberg param->addr_type == addr_type) 2937912b42efSJohan Hedberg return param; 29384b10966fSMarcel Holtmann } 29394b10966fSMarcel Holtmann 29404b10966fSMarcel Holtmann return NULL; 294115819a70SAndre Guedes } 294215819a70SAndre Guedes 294315819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 2944f75113a2SJakub Pawlowski struct hci_conn_params *hci_explicit_connect_lookup(struct hci_dev *hdev, 2945f75113a2SJakub Pawlowski bdaddr_t *addr, 2946f75113a2SJakub Pawlowski u8 addr_type) 2947f75113a2SJakub Pawlowski { 2948f75113a2SJakub Pawlowski struct hci_conn_params *param; 2949f75113a2SJakub Pawlowski 2950f75113a2SJakub Pawlowski list_for_each_entry(param, &hdev->pend_le_conns, action) { 2951f75113a2SJakub Pawlowski if (bacmp(¶m->addr, addr) == 0 && 2952f75113a2SJakub Pawlowski param->addr_type == addr_type && 2953f75113a2SJakub Pawlowski param->explicit_connect) 2954f75113a2SJakub Pawlowski return param; 2955f75113a2SJakub Pawlowski } 2956f75113a2SJakub Pawlowski 2957f75113a2SJakub Pawlowski return NULL; 2958f75113a2SJakub Pawlowski } 2959f75113a2SJakub Pawlowski 2960f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */ 296151d167c0SMarcel Holtmann struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev, 296251d167c0SMarcel Holtmann bdaddr_t *addr, u8 addr_type) 296315819a70SAndre Guedes { 296415819a70SAndre Guedes struct hci_conn_params *params; 296515819a70SAndre Guedes 296615819a70SAndre Guedes params = hci_conn_params_lookup(hdev, addr, addr_type); 2967cef952ceSAndre Guedes if (params) 296851d167c0SMarcel Holtmann return params; 296915819a70SAndre Guedes 297015819a70SAndre Guedes params = kzalloc(sizeof(*params), GFP_KERNEL); 297115819a70SAndre Guedes if (!params) { 297215819a70SAndre Guedes BT_ERR("Out of memory"); 297351d167c0SMarcel Holtmann return NULL; 297415819a70SAndre Guedes } 297515819a70SAndre Guedes 297615819a70SAndre Guedes bacpy(¶ms->addr, addr); 297715819a70SAndre Guedes params->addr_type = addr_type; 2978cef952ceSAndre Guedes 2979cef952ceSAndre Guedes list_add(¶ms->list, &hdev->le_conn_params); 298093450c75SJohan Hedberg INIT_LIST_HEAD(¶ms->action); 2981cef952ceSAndre Guedes 2982bf5b3c8bSMarcel Holtmann params->conn_min_interval = hdev->le_conn_min_interval; 2983bf5b3c8bSMarcel Holtmann params->conn_max_interval = hdev->le_conn_max_interval; 2984bf5b3c8bSMarcel Holtmann params->conn_latency = hdev->le_conn_latency; 2985bf5b3c8bSMarcel Holtmann params->supervision_timeout = hdev->le_supv_timeout; 2986bf5b3c8bSMarcel Holtmann params->auto_connect = HCI_AUTO_CONN_DISABLED; 2987bf5b3c8bSMarcel Holtmann 2988bf5b3c8bSMarcel Holtmann BT_DBG("addr %pMR (type %u)", addr, addr_type); 2989bf5b3c8bSMarcel Holtmann 299051d167c0SMarcel Holtmann return params; 2991bf5b3c8bSMarcel Holtmann } 2992bf5b3c8bSMarcel Holtmann 2993f6c63249SJohan Hedberg static void hci_conn_params_free(struct hci_conn_params *params) 2994f6c63249SJohan Hedberg { 2995f6c63249SJohan Hedberg if (params->conn) { 2996f6c63249SJohan Hedberg hci_conn_drop(params->conn); 2997f6c63249SJohan Hedberg hci_conn_put(params->conn); 2998f6c63249SJohan Hedberg } 2999f6c63249SJohan Hedberg 3000f6c63249SJohan Hedberg list_del(¶ms->action); 3001f6c63249SJohan Hedberg list_del(¶ms->list); 3002f6c63249SJohan Hedberg kfree(params); 3003f6c63249SJohan Hedberg } 3004f6c63249SJohan Hedberg 300515819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 300615819a70SAndre Guedes void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type) 300715819a70SAndre Guedes { 300815819a70SAndre Guedes struct hci_conn_params *params; 300915819a70SAndre Guedes 301015819a70SAndre Guedes params = hci_conn_params_lookup(hdev, addr, addr_type); 301115819a70SAndre Guedes if (!params) 301215819a70SAndre Guedes return; 301315819a70SAndre Guedes 3014f6c63249SJohan Hedberg hci_conn_params_free(params); 301515819a70SAndre Guedes 301695305baaSJohan Hedberg hci_update_background_scan(hdev); 301795305baaSJohan Hedberg 301815819a70SAndre Guedes BT_DBG("addr %pMR (type %u)", addr, addr_type); 301915819a70SAndre Guedes } 302015819a70SAndre Guedes 302115819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 302255af49a8SJohan Hedberg void hci_conn_params_clear_disabled(struct hci_dev *hdev) 302315819a70SAndre Guedes { 302415819a70SAndre Guedes struct hci_conn_params *params, *tmp; 302515819a70SAndre Guedes 302615819a70SAndre Guedes list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { 302755af49a8SJohan Hedberg if (params->auto_connect != HCI_AUTO_CONN_DISABLED) 302855af49a8SJohan Hedberg continue; 3029f75113a2SJakub Pawlowski 3030f75113a2SJakub Pawlowski /* If trying to estabilish one time connection to disabled 3031f75113a2SJakub Pawlowski * device, leave the params, but mark them as just once. 3032f75113a2SJakub Pawlowski */ 3033f75113a2SJakub Pawlowski if (params->explicit_connect) { 3034f75113a2SJakub Pawlowski params->auto_connect = HCI_AUTO_CONN_EXPLICIT; 3035f75113a2SJakub Pawlowski continue; 3036f75113a2SJakub Pawlowski } 3037f75113a2SJakub Pawlowski 303815819a70SAndre Guedes list_del(¶ms->list); 303915819a70SAndre Guedes kfree(params); 304015819a70SAndre Guedes } 304115819a70SAndre Guedes 304255af49a8SJohan Hedberg BT_DBG("All LE disabled connection parameters were removed"); 304355af49a8SJohan Hedberg } 304455af49a8SJohan Hedberg 304555af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */ 3046373110c5SJohan Hedberg void hci_conn_params_clear_all(struct hci_dev *hdev) 304715819a70SAndre Guedes { 304815819a70SAndre Guedes struct hci_conn_params *params, *tmp; 304915819a70SAndre Guedes 3050f6c63249SJohan Hedberg list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) 3051f6c63249SJohan Hedberg hci_conn_params_free(params); 305215819a70SAndre Guedes 3053a2f41a8fSJohan Hedberg hci_update_background_scan(hdev); 30541089b67dSMarcel Holtmann 305515819a70SAndre Guedes BT_DBG("All LE connection parameters were removed"); 305615819a70SAndre Guedes } 305715819a70SAndre Guedes 30581904a853SMarcel Holtmann static void inquiry_complete(struct hci_dev *hdev, u8 status, u16 opcode) 30597ba8b4beSAndre Guedes { 30604c87eaabSAndre Guedes if (status) { 30614c87eaabSAndre Guedes BT_ERR("Failed to start inquiry: status %d", status); 30627ba8b4beSAndre Guedes 30634c87eaabSAndre Guedes hci_dev_lock(hdev); 30644c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 30654c87eaabSAndre Guedes hci_dev_unlock(hdev); 30664c87eaabSAndre Guedes return; 30674c87eaabSAndre Guedes } 30687ba8b4beSAndre Guedes } 30697ba8b4beSAndre Guedes 30701904a853SMarcel Holtmann static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status, 30711904a853SMarcel Holtmann u16 opcode) 30727ba8b4beSAndre Guedes { 30734c87eaabSAndre Guedes /* General inquiry access code (GIAC) */ 30744c87eaabSAndre Guedes u8 lap[3] = { 0x33, 0x8b, 0x9e }; 30754c87eaabSAndre Guedes struct hci_cp_inquiry cp; 30767ba8b4beSAndre Guedes int err; 30777ba8b4beSAndre Guedes 30784c87eaabSAndre Guedes if (status) { 30794c87eaabSAndre Guedes BT_ERR("Failed to disable LE scanning: status %d", status); 30804c87eaabSAndre Guedes return; 30817ba8b4beSAndre Guedes } 30827ba8b4beSAndre Guedes 30832d28cfe7SJakub Pawlowski hdev->discovery.scan_start = 0; 30842d28cfe7SJakub Pawlowski 30854c87eaabSAndre Guedes switch (hdev->discovery.type) { 30864c87eaabSAndre Guedes case DISCOV_TYPE_LE: 30874c87eaabSAndre Guedes hci_dev_lock(hdev); 30884c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 30894c87eaabSAndre Guedes hci_dev_unlock(hdev); 30904c87eaabSAndre Guedes break; 30917dbfac1dSAndre Guedes 30924c87eaabSAndre Guedes case DISCOV_TYPE_INTERLEAVED: 30934c87eaabSAndre Guedes hci_dev_lock(hdev); 30944c87eaabSAndre Guedes 309507d2334aSJakub Pawlowski if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, 309607d2334aSJakub Pawlowski &hdev->quirks)) { 309707d2334aSJakub Pawlowski /* If we were running LE only scan, change discovery 309807d2334aSJakub Pawlowski * state. If we were running both LE and BR/EDR inquiry 309907d2334aSJakub Pawlowski * simultaneously, and BR/EDR inquiry is already 310007d2334aSJakub Pawlowski * finished, stop discovery, otherwise BR/EDR inquiry 3101177d0506SWesley Kuo * will stop discovery when finished. If we will resolve 3102177d0506SWesley Kuo * remote device name, do not change discovery state. 310307d2334aSJakub Pawlowski */ 3104177d0506SWesley Kuo if (!test_bit(HCI_INQUIRY, &hdev->flags) && 3105177d0506SWesley Kuo hdev->discovery.state != DISCOVERY_RESOLVING) 310607d2334aSJakub Pawlowski hci_discovery_set_state(hdev, 310707d2334aSJakub Pawlowski DISCOVERY_STOPPED); 310807d2334aSJakub Pawlowski } else { 3109baf880a9SJohan Hedberg struct hci_request req; 3110baf880a9SJohan Hedberg 31114c87eaabSAndre Guedes hci_inquiry_cache_flush(hdev); 31124c87eaabSAndre Guedes 3113baf880a9SJohan Hedberg hci_req_init(&req, hdev); 3114baf880a9SJohan Hedberg 3115baf880a9SJohan Hedberg memset(&cp, 0, sizeof(cp)); 3116baf880a9SJohan Hedberg memcpy(&cp.lap, lap, sizeof(cp.lap)); 3117baf880a9SJohan Hedberg cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; 3118baf880a9SJohan Hedberg hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); 3119baf880a9SJohan Hedberg 31204c87eaabSAndre Guedes err = hci_req_run(&req, inquiry_complete); 31214c87eaabSAndre Guedes if (err) { 31224c87eaabSAndre Guedes BT_ERR("Inquiry request failed: err %d", err); 312307d2334aSJakub Pawlowski hci_discovery_set_state(hdev, 312407d2334aSJakub Pawlowski DISCOVERY_STOPPED); 312507d2334aSJakub Pawlowski } 31267dbfac1dSAndre Guedes } 31277dbfac1dSAndre Guedes 31284c87eaabSAndre Guedes hci_dev_unlock(hdev); 31294c87eaabSAndre Guedes break; 31304c87eaabSAndre Guedes } 31317dbfac1dSAndre Guedes } 31327dbfac1dSAndre Guedes 31337ba8b4beSAndre Guedes static void le_scan_disable_work(struct work_struct *work) 31347ba8b4beSAndre Guedes { 31357ba8b4beSAndre Guedes struct hci_dev *hdev = container_of(work, struct hci_dev, 31367ba8b4beSAndre Guedes le_scan_disable.work); 31374c87eaabSAndre Guedes struct hci_request req; 31384c87eaabSAndre Guedes int err; 31397ba8b4beSAndre Guedes 31407ba8b4beSAndre Guedes BT_DBG("%s", hdev->name); 31417ba8b4beSAndre Guedes 31422d28cfe7SJakub Pawlowski cancel_delayed_work_sync(&hdev->le_scan_restart); 31432d28cfe7SJakub Pawlowski 31444c87eaabSAndre Guedes hci_req_init(&req, hdev); 31457ba8b4beSAndre Guedes 3146b1efcc28SAndre Guedes hci_req_add_le_scan_disable(&req); 31477ba8b4beSAndre Guedes 31484c87eaabSAndre Guedes err = hci_req_run(&req, le_scan_disable_work_complete); 31494c87eaabSAndre Guedes if (err) 31504c87eaabSAndre Guedes BT_ERR("Disable LE scanning request failed: err %d", err); 315128b75a89SAndre Guedes } 315228b75a89SAndre Guedes 31532d28cfe7SJakub Pawlowski static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status, 31542d28cfe7SJakub Pawlowski u16 opcode) 31552d28cfe7SJakub Pawlowski { 31562d28cfe7SJakub Pawlowski unsigned long timeout, duration, scan_start, now; 31572d28cfe7SJakub Pawlowski 31582d28cfe7SJakub Pawlowski BT_DBG("%s", hdev->name); 31592d28cfe7SJakub Pawlowski 31602d28cfe7SJakub Pawlowski if (status) { 31612d28cfe7SJakub Pawlowski BT_ERR("Failed to restart LE scan: status %d", status); 31622d28cfe7SJakub Pawlowski return; 31632d28cfe7SJakub Pawlowski } 31642d28cfe7SJakub Pawlowski 31652d28cfe7SJakub Pawlowski if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) || 31662d28cfe7SJakub Pawlowski !hdev->discovery.scan_start) 31672d28cfe7SJakub Pawlowski return; 31682d28cfe7SJakub Pawlowski 31692d28cfe7SJakub Pawlowski /* When the scan was started, hdev->le_scan_disable has been queued 31702d28cfe7SJakub Pawlowski * after duration from scan_start. During scan restart this job 31712d28cfe7SJakub Pawlowski * has been canceled, and we need to queue it again after proper 31722d28cfe7SJakub Pawlowski * timeout, to make sure that scan does not run indefinitely. 31732d28cfe7SJakub Pawlowski */ 31742d28cfe7SJakub Pawlowski duration = hdev->discovery.scan_duration; 31752d28cfe7SJakub Pawlowski scan_start = hdev->discovery.scan_start; 31762d28cfe7SJakub Pawlowski now = jiffies; 31772d28cfe7SJakub Pawlowski if (now - scan_start <= duration) { 31782d28cfe7SJakub Pawlowski int elapsed; 31792d28cfe7SJakub Pawlowski 31802d28cfe7SJakub Pawlowski if (now >= scan_start) 31812d28cfe7SJakub Pawlowski elapsed = now - scan_start; 31822d28cfe7SJakub Pawlowski else 31832d28cfe7SJakub Pawlowski elapsed = ULONG_MAX - scan_start + now; 31842d28cfe7SJakub Pawlowski 31852d28cfe7SJakub Pawlowski timeout = duration - elapsed; 31862d28cfe7SJakub Pawlowski } else { 31872d28cfe7SJakub Pawlowski timeout = 0; 31882d28cfe7SJakub Pawlowski } 31892d28cfe7SJakub Pawlowski queue_delayed_work(hdev->workqueue, 31902d28cfe7SJakub Pawlowski &hdev->le_scan_disable, timeout); 31912d28cfe7SJakub Pawlowski } 31922d28cfe7SJakub Pawlowski 31932d28cfe7SJakub Pawlowski static void le_scan_restart_work(struct work_struct *work) 31942d28cfe7SJakub Pawlowski { 31952d28cfe7SJakub Pawlowski struct hci_dev *hdev = container_of(work, struct hci_dev, 31962d28cfe7SJakub Pawlowski le_scan_restart.work); 31972d28cfe7SJakub Pawlowski struct hci_request req; 31982d28cfe7SJakub Pawlowski struct hci_cp_le_set_scan_enable cp; 31992d28cfe7SJakub Pawlowski int err; 32002d28cfe7SJakub Pawlowski 32012d28cfe7SJakub Pawlowski BT_DBG("%s", hdev->name); 32022d28cfe7SJakub Pawlowski 32032d28cfe7SJakub Pawlowski /* If controller is not scanning we are done. */ 3204d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) 32052d28cfe7SJakub Pawlowski return; 32062d28cfe7SJakub Pawlowski 32072d28cfe7SJakub Pawlowski hci_req_init(&req, hdev); 32082d28cfe7SJakub Pawlowski 32092d28cfe7SJakub Pawlowski hci_req_add_le_scan_disable(&req); 32102d28cfe7SJakub Pawlowski 32112d28cfe7SJakub Pawlowski memset(&cp, 0, sizeof(cp)); 32122d28cfe7SJakub Pawlowski cp.enable = LE_SCAN_ENABLE; 32132d28cfe7SJakub Pawlowski cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; 32142d28cfe7SJakub Pawlowski hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); 32152d28cfe7SJakub Pawlowski 32162d28cfe7SJakub Pawlowski err = hci_req_run(&req, le_scan_restart_work_complete); 32172d28cfe7SJakub Pawlowski if (err) 32182d28cfe7SJakub Pawlowski BT_ERR("Restart LE scan request failed: err %d", err); 32192d28cfe7SJakub Pawlowski } 32202d28cfe7SJakub Pawlowski 3221a1f4c318SJohan Hedberg /* Copy the Identity Address of the controller. 3222a1f4c318SJohan Hedberg * 3223a1f4c318SJohan Hedberg * If the controller has a public BD_ADDR, then by default use that one. 3224a1f4c318SJohan Hedberg * If this is a LE only controller without a public address, default to 3225a1f4c318SJohan Hedberg * the static random address. 3226a1f4c318SJohan Hedberg * 3227a1f4c318SJohan Hedberg * For debugging purposes it is possible to force controllers with a 3228a1f4c318SJohan Hedberg * public address to use the static random address instead. 322950b5b952SMarcel Holtmann * 323050b5b952SMarcel Holtmann * In case BR/EDR has been disabled on a dual-mode controller and 323150b5b952SMarcel Holtmann * userspace has configured a static address, then that address 323250b5b952SMarcel Holtmann * becomes the identity address instead of the public BR/EDR address. 3233a1f4c318SJohan Hedberg */ 3234a1f4c318SJohan Hedberg void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, 3235a1f4c318SJohan Hedberg u8 *bdaddr_type) 3236a1f4c318SJohan Hedberg { 3237b7cb93e5SMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 323850b5b952SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) || 3239d7a5a11dSMarcel Holtmann (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 324050b5b952SMarcel Holtmann bacmp(&hdev->static_addr, BDADDR_ANY))) { 3241a1f4c318SJohan Hedberg bacpy(bdaddr, &hdev->static_addr); 3242a1f4c318SJohan Hedberg *bdaddr_type = ADDR_LE_DEV_RANDOM; 3243a1f4c318SJohan Hedberg } else { 3244a1f4c318SJohan Hedberg bacpy(bdaddr, &hdev->bdaddr); 3245a1f4c318SJohan Hedberg *bdaddr_type = ADDR_LE_DEV_PUBLIC; 3246a1f4c318SJohan Hedberg } 3247a1f4c318SJohan Hedberg } 3248a1f4c318SJohan Hedberg 32499be0dab7SDavid Herrmann /* Alloc HCI device */ 32509be0dab7SDavid Herrmann struct hci_dev *hci_alloc_dev(void) 32519be0dab7SDavid Herrmann { 32529be0dab7SDavid Herrmann struct hci_dev *hdev; 32539be0dab7SDavid Herrmann 325427f70f3eSJohan Hedberg hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); 32559be0dab7SDavid Herrmann if (!hdev) 32569be0dab7SDavid Herrmann return NULL; 32579be0dab7SDavid Herrmann 3258b1b813d4SDavid Herrmann hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); 3259b1b813d4SDavid Herrmann hdev->esco_type = (ESCO_HV1); 3260b1b813d4SDavid Herrmann hdev->link_mode = (HCI_LM_ACCEPT); 3261b4cb9fb2SMarcel Holtmann hdev->num_iac = 0x01; /* One IAC support is mandatory */ 3262b1b813d4SDavid Herrmann hdev->io_capability = 0x03; /* No Input No Output */ 326396c2103aSMarcel Holtmann hdev->manufacturer = 0xffff; /* Default to internal use */ 3264bbaf444aSJohan Hedberg hdev->inq_tx_power = HCI_TX_POWER_INVALID; 3265bbaf444aSJohan Hedberg hdev->adv_tx_power = HCI_TX_POWER_INVALID; 3266d2609b34SFlorian Grandel hdev->adv_instance_cnt = 0; 3267d2609b34SFlorian Grandel hdev->cur_adv_instance = 0x00; 32685d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 3269b1b813d4SDavid Herrmann 3270b1b813d4SDavid Herrmann hdev->sniff_max_interval = 800; 3271b1b813d4SDavid Herrmann hdev->sniff_min_interval = 80; 3272b1b813d4SDavid Herrmann 32733f959d46SMarcel Holtmann hdev->le_adv_channel_map = 0x07; 3274628531c9SGeorg Lukas hdev->le_adv_min_interval = 0x0800; 3275628531c9SGeorg Lukas hdev->le_adv_max_interval = 0x0800; 3276bef64738SMarcel Holtmann hdev->le_scan_interval = 0x0060; 3277bef64738SMarcel Holtmann hdev->le_scan_window = 0x0030; 32784e70c7e7SMarcel Holtmann hdev->le_conn_min_interval = 0x0028; 32794e70c7e7SMarcel Holtmann hdev->le_conn_max_interval = 0x0038; 328004fb7d90SMarcel Holtmann hdev->le_conn_latency = 0x0000; 328104fb7d90SMarcel Holtmann hdev->le_supv_timeout = 0x002a; 3282a8e1bfaaSMarcel Holtmann hdev->le_def_tx_len = 0x001b; 3283a8e1bfaaSMarcel Holtmann hdev->le_def_tx_time = 0x0148; 3284a8e1bfaaSMarcel Holtmann hdev->le_max_tx_len = 0x001b; 3285a8e1bfaaSMarcel Holtmann hdev->le_max_tx_time = 0x0148; 3286a8e1bfaaSMarcel Holtmann hdev->le_max_rx_len = 0x001b; 3287a8e1bfaaSMarcel Holtmann hdev->le_max_rx_time = 0x0148; 3288bef64738SMarcel Holtmann 3289d6bfd59cSJohan Hedberg hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT; 3290b9a7a61eSLukasz Rymanowski hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT; 329131ad1691SAndrzej Kaczmarek hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE; 329231ad1691SAndrzej Kaczmarek hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE; 3293d6bfd59cSJohan Hedberg 3294b1b813d4SDavid Herrmann mutex_init(&hdev->lock); 3295b1b813d4SDavid Herrmann mutex_init(&hdev->req_lock); 3296b1b813d4SDavid Herrmann 3297b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->mgmt_pending); 3298b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->blacklist); 32996659358eSJohan Hedberg INIT_LIST_HEAD(&hdev->whitelist); 3300b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->uuids); 3301b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->link_keys); 3302b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->long_term_keys); 3303970c4e46SJohan Hedberg INIT_LIST_HEAD(&hdev->identity_resolving_keys); 3304b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->remote_oob_data); 3305d2ab0ac1SMarcel Holtmann INIT_LIST_HEAD(&hdev->le_white_list); 330615819a70SAndre Guedes INIT_LIST_HEAD(&hdev->le_conn_params); 330777a77a30SAndre Guedes INIT_LIST_HEAD(&hdev->pend_le_conns); 330866f8455aSJohan Hedberg INIT_LIST_HEAD(&hdev->pend_le_reports); 33096b536b5eSAndrei Emeltchenko INIT_LIST_HEAD(&hdev->conn_hash.list); 3310d2609b34SFlorian Grandel INIT_LIST_HEAD(&hdev->adv_instances); 3311b1b813d4SDavid Herrmann 3312b1b813d4SDavid Herrmann INIT_WORK(&hdev->rx_work, hci_rx_work); 3313b1b813d4SDavid Herrmann INIT_WORK(&hdev->cmd_work, hci_cmd_work); 3314b1b813d4SDavid Herrmann INIT_WORK(&hdev->tx_work, hci_tx_work); 3315b1b813d4SDavid Herrmann INIT_WORK(&hdev->power_on, hci_power_on); 3316c7741d16SMarcel Holtmann INIT_WORK(&hdev->error_reset, hci_error_reset); 3317b1b813d4SDavid Herrmann 3318b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); 3319b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); 3320b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); 33212d28cfe7SJakub Pawlowski INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work); 33225d900e46SFlorian Grandel INIT_DELAYED_WORK(&hdev->adv_instance_expire, hci_adv_timeout_expire); 3323b1b813d4SDavid Herrmann 3324b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->rx_q); 3325b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->cmd_q); 3326b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->raw_q); 3327b1b813d4SDavid Herrmann 3328b1b813d4SDavid Herrmann init_waitqueue_head(&hdev->req_wait_q); 3329b1b813d4SDavid Herrmann 333065cc2b49SMarcel Holtmann INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout); 3331b1b813d4SDavid Herrmann 3332b1b813d4SDavid Herrmann hci_init_sysfs(hdev); 3333b1b813d4SDavid Herrmann discovery_init(hdev); 33349be0dab7SDavid Herrmann 33359be0dab7SDavid Herrmann return hdev; 33369be0dab7SDavid Herrmann } 33379be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_alloc_dev); 33389be0dab7SDavid Herrmann 33399be0dab7SDavid Herrmann /* Free HCI device */ 33409be0dab7SDavid Herrmann void hci_free_dev(struct hci_dev *hdev) 33419be0dab7SDavid Herrmann { 33429be0dab7SDavid Herrmann /* will free via device release */ 33439be0dab7SDavid Herrmann put_device(&hdev->dev); 33449be0dab7SDavid Herrmann } 33459be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_free_dev); 33469be0dab7SDavid Herrmann 33471da177e4SLinus Torvalds /* Register HCI device */ 33481da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev) 33491da177e4SLinus Torvalds { 3350b1b813d4SDavid Herrmann int id, error; 33511da177e4SLinus Torvalds 335274292d5aSMarcel Holtmann if (!hdev->open || !hdev->close || !hdev->send) 33531da177e4SLinus Torvalds return -EINVAL; 33541da177e4SLinus Torvalds 335508add513SMat Martineau /* Do not allow HCI_AMP devices to register at index 0, 335608add513SMat Martineau * so the index can be used as the AMP controller ID. 335708add513SMat Martineau */ 33583df92b31SSasha Levin switch (hdev->dev_type) { 33593df92b31SSasha Levin case HCI_BREDR: 33603df92b31SSasha Levin id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); 33611da177e4SLinus Torvalds break; 33623df92b31SSasha Levin case HCI_AMP: 33633df92b31SSasha Levin id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); 33643df92b31SSasha Levin break; 33653df92b31SSasha Levin default: 33663df92b31SSasha Levin return -EINVAL; 33671da177e4SLinus Torvalds } 33681da177e4SLinus Torvalds 33693df92b31SSasha Levin if (id < 0) 33703df92b31SSasha Levin return id; 33713df92b31SSasha Levin 33721da177e4SLinus Torvalds sprintf(hdev->name, "hci%d", id); 33731da177e4SLinus Torvalds hdev->id = id; 33742d8b3a11SAndrei Emeltchenko 33752d8b3a11SAndrei Emeltchenko BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 33762d8b3a11SAndrei Emeltchenko 3377d8537548SKees Cook hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | 3378d8537548SKees Cook WQ_MEM_RECLAIM, 1, hdev->name); 337933ca954dSDavid Herrmann if (!hdev->workqueue) { 338033ca954dSDavid Herrmann error = -ENOMEM; 338133ca954dSDavid Herrmann goto err; 338233ca954dSDavid Herrmann } 3383f48fd9c8SMarcel Holtmann 3384d8537548SKees Cook hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | 3385d8537548SKees Cook WQ_MEM_RECLAIM, 1, hdev->name); 33866ead1bbcSJohan Hedberg if (!hdev->req_workqueue) { 33876ead1bbcSJohan Hedberg destroy_workqueue(hdev->workqueue); 33886ead1bbcSJohan Hedberg error = -ENOMEM; 33896ead1bbcSJohan Hedberg goto err; 33906ead1bbcSJohan Hedberg } 33916ead1bbcSJohan Hedberg 33920153e2ecSMarcel Holtmann if (!IS_ERR_OR_NULL(bt_debugfs)) 33930153e2ecSMarcel Holtmann hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs); 33940153e2ecSMarcel Holtmann 3395bdc3e0f1SMarcel Holtmann dev_set_name(&hdev->dev, "%s", hdev->name); 3396bdc3e0f1SMarcel Holtmann 3397bdc3e0f1SMarcel Holtmann error = device_add(&hdev->dev); 339833ca954dSDavid Herrmann if (error < 0) 339954506918SJohan Hedberg goto err_wqueue; 34001da177e4SLinus Torvalds 3401611b30f7SMarcel Holtmann hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, 3402a8c5fb1aSGustavo Padovan RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, 3403a8c5fb1aSGustavo Padovan hdev); 3404611b30f7SMarcel Holtmann if (hdev->rfkill) { 3405611b30f7SMarcel Holtmann if (rfkill_register(hdev->rfkill) < 0) { 3406611b30f7SMarcel Holtmann rfkill_destroy(hdev->rfkill); 3407611b30f7SMarcel Holtmann hdev->rfkill = NULL; 3408611b30f7SMarcel Holtmann } 3409611b30f7SMarcel Holtmann } 3410611b30f7SMarcel Holtmann 34115e130367SJohan Hedberg if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) 3412a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_RFKILLED); 34135e130367SJohan Hedberg 3414a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_SETUP); 3415a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_AUTO_OFF); 3416ce2be9acSAndrei Emeltchenko 341701cd3404SMarcel Holtmann if (hdev->dev_type == HCI_BREDR) { 341856f87901SJohan Hedberg /* Assume BR/EDR support until proven otherwise (such as 341956f87901SJohan Hedberg * through reading supported features during init. 342056f87901SJohan Hedberg */ 3421a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); 342256f87901SJohan Hedberg } 3423ce2be9acSAndrei Emeltchenko 3424fcee3377SGustavo Padovan write_lock(&hci_dev_list_lock); 3425fcee3377SGustavo Padovan list_add(&hdev->list, &hci_dev_list); 3426fcee3377SGustavo Padovan write_unlock(&hci_dev_list_lock); 3427fcee3377SGustavo Padovan 34284a964404SMarcel Holtmann /* Devices that are marked for raw-only usage are unconfigured 34294a964404SMarcel Holtmann * and should not be included in normal operation. 3430fee746b0SMarcel Holtmann */ 3431fee746b0SMarcel Holtmann if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 3432a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_UNCONFIGURED); 3433fee746b0SMarcel Holtmann 34341da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_REG); 3435dc946bd8SDavid Herrmann hci_dev_hold(hdev); 34361da177e4SLinus Torvalds 343719202573SJohan Hedberg queue_work(hdev->req_workqueue, &hdev->power_on); 3438fbe96d6fSMarcel Holtmann 34391da177e4SLinus Torvalds return id; 3440f48fd9c8SMarcel Holtmann 344133ca954dSDavid Herrmann err_wqueue: 344233ca954dSDavid Herrmann destroy_workqueue(hdev->workqueue); 34436ead1bbcSJohan Hedberg destroy_workqueue(hdev->req_workqueue); 344433ca954dSDavid Herrmann err: 34453df92b31SSasha Levin ida_simple_remove(&hci_index_ida, hdev->id); 3446f48fd9c8SMarcel Holtmann 344733ca954dSDavid Herrmann return error; 34481da177e4SLinus Torvalds } 34491da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_dev); 34501da177e4SLinus Torvalds 34511da177e4SLinus Torvalds /* Unregister HCI device */ 345259735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev) 34531da177e4SLinus Torvalds { 34542d7cc19eSMarcel Holtmann int id; 3455ef222013SMarcel Holtmann 3456c13854ceSMarcel Holtmann BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 34571da177e4SLinus Torvalds 3458a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_UNREGISTER); 345994324962SJohan Hovold 34603df92b31SSasha Levin id = hdev->id; 34613df92b31SSasha Levin 3462f20d09d5SGustavo F. Padovan write_lock(&hci_dev_list_lock); 34631da177e4SLinus Torvalds list_del(&hdev->list); 3464f20d09d5SGustavo F. Padovan write_unlock(&hci_dev_list_lock); 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds hci_dev_do_close(hdev); 34671da177e4SLinus Torvalds 3468b9b5ef18SGustavo Padovan cancel_work_sync(&hdev->power_on); 3469b9b5ef18SGustavo Padovan 3470ab81cbf9SJohan Hedberg if (!test_bit(HCI_INIT, &hdev->flags) && 3471d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_SETUP) && 3472d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) { 347309fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 3474744cf19eSJohan Hedberg mgmt_index_removed(hdev); 347509fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 347656e5cb86SJohan Hedberg } 3477ab81cbf9SJohan Hedberg 34782e58ef3eSJohan Hedberg /* mgmt_index_removed should take care of emptying the 34792e58ef3eSJohan Hedberg * pending list */ 34802e58ef3eSJohan Hedberg BUG_ON(!list_empty(&hdev->mgmt_pending)); 34812e58ef3eSJohan Hedberg 34821da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_UNREG); 34831da177e4SLinus Torvalds 3484611b30f7SMarcel Holtmann if (hdev->rfkill) { 3485611b30f7SMarcel Holtmann rfkill_unregister(hdev->rfkill); 3486611b30f7SMarcel Holtmann rfkill_destroy(hdev->rfkill); 3487611b30f7SMarcel Holtmann } 3488611b30f7SMarcel Holtmann 3489bdc3e0f1SMarcel Holtmann device_del(&hdev->dev); 3490147e2d59SDave Young 34910153e2ecSMarcel Holtmann debugfs_remove_recursive(hdev->debugfs); 34920153e2ecSMarcel Holtmann 3493f48fd9c8SMarcel Holtmann destroy_workqueue(hdev->workqueue); 34946ead1bbcSJohan Hedberg destroy_workqueue(hdev->req_workqueue); 3495f48fd9c8SMarcel Holtmann 349609fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 3497dcc36c16SJohan Hedberg hci_bdaddr_list_clear(&hdev->blacklist); 34986659358eSJohan Hedberg hci_bdaddr_list_clear(&hdev->whitelist); 34992aeb9a1aSJohan Hedberg hci_uuids_clear(hdev); 350055ed8ca1SJohan Hedberg hci_link_keys_clear(hdev); 3501b899efafSVinicius Costa Gomes hci_smp_ltks_clear(hdev); 3502970c4e46SJohan Hedberg hci_smp_irks_clear(hdev); 35032763eda6SSzymon Janc hci_remote_oob_data_clear(hdev); 3504d2609b34SFlorian Grandel hci_adv_instances_clear(hdev); 3505dcc36c16SJohan Hedberg hci_bdaddr_list_clear(&hdev->le_white_list); 3506373110c5SJohan Hedberg hci_conn_params_clear_all(hdev); 350722078800SMarcel Holtmann hci_discovery_filter_clear(hdev); 350809fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 3509e2e0cacbSJohan Hedberg 3510dc946bd8SDavid Herrmann hci_dev_put(hdev); 35113df92b31SSasha Levin 35123df92b31SSasha Levin ida_simple_remove(&hci_index_ida, id); 35131da177e4SLinus Torvalds } 35141da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_dev); 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds /* Suspend HCI device */ 35171da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev) 35181da177e4SLinus Torvalds { 35191da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_SUSPEND); 35201da177e4SLinus Torvalds return 0; 35211da177e4SLinus Torvalds } 35221da177e4SLinus Torvalds EXPORT_SYMBOL(hci_suspend_dev); 35231da177e4SLinus Torvalds 35241da177e4SLinus Torvalds /* Resume HCI device */ 35251da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev) 35261da177e4SLinus Torvalds { 35271da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_RESUME); 35281da177e4SLinus Torvalds return 0; 35291da177e4SLinus Torvalds } 35301da177e4SLinus Torvalds EXPORT_SYMBOL(hci_resume_dev); 35311da177e4SLinus Torvalds 353275e0569fSMarcel Holtmann /* Reset HCI device */ 353375e0569fSMarcel Holtmann int hci_reset_dev(struct hci_dev *hdev) 353475e0569fSMarcel Holtmann { 353575e0569fSMarcel Holtmann const u8 hw_err[] = { HCI_EV_HARDWARE_ERROR, 0x01, 0x00 }; 353675e0569fSMarcel Holtmann struct sk_buff *skb; 353775e0569fSMarcel Holtmann 353875e0569fSMarcel Holtmann skb = bt_skb_alloc(3, GFP_ATOMIC); 353975e0569fSMarcel Holtmann if (!skb) 354075e0569fSMarcel Holtmann return -ENOMEM; 354175e0569fSMarcel Holtmann 354275e0569fSMarcel Holtmann bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 354375e0569fSMarcel Holtmann memcpy(skb_put(skb, 3), hw_err, 3); 354475e0569fSMarcel Holtmann 354575e0569fSMarcel Holtmann /* Send Hardware Error to upper stack */ 354675e0569fSMarcel Holtmann return hci_recv_frame(hdev, skb); 354775e0569fSMarcel Holtmann } 354875e0569fSMarcel Holtmann EXPORT_SYMBOL(hci_reset_dev); 354975e0569fSMarcel Holtmann 355076bca880SMarcel Holtmann /* Receive frame from HCI drivers */ 3551e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) 355276bca880SMarcel Holtmann { 355376bca880SMarcel Holtmann if (!hdev || (!test_bit(HCI_UP, &hdev->flags) 355476bca880SMarcel Holtmann && !test_bit(HCI_INIT, &hdev->flags))) { 355576bca880SMarcel Holtmann kfree_skb(skb); 355676bca880SMarcel Holtmann return -ENXIO; 355776bca880SMarcel Holtmann } 355876bca880SMarcel Holtmann 3559fe806dceSMarcel Holtmann if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT && 3560fe806dceSMarcel Holtmann bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && 3561fe806dceSMarcel Holtmann bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) { 3562fe806dceSMarcel Holtmann kfree_skb(skb); 3563fe806dceSMarcel Holtmann return -EINVAL; 3564fe806dceSMarcel Holtmann } 3565fe806dceSMarcel Holtmann 3566d82603c6SJorrit Schippers /* Incoming skb */ 356776bca880SMarcel Holtmann bt_cb(skb)->incoming = 1; 356876bca880SMarcel Holtmann 356976bca880SMarcel Holtmann /* Time stamp */ 357076bca880SMarcel Holtmann __net_timestamp(skb); 357176bca880SMarcel Holtmann 357276bca880SMarcel Holtmann skb_queue_tail(&hdev->rx_q, skb); 3573b78752ccSMarcel Holtmann queue_work(hdev->workqueue, &hdev->rx_work); 3574c78ae283SMarcel Holtmann 357576bca880SMarcel Holtmann return 0; 357676bca880SMarcel Holtmann } 357776bca880SMarcel Holtmann EXPORT_SYMBOL(hci_recv_frame); 357876bca880SMarcel Holtmann 3579e875ff84SMarcel Holtmann /* Receive diagnostic message from HCI drivers */ 3580e875ff84SMarcel Holtmann int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb) 3581e875ff84SMarcel Holtmann { 3582581d6fd6SMarcel Holtmann /* Mark as diagnostic packet */ 3583581d6fd6SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_DIAG_PKT; 3584581d6fd6SMarcel Holtmann 3585e875ff84SMarcel Holtmann /* Time stamp */ 3586e875ff84SMarcel Holtmann __net_timestamp(skb); 3587e875ff84SMarcel Holtmann 3588581d6fd6SMarcel Holtmann skb_queue_tail(&hdev->rx_q, skb); 3589581d6fd6SMarcel Holtmann queue_work(hdev->workqueue, &hdev->rx_work); 3590e875ff84SMarcel Holtmann 3591e875ff84SMarcel Holtmann return 0; 3592e875ff84SMarcel Holtmann } 3593e875ff84SMarcel Holtmann EXPORT_SYMBOL(hci_recv_diag); 3594e875ff84SMarcel Holtmann 35951da177e4SLinus Torvalds /* ---- Interface to upper protocols ---- */ 35961da177e4SLinus Torvalds 35971da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *cb) 35981da177e4SLinus Torvalds { 35991da177e4SLinus Torvalds BT_DBG("%p name %s", cb, cb->name); 36001da177e4SLinus Torvalds 3601fba7ecf0SJohan Hedberg mutex_lock(&hci_cb_list_lock); 360200629e0fSJohan Hedberg list_add_tail(&cb->list, &hci_cb_list); 3603fba7ecf0SJohan Hedberg mutex_unlock(&hci_cb_list_lock); 36041da177e4SLinus Torvalds 36051da177e4SLinus Torvalds return 0; 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_cb); 36081da177e4SLinus Torvalds 36091da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *cb) 36101da177e4SLinus Torvalds { 36111da177e4SLinus Torvalds BT_DBG("%p name %s", cb, cb->name); 36121da177e4SLinus Torvalds 3613fba7ecf0SJohan Hedberg mutex_lock(&hci_cb_list_lock); 36141da177e4SLinus Torvalds list_del(&cb->list); 3615fba7ecf0SJohan Hedberg mutex_unlock(&hci_cb_list_lock); 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds return 0; 36181da177e4SLinus Torvalds } 36191da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_cb); 36201da177e4SLinus Torvalds 362151086991SMarcel Holtmann static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) 36221da177e4SLinus Torvalds { 3623cdc52faaSMarcel Holtmann int err; 3624cdc52faaSMarcel Holtmann 36250d48d939SMarcel Holtmann BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); 36261da177e4SLinus Torvalds 36271da177e4SLinus Torvalds /* Time stamp */ 3628a61bbcf2SPatrick McHardy __net_timestamp(skb); 36291da177e4SLinus Torvalds 3630cd82e61cSMarcel Holtmann /* Send copy to monitor */ 3631cd82e61cSMarcel Holtmann hci_send_to_monitor(hdev, skb); 3632cd82e61cSMarcel Holtmann 3633cd82e61cSMarcel Holtmann if (atomic_read(&hdev->promisc)) { 3634cd82e61cSMarcel Holtmann /* Send copy to the sockets */ 3635470fe1b5SMarcel Holtmann hci_send_to_sock(hdev, skb); 36361da177e4SLinus Torvalds } 36371da177e4SLinus Torvalds 36381da177e4SLinus Torvalds /* Get rid of skb owner, prior to sending to the driver. */ 36391da177e4SLinus Torvalds skb_orphan(skb); 36401da177e4SLinus Torvalds 364173d0d3c8SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) { 364273d0d3c8SMarcel Holtmann kfree_skb(skb); 364373d0d3c8SMarcel Holtmann return; 364473d0d3c8SMarcel Holtmann } 364573d0d3c8SMarcel Holtmann 3646cdc52faaSMarcel Holtmann err = hdev->send(hdev, skb); 3647cdc52faaSMarcel Holtmann if (err < 0) { 3648cdc52faaSMarcel Holtmann BT_ERR("%s sending frame failed (%d)", hdev->name, err); 3649cdc52faaSMarcel Holtmann kfree_skb(skb); 3650cdc52faaSMarcel Holtmann } 36511da177e4SLinus Torvalds } 36521da177e4SLinus Torvalds 36531ca3a9d0SJohan Hedberg /* Send HCI command */ 365407dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, 365507dc93ddSJohan Hedberg const void *param) 36561ca3a9d0SJohan Hedberg { 36571ca3a9d0SJohan Hedberg struct sk_buff *skb; 36581ca3a9d0SJohan Hedberg 36591ca3a9d0SJohan Hedberg BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); 36601ca3a9d0SJohan Hedberg 36611ca3a9d0SJohan Hedberg skb = hci_prepare_cmd(hdev, opcode, plen, param); 36621ca3a9d0SJohan Hedberg if (!skb) { 36631ca3a9d0SJohan Hedberg BT_ERR("%s no memory for command", hdev->name); 36641ca3a9d0SJohan Hedberg return -ENOMEM; 36651ca3a9d0SJohan Hedberg } 36661ca3a9d0SJohan Hedberg 366749c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as 366811714b3dSJohan Hedberg * single-command requests. 366911714b3dSJohan Hedberg */ 3670db6e3e8dSJohan Hedberg bt_cb(skb)->req.start = true; 367111714b3dSJohan Hedberg 36721da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 3673c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 36741da177e4SLinus Torvalds 36751da177e4SLinus Torvalds return 0; 36761da177e4SLinus Torvalds } 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds /* Get data from the previously sent command */ 3679a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) 36801da177e4SLinus Torvalds { 36811da177e4SLinus Torvalds struct hci_command_hdr *hdr; 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds if (!hdev->sent_cmd) 36841da177e4SLinus Torvalds return NULL; 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds hdr = (void *) hdev->sent_cmd->data; 36871da177e4SLinus Torvalds 3688a9de9248SMarcel Holtmann if (hdr->opcode != cpu_to_le16(opcode)) 36891da177e4SLinus Torvalds return NULL; 36901da177e4SLinus Torvalds 3691f0e09510SAndrei Emeltchenko BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); 36921da177e4SLinus Torvalds 36931da177e4SLinus Torvalds return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; 36941da177e4SLinus Torvalds } 36951da177e4SLinus Torvalds 3696fbef168fSLoic Poulain /* Send HCI command and wait for command commplete event */ 3697fbef168fSLoic Poulain struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 3698fbef168fSLoic Poulain const void *param, u32 timeout) 3699fbef168fSLoic Poulain { 3700fbef168fSLoic Poulain struct sk_buff *skb; 3701fbef168fSLoic Poulain 3702fbef168fSLoic Poulain if (!test_bit(HCI_UP, &hdev->flags)) 3703fbef168fSLoic Poulain return ERR_PTR(-ENETDOWN); 3704fbef168fSLoic Poulain 3705fbef168fSLoic Poulain bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); 3706fbef168fSLoic Poulain 3707fbef168fSLoic Poulain hci_req_lock(hdev); 3708fbef168fSLoic Poulain skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); 3709fbef168fSLoic Poulain hci_req_unlock(hdev); 3710fbef168fSLoic Poulain 3711fbef168fSLoic Poulain return skb; 3712fbef168fSLoic Poulain } 3713fbef168fSLoic Poulain EXPORT_SYMBOL(hci_cmd_sync); 3714fbef168fSLoic Poulain 37151da177e4SLinus Torvalds /* Send ACL data */ 37161da177e4SLinus Torvalds static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) 37171da177e4SLinus Torvalds { 37181da177e4SLinus Torvalds struct hci_acl_hdr *hdr; 37191da177e4SLinus Torvalds int len = skb->len; 37201da177e4SLinus Torvalds 3721badff6d0SArnaldo Carvalho de Melo skb_push(skb, HCI_ACL_HDR_SIZE); 3722badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 37239c70220bSArnaldo Carvalho de Melo hdr = (struct hci_acl_hdr *)skb_transport_header(skb); 3724aca3192cSYOSHIFUJI Hideaki hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); 3725aca3192cSYOSHIFUJI Hideaki hdr->dlen = cpu_to_le16(len); 37261da177e4SLinus Torvalds } 37271da177e4SLinus Torvalds 3728ee22be7eSAndrei Emeltchenko static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue, 372973d80debSLuiz Augusto von Dentz struct sk_buff *skb, __u16 flags) 37301da177e4SLinus Torvalds { 3731ee22be7eSAndrei Emeltchenko struct hci_conn *conn = chan->conn; 37321da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 37331da177e4SLinus Torvalds struct sk_buff *list; 37341da177e4SLinus Torvalds 3735087bfd99SGustavo Padovan skb->len = skb_headlen(skb); 3736087bfd99SGustavo Padovan skb->data_len = 0; 3737087bfd99SGustavo Padovan 3738087bfd99SGustavo Padovan bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 3739204a6e54SAndrei Emeltchenko 3740204a6e54SAndrei Emeltchenko switch (hdev->dev_type) { 3741204a6e54SAndrei Emeltchenko case HCI_BREDR: 3742087bfd99SGustavo Padovan hci_add_acl_hdr(skb, conn->handle, flags); 3743204a6e54SAndrei Emeltchenko break; 3744204a6e54SAndrei Emeltchenko case HCI_AMP: 3745204a6e54SAndrei Emeltchenko hci_add_acl_hdr(skb, chan->handle, flags); 3746204a6e54SAndrei Emeltchenko break; 3747204a6e54SAndrei Emeltchenko default: 3748204a6e54SAndrei Emeltchenko BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); 3749204a6e54SAndrei Emeltchenko return; 3750204a6e54SAndrei Emeltchenko } 3751087bfd99SGustavo Padovan 375270f23020SAndrei Emeltchenko list = skb_shinfo(skb)->frag_list; 375370f23020SAndrei Emeltchenko if (!list) { 37541da177e4SLinus Torvalds /* Non fragmented */ 37551da177e4SLinus Torvalds BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); 37561da177e4SLinus Torvalds 375773d80debSLuiz Augusto von Dentz skb_queue_tail(queue, skb); 37581da177e4SLinus Torvalds } else { 37591da177e4SLinus Torvalds /* Fragmented */ 37601da177e4SLinus Torvalds BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = NULL; 37631da177e4SLinus Torvalds 37649cfd5a23SJukka Rissanen /* Queue all fragments atomically. We need to use spin_lock_bh 37659cfd5a23SJukka Rissanen * here because of 6LoWPAN links, as there this function is 37669cfd5a23SJukka Rissanen * called from softirq and using normal spin lock could cause 37679cfd5a23SJukka Rissanen * deadlocks. 37689cfd5a23SJukka Rissanen */ 37699cfd5a23SJukka Rissanen spin_lock_bh(&queue->lock); 37701da177e4SLinus Torvalds 377173d80debSLuiz Augusto von Dentz __skb_queue_tail(queue, skb); 3772e702112fSAndrei Emeltchenko 3773e702112fSAndrei Emeltchenko flags &= ~ACL_START; 3774e702112fSAndrei Emeltchenko flags |= ACL_CONT; 37751da177e4SLinus Torvalds do { 37761da177e4SLinus Torvalds skb = list; list = list->next; 37771da177e4SLinus Torvalds 37780d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 3779e702112fSAndrei Emeltchenko hci_add_acl_hdr(skb, conn->handle, flags); 37801da177e4SLinus Torvalds 37811da177e4SLinus Torvalds BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); 37821da177e4SLinus Torvalds 378373d80debSLuiz Augusto von Dentz __skb_queue_tail(queue, skb); 37841da177e4SLinus Torvalds } while (list); 37851da177e4SLinus Torvalds 37869cfd5a23SJukka Rissanen spin_unlock_bh(&queue->lock); 37871da177e4SLinus Torvalds } 378873d80debSLuiz Augusto von Dentz } 378973d80debSLuiz Augusto von Dentz 379073d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) 379173d80debSLuiz Augusto von Dentz { 3792ee22be7eSAndrei Emeltchenko struct hci_dev *hdev = chan->conn->hdev; 379373d80debSLuiz Augusto von Dentz 3794f0e09510SAndrei Emeltchenko BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); 379573d80debSLuiz Augusto von Dentz 3796ee22be7eSAndrei Emeltchenko hci_queue_acl(chan, &chan->data_q, skb, flags); 37971da177e4SLinus Torvalds 37983eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 37991da177e4SLinus Torvalds } 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds /* Send SCO data */ 38020d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) 38031da177e4SLinus Torvalds { 38041da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 38051da177e4SLinus Torvalds struct hci_sco_hdr hdr; 38061da177e4SLinus Torvalds 38071da177e4SLinus Torvalds BT_DBG("%s len %d", hdev->name, skb->len); 38081da177e4SLinus Torvalds 3809aca3192cSYOSHIFUJI Hideaki hdr.handle = cpu_to_le16(conn->handle); 38101da177e4SLinus Torvalds hdr.dlen = skb->len; 38111da177e4SLinus Torvalds 3812badff6d0SArnaldo Carvalho de Melo skb_push(skb, HCI_SCO_HDR_SIZE); 3813badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 38149c70220bSArnaldo Carvalho de Melo memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); 38151da177e4SLinus Torvalds 38160d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 3817c78ae283SMarcel Holtmann 38181da177e4SLinus Torvalds skb_queue_tail(&conn->data_q, skb); 38193eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 38201da177e4SLinus Torvalds } 38211da177e4SLinus Torvalds 38221da177e4SLinus Torvalds /* ---- HCI TX task (outgoing data) ---- */ 38231da177e4SLinus Torvalds 38241da177e4SLinus Torvalds /* HCI Connection scheduler */ 38256039aa73SGustavo Padovan static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, 3826a8c5fb1aSGustavo Padovan int *quote) 38271da177e4SLinus Torvalds { 38281da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 38298035ded4SLuiz Augusto von Dentz struct hci_conn *conn = NULL, *c; 3830abc5de8fSMikel Astiz unsigned int num = 0, min = ~0; 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds /* We don't have to lock device here. Connections are always 38331da177e4SLinus Torvalds * added and removed with TX task disabled. */ 3834bf4c6325SGustavo F. Padovan 3835bf4c6325SGustavo F. Padovan rcu_read_lock(); 3836bf4c6325SGustavo F. Padovan 3837bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(c, &h->list, list) { 3838769be974SMarcel Holtmann if (c->type != type || skb_queue_empty(&c->data_q)) 38391da177e4SLinus Torvalds continue; 3840769be974SMarcel Holtmann 3841769be974SMarcel Holtmann if (c->state != BT_CONNECTED && c->state != BT_CONFIG) 3842769be974SMarcel Holtmann continue; 3843769be974SMarcel Holtmann 38441da177e4SLinus Torvalds num++; 38451da177e4SLinus Torvalds 38461da177e4SLinus Torvalds if (c->sent < min) { 38471da177e4SLinus Torvalds min = c->sent; 38481da177e4SLinus Torvalds conn = c; 38491da177e4SLinus Torvalds } 385052087a79SLuiz Augusto von Dentz 385152087a79SLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == num) 385252087a79SLuiz Augusto von Dentz break; 38531da177e4SLinus Torvalds } 38541da177e4SLinus Torvalds 3855bf4c6325SGustavo F. Padovan rcu_read_unlock(); 3856bf4c6325SGustavo F. Padovan 38571da177e4SLinus Torvalds if (conn) { 38586ed58ec5SVille Tervo int cnt, q; 38596ed58ec5SVille Tervo 38606ed58ec5SVille Tervo switch (conn->type) { 38616ed58ec5SVille Tervo case ACL_LINK: 38626ed58ec5SVille Tervo cnt = hdev->acl_cnt; 38636ed58ec5SVille Tervo break; 38646ed58ec5SVille Tervo case SCO_LINK: 38656ed58ec5SVille Tervo case ESCO_LINK: 38666ed58ec5SVille Tervo cnt = hdev->sco_cnt; 38676ed58ec5SVille Tervo break; 38686ed58ec5SVille Tervo case LE_LINK: 38696ed58ec5SVille Tervo cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; 38706ed58ec5SVille Tervo break; 38716ed58ec5SVille Tervo default: 38726ed58ec5SVille Tervo cnt = 0; 38736ed58ec5SVille Tervo BT_ERR("Unknown link type"); 38746ed58ec5SVille Tervo } 38756ed58ec5SVille Tervo 38766ed58ec5SVille Tervo q = cnt / num; 38771da177e4SLinus Torvalds *quote = q ? q : 1; 38781da177e4SLinus Torvalds } else 38791da177e4SLinus Torvalds *quote = 0; 38801da177e4SLinus Torvalds 38811da177e4SLinus Torvalds BT_DBG("conn %p quote %d", conn, *quote); 38821da177e4SLinus Torvalds return conn; 38831da177e4SLinus Torvalds } 38841da177e4SLinus Torvalds 38856039aa73SGustavo Padovan static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) 38861da177e4SLinus Torvalds { 38871da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 38881da177e4SLinus Torvalds struct hci_conn *c; 38891da177e4SLinus Torvalds 3890bae1f5d9SVille Tervo BT_ERR("%s link tx timeout", hdev->name); 38911da177e4SLinus Torvalds 3892bf4c6325SGustavo F. Padovan rcu_read_lock(); 3893bf4c6325SGustavo F. Padovan 38941da177e4SLinus Torvalds /* Kill stalled connections */ 3895bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(c, &h->list, list) { 3896bae1f5d9SVille Tervo if (c->type == type && c->sent) { 38976ed93dc6SAndrei Emeltchenko BT_ERR("%s killing stalled connection %pMR", 38986ed93dc6SAndrei Emeltchenko hdev->name, &c->dst); 3899bed71748SAndre Guedes hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); 39001da177e4SLinus Torvalds } 39011da177e4SLinus Torvalds } 3902bf4c6325SGustavo F. Padovan 3903bf4c6325SGustavo F. Padovan rcu_read_unlock(); 39041da177e4SLinus Torvalds } 39051da177e4SLinus Torvalds 39066039aa73SGustavo Padovan static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, 390773d80debSLuiz Augusto von Dentz int *quote) 390873d80debSLuiz Augusto von Dentz { 390973d80debSLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 391073d80debSLuiz Augusto von Dentz struct hci_chan *chan = NULL; 3911abc5de8fSMikel Astiz unsigned int num = 0, min = ~0, cur_prio = 0; 391273d80debSLuiz Augusto von Dentz struct hci_conn *conn; 391373d80debSLuiz Augusto von Dentz int cnt, q, conn_num = 0; 391473d80debSLuiz Augusto von Dentz 391573d80debSLuiz Augusto von Dentz BT_DBG("%s", hdev->name); 391673d80debSLuiz Augusto von Dentz 3917bf4c6325SGustavo F. Padovan rcu_read_lock(); 3918bf4c6325SGustavo F. Padovan 3919bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(conn, &h->list, list) { 392073d80debSLuiz Augusto von Dentz struct hci_chan *tmp; 392173d80debSLuiz Augusto von Dentz 392273d80debSLuiz Augusto von Dentz if (conn->type != type) 392373d80debSLuiz Augusto von Dentz continue; 392473d80debSLuiz Augusto von Dentz 392573d80debSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 392673d80debSLuiz Augusto von Dentz continue; 392773d80debSLuiz Augusto von Dentz 392873d80debSLuiz Augusto von Dentz conn_num++; 392973d80debSLuiz Augusto von Dentz 39308192edefSGustavo F. Padovan list_for_each_entry_rcu(tmp, &conn->chan_list, list) { 393173d80debSLuiz Augusto von Dentz struct sk_buff *skb; 393273d80debSLuiz Augusto von Dentz 393373d80debSLuiz Augusto von Dentz if (skb_queue_empty(&tmp->data_q)) 393473d80debSLuiz Augusto von Dentz continue; 393573d80debSLuiz Augusto von Dentz 393673d80debSLuiz Augusto von Dentz skb = skb_peek(&tmp->data_q); 393773d80debSLuiz Augusto von Dentz if (skb->priority < cur_prio) 393873d80debSLuiz Augusto von Dentz continue; 393973d80debSLuiz Augusto von Dentz 394073d80debSLuiz Augusto von Dentz if (skb->priority > cur_prio) { 394173d80debSLuiz Augusto von Dentz num = 0; 394273d80debSLuiz Augusto von Dentz min = ~0; 394373d80debSLuiz Augusto von Dentz cur_prio = skb->priority; 394473d80debSLuiz Augusto von Dentz } 394573d80debSLuiz Augusto von Dentz 394673d80debSLuiz Augusto von Dentz num++; 394773d80debSLuiz Augusto von Dentz 394873d80debSLuiz Augusto von Dentz if (conn->sent < min) { 394973d80debSLuiz Augusto von Dentz min = conn->sent; 395073d80debSLuiz Augusto von Dentz chan = tmp; 395173d80debSLuiz Augusto von Dentz } 395273d80debSLuiz Augusto von Dentz } 395373d80debSLuiz Augusto von Dentz 395473d80debSLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == conn_num) 395573d80debSLuiz Augusto von Dentz break; 395673d80debSLuiz Augusto von Dentz } 395773d80debSLuiz Augusto von Dentz 3958bf4c6325SGustavo F. Padovan rcu_read_unlock(); 3959bf4c6325SGustavo F. Padovan 396073d80debSLuiz Augusto von Dentz if (!chan) 396173d80debSLuiz Augusto von Dentz return NULL; 396273d80debSLuiz Augusto von Dentz 396373d80debSLuiz Augusto von Dentz switch (chan->conn->type) { 396473d80debSLuiz Augusto von Dentz case ACL_LINK: 396573d80debSLuiz Augusto von Dentz cnt = hdev->acl_cnt; 396673d80debSLuiz Augusto von Dentz break; 3967bd1eb66bSAndrei Emeltchenko case AMP_LINK: 3968bd1eb66bSAndrei Emeltchenko cnt = hdev->block_cnt; 3969bd1eb66bSAndrei Emeltchenko break; 397073d80debSLuiz Augusto von Dentz case SCO_LINK: 397173d80debSLuiz Augusto von Dentz case ESCO_LINK: 397273d80debSLuiz Augusto von Dentz cnt = hdev->sco_cnt; 397373d80debSLuiz Augusto von Dentz break; 397473d80debSLuiz Augusto von Dentz case LE_LINK: 397573d80debSLuiz Augusto von Dentz cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; 397673d80debSLuiz Augusto von Dentz break; 397773d80debSLuiz Augusto von Dentz default: 397873d80debSLuiz Augusto von Dentz cnt = 0; 397973d80debSLuiz Augusto von Dentz BT_ERR("Unknown link type"); 398073d80debSLuiz Augusto von Dentz } 398173d80debSLuiz Augusto von Dentz 398273d80debSLuiz Augusto von Dentz q = cnt / num; 398373d80debSLuiz Augusto von Dentz *quote = q ? q : 1; 398473d80debSLuiz Augusto von Dentz BT_DBG("chan %p quote %d", chan, *quote); 398573d80debSLuiz Augusto von Dentz return chan; 398673d80debSLuiz Augusto von Dentz } 398773d80debSLuiz Augusto von Dentz 398802b20f0bSLuiz Augusto von Dentz static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) 398902b20f0bSLuiz Augusto von Dentz { 399002b20f0bSLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 399102b20f0bSLuiz Augusto von Dentz struct hci_conn *conn; 399202b20f0bSLuiz Augusto von Dentz int num = 0; 399302b20f0bSLuiz Augusto von Dentz 399402b20f0bSLuiz Augusto von Dentz BT_DBG("%s", hdev->name); 399502b20f0bSLuiz Augusto von Dentz 3996bf4c6325SGustavo F. Padovan rcu_read_lock(); 3997bf4c6325SGustavo F. Padovan 3998bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(conn, &h->list, list) { 399902b20f0bSLuiz Augusto von Dentz struct hci_chan *chan; 400002b20f0bSLuiz Augusto von Dentz 400102b20f0bSLuiz Augusto von Dentz if (conn->type != type) 400202b20f0bSLuiz Augusto von Dentz continue; 400302b20f0bSLuiz Augusto von Dentz 400402b20f0bSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 400502b20f0bSLuiz Augusto von Dentz continue; 400602b20f0bSLuiz Augusto von Dentz 400702b20f0bSLuiz Augusto von Dentz num++; 400802b20f0bSLuiz Augusto von Dentz 40098192edefSGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_list, list) { 401002b20f0bSLuiz Augusto von Dentz struct sk_buff *skb; 401102b20f0bSLuiz Augusto von Dentz 401202b20f0bSLuiz Augusto von Dentz if (chan->sent) { 401302b20f0bSLuiz Augusto von Dentz chan->sent = 0; 401402b20f0bSLuiz Augusto von Dentz continue; 401502b20f0bSLuiz Augusto von Dentz } 401602b20f0bSLuiz Augusto von Dentz 401702b20f0bSLuiz Augusto von Dentz if (skb_queue_empty(&chan->data_q)) 401802b20f0bSLuiz Augusto von Dentz continue; 401902b20f0bSLuiz Augusto von Dentz 402002b20f0bSLuiz Augusto von Dentz skb = skb_peek(&chan->data_q); 402102b20f0bSLuiz Augusto von Dentz if (skb->priority >= HCI_PRIO_MAX - 1) 402202b20f0bSLuiz Augusto von Dentz continue; 402302b20f0bSLuiz Augusto von Dentz 402402b20f0bSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX - 1; 402502b20f0bSLuiz Augusto von Dentz 402602b20f0bSLuiz Augusto von Dentz BT_DBG("chan %p skb %p promoted to %d", chan, skb, 402702b20f0bSLuiz Augusto von Dentz skb->priority); 402802b20f0bSLuiz Augusto von Dentz } 402902b20f0bSLuiz Augusto von Dentz 403002b20f0bSLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == num) 403102b20f0bSLuiz Augusto von Dentz break; 403202b20f0bSLuiz Augusto von Dentz } 4033bf4c6325SGustavo F. Padovan 4034bf4c6325SGustavo F. Padovan rcu_read_unlock(); 4035bf4c6325SGustavo F. Padovan 403602b20f0bSLuiz Augusto von Dentz } 403702b20f0bSLuiz Augusto von Dentz 4038b71d385aSAndrei Emeltchenko static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) 4039b71d385aSAndrei Emeltchenko { 4040b71d385aSAndrei Emeltchenko /* Calculate count of blocks used by this packet */ 4041b71d385aSAndrei Emeltchenko return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); 4042b71d385aSAndrei Emeltchenko } 4043b71d385aSAndrei Emeltchenko 40446039aa73SGustavo Padovan static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) 40451da177e4SLinus Torvalds { 4046d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 40471da177e4SLinus Torvalds /* ACL tx timeout must be longer than maximum 40481da177e4SLinus Torvalds * link supervision timeout (40.9 seconds) */ 404963d2bc1bSAndrei Emeltchenko if (!cnt && time_after(jiffies, hdev->acl_last_tx + 40505f246e89SAndrei Emeltchenko HCI_ACL_TX_TIMEOUT)) 4051bae1f5d9SVille Tervo hci_link_tx_to(hdev, ACL_LINK); 40521da177e4SLinus Torvalds } 405363d2bc1bSAndrei Emeltchenko } 40541da177e4SLinus Torvalds 40556039aa73SGustavo Padovan static void hci_sched_acl_pkt(struct hci_dev *hdev) 405663d2bc1bSAndrei Emeltchenko { 405763d2bc1bSAndrei Emeltchenko unsigned int cnt = hdev->acl_cnt; 405863d2bc1bSAndrei Emeltchenko struct hci_chan *chan; 405963d2bc1bSAndrei Emeltchenko struct sk_buff *skb; 406063d2bc1bSAndrei Emeltchenko int quote; 406163d2bc1bSAndrei Emeltchenko 406263d2bc1bSAndrei Emeltchenko __check_timeout(hdev, cnt); 406304837f64SMarcel Holtmann 406473d80debSLuiz Augusto von Dentz while (hdev->acl_cnt && 406573d80debSLuiz Augusto von Dentz (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { 4066ec1cce24SLuiz Augusto von Dentz u32 priority = (skb_peek(&chan->data_q))->priority; 4067ec1cce24SLuiz Augusto von Dentz while (quote-- && (skb = skb_peek(&chan->data_q))) { 406873d80debSLuiz Augusto von Dentz BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 406973d80debSLuiz Augusto von Dentz skb->len, skb->priority); 407073d80debSLuiz Augusto von Dentz 4071ec1cce24SLuiz Augusto von Dentz /* Stop if priority has changed */ 4072ec1cce24SLuiz Augusto von Dentz if (skb->priority < priority) 4073ec1cce24SLuiz Augusto von Dentz break; 4074ec1cce24SLuiz Augusto von Dentz 4075ec1cce24SLuiz Augusto von Dentz skb = skb_dequeue(&chan->data_q); 4076ec1cce24SLuiz Augusto von Dentz 407773d80debSLuiz Augusto von Dentz hci_conn_enter_active_mode(chan->conn, 407873d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active); 407904837f64SMarcel Holtmann 408057d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 40811da177e4SLinus Torvalds hdev->acl_last_tx = jiffies; 40821da177e4SLinus Torvalds 40831da177e4SLinus Torvalds hdev->acl_cnt--; 408473d80debSLuiz Augusto von Dentz chan->sent++; 408573d80debSLuiz Augusto von Dentz chan->conn->sent++; 40861da177e4SLinus Torvalds } 40871da177e4SLinus Torvalds } 408802b20f0bSLuiz Augusto von Dentz 408902b20f0bSLuiz Augusto von Dentz if (cnt != hdev->acl_cnt) 409002b20f0bSLuiz Augusto von Dentz hci_prio_recalculate(hdev, ACL_LINK); 40911da177e4SLinus Torvalds } 40921da177e4SLinus Torvalds 40936039aa73SGustavo Padovan static void hci_sched_acl_blk(struct hci_dev *hdev) 4094b71d385aSAndrei Emeltchenko { 409563d2bc1bSAndrei Emeltchenko unsigned int cnt = hdev->block_cnt; 4096b71d385aSAndrei Emeltchenko struct hci_chan *chan; 4097b71d385aSAndrei Emeltchenko struct sk_buff *skb; 4098b71d385aSAndrei Emeltchenko int quote; 4099bd1eb66bSAndrei Emeltchenko u8 type; 4100b71d385aSAndrei Emeltchenko 410163d2bc1bSAndrei Emeltchenko __check_timeout(hdev, cnt); 4102b71d385aSAndrei Emeltchenko 4103bd1eb66bSAndrei Emeltchenko BT_DBG("%s", hdev->name); 4104bd1eb66bSAndrei Emeltchenko 4105bd1eb66bSAndrei Emeltchenko if (hdev->dev_type == HCI_AMP) 4106bd1eb66bSAndrei Emeltchenko type = AMP_LINK; 4107bd1eb66bSAndrei Emeltchenko else 4108bd1eb66bSAndrei Emeltchenko type = ACL_LINK; 4109bd1eb66bSAndrei Emeltchenko 4110b71d385aSAndrei Emeltchenko while (hdev->block_cnt > 0 && 4111bd1eb66bSAndrei Emeltchenko (chan = hci_chan_sent(hdev, type, "e))) { 4112b71d385aSAndrei Emeltchenko u32 priority = (skb_peek(&chan->data_q))->priority; 4113b71d385aSAndrei Emeltchenko while (quote > 0 && (skb = skb_peek(&chan->data_q))) { 4114b71d385aSAndrei Emeltchenko int blocks; 4115b71d385aSAndrei Emeltchenko 4116b71d385aSAndrei Emeltchenko BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 4117b71d385aSAndrei Emeltchenko skb->len, skb->priority); 4118b71d385aSAndrei Emeltchenko 4119b71d385aSAndrei Emeltchenko /* Stop if priority has changed */ 4120b71d385aSAndrei Emeltchenko if (skb->priority < priority) 4121b71d385aSAndrei Emeltchenko break; 4122b71d385aSAndrei Emeltchenko 4123b71d385aSAndrei Emeltchenko skb = skb_dequeue(&chan->data_q); 4124b71d385aSAndrei Emeltchenko 4125b71d385aSAndrei Emeltchenko blocks = __get_blocks(hdev, skb); 4126b71d385aSAndrei Emeltchenko if (blocks > hdev->block_cnt) 4127b71d385aSAndrei Emeltchenko return; 4128b71d385aSAndrei Emeltchenko 4129b71d385aSAndrei Emeltchenko hci_conn_enter_active_mode(chan->conn, 4130b71d385aSAndrei Emeltchenko bt_cb(skb)->force_active); 4131b71d385aSAndrei Emeltchenko 413257d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 4133b71d385aSAndrei Emeltchenko hdev->acl_last_tx = jiffies; 4134b71d385aSAndrei Emeltchenko 4135b71d385aSAndrei Emeltchenko hdev->block_cnt -= blocks; 4136b71d385aSAndrei Emeltchenko quote -= blocks; 4137b71d385aSAndrei Emeltchenko 4138b71d385aSAndrei Emeltchenko chan->sent += blocks; 4139b71d385aSAndrei Emeltchenko chan->conn->sent += blocks; 4140b71d385aSAndrei Emeltchenko } 4141b71d385aSAndrei Emeltchenko } 4142b71d385aSAndrei Emeltchenko 4143b71d385aSAndrei Emeltchenko if (cnt != hdev->block_cnt) 4144bd1eb66bSAndrei Emeltchenko hci_prio_recalculate(hdev, type); 4145b71d385aSAndrei Emeltchenko } 4146b71d385aSAndrei Emeltchenko 41476039aa73SGustavo Padovan static void hci_sched_acl(struct hci_dev *hdev) 4148b71d385aSAndrei Emeltchenko { 4149b71d385aSAndrei Emeltchenko BT_DBG("%s", hdev->name); 4150b71d385aSAndrei Emeltchenko 4151bd1eb66bSAndrei Emeltchenko /* No ACL link over BR/EDR controller */ 4152bd1eb66bSAndrei Emeltchenko if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) 4153bd1eb66bSAndrei Emeltchenko return; 4154bd1eb66bSAndrei Emeltchenko 4155bd1eb66bSAndrei Emeltchenko /* No AMP link over AMP controller */ 4156bd1eb66bSAndrei Emeltchenko if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) 4157b71d385aSAndrei Emeltchenko return; 4158b71d385aSAndrei Emeltchenko 4159b71d385aSAndrei Emeltchenko switch (hdev->flow_ctl_mode) { 4160b71d385aSAndrei Emeltchenko case HCI_FLOW_CTL_MODE_PACKET_BASED: 4161b71d385aSAndrei Emeltchenko hci_sched_acl_pkt(hdev); 4162b71d385aSAndrei Emeltchenko break; 4163b71d385aSAndrei Emeltchenko 4164b71d385aSAndrei Emeltchenko case HCI_FLOW_CTL_MODE_BLOCK_BASED: 4165b71d385aSAndrei Emeltchenko hci_sched_acl_blk(hdev); 4166b71d385aSAndrei Emeltchenko break; 4167b71d385aSAndrei Emeltchenko } 4168b71d385aSAndrei Emeltchenko } 4169b71d385aSAndrei Emeltchenko 41701da177e4SLinus Torvalds /* Schedule SCO */ 41716039aa73SGustavo Padovan static void hci_sched_sco(struct hci_dev *hdev) 41721da177e4SLinus Torvalds { 41731da177e4SLinus Torvalds struct hci_conn *conn; 41741da177e4SLinus Torvalds struct sk_buff *skb; 41751da177e4SLinus Torvalds int quote; 41761da177e4SLinus Torvalds 41771da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 41781da177e4SLinus Torvalds 417952087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, SCO_LINK)) 418052087a79SLuiz Augusto von Dentz return; 418152087a79SLuiz Augusto von Dentz 41821da177e4SLinus Torvalds while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { 41831da177e4SLinus Torvalds while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 41841da177e4SLinus Torvalds BT_DBG("skb %p len %d", skb, skb->len); 418557d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 41861da177e4SLinus Torvalds 41871da177e4SLinus Torvalds conn->sent++; 41881da177e4SLinus Torvalds if (conn->sent == ~0) 41891da177e4SLinus Torvalds conn->sent = 0; 41901da177e4SLinus Torvalds } 41911da177e4SLinus Torvalds } 41921da177e4SLinus Torvalds } 41931da177e4SLinus Torvalds 41946039aa73SGustavo Padovan static void hci_sched_esco(struct hci_dev *hdev) 4195b6a0dc82SMarcel Holtmann { 4196b6a0dc82SMarcel Holtmann struct hci_conn *conn; 4197b6a0dc82SMarcel Holtmann struct sk_buff *skb; 4198b6a0dc82SMarcel Holtmann int quote; 4199b6a0dc82SMarcel Holtmann 4200b6a0dc82SMarcel Holtmann BT_DBG("%s", hdev->name); 4201b6a0dc82SMarcel Holtmann 420252087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, ESCO_LINK)) 420352087a79SLuiz Augusto von Dentz return; 420452087a79SLuiz Augusto von Dentz 42058fc9ced3SGustavo Padovan while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, 42068fc9ced3SGustavo Padovan "e))) { 4207b6a0dc82SMarcel Holtmann while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 4208b6a0dc82SMarcel Holtmann BT_DBG("skb %p len %d", skb, skb->len); 420957d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 4210b6a0dc82SMarcel Holtmann 4211b6a0dc82SMarcel Holtmann conn->sent++; 4212b6a0dc82SMarcel Holtmann if (conn->sent == ~0) 4213b6a0dc82SMarcel Holtmann conn->sent = 0; 4214b6a0dc82SMarcel Holtmann } 4215b6a0dc82SMarcel Holtmann } 4216b6a0dc82SMarcel Holtmann } 4217b6a0dc82SMarcel Holtmann 42186039aa73SGustavo Padovan static void hci_sched_le(struct hci_dev *hdev) 42196ed58ec5SVille Tervo { 422073d80debSLuiz Augusto von Dentz struct hci_chan *chan; 42216ed58ec5SVille Tervo struct sk_buff *skb; 422202b20f0bSLuiz Augusto von Dentz int quote, cnt, tmp; 42236ed58ec5SVille Tervo 42246ed58ec5SVille Tervo BT_DBG("%s", hdev->name); 42256ed58ec5SVille Tervo 422652087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, LE_LINK)) 422752087a79SLuiz Augusto von Dentz return; 422852087a79SLuiz Augusto von Dentz 4229d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 42306ed58ec5SVille Tervo /* LE tx timeout must be longer than maximum 42316ed58ec5SVille Tervo * link supervision timeout (40.9 seconds) */ 4232bae1f5d9SVille Tervo if (!hdev->le_cnt && hdev->le_pkts && 42336ed58ec5SVille Tervo time_after(jiffies, hdev->le_last_tx + HZ * 45)) 4234bae1f5d9SVille Tervo hci_link_tx_to(hdev, LE_LINK); 42356ed58ec5SVille Tervo } 42366ed58ec5SVille Tervo 42376ed58ec5SVille Tervo cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; 423802b20f0bSLuiz Augusto von Dentz tmp = cnt; 423973d80debSLuiz Augusto von Dentz while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { 4240ec1cce24SLuiz Augusto von Dentz u32 priority = (skb_peek(&chan->data_q))->priority; 4241ec1cce24SLuiz Augusto von Dentz while (quote-- && (skb = skb_peek(&chan->data_q))) { 424273d80debSLuiz Augusto von Dentz BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 424373d80debSLuiz Augusto von Dentz skb->len, skb->priority); 42446ed58ec5SVille Tervo 4245ec1cce24SLuiz Augusto von Dentz /* Stop if priority has changed */ 4246ec1cce24SLuiz Augusto von Dentz if (skb->priority < priority) 4247ec1cce24SLuiz Augusto von Dentz break; 4248ec1cce24SLuiz Augusto von Dentz 4249ec1cce24SLuiz Augusto von Dentz skb = skb_dequeue(&chan->data_q); 4250ec1cce24SLuiz Augusto von Dentz 425157d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 42526ed58ec5SVille Tervo hdev->le_last_tx = jiffies; 42536ed58ec5SVille Tervo 42546ed58ec5SVille Tervo cnt--; 425573d80debSLuiz Augusto von Dentz chan->sent++; 425673d80debSLuiz Augusto von Dentz chan->conn->sent++; 42576ed58ec5SVille Tervo } 42586ed58ec5SVille Tervo } 425973d80debSLuiz Augusto von Dentz 42606ed58ec5SVille Tervo if (hdev->le_pkts) 42616ed58ec5SVille Tervo hdev->le_cnt = cnt; 42626ed58ec5SVille Tervo else 42636ed58ec5SVille Tervo hdev->acl_cnt = cnt; 426402b20f0bSLuiz Augusto von Dentz 426502b20f0bSLuiz Augusto von Dentz if (cnt != tmp) 426602b20f0bSLuiz Augusto von Dentz hci_prio_recalculate(hdev, LE_LINK); 42676ed58ec5SVille Tervo } 42686ed58ec5SVille Tervo 42693eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work) 42701da177e4SLinus Torvalds { 42713eff45eaSGustavo F. Padovan struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); 42721da177e4SLinus Torvalds struct sk_buff *skb; 42731da177e4SLinus Torvalds 42746ed58ec5SVille Tervo BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, 42756ed58ec5SVille Tervo hdev->sco_cnt, hdev->le_cnt); 42761da177e4SLinus Torvalds 4277d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 42781da177e4SLinus Torvalds /* Schedule queues and send stuff to HCI driver */ 42791da177e4SLinus Torvalds hci_sched_acl(hdev); 42801da177e4SLinus Torvalds hci_sched_sco(hdev); 4281b6a0dc82SMarcel Holtmann hci_sched_esco(hdev); 42826ed58ec5SVille Tervo hci_sched_le(hdev); 428352de599eSMarcel Holtmann } 42846ed58ec5SVille Tervo 42851da177e4SLinus Torvalds /* Send next queued raw (unknown type) packet */ 42861da177e4SLinus Torvalds while ((skb = skb_dequeue(&hdev->raw_q))) 428757d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 42881da177e4SLinus Torvalds } 42891da177e4SLinus Torvalds 429025985edcSLucas De Marchi /* ----- HCI RX task (incoming data processing) ----- */ 42911da177e4SLinus Torvalds 42921da177e4SLinus Torvalds /* ACL data packet */ 42936039aa73SGustavo Padovan static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) 42941da177e4SLinus Torvalds { 42951da177e4SLinus Torvalds struct hci_acl_hdr *hdr = (void *) skb->data; 42961da177e4SLinus Torvalds struct hci_conn *conn; 42971da177e4SLinus Torvalds __u16 handle, flags; 42981da177e4SLinus Torvalds 42991da177e4SLinus Torvalds skb_pull(skb, HCI_ACL_HDR_SIZE); 43001da177e4SLinus Torvalds 43011da177e4SLinus Torvalds handle = __le16_to_cpu(hdr->handle); 43021da177e4SLinus Torvalds flags = hci_flags(handle); 43031da177e4SLinus Torvalds handle = hci_handle(handle); 43041da177e4SLinus Torvalds 4305f0e09510SAndrei Emeltchenko BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len, 4306a8c5fb1aSGustavo Padovan handle, flags); 43071da177e4SLinus Torvalds 43081da177e4SLinus Torvalds hdev->stat.acl_rx++; 43091da177e4SLinus Torvalds 43101da177e4SLinus Torvalds hci_dev_lock(hdev); 43111da177e4SLinus Torvalds conn = hci_conn_hash_lookup_handle(hdev, handle); 43121da177e4SLinus Torvalds hci_dev_unlock(hdev); 43131da177e4SLinus Torvalds 43141da177e4SLinus Torvalds if (conn) { 431565983fc7SMat Martineau hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); 431604837f64SMarcel Holtmann 43171da177e4SLinus Torvalds /* Send to upper protocol */ 4318686ebf28SUlisses Furquim l2cap_recv_acldata(conn, skb, flags); 43191da177e4SLinus Torvalds return; 43201da177e4SLinus Torvalds } else { 43211da177e4SLinus Torvalds BT_ERR("%s ACL packet for unknown connection handle %d", 43221da177e4SLinus Torvalds hdev->name, handle); 43231da177e4SLinus Torvalds } 43241da177e4SLinus Torvalds 43251da177e4SLinus Torvalds kfree_skb(skb); 43261da177e4SLinus Torvalds } 43271da177e4SLinus Torvalds 43281da177e4SLinus Torvalds /* SCO data packet */ 43296039aa73SGustavo Padovan static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) 43301da177e4SLinus Torvalds { 43311da177e4SLinus Torvalds struct hci_sco_hdr *hdr = (void *) skb->data; 43321da177e4SLinus Torvalds struct hci_conn *conn; 43331da177e4SLinus Torvalds __u16 handle; 43341da177e4SLinus Torvalds 43351da177e4SLinus Torvalds skb_pull(skb, HCI_SCO_HDR_SIZE); 43361da177e4SLinus Torvalds 43371da177e4SLinus Torvalds handle = __le16_to_cpu(hdr->handle); 43381da177e4SLinus Torvalds 4339f0e09510SAndrei Emeltchenko BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle); 43401da177e4SLinus Torvalds 43411da177e4SLinus Torvalds hdev->stat.sco_rx++; 43421da177e4SLinus Torvalds 43431da177e4SLinus Torvalds hci_dev_lock(hdev); 43441da177e4SLinus Torvalds conn = hci_conn_hash_lookup_handle(hdev, handle); 43451da177e4SLinus Torvalds hci_dev_unlock(hdev); 43461da177e4SLinus Torvalds 43471da177e4SLinus Torvalds if (conn) { 43481da177e4SLinus Torvalds /* Send to upper protocol */ 4349686ebf28SUlisses Furquim sco_recv_scodata(conn, skb); 43501da177e4SLinus Torvalds return; 43511da177e4SLinus Torvalds } else { 43521da177e4SLinus Torvalds BT_ERR("%s SCO packet for unknown connection handle %d", 43531da177e4SLinus Torvalds hdev->name, handle); 43541da177e4SLinus Torvalds } 43551da177e4SLinus Torvalds 43561da177e4SLinus Torvalds kfree_skb(skb); 43571da177e4SLinus Torvalds } 43581da177e4SLinus Torvalds 43599238f36aSJohan Hedberg static bool hci_req_is_complete(struct hci_dev *hdev) 43609238f36aSJohan Hedberg { 43619238f36aSJohan Hedberg struct sk_buff *skb; 43629238f36aSJohan Hedberg 43639238f36aSJohan Hedberg skb = skb_peek(&hdev->cmd_q); 43649238f36aSJohan Hedberg if (!skb) 43659238f36aSJohan Hedberg return true; 43669238f36aSJohan Hedberg 4367db6e3e8dSJohan Hedberg return bt_cb(skb)->req.start; 43689238f36aSJohan Hedberg } 43699238f36aSJohan Hedberg 437042c6b129SJohan Hedberg static void hci_resend_last(struct hci_dev *hdev) 437142c6b129SJohan Hedberg { 437242c6b129SJohan Hedberg struct hci_command_hdr *sent; 437342c6b129SJohan Hedberg struct sk_buff *skb; 437442c6b129SJohan Hedberg u16 opcode; 437542c6b129SJohan Hedberg 437642c6b129SJohan Hedberg if (!hdev->sent_cmd) 437742c6b129SJohan Hedberg return; 437842c6b129SJohan Hedberg 437942c6b129SJohan Hedberg sent = (void *) hdev->sent_cmd->data; 438042c6b129SJohan Hedberg opcode = __le16_to_cpu(sent->opcode); 438142c6b129SJohan Hedberg if (opcode == HCI_OP_RESET) 438242c6b129SJohan Hedberg return; 438342c6b129SJohan Hedberg 438442c6b129SJohan Hedberg skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); 438542c6b129SJohan Hedberg if (!skb) 438642c6b129SJohan Hedberg return; 438742c6b129SJohan Hedberg 438842c6b129SJohan Hedberg skb_queue_head(&hdev->cmd_q, skb); 438942c6b129SJohan Hedberg queue_work(hdev->workqueue, &hdev->cmd_work); 439042c6b129SJohan Hedberg } 439142c6b129SJohan Hedberg 4392e6214487SJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status, 4393e6214487SJohan Hedberg hci_req_complete_t *req_complete, 4394e6214487SJohan Hedberg hci_req_complete_skb_t *req_complete_skb) 43959238f36aSJohan Hedberg { 43969238f36aSJohan Hedberg struct sk_buff *skb; 43979238f36aSJohan Hedberg unsigned long flags; 43989238f36aSJohan Hedberg 43999238f36aSJohan Hedberg BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); 44009238f36aSJohan Hedberg 440142c6b129SJohan Hedberg /* If the completed command doesn't match the last one that was 440242c6b129SJohan Hedberg * sent we need to do special handling of it. 44039238f36aSJohan Hedberg */ 440442c6b129SJohan Hedberg if (!hci_sent_cmd_data(hdev, opcode)) { 440542c6b129SJohan Hedberg /* Some CSR based controllers generate a spontaneous 440642c6b129SJohan Hedberg * reset complete event during init and any pending 440742c6b129SJohan Hedberg * command will never be completed. In such a case we 440842c6b129SJohan Hedberg * need to resend whatever was the last sent 440942c6b129SJohan Hedberg * command. 441042c6b129SJohan Hedberg */ 441142c6b129SJohan Hedberg if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) 441242c6b129SJohan Hedberg hci_resend_last(hdev); 441342c6b129SJohan Hedberg 44149238f36aSJohan Hedberg return; 441542c6b129SJohan Hedberg } 44169238f36aSJohan Hedberg 44179238f36aSJohan Hedberg /* If the command succeeded and there's still more commands in 44189238f36aSJohan Hedberg * this request the request is not yet complete. 44199238f36aSJohan Hedberg */ 44209238f36aSJohan Hedberg if (!status && !hci_req_is_complete(hdev)) 44219238f36aSJohan Hedberg return; 44229238f36aSJohan Hedberg 44239238f36aSJohan Hedberg /* If this was the last command in a request the complete 44249238f36aSJohan Hedberg * callback would be found in hdev->sent_cmd instead of the 44259238f36aSJohan Hedberg * command queue (hdev->cmd_q). 44269238f36aSJohan Hedberg */ 4427e6214487SJohan Hedberg if (bt_cb(hdev->sent_cmd)->req.complete) { 4428e6214487SJohan Hedberg *req_complete = bt_cb(hdev->sent_cmd)->req.complete; 4429e6214487SJohan Hedberg return; 44309238f36aSJohan Hedberg } 4431e6214487SJohan Hedberg 4432e6214487SJohan Hedberg if (bt_cb(hdev->sent_cmd)->req.complete_skb) { 4433e6214487SJohan Hedberg *req_complete_skb = bt_cb(hdev->sent_cmd)->req.complete_skb; 4434e6214487SJohan Hedberg return; 443553e21fbcSJohan Hedberg } 44369238f36aSJohan Hedberg 44379238f36aSJohan Hedberg /* Remove all pending commands belonging to this request */ 44389238f36aSJohan Hedberg spin_lock_irqsave(&hdev->cmd_q.lock, flags); 44399238f36aSJohan Hedberg while ((skb = __skb_dequeue(&hdev->cmd_q))) { 4440db6e3e8dSJohan Hedberg if (bt_cb(skb)->req.start) { 44419238f36aSJohan Hedberg __skb_queue_head(&hdev->cmd_q, skb); 44429238f36aSJohan Hedberg break; 44439238f36aSJohan Hedberg } 44449238f36aSJohan Hedberg 4445e6214487SJohan Hedberg *req_complete = bt_cb(skb)->req.complete; 4446e6214487SJohan Hedberg *req_complete_skb = bt_cb(skb)->req.complete_skb; 44479238f36aSJohan Hedberg kfree_skb(skb); 44489238f36aSJohan Hedberg } 44499238f36aSJohan Hedberg spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); 44509238f36aSJohan Hedberg } 44519238f36aSJohan Hedberg 4452b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work) 44531da177e4SLinus Torvalds { 4454b78752ccSMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); 44551da177e4SLinus Torvalds struct sk_buff *skb; 44561da177e4SLinus Torvalds 44571da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 44581da177e4SLinus Torvalds 44591da177e4SLinus Torvalds while ((skb = skb_dequeue(&hdev->rx_q))) { 4460cd82e61cSMarcel Holtmann /* Send copy to monitor */ 4461cd82e61cSMarcel Holtmann hci_send_to_monitor(hdev, skb); 4462cd82e61cSMarcel Holtmann 44631da177e4SLinus Torvalds if (atomic_read(&hdev->promisc)) { 44641da177e4SLinus Torvalds /* Send copy to the sockets */ 4465470fe1b5SMarcel Holtmann hci_send_to_sock(hdev, skb); 44661da177e4SLinus Torvalds } 44671da177e4SLinus Torvalds 4468d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 44691da177e4SLinus Torvalds kfree_skb(skb); 44701da177e4SLinus Torvalds continue; 44711da177e4SLinus Torvalds } 44721da177e4SLinus Torvalds 44731da177e4SLinus Torvalds if (test_bit(HCI_INIT, &hdev->flags)) { 44741da177e4SLinus Torvalds /* Don't process data packets in this states. */ 44750d48d939SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 44761da177e4SLinus Torvalds case HCI_ACLDATA_PKT: 44771da177e4SLinus Torvalds case HCI_SCODATA_PKT: 44781da177e4SLinus Torvalds kfree_skb(skb); 44791da177e4SLinus Torvalds continue; 44803ff50b79SStephen Hemminger } 44811da177e4SLinus Torvalds } 44821da177e4SLinus Torvalds 44831da177e4SLinus Torvalds /* Process frame */ 44840d48d939SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 44851da177e4SLinus Torvalds case HCI_EVENT_PKT: 4486b78752ccSMarcel Holtmann BT_DBG("%s Event packet", hdev->name); 44871da177e4SLinus Torvalds hci_event_packet(hdev, skb); 44881da177e4SLinus Torvalds break; 44891da177e4SLinus Torvalds 44901da177e4SLinus Torvalds case HCI_ACLDATA_PKT: 44911da177e4SLinus Torvalds BT_DBG("%s ACL data packet", hdev->name); 44921da177e4SLinus Torvalds hci_acldata_packet(hdev, skb); 44931da177e4SLinus Torvalds break; 44941da177e4SLinus Torvalds 44951da177e4SLinus Torvalds case HCI_SCODATA_PKT: 44961da177e4SLinus Torvalds BT_DBG("%s SCO data packet", hdev->name); 44971da177e4SLinus Torvalds hci_scodata_packet(hdev, skb); 44981da177e4SLinus Torvalds break; 44991da177e4SLinus Torvalds 45001da177e4SLinus Torvalds default: 45011da177e4SLinus Torvalds kfree_skb(skb); 45021da177e4SLinus Torvalds break; 45031da177e4SLinus Torvalds } 45041da177e4SLinus Torvalds } 45051da177e4SLinus Torvalds } 45061da177e4SLinus Torvalds 4507c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work) 45081da177e4SLinus Torvalds { 4509c347b765SGustavo F. Padovan struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); 45101da177e4SLinus Torvalds struct sk_buff *skb; 45111da177e4SLinus Torvalds 45122104786bSAndrei Emeltchenko BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, 45132104786bSAndrei Emeltchenko atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); 45141da177e4SLinus Torvalds 45151da177e4SLinus Torvalds /* Send queued commands */ 45165a08ecceSAndrei Emeltchenko if (atomic_read(&hdev->cmd_cnt)) { 45175a08ecceSAndrei Emeltchenko skb = skb_dequeue(&hdev->cmd_q); 45185a08ecceSAndrei Emeltchenko if (!skb) 45195a08ecceSAndrei Emeltchenko return; 45205a08ecceSAndrei Emeltchenko 45211da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 45221da177e4SLinus Torvalds 4523a675d7f1SMarcel Holtmann hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); 452470f23020SAndrei Emeltchenko if (hdev->sent_cmd) { 45251da177e4SLinus Torvalds atomic_dec(&hdev->cmd_cnt); 452657d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 45277bdb8a5cSSzymon Janc if (test_bit(HCI_RESET, &hdev->flags)) 452865cc2b49SMarcel Holtmann cancel_delayed_work(&hdev->cmd_timer); 45297bdb8a5cSSzymon Janc else 453065cc2b49SMarcel Holtmann schedule_delayed_work(&hdev->cmd_timer, 453165cc2b49SMarcel Holtmann HCI_CMD_TIMEOUT); 45321da177e4SLinus Torvalds } else { 45331da177e4SLinus Torvalds skb_queue_head(&hdev->cmd_q, skb); 4534c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 45351da177e4SLinus Torvalds } 45361da177e4SLinus Torvalds } 45371da177e4SLinus Torvalds } 4538