Commit | Line | Data |
---|---|---|
6a98e383 MH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * BlueZ - Bluetooth protocol stack for Linux | |
4 | * | |
5 | * Copyright (C) 2021 Intel Corporation | |
6 | */ | |
7 | ||
d0b13706 LAD |
8 | #include <linux/property.h> |
9 | ||
6a98e383 MH |
10 | #include <net/bluetooth/bluetooth.h> |
11 | #include <net/bluetooth/hci_core.h> | |
12 | #include <net/bluetooth/mgmt.h> | |
13 | ||
14 | #include "hci_request.h" | |
d0b13706 | 15 | #include "hci_debugfs.h" |
6a98e383 | 16 | #include "smp.h" |
161510cc | 17 | #include "eir.h" |
d0b13706 LAD |
18 | #include "msft.h" |
19 | #include "aosp.h" | |
20 | #include "leds.h" | |
6a98e383 MH |
21 | |
22 | static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, | |
23 | struct sk_buff *skb) | |
24 | { | |
25 | bt_dev_dbg(hdev, "result 0x%2.2x", result); | |
26 | ||
27 | if (hdev->req_status != HCI_REQ_PEND) | |
28 | return; | |
29 | ||
30 | hdev->req_result = result; | |
31 | hdev->req_status = HCI_REQ_DONE; | |
32 | ||
cba6b758 LAD |
33 | if (skb) { |
34 | struct sock *sk = hci_skb_sk(skb); | |
35 | ||
36 | /* Drop sk reference if set */ | |
37 | if (sk) | |
38 | sock_put(sk); | |
39 | ||
40 | hdev->req_skb = skb_get(skb); | |
41 | } | |
42 | ||
6a98e383 MH |
43 | wake_up_interruptible(&hdev->req_wait_q); |
44 | } | |
45 | ||
46 | static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, | |
47 | u32 plen, const void *param, | |
48 | struct sock *sk) | |
49 | { | |
50 | int len = HCI_COMMAND_HDR_SIZE + plen; | |
51 | struct hci_command_hdr *hdr; | |
52 | struct sk_buff *skb; | |
53 | ||
54 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
55 | if (!skb) | |
56 | return NULL; | |
57 | ||
58 | hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE); | |
59 | hdr->opcode = cpu_to_le16(opcode); | |
60 | hdr->plen = plen; | |
61 | ||
62 | if (plen) | |
63 | skb_put_data(skb, param, plen); | |
64 | ||
65 | bt_dev_dbg(hdev, "skb len %d", skb->len); | |
66 | ||
67 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; | |
68 | hci_skb_opcode(skb) = opcode; | |
69 | ||
cba6b758 LAD |
70 | /* Grab a reference if command needs to be associated with a sock (e.g. |
71 | * likely mgmt socket that initiated the command). | |
72 | */ | |
73 | if (sk) { | |
74 | hci_skb_sk(skb) = sk; | |
75 | sock_hold(sk); | |
76 | } | |
77 | ||
6a98e383 MH |
78 | return skb; |
79 | } | |
80 | ||
81 | static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen, | |
82 | const void *param, u8 event, struct sock *sk) | |
83 | { | |
84 | struct hci_dev *hdev = req->hdev; | |
85 | struct sk_buff *skb; | |
86 | ||
87 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); | |
88 | ||
89 | /* If an error occurred during request building, there is no point in | |
90 | * queueing the HCI command. We can simply return. | |
91 | */ | |
92 | if (req->err) | |
93 | return; | |
94 | ||
95 | skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk); | |
96 | if (!skb) { | |
97 | bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)", | |
98 | opcode); | |
99 | req->err = -ENOMEM; | |
100 | return; | |
101 | } | |
102 | ||
103 | if (skb_queue_empty(&req->cmd_q)) | |
104 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; | |
105 | ||
106 | bt_cb(skb)->hci.req_event = event; | |
107 | ||
108 | skb_queue_tail(&req->cmd_q, skb); | |
109 | } | |
110 | ||
111 | static int hci_cmd_sync_run(struct hci_request *req) | |
112 | { | |
113 | struct hci_dev *hdev = req->hdev; | |
114 | struct sk_buff *skb; | |
115 | unsigned long flags; | |
116 | ||
117 | bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q)); | |
118 | ||
119 | /* If an error occurred during request building, remove all HCI | |
120 | * commands queued on the HCI request queue. | |
121 | */ | |
122 | if (req->err) { | |
123 | skb_queue_purge(&req->cmd_q); | |
124 | return req->err; | |
125 | } | |
126 | ||
127 | /* Do not allow empty requests */ | |
128 | if (skb_queue_empty(&req->cmd_q)) | |
129 | return -ENODATA; | |
130 | ||
131 | skb = skb_peek_tail(&req->cmd_q); | |
132 | bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete; | |
133 | bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB; | |
134 | ||
135 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); | |
136 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); | |
137 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); | |
138 | ||
139 | queue_work(hdev->workqueue, &hdev->cmd_work); | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | /* This function requires the caller holds hdev->req_lock. */ | |
145 | struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen, | |
146 | const void *param, u8 event, u32 timeout, | |
147 | struct sock *sk) | |
148 | { | |
149 | struct hci_request req; | |
150 | struct sk_buff *skb; | |
151 | int err = 0; | |
152 | ||
d0b13706 | 153 | bt_dev_dbg(hdev, "Opcode 0x%4x", opcode); |
6a98e383 MH |
154 | |
155 | hci_req_init(&req, hdev); | |
156 | ||
157 | hci_cmd_sync_add(&req, opcode, plen, param, event, sk); | |
158 | ||
159 | hdev->req_status = HCI_REQ_PEND; | |
160 | ||
161 | err = hci_cmd_sync_run(&req); | |
162 | if (err < 0) | |
163 | return ERR_PTR(err); | |
164 | ||
165 | err = wait_event_interruptible_timeout(hdev->req_wait_q, | |
166 | hdev->req_status != HCI_REQ_PEND, | |
167 | timeout); | |
168 | ||
169 | if (err == -ERESTARTSYS) | |
170 | return ERR_PTR(-EINTR); | |
171 | ||
172 | switch (hdev->req_status) { | |
173 | case HCI_REQ_DONE: | |
174 | err = -bt_to_errno(hdev->req_result); | |
175 | break; | |
176 | ||
177 | case HCI_REQ_CANCELED: | |
178 | err = -hdev->req_result; | |
179 | break; | |
180 | ||
181 | default: | |
182 | err = -ETIMEDOUT; | |
183 | break; | |
184 | } | |
185 | ||
186 | hdev->req_status = 0; | |
187 | hdev->req_result = 0; | |
188 | skb = hdev->req_skb; | |
189 | hdev->req_skb = NULL; | |
190 | ||
191 | bt_dev_dbg(hdev, "end: err %d", err); | |
192 | ||
193 | if (err < 0) { | |
194 | kfree_skb(skb); | |
195 | return ERR_PTR(err); | |
196 | } | |
197 | ||
6a98e383 MH |
198 | return skb; |
199 | } | |
200 | EXPORT_SYMBOL(__hci_cmd_sync_sk); | |
201 | ||
202 | /* This function requires the caller holds hdev->req_lock. */ | |
203 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, | |
204 | const void *param, u32 timeout) | |
205 | { | |
206 | return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL); | |
207 | } | |
208 | EXPORT_SYMBOL(__hci_cmd_sync); | |
209 | ||
210 | /* Send HCI command and wait for command complete event */ | |
211 | struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, | |
212 | const void *param, u32 timeout) | |
213 | { | |
214 | struct sk_buff *skb; | |
215 | ||
216 | if (!test_bit(HCI_UP, &hdev->flags)) | |
217 | return ERR_PTR(-ENETDOWN); | |
218 | ||
219 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); | |
220 | ||
221 | hci_req_sync_lock(hdev); | |
222 | skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); | |
223 | hci_req_sync_unlock(hdev); | |
224 | ||
225 | return skb; | |
226 | } | |
227 | EXPORT_SYMBOL(hci_cmd_sync); | |
228 | ||
229 | /* This function requires the caller holds hdev->req_lock. */ | |
230 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, | |
231 | const void *param, u8 event, u32 timeout) | |
232 | { | |
233 | return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, | |
234 | NULL); | |
235 | } | |
236 | EXPORT_SYMBOL(__hci_cmd_sync_ev); | |
237 | ||
238 | /* This function requires the caller holds hdev->req_lock. */ | |
239 | int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen, | |
240 | const void *param, u8 event, u32 timeout, | |
241 | struct sock *sk) | |
242 | { | |
243 | struct sk_buff *skb; | |
244 | u8 status; | |
245 | ||
246 | skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk); | |
abfeea47 | 247 | if (IS_ERR(skb)) { |
6a98e383 MH |
248 | bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode, |
249 | PTR_ERR(skb)); | |
250 | return PTR_ERR(skb); | |
251 | } | |
252 | ||
abfeea47 LAD |
253 | /* If command return a status event skb will be set to NULL as there are |
254 | * no parameters, in case of failure IS_ERR(skb) would have be set to | |
255 | * the actual error would be found with PTR_ERR(skb). | |
256 | */ | |
257 | if (!skb) | |
258 | return 0; | |
259 | ||
6a98e383 MH |
260 | status = skb->data[0]; |
261 | ||
262 | kfree_skb(skb); | |
263 | ||
264 | return status; | |
265 | } | |
266 | EXPORT_SYMBOL(__hci_cmd_sync_status_sk); | |
267 | ||
268 | int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen, | |
269 | const void *param, u32 timeout) | |
270 | { | |
271 | return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout, | |
272 | NULL); | |
273 | } | |
274 | EXPORT_SYMBOL(__hci_cmd_sync_status); | |
275 | ||
276 | static void hci_cmd_sync_work(struct work_struct *work) | |
277 | { | |
278 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work); | |
279 | struct hci_cmd_sync_work_entry *entry; | |
280 | hci_cmd_sync_work_func_t func; | |
281 | hci_cmd_sync_work_destroy_t destroy; | |
282 | void *data; | |
283 | ||
284 | bt_dev_dbg(hdev, ""); | |
285 | ||
286 | mutex_lock(&hdev->cmd_sync_work_lock); | |
287 | entry = list_first_entry(&hdev->cmd_sync_work_list, | |
288 | struct hci_cmd_sync_work_entry, list); | |
289 | if (entry) { | |
290 | list_del(&entry->list); | |
291 | func = entry->func; | |
292 | data = entry->data; | |
293 | destroy = entry->destroy; | |
294 | kfree(entry); | |
295 | } else { | |
296 | func = NULL; | |
297 | data = NULL; | |
298 | destroy = NULL; | |
299 | } | |
300 | mutex_unlock(&hdev->cmd_sync_work_lock); | |
301 | ||
302 | if (func) { | |
303 | int err; | |
304 | ||
305 | hci_req_sync_lock(hdev); | |
306 | ||
307 | err = func(hdev, data); | |
308 | ||
309 | if (destroy) | |
310 | destroy(hdev, data, err); | |
311 | ||
312 | hci_req_sync_unlock(hdev); | |
313 | } | |
314 | } | |
315 | ||
316 | void hci_cmd_sync_init(struct hci_dev *hdev) | |
317 | { | |
318 | INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work); | |
319 | INIT_LIST_HEAD(&hdev->cmd_sync_work_list); | |
320 | mutex_init(&hdev->cmd_sync_work_lock); | |
321 | } | |
322 | ||
323 | void hci_cmd_sync_clear(struct hci_dev *hdev) | |
324 | { | |
325 | struct hci_cmd_sync_work_entry *entry, *tmp; | |
326 | ||
327 | cancel_work_sync(&hdev->cmd_sync_work); | |
328 | ||
329 | list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) { | |
330 | if (entry->destroy) | |
331 | entry->destroy(hdev, entry->data, -ECANCELED); | |
332 | ||
333 | list_del(&entry->list); | |
334 | kfree(entry); | |
335 | } | |
336 | } | |
337 | ||
914b08b3 BB |
338 | void hci_cmd_sync_cancel(struct hci_dev *hdev, int err) |
339 | { | |
340 | bt_dev_dbg(hdev, "err 0x%2.2x", err); | |
341 | ||
342 | if (hdev->req_status == HCI_REQ_PEND) { | |
343 | hdev->req_result = err; | |
344 | hdev->req_status = HCI_REQ_CANCELED; | |
345 | ||
346 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
347 | cancel_delayed_work_sync(&hdev->ncmd_timer); | |
348 | atomic_set(&hdev->cmd_cnt, 1); | |
349 | ||
350 | wake_up_interruptible(&hdev->req_wait_q); | |
351 | } | |
352 | } | |
353 | EXPORT_SYMBOL(hci_cmd_sync_cancel); | |
354 | ||
6a98e383 MH |
355 | int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, |
356 | void *data, hci_cmd_sync_work_destroy_t destroy) | |
357 | { | |
358 | struct hci_cmd_sync_work_entry *entry; | |
359 | ||
360 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
361 | if (!entry) | |
362 | return -ENOMEM; | |
363 | ||
364 | entry->func = func; | |
365 | entry->data = data; | |
366 | entry->destroy = destroy; | |
367 | ||
368 | mutex_lock(&hdev->cmd_sync_work_lock); | |
369 | list_add_tail(&entry->list, &hdev->cmd_sync_work_list); | |
370 | mutex_unlock(&hdev->cmd_sync_work_lock); | |
371 | ||
372 | queue_work(hdev->req_workqueue, &hdev->cmd_sync_work); | |
373 | ||
374 | return 0; | |
375 | } | |
376 | EXPORT_SYMBOL(hci_cmd_sync_queue); | |
161510cc LAD |
377 | |
378 | int hci_update_eir_sync(struct hci_dev *hdev) | |
379 | { | |
380 | struct hci_cp_write_eir cp; | |
381 | ||
382 | bt_dev_dbg(hdev, ""); | |
383 | ||
384 | if (!hdev_is_powered(hdev)) | |
385 | return 0; | |
386 | ||
387 | if (!lmp_ext_inq_capable(hdev)) | |
388 | return 0; | |
389 | ||
390 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
391 | return 0; | |
392 | ||
393 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) | |
394 | return 0; | |
395 | ||
396 | memset(&cp, 0, sizeof(cp)); | |
397 | ||
398 | eir_create(hdev, cp.data); | |
399 | ||
400 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) | |
401 | return 0; | |
402 | ||
403 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); | |
404 | ||
405 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, | |
406 | HCI_CMD_TIMEOUT); | |
407 | } | |
408 | ||
409 | static u8 get_service_classes(struct hci_dev *hdev) | |
410 | { | |
411 | struct bt_uuid *uuid; | |
412 | u8 val = 0; | |
413 | ||
414 | list_for_each_entry(uuid, &hdev->uuids, list) | |
415 | val |= uuid->svc_hint; | |
416 | ||
417 | return val; | |
418 | } | |
419 | ||
420 | int hci_update_class_sync(struct hci_dev *hdev) | |
421 | { | |
422 | u8 cod[3]; | |
423 | ||
424 | bt_dev_dbg(hdev, ""); | |
425 | ||
426 | if (!hdev_is_powered(hdev)) | |
427 | return 0; | |
428 | ||
429 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
430 | return 0; | |
431 | ||
432 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) | |
433 | return 0; | |
434 | ||
435 | cod[0] = hdev->minor_class; | |
436 | cod[1] = hdev->major_class; | |
437 | cod[2] = get_service_classes(hdev); | |
438 | ||
439 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) | |
440 | cod[1] |= 0x20; | |
441 | ||
442 | if (memcmp(cod, hdev->dev_class, 3) == 0) | |
443 | return 0; | |
444 | ||
445 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV, | |
446 | sizeof(cod), cod, HCI_CMD_TIMEOUT); | |
447 | } | |
cba6b758 LAD |
448 | |
449 | static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable) | |
450 | { | |
451 | /* If there is no connection we are OK to advertise. */ | |
452 | if (hci_conn_num(hdev, LE_LINK) == 0) | |
453 | return true; | |
454 | ||
455 | /* Check le_states if there is any connection in peripheral role. */ | |
456 | if (hdev->conn_hash.le_num_peripheral > 0) { | |
457 | /* Peripheral connection state and non connectable mode | |
458 | * bit 20. | |
459 | */ | |
460 | if (!connectable && !(hdev->le_states[2] & 0x10)) | |
461 | return false; | |
462 | ||
463 | /* Peripheral connection state and connectable mode bit 38 | |
464 | * and scannable bit 21. | |
465 | */ | |
466 | if (connectable && (!(hdev->le_states[4] & 0x40) || | |
467 | !(hdev->le_states[2] & 0x20))) | |
468 | return false; | |
469 | } | |
470 | ||
471 | /* Check le_states if there is any connection in central role. */ | |
472 | if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) { | |
473 | /* Central connection state and non connectable mode bit 18. */ | |
474 | if (!connectable && !(hdev->le_states[2] & 0x02)) | |
475 | return false; | |
476 | ||
477 | /* Central connection state and connectable mode bit 35 and | |
478 | * scannable 19. | |
479 | */ | |
480 | if (connectable && (!(hdev->le_states[4] & 0x08) || | |
481 | !(hdev->le_states[2] & 0x08))) | |
482 | return false; | |
483 | } | |
484 | ||
485 | return true; | |
486 | } | |
487 | ||
488 | static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags) | |
489 | { | |
490 | /* If privacy is not enabled don't use RPA */ | |
491 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) | |
492 | return false; | |
493 | ||
494 | /* If basic privacy mode is enabled use RPA */ | |
495 | if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) | |
496 | return true; | |
497 | ||
498 | /* If limited privacy mode is enabled don't use RPA if we're | |
499 | * both discoverable and bondable. | |
500 | */ | |
501 | if ((flags & MGMT_ADV_FLAG_DISCOV) && | |
502 | hci_dev_test_flag(hdev, HCI_BONDABLE)) | |
503 | return false; | |
504 | ||
505 | /* We're neither bondable nor discoverable in the limited | |
506 | * privacy mode, therefore use RPA. | |
507 | */ | |
508 | return true; | |
509 | } | |
510 | ||
511 | static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa) | |
512 | { | |
513 | /* If we're advertising or initiating an LE connection we can't | |
514 | * go ahead and change the random address at this time. This is | |
515 | * because the eventual initiator address used for the | |
516 | * subsequently created connection will be undefined (some | |
517 | * controllers use the new address and others the one we had | |
518 | * when the operation started). | |
519 | * | |
520 | * In this kind of scenario skip the update and let the random | |
521 | * address be updated at the next cycle. | |
522 | */ | |
523 | if (hci_dev_test_flag(hdev, HCI_LE_ADV) || | |
524 | hci_lookup_le_connect(hdev)) { | |
525 | bt_dev_dbg(hdev, "Deferring random address update"); | |
526 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); | |
527 | return 0; | |
528 | } | |
529 | ||
530 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR, | |
531 | 6, rpa, HCI_CMD_TIMEOUT); | |
532 | } | |
533 | ||
534 | int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy, | |
535 | bool rpa, u8 *own_addr_type) | |
536 | { | |
537 | int err; | |
538 | ||
539 | /* If privacy is enabled use a resolvable private address. If | |
540 | * current RPA has expired or there is something else than | |
541 | * the current RPA in use, then generate a new one. | |
542 | */ | |
543 | if (rpa) { | |
544 | /* If Controller supports LL Privacy use own address type is | |
545 | * 0x03 | |
546 | */ | |
ad383c2c | 547 | if (use_ll_privacy(hdev)) |
cba6b758 LAD |
548 | *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; |
549 | else | |
550 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
551 | ||
552 | /* Check if RPA is valid */ | |
553 | if (rpa_valid(hdev)) | |
554 | return 0; | |
555 | ||
556 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); | |
557 | if (err < 0) { | |
558 | bt_dev_err(hdev, "failed to generate new RPA"); | |
559 | return err; | |
560 | } | |
561 | ||
562 | err = hci_set_random_addr_sync(hdev, &hdev->rpa); | |
563 | if (err) | |
564 | return err; | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
569 | /* In case of required privacy without resolvable private address, | |
570 | * use an non-resolvable private address. This is useful for active | |
571 | * scanning and non-connectable advertising. | |
572 | */ | |
573 | if (require_privacy) { | |
574 | bdaddr_t nrpa; | |
575 | ||
576 | while (true) { | |
577 | /* The non-resolvable private address is generated | |
578 | * from random six bytes with the two most significant | |
579 | * bits cleared. | |
580 | */ | |
581 | get_random_bytes(&nrpa, 6); | |
582 | nrpa.b[5] &= 0x3f; | |
583 | ||
584 | /* The non-resolvable private address shall not be | |
585 | * equal to the public address. | |
586 | */ | |
587 | if (bacmp(&hdev->bdaddr, &nrpa)) | |
588 | break; | |
589 | } | |
590 | ||
591 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
592 | ||
593 | return hci_set_random_addr_sync(hdev, &nrpa); | |
594 | } | |
595 | ||
596 | /* If forcing static address is in use or there is no public | |
597 | * address use the static address as random address (but skip | |
598 | * the HCI command if the current random address is already the | |
599 | * static one. | |
600 | * | |
601 | * In case BR/EDR has been disabled on a dual-mode controller | |
602 | * and a static address has been configured, then use that | |
603 | * address instead of the public BR/EDR address. | |
604 | */ | |
605 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || | |
606 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || | |
607 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && | |
608 | bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
609 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
610 | if (bacmp(&hdev->static_addr, &hdev->random_addr)) | |
611 | return hci_set_random_addr_sync(hdev, | |
612 | &hdev->static_addr); | |
613 | return 0; | |
614 | } | |
615 | ||
616 | /* Neither privacy nor static address is being used so use a | |
617 | * public address. | |
618 | */ | |
619 | *own_addr_type = ADDR_LE_DEV_PUBLIC; | |
620 | ||
621 | return 0; | |
622 | } | |
623 | ||
624 | static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) | |
625 | { | |
626 | struct hci_cp_le_set_ext_adv_enable *cp; | |
627 | struct hci_cp_ext_adv_set *set; | |
628 | u8 data[sizeof(*cp) + sizeof(*set) * 1]; | |
629 | u8 size; | |
630 | ||
631 | /* If request specifies an instance that doesn't exist, fail */ | |
632 | if (instance > 0) { | |
633 | struct adv_info *adv; | |
634 | ||
635 | adv = hci_find_adv_instance(hdev, instance); | |
636 | if (!adv) | |
637 | return -EINVAL; | |
638 | ||
639 | /* If not enabled there is nothing to do */ | |
640 | if (!adv->enabled) | |
641 | return 0; | |
642 | } | |
643 | ||
644 | memset(data, 0, sizeof(data)); | |
645 | ||
646 | cp = (void *)data; | |
647 | set = (void *)cp->data; | |
648 | ||
649 | /* Instance 0x00 indicates all advertising instances will be disabled */ | |
650 | cp->num_of_sets = !!instance; | |
651 | cp->enable = 0x00; | |
652 | ||
653 | set->handle = instance; | |
654 | ||
655 | size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets; | |
656 | ||
657 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, | |
658 | size, data, HCI_CMD_TIMEOUT); | |
659 | } | |
660 | ||
661 | static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, | |
662 | bdaddr_t *random_addr) | |
663 | { | |
664 | struct hci_cp_le_set_adv_set_rand_addr cp; | |
665 | int err; | |
666 | ||
667 | if (!instance) { | |
668 | /* Instance 0x00 doesn't have an adv_info, instead it uses | |
669 | * hdev->random_addr to track its address so whenever it needs | |
670 | * to be updated this also set the random address since | |
671 | * hdev->random_addr is shared with scan state machine. | |
672 | */ | |
673 | err = hci_set_random_addr_sync(hdev, random_addr); | |
674 | if (err) | |
675 | return err; | |
676 | } | |
677 | ||
678 | memset(&cp, 0, sizeof(cp)); | |
679 | ||
680 | cp.handle = instance; | |
681 | bacpy(&cp.bdaddr, random_addr); | |
682 | ||
683 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, | |
684 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
685 | } | |
686 | ||
687 | int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) | |
688 | { | |
689 | struct hci_cp_le_set_ext_adv_params cp; | |
690 | bool connectable; | |
691 | u32 flags; | |
692 | bdaddr_t random_addr; | |
693 | u8 own_addr_type; | |
694 | int err; | |
695 | struct adv_info *adv; | |
696 | bool secondary_adv; | |
697 | ||
698 | if (instance > 0) { | |
699 | adv = hci_find_adv_instance(hdev, instance); | |
700 | if (!adv) | |
701 | return -EINVAL; | |
702 | } else { | |
703 | adv = NULL; | |
704 | } | |
705 | ||
706 | /* Updating parameters of an active instance will return a | |
707 | * Command Disallowed error, so we must first disable the | |
708 | * instance if it is active. | |
709 | */ | |
710 | if (adv && !adv->pending) { | |
711 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | |
712 | if (err) | |
713 | return err; | |
714 | } | |
715 | ||
716 | flags = hci_adv_instance_flags(hdev, instance); | |
717 | ||
718 | /* If the "connectable" instance flag was not set, then choose between | |
719 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. | |
720 | */ | |
721 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | |
722 | mgmt_get_connectable(hdev); | |
723 | ||
724 | if (!is_advertising_allowed(hdev, connectable)) | |
725 | return -EPERM; | |
726 | ||
727 | /* Set require_privacy to true only when non-connectable | |
728 | * advertising is used. In that case it is fine to use a | |
729 | * non-resolvable private address. | |
730 | */ | |
731 | err = hci_get_random_address(hdev, !connectable, | |
732 | adv_use_rpa(hdev, flags), adv, | |
733 | &own_addr_type, &random_addr); | |
734 | if (err < 0) | |
735 | return err; | |
736 | ||
737 | memset(&cp, 0, sizeof(cp)); | |
738 | ||
739 | if (adv) { | |
740 | hci_cpu_to_le24(adv->min_interval, cp.min_interval); | |
741 | hci_cpu_to_le24(adv->max_interval, cp.max_interval); | |
742 | cp.tx_power = adv->tx_power; | |
743 | } else { | |
744 | hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval); | |
745 | hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval); | |
746 | cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; | |
747 | } | |
748 | ||
749 | secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK); | |
750 | ||
751 | if (connectable) { | |
752 | if (secondary_adv) | |
753 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND); | |
754 | else | |
755 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND); | |
756 | } else if (hci_adv_instance_is_scannable(hdev, instance) || | |
757 | (flags & MGMT_ADV_PARAM_SCAN_RSP)) { | |
758 | if (secondary_adv) | |
759 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND); | |
760 | else | |
761 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND); | |
762 | } else { | |
763 | if (secondary_adv) | |
764 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND); | |
765 | else | |
766 | cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND); | |
767 | } | |
768 | ||
cf75ad8b LAD |
769 | /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter |
770 | * contains the peer’s Identity Address and the Peer_Address_Type | |
771 | * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01). | |
772 | * These parameters are used to locate the corresponding local IRK in | |
773 | * the resolving list; this IRK is used to generate their own address | |
774 | * used in the advertisement. | |
775 | */ | |
776 | if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) | |
777 | hci_copy_identity_address(hdev, &cp.peer_addr, | |
778 | &cp.peer_addr_type); | |
779 | ||
cba6b758 LAD |
780 | cp.own_addr_type = own_addr_type; |
781 | cp.channel_map = hdev->le_adv_channel_map; | |
782 | cp.handle = instance; | |
783 | ||
784 | if (flags & MGMT_ADV_FLAG_SEC_2M) { | |
785 | cp.primary_phy = HCI_ADV_PHY_1M; | |
786 | cp.secondary_phy = HCI_ADV_PHY_2M; | |
787 | } else if (flags & MGMT_ADV_FLAG_SEC_CODED) { | |
788 | cp.primary_phy = HCI_ADV_PHY_CODED; | |
789 | cp.secondary_phy = HCI_ADV_PHY_CODED; | |
790 | } else { | |
791 | /* In all other cases use 1M */ | |
792 | cp.primary_phy = HCI_ADV_PHY_1M; | |
793 | cp.secondary_phy = HCI_ADV_PHY_1M; | |
794 | } | |
795 | ||
796 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, | |
797 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
798 | if (err) | |
799 | return err; | |
800 | ||
801 | if ((own_addr_type == ADDR_LE_DEV_RANDOM || | |
802 | own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && | |
803 | bacmp(&random_addr, BDADDR_ANY)) { | |
804 | /* Check if random address need to be updated */ | |
805 | if (adv) { | |
806 | if (!bacmp(&random_addr, &adv->random_addr)) | |
807 | return 0; | |
808 | } else { | |
809 | if (!bacmp(&random_addr, &hdev->random_addr)) | |
810 | return 0; | |
811 | } | |
812 | ||
813 | return hci_set_adv_set_random_addr_sync(hdev, instance, | |
814 | &random_addr); | |
815 | } | |
816 | ||
817 | return 0; | |
818 | } | |
819 | ||
820 | static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
821 | { | |
822 | struct { | |
823 | struct hci_cp_le_set_ext_scan_rsp_data cp; | |
824 | u8 data[HCI_MAX_EXT_AD_LENGTH]; | |
825 | } pdu; | |
826 | u8 len; | |
827 | ||
828 | memset(&pdu, 0, sizeof(pdu)); | |
829 | ||
830 | len = eir_create_scan_rsp(hdev, instance, pdu.data); | |
831 | ||
832 | if (hdev->scan_rsp_data_len == len && | |
833 | !memcmp(pdu.data, hdev->scan_rsp_data, len)) | |
834 | return 0; | |
835 | ||
836 | memcpy(hdev->scan_rsp_data, pdu.data, len); | |
837 | hdev->scan_rsp_data_len = len; | |
838 | ||
839 | pdu.cp.handle = instance; | |
840 | pdu.cp.length = len; | |
841 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
842 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; | |
843 | ||
844 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, | |
845 | sizeof(pdu.cp) + len, &pdu.cp, | |
846 | HCI_CMD_TIMEOUT); | |
847 | } | |
848 | ||
849 | static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
850 | { | |
851 | struct hci_cp_le_set_scan_rsp_data cp; | |
852 | u8 len; | |
853 | ||
854 | memset(&cp, 0, sizeof(cp)); | |
855 | ||
856 | len = eir_create_scan_rsp(hdev, instance, cp.data); | |
857 | ||
858 | if (hdev->scan_rsp_data_len == len && | |
859 | !memcmp(cp.data, hdev->scan_rsp_data, len)) | |
860 | return 0; | |
861 | ||
862 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); | |
863 | hdev->scan_rsp_data_len = len; | |
864 | ||
865 | cp.length = len; | |
866 | ||
867 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA, | |
868 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
869 | } | |
870 | ||
871 | int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
872 | { | |
873 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
874 | return 0; | |
875 | ||
876 | if (ext_adv_capable(hdev)) | |
877 | return hci_set_ext_scan_rsp_data_sync(hdev, instance); | |
878 | ||
879 | return __hci_set_scan_rsp_data_sync(hdev, instance); | |
880 | } | |
881 | ||
882 | int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance) | |
883 | { | |
884 | struct hci_cp_le_set_ext_adv_enable *cp; | |
885 | struct hci_cp_ext_adv_set *set; | |
886 | u8 data[sizeof(*cp) + sizeof(*set) * 1]; | |
887 | struct adv_info *adv; | |
888 | ||
889 | if (instance > 0) { | |
890 | adv = hci_find_adv_instance(hdev, instance); | |
891 | if (!adv) | |
892 | return -EINVAL; | |
893 | /* If already enabled there is nothing to do */ | |
894 | if (adv->enabled) | |
895 | return 0; | |
896 | } else { | |
897 | adv = NULL; | |
898 | } | |
899 | ||
900 | cp = (void *)data; | |
901 | set = (void *)cp->data; | |
902 | ||
903 | memset(cp, 0, sizeof(*cp)); | |
904 | ||
905 | cp->enable = 0x01; | |
906 | cp->num_of_sets = 0x01; | |
907 | ||
908 | memset(set, 0, sizeof(*set)); | |
909 | ||
910 | set->handle = instance; | |
911 | ||
912 | /* Set duration per instance since controller is responsible for | |
913 | * scheduling it. | |
914 | */ | |
f16a491c | 915 | if (adv && adv->timeout) { |
cba6b758 LAD |
916 | u16 duration = adv->timeout * MSEC_PER_SEC; |
917 | ||
918 | /* Time = N * 10 ms */ | |
919 | set->duration = cpu_to_le16(duration / 10); | |
920 | } | |
921 | ||
922 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, | |
923 | sizeof(*cp) + | |
924 | sizeof(*set) * cp->num_of_sets, | |
925 | data, HCI_CMD_TIMEOUT); | |
926 | } | |
927 | ||
928 | int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance) | |
929 | { | |
930 | int err; | |
931 | ||
932 | err = hci_setup_ext_adv_instance_sync(hdev, instance); | |
933 | if (err) | |
934 | return err; | |
935 | ||
936 | err = hci_set_ext_scan_rsp_data_sync(hdev, instance); | |
937 | if (err) | |
938 | return err; | |
939 | ||
940 | return hci_enable_ext_advertising_sync(hdev, instance); | |
941 | } | |
942 | ||
943 | static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance) | |
944 | { | |
945 | int err; | |
946 | ||
947 | if (ext_adv_capable(hdev)) | |
948 | return hci_start_ext_adv_sync(hdev, instance); | |
949 | ||
950 | err = hci_update_adv_data_sync(hdev, instance); | |
951 | if (err) | |
952 | return err; | |
953 | ||
954 | err = hci_update_scan_rsp_data_sync(hdev, instance); | |
955 | if (err) | |
956 | return err; | |
957 | ||
958 | return hci_enable_advertising_sync(hdev); | |
959 | } | |
960 | ||
961 | int hci_enable_advertising_sync(struct hci_dev *hdev) | |
962 | { | |
963 | struct adv_info *adv_instance; | |
964 | struct hci_cp_le_set_adv_param cp; | |
965 | u8 own_addr_type, enable = 0x01; | |
966 | bool connectable; | |
967 | u16 adv_min_interval, adv_max_interval; | |
968 | u32 flags; | |
969 | u8 status; | |
970 | ||
ad383c2c LAD |
971 | if (ext_adv_capable(hdev)) |
972 | return hci_enable_ext_advertising_sync(hdev, | |
973 | hdev->cur_adv_instance); | |
974 | ||
cba6b758 LAD |
975 | flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance); |
976 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); | |
977 | ||
978 | /* If the "connectable" instance flag was not set, then choose between | |
979 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. | |
980 | */ | |
981 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | |
982 | mgmt_get_connectable(hdev); | |
983 | ||
984 | if (!is_advertising_allowed(hdev, connectable)) | |
985 | return -EINVAL; | |
986 | ||
ad383c2c LAD |
987 | status = hci_disable_advertising_sync(hdev); |
988 | if (status) | |
989 | return status; | |
cba6b758 LAD |
990 | |
991 | /* Clear the HCI_LE_ADV bit temporarily so that the | |
992 | * hci_update_random_address knows that it's safe to go ahead | |
993 | * and write a new random address. The flag will be set back on | |
994 | * as soon as the SET_ADV_ENABLE HCI command completes. | |
995 | */ | |
996 | hci_dev_clear_flag(hdev, HCI_LE_ADV); | |
997 | ||
998 | /* Set require_privacy to true only when non-connectable | |
999 | * advertising is used. In that case it is fine to use a | |
1000 | * non-resolvable private address. | |
1001 | */ | |
1002 | status = hci_update_random_address_sync(hdev, !connectable, | |
1003 | adv_use_rpa(hdev, flags), | |
1004 | &own_addr_type); | |
1005 | if (status) | |
1006 | return status; | |
1007 | ||
1008 | memset(&cp, 0, sizeof(cp)); | |
1009 | ||
1010 | if (adv_instance) { | |
1011 | adv_min_interval = adv_instance->min_interval; | |
1012 | adv_max_interval = adv_instance->max_interval; | |
1013 | } else { | |
1014 | adv_min_interval = hdev->le_adv_min_interval; | |
1015 | adv_max_interval = hdev->le_adv_max_interval; | |
1016 | } | |
1017 | ||
1018 | if (connectable) { | |
1019 | cp.type = LE_ADV_IND; | |
1020 | } else { | |
1021 | if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance)) | |
1022 | cp.type = LE_ADV_SCAN_IND; | |
1023 | else | |
1024 | cp.type = LE_ADV_NONCONN_IND; | |
1025 | ||
1026 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) || | |
1027 | hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { | |
1028 | adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN; | |
1029 | adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX; | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | cp.min_interval = cpu_to_le16(adv_min_interval); | |
1034 | cp.max_interval = cpu_to_le16(adv_max_interval); | |
1035 | cp.own_address_type = own_addr_type; | |
1036 | cp.channel_map = hdev->le_adv_channel_map; | |
1037 | ||
1038 | status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, | |
1039 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1040 | if (status) | |
1041 | return status; | |
1042 | ||
1043 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
1044 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
1045 | } | |
1046 | ||
abfeea47 LAD |
1047 | static int enable_advertising_sync(struct hci_dev *hdev, void *data) |
1048 | { | |
1049 | return hci_enable_advertising_sync(hdev); | |
1050 | } | |
1051 | ||
1052 | int hci_enable_advertising(struct hci_dev *hdev) | |
1053 | { | |
1054 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && | |
1055 | list_empty(&hdev->adv_instances)) | |
1056 | return 0; | |
1057 | ||
1058 | return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL); | |
1059 | } | |
1060 | ||
1061 | int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, | |
1062 | struct sock *sk) | |
cba6b758 LAD |
1063 | { |
1064 | int err; | |
1065 | ||
1066 | if (!ext_adv_capable(hdev)) | |
1067 | return 0; | |
1068 | ||
1069 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | |
1070 | if (err) | |
1071 | return err; | |
1072 | ||
1073 | /* If request specifies an instance that doesn't exist, fail */ | |
1074 | if (instance > 0 && !hci_find_adv_instance(hdev, instance)) | |
1075 | return -EINVAL; | |
1076 | ||
1077 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, | |
1078 | sizeof(instance), &instance, 0, | |
1079 | HCI_CMD_TIMEOUT, sk); | |
1080 | } | |
1081 | ||
1082 | static void cancel_adv_timeout(struct hci_dev *hdev) | |
1083 | { | |
1084 | if (hdev->adv_instance_timeout) { | |
1085 | hdev->adv_instance_timeout = 0; | |
1086 | cancel_delayed_work(&hdev->adv_instance_expire); | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1091 | { | |
1092 | struct { | |
1093 | struct hci_cp_le_set_ext_adv_data cp; | |
1094 | u8 data[HCI_MAX_EXT_AD_LENGTH]; | |
1095 | } pdu; | |
1096 | u8 len; | |
1097 | ||
1098 | memset(&pdu, 0, sizeof(pdu)); | |
1099 | ||
1100 | len = eir_create_adv_data(hdev, instance, pdu.data); | |
1101 | ||
1102 | /* There's nothing to do if the data hasn't changed */ | |
1103 | if (hdev->adv_data_len == len && | |
1104 | memcmp(pdu.data, hdev->adv_data, len) == 0) | |
1105 | return 0; | |
1106 | ||
1107 | memcpy(hdev->adv_data, pdu.data, len); | |
1108 | hdev->adv_data_len = len; | |
1109 | ||
1110 | pdu.cp.length = len; | |
1111 | pdu.cp.handle = instance; | |
1112 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
1113 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; | |
1114 | ||
1115 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, | |
1116 | sizeof(pdu.cp) + len, &pdu.cp, | |
1117 | HCI_CMD_TIMEOUT); | |
1118 | } | |
1119 | ||
1120 | static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1121 | { | |
1122 | struct hci_cp_le_set_adv_data cp; | |
1123 | u8 len; | |
1124 | ||
1125 | memset(&cp, 0, sizeof(cp)); | |
1126 | ||
1127 | len = eir_create_adv_data(hdev, instance, cp.data); | |
1128 | ||
1129 | /* There's nothing to do if the data hasn't changed */ | |
1130 | if (hdev->adv_data_len == len && | |
1131 | memcmp(cp.data, hdev->adv_data, len) == 0) | |
1132 | return 0; | |
1133 | ||
1134 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); | |
1135 | hdev->adv_data_len = len; | |
1136 | ||
1137 | cp.length = len; | |
1138 | ||
1139 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA, | |
1140 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1141 | } | |
1142 | ||
1143 | int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1144 | { | |
1145 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
1146 | return 0; | |
1147 | ||
1148 | if (ext_adv_capable(hdev)) | |
1149 | return hci_set_ext_adv_data_sync(hdev, instance); | |
1150 | ||
1151 | return hci_set_adv_data_sync(hdev, instance); | |
1152 | } | |
1153 | ||
1154 | int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance, | |
1155 | bool force) | |
1156 | { | |
1157 | struct adv_info *adv = NULL; | |
1158 | u16 timeout; | |
1159 | ||
cf75ad8b | 1160 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev)) |
cba6b758 LAD |
1161 | return -EPERM; |
1162 | ||
1163 | if (hdev->adv_instance_timeout) | |
1164 | return -EBUSY; | |
1165 | ||
1166 | adv = hci_find_adv_instance(hdev, instance); | |
1167 | if (!adv) | |
1168 | return -ENOENT; | |
1169 | ||
1170 | /* A zero timeout means unlimited advertising. As long as there is | |
1171 | * only one instance, duration should be ignored. We still set a timeout | |
1172 | * in case further instances are being added later on. | |
1173 | * | |
1174 | * If the remaining lifetime of the instance is more than the duration | |
1175 | * then the timeout corresponds to the duration, otherwise it will be | |
1176 | * reduced to the remaining instance lifetime. | |
1177 | */ | |
1178 | if (adv->timeout == 0 || adv->duration <= adv->remaining_time) | |
1179 | timeout = adv->duration; | |
1180 | else | |
1181 | timeout = adv->remaining_time; | |
1182 | ||
1183 | /* The remaining time is being reduced unless the instance is being | |
1184 | * advertised without time limit. | |
1185 | */ | |
1186 | if (adv->timeout) | |
1187 | adv->remaining_time = adv->remaining_time - timeout; | |
1188 | ||
1189 | /* Only use work for scheduling instances with legacy advertising */ | |
1190 | if (!ext_adv_capable(hdev)) { | |
1191 | hdev->adv_instance_timeout = timeout; | |
1192 | queue_delayed_work(hdev->req_workqueue, | |
1193 | &hdev->adv_instance_expire, | |
1194 | msecs_to_jiffies(timeout * 1000)); | |
1195 | } | |
1196 | ||
1197 | /* If we're just re-scheduling the same instance again then do not | |
1198 | * execute any HCI commands. This happens when a single instance is | |
1199 | * being advertised. | |
1200 | */ | |
1201 | if (!force && hdev->cur_adv_instance == instance && | |
1202 | hci_dev_test_flag(hdev, HCI_LE_ADV)) | |
1203 | return 0; | |
1204 | ||
1205 | hdev->cur_adv_instance = instance; | |
1206 | ||
1207 | return hci_start_adv_sync(hdev, instance); | |
1208 | } | |
1209 | ||
1210 | static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk) | |
1211 | { | |
1212 | int err; | |
1213 | ||
1214 | if (!ext_adv_capable(hdev)) | |
1215 | return 0; | |
1216 | ||
1217 | /* Disable instance 0x00 to disable all instances */ | |
1218 | err = hci_disable_ext_adv_instance_sync(hdev, 0x00); | |
1219 | if (err) | |
1220 | return err; | |
1221 | ||
1222 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS, | |
1223 | 0, NULL, 0, HCI_CMD_TIMEOUT, sk); | |
1224 | } | |
1225 | ||
1226 | static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force) | |
1227 | { | |
1228 | struct adv_info *adv, *n; | |
1229 | ||
1230 | if (ext_adv_capable(hdev)) | |
1231 | /* Remove all existing sets */ | |
1232 | return hci_clear_adv_sets_sync(hdev, sk); | |
1233 | ||
1234 | /* This is safe as long as there is no command send while the lock is | |
1235 | * held. | |
1236 | */ | |
1237 | hci_dev_lock(hdev); | |
1238 | ||
1239 | /* Cleanup non-ext instances */ | |
1240 | list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { | |
1241 | u8 instance = adv->instance; | |
1242 | int err; | |
1243 | ||
1244 | if (!(force || adv->timeout)) | |
1245 | continue; | |
1246 | ||
1247 | err = hci_remove_adv_instance(hdev, instance); | |
1248 | if (!err) | |
1249 | mgmt_advertising_removed(sk, hdev, instance); | |
1250 | } | |
1251 | ||
1252 | hci_dev_unlock(hdev); | |
1253 | ||
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance, | |
1258 | struct sock *sk) | |
1259 | { | |
1260 | int err; | |
1261 | ||
1262 | /* If we use extended advertising, instance has to be removed first. */ | |
1263 | if (ext_adv_capable(hdev)) | |
1264 | return hci_remove_ext_adv_instance_sync(hdev, instance, sk); | |
1265 | ||
1266 | /* This is safe as long as there is no command send while the lock is | |
1267 | * held. | |
1268 | */ | |
1269 | hci_dev_lock(hdev); | |
1270 | ||
1271 | err = hci_remove_adv_instance(hdev, instance); | |
1272 | if (!err) | |
1273 | mgmt_advertising_removed(sk, hdev, instance); | |
1274 | ||
1275 | hci_dev_unlock(hdev); | |
1276 | ||
1277 | return err; | |
1278 | } | |
1279 | ||
1280 | /* For a single instance: | |
1281 | * - force == true: The instance will be removed even when its remaining | |
1282 | * lifetime is not zero. | |
1283 | * - force == false: the instance will be deactivated but kept stored unless | |
1284 | * the remaining lifetime is zero. | |
1285 | * | |
1286 | * For instance == 0x00: | |
1287 | * - force == true: All instances will be removed regardless of their timeout | |
1288 | * setting. | |
1289 | * - force == false: Only instances that have a timeout will be removed. | |
1290 | */ | |
1291 | int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk, | |
1292 | u8 instance, bool force) | |
1293 | { | |
1294 | struct adv_info *next = NULL; | |
1295 | int err; | |
1296 | ||
1297 | /* Cancel any timeout concerning the removed instance(s). */ | |
1298 | if (!instance || hdev->cur_adv_instance == instance) | |
1299 | cancel_adv_timeout(hdev); | |
1300 | ||
1301 | /* Get the next instance to advertise BEFORE we remove | |
1302 | * the current one. This can be the same instance again | |
1303 | * if there is only one instance. | |
1304 | */ | |
1305 | if (hdev->cur_adv_instance == instance) | |
1306 | next = hci_get_next_instance(hdev, instance); | |
1307 | ||
1308 | if (!instance) { | |
1309 | err = hci_clear_adv_sync(hdev, sk, force); | |
1310 | if (err) | |
1311 | return err; | |
1312 | } else { | |
1313 | struct adv_info *adv = hci_find_adv_instance(hdev, instance); | |
1314 | ||
1315 | if (force || (adv && adv->timeout && !adv->remaining_time)) { | |
1316 | /* Don't advertise a removed instance. */ | |
1317 | if (next && next->instance == instance) | |
1318 | next = NULL; | |
1319 | ||
1320 | err = hci_remove_adv_sync(hdev, instance, sk); | |
1321 | if (err) | |
1322 | return err; | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
1327 | return 0; | |
1328 | ||
1329 | if (next && !ext_adv_capable(hdev)) | |
1330 | hci_schedule_adv_instance_sync(hdev, next->instance, false); | |
1331 | ||
1332 | return 0; | |
1333 | } | |
1334 | ||
47db6b42 BG |
1335 | int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle) |
1336 | { | |
1337 | struct hci_cp_read_rssi cp; | |
1338 | ||
1339 | cp.handle = handle; | |
1340 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI, | |
1341 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1342 | } | |
1343 | ||
5a750137 BG |
1344 | int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp) |
1345 | { | |
1346 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK, | |
1347 | sizeof(*cp), cp, HCI_CMD_TIMEOUT); | |
1348 | } | |
1349 | ||
47db6b42 BG |
1350 | int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type) |
1351 | { | |
1352 | struct hci_cp_read_tx_power cp; | |
1353 | ||
1354 | cp.handle = handle; | |
1355 | cp.type = type; | |
1356 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER, | |
1357 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1358 | } | |
1359 | ||
cba6b758 LAD |
1360 | int hci_disable_advertising_sync(struct hci_dev *hdev) |
1361 | { | |
1362 | u8 enable = 0x00; | |
1363 | ||
ad383c2c LAD |
1364 | /* If controller is not advertising we are done. */ |
1365 | if (!hci_dev_test_flag(hdev, HCI_LE_ADV)) | |
1366 | return 0; | |
1367 | ||
cba6b758 LAD |
1368 | if (ext_adv_capable(hdev)) |
1369 | return hci_disable_ext_adv_instance_sync(hdev, 0x00); | |
1370 | ||
1371 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
1372 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
1373 | } | |
e8907f76 LAD |
1374 | |
1375 | static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val, | |
1376 | u8 filter_dup) | |
1377 | { | |
1378 | struct hci_cp_le_set_ext_scan_enable cp; | |
1379 | ||
1380 | memset(&cp, 0, sizeof(cp)); | |
1381 | cp.enable = val; | |
1382 | cp.filter_dup = filter_dup; | |
1383 | ||
1384 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE, | |
1385 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1386 | } | |
1387 | ||
1388 | static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val, | |
1389 | u8 filter_dup) | |
1390 | { | |
1391 | struct hci_cp_le_set_scan_enable cp; | |
1392 | ||
1393 | if (use_ext_scan(hdev)) | |
1394 | return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup); | |
1395 | ||
1396 | memset(&cp, 0, sizeof(cp)); | |
1397 | cp.enable = val; | |
1398 | cp.filter_dup = filter_dup; | |
1399 | ||
1400 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE, | |
1401 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1402 | } | |
1403 | ||
1404 | static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val) | |
1405 | { | |
ad383c2c LAD |
1406 | if (!use_ll_privacy(hdev)) |
1407 | return 0; | |
1408 | ||
1409 | /* If controller is not/already resolving we are done. */ | |
1410 | if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) | |
e8907f76 LAD |
1411 | return 0; |
1412 | ||
1413 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, | |
1414 | sizeof(val), &val, HCI_CMD_TIMEOUT); | |
1415 | } | |
1416 | ||
27592ca1 | 1417 | static int hci_scan_disable_sync(struct hci_dev *hdev) |
e8907f76 LAD |
1418 | { |
1419 | int err; | |
1420 | ||
1421 | /* If controller is not scanning we are done. */ | |
1422 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) | |
1423 | return 0; | |
1424 | ||
1425 | if (hdev->scanning_paused) { | |
1426 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
1427 | return 0; | |
1428 | } | |
1429 | ||
e8907f76 LAD |
1430 | err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00); |
1431 | if (err) { | |
1432 | bt_dev_err(hdev, "Unable to disable scanning: %d", err); | |
1433 | return err; | |
1434 | } | |
1435 | ||
e8907f76 LAD |
1436 | return err; |
1437 | } | |
1438 | ||
1439 | static bool scan_use_rpa(struct hci_dev *hdev) | |
1440 | { | |
1441 | return hci_dev_test_flag(hdev, HCI_PRIVACY); | |
1442 | } | |
1443 | ||
1444 | static void hci_start_interleave_scan(struct hci_dev *hdev) | |
1445 | { | |
1446 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; | |
1447 | queue_delayed_work(hdev->req_workqueue, | |
1448 | &hdev->interleave_scan, 0); | |
1449 | } | |
1450 | ||
1451 | static bool is_interleave_scanning(struct hci_dev *hdev) | |
1452 | { | |
1453 | return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE; | |
1454 | } | |
1455 | ||
1456 | static void cancel_interleave_scan(struct hci_dev *hdev) | |
1457 | { | |
1458 | bt_dev_dbg(hdev, "cancelling interleave scan"); | |
1459 | ||
1460 | cancel_delayed_work_sync(&hdev->interleave_scan); | |
1461 | ||
1462 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE; | |
1463 | } | |
1464 | ||
1465 | /* Return true if interleave_scan wasn't started until exiting this function, | |
1466 | * otherwise, return false | |
1467 | */ | |
1468 | static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev) | |
1469 | { | |
1470 | /* Do interleaved scan only if all of the following are true: | |
1471 | * - There is at least one ADV monitor | |
1472 | * - At least one pending LE connection or one device to be scanned for | |
1473 | * - Monitor offloading is not supported | |
1474 | * If so, we should alternate between allowlist scan and one without | |
1475 | * any filters to save power. | |
1476 | */ | |
1477 | bool use_interleaving = hci_is_adv_monitoring(hdev) && | |
1478 | !(list_empty(&hdev->pend_le_conns) && | |
1479 | list_empty(&hdev->pend_le_reports)) && | |
1480 | hci_get_adv_monitor_offload_ext(hdev) == | |
1481 | HCI_ADV_MONITOR_EXT_NONE; | |
1482 | bool is_interleaving = is_interleave_scanning(hdev); | |
1483 | ||
1484 | if (use_interleaving && !is_interleaving) { | |
1485 | hci_start_interleave_scan(hdev); | |
1486 | bt_dev_dbg(hdev, "starting interleave scan"); | |
1487 | return true; | |
1488 | } | |
1489 | ||
1490 | if (!use_interleaving && is_interleaving) | |
1491 | cancel_interleave_scan(hdev); | |
1492 | ||
1493 | return false; | |
1494 | } | |
1495 | ||
1496 | /* Removes connection to resolve list if needed.*/ | |
1497 | static int hci_le_del_resolve_list_sync(struct hci_dev *hdev, | |
1498 | bdaddr_t *bdaddr, u8 bdaddr_type) | |
1499 | { | |
1500 | struct hci_cp_le_del_from_resolv_list cp; | |
1501 | struct bdaddr_list_with_irk *entry; | |
1502 | ||
ad383c2c | 1503 | if (!use_ll_privacy(hdev)) |
e8907f76 LAD |
1504 | return 0; |
1505 | ||
1506 | /* Check if the IRK has been programmed */ | |
1507 | entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr, | |
1508 | bdaddr_type); | |
1509 | if (!entry) | |
1510 | return 0; | |
1511 | ||
1512 | cp.bdaddr_type = bdaddr_type; | |
1513 | bacpy(&cp.bdaddr, bdaddr); | |
1514 | ||
1515 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST, | |
1516 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1517 | } | |
1518 | ||
1519 | static int hci_le_del_accept_list_sync(struct hci_dev *hdev, | |
1520 | bdaddr_t *bdaddr, u8 bdaddr_type) | |
1521 | { | |
1522 | struct hci_cp_le_del_from_accept_list cp; | |
1523 | int err; | |
1524 | ||
1525 | /* Check if device is on accept list before removing it */ | |
1526 | if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type)) | |
1527 | return 0; | |
1528 | ||
1529 | cp.bdaddr_type = bdaddr_type; | |
1530 | bacpy(&cp.bdaddr, bdaddr); | |
1531 | ||
ad383c2c LAD |
1532 | /* Ignore errors when removing from resolving list as that is likely |
1533 | * that the device was never added. | |
1534 | */ | |
1535 | hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); | |
1536 | ||
e8907f76 LAD |
1537 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, |
1538 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1539 | if (err) { | |
1540 | bt_dev_err(hdev, "Unable to remove from allow list: %d", err); | |
1541 | return err; | |
1542 | } | |
1543 | ||
1544 | bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr, | |
1545 | cp.bdaddr_type); | |
1546 | ||
ad383c2c | 1547 | return 0; |
e8907f76 LAD |
1548 | } |
1549 | ||
cf75ad8b LAD |
1550 | /* Adds connection to resolve list if needed. |
1551 | * Setting params to NULL programs local hdev->irk | |
1552 | */ | |
e8907f76 LAD |
1553 | static int hci_le_add_resolve_list_sync(struct hci_dev *hdev, |
1554 | struct hci_conn_params *params) | |
1555 | { | |
1556 | struct hci_cp_le_add_to_resolv_list cp; | |
1557 | struct smp_irk *irk; | |
1558 | struct bdaddr_list_with_irk *entry; | |
1559 | ||
ad383c2c | 1560 | if (!use_ll_privacy(hdev)) |
e8907f76 LAD |
1561 | return 0; |
1562 | ||
cf75ad8b LAD |
1563 | /* Attempt to program local identity address, type and irk if params is |
1564 | * NULL. | |
1565 | */ | |
1566 | if (!params) { | |
1567 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) | |
1568 | return 0; | |
1569 | ||
1570 | hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type); | |
1571 | memcpy(cp.peer_irk, hdev->irk, 16); | |
1572 | goto done; | |
1573 | } | |
1574 | ||
e8907f76 LAD |
1575 | irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); |
1576 | if (!irk) | |
1577 | return 0; | |
1578 | ||
1579 | /* Check if the IK has _not_ been programmed yet. */ | |
1580 | entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, | |
1581 | ¶ms->addr, | |
1582 | params->addr_type); | |
1583 | if (entry) | |
1584 | return 0; | |
1585 | ||
1586 | cp.bdaddr_type = params->addr_type; | |
1587 | bacpy(&cp.bdaddr, ¶ms->addr); | |
1588 | memcpy(cp.peer_irk, irk->val, 16); | |
1589 | ||
cf75ad8b | 1590 | done: |
e8907f76 LAD |
1591 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
1592 | memcpy(cp.local_irk, hdev->irk, 16); | |
1593 | else | |
1594 | memset(cp.local_irk, 0, 16); | |
1595 | ||
1596 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST, | |
1597 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1598 | } | |
1599 | ||
1600 | /* Adds connection to allow list if needed, if the device uses RPA (has IRK) | |
1601 | * this attempts to program the device in the resolving list as well. | |
1602 | */ | |
1603 | static int hci_le_add_accept_list_sync(struct hci_dev *hdev, | |
1604 | struct hci_conn_params *params, | |
ad383c2c | 1605 | u8 *num_entries) |
e8907f76 LAD |
1606 | { |
1607 | struct hci_cp_le_add_to_accept_list cp; | |
1608 | int err; | |
1609 | ||
1610 | /* Already in accept list */ | |
1611 | if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr, | |
1612 | params->addr_type)) | |
1613 | return 0; | |
1614 | ||
1615 | /* Select filter policy to accept all advertising */ | |
1616 | if (*num_entries >= hdev->le_accept_list_size) | |
1617 | return -ENOSPC; | |
1618 | ||
1619 | /* Accept list can not be used with RPAs */ | |
ad383c2c | 1620 | if (!use_ll_privacy(hdev) && |
e8907f76 LAD |
1621 | hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) { |
1622 | return -EINVAL; | |
1623 | } | |
1624 | ||
1625 | /* During suspend, only wakeable devices can be in acceptlist */ | |
fe92ee64 LAD |
1626 | if (hdev->suspended && |
1627 | !test_bit(HCI_CONN_FLAG_REMOTE_WAKEUP, params->flags)) | |
e8907f76 LAD |
1628 | return 0; |
1629 | ||
ad383c2c LAD |
1630 | /* Attempt to program the device in the resolving list first to avoid |
1631 | * having to rollback in case it fails since the resolving list is | |
1632 | * dynamic it can probably be smaller than the accept list. | |
1633 | */ | |
1634 | err = hci_le_add_resolve_list_sync(hdev, params); | |
1635 | if (err) { | |
1636 | bt_dev_err(hdev, "Unable to add to resolve list: %d", err); | |
1637 | return err; | |
1638 | } | |
1639 | ||
e8907f76 LAD |
1640 | *num_entries += 1; |
1641 | cp.bdaddr_type = params->addr_type; | |
1642 | bacpy(&cp.bdaddr, ¶ms->addr); | |
1643 | ||
1644 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST, | |
1645 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1646 | if (err) { | |
1647 | bt_dev_err(hdev, "Unable to add to allow list: %d", err); | |
ad383c2c LAD |
1648 | /* Rollback the device from the resolving list */ |
1649 | hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); | |
e8907f76 LAD |
1650 | return err; |
1651 | } | |
1652 | ||
1653 | bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr, | |
1654 | cp.bdaddr_type); | |
1655 | ||
ad383c2c LAD |
1656 | return 0; |
1657 | } | |
1658 | ||
182ee45d | 1659 | /* This function disables/pause all advertising instances */ |
ad383c2c LAD |
1660 | static int hci_pause_advertising_sync(struct hci_dev *hdev) |
1661 | { | |
1662 | int err; | |
182ee45d | 1663 | int old_state; |
ad383c2c LAD |
1664 | |
1665 | /* If there are no instances or advertising has already been paused | |
1666 | * there is nothing to do. | |
1667 | */ | |
1668 | if (!hdev->adv_instance_cnt || hdev->advertising_paused) | |
1669 | return 0; | |
1670 | ||
182ee45d LAD |
1671 | bt_dev_dbg(hdev, "Pausing directed advertising"); |
1672 | ||
1673 | /* Stop directed advertising */ | |
1674 | old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING); | |
1675 | if (old_state) { | |
1676 | /* When discoverable timeout triggers, then just make sure | |
1677 | * the limited discoverable flag is cleared. Even in the case | |
1678 | * of a timeout triggered from general discoverable, it is | |
1679 | * safe to unconditionally clear the flag. | |
1680 | */ | |
1681 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); | |
1682 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
1683 | hdev->discov_timeout = 0; | |
1684 | } | |
1685 | ||
ad383c2c LAD |
1686 | bt_dev_dbg(hdev, "Pausing advertising instances"); |
1687 | ||
1688 | /* Call to disable any advertisements active on the controller. | |
1689 | * This will succeed even if no advertisements are configured. | |
1690 | */ | |
1691 | err = hci_disable_advertising_sync(hdev); | |
1692 | if (err) | |
1693 | return err; | |
1694 | ||
1695 | /* If we are using software rotation, pause the loop */ | |
1696 | if (!ext_adv_capable(hdev)) | |
1697 | cancel_adv_timeout(hdev); | |
1698 | ||
1699 | hdev->advertising_paused = true; | |
182ee45d | 1700 | hdev->advertising_old_state = old_state; |
ad383c2c LAD |
1701 | |
1702 | return 0; | |
e8907f76 LAD |
1703 | } |
1704 | ||
182ee45d | 1705 | /* This function enables all user advertising instances */ |
ad383c2c LAD |
1706 | static int hci_resume_advertising_sync(struct hci_dev *hdev) |
1707 | { | |
1708 | struct adv_info *adv, *tmp; | |
1709 | int err; | |
1710 | ||
1711 | /* If advertising has not been paused there is nothing to do. */ | |
1712 | if (!hdev->advertising_paused) | |
1713 | return 0; | |
1714 | ||
182ee45d LAD |
1715 | /* Resume directed advertising */ |
1716 | hdev->advertising_paused = false; | |
1717 | if (hdev->advertising_old_state) { | |
1718 | hci_dev_set_flag(hdev, HCI_ADVERTISING); | |
182ee45d LAD |
1719 | hdev->advertising_old_state = 0; |
1720 | } | |
1721 | ||
ad383c2c LAD |
1722 | bt_dev_dbg(hdev, "Resuming advertising instances"); |
1723 | ||
1724 | if (ext_adv_capable(hdev)) { | |
1725 | /* Call for each tracked instance to be re-enabled */ | |
1726 | list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) { | |
1727 | err = hci_enable_ext_advertising_sync(hdev, | |
1728 | adv->instance); | |
1729 | if (!err) | |
1730 | continue; | |
1731 | ||
1732 | /* If the instance cannot be resumed remove it */ | |
1733 | hci_remove_ext_adv_instance_sync(hdev, adv->instance, | |
1734 | NULL); | |
1735 | } | |
1736 | } else { | |
1737 | /* Schedule for most recent instance to be restarted and begin | |
1738 | * the software rotation loop | |
1739 | */ | |
1740 | err = hci_schedule_adv_instance_sync(hdev, | |
1741 | hdev->cur_adv_instance, | |
1742 | true); | |
1743 | } | |
1744 | ||
1745 | hdev->advertising_paused = false; | |
1746 | ||
1747 | return err; | |
1748 | } | |
1749 | ||
f892244b BG |
1750 | struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev, |
1751 | bool extended, struct sock *sk) | |
1752 | { | |
1753 | u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA : | |
1754 | HCI_OP_READ_LOCAL_OOB_DATA; | |
1755 | ||
1756 | return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk); | |
1757 | } | |
1758 | ||
ad383c2c LAD |
1759 | /* Device must not be scanning when updating the accept list. |
1760 | * | |
1761 | * Update is done using the following sequence: | |
1762 | * | |
1763 | * use_ll_privacy((Disable Advertising) -> Disable Resolving List) -> | |
1764 | * Remove Devices From Accept List -> | |
1765 | * (has IRK && use_ll_privacy(Remove Devices From Resolving List))-> | |
1766 | * Add Devices to Accept List -> | |
1767 | * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) -> | |
1768 | * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) -> | |
1769 | * Enable Scanning | |
1770 | * | |
1771 | * In case of failure advertising shall be restored to its original state and | |
1772 | * return would disable accept list since either accept or resolving list could | |
1773 | * not be programmed. | |
1774 | * | |
1775 | */ | |
e8907f76 LAD |
1776 | static u8 hci_update_accept_list_sync(struct hci_dev *hdev) |
1777 | { | |
1778 | struct hci_conn_params *params; | |
1779 | struct bdaddr_list *b, *t; | |
1780 | u8 num_entries = 0; | |
1781 | bool pend_conn, pend_report; | |
ad383c2c LAD |
1782 | int err; |
1783 | ||
1784 | /* Pause advertising if resolving list can be used as controllers are | |
1785 | * cannot accept resolving list modifications while advertising. | |
e8907f76 | 1786 | */ |
ad383c2c LAD |
1787 | if (use_ll_privacy(hdev)) { |
1788 | err = hci_pause_advertising_sync(hdev); | |
1789 | if (err) { | |
1790 | bt_dev_err(hdev, "pause advertising failed: %d", err); | |
1791 | return 0x00; | |
1792 | } | |
1793 | } | |
e8907f76 | 1794 | |
ad383c2c LAD |
1795 | /* Disable address resolution while reprogramming accept list since |
1796 | * devices that do have an IRK will be programmed in the resolving list | |
1797 | * when LL Privacy is enabled. | |
1798 | */ | |
1799 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); | |
1800 | if (err) { | |
1801 | bt_dev_err(hdev, "Unable to disable LL privacy: %d", err); | |
1802 | goto done; | |
1803 | } | |
e8907f76 LAD |
1804 | |
1805 | /* Go through the current accept list programmed into the | |
1806 | * controller one by one and check if that address is still | |
1807 | * in the list of pending connections or list of devices to | |
1808 | * report. If not present in either list, then remove it from | |
1809 | * the controller. | |
1810 | */ | |
1811 | list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) { | |
1812 | pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns, | |
1813 | &b->bdaddr, | |
1814 | b->bdaddr_type); | |
1815 | pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports, | |
1816 | &b->bdaddr, | |
1817 | b->bdaddr_type); | |
1818 | ||
1819 | /* If the device is not likely to connect or report, | |
1820 | * remove it from the acceptlist. | |
1821 | */ | |
1822 | if (!pend_conn && !pend_report) { | |
1823 | hci_le_del_accept_list_sync(hdev, &b->bdaddr, | |
1824 | b->bdaddr_type); | |
1825 | continue; | |
1826 | } | |
1827 | ||
e8907f76 LAD |
1828 | num_entries++; |
1829 | } | |
1830 | ||
1831 | /* Since all no longer valid accept list entries have been | |
1832 | * removed, walk through the list of pending connections | |
1833 | * and ensure that any new device gets programmed into | |
1834 | * the controller. | |
1835 | * | |
1836 | * If the list of the devices is larger than the list of | |
1837 | * available accept list entries in the controller, then | |
1838 | * just abort and return filer policy value to not use the | |
1839 | * accept list. | |
1840 | */ | |
1841 | list_for_each_entry(params, &hdev->pend_le_conns, action) { | |
ad383c2c LAD |
1842 | err = hci_le_add_accept_list_sync(hdev, params, &num_entries); |
1843 | if (err) | |
1844 | goto done; | |
e8907f76 LAD |
1845 | } |
1846 | ||
1847 | /* After adding all new pending connections, walk through | |
1848 | * the list of pending reports and also add these to the | |
1849 | * accept list if there is still space. Abort if space runs out. | |
1850 | */ | |
1851 | list_for_each_entry(params, &hdev->pend_le_reports, action) { | |
ad383c2c LAD |
1852 | err = hci_le_add_accept_list_sync(hdev, params, &num_entries); |
1853 | if (err) | |
1854 | goto done; | |
e8907f76 LAD |
1855 | } |
1856 | ||
1857 | /* Use the allowlist unless the following conditions are all true: | |
1858 | * - We are not currently suspending | |
1859 | * - There are 1 or more ADV monitors registered and it's not offloaded | |
1860 | * - Interleaved scanning is not currently using the allowlist | |
1861 | */ | |
1862 | if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended && | |
1863 | hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE && | |
1864 | hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST) | |
ad383c2c LAD |
1865 | err = -EINVAL; |
1866 | ||
1867 | done: | |
1868 | /* Enable address resolution when LL Privacy is enabled. */ | |
1869 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01); | |
1870 | if (err) | |
1871 | bt_dev_err(hdev, "Unable to enable LL privacy: %d", err); | |
1872 | ||
1873 | /* Resume advertising if it was paused */ | |
1874 | if (use_ll_privacy(hdev)) | |
1875 | hci_resume_advertising_sync(hdev); | |
e8907f76 LAD |
1876 | |
1877 | /* Select filter policy to use accept list */ | |
ad383c2c | 1878 | return err ? 0x00 : 0x01; |
e8907f76 LAD |
1879 | } |
1880 | ||
1881 | /* Returns true if an le connection is in the scanning state */ | |
1882 | static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev) | |
1883 | { | |
1884 | struct hci_conn_hash *h = &hdev->conn_hash; | |
1885 | struct hci_conn *c; | |
1886 | ||
1887 | rcu_read_lock(); | |
1888 | ||
1889 | list_for_each_entry_rcu(c, &h->list, list) { | |
1890 | if (c->type == LE_LINK && c->state == BT_CONNECT && | |
1891 | test_bit(HCI_CONN_SCANNING, &c->flags)) { | |
1892 | rcu_read_unlock(); | |
1893 | return true; | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | rcu_read_unlock(); | |
1898 | ||
1899 | return false; | |
1900 | } | |
1901 | ||
1902 | static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type, | |
1903 | u16 interval, u16 window, | |
1904 | u8 own_addr_type, u8 filter_policy) | |
1905 | { | |
1906 | struct hci_cp_le_set_ext_scan_params *cp; | |
1907 | struct hci_cp_le_scan_phy_params *phy; | |
1908 | u8 data[sizeof(*cp) + sizeof(*phy) * 2]; | |
1909 | u8 num_phy = 0; | |
1910 | ||
1911 | cp = (void *)data; | |
1912 | phy = (void *)cp->data; | |
1913 | ||
1914 | memset(data, 0, sizeof(data)); | |
1915 | ||
1916 | cp->own_addr_type = own_addr_type; | |
1917 | cp->filter_policy = filter_policy; | |
1918 | ||
1919 | if (scan_1m(hdev) || scan_2m(hdev)) { | |
1920 | cp->scanning_phys |= LE_SCAN_PHY_1M; | |
1921 | ||
1922 | phy->type = type; | |
1923 | phy->interval = cpu_to_le16(interval); | |
1924 | phy->window = cpu_to_le16(window); | |
1925 | ||
1926 | num_phy++; | |
1927 | phy++; | |
1928 | } | |
1929 | ||
1930 | if (scan_coded(hdev)) { | |
1931 | cp->scanning_phys |= LE_SCAN_PHY_CODED; | |
1932 | ||
1933 | phy->type = type; | |
1934 | phy->interval = cpu_to_le16(interval); | |
1935 | phy->window = cpu_to_le16(window); | |
1936 | ||
1937 | num_phy++; | |
1938 | phy++; | |
1939 | } | |
1940 | ||
1941 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS, | |
1942 | sizeof(*cp) + sizeof(*phy) * num_phy, | |
1943 | data, HCI_CMD_TIMEOUT); | |
1944 | } | |
1945 | ||
1946 | static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type, | |
1947 | u16 interval, u16 window, | |
1948 | u8 own_addr_type, u8 filter_policy) | |
1949 | { | |
1950 | struct hci_cp_le_set_scan_param cp; | |
1951 | ||
1952 | if (use_ext_scan(hdev)) | |
1953 | return hci_le_set_ext_scan_param_sync(hdev, type, interval, | |
1954 | window, own_addr_type, | |
1955 | filter_policy); | |
1956 | ||
1957 | memset(&cp, 0, sizeof(cp)); | |
1958 | cp.type = type; | |
1959 | cp.interval = cpu_to_le16(interval); | |
1960 | cp.window = cpu_to_le16(window); | |
1961 | cp.own_address_type = own_addr_type; | |
1962 | cp.filter_policy = filter_policy; | |
1963 | ||
1964 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM, | |
1965 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1966 | } | |
1967 | ||
1968 | static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval, | |
abfeea47 LAD |
1969 | u16 window, u8 own_addr_type, u8 filter_policy, |
1970 | u8 filter_dup) | |
e8907f76 LAD |
1971 | { |
1972 | int err; | |
1973 | ||
1974 | if (hdev->scanning_paused) { | |
1975 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
1976 | return 0; | |
1977 | } | |
1978 | ||
e8907f76 LAD |
1979 | err = hci_le_set_scan_param_sync(hdev, type, interval, window, |
1980 | own_addr_type, filter_policy); | |
1981 | if (err) | |
1982 | return err; | |
1983 | ||
abfeea47 | 1984 | return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup); |
e8907f76 LAD |
1985 | } |
1986 | ||
27592ca1 | 1987 | static int hci_passive_scan_sync(struct hci_dev *hdev) |
e8907f76 LAD |
1988 | { |
1989 | u8 own_addr_type; | |
1990 | u8 filter_policy; | |
1991 | u16 window, interval; | |
ad383c2c | 1992 | int err; |
e8907f76 LAD |
1993 | |
1994 | if (hdev->scanning_paused) { | |
1995 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
1996 | return 0; | |
1997 | } | |
1998 | ||
ad383c2c LAD |
1999 | err = hci_scan_disable_sync(hdev); |
2000 | if (err) { | |
2001 | bt_dev_err(hdev, "disable scanning failed: %d", err); | |
2002 | return err; | |
2003 | } | |
2004 | ||
e8907f76 LAD |
2005 | /* Set require_privacy to false since no SCAN_REQ are send |
2006 | * during passive scanning. Not using an non-resolvable address | |
2007 | * here is important so that peer devices using direct | |
2008 | * advertising with our address will be correctly reported | |
2009 | * by the controller. | |
2010 | */ | |
2011 | if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev), | |
2012 | &own_addr_type)) | |
2013 | return 0; | |
2014 | ||
2015 | if (hdev->enable_advmon_interleave_scan && | |
2016 | hci_update_interleaved_scan_sync(hdev)) | |
2017 | return 0; | |
2018 | ||
2019 | bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state); | |
ad383c2c | 2020 | |
e8907f76 LAD |
2021 | /* Adding or removing entries from the accept list must |
2022 | * happen before enabling scanning. The controller does | |
2023 | * not allow accept list modification while scanning. | |
2024 | */ | |
2025 | filter_policy = hci_update_accept_list_sync(hdev); | |
2026 | ||
2027 | /* When the controller is using random resolvable addresses and | |
2028 | * with that having LE privacy enabled, then controllers with | |
2029 | * Extended Scanner Filter Policies support can now enable support | |
2030 | * for handling directed advertising. | |
2031 | * | |
2032 | * So instead of using filter polices 0x00 (no acceptlist) | |
2033 | * and 0x01 (acceptlist enabled) use the new filter policies | |
2034 | * 0x02 (no acceptlist) and 0x03 (acceptlist enabled). | |
2035 | */ | |
2036 | if (hci_dev_test_flag(hdev, HCI_PRIVACY) && | |
2037 | (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) | |
2038 | filter_policy |= 0x02; | |
2039 | ||
2040 | if (hdev->suspended) { | |
2041 | window = hdev->le_scan_window_suspend; | |
2042 | interval = hdev->le_scan_int_suspend; | |
e8907f76 LAD |
2043 | } else if (hci_is_le_conn_scanning(hdev)) { |
2044 | window = hdev->le_scan_window_connect; | |
2045 | interval = hdev->le_scan_int_connect; | |
2046 | } else if (hci_is_adv_monitoring(hdev)) { | |
2047 | window = hdev->le_scan_window_adv_monitor; | |
2048 | interval = hdev->le_scan_int_adv_monitor; | |
2049 | } else { | |
2050 | window = hdev->le_scan_window; | |
2051 | interval = hdev->le_scan_interval; | |
2052 | } | |
2053 | ||
2054 | bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy); | |
2055 | ||
2056 | return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window, | |
abfeea47 LAD |
2057 | own_addr_type, filter_policy, |
2058 | LE_SCAN_FILTER_DUP_ENABLE); | |
e8907f76 LAD |
2059 | } |
2060 | ||
2061 | /* This function controls the passive scanning based on hdev->pend_le_conns | |
2062 | * list. If there are pending LE connection we start the background scanning, | |
ad383c2c LAD |
2063 | * otherwise we stop it in the following sequence: |
2064 | * | |
2065 | * If there are devices to scan: | |
2066 | * | |
2067 | * Disable Scanning -> Update Accept List -> | |
2068 | * use_ll_privacy((Disable Advertising) -> Disable Resolving List -> | |
2069 | * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) -> | |
2070 | * Enable Scanning | |
2071 | * | |
2072 | * Otherwise: | |
2073 | * | |
2074 | * Disable Scanning | |
e8907f76 LAD |
2075 | */ |
2076 | int hci_update_passive_scan_sync(struct hci_dev *hdev) | |
2077 | { | |
2078 | int err; | |
2079 | ||
2080 | if (!test_bit(HCI_UP, &hdev->flags) || | |
2081 | test_bit(HCI_INIT, &hdev->flags) || | |
2082 | hci_dev_test_flag(hdev, HCI_SETUP) || | |
2083 | hci_dev_test_flag(hdev, HCI_CONFIG) || | |
2084 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || | |
2085 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) | |
2086 | return 0; | |
2087 | ||
2088 | /* No point in doing scanning if LE support hasn't been enabled */ | |
2089 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
2090 | return 0; | |
2091 | ||
2092 | /* If discovery is active don't interfere with it */ | |
2093 | if (hdev->discovery.state != DISCOVERY_STOPPED) | |
2094 | return 0; | |
2095 | ||
2096 | /* Reset RSSI and UUID filters when starting background scanning | |
2097 | * since these filters are meant for service discovery only. | |
2098 | * | |
2099 | * The Start Discovery and Start Service Discovery operations | |
2100 | * ensure to set proper values for RSSI threshold and UUID | |
2101 | * filter list. So it is safe to just reset them here. | |
2102 | */ | |
2103 | hci_discovery_filter_clear(hdev); | |
2104 | ||
2105 | bt_dev_dbg(hdev, "ADV monitoring is %s", | |
2106 | hci_is_adv_monitoring(hdev) ? "on" : "off"); | |
2107 | ||
2108 | if (list_empty(&hdev->pend_le_conns) && | |
2109 | list_empty(&hdev->pend_le_reports) && | |
2110 | !hci_is_adv_monitoring(hdev)) { | |
2111 | /* If there is no pending LE connections or devices | |
2112 | * to be scanned for or no ADV monitors, we should stop the | |
2113 | * background scanning. | |
2114 | */ | |
2115 | ||
2116 | bt_dev_dbg(hdev, "stopping background scanning"); | |
2117 | ||
ad383c2c | 2118 | err = hci_scan_disable_sync(hdev); |
e8907f76 LAD |
2119 | if (err) |
2120 | bt_dev_err(hdev, "stop background scanning failed: %d", | |
2121 | err); | |
2122 | } else { | |
2123 | /* If there is at least one pending LE connection, we should | |
2124 | * keep the background scan running. | |
2125 | */ | |
2126 | ||
2127 | /* If controller is connecting, we should not start scanning | |
2128 | * since some controllers are not able to scan and connect at | |
2129 | * the same time. | |
2130 | */ | |
2131 | if (hci_lookup_le_connect(hdev)) | |
2132 | return 0; | |
2133 | ||
e8907f76 LAD |
2134 | bt_dev_dbg(hdev, "start background scanning"); |
2135 | ||
2136 | err = hci_passive_scan_sync(hdev); | |
2137 | if (err) | |
2138 | bt_dev_err(hdev, "start background scanning failed: %d", | |
2139 | err); | |
2140 | } | |
2141 | ||
2142 | return err; | |
2143 | } | |
ad383c2c LAD |
2144 | |
2145 | static int update_passive_scan_sync(struct hci_dev *hdev, void *data) | |
2146 | { | |
2147 | return hci_update_passive_scan_sync(hdev); | |
2148 | } | |
2149 | ||
2150 | int hci_update_passive_scan(struct hci_dev *hdev) | |
2151 | { | |
5bee2fd6 LAD |
2152 | /* Only queue if it would have any effect */ |
2153 | if (!test_bit(HCI_UP, &hdev->flags) || | |
2154 | test_bit(HCI_INIT, &hdev->flags) || | |
2155 | hci_dev_test_flag(hdev, HCI_SETUP) || | |
2156 | hci_dev_test_flag(hdev, HCI_CONFIG) || | |
2157 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || | |
2158 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) | |
2159 | return 0; | |
2160 | ||
ad383c2c LAD |
2161 | return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL); |
2162 | } | |
cf75ad8b | 2163 | |
2f2eb0c9 | 2164 | int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val) |
cf75ad8b | 2165 | { |
2f2eb0c9 BG |
2166 | int err; |
2167 | ||
cf75ad8b LAD |
2168 | if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev)) |
2169 | return 0; | |
2170 | ||
2f2eb0c9 | 2171 | err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, |
cf75ad8b | 2172 | sizeof(val), &val, HCI_CMD_TIMEOUT); |
2f2eb0c9 BG |
2173 | |
2174 | if (!err) { | |
2175 | if (val) { | |
2176 | hdev->features[1][0] |= LMP_HOST_SC; | |
2177 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); | |
2178 | } else { | |
2179 | hdev->features[1][0] &= ~LMP_HOST_SC; | |
2180 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | return err; | |
cf75ad8b LAD |
2185 | } |
2186 | ||
3244845c | 2187 | int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode) |
cf75ad8b LAD |
2188 | { |
2189 | int err; | |
2190 | ||
2191 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || | |
2192 | lmp_host_ssp_capable(hdev)) | |
2193 | return 0; | |
2194 | ||
3244845c BG |
2195 | if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) { |
2196 | __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, | |
2197 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
2198 | } | |
2199 | ||
cf75ad8b LAD |
2200 | err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, |
2201 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
2202 | if (err) | |
2203 | return err; | |
2204 | ||
2205 | return hci_write_sc_support_sync(hdev, 0x01); | |
2206 | } | |
2207 | ||
d81a494c | 2208 | int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul) |
cf75ad8b LAD |
2209 | { |
2210 | struct hci_cp_write_le_host_supported cp; | |
2211 | ||
2212 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) || | |
2213 | !lmp_bredr_capable(hdev)) | |
2214 | return 0; | |
2215 | ||
2216 | /* Check first if we already have the right host state | |
2217 | * (host features set) | |
2218 | */ | |
2219 | if (le == lmp_host_le_capable(hdev) && | |
2220 | simul == lmp_host_le_br_capable(hdev)) | |
2221 | return 0; | |
2222 | ||
2223 | memset(&cp, 0, sizeof(cp)); | |
2224 | ||
2225 | cp.le = le; | |
2226 | cp.simul = simul; | |
2227 | ||
2228 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, | |
2229 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2230 | } | |
2231 | ||
2232 | static int hci_powered_update_adv_sync(struct hci_dev *hdev) | |
2233 | { | |
2234 | struct adv_info *adv, *tmp; | |
2235 | int err; | |
2236 | ||
2237 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
2238 | return 0; | |
2239 | ||
2240 | /* If RPA Resolution has not been enable yet it means the | |
2241 | * resolving list is empty and we should attempt to program the | |
2242 | * local IRK in order to support using own_addr_type | |
2243 | * ADDR_LE_DEV_RANDOM_RESOLVED (0x03). | |
2244 | */ | |
2245 | if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) { | |
2246 | hci_le_add_resolve_list_sync(hdev, NULL); | |
2247 | hci_le_set_addr_resolution_enable_sync(hdev, 0x01); | |
2248 | } | |
2249 | ||
2250 | /* Make sure the controller has a good default for | |
2251 | * advertising data. This also applies to the case | |
2252 | * where BR/EDR was toggled during the AUTO_OFF phase. | |
2253 | */ | |
2254 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || | |
2255 | list_empty(&hdev->adv_instances)) { | |
2256 | if (ext_adv_capable(hdev)) { | |
2257 | err = hci_setup_ext_adv_instance_sync(hdev, 0x00); | |
2258 | if (!err) | |
2259 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
2260 | } else { | |
2261 | err = hci_update_adv_data_sync(hdev, 0x00); | |
2262 | if (!err) | |
2263 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
2264 | } | |
2265 | ||
2266 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
2267 | hci_enable_advertising_sync(hdev); | |
2268 | } | |
2269 | ||
2270 | /* Call for each tracked instance to be scheduled */ | |
2271 | list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) | |
2272 | hci_schedule_adv_instance_sync(hdev, adv->instance, true); | |
2273 | ||
2274 | return 0; | |
2275 | } | |
2276 | ||
2277 | static int hci_write_auth_enable_sync(struct hci_dev *hdev) | |
2278 | { | |
2279 | u8 link_sec; | |
2280 | ||
2281 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); | |
2282 | if (link_sec == test_bit(HCI_AUTH, &hdev->flags)) | |
2283 | return 0; | |
2284 | ||
2285 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE, | |
2286 | sizeof(link_sec), &link_sec, | |
2287 | HCI_CMD_TIMEOUT); | |
2288 | } | |
2289 | ||
353a0249 | 2290 | int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable) |
cf75ad8b LAD |
2291 | { |
2292 | struct hci_cp_write_page_scan_activity cp; | |
2293 | u8 type; | |
2294 | int err = 0; | |
2295 | ||
2296 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
2297 | return 0; | |
2298 | ||
2299 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
2300 | return 0; | |
2301 | ||
2302 | memset(&cp, 0, sizeof(cp)); | |
2303 | ||
2304 | if (enable) { | |
2305 | type = PAGE_SCAN_TYPE_INTERLACED; | |
2306 | ||
2307 | /* 160 msec page scan interval */ | |
2308 | cp.interval = cpu_to_le16(0x0100); | |
2309 | } else { | |
2310 | type = hdev->def_page_scan_type; | |
2311 | cp.interval = cpu_to_le16(hdev->def_page_scan_int); | |
2312 | } | |
2313 | ||
2314 | cp.window = cpu_to_le16(hdev->def_page_scan_window); | |
2315 | ||
2316 | if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval || | |
2317 | __cpu_to_le16(hdev->page_scan_window) != cp.window) { | |
2318 | err = __hci_cmd_sync_status(hdev, | |
2319 | HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, | |
2320 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2321 | if (err) | |
2322 | return err; | |
2323 | } | |
2324 | ||
2325 | if (hdev->page_scan_type != type) | |
2326 | err = __hci_cmd_sync_status(hdev, | |
2327 | HCI_OP_WRITE_PAGE_SCAN_TYPE, | |
2328 | sizeof(type), &type, | |
2329 | HCI_CMD_TIMEOUT); | |
2330 | ||
2331 | return err; | |
2332 | } | |
2333 | ||
2334 | static bool disconnected_accept_list_entries(struct hci_dev *hdev) | |
2335 | { | |
2336 | struct bdaddr_list *b; | |
2337 | ||
2338 | list_for_each_entry(b, &hdev->accept_list, list) { | |
2339 | struct hci_conn *conn; | |
2340 | ||
2341 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); | |
2342 | if (!conn) | |
2343 | return true; | |
2344 | ||
2345 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) | |
2346 | return true; | |
2347 | } | |
2348 | ||
2349 | return false; | |
2350 | } | |
2351 | ||
2352 | static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val) | |
2353 | { | |
2354 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE, | |
2355 | sizeof(val), &val, | |
2356 | HCI_CMD_TIMEOUT); | |
2357 | } | |
2358 | ||
451d95a9 | 2359 | int hci_update_scan_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
2360 | { |
2361 | u8 scan; | |
2362 | ||
2363 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
2364 | return 0; | |
2365 | ||
2366 | if (!hdev_is_powered(hdev)) | |
2367 | return 0; | |
2368 | ||
2369 | if (mgmt_powering_down(hdev)) | |
2370 | return 0; | |
2371 | ||
2372 | if (hdev->scanning_paused) | |
2373 | return 0; | |
2374 | ||
2375 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || | |
2376 | disconnected_accept_list_entries(hdev)) | |
2377 | scan = SCAN_PAGE; | |
2378 | else | |
2379 | scan = SCAN_DISABLED; | |
2380 | ||
2381 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) | |
2382 | scan |= SCAN_INQUIRY; | |
2383 | ||
2384 | if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) && | |
2385 | test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY)) | |
2386 | return 0; | |
2387 | ||
2388 | return hci_write_scan_enable_sync(hdev, scan); | |
2389 | } | |
2390 | ||
6f6ff38a | 2391 | int hci_update_name_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
2392 | { |
2393 | struct hci_cp_write_local_name cp; | |
2394 | ||
2395 | memset(&cp, 0, sizeof(cp)); | |
2396 | ||
2397 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); | |
2398 | ||
2399 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME, | |
2400 | sizeof(cp), &cp, | |
2401 | HCI_CMD_TIMEOUT); | |
2402 | } | |
2403 | ||
2404 | /* This function perform powered update HCI command sequence after the HCI init | |
2405 | * sequence which end up resetting all states, the sequence is as follows: | |
2406 | * | |
2407 | * HCI_SSP_ENABLED(Enable SSP) | |
2408 | * HCI_LE_ENABLED(Enable LE) | |
2409 | * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) -> | |
2410 | * Update adv data) | |
2411 | * Enable Authentication | |
2412 | * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class -> | |
2413 | * Set Name -> Set EIR) | |
2414 | */ | |
2415 | int hci_powered_update_sync(struct hci_dev *hdev) | |
2416 | { | |
2417 | int err; | |
2418 | ||
2419 | /* Register the available SMP channels (BR/EDR and LE) only when | |
2420 | * successfully powering on the controller. This late | |
2421 | * registration is required so that LE SMP can clearly decide if | |
2422 | * the public address or static address is used. | |
2423 | */ | |
2424 | smp_register(hdev); | |
2425 | ||
2426 | err = hci_write_ssp_mode_sync(hdev, 0x01); | |
2427 | if (err) | |
2428 | return err; | |
2429 | ||
2430 | err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00); | |
2431 | if (err) | |
2432 | return err; | |
2433 | ||
2434 | err = hci_powered_update_adv_sync(hdev); | |
2435 | if (err) | |
2436 | return err; | |
2437 | ||
2438 | err = hci_write_auth_enable_sync(hdev); | |
2439 | if (err) | |
2440 | return err; | |
2441 | ||
2442 | if (lmp_bredr_capable(hdev)) { | |
2443 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) | |
2444 | hci_write_fast_connectable_sync(hdev, true); | |
2445 | else | |
2446 | hci_write_fast_connectable_sync(hdev, false); | |
2447 | hci_update_scan_sync(hdev); | |
2448 | hci_update_class_sync(hdev); | |
2449 | hci_update_name_sync(hdev); | |
2450 | hci_update_eir_sync(hdev); | |
2451 | } | |
2452 | ||
2453 | return 0; | |
2454 | } | |
2455 | ||
d0b13706 LAD |
2456 | /** |
2457 | * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address | |
2458 | * (BD_ADDR) for a HCI device from | |
2459 | * a firmware node property. | |
2460 | * @hdev: The HCI device | |
cf75ad8b | 2461 | * |
d0b13706 LAD |
2462 | * Search the firmware node for 'local-bd-address'. |
2463 | * | |
2464 | * All-zero BD addresses are rejected, because those could be properties | |
2465 | * that exist in the firmware tables, but were not updated by the firmware. For | |
2466 | * example, the DTS could define 'local-bd-address', with zero BD addresses. | |
cf75ad8b | 2467 | */ |
d0b13706 | 2468 | static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev) |
cf75ad8b | 2469 | { |
d0b13706 LAD |
2470 | struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent); |
2471 | bdaddr_t ba; | |
2472 | int ret; | |
cf75ad8b | 2473 | |
d0b13706 LAD |
2474 | ret = fwnode_property_read_u8_array(fwnode, "local-bd-address", |
2475 | (u8 *)&ba, sizeof(ba)); | |
2476 | if (ret < 0 || !bacmp(&ba, BDADDR_ANY)) | |
2477 | return; | |
cf75ad8b | 2478 | |
d0b13706 LAD |
2479 | bacpy(&hdev->public_addr, &ba); |
2480 | } | |
cf75ad8b | 2481 | |
d0b13706 LAD |
2482 | struct hci_init_stage { |
2483 | int (*func)(struct hci_dev *hdev); | |
2484 | }; | |
cf75ad8b | 2485 | |
d0b13706 LAD |
2486 | /* Run init stage NULL terminated function table */ |
2487 | static int hci_init_stage_sync(struct hci_dev *hdev, | |
2488 | const struct hci_init_stage *stage) | |
2489 | { | |
2490 | size_t i; | |
cf75ad8b | 2491 | |
d0b13706 LAD |
2492 | for (i = 0; stage[i].func; i++) { |
2493 | int err; | |
cf75ad8b | 2494 | |
d0b13706 LAD |
2495 | err = stage[i].func(hdev); |
2496 | if (err) | |
2497 | return err; | |
cf75ad8b LAD |
2498 | } |
2499 | ||
2500 | return 0; | |
2501 | } | |
2502 | ||
d0b13706 LAD |
2503 | /* Read Local Version */ |
2504 | static int hci_read_local_version_sync(struct hci_dev *hdev) | |
cf75ad8b | 2505 | { |
d0b13706 LAD |
2506 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION, |
2507 | 0, NULL, HCI_CMD_TIMEOUT); | |
2508 | } | |
cf75ad8b | 2509 | |
d0b13706 LAD |
2510 | /* Read BD Address */ |
2511 | static int hci_read_bd_addr_sync(struct hci_dev *hdev) | |
2512 | { | |
2513 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR, | |
2514 | 0, NULL, HCI_CMD_TIMEOUT); | |
2515 | } | |
cf75ad8b | 2516 | |
d0b13706 LAD |
2517 | #define HCI_INIT(_func) \ |
2518 | { \ | |
2519 | .func = _func, \ | |
cf75ad8b LAD |
2520 | } |
2521 | ||
d0b13706 LAD |
2522 | static const struct hci_init_stage hci_init0[] = { |
2523 | /* HCI_OP_READ_LOCAL_VERSION */ | |
2524 | HCI_INIT(hci_read_local_version_sync), | |
2525 | /* HCI_OP_READ_BD_ADDR */ | |
2526 | HCI_INIT(hci_read_bd_addr_sync), | |
2527 | {} | |
2528 | }; | |
2529 | ||
2530 | int hci_reset_sync(struct hci_dev *hdev) | |
cf75ad8b | 2531 | { |
cf75ad8b LAD |
2532 | int err; |
2533 | ||
d0b13706 | 2534 | set_bit(HCI_RESET, &hdev->flags); |
cf75ad8b | 2535 | |
d0b13706 LAD |
2536 | err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL, |
2537 | HCI_CMD_TIMEOUT); | |
2538 | if (err) | |
2539 | return err; | |
cf75ad8b | 2540 | |
d0b13706 LAD |
2541 | return 0; |
2542 | } | |
cf75ad8b | 2543 | |
d0b13706 LAD |
2544 | static int hci_init0_sync(struct hci_dev *hdev) |
2545 | { | |
2546 | int err; | |
cf75ad8b | 2547 | |
d0b13706 LAD |
2548 | bt_dev_dbg(hdev, ""); |
2549 | ||
2550 | /* Reset */ | |
2551 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { | |
2552 | err = hci_reset_sync(hdev); | |
cf75ad8b LAD |
2553 | if (err) |
2554 | return err; | |
2555 | } | |
2556 | ||
d0b13706 LAD |
2557 | return hci_init_stage_sync(hdev, hci_init0); |
2558 | } | |
abfeea47 | 2559 | |
d0b13706 LAD |
2560 | static int hci_unconf_init_sync(struct hci_dev *hdev) |
2561 | { | |
2562 | int err; | |
2563 | ||
2564 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) | |
cf75ad8b LAD |
2565 | return 0; |
2566 | ||
d0b13706 LAD |
2567 | err = hci_init0_sync(hdev); |
2568 | if (err < 0) | |
2569 | return err; | |
cf75ad8b | 2570 | |
d0b13706 LAD |
2571 | if (hci_dev_test_flag(hdev, HCI_SETUP)) |
2572 | hci_debugfs_create_basic(hdev); | |
cf75ad8b LAD |
2573 | |
2574 | return 0; | |
2575 | } | |
2576 | ||
d0b13706 LAD |
2577 | /* Read Local Supported Features. */ |
2578 | static int hci_read_local_features_sync(struct hci_dev *hdev) | |
cf75ad8b | 2579 | { |
d0b13706 LAD |
2580 | /* Not all AMP controllers support this command */ |
2581 | if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20)) | |
2582 | return 0; | |
cf75ad8b | 2583 | |
d0b13706 LAD |
2584 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES, |
2585 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
2586 | } |
2587 | ||
d0b13706 LAD |
2588 | /* BR Controller init stage 1 command sequence */ |
2589 | static const struct hci_init_stage br_init1[] = { | |
2590 | /* HCI_OP_READ_LOCAL_FEATURES */ | |
2591 | HCI_INIT(hci_read_local_features_sync), | |
2592 | /* HCI_OP_READ_LOCAL_VERSION */ | |
2593 | HCI_INIT(hci_read_local_version_sync), | |
2594 | /* HCI_OP_READ_BD_ADDR */ | |
2595 | HCI_INIT(hci_read_bd_addr_sync), | |
2596 | {} | |
2597 | }; | |
2598 | ||
2599 | /* Read Local Commands */ | |
2600 | static int hci_read_local_cmds_sync(struct hci_dev *hdev) | |
cf75ad8b | 2601 | { |
d0b13706 LAD |
2602 | /* All Bluetooth 1.2 and later controllers should support the |
2603 | * HCI command for reading the local supported commands. | |
2604 | * | |
2605 | * Unfortunately some controllers indicate Bluetooth 1.2 support, | |
2606 | * but do not have support for this command. If that is the case, | |
2607 | * the driver can quirk the behavior and skip reading the local | |
2608 | * supported commands. | |
2609 | */ | |
2610 | if (hdev->hci_ver > BLUETOOTH_VER_1_1 && | |
2611 | !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) | |
2612 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS, | |
2613 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b | 2614 | |
d0b13706 | 2615 | return 0; |
cf75ad8b LAD |
2616 | } |
2617 | ||
d0b13706 LAD |
2618 | /* Read Local AMP Info */ |
2619 | static int hci_read_local_amp_info_sync(struct hci_dev *hdev) | |
cf75ad8b | 2620 | { |
d0b13706 LAD |
2621 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO, |
2622 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
2623 | } |
2624 | ||
d0b13706 LAD |
2625 | /* Read Data Blk size */ |
2626 | static int hci_read_data_block_size_sync(struct hci_dev *hdev) | |
cf75ad8b | 2627 | { |
d0b13706 LAD |
2628 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, |
2629 | 0, NULL, HCI_CMD_TIMEOUT); | |
2630 | } | |
cf75ad8b | 2631 | |
d0b13706 LAD |
2632 | /* Read Flow Control Mode */ |
2633 | static int hci_read_flow_control_mode_sync(struct hci_dev *hdev) | |
2634 | { | |
2635 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, | |
2636 | 0, NULL, HCI_CMD_TIMEOUT); | |
2637 | } | |
cf75ad8b | 2638 | |
d0b13706 LAD |
2639 | /* Read Location Data */ |
2640 | static int hci_read_location_data_sync(struct hci_dev *hdev) | |
2641 | { | |
2642 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA, | |
2643 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
2644 | } |
2645 | ||
d0b13706 LAD |
2646 | /* AMP Controller init stage 1 command sequence */ |
2647 | static const struct hci_init_stage amp_init1[] = { | |
2648 | /* HCI_OP_READ_LOCAL_VERSION */ | |
2649 | HCI_INIT(hci_read_local_version_sync), | |
2650 | /* HCI_OP_READ_LOCAL_COMMANDS */ | |
2651 | HCI_INIT(hci_read_local_cmds_sync), | |
2652 | /* HCI_OP_READ_LOCAL_AMP_INFO */ | |
2653 | HCI_INIT(hci_read_local_amp_info_sync), | |
2654 | /* HCI_OP_READ_DATA_BLOCK_SIZE */ | |
2655 | HCI_INIT(hci_read_data_block_size_sync), | |
2656 | /* HCI_OP_READ_FLOW_CONTROL_MODE */ | |
2657 | HCI_INIT(hci_read_flow_control_mode_sync), | |
2658 | /* HCI_OP_READ_LOCATION_DATA */ | |
2659 | HCI_INIT(hci_read_location_data_sync), | |
2660 | }; | |
2661 | ||
2662 | static int hci_init1_sync(struct hci_dev *hdev) | |
cf75ad8b | 2663 | { |
d0b13706 | 2664 | int err; |
cf75ad8b | 2665 | |
d0b13706 | 2666 | bt_dev_dbg(hdev, ""); |
cf75ad8b | 2667 | |
d0b13706 LAD |
2668 | /* Reset */ |
2669 | if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { | |
2670 | err = hci_reset_sync(hdev); | |
2671 | if (err) | |
2672 | return err; | |
2673 | } | |
cf75ad8b | 2674 | |
d0b13706 LAD |
2675 | switch (hdev->dev_type) { |
2676 | case HCI_PRIMARY: | |
2677 | hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; | |
2678 | return hci_init_stage_sync(hdev, br_init1); | |
2679 | case HCI_AMP: | |
2680 | hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; | |
2681 | return hci_init_stage_sync(hdev, amp_init1); | |
2682 | default: | |
2683 | bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type); | |
2684 | break; | |
2685 | } | |
2686 | ||
2687 | return 0; | |
cf75ad8b LAD |
2688 | } |
2689 | ||
d0b13706 LAD |
2690 | /* AMP Controller init stage 2 command sequence */ |
2691 | static const struct hci_init_stage amp_init2[] = { | |
2692 | /* HCI_OP_READ_LOCAL_FEATURES */ | |
2693 | HCI_INIT(hci_read_local_features_sync), | |
2694 | }; | |
2695 | ||
2696 | /* Read Buffer Size (ACL mtu, max pkt, etc.) */ | |
2697 | static int hci_read_buffer_size_sync(struct hci_dev *hdev) | |
cf75ad8b | 2698 | { |
d0b13706 LAD |
2699 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE, |
2700 | 0, NULL, HCI_CMD_TIMEOUT); | |
2701 | } | |
cf75ad8b | 2702 | |
d0b13706 LAD |
2703 | /* Read Class of Device */ |
2704 | static int hci_read_dev_class_sync(struct hci_dev *hdev) | |
2705 | { | |
2706 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV, | |
2707 | 0, NULL, HCI_CMD_TIMEOUT); | |
2708 | } | |
cf75ad8b | 2709 | |
d0b13706 LAD |
2710 | /* Read Local Name */ |
2711 | static int hci_read_local_name_sync(struct hci_dev *hdev) | |
2712 | { | |
2713 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME, | |
2714 | 0, NULL, HCI_CMD_TIMEOUT); | |
2715 | } | |
cf75ad8b | 2716 | |
d0b13706 LAD |
2717 | /* Read Voice Setting */ |
2718 | static int hci_read_voice_setting_sync(struct hci_dev *hdev) | |
2719 | { | |
2720 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING, | |
2721 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
2722 | } |
2723 | ||
d0b13706 LAD |
2724 | /* Read Number of Supported IAC */ |
2725 | static int hci_read_num_supported_iac_sync(struct hci_dev *hdev) | |
cf75ad8b | 2726 | { |
d0b13706 LAD |
2727 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC, |
2728 | 0, NULL, HCI_CMD_TIMEOUT); | |
2729 | } | |
cf75ad8b | 2730 | |
d0b13706 LAD |
2731 | /* Read Current IAC LAP */ |
2732 | static int hci_read_current_iac_lap_sync(struct hci_dev *hdev) | |
2733 | { | |
2734 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP, | |
2735 | 0, NULL, HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
2736 | } |
2737 | ||
d0b13706 LAD |
2738 | static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type, |
2739 | u8 cond_type, bdaddr_t *bdaddr, | |
2740 | u8 auto_accept) | |
cf75ad8b | 2741 | { |
d0b13706 | 2742 | struct hci_cp_set_event_filter cp; |
cf75ad8b | 2743 | |
d0b13706 | 2744 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
cf75ad8b LAD |
2745 | return 0; |
2746 | ||
d0b13706 LAD |
2747 | memset(&cp, 0, sizeof(cp)); |
2748 | cp.flt_type = flt_type; | |
cf75ad8b | 2749 | |
d0b13706 LAD |
2750 | if (flt_type != HCI_FLT_CLEAR_ALL) { |
2751 | cp.cond_type = cond_type; | |
2752 | bacpy(&cp.addr_conn_flt.bdaddr, bdaddr); | |
2753 | cp.addr_conn_flt.auto_accept = auto_accept; | |
cf75ad8b LAD |
2754 | } |
2755 | ||
d0b13706 LAD |
2756 | return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT, |
2757 | flt_type == HCI_FLT_CLEAR_ALL ? | |
2758 | sizeof(cp.flt_type) : sizeof(cp), &cp, | |
2759 | HCI_CMD_TIMEOUT); | |
cf75ad8b LAD |
2760 | } |
2761 | ||
d0b13706 | 2762 | static int hci_clear_event_filter_sync(struct hci_dev *hdev) |
cf75ad8b | 2763 | { |
d0b13706 LAD |
2764 | if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED)) |
2765 | return 0; | |
2766 | ||
2767 | return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00, | |
2768 | BDADDR_ANY, 0x00); | |
2769 | } | |
2770 | ||
2771 | /* Connection accept timeout ~20 secs */ | |
2772 | static int hci_write_ca_timeout_sync(struct hci_dev *hdev) | |
2773 | { | |
2774 | __le16 param = cpu_to_le16(0x7d00); | |
2775 | ||
2776 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT, | |
2777 | sizeof(param), ¶m, HCI_CMD_TIMEOUT); | |
2778 | } | |
2779 | ||
2780 | /* BR Controller init stage 2 command sequence */ | |
2781 | static const struct hci_init_stage br_init2[] = { | |
2782 | /* HCI_OP_READ_BUFFER_SIZE */ | |
2783 | HCI_INIT(hci_read_buffer_size_sync), | |
2784 | /* HCI_OP_READ_CLASS_OF_DEV */ | |
2785 | HCI_INIT(hci_read_dev_class_sync), | |
2786 | /* HCI_OP_READ_LOCAL_NAME */ | |
2787 | HCI_INIT(hci_read_local_name_sync), | |
2788 | /* HCI_OP_READ_VOICE_SETTING */ | |
2789 | HCI_INIT(hci_read_voice_setting_sync), | |
2790 | /* HCI_OP_READ_NUM_SUPPORTED_IAC */ | |
2791 | HCI_INIT(hci_read_num_supported_iac_sync), | |
2792 | /* HCI_OP_READ_CURRENT_IAC_LAP */ | |
2793 | HCI_INIT(hci_read_current_iac_lap_sync), | |
2794 | /* HCI_OP_SET_EVENT_FLT */ | |
2795 | HCI_INIT(hci_clear_event_filter_sync), | |
2796 | /* HCI_OP_WRITE_CA_TIMEOUT */ | |
2797 | HCI_INIT(hci_write_ca_timeout_sync), | |
2798 | {} | |
2799 | }; | |
2800 | ||
2801 | static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev) | |
2802 | { | |
2803 | u8 mode = 0x01; | |
2804 | ||
2805 | if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
2806 | return 0; | |
2807 | ||
2808 | /* When SSP is available, then the host features page | |
2809 | * should also be available as well. However some | |
2810 | * controllers list the max_page as 0 as long as SSP | |
2811 | * has not been enabled. To achieve proper debugging | |
2812 | * output, force the minimum max_page to 1 at least. | |
2813 | */ | |
2814 | hdev->max_page = 0x01; | |
2815 | ||
2816 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, | |
2817 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
2818 | } | |
2819 | ||
2820 | static int hci_write_eir_sync(struct hci_dev *hdev) | |
2821 | { | |
2822 | struct hci_cp_write_eir cp; | |
2823 | ||
2824 | if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
2825 | return 0; | |
2826 | ||
2827 | memset(hdev->eir, 0, sizeof(hdev->eir)); | |
2828 | memset(&cp, 0, sizeof(cp)); | |
2829 | ||
2830 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, | |
2831 | HCI_CMD_TIMEOUT); | |
2832 | } | |
2833 | ||
2834 | static int hci_write_inquiry_mode_sync(struct hci_dev *hdev) | |
2835 | { | |
2836 | u8 mode; | |
2837 | ||
2838 | if (!lmp_inq_rssi_capable(hdev) && | |
2839 | !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) | |
2840 | return 0; | |
2841 | ||
2842 | /* If Extended Inquiry Result events are supported, then | |
2843 | * they are clearly preferred over Inquiry Result with RSSI | |
2844 | * events. | |
2845 | */ | |
2846 | mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; | |
2847 | ||
2848 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE, | |
2849 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
2850 | } | |
2851 | ||
2852 | static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev) | |
2853 | { | |
2854 | if (!lmp_inq_tx_pwr_capable(hdev)) | |
2855 | return 0; | |
2856 | ||
2857 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, | |
2858 | 0, NULL, HCI_CMD_TIMEOUT); | |
2859 | } | |
2860 | ||
2861 | static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page) | |
2862 | { | |
2863 | struct hci_cp_read_local_ext_features cp; | |
2864 | ||
2865 | if (!lmp_ext_feat_capable(hdev)) | |
2866 | return 0; | |
2867 | ||
2868 | memset(&cp, 0, sizeof(cp)); | |
2869 | cp.page = page; | |
2870 | ||
2871 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, | |
2872 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2873 | } | |
2874 | ||
2875 | static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev) | |
2876 | { | |
2877 | return hci_read_local_ext_features_sync(hdev, 0x01); | |
2878 | } | |
2879 | ||
2880 | /* HCI Controller init stage 2 command sequence */ | |
2881 | static const struct hci_init_stage hci_init2[] = { | |
2882 | /* HCI_OP_READ_LOCAL_COMMANDS */ | |
2883 | HCI_INIT(hci_read_local_cmds_sync), | |
2884 | /* HCI_OP_WRITE_SSP_MODE */ | |
2885 | HCI_INIT(hci_write_ssp_mode_1_sync), | |
2886 | /* HCI_OP_WRITE_EIR */ | |
2887 | HCI_INIT(hci_write_eir_sync), | |
2888 | /* HCI_OP_WRITE_INQUIRY_MODE */ | |
2889 | HCI_INIT(hci_write_inquiry_mode_sync), | |
2890 | /* HCI_OP_READ_INQ_RSP_TX_POWER */ | |
2891 | HCI_INIT(hci_read_inq_rsp_tx_power_sync), | |
2892 | /* HCI_OP_READ_LOCAL_EXT_FEATURES */ | |
2893 | HCI_INIT(hci_read_local_ext_features_1_sync), | |
2894 | /* HCI_OP_WRITE_AUTH_ENABLE */ | |
2895 | HCI_INIT(hci_write_auth_enable_sync), | |
2896 | {} | |
2897 | }; | |
2898 | ||
2899 | /* Read LE Buffer Size */ | |
2900 | static int hci_le_read_buffer_size_sync(struct hci_dev *hdev) | |
2901 | { | |
2902 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE, | |
2903 | 0, NULL, HCI_CMD_TIMEOUT); | |
2904 | } | |
2905 | ||
2906 | /* Read LE Local Supported Features */ | |
2907 | static int hci_le_read_local_features_sync(struct hci_dev *hdev) | |
2908 | { | |
2909 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES, | |
2910 | 0, NULL, HCI_CMD_TIMEOUT); | |
2911 | } | |
2912 | ||
2913 | /* Read LE Supported States */ | |
2914 | static int hci_le_read_supported_states_sync(struct hci_dev *hdev) | |
2915 | { | |
2916 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES, | |
2917 | 0, NULL, HCI_CMD_TIMEOUT); | |
2918 | } | |
2919 | ||
2920 | /* LE Controller init stage 2 command sequence */ | |
2921 | static const struct hci_init_stage le_init2[] = { | |
2922 | /* HCI_OP_LE_READ_BUFFER_SIZE */ | |
2923 | HCI_INIT(hci_le_read_buffer_size_sync), | |
2924 | /* HCI_OP_LE_READ_LOCAL_FEATURES */ | |
2925 | HCI_INIT(hci_le_read_local_features_sync), | |
2926 | /* HCI_OP_LE_READ_SUPPORTED_STATES */ | |
2927 | HCI_INIT(hci_le_read_supported_states_sync), | |
2928 | {} | |
2929 | }; | |
2930 | ||
2931 | static int hci_init2_sync(struct hci_dev *hdev) | |
2932 | { | |
2933 | int err; | |
2934 | ||
2935 | bt_dev_dbg(hdev, ""); | |
2936 | ||
2937 | if (hdev->dev_type == HCI_AMP) | |
2938 | return hci_init_stage_sync(hdev, amp_init2); | |
2939 | ||
2940 | if (lmp_bredr_capable(hdev)) { | |
2941 | err = hci_init_stage_sync(hdev, br_init2); | |
2942 | if (err) | |
2943 | return err; | |
2944 | } else { | |
2945 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); | |
2946 | } | |
2947 | ||
2948 | if (lmp_le_capable(hdev)) { | |
2949 | err = hci_init_stage_sync(hdev, le_init2); | |
2950 | if (err) | |
2951 | return err; | |
2952 | /* LE-only controllers have LE implicitly enabled */ | |
2953 | if (!lmp_bredr_capable(hdev)) | |
2954 | hci_dev_set_flag(hdev, HCI_LE_ENABLED); | |
2955 | } | |
2956 | ||
2957 | return hci_init_stage_sync(hdev, hci_init2); | |
2958 | } | |
2959 | ||
2960 | static int hci_set_event_mask_sync(struct hci_dev *hdev) | |
2961 | { | |
2962 | /* The second byte is 0xff instead of 0x9f (two reserved bits | |
2963 | * disabled) since a Broadcom 1.2 dongle doesn't respond to the | |
2964 | * command otherwise. | |
2965 | */ | |
2966 | u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; | |
2967 | ||
2968 | /* CSR 1.1 dongles does not accept any bitfield so don't try to set | |
2969 | * any event mask for pre 1.2 devices. | |
2970 | */ | |
2971 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
2972 | return 0; | |
2973 | ||
2974 | if (lmp_bredr_capable(hdev)) { | |
2975 | events[4] |= 0x01; /* Flow Specification Complete */ | |
182ee45d LAD |
2976 | |
2977 | /* Don't set Disconnect Complete when suspended as that | |
2978 | * would wakeup the host when disconnecting due to | |
2979 | * suspend. | |
2980 | */ | |
2981 | if (hdev->suspended) | |
2982 | events[0] &= 0xef; | |
d0b13706 LAD |
2983 | } else { |
2984 | /* Use a different default for LE-only devices */ | |
2985 | memset(events, 0, sizeof(events)); | |
2986 | events[1] |= 0x20; /* Command Complete */ | |
2987 | events[1] |= 0x40; /* Command Status */ | |
2988 | events[1] |= 0x80; /* Hardware Error */ | |
2989 | ||
2990 | /* If the controller supports the Disconnect command, enable | |
2991 | * the corresponding event. In addition enable packet flow | |
2992 | * control related events. | |
2993 | */ | |
2994 | if (hdev->commands[0] & 0x20) { | |
182ee45d LAD |
2995 | /* Don't set Disconnect Complete when suspended as that |
2996 | * would wakeup the host when disconnecting due to | |
2997 | * suspend. | |
2998 | */ | |
2999 | if (!hdev->suspended) | |
3000 | events[0] |= 0x10; /* Disconnection Complete */ | |
d0b13706 LAD |
3001 | events[2] |= 0x04; /* Number of Completed Packets */ |
3002 | events[3] |= 0x02; /* Data Buffer Overflow */ | |
3003 | } | |
3004 | ||
3005 | /* If the controller supports the Read Remote Version | |
3006 | * Information command, enable the corresponding event. | |
3007 | */ | |
3008 | if (hdev->commands[2] & 0x80) | |
3009 | events[1] |= 0x08; /* Read Remote Version Information | |
3010 | * Complete | |
3011 | */ | |
3012 | ||
3013 | if (hdev->le_features[0] & HCI_LE_ENCRYPTION) { | |
3014 | events[0] |= 0x80; /* Encryption Change */ | |
3015 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ | |
3016 | } | |
3017 | } | |
3018 | ||
3019 | if (lmp_inq_rssi_capable(hdev) || | |
3020 | test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) | |
3021 | events[4] |= 0x02; /* Inquiry Result with RSSI */ | |
3022 | ||
3023 | if (lmp_ext_feat_capable(hdev)) | |
3024 | events[4] |= 0x04; /* Read Remote Extended Features Complete */ | |
3025 | ||
3026 | if (lmp_esco_capable(hdev)) { | |
3027 | events[5] |= 0x08; /* Synchronous Connection Complete */ | |
3028 | events[5] |= 0x10; /* Synchronous Connection Changed */ | |
3029 | } | |
3030 | ||
3031 | if (lmp_sniffsubr_capable(hdev)) | |
3032 | events[5] |= 0x20; /* Sniff Subrating */ | |
3033 | ||
3034 | if (lmp_pause_enc_capable(hdev)) | |
3035 | events[5] |= 0x80; /* Encryption Key Refresh Complete */ | |
3036 | ||
3037 | if (lmp_ext_inq_capable(hdev)) | |
3038 | events[5] |= 0x40; /* Extended Inquiry Result */ | |
3039 | ||
3040 | if (lmp_no_flush_capable(hdev)) | |
3041 | events[7] |= 0x01; /* Enhanced Flush Complete */ | |
3042 | ||
3043 | if (lmp_lsto_capable(hdev)) | |
3044 | events[6] |= 0x80; /* Link Supervision Timeout Changed */ | |
3045 | ||
3046 | if (lmp_ssp_capable(hdev)) { | |
3047 | events[6] |= 0x01; /* IO Capability Request */ | |
3048 | events[6] |= 0x02; /* IO Capability Response */ | |
3049 | events[6] |= 0x04; /* User Confirmation Request */ | |
3050 | events[6] |= 0x08; /* User Passkey Request */ | |
3051 | events[6] |= 0x10; /* Remote OOB Data Request */ | |
3052 | events[6] |= 0x20; /* Simple Pairing Complete */ | |
3053 | events[7] |= 0x04; /* User Passkey Notification */ | |
3054 | events[7] |= 0x08; /* Keypress Notification */ | |
3055 | events[7] |= 0x10; /* Remote Host Supported | |
3056 | * Features Notification | |
3057 | */ | |
3058 | } | |
3059 | ||
3060 | if (lmp_le_capable(hdev)) | |
3061 | events[7] |= 0x20; /* LE Meta-Event */ | |
3062 | ||
3063 | return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK, | |
3064 | sizeof(events), events, HCI_CMD_TIMEOUT); | |
3065 | } | |
3066 | ||
3067 | static int hci_read_stored_link_key_sync(struct hci_dev *hdev) | |
3068 | { | |
3069 | struct hci_cp_read_stored_link_key cp; | |
3070 | ||
3071 | if (!(hdev->commands[6] & 0x20) || | |
3072 | test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) | |
3073 | return 0; | |
3074 | ||
3075 | memset(&cp, 0, sizeof(cp)); | |
3076 | bacpy(&cp.bdaddr, BDADDR_ANY); | |
3077 | cp.read_all = 0x01; | |
3078 | ||
3079 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY, | |
3080 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3081 | } | |
3082 | ||
3083 | static int hci_setup_link_policy_sync(struct hci_dev *hdev) | |
3084 | { | |
3085 | struct hci_cp_write_def_link_policy cp; | |
3086 | u16 link_policy = 0; | |
3087 | ||
3088 | if (!(hdev->commands[5] & 0x10)) | |
3089 | return 0; | |
3090 | ||
3091 | memset(&cp, 0, sizeof(cp)); | |
3092 | ||
3093 | if (lmp_rswitch_capable(hdev)) | |
3094 | link_policy |= HCI_LP_RSWITCH; | |
3095 | if (lmp_hold_capable(hdev)) | |
3096 | link_policy |= HCI_LP_HOLD; | |
3097 | if (lmp_sniff_capable(hdev)) | |
3098 | link_policy |= HCI_LP_SNIFF; | |
3099 | if (lmp_park_capable(hdev)) | |
3100 | link_policy |= HCI_LP_PARK; | |
3101 | ||
3102 | cp.policy = cpu_to_le16(link_policy); | |
3103 | ||
3104 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, | |
3105 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3106 | } | |
3107 | ||
3108 | static int hci_read_page_scan_activity_sync(struct hci_dev *hdev) | |
3109 | { | |
3110 | if (!(hdev->commands[8] & 0x01)) | |
3111 | return 0; | |
3112 | ||
3113 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY, | |
3114 | 0, NULL, HCI_CMD_TIMEOUT); | |
3115 | } | |
3116 | ||
3117 | static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev) | |
3118 | { | |
3119 | if (!(hdev->commands[18] & 0x04) || | |
3120 | test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks)) | |
3121 | return 0; | |
3122 | ||
3123 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING, | |
3124 | 0, NULL, HCI_CMD_TIMEOUT); | |
3125 | } | |
3126 | ||
3127 | static int hci_read_page_scan_type_sync(struct hci_dev *hdev) | |
3128 | { | |
3129 | /* Some older Broadcom based Bluetooth 1.2 controllers do not | |
3130 | * support the Read Page Scan Type command. Check support for | |
3131 | * this command in the bit mask of supported commands. | |
3132 | */ | |
3133 | if (!(hdev->commands[13] & 0x01)) | |
3134 | return 0; | |
3135 | ||
3136 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE, | |
3137 | 0, NULL, HCI_CMD_TIMEOUT); | |
3138 | } | |
3139 | ||
3140 | /* Read features beyond page 1 if available */ | |
3141 | static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev) | |
3142 | { | |
3143 | u8 page; | |
3144 | int err; | |
3145 | ||
3146 | if (!lmp_ext_feat_capable(hdev)) | |
3147 | return 0; | |
3148 | ||
3149 | for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page; | |
3150 | page++) { | |
3151 | err = hci_read_local_ext_features_sync(hdev, page); | |
3152 | if (err) | |
3153 | return err; | |
3154 | } | |
3155 | ||
3156 | return 0; | |
3157 | } | |
3158 | ||
3159 | /* HCI Controller init stage 3 command sequence */ | |
3160 | static const struct hci_init_stage hci_init3[] = { | |
3161 | /* HCI_OP_SET_EVENT_MASK */ | |
3162 | HCI_INIT(hci_set_event_mask_sync), | |
3163 | /* HCI_OP_READ_STORED_LINK_KEY */ | |
3164 | HCI_INIT(hci_read_stored_link_key_sync), | |
3165 | /* HCI_OP_WRITE_DEF_LINK_POLICY */ | |
3166 | HCI_INIT(hci_setup_link_policy_sync), | |
3167 | /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */ | |
3168 | HCI_INIT(hci_read_page_scan_activity_sync), | |
3169 | /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */ | |
3170 | HCI_INIT(hci_read_def_err_data_reporting_sync), | |
3171 | /* HCI_OP_READ_PAGE_SCAN_TYPE */ | |
3172 | HCI_INIT(hci_read_page_scan_type_sync), | |
3173 | /* HCI_OP_READ_LOCAL_EXT_FEATURES */ | |
3174 | HCI_INIT(hci_read_local_ext_features_all_sync), | |
3175 | {} | |
3176 | }; | |
3177 | ||
3178 | static int hci_le_set_event_mask_sync(struct hci_dev *hdev) | |
3179 | { | |
3180 | u8 events[8]; | |
3181 | ||
3182 | if (!lmp_le_capable(hdev)) | |
3183 | return 0; | |
3184 | ||
3185 | memset(events, 0, sizeof(events)); | |
3186 | ||
3187 | if (hdev->le_features[0] & HCI_LE_ENCRYPTION) | |
3188 | events[0] |= 0x10; /* LE Long Term Key Request */ | |
3189 | ||
3190 | /* If controller supports the Connection Parameters Request | |
3191 | * Link Layer Procedure, enable the corresponding event. | |
3192 | */ | |
3193 | if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC) | |
3194 | /* LE Remote Connection Parameter Request */ | |
3195 | events[0] |= 0x20; | |
3196 | ||
3197 | /* If the controller supports the Data Length Extension | |
3198 | * feature, enable the corresponding event. | |
3199 | */ | |
3200 | if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) | |
3201 | events[0] |= 0x40; /* LE Data Length Change */ | |
3202 | ||
3203 | /* If the controller supports LL Privacy feature, enable | |
3204 | * the corresponding event. | |
3205 | */ | |
3206 | if (hdev->le_features[0] & HCI_LE_LL_PRIVACY) | |
3207 | events[1] |= 0x02; /* LE Enhanced Connection Complete */ | |
3208 | ||
3209 | /* If the controller supports Extended Scanner Filter | |
3210 | * Policies, enable the corresponding event. | |
3211 | */ | |
3212 | if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY) | |
3213 | events[1] |= 0x04; /* LE Direct Advertising Report */ | |
3214 | ||
3215 | /* If the controller supports Channel Selection Algorithm #2 | |
3216 | * feature, enable the corresponding event. | |
3217 | */ | |
3218 | if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2) | |
3219 | events[2] |= 0x08; /* LE Channel Selection Algorithm */ | |
3220 | ||
3221 | /* If the controller supports the LE Set Scan Enable command, | |
3222 | * enable the corresponding advertising report event. | |
3223 | */ | |
3224 | if (hdev->commands[26] & 0x08) | |
3225 | events[0] |= 0x02; /* LE Advertising Report */ | |
3226 | ||
3227 | /* If the controller supports the LE Create Connection | |
3228 | * command, enable the corresponding event. | |
3229 | */ | |
3230 | if (hdev->commands[26] & 0x10) | |
3231 | events[0] |= 0x01; /* LE Connection Complete */ | |
3232 | ||
3233 | /* If the controller supports the LE Connection Update | |
3234 | * command, enable the corresponding event. | |
3235 | */ | |
3236 | if (hdev->commands[27] & 0x04) | |
3237 | events[0] |= 0x04; /* LE Connection Update Complete */ | |
3238 | ||
3239 | /* If the controller supports the LE Read Remote Used Features | |
3240 | * command, enable the corresponding event. | |
3241 | */ | |
3242 | if (hdev->commands[27] & 0x20) | |
3243 | /* LE Read Remote Used Features Complete */ | |
3244 | events[0] |= 0x08; | |
3245 | ||
3246 | /* If the controller supports the LE Read Local P-256 | |
3247 | * Public Key command, enable the corresponding event. | |
3248 | */ | |
3249 | if (hdev->commands[34] & 0x02) | |
3250 | /* LE Read Local P-256 Public Key Complete */ | |
3251 | events[0] |= 0x80; | |
3252 | ||
3253 | /* If the controller supports the LE Generate DHKey | |
3254 | * command, enable the corresponding event. | |
3255 | */ | |
3256 | if (hdev->commands[34] & 0x04) | |
3257 | events[1] |= 0x01; /* LE Generate DHKey Complete */ | |
3258 | ||
3259 | /* If the controller supports the LE Set Default PHY or | |
3260 | * LE Set PHY commands, enable the corresponding event. | |
3261 | */ | |
3262 | if (hdev->commands[35] & (0x20 | 0x40)) | |
3263 | events[1] |= 0x08; /* LE PHY Update Complete */ | |
3264 | ||
3265 | /* If the controller supports LE Set Extended Scan Parameters | |
3266 | * and LE Set Extended Scan Enable commands, enable the | |
3267 | * corresponding event. | |
3268 | */ | |
3269 | if (use_ext_scan(hdev)) | |
3270 | events[1] |= 0x10; /* LE Extended Advertising Report */ | |
3271 | ||
3272 | /* If the controller supports the LE Extended Advertising | |
3273 | * command, enable the corresponding event. | |
3274 | */ | |
3275 | if (ext_adv_capable(hdev)) | |
3276 | events[2] |= 0x02; /* LE Advertising Set Terminated */ | |
3277 | ||
3278 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK, | |
3279 | sizeof(events), events, HCI_CMD_TIMEOUT); | |
3280 | } | |
3281 | ||
3282 | /* Read LE Advertising Channel TX Power */ | |
3283 | static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev) | |
3284 | { | |
3285 | if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) { | |
3286 | /* HCI TS spec forbids mixing of legacy and extended | |
3287 | * advertising commands wherein READ_ADV_TX_POWER is | |
3288 | * also included. So do not call it if extended adv | |
3289 | * is supported otherwise controller will return | |
3290 | * COMMAND_DISALLOWED for extended commands. | |
3291 | */ | |
3292 | return __hci_cmd_sync_status(hdev, | |
3293 | HCI_OP_LE_READ_ADV_TX_POWER, | |
3294 | 0, NULL, HCI_CMD_TIMEOUT); | |
3295 | } | |
3296 | ||
3297 | return 0; | |
3298 | } | |
3299 | ||
3300 | /* Read LE Min/Max Tx Power*/ | |
3301 | static int hci_le_read_tx_power_sync(struct hci_dev *hdev) | |
3302 | { | |
d2f8114f AG |
3303 | if (!(hdev->commands[38] & 0x80) || |
3304 | test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks)) | |
d0b13706 LAD |
3305 | return 0; |
3306 | ||
3307 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER, | |
3308 | 0, NULL, HCI_CMD_TIMEOUT); | |
3309 | } | |
3310 | ||
3311 | /* Read LE Accept List Size */ | |
3312 | static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev) | |
3313 | { | |
3314 | if (!(hdev->commands[26] & 0x40)) | |
3315 | return 0; | |
3316 | ||
3317 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE, | |
3318 | 0, NULL, HCI_CMD_TIMEOUT); | |
3319 | } | |
3320 | ||
3321 | /* Clear LE Accept List */ | |
3322 | static int hci_le_clear_accept_list_sync(struct hci_dev *hdev) | |
3323 | { | |
3324 | if (!(hdev->commands[26] & 0x80)) | |
3325 | return 0; | |
3326 | ||
3327 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL, | |
3328 | HCI_CMD_TIMEOUT); | |
3329 | } | |
3330 | ||
3331 | /* Read LE Resolving List Size */ | |
3332 | static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev) | |
3333 | { | |
3334 | if (!(hdev->commands[34] & 0x40)) | |
3335 | return 0; | |
3336 | ||
3337 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE, | |
3338 | 0, NULL, HCI_CMD_TIMEOUT); | |
3339 | } | |
3340 | ||
3341 | /* Clear LE Resolving List */ | |
3342 | static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev) | |
3343 | { | |
3344 | if (!(hdev->commands[34] & 0x20)) | |
3345 | return 0; | |
3346 | ||
3347 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL, | |
3348 | HCI_CMD_TIMEOUT); | |
3349 | } | |
3350 | ||
3351 | /* Set RPA timeout */ | |
3352 | static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev) | |
3353 | { | |
3354 | __le16 timeout = cpu_to_le16(hdev->rpa_timeout); | |
3355 | ||
3356 | if (!(hdev->commands[35] & 0x04)) | |
3357 | return 0; | |
3358 | ||
3359 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT, | |
3360 | sizeof(timeout), &timeout, | |
3361 | HCI_CMD_TIMEOUT); | |
3362 | } | |
3363 | ||
3364 | /* Read LE Maximum Data Length */ | |
3365 | static int hci_le_read_max_data_len_sync(struct hci_dev *hdev) | |
3366 | { | |
3367 | if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) | |
3368 | return 0; | |
3369 | ||
3370 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL, | |
3371 | HCI_CMD_TIMEOUT); | |
3372 | } | |
3373 | ||
3374 | /* Read LE Suggested Default Data Length */ | |
3375 | static int hci_le_read_def_data_len_sync(struct hci_dev *hdev) | |
3376 | { | |
3377 | if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) | |
3378 | return 0; | |
3379 | ||
3380 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL, | |
3381 | HCI_CMD_TIMEOUT); | |
3382 | } | |
3383 | ||
3384 | /* Read LE Number of Supported Advertising Sets */ | |
3385 | static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev) | |
3386 | { | |
3387 | if (!ext_adv_capable(hdev)) | |
3388 | return 0; | |
3389 | ||
3390 | return __hci_cmd_sync_status(hdev, | |
3391 | HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS, | |
3392 | 0, NULL, HCI_CMD_TIMEOUT); | |
3393 | } | |
3394 | ||
3395 | /* Write LE Host Supported */ | |
3396 | static int hci_set_le_support_sync(struct hci_dev *hdev) | |
3397 | { | |
3398 | struct hci_cp_write_le_host_supported cp; | |
3399 | ||
3400 | /* LE-only devices do not support explicit enablement */ | |
3401 | if (!lmp_bredr_capable(hdev)) | |
3402 | return 0; | |
3403 | ||
3404 | memset(&cp, 0, sizeof(cp)); | |
3405 | ||
3406 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { | |
3407 | cp.le = 0x01; | |
3408 | cp.simul = 0x00; | |
3409 | } | |
3410 | ||
3411 | if (cp.le == lmp_host_le_capable(hdev)) | |
3412 | return 0; | |
3413 | ||
3414 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, | |
3415 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3416 | } | |
3417 | ||
3418 | /* LE Controller init stage 3 command sequence */ | |
3419 | static const struct hci_init_stage le_init3[] = { | |
3420 | /* HCI_OP_LE_SET_EVENT_MASK */ | |
3421 | HCI_INIT(hci_le_set_event_mask_sync), | |
3422 | /* HCI_OP_LE_READ_ADV_TX_POWER */ | |
3423 | HCI_INIT(hci_le_read_adv_tx_power_sync), | |
3424 | /* HCI_OP_LE_READ_TRANSMIT_POWER */ | |
3425 | HCI_INIT(hci_le_read_tx_power_sync), | |
3426 | /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */ | |
3427 | HCI_INIT(hci_le_read_accept_list_size_sync), | |
3428 | /* HCI_OP_LE_CLEAR_ACCEPT_LIST */ | |
3429 | HCI_INIT(hci_le_clear_accept_list_sync), | |
3430 | /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */ | |
3431 | HCI_INIT(hci_le_read_resolv_list_size_sync), | |
3432 | /* HCI_OP_LE_CLEAR_RESOLV_LIST */ | |
3433 | HCI_INIT(hci_le_clear_resolv_list_sync), | |
3434 | /* HCI_OP_LE_SET_RPA_TIMEOUT */ | |
3435 | HCI_INIT(hci_le_set_rpa_timeout_sync), | |
3436 | /* HCI_OP_LE_READ_MAX_DATA_LEN */ | |
3437 | HCI_INIT(hci_le_read_max_data_len_sync), | |
3438 | /* HCI_OP_LE_READ_DEF_DATA_LEN */ | |
3439 | HCI_INIT(hci_le_read_def_data_len_sync), | |
3440 | /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */ | |
3441 | HCI_INIT(hci_le_read_num_support_adv_sets_sync), | |
3442 | /* HCI_OP_WRITE_LE_HOST_SUPPORTED */ | |
3443 | HCI_INIT(hci_set_le_support_sync), | |
3444 | {} | |
3445 | }; | |
3446 | ||
3447 | static int hci_init3_sync(struct hci_dev *hdev) | |
3448 | { | |
3449 | int err; | |
3450 | ||
3451 | bt_dev_dbg(hdev, ""); | |
3452 | ||
3453 | err = hci_init_stage_sync(hdev, hci_init3); | |
3454 | if (err) | |
3455 | return err; | |
3456 | ||
3457 | if (lmp_le_capable(hdev)) | |
3458 | return hci_init_stage_sync(hdev, le_init3); | |
3459 | ||
3460 | return 0; | |
3461 | } | |
3462 | ||
3463 | static int hci_delete_stored_link_key_sync(struct hci_dev *hdev) | |
3464 | { | |
3465 | struct hci_cp_delete_stored_link_key cp; | |
3466 | ||
3467 | /* Some Broadcom based Bluetooth controllers do not support the | |
3468 | * Delete Stored Link Key command. They are clearly indicating its | |
3469 | * absence in the bit mask of supported commands. | |
3470 | * | |
3471 | * Check the supported commands and only if the command is marked | |
3472 | * as supported send it. If not supported assume that the controller | |
3473 | * does not have actual support for stored link keys which makes this | |
3474 | * command redundant anyway. | |
3475 | * | |
3476 | * Some controllers indicate that they support handling deleting | |
3477 | * stored link keys, but they don't. The quirk lets a driver | |
3478 | * just disable this command. | |
3479 | */ | |
3480 | if (!(hdev->commands[6] & 0x80) || | |
3481 | test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) | |
3482 | return 0; | |
3483 | ||
3484 | memset(&cp, 0, sizeof(cp)); | |
3485 | bacpy(&cp.bdaddr, BDADDR_ANY); | |
3486 | cp.delete_all = 0x01; | |
3487 | ||
3488 | return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY, | |
3489 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3490 | } | |
3491 | ||
3492 | static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev) | |
3493 | { | |
3494 | u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
3495 | bool changed = false; | |
3496 | ||
3497 | /* Set event mask page 2 if the HCI command for it is supported */ | |
3498 | if (!(hdev->commands[22] & 0x04)) | |
3499 | return 0; | |
3500 | ||
3501 | /* If Connectionless Peripheral Broadcast central role is supported | |
3502 | * enable all necessary events for it. | |
3503 | */ | |
3504 | if (lmp_cpb_central_capable(hdev)) { | |
3505 | events[1] |= 0x40; /* Triggered Clock Capture */ | |
3506 | events[1] |= 0x80; /* Synchronization Train Complete */ | |
3507 | events[2] |= 0x10; /* Peripheral Page Response Timeout */ | |
3508 | events[2] |= 0x20; /* CPB Channel Map Change */ | |
3509 | changed = true; | |
3510 | } | |
3511 | ||
3512 | /* If Connectionless Peripheral Broadcast peripheral role is supported | |
3513 | * enable all necessary events for it. | |
3514 | */ | |
3515 | if (lmp_cpb_peripheral_capable(hdev)) { | |
3516 | events[2] |= 0x01; /* Synchronization Train Received */ | |
3517 | events[2] |= 0x02; /* CPB Receive */ | |
3518 | events[2] |= 0x04; /* CPB Timeout */ | |
3519 | events[2] |= 0x08; /* Truncated Page Complete */ | |
3520 | changed = true; | |
3521 | } | |
3522 | ||
3523 | /* Enable Authenticated Payload Timeout Expired event if supported */ | |
3524 | if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) { | |
3525 | events[2] |= 0x80; | |
3526 | changed = true; | |
3527 | } | |
3528 | ||
3529 | /* Some Broadcom based controllers indicate support for Set Event | |
3530 | * Mask Page 2 command, but then actually do not support it. Since | |
3531 | * the default value is all bits set to zero, the command is only | |
3532 | * required if the event mask has to be changed. In case no change | |
3533 | * to the event mask is needed, skip this command. | |
3534 | */ | |
3535 | if (!changed) | |
3536 | return 0; | |
3537 | ||
3538 | return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2, | |
3539 | sizeof(events), events, HCI_CMD_TIMEOUT); | |
3540 | } | |
3541 | ||
3542 | /* Read local codec list if the HCI command is supported */ | |
3543 | static int hci_read_local_codecs_sync(struct hci_dev *hdev) | |
3544 | { | |
3545 | if (!(hdev->commands[29] & 0x20)) | |
3546 | return 0; | |
3547 | ||
3548 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL, | |
3549 | HCI_CMD_TIMEOUT); | |
3550 | } | |
3551 | ||
3552 | /* Read local pairing options if the HCI command is supported */ | |
3553 | static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev) | |
3554 | { | |
3555 | if (!(hdev->commands[41] & 0x08)) | |
3556 | return 0; | |
3557 | ||
3558 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS, | |
3559 | 0, NULL, HCI_CMD_TIMEOUT); | |
3560 | } | |
3561 | ||
3562 | /* Get MWS transport configuration if the HCI command is supported */ | |
3563 | static int hci_get_mws_transport_config_sync(struct hci_dev *hdev) | |
3564 | { | |
3565 | if (!(hdev->commands[30] & 0x08)) | |
3566 | return 0; | |
3567 | ||
3568 | return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG, | |
3569 | 0, NULL, HCI_CMD_TIMEOUT); | |
3570 | } | |
3571 | ||
3572 | /* Check for Synchronization Train support */ | |
3573 | static int hci_read_sync_train_params_sync(struct hci_dev *hdev) | |
3574 | { | |
3575 | if (!lmp_sync_train_capable(hdev)) | |
3576 | return 0; | |
3577 | ||
3578 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS, | |
3579 | 0, NULL, HCI_CMD_TIMEOUT); | |
3580 | } | |
3581 | ||
3582 | /* Enable Secure Connections if supported and configured */ | |
3583 | static int hci_write_sc_support_1_sync(struct hci_dev *hdev) | |
3584 | { | |
3585 | u8 support = 0x01; | |
3586 | ||
3587 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || | |
3588 | !bredr_sc_enabled(hdev)) | |
3589 | return 0; | |
3590 | ||
3591 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, | |
3592 | sizeof(support), &support, | |
3593 | HCI_CMD_TIMEOUT); | |
3594 | } | |
3595 | ||
3596 | /* Set erroneous data reporting if supported to the wideband speech | |
3597 | * setting value | |
3598 | */ | |
3599 | static int hci_set_err_data_report_sync(struct hci_dev *hdev) | |
3600 | { | |
3601 | struct hci_cp_write_def_err_data_reporting cp; | |
3602 | bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED); | |
3603 | ||
3604 | if (!(hdev->commands[18] & 0x08) || | |
3605 | test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks)) | |
3606 | return 0; | |
3607 | ||
3608 | if (enabled == hdev->err_data_reporting) | |
3609 | return 0; | |
3610 | ||
3611 | memset(&cp, 0, sizeof(cp)); | |
3612 | cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED : | |
3613 | ERR_DATA_REPORTING_DISABLED; | |
3614 | ||
3615 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING, | |
3616 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3617 | } | |
3618 | ||
3619 | static const struct hci_init_stage hci_init4[] = { | |
3620 | /* HCI_OP_DELETE_STORED_LINK_KEY */ | |
3621 | HCI_INIT(hci_delete_stored_link_key_sync), | |
3622 | /* HCI_OP_SET_EVENT_MASK_PAGE_2 */ | |
3623 | HCI_INIT(hci_set_event_mask_page_2_sync), | |
3624 | /* HCI_OP_READ_LOCAL_CODECS */ | |
3625 | HCI_INIT(hci_read_local_codecs_sync), | |
3626 | /* HCI_OP_READ_LOCAL_PAIRING_OPTS */ | |
3627 | HCI_INIT(hci_read_local_pairing_opts_sync), | |
3628 | /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */ | |
3629 | HCI_INIT(hci_get_mws_transport_config_sync), | |
3630 | /* HCI_OP_READ_SYNC_TRAIN_PARAMS */ | |
3631 | HCI_INIT(hci_read_sync_train_params_sync), | |
3632 | /* HCI_OP_WRITE_SC_SUPPORT */ | |
3633 | HCI_INIT(hci_write_sc_support_1_sync), | |
3634 | /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */ | |
3635 | HCI_INIT(hci_set_err_data_report_sync), | |
3636 | {} | |
3637 | }; | |
3638 | ||
3639 | /* Set Suggested Default Data Length to maximum if supported */ | |
3640 | static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev) | |
3641 | { | |
3642 | struct hci_cp_le_write_def_data_len cp; | |
3643 | ||
3644 | if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)) | |
3645 | return 0; | |
3646 | ||
3647 | memset(&cp, 0, sizeof(cp)); | |
3648 | cp.tx_len = cpu_to_le16(hdev->le_max_tx_len); | |
3649 | cp.tx_time = cpu_to_le16(hdev->le_max_tx_time); | |
3650 | ||
3651 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN, | |
3652 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3653 | } | |
3654 | ||
3655 | /* Set Default PHY parameters if command is supported */ | |
3656 | static int hci_le_set_default_phy_sync(struct hci_dev *hdev) | |
3657 | { | |
3658 | struct hci_cp_le_set_default_phy cp; | |
3659 | ||
3660 | if (!(hdev->commands[35] & 0x20)) | |
3661 | return 0; | |
3662 | ||
3663 | memset(&cp, 0, sizeof(cp)); | |
3664 | cp.all_phys = 0x00; | |
3665 | cp.tx_phys = hdev->le_tx_def_phys; | |
3666 | cp.rx_phys = hdev->le_rx_def_phys; | |
3667 | ||
3668 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY, | |
3669 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
3670 | } | |
3671 | ||
3672 | static const struct hci_init_stage le_init4[] = { | |
3673 | /* HCI_OP_LE_WRITE_DEF_DATA_LEN */ | |
3674 | HCI_INIT(hci_le_set_write_def_data_len_sync), | |
3675 | /* HCI_OP_LE_SET_DEFAULT_PHY */ | |
3676 | HCI_INIT(hci_le_set_default_phy_sync), | |
3677 | {} | |
3678 | }; | |
3679 | ||
3680 | static int hci_init4_sync(struct hci_dev *hdev) | |
3681 | { | |
3682 | int err; | |
3683 | ||
3684 | bt_dev_dbg(hdev, ""); | |
3685 | ||
3686 | err = hci_init_stage_sync(hdev, hci_init4); | |
3687 | if (err) | |
3688 | return err; | |
3689 | ||
3690 | if (lmp_le_capable(hdev)) | |
3691 | return hci_init_stage_sync(hdev, le_init4); | |
3692 | ||
3693 | return 0; | |
3694 | } | |
3695 | ||
3696 | static int hci_init_sync(struct hci_dev *hdev) | |
3697 | { | |
3698 | int err; | |
3699 | ||
3700 | err = hci_init1_sync(hdev); | |
3701 | if (err < 0) | |
3702 | return err; | |
3703 | ||
3704 | if (hci_dev_test_flag(hdev, HCI_SETUP)) | |
3705 | hci_debugfs_create_basic(hdev); | |
3706 | ||
3707 | err = hci_init2_sync(hdev); | |
3708 | if (err < 0) | |
3709 | return err; | |
3710 | ||
3711 | /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode | |
3712 | * BR/EDR/LE type controllers. AMP controllers only need the | |
3713 | * first two stages of init. | |
3714 | */ | |
3715 | if (hdev->dev_type != HCI_PRIMARY) | |
3716 | return 0; | |
3717 | ||
3718 | err = hci_init3_sync(hdev); | |
3719 | if (err < 0) | |
3720 | return err; | |
3721 | ||
3722 | err = hci_init4_sync(hdev); | |
3723 | if (err < 0) | |
3724 | return err; | |
3725 | ||
3726 | /* This function is only called when the controller is actually in | |
3727 | * configured state. When the controller is marked as unconfigured, | |
3728 | * this initialization procedure is not run. | |
3729 | * | |
3730 | * It means that it is possible that a controller runs through its | |
3731 | * setup phase and then discovers missing settings. If that is the | |
3732 | * case, then this function will not be called. It then will only | |
3733 | * be called during the config phase. | |
3734 | * | |
3735 | * So only when in setup phase or config phase, create the debugfs | |
3736 | * entries and register the SMP channels. | |
3737 | */ | |
3738 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
3739 | !hci_dev_test_flag(hdev, HCI_CONFIG)) | |
3740 | return 0; | |
3741 | ||
3742 | hci_debugfs_create_common(hdev); | |
3743 | ||
3744 | if (lmp_bredr_capable(hdev)) | |
3745 | hci_debugfs_create_bredr(hdev); | |
3746 | ||
3747 | if (lmp_le_capable(hdev)) | |
3748 | hci_debugfs_create_le(hdev); | |
3749 | ||
3750 | return 0; | |
3751 | } | |
3752 | ||
3753 | int hci_dev_open_sync(struct hci_dev *hdev) | |
3754 | { | |
3755 | int ret = 0; | |
3756 | ||
3757 | bt_dev_dbg(hdev, ""); | |
3758 | ||
3759 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { | |
3760 | ret = -ENODEV; | |
3761 | goto done; | |
3762 | } | |
3763 | ||
3764 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
3765 | !hci_dev_test_flag(hdev, HCI_CONFIG)) { | |
3766 | /* Check for rfkill but allow the HCI setup stage to | |
3767 | * proceed (which in itself doesn't cause any RF activity). | |
3768 | */ | |
3769 | if (hci_dev_test_flag(hdev, HCI_RFKILLED)) { | |
3770 | ret = -ERFKILL; | |
3771 | goto done; | |
3772 | } | |
3773 | ||
3774 | /* Check for valid public address or a configured static | |
3775 | * random address, but let the HCI setup proceed to | |
3776 | * be able to determine if there is a public address | |
3777 | * or not. | |
3778 | * | |
3779 | * In case of user channel usage, it is not important | |
3780 | * if a public address or static random address is | |
3781 | * available. | |
3782 | * | |
3783 | * This check is only valid for BR/EDR controllers | |
3784 | * since AMP controllers do not have an address. | |
3785 | */ | |
3786 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
3787 | hdev->dev_type == HCI_PRIMARY && | |
3788 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && | |
3789 | !bacmp(&hdev->static_addr, BDADDR_ANY)) { | |
3790 | ret = -EADDRNOTAVAIL; | |
3791 | goto done; | |
3792 | } | |
3793 | } | |
3794 | ||
3795 | if (test_bit(HCI_UP, &hdev->flags)) { | |
3796 | ret = -EALREADY; | |
3797 | goto done; | |
3798 | } | |
3799 | ||
3800 | if (hdev->open(hdev)) { | |
3801 | ret = -EIO; | |
3802 | goto done; | |
3803 | } | |
3804 | ||
3805 | set_bit(HCI_RUNNING, &hdev->flags); | |
3806 | hci_sock_dev_event(hdev, HCI_DEV_OPEN); | |
3807 | ||
3808 | atomic_set(&hdev->cmd_cnt, 1); | |
3809 | set_bit(HCI_INIT, &hdev->flags); | |
3810 | ||
3811 | if (hci_dev_test_flag(hdev, HCI_SETUP) || | |
3812 | test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) { | |
3813 | bool invalid_bdaddr; | |
3814 | ||
3815 | hci_sock_dev_event(hdev, HCI_DEV_SETUP); | |
3816 | ||
3817 | if (hdev->setup) | |
3818 | ret = hdev->setup(hdev); | |
3819 | ||
3820 | /* The transport driver can set the quirk to mark the | |
3821 | * BD_ADDR invalid before creating the HCI device or in | |
3822 | * its setup callback. | |
3823 | */ | |
3824 | invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, | |
3825 | &hdev->quirks); | |
3826 | ||
3827 | if (ret) | |
3828 | goto setup_failed; | |
3829 | ||
3830 | if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) { | |
3831 | if (!bacmp(&hdev->public_addr, BDADDR_ANY)) | |
3832 | hci_dev_get_bd_addr_from_property(hdev); | |
3833 | ||
3834 | if (bacmp(&hdev->public_addr, BDADDR_ANY) && | |
3835 | hdev->set_bdaddr) { | |
3836 | ret = hdev->set_bdaddr(hdev, | |
3837 | &hdev->public_addr); | |
3838 | ||
3839 | /* If setting of the BD_ADDR from the device | |
3840 | * property succeeds, then treat the address | |
3841 | * as valid even if the invalid BD_ADDR | |
3842 | * quirk indicates otherwise. | |
3843 | */ | |
3844 | if (!ret) | |
3845 | invalid_bdaddr = false; | |
3846 | } | |
3847 | } | |
3848 | ||
3849 | setup_failed: | |
3850 | /* The transport driver can set these quirks before | |
3851 | * creating the HCI device or in its setup callback. | |
3852 | * | |
3853 | * For the invalid BD_ADDR quirk it is possible that | |
3854 | * it becomes a valid address if the bootloader does | |
3855 | * provide it (see above). | |
3856 | * | |
3857 | * In case any of them is set, the controller has to | |
3858 | * start up as unconfigured. | |
3859 | */ | |
3860 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || | |
3861 | invalid_bdaddr) | |
3862 | hci_dev_set_flag(hdev, HCI_UNCONFIGURED); | |
3863 | ||
3864 | /* For an unconfigured controller it is required to | |
3865 | * read at least the version information provided by | |
3866 | * the Read Local Version Information command. | |
3867 | * | |
3868 | * If the set_bdaddr driver callback is provided, then | |
3869 | * also the original Bluetooth public device address | |
3870 | * will be read using the Read BD Address command. | |
3871 | */ | |
3872 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
3873 | ret = hci_unconf_init_sync(hdev); | |
3874 | } | |
3875 | ||
3876 | if (hci_dev_test_flag(hdev, HCI_CONFIG)) { | |
3877 | /* If public address change is configured, ensure that | |
3878 | * the address gets programmed. If the driver does not | |
3879 | * support changing the public address, fail the power | |
3880 | * on procedure. | |
3881 | */ | |
3882 | if (bacmp(&hdev->public_addr, BDADDR_ANY) && | |
3883 | hdev->set_bdaddr) | |
3884 | ret = hdev->set_bdaddr(hdev, &hdev->public_addr); | |
3885 | else | |
3886 | ret = -EADDRNOTAVAIL; | |
3887 | } | |
3888 | ||
3889 | if (!ret) { | |
3890 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && | |
3891 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { | |
3892 | ret = hci_init_sync(hdev); | |
3893 | if (!ret && hdev->post_init) | |
3894 | ret = hdev->post_init(hdev); | |
3895 | } | |
3896 | } | |
3897 | ||
3898 | /* If the HCI Reset command is clearing all diagnostic settings, | |
3899 | * then they need to be reprogrammed after the init procedure | |
3900 | * completed. | |
3901 | */ | |
3902 | if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) && | |
3903 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
3904 | hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag) | |
3905 | ret = hdev->set_diag(hdev, true); | |
3906 | ||
385315de JM |
3907 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
3908 | msft_do_open(hdev); | |
3909 | aosp_do_open(hdev); | |
3910 | } | |
d0b13706 LAD |
3911 | |
3912 | clear_bit(HCI_INIT, &hdev->flags); | |
3913 | ||
3914 | if (!ret) { | |
3915 | hci_dev_hold(hdev); | |
3916 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); | |
3917 | hci_adv_instances_set_rpa_expired(hdev, true); | |
3918 | set_bit(HCI_UP, &hdev->flags); | |
3919 | hci_sock_dev_event(hdev, HCI_DEV_UP); | |
3920 | hci_leds_update_powered(hdev, true); | |
3921 | if (!hci_dev_test_flag(hdev, HCI_SETUP) && | |
3922 | !hci_dev_test_flag(hdev, HCI_CONFIG) && | |
3923 | !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && | |
3924 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
3925 | hci_dev_test_flag(hdev, HCI_MGMT) && | |
3926 | hdev->dev_type == HCI_PRIMARY) { | |
3927 | ret = hci_powered_update_sync(hdev); | |
3928 | } | |
3929 | } else { | |
3930 | /* Init failed, cleanup */ | |
3931 | flush_work(&hdev->tx_work); | |
3932 | ||
3933 | /* Since hci_rx_work() is possible to awake new cmd_work | |
3934 | * it should be flushed first to avoid unexpected call of | |
3935 | * hci_cmd_work() | |
3936 | */ | |
3937 | flush_work(&hdev->rx_work); | |
3938 | flush_work(&hdev->cmd_work); | |
3939 | ||
3940 | skb_queue_purge(&hdev->cmd_q); | |
3941 | skb_queue_purge(&hdev->rx_q); | |
3942 | ||
3943 | if (hdev->flush) | |
3944 | hdev->flush(hdev); | |
3945 | ||
3946 | if (hdev->sent_cmd) { | |
3947 | kfree_skb(hdev->sent_cmd); | |
3948 | hdev->sent_cmd = NULL; | |
3949 | } | |
3950 | ||
3951 | clear_bit(HCI_RUNNING, &hdev->flags); | |
3952 | hci_sock_dev_event(hdev, HCI_DEV_CLOSE); | |
3953 | ||
3954 | hdev->close(hdev); | |
3955 | hdev->flags &= BIT(HCI_RAW); | |
3956 | } | |
3957 | ||
3958 | done: | |
3959 | return ret; | |
3960 | } | |
3961 | ||
3962 | /* This function requires the caller holds hdev->lock */ | |
3963 | static void hci_pend_le_actions_clear(struct hci_dev *hdev) | |
3964 | { | |
3965 | struct hci_conn_params *p; | |
3966 | ||
3967 | list_for_each_entry(p, &hdev->le_conn_params, list) { | |
3968 | if (p->conn) { | |
3969 | hci_conn_drop(p->conn); | |
3970 | hci_conn_put(p->conn); | |
3971 | p->conn = NULL; | |
3972 | } | |
3973 | list_del_init(&p->action); | |
3974 | } | |
3975 | ||
3976 | BT_DBG("All LE pending actions cleared"); | |
3977 | } | |
3978 | ||
3979 | int hci_dev_close_sync(struct hci_dev *hdev) | |
3980 | { | |
3981 | bool auto_off; | |
3982 | int err = 0; | |
3983 | ||
3984 | bt_dev_dbg(hdev, ""); | |
3985 | ||
3986 | cancel_delayed_work(&hdev->power_off); | |
3987 | cancel_delayed_work(&hdev->ncmd_timer); | |
3988 | ||
3989 | hci_request_cancel_all(hdev); | |
3990 | ||
3991 | if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && | |
3992 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
3993 | test_bit(HCI_UP, &hdev->flags)) { | |
3994 | /* Execute vendor specific shutdown routine */ | |
3995 | if (hdev->shutdown) | |
3996 | err = hdev->shutdown(hdev); | |
3997 | } | |
3998 | ||
3999 | if (!test_and_clear_bit(HCI_UP, &hdev->flags)) { | |
4000 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
4001 | return err; | |
4002 | } | |
4003 | ||
4004 | hci_leds_update_powered(hdev, false); | |
4005 | ||
4006 | /* Flush RX and TX works */ | |
4007 | flush_work(&hdev->tx_work); | |
4008 | flush_work(&hdev->rx_work); | |
4009 | ||
4010 | if (hdev->discov_timeout > 0) { | |
4011 | hdev->discov_timeout = 0; | |
4012 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); | |
4013 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); | |
4014 | } | |
4015 | ||
4016 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) | |
4017 | cancel_delayed_work(&hdev->service_cache); | |
4018 | ||
4019 | if (hci_dev_test_flag(hdev, HCI_MGMT)) { | |
4020 | struct adv_info *adv_instance; | |
4021 | ||
4022 | cancel_delayed_work_sync(&hdev->rpa_expired); | |
4023 | ||
4024 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) | |
4025 | cancel_delayed_work_sync(&adv_instance->rpa_expired_cb); | |
4026 | } | |
4027 | ||
4028 | /* Avoid potential lockdep warnings from the *_flush() calls by | |
4029 | * ensuring the workqueue is empty up front. | |
4030 | */ | |
4031 | drain_workqueue(hdev->workqueue); | |
4032 | ||
4033 | hci_dev_lock(hdev); | |
4034 | ||
4035 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); | |
4036 | ||
4037 | auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF); | |
4038 | ||
4039 | if (!auto_off && hdev->dev_type == HCI_PRIMARY && | |
4040 | !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) && | |
4041 | hci_dev_test_flag(hdev, HCI_MGMT)) | |
4042 | __mgmt_power_off(hdev); | |
4043 | ||
4044 | hci_inquiry_cache_flush(hdev); | |
4045 | hci_pend_le_actions_clear(hdev); | |
4046 | hci_conn_hash_flush(hdev); | |
4047 | hci_dev_unlock(hdev); | |
4048 | ||
4049 | smp_unregister(hdev); | |
4050 | ||
4051 | hci_sock_dev_event(hdev, HCI_DEV_DOWN); | |
4052 | ||
385315de JM |
4053 | if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
4054 | aosp_do_close(hdev); | |
4055 | msft_do_close(hdev); | |
4056 | } | |
d0b13706 LAD |
4057 | |
4058 | if (hdev->flush) | |
4059 | hdev->flush(hdev); | |
4060 | ||
4061 | /* Reset device */ | |
4062 | skb_queue_purge(&hdev->cmd_q); | |
4063 | atomic_set(&hdev->cmd_cnt, 1); | |
4064 | if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) && | |
4065 | !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { | |
4066 | set_bit(HCI_INIT, &hdev->flags); | |
4067 | hci_reset_sync(hdev); | |
4068 | clear_bit(HCI_INIT, &hdev->flags); | |
4069 | } | |
4070 | ||
4071 | /* flush cmd work */ | |
4072 | flush_work(&hdev->cmd_work); | |
4073 | ||
4074 | /* Drop queues */ | |
4075 | skb_queue_purge(&hdev->rx_q); | |
4076 | skb_queue_purge(&hdev->cmd_q); | |
4077 | skb_queue_purge(&hdev->raw_q); | |
4078 | ||
4079 | /* Drop last sent command */ | |
4080 | if (hdev->sent_cmd) { | |
4081 | cancel_delayed_work_sync(&hdev->cmd_timer); | |
4082 | kfree_skb(hdev->sent_cmd); | |
4083 | hdev->sent_cmd = NULL; | |
4084 | } | |
4085 | ||
4086 | clear_bit(HCI_RUNNING, &hdev->flags); | |
4087 | hci_sock_dev_event(hdev, HCI_DEV_CLOSE); | |
4088 | ||
d0b13706 LAD |
4089 | /* After this point our queues are empty and no tasks are scheduled. */ |
4090 | hdev->close(hdev); | |
4091 | ||
4092 | /* Clear flags */ | |
4093 | hdev->flags &= BIT(HCI_RAW); | |
4094 | hci_dev_clear_volatile_flags(hdev); | |
4095 | ||
4096 | /* Controller radio is available but is currently powered down */ | |
4097 | hdev->amp_status = AMP_STATUS_POWERED_DOWN; | |
4098 | ||
4099 | memset(hdev->eir, 0, sizeof(hdev->eir)); | |
4100 | memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); | |
4101 | bacpy(&hdev->random_addr, BDADDR_ANY); | |
4102 | ||
4103 | hci_dev_put(hdev); | |
4104 | return err; | |
4105 | } | |
4106 | ||
4107 | /* This function perform power on HCI command sequence as follows: | |
4108 | * | |
4109 | * If controller is already up (HCI_UP) performs hci_powered_update_sync | |
4110 | * sequence otherwise run hci_dev_open_sync which will follow with | |
4111 | * hci_powered_update_sync after the init sequence is completed. | |
4112 | */ | |
4113 | static int hci_power_on_sync(struct hci_dev *hdev) | |
4114 | { | |
4115 | int err; | |
4116 | ||
4117 | if (test_bit(HCI_UP, &hdev->flags) && | |
4118 | hci_dev_test_flag(hdev, HCI_MGMT) && | |
4119 | hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { | |
4120 | cancel_delayed_work(&hdev->power_off); | |
4121 | return hci_powered_update_sync(hdev); | |
4122 | } | |
4123 | ||
4124 | err = hci_dev_open_sync(hdev); | |
4125 | if (err < 0) | |
4126 | return err; | |
4127 | ||
4128 | /* During the HCI setup phase, a few error conditions are | |
4129 | * ignored and they need to be checked now. If they are still | |
4130 | * valid, it is important to return the device back off. | |
4131 | */ | |
4132 | if (hci_dev_test_flag(hdev, HCI_RFKILLED) || | |
4133 | hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || | |
4134 | (hdev->dev_type == HCI_PRIMARY && | |
4135 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && | |
4136 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
4137 | hci_dev_clear_flag(hdev, HCI_AUTO_OFF); | |
4138 | hci_dev_close_sync(hdev); | |
4139 | } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { | |
4140 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, | |
4141 | HCI_AUTO_OFF_TIMEOUT); | |
4142 | } | |
4143 | ||
4144 | if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { | |
4145 | /* For unconfigured devices, set the HCI_RAW flag | |
4146 | * so that userspace can easily identify them. | |
4147 | */ | |
4148 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
4149 | set_bit(HCI_RAW, &hdev->flags); | |
4150 | ||
4151 | /* For fully configured devices, this will send | |
4152 | * the Index Added event. For unconfigured devices, | |
4153 | * it will send Unconfigued Index Added event. | |
4154 | * | |
4155 | * Devices with HCI_QUIRK_RAW_DEVICE are ignored | |
4156 | * and no event will be send. | |
4157 | */ | |
4158 | mgmt_index_added(hdev); | |
4159 | } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { | |
4160 | /* When the controller is now configured, then it | |
4161 | * is important to clear the HCI_RAW flag. | |
4162 | */ | |
4163 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
4164 | clear_bit(HCI_RAW, &hdev->flags); | |
4165 | ||
4166 | /* Powering on the controller with HCI_CONFIG set only | |
4167 | * happens with the transition from unconfigured to | |
4168 | * configured. This will send the Index Added event. | |
4169 | */ | |
4170 | mgmt_index_added(hdev); | |
4171 | } | |
4172 | ||
4173 | return 0; | |
4174 | } | |
4175 | ||
4176 | static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr) | |
4177 | { | |
4178 | struct hci_cp_remote_name_req_cancel cp; | |
4179 | ||
4180 | memset(&cp, 0, sizeof(cp)); | |
4181 | bacpy(&cp.bdaddr, addr); | |
4182 | ||
4183 | return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, | |
4184 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4185 | } | |
4186 | ||
4187 | int hci_stop_discovery_sync(struct hci_dev *hdev) | |
4188 | { | |
4189 | struct discovery_state *d = &hdev->discovery; | |
4190 | struct inquiry_entry *e; | |
4191 | int err; | |
4192 | ||
4193 | bt_dev_dbg(hdev, "state %u", hdev->discovery.state); | |
4194 | ||
4195 | if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) { | |
4196 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { | |
4197 | err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, | |
4198 | 0, NULL, HCI_CMD_TIMEOUT); | |
4199 | if (err) | |
4200 | return err; | |
4201 | } | |
4202 | ||
4203 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { | |
4204 | cancel_delayed_work(&hdev->le_scan_disable); | |
4205 | cancel_delayed_work(&hdev->le_scan_restart); | |
4206 | ||
4207 | err = hci_scan_disable_sync(hdev); | |
4208 | if (err) | |
4209 | return err; | |
4210 | } | |
4211 | ||
4212 | } else { | |
4213 | err = hci_scan_disable_sync(hdev); | |
4214 | if (err) | |
4215 | return err; | |
4216 | } | |
4217 | ||
4218 | /* Resume advertising if it was paused */ | |
4219 | if (use_ll_privacy(hdev)) | |
4220 | hci_resume_advertising_sync(hdev); | |
4221 | ||
4222 | /* No further actions needed for LE-only discovery */ | |
4223 | if (d->type == DISCOV_TYPE_LE) | |
4224 | return 0; | |
4225 | ||
4226 | if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) { | |
4227 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, | |
4228 | NAME_PENDING); | |
4229 | if (!e) | |
4230 | return 0; | |
4231 | ||
4232 | return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr); | |
4233 | } | |
4234 | ||
4235 | return 0; | |
4236 | } | |
4237 | ||
4238 | static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle, | |
4239 | u8 reason) | |
4240 | { | |
4241 | struct hci_cp_disconn_phy_link cp; | |
4242 | ||
4243 | memset(&cp, 0, sizeof(cp)); | |
4244 | cp.phy_handle = HCI_PHY_HANDLE(handle); | |
4245 | cp.reason = reason; | |
4246 | ||
4247 | return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK, | |
4248 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4249 | } | |
4250 | ||
4251 | static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
4252 | u8 reason) | |
4253 | { | |
4254 | struct hci_cp_disconnect cp; | |
4255 | ||
4256 | if (conn->type == AMP_LINK) | |
4257 | return hci_disconnect_phy_link_sync(hdev, conn->handle, reason); | |
4258 | ||
4259 | memset(&cp, 0, sizeof(cp)); | |
4260 | cp.handle = cpu_to_le16(conn->handle); | |
4261 | cp.reason = reason; | |
4262 | ||
4263 | /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not | |
4264 | * suspending. | |
4265 | */ | |
4266 | if (!hdev->suspended) | |
4267 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT, | |
4268 | sizeof(cp), &cp, | |
4269 | HCI_EV_DISCONN_COMPLETE, | |
4270 | HCI_CMD_TIMEOUT, NULL); | |
4271 | ||
4272 | return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp, | |
4273 | HCI_CMD_TIMEOUT); | |
4274 | } | |
4275 | ||
4276 | static int hci_le_connect_cancel_sync(struct hci_dev *hdev, | |
4277 | struct hci_conn *conn) | |
4278 | { | |
4279 | if (test_bit(HCI_CONN_SCANNING, &conn->flags)) | |
4280 | return 0; | |
4281 | ||
4282 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL, | |
4283 | 6, &conn->dst, HCI_CMD_TIMEOUT); | |
4284 | } | |
4285 | ||
4286 | static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn) | |
4287 | { | |
4288 | if (conn->type == LE_LINK) | |
4289 | return hci_le_connect_cancel_sync(hdev, conn); | |
4290 | ||
4291 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
4292 | return 0; | |
4293 | ||
4294 | return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL, | |
4295 | 6, &conn->dst, HCI_CMD_TIMEOUT); | |
4296 | } | |
4297 | ||
4298 | static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
4299 | u8 reason) | |
4300 | { | |
4301 | struct hci_cp_reject_sync_conn_req cp; | |
4302 | ||
4303 | memset(&cp, 0, sizeof(cp)); | |
4304 | bacpy(&cp.bdaddr, &conn->dst); | |
4305 | cp.reason = reason; | |
4306 | ||
4307 | /* SCO rejection has its own limited set of | |
4308 | * allowed error values (0x0D-0x0F). | |
4309 | */ | |
4310 | if (reason < 0x0d || reason > 0x0f) | |
4311 | cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; | |
4312 | ||
4313 | return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ, | |
4314 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4315 | } | |
4316 | ||
4317 | static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
4318 | u8 reason) | |
4319 | { | |
4320 | struct hci_cp_reject_conn_req cp; | |
4321 | ||
4322 | if (conn->type == SCO_LINK || conn->type == ESCO_LINK) | |
4323 | return hci_reject_sco_sync(hdev, conn, reason); | |
4324 | ||
4325 | memset(&cp, 0, sizeof(cp)); | |
4326 | bacpy(&cp.bdaddr, &conn->dst); | |
4327 | cp.reason = reason; | |
4328 | ||
4329 | return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ, | |
4330 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4331 | } | |
4332 | ||
4333 | static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
4334 | u8 reason) | |
4335 | { | |
4336 | switch (conn->state) { | |
4337 | case BT_CONNECTED: | |
4338 | case BT_CONFIG: | |
4339 | return hci_disconnect_sync(hdev, conn, reason); | |
4340 | case BT_CONNECT: | |
4341 | return hci_connect_cancel_sync(hdev, conn); | |
4342 | case BT_CONNECT2: | |
4343 | return hci_reject_conn_sync(hdev, conn, reason); | |
4344 | default: | |
4345 | conn->state = BT_CLOSED; | |
4346 | break; | |
4347 | } | |
4348 | ||
4349 | return 0; | |
4350 | } | |
4351 | ||
182ee45d LAD |
4352 | static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason) |
4353 | { | |
4354 | struct hci_conn *conn, *tmp; | |
4355 | int err; | |
4356 | ||
4357 | list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) { | |
4358 | err = hci_abort_conn_sync(hdev, conn, reason); | |
4359 | if (err) | |
4360 | return err; | |
4361 | } | |
4362 | ||
4363 | return err; | |
4364 | } | |
4365 | ||
d0b13706 LAD |
4366 | /* This function perform power off HCI command sequence as follows: |
4367 | * | |
4368 | * Clear Advertising | |
4369 | * Stop Discovery | |
4370 | * Disconnect all connections | |
4371 | * hci_dev_close_sync | |
4372 | */ | |
4373 | static int hci_power_off_sync(struct hci_dev *hdev) | |
4374 | { | |
d0b13706 LAD |
4375 | int err; |
4376 | ||
4377 | /* If controller is already down there is nothing to do */ | |
4378 | if (!test_bit(HCI_UP, &hdev->flags)) | |
4379 | return 0; | |
4380 | ||
4381 | if (test_bit(HCI_ISCAN, &hdev->flags) || | |
4382 | test_bit(HCI_PSCAN, &hdev->flags)) { | |
4383 | err = hci_write_scan_enable_sync(hdev, 0x00); | |
4384 | if (err) | |
4385 | return err; | |
4386 | } | |
4387 | ||
4388 | err = hci_clear_adv_sync(hdev, NULL, false); | |
4389 | if (err) | |
4390 | return err; | |
4391 | ||
4392 | err = hci_stop_discovery_sync(hdev); | |
4393 | if (err) | |
4394 | return err; | |
4395 | ||
182ee45d LAD |
4396 | /* Terminated due to Power Off */ |
4397 | err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); | |
4398 | if (err) | |
4399 | return err; | |
d0b13706 LAD |
4400 | |
4401 | return hci_dev_close_sync(hdev); | |
4402 | } | |
4403 | ||
4404 | int hci_set_powered_sync(struct hci_dev *hdev, u8 val) | |
4405 | { | |
4406 | if (val) | |
4407 | return hci_power_on_sync(hdev); | |
cf75ad8b LAD |
4408 | |
4409 | return hci_power_off_sync(hdev); | |
4410 | } | |
abfeea47 | 4411 | |
2bd1b237 LAD |
4412 | static int hci_write_iac_sync(struct hci_dev *hdev) |
4413 | { | |
4414 | struct hci_cp_write_current_iac_lap cp; | |
4415 | ||
4416 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) | |
4417 | return 0; | |
4418 | ||
4419 | memset(&cp, 0, sizeof(cp)); | |
4420 | ||
4421 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { | |
4422 | /* Limited discoverable mode */ | |
4423 | cp.num_iac = min_t(u8, hdev->num_iac, 2); | |
4424 | cp.iac_lap[0] = 0x00; /* LIAC */ | |
4425 | cp.iac_lap[1] = 0x8b; | |
4426 | cp.iac_lap[2] = 0x9e; | |
4427 | cp.iac_lap[3] = 0x33; /* GIAC */ | |
4428 | cp.iac_lap[4] = 0x8b; | |
4429 | cp.iac_lap[5] = 0x9e; | |
4430 | } else { | |
4431 | /* General discoverable mode */ | |
4432 | cp.num_iac = 1; | |
4433 | cp.iac_lap[0] = 0x33; /* GIAC */ | |
4434 | cp.iac_lap[1] = 0x8b; | |
4435 | cp.iac_lap[2] = 0x9e; | |
4436 | } | |
4437 | ||
4438 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP, | |
4439 | (cp.num_iac * 3) + 1, &cp, | |
4440 | HCI_CMD_TIMEOUT); | |
4441 | } | |
4442 | ||
4443 | int hci_update_discoverable_sync(struct hci_dev *hdev) | |
4444 | { | |
4445 | int err = 0; | |
4446 | ||
4447 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { | |
4448 | err = hci_write_iac_sync(hdev); | |
4449 | if (err) | |
4450 | return err; | |
4451 | ||
4452 | err = hci_update_scan_sync(hdev); | |
4453 | if (err) | |
4454 | return err; | |
4455 | ||
4456 | err = hci_update_class_sync(hdev); | |
4457 | if (err) | |
4458 | return err; | |
4459 | } | |
4460 | ||
4461 | /* Advertising instances don't use the global discoverable setting, so | |
4462 | * only update AD if advertising was enabled using Set Advertising. | |
4463 | */ | |
4464 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) { | |
4465 | err = hci_update_adv_data_sync(hdev, 0x00); | |
4466 | if (err) | |
4467 | return err; | |
4468 | ||
4469 | /* Discoverable mode affects the local advertising | |
4470 | * address in limited privacy mode. | |
4471 | */ | |
4472 | if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) { | |
4473 | if (ext_adv_capable(hdev)) | |
4474 | err = hci_start_ext_adv_sync(hdev, 0x00); | |
4475 | else | |
4476 | err = hci_enable_advertising_sync(hdev); | |
4477 | } | |
4478 | } | |
4479 | ||
4480 | return err; | |
4481 | } | |
4482 | ||
4483 | static int update_discoverable_sync(struct hci_dev *hdev, void *data) | |
4484 | { | |
4485 | return hci_update_discoverable_sync(hdev); | |
4486 | } | |
4487 | ||
4488 | int hci_update_discoverable(struct hci_dev *hdev) | |
4489 | { | |
4490 | /* Only queue if it would have any effect */ | |
4491 | if (hdev_is_powered(hdev) && | |
4492 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && | |
4493 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && | |
4494 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) | |
4495 | return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL, | |
4496 | NULL); | |
4497 | ||
4498 | return 0; | |
4499 | } | |
4500 | ||
f056a657 LAD |
4501 | int hci_update_connectable_sync(struct hci_dev *hdev) |
4502 | { | |
4503 | int err; | |
4504 | ||
4505 | err = hci_update_scan_sync(hdev); | |
4506 | if (err) | |
4507 | return err; | |
4508 | ||
4509 | /* If BR/EDR is not enabled and we disable advertising as a | |
4510 | * by-product of disabling connectable, we need to update the | |
4511 | * advertising flags. | |
4512 | */ | |
4513 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
4514 | err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance); | |
4515 | ||
4516 | /* Update the advertising parameters if necessary */ | |
4517 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || | |
4518 | !list_empty(&hdev->adv_instances)) { | |
4519 | if (ext_adv_capable(hdev)) | |
4520 | err = hci_start_ext_adv_sync(hdev, | |
4521 | hdev->cur_adv_instance); | |
4522 | else | |
4523 | err = hci_enable_advertising_sync(hdev); | |
4524 | ||
4525 | if (err) | |
4526 | return err; | |
4527 | } | |
4528 | ||
4529 | return hci_update_passive_scan_sync(hdev); | |
4530 | } | |
4531 | ||
abfeea47 LAD |
4532 | static int hci_inquiry_sync(struct hci_dev *hdev, u8 length) |
4533 | { | |
4534 | const u8 giac[3] = { 0x33, 0x8b, 0x9e }; | |
4535 | const u8 liac[3] = { 0x00, 0x8b, 0x9e }; | |
4536 | struct hci_cp_inquiry cp; | |
4537 | ||
4538 | bt_dev_dbg(hdev, ""); | |
4539 | ||
4540 | if (hci_dev_test_flag(hdev, HCI_INQUIRY)) | |
4541 | return 0; | |
4542 | ||
4543 | hci_dev_lock(hdev); | |
4544 | hci_inquiry_cache_flush(hdev); | |
4545 | hci_dev_unlock(hdev); | |
4546 | ||
4547 | memset(&cp, 0, sizeof(cp)); | |
4548 | ||
4549 | if (hdev->discovery.limited) | |
4550 | memcpy(&cp.lap, liac, sizeof(cp.lap)); | |
4551 | else | |
4552 | memcpy(&cp.lap, giac, sizeof(cp.lap)); | |
4553 | ||
4554 | cp.length = length; | |
4555 | ||
4556 | return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY, | |
4557 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
4558 | } | |
4559 | ||
4560 | static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval) | |
4561 | { | |
4562 | u8 own_addr_type; | |
4563 | /* Accept list is not used for discovery */ | |
4564 | u8 filter_policy = 0x00; | |
4565 | /* Default is to enable duplicates filter */ | |
4566 | u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; | |
4567 | int err; | |
4568 | ||
4569 | bt_dev_dbg(hdev, ""); | |
4570 | ||
4571 | /* If controller is scanning, it means the passive scanning is | |
4572 | * running. Thus, we should temporarily stop it in order to set the | |
4573 | * discovery scanning parameters. | |
4574 | */ | |
4575 | err = hci_scan_disable_sync(hdev); | |
4576 | if (err) { | |
4577 | bt_dev_err(hdev, "Unable to disable scanning: %d", err); | |
4578 | return err; | |
4579 | } | |
4580 | ||
4581 | cancel_interleave_scan(hdev); | |
4582 | ||
4583 | /* Pause advertising since active scanning disables address resolution | |
4584 | * which advertising depend on in order to generate its RPAs. | |
4585 | */ | |
4586 | if (use_ll_privacy(hdev)) { | |
4587 | err = hci_pause_advertising_sync(hdev); | |
4588 | if (err) { | |
4589 | bt_dev_err(hdev, "pause advertising failed: %d", err); | |
4590 | goto failed; | |
4591 | } | |
4592 | } | |
4593 | ||
4594 | /* Disable address resolution while doing active scanning since the | |
4595 | * accept list shall not be used and all reports shall reach the host | |
4596 | * anyway. | |
4597 | */ | |
4598 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); | |
4599 | if (err) { | |
4600 | bt_dev_err(hdev, "Unable to disable Address Resolution: %d", | |
4601 | err); | |
4602 | goto failed; | |
4603 | } | |
4604 | ||
4605 | /* All active scans will be done with either a resolvable private | |
4606 | * address (when privacy feature has been enabled) or non-resolvable | |
4607 | * private address. | |
4608 | */ | |
4609 | err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev), | |
4610 | &own_addr_type); | |
4611 | if (err < 0) | |
4612 | own_addr_type = ADDR_LE_DEV_PUBLIC; | |
4613 | ||
4614 | if (hci_is_adv_monitoring(hdev)) { | |
4615 | /* Duplicate filter should be disabled when some advertisement | |
4616 | * monitor is activated, otherwise AdvMon can only receive one | |
4617 | * advertisement for one peer(*) during active scanning, and | |
4618 | * might report loss to these peers. | |
4619 | * | |
4620 | * Note that different controllers have different meanings of | |
4621 | * |duplicate|. Some of them consider packets with the same | |
4622 | * address as duplicate, and others consider packets with the | |
4623 | * same address and the same RSSI as duplicate. Although in the | |
4624 | * latter case we don't need to disable duplicate filter, but | |
4625 | * it is common to have active scanning for a short period of | |
4626 | * time, the power impact should be neglectable. | |
4627 | */ | |
4628 | filter_dup = LE_SCAN_FILTER_DUP_DISABLE; | |
4629 | } | |
4630 | ||
4631 | err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval, | |
4632 | hdev->le_scan_window_discovery, | |
4633 | own_addr_type, filter_policy, filter_dup); | |
4634 | if (!err) | |
4635 | return err; | |
4636 | ||
4637 | failed: | |
4638 | /* Resume advertising if it was paused */ | |
4639 | if (use_ll_privacy(hdev)) | |
4640 | hci_resume_advertising_sync(hdev); | |
4641 | ||
4642 | /* Resume passive scanning */ | |
4643 | hci_update_passive_scan_sync(hdev); | |
4644 | return err; | |
4645 | } | |
4646 | ||
4647 | static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev) | |
4648 | { | |
4649 | int err; | |
4650 | ||
4651 | bt_dev_dbg(hdev, ""); | |
4652 | ||
4653 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2); | |
4654 | if (err) | |
4655 | return err; | |
4656 | ||
4657 | return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); | |
4658 | } | |
4659 | ||
4660 | int hci_start_discovery_sync(struct hci_dev *hdev) | |
4661 | { | |
4662 | unsigned long timeout; | |
4663 | int err; | |
4664 | ||
4665 | bt_dev_dbg(hdev, "type %u", hdev->discovery.type); | |
4666 | ||
4667 | switch (hdev->discovery.type) { | |
4668 | case DISCOV_TYPE_BREDR: | |
4669 | return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); | |
4670 | case DISCOV_TYPE_INTERLEAVED: | |
4671 | /* When running simultaneous discovery, the LE scanning time | |
4672 | * should occupy the whole discovery time sine BR/EDR inquiry | |
4673 | * and LE scanning are scheduled by the controller. | |
4674 | * | |
4675 | * For interleaving discovery in comparison, BR/EDR inquiry | |
4676 | * and LE scanning are done sequentially with separate | |
4677 | * timeouts. | |
4678 | */ | |
4679 | if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, | |
4680 | &hdev->quirks)) { | |
4681 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); | |
4682 | /* During simultaneous discovery, we double LE scan | |
4683 | * interval. We must leave some time for the controller | |
4684 | * to do BR/EDR inquiry. | |
4685 | */ | |
4686 | err = hci_start_interleaved_discovery_sync(hdev); | |
4687 | break; | |
4688 | } | |
4689 | ||
4690 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); | |
4691 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); | |
4692 | break; | |
4693 | case DISCOV_TYPE_LE: | |
4694 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); | |
4695 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); | |
4696 | break; | |
4697 | default: | |
4698 | return -EINVAL; | |
4699 | } | |
4700 | ||
4701 | if (err) | |
4702 | return err; | |
4703 | ||
4704 | bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout)); | |
4705 | ||
4706 | /* When service discovery is used and the controller has a | |
4707 | * strict duplicate filter, it is important to remember the | |
4708 | * start and duration of the scan. This is required for | |
4709 | * restarting scanning during the discovery phase. | |
4710 | */ | |
4711 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) && | |
4712 | hdev->discovery.result_filtering) { | |
4713 | hdev->discovery.scan_start = jiffies; | |
4714 | hdev->discovery.scan_duration = timeout; | |
4715 | } | |
4716 | ||
4717 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable, | |
4718 | timeout); | |
abfeea47 LAD |
4719 | return 0; |
4720 | } | |
182ee45d LAD |
4721 | |
4722 | static void hci_suspend_monitor_sync(struct hci_dev *hdev) | |
4723 | { | |
4724 | switch (hci_get_adv_monitor_offload_ext(hdev)) { | |
4725 | case HCI_ADV_MONITOR_EXT_MSFT: | |
4726 | msft_suspend_sync(hdev); | |
4727 | break; | |
4728 | default: | |
4729 | return; | |
4730 | } | |
4731 | } | |
4732 | ||
4733 | /* This function disables discovery and mark it as paused */ | |
4734 | static int hci_pause_discovery_sync(struct hci_dev *hdev) | |
4735 | { | |
4736 | int old_state = hdev->discovery.state; | |
4737 | int err; | |
4738 | ||
4739 | /* If discovery already stopped/stopping/paused there nothing to do */ | |
4740 | if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING || | |
4741 | hdev->discovery_paused) | |
4742 | return 0; | |
4743 | ||
4744 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); | |
4745 | err = hci_stop_discovery_sync(hdev); | |
4746 | if (err) | |
4747 | return err; | |
4748 | ||
4749 | hdev->discovery_paused = true; | |
4750 | hdev->discovery_old_state = old_state; | |
4751 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); | |
4752 | ||
4753 | return 0; | |
4754 | } | |
4755 | ||
4756 | static int hci_update_event_filter_sync(struct hci_dev *hdev) | |
4757 | { | |
4758 | struct bdaddr_list_with_flags *b; | |
4759 | u8 scan = SCAN_DISABLED; | |
4760 | bool scanning = test_bit(HCI_PSCAN, &hdev->flags); | |
4761 | int err; | |
4762 | ||
4763 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
4764 | return 0; | |
4765 | ||
4766 | /* Always clear event filter when starting */ | |
4767 | hci_clear_event_filter_sync(hdev); | |
4768 | ||
4769 | list_for_each_entry(b, &hdev->accept_list, list) { | |
fe92ee64 | 4770 | if (!test_bit(HCI_CONN_FLAG_REMOTE_WAKEUP, b->flags)) |
182ee45d LAD |
4771 | continue; |
4772 | ||
4773 | bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr); | |
4774 | ||
4775 | err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP, | |
4776 | HCI_CONN_SETUP_ALLOW_BDADDR, | |
4777 | &b->bdaddr, | |
4778 | HCI_CONN_SETUP_AUTO_ON); | |
4779 | if (err) | |
4780 | bt_dev_dbg(hdev, "Failed to set event filter for %pMR", | |
4781 | &b->bdaddr); | |
4782 | else | |
4783 | scan = SCAN_PAGE; | |
4784 | } | |
4785 | ||
4786 | if (scan && !scanning) | |
4787 | hci_write_scan_enable_sync(hdev, scan); | |
4788 | else if (!scan && scanning) | |
4789 | hci_write_scan_enable_sync(hdev, scan); | |
4790 | ||
4791 | return 0; | |
4792 | } | |
4793 | ||
4794 | /* This function performs the HCI suspend procedures in the follow order: | |
4795 | * | |
4796 | * Pause discovery (active scanning/inquiry) | |
4797 | * Pause Directed Advertising/Advertising | |
4798 | * Disconnect all connections | |
4799 | * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup | |
4800 | * otherwise: | |
4801 | * Update event mask (only set events that are allowed to wake up the host) | |
4802 | * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP) | |
4803 | * Update passive scanning (lower duty cycle) | |
4804 | * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE | |
4805 | */ | |
4806 | int hci_suspend_sync(struct hci_dev *hdev) | |
4807 | { | |
4808 | int err; | |
4809 | ||
4810 | /* If marked as suspended there nothing to do */ | |
4811 | if (hdev->suspended) | |
4812 | return 0; | |
4813 | ||
4814 | /* Mark device as suspended */ | |
4815 | hdev->suspended = true; | |
4816 | ||
4817 | /* Pause discovery if not already stopped */ | |
4818 | hci_pause_discovery_sync(hdev); | |
4819 | ||
4820 | /* Pause other advertisements */ | |
4821 | hci_pause_advertising_sync(hdev); | |
4822 | ||
4823 | /* Disable page scan if enabled */ | |
4824 | if (test_bit(HCI_PSCAN, &hdev->flags)) | |
4825 | hci_write_scan_enable_sync(hdev, SCAN_DISABLED); | |
4826 | ||
4827 | /* Suspend monitor filters */ | |
4828 | hci_suspend_monitor_sync(hdev); | |
4829 | ||
4830 | /* Prevent disconnects from causing scanning to be re-enabled */ | |
4831 | hdev->scanning_paused = true; | |
4832 | ||
4833 | /* Soft disconnect everything (power off) */ | |
4834 | err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); | |
4835 | if (err) { | |
4836 | /* Set state to BT_RUNNING so resume doesn't notify */ | |
4837 | hdev->suspend_state = BT_RUNNING; | |
4838 | hci_resume_sync(hdev); | |
4839 | return err; | |
4840 | } | |
4841 | ||
4842 | /* Only configure accept list if disconnect succeeded and wake | |
4843 | * isn't being prevented. | |
4844 | */ | |
4845 | if (!hdev->wakeup || !hdev->wakeup(hdev)) { | |
4846 | hdev->suspend_state = BT_SUSPEND_DISCONNECT; | |
4847 | return 0; | |
4848 | } | |
4849 | ||
4850 | /* Unpause to take care of updating scanning params */ | |
4851 | hdev->scanning_paused = false; | |
4852 | ||
4853 | /* Update event mask so only the allowed event can wakeup the host */ | |
4854 | hci_set_event_mask_sync(hdev); | |
4855 | ||
4856 | /* Enable event filter for paired devices */ | |
4857 | hci_update_event_filter_sync(hdev); | |
4858 | ||
4859 | /* Update LE passive scan if enabled */ | |
4860 | hci_update_passive_scan_sync(hdev); | |
4861 | ||
4862 | /* Pause scan changes again. */ | |
4863 | hdev->scanning_paused = true; | |
4864 | ||
4865 | hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE; | |
4866 | ||
4867 | return 0; | |
4868 | } | |
4869 | ||
4870 | /* This function resumes discovery */ | |
4871 | static int hci_resume_discovery_sync(struct hci_dev *hdev) | |
4872 | { | |
4873 | int err; | |
4874 | ||
4875 | /* If discovery not paused there nothing to do */ | |
4876 | if (!hdev->discovery_paused) | |
4877 | return 0; | |
4878 | ||
4879 | hdev->discovery_paused = false; | |
4880 | ||
4881 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); | |
4882 | ||
4883 | err = hci_start_discovery_sync(hdev); | |
4884 | ||
4885 | hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED : | |
4886 | DISCOVERY_FINDING); | |
4887 | ||
4888 | return err; | |
4889 | } | |
4890 | ||
4891 | static void hci_resume_monitor_sync(struct hci_dev *hdev) | |
4892 | { | |
4893 | switch (hci_get_adv_monitor_offload_ext(hdev)) { | |
4894 | case HCI_ADV_MONITOR_EXT_MSFT: | |
4895 | msft_resume_sync(hdev); | |
4896 | break; | |
4897 | default: | |
4898 | return; | |
4899 | } | |
4900 | } | |
4901 | ||
4902 | /* This function performs the HCI suspend procedures in the follow order: | |
4903 | * | |
4904 | * Restore event mask | |
4905 | * Clear event filter | |
4906 | * Update passive scanning (normal duty cycle) | |
4907 | * Resume Directed Advertising/Advertising | |
4908 | * Resume discovery (active scanning/inquiry) | |
4909 | */ | |
4910 | int hci_resume_sync(struct hci_dev *hdev) | |
4911 | { | |
4912 | /* If not marked as suspended there nothing to do */ | |
4913 | if (!hdev->suspended) | |
4914 | return 0; | |
4915 | ||
4916 | hdev->suspended = false; | |
4917 | hdev->scanning_paused = false; | |
4918 | ||
4919 | /* Restore event mask */ | |
4920 | hci_set_event_mask_sync(hdev); | |
4921 | ||
4922 | /* Clear any event filters and restore scan state */ | |
4923 | hci_clear_event_filter_sync(hdev); | |
4924 | hci_update_scan_sync(hdev); | |
4925 | ||
4926 | /* Reset passive scanning to normal */ | |
4927 | hci_update_passive_scan_sync(hdev); | |
4928 | ||
4929 | /* Resume monitor filters */ | |
4930 | hci_resume_monitor_sync(hdev); | |
4931 | ||
4932 | /* Resume other advertisements */ | |
4933 | hci_resume_advertising_sync(hdev); | |
4934 | ||
4935 | /* Resume discovery */ | |
4936 | hci_resume_discovery_sync(hdev); | |
4937 | ||
4938 | return 0; | |
4939 | } |