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