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