xref: /linux/include/net/bluetooth/hci_core.h (revision 1d7a527788584e4a88ccc532ed73bfdacd0b1e74)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3    BlueZ - Bluetooth protocol stack for Linux
4    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
5    Copyright 2023-2024 NXP
6 
7    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
8 
9    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20    SOFTWARE IS DISCLAIMED.
21 */
22 
23 #ifndef __HCI_CORE_H
24 #define __HCI_CORE_H
25 
26 #include <linux/idr.h>
27 #include <linux/leds.h>
28 #include <linux/rculist.h>
29 #include <linux/spinlock.h>
30 #include <linux/srcu.h>
31 #include <linux/uio.h>
32 
33 #include <net/bluetooth/hci.h>
34 #include <net/bluetooth/hci_drv.h>
35 #include <net/bluetooth/hci_sync.h>
36 #include <net/bluetooth/hci_sock.h>
37 #include <net/bluetooth/coredump.h>
38 
39 /* HCI priority */
40 #define HCI_PRIO_MAX	7
41 
42 /* HCI maximum id value */
43 #define HCI_MAX_ID 10000
44 
45 /* HCI Core structures */
46 struct inquiry_data {
47 	bdaddr_t	bdaddr;
48 	__u8		pscan_rep_mode;
49 	__u8		pscan_period_mode;
50 	__u8		pscan_mode;
51 	__u8		dev_class[3];
52 	__le16		clock_offset;
53 	__s8		rssi;
54 	__u8		ssp_mode;
55 };
56 
57 struct inquiry_entry {
58 	struct list_head	all;		/* inq_cache.all */
59 	struct list_head	list;		/* unknown or resolve */
60 	enum {
61 		NAME_NOT_KNOWN,
62 		NAME_NEEDED,
63 		NAME_PENDING,
64 		NAME_KNOWN,
65 	} name_state;
66 	unsigned long		timestamp;
67 	struct inquiry_data	data;
68 };
69 
70 struct discovery_state {
71 	int			type;
72 	enum {
73 		DISCOVERY_STOPPED,
74 		DISCOVERY_STARTING,
75 		DISCOVERY_FINDING,
76 		DISCOVERY_RESOLVING,
77 		DISCOVERY_STOPPING,
78 	} state;
79 	struct list_head	all;	/* All devices found during inquiry */
80 	struct list_head	unknown;	/* Name state not known */
81 	struct list_head	resolve;	/* Name needs to be resolved */
82 	unsigned long		timestamp;
83 	bdaddr_t		last_adv_addr;
84 	u8			last_adv_addr_type;
85 	s8			last_adv_rssi;
86 	u32			last_adv_flags;
87 	u8			last_adv_data[HCI_MAX_EXT_AD_LENGTH];
88 	u8			last_adv_data_len;
89 	bool			report_invalid_rssi;
90 	bool			result_filtering;
91 	bool			limited;
92 	s8			rssi;
93 	u16			uuid_count;
94 	u8			(*uuids)[16];
95 	unsigned long		name_resolve_timeout;
96 	spinlock_t		lock;
97 };
98 
99 #define SUSPEND_NOTIFIER_TIMEOUT	msecs_to_jiffies(2000) /* 2 seconds */
100 
101 enum suspend_tasks {
102 	SUSPEND_PAUSE_DISCOVERY,
103 	SUSPEND_UNPAUSE_DISCOVERY,
104 
105 	SUSPEND_PAUSE_ADVERTISING,
106 	SUSPEND_UNPAUSE_ADVERTISING,
107 
108 	SUSPEND_SCAN_DISABLE,
109 	SUSPEND_SCAN_ENABLE,
110 	SUSPEND_DISCONNECTING,
111 
112 	SUSPEND_POWERING_DOWN,
113 
114 	SUSPEND_PREPARE_NOTIFIER,
115 
116 	SUSPEND_SET_ADV_FILTER,
117 	__SUSPEND_NUM_TASKS
118 };
119 
120 enum suspended_state {
121 	BT_RUNNING = 0,
122 	BT_SUSPEND_DISCONNECT,
123 	BT_SUSPEND_CONFIGURE_WAKE,
124 };
125 
126 struct hci_conn_hash {
127 	struct list_head list;
128 	unsigned int     acl_num;
129 	unsigned int     sco_num;
130 	unsigned int     cis_num;
131 	unsigned int     bis_num;
132 	unsigned int     pa_num;
133 	unsigned int     le_num;
134 	unsigned int     le_num_peripheral;
135 };
136 
137 struct bdaddr_list {
138 	struct list_head list;
139 	bdaddr_t bdaddr;
140 	u8 bdaddr_type;
141 };
142 
143 struct codec_list {
144 	struct list_head list;
145 	u8	id;
146 	__u16	cid;
147 	__u16	vid;
148 	u8	transport;
149 	u8	num_caps;
150 	u32	len;
151 	struct hci_codec_caps caps[];
152 };
153 
154 struct bdaddr_list_with_irk {
155 	struct list_head list;
156 	bdaddr_t bdaddr;
157 	u8 bdaddr_type;
158 	u8 peer_irk[16];
159 	u8 local_irk[16];
160 };
161 
162 /* Bitmask of connection flags */
163 enum hci_conn_flags {
164 	HCI_CONN_FLAG_REMOTE_WAKEUP = BIT(0),
165 	HCI_CONN_FLAG_DEVICE_PRIVACY = BIT(1),
166 	HCI_CONN_FLAG_ADDRESS_RESOLUTION = BIT(2),
167 	HCI_CONN_FLAG_PAST = BIT(3),
168 };
169 typedef u8 hci_conn_flags_t;
170 
171 struct bdaddr_list_with_flags {
172 	struct list_head list;
173 	bdaddr_t bdaddr;
174 	u8 bdaddr_type;
175 	hci_conn_flags_t flags;
176 };
177 
178 struct bt_uuid {
179 	struct list_head list;
180 	u8 uuid[16];
181 	u8 size;
182 	u8 svc_hint;
183 };
184 
185 struct blocked_key {
186 	struct list_head list;
187 	struct rcu_head rcu;
188 	u8 type;
189 	u8 val[16];
190 };
191 
192 struct smp_csrk {
193 	bdaddr_t bdaddr;
194 	u8 bdaddr_type;
195 	u8 type;
196 	u8 val[16];
197 };
198 
199 struct smp_ltk {
200 	struct list_head list;
201 	struct rcu_head rcu;
202 	bdaddr_t bdaddr;
203 	u8 bdaddr_type;
204 	u8 authenticated;
205 	u8 type;
206 	u8 enc_size;
207 	__le16 ediv;
208 	__le64 rand;
209 	u8 val[16];
210 };
211 
212 struct smp_irk {
213 	struct list_head list;
214 	struct rcu_head rcu;
215 	bdaddr_t rpa;
216 	bdaddr_t bdaddr;
217 	u8 addr_type;
218 	u8 val[16];
219 };
220 
221 struct link_key {
222 	struct list_head list;
223 	struct rcu_head rcu;
224 	bdaddr_t bdaddr;
225 	u8 type;
226 	u8 val[HCI_LINK_KEY_SIZE];
227 	u8 pin_len;
228 };
229 
230 struct oob_data {
231 	struct list_head list;
232 	bdaddr_t bdaddr;
233 	u8 bdaddr_type;
234 	u8 present;
235 	u8 hash192[16];
236 	u8 rand192[16];
237 	u8 hash256[16];
238 	u8 rand256[16];
239 };
240 
241 struct adv_info {
242 	struct list_head list;
243 	bool	enabled;
244 	bool	pending;
245 	bool	periodic;
246 	bool	periodic_enabled;
247 	__u8	mesh;
248 	__u8	instance;
249 	__u8	handle;
250 	__u8	sid;
251 	__u32	flags;
252 	__u16	timeout;
253 	__u16	remaining_time;
254 	__u16	duration;
255 	__u16	adv_data_len;
256 	__u8	adv_data[HCI_MAX_EXT_AD_LENGTH];
257 	bool	adv_data_changed;
258 	__u16	scan_rsp_len;
259 	__u8	scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
260 	bool	scan_rsp_changed;
261 	__u16	per_adv_data_len;
262 	__u8	per_adv_data[HCI_MAX_PER_AD_LENGTH];
263 	__s8	tx_power;
264 	__u32   min_interval;
265 	__u32   max_interval;
266 	bdaddr_t	random_addr;
267 	bool 		rpa_expired;
268 	struct delayed_work	rpa_expired_cb;
269 };
270 
271 struct tx_queue {
272 	struct sk_buff_head queue;
273 	unsigned int extra;
274 	unsigned int tracked;
275 };
276 
277 #define HCI_MAX_ADV_INSTANCES		5
278 #define HCI_DEFAULT_ADV_DURATION	2
279 
280 #define HCI_ADV_TX_POWER_NO_PREFERENCE 0x7F
281 
282 #define DATA_CMP(_d1, _l1, _d2, _l2) \
283 	(_l1 == _l2 ? memcmp(_d1, _d2, _l1) : _l1 - _l2)
284 
285 #define ADV_DATA_CMP(_adv, _data, _len) \
286 	DATA_CMP((_adv)->adv_data, (_adv)->adv_data_len, _data, _len)
287 
288 #define SCAN_RSP_CMP(_adv, _data, _len) \
289 	DATA_CMP((_adv)->scan_rsp_data, (_adv)->scan_rsp_len, _data, _len)
290 
291 struct monitored_device {
292 	struct list_head list;
293 
294 	bdaddr_t bdaddr;
295 	__u8     addr_type;
296 	__u16    handle;
297 	bool     notified;
298 };
299 
300 struct adv_pattern {
301 	struct list_head list;
302 	__u8 ad_type;
303 	__u8 offset;
304 	__u8 length;
305 	__u8 value[HCI_MAX_EXT_AD_LENGTH];
306 };
307 
308 struct adv_rssi_thresholds {
309 	__s8 low_threshold;
310 	__s8 high_threshold;
311 	__u16 low_threshold_timeout;
312 	__u16 high_threshold_timeout;
313 	__u8 sampling_period;
314 };
315 
316 struct adv_monitor {
317 	struct list_head patterns;
318 	struct adv_rssi_thresholds rssi;
319 	__u16		handle;
320 
321 	enum {
322 		ADV_MONITOR_STATE_NOT_REGISTERED,
323 		ADV_MONITOR_STATE_REGISTERED,
324 		ADV_MONITOR_STATE_OFFLOADED
325 	} state;
326 };
327 
328 #define HCI_MIN_ADV_MONITOR_HANDLE		1
329 #define HCI_MAX_ADV_MONITOR_NUM_HANDLES		32
330 #define HCI_MAX_ADV_MONITOR_NUM_PATTERNS	16
331 #define HCI_ADV_MONITOR_EXT_NONE		1
332 #define HCI_ADV_MONITOR_EXT_MSFT		2
333 
334 #define HCI_MAX_SHORT_NAME_LENGTH	10
335 
336 #define HCI_CONN_HANDLE_MAX		0x0eff
337 #define HCI_CONN_HANDLE_UNSET(_handle)	(_handle > HCI_CONN_HANDLE_MAX)
338 
339 /* Min encryption key size to match with SMP */
340 #define HCI_MIN_ENC_KEY_SIZE		7
341 
342 /* Default LE RPA expiry time, 15 minutes */
343 #define HCI_DEFAULT_RPA_TIMEOUT		(15 * 60)
344 
345 /* Default min/max age of connection information (1s/3s) */
346 #define DEFAULT_CONN_INFO_MIN_AGE	1000
347 #define DEFAULT_CONN_INFO_MAX_AGE	3000
348 /* Default authenticated payload timeout 30s */
349 #define DEFAULT_AUTH_PAYLOAD_TIMEOUT   0x0bb8
350 
351 #define HCI_MAX_PAGES	3
352 
353 struct hci_dev {
354 	struct list_head list;
355 	struct srcu_struct srcu;
356 	struct mutex	lock;
357 
358 	struct ida	unset_handle_ida;
359 
360 	const char	*name;
361 	unsigned long	flags;
362 	__u16		id;
363 	__u8		bus;
364 	bdaddr_t	bdaddr;
365 	bdaddr_t	setup_addr;
366 	bdaddr_t	public_addr;
367 	bdaddr_t	random_addr;
368 	bdaddr_t	static_addr;
369 	__u8		adv_addr_type;
370 	__u8		dev_name[HCI_MAX_NAME_LENGTH];
371 	__u8		short_name[HCI_MAX_SHORT_NAME_LENGTH];
372 	__u8		eir[HCI_MAX_EIR_LENGTH];
373 	__u16		appearance;
374 	__u8		dev_class[3];
375 	__u8		major_class;
376 	__u8		minor_class;
377 	__u8		max_page;
378 	__u8		features[HCI_MAX_PAGES][8];
379 	__u8		le_features[248];
380 	__u8		le_accept_list_size;
381 	__u8		le_resolv_list_size;
382 	__u8		le_num_of_adv_sets;
383 	__u8		le_states[8];
384 	__u8		mesh_ad_types[16];
385 	__u8		mesh_send_ref;
386 	__u8		commands[64];
387 	__u8		hci_ver;
388 	__u16		hci_rev;
389 	__u8		lmp_ver;
390 	__u16		manufacturer;
391 	__u16		lmp_subver;
392 	__u16		voice_setting;
393 	__u8		num_iac;
394 	__u16		stored_max_keys;
395 	__u16		stored_num_keys;
396 	__u8		io_capability;
397 	__s8		inq_tx_power;
398 	__u8		err_data_reporting;
399 	__u16		page_scan_interval;
400 	__u16		page_scan_window;
401 	__u8		page_scan_type;
402 	__u8		le_adv_channel_map;
403 	__u16		le_adv_min_interval;
404 	__u16		le_adv_max_interval;
405 	__u8		le_scan_type;
406 	__u16		le_scan_interval;
407 	__u16		le_scan_window;
408 	__u16		le_scan_int_suspend;
409 	__u16		le_scan_window_suspend;
410 	__u16		le_scan_int_discovery;
411 	__u16		le_scan_window_discovery;
412 	__u16		le_scan_int_adv_monitor;
413 	__u16		le_scan_window_adv_monitor;
414 	__u16		le_scan_int_connect;
415 	__u16		le_scan_window_connect;
416 	__u16		le_conn_min_interval;
417 	__u16		le_conn_max_interval;
418 	__u16		le_conn_latency;
419 	__u16		le_supv_timeout;
420 	__u16		le_min_rate_interval;
421 	__u16		le_def_tx_len;
422 	__u16		le_def_tx_time;
423 	__u16		le_max_tx_len;
424 	__u16		le_max_tx_time;
425 	__u16		le_max_rx_len;
426 	__u16		le_max_rx_time;
427 	__u8		le_max_key_size;
428 	__u8		le_min_key_size;
429 	__u16		discov_interleaved_timeout;
430 	__u16		conn_info_min_age;
431 	__u16		conn_info_max_age;
432 	__u16		auth_payload_timeout;
433 	__u8		min_enc_key_size;
434 	__u8		max_enc_key_size;
435 	__u8		pairing_opts;
436 	__u8		ssp_debug_mode;
437 	__u8		hw_error_code;
438 	__u32		clock;
439 	__u16		advmon_allowlist_duration;
440 	__u16		advmon_no_filter_duration;
441 	__u8		enable_advmon_interleave_scan;
442 
443 	__u16		devid_source;
444 	__u16		devid_vendor;
445 	__u16		devid_product;
446 	__u16		devid_version;
447 
448 	__u8		def_page_scan_type;
449 	__u16		def_page_scan_int;
450 	__u16		def_page_scan_window;
451 	__u8		def_inq_scan_type;
452 	__u16		def_inq_scan_int;
453 	__u16		def_inq_scan_window;
454 	__u16		def_br_lsto;
455 	__u16		def_page_timeout;
456 	__u16		def_multi_adv_rotation_duration;
457 	__u16		def_le_autoconnect_timeout;
458 	__s8		min_le_tx_power;
459 	__s8		max_le_tx_power;
460 
461 	__u16		pkt_type;
462 	__u16		esco_type;
463 	__u16		link_policy;
464 	__u16		link_mode;
465 
466 	__u32		idle_timeout;
467 	__u16		sniff_min_interval;
468 	__u16		sniff_max_interval;
469 
470 	unsigned int	auto_accept_delay;
471 
472 	DECLARE_BITMAP(quirk_flags, __HCI_NUM_QUIRKS);
473 
474 	atomic_t	cmd_cnt;
475 	unsigned int	acl_cnt;
476 	unsigned int	sco_cnt;
477 	unsigned int	le_cnt;
478 	unsigned int	iso_cnt;
479 
480 	unsigned int	acl_mtu;
481 	unsigned int	sco_mtu;
482 	unsigned int	le_mtu;
483 	unsigned int	iso_mtu;
484 	unsigned int	acl_pkts;
485 	unsigned int	sco_pkts;
486 	unsigned int	le_pkts;
487 	unsigned int	iso_pkts;
488 
489 	unsigned long	acl_last_tx;
490 	unsigned long	le_last_tx;
491 	unsigned long	iso_last_tx;
492 
493 	__u8		le_tx_def_phys;
494 	__u8		le_rx_def_phys;
495 
496 	struct workqueue_struct	*workqueue;
497 	struct workqueue_struct	*req_workqueue;
498 
499 	struct work_struct	power_on;
500 	struct delayed_work	power_off;
501 	struct work_struct	error_reset;
502 	struct work_struct	cmd_sync_work;
503 	struct list_head	cmd_sync_work_list;
504 	struct mutex		cmd_sync_work_lock;
505 	struct mutex		unregister_lock;
506 	struct work_struct	cmd_sync_cancel_work;
507 	struct work_struct	reenable_adv_work;
508 
509 	__u16			discov_timeout;
510 	struct delayed_work	discov_off;
511 
512 	struct delayed_work	service_cache;
513 
514 	struct delayed_work	cmd_timer;
515 	struct delayed_work	ncmd_timer;
516 
517 	struct work_struct	rx_work;
518 	struct work_struct	cmd_work;
519 	struct work_struct	tx_work;
520 
521 	struct delayed_work	le_scan_disable;
522 
523 	struct sk_buff_head	rx_q;
524 	struct sk_buff_head	raw_q;
525 	struct sk_buff_head	cmd_q;
526 
527 	struct sk_buff		*sent_cmd;
528 	struct sk_buff		*recv_event;
529 
530 	struct mutex		req_lock;
531 	wait_queue_head_t	req_wait_q;
532 	__u32			req_status;
533 	__u32			req_result;
534 	struct sk_buff		*req_skb;
535 	struct sk_buff		*req_rsp;
536 
537 	void			*smp_data;
538 	void			*smp_bredr_data;
539 
540 	struct discovery_state	discovery;
541 
542 	bool			discovery_paused;
543 	int			advertising_old_state;
544 	bool			advertising_paused;
545 
546 	struct notifier_block	suspend_notifier;
547 	enum suspended_state	suspend_state_next;
548 	enum suspended_state	suspend_state;
549 	bool			scanning_paused;
550 	bool			suspended;
551 	u8			wake_reason;
552 	bdaddr_t		wake_addr;
553 	u8			wake_addr_type;
554 
555 	struct hci_conn_hash	conn_hash;
556 
557 	struct list_head	mesh_pending;
558 	struct mutex		mgmt_pending_lock;
559 	struct list_head	mgmt_pending;
560 	struct list_head	reject_list;
561 	struct list_head	accept_list;
562 	struct list_head	uuids;
563 	struct list_head	link_keys;
564 	/* Serializes the writers of link_keys, see hci_add_link_key() */
565 	struct mutex		link_keys_lock;
566 	struct list_head	long_term_keys;
567 	struct list_head	identity_resolving_keys;
568 	struct mutex		remote_oob_lock;
569 	struct list_head	remote_oob_data;
570 	struct list_head	le_accept_list;
571 	struct list_head	le_resolv_list;
572 	struct list_head	le_conn_params;
573 	struct list_head	pend_le_conns;
574 	struct list_head	pend_le_reports;
575 	struct list_head	blocked_keys;
576 	struct list_head	local_codecs;
577 
578 	struct hci_dev_stats	stat;
579 
580 	atomic_t		promisc;
581 
582 	const char		*hw_info;
583 	const char		*fw_info;
584 	struct dentry		*debugfs;
585 
586 	struct hci_devcoredump	dump;
587 
588 	struct device		dev;
589 
590 	struct rfkill		*rfkill;
591 
592 	DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS);
593 	hci_conn_flags_t	conn_flags;
594 
595 	__s8			adv_tx_power;
596 	__u8			adv_data[HCI_MAX_EXT_AD_LENGTH];
597 	__u8			adv_data_len;
598 	__u8			scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
599 	__u8			scan_rsp_data_len;
600 	__u8			per_adv_data[HCI_MAX_PER_AD_LENGTH];
601 	__u8			per_adv_data_len;
602 
603 	struct list_head	adv_instances;
604 	unsigned int		adv_instance_cnt;
605 	__u8			cur_adv_instance;
606 	bool			adv_instance_timeout;
607 	struct delayed_work	adv_instance_expire;
608 
609 	struct idr		adv_monitors_idr;
610 	unsigned int		adv_monitors_cnt;
611 
612 	__u8			irk[16];
613 	__u32			rpa_timeout;
614 	struct delayed_work	rpa_expired;
615 	bdaddr_t		rpa;
616 
617 	struct delayed_work	mesh_send_done;
618 
619 	enum {
620 		INTERLEAVE_SCAN_NONE,
621 		INTERLEAVE_SCAN_NO_FILTER,
622 		INTERLEAVE_SCAN_ALLOWLIST
623 	} interleave_scan_state;
624 
625 	struct delayed_work	interleave_scan;
626 
627 	struct list_head	monitored_devices;
628 	bool			advmon_pend_notify;
629 
630 	struct hci_drv		*hci_drv;
631 
632 #if IS_ENABLED(CONFIG_BT_LEDS)
633 	struct led_trigger	*power_led;
634 #endif
635 
636 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
637 	__u16			msft_opcode;
638 	void			*msft_data;
639 	bool			msft_curve_validity;
640 #endif
641 
642 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
643 	bool			aosp_capable;
644 	bool			aosp_quality_report;
645 #endif
646 
647 	int (*open)(struct hci_dev *hdev);
648 	int (*close)(struct hci_dev *hdev);
649 	int (*flush)(struct hci_dev *hdev);
650 	int (*setup)(struct hci_dev *hdev);
651 	int (*shutdown)(struct hci_dev *hdev);
652 	int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
653 	/* Receive HCI_VENDOR_PKT */
654 	void (*recv_vendor_pkt)(struct hci_dev *hdev, struct sk_buff *skb);
655 	/* Handle HCI_EV_VENDOR; return true if handled, false otherwise */
656 	bool (*handle_ev_vendor)(struct hci_dev *hdev, struct sk_buff *skb);
657 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
658 	void (*hw_error)(struct hci_dev *hdev, u8 code);
659 	int (*post_init)(struct hci_dev *hdev);
660 	int (*set_diag)(struct hci_dev *hdev, bool enable);
661 	int (*set_bdaddr)(struct hci_dev *hdev, const bdaddr_t *bdaddr);
662 	void (*reset)(struct hci_dev *hdev);
663 	bool (*wakeup)(struct hci_dev *hdev);
664 	int (*set_quality_report)(struct hci_dev *hdev, bool enable);
665 	int (*get_data_path_id)(struct hci_dev *hdev, __u8 *data_path);
666 	int (*get_codec_config_data)(struct hci_dev *hdev, __u8 type,
667 				     struct bt_codec *codec, __u8 *vnd_len,
668 				     __u8 **vnd_data);
669 	u8 (*classify_pkt_type)(struct hci_dev *hdev, struct sk_buff *skb);
670 };
671 
672 #define hci_set_quirk(hdev, nr) set_bit((nr), (hdev)->quirk_flags)
673 #define hci_clear_quirk(hdev, nr) clear_bit((nr), (hdev)->quirk_flags)
674 #define hci_test_quirk(hdev, nr) test_bit((nr), (hdev)->quirk_flags)
675 
676 #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
677 
678 enum conn_reasons {
679 	CONN_REASON_PAIR_DEVICE,
680 	CONN_REASON_L2CAP_CHAN,
681 	CONN_REASON_SCO_CONNECT,
682 	CONN_REASON_ISO_CONNECT,
683 };
684 
685 enum {
686 	HCI_CONN_AUTH_PEND,
687 	HCI_CONN_ENCRYPT_PEND,
688 	HCI_CONN_RSWITCH_PEND,
689 	HCI_CONN_MODE_CHANGE_PEND,
690 	HCI_CONN_SCO_SETUP_PEND,
691 	HCI_CONN_MGMT_CONNECTED,
692 	HCI_CONN_SSP_ENABLED,
693 	HCI_CONN_SC_ENABLED,
694 	HCI_CONN_AES_CCM,
695 	HCI_CONN_POWER_SAVE,
696 	HCI_CONN_FLUSH_KEY,
697 	HCI_CONN_ENCRYPT,
698 	HCI_CONN_AUTH,
699 	HCI_CONN_SECURE,
700 	HCI_CONN_FIPS,
701 	HCI_CONN_STK_ENCRYPT,
702 	HCI_CONN_AUTH_INITIATOR,
703 	HCI_CONN_DROP,
704 	HCI_CONN_CANCEL,
705 	HCI_CONN_PARAM_REMOVAL_PEND,
706 	HCI_CONN_NEW_LINK_KEY,
707 	HCI_CONN_SCANNING,
708 	HCI_CONN_AUTH_FAILURE,
709 	HCI_CONN_PER_ADV,
710 	HCI_CONN_BIG_CREATED,
711 	HCI_CONN_CREATE,
712 	HCI_CONN_CREATE_CIS,
713 	HCI_CONN_CREATE_BIG_SYNC,
714 	HCI_CONN_BIG_SYNC,
715 	HCI_CONN_BIG_SYNC_FAILED,
716 	HCI_CONN_CREATE_PA_SYNC,
717 	HCI_CONN_PA_SYNC,
718 	HCI_CONN_PA_SYNC_FAILED,
719 	HCI_CONN_ENC_KEY_SIZE_PEND,
720 
721 	__HCI_CONN_NUM_FLAGS,
722 };
723 
724 struct hci_conn {
725 	struct list_head list;
726 
727 	atomic_t	refcnt;
728 
729 	bdaddr_t	dst;
730 	__u8		dst_type;
731 	bdaddr_t	src;
732 	__u8		src_type;
733 	bdaddr_t	init_addr;
734 	__u8		init_addr_type;
735 	bdaddr_t	resp_addr;
736 	__u8		resp_addr_type;
737 	__u8		adv_instance;
738 	__u16		handle;
739 	__u16		sync_handle;
740 	__u8		sid;
741 	__u16		state;
742 	__u16		mtu;
743 	__u8		mode;
744 	__u8		type;
745 	__u8		role;
746 	bool		out;
747 	__u8		attempt;
748 	__u8		dev_class[3];
749 	__u8		features[HCI_MAX_PAGES][8];
750 	__u8		le_features[248];
751 	__u16		pkt_type;
752 	__u16		link_policy;
753 	__u8		key_type;
754 	__u8		auth_type;
755 	__u8		sec_level;
756 	__u8		pending_sec_level;
757 	__u8		pin_length;
758 	__u8		enc_key_size;
759 	__u8		io_capability;
760 	__u32		passkey_notify;
761 	__u8		passkey_entered;
762 	__u16		disc_timeout;
763 	__u16		conn_timeout;
764 	__u16		setting;
765 	__u16		auth_payload_timeout;
766 	__u16		le_conn_min_interval;
767 	__u16		le_conn_max_interval;
768 	__u16		le_conn_interval;
769 	__u16		le_conn_latency;
770 	__u16		le_supv_timeout;
771 	__u16		le_rate_interval;
772 	__u16		le_subrate;
773 	__u16		le_rate_latency;
774 	__u16		le_cont_num;
775 	__u16		le_rate_supv_timeout;
776 	__u8		le_adv_data[HCI_MAX_EXT_AD_LENGTH];
777 	__u8		le_adv_data_len;
778 	__u8		le_per_adv_data[HCI_MAX_PER_AD_TOT_LEN];
779 	__u16		le_per_adv_data_len;
780 	__u16		le_per_adv_data_offset;
781 	__u8		le_adv_phy;
782 	__u8		le_adv_sec_phy;
783 	__u8		le_tx_def_phys;
784 	__u8		le_rx_def_phys;
785 	__u8		le_tx_phy;
786 	__u8		le_rx_phy;
787 	__s8		rssi;
788 	__s8		tx_power;
789 	__s8		max_tx_power;
790 	struct bt_iso_qos iso_qos;
791 	__u8		num_bis;
792 	__u8		bis[HCI_MAX_ISO_BIS];
793 
794 	DECLARE_BITMAP(flags, __HCI_CONN_NUM_FLAGS);
795 
796 	enum conn_reasons conn_reason;
797 	__u8		abort_reason;
798 
799 	__u32		clock;
800 	__u16		clock_accuracy;
801 
802 	unsigned long	conn_info_timestamp;
803 
804 	__u8		remote_cap;
805 	__u8		remote_auth;
806 
807 	unsigned int	sent;
808 
809 	struct sk_buff_head data_q;
810 	struct list_head chan_list;
811 
812 	struct tx_queue tx_q;
813 
814 	struct delayed_work disc_work;
815 	struct delayed_work auto_accept_work;
816 	struct delayed_work idle_work;
817 	struct delayed_work le_conn_timeout;
818 
819 	struct device	dev;
820 	struct dentry	*debugfs;
821 
822 	struct hci_dev	*hdev;
823 
824 	spinlock_t	proto_lock; /* lock guarding protocol data */
825 	void		*l2cap_data __guarded_by(&proto_lock, &hdev->lock);
826 	void		*sco_data;
827 	void		*iso_data __guarded_by(&proto_lock);
828 
829 	struct list_head link_list;
830 	struct hci_conn	*parent;
831 	struct hci_link *link;
832 
833 	struct bt_codec codec;
834 
835 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
836 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
837 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
838 
839 	void (*cleanup)(struct hci_conn *conn);
840 };
841 
842 struct hci_link {
843 	struct list_head list;
844 	struct hci_conn *conn;
845 };
846 
847 struct hci_chan {
848 	struct list_head list;
849 	__u16 handle;
850 	struct hci_conn *conn;
851 	struct sk_buff_head data_q;
852 	unsigned int	sent;
853 	__u8		state;
854 };
855 
856 struct hci_conn_params {
857 	struct list_head list;
858 	struct list_head action;
859 
860 	bdaddr_t addr;
861 	u8 addr_type;
862 
863 	u16 conn_min_interval;
864 	u16 conn_max_interval;
865 	u16 conn_latency;
866 	u16 supervision_timeout;
867 
868 	u16 rate_min_interval;
869 	u16 rate_max_interval;
870 	u16 subrate_min;
871 	u16 subrate_max;
872 	u16 max_latency;
873 	u16 cont_num;
874 	u16 rate_supv_timeout;
875 
876 	enum {
877 		HCI_AUTO_CONN_DISABLED,
878 		HCI_AUTO_CONN_REPORT,
879 		HCI_AUTO_CONN_DIRECT,
880 		HCI_AUTO_CONN_ALWAYS,
881 		HCI_AUTO_CONN_LINK_LOSS,
882 		HCI_AUTO_CONN_EXPLICIT,
883 	} auto_connect;
884 
885 	struct hci_conn *conn;
886 	bool explicit_connect;
887 	/* Accessed without hdev->lock: */
888 	hci_conn_flags_t flags;
889 	u8  privacy_mode;
890 };
891 
892 extern struct list_head hci_dev_list;
893 extern struct list_head hci_cb_list;
894 extern rwlock_t hci_dev_list_lock;
895 extern struct mutex hci_cb_list_lock;
896 
897 #define hci_dev_set_flag(hdev, nr)             set_bit((nr), (hdev)->dev_flags)
898 #define hci_dev_clear_flag(hdev, nr)           clear_bit((nr), (hdev)->dev_flags)
899 #define hci_dev_change_flag(hdev, nr)          change_bit((nr), (hdev)->dev_flags)
900 #define hci_dev_test_flag(hdev, nr)            test_bit((nr), (hdev)->dev_flags)
901 #define hci_dev_test_and_set_flag(hdev, nr)    test_and_set_bit((nr), (hdev)->dev_flags)
902 #define hci_dev_test_and_clear_flag(hdev, nr)  test_and_clear_bit((nr), (hdev)->dev_flags)
903 #define hci_dev_test_and_change_flag(hdev, nr) test_and_change_bit((nr), (hdev)->dev_flags)
904 
905 #define hci_dev_clear_volatile_flags(hdev)				\
906 	do {								\
907 		hci_dev_clear_flag((hdev), HCI_LE_SCAN);		\
908 		hci_dev_clear_flag((hdev), HCI_LE_ADV);			\
909 		hci_dev_clear_flag((hdev), HCI_LL_RPA_RESOLUTION);	\
910 		hci_dev_clear_flag((hdev), HCI_PERIODIC_INQ);		\
911 		hci_dev_clear_flag((hdev), HCI_QUALITY_REPORT);		\
912 	} while (0)
913 
914 #define hci_dev_le_state_simultaneous(hdev) \
915 	(!hci_test_quirk((hdev), HCI_QUIRK_BROKEN_LE_STATES) && \
916 	 ((hdev)->le_states[4] & 0x08) &&	/* Central */ \
917 	 ((hdev)->le_states[4] & 0x40) &&	/* Peripheral */ \
918 	 ((hdev)->le_states[3] & 0x10))		/* Simultaneous */
919 
920 /* ----- HCI interface to upper protocols ----- */
921 int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
922 int l2cap_disconn_ind(struct hci_conn *hcon);
923 int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, struct sk_buff *skb,
924 		       u16 flags);
925 
926 #if IS_ENABLED(CONFIG_BT_BREDR)
927 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
928 int sco_recv_scodata(struct hci_dev *hdev, u16 handle, struct sk_buff *skb);
929 #else
930 static inline int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
931 				  __u8 *flags)
932 {
933 	return 0;
934 }
935 
936 static inline int sco_recv_scodata(struct hci_dev *hdev, u16 handle,
937 				   struct sk_buff *skb)
938 {
939 	kfree_skb(skb);
940 	return -ENOENT;
941 }
942 #endif
943 
944 #if IS_ENABLED(CONFIG_BT_LE)
945 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
946 int iso_recv(struct hci_dev *hdev, u16 handle, struct sk_buff *skb,
947 	     u16 flags);
948 #else
949 static inline int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
950 				  __u8 *flags)
951 {
952 	return 0;
953 }
954 
955 static inline int iso_recv(struct hci_dev *hdev, u16 handle,
956 			   struct sk_buff *skb, u16 flags)
957 {
958 	kfree_skb(skb);
959 	return -ENOENT;
960 }
961 #endif
962 
963 /* ----- Inquiry cache ----- */
964 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
965 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
966 
967 static inline void discovery_init(struct hci_dev *hdev)
968 {
969 	spin_lock_init(&hdev->discovery.lock);
970 	hdev->discovery.state = DISCOVERY_STOPPED;
971 	INIT_LIST_HEAD(&hdev->discovery.all);
972 	INIT_LIST_HEAD(&hdev->discovery.unknown);
973 	INIT_LIST_HEAD(&hdev->discovery.resolve);
974 	hdev->discovery.report_invalid_rssi = true;
975 	hdev->discovery.rssi = HCI_RSSI_INVALID;
976 }
977 
978 static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
979 {
980 	hdev->discovery.result_filtering = false;
981 	hdev->discovery.report_invalid_rssi = true;
982 	hdev->discovery.rssi = HCI_RSSI_INVALID;
983 
984 	spin_lock(&hdev->discovery.lock);
985 	hdev->discovery.uuid_count = 0;
986 	kfree(hdev->discovery.uuids);
987 	hdev->discovery.uuids = NULL;
988 	spin_unlock(&hdev->discovery.lock);
989 }
990 
991 bool hci_discovery_active(struct hci_dev *hdev);
992 
993 void hci_discovery_set_state(struct hci_dev *hdev, int state);
994 
995 static inline int inquiry_cache_empty(struct hci_dev *hdev)
996 {
997 	return list_empty(&hdev->discovery.all);
998 }
999 
1000 static inline long inquiry_cache_age(struct hci_dev *hdev)
1001 {
1002 	struct discovery_state *c = &hdev->discovery;
1003 	return jiffies - c->timestamp;
1004 }
1005 
1006 static inline long inquiry_entry_age(struct inquiry_entry *e)
1007 {
1008 	return jiffies - e->timestamp;
1009 }
1010 
1011 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
1012 					       bdaddr_t *bdaddr);
1013 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
1014 						       bdaddr_t *bdaddr);
1015 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
1016 						       bdaddr_t *bdaddr,
1017 						       int state);
1018 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
1019 				      struct inquiry_entry *ie);
1020 u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
1021 			     bool name_known);
1022 void hci_inquiry_cache_flush(struct hci_dev *hdev);
1023 
1024 static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
1025 {
1026 	struct hci_dev *hdev = conn->hdev;
1027 	return hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
1028 	       test_bit(HCI_CONN_SSP_ENABLED, conn->flags);
1029 }
1030 
1031 static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
1032 {
1033 	struct hci_dev *hdev = conn->hdev;
1034 	return hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
1035 	       test_bit(HCI_CONN_SC_ENABLED, conn->flags);
1036 }
1037 
1038 static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
1039 {
1040 	struct hci_conn_hash *h = &hdev->conn_hash;
1041 
1042 	lockdep_assert_held(&hdev->lock);
1043 
1044 	list_add_tail_rcu(&c->list, &h->list);
1045 	switch (c->type) {
1046 	case ACL_LINK:
1047 		h->acl_num++;
1048 		break;
1049 	case LE_LINK:
1050 		h->le_num++;
1051 		if (c->role == HCI_ROLE_SLAVE)
1052 			h->le_num_peripheral++;
1053 		break;
1054 	case SCO_LINK:
1055 	case ESCO_LINK:
1056 		h->sco_num++;
1057 		break;
1058 	case CIS_LINK:
1059 		h->cis_num++;
1060 		break;
1061 	case BIS_LINK:
1062 		h->bis_num++;
1063 		break;
1064 	case PA_LINK:
1065 		h->pa_num++;
1066 		break;
1067 	}
1068 }
1069 
1070 static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
1071 {
1072 	struct hci_conn_hash *h = &hdev->conn_hash;
1073 
1074 	lockdep_assert_held(&hdev->lock);
1075 
1076 	list_del_rcu(&c->list);
1077 	synchronize_rcu();
1078 
1079 	switch (c->type) {
1080 	case ACL_LINK:
1081 		h->acl_num--;
1082 		break;
1083 	case LE_LINK:
1084 		h->le_num--;
1085 		if (c->role == HCI_ROLE_SLAVE)
1086 			h->le_num_peripheral--;
1087 		break;
1088 	case SCO_LINK:
1089 	case ESCO_LINK:
1090 		h->sco_num--;
1091 		break;
1092 	case CIS_LINK:
1093 		h->cis_num--;
1094 		break;
1095 	case BIS_LINK:
1096 		h->bis_num--;
1097 		break;
1098 	case PA_LINK:
1099 		h->pa_num--;
1100 		break;
1101 	}
1102 }
1103 
1104 #ifdef CONFIG_PROVE_RCU
1105 #define HCI_CONN_HASH_LOCKDEP_CHECK(hdev)				\
1106 	RCU_LOCKDEP_WARN(!lockdep_is_held(&(hdev)->lock) &&		\
1107 			 !rcu_read_lock_held(),				\
1108 			 "suspicious hci_conn locking")
1109 #else
1110 #define HCI_CONN_HASH_LOCKDEP_CHECK(hdev) do { } while (0 && (hdev))
1111 #endif
1112 
1113 static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
1114 {
1115 	struct hci_conn_hash *h = &hdev->conn_hash;
1116 	switch (type) {
1117 	case ACL_LINK:
1118 		return h->acl_num;
1119 	case LE_LINK:
1120 		return h->le_num;
1121 	case SCO_LINK:
1122 	case ESCO_LINK:
1123 		return h->sco_num;
1124 	case CIS_LINK:
1125 		return h->cis_num;
1126 	case BIS_LINK:
1127 		return h->bis_num;
1128 	case PA_LINK:
1129 		return h->pa_num;
1130 	default:
1131 		return 0;
1132 	}
1133 }
1134 
1135 static inline unsigned int hci_conn_count(struct hci_dev *hdev)
1136 {
1137 	struct hci_conn_hash *c = &hdev->conn_hash;
1138 
1139 	return c->acl_num + c->sco_num + c->le_num + c->cis_num + c->bis_num +
1140 		c->pa_num;
1141 }
1142 
1143 static inline unsigned int hci_iso_count(struct hci_dev *hdev)
1144 {
1145 	struct hci_conn_hash *c = &hdev->conn_hash;
1146 
1147 	return c->cis_num + c->bis_num;
1148 }
1149 
1150 static inline bool hci_conn_valid(struct hci_dev *hdev, struct hci_conn *conn)
1151 {
1152 	struct hci_conn_hash *h = &hdev->conn_hash;
1153 	struct hci_conn  *c;
1154 
1155 	rcu_read_lock();
1156 
1157 	list_for_each_entry_rcu(c, &h->list, list) {
1158 		if (c == conn) {
1159 			rcu_read_unlock();
1160 			return true;
1161 		}
1162 	}
1163 	rcu_read_unlock();
1164 
1165 	return false;
1166 }
1167 
1168 static inline __u8 hci_conn_lookup_type(struct hci_dev *hdev, __u16 handle)
1169 {
1170 	struct hci_conn_hash *h = &hdev->conn_hash;
1171 	struct hci_conn *c;
1172 	__u8 type = INVALID_LINK;
1173 
1174 	rcu_read_lock();
1175 
1176 	list_for_each_entry_rcu(c, &h->list, list) {
1177 		if (c->handle == handle) {
1178 			type = c->type;
1179 			break;
1180 		}
1181 	}
1182 
1183 	rcu_read_unlock();
1184 
1185 	return type;
1186 }
1187 
1188 static inline struct hci_conn *hci_conn_hash_lookup_bis(struct hci_dev *hdev,
1189 							bdaddr_t *ba, __u8 bis)
1190 {
1191 	struct hci_conn_hash *h = &hdev->conn_hash;
1192 	struct hci_conn  *c;
1193 
1194 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1195 
1196 	rcu_read_lock();
1197 
1198 	list_for_each_entry_rcu(c, &h->list, list) {
1199 		if (bacmp(&c->dst, ba) || c->type != BIS_LINK)
1200 			continue;
1201 
1202 		if (c->iso_qos.bcast.bis == bis) {
1203 			rcu_read_unlock();
1204 			return c;
1205 		}
1206 	}
1207 	rcu_read_unlock();
1208 
1209 	return NULL;
1210 }
1211 
1212 static inline struct hci_conn *
1213 hci_conn_hash_lookup_create_pa_sync(struct hci_dev *hdev)
1214 {
1215 	struct hci_conn_hash *h = &hdev->conn_hash;
1216 	struct hci_conn  *c;
1217 
1218 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1219 
1220 	rcu_read_lock();
1221 
1222 	list_for_each_entry_rcu(c, &h->list, list) {
1223 		if (c->type != PA_LINK)
1224 			continue;
1225 
1226 		if (!test_bit(HCI_CONN_CREATE_PA_SYNC, c->flags))
1227 			continue;
1228 
1229 		rcu_read_unlock();
1230 		return c;
1231 	}
1232 
1233 	rcu_read_unlock();
1234 
1235 	return NULL;
1236 }
1237 
1238 static inline struct hci_conn *
1239 hci_conn_hash_lookup_per_adv_bis(struct hci_dev *hdev,
1240 				 bdaddr_t *ba,
1241 				 __u8 big, __u8 bis)
1242 {
1243 	struct hci_conn_hash *h = &hdev->conn_hash;
1244 	struct hci_conn  *c;
1245 
1246 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1247 
1248 	rcu_read_lock();
1249 
1250 	list_for_each_entry_rcu(c, &h->list, list) {
1251 		if (bacmp(&c->dst, ba) || c->type != BIS_LINK ||
1252 		    !test_bit(HCI_CONN_PER_ADV, c->flags))
1253 			continue;
1254 
1255 		if (c->iso_qos.bcast.big == big &&
1256 		    c->iso_qos.bcast.bis == bis) {
1257 			rcu_read_unlock();
1258 			return c;
1259 		}
1260 	}
1261 	rcu_read_unlock();
1262 
1263 	return NULL;
1264 }
1265 
1266 static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
1267 								__u16 handle)
1268 {
1269 	struct hci_conn_hash *h = &hdev->conn_hash;
1270 	struct hci_conn  *c;
1271 
1272 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1273 
1274 	rcu_read_lock();
1275 
1276 	list_for_each_entry_rcu(c, &h->list, list) {
1277 		if (c->handle == handle) {
1278 			rcu_read_unlock();
1279 			return c;
1280 		}
1281 	}
1282 	rcu_read_unlock();
1283 
1284 	return NULL;
1285 }
1286 
1287 static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
1288 							__u8 type, bdaddr_t *ba)
1289 {
1290 	struct hci_conn_hash *h = &hdev->conn_hash;
1291 	struct hci_conn  *c;
1292 
1293 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1294 
1295 	rcu_read_lock();
1296 
1297 	list_for_each_entry_rcu(c, &h->list, list) {
1298 		if (c->type == type && !bacmp(&c->dst, ba)) {
1299 			rcu_read_unlock();
1300 			return c;
1301 		}
1302 	}
1303 
1304 	rcu_read_unlock();
1305 
1306 	return NULL;
1307 }
1308 
1309 static inline struct hci_conn *hci_conn_hash_lookup_role(struct hci_dev *hdev,
1310 							 __u8 type, __u8 role,
1311 							 bdaddr_t *ba)
1312 {
1313 	struct hci_conn_hash *h = &hdev->conn_hash;
1314 	struct hci_conn  *c;
1315 
1316 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1317 
1318 	rcu_read_lock();
1319 
1320 	list_for_each_entry_rcu(c, &h->list, list) {
1321 		if (c->type == type && c->role == role && !bacmp(&c->dst, ba)) {
1322 			rcu_read_unlock();
1323 			return c;
1324 		}
1325 	}
1326 
1327 	rcu_read_unlock();
1328 
1329 	return NULL;
1330 }
1331 
1332 static inline struct hci_conn *hci_conn_hash_lookup_le(struct hci_dev *hdev,
1333 						       bdaddr_t *ba,
1334 						       __u8 ba_type)
1335 {
1336 	struct hci_conn_hash *h = &hdev->conn_hash;
1337 	struct hci_conn  *c;
1338 
1339 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1340 
1341 	rcu_read_lock();
1342 
1343 	list_for_each_entry_rcu(c, &h->list, list) {
1344 		if (c->type != LE_LINK)
1345 		       continue;
1346 
1347 		if (ba_type == c->dst_type && !bacmp(&c->dst, ba)) {
1348 			rcu_read_unlock();
1349 			return c;
1350 		}
1351 	}
1352 
1353 	rcu_read_unlock();
1354 
1355 	return NULL;
1356 }
1357 
1358 static inline struct hci_conn *hci_conn_hash_lookup_cis(struct hci_dev *hdev,
1359 							bdaddr_t *ba,
1360 							__u8 ba_type,
1361 							__u8 cig,
1362 							__u8 id)
1363 {
1364 	struct hci_conn_hash *h = &hdev->conn_hash;
1365 	struct hci_conn  *c;
1366 
1367 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1368 
1369 	rcu_read_lock();
1370 
1371 	list_for_each_entry_rcu(c, &h->list, list) {
1372 		if (c->type != CIS_LINK)
1373 			continue;
1374 
1375 		/* Match CIG ID if set */
1376 		if (cig != c->iso_qos.ucast.cig)
1377 			continue;
1378 
1379 		/* Match CIS ID if set */
1380 		if (id != c->iso_qos.ucast.cis)
1381 			continue;
1382 
1383 		/* Match destination address if set */
1384 		if (!ba || (ba_type == c->dst_type && !bacmp(&c->dst, ba))) {
1385 			rcu_read_unlock();
1386 			return c;
1387 		}
1388 	}
1389 
1390 	rcu_read_unlock();
1391 
1392 	return NULL;
1393 }
1394 
1395 static inline struct hci_conn *hci_conn_hash_lookup_cig(struct hci_dev *hdev,
1396 							__u8 handle)
1397 {
1398 	struct hci_conn_hash *h = &hdev->conn_hash;
1399 	struct hci_conn  *c;
1400 
1401 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1402 
1403 	rcu_read_lock();
1404 
1405 	list_for_each_entry_rcu(c, &h->list, list) {
1406 		if (c->type != CIS_LINK)
1407 			continue;
1408 
1409 		if (handle == c->iso_qos.ucast.cig) {
1410 			rcu_read_unlock();
1411 			return c;
1412 		}
1413 	}
1414 
1415 	rcu_read_unlock();
1416 
1417 	return NULL;
1418 }
1419 
1420 static inline struct hci_conn *hci_conn_hash_lookup_big(struct hci_dev *hdev,
1421 							__u8 handle)
1422 {
1423 	struct hci_conn_hash *h = &hdev->conn_hash;
1424 	struct hci_conn  *c;
1425 
1426 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1427 
1428 	rcu_read_lock();
1429 
1430 	list_for_each_entry_rcu(c, &h->list, list) {
1431 		if (c->type != BIS_LINK)
1432 			continue;
1433 
1434 		if (handle == c->iso_qos.bcast.big) {
1435 			rcu_read_unlock();
1436 			return c;
1437 		}
1438 	}
1439 
1440 	rcu_read_unlock();
1441 
1442 	return NULL;
1443 }
1444 
1445 static inline struct hci_conn *
1446 hci_conn_hash_lookup_big_sync_pend(struct hci_dev *hdev,
1447 				   __u8 handle, __u8 num_bis)
1448 {
1449 	struct hci_conn_hash *h = &hdev->conn_hash;
1450 	struct hci_conn  *c;
1451 
1452 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1453 
1454 	rcu_read_lock();
1455 
1456 	list_for_each_entry_rcu(c, &h->list, list) {
1457 		if (c->type != PA_LINK)
1458 			continue;
1459 
1460 		if (handle == c->iso_qos.bcast.big && num_bis == c->num_bis) {
1461 			rcu_read_unlock();
1462 			return c;
1463 		}
1464 	}
1465 
1466 	rcu_read_unlock();
1467 
1468 	return NULL;
1469 }
1470 
1471 static inline struct hci_conn *
1472 hci_conn_hash_lookup_big_state(struct hci_dev *hdev, __u8 handle, __u16 state,
1473 			       __u8 role)
1474 {
1475 	struct hci_conn_hash *h = &hdev->conn_hash;
1476 	struct hci_conn  *c;
1477 
1478 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1479 
1480 	rcu_read_lock();
1481 
1482 	list_for_each_entry_rcu(c, &h->list, list) {
1483 		if (c->type != BIS_LINK || c->state != state || c->role != role)
1484 			continue;
1485 
1486 		if (handle == c->iso_qos.bcast.big) {
1487 			rcu_read_unlock();
1488 			return c;
1489 		}
1490 	}
1491 
1492 	rcu_read_unlock();
1493 
1494 	return NULL;
1495 }
1496 
1497 static inline struct hci_conn *
1498 hci_conn_hash_lookup_pa_sync_big_handle(struct hci_dev *hdev, __u8 big)
1499 {
1500 	struct hci_conn_hash *h = &hdev->conn_hash;
1501 	struct hci_conn  *c;
1502 
1503 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1504 
1505 	rcu_read_lock();
1506 
1507 	list_for_each_entry_rcu(c, &h->list, list) {
1508 		if (c->type != BIS_LINK ||
1509 		    !test_bit(HCI_CONN_PA_SYNC, c->flags))
1510 			continue;
1511 
1512 		if (c->iso_qos.bcast.big == big) {
1513 			rcu_read_unlock();
1514 			return c;
1515 		}
1516 	}
1517 	rcu_read_unlock();
1518 
1519 	return NULL;
1520 }
1521 
1522 static inline struct hci_conn *
1523 hci_conn_hash_lookup_pa_sync_handle(struct hci_dev *hdev, __u16 sync_handle)
1524 {
1525 	struct hci_conn_hash *h = &hdev->conn_hash;
1526 	struct hci_conn  *c;
1527 
1528 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1529 
1530 	rcu_read_lock();
1531 
1532 	list_for_each_entry_rcu(c, &h->list, list) {
1533 		if (c->type != PA_LINK)
1534 			continue;
1535 
1536 		/* Ignore the listen hcon, we are looking
1537 		 * for the child hcon that was created as
1538 		 * a result of the PA sync established event.
1539 		 */
1540 		if (c->state == BT_LISTEN)
1541 			continue;
1542 
1543 		if (c->sync_handle == sync_handle) {
1544 			rcu_read_unlock();
1545 			return c;
1546 		}
1547 	}
1548 	rcu_read_unlock();
1549 
1550 	return NULL;
1551 }
1552 
1553 typedef void (*hci_conn_func_t)(struct hci_conn *conn, void *data);
1554 static inline void hci_conn_hash_list_state(struct hci_dev *hdev,
1555 					    hci_conn_func_t func, __u8 type,
1556 					    __u16 state, void *data)
1557 {
1558 	struct hci_conn_hash *h = &hdev->conn_hash;
1559 	struct hci_conn  *c;
1560 
1561 	if (!func)
1562 		return;
1563 
1564 	rcu_read_lock();
1565 
1566 	list_for_each_entry_rcu(c, &h->list, list) {
1567 		if (c->type == type && c->state == state)
1568 			func(c, data);
1569 	}
1570 
1571 	rcu_read_unlock();
1572 }
1573 
1574 static inline void hci_conn_hash_list_flag(struct hci_dev *hdev,
1575 					    hci_conn_func_t func, __u8 type,
1576 					    __u8 flag, void *data)
1577 {
1578 	struct hci_conn_hash *h = &hdev->conn_hash;
1579 	struct hci_conn  *c;
1580 
1581 	if (!func)
1582 		return;
1583 
1584 	rcu_read_lock();
1585 
1586 	list_for_each_entry_rcu(c, &h->list, list) {
1587 		if (c->type == type && test_bit(flag, c->flags))
1588 			func(c, data);
1589 	}
1590 
1591 	rcu_read_unlock();
1592 }
1593 
1594 static inline struct hci_conn *hci_lookup_le_connect(struct hci_dev *hdev)
1595 {
1596 	struct hci_conn_hash *h = &hdev->conn_hash;
1597 	struct hci_conn  *c;
1598 
1599 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1600 
1601 	rcu_read_lock();
1602 
1603 	list_for_each_entry_rcu(c, &h->list, list) {
1604 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1605 		    !test_bit(HCI_CONN_SCANNING, c->flags)) {
1606 			rcu_read_unlock();
1607 			return c;
1608 		}
1609 	}
1610 
1611 	rcu_read_unlock();
1612 
1613 	return NULL;
1614 }
1615 
1616 /* Returns true if an le connection is in the scanning state */
1617 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1618 {
1619 	struct hci_conn_hash *h = &hdev->conn_hash;
1620 	struct hci_conn  *c;
1621 
1622 	rcu_read_lock();
1623 
1624 	list_for_each_entry_rcu(c, &h->list, list) {
1625 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1626 		    test_bit(HCI_CONN_SCANNING, c->flags)) {
1627 			rcu_read_unlock();
1628 			return true;
1629 		}
1630 	}
1631 
1632 	rcu_read_unlock();
1633 
1634 	return false;
1635 }
1636 
1637 int hci_disconnect(struct hci_conn *conn, __u8 reason);
1638 bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
1639 void hci_sco_setup(struct hci_conn *conn, __u8 status);
1640 bool hci_iso_setup_path(struct hci_conn *conn);
1641 int hci_le_create_cis_pending(struct hci_dev *hdev);
1642 int hci_conn_check_create_cis(struct hci_conn *conn);
1643 
1644 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1645 			      u8 dst_type, u8 role, u16 handle);
1646 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1647 				    bdaddr_t *dst, u8 dst_type, u8 role);
1648 void hci_conn_del(struct hci_conn *conn);
1649 void hci_conn_hash_flush(struct hci_dev *hdev);
1650 
1651 struct hci_chan *hci_chan_create(struct hci_conn *conn);
1652 void hci_chan_del(struct hci_chan *chan);
1653 void hci_chan_list_flush(struct hci_conn *conn);
1654 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
1655 
1656 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1657 				     u8 dst_type, u8 sec_level,
1658 				     u16 conn_timeout,
1659 				     enum conn_reasons conn_reason);
1660 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1661 				u8 dst_type, bool dst_resolved, u8 sec_level,
1662 				u16 conn_timeout, u8 role, u8 phy, u8 sec_phy);
1663 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status);
1664 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1665 				 u8 sec_level, u8 auth_type,
1666 				 enum conn_reasons conn_reason, u16 timeout);
1667 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1668 				 __u16 setting, struct bt_codec *codec,
1669 				 u16 timeout);
1670 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1671 			      __u8 dst_type, struct bt_iso_qos *qos,
1672 			      u16 timeout);
1673 struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid,
1674 			      struct bt_iso_qos *qos,
1675 			      __u8 base_len, __u8 *base, u16 timeout);
1676 int hci_past_bis(struct hci_conn *conn, bdaddr_t *dst, __u8 dst_type);
1677 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
1678 				 __u8 dst_type, struct bt_iso_qos *qos,
1679 				 u16 timeout);
1680 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
1681 				 __u8 dst_type, __u8 sid,
1682 				 struct bt_iso_qos *qos,
1683 				 __u8 data_len, __u8 *data, u16 timeout);
1684 struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
1685 		       __u8 dst_type, __u8 sid, struct bt_iso_qos *qos);
1686 int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
1687 			     struct bt_iso_qos *qos, __u16 sync_handle,
1688 			     __u8 num_bis, __u8 bis[]);
1689 int hci_conn_check_link_mode(struct hci_conn *conn);
1690 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
1691 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1692 		      bool initiator);
1693 int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
1694 
1695 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
1696 
1697 void hci_conn_failed(struct hci_conn *conn, u8 status);
1698 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle);
1699 
1700 void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb);
1701 void hci_conn_tx_dequeue(struct hci_conn *conn);
1702 void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
1703 			    const struct sockcm_cookie *sockc);
1704 
1705 static inline void hci_sockcm_init(struct sockcm_cookie *sockc, struct sock *sk)
1706 {
1707 	*sockc = (struct sockcm_cookie) {
1708 		.tsflags = READ_ONCE(sk->sk_tsflags),
1709 	};
1710 }
1711 
1712 /*
1713  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
1714  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
1715  * working or anything else. They just guarantee that the object is available
1716  * and can be dereferenced. So you can use its locks, local variables and any
1717  * other constant data.
1718  * Before accessing runtime data, you _must_ lock the object and then check that
1719  * it is still running. As soon as you release the locks, the connection might
1720  * get dropped, though.
1721  *
1722  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
1723  * how long the underlying connection is held. So every channel that runs on the
1724  * hci_conn object calls this to prevent the connection from disappearing. As
1725  * long as you hold a device, you must also guarantee that you have a valid
1726  * reference to the device via hci_conn_get() (or the initial reference from
1727  * hci_conn_add()).
1728  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
1729  * break because nobody cares for that. But this means, we cannot use
1730  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
1731  */
1732 
1733 static inline struct hci_conn *hci_conn_get(struct hci_conn *conn)
1734 {
1735 	get_device(&conn->dev);
1736 	return conn;
1737 }
1738 
1739 static inline void hci_conn_put(struct hci_conn *conn)
1740 {
1741 	put_device(&conn->dev);
1742 }
1743 
1744 static inline struct hci_conn *hci_conn_hold(struct hci_conn *conn)
1745 {
1746 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1747 
1748 	atomic_inc(&conn->refcnt);
1749 	cancel_delayed_work(&conn->disc_work);
1750 
1751 	return conn;
1752 }
1753 
1754 static inline void hci_conn_drop(struct hci_conn *conn)
1755 {
1756 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1757 
1758 	if (atomic_dec_and_test(&conn->refcnt)) {
1759 		unsigned long timeo;
1760 
1761 		switch (conn->type) {
1762 		case ACL_LINK:
1763 		case LE_LINK:
1764 			cancel_delayed_work(&conn->idle_work);
1765 			if (conn->state == BT_CONNECTED) {
1766 				timeo = conn->disc_timeout;
1767 				if (!conn->out)
1768 					timeo *= 2;
1769 			} else {
1770 				timeo = 0;
1771 			}
1772 			break;
1773 
1774 		default:
1775 			timeo = 0;
1776 			break;
1777 		}
1778 
1779 		cancel_delayed_work(&conn->disc_work);
1780 		queue_delayed_work(conn->hdev->workqueue,
1781 				   &conn->disc_work, timeo);
1782 	}
1783 }
1784 
1785 /* ----- HCI Devices ----- */
1786 static inline void hci_dev_put(struct hci_dev *d)
1787 {
1788 	BT_DBG("%s orig refcnt %d", d->name,
1789 	       kref_read(&d->dev.kobj.kref));
1790 
1791 	put_device(&d->dev);
1792 }
1793 
1794 static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
1795 {
1796 	BT_DBG("%s orig refcnt %d", d->name,
1797 	       kref_read(&d->dev.kobj.kref));
1798 
1799 	get_device(&d->dev);
1800 	return d;
1801 }
1802 
1803 #define hci_dev_lock(d)		mutex_lock(&d->lock)
1804 #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
1805 
1806 #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
1807 #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
1808 
1809 static inline void *hci_get_drvdata(struct hci_dev *hdev)
1810 {
1811 	return dev_get_drvdata(&hdev->dev);
1812 }
1813 
1814 static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
1815 {
1816 	dev_set_drvdata(&hdev->dev, data);
1817 }
1818 
1819 static inline void *hci_get_priv(struct hci_dev *hdev)
1820 {
1821 	return (char *)hdev + sizeof(*hdev);
1822 }
1823 
1824 struct hci_dev *hci_dev_get(int index);
1825 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, u8 src_type);
1826 
1827 struct hci_dev *hci_alloc_dev_priv(int sizeof_priv);
1828 
1829 static inline struct hci_dev *hci_alloc_dev(void)
1830 {
1831 	return hci_alloc_dev_priv(0);
1832 }
1833 
1834 void hci_free_dev(struct hci_dev *hdev);
1835 int hci_register_dev(struct hci_dev *hdev);
1836 void hci_unregister_dev(struct hci_dev *hdev);
1837 void hci_release_dev(struct hci_dev *hdev);
1838 int hci_register_suspend_notifier(struct hci_dev *hdev);
1839 int hci_unregister_suspend_notifier(struct hci_dev *hdev);
1840 int hci_suspend_dev(struct hci_dev *hdev);
1841 int hci_resume_dev(struct hci_dev *hdev);
1842 int __hci_reset_dev(struct hci_dev *hdev, u8 hw_err_code);
1843 
1844 static inline int hci_reset_dev(struct hci_dev *hdev)
1845 {
1846 	return __hci_reset_dev(hdev, 0);
1847 }
1848 
1849 int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
1850 int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb);
1851 __printf(2, 3) void hci_set_hw_info(struct hci_dev *hdev, const char *fmt, ...);
1852 __printf(2, 3) void hci_set_fw_info(struct hci_dev *hdev, const char *fmt, ...);
1853 
1854 static inline void hci_set_msft_opcode(struct hci_dev *hdev, __u16 opcode)
1855 {
1856 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
1857 	hdev->msft_opcode = opcode;
1858 #endif
1859 }
1860 
1861 static inline void hci_set_aosp_capable(struct hci_dev *hdev)
1862 {
1863 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
1864 	hdev->aosp_capable = true;
1865 #endif
1866 }
1867 
1868 static inline void hci_devcd_setup(struct hci_dev *hdev)
1869 {
1870 #ifdef CONFIG_DEV_COREDUMP
1871 	INIT_WORK(&hdev->dump.dump_rx, hci_devcd_rx);
1872 	INIT_DELAYED_WORK(&hdev->dump.dump_timeout, hci_devcd_timeout);
1873 	skb_queue_head_init(&hdev->dump.dump_q);
1874 #endif
1875 }
1876 
1877 int hci_dev_open(__u16 dev);
1878 int hci_dev_close(__u16 dev);
1879 int hci_dev_do_close(struct hci_dev *hdev);
1880 int hci_dev_reset(__u16 dev);
1881 int hci_dev_reset_stat(__u16 dev);
1882 int hci_dev_cmd(unsigned int cmd, void __user *arg);
1883 int hci_get_dev_list(void __user *arg);
1884 int hci_get_dev_info(void __user *arg);
1885 int hci_get_conn_list(void __user *arg);
1886 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
1887 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
1888 int hci_inquiry(void __user *arg);
1889 
1890 struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *list,
1891 					   bdaddr_t *bdaddr, u8 type);
1892 struct bdaddr_list_with_irk *hci_bdaddr_list_lookup_with_irk(
1893 				    struct list_head *list, bdaddr_t *bdaddr,
1894 				    u8 type);
1895 struct bdaddr_list_with_flags *
1896 hci_bdaddr_list_lookup_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1897 				  u8 type);
1898 int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1899 int hci_bdaddr_list_add_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1900 				 u8 type, u8 *peer_irk, u8 *local_irk);
1901 int hci_bdaddr_list_add_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1902 				   u8 type, u32 flags);
1903 int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1904 int hci_bdaddr_list_del_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1905 				 u8 type);
1906 void hci_bdaddr_list_clear(struct list_head *list);
1907 
1908 struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
1909 					       bdaddr_t *addr, u8 addr_type);
1910 struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
1911 					    bdaddr_t *addr, u8 addr_type);
1912 void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
1913 void hci_conn_params_clear_disabled(struct hci_dev *hdev);
1914 void hci_conn_params_free(struct hci_conn_params *param);
1915 
1916 void hci_pend_le_list_del_init(struct hci_conn_params *param);
1917 void hci_pend_le_list_add(struct hci_conn_params *param,
1918 			  struct list_head *list);
1919 struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
1920 						  bdaddr_t *addr,
1921 						  u8 addr_type);
1922 
1923 void hci_uuids_clear(struct hci_dev *hdev);
1924 
1925 void hci_link_keys_clear(struct hci_dev *hdev);
1926 struct link_key *__hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
1927 				    bdaddr_t *bdaddr, u8 *val, u8 type,
1928 				    u8 pin_len, bool *persistent);
1929 void hci_del_link_key(struct hci_dev *hdev, struct link_key *key);
1930 u8 *hci_conn_key_enc_size(struct hci_conn *conn);
1931 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1932 struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
1933 				  bdaddr_t *bdaddr, u8 *val, u8 type,
1934 				  u8 pin_len, bool *persistent);
1935 struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1936 			    u8 addr_type, u8 type, u8 authenticated,
1937 			    u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
1938 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1939 			     u8 addr_type, u8 role);
1940 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
1941 void hci_smp_ltks_clear(struct hci_dev *hdev);
1942 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1943 
1944 struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
1945 struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1946 				     u8 addr_type);
1947 struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1948 			    u8 addr_type, u8 val[16], bdaddr_t *rpa);
1949 void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
1950 bool hci_is_blocked_key(struct hci_dev *hdev, u8 type, u8 val[16]);
1951 void hci_blocked_keys_clear(struct hci_dev *hdev);
1952 void hci_smp_irks_clear(struct hci_dev *hdev);
1953 
1954 bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1955 
1956 void hci_remote_oob_data_clear(struct hci_dev *hdev);
1957 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1958 					  bdaddr_t *bdaddr, u8 bdaddr_type);
1959 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1960 			    u8 bdaddr_type, u8 *hash192, u8 *rand192,
1961 			    u8 *hash256, u8 *rand256);
1962 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1963 			       u8 bdaddr_type);
1964 
1965 void hci_adv_instances_clear(struct hci_dev *hdev);
1966 struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance);
1967 struct adv_info *hci_find_adv_sid(struct hci_dev *hdev, u8 sid);
1968 struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance);
1969 struct adv_info *hci_add_adv_instance(struct hci_dev *hdev, u8 instance,
1970 				      u32 flags, u16 adv_data_len, u8 *adv_data,
1971 				      u16 scan_rsp_len, u8 *scan_rsp_data,
1972 				      u16 timeout, u16 duration, s8 tx_power,
1973 				      u32 min_interval, u32 max_interval,
1974 				      u8 mesh_handle);
1975 struct adv_info *hci_add_per_instance(struct hci_dev *hdev, u8 instance, u8 sid,
1976 				      u32 flags, u8 data_len, u8 *data,
1977 				      u32 min_interval, u32 max_interval);
1978 int hci_set_adv_instance_data(struct hci_dev *hdev, u8 instance,
1979 			 u16 adv_data_len, u8 *adv_data,
1980 			 u16 scan_rsp_len, u8 *scan_rsp_data);
1981 void hci_free_adv_instance(struct hci_dev *hdev, struct adv_info *adv_instance);
1982 int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
1983 void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired);
1984 u32 hci_adv_instance_flags(struct hci_dev *hdev, u8 instance);
1985 bool hci_adv_instance_is_scannable(struct hci_dev *hdev, u8 instance);
1986 
1987 void hci_adv_monitors_clear(struct hci_dev *hdev);
1988 void hci_free_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1989 int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1990 int hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle);
1991 int hci_remove_all_adv_monitor(struct hci_dev *hdev);
1992 bool hci_is_adv_monitoring(struct hci_dev *hdev);
1993 int hci_get_adv_monitor_offload_ext(struct hci_dev *hdev);
1994 
1995 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
1996 
1997 void hci_init_sysfs(struct hci_dev *hdev);
1998 void hci_conn_init_sysfs(struct hci_conn *conn);
1999 void hci_conn_add_sysfs(struct hci_conn *conn);
2000 void hci_conn_del_sysfs(struct hci_conn *conn);
2001 
2002 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
2003 #define GET_HCIDEV_DEV(hdev) ((hdev)->dev.parent)
2004 
2005 /* ----- LMP capabilities ----- */
2006 #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
2007 #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
2008 #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
2009 #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
2010 #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
2011 #define lmp_sco_capable(dev)       ((dev)->features[0][1] & LMP_SCO)
2012 #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
2013 #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
2014 #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
2015 #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
2016 #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
2017 #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
2018 #define lmp_esco_2m_capable(dev)   ((dev)->features[0][5] & LMP_EDR_ESCO_2M)
2019 #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
2020 #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
2021 #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
2022 #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
2023 #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
2024 #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
2025 #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
2026 #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
2027 #define lmp_edr_2m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_2M)
2028 #define lmp_edr_3m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_3M)
2029 #define lmp_edr_3slot_capable(dev) ((dev)->features[0][4] & LMP_EDR_3SLOT)
2030 #define lmp_edr_5slot_capable(dev) ((dev)->features[0][5] & LMP_EDR_5SLOT)
2031 
2032 /* ----- Extended LMP capabilities ----- */
2033 #define lmp_cpb_central_capable(dev) ((dev)->features[2][0] & LMP_CPB_CENTRAL)
2034 #define lmp_cpb_peripheral_capable(dev) ((dev)->features[2][0] & LMP_CPB_PERIPHERAL)
2035 #define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
2036 #define lmp_sync_scan_capable(dev)  ((dev)->features[2][0] & LMP_SYNC_SCAN)
2037 #define lmp_sc_capable(dev)         ((dev)->features[2][1] & LMP_SC)
2038 #define lmp_ping_capable(dev)       ((dev)->features[2][1] & LMP_PING)
2039 
2040 /* ----- Host capabilities ----- */
2041 #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
2042 #define lmp_host_sc_capable(dev)   ((dev)->features[1][0] & LMP_HOST_SC)
2043 #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
2044 #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
2045 
2046 #define hdev_is_powered(dev)   (test_bit(HCI_UP, &(dev)->flags) && \
2047 				!hci_dev_test_flag(dev, HCI_AUTO_OFF))
2048 #define bredr_sc_enabled(dev)  (lmp_sc_capable(dev) && \
2049 				hci_dev_test_flag(dev, HCI_SC_ENABLED))
2050 #define rpa_valid(dev)         (bacmp(&dev->rpa, BDADDR_ANY) && \
2051 				!hci_dev_test_flag(dev, HCI_RPA_EXPIRED))
2052 #define adv_rpa_valid(adv)     (bacmp(&adv->random_addr, BDADDR_ANY) && \
2053 				!adv->rpa_expired)
2054 #define le_enabled(dev)        (lmp_le_capable(dev) && \
2055 				hci_dev_test_flag(dev, HCI_LE_ENABLED))
2056 
2057 #define scan_1m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_1M) || \
2058 		      ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_1M))
2059 
2060 #define le_2m_capable(dev) (((dev)->le_features[1] & HCI_LE_PHY_2M))
2061 
2062 #define scan_2m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_2M) || \
2063 		      ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_2M))
2064 
2065 #define le_coded_capable(dev) (((dev)->le_features[1] & HCI_LE_PHY_CODED) && \
2066 			       !hci_test_quirk((dev), \
2067 					       HCI_QUIRK_BROKEN_LE_CODED))
2068 
2069 #define scan_coded(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_CODED) || \
2070 			 ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_CODED))
2071 
2072 #define ll_privacy_capable(dev) ((dev)->le_features[0] & HCI_LE_LL_PRIVACY)
2073 #define ll_privacy_enabled(dev) (le_enabled(dev) && ll_privacy_capable(dev))
2074 
2075 #define privacy_mode_capable(dev) (ll_privacy_capable(dev) && \
2076 				   ((dev)->commands[39] & 0x04))
2077 
2078 #define read_key_size_capable(dev) \
2079 	((dev)->commands[20] & 0x10 && \
2080 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE))
2081 
2082 #define read_voice_setting_capable(dev) \
2083 	((dev)->commands[9] & 0x04 && \
2084 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_READ_VOICE_SETTING))
2085 
2086 /* Use enhanced synchronous connection if command is supported and its quirk
2087  * has not been set.
2088  */
2089 #define enhanced_sync_conn_capable(dev) \
2090 	(((dev)->commands[29] & 0x08) && \
2091 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN))
2092 
2093 /* Use ext scanning if set ext scan param and ext scan enable is supported */
2094 #define use_ext_scan(dev) (((dev)->commands[37] & 0x20) && \
2095 			   ((dev)->commands[37] & 0x40) && \
2096 			   !hci_test_quirk((dev), HCI_QUIRK_BROKEN_EXT_SCAN))
2097 
2098 /* Use ext create connection if command is supported */
2099 #define use_ext_conn(dev) (((dev)->commands[37] & 0x80) && \
2100 	!hci_test_quirk((dev), HCI_QUIRK_BROKEN_EXT_CREATE_CONN))
2101 /* Extended advertising support */
2102 #define ext_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_EXT_ADV))
2103 
2104 /* Maximum advertising length */
2105 #define max_adv_len(dev) \
2106 	(ext_adv_capable(dev) ? HCI_MAX_EXT_AD_LENGTH : HCI_MAX_AD_LENGTH)
2107 
2108 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 1789:
2109  *
2110  * C24: Mandatory if the LE Controller supports Connection State and either
2111  * LE Feature (LL Privacy) or LE Feature (Extended Advertising) is supported
2112  */
2113 #define use_enhanced_conn_complete(dev) ((ll_privacy_capable(dev) || \
2114 					 ext_adv_capable(dev)) && \
2115 					 !hci_test_quirk((dev), \
2116 							 HCI_QUIRK_BROKEN_EXT_CREATE_CONN))
2117 
2118 /* Periodic advertising support */
2119 #define per_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_PERIODIC_ADV))
2120 
2121 /* CIS Master/Slave and BIS support */
2122 #define iso_capable(dev) (cis_capable(dev) || bis_capable(dev))
2123 #define iso_enabled(dev) (le_enabled(dev) && iso_capable(dev))
2124 #define cis_capable(dev) \
2125 	(cis_central_capable(dev) || cis_peripheral_capable(dev))
2126 #define cis_enabled(dev) (le_enabled(dev) && cis_capable(dev))
2127 #define cis_central_capable(dev) \
2128 	((dev)->le_features[3] & HCI_LE_CIS_CENTRAL)
2129 #define cis_central_enabled(dev) \
2130 	(le_enabled(dev) && cis_central_capable(dev))
2131 #define cis_peripheral_capable(dev) \
2132 	((dev)->le_features[3] & HCI_LE_CIS_PERIPHERAL)
2133 #define cis_peripheral_enabled(dev) \
2134 	(le_enabled(dev) && cis_peripheral_capable(dev))
2135 #define bis_capable(dev) ((dev)->le_features[3] & HCI_LE_ISO_BROADCASTER)
2136 #define bis_enabled(dev) (le_enabled(dev) && bis_capable(dev))
2137 #define sync_recv_capable(dev) \
2138 	((dev)->le_features[3] & HCI_LE_ISO_SYNC_RECEIVER)
2139 #define sync_recv_enabled(dev) (le_enabled(dev) && sync_recv_capable(dev))
2140 #define past_sender_capable(dev) \
2141 	((dev)->le_features[3] & HCI_LE_PAST_SENDER)
2142 #define past_receiver_capable(dev) \
2143 	((dev)->le_features[3] & HCI_LE_PAST_RECEIVER)
2144 #define past_capable(dev) \
2145 	(past_sender_capable(dev) || past_receiver_capable(dev))
2146 #define past_sender_enabled(dev) \
2147 	(le_enabled(dev) && past_sender_capable(dev))
2148 #define past_receiver_enabled(dev) \
2149 	(le_enabled(dev) && past_receiver_capable(dev))
2150 #define past_enabled(dev) \
2151 	(past_sender_enabled(dev) || past_receiver_enabled(dev))
2152 #define ll_ext_feature_capable(dev) \
2153 	((dev)->le_features[7] & HCI_LE_LL_EXT_FEATURE)
2154 
2155 /* Channel sounding support */
2156 #define le_cs_capable(dev) \
2157 	((dev)->le_features[5] & HCI_LE_CS)
2158 #define le_cs_host_capable(dev) \
2159 	((dev)->le_features[5] & HCI_LE_CS_HOST)
2160 
2161 #define le_sci_capable(dev) \
2162 	((dev)->le_features[9] & HCI_LE_SCI)
2163 #define le_sci_enabled(dev) \
2164 	(le_enabled(dev) && le_sci_capable(dev))
2165 
2166 #define mws_transport_config_capable(dev) (((dev)->commands[30] & 0x08) && \
2167 	(!hci_test_quirk((dev), HCI_QUIRK_BROKEN_MWS_TRANSPORT_CONFIG)))
2168 
2169 /* ----- HCI protocols ----- */
2170 #define HCI_PROTO_DEFER             0x01
2171 
2172 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
2173 					__u8 type, __u8 *flags)
2174 {
2175 	switch (type) {
2176 	case ACL_LINK:
2177 		return l2cap_connect_ind(hdev, bdaddr);
2178 
2179 	case SCO_LINK:
2180 	case ESCO_LINK:
2181 		return sco_connect_ind(hdev, bdaddr, flags);
2182 
2183 	case CIS_LINK:
2184 	case BIS_LINK:
2185 	case PA_LINK:
2186 		return iso_connect_ind(hdev, bdaddr, flags);
2187 
2188 	default:
2189 		BT_ERR("unknown link type %d", type);
2190 		return -EINVAL;
2191 	}
2192 }
2193 
2194 static inline int hci_proto_disconn_ind(struct hci_conn *conn)
2195 {
2196 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
2197 		return HCI_ERROR_REMOTE_USER_TERM;
2198 
2199 	return l2cap_disconn_ind(conn);
2200 }
2201 
2202 /* ----- HCI callbacks ----- */
2203 struct hci_cb {
2204 	struct list_head list;
2205 
2206 	char *name;
2207 
2208 	void (*connect_cfm)	(struct hci_conn *conn, __u8 status);
2209 	void (*disconn_cfm)	(struct hci_conn *conn, __u8 status);
2210 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
2211 								__u8 encrypt);
2212 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
2213 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
2214 };
2215 
2216 static inline void hci_connect_cfm(struct hci_conn *conn, __u8 status)
2217 {
2218 	struct hci_cb *cb;
2219 
2220 	mutex_lock(&hci_cb_list_lock);
2221 	list_for_each_entry(cb, &hci_cb_list, list) {
2222 		if (cb->connect_cfm)
2223 			cb->connect_cfm(conn, status);
2224 	}
2225 	mutex_unlock(&hci_cb_list_lock);
2226 
2227 	if (conn->connect_cfm_cb)
2228 		conn->connect_cfm_cb(conn, status);
2229 }
2230 
2231 static inline void hci_disconn_cfm(struct hci_conn *conn, __u8 reason)
2232 {
2233 	struct hci_cb *cb;
2234 
2235 	mutex_lock(&hci_cb_list_lock);
2236 	list_for_each_entry(cb, &hci_cb_list, list) {
2237 		if (cb->disconn_cfm)
2238 			cb->disconn_cfm(conn, reason);
2239 	}
2240 	mutex_unlock(&hci_cb_list_lock);
2241 
2242 	if (conn->disconn_cfm_cb)
2243 		conn->disconn_cfm_cb(conn, reason);
2244 }
2245 
2246 static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
2247 {
2248 	struct hci_cb *cb;
2249 	__u8 encrypt;
2250 
2251 	if (test_bit(HCI_CONN_ENCRYPT_PEND, conn->flags))
2252 		return;
2253 
2254 	encrypt = test_bit(HCI_CONN_ENCRYPT, conn->flags) ? 0x01 : 0x00;
2255 
2256 	mutex_lock(&hci_cb_list_lock);
2257 	list_for_each_entry(cb, &hci_cb_list, list) {
2258 		if (cb->security_cfm)
2259 			cb->security_cfm(conn, status, encrypt);
2260 	}
2261 	mutex_unlock(&hci_cb_list_lock);
2262 
2263 	if (conn->security_cfm_cb)
2264 		conn->security_cfm_cb(conn, status);
2265 }
2266 
2267 void mgmt_security_level_changed(struct hci_conn *conn);
2268 
2269 static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status)
2270 {
2271 	struct hci_cb *cb;
2272 	__u8 encrypt;
2273 
2274 	if (conn->state == BT_CONFIG) {
2275 		if (!status)
2276 			conn->state = BT_CONNECTED;
2277 
2278 		hci_connect_cfm(conn, status);
2279 		hci_conn_drop(conn);
2280 		return;
2281 	}
2282 
2283 	if (!test_bit(HCI_CONN_ENCRYPT, conn->flags))
2284 		encrypt = 0x00;
2285 	else if (test_bit(HCI_CONN_AES_CCM, conn->flags))
2286 		encrypt = 0x02;
2287 	else
2288 		encrypt = 0x01;
2289 
2290 	if (!status) {
2291 		/* Callers either set conn->sec_level = conn->pending_sec_level
2292 		 * unconditionally before calling this function, or never
2293 		 * reach here with status == 0 (e.g. conn->state == BT_CONFIG
2294 		 * returns early above, or status is actually an error).  So
2295 		 * conn->pending_sec_level is never greater than
2296 		 * conn->sec_level at this point; only the initial
2297 		 * BT_SECURITY_SDP -> BT_SECURITY_LOW bump can still happen
2298 		 * here.
2299 		 */
2300 		if (conn->sec_level == BT_SECURITY_SDP) {
2301 			conn->sec_level = BT_SECURITY_LOW;
2302 			mgmt_security_level_changed(conn);
2303 		}
2304 	}
2305 
2306 	mutex_lock(&hci_cb_list_lock);
2307 	list_for_each_entry(cb, &hci_cb_list, list) {
2308 		if (cb->security_cfm)
2309 			cb->security_cfm(conn, status, encrypt);
2310 	}
2311 	mutex_unlock(&hci_cb_list_lock);
2312 
2313 	if (conn->security_cfm_cb)
2314 		conn->security_cfm_cb(conn, status);
2315 }
2316 
2317 static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
2318 {
2319 	struct hci_cb *cb;
2320 
2321 	mutex_lock(&hci_cb_list_lock);
2322 	list_for_each_entry(cb, &hci_cb_list, list) {
2323 		if (cb->key_change_cfm)
2324 			cb->key_change_cfm(conn, status);
2325 	}
2326 	mutex_unlock(&hci_cb_list_lock);
2327 }
2328 
2329 static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
2330 								__u8 role)
2331 {
2332 	struct hci_cb *cb;
2333 
2334 	mutex_lock(&hci_cb_list_lock);
2335 	list_for_each_entry(cb, &hci_cb_list, list) {
2336 		if (cb->role_switch_cfm)
2337 			cb->role_switch_cfm(conn, status, role);
2338 	}
2339 	mutex_unlock(&hci_cb_list_lock);
2340 }
2341 
2342 static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
2343 {
2344 	if (addr_type != ADDR_LE_DEV_RANDOM)
2345 		return false;
2346 
2347 	if ((bdaddr->b[5] & 0xc0) == 0x40)
2348 	       return true;
2349 
2350 	return false;
2351 }
2352 
2353 static inline bool hci_is_identity_address(bdaddr_t *addr, u8 addr_type)
2354 {
2355 	if (addr_type == ADDR_LE_DEV_PUBLIC)
2356 		return true;
2357 
2358 	/* Check for Random Static address type */
2359 	if ((addr->b[5] & 0xc0) == 0xc0)
2360 		return true;
2361 
2362 	return false;
2363 }
2364 
2365 static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
2366 					  bdaddr_t *bdaddr, u8 addr_type)
2367 {
2368 	if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
2369 		return NULL;
2370 
2371 	return hci_find_irk_by_rpa(hdev, bdaddr);
2372 }
2373 
2374 static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,
2375 					u16 to_multiplier)
2376 {
2377 	if (min > max) {
2378 		BT_WARN("min %d > max %d", min, max);
2379 		return -EINVAL;
2380 	}
2381 
2382 	if (min < 6) {
2383 		BT_WARN("min %d < 6", min);
2384 		return -EINVAL;
2385 	}
2386 
2387 	if (max > 3200) {
2388 		BT_WARN("max %d > 3200", max);
2389 		return -EINVAL;
2390 	}
2391 
2392 	if (to_multiplier < 10) {
2393 		BT_WARN("to_multiplier %d < 10", to_multiplier);
2394 		return -EINVAL;
2395 	}
2396 
2397 	if (to_multiplier > 3200) {
2398 		BT_WARN("to_multiplier %d > 3200", to_multiplier);
2399 		return -EINVAL;
2400 	}
2401 
2402 	if (latency > 499) {
2403 		BT_WARN("latency %d > 499", latency);
2404 		return -EINVAL;
2405 	}
2406 
2407 	if ((latency + 1) * max >= to_multiplier * 4) {
2408 		BT_WARN("latency %d, max %d and timeout %d are incompatible",
2409 			latency, max, to_multiplier);
2410 		return -EINVAL;
2411 	}
2412 
2413 	return 0;
2414 }
2415 
2416 int hci_register_cb(struct hci_cb *hcb);
2417 int hci_unregister_cb(struct hci_cb *hcb);
2418 
2419 int hci_send_vendor_frame(struct hci_dev *hdev, struct iov_iter *iter);
2420 
2421 int __hci_cmd_send(struct hci_dev *hdev, u16 opcode, u32 plen,
2422 		   const void *param);
2423 
2424 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
2425 		 const void *param);
2426 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
2427 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
2428 void hci_send_iso(struct hci_conn *conn, struct sk_buff *skb);
2429 
2430 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
2431 void *hci_sent_cmd_data_len(struct hci_dev *hdev, __u16 opcode,
2432 			    size_t *len);
2433 void *hci_recv_event_data(struct hci_dev *hdev, __u8 event);
2434 
2435 u32 hci_conn_get_phy(struct hci_conn *conn);
2436 int hci_conn_set_phy(struct hci_conn *conn, u32 phys);
2437 
2438 /* ----- HCI Sockets ----- */
2439 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
2440 void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
2441 			 int flag, struct sock *skip_sk);
2442 void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
2443 void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
2444 				 void *data, u16 data_len, ktime_t tstamp,
2445 				 int flag, struct sock *skip_sk);
2446 
2447 void hci_sock_dev_event(struct hci_dev *hdev, int event);
2448 
2449 #define HCI_MGMT_VAR_LEN	BIT(0)
2450 #define HCI_MGMT_NO_HDEV	BIT(1)
2451 #define HCI_MGMT_UNTRUSTED	BIT(2)
2452 #define HCI_MGMT_UNCONFIGURED	BIT(3)
2453 #define HCI_MGMT_HDEV_OPTIONAL	BIT(4)
2454 
2455 struct hci_mgmt_handler {
2456 	int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
2457 		     u16 data_len);
2458 	size_t data_len;
2459 	unsigned long flags;
2460 };
2461 
2462 struct hci_mgmt_chan {
2463 	struct list_head list;
2464 	unsigned short channel;
2465 	size_t handler_count;
2466 	const struct hci_mgmt_handler *handlers;
2467 	void (*hdev_init) (struct sock *sk, struct hci_dev *hdev);
2468 };
2469 
2470 int hci_mgmt_chan_register(struct hci_mgmt_chan *c);
2471 void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c);
2472 
2473 /* Management interface */
2474 #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
2475 #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
2476 					 BIT(BDADDR_LE_RANDOM))
2477 #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
2478 					 BIT(BDADDR_LE_PUBLIC) | \
2479 					 BIT(BDADDR_LE_RANDOM))
2480 
2481 /* These LE scan and inquiry parameters were chosen according to LE General
2482  * Discovery Procedure specification.
2483  */
2484 #define DISCOV_LE_SCAN_WIN		0x0012 /* 11.25 msec */
2485 #define DISCOV_LE_SCAN_INT		0x0012 /* 11.25 msec */
2486 #define DISCOV_LE_SCAN_INT_FAST		0x0060 /* 60 msec */
2487 #define DISCOV_LE_SCAN_WIN_FAST		0x0030 /* 30 msec */
2488 #define DISCOV_LE_SCAN_INT_CONN		0x0060 /* 60 msec */
2489 #define DISCOV_LE_SCAN_WIN_CONN		0x0060 /* 60 msec */
2490 #define DISCOV_LE_SCAN_INT_SLOW1	0x0800 /* 1.28 sec */
2491 #define DISCOV_LE_SCAN_WIN_SLOW1	0x0012 /* 11.25 msec */
2492 #define DISCOV_LE_SCAN_INT_SLOW2	0x1000 /* 2.56 sec */
2493 #define DISCOV_LE_SCAN_WIN_SLOW2	0x0024 /* 22.5 msec */
2494 #define DISCOV_CODED_SCAN_INT_FAST	0x0120 /* 180 msec */
2495 #define DISCOV_CODED_SCAN_WIN_FAST	0x0090 /* 90 msec */
2496 #define DISCOV_CODED_SCAN_INT_SLOW1	0x1800 /* 3.84 sec */
2497 #define DISCOV_CODED_SCAN_WIN_SLOW1	0x0036 /* 33.75 msec */
2498 #define DISCOV_CODED_SCAN_INT_SLOW2	0x3000 /* 7.68 sec */
2499 #define DISCOV_CODED_SCAN_WIN_SLOW2	0x006c /* 67.5 msec */
2500 #define DISCOV_LE_TIMEOUT		10240	/* msec */
2501 #define DISCOV_INTERLEAVED_TIMEOUT	5120	/* msec */
2502 #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
2503 #define DISCOV_BREDR_INQUIRY_LEN	0x08
2504 #define DISCOV_LE_RESTART_DELAY		msecs_to_jiffies(200)	/* msec */
2505 #define DISCOV_LE_FAST_ADV_INT_MIN	0x00A0	/* 100 msec */
2506 #define DISCOV_LE_FAST_ADV_INT_MAX	0x00F0	/* 150 msec */
2507 #define DISCOV_LE_PER_ADV_INT_MIN	0x00A0	/* 200 msec */
2508 #define DISCOV_LE_PER_ADV_INT_MAX	0x00A0	/* 200 msec */
2509 #define DISCOV_LE_ADV_MESH_MIN		0x00A0  /* 100 msec */
2510 #define DISCOV_LE_ADV_MESH_MAX		0x00A0  /* 100 msec */
2511 #define INTERVAL_TO_MS(x)		(((x) * 10) / 0x10)
2512 
2513 #define NAME_RESOLVE_DURATION		msecs_to_jiffies(10240)	/* 10.24 sec */
2514 
2515 void mgmt_fill_version_info(void *ver);
2516 int mgmt_new_settings(struct hci_dev *hdev);
2517 void mgmt_index_added(struct hci_dev *hdev);
2518 void mgmt_index_removed(struct hci_dev *hdev);
2519 void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
2520 void mgmt_power_on(struct hci_dev *hdev, int err);
2521 void __mgmt_power_off(struct hci_dev *hdev);
2522 void mgmt_mesh_flush(struct hci_dev *hdev);
2523 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
2524 		       bool persistent);
2525 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
2526 			   u8 *name, u8 name_len);
2527 u8 hci_to_mgmt_reason(u8 err);
2528 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
2529 			      u8 link_type, u8 addr_type, u8 reason,
2530 			      bool mgmt_connected);
2531 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
2532 			    u8 link_type, u8 addr_type, u8 status);
2533 void mgmt_connect_failed(struct hci_dev *hdev, struct hci_conn *conn,
2534 			 u8 status);
2535 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
2536 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2537 				  u8 status);
2538 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2539 				      u8 status);
2540 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2541 			      u8 link_type, u8 addr_type, u32 value,
2542 			      u8 confirm_hint);
2543 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2544 				     u8 link_type, u8 addr_type, u8 status);
2545 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2546 					 u8 link_type, u8 addr_type, u8 status);
2547 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2548 			      u8 link_type, u8 addr_type);
2549 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2550 				     u8 link_type, u8 addr_type, u8 status);
2551 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2552 					 u8 link_type, u8 addr_type, u8 status);
2553 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
2554 			     u8 link_type, u8 addr_type, u32 passkey,
2555 			     u8 entered);
2556 void mgmt_auth_failed(struct hci_conn *conn, u8 status);
2557 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
2558 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
2559 				    u8 status);
2560 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
2561 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2562 		       u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
2563 		       u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
2564 		       u64 instant);
2565 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2566 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
2567 void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
2568 void mgmt_suspending(struct hci_dev *hdev, u8 state);
2569 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
2570 		   u8 addr_type);
2571 bool mgmt_powering_down(struct hci_dev *hdev);
2572 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
2573 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent);
2574 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
2575 		   bool persistent);
2576 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
2577 			 u8 bdaddr_type, u8 store_hint, u16 min_interval,
2578 			 u16 max_interval, u16 latency, u16 timeout);
2579 void mgmt_smp_complete(struct hci_conn *conn, bool complete);
2580 bool mgmt_get_connectable(struct hci_dev *hdev);
2581 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev);
2582 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev,
2583 			    u8 instance);
2584 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
2585 			      u8 instance);
2586 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip);
2587 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
2588 				  bdaddr_t *bdaddr, u8 addr_type);
2589 void mgmt_conn_subrate_notify(struct hci_dev *hdev, struct hci_conn *conn,
2590 			      u8 status);
2591 
2592 int hci_abort_conn(struct hci_conn *conn, u8 reason);
2593 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
2594 		      u16 to_multiplier);
2595 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
2596 		      __u8 ltk[16], __u8 key_size);
2597 
2598 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
2599 			       u8 *bdaddr_type);
2600 
2601 #define SCO_AIRMODE_MASK       0x0003
2602 #define SCO_AIRMODE_CVSD       0x0000
2603 #define SCO_AIRMODE_TRANSP     0x0003
2604 
2605 #define LOCAL_CODEC_ACL_MASK	BIT(0)
2606 #define LOCAL_CODEC_SCO_MASK	BIT(1)
2607 
2608 #define TRANSPORT_TYPE_MAX	0x04
2609 
2610 #endif /* __HCI_CORE_H */
2611