Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | |
| 4 | Copyright (C) 2014 Intel Corporation |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License version 2 as |
| 8 | published by the Free Software Foundation; |
| 9 | |
| 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 11 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 13 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 14 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 15 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
| 19 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 20 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 21 | SOFTWARE IS DISCLAIMED. |
| 22 | */ |
| 23 | |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 24 | #include <linux/sched/signal.h> |
| 25 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 26 | #include <net/bluetooth/bluetooth.h> |
| 27 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 28 | #include <net/bluetooth/mgmt.h> |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 29 | |
| 30 | #include "smp.h" |
| 31 | #include "hci_request.h" |
Howard Chung | bf6a4e3 | 2021-01-22 16:36:17 +0800 | [diff] [blame] | 32 | #include "msft.h" |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 33 | #include "eir.h" |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 34 | |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 35 | #define HCI_REQ_DONE 0 |
| 36 | #define HCI_REQ_PEND 1 |
| 37 | #define HCI_REQ_CANCELED 2 |
| 38 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 39 | void hci_req_init(struct hci_request *req, struct hci_dev *hdev) |
| 40 | { |
| 41 | skb_queue_head_init(&req->cmd_q); |
| 42 | req->hdev = hdev; |
| 43 | req->err = 0; |
| 44 | } |
| 45 | |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 46 | void hci_req_purge(struct hci_request *req) |
| 47 | { |
| 48 | skb_queue_purge(&req->cmd_q); |
| 49 | } |
| 50 | |
João Paulo Rechi Vita | f80c5da | 2019-05-02 10:01:52 +0800 | [diff] [blame] | 51 | bool hci_req_status_pend(struct hci_dev *hdev) |
| 52 | { |
| 53 | return hdev->req_status == HCI_REQ_PEND; |
| 54 | } |
| 55 | |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 56 | static int req_run(struct hci_request *req, hci_req_complete_t complete, |
| 57 | hci_req_complete_skb_t complete_skb) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 58 | { |
| 59 | struct hci_dev *hdev = req->hdev; |
| 60 | struct sk_buff *skb; |
| 61 | unsigned long flags; |
| 62 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 63 | bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q)); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 64 | |
| 65 | /* If an error occurred during request building, remove all HCI |
| 66 | * commands queued on the HCI request queue. |
| 67 | */ |
| 68 | if (req->err) { |
| 69 | skb_queue_purge(&req->cmd_q); |
| 70 | return req->err; |
| 71 | } |
| 72 | |
| 73 | /* Do not allow empty requests */ |
| 74 | if (skb_queue_empty(&req->cmd_q)) |
| 75 | return -ENODATA; |
| 76 | |
| 77 | skb = skb_peek_tail(&req->cmd_q); |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 78 | if (complete) { |
| 79 | bt_cb(skb)->hci.req_complete = complete; |
| 80 | } else if (complete_skb) { |
| 81 | bt_cb(skb)->hci.req_complete_skb = complete_skb; |
| 82 | bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB; |
| 83 | } |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 84 | |
| 85 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 86 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); |
| 87 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 88 | |
| 89 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 94 | int hci_req_run(struct hci_request *req, hci_req_complete_t complete) |
| 95 | { |
| 96 | return req_run(req, complete, NULL); |
| 97 | } |
| 98 | |
| 99 | int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete) |
| 100 | { |
| 101 | return req_run(req, NULL, complete); |
| 102 | } |
| 103 | |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 104 | static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, |
| 105 | struct sk_buff *skb) |
| 106 | { |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 107 | bt_dev_dbg(hdev, "result 0x%2.2x", result); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 108 | |
| 109 | if (hdev->req_status == HCI_REQ_PEND) { |
| 110 | hdev->req_result = result; |
| 111 | hdev->req_status = HCI_REQ_DONE; |
| 112 | if (skb) |
| 113 | hdev->req_skb = skb_get(skb); |
| 114 | wake_up_interruptible(&hdev->req_wait_q); |
| 115 | } |
| 116 | } |
| 117 | |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 118 | void hci_req_sync_cancel(struct hci_dev *hdev, int err) |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 119 | { |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 120 | bt_dev_dbg(hdev, "err 0x%2.2x", err); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 121 | |
| 122 | if (hdev->req_status == HCI_REQ_PEND) { |
| 123 | hdev->req_result = err; |
| 124 | hdev->req_status = HCI_REQ_CANCELED; |
| 125 | wake_up_interruptible(&hdev->req_wait_q); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, |
| 130 | const void *param, u8 event, u32 timeout) |
| 131 | { |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 132 | struct hci_request req; |
| 133 | struct sk_buff *skb; |
| 134 | int err = 0; |
| 135 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 136 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 137 | |
| 138 | hci_req_init(&req, hdev); |
| 139 | |
| 140 | hci_req_add_ev(&req, opcode, plen, param, event); |
| 141 | |
| 142 | hdev->req_status = HCI_REQ_PEND; |
| 143 | |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 144 | err = hci_req_run_skb(&req, hci_req_sync_complete); |
John Keeping | 67d8cee | 2018-04-19 16:29:37 +0100 | [diff] [blame] | 145 | if (err < 0) |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 146 | return ERR_PTR(err); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 147 | |
John Keeping | 67d8cee | 2018-04-19 16:29:37 +0100 | [diff] [blame] | 148 | err = wait_event_interruptible_timeout(hdev->req_wait_q, |
| 149 | hdev->req_status != HCI_REQ_PEND, timeout); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 150 | |
John Keeping | 67d8cee | 2018-04-19 16:29:37 +0100 | [diff] [blame] | 151 | if (err == -ERESTARTSYS) |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 152 | return ERR_PTR(-EINTR); |
| 153 | |
| 154 | switch (hdev->req_status) { |
| 155 | case HCI_REQ_DONE: |
| 156 | err = -bt_to_errno(hdev->req_result); |
| 157 | break; |
| 158 | |
| 159 | case HCI_REQ_CANCELED: |
| 160 | err = -hdev->req_result; |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | err = -ETIMEDOUT; |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | hdev->req_status = hdev->req_result = 0; |
| 169 | skb = hdev->req_skb; |
| 170 | hdev->req_skb = NULL; |
| 171 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 172 | bt_dev_dbg(hdev, "end: err %d", err); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 173 | |
| 174 | if (err < 0) { |
| 175 | kfree_skb(skb); |
| 176 | return ERR_PTR(err); |
| 177 | } |
| 178 | |
| 179 | if (!skb) |
| 180 | return ERR_PTR(-ENODATA); |
| 181 | |
| 182 | return skb; |
| 183 | } |
| 184 | EXPORT_SYMBOL(__hci_cmd_sync_ev); |
| 185 | |
| 186 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, |
| 187 | const void *param, u32 timeout) |
| 188 | { |
| 189 | return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout); |
| 190 | } |
| 191 | EXPORT_SYMBOL(__hci_cmd_sync); |
| 192 | |
| 193 | /* Execute request and wait for completion. */ |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 194 | int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct hci_request *req, |
| 195 | unsigned long opt), |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 196 | unsigned long opt, u32 timeout, u8 *hci_status) |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 197 | { |
| 198 | struct hci_request req; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 199 | int err = 0; |
| 200 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 201 | bt_dev_dbg(hdev, "start"); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 202 | |
| 203 | hci_req_init(&req, hdev); |
| 204 | |
| 205 | hdev->req_status = HCI_REQ_PEND; |
| 206 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 207 | err = func(&req, opt); |
| 208 | if (err) { |
| 209 | if (hci_status) |
| 210 | *hci_status = HCI_ERROR_UNSPECIFIED; |
| 211 | return err; |
| 212 | } |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 213 | |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 214 | err = hci_req_run_skb(&req, hci_req_sync_complete); |
| 215 | if (err < 0) { |
| 216 | hdev->req_status = 0; |
| 217 | |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 218 | /* ENODATA means the HCI request command queue is empty. |
| 219 | * This can happen when a request with conditionals doesn't |
| 220 | * trigger any commands to be sent. This is normal behavior |
| 221 | * and should not trigger an error return. |
| 222 | */ |
Johan Hedberg | 568f44f | 2015-11-23 14:40:47 +0200 | [diff] [blame] | 223 | if (err == -ENODATA) { |
| 224 | if (hci_status) |
| 225 | *hci_status = 0; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 226 | return 0; |
Johan Hedberg | 568f44f | 2015-11-23 14:40:47 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (hci_status) |
| 230 | *hci_status = HCI_ERROR_UNSPECIFIED; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 231 | |
| 232 | return err; |
| 233 | } |
| 234 | |
John Keeping | 67d8cee | 2018-04-19 16:29:37 +0100 | [diff] [blame] | 235 | err = wait_event_interruptible_timeout(hdev->req_wait_q, |
| 236 | hdev->req_status != HCI_REQ_PEND, timeout); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 237 | |
John Keeping | 67d8cee | 2018-04-19 16:29:37 +0100 | [diff] [blame] | 238 | if (err == -ERESTARTSYS) |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 239 | return -EINTR; |
| 240 | |
| 241 | switch (hdev->req_status) { |
| 242 | case HCI_REQ_DONE: |
| 243 | err = -bt_to_errno(hdev->req_result); |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 244 | if (hci_status) |
| 245 | *hci_status = hdev->req_result; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case HCI_REQ_CANCELED: |
| 249 | err = -hdev->req_result; |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 250 | if (hci_status) |
| 251 | *hci_status = HCI_ERROR_UNSPECIFIED; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 252 | break; |
| 253 | |
| 254 | default: |
| 255 | err = -ETIMEDOUT; |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 256 | if (hci_status) |
| 257 | *hci_status = HCI_ERROR_UNSPECIFIED; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | |
Frederic Dalleau | 9afee94 | 2016-08-23 07:59:19 +0200 | [diff] [blame] | 261 | kfree_skb(hdev->req_skb); |
| 262 | hdev->req_skb = NULL; |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 263 | hdev->req_status = hdev->req_result = 0; |
| 264 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 265 | bt_dev_dbg(hdev, "end: err %d", err); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 266 | |
| 267 | return err; |
| 268 | } |
| 269 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 270 | int hci_req_sync(struct hci_dev *hdev, int (*req)(struct hci_request *req, |
| 271 | unsigned long opt), |
Johan Hedberg | 4ebeee2 | 2015-11-11 08:11:19 +0200 | [diff] [blame] | 272 | unsigned long opt, u32 timeout, u8 *hci_status) |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 273 | { |
| 274 | int ret; |
| 275 | |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 276 | /* Serialize all requests */ |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 277 | hci_req_sync_lock(hdev); |
Lin Ma | e2cb6b8 | 2021-04-12 19:17:57 +0800 | [diff] [blame] | 278 | /* check the state after obtaing the lock to protect the HCI_UP |
| 279 | * against any races from hci_dev_do_close when the controller |
| 280 | * gets removed. |
| 281 | */ |
| 282 | if (test_bit(HCI_UP, &hdev->flags)) |
| 283 | ret = __hci_req_sync(hdev, req, opt, timeout, hci_status); |
| 284 | else |
| 285 | ret = -ENETDOWN; |
Johan Hedberg | b504430 | 2015-11-10 09:44:55 +0200 | [diff] [blame] | 286 | hci_req_sync_unlock(hdev); |
Johan Hedberg | be91cd0 | 2015-11-10 09:44:54 +0200 | [diff] [blame] | 287 | |
| 288 | return ret; |
| 289 | } |
| 290 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 291 | struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen, |
| 292 | const void *param) |
| 293 | { |
| 294 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 295 | struct hci_command_hdr *hdr; |
| 296 | struct sk_buff *skb; |
| 297 | |
| 298 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 299 | if (!skb) |
| 300 | return NULL; |
| 301 | |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 302 | hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 303 | hdr->opcode = cpu_to_le16(opcode); |
| 304 | hdr->plen = plen; |
| 305 | |
| 306 | if (plen) |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 307 | skb_put_data(skb, param, plen); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 308 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 309 | bt_dev_dbg(hdev, "skb len %d", skb->len); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 310 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 311 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
| 312 | hci_skb_opcode(skb) = opcode; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 313 | |
| 314 | return skb; |
| 315 | } |
| 316 | |
| 317 | /* Queue a command to an asynchronous HCI request */ |
| 318 | void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, |
| 319 | const void *param, u8 event) |
| 320 | { |
| 321 | struct hci_dev *hdev = req->hdev; |
| 322 | struct sk_buff *skb; |
| 323 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 324 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 325 | |
| 326 | /* If an error occurred during request building, there is no point in |
| 327 | * queueing the HCI command. We can simply return. |
| 328 | */ |
| 329 | if (req->err) |
| 330 | return; |
| 331 | |
| 332 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 333 | if (!skb) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 334 | bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)", |
| 335 | opcode); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 336 | req->err = -ENOMEM; |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | if (skb_queue_empty(&req->cmd_q)) |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame] | 341 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 342 | |
Marcel Holtmann | 242c0eb | 2015-10-25 22:45:53 +0100 | [diff] [blame] | 343 | bt_cb(skb)->hci.req_event = event; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 344 | |
| 345 | skb_queue_tail(&req->cmd_q, skb); |
| 346 | } |
| 347 | |
| 348 | void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, |
| 349 | const void *param) |
| 350 | { |
| 351 | hci_req_add_ev(req, opcode, plen, param, 0); |
| 352 | } |
| 353 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 354 | void __hci_req_write_fast_connectable(struct hci_request *req, bool enable) |
| 355 | { |
| 356 | struct hci_dev *hdev = req->hdev; |
| 357 | struct hci_cp_write_page_scan_activity acp; |
| 358 | u8 type; |
| 359 | |
| 360 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 361 | return; |
| 362 | |
| 363 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 364 | return; |
| 365 | |
| 366 | if (enable) { |
| 367 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 368 | |
| 369 | /* 160 msec page scan interval */ |
| 370 | acp.interval = cpu_to_le16(0x0100); |
| 371 | } else { |
Alain Michaud | 10873f9 | 2020-06-11 02:01:56 +0000 | [diff] [blame] | 372 | type = hdev->def_page_scan_type; |
| 373 | acp.interval = cpu_to_le16(hdev->def_page_scan_int); |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Alain Michaud | 10873f9 | 2020-06-11 02:01:56 +0000 | [diff] [blame] | 376 | acp.window = cpu_to_le16(hdev->def_page_scan_window); |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 377 | |
| 378 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 379 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 380 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 381 | sizeof(acp), &acp); |
| 382 | |
| 383 | if (hdev->page_scan_type != type) |
| 384 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 385 | } |
| 386 | |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 387 | static void start_interleave_scan(struct hci_dev *hdev) |
| 388 | { |
| 389 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; |
| 390 | queue_delayed_work(hdev->req_workqueue, |
| 391 | &hdev->interleave_scan, 0); |
| 392 | } |
| 393 | |
| 394 | static bool is_interleave_scanning(struct hci_dev *hdev) |
| 395 | { |
| 396 | return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE; |
| 397 | } |
| 398 | |
| 399 | static void cancel_interleave_scan(struct hci_dev *hdev) |
| 400 | { |
| 401 | bt_dev_dbg(hdev, "cancelling interleave scan"); |
| 402 | |
| 403 | cancel_delayed_work_sync(&hdev->interleave_scan); |
| 404 | |
| 405 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE; |
| 406 | } |
| 407 | |
| 408 | /* Return true if interleave_scan wasn't started until exiting this function, |
| 409 | * otherwise, return false |
| 410 | */ |
| 411 | static bool __hci_update_interleaved_scan(struct hci_dev *hdev) |
| 412 | { |
Archie Pusaka | 58ceb1e | 2021-01-22 16:36:16 +0800 | [diff] [blame] | 413 | /* Do interleaved scan only if all of the following are true: |
| 414 | * - There is at least one ADV monitor |
| 415 | * - At least one pending LE connection or one device to be scanned for |
| 416 | * - Monitor offloading is not supported |
| 417 | * If so, we should alternate between allowlist scan and one without |
| 418 | * any filters to save power. |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 419 | */ |
| 420 | bool use_interleaving = hci_is_adv_monitoring(hdev) && |
| 421 | !(list_empty(&hdev->pend_le_conns) && |
Archie Pusaka | 58ceb1e | 2021-01-22 16:36:16 +0800 | [diff] [blame] | 422 | list_empty(&hdev->pend_le_reports)) && |
| 423 | hci_get_adv_monitor_offload_ext(hdev) == |
| 424 | HCI_ADV_MONITOR_EXT_NONE; |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 425 | bool is_interleaving = is_interleave_scanning(hdev); |
| 426 | |
| 427 | if (use_interleaving && !is_interleaving) { |
| 428 | start_interleave_scan(hdev); |
| 429 | bt_dev_dbg(hdev, "starting interleave scan"); |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | if (!use_interleaving && is_interleaving) |
| 434 | cancel_interleave_scan(hdev); |
| 435 | |
| 436 | return false; |
| 437 | } |
| 438 | |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 439 | /* This function controls the background scanning based on hdev->pend_le_conns |
| 440 | * list. If there are pending LE connection we start the background scanning, |
| 441 | * otherwise we stop it. |
| 442 | * |
| 443 | * This function requires the caller holds hdev->lock. |
| 444 | */ |
| 445 | static void __hci_update_background_scan(struct hci_request *req) |
| 446 | { |
| 447 | struct hci_dev *hdev = req->hdev; |
| 448 | |
| 449 | if (!test_bit(HCI_UP, &hdev->flags) || |
| 450 | test_bit(HCI_INIT, &hdev->flags) || |
| 451 | hci_dev_test_flag(hdev, HCI_SETUP) || |
| 452 | hci_dev_test_flag(hdev, HCI_CONFIG) || |
| 453 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || |
| 454 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
| 455 | return; |
| 456 | |
| 457 | /* No point in doing scanning if LE support hasn't been enabled */ |
| 458 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 459 | return; |
| 460 | |
| 461 | /* If discovery is active don't interfere with it */ |
| 462 | if (hdev->discovery.state != DISCOVERY_STOPPED) |
| 463 | return; |
| 464 | |
| 465 | /* Reset RSSI and UUID filters when starting background scanning |
| 466 | * since these filters are meant for service discovery only. |
| 467 | * |
| 468 | * The Start Discovery and Start Service Discovery operations |
| 469 | * ensure to set proper values for RSSI threshold and UUID |
| 470 | * filter list. So it is safe to just reset them here. |
| 471 | */ |
| 472 | hci_discovery_filter_clear(hdev); |
| 473 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 474 | bt_dev_dbg(hdev, "ADV monitoring is %s", |
| 475 | hci_is_adv_monitoring(hdev) ? "on" : "off"); |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 476 | |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 477 | if (list_empty(&hdev->pend_le_conns) && |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 478 | list_empty(&hdev->pend_le_reports) && |
| 479 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 480 | /* If there is no pending LE connections or devices |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 481 | * to be scanned for or no ADV monitors, we should stop the |
| 482 | * background scanning. |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 483 | */ |
| 484 | |
| 485 | /* If controller is not scanning we are done. */ |
| 486 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
| 487 | return; |
| 488 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 489 | hci_req_add_le_scan_disable(req, false); |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 490 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 491 | bt_dev_dbg(hdev, "stopping background scanning"); |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 492 | } else { |
| 493 | /* If there is at least one pending LE connection, we should |
| 494 | * keep the background scan running. |
| 495 | */ |
| 496 | |
| 497 | /* If controller is connecting, we should not start scanning |
| 498 | * since some controllers are not able to scan and connect at |
| 499 | * the same time. |
| 500 | */ |
| 501 | if (hci_lookup_le_connect(hdev)) |
| 502 | return; |
| 503 | |
| 504 | /* If controller is currently scanning, we stop it to ensure we |
| 505 | * don't miss any advertising (due to duplicates filter). |
| 506 | */ |
| 507 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 508 | hci_req_add_le_scan_disable(req, false); |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 509 | |
| 510 | hci_req_add_le_passive_scan(req); |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 511 | bt_dev_dbg(hdev, "starting background scanning"); |
Johan Hedberg | 196a5e9 | 2015-11-22 18:55:44 +0200 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 515 | void __hci_req_update_name(struct hci_request *req) |
| 516 | { |
| 517 | struct hci_dev *hdev = req->hdev; |
| 518 | struct hci_cp_write_local_name cp; |
| 519 | |
| 520 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
| 521 | |
| 522 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
| 523 | } |
| 524 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 525 | void __hci_req_update_eir(struct hci_request *req) |
| 526 | { |
| 527 | struct hci_dev *hdev = req->hdev; |
| 528 | struct hci_cp_write_eir cp; |
| 529 | |
| 530 | if (!hdev_is_powered(hdev)) |
| 531 | return; |
| 532 | |
| 533 | if (!lmp_ext_inq_capable(hdev)) |
| 534 | return; |
| 535 | |
| 536 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
| 537 | return; |
| 538 | |
| 539 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
| 540 | return; |
| 541 | |
| 542 | memset(&cp, 0, sizeof(cp)); |
| 543 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 544 | eir_create(hdev, cp.data); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 545 | |
| 546 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 547 | return; |
| 548 | |
| 549 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 550 | |
| 551 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 552 | } |
| 553 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 554 | void hci_req_add_le_scan_disable(struct hci_request *req, bool rpa_le_conn) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 555 | { |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 556 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 557 | |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 558 | if (hdev->scanning_paused) { |
| 559 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); |
| 560 | return; |
| 561 | } |
| 562 | |
Abhishek Pandit-Subedi | dce0a4b | 2020-12-04 11:14:31 +0800 | [diff] [blame] | 563 | if (hdev->suspended) |
| 564 | set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks); |
| 565 | |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 566 | if (use_ext_scan(hdev)) { |
| 567 | struct hci_cp_le_set_ext_scan_enable cp; |
| 568 | |
| 569 | memset(&cp, 0, sizeof(cp)); |
| 570 | cp.enable = LE_SCAN_DISABLE; |
| 571 | hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(cp), |
| 572 | &cp); |
| 573 | } else { |
| 574 | struct hci_cp_le_set_scan_enable cp; |
| 575 | |
| 576 | memset(&cp, 0, sizeof(cp)); |
| 577 | cp.enable = LE_SCAN_DISABLE; |
| 578 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
| 579 | } |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 580 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 581 | /* Disable address resolution */ |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 582 | if (use_ll_privacy(hdev) && |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 583 | hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) && |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 584 | hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION) && !rpa_le_conn) { |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 585 | __u8 enable = 0x00; |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 586 | |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 587 | hci_req_add(req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable); |
| 588 | } |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 591 | static void del_from_accept_list(struct hci_request *req, bdaddr_t *bdaddr, |
| 592 | u8 bdaddr_type) |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 593 | { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 594 | struct hci_cp_le_del_from_accept_list cp; |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 595 | |
| 596 | cp.bdaddr_type = bdaddr_type; |
| 597 | bacpy(&cp.bdaddr, bdaddr); |
| 598 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 599 | bt_dev_dbg(req->hdev, "Remove %pMR (0x%x) from accept list", &cp.bdaddr, |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 600 | cp.bdaddr_type); |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 601 | hci_req_add(req, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, sizeof(cp), &cp); |
Marcel Holtmann | 0eee35b | 2020-07-23 18:08:58 +0530 | [diff] [blame] | 602 | |
Sathish Narasimman | 1fb17df | 2020-10-29 13:18:21 +0530 | [diff] [blame] | 603 | if (use_ll_privacy(req->hdev) && |
| 604 | hci_dev_test_flag(req->hdev, HCI_ENABLE_LL_PRIVACY)) { |
Marcel Holtmann | 0eee35b | 2020-07-23 18:08:58 +0530 | [diff] [blame] | 605 | struct smp_irk *irk; |
| 606 | |
| 607 | irk = hci_find_irk_by_addr(req->hdev, bdaddr, bdaddr_type); |
| 608 | if (irk) { |
| 609 | struct hci_cp_le_del_from_resolv_list cp; |
| 610 | |
| 611 | cp.bdaddr_type = bdaddr_type; |
| 612 | bacpy(&cp.bdaddr, bdaddr); |
| 613 | |
| 614 | hci_req_add(req, HCI_OP_LE_DEL_FROM_RESOLV_LIST, |
| 615 | sizeof(cp), &cp); |
| 616 | } |
| 617 | } |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 620 | /* Adds connection to accept list if needed. On error, returns -1. */ |
| 621 | static int add_to_accept_list(struct hci_request *req, |
| 622 | struct hci_conn_params *params, u8 *num_entries, |
| 623 | bool allow_rpa) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 624 | { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 625 | struct hci_cp_le_add_to_accept_list cp; |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 626 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 627 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 628 | /* Already in accept list */ |
| 629 | if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr, |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 630 | params->addr_type)) |
| 631 | return 0; |
| 632 | |
| 633 | /* Select filter policy to accept all advertising */ |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 634 | if (*num_entries >= hdev->le_accept_list_size) |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 635 | return -1; |
| 636 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 637 | /* Accept list can not be used with RPAs */ |
Sathish Narasimman | 1fb17df | 2020-10-29 13:18:21 +0530 | [diff] [blame] | 638 | if (!allow_rpa && |
| 639 | !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) && |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 640 | hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) { |
| 641 | return -1; |
| 642 | } |
| 643 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 644 | /* During suspend, only wakeable devices can be in accept list */ |
Abhishek Pandit-Subedi | a1fc753 | 2020-06-17 16:39:10 +0200 | [diff] [blame] | 645 | if (hdev->suspended && !hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP, |
| 646 | params->current_flags)) |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 647 | return 0; |
| 648 | |
| 649 | *num_entries += 1; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 650 | cp.bdaddr_type = params->addr_type; |
| 651 | bacpy(&cp.bdaddr, ¶ms->addr); |
| 652 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 653 | bt_dev_dbg(hdev, "Add %pMR (0x%x) to accept list", &cp.bdaddr, |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 654 | cp.bdaddr_type); |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 655 | hci_req_add(req, HCI_OP_LE_ADD_TO_ACCEPT_LIST, sizeof(cp), &cp); |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 656 | |
Sathish Narasimman | 1fb17df | 2020-10-29 13:18:21 +0530 | [diff] [blame] | 657 | if (use_ll_privacy(hdev) && |
| 658 | hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) { |
Marcel Holtmann | 0eee35b | 2020-07-23 18:08:58 +0530 | [diff] [blame] | 659 | struct smp_irk *irk; |
| 660 | |
| 661 | irk = hci_find_irk_by_addr(hdev, ¶ms->addr, |
| 662 | params->addr_type); |
| 663 | if (irk) { |
| 664 | struct hci_cp_le_add_to_resolv_list cp; |
| 665 | |
| 666 | cp.bdaddr_type = params->addr_type; |
| 667 | bacpy(&cp.bdaddr, ¶ms->addr); |
| 668 | memcpy(cp.peer_irk, irk->val, 16); |
| 669 | |
| 670 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
| 671 | memcpy(cp.local_irk, hdev->irk, 16); |
| 672 | else |
| 673 | memset(cp.local_irk, 0, 16); |
| 674 | |
| 675 | hci_req_add(req, HCI_OP_LE_ADD_TO_RESOLV_LIST, |
| 676 | sizeof(cp), &cp); |
| 677 | } |
| 678 | } |
| 679 | |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 680 | return 0; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 683 | static u8 update_accept_list(struct hci_request *req) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 684 | { |
| 685 | struct hci_dev *hdev = req->hdev; |
| 686 | struct hci_conn_params *params; |
| 687 | struct bdaddr_list *b; |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 688 | u8 num_entries = 0; |
| 689 | bool pend_conn, pend_report; |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 690 | /* We allow usage of accept list even with RPAs in suspend. In the worst |
| 691 | * case, we won't be able to wake from devices that use the privacy1.2 |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 692 | * features. Additionally, once we support privacy1.2 and IRK |
| 693 | * offloading, we can update this to also check for those conditions. |
| 694 | */ |
| 695 | bool allow_rpa = hdev->suspended; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 696 | |
Sathish Narasimman | 8ce85ad | 2021-04-05 20:00:41 +0530 | [diff] [blame] | 697 | if (use_ll_privacy(hdev) && |
| 698 | hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 699 | allow_rpa = true; |
| 700 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 701 | /* Go through the current accept list programmed into the |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 702 | * controller one by one and check if that address is still |
| 703 | * in the list of pending connections or list of devices to |
| 704 | * report. If not present in either list, then queue the |
| 705 | * command to remove it from the controller. |
| 706 | */ |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 707 | list_for_each_entry(b, &hdev->le_accept_list, list) { |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 708 | pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns, |
| 709 | &b->bdaddr, |
| 710 | b->bdaddr_type); |
| 711 | pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports, |
| 712 | &b->bdaddr, |
| 713 | b->bdaddr_type); |
| 714 | |
| 715 | /* If the device is not likely to connect or report, |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 716 | * remove it from the accept list. |
Johan Hedberg | cff10ce | 2016-01-26 14:31:31 -0500 | [diff] [blame] | 717 | */ |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 718 | if (!pend_conn && !pend_report) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 719 | del_from_accept_list(req, &b->bdaddr, b->bdaddr_type); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 720 | continue; |
| 721 | } |
| 722 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 723 | /* Accept list can not be used with RPAs */ |
Sathish Narasimman | 1fb17df | 2020-10-29 13:18:21 +0530 | [diff] [blame] | 724 | if (!allow_rpa && |
| 725 | !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) && |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 726 | hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) { |
Johan Hedberg | cff10ce | 2016-01-26 14:31:31 -0500 | [diff] [blame] | 727 | return 0x00; |
| 728 | } |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 729 | |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 730 | num_entries++; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 733 | /* Since all no longer valid accept list entries have been |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 734 | * removed, walk through the list of pending connections |
| 735 | * and ensure that any new device gets programmed into |
| 736 | * the controller. |
| 737 | * |
| 738 | * If the list of the devices is larger than the list of |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 739 | * available accept list entries in the controller, then |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 740 | * just abort and return filer policy value to not use the |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 741 | * accept list. |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 742 | */ |
| 743 | list_for_each_entry(params, &hdev->pend_le_conns, action) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 744 | if (add_to_accept_list(req, params, &num_entries, allow_rpa)) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 745 | return 0x00; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /* After adding all new pending connections, walk through |
| 749 | * the list of pending reports and also add these to the |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 750 | * accept list if there is still space. Abort if space runs out. |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 751 | */ |
| 752 | list_for_each_entry(params, &hdev->pend_le_reports, action) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 753 | if (add_to_accept_list(req, params, &num_entries, allow_rpa)) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 754 | return 0x00; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 757 | /* Use the allowlist unless the following conditions are all true: |
| 758 | * - We are not currently suspending |
Archie Pusaka | 58ceb1e | 2021-01-22 16:36:16 +0800 | [diff] [blame] | 759 | * - There are 1 or more ADV monitors registered and it's not offloaded |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 760 | * - Interleaved scanning is not currently using the allowlist |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 761 | */ |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 762 | if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended && |
Archie Pusaka | 58ceb1e | 2021-01-22 16:36:16 +0800 | [diff] [blame] | 763 | hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE && |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 764 | hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST) |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 765 | return 0x00; |
| 766 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 767 | /* Select filter policy to use accept list */ |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 768 | return 0x01; |
| 769 | } |
| 770 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 771 | static bool scan_use_rpa(struct hci_dev *hdev) |
| 772 | { |
| 773 | return hci_dev_test_flag(hdev, HCI_PRIVACY); |
| 774 | } |
| 775 | |
Jaganath Kanakkassery | 3baef81 | 2018-07-06 17:05:27 +0530 | [diff] [blame] | 776 | static void hci_req_start_scan(struct hci_request *req, u8 type, u16 interval, |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 777 | u16 window, u8 own_addr_type, u8 filter_policy, |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 778 | bool filter_dup, bool addr_resolv) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 779 | { |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 780 | struct hci_dev *hdev = req->hdev; |
Jaganath Kanakkassery | 3baef81 | 2018-07-06 17:05:27 +0530 | [diff] [blame] | 781 | |
Abhishek Pandit-Subedi | 3a0377d | 2020-06-24 11:34:19 -0700 | [diff] [blame] | 782 | if (hdev->scanning_paused) { |
| 783 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); |
| 784 | return; |
| 785 | } |
| 786 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 787 | if (use_ll_privacy(hdev) && |
| 788 | hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) && |
| 789 | addr_resolv) { |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 790 | u8 enable = 0x01; |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 791 | |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 792 | hci_req_add(req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable); |
| 793 | } |
| 794 | |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 795 | /* Use ext scanning if set ext scan param and ext scan enable is |
| 796 | * supported |
| 797 | */ |
| 798 | if (use_ext_scan(hdev)) { |
| 799 | struct hci_cp_le_set_ext_scan_params *ext_param_cp; |
| 800 | struct hci_cp_le_set_ext_scan_enable ext_enable_cp; |
| 801 | struct hci_cp_le_scan_phy_params *phy_params; |
Jaganath Kanakkassery | 45bdd86 | 2018-07-19 17:09:37 +0530 | [diff] [blame] | 802 | u8 data[sizeof(*ext_param_cp) + sizeof(*phy_params) * 2]; |
| 803 | u32 plen; |
Jaganath Kanakkassery | 3baef81 | 2018-07-06 17:05:27 +0530 | [diff] [blame] | 804 | |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 805 | ext_param_cp = (void *)data; |
| 806 | phy_params = (void *)ext_param_cp->data; |
| 807 | |
| 808 | memset(ext_param_cp, 0, sizeof(*ext_param_cp)); |
| 809 | ext_param_cp->own_addr_type = own_addr_type; |
| 810 | ext_param_cp->filter_policy = filter_policy; |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 811 | |
Jaganath Kanakkassery | 45bdd86 | 2018-07-19 17:09:37 +0530 | [diff] [blame] | 812 | plen = sizeof(*ext_param_cp); |
| 813 | |
| 814 | if (scan_1m(hdev) || scan_2m(hdev)) { |
| 815 | ext_param_cp->scanning_phys |= LE_SCAN_PHY_1M; |
| 816 | |
| 817 | memset(phy_params, 0, sizeof(*phy_params)); |
| 818 | phy_params->type = type; |
| 819 | phy_params->interval = cpu_to_le16(interval); |
| 820 | phy_params->window = cpu_to_le16(window); |
| 821 | |
| 822 | plen += sizeof(*phy_params); |
| 823 | phy_params++; |
| 824 | } |
| 825 | |
| 826 | if (scan_coded(hdev)) { |
| 827 | ext_param_cp->scanning_phys |= LE_SCAN_PHY_CODED; |
| 828 | |
| 829 | memset(phy_params, 0, sizeof(*phy_params)); |
| 830 | phy_params->type = type; |
| 831 | phy_params->interval = cpu_to_le16(interval); |
| 832 | phy_params->window = cpu_to_le16(window); |
| 833 | |
| 834 | plen += sizeof(*phy_params); |
| 835 | phy_params++; |
| 836 | } |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 837 | |
| 838 | hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_PARAMS, |
Jaganath Kanakkassery | 45bdd86 | 2018-07-19 17:09:37 +0530 | [diff] [blame] | 839 | plen, ext_param_cp); |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 840 | |
| 841 | memset(&ext_enable_cp, 0, sizeof(ext_enable_cp)); |
| 842 | ext_enable_cp.enable = LE_SCAN_ENABLE; |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 843 | ext_enable_cp.filter_dup = filter_dup; |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 844 | |
| 845 | hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE, |
| 846 | sizeof(ext_enable_cp), &ext_enable_cp); |
| 847 | } else { |
| 848 | struct hci_cp_le_set_scan_param param_cp; |
| 849 | struct hci_cp_le_set_scan_enable enable_cp; |
| 850 | |
| 851 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 852 | param_cp.type = type; |
| 853 | param_cp.interval = cpu_to_le16(interval); |
| 854 | param_cp.window = cpu_to_le16(window); |
| 855 | param_cp.own_address_type = own_addr_type; |
| 856 | param_cp.filter_policy = filter_policy; |
| 857 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 858 | ¶m_cp); |
| 859 | |
| 860 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 861 | enable_cp.enable = LE_SCAN_ENABLE; |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 862 | enable_cp.filter_dup = filter_dup; |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 863 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 864 | &enable_cp); |
| 865 | } |
Jaganath Kanakkassery | 3baef81 | 2018-07-06 17:05:27 +0530 | [diff] [blame] | 866 | } |
| 867 | |
Alain Michaud | 9a9373f | 2020-07-31 01:05:34 +0000 | [diff] [blame] | 868 | /* Returns true if an le connection is in the scanning state */ |
| 869 | static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev) |
| 870 | { |
| 871 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 872 | struct hci_conn *c; |
| 873 | |
| 874 | rcu_read_lock(); |
| 875 | |
| 876 | list_for_each_entry_rcu(c, &h->list, list) { |
| 877 | if (c->type == LE_LINK && c->state == BT_CONNECT && |
| 878 | test_bit(HCI_CONN_SCANNING, &c->flags)) { |
| 879 | rcu_read_unlock(); |
| 880 | return true; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | rcu_read_unlock(); |
| 885 | |
| 886 | return false; |
| 887 | } |
| 888 | |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 889 | /* Ensure to call hci_req_add_le_scan_disable() first to disable the |
| 890 | * controller based address resolution to be able to reconfigure |
| 891 | * resolving list. |
| 892 | */ |
Jaganath Kanakkassery | 3baef81 | 2018-07-06 17:05:27 +0530 | [diff] [blame] | 893 | void hci_req_add_le_passive_scan(struct hci_request *req) |
| 894 | { |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 895 | struct hci_dev *hdev = req->hdev; |
| 896 | u8 own_addr_type; |
| 897 | u8 filter_policy; |
Abhishek Pandit-Subedi | aaebf8e | 2020-05-12 19:09:32 -0700 | [diff] [blame] | 898 | u16 window, interval; |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 899 | /* Default is to enable duplicates filter */ |
| 900 | u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 901 | /* Background scanning should run with address resolution */ |
| 902 | bool addr_resolv = true; |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 903 | |
| 904 | if (hdev->scanning_paused) { |
| 905 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); |
| 906 | return; |
| 907 | } |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 908 | |
| 909 | /* Set require_privacy to false since no SCAN_REQ are send |
| 910 | * during passive scanning. Not using an non-resolvable address |
| 911 | * here is important so that peer devices using direct |
| 912 | * advertising with our address will be correctly reported |
| 913 | * by the controller. |
| 914 | */ |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 915 | if (hci_update_random_address(req, false, scan_use_rpa(hdev), |
| 916 | &own_addr_type)) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 917 | return; |
| 918 | |
Howard Chung | 80af16a | 2020-11-26 12:22:25 +0800 | [diff] [blame] | 919 | if (hdev->enable_advmon_interleave_scan && |
| 920 | __hci_update_interleaved_scan(hdev)) |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 921 | return; |
| 922 | |
| 923 | bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state); |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 924 | /* Adding or removing entries from the accept list must |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 925 | * happen before enabling scanning. The controller does |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 926 | * not allow accept list modification while scanning. |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 927 | */ |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 928 | filter_policy = update_accept_list(req); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 929 | |
| 930 | /* When the controller is using random resolvable addresses and |
| 931 | * with that having LE privacy enabled, then controllers with |
| 932 | * Extended Scanner Filter Policies support can now enable support |
| 933 | * for handling directed advertising. |
| 934 | * |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 935 | * So instead of using filter polices 0x00 (no accept list) |
| 936 | * and 0x01 (accept list enabled) use the new filter policies |
| 937 | * 0x02 (no accept list) and 0x03 (accept list enabled). |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 938 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 939 | if (hci_dev_test_flag(hdev, HCI_PRIVACY) && |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 940 | (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) |
| 941 | filter_policy |= 0x02; |
| 942 | |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 943 | if (hdev->suspended) { |
Alain Michaud | 10873f9 | 2020-06-11 02:01:56 +0000 | [diff] [blame] | 944 | window = hdev->le_scan_window_suspend; |
| 945 | interval = hdev->le_scan_int_suspend; |
Abhishek Pandit-Subedi | 295fa2a | 2020-12-07 16:12:54 -0800 | [diff] [blame] | 946 | |
| 947 | set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks); |
Alain Michaud | 9a9373f | 2020-07-31 01:05:34 +0000 | [diff] [blame] | 948 | } else if (hci_is_le_conn_scanning(hdev)) { |
| 949 | window = hdev->le_scan_window_connect; |
| 950 | interval = hdev->le_scan_int_connect; |
Howard Chung | 291f0c5 | 2020-09-18 11:11:49 +0800 | [diff] [blame] | 951 | } else if (hci_is_adv_monitoring(hdev)) { |
| 952 | window = hdev->le_scan_window_adv_monitor; |
| 953 | interval = hdev->le_scan_int_adv_monitor; |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 954 | |
| 955 | /* Disable duplicates filter when scanning for advertisement |
| 956 | * monitor for the following reasons. |
| 957 | * |
| 958 | * For HW pattern filtering (ex. MSFT), Realtek and Qualcomm |
| 959 | * controllers ignore RSSI_Sampling_Period when the duplicates |
| 960 | * filter is enabled. |
| 961 | * |
| 962 | * For SW pattern filtering, when we're not doing interleaved |
| 963 | * scanning, it is necessary to disable duplicates filter, |
| 964 | * otherwise hosts can only receive one advertisement and it's |
| 965 | * impossible to know if a peer is still in range. |
| 966 | */ |
| 967 | filter_dup = LE_SCAN_FILTER_DUP_DISABLE; |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 968 | } else { |
| 969 | window = hdev->le_scan_window; |
| 970 | interval = hdev->le_scan_interval; |
| 971 | } |
| 972 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 973 | bt_dev_dbg(hdev, "LE passive scan with accept list = %d", |
| 974 | filter_policy); |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 975 | hci_req_start_scan(req, LE_SCAN_PASSIVE, interval, window, |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 976 | own_addr_type, filter_policy, filter_dup, |
| 977 | addr_resolv); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 980 | static void hci_req_clear_event_filter(struct hci_request *req) |
| 981 | { |
| 982 | struct hci_cp_set_event_filter f; |
| 983 | |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 984 | if (!hci_dev_test_flag(req->hdev, HCI_BREDR_ENABLED)) |
| 985 | return; |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 986 | |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 987 | if (hci_dev_test_flag(req->hdev, HCI_EVENT_FILTER_CONFIGURED)) { |
| 988 | memset(&f, 0, sizeof(f)); |
| 989 | f.flt_type = HCI_FLT_CLEAR_ALL; |
| 990 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &f); |
| 991 | } |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static void hci_req_set_event_filter(struct hci_request *req) |
| 995 | { |
Abhishek Pandit-Subedi | 7a92906 | 2020-06-17 16:39:09 +0200 | [diff] [blame] | 996 | struct bdaddr_list_with_flags *b; |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 997 | struct hci_cp_set_event_filter f; |
| 998 | struct hci_dev *hdev = req->hdev; |
Abhishek Pandit-Subedi | 7a92906 | 2020-06-17 16:39:09 +0200 | [diff] [blame] | 999 | u8 scan = SCAN_DISABLED; |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1000 | bool scanning = test_bit(HCI_PSCAN, &hdev->flags); |
| 1001 | |
| 1002 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1003 | return; |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1004 | |
| 1005 | /* Always clear event filter when starting */ |
| 1006 | hci_req_clear_event_filter(req); |
| 1007 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 1008 | list_for_each_entry(b, &hdev->accept_list, list) { |
Abhishek Pandit-Subedi | 7a92906 | 2020-06-17 16:39:09 +0200 | [diff] [blame] | 1009 | if (!hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP, |
| 1010 | b->current_flags)) |
| 1011 | continue; |
| 1012 | |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1013 | memset(&f, 0, sizeof(f)); |
| 1014 | bacpy(&f.addr_conn_flt.bdaddr, &b->bdaddr); |
| 1015 | f.flt_type = HCI_FLT_CONN_SETUP; |
| 1016 | f.cond_type = HCI_CONN_SETUP_ALLOW_BDADDR; |
| 1017 | f.addr_conn_flt.auto_accept = HCI_CONN_SETUP_AUTO_ON; |
| 1018 | |
| 1019 | bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr); |
| 1020 | hci_req_add(req, HCI_OP_SET_EVENT_FLT, sizeof(f), &f); |
Abhishek Pandit-Subedi | 7a92906 | 2020-06-17 16:39:09 +0200 | [diff] [blame] | 1021 | scan = SCAN_PAGE; |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1024 | if (scan && !scanning) { |
Abhishek Pandit-Subedi | dce0a4b | 2020-12-04 11:14:31 +0800 | [diff] [blame] | 1025 | set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks); |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1026 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1027 | } else if (!scan && scanning) { |
Abhishek Pandit-Subedi | dce0a4b | 2020-12-04 11:14:31 +0800 | [diff] [blame] | 1028 | set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks); |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1029 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1030 | } |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1033 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1034 | { |
| 1035 | if (hdev->adv_instance_timeout) { |
| 1036 | hdev->adv_instance_timeout = 0; |
| 1037 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /* This function requires the caller holds hdev->lock */ |
Daniel Winkler | 2943d8e | 2020-11-06 15:20:19 -0800 | [diff] [blame] | 1042 | void __hci_req_pause_adv_instances(struct hci_request *req) |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1043 | { |
Daniel Winkler | 2943d8e | 2020-11-06 15:20:19 -0800 | [diff] [blame] | 1044 | bt_dev_dbg(req->hdev, "Pausing advertising instances"); |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* Call to disable any advertisements active on the controller. |
| 1047 | * This will succeed even if no advertisements are configured. |
| 1048 | */ |
| 1049 | __hci_req_disable_advertising(req); |
| 1050 | |
| 1051 | /* If we are using software rotation, pause the loop */ |
| 1052 | if (!ext_adv_capable(req->hdev)) |
| 1053 | cancel_adv_timeout(req->hdev); |
| 1054 | } |
| 1055 | |
| 1056 | /* This function requires the caller holds hdev->lock */ |
Daniel Winkler | 2943d8e | 2020-11-06 15:20:19 -0800 | [diff] [blame] | 1057 | static void __hci_req_resume_adv_instances(struct hci_request *req) |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1058 | { |
| 1059 | struct adv_info *adv; |
| 1060 | |
| 1061 | bt_dev_dbg(req->hdev, "Resuming advertising instances"); |
| 1062 | |
| 1063 | if (ext_adv_capable(req->hdev)) { |
| 1064 | /* Call for each tracked instance to be re-enabled */ |
| 1065 | list_for_each_entry(adv, &req->hdev->adv_instances, list) { |
| 1066 | __hci_req_enable_ext_advertising(req, |
| 1067 | adv->instance); |
| 1068 | } |
| 1069 | |
| 1070 | } else { |
| 1071 | /* Schedule for most recent instance to be restarted and begin |
| 1072 | * the software rotation loop |
| 1073 | */ |
| 1074 | __hci_req_schedule_adv_instance(req, |
| 1075 | req->hdev->cur_adv_instance, |
| 1076 | true); |
| 1077 | } |
| 1078 | } |
| 1079 | |
Daniel Winkler | 2943d8e | 2020-11-06 15:20:19 -0800 | [diff] [blame] | 1080 | /* This function requires the caller holds hdev->lock */ |
| 1081 | int hci_req_resume_adv_instances(struct hci_dev *hdev) |
| 1082 | { |
| 1083 | struct hci_request req; |
| 1084 | |
| 1085 | hci_req_init(&req, hdev); |
| 1086 | __hci_req_resume_adv_instances(&req); |
| 1087 | |
| 1088 | return hci_req_run(&req, NULL); |
| 1089 | } |
| 1090 | |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1091 | static void suspend_req_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 1092 | { |
| 1093 | bt_dev_dbg(hdev, "Request complete opcode=0x%x, status=0x%x", opcode, |
| 1094 | status); |
Abhishek Pandit-Subedi | 295fa2a | 2020-12-07 16:12:54 -0800 | [diff] [blame] | 1095 | if (test_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks) || |
| 1096 | test_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks)) { |
| 1097 | clear_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks); |
| 1098 | clear_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks); |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1099 | wake_up(&hdev->suspend_wait_q); |
| 1100 | } |
Howard Chung | bf6a4e3 | 2021-01-22 16:36:17 +0800 | [diff] [blame] | 1101 | |
| 1102 | if (test_bit(SUSPEND_SET_ADV_FILTER, hdev->suspend_tasks)) { |
| 1103 | clear_bit(SUSPEND_SET_ADV_FILTER, hdev->suspend_tasks); |
| 1104 | wake_up(&hdev->suspend_wait_q); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | static void hci_req_add_set_adv_filter_enable(struct hci_request *req, |
| 1109 | bool enable) |
| 1110 | { |
| 1111 | struct hci_dev *hdev = req->hdev; |
| 1112 | |
| 1113 | switch (hci_get_adv_monitor_offload_ext(hdev)) { |
| 1114 | case HCI_ADV_MONITOR_EXT_MSFT: |
| 1115 | msft_req_add_set_filter_enable(req, enable); |
| 1116 | break; |
| 1117 | default: |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | /* No need to block when enabling since it's on resume path */ |
| 1122 | if (hdev->suspended && !enable) |
| 1123 | set_bit(SUSPEND_SET_ADV_FILTER, hdev->suspend_tasks); |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Abhishek Pandit-Subedi | 9952d90 | 2020-03-11 08:54:00 -0700 | [diff] [blame] | 1126 | /* Call with hci_dev_lock */ |
| 1127 | void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next) |
| 1128 | { |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1129 | int old_state; |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1130 | struct hci_conn *conn; |
| 1131 | struct hci_request req; |
| 1132 | u8 page_scan; |
| 1133 | int disconnect_counter; |
| 1134 | |
Abhishek Pandit-Subedi | 9952d90 | 2020-03-11 08:54:00 -0700 | [diff] [blame] | 1135 | if (next == hdev->suspend_state) { |
| 1136 | bt_dev_dbg(hdev, "Same state before and after: %d", next); |
| 1137 | goto done; |
| 1138 | } |
| 1139 | |
| 1140 | hdev->suspend_state = next; |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1141 | hci_req_init(&req, hdev); |
| 1142 | |
| 1143 | if (next == BT_SUSPEND_DISCONNECT) { |
| 1144 | /* Mark device as suspended */ |
| 1145 | hdev->suspended = true; |
| 1146 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1147 | /* Pause discovery if not already stopped */ |
| 1148 | old_state = hdev->discovery.state; |
| 1149 | if (old_state != DISCOVERY_STOPPED) { |
| 1150 | set_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks); |
| 1151 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1152 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 1153 | } |
| 1154 | |
| 1155 | hdev->discovery_paused = true; |
| 1156 | hdev->discovery_old_state = old_state; |
| 1157 | |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1158 | /* Stop directed advertising */ |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1159 | old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING); |
| 1160 | if (old_state) { |
| 1161 | set_bit(SUSPEND_PAUSE_ADVERTISING, hdev->suspend_tasks); |
| 1162 | cancel_delayed_work(&hdev->discov_off); |
| 1163 | queue_delayed_work(hdev->req_workqueue, |
| 1164 | &hdev->discov_off, 0); |
| 1165 | } |
| 1166 | |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1167 | /* Pause other advertisements */ |
| 1168 | if (hdev->adv_instance_cnt) |
Daniel Winkler | 2943d8e | 2020-11-06 15:20:19 -0800 | [diff] [blame] | 1169 | __hci_req_pause_adv_instances(&req); |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1170 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1171 | hdev->advertising_paused = true; |
| 1172 | hdev->advertising_old_state = old_state; |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1173 | |
| 1174 | /* Disable page scan if enabled */ |
| 1175 | if (test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1176 | page_scan = SCAN_DISABLED; |
| 1177 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, |
| 1178 | &page_scan); |
| 1179 | set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks); |
| 1180 | } |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1181 | |
Manish Mandlik | 6fb00d4 | 2020-06-01 18:42:51 -0700 | [diff] [blame] | 1182 | /* Disable LE passive scan if enabled */ |
Howard Chung | 36afe87 | 2020-11-26 12:22:22 +0800 | [diff] [blame] | 1183 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
| 1184 | cancel_interleave_scan(hdev); |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 1185 | hci_req_add_le_scan_disable(&req, false); |
Howard Chung | 36afe87 | 2020-11-26 12:22:22 +0800 | [diff] [blame] | 1186 | } |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 1187 | |
Howard Chung | bf6a4e3 | 2021-01-22 16:36:17 +0800 | [diff] [blame] | 1188 | /* Disable advertisement filters */ |
| 1189 | hci_req_add_set_adv_filter_enable(&req, false); |
| 1190 | |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1191 | /* Prevent disconnects from causing scanning to be re-enabled */ |
| 1192 | hdev->scanning_paused = true; |
| 1193 | |
| 1194 | /* Run commands before disconnecting */ |
| 1195 | hci_req_run(&req, suspend_req_complete); |
| 1196 | |
| 1197 | disconnect_counter = 0; |
| 1198 | /* Soft disconnect everything (power off) */ |
| 1199 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1200 | hci_disconnect(conn, HCI_ERROR_REMOTE_POWER_OFF); |
| 1201 | disconnect_counter++; |
| 1202 | } |
| 1203 | |
| 1204 | if (disconnect_counter > 0) { |
| 1205 | bt_dev_dbg(hdev, |
| 1206 | "Had %d disconnects. Will wait on them", |
| 1207 | disconnect_counter); |
| 1208 | set_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks); |
| 1209 | } |
Abhishek Pandit-Subedi | 0d2c982 | 2020-05-12 19:19:25 -0700 | [diff] [blame] | 1210 | } else if (next == BT_SUSPEND_CONFIGURE_WAKE) { |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1211 | /* Unpause to take care of updating scanning params */ |
| 1212 | hdev->scanning_paused = false; |
| 1213 | /* Enable event filter for paired devices */ |
| 1214 | hci_req_set_event_filter(&req); |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 1215 | /* Enable passive scan at lower duty cycle */ |
Abhishek Pandit-Subedi | 295fa2a | 2020-12-07 16:12:54 -0800 | [diff] [blame] | 1216 | __hci_update_background_scan(&req); |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1217 | /* Pause scan changes again. */ |
| 1218 | hdev->scanning_paused = true; |
| 1219 | hci_req_run(&req, suspend_req_complete); |
| 1220 | } else { |
| 1221 | hdev->suspended = false; |
| 1222 | hdev->scanning_paused = false; |
| 1223 | |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1224 | /* Clear any event filters and restore scan state */ |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1225 | hci_req_clear_event_filter(&req); |
Abhishek Pandit-Subedi | e5b0ad6 | 2021-03-03 08:34:04 -0800 | [diff] [blame] | 1226 | __hci_req_update_scan(&req); |
| 1227 | |
Abhishek Pandit-Subedi | dd522a7 | 2020-03-11 08:54:02 -0700 | [diff] [blame] | 1228 | /* Reset passive/background scanning to normal */ |
Abhishek Pandit-Subedi | 295fa2a | 2020-12-07 16:12:54 -0800 | [diff] [blame] | 1229 | __hci_update_background_scan(&req); |
Howard Chung | bf6a4e3 | 2021-01-22 16:36:17 +0800 | [diff] [blame] | 1230 | /* Enable all of the advertisement filters */ |
| 1231 | hci_req_add_set_adv_filter_enable(&req, true); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1232 | |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1233 | /* Unpause directed advertising */ |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1234 | hdev->advertising_paused = false; |
| 1235 | if (hdev->advertising_old_state) { |
| 1236 | set_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 1237 | hdev->suspend_tasks); |
| 1238 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
| 1239 | queue_work(hdev->req_workqueue, |
| 1240 | &hdev->discoverable_update); |
| 1241 | hdev->advertising_old_state = 0; |
| 1242 | } |
| 1243 | |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1244 | /* Resume other advertisements */ |
| 1245 | if (hdev->adv_instance_cnt) |
Daniel Winkler | 2943d8e | 2020-11-06 15:20:19 -0800 | [diff] [blame] | 1246 | __hci_req_resume_adv_instances(&req); |
Daniel Winkler | 5327447 | 2020-09-15 14:14:27 -0700 | [diff] [blame] | 1247 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1248 | /* Unpause discovery */ |
| 1249 | hdev->discovery_paused = false; |
| 1250 | if (hdev->discovery_old_state != DISCOVERY_STOPPED && |
| 1251 | hdev->discovery_old_state != DISCOVERY_STOPPING) { |
| 1252 | set_bit(SUSPEND_UNPAUSE_DISCOVERY, hdev->suspend_tasks); |
| 1253 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
| 1254 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 1255 | } |
| 1256 | |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 1257 | hci_req_run(&req, suspend_req_complete); |
| 1258 | } |
| 1259 | |
| 1260 | hdev->suspend_state = next; |
Abhishek Pandit-Subedi | 9952d90 | 2020-03-11 08:54:00 -0700 | [diff] [blame] | 1261 | |
| 1262 | done: |
| 1263 | clear_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks); |
| 1264 | wake_up(&hdev->suspend_wait_q); |
| 1265 | } |
| 1266 | |
Luiz Augusto von Dentz | aeeae47 | 2020-11-13 16:44:34 -0800 | [diff] [blame] | 1267 | static bool adv_cur_instance_is_scannable(struct hci_dev *hdev) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1268 | { |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1269 | return hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | void __hci_req_disable_advertising(struct hci_request *req) |
| 1273 | { |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 1274 | if (ext_adv_capable(req->hdev)) { |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 1275 | __hci_req_disable_ext_adv_instance(req, 0x00); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1276 | |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 1277 | } else { |
| 1278 | u8 enable = 0x00; |
| 1279 | |
| 1280 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1281 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1282 | } |
| 1283 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1284 | static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags) |
| 1285 | { |
| 1286 | /* If privacy is not enabled don't use RPA */ |
| 1287 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) |
| 1288 | return false; |
| 1289 | |
| 1290 | /* If basic privacy mode is enabled use RPA */ |
| 1291 | if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1292 | return true; |
| 1293 | |
| 1294 | /* If limited privacy mode is enabled don't use RPA if we're |
| 1295 | * both discoverable and bondable. |
| 1296 | */ |
| 1297 | if ((flags & MGMT_ADV_FLAG_DISCOV) && |
| 1298 | hci_dev_test_flag(hdev, HCI_BONDABLE)) |
| 1299 | return false; |
| 1300 | |
| 1301 | /* We're neither bondable nor discoverable in the limited |
| 1302 | * privacy mode, therefore use RPA. |
| 1303 | */ |
| 1304 | return true; |
| 1305 | } |
| 1306 | |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1307 | static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable) |
| 1308 | { |
| 1309 | /* If there is no connection we are OK to advertise. */ |
| 1310 | if (hci_conn_num(hdev, LE_LINK) == 0) |
| 1311 | return true; |
| 1312 | |
Archie Pusaka | 39bc74c | 2021-06-04 16:26:26 +0800 | [diff] [blame] | 1313 | /* Check le_states if there is any connection in peripheral role. */ |
| 1314 | if (hdev->conn_hash.le_num_peripheral > 0) { |
| 1315 | /* Peripheral connection state and non connectable mode bit 20. |
| 1316 | */ |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1317 | if (!connectable && !(hdev->le_states[2] & 0x10)) |
| 1318 | return false; |
| 1319 | |
Archie Pusaka | 39bc74c | 2021-06-04 16:26:26 +0800 | [diff] [blame] | 1320 | /* Peripheral connection state and connectable mode bit 38 |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1321 | * and scannable bit 21. |
| 1322 | */ |
Łukasz Rymanowski | 62ebdc2 | 2018-02-09 18:26:02 +0100 | [diff] [blame] | 1323 | if (connectable && (!(hdev->le_states[4] & 0x40) || |
| 1324 | !(hdev->le_states[2] & 0x20))) |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1325 | return false; |
| 1326 | } |
| 1327 | |
Archie Pusaka | 39bc74c | 2021-06-04 16:26:26 +0800 | [diff] [blame] | 1328 | /* Check le_states if there is any connection in central role. */ |
| 1329 | if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) { |
| 1330 | /* Central connection state and non connectable mode bit 18. */ |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1331 | if (!connectable && !(hdev->le_states[2] & 0x02)) |
| 1332 | return false; |
| 1333 | |
Archie Pusaka | 39bc74c | 2021-06-04 16:26:26 +0800 | [diff] [blame] | 1334 | /* Central connection state and connectable mode bit 35 and |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1335 | * scannable 19. |
| 1336 | */ |
Łukasz Rymanowski | 62ebdc2 | 2018-02-09 18:26:02 +0100 | [diff] [blame] | 1337 | if (connectable && (!(hdev->le_states[4] & 0x08) || |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1338 | !(hdev->le_states[2] & 0x08))) |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1345 | void __hci_req_enable_advertising(struct hci_request *req) |
| 1346 | { |
| 1347 | struct hci_dev *hdev = req->hdev; |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1348 | struct adv_info *adv; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1349 | struct hci_cp_le_set_adv_param cp; |
| 1350 | u8 own_addr_type, enable = 0x01; |
| 1351 | bool connectable; |
Spoorthi Ravishankar Koppad | ad4a679 | 2019-07-15 17:05:22 +0530 | [diff] [blame] | 1352 | u16 adv_min_interval, adv_max_interval; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1353 | u32 flags; |
| 1354 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1355 | flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance); |
| 1356 | adv = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
Łukasz Rymanowski | 9e1e9f2 | 2017-12-08 13:40:57 +0100 | [diff] [blame] | 1357 | |
| 1358 | /* If the "connectable" instance flag was not set, then choose between |
| 1359 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. |
| 1360 | */ |
| 1361 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || |
| 1362 | mgmt_get_connectable(hdev); |
| 1363 | |
| 1364 | if (!is_advertising_allowed(hdev, connectable)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1365 | return; |
| 1366 | |
| 1367 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 1368 | __hci_req_disable_advertising(req); |
| 1369 | |
| 1370 | /* Clear the HCI_LE_ADV bit temporarily so that the |
| 1371 | * hci_update_random_address knows that it's safe to go ahead |
| 1372 | * and write a new random address. The flag will be set back on |
| 1373 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1374 | */ |
| 1375 | hci_dev_clear_flag(hdev, HCI_LE_ADV); |
| 1376 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1377 | /* Set require_privacy to true only when non-connectable |
| 1378 | * advertising is used. In that case it is fine to use a |
| 1379 | * non-resolvable private address. |
| 1380 | */ |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1381 | if (hci_update_random_address(req, !connectable, |
| 1382 | adv_use_rpa(hdev, flags), |
| 1383 | &own_addr_type) < 0) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1384 | return; |
| 1385 | |
| 1386 | memset(&cp, 0, sizeof(cp)); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1387 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1388 | if (adv) { |
| 1389 | adv_min_interval = adv->min_interval; |
| 1390 | adv_max_interval = adv->max_interval; |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 1391 | } else { |
Spoorthi Ravishankar Koppad | ad4a679 | 2019-07-15 17:05:22 +0530 | [diff] [blame] | 1392 | adv_min_interval = hdev->le_adv_min_interval; |
| 1393 | adv_max_interval = hdev->le_adv_max_interval; |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | if (connectable) { |
| 1397 | cp.type = LE_ADV_IND; |
Spoorthi Ravishankar Koppad | ad4a679 | 2019-07-15 17:05:22 +0530 | [diff] [blame] | 1398 | } else { |
Luiz Augusto von Dentz | aeeae47 | 2020-11-13 16:44:34 -0800 | [diff] [blame] | 1399 | if (adv_cur_instance_is_scannable(hdev)) |
Spoorthi Ravishankar Koppad | ad4a679 | 2019-07-15 17:05:22 +0530 | [diff] [blame] | 1400 | cp.type = LE_ADV_SCAN_IND; |
| 1401 | else |
| 1402 | cp.type = LE_ADV_NONCONN_IND; |
| 1403 | |
| 1404 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) || |
| 1405 | hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { |
| 1406 | adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN; |
| 1407 | adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX; |
Spoorthi Ravishankar Koppad | ad4a679 | 2019-07-15 17:05:22 +0530 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | cp.min_interval = cpu_to_le16(adv_min_interval); |
| 1412 | cp.max_interval = cpu_to_le16(adv_max_interval); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1413 | cp.own_address_type = own_addr_type; |
| 1414 | cp.channel_map = hdev->le_adv_channel_map; |
| 1415 | |
| 1416 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1417 | |
| 1418 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1419 | } |
| 1420 | |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1421 | void __hci_req_update_scan_rsp_data(struct hci_request *req, u8 instance) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1422 | { |
| 1423 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1424 | u8 len; |
| 1425 | |
| 1426 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1427 | return; |
| 1428 | |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1429 | if (ext_adv_capable(hdev)) { |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1430 | struct { |
| 1431 | struct hci_cp_le_set_ext_scan_rsp_data cp; |
| 1432 | u8 data[HCI_MAX_EXT_AD_LENGTH]; |
| 1433 | } pdu; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1434 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1435 | memset(&pdu, 0, sizeof(pdu)); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1436 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1437 | len = eir_create_scan_rsp(hdev, instance, pdu.data); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1438 | |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1439 | if (hdev->scan_rsp_data_len == len && |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1440 | !memcmp(pdu.data, hdev->scan_rsp_data, len)) |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1441 | return; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1442 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1443 | memcpy(hdev->scan_rsp_data, pdu.data, len); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1444 | hdev->scan_rsp_data_len = len; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1445 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1446 | pdu.cp.handle = instance; |
| 1447 | pdu.cp.length = len; |
| 1448 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; |
| 1449 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1450 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1451 | hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, |
| 1452 | sizeof(pdu.cp) + len, &pdu.cp); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1453 | } else { |
| 1454 | struct hci_cp_le_set_scan_rsp_data cp; |
| 1455 | |
| 1456 | memset(&cp, 0, sizeof(cp)); |
| 1457 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1458 | len = eir_create_scan_rsp(hdev, instance, cp.data); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1459 | |
| 1460 | if (hdev->scan_rsp_data_len == len && |
| 1461 | !memcmp(cp.data, hdev->scan_rsp_data, len)) |
| 1462 | return; |
| 1463 | |
| 1464 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 1465 | hdev->scan_rsp_data_len = len; |
| 1466 | |
| 1467 | cp.length = len; |
| 1468 | |
| 1469 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 1470 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1473 | void __hci_req_update_adv_data(struct hci_request *req, u8 instance) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1474 | { |
| 1475 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1476 | u8 len; |
| 1477 | |
| 1478 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1479 | return; |
| 1480 | |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1481 | if (ext_adv_capable(hdev)) { |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1482 | struct { |
| 1483 | struct hci_cp_le_set_ext_adv_data cp; |
| 1484 | u8 data[HCI_MAX_EXT_AD_LENGTH]; |
| 1485 | } pdu; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1486 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1487 | memset(&pdu, 0, sizeof(pdu)); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1488 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1489 | len = eir_create_adv_data(hdev, instance, pdu.data); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1490 | |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1491 | /* There's nothing to do if the data hasn't changed */ |
| 1492 | if (hdev->adv_data_len == len && |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1493 | memcmp(pdu.data, hdev->adv_data, len) == 0) |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1494 | return; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1495 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1496 | memcpy(hdev->adv_data, pdu.data, len); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1497 | hdev->adv_data_len = len; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1498 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1499 | pdu.cp.length = len; |
| 1500 | pdu.cp.handle = instance; |
| 1501 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; |
| 1502 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1503 | |
Luiz Augusto von Dentz | c9ed0a7 | 2021-06-09 11:09:27 -0700 | [diff] [blame] | 1504 | hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_DATA, |
| 1505 | sizeof(pdu.cp) + len, &pdu.cp); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1506 | } else { |
| 1507 | struct hci_cp_le_set_adv_data cp; |
| 1508 | |
| 1509 | memset(&cp, 0, sizeof(cp)); |
| 1510 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1511 | len = eir_create_adv_data(hdev, instance, cp.data); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1512 | |
| 1513 | /* There's nothing to do if the data hasn't changed */ |
| 1514 | if (hdev->adv_data_len == len && |
| 1515 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 1516 | return; |
| 1517 | |
| 1518 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 1519 | hdev->adv_data_len = len; |
| 1520 | |
| 1521 | cp.length = len; |
| 1522 | |
| 1523 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 1524 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1525 | } |
| 1526 | |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1527 | int hci_req_update_adv_data(struct hci_dev *hdev, u8 instance) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1528 | { |
| 1529 | struct hci_request req; |
| 1530 | |
| 1531 | hci_req_init(&req, hdev); |
| 1532 | __hci_req_update_adv_data(&req, instance); |
| 1533 | |
| 1534 | return hci_req_run(&req, NULL); |
| 1535 | } |
| 1536 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 1537 | static void enable_addr_resolution_complete(struct hci_dev *hdev, u8 status, |
| 1538 | u16 opcode) |
| 1539 | { |
| 1540 | BT_DBG("%s status %u", hdev->name, status); |
| 1541 | } |
| 1542 | |
| 1543 | void hci_req_disable_address_resolution(struct hci_dev *hdev) |
| 1544 | { |
| 1545 | struct hci_request req; |
| 1546 | __u8 enable = 0x00; |
| 1547 | |
| 1548 | if (!use_ll_privacy(hdev) && |
| 1549 | !hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) |
| 1550 | return; |
| 1551 | |
| 1552 | hci_req_init(&req, hdev); |
| 1553 | |
| 1554 | hci_req_add(&req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable); |
| 1555 | |
| 1556 | hci_req_run(&req, enable_addr_resolution_complete); |
| 1557 | } |
| 1558 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1559 | static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 1560 | { |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 1561 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | void hci_req_reenable_advertising(struct hci_dev *hdev) |
| 1565 | { |
| 1566 | struct hci_request req; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1567 | |
| 1568 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 1569 | list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1570 | return; |
| 1571 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1572 | hci_req_init(&req, hdev); |
| 1573 | |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1574 | if (hdev->cur_adv_instance) { |
| 1575 | __hci_req_schedule_adv_instance(&req, hdev->cur_adv_instance, |
| 1576 | true); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1577 | } else { |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1578 | if (ext_adv_capable(hdev)) { |
| 1579 | __hci_req_start_ext_adv(&req, 0x00); |
| 1580 | } else { |
| 1581 | __hci_req_update_adv_data(&req, 0x00); |
| 1582 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1583 | __hci_req_enable_advertising(&req); |
| 1584 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | hci_req_run(&req, adv_enable_complete); |
| 1588 | } |
| 1589 | |
| 1590 | static void adv_timeout_expire(struct work_struct *work) |
| 1591 | { |
| 1592 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1593 | adv_instance_expire.work); |
| 1594 | |
| 1595 | struct hci_request req; |
| 1596 | u8 instance; |
| 1597 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 1598 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1599 | |
| 1600 | hci_dev_lock(hdev); |
| 1601 | |
| 1602 | hdev->adv_instance_timeout = 0; |
| 1603 | |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1604 | instance = hdev->cur_adv_instance; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1605 | if (instance == 0x00) |
| 1606 | goto unlock; |
| 1607 | |
| 1608 | hci_req_init(&req, hdev); |
| 1609 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1610 | hci_req_clear_adv_instance(hdev, NULL, &req, instance, false); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1611 | |
| 1612 | if (list_empty(&hdev->adv_instances)) |
| 1613 | __hci_req_disable_advertising(&req); |
| 1614 | |
Johan Hedberg | 550a8ca | 2015-11-27 11:11:52 +0200 | [diff] [blame] | 1615 | hci_req_run(&req, NULL); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1616 | |
| 1617 | unlock: |
| 1618 | hci_dev_unlock(hdev); |
| 1619 | } |
| 1620 | |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 1621 | static int hci_req_add_le_interleaved_scan(struct hci_request *req, |
| 1622 | unsigned long opt) |
| 1623 | { |
| 1624 | struct hci_dev *hdev = req->hdev; |
| 1625 | int ret = 0; |
| 1626 | |
| 1627 | hci_dev_lock(hdev); |
| 1628 | |
| 1629 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
| 1630 | hci_req_add_le_scan_disable(req, false); |
| 1631 | hci_req_add_le_passive_scan(req); |
| 1632 | |
| 1633 | switch (hdev->interleave_scan_state) { |
| 1634 | case INTERLEAVE_SCAN_ALLOWLIST: |
| 1635 | bt_dev_dbg(hdev, "next state: allowlist"); |
| 1636 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; |
| 1637 | break; |
| 1638 | case INTERLEAVE_SCAN_NO_FILTER: |
| 1639 | bt_dev_dbg(hdev, "next state: no filter"); |
| 1640 | hdev->interleave_scan_state = INTERLEAVE_SCAN_ALLOWLIST; |
| 1641 | break; |
| 1642 | case INTERLEAVE_SCAN_NONE: |
| 1643 | BT_ERR("unexpected error"); |
| 1644 | ret = -1; |
| 1645 | } |
| 1646 | |
| 1647 | hci_dev_unlock(hdev); |
| 1648 | |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
| 1652 | static void interleave_scan_work(struct work_struct *work) |
| 1653 | { |
| 1654 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1655 | interleave_scan.work); |
| 1656 | u8 status; |
| 1657 | unsigned long timeout; |
| 1658 | |
| 1659 | if (hdev->interleave_scan_state == INTERLEAVE_SCAN_ALLOWLIST) { |
| 1660 | timeout = msecs_to_jiffies(hdev->advmon_allowlist_duration); |
| 1661 | } else if (hdev->interleave_scan_state == INTERLEAVE_SCAN_NO_FILTER) { |
| 1662 | timeout = msecs_to_jiffies(hdev->advmon_no_filter_duration); |
| 1663 | } else { |
| 1664 | bt_dev_err(hdev, "unexpected error"); |
| 1665 | return; |
| 1666 | } |
| 1667 | |
| 1668 | hci_req_sync(hdev, hci_req_add_le_interleaved_scan, 0, |
| 1669 | HCI_CMD_TIMEOUT, &status); |
| 1670 | |
| 1671 | /* Don't continue interleaving if it was canceled */ |
| 1672 | if (is_interleave_scanning(hdev)) |
| 1673 | queue_delayed_work(hdev->req_workqueue, |
| 1674 | &hdev->interleave_scan, timeout); |
| 1675 | } |
| 1676 | |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1677 | int hci_get_random_address(struct hci_dev *hdev, bool require_privacy, |
| 1678 | bool use_rpa, struct adv_info *adv_instance, |
| 1679 | u8 *own_addr_type, bdaddr_t *rand_addr) |
| 1680 | { |
| 1681 | int err; |
| 1682 | |
| 1683 | bacpy(rand_addr, BDADDR_ANY); |
| 1684 | |
| 1685 | /* If privacy is enabled use a resolvable private address. If |
| 1686 | * current RPA has expired then generate a new one. |
| 1687 | */ |
| 1688 | if (use_rpa) { |
Sathish Narasimman | c0ee064 | 2020-09-25 18:02:15 +0530 | [diff] [blame] | 1689 | /* If Controller supports LL Privacy use own address type is |
| 1690 | * 0x03 |
| 1691 | */ |
Sathish Narasimman | abb638b | 2021-04-05 20:00:23 +0530 | [diff] [blame] | 1692 | if (use_ll_privacy(hdev) && |
| 1693 | hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
Sathish Narasimman | c0ee064 | 2020-09-25 18:02:15 +0530 | [diff] [blame] | 1694 | *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; |
| 1695 | else |
| 1696 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1697 | |
| 1698 | if (adv_instance) { |
Luiz Augusto von Dentz | c45074d | 2021-08-02 16:56:19 -0700 | [diff] [blame] | 1699 | if (adv_rpa_valid(adv_instance)) |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1700 | return 0; |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1701 | } else { |
Luiz Augusto von Dentz | c45074d | 2021-08-02 16:56:19 -0700 | [diff] [blame] | 1702 | if (rpa_valid(hdev)) |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); |
| 1707 | if (err < 0) { |
Marcel Holtmann | 00b383b | 2020-03-09 22:48:10 +0100 | [diff] [blame] | 1708 | bt_dev_err(hdev, "failed to generate new RPA"); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1709 | return err; |
| 1710 | } |
| 1711 | |
| 1712 | bacpy(rand_addr, &hdev->rpa); |
| 1713 | |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /* In case of required privacy without resolvable private address, |
| 1718 | * use an non-resolvable private address. This is useful for |
| 1719 | * non-connectable advertising. |
| 1720 | */ |
| 1721 | if (require_privacy) { |
| 1722 | bdaddr_t nrpa; |
| 1723 | |
| 1724 | while (true) { |
| 1725 | /* The non-resolvable private address is generated |
| 1726 | * from random six bytes with the two most significant |
| 1727 | * bits cleared. |
| 1728 | */ |
| 1729 | get_random_bytes(&nrpa, 6); |
| 1730 | nrpa.b[5] &= 0x3f; |
| 1731 | |
| 1732 | /* The non-resolvable private address shall not be |
| 1733 | * equal to the public address. |
| 1734 | */ |
| 1735 | if (bacmp(&hdev->bdaddr, &nrpa)) |
| 1736 | break; |
| 1737 | } |
| 1738 | |
| 1739 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
| 1740 | bacpy(rand_addr, &nrpa); |
| 1741 | |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | /* No privacy so use a public address. */ |
| 1746 | *own_addr_type = ADDR_LE_DEV_PUBLIC; |
| 1747 | |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 1751 | void __hci_req_clear_ext_adv_sets(struct hci_request *req) |
| 1752 | { |
| 1753 | hci_req_add(req, HCI_OP_LE_CLEAR_ADV_SETS, 0, NULL); |
| 1754 | } |
| 1755 | |
Luiz Augusto von Dentz | c45074d | 2021-08-02 16:56:19 -0700 | [diff] [blame] | 1756 | static void set_random_addr(struct hci_request *req, bdaddr_t *rpa) |
| 1757 | { |
| 1758 | struct hci_dev *hdev = req->hdev; |
| 1759 | |
| 1760 | /* If we're advertising or initiating an LE connection we can't |
| 1761 | * go ahead and change the random address at this time. This is |
| 1762 | * because the eventual initiator address used for the |
| 1763 | * subsequently created connection will be undefined (some |
| 1764 | * controllers use the new address and others the one we had |
| 1765 | * when the operation started). |
| 1766 | * |
| 1767 | * In this kind of scenario skip the update and let the random |
| 1768 | * address be updated at the next cycle. |
| 1769 | */ |
| 1770 | if (hci_dev_test_flag(hdev, HCI_LE_ADV) || |
| 1771 | hci_lookup_le_connect(hdev)) { |
| 1772 | bt_dev_dbg(hdev, "Deferring random address update"); |
| 1773 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
| 1774 | return; |
| 1775 | } |
| 1776 | |
| 1777 | hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa); |
| 1778 | } |
| 1779 | |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1780 | int __hci_req_setup_ext_adv_instance(struct hci_request *req, u8 instance) |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1781 | { |
| 1782 | struct hci_cp_le_set_ext_adv_params cp; |
| 1783 | struct hci_dev *hdev = req->hdev; |
| 1784 | bool connectable; |
| 1785 | u32 flags; |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1786 | bdaddr_t random_addr; |
| 1787 | u8 own_addr_type; |
| 1788 | int err; |
| 1789 | struct adv_info *adv_instance; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 1790 | bool secondary_adv; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1791 | |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1792 | if (instance > 0) { |
| 1793 | adv_instance = hci_find_adv_instance(hdev, instance); |
| 1794 | if (!adv_instance) |
| 1795 | return -EINVAL; |
| 1796 | } else { |
| 1797 | adv_instance = NULL; |
| 1798 | } |
| 1799 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1800 | flags = hci_adv_instance_flags(hdev, instance); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1801 | |
| 1802 | /* If the "connectable" instance flag was not set, then choose between |
| 1803 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. |
| 1804 | */ |
| 1805 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || |
| 1806 | mgmt_get_connectable(hdev); |
| 1807 | |
Colin Ian King | 75edd1f | 2018-11-09 13:27:36 +0000 | [diff] [blame] | 1808 | if (!is_advertising_allowed(hdev, connectable)) |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1809 | return -EPERM; |
| 1810 | |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1811 | /* Set require_privacy to true only when non-connectable |
| 1812 | * advertising is used. In that case it is fine to use a |
| 1813 | * non-resolvable private address. |
| 1814 | */ |
| 1815 | err = hci_get_random_address(hdev, !connectable, |
| 1816 | adv_use_rpa(hdev, flags), adv_instance, |
| 1817 | &own_addr_type, &random_addr); |
| 1818 | if (err < 0) |
| 1819 | return err; |
| 1820 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1821 | memset(&cp, 0, sizeof(cp)); |
| 1822 | |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 1823 | if (adv_instance) { |
| 1824 | hci_cpu_to_le24(adv_instance->min_interval, cp.min_interval); |
| 1825 | hci_cpu_to_le24(adv_instance->max_interval, cp.max_interval); |
| 1826 | cp.tx_power = adv_instance->tx_power; |
| 1827 | } else { |
| 1828 | hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval); |
| 1829 | hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval); |
| 1830 | cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 1831 | } |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1832 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 1833 | secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK); |
| 1834 | |
| 1835 | if (connectable) { |
| 1836 | if (secondary_adv) |
| 1837 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND); |
| 1838 | else |
| 1839 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND); |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame^] | 1840 | } else if (hci_adv_instance_is_scannable(hdev, instance) || |
Daniel Winkler | ff02db1 | 2021-03-03 11:15:23 -0800 | [diff] [blame] | 1841 | (flags & MGMT_ADV_PARAM_SCAN_RSP)) { |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 1842 | if (secondary_adv) |
| 1843 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND); |
| 1844 | else |
| 1845 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND); |
| 1846 | } else { |
| 1847 | if (secondary_adv) |
| 1848 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND); |
| 1849 | else |
| 1850 | cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND); |
| 1851 | } |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1852 | |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1853 | cp.own_addr_type = own_addr_type; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1854 | cp.channel_map = hdev->le_adv_channel_map; |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1855 | cp.handle = instance; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1856 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 1857 | if (flags & MGMT_ADV_FLAG_SEC_2M) { |
| 1858 | cp.primary_phy = HCI_ADV_PHY_1M; |
| 1859 | cp.secondary_phy = HCI_ADV_PHY_2M; |
| 1860 | } else if (flags & MGMT_ADV_FLAG_SEC_CODED) { |
| 1861 | cp.primary_phy = HCI_ADV_PHY_CODED; |
| 1862 | cp.secondary_phy = HCI_ADV_PHY_CODED; |
| 1863 | } else { |
| 1864 | /* In all other cases use 1M */ |
| 1865 | cp.primary_phy = HCI_ADV_PHY_1M; |
| 1866 | cp.secondary_phy = HCI_ADV_PHY_1M; |
| 1867 | } |
| 1868 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1869 | hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(cp), &cp); |
| 1870 | |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1871 | if (own_addr_type == ADDR_LE_DEV_RANDOM && |
| 1872 | bacmp(&random_addr, BDADDR_ANY)) { |
| 1873 | struct hci_cp_le_set_adv_set_rand_addr cp; |
| 1874 | |
| 1875 | /* Check if random address need to be updated */ |
| 1876 | if (adv_instance) { |
| 1877 | if (!bacmp(&random_addr, &adv_instance->random_addr)) |
| 1878 | return 0; |
| 1879 | } else { |
| 1880 | if (!bacmp(&random_addr, &hdev->random_addr)) |
| 1881 | return 0; |
Luiz Augusto von Dentz | c45074d | 2021-08-02 16:56:19 -0700 | [diff] [blame] | 1882 | /* Instance 0x00 doesn't have an adv_info, instead it |
| 1883 | * uses hdev->random_addr to track its address so |
| 1884 | * whenever it needs to be updated this also set the |
| 1885 | * random address since hdev->random_addr is shared with |
| 1886 | * scan state machine. |
| 1887 | */ |
| 1888 | set_random_addr(req, &random_addr); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | memset(&cp, 0, sizeof(cp)); |
| 1892 | |
Tedd Ho-Jeong An | eaa7b72 | 2020-05-01 10:00:50 -0700 | [diff] [blame] | 1893 | cp.handle = instance; |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 1894 | bacpy(&cp.bdaddr, &random_addr); |
| 1895 | |
| 1896 | hci_req_add(req, |
| 1897 | HCI_OP_LE_SET_ADV_SET_RAND_ADDR, |
| 1898 | sizeof(cp), &cp); |
| 1899 | } |
| 1900 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1901 | return 0; |
| 1902 | } |
| 1903 | |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1904 | int __hci_req_enable_ext_advertising(struct hci_request *req, u8 instance) |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1905 | { |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1906 | struct hci_dev *hdev = req->hdev; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1907 | struct hci_cp_le_set_ext_adv_enable *cp; |
| 1908 | struct hci_cp_ext_adv_set *adv_set; |
| 1909 | u8 data[sizeof(*cp) + sizeof(*adv_set) * 1]; |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1910 | struct adv_info *adv_instance; |
| 1911 | |
| 1912 | if (instance > 0) { |
| 1913 | adv_instance = hci_find_adv_instance(hdev, instance); |
| 1914 | if (!adv_instance) |
| 1915 | return -EINVAL; |
| 1916 | } else { |
| 1917 | adv_instance = NULL; |
| 1918 | } |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1919 | |
| 1920 | cp = (void *) data; |
| 1921 | adv_set = (void *) cp->data; |
| 1922 | |
| 1923 | memset(cp, 0, sizeof(*cp)); |
| 1924 | |
| 1925 | cp->enable = 0x01; |
| 1926 | cp->num_of_sets = 0x01; |
| 1927 | |
| 1928 | memset(adv_set, 0, sizeof(*adv_set)); |
| 1929 | |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1930 | adv_set->handle = instance; |
| 1931 | |
| 1932 | /* Set duration per instance since controller is responsible for |
| 1933 | * scheduling it. |
| 1934 | */ |
| 1935 | if (adv_instance && adv_instance->duration) { |
Luiz Augusto von Dentz | 10bbffa | 2019-10-24 16:15:42 +0300 | [diff] [blame] | 1936 | u16 duration = adv_instance->timeout * MSEC_PER_SEC; |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1937 | |
| 1938 | /* Time = N * 10 ms */ |
| 1939 | adv_set->duration = cpu_to_le16(duration / 10); |
| 1940 | } |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1941 | |
| 1942 | hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_ENABLE, |
| 1943 | sizeof(*cp) + sizeof(*adv_set) * cp->num_of_sets, |
| 1944 | data); |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 1945 | |
| 1946 | return 0; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1947 | } |
| 1948 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 1949 | int __hci_req_disable_ext_adv_instance(struct hci_request *req, u8 instance) |
| 1950 | { |
| 1951 | struct hci_dev *hdev = req->hdev; |
| 1952 | struct hci_cp_le_set_ext_adv_enable *cp; |
| 1953 | struct hci_cp_ext_adv_set *adv_set; |
| 1954 | u8 data[sizeof(*cp) + sizeof(*adv_set) * 1]; |
| 1955 | u8 req_size; |
| 1956 | |
| 1957 | /* If request specifies an instance that doesn't exist, fail */ |
| 1958 | if (instance > 0 && !hci_find_adv_instance(hdev, instance)) |
| 1959 | return -EINVAL; |
| 1960 | |
| 1961 | memset(data, 0, sizeof(data)); |
| 1962 | |
| 1963 | cp = (void *)data; |
| 1964 | adv_set = (void *)cp->data; |
| 1965 | |
| 1966 | /* Instance 0x00 indicates all advertising instances will be disabled */ |
| 1967 | cp->num_of_sets = !!instance; |
| 1968 | cp->enable = 0x00; |
| 1969 | |
| 1970 | adv_set->handle = instance; |
| 1971 | |
| 1972 | req_size = sizeof(*cp) + sizeof(*adv_set) * cp->num_of_sets; |
| 1973 | hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_ENABLE, req_size, data); |
| 1974 | |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | int __hci_req_remove_ext_adv_instance(struct hci_request *req, u8 instance) |
| 1979 | { |
| 1980 | struct hci_dev *hdev = req->hdev; |
| 1981 | |
| 1982 | /* If request specifies an instance that doesn't exist, fail */ |
| 1983 | if (instance > 0 && !hci_find_adv_instance(hdev, instance)) |
| 1984 | return -EINVAL; |
| 1985 | |
| 1986 | hci_req_add(req, HCI_OP_LE_REMOVE_ADV_SET, sizeof(instance), &instance); |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1991 | int __hci_req_start_ext_adv(struct hci_request *req, u8 instance) |
| 1992 | { |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 1993 | struct hci_dev *hdev = req->hdev; |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 1994 | struct adv_info *adv_instance = hci_find_adv_instance(hdev, instance); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 1995 | int err; |
| 1996 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 1997 | /* If instance isn't pending, the chip knows about it, and it's safe to |
| 1998 | * disable |
| 1999 | */ |
| 2000 | if (adv_instance && !adv_instance->pending) |
| 2001 | __hci_req_disable_ext_adv_instance(req, instance); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 2002 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 2003 | err = __hci_req_setup_ext_adv_instance(req, instance); |
| 2004 | if (err < 0) |
| 2005 | return err; |
| 2006 | |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 2007 | __hci_req_update_scan_rsp_data(req, instance); |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 2008 | __hci_req_enable_ext_advertising(req, instance); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 2009 | |
| 2010 | return 0; |
| 2011 | } |
| 2012 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2013 | int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance, |
| 2014 | bool force) |
| 2015 | { |
| 2016 | struct hci_dev *hdev = req->hdev; |
| 2017 | struct adv_info *adv_instance = NULL; |
| 2018 | u16 timeout; |
| 2019 | |
| 2020 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 2021 | list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2022 | return -EPERM; |
| 2023 | |
| 2024 | if (hdev->adv_instance_timeout) |
| 2025 | return -EBUSY; |
| 2026 | |
| 2027 | adv_instance = hci_find_adv_instance(hdev, instance); |
| 2028 | if (!adv_instance) |
| 2029 | return -ENOENT; |
| 2030 | |
| 2031 | /* A zero timeout means unlimited advertising. As long as there is |
| 2032 | * only one instance, duration should be ignored. We still set a timeout |
| 2033 | * in case further instances are being added later on. |
| 2034 | * |
| 2035 | * If the remaining lifetime of the instance is more than the duration |
| 2036 | * then the timeout corresponds to the duration, otherwise it will be |
| 2037 | * reduced to the remaining instance lifetime. |
| 2038 | */ |
| 2039 | if (adv_instance->timeout == 0 || |
| 2040 | adv_instance->duration <= adv_instance->remaining_time) |
| 2041 | timeout = adv_instance->duration; |
| 2042 | else |
| 2043 | timeout = adv_instance->remaining_time; |
| 2044 | |
| 2045 | /* The remaining time is being reduced unless the instance is being |
| 2046 | * advertised without time limit. |
| 2047 | */ |
| 2048 | if (adv_instance->timeout) |
| 2049 | adv_instance->remaining_time = |
| 2050 | adv_instance->remaining_time - timeout; |
| 2051 | |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 2052 | /* Only use work for scheduling instances with legacy advertising */ |
| 2053 | if (!ext_adv_capable(hdev)) { |
| 2054 | hdev->adv_instance_timeout = timeout; |
| 2055 | queue_delayed_work(hdev->req_workqueue, |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2056 | &hdev->adv_instance_expire, |
| 2057 | msecs_to_jiffies(timeout * 1000)); |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 2058 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2059 | |
| 2060 | /* If we're just re-scheduling the same instance again then do not |
| 2061 | * execute any HCI commands. This happens when a single instance is |
| 2062 | * being advertised. |
| 2063 | */ |
| 2064 | if (!force && hdev->cur_adv_instance == instance && |
| 2065 | hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 2066 | return 0; |
| 2067 | |
| 2068 | hdev->cur_adv_instance = instance; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 2069 | if (ext_adv_capable(hdev)) { |
| 2070 | __hci_req_start_ext_adv(req, instance); |
| 2071 | } else { |
| 2072 | __hci_req_update_adv_data(req, instance); |
| 2073 | __hci_req_update_scan_rsp_data(req, instance); |
| 2074 | __hci_req_enable_advertising(req); |
| 2075 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2076 | |
| 2077 | return 0; |
| 2078 | } |
| 2079 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2080 | /* For a single instance: |
| 2081 | * - force == true: The instance will be removed even when its remaining |
| 2082 | * lifetime is not zero. |
| 2083 | * - force == false: the instance will be deactivated but kept stored unless |
| 2084 | * the remaining lifetime is zero. |
| 2085 | * |
| 2086 | * For instance == 0x00: |
| 2087 | * - force == true: All instances will be removed regardless of their timeout |
| 2088 | * setting. |
| 2089 | * - force == false: Only instances that have a timeout will be removed. |
| 2090 | */ |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 2091 | void hci_req_clear_adv_instance(struct hci_dev *hdev, struct sock *sk, |
| 2092 | struct hci_request *req, u8 instance, |
| 2093 | bool force) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2094 | { |
| 2095 | struct adv_info *adv_instance, *n, *next_instance = NULL; |
| 2096 | int err; |
| 2097 | u8 rem_inst; |
| 2098 | |
| 2099 | /* Cancel any timeout concerning the removed instance(s). */ |
| 2100 | if (!instance || hdev->cur_adv_instance == instance) |
| 2101 | cancel_adv_timeout(hdev); |
| 2102 | |
| 2103 | /* Get the next instance to advertise BEFORE we remove |
| 2104 | * the current one. This can be the same instance again |
| 2105 | * if there is only one instance. |
| 2106 | */ |
| 2107 | if (instance && hdev->cur_adv_instance == instance) |
| 2108 | next_instance = hci_get_next_instance(hdev, instance); |
| 2109 | |
| 2110 | if (instance == 0x00) { |
| 2111 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, |
| 2112 | list) { |
| 2113 | if (!(force || adv_instance->timeout)) |
| 2114 | continue; |
| 2115 | |
| 2116 | rem_inst = adv_instance->instance; |
| 2117 | err = hci_remove_adv_instance(hdev, rem_inst); |
| 2118 | if (!err) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 2119 | mgmt_advertising_removed(sk, hdev, rem_inst); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2120 | } |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2121 | } else { |
| 2122 | adv_instance = hci_find_adv_instance(hdev, instance); |
| 2123 | |
| 2124 | if (force || (adv_instance && adv_instance->timeout && |
| 2125 | !adv_instance->remaining_time)) { |
| 2126 | /* Don't advertise a removed instance. */ |
| 2127 | if (next_instance && |
| 2128 | next_instance->instance == instance) |
| 2129 | next_instance = NULL; |
| 2130 | |
| 2131 | err = hci_remove_adv_instance(hdev, instance); |
| 2132 | if (!err) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 2133 | mgmt_advertising_removed(sk, hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2137 | if (!req || !hdev_is_powered(hdev) || |
| 2138 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 2139 | return; |
| 2140 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 2141 | if (next_instance && !ext_adv_capable(hdev)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2142 | __hci_req_schedule_adv_instance(req, next_instance->instance, |
| 2143 | false); |
| 2144 | } |
| 2145 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2146 | int hci_update_random_address(struct hci_request *req, bool require_privacy, |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 2147 | bool use_rpa, u8 *own_addr_type) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2148 | { |
| 2149 | struct hci_dev *hdev = req->hdev; |
| 2150 | int err; |
| 2151 | |
| 2152 | /* If privacy is enabled use a resolvable private address. If |
| 2153 | * current RPA has expired or there is something else than |
| 2154 | * the current RPA in use, then generate a new one. |
| 2155 | */ |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 2156 | if (use_rpa) { |
Sathish Narasimman | d03c759 | 2020-07-23 18:09:00 +0530 | [diff] [blame] | 2157 | /* If Controller supports LL Privacy use own address type is |
| 2158 | * 0x03 |
| 2159 | */ |
Sathish Narasimman | abb638b | 2021-04-05 20:00:23 +0530 | [diff] [blame] | 2160 | if (use_ll_privacy(hdev) && |
| 2161 | hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
Sathish Narasimman | d03c759 | 2020-07-23 18:09:00 +0530 | [diff] [blame] | 2162 | *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; |
| 2163 | else |
| 2164 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2165 | |
Luiz Augusto von Dentz | c45074d | 2021-08-02 16:56:19 -0700 | [diff] [blame] | 2166 | if (rpa_valid(hdev)) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2167 | return 0; |
| 2168 | |
| 2169 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); |
| 2170 | if (err < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2171 | bt_dev_err(hdev, "failed to generate new RPA"); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2172 | return err; |
| 2173 | } |
| 2174 | |
| 2175 | set_random_addr(req, &hdev->rpa); |
| 2176 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2177 | return 0; |
| 2178 | } |
| 2179 | |
| 2180 | /* In case of required privacy without resolvable private address, |
| 2181 | * use an non-resolvable private address. This is useful for active |
| 2182 | * scanning and non-connectable advertising. |
| 2183 | */ |
| 2184 | if (require_privacy) { |
| 2185 | bdaddr_t nrpa; |
| 2186 | |
| 2187 | while (true) { |
| 2188 | /* The non-resolvable private address is generated |
| 2189 | * from random six bytes with the two most significant |
| 2190 | * bits cleared. |
| 2191 | */ |
| 2192 | get_random_bytes(&nrpa, 6); |
| 2193 | nrpa.b[5] &= 0x3f; |
| 2194 | |
| 2195 | /* The non-resolvable private address shall not be |
| 2196 | * equal to the public address. |
| 2197 | */ |
| 2198 | if (bacmp(&hdev->bdaddr, &nrpa)) |
| 2199 | break; |
| 2200 | } |
| 2201 | |
| 2202 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
| 2203 | set_random_addr(req, &nrpa); |
| 2204 | return 0; |
| 2205 | } |
| 2206 | |
| 2207 | /* If forcing static address is in use or there is no public |
| 2208 | * address use the static address as random address (but skip |
| 2209 | * the HCI command if the current random address is already the |
| 2210 | * static one. |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 2211 | * |
| 2212 | * In case BR/EDR has been disabled on a dual-mode controller |
| 2213 | * and a static address has been configured, then use that |
| 2214 | * address instead of the public BR/EDR address. |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2215 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 2216 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 2217 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2218 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 2219 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 2220 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
| 2221 | if (bacmp(&hdev->static_addr, &hdev->random_addr)) |
| 2222 | hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, |
| 2223 | &hdev->static_addr); |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
| 2227 | /* Neither privacy nor static address is being used so use a |
| 2228 | * public address. |
| 2229 | */ |
| 2230 | *own_addr_type = ADDR_LE_DEV_PUBLIC; |
| 2231 | |
| 2232 | return 0; |
| 2233 | } |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 2234 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 2235 | static bool disconnected_accept_list_entries(struct hci_dev *hdev) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2236 | { |
| 2237 | struct bdaddr_list *b; |
| 2238 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 2239 | list_for_each_entry(b, &hdev->accept_list, list) { |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2240 | struct hci_conn *conn; |
| 2241 | |
| 2242 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); |
| 2243 | if (!conn) |
| 2244 | return true; |
| 2245 | |
| 2246 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 2247 | return true; |
| 2248 | } |
| 2249 | |
| 2250 | return false; |
| 2251 | } |
| 2252 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 2253 | void __hci_req_update_scan(struct hci_request *req) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2254 | { |
| 2255 | struct hci_dev *hdev = req->hdev; |
| 2256 | u8 scan; |
| 2257 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2258 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2259 | return; |
| 2260 | |
| 2261 | if (!hdev_is_powered(hdev)) |
| 2262 | return; |
| 2263 | |
| 2264 | if (mgmt_powering_down(hdev)) |
| 2265 | return; |
| 2266 | |
Abhishek Pandit-Subedi | 4f40afc | 2020-03-11 08:54:01 -0700 | [diff] [blame] | 2267 | if (hdev->scanning_paused) |
| 2268 | return; |
| 2269 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2270 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 2271 | disconnected_accept_list_entries(hdev)) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2272 | scan = SCAN_PAGE; |
| 2273 | else |
| 2274 | scan = SCAN_DISABLED; |
| 2275 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2276 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2277 | scan |= SCAN_INQUIRY; |
| 2278 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 2279 | if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) && |
| 2280 | test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY)) |
| 2281 | return; |
| 2282 | |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2283 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 2284 | } |
| 2285 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 2286 | static int update_scan(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2287 | { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 2288 | hci_dev_lock(req->hdev); |
| 2289 | __hci_req_update_scan(req); |
| 2290 | hci_dev_unlock(req->hdev); |
| 2291 | return 0; |
| 2292 | } |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2293 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 2294 | static void scan_update_work(struct work_struct *work) |
| 2295 | { |
| 2296 | struct hci_dev *hdev = container_of(work, struct hci_dev, scan_update); |
| 2297 | |
| 2298 | hci_req_sync(hdev, update_scan, 0, HCI_CMD_TIMEOUT, NULL); |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 2299 | } |
| 2300 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 2301 | static int connectable_update(struct hci_request *req, unsigned long opt) |
| 2302 | { |
| 2303 | struct hci_dev *hdev = req->hdev; |
| 2304 | |
| 2305 | hci_dev_lock(hdev); |
| 2306 | |
| 2307 | __hci_req_update_scan(req); |
| 2308 | |
| 2309 | /* If BR/EDR is not enabled and we disable advertising as a |
| 2310 | * by-product of disabling connectable, we need to update the |
| 2311 | * advertising flags. |
| 2312 | */ |
| 2313 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 2314 | __hci_req_update_adv_data(req, hdev->cur_adv_instance); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 2315 | |
| 2316 | /* Update the advertising parameters if necessary */ |
| 2317 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 2318 | !list_empty(&hdev->adv_instances)) { |
| 2319 | if (ext_adv_capable(hdev)) |
| 2320 | __hci_req_start_ext_adv(req, hdev->cur_adv_instance); |
| 2321 | else |
| 2322 | __hci_req_enable_advertising(req); |
| 2323 | } |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 2324 | |
| 2325 | __hci_update_background_scan(req); |
| 2326 | |
| 2327 | hci_dev_unlock(hdev); |
| 2328 | |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | static void connectable_update_work(struct work_struct *work) |
| 2333 | { |
| 2334 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 2335 | connectable_update); |
| 2336 | u8 status; |
| 2337 | |
| 2338 | hci_req_sync(hdev, connectable_update, 0, HCI_CMD_TIMEOUT, &status); |
| 2339 | mgmt_set_connectable_complete(hdev, status); |
| 2340 | } |
| 2341 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2342 | static u8 get_service_classes(struct hci_dev *hdev) |
| 2343 | { |
| 2344 | struct bt_uuid *uuid; |
| 2345 | u8 val = 0; |
| 2346 | |
| 2347 | list_for_each_entry(uuid, &hdev->uuids, list) |
| 2348 | val |= uuid->svc_hint; |
| 2349 | |
| 2350 | return val; |
| 2351 | } |
| 2352 | |
| 2353 | void __hci_req_update_class(struct hci_request *req) |
| 2354 | { |
| 2355 | struct hci_dev *hdev = req->hdev; |
| 2356 | u8 cod[3]; |
| 2357 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2358 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2359 | |
| 2360 | if (!hdev_is_powered(hdev)) |
| 2361 | return; |
| 2362 | |
| 2363 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 2364 | return; |
| 2365 | |
| 2366 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
| 2367 | return; |
| 2368 | |
| 2369 | cod[0] = hdev->minor_class; |
| 2370 | cod[1] = hdev->major_class; |
| 2371 | cod[2] = get_service_classes(hdev); |
| 2372 | |
| 2373 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
| 2374 | cod[1] |= 0x20; |
| 2375 | |
| 2376 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 2377 | return; |
| 2378 | |
| 2379 | hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 2380 | } |
| 2381 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 2382 | static void write_iac(struct hci_request *req) |
| 2383 | { |
| 2384 | struct hci_dev *hdev = req->hdev; |
| 2385 | struct hci_cp_write_current_iac_lap cp; |
| 2386 | |
| 2387 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
| 2388 | return; |
| 2389 | |
| 2390 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { |
| 2391 | /* Limited discoverable mode */ |
| 2392 | cp.num_iac = min_t(u8, hdev->num_iac, 2); |
| 2393 | cp.iac_lap[0] = 0x00; /* LIAC */ |
| 2394 | cp.iac_lap[1] = 0x8b; |
| 2395 | cp.iac_lap[2] = 0x9e; |
| 2396 | cp.iac_lap[3] = 0x33; /* GIAC */ |
| 2397 | cp.iac_lap[4] = 0x8b; |
| 2398 | cp.iac_lap[5] = 0x9e; |
| 2399 | } else { |
| 2400 | /* General discoverable mode */ |
| 2401 | cp.num_iac = 1; |
| 2402 | cp.iac_lap[0] = 0x33; /* GIAC */ |
| 2403 | cp.iac_lap[1] = 0x8b; |
| 2404 | cp.iac_lap[2] = 0x9e; |
| 2405 | } |
| 2406 | |
| 2407 | hci_req_add(req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 2408 | (cp.num_iac * 3) + 1, &cp); |
| 2409 | } |
| 2410 | |
| 2411 | static int discoverable_update(struct hci_request *req, unsigned long opt) |
| 2412 | { |
| 2413 | struct hci_dev *hdev = req->hdev; |
| 2414 | |
| 2415 | hci_dev_lock(hdev); |
| 2416 | |
| 2417 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 2418 | write_iac(req); |
| 2419 | __hci_req_update_scan(req); |
| 2420 | __hci_req_update_class(req); |
| 2421 | } |
| 2422 | |
| 2423 | /* Advertising instances don't use the global discoverable setting, so |
| 2424 | * only update AD if advertising was enabled using Set Advertising. |
| 2425 | */ |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 2426 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 2427 | __hci_req_update_adv_data(req, 0x00); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 2428 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 2429 | /* Discoverable mode affects the local advertising |
| 2430 | * address in limited privacy mode. |
| 2431 | */ |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 2432 | if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) { |
| 2433 | if (ext_adv_capable(hdev)) |
| 2434 | __hci_req_start_ext_adv(req, 0x00); |
| 2435 | else |
| 2436 | __hci_req_enable_advertising(req); |
| 2437 | } |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 2440 | hci_dev_unlock(hdev); |
| 2441 | |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | static void discoverable_update_work(struct work_struct *work) |
| 2446 | { |
| 2447 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 2448 | discoverable_update); |
| 2449 | u8 status; |
| 2450 | |
| 2451 | hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, &status); |
| 2452 | mgmt_set_discoverable_complete(hdev, status); |
| 2453 | } |
| 2454 | |
Johan Hedberg | dcc0f0d9 | 2015-10-22 10:49:37 +0300 | [diff] [blame] | 2455 | void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn, |
| 2456 | u8 reason) |
| 2457 | { |
| 2458 | switch (conn->state) { |
| 2459 | case BT_CONNECTED: |
| 2460 | case BT_CONFIG: |
| 2461 | if (conn->type == AMP_LINK) { |
| 2462 | struct hci_cp_disconn_phy_link cp; |
| 2463 | |
| 2464 | cp.phy_handle = HCI_PHY_HANDLE(conn->handle); |
| 2465 | cp.reason = reason; |
| 2466 | hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp), |
| 2467 | &cp); |
| 2468 | } else { |
| 2469 | struct hci_cp_disconnect dc; |
| 2470 | |
| 2471 | dc.handle = cpu_to_le16(conn->handle); |
| 2472 | dc.reason = reason; |
| 2473 | hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2474 | } |
| 2475 | |
| 2476 | conn->state = BT_DISCONN; |
| 2477 | |
| 2478 | break; |
| 2479 | case BT_CONNECT: |
| 2480 | if (conn->type == LE_LINK) { |
| 2481 | if (test_bit(HCI_CONN_SCANNING, &conn->flags)) |
| 2482 | break; |
| 2483 | hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 2484 | 0, NULL); |
| 2485 | } else if (conn->type == ACL_LINK) { |
| 2486 | if (req->hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 2487 | break; |
| 2488 | hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL, |
| 2489 | 6, &conn->dst); |
| 2490 | } |
| 2491 | break; |
| 2492 | case BT_CONNECT2: |
| 2493 | if (conn->type == ACL_LINK) { |
| 2494 | struct hci_cp_reject_conn_req rej; |
| 2495 | |
| 2496 | bacpy(&rej.bdaddr, &conn->dst); |
| 2497 | rej.reason = reason; |
| 2498 | |
| 2499 | hci_req_add(req, HCI_OP_REJECT_CONN_REQ, |
| 2500 | sizeof(rej), &rej); |
| 2501 | } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { |
| 2502 | struct hci_cp_reject_sync_conn_req rej; |
| 2503 | |
| 2504 | bacpy(&rej.bdaddr, &conn->dst); |
| 2505 | |
| 2506 | /* SCO rejection has its own limited set of |
| 2507 | * allowed error values (0x0D-0x0F) which isn't |
| 2508 | * compatible with most values passed to this |
| 2509 | * function. To be safe hard-code one of the |
| 2510 | * values that's suitable for SCO. |
| 2511 | */ |
Frédéric Dalleau | 3c0975a | 2016-09-08 12:00:11 +0200 | [diff] [blame] | 2512 | rej.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; |
Johan Hedberg | dcc0f0d9 | 2015-10-22 10:49:37 +0300 | [diff] [blame] | 2513 | |
| 2514 | hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 2515 | sizeof(rej), &rej); |
| 2516 | } |
| 2517 | break; |
| 2518 | default: |
| 2519 | conn->state = BT_CLOSED; |
| 2520 | break; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 2525 | { |
| 2526 | if (status) |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2527 | bt_dev_dbg(hdev, "Failed to abort connection: status 0x%2.2x", status); |
Johan Hedberg | dcc0f0d9 | 2015-10-22 10:49:37 +0300 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | int hci_abort_conn(struct hci_conn *conn, u8 reason) |
| 2531 | { |
| 2532 | struct hci_request req; |
| 2533 | int err; |
| 2534 | |
| 2535 | hci_req_init(&req, conn->hdev); |
| 2536 | |
| 2537 | __hci_abort_conn(&req, conn, reason); |
| 2538 | |
| 2539 | err = hci_req_run(&req, abort_conn_complete); |
| 2540 | if (err && err != -ENODATA) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2541 | bt_dev_err(conn->hdev, "failed to run HCI request: err %d", err); |
Johan Hedberg | dcc0f0d9 | 2015-10-22 10:49:37 +0300 | [diff] [blame] | 2542 | return err; |
| 2543 | } |
| 2544 | |
| 2545 | return 0; |
| 2546 | } |
Johan Hedberg | 5fc16cc | 2015-11-11 08:11:16 +0200 | [diff] [blame] | 2547 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 2548 | static int update_bg_scan(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 2549 | { |
| 2550 | hci_dev_lock(req->hdev); |
| 2551 | __hci_update_background_scan(req); |
| 2552 | hci_dev_unlock(req->hdev); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 2553 | return 0; |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | static void bg_scan_update(struct work_struct *work) |
| 2557 | { |
| 2558 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 2559 | bg_scan_update); |
Johan Hedberg | 84235d2 | 2015-11-11 08:11:20 +0200 | [diff] [blame] | 2560 | struct hci_conn *conn; |
| 2561 | u8 status; |
| 2562 | int err; |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 2563 | |
Johan Hedberg | 84235d2 | 2015-11-11 08:11:20 +0200 | [diff] [blame] | 2564 | err = hci_req_sync(hdev, update_bg_scan, 0, HCI_CMD_TIMEOUT, &status); |
| 2565 | if (!err) |
| 2566 | return; |
| 2567 | |
| 2568 | hci_dev_lock(hdev); |
| 2569 | |
| 2570 | conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); |
| 2571 | if (conn) |
| 2572 | hci_le_conn_failed(conn, status); |
| 2573 | |
| 2574 | hci_dev_unlock(hdev); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 2577 | static int le_scan_disable(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2578 | { |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 2579 | hci_req_add_le_scan_disable(req, false); |
Johan Hedberg | a1d01db | 2015-11-11 08:11:25 +0200 | [diff] [blame] | 2580 | return 0; |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2583 | static int bredr_inquiry(struct hci_request *req, unsigned long opt) |
| 2584 | { |
| 2585 | u8 length = opt; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 2586 | const u8 giac[3] = { 0x33, 0x8b, 0x9e }; |
| 2587 | const u8 liac[3] = { 0x00, 0x8b, 0x9e }; |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2588 | struct hci_cp_inquiry cp; |
| 2589 | |
Archie Pusaka | 06752d1 | 2021-04-01 11:11:33 +0800 | [diff] [blame] | 2590 | if (test_bit(HCI_INQUIRY, &req->hdev->flags)) |
| 2591 | return 0; |
| 2592 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2593 | bt_dev_dbg(req->hdev, ""); |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2594 | |
| 2595 | hci_dev_lock(req->hdev); |
| 2596 | hci_inquiry_cache_flush(req->hdev); |
| 2597 | hci_dev_unlock(req->hdev); |
| 2598 | |
| 2599 | memset(&cp, 0, sizeof(cp)); |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 2600 | |
| 2601 | if (req->hdev->discovery.limited) |
| 2602 | memcpy(&cp.lap, liac, sizeof(cp.lap)); |
| 2603 | else |
| 2604 | memcpy(&cp.lap, giac, sizeof(cp.lap)); |
| 2605 | |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2606 | cp.length = length; |
| 2607 | |
| 2608 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 2609 | |
| 2610 | return 0; |
| 2611 | } |
| 2612 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2613 | static void le_scan_disable_work(struct work_struct *work) |
| 2614 | { |
| 2615 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 2616 | le_scan_disable.work); |
| 2617 | u8 status; |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2618 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2619 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2620 | |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2621 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2622 | return; |
| 2623 | |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2624 | cancel_delayed_work(&hdev->le_scan_restart); |
| 2625 | |
| 2626 | hci_req_sync(hdev, le_scan_disable, 0, HCI_CMD_TIMEOUT, &status); |
| 2627 | if (status) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2628 | bt_dev_err(hdev, "failed to disable LE scan: status 0x%02x", |
| 2629 | status); |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2630 | return; |
| 2631 | } |
| 2632 | |
| 2633 | hdev->discovery.scan_start = 0; |
| 2634 | |
| 2635 | /* If we were running LE only scan, change discovery state. If |
| 2636 | * we were running both LE and BR/EDR inquiry simultaneously, |
| 2637 | * and BR/EDR inquiry is already finished, stop discovery, |
| 2638 | * otherwise BR/EDR inquiry will stop discovery when finished. |
| 2639 | * If we will resolve remote device name, do not change |
| 2640 | * discovery state. |
| 2641 | */ |
| 2642 | |
| 2643 | if (hdev->discovery.type == DISCOV_TYPE_LE) |
| 2644 | goto discov_stopped; |
| 2645 | |
| 2646 | if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED) |
| 2647 | return; |
| 2648 | |
| 2649 | if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) { |
| 2650 | if (!test_bit(HCI_INQUIRY, &hdev->flags) && |
| 2651 | hdev->discovery.state != DISCOVERY_RESOLVING) |
| 2652 | goto discov_stopped; |
| 2653 | |
| 2654 | return; |
| 2655 | } |
| 2656 | |
| 2657 | hci_req_sync(hdev, bredr_inquiry, DISCOV_INTERLEAVED_INQUIRY_LEN, |
| 2658 | HCI_CMD_TIMEOUT, &status); |
| 2659 | if (status) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2660 | bt_dev_err(hdev, "inquiry failed: status 0x%02x", status); |
Johan Hedberg | f4a2cb4 | 2015-11-11 12:24:22 +0200 | [diff] [blame] | 2661 | goto discov_stopped; |
| 2662 | } |
| 2663 | |
| 2664 | return; |
| 2665 | |
| 2666 | discov_stopped: |
| 2667 | hci_dev_lock(hdev); |
| 2668 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2669 | hci_dev_unlock(hdev); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2672 | static int le_scan_restart(struct hci_request *req, unsigned long opt) |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2673 | { |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2674 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2675 | |
| 2676 | /* If controller is not scanning we are done. */ |
| 2677 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
| 2678 | return 0; |
| 2679 | |
Abhishek Pandit-Subedi | 3a0377d | 2020-06-24 11:34:19 -0700 | [diff] [blame] | 2680 | if (hdev->scanning_paused) { |
| 2681 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 2685 | hci_req_add_le_scan_disable(req, false); |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2686 | |
Jaganath Kanakkassery | a2344b9 | 2018-07-06 17:05:28 +0530 | [diff] [blame] | 2687 | if (use_ext_scan(hdev)) { |
| 2688 | struct hci_cp_le_set_ext_scan_enable ext_enable_cp; |
| 2689 | |
| 2690 | memset(&ext_enable_cp, 0, sizeof(ext_enable_cp)); |
| 2691 | ext_enable_cp.enable = LE_SCAN_ENABLE; |
| 2692 | ext_enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 2693 | |
| 2694 | hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE, |
| 2695 | sizeof(ext_enable_cp), &ext_enable_cp); |
| 2696 | } else { |
| 2697 | struct hci_cp_le_set_scan_enable cp; |
| 2698 | |
| 2699 | memset(&cp, 0, sizeof(cp)); |
| 2700 | cp.enable = LE_SCAN_ENABLE; |
| 2701 | cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 2702 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
| 2703 | } |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2704 | |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
| 2708 | static void le_scan_restart_work(struct work_struct *work) |
| 2709 | { |
| 2710 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 2711 | le_scan_restart.work); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2712 | unsigned long timeout, duration, scan_start, now; |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2713 | u8 status; |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2714 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2715 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2716 | |
Johan Hedberg | 3dfe590 | 2015-11-11 12:24:23 +0200 | [diff] [blame] | 2717 | hci_req_sync(hdev, le_scan_restart, 0, HCI_CMD_TIMEOUT, &status); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2718 | if (status) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2719 | bt_dev_err(hdev, "failed to restart LE scan: status %d", |
| 2720 | status); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 2721 | return; |
| 2722 | } |
| 2723 | |
| 2724 | hci_dev_lock(hdev); |
| 2725 | |
| 2726 | if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) || |
| 2727 | !hdev->discovery.scan_start) |
| 2728 | goto unlock; |
| 2729 | |
| 2730 | /* When the scan was started, hdev->le_scan_disable has been queued |
| 2731 | * after duration from scan_start. During scan restart this job |
| 2732 | * has been canceled, and we need to queue it again after proper |
| 2733 | * timeout, to make sure that scan does not run indefinitely. |
| 2734 | */ |
| 2735 | duration = hdev->discovery.scan_duration; |
| 2736 | scan_start = hdev->discovery.scan_start; |
| 2737 | now = jiffies; |
| 2738 | if (now - scan_start <= duration) { |
| 2739 | int elapsed; |
| 2740 | |
| 2741 | if (now >= scan_start) |
| 2742 | elapsed = now - scan_start; |
| 2743 | else |
| 2744 | elapsed = ULONG_MAX - scan_start + now; |
| 2745 | |
| 2746 | timeout = duration - elapsed; |
| 2747 | } else { |
| 2748 | timeout = 0; |
| 2749 | } |
| 2750 | |
| 2751 | queue_delayed_work(hdev->req_workqueue, |
| 2752 | &hdev->le_scan_disable, timeout); |
| 2753 | |
| 2754 | unlock: |
| 2755 | hci_dev_unlock(hdev); |
| 2756 | } |
| 2757 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2758 | static int active_scan(struct hci_request *req, unsigned long opt) |
| 2759 | { |
| 2760 | uint16_t interval = opt; |
| 2761 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2762 | u8 own_addr_type; |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 2763 | /* Accept list is not used for discovery */ |
Marcel Holtmann | 849c9c3 | 2020-04-09 08:05:48 +0200 | [diff] [blame] | 2764 | u8 filter_policy = 0x00; |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 2765 | /* Default is to enable duplicates filter */ |
| 2766 | u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
Marcel Holtmann | e1d5723 | 2020-07-23 18:08:57 +0530 | [diff] [blame] | 2767 | /* Discovery doesn't require controller address resolution */ |
| 2768 | bool addr_resolv = false; |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2769 | int err; |
| 2770 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2771 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2772 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2773 | /* If controller is scanning, it means the background scanning is |
| 2774 | * running. Thus, we should temporarily stop it in order to set the |
| 2775 | * discovery scanning parameters. |
| 2776 | */ |
Howard Chung | 422bb17 | 2020-11-26 12:22:23 +0800 | [diff] [blame] | 2777 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 2778 | hci_req_add_le_scan_disable(req, false); |
Howard Chung | 422bb17 | 2020-11-26 12:22:23 +0800 | [diff] [blame] | 2779 | cancel_interleave_scan(hdev); |
| 2780 | } |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2781 | |
| 2782 | /* All active scans will be done with either a resolvable private |
| 2783 | * address (when privacy feature has been enabled) or non-resolvable |
| 2784 | * private address. |
| 2785 | */ |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 2786 | err = hci_update_random_address(req, true, scan_use_rpa(hdev), |
| 2787 | &own_addr_type); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2788 | if (err < 0) |
| 2789 | own_addr_type = ADDR_LE_DEV_PUBLIC; |
| 2790 | |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 2791 | if (hci_is_adv_monitoring(hdev)) { |
| 2792 | /* Duplicate filter should be disabled when some advertisement |
| 2793 | * monitor is activated, otherwise AdvMon can only receive one |
| 2794 | * advertisement for one peer(*) during active scanning, and |
| 2795 | * might report loss to these peers. |
| 2796 | * |
| 2797 | * Note that different controllers have different meanings of |
| 2798 | * |duplicate|. Some of them consider packets with the same |
| 2799 | * address as duplicate, and others consider packets with the |
| 2800 | * same address and the same RSSI as duplicate. Although in the |
| 2801 | * latter case we don't need to disable duplicate filter, but |
| 2802 | * it is common to have active scanning for a short period of |
| 2803 | * time, the power impact should be neglectable. |
| 2804 | */ |
| 2805 | filter_dup = LE_SCAN_FILTER_DUP_DISABLE; |
| 2806 | } |
| 2807 | |
Alain Michaud | d4edda0 | 2020-06-29 17:04:15 +0000 | [diff] [blame] | 2808 | hci_req_start_scan(req, LE_SCAN_ACTIVE, interval, |
| 2809 | hdev->le_scan_window_discovery, own_addr_type, |
Yun-Hao Chung | c32d624 | 2021-05-20 13:12:09 +0800 | [diff] [blame] | 2810 | filter_policy, filter_dup, addr_resolv); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2811 | return 0; |
| 2812 | } |
| 2813 | |
| 2814 | static int interleaved_discov(struct hci_request *req, unsigned long opt) |
| 2815 | { |
| 2816 | int err; |
| 2817 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2818 | bt_dev_dbg(req->hdev, ""); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2819 | |
| 2820 | err = active_scan(req, opt); |
| 2821 | if (err) |
| 2822 | return err; |
| 2823 | |
Johan Hedberg | 7df26b5 | 2015-11-11 12:24:21 +0200 | [diff] [blame] | 2824 | return bredr_inquiry(req, DISCOV_BREDR_INQUIRY_LEN); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | static void start_discovery(struct hci_dev *hdev, u8 *status) |
| 2828 | { |
| 2829 | unsigned long timeout; |
| 2830 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2831 | bt_dev_dbg(hdev, "type %u", hdev->discovery.type); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2832 | |
| 2833 | switch (hdev->discovery.type) { |
| 2834 | case DISCOV_TYPE_BREDR: |
| 2835 | if (!hci_dev_test_flag(hdev, HCI_INQUIRY)) |
Johan Hedberg | 7df26b5 | 2015-11-11 12:24:21 +0200 | [diff] [blame] | 2836 | hci_req_sync(hdev, bredr_inquiry, |
| 2837 | DISCOV_BREDR_INQUIRY_LEN, HCI_CMD_TIMEOUT, |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2838 | status); |
| 2839 | return; |
| 2840 | case DISCOV_TYPE_INTERLEAVED: |
| 2841 | /* When running simultaneous discovery, the LE scanning time |
| 2842 | * should occupy the whole discovery time sine BR/EDR inquiry |
| 2843 | * and LE scanning are scheduled by the controller. |
| 2844 | * |
| 2845 | * For interleaving discovery in comparison, BR/EDR inquiry |
| 2846 | * and LE scanning are done sequentially with separate |
| 2847 | * timeouts. |
| 2848 | */ |
| 2849 | if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, |
| 2850 | &hdev->quirks)) { |
| 2851 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
| 2852 | /* During simultaneous discovery, we double LE scan |
| 2853 | * interval. We must leave some time for the controller |
| 2854 | * to do BR/EDR inquiry. |
| 2855 | */ |
| 2856 | hci_req_sync(hdev, interleaved_discov, |
Alain Michaud | d4edda0 | 2020-06-29 17:04:15 +0000 | [diff] [blame] | 2857 | hdev->le_scan_int_discovery * 2, HCI_CMD_TIMEOUT, |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2858 | status); |
| 2859 | break; |
| 2860 | } |
| 2861 | |
| 2862 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Alain Michaud | d4edda0 | 2020-06-29 17:04:15 +0000 | [diff] [blame] | 2863 | hci_req_sync(hdev, active_scan, hdev->le_scan_int_discovery, |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2864 | HCI_CMD_TIMEOUT, status); |
| 2865 | break; |
| 2866 | case DISCOV_TYPE_LE: |
| 2867 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Alain Michaud | d4edda0 | 2020-06-29 17:04:15 +0000 | [diff] [blame] | 2868 | hci_req_sync(hdev, active_scan, hdev->le_scan_int_discovery, |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2869 | HCI_CMD_TIMEOUT, status); |
| 2870 | break; |
| 2871 | default: |
| 2872 | *status = HCI_ERROR_UNSPECIFIED; |
| 2873 | return; |
| 2874 | } |
| 2875 | |
| 2876 | if (*status) |
| 2877 | return; |
| 2878 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2879 | bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout)); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 2880 | |
| 2881 | /* When service discovery is used and the controller has a |
| 2882 | * strict duplicate filter, it is important to remember the |
| 2883 | * start and duration of the scan. This is required for |
| 2884 | * restarting scanning during the discovery phase. |
| 2885 | */ |
| 2886 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) && |
| 2887 | hdev->discovery.result_filtering) { |
| 2888 | hdev->discovery.scan_start = jiffies; |
| 2889 | hdev->discovery.scan_duration = timeout; |
| 2890 | } |
| 2891 | |
| 2892 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable, |
| 2893 | timeout); |
| 2894 | } |
| 2895 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 2896 | bool hci_req_stop_discovery(struct hci_request *req) |
| 2897 | { |
| 2898 | struct hci_dev *hdev = req->hdev; |
| 2899 | struct discovery_state *d = &hdev->discovery; |
| 2900 | struct hci_cp_remote_name_req_cancel cp; |
| 2901 | struct inquiry_entry *e; |
| 2902 | bool ret = false; |
| 2903 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 2904 | bt_dev_dbg(hdev, "state %u", hdev->discovery.state); |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 2905 | |
| 2906 | if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) { |
| 2907 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 2908 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 2909 | |
| 2910 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
| 2911 | cancel_delayed_work(&hdev->le_scan_disable); |
Sonny Sasaka | c06632a | 2021-03-15 10:30:59 -0700 | [diff] [blame] | 2912 | cancel_delayed_work(&hdev->le_scan_restart); |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 2913 | hci_req_add_le_scan_disable(req, false); |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | ret = true; |
| 2917 | } else { |
| 2918 | /* Passive scanning */ |
| 2919 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 2920 | hci_req_add_le_scan_disable(req, false); |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 2921 | ret = true; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | /* No further actions needed for LE-only discovery */ |
| 2926 | if (d->type == DISCOV_TYPE_LE) |
| 2927 | return ret; |
| 2928 | |
| 2929 | if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) { |
| 2930 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 2931 | NAME_PENDING); |
| 2932 | if (!e) |
| 2933 | return ret; |
| 2934 | |
| 2935 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 2936 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 2937 | &cp); |
| 2938 | ret = true; |
| 2939 | } |
| 2940 | |
| 2941 | return ret; |
| 2942 | } |
| 2943 | |
Kiran K | 9798fbd | 2021-09-07 15:42:44 +0530 | [diff] [blame] | 2944 | static void config_data_path_complete(struct hci_dev *hdev, u8 status, |
| 2945 | u16 opcode) |
| 2946 | { |
| 2947 | bt_dev_dbg(hdev, "status %u", status); |
| 2948 | } |
| 2949 | |
| 2950 | int hci_req_configure_datapath(struct hci_dev *hdev, struct bt_codec *codec) |
| 2951 | { |
| 2952 | struct hci_request req; |
| 2953 | int err; |
| 2954 | __u8 vnd_len, *vnd_data = NULL; |
| 2955 | struct hci_op_configure_data_path *cmd = NULL; |
| 2956 | |
| 2957 | hci_req_init(&req, hdev); |
| 2958 | |
| 2959 | err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len, |
| 2960 | &vnd_data); |
| 2961 | if (err < 0) |
| 2962 | goto error; |
| 2963 | |
| 2964 | cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL); |
| 2965 | if (!cmd) { |
| 2966 | err = -ENOMEM; |
| 2967 | goto error; |
| 2968 | } |
| 2969 | |
| 2970 | err = hdev->get_data_path_id(hdev, &cmd->data_path_id); |
| 2971 | if (err < 0) |
| 2972 | goto error; |
| 2973 | |
| 2974 | cmd->vnd_len = vnd_len; |
| 2975 | memcpy(cmd->vnd_data, vnd_data, vnd_len); |
| 2976 | |
| 2977 | cmd->direction = 0x00; |
| 2978 | hci_req_add(&req, HCI_CONFIGURE_DATA_PATH, sizeof(*cmd) + vnd_len, cmd); |
| 2979 | |
| 2980 | cmd->direction = 0x01; |
| 2981 | hci_req_add(&req, HCI_CONFIGURE_DATA_PATH, sizeof(*cmd) + vnd_len, cmd); |
| 2982 | |
| 2983 | err = hci_req_run(&req, config_data_path_complete); |
| 2984 | error: |
| 2985 | |
| 2986 | kfree(cmd); |
| 2987 | kfree(vnd_data); |
| 2988 | return err; |
| 2989 | } |
| 2990 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 2991 | static int stop_discovery(struct hci_request *req, unsigned long opt) |
| 2992 | { |
| 2993 | hci_dev_lock(req->hdev); |
| 2994 | hci_req_stop_discovery(req); |
| 2995 | hci_dev_unlock(req->hdev); |
| 2996 | |
| 2997 | return 0; |
| 2998 | } |
| 2999 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3000 | static void discov_update(struct work_struct *work) |
| 3001 | { |
| 3002 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 3003 | discov_update); |
| 3004 | u8 status = 0; |
| 3005 | |
| 3006 | switch (hdev->discovery.state) { |
| 3007 | case DISCOVERY_STARTING: |
| 3008 | start_discovery(hdev, &status); |
| 3009 | mgmt_start_discovery_complete(hdev, status); |
| 3010 | if (status) |
| 3011 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3012 | else |
| 3013 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3014 | break; |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 3015 | case DISCOVERY_STOPPING: |
| 3016 | hci_req_sync(hdev, stop_discovery, 0, HCI_CMD_TIMEOUT, &status); |
| 3017 | mgmt_stop_discovery_complete(hdev, status); |
| 3018 | if (!status) |
| 3019 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3020 | break; |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3021 | case DISCOVERY_STOPPED: |
| 3022 | default: |
| 3023 | return; |
| 3024 | } |
| 3025 | } |
| 3026 | |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 3027 | static void discov_off(struct work_struct *work) |
| 3028 | { |
| 3029 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 3030 | discov_off.work); |
| 3031 | |
Howard Chung | 22fbcfc | 2020-11-11 15:02:19 +0800 | [diff] [blame] | 3032 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 3033 | |
| 3034 | hci_dev_lock(hdev); |
| 3035 | |
| 3036 | /* When discoverable timeout triggers, then just make sure |
| 3037 | * the limited discoverable flag is cleared. Even in the case |
| 3038 | * of a timeout triggered from general discoverable, it is |
| 3039 | * safe to unconditionally clear the flag. |
| 3040 | */ |
| 3041 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 3042 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 3043 | hdev->discov_timeout = 0; |
| 3044 | |
| 3045 | hci_dev_unlock(hdev); |
| 3046 | |
| 3047 | hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, NULL); |
| 3048 | mgmt_new_settings(hdev); |
| 3049 | } |
| 3050 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3051 | static int powered_update_hci(struct hci_request *req, unsigned long opt) |
| 3052 | { |
| 3053 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3054 | u8 link_sec; |
| 3055 | |
| 3056 | hci_dev_lock(hdev); |
| 3057 | |
| 3058 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && |
| 3059 | !lmp_host_ssp_capable(hdev)) { |
| 3060 | u8 mode = 0x01; |
| 3061 | |
| 3062 | hci_req_add(req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode); |
| 3063 | |
| 3064 | if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) { |
| 3065 | u8 support = 0x01; |
| 3066 | |
| 3067 | hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT, |
| 3068 | sizeof(support), &support); |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 3073 | lmp_bredr_capable(hdev)) { |
| 3074 | struct hci_cp_write_le_host_supported cp; |
| 3075 | |
| 3076 | cp.le = 0x01; |
| 3077 | cp.simul = 0x00; |
| 3078 | |
| 3079 | /* Check first if we already have the right |
| 3080 | * host state (host features set) |
| 3081 | */ |
| 3082 | if (cp.le != lmp_host_le_capable(hdev) || |
| 3083 | cp.simul != lmp_host_le_br_capable(hdev)) |
| 3084 | hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 3085 | sizeof(cp), &cp); |
| 3086 | } |
| 3087 | |
Johan Hedberg | d6b7e2c | 2015-11-30 11:21:44 +0200 | [diff] [blame] | 3088 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3089 | /* Make sure the controller has a good default for |
| 3090 | * advertising data. This also applies to the case |
| 3091 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 3092 | */ |
Johan Hedberg | d6b7e2c | 2015-11-30 11:21:44 +0200 | [diff] [blame] | 3093 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 3094 | list_empty(&hdev->adv_instances)) { |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 3095 | int err; |
| 3096 | |
| 3097 | if (ext_adv_capable(hdev)) { |
| 3098 | err = __hci_req_setup_ext_adv_instance(req, |
| 3099 | 0x00); |
| 3100 | if (!err) |
| 3101 | __hci_req_update_scan_rsp_data(req, |
| 3102 | 0x00); |
| 3103 | } else { |
| 3104 | err = 0; |
| 3105 | __hci_req_update_adv_data(req, 0x00); |
| 3106 | __hci_req_update_scan_rsp_data(req, 0x00); |
| 3107 | } |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3108 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 3109 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 3110 | if (!ext_adv_capable(hdev)) |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 3111 | __hci_req_enable_advertising(req); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 3112 | else if (!err) |
Luiz Augusto von Dentz | 1d0fac2 | 2019-06-03 13:48:42 +0300 | [diff] [blame] | 3113 | __hci_req_enable_ext_advertising(req, |
| 3114 | 0x00); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 3115 | } |
Johan Hedberg | d6b7e2c | 2015-11-30 11:21:44 +0200 | [diff] [blame] | 3116 | } else if (!list_empty(&hdev->adv_instances)) { |
| 3117 | struct adv_info *adv_instance; |
| 3118 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3119 | adv_instance = list_first_entry(&hdev->adv_instances, |
| 3120 | struct adv_info, list); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3121 | __hci_req_schedule_adv_instance(req, |
Johan Hedberg | d6b7e2c | 2015-11-30 11:21:44 +0200 | [diff] [blame] | 3122 | adv_instance->instance, |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3123 | true); |
Johan Hedberg | d6b7e2c | 2015-11-30 11:21:44 +0200 | [diff] [blame] | 3124 | } |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); |
| 3128 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
| 3129 | hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, |
| 3130 | sizeof(link_sec), &link_sec); |
| 3131 | |
| 3132 | if (lmp_bredr_capable(hdev)) { |
| 3133 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
| 3134 | __hci_req_write_fast_connectable(req, true); |
| 3135 | else |
| 3136 | __hci_req_write_fast_connectable(req, false); |
| 3137 | __hci_req_update_scan(req); |
| 3138 | __hci_req_update_class(req); |
| 3139 | __hci_req_update_name(req); |
| 3140 | __hci_req_update_eir(req); |
| 3141 | } |
| 3142 | |
| 3143 | hci_dev_unlock(hdev); |
| 3144 | return 0; |
| 3145 | } |
| 3146 | |
| 3147 | int __hci_req_hci_power_on(struct hci_dev *hdev) |
| 3148 | { |
| 3149 | /* Register the available SMP channels (BR/EDR and LE) only when |
| 3150 | * successfully powering on the controller. This late |
| 3151 | * registration is required so that LE SMP can clearly decide if |
| 3152 | * the public address or static address is used. |
| 3153 | */ |
| 3154 | smp_register(hdev); |
| 3155 | |
| 3156 | return __hci_req_sync(hdev, powered_update_hci, 0, HCI_CMD_TIMEOUT, |
| 3157 | NULL); |
| 3158 | } |
| 3159 | |
Johan Hedberg | 5fc16cc | 2015-11-11 08:11:16 +0200 | [diff] [blame] | 3160 | void hci_request_setup(struct hci_dev *hdev) |
| 3161 | { |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3162 | INIT_WORK(&hdev->discov_update, discov_update); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 3163 | INIT_WORK(&hdev->bg_scan_update, bg_scan_update); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 3164 | INIT_WORK(&hdev->scan_update, scan_update_work); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 3165 | INIT_WORK(&hdev->connectable_update, connectable_update_work); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 3166 | INIT_WORK(&hdev->discoverable_update, discoverable_update_work); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 3167 | INIT_DELAYED_WORK(&hdev->discov_off, discov_off); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 3168 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work); |
| 3169 | INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 3170 | INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire); |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 3171 | INIT_DELAYED_WORK(&hdev->interleave_scan, interleave_scan_work); |
Johan Hedberg | 5fc16cc | 2015-11-11 08:11:16 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | void hci_request_cancel_all(struct hci_dev *hdev) |
| 3175 | { |
Johan Hedberg | 7df0f73 | 2015-11-12 15:15:00 +0200 | [diff] [blame] | 3176 | hci_req_sync_cancel(hdev, ENODEV); |
| 3177 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3178 | cancel_work_sync(&hdev->discov_update); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 3179 | cancel_work_sync(&hdev->bg_scan_update); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 3180 | cancel_work_sync(&hdev->scan_update); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 3181 | cancel_work_sync(&hdev->connectable_update); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 3182 | cancel_work_sync(&hdev->discoverable_update); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 3183 | cancel_delayed_work_sync(&hdev->discov_off); |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 3184 | cancel_delayed_work_sync(&hdev->le_scan_disable); |
| 3185 | cancel_delayed_work_sync(&hdev->le_scan_restart); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 3186 | |
| 3187 | if (hdev->adv_instance_timeout) { |
| 3188 | cancel_delayed_work_sync(&hdev->adv_instance_expire); |
| 3189 | hdev->adv_instance_timeout = 0; |
| 3190 | } |
Howard Chung | c4f1f40 | 2020-11-26 12:22:21 +0800 | [diff] [blame] | 3191 | |
| 3192 | cancel_interleave_scan(hdev); |
Johan Hedberg | 5fc16cc | 2015-11-11 08:11:16 +0200 | [diff] [blame] | 3193 | } |