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