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