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