xref: /openbmc/linux/include/net/bluetooth/hci_core.h (revision 4e70c7e7)
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>
291da177e4SLinus Torvalds 
305e59b791SLuiz Augusto von Dentz /* HCI priority */
315e59b791SLuiz Augusto von Dentz #define HCI_PRIO_MAX	7
325e59b791SLuiz Augusto von Dentz 
331da177e4SLinus Torvalds /* HCI Core structures */
341da177e4SLinus Torvalds struct inquiry_data {
351da177e4SLinus Torvalds 	bdaddr_t	bdaddr;
361da177e4SLinus Torvalds 	__u8		pscan_rep_mode;
371da177e4SLinus Torvalds 	__u8		pscan_period_mode;
381da177e4SLinus Torvalds 	__u8		pscan_mode;
391da177e4SLinus Torvalds 	__u8		dev_class[3];
401ebb9252SMarcel Holtmann 	__le16		clock_offset;
411da177e4SLinus Torvalds 	__s8		rssi;
4241a96212SMarcel Holtmann 	__u8		ssp_mode;
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds struct inquiry_entry {
46561aafbcSJohan Hedberg 	struct list_head	all;		/* inq_cache.all */
47561aafbcSJohan Hedberg 	struct list_head	list;		/* unknown or resolve */
48561aafbcSJohan Hedberg 	enum {
49561aafbcSJohan Hedberg 		NAME_NOT_KNOWN,
50561aafbcSJohan Hedberg 		NAME_NEEDED,
51561aafbcSJohan Hedberg 		NAME_PENDING,
52561aafbcSJohan Hedberg 		NAME_KNOWN,
53561aafbcSJohan Hedberg 	} name_state;
541da177e4SLinus Torvalds 	__u32			timestamp;
551da177e4SLinus Torvalds 	struct inquiry_data	data;
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
5830883512SJohan Hedberg struct discovery_state {
594aab14e5SAndre Guedes 	int			type;
60ff9ef578SJohan Hedberg 	enum {
61ff9ef578SJohan Hedberg 		DISCOVERY_STOPPED,
62ff9ef578SJohan Hedberg 		DISCOVERY_STARTING,
63343f935bSAndre Guedes 		DISCOVERY_FINDING,
6430dc78e1SJohan Hedberg 		DISCOVERY_RESOLVING,
65ff9ef578SJohan Hedberg 		DISCOVERY_STOPPING,
66ff9ef578SJohan Hedberg 	} state;
67561aafbcSJohan Hedberg 	struct list_head	all;	/* All devices found during inquiry */
68561aafbcSJohan Hedberg 	struct list_head	unknown;	/* Name state not known */
69561aafbcSJohan Hedberg 	struct list_head	resolve;	/* Name needs to be resolved */
701da177e4SLinus Torvalds 	__u32			timestamp;
711da177e4SLinus Torvalds };
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds struct hci_conn_hash {
741da177e4SLinus Torvalds 	struct list_head list;
751da177e4SLinus Torvalds 	unsigned int     acl_num;
76bd1eb66bSAndrei Emeltchenko 	unsigned int     amp_num;
771da177e4SLinus Torvalds 	unsigned int     sco_num;
78fcd89c09SVille Tervo 	unsigned int     le_num;
791da177e4SLinus Torvalds };
801da177e4SLinus Torvalds 
81f0358568SJohan Hedberg struct bdaddr_list {
82f0358568SJohan Hedberg 	struct list_head list;
83f0358568SJohan Hedberg 	bdaddr_t bdaddr;
84b9ee0a78SMarcel Holtmann 	u8 bdaddr_type;
85f0358568SJohan Hedberg };
862aeb9a1aSJohan Hedberg 
872aeb9a1aSJohan Hedberg struct bt_uuid {
882aeb9a1aSJohan Hedberg 	struct list_head list;
892aeb9a1aSJohan Hedberg 	u8 uuid[16];
9083be8ecaSJohan Hedberg 	u8 size;
911aff6f09SJohan Hedberg 	u8 svc_hint;
922aeb9a1aSJohan Hedberg };
932aeb9a1aSJohan Hedberg 
94b899efafSVinicius Costa Gomes struct smp_ltk {
95b899efafSVinicius Costa Gomes 	struct list_head list;
96b899efafSVinicius Costa Gomes 	bdaddr_t bdaddr;
97b899efafSVinicius Costa Gomes 	u8 bdaddr_type;
98b899efafSVinicius Costa Gomes 	u8 authenticated;
99b899efafSVinicius Costa Gomes 	u8 type;
100b899efafSVinicius Costa Gomes 	u8 enc_size;
101b899efafSVinicius Costa Gomes 	__le16 ediv;
102b899efafSVinicius Costa Gomes 	u8 rand[8];
103b899efafSVinicius Costa Gomes 	u8 val[16];
104b899efafSVinicius Costa Gomes } __packed;
105b899efafSVinicius Costa Gomes 
10655ed8ca1SJohan Hedberg struct link_key {
10755ed8ca1SJohan Hedberg 	struct list_head list;
10855ed8ca1SJohan Hedberg 	bdaddr_t bdaddr;
10955ed8ca1SJohan Hedberg 	u8 type;
1109b3b4460SAndrei Emeltchenko 	u8 val[HCI_LINK_KEY_SIZE];
11155ed8ca1SJohan Hedberg 	u8 pin_len;
11255ed8ca1SJohan Hedberg };
11355ed8ca1SJohan Hedberg 
1142763eda6SSzymon Janc struct oob_data {
1152763eda6SSzymon Janc 	struct list_head list;
1162763eda6SSzymon Janc 	bdaddr_t bdaddr;
1172763eda6SSzymon Janc 	u8 hash[16];
1182763eda6SSzymon Janc 	u8 randomizer[16];
1192763eda6SSzymon Janc };
1202763eda6SSzymon Janc 
121490c5babSJohan Hedberg #define HCI_MAX_SHORT_NAME_LENGTH	10
122490c5babSJohan Hedberg 
123903e4541SAndrei Emeltchenko struct amp_assoc {
124903e4541SAndrei Emeltchenko 	__u16	len;
125903e4541SAndrei Emeltchenko 	__u16	offset;
12693c284eeSAndrei Emeltchenko 	__u16	rem_len;
12793c284eeSAndrei Emeltchenko 	__u16	len_so_far;
128903e4541SAndrei Emeltchenko 	__u8	data[HCI_MAX_AMP_ASSOC_SIZE];
129903e4541SAndrei Emeltchenko };
130903e4541SAndrei Emeltchenko 
131d2c5d77fSJohan Hedberg #define HCI_MAX_PAGES	3
132cad718edSJohan Hedberg 
133cd4c5391SSuraj Sumangala #define NUM_REASSEMBLY 4
1341da177e4SLinus Torvalds struct hci_dev {
1351da177e4SLinus Torvalds 	struct list_head list;
13609fd0de5SGustavo F. Padovan 	struct mutex	lock;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	char		name[8];
1391da177e4SLinus Torvalds 	unsigned long	flags;
1401da177e4SLinus Torvalds 	__u16		id;
141c13854ceSMarcel Holtmann 	__u8		bus;
142943da25dSMarcel Holtmann 	__u8		dev_type;
1431da177e4SLinus Torvalds 	bdaddr_t	bdaddr;
144d13eafceSMarcel Holtmann 	bdaddr_t	static_addr;
14579830f66SMarcel Holtmann 	__u8		own_addr_type;
1461f6c6378SJohan Hedberg 	__u8		dev_name[HCI_MAX_NAME_LENGTH];
147490c5babSJohan Hedberg 	__u8		short_name[HCI_MAX_SHORT_NAME_LENGTH];
14880a1e1dbSJohan Hedberg 	__u8		eir[HCI_MAX_EIR_LENGTH];
149a9de9248SMarcel Holtmann 	__u8		dev_class[3];
1501aff6f09SJohan Hedberg 	__u8		major_class;
1511aff6f09SJohan Hedberg 	__u8		minor_class;
152d2c5d77fSJohan Hedberg 	__u8		max_page;
153cad718edSJohan Hedberg 	__u8		features[HCI_MAX_PAGES][8];
15460e77321SJohan Hedberg 	__u8		le_features[8];
155cf1d081fSJohan Hedberg 	__u8		le_white_list_size;
1569b008c04SJohan Hedberg 	__u8		le_states[8];
157a9de9248SMarcel Holtmann 	__u8		commands[64];
1581143e5a6SMarcel Holtmann 	__u8		hci_ver;
1591143e5a6SMarcel Holtmann 	__u16		hci_rev;
160d5859e22SJohan Hedberg 	__u8		lmp_ver;
1611143e5a6SMarcel Holtmann 	__u16		manufacturer;
1627d69230cSAndrei Emeltchenko 	__u16		lmp_subver;
1631da177e4SLinus Torvalds 	__u16		voice_setting;
164b4cb9fb2SMarcel Holtmann 	__u8		num_iac;
16517fa4b9dSJohan Hedberg 	__u8		io_capability;
16691c4e9b1SMarcel Holtmann 	__s8		inq_tx_power;
167f332ec66SJohan Hedberg 	__u16		page_scan_interval;
168f332ec66SJohan Hedberg 	__u16		page_scan_window;
169f332ec66SJohan Hedberg 	__u8		page_scan_type;
170bef64738SMarcel Holtmann 	__u16		le_scan_interval;
171bef64738SMarcel Holtmann 	__u16		le_scan_window;
1724e70c7e7SMarcel Holtmann 	__u16		le_conn_min_interval;
1734e70c7e7SMarcel Holtmann 	__u16		le_conn_max_interval;
17406f5b778SMarcel Holtmann 	__u8		ssp_debug_mode;
175f332ec66SJohan Hedberg 
1762b9be137SMarcel Holtmann 	__u16		devid_source;
1772b9be137SMarcel Holtmann 	__u16		devid_vendor;
1782b9be137SMarcel Holtmann 	__u16		devid_product;
1792b9be137SMarcel Holtmann 	__u16		devid_version;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	__u16		pkt_type;
1825b7f9909SMarcel Holtmann 	__u16		esco_type;
1831da177e4SLinus Torvalds 	__u16		link_policy;
1841da177e4SLinus Torvalds 	__u16		link_mode;
1851da177e4SLinus Torvalds 
18604837f64SMarcel Holtmann 	__u32		idle_timeout;
18704837f64SMarcel Holtmann 	__u16		sniff_min_interval;
18804837f64SMarcel Holtmann 	__u16		sniff_max_interval;
18904837f64SMarcel Holtmann 
190928abaa7SAndrei Emeltchenko 	__u8		amp_status;
191928abaa7SAndrei Emeltchenko 	__u32		amp_total_bw;
192928abaa7SAndrei Emeltchenko 	__u32		amp_max_bw;
193928abaa7SAndrei Emeltchenko 	__u32		amp_min_latency;
194928abaa7SAndrei Emeltchenko 	__u32		amp_max_pdu;
195928abaa7SAndrei Emeltchenko 	__u8		amp_type;
196928abaa7SAndrei Emeltchenko 	__u16		amp_pal_cap;
197928abaa7SAndrei Emeltchenko 	__u16		amp_assoc_size;
198928abaa7SAndrei Emeltchenko 	__u32		amp_max_flush_to;
199928abaa7SAndrei Emeltchenko 	__u32		amp_be_flush_to;
200928abaa7SAndrei Emeltchenko 
201903e4541SAndrei Emeltchenko 	struct amp_assoc	loc_assoc;
202903e4541SAndrei Emeltchenko 
2031e89cffbSAndrei Emeltchenko 	__u8		flow_ctl_mode;
2041e89cffbSAndrei Emeltchenko 
2059f61656aSJohan Hedberg 	unsigned int	auto_accept_delay;
2069f61656aSJohan Hedberg 
2071da177e4SLinus Torvalds 	unsigned long	quirks;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	atomic_t	cmd_cnt;
2101da177e4SLinus Torvalds 	unsigned int	acl_cnt;
2111da177e4SLinus Torvalds 	unsigned int	sco_cnt;
2126ed58ec5SVille Tervo 	unsigned int	le_cnt;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	unsigned int	acl_mtu;
2151da177e4SLinus Torvalds 	unsigned int	sco_mtu;
2166ed58ec5SVille Tervo 	unsigned int	le_mtu;
2171da177e4SLinus Torvalds 	unsigned int	acl_pkts;
2181da177e4SLinus Torvalds 	unsigned int	sco_pkts;
2196ed58ec5SVille Tervo 	unsigned int	le_pkts;
2201da177e4SLinus Torvalds 
221350ee4cfSAndrei Emeltchenko 	__u16		block_len;
222350ee4cfSAndrei Emeltchenko 	__u16		block_mtu;
223350ee4cfSAndrei Emeltchenko 	__u16		num_blocks;
224350ee4cfSAndrei Emeltchenko 	__u16		block_cnt;
225350ee4cfSAndrei Emeltchenko 
2261da177e4SLinus Torvalds 	unsigned long	acl_last_tx;
2271da177e4SLinus Torvalds 	unsigned long	sco_last_tx;
2286ed58ec5SVille Tervo 	unsigned long	le_last_tx;
2291da177e4SLinus Torvalds 
230f48fd9c8SMarcel Holtmann 	struct workqueue_struct	*workqueue;
2316ead1bbcSJohan Hedberg 	struct workqueue_struct	*req_workqueue;
232f48fd9c8SMarcel Holtmann 
233ab81cbf9SJohan Hedberg 	struct work_struct	power_on;
2343243553fSJohan Hedberg 	struct delayed_work	power_off;
235ab81cbf9SJohan Hedberg 
23616ab91abSJohan Hedberg 	__u16			discov_timeout;
23716ab91abSJohan Hedberg 	struct delayed_work	discov_off;
23816ab91abSJohan Hedberg 
2397d78525dSJohan Hedberg 	struct delayed_work	service_cache;
2407d78525dSJohan Hedberg 
2416bd32326SVille Tervo 	struct timer_list	cmd_timer;
242b78752ccSMarcel Holtmann 
243b78752ccSMarcel Holtmann 	struct work_struct	rx_work;
244c347b765SGustavo F. Padovan 	struct work_struct	cmd_work;
2453eff45eaSGustavo F. Padovan 	struct work_struct	tx_work;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	struct sk_buff_head	rx_q;
2481da177e4SLinus Torvalds 	struct sk_buff_head	raw_q;
2491da177e4SLinus Torvalds 	struct sk_buff_head	cmd_q;
2501da177e4SLinus Torvalds 
251b6ddb638SJohan Hedberg 	struct sk_buff		*recv_evt;
2521da177e4SLinus Torvalds 	struct sk_buff		*sent_cmd;
253cd4c5391SSuraj Sumangala 	struct sk_buff		*reassembly[NUM_REASSEMBLY];
2541da177e4SLinus Torvalds 
255a6a67efdSThomas Gleixner 	struct mutex		req_lock;
2561da177e4SLinus Torvalds 	wait_queue_head_t	req_wait_q;
2571da177e4SLinus Torvalds 	__u32			req_status;
2581da177e4SLinus Torvalds 	__u32			req_result;
259a5040efaSJohan Hedberg 
2602e58ef3eSJohan Hedberg 	struct list_head	mgmt_pending;
2612e58ef3eSJohan Hedberg 
26230883512SJohan Hedberg 	struct discovery_state	discovery;
2631da177e4SLinus Torvalds 	struct hci_conn_hash	conn_hash;
264ea4bd8baSDavid Miller 	struct list_head	blacklist;
2651da177e4SLinus Torvalds 
2662aeb9a1aSJohan Hedberg 	struct list_head	uuids;
2672aeb9a1aSJohan Hedberg 
26855ed8ca1SJohan Hedberg 	struct list_head	link_keys;
26955ed8ca1SJohan Hedberg 
270b899efafSVinicius Costa Gomes 	struct list_head	long_term_keys;
271b899efafSVinicius Costa Gomes 
2722763eda6SSzymon Janc 	struct list_head	remote_oob_data;
2732763eda6SSzymon Janc 
2741da177e4SLinus Torvalds 	struct hci_dev_stats	stat;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	atomic_t		promisc;
2771da177e4SLinus Torvalds 
278ca325f69SMarcel Holtmann 	struct dentry		*debugfs;
279ca325f69SMarcel Holtmann 
280a91f2e39SMarcel Holtmann 	struct device		dev;
2811da177e4SLinus Torvalds 
282611b30f7SMarcel Holtmann 	struct rfkill		*rfkill;
283611b30f7SMarcel Holtmann 
284d23264a8SAndre Guedes 	unsigned long		dev_flags;
285d23264a8SAndre Guedes 
2867ba8b4beSAndre Guedes 	struct delayed_work	le_scan_disable;
2877ba8b4beSAndre Guedes 
2888fa19098SJohan Hedberg 	__s8			adv_tx_power;
2893f0f524bSJohan Hedberg 	__u8			adv_data[HCI_MAX_AD_LENGTH];
2903f0f524bSJohan Hedberg 	__u8			adv_data_len;
291f8e808bdSMarcel Holtmann 	__u8			scan_rsp_data[HCI_MAX_AD_LENGTH];
292f8e808bdSMarcel Holtmann 	__u8			scan_rsp_data_len;
2938fa19098SJohan Hedberg 
2941da177e4SLinus Torvalds 	int (*open)(struct hci_dev *hdev);
2951da177e4SLinus Torvalds 	int (*close)(struct hci_dev *hdev);
2961da177e4SLinus Torvalds 	int (*flush)(struct hci_dev *hdev);
297f41c70c4SMarcel Holtmann 	int (*setup)(struct hci_dev *hdev);
2987bd8f09fSMarcel Holtmann 	int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
2991da177e4SLinus Torvalds 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
3001da177e4SLinus Torvalds };
3011da177e4SLinus Torvalds 
30253502d69SAndrei Emeltchenko #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
30353502d69SAndrei Emeltchenko 
3041da177e4SLinus Torvalds struct hci_conn {
3051da177e4SLinus Torvalds 	struct list_head list;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	atomic_t	refcnt;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	bdaddr_t	dst;
31029b7988aSAndre Guedes 	__u8		dst_type;
311662e8820SMarcel Holtmann 	bdaddr_t	src;
312e7c4096eSMarcel Holtmann 	__u8		src_type;
3131da177e4SLinus Torvalds 	__u16		handle;
3141da177e4SLinus Torvalds 	__u16		state;
31504837f64SMarcel Holtmann 	__u8		mode;
3161da177e4SLinus Torvalds 	__u8		type;
317a0c808b3SJohan Hedberg 	bool		out;
3184c67bc74SMarcel Holtmann 	__u8		attempt;
3191da177e4SLinus Torvalds 	__u8		dev_class[3];
320cad718edSJohan Hedberg 	__u8		features[HCI_MAX_PAGES][8];
321a8746417SMarcel Holtmann 	__u16		pkt_type;
32204837f64SMarcel Holtmann 	__u16		link_policy;
3231da177e4SLinus Torvalds 	__u32		link_mode;
32413d39315SWaldemar Rymarkiewicz 	__u8		key_type;
32540be492fSMarcel Holtmann 	__u8		auth_type;
3268c1b2355SMarcel Holtmann 	__u8		sec_level;
327765c2a96SJohan Hedberg 	__u8		pending_sec_level;
328980e1a53SJohan Hedberg 	__u8		pin_length;
329726b4ffcSVinicius Costa Gomes 	__u8		enc_key_size;
33017fa4b9dSJohan Hedberg 	__u8		io_capability;
33192a25256SJohan Hedberg 	__u32		passkey_notify;
33292a25256SJohan Hedberg 	__u8		passkey_entered;
333052b30b0SMarcel Holtmann 	__u16		disc_timeout;
33410c62ddcSFrédéric Dalleau 	__u16		setting;
33551a8efd7SJohan Hedberg 	unsigned long	flags;
3361da177e4SLinus Torvalds 
33703b555e1SJohan Hedberg 	__u8		remote_cap;
33803b555e1SJohan Hedberg 	__u8		remote_auth;
3393161ae1cSAndrei Emeltchenko 	__u8		remote_id;
3406ec5bcadSVishal Agarwal 	bool		flush_key;
34103b555e1SJohan Hedberg 
3421da177e4SLinus Torvalds 	unsigned int	sent;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	struct sk_buff_head data_q;
3452c33c06aSGustavo F. Padovan 	struct list_head chan_list;
3461da177e4SLinus Torvalds 
34719c40e3bSGustavo F. Padovan 	struct delayed_work disc_work;
3487bc18d9dSJohan Hedberg 	struct delayed_work auto_accept_work;
349a74a84f6SJohan Hedberg 	struct delayed_work idle_work;
3501da177e4SLinus Torvalds 
351b219e3acSMarcel Holtmann 	struct device	dev;
352b219e3acSMarcel Holtmann 
3531da177e4SLinus Torvalds 	struct hci_dev	*hdev;
3541da177e4SLinus Torvalds 	void		*l2cap_data;
3551da177e4SLinus Torvalds 	void		*sco_data;
3562b64d153SBrian Gix 	void		*smp_conn;
3579740e49dSAndrei Emeltchenko 	struct amp_mgr	*amp_mgr;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	struct hci_conn	*link;
360e9a416b5SJohan Hedberg 
361e9a416b5SJohan Hedberg 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
362e9a416b5SJohan Hedberg 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
363e9a416b5SJohan Hedberg 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
3641da177e4SLinus Torvalds };
3651da177e4SLinus Torvalds 
36673d80debSLuiz Augusto von Dentz struct hci_chan {
36773d80debSLuiz Augusto von Dentz 	struct list_head list;
36842c4e53eSAndrei Emeltchenko 	__u16 handle;
36973d80debSLuiz Augusto von Dentz 	struct hci_conn *conn;
37073d80debSLuiz Augusto von Dentz 	struct sk_buff_head data_q;
37173d80debSLuiz Augusto von Dentz 	unsigned int	sent;
372168df8e5SMat Martineau 	__u8		state;
37373d80debSLuiz Augusto von Dentz };
37473d80debSLuiz Augusto von Dentz 
3751da177e4SLinus Torvalds extern struct list_head hci_dev_list;
3761da177e4SLinus Torvalds extern struct list_head hci_cb_list;
3771da177e4SLinus Torvalds extern rwlock_t hci_dev_list_lock;
3781da177e4SLinus Torvalds extern rwlock_t hci_cb_list_lock;
3791da177e4SLinus Torvalds 
380686ebf28SUlisses Furquim /* ----- HCI interface to upper protocols ----- */
381686ebf28SUlisses Furquim extern int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
3829e664631SAndrei Emeltchenko extern void l2cap_connect_cfm(struct hci_conn *hcon, u8 status);
383686ebf28SUlisses Furquim extern int l2cap_disconn_ind(struct hci_conn *hcon);
3849e664631SAndrei Emeltchenko extern void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason);
385686ebf28SUlisses Furquim extern int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
386c3c7ea65SGustavo Padovan extern int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb,
387c3c7ea65SGustavo Padovan 			      u16 flags);
388686ebf28SUlisses Furquim 
38920714bfeSFrédéric Dalleau extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
3909e664631SAndrei Emeltchenko extern void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
3919e664631SAndrei Emeltchenko extern void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
392686ebf28SUlisses Furquim extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
393686ebf28SUlisses Furquim 
3941da177e4SLinus Torvalds /* ----- Inquiry cache ----- */
39570f23020SAndrei Emeltchenko #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
39670f23020SAndrei Emeltchenko #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
3971da177e4SLinus Torvalds 
39830883512SJohan Hedberg static inline void discovery_init(struct hci_dev *hdev)
3991da177e4SLinus Torvalds {
400ff9ef578SJohan Hedberg 	hdev->discovery.state = DISCOVERY_STOPPED;
40130883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.all);
40230883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.unknown);
40330883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.resolve);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
40630dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev);
40730dc78e1SJohan Hedberg 
408ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state);
409ff9ef578SJohan Hedberg 
4101da177e4SLinus Torvalds static inline int inquiry_cache_empty(struct hci_dev *hdev)
4111da177e4SLinus Torvalds {
41230883512SJohan Hedberg 	return list_empty(&hdev->discovery.all);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static inline long inquiry_cache_age(struct hci_dev *hdev)
4161da177e4SLinus Torvalds {
41730883512SJohan Hedberg 	struct discovery_state *c = &hdev->discovery;
4181da177e4SLinus Torvalds 	return jiffies - c->timestamp;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds static inline long inquiry_entry_age(struct inquiry_entry *e)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	return jiffies - e->timestamp;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4265a9d0a3fSWaldemar Rymarkiewicz struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
4275a9d0a3fSWaldemar Rymarkiewicz 					       bdaddr_t *bdaddr);
428561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
429561aafbcSJohan Hedberg 						       bdaddr_t *bdaddr);
43030dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
43130dc78e1SJohan Hedberg 						       bdaddr_t *bdaddr,
43230dc78e1SJohan Hedberg 						       int state);
433a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
434a3d4e20aSJohan Hedberg 				      struct inquiry_entry *ie);
4353175405bSJohan Hedberg bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
436388fc8faSJohan Hedberg 			      bool name_known, bool *ssp);
4371f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds /* ----- HCI Connections ----- */
4401da177e4SLinus Torvalds enum {
4411da177e4SLinus Torvalds 	HCI_CONN_AUTH_PEND,
44219f8def0SWaldemar Rymarkiewicz 	HCI_CONN_REAUTH_PEND,
4431da177e4SLinus Torvalds 	HCI_CONN_ENCRYPT_PEND,
44404837f64SMarcel Holtmann 	HCI_CONN_RSWITCH_PEND,
44504837f64SMarcel Holtmann 	HCI_CONN_MODE_CHANGE_PEND,
446e73439d8SMarcel Holtmann 	HCI_CONN_SCO_SETUP_PEND,
447d26a2345SVinicius Costa Gomes 	HCI_CONN_LE_SMP_PEND,
448b644ba33SJohan Hedberg 	HCI_CONN_MGMT_CONNECTED,
44958a681efSJohan Hedberg 	HCI_CONN_SSP_ENABLED,
45058a681efSJohan Hedberg 	HCI_CONN_POWER_SAVE,
45158a681efSJohan Hedberg 	HCI_CONN_REMOTE_OOB,
4521da177e4SLinus Torvalds };
4531da177e4SLinus Torvalds 
454aa64a8b5SJohan Hedberg static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
455aa64a8b5SJohan Hedberg {
456aa64a8b5SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
457c3c7ea65SGustavo Padovan 	return test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
458c3c7ea65SGustavo Padovan 	       test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
459aa64a8b5SJohan Hedberg }
460aa64a8b5SJohan Hedberg 
4611da177e4SLinus Torvalds static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
464bf4c6325SGustavo F. Padovan 	list_add_rcu(&c->list, &h->list);
465fcd89c09SVille Tervo 	switch (c->type) {
466fcd89c09SVille Tervo 	case ACL_LINK:
4671da177e4SLinus Torvalds 		h->acl_num++;
468fcd89c09SVille Tervo 		break;
469bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
470bd1eb66bSAndrei Emeltchenko 		h->amp_num++;
471bd1eb66bSAndrei Emeltchenko 		break;
472fcd89c09SVille Tervo 	case LE_LINK:
473fcd89c09SVille Tervo 		h->le_num++;
474fcd89c09SVille Tervo 		break;
475fcd89c09SVille Tervo 	case SCO_LINK:
476fcd89c09SVille Tervo 	case ESCO_LINK:
4771da177e4SLinus Torvalds 		h->sco_num++;
478fcd89c09SVille Tervo 		break;
479fcd89c09SVille Tervo 	}
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
485bf4c6325SGustavo F. Padovan 
486bf4c6325SGustavo F. Padovan 	list_del_rcu(&c->list);
487bf4c6325SGustavo F. Padovan 	synchronize_rcu();
488bf4c6325SGustavo F. Padovan 
489fcd89c09SVille Tervo 	switch (c->type) {
490fcd89c09SVille Tervo 	case ACL_LINK:
4911da177e4SLinus Torvalds 		h->acl_num--;
492fcd89c09SVille Tervo 		break;
493bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
494bd1eb66bSAndrei Emeltchenko 		h->amp_num--;
495bd1eb66bSAndrei Emeltchenko 		break;
496fcd89c09SVille Tervo 	case LE_LINK:
497fcd89c09SVille Tervo 		h->le_num--;
498fcd89c09SVille Tervo 		break;
499fcd89c09SVille Tervo 	case SCO_LINK:
500fcd89c09SVille Tervo 	case ESCO_LINK:
5011da177e4SLinus Torvalds 		h->sco_num--;
502fcd89c09SVille Tervo 		break;
503fcd89c09SVille Tervo 	}
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
50652087a79SLuiz Augusto von Dentz static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
50752087a79SLuiz Augusto von Dentz {
50852087a79SLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
50952087a79SLuiz Augusto von Dentz 	switch (type) {
51052087a79SLuiz Augusto von Dentz 	case ACL_LINK:
51152087a79SLuiz Augusto von Dentz 		return h->acl_num;
512bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
513bd1eb66bSAndrei Emeltchenko 		return h->amp_num;
51452087a79SLuiz Augusto von Dentz 	case LE_LINK:
51552087a79SLuiz Augusto von Dentz 		return h->le_num;
51652087a79SLuiz Augusto von Dentz 	case SCO_LINK:
51752087a79SLuiz Augusto von Dentz 	case ESCO_LINK:
51852087a79SLuiz Augusto von Dentz 		return h->sco_num;
51952087a79SLuiz Augusto von Dentz 	default:
52052087a79SLuiz Augusto von Dentz 		return 0;
52152087a79SLuiz Augusto von Dentz 	}
52252087a79SLuiz Augusto von Dentz }
52352087a79SLuiz Augusto von Dentz 
5241da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
5251da177e4SLinus Torvalds 								__u16 handle)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
5281da177e4SLinus Torvalds 	struct hci_conn  *c;
5291da177e4SLinus Torvalds 
530bf4c6325SGustavo F. Padovan 	rcu_read_lock();
531bf4c6325SGustavo F. Padovan 
532bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
533bf4c6325SGustavo F. Padovan 		if (c->handle == handle) {
534bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5351da177e4SLinus Torvalds 			return c;
5361da177e4SLinus Torvalds 		}
537bf4c6325SGustavo F. Padovan 	}
538bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
539bf4c6325SGustavo F. Padovan 
5401da177e4SLinus Torvalds 	return NULL;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
5441da177e4SLinus Torvalds 							__u8 type, bdaddr_t *ba)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
5471da177e4SLinus Torvalds 	struct hci_conn  *c;
5481da177e4SLinus Torvalds 
549bf4c6325SGustavo F. Padovan 	rcu_read_lock();
550bf4c6325SGustavo F. Padovan 
551bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
552bf4c6325SGustavo F. Padovan 		if (c->type == type && !bacmp(&c->dst, ba)) {
553bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5541da177e4SLinus Torvalds 			return c;
5551da177e4SLinus Torvalds 		}
556bf4c6325SGustavo F. Padovan 	}
557bf4c6325SGustavo F. Padovan 
558bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
559bf4c6325SGustavo F. Padovan 
5601da177e4SLinus Torvalds 	return NULL;
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5634c67bc74SMarcel Holtmann static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
5644c67bc74SMarcel Holtmann 							__u8 type, __u16 state)
5654c67bc74SMarcel Holtmann {
5664c67bc74SMarcel Holtmann 	struct hci_conn_hash *h = &hdev->conn_hash;
5674c67bc74SMarcel Holtmann 	struct hci_conn  *c;
5684c67bc74SMarcel Holtmann 
569bf4c6325SGustavo F. Padovan 	rcu_read_lock();
570bf4c6325SGustavo F. Padovan 
571bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
572bf4c6325SGustavo F. Padovan 		if (c->type == type && c->state == state) {
573bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5744c67bc74SMarcel Holtmann 			return c;
5754c67bc74SMarcel Holtmann 		}
576bf4c6325SGustavo F. Padovan 	}
577bf4c6325SGustavo F. Padovan 
578bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
579bf4c6325SGustavo F. Padovan 
5804c67bc74SMarcel Holtmann 	return NULL;
5814c67bc74SMarcel Holtmann }
5824c67bc74SMarcel Holtmann 
583bed71748SAndre Guedes void hci_disconnect(struct hci_conn *conn, __u8 reason);
5842dea632fSFrédéric Dalleau bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
585e73439d8SMarcel Holtmann void hci_sco_setup(struct hci_conn *conn, __u8 status);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
5881da177e4SLinus Torvalds int hci_conn_del(struct hci_conn *conn);
5891da177e4SLinus Torvalds void hci_conn_hash_flush(struct hci_dev *hdev);
590a9de9248SMarcel Holtmann void hci_conn_check_pending(struct hci_dev *hdev);
5911da177e4SLinus Torvalds 
59273d80debSLuiz Augusto von Dentz struct hci_chan *hci_chan_create(struct hci_conn *conn);
5939472007cSAndrei Emeltchenko void hci_chan_del(struct hci_chan *chan);
5942c33c06aSGustavo F. Padovan void hci_chan_list_flush(struct hci_conn *conn);
59542c4e53eSAndrei Emeltchenko struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
59673d80debSLuiz Augusto von Dentz 
5975a9d0a3fSWaldemar Rymarkiewicz struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,
598b12f62cfSAndre Guedes 			     __u8 dst_type, __u8 sec_level, __u8 auth_type);
59910c62ddcSFrédéric Dalleau struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
60010c62ddcSFrédéric Dalleau 				 __u16 setting);
601e7c29cb1SMarcel Holtmann int hci_conn_check_link_mode(struct hci_conn *conn);
602b3b1b061SWaldemar Rymarkiewicz int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
6030684e5f9SMarcel Holtmann int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
6041da177e4SLinus Torvalds int hci_conn_change_link_key(struct hci_conn *conn);
6058c1b2355SMarcel Holtmann int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
6061da177e4SLinus Torvalds 
60714b12d0bSJaikumar Ganesh void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
6081da177e4SLinus Torvalds 
6098d12356fSDavid Herrmann /*
6108d12356fSDavid Herrmann  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
6118d12356fSDavid Herrmann  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
6128d12356fSDavid Herrmann  * working or anything else. They just guarantee that the object is available
6138d12356fSDavid Herrmann  * and can be dereferenced. So you can use its locks, local variables and any
6148d12356fSDavid Herrmann  * other constant data.
6158d12356fSDavid Herrmann  * Before accessing runtime data, you _must_ lock the object and then check that
6168d12356fSDavid Herrmann  * it is still running. As soon as you release the locks, the connection might
6178d12356fSDavid Herrmann  * get dropped, though.
6188d12356fSDavid Herrmann  *
6198d12356fSDavid Herrmann  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
6208d12356fSDavid Herrmann  * how long the underlying connection is held. So every channel that runs on the
6218d12356fSDavid Herrmann  * hci_conn object calls this to prevent the connection from disappearing. As
6228d12356fSDavid Herrmann  * long as you hold a device, you must also guarantee that you have a valid
6238d12356fSDavid Herrmann  * reference to the device via hci_conn_get() (or the initial reference from
6248d12356fSDavid Herrmann  * hci_conn_add()).
6258d12356fSDavid Herrmann  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
6268d12356fSDavid Herrmann  * break because nobody cares for that. But this means, we cannot use
6278d12356fSDavid Herrmann  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
6288d12356fSDavid Herrmann  */
6298d12356fSDavid Herrmann 
6308d12356fSDavid Herrmann static inline void hci_conn_get(struct hci_conn *conn)
6318d12356fSDavid Herrmann {
6328d12356fSDavid Herrmann 	get_device(&conn->dev);
6338d12356fSDavid Herrmann }
6348d12356fSDavid Herrmann 
6358d12356fSDavid Herrmann static inline void hci_conn_put(struct hci_conn *conn)
6368d12356fSDavid Herrmann {
6378d12356fSDavid Herrmann 	put_device(&conn->dev);
6388d12356fSDavid Herrmann }
6398d12356fSDavid Herrmann 
6401da177e4SLinus Torvalds static inline void hci_conn_hold(struct hci_conn *conn)
6411da177e4SLinus Torvalds {
64271becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
64338b3fef1SAndrei Emeltchenko 
6441da177e4SLinus Torvalds 	atomic_inc(&conn->refcnt);
6452f304d1eSAndre Guedes 	cancel_delayed_work(&conn->disc_work);
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
64876a68ba0SDavid Herrmann static inline void hci_conn_drop(struct hci_conn *conn)
6491da177e4SLinus Torvalds {
65071becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
65138b3fef1SAndrei Emeltchenko 
6521da177e4SLinus Torvalds 	if (atomic_dec_and_test(&conn->refcnt)) {
65304837f64SMarcel Holtmann 		unsigned long timeo;
654716e4ab5SAndrei Emeltchenko 
655716e4ab5SAndrei Emeltchenko 		switch (conn->type) {
656716e4ab5SAndrei Emeltchenko 		case ACL_LINK:
657716e4ab5SAndrei Emeltchenko 		case LE_LINK:
658a74a84f6SJohan Hedberg 			cancel_delayed_work(&conn->idle_work);
6596ac59344SMarcel Holtmann 			if (conn->state == BT_CONNECTED) {
6605f246e89SAndrei Emeltchenko 				timeo = conn->disc_timeout;
66104837f64SMarcel Holtmann 				if (!conn->out)
662052b30b0SMarcel Holtmann 					timeo *= 2;
6635a9d0a3fSWaldemar Rymarkiewicz 			} else {
6646ac59344SMarcel Holtmann 				timeo = msecs_to_jiffies(10);
6655a9d0a3fSWaldemar Rymarkiewicz 			}
666716e4ab5SAndrei Emeltchenko 			break;
667716e4ab5SAndrei Emeltchenko 
668716e4ab5SAndrei Emeltchenko 		case AMP_LINK:
669716e4ab5SAndrei Emeltchenko 			timeo = conn->disc_timeout;
670716e4ab5SAndrei Emeltchenko 			break;
671716e4ab5SAndrei Emeltchenko 
672716e4ab5SAndrei Emeltchenko 		default:
67304837f64SMarcel Holtmann 			timeo = msecs_to_jiffies(10);
674716e4ab5SAndrei Emeltchenko 			break;
6755a9d0a3fSWaldemar Rymarkiewicz 		}
676716e4ab5SAndrei Emeltchenko 
6772f304d1eSAndre Guedes 		cancel_delayed_work(&conn->disc_work);
67819c40e3bSGustavo F. Padovan 		queue_delayed_work(conn->hdev->workqueue,
6791931782bSVinicius Costa Gomes 				   &conn->disc_work, timeo);
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /* ----- HCI Devices ----- */
684dc946bd8SDavid Herrmann static inline void hci_dev_put(struct hci_dev *d)
6851da177e4SLinus Torvalds {
686376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
687376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
688376261aeSAndrei Emeltchenko 
6894c724c71SDavid Herrmann 	put_device(&d->dev);
690010666a1SDavid Herrmann }
6911da177e4SLinus Torvalds 
692dc946bd8SDavid Herrmann static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
6931da177e4SLinus Torvalds {
694376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
695376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
696376261aeSAndrei Emeltchenko 
6974c724c71SDavid Herrmann 	get_device(&d->dev);
6981da177e4SLinus Torvalds 	return d;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
70109fd0de5SGustavo F. Padovan #define hci_dev_lock(d)		mutex_lock(&d->lock)
70209fd0de5SGustavo F. Padovan #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
7031da177e4SLinus Torvalds 
704aa2b86d7SDavid Herrmann #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
7053dc07322SDavid Herrmann #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
706aa2b86d7SDavid Herrmann 
707155961e8SDavid Herrmann static inline void *hci_get_drvdata(struct hci_dev *hdev)
708155961e8SDavid Herrmann {
709155961e8SDavid Herrmann 	return dev_get_drvdata(&hdev->dev);
710155961e8SDavid Herrmann }
711155961e8SDavid Herrmann 
712155961e8SDavid Herrmann static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
713155961e8SDavid Herrmann {
714155961e8SDavid Herrmann 	dev_set_drvdata(&hdev->dev, data);
715155961e8SDavid Herrmann }
716155961e8SDavid Herrmann 
7171da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index);
7180c0afedfSJohan Hedberg struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds struct hci_dev *hci_alloc_dev(void);
7211da177e4SLinus Torvalds void hci_free_dev(struct hci_dev *hdev);
7221da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev);
72359735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev);
7241da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev);
7251da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev);
7261da177e4SLinus Torvalds int hci_dev_open(__u16 dev);
7271da177e4SLinus Torvalds int hci_dev_close(__u16 dev);
7281da177e4SLinus Torvalds int hci_dev_reset(__u16 dev);
7291da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev);
7301da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg);
7311da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg);
7321da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg);
7331da177e4SLinus Torvalds int hci_get_conn_list(void __user *arg);
7341da177e4SLinus Torvalds int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
73540be492fSMarcel Holtmann int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
7361da177e4SLinus Torvalds int hci_inquiry(void __user *arg);
7371da177e4SLinus Torvalds 
738c3c7ea65SGustavo Padovan struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
739b9ee0a78SMarcel Holtmann 					 bdaddr_t *bdaddr, u8 type);
740f0358568SJohan Hedberg int hci_blacklist_clear(struct hci_dev *hdev);
74188c1fe4bSJohan Hedberg int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
74288c1fe4bSJohan Hedberg int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
743f0358568SJohan Hedberg 
7442aeb9a1aSJohan Hedberg int hci_uuids_clear(struct hci_dev *hdev);
7452aeb9a1aSJohan Hedberg 
74655ed8ca1SJohan Hedberg int hci_link_keys_clear(struct hci_dev *hdev);
74755ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
748d25e28abSJohan Hedberg int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
749d25e28abSJohan Hedberg 		     bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len);
750c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]);
751c9839a11SVinicius Costa Gomes int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
7529a006657SAndrei Emeltchenko 		int new_key, u8 authenticated, u8 tk[16], u8 enc_size,
7539a006657SAndrei Emeltchenko 		__le16 ediv, u8 rand[8]);
754c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
755c9839a11SVinicius Costa Gomes 				     u8 addr_type);
756b899efafSVinicius Costa Gomes int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr);
757b899efafSVinicius Costa Gomes int hci_smp_ltks_clear(struct hci_dev *hdev);
75855ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
75955ed8ca1SJohan Hedberg 
7602763eda6SSzymon Janc int hci_remote_oob_data_clear(struct hci_dev *hdev);
7612763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
7622763eda6SSzymon Janc 							bdaddr_t *bdaddr);
7632763eda6SSzymon Janc int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
7642763eda6SSzymon Janc 								u8 *randomizer);
7652763eda6SSzymon Janc int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
7662763eda6SSzymon Janc 
7671da177e4SLinus Torvalds void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
7681da177e4SLinus Torvalds 
769e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
770ef222013SMarcel Holtmann int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
77199811510SSuraj Sumangala int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
772ef222013SMarcel Holtmann 
7730ac7e700SDavid Herrmann void hci_init_sysfs(struct hci_dev *hdev);
774a67e899cSMarcel Holtmann void hci_conn_init_sysfs(struct hci_conn *conn);
775b219e3acSMarcel Holtmann void hci_conn_add_sysfs(struct hci_conn *conn);
776b219e3acSMarcel Holtmann void hci_conn_del_sysfs(struct hci_conn *conn);
7771da177e4SLinus Torvalds 
7786935e0f5SDavid Herrmann #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds /* ----- LMP capabilities ----- */
781cad718edSJohan Hedberg #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
782cad718edSJohan Hedberg #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
783cad718edSJohan Hedberg #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
784cad718edSJohan Hedberg #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
785cad718edSJohan Hedberg #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
786cad718edSJohan Hedberg #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
787cad718edSJohan Hedberg #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
788cad718edSJohan Hedberg #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
789cad718edSJohan Hedberg #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
790cad718edSJohan Hedberg #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
791cad718edSJohan Hedberg #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
792cad718edSJohan Hedberg #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
793cad718edSJohan Hedberg #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
794cad718edSJohan Hedberg #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
795cad718edSJohan Hedberg #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
796cad718edSJohan Hedberg #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
797cad718edSJohan Hedberg #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
798cad718edSJohan Hedberg #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
79907a5c61eSFrédéric Dalleau #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
8001da177e4SLinus Torvalds 
801eead27daSAndre Guedes /* ----- Extended LMP capabilities ----- */
802cad718edSJohan Hedberg #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
803cad718edSJohan Hedberg #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
804cad718edSJohan Hedberg #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
805eead27daSAndre Guedes 
8061da177e4SLinus Torvalds /* ----- HCI protocols ----- */
80720714bfeSFrédéric Dalleau #define HCI_PROTO_DEFER             0x01
80820714bfeSFrédéric Dalleau 
8095a9d0a3fSWaldemar Rymarkiewicz static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
81020714bfeSFrédéric Dalleau 					__u8 type, __u8 *flags)
8111da177e4SLinus Torvalds {
812686ebf28SUlisses Furquim 	switch (type) {
813686ebf28SUlisses Furquim 	case ACL_LINK:
814686ebf28SUlisses Furquim 		return l2cap_connect_ind(hdev, bdaddr);
8151da177e4SLinus Torvalds 
816686ebf28SUlisses Furquim 	case SCO_LINK:
817686ebf28SUlisses Furquim 	case ESCO_LINK:
81820714bfeSFrédéric Dalleau 		return sco_connect_ind(hdev, bdaddr, flags);
8191da177e4SLinus Torvalds 
820686ebf28SUlisses Furquim 	default:
821686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", type);
822686ebf28SUlisses Furquim 		return -EINVAL;
823686ebf28SUlisses Furquim 	}
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
8271da177e4SLinus Torvalds {
828686ebf28SUlisses Furquim 	switch (conn->type) {
829686ebf28SUlisses Furquim 	case ACL_LINK:
830686ebf28SUlisses Furquim 	case LE_LINK:
831686ebf28SUlisses Furquim 		l2cap_connect_cfm(conn, status);
832686ebf28SUlisses Furquim 		break;
8331da177e4SLinus Torvalds 
834686ebf28SUlisses Furquim 	case SCO_LINK:
835686ebf28SUlisses Furquim 	case ESCO_LINK:
836686ebf28SUlisses Furquim 		sco_connect_cfm(conn, status);
837686ebf28SUlisses Furquim 		break;
8381da177e4SLinus Torvalds 
839686ebf28SUlisses Furquim 	default:
840686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", conn->type);
841686ebf28SUlisses Furquim 		break;
842686ebf28SUlisses Furquim 	}
843e9a416b5SJohan Hedberg 
844e9a416b5SJohan Hedberg 	if (conn->connect_cfm_cb)
845e9a416b5SJohan Hedberg 		conn->connect_cfm_cb(conn, status);
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8482950f21aSMarcel Holtmann static inline int hci_proto_disconn_ind(struct hci_conn *conn)
8492950f21aSMarcel Holtmann {
850686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
851686ebf28SUlisses Furquim 		return HCI_ERROR_REMOTE_USER_TERM;
8522950f21aSMarcel Holtmann 
853686ebf28SUlisses Furquim 	return l2cap_disconn_ind(conn);
8542950f21aSMarcel Holtmann }
8552950f21aSMarcel Holtmann 
8562950f21aSMarcel Holtmann static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
8571da177e4SLinus Torvalds {
858686ebf28SUlisses Furquim 	switch (conn->type) {
859686ebf28SUlisses Furquim 	case ACL_LINK:
860686ebf28SUlisses Furquim 	case LE_LINK:
861686ebf28SUlisses Furquim 		l2cap_disconn_cfm(conn, reason);
862686ebf28SUlisses Furquim 		break;
8631da177e4SLinus Torvalds 
864686ebf28SUlisses Furquim 	case SCO_LINK:
865686ebf28SUlisses Furquim 	case ESCO_LINK:
866686ebf28SUlisses Furquim 		sco_disconn_cfm(conn, reason);
867686ebf28SUlisses Furquim 		break;
8681da177e4SLinus Torvalds 
869bd1eb66bSAndrei Emeltchenko 	/* L2CAP would be handled for BREDR chan */
870bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
871bd1eb66bSAndrei Emeltchenko 		break;
872bd1eb66bSAndrei Emeltchenko 
873686ebf28SUlisses Furquim 	default:
874686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", conn->type);
875686ebf28SUlisses Furquim 		break;
876686ebf28SUlisses Furquim 	}
877e9a416b5SJohan Hedberg 
878e9a416b5SJohan Hedberg 	if (conn->disconn_cfm_cb)
879e9a416b5SJohan Hedberg 		conn->disconn_cfm_cb(conn, reason);
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
8831da177e4SLinus Torvalds {
8848c1b2355SMarcel Holtmann 	__u8 encrypt;
8858c1b2355SMarcel Holtmann 
886686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
887686ebf28SUlisses Furquim 		return;
888686ebf28SUlisses Furquim 
88951a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
8908c1b2355SMarcel Holtmann 		return;
8918c1b2355SMarcel Holtmann 
8928c1b2355SMarcel Holtmann 	encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
893686ebf28SUlisses Furquim 	l2cap_security_cfm(conn, status, encrypt);
894e9a416b5SJohan Hedberg 
895e9a416b5SJohan Hedberg 	if (conn->security_cfm_cb)
896e9a416b5SJohan Hedberg 		conn->security_cfm_cb(conn, status);
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
8995a9d0a3fSWaldemar Rymarkiewicz static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status,
9005a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
9011da177e4SLinus Torvalds {
902686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
903686ebf28SUlisses Furquim 		return;
9041da177e4SLinus Torvalds 
905686ebf28SUlisses Furquim 	l2cap_security_cfm(conn, status, encrypt);
906e9a416b5SJohan Hedberg 
907e9a416b5SJohan Hedberg 	if (conn->security_cfm_cb)
908e9a416b5SJohan Hedberg 		conn->security_cfm_cb(conn, status);
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds /* ----- HCI callbacks ----- */
9121da177e4SLinus Torvalds struct hci_cb {
9131da177e4SLinus Torvalds 	struct list_head list;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	char *name;
9161da177e4SLinus Torvalds 
9175a9d0a3fSWaldemar Rymarkiewicz 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
9185a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt);
9191da177e4SLinus Torvalds 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
9201da177e4SLinus Torvalds 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
9211da177e4SLinus Torvalds };
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
9241da177e4SLinus Torvalds {
925711584eaSDenis Kirjanov 	struct hci_cb *cb;
9268c1b2355SMarcel Holtmann 	__u8 encrypt;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	hci_proto_auth_cfm(conn, status);
9291da177e4SLinus Torvalds 
93051a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
9318c1b2355SMarcel Holtmann 		return;
9328c1b2355SMarcel Holtmann 
9338c1b2355SMarcel Holtmann 	encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
9348c1b2355SMarcel Holtmann 
935f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
936711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9378c1b2355SMarcel Holtmann 		if (cb->security_cfm)
9388c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
9391da177e4SLinus Torvalds 	}
940f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
9435a9d0a3fSWaldemar Rymarkiewicz static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
9445a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
9451da177e4SLinus Torvalds {
946711584eaSDenis Kirjanov 	struct hci_cb *cb;
9471da177e4SLinus Torvalds 
948435fef20SMarcel Holtmann 	if (conn->sec_level == BT_SECURITY_SDP)
949435fef20SMarcel Holtmann 		conn->sec_level = BT_SECURITY_LOW;
950435fef20SMarcel Holtmann 
95188167aedSVinicius Costa Gomes 	if (conn->pending_sec_level > conn->sec_level)
95288167aedSVinicius Costa Gomes 		conn->sec_level = conn->pending_sec_level;
95388167aedSVinicius Costa Gomes 
9549719f8afSMarcel Holtmann 	hci_proto_encrypt_cfm(conn, status, encrypt);
9551da177e4SLinus Torvalds 
956f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
957711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9588c1b2355SMarcel Holtmann 		if (cb->security_cfm)
9598c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
9601da177e4SLinus Torvalds 	}
961f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
9651da177e4SLinus Torvalds {
966711584eaSDenis Kirjanov 	struct hci_cb *cb;
9671da177e4SLinus Torvalds 
968f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
969711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9701da177e4SLinus Torvalds 		if (cb->key_change_cfm)
9711da177e4SLinus Torvalds 			cb->key_change_cfm(conn, status);
9721da177e4SLinus Torvalds 	}
973f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9765a9d0a3fSWaldemar Rymarkiewicz static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
9775a9d0a3fSWaldemar Rymarkiewicz 								__u8 role)
9781da177e4SLinus Torvalds {
979711584eaSDenis Kirjanov 	struct hci_cb *cb;
9801da177e4SLinus Torvalds 
981f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
982711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9831da177e4SLinus Torvalds 		if (cb->role_switch_cfm)
9841da177e4SLinus Torvalds 			cb->role_switch_cfm(conn, status, role);
9851da177e4SLinus Torvalds 	}
986f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9896759a675SJohan Hedberg static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
9906759a675SJohan Hedberg {
99184d9d071SJohan Hedberg 	size_t parsed = 0;
9926759a675SJohan Hedberg 
9936c0c331eSJohan Hedberg 	if (data_len < 2)
9946c0c331eSJohan Hedberg 		return false;
9956c0c331eSJohan Hedberg 
99684d9d071SJohan Hedberg 	while (parsed < data_len - 1) {
99784d9d071SJohan Hedberg 		u8 field_len = data[0];
9986759a675SJohan Hedberg 
9996759a675SJohan Hedberg 		if (field_len == 0)
10006759a675SJohan Hedberg 			break;
10016759a675SJohan Hedberg 
10026759a675SJohan Hedberg 		parsed += field_len + 1;
10036759a675SJohan Hedberg 
10046759a675SJohan Hedberg 		if (parsed > data_len)
10056759a675SJohan Hedberg 			break;
10066759a675SJohan Hedberg 
10076759a675SJohan Hedberg 		if (data[1] == type)
10086759a675SJohan Hedberg 			return true;
10096759a675SJohan Hedberg 
10106759a675SJohan Hedberg 		data += field_len + 1;
10116759a675SJohan Hedberg 	}
10126759a675SJohan Hedberg 
10136759a675SJohan Hedberg 	return false;
10146759a675SJohan Hedberg }
10156759a675SJohan Hedberg 
10161da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *hcb);
10171da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *hcb);
10181da177e4SLinus Torvalds 
10193119ae95SJohan Hedberg struct hci_request {
10203119ae95SJohan Hedberg 	struct hci_dev		*hdev;
10213119ae95SJohan Hedberg 	struct sk_buff_head	cmd_q;
10225d73e034SAndre Guedes 
10235d73e034SAndre Guedes 	/* If something goes wrong when building the HCI request, the error
10245d73e034SAndre Guedes 	 * value is stored in this field.
10255d73e034SAndre Guedes 	 */
10265d73e034SAndre Guedes 	int			err;
10273119ae95SJohan Hedberg };
10283119ae95SJohan Hedberg 
10293119ae95SJohan Hedberg void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
10303119ae95SJohan Hedberg int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
103107dc93ddSJohan Hedberg void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
103207dc93ddSJohan Hedberg 		 const void *param);
103307dc93ddSJohan Hedberg void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
103407dc93ddSJohan Hedberg 		    const void *param, u8 event);
10359238f36aSJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
10363119ae95SJohan Hedberg 
103775e84b7cSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
103807dc93ddSJohan Hedberg 			       const void *param, u32 timeout);
10397b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
104007dc93ddSJohan Hedberg 				  const void *param, u8 event, u32 timeout);
104175e84b7cSJohan Hedberg 
104207dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
104307dc93ddSJohan Hedberg 		 const void *param);
104473d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
10450d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
10461da177e4SLinus Torvalds 
1047a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /* ----- HCI Sockets ----- */
1050470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
1051470fe1b5SMarcel Holtmann void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk);
1052cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
10531da177e4SLinus Torvalds 
1054040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event);
1055040030efSMarcel Holtmann 
10560381101fSJohan Hedberg /* Management interface */
1057591f47f3SAndre Guedes #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
1058591f47f3SAndre Guedes #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
1059591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1060591f47f3SAndre Guedes #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
1061591f47f3SAndre Guedes 					 BIT(BDADDR_LE_PUBLIC) | \
1062591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1063f39799f5SAndre Guedes 
10640d8cc935SAndre Guedes /* These LE scan and inquiry parameters were chosen according to LE General
10650d8cc935SAndre Guedes  * Discovery Procedure specification.
10660d8cc935SAndre Guedes  */
10670d8cc935SAndre Guedes #define DISCOV_LE_SCAN_WIN		0x12
10680d8cc935SAndre Guedes #define DISCOV_LE_SCAN_INT		0x12
10690d8cc935SAndre Guedes #define DISCOV_LE_TIMEOUT		msecs_to_jiffies(10240)
10700d8cc935SAndre Guedes #define DISCOV_INTERLEAVED_TIMEOUT	msecs_to_jiffies(5120)
10710d8cc935SAndre Guedes #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
10720d8cc935SAndre Guedes #define DISCOV_BREDR_INQUIRY_LEN	0x08
10730d8cc935SAndre Guedes 
10740381101fSJohan Hedberg int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
1075bf6b56dbSMarcel Holtmann void mgmt_index_added(struct hci_dev *hdev);
1076bf6b56dbSMarcel Holtmann void mgmt_index_removed(struct hci_dev *hdev);
10773eec705eSMarcel Holtmann void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
1078744cf19eSJohan Hedberg int mgmt_powered(struct hci_dev *hdev, u8 powered);
1079d1967ff8SMarcel Holtmann void mgmt_discoverable_timeout(struct hci_dev *hdev);
108086a75645SMarcel Holtmann void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
1081a330916cSMarcel Holtmann void mgmt_connectable(struct hci_dev *hdev, u8 connectable);
10824796e8afSMarcel Holtmann void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status);
1083dc4a5ee2SMarcel Holtmann void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1084745c0ce3SVishal Agarwal 		       bool persistent);
1085ecd90ae7SMarcel Holtmann void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
108604124681SGustavo F. Padovan 			   u8 addr_type, u32 flags, u8 *name, u8 name_len,
108704124681SGustavo F. Padovan 			   u8 *dev_class);
10889b80ec5eSMarcel Holtmann void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
1089f0d6a0eaSMikel Astiz 			      u8 link_type, u8 addr_type, u8 reason);
10907892924cSMarcel Holtmann void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
109188c3df13SJohan Hedberg 			    u8 link_type, u8 addr_type, u8 status);
1092445608d0SMarcel Holtmann void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
109348264f06SJohan Hedberg 			 u8 addr_type, u8 status);
1094ce0e4a0dSMarcel Holtmann void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
1095e669cf80SMarcel Holtmann void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1096c35938b2SSzymon Janc 				  u8 status);
10973eb38528SMarcel Holtmann void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1098744cf19eSJohan Hedberg 				      u8 status);
1099744cf19eSJohan Hedberg int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1100272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type, __le32 value,
1101272d90dfSJohan Hedberg 			      u8 confirm_hint);
1102744cf19eSJohan Hedberg int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1103272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1104272d90dfSJohan Hedberg int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1105272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
1106272d90dfSJohan Hedberg int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1107272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type);
1108604086b7SBrian Gix int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1109272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1110272d90dfSJohan Hedberg int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1111272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
111292a25256SJohan Hedberg int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
111392a25256SJohan Hedberg 			     u8 link_type, u8 addr_type, u32 passkey,
111492a25256SJohan Hedberg 			     u8 entered);
1115e546099cSMarcel Holtmann void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1116bab73cb6SJohan Hedberg 		      u8 addr_type, u8 status);
1117464996aeSMarcel Holtmann void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
11183e248560SMarcel Holtmann void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
11194e1b0245SMarcel Holtmann void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
11207f9a903cSMarcel Holtmann 				    u8 status);
11217667da34SMarcel Holtmann void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
11223edaf092SMarcel Holtmann void mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
1123744cf19eSJohan Hedberg 					     u8 *randomizer, u8 status);
1124901801b9SMarcel Holtmann void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
112504124681SGustavo F. Padovan 		       u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
112604124681SGustavo F. Padovan 		       u8 ssp, u8 *eir, u16 eir_len);
11279cf12aeeSMarcel Holtmann void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1128b644ba33SJohan Hedberg 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
11292f1e063bSMarcel Holtmann void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
113088c1fe4bSJohan Hedberg int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
113188c1fe4bSJohan Hedberg int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1132083368f7SMarcel Holtmann void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent);
11335976e608SMarcel Holtmann void mgmt_reenable_advertising(struct hci_dev *hdev);
1134346af67bSVinicius Costa Gomes 
11351da177e4SLinus Torvalds /* HCI info for socket */
11361da177e4SLinus Torvalds #define hci_pi(sk) ((struct hci_pinfo *) sk)
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds struct hci_pinfo {
11391da177e4SLinus Torvalds 	struct bt_sock    bt;
11401da177e4SLinus Torvalds 	struct hci_dev    *hdev;
11411da177e4SLinus Torvalds 	struct hci_filter filter;
11421da177e4SLinus Torvalds 	__u32             cmsg_mask;
1143c02178d2SJohan Hedberg 	unsigned short   channel;
11441da177e4SLinus Torvalds };
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds /* HCI security filter */
11471da177e4SLinus Torvalds #define HCI_SFLT_MAX_OGF  5
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds struct hci_sec_filter {
11501da177e4SLinus Torvalds 	__u32 type_mask;
11511da177e4SLinus Torvalds 	__u32 event_mask[2];
11521da177e4SLinus Torvalds 	__u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
11531da177e4SLinus Torvalds };
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds /* ----- HCI requests ----- */
11561da177e4SLinus Torvalds #define HCI_REQ_DONE	  0
11571da177e4SLinus Torvalds #define HCI_REQ_PEND	  1
11581da177e4SLinus Torvalds #define HCI_REQ_CANCELED  2
11591da177e4SLinus Torvalds 
1160a6a67efdSThomas Gleixner #define hci_req_lock(d)		mutex_lock(&d->req_lock)
1161a6a67efdSThomas Gleixner #define hci_req_unlock(d)	mutex_unlock(&d->req_lock)
11621da177e4SLinus Torvalds 
11632ce603ebSClaudio Takahasi void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
11642ce603ebSClaudio Takahasi 					u16 latency, u16 to_multiplier);
1165a7a595f6SVinicius Costa Gomes void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
1166a7a595f6SVinicius Costa Gomes 							__u8 ltk[16]);
11672519a1fcSAndre Guedes 
11685d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_MASK       0x0003
11695d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_CVSD       0x0000
11705d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_TRANSP     0x0003
11715d4d62f6SFrédéric Dalleau 
11721da177e4SLinus Torvalds #endif /* __HCI_CORE_H */
1173