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 | ||
8 | #include <net/bluetooth/bluetooth.h> | |
9 | #include <net/bluetooth/hci_core.h> | |
10 | #include <net/bluetooth/mgmt.h> | |
11 | ||
12 | #include "hci_request.h" | |
13 | #include "smp.h" | |
161510cc | 14 | #include "eir.h" |
6a98e383 MH |
15 | |
16 | static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode, | |
17 | struct sk_buff *skb) | |
18 | { | |
19 | bt_dev_dbg(hdev, "result 0x%2.2x", result); | |
20 | ||
21 | if (hdev->req_status != HCI_REQ_PEND) | |
22 | return; | |
23 | ||
24 | hdev->req_result = result; | |
25 | hdev->req_status = HCI_REQ_DONE; | |
26 | ||
cba6b758 LAD |
27 | if (skb) { |
28 | struct sock *sk = hci_skb_sk(skb); | |
29 | ||
30 | /* Drop sk reference if set */ | |
31 | if (sk) | |
32 | sock_put(sk); | |
33 | ||
34 | hdev->req_skb = skb_get(skb); | |
35 | } | |
36 | ||
6a98e383 MH |
37 | wake_up_interruptible(&hdev->req_wait_q); |
38 | } | |
39 | ||
40 | static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, | |
41 | u32 plen, const void *param, | |
42 | struct sock *sk) | |
43 | { | |
44 | int len = HCI_COMMAND_HDR_SIZE + plen; | |
45 | struct hci_command_hdr *hdr; | |
46 | struct sk_buff *skb; | |
47 | ||
48 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
49 | if (!skb) | |
50 | return NULL; | |
51 | ||
52 | hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE); | |
53 | hdr->opcode = cpu_to_le16(opcode); | |
54 | hdr->plen = plen; | |
55 | ||
56 | if (plen) | |
57 | skb_put_data(skb, param, plen); | |
58 | ||
59 | bt_dev_dbg(hdev, "skb len %d", skb->len); | |
60 | ||
61 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; | |
62 | hci_skb_opcode(skb) = opcode; | |
63 | ||
cba6b758 LAD |
64 | /* Grab a reference if command needs to be associated with a sock (e.g. |
65 | * likely mgmt socket that initiated the command). | |
66 | */ | |
67 | if (sk) { | |
68 | hci_skb_sk(skb) = sk; | |
69 | sock_hold(sk); | |
70 | } | |
71 | ||
6a98e383 MH |
72 | return skb; |
73 | } | |
74 | ||
75 | static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen, | |
76 | const void *param, u8 event, struct sock *sk) | |
77 | { | |
78 | struct hci_dev *hdev = req->hdev; | |
79 | struct sk_buff *skb; | |
80 | ||
81 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); | |
82 | ||
83 | /* If an error occurred during request building, there is no point in | |
84 | * queueing the HCI command. We can simply return. | |
85 | */ | |
86 | if (req->err) | |
87 | return; | |
88 | ||
89 | skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk); | |
90 | if (!skb) { | |
91 | bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)", | |
92 | opcode); | |
93 | req->err = -ENOMEM; | |
94 | return; | |
95 | } | |
96 | ||
97 | if (skb_queue_empty(&req->cmd_q)) | |
98 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; | |
99 | ||
100 | bt_cb(skb)->hci.req_event = event; | |
101 | ||
102 | skb_queue_tail(&req->cmd_q, skb); | |
103 | } | |
104 | ||
105 | static int hci_cmd_sync_run(struct hci_request *req) | |
106 | { | |
107 | struct hci_dev *hdev = req->hdev; | |
108 | struct sk_buff *skb; | |
109 | unsigned long flags; | |
110 | ||
111 | bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q)); | |
112 | ||
113 | /* If an error occurred during request building, remove all HCI | |
114 | * commands queued on the HCI request queue. | |
115 | */ | |
116 | if (req->err) { | |
117 | skb_queue_purge(&req->cmd_q); | |
118 | return req->err; | |
119 | } | |
120 | ||
121 | /* Do not allow empty requests */ | |
122 | if (skb_queue_empty(&req->cmd_q)) | |
123 | return -ENODATA; | |
124 | ||
125 | skb = skb_peek_tail(&req->cmd_q); | |
126 | bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete; | |
127 | bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB; | |
128 | ||
129 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); | |
130 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); | |
131 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); | |
132 | ||
133 | queue_work(hdev->workqueue, &hdev->cmd_work); | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | /* This function requires the caller holds hdev->req_lock. */ | |
139 | struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen, | |
140 | const void *param, u8 event, u32 timeout, | |
141 | struct sock *sk) | |
142 | { | |
143 | struct hci_request req; | |
144 | struct sk_buff *skb; | |
145 | int err = 0; | |
146 | ||
147 | bt_dev_dbg(hdev, ""); | |
148 | ||
149 | hci_req_init(&req, hdev); | |
150 | ||
151 | hci_cmd_sync_add(&req, opcode, plen, param, event, sk); | |
152 | ||
153 | hdev->req_status = HCI_REQ_PEND; | |
154 | ||
155 | err = hci_cmd_sync_run(&req); | |
156 | if (err < 0) | |
157 | return ERR_PTR(err); | |
158 | ||
159 | err = wait_event_interruptible_timeout(hdev->req_wait_q, | |
160 | hdev->req_status != HCI_REQ_PEND, | |
161 | timeout); | |
162 | ||
163 | if (err == -ERESTARTSYS) | |
164 | return ERR_PTR(-EINTR); | |
165 | ||
166 | switch (hdev->req_status) { | |
167 | case HCI_REQ_DONE: | |
168 | err = -bt_to_errno(hdev->req_result); | |
169 | break; | |
170 | ||
171 | case HCI_REQ_CANCELED: | |
172 | err = -hdev->req_result; | |
173 | break; | |
174 | ||
175 | default: | |
176 | err = -ETIMEDOUT; | |
177 | break; | |
178 | } | |
179 | ||
180 | hdev->req_status = 0; | |
181 | hdev->req_result = 0; | |
182 | skb = hdev->req_skb; | |
183 | hdev->req_skb = NULL; | |
184 | ||
185 | bt_dev_dbg(hdev, "end: err %d", err); | |
186 | ||
187 | if (err < 0) { | |
188 | kfree_skb(skb); | |
189 | return ERR_PTR(err); | |
190 | } | |
191 | ||
6a98e383 MH |
192 | return skb; |
193 | } | |
194 | EXPORT_SYMBOL(__hci_cmd_sync_sk); | |
195 | ||
196 | /* This function requires the caller holds hdev->req_lock. */ | |
197 | struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, | |
198 | const void *param, u32 timeout) | |
199 | { | |
200 | return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL); | |
201 | } | |
202 | EXPORT_SYMBOL(__hci_cmd_sync); | |
203 | ||
204 | /* Send HCI command and wait for command complete event */ | |
205 | struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, | |
206 | const void *param, u32 timeout) | |
207 | { | |
208 | struct sk_buff *skb; | |
209 | ||
210 | if (!test_bit(HCI_UP, &hdev->flags)) | |
211 | return ERR_PTR(-ENETDOWN); | |
212 | ||
213 | bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen); | |
214 | ||
215 | hci_req_sync_lock(hdev); | |
216 | skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout); | |
217 | hci_req_sync_unlock(hdev); | |
218 | ||
219 | return skb; | |
220 | } | |
221 | EXPORT_SYMBOL(hci_cmd_sync); | |
222 | ||
223 | /* This function requires the caller holds hdev->req_lock. */ | |
224 | struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, | |
225 | const void *param, u8 event, u32 timeout) | |
226 | { | |
227 | return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, | |
228 | NULL); | |
229 | } | |
230 | EXPORT_SYMBOL(__hci_cmd_sync_ev); | |
231 | ||
232 | /* This function requires the caller holds hdev->req_lock. */ | |
233 | int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen, | |
234 | const void *param, u8 event, u32 timeout, | |
235 | struct sock *sk) | |
236 | { | |
237 | struct sk_buff *skb; | |
238 | u8 status; | |
239 | ||
240 | skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk); | |
abfeea47 | 241 | if (IS_ERR(skb)) { |
6a98e383 MH |
242 | bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode, |
243 | PTR_ERR(skb)); | |
244 | return PTR_ERR(skb); | |
245 | } | |
246 | ||
abfeea47 LAD |
247 | /* If command return a status event skb will be set to NULL as there are |
248 | * no parameters, in case of failure IS_ERR(skb) would have be set to | |
249 | * the actual error would be found with PTR_ERR(skb). | |
250 | */ | |
251 | if (!skb) | |
252 | return 0; | |
253 | ||
6a98e383 MH |
254 | status = skb->data[0]; |
255 | ||
256 | kfree_skb(skb); | |
257 | ||
258 | return status; | |
259 | } | |
260 | EXPORT_SYMBOL(__hci_cmd_sync_status_sk); | |
261 | ||
262 | int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen, | |
263 | const void *param, u32 timeout) | |
264 | { | |
265 | return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout, | |
266 | NULL); | |
267 | } | |
268 | EXPORT_SYMBOL(__hci_cmd_sync_status); | |
269 | ||
270 | static void hci_cmd_sync_work(struct work_struct *work) | |
271 | { | |
272 | struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work); | |
273 | struct hci_cmd_sync_work_entry *entry; | |
274 | hci_cmd_sync_work_func_t func; | |
275 | hci_cmd_sync_work_destroy_t destroy; | |
276 | void *data; | |
277 | ||
278 | bt_dev_dbg(hdev, ""); | |
279 | ||
280 | mutex_lock(&hdev->cmd_sync_work_lock); | |
281 | entry = list_first_entry(&hdev->cmd_sync_work_list, | |
282 | struct hci_cmd_sync_work_entry, list); | |
283 | if (entry) { | |
284 | list_del(&entry->list); | |
285 | func = entry->func; | |
286 | data = entry->data; | |
287 | destroy = entry->destroy; | |
288 | kfree(entry); | |
289 | } else { | |
290 | func = NULL; | |
291 | data = NULL; | |
292 | destroy = NULL; | |
293 | } | |
294 | mutex_unlock(&hdev->cmd_sync_work_lock); | |
295 | ||
296 | if (func) { | |
297 | int err; | |
298 | ||
299 | hci_req_sync_lock(hdev); | |
300 | ||
301 | err = func(hdev, data); | |
302 | ||
303 | if (destroy) | |
304 | destroy(hdev, data, err); | |
305 | ||
306 | hci_req_sync_unlock(hdev); | |
307 | } | |
308 | } | |
309 | ||
310 | void hci_cmd_sync_init(struct hci_dev *hdev) | |
311 | { | |
312 | INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work); | |
313 | INIT_LIST_HEAD(&hdev->cmd_sync_work_list); | |
314 | mutex_init(&hdev->cmd_sync_work_lock); | |
315 | } | |
316 | ||
317 | void hci_cmd_sync_clear(struct hci_dev *hdev) | |
318 | { | |
319 | struct hci_cmd_sync_work_entry *entry, *tmp; | |
320 | ||
321 | cancel_work_sync(&hdev->cmd_sync_work); | |
322 | ||
323 | list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) { | |
324 | if (entry->destroy) | |
325 | entry->destroy(hdev, entry->data, -ECANCELED); | |
326 | ||
327 | list_del(&entry->list); | |
328 | kfree(entry); | |
329 | } | |
330 | } | |
331 | ||
332 | int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, | |
333 | void *data, hci_cmd_sync_work_destroy_t destroy) | |
334 | { | |
335 | struct hci_cmd_sync_work_entry *entry; | |
336 | ||
337 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
338 | if (!entry) | |
339 | return -ENOMEM; | |
340 | ||
341 | entry->func = func; | |
342 | entry->data = data; | |
343 | entry->destroy = destroy; | |
344 | ||
345 | mutex_lock(&hdev->cmd_sync_work_lock); | |
346 | list_add_tail(&entry->list, &hdev->cmd_sync_work_list); | |
347 | mutex_unlock(&hdev->cmd_sync_work_lock); | |
348 | ||
349 | queue_work(hdev->req_workqueue, &hdev->cmd_sync_work); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | EXPORT_SYMBOL(hci_cmd_sync_queue); | |
161510cc LAD |
354 | |
355 | int hci_update_eir_sync(struct hci_dev *hdev) | |
356 | { | |
357 | struct hci_cp_write_eir cp; | |
358 | ||
359 | bt_dev_dbg(hdev, ""); | |
360 | ||
361 | if (!hdev_is_powered(hdev)) | |
362 | return 0; | |
363 | ||
364 | if (!lmp_ext_inq_capable(hdev)) | |
365 | return 0; | |
366 | ||
367 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) | |
368 | return 0; | |
369 | ||
370 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) | |
371 | return 0; | |
372 | ||
373 | memset(&cp, 0, sizeof(cp)); | |
374 | ||
375 | eir_create(hdev, cp.data); | |
376 | ||
377 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) | |
378 | return 0; | |
379 | ||
380 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); | |
381 | ||
382 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, | |
383 | HCI_CMD_TIMEOUT); | |
384 | } | |
385 | ||
386 | static u8 get_service_classes(struct hci_dev *hdev) | |
387 | { | |
388 | struct bt_uuid *uuid; | |
389 | u8 val = 0; | |
390 | ||
391 | list_for_each_entry(uuid, &hdev->uuids, list) | |
392 | val |= uuid->svc_hint; | |
393 | ||
394 | return val; | |
395 | } | |
396 | ||
397 | int hci_update_class_sync(struct hci_dev *hdev) | |
398 | { | |
399 | u8 cod[3]; | |
400 | ||
401 | bt_dev_dbg(hdev, ""); | |
402 | ||
403 | if (!hdev_is_powered(hdev)) | |
404 | return 0; | |
405 | ||
406 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
407 | return 0; | |
408 | ||
409 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) | |
410 | return 0; | |
411 | ||
412 | cod[0] = hdev->minor_class; | |
413 | cod[1] = hdev->major_class; | |
414 | cod[2] = get_service_classes(hdev); | |
415 | ||
416 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) | |
417 | cod[1] |= 0x20; | |
418 | ||
419 | if (memcmp(cod, hdev->dev_class, 3) == 0) | |
420 | return 0; | |
421 | ||
422 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV, | |
423 | sizeof(cod), cod, HCI_CMD_TIMEOUT); | |
424 | } | |
cba6b758 LAD |
425 | |
426 | static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable) | |
427 | { | |
428 | /* If there is no connection we are OK to advertise. */ | |
429 | if (hci_conn_num(hdev, LE_LINK) == 0) | |
430 | return true; | |
431 | ||
432 | /* Check le_states if there is any connection in peripheral role. */ | |
433 | if (hdev->conn_hash.le_num_peripheral > 0) { | |
434 | /* Peripheral connection state and non connectable mode | |
435 | * bit 20. | |
436 | */ | |
437 | if (!connectable && !(hdev->le_states[2] & 0x10)) | |
438 | return false; | |
439 | ||
440 | /* Peripheral connection state and connectable mode bit 38 | |
441 | * and scannable bit 21. | |
442 | */ | |
443 | if (connectable && (!(hdev->le_states[4] & 0x40) || | |
444 | !(hdev->le_states[2] & 0x20))) | |
445 | return false; | |
446 | } | |
447 | ||
448 | /* Check le_states if there is any connection in central role. */ | |
449 | if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) { | |
450 | /* Central connection state and non connectable mode bit 18. */ | |
451 | if (!connectable && !(hdev->le_states[2] & 0x02)) | |
452 | return false; | |
453 | ||
454 | /* Central connection state and connectable mode bit 35 and | |
455 | * scannable 19. | |
456 | */ | |
457 | if (connectable && (!(hdev->le_states[4] & 0x08) || | |
458 | !(hdev->le_states[2] & 0x08))) | |
459 | return false; | |
460 | } | |
461 | ||
462 | return true; | |
463 | } | |
464 | ||
465 | static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags) | |
466 | { | |
467 | /* If privacy is not enabled don't use RPA */ | |
468 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) | |
469 | return false; | |
470 | ||
471 | /* If basic privacy mode is enabled use RPA */ | |
472 | if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) | |
473 | return true; | |
474 | ||
475 | /* If limited privacy mode is enabled don't use RPA if we're | |
476 | * both discoverable and bondable. | |
477 | */ | |
478 | if ((flags & MGMT_ADV_FLAG_DISCOV) && | |
479 | hci_dev_test_flag(hdev, HCI_BONDABLE)) | |
480 | return false; | |
481 | ||
482 | /* We're neither bondable nor discoverable in the limited | |
483 | * privacy mode, therefore use RPA. | |
484 | */ | |
485 | return true; | |
486 | } | |
487 | ||
488 | static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa) | |
489 | { | |
490 | /* If we're advertising or initiating an LE connection we can't | |
491 | * go ahead and change the random address at this time. This is | |
492 | * because the eventual initiator address used for the | |
493 | * subsequently created connection will be undefined (some | |
494 | * controllers use the new address and others the one we had | |
495 | * when the operation started). | |
496 | * | |
497 | * In this kind of scenario skip the update and let the random | |
498 | * address be updated at the next cycle. | |
499 | */ | |
500 | if (hci_dev_test_flag(hdev, HCI_LE_ADV) || | |
501 | hci_lookup_le_connect(hdev)) { | |
502 | bt_dev_dbg(hdev, "Deferring random address update"); | |
503 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); | |
504 | return 0; | |
505 | } | |
506 | ||
507 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR, | |
508 | 6, rpa, HCI_CMD_TIMEOUT); | |
509 | } | |
510 | ||
511 | int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy, | |
512 | bool rpa, u8 *own_addr_type) | |
513 | { | |
514 | int err; | |
515 | ||
516 | /* If privacy is enabled use a resolvable private address. If | |
517 | * current RPA has expired or there is something else than | |
518 | * the current RPA in use, then generate a new one. | |
519 | */ | |
520 | if (rpa) { | |
521 | /* If Controller supports LL Privacy use own address type is | |
522 | * 0x03 | |
523 | */ | |
ad383c2c | 524 | if (use_ll_privacy(hdev)) |
cba6b758 LAD |
525 | *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; |
526 | else | |
527 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
528 | ||
529 | /* Check if RPA is valid */ | |
530 | if (rpa_valid(hdev)) | |
531 | return 0; | |
532 | ||
533 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); | |
534 | if (err < 0) { | |
535 | bt_dev_err(hdev, "failed to generate new RPA"); | |
536 | return err; | |
537 | } | |
538 | ||
539 | err = hci_set_random_addr_sync(hdev, &hdev->rpa); | |
540 | if (err) | |
541 | return err; | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
546 | /* In case of required privacy without resolvable private address, | |
547 | * use an non-resolvable private address. This is useful for active | |
548 | * scanning and non-connectable advertising. | |
549 | */ | |
550 | if (require_privacy) { | |
551 | bdaddr_t nrpa; | |
552 | ||
553 | while (true) { | |
554 | /* The non-resolvable private address is generated | |
555 | * from random six bytes with the two most significant | |
556 | * bits cleared. | |
557 | */ | |
558 | get_random_bytes(&nrpa, 6); | |
559 | nrpa.b[5] &= 0x3f; | |
560 | ||
561 | /* The non-resolvable private address shall not be | |
562 | * equal to the public address. | |
563 | */ | |
564 | if (bacmp(&hdev->bdaddr, &nrpa)) | |
565 | break; | |
566 | } | |
567 | ||
568 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
569 | ||
570 | return hci_set_random_addr_sync(hdev, &nrpa); | |
571 | } | |
572 | ||
573 | /* If forcing static address is in use or there is no public | |
574 | * address use the static address as random address (but skip | |
575 | * the HCI command if the current random address is already the | |
576 | * static one. | |
577 | * | |
578 | * In case BR/EDR has been disabled on a dual-mode controller | |
579 | * and a static address has been configured, then use that | |
580 | * address instead of the public BR/EDR address. | |
581 | */ | |
582 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || | |
583 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || | |
584 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && | |
585 | bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
586 | *own_addr_type = ADDR_LE_DEV_RANDOM; | |
587 | if (bacmp(&hdev->static_addr, &hdev->random_addr)) | |
588 | return hci_set_random_addr_sync(hdev, | |
589 | &hdev->static_addr); | |
590 | return 0; | |
591 | } | |
592 | ||
593 | /* Neither privacy nor static address is being used so use a | |
594 | * public address. | |
595 | */ | |
596 | *own_addr_type = ADDR_LE_DEV_PUBLIC; | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) | |
602 | { | |
603 | struct hci_cp_le_set_ext_adv_enable *cp; | |
604 | struct hci_cp_ext_adv_set *set; | |
605 | u8 data[sizeof(*cp) + sizeof(*set) * 1]; | |
606 | u8 size; | |
607 | ||
608 | /* If request specifies an instance that doesn't exist, fail */ | |
609 | if (instance > 0) { | |
610 | struct adv_info *adv; | |
611 | ||
612 | adv = hci_find_adv_instance(hdev, instance); | |
613 | if (!adv) | |
614 | return -EINVAL; | |
615 | ||
616 | /* If not enabled there is nothing to do */ | |
617 | if (!adv->enabled) | |
618 | return 0; | |
619 | } | |
620 | ||
621 | memset(data, 0, sizeof(data)); | |
622 | ||
623 | cp = (void *)data; | |
624 | set = (void *)cp->data; | |
625 | ||
626 | /* Instance 0x00 indicates all advertising instances will be disabled */ | |
627 | cp->num_of_sets = !!instance; | |
628 | cp->enable = 0x00; | |
629 | ||
630 | set->handle = instance; | |
631 | ||
632 | size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets; | |
633 | ||
634 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, | |
635 | size, data, HCI_CMD_TIMEOUT); | |
636 | } | |
637 | ||
638 | static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, | |
639 | bdaddr_t *random_addr) | |
640 | { | |
641 | struct hci_cp_le_set_adv_set_rand_addr cp; | |
642 | int err; | |
643 | ||
644 | if (!instance) { | |
645 | /* Instance 0x00 doesn't have an adv_info, instead it uses | |
646 | * hdev->random_addr to track its address so whenever it needs | |
647 | * to be updated this also set the random address since | |
648 | * hdev->random_addr is shared with scan state machine. | |
649 | */ | |
650 | err = hci_set_random_addr_sync(hdev, random_addr); | |
651 | if (err) | |
652 | return err; | |
653 | } | |
654 | ||
655 | memset(&cp, 0, sizeof(cp)); | |
656 | ||
657 | cp.handle = instance; | |
658 | bacpy(&cp.bdaddr, random_addr); | |
659 | ||
660 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, | |
661 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
662 | } | |
663 | ||
664 | int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) | |
665 | { | |
666 | struct hci_cp_le_set_ext_adv_params cp; | |
667 | bool connectable; | |
668 | u32 flags; | |
669 | bdaddr_t random_addr; | |
670 | u8 own_addr_type; | |
671 | int err; | |
672 | struct adv_info *adv; | |
673 | bool secondary_adv; | |
674 | ||
675 | if (instance > 0) { | |
676 | adv = hci_find_adv_instance(hdev, instance); | |
677 | if (!adv) | |
678 | return -EINVAL; | |
679 | } else { | |
680 | adv = NULL; | |
681 | } | |
682 | ||
683 | /* Updating parameters of an active instance will return a | |
684 | * Command Disallowed error, so we must first disable the | |
685 | * instance if it is active. | |
686 | */ | |
687 | if (adv && !adv->pending) { | |
688 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | |
689 | if (err) | |
690 | return err; | |
691 | } | |
692 | ||
693 | flags = hci_adv_instance_flags(hdev, instance); | |
694 | ||
695 | /* If the "connectable" instance flag was not set, then choose between | |
696 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. | |
697 | */ | |
698 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | |
699 | mgmt_get_connectable(hdev); | |
700 | ||
701 | if (!is_advertising_allowed(hdev, connectable)) | |
702 | return -EPERM; | |
703 | ||
704 | /* Set require_privacy to true only when non-connectable | |
705 | * advertising is used. In that case it is fine to use a | |
706 | * non-resolvable private address. | |
707 | */ | |
708 | err = hci_get_random_address(hdev, !connectable, | |
709 | adv_use_rpa(hdev, flags), adv, | |
710 | &own_addr_type, &random_addr); | |
711 | if (err < 0) | |
712 | return err; | |
713 | ||
714 | memset(&cp, 0, sizeof(cp)); | |
715 | ||
716 | if (adv) { | |
717 | hci_cpu_to_le24(adv->min_interval, cp.min_interval); | |
718 | hci_cpu_to_le24(adv->max_interval, cp.max_interval); | |
719 | cp.tx_power = adv->tx_power; | |
720 | } else { | |
721 | hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval); | |
722 | hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval); | |
723 | cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; | |
724 | } | |
725 | ||
726 | secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK); | |
727 | ||
728 | if (connectable) { | |
729 | if (secondary_adv) | |
730 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND); | |
731 | else | |
732 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND); | |
733 | } else if (hci_adv_instance_is_scannable(hdev, instance) || | |
734 | (flags & MGMT_ADV_PARAM_SCAN_RSP)) { | |
735 | if (secondary_adv) | |
736 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND); | |
737 | else | |
738 | cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND); | |
739 | } else { | |
740 | if (secondary_adv) | |
741 | cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND); | |
742 | else | |
743 | cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND); | |
744 | } | |
745 | ||
cf75ad8b LAD |
746 | /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter |
747 | * contains the peer’s Identity Address and the Peer_Address_Type | |
748 | * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01). | |
749 | * These parameters are used to locate the corresponding local IRK in | |
750 | * the resolving list; this IRK is used to generate their own address | |
751 | * used in the advertisement. | |
752 | */ | |
753 | if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) | |
754 | hci_copy_identity_address(hdev, &cp.peer_addr, | |
755 | &cp.peer_addr_type); | |
756 | ||
cba6b758 LAD |
757 | cp.own_addr_type = own_addr_type; |
758 | cp.channel_map = hdev->le_adv_channel_map; | |
759 | cp.handle = instance; | |
760 | ||
761 | if (flags & MGMT_ADV_FLAG_SEC_2M) { | |
762 | cp.primary_phy = HCI_ADV_PHY_1M; | |
763 | cp.secondary_phy = HCI_ADV_PHY_2M; | |
764 | } else if (flags & MGMT_ADV_FLAG_SEC_CODED) { | |
765 | cp.primary_phy = HCI_ADV_PHY_CODED; | |
766 | cp.secondary_phy = HCI_ADV_PHY_CODED; | |
767 | } else { | |
768 | /* In all other cases use 1M */ | |
769 | cp.primary_phy = HCI_ADV_PHY_1M; | |
770 | cp.secondary_phy = HCI_ADV_PHY_1M; | |
771 | } | |
772 | ||
773 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, | |
774 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
775 | if (err) | |
776 | return err; | |
777 | ||
778 | if ((own_addr_type == ADDR_LE_DEV_RANDOM || | |
779 | own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && | |
780 | bacmp(&random_addr, BDADDR_ANY)) { | |
781 | /* Check if random address need to be updated */ | |
782 | if (adv) { | |
783 | if (!bacmp(&random_addr, &adv->random_addr)) | |
784 | return 0; | |
785 | } else { | |
786 | if (!bacmp(&random_addr, &hdev->random_addr)) | |
787 | return 0; | |
788 | } | |
789 | ||
790 | return hci_set_adv_set_random_addr_sync(hdev, instance, | |
791 | &random_addr); | |
792 | } | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
797 | static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
798 | { | |
799 | struct { | |
800 | struct hci_cp_le_set_ext_scan_rsp_data cp; | |
801 | u8 data[HCI_MAX_EXT_AD_LENGTH]; | |
802 | } pdu; | |
803 | u8 len; | |
804 | ||
805 | memset(&pdu, 0, sizeof(pdu)); | |
806 | ||
807 | len = eir_create_scan_rsp(hdev, instance, pdu.data); | |
808 | ||
809 | if (hdev->scan_rsp_data_len == len && | |
810 | !memcmp(pdu.data, hdev->scan_rsp_data, len)) | |
811 | return 0; | |
812 | ||
813 | memcpy(hdev->scan_rsp_data, pdu.data, len); | |
814 | hdev->scan_rsp_data_len = len; | |
815 | ||
816 | pdu.cp.handle = instance; | |
817 | pdu.cp.length = len; | |
818 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
819 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; | |
820 | ||
821 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, | |
822 | sizeof(pdu.cp) + len, &pdu.cp, | |
823 | HCI_CMD_TIMEOUT); | |
824 | } | |
825 | ||
826 | static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
827 | { | |
828 | struct hci_cp_le_set_scan_rsp_data cp; | |
829 | u8 len; | |
830 | ||
831 | memset(&cp, 0, sizeof(cp)); | |
832 | ||
833 | len = eir_create_scan_rsp(hdev, instance, cp.data); | |
834 | ||
835 | if (hdev->scan_rsp_data_len == len && | |
836 | !memcmp(cp.data, hdev->scan_rsp_data, len)) | |
837 | return 0; | |
838 | ||
839 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); | |
840 | hdev->scan_rsp_data_len = len; | |
841 | ||
842 | cp.length = len; | |
843 | ||
844 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA, | |
845 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
846 | } | |
847 | ||
848 | int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) | |
849 | { | |
850 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
851 | return 0; | |
852 | ||
853 | if (ext_adv_capable(hdev)) | |
854 | return hci_set_ext_scan_rsp_data_sync(hdev, instance); | |
855 | ||
856 | return __hci_set_scan_rsp_data_sync(hdev, instance); | |
857 | } | |
858 | ||
859 | int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance) | |
860 | { | |
861 | struct hci_cp_le_set_ext_adv_enable *cp; | |
862 | struct hci_cp_ext_adv_set *set; | |
863 | u8 data[sizeof(*cp) + sizeof(*set) * 1]; | |
864 | struct adv_info *adv; | |
865 | ||
866 | if (instance > 0) { | |
867 | adv = hci_find_adv_instance(hdev, instance); | |
868 | if (!adv) | |
869 | return -EINVAL; | |
870 | /* If already enabled there is nothing to do */ | |
871 | if (adv->enabled) | |
872 | return 0; | |
873 | } else { | |
874 | adv = NULL; | |
875 | } | |
876 | ||
877 | cp = (void *)data; | |
878 | set = (void *)cp->data; | |
879 | ||
880 | memset(cp, 0, sizeof(*cp)); | |
881 | ||
882 | cp->enable = 0x01; | |
883 | cp->num_of_sets = 0x01; | |
884 | ||
885 | memset(set, 0, sizeof(*set)); | |
886 | ||
887 | set->handle = instance; | |
888 | ||
889 | /* Set duration per instance since controller is responsible for | |
890 | * scheduling it. | |
891 | */ | |
892 | if (adv && adv->duration) { | |
893 | u16 duration = adv->timeout * MSEC_PER_SEC; | |
894 | ||
895 | /* Time = N * 10 ms */ | |
896 | set->duration = cpu_to_le16(duration / 10); | |
897 | } | |
898 | ||
899 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, | |
900 | sizeof(*cp) + | |
901 | sizeof(*set) * cp->num_of_sets, | |
902 | data, HCI_CMD_TIMEOUT); | |
903 | } | |
904 | ||
905 | int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance) | |
906 | { | |
907 | int err; | |
908 | ||
909 | err = hci_setup_ext_adv_instance_sync(hdev, instance); | |
910 | if (err) | |
911 | return err; | |
912 | ||
913 | err = hci_set_ext_scan_rsp_data_sync(hdev, instance); | |
914 | if (err) | |
915 | return err; | |
916 | ||
917 | return hci_enable_ext_advertising_sync(hdev, instance); | |
918 | } | |
919 | ||
920 | static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance) | |
921 | { | |
922 | int err; | |
923 | ||
924 | if (ext_adv_capable(hdev)) | |
925 | return hci_start_ext_adv_sync(hdev, instance); | |
926 | ||
927 | err = hci_update_adv_data_sync(hdev, instance); | |
928 | if (err) | |
929 | return err; | |
930 | ||
931 | err = hci_update_scan_rsp_data_sync(hdev, instance); | |
932 | if (err) | |
933 | return err; | |
934 | ||
935 | return hci_enable_advertising_sync(hdev); | |
936 | } | |
937 | ||
938 | int hci_enable_advertising_sync(struct hci_dev *hdev) | |
939 | { | |
940 | struct adv_info *adv_instance; | |
941 | struct hci_cp_le_set_adv_param cp; | |
942 | u8 own_addr_type, enable = 0x01; | |
943 | bool connectable; | |
944 | u16 adv_min_interval, adv_max_interval; | |
945 | u32 flags; | |
946 | u8 status; | |
947 | ||
ad383c2c LAD |
948 | if (ext_adv_capable(hdev)) |
949 | return hci_enable_ext_advertising_sync(hdev, | |
950 | hdev->cur_adv_instance); | |
951 | ||
cba6b758 LAD |
952 | flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance); |
953 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); | |
954 | ||
955 | /* If the "connectable" instance flag was not set, then choose between | |
956 | * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. | |
957 | */ | |
958 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | |
959 | mgmt_get_connectable(hdev); | |
960 | ||
961 | if (!is_advertising_allowed(hdev, connectable)) | |
962 | return -EINVAL; | |
963 | ||
ad383c2c LAD |
964 | status = hci_disable_advertising_sync(hdev); |
965 | if (status) | |
966 | return status; | |
cba6b758 LAD |
967 | |
968 | /* Clear the HCI_LE_ADV bit temporarily so that the | |
969 | * hci_update_random_address knows that it's safe to go ahead | |
970 | * and write a new random address. The flag will be set back on | |
971 | * as soon as the SET_ADV_ENABLE HCI command completes. | |
972 | */ | |
973 | hci_dev_clear_flag(hdev, HCI_LE_ADV); | |
974 | ||
975 | /* Set require_privacy to true only when non-connectable | |
976 | * advertising is used. In that case it is fine to use a | |
977 | * non-resolvable private address. | |
978 | */ | |
979 | status = hci_update_random_address_sync(hdev, !connectable, | |
980 | adv_use_rpa(hdev, flags), | |
981 | &own_addr_type); | |
982 | if (status) | |
983 | return status; | |
984 | ||
985 | memset(&cp, 0, sizeof(cp)); | |
986 | ||
987 | if (adv_instance) { | |
988 | adv_min_interval = adv_instance->min_interval; | |
989 | adv_max_interval = adv_instance->max_interval; | |
990 | } else { | |
991 | adv_min_interval = hdev->le_adv_min_interval; | |
992 | adv_max_interval = hdev->le_adv_max_interval; | |
993 | } | |
994 | ||
995 | if (connectable) { | |
996 | cp.type = LE_ADV_IND; | |
997 | } else { | |
998 | if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance)) | |
999 | cp.type = LE_ADV_SCAN_IND; | |
1000 | else | |
1001 | cp.type = LE_ADV_NONCONN_IND; | |
1002 | ||
1003 | if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) || | |
1004 | hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { | |
1005 | adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN; | |
1006 | adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | cp.min_interval = cpu_to_le16(adv_min_interval); | |
1011 | cp.max_interval = cpu_to_le16(adv_max_interval); | |
1012 | cp.own_address_type = own_addr_type; | |
1013 | cp.channel_map = hdev->le_adv_channel_map; | |
1014 | ||
1015 | status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, | |
1016 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1017 | if (status) | |
1018 | return status; | |
1019 | ||
1020 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
1021 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
1022 | } | |
1023 | ||
abfeea47 LAD |
1024 | static int enable_advertising_sync(struct hci_dev *hdev, void *data) |
1025 | { | |
1026 | return hci_enable_advertising_sync(hdev); | |
1027 | } | |
1028 | ||
1029 | int hci_enable_advertising(struct hci_dev *hdev) | |
1030 | { | |
1031 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && | |
1032 | list_empty(&hdev->adv_instances)) | |
1033 | return 0; | |
1034 | ||
1035 | return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL); | |
1036 | } | |
1037 | ||
1038 | int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, | |
1039 | struct sock *sk) | |
cba6b758 LAD |
1040 | { |
1041 | int err; | |
1042 | ||
1043 | if (!ext_adv_capable(hdev)) | |
1044 | return 0; | |
1045 | ||
1046 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | |
1047 | if (err) | |
1048 | return err; | |
1049 | ||
1050 | /* If request specifies an instance that doesn't exist, fail */ | |
1051 | if (instance > 0 && !hci_find_adv_instance(hdev, instance)) | |
1052 | return -EINVAL; | |
1053 | ||
1054 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, | |
1055 | sizeof(instance), &instance, 0, | |
1056 | HCI_CMD_TIMEOUT, sk); | |
1057 | } | |
1058 | ||
1059 | static void cancel_adv_timeout(struct hci_dev *hdev) | |
1060 | { | |
1061 | if (hdev->adv_instance_timeout) { | |
1062 | hdev->adv_instance_timeout = 0; | |
1063 | cancel_delayed_work(&hdev->adv_instance_expire); | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1068 | { | |
1069 | struct { | |
1070 | struct hci_cp_le_set_ext_adv_data cp; | |
1071 | u8 data[HCI_MAX_EXT_AD_LENGTH]; | |
1072 | } pdu; | |
1073 | u8 len; | |
1074 | ||
1075 | memset(&pdu, 0, sizeof(pdu)); | |
1076 | ||
1077 | len = eir_create_adv_data(hdev, instance, pdu.data); | |
1078 | ||
1079 | /* There's nothing to do if the data hasn't changed */ | |
1080 | if (hdev->adv_data_len == len && | |
1081 | memcmp(pdu.data, hdev->adv_data, len) == 0) | |
1082 | return 0; | |
1083 | ||
1084 | memcpy(hdev->adv_data, pdu.data, len); | |
1085 | hdev->adv_data_len = len; | |
1086 | ||
1087 | pdu.cp.length = len; | |
1088 | pdu.cp.handle = instance; | |
1089 | pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; | |
1090 | pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; | |
1091 | ||
1092 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, | |
1093 | sizeof(pdu.cp) + len, &pdu.cp, | |
1094 | HCI_CMD_TIMEOUT); | |
1095 | } | |
1096 | ||
1097 | static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1098 | { | |
1099 | struct hci_cp_le_set_adv_data cp; | |
1100 | u8 len; | |
1101 | ||
1102 | memset(&cp, 0, sizeof(cp)); | |
1103 | ||
1104 | len = eir_create_adv_data(hdev, instance, cp.data); | |
1105 | ||
1106 | /* There's nothing to do if the data hasn't changed */ | |
1107 | if (hdev->adv_data_len == len && | |
1108 | memcmp(cp.data, hdev->adv_data, len) == 0) | |
1109 | return 0; | |
1110 | ||
1111 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); | |
1112 | hdev->adv_data_len = len; | |
1113 | ||
1114 | cp.length = len; | |
1115 | ||
1116 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA, | |
1117 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1118 | } | |
1119 | ||
1120 | int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance) | |
1121 | { | |
1122 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
1123 | return 0; | |
1124 | ||
1125 | if (ext_adv_capable(hdev)) | |
1126 | return hci_set_ext_adv_data_sync(hdev, instance); | |
1127 | ||
1128 | return hci_set_adv_data_sync(hdev, instance); | |
1129 | } | |
1130 | ||
1131 | int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance, | |
1132 | bool force) | |
1133 | { | |
1134 | struct adv_info *adv = NULL; | |
1135 | u16 timeout; | |
1136 | ||
cf75ad8b | 1137 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev)) |
cba6b758 LAD |
1138 | return -EPERM; |
1139 | ||
1140 | if (hdev->adv_instance_timeout) | |
1141 | return -EBUSY; | |
1142 | ||
1143 | adv = hci_find_adv_instance(hdev, instance); | |
1144 | if (!adv) | |
1145 | return -ENOENT; | |
1146 | ||
1147 | /* A zero timeout means unlimited advertising. As long as there is | |
1148 | * only one instance, duration should be ignored. We still set a timeout | |
1149 | * in case further instances are being added later on. | |
1150 | * | |
1151 | * If the remaining lifetime of the instance is more than the duration | |
1152 | * then the timeout corresponds to the duration, otherwise it will be | |
1153 | * reduced to the remaining instance lifetime. | |
1154 | */ | |
1155 | if (adv->timeout == 0 || adv->duration <= adv->remaining_time) | |
1156 | timeout = adv->duration; | |
1157 | else | |
1158 | timeout = adv->remaining_time; | |
1159 | ||
1160 | /* The remaining time is being reduced unless the instance is being | |
1161 | * advertised without time limit. | |
1162 | */ | |
1163 | if (adv->timeout) | |
1164 | adv->remaining_time = adv->remaining_time - timeout; | |
1165 | ||
1166 | /* Only use work for scheduling instances with legacy advertising */ | |
1167 | if (!ext_adv_capable(hdev)) { | |
1168 | hdev->adv_instance_timeout = timeout; | |
1169 | queue_delayed_work(hdev->req_workqueue, | |
1170 | &hdev->adv_instance_expire, | |
1171 | msecs_to_jiffies(timeout * 1000)); | |
1172 | } | |
1173 | ||
1174 | /* If we're just re-scheduling the same instance again then do not | |
1175 | * execute any HCI commands. This happens when a single instance is | |
1176 | * being advertised. | |
1177 | */ | |
1178 | if (!force && hdev->cur_adv_instance == instance && | |
1179 | hci_dev_test_flag(hdev, HCI_LE_ADV)) | |
1180 | return 0; | |
1181 | ||
1182 | hdev->cur_adv_instance = instance; | |
1183 | ||
1184 | return hci_start_adv_sync(hdev, instance); | |
1185 | } | |
1186 | ||
1187 | static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk) | |
1188 | { | |
1189 | int err; | |
1190 | ||
1191 | if (!ext_adv_capable(hdev)) | |
1192 | return 0; | |
1193 | ||
1194 | /* Disable instance 0x00 to disable all instances */ | |
1195 | err = hci_disable_ext_adv_instance_sync(hdev, 0x00); | |
1196 | if (err) | |
1197 | return err; | |
1198 | ||
1199 | return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS, | |
1200 | 0, NULL, 0, HCI_CMD_TIMEOUT, sk); | |
1201 | } | |
1202 | ||
1203 | static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force) | |
1204 | { | |
1205 | struct adv_info *adv, *n; | |
1206 | ||
1207 | if (ext_adv_capable(hdev)) | |
1208 | /* Remove all existing sets */ | |
1209 | return hci_clear_adv_sets_sync(hdev, sk); | |
1210 | ||
1211 | /* This is safe as long as there is no command send while the lock is | |
1212 | * held. | |
1213 | */ | |
1214 | hci_dev_lock(hdev); | |
1215 | ||
1216 | /* Cleanup non-ext instances */ | |
1217 | list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { | |
1218 | u8 instance = adv->instance; | |
1219 | int err; | |
1220 | ||
1221 | if (!(force || adv->timeout)) | |
1222 | continue; | |
1223 | ||
1224 | err = hci_remove_adv_instance(hdev, instance); | |
1225 | if (!err) | |
1226 | mgmt_advertising_removed(sk, hdev, instance); | |
1227 | } | |
1228 | ||
1229 | hci_dev_unlock(hdev); | |
1230 | ||
1231 | return 0; | |
1232 | } | |
1233 | ||
1234 | static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance, | |
1235 | struct sock *sk) | |
1236 | { | |
1237 | int err; | |
1238 | ||
1239 | /* If we use extended advertising, instance has to be removed first. */ | |
1240 | if (ext_adv_capable(hdev)) | |
1241 | return hci_remove_ext_adv_instance_sync(hdev, instance, sk); | |
1242 | ||
1243 | /* This is safe as long as there is no command send while the lock is | |
1244 | * held. | |
1245 | */ | |
1246 | hci_dev_lock(hdev); | |
1247 | ||
1248 | err = hci_remove_adv_instance(hdev, instance); | |
1249 | if (!err) | |
1250 | mgmt_advertising_removed(sk, hdev, instance); | |
1251 | ||
1252 | hci_dev_unlock(hdev); | |
1253 | ||
1254 | return err; | |
1255 | } | |
1256 | ||
1257 | /* For a single instance: | |
1258 | * - force == true: The instance will be removed even when its remaining | |
1259 | * lifetime is not zero. | |
1260 | * - force == false: the instance will be deactivated but kept stored unless | |
1261 | * the remaining lifetime is zero. | |
1262 | * | |
1263 | * For instance == 0x00: | |
1264 | * - force == true: All instances will be removed regardless of their timeout | |
1265 | * setting. | |
1266 | * - force == false: Only instances that have a timeout will be removed. | |
1267 | */ | |
1268 | int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk, | |
1269 | u8 instance, bool force) | |
1270 | { | |
1271 | struct adv_info *next = NULL; | |
1272 | int err; | |
1273 | ||
1274 | /* Cancel any timeout concerning the removed instance(s). */ | |
1275 | if (!instance || hdev->cur_adv_instance == instance) | |
1276 | cancel_adv_timeout(hdev); | |
1277 | ||
1278 | /* Get the next instance to advertise BEFORE we remove | |
1279 | * the current one. This can be the same instance again | |
1280 | * if there is only one instance. | |
1281 | */ | |
1282 | if (hdev->cur_adv_instance == instance) | |
1283 | next = hci_get_next_instance(hdev, instance); | |
1284 | ||
1285 | if (!instance) { | |
1286 | err = hci_clear_adv_sync(hdev, sk, force); | |
1287 | if (err) | |
1288 | return err; | |
1289 | } else { | |
1290 | struct adv_info *adv = hci_find_adv_instance(hdev, instance); | |
1291 | ||
1292 | if (force || (adv && adv->timeout && !adv->remaining_time)) { | |
1293 | /* Don't advertise a removed instance. */ | |
1294 | if (next && next->instance == instance) | |
1295 | next = NULL; | |
1296 | ||
1297 | err = hci_remove_adv_sync(hdev, instance, sk); | |
1298 | if (err) | |
1299 | return err; | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
1304 | return 0; | |
1305 | ||
1306 | if (next && !ext_adv_capable(hdev)) | |
1307 | hci_schedule_adv_instance_sync(hdev, next->instance, false); | |
1308 | ||
1309 | return 0; | |
1310 | } | |
1311 | ||
47db6b42 BG |
1312 | int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle) |
1313 | { | |
1314 | struct hci_cp_read_rssi cp; | |
1315 | ||
1316 | cp.handle = handle; | |
1317 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI, | |
1318 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1319 | } | |
1320 | ||
5a750137 BG |
1321 | int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp) |
1322 | { | |
1323 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK, | |
1324 | sizeof(*cp), cp, HCI_CMD_TIMEOUT); | |
1325 | } | |
1326 | ||
47db6b42 BG |
1327 | int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type) |
1328 | { | |
1329 | struct hci_cp_read_tx_power cp; | |
1330 | ||
1331 | cp.handle = handle; | |
1332 | cp.type = type; | |
1333 | return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER, | |
1334 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1335 | } | |
1336 | ||
cba6b758 LAD |
1337 | int hci_disable_advertising_sync(struct hci_dev *hdev) |
1338 | { | |
1339 | u8 enable = 0x00; | |
1340 | ||
ad383c2c LAD |
1341 | /* If controller is not advertising we are done. */ |
1342 | if (!hci_dev_test_flag(hdev, HCI_LE_ADV)) | |
1343 | return 0; | |
1344 | ||
cba6b758 LAD |
1345 | if (ext_adv_capable(hdev)) |
1346 | return hci_disable_ext_adv_instance_sync(hdev, 0x00); | |
1347 | ||
1348 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, | |
1349 | sizeof(enable), &enable, HCI_CMD_TIMEOUT); | |
1350 | } | |
e8907f76 LAD |
1351 | |
1352 | static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val, | |
1353 | u8 filter_dup) | |
1354 | { | |
1355 | struct hci_cp_le_set_ext_scan_enable cp; | |
1356 | ||
1357 | memset(&cp, 0, sizeof(cp)); | |
1358 | cp.enable = val; | |
1359 | cp.filter_dup = filter_dup; | |
1360 | ||
1361 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE, | |
1362 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1363 | } | |
1364 | ||
1365 | static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val, | |
1366 | u8 filter_dup) | |
1367 | { | |
1368 | struct hci_cp_le_set_scan_enable cp; | |
1369 | ||
1370 | if (use_ext_scan(hdev)) | |
1371 | return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup); | |
1372 | ||
1373 | memset(&cp, 0, sizeof(cp)); | |
1374 | cp.enable = val; | |
1375 | cp.filter_dup = filter_dup; | |
1376 | ||
1377 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE, | |
1378 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1379 | } | |
1380 | ||
1381 | static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val) | |
1382 | { | |
ad383c2c LAD |
1383 | if (!use_ll_privacy(hdev)) |
1384 | return 0; | |
1385 | ||
1386 | /* If controller is not/already resolving we are done. */ | |
1387 | if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) | |
e8907f76 LAD |
1388 | return 0; |
1389 | ||
1390 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, | |
1391 | sizeof(val), &val, HCI_CMD_TIMEOUT); | |
1392 | } | |
1393 | ||
ad383c2c | 1394 | int hci_scan_disable_sync(struct hci_dev *hdev) |
e8907f76 LAD |
1395 | { |
1396 | int err; | |
1397 | ||
1398 | /* If controller is not scanning we are done. */ | |
1399 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) | |
1400 | return 0; | |
1401 | ||
1402 | if (hdev->scanning_paused) { | |
1403 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
1404 | return 0; | |
1405 | } | |
1406 | ||
1407 | if (hdev->suspended) | |
1408 | set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks); | |
1409 | ||
1410 | err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00); | |
1411 | if (err) { | |
1412 | bt_dev_err(hdev, "Unable to disable scanning: %d", err); | |
1413 | return err; | |
1414 | } | |
1415 | ||
e8907f76 LAD |
1416 | return err; |
1417 | } | |
1418 | ||
1419 | static bool scan_use_rpa(struct hci_dev *hdev) | |
1420 | { | |
1421 | return hci_dev_test_flag(hdev, HCI_PRIVACY); | |
1422 | } | |
1423 | ||
1424 | static void hci_start_interleave_scan(struct hci_dev *hdev) | |
1425 | { | |
1426 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; | |
1427 | queue_delayed_work(hdev->req_workqueue, | |
1428 | &hdev->interleave_scan, 0); | |
1429 | } | |
1430 | ||
1431 | static bool is_interleave_scanning(struct hci_dev *hdev) | |
1432 | { | |
1433 | return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE; | |
1434 | } | |
1435 | ||
1436 | static void cancel_interleave_scan(struct hci_dev *hdev) | |
1437 | { | |
1438 | bt_dev_dbg(hdev, "cancelling interleave scan"); | |
1439 | ||
1440 | cancel_delayed_work_sync(&hdev->interleave_scan); | |
1441 | ||
1442 | hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE; | |
1443 | } | |
1444 | ||
1445 | /* Return true if interleave_scan wasn't started until exiting this function, | |
1446 | * otherwise, return false | |
1447 | */ | |
1448 | static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev) | |
1449 | { | |
1450 | /* Do interleaved scan only if all of the following are true: | |
1451 | * - There is at least one ADV monitor | |
1452 | * - At least one pending LE connection or one device to be scanned for | |
1453 | * - Monitor offloading is not supported | |
1454 | * If so, we should alternate between allowlist scan and one without | |
1455 | * any filters to save power. | |
1456 | */ | |
1457 | bool use_interleaving = hci_is_adv_monitoring(hdev) && | |
1458 | !(list_empty(&hdev->pend_le_conns) && | |
1459 | list_empty(&hdev->pend_le_reports)) && | |
1460 | hci_get_adv_monitor_offload_ext(hdev) == | |
1461 | HCI_ADV_MONITOR_EXT_NONE; | |
1462 | bool is_interleaving = is_interleave_scanning(hdev); | |
1463 | ||
1464 | if (use_interleaving && !is_interleaving) { | |
1465 | hci_start_interleave_scan(hdev); | |
1466 | bt_dev_dbg(hdev, "starting interleave scan"); | |
1467 | return true; | |
1468 | } | |
1469 | ||
1470 | if (!use_interleaving && is_interleaving) | |
1471 | cancel_interleave_scan(hdev); | |
1472 | ||
1473 | return false; | |
1474 | } | |
1475 | ||
1476 | /* Removes connection to resolve list if needed.*/ | |
1477 | static int hci_le_del_resolve_list_sync(struct hci_dev *hdev, | |
1478 | bdaddr_t *bdaddr, u8 bdaddr_type) | |
1479 | { | |
1480 | struct hci_cp_le_del_from_resolv_list cp; | |
1481 | struct bdaddr_list_with_irk *entry; | |
1482 | ||
ad383c2c | 1483 | if (!use_ll_privacy(hdev)) |
e8907f76 LAD |
1484 | return 0; |
1485 | ||
1486 | /* Check if the IRK has been programmed */ | |
1487 | entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr, | |
1488 | bdaddr_type); | |
1489 | if (!entry) | |
1490 | return 0; | |
1491 | ||
1492 | cp.bdaddr_type = bdaddr_type; | |
1493 | bacpy(&cp.bdaddr, bdaddr); | |
1494 | ||
1495 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST, | |
1496 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1497 | } | |
1498 | ||
1499 | static int hci_le_del_accept_list_sync(struct hci_dev *hdev, | |
1500 | bdaddr_t *bdaddr, u8 bdaddr_type) | |
1501 | { | |
1502 | struct hci_cp_le_del_from_accept_list cp; | |
1503 | int err; | |
1504 | ||
1505 | /* Check if device is on accept list before removing it */ | |
1506 | if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type)) | |
1507 | return 0; | |
1508 | ||
1509 | cp.bdaddr_type = bdaddr_type; | |
1510 | bacpy(&cp.bdaddr, bdaddr); | |
1511 | ||
ad383c2c LAD |
1512 | /* Ignore errors when removing from resolving list as that is likely |
1513 | * that the device was never added. | |
1514 | */ | |
1515 | hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); | |
1516 | ||
e8907f76 LAD |
1517 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, |
1518 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1519 | if (err) { | |
1520 | bt_dev_err(hdev, "Unable to remove from allow list: %d", err); | |
1521 | return err; | |
1522 | } | |
1523 | ||
1524 | bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr, | |
1525 | cp.bdaddr_type); | |
1526 | ||
ad383c2c | 1527 | return 0; |
e8907f76 LAD |
1528 | } |
1529 | ||
cf75ad8b LAD |
1530 | /* Adds connection to resolve list if needed. |
1531 | * Setting params to NULL programs local hdev->irk | |
1532 | */ | |
e8907f76 LAD |
1533 | static int hci_le_add_resolve_list_sync(struct hci_dev *hdev, |
1534 | struct hci_conn_params *params) | |
1535 | { | |
1536 | struct hci_cp_le_add_to_resolv_list cp; | |
1537 | struct smp_irk *irk; | |
1538 | struct bdaddr_list_with_irk *entry; | |
1539 | ||
ad383c2c | 1540 | if (!use_ll_privacy(hdev)) |
e8907f76 LAD |
1541 | return 0; |
1542 | ||
cf75ad8b LAD |
1543 | /* Attempt to program local identity address, type and irk if params is |
1544 | * NULL. | |
1545 | */ | |
1546 | if (!params) { | |
1547 | if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) | |
1548 | return 0; | |
1549 | ||
1550 | hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type); | |
1551 | memcpy(cp.peer_irk, hdev->irk, 16); | |
1552 | goto done; | |
1553 | } | |
1554 | ||
e8907f76 LAD |
1555 | irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); |
1556 | if (!irk) | |
1557 | return 0; | |
1558 | ||
1559 | /* Check if the IK has _not_ been programmed yet. */ | |
1560 | entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, | |
1561 | ¶ms->addr, | |
1562 | params->addr_type); | |
1563 | if (entry) | |
1564 | return 0; | |
1565 | ||
1566 | cp.bdaddr_type = params->addr_type; | |
1567 | bacpy(&cp.bdaddr, ¶ms->addr); | |
1568 | memcpy(cp.peer_irk, irk->val, 16); | |
1569 | ||
cf75ad8b | 1570 | done: |
e8907f76 LAD |
1571 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
1572 | memcpy(cp.local_irk, hdev->irk, 16); | |
1573 | else | |
1574 | memset(cp.local_irk, 0, 16); | |
1575 | ||
1576 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST, | |
1577 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1578 | } | |
1579 | ||
1580 | /* Adds connection to allow list if needed, if the device uses RPA (has IRK) | |
1581 | * this attempts to program the device in the resolving list as well. | |
1582 | */ | |
1583 | static int hci_le_add_accept_list_sync(struct hci_dev *hdev, | |
1584 | struct hci_conn_params *params, | |
ad383c2c | 1585 | u8 *num_entries) |
e8907f76 LAD |
1586 | { |
1587 | struct hci_cp_le_add_to_accept_list cp; | |
1588 | int err; | |
1589 | ||
1590 | /* Already in accept list */ | |
1591 | if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr, | |
1592 | params->addr_type)) | |
1593 | return 0; | |
1594 | ||
1595 | /* Select filter policy to accept all advertising */ | |
1596 | if (*num_entries >= hdev->le_accept_list_size) | |
1597 | return -ENOSPC; | |
1598 | ||
1599 | /* Accept list can not be used with RPAs */ | |
ad383c2c | 1600 | if (!use_ll_privacy(hdev) && |
e8907f76 LAD |
1601 | hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) { |
1602 | return -EINVAL; | |
1603 | } | |
1604 | ||
1605 | /* During suspend, only wakeable devices can be in acceptlist */ | |
1606 | if (hdev->suspended && !hci_conn_test_flag(HCI_CONN_FLAG_REMOTE_WAKEUP, | |
1607 | params->current_flags)) | |
1608 | return 0; | |
1609 | ||
ad383c2c LAD |
1610 | /* Attempt to program the device in the resolving list first to avoid |
1611 | * having to rollback in case it fails since the resolving list is | |
1612 | * dynamic it can probably be smaller than the accept list. | |
1613 | */ | |
1614 | err = hci_le_add_resolve_list_sync(hdev, params); | |
1615 | if (err) { | |
1616 | bt_dev_err(hdev, "Unable to add to resolve list: %d", err); | |
1617 | return err; | |
1618 | } | |
1619 | ||
e8907f76 LAD |
1620 | *num_entries += 1; |
1621 | cp.bdaddr_type = params->addr_type; | |
1622 | bacpy(&cp.bdaddr, ¶ms->addr); | |
1623 | ||
1624 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST, | |
1625 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1626 | if (err) { | |
1627 | bt_dev_err(hdev, "Unable to add to allow list: %d", err); | |
ad383c2c LAD |
1628 | /* Rollback the device from the resolving list */ |
1629 | hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); | |
e8907f76 LAD |
1630 | return err; |
1631 | } | |
1632 | ||
1633 | bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr, | |
1634 | cp.bdaddr_type); | |
1635 | ||
ad383c2c LAD |
1636 | return 0; |
1637 | } | |
1638 | ||
1639 | /* This function disables all advertising instances (including 0x00) */ | |
1640 | static int hci_pause_advertising_sync(struct hci_dev *hdev) | |
1641 | { | |
1642 | int err; | |
1643 | ||
1644 | /* If there are no instances or advertising has already been paused | |
1645 | * there is nothing to do. | |
1646 | */ | |
1647 | if (!hdev->adv_instance_cnt || hdev->advertising_paused) | |
1648 | return 0; | |
1649 | ||
1650 | bt_dev_dbg(hdev, "Pausing advertising instances"); | |
1651 | ||
1652 | /* Call to disable any advertisements active on the controller. | |
1653 | * This will succeed even if no advertisements are configured. | |
1654 | */ | |
1655 | err = hci_disable_advertising_sync(hdev); | |
1656 | if (err) | |
1657 | return err; | |
1658 | ||
1659 | /* If we are using software rotation, pause the loop */ | |
1660 | if (!ext_adv_capable(hdev)) | |
1661 | cancel_adv_timeout(hdev); | |
1662 | ||
1663 | hdev->advertising_paused = true; | |
1664 | ||
1665 | return 0; | |
e8907f76 LAD |
1666 | } |
1667 | ||
abfeea47 | 1668 | /* This function enables all user advertising instances (excluding 0x00) */ |
ad383c2c LAD |
1669 | static int hci_resume_advertising_sync(struct hci_dev *hdev) |
1670 | { | |
1671 | struct adv_info *adv, *tmp; | |
1672 | int err; | |
1673 | ||
1674 | /* If advertising has not been paused there is nothing to do. */ | |
1675 | if (!hdev->advertising_paused) | |
1676 | return 0; | |
1677 | ||
1678 | bt_dev_dbg(hdev, "Resuming advertising instances"); | |
1679 | ||
1680 | if (ext_adv_capable(hdev)) { | |
1681 | /* Call for each tracked instance to be re-enabled */ | |
1682 | list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) { | |
1683 | err = hci_enable_ext_advertising_sync(hdev, | |
1684 | adv->instance); | |
1685 | if (!err) | |
1686 | continue; | |
1687 | ||
1688 | /* If the instance cannot be resumed remove it */ | |
1689 | hci_remove_ext_adv_instance_sync(hdev, adv->instance, | |
1690 | NULL); | |
1691 | } | |
1692 | } else { | |
1693 | /* Schedule for most recent instance to be restarted and begin | |
1694 | * the software rotation loop | |
1695 | */ | |
1696 | err = hci_schedule_adv_instance_sync(hdev, | |
1697 | hdev->cur_adv_instance, | |
1698 | true); | |
1699 | } | |
1700 | ||
1701 | hdev->advertising_paused = false; | |
1702 | ||
1703 | return err; | |
1704 | } | |
1705 | ||
f892244b BG |
1706 | struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev, |
1707 | bool extended, struct sock *sk) | |
1708 | { | |
1709 | u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA : | |
1710 | HCI_OP_READ_LOCAL_OOB_DATA; | |
1711 | ||
1712 | return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk); | |
1713 | } | |
1714 | ||
ad383c2c LAD |
1715 | /* Device must not be scanning when updating the accept list. |
1716 | * | |
1717 | * Update is done using the following sequence: | |
1718 | * | |
1719 | * use_ll_privacy((Disable Advertising) -> Disable Resolving List) -> | |
1720 | * Remove Devices From Accept List -> | |
1721 | * (has IRK && use_ll_privacy(Remove Devices From Resolving List))-> | |
1722 | * Add Devices to Accept List -> | |
1723 | * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) -> | |
1724 | * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) -> | |
1725 | * Enable Scanning | |
1726 | * | |
1727 | * In case of failure advertising shall be restored to its original state and | |
1728 | * return would disable accept list since either accept or resolving list could | |
1729 | * not be programmed. | |
1730 | * | |
1731 | */ | |
e8907f76 LAD |
1732 | static u8 hci_update_accept_list_sync(struct hci_dev *hdev) |
1733 | { | |
1734 | struct hci_conn_params *params; | |
1735 | struct bdaddr_list *b, *t; | |
1736 | u8 num_entries = 0; | |
1737 | bool pend_conn, pend_report; | |
ad383c2c LAD |
1738 | int err; |
1739 | ||
1740 | /* Pause advertising if resolving list can be used as controllers are | |
1741 | * cannot accept resolving list modifications while advertising. | |
e8907f76 | 1742 | */ |
ad383c2c LAD |
1743 | if (use_ll_privacy(hdev)) { |
1744 | err = hci_pause_advertising_sync(hdev); | |
1745 | if (err) { | |
1746 | bt_dev_err(hdev, "pause advertising failed: %d", err); | |
1747 | return 0x00; | |
1748 | } | |
1749 | } | |
e8907f76 | 1750 | |
ad383c2c LAD |
1751 | /* Disable address resolution while reprogramming accept list since |
1752 | * devices that do have an IRK will be programmed in the resolving list | |
1753 | * when LL Privacy is enabled. | |
1754 | */ | |
1755 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); | |
1756 | if (err) { | |
1757 | bt_dev_err(hdev, "Unable to disable LL privacy: %d", err); | |
1758 | goto done; | |
1759 | } | |
e8907f76 LAD |
1760 | |
1761 | /* Go through the current accept list programmed into the | |
1762 | * controller one by one and check if that address is still | |
1763 | * in the list of pending connections or list of devices to | |
1764 | * report. If not present in either list, then remove it from | |
1765 | * the controller. | |
1766 | */ | |
1767 | list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) { | |
1768 | pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns, | |
1769 | &b->bdaddr, | |
1770 | b->bdaddr_type); | |
1771 | pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports, | |
1772 | &b->bdaddr, | |
1773 | b->bdaddr_type); | |
1774 | ||
1775 | /* If the device is not likely to connect or report, | |
1776 | * remove it from the acceptlist. | |
1777 | */ | |
1778 | if (!pend_conn && !pend_report) { | |
1779 | hci_le_del_accept_list_sync(hdev, &b->bdaddr, | |
1780 | b->bdaddr_type); | |
1781 | continue; | |
1782 | } | |
1783 | ||
e8907f76 LAD |
1784 | num_entries++; |
1785 | } | |
1786 | ||
1787 | /* Since all no longer valid accept list entries have been | |
1788 | * removed, walk through the list of pending connections | |
1789 | * and ensure that any new device gets programmed into | |
1790 | * the controller. | |
1791 | * | |
1792 | * If the list of the devices is larger than the list of | |
1793 | * available accept list entries in the controller, then | |
1794 | * just abort and return filer policy value to not use the | |
1795 | * accept list. | |
1796 | */ | |
1797 | list_for_each_entry(params, &hdev->pend_le_conns, action) { | |
ad383c2c LAD |
1798 | err = hci_le_add_accept_list_sync(hdev, params, &num_entries); |
1799 | if (err) | |
1800 | goto done; | |
e8907f76 LAD |
1801 | } |
1802 | ||
1803 | /* After adding all new pending connections, walk through | |
1804 | * the list of pending reports and also add these to the | |
1805 | * accept list if there is still space. Abort if space runs out. | |
1806 | */ | |
1807 | list_for_each_entry(params, &hdev->pend_le_reports, action) { | |
ad383c2c LAD |
1808 | err = hci_le_add_accept_list_sync(hdev, params, &num_entries); |
1809 | if (err) | |
1810 | goto done; | |
e8907f76 LAD |
1811 | } |
1812 | ||
1813 | /* Use the allowlist unless the following conditions are all true: | |
1814 | * - We are not currently suspending | |
1815 | * - There are 1 or more ADV monitors registered and it's not offloaded | |
1816 | * - Interleaved scanning is not currently using the allowlist | |
1817 | */ | |
1818 | if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended && | |
1819 | hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE && | |
1820 | hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST) | |
ad383c2c LAD |
1821 | err = -EINVAL; |
1822 | ||
1823 | done: | |
1824 | /* Enable address resolution when LL Privacy is enabled. */ | |
1825 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01); | |
1826 | if (err) | |
1827 | bt_dev_err(hdev, "Unable to enable LL privacy: %d", err); | |
1828 | ||
1829 | /* Resume advertising if it was paused */ | |
1830 | if (use_ll_privacy(hdev)) | |
1831 | hci_resume_advertising_sync(hdev); | |
e8907f76 LAD |
1832 | |
1833 | /* Select filter policy to use accept list */ | |
ad383c2c | 1834 | return err ? 0x00 : 0x01; |
e8907f76 LAD |
1835 | } |
1836 | ||
1837 | /* Returns true if an le connection is in the scanning state */ | |
1838 | static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev) | |
1839 | { | |
1840 | struct hci_conn_hash *h = &hdev->conn_hash; | |
1841 | struct hci_conn *c; | |
1842 | ||
1843 | rcu_read_lock(); | |
1844 | ||
1845 | list_for_each_entry_rcu(c, &h->list, list) { | |
1846 | if (c->type == LE_LINK && c->state == BT_CONNECT && | |
1847 | test_bit(HCI_CONN_SCANNING, &c->flags)) { | |
1848 | rcu_read_unlock(); | |
1849 | return true; | |
1850 | } | |
1851 | } | |
1852 | ||
1853 | rcu_read_unlock(); | |
1854 | ||
1855 | return false; | |
1856 | } | |
1857 | ||
1858 | static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type, | |
1859 | u16 interval, u16 window, | |
1860 | u8 own_addr_type, u8 filter_policy) | |
1861 | { | |
1862 | struct hci_cp_le_set_ext_scan_params *cp; | |
1863 | struct hci_cp_le_scan_phy_params *phy; | |
1864 | u8 data[sizeof(*cp) + sizeof(*phy) * 2]; | |
1865 | u8 num_phy = 0; | |
1866 | ||
1867 | cp = (void *)data; | |
1868 | phy = (void *)cp->data; | |
1869 | ||
1870 | memset(data, 0, sizeof(data)); | |
1871 | ||
1872 | cp->own_addr_type = own_addr_type; | |
1873 | cp->filter_policy = filter_policy; | |
1874 | ||
1875 | if (scan_1m(hdev) || scan_2m(hdev)) { | |
1876 | cp->scanning_phys |= LE_SCAN_PHY_1M; | |
1877 | ||
1878 | phy->type = type; | |
1879 | phy->interval = cpu_to_le16(interval); | |
1880 | phy->window = cpu_to_le16(window); | |
1881 | ||
1882 | num_phy++; | |
1883 | phy++; | |
1884 | } | |
1885 | ||
1886 | if (scan_coded(hdev)) { | |
1887 | cp->scanning_phys |= LE_SCAN_PHY_CODED; | |
1888 | ||
1889 | phy->type = type; | |
1890 | phy->interval = cpu_to_le16(interval); | |
1891 | phy->window = cpu_to_le16(window); | |
1892 | ||
1893 | num_phy++; | |
1894 | phy++; | |
1895 | } | |
1896 | ||
1897 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS, | |
1898 | sizeof(*cp) + sizeof(*phy) * num_phy, | |
1899 | data, HCI_CMD_TIMEOUT); | |
1900 | } | |
1901 | ||
1902 | static int hci_le_set_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_scan_param cp; | |
1907 | ||
1908 | if (use_ext_scan(hdev)) | |
1909 | return hci_le_set_ext_scan_param_sync(hdev, type, interval, | |
1910 | window, own_addr_type, | |
1911 | filter_policy); | |
1912 | ||
1913 | memset(&cp, 0, sizeof(cp)); | |
1914 | cp.type = type; | |
1915 | cp.interval = cpu_to_le16(interval); | |
1916 | cp.window = cpu_to_le16(window); | |
1917 | cp.own_address_type = own_addr_type; | |
1918 | cp.filter_policy = filter_policy; | |
1919 | ||
1920 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM, | |
1921 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
1922 | } | |
1923 | ||
1924 | static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval, | |
abfeea47 LAD |
1925 | u16 window, u8 own_addr_type, u8 filter_policy, |
1926 | u8 filter_dup) | |
e8907f76 LAD |
1927 | { |
1928 | int err; | |
1929 | ||
1930 | if (hdev->scanning_paused) { | |
1931 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
1932 | return 0; | |
1933 | } | |
1934 | ||
e8907f76 LAD |
1935 | err = hci_le_set_scan_param_sync(hdev, type, interval, window, |
1936 | own_addr_type, filter_policy); | |
1937 | if (err) | |
1938 | return err; | |
1939 | ||
abfeea47 | 1940 | return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup); |
e8907f76 LAD |
1941 | } |
1942 | ||
e8907f76 LAD |
1943 | int hci_passive_scan_sync(struct hci_dev *hdev) |
1944 | { | |
1945 | u8 own_addr_type; | |
1946 | u8 filter_policy; | |
1947 | u16 window, interval; | |
ad383c2c | 1948 | int err; |
e8907f76 LAD |
1949 | |
1950 | if (hdev->scanning_paused) { | |
1951 | bt_dev_dbg(hdev, "Scanning is paused for suspend"); | |
1952 | return 0; | |
1953 | } | |
1954 | ||
ad383c2c LAD |
1955 | err = hci_scan_disable_sync(hdev); |
1956 | if (err) { | |
1957 | bt_dev_err(hdev, "disable scanning failed: %d", err); | |
1958 | return err; | |
1959 | } | |
1960 | ||
e8907f76 LAD |
1961 | /* Set require_privacy to false since no SCAN_REQ are send |
1962 | * during passive scanning. Not using an non-resolvable address | |
1963 | * here is important so that peer devices using direct | |
1964 | * advertising with our address will be correctly reported | |
1965 | * by the controller. | |
1966 | */ | |
1967 | if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev), | |
1968 | &own_addr_type)) | |
1969 | return 0; | |
1970 | ||
1971 | if (hdev->enable_advmon_interleave_scan && | |
1972 | hci_update_interleaved_scan_sync(hdev)) | |
1973 | return 0; | |
1974 | ||
1975 | bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state); | |
ad383c2c | 1976 | |
e8907f76 LAD |
1977 | /* Adding or removing entries from the accept list must |
1978 | * happen before enabling scanning. The controller does | |
1979 | * not allow accept list modification while scanning. | |
1980 | */ | |
1981 | filter_policy = hci_update_accept_list_sync(hdev); | |
1982 | ||
1983 | /* When the controller is using random resolvable addresses and | |
1984 | * with that having LE privacy enabled, then controllers with | |
1985 | * Extended Scanner Filter Policies support can now enable support | |
1986 | * for handling directed advertising. | |
1987 | * | |
1988 | * So instead of using filter polices 0x00 (no acceptlist) | |
1989 | * and 0x01 (acceptlist enabled) use the new filter policies | |
1990 | * 0x02 (no acceptlist) and 0x03 (acceptlist enabled). | |
1991 | */ | |
1992 | if (hci_dev_test_flag(hdev, HCI_PRIVACY) && | |
1993 | (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) | |
1994 | filter_policy |= 0x02; | |
1995 | ||
1996 | if (hdev->suspended) { | |
1997 | window = hdev->le_scan_window_suspend; | |
1998 | interval = hdev->le_scan_int_suspend; | |
1999 | ||
2000 | set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks); | |
2001 | } else if (hci_is_le_conn_scanning(hdev)) { | |
2002 | window = hdev->le_scan_window_connect; | |
2003 | interval = hdev->le_scan_int_connect; | |
2004 | } else if (hci_is_adv_monitoring(hdev)) { | |
2005 | window = hdev->le_scan_window_adv_monitor; | |
2006 | interval = hdev->le_scan_int_adv_monitor; | |
2007 | } else { | |
2008 | window = hdev->le_scan_window; | |
2009 | interval = hdev->le_scan_interval; | |
2010 | } | |
2011 | ||
2012 | bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy); | |
2013 | ||
2014 | return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window, | |
abfeea47 LAD |
2015 | own_addr_type, filter_policy, |
2016 | LE_SCAN_FILTER_DUP_ENABLE); | |
e8907f76 LAD |
2017 | } |
2018 | ||
2019 | /* This function controls the passive scanning based on hdev->pend_le_conns | |
2020 | * list. If there are pending LE connection we start the background scanning, | |
ad383c2c LAD |
2021 | * otherwise we stop it in the following sequence: |
2022 | * | |
2023 | * If there are devices to scan: | |
2024 | * | |
2025 | * Disable Scanning -> Update Accept List -> | |
2026 | * use_ll_privacy((Disable Advertising) -> Disable Resolving List -> | |
2027 | * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) -> | |
2028 | * Enable Scanning | |
2029 | * | |
2030 | * Otherwise: | |
2031 | * | |
2032 | * Disable Scanning | |
e8907f76 LAD |
2033 | */ |
2034 | int hci_update_passive_scan_sync(struct hci_dev *hdev) | |
2035 | { | |
2036 | int err; | |
2037 | ||
2038 | if (!test_bit(HCI_UP, &hdev->flags) || | |
2039 | test_bit(HCI_INIT, &hdev->flags) || | |
2040 | hci_dev_test_flag(hdev, HCI_SETUP) || | |
2041 | hci_dev_test_flag(hdev, HCI_CONFIG) || | |
2042 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || | |
2043 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) | |
2044 | return 0; | |
2045 | ||
2046 | /* No point in doing scanning if LE support hasn't been enabled */ | |
2047 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
2048 | return 0; | |
2049 | ||
2050 | /* If discovery is active don't interfere with it */ | |
2051 | if (hdev->discovery.state != DISCOVERY_STOPPED) | |
2052 | return 0; | |
2053 | ||
2054 | /* Reset RSSI and UUID filters when starting background scanning | |
2055 | * since these filters are meant for service discovery only. | |
2056 | * | |
2057 | * The Start Discovery and Start Service Discovery operations | |
2058 | * ensure to set proper values for RSSI threshold and UUID | |
2059 | * filter list. So it is safe to just reset them here. | |
2060 | */ | |
2061 | hci_discovery_filter_clear(hdev); | |
2062 | ||
2063 | bt_dev_dbg(hdev, "ADV monitoring is %s", | |
2064 | hci_is_adv_monitoring(hdev) ? "on" : "off"); | |
2065 | ||
2066 | if (list_empty(&hdev->pend_le_conns) && | |
2067 | list_empty(&hdev->pend_le_reports) && | |
2068 | !hci_is_adv_monitoring(hdev)) { | |
2069 | /* If there is no pending LE connections or devices | |
2070 | * to be scanned for or no ADV monitors, we should stop the | |
2071 | * background scanning. | |
2072 | */ | |
2073 | ||
2074 | bt_dev_dbg(hdev, "stopping background scanning"); | |
2075 | ||
ad383c2c | 2076 | err = hci_scan_disable_sync(hdev); |
e8907f76 LAD |
2077 | if (err) |
2078 | bt_dev_err(hdev, "stop background scanning failed: %d", | |
2079 | err); | |
2080 | } else { | |
2081 | /* If there is at least one pending LE connection, we should | |
2082 | * keep the background scan running. | |
2083 | */ | |
2084 | ||
2085 | /* If controller is connecting, we should not start scanning | |
2086 | * since some controllers are not able to scan and connect at | |
2087 | * the same time. | |
2088 | */ | |
2089 | if (hci_lookup_le_connect(hdev)) | |
2090 | return 0; | |
2091 | ||
e8907f76 LAD |
2092 | bt_dev_dbg(hdev, "start background scanning"); |
2093 | ||
2094 | err = hci_passive_scan_sync(hdev); | |
2095 | if (err) | |
2096 | bt_dev_err(hdev, "start background scanning failed: %d", | |
2097 | err); | |
2098 | } | |
2099 | ||
2100 | return err; | |
2101 | } | |
ad383c2c LAD |
2102 | |
2103 | static int update_passive_scan_sync(struct hci_dev *hdev, void *data) | |
2104 | { | |
2105 | return hci_update_passive_scan_sync(hdev); | |
2106 | } | |
2107 | ||
2108 | int hci_update_passive_scan(struct hci_dev *hdev) | |
2109 | { | |
5bee2fd6 LAD |
2110 | /* Only queue if it would have any effect */ |
2111 | if (!test_bit(HCI_UP, &hdev->flags) || | |
2112 | test_bit(HCI_INIT, &hdev->flags) || | |
2113 | hci_dev_test_flag(hdev, HCI_SETUP) || | |
2114 | hci_dev_test_flag(hdev, HCI_CONFIG) || | |
2115 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || | |
2116 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) | |
2117 | return 0; | |
2118 | ||
ad383c2c LAD |
2119 | return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL); |
2120 | } | |
cf75ad8b | 2121 | |
2f2eb0c9 | 2122 | int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val) |
cf75ad8b | 2123 | { |
2f2eb0c9 BG |
2124 | int err; |
2125 | ||
cf75ad8b LAD |
2126 | if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev)) |
2127 | return 0; | |
2128 | ||
2f2eb0c9 | 2129 | err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, |
cf75ad8b | 2130 | sizeof(val), &val, HCI_CMD_TIMEOUT); |
2f2eb0c9 BG |
2131 | |
2132 | if (!err) { | |
2133 | if (val) { | |
2134 | hdev->features[1][0] |= LMP_HOST_SC; | |
2135 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); | |
2136 | } else { | |
2137 | hdev->features[1][0] &= ~LMP_HOST_SC; | |
2138 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); | |
2139 | } | |
2140 | } | |
2141 | ||
2142 | return err; | |
cf75ad8b LAD |
2143 | } |
2144 | ||
2145 | static int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode) | |
2146 | { | |
2147 | int err; | |
2148 | ||
2149 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || | |
2150 | lmp_host_ssp_capable(hdev)) | |
2151 | return 0; | |
2152 | ||
2153 | err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, | |
2154 | sizeof(mode), &mode, HCI_CMD_TIMEOUT); | |
2155 | if (err) | |
2156 | return err; | |
2157 | ||
2158 | return hci_write_sc_support_sync(hdev, 0x01); | |
2159 | } | |
2160 | ||
d81a494c | 2161 | int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul) |
cf75ad8b LAD |
2162 | { |
2163 | struct hci_cp_write_le_host_supported cp; | |
2164 | ||
2165 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) || | |
2166 | !lmp_bredr_capable(hdev)) | |
2167 | return 0; | |
2168 | ||
2169 | /* Check first if we already have the right host state | |
2170 | * (host features set) | |
2171 | */ | |
2172 | if (le == lmp_host_le_capable(hdev) && | |
2173 | simul == lmp_host_le_br_capable(hdev)) | |
2174 | return 0; | |
2175 | ||
2176 | memset(&cp, 0, sizeof(cp)); | |
2177 | ||
2178 | cp.le = le; | |
2179 | cp.simul = simul; | |
2180 | ||
2181 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, | |
2182 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2183 | } | |
2184 | ||
2185 | static int hci_powered_update_adv_sync(struct hci_dev *hdev) | |
2186 | { | |
2187 | struct adv_info *adv, *tmp; | |
2188 | int err; | |
2189 | ||
2190 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) | |
2191 | return 0; | |
2192 | ||
2193 | /* If RPA Resolution has not been enable yet it means the | |
2194 | * resolving list is empty and we should attempt to program the | |
2195 | * local IRK in order to support using own_addr_type | |
2196 | * ADDR_LE_DEV_RANDOM_RESOLVED (0x03). | |
2197 | */ | |
2198 | if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) { | |
2199 | hci_le_add_resolve_list_sync(hdev, NULL); | |
2200 | hci_le_set_addr_resolution_enable_sync(hdev, 0x01); | |
2201 | } | |
2202 | ||
2203 | /* Make sure the controller has a good default for | |
2204 | * advertising data. This also applies to the case | |
2205 | * where BR/EDR was toggled during the AUTO_OFF phase. | |
2206 | */ | |
2207 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || | |
2208 | list_empty(&hdev->adv_instances)) { | |
2209 | if (ext_adv_capable(hdev)) { | |
2210 | err = hci_setup_ext_adv_instance_sync(hdev, 0x00); | |
2211 | if (!err) | |
2212 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
2213 | } else { | |
2214 | err = hci_update_adv_data_sync(hdev, 0x00); | |
2215 | if (!err) | |
2216 | hci_update_scan_rsp_data_sync(hdev, 0x00); | |
2217 | } | |
2218 | ||
2219 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) | |
2220 | hci_enable_advertising_sync(hdev); | |
2221 | } | |
2222 | ||
2223 | /* Call for each tracked instance to be scheduled */ | |
2224 | list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) | |
2225 | hci_schedule_adv_instance_sync(hdev, adv->instance, true); | |
2226 | ||
2227 | return 0; | |
2228 | } | |
2229 | ||
2230 | static int hci_write_auth_enable_sync(struct hci_dev *hdev) | |
2231 | { | |
2232 | u8 link_sec; | |
2233 | ||
2234 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); | |
2235 | if (link_sec == test_bit(HCI_AUTH, &hdev->flags)) | |
2236 | return 0; | |
2237 | ||
2238 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE, | |
2239 | sizeof(link_sec), &link_sec, | |
2240 | HCI_CMD_TIMEOUT); | |
2241 | } | |
2242 | ||
353a0249 | 2243 | int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable) |
cf75ad8b LAD |
2244 | { |
2245 | struct hci_cp_write_page_scan_activity cp; | |
2246 | u8 type; | |
2247 | int err = 0; | |
2248 | ||
2249 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
2250 | return 0; | |
2251 | ||
2252 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
2253 | return 0; | |
2254 | ||
2255 | memset(&cp, 0, sizeof(cp)); | |
2256 | ||
2257 | if (enable) { | |
2258 | type = PAGE_SCAN_TYPE_INTERLACED; | |
2259 | ||
2260 | /* 160 msec page scan interval */ | |
2261 | cp.interval = cpu_to_le16(0x0100); | |
2262 | } else { | |
2263 | type = hdev->def_page_scan_type; | |
2264 | cp.interval = cpu_to_le16(hdev->def_page_scan_int); | |
2265 | } | |
2266 | ||
2267 | cp.window = cpu_to_le16(hdev->def_page_scan_window); | |
2268 | ||
2269 | if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval || | |
2270 | __cpu_to_le16(hdev->page_scan_window) != cp.window) { | |
2271 | err = __hci_cmd_sync_status(hdev, | |
2272 | HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, | |
2273 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2274 | if (err) | |
2275 | return err; | |
2276 | } | |
2277 | ||
2278 | if (hdev->page_scan_type != type) | |
2279 | err = __hci_cmd_sync_status(hdev, | |
2280 | HCI_OP_WRITE_PAGE_SCAN_TYPE, | |
2281 | sizeof(type), &type, | |
2282 | HCI_CMD_TIMEOUT); | |
2283 | ||
2284 | return err; | |
2285 | } | |
2286 | ||
2287 | static bool disconnected_accept_list_entries(struct hci_dev *hdev) | |
2288 | { | |
2289 | struct bdaddr_list *b; | |
2290 | ||
2291 | list_for_each_entry(b, &hdev->accept_list, list) { | |
2292 | struct hci_conn *conn; | |
2293 | ||
2294 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); | |
2295 | if (!conn) | |
2296 | return true; | |
2297 | ||
2298 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) | |
2299 | return true; | |
2300 | } | |
2301 | ||
2302 | return false; | |
2303 | } | |
2304 | ||
2305 | static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val) | |
2306 | { | |
2307 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE, | |
2308 | sizeof(val), &val, | |
2309 | HCI_CMD_TIMEOUT); | |
2310 | } | |
2311 | ||
451d95a9 | 2312 | int hci_update_scan_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
2313 | { |
2314 | u8 scan; | |
2315 | ||
2316 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) | |
2317 | return 0; | |
2318 | ||
2319 | if (!hdev_is_powered(hdev)) | |
2320 | return 0; | |
2321 | ||
2322 | if (mgmt_powering_down(hdev)) | |
2323 | return 0; | |
2324 | ||
2325 | if (hdev->scanning_paused) | |
2326 | return 0; | |
2327 | ||
2328 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || | |
2329 | disconnected_accept_list_entries(hdev)) | |
2330 | scan = SCAN_PAGE; | |
2331 | else | |
2332 | scan = SCAN_DISABLED; | |
2333 | ||
2334 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) | |
2335 | scan |= SCAN_INQUIRY; | |
2336 | ||
2337 | if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) && | |
2338 | test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY)) | |
2339 | return 0; | |
2340 | ||
2341 | return hci_write_scan_enable_sync(hdev, scan); | |
2342 | } | |
2343 | ||
6f6ff38a | 2344 | int hci_update_name_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
2345 | { |
2346 | struct hci_cp_write_local_name cp; | |
2347 | ||
2348 | memset(&cp, 0, sizeof(cp)); | |
2349 | ||
2350 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); | |
2351 | ||
2352 | return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME, | |
2353 | sizeof(cp), &cp, | |
2354 | HCI_CMD_TIMEOUT); | |
2355 | } | |
2356 | ||
2357 | /* This function perform powered update HCI command sequence after the HCI init | |
2358 | * sequence which end up resetting all states, the sequence is as follows: | |
2359 | * | |
2360 | * HCI_SSP_ENABLED(Enable SSP) | |
2361 | * HCI_LE_ENABLED(Enable LE) | |
2362 | * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) -> | |
2363 | * Update adv data) | |
2364 | * Enable Authentication | |
2365 | * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class -> | |
2366 | * Set Name -> Set EIR) | |
2367 | */ | |
2368 | int hci_powered_update_sync(struct hci_dev *hdev) | |
2369 | { | |
2370 | int err; | |
2371 | ||
2372 | /* Register the available SMP channels (BR/EDR and LE) only when | |
2373 | * successfully powering on the controller. This late | |
2374 | * registration is required so that LE SMP can clearly decide if | |
2375 | * the public address or static address is used. | |
2376 | */ | |
2377 | smp_register(hdev); | |
2378 | ||
2379 | err = hci_write_ssp_mode_sync(hdev, 0x01); | |
2380 | if (err) | |
2381 | return err; | |
2382 | ||
2383 | err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00); | |
2384 | if (err) | |
2385 | return err; | |
2386 | ||
2387 | err = hci_powered_update_adv_sync(hdev); | |
2388 | if (err) | |
2389 | return err; | |
2390 | ||
2391 | err = hci_write_auth_enable_sync(hdev); | |
2392 | if (err) | |
2393 | return err; | |
2394 | ||
2395 | if (lmp_bredr_capable(hdev)) { | |
2396 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) | |
2397 | hci_write_fast_connectable_sync(hdev, true); | |
2398 | else | |
2399 | hci_write_fast_connectable_sync(hdev, false); | |
2400 | hci_update_scan_sync(hdev); | |
2401 | hci_update_class_sync(hdev); | |
2402 | hci_update_name_sync(hdev); | |
2403 | hci_update_eir_sync(hdev); | |
2404 | } | |
2405 | ||
2406 | return 0; | |
2407 | } | |
2408 | ||
2409 | /* This function perform power on HCI command sequence as follows: | |
2410 | * | |
2411 | * If controller is already up (HCI_UP) performs hci_powered_update_sync | |
2412 | * sequence otherwise run hci_dev_open_sync which will follow with | |
2413 | * hci_powered_update_sync after the init sequence is completed. | |
2414 | */ | |
2415 | static int hci_power_on_sync(struct hci_dev *hdev) | |
2416 | { | |
2417 | int err; | |
2418 | ||
2419 | if (test_bit(HCI_UP, &hdev->flags) && | |
2420 | hci_dev_test_flag(hdev, HCI_MGMT) && | |
2421 | hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { | |
2422 | cancel_delayed_work(&hdev->power_off); | |
2423 | return hci_powered_update_sync(hdev); | |
2424 | } | |
2425 | ||
2426 | err = hci_dev_open_sync(hdev); | |
2427 | if (err < 0) | |
2428 | return err; | |
2429 | ||
2430 | /* During the HCI setup phase, a few error conditions are | |
2431 | * ignored and they need to be checked now. If they are still | |
2432 | * valid, it is important to return the device back off. | |
2433 | */ | |
2434 | if (hci_dev_test_flag(hdev, HCI_RFKILLED) || | |
2435 | hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || | |
2436 | (hdev->dev_type == HCI_PRIMARY && | |
2437 | !bacmp(&hdev->bdaddr, BDADDR_ANY) && | |
2438 | !bacmp(&hdev->static_addr, BDADDR_ANY))) { | |
2439 | hci_dev_clear_flag(hdev, HCI_AUTO_OFF); | |
2440 | hci_dev_close_sync(hdev); | |
2441 | } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { | |
2442 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, | |
2443 | HCI_AUTO_OFF_TIMEOUT); | |
2444 | } | |
2445 | ||
2446 | if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { | |
2447 | /* For unconfigured devices, set the HCI_RAW flag | |
2448 | * so that userspace can easily identify them. | |
2449 | */ | |
2450 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
2451 | set_bit(HCI_RAW, &hdev->flags); | |
2452 | ||
2453 | /* For fully configured devices, this will send | |
2454 | * the Index Added event. For unconfigured devices, | |
2455 | * it will send Unconfigued Index Added event. | |
2456 | * | |
2457 | * Devices with HCI_QUIRK_RAW_DEVICE are ignored | |
2458 | * and no event will be send. | |
2459 | */ | |
2460 | mgmt_index_added(hdev); | |
2461 | } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { | |
2462 | /* When the controller is now configured, then it | |
2463 | * is important to clear the HCI_RAW flag. | |
2464 | */ | |
2465 | if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) | |
2466 | clear_bit(HCI_RAW, &hdev->flags); | |
2467 | ||
2468 | /* Powering on the controller with HCI_CONFIG set only | |
2469 | * happens with the transition from unconfigured to | |
2470 | * configured. This will send the Index Added event. | |
2471 | */ | |
2472 | mgmt_index_added(hdev); | |
2473 | } | |
2474 | ||
2475 | return 0; | |
2476 | } | |
2477 | ||
2478 | static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr) | |
2479 | { | |
2480 | struct hci_cp_remote_name_req_cancel cp; | |
2481 | ||
2482 | memset(&cp, 0, sizeof(cp)); | |
2483 | bacpy(&cp.bdaddr, addr); | |
2484 | ||
2485 | return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, | |
2486 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2487 | } | |
2488 | ||
abfeea47 | 2489 | int hci_stop_discovery_sync(struct hci_dev *hdev) |
cf75ad8b LAD |
2490 | { |
2491 | struct discovery_state *d = &hdev->discovery; | |
2492 | struct inquiry_entry *e; | |
2493 | int err; | |
2494 | ||
2495 | bt_dev_dbg(hdev, "state %u", hdev->discovery.state); | |
2496 | ||
2497 | if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) { | |
2498 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { | |
2499 | err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, | |
2500 | 0, NULL, HCI_CMD_TIMEOUT); | |
2501 | if (err) | |
2502 | return err; | |
2503 | } | |
2504 | ||
2505 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { | |
2506 | cancel_delayed_work(&hdev->le_scan_disable); | |
2507 | cancel_delayed_work(&hdev->le_scan_restart); | |
2508 | ||
2509 | err = hci_scan_disable_sync(hdev); | |
2510 | if (err) | |
2511 | return err; | |
2512 | } | |
2513 | ||
2514 | } else { | |
2515 | err = hci_scan_disable_sync(hdev); | |
2516 | if (err) | |
2517 | return err; | |
2518 | } | |
2519 | ||
abfeea47 LAD |
2520 | /* Resume advertising if it was paused */ |
2521 | if (use_ll_privacy(hdev)) | |
2522 | hci_resume_advertising_sync(hdev); | |
2523 | ||
cf75ad8b LAD |
2524 | /* No further actions needed for LE-only discovery */ |
2525 | if (d->type == DISCOV_TYPE_LE) | |
2526 | return 0; | |
2527 | ||
2528 | if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) { | |
2529 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, | |
2530 | NAME_PENDING); | |
2531 | if (!e) | |
2532 | return 0; | |
2533 | ||
2534 | return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr); | |
2535 | } | |
2536 | ||
2537 | return 0; | |
2538 | } | |
2539 | ||
2540 | static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle, | |
2541 | u8 reason) | |
2542 | { | |
2543 | struct hci_cp_disconn_phy_link cp; | |
2544 | ||
2545 | memset(&cp, 0, sizeof(cp)); | |
2546 | cp.phy_handle = HCI_PHY_HANDLE(handle); | |
2547 | cp.reason = reason; | |
2548 | ||
2549 | return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK, | |
2550 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2551 | } | |
2552 | ||
2553 | static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
2554 | u8 reason) | |
2555 | { | |
2556 | struct hci_cp_disconnect cp; | |
2557 | ||
2558 | if (conn->type == AMP_LINK) | |
2559 | return hci_disconnect_phy_link_sync(hdev, conn->handle, reason); | |
2560 | ||
2561 | memset(&cp, 0, sizeof(cp)); | |
2562 | cp.handle = cpu_to_le16(conn->handle); | |
2563 | cp.reason = reason; | |
2564 | ||
2565 | return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, | |
2566 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2567 | } | |
2568 | ||
2569 | static int hci_le_connect_cancel_sync(struct hci_dev *hdev, | |
2570 | struct hci_conn *conn) | |
2571 | { | |
2572 | if (test_bit(HCI_CONN_SCANNING, &conn->flags)) | |
2573 | return 0; | |
2574 | ||
2575 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL, | |
2576 | 6, &conn->dst, HCI_CMD_TIMEOUT); | |
2577 | } | |
2578 | ||
2579 | static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn) | |
2580 | { | |
2581 | if (conn->type == LE_LINK) | |
2582 | return hci_le_connect_cancel_sync(hdev, conn); | |
2583 | ||
2584 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) | |
2585 | return 0; | |
2586 | ||
2587 | return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL, | |
2588 | 6, &conn->dst, HCI_CMD_TIMEOUT); | |
2589 | } | |
2590 | ||
2591 | static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
2592 | u8 reason) | |
2593 | { | |
2594 | struct hci_cp_reject_sync_conn_req cp; | |
2595 | ||
2596 | memset(&cp, 0, sizeof(cp)); | |
2597 | bacpy(&cp.bdaddr, &conn->dst); | |
2598 | cp.reason = reason; | |
2599 | ||
2600 | /* SCO rejection has its own limited set of | |
2601 | * allowed error values (0x0D-0x0F). | |
2602 | */ | |
2603 | if (reason < 0x0d || reason > 0x0f) | |
2604 | cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; | |
2605 | ||
2606 | return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ, | |
2607 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2608 | } | |
2609 | ||
2610 | static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
2611 | u8 reason) | |
2612 | { | |
2613 | struct hci_cp_reject_conn_req cp; | |
2614 | ||
2615 | if (conn->type == SCO_LINK || conn->type == ESCO_LINK) | |
2616 | return hci_reject_sco_sync(hdev, conn, reason); | |
2617 | ||
2618 | memset(&cp, 0, sizeof(cp)); | |
2619 | bacpy(&cp.bdaddr, &conn->dst); | |
2620 | cp.reason = reason; | |
2621 | ||
2622 | return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ, | |
2623 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2624 | } | |
2625 | ||
2626 | static int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, | |
2627 | u8 reason) | |
2628 | { | |
2629 | switch (conn->state) { | |
2630 | case BT_CONNECTED: | |
2631 | case BT_CONFIG: | |
2632 | return hci_disconnect_sync(hdev, conn, reason); | |
2633 | case BT_CONNECT: | |
2634 | return hci_connect_cancel_sync(hdev, conn); | |
2635 | case BT_CONNECT2: | |
2636 | return hci_reject_conn_sync(hdev, conn, reason); | |
2637 | default: | |
2638 | conn->state = BT_CLOSED; | |
2639 | break; | |
2640 | } | |
2641 | ||
2642 | return 0; | |
2643 | } | |
2644 | ||
2645 | /* This function perform power off HCI command sequence as follows: | |
2646 | * | |
2647 | * Clear Advertising | |
2648 | * Stop Discovery | |
2649 | * Disconnect all connections | |
2650 | * hci_dev_close_sync | |
2651 | */ | |
2652 | static int hci_power_off_sync(struct hci_dev *hdev) | |
2653 | { | |
2654 | struct hci_conn *conn; | |
2655 | int err; | |
2656 | ||
2657 | /* If controller is already down there is nothing to do */ | |
2658 | if (!test_bit(HCI_UP, &hdev->flags)) | |
2659 | return 0; | |
2660 | ||
2661 | if (test_bit(HCI_ISCAN, &hdev->flags) || | |
2662 | test_bit(HCI_PSCAN, &hdev->flags)) { | |
2663 | err = hci_write_scan_enable_sync(hdev, 0x00); | |
2664 | if (err) | |
2665 | return err; | |
2666 | } | |
2667 | ||
2668 | err = hci_clear_adv_sync(hdev, NULL, false); | |
2669 | if (err) | |
2670 | return err; | |
2671 | ||
2672 | err = hci_stop_discovery_sync(hdev); | |
2673 | if (err) | |
2674 | return err; | |
2675 | ||
2676 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { | |
2677 | /* 0x15 == Terminated due to Power Off */ | |
2678 | hci_abort_conn_sync(hdev, conn, 0x15); | |
2679 | } | |
2680 | ||
2681 | return hci_dev_close_sync(hdev); | |
2682 | } | |
2683 | ||
2684 | int hci_set_powered_sync(struct hci_dev *hdev, u8 val) | |
2685 | { | |
2686 | if (val) | |
2687 | return hci_power_on_sync(hdev); | |
2688 | ||
2689 | return hci_power_off_sync(hdev); | |
2690 | } | |
abfeea47 LAD |
2691 | |
2692 | static int hci_inquiry_sync(struct hci_dev *hdev, u8 length) | |
2693 | { | |
2694 | const u8 giac[3] = { 0x33, 0x8b, 0x9e }; | |
2695 | const u8 liac[3] = { 0x00, 0x8b, 0x9e }; | |
2696 | struct hci_cp_inquiry cp; | |
2697 | ||
2698 | bt_dev_dbg(hdev, ""); | |
2699 | ||
2700 | if (hci_dev_test_flag(hdev, HCI_INQUIRY)) | |
2701 | return 0; | |
2702 | ||
2703 | hci_dev_lock(hdev); | |
2704 | hci_inquiry_cache_flush(hdev); | |
2705 | hci_dev_unlock(hdev); | |
2706 | ||
2707 | memset(&cp, 0, sizeof(cp)); | |
2708 | ||
2709 | if (hdev->discovery.limited) | |
2710 | memcpy(&cp.lap, liac, sizeof(cp.lap)); | |
2711 | else | |
2712 | memcpy(&cp.lap, giac, sizeof(cp.lap)); | |
2713 | ||
2714 | cp.length = length; | |
2715 | ||
2716 | return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY, | |
2717 | sizeof(cp), &cp, HCI_CMD_TIMEOUT); | |
2718 | } | |
2719 | ||
2720 | static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval) | |
2721 | { | |
2722 | u8 own_addr_type; | |
2723 | /* Accept list is not used for discovery */ | |
2724 | u8 filter_policy = 0x00; | |
2725 | /* Default is to enable duplicates filter */ | |
2726 | u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; | |
2727 | int err; | |
2728 | ||
2729 | bt_dev_dbg(hdev, ""); | |
2730 | ||
2731 | /* If controller is scanning, it means the passive scanning is | |
2732 | * running. Thus, we should temporarily stop it in order to set the | |
2733 | * discovery scanning parameters. | |
2734 | */ | |
2735 | err = hci_scan_disable_sync(hdev); | |
2736 | if (err) { | |
2737 | bt_dev_err(hdev, "Unable to disable scanning: %d", err); | |
2738 | return err; | |
2739 | } | |
2740 | ||
2741 | cancel_interleave_scan(hdev); | |
2742 | ||
2743 | /* Pause advertising since active scanning disables address resolution | |
2744 | * which advertising depend on in order to generate its RPAs. | |
2745 | */ | |
2746 | if (use_ll_privacy(hdev)) { | |
2747 | err = hci_pause_advertising_sync(hdev); | |
2748 | if (err) { | |
2749 | bt_dev_err(hdev, "pause advertising failed: %d", err); | |
2750 | goto failed; | |
2751 | } | |
2752 | } | |
2753 | ||
2754 | /* Disable address resolution while doing active scanning since the | |
2755 | * accept list shall not be used and all reports shall reach the host | |
2756 | * anyway. | |
2757 | */ | |
2758 | err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); | |
2759 | if (err) { | |
2760 | bt_dev_err(hdev, "Unable to disable Address Resolution: %d", | |
2761 | err); | |
2762 | goto failed; | |
2763 | } | |
2764 | ||
2765 | /* All active scans will be done with either a resolvable private | |
2766 | * address (when privacy feature has been enabled) or non-resolvable | |
2767 | * private address. | |
2768 | */ | |
2769 | err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev), | |
2770 | &own_addr_type); | |
2771 | if (err < 0) | |
2772 | own_addr_type = ADDR_LE_DEV_PUBLIC; | |
2773 | ||
2774 | if (hci_is_adv_monitoring(hdev)) { | |
2775 | /* Duplicate filter should be disabled when some advertisement | |
2776 | * monitor is activated, otherwise AdvMon can only receive one | |
2777 | * advertisement for one peer(*) during active scanning, and | |
2778 | * might report loss to these peers. | |
2779 | * | |
2780 | * Note that different controllers have different meanings of | |
2781 | * |duplicate|. Some of them consider packets with the same | |
2782 | * address as duplicate, and others consider packets with the | |
2783 | * same address and the same RSSI as duplicate. Although in the | |
2784 | * latter case we don't need to disable duplicate filter, but | |
2785 | * it is common to have active scanning for a short period of | |
2786 | * time, the power impact should be neglectable. | |
2787 | */ | |
2788 | filter_dup = LE_SCAN_FILTER_DUP_DISABLE; | |
2789 | } | |
2790 | ||
2791 | err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval, | |
2792 | hdev->le_scan_window_discovery, | |
2793 | own_addr_type, filter_policy, filter_dup); | |
2794 | if (!err) | |
2795 | return err; | |
2796 | ||
2797 | failed: | |
2798 | /* Resume advertising if it was paused */ | |
2799 | if (use_ll_privacy(hdev)) | |
2800 | hci_resume_advertising_sync(hdev); | |
2801 | ||
2802 | /* Resume passive scanning */ | |
2803 | hci_update_passive_scan_sync(hdev); | |
2804 | return err; | |
2805 | } | |
2806 | ||
2807 | static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev) | |
2808 | { | |
2809 | int err; | |
2810 | ||
2811 | bt_dev_dbg(hdev, ""); | |
2812 | ||
2813 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2); | |
2814 | if (err) | |
2815 | return err; | |
2816 | ||
2817 | return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); | |
2818 | } | |
2819 | ||
2820 | int hci_start_discovery_sync(struct hci_dev *hdev) | |
2821 | { | |
2822 | unsigned long timeout; | |
2823 | int err; | |
2824 | ||
2825 | bt_dev_dbg(hdev, "type %u", hdev->discovery.type); | |
2826 | ||
2827 | switch (hdev->discovery.type) { | |
2828 | case DISCOV_TYPE_BREDR: | |
2829 | return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); | |
2830 | case DISCOV_TYPE_INTERLEAVED: | |
2831 | /* When running simultaneous discovery, the LE scanning time | |
2832 | * should occupy the whole discovery time sine BR/EDR inquiry | |
2833 | * and LE scanning are scheduled by the controller. | |
2834 | * | |
2835 | * For interleaving discovery in comparison, BR/EDR inquiry | |
2836 | * and LE scanning are done sequentially with separate | |
2837 | * timeouts. | |
2838 | */ | |
2839 | if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, | |
2840 | &hdev->quirks)) { | |
2841 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); | |
2842 | /* During simultaneous discovery, we double LE scan | |
2843 | * interval. We must leave some time for the controller | |
2844 | * to do BR/EDR inquiry. | |
2845 | */ | |
2846 | err = hci_start_interleaved_discovery_sync(hdev); | |
2847 | break; | |
2848 | } | |
2849 | ||
2850 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); | |
2851 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); | |
2852 | break; | |
2853 | case DISCOV_TYPE_LE: | |
2854 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); | |
2855 | err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); | |
2856 | break; | |
2857 | default: | |
2858 | return -EINVAL; | |
2859 | } | |
2860 | ||
2861 | if (err) | |
2862 | return err; | |
2863 | ||
2864 | bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout)); | |
2865 | ||
2866 | /* When service discovery is used and the controller has a | |
2867 | * strict duplicate filter, it is important to remember the | |
2868 | * start and duration of the scan. This is required for | |
2869 | * restarting scanning during the discovery phase. | |
2870 | */ | |
2871 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) && | |
2872 | hdev->discovery.result_filtering) { | |
2873 | hdev->discovery.scan_start = jiffies; | |
2874 | hdev->discovery.scan_duration = timeout; | |
2875 | } | |
2876 | ||
2877 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable, | |
2878 | timeout); | |
2879 | ||
2880 | return 0; | |
2881 | } |