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