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