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