xref: /openbmc/linux/include/net/bluetooth/hci_core.h (revision 4b4113d6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
32d0a0346SRon Shaffer    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds    This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds    it under the terms of the GNU General Public License version 2 as
91da177e4SLinus Torvalds    published by the Free Software Foundation;
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
121da177e4SLinus Torvalds    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131da177e4SLinus Torvalds    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
141da177e4SLinus Torvalds    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
151da177e4SLinus Torvalds    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
161da177e4SLinus Torvalds    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
171da177e4SLinus Torvalds    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
181da177e4SLinus Torvalds    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
211da177e4SLinus Torvalds    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
221da177e4SLinus Torvalds    SOFTWARE IS DISCLAIMED.
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #ifndef __HCI_CORE_H
261da177e4SLinus Torvalds #define __HCI_CORE_H
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <net/bluetooth/hci.h>
29f49daa81SMarcel Holtmann #include <net/bluetooth/hci_sock.h>
301da177e4SLinus Torvalds 
315e59b791SLuiz Augusto von Dentz /* HCI priority */
325e59b791SLuiz Augusto von Dentz #define HCI_PRIO_MAX	7
335e59b791SLuiz Augusto von Dentz 
341da177e4SLinus Torvalds /* HCI Core structures */
351da177e4SLinus Torvalds struct inquiry_data {
361da177e4SLinus Torvalds 	bdaddr_t	bdaddr;
371da177e4SLinus Torvalds 	__u8		pscan_rep_mode;
381da177e4SLinus Torvalds 	__u8		pscan_period_mode;
391da177e4SLinus Torvalds 	__u8		pscan_mode;
401da177e4SLinus Torvalds 	__u8		dev_class[3];
411ebb9252SMarcel Holtmann 	__le16		clock_offset;
421da177e4SLinus Torvalds 	__s8		rssi;
4341a96212SMarcel Holtmann 	__u8		ssp_mode;
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds struct inquiry_entry {
47561aafbcSJohan Hedberg 	struct list_head	all;		/* inq_cache.all */
48561aafbcSJohan Hedberg 	struct list_head	list;		/* unknown or resolve */
49561aafbcSJohan Hedberg 	enum {
50561aafbcSJohan Hedberg 		NAME_NOT_KNOWN,
51561aafbcSJohan Hedberg 		NAME_NEEDED,
52561aafbcSJohan Hedberg 		NAME_PENDING,
53561aafbcSJohan Hedberg 		NAME_KNOWN,
54561aafbcSJohan Hedberg 	} name_state;
551da177e4SLinus Torvalds 	__u32			timestamp;
561da177e4SLinus Torvalds 	struct inquiry_data	data;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
5930883512SJohan Hedberg struct discovery_state {
604aab14e5SAndre Guedes 	int			type;
61ff9ef578SJohan Hedberg 	enum {
62ff9ef578SJohan Hedberg 		DISCOVERY_STOPPED,
63ff9ef578SJohan Hedberg 		DISCOVERY_STARTING,
64343f935bSAndre Guedes 		DISCOVERY_FINDING,
6530dc78e1SJohan Hedberg 		DISCOVERY_RESOLVING,
66ff9ef578SJohan Hedberg 		DISCOVERY_STOPPING,
67ff9ef578SJohan Hedberg 	} state;
68561aafbcSJohan Hedberg 	struct list_head	all;	/* All devices found during inquiry */
69561aafbcSJohan Hedberg 	struct list_head	unknown;	/* Name state not known */
70561aafbcSJohan Hedberg 	struct list_head	resolve;	/* Name needs to be resolved */
711da177e4SLinus Torvalds 	__u32			timestamp;
72b9a6328fSJohan Hedberg 	bdaddr_t		last_adv_addr;
73b9a6328fSJohan Hedberg 	u8			last_adv_addr_type;
74ff5cd29fSJohan Hedberg 	s8			last_adv_rssi;
75c70a7e4cSMarcel Holtmann 	u32			last_adv_flags;
76b9a6328fSJohan Hedberg 	u8			last_adv_data[HCI_MAX_AD_LENGTH];
77b9a6328fSJohan Hedberg 	u8			last_adv_data_len;
78da25cf6aSMarcel Holtmann 	bool			report_invalid_rssi;
7982f8b651SJakub Pawlowski 	bool			result_filtering;
8037eab042SJakub Pawlowski 	s8			rssi;
8137eab042SJakub Pawlowski 	u16			uuid_count;
8237eab042SJakub Pawlowski 	u8			(*uuids)[16];
832d28cfe7SJakub Pawlowski 	unsigned long		scan_start;
842d28cfe7SJakub Pawlowski 	unsigned long		scan_duration;
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds struct hci_conn_hash {
881da177e4SLinus Torvalds 	struct list_head list;
891da177e4SLinus Torvalds 	unsigned int     acl_num;
90bd1eb66bSAndrei Emeltchenko 	unsigned int     amp_num;
911da177e4SLinus Torvalds 	unsigned int     sco_num;
92fcd89c09SVille Tervo 	unsigned int     le_num;
93f8218dc6SJohan Hedberg 	unsigned int     le_num_slave;
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
96f0358568SJohan Hedberg struct bdaddr_list {
97f0358568SJohan Hedberg 	struct list_head list;
98f0358568SJohan Hedberg 	bdaddr_t bdaddr;
99b9ee0a78SMarcel Holtmann 	u8 bdaddr_type;
100f0358568SJohan Hedberg };
1012aeb9a1aSJohan Hedberg 
1022aeb9a1aSJohan Hedberg struct bt_uuid {
1032aeb9a1aSJohan Hedberg 	struct list_head list;
1042aeb9a1aSJohan Hedberg 	u8 uuid[16];
10583be8ecaSJohan Hedberg 	u8 size;
1061aff6f09SJohan Hedberg 	u8 svc_hint;
1072aeb9a1aSJohan Hedberg };
1082aeb9a1aSJohan Hedberg 
1097ee4ea36SMarcel Holtmann struct smp_csrk {
1107ee4ea36SMarcel Holtmann 	bdaddr_t bdaddr;
1117ee4ea36SMarcel Holtmann 	u8 bdaddr_type;
1124cd3928aSJohan Hedberg 	u8 type;
1137ee4ea36SMarcel Holtmann 	u8 val[16];
1147ee4ea36SMarcel Holtmann };
1157ee4ea36SMarcel Holtmann 
116b899efafSVinicius Costa Gomes struct smp_ltk {
117b899efafSVinicius Costa Gomes 	struct list_head list;
118970d0f1bSJohan Hedberg 	struct rcu_head rcu;
119b899efafSVinicius Costa Gomes 	bdaddr_t bdaddr;
120b899efafSVinicius Costa Gomes 	u8 bdaddr_type;
121b899efafSVinicius Costa Gomes 	u8 authenticated;
122b899efafSVinicius Costa Gomes 	u8 type;
123b899efafSVinicius Costa Gomes 	u8 enc_size;
124b899efafSVinicius Costa Gomes 	__le16 ediv;
125fe39c7b2SMarcel Holtmann 	__le64 rand;
126b899efafSVinicius Costa Gomes 	u8 val[16];
12703c515d7SMarcel Holtmann };
128b899efafSVinicius Costa Gomes 
129970c4e46SJohan Hedberg struct smp_irk {
130970c4e46SJohan Hedberg 	struct list_head list;
131adae20cbSJohan Hedberg 	struct rcu_head rcu;
132970c4e46SJohan Hedberg 	bdaddr_t rpa;
133970c4e46SJohan Hedberg 	bdaddr_t bdaddr;
134970c4e46SJohan Hedberg 	u8 addr_type;
135970c4e46SJohan Hedberg 	u8 val[16];
136970c4e46SJohan Hedberg };
137970c4e46SJohan Hedberg 
13855ed8ca1SJohan Hedberg struct link_key {
13955ed8ca1SJohan Hedberg 	struct list_head list;
1400378b597SJohan Hedberg 	struct rcu_head rcu;
14155ed8ca1SJohan Hedberg 	bdaddr_t bdaddr;
14255ed8ca1SJohan Hedberg 	u8 type;
1439b3b4460SAndrei Emeltchenko 	u8 val[HCI_LINK_KEY_SIZE];
14455ed8ca1SJohan Hedberg 	u8 pin_len;
14555ed8ca1SJohan Hedberg };
14655ed8ca1SJohan Hedberg 
1472763eda6SSzymon Janc struct oob_data {
1482763eda6SSzymon Janc 	struct list_head list;
1492763eda6SSzymon Janc 	bdaddr_t bdaddr;
1506928a924SJohan Hedberg 	u8 bdaddr_type;
151f7697b16SMarcel Holtmann 	u8 present;
152519ca9d0SMarcel Holtmann 	u8 hash192[16];
15338da1703SJohan Hedberg 	u8 rand192[16];
154519ca9d0SMarcel Holtmann 	u8 hash256[16];
15538da1703SJohan Hedberg 	u8 rand256[16];
1562763eda6SSzymon Janc };
1572763eda6SSzymon Janc 
158203fea01SArman Uguray struct adv_info {
159d2609b34SFlorian Grandel 	struct list_head list;
160fffd38bcSFlorian Grandel 	bool pending;
161203fea01SArman Uguray 	__u8	instance;
162203fea01SArman Uguray 	__u32	flags;
163912098a6SArman Uguray 	__u16	timeout;
1645d900e46SFlorian Grandel 	__u16	remaining_time;
165d2609b34SFlorian Grandel 	__u16	duration;
166203fea01SArman Uguray 	__u16	adv_data_len;
167203fea01SArman Uguray 	__u8	adv_data[HCI_MAX_AD_LENGTH];
168203fea01SArman Uguray 	__u16	scan_rsp_len;
169203fea01SArman Uguray 	__u8	scan_rsp_data[HCI_MAX_AD_LENGTH];
170203fea01SArman Uguray };
171203fea01SArman Uguray 
172db25be66SFlorian Grandel #define HCI_MAX_ADV_INSTANCES		5
173d2609b34SFlorian Grandel #define HCI_DEFAULT_ADV_DURATION	2
174d2609b34SFlorian Grandel 
175490c5babSJohan Hedberg #define HCI_MAX_SHORT_NAME_LENGTH	10
176490c5babSJohan Hedberg 
177d6bfd59cSJohan Hedberg /* Default LE RPA expiry time, 15 minutes */
178d6bfd59cSJohan Hedberg #define HCI_DEFAULT_RPA_TIMEOUT		(15 * 60)
179d6bfd59cSJohan Hedberg 
18031ad1691SAndrzej Kaczmarek /* Default min/max age of connection information (1s/3s) */
18131ad1691SAndrzej Kaczmarek #define DEFAULT_CONN_INFO_MIN_AGE	1000
18231ad1691SAndrzej Kaczmarek #define DEFAULT_CONN_INFO_MAX_AGE	3000
18331ad1691SAndrzej Kaczmarek 
184903e4541SAndrei Emeltchenko struct amp_assoc {
185903e4541SAndrei Emeltchenko 	__u16	len;
186903e4541SAndrei Emeltchenko 	__u16	offset;
18793c284eeSAndrei Emeltchenko 	__u16	rem_len;
18893c284eeSAndrei Emeltchenko 	__u16	len_so_far;
189903e4541SAndrei Emeltchenko 	__u8	data[HCI_MAX_AMP_ASSOC_SIZE];
190903e4541SAndrei Emeltchenko };
191903e4541SAndrei Emeltchenko 
192d2c5d77fSJohan Hedberg #define HCI_MAX_PAGES	3
193cad718edSJohan Hedberg 
1941da177e4SLinus Torvalds struct hci_dev {
1951da177e4SLinus Torvalds 	struct list_head list;
19609fd0de5SGustavo F. Padovan 	struct mutex	lock;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	char		name[8];
1991da177e4SLinus Torvalds 	unsigned long	flags;
2001da177e4SLinus Torvalds 	__u16		id;
201c13854ceSMarcel Holtmann 	__u8		bus;
202943da25dSMarcel Holtmann 	__u8		dev_type;
2031da177e4SLinus Torvalds 	bdaddr_t	bdaddr;
204e30d3f5fSMarcel Holtmann 	bdaddr_t	setup_addr;
20524c457e2SMarcel Holtmann 	bdaddr_t	public_addr;
2067a4cd51dSMarcel Holtmann 	bdaddr_t	random_addr;
207d13eafceSMarcel Holtmann 	bdaddr_t	static_addr;
20856ed2cb8SJohan Hedberg 	__u8		adv_addr_type;
2091f6c6378SJohan Hedberg 	__u8		dev_name[HCI_MAX_NAME_LENGTH];
210490c5babSJohan Hedberg 	__u8		short_name[HCI_MAX_SHORT_NAME_LENGTH];
21180a1e1dbSJohan Hedberg 	__u8		eir[HCI_MAX_EIR_LENGTH];
212a9de9248SMarcel Holtmann 	__u8		dev_class[3];
2131aff6f09SJohan Hedberg 	__u8		major_class;
2141aff6f09SJohan Hedberg 	__u8		minor_class;
215d2c5d77fSJohan Hedberg 	__u8		max_page;
216cad718edSJohan Hedberg 	__u8		features[HCI_MAX_PAGES][8];
21760e77321SJohan Hedberg 	__u8		le_features[8];
218cf1d081fSJohan Hedberg 	__u8		le_white_list_size;
2199b008c04SJohan Hedberg 	__u8		le_states[8];
220a9de9248SMarcel Holtmann 	__u8		commands[64];
2211143e5a6SMarcel Holtmann 	__u8		hci_ver;
2221143e5a6SMarcel Holtmann 	__u16		hci_rev;
223d5859e22SJohan Hedberg 	__u8		lmp_ver;
2241143e5a6SMarcel Holtmann 	__u16		manufacturer;
2257d69230cSAndrei Emeltchenko 	__u16		lmp_subver;
2261da177e4SLinus Torvalds 	__u16		voice_setting;
227b4cb9fb2SMarcel Holtmann 	__u8		num_iac;
228c2f0f979SMarcel Holtmann 	__u8		stored_max_keys;
229c2f0f979SMarcel Holtmann 	__u8		stored_num_keys;
23017fa4b9dSJohan Hedberg 	__u8		io_capability;
23191c4e9b1SMarcel Holtmann 	__s8		inq_tx_power;
232f332ec66SJohan Hedberg 	__u16		page_scan_interval;
233f332ec66SJohan Hedberg 	__u16		page_scan_window;
234f332ec66SJohan Hedberg 	__u8		page_scan_type;
2353f959d46SMarcel Holtmann 	__u8		le_adv_channel_map;
236628531c9SGeorg Lukas 	__u16		le_adv_min_interval;
237628531c9SGeorg Lukas 	__u16		le_adv_max_interval;
238533553f8SMarcel Holtmann 	__u8		le_scan_type;
239bef64738SMarcel Holtmann 	__u16		le_scan_interval;
240bef64738SMarcel Holtmann 	__u16		le_scan_window;
2414e70c7e7SMarcel Holtmann 	__u16		le_conn_min_interval;
2424e70c7e7SMarcel Holtmann 	__u16		le_conn_max_interval;
24304fb7d90SMarcel Holtmann 	__u16		le_conn_latency;
24404fb7d90SMarcel Holtmann 	__u16		le_supv_timeout;
245a8e1bfaaSMarcel Holtmann 	__u16		le_def_tx_len;
246a8e1bfaaSMarcel Holtmann 	__u16		le_def_tx_time;
247a8e1bfaaSMarcel Holtmann 	__u16		le_max_tx_len;
248a8e1bfaaSMarcel Holtmann 	__u16		le_max_tx_time;
249a8e1bfaaSMarcel Holtmann 	__u16		le_max_rx_len;
250a8e1bfaaSMarcel Holtmann 	__u16		le_max_rx_time;
251b9a7a61eSLukasz Rymanowski 	__u16		discov_interleaved_timeout;
25231ad1691SAndrzej Kaczmarek 	__u16		conn_info_min_age;
25331ad1691SAndrzej Kaczmarek 	__u16		conn_info_max_age;
25406f5b778SMarcel Holtmann 	__u8		ssp_debug_mode;
255c7741d16SMarcel Holtmann 	__u8		hw_error_code;
25633f35721SJohan Hedberg 	__u32		clock;
257f332ec66SJohan Hedberg 
2582b9be137SMarcel Holtmann 	__u16		devid_source;
2592b9be137SMarcel Holtmann 	__u16		devid_vendor;
2602b9be137SMarcel Holtmann 	__u16		devid_product;
2612b9be137SMarcel Holtmann 	__u16		devid_version;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	__u16		pkt_type;
2645b7f9909SMarcel Holtmann 	__u16		esco_type;
2651da177e4SLinus Torvalds 	__u16		link_policy;
2661da177e4SLinus Torvalds 	__u16		link_mode;
2671da177e4SLinus Torvalds 
26804837f64SMarcel Holtmann 	__u32		idle_timeout;
26904837f64SMarcel Holtmann 	__u16		sniff_min_interval;
27004837f64SMarcel Holtmann 	__u16		sniff_max_interval;
27104837f64SMarcel Holtmann 
272928abaa7SAndrei Emeltchenko 	__u8		amp_status;
273928abaa7SAndrei Emeltchenko 	__u32		amp_total_bw;
274928abaa7SAndrei Emeltchenko 	__u32		amp_max_bw;
275928abaa7SAndrei Emeltchenko 	__u32		amp_min_latency;
276928abaa7SAndrei Emeltchenko 	__u32		amp_max_pdu;
277928abaa7SAndrei Emeltchenko 	__u8		amp_type;
278928abaa7SAndrei Emeltchenko 	__u16		amp_pal_cap;
279928abaa7SAndrei Emeltchenko 	__u16		amp_assoc_size;
280928abaa7SAndrei Emeltchenko 	__u32		amp_max_flush_to;
281928abaa7SAndrei Emeltchenko 	__u32		amp_be_flush_to;
282928abaa7SAndrei Emeltchenko 
283903e4541SAndrei Emeltchenko 	struct amp_assoc	loc_assoc;
284903e4541SAndrei Emeltchenko 
2851e89cffbSAndrei Emeltchenko 	__u8		flow_ctl_mode;
2861e89cffbSAndrei Emeltchenko 
2879f61656aSJohan Hedberg 	unsigned int	auto_accept_delay;
2889f61656aSJohan Hedberg 
2891da177e4SLinus Torvalds 	unsigned long	quirks;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	atomic_t	cmd_cnt;
2921da177e4SLinus Torvalds 	unsigned int	acl_cnt;
2931da177e4SLinus Torvalds 	unsigned int	sco_cnt;
2946ed58ec5SVille Tervo 	unsigned int	le_cnt;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	unsigned int	acl_mtu;
2971da177e4SLinus Torvalds 	unsigned int	sco_mtu;
2986ed58ec5SVille Tervo 	unsigned int	le_mtu;
2991da177e4SLinus Torvalds 	unsigned int	acl_pkts;
3001da177e4SLinus Torvalds 	unsigned int	sco_pkts;
3016ed58ec5SVille Tervo 	unsigned int	le_pkts;
3021da177e4SLinus Torvalds 
303350ee4cfSAndrei Emeltchenko 	__u16		block_len;
304350ee4cfSAndrei Emeltchenko 	__u16		block_mtu;
305350ee4cfSAndrei Emeltchenko 	__u16		num_blocks;
306350ee4cfSAndrei Emeltchenko 	__u16		block_cnt;
307350ee4cfSAndrei Emeltchenko 
3081da177e4SLinus Torvalds 	unsigned long	acl_last_tx;
3091da177e4SLinus Torvalds 	unsigned long	sco_last_tx;
3106ed58ec5SVille Tervo 	unsigned long	le_last_tx;
3111da177e4SLinus Torvalds 
312f48fd9c8SMarcel Holtmann 	struct workqueue_struct	*workqueue;
3136ead1bbcSJohan Hedberg 	struct workqueue_struct	*req_workqueue;
314f48fd9c8SMarcel Holtmann 
315ab81cbf9SJohan Hedberg 	struct work_struct	power_on;
3163243553fSJohan Hedberg 	struct delayed_work	power_off;
317c7741d16SMarcel Holtmann 	struct work_struct	error_reset;
318ab81cbf9SJohan Hedberg 
31916ab91abSJohan Hedberg 	__u16			discov_timeout;
32016ab91abSJohan Hedberg 	struct delayed_work	discov_off;
32116ab91abSJohan Hedberg 
3227d78525dSJohan Hedberg 	struct delayed_work	service_cache;
3237d78525dSJohan Hedberg 
32465cc2b49SMarcel Holtmann 	struct delayed_work	cmd_timer;
325b78752ccSMarcel Holtmann 
326b78752ccSMarcel Holtmann 	struct work_struct	rx_work;
327c347b765SGustavo F. Padovan 	struct work_struct	cmd_work;
3283eff45eaSGustavo F. Padovan 	struct work_struct	tx_work;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	struct sk_buff_head	rx_q;
3311da177e4SLinus Torvalds 	struct sk_buff_head	raw_q;
3321da177e4SLinus Torvalds 	struct sk_buff_head	cmd_q;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	struct sk_buff		*sent_cmd;
3351da177e4SLinus Torvalds 
336a6a67efdSThomas Gleixner 	struct mutex		req_lock;
3371da177e4SLinus Torvalds 	wait_queue_head_t	req_wait_q;
3381da177e4SLinus Torvalds 	__u32			req_status;
3391da177e4SLinus Torvalds 	__u32			req_result;
340f60cb305SJohan Hedberg 	struct sk_buff		*req_skb;
341a5040efaSJohan Hedberg 
34270db83c4SJohan Hedberg 	void			*smp_data;
343ef8efe4bSJohan Hedberg 	void			*smp_bredr_data;
3442e58ef3eSJohan Hedberg 
34530883512SJohan Hedberg 	struct discovery_state	discovery;
3461da177e4SLinus Torvalds 	struct hci_conn_hash	conn_hash;
3475c136e90SAndre Guedes 
3485c136e90SAndre Guedes 	struct list_head	mgmt_pending;
349ea4bd8baSDavid Miller 	struct list_head	blacklist;
3506659358eSJohan Hedberg 	struct list_head	whitelist;
3512aeb9a1aSJohan Hedberg 	struct list_head	uuids;
35255ed8ca1SJohan Hedberg 	struct list_head	link_keys;
353b899efafSVinicius Costa Gomes 	struct list_head	long_term_keys;
354970c4e46SJohan Hedberg 	struct list_head	identity_resolving_keys;
3552763eda6SSzymon Janc 	struct list_head	remote_oob_data;
356d2ab0ac1SMarcel Holtmann 	struct list_head	le_white_list;
35715819a70SAndre Guedes 	struct list_head	le_conn_params;
35877a77a30SAndre Guedes 	struct list_head	pend_le_conns;
35966f8455aSJohan Hedberg 	struct list_head	pend_le_reports;
3602763eda6SSzymon Janc 
3611da177e4SLinus Torvalds 	struct hci_dev_stats	stat;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	atomic_t		promisc;
3641da177e4SLinus Torvalds 
365ca325f69SMarcel Holtmann 	struct dentry		*debugfs;
366ca325f69SMarcel Holtmann 
367a91f2e39SMarcel Holtmann 	struct device		dev;
3681da177e4SLinus Torvalds 
369611b30f7SMarcel Holtmann 	struct rfkill		*rfkill;
370611b30f7SMarcel Holtmann 
371eacb44dfSMarcel Holtmann 	DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS);
372d23264a8SAndre Guedes 
3737ba8b4beSAndre Guedes 	struct delayed_work	le_scan_disable;
3742d28cfe7SJakub Pawlowski 	struct delayed_work	le_scan_restart;
3757ba8b4beSAndre Guedes 
3768fa19098SJohan Hedberg 	__s8			adv_tx_power;
3773f0f524bSJohan Hedberg 	__u8			adv_data[HCI_MAX_AD_LENGTH];
3783f0f524bSJohan Hedberg 	__u8			adv_data_len;
379f8e808bdSMarcel Holtmann 	__u8			scan_rsp_data[HCI_MAX_AD_LENGTH];
380f8e808bdSMarcel Holtmann 	__u8			scan_rsp_data_len;
3818fa19098SJohan Hedberg 
382d2609b34SFlorian Grandel 	struct list_head	adv_instances;
383d2609b34SFlorian Grandel 	unsigned int		adv_instance_cnt;
384d2609b34SFlorian Grandel 	__u8			cur_adv_instance;
3855d900e46SFlorian Grandel 	__u16			adv_instance_timeout;
3865d900e46SFlorian Grandel 	struct delayed_work	adv_instance_expire;
387203fea01SArman Uguray 
388863efaf2SJohan Hedberg 	__u8			irk[16];
389d6bfd59cSJohan Hedberg 	__u32			rpa_timeout;
390d6bfd59cSJohan Hedberg 	struct delayed_work	rpa_expired;
3912b5224dcSMarcel Holtmann 	bdaddr_t		rpa;
392863efaf2SJohan Hedberg 
3931da177e4SLinus Torvalds 	int (*open)(struct hci_dev *hdev);
3941da177e4SLinus Torvalds 	int (*close)(struct hci_dev *hdev);
3951da177e4SLinus Torvalds 	int (*flush)(struct hci_dev *hdev);
396f41c70c4SMarcel Holtmann 	int (*setup)(struct hci_dev *hdev);
397a44fecbdSTedd Ho-Jeong An 	int (*shutdown)(struct hci_dev *hdev);
3987bd8f09fSMarcel Holtmann 	int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
3991da177e4SLinus Torvalds 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
400c7741d16SMarcel Holtmann 	void (*hw_error)(struct hci_dev *hdev, u8 code);
4014b4113d6SMarcel Holtmann 	int (*set_diag)(struct hci_dev *hdev, bool enable);
40224c457e2SMarcel Holtmann 	int (*set_bdaddr)(struct hci_dev *hdev, const bdaddr_t *bdaddr);
4031da177e4SLinus Torvalds };
4041da177e4SLinus Torvalds 
40553502d69SAndrei Emeltchenko #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
40653502d69SAndrei Emeltchenko 
4071da177e4SLinus Torvalds struct hci_conn {
4081da177e4SLinus Torvalds 	struct list_head list;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	atomic_t	refcnt;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	bdaddr_t	dst;
41329b7988aSAndre Guedes 	__u8		dst_type;
414662e8820SMarcel Holtmann 	bdaddr_t	src;
415e7c4096eSMarcel Holtmann 	__u8		src_type;
416cb1d68f7SJohan Hedberg 	bdaddr_t	init_addr;
417cb1d68f7SJohan Hedberg 	__u8		init_addr_type;
418cb1d68f7SJohan Hedberg 	bdaddr_t	resp_addr;
419cb1d68f7SJohan Hedberg 	__u8		resp_addr_type;
4201da177e4SLinus Torvalds 	__u16		handle;
4211da177e4SLinus Torvalds 	__u16		state;
42204837f64SMarcel Holtmann 	__u8		mode;
4231da177e4SLinus Torvalds 	__u8		type;
42440bef302SJohan Hedberg 	__u8		role;
425a0c808b3SJohan Hedberg 	bool		out;
4264c67bc74SMarcel Holtmann 	__u8		attempt;
4271da177e4SLinus Torvalds 	__u8		dev_class[3];
428cad718edSJohan Hedberg 	__u8		features[HCI_MAX_PAGES][8];
429a8746417SMarcel Holtmann 	__u16		pkt_type;
43004837f64SMarcel Holtmann 	__u16		link_policy;
43113d39315SWaldemar Rymarkiewicz 	__u8		key_type;
43240be492fSMarcel Holtmann 	__u8		auth_type;
4338c1b2355SMarcel Holtmann 	__u8		sec_level;
434765c2a96SJohan Hedberg 	__u8		pending_sec_level;
435980e1a53SJohan Hedberg 	__u8		pin_length;
436726b4ffcSVinicius Costa Gomes 	__u8		enc_key_size;
43717fa4b9dSJohan Hedberg 	__u8		io_capability;
43892a25256SJohan Hedberg 	__u32		passkey_notify;
43992a25256SJohan Hedberg 	__u8		passkey_entered;
440052b30b0SMarcel Holtmann 	__u16		disc_timeout;
44109ae260bSJohan Hedberg 	__u16		conn_timeout;
44210c62ddcSFrédéric Dalleau 	__u16		setting;
4431e406eefSAndre Guedes 	__u16		le_conn_min_interval;
4441e406eefSAndre Guedes 	__u16		le_conn_max_interval;
445e04fde60SMarcel Holtmann 	__u16		le_conn_interval;
446e04fde60SMarcel Holtmann 	__u16		le_conn_latency;
447e04fde60SMarcel Holtmann 	__u16		le_supv_timeout;
448fd45ada9SAlfonso Acosta 	__u8		le_adv_data[HCI_MAX_AD_LENGTH];
449fd45ada9SAlfonso Acosta 	__u8		le_adv_data_len;
4505ae76a94SAndrzej Kaczmarek 	__s8		rssi;
4515a134faeSAndrzej Kaczmarek 	__s8		tx_power;
452d0455ed9SAndrzej Kaczmarek 	__s8		max_tx_power;
45351a8efd7SJohan Hedberg 	unsigned long	flags;
4541da177e4SLinus Torvalds 
45533f35721SJohan Hedberg 	__u32		clock;
45633f35721SJohan Hedberg 	__u16		clock_accuracy;
45733f35721SJohan Hedberg 
458dd983808SAndrzej Kaczmarek 	unsigned long	conn_info_timestamp;
459dd983808SAndrzej Kaczmarek 
46003b555e1SJohan Hedberg 	__u8		remote_cap;
46103b555e1SJohan Hedberg 	__u8		remote_auth;
4623161ae1cSAndrei Emeltchenko 	__u8		remote_id;
46303b555e1SJohan Hedberg 
4641da177e4SLinus Torvalds 	unsigned int	sent;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	struct sk_buff_head data_q;
4672c33c06aSGustavo F. Padovan 	struct list_head chan_list;
4681da177e4SLinus Torvalds 
46919c40e3bSGustavo F. Padovan 	struct delayed_work disc_work;
4707bc18d9dSJohan Hedberg 	struct delayed_work auto_accept_work;
471a74a84f6SJohan Hedberg 	struct delayed_work idle_work;
4729489eca4SJohan Hedberg 	struct delayed_work le_conn_timeout;
4731da177e4SLinus Torvalds 
474b219e3acSMarcel Holtmann 	struct device	dev;
47523b9ceb7SMarcel Holtmann 	struct dentry	*debugfs;
476b219e3acSMarcel Holtmann 
4771da177e4SLinus Torvalds 	struct hci_dev	*hdev;
4781da177e4SLinus Torvalds 	void		*l2cap_data;
4791da177e4SLinus Torvalds 	void		*sco_data;
4809740e49dSAndrei Emeltchenko 	struct amp_mgr	*amp_mgr;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	struct hci_conn	*link;
483e9a416b5SJohan Hedberg 
484e9a416b5SJohan Hedberg 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
485e9a416b5SJohan Hedberg 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
486e9a416b5SJohan Hedberg 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
4871da177e4SLinus Torvalds };
4881da177e4SLinus Torvalds 
48973d80debSLuiz Augusto von Dentz struct hci_chan {
49073d80debSLuiz Augusto von Dentz 	struct list_head list;
49142c4e53eSAndrei Emeltchenko 	__u16 handle;
49273d80debSLuiz Augusto von Dentz 	struct hci_conn *conn;
49373d80debSLuiz Augusto von Dentz 	struct sk_buff_head data_q;
49473d80debSLuiz Augusto von Dentz 	unsigned int	sent;
495168df8e5SMat Martineau 	__u8		state;
49673d80debSLuiz Augusto von Dentz };
49773d80debSLuiz Augusto von Dentz 
49815819a70SAndre Guedes struct hci_conn_params {
49915819a70SAndre Guedes 	struct list_head list;
50093450c75SJohan Hedberg 	struct list_head action;
50115819a70SAndre Guedes 
50215819a70SAndre Guedes 	bdaddr_t addr;
50315819a70SAndre Guedes 	u8 addr_type;
50415819a70SAndre Guedes 
50515819a70SAndre Guedes 	u16 conn_min_interval;
50615819a70SAndre Guedes 	u16 conn_max_interval;
507f044eb05SMarcel Holtmann 	u16 conn_latency;
508f044eb05SMarcel Holtmann 	u16 supervision_timeout;
5099fcb18efSAndre Guedes 
5109fcb18efSAndre Guedes 	enum {
5119fcb18efSAndre Guedes 		HCI_AUTO_CONN_DISABLED,
512a3451d27SJohan Hedberg 		HCI_AUTO_CONN_REPORT,
5134b9e7e75SMarcel Holtmann 		HCI_AUTO_CONN_DIRECT,
5149fcb18efSAndre Guedes 		HCI_AUTO_CONN_ALWAYS,
5159fcb18efSAndre Guedes 		HCI_AUTO_CONN_LINK_LOSS,
516158e9218SJakub Pawlowski 		HCI_AUTO_CONN_EXPLICIT,
5179fcb18efSAndre Guedes 	} auto_connect;
518f161dd41SJohan Hedberg 
519f161dd41SJohan Hedberg 	struct hci_conn *conn;
520158e9218SJakub Pawlowski 	bool explicit_connect;
52115819a70SAndre Guedes };
52215819a70SAndre Guedes 
5231da177e4SLinus Torvalds extern struct list_head hci_dev_list;
5241da177e4SLinus Torvalds extern struct list_head hci_cb_list;
5251da177e4SLinus Torvalds extern rwlock_t hci_dev_list_lock;
526fba7ecf0SJohan Hedberg extern struct mutex hci_cb_list_lock;
5271da177e4SLinus Torvalds 
528eacb44dfSMarcel Holtmann #define hci_dev_set_flag(hdev, nr)             set_bit((nr), (hdev)->dev_flags)
529eacb44dfSMarcel Holtmann #define hci_dev_clear_flag(hdev, nr)           clear_bit((nr), (hdev)->dev_flags)
530eacb44dfSMarcel Holtmann #define hci_dev_change_flag(hdev, nr)          change_bit((nr), (hdev)->dev_flags)
531eacb44dfSMarcel Holtmann #define hci_dev_test_flag(hdev, nr)            test_bit((nr), (hdev)->dev_flags)
532eacb44dfSMarcel Holtmann #define hci_dev_test_and_set_flag(hdev, nr)    test_and_set_bit((nr), (hdev)->dev_flags)
533eacb44dfSMarcel Holtmann #define hci_dev_test_and_clear_flag(hdev, nr)  test_and_clear_bit((nr), (hdev)->dev_flags)
534eacb44dfSMarcel Holtmann #define hci_dev_test_and_change_flag(hdev, nr) test_and_change_bit((nr), (hdev)->dev_flags)
535d7a5a11dSMarcel Holtmann 
536eacb44dfSMarcel Holtmann #define hci_dev_clear_volatile_flags(hdev)			\
537eacb44dfSMarcel Holtmann 	do {							\
538eacb44dfSMarcel Holtmann 		hci_dev_clear_flag(hdev, HCI_LE_SCAN);		\
539eacb44dfSMarcel Holtmann 		hci_dev_clear_flag(hdev, HCI_LE_ADV);		\
540eacb44dfSMarcel Holtmann 		hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);	\
541eacb44dfSMarcel Holtmann 	} while (0)
542516018a9SMarcel Holtmann 
543686ebf28SUlisses Furquim /* ----- HCI interface to upper protocols ----- */
544e74e58f8SJoe Perches int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
545e74e58f8SJoe Perches int l2cap_disconn_ind(struct hci_conn *hcon);
5469b4c3336SArron Wang void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags);
547686ebf28SUlisses Furquim 
548ff50e8afSArron Wang #if IS_ENABLED(CONFIG_BT_BREDR)
549e74e58f8SJoe Perches int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
5509b4c3336SArron Wang void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
551ff50e8afSArron Wang #else
552ff50e8afSArron Wang static inline int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
553ff50e8afSArron Wang 				  __u8 *flags)
554ff50e8afSArron Wang {
555ff50e8afSArron Wang 	return 0;
556ff50e8afSArron Wang }
557ff50e8afSArron Wang 
558ff50e8afSArron Wang static inline void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
559ff50e8afSArron Wang {
560ff50e8afSArron Wang }
561ff50e8afSArron Wang #endif
562686ebf28SUlisses Furquim 
5631da177e4SLinus Torvalds /* ----- Inquiry cache ----- */
56470f23020SAndrei Emeltchenko #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
56570f23020SAndrei Emeltchenko #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
5661da177e4SLinus Torvalds 
56730883512SJohan Hedberg static inline void discovery_init(struct hci_dev *hdev)
5681da177e4SLinus Torvalds {
569ff9ef578SJohan Hedberg 	hdev->discovery.state = DISCOVERY_STOPPED;
57030883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.all);
57130883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.unknown);
57230883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.resolve);
573da25cf6aSMarcel Holtmann 	hdev->discovery.report_invalid_rssi = true;
57437eab042SJakub Pawlowski 	hdev->discovery.rssi = HCI_RSSI_INVALID;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5770256325eSMarcel Holtmann static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
5780256325eSMarcel Holtmann {
57982f8b651SJakub Pawlowski 	hdev->discovery.result_filtering = false;
580da25cf6aSMarcel Holtmann 	hdev->discovery.report_invalid_rssi = true;
5810256325eSMarcel Holtmann 	hdev->discovery.rssi = HCI_RSSI_INVALID;
5820256325eSMarcel Holtmann 	hdev->discovery.uuid_count = 0;
5830256325eSMarcel Holtmann 	kfree(hdev->discovery.uuids);
5840256325eSMarcel Holtmann 	hdev->discovery.uuids = NULL;
5852d28cfe7SJakub Pawlowski 	hdev->discovery.scan_start = 0;
5862d28cfe7SJakub Pawlowski 	hdev->discovery.scan_duration = 0;
5870256325eSMarcel Holtmann }
5880256325eSMarcel Holtmann 
58930dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev);
59030dc78e1SJohan Hedberg 
591ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state);
592ff9ef578SJohan Hedberg 
5931da177e4SLinus Torvalds static inline int inquiry_cache_empty(struct hci_dev *hdev)
5941da177e4SLinus Torvalds {
59530883512SJohan Hedberg 	return list_empty(&hdev->discovery.all);
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds static inline long inquiry_cache_age(struct hci_dev *hdev)
5991da177e4SLinus Torvalds {
60030883512SJohan Hedberg 	struct discovery_state *c = &hdev->discovery;
6011da177e4SLinus Torvalds 	return jiffies - c->timestamp;
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds static inline long inquiry_entry_age(struct inquiry_entry *e)
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds 	return jiffies - e->timestamp;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6095a9d0a3fSWaldemar Rymarkiewicz struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
6105a9d0a3fSWaldemar Rymarkiewicz 					       bdaddr_t *bdaddr);
611561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
612561aafbcSJohan Hedberg 						       bdaddr_t *bdaddr);
61330dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
61430dc78e1SJohan Hedberg 						       bdaddr_t *bdaddr,
61530dc78e1SJohan Hedberg 						       int state);
616a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
617a3d4e20aSJohan Hedberg 				      struct inquiry_entry *ie);
618af58925cSMarcel Holtmann u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
619af58925cSMarcel Holtmann 			     bool name_known);
6201f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /* ----- HCI Connections ----- */
6231da177e4SLinus Torvalds enum {
6241da177e4SLinus Torvalds 	HCI_CONN_AUTH_PEND,
62519f8def0SWaldemar Rymarkiewicz 	HCI_CONN_REAUTH_PEND,
6261da177e4SLinus Torvalds 	HCI_CONN_ENCRYPT_PEND,
62704837f64SMarcel Holtmann 	HCI_CONN_RSWITCH_PEND,
62804837f64SMarcel Holtmann 	HCI_CONN_MODE_CHANGE_PEND,
629e73439d8SMarcel Holtmann 	HCI_CONN_SCO_SETUP_PEND,
630b644ba33SJohan Hedberg 	HCI_CONN_MGMT_CONNECTED,
63158a681efSJohan Hedberg 	HCI_CONN_SSP_ENABLED,
632eb9a8f3fSMarcel Holtmann 	HCI_CONN_SC_ENABLED,
633abf76badSMarcel Holtmann 	HCI_CONN_AES_CCM,
63458a681efSJohan Hedberg 	HCI_CONN_POWER_SAVE,
635af6a9c32SJohan Hedberg 	HCI_CONN_FLUSH_KEY,
6364dae2798SJohan Hedberg 	HCI_CONN_ENCRYPT,
6374dae2798SJohan Hedberg 	HCI_CONN_AUTH,
6384dae2798SJohan Hedberg 	HCI_CONN_SECURE,
6394dae2798SJohan Hedberg 	HCI_CONN_FIPS,
640fe59a05fSJohan Hedberg 	HCI_CONN_STK_ENCRYPT,
641977f8fceSJohan Hedberg 	HCI_CONN_AUTH_INITIATOR,
642f94b665dSJohan Hedberg 	HCI_CONN_DROP,
64389cbb063SAlfonso Acosta 	HCI_CONN_PARAM_REMOVAL_PEND,
644fe8bc5acSJohan Hedberg 	HCI_CONN_NEW_LINK_KEY,
645158e9218SJakub Pawlowski 	HCI_CONN_SCANNING,
6461da177e4SLinus Torvalds };
6471da177e4SLinus Torvalds 
648aa64a8b5SJohan Hedberg static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
649aa64a8b5SJohan Hedberg {
650aa64a8b5SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
651d7a5a11dSMarcel Holtmann 	return hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
652c3c7ea65SGustavo Padovan 	       test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
653aa64a8b5SJohan Hedberg }
654aa64a8b5SJohan Hedberg 
655eb9a8f3fSMarcel Holtmann static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
656eb9a8f3fSMarcel Holtmann {
657eb9a8f3fSMarcel Holtmann 	struct hci_dev *hdev = conn->hdev;
658d7a5a11dSMarcel Holtmann 	return hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
659eb9a8f3fSMarcel Holtmann 	       test_bit(HCI_CONN_SC_ENABLED, &conn->flags);
660eb9a8f3fSMarcel Holtmann }
661eb9a8f3fSMarcel Holtmann 
6621da177e4SLinus Torvalds static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
665bf4c6325SGustavo F. Padovan 	list_add_rcu(&c->list, &h->list);
666fcd89c09SVille Tervo 	switch (c->type) {
667fcd89c09SVille Tervo 	case ACL_LINK:
6681da177e4SLinus Torvalds 		h->acl_num++;
669fcd89c09SVille Tervo 		break;
670bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
671bd1eb66bSAndrei Emeltchenko 		h->amp_num++;
672bd1eb66bSAndrei Emeltchenko 		break;
673fcd89c09SVille Tervo 	case LE_LINK:
674fcd89c09SVille Tervo 		h->le_num++;
675f8218dc6SJohan Hedberg 		if (c->role == HCI_ROLE_SLAVE)
676f8218dc6SJohan Hedberg 			h->le_num_slave++;
677fcd89c09SVille Tervo 		break;
678fcd89c09SVille Tervo 	case SCO_LINK:
679fcd89c09SVille Tervo 	case ESCO_LINK:
6801da177e4SLinus Torvalds 		h->sco_num++;
681fcd89c09SVille Tervo 		break;
682fcd89c09SVille Tervo 	}
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
688bf4c6325SGustavo F. Padovan 
689bf4c6325SGustavo F. Padovan 	list_del_rcu(&c->list);
690bf4c6325SGustavo F. Padovan 	synchronize_rcu();
691bf4c6325SGustavo F. Padovan 
692fcd89c09SVille Tervo 	switch (c->type) {
693fcd89c09SVille Tervo 	case ACL_LINK:
6941da177e4SLinus Torvalds 		h->acl_num--;
695fcd89c09SVille Tervo 		break;
696bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
697bd1eb66bSAndrei Emeltchenko 		h->amp_num--;
698bd1eb66bSAndrei Emeltchenko 		break;
699fcd89c09SVille Tervo 	case LE_LINK:
700fcd89c09SVille Tervo 		h->le_num--;
701f8218dc6SJohan Hedberg 		if (c->role == HCI_ROLE_SLAVE)
702f8218dc6SJohan Hedberg 			h->le_num_slave--;
703fcd89c09SVille Tervo 		break;
704fcd89c09SVille Tervo 	case SCO_LINK:
705fcd89c09SVille Tervo 	case ESCO_LINK:
7061da177e4SLinus Torvalds 		h->sco_num--;
707fcd89c09SVille Tervo 		break;
708fcd89c09SVille Tervo 	}
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
71152087a79SLuiz Augusto von Dentz static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
71252087a79SLuiz Augusto von Dentz {
71352087a79SLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
71452087a79SLuiz Augusto von Dentz 	switch (type) {
71552087a79SLuiz Augusto von Dentz 	case ACL_LINK:
71652087a79SLuiz Augusto von Dentz 		return h->acl_num;
717bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
718bd1eb66bSAndrei Emeltchenko 		return h->amp_num;
71952087a79SLuiz Augusto von Dentz 	case LE_LINK:
72052087a79SLuiz Augusto von Dentz 		return h->le_num;
72152087a79SLuiz Augusto von Dentz 	case SCO_LINK:
72252087a79SLuiz Augusto von Dentz 	case ESCO_LINK:
72352087a79SLuiz Augusto von Dentz 		return h->sco_num;
72452087a79SLuiz Augusto von Dentz 	default:
72552087a79SLuiz Augusto von Dentz 		return 0;
72652087a79SLuiz Augusto von Dentz 	}
72752087a79SLuiz Augusto von Dentz }
72852087a79SLuiz Augusto von Dentz 
729f4f07505SJohan Hedberg static inline unsigned int hci_conn_count(struct hci_dev *hdev)
730f4f07505SJohan Hedberg {
731f4f07505SJohan Hedberg 	struct hci_conn_hash *c = &hdev->conn_hash;
732f4f07505SJohan Hedberg 
733f4f07505SJohan Hedberg 	return c->acl_num + c->amp_num + c->sco_num + c->le_num;
734f4f07505SJohan Hedberg }
735f4f07505SJohan Hedberg 
736845472e8SMarcel Holtmann static inline __u8 hci_conn_lookup_type(struct hci_dev *hdev, __u16 handle)
737845472e8SMarcel Holtmann {
738845472e8SMarcel Holtmann 	struct hci_conn_hash *h = &hdev->conn_hash;
739845472e8SMarcel Holtmann 	struct hci_conn *c;
740845472e8SMarcel Holtmann 	__u8 type = INVALID_LINK;
741845472e8SMarcel Holtmann 
742845472e8SMarcel Holtmann 	rcu_read_lock();
743845472e8SMarcel Holtmann 
744845472e8SMarcel Holtmann 	list_for_each_entry_rcu(c, &h->list, list) {
745845472e8SMarcel Holtmann 		if (c->handle == handle) {
746845472e8SMarcel Holtmann 			type = c->type;
747845472e8SMarcel Holtmann 			break;
748845472e8SMarcel Holtmann 		}
749845472e8SMarcel Holtmann 	}
750845472e8SMarcel Holtmann 
751845472e8SMarcel Holtmann 	rcu_read_unlock();
752845472e8SMarcel Holtmann 
753845472e8SMarcel Holtmann 	return type;
754845472e8SMarcel Holtmann }
755845472e8SMarcel Holtmann 
7561da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
7571da177e4SLinus Torvalds 								__u16 handle)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
7601da177e4SLinus Torvalds 	struct hci_conn  *c;
7611da177e4SLinus Torvalds 
762bf4c6325SGustavo F. Padovan 	rcu_read_lock();
763bf4c6325SGustavo F. Padovan 
764bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
765bf4c6325SGustavo F. Padovan 		if (c->handle == handle) {
766bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
7671da177e4SLinus Torvalds 			return c;
7681da177e4SLinus Torvalds 		}
769bf4c6325SGustavo F. Padovan 	}
770bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
771bf4c6325SGustavo F. Padovan 
7721da177e4SLinus Torvalds 	return NULL;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
7761da177e4SLinus Torvalds 							__u8 type, bdaddr_t *ba)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
7791da177e4SLinus Torvalds 	struct hci_conn  *c;
7801da177e4SLinus Torvalds 
781bf4c6325SGustavo F. Padovan 	rcu_read_lock();
782bf4c6325SGustavo F. Padovan 
783bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
784bf4c6325SGustavo F. Padovan 		if (c->type == type && !bacmp(&c->dst, ba)) {
785bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
7861da177e4SLinus Torvalds 			return c;
7871da177e4SLinus Torvalds 		}
788bf4c6325SGustavo F. Padovan 	}
789bf4c6325SGustavo F. Padovan 
790bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
791bf4c6325SGustavo F. Padovan 
7921da177e4SLinus Torvalds 	return NULL;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7954c67bc74SMarcel Holtmann static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
7964c67bc74SMarcel Holtmann 							__u8 type, __u16 state)
7974c67bc74SMarcel Holtmann {
7984c67bc74SMarcel Holtmann 	struct hci_conn_hash *h = &hdev->conn_hash;
7994c67bc74SMarcel Holtmann 	struct hci_conn  *c;
8004c67bc74SMarcel Holtmann 
801bf4c6325SGustavo F. Padovan 	rcu_read_lock();
802bf4c6325SGustavo F. Padovan 
803bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
804bf4c6325SGustavo F. Padovan 		if (c->type == type && c->state == state) {
805bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
8064c67bc74SMarcel Holtmann 			return c;
8074c67bc74SMarcel Holtmann 		}
808bf4c6325SGustavo F. Padovan 	}
809bf4c6325SGustavo F. Padovan 
810bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
811bf4c6325SGustavo F. Padovan 
8124c67bc74SMarcel Holtmann 	return NULL;
8134c67bc74SMarcel Holtmann }
8144c67bc74SMarcel Holtmann 
815e7d9ab73SJakub Pawlowski static inline struct hci_conn *hci_lookup_le_connect(struct hci_dev *hdev)
816e7d9ab73SJakub Pawlowski {
817e7d9ab73SJakub Pawlowski 	struct hci_conn_hash *h = &hdev->conn_hash;
818e7d9ab73SJakub Pawlowski 	struct hci_conn  *c;
819e7d9ab73SJakub Pawlowski 
820e7d9ab73SJakub Pawlowski 	rcu_read_lock();
821e7d9ab73SJakub Pawlowski 
822e7d9ab73SJakub Pawlowski 	list_for_each_entry_rcu(c, &h->list, list) {
823e7d9ab73SJakub Pawlowski 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
824e7d9ab73SJakub Pawlowski 		    !test_bit(HCI_CONN_SCANNING, &c->flags)) {
825e7d9ab73SJakub Pawlowski 			rcu_read_unlock();
826e7d9ab73SJakub Pawlowski 			return c;
827e7d9ab73SJakub Pawlowski 		}
828e7d9ab73SJakub Pawlowski 	}
829e7d9ab73SJakub Pawlowski 
830e7d9ab73SJakub Pawlowski 	rcu_read_unlock();
831e7d9ab73SJakub Pawlowski 
832e7d9ab73SJakub Pawlowski 	return NULL;
833e7d9ab73SJakub Pawlowski }
834e7d9ab73SJakub Pawlowski 
835e3b679d5SJohan Hedberg int hci_disconnect(struct hci_conn *conn, __u8 reason);
8362dea632fSFrédéric Dalleau bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
837e73439d8SMarcel Holtmann void hci_sco_setup(struct hci_conn *conn, __u8 status);
8381da177e4SLinus Torvalds 
839a5c4e309SJohan Hedberg struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
840a5c4e309SJohan Hedberg 			      u8 role);
8411da177e4SLinus Torvalds int hci_conn_del(struct hci_conn *conn);
8421da177e4SLinus Torvalds void hci_conn_hash_flush(struct hci_dev *hdev);
843a9de9248SMarcel Holtmann void hci_conn_check_pending(struct hci_dev *hdev);
8441da177e4SLinus Torvalds 
84573d80debSLuiz Augusto von Dentz struct hci_chan *hci_chan_create(struct hci_conn *conn);
8469472007cSAndrei Emeltchenko void hci_chan_del(struct hci_chan *chan);
8472c33c06aSGustavo F. Padovan void hci_chan_list_flush(struct hci_conn *conn);
84842c4e53eSAndrei Emeltchenko struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
84973d80debSLuiz Augusto von Dentz 
850f75113a2SJakub Pawlowski struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
851f75113a2SJakub Pawlowski 				     u8 dst_type, u8 sec_level,
852f75113a2SJakub Pawlowski 				     u16 conn_timeout, u8 role);
85304a6c589SAndre Guedes struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
854cdd6275eSJohan Hedberg 				u8 dst_type, u8 sec_level, u16 conn_timeout,
855e804d25dSJohan Hedberg 				u8 role);
85604a6c589SAndre Guedes struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
85704a6c589SAndre Guedes 				 u8 sec_level, u8 auth_type);
85810c62ddcSFrédéric Dalleau struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
85910c62ddcSFrédéric Dalleau 				 __u16 setting);
860e7c29cb1SMarcel Holtmann int hci_conn_check_link_mode(struct hci_conn *conn);
861b3b1b061SWaldemar Rymarkiewicz int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
862e7cafc45SJohan Hedberg int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
863e7cafc45SJohan Hedberg 		      bool initiator);
8648c1b2355SMarcel Holtmann int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
8651da177e4SLinus Torvalds 
86614b12d0bSJaikumar Ganesh void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
8671da177e4SLinus Torvalds 
86806c053fbSAndre Guedes void hci_le_conn_failed(struct hci_conn *conn, u8 status);
86906c053fbSAndre Guedes 
8708d12356fSDavid Herrmann /*
8718d12356fSDavid Herrmann  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
8728d12356fSDavid Herrmann  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
8738d12356fSDavid Herrmann  * working or anything else. They just guarantee that the object is available
8748d12356fSDavid Herrmann  * and can be dereferenced. So you can use its locks, local variables and any
8758d12356fSDavid Herrmann  * other constant data.
8768d12356fSDavid Herrmann  * Before accessing runtime data, you _must_ lock the object and then check that
8778d12356fSDavid Herrmann  * it is still running. As soon as you release the locks, the connection might
8788d12356fSDavid Herrmann  * get dropped, though.
8798d12356fSDavid Herrmann  *
8808d12356fSDavid Herrmann  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
8818d12356fSDavid Herrmann  * how long the underlying connection is held. So every channel that runs on the
8828d12356fSDavid Herrmann  * hci_conn object calls this to prevent the connection from disappearing. As
8838d12356fSDavid Herrmann  * long as you hold a device, you must also guarantee that you have a valid
8848d12356fSDavid Herrmann  * reference to the device via hci_conn_get() (or the initial reference from
8858d12356fSDavid Herrmann  * hci_conn_add()).
8868d12356fSDavid Herrmann  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
8878d12356fSDavid Herrmann  * break because nobody cares for that. But this means, we cannot use
8888d12356fSDavid Herrmann  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
8898d12356fSDavid Herrmann  */
8908d12356fSDavid Herrmann 
89151bb8457SJohan Hedberg static inline struct hci_conn *hci_conn_get(struct hci_conn *conn)
8928d12356fSDavid Herrmann {
8938d12356fSDavid Herrmann 	get_device(&conn->dev);
89451bb8457SJohan Hedberg 	return conn;
8958d12356fSDavid Herrmann }
8968d12356fSDavid Herrmann 
8978d12356fSDavid Herrmann static inline void hci_conn_put(struct hci_conn *conn)
8988d12356fSDavid Herrmann {
8998d12356fSDavid Herrmann 	put_device(&conn->dev);
9008d12356fSDavid Herrmann }
9018d12356fSDavid Herrmann 
9021da177e4SLinus Torvalds static inline void hci_conn_hold(struct hci_conn *conn)
9031da177e4SLinus Torvalds {
90471becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
90538b3fef1SAndrei Emeltchenko 
9061da177e4SLinus Torvalds 	atomic_inc(&conn->refcnt);
9072f304d1eSAndre Guedes 	cancel_delayed_work(&conn->disc_work);
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
91076a68ba0SDavid Herrmann static inline void hci_conn_drop(struct hci_conn *conn)
9111da177e4SLinus Torvalds {
91271becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
91338b3fef1SAndrei Emeltchenko 
9141da177e4SLinus Torvalds 	if (atomic_dec_and_test(&conn->refcnt)) {
91504837f64SMarcel Holtmann 		unsigned long timeo;
916716e4ab5SAndrei Emeltchenko 
917716e4ab5SAndrei Emeltchenko 		switch (conn->type) {
918716e4ab5SAndrei Emeltchenko 		case ACL_LINK:
919716e4ab5SAndrei Emeltchenko 		case LE_LINK:
920a74a84f6SJohan Hedberg 			cancel_delayed_work(&conn->idle_work);
9216ac59344SMarcel Holtmann 			if (conn->state == BT_CONNECTED) {
9225f246e89SAndrei Emeltchenko 				timeo = conn->disc_timeout;
92304837f64SMarcel Holtmann 				if (!conn->out)
924052b30b0SMarcel Holtmann 					timeo *= 2;
9255a9d0a3fSWaldemar Rymarkiewicz 			} else {
926eb78d7e5SJohan Hedberg 				timeo = 0;
9275a9d0a3fSWaldemar Rymarkiewicz 			}
928716e4ab5SAndrei Emeltchenko 			break;
929716e4ab5SAndrei Emeltchenko 
930716e4ab5SAndrei Emeltchenko 		case AMP_LINK:
931716e4ab5SAndrei Emeltchenko 			timeo = conn->disc_timeout;
932716e4ab5SAndrei Emeltchenko 			break;
933716e4ab5SAndrei Emeltchenko 
934716e4ab5SAndrei Emeltchenko 		default:
935eb78d7e5SJohan Hedberg 			timeo = 0;
936716e4ab5SAndrei Emeltchenko 			break;
9375a9d0a3fSWaldemar Rymarkiewicz 		}
938716e4ab5SAndrei Emeltchenko 
9392f304d1eSAndre Guedes 		cancel_delayed_work(&conn->disc_work);
94019c40e3bSGustavo F. Padovan 		queue_delayed_work(conn->hdev->workqueue,
9411931782bSVinicius Costa Gomes 				   &conn->disc_work, timeo);
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds /* ----- HCI Devices ----- */
946dc946bd8SDavid Herrmann static inline void hci_dev_put(struct hci_dev *d)
9471da177e4SLinus Torvalds {
948376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
949376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
950376261aeSAndrei Emeltchenko 
9514c724c71SDavid Herrmann 	put_device(&d->dev);
952010666a1SDavid Herrmann }
9531da177e4SLinus Torvalds 
954dc946bd8SDavid Herrmann static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
9551da177e4SLinus Torvalds {
956376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
957376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
958376261aeSAndrei Emeltchenko 
9594c724c71SDavid Herrmann 	get_device(&d->dev);
9601da177e4SLinus Torvalds 	return d;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
96309fd0de5SGustavo F. Padovan #define hci_dev_lock(d)		mutex_lock(&d->lock)
96409fd0de5SGustavo F. Padovan #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
9651da177e4SLinus Torvalds 
966aa2b86d7SDavid Herrmann #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
9673dc07322SDavid Herrmann #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
968aa2b86d7SDavid Herrmann 
969155961e8SDavid Herrmann static inline void *hci_get_drvdata(struct hci_dev *hdev)
970155961e8SDavid Herrmann {
971155961e8SDavid Herrmann 	return dev_get_drvdata(&hdev->dev);
972155961e8SDavid Herrmann }
973155961e8SDavid Herrmann 
974155961e8SDavid Herrmann static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
975155961e8SDavid Herrmann {
976155961e8SDavid Herrmann 	dev_set_drvdata(&hdev->dev, data);
977155961e8SDavid Herrmann }
978155961e8SDavid Herrmann 
9791da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index);
9800c0afedfSJohan Hedberg struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds struct hci_dev *hci_alloc_dev(void);
9831da177e4SLinus Torvalds void hci_free_dev(struct hci_dev *hdev);
9841da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev);
98559735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev);
9861da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev);
9871da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev);
98875e0569fSMarcel Holtmann int hci_reset_dev(struct hci_dev *hdev);
9891da177e4SLinus Torvalds int hci_dev_open(__u16 dev);
9901da177e4SLinus Torvalds int hci_dev_close(__u16 dev);
9916b3cc1dbSSimon Fels int hci_dev_do_close(struct hci_dev *hdev);
9921da177e4SLinus Torvalds int hci_dev_reset(__u16 dev);
9931da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev);
9941da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg);
9951da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg);
9961da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg);
9971da177e4SLinus Torvalds int hci_get_conn_list(void __user *arg);
9981da177e4SLinus Torvalds int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
99940be492fSMarcel Holtmann int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
10001da177e4SLinus Torvalds int hci_inquiry(void __user *arg);
10011da177e4SLinus Torvalds 
1002dcc36c16SJohan Hedberg struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *list,
1003b9ee0a78SMarcel Holtmann 					   bdaddr_t *bdaddr, u8 type);
1004dcc36c16SJohan Hedberg int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1005dcc36c16SJohan Hedberg int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1006dcc36c16SJohan Hedberg void hci_bdaddr_list_clear(struct list_head *list);
1007d2ab0ac1SMarcel Holtmann 
100815819a70SAndre Guedes struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
100915819a70SAndre Guedes 					       bdaddr_t *addr, u8 addr_type);
101051d167c0SMarcel Holtmann struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
101151d167c0SMarcel Holtmann 					    bdaddr_t *addr, u8 addr_type);
101215819a70SAndre Guedes void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
1013373110c5SJohan Hedberg void hci_conn_params_clear_all(struct hci_dev *hdev);
101455af49a8SJohan Hedberg void hci_conn_params_clear_disabled(struct hci_dev *hdev);
101515819a70SAndre Guedes 
1016501f8827SJohan Hedberg struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
1017501f8827SJohan Hedberg 						  bdaddr_t *addr,
1018501f8827SJohan Hedberg 						  u8 addr_type);
1019f75113a2SJakub Pawlowski struct hci_conn_params *hci_explicit_connect_lookup(struct hci_dev *hdev,
1020f75113a2SJakub Pawlowski 						    bdaddr_t *addr,
1021f75113a2SJakub Pawlowski 						    u8 addr_type);
102277a77a30SAndre Guedes 
102335f7498aSJohan Hedberg void hci_uuids_clear(struct hci_dev *hdev);
10242aeb9a1aSJohan Hedberg 
102535f7498aSJohan Hedberg void hci_link_keys_clear(struct hci_dev *hdev);
102655ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1027567fa2aaSJohan Hedberg struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
10287652ff6aSJohan Hedberg 				  bdaddr_t *bdaddr, u8 *val, u8 type,
10297652ff6aSJohan Hedberg 				  u8 pin_len, bool *persistent);
1030ca9142b8SJohan Hedberg struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
103135d70271SJohan Hedberg 			    u8 addr_type, u8 type, u8 authenticated,
1032fe39c7b2SMarcel Holtmann 			    u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
1033f3a73d97SJohan Hedberg struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1034e804d25dSJohan Hedberg 			     u8 addr_type, u8 role);
1035e0b2b27eSJohan Hedberg int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
103635f7498aSJohan Hedberg void hci_smp_ltks_clear(struct hci_dev *hdev);
103755ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
103855ed8ca1SJohan Hedberg 
1039970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
1040970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1041970c4e46SJohan Hedberg 				     u8 addr_type);
1042ca9142b8SJohan Hedberg struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1043ca9142b8SJohan Hedberg 			    u8 addr_type, u8 val[16], bdaddr_t *rpa);
1044a7ec7338SJohan Hedberg void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
1045970c4e46SJohan Hedberg void hci_smp_irks_clear(struct hci_dev *hdev);
1046970c4e46SJohan Hedberg 
104755e76b38SJohan Hedberg bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
104855e76b38SJohan Hedberg 
104935f7498aSJohan Hedberg void hci_remote_oob_data_clear(struct hci_dev *hdev);
10502763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
10516928a924SJohan Hedberg 					  bdaddr_t *bdaddr, u8 bdaddr_type);
10520798872eSMarcel Holtmann int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
10536928a924SJohan Hedberg 			    u8 bdaddr_type, u8 *hash192, u8 *rand192,
105438da1703SJohan Hedberg 			    u8 *hash256, u8 *rand256);
10556928a924SJohan Hedberg int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
10566928a924SJohan Hedberg 			       u8 bdaddr_type);
10572763eda6SSzymon Janc 
1058d2609b34SFlorian Grandel void hci_adv_instances_clear(struct hci_dev *hdev);
1059d2609b34SFlorian Grandel struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance);
1060d2609b34SFlorian Grandel struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance);
1061d2609b34SFlorian Grandel int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags,
1062d2609b34SFlorian Grandel 			 u16 adv_data_len, u8 *adv_data,
1063d2609b34SFlorian Grandel 			 u16 scan_rsp_len, u8 *scan_rsp_data,
1064d2609b34SFlorian Grandel 			 u16 timeout, u16 duration);
1065d2609b34SFlorian Grandel int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
1066d2609b34SFlorian Grandel 
10671da177e4SLinus Torvalds void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
10681da177e4SLinus Torvalds 
1069e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
1070e875ff84SMarcel Holtmann int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb);
1071ef222013SMarcel Holtmann 
10720ac7e700SDavid Herrmann void hci_init_sysfs(struct hci_dev *hdev);
1073a67e899cSMarcel Holtmann void hci_conn_init_sysfs(struct hci_conn *conn);
1074b219e3acSMarcel Holtmann void hci_conn_add_sysfs(struct hci_conn *conn);
1075b219e3acSMarcel Holtmann void hci_conn_del_sysfs(struct hci_conn *conn);
10761da177e4SLinus Torvalds 
10776935e0f5SDavid Herrmann #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds /* ----- LMP capabilities ----- */
1080cad718edSJohan Hedberg #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
1081cad718edSJohan Hedberg #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
1082cad718edSJohan Hedberg #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
1083cad718edSJohan Hedberg #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
1084cad718edSJohan Hedberg #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
1085cad718edSJohan Hedberg #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
1086cad718edSJohan Hedberg #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
1087cad718edSJohan Hedberg #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
1088cad718edSJohan Hedberg #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
1089cad718edSJohan Hedberg #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
1090cad718edSJohan Hedberg #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
1091cad718edSJohan Hedberg #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
1092cad718edSJohan Hedberg #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
1093cad718edSJohan Hedberg #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
1094cad718edSJohan Hedberg #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
1095cad718edSJohan Hedberg #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
1096cad718edSJohan Hedberg #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
1097cad718edSJohan Hedberg #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
109807a5c61eSFrédéric Dalleau #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
10991da177e4SLinus Torvalds 
1100eead27daSAndre Guedes /* ----- Extended LMP capabilities ----- */
110153b834d2SMarcel Holtmann #define lmp_csb_master_capable(dev) ((dev)->features[2][0] & LMP_CSB_MASTER)
110253b834d2SMarcel Holtmann #define lmp_csb_slave_capable(dev)  ((dev)->features[2][0] & LMP_CSB_SLAVE)
110353b834d2SMarcel Holtmann #define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
110453b834d2SMarcel Holtmann #define lmp_sync_scan_capable(dev)  ((dev)->features[2][0] & LMP_SYNC_SCAN)
1105d5991585SMarcel Holtmann #define lmp_sc_capable(dev)         ((dev)->features[2][1] & LMP_SC)
1106d5991585SMarcel Holtmann #define lmp_ping_capable(dev)       ((dev)->features[2][1] & LMP_PING)
110753b834d2SMarcel Holtmann 
110853b834d2SMarcel Holtmann /* ----- Host capabilities ----- */
1109cad718edSJohan Hedberg #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
1110d5991585SMarcel Holtmann #define lmp_host_sc_capable(dev)   ((dev)->features[1][0] & LMP_HOST_SC)
1111cad718edSJohan Hedberg #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
1112cad718edSJohan Hedberg #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
1113eead27daSAndre Guedes 
1114d7a5a11dSMarcel Holtmann #define hdev_is_powered(dev)   (test_bit(HCI_UP, &(dev)->flags) && \
1115d7a5a11dSMarcel Holtmann 				!hci_dev_test_flag(dev, HCI_AUTO_OFF))
111605b3c3e7SMarcel Holtmann #define bredr_sc_enabled(dev)  (lmp_sc_capable(dev) && \
1117d7a5a11dSMarcel Holtmann 				hci_dev_test_flag(dev, HCI_SC_ENABLED))
1118432df05eSJohan Hedberg 
11191da177e4SLinus Torvalds /* ----- HCI protocols ----- */
112020714bfeSFrédéric Dalleau #define HCI_PROTO_DEFER             0x01
112120714bfeSFrédéric Dalleau 
11225a9d0a3fSWaldemar Rymarkiewicz static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
112320714bfeSFrédéric Dalleau 					__u8 type, __u8 *flags)
11241da177e4SLinus Torvalds {
1125686ebf28SUlisses Furquim 	switch (type) {
1126686ebf28SUlisses Furquim 	case ACL_LINK:
1127686ebf28SUlisses Furquim 		return l2cap_connect_ind(hdev, bdaddr);
11281da177e4SLinus Torvalds 
1129686ebf28SUlisses Furquim 	case SCO_LINK:
1130686ebf28SUlisses Furquim 	case ESCO_LINK:
113120714bfeSFrédéric Dalleau 		return sco_connect_ind(hdev, bdaddr, flags);
11321da177e4SLinus Torvalds 
1133686ebf28SUlisses Furquim 	default:
1134686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", type);
1135686ebf28SUlisses Furquim 		return -EINVAL;
1136686ebf28SUlisses Furquim 	}
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
11392950f21aSMarcel Holtmann static inline int hci_proto_disconn_ind(struct hci_conn *conn)
11402950f21aSMarcel Holtmann {
1141686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
1142686ebf28SUlisses Furquim 		return HCI_ERROR_REMOTE_USER_TERM;
11432950f21aSMarcel Holtmann 
1144686ebf28SUlisses Furquim 	return l2cap_disconn_ind(conn);
11452950f21aSMarcel Holtmann }
11462950f21aSMarcel Holtmann 
11471da177e4SLinus Torvalds /* ----- HCI callbacks ----- */
11481da177e4SLinus Torvalds struct hci_cb {
11491da177e4SLinus Torvalds 	struct list_head list;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	char *name;
11521da177e4SLinus Torvalds 
1153539c496dSJohan Hedberg 	void (*connect_cfm)	(struct hci_conn *conn, __u8 status);
11543a6d576bSJohan Hedberg 	void (*disconn_cfm)	(struct hci_conn *conn, __u8 status);
11555a9d0a3fSWaldemar Rymarkiewicz 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
11565a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt);
11571da177e4SLinus Torvalds 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
11581da177e4SLinus Torvalds 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
11591da177e4SLinus Torvalds };
11601da177e4SLinus Torvalds 
1161539c496dSJohan Hedberg static inline void hci_connect_cfm(struct hci_conn *conn, __u8 status)
1162539c496dSJohan Hedberg {
1163539c496dSJohan Hedberg 	struct hci_cb *cb;
1164539c496dSJohan Hedberg 
1165539c496dSJohan Hedberg 	mutex_lock(&hci_cb_list_lock);
1166539c496dSJohan Hedberg 	list_for_each_entry(cb, &hci_cb_list, list) {
1167539c496dSJohan Hedberg 		if (cb->connect_cfm)
1168539c496dSJohan Hedberg 			cb->connect_cfm(conn, status);
1169539c496dSJohan Hedberg 	}
1170539c496dSJohan Hedberg 	mutex_unlock(&hci_cb_list_lock);
1171539c496dSJohan Hedberg 
1172539c496dSJohan Hedberg 	if (conn->connect_cfm_cb)
1173539c496dSJohan Hedberg 		conn->connect_cfm_cb(conn, status);
1174539c496dSJohan Hedberg }
1175539c496dSJohan Hedberg 
11763a6d576bSJohan Hedberg static inline void hci_disconn_cfm(struct hci_conn *conn, __u8 reason)
11773a6d576bSJohan Hedberg {
11783a6d576bSJohan Hedberg 	struct hci_cb *cb;
11793a6d576bSJohan Hedberg 
11803a6d576bSJohan Hedberg 	mutex_lock(&hci_cb_list_lock);
11813a6d576bSJohan Hedberg 	list_for_each_entry(cb, &hci_cb_list, list) {
11823a6d576bSJohan Hedberg 		if (cb->disconn_cfm)
11833a6d576bSJohan Hedberg 			cb->disconn_cfm(conn, reason);
11843a6d576bSJohan Hedberg 	}
11853a6d576bSJohan Hedberg 	mutex_unlock(&hci_cb_list_lock);
11863a6d576bSJohan Hedberg 
11873a6d576bSJohan Hedberg 	if (conn->disconn_cfm_cb)
11883a6d576bSJohan Hedberg 		conn->disconn_cfm_cb(conn, reason);
11893a6d576bSJohan Hedberg }
11903a6d576bSJohan Hedberg 
11911da177e4SLinus Torvalds static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
11921da177e4SLinus Torvalds {
1193711584eaSDenis Kirjanov 	struct hci_cb *cb;
11948c1b2355SMarcel Holtmann 	__u8 encrypt;
11951da177e4SLinus Torvalds 
119651a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
11978c1b2355SMarcel Holtmann 		return;
11988c1b2355SMarcel Holtmann 
11994dae2798SJohan Hedberg 	encrypt = test_bit(HCI_CONN_ENCRYPT, &conn->flags) ? 0x01 : 0x00;
12008c1b2355SMarcel Holtmann 
1201fba7ecf0SJohan Hedberg 	mutex_lock(&hci_cb_list_lock);
1202711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
12038c1b2355SMarcel Holtmann 		if (cb->security_cfm)
12048c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
12051da177e4SLinus Torvalds 	}
1206fba7ecf0SJohan Hedberg 	mutex_unlock(&hci_cb_list_lock);
1207354fe804SJohan Hedberg 
1208354fe804SJohan Hedberg 	if (conn->security_cfm_cb)
1209354fe804SJohan Hedberg 		conn->security_cfm_cb(conn, status);
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
12125a9d0a3fSWaldemar Rymarkiewicz static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
12135a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
12141da177e4SLinus Torvalds {
1215711584eaSDenis Kirjanov 	struct hci_cb *cb;
12161da177e4SLinus Torvalds 
1217435fef20SMarcel Holtmann 	if (conn->sec_level == BT_SECURITY_SDP)
1218435fef20SMarcel Holtmann 		conn->sec_level = BT_SECURITY_LOW;
1219435fef20SMarcel Holtmann 
122088167aedSVinicius Costa Gomes 	if (conn->pending_sec_level > conn->sec_level)
122188167aedSVinicius Costa Gomes 		conn->sec_level = conn->pending_sec_level;
122288167aedSVinicius Costa Gomes 
1223fba7ecf0SJohan Hedberg 	mutex_lock(&hci_cb_list_lock);
1224711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
12258c1b2355SMarcel Holtmann 		if (cb->security_cfm)
12268c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
12271da177e4SLinus Torvalds 	}
1228fba7ecf0SJohan Hedberg 	mutex_unlock(&hci_cb_list_lock);
1229354fe804SJohan Hedberg 
1230354fe804SJohan Hedberg 	if (conn->security_cfm_cb)
1231354fe804SJohan Hedberg 		conn->security_cfm_cb(conn, status);
12321da177e4SLinus Torvalds }
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
12351da177e4SLinus Torvalds {
1236711584eaSDenis Kirjanov 	struct hci_cb *cb;
12371da177e4SLinus Torvalds 
1238fba7ecf0SJohan Hedberg 	mutex_lock(&hci_cb_list_lock);
1239711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
12401da177e4SLinus Torvalds 		if (cb->key_change_cfm)
12411da177e4SLinus Torvalds 			cb->key_change_cfm(conn, status);
12421da177e4SLinus Torvalds 	}
1243fba7ecf0SJohan Hedberg 	mutex_unlock(&hci_cb_list_lock);
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12465a9d0a3fSWaldemar Rymarkiewicz static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
12475a9d0a3fSWaldemar Rymarkiewicz 								__u8 role)
12481da177e4SLinus Torvalds {
1249711584eaSDenis Kirjanov 	struct hci_cb *cb;
12501da177e4SLinus Torvalds 
1251fba7ecf0SJohan Hedberg 	mutex_lock(&hci_cb_list_lock);
1252711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
12531da177e4SLinus Torvalds 		if (cb->role_switch_cfm)
12541da177e4SLinus Torvalds 			cb->role_switch_cfm(conn, status, role);
12551da177e4SLinus Torvalds 	}
1256fba7ecf0SJohan Hedberg 	mutex_unlock(&hci_cb_list_lock);
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds 
12596759a675SJohan Hedberg static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
12606759a675SJohan Hedberg {
126184d9d071SJohan Hedberg 	size_t parsed = 0;
12626759a675SJohan Hedberg 
12636c0c331eSJohan Hedberg 	if (data_len < 2)
12646c0c331eSJohan Hedberg 		return false;
12656c0c331eSJohan Hedberg 
126684d9d071SJohan Hedberg 	while (parsed < data_len - 1) {
126784d9d071SJohan Hedberg 		u8 field_len = data[0];
12686759a675SJohan Hedberg 
12696759a675SJohan Hedberg 		if (field_len == 0)
12706759a675SJohan Hedberg 			break;
12716759a675SJohan Hedberg 
12726759a675SJohan Hedberg 		parsed += field_len + 1;
12736759a675SJohan Hedberg 
12746759a675SJohan Hedberg 		if (parsed > data_len)
12756759a675SJohan Hedberg 			break;
12766759a675SJohan Hedberg 
12776759a675SJohan Hedberg 		if (data[1] == type)
12786759a675SJohan Hedberg 			return true;
12796759a675SJohan Hedberg 
12806759a675SJohan Hedberg 		data += field_len + 1;
12816759a675SJohan Hedberg 	}
12826759a675SJohan Hedberg 
12836759a675SJohan Hedberg 	return false;
12846759a675SJohan Hedberg }
12856759a675SJohan Hedberg 
1286301cb2d8SJohan Hedberg static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
1287301cb2d8SJohan Hedberg {
1288dbbfa2abSAndre Guedes 	if (addr_type != ADDR_LE_DEV_RANDOM)
1289301cb2d8SJohan Hedberg 		return false;
1290301cb2d8SJohan Hedberg 
1291301cb2d8SJohan Hedberg 	if ((bdaddr->b[5] & 0xc0) == 0x40)
1292301cb2d8SJohan Hedberg 	       return true;
1293301cb2d8SJohan Hedberg 
1294301cb2d8SJohan Hedberg 	return false;
1295301cb2d8SJohan Hedberg }
1296301cb2d8SJohan Hedberg 
1297c46245b3SJohan Hedberg static inline bool hci_is_identity_address(bdaddr_t *addr, u8 addr_type)
1298c46245b3SJohan Hedberg {
1299c46245b3SJohan Hedberg 	if (addr_type == ADDR_LE_DEV_PUBLIC)
1300c46245b3SJohan Hedberg 		return true;
1301c46245b3SJohan Hedberg 
1302c46245b3SJohan Hedberg 	/* Check for Random Static address type */
1303c46245b3SJohan Hedberg 	if ((addr->b[5] & 0xc0) == 0xc0)
1304c46245b3SJohan Hedberg 		return true;
1305c46245b3SJohan Hedberg 
1306c46245b3SJohan Hedberg 	return false;
1307c46245b3SJohan Hedberg }
1308c46245b3SJohan Hedberg 
13092426f3a5SJohan Hedberg static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
13102426f3a5SJohan Hedberg 					  bdaddr_t *bdaddr, u8 addr_type)
13112426f3a5SJohan Hedberg {
13122426f3a5SJohan Hedberg 	if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
13132426f3a5SJohan Hedberg 		return NULL;
13142426f3a5SJohan Hedberg 
13152426f3a5SJohan Hedberg 	return hci_find_irk_by_rpa(hdev, bdaddr);
13162426f3a5SJohan Hedberg }
13172426f3a5SJohan Hedberg 
1318d4905f24SAndre Guedes static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,
1319d4905f24SAndre Guedes 					u16 to_multiplier)
1320d4905f24SAndre Guedes {
1321d4905f24SAndre Guedes 	u16 max_latency;
1322d4905f24SAndre Guedes 
1323d4905f24SAndre Guedes 	if (min > max || min < 6 || max > 3200)
1324d4905f24SAndre Guedes 		return -EINVAL;
1325d4905f24SAndre Guedes 
1326d4905f24SAndre Guedes 	if (to_multiplier < 10 || to_multiplier > 3200)
1327d4905f24SAndre Guedes 		return -EINVAL;
1328d4905f24SAndre Guedes 
1329d4905f24SAndre Guedes 	if (max >= to_multiplier * 8)
1330d4905f24SAndre Guedes 		return -EINVAL;
1331d4905f24SAndre Guedes 
13328757825bSSeungyoun Ju 	max_latency = (to_multiplier * 4 / max) - 1;
1333d4905f24SAndre Guedes 	if (latency > 499 || latency > max_latency)
1334d4905f24SAndre Guedes 		return -EINVAL;
1335d4905f24SAndre Guedes 
1336d4905f24SAndre Guedes 	return 0;
1337d4905f24SAndre Guedes }
1338d4905f24SAndre Guedes 
13391da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *hcb);
13401da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *hcb);
13411da177e4SLinus Torvalds 
134275e84b7cSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
134307dc93ddSJohan Hedberg 			       const void *param, u32 timeout);
13447b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
134507dc93ddSJohan Hedberg 				  const void *param, u8 event, u32 timeout);
134675e84b7cSJohan Hedberg 
134707dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
134807dc93ddSJohan Hedberg 		 const void *param);
134973d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
13500d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
13511da177e4SLinus Torvalds 
1352a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
13531da177e4SLinus Torvalds 
1354fbef168fSLoic Poulain struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
1355fbef168fSLoic Poulain 			     const void *param, u32 timeout);
1356fbef168fSLoic Poulain 
13571da177e4SLinus Torvalds /* ----- HCI Sockets ----- */
1358470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
13597129069eSJohan Hedberg void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
1360c08b1a1dSMarcel Holtmann 			 int flag, struct sock *skip_sk);
1361cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
13621da177e4SLinus Torvalds 
1363040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event);
1364040030efSMarcel Holtmann 
1365a958452aSMarcel Holtmann #define HCI_MGMT_VAR_LEN	BIT(0)
1366a958452aSMarcel Holtmann #define HCI_MGMT_NO_HDEV	BIT(1)
1367a958452aSMarcel Holtmann #define HCI_MGMT_UNTRUSTED	BIT(2)
1368a958452aSMarcel Holtmann #define HCI_MGMT_UNCONFIGURED	BIT(3)
1369b9a245fbSJohan Hedberg 
1370801c1e8dSJohan Hedberg struct hci_mgmt_handler {
1371801c1e8dSJohan Hedberg 	int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
1372801c1e8dSJohan Hedberg 		     u16 data_len);
1373801c1e8dSJohan Hedberg 	size_t data_len;
1374b9a245fbSJohan Hedberg 	unsigned long flags;
1375801c1e8dSJohan Hedberg };
1376801c1e8dSJohan Hedberg 
1377801c1e8dSJohan Hedberg struct hci_mgmt_chan {
1378801c1e8dSJohan Hedberg 	struct list_head list;
1379801c1e8dSJohan Hedberg 	unsigned short channel;
1380801c1e8dSJohan Hedberg 	size_t handler_count;
1381801c1e8dSJohan Hedberg 	const struct hci_mgmt_handler *handlers;
138288b94ce9SJohan Hedberg 	void (*hdev_init) (struct sock *sk, struct hci_dev *hdev);
1383801c1e8dSJohan Hedberg };
1384801c1e8dSJohan Hedberg 
1385801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c);
1386801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c);
1387801c1e8dSJohan Hedberg 
13880381101fSJohan Hedberg /* Management interface */
1389591f47f3SAndre Guedes #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
1390591f47f3SAndre Guedes #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
1391591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1392591f47f3SAndre Guedes #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
1393591f47f3SAndre Guedes 					 BIT(BDADDR_LE_PUBLIC) | \
1394591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1395f39799f5SAndre Guedes 
13960d8cc935SAndre Guedes /* These LE scan and inquiry parameters were chosen according to LE General
13970d8cc935SAndre Guedes  * Discovery Procedure specification.
13980d8cc935SAndre Guedes  */
13990d8cc935SAndre Guedes #define DISCOV_LE_SCAN_WIN		0x12
14000d8cc935SAndre Guedes #define DISCOV_LE_SCAN_INT		0x12
14013d5a76f0SLukasz Rymanowski #define DISCOV_LE_TIMEOUT		10240	/* msec */
1402ae55f598SLukasz Rymanowski #define DISCOV_INTERLEAVED_TIMEOUT	5120	/* msec */
14030d8cc935SAndre Guedes #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
14040d8cc935SAndre Guedes #define DISCOV_BREDR_INQUIRY_LEN	0x08
14054b0e0cedSJakub Pawlowski #define DISCOV_LE_RESTART_DELAY		msecs_to_jiffies(200)	/* msec */
14060d8cc935SAndre Guedes 
140791a668b0SJohan Hedberg int mgmt_new_settings(struct hci_dev *hdev);
1408bf6b56dbSMarcel Holtmann void mgmt_index_added(struct hci_dev *hdev);
1409bf6b56dbSMarcel Holtmann void mgmt_index_removed(struct hci_dev *hdev);
14103eec705eSMarcel Holtmann void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
1411744cf19eSJohan Hedberg int mgmt_powered(struct hci_dev *hdev, u8 powered);
1412bc6d2d04SJohan Hedberg int mgmt_update_adv_data(struct hci_dev *hdev);
1413d1967ff8SMarcel Holtmann void mgmt_discoverable_timeout(struct hci_dev *hdev);
14145d900e46SFlorian Grandel void mgmt_adv_timeout_expired(struct hci_dev *hdev);
1415dc4a5ee2SMarcel Holtmann void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1416745c0ce3SVishal Agarwal 		       bool persistent);
141748ec92faSAlfonso Acosta void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
141848ec92faSAlfonso Acosta 			   u32 flags, u8 *name, u8 name_len);
14199b80ec5eSMarcel Holtmann void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
142012d4a3b2SJohan Hedberg 			      u8 link_type, u8 addr_type, u8 reason,
142112d4a3b2SJohan Hedberg 			      bool mgmt_connected);
14227892924cSMarcel Holtmann void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
142388c3df13SJohan Hedberg 			    u8 link_type, u8 addr_type, u8 status);
1424445608d0SMarcel Holtmann void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
142548264f06SJohan Hedberg 			 u8 addr_type, u8 status);
1426ce0e4a0dSMarcel Holtmann void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
1427e669cf80SMarcel Holtmann void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1428c35938b2SSzymon Janc 				  u8 status);
14293eb38528SMarcel Holtmann void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1430744cf19eSJohan Hedberg 				      u8 status);
1431744cf19eSJohan Hedberg int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
143239adbffeSJohan Hedberg 			      u8 link_type, u8 addr_type, u32 value,
1433272d90dfSJohan Hedberg 			      u8 confirm_hint);
1434744cf19eSJohan Hedberg int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1435272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1436272d90dfSJohan Hedberg int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1437272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
1438272d90dfSJohan Hedberg int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1439272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type);
1440604086b7SBrian Gix int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1441272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1442272d90dfSJohan Hedberg int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1443272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
144492a25256SJohan Hedberg int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
144592a25256SJohan Hedberg 			     u8 link_type, u8 addr_type, u32 passkey,
144692a25256SJohan Hedberg 			     u8 entered);
1447e1e930f5SJohan Hedberg void mgmt_auth_failed(struct hci_conn *conn, u8 status);
1448464996aeSMarcel Holtmann void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
14493e248560SMarcel Holtmann void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
14504e1b0245SMarcel Holtmann void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
14517f9a903cSMarcel Holtmann 				    u8 status);
14527667da34SMarcel Holtmann void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
1453901801b9SMarcel Holtmann void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1454af58925cSMarcel Holtmann 		       u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
1455af58925cSMarcel Holtmann 		       u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len);
14569cf12aeeSMarcel Holtmann void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1457b644ba33SJohan Hedberg 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
14582f1e063bSMarcel Holtmann void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
145984c61d92SJohan Hedberg bool mgmt_powering_down(struct hci_dev *hdev);
146053ac6ab6SMarcel Holtmann void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
146195fbac8aSJohan Hedberg void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk);
146253ac6ab6SMarcel Holtmann void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
146353ac6ab6SMarcel Holtmann 		   bool persistent);
1464ffb5a827SAndre Guedes void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
1465f4869e2aSJohan Hedberg 			 u8 bdaddr_type, u8 store_hint, u16 min_interval,
1466f4869e2aSJohan Hedberg 			 u16 max_interval, u16 latency, u16 timeout);
14675976e608SMarcel Holtmann void mgmt_reenable_advertising(struct hci_dev *hdev);
1468f4a407beSJohan Hedberg void mgmt_smp_complete(struct hci_conn *conn, bool complete);
1469346af67bSVinicius Costa Gomes 
14707d6ca693SJohan Hedberg u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
14717d6ca693SJohan Hedberg 		      u16 to_multiplier);
1472fe39c7b2SMarcel Holtmann void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
14738b76ce34SJohan Hedberg 		      __u8 ltk[16], __u8 key_size);
14742519a1fcSAndre Guedes 
1475a1f4c318SJohan Hedberg void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
1476a1f4c318SJohan Hedberg 			       u8 *bdaddr_type);
1477ebd3a747SJohan Hedberg 
14785d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_MASK       0x0003
14795d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_CVSD       0x0000
14805d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_TRANSP     0x0003
14815d4d62f6SFrédéric Dalleau 
14821da177e4SLinus Torvalds #endif /* __HCI_CORE_H */
1483