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 1654b4113d6SMarcel Holtmann hci_req_lock(hdev); 1664b4113d6SMarcel Holtmann err = hdev->set_diag(hdev, enable); 1674b4113d6SMarcel Holtmann hci_req_unlock(hdev); 1684b4113d6SMarcel Holtmann 1694b4113d6SMarcel Holtmann if (err < 0) 1704b4113d6SMarcel Holtmann return err; 1714b4113d6SMarcel Holtmann 1724b4113d6SMarcel Holtmann if (enable) 1734b4113d6SMarcel Holtmann hci_dev_set_flag(hdev, HCI_VENDOR_DIAG); 1744b4113d6SMarcel Holtmann else 1754b4113d6SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG); 1764b4113d6SMarcel Holtmann 1774b4113d6SMarcel Holtmann return count; 1784b4113d6SMarcel Holtmann } 1794b4113d6SMarcel Holtmann 1804b4113d6SMarcel Holtmann static const struct file_operations vendor_diag_fops = { 1814b4113d6SMarcel Holtmann .open = simple_open, 1824b4113d6SMarcel Holtmann .read = vendor_diag_read, 1834b4113d6SMarcel Holtmann .write = vendor_diag_write, 1844b4113d6SMarcel Holtmann .llseek = default_llseek, 1854b4113d6SMarcel Holtmann }; 1864b4113d6SMarcel Holtmann 187f640ee98SMarcel Holtmann static void hci_debugfs_create_basic(struct hci_dev *hdev) 188f640ee98SMarcel Holtmann { 189f640ee98SMarcel Holtmann debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev, 190f640ee98SMarcel Holtmann &dut_mode_fops); 191f640ee98SMarcel Holtmann 192f640ee98SMarcel Holtmann if (hdev->set_diag) 193f640ee98SMarcel Holtmann debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev, 194f640ee98SMarcel Holtmann &vendor_diag_fops); 195f640ee98SMarcel Holtmann } 196f640ee98SMarcel Holtmann 1971da177e4SLinus Torvalds /* ---- HCI requests ---- */ 1981da177e4SLinus Torvalds 199f60cb305SJohan Hedberg static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, 200f60cb305SJohan Hedberg struct sk_buff *skb) 2011da177e4SLinus Torvalds { 20242c6b129SJohan Hedberg BT_DBG("%s result 0x%2.2x", hdev->name, result); 20375fb0e32SJohan Hedberg 2041da177e4SLinus Torvalds if (hdev->req_status == HCI_REQ_PEND) { 2051da177e4SLinus Torvalds hdev->req_result = result; 2061da177e4SLinus Torvalds hdev->req_status = HCI_REQ_DONE; 207f60cb305SJohan Hedberg if (skb) 208f60cb305SJohan Hedberg hdev->req_skb = skb_get(skb); 2091da177e4SLinus Torvalds wake_up_interruptible(&hdev->req_wait_q); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static void hci_req_cancel(struct hci_dev *hdev, int err) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds BT_DBG("%s err 0x%2.2x", hdev->name, err); 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if (hdev->req_status == HCI_REQ_PEND) { 2181da177e4SLinus Torvalds hdev->req_result = err; 2191da177e4SLinus Torvalds hdev->req_status = HCI_REQ_CANCELED; 2201da177e4SLinus Torvalds wake_up_interruptible(&hdev->req_wait_q); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2247b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, 22507dc93ddSJohan Hedberg const void *param, u8 event, u32 timeout) 22675e84b7cSJohan Hedberg { 22775e84b7cSJohan Hedberg DECLARE_WAITQUEUE(wait, current); 22875e84b7cSJohan Hedberg struct hci_request req; 229f60cb305SJohan Hedberg struct sk_buff *skb; 23075e84b7cSJohan Hedberg int err = 0; 23175e84b7cSJohan Hedberg 23275e84b7cSJohan Hedberg BT_DBG("%s", hdev->name); 23375e84b7cSJohan Hedberg 23475e84b7cSJohan Hedberg hci_req_init(&req, hdev); 23575e84b7cSJohan Hedberg 2367b1abbbeSJohan Hedberg hci_req_add_ev(&req, opcode, plen, param, event); 23775e84b7cSJohan Hedberg 23875e84b7cSJohan Hedberg hdev->req_status = HCI_REQ_PEND; 23975e84b7cSJohan Hedberg 24075e84b7cSJohan Hedberg add_wait_queue(&hdev->req_wait_q, &wait); 24175e84b7cSJohan Hedberg set_current_state(TASK_INTERRUPTIBLE); 24275e84b7cSJohan Hedberg 243f60cb305SJohan Hedberg err = hci_req_run_skb(&req, hci_req_sync_complete); 244039fada5SChan-yeol Park if (err < 0) { 245039fada5SChan-yeol Park remove_wait_queue(&hdev->req_wait_q, &wait); 24622a3ceabSJohan Hedberg set_current_state(TASK_RUNNING); 247039fada5SChan-yeol Park return ERR_PTR(err); 248039fada5SChan-yeol Park } 249039fada5SChan-yeol Park 25075e84b7cSJohan Hedberg schedule_timeout(timeout); 25175e84b7cSJohan Hedberg 25275e84b7cSJohan Hedberg remove_wait_queue(&hdev->req_wait_q, &wait); 25375e84b7cSJohan Hedberg 25475e84b7cSJohan Hedberg if (signal_pending(current)) 25575e84b7cSJohan Hedberg return ERR_PTR(-EINTR); 25675e84b7cSJohan Hedberg 25775e84b7cSJohan Hedberg switch (hdev->req_status) { 25875e84b7cSJohan Hedberg case HCI_REQ_DONE: 25975e84b7cSJohan Hedberg err = -bt_to_errno(hdev->req_result); 26075e84b7cSJohan Hedberg break; 26175e84b7cSJohan Hedberg 26275e84b7cSJohan Hedberg case HCI_REQ_CANCELED: 26375e84b7cSJohan Hedberg err = -hdev->req_result; 26475e84b7cSJohan Hedberg break; 26575e84b7cSJohan Hedberg 26675e84b7cSJohan Hedberg default: 26775e84b7cSJohan Hedberg err = -ETIMEDOUT; 26875e84b7cSJohan Hedberg break; 26975e84b7cSJohan Hedberg } 27075e84b7cSJohan Hedberg 27175e84b7cSJohan Hedberg hdev->req_status = hdev->req_result = 0; 272f60cb305SJohan Hedberg skb = hdev->req_skb; 273f60cb305SJohan Hedberg hdev->req_skb = NULL; 27475e84b7cSJohan Hedberg 27575e84b7cSJohan Hedberg BT_DBG("%s end: err %d", hdev->name, err); 27675e84b7cSJohan Hedberg 277f60cb305SJohan Hedberg if (err < 0) { 278f60cb305SJohan Hedberg kfree_skb(skb); 27975e84b7cSJohan Hedberg return ERR_PTR(err); 280f60cb305SJohan Hedberg } 28175e84b7cSJohan Hedberg 282757aa0b5SJohan Hedberg if (!skb) 283757aa0b5SJohan Hedberg return ERR_PTR(-ENODATA); 284757aa0b5SJohan Hedberg 285757aa0b5SJohan Hedberg return skb; 2867b1abbbeSJohan Hedberg } 2877b1abbbeSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync_ev); 2887b1abbbeSJohan Hedberg 2897b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 29007dc93ddSJohan Hedberg const void *param, u32 timeout) 2917b1abbbeSJohan Hedberg { 2927b1abbbeSJohan Hedberg return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); 29375e84b7cSJohan Hedberg } 29475e84b7cSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync); 29575e84b7cSJohan Hedberg 2961da177e4SLinus Torvalds /* Execute request and wait for completion. */ 29701178cd4SJohan Hedberg static int __hci_req_sync(struct hci_dev *hdev, 29842c6b129SJohan Hedberg void (*func)(struct hci_request *req, 29942c6b129SJohan Hedberg unsigned long opt), 3001da177e4SLinus Torvalds unsigned long opt, __u32 timeout) 3011da177e4SLinus Torvalds { 30242c6b129SJohan Hedberg struct hci_request req; 3031da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 3041da177e4SLinus Torvalds int err = 0; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds BT_DBG("%s start", hdev->name); 3071da177e4SLinus Torvalds 30842c6b129SJohan Hedberg hci_req_init(&req, hdev); 30942c6b129SJohan Hedberg 3101da177e4SLinus Torvalds hdev->req_status = HCI_REQ_PEND; 3111da177e4SLinus Torvalds 31242c6b129SJohan Hedberg func(&req, opt); 31353cce22dSJohan Hedberg 314039fada5SChan-yeol Park add_wait_queue(&hdev->req_wait_q, &wait); 315039fada5SChan-yeol Park set_current_state(TASK_INTERRUPTIBLE); 316039fada5SChan-yeol Park 317f60cb305SJohan Hedberg err = hci_req_run_skb(&req, hci_req_sync_complete); 31842c6b129SJohan Hedberg if (err < 0) { 31953cce22dSJohan Hedberg hdev->req_status = 0; 320920c8300SAndre Guedes 321039fada5SChan-yeol Park remove_wait_queue(&hdev->req_wait_q, &wait); 32222a3ceabSJohan Hedberg set_current_state(TASK_RUNNING); 323039fada5SChan-yeol Park 324920c8300SAndre Guedes /* ENODATA means the HCI request command queue is empty. 325920c8300SAndre Guedes * This can happen when a request with conditionals doesn't 326920c8300SAndre Guedes * trigger any commands to be sent. This is normal behavior 327920c8300SAndre Guedes * and should not trigger an error return. 32842c6b129SJohan Hedberg */ 329920c8300SAndre Guedes if (err == -ENODATA) 33042c6b129SJohan Hedberg return 0; 331920c8300SAndre Guedes 332920c8300SAndre Guedes return err; 33353cce22dSJohan Hedberg } 33453cce22dSJohan Hedberg 3351da177e4SLinus Torvalds schedule_timeout(timeout); 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds remove_wait_queue(&hdev->req_wait_q, &wait); 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds if (signal_pending(current)) 3401da177e4SLinus Torvalds return -EINTR; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds switch (hdev->req_status) { 3431da177e4SLinus Torvalds case HCI_REQ_DONE: 344e175072fSJoe Perches err = -bt_to_errno(hdev->req_result); 3451da177e4SLinus Torvalds break; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds case HCI_REQ_CANCELED: 3481da177e4SLinus Torvalds err = -hdev->req_result; 3491da177e4SLinus Torvalds break; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds default: 3521da177e4SLinus Torvalds err = -ETIMEDOUT; 3531da177e4SLinus Torvalds break; 3543ff50b79SStephen Hemminger } 3551da177e4SLinus Torvalds 356a5040efaSJohan Hedberg hdev->req_status = hdev->req_result = 0; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds BT_DBG("%s end: err %d", hdev->name, err); 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds return err; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 36301178cd4SJohan Hedberg static int hci_req_sync(struct hci_dev *hdev, 36442c6b129SJohan Hedberg void (*req)(struct hci_request *req, 36542c6b129SJohan Hedberg unsigned long opt), 3661da177e4SLinus Torvalds unsigned long opt, __u32 timeout) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds int ret; 3691da177e4SLinus Torvalds 3707c6a329eSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) 3717c6a329eSMarcel Holtmann return -ENETDOWN; 3727c6a329eSMarcel Holtmann 3731da177e4SLinus Torvalds /* Serialize all requests */ 3741da177e4SLinus Torvalds hci_req_lock(hdev); 37501178cd4SJohan Hedberg ret = __hci_req_sync(hdev, req, opt, timeout); 3761da177e4SLinus Torvalds hci_req_unlock(hdev); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds return ret; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 38142c6b129SJohan Hedberg static void hci_reset_req(struct hci_request *req, unsigned long opt) 3821da177e4SLinus Torvalds { 38342c6b129SJohan Hedberg BT_DBG("%s %ld", req->hdev->name, opt); 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* Reset device */ 38642c6b129SJohan Hedberg set_bit(HCI_RESET, &req->hdev->flags); 38742c6b129SJohan Hedberg hci_req_add(req, HCI_OP_RESET, 0, NULL); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 39042c6b129SJohan Hedberg static void bredr_init(struct hci_request *req) 3911da177e4SLinus Torvalds { 39242c6b129SJohan Hedberg req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; 3932455a3eaSAndrei Emeltchenko 3941da177e4SLinus Torvalds /* Read Local Supported Features */ 39542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); 3961da177e4SLinus Torvalds 3971143e5a6SMarcel Holtmann /* Read Local Version */ 39842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 3992177bab5SJohan Hedberg 4002177bab5SJohan Hedberg /* Read BD Address */ 40142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4040af801b9SJohan Hedberg static void amp_init1(struct hci_request *req) 405e61ef499SAndrei Emeltchenko { 40642c6b129SJohan Hedberg req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; 4072455a3eaSAndrei Emeltchenko 408e61ef499SAndrei Emeltchenko /* Read Local Version */ 40942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 4106bcbc489SAndrei Emeltchenko 411f6996cfeSMarcel Holtmann /* Read Local Supported Commands */ 412f6996cfeSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 413f6996cfeSMarcel Holtmann 4146bcbc489SAndrei Emeltchenko /* Read Local AMP Info */ 41542c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); 416e71dfabaSAndrei Emeltchenko 417e71dfabaSAndrei Emeltchenko /* Read Data Blk size */ 41842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); 4197528ca1cSMarcel Holtmann 420f38ba941SMarcel Holtmann /* Read Flow Control Mode */ 421f38ba941SMarcel Holtmann hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL); 422f38ba941SMarcel Holtmann 4237528ca1cSMarcel Holtmann /* Read Location Data */ 4247528ca1cSMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); 425e61ef499SAndrei Emeltchenko } 426e61ef499SAndrei Emeltchenko 4270af801b9SJohan Hedberg static void amp_init2(struct hci_request *req) 4280af801b9SJohan Hedberg { 4290af801b9SJohan Hedberg /* Read Local Supported Features. Not all AMP controllers 4300af801b9SJohan Hedberg * support this so it's placed conditionally in the second 4310af801b9SJohan Hedberg * stage init. 4320af801b9SJohan Hedberg */ 4330af801b9SJohan Hedberg if (req->hdev->commands[14] & 0x20) 4340af801b9SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL); 4350af801b9SJohan Hedberg } 4360af801b9SJohan Hedberg 43742c6b129SJohan Hedberg static void hci_init1_req(struct hci_request *req, unsigned long opt) 438e61ef499SAndrei Emeltchenko { 43942c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 440e61ef499SAndrei Emeltchenko 441e61ef499SAndrei Emeltchenko BT_DBG("%s %ld", hdev->name, opt); 442e61ef499SAndrei Emeltchenko 44311778716SAndrei Emeltchenko /* Reset */ 44411778716SAndrei Emeltchenko if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) 44542c6b129SJohan Hedberg hci_reset_req(req, 0); 44611778716SAndrei Emeltchenko 447e61ef499SAndrei Emeltchenko switch (hdev->dev_type) { 448e61ef499SAndrei Emeltchenko case HCI_BREDR: 44942c6b129SJohan Hedberg bredr_init(req); 450e61ef499SAndrei Emeltchenko break; 451e61ef499SAndrei Emeltchenko 452e61ef499SAndrei Emeltchenko case HCI_AMP: 4530af801b9SJohan Hedberg amp_init1(req); 454e61ef499SAndrei Emeltchenko break; 455e61ef499SAndrei Emeltchenko 456e61ef499SAndrei Emeltchenko default: 457e61ef499SAndrei Emeltchenko BT_ERR("Unknown device type %d", hdev->dev_type); 458e61ef499SAndrei Emeltchenko break; 459e61ef499SAndrei Emeltchenko } 460e61ef499SAndrei Emeltchenko } 461e61ef499SAndrei Emeltchenko 46242c6b129SJohan Hedberg static void bredr_setup(struct hci_request *req) 4632177bab5SJohan Hedberg { 4642177bab5SJohan Hedberg __le16 param; 4652177bab5SJohan Hedberg __u8 flt_type; 4662177bab5SJohan Hedberg 4672177bab5SJohan Hedberg /* Read Buffer Size (ACL mtu, max pkt, etc.) */ 46842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL); 4692177bab5SJohan Hedberg 4702177bab5SJohan Hedberg /* Read Class of Device */ 47142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL); 4722177bab5SJohan Hedberg 4732177bab5SJohan Hedberg /* Read Local Name */ 47442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL); 4752177bab5SJohan Hedberg 4762177bab5SJohan Hedberg /* Read Voice Setting */ 47742c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); 4782177bab5SJohan Hedberg 479b4cb9fb2SMarcel Holtmann /* Read Number of Supported IAC */ 480b4cb9fb2SMarcel Holtmann hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL); 481b4cb9fb2SMarcel Holtmann 4824b836f39SMarcel Holtmann /* Read Current IAC LAP */ 4834b836f39SMarcel Holtmann hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL); 4844b836f39SMarcel Holtmann 4852177bab5SJohan Hedberg /* Clear Event Filters */ 4862177bab5SJohan Hedberg flt_type = HCI_FLT_CLEAR_ALL; 48742c6b129SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); 4882177bab5SJohan Hedberg 4892177bab5SJohan Hedberg /* Connection accept timeout ~20 secs */ 490dcf4adbfSJoe Perches param = cpu_to_le16(0x7d00); 49142c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m); 4922177bab5SJohan Hedberg } 4932177bab5SJohan Hedberg 49442c6b129SJohan Hedberg static void le_setup(struct hci_request *req) 4952177bab5SJohan Hedberg { 496c73eee91SJohan Hedberg struct hci_dev *hdev = req->hdev; 497c73eee91SJohan Hedberg 4982177bab5SJohan Hedberg /* Read LE Buffer Size */ 49942c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL); 5002177bab5SJohan Hedberg 5012177bab5SJohan Hedberg /* Read LE Local Supported Features */ 50242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL); 5032177bab5SJohan Hedberg 504747d3f03SMarcel Holtmann /* Read LE Supported States */ 505747d3f03SMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL); 506747d3f03SMarcel Holtmann 5072177bab5SJohan Hedberg /* Read LE White List Size */ 50842c6b129SJohan Hedberg hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL); 5092177bab5SJohan Hedberg 510747d3f03SMarcel Holtmann /* Clear LE White List */ 511747d3f03SMarcel Holtmann hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL); 512c73eee91SJohan Hedberg 513c73eee91SJohan Hedberg /* LE-only controllers have LE implicitly enabled */ 514c73eee91SJohan Hedberg if (!lmp_bredr_capable(hdev)) 515a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_LE_ENABLED); 5162177bab5SJohan Hedberg } 5172177bab5SJohan Hedberg 51842c6b129SJohan Hedberg static void hci_setup_event_mask(struct hci_request *req) 5192177bab5SJohan Hedberg { 52042c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 52142c6b129SJohan Hedberg 5222177bab5SJohan Hedberg /* The second byte is 0xff instead of 0x9f (two reserved bits 5232177bab5SJohan Hedberg * disabled) since a Broadcom 1.2 dongle doesn't respond to the 5242177bab5SJohan Hedberg * command otherwise. 5252177bab5SJohan Hedberg */ 5262177bab5SJohan Hedberg u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; 5272177bab5SJohan Hedberg 5282177bab5SJohan Hedberg /* CSR 1.1 dongles does not accept any bitfield so don't try to set 5292177bab5SJohan Hedberg * any event mask for pre 1.2 devices. 5302177bab5SJohan Hedberg */ 5312177bab5SJohan Hedberg if (hdev->hci_ver < BLUETOOTH_VER_1_2) 5322177bab5SJohan Hedberg return; 5332177bab5SJohan Hedberg 5342177bab5SJohan Hedberg if (lmp_bredr_capable(hdev)) { 5352177bab5SJohan Hedberg events[4] |= 0x01; /* Flow Specification Complete */ 5362177bab5SJohan Hedberg events[4] |= 0x02; /* Inquiry Result with RSSI */ 5372177bab5SJohan Hedberg events[4] |= 0x04; /* Read Remote Extended Features Complete */ 5382177bab5SJohan Hedberg events[5] |= 0x08; /* Synchronous Connection Complete */ 5392177bab5SJohan Hedberg events[5] |= 0x10; /* Synchronous Connection Changed */ 540c7882cbdSMarcel Holtmann } else { 541c7882cbdSMarcel Holtmann /* Use a different default for LE-only devices */ 542c7882cbdSMarcel Holtmann memset(events, 0, sizeof(events)); 543c7882cbdSMarcel Holtmann events[0] |= 0x10; /* Disconnection Complete */ 544c7882cbdSMarcel Holtmann events[1] |= 0x08; /* Read Remote Version Information Complete */ 545c7882cbdSMarcel Holtmann events[1] |= 0x20; /* Command Complete */ 546c7882cbdSMarcel Holtmann events[1] |= 0x40; /* Command Status */ 547c7882cbdSMarcel Holtmann events[1] |= 0x80; /* Hardware Error */ 548c7882cbdSMarcel Holtmann events[2] |= 0x04; /* Number of Completed Packets */ 549c7882cbdSMarcel Holtmann events[3] |= 0x02; /* Data Buffer Overflow */ 5500da71f1bSMarcel Holtmann 5510da71f1bSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_ENCRYPTION) { 5520da71f1bSMarcel Holtmann events[0] |= 0x80; /* Encryption Change */ 553c7882cbdSMarcel Holtmann events[5] |= 0x80; /* Encryption Key Refresh Complete */ 5542177bab5SJohan Hedberg } 5550da71f1bSMarcel Holtmann } 5562177bab5SJohan Hedberg 5572177bab5SJohan Hedberg if (lmp_inq_rssi_capable(hdev)) 5582177bab5SJohan Hedberg events[4] |= 0x02; /* Inquiry Result with RSSI */ 5592177bab5SJohan Hedberg 5602177bab5SJohan Hedberg if (lmp_sniffsubr_capable(hdev)) 5612177bab5SJohan Hedberg events[5] |= 0x20; /* Sniff Subrating */ 5622177bab5SJohan Hedberg 5632177bab5SJohan Hedberg if (lmp_pause_enc_capable(hdev)) 5642177bab5SJohan Hedberg events[5] |= 0x80; /* Encryption Key Refresh Complete */ 5652177bab5SJohan Hedberg 5662177bab5SJohan Hedberg if (lmp_ext_inq_capable(hdev)) 5672177bab5SJohan Hedberg events[5] |= 0x40; /* Extended Inquiry Result */ 5682177bab5SJohan Hedberg 5692177bab5SJohan Hedberg if (lmp_no_flush_capable(hdev)) 5702177bab5SJohan Hedberg events[7] |= 0x01; /* Enhanced Flush Complete */ 5712177bab5SJohan Hedberg 5722177bab5SJohan Hedberg if (lmp_lsto_capable(hdev)) 5732177bab5SJohan Hedberg events[6] |= 0x80; /* Link Supervision Timeout Changed */ 5742177bab5SJohan Hedberg 5752177bab5SJohan Hedberg if (lmp_ssp_capable(hdev)) { 5762177bab5SJohan Hedberg events[6] |= 0x01; /* IO Capability Request */ 5772177bab5SJohan Hedberg events[6] |= 0x02; /* IO Capability Response */ 5782177bab5SJohan Hedberg events[6] |= 0x04; /* User Confirmation Request */ 5792177bab5SJohan Hedberg events[6] |= 0x08; /* User Passkey Request */ 5802177bab5SJohan Hedberg events[6] |= 0x10; /* Remote OOB Data Request */ 5812177bab5SJohan Hedberg events[6] |= 0x20; /* Simple Pairing Complete */ 5822177bab5SJohan Hedberg events[7] |= 0x04; /* User Passkey Notification */ 5832177bab5SJohan Hedberg events[7] |= 0x08; /* Keypress Notification */ 5842177bab5SJohan Hedberg events[7] |= 0x10; /* Remote Host Supported 5852177bab5SJohan Hedberg * Features Notification 5862177bab5SJohan Hedberg */ 5872177bab5SJohan Hedberg } 5882177bab5SJohan Hedberg 5892177bab5SJohan Hedberg if (lmp_le_capable(hdev)) 5902177bab5SJohan Hedberg events[7] |= 0x20; /* LE Meta-Event */ 5912177bab5SJohan Hedberg 59242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events); 5932177bab5SJohan Hedberg } 5942177bab5SJohan Hedberg 59542c6b129SJohan Hedberg static void hci_init2_req(struct hci_request *req, unsigned long opt) 5962177bab5SJohan Hedberg { 59742c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 59842c6b129SJohan Hedberg 5990af801b9SJohan Hedberg if (hdev->dev_type == HCI_AMP) 6000af801b9SJohan Hedberg return amp_init2(req); 6010af801b9SJohan Hedberg 6022177bab5SJohan Hedberg if (lmp_bredr_capable(hdev)) 60342c6b129SJohan Hedberg bredr_setup(req); 60456f87901SJohan Hedberg else 605a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); 6062177bab5SJohan Hedberg 6072177bab5SJohan Hedberg if (lmp_le_capable(hdev)) 60842c6b129SJohan Hedberg le_setup(req); 6092177bab5SJohan Hedberg 6100f3adeaeSMarcel Holtmann /* All Bluetooth 1.2 and later controllers should support the 6110f3adeaeSMarcel Holtmann * HCI command for reading the local supported commands. 6120f3adeaeSMarcel Holtmann * 6130f3adeaeSMarcel Holtmann * Unfortunately some controllers indicate Bluetooth 1.2 support, 6140f3adeaeSMarcel Holtmann * but do not have support for this command. If that is the case, 6150f3adeaeSMarcel Holtmann * the driver can quirk the behavior and skip reading the local 6160f3adeaeSMarcel Holtmann * supported commands. 6173f8e2d75SJohan Hedberg */ 6180f3adeaeSMarcel Holtmann if (hdev->hci_ver > BLUETOOTH_VER_1_1 && 6190f3adeaeSMarcel Holtmann !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) 62042c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 6212177bab5SJohan Hedberg 6222177bab5SJohan Hedberg if (lmp_ssp_capable(hdev)) { 62357af75a8SMarcel Holtmann /* When SSP is available, then the host features page 62457af75a8SMarcel Holtmann * should also be available as well. However some 62557af75a8SMarcel Holtmann * controllers list the max_page as 0 as long as SSP 62657af75a8SMarcel Holtmann * has not been enabled. To achieve proper debugging 62757af75a8SMarcel Holtmann * output, force the minimum max_page to 1 at least. 62857af75a8SMarcel Holtmann */ 62957af75a8SMarcel Holtmann hdev->max_page = 0x01; 63057af75a8SMarcel Holtmann 631d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { 6322177bab5SJohan Hedberg u8 mode = 0x01; 633574ea3c7SMarcel Holtmann 63442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_SSP_MODE, 6352177bab5SJohan Hedberg sizeof(mode), &mode); 6362177bab5SJohan Hedberg } else { 6372177bab5SJohan Hedberg struct hci_cp_write_eir cp; 6382177bab5SJohan Hedberg 6392177bab5SJohan Hedberg memset(hdev->eir, 0, sizeof(hdev->eir)); 6402177bab5SJohan Hedberg memset(&cp, 0, sizeof(cp)); 6412177bab5SJohan Hedberg 64242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); 6432177bab5SJohan Hedberg } 6442177bab5SJohan Hedberg } 6452177bab5SJohan Hedberg 646043ec9bfSMarcel Holtmann if (lmp_inq_rssi_capable(hdev) || 647043ec9bfSMarcel Holtmann test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) { 64804422da9SMarcel Holtmann u8 mode; 64904422da9SMarcel Holtmann 65004422da9SMarcel Holtmann /* If Extended Inquiry Result events are supported, then 65104422da9SMarcel Holtmann * they are clearly preferred over Inquiry Result with RSSI 65204422da9SMarcel Holtmann * events. 65304422da9SMarcel Holtmann */ 65404422da9SMarcel Holtmann mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; 65504422da9SMarcel Holtmann 65604422da9SMarcel Holtmann hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode); 65704422da9SMarcel Holtmann } 6582177bab5SJohan Hedberg 6592177bab5SJohan Hedberg if (lmp_inq_tx_pwr_capable(hdev)) 66042c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL); 6612177bab5SJohan Hedberg 6622177bab5SJohan Hedberg if (lmp_ext_feat_capable(hdev)) { 6632177bab5SJohan Hedberg struct hci_cp_read_local_ext_features cp; 6642177bab5SJohan Hedberg 6652177bab5SJohan Hedberg cp.page = 0x01; 66642c6b129SJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, 66742c6b129SJohan Hedberg sizeof(cp), &cp); 6682177bab5SJohan Hedberg } 6692177bab5SJohan Hedberg 670d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { 6712177bab5SJohan Hedberg u8 enable = 1; 67242c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), 6732177bab5SJohan Hedberg &enable); 6742177bab5SJohan Hedberg } 6752177bab5SJohan Hedberg } 6762177bab5SJohan Hedberg 67742c6b129SJohan Hedberg static void hci_setup_link_policy(struct hci_request *req) 6782177bab5SJohan Hedberg { 67942c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 6802177bab5SJohan Hedberg struct hci_cp_write_def_link_policy cp; 6812177bab5SJohan Hedberg u16 link_policy = 0; 6822177bab5SJohan Hedberg 6832177bab5SJohan Hedberg if (lmp_rswitch_capable(hdev)) 6842177bab5SJohan Hedberg link_policy |= HCI_LP_RSWITCH; 6852177bab5SJohan Hedberg if (lmp_hold_capable(hdev)) 6862177bab5SJohan Hedberg link_policy |= HCI_LP_HOLD; 6872177bab5SJohan Hedberg if (lmp_sniff_capable(hdev)) 6882177bab5SJohan Hedberg link_policy |= HCI_LP_SNIFF; 6892177bab5SJohan Hedberg if (lmp_park_capable(hdev)) 6902177bab5SJohan Hedberg link_policy |= HCI_LP_PARK; 6912177bab5SJohan Hedberg 6922177bab5SJohan Hedberg cp.policy = cpu_to_le16(link_policy); 69342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp); 6942177bab5SJohan Hedberg } 6952177bab5SJohan Hedberg 69642c6b129SJohan Hedberg static void hci_set_le_support(struct hci_request *req) 6972177bab5SJohan Hedberg { 69842c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 6992177bab5SJohan Hedberg struct hci_cp_write_le_host_supported cp; 7002177bab5SJohan Hedberg 701c73eee91SJohan Hedberg /* LE-only devices do not support explicit enablement */ 702c73eee91SJohan Hedberg if (!lmp_bredr_capable(hdev)) 703c73eee91SJohan Hedberg return; 704c73eee91SJohan Hedberg 7052177bab5SJohan Hedberg memset(&cp, 0, sizeof(cp)); 7062177bab5SJohan Hedberg 707d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 7082177bab5SJohan Hedberg cp.le = 0x01; 70932226e4fSMarcel Holtmann cp.simul = 0x00; 7102177bab5SJohan Hedberg } 7112177bab5SJohan Hedberg 7122177bab5SJohan Hedberg if (cp.le != lmp_host_le_capable(hdev)) 71342c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp), 7142177bab5SJohan Hedberg &cp); 7152177bab5SJohan Hedberg } 7162177bab5SJohan Hedberg 717d62e6d67SJohan Hedberg static void hci_set_event_mask_page_2(struct hci_request *req) 718d62e6d67SJohan Hedberg { 719d62e6d67SJohan Hedberg struct hci_dev *hdev = req->hdev; 720d62e6d67SJohan Hedberg u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 721d62e6d67SJohan Hedberg 722d62e6d67SJohan Hedberg /* If Connectionless Slave Broadcast master role is supported 723d62e6d67SJohan Hedberg * enable all necessary events for it. 724d62e6d67SJohan Hedberg */ 72553b834d2SMarcel Holtmann if (lmp_csb_master_capable(hdev)) { 726d62e6d67SJohan Hedberg events[1] |= 0x40; /* Triggered Clock Capture */ 727d62e6d67SJohan Hedberg events[1] |= 0x80; /* Synchronization Train Complete */ 728d62e6d67SJohan Hedberg events[2] |= 0x10; /* Slave Page Response Timeout */ 729d62e6d67SJohan Hedberg events[2] |= 0x20; /* CSB Channel Map Change */ 730d62e6d67SJohan Hedberg } 731d62e6d67SJohan Hedberg 732d62e6d67SJohan Hedberg /* If Connectionless Slave Broadcast slave role is supported 733d62e6d67SJohan Hedberg * enable all necessary events for it. 734d62e6d67SJohan Hedberg */ 73553b834d2SMarcel Holtmann if (lmp_csb_slave_capable(hdev)) { 736d62e6d67SJohan Hedberg events[2] |= 0x01; /* Synchronization Train Received */ 737d62e6d67SJohan Hedberg events[2] |= 0x02; /* CSB Receive */ 738d62e6d67SJohan Hedberg events[2] |= 0x04; /* CSB Timeout */ 739d62e6d67SJohan Hedberg events[2] |= 0x08; /* Truncated Page Complete */ 740d62e6d67SJohan Hedberg } 741d62e6d67SJohan Hedberg 74240c59fcbSMarcel Holtmann /* Enable Authenticated Payload Timeout Expired event if supported */ 743cd7ca0ecSMarcel Holtmann if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) 74440c59fcbSMarcel Holtmann events[2] |= 0x80; 74540c59fcbSMarcel Holtmann 746d62e6d67SJohan Hedberg hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events); 747d62e6d67SJohan Hedberg } 748d62e6d67SJohan Hedberg 74942c6b129SJohan Hedberg static void hci_init3_req(struct hci_request *req, unsigned long opt) 7502177bab5SJohan Hedberg { 75142c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 752d2c5d77fSJohan Hedberg u8 p; 75342c6b129SJohan Hedberg 7540da71f1bSMarcel Holtmann hci_setup_event_mask(req); 7550da71f1bSMarcel Holtmann 756e81be90bSJohan Hedberg if (hdev->commands[6] & 0x20 && 757e81be90bSJohan Hedberg !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { 75848ce62c4SMarcel Holtmann struct hci_cp_read_stored_link_key cp; 75948ce62c4SMarcel Holtmann 76048ce62c4SMarcel Holtmann bacpy(&cp.bdaddr, BDADDR_ANY); 76148ce62c4SMarcel Holtmann cp.read_all = 0x01; 76248ce62c4SMarcel Holtmann hci_req_add(req, HCI_OP_READ_STORED_LINK_KEY, sizeof(cp), &cp); 76348ce62c4SMarcel Holtmann } 76448ce62c4SMarcel Holtmann 7652177bab5SJohan Hedberg if (hdev->commands[5] & 0x10) 76642c6b129SJohan Hedberg hci_setup_link_policy(req); 7672177bab5SJohan Hedberg 768417287deSMarcel Holtmann if (hdev->commands[8] & 0x01) 769417287deSMarcel Holtmann hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); 770417287deSMarcel Holtmann 771417287deSMarcel Holtmann /* Some older Broadcom based Bluetooth 1.2 controllers do not 772417287deSMarcel Holtmann * support the Read Page Scan Type command. Check support for 773417287deSMarcel Holtmann * this command in the bit mask of supported commands. 774417287deSMarcel Holtmann */ 775417287deSMarcel Holtmann if (hdev->commands[13] & 0x01) 776417287deSMarcel Holtmann hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); 777417287deSMarcel Holtmann 7789193c6e8SAndre Guedes if (lmp_le_capable(hdev)) { 7799193c6e8SAndre Guedes u8 events[8]; 7809193c6e8SAndre Guedes 7819193c6e8SAndre Guedes memset(events, 0, sizeof(events)); 7824d6c705bSMarcel Holtmann events[0] = 0x0f; 7834d6c705bSMarcel Holtmann 7844d6c705bSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_ENCRYPTION) 7854d6c705bSMarcel Holtmann events[0] |= 0x10; /* LE Long Term Key Request */ 786662bc2e6SAndre Guedes 787662bc2e6SAndre Guedes /* If controller supports the Connection Parameters Request 788662bc2e6SAndre Guedes * Link Layer Procedure, enable the corresponding event. 789662bc2e6SAndre Guedes */ 790662bc2e6SAndre Guedes if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) 791662bc2e6SAndre Guedes events[0] |= 0x20; /* LE Remote Connection 792662bc2e6SAndre Guedes * Parameter Request 793662bc2e6SAndre Guedes */ 794662bc2e6SAndre Guedes 795a9f6068eSMarcel Holtmann /* If the controller supports the Data Length Extension 796a9f6068eSMarcel Holtmann * feature, enable the corresponding event. 797a9f6068eSMarcel Holtmann */ 798a9f6068eSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) 799a9f6068eSMarcel Holtmann events[0] |= 0x40; /* LE Data Length Change */ 800a9f6068eSMarcel Holtmann 8014b71bba4SMarcel Holtmann /* If the controller supports Extended Scanner Filter 8024b71bba4SMarcel Holtmann * Policies, enable the correspondig event. 8034b71bba4SMarcel Holtmann */ 8044b71bba4SMarcel Holtmann if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY) 8054b71bba4SMarcel Holtmann events[1] |= 0x04; /* LE Direct Advertising 8064b71bba4SMarcel Holtmann * Report 8074b71bba4SMarcel Holtmann */ 8084b71bba4SMarcel Holtmann 8095a34bd5fSMarcel Holtmann /* If the controller supports the LE Read Local P-256 8105a34bd5fSMarcel Holtmann * Public Key command, enable the corresponding event. 8115a34bd5fSMarcel Holtmann */ 8125a34bd5fSMarcel Holtmann if (hdev->commands[34] & 0x02) 8135a34bd5fSMarcel Holtmann events[0] |= 0x80; /* LE Read Local P-256 8145a34bd5fSMarcel Holtmann * Public Key Complete 8155a34bd5fSMarcel Holtmann */ 8165a34bd5fSMarcel Holtmann 8175a34bd5fSMarcel Holtmann /* If the controller supports the LE Generate DHKey 8185a34bd5fSMarcel Holtmann * command, enable the corresponding event. 8195a34bd5fSMarcel Holtmann */ 8205a34bd5fSMarcel Holtmann if (hdev->commands[34] & 0x04) 8215a34bd5fSMarcel Holtmann events[1] |= 0x01; /* LE Generate DHKey Complete */ 8225a34bd5fSMarcel Holtmann 8239193c6e8SAndre Guedes hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events), 8249193c6e8SAndre Guedes events); 8259193c6e8SAndre Guedes 82615a49ccaSMarcel Holtmann if (hdev->commands[25] & 0x40) { 82715a49ccaSMarcel Holtmann /* Read LE Advertising Channel TX Power */ 82815a49ccaSMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL); 82915a49ccaSMarcel Holtmann } 83015a49ccaSMarcel Holtmann 831a9f6068eSMarcel Holtmann if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) { 832a9f6068eSMarcel Holtmann /* Read LE Maximum Data Length */ 833a9f6068eSMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL); 834a9f6068eSMarcel Holtmann 835a9f6068eSMarcel Holtmann /* Read LE Suggested Default Data Length */ 836a9f6068eSMarcel Holtmann hci_req_add(req, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL); 837a9f6068eSMarcel Holtmann } 838a9f6068eSMarcel Holtmann 83942c6b129SJohan Hedberg hci_set_le_support(req); 8409193c6e8SAndre Guedes } 841d2c5d77fSJohan Hedberg 842d2c5d77fSJohan Hedberg /* Read features beyond page 1 if available */ 843d2c5d77fSJohan Hedberg for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) { 844d2c5d77fSJohan Hedberg struct hci_cp_read_local_ext_features cp; 845d2c5d77fSJohan Hedberg 846d2c5d77fSJohan Hedberg cp.page = p; 847d2c5d77fSJohan Hedberg hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES, 848d2c5d77fSJohan Hedberg sizeof(cp), &cp); 849d2c5d77fSJohan Hedberg } 8502177bab5SJohan Hedberg } 8512177bab5SJohan Hedberg 8525d4e7e8dSJohan Hedberg static void hci_init4_req(struct hci_request *req, unsigned long opt) 8535d4e7e8dSJohan Hedberg { 8545d4e7e8dSJohan Hedberg struct hci_dev *hdev = req->hdev; 8555d4e7e8dSJohan Hedberg 85636f260ceSMarcel Holtmann /* Some Broadcom based Bluetooth controllers do not support the 85736f260ceSMarcel Holtmann * Delete Stored Link Key command. They are clearly indicating its 85836f260ceSMarcel Holtmann * absence in the bit mask of supported commands. 85936f260ceSMarcel Holtmann * 86036f260ceSMarcel Holtmann * Check the supported commands and only if the the command is marked 86136f260ceSMarcel Holtmann * as supported send it. If not supported assume that the controller 86236f260ceSMarcel Holtmann * does not have actual support for stored link keys which makes this 86336f260ceSMarcel Holtmann * command redundant anyway. 86436f260ceSMarcel Holtmann * 86536f260ceSMarcel Holtmann * Some controllers indicate that they support handling deleting 86636f260ceSMarcel Holtmann * stored link keys, but they don't. The quirk lets a driver 86736f260ceSMarcel Holtmann * just disable this command. 86836f260ceSMarcel Holtmann */ 86936f260ceSMarcel Holtmann if (hdev->commands[6] & 0x80 && 87036f260ceSMarcel Holtmann !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) { 87136f260ceSMarcel Holtmann struct hci_cp_delete_stored_link_key cp; 87236f260ceSMarcel Holtmann 87336f260ceSMarcel Holtmann bacpy(&cp.bdaddr, BDADDR_ANY); 87436f260ceSMarcel Holtmann cp.delete_all = 0x01; 87536f260ceSMarcel Holtmann hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY, 87636f260ceSMarcel Holtmann sizeof(cp), &cp); 87736f260ceSMarcel Holtmann } 87836f260ceSMarcel Holtmann 879d62e6d67SJohan Hedberg /* Set event mask page 2 if the HCI command for it is supported */ 880d62e6d67SJohan Hedberg if (hdev->commands[22] & 0x04) 881d62e6d67SJohan Hedberg hci_set_event_mask_page_2(req); 882d62e6d67SJohan Hedberg 883109e3191SMarcel Holtmann /* Read local codec list if the HCI command is supported */ 884109e3191SMarcel Holtmann if (hdev->commands[29] & 0x20) 885109e3191SMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_CODECS, 0, NULL); 886109e3191SMarcel Holtmann 887f4fe73edSMarcel Holtmann /* Get MWS transport configuration if the HCI command is supported */ 888f4fe73edSMarcel Holtmann if (hdev->commands[30] & 0x08) 889f4fe73edSMarcel Holtmann hci_req_add(req, HCI_OP_GET_MWS_TRANSPORT_CONFIG, 0, NULL); 890f4fe73edSMarcel Holtmann 8915d4e7e8dSJohan Hedberg /* Check for Synchronization Train support */ 89253b834d2SMarcel Holtmann if (lmp_sync_train_capable(hdev)) 8935d4e7e8dSJohan Hedberg hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); 894a6d0d690SMarcel Holtmann 895a6d0d690SMarcel Holtmann /* Enable Secure Connections if supported and configured */ 896d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && 897574ea3c7SMarcel Holtmann bredr_sc_enabled(hdev)) { 898a6d0d690SMarcel Holtmann u8 support = 0x01; 899574ea3c7SMarcel Holtmann 900a6d0d690SMarcel Holtmann hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT, 901a6d0d690SMarcel Holtmann sizeof(support), &support); 902a6d0d690SMarcel Holtmann } 9035d4e7e8dSJohan Hedberg } 9045d4e7e8dSJohan Hedberg 9052177bab5SJohan Hedberg static int __hci_init(struct hci_dev *hdev) 9062177bab5SJohan Hedberg { 9072177bab5SJohan Hedberg int err; 9082177bab5SJohan Hedberg 9092177bab5SJohan Hedberg err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT); 9102177bab5SJohan Hedberg if (err < 0) 9112177bab5SJohan Hedberg return err; 9122177bab5SJohan Hedberg 913f640ee98SMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SETUP)) 914f640ee98SMarcel Holtmann hci_debugfs_create_basic(hdev); 9154b4148e9SMarcel Holtmann 9162177bab5SJohan Hedberg err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT); 9172177bab5SJohan Hedberg if (err < 0) 9182177bab5SJohan Hedberg return err; 9192177bab5SJohan Hedberg 9200af801b9SJohan Hedberg /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode 9210af801b9SJohan Hedberg * BR/EDR/LE type controllers. AMP controllers only need the 9220af801b9SJohan Hedberg * first two stages of init. 9230af801b9SJohan Hedberg */ 9240af801b9SJohan Hedberg if (hdev->dev_type != HCI_BREDR) 9250af801b9SJohan Hedberg return 0; 9260af801b9SJohan Hedberg 9275d4e7e8dSJohan Hedberg err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); 9285d4e7e8dSJohan Hedberg if (err < 0) 9295d4e7e8dSJohan Hedberg return err; 9305d4e7e8dSJohan Hedberg 931baf27f6eSMarcel Holtmann err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT); 932baf27f6eSMarcel Holtmann if (err < 0) 933baf27f6eSMarcel Holtmann return err; 934baf27f6eSMarcel Holtmann 935ec6cef9cSMarcel Holtmann /* This function is only called when the controller is actually in 936ec6cef9cSMarcel Holtmann * configured state. When the controller is marked as unconfigured, 937ec6cef9cSMarcel Holtmann * this initialization procedure is not run. 938ec6cef9cSMarcel Holtmann * 939ec6cef9cSMarcel Holtmann * It means that it is possible that a controller runs through its 940ec6cef9cSMarcel Holtmann * setup phase and then discovers missing settings. If that is the 941ec6cef9cSMarcel Holtmann * case, then this function will not be called. It then will only 942ec6cef9cSMarcel Holtmann * be called during the config phase. 943ec6cef9cSMarcel Holtmann * 944ec6cef9cSMarcel Holtmann * So only when in setup phase or config phase, create the debugfs 945ec6cef9cSMarcel Holtmann * entries and register the SMP channels. 946baf27f6eSMarcel Holtmann */ 947d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 948d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) 949baf27f6eSMarcel Holtmann return 0; 950baf27f6eSMarcel Holtmann 95160c5f5fbSMarcel Holtmann hci_debugfs_create_common(hdev); 95260c5f5fbSMarcel Holtmann 95371c3b60eSMarcel Holtmann if (lmp_bredr_capable(hdev)) 95460c5f5fbSMarcel Holtmann hci_debugfs_create_bredr(hdev); 9552bfa3531SMarcel Holtmann 956162a3bacSMarcel Holtmann if (lmp_le_capable(hdev)) 95760c5f5fbSMarcel Holtmann hci_debugfs_create_le(hdev); 958e7b8fc92SMarcel Holtmann 959baf27f6eSMarcel Holtmann return 0; 9602177bab5SJohan Hedberg } 9612177bab5SJohan Hedberg 9620ebca7d6SMarcel Holtmann static void hci_init0_req(struct hci_request *req, unsigned long opt) 9630ebca7d6SMarcel Holtmann { 9640ebca7d6SMarcel Holtmann struct hci_dev *hdev = req->hdev; 9650ebca7d6SMarcel Holtmann 9660ebca7d6SMarcel Holtmann BT_DBG("%s %ld", hdev->name, opt); 9670ebca7d6SMarcel Holtmann 9680ebca7d6SMarcel Holtmann /* Reset */ 9690ebca7d6SMarcel Holtmann if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) 9700ebca7d6SMarcel Holtmann hci_reset_req(req, 0); 9710ebca7d6SMarcel Holtmann 9720ebca7d6SMarcel Holtmann /* Read Local Version */ 9730ebca7d6SMarcel Holtmann hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 9740ebca7d6SMarcel Holtmann 9750ebca7d6SMarcel Holtmann /* Read BD Address */ 9760ebca7d6SMarcel Holtmann if (hdev->set_bdaddr) 9770ebca7d6SMarcel Holtmann hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); 9780ebca7d6SMarcel Holtmann } 9790ebca7d6SMarcel Holtmann 9800ebca7d6SMarcel Holtmann static int __hci_unconf_init(struct hci_dev *hdev) 9810ebca7d6SMarcel Holtmann { 9820ebca7d6SMarcel Holtmann int err; 9830ebca7d6SMarcel Holtmann 984cc78b44bSMarcel Holtmann if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 985cc78b44bSMarcel Holtmann return 0; 986cc78b44bSMarcel Holtmann 9870ebca7d6SMarcel Holtmann err = __hci_req_sync(hdev, hci_init0_req, 0, HCI_INIT_TIMEOUT); 9880ebca7d6SMarcel Holtmann if (err < 0) 9890ebca7d6SMarcel Holtmann return err; 9900ebca7d6SMarcel Holtmann 991f640ee98SMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SETUP)) 992f640ee98SMarcel Holtmann hci_debugfs_create_basic(hdev); 993f640ee98SMarcel Holtmann 9940ebca7d6SMarcel Holtmann return 0; 9950ebca7d6SMarcel Holtmann } 9960ebca7d6SMarcel Holtmann 99742c6b129SJohan Hedberg static void hci_scan_req(struct hci_request *req, unsigned long opt) 9981da177e4SLinus Torvalds { 9991da177e4SLinus Torvalds __u8 scan = opt; 10001da177e4SLinus Torvalds 100142c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, scan); 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* Inquiry and Page scans */ 100442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 100742c6b129SJohan Hedberg static void hci_auth_req(struct hci_request *req, unsigned long opt) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds __u8 auth = opt; 10101da177e4SLinus Torvalds 101142c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, auth); 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds /* Authentication */ 101442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth); 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 101742c6b129SJohan Hedberg static void hci_encrypt_req(struct hci_request *req, unsigned long opt) 10181da177e4SLinus Torvalds { 10191da177e4SLinus Torvalds __u8 encrypt = opt; 10201da177e4SLinus Torvalds 102142c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, encrypt); 10221da177e4SLinus Torvalds 1023e4e8e37cSMarcel Holtmann /* Encryption */ 102442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt); 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds 102742c6b129SJohan Hedberg static void hci_linkpol_req(struct hci_request *req, unsigned long opt) 1028e4e8e37cSMarcel Holtmann { 1029e4e8e37cSMarcel Holtmann __le16 policy = cpu_to_le16(opt); 1030e4e8e37cSMarcel Holtmann 103142c6b129SJohan Hedberg BT_DBG("%s %x", req->hdev->name, policy); 1032e4e8e37cSMarcel Holtmann 1033e4e8e37cSMarcel Holtmann /* Default link policy */ 103442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy); 1035e4e8e37cSMarcel Holtmann } 1036e4e8e37cSMarcel Holtmann 10371da177e4SLinus Torvalds /* Get HCI device by index. 10381da177e4SLinus Torvalds * Device is held on return. */ 10391da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index) 10401da177e4SLinus Torvalds { 10418035ded4SLuiz Augusto von Dentz struct hci_dev *hdev = NULL, *d; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds BT_DBG("%d", index); 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds if (index < 0) 10461da177e4SLinus Torvalds return NULL; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds read_lock(&hci_dev_list_lock); 10498035ded4SLuiz Augusto von Dentz list_for_each_entry(d, &hci_dev_list, list) { 10501da177e4SLinus Torvalds if (d->id == index) { 10511da177e4SLinus Torvalds hdev = hci_dev_hold(d); 10521da177e4SLinus Torvalds break; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds read_unlock(&hci_dev_list_lock); 10561da177e4SLinus Torvalds return hdev; 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* ---- Inquiry support ---- */ 1060ff9ef578SJohan Hedberg 106130dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev) 106230dc78e1SJohan Hedberg { 106330dc78e1SJohan Hedberg struct discovery_state *discov = &hdev->discovery; 106430dc78e1SJohan Hedberg 10656fbe195dSAndre Guedes switch (discov->state) { 1066343f935bSAndre Guedes case DISCOVERY_FINDING: 10676fbe195dSAndre Guedes case DISCOVERY_RESOLVING: 106830dc78e1SJohan Hedberg return true; 106930dc78e1SJohan Hedberg 10706fbe195dSAndre Guedes default: 107130dc78e1SJohan Hedberg return false; 107230dc78e1SJohan Hedberg } 10736fbe195dSAndre Guedes } 107430dc78e1SJohan Hedberg 1075ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state) 1076ff9ef578SJohan Hedberg { 1077bb3e0a33SJohan Hedberg int old_state = hdev->discovery.state; 1078bb3e0a33SJohan Hedberg 1079ff9ef578SJohan Hedberg BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state); 1080ff9ef578SJohan Hedberg 1081bb3e0a33SJohan Hedberg if (old_state == state) 1082ff9ef578SJohan Hedberg return; 1083ff9ef578SJohan Hedberg 1084bb3e0a33SJohan Hedberg hdev->discovery.state = state; 1085bb3e0a33SJohan Hedberg 1086ff9ef578SJohan Hedberg switch (state) { 1087ff9ef578SJohan Hedberg case DISCOVERY_STOPPED: 1088c54c3860SAndre Guedes hci_update_background_scan(hdev); 1089c54c3860SAndre Guedes 1090bb3e0a33SJohan Hedberg if (old_state != DISCOVERY_STARTING) 1091ff9ef578SJohan Hedberg mgmt_discovering(hdev, 0); 1092ff9ef578SJohan Hedberg break; 1093ff9ef578SJohan Hedberg case DISCOVERY_STARTING: 1094ff9ef578SJohan Hedberg break; 1095343f935bSAndre Guedes case DISCOVERY_FINDING: 1096ff9ef578SJohan Hedberg mgmt_discovering(hdev, 1); 1097ff9ef578SJohan Hedberg break; 109830dc78e1SJohan Hedberg case DISCOVERY_RESOLVING: 109930dc78e1SJohan Hedberg break; 1100ff9ef578SJohan Hedberg case DISCOVERY_STOPPING: 1101ff9ef578SJohan Hedberg break; 1102ff9ef578SJohan Hedberg } 1103ff9ef578SJohan Hedberg } 1104ff9ef578SJohan Hedberg 11051f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev) 11061da177e4SLinus Torvalds { 110730883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1108b57c1a56SJohan Hedberg struct inquiry_entry *p, *n; 11091da177e4SLinus Torvalds 1110561aafbcSJohan Hedberg list_for_each_entry_safe(p, n, &cache->all, all) { 1111561aafbcSJohan Hedberg list_del(&p->all); 1112b57c1a56SJohan Hedberg kfree(p); 11131da177e4SLinus Torvalds } 1114561aafbcSJohan Hedberg 1115561aafbcSJohan Hedberg INIT_LIST_HEAD(&cache->unknown); 1116561aafbcSJohan Hedberg INIT_LIST_HEAD(&cache->resolve); 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 1119a8c5fb1aSGustavo Padovan struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, 1120a8c5fb1aSGustavo Padovan bdaddr_t *bdaddr) 11211da177e4SLinus Torvalds { 112230883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 11231da177e4SLinus Torvalds struct inquiry_entry *e; 11241da177e4SLinus Torvalds 11256ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, bdaddr); 11261da177e4SLinus Torvalds 1127561aafbcSJohan Hedberg list_for_each_entry(e, &cache->all, all) { 11281da177e4SLinus Torvalds if (!bacmp(&e->data.bdaddr, bdaddr)) 11291da177e4SLinus Torvalds return e; 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 1132b57c1a56SJohan Hedberg return NULL; 1133b57c1a56SJohan Hedberg } 1134b57c1a56SJohan Hedberg 1135561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev, 1136561aafbcSJohan Hedberg bdaddr_t *bdaddr) 1137561aafbcSJohan Hedberg { 113830883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1139561aafbcSJohan Hedberg struct inquiry_entry *e; 1140561aafbcSJohan Hedberg 11416ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, bdaddr); 1142561aafbcSJohan Hedberg 1143561aafbcSJohan Hedberg list_for_each_entry(e, &cache->unknown, list) { 1144561aafbcSJohan Hedberg if (!bacmp(&e->data.bdaddr, bdaddr)) 1145561aafbcSJohan Hedberg return e; 1146561aafbcSJohan Hedberg } 1147561aafbcSJohan Hedberg 1148561aafbcSJohan Hedberg return NULL; 1149561aafbcSJohan Hedberg } 1150561aafbcSJohan Hedberg 115130dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev, 115230dc78e1SJohan Hedberg bdaddr_t *bdaddr, 115330dc78e1SJohan Hedberg int state) 115430dc78e1SJohan Hedberg { 115530dc78e1SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 115630dc78e1SJohan Hedberg struct inquiry_entry *e; 115730dc78e1SJohan Hedberg 11586ed93dc6SAndrei Emeltchenko BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state); 115930dc78e1SJohan Hedberg 116030dc78e1SJohan Hedberg list_for_each_entry(e, &cache->resolve, list) { 116130dc78e1SJohan Hedberg if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state) 116230dc78e1SJohan Hedberg return e; 116330dc78e1SJohan Hedberg if (!bacmp(&e->data.bdaddr, bdaddr)) 116430dc78e1SJohan Hedberg return e; 116530dc78e1SJohan Hedberg } 116630dc78e1SJohan Hedberg 116730dc78e1SJohan Hedberg return NULL; 116830dc78e1SJohan Hedberg } 116930dc78e1SJohan Hedberg 1170a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev, 1171a3d4e20aSJohan Hedberg struct inquiry_entry *ie) 1172a3d4e20aSJohan Hedberg { 1173a3d4e20aSJohan Hedberg struct discovery_state *cache = &hdev->discovery; 1174a3d4e20aSJohan Hedberg struct list_head *pos = &cache->resolve; 1175a3d4e20aSJohan Hedberg struct inquiry_entry *p; 1176a3d4e20aSJohan Hedberg 1177a3d4e20aSJohan Hedberg list_del(&ie->list); 1178a3d4e20aSJohan Hedberg 1179a3d4e20aSJohan Hedberg list_for_each_entry(p, &cache->resolve, list) { 1180a3d4e20aSJohan Hedberg if (p->name_state != NAME_PENDING && 1181a3d4e20aSJohan Hedberg abs(p->data.rssi) >= abs(ie->data.rssi)) 1182a3d4e20aSJohan Hedberg break; 1183a3d4e20aSJohan Hedberg pos = &p->list; 1184a3d4e20aSJohan Hedberg } 1185a3d4e20aSJohan Hedberg 1186a3d4e20aSJohan Hedberg list_add(&ie->list, pos); 1187a3d4e20aSJohan Hedberg } 1188a3d4e20aSJohan Hedberg 1189af58925cSMarcel Holtmann u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data, 1190af58925cSMarcel Holtmann bool name_known) 11911da177e4SLinus Torvalds { 119230883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 119370f23020SAndrei Emeltchenko struct inquiry_entry *ie; 1194af58925cSMarcel Holtmann u32 flags = 0; 11951da177e4SLinus Torvalds 11966ed93dc6SAndrei Emeltchenko BT_DBG("cache %p, %pMR", cache, &data->bdaddr); 11971da177e4SLinus Torvalds 11986928a924SJohan Hedberg hci_remove_remote_oob_data(hdev, &data->bdaddr, BDADDR_BREDR); 11992b2fec4dSSzymon Janc 1200af58925cSMarcel Holtmann if (!data->ssp_mode) 1201af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; 1202388fc8faSJohan Hedberg 120370f23020SAndrei Emeltchenko ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr); 1204a3d4e20aSJohan Hedberg if (ie) { 1205af58925cSMarcel Holtmann if (!ie->data.ssp_mode) 1206af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_LEGACY_PAIRING; 1207388fc8faSJohan Hedberg 1208a3d4e20aSJohan Hedberg if (ie->name_state == NAME_NEEDED && 1209a3d4e20aSJohan Hedberg data->rssi != ie->data.rssi) { 1210a3d4e20aSJohan Hedberg ie->data.rssi = data->rssi; 1211a3d4e20aSJohan Hedberg hci_inquiry_cache_update_resolve(hdev, ie); 1212a3d4e20aSJohan Hedberg } 1213a3d4e20aSJohan Hedberg 1214561aafbcSJohan Hedberg goto update; 1215a3d4e20aSJohan Hedberg } 1216561aafbcSJohan Hedberg 12171da177e4SLinus Torvalds /* Entry not in the cache. Add new one. */ 121827f70f3eSJohan Hedberg ie = kzalloc(sizeof(*ie), GFP_KERNEL); 1219af58925cSMarcel Holtmann if (!ie) { 1220af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_CONFIRM_NAME; 1221af58925cSMarcel Holtmann goto done; 1222af58925cSMarcel Holtmann } 122370f23020SAndrei Emeltchenko 1224561aafbcSJohan Hedberg list_add(&ie->all, &cache->all); 1225561aafbcSJohan Hedberg 1226561aafbcSJohan Hedberg if (name_known) { 1227561aafbcSJohan Hedberg ie->name_state = NAME_KNOWN; 1228561aafbcSJohan Hedberg } else { 1229561aafbcSJohan Hedberg ie->name_state = NAME_NOT_KNOWN; 1230561aafbcSJohan Hedberg list_add(&ie->list, &cache->unknown); 1231561aafbcSJohan Hedberg } 1232561aafbcSJohan Hedberg 1233561aafbcSJohan Hedberg update: 1234561aafbcSJohan Hedberg if (name_known && ie->name_state != NAME_KNOWN && 1235561aafbcSJohan Hedberg ie->name_state != NAME_PENDING) { 1236561aafbcSJohan Hedberg ie->name_state = NAME_KNOWN; 1237561aafbcSJohan Hedberg list_del(&ie->list); 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 124070f23020SAndrei Emeltchenko memcpy(&ie->data, data, sizeof(*data)); 124170f23020SAndrei Emeltchenko ie->timestamp = jiffies; 12421da177e4SLinus Torvalds cache->timestamp = jiffies; 12433175405bSJohan Hedberg 12443175405bSJohan Hedberg if (ie->name_state == NAME_NOT_KNOWN) 1245af58925cSMarcel Holtmann flags |= MGMT_DEV_FOUND_CONFIRM_NAME; 12463175405bSJohan Hedberg 1247af58925cSMarcel Holtmann done: 1248af58925cSMarcel Holtmann return flags; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf) 12521da177e4SLinus Torvalds { 125330883512SJohan Hedberg struct discovery_state *cache = &hdev->discovery; 12541da177e4SLinus Torvalds struct inquiry_info *info = (struct inquiry_info *) buf; 12551da177e4SLinus Torvalds struct inquiry_entry *e; 12561da177e4SLinus Torvalds int copied = 0; 12571da177e4SLinus Torvalds 1258561aafbcSJohan Hedberg list_for_each_entry(e, &cache->all, all) { 12591da177e4SLinus Torvalds struct inquiry_data *data = &e->data; 1260b57c1a56SJohan Hedberg 1261b57c1a56SJohan Hedberg if (copied >= num) 1262b57c1a56SJohan Hedberg break; 1263b57c1a56SJohan Hedberg 12641da177e4SLinus Torvalds bacpy(&info->bdaddr, &data->bdaddr); 12651da177e4SLinus Torvalds info->pscan_rep_mode = data->pscan_rep_mode; 12661da177e4SLinus Torvalds info->pscan_period_mode = data->pscan_period_mode; 12671da177e4SLinus Torvalds info->pscan_mode = data->pscan_mode; 12681da177e4SLinus Torvalds memcpy(info->dev_class, data->dev_class, 3); 12691da177e4SLinus Torvalds info->clock_offset = data->clock_offset; 1270b57c1a56SJohan Hedberg 12711da177e4SLinus Torvalds info++; 1272b57c1a56SJohan Hedberg copied++; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds BT_DBG("cache %p, copied %d", cache, copied); 12761da177e4SLinus Torvalds return copied; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 127942c6b129SJohan Hedberg static void hci_inq_req(struct hci_request *req, unsigned long opt) 12801da177e4SLinus Torvalds { 12811da177e4SLinus Torvalds struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt; 128242c6b129SJohan Hedberg struct hci_dev *hdev = req->hdev; 12831da177e4SLinus Torvalds struct hci_cp_inquiry cp; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds if (test_bit(HCI_INQUIRY, &hdev->flags)) 12881da177e4SLinus Torvalds return; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds /* Start Inquiry */ 12911da177e4SLinus Torvalds memcpy(&cp.lap, &ir->lap, 3); 12921da177e4SLinus Torvalds cp.length = ir->length; 12931da177e4SLinus Torvalds cp.num_rsp = ir->num_rsp; 129442c6b129SJohan Hedberg hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds int hci_inquiry(void __user *arg) 12981da177e4SLinus Torvalds { 12991da177e4SLinus Torvalds __u8 __user *ptr = arg; 13001da177e4SLinus Torvalds struct hci_inquiry_req ir; 13011da177e4SLinus Torvalds struct hci_dev *hdev; 13021da177e4SLinus Torvalds int err = 0, do_inquiry = 0, max_rsp; 13031da177e4SLinus Torvalds long timeo; 13041da177e4SLinus Torvalds __u8 *buf; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds if (copy_from_user(&ir, ptr, sizeof(ir))) 13071da177e4SLinus Torvalds return -EFAULT; 13081da177e4SLinus Torvalds 13095a08ecceSAndrei Emeltchenko hdev = hci_dev_get(ir.dev_id); 13105a08ecceSAndrei Emeltchenko if (!hdev) 13111da177e4SLinus Torvalds return -ENODEV; 13121da177e4SLinus Torvalds 1313d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 13140736cfa8SMarcel Holtmann err = -EBUSY; 13150736cfa8SMarcel Holtmann goto done; 13160736cfa8SMarcel Holtmann } 13170736cfa8SMarcel Holtmann 1318d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 1319fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 1320fee746b0SMarcel Holtmann goto done; 1321fee746b0SMarcel Holtmann } 1322fee746b0SMarcel Holtmann 13235b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) { 13245b69bef5SMarcel Holtmann err = -EOPNOTSUPP; 13255b69bef5SMarcel Holtmann goto done; 13265b69bef5SMarcel Holtmann } 13275b69bef5SMarcel Holtmann 1328d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 132956f87901SJohan Hedberg err = -EOPNOTSUPP; 133056f87901SJohan Hedberg goto done; 133156f87901SJohan Hedberg } 133256f87901SJohan Hedberg 133309fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 13341da177e4SLinus Torvalds if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 1335a8c5fb1aSGustavo Padovan inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { 13361f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 13371da177e4SLinus Torvalds do_inquiry = 1; 13381da177e4SLinus Torvalds } 133909fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 13401da177e4SLinus Torvalds 134104837f64SMarcel Holtmann timeo = ir.length * msecs_to_jiffies(2000); 134270f23020SAndrei Emeltchenko 134370f23020SAndrei Emeltchenko if (do_inquiry) { 134401178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir, 134501178cd4SJohan Hedberg timeo); 134670f23020SAndrei Emeltchenko if (err < 0) 13471da177e4SLinus Torvalds goto done; 13483e13fa1eSAndre Guedes 13493e13fa1eSAndre Guedes /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is 13503e13fa1eSAndre Guedes * cleared). If it is interrupted by a signal, return -EINTR. 13513e13fa1eSAndre Guedes */ 135274316201SNeilBrown if (wait_on_bit(&hdev->flags, HCI_INQUIRY, 13533e13fa1eSAndre Guedes TASK_INTERRUPTIBLE)) 13543e13fa1eSAndre Guedes return -EINTR; 135570f23020SAndrei Emeltchenko } 13561da177e4SLinus Torvalds 13578fc9ced3SGustavo Padovan /* for unlimited number of responses we will use buffer with 13588fc9ced3SGustavo Padovan * 255 entries 13598fc9ced3SGustavo Padovan */ 13601da177e4SLinus Torvalds max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds /* cache_dump can't sleep. Therefore we allocate temp buffer and then 13631da177e4SLinus Torvalds * copy it to the user space. 13641da177e4SLinus Torvalds */ 136570f23020SAndrei Emeltchenko buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL); 136670f23020SAndrei Emeltchenko if (!buf) { 13671da177e4SLinus Torvalds err = -ENOMEM; 13681da177e4SLinus Torvalds goto done; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 137109fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 13721da177e4SLinus Torvalds ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); 137309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds BT_DBG("num_rsp %d", ir.num_rsp); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds if (!copy_to_user(ptr, &ir, sizeof(ir))) { 13781da177e4SLinus Torvalds ptr += sizeof(ir); 13791da177e4SLinus Torvalds if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) * 13801da177e4SLinus Torvalds ir.num_rsp)) 13811da177e4SLinus Torvalds err = -EFAULT; 13821da177e4SLinus Torvalds } else 13831da177e4SLinus Torvalds err = -EFAULT; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds kfree(buf); 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds done: 13881da177e4SLinus Torvalds hci_dev_put(hdev); 13891da177e4SLinus Torvalds return err; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 1392cbed0ca1SJohan Hedberg static int hci_dev_do_open(struct hci_dev *hdev) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds int ret = 0; 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds BT_DBG("%s %p", hdev->name, hdev); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds hci_req_lock(hdev); 13991da177e4SLinus Torvalds 1400d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 140194324962SJohan Hovold ret = -ENODEV; 140294324962SJohan Hovold goto done; 140394324962SJohan Hovold } 140494324962SJohan Hovold 1405d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 1406d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) { 1407a5c8f270SMarcel Holtmann /* Check for rfkill but allow the HCI setup stage to 1408a5c8f270SMarcel Holtmann * proceed (which in itself doesn't cause any RF activity). 1409bf543036SJohan Hedberg */ 1410d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_RFKILLED)) { 1411611b30f7SMarcel Holtmann ret = -ERFKILL; 1412611b30f7SMarcel Holtmann goto done; 1413611b30f7SMarcel Holtmann } 1414611b30f7SMarcel Holtmann 1415a5c8f270SMarcel Holtmann /* Check for valid public address or a configured static 1416a5c8f270SMarcel Holtmann * random adddress, but let the HCI setup proceed to 1417a5c8f270SMarcel Holtmann * be able to determine if there is a public address 1418a5c8f270SMarcel Holtmann * or not. 1419a5c8f270SMarcel Holtmann * 1420c6beca0eSMarcel Holtmann * In case of user channel usage, it is not important 1421c6beca0eSMarcel Holtmann * if a public address or static random address is 1422c6beca0eSMarcel Holtmann * available. 1423c6beca0eSMarcel Holtmann * 1424a5c8f270SMarcel Holtmann * This check is only valid for BR/EDR controllers 1425a5c8f270SMarcel Holtmann * since AMP controllers do not have an address. 1426a5c8f270SMarcel Holtmann */ 1427d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 1428c6beca0eSMarcel Holtmann hdev->dev_type == HCI_BREDR && 1429a5c8f270SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) && 1430a5c8f270SMarcel Holtmann !bacmp(&hdev->static_addr, BDADDR_ANY)) { 1431a5c8f270SMarcel Holtmann ret = -EADDRNOTAVAIL; 1432a5c8f270SMarcel Holtmann goto done; 1433a5c8f270SMarcel Holtmann } 1434a5c8f270SMarcel Holtmann } 1435a5c8f270SMarcel Holtmann 14361da177e4SLinus Torvalds if (test_bit(HCI_UP, &hdev->flags)) { 14371da177e4SLinus Torvalds ret = -EALREADY; 14381da177e4SLinus Torvalds goto done; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds if (hdev->open(hdev)) { 14421da177e4SLinus Torvalds ret = -EIO; 14431da177e4SLinus Torvalds goto done; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 1446e9ca8bf1SMarcel Holtmann set_bit(HCI_RUNNING, &hdev->flags); 14474a3f95b7SMarcel Holtmann hci_notify(hdev, HCI_DEV_OPEN); 14484a3f95b7SMarcel Holtmann 14491da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 14501da177e4SLinus Torvalds set_bit(HCI_INIT, &hdev->flags); 1451f41c70c4SMarcel Holtmann 1452d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_SETUP)) { 1453af202f84SMarcel Holtmann if (hdev->setup) 1454f41c70c4SMarcel Holtmann ret = hdev->setup(hdev); 1455f41c70c4SMarcel Holtmann 1456af202f84SMarcel Holtmann /* The transport driver can set these quirks before 1457af202f84SMarcel Holtmann * creating the HCI device or in its setup callback. 1458af202f84SMarcel Holtmann * 1459af202f84SMarcel Holtmann * In case any of them is set, the controller has to 1460af202f84SMarcel Holtmann * start up as unconfigured. 1461af202f84SMarcel Holtmann */ 1462eb1904f4SMarcel Holtmann if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || 1463eb1904f4SMarcel Holtmann test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks)) 1464a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_UNCONFIGURED); 1465f41c70c4SMarcel Holtmann 14660ebca7d6SMarcel Holtmann /* For an unconfigured controller it is required to 14670ebca7d6SMarcel Holtmann * read at least the version information provided by 14680ebca7d6SMarcel Holtmann * the Read Local Version Information command. 14690ebca7d6SMarcel Holtmann * 14700ebca7d6SMarcel Holtmann * If the set_bdaddr driver callback is provided, then 14710ebca7d6SMarcel Holtmann * also the original Bluetooth public device address 14720ebca7d6SMarcel Holtmann * will be read using the Read BD Address command. 14730ebca7d6SMarcel Holtmann */ 1474d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 14750ebca7d6SMarcel Holtmann ret = __hci_unconf_init(hdev); 147689bc22d2SMarcel Holtmann } 147789bc22d2SMarcel Holtmann 1478d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_CONFIG)) { 14799713c17bSMarcel Holtmann /* If public address change is configured, ensure that 14809713c17bSMarcel Holtmann * the address gets programmed. If the driver does not 14819713c17bSMarcel Holtmann * support changing the public address, fail the power 14829713c17bSMarcel Holtmann * on procedure. 148324c457e2SMarcel Holtmann */ 14849713c17bSMarcel Holtmann if (bacmp(&hdev->public_addr, BDADDR_ANY) && 14859713c17bSMarcel Holtmann hdev->set_bdaddr) 148624c457e2SMarcel Holtmann ret = hdev->set_bdaddr(hdev, &hdev->public_addr); 148724c457e2SMarcel Holtmann else 148824c457e2SMarcel Holtmann ret = -EADDRNOTAVAIL; 148924c457e2SMarcel Holtmann } 149024c457e2SMarcel Holtmann 1491f41c70c4SMarcel Holtmann if (!ret) { 1492d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1493d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 14942177bab5SJohan Hedberg ret = __hci_init(hdev); 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 1497f41c70c4SMarcel Holtmann clear_bit(HCI_INIT, &hdev->flags); 1498f41c70c4SMarcel Holtmann 14991da177e4SLinus Torvalds if (!ret) { 15001da177e4SLinus Torvalds hci_dev_hold(hdev); 1501a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 15021da177e4SLinus Torvalds set_bit(HCI_UP, &hdev->flags); 15031da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_UP); 1504d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 1505d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG) && 1506d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1507d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 15081514b892SMarcel Holtmann hdev->dev_type == HCI_BREDR) { 150909fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 1510744cf19eSJohan Hedberg mgmt_powered(hdev, 1); 151109fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 151256e5cb86SJohan Hedberg } 15131da177e4SLinus Torvalds } else { 15141da177e4SLinus Torvalds /* Init failed, cleanup */ 15153eff45eaSGustavo F. Padovan flush_work(&hdev->tx_work); 1516c347b765SGustavo F. Padovan flush_work(&hdev->cmd_work); 1517b78752ccSMarcel Holtmann flush_work(&hdev->rx_work); 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 15201da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds if (hdev->flush) 15231da177e4SLinus Torvalds hdev->flush(hdev); 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds if (hdev->sent_cmd) { 15261da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 15271da177e4SLinus Torvalds hdev->sent_cmd = NULL; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530e9ca8bf1SMarcel Holtmann clear_bit(HCI_RUNNING, &hdev->flags); 15314a3f95b7SMarcel Holtmann hci_notify(hdev, HCI_DEV_CLOSE); 15324a3f95b7SMarcel Holtmann 15331da177e4SLinus Torvalds hdev->close(hdev); 1534fee746b0SMarcel Holtmann hdev->flags &= BIT(HCI_RAW); 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds done: 15381da177e4SLinus Torvalds hci_req_unlock(hdev); 15391da177e4SLinus Torvalds return ret; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 1542cbed0ca1SJohan Hedberg /* ---- HCI ioctl helpers ---- */ 1543cbed0ca1SJohan Hedberg 1544cbed0ca1SJohan Hedberg int hci_dev_open(__u16 dev) 1545cbed0ca1SJohan Hedberg { 1546cbed0ca1SJohan Hedberg struct hci_dev *hdev; 1547cbed0ca1SJohan Hedberg int err; 1548cbed0ca1SJohan Hedberg 1549cbed0ca1SJohan Hedberg hdev = hci_dev_get(dev); 1550cbed0ca1SJohan Hedberg if (!hdev) 1551cbed0ca1SJohan Hedberg return -ENODEV; 1552cbed0ca1SJohan Hedberg 15534a964404SMarcel Holtmann /* Devices that are marked as unconfigured can only be powered 1554fee746b0SMarcel Holtmann * up as user channel. Trying to bring them up as normal devices 1555fee746b0SMarcel Holtmann * will result into a failure. Only user channel operation is 1556fee746b0SMarcel Holtmann * possible. 1557fee746b0SMarcel Holtmann * 1558fee746b0SMarcel Holtmann * When this function is called for a user channel, the flag 1559fee746b0SMarcel Holtmann * HCI_USER_CHANNEL will be set first before attempting to 1560fee746b0SMarcel Holtmann * open the device. 1561fee746b0SMarcel Holtmann */ 1562d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && 1563d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 1564fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 1565fee746b0SMarcel Holtmann goto done; 1566fee746b0SMarcel Holtmann } 1567fee746b0SMarcel Holtmann 1568e1d08f40SJohan Hedberg /* We need to ensure that no other power on/off work is pending 1569e1d08f40SJohan Hedberg * before proceeding to call hci_dev_do_open. This is 1570e1d08f40SJohan Hedberg * particularly important if the setup procedure has not yet 1571e1d08f40SJohan Hedberg * completed. 1572e1d08f40SJohan Hedberg */ 1573a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) 1574e1d08f40SJohan Hedberg cancel_delayed_work(&hdev->power_off); 1575e1d08f40SJohan Hedberg 1576a5c8f270SMarcel Holtmann /* After this call it is guaranteed that the setup procedure 1577a5c8f270SMarcel Holtmann * has finished. This means that error conditions like RFKILL 1578a5c8f270SMarcel Holtmann * or no valid public or static random address apply. 1579a5c8f270SMarcel Holtmann */ 1580e1d08f40SJohan Hedberg flush_workqueue(hdev->req_workqueue); 1581e1d08f40SJohan Hedberg 158212aa4f0aSMarcel Holtmann /* For controllers not using the management interface and that 1583b6ae8457SJohan Hedberg * are brought up using legacy ioctl, set the HCI_BONDABLE bit 158412aa4f0aSMarcel Holtmann * so that pairing works for them. Once the management interface 158512aa4f0aSMarcel Holtmann * is in use this bit will be cleared again and userspace has 158612aa4f0aSMarcel Holtmann * to explicitly enable it. 158712aa4f0aSMarcel Holtmann */ 1588d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 1589d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_MGMT)) 1590a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_BONDABLE); 159112aa4f0aSMarcel Holtmann 1592cbed0ca1SJohan Hedberg err = hci_dev_do_open(hdev); 1593cbed0ca1SJohan Hedberg 1594fee746b0SMarcel Holtmann done: 1595cbed0ca1SJohan Hedberg hci_dev_put(hdev); 1596cbed0ca1SJohan Hedberg return err; 1597cbed0ca1SJohan Hedberg } 1598cbed0ca1SJohan Hedberg 1599d7347f3cSJohan Hedberg /* This function requires the caller holds hdev->lock */ 1600d7347f3cSJohan Hedberg static void hci_pend_le_actions_clear(struct hci_dev *hdev) 1601d7347f3cSJohan Hedberg { 1602d7347f3cSJohan Hedberg struct hci_conn_params *p; 1603d7347f3cSJohan Hedberg 1604f161dd41SJohan Hedberg list_for_each_entry(p, &hdev->le_conn_params, list) { 1605f161dd41SJohan Hedberg if (p->conn) { 1606f161dd41SJohan Hedberg hci_conn_drop(p->conn); 1607f8aaf9b6SJohan Hedberg hci_conn_put(p->conn); 1608f161dd41SJohan Hedberg p->conn = NULL; 1609f161dd41SJohan Hedberg } 1610d7347f3cSJohan Hedberg list_del_init(&p->action); 1611f161dd41SJohan Hedberg } 1612d7347f3cSJohan Hedberg 1613d7347f3cSJohan Hedberg BT_DBG("All LE pending actions cleared"); 1614d7347f3cSJohan Hedberg } 1615d7347f3cSJohan Hedberg 16166b3cc1dbSSimon Fels int hci_dev_do_close(struct hci_dev *hdev) 16171da177e4SLinus Torvalds { 1618acc649c6SMarcel Holtmann bool auto_off; 1619acc649c6SMarcel Holtmann 16201da177e4SLinus Torvalds BT_DBG("%s %p", hdev->name, hdev); 16211da177e4SLinus Torvalds 1622d24d8144SGabriele Mazzotta if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && 1623867146a0SLoic Poulain !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && 1624d24d8144SGabriele Mazzotta test_bit(HCI_UP, &hdev->flags)) { 1625a44fecbdSTedd Ho-Jeong An /* Execute vendor specific shutdown routine */ 1626a44fecbdSTedd Ho-Jeong An if (hdev->shutdown) 1627a44fecbdSTedd Ho-Jeong An hdev->shutdown(hdev); 1628a44fecbdSTedd Ho-Jeong An } 1629a44fecbdSTedd Ho-Jeong An 163078c04c0bSVinicius Costa Gomes cancel_delayed_work(&hdev->power_off); 163178c04c0bSVinicius Costa Gomes 16321da177e4SLinus Torvalds hci_req_cancel(hdev, ENODEV); 16331da177e4SLinus Torvalds hci_req_lock(hdev); 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { 163665cc2b49SMarcel Holtmann cancel_delayed_work_sync(&hdev->cmd_timer); 16371da177e4SLinus Torvalds hci_req_unlock(hdev); 16381da177e4SLinus Torvalds return 0; 16391da177e4SLinus Torvalds } 16401da177e4SLinus Torvalds 16413eff45eaSGustavo F. Padovan /* Flush RX and TX works */ 16423eff45eaSGustavo F. Padovan flush_work(&hdev->tx_work); 1643b78752ccSMarcel Holtmann flush_work(&hdev->rx_work); 16441da177e4SLinus Torvalds 164516ab91abSJohan Hedberg if (hdev->discov_timeout > 0) { 1646e0f9309fSJohan Hedberg cancel_delayed_work(&hdev->discov_off); 164716ab91abSJohan Hedberg hdev->discov_timeout = 0; 1648a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 1649a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 165016ab91abSJohan Hedberg } 165116ab91abSJohan Hedberg 1652a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) 16537d78525dSJohan Hedberg cancel_delayed_work(&hdev->service_cache); 16547d78525dSJohan Hedberg 16557ba8b4beSAndre Guedes cancel_delayed_work_sync(&hdev->le_scan_disable); 16562d28cfe7SJakub Pawlowski cancel_delayed_work_sync(&hdev->le_scan_restart); 16574518bb0fSJohan Hedberg 1658d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_MGMT)) 1659d6bfd59cSJohan Hedberg cancel_delayed_work_sync(&hdev->rpa_expired); 16607ba8b4beSAndre Guedes 16615d900e46SFlorian Grandel if (hdev->adv_instance_timeout) { 16625d900e46SFlorian Grandel cancel_delayed_work_sync(&hdev->adv_instance_expire); 16635d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 16645d900e46SFlorian Grandel } 16655d900e46SFlorian Grandel 166676727c02SJohan Hedberg /* Avoid potential lockdep warnings from the *_flush() calls by 166776727c02SJohan Hedberg * ensuring the workqueue is empty up front. 166876727c02SJohan Hedberg */ 166976727c02SJohan Hedberg drain_workqueue(hdev->workqueue); 167076727c02SJohan Hedberg 167109fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 16721aeb9c65SJohan Hedberg 16738f502f84SJohan Hedberg hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 16748f502f84SJohan Hedberg 1675acc649c6SMarcel Holtmann auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); 1676acc649c6SMarcel Holtmann 1677acc649c6SMarcel Holtmann if (!auto_off && hdev->dev_type == HCI_BREDR) 16781aeb9c65SJohan Hedberg mgmt_powered(hdev, 0); 16791aeb9c65SJohan Hedberg 16801f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 1681d7347f3cSJohan Hedberg hci_pend_le_actions_clear(hdev); 1682f161dd41SJohan Hedberg hci_conn_hash_flush(hdev); 168309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 16841da177e4SLinus Torvalds 168564dae967SMarcel Holtmann smp_unregister(hdev); 168664dae967SMarcel Holtmann 16871da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_DOWN); 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds if (hdev->flush) 16901da177e4SLinus Torvalds hdev->flush(hdev); 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds /* Reset device */ 16931da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 16941da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 1695acc649c6SMarcel Holtmann if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) && 1696acc649c6SMarcel Holtmann !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 16971da177e4SLinus Torvalds set_bit(HCI_INIT, &hdev->flags); 169801178cd4SJohan Hedberg __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); 16991da177e4SLinus Torvalds clear_bit(HCI_INIT, &hdev->flags); 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 1702c347b765SGustavo F. Padovan /* flush cmd work */ 1703c347b765SGustavo F. Padovan flush_work(&hdev->cmd_work); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* Drop queues */ 17061da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 17071da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 17081da177e4SLinus Torvalds skb_queue_purge(&hdev->raw_q); 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds /* Drop last sent command */ 17111da177e4SLinus Torvalds if (hdev->sent_cmd) { 171265cc2b49SMarcel Holtmann cancel_delayed_work_sync(&hdev->cmd_timer); 17131da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 17141da177e4SLinus Torvalds hdev->sent_cmd = NULL; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 1717e9ca8bf1SMarcel Holtmann clear_bit(HCI_RUNNING, &hdev->flags); 17184a3f95b7SMarcel Holtmann hci_notify(hdev, HCI_DEV_CLOSE); 17194a3f95b7SMarcel Holtmann 17201da177e4SLinus Torvalds /* After this point our queues are empty 17211da177e4SLinus Torvalds * and no tasks are scheduled. */ 17221da177e4SLinus Torvalds hdev->close(hdev); 17231da177e4SLinus Torvalds 172435b973c9SJohan Hedberg /* Clear flags */ 1725fee746b0SMarcel Holtmann hdev->flags &= BIT(HCI_RAW); 1726eacb44dfSMarcel Holtmann hci_dev_clear_volatile_flags(hdev); 172735b973c9SJohan Hedberg 1728ced5c338SAndrei Emeltchenko /* Controller radio is available but is currently powered down */ 1729536619e8SMarcel Holtmann hdev->amp_status = AMP_STATUS_POWERED_DOWN; 1730ced5c338SAndrei Emeltchenko 1731e59fda8dSJohan Hedberg memset(hdev->eir, 0, sizeof(hdev->eir)); 173209b3c3fbSJohan Hedberg memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); 17337a4cd51dSMarcel Holtmann bacpy(&hdev->random_addr, BDADDR_ANY); 1734e59fda8dSJohan Hedberg 17351da177e4SLinus Torvalds hci_req_unlock(hdev); 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds hci_dev_put(hdev); 17381da177e4SLinus Torvalds return 0; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds int hci_dev_close(__u16 dev) 17421da177e4SLinus Torvalds { 17431da177e4SLinus Torvalds struct hci_dev *hdev; 17441da177e4SLinus Torvalds int err; 17451da177e4SLinus Torvalds 174670f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 174770f23020SAndrei Emeltchenko if (!hdev) 17481da177e4SLinus Torvalds return -ENODEV; 17498ee56540SMarcel Holtmann 1750d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 17510736cfa8SMarcel Holtmann err = -EBUSY; 17520736cfa8SMarcel Holtmann goto done; 17530736cfa8SMarcel Holtmann } 17540736cfa8SMarcel Holtmann 1755a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) 17568ee56540SMarcel Holtmann cancel_delayed_work(&hdev->power_off); 17578ee56540SMarcel Holtmann 17581da177e4SLinus Torvalds err = hci_dev_do_close(hdev); 17598ee56540SMarcel Holtmann 17600736cfa8SMarcel Holtmann done: 17611da177e4SLinus Torvalds hci_dev_put(hdev); 17621da177e4SLinus Torvalds return err; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 17655c912495SMarcel Holtmann static int hci_dev_do_reset(struct hci_dev *hdev) 17661da177e4SLinus Torvalds { 17675c912495SMarcel Holtmann int ret; 17681da177e4SLinus Torvalds 17695c912495SMarcel Holtmann BT_DBG("%s %p", hdev->name, hdev); 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds hci_req_lock(hdev); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds /* Drop queues */ 17741da177e4SLinus Torvalds skb_queue_purge(&hdev->rx_q); 17751da177e4SLinus Torvalds skb_queue_purge(&hdev->cmd_q); 17761da177e4SLinus Torvalds 177776727c02SJohan Hedberg /* Avoid potential lockdep warnings from the *_flush() calls by 177876727c02SJohan Hedberg * ensuring the workqueue is empty up front. 177976727c02SJohan Hedberg */ 178076727c02SJohan Hedberg drain_workqueue(hdev->workqueue); 178176727c02SJohan Hedberg 178209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 17831f9b9a5dSAndre Guedes hci_inquiry_cache_flush(hdev); 17841da177e4SLinus Torvalds hci_conn_hash_flush(hdev); 178509fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds if (hdev->flush) 17881da177e4SLinus Torvalds hdev->flush(hdev); 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds atomic_set(&hdev->cmd_cnt, 1); 17916ed58ec5SVille Tervo hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0; 17921da177e4SLinus Torvalds 179301178cd4SJohan Hedberg ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds hci_req_unlock(hdev); 17961da177e4SLinus Torvalds return ret; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17995c912495SMarcel Holtmann int hci_dev_reset(__u16 dev) 18005c912495SMarcel Holtmann { 18015c912495SMarcel Holtmann struct hci_dev *hdev; 18025c912495SMarcel Holtmann int err; 18035c912495SMarcel Holtmann 18045c912495SMarcel Holtmann hdev = hci_dev_get(dev); 18055c912495SMarcel Holtmann if (!hdev) 18065c912495SMarcel Holtmann return -ENODEV; 18075c912495SMarcel Holtmann 18085c912495SMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) { 18095c912495SMarcel Holtmann err = -ENETDOWN; 18105c912495SMarcel Holtmann goto done; 18115c912495SMarcel Holtmann } 18125c912495SMarcel Holtmann 1813d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 18145c912495SMarcel Holtmann err = -EBUSY; 18155c912495SMarcel Holtmann goto done; 18165c912495SMarcel Holtmann } 18175c912495SMarcel Holtmann 1818d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 18195c912495SMarcel Holtmann err = -EOPNOTSUPP; 18205c912495SMarcel Holtmann goto done; 18215c912495SMarcel Holtmann } 18225c912495SMarcel Holtmann 18235c912495SMarcel Holtmann err = hci_dev_do_reset(hdev); 18245c912495SMarcel Holtmann 18255c912495SMarcel Holtmann done: 18265c912495SMarcel Holtmann hci_dev_put(hdev); 18275c912495SMarcel Holtmann return err; 18285c912495SMarcel Holtmann } 18295c912495SMarcel Holtmann 18301da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds struct hci_dev *hdev; 18331da177e4SLinus Torvalds int ret = 0; 18341da177e4SLinus Torvalds 183570f23020SAndrei Emeltchenko hdev = hci_dev_get(dev); 183670f23020SAndrei Emeltchenko if (!hdev) 18371da177e4SLinus Torvalds return -ENODEV; 18381da177e4SLinus Torvalds 1839d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 18400736cfa8SMarcel Holtmann ret = -EBUSY; 18410736cfa8SMarcel Holtmann goto done; 18420736cfa8SMarcel Holtmann } 18430736cfa8SMarcel Holtmann 1844d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 1845fee746b0SMarcel Holtmann ret = -EOPNOTSUPP; 1846fee746b0SMarcel Holtmann goto done; 1847fee746b0SMarcel Holtmann } 1848fee746b0SMarcel Holtmann 18491da177e4SLinus Torvalds memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); 18501da177e4SLinus Torvalds 18510736cfa8SMarcel Holtmann done: 18521da177e4SLinus Torvalds hci_dev_put(hdev); 18531da177e4SLinus Torvalds return ret; 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 1856123abc08SJohan Hedberg static void hci_update_scan_state(struct hci_dev *hdev, u8 scan) 1857123abc08SJohan Hedberg { 1858bc6d2d04SJohan Hedberg bool conn_changed, discov_changed; 1859123abc08SJohan Hedberg 1860123abc08SJohan Hedberg BT_DBG("%s scan 0x%02x", hdev->name, scan); 1861123abc08SJohan Hedberg 1862123abc08SJohan Hedberg if ((scan & SCAN_PAGE)) 1863238be788SMarcel Holtmann conn_changed = !hci_dev_test_and_set_flag(hdev, 1864238be788SMarcel Holtmann HCI_CONNECTABLE); 1865123abc08SJohan Hedberg else 1866a69d8927SMarcel Holtmann conn_changed = hci_dev_test_and_clear_flag(hdev, 1867a69d8927SMarcel Holtmann HCI_CONNECTABLE); 1868123abc08SJohan Hedberg 1869bc6d2d04SJohan Hedberg if ((scan & SCAN_INQUIRY)) { 1870238be788SMarcel Holtmann discov_changed = !hci_dev_test_and_set_flag(hdev, 1871238be788SMarcel Holtmann HCI_DISCOVERABLE); 1872bc6d2d04SJohan Hedberg } else { 1873a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1874a69d8927SMarcel Holtmann discov_changed = hci_dev_test_and_clear_flag(hdev, 1875a69d8927SMarcel Holtmann HCI_DISCOVERABLE); 1876bc6d2d04SJohan Hedberg } 1877bc6d2d04SJohan Hedberg 1878d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_MGMT)) 1879123abc08SJohan Hedberg return; 1880123abc08SJohan Hedberg 1881bc6d2d04SJohan Hedberg if (conn_changed || discov_changed) { 1882bc6d2d04SJohan Hedberg /* In case this was disabled through mgmt */ 1883a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); 1884bc6d2d04SJohan Hedberg 1885d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1886bc6d2d04SJohan Hedberg mgmt_update_adv_data(hdev); 1887bc6d2d04SJohan Hedberg 1888123abc08SJohan Hedberg mgmt_new_settings(hdev); 1889123abc08SJohan Hedberg } 1890bc6d2d04SJohan Hedberg } 1891123abc08SJohan Hedberg 18921da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg) 18931da177e4SLinus Torvalds { 18941da177e4SLinus Torvalds struct hci_dev *hdev; 18951da177e4SLinus Torvalds struct hci_dev_req dr; 18961da177e4SLinus Torvalds int err = 0; 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds if (copy_from_user(&dr, arg, sizeof(dr))) 18991da177e4SLinus Torvalds return -EFAULT; 19001da177e4SLinus Torvalds 190170f23020SAndrei Emeltchenko hdev = hci_dev_get(dr.dev_id); 190270f23020SAndrei Emeltchenko if (!hdev) 19031da177e4SLinus Torvalds return -ENODEV; 19041da177e4SLinus Torvalds 1905d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 19060736cfa8SMarcel Holtmann err = -EBUSY; 19070736cfa8SMarcel Holtmann goto done; 19080736cfa8SMarcel Holtmann } 19090736cfa8SMarcel Holtmann 1910d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 1911fee746b0SMarcel Holtmann err = -EOPNOTSUPP; 1912fee746b0SMarcel Holtmann goto done; 1913fee746b0SMarcel Holtmann } 1914fee746b0SMarcel Holtmann 19155b69bef5SMarcel Holtmann if (hdev->dev_type != HCI_BREDR) { 19165b69bef5SMarcel Holtmann err = -EOPNOTSUPP; 19175b69bef5SMarcel Holtmann goto done; 19185b69bef5SMarcel Holtmann } 19195b69bef5SMarcel Holtmann 1920d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 192156f87901SJohan Hedberg err = -EOPNOTSUPP; 192256f87901SJohan Hedberg goto done; 192356f87901SJohan Hedberg } 192456f87901SJohan Hedberg 19251da177e4SLinus Torvalds switch (cmd) { 19261da177e4SLinus Torvalds case HCISETAUTH: 192701178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 19285f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19291da177e4SLinus Torvalds break; 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds case HCISETENCRYPT: 19321da177e4SLinus Torvalds if (!lmp_encrypt_capable(hdev)) { 19331da177e4SLinus Torvalds err = -EOPNOTSUPP; 19341da177e4SLinus Torvalds break; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds if (!test_bit(HCI_AUTH, &hdev->flags)) { 19381da177e4SLinus Torvalds /* Auth must be enabled first */ 193901178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 19405f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19411da177e4SLinus Torvalds if (err) 19421da177e4SLinus Torvalds break; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 194501178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt, 19465f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19471da177e4SLinus Torvalds break; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds case HCISETSCAN: 195001178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt, 19515f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 195291a668b0SJohan Hedberg 1953bc6d2d04SJohan Hedberg /* Ensure that the connectable and discoverable states 1954bc6d2d04SJohan Hedberg * get correctly modified as this was a non-mgmt change. 195591a668b0SJohan Hedberg */ 1956123abc08SJohan Hedberg if (!err) 1957123abc08SJohan Hedberg hci_update_scan_state(hdev, dr.dev_opt); 19581da177e4SLinus Torvalds break; 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds case HCISETLINKPOL: 196101178cd4SJohan Hedberg err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt, 19625f246e89SAndrei Emeltchenko HCI_INIT_TIMEOUT); 19631da177e4SLinus Torvalds break; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds case HCISETLINKMODE: 1966e4e8e37cSMarcel Holtmann hdev->link_mode = ((__u16) dr.dev_opt) & 1967e4e8e37cSMarcel Holtmann (HCI_LM_MASTER | HCI_LM_ACCEPT); 1968e4e8e37cSMarcel Holtmann break; 1969e4e8e37cSMarcel Holtmann 1970e4e8e37cSMarcel Holtmann case HCISETPTYPE: 1971e4e8e37cSMarcel Holtmann hdev->pkt_type = (__u16) dr.dev_opt; 19721da177e4SLinus Torvalds break; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds case HCISETACLMTU: 19751da177e4SLinus Torvalds hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1); 19761da177e4SLinus Torvalds hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0); 19771da177e4SLinus Torvalds break; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds case HCISETSCOMTU: 19801da177e4SLinus Torvalds hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1); 19811da177e4SLinus Torvalds hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0); 19821da177e4SLinus Torvalds break; 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds default: 19851da177e4SLinus Torvalds err = -EINVAL; 19861da177e4SLinus Torvalds break; 19871da177e4SLinus Torvalds } 1988e4e8e37cSMarcel Holtmann 19890736cfa8SMarcel Holtmann done: 19901da177e4SLinus Torvalds hci_dev_put(hdev); 19911da177e4SLinus Torvalds return err; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg) 19951da177e4SLinus Torvalds { 19968035ded4SLuiz Augusto von Dentz struct hci_dev *hdev; 19971da177e4SLinus Torvalds struct hci_dev_list_req *dl; 19981da177e4SLinus Torvalds struct hci_dev_req *dr; 19991da177e4SLinus Torvalds int n = 0, size, err; 20001da177e4SLinus Torvalds __u16 dev_num; 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds if (get_user(dev_num, (__u16 __user *) arg)) 20031da177e4SLinus Torvalds return -EFAULT; 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) 20061da177e4SLinus Torvalds return -EINVAL; 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds size = sizeof(*dl) + dev_num * sizeof(*dr); 20091da177e4SLinus Torvalds 201070f23020SAndrei Emeltchenko dl = kzalloc(size, GFP_KERNEL); 201170f23020SAndrei Emeltchenko if (!dl) 20121da177e4SLinus Torvalds return -ENOMEM; 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds dr = dl->dev_req; 20151da177e4SLinus Torvalds 2016f20d09d5SGustavo F. Padovan read_lock(&hci_dev_list_lock); 20178035ded4SLuiz Augusto von Dentz list_for_each_entry(hdev, &hci_dev_list, list) { 20182e84d8dbSMarcel Holtmann unsigned long flags = hdev->flags; 2019c542a06cSJohan Hedberg 20202e84d8dbSMarcel Holtmann /* When the auto-off is configured it means the transport 20212e84d8dbSMarcel Holtmann * is running, but in that case still indicate that the 20222e84d8dbSMarcel Holtmann * device is actually down. 20232e84d8dbSMarcel Holtmann */ 2024d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) 20252e84d8dbSMarcel Holtmann flags &= ~BIT(HCI_UP); 2026c542a06cSJohan Hedberg 20271da177e4SLinus Torvalds (dr + n)->dev_id = hdev->id; 20282e84d8dbSMarcel Holtmann (dr + n)->dev_opt = flags; 2029c542a06cSJohan Hedberg 20301da177e4SLinus Torvalds if (++n >= dev_num) 20311da177e4SLinus Torvalds break; 20321da177e4SLinus Torvalds } 2033f20d09d5SGustavo F. Padovan read_unlock(&hci_dev_list_lock); 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds dl->dev_num = n; 20361da177e4SLinus Torvalds size = sizeof(*dl) + n * sizeof(*dr); 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds err = copy_to_user(arg, dl, size); 20391da177e4SLinus Torvalds kfree(dl); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds return err ? -EFAULT : 0; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg) 20451da177e4SLinus Torvalds { 20461da177e4SLinus Torvalds struct hci_dev *hdev; 20471da177e4SLinus Torvalds struct hci_dev_info di; 20482e84d8dbSMarcel Holtmann unsigned long flags; 20491da177e4SLinus Torvalds int err = 0; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds if (copy_from_user(&di, arg, sizeof(di))) 20521da177e4SLinus Torvalds return -EFAULT; 20531da177e4SLinus Torvalds 205470f23020SAndrei Emeltchenko hdev = hci_dev_get(di.dev_id); 205570f23020SAndrei Emeltchenko if (!hdev) 20561da177e4SLinus Torvalds return -ENODEV; 20571da177e4SLinus Torvalds 20582e84d8dbSMarcel Holtmann /* When the auto-off is configured it means the transport 20592e84d8dbSMarcel Holtmann * is running, but in that case still indicate that the 20602e84d8dbSMarcel Holtmann * device is actually down. 20612e84d8dbSMarcel Holtmann */ 2062d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) 20632e84d8dbSMarcel Holtmann flags = hdev->flags & ~BIT(HCI_UP); 20642e84d8dbSMarcel Holtmann else 20652e84d8dbSMarcel Holtmann flags = hdev->flags; 2066c542a06cSJohan Hedberg 20671da177e4SLinus Torvalds strcpy(di.name, hdev->name); 20681da177e4SLinus Torvalds di.bdaddr = hdev->bdaddr; 206960f2a3edSMarcel Holtmann di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4); 20702e84d8dbSMarcel Holtmann di.flags = flags; 20711da177e4SLinus Torvalds di.pkt_type = hdev->pkt_type; 2072572c7f84SJohan Hedberg if (lmp_bredr_capable(hdev)) { 20731da177e4SLinus Torvalds di.acl_mtu = hdev->acl_mtu; 20741da177e4SLinus Torvalds di.acl_pkts = hdev->acl_pkts; 20751da177e4SLinus Torvalds di.sco_mtu = hdev->sco_mtu; 20761da177e4SLinus Torvalds di.sco_pkts = hdev->sco_pkts; 2077572c7f84SJohan Hedberg } else { 2078572c7f84SJohan Hedberg di.acl_mtu = hdev->le_mtu; 2079572c7f84SJohan Hedberg di.acl_pkts = hdev->le_pkts; 2080572c7f84SJohan Hedberg di.sco_mtu = 0; 2081572c7f84SJohan Hedberg di.sco_pkts = 0; 2082572c7f84SJohan Hedberg } 20831da177e4SLinus Torvalds di.link_policy = hdev->link_policy; 20841da177e4SLinus Torvalds di.link_mode = hdev->link_mode; 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds memcpy(&di.stat, &hdev->stat, sizeof(di.stat)); 20871da177e4SLinus Torvalds memcpy(&di.features, &hdev->features, sizeof(di.features)); 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds if (copy_to_user(arg, &di, sizeof(di))) 20901da177e4SLinus Torvalds err = -EFAULT; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds hci_dev_put(hdev); 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds return err; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds /* ---- Interface to HCI drivers ---- */ 20981da177e4SLinus Torvalds 2099611b30f7SMarcel Holtmann static int hci_rfkill_set_block(void *data, bool blocked) 2100611b30f7SMarcel Holtmann { 2101611b30f7SMarcel Holtmann struct hci_dev *hdev = data; 2102611b30f7SMarcel Holtmann 2103611b30f7SMarcel Holtmann BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); 2104611b30f7SMarcel Holtmann 2105d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) 21060736cfa8SMarcel Holtmann return -EBUSY; 21070736cfa8SMarcel Holtmann 21085e130367SJohan Hedberg if (blocked) { 2109a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_RFKILLED); 2110d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_SETUP) && 2111d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) 2112611b30f7SMarcel Holtmann hci_dev_do_close(hdev); 21135e130367SJohan Hedberg } else { 2114a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_RFKILLED); 21155e130367SJohan Hedberg } 2116611b30f7SMarcel Holtmann 2117611b30f7SMarcel Holtmann return 0; 2118611b30f7SMarcel Holtmann } 2119611b30f7SMarcel Holtmann 2120611b30f7SMarcel Holtmann static const struct rfkill_ops hci_rfkill_ops = { 2121611b30f7SMarcel Holtmann .set_block = hci_rfkill_set_block, 2122611b30f7SMarcel Holtmann }; 2123611b30f7SMarcel Holtmann 2124ab81cbf9SJohan Hedberg static void hci_power_on(struct work_struct *work) 2125ab81cbf9SJohan Hedberg { 2126ab81cbf9SJohan Hedberg struct hci_dev *hdev = container_of(work, struct hci_dev, power_on); 212796570ffcSJohan Hedberg int err; 2128ab81cbf9SJohan Hedberg 2129ab81cbf9SJohan Hedberg BT_DBG("%s", hdev->name); 2130ab81cbf9SJohan Hedberg 2131cbed0ca1SJohan Hedberg err = hci_dev_do_open(hdev); 213296570ffcSJohan Hedberg if (err < 0) { 21333ad67582SJaganath Kanakkassery hci_dev_lock(hdev); 213496570ffcSJohan Hedberg mgmt_set_powered_failed(hdev, err); 21353ad67582SJaganath Kanakkassery hci_dev_unlock(hdev); 2136ab81cbf9SJohan Hedberg return; 213796570ffcSJohan Hedberg } 2138ab81cbf9SJohan Hedberg 2139a5c8f270SMarcel Holtmann /* During the HCI setup phase, a few error conditions are 2140a5c8f270SMarcel Holtmann * ignored and they need to be checked now. If they are still 2141a5c8f270SMarcel Holtmann * valid, it is important to turn the device back off. 2142a5c8f270SMarcel Holtmann */ 2143d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_RFKILLED) || 2144d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || 2145a5c8f270SMarcel Holtmann (hdev->dev_type == HCI_BREDR && 2146a5c8f270SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) && 2147a5c8f270SMarcel Holtmann !bacmp(&hdev->static_addr, BDADDR_ANY))) { 2148a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_AUTO_OFF); 2149bf543036SJohan Hedberg hci_dev_do_close(hdev); 2150d7a5a11dSMarcel Holtmann } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { 215119202573SJohan Hedberg queue_delayed_work(hdev->req_workqueue, &hdev->power_off, 215219202573SJohan Hedberg HCI_AUTO_OFF_TIMEOUT); 2153bf543036SJohan Hedberg } 2154ab81cbf9SJohan Hedberg 2155a69d8927SMarcel Holtmann if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { 21564a964404SMarcel Holtmann /* For unconfigured devices, set the HCI_RAW flag 21574a964404SMarcel Holtmann * so that userspace can easily identify them. 21584a964404SMarcel Holtmann */ 2159d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 21604a964404SMarcel Holtmann set_bit(HCI_RAW, &hdev->flags); 21610602a8adSMarcel Holtmann 21620602a8adSMarcel Holtmann /* For fully configured devices, this will send 21630602a8adSMarcel Holtmann * the Index Added event. For unconfigured devices, 21640602a8adSMarcel Holtmann * it will send Unconfigued Index Added event. 21650602a8adSMarcel Holtmann * 21660602a8adSMarcel Holtmann * Devices with HCI_QUIRK_RAW_DEVICE are ignored 21670602a8adSMarcel Holtmann * and no event will be send. 21680602a8adSMarcel Holtmann */ 2169744cf19eSJohan Hedberg mgmt_index_added(hdev); 2170a69d8927SMarcel Holtmann } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { 21715ea234d3SMarcel Holtmann /* When the controller is now configured, then it 21725ea234d3SMarcel Holtmann * is important to clear the HCI_RAW flag. 21735ea234d3SMarcel Holtmann */ 2174d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 21755ea234d3SMarcel Holtmann clear_bit(HCI_RAW, &hdev->flags); 21765ea234d3SMarcel Holtmann 2177d603b76bSMarcel Holtmann /* Powering on the controller with HCI_CONFIG set only 2178d603b76bSMarcel Holtmann * happens with the transition from unconfigured to 2179d603b76bSMarcel Holtmann * configured. This will send the Index Added event. 2180d603b76bSMarcel Holtmann */ 2181d603b76bSMarcel Holtmann mgmt_index_added(hdev); 2182ab81cbf9SJohan Hedberg } 2183ab81cbf9SJohan Hedberg } 2184ab81cbf9SJohan Hedberg 2185ab81cbf9SJohan Hedberg static void hci_power_off(struct work_struct *work) 2186ab81cbf9SJohan Hedberg { 21873243553fSJohan Hedberg struct hci_dev *hdev = container_of(work, struct hci_dev, 21883243553fSJohan Hedberg power_off.work); 2189ab81cbf9SJohan Hedberg 2190ab81cbf9SJohan Hedberg BT_DBG("%s", hdev->name); 2191ab81cbf9SJohan Hedberg 21928ee56540SMarcel Holtmann hci_dev_do_close(hdev); 2193ab81cbf9SJohan Hedberg } 2194ab81cbf9SJohan Hedberg 2195c7741d16SMarcel Holtmann static void hci_error_reset(struct work_struct *work) 2196c7741d16SMarcel Holtmann { 2197c7741d16SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, error_reset); 2198c7741d16SMarcel Holtmann 2199c7741d16SMarcel Holtmann BT_DBG("%s", hdev->name); 2200c7741d16SMarcel Holtmann 2201c7741d16SMarcel Holtmann if (hdev->hw_error) 2202c7741d16SMarcel Holtmann hdev->hw_error(hdev, hdev->hw_error_code); 2203c7741d16SMarcel Holtmann else 2204c7741d16SMarcel Holtmann BT_ERR("%s hardware error 0x%2.2x", hdev->name, 2205c7741d16SMarcel Holtmann hdev->hw_error_code); 2206c7741d16SMarcel Holtmann 2207c7741d16SMarcel Holtmann if (hci_dev_do_close(hdev)) 2208c7741d16SMarcel Holtmann return; 2209c7741d16SMarcel Holtmann 2210c7741d16SMarcel Holtmann hci_dev_do_open(hdev); 2211c7741d16SMarcel Holtmann } 2212c7741d16SMarcel Holtmann 221316ab91abSJohan Hedberg static void hci_discov_off(struct work_struct *work) 221416ab91abSJohan Hedberg { 221516ab91abSJohan Hedberg struct hci_dev *hdev; 221616ab91abSJohan Hedberg 221716ab91abSJohan Hedberg hdev = container_of(work, struct hci_dev, discov_off.work); 221816ab91abSJohan Hedberg 221916ab91abSJohan Hedberg BT_DBG("%s", hdev->name); 222016ab91abSJohan Hedberg 2221d1967ff8SMarcel Holtmann mgmt_discoverable_timeout(hdev); 222216ab91abSJohan Hedberg } 222316ab91abSJohan Hedberg 22245d900e46SFlorian Grandel static void hci_adv_timeout_expire(struct work_struct *work) 22255d900e46SFlorian Grandel { 22265d900e46SFlorian Grandel struct hci_dev *hdev; 22275d900e46SFlorian Grandel 22285d900e46SFlorian Grandel hdev = container_of(work, struct hci_dev, adv_instance_expire.work); 22295d900e46SFlorian Grandel 22305d900e46SFlorian Grandel BT_DBG("%s", hdev->name); 22315d900e46SFlorian Grandel 22325d900e46SFlorian Grandel mgmt_adv_timeout_expired(hdev); 22335d900e46SFlorian Grandel } 22345d900e46SFlorian Grandel 223535f7498aSJohan Hedberg void hci_uuids_clear(struct hci_dev *hdev) 22362aeb9a1aSJohan Hedberg { 22374821002cSJohan Hedberg struct bt_uuid *uuid, *tmp; 22382aeb9a1aSJohan Hedberg 22394821002cSJohan Hedberg list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) { 22404821002cSJohan Hedberg list_del(&uuid->list); 22412aeb9a1aSJohan Hedberg kfree(uuid); 22422aeb9a1aSJohan Hedberg } 22432aeb9a1aSJohan Hedberg } 22442aeb9a1aSJohan Hedberg 224535f7498aSJohan Hedberg void hci_link_keys_clear(struct hci_dev *hdev) 224655ed8ca1SJohan Hedberg { 224755ed8ca1SJohan Hedberg struct link_key *key; 224855ed8ca1SJohan Hedberg 22490378b597SJohan Hedberg list_for_each_entry_rcu(key, &hdev->link_keys, list) { 22500378b597SJohan Hedberg list_del_rcu(&key->list); 22510378b597SJohan Hedberg kfree_rcu(key, rcu); 225255ed8ca1SJohan Hedberg } 225355ed8ca1SJohan Hedberg } 225455ed8ca1SJohan Hedberg 225535f7498aSJohan Hedberg void hci_smp_ltks_clear(struct hci_dev *hdev) 2256b899efafSVinicius Costa Gomes { 2257970d0f1bSJohan Hedberg struct smp_ltk *k; 2258b899efafSVinicius Costa Gomes 2259970d0f1bSJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 2260970d0f1bSJohan Hedberg list_del_rcu(&k->list); 2261970d0f1bSJohan Hedberg kfree_rcu(k, rcu); 2262b899efafSVinicius Costa Gomes } 2263b899efafSVinicius Costa Gomes } 2264b899efafSVinicius Costa Gomes 2265970c4e46SJohan Hedberg void hci_smp_irks_clear(struct hci_dev *hdev) 2266970c4e46SJohan Hedberg { 2267adae20cbSJohan Hedberg struct smp_irk *k; 2268970c4e46SJohan Hedberg 2269adae20cbSJohan Hedberg list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) { 2270adae20cbSJohan Hedberg list_del_rcu(&k->list); 2271adae20cbSJohan Hedberg kfree_rcu(k, rcu); 2272970c4e46SJohan Hedberg } 2273970c4e46SJohan Hedberg } 2274970c4e46SJohan Hedberg 227555ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) 227655ed8ca1SJohan Hedberg { 227755ed8ca1SJohan Hedberg struct link_key *k; 227855ed8ca1SJohan Hedberg 22790378b597SJohan Hedberg rcu_read_lock(); 22800378b597SJohan Hedberg list_for_each_entry_rcu(k, &hdev->link_keys, list) { 22810378b597SJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) == 0) { 22820378b597SJohan Hedberg rcu_read_unlock(); 228355ed8ca1SJohan Hedberg return k; 22840378b597SJohan Hedberg } 22850378b597SJohan Hedberg } 22860378b597SJohan Hedberg rcu_read_unlock(); 228755ed8ca1SJohan Hedberg 228855ed8ca1SJohan Hedberg return NULL; 228955ed8ca1SJohan Hedberg } 229055ed8ca1SJohan Hedberg 2291745c0ce3SVishal Agarwal static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn, 2292d25e28abSJohan Hedberg u8 key_type, u8 old_key_type) 2293d25e28abSJohan Hedberg { 2294d25e28abSJohan Hedberg /* Legacy key */ 2295d25e28abSJohan Hedberg if (key_type < 0x03) 2296745c0ce3SVishal Agarwal return true; 2297d25e28abSJohan Hedberg 2298d25e28abSJohan Hedberg /* Debug keys are insecure so don't store them persistently */ 2299d25e28abSJohan Hedberg if (key_type == HCI_LK_DEBUG_COMBINATION) 2300745c0ce3SVishal Agarwal return false; 2301d25e28abSJohan Hedberg 2302d25e28abSJohan Hedberg /* Changed combination key and there's no previous one */ 2303d25e28abSJohan Hedberg if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff) 2304745c0ce3SVishal Agarwal return false; 2305d25e28abSJohan Hedberg 2306d25e28abSJohan Hedberg /* Security mode 3 case */ 2307d25e28abSJohan Hedberg if (!conn) 2308745c0ce3SVishal Agarwal return true; 2309d25e28abSJohan Hedberg 2310e3befab9SJohan Hedberg /* BR/EDR key derived using SC from an LE link */ 2311e3befab9SJohan Hedberg if (conn->type == LE_LINK) 2312e3befab9SJohan Hedberg return true; 2313e3befab9SJohan Hedberg 2314d25e28abSJohan Hedberg /* Neither local nor remote side had no-bonding as requirement */ 2315d25e28abSJohan Hedberg if (conn->auth_type > 0x01 && conn->remote_auth > 0x01) 2316745c0ce3SVishal Agarwal return true; 2317d25e28abSJohan Hedberg 2318d25e28abSJohan Hedberg /* Local side had dedicated bonding as requirement */ 2319d25e28abSJohan Hedberg if (conn->auth_type == 0x02 || conn->auth_type == 0x03) 2320745c0ce3SVishal Agarwal return true; 2321d25e28abSJohan Hedberg 2322d25e28abSJohan Hedberg /* Remote side had dedicated bonding as requirement */ 2323d25e28abSJohan Hedberg if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) 2324745c0ce3SVishal Agarwal return true; 2325d25e28abSJohan Hedberg 2326d25e28abSJohan Hedberg /* If none of the above criteria match, then don't store the key 2327d25e28abSJohan Hedberg * persistently */ 2328745c0ce3SVishal Agarwal return false; 2329d25e28abSJohan Hedberg } 2330d25e28abSJohan Hedberg 2331e804d25dSJohan Hedberg static u8 ltk_role(u8 type) 233298a0b845SJohan Hedberg { 2333e804d25dSJohan Hedberg if (type == SMP_LTK) 2334e804d25dSJohan Hedberg return HCI_ROLE_MASTER; 233598a0b845SJohan Hedberg 2336e804d25dSJohan Hedberg return HCI_ROLE_SLAVE; 233798a0b845SJohan Hedberg } 233898a0b845SJohan Hedberg 2339f3a73d97SJohan Hedberg struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, 2340e804d25dSJohan Hedberg u8 addr_type, u8 role) 234175d262c2SVinicius Costa Gomes { 2342c9839a11SVinicius Costa Gomes struct smp_ltk *k; 234375d262c2SVinicius Costa Gomes 2344970d0f1bSJohan Hedberg rcu_read_lock(); 2345970d0f1bSJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 23465378bc56SJohan Hedberg if (addr_type != k->bdaddr_type || bacmp(bdaddr, &k->bdaddr)) 23475378bc56SJohan Hedberg continue; 23485378bc56SJohan Hedberg 2349923e2414SJohan Hedberg if (smp_ltk_is_sc(k) || ltk_role(k->type) == role) { 2350970d0f1bSJohan Hedberg rcu_read_unlock(); 235175d262c2SVinicius Costa Gomes return k; 2352970d0f1bSJohan Hedberg } 2353970d0f1bSJohan Hedberg } 2354970d0f1bSJohan Hedberg rcu_read_unlock(); 235575d262c2SVinicius Costa Gomes 235675d262c2SVinicius Costa Gomes return NULL; 235775d262c2SVinicius Costa Gomes } 235875d262c2SVinicius Costa Gomes 2359970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa) 2360970c4e46SJohan Hedberg { 2361970c4e46SJohan Hedberg struct smp_irk *irk; 2362970c4e46SJohan Hedberg 2363adae20cbSJohan Hedberg rcu_read_lock(); 2364adae20cbSJohan Hedberg list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { 2365adae20cbSJohan Hedberg if (!bacmp(&irk->rpa, rpa)) { 2366adae20cbSJohan Hedberg rcu_read_unlock(); 2367970c4e46SJohan Hedberg return irk; 2368970c4e46SJohan Hedberg } 2369adae20cbSJohan Hedberg } 2370970c4e46SJohan Hedberg 2371adae20cbSJohan Hedberg list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { 2372defce9e8SJohan Hedberg if (smp_irk_matches(hdev, irk->val, rpa)) { 2373970c4e46SJohan Hedberg bacpy(&irk->rpa, rpa); 2374adae20cbSJohan Hedberg rcu_read_unlock(); 2375970c4e46SJohan Hedberg return irk; 2376970c4e46SJohan Hedberg } 2377970c4e46SJohan Hedberg } 2378adae20cbSJohan Hedberg rcu_read_unlock(); 2379970c4e46SJohan Hedberg 2380970c4e46SJohan Hedberg return NULL; 2381970c4e46SJohan Hedberg } 2382970c4e46SJohan Hedberg 2383970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, 2384970c4e46SJohan Hedberg u8 addr_type) 2385970c4e46SJohan Hedberg { 2386970c4e46SJohan Hedberg struct smp_irk *irk; 2387970c4e46SJohan Hedberg 23886cfc9988SJohan Hedberg /* Identity Address must be public or static random */ 23896cfc9988SJohan Hedberg if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0) 23906cfc9988SJohan Hedberg return NULL; 23916cfc9988SJohan Hedberg 2392adae20cbSJohan Hedberg rcu_read_lock(); 2393adae20cbSJohan Hedberg list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) { 2394970c4e46SJohan Hedberg if (addr_type == irk->addr_type && 2395adae20cbSJohan Hedberg bacmp(bdaddr, &irk->bdaddr) == 0) { 2396adae20cbSJohan Hedberg rcu_read_unlock(); 2397970c4e46SJohan Hedberg return irk; 2398970c4e46SJohan Hedberg } 2399adae20cbSJohan Hedberg } 2400adae20cbSJohan Hedberg rcu_read_unlock(); 2401970c4e46SJohan Hedberg 2402970c4e46SJohan Hedberg return NULL; 2403970c4e46SJohan Hedberg } 2404970c4e46SJohan Hedberg 2405567fa2aaSJohan Hedberg struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, 24067652ff6aSJohan Hedberg bdaddr_t *bdaddr, u8 *val, u8 type, 24077652ff6aSJohan Hedberg u8 pin_len, bool *persistent) 240855ed8ca1SJohan Hedberg { 240955ed8ca1SJohan Hedberg struct link_key *key, *old_key; 2410745c0ce3SVishal Agarwal u8 old_key_type; 241155ed8ca1SJohan Hedberg 241255ed8ca1SJohan Hedberg old_key = hci_find_link_key(hdev, bdaddr); 241355ed8ca1SJohan Hedberg if (old_key) { 241455ed8ca1SJohan Hedberg old_key_type = old_key->type; 241555ed8ca1SJohan Hedberg key = old_key; 241655ed8ca1SJohan Hedberg } else { 241712adcf3aSJohan Hedberg old_key_type = conn ? conn->key_type : 0xff; 24180a14ab41SJohan Hedberg key = kzalloc(sizeof(*key), GFP_KERNEL); 241955ed8ca1SJohan Hedberg if (!key) 2420567fa2aaSJohan Hedberg return NULL; 24210378b597SJohan Hedberg list_add_rcu(&key->list, &hdev->link_keys); 242255ed8ca1SJohan Hedberg } 242355ed8ca1SJohan Hedberg 24246ed93dc6SAndrei Emeltchenko BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type); 242555ed8ca1SJohan Hedberg 2426d25e28abSJohan Hedberg /* Some buggy controller combinations generate a changed 2427d25e28abSJohan Hedberg * combination key for legacy pairing even when there's no 2428d25e28abSJohan Hedberg * previous key */ 2429d25e28abSJohan Hedberg if (type == HCI_LK_CHANGED_COMBINATION && 2430a8c5fb1aSGustavo Padovan (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) { 2431d25e28abSJohan Hedberg type = HCI_LK_COMBINATION; 2432655fe6ecSJohan Hedberg if (conn) 2433655fe6ecSJohan Hedberg conn->key_type = type; 2434655fe6ecSJohan Hedberg } 2435d25e28abSJohan Hedberg 243655ed8ca1SJohan Hedberg bacpy(&key->bdaddr, bdaddr); 24379b3b4460SAndrei Emeltchenko memcpy(key->val, val, HCI_LINK_KEY_SIZE); 243855ed8ca1SJohan Hedberg key->pin_len = pin_len; 243955ed8ca1SJohan Hedberg 2440b6020ba0SWaldemar Rymarkiewicz if (type == HCI_LK_CHANGED_COMBINATION) 244155ed8ca1SJohan Hedberg key->type = old_key_type; 24424748fed2SJohan Hedberg else 24434748fed2SJohan Hedberg key->type = type; 24444748fed2SJohan Hedberg 24457652ff6aSJohan Hedberg if (persistent) 24467652ff6aSJohan Hedberg *persistent = hci_persistent_key(hdev, conn, type, 24477652ff6aSJohan Hedberg old_key_type); 24484df378a1SJohan Hedberg 2449567fa2aaSJohan Hedberg return key; 245055ed8ca1SJohan Hedberg } 245155ed8ca1SJohan Hedberg 2452ca9142b8SJohan Hedberg struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, 245335d70271SJohan Hedberg u8 addr_type, u8 type, u8 authenticated, 2454fe39c7b2SMarcel Holtmann u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand) 245575d262c2SVinicius Costa Gomes { 2456c9839a11SVinicius Costa Gomes struct smp_ltk *key, *old_key; 2457e804d25dSJohan Hedberg u8 role = ltk_role(type); 245875d262c2SVinicius Costa Gomes 2459f3a73d97SJohan Hedberg old_key = hci_find_ltk(hdev, bdaddr, addr_type, role); 2460c9839a11SVinicius Costa Gomes if (old_key) 246175d262c2SVinicius Costa Gomes key = old_key; 2462c9839a11SVinicius Costa Gomes else { 24630a14ab41SJohan Hedberg key = kzalloc(sizeof(*key), GFP_KERNEL); 246475d262c2SVinicius Costa Gomes if (!key) 2465ca9142b8SJohan Hedberg return NULL; 2466970d0f1bSJohan Hedberg list_add_rcu(&key->list, &hdev->long_term_keys); 246775d262c2SVinicius Costa Gomes } 246875d262c2SVinicius Costa Gomes 246975d262c2SVinicius Costa Gomes bacpy(&key->bdaddr, bdaddr); 2470c9839a11SVinicius Costa Gomes key->bdaddr_type = addr_type; 2471c9839a11SVinicius Costa Gomes memcpy(key->val, tk, sizeof(key->val)); 2472c9839a11SVinicius Costa Gomes key->authenticated = authenticated; 2473c9839a11SVinicius Costa Gomes key->ediv = ediv; 2474fe39c7b2SMarcel Holtmann key->rand = rand; 2475c9839a11SVinicius Costa Gomes key->enc_size = enc_size; 2476c9839a11SVinicius Costa Gomes key->type = type; 247775d262c2SVinicius Costa Gomes 2478ca9142b8SJohan Hedberg return key; 247975d262c2SVinicius Costa Gomes } 248075d262c2SVinicius Costa Gomes 2481ca9142b8SJohan Hedberg struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, 2482ca9142b8SJohan Hedberg u8 addr_type, u8 val[16], bdaddr_t *rpa) 2483970c4e46SJohan Hedberg { 2484970c4e46SJohan Hedberg struct smp_irk *irk; 2485970c4e46SJohan Hedberg 2486970c4e46SJohan Hedberg irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type); 2487970c4e46SJohan Hedberg if (!irk) { 2488970c4e46SJohan Hedberg irk = kzalloc(sizeof(*irk), GFP_KERNEL); 2489970c4e46SJohan Hedberg if (!irk) 2490ca9142b8SJohan Hedberg return NULL; 2491970c4e46SJohan Hedberg 2492970c4e46SJohan Hedberg bacpy(&irk->bdaddr, bdaddr); 2493970c4e46SJohan Hedberg irk->addr_type = addr_type; 2494970c4e46SJohan Hedberg 2495adae20cbSJohan Hedberg list_add_rcu(&irk->list, &hdev->identity_resolving_keys); 2496970c4e46SJohan Hedberg } 2497970c4e46SJohan Hedberg 2498970c4e46SJohan Hedberg memcpy(irk->val, val, 16); 2499970c4e46SJohan Hedberg bacpy(&irk->rpa, rpa); 2500970c4e46SJohan Hedberg 2501ca9142b8SJohan Hedberg return irk; 2502970c4e46SJohan Hedberg } 2503970c4e46SJohan Hedberg 250455ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr) 250555ed8ca1SJohan Hedberg { 250655ed8ca1SJohan Hedberg struct link_key *key; 250755ed8ca1SJohan Hedberg 250855ed8ca1SJohan Hedberg key = hci_find_link_key(hdev, bdaddr); 250955ed8ca1SJohan Hedberg if (!key) 251055ed8ca1SJohan Hedberg return -ENOENT; 251155ed8ca1SJohan Hedberg 25126ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 251355ed8ca1SJohan Hedberg 25140378b597SJohan Hedberg list_del_rcu(&key->list); 25150378b597SJohan Hedberg kfree_rcu(key, rcu); 251655ed8ca1SJohan Hedberg 251755ed8ca1SJohan Hedberg return 0; 251855ed8ca1SJohan Hedberg } 251955ed8ca1SJohan Hedberg 2520e0b2b27eSJohan Hedberg int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type) 2521b899efafSVinicius Costa Gomes { 2522970d0f1bSJohan Hedberg struct smp_ltk *k; 2523c51ffa0bSJohan Hedberg int removed = 0; 2524b899efafSVinicius Costa Gomes 2525970d0f1bSJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 2526e0b2b27eSJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type) 2527b899efafSVinicius Costa Gomes continue; 2528b899efafSVinicius Costa Gomes 25296ed93dc6SAndrei Emeltchenko BT_DBG("%s removing %pMR", hdev->name, bdaddr); 2530b899efafSVinicius Costa Gomes 2531970d0f1bSJohan Hedberg list_del_rcu(&k->list); 2532970d0f1bSJohan Hedberg kfree_rcu(k, rcu); 2533c51ffa0bSJohan Hedberg removed++; 2534b899efafSVinicius Costa Gomes } 2535b899efafSVinicius Costa Gomes 2536c51ffa0bSJohan Hedberg return removed ? 0 : -ENOENT; 2537b899efafSVinicius Costa Gomes } 2538b899efafSVinicius Costa Gomes 2539a7ec7338SJohan Hedberg void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type) 2540a7ec7338SJohan Hedberg { 2541adae20cbSJohan Hedberg struct smp_irk *k; 2542a7ec7338SJohan Hedberg 2543adae20cbSJohan Hedberg list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) { 2544a7ec7338SJohan Hedberg if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type) 2545a7ec7338SJohan Hedberg continue; 2546a7ec7338SJohan Hedberg 2547a7ec7338SJohan Hedberg BT_DBG("%s removing %pMR", hdev->name, bdaddr); 2548a7ec7338SJohan Hedberg 2549adae20cbSJohan Hedberg list_del_rcu(&k->list); 2550adae20cbSJohan Hedberg kfree_rcu(k, rcu); 2551a7ec7338SJohan Hedberg } 2552a7ec7338SJohan Hedberg } 2553a7ec7338SJohan Hedberg 255455e76b38SJohan Hedberg bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 255555e76b38SJohan Hedberg { 255655e76b38SJohan Hedberg struct smp_ltk *k; 25574ba9faf3SJohan Hedberg struct smp_irk *irk; 255855e76b38SJohan Hedberg u8 addr_type; 255955e76b38SJohan Hedberg 256055e76b38SJohan Hedberg if (type == BDADDR_BREDR) { 256155e76b38SJohan Hedberg if (hci_find_link_key(hdev, bdaddr)) 256255e76b38SJohan Hedberg return true; 256355e76b38SJohan Hedberg return false; 256455e76b38SJohan Hedberg } 256555e76b38SJohan Hedberg 256655e76b38SJohan Hedberg /* Convert to HCI addr type which struct smp_ltk uses */ 256755e76b38SJohan Hedberg if (type == BDADDR_LE_PUBLIC) 256855e76b38SJohan Hedberg addr_type = ADDR_LE_DEV_PUBLIC; 256955e76b38SJohan Hedberg else 257055e76b38SJohan Hedberg addr_type = ADDR_LE_DEV_RANDOM; 257155e76b38SJohan Hedberg 25724ba9faf3SJohan Hedberg irk = hci_get_irk(hdev, bdaddr, addr_type); 25734ba9faf3SJohan Hedberg if (irk) { 25744ba9faf3SJohan Hedberg bdaddr = &irk->bdaddr; 25754ba9faf3SJohan Hedberg addr_type = irk->addr_type; 25764ba9faf3SJohan Hedberg } 25774ba9faf3SJohan Hedberg 257855e76b38SJohan Hedberg rcu_read_lock(); 257955e76b38SJohan Hedberg list_for_each_entry_rcu(k, &hdev->long_term_keys, list) { 258087c8b28dSJohan Hedberg if (k->bdaddr_type == addr_type && !bacmp(bdaddr, &k->bdaddr)) { 258187c8b28dSJohan Hedberg rcu_read_unlock(); 258255e76b38SJohan Hedberg return true; 258355e76b38SJohan Hedberg } 258487c8b28dSJohan Hedberg } 258555e76b38SJohan Hedberg rcu_read_unlock(); 258655e76b38SJohan Hedberg 258755e76b38SJohan Hedberg return false; 258855e76b38SJohan Hedberg } 258955e76b38SJohan Hedberg 25906bd32326SVille Tervo /* HCI command timer function */ 259165cc2b49SMarcel Holtmann static void hci_cmd_timeout(struct work_struct *work) 25926bd32326SVille Tervo { 259365cc2b49SMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, 259465cc2b49SMarcel Holtmann cmd_timer.work); 25956bd32326SVille Tervo 2596bda4f23aSAndrei Emeltchenko if (hdev->sent_cmd) { 2597bda4f23aSAndrei Emeltchenko struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data; 2598bda4f23aSAndrei Emeltchenko u16 opcode = __le16_to_cpu(sent->opcode); 2599bda4f23aSAndrei Emeltchenko 2600bda4f23aSAndrei Emeltchenko BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode); 2601bda4f23aSAndrei Emeltchenko } else { 26026bd32326SVille Tervo BT_ERR("%s command tx timeout", hdev->name); 2603bda4f23aSAndrei Emeltchenko } 2604bda4f23aSAndrei Emeltchenko 26056bd32326SVille Tervo atomic_set(&hdev->cmd_cnt, 1); 2606c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 26076bd32326SVille Tervo } 26086bd32326SVille Tervo 26092763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, 26106928a924SJohan Hedberg bdaddr_t *bdaddr, u8 bdaddr_type) 26112763eda6SSzymon Janc { 26122763eda6SSzymon Janc struct oob_data *data; 26132763eda6SSzymon Janc 26146928a924SJohan Hedberg list_for_each_entry(data, &hdev->remote_oob_data, list) { 26156928a924SJohan Hedberg if (bacmp(bdaddr, &data->bdaddr) != 0) 26166928a924SJohan Hedberg continue; 26176928a924SJohan Hedberg if (data->bdaddr_type != bdaddr_type) 26186928a924SJohan Hedberg continue; 26192763eda6SSzymon Janc return data; 26206928a924SJohan Hedberg } 26212763eda6SSzymon Janc 26222763eda6SSzymon Janc return NULL; 26232763eda6SSzymon Janc } 26242763eda6SSzymon Janc 26256928a924SJohan Hedberg int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, 26266928a924SJohan Hedberg u8 bdaddr_type) 26272763eda6SSzymon Janc { 26282763eda6SSzymon Janc struct oob_data *data; 26292763eda6SSzymon Janc 26306928a924SJohan Hedberg data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type); 26312763eda6SSzymon Janc if (!data) 26322763eda6SSzymon Janc return -ENOENT; 26332763eda6SSzymon Janc 26346928a924SJohan Hedberg BT_DBG("%s removing %pMR (%u)", hdev->name, bdaddr, bdaddr_type); 26352763eda6SSzymon Janc 26362763eda6SSzymon Janc list_del(&data->list); 26372763eda6SSzymon Janc kfree(data); 26382763eda6SSzymon Janc 26392763eda6SSzymon Janc return 0; 26402763eda6SSzymon Janc } 26412763eda6SSzymon Janc 264235f7498aSJohan Hedberg void hci_remote_oob_data_clear(struct hci_dev *hdev) 26432763eda6SSzymon Janc { 26442763eda6SSzymon Janc struct oob_data *data, *n; 26452763eda6SSzymon Janc 26462763eda6SSzymon Janc list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) { 26472763eda6SSzymon Janc list_del(&data->list); 26482763eda6SSzymon Janc kfree(data); 26492763eda6SSzymon Janc } 26502763eda6SSzymon Janc } 26512763eda6SSzymon Janc 26520798872eSMarcel Holtmann int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, 26536928a924SJohan Hedberg u8 bdaddr_type, u8 *hash192, u8 *rand192, 265438da1703SJohan Hedberg u8 *hash256, u8 *rand256) 26550798872eSMarcel Holtmann { 26560798872eSMarcel Holtmann struct oob_data *data; 26570798872eSMarcel Holtmann 26586928a924SJohan Hedberg data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type); 26590798872eSMarcel Holtmann if (!data) { 26600a14ab41SJohan Hedberg data = kmalloc(sizeof(*data), GFP_KERNEL); 26610798872eSMarcel Holtmann if (!data) 26620798872eSMarcel Holtmann return -ENOMEM; 26630798872eSMarcel Holtmann 26640798872eSMarcel Holtmann bacpy(&data->bdaddr, bdaddr); 26656928a924SJohan Hedberg data->bdaddr_type = bdaddr_type; 26660798872eSMarcel Holtmann list_add(&data->list, &hdev->remote_oob_data); 26670798872eSMarcel Holtmann } 26680798872eSMarcel Holtmann 266981328d5cSJohan Hedberg if (hash192 && rand192) { 26700798872eSMarcel Holtmann memcpy(data->hash192, hash192, sizeof(data->hash192)); 267138da1703SJohan Hedberg memcpy(data->rand192, rand192, sizeof(data->rand192)); 2672f7697b16SMarcel Holtmann if (hash256 && rand256) 2673f7697b16SMarcel Holtmann data->present = 0x03; 267481328d5cSJohan Hedberg } else { 267581328d5cSJohan Hedberg memset(data->hash192, 0, sizeof(data->hash192)); 267681328d5cSJohan Hedberg memset(data->rand192, 0, sizeof(data->rand192)); 2677f7697b16SMarcel Holtmann if (hash256 && rand256) 2678f7697b16SMarcel Holtmann data->present = 0x02; 2679f7697b16SMarcel Holtmann else 2680f7697b16SMarcel Holtmann data->present = 0x00; 268181328d5cSJohan Hedberg } 26820798872eSMarcel Holtmann 268381328d5cSJohan Hedberg if (hash256 && rand256) { 26840798872eSMarcel Holtmann memcpy(data->hash256, hash256, sizeof(data->hash256)); 268538da1703SJohan Hedberg memcpy(data->rand256, rand256, sizeof(data->rand256)); 268681328d5cSJohan Hedberg } else { 268781328d5cSJohan Hedberg memset(data->hash256, 0, sizeof(data->hash256)); 268881328d5cSJohan Hedberg memset(data->rand256, 0, sizeof(data->rand256)); 2689f7697b16SMarcel Holtmann if (hash192 && rand192) 2690f7697b16SMarcel Holtmann data->present = 0x01; 269181328d5cSJohan Hedberg } 26920798872eSMarcel Holtmann 26936ed93dc6SAndrei Emeltchenko BT_DBG("%s for %pMR", hdev->name, bdaddr); 26942763eda6SSzymon Janc 26952763eda6SSzymon Janc return 0; 26962763eda6SSzymon Janc } 26972763eda6SSzymon Janc 2698d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2699d2609b34SFlorian Grandel struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance) 2700d2609b34SFlorian Grandel { 2701d2609b34SFlorian Grandel struct adv_info *adv_instance; 2702d2609b34SFlorian Grandel 2703d2609b34SFlorian Grandel list_for_each_entry(adv_instance, &hdev->adv_instances, list) { 2704d2609b34SFlorian Grandel if (adv_instance->instance == instance) 2705d2609b34SFlorian Grandel return adv_instance; 2706d2609b34SFlorian Grandel } 2707d2609b34SFlorian Grandel 2708d2609b34SFlorian Grandel return NULL; 2709d2609b34SFlorian Grandel } 2710d2609b34SFlorian Grandel 2711d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2712d2609b34SFlorian Grandel struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance) { 2713d2609b34SFlorian Grandel struct adv_info *cur_instance; 2714d2609b34SFlorian Grandel 2715d2609b34SFlorian Grandel cur_instance = hci_find_adv_instance(hdev, instance); 2716d2609b34SFlorian Grandel if (!cur_instance) 2717d2609b34SFlorian Grandel return NULL; 2718d2609b34SFlorian Grandel 2719d2609b34SFlorian Grandel if (cur_instance == list_last_entry(&hdev->adv_instances, 2720d2609b34SFlorian Grandel struct adv_info, list)) 2721d2609b34SFlorian Grandel return list_first_entry(&hdev->adv_instances, 2722d2609b34SFlorian Grandel struct adv_info, list); 2723d2609b34SFlorian Grandel else 2724d2609b34SFlorian Grandel return list_next_entry(cur_instance, list); 2725d2609b34SFlorian Grandel } 2726d2609b34SFlorian Grandel 2727d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2728d2609b34SFlorian Grandel int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance) 2729d2609b34SFlorian Grandel { 2730d2609b34SFlorian Grandel struct adv_info *adv_instance; 2731d2609b34SFlorian Grandel 2732d2609b34SFlorian Grandel adv_instance = hci_find_adv_instance(hdev, instance); 2733d2609b34SFlorian Grandel if (!adv_instance) 2734d2609b34SFlorian Grandel return -ENOENT; 2735d2609b34SFlorian Grandel 2736d2609b34SFlorian Grandel BT_DBG("%s removing %dMR", hdev->name, instance); 2737d2609b34SFlorian Grandel 27385d900e46SFlorian Grandel if (hdev->cur_adv_instance == instance && hdev->adv_instance_timeout) { 27395d900e46SFlorian Grandel cancel_delayed_work(&hdev->adv_instance_expire); 27405d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 27415d900e46SFlorian Grandel } 27425d900e46SFlorian Grandel 2743d2609b34SFlorian Grandel list_del(&adv_instance->list); 2744d2609b34SFlorian Grandel kfree(adv_instance); 2745d2609b34SFlorian Grandel 2746d2609b34SFlorian Grandel hdev->adv_instance_cnt--; 2747d2609b34SFlorian Grandel 2748d2609b34SFlorian Grandel return 0; 2749d2609b34SFlorian Grandel } 2750d2609b34SFlorian Grandel 2751d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2752d2609b34SFlorian Grandel void hci_adv_instances_clear(struct hci_dev *hdev) 2753d2609b34SFlorian Grandel { 2754d2609b34SFlorian Grandel struct adv_info *adv_instance, *n; 2755d2609b34SFlorian Grandel 27565d900e46SFlorian Grandel if (hdev->adv_instance_timeout) { 27575d900e46SFlorian Grandel cancel_delayed_work(&hdev->adv_instance_expire); 27585d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 27595d900e46SFlorian Grandel } 27605d900e46SFlorian Grandel 2761d2609b34SFlorian Grandel list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { 2762d2609b34SFlorian Grandel list_del(&adv_instance->list); 2763d2609b34SFlorian Grandel kfree(adv_instance); 2764d2609b34SFlorian Grandel } 2765d2609b34SFlorian Grandel 2766d2609b34SFlorian Grandel hdev->adv_instance_cnt = 0; 2767d2609b34SFlorian Grandel } 2768d2609b34SFlorian Grandel 2769d2609b34SFlorian Grandel /* This function requires the caller holds hdev->lock */ 2770d2609b34SFlorian Grandel int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags, 2771d2609b34SFlorian Grandel u16 adv_data_len, u8 *adv_data, 2772d2609b34SFlorian Grandel u16 scan_rsp_len, u8 *scan_rsp_data, 2773d2609b34SFlorian Grandel u16 timeout, u16 duration) 2774d2609b34SFlorian Grandel { 2775d2609b34SFlorian Grandel struct adv_info *adv_instance; 2776d2609b34SFlorian Grandel 2777d2609b34SFlorian Grandel adv_instance = hci_find_adv_instance(hdev, instance); 2778d2609b34SFlorian Grandel if (adv_instance) { 2779d2609b34SFlorian Grandel memset(adv_instance->adv_data, 0, 2780d2609b34SFlorian Grandel sizeof(adv_instance->adv_data)); 2781d2609b34SFlorian Grandel memset(adv_instance->scan_rsp_data, 0, 2782d2609b34SFlorian Grandel sizeof(adv_instance->scan_rsp_data)); 2783d2609b34SFlorian Grandel } else { 2784d2609b34SFlorian Grandel if (hdev->adv_instance_cnt >= HCI_MAX_ADV_INSTANCES || 2785d2609b34SFlorian Grandel instance < 1 || instance > HCI_MAX_ADV_INSTANCES) 2786d2609b34SFlorian Grandel return -EOVERFLOW; 2787d2609b34SFlorian Grandel 278839ecfad6SJohan Hedberg adv_instance = kzalloc(sizeof(*adv_instance), GFP_KERNEL); 2789d2609b34SFlorian Grandel if (!adv_instance) 2790d2609b34SFlorian Grandel return -ENOMEM; 2791d2609b34SFlorian Grandel 2792fffd38bcSFlorian Grandel adv_instance->pending = true; 2793d2609b34SFlorian Grandel adv_instance->instance = instance; 2794d2609b34SFlorian Grandel list_add(&adv_instance->list, &hdev->adv_instances); 2795d2609b34SFlorian Grandel hdev->adv_instance_cnt++; 2796d2609b34SFlorian Grandel } 2797d2609b34SFlorian Grandel 2798d2609b34SFlorian Grandel adv_instance->flags = flags; 2799d2609b34SFlorian Grandel adv_instance->adv_data_len = adv_data_len; 2800d2609b34SFlorian Grandel adv_instance->scan_rsp_len = scan_rsp_len; 2801d2609b34SFlorian Grandel 2802d2609b34SFlorian Grandel if (adv_data_len) 2803d2609b34SFlorian Grandel memcpy(adv_instance->adv_data, adv_data, adv_data_len); 2804d2609b34SFlorian Grandel 2805d2609b34SFlorian Grandel if (scan_rsp_len) 2806d2609b34SFlorian Grandel memcpy(adv_instance->scan_rsp_data, 2807d2609b34SFlorian Grandel scan_rsp_data, scan_rsp_len); 2808d2609b34SFlorian Grandel 2809d2609b34SFlorian Grandel adv_instance->timeout = timeout; 28105d900e46SFlorian Grandel adv_instance->remaining_time = timeout; 2811d2609b34SFlorian Grandel 2812d2609b34SFlorian Grandel if (duration == 0) 2813d2609b34SFlorian Grandel adv_instance->duration = HCI_DEFAULT_ADV_DURATION; 2814d2609b34SFlorian Grandel else 2815d2609b34SFlorian Grandel adv_instance->duration = duration; 2816d2609b34SFlorian Grandel 2817d2609b34SFlorian Grandel BT_DBG("%s for %dMR", hdev->name, instance); 2818d2609b34SFlorian Grandel 2819d2609b34SFlorian Grandel return 0; 2820d2609b34SFlorian Grandel } 2821d2609b34SFlorian Grandel 2822dcc36c16SJohan Hedberg struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list, 2823b9ee0a78SMarcel Holtmann bdaddr_t *bdaddr, u8 type) 2824b2a66aadSAntti Julku { 2825b2a66aadSAntti Julku struct bdaddr_list *b; 2826b2a66aadSAntti Julku 2827dcc36c16SJohan Hedberg list_for_each_entry(b, bdaddr_list, list) { 2828b9ee0a78SMarcel Holtmann if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type) 2829b2a66aadSAntti Julku return b; 2830b9ee0a78SMarcel Holtmann } 2831b2a66aadSAntti Julku 2832b2a66aadSAntti Julku return NULL; 2833b2a66aadSAntti Julku } 2834b2a66aadSAntti Julku 2835dcc36c16SJohan Hedberg void hci_bdaddr_list_clear(struct list_head *bdaddr_list) 2836b2a66aadSAntti Julku { 2837b2a66aadSAntti Julku struct list_head *p, *n; 2838b2a66aadSAntti Julku 2839dcc36c16SJohan Hedberg list_for_each_safe(p, n, bdaddr_list) { 2840b9ee0a78SMarcel Holtmann struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list); 2841b2a66aadSAntti Julku 2842b2a66aadSAntti Julku list_del(p); 2843b2a66aadSAntti Julku kfree(b); 2844b2a66aadSAntti Julku } 2845b2a66aadSAntti Julku } 2846b2a66aadSAntti Julku 2847dcc36c16SJohan Hedberg int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type) 2848b2a66aadSAntti Julku { 2849b2a66aadSAntti Julku struct bdaddr_list *entry; 2850b2a66aadSAntti Julku 2851b9ee0a78SMarcel Holtmann if (!bacmp(bdaddr, BDADDR_ANY)) 2852b2a66aadSAntti Julku return -EBADF; 2853b2a66aadSAntti Julku 2854dcc36c16SJohan Hedberg if (hci_bdaddr_list_lookup(list, bdaddr, type)) 28555e762444SAntti Julku return -EEXIST; 2856b2a66aadSAntti Julku 285727f70f3eSJohan Hedberg entry = kzalloc(sizeof(*entry), GFP_KERNEL); 28585e762444SAntti Julku if (!entry) 28595e762444SAntti Julku return -ENOMEM; 2860b2a66aadSAntti Julku 2861b2a66aadSAntti Julku bacpy(&entry->bdaddr, bdaddr); 2862b9ee0a78SMarcel Holtmann entry->bdaddr_type = type; 2863b2a66aadSAntti Julku 2864dcc36c16SJohan Hedberg list_add(&entry->list, list); 2865b2a66aadSAntti Julku 28662a8357f2SJohan Hedberg return 0; 2867b2a66aadSAntti Julku } 2868b2a66aadSAntti Julku 2869dcc36c16SJohan Hedberg int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type) 2870b2a66aadSAntti Julku { 2871b2a66aadSAntti Julku struct bdaddr_list *entry; 2872b2a66aadSAntti Julku 287335f7498aSJohan Hedberg if (!bacmp(bdaddr, BDADDR_ANY)) { 2874dcc36c16SJohan Hedberg hci_bdaddr_list_clear(list); 287535f7498aSJohan Hedberg return 0; 287635f7498aSJohan Hedberg } 2877b2a66aadSAntti Julku 2878dcc36c16SJohan Hedberg entry = hci_bdaddr_list_lookup(list, bdaddr, type); 2879d2ab0ac1SMarcel Holtmann if (!entry) 2880d2ab0ac1SMarcel Holtmann return -ENOENT; 2881d2ab0ac1SMarcel Holtmann 2882d2ab0ac1SMarcel Holtmann list_del(&entry->list); 2883d2ab0ac1SMarcel Holtmann kfree(entry); 2884d2ab0ac1SMarcel Holtmann 2885d2ab0ac1SMarcel Holtmann return 0; 2886d2ab0ac1SMarcel Holtmann } 2887d2ab0ac1SMarcel Holtmann 288815819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 288915819a70SAndre Guedes struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, 289015819a70SAndre Guedes bdaddr_t *addr, u8 addr_type) 289115819a70SAndre Guedes { 289215819a70SAndre Guedes struct hci_conn_params *params; 289315819a70SAndre Guedes 289415819a70SAndre Guedes list_for_each_entry(params, &hdev->le_conn_params, list) { 289515819a70SAndre Guedes if (bacmp(¶ms->addr, addr) == 0 && 289615819a70SAndre Guedes params->addr_type == addr_type) { 289715819a70SAndre Guedes return params; 289815819a70SAndre Guedes } 289915819a70SAndre Guedes } 290015819a70SAndre Guedes 290115819a70SAndre Guedes return NULL; 290215819a70SAndre Guedes } 290315819a70SAndre Guedes 290415819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 2905501f8827SJohan Hedberg struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list, 29064b10966fSMarcel Holtmann bdaddr_t *addr, u8 addr_type) 290715819a70SAndre Guedes { 2908912b42efSJohan Hedberg struct hci_conn_params *param; 290915819a70SAndre Guedes 2910501f8827SJohan Hedberg list_for_each_entry(param, list, action) { 2911912b42efSJohan Hedberg if (bacmp(¶m->addr, addr) == 0 && 2912912b42efSJohan Hedberg param->addr_type == addr_type) 2913912b42efSJohan Hedberg return param; 29144b10966fSMarcel Holtmann } 29154b10966fSMarcel Holtmann 29164b10966fSMarcel Holtmann return NULL; 291715819a70SAndre Guedes } 291815819a70SAndre Guedes 291915819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 2920f75113a2SJakub Pawlowski struct hci_conn_params *hci_explicit_connect_lookup(struct hci_dev *hdev, 2921f75113a2SJakub Pawlowski bdaddr_t *addr, 2922f75113a2SJakub Pawlowski u8 addr_type) 2923f75113a2SJakub Pawlowski { 2924f75113a2SJakub Pawlowski struct hci_conn_params *param; 2925f75113a2SJakub Pawlowski 2926f75113a2SJakub Pawlowski list_for_each_entry(param, &hdev->pend_le_conns, action) { 2927f75113a2SJakub Pawlowski if (bacmp(¶m->addr, addr) == 0 && 2928f75113a2SJakub Pawlowski param->addr_type == addr_type && 2929f75113a2SJakub Pawlowski param->explicit_connect) 2930f75113a2SJakub Pawlowski return param; 2931f75113a2SJakub Pawlowski } 2932f75113a2SJakub Pawlowski 2933f75113a2SJakub Pawlowski return NULL; 2934f75113a2SJakub Pawlowski } 2935f75113a2SJakub Pawlowski 2936f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */ 293751d167c0SMarcel Holtmann struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev, 293851d167c0SMarcel Holtmann bdaddr_t *addr, u8 addr_type) 293915819a70SAndre Guedes { 294015819a70SAndre Guedes struct hci_conn_params *params; 294115819a70SAndre Guedes 294215819a70SAndre Guedes params = hci_conn_params_lookup(hdev, addr, addr_type); 2943cef952ceSAndre Guedes if (params) 294451d167c0SMarcel Holtmann return params; 294515819a70SAndre Guedes 294615819a70SAndre Guedes params = kzalloc(sizeof(*params), GFP_KERNEL); 294715819a70SAndre Guedes if (!params) { 294815819a70SAndre Guedes BT_ERR("Out of memory"); 294951d167c0SMarcel Holtmann return NULL; 295015819a70SAndre Guedes } 295115819a70SAndre Guedes 295215819a70SAndre Guedes bacpy(¶ms->addr, addr); 295315819a70SAndre Guedes params->addr_type = addr_type; 2954cef952ceSAndre Guedes 2955cef952ceSAndre Guedes list_add(¶ms->list, &hdev->le_conn_params); 295693450c75SJohan Hedberg INIT_LIST_HEAD(¶ms->action); 2957cef952ceSAndre Guedes 2958bf5b3c8bSMarcel Holtmann params->conn_min_interval = hdev->le_conn_min_interval; 2959bf5b3c8bSMarcel Holtmann params->conn_max_interval = hdev->le_conn_max_interval; 2960bf5b3c8bSMarcel Holtmann params->conn_latency = hdev->le_conn_latency; 2961bf5b3c8bSMarcel Holtmann params->supervision_timeout = hdev->le_supv_timeout; 2962bf5b3c8bSMarcel Holtmann params->auto_connect = HCI_AUTO_CONN_DISABLED; 2963bf5b3c8bSMarcel Holtmann 2964bf5b3c8bSMarcel Holtmann BT_DBG("addr %pMR (type %u)", addr, addr_type); 2965bf5b3c8bSMarcel Holtmann 296651d167c0SMarcel Holtmann return params; 2967bf5b3c8bSMarcel Holtmann } 2968bf5b3c8bSMarcel Holtmann 2969f6c63249SJohan Hedberg static void hci_conn_params_free(struct hci_conn_params *params) 2970f6c63249SJohan Hedberg { 2971f6c63249SJohan Hedberg if (params->conn) { 2972f6c63249SJohan Hedberg hci_conn_drop(params->conn); 2973f6c63249SJohan Hedberg hci_conn_put(params->conn); 2974f6c63249SJohan Hedberg } 2975f6c63249SJohan Hedberg 2976f6c63249SJohan Hedberg list_del(¶ms->action); 2977f6c63249SJohan Hedberg list_del(¶ms->list); 2978f6c63249SJohan Hedberg kfree(params); 2979f6c63249SJohan Hedberg } 2980f6c63249SJohan Hedberg 298115819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 298215819a70SAndre Guedes void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type) 298315819a70SAndre Guedes { 298415819a70SAndre Guedes struct hci_conn_params *params; 298515819a70SAndre Guedes 298615819a70SAndre Guedes params = hci_conn_params_lookup(hdev, addr, addr_type); 298715819a70SAndre Guedes if (!params) 298815819a70SAndre Guedes return; 298915819a70SAndre Guedes 2990f6c63249SJohan Hedberg hci_conn_params_free(params); 299115819a70SAndre Guedes 299295305baaSJohan Hedberg hci_update_background_scan(hdev); 299395305baaSJohan Hedberg 299415819a70SAndre Guedes BT_DBG("addr %pMR (type %u)", addr, addr_type); 299515819a70SAndre Guedes } 299615819a70SAndre Guedes 299715819a70SAndre Guedes /* This function requires the caller holds hdev->lock */ 299855af49a8SJohan Hedberg void hci_conn_params_clear_disabled(struct hci_dev *hdev) 299915819a70SAndre Guedes { 300015819a70SAndre Guedes struct hci_conn_params *params, *tmp; 300115819a70SAndre Guedes 300215819a70SAndre Guedes list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) { 300355af49a8SJohan Hedberg if (params->auto_connect != HCI_AUTO_CONN_DISABLED) 300455af49a8SJohan Hedberg continue; 3005f75113a2SJakub Pawlowski 3006f75113a2SJakub Pawlowski /* If trying to estabilish one time connection to disabled 3007f75113a2SJakub Pawlowski * device, leave the params, but mark them as just once. 3008f75113a2SJakub Pawlowski */ 3009f75113a2SJakub Pawlowski if (params->explicit_connect) { 3010f75113a2SJakub Pawlowski params->auto_connect = HCI_AUTO_CONN_EXPLICIT; 3011f75113a2SJakub Pawlowski continue; 3012f75113a2SJakub Pawlowski } 3013f75113a2SJakub Pawlowski 301415819a70SAndre Guedes list_del(¶ms->list); 301515819a70SAndre Guedes kfree(params); 301615819a70SAndre Guedes } 301715819a70SAndre Guedes 301855af49a8SJohan Hedberg BT_DBG("All LE disabled connection parameters were removed"); 301955af49a8SJohan Hedberg } 302055af49a8SJohan Hedberg 302155af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */ 3022373110c5SJohan Hedberg void hci_conn_params_clear_all(struct hci_dev *hdev) 302315819a70SAndre Guedes { 302415819a70SAndre Guedes struct hci_conn_params *params, *tmp; 302515819a70SAndre Guedes 3026f6c63249SJohan Hedberg list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) 3027f6c63249SJohan Hedberg hci_conn_params_free(params); 302815819a70SAndre Guedes 3029a2f41a8fSJohan Hedberg hci_update_background_scan(hdev); 30301089b67dSMarcel Holtmann 303115819a70SAndre Guedes BT_DBG("All LE connection parameters were removed"); 303215819a70SAndre Guedes } 303315819a70SAndre Guedes 30341904a853SMarcel Holtmann static void inquiry_complete(struct hci_dev *hdev, u8 status, u16 opcode) 30357ba8b4beSAndre Guedes { 30364c87eaabSAndre Guedes if (status) { 30374c87eaabSAndre Guedes BT_ERR("Failed to start inquiry: status %d", status); 30387ba8b4beSAndre Guedes 30394c87eaabSAndre Guedes hci_dev_lock(hdev); 30404c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 30414c87eaabSAndre Guedes hci_dev_unlock(hdev); 30424c87eaabSAndre Guedes return; 30434c87eaabSAndre Guedes } 30447ba8b4beSAndre Guedes } 30457ba8b4beSAndre Guedes 30461904a853SMarcel Holtmann static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status, 30471904a853SMarcel Holtmann u16 opcode) 30487ba8b4beSAndre Guedes { 30494c87eaabSAndre Guedes /* General inquiry access code (GIAC) */ 30504c87eaabSAndre Guedes u8 lap[3] = { 0x33, 0x8b, 0x9e }; 30514c87eaabSAndre Guedes struct hci_cp_inquiry cp; 30527ba8b4beSAndre Guedes int err; 30537ba8b4beSAndre Guedes 30544c87eaabSAndre Guedes if (status) { 30554c87eaabSAndre Guedes BT_ERR("Failed to disable LE scanning: status %d", status); 30564c87eaabSAndre Guedes return; 30577ba8b4beSAndre Guedes } 30587ba8b4beSAndre Guedes 30592d28cfe7SJakub Pawlowski hdev->discovery.scan_start = 0; 30602d28cfe7SJakub Pawlowski 30614c87eaabSAndre Guedes switch (hdev->discovery.type) { 30624c87eaabSAndre Guedes case DISCOV_TYPE_LE: 30634c87eaabSAndre Guedes hci_dev_lock(hdev); 30644c87eaabSAndre Guedes hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 30654c87eaabSAndre Guedes hci_dev_unlock(hdev); 30664c87eaabSAndre Guedes break; 30677dbfac1dSAndre Guedes 30684c87eaabSAndre Guedes case DISCOV_TYPE_INTERLEAVED: 30694c87eaabSAndre Guedes hci_dev_lock(hdev); 30704c87eaabSAndre Guedes 307107d2334aSJakub Pawlowski if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, 307207d2334aSJakub Pawlowski &hdev->quirks)) { 307307d2334aSJakub Pawlowski /* If we were running LE only scan, change discovery 307407d2334aSJakub Pawlowski * state. If we were running both LE and BR/EDR inquiry 307507d2334aSJakub Pawlowski * simultaneously, and BR/EDR inquiry is already 307607d2334aSJakub Pawlowski * finished, stop discovery, otherwise BR/EDR inquiry 3077177d0506SWesley Kuo * will stop discovery when finished. If we will resolve 3078177d0506SWesley Kuo * remote device name, do not change discovery state. 307907d2334aSJakub Pawlowski */ 3080177d0506SWesley Kuo if (!test_bit(HCI_INQUIRY, &hdev->flags) && 3081177d0506SWesley Kuo hdev->discovery.state != DISCOVERY_RESOLVING) 308207d2334aSJakub Pawlowski hci_discovery_set_state(hdev, 308307d2334aSJakub Pawlowski DISCOVERY_STOPPED); 308407d2334aSJakub Pawlowski } else { 3085baf880a9SJohan Hedberg struct hci_request req; 3086baf880a9SJohan Hedberg 30874c87eaabSAndre Guedes hci_inquiry_cache_flush(hdev); 30884c87eaabSAndre Guedes 3089baf880a9SJohan Hedberg hci_req_init(&req, hdev); 3090baf880a9SJohan Hedberg 3091baf880a9SJohan Hedberg memset(&cp, 0, sizeof(cp)); 3092baf880a9SJohan Hedberg memcpy(&cp.lap, lap, sizeof(cp.lap)); 3093baf880a9SJohan Hedberg cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; 3094baf880a9SJohan Hedberg hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); 3095baf880a9SJohan Hedberg 30964c87eaabSAndre Guedes err = hci_req_run(&req, inquiry_complete); 30974c87eaabSAndre Guedes if (err) { 30984c87eaabSAndre Guedes BT_ERR("Inquiry request failed: err %d", err); 309907d2334aSJakub Pawlowski hci_discovery_set_state(hdev, 310007d2334aSJakub Pawlowski DISCOVERY_STOPPED); 310107d2334aSJakub Pawlowski } 31027dbfac1dSAndre Guedes } 31037dbfac1dSAndre Guedes 31044c87eaabSAndre Guedes hci_dev_unlock(hdev); 31054c87eaabSAndre Guedes break; 31064c87eaabSAndre Guedes } 31077dbfac1dSAndre Guedes } 31087dbfac1dSAndre Guedes 31097ba8b4beSAndre Guedes static void le_scan_disable_work(struct work_struct *work) 31107ba8b4beSAndre Guedes { 31117ba8b4beSAndre Guedes struct hci_dev *hdev = container_of(work, struct hci_dev, 31127ba8b4beSAndre Guedes le_scan_disable.work); 31134c87eaabSAndre Guedes struct hci_request req; 31144c87eaabSAndre Guedes int err; 31157ba8b4beSAndre Guedes 31167ba8b4beSAndre Guedes BT_DBG("%s", hdev->name); 31177ba8b4beSAndre Guedes 31182d28cfe7SJakub Pawlowski cancel_delayed_work_sync(&hdev->le_scan_restart); 31192d28cfe7SJakub Pawlowski 31204c87eaabSAndre Guedes hci_req_init(&req, hdev); 31217ba8b4beSAndre Guedes 3122b1efcc28SAndre Guedes hci_req_add_le_scan_disable(&req); 31237ba8b4beSAndre Guedes 31244c87eaabSAndre Guedes err = hci_req_run(&req, le_scan_disable_work_complete); 31254c87eaabSAndre Guedes if (err) 31264c87eaabSAndre Guedes BT_ERR("Disable LE scanning request failed: err %d", err); 312728b75a89SAndre Guedes } 312828b75a89SAndre Guedes 31292d28cfe7SJakub Pawlowski static void le_scan_restart_work_complete(struct hci_dev *hdev, u8 status, 31302d28cfe7SJakub Pawlowski u16 opcode) 31312d28cfe7SJakub Pawlowski { 31322d28cfe7SJakub Pawlowski unsigned long timeout, duration, scan_start, now; 31332d28cfe7SJakub Pawlowski 31342d28cfe7SJakub Pawlowski BT_DBG("%s", hdev->name); 31352d28cfe7SJakub Pawlowski 31362d28cfe7SJakub Pawlowski if (status) { 31372d28cfe7SJakub Pawlowski BT_ERR("Failed to restart LE scan: status %d", status); 31382d28cfe7SJakub Pawlowski return; 31392d28cfe7SJakub Pawlowski } 31402d28cfe7SJakub Pawlowski 31412d28cfe7SJakub Pawlowski if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) || 31422d28cfe7SJakub Pawlowski !hdev->discovery.scan_start) 31432d28cfe7SJakub Pawlowski return; 31442d28cfe7SJakub Pawlowski 31452d28cfe7SJakub Pawlowski /* When the scan was started, hdev->le_scan_disable has been queued 31462d28cfe7SJakub Pawlowski * after duration from scan_start. During scan restart this job 31472d28cfe7SJakub Pawlowski * has been canceled, and we need to queue it again after proper 31482d28cfe7SJakub Pawlowski * timeout, to make sure that scan does not run indefinitely. 31492d28cfe7SJakub Pawlowski */ 31502d28cfe7SJakub Pawlowski duration = hdev->discovery.scan_duration; 31512d28cfe7SJakub Pawlowski scan_start = hdev->discovery.scan_start; 31522d28cfe7SJakub Pawlowski now = jiffies; 31532d28cfe7SJakub Pawlowski if (now - scan_start <= duration) { 31542d28cfe7SJakub Pawlowski int elapsed; 31552d28cfe7SJakub Pawlowski 31562d28cfe7SJakub Pawlowski if (now >= scan_start) 31572d28cfe7SJakub Pawlowski elapsed = now - scan_start; 31582d28cfe7SJakub Pawlowski else 31592d28cfe7SJakub Pawlowski elapsed = ULONG_MAX - scan_start + now; 31602d28cfe7SJakub Pawlowski 31612d28cfe7SJakub Pawlowski timeout = duration - elapsed; 31622d28cfe7SJakub Pawlowski } else { 31632d28cfe7SJakub Pawlowski timeout = 0; 31642d28cfe7SJakub Pawlowski } 31652d28cfe7SJakub Pawlowski queue_delayed_work(hdev->workqueue, 31662d28cfe7SJakub Pawlowski &hdev->le_scan_disable, timeout); 31672d28cfe7SJakub Pawlowski } 31682d28cfe7SJakub Pawlowski 31692d28cfe7SJakub Pawlowski static void le_scan_restart_work(struct work_struct *work) 31702d28cfe7SJakub Pawlowski { 31712d28cfe7SJakub Pawlowski struct hci_dev *hdev = container_of(work, struct hci_dev, 31722d28cfe7SJakub Pawlowski le_scan_restart.work); 31732d28cfe7SJakub Pawlowski struct hci_request req; 31742d28cfe7SJakub Pawlowski struct hci_cp_le_set_scan_enable cp; 31752d28cfe7SJakub Pawlowski int err; 31762d28cfe7SJakub Pawlowski 31772d28cfe7SJakub Pawlowski BT_DBG("%s", hdev->name); 31782d28cfe7SJakub Pawlowski 31792d28cfe7SJakub Pawlowski /* If controller is not scanning we are done. */ 3180d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) 31812d28cfe7SJakub Pawlowski return; 31822d28cfe7SJakub Pawlowski 31832d28cfe7SJakub Pawlowski hci_req_init(&req, hdev); 31842d28cfe7SJakub Pawlowski 31852d28cfe7SJakub Pawlowski hci_req_add_le_scan_disable(&req); 31862d28cfe7SJakub Pawlowski 31872d28cfe7SJakub Pawlowski memset(&cp, 0, sizeof(cp)); 31882d28cfe7SJakub Pawlowski cp.enable = LE_SCAN_ENABLE; 31892d28cfe7SJakub Pawlowski cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; 31902d28cfe7SJakub Pawlowski hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); 31912d28cfe7SJakub Pawlowski 31922d28cfe7SJakub Pawlowski err = hci_req_run(&req, le_scan_restart_work_complete); 31932d28cfe7SJakub Pawlowski if (err) 31942d28cfe7SJakub Pawlowski BT_ERR("Restart LE scan request failed: err %d", err); 31952d28cfe7SJakub Pawlowski } 31962d28cfe7SJakub Pawlowski 3197a1f4c318SJohan Hedberg /* Copy the Identity Address of the controller. 3198a1f4c318SJohan Hedberg * 3199a1f4c318SJohan Hedberg * If the controller has a public BD_ADDR, then by default use that one. 3200a1f4c318SJohan Hedberg * If this is a LE only controller without a public address, default to 3201a1f4c318SJohan Hedberg * the static random address. 3202a1f4c318SJohan Hedberg * 3203a1f4c318SJohan Hedberg * For debugging purposes it is possible to force controllers with a 3204a1f4c318SJohan Hedberg * public address to use the static random address instead. 320550b5b952SMarcel Holtmann * 320650b5b952SMarcel Holtmann * In case BR/EDR has been disabled on a dual-mode controller and 320750b5b952SMarcel Holtmann * userspace has configured a static address, then that address 320850b5b952SMarcel Holtmann * becomes the identity address instead of the public BR/EDR address. 3209a1f4c318SJohan Hedberg */ 3210a1f4c318SJohan Hedberg void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, 3211a1f4c318SJohan Hedberg u8 *bdaddr_type) 3212a1f4c318SJohan Hedberg { 3213b7cb93e5SMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 321450b5b952SMarcel Holtmann !bacmp(&hdev->bdaddr, BDADDR_ANY) || 3215d7a5a11dSMarcel Holtmann (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 321650b5b952SMarcel Holtmann bacmp(&hdev->static_addr, BDADDR_ANY))) { 3217a1f4c318SJohan Hedberg bacpy(bdaddr, &hdev->static_addr); 3218a1f4c318SJohan Hedberg *bdaddr_type = ADDR_LE_DEV_RANDOM; 3219a1f4c318SJohan Hedberg } else { 3220a1f4c318SJohan Hedberg bacpy(bdaddr, &hdev->bdaddr); 3221a1f4c318SJohan Hedberg *bdaddr_type = ADDR_LE_DEV_PUBLIC; 3222a1f4c318SJohan Hedberg } 3223a1f4c318SJohan Hedberg } 3224a1f4c318SJohan Hedberg 32259be0dab7SDavid Herrmann /* Alloc HCI device */ 32269be0dab7SDavid Herrmann struct hci_dev *hci_alloc_dev(void) 32279be0dab7SDavid Herrmann { 32289be0dab7SDavid Herrmann struct hci_dev *hdev; 32299be0dab7SDavid Herrmann 323027f70f3eSJohan Hedberg hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); 32319be0dab7SDavid Herrmann if (!hdev) 32329be0dab7SDavid Herrmann return NULL; 32339be0dab7SDavid Herrmann 3234b1b813d4SDavid Herrmann hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); 3235b1b813d4SDavid Herrmann hdev->esco_type = (ESCO_HV1); 3236b1b813d4SDavid Herrmann hdev->link_mode = (HCI_LM_ACCEPT); 3237b4cb9fb2SMarcel Holtmann hdev->num_iac = 0x01; /* One IAC support is mandatory */ 3238b1b813d4SDavid Herrmann hdev->io_capability = 0x03; /* No Input No Output */ 323996c2103aSMarcel Holtmann hdev->manufacturer = 0xffff; /* Default to internal use */ 3240bbaf444aSJohan Hedberg hdev->inq_tx_power = HCI_TX_POWER_INVALID; 3241bbaf444aSJohan Hedberg hdev->adv_tx_power = HCI_TX_POWER_INVALID; 3242d2609b34SFlorian Grandel hdev->adv_instance_cnt = 0; 3243d2609b34SFlorian Grandel hdev->cur_adv_instance = 0x00; 32445d900e46SFlorian Grandel hdev->adv_instance_timeout = 0; 3245b1b813d4SDavid Herrmann 3246b1b813d4SDavid Herrmann hdev->sniff_max_interval = 800; 3247b1b813d4SDavid Herrmann hdev->sniff_min_interval = 80; 3248b1b813d4SDavid Herrmann 32493f959d46SMarcel Holtmann hdev->le_adv_channel_map = 0x07; 3250628531c9SGeorg Lukas hdev->le_adv_min_interval = 0x0800; 3251628531c9SGeorg Lukas hdev->le_adv_max_interval = 0x0800; 3252bef64738SMarcel Holtmann hdev->le_scan_interval = 0x0060; 3253bef64738SMarcel Holtmann hdev->le_scan_window = 0x0030; 32544e70c7e7SMarcel Holtmann hdev->le_conn_min_interval = 0x0028; 32554e70c7e7SMarcel Holtmann hdev->le_conn_max_interval = 0x0038; 325604fb7d90SMarcel Holtmann hdev->le_conn_latency = 0x0000; 325704fb7d90SMarcel Holtmann hdev->le_supv_timeout = 0x002a; 3258a8e1bfaaSMarcel Holtmann hdev->le_def_tx_len = 0x001b; 3259a8e1bfaaSMarcel Holtmann hdev->le_def_tx_time = 0x0148; 3260a8e1bfaaSMarcel Holtmann hdev->le_max_tx_len = 0x001b; 3261a8e1bfaaSMarcel Holtmann hdev->le_max_tx_time = 0x0148; 3262a8e1bfaaSMarcel Holtmann hdev->le_max_rx_len = 0x001b; 3263a8e1bfaaSMarcel Holtmann hdev->le_max_rx_time = 0x0148; 3264bef64738SMarcel Holtmann 3265d6bfd59cSJohan Hedberg hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT; 3266b9a7a61eSLukasz Rymanowski hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT; 326731ad1691SAndrzej Kaczmarek hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE; 326831ad1691SAndrzej Kaczmarek hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE; 3269d6bfd59cSJohan Hedberg 3270b1b813d4SDavid Herrmann mutex_init(&hdev->lock); 3271b1b813d4SDavid Herrmann mutex_init(&hdev->req_lock); 3272b1b813d4SDavid Herrmann 3273b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->mgmt_pending); 3274b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->blacklist); 32756659358eSJohan Hedberg INIT_LIST_HEAD(&hdev->whitelist); 3276b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->uuids); 3277b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->link_keys); 3278b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->long_term_keys); 3279970c4e46SJohan Hedberg INIT_LIST_HEAD(&hdev->identity_resolving_keys); 3280b1b813d4SDavid Herrmann INIT_LIST_HEAD(&hdev->remote_oob_data); 3281d2ab0ac1SMarcel Holtmann INIT_LIST_HEAD(&hdev->le_white_list); 328215819a70SAndre Guedes INIT_LIST_HEAD(&hdev->le_conn_params); 328377a77a30SAndre Guedes INIT_LIST_HEAD(&hdev->pend_le_conns); 328466f8455aSJohan Hedberg INIT_LIST_HEAD(&hdev->pend_le_reports); 32856b536b5eSAndrei Emeltchenko INIT_LIST_HEAD(&hdev->conn_hash.list); 3286d2609b34SFlorian Grandel INIT_LIST_HEAD(&hdev->adv_instances); 3287b1b813d4SDavid Herrmann 3288b1b813d4SDavid Herrmann INIT_WORK(&hdev->rx_work, hci_rx_work); 3289b1b813d4SDavid Herrmann INIT_WORK(&hdev->cmd_work, hci_cmd_work); 3290b1b813d4SDavid Herrmann INIT_WORK(&hdev->tx_work, hci_tx_work); 3291b1b813d4SDavid Herrmann INIT_WORK(&hdev->power_on, hci_power_on); 3292c7741d16SMarcel Holtmann INIT_WORK(&hdev->error_reset, hci_error_reset); 3293b1b813d4SDavid Herrmann 3294b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); 3295b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off); 3296b1b813d4SDavid Herrmann INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); 32972d28cfe7SJakub Pawlowski INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work); 32985d900e46SFlorian Grandel INIT_DELAYED_WORK(&hdev->adv_instance_expire, hci_adv_timeout_expire); 3299b1b813d4SDavid Herrmann 3300b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->rx_q); 3301b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->cmd_q); 3302b1b813d4SDavid Herrmann skb_queue_head_init(&hdev->raw_q); 3303b1b813d4SDavid Herrmann 3304b1b813d4SDavid Herrmann init_waitqueue_head(&hdev->req_wait_q); 3305b1b813d4SDavid Herrmann 330665cc2b49SMarcel Holtmann INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout); 3307b1b813d4SDavid Herrmann 3308b1b813d4SDavid Herrmann hci_init_sysfs(hdev); 3309b1b813d4SDavid Herrmann discovery_init(hdev); 33109be0dab7SDavid Herrmann 33119be0dab7SDavid Herrmann return hdev; 33129be0dab7SDavid Herrmann } 33139be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_alloc_dev); 33149be0dab7SDavid Herrmann 33159be0dab7SDavid Herrmann /* Free HCI device */ 33169be0dab7SDavid Herrmann void hci_free_dev(struct hci_dev *hdev) 33179be0dab7SDavid Herrmann { 33189be0dab7SDavid Herrmann /* will free via device release */ 33199be0dab7SDavid Herrmann put_device(&hdev->dev); 33209be0dab7SDavid Herrmann } 33219be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_free_dev); 33229be0dab7SDavid Herrmann 33231da177e4SLinus Torvalds /* Register HCI device */ 33241da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev) 33251da177e4SLinus Torvalds { 3326b1b813d4SDavid Herrmann int id, error; 33271da177e4SLinus Torvalds 332874292d5aSMarcel Holtmann if (!hdev->open || !hdev->close || !hdev->send) 33291da177e4SLinus Torvalds return -EINVAL; 33301da177e4SLinus Torvalds 333108add513SMat Martineau /* Do not allow HCI_AMP devices to register at index 0, 333208add513SMat Martineau * so the index can be used as the AMP controller ID. 333308add513SMat Martineau */ 33343df92b31SSasha Levin switch (hdev->dev_type) { 33353df92b31SSasha Levin case HCI_BREDR: 33363df92b31SSasha Levin id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL); 33371da177e4SLinus Torvalds break; 33383df92b31SSasha Levin case HCI_AMP: 33393df92b31SSasha Levin id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL); 33403df92b31SSasha Levin break; 33413df92b31SSasha Levin default: 33423df92b31SSasha Levin return -EINVAL; 33431da177e4SLinus Torvalds } 33441da177e4SLinus Torvalds 33453df92b31SSasha Levin if (id < 0) 33463df92b31SSasha Levin return id; 33473df92b31SSasha Levin 33481da177e4SLinus Torvalds sprintf(hdev->name, "hci%d", id); 33491da177e4SLinus Torvalds hdev->id = id; 33502d8b3a11SAndrei Emeltchenko 33512d8b3a11SAndrei Emeltchenko BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 33522d8b3a11SAndrei Emeltchenko 3353d8537548SKees Cook hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | 3354d8537548SKees Cook WQ_MEM_RECLAIM, 1, hdev->name); 335533ca954dSDavid Herrmann if (!hdev->workqueue) { 335633ca954dSDavid Herrmann error = -ENOMEM; 335733ca954dSDavid Herrmann goto err; 335833ca954dSDavid Herrmann } 3359f48fd9c8SMarcel Holtmann 3360d8537548SKees Cook hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND | 3361d8537548SKees Cook WQ_MEM_RECLAIM, 1, hdev->name); 33626ead1bbcSJohan Hedberg if (!hdev->req_workqueue) { 33636ead1bbcSJohan Hedberg destroy_workqueue(hdev->workqueue); 33646ead1bbcSJohan Hedberg error = -ENOMEM; 33656ead1bbcSJohan Hedberg goto err; 33666ead1bbcSJohan Hedberg } 33676ead1bbcSJohan Hedberg 33680153e2ecSMarcel Holtmann if (!IS_ERR_OR_NULL(bt_debugfs)) 33690153e2ecSMarcel Holtmann hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs); 33700153e2ecSMarcel Holtmann 3371bdc3e0f1SMarcel Holtmann dev_set_name(&hdev->dev, "%s", hdev->name); 3372bdc3e0f1SMarcel Holtmann 3373bdc3e0f1SMarcel Holtmann error = device_add(&hdev->dev); 337433ca954dSDavid Herrmann if (error < 0) 337554506918SJohan Hedberg goto err_wqueue; 33761da177e4SLinus Torvalds 3377611b30f7SMarcel Holtmann hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, 3378a8c5fb1aSGustavo Padovan RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, 3379a8c5fb1aSGustavo Padovan hdev); 3380611b30f7SMarcel Holtmann if (hdev->rfkill) { 3381611b30f7SMarcel Holtmann if (rfkill_register(hdev->rfkill) < 0) { 3382611b30f7SMarcel Holtmann rfkill_destroy(hdev->rfkill); 3383611b30f7SMarcel Holtmann hdev->rfkill = NULL; 3384611b30f7SMarcel Holtmann } 3385611b30f7SMarcel Holtmann } 3386611b30f7SMarcel Holtmann 33875e130367SJohan Hedberg if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) 3388a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_RFKILLED); 33895e130367SJohan Hedberg 3390a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_SETUP); 3391a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_AUTO_OFF); 3392ce2be9acSAndrei Emeltchenko 339301cd3404SMarcel Holtmann if (hdev->dev_type == HCI_BREDR) { 339456f87901SJohan Hedberg /* Assume BR/EDR support until proven otherwise (such as 339556f87901SJohan Hedberg * through reading supported features during init. 339656f87901SJohan Hedberg */ 3397a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); 339856f87901SJohan Hedberg } 3399ce2be9acSAndrei Emeltchenko 3400fcee3377SGustavo Padovan write_lock(&hci_dev_list_lock); 3401fcee3377SGustavo Padovan list_add(&hdev->list, &hci_dev_list); 3402fcee3377SGustavo Padovan write_unlock(&hci_dev_list_lock); 3403fcee3377SGustavo Padovan 34044a964404SMarcel Holtmann /* Devices that are marked for raw-only usage are unconfigured 34054a964404SMarcel Holtmann * and should not be included in normal operation. 3406fee746b0SMarcel Holtmann */ 3407fee746b0SMarcel Holtmann if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 3408a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_UNCONFIGURED); 3409fee746b0SMarcel Holtmann 34101da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_REG); 3411dc946bd8SDavid Herrmann hci_dev_hold(hdev); 34121da177e4SLinus Torvalds 341319202573SJohan Hedberg queue_work(hdev->req_workqueue, &hdev->power_on); 3414fbe96d6fSMarcel Holtmann 34151da177e4SLinus Torvalds return id; 3416f48fd9c8SMarcel Holtmann 341733ca954dSDavid Herrmann err_wqueue: 341833ca954dSDavid Herrmann destroy_workqueue(hdev->workqueue); 34196ead1bbcSJohan Hedberg destroy_workqueue(hdev->req_workqueue); 342033ca954dSDavid Herrmann err: 34213df92b31SSasha Levin ida_simple_remove(&hci_index_ida, hdev->id); 3422f48fd9c8SMarcel Holtmann 342333ca954dSDavid Herrmann return error; 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_dev); 34261da177e4SLinus Torvalds 34271da177e4SLinus Torvalds /* Unregister HCI device */ 342859735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev) 34291da177e4SLinus Torvalds { 34302d7cc19eSMarcel Holtmann int id; 3431ef222013SMarcel Holtmann 3432c13854ceSMarcel Holtmann BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 34331da177e4SLinus Torvalds 3434a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_UNREGISTER); 343594324962SJohan Hovold 34363df92b31SSasha Levin id = hdev->id; 34373df92b31SSasha Levin 3438f20d09d5SGustavo F. Padovan write_lock(&hci_dev_list_lock); 34391da177e4SLinus Torvalds list_del(&hdev->list); 3440f20d09d5SGustavo F. Padovan write_unlock(&hci_dev_list_lock); 34411da177e4SLinus Torvalds 34421da177e4SLinus Torvalds hci_dev_do_close(hdev); 34431da177e4SLinus Torvalds 3444b9b5ef18SGustavo Padovan cancel_work_sync(&hdev->power_on); 3445b9b5ef18SGustavo Padovan 3446ab81cbf9SJohan Hedberg if (!test_bit(HCI_INIT, &hdev->flags) && 3447d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_SETUP) && 3448d7a5a11dSMarcel Holtmann !hci_dev_test_flag(hdev, HCI_CONFIG)) { 344909fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 3450744cf19eSJohan Hedberg mgmt_index_removed(hdev); 345109fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 345256e5cb86SJohan Hedberg } 3453ab81cbf9SJohan Hedberg 34542e58ef3eSJohan Hedberg /* mgmt_index_removed should take care of emptying the 34552e58ef3eSJohan Hedberg * pending list */ 34562e58ef3eSJohan Hedberg BUG_ON(!list_empty(&hdev->mgmt_pending)); 34572e58ef3eSJohan Hedberg 34581da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_UNREG); 34591da177e4SLinus Torvalds 3460611b30f7SMarcel Holtmann if (hdev->rfkill) { 3461611b30f7SMarcel Holtmann rfkill_unregister(hdev->rfkill); 3462611b30f7SMarcel Holtmann rfkill_destroy(hdev->rfkill); 3463611b30f7SMarcel Holtmann } 3464611b30f7SMarcel Holtmann 3465bdc3e0f1SMarcel Holtmann device_del(&hdev->dev); 3466147e2d59SDave Young 34670153e2ecSMarcel Holtmann debugfs_remove_recursive(hdev->debugfs); 34680153e2ecSMarcel Holtmann 3469f48fd9c8SMarcel Holtmann destroy_workqueue(hdev->workqueue); 34706ead1bbcSJohan Hedberg destroy_workqueue(hdev->req_workqueue); 3471f48fd9c8SMarcel Holtmann 347209fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 3473dcc36c16SJohan Hedberg hci_bdaddr_list_clear(&hdev->blacklist); 34746659358eSJohan Hedberg hci_bdaddr_list_clear(&hdev->whitelist); 34752aeb9a1aSJohan Hedberg hci_uuids_clear(hdev); 347655ed8ca1SJohan Hedberg hci_link_keys_clear(hdev); 3477b899efafSVinicius Costa Gomes hci_smp_ltks_clear(hdev); 3478970c4e46SJohan Hedberg hci_smp_irks_clear(hdev); 34792763eda6SSzymon Janc hci_remote_oob_data_clear(hdev); 3480d2609b34SFlorian Grandel hci_adv_instances_clear(hdev); 3481dcc36c16SJohan Hedberg hci_bdaddr_list_clear(&hdev->le_white_list); 3482373110c5SJohan Hedberg hci_conn_params_clear_all(hdev); 348322078800SMarcel Holtmann hci_discovery_filter_clear(hdev); 348409fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 3485e2e0cacbSJohan Hedberg 3486dc946bd8SDavid Herrmann hci_dev_put(hdev); 34873df92b31SSasha Levin 34883df92b31SSasha Levin ida_simple_remove(&hci_index_ida, id); 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_dev); 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds /* Suspend HCI device */ 34931da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev) 34941da177e4SLinus Torvalds { 34951da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_SUSPEND); 34961da177e4SLinus Torvalds return 0; 34971da177e4SLinus Torvalds } 34981da177e4SLinus Torvalds EXPORT_SYMBOL(hci_suspend_dev); 34991da177e4SLinus Torvalds 35001da177e4SLinus Torvalds /* Resume HCI device */ 35011da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev) 35021da177e4SLinus Torvalds { 35031da177e4SLinus Torvalds hci_notify(hdev, HCI_DEV_RESUME); 35041da177e4SLinus Torvalds return 0; 35051da177e4SLinus Torvalds } 35061da177e4SLinus Torvalds EXPORT_SYMBOL(hci_resume_dev); 35071da177e4SLinus Torvalds 350875e0569fSMarcel Holtmann /* Reset HCI device */ 350975e0569fSMarcel Holtmann int hci_reset_dev(struct hci_dev *hdev) 351075e0569fSMarcel Holtmann { 351175e0569fSMarcel Holtmann const u8 hw_err[] = { HCI_EV_HARDWARE_ERROR, 0x01, 0x00 }; 351275e0569fSMarcel Holtmann struct sk_buff *skb; 351375e0569fSMarcel Holtmann 351475e0569fSMarcel Holtmann skb = bt_skb_alloc(3, GFP_ATOMIC); 351575e0569fSMarcel Holtmann if (!skb) 351675e0569fSMarcel Holtmann return -ENOMEM; 351775e0569fSMarcel Holtmann 351875e0569fSMarcel Holtmann bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 351975e0569fSMarcel Holtmann memcpy(skb_put(skb, 3), hw_err, 3); 352075e0569fSMarcel Holtmann 352175e0569fSMarcel Holtmann /* Send Hardware Error to upper stack */ 352275e0569fSMarcel Holtmann return hci_recv_frame(hdev, skb); 352375e0569fSMarcel Holtmann } 352475e0569fSMarcel Holtmann EXPORT_SYMBOL(hci_reset_dev); 352575e0569fSMarcel Holtmann 352676bca880SMarcel Holtmann /* Receive frame from HCI drivers */ 3527e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb) 352876bca880SMarcel Holtmann { 352976bca880SMarcel Holtmann if (!hdev || (!test_bit(HCI_UP, &hdev->flags) 353076bca880SMarcel Holtmann && !test_bit(HCI_INIT, &hdev->flags))) { 353176bca880SMarcel Holtmann kfree_skb(skb); 353276bca880SMarcel Holtmann return -ENXIO; 353376bca880SMarcel Holtmann } 353476bca880SMarcel Holtmann 3535fe806dceSMarcel Holtmann if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT && 3536fe806dceSMarcel Holtmann bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT && 3537fe806dceSMarcel Holtmann bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) { 3538fe806dceSMarcel Holtmann kfree_skb(skb); 3539fe806dceSMarcel Holtmann return -EINVAL; 3540fe806dceSMarcel Holtmann } 3541fe806dceSMarcel Holtmann 3542d82603c6SJorrit Schippers /* Incoming skb */ 354376bca880SMarcel Holtmann bt_cb(skb)->incoming = 1; 354476bca880SMarcel Holtmann 354576bca880SMarcel Holtmann /* Time stamp */ 354676bca880SMarcel Holtmann __net_timestamp(skb); 354776bca880SMarcel Holtmann 354876bca880SMarcel Holtmann skb_queue_tail(&hdev->rx_q, skb); 3549b78752ccSMarcel Holtmann queue_work(hdev->workqueue, &hdev->rx_work); 3550c78ae283SMarcel Holtmann 355176bca880SMarcel Holtmann return 0; 355276bca880SMarcel Holtmann } 355376bca880SMarcel Holtmann EXPORT_SYMBOL(hci_recv_frame); 355476bca880SMarcel Holtmann 3555e875ff84SMarcel Holtmann /* Receive diagnostic message from HCI drivers */ 3556e875ff84SMarcel Holtmann int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb) 3557e875ff84SMarcel Holtmann { 3558581d6fd6SMarcel Holtmann /* Mark as diagnostic packet */ 3559581d6fd6SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_DIAG_PKT; 3560581d6fd6SMarcel Holtmann 3561e875ff84SMarcel Holtmann /* Time stamp */ 3562e875ff84SMarcel Holtmann __net_timestamp(skb); 3563e875ff84SMarcel Holtmann 3564581d6fd6SMarcel Holtmann skb_queue_tail(&hdev->rx_q, skb); 3565581d6fd6SMarcel Holtmann queue_work(hdev->workqueue, &hdev->rx_work); 3566e875ff84SMarcel Holtmann 3567e875ff84SMarcel Holtmann return 0; 3568e875ff84SMarcel Holtmann } 3569e875ff84SMarcel Holtmann EXPORT_SYMBOL(hci_recv_diag); 3570e875ff84SMarcel Holtmann 35711da177e4SLinus Torvalds /* ---- Interface to upper protocols ---- */ 35721da177e4SLinus Torvalds 35731da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *cb) 35741da177e4SLinus Torvalds { 35751da177e4SLinus Torvalds BT_DBG("%p name %s", cb, cb->name); 35761da177e4SLinus Torvalds 3577fba7ecf0SJohan Hedberg mutex_lock(&hci_cb_list_lock); 357800629e0fSJohan Hedberg list_add_tail(&cb->list, &hci_cb_list); 3579fba7ecf0SJohan Hedberg mutex_unlock(&hci_cb_list_lock); 35801da177e4SLinus Torvalds 35811da177e4SLinus Torvalds return 0; 35821da177e4SLinus Torvalds } 35831da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_cb); 35841da177e4SLinus Torvalds 35851da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *cb) 35861da177e4SLinus Torvalds { 35871da177e4SLinus Torvalds BT_DBG("%p name %s", cb, cb->name); 35881da177e4SLinus Torvalds 3589fba7ecf0SJohan Hedberg mutex_lock(&hci_cb_list_lock); 35901da177e4SLinus Torvalds list_del(&cb->list); 3591fba7ecf0SJohan Hedberg mutex_unlock(&hci_cb_list_lock); 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds return 0; 35941da177e4SLinus Torvalds } 35951da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_cb); 35961da177e4SLinus Torvalds 359751086991SMarcel Holtmann static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) 35981da177e4SLinus Torvalds { 3599cdc52faaSMarcel Holtmann int err; 3600cdc52faaSMarcel Holtmann 36010d48d939SMarcel Holtmann BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds /* Time stamp */ 3604a61bbcf2SPatrick McHardy __net_timestamp(skb); 36051da177e4SLinus Torvalds 3606cd82e61cSMarcel Holtmann /* Send copy to monitor */ 3607cd82e61cSMarcel Holtmann hci_send_to_monitor(hdev, skb); 3608cd82e61cSMarcel Holtmann 3609cd82e61cSMarcel Holtmann if (atomic_read(&hdev->promisc)) { 3610cd82e61cSMarcel Holtmann /* Send copy to the sockets */ 3611470fe1b5SMarcel Holtmann hci_send_to_sock(hdev, skb); 36121da177e4SLinus Torvalds } 36131da177e4SLinus Torvalds 36141da177e4SLinus Torvalds /* Get rid of skb owner, prior to sending to the driver. */ 36151da177e4SLinus Torvalds skb_orphan(skb); 36161da177e4SLinus Torvalds 361773d0d3c8SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) { 361873d0d3c8SMarcel Holtmann kfree_skb(skb); 361973d0d3c8SMarcel Holtmann return; 362073d0d3c8SMarcel Holtmann } 362173d0d3c8SMarcel Holtmann 3622cdc52faaSMarcel Holtmann err = hdev->send(hdev, skb); 3623cdc52faaSMarcel Holtmann if (err < 0) { 3624cdc52faaSMarcel Holtmann BT_ERR("%s sending frame failed (%d)", hdev->name, err); 3625cdc52faaSMarcel Holtmann kfree_skb(skb); 3626cdc52faaSMarcel Holtmann } 36271da177e4SLinus Torvalds } 36281da177e4SLinus Torvalds 36291ca3a9d0SJohan Hedberg /* Send HCI command */ 363007dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, 363107dc93ddSJohan Hedberg const void *param) 36321ca3a9d0SJohan Hedberg { 36331ca3a9d0SJohan Hedberg struct sk_buff *skb; 36341ca3a9d0SJohan Hedberg 36351ca3a9d0SJohan Hedberg BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); 36361ca3a9d0SJohan Hedberg 36371ca3a9d0SJohan Hedberg skb = hci_prepare_cmd(hdev, opcode, plen, param); 36381ca3a9d0SJohan Hedberg if (!skb) { 36391ca3a9d0SJohan Hedberg BT_ERR("%s no memory for command", hdev->name); 36401ca3a9d0SJohan Hedberg return -ENOMEM; 36411ca3a9d0SJohan Hedberg } 36421ca3a9d0SJohan Hedberg 364349c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as 364411714b3dSJohan Hedberg * single-command requests. 364511714b3dSJohan Hedberg */ 3646db6e3e8dSJohan Hedberg bt_cb(skb)->req.start = true; 364711714b3dSJohan Hedberg 36481da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb); 3649c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds return 0; 36521da177e4SLinus Torvalds } 36531da177e4SLinus Torvalds 36541da177e4SLinus Torvalds /* Get data from the previously sent command */ 3655a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode) 36561da177e4SLinus Torvalds { 36571da177e4SLinus Torvalds struct hci_command_hdr *hdr; 36581da177e4SLinus Torvalds 36591da177e4SLinus Torvalds if (!hdev->sent_cmd) 36601da177e4SLinus Torvalds return NULL; 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds hdr = (void *) hdev->sent_cmd->data; 36631da177e4SLinus Torvalds 3664a9de9248SMarcel Holtmann if (hdr->opcode != cpu_to_le16(opcode)) 36651da177e4SLinus Torvalds return NULL; 36661da177e4SLinus Torvalds 3667f0e09510SAndrei Emeltchenko BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); 36681da177e4SLinus Torvalds 36691da177e4SLinus Torvalds return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE; 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds 3672fbef168fSLoic Poulain /* Send HCI command and wait for command commplete event */ 3673fbef168fSLoic Poulain struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, 3674fbef168fSLoic Poulain const void *param, u32 timeout) 3675fbef168fSLoic Poulain { 3676fbef168fSLoic Poulain struct sk_buff *skb; 3677fbef168fSLoic Poulain 3678fbef168fSLoic Poulain if (!test_bit(HCI_UP, &hdev->flags)) 3679fbef168fSLoic Poulain return ERR_PTR(-ENETDOWN); 3680fbef168fSLoic Poulain 3681fbef168fSLoic Poulain bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); 3682fbef168fSLoic Poulain 3683fbef168fSLoic Poulain hci_req_lock(hdev); 3684fbef168fSLoic Poulain skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); 3685fbef168fSLoic Poulain hci_req_unlock(hdev); 3686fbef168fSLoic Poulain 3687fbef168fSLoic Poulain return skb; 3688fbef168fSLoic Poulain } 3689fbef168fSLoic Poulain EXPORT_SYMBOL(hci_cmd_sync); 3690fbef168fSLoic Poulain 36911da177e4SLinus Torvalds /* Send ACL data */ 36921da177e4SLinus Torvalds static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags) 36931da177e4SLinus Torvalds { 36941da177e4SLinus Torvalds struct hci_acl_hdr *hdr; 36951da177e4SLinus Torvalds int len = skb->len; 36961da177e4SLinus Torvalds 3697badff6d0SArnaldo Carvalho de Melo skb_push(skb, HCI_ACL_HDR_SIZE); 3698badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 36999c70220bSArnaldo Carvalho de Melo hdr = (struct hci_acl_hdr *)skb_transport_header(skb); 3700aca3192cSYOSHIFUJI Hideaki hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags)); 3701aca3192cSYOSHIFUJI Hideaki hdr->dlen = cpu_to_le16(len); 37021da177e4SLinus Torvalds } 37031da177e4SLinus Torvalds 3704ee22be7eSAndrei Emeltchenko static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue, 370573d80debSLuiz Augusto von Dentz struct sk_buff *skb, __u16 flags) 37061da177e4SLinus Torvalds { 3707ee22be7eSAndrei Emeltchenko struct hci_conn *conn = chan->conn; 37081da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 37091da177e4SLinus Torvalds struct sk_buff *list; 37101da177e4SLinus Torvalds 3711087bfd99SGustavo Padovan skb->len = skb_headlen(skb); 3712087bfd99SGustavo Padovan skb->data_len = 0; 3713087bfd99SGustavo Padovan 3714087bfd99SGustavo Padovan bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 3715204a6e54SAndrei Emeltchenko 3716204a6e54SAndrei Emeltchenko switch (hdev->dev_type) { 3717204a6e54SAndrei Emeltchenko case HCI_BREDR: 3718087bfd99SGustavo Padovan hci_add_acl_hdr(skb, conn->handle, flags); 3719204a6e54SAndrei Emeltchenko break; 3720204a6e54SAndrei Emeltchenko case HCI_AMP: 3721204a6e54SAndrei Emeltchenko hci_add_acl_hdr(skb, chan->handle, flags); 3722204a6e54SAndrei Emeltchenko break; 3723204a6e54SAndrei Emeltchenko default: 3724204a6e54SAndrei Emeltchenko BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type); 3725204a6e54SAndrei Emeltchenko return; 3726204a6e54SAndrei Emeltchenko } 3727087bfd99SGustavo Padovan 372870f23020SAndrei Emeltchenko list = skb_shinfo(skb)->frag_list; 372970f23020SAndrei Emeltchenko if (!list) { 37301da177e4SLinus Torvalds /* Non fragmented */ 37311da177e4SLinus Torvalds BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len); 37321da177e4SLinus Torvalds 373373d80debSLuiz Augusto von Dentz skb_queue_tail(queue, skb); 37341da177e4SLinus Torvalds } else { 37351da177e4SLinus Torvalds /* Fragmented */ 37361da177e4SLinus Torvalds BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); 37371da177e4SLinus Torvalds 37381da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = NULL; 37391da177e4SLinus Torvalds 37409cfd5a23SJukka Rissanen /* Queue all fragments atomically. We need to use spin_lock_bh 37419cfd5a23SJukka Rissanen * here because of 6LoWPAN links, as there this function is 37429cfd5a23SJukka Rissanen * called from softirq and using normal spin lock could cause 37439cfd5a23SJukka Rissanen * deadlocks. 37449cfd5a23SJukka Rissanen */ 37459cfd5a23SJukka Rissanen spin_lock_bh(&queue->lock); 37461da177e4SLinus Torvalds 374773d80debSLuiz Augusto von Dentz __skb_queue_tail(queue, skb); 3748e702112fSAndrei Emeltchenko 3749e702112fSAndrei Emeltchenko flags &= ~ACL_START; 3750e702112fSAndrei Emeltchenko flags |= ACL_CONT; 37511da177e4SLinus Torvalds do { 37521da177e4SLinus Torvalds skb = list; list = list->next; 37531da177e4SLinus Torvalds 37540d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 3755e702112fSAndrei Emeltchenko hci_add_acl_hdr(skb, conn->handle, flags); 37561da177e4SLinus Torvalds 37571da177e4SLinus Torvalds BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len); 37581da177e4SLinus Torvalds 375973d80debSLuiz Augusto von Dentz __skb_queue_tail(queue, skb); 37601da177e4SLinus Torvalds } while (list); 37611da177e4SLinus Torvalds 37629cfd5a23SJukka Rissanen spin_unlock_bh(&queue->lock); 37631da177e4SLinus Torvalds } 376473d80debSLuiz Augusto von Dentz } 376573d80debSLuiz Augusto von Dentz 376673d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags) 376773d80debSLuiz Augusto von Dentz { 3768ee22be7eSAndrei Emeltchenko struct hci_dev *hdev = chan->conn->hdev; 376973d80debSLuiz Augusto von Dentz 3770f0e09510SAndrei Emeltchenko BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); 377173d80debSLuiz Augusto von Dentz 3772ee22be7eSAndrei Emeltchenko hci_queue_acl(chan, &chan->data_q, skb, flags); 37731da177e4SLinus Torvalds 37743eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 37751da177e4SLinus Torvalds } 37761da177e4SLinus Torvalds 37771da177e4SLinus Torvalds /* Send SCO data */ 37780d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb) 37791da177e4SLinus Torvalds { 37801da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 37811da177e4SLinus Torvalds struct hci_sco_hdr hdr; 37821da177e4SLinus Torvalds 37831da177e4SLinus Torvalds BT_DBG("%s len %d", hdev->name, skb->len); 37841da177e4SLinus Torvalds 3785aca3192cSYOSHIFUJI Hideaki hdr.handle = cpu_to_le16(conn->handle); 37861da177e4SLinus Torvalds hdr.dlen = skb->len; 37871da177e4SLinus Torvalds 3788badff6d0SArnaldo Carvalho de Melo skb_push(skb, HCI_SCO_HDR_SIZE); 3789badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 37909c70220bSArnaldo Carvalho de Melo memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); 37911da177e4SLinus Torvalds 37920d48d939SMarcel Holtmann bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 3793c78ae283SMarcel Holtmann 37941da177e4SLinus Torvalds skb_queue_tail(&conn->data_q, skb); 37953eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work); 37961da177e4SLinus Torvalds } 37971da177e4SLinus Torvalds 37981da177e4SLinus Torvalds /* ---- HCI TX task (outgoing data) ---- */ 37991da177e4SLinus Torvalds 38001da177e4SLinus Torvalds /* HCI Connection scheduler */ 38016039aa73SGustavo Padovan static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, 3802a8c5fb1aSGustavo Padovan int *quote) 38031da177e4SLinus Torvalds { 38041da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 38058035ded4SLuiz Augusto von Dentz struct hci_conn *conn = NULL, *c; 3806abc5de8fSMikel Astiz unsigned int num = 0, min = ~0; 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds /* We don't have to lock device here. Connections are always 38091da177e4SLinus Torvalds * added and removed with TX task disabled. */ 3810bf4c6325SGustavo F. Padovan 3811bf4c6325SGustavo F. Padovan rcu_read_lock(); 3812bf4c6325SGustavo F. Padovan 3813bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(c, &h->list, list) { 3814769be974SMarcel Holtmann if (c->type != type || skb_queue_empty(&c->data_q)) 38151da177e4SLinus Torvalds continue; 3816769be974SMarcel Holtmann 3817769be974SMarcel Holtmann if (c->state != BT_CONNECTED && c->state != BT_CONFIG) 3818769be974SMarcel Holtmann continue; 3819769be974SMarcel Holtmann 38201da177e4SLinus Torvalds num++; 38211da177e4SLinus Torvalds 38221da177e4SLinus Torvalds if (c->sent < min) { 38231da177e4SLinus Torvalds min = c->sent; 38241da177e4SLinus Torvalds conn = c; 38251da177e4SLinus Torvalds } 382652087a79SLuiz Augusto von Dentz 382752087a79SLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == num) 382852087a79SLuiz Augusto von Dentz break; 38291da177e4SLinus Torvalds } 38301da177e4SLinus Torvalds 3831bf4c6325SGustavo F. Padovan rcu_read_unlock(); 3832bf4c6325SGustavo F. Padovan 38331da177e4SLinus Torvalds if (conn) { 38346ed58ec5SVille Tervo int cnt, q; 38356ed58ec5SVille Tervo 38366ed58ec5SVille Tervo switch (conn->type) { 38376ed58ec5SVille Tervo case ACL_LINK: 38386ed58ec5SVille Tervo cnt = hdev->acl_cnt; 38396ed58ec5SVille Tervo break; 38406ed58ec5SVille Tervo case SCO_LINK: 38416ed58ec5SVille Tervo case ESCO_LINK: 38426ed58ec5SVille Tervo cnt = hdev->sco_cnt; 38436ed58ec5SVille Tervo break; 38446ed58ec5SVille Tervo case LE_LINK: 38456ed58ec5SVille Tervo cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; 38466ed58ec5SVille Tervo break; 38476ed58ec5SVille Tervo default: 38486ed58ec5SVille Tervo cnt = 0; 38496ed58ec5SVille Tervo BT_ERR("Unknown link type"); 38506ed58ec5SVille Tervo } 38516ed58ec5SVille Tervo 38526ed58ec5SVille Tervo q = cnt / num; 38531da177e4SLinus Torvalds *quote = q ? q : 1; 38541da177e4SLinus Torvalds } else 38551da177e4SLinus Torvalds *quote = 0; 38561da177e4SLinus Torvalds 38571da177e4SLinus Torvalds BT_DBG("conn %p quote %d", conn, *quote); 38581da177e4SLinus Torvalds return conn; 38591da177e4SLinus Torvalds } 38601da177e4SLinus Torvalds 38616039aa73SGustavo Padovan static void hci_link_tx_to(struct hci_dev *hdev, __u8 type) 38621da177e4SLinus Torvalds { 38631da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 38641da177e4SLinus Torvalds struct hci_conn *c; 38651da177e4SLinus Torvalds 3866bae1f5d9SVille Tervo BT_ERR("%s link tx timeout", hdev->name); 38671da177e4SLinus Torvalds 3868bf4c6325SGustavo F. Padovan rcu_read_lock(); 3869bf4c6325SGustavo F. Padovan 38701da177e4SLinus Torvalds /* Kill stalled connections */ 3871bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(c, &h->list, list) { 3872bae1f5d9SVille Tervo if (c->type == type && c->sent) { 38736ed93dc6SAndrei Emeltchenko BT_ERR("%s killing stalled connection %pMR", 38746ed93dc6SAndrei Emeltchenko hdev->name, &c->dst); 3875bed71748SAndre Guedes hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM); 38761da177e4SLinus Torvalds } 38771da177e4SLinus Torvalds } 3878bf4c6325SGustavo F. Padovan 3879bf4c6325SGustavo F. Padovan rcu_read_unlock(); 38801da177e4SLinus Torvalds } 38811da177e4SLinus Torvalds 38826039aa73SGustavo Padovan static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, 388373d80debSLuiz Augusto von Dentz int *quote) 388473d80debSLuiz Augusto von Dentz { 388573d80debSLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 388673d80debSLuiz Augusto von Dentz struct hci_chan *chan = NULL; 3887abc5de8fSMikel Astiz unsigned int num = 0, min = ~0, cur_prio = 0; 388873d80debSLuiz Augusto von Dentz struct hci_conn *conn; 388973d80debSLuiz Augusto von Dentz int cnt, q, conn_num = 0; 389073d80debSLuiz Augusto von Dentz 389173d80debSLuiz Augusto von Dentz BT_DBG("%s", hdev->name); 389273d80debSLuiz Augusto von Dentz 3893bf4c6325SGustavo F. Padovan rcu_read_lock(); 3894bf4c6325SGustavo F. Padovan 3895bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(conn, &h->list, list) { 389673d80debSLuiz Augusto von Dentz struct hci_chan *tmp; 389773d80debSLuiz Augusto von Dentz 389873d80debSLuiz Augusto von Dentz if (conn->type != type) 389973d80debSLuiz Augusto von Dentz continue; 390073d80debSLuiz Augusto von Dentz 390173d80debSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 390273d80debSLuiz Augusto von Dentz continue; 390373d80debSLuiz Augusto von Dentz 390473d80debSLuiz Augusto von Dentz conn_num++; 390573d80debSLuiz Augusto von Dentz 39068192edefSGustavo F. Padovan list_for_each_entry_rcu(tmp, &conn->chan_list, list) { 390773d80debSLuiz Augusto von Dentz struct sk_buff *skb; 390873d80debSLuiz Augusto von Dentz 390973d80debSLuiz Augusto von Dentz if (skb_queue_empty(&tmp->data_q)) 391073d80debSLuiz Augusto von Dentz continue; 391173d80debSLuiz Augusto von Dentz 391273d80debSLuiz Augusto von Dentz skb = skb_peek(&tmp->data_q); 391373d80debSLuiz Augusto von Dentz if (skb->priority < cur_prio) 391473d80debSLuiz Augusto von Dentz continue; 391573d80debSLuiz Augusto von Dentz 391673d80debSLuiz Augusto von Dentz if (skb->priority > cur_prio) { 391773d80debSLuiz Augusto von Dentz num = 0; 391873d80debSLuiz Augusto von Dentz min = ~0; 391973d80debSLuiz Augusto von Dentz cur_prio = skb->priority; 392073d80debSLuiz Augusto von Dentz } 392173d80debSLuiz Augusto von Dentz 392273d80debSLuiz Augusto von Dentz num++; 392373d80debSLuiz Augusto von Dentz 392473d80debSLuiz Augusto von Dentz if (conn->sent < min) { 392573d80debSLuiz Augusto von Dentz min = conn->sent; 392673d80debSLuiz Augusto von Dentz chan = tmp; 392773d80debSLuiz Augusto von Dentz } 392873d80debSLuiz Augusto von Dentz } 392973d80debSLuiz Augusto von Dentz 393073d80debSLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == conn_num) 393173d80debSLuiz Augusto von Dentz break; 393273d80debSLuiz Augusto von Dentz } 393373d80debSLuiz Augusto von Dentz 3934bf4c6325SGustavo F. Padovan rcu_read_unlock(); 3935bf4c6325SGustavo F. Padovan 393673d80debSLuiz Augusto von Dentz if (!chan) 393773d80debSLuiz Augusto von Dentz return NULL; 393873d80debSLuiz Augusto von Dentz 393973d80debSLuiz Augusto von Dentz switch (chan->conn->type) { 394073d80debSLuiz Augusto von Dentz case ACL_LINK: 394173d80debSLuiz Augusto von Dentz cnt = hdev->acl_cnt; 394273d80debSLuiz Augusto von Dentz break; 3943bd1eb66bSAndrei Emeltchenko case AMP_LINK: 3944bd1eb66bSAndrei Emeltchenko cnt = hdev->block_cnt; 3945bd1eb66bSAndrei Emeltchenko break; 394673d80debSLuiz Augusto von Dentz case SCO_LINK: 394773d80debSLuiz Augusto von Dentz case ESCO_LINK: 394873d80debSLuiz Augusto von Dentz cnt = hdev->sco_cnt; 394973d80debSLuiz Augusto von Dentz break; 395073d80debSLuiz Augusto von Dentz case LE_LINK: 395173d80debSLuiz Augusto von Dentz cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt; 395273d80debSLuiz Augusto von Dentz break; 395373d80debSLuiz Augusto von Dentz default: 395473d80debSLuiz Augusto von Dentz cnt = 0; 395573d80debSLuiz Augusto von Dentz BT_ERR("Unknown link type"); 395673d80debSLuiz Augusto von Dentz } 395773d80debSLuiz Augusto von Dentz 395873d80debSLuiz Augusto von Dentz q = cnt / num; 395973d80debSLuiz Augusto von Dentz *quote = q ? q : 1; 396073d80debSLuiz Augusto von Dentz BT_DBG("chan %p quote %d", chan, *quote); 396173d80debSLuiz Augusto von Dentz return chan; 396273d80debSLuiz Augusto von Dentz } 396373d80debSLuiz Augusto von Dentz 396402b20f0bSLuiz Augusto von Dentz static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type) 396502b20f0bSLuiz Augusto von Dentz { 396602b20f0bSLuiz Augusto von Dentz struct hci_conn_hash *h = &hdev->conn_hash; 396702b20f0bSLuiz Augusto von Dentz struct hci_conn *conn; 396802b20f0bSLuiz Augusto von Dentz int num = 0; 396902b20f0bSLuiz Augusto von Dentz 397002b20f0bSLuiz Augusto von Dentz BT_DBG("%s", hdev->name); 397102b20f0bSLuiz Augusto von Dentz 3972bf4c6325SGustavo F. Padovan rcu_read_lock(); 3973bf4c6325SGustavo F. Padovan 3974bf4c6325SGustavo F. Padovan list_for_each_entry_rcu(conn, &h->list, list) { 397502b20f0bSLuiz Augusto von Dentz struct hci_chan *chan; 397602b20f0bSLuiz Augusto von Dentz 397702b20f0bSLuiz Augusto von Dentz if (conn->type != type) 397802b20f0bSLuiz Augusto von Dentz continue; 397902b20f0bSLuiz Augusto von Dentz 398002b20f0bSLuiz Augusto von Dentz if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 398102b20f0bSLuiz Augusto von Dentz continue; 398202b20f0bSLuiz Augusto von Dentz 398302b20f0bSLuiz Augusto von Dentz num++; 398402b20f0bSLuiz Augusto von Dentz 39858192edefSGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_list, list) { 398602b20f0bSLuiz Augusto von Dentz struct sk_buff *skb; 398702b20f0bSLuiz Augusto von Dentz 398802b20f0bSLuiz Augusto von Dentz if (chan->sent) { 398902b20f0bSLuiz Augusto von Dentz chan->sent = 0; 399002b20f0bSLuiz Augusto von Dentz continue; 399102b20f0bSLuiz Augusto von Dentz } 399202b20f0bSLuiz Augusto von Dentz 399302b20f0bSLuiz Augusto von Dentz if (skb_queue_empty(&chan->data_q)) 399402b20f0bSLuiz Augusto von Dentz continue; 399502b20f0bSLuiz Augusto von Dentz 399602b20f0bSLuiz Augusto von Dentz skb = skb_peek(&chan->data_q); 399702b20f0bSLuiz Augusto von Dentz if (skb->priority >= HCI_PRIO_MAX - 1) 399802b20f0bSLuiz Augusto von Dentz continue; 399902b20f0bSLuiz Augusto von Dentz 400002b20f0bSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX - 1; 400102b20f0bSLuiz Augusto von Dentz 400202b20f0bSLuiz Augusto von Dentz BT_DBG("chan %p skb %p promoted to %d", chan, skb, 400302b20f0bSLuiz Augusto von Dentz skb->priority); 400402b20f0bSLuiz Augusto von Dentz } 400502b20f0bSLuiz Augusto von Dentz 400602b20f0bSLuiz Augusto von Dentz if (hci_conn_num(hdev, type) == num) 400702b20f0bSLuiz Augusto von Dentz break; 400802b20f0bSLuiz Augusto von Dentz } 4009bf4c6325SGustavo F. Padovan 4010bf4c6325SGustavo F. Padovan rcu_read_unlock(); 4011bf4c6325SGustavo F. Padovan 401202b20f0bSLuiz Augusto von Dentz } 401302b20f0bSLuiz Augusto von Dentz 4014b71d385aSAndrei Emeltchenko static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb) 4015b71d385aSAndrei Emeltchenko { 4016b71d385aSAndrei Emeltchenko /* Calculate count of blocks used by this packet */ 4017b71d385aSAndrei Emeltchenko return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len); 4018b71d385aSAndrei Emeltchenko } 4019b71d385aSAndrei Emeltchenko 40206039aa73SGustavo Padovan static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) 40211da177e4SLinus Torvalds { 4022d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 40231da177e4SLinus Torvalds /* ACL tx timeout must be longer than maximum 40241da177e4SLinus Torvalds * link supervision timeout (40.9 seconds) */ 402563d2bc1bSAndrei Emeltchenko if (!cnt && time_after(jiffies, hdev->acl_last_tx + 40265f246e89SAndrei Emeltchenko HCI_ACL_TX_TIMEOUT)) 4027bae1f5d9SVille Tervo hci_link_tx_to(hdev, ACL_LINK); 40281da177e4SLinus Torvalds } 402963d2bc1bSAndrei Emeltchenko } 40301da177e4SLinus Torvalds 40316039aa73SGustavo Padovan static void hci_sched_acl_pkt(struct hci_dev *hdev) 403263d2bc1bSAndrei Emeltchenko { 403363d2bc1bSAndrei Emeltchenko unsigned int cnt = hdev->acl_cnt; 403463d2bc1bSAndrei Emeltchenko struct hci_chan *chan; 403563d2bc1bSAndrei Emeltchenko struct sk_buff *skb; 403663d2bc1bSAndrei Emeltchenko int quote; 403763d2bc1bSAndrei Emeltchenko 403863d2bc1bSAndrei Emeltchenko __check_timeout(hdev, cnt); 403904837f64SMarcel Holtmann 404073d80debSLuiz Augusto von Dentz while (hdev->acl_cnt && 404173d80debSLuiz Augusto von Dentz (chan = hci_chan_sent(hdev, ACL_LINK, "e))) { 4042ec1cce24SLuiz Augusto von Dentz u32 priority = (skb_peek(&chan->data_q))->priority; 4043ec1cce24SLuiz Augusto von Dentz while (quote-- && (skb = skb_peek(&chan->data_q))) { 404473d80debSLuiz Augusto von Dentz BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 404573d80debSLuiz Augusto von Dentz skb->len, skb->priority); 404673d80debSLuiz Augusto von Dentz 4047ec1cce24SLuiz Augusto von Dentz /* Stop if priority has changed */ 4048ec1cce24SLuiz Augusto von Dentz if (skb->priority < priority) 4049ec1cce24SLuiz Augusto von Dentz break; 4050ec1cce24SLuiz Augusto von Dentz 4051ec1cce24SLuiz Augusto von Dentz skb = skb_dequeue(&chan->data_q); 4052ec1cce24SLuiz Augusto von Dentz 405373d80debSLuiz Augusto von Dentz hci_conn_enter_active_mode(chan->conn, 405473d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active); 405504837f64SMarcel Holtmann 405657d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 40571da177e4SLinus Torvalds hdev->acl_last_tx = jiffies; 40581da177e4SLinus Torvalds 40591da177e4SLinus Torvalds hdev->acl_cnt--; 406073d80debSLuiz Augusto von Dentz chan->sent++; 406173d80debSLuiz Augusto von Dentz chan->conn->sent++; 40621da177e4SLinus Torvalds } 40631da177e4SLinus Torvalds } 406402b20f0bSLuiz Augusto von Dentz 406502b20f0bSLuiz Augusto von Dentz if (cnt != hdev->acl_cnt) 406602b20f0bSLuiz Augusto von Dentz hci_prio_recalculate(hdev, ACL_LINK); 40671da177e4SLinus Torvalds } 40681da177e4SLinus Torvalds 40696039aa73SGustavo Padovan static void hci_sched_acl_blk(struct hci_dev *hdev) 4070b71d385aSAndrei Emeltchenko { 407163d2bc1bSAndrei Emeltchenko unsigned int cnt = hdev->block_cnt; 4072b71d385aSAndrei Emeltchenko struct hci_chan *chan; 4073b71d385aSAndrei Emeltchenko struct sk_buff *skb; 4074b71d385aSAndrei Emeltchenko int quote; 4075bd1eb66bSAndrei Emeltchenko u8 type; 4076b71d385aSAndrei Emeltchenko 407763d2bc1bSAndrei Emeltchenko __check_timeout(hdev, cnt); 4078b71d385aSAndrei Emeltchenko 4079bd1eb66bSAndrei Emeltchenko BT_DBG("%s", hdev->name); 4080bd1eb66bSAndrei Emeltchenko 4081bd1eb66bSAndrei Emeltchenko if (hdev->dev_type == HCI_AMP) 4082bd1eb66bSAndrei Emeltchenko type = AMP_LINK; 4083bd1eb66bSAndrei Emeltchenko else 4084bd1eb66bSAndrei Emeltchenko type = ACL_LINK; 4085bd1eb66bSAndrei Emeltchenko 4086b71d385aSAndrei Emeltchenko while (hdev->block_cnt > 0 && 4087bd1eb66bSAndrei Emeltchenko (chan = hci_chan_sent(hdev, type, "e))) { 4088b71d385aSAndrei Emeltchenko u32 priority = (skb_peek(&chan->data_q))->priority; 4089b71d385aSAndrei Emeltchenko while (quote > 0 && (skb = skb_peek(&chan->data_q))) { 4090b71d385aSAndrei Emeltchenko int blocks; 4091b71d385aSAndrei Emeltchenko 4092b71d385aSAndrei Emeltchenko BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 4093b71d385aSAndrei Emeltchenko skb->len, skb->priority); 4094b71d385aSAndrei Emeltchenko 4095b71d385aSAndrei Emeltchenko /* Stop if priority has changed */ 4096b71d385aSAndrei Emeltchenko if (skb->priority < priority) 4097b71d385aSAndrei Emeltchenko break; 4098b71d385aSAndrei Emeltchenko 4099b71d385aSAndrei Emeltchenko skb = skb_dequeue(&chan->data_q); 4100b71d385aSAndrei Emeltchenko 4101b71d385aSAndrei Emeltchenko blocks = __get_blocks(hdev, skb); 4102b71d385aSAndrei Emeltchenko if (blocks > hdev->block_cnt) 4103b71d385aSAndrei Emeltchenko return; 4104b71d385aSAndrei Emeltchenko 4105b71d385aSAndrei Emeltchenko hci_conn_enter_active_mode(chan->conn, 4106b71d385aSAndrei Emeltchenko bt_cb(skb)->force_active); 4107b71d385aSAndrei Emeltchenko 410857d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 4109b71d385aSAndrei Emeltchenko hdev->acl_last_tx = jiffies; 4110b71d385aSAndrei Emeltchenko 4111b71d385aSAndrei Emeltchenko hdev->block_cnt -= blocks; 4112b71d385aSAndrei Emeltchenko quote -= blocks; 4113b71d385aSAndrei Emeltchenko 4114b71d385aSAndrei Emeltchenko chan->sent += blocks; 4115b71d385aSAndrei Emeltchenko chan->conn->sent += blocks; 4116b71d385aSAndrei Emeltchenko } 4117b71d385aSAndrei Emeltchenko } 4118b71d385aSAndrei Emeltchenko 4119b71d385aSAndrei Emeltchenko if (cnt != hdev->block_cnt) 4120bd1eb66bSAndrei Emeltchenko hci_prio_recalculate(hdev, type); 4121b71d385aSAndrei Emeltchenko } 4122b71d385aSAndrei Emeltchenko 41236039aa73SGustavo Padovan static void hci_sched_acl(struct hci_dev *hdev) 4124b71d385aSAndrei Emeltchenko { 4125b71d385aSAndrei Emeltchenko BT_DBG("%s", hdev->name); 4126b71d385aSAndrei Emeltchenko 4127bd1eb66bSAndrei Emeltchenko /* No ACL link over BR/EDR controller */ 4128bd1eb66bSAndrei Emeltchenko if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR) 4129bd1eb66bSAndrei Emeltchenko return; 4130bd1eb66bSAndrei Emeltchenko 4131bd1eb66bSAndrei Emeltchenko /* No AMP link over AMP controller */ 4132bd1eb66bSAndrei Emeltchenko if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP) 4133b71d385aSAndrei Emeltchenko return; 4134b71d385aSAndrei Emeltchenko 4135b71d385aSAndrei Emeltchenko switch (hdev->flow_ctl_mode) { 4136b71d385aSAndrei Emeltchenko case HCI_FLOW_CTL_MODE_PACKET_BASED: 4137b71d385aSAndrei Emeltchenko hci_sched_acl_pkt(hdev); 4138b71d385aSAndrei Emeltchenko break; 4139b71d385aSAndrei Emeltchenko 4140b71d385aSAndrei Emeltchenko case HCI_FLOW_CTL_MODE_BLOCK_BASED: 4141b71d385aSAndrei Emeltchenko hci_sched_acl_blk(hdev); 4142b71d385aSAndrei Emeltchenko break; 4143b71d385aSAndrei Emeltchenko } 4144b71d385aSAndrei Emeltchenko } 4145b71d385aSAndrei Emeltchenko 41461da177e4SLinus Torvalds /* Schedule SCO */ 41476039aa73SGustavo Padovan static void hci_sched_sco(struct hci_dev *hdev) 41481da177e4SLinus Torvalds { 41491da177e4SLinus Torvalds struct hci_conn *conn; 41501da177e4SLinus Torvalds struct sk_buff *skb; 41511da177e4SLinus Torvalds int quote; 41521da177e4SLinus Torvalds 41531da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 41541da177e4SLinus Torvalds 415552087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, SCO_LINK)) 415652087a79SLuiz Augusto von Dentz return; 415752087a79SLuiz Augusto von Dentz 41581da177e4SLinus Torvalds while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) { 41591da177e4SLinus Torvalds while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 41601da177e4SLinus Torvalds BT_DBG("skb %p len %d", skb, skb->len); 416157d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 41621da177e4SLinus Torvalds 41631da177e4SLinus Torvalds conn->sent++; 41641da177e4SLinus Torvalds if (conn->sent == ~0) 41651da177e4SLinus Torvalds conn->sent = 0; 41661da177e4SLinus Torvalds } 41671da177e4SLinus Torvalds } 41681da177e4SLinus Torvalds } 41691da177e4SLinus Torvalds 41706039aa73SGustavo Padovan static void hci_sched_esco(struct hci_dev *hdev) 4171b6a0dc82SMarcel Holtmann { 4172b6a0dc82SMarcel Holtmann struct hci_conn *conn; 4173b6a0dc82SMarcel Holtmann struct sk_buff *skb; 4174b6a0dc82SMarcel Holtmann int quote; 4175b6a0dc82SMarcel Holtmann 4176b6a0dc82SMarcel Holtmann BT_DBG("%s", hdev->name); 4177b6a0dc82SMarcel Holtmann 417852087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, ESCO_LINK)) 417952087a79SLuiz Augusto von Dentz return; 418052087a79SLuiz Augusto von Dentz 41818fc9ced3SGustavo Padovan while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, 41828fc9ced3SGustavo Padovan "e))) { 4183b6a0dc82SMarcel Holtmann while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 4184b6a0dc82SMarcel Holtmann BT_DBG("skb %p len %d", skb, skb->len); 418557d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 4186b6a0dc82SMarcel Holtmann 4187b6a0dc82SMarcel Holtmann conn->sent++; 4188b6a0dc82SMarcel Holtmann if (conn->sent == ~0) 4189b6a0dc82SMarcel Holtmann conn->sent = 0; 4190b6a0dc82SMarcel Holtmann } 4191b6a0dc82SMarcel Holtmann } 4192b6a0dc82SMarcel Holtmann } 4193b6a0dc82SMarcel Holtmann 41946039aa73SGustavo Padovan static void hci_sched_le(struct hci_dev *hdev) 41956ed58ec5SVille Tervo { 419673d80debSLuiz Augusto von Dentz struct hci_chan *chan; 41976ed58ec5SVille Tervo struct sk_buff *skb; 419802b20f0bSLuiz Augusto von Dentz int quote, cnt, tmp; 41996ed58ec5SVille Tervo 42006ed58ec5SVille Tervo BT_DBG("%s", hdev->name); 42016ed58ec5SVille Tervo 420252087a79SLuiz Augusto von Dentz if (!hci_conn_num(hdev, LE_LINK)) 420352087a79SLuiz Augusto von Dentz return; 420452087a79SLuiz Augusto von Dentz 4205d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 42066ed58ec5SVille Tervo /* LE tx timeout must be longer than maximum 42076ed58ec5SVille Tervo * link supervision timeout (40.9 seconds) */ 4208bae1f5d9SVille Tervo if (!hdev->le_cnt && hdev->le_pkts && 42096ed58ec5SVille Tervo time_after(jiffies, hdev->le_last_tx + HZ * 45)) 4210bae1f5d9SVille Tervo hci_link_tx_to(hdev, LE_LINK); 42116ed58ec5SVille Tervo } 42126ed58ec5SVille Tervo 42136ed58ec5SVille Tervo cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt; 421402b20f0bSLuiz Augusto von Dentz tmp = cnt; 421573d80debSLuiz Augusto von Dentz while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) { 4216ec1cce24SLuiz Augusto von Dentz u32 priority = (skb_peek(&chan->data_q))->priority; 4217ec1cce24SLuiz Augusto von Dentz while (quote-- && (skb = skb_peek(&chan->data_q))) { 421873d80debSLuiz Augusto von Dentz BT_DBG("chan %p skb %p len %d priority %u", chan, skb, 421973d80debSLuiz Augusto von Dentz skb->len, skb->priority); 42206ed58ec5SVille Tervo 4221ec1cce24SLuiz Augusto von Dentz /* Stop if priority has changed */ 4222ec1cce24SLuiz Augusto von Dentz if (skb->priority < priority) 4223ec1cce24SLuiz Augusto von Dentz break; 4224ec1cce24SLuiz Augusto von Dentz 4225ec1cce24SLuiz Augusto von Dentz skb = skb_dequeue(&chan->data_q); 4226ec1cce24SLuiz Augusto von Dentz 422757d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 42286ed58ec5SVille Tervo hdev->le_last_tx = jiffies; 42296ed58ec5SVille Tervo 42306ed58ec5SVille Tervo cnt--; 423173d80debSLuiz Augusto von Dentz chan->sent++; 423273d80debSLuiz Augusto von Dentz chan->conn->sent++; 42336ed58ec5SVille Tervo } 42346ed58ec5SVille Tervo } 423573d80debSLuiz Augusto von Dentz 42366ed58ec5SVille Tervo if (hdev->le_pkts) 42376ed58ec5SVille Tervo hdev->le_cnt = cnt; 42386ed58ec5SVille Tervo else 42396ed58ec5SVille Tervo hdev->acl_cnt = cnt; 424002b20f0bSLuiz Augusto von Dentz 424102b20f0bSLuiz Augusto von Dentz if (cnt != tmp) 424202b20f0bSLuiz Augusto von Dentz hci_prio_recalculate(hdev, LE_LINK); 42436ed58ec5SVille Tervo } 42446ed58ec5SVille Tervo 42453eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work) 42461da177e4SLinus Torvalds { 42473eff45eaSGustavo F. Padovan struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work); 42481da177e4SLinus Torvalds struct sk_buff *skb; 42491da177e4SLinus Torvalds 42506ed58ec5SVille Tervo BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, 42516ed58ec5SVille Tervo hdev->sco_cnt, hdev->le_cnt); 42521da177e4SLinus Torvalds 4253d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 42541da177e4SLinus Torvalds /* Schedule queues and send stuff to HCI driver */ 42551da177e4SLinus Torvalds hci_sched_acl(hdev); 42561da177e4SLinus Torvalds hci_sched_sco(hdev); 4257b6a0dc82SMarcel Holtmann hci_sched_esco(hdev); 42586ed58ec5SVille Tervo hci_sched_le(hdev); 425952de599eSMarcel Holtmann } 42606ed58ec5SVille Tervo 42611da177e4SLinus Torvalds /* Send next queued raw (unknown type) packet */ 42621da177e4SLinus Torvalds while ((skb = skb_dequeue(&hdev->raw_q))) 426357d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 42641da177e4SLinus Torvalds } 42651da177e4SLinus Torvalds 426625985edcSLucas De Marchi /* ----- HCI RX task (incoming data processing) ----- */ 42671da177e4SLinus Torvalds 42681da177e4SLinus Torvalds /* ACL data packet */ 42696039aa73SGustavo Padovan static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb) 42701da177e4SLinus Torvalds { 42711da177e4SLinus Torvalds struct hci_acl_hdr *hdr = (void *) skb->data; 42721da177e4SLinus Torvalds struct hci_conn *conn; 42731da177e4SLinus Torvalds __u16 handle, flags; 42741da177e4SLinus Torvalds 42751da177e4SLinus Torvalds skb_pull(skb, HCI_ACL_HDR_SIZE); 42761da177e4SLinus Torvalds 42771da177e4SLinus Torvalds handle = __le16_to_cpu(hdr->handle); 42781da177e4SLinus Torvalds flags = hci_flags(handle); 42791da177e4SLinus Torvalds handle = hci_handle(handle); 42801da177e4SLinus Torvalds 4281f0e09510SAndrei Emeltchenko BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len, 4282a8c5fb1aSGustavo Padovan handle, flags); 42831da177e4SLinus Torvalds 42841da177e4SLinus Torvalds hdev->stat.acl_rx++; 42851da177e4SLinus Torvalds 42861da177e4SLinus Torvalds hci_dev_lock(hdev); 42871da177e4SLinus Torvalds conn = hci_conn_hash_lookup_handle(hdev, handle); 42881da177e4SLinus Torvalds hci_dev_unlock(hdev); 42891da177e4SLinus Torvalds 42901da177e4SLinus Torvalds if (conn) { 429165983fc7SMat Martineau hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF); 429204837f64SMarcel Holtmann 42931da177e4SLinus Torvalds /* Send to upper protocol */ 4294686ebf28SUlisses Furquim l2cap_recv_acldata(conn, skb, flags); 42951da177e4SLinus Torvalds return; 42961da177e4SLinus Torvalds } else { 42971da177e4SLinus Torvalds BT_ERR("%s ACL packet for unknown connection handle %d", 42981da177e4SLinus Torvalds hdev->name, handle); 42991da177e4SLinus Torvalds } 43001da177e4SLinus Torvalds 43011da177e4SLinus Torvalds kfree_skb(skb); 43021da177e4SLinus Torvalds } 43031da177e4SLinus Torvalds 43041da177e4SLinus Torvalds /* SCO data packet */ 43056039aa73SGustavo Padovan static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb) 43061da177e4SLinus Torvalds { 43071da177e4SLinus Torvalds struct hci_sco_hdr *hdr = (void *) skb->data; 43081da177e4SLinus Torvalds struct hci_conn *conn; 43091da177e4SLinus Torvalds __u16 handle; 43101da177e4SLinus Torvalds 43111da177e4SLinus Torvalds skb_pull(skb, HCI_SCO_HDR_SIZE); 43121da177e4SLinus Torvalds 43131da177e4SLinus Torvalds handle = __le16_to_cpu(hdr->handle); 43141da177e4SLinus Torvalds 4315f0e09510SAndrei Emeltchenko BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle); 43161da177e4SLinus Torvalds 43171da177e4SLinus Torvalds hdev->stat.sco_rx++; 43181da177e4SLinus Torvalds 43191da177e4SLinus Torvalds hci_dev_lock(hdev); 43201da177e4SLinus Torvalds conn = hci_conn_hash_lookup_handle(hdev, handle); 43211da177e4SLinus Torvalds hci_dev_unlock(hdev); 43221da177e4SLinus Torvalds 43231da177e4SLinus Torvalds if (conn) { 43241da177e4SLinus Torvalds /* Send to upper protocol */ 4325686ebf28SUlisses Furquim sco_recv_scodata(conn, skb); 43261da177e4SLinus Torvalds return; 43271da177e4SLinus Torvalds } else { 43281da177e4SLinus Torvalds BT_ERR("%s SCO packet for unknown connection handle %d", 43291da177e4SLinus Torvalds hdev->name, handle); 43301da177e4SLinus Torvalds } 43311da177e4SLinus Torvalds 43321da177e4SLinus Torvalds kfree_skb(skb); 43331da177e4SLinus Torvalds } 43341da177e4SLinus Torvalds 43359238f36aSJohan Hedberg static bool hci_req_is_complete(struct hci_dev *hdev) 43369238f36aSJohan Hedberg { 43379238f36aSJohan Hedberg struct sk_buff *skb; 43389238f36aSJohan Hedberg 43399238f36aSJohan Hedberg skb = skb_peek(&hdev->cmd_q); 43409238f36aSJohan Hedberg if (!skb) 43419238f36aSJohan Hedberg return true; 43429238f36aSJohan Hedberg 4343db6e3e8dSJohan Hedberg return bt_cb(skb)->req.start; 43449238f36aSJohan Hedberg } 43459238f36aSJohan Hedberg 434642c6b129SJohan Hedberg static void hci_resend_last(struct hci_dev *hdev) 434742c6b129SJohan Hedberg { 434842c6b129SJohan Hedberg struct hci_command_hdr *sent; 434942c6b129SJohan Hedberg struct sk_buff *skb; 435042c6b129SJohan Hedberg u16 opcode; 435142c6b129SJohan Hedberg 435242c6b129SJohan Hedberg if (!hdev->sent_cmd) 435342c6b129SJohan Hedberg return; 435442c6b129SJohan Hedberg 435542c6b129SJohan Hedberg sent = (void *) hdev->sent_cmd->data; 435642c6b129SJohan Hedberg opcode = __le16_to_cpu(sent->opcode); 435742c6b129SJohan Hedberg if (opcode == HCI_OP_RESET) 435842c6b129SJohan Hedberg return; 435942c6b129SJohan Hedberg 436042c6b129SJohan Hedberg skb = skb_clone(hdev->sent_cmd, GFP_KERNEL); 436142c6b129SJohan Hedberg if (!skb) 436242c6b129SJohan Hedberg return; 436342c6b129SJohan Hedberg 436442c6b129SJohan Hedberg skb_queue_head(&hdev->cmd_q, skb); 436542c6b129SJohan Hedberg queue_work(hdev->workqueue, &hdev->cmd_work); 436642c6b129SJohan Hedberg } 436742c6b129SJohan Hedberg 4368e6214487SJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status, 4369e6214487SJohan Hedberg hci_req_complete_t *req_complete, 4370e6214487SJohan Hedberg hci_req_complete_skb_t *req_complete_skb) 43719238f36aSJohan Hedberg { 43729238f36aSJohan Hedberg struct sk_buff *skb; 43739238f36aSJohan Hedberg unsigned long flags; 43749238f36aSJohan Hedberg 43759238f36aSJohan Hedberg BT_DBG("opcode 0x%04x status 0x%02x", opcode, status); 43769238f36aSJohan Hedberg 437742c6b129SJohan Hedberg /* If the completed command doesn't match the last one that was 437842c6b129SJohan Hedberg * sent we need to do special handling of it. 43799238f36aSJohan Hedberg */ 438042c6b129SJohan Hedberg if (!hci_sent_cmd_data(hdev, opcode)) { 438142c6b129SJohan Hedberg /* Some CSR based controllers generate a spontaneous 438242c6b129SJohan Hedberg * reset complete event during init and any pending 438342c6b129SJohan Hedberg * command will never be completed. In such a case we 438442c6b129SJohan Hedberg * need to resend whatever was the last sent 438542c6b129SJohan Hedberg * command. 438642c6b129SJohan Hedberg */ 438742c6b129SJohan Hedberg if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET) 438842c6b129SJohan Hedberg hci_resend_last(hdev); 438942c6b129SJohan Hedberg 43909238f36aSJohan Hedberg return; 439142c6b129SJohan Hedberg } 43929238f36aSJohan Hedberg 43939238f36aSJohan Hedberg /* If the command succeeded and there's still more commands in 43949238f36aSJohan Hedberg * this request the request is not yet complete. 43959238f36aSJohan Hedberg */ 43969238f36aSJohan Hedberg if (!status && !hci_req_is_complete(hdev)) 43979238f36aSJohan Hedberg return; 43989238f36aSJohan Hedberg 43999238f36aSJohan Hedberg /* If this was the last command in a request the complete 44009238f36aSJohan Hedberg * callback would be found in hdev->sent_cmd instead of the 44019238f36aSJohan Hedberg * command queue (hdev->cmd_q). 44029238f36aSJohan Hedberg */ 4403e6214487SJohan Hedberg if (bt_cb(hdev->sent_cmd)->req.complete) { 4404e6214487SJohan Hedberg *req_complete = bt_cb(hdev->sent_cmd)->req.complete; 4405e6214487SJohan Hedberg return; 44069238f36aSJohan Hedberg } 4407e6214487SJohan Hedberg 4408e6214487SJohan Hedberg if (bt_cb(hdev->sent_cmd)->req.complete_skb) { 4409e6214487SJohan Hedberg *req_complete_skb = bt_cb(hdev->sent_cmd)->req.complete_skb; 4410e6214487SJohan Hedberg return; 441153e21fbcSJohan Hedberg } 44129238f36aSJohan Hedberg 44139238f36aSJohan Hedberg /* Remove all pending commands belonging to this request */ 44149238f36aSJohan Hedberg spin_lock_irqsave(&hdev->cmd_q.lock, flags); 44159238f36aSJohan Hedberg while ((skb = __skb_dequeue(&hdev->cmd_q))) { 4416db6e3e8dSJohan Hedberg if (bt_cb(skb)->req.start) { 44179238f36aSJohan Hedberg __skb_queue_head(&hdev->cmd_q, skb); 44189238f36aSJohan Hedberg break; 44199238f36aSJohan Hedberg } 44209238f36aSJohan Hedberg 4421e6214487SJohan Hedberg *req_complete = bt_cb(skb)->req.complete; 4422e6214487SJohan Hedberg *req_complete_skb = bt_cb(skb)->req.complete_skb; 44239238f36aSJohan Hedberg kfree_skb(skb); 44249238f36aSJohan Hedberg } 44259238f36aSJohan Hedberg spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); 44269238f36aSJohan Hedberg } 44279238f36aSJohan Hedberg 4428b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work) 44291da177e4SLinus Torvalds { 4430b78752ccSMarcel Holtmann struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work); 44311da177e4SLinus Torvalds struct sk_buff *skb; 44321da177e4SLinus Torvalds 44331da177e4SLinus Torvalds BT_DBG("%s", hdev->name); 44341da177e4SLinus Torvalds 44351da177e4SLinus Torvalds while ((skb = skb_dequeue(&hdev->rx_q))) { 4436cd82e61cSMarcel Holtmann /* Send copy to monitor */ 4437cd82e61cSMarcel Holtmann hci_send_to_monitor(hdev, skb); 4438cd82e61cSMarcel Holtmann 44391da177e4SLinus Torvalds if (atomic_read(&hdev->promisc)) { 44401da177e4SLinus Torvalds /* Send copy to the sockets */ 4441470fe1b5SMarcel Holtmann hci_send_to_sock(hdev, skb); 44421da177e4SLinus Torvalds } 44431da177e4SLinus Torvalds 4444d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { 44451da177e4SLinus Torvalds kfree_skb(skb); 44461da177e4SLinus Torvalds continue; 44471da177e4SLinus Torvalds } 44481da177e4SLinus Torvalds 44491da177e4SLinus Torvalds if (test_bit(HCI_INIT, &hdev->flags)) { 44501da177e4SLinus Torvalds /* Don't process data packets in this states. */ 44510d48d939SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 44521da177e4SLinus Torvalds case HCI_ACLDATA_PKT: 44531da177e4SLinus Torvalds case HCI_SCODATA_PKT: 44541da177e4SLinus Torvalds kfree_skb(skb); 44551da177e4SLinus Torvalds continue; 44563ff50b79SStephen Hemminger } 44571da177e4SLinus Torvalds } 44581da177e4SLinus Torvalds 44591da177e4SLinus Torvalds /* Process frame */ 44600d48d939SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 44611da177e4SLinus Torvalds case HCI_EVENT_PKT: 4462b78752ccSMarcel Holtmann BT_DBG("%s Event packet", hdev->name); 44631da177e4SLinus Torvalds hci_event_packet(hdev, skb); 44641da177e4SLinus Torvalds break; 44651da177e4SLinus Torvalds 44661da177e4SLinus Torvalds case HCI_ACLDATA_PKT: 44671da177e4SLinus Torvalds BT_DBG("%s ACL data packet", hdev->name); 44681da177e4SLinus Torvalds hci_acldata_packet(hdev, skb); 44691da177e4SLinus Torvalds break; 44701da177e4SLinus Torvalds 44711da177e4SLinus Torvalds case HCI_SCODATA_PKT: 44721da177e4SLinus Torvalds BT_DBG("%s SCO data packet", hdev->name); 44731da177e4SLinus Torvalds hci_scodata_packet(hdev, skb); 44741da177e4SLinus Torvalds break; 44751da177e4SLinus Torvalds 44761da177e4SLinus Torvalds default: 44771da177e4SLinus Torvalds kfree_skb(skb); 44781da177e4SLinus Torvalds break; 44791da177e4SLinus Torvalds } 44801da177e4SLinus Torvalds } 44811da177e4SLinus Torvalds } 44821da177e4SLinus Torvalds 4483c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work) 44841da177e4SLinus Torvalds { 4485c347b765SGustavo F. Padovan struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work); 44861da177e4SLinus Torvalds struct sk_buff *skb; 44871da177e4SLinus Torvalds 44882104786bSAndrei Emeltchenko BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name, 44892104786bSAndrei Emeltchenko atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q)); 44901da177e4SLinus Torvalds 44911da177e4SLinus Torvalds /* Send queued commands */ 44925a08ecceSAndrei Emeltchenko if (atomic_read(&hdev->cmd_cnt)) { 44935a08ecceSAndrei Emeltchenko skb = skb_dequeue(&hdev->cmd_q); 44945a08ecceSAndrei Emeltchenko if (!skb) 44955a08ecceSAndrei Emeltchenko return; 44965a08ecceSAndrei Emeltchenko 44971da177e4SLinus Torvalds kfree_skb(hdev->sent_cmd); 44981da177e4SLinus Torvalds 4499a675d7f1SMarcel Holtmann hdev->sent_cmd = skb_clone(skb, GFP_KERNEL); 450070f23020SAndrei Emeltchenko if (hdev->sent_cmd) { 45011da177e4SLinus Torvalds atomic_dec(&hdev->cmd_cnt); 450257d17d70SMarcel Holtmann hci_send_frame(hdev, skb); 45037bdb8a5cSSzymon Janc if (test_bit(HCI_RESET, &hdev->flags)) 450465cc2b49SMarcel Holtmann cancel_delayed_work(&hdev->cmd_timer); 45057bdb8a5cSSzymon Janc else 450665cc2b49SMarcel Holtmann schedule_delayed_work(&hdev->cmd_timer, 450765cc2b49SMarcel Holtmann HCI_CMD_TIMEOUT); 45081da177e4SLinus Torvalds } else { 45091da177e4SLinus Torvalds skb_queue_head(&hdev->cmd_q, skb); 4510c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work); 45111da177e4SLinus Torvalds } 45121da177e4SLinus Torvalds } 45131da177e4SLinus Torvalds } 4514