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