xref: /openbmc/linux/include/net/bluetooth/hci_core.h (revision 662e8820)
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;
84f0358568SJohan Hedberg };
852aeb9a1aSJohan Hedberg 
862aeb9a1aSJohan Hedberg struct bt_uuid {
872aeb9a1aSJohan Hedberg 	struct list_head list;
882aeb9a1aSJohan Hedberg 	u8 uuid[16];
8983be8ecaSJohan Hedberg 	u8 size;
901aff6f09SJohan Hedberg 	u8 svc_hint;
912aeb9a1aSJohan Hedberg };
922aeb9a1aSJohan Hedberg 
93b899efafSVinicius Costa Gomes struct smp_ltk {
94b899efafSVinicius Costa Gomes 	struct list_head list;
95b899efafSVinicius Costa Gomes 	bdaddr_t bdaddr;
96b899efafSVinicius Costa Gomes 	u8 bdaddr_type;
97b899efafSVinicius Costa Gomes 	u8 authenticated;
98b899efafSVinicius Costa Gomes 	u8 type;
99b899efafSVinicius Costa Gomes 	u8 enc_size;
100b899efafSVinicius Costa Gomes 	__le16 ediv;
101b899efafSVinicius Costa Gomes 	u8 rand[8];
102b899efafSVinicius Costa Gomes 	u8 val[16];
103b899efafSVinicius Costa Gomes } __packed;
104b899efafSVinicius Costa Gomes 
10555ed8ca1SJohan Hedberg struct link_key {
10655ed8ca1SJohan Hedberg 	struct list_head list;
10755ed8ca1SJohan Hedberg 	bdaddr_t bdaddr;
10855ed8ca1SJohan Hedberg 	u8 type;
1099b3b4460SAndrei Emeltchenko 	u8 val[HCI_LINK_KEY_SIZE];
11055ed8ca1SJohan Hedberg 	u8 pin_len;
11155ed8ca1SJohan Hedberg };
11255ed8ca1SJohan Hedberg 
1132763eda6SSzymon Janc struct oob_data {
1142763eda6SSzymon Janc 	struct list_head list;
1152763eda6SSzymon Janc 	bdaddr_t bdaddr;
1162763eda6SSzymon Janc 	u8 hash[16];
1172763eda6SSzymon Janc 	u8 randomizer[16];
1182763eda6SSzymon Janc };
1192763eda6SSzymon Janc 
120490c5babSJohan Hedberg #define HCI_MAX_SHORT_NAME_LENGTH	10
121490c5babSJohan Hedberg 
122903e4541SAndrei Emeltchenko struct amp_assoc {
123903e4541SAndrei Emeltchenko 	__u16	len;
124903e4541SAndrei Emeltchenko 	__u16	offset;
12593c284eeSAndrei Emeltchenko 	__u16	rem_len;
12693c284eeSAndrei Emeltchenko 	__u16	len_so_far;
127903e4541SAndrei Emeltchenko 	__u8	data[HCI_MAX_AMP_ASSOC_SIZE];
128903e4541SAndrei Emeltchenko };
129903e4541SAndrei Emeltchenko 
130d2c5d77fSJohan Hedberg #define HCI_MAX_PAGES	3
131cad718edSJohan Hedberg 
132cd4c5391SSuraj Sumangala #define NUM_REASSEMBLY 4
1331da177e4SLinus Torvalds struct hci_dev {
1341da177e4SLinus Torvalds 	struct list_head list;
13509fd0de5SGustavo F. Padovan 	struct mutex	lock;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	char		name[8];
1381da177e4SLinus Torvalds 	unsigned long	flags;
1391da177e4SLinus Torvalds 	__u16		id;
140c13854ceSMarcel Holtmann 	__u8		bus;
141943da25dSMarcel Holtmann 	__u8		dev_type;
1421da177e4SLinus Torvalds 	bdaddr_t	bdaddr;
143d13eafceSMarcel Holtmann 	bdaddr_t	static_addr;
1441f6c6378SJohan Hedberg 	__u8		dev_name[HCI_MAX_NAME_LENGTH];
145490c5babSJohan Hedberg 	__u8		short_name[HCI_MAX_SHORT_NAME_LENGTH];
14680a1e1dbSJohan Hedberg 	__u8		eir[HCI_MAX_EIR_LENGTH];
147a9de9248SMarcel Holtmann 	__u8		dev_class[3];
1481aff6f09SJohan Hedberg 	__u8		major_class;
1491aff6f09SJohan Hedberg 	__u8		minor_class;
150d2c5d77fSJohan Hedberg 	__u8		max_page;
151cad718edSJohan Hedberg 	__u8		features[HCI_MAX_PAGES][8];
15260e77321SJohan Hedberg 	__u8		le_features[8];
153cf1d081fSJohan Hedberg 	__u8		le_white_list_size;
1549b008c04SJohan Hedberg 	__u8		le_states[8];
155a9de9248SMarcel Holtmann 	__u8		commands[64];
1561143e5a6SMarcel Holtmann 	__u8		hci_ver;
1571143e5a6SMarcel Holtmann 	__u16		hci_rev;
158d5859e22SJohan Hedberg 	__u8		lmp_ver;
1591143e5a6SMarcel Holtmann 	__u16		manufacturer;
1607d69230cSAndrei Emeltchenko 	__u16		lmp_subver;
1611da177e4SLinus Torvalds 	__u16		voice_setting;
16217fa4b9dSJohan Hedberg 	__u8		io_capability;
16391c4e9b1SMarcel Holtmann 	__s8		inq_tx_power;
164f332ec66SJohan Hedberg 	__u16		page_scan_interval;
165f332ec66SJohan Hedberg 	__u16		page_scan_window;
166f332ec66SJohan Hedberg 	__u8		page_scan_type;
167bef64738SMarcel Holtmann 	__u16		le_scan_interval;
168bef64738SMarcel Holtmann 	__u16		le_scan_window;
169f332ec66SJohan Hedberg 
1702b9be137SMarcel Holtmann 	__u16		devid_source;
1712b9be137SMarcel Holtmann 	__u16		devid_vendor;
1722b9be137SMarcel Holtmann 	__u16		devid_product;
1732b9be137SMarcel Holtmann 	__u16		devid_version;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	__u16		pkt_type;
1765b7f9909SMarcel Holtmann 	__u16		esco_type;
1771da177e4SLinus Torvalds 	__u16		link_policy;
1781da177e4SLinus Torvalds 	__u16		link_mode;
1791da177e4SLinus Torvalds 
18004837f64SMarcel Holtmann 	__u32		idle_timeout;
18104837f64SMarcel Holtmann 	__u16		sniff_min_interval;
18204837f64SMarcel Holtmann 	__u16		sniff_max_interval;
18304837f64SMarcel Holtmann 
184928abaa7SAndrei Emeltchenko 	__u8		amp_status;
185928abaa7SAndrei Emeltchenko 	__u32		amp_total_bw;
186928abaa7SAndrei Emeltchenko 	__u32		amp_max_bw;
187928abaa7SAndrei Emeltchenko 	__u32		amp_min_latency;
188928abaa7SAndrei Emeltchenko 	__u32		amp_max_pdu;
189928abaa7SAndrei Emeltchenko 	__u8		amp_type;
190928abaa7SAndrei Emeltchenko 	__u16		amp_pal_cap;
191928abaa7SAndrei Emeltchenko 	__u16		amp_assoc_size;
192928abaa7SAndrei Emeltchenko 	__u32		amp_max_flush_to;
193928abaa7SAndrei Emeltchenko 	__u32		amp_be_flush_to;
194928abaa7SAndrei Emeltchenko 
195903e4541SAndrei Emeltchenko 	struct amp_assoc	loc_assoc;
196903e4541SAndrei Emeltchenko 
1971e89cffbSAndrei Emeltchenko 	__u8		flow_ctl_mode;
1981e89cffbSAndrei Emeltchenko 
1999f61656aSJohan Hedberg 	unsigned int	auto_accept_delay;
2009f61656aSJohan Hedberg 
2011da177e4SLinus Torvalds 	unsigned long	quirks;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	atomic_t	cmd_cnt;
2041da177e4SLinus Torvalds 	unsigned int	acl_cnt;
2051da177e4SLinus Torvalds 	unsigned int	sco_cnt;
2066ed58ec5SVille Tervo 	unsigned int	le_cnt;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	unsigned int	acl_mtu;
2091da177e4SLinus Torvalds 	unsigned int	sco_mtu;
2106ed58ec5SVille Tervo 	unsigned int	le_mtu;
2111da177e4SLinus Torvalds 	unsigned int	acl_pkts;
2121da177e4SLinus Torvalds 	unsigned int	sco_pkts;
2136ed58ec5SVille Tervo 	unsigned int	le_pkts;
2141da177e4SLinus Torvalds 
215350ee4cfSAndrei Emeltchenko 	__u16		block_len;
216350ee4cfSAndrei Emeltchenko 	__u16		block_mtu;
217350ee4cfSAndrei Emeltchenko 	__u16		num_blocks;
218350ee4cfSAndrei Emeltchenko 	__u16		block_cnt;
219350ee4cfSAndrei Emeltchenko 
2201da177e4SLinus Torvalds 	unsigned long	acl_last_tx;
2211da177e4SLinus Torvalds 	unsigned long	sco_last_tx;
2226ed58ec5SVille Tervo 	unsigned long	le_last_tx;
2231da177e4SLinus Torvalds 
224f48fd9c8SMarcel Holtmann 	struct workqueue_struct	*workqueue;
2256ead1bbcSJohan Hedberg 	struct workqueue_struct	*req_workqueue;
226f48fd9c8SMarcel Holtmann 
227ab81cbf9SJohan Hedberg 	struct work_struct	power_on;
2283243553fSJohan Hedberg 	struct delayed_work	power_off;
229ab81cbf9SJohan Hedberg 
23016ab91abSJohan Hedberg 	__u16			discov_timeout;
23116ab91abSJohan Hedberg 	struct delayed_work	discov_off;
23216ab91abSJohan Hedberg 
2337d78525dSJohan Hedberg 	struct delayed_work	service_cache;
2347d78525dSJohan Hedberg 
2356bd32326SVille Tervo 	struct timer_list	cmd_timer;
236b78752ccSMarcel Holtmann 
237b78752ccSMarcel Holtmann 	struct work_struct	rx_work;
238c347b765SGustavo F. Padovan 	struct work_struct	cmd_work;
2393eff45eaSGustavo F. Padovan 	struct work_struct	tx_work;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	struct sk_buff_head	rx_q;
2421da177e4SLinus Torvalds 	struct sk_buff_head	raw_q;
2431da177e4SLinus Torvalds 	struct sk_buff_head	cmd_q;
2441da177e4SLinus Torvalds 
245b6ddb638SJohan Hedberg 	struct sk_buff		*recv_evt;
2461da177e4SLinus Torvalds 	struct sk_buff		*sent_cmd;
247cd4c5391SSuraj Sumangala 	struct sk_buff		*reassembly[NUM_REASSEMBLY];
2481da177e4SLinus Torvalds 
249a6a67efdSThomas Gleixner 	struct mutex		req_lock;
2501da177e4SLinus Torvalds 	wait_queue_head_t	req_wait_q;
2511da177e4SLinus Torvalds 	__u32			req_status;
2521da177e4SLinus Torvalds 	__u32			req_result;
253a5040efaSJohan Hedberg 
2542e58ef3eSJohan Hedberg 	struct list_head	mgmt_pending;
2552e58ef3eSJohan Hedberg 
25630883512SJohan Hedberg 	struct discovery_state	discovery;
2571da177e4SLinus Torvalds 	struct hci_conn_hash	conn_hash;
258ea4bd8baSDavid Miller 	struct list_head	blacklist;
2591da177e4SLinus Torvalds 
2602aeb9a1aSJohan Hedberg 	struct list_head	uuids;
2612aeb9a1aSJohan Hedberg 
26255ed8ca1SJohan Hedberg 	struct list_head	link_keys;
26355ed8ca1SJohan Hedberg 
264b899efafSVinicius Costa Gomes 	struct list_head	long_term_keys;
265b899efafSVinicius Costa Gomes 
2662763eda6SSzymon Janc 	struct list_head	remote_oob_data;
2672763eda6SSzymon Janc 
2681da177e4SLinus Torvalds 	struct hci_dev_stats	stat;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	atomic_t		promisc;
2711da177e4SLinus Torvalds 
272ca325f69SMarcel Holtmann 	struct dentry		*debugfs;
273ca325f69SMarcel Holtmann 
274a91f2e39SMarcel Holtmann 	struct device		dev;
2751da177e4SLinus Torvalds 
276611b30f7SMarcel Holtmann 	struct rfkill		*rfkill;
277611b30f7SMarcel Holtmann 
278d23264a8SAndre Guedes 	unsigned long		dev_flags;
279d23264a8SAndre Guedes 
2807ba8b4beSAndre Guedes 	struct delayed_work	le_scan_disable;
2817ba8b4beSAndre Guedes 
2828fa19098SJohan Hedberg 	__s8			adv_tx_power;
2833f0f524bSJohan Hedberg 	__u8			adv_data[HCI_MAX_AD_LENGTH];
2843f0f524bSJohan Hedberg 	__u8			adv_data_len;
2858fa19098SJohan Hedberg 
2861da177e4SLinus Torvalds 	int (*open)(struct hci_dev *hdev);
2871da177e4SLinus Torvalds 	int (*close)(struct hci_dev *hdev);
2881da177e4SLinus Torvalds 	int (*flush)(struct hci_dev *hdev);
289f41c70c4SMarcel Holtmann 	int (*setup)(struct hci_dev *hdev);
2907bd8f09fSMarcel Holtmann 	int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
2911da177e4SLinus Torvalds 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
2921da177e4SLinus Torvalds };
2931da177e4SLinus Torvalds 
29453502d69SAndrei Emeltchenko #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
29553502d69SAndrei Emeltchenko 
2961da177e4SLinus Torvalds struct hci_conn {
2971da177e4SLinus Torvalds 	struct list_head list;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	atomic_t	refcnt;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	bdaddr_t	dst;
30229b7988aSAndre Guedes 	__u8		dst_type;
303662e8820SMarcel Holtmann 	bdaddr_t	src;
304e7c4096eSMarcel Holtmann 	__u8		src_type;
3051da177e4SLinus Torvalds 	__u16		handle;
3061da177e4SLinus Torvalds 	__u16		state;
30704837f64SMarcel Holtmann 	__u8		mode;
3081da177e4SLinus Torvalds 	__u8		type;
309a0c808b3SJohan Hedberg 	bool		out;
3104c67bc74SMarcel Holtmann 	__u8		attempt;
3111da177e4SLinus Torvalds 	__u8		dev_class[3];
312cad718edSJohan Hedberg 	__u8		features[HCI_MAX_PAGES][8];
31304837f64SMarcel Holtmann 	__u16		interval;
314a8746417SMarcel Holtmann 	__u16		pkt_type;
31504837f64SMarcel Holtmann 	__u16		link_policy;
3161da177e4SLinus Torvalds 	__u32		link_mode;
31713d39315SWaldemar Rymarkiewicz 	__u8		key_type;
31840be492fSMarcel Holtmann 	__u8		auth_type;
3198c1b2355SMarcel Holtmann 	__u8		sec_level;
320765c2a96SJohan Hedberg 	__u8		pending_sec_level;
321980e1a53SJohan Hedberg 	__u8		pin_length;
322726b4ffcSVinicius Costa Gomes 	__u8		enc_key_size;
32317fa4b9dSJohan Hedberg 	__u8		io_capability;
32492a25256SJohan Hedberg 	__u32		passkey_notify;
32592a25256SJohan Hedberg 	__u8		passkey_entered;
326052b30b0SMarcel Holtmann 	__u16		disc_timeout;
32710c62ddcSFrédéric Dalleau 	__u16		setting;
32851a8efd7SJohan Hedberg 	unsigned long	flags;
3291da177e4SLinus Torvalds 
33003b555e1SJohan Hedberg 	__u8		remote_cap;
33103b555e1SJohan Hedberg 	__u8		remote_auth;
3323161ae1cSAndrei Emeltchenko 	__u8		remote_id;
3336ec5bcadSVishal Agarwal 	bool		flush_key;
33403b555e1SJohan Hedberg 
3351da177e4SLinus Torvalds 	unsigned int	sent;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	struct sk_buff_head data_q;
3382c33c06aSGustavo F. Padovan 	struct list_head chan_list;
3391da177e4SLinus Torvalds 
34019c40e3bSGustavo F. Padovan 	struct delayed_work disc_work;
34104837f64SMarcel Holtmann 	struct timer_list idle_timer;
3429f61656aSJohan Hedberg 	struct timer_list auto_accept_timer;
3431da177e4SLinus Torvalds 
344b219e3acSMarcel Holtmann 	struct device	dev;
345b219e3acSMarcel Holtmann 
3461da177e4SLinus Torvalds 	struct hci_dev	*hdev;
3471da177e4SLinus Torvalds 	void		*l2cap_data;
3481da177e4SLinus Torvalds 	void		*sco_data;
3492b64d153SBrian Gix 	void		*smp_conn;
3509740e49dSAndrei Emeltchenko 	struct amp_mgr	*amp_mgr;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	struct hci_conn	*link;
353e9a416b5SJohan Hedberg 
354e9a416b5SJohan Hedberg 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
355e9a416b5SJohan Hedberg 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
356e9a416b5SJohan Hedberg 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
3571da177e4SLinus Torvalds };
3581da177e4SLinus Torvalds 
35973d80debSLuiz Augusto von Dentz struct hci_chan {
36073d80debSLuiz Augusto von Dentz 	struct list_head list;
36142c4e53eSAndrei Emeltchenko 	__u16 handle;
36273d80debSLuiz Augusto von Dentz 	struct hci_conn *conn;
36373d80debSLuiz Augusto von Dentz 	struct sk_buff_head data_q;
36473d80debSLuiz Augusto von Dentz 	unsigned int	sent;
365168df8e5SMat Martineau 	__u8		state;
36673d80debSLuiz Augusto von Dentz };
36773d80debSLuiz Augusto von Dentz 
3681da177e4SLinus Torvalds extern struct list_head hci_dev_list;
3691da177e4SLinus Torvalds extern struct list_head hci_cb_list;
3701da177e4SLinus Torvalds extern rwlock_t hci_dev_list_lock;
3711da177e4SLinus Torvalds extern rwlock_t hci_cb_list_lock;
3721da177e4SLinus Torvalds 
373686ebf28SUlisses Furquim /* ----- HCI interface to upper protocols ----- */
374686ebf28SUlisses Furquim extern int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
3759e664631SAndrei Emeltchenko extern void l2cap_connect_cfm(struct hci_conn *hcon, u8 status);
376686ebf28SUlisses Furquim extern int l2cap_disconn_ind(struct hci_conn *hcon);
3779e664631SAndrei Emeltchenko extern void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason);
378686ebf28SUlisses Furquim extern int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
379c3c7ea65SGustavo Padovan extern int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb,
380c3c7ea65SGustavo Padovan 			      u16 flags);
381686ebf28SUlisses Furquim 
38220714bfeSFrédéric Dalleau extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
3839e664631SAndrei Emeltchenko extern void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
3849e664631SAndrei Emeltchenko extern void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
385686ebf28SUlisses Furquim extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
386686ebf28SUlisses Furquim 
3871da177e4SLinus Torvalds /* ----- Inquiry cache ----- */
38870f23020SAndrei Emeltchenko #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
38970f23020SAndrei Emeltchenko #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
3901da177e4SLinus Torvalds 
39130883512SJohan Hedberg static inline void discovery_init(struct hci_dev *hdev)
3921da177e4SLinus Torvalds {
393ff9ef578SJohan Hedberg 	hdev->discovery.state = DISCOVERY_STOPPED;
39430883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.all);
39530883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.unknown);
39630883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.resolve);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
39930dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev);
40030dc78e1SJohan Hedberg 
401ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state);
402ff9ef578SJohan Hedberg 
4031da177e4SLinus Torvalds static inline int inquiry_cache_empty(struct hci_dev *hdev)
4041da177e4SLinus Torvalds {
40530883512SJohan Hedberg 	return list_empty(&hdev->discovery.all);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds static inline long inquiry_cache_age(struct hci_dev *hdev)
4091da177e4SLinus Torvalds {
41030883512SJohan Hedberg 	struct discovery_state *c = &hdev->discovery;
4111da177e4SLinus Torvalds 	return jiffies - c->timestamp;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds static inline long inquiry_entry_age(struct inquiry_entry *e)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	return jiffies - e->timestamp;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4195a9d0a3fSWaldemar Rymarkiewicz struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
4205a9d0a3fSWaldemar Rymarkiewicz 					       bdaddr_t *bdaddr);
421561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
422561aafbcSJohan Hedberg 						       bdaddr_t *bdaddr);
42330dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
42430dc78e1SJohan Hedberg 						       bdaddr_t *bdaddr,
42530dc78e1SJohan Hedberg 						       int state);
426a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
427a3d4e20aSJohan Hedberg 				      struct inquiry_entry *ie);
4283175405bSJohan Hedberg bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
429388fc8faSJohan Hedberg 			      bool name_known, bool *ssp);
4301f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds /* ----- HCI Connections ----- */
4331da177e4SLinus Torvalds enum {
4341da177e4SLinus Torvalds 	HCI_CONN_AUTH_PEND,
43519f8def0SWaldemar Rymarkiewicz 	HCI_CONN_REAUTH_PEND,
4361da177e4SLinus Torvalds 	HCI_CONN_ENCRYPT_PEND,
43704837f64SMarcel Holtmann 	HCI_CONN_RSWITCH_PEND,
43804837f64SMarcel Holtmann 	HCI_CONN_MODE_CHANGE_PEND,
439e73439d8SMarcel Holtmann 	HCI_CONN_SCO_SETUP_PEND,
440d26a2345SVinicius Costa Gomes 	HCI_CONN_LE_SMP_PEND,
441b644ba33SJohan Hedberg 	HCI_CONN_MGMT_CONNECTED,
44258a681efSJohan Hedberg 	HCI_CONN_SSP_ENABLED,
44358a681efSJohan Hedberg 	HCI_CONN_POWER_SAVE,
44458a681efSJohan Hedberg 	HCI_CONN_REMOTE_OOB,
4451da177e4SLinus Torvalds };
4461da177e4SLinus Torvalds 
447aa64a8b5SJohan Hedberg static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
448aa64a8b5SJohan Hedberg {
449aa64a8b5SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
450c3c7ea65SGustavo Padovan 	return test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
451c3c7ea65SGustavo Padovan 	       test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
452aa64a8b5SJohan Hedberg }
453aa64a8b5SJohan Hedberg 
4541da177e4SLinus Torvalds static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
457bf4c6325SGustavo F. Padovan 	list_add_rcu(&c->list, &h->list);
458fcd89c09SVille Tervo 	switch (c->type) {
459fcd89c09SVille Tervo 	case ACL_LINK:
4601da177e4SLinus Torvalds 		h->acl_num++;
461fcd89c09SVille Tervo 		break;
462bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
463bd1eb66bSAndrei Emeltchenko 		h->amp_num++;
464bd1eb66bSAndrei Emeltchenko 		break;
465fcd89c09SVille Tervo 	case LE_LINK:
466fcd89c09SVille Tervo 		h->le_num++;
467fcd89c09SVille Tervo 		break;
468fcd89c09SVille Tervo 	case SCO_LINK:
469fcd89c09SVille Tervo 	case ESCO_LINK:
4701da177e4SLinus Torvalds 		h->sco_num++;
471fcd89c09SVille Tervo 		break;
472fcd89c09SVille Tervo 	}
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
478bf4c6325SGustavo F. Padovan 
479bf4c6325SGustavo F. Padovan 	list_del_rcu(&c->list);
480bf4c6325SGustavo F. Padovan 	synchronize_rcu();
481bf4c6325SGustavo F. Padovan 
482fcd89c09SVille Tervo 	switch (c->type) {
483fcd89c09SVille Tervo 	case ACL_LINK:
4841da177e4SLinus Torvalds 		h->acl_num--;
485fcd89c09SVille Tervo 		break;
486bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
487bd1eb66bSAndrei Emeltchenko 		h->amp_num--;
488bd1eb66bSAndrei Emeltchenko 		break;
489fcd89c09SVille Tervo 	case LE_LINK:
490fcd89c09SVille Tervo 		h->le_num--;
491fcd89c09SVille Tervo 		break;
492fcd89c09SVille Tervo 	case SCO_LINK:
493fcd89c09SVille Tervo 	case ESCO_LINK:
4941da177e4SLinus Torvalds 		h->sco_num--;
495fcd89c09SVille Tervo 		break;
496fcd89c09SVille Tervo 	}
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
49952087a79SLuiz Augusto von Dentz static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
50052087a79SLuiz Augusto von Dentz {
50152087a79SLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
50252087a79SLuiz Augusto von Dentz 	switch (type) {
50352087a79SLuiz Augusto von Dentz 	case ACL_LINK:
50452087a79SLuiz Augusto von Dentz 		return h->acl_num;
505bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
506bd1eb66bSAndrei Emeltchenko 		return h->amp_num;
50752087a79SLuiz Augusto von Dentz 	case LE_LINK:
50852087a79SLuiz Augusto von Dentz 		return h->le_num;
50952087a79SLuiz Augusto von Dentz 	case SCO_LINK:
51052087a79SLuiz Augusto von Dentz 	case ESCO_LINK:
51152087a79SLuiz Augusto von Dentz 		return h->sco_num;
51252087a79SLuiz Augusto von Dentz 	default:
51352087a79SLuiz Augusto von Dentz 		return 0;
51452087a79SLuiz Augusto von Dentz 	}
51552087a79SLuiz Augusto von Dentz }
51652087a79SLuiz Augusto von Dentz 
5171da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
5181da177e4SLinus Torvalds 								__u16 handle)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
5211da177e4SLinus Torvalds 	struct hci_conn  *c;
5221da177e4SLinus Torvalds 
523bf4c6325SGustavo F. Padovan 	rcu_read_lock();
524bf4c6325SGustavo F. Padovan 
525bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
526bf4c6325SGustavo F. Padovan 		if (c->handle == handle) {
527bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5281da177e4SLinus Torvalds 			return c;
5291da177e4SLinus Torvalds 		}
530bf4c6325SGustavo F. Padovan 	}
531bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
532bf4c6325SGustavo F. Padovan 
5331da177e4SLinus Torvalds 	return NULL;
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
5371da177e4SLinus Torvalds 							__u8 type, bdaddr_t *ba)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
5401da177e4SLinus Torvalds 	struct hci_conn  *c;
5411da177e4SLinus Torvalds 
542bf4c6325SGustavo F. Padovan 	rcu_read_lock();
543bf4c6325SGustavo F. Padovan 
544bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
545bf4c6325SGustavo F. Padovan 		if (c->type == type && !bacmp(&c->dst, ba)) {
546bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5471da177e4SLinus Torvalds 			return c;
5481da177e4SLinus Torvalds 		}
549bf4c6325SGustavo F. Padovan 	}
550bf4c6325SGustavo F. Padovan 
551bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
552bf4c6325SGustavo F. Padovan 
5531da177e4SLinus Torvalds 	return NULL;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5564c67bc74SMarcel Holtmann static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
5574c67bc74SMarcel Holtmann 							__u8 type, __u16 state)
5584c67bc74SMarcel Holtmann {
5594c67bc74SMarcel Holtmann 	struct hci_conn_hash *h = &hdev->conn_hash;
5604c67bc74SMarcel Holtmann 	struct hci_conn  *c;
5614c67bc74SMarcel Holtmann 
562bf4c6325SGustavo F. Padovan 	rcu_read_lock();
563bf4c6325SGustavo F. Padovan 
564bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
565bf4c6325SGustavo F. Padovan 		if (c->type == type && c->state == state) {
566bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5674c67bc74SMarcel Holtmann 			return c;
5684c67bc74SMarcel Holtmann 		}
569bf4c6325SGustavo F. Padovan 	}
570bf4c6325SGustavo F. Padovan 
571bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
572bf4c6325SGustavo F. Padovan 
5734c67bc74SMarcel Holtmann 	return NULL;
5744c67bc74SMarcel Holtmann }
5754c67bc74SMarcel Holtmann 
576bed71748SAndre Guedes void hci_disconnect(struct hci_conn *conn, __u8 reason);
5772dea632fSFrédéric Dalleau bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
578e73439d8SMarcel Holtmann void hci_sco_setup(struct hci_conn *conn, __u8 status);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
5811da177e4SLinus Torvalds int hci_conn_del(struct hci_conn *conn);
5821da177e4SLinus Torvalds void hci_conn_hash_flush(struct hci_dev *hdev);
583a9de9248SMarcel Holtmann void hci_conn_check_pending(struct hci_dev *hdev);
5841da177e4SLinus Torvalds 
58573d80debSLuiz Augusto von Dentz struct hci_chan *hci_chan_create(struct hci_conn *conn);
5869472007cSAndrei Emeltchenko void hci_chan_del(struct hci_chan *chan);
5872c33c06aSGustavo F. Padovan void hci_chan_list_flush(struct hci_conn *conn);
58842c4e53eSAndrei Emeltchenko struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
58973d80debSLuiz Augusto von Dentz 
5905a9d0a3fSWaldemar Rymarkiewicz struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,
591b12f62cfSAndre Guedes 			     __u8 dst_type, __u8 sec_level, __u8 auth_type);
59210c62ddcSFrédéric Dalleau struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
59310c62ddcSFrédéric Dalleau 				 __u16 setting);
594e7c29cb1SMarcel Holtmann int hci_conn_check_link_mode(struct hci_conn *conn);
595b3b1b061SWaldemar Rymarkiewicz int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
5960684e5f9SMarcel Holtmann int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
5971da177e4SLinus Torvalds int hci_conn_change_link_key(struct hci_conn *conn);
5988c1b2355SMarcel Holtmann int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
5991da177e4SLinus Torvalds 
60014b12d0bSJaikumar Ganesh void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
6011da177e4SLinus Torvalds 
6028d12356fSDavid Herrmann /*
6038d12356fSDavid Herrmann  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
6048d12356fSDavid Herrmann  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
6058d12356fSDavid Herrmann  * working or anything else. They just guarantee that the object is available
6068d12356fSDavid Herrmann  * and can be dereferenced. So you can use its locks, local variables and any
6078d12356fSDavid Herrmann  * other constant data.
6088d12356fSDavid Herrmann  * Before accessing runtime data, you _must_ lock the object and then check that
6098d12356fSDavid Herrmann  * it is still running. As soon as you release the locks, the connection might
6108d12356fSDavid Herrmann  * get dropped, though.
6118d12356fSDavid Herrmann  *
6128d12356fSDavid Herrmann  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
6138d12356fSDavid Herrmann  * how long the underlying connection is held. So every channel that runs on the
6148d12356fSDavid Herrmann  * hci_conn object calls this to prevent the connection from disappearing. As
6158d12356fSDavid Herrmann  * long as you hold a device, you must also guarantee that you have a valid
6168d12356fSDavid Herrmann  * reference to the device via hci_conn_get() (or the initial reference from
6178d12356fSDavid Herrmann  * hci_conn_add()).
6188d12356fSDavid Herrmann  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
6198d12356fSDavid Herrmann  * break because nobody cares for that. But this means, we cannot use
6208d12356fSDavid Herrmann  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
6218d12356fSDavid Herrmann  */
6228d12356fSDavid Herrmann 
6238d12356fSDavid Herrmann static inline void hci_conn_get(struct hci_conn *conn)
6248d12356fSDavid Herrmann {
6258d12356fSDavid Herrmann 	get_device(&conn->dev);
6268d12356fSDavid Herrmann }
6278d12356fSDavid Herrmann 
6288d12356fSDavid Herrmann static inline void hci_conn_put(struct hci_conn *conn)
6298d12356fSDavid Herrmann {
6308d12356fSDavid Herrmann 	put_device(&conn->dev);
6318d12356fSDavid Herrmann }
6328d12356fSDavid Herrmann 
6331da177e4SLinus Torvalds static inline void hci_conn_hold(struct hci_conn *conn)
6341da177e4SLinus Torvalds {
63571becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
63638b3fef1SAndrei Emeltchenko 
6371da177e4SLinus Torvalds 	atomic_inc(&conn->refcnt);
6382f304d1eSAndre Guedes 	cancel_delayed_work(&conn->disc_work);
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
64176a68ba0SDavid Herrmann static inline void hci_conn_drop(struct hci_conn *conn)
6421da177e4SLinus Torvalds {
64371becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
64438b3fef1SAndrei Emeltchenko 
6451da177e4SLinus Torvalds 	if (atomic_dec_and_test(&conn->refcnt)) {
64604837f64SMarcel Holtmann 		unsigned long timeo;
647716e4ab5SAndrei Emeltchenko 
648716e4ab5SAndrei Emeltchenko 		switch (conn->type) {
649716e4ab5SAndrei Emeltchenko 		case ACL_LINK:
650716e4ab5SAndrei Emeltchenko 		case LE_LINK:
6516ac59344SMarcel Holtmann 			del_timer(&conn->idle_timer);
6526ac59344SMarcel Holtmann 			if (conn->state == BT_CONNECTED) {
6535f246e89SAndrei Emeltchenko 				timeo = conn->disc_timeout;
65404837f64SMarcel Holtmann 				if (!conn->out)
655052b30b0SMarcel Holtmann 					timeo *= 2;
6565a9d0a3fSWaldemar Rymarkiewicz 			} else {
6576ac59344SMarcel Holtmann 				timeo = msecs_to_jiffies(10);
6585a9d0a3fSWaldemar Rymarkiewicz 			}
659716e4ab5SAndrei Emeltchenko 			break;
660716e4ab5SAndrei Emeltchenko 
661716e4ab5SAndrei Emeltchenko 		case AMP_LINK:
662716e4ab5SAndrei Emeltchenko 			timeo = conn->disc_timeout;
663716e4ab5SAndrei Emeltchenko 			break;
664716e4ab5SAndrei Emeltchenko 
665716e4ab5SAndrei Emeltchenko 		default:
66604837f64SMarcel Holtmann 			timeo = msecs_to_jiffies(10);
667716e4ab5SAndrei Emeltchenko 			break;
6685a9d0a3fSWaldemar Rymarkiewicz 		}
669716e4ab5SAndrei Emeltchenko 
6702f304d1eSAndre Guedes 		cancel_delayed_work(&conn->disc_work);
67119c40e3bSGustavo F. Padovan 		queue_delayed_work(conn->hdev->workqueue,
6721931782bSVinicius Costa Gomes 				   &conn->disc_work, timeo);
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /* ----- HCI Devices ----- */
677dc946bd8SDavid Herrmann static inline void hci_dev_put(struct hci_dev *d)
6781da177e4SLinus Torvalds {
679376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
680376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
681376261aeSAndrei Emeltchenko 
6824c724c71SDavid Herrmann 	put_device(&d->dev);
683010666a1SDavid Herrmann }
6841da177e4SLinus Torvalds 
685dc946bd8SDavid Herrmann static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
6861da177e4SLinus Torvalds {
687376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
688376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
689376261aeSAndrei Emeltchenko 
6904c724c71SDavid Herrmann 	get_device(&d->dev);
6911da177e4SLinus Torvalds 	return d;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
69409fd0de5SGustavo F. Padovan #define hci_dev_lock(d)		mutex_lock(&d->lock)
69509fd0de5SGustavo F. Padovan #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
6961da177e4SLinus Torvalds 
697aa2b86d7SDavid Herrmann #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
6983dc07322SDavid Herrmann #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
699aa2b86d7SDavid Herrmann 
700155961e8SDavid Herrmann static inline void *hci_get_drvdata(struct hci_dev *hdev)
701155961e8SDavid Herrmann {
702155961e8SDavid Herrmann 	return dev_get_drvdata(&hdev->dev);
703155961e8SDavid Herrmann }
704155961e8SDavid Herrmann 
705155961e8SDavid Herrmann static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
706155961e8SDavid Herrmann {
707155961e8SDavid Herrmann 	dev_set_drvdata(&hdev->dev, data);
708155961e8SDavid Herrmann }
709155961e8SDavid Herrmann 
7101da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index);
7110c0afedfSJohan Hedberg struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds struct hci_dev *hci_alloc_dev(void);
7141da177e4SLinus Torvalds void hci_free_dev(struct hci_dev *hdev);
7151da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev);
71659735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev);
7171da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev);
7181da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev);
7191da177e4SLinus Torvalds int hci_dev_open(__u16 dev);
7201da177e4SLinus Torvalds int hci_dev_close(__u16 dev);
7211da177e4SLinus Torvalds int hci_dev_reset(__u16 dev);
7221da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev);
7231da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg);
7241da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg);
7251da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg);
7261da177e4SLinus Torvalds int hci_get_conn_list(void __user *arg);
7271da177e4SLinus Torvalds int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
72840be492fSMarcel Holtmann int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
7291da177e4SLinus Torvalds int hci_inquiry(void __user *arg);
7301da177e4SLinus Torvalds 
731c3c7ea65SGustavo Padovan struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
732c3c7ea65SGustavo Padovan 					 bdaddr_t *bdaddr);
733f0358568SJohan Hedberg int hci_blacklist_clear(struct hci_dev *hdev);
73488c1fe4bSJohan Hedberg int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
73588c1fe4bSJohan Hedberg int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
736f0358568SJohan Hedberg 
7372aeb9a1aSJohan Hedberg int hci_uuids_clear(struct hci_dev *hdev);
7382aeb9a1aSJohan Hedberg 
73955ed8ca1SJohan Hedberg int hci_link_keys_clear(struct hci_dev *hdev);
74055ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
741d25e28abSJohan Hedberg int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
742d25e28abSJohan Hedberg 		     bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len);
743c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]);
744c9839a11SVinicius Costa Gomes int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
7459a006657SAndrei Emeltchenko 		int new_key, u8 authenticated, u8 tk[16], u8 enc_size,
7469a006657SAndrei Emeltchenko 		__le16 ediv, u8 rand[8]);
747c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
748c9839a11SVinicius Costa Gomes 				     u8 addr_type);
749b899efafSVinicius Costa Gomes int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr);
750b899efafSVinicius Costa Gomes int hci_smp_ltks_clear(struct hci_dev *hdev);
75155ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
75255ed8ca1SJohan Hedberg 
7532763eda6SSzymon Janc int hci_remote_oob_data_clear(struct hci_dev *hdev);
7542763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
7552763eda6SSzymon Janc 							bdaddr_t *bdaddr);
7562763eda6SSzymon Janc int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
7572763eda6SSzymon Janc 								u8 *randomizer);
7582763eda6SSzymon Janc int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
7592763eda6SSzymon Janc 
7601da177e4SLinus Torvalds void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
7611da177e4SLinus Torvalds 
762e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
763ef222013SMarcel Holtmann int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
76499811510SSuraj Sumangala int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
765ef222013SMarcel Holtmann 
7660ac7e700SDavid Herrmann void hci_init_sysfs(struct hci_dev *hdev);
767ce242970SDavid Herrmann int hci_add_sysfs(struct hci_dev *hdev);
768ce242970SDavid Herrmann void hci_del_sysfs(struct hci_dev *hdev);
769a67e899cSMarcel Holtmann void hci_conn_init_sysfs(struct hci_conn *conn);
770b219e3acSMarcel Holtmann void hci_conn_add_sysfs(struct hci_conn *conn);
771b219e3acSMarcel Holtmann void hci_conn_del_sysfs(struct hci_conn *conn);
7721da177e4SLinus Torvalds 
7736935e0f5SDavid Herrmann #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /* ----- LMP capabilities ----- */
776cad718edSJohan Hedberg #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
777cad718edSJohan Hedberg #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
778cad718edSJohan Hedberg #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
779cad718edSJohan Hedberg #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
780cad718edSJohan Hedberg #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
781cad718edSJohan Hedberg #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
782cad718edSJohan Hedberg #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
783cad718edSJohan Hedberg #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
784cad718edSJohan Hedberg #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
785cad718edSJohan Hedberg #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
786cad718edSJohan Hedberg #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
787cad718edSJohan Hedberg #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
788cad718edSJohan Hedberg #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
789cad718edSJohan Hedberg #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
790cad718edSJohan Hedberg #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
791cad718edSJohan Hedberg #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
792cad718edSJohan Hedberg #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
793cad718edSJohan Hedberg #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
79407a5c61eSFrédéric Dalleau #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
7951da177e4SLinus Torvalds 
796eead27daSAndre Guedes /* ----- Extended LMP capabilities ----- */
797cad718edSJohan Hedberg #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
798cad718edSJohan Hedberg #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
799cad718edSJohan Hedberg #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
800eead27daSAndre Guedes 
8011da177e4SLinus Torvalds /* ----- HCI protocols ----- */
80220714bfeSFrédéric Dalleau #define HCI_PROTO_DEFER             0x01
80320714bfeSFrédéric Dalleau 
8045a9d0a3fSWaldemar Rymarkiewicz static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
80520714bfeSFrédéric Dalleau 					__u8 type, __u8 *flags)
8061da177e4SLinus Torvalds {
807686ebf28SUlisses Furquim 	switch (type) {
808686ebf28SUlisses Furquim 	case ACL_LINK:
809686ebf28SUlisses Furquim 		return l2cap_connect_ind(hdev, bdaddr);
8101da177e4SLinus Torvalds 
811686ebf28SUlisses Furquim 	case SCO_LINK:
812686ebf28SUlisses Furquim 	case ESCO_LINK:
81320714bfeSFrédéric Dalleau 		return sco_connect_ind(hdev, bdaddr, flags);
8141da177e4SLinus Torvalds 
815686ebf28SUlisses Furquim 	default:
816686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", type);
817686ebf28SUlisses Furquim 		return -EINVAL;
818686ebf28SUlisses Furquim 	}
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
8221da177e4SLinus Torvalds {
823686ebf28SUlisses Furquim 	switch (conn->type) {
824686ebf28SUlisses Furquim 	case ACL_LINK:
825686ebf28SUlisses Furquim 	case LE_LINK:
826686ebf28SUlisses Furquim 		l2cap_connect_cfm(conn, status);
827686ebf28SUlisses Furquim 		break;
8281da177e4SLinus Torvalds 
829686ebf28SUlisses Furquim 	case SCO_LINK:
830686ebf28SUlisses Furquim 	case ESCO_LINK:
831686ebf28SUlisses Furquim 		sco_connect_cfm(conn, status);
832686ebf28SUlisses Furquim 		break;
8331da177e4SLinus Torvalds 
834686ebf28SUlisses Furquim 	default:
835686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", conn->type);
836686ebf28SUlisses Furquim 		break;
837686ebf28SUlisses Furquim 	}
838e9a416b5SJohan Hedberg 
839e9a416b5SJohan Hedberg 	if (conn->connect_cfm_cb)
840e9a416b5SJohan Hedberg 		conn->connect_cfm_cb(conn, status);
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8432950f21aSMarcel Holtmann static inline int hci_proto_disconn_ind(struct hci_conn *conn)
8442950f21aSMarcel Holtmann {
845686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
846686ebf28SUlisses Furquim 		return HCI_ERROR_REMOTE_USER_TERM;
8472950f21aSMarcel Holtmann 
848686ebf28SUlisses Furquim 	return l2cap_disconn_ind(conn);
8492950f21aSMarcel Holtmann }
8502950f21aSMarcel Holtmann 
8512950f21aSMarcel Holtmann static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
8521da177e4SLinus Torvalds {
853686ebf28SUlisses Furquim 	switch (conn->type) {
854686ebf28SUlisses Furquim 	case ACL_LINK:
855686ebf28SUlisses Furquim 	case LE_LINK:
856686ebf28SUlisses Furquim 		l2cap_disconn_cfm(conn, reason);
857686ebf28SUlisses Furquim 		break;
8581da177e4SLinus Torvalds 
859686ebf28SUlisses Furquim 	case SCO_LINK:
860686ebf28SUlisses Furquim 	case ESCO_LINK:
861686ebf28SUlisses Furquim 		sco_disconn_cfm(conn, reason);
862686ebf28SUlisses Furquim 		break;
8631da177e4SLinus Torvalds 
864bd1eb66bSAndrei Emeltchenko 	/* L2CAP would be handled for BREDR chan */
865bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
866bd1eb66bSAndrei Emeltchenko 		break;
867bd1eb66bSAndrei Emeltchenko 
868686ebf28SUlisses Furquim 	default:
869686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", conn->type);
870686ebf28SUlisses Furquim 		break;
871686ebf28SUlisses Furquim 	}
872e9a416b5SJohan Hedberg 
873e9a416b5SJohan Hedberg 	if (conn->disconn_cfm_cb)
874e9a416b5SJohan Hedberg 		conn->disconn_cfm_cb(conn, reason);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
8781da177e4SLinus Torvalds {
8798c1b2355SMarcel Holtmann 	__u8 encrypt;
8808c1b2355SMarcel Holtmann 
881686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
882686ebf28SUlisses Furquim 		return;
883686ebf28SUlisses Furquim 
88451a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
8858c1b2355SMarcel Holtmann 		return;
8868c1b2355SMarcel Holtmann 
8878c1b2355SMarcel Holtmann 	encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
888686ebf28SUlisses Furquim 	l2cap_security_cfm(conn, status, encrypt);
889e9a416b5SJohan Hedberg 
890e9a416b5SJohan Hedberg 	if (conn->security_cfm_cb)
891e9a416b5SJohan Hedberg 		conn->security_cfm_cb(conn, status);
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8945a9d0a3fSWaldemar Rymarkiewicz static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status,
8955a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
8961da177e4SLinus Torvalds {
897686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
898686ebf28SUlisses Furquim 		return;
8991da177e4SLinus Torvalds 
900686ebf28SUlisses Furquim 	l2cap_security_cfm(conn, status, encrypt);
901e9a416b5SJohan Hedberg 
902e9a416b5SJohan Hedberg 	if (conn->security_cfm_cb)
903e9a416b5SJohan Hedberg 		conn->security_cfm_cb(conn, status);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds /* ----- HCI callbacks ----- */
9071da177e4SLinus Torvalds struct hci_cb {
9081da177e4SLinus Torvalds 	struct list_head list;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	char *name;
9111da177e4SLinus Torvalds 
9125a9d0a3fSWaldemar Rymarkiewicz 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
9135a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt);
9141da177e4SLinus Torvalds 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
9151da177e4SLinus Torvalds 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
9161da177e4SLinus Torvalds };
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
9191da177e4SLinus Torvalds {
920711584eaSDenis Kirjanov 	struct hci_cb *cb;
9218c1b2355SMarcel Holtmann 	__u8 encrypt;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	hci_proto_auth_cfm(conn, status);
9241da177e4SLinus Torvalds 
92551a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
9268c1b2355SMarcel Holtmann 		return;
9278c1b2355SMarcel Holtmann 
9288c1b2355SMarcel Holtmann 	encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
9298c1b2355SMarcel Holtmann 
930f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
931711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9328c1b2355SMarcel Holtmann 		if (cb->security_cfm)
9338c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
9341da177e4SLinus Torvalds 	}
935f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9385a9d0a3fSWaldemar Rymarkiewicz static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
9395a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
9401da177e4SLinus Torvalds {
941711584eaSDenis Kirjanov 	struct hci_cb *cb;
9421da177e4SLinus Torvalds 
943435fef20SMarcel Holtmann 	if (conn->sec_level == BT_SECURITY_SDP)
944435fef20SMarcel Holtmann 		conn->sec_level = BT_SECURITY_LOW;
945435fef20SMarcel Holtmann 
94688167aedSVinicius Costa Gomes 	if (conn->pending_sec_level > conn->sec_level)
94788167aedSVinicius Costa Gomes 		conn->sec_level = conn->pending_sec_level;
94888167aedSVinicius Costa Gomes 
9499719f8afSMarcel Holtmann 	hci_proto_encrypt_cfm(conn, status, encrypt);
9501da177e4SLinus Torvalds 
951f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
952711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9538c1b2355SMarcel Holtmann 		if (cb->security_cfm)
9548c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
9551da177e4SLinus Torvalds 	}
956f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
9601da177e4SLinus Torvalds {
961711584eaSDenis Kirjanov 	struct hci_cb *cb;
9621da177e4SLinus Torvalds 
963f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
964711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9651da177e4SLinus Torvalds 		if (cb->key_change_cfm)
9661da177e4SLinus Torvalds 			cb->key_change_cfm(conn, status);
9671da177e4SLinus Torvalds 	}
968f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9715a9d0a3fSWaldemar Rymarkiewicz static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
9725a9d0a3fSWaldemar Rymarkiewicz 								__u8 role)
9731da177e4SLinus Torvalds {
974711584eaSDenis Kirjanov 	struct hci_cb *cb;
9751da177e4SLinus Torvalds 
976f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
977711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9781da177e4SLinus Torvalds 		if (cb->role_switch_cfm)
9791da177e4SLinus Torvalds 			cb->role_switch_cfm(conn, status, role);
9801da177e4SLinus Torvalds 	}
981f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
9846759a675SJohan Hedberg static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
9856759a675SJohan Hedberg {
98684d9d071SJohan Hedberg 	size_t parsed = 0;
9876759a675SJohan Hedberg 
9886c0c331eSJohan Hedberg 	if (data_len < 2)
9896c0c331eSJohan Hedberg 		return false;
9906c0c331eSJohan Hedberg 
99184d9d071SJohan Hedberg 	while (parsed < data_len - 1) {
99284d9d071SJohan Hedberg 		u8 field_len = data[0];
9936759a675SJohan Hedberg 
9946759a675SJohan Hedberg 		if (field_len == 0)
9956759a675SJohan Hedberg 			break;
9966759a675SJohan Hedberg 
9976759a675SJohan Hedberg 		parsed += field_len + 1;
9986759a675SJohan Hedberg 
9996759a675SJohan Hedberg 		if (parsed > data_len)
10006759a675SJohan Hedberg 			break;
10016759a675SJohan Hedberg 
10026759a675SJohan Hedberg 		if (data[1] == type)
10036759a675SJohan Hedberg 			return true;
10046759a675SJohan Hedberg 
10056759a675SJohan Hedberg 		data += field_len + 1;
10066759a675SJohan Hedberg 	}
10076759a675SJohan Hedberg 
10086759a675SJohan Hedberg 	return false;
10096759a675SJohan Hedberg }
10106759a675SJohan Hedberg 
10119d939d94SVishal Agarwal static inline size_t eir_get_length(u8 *eir, size_t eir_len)
10129d939d94SVishal Agarwal {
10139d939d94SVishal Agarwal 	size_t parsed = 0;
10149d939d94SVishal Agarwal 
10159d939d94SVishal Agarwal 	while (parsed < eir_len) {
10169d939d94SVishal Agarwal 		u8 field_len = eir[0];
10179d939d94SVishal Agarwal 
10189d939d94SVishal Agarwal 		if (field_len == 0)
10199d939d94SVishal Agarwal 			return parsed;
10209d939d94SVishal Agarwal 
10219d939d94SVishal Agarwal 		parsed += field_len + 1;
10229d939d94SVishal Agarwal 		eir += field_len + 1;
10239d939d94SVishal Agarwal 	}
10249d939d94SVishal Agarwal 
10259d939d94SVishal Agarwal 	return eir_len;
10269d939d94SVishal Agarwal }
10279d939d94SVishal Agarwal 
10281dc06093SJohan Hedberg static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
10291dc06093SJohan Hedberg 				  u8 data_len)
10301dc06093SJohan Hedberg {
10311dc06093SJohan Hedberg 	eir[eir_len++] = sizeof(type) + data_len;
10321dc06093SJohan Hedberg 	eir[eir_len++] = type;
10331dc06093SJohan Hedberg 	memcpy(&eir[eir_len], data, data_len);
10341dc06093SJohan Hedberg 	eir_len += data_len;
10351dc06093SJohan Hedberg 
10361dc06093SJohan Hedberg 	return eir_len;
10371dc06093SJohan Hedberg }
10381dc06093SJohan Hedberg 
10391da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *hcb);
10401da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *hcb);
10411da177e4SLinus Torvalds 
10423119ae95SJohan Hedberg struct hci_request {
10433119ae95SJohan Hedberg 	struct hci_dev		*hdev;
10443119ae95SJohan Hedberg 	struct sk_buff_head	cmd_q;
10455d73e034SAndre Guedes 
10465d73e034SAndre Guedes 	/* If something goes wrong when building the HCI request, the error
10475d73e034SAndre Guedes 	 * value is stored in this field.
10485d73e034SAndre Guedes 	 */
10495d73e034SAndre Guedes 	int			err;
10503119ae95SJohan Hedberg };
10513119ae95SJohan Hedberg 
10523119ae95SJohan Hedberg void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
10533119ae95SJohan Hedberg int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
105407dc93ddSJohan Hedberg void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
105507dc93ddSJohan Hedberg 		 const void *param);
105607dc93ddSJohan Hedberg void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
105707dc93ddSJohan Hedberg 		    const void *param, u8 event);
10589238f36aSJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
10593119ae95SJohan Hedberg 
106075e84b7cSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
106107dc93ddSJohan Hedberg 			       const void *param, u32 timeout);
10627b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
106307dc93ddSJohan Hedberg 				  const void *param, u8 event, u32 timeout);
106475e84b7cSJohan Hedberg 
106507dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
106607dc93ddSJohan Hedberg 		 const void *param);
106773d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
10680d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
10691da177e4SLinus Torvalds 
1070a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds /* ----- HCI Sockets ----- */
1073470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
1074470fe1b5SMarcel Holtmann void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk);
1075cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
10761da177e4SLinus Torvalds 
1077040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event);
1078040030efSMarcel Holtmann 
10790381101fSJohan Hedberg /* Management interface */
1080591f47f3SAndre Guedes #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
1081591f47f3SAndre Guedes #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
1082591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1083591f47f3SAndre Guedes #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
1084591f47f3SAndre Guedes 					 BIT(BDADDR_LE_PUBLIC) | \
1085591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1086f39799f5SAndre Guedes 
10870d8cc935SAndre Guedes /* These LE scan and inquiry parameters were chosen according to LE General
10880d8cc935SAndre Guedes  * Discovery Procedure specification.
10890d8cc935SAndre Guedes  */
10900d8cc935SAndre Guedes #define DISCOV_LE_SCAN_WIN		0x12
10910d8cc935SAndre Guedes #define DISCOV_LE_SCAN_INT		0x12
10920d8cc935SAndre Guedes #define DISCOV_LE_TIMEOUT		msecs_to_jiffies(10240)
10930d8cc935SAndre Guedes #define DISCOV_INTERLEAVED_TIMEOUT	msecs_to_jiffies(5120)
10940d8cc935SAndre Guedes #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
10950d8cc935SAndre Guedes #define DISCOV_BREDR_INQUIRY_LEN	0x08
10960d8cc935SAndre Guedes 
10970381101fSJohan Hedberg int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
1098bf6b56dbSMarcel Holtmann void mgmt_index_added(struct hci_dev *hdev);
1099bf6b56dbSMarcel Holtmann void mgmt_index_removed(struct hci_dev *hdev);
11003eec705eSMarcel Holtmann void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
1101744cf19eSJohan Hedberg int mgmt_powered(struct hci_dev *hdev, u8 powered);
1102744cf19eSJohan Hedberg int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
1103744cf19eSJohan Hedberg int mgmt_connectable(struct hci_dev *hdev, u8 connectable);
1104744cf19eSJohan Hedberg int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status);
1105744cf19eSJohan Hedberg int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1106745c0ce3SVishal Agarwal 		      bool persistent);
1107ecd90ae7SMarcel Holtmann void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
110804124681SGustavo F. Padovan 			   u8 addr_type, u32 flags, u8 *name, u8 name_len,
110904124681SGustavo F. Padovan 			   u8 *dev_class);
11109b80ec5eSMarcel Holtmann void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
1111f0d6a0eaSMikel Astiz 			      u8 link_type, u8 addr_type, u8 reason);
11127892924cSMarcel Holtmann void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
111388c3df13SJohan Hedberg 			    u8 link_type, u8 addr_type, u8 status);
1114445608d0SMarcel Holtmann void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
111548264f06SJohan Hedberg 			 u8 addr_type, u8 status);
1116744cf19eSJohan Hedberg int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
1117744cf19eSJohan Hedberg int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1118c35938b2SSzymon Janc 				 u8 status);
1119744cf19eSJohan Hedberg int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1120744cf19eSJohan Hedberg 				     u8 status);
1121744cf19eSJohan Hedberg int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1122272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type, __le32 value,
1123272d90dfSJohan Hedberg 			      u8 confirm_hint);
1124744cf19eSJohan Hedberg int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1125272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1126272d90dfSJohan Hedberg int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1127272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
1128272d90dfSJohan Hedberg int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1129272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type);
1130604086b7SBrian Gix int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1131272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1132272d90dfSJohan Hedberg int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1133272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
113492a25256SJohan Hedberg int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
113592a25256SJohan Hedberg 			     u8 link_type, u8 addr_type, u32 passkey,
113692a25256SJohan Hedberg 			     u8 entered);
1137bab73cb6SJohan Hedberg int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1138bab73cb6SJohan Hedberg 		     u8 addr_type, u8 status);
113933ef95edSJohan Hedberg int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
1140c0ecddc2SJohan Hedberg int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
11417f9a903cSMarcel Holtmann int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
11427f9a903cSMarcel Holtmann 				   u8 status);
1143744cf19eSJohan Hedberg int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
1144744cf19eSJohan Hedberg int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
1145744cf19eSJohan Hedberg 					    u8 *randomizer, u8 status);
1146901801b9SMarcel Holtmann void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
114704124681SGustavo F. Padovan 		       u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
114804124681SGustavo F. Padovan 		       u8 ssp, u8 *eir, u16 eir_len);
11499cf12aeeSMarcel Holtmann void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1150b644ba33SJohan Hedberg 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
11512f1e063bSMarcel Holtmann void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
115288c1fe4bSJohan Hedberg int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
115388c1fe4bSJohan Hedberg int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1154346af67bSVinicius Costa Gomes int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent);
11555976e608SMarcel Holtmann void mgmt_reenable_advertising(struct hci_dev *hdev);
1156346af67bSVinicius Costa Gomes 
11571da177e4SLinus Torvalds /* HCI info for socket */
11581da177e4SLinus Torvalds #define hci_pi(sk) ((struct hci_pinfo *) sk)
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds struct hci_pinfo {
11611da177e4SLinus Torvalds 	struct bt_sock    bt;
11621da177e4SLinus Torvalds 	struct hci_dev    *hdev;
11631da177e4SLinus Torvalds 	struct hci_filter filter;
11641da177e4SLinus Torvalds 	__u32             cmsg_mask;
1165c02178d2SJohan Hedberg 	unsigned short   channel;
11661da177e4SLinus Torvalds };
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds /* HCI security filter */
11691da177e4SLinus Torvalds #define HCI_SFLT_MAX_OGF  5
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds struct hci_sec_filter {
11721da177e4SLinus Torvalds 	__u32 type_mask;
11731da177e4SLinus Torvalds 	__u32 event_mask[2];
11741da177e4SLinus Torvalds 	__u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
11751da177e4SLinus Torvalds };
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds /* ----- HCI requests ----- */
11781da177e4SLinus Torvalds #define HCI_REQ_DONE	  0
11791da177e4SLinus Torvalds #define HCI_REQ_PEND	  1
11801da177e4SLinus Torvalds #define HCI_REQ_CANCELED  2
11811da177e4SLinus Torvalds 
1182a6a67efdSThomas Gleixner #define hci_req_lock(d)		mutex_lock(&d->req_lock)
1183a6a67efdSThomas Gleixner #define hci_req_unlock(d)	mutex_unlock(&d->req_lock)
11841da177e4SLinus Torvalds 
118504b4edcbSJohan Hedberg void hci_update_ad(struct hci_request *req);
118604b4edcbSJohan Hedberg 
11872ce603ebSClaudio Takahasi void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
11882ce603ebSClaudio Takahasi 					u16 latency, u16 to_multiplier);
1189a7a595f6SVinicius Costa Gomes void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
1190a7a595f6SVinicius Costa Gomes 							__u8 ltk[16]);
11912519a1fcSAndre Guedes 
11925d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_MASK       0x0003
11935d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_CVSD       0x0000
11945d4d62f6SFrédéric Dalleau #define SCO_AIRMODE_TRANSP     0x0003
11955d4d62f6SFrédéric Dalleau 
11961da177e4SLinus Torvalds #endif /* __HCI_CORE_H */
1197