1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4 
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10 
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24 
25 #ifndef __BLUETOOTH_H
26 #define __BLUETOOTH_H
27 
28 #include <linux/poll.h>
29 #include <net/sock.h>
30 
31 #ifndef AF_BLUETOOTH
32 #define AF_BLUETOOTH	31
33 #define PF_BLUETOOTH	AF_BLUETOOTH
34 #endif
35 
36 /* Bluetooth versions */
37 #define BLUETOOTH_VER_1_1	1
38 #define BLUETOOTH_VER_1_2	2
39 #define BLUETOOTH_VER_2_0	3
40 
41 /* Reserv for core and drivers use */
42 #define BT_SKB_RESERVE	8
43 
44 #define BTPROTO_L2CAP	0
45 #define BTPROTO_HCI	1
46 #define BTPROTO_SCO	2
47 #define BTPROTO_RFCOMM	3
48 #define BTPROTO_BNEP	4
49 #define BTPROTO_CMTP	5
50 #define BTPROTO_HIDP	6
51 #define BTPROTO_AVDTP	7
52 
53 #define SOL_HCI		0
54 #define SOL_L2CAP	6
55 #define SOL_SCO		17
56 #define SOL_RFCOMM	18
57 
58 #define BT_SECURITY	4
59 struct bt_security {
60 	__u8 level;
61 	__u8 key_size;
62 };
63 #define BT_SECURITY_SDP		0
64 #define BT_SECURITY_LOW		1
65 #define BT_SECURITY_MEDIUM	2
66 #define BT_SECURITY_HIGH	3
67 
68 #define BT_DEFER_SETUP	7
69 
70 #define BT_FLUSHABLE	8
71 
72 #define BT_FLUSHABLE_OFF	0
73 #define BT_FLUSHABLE_ON		1
74 
75 #define BT_POWER	9
76 struct bt_power {
77 	__u8 force_active;
78 };
79 #define BT_POWER_FORCE_ACTIVE_OFF 0
80 #define BT_POWER_FORCE_ACTIVE_ON  1
81 
82 #define BT_CHANNEL_POLICY	10
83 
84 /* BR/EDR only (default policy)
85  *   AMP controllers cannot be used.
86  *   Channel move requests from the remote device are denied.
87  *   If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
88  */
89 #define BT_CHANNEL_POLICY_BREDR_ONLY		0
90 
91 /* BR/EDR Preferred
92  *   Allow use of AMP controllers.
93  *   If the L2CAP channel is currently on AMP, move it to BR/EDR.
94  *   Channel move requests from the remote device are allowed.
95  */
96 #define BT_CHANNEL_POLICY_BREDR_PREFERRED	1
97 
98 /* AMP Preferred
99  *   Allow use of AMP controllers
100  *   If the L2CAP channel is currently on BR/EDR and AMP controller
101  *     resources are available, initiate a channel move to AMP.
102  *   Channel move requests from the remote device are allowed.
103  *   If the L2CAP socket has not been connected yet, try to create
104  *     and configure the channel directly on an AMP controller rather
105  *     than BR/EDR.
106  */
107 #define BT_CHANNEL_POLICY_AMP_PREFERRED		2
108 
109 __printf(1, 2)
110 int bt_info(const char *fmt, ...);
111 __printf(1, 2)
112 int bt_err(const char *fmt, ...);
113 
114 #define BT_INFO(fmt, ...)	bt_info(fmt "\n", ##__VA_ARGS__)
115 #define BT_ERR(fmt, ...)	bt_err(fmt "\n", ##__VA_ARGS__)
116 #define BT_DBG(fmt, ...)	pr_debug(fmt "\n", ##__VA_ARGS__)
117 
118 /* Connection and socket states */
119 enum {
120 	BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
121 	BT_OPEN,
122 	BT_BOUND,
123 	BT_LISTEN,
124 	BT_CONNECT,
125 	BT_CONNECT2,
126 	BT_CONFIG,
127 	BT_DISCONN,
128 	BT_CLOSED
129 };
130 
131 /* If unused will be removed by compiler */
132 static inline const char *state_to_string(int state)
133 {
134 	switch (state) {
135 	case BT_CONNECTED:
136 		return "BT_CONNECTED";
137 	case BT_OPEN:
138 		return "BT_OPEN";
139 	case BT_BOUND:
140 		return "BT_BOUND";
141 	case BT_LISTEN:
142 		return "BT_LISTEN";
143 	case BT_CONNECT:
144 		return "BT_CONNECT";
145 	case BT_CONNECT2:
146 		return "BT_CONNECT2";
147 	case BT_CONFIG:
148 		return "BT_CONFIG";
149 	case BT_DISCONN:
150 		return "BT_DISCONN";
151 	case BT_CLOSED:
152 		return "BT_CLOSED";
153 	}
154 
155 	return "invalid state";
156 }
157 
158 /* BD Address */
159 typedef struct {
160 	__u8 b[6];
161 } __packed bdaddr_t;
162 
163 /* BD Address type */
164 #define BDADDR_BREDR		0x00
165 #define BDADDR_LE_PUBLIC	0x01
166 #define BDADDR_LE_RANDOM	0x02
167 
168 #define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0} })
169 #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff} })
170 
171 /* Copy, swap, convert BD Address */
172 static inline int bacmp(bdaddr_t *ba1, bdaddr_t *ba2)
173 {
174 	return memcmp(ba1, ba2, sizeof(bdaddr_t));
175 }
176 static inline void bacpy(bdaddr_t *dst, bdaddr_t *src)
177 {
178 	memcpy(dst, src, sizeof(bdaddr_t));
179 }
180 
181 void baswap(bdaddr_t *dst, bdaddr_t *src);
182 char *batostr(bdaddr_t *ba);
183 
184 /* Common socket structures and functions */
185 
186 #define bt_sk(__sk) ((struct bt_sock *) __sk)
187 
188 struct bt_sock {
189 	struct sock sk;
190 	bdaddr_t    src;
191 	bdaddr_t    dst;
192 	struct list_head accept_q;
193 	struct sock *parent;
194 	unsigned long flags;
195 };
196 
197 enum {
198 	BT_SK_DEFER_SETUP,
199 	BT_SK_SUSPEND,
200 };
201 
202 struct bt_sock_list {
203 	struct hlist_head head;
204 	rwlock_t          lock;
205 };
206 
207 int  bt_sock_register(int proto, const struct net_proto_family *ops);
208 int  bt_sock_unregister(int proto);
209 void bt_sock_link(struct bt_sock_list *l, struct sock *s);
210 void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
211 int  bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
212 				struct msghdr *msg, size_t len, int flags);
213 int  bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
214 			struct msghdr *msg, size_t len, int flags);
215 uint bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
216 int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
217 int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
218 
219 void bt_accept_enqueue(struct sock *parent, struct sock *sk);
220 void bt_accept_unlink(struct sock *sk);
221 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
222 
223 /* Skb helpers */
224 struct l2cap_ctrl {
225 	unsigned int	sframe:1,
226 			poll:1,
227 			final:1,
228 			fcs:1,
229 			sar:2,
230 			super:2;
231 	__u16		reqseq;
232 	__u16		txseq;
233 	__u8		retries;
234 };
235 
236 struct bt_skb_cb {
237 	__u8 pkt_type;
238 	__u8 incoming;
239 	__u16 expect;
240 	__u8 force_active;
241 	struct l2cap_ctrl control;
242 };
243 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
244 
245 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
246 {
247 	struct sk_buff *skb;
248 
249 	skb = alloc_skb(len + BT_SKB_RESERVE, how);
250 	if (skb) {
251 		skb_reserve(skb, BT_SKB_RESERVE);
252 		bt_cb(skb)->incoming  = 0;
253 	}
254 	return skb;
255 }
256 
257 static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
258 					unsigned long len, int nb, int *err)
259 {
260 	struct sk_buff *skb;
261 
262 	skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
263 	if (skb) {
264 		skb_reserve(skb, BT_SKB_RESERVE);
265 		bt_cb(skb)->incoming  = 0;
266 	}
267 
268 	if (!skb && *err)
269 		return NULL;
270 
271 	*err = sock_error(sk);
272 	if (*err)
273 		goto out;
274 
275 	if (sk->sk_shutdown) {
276 		*err = -ECONNRESET;
277 		goto out;
278 	}
279 
280 	return skb;
281 
282 out:
283 	kfree_skb(skb);
284 	return NULL;
285 }
286 
287 int bt_to_errno(__u16 code);
288 
289 extern int hci_sock_init(void);
290 extern void hci_sock_cleanup(void);
291 
292 extern int bt_sysfs_init(void);
293 extern void bt_sysfs_cleanup(void);
294 
295 extern struct dentry *bt_debugfs;
296 
297 int l2cap_init(void);
298 void l2cap_exit(void);
299 
300 int sco_init(void);
301 void sco_exit(void);
302 
303 void bt_sock_reclassify_lock(struct sock *sk, int proto);
304 
305 #endif /* __BLUETOOTH_H */
306