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