xref: /linux/include/net/bluetooth/hci_core.h (revision c1d50a71cdee90a0c68b7a67221d8aae96d14a0c)
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 	struct list_head	long_term_keys;
565 	struct list_head	identity_resolving_keys;
566 	struct mutex		remote_oob_lock;
567 	struct list_head	remote_oob_data;
568 	struct list_head	le_accept_list;
569 	struct list_head	le_resolv_list;
570 	struct list_head	le_conn_params;
571 	struct list_head	pend_le_conns;
572 	struct list_head	pend_le_reports;
573 	struct list_head	blocked_keys;
574 	struct list_head	local_codecs;
575 
576 	struct hci_dev_stats	stat;
577 
578 	atomic_t		promisc;
579 
580 	const char		*hw_info;
581 	const char		*fw_info;
582 	struct dentry		*debugfs;
583 
584 	struct hci_devcoredump	dump;
585 
586 	struct device		dev;
587 
588 	struct rfkill		*rfkill;
589 
590 	DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS);
591 	hci_conn_flags_t	conn_flags;
592 
593 	__s8			adv_tx_power;
594 	__u8			adv_data[HCI_MAX_EXT_AD_LENGTH];
595 	__u8			adv_data_len;
596 	__u8			scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
597 	__u8			scan_rsp_data_len;
598 	__u8			per_adv_data[HCI_MAX_PER_AD_LENGTH];
599 	__u8			per_adv_data_len;
600 
601 	struct list_head	adv_instances;
602 	unsigned int		adv_instance_cnt;
603 	__u8			cur_adv_instance;
604 	bool			adv_instance_timeout;
605 	struct delayed_work	adv_instance_expire;
606 
607 	struct idr		adv_monitors_idr;
608 	unsigned int		adv_monitors_cnt;
609 
610 	__u8			irk[16];
611 	__u32			rpa_timeout;
612 	struct delayed_work	rpa_expired;
613 	bdaddr_t		rpa;
614 
615 	struct delayed_work	mesh_send_done;
616 
617 	enum {
618 		INTERLEAVE_SCAN_NONE,
619 		INTERLEAVE_SCAN_NO_FILTER,
620 		INTERLEAVE_SCAN_ALLOWLIST
621 	} interleave_scan_state;
622 
623 	struct delayed_work	interleave_scan;
624 
625 	struct list_head	monitored_devices;
626 	bool			advmon_pend_notify;
627 
628 	struct hci_drv		*hci_drv;
629 
630 #if IS_ENABLED(CONFIG_BT_LEDS)
631 	struct led_trigger	*power_led;
632 #endif
633 
634 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
635 	__u16			msft_opcode;
636 	void			*msft_data;
637 	bool			msft_curve_validity;
638 #endif
639 
640 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
641 	bool			aosp_capable;
642 	bool			aosp_quality_report;
643 #endif
644 
645 	int (*open)(struct hci_dev *hdev);
646 	int (*close)(struct hci_dev *hdev);
647 	int (*flush)(struct hci_dev *hdev);
648 	int (*setup)(struct hci_dev *hdev);
649 	int (*shutdown)(struct hci_dev *hdev);
650 	int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
651 	/* Receive HCI_VENDOR_PKT */
652 	void (*recv_vendor_pkt)(struct hci_dev *hdev, struct sk_buff *skb);
653 	/* Handle HCI_EV_VENDOR; return true if handled, false otherwise */
654 	bool (*handle_ev_vendor)(struct hci_dev *hdev, struct sk_buff *skb);
655 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
656 	void (*hw_error)(struct hci_dev *hdev, u8 code);
657 	int (*post_init)(struct hci_dev *hdev);
658 	int (*set_diag)(struct hci_dev *hdev, bool enable);
659 	int (*set_bdaddr)(struct hci_dev *hdev, const bdaddr_t *bdaddr);
660 	void (*reset)(struct hci_dev *hdev);
661 	bool (*wakeup)(struct hci_dev *hdev);
662 	int (*set_quality_report)(struct hci_dev *hdev, bool enable);
663 	int (*get_data_path_id)(struct hci_dev *hdev, __u8 *data_path);
664 	int (*get_codec_config_data)(struct hci_dev *hdev, __u8 type,
665 				     struct bt_codec *codec, __u8 *vnd_len,
666 				     __u8 **vnd_data);
667 	u8 (*classify_pkt_type)(struct hci_dev *hdev, struct sk_buff *skb);
668 };
669 
670 #define hci_set_quirk(hdev, nr) set_bit((nr), (hdev)->quirk_flags)
671 #define hci_clear_quirk(hdev, nr) clear_bit((nr), (hdev)->quirk_flags)
672 #define hci_test_quirk(hdev, nr) test_bit((nr), (hdev)->quirk_flags)
673 
674 #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
675 
676 enum conn_reasons {
677 	CONN_REASON_PAIR_DEVICE,
678 	CONN_REASON_L2CAP_CHAN,
679 	CONN_REASON_SCO_CONNECT,
680 	CONN_REASON_ISO_CONNECT,
681 };
682 
683 enum {
684 	HCI_CONN_AUTH_PEND,
685 	HCI_CONN_ENCRYPT_PEND,
686 	HCI_CONN_RSWITCH_PEND,
687 	HCI_CONN_MODE_CHANGE_PEND,
688 	HCI_CONN_SCO_SETUP_PEND,
689 	HCI_CONN_MGMT_CONNECTED,
690 	HCI_CONN_SSP_ENABLED,
691 	HCI_CONN_SC_ENABLED,
692 	HCI_CONN_AES_CCM,
693 	HCI_CONN_POWER_SAVE,
694 	HCI_CONN_FLUSH_KEY,
695 	HCI_CONN_ENCRYPT,
696 	HCI_CONN_AUTH,
697 	HCI_CONN_SECURE,
698 	HCI_CONN_FIPS,
699 	HCI_CONN_STK_ENCRYPT,
700 	HCI_CONN_AUTH_INITIATOR,
701 	HCI_CONN_DROP,
702 	HCI_CONN_CANCEL,
703 	HCI_CONN_PARAM_REMOVAL_PEND,
704 	HCI_CONN_NEW_LINK_KEY,
705 	HCI_CONN_SCANNING,
706 	HCI_CONN_AUTH_FAILURE,
707 	HCI_CONN_PER_ADV,
708 	HCI_CONN_BIG_CREATED,
709 	HCI_CONN_CREATE,
710 	HCI_CONN_CREATE_CIS,
711 	HCI_CONN_CREATE_BIG_SYNC,
712 	HCI_CONN_BIG_SYNC,
713 	HCI_CONN_BIG_SYNC_FAILED,
714 	HCI_CONN_CREATE_PA_SYNC,
715 	HCI_CONN_PA_SYNC,
716 	HCI_CONN_PA_SYNC_FAILED,
717 	HCI_CONN_ENC_KEY_SIZE_PEND,
718 
719 	__HCI_CONN_NUM_FLAGS,
720 };
721 
722 struct hci_conn {
723 	struct list_head list;
724 
725 	atomic_t	refcnt;
726 
727 	bdaddr_t	dst;
728 	__u8		dst_type;
729 	bdaddr_t	src;
730 	__u8		src_type;
731 	bdaddr_t	init_addr;
732 	__u8		init_addr_type;
733 	bdaddr_t	resp_addr;
734 	__u8		resp_addr_type;
735 	__u8		adv_instance;
736 	__u16		handle;
737 	__u16		sync_handle;
738 	__u8		sid;
739 	__u16		state;
740 	__u16		mtu;
741 	__u8		mode;
742 	__u8		type;
743 	__u8		role;
744 	bool		out;
745 	__u8		attempt;
746 	__u8		dev_class[3];
747 	__u8		features[HCI_MAX_PAGES][8];
748 	__u8		le_features[248];
749 	__u16		pkt_type;
750 	__u16		link_policy;
751 	__u8		key_type;
752 	__u8		auth_type;
753 	__u8		sec_level;
754 	__u8		pending_sec_level;
755 	__u8		pin_length;
756 	__u8		enc_key_size;
757 	__u8		io_capability;
758 	__u32		passkey_notify;
759 	__u8		passkey_entered;
760 	__u16		disc_timeout;
761 	__u16		conn_timeout;
762 	__u16		setting;
763 	__u16		auth_payload_timeout;
764 	__u16		le_conn_min_interval;
765 	__u16		le_conn_max_interval;
766 	__u16		le_conn_interval;
767 	__u16		le_conn_latency;
768 	__u16		le_supv_timeout;
769 	__u16		le_rate_interval;
770 	__u16		le_subrate;
771 	__u16		le_rate_latency;
772 	__u16		le_cont_num;
773 	__u16		le_rate_supv_timeout;
774 	__u8		le_adv_data[HCI_MAX_EXT_AD_LENGTH];
775 	__u8		le_adv_data_len;
776 	__u8		le_per_adv_data[HCI_MAX_PER_AD_TOT_LEN];
777 	__u16		le_per_adv_data_len;
778 	__u16		le_per_adv_data_offset;
779 	__u8		le_adv_phy;
780 	__u8		le_adv_sec_phy;
781 	__u8		le_tx_def_phys;
782 	__u8		le_rx_def_phys;
783 	__u8		le_tx_phy;
784 	__u8		le_rx_phy;
785 	__s8		rssi;
786 	__s8		tx_power;
787 	__s8		max_tx_power;
788 	struct bt_iso_qos iso_qos;
789 	__u8		num_bis;
790 	__u8		bis[HCI_MAX_ISO_BIS];
791 
792 	DECLARE_BITMAP(flags, __HCI_CONN_NUM_FLAGS);
793 
794 	enum conn_reasons conn_reason;
795 	__u8		abort_reason;
796 
797 	__u32		clock;
798 	__u16		clock_accuracy;
799 
800 	unsigned long	conn_info_timestamp;
801 
802 	__u8		remote_cap;
803 	__u8		remote_auth;
804 
805 	unsigned int	sent;
806 
807 	struct sk_buff_head data_q;
808 	struct list_head chan_list;
809 
810 	struct tx_queue tx_q;
811 
812 	struct delayed_work disc_work;
813 	struct delayed_work auto_accept_work;
814 	struct delayed_work idle_work;
815 	struct delayed_work le_conn_timeout;
816 
817 	struct device	dev;
818 	struct dentry	*debugfs;
819 
820 	struct hci_dev	*hdev;
821 
822 	spinlock_t	proto_lock; /* lock guarding protocol data */
823 	void		*l2cap_data __guarded_by(&proto_lock, &hdev->lock);
824 	void		*sco_data;
825 	void		*iso_data __guarded_by(&proto_lock);
826 
827 	struct list_head link_list;
828 	struct hci_conn	*parent;
829 	struct hci_link *link;
830 
831 	struct bt_codec codec;
832 
833 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
834 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
835 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
836 
837 	void (*cleanup)(struct hci_conn *conn);
838 };
839 
840 struct hci_link {
841 	struct list_head list;
842 	struct hci_conn *conn;
843 };
844 
845 struct hci_chan {
846 	struct list_head list;
847 	__u16 handle;
848 	struct hci_conn *conn;
849 	struct sk_buff_head data_q;
850 	unsigned int	sent;
851 	__u8		state;
852 };
853 
854 struct hci_conn_params {
855 	struct list_head list;
856 	struct list_head action;
857 
858 	bdaddr_t addr;
859 	u8 addr_type;
860 
861 	u16 conn_min_interval;
862 	u16 conn_max_interval;
863 	u16 conn_latency;
864 	u16 supervision_timeout;
865 
866 	u16 rate_min_interval;
867 	u16 rate_max_interval;
868 	u16 subrate_min;
869 	u16 subrate_max;
870 	u16 max_latency;
871 	u16 cont_num;
872 	u16 rate_supv_timeout;
873 
874 	enum {
875 		HCI_AUTO_CONN_DISABLED,
876 		HCI_AUTO_CONN_REPORT,
877 		HCI_AUTO_CONN_DIRECT,
878 		HCI_AUTO_CONN_ALWAYS,
879 		HCI_AUTO_CONN_LINK_LOSS,
880 		HCI_AUTO_CONN_EXPLICIT,
881 	} auto_connect;
882 
883 	struct hci_conn *conn;
884 	bool explicit_connect;
885 	/* Accessed without hdev->lock: */
886 	hci_conn_flags_t flags;
887 	u8  privacy_mode;
888 };
889 
890 extern struct list_head hci_dev_list;
891 extern struct list_head hci_cb_list;
892 extern rwlock_t hci_dev_list_lock;
893 extern struct mutex hci_cb_list_lock;
894 
895 #define hci_dev_set_flag(hdev, nr)             set_bit((nr), (hdev)->dev_flags)
896 #define hci_dev_clear_flag(hdev, nr)           clear_bit((nr), (hdev)->dev_flags)
897 #define hci_dev_change_flag(hdev, nr)          change_bit((nr), (hdev)->dev_flags)
898 #define hci_dev_test_flag(hdev, nr)            test_bit((nr), (hdev)->dev_flags)
899 #define hci_dev_test_and_set_flag(hdev, nr)    test_and_set_bit((nr), (hdev)->dev_flags)
900 #define hci_dev_test_and_clear_flag(hdev, nr)  test_and_clear_bit((nr), (hdev)->dev_flags)
901 #define hci_dev_test_and_change_flag(hdev, nr) test_and_change_bit((nr), (hdev)->dev_flags)
902 
903 #define hci_dev_clear_volatile_flags(hdev)				\
904 	do {								\
905 		hci_dev_clear_flag((hdev), HCI_LE_SCAN);		\
906 		hci_dev_clear_flag((hdev), HCI_LE_ADV);			\
907 		hci_dev_clear_flag((hdev), HCI_LL_RPA_RESOLUTION);	\
908 		hci_dev_clear_flag((hdev), HCI_PERIODIC_INQ);		\
909 		hci_dev_clear_flag((hdev), HCI_QUALITY_REPORT);		\
910 	} while (0)
911 
912 #define hci_dev_le_state_simultaneous(hdev) \
913 	(!hci_test_quirk((hdev), HCI_QUIRK_BROKEN_LE_STATES) && \
914 	 ((hdev)->le_states[4] & 0x08) &&	/* Central */ \
915 	 ((hdev)->le_states[4] & 0x40) &&	/* Peripheral */ \
916 	 ((hdev)->le_states[3] & 0x10))		/* Simultaneous */
917 
918 /* ----- HCI interface to upper protocols ----- */
919 int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
920 int l2cap_disconn_ind(struct hci_conn *hcon);
921 int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, struct sk_buff *skb,
922 		       u16 flags);
923 
924 #if IS_ENABLED(CONFIG_BT_BREDR)
925 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
926 int sco_recv_scodata(struct hci_dev *hdev, u16 handle, struct sk_buff *skb);
927 #else
928 static inline int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
929 				  __u8 *flags)
930 {
931 	return 0;
932 }
933 
934 static inline int sco_recv_scodata(struct hci_dev *hdev, u16 handle,
935 				   struct sk_buff *skb)
936 {
937 	kfree_skb(skb);
938 	return -ENOENT;
939 }
940 #endif
941 
942 #if IS_ENABLED(CONFIG_BT_LE)
943 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
944 int iso_recv(struct hci_dev *hdev, u16 handle, struct sk_buff *skb,
945 	     u16 flags);
946 #else
947 static inline int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
948 				  __u8 *flags)
949 {
950 	return 0;
951 }
952 
953 static inline int iso_recv(struct hci_dev *hdev, u16 handle,
954 			   struct sk_buff *skb, u16 flags)
955 {
956 	kfree_skb(skb);
957 	return -ENOENT;
958 }
959 #endif
960 
961 /* ----- Inquiry cache ----- */
962 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
963 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
964 
965 static inline void discovery_init(struct hci_dev *hdev)
966 {
967 	spin_lock_init(&hdev->discovery.lock);
968 	hdev->discovery.state = DISCOVERY_STOPPED;
969 	INIT_LIST_HEAD(&hdev->discovery.all);
970 	INIT_LIST_HEAD(&hdev->discovery.unknown);
971 	INIT_LIST_HEAD(&hdev->discovery.resolve);
972 	hdev->discovery.report_invalid_rssi = true;
973 	hdev->discovery.rssi = HCI_RSSI_INVALID;
974 }
975 
976 static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
977 {
978 	hdev->discovery.result_filtering = false;
979 	hdev->discovery.report_invalid_rssi = true;
980 	hdev->discovery.rssi = HCI_RSSI_INVALID;
981 
982 	spin_lock(&hdev->discovery.lock);
983 	hdev->discovery.uuid_count = 0;
984 	kfree(hdev->discovery.uuids);
985 	hdev->discovery.uuids = NULL;
986 	spin_unlock(&hdev->discovery.lock);
987 }
988 
989 bool hci_discovery_active(struct hci_dev *hdev);
990 
991 void hci_discovery_set_state(struct hci_dev *hdev, int state);
992 
993 static inline int inquiry_cache_empty(struct hci_dev *hdev)
994 {
995 	return list_empty(&hdev->discovery.all);
996 }
997 
998 static inline long inquiry_cache_age(struct hci_dev *hdev)
999 {
1000 	struct discovery_state *c = &hdev->discovery;
1001 	return jiffies - c->timestamp;
1002 }
1003 
1004 static inline long inquiry_entry_age(struct inquiry_entry *e)
1005 {
1006 	return jiffies - e->timestamp;
1007 }
1008 
1009 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
1010 					       bdaddr_t *bdaddr);
1011 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
1012 						       bdaddr_t *bdaddr);
1013 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
1014 						       bdaddr_t *bdaddr,
1015 						       int state);
1016 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
1017 				      struct inquiry_entry *ie);
1018 u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
1019 			     bool name_known);
1020 void hci_inquiry_cache_flush(struct hci_dev *hdev);
1021 
1022 static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
1023 {
1024 	struct hci_dev *hdev = conn->hdev;
1025 	return hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
1026 	       test_bit(HCI_CONN_SSP_ENABLED, conn->flags);
1027 }
1028 
1029 static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
1030 {
1031 	struct hci_dev *hdev = conn->hdev;
1032 	return hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
1033 	       test_bit(HCI_CONN_SC_ENABLED, conn->flags);
1034 }
1035 
1036 static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
1037 {
1038 	struct hci_conn_hash *h = &hdev->conn_hash;
1039 
1040 	lockdep_assert_held(&hdev->lock);
1041 
1042 	list_add_tail_rcu(&c->list, &h->list);
1043 	switch (c->type) {
1044 	case ACL_LINK:
1045 		h->acl_num++;
1046 		break;
1047 	case LE_LINK:
1048 		h->le_num++;
1049 		if (c->role == HCI_ROLE_SLAVE)
1050 			h->le_num_peripheral++;
1051 		break;
1052 	case SCO_LINK:
1053 	case ESCO_LINK:
1054 		h->sco_num++;
1055 		break;
1056 	case CIS_LINK:
1057 		h->cis_num++;
1058 		break;
1059 	case BIS_LINK:
1060 		h->bis_num++;
1061 		break;
1062 	case PA_LINK:
1063 		h->pa_num++;
1064 		break;
1065 	}
1066 }
1067 
1068 static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
1069 {
1070 	struct hci_conn_hash *h = &hdev->conn_hash;
1071 
1072 	lockdep_assert_held(&hdev->lock);
1073 
1074 	list_del_rcu(&c->list);
1075 	synchronize_rcu();
1076 
1077 	switch (c->type) {
1078 	case ACL_LINK:
1079 		h->acl_num--;
1080 		break;
1081 	case LE_LINK:
1082 		h->le_num--;
1083 		if (c->role == HCI_ROLE_SLAVE)
1084 			h->le_num_peripheral--;
1085 		break;
1086 	case SCO_LINK:
1087 	case ESCO_LINK:
1088 		h->sco_num--;
1089 		break;
1090 	case CIS_LINK:
1091 		h->cis_num--;
1092 		break;
1093 	case BIS_LINK:
1094 		h->bis_num--;
1095 		break;
1096 	case PA_LINK:
1097 		h->pa_num--;
1098 		break;
1099 	}
1100 }
1101 
1102 #ifdef CONFIG_PROVE_RCU
1103 #define HCI_CONN_HASH_LOCKDEP_CHECK(hdev)				\
1104 	RCU_LOCKDEP_WARN(!lockdep_is_held(&(hdev)->lock) &&		\
1105 			 !rcu_read_lock_held(),				\
1106 			 "suspicious hci_conn locking")
1107 #else
1108 #define HCI_CONN_HASH_LOCKDEP_CHECK(hdev) do { } while (0 && (hdev))
1109 #endif
1110 
1111 static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
1112 {
1113 	struct hci_conn_hash *h = &hdev->conn_hash;
1114 	switch (type) {
1115 	case ACL_LINK:
1116 		return h->acl_num;
1117 	case LE_LINK:
1118 		return h->le_num;
1119 	case SCO_LINK:
1120 	case ESCO_LINK:
1121 		return h->sco_num;
1122 	case CIS_LINK:
1123 		return h->cis_num;
1124 	case BIS_LINK:
1125 		return h->bis_num;
1126 	case PA_LINK:
1127 		return h->pa_num;
1128 	default:
1129 		return 0;
1130 	}
1131 }
1132 
1133 static inline unsigned int hci_conn_count(struct hci_dev *hdev)
1134 {
1135 	struct hci_conn_hash *c = &hdev->conn_hash;
1136 
1137 	return c->acl_num + c->sco_num + c->le_num + c->cis_num + c->bis_num +
1138 		c->pa_num;
1139 }
1140 
1141 static inline unsigned int hci_iso_count(struct hci_dev *hdev)
1142 {
1143 	struct hci_conn_hash *c = &hdev->conn_hash;
1144 
1145 	return c->cis_num + c->bis_num;
1146 }
1147 
1148 static inline bool hci_conn_valid(struct hci_dev *hdev, struct hci_conn *conn)
1149 {
1150 	struct hci_conn_hash *h = &hdev->conn_hash;
1151 	struct hci_conn  *c;
1152 
1153 	rcu_read_lock();
1154 
1155 	list_for_each_entry_rcu(c, &h->list, list) {
1156 		if (c == conn) {
1157 			rcu_read_unlock();
1158 			return true;
1159 		}
1160 	}
1161 	rcu_read_unlock();
1162 
1163 	return false;
1164 }
1165 
1166 static inline __u8 hci_conn_lookup_type(struct hci_dev *hdev, __u16 handle)
1167 {
1168 	struct hci_conn_hash *h = &hdev->conn_hash;
1169 	struct hci_conn *c;
1170 	__u8 type = INVALID_LINK;
1171 
1172 	rcu_read_lock();
1173 
1174 	list_for_each_entry_rcu(c, &h->list, list) {
1175 		if (c->handle == handle) {
1176 			type = c->type;
1177 			break;
1178 		}
1179 	}
1180 
1181 	rcu_read_unlock();
1182 
1183 	return type;
1184 }
1185 
1186 static inline struct hci_conn *hci_conn_hash_lookup_bis(struct hci_dev *hdev,
1187 							bdaddr_t *ba, __u8 bis)
1188 {
1189 	struct hci_conn_hash *h = &hdev->conn_hash;
1190 	struct hci_conn  *c;
1191 
1192 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1193 
1194 	rcu_read_lock();
1195 
1196 	list_for_each_entry_rcu(c, &h->list, list) {
1197 		if (bacmp(&c->dst, ba) || c->type != BIS_LINK)
1198 			continue;
1199 
1200 		if (c->iso_qos.bcast.bis == bis) {
1201 			rcu_read_unlock();
1202 			return c;
1203 		}
1204 	}
1205 	rcu_read_unlock();
1206 
1207 	return NULL;
1208 }
1209 
1210 static inline struct hci_conn *
1211 hci_conn_hash_lookup_create_pa_sync(struct hci_dev *hdev)
1212 {
1213 	struct hci_conn_hash *h = &hdev->conn_hash;
1214 	struct hci_conn  *c;
1215 
1216 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1217 
1218 	rcu_read_lock();
1219 
1220 	list_for_each_entry_rcu(c, &h->list, list) {
1221 		if (c->type != PA_LINK)
1222 			continue;
1223 
1224 		if (!test_bit(HCI_CONN_CREATE_PA_SYNC, c->flags))
1225 			continue;
1226 
1227 		rcu_read_unlock();
1228 		return c;
1229 	}
1230 
1231 	rcu_read_unlock();
1232 
1233 	return NULL;
1234 }
1235 
1236 static inline struct hci_conn *
1237 hci_conn_hash_lookup_per_adv_bis(struct hci_dev *hdev,
1238 				 bdaddr_t *ba,
1239 				 __u8 big, __u8 bis)
1240 {
1241 	struct hci_conn_hash *h = &hdev->conn_hash;
1242 	struct hci_conn  *c;
1243 
1244 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1245 
1246 	rcu_read_lock();
1247 
1248 	list_for_each_entry_rcu(c, &h->list, list) {
1249 		if (bacmp(&c->dst, ba) || c->type != BIS_LINK ||
1250 		    !test_bit(HCI_CONN_PER_ADV, c->flags))
1251 			continue;
1252 
1253 		if (c->iso_qos.bcast.big == big &&
1254 		    c->iso_qos.bcast.bis == bis) {
1255 			rcu_read_unlock();
1256 			return c;
1257 		}
1258 	}
1259 	rcu_read_unlock();
1260 
1261 	return NULL;
1262 }
1263 
1264 static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
1265 								__u16 handle)
1266 {
1267 	struct hci_conn_hash *h = &hdev->conn_hash;
1268 	struct hci_conn  *c;
1269 
1270 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1271 
1272 	rcu_read_lock();
1273 
1274 	list_for_each_entry_rcu(c, &h->list, list) {
1275 		if (c->handle == handle) {
1276 			rcu_read_unlock();
1277 			return c;
1278 		}
1279 	}
1280 	rcu_read_unlock();
1281 
1282 	return NULL;
1283 }
1284 
1285 static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
1286 							__u8 type, bdaddr_t *ba)
1287 {
1288 	struct hci_conn_hash *h = &hdev->conn_hash;
1289 	struct hci_conn  *c;
1290 
1291 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1292 
1293 	rcu_read_lock();
1294 
1295 	list_for_each_entry_rcu(c, &h->list, list) {
1296 		if (c->type == type && !bacmp(&c->dst, ba)) {
1297 			rcu_read_unlock();
1298 			return c;
1299 		}
1300 	}
1301 
1302 	rcu_read_unlock();
1303 
1304 	return NULL;
1305 }
1306 
1307 static inline struct hci_conn *hci_conn_hash_lookup_role(struct hci_dev *hdev,
1308 							 __u8 type, __u8 role,
1309 							 bdaddr_t *ba)
1310 {
1311 	struct hci_conn_hash *h = &hdev->conn_hash;
1312 	struct hci_conn  *c;
1313 
1314 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1315 
1316 	rcu_read_lock();
1317 
1318 	list_for_each_entry_rcu(c, &h->list, list) {
1319 		if (c->type == type && c->role == role && !bacmp(&c->dst, ba)) {
1320 			rcu_read_unlock();
1321 			return c;
1322 		}
1323 	}
1324 
1325 	rcu_read_unlock();
1326 
1327 	return NULL;
1328 }
1329 
1330 static inline struct hci_conn *hci_conn_hash_lookup_le(struct hci_dev *hdev,
1331 						       bdaddr_t *ba,
1332 						       __u8 ba_type)
1333 {
1334 	struct hci_conn_hash *h = &hdev->conn_hash;
1335 	struct hci_conn  *c;
1336 
1337 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1338 
1339 	rcu_read_lock();
1340 
1341 	list_for_each_entry_rcu(c, &h->list, list) {
1342 		if (c->type != LE_LINK)
1343 		       continue;
1344 
1345 		if (ba_type == c->dst_type && !bacmp(&c->dst, ba)) {
1346 			rcu_read_unlock();
1347 			return c;
1348 		}
1349 	}
1350 
1351 	rcu_read_unlock();
1352 
1353 	return NULL;
1354 }
1355 
1356 static inline struct hci_conn *hci_conn_hash_lookup_cis(struct hci_dev *hdev,
1357 							bdaddr_t *ba,
1358 							__u8 ba_type,
1359 							__u8 cig,
1360 							__u8 id)
1361 {
1362 	struct hci_conn_hash *h = &hdev->conn_hash;
1363 	struct hci_conn  *c;
1364 
1365 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1366 
1367 	rcu_read_lock();
1368 
1369 	list_for_each_entry_rcu(c, &h->list, list) {
1370 		if (c->type != CIS_LINK)
1371 			continue;
1372 
1373 		/* Match CIG ID if set */
1374 		if (cig != c->iso_qos.ucast.cig)
1375 			continue;
1376 
1377 		/* Match CIS ID if set */
1378 		if (id != c->iso_qos.ucast.cis)
1379 			continue;
1380 
1381 		/* Match destination address if set */
1382 		if (!ba || (ba_type == c->dst_type && !bacmp(&c->dst, ba))) {
1383 			rcu_read_unlock();
1384 			return c;
1385 		}
1386 	}
1387 
1388 	rcu_read_unlock();
1389 
1390 	return NULL;
1391 }
1392 
1393 static inline struct hci_conn *hci_conn_hash_lookup_cig(struct hci_dev *hdev,
1394 							__u8 handle)
1395 {
1396 	struct hci_conn_hash *h = &hdev->conn_hash;
1397 	struct hci_conn  *c;
1398 
1399 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1400 
1401 	rcu_read_lock();
1402 
1403 	list_for_each_entry_rcu(c, &h->list, list) {
1404 		if (c->type != CIS_LINK)
1405 			continue;
1406 
1407 		if (handle == c->iso_qos.ucast.cig) {
1408 			rcu_read_unlock();
1409 			return c;
1410 		}
1411 	}
1412 
1413 	rcu_read_unlock();
1414 
1415 	return NULL;
1416 }
1417 
1418 static inline struct hci_conn *hci_conn_hash_lookup_big(struct hci_dev *hdev,
1419 							__u8 handle)
1420 {
1421 	struct hci_conn_hash *h = &hdev->conn_hash;
1422 	struct hci_conn  *c;
1423 
1424 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1425 
1426 	rcu_read_lock();
1427 
1428 	list_for_each_entry_rcu(c, &h->list, list) {
1429 		if (c->type != BIS_LINK)
1430 			continue;
1431 
1432 		if (handle == c->iso_qos.bcast.big) {
1433 			rcu_read_unlock();
1434 			return c;
1435 		}
1436 	}
1437 
1438 	rcu_read_unlock();
1439 
1440 	return NULL;
1441 }
1442 
1443 static inline struct hci_conn *
1444 hci_conn_hash_lookup_big_sync_pend(struct hci_dev *hdev,
1445 				   __u8 handle, __u8 num_bis)
1446 {
1447 	struct hci_conn_hash *h = &hdev->conn_hash;
1448 	struct hci_conn  *c;
1449 
1450 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1451 
1452 	rcu_read_lock();
1453 
1454 	list_for_each_entry_rcu(c, &h->list, list) {
1455 		if (c->type != PA_LINK)
1456 			continue;
1457 
1458 		if (handle == c->iso_qos.bcast.big && num_bis == c->num_bis) {
1459 			rcu_read_unlock();
1460 			return c;
1461 		}
1462 	}
1463 
1464 	rcu_read_unlock();
1465 
1466 	return NULL;
1467 }
1468 
1469 static inline struct hci_conn *
1470 hci_conn_hash_lookup_big_state(struct hci_dev *hdev, __u8 handle, __u16 state,
1471 			       __u8 role)
1472 {
1473 	struct hci_conn_hash *h = &hdev->conn_hash;
1474 	struct hci_conn  *c;
1475 
1476 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1477 
1478 	rcu_read_lock();
1479 
1480 	list_for_each_entry_rcu(c, &h->list, list) {
1481 		if (c->type != BIS_LINK || c->state != state || c->role != role)
1482 			continue;
1483 
1484 		if (handle == c->iso_qos.bcast.big) {
1485 			rcu_read_unlock();
1486 			return c;
1487 		}
1488 	}
1489 
1490 	rcu_read_unlock();
1491 
1492 	return NULL;
1493 }
1494 
1495 static inline struct hci_conn *
1496 hci_conn_hash_lookup_pa_sync_big_handle(struct hci_dev *hdev, __u8 big)
1497 {
1498 	struct hci_conn_hash *h = &hdev->conn_hash;
1499 	struct hci_conn  *c;
1500 
1501 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1502 
1503 	rcu_read_lock();
1504 
1505 	list_for_each_entry_rcu(c, &h->list, list) {
1506 		if (c->type != BIS_LINK ||
1507 		    !test_bit(HCI_CONN_PA_SYNC, c->flags))
1508 			continue;
1509 
1510 		if (c->iso_qos.bcast.big == big) {
1511 			rcu_read_unlock();
1512 			return c;
1513 		}
1514 	}
1515 	rcu_read_unlock();
1516 
1517 	return NULL;
1518 }
1519 
1520 static inline struct hci_conn *
1521 hci_conn_hash_lookup_pa_sync_handle(struct hci_dev *hdev, __u16 sync_handle)
1522 {
1523 	struct hci_conn_hash *h = &hdev->conn_hash;
1524 	struct hci_conn  *c;
1525 
1526 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1527 
1528 	rcu_read_lock();
1529 
1530 	list_for_each_entry_rcu(c, &h->list, list) {
1531 		if (c->type != PA_LINK)
1532 			continue;
1533 
1534 		/* Ignore the listen hcon, we are looking
1535 		 * for the child hcon that was created as
1536 		 * a result of the PA sync established event.
1537 		 */
1538 		if (c->state == BT_LISTEN)
1539 			continue;
1540 
1541 		if (c->sync_handle == sync_handle) {
1542 			rcu_read_unlock();
1543 			return c;
1544 		}
1545 	}
1546 	rcu_read_unlock();
1547 
1548 	return NULL;
1549 }
1550 
1551 typedef void (*hci_conn_func_t)(struct hci_conn *conn, void *data);
1552 static inline void hci_conn_hash_list_state(struct hci_dev *hdev,
1553 					    hci_conn_func_t func, __u8 type,
1554 					    __u16 state, void *data)
1555 {
1556 	struct hci_conn_hash *h = &hdev->conn_hash;
1557 	struct hci_conn  *c;
1558 
1559 	if (!func)
1560 		return;
1561 
1562 	rcu_read_lock();
1563 
1564 	list_for_each_entry_rcu(c, &h->list, list) {
1565 		if (c->type == type && c->state == state)
1566 			func(c, data);
1567 	}
1568 
1569 	rcu_read_unlock();
1570 }
1571 
1572 static inline void hci_conn_hash_list_flag(struct hci_dev *hdev,
1573 					    hci_conn_func_t func, __u8 type,
1574 					    __u8 flag, void *data)
1575 {
1576 	struct hci_conn_hash *h = &hdev->conn_hash;
1577 	struct hci_conn  *c;
1578 
1579 	if (!func)
1580 		return;
1581 
1582 	rcu_read_lock();
1583 
1584 	list_for_each_entry_rcu(c, &h->list, list) {
1585 		if (c->type == type && test_bit(flag, c->flags))
1586 			func(c, data);
1587 	}
1588 
1589 	rcu_read_unlock();
1590 }
1591 
1592 static inline struct hci_conn *hci_lookup_le_connect(struct hci_dev *hdev)
1593 {
1594 	struct hci_conn_hash *h = &hdev->conn_hash;
1595 	struct hci_conn  *c;
1596 
1597 	HCI_CONN_HASH_LOCKDEP_CHECK(hdev);
1598 
1599 	rcu_read_lock();
1600 
1601 	list_for_each_entry_rcu(c, &h->list, list) {
1602 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1603 		    !test_bit(HCI_CONN_SCANNING, c->flags)) {
1604 			rcu_read_unlock();
1605 			return c;
1606 		}
1607 	}
1608 
1609 	rcu_read_unlock();
1610 
1611 	return NULL;
1612 }
1613 
1614 /* Returns true if an le connection is in the scanning state */
1615 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1616 {
1617 	struct hci_conn_hash *h = &hdev->conn_hash;
1618 	struct hci_conn  *c;
1619 
1620 	rcu_read_lock();
1621 
1622 	list_for_each_entry_rcu(c, &h->list, list) {
1623 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1624 		    test_bit(HCI_CONN_SCANNING, c->flags)) {
1625 			rcu_read_unlock();
1626 			return true;
1627 		}
1628 	}
1629 
1630 	rcu_read_unlock();
1631 
1632 	return false;
1633 }
1634 
1635 int hci_disconnect(struct hci_conn *conn, __u8 reason);
1636 bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
1637 void hci_sco_setup(struct hci_conn *conn, __u8 status);
1638 bool hci_iso_setup_path(struct hci_conn *conn);
1639 int hci_le_create_cis_pending(struct hci_dev *hdev);
1640 int hci_conn_check_create_cis(struct hci_conn *conn);
1641 
1642 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1643 			      u8 dst_type, u8 role, u16 handle);
1644 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1645 				    bdaddr_t *dst, u8 dst_type, u8 role);
1646 void hci_conn_del(struct hci_conn *conn);
1647 void hci_conn_hash_flush(struct hci_dev *hdev);
1648 
1649 struct hci_chan *hci_chan_create(struct hci_conn *conn);
1650 void hci_chan_del(struct hci_chan *chan);
1651 void hci_chan_list_flush(struct hci_conn *conn);
1652 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
1653 
1654 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1655 				     u8 dst_type, u8 sec_level,
1656 				     u16 conn_timeout,
1657 				     enum conn_reasons conn_reason);
1658 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1659 				u8 dst_type, bool dst_resolved, u8 sec_level,
1660 				u16 conn_timeout, u8 role, u8 phy, u8 sec_phy);
1661 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status);
1662 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1663 				 u8 sec_level, u8 auth_type,
1664 				 enum conn_reasons conn_reason, u16 timeout);
1665 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1666 				 __u16 setting, struct bt_codec *codec,
1667 				 u16 timeout);
1668 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1669 			      __u8 dst_type, struct bt_iso_qos *qos,
1670 			      u16 timeout);
1671 struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid,
1672 			      struct bt_iso_qos *qos,
1673 			      __u8 base_len, __u8 *base, u16 timeout);
1674 int hci_past_bis(struct hci_conn *conn, bdaddr_t *dst, __u8 dst_type);
1675 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
1676 				 __u8 dst_type, struct bt_iso_qos *qos,
1677 				 u16 timeout);
1678 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
1679 				 __u8 dst_type, __u8 sid,
1680 				 struct bt_iso_qos *qos,
1681 				 __u8 data_len, __u8 *data, u16 timeout);
1682 struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
1683 		       __u8 dst_type, __u8 sid, struct bt_iso_qos *qos);
1684 int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
1685 			     struct bt_iso_qos *qos, __u16 sync_handle,
1686 			     __u8 num_bis, __u8 bis[]);
1687 int hci_conn_check_link_mode(struct hci_conn *conn);
1688 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
1689 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1690 		      bool initiator);
1691 int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
1692 
1693 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
1694 
1695 void hci_conn_failed(struct hci_conn *conn, u8 status);
1696 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle);
1697 
1698 void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb);
1699 void hci_conn_tx_dequeue(struct hci_conn *conn);
1700 void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
1701 			    const struct sockcm_cookie *sockc);
1702 
1703 static inline void hci_sockcm_init(struct sockcm_cookie *sockc, struct sock *sk)
1704 {
1705 	*sockc = (struct sockcm_cookie) {
1706 		.tsflags = READ_ONCE(sk->sk_tsflags),
1707 	};
1708 }
1709 
1710 /*
1711  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
1712  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
1713  * working or anything else. They just guarantee that the object is available
1714  * and can be dereferenced. So you can use its locks, local variables and any
1715  * other constant data.
1716  * Before accessing runtime data, you _must_ lock the object and then check that
1717  * it is still running. As soon as you release the locks, the connection might
1718  * get dropped, though.
1719  *
1720  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
1721  * how long the underlying connection is held. So every channel that runs on the
1722  * hci_conn object calls this to prevent the connection from disappearing. As
1723  * long as you hold a device, you must also guarantee that you have a valid
1724  * reference to the device via hci_conn_get() (or the initial reference from
1725  * hci_conn_add()).
1726  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
1727  * break because nobody cares for that. But this means, we cannot use
1728  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
1729  */
1730 
1731 static inline struct hci_conn *hci_conn_get(struct hci_conn *conn)
1732 {
1733 	get_device(&conn->dev);
1734 	return conn;
1735 }
1736 
1737 static inline void hci_conn_put(struct hci_conn *conn)
1738 {
1739 	put_device(&conn->dev);
1740 }
1741 
1742 static inline struct hci_conn *hci_conn_hold(struct hci_conn *conn)
1743 {
1744 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1745 
1746 	atomic_inc(&conn->refcnt);
1747 	cancel_delayed_work(&conn->disc_work);
1748 
1749 	return conn;
1750 }
1751 
1752 static inline void hci_conn_drop(struct hci_conn *conn)
1753 {
1754 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1755 
1756 	if (atomic_dec_and_test(&conn->refcnt)) {
1757 		unsigned long timeo;
1758 
1759 		switch (conn->type) {
1760 		case ACL_LINK:
1761 		case LE_LINK:
1762 			cancel_delayed_work(&conn->idle_work);
1763 			if (conn->state == BT_CONNECTED) {
1764 				timeo = conn->disc_timeout;
1765 				if (!conn->out)
1766 					timeo *= 2;
1767 			} else {
1768 				timeo = 0;
1769 			}
1770 			break;
1771 
1772 		default:
1773 			timeo = 0;
1774 			break;
1775 		}
1776 
1777 		cancel_delayed_work(&conn->disc_work);
1778 		queue_delayed_work(conn->hdev->workqueue,
1779 				   &conn->disc_work, timeo);
1780 	}
1781 }
1782 
1783 /* ----- HCI Devices ----- */
1784 static inline void hci_dev_put(struct hci_dev *d)
1785 {
1786 	BT_DBG("%s orig refcnt %d", d->name,
1787 	       kref_read(&d->dev.kobj.kref));
1788 
1789 	put_device(&d->dev);
1790 }
1791 
1792 static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
1793 {
1794 	BT_DBG("%s orig refcnt %d", d->name,
1795 	       kref_read(&d->dev.kobj.kref));
1796 
1797 	get_device(&d->dev);
1798 	return d;
1799 }
1800 
1801 #define hci_dev_lock(d)		mutex_lock(&d->lock)
1802 #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
1803 
1804 #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
1805 #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
1806 
1807 static inline void *hci_get_drvdata(struct hci_dev *hdev)
1808 {
1809 	return dev_get_drvdata(&hdev->dev);
1810 }
1811 
1812 static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
1813 {
1814 	dev_set_drvdata(&hdev->dev, data);
1815 }
1816 
1817 static inline void *hci_get_priv(struct hci_dev *hdev)
1818 {
1819 	return (char *)hdev + sizeof(*hdev);
1820 }
1821 
1822 struct hci_dev *hci_dev_get(int index);
1823 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, u8 src_type);
1824 
1825 struct hci_dev *hci_alloc_dev_priv(int sizeof_priv);
1826 
1827 static inline struct hci_dev *hci_alloc_dev(void)
1828 {
1829 	return hci_alloc_dev_priv(0);
1830 }
1831 
1832 void hci_free_dev(struct hci_dev *hdev);
1833 int hci_register_dev(struct hci_dev *hdev);
1834 void hci_unregister_dev(struct hci_dev *hdev);
1835 void hci_release_dev(struct hci_dev *hdev);
1836 int hci_register_suspend_notifier(struct hci_dev *hdev);
1837 int hci_unregister_suspend_notifier(struct hci_dev *hdev);
1838 int hci_suspend_dev(struct hci_dev *hdev);
1839 int hci_resume_dev(struct hci_dev *hdev);
1840 int __hci_reset_dev(struct hci_dev *hdev, u8 hw_err_code);
1841 
1842 static inline int hci_reset_dev(struct hci_dev *hdev)
1843 {
1844 	return __hci_reset_dev(hdev, 0);
1845 }
1846 
1847 int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
1848 int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb);
1849 __printf(2, 3) void hci_set_hw_info(struct hci_dev *hdev, const char *fmt, ...);
1850 __printf(2, 3) void hci_set_fw_info(struct hci_dev *hdev, const char *fmt, ...);
1851 
1852 static inline void hci_set_msft_opcode(struct hci_dev *hdev, __u16 opcode)
1853 {
1854 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
1855 	hdev->msft_opcode = opcode;
1856 #endif
1857 }
1858 
1859 static inline void hci_set_aosp_capable(struct hci_dev *hdev)
1860 {
1861 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
1862 	hdev->aosp_capable = true;
1863 #endif
1864 }
1865 
1866 static inline void hci_devcd_setup(struct hci_dev *hdev)
1867 {
1868 #ifdef CONFIG_DEV_COREDUMP
1869 	INIT_WORK(&hdev->dump.dump_rx, hci_devcd_rx);
1870 	INIT_DELAYED_WORK(&hdev->dump.dump_timeout, hci_devcd_timeout);
1871 	skb_queue_head_init(&hdev->dump.dump_q);
1872 #endif
1873 }
1874 
1875 int hci_dev_open(__u16 dev);
1876 int hci_dev_close(__u16 dev);
1877 int hci_dev_do_close(struct hci_dev *hdev);
1878 int hci_dev_reset(__u16 dev);
1879 int hci_dev_reset_stat(__u16 dev);
1880 int hci_dev_cmd(unsigned int cmd, void __user *arg);
1881 int hci_get_dev_list(void __user *arg);
1882 int hci_get_dev_info(void __user *arg);
1883 int hci_get_conn_list(void __user *arg);
1884 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
1885 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
1886 int hci_inquiry(void __user *arg);
1887 
1888 struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *list,
1889 					   bdaddr_t *bdaddr, u8 type);
1890 struct bdaddr_list_with_irk *hci_bdaddr_list_lookup_with_irk(
1891 				    struct list_head *list, bdaddr_t *bdaddr,
1892 				    u8 type);
1893 struct bdaddr_list_with_flags *
1894 hci_bdaddr_list_lookup_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1895 				  u8 type);
1896 int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1897 int hci_bdaddr_list_add_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1898 				 u8 type, u8 *peer_irk, u8 *local_irk);
1899 int hci_bdaddr_list_add_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1900 				   u8 type, u32 flags);
1901 int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1902 int hci_bdaddr_list_del_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1903 				 u8 type);
1904 void hci_bdaddr_list_clear(struct list_head *list);
1905 
1906 struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
1907 					       bdaddr_t *addr, u8 addr_type);
1908 struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
1909 					    bdaddr_t *addr, u8 addr_type);
1910 void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
1911 void hci_conn_params_clear_disabled(struct hci_dev *hdev);
1912 void hci_conn_params_free(struct hci_conn_params *param);
1913 
1914 void hci_pend_le_list_del_init(struct hci_conn_params *param);
1915 void hci_pend_le_list_add(struct hci_conn_params *param,
1916 			  struct list_head *list);
1917 struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
1918 						  bdaddr_t *addr,
1919 						  u8 addr_type);
1920 
1921 void hci_uuids_clear(struct hci_dev *hdev);
1922 
1923 void hci_link_keys_clear(struct hci_dev *hdev);
1924 u8 *hci_conn_key_enc_size(struct hci_conn *conn);
1925 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
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 struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1930 			    u8 addr_type, u8 type, u8 authenticated,
1931 			    u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
1932 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1933 			     u8 addr_type, u8 role);
1934 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
1935 void hci_smp_ltks_clear(struct hci_dev *hdev);
1936 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1937 
1938 struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
1939 struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1940 				     u8 addr_type);
1941 struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1942 			    u8 addr_type, u8 val[16], bdaddr_t *rpa);
1943 void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
1944 bool hci_is_blocked_key(struct hci_dev *hdev, u8 type, u8 val[16]);
1945 void hci_blocked_keys_clear(struct hci_dev *hdev);
1946 void hci_smp_irks_clear(struct hci_dev *hdev);
1947 
1948 bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1949 
1950 void hci_remote_oob_data_clear(struct hci_dev *hdev);
1951 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1952 					  bdaddr_t *bdaddr, u8 bdaddr_type);
1953 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1954 			    u8 bdaddr_type, u8 *hash192, u8 *rand192,
1955 			    u8 *hash256, u8 *rand256);
1956 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1957 			       u8 bdaddr_type);
1958 
1959 void hci_adv_instances_clear(struct hci_dev *hdev);
1960 struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance);
1961 struct adv_info *hci_find_adv_sid(struct hci_dev *hdev, u8 sid);
1962 struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance);
1963 struct adv_info *hci_add_adv_instance(struct hci_dev *hdev, u8 instance,
1964 				      u32 flags, u16 adv_data_len, u8 *adv_data,
1965 				      u16 scan_rsp_len, u8 *scan_rsp_data,
1966 				      u16 timeout, u16 duration, s8 tx_power,
1967 				      u32 min_interval, u32 max_interval,
1968 				      u8 mesh_handle);
1969 struct adv_info *hci_add_per_instance(struct hci_dev *hdev, u8 instance, u8 sid,
1970 				      u32 flags, u8 data_len, u8 *data,
1971 				      u32 min_interval, u32 max_interval);
1972 int hci_set_adv_instance_data(struct hci_dev *hdev, u8 instance,
1973 			 u16 adv_data_len, u8 *adv_data,
1974 			 u16 scan_rsp_len, u8 *scan_rsp_data);
1975 int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
1976 void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired);
1977 u32 hci_adv_instance_flags(struct hci_dev *hdev, u8 instance);
1978 bool hci_adv_instance_is_scannable(struct hci_dev *hdev, u8 instance);
1979 
1980 void hci_adv_monitors_clear(struct hci_dev *hdev);
1981 void hci_free_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1982 int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1983 int hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle);
1984 int hci_remove_all_adv_monitor(struct hci_dev *hdev);
1985 bool hci_is_adv_monitoring(struct hci_dev *hdev);
1986 int hci_get_adv_monitor_offload_ext(struct hci_dev *hdev);
1987 
1988 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
1989 
1990 void hci_init_sysfs(struct hci_dev *hdev);
1991 void hci_conn_init_sysfs(struct hci_conn *conn);
1992 void hci_conn_add_sysfs(struct hci_conn *conn);
1993 void hci_conn_del_sysfs(struct hci_conn *conn);
1994 
1995 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
1996 #define GET_HCIDEV_DEV(hdev) ((hdev)->dev.parent)
1997 
1998 /* ----- LMP capabilities ----- */
1999 #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
2000 #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
2001 #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
2002 #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
2003 #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
2004 #define lmp_sco_capable(dev)       ((dev)->features[0][1] & LMP_SCO)
2005 #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
2006 #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
2007 #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
2008 #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
2009 #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
2010 #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
2011 #define lmp_esco_2m_capable(dev)   ((dev)->features[0][5] & LMP_EDR_ESCO_2M)
2012 #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
2013 #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
2014 #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
2015 #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
2016 #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
2017 #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
2018 #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
2019 #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
2020 #define lmp_edr_2m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_2M)
2021 #define lmp_edr_3m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_3M)
2022 #define lmp_edr_3slot_capable(dev) ((dev)->features[0][4] & LMP_EDR_3SLOT)
2023 #define lmp_edr_5slot_capable(dev) ((dev)->features[0][5] & LMP_EDR_5SLOT)
2024 
2025 /* ----- Extended LMP capabilities ----- */
2026 #define lmp_cpb_central_capable(dev) ((dev)->features[2][0] & LMP_CPB_CENTRAL)
2027 #define lmp_cpb_peripheral_capable(dev) ((dev)->features[2][0] & LMP_CPB_PERIPHERAL)
2028 #define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
2029 #define lmp_sync_scan_capable(dev)  ((dev)->features[2][0] & LMP_SYNC_SCAN)
2030 #define lmp_sc_capable(dev)         ((dev)->features[2][1] & LMP_SC)
2031 #define lmp_ping_capable(dev)       ((dev)->features[2][1] & LMP_PING)
2032 
2033 /* ----- Host capabilities ----- */
2034 #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
2035 #define lmp_host_sc_capable(dev)   ((dev)->features[1][0] & LMP_HOST_SC)
2036 #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
2037 #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
2038 
2039 #define hdev_is_powered(dev)   (test_bit(HCI_UP, &(dev)->flags) && \
2040 				!hci_dev_test_flag(dev, HCI_AUTO_OFF))
2041 #define bredr_sc_enabled(dev)  (lmp_sc_capable(dev) && \
2042 				hci_dev_test_flag(dev, HCI_SC_ENABLED))
2043 #define rpa_valid(dev)         (bacmp(&dev->rpa, BDADDR_ANY) && \
2044 				!hci_dev_test_flag(dev, HCI_RPA_EXPIRED))
2045 #define adv_rpa_valid(adv)     (bacmp(&adv->random_addr, BDADDR_ANY) && \
2046 				!adv->rpa_expired)
2047 #define le_enabled(dev)        (lmp_le_capable(dev) && \
2048 				hci_dev_test_flag(dev, HCI_LE_ENABLED))
2049 
2050 #define scan_1m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_1M) || \
2051 		      ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_1M))
2052 
2053 #define le_2m_capable(dev) (((dev)->le_features[1] & HCI_LE_PHY_2M))
2054 
2055 #define scan_2m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_2M) || \
2056 		      ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_2M))
2057 
2058 #define le_coded_capable(dev) (((dev)->le_features[1] & HCI_LE_PHY_CODED) && \
2059 			       !hci_test_quirk((dev), \
2060 					       HCI_QUIRK_BROKEN_LE_CODED))
2061 
2062 #define scan_coded(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_CODED) || \
2063 			 ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_CODED))
2064 
2065 #define ll_privacy_capable(dev) ((dev)->le_features[0] & HCI_LE_LL_PRIVACY)
2066 #define ll_privacy_enabled(dev) (le_enabled(dev) && ll_privacy_capable(dev))
2067 
2068 #define privacy_mode_capable(dev) (ll_privacy_capable(dev) && \
2069 				   ((dev)->commands[39] & 0x04))
2070 
2071 #define read_key_size_capable(dev) \
2072 	((dev)->commands[20] & 0x10 && \
2073 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE))
2074 
2075 #define read_voice_setting_capable(dev) \
2076 	((dev)->commands[9] & 0x04 && \
2077 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_READ_VOICE_SETTING))
2078 
2079 /* Use enhanced synchronous connection if command is supported and its quirk
2080  * has not been set.
2081  */
2082 #define enhanced_sync_conn_capable(dev) \
2083 	(((dev)->commands[29] & 0x08) && \
2084 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN))
2085 
2086 /* Use ext scanning if set ext scan param and ext scan enable is supported */
2087 #define use_ext_scan(dev) (((dev)->commands[37] & 0x20) && \
2088 			   ((dev)->commands[37] & 0x40) && \
2089 			   !hci_test_quirk((dev), HCI_QUIRK_BROKEN_EXT_SCAN))
2090 
2091 /* Use ext create connection if command is supported */
2092 #define use_ext_conn(dev) (((dev)->commands[37] & 0x80) && \
2093 	!hci_test_quirk((dev), HCI_QUIRK_BROKEN_EXT_CREATE_CONN))
2094 /* Extended advertising support */
2095 #define ext_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_EXT_ADV))
2096 
2097 /* Maximum advertising length */
2098 #define max_adv_len(dev) \
2099 	(ext_adv_capable(dev) ? HCI_MAX_EXT_AD_LENGTH : HCI_MAX_AD_LENGTH)
2100 
2101 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 1789:
2102  *
2103  * C24: Mandatory if the LE Controller supports Connection State and either
2104  * LE Feature (LL Privacy) or LE Feature (Extended Advertising) is supported
2105  */
2106 #define use_enhanced_conn_complete(dev) ((ll_privacy_capable(dev) || \
2107 					 ext_adv_capable(dev)) && \
2108 					 !hci_test_quirk((dev), \
2109 							 HCI_QUIRK_BROKEN_EXT_CREATE_CONN))
2110 
2111 /* Periodic advertising support */
2112 #define per_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_PERIODIC_ADV))
2113 
2114 /* CIS Master/Slave and BIS support */
2115 #define iso_capable(dev) (cis_capable(dev) || bis_capable(dev))
2116 #define iso_enabled(dev) (le_enabled(dev) && iso_capable(dev))
2117 #define cis_capable(dev) \
2118 	(cis_central_capable(dev) || cis_peripheral_capable(dev))
2119 #define cis_enabled(dev) (le_enabled(dev) && cis_capable(dev))
2120 #define cis_central_capable(dev) \
2121 	((dev)->le_features[3] & HCI_LE_CIS_CENTRAL)
2122 #define cis_central_enabled(dev) \
2123 	(le_enabled(dev) && cis_central_capable(dev))
2124 #define cis_peripheral_capable(dev) \
2125 	((dev)->le_features[3] & HCI_LE_CIS_PERIPHERAL)
2126 #define cis_peripheral_enabled(dev) \
2127 	(le_enabled(dev) && cis_peripheral_capable(dev))
2128 #define bis_capable(dev) ((dev)->le_features[3] & HCI_LE_ISO_BROADCASTER)
2129 #define bis_enabled(dev) (le_enabled(dev) && bis_capable(dev))
2130 #define sync_recv_capable(dev) \
2131 	((dev)->le_features[3] & HCI_LE_ISO_SYNC_RECEIVER)
2132 #define sync_recv_enabled(dev) (le_enabled(dev) && sync_recv_capable(dev))
2133 #define past_sender_capable(dev) \
2134 	((dev)->le_features[3] & HCI_LE_PAST_SENDER)
2135 #define past_receiver_capable(dev) \
2136 	((dev)->le_features[3] & HCI_LE_PAST_RECEIVER)
2137 #define past_capable(dev) \
2138 	(past_sender_capable(dev) || past_receiver_capable(dev))
2139 #define past_sender_enabled(dev) \
2140 	(le_enabled(dev) && past_sender_capable(dev))
2141 #define past_receiver_enabled(dev) \
2142 	(le_enabled(dev) && past_receiver_capable(dev))
2143 #define past_enabled(dev) \
2144 	(past_sender_enabled(dev) || past_receiver_enabled(dev))
2145 #define ll_ext_feature_capable(dev) \
2146 	((dev)->le_features[7] & HCI_LE_LL_EXT_FEATURE)
2147 
2148 /* Channel sounding support */
2149 #define le_cs_capable(dev) \
2150 	((dev)->le_features[5] & HCI_LE_CS)
2151 #define le_cs_host_capable(dev) \
2152 	((dev)->le_features[5] & HCI_LE_CS_HOST)
2153 
2154 #define le_sci_capable(dev) \
2155 	((dev)->le_features[9] & HCI_LE_SCI)
2156 #define le_sci_enabled(dev) \
2157 	(le_enabled(dev) && le_sci_capable(dev))
2158 
2159 #define mws_transport_config_capable(dev) (((dev)->commands[30] & 0x08) && \
2160 	(!hci_test_quirk((dev), HCI_QUIRK_BROKEN_MWS_TRANSPORT_CONFIG)))
2161 
2162 /* ----- HCI protocols ----- */
2163 #define HCI_PROTO_DEFER             0x01
2164 
2165 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
2166 					__u8 type, __u8 *flags)
2167 {
2168 	switch (type) {
2169 	case ACL_LINK:
2170 		return l2cap_connect_ind(hdev, bdaddr);
2171 
2172 	case SCO_LINK:
2173 	case ESCO_LINK:
2174 		return sco_connect_ind(hdev, bdaddr, flags);
2175 
2176 	case CIS_LINK:
2177 	case BIS_LINK:
2178 	case PA_LINK:
2179 		return iso_connect_ind(hdev, bdaddr, flags);
2180 
2181 	default:
2182 		BT_ERR("unknown link type %d", type);
2183 		return -EINVAL;
2184 	}
2185 }
2186 
2187 static inline int hci_proto_disconn_ind(struct hci_conn *conn)
2188 {
2189 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
2190 		return HCI_ERROR_REMOTE_USER_TERM;
2191 
2192 	return l2cap_disconn_ind(conn);
2193 }
2194 
2195 /* ----- HCI callbacks ----- */
2196 struct hci_cb {
2197 	struct list_head list;
2198 
2199 	char *name;
2200 
2201 	void (*connect_cfm)	(struct hci_conn *conn, __u8 status);
2202 	void (*disconn_cfm)	(struct hci_conn *conn, __u8 status);
2203 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
2204 								__u8 encrypt);
2205 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
2206 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
2207 };
2208 
2209 static inline void hci_connect_cfm(struct hci_conn *conn, __u8 status)
2210 {
2211 	struct hci_cb *cb;
2212 
2213 	mutex_lock(&hci_cb_list_lock);
2214 	list_for_each_entry(cb, &hci_cb_list, list) {
2215 		if (cb->connect_cfm)
2216 			cb->connect_cfm(conn, status);
2217 	}
2218 	mutex_unlock(&hci_cb_list_lock);
2219 
2220 	if (conn->connect_cfm_cb)
2221 		conn->connect_cfm_cb(conn, status);
2222 }
2223 
2224 static inline void hci_disconn_cfm(struct hci_conn *conn, __u8 reason)
2225 {
2226 	struct hci_cb *cb;
2227 
2228 	mutex_lock(&hci_cb_list_lock);
2229 	list_for_each_entry(cb, &hci_cb_list, list) {
2230 		if (cb->disconn_cfm)
2231 			cb->disconn_cfm(conn, reason);
2232 	}
2233 	mutex_unlock(&hci_cb_list_lock);
2234 
2235 	if (conn->disconn_cfm_cb)
2236 		conn->disconn_cfm_cb(conn, reason);
2237 }
2238 
2239 static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
2240 {
2241 	struct hci_cb *cb;
2242 	__u8 encrypt;
2243 
2244 	if (test_bit(HCI_CONN_ENCRYPT_PEND, conn->flags))
2245 		return;
2246 
2247 	encrypt = test_bit(HCI_CONN_ENCRYPT, conn->flags) ? 0x01 : 0x00;
2248 
2249 	mutex_lock(&hci_cb_list_lock);
2250 	list_for_each_entry(cb, &hci_cb_list, list) {
2251 		if (cb->security_cfm)
2252 			cb->security_cfm(conn, status, encrypt);
2253 	}
2254 	mutex_unlock(&hci_cb_list_lock);
2255 
2256 	if (conn->security_cfm_cb)
2257 		conn->security_cfm_cb(conn, status);
2258 }
2259 
2260 void mgmt_security_level_changed(struct hci_conn *conn);
2261 
2262 static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status)
2263 {
2264 	struct hci_cb *cb;
2265 	__u8 encrypt;
2266 
2267 	if (conn->state == BT_CONFIG) {
2268 		if (!status)
2269 			conn->state = BT_CONNECTED;
2270 
2271 		hci_connect_cfm(conn, status);
2272 		hci_conn_drop(conn);
2273 		return;
2274 	}
2275 
2276 	if (!test_bit(HCI_CONN_ENCRYPT, conn->flags))
2277 		encrypt = 0x00;
2278 	else if (test_bit(HCI_CONN_AES_CCM, conn->flags))
2279 		encrypt = 0x02;
2280 	else
2281 		encrypt = 0x01;
2282 
2283 	if (!status) {
2284 		/* Callers either set conn->sec_level = conn->pending_sec_level
2285 		 * unconditionally before calling this function, or never
2286 		 * reach here with status == 0 (e.g. conn->state == BT_CONFIG
2287 		 * returns early above, or status is actually an error).  So
2288 		 * conn->pending_sec_level is never greater than
2289 		 * conn->sec_level at this point; only the initial
2290 		 * BT_SECURITY_SDP -> BT_SECURITY_LOW bump can still happen
2291 		 * here.
2292 		 */
2293 		if (conn->sec_level == BT_SECURITY_SDP) {
2294 			conn->sec_level = BT_SECURITY_LOW;
2295 			mgmt_security_level_changed(conn);
2296 		}
2297 	}
2298 
2299 	mutex_lock(&hci_cb_list_lock);
2300 	list_for_each_entry(cb, &hci_cb_list, list) {
2301 		if (cb->security_cfm)
2302 			cb->security_cfm(conn, status, encrypt);
2303 	}
2304 	mutex_unlock(&hci_cb_list_lock);
2305 
2306 	if (conn->security_cfm_cb)
2307 		conn->security_cfm_cb(conn, status);
2308 }
2309 
2310 static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
2311 {
2312 	struct hci_cb *cb;
2313 
2314 	mutex_lock(&hci_cb_list_lock);
2315 	list_for_each_entry(cb, &hci_cb_list, list) {
2316 		if (cb->key_change_cfm)
2317 			cb->key_change_cfm(conn, status);
2318 	}
2319 	mutex_unlock(&hci_cb_list_lock);
2320 }
2321 
2322 static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
2323 								__u8 role)
2324 {
2325 	struct hci_cb *cb;
2326 
2327 	mutex_lock(&hci_cb_list_lock);
2328 	list_for_each_entry(cb, &hci_cb_list, list) {
2329 		if (cb->role_switch_cfm)
2330 			cb->role_switch_cfm(conn, status, role);
2331 	}
2332 	mutex_unlock(&hci_cb_list_lock);
2333 }
2334 
2335 static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
2336 {
2337 	if (addr_type != ADDR_LE_DEV_RANDOM)
2338 		return false;
2339 
2340 	if ((bdaddr->b[5] & 0xc0) == 0x40)
2341 	       return true;
2342 
2343 	return false;
2344 }
2345 
2346 static inline bool hci_is_identity_address(bdaddr_t *addr, u8 addr_type)
2347 {
2348 	if (addr_type == ADDR_LE_DEV_PUBLIC)
2349 		return true;
2350 
2351 	/* Check for Random Static address type */
2352 	if ((addr->b[5] & 0xc0) == 0xc0)
2353 		return true;
2354 
2355 	return false;
2356 }
2357 
2358 static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
2359 					  bdaddr_t *bdaddr, u8 addr_type)
2360 {
2361 	if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
2362 		return NULL;
2363 
2364 	return hci_find_irk_by_rpa(hdev, bdaddr);
2365 }
2366 
2367 static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,
2368 					u16 to_multiplier)
2369 {
2370 	if (min > max) {
2371 		BT_WARN("min %d > max %d", min, max);
2372 		return -EINVAL;
2373 	}
2374 
2375 	if (min < 6) {
2376 		BT_WARN("min %d < 6", min);
2377 		return -EINVAL;
2378 	}
2379 
2380 	if (max > 3200) {
2381 		BT_WARN("max %d > 3200", max);
2382 		return -EINVAL;
2383 	}
2384 
2385 	if (to_multiplier < 10) {
2386 		BT_WARN("to_multiplier %d < 10", to_multiplier);
2387 		return -EINVAL;
2388 	}
2389 
2390 	if (to_multiplier > 3200) {
2391 		BT_WARN("to_multiplier %d > 3200", to_multiplier);
2392 		return -EINVAL;
2393 	}
2394 
2395 	if (latency > 499) {
2396 		BT_WARN("latency %d > 499", latency);
2397 		return -EINVAL;
2398 	}
2399 
2400 	if ((latency + 1) * max >= to_multiplier * 4) {
2401 		BT_WARN("latency %d, max %d and timeout %d are incompatible",
2402 			latency, max, to_multiplier);
2403 		return -EINVAL;
2404 	}
2405 
2406 	return 0;
2407 }
2408 
2409 int hci_register_cb(struct hci_cb *hcb);
2410 int hci_unregister_cb(struct hci_cb *hcb);
2411 
2412 int hci_send_vendor_frame(struct hci_dev *hdev, struct iov_iter *iter);
2413 
2414 int __hci_cmd_send(struct hci_dev *hdev, u16 opcode, u32 plen,
2415 		   const void *param);
2416 
2417 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
2418 		 const void *param);
2419 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
2420 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
2421 void hci_send_iso(struct hci_conn *conn, struct sk_buff *skb);
2422 
2423 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
2424 void *hci_sent_cmd_data_len(struct hci_dev *hdev, __u16 opcode,
2425 			    size_t *len);
2426 void *hci_recv_event_data(struct hci_dev *hdev, __u8 event);
2427 
2428 u32 hci_conn_get_phy(struct hci_conn *conn);
2429 int hci_conn_set_phy(struct hci_conn *conn, u32 phys);
2430 
2431 /* ----- HCI Sockets ----- */
2432 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
2433 void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
2434 			 int flag, struct sock *skip_sk);
2435 void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
2436 void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
2437 				 void *data, u16 data_len, ktime_t tstamp,
2438 				 int flag, struct sock *skip_sk);
2439 
2440 void hci_sock_dev_event(struct hci_dev *hdev, int event);
2441 
2442 #define HCI_MGMT_VAR_LEN	BIT(0)
2443 #define HCI_MGMT_NO_HDEV	BIT(1)
2444 #define HCI_MGMT_UNTRUSTED	BIT(2)
2445 #define HCI_MGMT_UNCONFIGURED	BIT(3)
2446 #define HCI_MGMT_HDEV_OPTIONAL	BIT(4)
2447 
2448 struct hci_mgmt_handler {
2449 	int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
2450 		     u16 data_len);
2451 	size_t data_len;
2452 	unsigned long flags;
2453 };
2454 
2455 struct hci_mgmt_chan {
2456 	struct list_head list;
2457 	unsigned short channel;
2458 	size_t handler_count;
2459 	const struct hci_mgmt_handler *handlers;
2460 	void (*hdev_init) (struct sock *sk, struct hci_dev *hdev);
2461 };
2462 
2463 int hci_mgmt_chan_register(struct hci_mgmt_chan *c);
2464 void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c);
2465 
2466 /* Management interface */
2467 #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
2468 #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
2469 					 BIT(BDADDR_LE_RANDOM))
2470 #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
2471 					 BIT(BDADDR_LE_PUBLIC) | \
2472 					 BIT(BDADDR_LE_RANDOM))
2473 
2474 /* These LE scan and inquiry parameters were chosen according to LE General
2475  * Discovery Procedure specification.
2476  */
2477 #define DISCOV_LE_SCAN_WIN		0x0012 /* 11.25 msec */
2478 #define DISCOV_LE_SCAN_INT		0x0012 /* 11.25 msec */
2479 #define DISCOV_LE_SCAN_INT_FAST		0x0060 /* 60 msec */
2480 #define DISCOV_LE_SCAN_WIN_FAST		0x0030 /* 30 msec */
2481 #define DISCOV_LE_SCAN_INT_CONN		0x0060 /* 60 msec */
2482 #define DISCOV_LE_SCAN_WIN_CONN		0x0060 /* 60 msec */
2483 #define DISCOV_LE_SCAN_INT_SLOW1	0x0800 /* 1.28 sec */
2484 #define DISCOV_LE_SCAN_WIN_SLOW1	0x0012 /* 11.25 msec */
2485 #define DISCOV_LE_SCAN_INT_SLOW2	0x1000 /* 2.56 sec */
2486 #define DISCOV_LE_SCAN_WIN_SLOW2	0x0024 /* 22.5 msec */
2487 #define DISCOV_CODED_SCAN_INT_FAST	0x0120 /* 180 msec */
2488 #define DISCOV_CODED_SCAN_WIN_FAST	0x0090 /* 90 msec */
2489 #define DISCOV_CODED_SCAN_INT_SLOW1	0x1800 /* 3.84 sec */
2490 #define DISCOV_CODED_SCAN_WIN_SLOW1	0x0036 /* 33.75 msec */
2491 #define DISCOV_CODED_SCAN_INT_SLOW2	0x3000 /* 7.68 sec */
2492 #define DISCOV_CODED_SCAN_WIN_SLOW2	0x006c /* 67.5 msec */
2493 #define DISCOV_LE_TIMEOUT		10240	/* msec */
2494 #define DISCOV_INTERLEAVED_TIMEOUT	5120	/* msec */
2495 #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
2496 #define DISCOV_BREDR_INQUIRY_LEN	0x08
2497 #define DISCOV_LE_RESTART_DELAY		msecs_to_jiffies(200)	/* msec */
2498 #define DISCOV_LE_FAST_ADV_INT_MIN	0x00A0	/* 100 msec */
2499 #define DISCOV_LE_FAST_ADV_INT_MAX	0x00F0	/* 150 msec */
2500 #define DISCOV_LE_PER_ADV_INT_MIN	0x00A0	/* 200 msec */
2501 #define DISCOV_LE_PER_ADV_INT_MAX	0x00A0	/* 200 msec */
2502 #define DISCOV_LE_ADV_MESH_MIN		0x00A0  /* 100 msec */
2503 #define DISCOV_LE_ADV_MESH_MAX		0x00A0  /* 100 msec */
2504 #define INTERVAL_TO_MS(x)		(((x) * 10) / 0x10)
2505 
2506 #define NAME_RESOLVE_DURATION		msecs_to_jiffies(10240)	/* 10.24 sec */
2507 
2508 void mgmt_fill_version_info(void *ver);
2509 int mgmt_new_settings(struct hci_dev *hdev);
2510 void mgmt_index_added(struct hci_dev *hdev);
2511 void mgmt_index_removed(struct hci_dev *hdev);
2512 void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
2513 void mgmt_power_on(struct hci_dev *hdev, int err);
2514 void __mgmt_power_off(struct hci_dev *hdev);
2515 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
2516 		       bool persistent);
2517 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
2518 			   u8 *name, u8 name_len);
2519 u8 hci_to_mgmt_reason(u8 err);
2520 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
2521 			      u8 link_type, u8 addr_type, u8 reason,
2522 			      bool mgmt_connected);
2523 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
2524 			    u8 link_type, u8 addr_type, u8 status);
2525 void mgmt_connect_failed(struct hci_dev *hdev, struct hci_conn *conn,
2526 			 u8 status);
2527 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
2528 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2529 				  u8 status);
2530 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2531 				      u8 status);
2532 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2533 			      u8 link_type, u8 addr_type, u32 value,
2534 			      u8 confirm_hint);
2535 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2536 				     u8 link_type, u8 addr_type, u8 status);
2537 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2538 					 u8 link_type, u8 addr_type, u8 status);
2539 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2540 			      u8 link_type, u8 addr_type);
2541 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2542 				     u8 link_type, u8 addr_type, u8 status);
2543 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2544 					 u8 link_type, u8 addr_type, u8 status);
2545 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
2546 			     u8 link_type, u8 addr_type, u32 passkey,
2547 			     u8 entered);
2548 void mgmt_auth_failed(struct hci_conn *conn, u8 status);
2549 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
2550 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
2551 				    u8 status);
2552 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
2553 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2554 		       u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
2555 		       u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
2556 		       u64 instant);
2557 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2558 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
2559 void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
2560 void mgmt_suspending(struct hci_dev *hdev, u8 state);
2561 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
2562 		   u8 addr_type);
2563 bool mgmt_powering_down(struct hci_dev *hdev);
2564 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
2565 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent);
2566 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
2567 		   bool persistent);
2568 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
2569 			 u8 bdaddr_type, u8 store_hint, u16 min_interval,
2570 			 u16 max_interval, u16 latency, u16 timeout);
2571 void mgmt_smp_complete(struct hci_conn *conn, bool complete);
2572 bool mgmt_get_connectable(struct hci_dev *hdev);
2573 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev);
2574 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev,
2575 			    u8 instance);
2576 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
2577 			      u8 instance);
2578 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip);
2579 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
2580 				  bdaddr_t *bdaddr, u8 addr_type);
2581 void mgmt_conn_subrate_notify(struct hci_dev *hdev, struct hci_conn *conn,
2582 			      u8 status);
2583 
2584 int hci_abort_conn(struct hci_conn *conn, u8 reason);
2585 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
2586 		      u16 to_multiplier);
2587 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
2588 		      __u8 ltk[16], __u8 key_size);
2589 
2590 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
2591 			       u8 *bdaddr_type);
2592 
2593 #define SCO_AIRMODE_MASK       0x0003
2594 #define SCO_AIRMODE_CVSD       0x0000
2595 #define SCO_AIRMODE_TRANSP     0x0003
2596 
2597 #define LOCAL_CODEC_ACL_MASK	BIT(0)
2598 #define LOCAL_CODEC_SCO_MASK	BIT(1)
2599 
2600 #define TRANSPORT_TYPE_MAX	0x04
2601 
2602 #endif /* __HCI_CORE_H */
2603