11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds    Copyright (C) 2000-2001 Qualcomm Incorporated
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 __BLUETOOTH_H
261da177e4SLinus Torvalds #define __BLUETOOTH_H
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/poll.h>
291da177e4SLinus Torvalds #include <net/sock.h>
30256a06c8SMasatake YAMATO #include <linux/seq_file.h>
311da177e4SLinus Torvalds 
329e8305b3SMarcel Holtmann #define BT_SUBSYS_VERSION	2
334037a774SMarcel Holtmann #define BT_SUBSYS_REVISION	22
34dd31506dSMarcel Holtmann 
351da177e4SLinus Torvalds #ifndef AF_BLUETOOTH
361da177e4SLinus Torvalds #define AF_BLUETOOTH	31
371da177e4SLinus Torvalds #define PF_BLUETOOTH	AF_BLUETOOTH
381da177e4SLinus Torvalds #endif
391da177e4SLinus Torvalds 
40d095c1ebSAndrei Emeltchenko /* Bluetooth versions */
41d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_1	1
42d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_1_2	2
43d095c1ebSAndrei Emeltchenko #define BLUETOOTH_VER_2_0	3
44cde1a8a9SIsmael Ferreras Morezuelas #define BLUETOOTH_VER_2_1	4
45cde1a8a9SIsmael Ferreras Morezuelas #define BLUETOOTH_VER_4_0	6
46d095c1ebSAndrei Emeltchenko 
471da177e4SLinus Torvalds /* Reserv for core and drivers use */
481da177e4SLinus Torvalds #define BT_SKB_RESERVE	8
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define BTPROTO_L2CAP	0
511da177e4SLinus Torvalds #define BTPROTO_HCI	1
521da177e4SLinus Torvalds #define BTPROTO_SCO	2
531da177e4SLinus Torvalds #define BTPROTO_RFCOMM	3
541da177e4SLinus Torvalds #define BTPROTO_BNEP	4
551da177e4SLinus Torvalds #define BTPROTO_CMTP	5
561da177e4SLinus Torvalds #define BTPROTO_HIDP	6
571da177e4SLinus Torvalds #define BTPROTO_AVDTP	7
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define SOL_HCI		0
601da177e4SLinus Torvalds #define SOL_L2CAP	6
611da177e4SLinus Torvalds #define SOL_SCO		17
621da177e4SLinus Torvalds #define SOL_RFCOMM	18
631da177e4SLinus Torvalds 
648c1b2355SMarcel Holtmann #define BT_SECURITY	4
658c1b2355SMarcel Holtmann struct bt_security {
668c1b2355SMarcel Holtmann 	__u8 level;
678f360119SVinicius Costa Gomes 	__u8 key_size;
688c1b2355SMarcel Holtmann };
698c1b2355SMarcel Holtmann #define BT_SECURITY_SDP		0
708c1b2355SMarcel Holtmann #define BT_SECURITY_LOW		1
718c1b2355SMarcel Holtmann #define BT_SECURITY_MEDIUM	2
728c1b2355SMarcel Holtmann #define BT_SECURITY_HIGH	3
737b5a9241SMarcel Holtmann #define BT_SECURITY_FIPS	4
748c1b2355SMarcel Holtmann 
75c4f912e1SMarcel Holtmann #define BT_DEFER_SETUP	7
76c4f912e1SMarcel Holtmann 
77e702112fSAndrei Emeltchenko #define BT_FLUSHABLE	8
78e702112fSAndrei Emeltchenko 
79e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_OFF	0
80e702112fSAndrei Emeltchenko #define BT_FLUSHABLE_ON		1
81e702112fSAndrei Emeltchenko 
8214b12d0bSJaikumar Ganesh #define BT_POWER	9
8314b12d0bSJaikumar Ganesh struct bt_power {
8414b12d0bSJaikumar Ganesh 	__u8 force_active;
8514b12d0bSJaikumar Ganesh };
8614b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_OFF 0
8714b12d0bSJaikumar Ganesh #define BT_POWER_FORCE_ACTIVE_ON  1
8814b12d0bSJaikumar Ganesh 
89c14968b0SMat Martineau #define BT_CHANNEL_POLICY	10
90c14968b0SMat Martineau 
91c14968b0SMat Martineau /* BR/EDR only (default policy)
92c14968b0SMat Martineau  *   AMP controllers cannot be used.
93c14968b0SMat Martineau  *   Channel move requests from the remote device are denied.
94c14968b0SMat Martineau  *   If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
95c14968b0SMat Martineau  */
96c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_ONLY		0
97c14968b0SMat Martineau 
98c14968b0SMat Martineau /* BR/EDR Preferred
99c14968b0SMat Martineau  *   Allow use of AMP controllers.
100c14968b0SMat Martineau  *   If the L2CAP channel is currently on AMP, move it to BR/EDR.
101c14968b0SMat Martineau  *   Channel move requests from the remote device are allowed.
102c14968b0SMat Martineau  */
103c14968b0SMat Martineau #define BT_CHANNEL_POLICY_BREDR_PREFERRED	1
104c14968b0SMat Martineau 
105c14968b0SMat Martineau /* AMP Preferred
106c14968b0SMat Martineau  *   Allow use of AMP controllers
107c14968b0SMat Martineau  *   If the L2CAP channel is currently on BR/EDR and AMP controller
108c14968b0SMat Martineau  *     resources are available, initiate a channel move to AMP.
109c14968b0SMat Martineau  *   Channel move requests from the remote device are allowed.
110c14968b0SMat Martineau  *   If the L2CAP socket has not been connected yet, try to create
111c14968b0SMat Martineau  *     and configure the channel directly on an AMP controller rather
112c14968b0SMat Martineau  *     than BR/EDR.
113c14968b0SMat Martineau  */
114c14968b0SMat Martineau #define BT_CHANNEL_POLICY_AMP_PREFERRED		2
115c14968b0SMat Martineau 
116ad10b1a4SFrédéric Dalleau #define BT_VOICE		11
117ad10b1a4SFrédéric Dalleau struct bt_voice {
118ad10b1a4SFrédéric Dalleau 	__u16 setting;
119ad10b1a4SFrédéric Dalleau };
120ad10b1a4SFrédéric Dalleau 
121ad10b1a4SFrédéric Dalleau #define BT_VOICE_TRANSPARENT			0x0003
122ad10b1a4SFrédéric Dalleau #define BT_VOICE_CVSD_16BIT			0x0060
123ad10b1a4SFrédéric Dalleau 
1241f435424SJohan Hedberg #define BT_SNDMTU		12
1251f435424SJohan Hedberg #define BT_RCVMTU		13
126eab2404bSLuiz Augusto von Dentz #define BT_PHY			14
127eab2404bSLuiz Augusto von Dentz 
128eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_1SLOT	0x00000001
129eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_3SLOT	0x00000002
130eab2404bSLuiz Augusto von Dentz #define BT_PHY_BR_1M_5SLOT	0x00000004
131eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_1SLOT	0x00000008
132eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_3SLOT	0x00000010
133eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_2M_5SLOT	0x00000020
134eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_1SLOT	0x00000040
135eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_3SLOT	0x00000080
136eab2404bSLuiz Augusto von Dentz #define BT_PHY_EDR_3M_5SLOT	0x00000100
137eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_TX		0x00000200
138eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_1M_RX		0x00000400
139eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_TX		0x00000800
140eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_2M_RX		0x00001000
141eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_TX	0x00002000
142eab2404bSLuiz Augusto von Dentz #define BT_PHY_LE_CODED_RX	0x00004000
1431f435424SJohan Hedberg 
1443ee7b7cdSLuiz Augusto von Dentz #define BT_MODE			15
1453ee7b7cdSLuiz Augusto von Dentz 
1463ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_BASIC		0x00
1473ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_ERTM		0x01
1483ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_STREAMING	0x02
1493ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_LE_FLOWCTL	0x03
1503ee7b7cdSLuiz Augusto von Dentz #define BT_MODE_EXT_FLOWCTL	0x04
1513ee7b7cdSLuiz Augusto von Dentz 
15200398e1dSAlain Michaud #define BT_PKT_STATUS          16
15300398e1dSAlain Michaud 
15400398e1dSAlain Michaud #define BT_SCM_PKT_STATUS	0x03
15500398e1dSAlain Michaud 
156248733e8SKiran K #define BT_CODEC	19
157248733e8SKiran K 
158248733e8SKiran K struct	bt_codec_caps {
159248733e8SKiran K 	__u8	len;
160248733e8SKiran K 	__u8	data[];
161248733e8SKiran K } __packed;
162248733e8SKiran K 
163248733e8SKiran K struct bt_codec {
164248733e8SKiran K 	__u8	id;
165248733e8SKiran K 	__u16	cid;
166248733e8SKiran K 	__u16	vid;
167248733e8SKiran K 	__u8	data_path;
168248733e8SKiran K 	__u8	num_caps;
169248733e8SKiran K } __packed;
170248733e8SKiran K 
171248733e8SKiran K struct bt_codecs {
172248733e8SKiran K 	__u8		num_codecs;
173248733e8SKiran K 	struct bt_codec	codecs[];
174248733e8SKiran K } __packed;
175248733e8SKiran K 
176f6873401SKiran K #define BT_CODEC_CVSD		0x02
177b2af264aSKiran K #define BT_CODEC_TRANSPARENT	0x03
178904c139aSKiran K #define BT_CODEC_MSBC		0x05
179f6873401SKiran K 
1803ed7003eSJoe Perches __printf(1, 2)
1812b0bf6c8SJoe Perches void bt_info(const char *fmt, ...);
1823ed7003eSJoe Perches __printf(1, 2)
183594b31eaSFrederic Danis void bt_warn(const char *fmt, ...);
184594b31eaSFrederic Danis __printf(1, 2)
1852b0bf6c8SJoe Perches void bt_err(const char *fmt, ...);
186e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
187e625e50cSMarcel Holtmann void bt_dbg_set(bool enable);
188e625e50cSMarcel Holtmann bool bt_dbg_get(void);
189e625e50cSMarcel Holtmann __printf(1, 2)
190e625e50cSMarcel Holtmann void bt_dbg(const char *fmt, ...);
191e625e50cSMarcel Holtmann #endif
192e781b7f7SSzymon Janc __printf(1, 2)
19336278a5dSAlain Michaud void bt_warn_ratelimited(const char *fmt, ...);
19436278a5dSAlain Michaud __printf(1, 2)
195e781b7f7SSzymon Janc void bt_err_ratelimited(const char *fmt, ...);
196e1447d8dSJoe Perches 
1973ed7003eSJoe Perches #define BT_INFO(fmt, ...)	bt_info(fmt "\n", ##__VA_ARGS__)
198594b31eaSFrederic Danis #define BT_WARN(fmt, ...)	bt_warn(fmt "\n", ##__VA_ARGS__)
1993ed7003eSJoe Perches #define BT_ERR(fmt, ...)	bt_err(fmt "\n", ##__VA_ARGS__)
200e625e50cSMarcel Holtmann 
201e625e50cSMarcel Holtmann #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
202e625e50cSMarcel Holtmann #define BT_DBG(fmt, ...)	bt_dbg(fmt "\n", ##__VA_ARGS__)
203e625e50cSMarcel Holtmann #else
2043ed7003eSJoe Perches #define BT_DBG(fmt, ...)	pr_debug(fmt "\n", ##__VA_ARGS__)
205e625e50cSMarcel Holtmann #endif
2061da177e4SLinus Torvalds 
2079b392e0eSLuiz Augusto von Dentz #define bt_dev_name(hdev) ((hdev) ? (hdev)->name : "null")
2089b392e0eSLuiz Augusto von Dentz 
2096f558b70SLoic Poulain #define bt_dev_info(hdev, fmt, ...)				\
2109b392e0eSLuiz Augusto von Dentz 	BT_INFO("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
211594b31eaSFrederic Danis #define bt_dev_warn(hdev, fmt, ...)				\
2129b392e0eSLuiz Augusto von Dentz 	BT_WARN("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2136f558b70SLoic Poulain #define bt_dev_err(hdev, fmt, ...)				\
2149b392e0eSLuiz Augusto von Dentz 	BT_ERR("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2156f558b70SLoic Poulain #define bt_dev_dbg(hdev, fmt, ...)				\
2169b392e0eSLuiz Augusto von Dentz 	BT_DBG("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2176f558b70SLoic Poulain 
21836278a5dSAlain Michaud #define bt_dev_warn_ratelimited(hdev, fmt, ...)			\
2199b392e0eSLuiz Augusto von Dentz 	bt_warn_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2202064ee33SMarcel Holtmann #define bt_dev_err_ratelimited(hdev, fmt, ...)			\
2219b392e0eSLuiz Augusto von Dentz 	bt_err_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
2222064ee33SMarcel Holtmann 
2231da177e4SLinus Torvalds /* Connection and socket states */
2241da177e4SLinus Torvalds enum {
2251da177e4SLinus Torvalds 	BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
2261da177e4SLinus Torvalds 	BT_OPEN,
2271da177e4SLinus Torvalds 	BT_BOUND,
2281da177e4SLinus Torvalds 	BT_LISTEN,
2291da177e4SLinus Torvalds 	BT_CONNECT,
2301da177e4SLinus Torvalds 	BT_CONNECT2,
2311da177e4SLinus Torvalds 	BT_CONFIG,
2321da177e4SLinus Torvalds 	BT_DISCONN,
2331da177e4SLinus Torvalds 	BT_CLOSED
2341da177e4SLinus Torvalds };
2351da177e4SLinus Torvalds 
23620d1803aSAndrei Emeltchenko /* If unused will be removed by compiler */
23720d1803aSAndrei Emeltchenko static inline const char *state_to_string(int state)
23820d1803aSAndrei Emeltchenko {
23920d1803aSAndrei Emeltchenko 	switch (state) {
24020d1803aSAndrei Emeltchenko 	case BT_CONNECTED:
24120d1803aSAndrei Emeltchenko 		return "BT_CONNECTED";
24220d1803aSAndrei Emeltchenko 	case BT_OPEN:
24320d1803aSAndrei Emeltchenko 		return "BT_OPEN";
24420d1803aSAndrei Emeltchenko 	case BT_BOUND:
24520d1803aSAndrei Emeltchenko 		return "BT_BOUND";
24620d1803aSAndrei Emeltchenko 	case BT_LISTEN:
24720d1803aSAndrei Emeltchenko 		return "BT_LISTEN";
24820d1803aSAndrei Emeltchenko 	case BT_CONNECT:
24920d1803aSAndrei Emeltchenko 		return "BT_CONNECT";
25020d1803aSAndrei Emeltchenko 	case BT_CONNECT2:
25120d1803aSAndrei Emeltchenko 		return "BT_CONNECT2";
25220d1803aSAndrei Emeltchenko 	case BT_CONFIG:
25320d1803aSAndrei Emeltchenko 		return "BT_CONFIG";
25420d1803aSAndrei Emeltchenko 	case BT_DISCONN:
25520d1803aSAndrei Emeltchenko 		return "BT_DISCONN";
25620d1803aSAndrei Emeltchenko 	case BT_CLOSED:
25720d1803aSAndrei Emeltchenko 		return "BT_CLOSED";
25820d1803aSAndrei Emeltchenko 	}
25920d1803aSAndrei Emeltchenko 
26020d1803aSAndrei Emeltchenko 	return "invalid state";
26120d1803aSAndrei Emeltchenko }
26220d1803aSAndrei Emeltchenko 
2631da177e4SLinus Torvalds /* BD Address */
2641da177e4SLinus Torvalds typedef struct {
2651da177e4SLinus Torvalds 	__u8 b[6];
26666c853ccSGustavo F. Padovan } __packed bdaddr_t;
2671da177e4SLinus Torvalds 
268591f47f3SAndre Guedes /* BD Address type */
269591f47f3SAndre Guedes #define BDADDR_BREDR		0x00
270591f47f3SAndre Guedes #define BDADDR_LE_PUBLIC	0x01
271591f47f3SAndre Guedes #define BDADDR_LE_RANDOM	0x02
272591f47f3SAndre Guedes 
27321fcf572SPavel Machek static inline bool bdaddr_type_is_valid(u8 type)
274679efe2bSJohan Hedberg {
275679efe2bSJohan Hedberg 	switch (type) {
276679efe2bSJohan Hedberg 	case BDADDR_BREDR:
277679efe2bSJohan Hedberg 	case BDADDR_LE_PUBLIC:
278679efe2bSJohan Hedberg 	case BDADDR_LE_RANDOM:
279679efe2bSJohan Hedberg 		return true;
280679efe2bSJohan Hedberg 	}
281679efe2bSJohan Hedberg 
282679efe2bSJohan Hedberg 	return false;
283679efe2bSJohan Hedberg }
284679efe2bSJohan Hedberg 
28521fcf572SPavel Machek static inline bool bdaddr_type_is_le(u8 type)
286679efe2bSJohan Hedberg {
287679efe2bSJohan Hedberg 	switch (type) {
288679efe2bSJohan Hedberg 	case BDADDR_LE_PUBLIC:
289679efe2bSJohan Hedberg 	case BDADDR_LE_RANDOM:
290679efe2bSJohan Hedberg 		return true;
291679efe2bSJohan Hedberg 	}
292679efe2bSJohan Hedberg 
293679efe2bSJohan Hedberg 	return false;
294679efe2bSJohan Hedberg }
295679efe2bSJohan Hedberg 
2961da177e4SLinus Torvalds #define BDADDR_ANY  (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
297a59ac2f7SMarcel Holtmann #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /* Copy, swap, convert BD Address */
300f53c20e9SDavid Herrmann static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	return memcmp(ba1, ba2, sizeof(bdaddr_t));
3031da177e4SLinus Torvalds }
304f53c20e9SDavid Herrmann static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	memcpy(dst, src, sizeof(bdaddr_t));
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
30965bce462SLoic Poulain void baswap(bdaddr_t *dst, const bdaddr_t *src);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /* Common socket structures and functions */
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds #define bt_sk(__sk) ((struct bt_sock *) __sk)
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds struct bt_sock {
3161da177e4SLinus Torvalds 	struct sock sk;
3171da177e4SLinus Torvalds 	struct list_head accept_q;
3181da177e4SLinus Torvalds 	struct sock *parent;
319c5daa683SGustavo Padovan 	unsigned long flags;
320d9763698SMarcel Holtmann 	void (*skb_msg_name)(struct sk_buff *, void *, int *);
32100398e1dSAlain Michaud 	void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *);
322c5daa683SGustavo Padovan };
323c5daa683SGustavo Padovan 
324c5daa683SGustavo Padovan enum {
325c5daa683SGustavo Padovan 	BT_SK_DEFER_SETUP,
326c5daa683SGustavo Padovan 	BT_SK_SUSPEND,
3271da177e4SLinus Torvalds };
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds struct bt_sock_list {
3301da177e4SLinus Torvalds 	struct hlist_head head;
3311da177e4SLinus Torvalds 	rwlock_t          lock;
332256a06c8SMasatake YAMATO #ifdef CONFIG_PROC_FS
333256a06c8SMasatake YAMATO         int (* custom_seq_show)(struct seq_file *, void *);
334256a06c8SMasatake YAMATO #endif
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
337ec1b4cf7SStephen Hemminger int  bt_sock_register(int proto, const struct net_proto_family *ops);
338be9f97f0SDavid Herrmann void bt_sock_unregister(int proto);
3391da177e4SLinus Torvalds void bt_sock_link(struct bt_sock_list *l, struct sock *s);
3401da177e4SLinus Torvalds void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
3411b784140SYing Xue int  bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3421b784140SYing Xue 		     int flags);
3431b784140SYing Xue int  bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
3441b784140SYing Xue 			    size_t len, int flags);
345a11e1d43SLinus Torvalds __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
3463241ad82SMarcel Holtmann int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
3471da177e4SLinus Torvalds int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
348da891217SGavin Li int  bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags);
3491da177e4SLinus Torvalds 
350c4f5627fSMatthias Kaehlcke void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
3511da177e4SLinus Torvalds void bt_accept_unlink(struct sock *sk);
3521da177e4SLinus Torvalds struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /* Skb helpers */
35500e3112cSMat Martineau struct l2cap_ctrl {
35621fcf572SPavel Machek 	u8	sframe:1,
35700e3112cSMat Martineau 		poll:1,
35800e3112cSMat Martineau 		final:1,
35900e3112cSMat Martineau 		fcs:1,
36000e3112cSMat Martineau 		sar:2,
36100e3112cSMat Martineau 		super:2;
36221fcf572SPavel Machek 
36321fcf572SPavel Machek 	u16	reqseq;
36421fcf572SPavel Machek 	u16	txseq;
36521fcf572SPavel Machek 	u8	retries;
366a4368ff3SJohan Hedberg 	__le16  psm;
367a4368ff3SJohan Hedberg 	bdaddr_t bdaddr;
368a4368ff3SJohan Hedberg 	struct l2cap_chan *chan;
36900e3112cSMat Martineau };
37000e3112cSMat Martineau 
37100398e1dSAlain Michaud struct sco_ctrl {
37200398e1dSAlain Michaud 	u8	pkt_status;
37300398e1dSAlain Michaud };
37400398e1dSAlain Michaud 
3753119ae95SJohan Hedberg struct hci_dev;
3763119ae95SJohan Hedberg 
3771904a853SMarcel Holtmann typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
378e6214487SJohan Hedberg typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
379e6214487SJohan Hedberg 				       u16 opcode, struct sk_buff *skb);
3803119ae95SJohan Hedberg 
38144d27137SJohan Hedberg #define HCI_REQ_START	BIT(0)
38244d27137SJohan Hedberg #define HCI_REQ_SKB	BIT(1)
38344d27137SJohan Hedberg 
384242c0ebdSMarcel Holtmann struct hci_ctrl {
385cba6b758SLuiz Augusto von Dentz 	struct sock *sk;
38621fcf572SPavel Machek 	u16 opcode;
38744d27137SJohan Hedberg 	u8 req_flags;
388242c0ebdSMarcel Holtmann 	u8 req_event;
38944d27137SJohan Hedberg 	union {
390242c0ebdSMarcel Holtmann 		hci_req_complete_t req_complete;
391242c0ebdSMarcel Holtmann 		hci_req_complete_skb_t req_complete_skb;
392db6e3e8dSJohan Hedberg 	};
39344d27137SJohan Hedberg };
394db6e3e8dSJohan Hedberg 
3958aca46f9SLuiz Augusto von Dentz struct mgmt_ctrl {
3968aca46f9SLuiz Augusto von Dentz 	struct hci_dev *hdev;
3978aca46f9SLuiz Augusto von Dentz 	u16 opcode;
3988aca46f9SLuiz Augusto von Dentz };
3998aca46f9SLuiz Augusto von Dentz 
4001da177e4SLinus Torvalds struct bt_skb_cb {
40121fcf572SPavel Machek 	u8 pkt_type;
40221fcf572SPavel Machek 	u8 force_active;
40321fcf572SPavel Machek 	u16 expect;
40421fcf572SPavel Machek 	u8 incoming:1;
405db6e3e8dSJohan Hedberg 	union {
406a4368ff3SJohan Hedberg 		struct l2cap_ctrl l2cap;
40700398e1dSAlain Michaud 		struct sco_ctrl sco;
408242c0ebdSMarcel Holtmann 		struct hci_ctrl hci;
4098aca46f9SLuiz Augusto von Dentz 		struct mgmt_ctrl mgmt;
410db6e3e8dSJohan Hedberg 	};
4111da177e4SLinus Torvalds };
4121c2acffbSGustavo F. Padovan #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
4131da177e4SLinus Torvalds 
414f5c4a42aSMarcel Holtmann #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type
415f5c4a42aSMarcel Holtmann #define hci_skb_expect(skb) bt_cb((skb))->expect
416f5c4a42aSMarcel Holtmann #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode
41785b56857SLuiz Augusto von Dentz #define hci_skb_event(skb) bt_cb((skb))->hci.req_event
418cba6b758SLuiz Augusto von Dentz #define hci_skb_sk(skb) bt_cb((skb))->hci.sk
419f5c4a42aSMarcel Holtmann 
420dd0fc66fSAl Viro static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct sk_buff *skb;
4231da177e4SLinus Torvalds 
424c3c7ea65SGustavo Padovan 	skb = alloc_skb(len + BT_SKB_RESERVE, how);
4255fcc86bdSJohan Hedberg 	if (skb)
4261da177e4SLinus Torvalds 		skb_reserve(skb, BT_SKB_RESERVE);
4271da177e4SLinus Torvalds 	return skb;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4305a9d0a3fSWaldemar Rymarkiewicz static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
4315a9d0a3fSWaldemar Rymarkiewicz 					unsigned long len, int nb, int *err)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct sk_buff *skb;
4341da177e4SLinus Torvalds 
435c3c7ea65SGustavo Padovan 	skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
4365fcc86bdSJohan Hedberg 	if (skb)
4371da177e4SLinus Torvalds 		skb_reserve(skb, BT_SKB_RESERVE);
438e454c844SGustavo F. Padovan 
439e454c844SGustavo F. Padovan 	if (!skb && *err)
440e454c844SGustavo F. Padovan 		return NULL;
441e454c844SGustavo F. Padovan 
442e454c844SGustavo F. Padovan 	*err = sock_error(sk);
443e454c844SGustavo F. Padovan 	if (*err)
444e454c844SGustavo F. Padovan 		goto out;
445e454c844SGustavo F. Padovan 
446e454c844SGustavo F. Padovan 	if (sk->sk_shutdown) {
447e454c844SGustavo F. Padovan 		*err = -ECONNRESET;
448e454c844SGustavo F. Padovan 		goto out;
449e454c844SGustavo F. Padovan 	}
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	return skb;
452e454c844SGustavo F. Padovan 
453e454c844SGustavo F. Padovan out:
454e454c844SGustavo F. Padovan 	kfree_skb(skb);
455e454c844SGustavo F. Padovan 	return NULL;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
45838f64f65SLuiz Augusto von Dentz /* Shall not be called with lock_sock held */
45938f64f65SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk,
46038f64f65SLuiz Augusto von Dentz 					     struct msghdr *msg,
46138f64f65SLuiz Augusto von Dentz 					     size_t len, size_t mtu,
46238f64f65SLuiz Augusto von Dentz 					     size_t headroom, size_t tailroom)
46338f64f65SLuiz Augusto von Dentz {
46438f64f65SLuiz Augusto von Dentz 	struct sk_buff *skb;
46538f64f65SLuiz Augusto von Dentz 	size_t size = min_t(size_t, len, mtu);
46638f64f65SLuiz Augusto von Dentz 	int err;
46738f64f65SLuiz Augusto von Dentz 
46838f64f65SLuiz Augusto von Dentz 	skb = bt_skb_send_alloc(sk, size + headroom + tailroom,
46938f64f65SLuiz Augusto von Dentz 				msg->msg_flags & MSG_DONTWAIT, &err);
47038f64f65SLuiz Augusto von Dentz 	if (!skb)
47138f64f65SLuiz Augusto von Dentz 		return ERR_PTR(err);
47238f64f65SLuiz Augusto von Dentz 
47338f64f65SLuiz Augusto von Dentz 	skb_reserve(skb, headroom);
47438f64f65SLuiz Augusto von Dentz 	skb_tailroom_reserve(skb, mtu, tailroom);
47538f64f65SLuiz Augusto von Dentz 
47638f64f65SLuiz Augusto von Dentz 	if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) {
47738f64f65SLuiz Augusto von Dentz 		kfree_skb(skb);
47838f64f65SLuiz Augusto von Dentz 		return ERR_PTR(-EFAULT);
47938f64f65SLuiz Augusto von Dentz 	}
48038f64f65SLuiz Augusto von Dentz 
48138f64f65SLuiz Augusto von Dentz 	skb->priority = sk->sk_priority;
48238f64f65SLuiz Augusto von Dentz 
48338f64f65SLuiz Augusto von Dentz 	return skb;
48438f64f65SLuiz Augusto von Dentz }
48538f64f65SLuiz Augusto von Dentz 
48697e4e802SLuiz Augusto von Dentz /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments
48797e4e802SLuiz Augusto von Dentz  * accourding to the MTU.
48897e4e802SLuiz Augusto von Dentz  */
48997e4e802SLuiz Augusto von Dentz static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk,
49097e4e802SLuiz Augusto von Dentz 					      struct msghdr *msg,
49197e4e802SLuiz Augusto von Dentz 					      size_t len, size_t mtu,
49297e4e802SLuiz Augusto von Dentz 					      size_t headroom, size_t tailroom)
49397e4e802SLuiz Augusto von Dentz {
49497e4e802SLuiz Augusto von Dentz 	struct sk_buff *skb, **frag;
49597e4e802SLuiz Augusto von Dentz 
49697e4e802SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
4976f43f616SDan Carpenter 	if (IS_ERR(skb))
49897e4e802SLuiz Augusto von Dentz 		return skb;
49997e4e802SLuiz Augusto von Dentz 
50097e4e802SLuiz Augusto von Dentz 	len -= skb->len;
50197e4e802SLuiz Augusto von Dentz 	if (!len)
50297e4e802SLuiz Augusto von Dentz 		return skb;
50397e4e802SLuiz Augusto von Dentz 
50497e4e802SLuiz Augusto von Dentz 	/* Add remaining data over MTU as continuation fragments */
50597e4e802SLuiz Augusto von Dentz 	frag = &skb_shinfo(skb)->frag_list;
50697e4e802SLuiz Augusto von Dentz 	while (len) {
50797e4e802SLuiz Augusto von Dentz 		struct sk_buff *tmp;
50897e4e802SLuiz Augusto von Dentz 
50997e4e802SLuiz Augusto von Dentz 		tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
510266191aaSLuiz Augusto von Dentz 		if (IS_ERR(tmp)) {
51129fb6083SLuiz Augusto von Dentz 			return skb;
51297e4e802SLuiz Augusto von Dentz 		}
51397e4e802SLuiz Augusto von Dentz 
51497e4e802SLuiz Augusto von Dentz 		len -= tmp->len;
51597e4e802SLuiz Augusto von Dentz 
51697e4e802SLuiz Augusto von Dentz 		*frag = tmp;
51797e4e802SLuiz Augusto von Dentz 		frag = &(*frag)->next;
51897e4e802SLuiz Augusto von Dentz 	}
51997e4e802SLuiz Augusto von Dentz 
52097e4e802SLuiz Augusto von Dentz 	return skb;
52197e4e802SLuiz Augusto von Dentz }
52297e4e802SLuiz Augusto von Dentz 
52321fcf572SPavel Machek int bt_to_errno(u16 code);
524*ca2045e0SLuiz Augusto von Dentz __u8 bt_status(int err);
5251da177e4SLinus Torvalds 
5266befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr);
5276befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr);
528c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr);
529d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk);
53070ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk);
5316befc644SMarcel Holtmann 
532e74e58f8SJoe Perches int hci_sock_init(void);
533e74e58f8SJoe Perches void hci_sock_cleanup(void);
5346516455dSMarcel Holtmann 
535e74e58f8SJoe Perches int bt_sysfs_init(void);
536e74e58f8SJoe Perches void bt_sysfs_cleanup(void);
5376516455dSMarcel Holtmann 
538e74e58f8SJoe Perches int bt_procfs_init(struct net *net, const char *name,
539256a06c8SMasatake YAMATO 		   struct bt_sock_list *sk_list,
540256a06c8SMasatake YAMATO 		   int (*seq_show)(struct seq_file *, void *));
541e74e58f8SJoe Perches void bt_procfs_cleanup(struct net *net, const char *name);
542256a06c8SMasatake YAMATO 
543aef7d97cSMarcel Holtmann extern struct dentry *bt_debugfs;
544be9d1227SMarcel Holtmann 
54564274518SGustavo F. Padovan int l2cap_init(void);
54664274518SGustavo F. Padovan void l2cap_exit(void);
54764274518SGustavo F. Padovan 
548ff50e8afSArron Wang #if IS_ENABLED(CONFIG_BT_BREDR)
54964274518SGustavo F. Padovan int sco_init(void);
55064274518SGustavo F. Padovan void sco_exit(void);
551ff50e8afSArron Wang #else
552ff50e8afSArron Wang static inline int sco_init(void)
553ff50e8afSArron Wang {
554ff50e8afSArron Wang 	return 0;
555ff50e8afSArron Wang }
556ff50e8afSArron Wang 
557ff50e8afSArron Wang static inline void sco_exit(void)
558ff50e8afSArron Wang {
559ff50e8afSArron Wang }
560ff50e8afSArron Wang #endif
56164274518SGustavo F. Padovan 
5626d785aa3SJohan Hedberg int mgmt_init(void);
5636d785aa3SJohan Hedberg void mgmt_exit(void);
5646d785aa3SJohan Hedberg 
565d22015aaSOctavian Purdila void bt_sock_reclassify_lock(struct sock *sk, int proto);
566d22015aaSOctavian Purdila 
5671da177e4SLinus Torvalds #endif /* __BLUETOOTH_H */
568