xref: /openbmc/linux/include/net/bluetooth/hci_core.h (revision 8d12356f)
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 
1207ba8b4beSAndre Guedes struct le_scan_params {
1217ba8b4beSAndre Guedes 	u8 type;
1227ba8b4beSAndre Guedes 	u16 interval;
1237ba8b4beSAndre Guedes 	u16 window;
12428b75a89SAndre Guedes 	int timeout;
1257ba8b4beSAndre Guedes };
1267ba8b4beSAndre Guedes 
127490c5babSJohan Hedberg #define HCI_MAX_SHORT_NAME_LENGTH	10
128490c5babSJohan Hedberg 
129903e4541SAndrei Emeltchenko struct amp_assoc {
130903e4541SAndrei Emeltchenko 	__u16	len;
131903e4541SAndrei Emeltchenko 	__u16	offset;
13293c284eeSAndrei Emeltchenko 	__u16	rem_len;
13393c284eeSAndrei Emeltchenko 	__u16	len_so_far;
134903e4541SAndrei Emeltchenko 	__u8	data[HCI_MAX_AMP_ASSOC_SIZE];
135903e4541SAndrei Emeltchenko };
136903e4541SAndrei Emeltchenko 
137cd4c5391SSuraj Sumangala #define NUM_REASSEMBLY 4
1381da177e4SLinus Torvalds struct hci_dev {
1391da177e4SLinus Torvalds 	struct list_head list;
14009fd0de5SGustavo F. Padovan 	struct mutex	lock;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	char		name[8];
1431da177e4SLinus Torvalds 	unsigned long	flags;
1441da177e4SLinus Torvalds 	__u16		id;
145c13854ceSMarcel Holtmann 	__u8		bus;
146943da25dSMarcel Holtmann 	__u8		dev_type;
1471da177e4SLinus Torvalds 	bdaddr_t	bdaddr;
1481f6c6378SJohan Hedberg 	__u8		dev_name[HCI_MAX_NAME_LENGTH];
149490c5babSJohan Hedberg 	__u8		short_name[HCI_MAX_SHORT_NAME_LENGTH];
15080a1e1dbSJohan Hedberg 	__u8		eir[HCI_MAX_EIR_LENGTH];
151a9de9248SMarcel Holtmann 	__u8		dev_class[3];
1521aff6f09SJohan Hedberg 	__u8		major_class;
1531aff6f09SJohan Hedberg 	__u8		minor_class;
1541da177e4SLinus Torvalds 	__u8		features[8];
15559e29406SAndre Guedes 	__u8		host_features[8];
15660e77321SJohan Hedberg 	__u8		le_features[8];
157cf1d081fSJohan Hedberg 	__u8		le_white_list_size;
1589b008c04SJohan Hedberg 	__u8		le_states[8];
159a9de9248SMarcel Holtmann 	__u8		commands[64];
1601143e5a6SMarcel Holtmann 	__u8		hci_ver;
1611143e5a6SMarcel Holtmann 	__u16		hci_rev;
162d5859e22SJohan Hedberg 	__u8		lmp_ver;
1631143e5a6SMarcel Holtmann 	__u16		manufacturer;
1647d69230cSAndrei Emeltchenko 	__u16		lmp_subver;
1651da177e4SLinus Torvalds 	__u16		voice_setting;
16617fa4b9dSJohan Hedberg 	__u8		io_capability;
16791c4e9b1SMarcel Holtmann 	__s8		inq_tx_power;
168f332ec66SJohan Hedberg 	__u16		page_scan_interval;
169f332ec66SJohan Hedberg 	__u16		page_scan_window;
170f332ec66SJohan Hedberg 	__u8		page_scan_type;
171f332ec66SJohan Hedberg 
1722b9be137SMarcel Holtmann 	__u16		devid_source;
1732b9be137SMarcel Holtmann 	__u16		devid_vendor;
1742b9be137SMarcel Holtmann 	__u16		devid_product;
1752b9be137SMarcel Holtmann 	__u16		devid_version;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	__u16		pkt_type;
1785b7f9909SMarcel Holtmann 	__u16		esco_type;
1791da177e4SLinus Torvalds 	__u16		link_policy;
1801da177e4SLinus Torvalds 	__u16		link_mode;
1811da177e4SLinus Torvalds 
18204837f64SMarcel Holtmann 	__u32		idle_timeout;
18304837f64SMarcel Holtmann 	__u16		sniff_min_interval;
18404837f64SMarcel Holtmann 	__u16		sniff_max_interval;
18504837f64SMarcel Holtmann 
186928abaa7SAndrei Emeltchenko 	__u8		amp_status;
187928abaa7SAndrei Emeltchenko 	__u32		amp_total_bw;
188928abaa7SAndrei Emeltchenko 	__u32		amp_max_bw;
189928abaa7SAndrei Emeltchenko 	__u32		amp_min_latency;
190928abaa7SAndrei Emeltchenko 	__u32		amp_max_pdu;
191928abaa7SAndrei Emeltchenko 	__u8		amp_type;
192928abaa7SAndrei Emeltchenko 	__u16		amp_pal_cap;
193928abaa7SAndrei Emeltchenko 	__u16		amp_assoc_size;
194928abaa7SAndrei Emeltchenko 	__u32		amp_max_flush_to;
195928abaa7SAndrei Emeltchenko 	__u32		amp_be_flush_to;
196928abaa7SAndrei Emeltchenko 
197903e4541SAndrei Emeltchenko 	struct amp_assoc	loc_assoc;
198903e4541SAndrei Emeltchenko 
1991e89cffbSAndrei Emeltchenko 	__u8		flow_ctl_mode;
2001e89cffbSAndrei Emeltchenko 
2019f61656aSJohan Hedberg 	unsigned int	auto_accept_delay;
2029f61656aSJohan Hedberg 
2031da177e4SLinus Torvalds 	unsigned long	quirks;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	atomic_t	cmd_cnt;
2061da177e4SLinus Torvalds 	unsigned int	acl_cnt;
2071da177e4SLinus Torvalds 	unsigned int	sco_cnt;
2086ed58ec5SVille Tervo 	unsigned int	le_cnt;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	unsigned int	acl_mtu;
2111da177e4SLinus Torvalds 	unsigned int	sco_mtu;
2126ed58ec5SVille Tervo 	unsigned int	le_mtu;
2131da177e4SLinus Torvalds 	unsigned int	acl_pkts;
2141da177e4SLinus Torvalds 	unsigned int	sco_pkts;
2156ed58ec5SVille Tervo 	unsigned int	le_pkts;
2161da177e4SLinus Torvalds 
217350ee4cfSAndrei Emeltchenko 	__u16		block_len;
218350ee4cfSAndrei Emeltchenko 	__u16		block_mtu;
219350ee4cfSAndrei Emeltchenko 	__u16		num_blocks;
220350ee4cfSAndrei Emeltchenko 	__u16		block_cnt;
221350ee4cfSAndrei Emeltchenko 
2221da177e4SLinus Torvalds 	unsigned long	acl_last_tx;
2231da177e4SLinus Torvalds 	unsigned long	sco_last_tx;
2246ed58ec5SVille Tervo 	unsigned long	le_last_tx;
2251da177e4SLinus Torvalds 
226f48fd9c8SMarcel Holtmann 	struct workqueue_struct	*workqueue;
2276ead1bbcSJohan Hedberg 	struct workqueue_struct	*req_workqueue;
228f48fd9c8SMarcel Holtmann 
229ab81cbf9SJohan Hedberg 	struct work_struct	power_on;
2303243553fSJohan Hedberg 	struct delayed_work	power_off;
231ab81cbf9SJohan Hedberg 
23216ab91abSJohan Hedberg 	__u16			discov_timeout;
23316ab91abSJohan Hedberg 	struct delayed_work	discov_off;
23416ab91abSJohan Hedberg 
2357d78525dSJohan Hedberg 	struct delayed_work	service_cache;
2367d78525dSJohan Hedberg 
2376bd32326SVille Tervo 	struct timer_list	cmd_timer;
238b78752ccSMarcel Holtmann 
239b78752ccSMarcel Holtmann 	struct work_struct	rx_work;
240c347b765SGustavo F. Padovan 	struct work_struct	cmd_work;
2413eff45eaSGustavo F. Padovan 	struct work_struct	tx_work;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	struct sk_buff_head	rx_q;
2441da177e4SLinus Torvalds 	struct sk_buff_head	raw_q;
2451da177e4SLinus Torvalds 	struct sk_buff_head	cmd_q;
2461da177e4SLinus Torvalds 
247b6ddb638SJohan Hedberg 	struct sk_buff		*recv_evt;
2481da177e4SLinus Torvalds 	struct sk_buff		*sent_cmd;
249cd4c5391SSuraj Sumangala 	struct sk_buff		*reassembly[NUM_REASSEMBLY];
2501da177e4SLinus Torvalds 
251a6a67efdSThomas Gleixner 	struct mutex		req_lock;
2521da177e4SLinus Torvalds 	wait_queue_head_t	req_wait_q;
2531da177e4SLinus Torvalds 	__u32			req_status;
2541da177e4SLinus Torvalds 	__u32			req_result;
255a5040efaSJohan Hedberg 
2562e58ef3eSJohan Hedberg 	struct list_head	mgmt_pending;
2572e58ef3eSJohan Hedberg 
25830883512SJohan Hedberg 	struct discovery_state	discovery;
2591da177e4SLinus Torvalds 	struct hci_conn_hash	conn_hash;
260ea4bd8baSDavid Miller 	struct list_head	blacklist;
2611da177e4SLinus Torvalds 
2622aeb9a1aSJohan Hedberg 	struct list_head	uuids;
2632aeb9a1aSJohan Hedberg 
26455ed8ca1SJohan Hedberg 	struct list_head	link_keys;
26555ed8ca1SJohan Hedberg 
266b899efafSVinicius Costa Gomes 	struct list_head	long_term_keys;
267b899efafSVinicius Costa Gomes 
2682763eda6SSzymon Janc 	struct list_head	remote_oob_data;
2692763eda6SSzymon Janc 
2701da177e4SLinus Torvalds 	struct hci_dev_stats	stat;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	atomic_t		promisc;
2731da177e4SLinus Torvalds 
274ca325f69SMarcel Holtmann 	struct dentry		*debugfs;
275ca325f69SMarcel Holtmann 
276a91f2e39SMarcel Holtmann 	struct device		dev;
2771da177e4SLinus Torvalds 
278611b30f7SMarcel Holtmann 	struct rfkill		*rfkill;
279611b30f7SMarcel Holtmann 
280d23264a8SAndre Guedes 	unsigned long		dev_flags;
281d23264a8SAndre Guedes 
2827ba8b4beSAndre Guedes 	struct delayed_work	le_scan_disable;
2837ba8b4beSAndre Guedes 
28428b75a89SAndre Guedes 	struct work_struct	le_scan;
28528b75a89SAndre Guedes 	struct le_scan_params	le_scan_params;
28628b75a89SAndre Guedes 
2878fa19098SJohan Hedberg 	__s8			adv_tx_power;
2883f0f524bSJohan Hedberg 	__u8			adv_data[HCI_MAX_AD_LENGTH];
2893f0f524bSJohan Hedberg 	__u8			adv_data_len;
2908fa19098SJohan Hedberg 
2911da177e4SLinus Torvalds 	int (*open)(struct hci_dev *hdev);
2921da177e4SLinus Torvalds 	int (*close)(struct hci_dev *hdev);
2931da177e4SLinus Torvalds 	int (*flush)(struct hci_dev *hdev);
294f41c70c4SMarcel Holtmann 	int (*setup)(struct hci_dev *hdev);
2951da177e4SLinus Torvalds 	int (*send)(struct sk_buff *skb);
2961da177e4SLinus Torvalds 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
2971da177e4SLinus Torvalds 	int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
2981da177e4SLinus Torvalds };
2991da177e4SLinus Torvalds 
30053502d69SAndrei Emeltchenko #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
30153502d69SAndrei Emeltchenko 
3021da177e4SLinus Torvalds struct hci_conn {
3031da177e4SLinus Torvalds 	struct list_head list;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	atomic_t	refcnt;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	bdaddr_t	dst;
30829b7988aSAndre Guedes 	__u8		dst_type;
3091da177e4SLinus Torvalds 	__u16		handle;
3101da177e4SLinus Torvalds 	__u16		state;
31104837f64SMarcel Holtmann 	__u8		mode;
3121da177e4SLinus Torvalds 	__u8		type;
313a0c808b3SJohan Hedberg 	bool		out;
3144c67bc74SMarcel Holtmann 	__u8		attempt;
3151da177e4SLinus Torvalds 	__u8		dev_class[3];
31604837f64SMarcel Holtmann 	__u8		features[8];
31704837f64SMarcel Holtmann 	__u16		interval;
318a8746417SMarcel Holtmann 	__u16		pkt_type;
31904837f64SMarcel Holtmann 	__u16		link_policy;
3201da177e4SLinus Torvalds 	__u32		link_mode;
32113d39315SWaldemar Rymarkiewicz 	__u8		key_type;
32240be492fSMarcel Holtmann 	__u8		auth_type;
3238c1b2355SMarcel Holtmann 	__u8		sec_level;
324765c2a96SJohan Hedberg 	__u8		pending_sec_level;
325980e1a53SJohan Hedberg 	__u8		pin_length;
326726b4ffcSVinicius Costa Gomes 	__u8		enc_key_size;
32717fa4b9dSJohan Hedberg 	__u8		io_capability;
32892a25256SJohan Hedberg 	__u32		passkey_notify;
32992a25256SJohan Hedberg 	__u8		passkey_entered;
330052b30b0SMarcel Holtmann 	__u16		disc_timeout;
33151a8efd7SJohan Hedberg 	unsigned long	flags;
3321da177e4SLinus Torvalds 
33303b555e1SJohan Hedberg 	__u8		remote_cap;
33403b555e1SJohan Hedberg 	__u8		remote_auth;
3353161ae1cSAndrei Emeltchenko 	__u8		remote_id;
3366ec5bcadSVishal Agarwal 	bool		flush_key;
33703b555e1SJohan Hedberg 
3381da177e4SLinus Torvalds 	unsigned int	sent;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	struct sk_buff_head data_q;
3412c33c06aSGustavo F. Padovan 	struct list_head chan_list;
3421da177e4SLinus Torvalds 
34319c40e3bSGustavo F. Padovan 	struct delayed_work disc_work;
34404837f64SMarcel Holtmann 	struct timer_list idle_timer;
3459f61656aSJohan Hedberg 	struct timer_list auto_accept_timer;
3461da177e4SLinus Torvalds 
347b219e3acSMarcel Holtmann 	struct device	dev;
348b219e3acSMarcel Holtmann 
3491da177e4SLinus Torvalds 	struct hci_dev	*hdev;
3501da177e4SLinus Torvalds 	void		*l2cap_data;
3511da177e4SLinus Torvalds 	void		*sco_data;
3522b64d153SBrian Gix 	void		*smp_conn;
3539740e49dSAndrei Emeltchenko 	struct amp_mgr	*amp_mgr;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	struct hci_conn	*link;
356e9a416b5SJohan Hedberg 
357e9a416b5SJohan Hedberg 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
358e9a416b5SJohan Hedberg 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
359e9a416b5SJohan Hedberg 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
3601da177e4SLinus Torvalds };
3611da177e4SLinus Torvalds 
36273d80debSLuiz Augusto von Dentz struct hci_chan {
36373d80debSLuiz Augusto von Dentz 	struct list_head list;
36442c4e53eSAndrei Emeltchenko 	__u16 handle;
36573d80debSLuiz Augusto von Dentz 	struct hci_conn *conn;
36673d80debSLuiz Augusto von Dentz 	struct sk_buff_head data_q;
36773d80debSLuiz Augusto von Dentz 	unsigned int	sent;
368168df8e5SMat Martineau 	__u8		state;
36973d80debSLuiz Augusto von Dentz };
37073d80debSLuiz Augusto von Dentz 
3711da177e4SLinus Torvalds extern struct list_head hci_dev_list;
3721da177e4SLinus Torvalds extern struct list_head hci_cb_list;
3731da177e4SLinus Torvalds extern rwlock_t hci_dev_list_lock;
3741da177e4SLinus Torvalds extern rwlock_t hci_cb_list_lock;
3751da177e4SLinus Torvalds 
376686ebf28SUlisses Furquim /* ----- HCI interface to upper protocols ----- */
377686ebf28SUlisses Furquim extern int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
3789e664631SAndrei Emeltchenko extern void l2cap_connect_cfm(struct hci_conn *hcon, u8 status);
379686ebf28SUlisses Furquim extern int l2cap_disconn_ind(struct hci_conn *hcon);
3809e664631SAndrei Emeltchenko extern void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason);
381686ebf28SUlisses Furquim extern int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
382c3c7ea65SGustavo Padovan extern int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb,
383c3c7ea65SGustavo Padovan 			      u16 flags);
384686ebf28SUlisses Furquim 
38520714bfeSFrédéric Dalleau extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
3869e664631SAndrei Emeltchenko extern void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
3879e664631SAndrei Emeltchenko extern void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
388686ebf28SUlisses Furquim extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
389686ebf28SUlisses Furquim 
3901da177e4SLinus Torvalds /* ----- Inquiry cache ----- */
39170f23020SAndrei Emeltchenko #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
39270f23020SAndrei Emeltchenko #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
3931da177e4SLinus Torvalds 
39430883512SJohan Hedberg static inline void discovery_init(struct hci_dev *hdev)
3951da177e4SLinus Torvalds {
396ff9ef578SJohan Hedberg 	hdev->discovery.state = DISCOVERY_STOPPED;
39730883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.all);
39830883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.unknown);
39930883512SJohan Hedberg 	INIT_LIST_HEAD(&hdev->discovery.resolve);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
40230dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev);
40330dc78e1SJohan Hedberg 
404ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state);
405ff9ef578SJohan Hedberg 
4061da177e4SLinus Torvalds static inline int inquiry_cache_empty(struct hci_dev *hdev)
4071da177e4SLinus Torvalds {
40830883512SJohan Hedberg 	return list_empty(&hdev->discovery.all);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds static inline long inquiry_cache_age(struct hci_dev *hdev)
4121da177e4SLinus Torvalds {
41330883512SJohan Hedberg 	struct discovery_state *c = &hdev->discovery;
4141da177e4SLinus Torvalds 	return jiffies - c->timestamp;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static inline long inquiry_entry_age(struct inquiry_entry *e)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	return jiffies - e->timestamp;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4225a9d0a3fSWaldemar Rymarkiewicz struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
4235a9d0a3fSWaldemar Rymarkiewicz 					       bdaddr_t *bdaddr);
424561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
425561aafbcSJohan Hedberg 						       bdaddr_t *bdaddr);
42630dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
42730dc78e1SJohan Hedberg 						       bdaddr_t *bdaddr,
42830dc78e1SJohan Hedberg 						       int state);
429a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
430a3d4e20aSJohan Hedberg 				      struct inquiry_entry *ie);
4313175405bSJohan Hedberg bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
432388fc8faSJohan Hedberg 			      bool name_known, bool *ssp);
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /* ----- HCI Connections ----- */
4351da177e4SLinus Torvalds enum {
4361da177e4SLinus Torvalds 	HCI_CONN_AUTH_PEND,
43719f8def0SWaldemar Rymarkiewicz 	HCI_CONN_REAUTH_PEND,
4381da177e4SLinus Torvalds 	HCI_CONN_ENCRYPT_PEND,
43904837f64SMarcel Holtmann 	HCI_CONN_RSWITCH_PEND,
44004837f64SMarcel Holtmann 	HCI_CONN_MODE_CHANGE_PEND,
441e73439d8SMarcel Holtmann 	HCI_CONN_SCO_SETUP_PEND,
442d26a2345SVinicius Costa Gomes 	HCI_CONN_LE_SMP_PEND,
443b644ba33SJohan Hedberg 	HCI_CONN_MGMT_CONNECTED,
44458a681efSJohan Hedberg 	HCI_CONN_SSP_ENABLED,
44558a681efSJohan Hedberg 	HCI_CONN_POWER_SAVE,
44658a681efSJohan Hedberg 	HCI_CONN_REMOTE_OOB,
4471da177e4SLinus Torvalds };
4481da177e4SLinus Torvalds 
449aa64a8b5SJohan Hedberg static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
450aa64a8b5SJohan Hedberg {
451aa64a8b5SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
452c3c7ea65SGustavo Padovan 	return test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
453c3c7ea65SGustavo Padovan 	       test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
454aa64a8b5SJohan Hedberg }
455aa64a8b5SJohan Hedberg 
4561da177e4SLinus Torvalds static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
459bf4c6325SGustavo F. Padovan 	list_add_rcu(&c->list, &h->list);
460fcd89c09SVille Tervo 	switch (c->type) {
461fcd89c09SVille Tervo 	case ACL_LINK:
4621da177e4SLinus Torvalds 		h->acl_num++;
463fcd89c09SVille Tervo 		break;
464bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
465bd1eb66bSAndrei Emeltchenko 		h->amp_num++;
466bd1eb66bSAndrei Emeltchenko 		break;
467fcd89c09SVille Tervo 	case LE_LINK:
468fcd89c09SVille Tervo 		h->le_num++;
469fcd89c09SVille Tervo 		break;
470fcd89c09SVille Tervo 	case SCO_LINK:
471fcd89c09SVille Tervo 	case ESCO_LINK:
4721da177e4SLinus Torvalds 		h->sco_num++;
473fcd89c09SVille Tervo 		break;
474fcd89c09SVille Tervo 	}
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
480bf4c6325SGustavo F. Padovan 
481bf4c6325SGustavo F. Padovan 	list_del_rcu(&c->list);
482bf4c6325SGustavo F. Padovan 	synchronize_rcu();
483bf4c6325SGustavo F. Padovan 
484fcd89c09SVille Tervo 	switch (c->type) {
485fcd89c09SVille Tervo 	case ACL_LINK:
4861da177e4SLinus Torvalds 		h->acl_num--;
487fcd89c09SVille Tervo 		break;
488bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
489bd1eb66bSAndrei Emeltchenko 		h->amp_num--;
490bd1eb66bSAndrei Emeltchenko 		break;
491fcd89c09SVille Tervo 	case LE_LINK:
492fcd89c09SVille Tervo 		h->le_num--;
493fcd89c09SVille Tervo 		break;
494fcd89c09SVille Tervo 	case SCO_LINK:
495fcd89c09SVille Tervo 	case ESCO_LINK:
4961da177e4SLinus Torvalds 		h->sco_num--;
497fcd89c09SVille Tervo 		break;
498fcd89c09SVille Tervo 	}
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
50152087a79SLuiz Augusto von Dentz static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
50252087a79SLuiz Augusto von Dentz {
50352087a79SLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
50452087a79SLuiz Augusto von Dentz 	switch (type) {
50552087a79SLuiz Augusto von Dentz 	case ACL_LINK:
50652087a79SLuiz Augusto von Dentz 		return h->acl_num;
507bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
508bd1eb66bSAndrei Emeltchenko 		return h->amp_num;
50952087a79SLuiz Augusto von Dentz 	case LE_LINK:
51052087a79SLuiz Augusto von Dentz 		return h->le_num;
51152087a79SLuiz Augusto von Dentz 	case SCO_LINK:
51252087a79SLuiz Augusto von Dentz 	case ESCO_LINK:
51352087a79SLuiz Augusto von Dentz 		return h->sco_num;
51452087a79SLuiz Augusto von Dentz 	default:
51552087a79SLuiz Augusto von Dentz 		return 0;
51652087a79SLuiz Augusto von Dentz 	}
51752087a79SLuiz Augusto von Dentz }
51852087a79SLuiz Augusto von Dentz 
5191da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
5201da177e4SLinus Torvalds 								__u16 handle)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
5231da177e4SLinus Torvalds 	struct hci_conn  *c;
5241da177e4SLinus Torvalds 
525bf4c6325SGustavo F. Padovan 	rcu_read_lock();
526bf4c6325SGustavo F. Padovan 
527bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
528bf4c6325SGustavo F. Padovan 		if (c->handle == handle) {
529bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5301da177e4SLinus Torvalds 			return c;
5311da177e4SLinus Torvalds 		}
532bf4c6325SGustavo F. Padovan 	}
533bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
534bf4c6325SGustavo F. Padovan 
5351da177e4SLinus Torvalds 	return NULL;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
5391da177e4SLinus Torvalds 							__u8 type, bdaddr_t *ba)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
5421da177e4SLinus Torvalds 	struct hci_conn  *c;
5431da177e4SLinus Torvalds 
544bf4c6325SGustavo F. Padovan 	rcu_read_lock();
545bf4c6325SGustavo F. Padovan 
546bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
547bf4c6325SGustavo F. Padovan 		if (c->type == type && !bacmp(&c->dst, ba)) {
548bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5491da177e4SLinus Torvalds 			return c;
5501da177e4SLinus Torvalds 		}
551bf4c6325SGustavo F. Padovan 	}
552bf4c6325SGustavo F. Padovan 
553bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
554bf4c6325SGustavo F. Padovan 
5551da177e4SLinus Torvalds 	return NULL;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5584c67bc74SMarcel Holtmann static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
5594c67bc74SMarcel Holtmann 							__u8 type, __u16 state)
5604c67bc74SMarcel Holtmann {
5614c67bc74SMarcel Holtmann 	struct hci_conn_hash *h = &hdev->conn_hash;
5624c67bc74SMarcel Holtmann 	struct hci_conn  *c;
5634c67bc74SMarcel Holtmann 
564bf4c6325SGustavo F. Padovan 	rcu_read_lock();
565bf4c6325SGustavo F. Padovan 
566bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
567bf4c6325SGustavo F. Padovan 		if (c->type == type && c->state == state) {
568bf4c6325SGustavo F. Padovan 			rcu_read_unlock();
5694c67bc74SMarcel Holtmann 			return c;
5704c67bc74SMarcel Holtmann 		}
571bf4c6325SGustavo F. Padovan 	}
572bf4c6325SGustavo F. Padovan 
573bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
574bf4c6325SGustavo F. Padovan 
5754c67bc74SMarcel Holtmann 	return NULL;
5764c67bc74SMarcel Holtmann }
5774c67bc74SMarcel Holtmann 
578bed71748SAndre Guedes void hci_disconnect(struct hci_conn *conn, __u8 reason);
579b6a0dc82SMarcel Holtmann void hci_setup_sync(struct hci_conn *conn, __u16 handle);
580e73439d8SMarcel Holtmann void hci_sco_setup(struct hci_conn *conn, __u8 status);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
5831da177e4SLinus Torvalds int hci_conn_del(struct hci_conn *conn);
5841da177e4SLinus Torvalds void hci_conn_hash_flush(struct hci_dev *hdev);
585a9de9248SMarcel Holtmann void hci_conn_check_pending(struct hci_dev *hdev);
58620714bfeSFrédéric Dalleau void hci_conn_accept(struct hci_conn *conn, int mask);
5871da177e4SLinus Torvalds 
58873d80debSLuiz Augusto von Dentz struct hci_chan *hci_chan_create(struct hci_conn *conn);
5899472007cSAndrei Emeltchenko void hci_chan_del(struct hci_chan *chan);
5902c33c06aSGustavo F. Padovan void hci_chan_list_flush(struct hci_conn *conn);
59142c4e53eSAndrei Emeltchenko struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
59273d80debSLuiz Augusto von Dentz 
5935a9d0a3fSWaldemar Rymarkiewicz struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,
594b12f62cfSAndre Guedes 			     __u8 dst_type, __u8 sec_level, __u8 auth_type);
595e7c29cb1SMarcel Holtmann int hci_conn_check_link_mode(struct hci_conn *conn);
596b3b1b061SWaldemar Rymarkiewicz int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
5970684e5f9SMarcel Holtmann int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
5981da177e4SLinus Torvalds int hci_conn_change_link_key(struct hci_conn *conn);
5998c1b2355SMarcel Holtmann int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
6001da177e4SLinus Torvalds 
60114b12d0bSJaikumar Ganesh void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
6021da177e4SLinus Torvalds 
6038d12356fSDavid Herrmann /*
6048d12356fSDavid Herrmann  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
6058d12356fSDavid Herrmann  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
6068d12356fSDavid Herrmann  * working or anything else. They just guarantee that the object is available
6078d12356fSDavid Herrmann  * and can be dereferenced. So you can use its locks, local variables and any
6088d12356fSDavid Herrmann  * other constant data.
6098d12356fSDavid Herrmann  * Before accessing runtime data, you _must_ lock the object and then check that
6108d12356fSDavid Herrmann  * it is still running. As soon as you release the locks, the connection might
6118d12356fSDavid Herrmann  * get dropped, though.
6128d12356fSDavid Herrmann  *
6138d12356fSDavid Herrmann  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
6148d12356fSDavid Herrmann  * how long the underlying connection is held. So every channel that runs on the
6158d12356fSDavid Herrmann  * hci_conn object calls this to prevent the connection from disappearing. As
6168d12356fSDavid Herrmann  * long as you hold a device, you must also guarantee that you have a valid
6178d12356fSDavid Herrmann  * reference to the device via hci_conn_get() (or the initial reference from
6188d12356fSDavid Herrmann  * hci_conn_add()).
6198d12356fSDavid Herrmann  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
6208d12356fSDavid Herrmann  * break because nobody cares for that. But this means, we cannot use
6218d12356fSDavid Herrmann  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
6228d12356fSDavid Herrmann  */
6238d12356fSDavid Herrmann 
6248d12356fSDavid Herrmann static inline void hci_conn_get(struct hci_conn *conn)
6258d12356fSDavid Herrmann {
6268d12356fSDavid Herrmann 	get_device(&conn->dev);
6278d12356fSDavid Herrmann }
6288d12356fSDavid Herrmann 
6298d12356fSDavid Herrmann static inline void hci_conn_put(struct hci_conn *conn)
6308d12356fSDavid Herrmann {
6318d12356fSDavid Herrmann 	put_device(&conn->dev);
6328d12356fSDavid Herrmann }
6338d12356fSDavid Herrmann 
6341da177e4SLinus Torvalds static inline void hci_conn_hold(struct hci_conn *conn)
6351da177e4SLinus Torvalds {
63671becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
63738b3fef1SAndrei Emeltchenko 
6381da177e4SLinus Torvalds 	atomic_inc(&conn->refcnt);
6392f304d1eSAndre Guedes 	cancel_delayed_work(&conn->disc_work);
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
64276a68ba0SDavid Herrmann static inline void hci_conn_drop(struct hci_conn *conn)
6431da177e4SLinus Torvalds {
64471becf0cSAndrei Emeltchenko 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
64538b3fef1SAndrei Emeltchenko 
6461da177e4SLinus Torvalds 	if (atomic_dec_and_test(&conn->refcnt)) {
64704837f64SMarcel Holtmann 		unsigned long timeo;
648716e4ab5SAndrei Emeltchenko 
649716e4ab5SAndrei Emeltchenko 		switch (conn->type) {
650716e4ab5SAndrei Emeltchenko 		case ACL_LINK:
651716e4ab5SAndrei Emeltchenko 		case LE_LINK:
6526ac59344SMarcel Holtmann 			del_timer(&conn->idle_timer);
6536ac59344SMarcel Holtmann 			if (conn->state == BT_CONNECTED) {
6545f246e89SAndrei Emeltchenko 				timeo = conn->disc_timeout;
65504837f64SMarcel Holtmann 				if (!conn->out)
656052b30b0SMarcel Holtmann 					timeo *= 2;
6575a9d0a3fSWaldemar Rymarkiewicz 			} else {
6586ac59344SMarcel Holtmann 				timeo = msecs_to_jiffies(10);
6595a9d0a3fSWaldemar Rymarkiewicz 			}
660716e4ab5SAndrei Emeltchenko 			break;
661716e4ab5SAndrei Emeltchenko 
662716e4ab5SAndrei Emeltchenko 		case AMP_LINK:
663716e4ab5SAndrei Emeltchenko 			timeo = conn->disc_timeout;
664716e4ab5SAndrei Emeltchenko 			break;
665716e4ab5SAndrei Emeltchenko 
666716e4ab5SAndrei Emeltchenko 		default:
66704837f64SMarcel Holtmann 			timeo = msecs_to_jiffies(10);
668716e4ab5SAndrei Emeltchenko 			break;
6695a9d0a3fSWaldemar Rymarkiewicz 		}
670716e4ab5SAndrei Emeltchenko 
6712f304d1eSAndre Guedes 		cancel_delayed_work(&conn->disc_work);
67219c40e3bSGustavo F. Padovan 		queue_delayed_work(conn->hdev->workqueue,
6731931782bSVinicius Costa Gomes 				   &conn->disc_work, timeo);
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds /* ----- HCI Devices ----- */
678dc946bd8SDavid Herrmann static inline void hci_dev_put(struct hci_dev *d)
6791da177e4SLinus Torvalds {
680376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
681376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
682376261aeSAndrei Emeltchenko 
6834c724c71SDavid Herrmann 	put_device(&d->dev);
684010666a1SDavid Herrmann }
6851da177e4SLinus Torvalds 
686dc946bd8SDavid Herrmann static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
6871da177e4SLinus Torvalds {
688376261aeSAndrei Emeltchenko 	BT_DBG("%s orig refcnt %d", d->name,
689376261aeSAndrei Emeltchenko 	       atomic_read(&d->dev.kobj.kref.refcount));
690376261aeSAndrei Emeltchenko 
6914c724c71SDavid Herrmann 	get_device(&d->dev);
6921da177e4SLinus Torvalds 	return d;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
69509fd0de5SGustavo F. Padovan #define hci_dev_lock(d)		mutex_lock(&d->lock)
69609fd0de5SGustavo F. Padovan #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
6971da177e4SLinus Torvalds 
698aa2b86d7SDavid Herrmann #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
6993dc07322SDavid Herrmann #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
700aa2b86d7SDavid Herrmann 
701155961e8SDavid Herrmann static inline void *hci_get_drvdata(struct hci_dev *hdev)
702155961e8SDavid Herrmann {
703155961e8SDavid Herrmann 	return dev_get_drvdata(&hdev->dev);
704155961e8SDavid Herrmann }
705155961e8SDavid Herrmann 
706155961e8SDavid Herrmann static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
707155961e8SDavid Herrmann {
708155961e8SDavid Herrmann 	dev_set_drvdata(&hdev->dev, data);
709155961e8SDavid Herrmann }
710155961e8SDavid Herrmann 
7118598d064SAndrei Emeltchenko /* hci_dev_list shall be locked */
7128598d064SAndrei Emeltchenko static inline uint8_t __hci_num_ctrl(void)
7138598d064SAndrei Emeltchenko {
7148598d064SAndrei Emeltchenko 	uint8_t count = 0;
7158598d064SAndrei Emeltchenko 	struct list_head *p;
7168598d064SAndrei Emeltchenko 
7178598d064SAndrei Emeltchenko 	list_for_each(p, &hci_dev_list) {
7188598d064SAndrei Emeltchenko 		count++;
7198598d064SAndrei Emeltchenko 	}
7208598d064SAndrei Emeltchenko 
7218598d064SAndrei Emeltchenko 	return count;
7228598d064SAndrei Emeltchenko }
7238598d064SAndrei Emeltchenko 
7241da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index);
7250c0afedfSJohan Hedberg struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds struct hci_dev *hci_alloc_dev(void);
7281da177e4SLinus Torvalds void hci_free_dev(struct hci_dev *hdev);
7291da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev);
73059735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev);
7311da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev);
7321da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev);
7331da177e4SLinus Torvalds int hci_dev_open(__u16 dev);
7341da177e4SLinus Torvalds int hci_dev_close(__u16 dev);
7351da177e4SLinus Torvalds int hci_dev_reset(__u16 dev);
7361da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev);
7371da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg);
7381da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg);
7391da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg);
7401da177e4SLinus Torvalds int hci_get_conn_list(void __user *arg);
7411da177e4SLinus Torvalds int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
74240be492fSMarcel Holtmann int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
7431da177e4SLinus Torvalds int hci_inquiry(void __user *arg);
7441da177e4SLinus Torvalds 
745c3c7ea65SGustavo Padovan struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
746c3c7ea65SGustavo Padovan 					 bdaddr_t *bdaddr);
747f0358568SJohan Hedberg int hci_blacklist_clear(struct hci_dev *hdev);
74888c1fe4bSJohan Hedberg int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
74988c1fe4bSJohan Hedberg int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
750f0358568SJohan Hedberg 
7512aeb9a1aSJohan Hedberg int hci_uuids_clear(struct hci_dev *hdev);
7522aeb9a1aSJohan Hedberg 
75355ed8ca1SJohan Hedberg int hci_link_keys_clear(struct hci_dev *hdev);
75455ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
755d25e28abSJohan Hedberg int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
756d25e28abSJohan Hedberg 		     bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len);
757c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]);
758c9839a11SVinicius Costa Gomes int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
7599a006657SAndrei Emeltchenko 		int new_key, u8 authenticated, u8 tk[16], u8 enc_size,
7609a006657SAndrei Emeltchenko 		__le16 ediv, u8 rand[8]);
761c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
762c9839a11SVinicius Costa Gomes 				     u8 addr_type);
763b899efafSVinicius Costa Gomes int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr);
764b899efafSVinicius Costa Gomes int hci_smp_ltks_clear(struct hci_dev *hdev);
76555ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
76655ed8ca1SJohan Hedberg 
7672763eda6SSzymon Janc int hci_remote_oob_data_clear(struct hci_dev *hdev);
7682763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
7692763eda6SSzymon Janc 							bdaddr_t *bdaddr);
7702763eda6SSzymon Janc int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
7712763eda6SSzymon Janc 								u8 *randomizer);
7722763eda6SSzymon Janc int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
7732763eda6SSzymon Janc 
7741da177e4SLinus Torvalds void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
7751da177e4SLinus Torvalds 
77676bca880SMarcel Holtmann int hci_recv_frame(struct sk_buff *skb);
777ef222013SMarcel Holtmann int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
77899811510SSuraj Sumangala int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
779ef222013SMarcel Holtmann 
7800ac7e700SDavid Herrmann void hci_init_sysfs(struct hci_dev *hdev);
781ce242970SDavid Herrmann int hci_add_sysfs(struct hci_dev *hdev);
782ce242970SDavid Herrmann void hci_del_sysfs(struct hci_dev *hdev);
783a67e899cSMarcel Holtmann void hci_conn_init_sysfs(struct hci_conn *conn);
784b219e3acSMarcel Holtmann void hci_conn_add_sysfs(struct hci_conn *conn);
785b219e3acSMarcel Holtmann void hci_conn_del_sysfs(struct hci_conn *conn);
7861da177e4SLinus Torvalds 
7876935e0f5SDavid Herrmann #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds /* ----- LMP capabilities ----- */
79004837f64SMarcel Holtmann #define lmp_encrypt_capable(dev)   ((dev)->features[0] & LMP_ENCRYPT)
791761f09e4SJohan Hedberg #define lmp_rswitch_capable(dev)   ((dev)->features[0] & LMP_RSWITCH)
792761f09e4SJohan Hedberg #define lmp_hold_capable(dev)      ((dev)->features[0] & LMP_HOLD)
79304837f64SMarcel Holtmann #define lmp_sniff_capable(dev)     ((dev)->features[0] & LMP_SNIFF)
794761f09e4SJohan Hedberg #define lmp_park_capable(dev)      ((dev)->features[1] & LMP_PARK)
795761f09e4SJohan Hedberg #define lmp_inq_rssi_capable(dev)  ((dev)->features[3] & LMP_RSSI_INQ)
7965b7f9909SMarcel Holtmann #define lmp_esco_capable(dev)      ((dev)->features[3] & LMP_ESCO)
797761f09e4SJohan Hedberg #define lmp_bredr_capable(dev)     (!((dev)->features[4] & LMP_NO_BREDR))
798761f09e4SJohan Hedberg #define lmp_le_capable(dev)        ((dev)->features[4] & LMP_LE)
799761f09e4SJohan Hedberg #define lmp_sniffsubr_capable(dev) ((dev)->features[5] & LMP_SNIFF_SUBR)
800761f09e4SJohan Hedberg #define lmp_pause_enc_capable(dev) ((dev)->features[5] & LMP_PAUSE_ENC)
801761f09e4SJohan Hedberg #define lmp_ext_inq_capable(dev)   ((dev)->features[6] & LMP_EXT_INQ)
802ffa88e02SGustavo Padovan #define lmp_le_br_capable(dev)     !!((dev)->features[6] & LMP_SIMUL_LE_BR)
803769be974SMarcel Holtmann #define lmp_ssp_capable(dev)       ((dev)->features[6] & LMP_SIMPLE_PAIR)
804e702112fSAndrei Emeltchenko #define lmp_no_flush_capable(dev)  ((dev)->features[6] & LMP_NO_FLUSH)
805761f09e4SJohan Hedberg #define lmp_lsto_capable(dev)      ((dev)->features[7] & LMP_LSTO)
806761f09e4SJohan Hedberg #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[7] & LMP_INQ_TX_PWR)
807761f09e4SJohan Hedberg #define lmp_ext_feat_capable(dev)  ((dev)->features[7] & LMP_EXTFEATURES)
8081da177e4SLinus Torvalds 
809eead27daSAndre Guedes /* ----- Extended LMP capabilities ----- */
8106b4b73eeSJohan Hedberg #define lmp_host_ssp_capable(dev)  ((dev)->host_features[0] & LMP_HOST_SSP)
811ffa88e02SGustavo Padovan #define lmp_host_le_capable(dev)   !!((dev)->host_features[0] & LMP_HOST_LE)
812ffa88e02SGustavo Padovan #define lmp_host_le_br_capable(dev) !!((dev)->host_features[0] & LMP_HOST_LE_BREDR)
813eead27daSAndre Guedes 
8145d05416eSAndrei Emeltchenko /* returns true if at least one AMP active */
8155d05416eSAndrei Emeltchenko static inline bool hci_amp_capable(void)
8165d05416eSAndrei Emeltchenko {
8175d05416eSAndrei Emeltchenko 	struct hci_dev *hdev;
8185d05416eSAndrei Emeltchenko 	bool ret = false;
8195d05416eSAndrei Emeltchenko 
8205d05416eSAndrei Emeltchenko 	read_lock(&hci_dev_list_lock);
8215d05416eSAndrei Emeltchenko 	list_for_each_entry(hdev, &hci_dev_list, list)
8225d05416eSAndrei Emeltchenko 		if (hdev->amp_type == HCI_AMP &&
8235d05416eSAndrei Emeltchenko 		    test_bit(HCI_UP, &hdev->flags))
8245d05416eSAndrei Emeltchenko 			ret = true;
8255d05416eSAndrei Emeltchenko 	read_unlock(&hci_dev_list_lock);
8265d05416eSAndrei Emeltchenko 
8275d05416eSAndrei Emeltchenko 	return ret;
8285d05416eSAndrei Emeltchenko }
8295d05416eSAndrei Emeltchenko 
8301da177e4SLinus Torvalds /* ----- HCI protocols ----- */
83120714bfeSFrédéric Dalleau #define HCI_PROTO_DEFER             0x01
83220714bfeSFrédéric Dalleau 
8335a9d0a3fSWaldemar Rymarkiewicz static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
83420714bfeSFrédéric Dalleau 					__u8 type, __u8 *flags)
8351da177e4SLinus Torvalds {
836686ebf28SUlisses Furquim 	switch (type) {
837686ebf28SUlisses Furquim 	case ACL_LINK:
838686ebf28SUlisses Furquim 		return l2cap_connect_ind(hdev, bdaddr);
8391da177e4SLinus Torvalds 
840686ebf28SUlisses Furquim 	case SCO_LINK:
841686ebf28SUlisses Furquim 	case ESCO_LINK:
84220714bfeSFrédéric Dalleau 		return sco_connect_ind(hdev, bdaddr, flags);
8431da177e4SLinus Torvalds 
844686ebf28SUlisses Furquim 	default:
845686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", type);
846686ebf28SUlisses Furquim 		return -EINVAL;
847686ebf28SUlisses Furquim 	}
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
8511da177e4SLinus Torvalds {
852686ebf28SUlisses Furquim 	switch (conn->type) {
853686ebf28SUlisses Furquim 	case ACL_LINK:
854686ebf28SUlisses Furquim 	case LE_LINK:
855686ebf28SUlisses Furquim 		l2cap_connect_cfm(conn, status);
856686ebf28SUlisses Furquim 		break;
8571da177e4SLinus Torvalds 
858686ebf28SUlisses Furquim 	case SCO_LINK:
859686ebf28SUlisses Furquim 	case ESCO_LINK:
860686ebf28SUlisses Furquim 		sco_connect_cfm(conn, status);
861686ebf28SUlisses Furquim 		break;
8621da177e4SLinus Torvalds 
863686ebf28SUlisses Furquim 	default:
864686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", conn->type);
865686ebf28SUlisses Furquim 		break;
866686ebf28SUlisses Furquim 	}
867e9a416b5SJohan Hedberg 
868e9a416b5SJohan Hedberg 	if (conn->connect_cfm_cb)
869e9a416b5SJohan Hedberg 		conn->connect_cfm_cb(conn, status);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8722950f21aSMarcel Holtmann static inline int hci_proto_disconn_ind(struct hci_conn *conn)
8732950f21aSMarcel Holtmann {
874686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
875686ebf28SUlisses Furquim 		return HCI_ERROR_REMOTE_USER_TERM;
8762950f21aSMarcel Holtmann 
877686ebf28SUlisses Furquim 	return l2cap_disconn_ind(conn);
8782950f21aSMarcel Holtmann }
8792950f21aSMarcel Holtmann 
8802950f21aSMarcel Holtmann static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
8811da177e4SLinus Torvalds {
882686ebf28SUlisses Furquim 	switch (conn->type) {
883686ebf28SUlisses Furquim 	case ACL_LINK:
884686ebf28SUlisses Furquim 	case LE_LINK:
885686ebf28SUlisses Furquim 		l2cap_disconn_cfm(conn, reason);
886686ebf28SUlisses Furquim 		break;
8871da177e4SLinus Torvalds 
888686ebf28SUlisses Furquim 	case SCO_LINK:
889686ebf28SUlisses Furquim 	case ESCO_LINK:
890686ebf28SUlisses Furquim 		sco_disconn_cfm(conn, reason);
891686ebf28SUlisses Furquim 		break;
8921da177e4SLinus Torvalds 
893bd1eb66bSAndrei Emeltchenko 	/* L2CAP would be handled for BREDR chan */
894bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
895bd1eb66bSAndrei Emeltchenko 		break;
896bd1eb66bSAndrei Emeltchenko 
897686ebf28SUlisses Furquim 	default:
898686ebf28SUlisses Furquim 		BT_ERR("unknown link type %d", conn->type);
899686ebf28SUlisses Furquim 		break;
900686ebf28SUlisses Furquim 	}
901e9a416b5SJohan Hedberg 
902e9a416b5SJohan Hedberg 	if (conn->disconn_cfm_cb)
903e9a416b5SJohan Hedberg 		conn->disconn_cfm_cb(conn, reason);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
9071da177e4SLinus Torvalds {
9088c1b2355SMarcel Holtmann 	__u8 encrypt;
9098c1b2355SMarcel Holtmann 
910686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
911686ebf28SUlisses Furquim 		return;
912686ebf28SUlisses Furquim 
91351a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
9148c1b2355SMarcel Holtmann 		return;
9158c1b2355SMarcel Holtmann 
9168c1b2355SMarcel Holtmann 	encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
917686ebf28SUlisses Furquim 	l2cap_security_cfm(conn, status, encrypt);
918e9a416b5SJohan Hedberg 
919e9a416b5SJohan Hedberg 	if (conn->security_cfm_cb)
920e9a416b5SJohan Hedberg 		conn->security_cfm_cb(conn, status);
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9235a9d0a3fSWaldemar Rymarkiewicz static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status,
9245a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
9251da177e4SLinus Torvalds {
926686ebf28SUlisses Furquim 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
927686ebf28SUlisses Furquim 		return;
9281da177e4SLinus Torvalds 
929686ebf28SUlisses Furquim 	l2cap_security_cfm(conn, status, encrypt);
930e9a416b5SJohan Hedberg 
931e9a416b5SJohan Hedberg 	if (conn->security_cfm_cb)
932e9a416b5SJohan Hedberg 		conn->security_cfm_cb(conn, status);
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds /* ----- HCI callbacks ----- */
9361da177e4SLinus Torvalds struct hci_cb {
9371da177e4SLinus Torvalds 	struct list_head list;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	char *name;
9401da177e4SLinus Torvalds 
9415a9d0a3fSWaldemar Rymarkiewicz 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
9425a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt);
9431da177e4SLinus Torvalds 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
9441da177e4SLinus Torvalds 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
9451da177e4SLinus Torvalds };
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
9481da177e4SLinus Torvalds {
949711584eaSDenis Kirjanov 	struct hci_cb *cb;
9508c1b2355SMarcel Holtmann 	__u8 encrypt;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	hci_proto_auth_cfm(conn, status);
9531da177e4SLinus Torvalds 
95451a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
9558c1b2355SMarcel Holtmann 		return;
9568c1b2355SMarcel Holtmann 
9578c1b2355SMarcel Holtmann 	encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
9588c1b2355SMarcel Holtmann 
959f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
960711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9618c1b2355SMarcel Holtmann 		if (cb->security_cfm)
9628c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
9631da177e4SLinus Torvalds 	}
964f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
9675a9d0a3fSWaldemar Rymarkiewicz static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
9685a9d0a3fSWaldemar Rymarkiewicz 								__u8 encrypt)
9691da177e4SLinus Torvalds {
970711584eaSDenis Kirjanov 	struct hci_cb *cb;
9711da177e4SLinus Torvalds 
972435fef20SMarcel Holtmann 	if (conn->sec_level == BT_SECURITY_SDP)
973435fef20SMarcel Holtmann 		conn->sec_level = BT_SECURITY_LOW;
974435fef20SMarcel Holtmann 
97588167aedSVinicius Costa Gomes 	if (conn->pending_sec_level > conn->sec_level)
97688167aedSVinicius Costa Gomes 		conn->sec_level = conn->pending_sec_level;
97788167aedSVinicius Costa Gomes 
9789719f8afSMarcel Holtmann 	hci_proto_encrypt_cfm(conn, status, encrypt);
9791da177e4SLinus Torvalds 
980f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
981711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9828c1b2355SMarcel Holtmann 		if (cb->security_cfm)
9838c1b2355SMarcel Holtmann 			cb->security_cfm(conn, status, encrypt);
9841da177e4SLinus Torvalds 	}
985f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
9891da177e4SLinus Torvalds {
990711584eaSDenis Kirjanov 	struct hci_cb *cb;
9911da177e4SLinus Torvalds 
992f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
993711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
9941da177e4SLinus Torvalds 		if (cb->key_change_cfm)
9951da177e4SLinus Torvalds 			cb->key_change_cfm(conn, status);
9961da177e4SLinus Torvalds 	}
997f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10005a9d0a3fSWaldemar Rymarkiewicz static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
10015a9d0a3fSWaldemar Rymarkiewicz 								__u8 role)
10021da177e4SLinus Torvalds {
1003711584eaSDenis Kirjanov 	struct hci_cb *cb;
10041da177e4SLinus Torvalds 
1005f20d09d5SGustavo F. Padovan 	read_lock(&hci_cb_list_lock);
1006711584eaSDenis Kirjanov 	list_for_each_entry(cb, &hci_cb_list, list) {
10071da177e4SLinus Torvalds 		if (cb->role_switch_cfm)
10081da177e4SLinus Torvalds 			cb->role_switch_cfm(conn, status, role);
10091da177e4SLinus Torvalds 	}
1010f20d09d5SGustavo F. Padovan 	read_unlock(&hci_cb_list_lock);
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10136759a675SJohan Hedberg static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
10146759a675SJohan Hedberg {
101584d9d071SJohan Hedberg 	size_t parsed = 0;
10166759a675SJohan Hedberg 
10176c0c331eSJohan Hedberg 	if (data_len < 2)
10186c0c331eSJohan Hedberg 		return false;
10196c0c331eSJohan Hedberg 
102084d9d071SJohan Hedberg 	while (parsed < data_len - 1) {
102184d9d071SJohan Hedberg 		u8 field_len = data[0];
10226759a675SJohan Hedberg 
10236759a675SJohan Hedberg 		if (field_len == 0)
10246759a675SJohan Hedberg 			break;
10256759a675SJohan Hedberg 
10266759a675SJohan Hedberg 		parsed += field_len + 1;
10276759a675SJohan Hedberg 
10286759a675SJohan Hedberg 		if (parsed > data_len)
10296759a675SJohan Hedberg 			break;
10306759a675SJohan Hedberg 
10316759a675SJohan Hedberg 		if (data[1] == type)
10326759a675SJohan Hedberg 			return true;
10336759a675SJohan Hedberg 
10346759a675SJohan Hedberg 		data += field_len + 1;
10356759a675SJohan Hedberg 	}
10366759a675SJohan Hedberg 
10376759a675SJohan Hedberg 	return false;
10386759a675SJohan Hedberg }
10396759a675SJohan Hedberg 
10409d939d94SVishal Agarwal static inline size_t eir_get_length(u8 *eir, size_t eir_len)
10419d939d94SVishal Agarwal {
10429d939d94SVishal Agarwal 	size_t parsed = 0;
10439d939d94SVishal Agarwal 
10449d939d94SVishal Agarwal 	while (parsed < eir_len) {
10459d939d94SVishal Agarwal 		u8 field_len = eir[0];
10469d939d94SVishal Agarwal 
10479d939d94SVishal Agarwal 		if (field_len == 0)
10489d939d94SVishal Agarwal 			return parsed;
10499d939d94SVishal Agarwal 
10509d939d94SVishal Agarwal 		parsed += field_len + 1;
10519d939d94SVishal Agarwal 		eir += field_len + 1;
10529d939d94SVishal Agarwal 	}
10539d939d94SVishal Agarwal 
10549d939d94SVishal Agarwal 	return eir_len;
10559d939d94SVishal Agarwal }
10569d939d94SVishal Agarwal 
10571dc06093SJohan Hedberg static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
10581dc06093SJohan Hedberg 				  u8 data_len)
10591dc06093SJohan Hedberg {
10601dc06093SJohan Hedberg 	eir[eir_len++] = sizeof(type) + data_len;
10611dc06093SJohan Hedberg 	eir[eir_len++] = type;
10621dc06093SJohan Hedberg 	memcpy(&eir[eir_len], data, data_len);
10631dc06093SJohan Hedberg 	eir_len += data_len;
10641dc06093SJohan Hedberg 
10651dc06093SJohan Hedberg 	return eir_len;
10661dc06093SJohan Hedberg }
10671dc06093SJohan Hedberg 
10681da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *hcb);
10691da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *hcb);
10701da177e4SLinus Torvalds 
10713119ae95SJohan Hedberg struct hci_request {
10723119ae95SJohan Hedberg 	struct hci_dev		*hdev;
10733119ae95SJohan Hedberg 	struct sk_buff_head	cmd_q;
10745d73e034SAndre Guedes 
10755d73e034SAndre Guedes 	/* If something goes wrong when building the HCI request, the error
10765d73e034SAndre Guedes 	 * value is stored in this field.
10775d73e034SAndre Guedes 	 */
10785d73e034SAndre Guedes 	int			err;
10793119ae95SJohan Hedberg };
10803119ae95SJohan Hedberg 
10813119ae95SJohan Hedberg void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
10823119ae95SJohan Hedberg int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
1083e348fe6bSAndre Guedes void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param);
108402350a72SJohan Hedberg void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, void *param,
108502350a72SJohan Hedberg 		    u8 event);
10869238f36aSJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
10873119ae95SJohan Hedberg 
108875e84b7cSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
108975e84b7cSJohan Hedberg 			       void *param, u32 timeout);
10907b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
10917b1abbbeSJohan Hedberg 				  void *param, u8 event, u32 timeout);
109275e84b7cSJohan Hedberg 
1093a9de9248SMarcel Holtmann int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
109473d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
10950d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
10961da177e4SLinus Torvalds 
1097a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds /* ----- HCI Sockets ----- */
1100470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
1101470fe1b5SMarcel Holtmann void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk);
1102cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
11031da177e4SLinus Torvalds 
1104040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event);
1105040030efSMarcel Holtmann 
11060381101fSJohan Hedberg /* Management interface */
1107591f47f3SAndre Guedes #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
1108591f47f3SAndre Guedes #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
1109591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1110591f47f3SAndre Guedes #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
1111591f47f3SAndre Guedes 					 BIT(BDADDR_LE_PUBLIC) | \
1112591f47f3SAndre Guedes 					 BIT(BDADDR_LE_RANDOM))
1113f39799f5SAndre Guedes 
11140381101fSJohan Hedberg int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
1115744cf19eSJohan Hedberg int mgmt_index_added(struct hci_dev *hdev);
1116744cf19eSJohan Hedberg int mgmt_index_removed(struct hci_dev *hdev);
1117744cf19eSJohan Hedberg int mgmt_powered(struct hci_dev *hdev, u8 powered);
1118744cf19eSJohan Hedberg int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
1119744cf19eSJohan Hedberg int mgmt_connectable(struct hci_dev *hdev, u8 connectable);
1120744cf19eSJohan Hedberg int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status);
1121744cf19eSJohan Hedberg int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1122745c0ce3SVishal Agarwal 		      bool persistent);
1123afc747a6SJohan Hedberg int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
112404124681SGustavo F. Padovan 			  u8 addr_type, u32 flags, u8 *name, u8 name_len,
112504124681SGustavo F. Padovan 			  u8 *dev_class);
1126afc747a6SJohan Hedberg int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
1127f0d6a0eaSMikel Astiz 			     u8 link_type, u8 addr_type, u8 reason);
112888c3df13SJohan Hedberg int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
112988c3df13SJohan Hedberg 			   u8 link_type, u8 addr_type, u8 status);
113048264f06SJohan Hedberg int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
113148264f06SJohan Hedberg 			u8 addr_type, u8 status);
1132744cf19eSJohan Hedberg int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
1133744cf19eSJohan Hedberg int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1134c35938b2SSzymon Janc 				 u8 status);
1135744cf19eSJohan Hedberg int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1136744cf19eSJohan Hedberg 				     u8 status);
1137744cf19eSJohan Hedberg int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1138272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type, __le32 value,
1139272d90dfSJohan Hedberg 			      u8 confirm_hint);
1140744cf19eSJohan Hedberg int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1141272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1142272d90dfSJohan Hedberg int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1143272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
1144272d90dfSJohan Hedberg int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1145272d90dfSJohan Hedberg 			      u8 link_type, u8 addr_type);
1146604086b7SBrian Gix int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1147272d90dfSJohan Hedberg 				     u8 link_type, u8 addr_type, u8 status);
1148272d90dfSJohan Hedberg int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1149272d90dfSJohan Hedberg 					 u8 link_type, u8 addr_type, u8 status);
115092a25256SJohan Hedberg int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
115192a25256SJohan Hedberg 			     u8 link_type, u8 addr_type, u32 passkey,
115292a25256SJohan Hedberg 			     u8 entered);
1153bab73cb6SJohan Hedberg int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1154bab73cb6SJohan Hedberg 		     u8 addr_type, u8 status);
115533ef95edSJohan Hedberg int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
1156c0ecddc2SJohan Hedberg int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
11577f9a903cSMarcel Holtmann int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
11587f9a903cSMarcel Holtmann 				   u8 status);
1159744cf19eSJohan Hedberg int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
1160744cf19eSJohan Hedberg int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
1161744cf19eSJohan Hedberg 					    u8 *randomizer, u8 status);
116206199cf8SJohan Hedberg int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
116348264f06SJohan Hedberg int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
116404124681SGustavo F. Padovan 		      u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
116504124681SGustavo F. Padovan 		      u8 ssp, u8 *eir, u16 eir_len);
1166b644ba33SJohan Hedberg int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1167b644ba33SJohan Hedberg 		     u8 addr_type, s8 rssi, u8 *name, u8 name_len);
11687a135109SAndre Guedes int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status);
1169e6d465cbSAndre Guedes int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status);
1170744cf19eSJohan Hedberg int mgmt_discovering(struct hci_dev *hdev, u8 discovering);
11715e0452c0SAndre Guedes int mgmt_interleaved_discovery(struct hci_dev *hdev);
117288c1fe4bSJohan Hedberg int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
117388c1fe4bSJohan Hedberg int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1174bb4b2a9aSAndrei Emeltchenko bool mgmt_valid_hdev(struct hci_dev *hdev);
1175346af67bSVinicius Costa Gomes int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent);
1176346af67bSVinicius Costa Gomes 
11771da177e4SLinus Torvalds /* HCI info for socket */
11781da177e4SLinus Torvalds #define hci_pi(sk) ((struct hci_pinfo *) sk)
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds struct hci_pinfo {
11811da177e4SLinus Torvalds 	struct bt_sock    bt;
11821da177e4SLinus Torvalds 	struct hci_dev    *hdev;
11831da177e4SLinus Torvalds 	struct hci_filter filter;
11841da177e4SLinus Torvalds 	__u32             cmsg_mask;
1185c02178d2SJohan Hedberg 	unsigned short   channel;
11861da177e4SLinus Torvalds };
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds /* HCI security filter */
11891da177e4SLinus Torvalds #define HCI_SFLT_MAX_OGF  5
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds struct hci_sec_filter {
11921da177e4SLinus Torvalds 	__u32 type_mask;
11931da177e4SLinus Torvalds 	__u32 event_mask[2];
11941da177e4SLinus Torvalds 	__u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
11951da177e4SLinus Torvalds };
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds /* ----- HCI requests ----- */
11981da177e4SLinus Torvalds #define HCI_REQ_DONE	  0
11991da177e4SLinus Torvalds #define HCI_REQ_PEND	  1
12001da177e4SLinus Torvalds #define HCI_REQ_CANCELED  2
12011da177e4SLinus Torvalds 
1202a6a67efdSThomas Gleixner #define hci_req_lock(d)		mutex_lock(&d->req_lock)
1203a6a67efdSThomas Gleixner #define hci_req_unlock(d)	mutex_unlock(&d->req_lock)
12041da177e4SLinus Torvalds 
120504b4edcbSJohan Hedberg void hci_update_ad(struct hci_request *req);
120604b4edcbSJohan Hedberg 
12072ce603ebSClaudio Takahasi void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
12082ce603ebSClaudio Takahasi 					u16 latency, u16 to_multiplier);
1209a7a595f6SVinicius Costa Gomes void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
1210a7a595f6SVinicius Costa Gomes 							__u8 ltk[16]);
12112519a1fcSAndre Guedes int hci_do_inquiry(struct hci_dev *hdev, u8 length);
1212023d5049SAndre Guedes int hci_cancel_inquiry(struct hci_dev *hdev);
121328b75a89SAndre Guedes int hci_le_scan(struct hci_dev *hdev, u8 type, u16 interval, u16 window,
121428b75a89SAndre Guedes 		int timeout);
12157dbfac1dSAndre Guedes int hci_cancel_le_scan(struct hci_dev *hdev);
12162519a1fcSAndre Guedes 
121731f7956cSAndre Guedes u8 bdaddr_to_le(u8 bdaddr_type);
121831f7956cSAndre Guedes 
12191da177e4SLinus Torvalds #endif /* __HCI_CORE_H */
1220