Commit | Line | Data |
---|---|---|
6a98e383 MH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * BlueZ - Bluetooth protocol stack for Linux | |
4 | * | |
5 | * Copyright (C) 2021 Intel Corporation | |
6a42e9bf | 6 | * Copyright 2023 NXP |
6a98e383 MH |
7 | */ |
8 | ||
d0b13706 LAD |
9 | #include <linux/property.h> |
10 | ||
6a98e383 MH |
11 | #include <net/bluetooth/bluetooth.h> |
12 | #include <net/bluetooth/hci_core.h> | |
13 | #include <net/bluetooth/mgmt.h> | |
14 | ||
15 | #include "hci_request.h" | |
828cea2b | 16 | #include "hci_codec.h" |
d0b13706 | 17 | #include "hci_debugfs.h" |
6a98e383 | 18 | #include "smp.h" |
161510cc | 19 | #include "eir.h" |
d0b13706 LAD |
20 | #include "msft.h" |
21 | #include "aosp.h" | |
22 | #include "leds.h" | |
6a98e383 MH |
23 | |
24 | static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, | |
25 | struct sk_buff *skb) | |
26 | { | |
27 | bt_dev_dbg(hdev, "result 0x%2.2x", result); | |
28 | ||
29 | if (hdev->req_status != HCI_REQ_PEND) | |
30 | return; | |
31 | ||
32 | hdev->req_result = result; | |
33 | hdev->req_status = HCI_REQ_DONE; | |
34 | ||
cba6b758 LAD |
35 | if (skb) { |
36 | struct sock *sk = hci_skb_sk(skb); | |
37 | ||
38 | /* Drop sk reference if set */ | |
39 | if (sk) | |
40 | sock_put(sk); | |
41 | ||
42 | hdev->req_skb = skb_get(skb); | |
43 | } | |
44 | ||
6a98e383 MH |
45 | wake_up_interruptible(&hdev->req_wait_q); |
46 | } | |
47 | ||
48 | static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, | |
49 | u32 plen, const void *param, | |
50 | struct sock *sk) | |
51 | { | |
52 | int len = HCI_COMMAND_HDR_SIZE + plen; | |
53 | struct hci_command_hdr *hdr; | |
54 | struct sk_buff *skb; | |
55 | ||
56 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
57 | if (!skb) | |
58 | return NULL; | |
59 | ||
60 | hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE); | |
61 | hdr->opcode = cpu_to_le16(opcode); | |
62 | hdr->plen = plen; | |
63 | ||
64 | if (plen) | |
65 | skb_put_data(skb, param, plen); | |
66 | ||
67 | bt_dev_dbg(hdev, "skb len %d", skb->len); | |
68 | ||
69 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; | |
70 | hci_skb_opcode(skb) = opcode; | |
71 | ||
cba6b758 LAD |
72 | /* Grab a reference if command needs to be associated with a sock (e.g. |
73 | * likely mgmt socket that initiated the command). | |
74 | */ | |
75 | if (sk) { | |
76 | hci_skb_sk(skb) = sk; | |
77 | sock_hold(sk); | |
78 | } | |
79 | ||
6a98e383 MH |
80 | return skb; |
81 | } | |
82 | ||
83 | static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen, | |
84 | const void *param, u8 event, struct sock *sk) | |
85 | { | |
86 | struct hci_dev *hdev = req->hdev; | |
87 | struct sk_buff *skb; | |
88 | ||
89 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); | |
90 | ||
91 | /* If an error occurred during request building, there is no point in | |
92 | * queueing the HCI command. We can simply return. | |
93 | */ | |
94 | if (req->err) | |
95 | return; | |
96 | ||
97 | skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk); | |
98 | if (!skb) { | |
99 | bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)", | |
100 | opcode); | |
101 | req->err = -ENOMEM; | |
102 | return; | |
103 | } | |
104 | ||
105 | if (skb_queue_empty(&req->cmd_q)) | |
106 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; | |
107 | ||
85b56857 | 108 | hci_skb_event(skb) = event; |
6a98e383 MH |
109 | |
110 | skb_queue_tail(&req->cmd_q, skb); | |
111 | } | |
112 | ||
113 | static int hci_cmd_sync_run(struct hci_request *req) | |
114 | { | |
115 | struct hci_dev *hdev = req->hdev; | |
116 | struct sk_buff *skb; | |
117 | unsigned long flags; | |
118 | ||
119 | bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q)); | |
120 | ||
121 | /* If an error occurred during request building, remove all HCI | |
122 | * commands queued on the HCI request queue. | |
123 | */ | |
124 | if (req->err) { | |
125 | skb_queue_purge(&req->cmd_q); | |
126 | return req->err; | |
127 | } | |
128 | ||
129 | /* Do not allow empty requests */ | |
130 | if (skb_queue_empty(&req->cmd_q)) | |
131 | return -ENODATA; | |
132 | ||
133 | skb = skb_peek_tail(&req->cmd_q); | |
134 | bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete; | |
135 | bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB; | |
136 | ||
137 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); | |
138 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); | |
139 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); | |
140 | ||
141 | queue_work(hdev->workqueue, &hdev->cmd_work); | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | /* This function requires the caller holds hdev->req_lock. */ | |
147 | struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen, | |
148 | const void *param, u8 event, u32 timeout, | |
149 | struct sock *sk) | |
150 | { | |
151 | struct hci_request req; | |
152 | struct sk_buff *skb; | |
153 | int err = 0; | |
154 | ||
d0b13706 | 155 | bt_dev_dbg(hdev, "Opcode 0x%4x", opcode); |
6a98e383 MH |
156 | |
157 | hci_req_init(&req, hdev); | |
158 | ||
159 | hci_cmd_sync_add(&req, opcode, plen, param, event, sk); | |
160 | ||
161 | hdev->req_status = HCI_REQ_PEND; | |
162 | ||
163 | err = hci_cmd_sync_run(&req); | |
164 | if (err < 0) | |
165 | return ERR_PTR(err); | |
166 | ||
167 | err = wait_event_interruptible_timeout(hdev->req_wait_q, | |
168 | hdev->req_status != HCI_REQ_PEND, | |
169 | timeout); | |
170 | ||
171 | if (err == -ERESTARTSYS) | |
172 | return ERR_PTR(-EINTR); | |
173 | ||
174 | switch (hdev->req_status) { | |
175 | case HCI_REQ_DONE: | |
176 | err = -bt_to_errno(hdev->req_result); | |
177 | break; | |
178 | ||
179 | case HCI_REQ_CANCELED: | |
180 | err = -hdev->req_result; | |
181 | break; | |
182 | ||
183 | default: | |
184 | err = -ETIMEDOUT; | |
185 | break; | |
186 | } | |
187 | ||
188 | hdev->req_status = 0; | |
189 | hdev->req_result = 0; | |
190 | skb = hdev->req_skb; | |
191 | hdev->req_skb = NULL; | |
192 | ||
193 | bt_dev_dbg(hdev, "end: err %d", err); | |
194 | ||
195 | if (err < 0) { | |
196 | kfree_skb(skb); | |
197 | return ERR_PTR(err); | |
198 | } | |
199 | ||
6a98e383 MH |
200 | return skb; |
201 | } | |
202 | EXPORT_SYMBOL(__hci_cmd_sync_sk); | |
203 | ||
204 | /* This function requires the caller holds hdev->req_lock. */ | |
205 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, | |
206 | const void *param, u32 timeout) | |
207 | { | |
208 | return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL); | |
209 | } | |
210 | EXPORT_SYMBOL(__hci_cmd_sync); | |
211 | ||
212 | /* Send HCI command and wait for command complete event */ | |
213 | struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, | |
214 | const void *param, u32 timeout) | |
215 | { | |
216 | struct sk_buff *skb; | |
217 | ||
218 | if (!test_bit(HCI_UP, &hdev->flags)) | |
219 | return ERR_PTR(-ENETDOWN); | |
220 | ||
221 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); | |
222 | ||
223 | hci_req_sync_lock(hdev); | |
224 | skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); | |
225 | hci_req_sync_unlock(hdev); | |
226 | ||
227 | return skb; | |
228 | } | |
229 | EXPORT_SYMBOL(hci_cmd_sync); | |
230 | ||
231 | /* This function requires the caller holds hdev->req_lock. */ | |
232 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, | |
233 | const void *param, u8 event, u32 timeout) | |
234 | { | |
235 | return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, | |
236 | NULL); | |
237 | } | |
238 | EXPORT_SYMBOL(__hci_cmd_sync_ev); | |
239 | ||
240 | /* This function requires the caller holds hdev->req_lock. */ | |
241 | int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen, | |
242 | const void *param, u8 event, u32 timeout, | |
243 | struct sock *sk) | |
244 | { | |
245 | struct sk_buff *skb; | |
246 | u8 status; | |
247 | ||
248 | skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk); | |
abfeea47 | 249 | if (IS_ERR(skb)) { |
b62e7220 LAD |
250 | if (!event) |
251 | bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode, | |
252 | PTR_ERR(skb)); | |
6a98e383 MH |
253 | return PTR_ERR(skb); |
254 | } | |
255 | ||
abfeea47 LAD |
256 | /* If command return a status event skb will be set to NULL as there are |
257 | * no parameters, in case of failure IS_ERR(skb) would have be set to | |
258 | * the actual error would be found with PTR_ERR(skb). | |
259 | */ | |
260 | if (!skb) | |
261 | return 0; | |
262 | ||
6a98e383 MH |
263 | status = skb->data[0]; |
264 | ||
265 | kfree_skb(skb); | |
266 | ||
267 | return status; | |
268 | } | |
269 | EXPORT_SYMBOL(__hci_cmd_sync_status_sk); | |
270 | ||
271 | int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen, | |
272 | const void *param, u32 timeout) | |
273 | { | |
274 | return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout, | |
275 | NULL); | |
276 | } | |
277 | EXPORT_SYMBOL(__hci_cmd_sync_status); | |
278 | ||
279 | static void hci_cmd_sync_work(struct work_struct *work) | |
280 | { | |
281 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work); | |
6a98e383 MH |
282 | |
283 | bt_dev_dbg(hdev, ""); | |
284 | ||
008ee9eb LAD |
285 | /* Dequeue all entries and run them */ |
286 | while (1) { | |
287 | struct hci_cmd_sync_work_entry *entry; | |
6a98e383 | 288 | |
008ee9eb LAD |
289 | mutex_lock(&hdev->cmd_sync_work_lock); |
290 | entry = list_first_entry_or_null(&hdev->cmd_sync_work_list, | |
291 | struct hci_cmd_sync_work_entry, | |
292 | list); | |
293 | if (entry) | |
294 | list_del(&entry->list); | |
295 | mutex_unlock(&hdev->cmd_sync_work_lock); | |
6a98e383 | 296 | |
008ee9eb LAD |
297 | if (!entry) |
298 | break; | |
6a98e383 | 299 | |
008ee9eb | 300 | bt_dev_dbg(hdev, "entry %p", entry); |
6a98e383 | 301 | |
008ee9eb LAD |
302 | if (entry->func) { |
303 | int err; | |
6a98e383 | 304 | |
008ee9eb LAD |
305 | hci_req_sync_lock(hdev); |
306 | err = entry->func(hdev, entry->data); | |
307 | if (entry->destroy) | |
308 | entry->destroy(hdev, entry->data, err); | |
309 | hci_req_sync_unlock(hdev); | |
310 | } | |
311 | ||
312 | kfree(entry); | |
6a98e383 MH |
313 | } |
314 | } | |
315 | ||
744451c1 BB |
316 | static void hci_cmd_sync_cancel_work(struct work_struct *work) |
317 | { | |
318 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work); | |
319 | ||
320 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
321 | cancel_delayed_work_sync(&hdev->ncmd_timer); | |
322 | atomic_set(&hdev->cmd_cnt, 1); | |
323 | ||
324 | wake_up_interruptible(&hdev->req_wait_q); | |
325 | } | |
326 | ||
8ffde2a7 BG |
327 | static int hci_scan_disable_sync(struct hci_dev *hdev); |
328 | static int scan_disable_sync(struct hci_dev *hdev, void *data) | |
329 | { | |
330 | return hci_scan_disable_sync(hdev); | |
331 | } | |
332 | ||
333 | static int hci_inquiry_sync(struct hci_dev *hdev, u8 length); | |
334 | static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data) | |
335 | { | |
336 | return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN); | |
337 | } | |
338 | ||
339 | static void le_scan_disable(struct work_struct *work) | |
340 | { | |
341 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
342 | le_scan_disable.work); | |
343 | int status; | |
344 | ||
345 | bt_dev_dbg(hdev, ""); | |
346 | hci_dev_lock(hdev); | |
347 | ||
348 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) | |
349 | goto _return; | |
350 | ||
351 | cancel_delayed_work(&hdev->le_scan_restart); | |
352 | ||
353 | status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL); | |
354 | if (status) { | |
355 | bt_dev_err(hdev, "failed to disable LE scan: %d", status); | |
356 | goto _return; | |
357 | } | |
358 | ||
359 | hdev->discovery.scan_start = 0; | |
360 | ||
361 | /* If we were running LE only scan, change discovery state. If | |
362 | * we were running both LE and BR/EDR inquiry simultaneously, | |
363 | * and BR/EDR inquiry is already finished, stop discovery, | |
364 | * otherwise BR/EDR inquiry will stop discovery when finished. | |
365 | * If we will resolve remote device name, do not change | |
366 | * discovery state. | |
367 | */ | |
368 | ||
369 | if (hdev->discovery.type == DISCOV_TYPE_LE) | |
370 | goto discov_stopped; | |
371 | ||
372 | if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED) | |
373 | goto _return; | |
374 | ||
375 | if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) { | |
376 | if (!test_bit(HCI_INQUIRY, &hdev->flags) && | |
377 | hdev->discovery.state != DISCOVERY_RESOLVING) | |
378 | goto discov_stopped; | |
379 | ||
380 | goto _return; | |
381 | } | |
382 | ||
383 | status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL); | |
384 | if (status) { | |
385 | bt_dev_err(hdev, "inquiry failed: status %d", status); | |
386 | goto discov_stopped; | |
387 | } | |
388 | ||
389 | goto _return; | |
390 | ||
391 | discov_stopped: | |
392 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); | |
393 | ||
394 | _return: | |
395 | hci_dev_unlock(hdev); | |
396 | } | |
397 | ||
27d54b77 BG |
398 | static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val, |
399 | u8 filter_dup); | |
400 | static int hci_le_scan_restart_sync(struct hci_dev *hdev) | |
401 | { | |
402 | /* If controller is not scanning we are done. */ | |
403 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) | |
404 | return 0; | |
405 | ||
406 | if (hdev->scanning_paused) { | |
407 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00); | |
412 | return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, | |
413 | LE_SCAN_FILTER_DUP_ENABLE); | |
414 | } | |
415 | ||
416 | static int le_scan_restart_sync(struct hci_dev *hdev, void *data) | |
417 | { | |
418 | return hci_le_scan_restart_sync(hdev); | |
419 | } | |
420 | ||
421 | static void le_scan_restart(struct work_struct *work) | |
422 | { | |
423 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
424 | le_scan_restart.work); | |
425 | unsigned long timeout, duration, scan_start, now; | |
426 | int status; | |
427 | ||
428 | bt_dev_dbg(hdev, ""); | |
429 | ||
430 | hci_dev_lock(hdev); | |
431 | ||
432 | status = hci_cmd_sync_queue(hdev, le_scan_restart_sync, NULL, NULL); | |
433 | if (status) { | |
434 | bt_dev_err(hdev, "failed to restart LE scan: status %d", | |
435 | status); | |
436 | goto unlock; | |
437 | } | |
438 | ||
439 | if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) || | |
440 | !hdev->discovery.scan_start) | |
441 | goto unlock; | |
442 | ||
443 | /* When the scan was started, hdev->le_scan_disable has been queued | |
444 | * after duration from scan_start. During scan restart this job | |
445 | * has been canceled, and we need to queue it again after proper | |
446 | * timeout, to make sure that scan does not run indefinitely. | |
447 | */ | |
448 | duration = hdev->discovery.scan_duration; | |
449 | scan_start = hdev->discovery.scan_start; | |
450 | now = jiffies; | |
451 | if (now - scan_start <= duration) { | |
452 | int elapsed; | |
453 | ||
454 | if (now >= scan_start) | |
455 | elapsed = now - scan_start; | |
456 | else | |
457 | elapsed = ULONG_MAX - scan_start + now; | |
458 | ||
459 | timeout = duration - elapsed; | |
460 | } else { | |
461 | timeout = 0; | |
462 | } | |
463 | ||
464 | queue_delayed_work(hdev->req_workqueue, | |
465 | &hdev->le_scan_disable, timeout); | |
466 | ||
467 | unlock: | |
468 | hci_dev_unlock(hdev); | |
469 | } | |
470 | ||
b338d917 BG |
471 | static int reenable_adv_sync(struct hci_dev *hdev, void *data) |
472 | { | |
473 | bt_dev_dbg(hdev, ""); | |
474 | ||
475 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && | |
476 | list_empty(&hdev->adv_instances)) | |
477 | return 0; | |
478 | ||
479 | if (hdev->cur_adv_instance) { | |
480 | return hci_schedule_adv_instance_sync(hdev, | |
481 | hdev->cur_adv_instance, | |
482 | true); | |
483 | } else { | |
484 | if (ext_adv_capable(hdev)) { | |
485 | hci_start_ext_adv_sync(hdev, 0x00); | |
486 | } else { | |
487 | hci_update_adv_data_sync(hdev, 0x00); | |
488 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
489 | hci_enable_advertising_sync(hdev); | |
490 | } | |
491 | } | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
496 | static void reenable_adv(struct work_struct *work) | |
497 | { | |
498 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
499 | reenable_adv_work); | |
500 | int status; | |
501 | ||
502 | bt_dev_dbg(hdev, ""); | |
503 | ||
504 | hci_dev_lock(hdev); | |
505 | ||
506 | status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL); | |
507 | if (status) | |
508 | bt_dev_err(hdev, "failed to reenable ADV: %d", status); | |
509 | ||
510 | hci_dev_unlock(hdev); | |
511 | } | |
512 | ||
c249ea9b BG |
513 | static void cancel_adv_timeout(struct hci_dev *hdev) |
514 | { | |
515 | if (hdev->adv_instance_timeout) { | |
516 | hdev->adv_instance_timeout = 0; | |
517 | cancel_delayed_work(&hdev->adv_instance_expire); | |
518 | } | |
519 | } | |
520 | ||
521 | /* For a single instance: | |
522 | * - force == true: The instance will be removed even when its remaining | |
523 | * lifetime is not zero. | |
524 | * - force == false: the instance will be deactivated but kept stored unless | |
525 | * the remaining lifetime is zero. | |
526 | * | |
527 | * For instance == 0x00: | |
528 | * - force == true: All instances will be removed regardless of their timeout | |
529 | * setting. | |
530 | * - force == false: Only instances that have a timeout will be removed. | |
531 | */ | |
532 | int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk, | |
533 | u8 instance, bool force) | |
534 | { | |
535 | struct adv_info *adv_instance, *n, *next_instance = NULL; | |
536 | int err; | |
537 | u8 rem_inst; | |
538 | ||
539 | /* Cancel any timeout concerning the removed instance(s). */ | |
540 | if (!instance || hdev->cur_adv_instance == instance) | |
541 | cancel_adv_timeout(hdev); | |
542 | ||
543 | /* Get the next instance to advertise BEFORE we remove | |
544 | * the current one. This can be the same instance again | |
545 | * if there is only one instance. | |
546 | */ | |
547 | if (instance && hdev->cur_adv_instance == instance) | |
548 | next_instance = hci_get_next_instance(hdev, instance); | |
549 | ||
550 | if (instance == 0x00) { | |
551 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, | |
552 | list) { | |
553 | if (!(force || adv_instance->timeout)) | |
554 | continue; | |
555 | ||
556 | rem_inst = adv_instance->instance; | |
557 | err = hci_remove_adv_instance(hdev, rem_inst); | |
558 | if (!err) | |
559 | mgmt_advertising_removed(sk, hdev, rem_inst); | |
560 | } | |
561 | } else { | |
562 | adv_instance = hci_find_adv_instance(hdev, instance); | |
563 | ||
564 | if (force || (adv_instance && adv_instance->timeout && | |
565 | !adv_instance->remaining_time)) { | |
566 | /* Don't advertise a removed instance. */ | |
567 | if (next_instance && | |
568 | next_instance->instance == instance) | |
569 | next_instance = NULL; | |
570 | ||
571 | err = hci_remove_adv_instance(hdev, instance); | |
572 | if (!err) | |
573 | mgmt_advertising_removed(sk, hdev, instance); | |
574 | } | |
575 | } | |
576 | ||
577 | if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
578 | return 0; | |
579 | ||
580 | if (next_instance && !ext_adv_capable(hdev)) | |
581 | return hci_schedule_adv_instance_sync(hdev, | |
582 | next_instance->instance, | |
583 | false); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data) | |
589 | { | |
590 | u8 instance = *(u8 *)data; | |
591 | ||
592 | kfree(data); | |
593 | ||
594 | hci_clear_adv_instance_sync(hdev, NULL, instance, false); | |
595 | ||
596 | if (list_empty(&hdev->adv_instances)) | |
597 | return hci_disable_advertising_sync(hdev); | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
602 | static void adv_timeout_expire(struct work_struct *work) | |
603 | { | |
604 | u8 *inst_ptr; | |
605 | struct hci_dev *hdev = container_of(work, struct hci_dev, | |
606 | adv_instance_expire.work); | |
607 | ||
608 | bt_dev_dbg(hdev, ""); | |
609 | ||
610 | hci_dev_lock(hdev); | |
611 | ||
612 | hdev->adv_instance_timeout = 0; | |
613 | ||
614 | if (hdev->cur_adv_instance == 0x00) | |
615 | goto unlock; | |
616 | ||
617 | inst_ptr = kmalloc(1, GFP_KERNEL); | |
618 | if (!inst_ptr) | |
619 | goto unlock; | |
620 | ||
621 | *inst_ptr = hdev->cur_adv_instance; | |
622 | hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL); | |
623 | ||
624 | unlock: | |
625 | hci_dev_unlock(hdev); | |
626 | } | |
627 | ||
6a98e383 MH |
628 | void hci_cmd_sync_init(struct hci_dev *hdev) |
629 | { | |
630 | INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work); | |
631 | INIT_LIST_HEAD(&hdev->cmd_sync_work_list); | |
632 | mutex_init(&hdev->cmd_sync_work_lock); | |
1857c199 | 633 | mutex_init(&hdev->unregister_lock); |
744451c1 BB |
634 | |
635 | INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work); | |
b338d917 | 636 | INIT_WORK(&hdev->reenable_adv_work, reenable_adv); |
8ffde2a7 | 637 | INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable); |
27d54b77 | 638 | INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart); |
c249ea9b | 639 | INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire); |
6a98e383 MH |
640 | } |
641 | ||
642 | void hci_cmd_sync_clear(struct hci_dev *hdev) | |
643 | { | |
644 | struct hci_cmd_sync_work_entry *entry, *tmp; | |
645 | ||
646 | cancel_work_sync(&hdev->cmd_sync_work); | |
b338d917 | 647 | cancel_work_sync(&hdev->reenable_adv_work); |
6a98e383 | 648 | |
1c66bee4 | 649 | mutex_lock(&hdev->cmd_sync_work_lock); |
6a98e383 MH |
650 | list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) { |
651 | if (entry->destroy) | |
652 | entry->destroy(hdev, entry->data, -ECANCELED); | |
653 | ||
654 | list_del(&entry->list); | |
655 | kfree(entry); | |
656 | } | |
1c66bee4 | 657 | mutex_unlock(&hdev->cmd_sync_work_lock); |
6a98e383 MH |
658 | } |
659 | ||
744451c1 | 660 | void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err) |
914b08b3 BB |
661 | { |
662 | bt_dev_dbg(hdev, "err 0x%2.2x", err); | |
663 | ||
664 | if (hdev->req_status == HCI_REQ_PEND) { | |
665 | hdev->req_result = err; | |
666 | hdev->req_status = HCI_REQ_CANCELED; | |
667 | ||
668 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
669 | cancel_delayed_work_sync(&hdev->ncmd_timer); | |
670 | atomic_set(&hdev->cmd_cnt, 1); | |
671 | ||
672 | wake_up_interruptible(&hdev->req_wait_q); | |
673 | } | |
674 | } | |
744451c1 BB |
675 | |
676 | void hci_cmd_sync_cancel(struct hci_dev *hdev, int err) | |
677 | { | |
678 | bt_dev_dbg(hdev, "err 0x%2.2x", err); | |
679 | ||
680 | if (hdev->req_status == HCI_REQ_PEND) { | |
681 | hdev->req_result = err; | |
682 | hdev->req_status = HCI_REQ_CANCELED; | |
683 | ||
684 | queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work); | |
685 | } | |
686 | } | |
914b08b3 BB |
687 | EXPORT_SYMBOL(hci_cmd_sync_cancel); |
688 | ||
d883a466 LAD |
689 | /* Submit HCI command to be run in as cmd_sync_work: |
690 | * | |
691 | * - hdev must _not_ be unregistered | |
692 | */ | |
693 | int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, | |
694 | void *data, hci_cmd_sync_work_destroy_t destroy) | |
6a98e383 MH |
695 | { |
696 | struct hci_cmd_sync_work_entry *entry; | |
1857c199 | 697 | int err = 0; |
6a98e383 | 698 | |
1857c199 ZJ |
699 | mutex_lock(&hdev->unregister_lock); |
700 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { | |
701 | err = -ENODEV; | |
702 | goto unlock; | |
703 | } | |
0b94f265 | 704 | |
6a98e383 | 705 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
1857c199 ZJ |
706 | if (!entry) { |
707 | err = -ENOMEM; | |
708 | goto unlock; | |
709 | } | |
6a98e383 MH |
710 | entry->func = func; |
711 | entry->data = data; | |
712 | entry->destroy = destroy; | |
713 | ||
714 | mutex_lock(&hdev->cmd_sync_work_lock); | |
715 | list_add_tail(&entry->list, &hdev->cmd_sync_work_list); | |
716 | mutex_unlock(&hdev->cmd_sync_work_lock); | |
717 | ||
718 | queue_work(hdev->req_workqueue, &hdev->cmd_sync_work); | |
719 | ||
1857c199 ZJ |
720 | unlock: |
721 | mutex_unlock(&hdev->unregister_lock); | |
722 | return err; | |
6a98e383 | 723 | } |
d883a466 LAD |
724 | EXPORT_SYMBOL(hci_cmd_sync_submit); |
725 | ||
726 | /* Queue HCI command: | |
727 | * | |
728 | * - hdev must be running | |
729 | */ | |
730 | int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, | |
731 | void *data, hci_cmd_sync_work_destroy_t destroy) | |
732 | { | |
733 | /* Only queue command if hdev is running which means it had been opened | |
734 | * and is either on init phase or is already up. | |
735 | */ | |
736 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
737 | return -ENETDOWN; | |
738 | ||
739 | return hci_cmd_sync_submit(hdev, func, data, destroy); | |
740 | } | |
6a98e383 | 741 | EXPORT_SYMBOL(hci_cmd_sync_queue); |
161510cc LAD |
742 | |
743 | int hci_update_eir_sync(struct hci_dev *hdev) | |
744 | { | |
745 | struct hci_cp_write_eir cp; | |
746 | ||
747 | bt_dev_dbg(hdev, ""); | |
748 | ||
749 | if (!hdev_is_powered(hdev)) | |
750 | return 0; | |
751 | ||
752 | if (!lmp_ext_inq_capable(hdev)) | |
753 | return 0; | |
754 | ||
755 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
756 | return 0; | |
757 | ||
758 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) | |
759 | return 0; | |
760 | ||
761 | memset(&cp, 0, sizeof(cp)); | |
762 | ||
763 | eir_create(hdev, cp.data); | |
764 | ||
765 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) | |
766 | return 0; | |
767 | ||
768 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); | |
769 | ||
770 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, | |
771 | HCI_CMD_TIMEOUT); | |
772 | } | |
773 | ||
774 | static u8 get_service_classes(struct hci_dev *hdev) | |
775 | { | |
776 | struct bt_uuid *uuid; | |
777 | u8 val = 0; | |
778 | ||
779 | list_for_each_entry(uuid, &hdev->uuids, list) | |
780 | val |= uuid->svc_hint; | |
781 | ||
782 | return val; | |
783 | } | |
784 | ||
785 | int hci_update_class_sync(struct hci_dev *hdev) | |
786 | { | |
787 | u8 cod[3]; | |
788 | ||
789 | bt_dev_dbg(hdev, ""); | |
790 | ||
791 | if (!hdev_is_powered(hdev)) | |
792 | return 0; | |
793 | ||
794 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
795 | return 0; | |
796 | ||
797 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) | |
798 | return 0; | |
799 | ||
800 | cod[0] = hdev->minor_class; | |
801 | cod[1] = hdev->major_class; | |
802 | cod[2] = get_service_classes(hdev); | |
803 | ||
804 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) | |
805 | cod[1] |= 0x20; | |
806 | ||
807 | if (memcmp(cod, hdev->dev_class, 3) == 0) | |
808 | return 0; | |
809 | ||
810 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV, | |
811 | sizeof(cod), cod, HCI_CMD_TIMEOUT); | |
812 | } | |
cba6b758 LAD |
813 | |
814 | static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable) | |
815 | { | |
816 | /* If there is no connection we are OK to advertise. */ | |
817 | if (hci_conn_num(hdev, LE_LINK) == 0) | |
818 | return true; | |
819 | ||
820 | /* Check le_states if there is any connection in peripheral role. */ | |
821 | if (hdev->conn_hash.le_num_peripheral > 0) { | |
822 | /* Peripheral connection state and non connectable mode | |
823 | * bit 20. | |
824 | */ | |
825 | if (!connectable && !(hdev->le_states[2] & 0x10)) | |
826 | return false; | |
827 | ||
828 | /* Peripheral connection state and connectable mode bit 38 | |
829 | * and scannable bit 21. | |
830 | */ | |
831 | if (connectable && (!(hdev->le_states[4] & 0x40) || | |
832 | !(hdev->le_states[2] & 0x20))) | |
833 | return false; | |
834 | } | |
835 | ||
836 | /* Check le_states if there is any connection in central role. */ | |
837 | if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) { | |
838 | /* Central connection state and non connectable mode bit 18. */ | |
839 | if (!connectable && !(hdev->le_states[2] & 0x02)) | |
840 | return false; | |
841 | ||
842 | /* Central connection state and connectable mode bit 35 and | |
843 | * scannable 19. | |
844 | */ | |
845 | if (connectable && (!(hdev->le_states[4] & 0x08) || | |
846 | !(hdev->le_states[2] & 0x08))) | |
847 | return false; | |
848 | } | |
849 | ||
850 | return true; | |
851 | } | |
852 | ||
853 | static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags) | |
854 | { | |
855 | /* If privacy is not enabled don't use RPA */ | |
856 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) | |
857 | return false; | |
858 | ||
859 | /* If basic privacy mode is enabled use RPA */ | |
860 | if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) | |
861 | return true; | |
862 | ||
863 | /* If limited privacy mode is enabled don't use RPA if we're | |
864 | * both discoverable and bondable. | |
865 | */ | |
866 | if ((flags & MGMT_ADV_FLAG_DISCOV) && | |
867 | hci_dev_test_flag(hdev, HCI_BONDABLE)) | |
868 | return false; | |
869 | ||
870 | /* We're neither bondable nor discoverable in the limited | |
871 | * privacy mode, therefore use RPA. | |
872 | */ | |
873 | return true; | |
874 | } | |
875 | ||
876 | static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa) | |
877 | { | |
878 | /* If we're advertising or initiating an LE connection we can't | |
879 | * go ahead and change the random address at this time. This is | |
880 | * because the eventual initiator address used for the | |
881 | * subsequently created connection will be undefined (some | |
882 | * controllers use the new address and others the one we had | |
883 | * when the operation started). | |
884 | * | |
885 | * In this kind of scenario skip the update and let the random | |
886 | * address be updated at the next cycle. | |
887 | */ | |
888 | if (hci_dev_test_flag(hdev, HCI_LE_ADV) || | |
889 | hci_lookup_le_connect(hdev)) { | |
890 | bt_dev_dbg(hdev, "Deferring random address update"); | |
891 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); | |
892 | return 0; | |
893 | } | |
894 | ||
895 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR, | |
896 | 6, rpa, HCI_CMD_TIMEOUT); | |
897 | } | |
898 | ||
899 | int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy, | |
900 | bool rpa, u8 *own_addr_type) | |
901 | { | |
902 | int err; | |
903 | ||
904 | /* If privacy is enabled use a resolvable private address. If | |
905 | * current RPA has expired or there is something else than | |
906 | * the current RPA in use, then generate a new one. | |
907 | */ | |
908 | if (rpa) { | |
909 | /* If Controller supports LL Privacy use own address type is | |
910 | * 0x03 | |
911 | */ | |
ad383c2c | 912 | if (use_ll_privacy(hdev)) |
cba6b758 LAD |
913 | *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; |
914 | else | |
915 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
916 | ||
917 | /* Check if RPA is valid */ | |
918 | if (rpa_valid(hdev)) | |
919 | return 0; | |
920 | ||
921 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); | |
922 | if (err < 0) { | |
923 | bt_dev_err(hdev, "failed to generate new RPA"); | |
924 | return err; | |
925 | } | |
926 | ||
927 | err = hci_set_random_addr_sync(hdev, &hdev->rpa); | |
928 | if (err) | |
929 | return err; | |
930 | ||
931 | return 0; | |
932 | } | |
933 | ||
934 | /* In case of required privacy without resolvable private address, | |
935 | * use an non-resolvable private address. This is useful for active | |
936 | * scanning and non-connectable advertising. | |
937 | */ | |
938 | if (require_privacy) { | |
939 | bdaddr_t nrpa; | |
940 | ||
941 | while (true) { | |
942 | /* The non-resolvable private address is generated | |
943 | * from random six bytes with the two most significant | |
944 | * bits cleared. | |
945 | */ | |
946 | get_random_bytes(&nrpa, 6); | |
947 | nrpa.b[5] &= 0x3f; | |
948 | ||
949 | /* The non-resolvable private address shall not be | |
950 | * equal to the public address. | |
951 | */ | |
952 | if (bacmp(&hdev->bdaddr, &nrpa)) | |
953 | break; | |
954 | } | |
955 | ||
956 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
957 | ||
958 | return hci_set_random_addr_sync(hdev, &nrpa); | |
959 | } | |
960 | ||
961 | /* If forcing static address is in use or there is no public | |
962 | * address use the static address as random address (but skip | |
963 | * the HCI command if the current random address is already the | |
964 | * static one. | |
965 | * | |
966 | * In case BR/EDR has been disabled on a dual-mode controller | |
967 | * and a static address has been configured, then use that | |
968 | * address instead of the public BR/EDR address. | |
969 | */ | |
970 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || | |
971 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || | |
972 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && | |
973 | bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
974 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
975 | if (bacmp(&hdev->static_addr, &hdev->random_addr)) | |
976 | return hci_set_random_addr_sync(hdev, | |
977 | &hdev->static_addr); | |
978 | return 0; | |
979 | } | |
980 | ||
981 | /* Neither privacy nor static address is being used so use a | |
982 | * public address. | |
983 | */ | |
984 | *own_addr_type = ADDR_LE_DEV_PUBLIC; | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
989 | static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) | |
990 | { | |
991 | struct hci_cp_le_set_ext_adv_enable *cp; | |
992 | struct hci_cp_ext_adv_set *set; | |
993 | u8 data[sizeof(*cp) + sizeof(*set) * 1]; | |
994 | u8 size; | |
995 | ||
996 | /* If request specifies an instance that doesn't exist, fail */ | |
997 | if (instance > 0) { | |
998 | struct adv_info *adv; | |
999 | ||
1000 | adv = hci_find_adv_instance(hdev, instance); | |
1001 | if (!adv) | |
1002 | return -EINVAL; | |
1003 | ||
1004 | /* If not enabled there is nothing to do */ | |
1005 | if (!adv->enabled) | |
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | memset(data, 0, sizeof(data)); | |
1010 | ||
1011 | cp = (void *)data; | |
1012 | set = (void *)cp->data; | |
1013 | ||
1014 | /* Instance 0x00 indicates all advertising instances will be disabled */ | |
1015 | cp->num_of_sets = !!instance; | |
1016 | cp->enable = 0x00; | |
1017 | ||
1018 | set->handle = instance; | |
1019 | ||
1020 | size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets; | |
1021 | ||
1022 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, | |
1023 | size, data, HCI_CMD_TIMEOUT); | |
1024 | } | |
1025 | ||
1026 | static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, | |
1027 | bdaddr_t *random_addr) | |
1028 | { | |
1029 | struct hci_cp_le_set_adv_set_rand_addr cp; | |
1030 | int err; | |
1031 | ||
1032 | if (!instance) { | |
1033 | /* Instance 0x00 doesn't have an adv_info, instead it uses | |
1034 | * hdev->random_addr to track its address so whenever it needs | |
1035 | * to be updated this also set the random address since | |
1036 | * hdev->random_addr is shared with scan state machine. | |
1037 | */ | |
1038 | err = hci_set_random_addr_sync(hdev, random_addr); | |
1039 | if (err) | |
1040 | return err; | |
1041 | } | |
1042 | ||
1043 | memset(&cp, 0, sizeof(cp)); | |
1044 | ||
1045 | cp.handle = instance; | |
1046 | bacpy(&cp.bdaddr, random_addr); | |
1047 | ||
1048 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, | |
1049 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1050 | } | |
1051 | ||
1052 | int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) | |
1053 | { | |
1054 | struct hci_cp_le_set_ext_adv_params cp; | |
1055 | bool connectable; | |
1056 | u32 flags; | |
1057 | bdaddr_t random_addr; | |
1058 | u8 own_addr_type; | |
1059 | int err; | |
1060 | struct adv_info *adv; | |
1061 | bool secondary_adv; | |
1062 | ||
1063 | if (instance > 0) { | |
1064 | adv = hci_find_adv_instance(hdev, instance); | |
1065 | if (!adv) | |
1066 | return -EINVAL; | |
1067 | } else { | |
1068 | adv = NULL; | |
1069 | } | |
1070 | ||
1071 | /* Updating parameters of an active instance will return a | |
1072 | * Command Disallowed error, so we must first disable the | |
1073 | * instance if it is active. | |
1074 | */ | |
1075 | if (adv && !adv->pending) { | |
1076 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | |
1077 | if (err) | |
1078 | return err; | |
1079 | } | |
1080 | ||
1081 | flags = hci_adv_instance_flags(hdev, instance); | |
1082 | ||
1083 | /* If the "connectable" instance flag was not set, then choose between | |
1084 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. | |
1085 | */ | |
1086 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | |
1087 | mgmt_get_connectable(hdev); | |
1088 | ||
1089 | if (!is_advertising_allowed(hdev, connectable)) | |
1090 | return -EPERM; | |
1091 | ||
1092 | /* Set require_privacy to true only when non-connectable | |
1093 | * advertising is used. In that case it is fine to use a | |
1094 | * non-resolvable private address. | |
1095 | */ | |
1096 | err = hci_get_random_address(hdev, !connectable, | |
1097 | adv_use_rpa(hdev, flags), adv, | |
1098 | &own_addr_type, &random_addr); | |
1099 | if (err < 0) | |
1100 | return err; | |
1101 | ||
1102 | memset(&cp, 0, sizeof(cp)); | |
1103 | ||
1104 | if (adv) { | |
1105 | hci_cpu_to_le24(adv->min_interval, cp.min_interval); | |
1106 | hci_cpu_to_le24(adv->max_interval, cp.max_interval); | |
1107 | cp.tx_power = adv->tx_power; | |
1108 | } else { | |
1109 | hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval); | |
1110 | hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval); | |
1111 | cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; | |
1112 | } | |
1113 | ||
1114 | secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK); | |
1115 | ||
1116 | if (connectable) { | |
1117 | if (secondary_adv) | |
1118 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND); | |
1119 | else | |
1120 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND); | |
1121 | } else if (hci_adv_instance_is_scannable(hdev, instance) || | |
1122 | (flags & MGMT_ADV_PARAM_SCAN_RSP)) { | |
1123 | if (secondary_adv) | |
1124 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND); | |
1125 | else | |
1126 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND); | |
1127 | } else { | |
1128 | if (secondary_adv) | |
1129 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND); | |
1130 | else | |
1131 | cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND); | |
1132 | } | |
1133 | ||
cf75ad8b LAD |
1134 | /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter |
1135 | * contains the peer’s Identity Address and the Peer_Address_Type | |
1136 | * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01). | |
1137 | * These parameters are used to locate the corresponding local IRK in | |
1138 | * the resolving list; this IRK is used to generate their own address | |
1139 | * used in the advertisement. | |
1140 | */ | |
1141 | if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) | |
1142 | hci_copy_identity_address(hdev, &cp.peer_addr, | |
1143 | &cp.peer_addr_type); | |
1144 | ||
cba6b758 LAD |
1145 | cp.own_addr_type = own_addr_type; |
1146 | cp.channel_map = hdev->le_adv_channel_map; | |
1147 | cp.handle = instance; | |
1148 | ||
1149 | if (flags & MGMT_ADV_FLAG_SEC_2M) { | |
1150 | cp.primary_phy = HCI_ADV_PHY_1M; | |
1151 | cp.secondary_phy = HCI_ADV_PHY_2M; | |
1152 | } else if (flags & MGMT_ADV_FLAG_SEC_CODED) { | |
1153 | cp.primary_phy = HCI_ADV_PHY_CODED; | |
1154 | cp.secondary_phy = HCI_ADV_PHY_CODED; | |
1155 | } else { | |
1156 | /* In all other cases use 1M */ | |
1157 | cp.primary_phy = HCI_ADV_PHY_1M; | |
1158 | cp.secondary_phy = HCI_ADV_PHY_1M; | |
1159 | } | |
1160 | ||
1161 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, | |
1162 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1163 | if (err) | |
1164 | return err; | |
1165 | ||
1166 | if ((own_addr_type == ADDR_LE_DEV_RANDOM || | |
1167 | own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && | |
1168 | bacmp(&random_addr, BDADDR_ANY)) { | |
1169 | /* Check if random address need to be updated */ | |
1170 | if (adv) { | |
1171 | if (!bacmp(&random_addr, &adv->random_addr)) | |
1172 | return 0; | |
1173 | } else { | |
1174 | if (!bacmp(&random_addr, &hdev->random_addr)) | |
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | return hci_set_adv_set_random_addr_sync(hdev, instance, | |
1179 | &random_addr); | |
1180 | } | |
1181 | ||
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
1186 | { | |
1187 | struct { | |
1188 | struct hci_cp_le_set_ext_scan_rsp_data cp; | |
1189 | u8 data[HCI_MAX_EXT_AD_LENGTH]; | |
1190 | } pdu; | |
1191 | u8 len; | |
34a718bc LAD |
1192 | struct adv_info *adv = NULL; |
1193 | int err; | |
cba6b758 LAD |
1194 | |
1195 | memset(&pdu, 0, sizeof(pdu)); | |
1196 | ||
34a718bc LAD |
1197 | if (instance) { |
1198 | adv = hci_find_adv_instance(hdev, instance); | |
1199 | if (!adv || !adv->scan_rsp_changed) | |
1200 | return 0; | |
1201 | } | |
cba6b758 | 1202 | |
34a718bc | 1203 | len = eir_create_scan_rsp(hdev, instance, pdu.data); |
cba6b758 LAD |
1204 | |
1205 | pdu.cp.handle = instance; | |
1206 | pdu.cp.length = len; | |
1207 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
1208 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; | |
1209 | ||
34a718bc LAD |
1210 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, |
1211 | sizeof(pdu.cp) + len, &pdu.cp, | |
1212 | HCI_CMD_TIMEOUT); | |
1213 | if (err) | |
1214 | return err; | |
1215 | ||
1216 | if (adv) { | |
1217 | adv->scan_rsp_changed = false; | |
1218 | } else { | |
1219 | memcpy(hdev->scan_rsp_data, pdu.data, len); | |
1220 | hdev->scan_rsp_data_len = len; | |
1221 | } | |
1222 | ||
1223 | return 0; | |
cba6b758 LAD |
1224 | } |
1225 | ||
1226 | static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
1227 | { | |
1228 | struct hci_cp_le_set_scan_rsp_data cp; | |
1229 | u8 len; | |
1230 | ||
1231 | memset(&cp, 0, sizeof(cp)); | |
1232 | ||
1233 | len = eir_create_scan_rsp(hdev, instance, cp.data); | |
1234 | ||
1235 | if (hdev->scan_rsp_data_len == len && | |
1236 | !memcmp(cp.data, hdev->scan_rsp_data, len)) | |
1237 | return 0; | |
1238 | ||
1239 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); | |
1240 | hdev->scan_rsp_data_len = len; | |
1241 | ||
1242 | cp.length = len; | |
1243 | ||
1244 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA, | |
1245 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1246 | } | |
1247 | ||
1248 | int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
1249 | { | |
1250 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
1251 | return 0; | |
1252 | ||
1253 | if (ext_adv_capable(hdev)) | |
1254 | return hci_set_ext_scan_rsp_data_sync(hdev, instance); | |
1255 | ||
1256 | return __hci_set_scan_rsp_data_sync(hdev, instance); | |
1257 | } | |
1258 | ||
1259 | int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance) | |
1260 | { | |
1261 | struct hci_cp_le_set_ext_adv_enable *cp; | |
1262 | struct hci_cp_ext_adv_set *set; | |
1263 | u8 data[sizeof(*cp) + sizeof(*set) * 1]; | |
1264 | struct adv_info *adv; | |
1265 | ||
1266 | if (instance > 0) { | |
1267 | adv = hci_find_adv_instance(hdev, instance); | |
1268 | if (!adv) | |
1269 | return -EINVAL; | |
1270 | /* If already enabled there is nothing to do */ | |
1271 | if (adv->enabled) | |
1272 | return 0; | |
1273 | } else { | |
1274 | adv = NULL; | |
1275 | } | |
1276 | ||
1277 | cp = (void *)data; | |
1278 | set = (void *)cp->data; | |
1279 | ||
1280 | memset(cp, 0, sizeof(*cp)); | |
1281 | ||
1282 | cp->enable = 0x01; | |
1283 | cp->num_of_sets = 0x01; | |
1284 | ||
1285 | memset(set, 0, sizeof(*set)); | |
1286 | ||
1287 | set->handle = instance; | |
1288 | ||
1289 | /* Set duration per instance since controller is responsible for | |
1290 | * scheduling it. | |
1291 | */ | |
f16a491c | 1292 | if (adv && adv->timeout) { |
cba6b758 LAD |
1293 | u16 duration = adv->timeout * MSEC_PER_SEC; |
1294 | ||
1295 | /* Time = N * 10 ms */ | |
1296 | set->duration = cpu_to_le16(duration / 10); | |
1297 | } | |
1298 | ||
1299 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, | |
1300 | sizeof(*cp) + | |
1301 | sizeof(*set) * cp->num_of_sets, | |
1302 | data, HCI_CMD_TIMEOUT); | |
1303 | } | |
1304 | ||
1305 | int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance) | |
1306 | { | |
1307 | int err; | |
1308 | ||
1309 | err = hci_setup_ext_adv_instance_sync(hdev, instance); | |
1310 | if (err) | |
1311 | return err; | |
1312 | ||
1313 | err = hci_set_ext_scan_rsp_data_sync(hdev, instance); | |
1314 | if (err) | |
1315 | return err; | |
1316 | ||
1317 | return hci_enable_ext_advertising_sync(hdev, instance); | |
1318 | } | |
1319 | ||
eca0ae4a LAD |
1320 | static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance) |
1321 | { | |
1322 | struct hci_cp_le_set_per_adv_enable cp; | |
6a42e9bf | 1323 | struct adv_info *adv = NULL; |
eca0ae4a LAD |
1324 | |
1325 | /* If periodic advertising already disabled there is nothing to do. */ | |
6a42e9bf IT |
1326 | adv = hci_find_adv_instance(hdev, instance); |
1327 | if (!adv || !adv->periodic || !adv->enabled) | |
eca0ae4a LAD |
1328 | return 0; |
1329 | ||
1330 | memset(&cp, 0, sizeof(cp)); | |
1331 | ||
1332 | cp.enable = 0x00; | |
1333 | cp.handle = instance; | |
1334 | ||
1335 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE, | |
1336 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1337 | } | |
1338 | ||
1339 | static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance, | |
1340 | u16 min_interval, u16 max_interval) | |
1341 | { | |
1342 | struct hci_cp_le_set_per_adv_params cp; | |
1343 | ||
1344 | memset(&cp, 0, sizeof(cp)); | |
1345 | ||
1346 | if (!min_interval) | |
1347 | min_interval = DISCOV_LE_PER_ADV_INT_MIN; | |
1348 | ||
1349 | if (!max_interval) | |
1350 | max_interval = DISCOV_LE_PER_ADV_INT_MAX; | |
1351 | ||
1352 | cp.handle = instance; | |
1353 | cp.min_interval = cpu_to_le16(min_interval); | |
1354 | cp.max_interval = cpu_to_le16(max_interval); | |
1355 | cp.periodic_properties = 0x0000; | |
1356 | ||
1357 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS, | |
1358 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1359 | } | |
1360 | ||
1361 | static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1362 | { | |
1363 | struct { | |
1364 | struct hci_cp_le_set_per_adv_data cp; | |
1365 | u8 data[HCI_MAX_PER_AD_LENGTH]; | |
1366 | } pdu; | |
1367 | u8 len; | |
1368 | ||
1369 | memset(&pdu, 0, sizeof(pdu)); | |
1370 | ||
1371 | if (instance) { | |
1372 | struct adv_info *adv = hci_find_adv_instance(hdev, instance); | |
1373 | ||
1374 | if (!adv || !adv->periodic) | |
1375 | return 0; | |
1376 | } | |
1377 | ||
1378 | len = eir_create_per_adv_data(hdev, instance, pdu.data); | |
1379 | ||
1380 | pdu.cp.length = len; | |
1381 | pdu.cp.handle = instance; | |
1382 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
1383 | ||
1384 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA, | |
1385 | sizeof(pdu.cp) + len, &pdu, | |
1386 | HCI_CMD_TIMEOUT); | |
1387 | } | |
1388 | ||
1389 | static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance) | |
1390 | { | |
1391 | struct hci_cp_le_set_per_adv_enable cp; | |
6a42e9bf | 1392 | struct adv_info *adv = NULL; |
eca0ae4a LAD |
1393 | |
1394 | /* If periodic advertising already enabled there is nothing to do. */ | |
6a42e9bf IT |
1395 | adv = hci_find_adv_instance(hdev, instance); |
1396 | if (adv && adv->periodic && adv->enabled) | |
eca0ae4a LAD |
1397 | return 0; |
1398 | ||
1399 | memset(&cp, 0, sizeof(cp)); | |
1400 | ||
1401 | cp.enable = 0x01; | |
1402 | cp.handle = instance; | |
1403 | ||
1404 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE, | |
1405 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1406 | } | |
1407 | ||
1408 | /* Checks if periodic advertising data contains a Basic Announcement and if it | |
1409 | * does generates a Broadcast ID and add Broadcast Announcement. | |
1410 | */ | |
1411 | static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv) | |
1412 | { | |
1413 | u8 bid[3]; | |
1414 | u8 ad[4 + 3]; | |
1415 | ||
1416 | /* Skip if NULL adv as instance 0x00 is used for general purpose | |
1417 | * advertising so it cannot used for the likes of Broadcast Announcement | |
1418 | * as it can be overwritten at any point. | |
1419 | */ | |
1420 | if (!adv) | |
1421 | return 0; | |
1422 | ||
1423 | /* Check if PA data doesn't contains a Basic Audio Announcement then | |
1424 | * there is nothing to do. | |
1425 | */ | |
1426 | if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len, | |
1427 | 0x1851, NULL)) | |
1428 | return 0; | |
1429 | ||
1430 | /* Check if advertising data already has a Broadcast Announcement since | |
1431 | * the process may want to control the Broadcast ID directly and in that | |
1432 | * case the kernel shall no interfere. | |
1433 | */ | |
1434 | if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852, | |
1435 | NULL)) | |
1436 | return 0; | |
1437 | ||
1438 | /* Generate Broadcast ID */ | |
1439 | get_random_bytes(bid, sizeof(bid)); | |
1440 | eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid)); | |
1441 | hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL); | |
1442 | ||
1443 | return hci_update_adv_data_sync(hdev, adv->instance); | |
1444 | } | |
1445 | ||
1446 | int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len, | |
1447 | u8 *data, u32 flags, u16 min_interval, | |
1448 | u16 max_interval, u16 sync_interval) | |
1449 | { | |
1450 | struct adv_info *adv = NULL; | |
1451 | int err; | |
1452 | bool added = false; | |
1453 | ||
1454 | hci_disable_per_advertising_sync(hdev, instance); | |
1455 | ||
1456 | if (instance) { | |
1457 | adv = hci_find_adv_instance(hdev, instance); | |
1458 | /* Create an instance if that could not be found */ | |
1459 | if (!adv) { | |
1460 | adv = hci_add_per_instance(hdev, instance, flags, | |
1461 | data_len, data, | |
1462 | sync_interval, | |
1463 | sync_interval); | |
1464 | if (IS_ERR(adv)) | |
1465 | return PTR_ERR(adv); | |
6a42e9bf | 1466 | adv->pending = false; |
eca0ae4a LAD |
1467 | added = true; |
1468 | } | |
1469 | } | |
1470 | ||
6a42e9bf IT |
1471 | /* Start advertising */ |
1472 | err = hci_start_ext_adv_sync(hdev, instance); | |
1473 | if (err < 0) | |
1474 | goto fail; | |
1475 | ||
1476 | err = hci_adv_bcast_annoucement(hdev, adv); | |
1477 | if (err < 0) | |
1478 | goto fail; | |
eca0ae4a LAD |
1479 | |
1480 | err = hci_set_per_adv_params_sync(hdev, instance, min_interval, | |
1481 | max_interval); | |
1482 | if (err < 0) | |
1483 | goto fail; | |
1484 | ||
1485 | err = hci_set_per_adv_data_sync(hdev, instance); | |
1486 | if (err < 0) | |
1487 | goto fail; | |
1488 | ||
1489 | err = hci_enable_per_advertising_sync(hdev, instance); | |
1490 | if (err < 0) | |
1491 | goto fail; | |
1492 | ||
1493 | return 0; | |
1494 | ||
1495 | fail: | |
1496 | if (added) | |
1497 | hci_remove_adv_instance(hdev, instance); | |
1498 | ||
1499 | return err; | |
1500 | } | |
1501 | ||
cba6b758 LAD |
1502 | static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance) |
1503 | { | |
1504 | int err; | |
1505 | ||
1506 | if (ext_adv_capable(hdev)) | |
1507 | return hci_start_ext_adv_sync(hdev, instance); | |
1508 | ||
1509 | err = hci_update_adv_data_sync(hdev, instance); | |
1510 | if (err) | |
1511 | return err; | |
1512 | ||
1513 | err = hci_update_scan_rsp_data_sync(hdev, instance); | |
1514 | if (err) | |
1515 | return err; | |
1516 | ||
1517 | return hci_enable_advertising_sync(hdev); | |
1518 | } | |
1519 | ||
1520 | int hci_enable_advertising_sync(struct hci_dev *hdev) | |
1521 | { | |
1522 | struct adv_info *adv_instance; | |
1523 | struct hci_cp_le_set_adv_param cp; | |
1524 | u8 own_addr_type, enable = 0x01; | |
1525 | bool connectable; | |
1526 | u16 adv_min_interval, adv_max_interval; | |
1527 | u32 flags; | |
1528 | u8 status; | |
1529 | ||
ad383c2c LAD |
1530 | if (ext_adv_capable(hdev)) |
1531 | return hci_enable_ext_advertising_sync(hdev, | |
1532 | hdev->cur_adv_instance); | |
1533 | ||
cba6b758 LAD |
1534 | flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance); |
1535 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); | |
1536 | ||
1537 | /* If the "connectable" instance flag was not set, then choose between | |
1538 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. | |
1539 | */ | |
1540 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | |
1541 | mgmt_get_connectable(hdev); | |
1542 | ||
1543 | if (!is_advertising_allowed(hdev, connectable)) | |
1544 | return -EINVAL; | |
1545 | ||
ad383c2c LAD |
1546 | status = hci_disable_advertising_sync(hdev); |
1547 | if (status) | |
1548 | return status; | |
cba6b758 LAD |
1549 | |
1550 | /* Clear the HCI_LE_ADV bit temporarily so that the | |
1551 | * hci_update_random_address knows that it's safe to go ahead | |
1552 | * and write a new random address. The flag will be set back on | |
1553 | * as soon as the SET_ADV_ENABLE HCI command completes. | |
1554 | */ | |
1555 | hci_dev_clear_flag(hdev, HCI_LE_ADV); | |
1556 | ||
1557 | /* Set require_privacy to true only when non-connectable | |
1558 | * advertising is used. In that case it is fine to use a | |
1559 | * non-resolvable private address. | |
1560 | */ | |
1561 | status = hci_update_random_address_sync(hdev, !connectable, | |
1562 | adv_use_rpa(hdev, flags), | |
1563 | &own_addr_type); | |
1564 | if (status) | |
1565 | return status; | |
1566 | ||
1567 | memset(&cp, 0, sizeof(cp)); | |
1568 | ||
1569 | if (adv_instance) { | |
1570 | adv_min_interval = adv_instance->min_interval; | |
1571 | adv_max_interval = adv_instance->max_interval; | |
1572 | } else { | |
1573 | adv_min_interval = hdev->le_adv_min_interval; | |
1574 | adv_max_interval = hdev->le_adv_max_interval; | |
1575 | } | |
1576 | ||
1577 | if (connectable) { | |
1578 | cp.type = LE_ADV_IND; | |
1579 | } else { | |
1580 | if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance)) | |
1581 | cp.type = LE_ADV_SCAN_IND; | |
1582 | else | |
1583 | cp.type = LE_ADV_NONCONN_IND; | |
1584 | ||
1585 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) || | |
1586 | hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { | |
1587 | adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN; | |
1588 | adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX; | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | cp.min_interval = cpu_to_le16(adv_min_interval); | |
1593 | cp.max_interval = cpu_to_le16(adv_max_interval); | |
1594 | cp.own_address_type = own_addr_type; | |
1595 | cp.channel_map = hdev->le_adv_channel_map; | |
1596 | ||
1597 | status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, | |
1598 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1599 | if (status) | |
1600 | return status; | |
1601 | ||
1602 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
1603 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
1604 | } | |
1605 | ||
abfeea47 LAD |
1606 | static int enable_advertising_sync(struct hci_dev *hdev, void *data) |
1607 | { | |
1608 | return hci_enable_advertising_sync(hdev); | |
1609 | } | |
1610 | ||
1611 | int hci_enable_advertising(struct hci_dev *hdev) | |
1612 | { | |
1613 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && | |
1614 | list_empty(&hdev->adv_instances)) | |
1615 | return 0; | |
1616 | ||
1617 | return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL); | |
1618 | } | |
1619 | ||
1620 | int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, | |
1621 | struct sock *sk) | |
cba6b758 LAD |
1622 | { |
1623 | int err; | |
1624 | ||
1625 | if (!ext_adv_capable(hdev)) | |
1626 | return 0; | |
1627 | ||
1628 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | |
1629 | if (err) | |
1630 | return err; | |
1631 | ||
1632 | /* If request specifies an instance that doesn't exist, fail */ | |
1633 | if (instance > 0 && !hci_find_adv_instance(hdev, instance)) | |
1634 | return -EINVAL; | |
1635 | ||
1636 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, | |
1637 | sizeof(instance), &instance, 0, | |
1638 | HCI_CMD_TIMEOUT, sk); | |
1639 | } | |
1640 | ||
eca0ae4a LAD |
1641 | static int remove_ext_adv_sync(struct hci_dev *hdev, void *data) |
1642 | { | |
1643 | struct adv_info *adv = data; | |
1644 | u8 instance = 0; | |
1645 | ||
1646 | if (adv) | |
1647 | instance = adv->instance; | |
1648 | ||
1649 | return hci_remove_ext_adv_instance_sync(hdev, instance, NULL); | |
1650 | } | |
1651 | ||
1652 | int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance) | |
1653 | { | |
1654 | struct adv_info *adv = NULL; | |
1655 | ||
1656 | if (instance) { | |
1657 | adv = hci_find_adv_instance(hdev, instance); | |
1658 | if (!adv) | |
1659 | return -EINVAL; | |
1660 | } | |
1661 | ||
1662 | return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL); | |
1663 | } | |
1664 | ||
1665 | int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason) | |
1666 | { | |
1667 | struct hci_cp_le_term_big cp; | |
1668 | ||
1669 | memset(&cp, 0, sizeof(cp)); | |
1670 | cp.handle = handle; | |
1671 | cp.reason = reason; | |
1672 | ||
1673 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG, | |
1674 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1675 | } | |
1676 | ||
cba6b758 LAD |
1677 | static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) |
1678 | { | |
1679 | struct { | |
1680 | struct hci_cp_le_set_ext_adv_data cp; | |
1681 | u8 data[HCI_MAX_EXT_AD_LENGTH]; | |
1682 | } pdu; | |
1683 | u8 len; | |
34a718bc LAD |
1684 | struct adv_info *adv = NULL; |
1685 | int err; | |
cba6b758 LAD |
1686 | |
1687 | memset(&pdu, 0, sizeof(pdu)); | |
1688 | ||
34a718bc LAD |
1689 | if (instance) { |
1690 | adv = hci_find_adv_instance(hdev, instance); | |
1691 | if (!adv || !adv->adv_data_changed) | |
1692 | return 0; | |
1693 | } | |
cba6b758 | 1694 | |
34a718bc | 1695 | len = eir_create_adv_data(hdev, instance, pdu.data); |
cba6b758 LAD |
1696 | |
1697 | pdu.cp.length = len; | |
1698 | pdu.cp.handle = instance; | |
1699 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
1700 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; | |
1701 | ||
34a718bc LAD |
1702 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, |
1703 | sizeof(pdu.cp) + len, &pdu.cp, | |
1704 | HCI_CMD_TIMEOUT); | |
1705 | if (err) | |
1706 | return err; | |
1707 | ||
1708 | /* Update data if the command succeed */ | |
1709 | if (adv) { | |
1710 | adv->adv_data_changed = false; | |
1711 | } else { | |
1712 | memcpy(hdev->adv_data, pdu.data, len); | |
1713 | hdev->adv_data_len = len; | |
1714 | } | |
1715 | ||
1716 | return 0; | |
cba6b758 LAD |
1717 | } |
1718 | ||
1719 | static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1720 | { | |
1721 | struct hci_cp_le_set_adv_data cp; | |
1722 | u8 len; | |
1723 | ||
1724 | memset(&cp, 0, sizeof(cp)); | |
1725 | ||
1726 | len = eir_create_adv_data(hdev, instance, cp.data); | |
1727 | ||
1728 | /* There's nothing to do if the data hasn't changed */ | |
1729 | if (hdev->adv_data_len == len && | |
1730 | memcmp(cp.data, hdev->adv_data, len) == 0) | |
1731 | return 0; | |
1732 | ||
1733 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); | |
1734 | hdev->adv_data_len = len; | |
1735 | ||
1736 | cp.length = len; | |
1737 | ||
1738 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA, | |
1739 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1740 | } | |
1741 | ||
1742 | int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1743 | { | |
1744 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
1745 | return 0; | |
1746 | ||
1747 | if (ext_adv_capable(hdev)) | |
1748 | return hci_set_ext_adv_data_sync(hdev, instance); | |
1749 | ||
1750 | return hci_set_adv_data_sync(hdev, instance); | |
1751 | } | |
1752 | ||
1753 | int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance, | |
1754 | bool force) | |
1755 | { | |
1756 | struct adv_info *adv = NULL; | |
1757 | u16 timeout; | |
1758 | ||
cf75ad8b | 1759 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev)) |
cba6b758 LAD |
1760 | return -EPERM; |
1761 | ||
1762 | if (hdev->adv_instance_timeout) | |
1763 | return -EBUSY; | |
1764 | ||
1765 | adv = hci_find_adv_instance(hdev, instance); | |
1766 | if (!adv) | |
1767 | return -ENOENT; | |
1768 | ||
1769 | /* A zero timeout means unlimited advertising. As long as there is | |
1770 | * only one instance, duration should be ignored. We still set a timeout | |
1771 | * in case further instances are being added later on. | |
1772 | * | |
1773 | * If the remaining lifetime of the instance is more than the duration | |
1774 | * then the timeout corresponds to the duration, otherwise it will be | |
1775 | * reduced to the remaining instance lifetime. | |
1776 | */ | |
1777 | if (adv->timeout == 0 || adv->duration <= adv->remaining_time) | |
1778 | timeout = adv->duration; | |
1779 | else | |
1780 | timeout = adv->remaining_time; | |
1781 | ||
1782 | /* The remaining time is being reduced unless the instance is being | |
1783 | * advertised without time limit. | |
1784 | */ | |
1785 | if (adv->timeout) | |
1786 | adv->remaining_time = adv->remaining_time - timeout; | |
1787 | ||
1788 | /* Only use work for scheduling instances with legacy advertising */ | |
1789 | if (!ext_adv_capable(hdev)) { | |
1790 | hdev->adv_instance_timeout = timeout; | |
1791 | queue_delayed_work(hdev->req_workqueue, | |
1792 | &hdev->adv_instance_expire, | |
1793 | msecs_to_jiffies(timeout * 1000)); | |
1794 | } | |
1795 | ||
1796 | /* If we're just re-scheduling the same instance again then do not | |
1797 | * execute any HCI commands. This happens when a single instance is | |
1798 | * being advertised. | |
1799 | */ | |
1800 | if (!force && hdev->cur_adv_instance == instance && | |
1801 | hci_dev_test_flag(hdev, HCI_LE_ADV)) | |
1802 | return 0; | |
1803 | ||
1804 | hdev->cur_adv_instance = instance; | |
1805 | ||
1806 | return hci_start_adv_sync(hdev, instance); | |
1807 | } | |
1808 | ||
1809 | static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk) | |
1810 | { | |
1811 | int err; | |
1812 | ||
1813 | if (!ext_adv_capable(hdev)) | |
1814 | return 0; | |
1815 | ||
1816 | /* Disable instance 0x00 to disable all instances */ | |
1817 | err = hci_disable_ext_adv_instance_sync(hdev, 0x00); | |
1818 | if (err) | |
1819 | return err; | |
1820 | ||
1821 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS, | |
1822 | 0, NULL, 0, HCI_CMD_TIMEOUT, sk); | |
1823 | } | |
1824 | ||
1825 | static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force) | |
1826 | { | |
1827 | struct adv_info *adv, *n; | |
b338d917 | 1828 | int err = 0; |
cba6b758 LAD |
1829 | |
1830 | if (ext_adv_capable(hdev)) | |
1831 | /* Remove all existing sets */ | |
b338d917 BG |
1832 | err = hci_clear_adv_sets_sync(hdev, sk); |
1833 | if (ext_adv_capable(hdev)) | |
1834 | return err; | |
cba6b758 LAD |
1835 | |
1836 | /* This is safe as long as there is no command send while the lock is | |
1837 | * held. | |
1838 | */ | |
1839 | hci_dev_lock(hdev); | |
1840 | ||
1841 | /* Cleanup non-ext instances */ | |
1842 | list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { | |
1843 | u8 instance = adv->instance; | |
1844 | int err; | |
1845 | ||
1846 | if (!(force || adv->timeout)) | |
1847 | continue; | |
1848 | ||
1849 | err = hci_remove_adv_instance(hdev, instance); | |
1850 | if (!err) | |
1851 | mgmt_advertising_removed(sk, hdev, instance); | |
1852 | } | |
1853 | ||
1854 | hci_dev_unlock(hdev); | |
1855 | ||
1856 | return 0; | |
1857 | } | |
1858 | ||
1859 | static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance, | |
1860 | struct sock *sk) | |
1861 | { | |
b338d917 | 1862 | int err = 0; |
cba6b758 LAD |
1863 | |
1864 | /* If we use extended advertising, instance has to be removed first. */ | |
1865 | if (ext_adv_capable(hdev)) | |
b338d917 BG |
1866 | err = hci_remove_ext_adv_instance_sync(hdev, instance, sk); |
1867 | if (ext_adv_capable(hdev)) | |
1868 | return err; | |
cba6b758 LAD |
1869 | |
1870 | /* This is safe as long as there is no command send while the lock is | |
1871 | * held. | |
1872 | */ | |
1873 | hci_dev_lock(hdev); | |
1874 | ||
1875 | err = hci_remove_adv_instance(hdev, instance); | |
1876 | if (!err) | |
1877 | mgmt_advertising_removed(sk, hdev, instance); | |
1878 | ||
1879 | hci_dev_unlock(hdev); | |
1880 | ||
1881 | return err; | |
1882 | } | |
1883 | ||
1884 | /* For a single instance: | |
1885 | * - force == true: The instance will be removed even when its remaining | |
1886 | * lifetime is not zero. | |
1887 | * - force == false: the instance will be deactivated but kept stored unless | |
1888 | * the remaining lifetime is zero. | |
1889 | * | |
1890 | * For instance == 0x00: | |
1891 | * - force == true: All instances will be removed regardless of their timeout | |
1892 | * setting. | |
1893 | * - force == false: Only instances that have a timeout will be removed. | |
1894 | */ | |
1895 | int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk, | |
1896 | u8 instance, bool force) | |
1897 | { | |
1898 | struct adv_info *next = NULL; | |
1899 | int err; | |
1900 | ||
1901 | /* Cancel any timeout concerning the removed instance(s). */ | |
1902 | if (!instance || hdev->cur_adv_instance == instance) | |
1903 | cancel_adv_timeout(hdev); | |
1904 | ||
1905 | /* Get the next instance to advertise BEFORE we remove | |
1906 | * the current one. This can be the same instance again | |
1907 | * if there is only one instance. | |
1908 | */ | |
1909 | if (hdev->cur_adv_instance == instance) | |
1910 | next = hci_get_next_instance(hdev, instance); | |
1911 | ||
1912 | if (!instance) { | |
1913 | err = hci_clear_adv_sync(hdev, sk, force); | |
1914 | if (err) | |
1915 | return err; | |
1916 | } else { | |
1917 | struct adv_info *adv = hci_find_adv_instance(hdev, instance); | |
1918 | ||
1919 | if (force || (adv && adv->timeout && !adv->remaining_time)) { | |
1920 | /* Don't advertise a removed instance. */ | |
1921 | if (next && next->instance == instance) | |
1922 | next = NULL; | |
1923 | ||
1924 | err = hci_remove_adv_sync(hdev, instance, sk); | |
1925 | if (err) | |
1926 | return err; | |
1927 | } | |
1928 | } | |
1929 | ||
1930 | if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
1931 | return 0; | |
1932 | ||
1933 | if (next && !ext_adv_capable(hdev)) | |
1934 | hci_schedule_adv_instance_sync(hdev, next->instance, false); | |
1935 | ||
1936 | return 0; | |
1937 | } | |
1938 | ||
47db6b42 BG |
1939 | int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle) |
1940 | { | |
1941 | struct hci_cp_read_rssi cp; | |
1942 | ||
1943 | cp.handle = handle; | |
1944 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI, | |
1945 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1946 | } | |
1947 | ||
5a750137 BG |
1948 | int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp) |
1949 | { | |
1950 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK, | |
1951 | sizeof(*cp), cp, HCI_CMD_TIMEOUT); | |
1952 | } | |
1953 | ||
47db6b42 BG |
1954 | int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type) |
1955 | { | |
1956 | struct hci_cp_read_tx_power cp; | |
1957 | ||
1958 | cp.handle = handle; | |
1959 | cp.type = type; | |
1960 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER, | |
1961 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1962 | } | |
1963 | ||
cba6b758 LAD |
1964 | int hci_disable_advertising_sync(struct hci_dev *hdev) |
1965 | { | |
1966 | u8 enable = 0x00; | |
b338d917 | 1967 | int err = 0; |
cba6b758 | 1968 | |
ad383c2c LAD |
1969 | /* If controller is not advertising we are done. */ |
1970 | if (!hci_dev_test_flag(hdev, HCI_LE_ADV)) | |
1971 | return 0; | |
1972 | ||
cba6b758 | 1973 | if (ext_adv_capable(hdev)) |
b338d917 BG |
1974 | err = hci_disable_ext_adv_instance_sync(hdev, 0x00); |
1975 | if (ext_adv_capable(hdev)) | |
1976 | return err; | |
cba6b758 LAD |
1977 | |
1978 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
1979 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
1980 | } | |
e8907f76 LAD |
1981 | |
1982 | static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val, | |
1983 | u8 filter_dup) | |
1984 | { | |
1985 | struct hci_cp_le_set_ext_scan_enable cp; | |
1986 | ||
1987 | memset(&cp, 0, sizeof(cp)); | |
1988 | cp.enable = val; | |
b338d917 BG |
1989 | |
1990 | if (hci_dev_test_flag(hdev, HCI_MESH)) | |
1991 | cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE; | |
1992 | else | |
1993 | cp.filter_dup = filter_dup; | |
e8907f76 LAD |
1994 | |
1995 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE, | |
1996 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1997 | } | |
1998 | ||
1999 | static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val, | |
2000 | u8 filter_dup) | |
2001 | { | |
2002 | struct hci_cp_le_set_scan_enable cp; | |
2003 | ||
2004 | if (use_ext_scan(hdev)) | |
2005 | return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup); | |
2006 | ||
2007 | memset(&cp, 0, sizeof(cp)); | |
2008 | cp.enable = val; | |
b338d917 BG |
2009 | |
2010 | if (val && hci_dev_test_flag(hdev, HCI_MESH)) | |
2011 | cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE; | |
2012 | else | |
2013 | cp.filter_dup = filter_dup; | |
e8907f76 LAD |
2014 | |
2015 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE, | |
2016 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2017 | } | |
2018 | ||
2019 | static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val) | |
2020 | { | |
ad383c2c LAD |
2021 | if (!use_ll_privacy(hdev)) |
2022 | return 0; | |
2023 | ||
2024 | /* If controller is not/already resolving we are done. */ | |
2025 | if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) | |
e8907f76 LAD |
2026 | return 0; |
2027 | ||
2028 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, | |
2029 | sizeof(val), &val, HCI_CMD_TIMEOUT); | |
2030 | } | |
2031 | ||
27592ca1 | 2032 | static int hci_scan_disable_sync(struct hci_dev *hdev) |
e8907f76 LAD |
2033 | { |
2034 | int err; | |
2035 | ||
2036 | /* If controller is not scanning we are done. */ | |
2037 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) | |
2038 | return 0; | |
2039 | ||
2040 | if (hdev->scanning_paused) { | |
2041 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
2042 | return 0; | |
2043 | } | |
2044 | ||
e8907f76 LAD |
2045 | err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00); |
2046 | if (err) { | |
2047 | bt_dev_err(hdev, "Unable to disable scanning: %d", err); | |
2048 | return err; | |
2049 | } | |
2050 | ||
e8907f76 LAD |
2051 | return err; |
2052 | } | |
2053 | ||
2054 | static bool scan_use_rpa(struct hci_dev *hdev) | |
2055 | { | |
2056 | return hci_dev_test_flag(hdev, HCI_PRIVACY); | |
2057 | } | |
2058 | ||
2059 | static void hci_start_interleave_scan(struct hci_dev *hdev) | |
2060 | { | |
2061 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; | |
2062 | queue_delayed_work(hdev->req_workqueue, | |
2063 | &hdev->interleave_scan, 0); | |
2064 | } | |
2065 | ||
2066 | static bool is_interleave_scanning(struct hci_dev *hdev) | |
2067 | { | |
2068 | return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE; | |
2069 | } | |
2070 | ||
2071 | static void cancel_interleave_scan(struct hci_dev *hdev) | |
2072 | { | |
2073 | bt_dev_dbg(hdev, "cancelling interleave scan"); | |
2074 | ||
2075 | cancel_delayed_work_sync(&hdev->interleave_scan); | |
2076 | ||
2077 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE; | |
2078 | } | |
2079 | ||
2080 | /* Return true if interleave_scan wasn't started until exiting this function, | |
2081 | * otherwise, return false | |
2082 | */ | |
2083 | static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev) | |
2084 | { | |
2085 | /* Do interleaved scan only if all of the following are true: | |
2086 | * - There is at least one ADV monitor | |
2087 | * - At least one pending LE connection or one device to be scanned for | |
2088 | * - Monitor offloading is not supported | |
2089 | * If so, we should alternate between allowlist scan and one without | |
2090 | * any filters to save power. | |
2091 | */ | |
2092 | bool use_interleaving = hci_is_adv_monitoring(hdev) && | |
2093 | !(list_empty(&hdev->pend_le_conns) && | |
2094 | list_empty(&hdev->pend_le_reports)) && | |
2095 | hci_get_adv_monitor_offload_ext(hdev) == | |
2096 | HCI_ADV_MONITOR_EXT_NONE; | |
2097 | bool is_interleaving = is_interleave_scanning(hdev); | |
2098 | ||
2099 | if (use_interleaving && !is_interleaving) { | |
2100 | hci_start_interleave_scan(hdev); | |
2101 | bt_dev_dbg(hdev, "starting interleave scan"); | |
2102 | return true; | |
2103 | } | |
2104 | ||
2105 | if (!use_interleaving && is_interleaving) | |
2106 | cancel_interleave_scan(hdev); | |
2107 | ||
2108 | return false; | |
2109 | } | |
2110 | ||
2111 | /* Removes connection to resolve list if needed.*/ | |
2112 | static int hci_le_del_resolve_list_sync(struct hci_dev *hdev, | |
2113 | bdaddr_t *bdaddr, u8 bdaddr_type) | |
2114 | { | |
2115 | struct hci_cp_le_del_from_resolv_list cp; | |
2116 | struct bdaddr_list_with_irk *entry; | |
2117 | ||
ad383c2c | 2118 | if (!use_ll_privacy(hdev)) |
e8907f76 LAD |
2119 | return 0; |
2120 | ||
2121 | /* Check if the IRK has been programmed */ | |
2122 | entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr, | |
2123 | bdaddr_type); | |
2124 | if (!entry) | |
2125 | return 0; | |
2126 | ||
2127 | cp.bdaddr_type = bdaddr_type; | |
2128 | bacpy(&cp.bdaddr, bdaddr); | |
2129 | ||
2130 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST, | |
2131 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2132 | } | |
2133 | ||
2134 | static int hci_le_del_accept_list_sync(struct hci_dev *hdev, | |
2135 | bdaddr_t *bdaddr, u8 bdaddr_type) | |
2136 | { | |
2137 | struct hci_cp_le_del_from_accept_list cp; | |
2138 | int err; | |
2139 | ||
2140 | /* Check if device is on accept list before removing it */ | |
2141 | if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type)) | |
2142 | return 0; | |
2143 | ||
2144 | cp.bdaddr_type = bdaddr_type; | |
2145 | bacpy(&cp.bdaddr, bdaddr); | |
2146 | ||
ad383c2c LAD |
2147 | /* Ignore errors when removing from resolving list as that is likely |
2148 | * that the device was never added. | |
2149 | */ | |
2150 | hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); | |
2151 | ||
e8907f76 LAD |
2152 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, |
2153 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2154 | if (err) { | |
2155 | bt_dev_err(hdev, "Unable to remove from allow list: %d", err); | |
2156 | return err; | |
2157 | } | |
2158 | ||
2159 | bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr, | |
2160 | cp.bdaddr_type); | |
2161 | ||
ad383c2c | 2162 | return 0; |
e8907f76 LAD |
2163 | } |
2164 | ||
195ef75e PV |
2165 | struct conn_params { |
2166 | bdaddr_t addr; | |
2167 | u8 addr_type; | |
2168 | hci_conn_flags_t flags; | |
2169 | u8 privacy_mode; | |
2170 | }; | |
2171 | ||
cf75ad8b LAD |
2172 | /* Adds connection to resolve list if needed. |
2173 | * Setting params to NULL programs local hdev->irk | |
2174 | */ | |
e8907f76 | 2175 | static int hci_le_add_resolve_list_sync(struct hci_dev *hdev, |
195ef75e | 2176 | struct conn_params *params) |
e8907f76 LAD |
2177 | { |
2178 | struct hci_cp_le_add_to_resolv_list cp; | |
2179 | struct smp_irk *irk; | |
2180 | struct bdaddr_list_with_irk *entry; | |
195ef75e | 2181 | struct hci_conn_params *p; |
e8907f76 | 2182 | |
ad383c2c | 2183 | if (!use_ll_privacy(hdev)) |
e8907f76 LAD |
2184 | return 0; |
2185 | ||
cf75ad8b LAD |
2186 | /* Attempt to program local identity address, type and irk if params is |
2187 | * NULL. | |
2188 | */ | |
2189 | if (!params) { | |
2190 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) | |
2191 | return 0; | |
2192 | ||
2193 | hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type); | |
2194 | memcpy(cp.peer_irk, hdev->irk, 16); | |
2195 | goto done; | |
2196 | } | |
2197 | ||
e8907f76 LAD |
2198 | irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); |
2199 | if (!irk) | |
2200 | return 0; | |
2201 | ||
2202 | /* Check if the IK has _not_ been programmed yet. */ | |
2203 | entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, | |
2204 | ¶ms->addr, | |
2205 | params->addr_type); | |
2206 | if (entry) | |
2207 | return 0; | |
2208 | ||
2209 | cp.bdaddr_type = params->addr_type; | |
2210 | bacpy(&cp.bdaddr, ¶ms->addr); | |
2211 | memcpy(cp.peer_irk, irk->val, 16); | |
2212 | ||
0900b1c6 LAD |
2213 | /* Default privacy mode is always Network */ |
2214 | params->privacy_mode = HCI_NETWORK_PRIVACY; | |
2215 | ||
195ef75e PV |
2216 | rcu_read_lock(); |
2217 | p = hci_pend_le_action_lookup(&hdev->pend_le_conns, | |
2218 | ¶ms->addr, params->addr_type); | |
2219 | if (!p) | |
2220 | p = hci_pend_le_action_lookup(&hdev->pend_le_reports, | |
2221 | ¶ms->addr, params->addr_type); | |
2222 | if (p) | |
2223 | WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY); | |
2224 | rcu_read_unlock(); | |
2225 | ||
cf75ad8b | 2226 | done: |
e8907f76 LAD |
2227 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
2228 | memcpy(cp.local_irk, hdev->irk, 16); | |
2229 | else | |
2230 | memset(cp.local_irk, 0, 16); | |
2231 | ||
2232 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST, | |
2233 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2234 | } | |
2235 | ||
853b70b5 LAD |
2236 | /* Set Device Privacy Mode. */ |
2237 | static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev, | |
195ef75e | 2238 | struct conn_params *params) |
853b70b5 LAD |
2239 | { |
2240 | struct hci_cp_le_set_privacy_mode cp; | |
2241 | struct smp_irk *irk; | |
2242 | ||
2243 | /* If device privacy mode has already been set there is nothing to do */ | |
2244 | if (params->privacy_mode == HCI_DEVICE_PRIVACY) | |
2245 | return 0; | |
2246 | ||
2247 | /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also | |
2248 | * indicates that LL Privacy has been enabled and | |
2249 | * HCI_OP_LE_SET_PRIVACY_MODE is supported. | |
2250 | */ | |
e1cff700 | 2251 | if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY)) |
853b70b5 LAD |
2252 | return 0; |
2253 | ||
2254 | irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); | |
2255 | if (!irk) | |
2256 | return 0; | |
2257 | ||
2258 | memset(&cp, 0, sizeof(cp)); | |
2259 | cp.bdaddr_type = irk->addr_type; | |
2260 | bacpy(&cp.bdaddr, &irk->bdaddr); | |
2261 | cp.mode = HCI_DEVICE_PRIVACY; | |
2262 | ||
195ef75e PV |
2263 | /* Note: params->privacy_mode is not updated since it is a copy */ |
2264 | ||
853b70b5 LAD |
2265 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE, |
2266 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2267 | } | |
2268 | ||
e8907f76 | 2269 | /* Adds connection to allow list if needed, if the device uses RPA (has IRK) |
853b70b5 LAD |
2270 | * this attempts to program the device in the resolving list as well and |
2271 | * properly set the privacy mode. | |
e8907f76 LAD |
2272 | */ |
2273 | static int hci_le_add_accept_list_sync(struct hci_dev *hdev, | |
195ef75e | 2274 | struct conn_params *params, |
ad383c2c | 2275 | u8 *num_entries) |
e8907f76 LAD |
2276 | { |
2277 | struct hci_cp_le_add_to_accept_list cp; | |
2278 | int err; | |
2279 | ||
3b420553 LAD |
2280 | /* During suspend, only wakeable devices can be in acceptlist */ |
2281 | if (hdev->suspended && | |
e1cff700 | 2282 | !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) |
3b420553 LAD |
2283 | return 0; |
2284 | ||
e8907f76 LAD |
2285 | /* Select filter policy to accept all advertising */ |
2286 | if (*num_entries >= hdev->le_accept_list_size) | |
2287 | return -ENOSPC; | |
2288 | ||
2289 | /* Accept list can not be used with RPAs */ | |
ad383c2c | 2290 | if (!use_ll_privacy(hdev) && |
3b420553 | 2291 | hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) |
e8907f76 | 2292 | return -EINVAL; |
e8907f76 | 2293 | |
ad383c2c LAD |
2294 | /* Attempt to program the device in the resolving list first to avoid |
2295 | * having to rollback in case it fails since the resolving list is | |
2296 | * dynamic it can probably be smaller than the accept list. | |
2297 | */ | |
2298 | err = hci_le_add_resolve_list_sync(hdev, params); | |
2299 | if (err) { | |
2300 | bt_dev_err(hdev, "Unable to add to resolve list: %d", err); | |
2301 | return err; | |
2302 | } | |
2303 | ||
853b70b5 LAD |
2304 | /* Set Privacy Mode */ |
2305 | err = hci_le_set_privacy_mode_sync(hdev, params); | |
2306 | if (err) { | |
2307 | bt_dev_err(hdev, "Unable to set privacy mode: %d", err); | |
2308 | return err; | |
2309 | } | |
2310 | ||
2311 | /* Check if already in accept list */ | |
2312 | if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr, | |
2313 | params->addr_type)) | |
2314 | return 0; | |
2315 | ||
e8907f76 LAD |
2316 | *num_entries += 1; |
2317 | cp.bdaddr_type = params->addr_type; | |
2318 | bacpy(&cp.bdaddr, ¶ms->addr); | |
2319 | ||
2320 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST, | |
2321 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2322 | if (err) { | |
2323 | bt_dev_err(hdev, "Unable to add to allow list: %d", err); | |
ad383c2c LAD |
2324 | /* Rollback the device from the resolving list */ |
2325 | hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); | |
e8907f76 LAD |
2326 | return err; |
2327 | } | |
2328 | ||
2329 | bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr, | |
2330 | cp.bdaddr_type); | |
2331 | ||
ad383c2c LAD |
2332 | return 0; |
2333 | } | |
2334 | ||
182ee45d | 2335 | /* This function disables/pause all advertising instances */ |
ad383c2c LAD |
2336 | static int hci_pause_advertising_sync(struct hci_dev *hdev) |
2337 | { | |
2338 | int err; | |
182ee45d | 2339 | int old_state; |
ad383c2c | 2340 | |
9446bdde LAD |
2341 | /* If already been paused there is nothing to do. */ |
2342 | if (hdev->advertising_paused) | |
ad383c2c LAD |
2343 | return 0; |
2344 | ||
182ee45d LAD |
2345 | bt_dev_dbg(hdev, "Pausing directed advertising"); |
2346 | ||
2347 | /* Stop directed advertising */ | |
2348 | old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING); | |
2349 | if (old_state) { | |
2350 | /* When discoverable timeout triggers, then just make sure | |
2351 | * the limited discoverable flag is cleared. Even in the case | |
2352 | * of a timeout triggered from general discoverable, it is | |
2353 | * safe to unconditionally clear the flag. | |
2354 | */ | |
2355 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); | |
2356 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
2357 | hdev->discov_timeout = 0; | |
2358 | } | |
2359 | ||
ad383c2c LAD |
2360 | bt_dev_dbg(hdev, "Pausing advertising instances"); |
2361 | ||
2362 | /* Call to disable any advertisements active on the controller. | |
2363 | * This will succeed even if no advertisements are configured. | |
2364 | */ | |
2365 | err = hci_disable_advertising_sync(hdev); | |
2366 | if (err) | |
2367 | return err; | |
2368 | ||
2369 | /* If we are using software rotation, pause the loop */ | |
2370 | if (!ext_adv_capable(hdev)) | |
2371 | cancel_adv_timeout(hdev); | |
2372 | ||
2373 | hdev->advertising_paused = true; | |
182ee45d | 2374 | hdev->advertising_old_state = old_state; |
ad383c2c LAD |
2375 | |
2376 | return 0; | |
e8907f76 LAD |
2377 | } |
2378 | ||
182ee45d | 2379 | /* This function enables all user advertising instances */ |
ad383c2c LAD |
2380 | static int hci_resume_advertising_sync(struct hci_dev *hdev) |
2381 | { | |
2382 | struct adv_info *adv, *tmp; | |
2383 | int err; | |
2384 | ||
2385 | /* If advertising has not been paused there is nothing to do. */ | |
2386 | if (!hdev->advertising_paused) | |
2387 | return 0; | |
2388 | ||
182ee45d LAD |
2389 | /* Resume directed advertising */ |
2390 | hdev->advertising_paused = false; | |
2391 | if (hdev->advertising_old_state) { | |
2392 | hci_dev_set_flag(hdev, HCI_ADVERTISING); | |
182ee45d LAD |
2393 | hdev->advertising_old_state = 0; |
2394 | } | |
2395 | ||
ad383c2c LAD |
2396 | bt_dev_dbg(hdev, "Resuming advertising instances"); |
2397 | ||
2398 | if (ext_adv_capable(hdev)) { | |
2399 | /* Call for each tracked instance to be re-enabled */ | |
2400 | list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) { | |
2401 | err = hci_enable_ext_advertising_sync(hdev, | |
2402 | adv->instance); | |
2403 | if (!err) | |
2404 | continue; | |
2405 | ||
2406 | /* If the instance cannot be resumed remove it */ | |
2407 | hci_remove_ext_adv_instance_sync(hdev, adv->instance, | |
2408 | NULL); | |
2409 | } | |
2410 | } else { | |
2411 | /* Schedule for most recent instance to be restarted and begin | |
2412 | * the software rotation loop | |
2413 | */ | |
2414 | err = hci_schedule_adv_instance_sync(hdev, | |
2415 | hdev->cur_adv_instance, | |
2416 | true); | |
2417 | } | |
2418 | ||
2419 | hdev->advertising_paused = false; | |
2420 | ||
2421 | return err; | |
2422 | } | |
2423 | ||
3c44a431 ZJ |
2424 | static int hci_pause_addr_resolution(struct hci_dev *hdev) |
2425 | { | |
2426 | int err; | |
2427 | ||
2428 | if (!use_ll_privacy(hdev)) | |
2429 | return 0; | |
2430 | ||
2431 | if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) | |
2432 | return 0; | |
2433 | ||
2434 | /* Cannot disable addr resolution if scanning is enabled or | |
2435 | * when initiating an LE connection. | |
2436 | */ | |
2437 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) || | |
2438 | hci_lookup_le_connect(hdev)) { | |
2439 | bt_dev_err(hdev, "Command not allowed when scan/LE connect"); | |
2440 | return -EPERM; | |
2441 | } | |
2442 | ||
2443 | /* Cannot disable addr resolution if advertising is enabled. */ | |
2444 | err = hci_pause_advertising_sync(hdev); | |
2445 | if (err) { | |
2446 | bt_dev_err(hdev, "Pause advertising failed: %d", err); | |
2447 | return err; | |
2448 | } | |
2449 | ||
2450 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); | |
2451 | if (err) | |
2452 | bt_dev_err(hdev, "Unable to disable Address Resolution: %d", | |
2453 | err); | |
2454 | ||
2455 | /* Return if address resolution is disabled and RPA is not used. */ | |
2456 | if (!err && scan_use_rpa(hdev)) | |
ae051b04 | 2457 | return 0; |
3c44a431 ZJ |
2458 | |
2459 | hci_resume_advertising_sync(hdev); | |
2460 | return err; | |
2461 | } | |
2462 | ||
f892244b BG |
2463 | struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev, |
2464 | bool extended, struct sock *sk) | |
2465 | { | |
2466 | u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA : | |
2467 | HCI_OP_READ_LOCAL_OOB_DATA; | |
2468 | ||
2469 | return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk); | |
2470 | } | |
2471 | ||
195ef75e PV |
2472 | static struct conn_params *conn_params_copy(struct list_head *list, size_t *n) |
2473 | { | |
2474 | struct hci_conn_params *params; | |
2475 | struct conn_params *p; | |
2476 | size_t i; | |
2477 | ||
2478 | rcu_read_lock(); | |
2479 | ||
2480 | i = 0; | |
2481 | list_for_each_entry_rcu(params, list, action) | |
2482 | ++i; | |
2483 | *n = i; | |
2484 | ||
2485 | rcu_read_unlock(); | |
2486 | ||
2487 | p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL); | |
2488 | if (!p) | |
2489 | return NULL; | |
2490 | ||
2491 | rcu_read_lock(); | |
2492 | ||
2493 | i = 0; | |
2494 | list_for_each_entry_rcu(params, list, action) { | |
2495 | /* Racing adds are handled in next scan update */ | |
2496 | if (i >= *n) | |
2497 | break; | |
2498 | ||
2499 | /* No hdev->lock, but: addr, addr_type are immutable. | |
2500 | * privacy_mode is only written by us or in | |
2501 | * hci_cc_le_set_privacy_mode that we wait for. | |
2502 | * We should be idempotent so MGMT updating flags | |
2503 | * while we are processing is OK. | |
2504 | */ | |
2505 | bacpy(&p[i].addr, ¶ms->addr); | |
2506 | p[i].addr_type = params->addr_type; | |
2507 | p[i].flags = READ_ONCE(params->flags); | |
2508 | p[i].privacy_mode = READ_ONCE(params->privacy_mode); | |
2509 | ++i; | |
2510 | } | |
2511 | ||
2512 | rcu_read_unlock(); | |
2513 | ||
2514 | *n = i; | |
2515 | return p; | |
2516 | } | |
2517 | ||
ad383c2c LAD |
2518 | /* Device must not be scanning when updating the accept list. |
2519 | * | |
2520 | * Update is done using the following sequence: | |
2521 | * | |
2522 | * use_ll_privacy((Disable Advertising) -> Disable Resolving List) -> | |
2523 | * Remove Devices From Accept List -> | |
2524 | * (has IRK && use_ll_privacy(Remove Devices From Resolving List))-> | |
2525 | * Add Devices to Accept List -> | |
2526 | * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) -> | |
2527 | * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) -> | |
2528 | * Enable Scanning | |
2529 | * | |
2530 | * In case of failure advertising shall be restored to its original state and | |
2531 | * return would disable accept list since either accept or resolving list could | |
2532 | * not be programmed. | |
2533 | * | |
2534 | */ | |
e8907f76 LAD |
2535 | static u8 hci_update_accept_list_sync(struct hci_dev *hdev) |
2536 | { | |
195ef75e | 2537 | struct conn_params *params; |
e8907f76 LAD |
2538 | struct bdaddr_list *b, *t; |
2539 | u8 num_entries = 0; | |
2540 | bool pend_conn, pend_report; | |
80740ebb | 2541 | u8 filter_policy; |
195ef75e | 2542 | size_t i, n; |
ad383c2c LAD |
2543 | int err; |
2544 | ||
3c44a431 | 2545 | /* Pause advertising if resolving list can be used as controllers |
ad383c2c | 2546 | * cannot accept resolving list modifications while advertising. |
e8907f76 | 2547 | */ |
ad383c2c LAD |
2548 | if (use_ll_privacy(hdev)) { |
2549 | err = hci_pause_advertising_sync(hdev); | |
2550 | if (err) { | |
2551 | bt_dev_err(hdev, "pause advertising failed: %d", err); | |
2552 | return 0x00; | |
2553 | } | |
2554 | } | |
e8907f76 | 2555 | |
ad383c2c LAD |
2556 | /* Disable address resolution while reprogramming accept list since |
2557 | * devices that do have an IRK will be programmed in the resolving list | |
2558 | * when LL Privacy is enabled. | |
2559 | */ | |
2560 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); | |
2561 | if (err) { | |
2562 | bt_dev_err(hdev, "Unable to disable LL privacy: %d", err); | |
2563 | goto done; | |
2564 | } | |
e8907f76 LAD |
2565 | |
2566 | /* Go through the current accept list programmed into the | |
6828b583 LAD |
2567 | * controller one by one and check if that address is connected or is |
2568 | * still in the list of pending connections or list of devices to | |
e8907f76 LAD |
2569 | * report. If not present in either list, then remove it from |
2570 | * the controller. | |
2571 | */ | |
2572 | list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) { | |
6828b583 LAD |
2573 | if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type)) |
2574 | continue; | |
2575 | ||
195ef75e | 2576 | /* Pointers not dereferenced, no locks needed */ |
e8907f76 LAD |
2577 | pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns, |
2578 | &b->bdaddr, | |
2579 | b->bdaddr_type); | |
2580 | pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports, | |
2581 | &b->bdaddr, | |
2582 | b->bdaddr_type); | |
2583 | ||
2584 | /* If the device is not likely to connect or report, | |
2585 | * remove it from the acceptlist. | |
2586 | */ | |
2587 | if (!pend_conn && !pend_report) { | |
2588 | hci_le_del_accept_list_sync(hdev, &b->bdaddr, | |
2589 | b->bdaddr_type); | |
2590 | continue; | |
2591 | } | |
2592 | ||
e8907f76 LAD |
2593 | num_entries++; |
2594 | } | |
2595 | ||
2596 | /* Since all no longer valid accept list entries have been | |
2597 | * removed, walk through the list of pending connections | |
2598 | * and ensure that any new device gets programmed into | |
2599 | * the controller. | |
2600 | * | |
2601 | * If the list of the devices is larger than the list of | |
2602 | * available accept list entries in the controller, then | |
2603 | * just abort and return filer policy value to not use the | |
2604 | * accept list. | |
195ef75e PV |
2605 | * |
2606 | * The list and params may be mutated while we wait for events, | |
2607 | * so make a copy and iterate it. | |
e8907f76 | 2608 | */ |
195ef75e PV |
2609 | |
2610 | params = conn_params_copy(&hdev->pend_le_conns, &n); | |
2611 | if (!params) { | |
2612 | err = -ENOMEM; | |
2613 | goto done; | |
2614 | } | |
2615 | ||
2616 | for (i = 0; i < n; ++i) { | |
2617 | err = hci_le_add_accept_list_sync(hdev, ¶ms[i], | |
2618 | &num_entries); | |
2619 | if (err) { | |
2620 | kvfree(params); | |
ad383c2c | 2621 | goto done; |
195ef75e | 2622 | } |
e8907f76 LAD |
2623 | } |
2624 | ||
195ef75e PV |
2625 | kvfree(params); |
2626 | ||
e8907f76 LAD |
2627 | /* After adding all new pending connections, walk through |
2628 | * the list of pending reports and also add these to the | |
2629 | * accept list if there is still space. Abort if space runs out. | |
2630 | */ | |
195ef75e PV |
2631 | |
2632 | params = conn_params_copy(&hdev->pend_le_reports, &n); | |
2633 | if (!params) { | |
2634 | err = -ENOMEM; | |
2635 | goto done; | |
2636 | } | |
2637 | ||
2638 | for (i = 0; i < n; ++i) { | |
2639 | err = hci_le_add_accept_list_sync(hdev, ¶ms[i], | |
2640 | &num_entries); | |
2641 | if (err) { | |
2642 | kvfree(params); | |
ad383c2c | 2643 | goto done; |
195ef75e | 2644 | } |
e8907f76 LAD |
2645 | } |
2646 | ||
195ef75e PV |
2647 | kvfree(params); |
2648 | ||
e8907f76 LAD |
2649 | /* Use the allowlist unless the following conditions are all true: |
2650 | * - We are not currently suspending | |
2651 | * - There are 1 or more ADV monitors registered and it's not offloaded | |
2652 | * - Interleaved scanning is not currently using the allowlist | |
2653 | */ | |
2654 | if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended && | |
2655 | hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE && | |
2656 | hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST) | |
ad383c2c LAD |
2657 | err = -EINVAL; |
2658 | ||
2659 | done: | |
80740ebb LAD |
2660 | filter_policy = err ? 0x00 : 0x01; |
2661 | ||
ad383c2c LAD |
2662 | /* Enable address resolution when LL Privacy is enabled. */ |
2663 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01); | |
2664 | if (err) | |
2665 | bt_dev_err(hdev, "Unable to enable LL privacy: %d", err); | |
2666 | ||
2667 | /* Resume advertising if it was paused */ | |
2668 | if (use_ll_privacy(hdev)) | |
2669 | hci_resume_advertising_sync(hdev); | |
e8907f76 LAD |
2670 | |
2671 | /* Select filter policy to use accept list */ | |
80740ebb | 2672 | return filter_policy; |
e8907f76 LAD |
2673 | } |
2674 | ||
2675 | /* Returns true if an le connection is in the scanning state */ | |
2676 | static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev) | |
2677 | { | |
2678 | struct hci_conn_hash *h = &hdev->conn_hash; | |
2679 | struct hci_conn *c; | |
2680 | ||
2681 | rcu_read_lock(); | |
2682 | ||
2683 | list_for_each_entry_rcu(c, &h->list, list) { | |
2684 | if (c->type == LE_LINK && c->state == BT_CONNECT && | |
2685 | test_bit(HCI_CONN_SCANNING, &c->flags)) { | |
2686 | rcu_read_unlock(); | |
2687 | return true; | |
2688 | } | |
2689 | } | |
2690 | ||
2691 | rcu_read_unlock(); | |
2692 | ||
2693 | return false; | |
2694 | } | |
2695 | ||
2696 | static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type, | |
2697 | u16 interval, u16 window, | |
2698 | u8 own_addr_type, u8 filter_policy) | |
2699 | { | |
2700 | struct hci_cp_le_set_ext_scan_params *cp; | |
2701 | struct hci_cp_le_scan_phy_params *phy; | |
2702 | u8 data[sizeof(*cp) + sizeof(*phy) * 2]; | |
2703 | u8 num_phy = 0; | |
2704 | ||
2705 | cp = (void *)data; | |
2706 | phy = (void *)cp->data; | |
2707 | ||
2708 | memset(data, 0, sizeof(data)); | |
2709 | ||
2710 | cp->own_addr_type = own_addr_type; | |
2711 | cp->filter_policy = filter_policy; | |
2712 | ||
2713 | if (scan_1m(hdev) || scan_2m(hdev)) { | |
2714 | cp->scanning_phys |= LE_SCAN_PHY_1M; | |
2715 | ||
2716 | phy->type = type; | |
2717 | phy->interval = cpu_to_le16(interval); | |
2718 | phy->window = cpu_to_le16(window); | |
2719 | ||
2720 | num_phy++; | |
2721 | phy++; | |
2722 | } | |
2723 | ||
2724 | if (scan_coded(hdev)) { | |
2725 | cp->scanning_phys |= LE_SCAN_PHY_CODED; | |
2726 | ||
2727 | phy->type = type; | |
2728 | phy->interval = cpu_to_le16(interval); | |
2729 | phy->window = cpu_to_le16(window); | |
2730 | ||
2731 | num_phy++; | |
2732 | phy++; | |
2733 | } | |
2734 | ||
2735 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS, | |
2736 | sizeof(*cp) + sizeof(*phy) * num_phy, | |
2737 | data, HCI_CMD_TIMEOUT); | |
2738 | } | |
2739 | ||
2740 | static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type, | |
2741 | u16 interval, u16 window, | |
2742 | u8 own_addr_type, u8 filter_policy) | |
2743 | { | |
2744 | struct hci_cp_le_set_scan_param cp; | |
2745 | ||
2746 | if (use_ext_scan(hdev)) | |
2747 | return hci_le_set_ext_scan_param_sync(hdev, type, interval, | |
2748 | window, own_addr_type, | |
2749 | filter_policy); | |
2750 | ||
2751 | memset(&cp, 0, sizeof(cp)); | |
2752 | cp.type = type; | |
2753 | cp.interval = cpu_to_le16(interval); | |
2754 | cp.window = cpu_to_le16(window); | |
2755 | cp.own_address_type = own_addr_type; | |
2756 | cp.filter_policy = filter_policy; | |
2757 | ||
2758 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM, | |
2759 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2760 | } | |
2761 | ||
2762 | static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval, | |
abfeea47 LAD |
2763 | u16 window, u8 own_addr_type, u8 filter_policy, |
2764 | u8 filter_dup) | |
e8907f76 LAD |
2765 | { |
2766 | int err; | |
2767 | ||
2768 | if (hdev->scanning_paused) { | |
2769 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
2770 | return 0; | |
2771 | } | |
2772 | ||
e8907f76 LAD |
2773 | err = hci_le_set_scan_param_sync(hdev, type, interval, window, |
2774 | own_addr_type, filter_policy); | |
2775 | if (err) | |
2776 | return err; | |
2777 | ||
abfeea47 | 2778 | return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup); |
e8907f76 LAD |
2779 | } |
2780 | ||
27592ca1 | 2781 | static int hci_passive_scan_sync(struct hci_dev *hdev) |
e8907f76 LAD |
2782 | { |
2783 | u8 own_addr_type; | |
2784 | u8 filter_policy; | |
2785 | u16 window, interval; | |
b338d917 | 2786 | u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE; |
ad383c2c | 2787 | int err; |
e8907f76 LAD |
2788 | |
2789 | if (hdev->scanning_paused) { | |
2790 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
2791 | return 0; | |
2792 | } | |
2793 | ||
ad383c2c LAD |
2794 | err = hci_scan_disable_sync(hdev); |
2795 | if (err) { | |
2796 | bt_dev_err(hdev, "disable scanning failed: %d", err); | |
2797 | return err; | |
2798 | } | |
2799 | ||
e8907f76 LAD |
2800 | /* Set require_privacy to false since no SCAN_REQ are send |
2801 | * during passive scanning. Not using an non-resolvable address | |
2802 | * here is important so that peer devices using direct | |
2803 | * advertising with our address will be correctly reported | |
2804 | * by the controller. | |
2805 | */ | |
2806 | if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev), | |
2807 | &own_addr_type)) | |
2808 | return 0; | |
2809 | ||
2810 | if (hdev->enable_advmon_interleave_scan && | |
2811 | hci_update_interleaved_scan_sync(hdev)) | |
2812 | return 0; | |
2813 | ||
2814 | bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state); | |
ad383c2c | 2815 | |
e8907f76 LAD |
2816 | /* Adding or removing entries from the accept list must |
2817 | * happen before enabling scanning. The controller does | |
2818 | * not allow accept list modification while scanning. | |
2819 | */ | |
2820 | filter_policy = hci_update_accept_list_sync(hdev); | |
2821 | ||
2822 | /* When the controller is using random resolvable addresses and | |
2823 | * with that having LE privacy enabled, then controllers with | |
2824 | * Extended Scanner Filter Policies support can now enable support | |
2825 | * for handling directed advertising. | |
2826 | * | |
2827 | * So instead of using filter polices 0x00 (no acceptlist) | |
2828 | * and 0x01 (acceptlist enabled) use the new filter policies | |
2829 | * 0x02 (no acceptlist) and 0x03 (acceptlist enabled). | |
2830 | */ | |
2831 | if (hci_dev_test_flag(hdev, HCI_PRIVACY) && | |
2832 | (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) | |
2833 | filter_policy |= 0x02; | |
2834 | ||
2835 | if (hdev->suspended) { | |
2836 | window = hdev->le_scan_window_suspend; | |
2837 | interval = hdev->le_scan_int_suspend; | |
e8907f76 LAD |
2838 | } else if (hci_is_le_conn_scanning(hdev)) { |
2839 | window = hdev->le_scan_window_connect; | |
2840 | interval = hdev->le_scan_int_connect; | |
2841 | } else if (hci_is_adv_monitoring(hdev)) { | |
2842 | window = hdev->le_scan_window_adv_monitor; | |
2843 | interval = hdev->le_scan_int_adv_monitor; | |
2844 | } else { | |
2845 | window = hdev->le_scan_window; | |
2846 | interval = hdev->le_scan_interval; | |
2847 | } | |
2848 | ||
b338d917 BG |
2849 | /* Disable all filtering for Mesh */ |
2850 | if (hci_dev_test_flag(hdev, HCI_MESH)) { | |
2851 | filter_policy = 0; | |
2852 | filter_dups = LE_SCAN_FILTER_DUP_DISABLE; | |
2853 | } | |
2854 | ||
e8907f76 LAD |
2855 | bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy); |
2856 | ||
2857 | return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window, | |
b338d917 | 2858 | own_addr_type, filter_policy, filter_dups); |
e8907f76 LAD |
2859 | } |
2860 | ||
2861 | /* This function controls the passive scanning based on hdev->pend_le_conns | |
2862 | * list. If there are pending LE connection we start the background scanning, | |
ad383c2c LAD |
2863 | * otherwise we stop it in the following sequence: |
2864 | * | |
2865 | * If there are devices to scan: | |
2866 | * | |
2867 | * Disable Scanning -> Update Accept List -> | |
2868 | * use_ll_privacy((Disable Advertising) -> Disable Resolving List -> | |
2869 | * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) -> | |
2870 | * Enable Scanning | |
2871 | * | |
2872 | * Otherwise: | |
2873 | * | |
2874 | * Disable Scanning | |
e8907f76 LAD |
2875 | */ |
2876 | int hci_update_passive_scan_sync(struct hci_dev *hdev) | |
2877 | { | |
2878 | int err; | |
2879 | ||
2880 | if (!test_bit(HCI_UP, &hdev->flags) || | |
2881 | test_bit(HCI_INIT, &hdev->flags) || | |
2882 | hci_dev_test_flag(hdev, HCI_SETUP) || | |
2883 | hci_dev_test_flag(hdev, HCI_CONFIG) || | |
2884 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || | |
2885 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) | |
2886 | return 0; | |
2887 | ||
2888 | /* No point in doing scanning if LE support hasn't been enabled */ | |
2889 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
2890 | return 0; | |
2891 | ||
2892 | /* If discovery is active don't interfere with it */ | |
2893 | if (hdev->discovery.state != DISCOVERY_STOPPED) | |
2894 | return 0; | |
2895 | ||
2896 | /* Reset RSSI and UUID filters when starting background scanning | |
2897 | * since these filters are meant for service discovery only. | |
2898 | * | |
2899 | * The Start Discovery and Start Service Discovery operations | |
2900 | * ensure to set proper values for RSSI threshold and UUID | |
2901 | * filter list. So it is safe to just reset them here. | |
2902 | */ | |
2903 | hci_discovery_filter_clear(hdev); | |
2904 | ||
2905 | bt_dev_dbg(hdev, "ADV monitoring is %s", | |
2906 | hci_is_adv_monitoring(hdev) ? "on" : "off"); | |
2907 | ||
b338d917 BG |
2908 | if (!hci_dev_test_flag(hdev, HCI_MESH) && |
2909 | list_empty(&hdev->pend_le_conns) && | |
e8907f76 | 2910 | list_empty(&hdev->pend_le_reports) && |
eca0ae4a LAD |
2911 | !hci_is_adv_monitoring(hdev) && |
2912 | !hci_dev_test_flag(hdev, HCI_PA_SYNC)) { | |
e8907f76 LAD |
2913 | /* If there is no pending LE connections or devices |
2914 | * to be scanned for or no ADV monitors, we should stop the | |
2915 | * background scanning. | |
2916 | */ | |
2917 | ||
2918 | bt_dev_dbg(hdev, "stopping background scanning"); | |
2919 | ||
ad383c2c | 2920 | err = hci_scan_disable_sync(hdev); |
e8907f76 LAD |
2921 | if (err) |
2922 | bt_dev_err(hdev, "stop background scanning failed: %d", | |
2923 | err); | |
2924 | } else { | |
2925 | /* If there is at least one pending LE connection, we should | |
2926 | * keep the background scan running. | |
2927 | */ | |
2928 | ||
2929 | /* If controller is connecting, we should not start scanning | |
2930 | * since some controllers are not able to scan and connect at | |
2931 | * the same time. | |
2932 | */ | |
2933 | if (hci_lookup_le_connect(hdev)) | |
2934 | return 0; | |
2935 | ||
e8907f76 LAD |
2936 | bt_dev_dbg(hdev, "start background scanning"); |
2937 | ||
2938 | err = hci_passive_scan_sync(hdev); | |
2939 | if (err) | |
2940 | bt_dev_err(hdev, "start background scanning failed: %d", | |
2941 | err); | |
2942 | } | |
2943 | ||
2944 | return err; | |
2945 | } | |
ad383c2c | 2946 | |
bb876725 BG |
2947 | static int update_scan_sync(struct hci_dev *hdev, void *data) |
2948 | { | |
2949 | return hci_update_scan_sync(hdev); | |
2950 | } | |
2951 | ||
2952 | int hci_update_scan(struct hci_dev *hdev) | |
2953 | { | |
2954 | return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL); | |
2955 | } | |
2956 | ||
ad383c2c LAD |
2957 | static int update_passive_scan_sync(struct hci_dev *hdev, void *data) |
2958 | { | |
2959 | return hci_update_passive_scan_sync(hdev); | |
2960 | } | |
2961 | ||
2962 | int hci_update_passive_scan(struct hci_dev *hdev) | |
2963 | { | |
5bee2fd6 LAD |
2964 | /* Only queue if it would have any effect */ |
2965 | if (!test_bit(HCI_UP, &hdev->flags) || | |
2966 | test_bit(HCI_INIT, &hdev->flags) || | |
2967 | hci_dev_test_flag(hdev, HCI_SETUP) || | |
2968 | hci_dev_test_flag(hdev, HCI_CONFIG) || | |
2969 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || | |
2970 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) | |
2971 | return 0; | |
2972 | ||
ad383c2c LAD |
2973 | return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL); |
2974 | } | |
cf75ad8b | 2975 | |
2f2eb0c9 | 2976 | int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val) |
cf75ad8b | 2977 | { |
2f2eb0c9 BG |
2978 | int err; |
2979 | ||
cf75ad8b LAD |
2980 | if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev)) |
2981 | return 0; | |
2982 | ||
2f2eb0c9 | 2983 | err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, |
cf75ad8b | 2984 | sizeof(val), &val, HCI_CMD_TIMEOUT); |
2f2eb0c9 BG |
2985 | |
2986 | if (!err) { | |
2987 | if (val) { | |
2988 | hdev->features[1][0] |= LMP_HOST_SC; | |
2989 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); | |
2990 | } else { | |
2991 | hdev->features[1][0] &= ~LMP_HOST_SC; | |
2992 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); | |
2993 | } | |
2994 | } | |
2995 | ||
2996 | return err; | |
cf75ad8b LAD |
2997 | } |
2998 | ||
3244845c | 2999 | int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode) |
cf75ad8b LAD |
3000 | { |
3001 | int err; | |
3002 | ||
3003 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || | |
3004 | lmp_host_ssp_capable(hdev)) | |
3005 | return 0; | |
3006 | ||
3244845c BG |
3007 | if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) { |
3008 | __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, | |
3009 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
3010 | } | |
3011 | ||
cf75ad8b LAD |
3012 | err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, |
3013 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
3014 | if (err) | |
3015 | return err; | |
3016 | ||
3017 | return hci_write_sc_support_sync(hdev, 0x01); | |
3018 | } | |
3019 | ||
d81a494c | 3020 | int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul) |
cf75ad8b LAD |
3021 | { |
3022 | struct hci_cp_write_le_host_supported cp; | |
3023 | ||
3024 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) || | |
3025 | !lmp_bredr_capable(hdev)) | |
3026 | return 0; | |
3027 | ||
3028 | /* Check first if we already have the right host state | |
3029 | * (host features set) | |
3030 | */ | |
3031 | if (le == lmp_host_le_capable(hdev) && | |
3032 | simul == lmp_host_le_br_capable(hdev)) | |
3033 | return 0; | |
3034 | ||
3035 | memset(&cp, 0, sizeof(cp)); | |
3036 | ||
3037 | cp.le = le; | |
3038 | cp.simul = simul; | |
3039 | ||
3040 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, | |
3041 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3042 | } | |
3043 | ||
3044 | static int hci_powered_update_adv_sync(struct hci_dev *hdev) | |
3045 | { | |
3046 | struct adv_info *adv, *tmp; | |
3047 | int err; | |
3048 | ||
3049 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
3050 | return 0; | |
3051 | ||
3052 | /* If RPA Resolution has not been enable yet it means the | |
3053 | * resolving list is empty and we should attempt to program the | |
3054 | * local IRK in order to support using own_addr_type | |
3055 | * ADDR_LE_DEV_RANDOM_RESOLVED (0x03). | |
3056 | */ | |
3057 | if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) { | |
3058 | hci_le_add_resolve_list_sync(hdev, NULL); | |
3059 | hci_le_set_addr_resolution_enable_sync(hdev, 0x01); | |
3060 | } | |
3061 | ||
3062 | /* Make sure the controller has a good default for | |
3063 | * advertising data. This also applies to the case | |
3064 | * where BR/EDR was toggled during the AUTO_OFF phase. | |
3065 | */ | |
3066 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || | |
3067 | list_empty(&hdev->adv_instances)) { | |
3068 | if (ext_adv_capable(hdev)) { | |
3069 | err = hci_setup_ext_adv_instance_sync(hdev, 0x00); | |
3070 | if (!err) | |
3071 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
3072 | } else { | |
3073 | err = hci_update_adv_data_sync(hdev, 0x00); | |
3074 | if (!err) | |
3075 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
3076 | } | |
3077 | ||
3078 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
3079 | hci_enable_advertising_sync(hdev); | |
3080 | } | |
3081 | ||
3082 | /* Call for each tracked instance to be scheduled */ | |
3083 | list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) | |
3084 | hci_schedule_adv_instance_sync(hdev, adv->instance, true); | |
3085 | ||
3086 | return 0; | |
3087 | } | |
3088 | ||
3089 | static int hci_write_auth_enable_sync(struct hci_dev *hdev) | |
3090 | { | |
3091 | u8 link_sec; | |
3092 | ||
3093 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); | |
3094 | if (link_sec == test_bit(HCI_AUTH, &hdev->flags)) | |
3095 | return 0; | |
3096 | ||
3097 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE, | |
3098 | sizeof(link_sec), &link_sec, | |
3099 | HCI_CMD_TIMEOUT); | |
3100 | } | |
3101 | ||
353a0249 | 3102 | int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable) |
cf75ad8b LAD |
3103 | { |
3104 | struct hci_cp_write_page_scan_activity cp; | |
3105 | u8 type; | |
3106 | int err = 0; | |
3107 | ||
3108 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
3109 | return 0; | |
3110 | ||
3111 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
3112 | return 0; | |
3113 | ||
3114 | memset(&cp, 0, sizeof(cp)); | |
3115 | ||
3116 | if (enable) { | |
3117 | type = PAGE_SCAN_TYPE_INTERLACED; | |
3118 | ||
3119 | /* 160 msec page scan interval */ | |
3120 | cp.interval = cpu_to_le16(0x0100); | |
3121 | } else { | |
3122 | type = hdev->def_page_scan_type; | |
3123 | cp.interval = cpu_to_le16(hdev->def_page_scan_int); | |
3124 | } | |
3125 | ||
3126 | cp.window = cpu_to_le16(hdev->def_page_scan_window); | |
3127 | ||
3128 | if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval || | |
3129 | __cpu_to_le16(hdev->page_scan_window) != cp.window) { | |
3130 | err = __hci_cmd_sync_status(hdev, | |
3131 | HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, | |
3132 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3133 | if (err) | |
3134 | return err; | |
3135 | } | |
3136 | ||
3137 | if (hdev->page_scan_type != type) | |
3138 | err = __hci_cmd_sync_status(hdev, | |
3139 | HCI_OP_WRITE_PAGE_SCAN_TYPE, | |
3140 | sizeof(type), &type, | |
3141 | HCI_CMD_TIMEOUT); | |
3142 | ||
3143 | return err; | |
3144 | } | |
3145 | ||
3146 | static bool disconnected_accept_list_entries(struct hci_dev *hdev) | |
3147 | { | |
3148 | struct bdaddr_list *b; | |
3149 | ||
3150 | list_for_each_entry(b, &hdev->accept_list, list) { | |
3151 | struct hci_conn *conn; | |
3152 | ||
3153 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); | |
3154 | if (!conn) | |
3155 | return true; | |
3156 | ||
3157 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) | |
3158 | return true; | |
3159 | } | |
3160 | ||
3161 | return false; | |
3162 | } | |
3163 | ||
3164 | static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val) | |
3165 | { | |
3166 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE, | |
3167 | sizeof(val), &val, | |
3168 | HCI_CMD_TIMEOUT); | |
3169 | } | |
3170 | ||
451d95a9 | 3171 | int hci_update_scan_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
3172 | { |
3173 | u8 scan; | |
3174 | ||
3175 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
3176 | return 0; | |
3177 | ||
3178 | if (!hdev_is_powered(hdev)) | |
3179 | return 0; | |
3180 | ||
3181 | if (mgmt_powering_down(hdev)) | |
3182 | return 0; | |
3183 | ||
3184 | if (hdev->scanning_paused) | |
3185 | return 0; | |
3186 | ||
3187 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || | |
3188 | disconnected_accept_list_entries(hdev)) | |
3189 | scan = SCAN_PAGE; | |
3190 | else | |
3191 | scan = SCAN_DISABLED; | |
3192 | ||
3193 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) | |
3194 | scan |= SCAN_INQUIRY; | |
3195 | ||
3196 | if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) && | |
3197 | test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY)) | |
3198 | return 0; | |
3199 | ||
3200 | return hci_write_scan_enable_sync(hdev, scan); | |
3201 | } | |
3202 | ||
6f6ff38a | 3203 | int hci_update_name_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
3204 | { |
3205 | struct hci_cp_write_local_name cp; | |
3206 | ||
3207 | memset(&cp, 0, sizeof(cp)); | |
3208 | ||
3209 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); | |
3210 | ||
3211 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME, | |
3212 | sizeof(cp), &cp, | |
3213 | HCI_CMD_TIMEOUT); | |
3214 | } | |
3215 | ||
3216 | /* This function perform powered update HCI command sequence after the HCI init | |
3217 | * sequence which end up resetting all states, the sequence is as follows: | |
3218 | * | |
3219 | * HCI_SSP_ENABLED(Enable SSP) | |
3220 | * HCI_LE_ENABLED(Enable LE) | |
3221 | * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) -> | |
3222 | * Update adv data) | |
3223 | * Enable Authentication | |
3224 | * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class -> | |
3225 | * Set Name -> Set EIR) | |
e411443c | 3226 | * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address) |
cf75ad8b LAD |
3227 | */ |
3228 | int hci_powered_update_sync(struct hci_dev *hdev) | |
3229 | { | |
3230 | int err; | |
3231 | ||
3232 | /* Register the available SMP channels (BR/EDR and LE) only when | |
3233 | * successfully powering on the controller. This late | |
3234 | * registration is required so that LE SMP can clearly decide if | |
3235 | * the public address or static address is used. | |
3236 | */ | |
3237 | smp_register(hdev); | |
3238 | ||
3239 | err = hci_write_ssp_mode_sync(hdev, 0x01); | |
3240 | if (err) | |
3241 | return err; | |
3242 | ||
3243 | err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00); | |
3244 | if (err) | |
3245 | return err; | |
3246 | ||
3247 | err = hci_powered_update_adv_sync(hdev); | |
3248 | if (err) | |
3249 | return err; | |
3250 | ||
3251 | err = hci_write_auth_enable_sync(hdev); | |
3252 | if (err) | |
3253 | return err; | |
3254 | ||
3255 | if (lmp_bredr_capable(hdev)) { | |
3256 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) | |
3257 | hci_write_fast_connectable_sync(hdev, true); | |
3258 | else | |
3259 | hci_write_fast_connectable_sync(hdev, false); | |
3260 | hci_update_scan_sync(hdev); | |
3261 | hci_update_class_sync(hdev); | |
3262 | hci_update_name_sync(hdev); | |
3263 | hci_update_eir_sync(hdev); | |
3264 | } | |
3265 | ||
e411443c LAD |
3266 | /* If forcing static address is in use or there is no public |
3267 | * address use the static address as random address (but skip | |
3268 | * the HCI command if the current random address is already the | |
3269 | * static one. | |
3270 | * | |
3271 | * In case BR/EDR has been disabled on a dual-mode controller | |
3272 | * and a static address has been configured, then use that | |
3273 | * address instead of the public BR/EDR address. | |
3274 | */ | |
3275 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || | |
3276 | (!bacmp(&hdev->bdaddr, BDADDR_ANY) && | |
3277 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) { | |
3278 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) | |
3279 | return hci_set_random_addr_sync(hdev, | |
3280 | &hdev->static_addr); | |
3281 | } | |
3282 | ||
cf75ad8b LAD |
3283 | return 0; |
3284 | } | |
3285 | ||
d0b13706 LAD |
3286 | /** |
3287 | * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address | |
3288 | * (BD_ADDR) for a HCI device from | |
3289 | * a firmware node property. | |
3290 | * @hdev: The HCI device | |
cf75ad8b | 3291 | * |
d0b13706 LAD |
3292 | * Search the firmware node for 'local-bd-address'. |
3293 | * | |
3294 | * All-zero BD addresses are rejected, because those could be properties | |
3295 | * that exist in the firmware tables, but were not updated by the firmware. For | |
3296 | * example, the DTS could define 'local-bd-address', with zero BD addresses. | |
cf75ad8b | 3297 | */ |
d0b13706 | 3298 | static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev) |
cf75ad8b | 3299 | { |
d0b13706 LAD |
3300 | struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent); |
3301 | bdaddr_t ba; | |
3302 | int ret; | |
cf75ad8b | 3303 | |
d0b13706 LAD |
3304 | ret = fwnode_property_read_u8_array(fwnode, "local-bd-address", |
3305 | (u8 *)&ba, sizeof(ba)); | |
3306 | if (ret < 0 || !bacmp(&ba, BDADDR_ANY)) | |
3307 | return; | |
cf75ad8b | 3308 | |
d0b13706 LAD |
3309 | bacpy(&hdev->public_addr, &ba); |
3310 | } | |
cf75ad8b | 3311 | |
d0b13706 LAD |
3312 | struct hci_init_stage { |
3313 | int (*func)(struct hci_dev *hdev); | |
3314 | }; | |
cf75ad8b | 3315 | |
d0b13706 LAD |
3316 | /* Run init stage NULL terminated function table */ |
3317 | static int hci_init_stage_sync(struct hci_dev *hdev, | |
3318 | const struct hci_init_stage *stage) | |
3319 | { | |
3320 | size_t i; | |
cf75ad8b | 3321 | |
d0b13706 LAD |
3322 | for (i = 0; stage[i].func; i++) { |
3323 | int err; | |
cf75ad8b | 3324 | |
d0b13706 LAD |
3325 | err = stage[i].func(hdev); |
3326 | if (err) | |
3327 | return err; | |
cf75ad8b LAD |
3328 | } |
3329 | ||
3330 | return 0; | |
3331 | } | |
3332 | ||
d0b13706 LAD |
3333 | /* Read Local Version */ |
3334 | static int hci_read_local_version_sync(struct hci_dev *hdev) | |
cf75ad8b | 3335 | { |
d0b13706 LAD |
3336 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION, |
3337 | 0, NULL, HCI_CMD_TIMEOUT); | |
3338 | } | |
cf75ad8b | 3339 | |
d0b13706 LAD |
3340 | /* Read BD Address */ |
3341 | static int hci_read_bd_addr_sync(struct hci_dev *hdev) | |
3342 | { | |
3343 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR, | |
3344 | 0, NULL, HCI_CMD_TIMEOUT); | |
3345 | } | |
cf75ad8b | 3346 | |
d0b13706 LAD |
3347 | #define HCI_INIT(_func) \ |
3348 | { \ | |
3349 | .func = _func, \ | |
cf75ad8b LAD |
3350 | } |
3351 | ||
d0b13706 LAD |
3352 | static const struct hci_init_stage hci_init0[] = { |
3353 | /* HCI_OP_READ_LOCAL_VERSION */ | |
3354 | HCI_INIT(hci_read_local_version_sync), | |
3355 | /* HCI_OP_READ_BD_ADDR */ | |
3356 | HCI_INIT(hci_read_bd_addr_sync), | |
3357 | {} | |
3358 | }; | |
3359 | ||
3360 | int hci_reset_sync(struct hci_dev *hdev) | |
cf75ad8b | 3361 | { |
cf75ad8b LAD |
3362 | int err; |
3363 | ||
d0b13706 | 3364 | set_bit(HCI_RESET, &hdev->flags); |
cf75ad8b | 3365 | |
d0b13706 LAD |
3366 | err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL, |
3367 | HCI_CMD_TIMEOUT); | |
3368 | if (err) | |
3369 | return err; | |
cf75ad8b | 3370 | |
d0b13706 LAD |
3371 | return 0; |
3372 | } | |
cf75ad8b | 3373 | |
d0b13706 LAD |
3374 | static int hci_init0_sync(struct hci_dev *hdev) |
3375 | { | |
3376 | int err; | |
cf75ad8b | 3377 | |
d0b13706 LAD |
3378 | bt_dev_dbg(hdev, ""); |
3379 | ||
3380 | /* Reset */ | |
3381 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { | |
3382 | err = hci_reset_sync(hdev); | |
cf75ad8b LAD |
3383 | if (err) |
3384 | return err; | |
3385 | } | |
3386 | ||
d0b13706 LAD |
3387 | return hci_init_stage_sync(hdev, hci_init0); |
3388 | } | |
abfeea47 | 3389 | |
d0b13706 LAD |
3390 | static int hci_unconf_init_sync(struct hci_dev *hdev) |
3391 | { | |
3392 | int err; | |
3393 | ||
3394 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) | |
cf75ad8b LAD |
3395 | return 0; |
3396 | ||
d0b13706 LAD |
3397 | err = hci_init0_sync(hdev); |
3398 | if (err < 0) | |
3399 | return err; | |
cf75ad8b | 3400 | |
d0b13706 LAD |
3401 | if (hci_dev_test_flag(hdev, HCI_SETUP)) |
3402 | hci_debugfs_create_basic(hdev); | |
cf75ad8b LAD |
3403 | |
3404 | return 0; | |
3405 | } | |
3406 | ||
d0b13706 LAD |
3407 | /* Read Local Supported Features. */ |
3408 | static int hci_read_local_features_sync(struct hci_dev *hdev) | |
cf75ad8b | 3409 | { |
d0b13706 LAD |
3410 | /* Not all AMP controllers support this command */ |
3411 | if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20)) | |
3412 | return 0; | |
cf75ad8b | 3413 | |
d0b13706 LAD |
3414 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES, |
3415 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
3416 | } |
3417 | ||
d0b13706 LAD |
3418 | /* BR Controller init stage 1 command sequence */ |
3419 | static const struct hci_init_stage br_init1[] = { | |
3420 | /* HCI_OP_READ_LOCAL_FEATURES */ | |
3421 | HCI_INIT(hci_read_local_features_sync), | |
3422 | /* HCI_OP_READ_LOCAL_VERSION */ | |
3423 | HCI_INIT(hci_read_local_version_sync), | |
3424 | /* HCI_OP_READ_BD_ADDR */ | |
3425 | HCI_INIT(hci_read_bd_addr_sync), | |
3426 | {} | |
3427 | }; | |
3428 | ||
3429 | /* Read Local Commands */ | |
3430 | static int hci_read_local_cmds_sync(struct hci_dev *hdev) | |
cf75ad8b | 3431 | { |
d0b13706 LAD |
3432 | /* All Bluetooth 1.2 and later controllers should support the |
3433 | * HCI command for reading the local supported commands. | |
3434 | * | |
3435 | * Unfortunately some controllers indicate Bluetooth 1.2 support, | |
3436 | * but do not have support for this command. If that is the case, | |
3437 | * the driver can quirk the behavior and skip reading the local | |
3438 | * supported commands. | |
3439 | */ | |
3440 | if (hdev->hci_ver > BLUETOOTH_VER_1_1 && | |
3441 | !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) | |
3442 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS, | |
3443 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b | 3444 | |
d0b13706 | 3445 | return 0; |
cf75ad8b LAD |
3446 | } |
3447 | ||
d0b13706 LAD |
3448 | /* Read Local AMP Info */ |
3449 | static int hci_read_local_amp_info_sync(struct hci_dev *hdev) | |
cf75ad8b | 3450 | { |
d0b13706 LAD |
3451 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO, |
3452 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
3453 | } |
3454 | ||
d0b13706 LAD |
3455 | /* Read Data Blk size */ |
3456 | static int hci_read_data_block_size_sync(struct hci_dev *hdev) | |
cf75ad8b | 3457 | { |
d0b13706 LAD |
3458 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, |
3459 | 0, NULL, HCI_CMD_TIMEOUT); | |
3460 | } | |
cf75ad8b | 3461 | |
d0b13706 LAD |
3462 | /* Read Flow Control Mode */ |
3463 | static int hci_read_flow_control_mode_sync(struct hci_dev *hdev) | |
3464 | { | |
3465 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, | |
3466 | 0, NULL, HCI_CMD_TIMEOUT); | |
3467 | } | |
cf75ad8b | 3468 | |
d0b13706 LAD |
3469 | /* Read Location Data */ |
3470 | static int hci_read_location_data_sync(struct hci_dev *hdev) | |
3471 | { | |
3472 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA, | |
3473 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
3474 | } |
3475 | ||
d0b13706 LAD |
3476 | /* AMP Controller init stage 1 command sequence */ |
3477 | static const struct hci_init_stage amp_init1[] = { | |
3478 | /* HCI_OP_READ_LOCAL_VERSION */ | |
3479 | HCI_INIT(hci_read_local_version_sync), | |
3480 | /* HCI_OP_READ_LOCAL_COMMANDS */ | |
3481 | HCI_INIT(hci_read_local_cmds_sync), | |
3482 | /* HCI_OP_READ_LOCAL_AMP_INFO */ | |
3483 | HCI_INIT(hci_read_local_amp_info_sync), | |
3484 | /* HCI_OP_READ_DATA_BLOCK_SIZE */ | |
3485 | HCI_INIT(hci_read_data_block_size_sync), | |
3486 | /* HCI_OP_READ_FLOW_CONTROL_MODE */ | |
3487 | HCI_INIT(hci_read_flow_control_mode_sync), | |
3488 | /* HCI_OP_READ_LOCATION_DATA */ | |
3489 | HCI_INIT(hci_read_location_data_sync), | |
bce56405 | 3490 | {} |
d0b13706 LAD |
3491 | }; |
3492 | ||
3493 | static int hci_init1_sync(struct hci_dev *hdev) | |
cf75ad8b | 3494 | { |
d0b13706 | 3495 | int err; |
cf75ad8b | 3496 | |
d0b13706 | 3497 | bt_dev_dbg(hdev, ""); |
cf75ad8b | 3498 | |
d0b13706 LAD |
3499 | /* Reset */ |
3500 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { | |
3501 | err = hci_reset_sync(hdev); | |
3502 | if (err) | |
3503 | return err; | |
3504 | } | |
cf75ad8b | 3505 | |
d0b13706 LAD |
3506 | switch (hdev->dev_type) { |
3507 | case HCI_PRIMARY: | |
3508 | hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; | |
3509 | return hci_init_stage_sync(hdev, br_init1); | |
3510 | case HCI_AMP: | |
3511 | hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; | |
3512 | return hci_init_stage_sync(hdev, amp_init1); | |
3513 | default: | |
3514 | bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type); | |
3515 | break; | |
3516 | } | |
3517 | ||
3518 | return 0; | |
cf75ad8b LAD |
3519 | } |
3520 | ||
d0b13706 LAD |
3521 | /* AMP Controller init stage 2 command sequence */ |
3522 | static const struct hci_init_stage amp_init2[] = { | |
3523 | /* HCI_OP_READ_LOCAL_FEATURES */ | |
3524 | HCI_INIT(hci_read_local_features_sync), | |
bce56405 | 3525 | {} |
d0b13706 LAD |
3526 | }; |
3527 | ||
3528 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ | |
3529 | static int hci_read_buffer_size_sync(struct hci_dev *hdev) | |
cf75ad8b | 3530 | { |
d0b13706 LAD |
3531 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE, |
3532 | 0, NULL, HCI_CMD_TIMEOUT); | |
3533 | } | |
cf75ad8b | 3534 | |
d0b13706 LAD |
3535 | /* Read Class of Device */ |
3536 | static int hci_read_dev_class_sync(struct hci_dev *hdev) | |
3537 | { | |
3538 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV, | |
3539 | 0, NULL, HCI_CMD_TIMEOUT); | |
3540 | } | |
cf75ad8b | 3541 | |
d0b13706 LAD |
3542 | /* Read Local Name */ |
3543 | static int hci_read_local_name_sync(struct hci_dev *hdev) | |
3544 | { | |
3545 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME, | |
3546 | 0, NULL, HCI_CMD_TIMEOUT); | |
3547 | } | |
cf75ad8b | 3548 | |
d0b13706 LAD |
3549 | /* Read Voice Setting */ |
3550 | static int hci_read_voice_setting_sync(struct hci_dev *hdev) | |
3551 | { | |
3552 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING, | |
3553 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
3554 | } |
3555 | ||
d0b13706 LAD |
3556 | /* Read Number of Supported IAC */ |
3557 | static int hci_read_num_supported_iac_sync(struct hci_dev *hdev) | |
cf75ad8b | 3558 | { |
d0b13706 LAD |
3559 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC, |
3560 | 0, NULL, HCI_CMD_TIMEOUT); | |
3561 | } | |
cf75ad8b | 3562 | |
d0b13706 LAD |
3563 | /* Read Current IAC LAP */ |
3564 | static int hci_read_current_iac_lap_sync(struct hci_dev *hdev) | |
3565 | { | |
3566 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP, | |
3567 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
3568 | } |
3569 | ||
d0b13706 LAD |
3570 | static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type, |
3571 | u8 cond_type, bdaddr_t *bdaddr, | |
3572 | u8 auto_accept) | |
cf75ad8b | 3573 | { |
d0b13706 | 3574 | struct hci_cp_set_event_filter cp; |
cf75ad8b | 3575 | |
d0b13706 | 3576 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
cf75ad8b LAD |
3577 | return 0; |
3578 | ||
0eaecfb2 IFM |
3579 | if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) |
3580 | return 0; | |
3581 | ||
d0b13706 LAD |
3582 | memset(&cp, 0, sizeof(cp)); |
3583 | cp.flt_type = flt_type; | |
cf75ad8b | 3584 | |
d0b13706 LAD |
3585 | if (flt_type != HCI_FLT_CLEAR_ALL) { |
3586 | cp.cond_type = cond_type; | |
3587 | bacpy(&cp.addr_conn_flt.bdaddr, bdaddr); | |
3588 | cp.addr_conn_flt.auto_accept = auto_accept; | |
cf75ad8b LAD |
3589 | } |
3590 | ||
d0b13706 LAD |
3591 | return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT, |
3592 | flt_type == HCI_FLT_CLEAR_ALL ? | |
3593 | sizeof(cp.flt_type) : sizeof(cp), &cp, | |
3594 | HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
3595 | } |
3596 | ||
d0b13706 | 3597 | static int hci_clear_event_filter_sync(struct hci_dev *hdev) |
cf75ad8b | 3598 | { |
d0b13706 LAD |
3599 | if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED)) |
3600 | return 0; | |
3601 | ||
0eaecfb2 IFM |
3602 | /* In theory the state machine should not reach here unless |
3603 | * a hci_set_event_filter_sync() call succeeds, but we do | |
3604 | * the check both for parity and as a future reminder. | |
3605 | */ | |
3606 | if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) | |
3607 | return 0; | |
3608 | ||
d0b13706 LAD |
3609 | return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00, |
3610 | BDADDR_ANY, 0x00); | |
3611 | } | |
3612 | ||
3613 | /* Connection accept timeout ~20 secs */ | |
3614 | static int hci_write_ca_timeout_sync(struct hci_dev *hdev) | |
3615 | { | |
3616 | __le16 param = cpu_to_le16(0x7d00); | |
3617 | ||
3618 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT, | |
3619 | sizeof(param), ¶m, HCI_CMD_TIMEOUT); | |
3620 | } | |
3621 | ||
3622 | /* BR Controller init stage 2 command sequence */ | |
3623 | static const struct hci_init_stage br_init2[] = { | |
3624 | /* HCI_OP_READ_BUFFER_SIZE */ | |
3625 | HCI_INIT(hci_read_buffer_size_sync), | |
3626 | /* HCI_OP_READ_CLASS_OF_DEV */ | |
3627 | HCI_INIT(hci_read_dev_class_sync), | |
3628 | /* HCI_OP_READ_LOCAL_NAME */ | |
3629 | HCI_INIT(hci_read_local_name_sync), | |
3630 | /* HCI_OP_READ_VOICE_SETTING */ | |
3631 | HCI_INIT(hci_read_voice_setting_sync), | |
3632 | /* HCI_OP_READ_NUM_SUPPORTED_IAC */ | |
3633 | HCI_INIT(hci_read_num_supported_iac_sync), | |
3634 | /* HCI_OP_READ_CURRENT_IAC_LAP */ | |
3635 | HCI_INIT(hci_read_current_iac_lap_sync), | |
3636 | /* HCI_OP_SET_EVENT_FLT */ | |
3637 | HCI_INIT(hci_clear_event_filter_sync), | |
3638 | /* HCI_OP_WRITE_CA_TIMEOUT */ | |
3639 | HCI_INIT(hci_write_ca_timeout_sync), | |
3640 | {} | |
3641 | }; | |
3642 | ||
3643 | static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev) | |
3644 | { | |
3645 | u8 mode = 0x01; | |
3646 | ||
3647 | if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
3648 | return 0; | |
3649 | ||
3650 | /* When SSP is available, then the host features page | |
3651 | * should also be available as well. However some | |
3652 | * controllers list the max_page as 0 as long as SSP | |
3653 | * has not been enabled. To achieve proper debugging | |
3654 | * output, force the minimum max_page to 1 at least. | |
3655 | */ | |
3656 | hdev->max_page = 0x01; | |
3657 | ||
3658 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, | |
3659 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
3660 | } | |
3661 | ||
3662 | static int hci_write_eir_sync(struct hci_dev *hdev) | |
3663 | { | |
3664 | struct hci_cp_write_eir cp; | |
3665 | ||
3666 | if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
3667 | return 0; | |
3668 | ||
3669 | memset(hdev->eir, 0, sizeof(hdev->eir)); | |
3670 | memset(&cp, 0, sizeof(cp)); | |
3671 | ||
3672 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, | |
3673 | HCI_CMD_TIMEOUT); | |
3674 | } | |
3675 | ||
3676 | static int hci_write_inquiry_mode_sync(struct hci_dev *hdev) | |
3677 | { | |
3678 | u8 mode; | |
3679 | ||
3680 | if (!lmp_inq_rssi_capable(hdev) && | |
3681 | !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) | |
3682 | return 0; | |
3683 | ||
3684 | /* If Extended Inquiry Result events are supported, then | |
3685 | * they are clearly preferred over Inquiry Result with RSSI | |
3686 | * events. | |
3687 | */ | |
3688 | mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; | |
3689 | ||
3690 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE, | |
3691 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
3692 | } | |
3693 | ||
3694 | static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev) | |
3695 | { | |
3696 | if (!lmp_inq_tx_pwr_capable(hdev)) | |
3697 | return 0; | |
3698 | ||
3699 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, | |
3700 | 0, NULL, HCI_CMD_TIMEOUT); | |
3701 | } | |
3702 | ||
3703 | static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page) | |
3704 | { | |
3705 | struct hci_cp_read_local_ext_features cp; | |
3706 | ||
3707 | if (!lmp_ext_feat_capable(hdev)) | |
3708 | return 0; | |
3709 | ||
3710 | memset(&cp, 0, sizeof(cp)); | |
3711 | cp.page = page; | |
3712 | ||
3713 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, | |
3714 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3715 | } | |
3716 | ||
3717 | static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev) | |
3718 | { | |
3719 | return hci_read_local_ext_features_sync(hdev, 0x01); | |
3720 | } | |
3721 | ||
3722 | /* HCI Controller init stage 2 command sequence */ | |
3723 | static const struct hci_init_stage hci_init2[] = { | |
3724 | /* HCI_OP_READ_LOCAL_COMMANDS */ | |
3725 | HCI_INIT(hci_read_local_cmds_sync), | |
3726 | /* HCI_OP_WRITE_SSP_MODE */ | |
3727 | HCI_INIT(hci_write_ssp_mode_1_sync), | |
3728 | /* HCI_OP_WRITE_EIR */ | |
3729 | HCI_INIT(hci_write_eir_sync), | |
3730 | /* HCI_OP_WRITE_INQUIRY_MODE */ | |
3731 | HCI_INIT(hci_write_inquiry_mode_sync), | |
3732 | /* HCI_OP_READ_INQ_RSP_TX_POWER */ | |
3733 | HCI_INIT(hci_read_inq_rsp_tx_power_sync), | |
3734 | /* HCI_OP_READ_LOCAL_EXT_FEATURES */ | |
3735 | HCI_INIT(hci_read_local_ext_features_1_sync), | |
3736 | /* HCI_OP_WRITE_AUTH_ENABLE */ | |
3737 | HCI_INIT(hci_write_auth_enable_sync), | |
3738 | {} | |
3739 | }; | |
3740 | ||
3741 | /* Read LE Buffer Size */ | |
3742 | static int hci_le_read_buffer_size_sync(struct hci_dev *hdev) | |
3743 | { | |
c1631dbc | 3744 | /* Use Read LE Buffer Size V2 if supported */ |
3a4d29b6 | 3745 | if (iso_capable(hdev) && hdev->commands[41] & 0x20) |
c1631dbc LAD |
3746 | return __hci_cmd_sync_status(hdev, |
3747 | HCI_OP_LE_READ_BUFFER_SIZE_V2, | |
3748 | 0, NULL, HCI_CMD_TIMEOUT); | |
3749 | ||
d0b13706 LAD |
3750 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE, |
3751 | 0, NULL, HCI_CMD_TIMEOUT); | |
3752 | } | |
3753 | ||
3754 | /* Read LE Local Supported Features */ | |
3755 | static int hci_le_read_local_features_sync(struct hci_dev *hdev) | |
3756 | { | |
3757 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES, | |
3758 | 0, NULL, HCI_CMD_TIMEOUT); | |
3759 | } | |
3760 | ||
3761 | /* Read LE Supported States */ | |
3762 | static int hci_le_read_supported_states_sync(struct hci_dev *hdev) | |
3763 | { | |
3764 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES, | |
3765 | 0, NULL, HCI_CMD_TIMEOUT); | |
3766 | } | |
3767 | ||
3768 | /* LE Controller init stage 2 command sequence */ | |
3769 | static const struct hci_init_stage le_init2[] = { | |
d0b13706 LAD |
3770 | /* HCI_OP_LE_READ_LOCAL_FEATURES */ |
3771 | HCI_INIT(hci_le_read_local_features_sync), | |
3a4d29b6 LAD |
3772 | /* HCI_OP_LE_READ_BUFFER_SIZE */ |
3773 | HCI_INIT(hci_le_read_buffer_size_sync), | |
d0b13706 LAD |
3774 | /* HCI_OP_LE_READ_SUPPORTED_STATES */ |
3775 | HCI_INIT(hci_le_read_supported_states_sync), | |
3776 | {} | |
3777 | }; | |
3778 | ||
3779 | static int hci_init2_sync(struct hci_dev *hdev) | |
3780 | { | |
3781 | int err; | |
3782 | ||
3783 | bt_dev_dbg(hdev, ""); | |
3784 | ||
3785 | if (hdev->dev_type == HCI_AMP) | |
3786 | return hci_init_stage_sync(hdev, amp_init2); | |
3787 | ||
26afbd82 LAD |
3788 | err = hci_init_stage_sync(hdev, hci_init2); |
3789 | if (err) | |
3790 | return err; | |
3791 | ||
d0b13706 LAD |
3792 | if (lmp_bredr_capable(hdev)) { |
3793 | err = hci_init_stage_sync(hdev, br_init2); | |
3794 | if (err) | |
3795 | return err; | |
3796 | } else { | |
3797 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); | |
3798 | } | |
3799 | ||
3800 | if (lmp_le_capable(hdev)) { | |
3801 | err = hci_init_stage_sync(hdev, le_init2); | |
3802 | if (err) | |
3803 | return err; | |
3804 | /* LE-only controllers have LE implicitly enabled */ | |
3805 | if (!lmp_bredr_capable(hdev)) | |
3806 | hci_dev_set_flag(hdev, HCI_LE_ENABLED); | |
3807 | } | |
3808 | ||
26afbd82 | 3809 | return 0; |
d0b13706 LAD |
3810 | } |
3811 | ||
3812 | static int hci_set_event_mask_sync(struct hci_dev *hdev) | |
3813 | { | |
3814 | /* The second byte is 0xff instead of 0x9f (two reserved bits | |
3815 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the | |
3816 | * command otherwise. | |
3817 | */ | |
3818 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; | |
3819 | ||
3820 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set | |
3821 | * any event mask for pre 1.2 devices. | |
3822 | */ | |
3823 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
3824 | return 0; | |
3825 | ||
3826 | if (lmp_bredr_capable(hdev)) { | |
3827 | events[4] |= 0x01; /* Flow Specification Complete */ | |
182ee45d LAD |
3828 | |
3829 | /* Don't set Disconnect Complete when suspended as that | |
3830 | * would wakeup the host when disconnecting due to | |
3831 | * suspend. | |
3832 | */ | |
3833 | if (hdev->suspended) | |
3834 | events[0] &= 0xef; | |
d0b13706 LAD |
3835 | } else { |
3836 | /* Use a different default for LE-only devices */ | |
3837 | memset(events, 0, sizeof(events)); | |
3838 | events[1] |= 0x20; /* Command Complete */ | |
3839 | events[1] |= 0x40; /* Command Status */ | |
3840 | events[1] |= 0x80; /* Hardware Error */ | |
3841 | ||
3842 | /* If the controller supports the Disconnect command, enable | |
3843 | * the corresponding event. In addition enable packet flow | |
3844 | * control related events. | |
3845 | */ | |
3846 | if (hdev->commands[0] & 0x20) { | |
182ee45d LAD |
3847 | /* Don't set Disconnect Complete when suspended as that |
3848 | * would wakeup the host when disconnecting due to | |
3849 | * suspend. | |
3850 | */ | |
3851 | if (!hdev->suspended) | |
3852 | events[0] |= 0x10; /* Disconnection Complete */ | |
d0b13706 LAD |
3853 | events[2] |= 0x04; /* Number of Completed Packets */ |
3854 | events[3] |= 0x02; /* Data Buffer Overflow */ | |
3855 | } | |
3856 | ||
3857 | /* If the controller supports the Read Remote Version | |
3858 | * Information command, enable the corresponding event. | |
3859 | */ | |
3860 | if (hdev->commands[2] & 0x80) | |
3861 | events[1] |= 0x08; /* Read Remote Version Information | |
3862 | * Complete | |
3863 | */ | |
3864 | ||
3865 | if (hdev->le_features[0] & HCI_LE_ENCRYPTION) { | |
3866 | events[0] |= 0x80; /* Encryption Change */ | |
3867 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ | |
3868 | } | |
3869 | } | |
3870 | ||
3871 | if (lmp_inq_rssi_capable(hdev) || | |
3872 | test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) | |
3873 | events[4] |= 0x02; /* Inquiry Result with RSSI */ | |
3874 | ||
3875 | if (lmp_ext_feat_capable(hdev)) | |
3876 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ | |
3877 | ||
3878 | if (lmp_esco_capable(hdev)) { | |
3879 | events[5] |= 0x08; /* Synchronous Connection Complete */ | |
3880 | events[5] |= 0x10; /* Synchronous Connection Changed */ | |
3881 | } | |
3882 | ||
3883 | if (lmp_sniffsubr_capable(hdev)) | |
3884 | events[5] |= 0x20; /* Sniff Subrating */ | |
3885 | ||
3886 | if (lmp_pause_enc_capable(hdev)) | |
3887 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ | |
3888 | ||
3889 | if (lmp_ext_inq_capable(hdev)) | |
3890 | events[5] |= 0x40; /* Extended Inquiry Result */ | |
3891 | ||
3892 | if (lmp_no_flush_capable(hdev)) | |
3893 | events[7] |= 0x01; /* Enhanced Flush Complete */ | |
3894 | ||
3895 | if (lmp_lsto_capable(hdev)) | |
3896 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ | |
3897 | ||
3898 | if (lmp_ssp_capable(hdev)) { | |
3899 | events[6] |= 0x01; /* IO Capability Request */ | |
3900 | events[6] |= 0x02; /* IO Capability Response */ | |
3901 | events[6] |= 0x04; /* User Confirmation Request */ | |
3902 | events[6] |= 0x08; /* User Passkey Request */ | |
3903 | events[6] |= 0x10; /* Remote OOB Data Request */ | |
3904 | events[6] |= 0x20; /* Simple Pairing Complete */ | |
3905 | events[7] |= 0x04; /* User Passkey Notification */ | |
3906 | events[7] |= 0x08; /* Keypress Notification */ | |
3907 | events[7] |= 0x10; /* Remote Host Supported | |
3908 | * Features Notification | |
3909 | */ | |
3910 | } | |
3911 | ||
3912 | if (lmp_le_capable(hdev)) | |
3913 | events[7] |= 0x20; /* LE Meta-Event */ | |
3914 | ||
3915 | return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK, | |
3916 | sizeof(events), events, HCI_CMD_TIMEOUT); | |
3917 | } | |
3918 | ||
3919 | static int hci_read_stored_link_key_sync(struct hci_dev *hdev) | |
3920 | { | |
3921 | struct hci_cp_read_stored_link_key cp; | |
3922 | ||
3923 | if (!(hdev->commands[6] & 0x20) || | |
3924 | test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) | |
3925 | return 0; | |
3926 | ||
3927 | memset(&cp, 0, sizeof(cp)); | |
3928 | bacpy(&cp.bdaddr, BDADDR_ANY); | |
3929 | cp.read_all = 0x01; | |
3930 | ||
3931 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY, | |
3932 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3933 | } | |
3934 | ||
3935 | static int hci_setup_link_policy_sync(struct hci_dev *hdev) | |
3936 | { | |
3937 | struct hci_cp_write_def_link_policy cp; | |
3938 | u16 link_policy = 0; | |
3939 | ||
3940 | if (!(hdev->commands[5] & 0x10)) | |
3941 | return 0; | |
3942 | ||
3943 | memset(&cp, 0, sizeof(cp)); | |
3944 | ||
3945 | if (lmp_rswitch_capable(hdev)) | |
3946 | link_policy |= HCI_LP_RSWITCH; | |
3947 | if (lmp_hold_capable(hdev)) | |
3948 | link_policy |= HCI_LP_HOLD; | |
3949 | if (lmp_sniff_capable(hdev)) | |
3950 | link_policy |= HCI_LP_SNIFF; | |
3951 | if (lmp_park_capable(hdev)) | |
3952 | link_policy |= HCI_LP_PARK; | |
3953 | ||
3954 | cp.policy = cpu_to_le16(link_policy); | |
3955 | ||
3956 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, | |
3957 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3958 | } | |
3959 | ||
3960 | static int hci_read_page_scan_activity_sync(struct hci_dev *hdev) | |
3961 | { | |
3962 | if (!(hdev->commands[8] & 0x01)) | |
3963 | return 0; | |
3964 | ||
3965 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY, | |
3966 | 0, NULL, HCI_CMD_TIMEOUT); | |
3967 | } | |
3968 | ||
3969 | static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev) | |
3970 | { | |
3971 | if (!(hdev->commands[18] & 0x04) || | |
42d7731e IFM |
3972 | !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) || |
3973 | test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks)) | |
d0b13706 LAD |
3974 | return 0; |
3975 | ||
3976 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING, | |
3977 | 0, NULL, HCI_CMD_TIMEOUT); | |
3978 | } | |
3979 | ||
3980 | static int hci_read_page_scan_type_sync(struct hci_dev *hdev) | |
3981 | { | |
3982 | /* Some older Broadcom based Bluetooth 1.2 controllers do not | |
3983 | * support the Read Page Scan Type command. Check support for | |
3984 | * this command in the bit mask of supported commands. | |
3985 | */ | |
3986 | if (!(hdev->commands[13] & 0x01)) | |
3987 | return 0; | |
3988 | ||
3989 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE, | |
3990 | 0, NULL, HCI_CMD_TIMEOUT); | |
3991 | } | |
3992 | ||
3993 | /* Read features beyond page 1 if available */ | |
3994 | static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev) | |
3995 | { | |
3996 | u8 page; | |
3997 | int err; | |
3998 | ||
3999 | if (!lmp_ext_feat_capable(hdev)) | |
4000 | return 0; | |
4001 | ||
4002 | for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page; | |
4003 | page++) { | |
4004 | err = hci_read_local_ext_features_sync(hdev, page); | |
4005 | if (err) | |
4006 | return err; | |
4007 | } | |
4008 | ||
4009 | return 0; | |
4010 | } | |
4011 | ||
4012 | /* HCI Controller init stage 3 command sequence */ | |
4013 | static const struct hci_init_stage hci_init3[] = { | |
4014 | /* HCI_OP_SET_EVENT_MASK */ | |
4015 | HCI_INIT(hci_set_event_mask_sync), | |
4016 | /* HCI_OP_READ_STORED_LINK_KEY */ | |
4017 | HCI_INIT(hci_read_stored_link_key_sync), | |
4018 | /* HCI_OP_WRITE_DEF_LINK_POLICY */ | |
4019 | HCI_INIT(hci_setup_link_policy_sync), | |
4020 | /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */ | |
4021 | HCI_INIT(hci_read_page_scan_activity_sync), | |
4022 | /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */ | |
4023 | HCI_INIT(hci_read_def_err_data_reporting_sync), | |
4024 | /* HCI_OP_READ_PAGE_SCAN_TYPE */ | |
4025 | HCI_INIT(hci_read_page_scan_type_sync), | |
4026 | /* HCI_OP_READ_LOCAL_EXT_FEATURES */ | |
4027 | HCI_INIT(hci_read_local_ext_features_all_sync), | |
4028 | {} | |
4029 | }; | |
4030 | ||
4031 | static int hci_le_set_event_mask_sync(struct hci_dev *hdev) | |
4032 | { | |
4033 | u8 events[8]; | |
4034 | ||
4035 | if (!lmp_le_capable(hdev)) | |
4036 | return 0; | |
4037 | ||
4038 | memset(events, 0, sizeof(events)); | |
4039 | ||
4040 | if (hdev->le_features[0] & HCI_LE_ENCRYPTION) | |
4041 | events[0] |= 0x10; /* LE Long Term Key Request */ | |
4042 | ||
4043 | /* If controller supports the Connection Parameters Request | |
4044 | * Link Layer Procedure, enable the corresponding event. | |
4045 | */ | |
4046 | if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) | |
4047 | /* LE Remote Connection Parameter Request */ | |
4048 | events[0] |= 0x20; | |
4049 | ||
4050 | /* If the controller supports the Data Length Extension | |
4051 | * feature, enable the corresponding event. | |
4052 | */ | |
4053 | if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) | |
4054 | events[0] |= 0x40; /* LE Data Length Change */ | |
4055 | ||
a56a1138 LAD |
4056 | /* If the controller supports LL Privacy feature or LE Extended Adv, |
4057 | * enable the corresponding event. | |
d0b13706 | 4058 | */ |
a56a1138 | 4059 | if (use_enhanced_conn_complete(hdev)) |
d0b13706 LAD |
4060 | events[1] |= 0x02; /* LE Enhanced Connection Complete */ |
4061 | ||
4062 | /* If the controller supports Extended Scanner Filter | |
4063 | * Policies, enable the corresponding event. | |
4064 | */ | |
4065 | if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY) | |
4066 | events[1] |= 0x04; /* LE Direct Advertising Report */ | |
4067 | ||
4068 | /* If the controller supports Channel Selection Algorithm #2 | |
4069 | * feature, enable the corresponding event. | |
4070 | */ | |
4071 | if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2) | |
4072 | events[2] |= 0x08; /* LE Channel Selection Algorithm */ | |
4073 | ||
4074 | /* If the controller supports the LE Set Scan Enable command, | |
4075 | * enable the corresponding advertising report event. | |
4076 | */ | |
4077 | if (hdev->commands[26] & 0x08) | |
4078 | events[0] |= 0x02; /* LE Advertising Report */ | |
4079 | ||
4080 | /* If the controller supports the LE Create Connection | |
4081 | * command, enable the corresponding event. | |
4082 | */ | |
4083 | if (hdev->commands[26] & 0x10) | |
4084 | events[0] |= 0x01; /* LE Connection Complete */ | |
4085 | ||
4086 | /* If the controller supports the LE Connection Update | |
4087 | * command, enable the corresponding event. | |
4088 | */ | |
4089 | if (hdev->commands[27] & 0x04) | |
4090 | events[0] |= 0x04; /* LE Connection Update Complete */ | |
4091 | ||
4092 | /* If the controller supports the LE Read Remote Used Features | |
4093 | * command, enable the corresponding event. | |
4094 | */ | |
4095 | if (hdev->commands[27] & 0x20) | |
4096 | /* LE Read Remote Used Features Complete */ | |
4097 | events[0] |= 0x08; | |
4098 | ||
4099 | /* If the controller supports the LE Read Local P-256 | |
4100 | * Public Key command, enable the corresponding event. | |
4101 | */ | |
4102 | if (hdev->commands[34] & 0x02) | |
4103 | /* LE Read Local P-256 Public Key Complete */ | |
4104 | events[0] |= 0x80; | |
4105 | ||
4106 | /* If the controller supports the LE Generate DHKey | |
4107 | * command, enable the corresponding event. | |
4108 | */ | |
4109 | if (hdev->commands[34] & 0x04) | |
4110 | events[1] |= 0x01; /* LE Generate DHKey Complete */ | |
4111 | ||
4112 | /* If the controller supports the LE Set Default PHY or | |
4113 | * LE Set PHY commands, enable the corresponding event. | |
4114 | */ | |
4115 | if (hdev->commands[35] & (0x20 | 0x40)) | |
4116 | events[1] |= 0x08; /* LE PHY Update Complete */ | |
4117 | ||
4118 | /* If the controller supports LE Set Extended Scan Parameters | |
4119 | * and LE Set Extended Scan Enable commands, enable the | |
4120 | * corresponding event. | |
4121 | */ | |
4122 | if (use_ext_scan(hdev)) | |
4123 | events[1] |= 0x10; /* LE Extended Advertising Report */ | |
4124 | ||
4125 | /* If the controller supports the LE Extended Advertising | |
4126 | * command, enable the corresponding event. | |
4127 | */ | |
4128 | if (ext_adv_capable(hdev)) | |
4129 | events[2] |= 0x02; /* LE Advertising Set Terminated */ | |
4130 | ||
26afbd82 LAD |
4131 | if (cis_capable(hdev)) { |
4132 | events[3] |= 0x01; /* LE CIS Established */ | |
4133 | if (cis_peripheral_capable(hdev)) | |
4134 | events[3] |= 0x02; /* LE CIS Request */ | |
4135 | } | |
4136 | ||
eca0ae4a | 4137 | if (bis_capable(hdev)) { |
c33362a5 CD |
4138 | events[1] |= 0x20; /* LE PA Report */ |
4139 | events[1] |= 0x40; /* LE PA Sync Established */ | |
eca0ae4a LAD |
4140 | events[3] |= 0x04; /* LE Create BIG Complete */ |
4141 | events[3] |= 0x08; /* LE Terminate BIG Complete */ | |
4142 | events[3] |= 0x10; /* LE BIG Sync Established */ | |
4143 | events[3] |= 0x20; /* LE BIG Sync Loss */ | |
c33362a5 | 4144 | events[4] |= 0x02; /* LE BIG Info Advertising Report */ |
eca0ae4a LAD |
4145 | } |
4146 | ||
d0b13706 LAD |
4147 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK, |
4148 | sizeof(events), events, HCI_CMD_TIMEOUT); | |
4149 | } | |
4150 | ||
4151 | /* Read LE Advertising Channel TX Power */ | |
4152 | static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev) | |
4153 | { | |
4154 | if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) { | |
4155 | /* HCI TS spec forbids mixing of legacy and extended | |
4156 | * advertising commands wherein READ_ADV_TX_POWER is | |
4157 | * also included. So do not call it if extended adv | |
4158 | * is supported otherwise controller will return | |
4159 | * COMMAND_DISALLOWED for extended commands. | |
4160 | */ | |
4161 | return __hci_cmd_sync_status(hdev, | |
4162 | HCI_OP_LE_READ_ADV_TX_POWER, | |
4163 | 0, NULL, HCI_CMD_TIMEOUT); | |
4164 | } | |
4165 | ||
4166 | return 0; | |
4167 | } | |
4168 | ||
4169 | /* Read LE Min/Max Tx Power*/ | |
4170 | static int hci_le_read_tx_power_sync(struct hci_dev *hdev) | |
4171 | { | |
d2f8114f AG |
4172 | if (!(hdev->commands[38] & 0x80) || |
4173 | test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks)) | |
d0b13706 LAD |
4174 | return 0; |
4175 | ||
4176 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER, | |
4177 | 0, NULL, HCI_CMD_TIMEOUT); | |
4178 | } | |
4179 | ||
4180 | /* Read LE Accept List Size */ | |
4181 | static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev) | |
4182 | { | |
4183 | if (!(hdev->commands[26] & 0x40)) | |
4184 | return 0; | |
4185 | ||
4186 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE, | |
4187 | 0, NULL, HCI_CMD_TIMEOUT); | |
4188 | } | |
4189 | ||
4190 | /* Clear LE Accept List */ | |
4191 | static int hci_le_clear_accept_list_sync(struct hci_dev *hdev) | |
4192 | { | |
4193 | if (!(hdev->commands[26] & 0x80)) | |
4194 | return 0; | |
4195 | ||
4196 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL, | |
4197 | HCI_CMD_TIMEOUT); | |
4198 | } | |
4199 | ||
4200 | /* Read LE Resolving List Size */ | |
4201 | static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev) | |
4202 | { | |
4203 | if (!(hdev->commands[34] & 0x40)) | |
4204 | return 0; | |
4205 | ||
4206 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE, | |
4207 | 0, NULL, HCI_CMD_TIMEOUT); | |
4208 | } | |
4209 | ||
4210 | /* Clear LE Resolving List */ | |
4211 | static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev) | |
4212 | { | |
4213 | if (!(hdev->commands[34] & 0x20)) | |
4214 | return 0; | |
4215 | ||
4216 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL, | |
4217 | HCI_CMD_TIMEOUT); | |
4218 | } | |
4219 | ||
4220 | /* Set RPA timeout */ | |
4221 | static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev) | |
4222 | { | |
4223 | __le16 timeout = cpu_to_le16(hdev->rpa_timeout); | |
4224 | ||
91b6d02d RC |
4225 | if (!(hdev->commands[35] & 0x04) || |
4226 | test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks)) | |
d0b13706 LAD |
4227 | return 0; |
4228 | ||
4229 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT, | |
4230 | sizeof(timeout), &timeout, | |
4231 | HCI_CMD_TIMEOUT); | |
4232 | } | |
4233 | ||
4234 | /* Read LE Maximum Data Length */ | |
4235 | static int hci_le_read_max_data_len_sync(struct hci_dev *hdev) | |
4236 | { | |
4237 | if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) | |
4238 | return 0; | |
4239 | ||
4240 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL, | |
4241 | HCI_CMD_TIMEOUT); | |
4242 | } | |
4243 | ||
4244 | /* Read LE Suggested Default Data Length */ | |
4245 | static int hci_le_read_def_data_len_sync(struct hci_dev *hdev) | |
4246 | { | |
4247 | if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) | |
4248 | return 0; | |
4249 | ||
4250 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL, | |
4251 | HCI_CMD_TIMEOUT); | |
4252 | } | |
4253 | ||
4254 | /* Read LE Number of Supported Advertising Sets */ | |
4255 | static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev) | |
4256 | { | |
4257 | if (!ext_adv_capable(hdev)) | |
4258 | return 0; | |
4259 | ||
4260 | return __hci_cmd_sync_status(hdev, | |
4261 | HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS, | |
4262 | 0, NULL, HCI_CMD_TIMEOUT); | |
4263 | } | |
4264 | ||
4265 | /* Write LE Host Supported */ | |
4266 | static int hci_set_le_support_sync(struct hci_dev *hdev) | |
4267 | { | |
4268 | struct hci_cp_write_le_host_supported cp; | |
4269 | ||
4270 | /* LE-only devices do not support explicit enablement */ | |
4271 | if (!lmp_bredr_capable(hdev)) | |
4272 | return 0; | |
4273 | ||
4274 | memset(&cp, 0, sizeof(cp)); | |
4275 | ||
4276 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { | |
4277 | cp.le = 0x01; | |
4278 | cp.simul = 0x00; | |
4279 | } | |
4280 | ||
4281 | if (cp.le == lmp_host_le_capable(hdev)) | |
4282 | return 0; | |
4283 | ||
4284 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, | |
4285 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4286 | } | |
4287 | ||
26afbd82 LAD |
4288 | /* LE Set Host Feature */ |
4289 | static int hci_le_set_host_feature_sync(struct hci_dev *hdev) | |
4290 | { | |
4291 | struct hci_cp_le_set_host_feature cp; | |
4292 | ||
4293 | if (!iso_capable(hdev)) | |
4294 | return 0; | |
4295 | ||
4296 | memset(&cp, 0, sizeof(cp)); | |
4297 | ||
4298 | /* Isochronous Channels (Host Support) */ | |
4299 | cp.bit_number = 32; | |
4300 | cp.bit_value = 1; | |
4301 | ||
4302 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE, | |
4303 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4304 | } | |
4305 | ||
d0b13706 LAD |
4306 | /* LE Controller init stage 3 command sequence */ |
4307 | static const struct hci_init_stage le_init3[] = { | |
4308 | /* HCI_OP_LE_SET_EVENT_MASK */ | |
4309 | HCI_INIT(hci_le_set_event_mask_sync), | |
4310 | /* HCI_OP_LE_READ_ADV_TX_POWER */ | |
4311 | HCI_INIT(hci_le_read_adv_tx_power_sync), | |
4312 | /* HCI_OP_LE_READ_TRANSMIT_POWER */ | |
4313 | HCI_INIT(hci_le_read_tx_power_sync), | |
4314 | /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */ | |
4315 | HCI_INIT(hci_le_read_accept_list_size_sync), | |
4316 | /* HCI_OP_LE_CLEAR_ACCEPT_LIST */ | |
4317 | HCI_INIT(hci_le_clear_accept_list_sync), | |
4318 | /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */ | |
4319 | HCI_INIT(hci_le_read_resolv_list_size_sync), | |
4320 | /* HCI_OP_LE_CLEAR_RESOLV_LIST */ | |
4321 | HCI_INIT(hci_le_clear_resolv_list_sync), | |
4322 | /* HCI_OP_LE_SET_RPA_TIMEOUT */ | |
4323 | HCI_INIT(hci_le_set_rpa_timeout_sync), | |
4324 | /* HCI_OP_LE_READ_MAX_DATA_LEN */ | |
4325 | HCI_INIT(hci_le_read_max_data_len_sync), | |
4326 | /* HCI_OP_LE_READ_DEF_DATA_LEN */ | |
4327 | HCI_INIT(hci_le_read_def_data_len_sync), | |
4328 | /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */ | |
4329 | HCI_INIT(hci_le_read_num_support_adv_sets_sync), | |
4330 | /* HCI_OP_WRITE_LE_HOST_SUPPORTED */ | |
4331 | HCI_INIT(hci_set_le_support_sync), | |
26afbd82 LAD |
4332 | /* HCI_OP_LE_SET_HOST_FEATURE */ |
4333 | HCI_INIT(hci_le_set_host_feature_sync), | |
d0b13706 LAD |
4334 | {} |
4335 | }; | |
4336 | ||
4337 | static int hci_init3_sync(struct hci_dev *hdev) | |
4338 | { | |
4339 | int err; | |
4340 | ||
4341 | bt_dev_dbg(hdev, ""); | |
4342 | ||
4343 | err = hci_init_stage_sync(hdev, hci_init3); | |
4344 | if (err) | |
4345 | return err; | |
4346 | ||
4347 | if (lmp_le_capable(hdev)) | |
4348 | return hci_init_stage_sync(hdev, le_init3); | |
4349 | ||
4350 | return 0; | |
4351 | } | |
4352 | ||
4353 | static int hci_delete_stored_link_key_sync(struct hci_dev *hdev) | |
4354 | { | |
4355 | struct hci_cp_delete_stored_link_key cp; | |
4356 | ||
4357 | /* Some Broadcom based Bluetooth controllers do not support the | |
4358 | * Delete Stored Link Key command. They are clearly indicating its | |
4359 | * absence in the bit mask of supported commands. | |
4360 | * | |
4361 | * Check the supported commands and only if the command is marked | |
4362 | * as supported send it. If not supported assume that the controller | |
4363 | * does not have actual support for stored link keys which makes this | |
4364 | * command redundant anyway. | |
4365 | * | |
4366 | * Some controllers indicate that they support handling deleting | |
4367 | * stored link keys, but they don't. The quirk lets a driver | |
4368 | * just disable this command. | |
4369 | */ | |
4370 | if (!(hdev->commands[6] & 0x80) || | |
4371 | test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) | |
4372 | return 0; | |
4373 | ||
4374 | memset(&cp, 0, sizeof(cp)); | |
4375 | bacpy(&cp.bdaddr, BDADDR_ANY); | |
4376 | cp.delete_all = 0x01; | |
4377 | ||
4378 | return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY, | |
4379 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4380 | } | |
4381 | ||
4382 | static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev) | |
4383 | { | |
4384 | u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
4385 | bool changed = false; | |
4386 | ||
4387 | /* Set event mask page 2 if the HCI command for it is supported */ | |
4388 | if (!(hdev->commands[22] & 0x04)) | |
4389 | return 0; | |
4390 | ||
4391 | /* If Connectionless Peripheral Broadcast central role is supported | |
4392 | * enable all necessary events for it. | |
4393 | */ | |
4394 | if (lmp_cpb_central_capable(hdev)) { | |
4395 | events[1] |= 0x40; /* Triggered Clock Capture */ | |
4396 | events[1] |= 0x80; /* Synchronization Train Complete */ | |
0feb8af0 | 4397 | events[2] |= 0x08; /* Truncated Page Complete */ |
d0b13706 LAD |
4398 | events[2] |= 0x20; /* CPB Channel Map Change */ |
4399 | changed = true; | |
4400 | } | |
4401 | ||
4402 | /* If Connectionless Peripheral Broadcast peripheral role is supported | |
4403 | * enable all necessary events for it. | |
4404 | */ | |
4405 | if (lmp_cpb_peripheral_capable(hdev)) { | |
4406 | events[2] |= 0x01; /* Synchronization Train Received */ | |
4407 | events[2] |= 0x02; /* CPB Receive */ | |
4408 | events[2] |= 0x04; /* CPB Timeout */ | |
0feb8af0 | 4409 | events[2] |= 0x10; /* Peripheral Page Response Timeout */ |
d0b13706 LAD |
4410 | changed = true; |
4411 | } | |
4412 | ||
4413 | /* Enable Authenticated Payload Timeout Expired event if supported */ | |
4414 | if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) { | |
4415 | events[2] |= 0x80; | |
4416 | changed = true; | |
4417 | } | |
4418 | ||
4419 | /* Some Broadcom based controllers indicate support for Set Event | |
4420 | * Mask Page 2 command, but then actually do not support it. Since | |
4421 | * the default value is all bits set to zero, the command is only | |
4422 | * required if the event mask has to be changed. In case no change | |
4423 | * to the event mask is needed, skip this command. | |
4424 | */ | |
4425 | if (!changed) | |
4426 | return 0; | |
4427 | ||
4428 | return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2, | |
4429 | sizeof(events), events, HCI_CMD_TIMEOUT); | |
4430 | } | |
4431 | ||
4432 | /* Read local codec list if the HCI command is supported */ | |
4433 | static int hci_read_local_codecs_sync(struct hci_dev *hdev) | |
4434 | { | |
828cea2b C |
4435 | if (hdev->commands[45] & 0x04) |
4436 | hci_read_supported_codecs_v2(hdev); | |
4437 | else if (hdev->commands[29] & 0x20) | |
4438 | hci_read_supported_codecs(hdev); | |
d0b13706 | 4439 | |
828cea2b | 4440 | return 0; |
d0b13706 LAD |
4441 | } |
4442 | ||
4443 | /* Read local pairing options if the HCI command is supported */ | |
4444 | static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev) | |
4445 | { | |
4446 | if (!(hdev->commands[41] & 0x08)) | |
4447 | return 0; | |
4448 | ||
4449 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS, | |
4450 | 0, NULL, HCI_CMD_TIMEOUT); | |
4451 | } | |
4452 | ||
4453 | /* Get MWS transport configuration if the HCI command is supported */ | |
4454 | static int hci_get_mws_transport_config_sync(struct hci_dev *hdev) | |
4455 | { | |
ffcb0a44 | 4456 | if (!mws_transport_config_capable(hdev)) |
d0b13706 LAD |
4457 | return 0; |
4458 | ||
4459 | return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG, | |
4460 | 0, NULL, HCI_CMD_TIMEOUT); | |
4461 | } | |
4462 | ||
4463 | /* Check for Synchronization Train support */ | |
4464 | static int hci_read_sync_train_params_sync(struct hci_dev *hdev) | |
4465 | { | |
4466 | if (!lmp_sync_train_capable(hdev)) | |
4467 | return 0; | |
4468 | ||
4469 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS, | |
4470 | 0, NULL, HCI_CMD_TIMEOUT); | |
4471 | } | |
4472 | ||
4473 | /* Enable Secure Connections if supported and configured */ | |
4474 | static int hci_write_sc_support_1_sync(struct hci_dev *hdev) | |
4475 | { | |
4476 | u8 support = 0x01; | |
4477 | ||
4478 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || | |
4479 | !bredr_sc_enabled(hdev)) | |
4480 | return 0; | |
4481 | ||
4482 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, | |
4483 | sizeof(support), &support, | |
4484 | HCI_CMD_TIMEOUT); | |
4485 | } | |
4486 | ||
4487 | /* Set erroneous data reporting if supported to the wideband speech | |
4488 | * setting value | |
4489 | */ | |
4490 | static int hci_set_err_data_report_sync(struct hci_dev *hdev) | |
4491 | { | |
4492 | struct hci_cp_write_def_err_data_reporting cp; | |
4493 | bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED); | |
4494 | ||
4495 | if (!(hdev->commands[18] & 0x08) || | |
42d7731e IFM |
4496 | !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) || |
4497 | test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks)) | |
d0b13706 LAD |
4498 | return 0; |
4499 | ||
4500 | if (enabled == hdev->err_data_reporting) | |
4501 | return 0; | |
4502 | ||
4503 | memset(&cp, 0, sizeof(cp)); | |
4504 | cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED : | |
4505 | ERR_DATA_REPORTING_DISABLED; | |
4506 | ||
4507 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING, | |
4508 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4509 | } | |
4510 | ||
4511 | static const struct hci_init_stage hci_init4[] = { | |
4512 | /* HCI_OP_DELETE_STORED_LINK_KEY */ | |
4513 | HCI_INIT(hci_delete_stored_link_key_sync), | |
4514 | /* HCI_OP_SET_EVENT_MASK_PAGE_2 */ | |
4515 | HCI_INIT(hci_set_event_mask_page_2_sync), | |
4516 | /* HCI_OP_READ_LOCAL_CODECS */ | |
4517 | HCI_INIT(hci_read_local_codecs_sync), | |
4518 | /* HCI_OP_READ_LOCAL_PAIRING_OPTS */ | |
4519 | HCI_INIT(hci_read_local_pairing_opts_sync), | |
4520 | /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */ | |
4521 | HCI_INIT(hci_get_mws_transport_config_sync), | |
4522 | /* HCI_OP_READ_SYNC_TRAIN_PARAMS */ | |
4523 | HCI_INIT(hci_read_sync_train_params_sync), | |
4524 | /* HCI_OP_WRITE_SC_SUPPORT */ | |
4525 | HCI_INIT(hci_write_sc_support_1_sync), | |
4526 | /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */ | |
4527 | HCI_INIT(hci_set_err_data_report_sync), | |
4528 | {} | |
4529 | }; | |
4530 | ||
4531 | /* Set Suggested Default Data Length to maximum if supported */ | |
4532 | static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev) | |
4533 | { | |
4534 | struct hci_cp_le_write_def_data_len cp; | |
4535 | ||
4536 | if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) | |
4537 | return 0; | |
4538 | ||
4539 | memset(&cp, 0, sizeof(cp)); | |
4540 | cp.tx_len = cpu_to_le16(hdev->le_max_tx_len); | |
4541 | cp.tx_time = cpu_to_le16(hdev->le_max_tx_time); | |
4542 | ||
4543 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN, | |
4544 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4545 | } | |
4546 | ||
288c9022 LAD |
4547 | /* Set Default PHY parameters if command is supported, enables all supported |
4548 | * PHYs according to the LE Features bits. | |
4549 | */ | |
d0b13706 LAD |
4550 | static int hci_le_set_default_phy_sync(struct hci_dev *hdev) |
4551 | { | |
4552 | struct hci_cp_le_set_default_phy cp; | |
4553 | ||
288c9022 LAD |
4554 | if (!(hdev->commands[35] & 0x20)) { |
4555 | /* If the command is not supported it means only 1M PHY is | |
4556 | * supported. | |
4557 | */ | |
4558 | hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M; | |
4559 | hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M; | |
d0b13706 | 4560 | return 0; |
288c9022 | 4561 | } |
d0b13706 LAD |
4562 | |
4563 | memset(&cp, 0, sizeof(cp)); | |
4564 | cp.all_phys = 0x00; | |
288c9022 LAD |
4565 | cp.tx_phys = HCI_LE_SET_PHY_1M; |
4566 | cp.rx_phys = HCI_LE_SET_PHY_1M; | |
4567 | ||
4568 | /* Enables 2M PHY if supported */ | |
4569 | if (le_2m_capable(hdev)) { | |
4570 | cp.tx_phys |= HCI_LE_SET_PHY_2M; | |
4571 | cp.rx_phys |= HCI_LE_SET_PHY_2M; | |
4572 | } | |
4573 | ||
4574 | /* Enables Coded PHY if supported */ | |
4575 | if (le_coded_capable(hdev)) { | |
4576 | cp.tx_phys |= HCI_LE_SET_PHY_CODED; | |
4577 | cp.rx_phys |= HCI_LE_SET_PHY_CODED; | |
4578 | } | |
d0b13706 LAD |
4579 | |
4580 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY, | |
4581 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4582 | } | |
4583 | ||
4584 | static const struct hci_init_stage le_init4[] = { | |
4585 | /* HCI_OP_LE_WRITE_DEF_DATA_LEN */ | |
4586 | HCI_INIT(hci_le_set_write_def_data_len_sync), | |
4587 | /* HCI_OP_LE_SET_DEFAULT_PHY */ | |
4588 | HCI_INIT(hci_le_set_default_phy_sync), | |
4589 | {} | |
4590 | }; | |
4591 | ||
4592 | static int hci_init4_sync(struct hci_dev *hdev) | |
4593 | { | |
4594 | int err; | |
4595 | ||
4596 | bt_dev_dbg(hdev, ""); | |
4597 | ||
4598 | err = hci_init_stage_sync(hdev, hci_init4); | |
4599 | if (err) | |
4600 | return err; | |
4601 | ||
4602 | if (lmp_le_capable(hdev)) | |
4603 | return hci_init_stage_sync(hdev, le_init4); | |
4604 | ||
4605 | return 0; | |
4606 | } | |
4607 | ||
4608 | static int hci_init_sync(struct hci_dev *hdev) | |
4609 | { | |
4610 | int err; | |
4611 | ||
4612 | err = hci_init1_sync(hdev); | |
4613 | if (err < 0) | |
4614 | return err; | |
4615 | ||
4616 | if (hci_dev_test_flag(hdev, HCI_SETUP)) | |
4617 | hci_debugfs_create_basic(hdev); | |
4618 | ||
4619 | err = hci_init2_sync(hdev); | |
4620 | if (err < 0) | |
4621 | return err; | |
4622 | ||
4623 | /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode | |
4624 | * BR/EDR/LE type controllers. AMP controllers only need the | |
4625 | * first two stages of init. | |
4626 | */ | |
4627 | if (hdev->dev_type != HCI_PRIMARY) | |
4628 | return 0; | |
4629 | ||
4630 | err = hci_init3_sync(hdev); | |
4631 | if (err < 0) | |
4632 | return err; | |
4633 | ||
4634 | err = hci_init4_sync(hdev); | |
4635 | if (err < 0) | |
4636 | return err; | |
4637 | ||
4638 | /* This function is only called when the controller is actually in | |
4639 | * configured state. When the controller is marked as unconfigured, | |
4640 | * this initialization procedure is not run. | |
4641 | * | |
4642 | * It means that it is possible that a controller runs through its | |
4643 | * setup phase and then discovers missing settings. If that is the | |
4644 | * case, then this function will not be called. It then will only | |
4645 | * be called during the config phase. | |
4646 | * | |
4647 | * So only when in setup phase or config phase, create the debugfs | |
4648 | * entries and register the SMP channels. | |
4649 | */ | |
4650 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
4651 | !hci_dev_test_flag(hdev, HCI_CONFIG)) | |
4652 | return 0; | |
4653 | ||
fe2ccc6c JH |
4654 | if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED)) |
4655 | return 0; | |
4656 | ||
d0b13706 LAD |
4657 | hci_debugfs_create_common(hdev); |
4658 | ||
4659 | if (lmp_bredr_capable(hdev)) | |
4660 | hci_debugfs_create_bredr(hdev); | |
4661 | ||
4662 | if (lmp_le_capable(hdev)) | |
4663 | hci_debugfs_create_le(hdev); | |
4664 | ||
4665 | return 0; | |
4666 | } | |
4667 | ||
6b5c1cda LAD |
4668 | #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc } |
4669 | ||
4670 | static const struct { | |
4671 | unsigned long quirk; | |
4672 | const char *desc; | |
4673 | } hci_broken_table[] = { | |
4674 | HCI_QUIRK_BROKEN(LOCAL_COMMANDS, | |
4675 | "HCI Read Local Supported Commands not supported"), | |
4676 | HCI_QUIRK_BROKEN(STORED_LINK_KEY, | |
4677 | "HCI Delete Stored Link Key command is advertised, " | |
4678 | "but not supported."), | |
42d7731e IFM |
4679 | HCI_QUIRK_BROKEN(ERR_DATA_REPORTING, |
4680 | "HCI Read Default Erroneous Data Reporting command is " | |
4681 | "advertised, but not supported."), | |
6b5c1cda LAD |
4682 | HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER, |
4683 | "HCI Read Transmit Power Level command is advertised, " | |
4684 | "but not supported."), | |
4685 | HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL, | |
4686 | "HCI Set Event Filter command not supported."), | |
4687 | HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN, | |
4688 | "HCI Enhanced Setup Synchronous Connection command is " | |
91b6d02d RC |
4689 | "advertised, but not supported."), |
4690 | HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT, | |
4691 | "HCI LE Set Random Private Address Timeout command is " | |
6b5c1cda LAD |
4692 | "advertised, but not supported.") |
4693 | }; | |
4694 | ||
1bbf4023 LAD |
4695 | /* This function handles hdev setup stage: |
4696 | * | |
4697 | * Calls hdev->setup | |
4698 | * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set. | |
4699 | */ | |
4700 | static int hci_dev_setup_sync(struct hci_dev *hdev) | |
d0b13706 LAD |
4701 | { |
4702 | int ret = 0; | |
1bbf4023 LAD |
4703 | bool invalid_bdaddr; |
4704 | size_t i; | |
d0b13706 LAD |
4705 | |
4706 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
1bbf4023 LAD |
4707 | !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) |
4708 | return 0; | |
d0b13706 | 4709 | |
1bbf4023 | 4710 | bt_dev_dbg(hdev, ""); |
d0b13706 | 4711 | |
1bbf4023 | 4712 | hci_sock_dev_event(hdev, HCI_DEV_SETUP); |
d0b13706 | 4713 | |
1bbf4023 LAD |
4714 | if (hdev->setup) |
4715 | ret = hdev->setup(hdev); | |
6b5c1cda | 4716 | |
1bbf4023 LAD |
4717 | for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) { |
4718 | if (test_bit(hci_broken_table[i].quirk, &hdev->quirks)) | |
4719 | bt_dev_warn(hdev, "%s", hci_broken_table[i].desc); | |
4720 | } | |
d0b13706 | 4721 | |
1bbf4023 LAD |
4722 | /* The transport driver can set the quirk to mark the |
4723 | * BD_ADDR invalid before creating the HCI device or in | |
4724 | * its setup callback. | |
4725 | */ | |
6945795b JH |
4726 | invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
4727 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); | |
1bbf4023 | 4728 | if (!ret) { |
0cb73658 JH |
4729 | if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) && |
4730 | !bacmp(&hdev->public_addr, BDADDR_ANY)) | |
4731 | hci_dev_get_bd_addr_from_property(hdev); | |
4732 | ||
6945795b | 4733 | if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) && |
0cb73658 JH |
4734 | hdev->set_bdaddr) { |
4735 | ret = hdev->set_bdaddr(hdev, &hdev->public_addr); | |
4736 | if (!ret) | |
4737 | invalid_bdaddr = false; | |
d0b13706 | 4738 | } |
1bbf4023 | 4739 | } |
d0b13706 | 4740 | |
1bbf4023 LAD |
4741 | /* The transport driver can set these quirks before |
4742 | * creating the HCI device or in its setup callback. | |
4743 | * | |
4744 | * For the invalid BD_ADDR quirk it is possible that | |
4745 | * it becomes a valid address if the bootloader does | |
4746 | * provide it (see above). | |
4747 | * | |
4748 | * In case any of them is set, the controller has to | |
4749 | * start up as unconfigured. | |
4750 | */ | |
4751 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || | |
4752 | invalid_bdaddr) | |
4753 | hci_dev_set_flag(hdev, HCI_UNCONFIGURED); | |
d0b13706 | 4754 | |
1bbf4023 LAD |
4755 | /* For an unconfigured controller it is required to |
4756 | * read at least the version information provided by | |
4757 | * the Read Local Version Information command. | |
4758 | * | |
4759 | * If the set_bdaddr driver callback is provided, then | |
4760 | * also the original Bluetooth public device address | |
4761 | * will be read using the Read BD Address command. | |
4762 | */ | |
4763 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
4764 | return hci_unconf_init_sync(hdev); | |
4765 | ||
4766 | return ret; | |
4767 | } | |
4768 | ||
4769 | /* This function handles hdev init stage: | |
4770 | * | |
4771 | * Calls hci_dev_setup_sync to perform setup stage | |
4772 | * Calls hci_init_sync to perform HCI command init sequence | |
4773 | */ | |
4774 | static int hci_dev_init_sync(struct hci_dev *hdev) | |
4775 | { | |
4776 | int ret; | |
4777 | ||
4778 | bt_dev_dbg(hdev, ""); | |
4779 | ||
4780 | atomic_set(&hdev->cmd_cnt, 1); | |
4781 | set_bit(HCI_INIT, &hdev->flags); | |
4782 | ||
4783 | ret = hci_dev_setup_sync(hdev); | |
d0b13706 LAD |
4784 | |
4785 | if (hci_dev_test_flag(hdev, HCI_CONFIG)) { | |
4786 | /* If public address change is configured, ensure that | |
4787 | * the address gets programmed. If the driver does not | |
4788 | * support changing the public address, fail the power | |
4789 | * on procedure. | |
4790 | */ | |
4791 | if (bacmp(&hdev->public_addr, BDADDR_ANY) && | |
4792 | hdev->set_bdaddr) | |
4793 | ret = hdev->set_bdaddr(hdev, &hdev->public_addr); | |
4794 | else | |
4795 | ret = -EADDRNOTAVAIL; | |
4796 | } | |
4797 | ||
4798 | if (!ret) { | |
4799 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && | |
4800 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { | |
4801 | ret = hci_init_sync(hdev); | |
4802 | if (!ret && hdev->post_init) | |
4803 | ret = hdev->post_init(hdev); | |
4804 | } | |
4805 | } | |
4806 | ||
4807 | /* If the HCI Reset command is clearing all diagnostic settings, | |
4808 | * then they need to be reprogrammed after the init procedure | |
4809 | * completed. | |
4810 | */ | |
4811 | if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && | |
4812 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
4813 | hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag) | |
4814 | ret = hdev->set_diag(hdev, true); | |
4815 | ||
385315de JM |
4816 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
4817 | msft_do_open(hdev); | |
4818 | aosp_do_open(hdev); | |
4819 | } | |
d0b13706 LAD |
4820 | |
4821 | clear_bit(HCI_INIT, &hdev->flags); | |
4822 | ||
1bbf4023 LAD |
4823 | return ret; |
4824 | } | |
4825 | ||
4826 | int hci_dev_open_sync(struct hci_dev *hdev) | |
4827 | { | |
4828 | int ret; | |
4829 | ||
4830 | bt_dev_dbg(hdev, ""); | |
4831 | ||
4832 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { | |
4833 | ret = -ENODEV; | |
4834 | goto done; | |
4835 | } | |
4836 | ||
4837 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
4838 | !hci_dev_test_flag(hdev, HCI_CONFIG)) { | |
4839 | /* Check for rfkill but allow the HCI setup stage to | |
4840 | * proceed (which in itself doesn't cause any RF activity). | |
4841 | */ | |
4842 | if (hci_dev_test_flag(hdev, HCI_RFKILLED)) { | |
4843 | ret = -ERFKILL; | |
4844 | goto done; | |
4845 | } | |
4846 | ||
4847 | /* Check for valid public address or a configured static | |
4848 | * random address, but let the HCI setup proceed to | |
4849 | * be able to determine if there is a public address | |
4850 | * or not. | |
4851 | * | |
4852 | * In case of user channel usage, it is not important | |
4853 | * if a public address or static random address is | |
4854 | * available. | |
4855 | * | |
4856 | * This check is only valid for BR/EDR controllers | |
4857 | * since AMP controllers do not have an address. | |
4858 | */ | |
4859 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
4860 | hdev->dev_type == HCI_PRIMARY && | |
4861 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && | |
4862 | !bacmp(&hdev->static_addr, BDADDR_ANY)) { | |
4863 | ret = -EADDRNOTAVAIL; | |
4864 | goto done; | |
4865 | } | |
4866 | } | |
4867 | ||
4868 | if (test_bit(HCI_UP, &hdev->flags)) { | |
4869 | ret = -EALREADY; | |
4870 | goto done; | |
4871 | } | |
4872 | ||
4873 | if (hdev->open(hdev)) { | |
4874 | ret = -EIO; | |
4875 | goto done; | |
4876 | } | |
4877 | ||
9695ef87 APS |
4878 | hci_devcd_reset(hdev); |
4879 | ||
1bbf4023 LAD |
4880 | set_bit(HCI_RUNNING, &hdev->flags); |
4881 | hci_sock_dev_event(hdev, HCI_DEV_OPEN); | |
4882 | ||
4883 | ret = hci_dev_init_sync(hdev); | |
d0b13706 LAD |
4884 | if (!ret) { |
4885 | hci_dev_hold(hdev); | |
4886 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); | |
4887 | hci_adv_instances_set_rpa_expired(hdev, true); | |
4888 | set_bit(HCI_UP, &hdev->flags); | |
4889 | hci_sock_dev_event(hdev, HCI_DEV_UP); | |
4890 | hci_leds_update_powered(hdev, true); | |
4891 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
4892 | !hci_dev_test_flag(hdev, HCI_CONFIG) && | |
4893 | !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && | |
4894 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
4895 | hci_dev_test_flag(hdev, HCI_MGMT) && | |
4896 | hdev->dev_type == HCI_PRIMARY) { | |
4897 | ret = hci_powered_update_sync(hdev); | |
6abf0dae | 4898 | mgmt_power_on(hdev, ret); |
d0b13706 LAD |
4899 | } |
4900 | } else { | |
4901 | /* Init failed, cleanup */ | |
4902 | flush_work(&hdev->tx_work); | |
4903 | ||
4904 | /* Since hci_rx_work() is possible to awake new cmd_work | |
4905 | * it should be flushed first to avoid unexpected call of | |
4906 | * hci_cmd_work() | |
4907 | */ | |
4908 | flush_work(&hdev->rx_work); | |
4909 | flush_work(&hdev->cmd_work); | |
4910 | ||
4911 | skb_queue_purge(&hdev->cmd_q); | |
4912 | skb_queue_purge(&hdev->rx_q); | |
4913 | ||
4914 | if (hdev->flush) | |
4915 | hdev->flush(hdev); | |
4916 | ||
4917 | if (hdev->sent_cmd) { | |
97dfaf07 | 4918 | cancel_delayed_work_sync(&hdev->cmd_timer); |
d0b13706 LAD |
4919 | kfree_skb(hdev->sent_cmd); |
4920 | hdev->sent_cmd = NULL; | |
4921 | } | |
4922 | ||
4923 | clear_bit(HCI_RUNNING, &hdev->flags); | |
4924 | hci_sock_dev_event(hdev, HCI_DEV_CLOSE); | |
4925 | ||
4926 | hdev->close(hdev); | |
4927 | hdev->flags &= BIT(HCI_RAW); | |
4928 | } | |
4929 | ||
4930 | done: | |
4931 | return ret; | |
4932 | } | |
4933 | ||
4934 | /* This function requires the caller holds hdev->lock */ | |
4935 | static void hci_pend_le_actions_clear(struct hci_dev *hdev) | |
4936 | { | |
4937 | struct hci_conn_params *p; | |
4938 | ||
4939 | list_for_each_entry(p, &hdev->le_conn_params, list) { | |
195ef75e | 4940 | hci_pend_le_list_del_init(p); |
d0b13706 LAD |
4941 | if (p->conn) { |
4942 | hci_conn_drop(p->conn); | |
4943 | hci_conn_put(p->conn); | |
4944 | p->conn = NULL; | |
4945 | } | |
d0b13706 LAD |
4946 | } |
4947 | ||
4948 | BT_DBG("All LE pending actions cleared"); | |
4949 | } | |
4950 | ||
8dbc3e75 APS |
4951 | static int hci_dev_shutdown(struct hci_dev *hdev) |
4952 | { | |
4953 | int err = 0; | |
4954 | /* Similar to how we first do setup and then set the exclusive access | |
4955 | * bit for userspace, we must first unset userchannel and then clean up. | |
4956 | * Otherwise, the kernel can't properly use the hci channel to clean up | |
4957 | * the controller (some shutdown routines require sending additional | |
4958 | * commands to the controller for example). | |
4959 | */ | |
4960 | bool was_userchannel = | |
4961 | hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL); | |
4962 | ||
4963 | if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && | |
4964 | test_bit(HCI_UP, &hdev->flags)) { | |
4965 | /* Execute vendor specific shutdown routine */ | |
4966 | if (hdev->shutdown) | |
4967 | err = hdev->shutdown(hdev); | |
4968 | } | |
4969 | ||
4970 | if (was_userchannel) | |
4971 | hci_dev_set_flag(hdev, HCI_USER_CHANNEL); | |
4972 | ||
4973 | return err; | |
4974 | } | |
4975 | ||
d0b13706 LAD |
4976 | int hci_dev_close_sync(struct hci_dev *hdev) |
4977 | { | |
4978 | bool auto_off; | |
4979 | int err = 0; | |
4980 | ||
4981 | bt_dev_dbg(hdev, ""); | |
4982 | ||
4983 | cancel_delayed_work(&hdev->power_off); | |
4984 | cancel_delayed_work(&hdev->ncmd_timer); | |
8ffde2a7 | 4985 | cancel_delayed_work(&hdev->le_scan_disable); |
27d54b77 | 4986 | cancel_delayed_work(&hdev->le_scan_restart); |
d0b13706 LAD |
4987 | |
4988 | hci_request_cancel_all(hdev); | |
4989 | ||
c249ea9b BG |
4990 | if (hdev->adv_instance_timeout) { |
4991 | cancel_delayed_work_sync(&hdev->adv_instance_expire); | |
4992 | hdev->adv_instance_timeout = 0; | |
4993 | } | |
4994 | ||
8dbc3e75 | 4995 | err = hci_dev_shutdown(hdev); |
d0b13706 LAD |
4996 | |
4997 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { | |
4998 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
4999 | return err; | |
5000 | } | |
5001 | ||
5002 | hci_leds_update_powered(hdev, false); | |
5003 | ||
5004 | /* Flush RX and TX works */ | |
5005 | flush_work(&hdev->tx_work); | |
5006 | flush_work(&hdev->rx_work); | |
5007 | ||
5008 | if (hdev->discov_timeout > 0) { | |
5009 | hdev->discov_timeout = 0; | |
5010 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
5011 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); | |
5012 | } | |
5013 | ||
5014 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) | |
5015 | cancel_delayed_work(&hdev->service_cache); | |
5016 | ||
5017 | if (hci_dev_test_flag(hdev, HCI_MGMT)) { | |
5018 | struct adv_info *adv_instance; | |
5019 | ||
5020 | cancel_delayed_work_sync(&hdev->rpa_expired); | |
5021 | ||
5022 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) | |
5023 | cancel_delayed_work_sync(&adv_instance->rpa_expired_cb); | |
5024 | } | |
5025 | ||
5026 | /* Avoid potential lockdep warnings from the *_flush() calls by | |
5027 | * ensuring the workqueue is empty up front. | |
5028 | */ | |
5029 | drain_workqueue(hdev->workqueue); | |
5030 | ||
5031 | hci_dev_lock(hdev); | |
5032 | ||
5033 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); | |
5034 | ||
5035 | auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); | |
5036 | ||
5037 | if (!auto_off && hdev->dev_type == HCI_PRIMARY && | |
5038 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
5039 | hci_dev_test_flag(hdev, HCI_MGMT)) | |
5040 | __mgmt_power_off(hdev); | |
5041 | ||
5042 | hci_inquiry_cache_flush(hdev); | |
5043 | hci_pend_le_actions_clear(hdev); | |
5044 | hci_conn_hash_flush(hdev); | |
fa78d2d1 | 5045 | /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */ |
d0b13706 | 5046 | smp_unregister(hdev); |
fa78d2d1 | 5047 | hci_dev_unlock(hdev); |
d0b13706 LAD |
5048 | |
5049 | hci_sock_dev_event(hdev, HCI_DEV_DOWN); | |
5050 | ||
385315de JM |
5051 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
5052 | aosp_do_close(hdev); | |
5053 | msft_do_close(hdev); | |
5054 | } | |
d0b13706 LAD |
5055 | |
5056 | if (hdev->flush) | |
5057 | hdev->flush(hdev); | |
5058 | ||
5059 | /* Reset device */ | |
5060 | skb_queue_purge(&hdev->cmd_q); | |
5061 | atomic_set(&hdev->cmd_cnt, 1); | |
5062 | if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) && | |
5063 | !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { | |
5064 | set_bit(HCI_INIT, &hdev->flags); | |
5065 | hci_reset_sync(hdev); | |
5066 | clear_bit(HCI_INIT, &hdev->flags); | |
5067 | } | |
5068 | ||
5069 | /* flush cmd work */ | |
5070 | flush_work(&hdev->cmd_work); | |
5071 | ||
5072 | /* Drop queues */ | |
5073 | skb_queue_purge(&hdev->rx_q); | |
5074 | skb_queue_purge(&hdev->cmd_q); | |
5075 | skb_queue_purge(&hdev->raw_q); | |
5076 | ||
5077 | /* Drop last sent command */ | |
5078 | if (hdev->sent_cmd) { | |
5079 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
5080 | kfree_skb(hdev->sent_cmd); | |
5081 | hdev->sent_cmd = NULL; | |
5082 | } | |
5083 | ||
5084 | clear_bit(HCI_RUNNING, &hdev->flags); | |
5085 | hci_sock_dev_event(hdev, HCI_DEV_CLOSE); | |
5086 | ||
d0b13706 LAD |
5087 | /* After this point our queues are empty and no tasks are scheduled. */ |
5088 | hdev->close(hdev); | |
5089 | ||
5090 | /* Clear flags */ | |
5091 | hdev->flags &= BIT(HCI_RAW); | |
5092 | hci_dev_clear_volatile_flags(hdev); | |
5093 | ||
5094 | /* Controller radio is available but is currently powered down */ | |
5095 | hdev->amp_status = AMP_STATUS_POWERED_DOWN; | |
5096 | ||
5097 | memset(hdev->eir, 0, sizeof(hdev->eir)); | |
5098 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); | |
5099 | bacpy(&hdev->random_addr, BDADDR_ANY); | |
5100 | ||
5101 | hci_dev_put(hdev); | |
5102 | return err; | |
5103 | } | |
5104 | ||
5105 | /* This function perform power on HCI command sequence as follows: | |
5106 | * | |
5107 | * If controller is already up (HCI_UP) performs hci_powered_update_sync | |
5108 | * sequence otherwise run hci_dev_open_sync which will follow with | |
5109 | * hci_powered_update_sync after the init sequence is completed. | |
5110 | */ | |
5111 | static int hci_power_on_sync(struct hci_dev *hdev) | |
5112 | { | |
5113 | int err; | |
5114 | ||
5115 | if (test_bit(HCI_UP, &hdev->flags) && | |
5116 | hci_dev_test_flag(hdev, HCI_MGMT) && | |
5117 | hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { | |
5118 | cancel_delayed_work(&hdev->power_off); | |
5119 | return hci_powered_update_sync(hdev); | |
5120 | } | |
5121 | ||
5122 | err = hci_dev_open_sync(hdev); | |
5123 | if (err < 0) | |
5124 | return err; | |
5125 | ||
5126 | /* During the HCI setup phase, a few error conditions are | |
5127 | * ignored and they need to be checked now. If they are still | |
5128 | * valid, it is important to return the device back off. | |
5129 | */ | |
5130 | if (hci_dev_test_flag(hdev, HCI_RFKILLED) || | |
5131 | hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || | |
5132 | (hdev->dev_type == HCI_PRIMARY && | |
5133 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && | |
5134 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
5135 | hci_dev_clear_flag(hdev, HCI_AUTO_OFF); | |
5136 | hci_dev_close_sync(hdev); | |
5137 | } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { | |
5138 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, | |
5139 | HCI_AUTO_OFF_TIMEOUT); | |
5140 | } | |
5141 | ||
5142 | if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { | |
5143 | /* For unconfigured devices, set the HCI_RAW flag | |
5144 | * so that userspace can easily identify them. | |
5145 | */ | |
5146 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
5147 | set_bit(HCI_RAW, &hdev->flags); | |
5148 | ||
5149 | /* For fully configured devices, this will send | |
5150 | * the Index Added event. For unconfigured devices, | |
5151 | * it will send Unconfigued Index Added event. | |
5152 | * | |
5153 | * Devices with HCI_QUIRK_RAW_DEVICE are ignored | |
5154 | * and no event will be send. | |
5155 | */ | |
5156 | mgmt_index_added(hdev); | |
5157 | } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { | |
5158 | /* When the controller is now configured, then it | |
5159 | * is important to clear the HCI_RAW flag. | |
5160 | */ | |
5161 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
5162 | clear_bit(HCI_RAW, &hdev->flags); | |
5163 | ||
5164 | /* Powering on the controller with HCI_CONFIG set only | |
5165 | * happens with the transition from unconfigured to | |
5166 | * configured. This will send the Index Added event. | |
5167 | */ | |
5168 | mgmt_index_added(hdev); | |
5169 | } | |
5170 | ||
5171 | return 0; | |
5172 | } | |
5173 | ||
5174 | static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr) | |
5175 | { | |
5176 | struct hci_cp_remote_name_req_cancel cp; | |
5177 | ||
5178 | memset(&cp, 0, sizeof(cp)); | |
5179 | bacpy(&cp.bdaddr, addr); | |
5180 | ||
5181 | return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, | |
5182 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
5183 | } | |
5184 | ||
5185 | int hci_stop_discovery_sync(struct hci_dev *hdev) | |
5186 | { | |
5187 | struct discovery_state *d = &hdev->discovery; | |
5188 | struct inquiry_entry *e; | |
5189 | int err; | |
5190 | ||
5191 | bt_dev_dbg(hdev, "state %u", hdev->discovery.state); | |
5192 | ||
5193 | if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) { | |
5194 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { | |
5195 | err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, | |
5196 | 0, NULL, HCI_CMD_TIMEOUT); | |
5197 | if (err) | |
5198 | return err; | |
5199 | } | |
5200 | ||
5201 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { | |
5202 | cancel_delayed_work(&hdev->le_scan_disable); | |
5203 | cancel_delayed_work(&hdev->le_scan_restart); | |
5204 | ||
5205 | err = hci_scan_disable_sync(hdev); | |
5206 | if (err) | |
5207 | return err; | |
5208 | } | |
5209 | ||
5210 | } else { | |
5211 | err = hci_scan_disable_sync(hdev); | |
5212 | if (err) | |
5213 | return err; | |
5214 | } | |
5215 | ||
5216 | /* Resume advertising if it was paused */ | |
5217 | if (use_ll_privacy(hdev)) | |
5218 | hci_resume_advertising_sync(hdev); | |
5219 | ||
5220 | /* No further actions needed for LE-only discovery */ | |
5221 | if (d->type == DISCOV_TYPE_LE) | |
5222 | return 0; | |
5223 | ||
5224 | if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) { | |
5225 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, | |
5226 | NAME_PENDING); | |
5227 | if (!e) | |
5228 | return 0; | |
5229 | ||
5230 | return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr); | |
5231 | } | |
5232 | ||
5233 | return 0; | |
5234 | } | |
5235 | ||
5236 | static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle, | |
5237 | u8 reason) | |
5238 | { | |
5239 | struct hci_cp_disconn_phy_link cp; | |
5240 | ||
5241 | memset(&cp, 0, sizeof(cp)); | |
5242 | cp.phy_handle = HCI_PHY_HANDLE(handle); | |
5243 | cp.reason = reason; | |
5244 | ||
5245 | return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK, | |
5246 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
5247 | } | |
5248 | ||
5249 | static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
5250 | u8 reason) | |
5251 | { | |
5252 | struct hci_cp_disconnect cp; | |
5253 | ||
5254 | if (conn->type == AMP_LINK) | |
5255 | return hci_disconnect_phy_link_sync(hdev, conn->handle, reason); | |
5256 | ||
5257 | memset(&cp, 0, sizeof(cp)); | |
5258 | cp.handle = cpu_to_le16(conn->handle); | |
5259 | cp.reason = reason; | |
5260 | ||
f5d13029 AP |
5261 | /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the |
5262 | * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is | |
5263 | * used when suspending or powering off, where we don't want to wait | |
5264 | * for the peer's response. | |
d0b13706 | 5265 | */ |
f5d13029 | 5266 | if (reason != HCI_ERROR_REMOTE_POWER_OFF) |
d0b13706 LAD |
5267 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT, |
5268 | sizeof(cp), &cp, | |
5269 | HCI_EV_DISCONN_COMPLETE, | |
5270 | HCI_CMD_TIMEOUT, NULL); | |
5271 | ||
5272 | return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp, | |
5273 | HCI_CMD_TIMEOUT); | |
5274 | } | |
5275 | ||
5276 | static int hci_le_connect_cancel_sync(struct hci_dev *hdev, | |
a13f316e | 5277 | struct hci_conn *conn, u8 reason) |
d0b13706 | 5278 | { |
a13f316e LAD |
5279 | /* Return reason if scanning since the connection shall probably be |
5280 | * cleanup directly. | |
5281 | */ | |
d0b13706 | 5282 | if (test_bit(HCI_CONN_SCANNING, &conn->flags)) |
a13f316e | 5283 | return reason; |
d0b13706 | 5284 | |
a13f316e LAD |
5285 | if (conn->role == HCI_ROLE_SLAVE || |
5286 | test_and_set_bit(HCI_CONN_CANCEL, &conn->flags)) | |
b62e7220 LAD |
5287 | return 0; |
5288 | ||
d0b13706 | 5289 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL, |
b62e7220 | 5290 | 0, NULL, HCI_CMD_TIMEOUT); |
d0b13706 LAD |
5291 | } |
5292 | ||
a13f316e LAD |
5293 | static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn, |
5294 | u8 reason) | |
d0b13706 LAD |
5295 | { |
5296 | if (conn->type == LE_LINK) | |
a13f316e | 5297 | return hci_le_connect_cancel_sync(hdev, conn, reason); |
d0b13706 | 5298 | |
04a51d61 LAD |
5299 | if (conn->type == ISO_LINK) { |
5300 | /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E | |
5301 | * page 1857: | |
5302 | * | |
5303 | * If this command is issued for a CIS on the Central and the | |
5304 | * CIS is successfully terminated before being established, | |
5305 | * then an HCI_LE_CIS_Established event shall also be sent for | |
5306 | * this CIS with the Status Operation Cancelled by Host (0x44). | |
5307 | */ | |
5308 | if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) | |
5309 | return hci_disconnect_sync(hdev, conn, reason); | |
5310 | ||
5311 | /* There is no way to cancel a BIS without terminating the BIG | |
5312 | * which is done later on connection cleanup. | |
5313 | */ | |
5314 | return 0; | |
5315 | } | |
5316 | ||
d0b13706 LAD |
5317 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
5318 | return 0; | |
5319 | ||
5320 | return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL, | |
5321 | 6, &conn->dst, HCI_CMD_TIMEOUT); | |
5322 | } | |
5323 | ||
5324 | static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
5325 | u8 reason) | |
5326 | { | |
5327 | struct hci_cp_reject_sync_conn_req cp; | |
5328 | ||
5329 | memset(&cp, 0, sizeof(cp)); | |
5330 | bacpy(&cp.bdaddr, &conn->dst); | |
5331 | cp.reason = reason; | |
5332 | ||
5333 | /* SCO rejection has its own limited set of | |
5334 | * allowed error values (0x0D-0x0F). | |
5335 | */ | |
5336 | if (reason < 0x0d || reason > 0x0f) | |
5337 | cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; | |
5338 | ||
5339 | return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ, | |
5340 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
5341 | } | |
5342 | ||
04a51d61 LAD |
5343 | static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn, |
5344 | u8 reason) | |
5345 | { | |
5346 | struct hci_cp_le_reject_cis cp; | |
5347 | ||
5348 | memset(&cp, 0, sizeof(cp)); | |
5349 | cp.handle = cpu_to_le16(conn->handle); | |
5350 | cp.reason = reason; | |
5351 | ||
5352 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS, | |
5353 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
5354 | } | |
5355 | ||
d0b13706 LAD |
5356 | static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, |
5357 | u8 reason) | |
5358 | { | |
5359 | struct hci_cp_reject_conn_req cp; | |
5360 | ||
04a51d61 LAD |
5361 | if (conn->type == ISO_LINK) |
5362 | return hci_le_reject_cis_sync(hdev, conn, reason); | |
5363 | ||
d0b13706 LAD |
5364 | if (conn->type == SCO_LINK || conn->type == ESCO_LINK) |
5365 | return hci_reject_sco_sync(hdev, conn, reason); | |
5366 | ||
5367 | memset(&cp, 0, sizeof(cp)); | |
5368 | bacpy(&cp.bdaddr, &conn->dst); | |
5369 | cp.reason = reason; | |
5370 | ||
5371 | return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ, | |
5372 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
5373 | } | |
5374 | ||
1f7435c8 | 5375 | int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason) |
d0b13706 | 5376 | { |
9b3628d7 LAD |
5377 | int err; |
5378 | ||
d0b13706 LAD |
5379 | switch (conn->state) { |
5380 | case BT_CONNECTED: | |
5381 | case BT_CONFIG: | |
5382 | return hci_disconnect_sync(hdev, conn, reason); | |
5383 | case BT_CONNECT: | |
a13f316e | 5384 | err = hci_connect_cancel_sync(hdev, conn, reason); |
9b3628d7 | 5385 | /* Cleanup hci_conn object if it cannot be cancelled as it |
a13f316e LAD |
5386 | * likelly means the controller and host stack are out of sync |
5387 | * or in case of LE it was still scanning so it can be cleanup | |
5388 | * safely. | |
9b3628d7 | 5389 | */ |
b8288548 ZJ |
5390 | if (err) { |
5391 | hci_dev_lock(hdev); | |
9b3628d7 | 5392 | hci_conn_failed(conn, err); |
b8288548 ZJ |
5393 | hci_dev_unlock(hdev); |
5394 | } | |
9b3628d7 | 5395 | return err; |
d0b13706 LAD |
5396 | case BT_CONNECT2: |
5397 | return hci_reject_conn_sync(hdev, conn, reason); | |
f777d882 IT |
5398 | case BT_OPEN: |
5399 | /* Cleanup bises that failed to be established */ | |
5400 | if (test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags)) { | |
5401 | hci_dev_lock(hdev); | |
5402 | hci_conn_failed(conn, reason); | |
5403 | hci_dev_unlock(hdev); | |
5404 | } | |
5405 | break; | |
d0b13706 LAD |
5406 | default: |
5407 | conn->state = BT_CLOSED; | |
5408 | break; | |
5409 | } | |
5410 | ||
5411 | return 0; | |
5412 | } | |
5413 | ||
182ee45d LAD |
5414 | static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason) |
5415 | { | |
5416 | struct hci_conn *conn, *tmp; | |
5417 | int err; | |
5418 | ||
5419 | list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) { | |
5420 | err = hci_abort_conn_sync(hdev, conn, reason); | |
5421 | if (err) | |
5422 | return err; | |
5423 | } | |
5424 | ||
8cd3c55c | 5425 | return 0; |
182ee45d LAD |
5426 | } |
5427 | ||
d0b13706 LAD |
5428 | /* This function perform power off HCI command sequence as follows: |
5429 | * | |
5430 | * Clear Advertising | |
5431 | * Stop Discovery | |
5432 | * Disconnect all connections | |
5433 | * hci_dev_close_sync | |
5434 | */ | |
5435 | static int hci_power_off_sync(struct hci_dev *hdev) | |
5436 | { | |
d0b13706 LAD |
5437 | int err; |
5438 | ||
5439 | /* If controller is already down there is nothing to do */ | |
5440 | if (!test_bit(HCI_UP, &hdev->flags)) | |
5441 | return 0; | |
5442 | ||
5443 | if (test_bit(HCI_ISCAN, &hdev->flags) || | |
5444 | test_bit(HCI_PSCAN, &hdev->flags)) { | |
5445 | err = hci_write_scan_enable_sync(hdev, 0x00); | |
5446 | if (err) | |
5447 | return err; | |
5448 | } | |
5449 | ||
5450 | err = hci_clear_adv_sync(hdev, NULL, false); | |
5451 | if (err) | |
5452 | return err; | |
5453 | ||
5454 | err = hci_stop_discovery_sync(hdev); | |
5455 | if (err) | |
5456 | return err; | |
5457 | ||
182ee45d LAD |
5458 | /* Terminated due to Power Off */ |
5459 | err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); | |
5460 | if (err) | |
5461 | return err; | |
d0b13706 LAD |
5462 | |
5463 | return hci_dev_close_sync(hdev); | |
5464 | } | |
5465 | ||
5466 | int hci_set_powered_sync(struct hci_dev *hdev, u8 val) | |
5467 | { | |
5468 | if (val) | |
5469 | return hci_power_on_sync(hdev); | |
cf75ad8b LAD |
5470 | |
5471 | return hci_power_off_sync(hdev); | |
5472 | } | |
abfeea47 | 5473 | |
2bd1b237 LAD |
5474 | static int hci_write_iac_sync(struct hci_dev *hdev) |
5475 | { | |
5476 | struct hci_cp_write_current_iac_lap cp; | |
5477 | ||
5478 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) | |
5479 | return 0; | |
5480 | ||
5481 | memset(&cp, 0, sizeof(cp)); | |
5482 | ||
5483 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { | |
5484 | /* Limited discoverable mode */ | |
5485 | cp.num_iac = min_t(u8, hdev->num_iac, 2); | |
5486 | cp.iac_lap[0] = 0x00; /* LIAC */ | |
5487 | cp.iac_lap[1] = 0x8b; | |
5488 | cp.iac_lap[2] = 0x9e; | |
5489 | cp.iac_lap[3] = 0x33; /* GIAC */ | |
5490 | cp.iac_lap[4] = 0x8b; | |
5491 | cp.iac_lap[5] = 0x9e; | |
5492 | } else { | |
5493 | /* General discoverable mode */ | |
5494 | cp.num_iac = 1; | |
5495 | cp.iac_lap[0] = 0x33; /* GIAC */ | |
5496 | cp.iac_lap[1] = 0x8b; | |
5497 | cp.iac_lap[2] = 0x9e; | |
5498 | } | |
5499 | ||
5500 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP, | |
5501 | (cp.num_iac * 3) + 1, &cp, | |
5502 | HCI_CMD_TIMEOUT); | |
5503 | } | |
5504 | ||
5505 | int hci_update_discoverable_sync(struct hci_dev *hdev) | |
5506 | { | |
5507 | int err = 0; | |
5508 | ||
5509 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { | |
5510 | err = hci_write_iac_sync(hdev); | |
5511 | if (err) | |
5512 | return err; | |
5513 | ||
5514 | err = hci_update_scan_sync(hdev); | |
5515 | if (err) | |
5516 | return err; | |
5517 | ||
5518 | err = hci_update_class_sync(hdev); | |
5519 | if (err) | |
5520 | return err; | |
5521 | } | |
5522 | ||
5523 | /* Advertising instances don't use the global discoverable setting, so | |
5524 | * only update AD if advertising was enabled using Set Advertising. | |
5525 | */ | |
5526 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) { | |
5527 | err = hci_update_adv_data_sync(hdev, 0x00); | |
5528 | if (err) | |
5529 | return err; | |
5530 | ||
5531 | /* Discoverable mode affects the local advertising | |
5532 | * address in limited privacy mode. | |
5533 | */ | |
5534 | if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) { | |
5535 | if (ext_adv_capable(hdev)) | |
5536 | err = hci_start_ext_adv_sync(hdev, 0x00); | |
5537 | else | |
5538 | err = hci_enable_advertising_sync(hdev); | |
5539 | } | |
5540 | } | |
5541 | ||
5542 | return err; | |
5543 | } | |
5544 | ||
5545 | static int update_discoverable_sync(struct hci_dev *hdev, void *data) | |
5546 | { | |
5547 | return hci_update_discoverable_sync(hdev); | |
5548 | } | |
5549 | ||
5550 | int hci_update_discoverable(struct hci_dev *hdev) | |
5551 | { | |
5552 | /* Only queue if it would have any effect */ | |
5553 | if (hdev_is_powered(hdev) && | |
5554 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && | |
5555 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && | |
5556 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) | |
5557 | return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL, | |
5558 | NULL); | |
5559 | ||
5560 | return 0; | |
5561 | } | |
5562 | ||
f056a657 LAD |
5563 | int hci_update_connectable_sync(struct hci_dev *hdev) |
5564 | { | |
5565 | int err; | |
5566 | ||
5567 | err = hci_update_scan_sync(hdev); | |
5568 | if (err) | |
5569 | return err; | |
5570 | ||
5571 | /* If BR/EDR is not enabled and we disable advertising as a | |
5572 | * by-product of disabling connectable, we need to update the | |
5573 | * advertising flags. | |
5574 | */ | |
5575 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
5576 | err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance); | |
5577 | ||
5578 | /* Update the advertising parameters if necessary */ | |
5579 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || | |
5580 | !list_empty(&hdev->adv_instances)) { | |
5581 | if (ext_adv_capable(hdev)) | |
5582 | err = hci_start_ext_adv_sync(hdev, | |
5583 | hdev->cur_adv_instance); | |
5584 | else | |
5585 | err = hci_enable_advertising_sync(hdev); | |
5586 | ||
5587 | if (err) | |
5588 | return err; | |
5589 | } | |
5590 | ||
5591 | return hci_update_passive_scan_sync(hdev); | |
5592 | } | |
5593 | ||
abfeea47 LAD |
5594 | static int hci_inquiry_sync(struct hci_dev *hdev, u8 length) |
5595 | { | |
5596 | const u8 giac[3] = { 0x33, 0x8b, 0x9e }; | |
5597 | const u8 liac[3] = { 0x00, 0x8b, 0x9e }; | |
5598 | struct hci_cp_inquiry cp; | |
5599 | ||
5600 | bt_dev_dbg(hdev, ""); | |
5601 | ||
5602 | if (hci_dev_test_flag(hdev, HCI_INQUIRY)) | |
5603 | return 0; | |
5604 | ||
5605 | hci_dev_lock(hdev); | |
5606 | hci_inquiry_cache_flush(hdev); | |
5607 | hci_dev_unlock(hdev); | |
5608 | ||
5609 | memset(&cp, 0, sizeof(cp)); | |
5610 | ||
5611 | if (hdev->discovery.limited) | |
5612 | memcpy(&cp.lap, liac, sizeof(cp.lap)); | |
5613 | else | |
5614 | memcpy(&cp.lap, giac, sizeof(cp.lap)); | |
5615 | ||
5616 | cp.length = length; | |
5617 | ||
5618 | return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY, | |
5619 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
5620 | } | |
5621 | ||
5622 | static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval) | |
5623 | { | |
5624 | u8 own_addr_type; | |
5625 | /* Accept list is not used for discovery */ | |
5626 | u8 filter_policy = 0x00; | |
5627 | /* Default is to enable duplicates filter */ | |
5628 | u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; | |
5629 | int err; | |
5630 | ||
5631 | bt_dev_dbg(hdev, ""); | |
5632 | ||
5633 | /* If controller is scanning, it means the passive scanning is | |
5634 | * running. Thus, we should temporarily stop it in order to set the | |
5635 | * discovery scanning parameters. | |
5636 | */ | |
5637 | err = hci_scan_disable_sync(hdev); | |
5638 | if (err) { | |
5639 | bt_dev_err(hdev, "Unable to disable scanning: %d", err); | |
5640 | return err; | |
5641 | } | |
5642 | ||
5643 | cancel_interleave_scan(hdev); | |
5644 | ||
3c44a431 ZJ |
5645 | /* Pause address resolution for active scan and stop advertising if |
5646 | * privacy is enabled. | |
abfeea47 | 5647 | */ |
3c44a431 ZJ |
5648 | err = hci_pause_addr_resolution(hdev); |
5649 | if (err) | |
abfeea47 | 5650 | goto failed; |
abfeea47 LAD |
5651 | |
5652 | /* All active scans will be done with either a resolvable private | |
5653 | * address (when privacy feature has been enabled) or non-resolvable | |
5654 | * private address. | |
5655 | */ | |
5656 | err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev), | |
5657 | &own_addr_type); | |
5658 | if (err < 0) | |
5659 | own_addr_type = ADDR_LE_DEV_PUBLIC; | |
5660 | ||
5661 | if (hci_is_adv_monitoring(hdev)) { | |
5662 | /* Duplicate filter should be disabled when some advertisement | |
5663 | * monitor is activated, otherwise AdvMon can only receive one | |
5664 | * advertisement for one peer(*) during active scanning, and | |
5665 | * might report loss to these peers. | |
5666 | * | |
5667 | * Note that different controllers have different meanings of | |
5668 | * |duplicate|. Some of them consider packets with the same | |
5669 | * address as duplicate, and others consider packets with the | |
5670 | * same address and the same RSSI as duplicate. Although in the | |
5671 | * latter case we don't need to disable duplicate filter, but | |
5672 | * it is common to have active scanning for a short period of | |
5673 | * time, the power impact should be neglectable. | |
5674 | */ | |
5675 | filter_dup = LE_SCAN_FILTER_DUP_DISABLE; | |
5676 | } | |
5677 | ||
5678 | err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval, | |
5679 | hdev->le_scan_window_discovery, | |
5680 | own_addr_type, filter_policy, filter_dup); | |
5681 | if (!err) | |
5682 | return err; | |
5683 | ||
5684 | failed: | |
5685 | /* Resume advertising if it was paused */ | |
5686 | if (use_ll_privacy(hdev)) | |
5687 | hci_resume_advertising_sync(hdev); | |
5688 | ||
5689 | /* Resume passive scanning */ | |
5690 | hci_update_passive_scan_sync(hdev); | |
5691 | return err; | |
5692 | } | |
5693 | ||
5694 | static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev) | |
5695 | { | |
5696 | int err; | |
5697 | ||
5698 | bt_dev_dbg(hdev, ""); | |
5699 | ||
5700 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2); | |
5701 | if (err) | |
5702 | return err; | |
5703 | ||
5704 | return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); | |
5705 | } | |
5706 | ||
5707 | int hci_start_discovery_sync(struct hci_dev *hdev) | |
5708 | { | |
5709 | unsigned long timeout; | |
5710 | int err; | |
5711 | ||
5712 | bt_dev_dbg(hdev, "type %u", hdev->discovery.type); | |
5713 | ||
5714 | switch (hdev->discovery.type) { | |
5715 | case DISCOV_TYPE_BREDR: | |
5716 | return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); | |
5717 | case DISCOV_TYPE_INTERLEAVED: | |
5718 | /* When running simultaneous discovery, the LE scanning time | |
5719 | * should occupy the whole discovery time sine BR/EDR inquiry | |
5720 | * and LE scanning are scheduled by the controller. | |
5721 | * | |
5722 | * For interleaving discovery in comparison, BR/EDR inquiry | |
5723 | * and LE scanning are done sequentially with separate | |
5724 | * timeouts. | |
5725 | */ | |
5726 | if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, | |
5727 | &hdev->quirks)) { | |
5728 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); | |
5729 | /* During simultaneous discovery, we double LE scan | |
5730 | * interval. We must leave some time for the controller | |
5731 | * to do BR/EDR inquiry. | |
5732 | */ | |
5733 | err = hci_start_interleaved_discovery_sync(hdev); | |
5734 | break; | |
5735 | } | |
5736 | ||
5737 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); | |
5738 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); | |
5739 | break; | |
5740 | case DISCOV_TYPE_LE: | |
5741 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); | |
5742 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); | |
5743 | break; | |
5744 | default: | |
5745 | return -EINVAL; | |
5746 | } | |
5747 | ||
5748 | if (err) | |
5749 | return err; | |
5750 | ||
5751 | bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout)); | |
5752 | ||
5753 | /* When service discovery is used and the controller has a | |
5754 | * strict duplicate filter, it is important to remember the | |
5755 | * start and duration of the scan. This is required for | |
5756 | * restarting scanning during the discovery phase. | |
5757 | */ | |
5758 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) && | |
5759 | hdev->discovery.result_filtering) { | |
5760 | hdev->discovery.scan_start = jiffies; | |
5761 | hdev->discovery.scan_duration = timeout; | |
5762 | } | |
5763 | ||
5764 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable, | |
5765 | timeout); | |
abfeea47 LAD |
5766 | return 0; |
5767 | } | |
182ee45d LAD |
5768 | |
5769 | static void hci_suspend_monitor_sync(struct hci_dev *hdev) | |
5770 | { | |
5771 | switch (hci_get_adv_monitor_offload_ext(hdev)) { | |
5772 | case HCI_ADV_MONITOR_EXT_MSFT: | |
5773 | msft_suspend_sync(hdev); | |
5774 | break; | |
5775 | default: | |
5776 | return; | |
5777 | } | |
5778 | } | |
5779 | ||
5780 | /* This function disables discovery and mark it as paused */ | |
5781 | static int hci_pause_discovery_sync(struct hci_dev *hdev) | |
5782 | { | |
5783 | int old_state = hdev->discovery.state; | |
5784 | int err; | |
5785 | ||
5786 | /* If discovery already stopped/stopping/paused there nothing to do */ | |
5787 | if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING || | |
5788 | hdev->discovery_paused) | |
5789 | return 0; | |
5790 | ||
5791 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); | |
5792 | err = hci_stop_discovery_sync(hdev); | |
5793 | if (err) | |
5794 | return err; | |
5795 | ||
5796 | hdev->discovery_paused = true; | |
5797 | hdev->discovery_old_state = old_state; | |
5798 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); | |
5799 | ||
5800 | return 0; | |
5801 | } | |
5802 | ||
5803 | static int hci_update_event_filter_sync(struct hci_dev *hdev) | |
5804 | { | |
5805 | struct bdaddr_list_with_flags *b; | |
5806 | u8 scan = SCAN_DISABLED; | |
5807 | bool scanning = test_bit(HCI_PSCAN, &hdev->flags); | |
5808 | int err; | |
5809 | ||
5810 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
5811 | return 0; | |
5812 | ||
0eaecfb2 IFM |
5813 | /* Some fake CSR controllers lock up after setting this type of |
5814 | * filter, so avoid sending the request altogether. | |
5815 | */ | |
5816 | if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) | |
5817 | return 0; | |
5818 | ||
182ee45d LAD |
5819 | /* Always clear event filter when starting */ |
5820 | hci_clear_event_filter_sync(hdev); | |
5821 | ||
5822 | list_for_each_entry(b, &hdev->accept_list, list) { | |
e1cff700 | 5823 | if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) |
182ee45d LAD |
5824 | continue; |
5825 | ||
5826 | bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr); | |
5827 | ||
5828 | err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP, | |
5829 | HCI_CONN_SETUP_ALLOW_BDADDR, | |
5830 | &b->bdaddr, | |
5831 | HCI_CONN_SETUP_AUTO_ON); | |
5832 | if (err) | |
5833 | bt_dev_dbg(hdev, "Failed to set event filter for %pMR", | |
5834 | &b->bdaddr); | |
5835 | else | |
5836 | scan = SCAN_PAGE; | |
5837 | } | |
5838 | ||
5839 | if (scan && !scanning) | |
5840 | hci_write_scan_enable_sync(hdev, scan); | |
5841 | else if (!scan && scanning) | |
5842 | hci_write_scan_enable_sync(hdev, scan); | |
5843 | ||
5844 | return 0; | |
5845 | } | |
5846 | ||
3b420553 LAD |
5847 | /* This function disables scan (BR and LE) and mark it as paused */ |
5848 | static int hci_pause_scan_sync(struct hci_dev *hdev) | |
5849 | { | |
5850 | if (hdev->scanning_paused) | |
5851 | return 0; | |
5852 | ||
5853 | /* Disable page scan if enabled */ | |
5854 | if (test_bit(HCI_PSCAN, &hdev->flags)) | |
5855 | hci_write_scan_enable_sync(hdev, SCAN_DISABLED); | |
5856 | ||
5857 | hci_scan_disable_sync(hdev); | |
5858 | ||
5859 | hdev->scanning_paused = true; | |
5860 | ||
5861 | return 0; | |
5862 | } | |
5863 | ||
182ee45d LAD |
5864 | /* This function performs the HCI suspend procedures in the follow order: |
5865 | * | |
5866 | * Pause discovery (active scanning/inquiry) | |
5867 | * Pause Directed Advertising/Advertising | |
3b420553 | 5868 | * Pause Scanning (passive scanning in case discovery was not active) |
182ee45d LAD |
5869 | * Disconnect all connections |
5870 | * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup | |
5871 | * otherwise: | |
5872 | * Update event mask (only set events that are allowed to wake up the host) | |
5873 | * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP) | |
5874 | * Update passive scanning (lower duty cycle) | |
5875 | * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE | |
5876 | */ | |
5877 | int hci_suspend_sync(struct hci_dev *hdev) | |
5878 | { | |
5879 | int err; | |
5880 | ||
5881 | /* If marked as suspended there nothing to do */ | |
5882 | if (hdev->suspended) | |
5883 | return 0; | |
5884 | ||
5885 | /* Mark device as suspended */ | |
5886 | hdev->suspended = true; | |
5887 | ||
5888 | /* Pause discovery if not already stopped */ | |
5889 | hci_pause_discovery_sync(hdev); | |
5890 | ||
5891 | /* Pause other advertisements */ | |
5892 | hci_pause_advertising_sync(hdev); | |
5893 | ||
182ee45d LAD |
5894 | /* Suspend monitor filters */ |
5895 | hci_suspend_monitor_sync(hdev); | |
5896 | ||
5897 | /* Prevent disconnects from causing scanning to be re-enabled */ | |
3b420553 | 5898 | hci_pause_scan_sync(hdev); |
182ee45d | 5899 | |
123f6d3a LAD |
5900 | if (hci_conn_count(hdev)) { |
5901 | /* Soft disconnect everything (power off) */ | |
5902 | err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); | |
5903 | if (err) { | |
5904 | /* Set state to BT_RUNNING so resume doesn't notify */ | |
5905 | hdev->suspend_state = BT_RUNNING; | |
5906 | hci_resume_sync(hdev); | |
5907 | return err; | |
5908 | } | |
182ee45d | 5909 | |
123f6d3a LAD |
5910 | /* Update event mask so only the allowed event can wakeup the |
5911 | * host. | |
5912 | */ | |
5913 | hci_set_event_mask_sync(hdev); | |
5914 | } | |
ef61b6ea | 5915 | |
182ee45d LAD |
5916 | /* Only configure accept list if disconnect succeeded and wake |
5917 | * isn't being prevented. | |
5918 | */ | |
5919 | if (!hdev->wakeup || !hdev->wakeup(hdev)) { | |
5920 | hdev->suspend_state = BT_SUSPEND_DISCONNECT; | |
5921 | return 0; | |
5922 | } | |
5923 | ||
5924 | /* Unpause to take care of updating scanning params */ | |
5925 | hdev->scanning_paused = false; | |
5926 | ||
182ee45d LAD |
5927 | /* Enable event filter for paired devices */ |
5928 | hci_update_event_filter_sync(hdev); | |
5929 | ||
5930 | /* Update LE passive scan if enabled */ | |
5931 | hci_update_passive_scan_sync(hdev); | |
5932 | ||
5933 | /* Pause scan changes again. */ | |
5934 | hdev->scanning_paused = true; | |
5935 | ||
5936 | hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE; | |
5937 | ||
5938 | return 0; | |
5939 | } | |
5940 | ||
5941 | /* This function resumes discovery */ | |
5942 | static int hci_resume_discovery_sync(struct hci_dev *hdev) | |
5943 | { | |
5944 | int err; | |
5945 | ||
5946 | /* If discovery not paused there nothing to do */ | |
5947 | if (!hdev->discovery_paused) | |
5948 | return 0; | |
5949 | ||
5950 | hdev->discovery_paused = false; | |
5951 | ||
5952 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); | |
5953 | ||
5954 | err = hci_start_discovery_sync(hdev); | |
5955 | ||
5956 | hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED : | |
5957 | DISCOVERY_FINDING); | |
5958 | ||
5959 | return err; | |
5960 | } | |
5961 | ||
5962 | static void hci_resume_monitor_sync(struct hci_dev *hdev) | |
5963 | { | |
5964 | switch (hci_get_adv_monitor_offload_ext(hdev)) { | |
5965 | case HCI_ADV_MONITOR_EXT_MSFT: | |
5966 | msft_resume_sync(hdev); | |
5967 | break; | |
5968 | default: | |
5969 | return; | |
5970 | } | |
5971 | } | |
5972 | ||
3b420553 LAD |
5973 | /* This function resume scan and reset paused flag */ |
5974 | static int hci_resume_scan_sync(struct hci_dev *hdev) | |
5975 | { | |
5976 | if (!hdev->scanning_paused) | |
5977 | return 0; | |
5978 | ||
68253f3c ZJ |
5979 | hdev->scanning_paused = false; |
5980 | ||
3b420553 LAD |
5981 | hci_update_scan_sync(hdev); |
5982 | ||
5983 | /* Reset passive scanning to normal */ | |
5984 | hci_update_passive_scan_sync(hdev); | |
5985 | ||
3b420553 LAD |
5986 | return 0; |
5987 | } | |
5988 | ||
182ee45d LAD |
5989 | /* This function performs the HCI suspend procedures in the follow order: |
5990 | * | |
5991 | * Restore event mask | |
5992 | * Clear event filter | |
5993 | * Update passive scanning (normal duty cycle) | |
5994 | * Resume Directed Advertising/Advertising | |
5995 | * Resume discovery (active scanning/inquiry) | |
5996 | */ | |
5997 | int hci_resume_sync(struct hci_dev *hdev) | |
5998 | { | |
5999 | /* If not marked as suspended there nothing to do */ | |
6000 | if (!hdev->suspended) | |
6001 | return 0; | |
6002 | ||
6003 | hdev->suspended = false; | |
182ee45d LAD |
6004 | |
6005 | /* Restore event mask */ | |
6006 | hci_set_event_mask_sync(hdev); | |
6007 | ||
6008 | /* Clear any event filters and restore scan state */ | |
6009 | hci_clear_event_filter_sync(hdev); | |
182ee45d | 6010 | |
3b420553 LAD |
6011 | /* Resume scanning */ |
6012 | hci_resume_scan_sync(hdev); | |
182ee45d LAD |
6013 | |
6014 | /* Resume monitor filters */ | |
6015 | hci_resume_monitor_sync(hdev); | |
6016 | ||
6017 | /* Resume other advertisements */ | |
6018 | hci_resume_advertising_sync(hdev); | |
6019 | ||
6020 | /* Resume discovery */ | |
6021 | hci_resume_discovery_sync(hdev); | |
6022 | ||
6023 | return 0; | |
6024 | } | |
8e8b92ee LAD |
6025 | |
6026 | static bool conn_use_rpa(struct hci_conn *conn) | |
6027 | { | |
6028 | struct hci_dev *hdev = conn->hdev; | |
6029 | ||
6030 | return hci_dev_test_flag(hdev, HCI_PRIVACY); | |
6031 | } | |
6032 | ||
6033 | static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev, | |
6034 | struct hci_conn *conn) | |
6035 | { | |
6036 | struct hci_cp_le_set_ext_adv_params cp; | |
6037 | int err; | |
6038 | bdaddr_t random_addr; | |
6039 | u8 own_addr_type; | |
6040 | ||
6041 | err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), | |
6042 | &own_addr_type); | |
6043 | if (err) | |
6044 | return err; | |
6045 | ||
6046 | /* Set require_privacy to false so that the remote device has a | |
6047 | * chance of identifying us. | |
6048 | */ | |
6049 | err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL, | |
6050 | &own_addr_type, &random_addr); | |
6051 | if (err) | |
6052 | return err; | |
6053 | ||
6054 | memset(&cp, 0, sizeof(cp)); | |
6055 | ||
6056 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND); | |
8e8b92ee LAD |
6057 | cp.channel_map = hdev->le_adv_channel_map; |
6058 | cp.tx_power = HCI_TX_POWER_INVALID; | |
6059 | cp.primary_phy = HCI_ADV_PHY_1M; | |
6060 | cp.secondary_phy = HCI_ADV_PHY_1M; | |
6061 | cp.handle = 0x00; /* Use instance 0 for directed adv */ | |
6062 | cp.own_addr_type = own_addr_type; | |
6063 | cp.peer_addr_type = conn->dst_type; | |
6064 | bacpy(&cp.peer_addr, &conn->dst); | |
6065 | ||
6066 | /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for | |
6067 | * advertising_event_property LE_LEGACY_ADV_DIRECT_IND | |
6068 | * does not supports advertising data when the advertising set already | |
6069 | * contains some, the controller shall return erroc code 'Invalid | |
6070 | * HCI Command Parameters(0x12). | |
6071 | * So it is required to remove adv set for handle 0x00. since we use | |
6072 | * instance 0 for directed adv. | |
6073 | */ | |
6074 | err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL); | |
6075 | if (err) | |
6076 | return err; | |
6077 | ||
6078 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, | |
6079 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
6080 | if (err) | |
6081 | return err; | |
6082 | ||
6083 | /* Check if random address need to be updated */ | |
6084 | if (own_addr_type == ADDR_LE_DEV_RANDOM && | |
6085 | bacmp(&random_addr, BDADDR_ANY) && | |
6086 | bacmp(&random_addr, &hdev->random_addr)) { | |
6087 | err = hci_set_adv_set_random_addr_sync(hdev, 0x00, | |
6088 | &random_addr); | |
6089 | if (err) | |
6090 | return err; | |
6091 | } | |
6092 | ||
6093 | return hci_enable_ext_advertising_sync(hdev, 0x00); | |
6094 | } | |
6095 | ||
6096 | static int hci_le_directed_advertising_sync(struct hci_dev *hdev, | |
6097 | struct hci_conn *conn) | |
6098 | { | |
6099 | struct hci_cp_le_set_adv_param cp; | |
6100 | u8 status; | |
6101 | u8 own_addr_type; | |
6102 | u8 enable; | |
6103 | ||
6104 | if (ext_adv_capable(hdev)) | |
6105 | return hci_le_ext_directed_advertising_sync(hdev, conn); | |
6106 | ||
6107 | /* Clear the HCI_LE_ADV bit temporarily so that the | |
6108 | * hci_update_random_address knows that it's safe to go ahead | |
6109 | * and write a new random address. The flag will be set back on | |
6110 | * as soon as the SET_ADV_ENABLE HCI command completes. | |
6111 | */ | |
6112 | hci_dev_clear_flag(hdev, HCI_LE_ADV); | |
6113 | ||
6114 | /* Set require_privacy to false so that the remote device has a | |
6115 | * chance of identifying us. | |
6116 | */ | |
6117 | status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), | |
6118 | &own_addr_type); | |
6119 | if (status) | |
6120 | return status; | |
6121 | ||
6122 | memset(&cp, 0, sizeof(cp)); | |
6123 | ||
6124 | /* Some controllers might reject command if intervals are not | |
6125 | * within range for undirected advertising. | |
6126 | * BCM20702A0 is known to be affected by this. | |
6127 | */ | |
6128 | cp.min_interval = cpu_to_le16(0x0020); | |
6129 | cp.max_interval = cpu_to_le16(0x0020); | |
6130 | ||
6131 | cp.type = LE_ADV_DIRECT_IND; | |
6132 | cp.own_address_type = own_addr_type; | |
6133 | cp.direct_addr_type = conn->dst_type; | |
6134 | bacpy(&cp.direct_addr, &conn->dst); | |
6135 | cp.channel_map = hdev->le_adv_channel_map; | |
6136 | ||
6137 | status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, | |
6138 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
6139 | if (status) | |
6140 | return status; | |
6141 | ||
6142 | enable = 0x01; | |
6143 | ||
6144 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
6145 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
6146 | } | |
6147 | ||
6148 | static void set_ext_conn_params(struct hci_conn *conn, | |
6149 | struct hci_cp_le_ext_conn_param *p) | |
6150 | { | |
6151 | struct hci_dev *hdev = conn->hdev; | |
6152 | ||
6153 | memset(p, 0, sizeof(*p)); | |
6154 | ||
6155 | p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect); | |
6156 | p->scan_window = cpu_to_le16(hdev->le_scan_window_connect); | |
6157 | p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); | |
6158 | p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); | |
6159 | p->conn_latency = cpu_to_le16(conn->le_conn_latency); | |
6160 | p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout); | |
6161 | p->min_ce_len = cpu_to_le16(0x0000); | |
6162 | p->max_ce_len = cpu_to_le16(0x0000); | |
6163 | } | |
6164 | ||
89a0b8b9 LAD |
6165 | static int hci_le_ext_create_conn_sync(struct hci_dev *hdev, |
6166 | struct hci_conn *conn, u8 own_addr_type) | |
8e8b92ee LAD |
6167 | { |
6168 | struct hci_cp_le_ext_create_conn *cp; | |
6169 | struct hci_cp_le_ext_conn_param *p; | |
6170 | u8 data[sizeof(*cp) + sizeof(*p) * 3]; | |
6171 | u32 plen; | |
6172 | ||
6173 | cp = (void *)data; | |
6174 | p = (void *)cp->data; | |
6175 | ||
6176 | memset(cp, 0, sizeof(*cp)); | |
6177 | ||
6178 | bacpy(&cp->peer_addr, &conn->dst); | |
6179 | cp->peer_addr_type = conn->dst_type; | |
6180 | cp->own_addr_type = own_addr_type; | |
6181 | ||
6182 | plen = sizeof(*cp); | |
6183 | ||
6184 | if (scan_1m(hdev)) { | |
6185 | cp->phys |= LE_SCAN_PHY_1M; | |
6186 | set_ext_conn_params(conn, p); | |
6187 | ||
6188 | p++; | |
6189 | plen += sizeof(*p); | |
6190 | } | |
6191 | ||
6192 | if (scan_2m(hdev)) { | |
6193 | cp->phys |= LE_SCAN_PHY_2M; | |
6194 | set_ext_conn_params(conn, p); | |
6195 | ||
6196 | p++; | |
6197 | plen += sizeof(*p); | |
6198 | } | |
6199 | ||
6200 | if (scan_coded(hdev)) { | |
6201 | cp->phys |= LE_SCAN_PHY_CODED; | |
6202 | set_ext_conn_params(conn, p); | |
6203 | ||
6204 | plen += sizeof(*p); | |
6205 | } | |
6206 | ||
6cd29ec6 LAD |
6207 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN, |
6208 | plen, data, | |
6209 | HCI_EV_LE_ENHANCED_CONN_COMPLETE, | |
a56a1138 | 6210 | conn->conn_timeout, NULL); |
8e8b92ee LAD |
6211 | } |
6212 | ||
6213 | int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn) | |
6214 | { | |
6215 | struct hci_cp_le_create_conn cp; | |
6216 | struct hci_conn_params *params; | |
6217 | u8 own_addr_type; | |
6218 | int err; | |
6219 | ||
8e8b92ee LAD |
6220 | /* If requested to connect as peripheral use directed advertising */ |
6221 | if (conn->role == HCI_ROLE_SLAVE) { | |
76d0685b LAD |
6222 | /* If we're active scanning and simultaneous roles is not |
6223 | * enabled simply reject the attempt. | |
8e8b92ee LAD |
6224 | */ |
6225 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && | |
4fc9857a | 6226 | hdev->le_scan_type == LE_SCAN_ACTIVE && |
76d0685b | 6227 | !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) { |
8e8b92ee LAD |
6228 | hci_conn_del(conn); |
6229 | return -EBUSY; | |
6230 | } | |
6231 | ||
4fc9857a LAD |
6232 | /* Pause advertising while doing directed advertising. */ |
6233 | hci_pause_advertising_sync(hdev); | |
6234 | ||
8e8b92ee LAD |
6235 | err = hci_le_directed_advertising_sync(hdev, conn); |
6236 | goto done; | |
6237 | } | |
6238 | ||
76d0685b LAD |
6239 | /* Disable advertising if simultaneous roles is not in use. */ |
6240 | if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) | |
4fc9857a LAD |
6241 | hci_pause_advertising_sync(hdev); |
6242 | ||
8e8b92ee LAD |
6243 | params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); |
6244 | if (params) { | |
6245 | conn->le_conn_min_interval = params->conn_min_interval; | |
6246 | conn->le_conn_max_interval = params->conn_max_interval; | |
6247 | conn->le_conn_latency = params->conn_latency; | |
6248 | conn->le_supv_timeout = params->supervision_timeout; | |
6249 | } else { | |
6250 | conn->le_conn_min_interval = hdev->le_conn_min_interval; | |
6251 | conn->le_conn_max_interval = hdev->le_conn_max_interval; | |
6252 | conn->le_conn_latency = hdev->le_conn_latency; | |
6253 | conn->le_supv_timeout = hdev->le_supv_timeout; | |
6254 | } | |
6255 | ||
6256 | /* If controller is scanning, we stop it since some controllers are | |
6257 | * not able to scan and connect at the same time. Also set the | |
6258 | * HCI_LE_SCAN_INTERRUPTED flag so that the command complete | |
6259 | * handler for scan disabling knows to set the correct discovery | |
6260 | * state. | |
6261 | */ | |
6262 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { | |
6263 | hci_scan_disable_sync(hdev); | |
6264 | hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED); | |
6265 | } | |
6266 | ||
6267 | /* Update random address, but set require_privacy to false so | |
6268 | * that we never connect with an non-resolvable address. | |
6269 | */ | |
6270 | err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), | |
6271 | &own_addr_type); | |
6272 | if (err) | |
6273 | goto done; | |
6274 | ||
6275 | if (use_ext_conn(hdev)) { | |
6276 | err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type); | |
6277 | goto done; | |
6278 | } | |
6279 | ||
6280 | memset(&cp, 0, sizeof(cp)); | |
6281 | ||
6282 | cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect); | |
6283 | cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect); | |
6284 | ||
6285 | bacpy(&cp.peer_addr, &conn->dst); | |
6286 | cp.peer_addr_type = conn->dst_type; | |
6287 | cp.own_address_type = own_addr_type; | |
6288 | cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); | |
6289 | cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); | |
6290 | cp.conn_latency = cpu_to_le16(conn->le_conn_latency); | |
6291 | cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout); | |
6292 | cp.min_ce_len = cpu_to_le16(0x0000); | |
6293 | cp.max_ce_len = cpu_to_le16(0x0000); | |
6294 | ||
a56a1138 LAD |
6295 | /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261: |
6296 | * | |
6297 | * If this event is unmasked and the HCI_LE_Connection_Complete event | |
6298 | * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is | |
6299 | * sent when a new connection has been created. | |
6300 | */ | |
6cd29ec6 | 6301 | err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN, |
a56a1138 LAD |
6302 | sizeof(cp), &cp, |
6303 | use_enhanced_conn_complete(hdev) ? | |
6304 | HCI_EV_LE_ENHANCED_CONN_COMPLETE : | |
6305 | HCI_EV_LE_CONN_COMPLETE, | |
6306 | conn->conn_timeout, NULL); | |
8e8b92ee LAD |
6307 | |
6308 | done: | |
b62e7220 | 6309 | if (err == -ETIMEDOUT) |
a13f316e | 6310 | hci_le_connect_cancel_sync(hdev, conn, 0x00); |
b62e7220 | 6311 | |
4fc9857a | 6312 | /* Re-enable advertising after the connection attempt is finished. */ |
8e8b92ee LAD |
6313 | hci_resume_advertising_sync(hdev); |
6314 | return err; | |
6315 | } | |
26afbd82 | 6316 | |
7f74563e | 6317 | int hci_le_create_cis_sync(struct hci_dev *hdev) |
c09b80be LAD |
6318 | { |
6319 | struct { | |
6320 | struct hci_cp_le_create_cis cp; | |
6321 | struct hci_cis cis[0x1f]; | |
6322 | } cmd; | |
7f74563e PV |
6323 | struct hci_conn *conn; |
6324 | u8 cig = BT_ISO_QOS_CIG_UNSET; | |
6325 | ||
6326 | /* The spec allows only one pending LE Create CIS command at a time. If | |
6327 | * the command is pending now, don't do anything. We check for pending | |
6328 | * connections after each CIS Established event. | |
6329 | * | |
6330 | * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E | |
6331 | * page 2566: | |
6332 | * | |
6333 | * If the Host issues this command before all the | |
6334 | * HCI_LE_CIS_Established events from the previous use of the | |
6335 | * command have been generated, the Controller shall return the | |
6336 | * error code Command Disallowed (0x0C). | |
6337 | * | |
6338 | * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E | |
6339 | * page 2567: | |
6340 | * | |
6341 | * When the Controller receives the HCI_LE_Create_CIS command, the | |
6342 | * Controller sends the HCI_Command_Status event to the Host. An | |
6343 | * HCI_LE_CIS_Established event will be generated for each CIS when it | |
6344 | * is established or if it is disconnected or considered lost before | |
6345 | * being established; until all the events are generated, the command | |
6346 | * remains pending. | |
6347 | */ | |
c09b80be LAD |
6348 | |
6349 | memset(&cmd, 0, sizeof(cmd)); | |
c09b80be LAD |
6350 | |
6351 | hci_dev_lock(hdev); | |
6352 | ||
6353 | rcu_read_lock(); | |
6354 | ||
7f74563e | 6355 | /* Wait until previous Create CIS has completed */ |
c09b80be | 6356 | list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { |
7f74563e PV |
6357 | if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) |
6358 | goto done; | |
6359 | } | |
c09b80be | 6360 | |
7f74563e PV |
6361 | /* Find CIG with all CIS ready */ |
6362 | list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { | |
6363 | struct hci_conn *link; | |
6364 | ||
6365 | if (hci_conn_check_create_cis(conn)) | |
c09b80be LAD |
6366 | continue; |
6367 | ||
7f74563e PV |
6368 | cig = conn->iso_qos.ucast.cig; |
6369 | ||
6370 | list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) { | |
6371 | if (hci_conn_check_create_cis(link) > 0 && | |
6372 | link->iso_qos.ucast.cig == cig && | |
6373 | link->state != BT_CONNECTED) { | |
6374 | cig = BT_ISO_QOS_CIG_UNSET; | |
6375 | break; | |
6376 | } | |
c09b80be LAD |
6377 | } |
6378 | ||
7f74563e PV |
6379 | if (cig != BT_ISO_QOS_CIG_UNSET) |
6380 | break; | |
6381 | } | |
6382 | ||
6383 | if (cig == BT_ISO_QOS_CIG_UNSET) | |
6384 | goto done; | |
6385 | ||
6386 | list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { | |
6387 | struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis]; | |
6388 | ||
6389 | if (hci_conn_check_create_cis(conn) || | |
6390 | conn->iso_qos.ucast.cig != cig) | |
6391 | continue; | |
6392 | ||
6393 | set_bit(HCI_CONN_CREATE_CIS, &conn->flags); | |
c09b80be LAD |
6394 | cis->acl_handle = cpu_to_le16(conn->parent->handle); |
6395 | cis->cis_handle = cpu_to_le16(conn->handle); | |
6396 | cmd.cp.num_cis++; | |
7f74563e PV |
6397 | |
6398 | if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis)) | |
6399 | break; | |
c09b80be LAD |
6400 | } |
6401 | ||
7f74563e | 6402 | done: |
c09b80be LAD |
6403 | rcu_read_unlock(); |
6404 | ||
6405 | hci_dev_unlock(hdev); | |
6406 | ||
6407 | if (!cmd.cp.num_cis) | |
6408 | return 0; | |
6409 | ||
6410 | /* Wait for HCI_LE_CIS_Established */ | |
6411 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS, | |
6412 | sizeof(cmd.cp) + sizeof(cmd.cis[0]) * | |
6413 | cmd.cp.num_cis, &cmd, | |
6414 | HCI_EVT_LE_CIS_ESTABLISHED, | |
6415 | conn->conn_timeout, NULL); | |
6416 | } | |
6417 | ||
26afbd82 LAD |
6418 | int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle) |
6419 | { | |
6420 | struct hci_cp_le_remove_cig cp; | |
6421 | ||
6422 | memset(&cp, 0, sizeof(cp)); | |
6423 | cp.cig_id = handle; | |
6424 | ||
6425 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp), | |
6426 | &cp, HCI_CMD_TIMEOUT); | |
6427 | } | |
eca0ae4a LAD |
6428 | |
6429 | int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle) | |
6430 | { | |
6431 | struct hci_cp_le_big_term_sync cp; | |
6432 | ||
6433 | memset(&cp, 0, sizeof(cp)); | |
6434 | cp.handle = handle; | |
6435 | ||
6436 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC, | |
6437 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
6438 | } | |
6439 | ||
6440 | int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle) | |
6441 | { | |
6442 | struct hci_cp_le_pa_term_sync cp; | |
6443 | ||
6444 | memset(&cp, 0, sizeof(cp)); | |
6445 | cp.handle = cpu_to_le16(handle); | |
6446 | ||
6447 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC, | |
6448 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
6449 | } | |
3fe318ee BG |
6450 | |
6451 | int hci_get_random_address(struct hci_dev *hdev, bool require_privacy, | |
6452 | bool use_rpa, struct adv_info *adv_instance, | |
6453 | u8 *own_addr_type, bdaddr_t *rand_addr) | |
6454 | { | |
6455 | int err; | |
6456 | ||
6457 | bacpy(rand_addr, BDADDR_ANY); | |
6458 | ||
6459 | /* If privacy is enabled use a resolvable private address. If | |
6460 | * current RPA has expired then generate a new one. | |
6461 | */ | |
6462 | if (use_rpa) { | |
6463 | /* If Controller supports LL Privacy use own address type is | |
6464 | * 0x03 | |
6465 | */ | |
6466 | if (use_ll_privacy(hdev)) | |
6467 | *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; | |
6468 | else | |
6469 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
6470 | ||
6471 | if (adv_instance) { | |
6472 | if (adv_rpa_valid(adv_instance)) | |
6473 | return 0; | |
6474 | } else { | |
6475 | if (rpa_valid(hdev)) | |
6476 | return 0; | |
6477 | } | |
6478 | ||
6479 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); | |
6480 | if (err < 0) { | |
6481 | bt_dev_err(hdev, "failed to generate new RPA"); | |
6482 | return err; | |
6483 | } | |
6484 | ||
6485 | bacpy(rand_addr, &hdev->rpa); | |
6486 | ||
6487 | return 0; | |
6488 | } | |
6489 | ||
6490 | /* In case of required privacy without resolvable private address, | |
6491 | * use an non-resolvable private address. This is useful for | |
6492 | * non-connectable advertising. | |
6493 | */ | |
6494 | if (require_privacy) { | |
6495 | bdaddr_t nrpa; | |
6496 | ||
6497 | while (true) { | |
6498 | /* The non-resolvable private address is generated | |
6499 | * from random six bytes with the two most significant | |
6500 | * bits cleared. | |
6501 | */ | |
6502 | get_random_bytes(&nrpa, 6); | |
6503 | nrpa.b[5] &= 0x3f; | |
6504 | ||
6505 | /* The non-resolvable private address shall not be | |
6506 | * equal to the public address. | |
6507 | */ | |
6508 | if (bacmp(&hdev->bdaddr, &nrpa)) | |
6509 | break; | |
6510 | } | |
6511 | ||
6512 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
6513 | bacpy(rand_addr, &nrpa); | |
6514 | ||
6515 | return 0; | |
6516 | } | |
6517 | ||
6518 | /* No privacy so use a public address. */ | |
6519 | *own_addr_type = ADDR_LE_DEV_PUBLIC; | |
6520 | ||
6521 | return 0; | |
6522 | } | |
651cd3d6 BG |
6523 | |
6524 | static int _update_adv_data_sync(struct hci_dev *hdev, void *data) | |
6525 | { | |
1ed8b37c | 6526 | u8 instance = PTR_ERR(data); |
651cd3d6 BG |
6527 | |
6528 | return hci_update_adv_data_sync(hdev, instance); | |
6529 | } | |
6530 | ||
6531 | int hci_update_adv_data(struct hci_dev *hdev, u8 instance) | |
6532 | { | |
1ed8b37c ZS |
6533 | return hci_cmd_sync_queue(hdev, _update_adv_data_sync, |
6534 | ERR_PTR(instance), NULL); | |
651cd3d6 | 6535 | } |