Commit | Line | Data |
---|---|---|
38351c66 | 1 | /* |
1da177e4 LT |
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 | |
38351c66 GP |
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 | |
1da177e4 LT |
18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 | ||
38351c66 GP |
20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
22 | SOFTWARE IS DISCLAIMED. |
23 | */ | |
24 | ||
25 | #ifndef __BLUETOOTH_H | |
26 | #define __BLUETOOTH_H | |
27 | ||
1da177e4 LT |
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 | ||
d095c1eb AE |
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 | ||
1da177e4 LT |
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 | ||
8c1b2355 MH |
58 | #define BT_SECURITY 4 |
59 | struct bt_security { | |
60 | __u8 level; | |
8f360119 | 61 | __u8 key_size; |
8c1b2355 MH |
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 | ||
c4f912e1 MH |
68 | #define BT_DEFER_SETUP 7 |
69 | ||
e702112f AE |
70 | #define BT_FLUSHABLE 8 |
71 | ||
72 | #define BT_FLUSHABLE_OFF 0 | |
73 | #define BT_FLUSHABLE_ON 1 | |
74 | ||
14b12d0b JG |
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 | ||
c14968b0 MM |
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 | ||
3ed7003e JP |
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__) | |
1da177e4 | 117 | |
1da177e4 LT |
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 | ||
20d1803a AE |
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 | ||
1da177e4 LT |
158 | /* BD Address */ |
159 | typedef struct { | |
160 | __u8 b[6]; | |
66c853cc | 161 | } __packed bdaddr_t; |
1da177e4 | 162 | |
591f47f3 AG |
163 | /* BD Address type */ |
164 | #define BDADDR_BREDR 0x00 | |
165 | #define BDADDR_LE_PUBLIC 0x01 | |
166 | #define BDADDR_LE_RANDOM 0x02 | |
167 | ||
c3c7ea65 GP |
168 | #define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0} }) |
169 | #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff} }) | |
1da177e4 LT |
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); | |
1da177e4 LT |
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; | |
c5daa683 GP |
194 | unsigned long flags; |
195 | }; | |
196 | ||
197 | enum { | |
198 | BT_SK_DEFER_SETUP, | |
199 | BT_SK_SUSPEND, | |
1da177e4 LT |
200 | }; |
201 | ||
202 | struct bt_sock_list { | |
203 | struct hlist_head head; | |
204 | rwlock_t lock; | |
205 | }; | |
206 | ||
ec1b4cf7 | 207 | int bt_sock_register(int proto, const struct net_proto_family *ops); |
1da177e4 LT |
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); | |
5a9d0a3f WR |
211 | int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
212 | struct msghdr *msg, size_t len, int flags); | |
796c86ee MM |
213 | int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock, |
214 | struct msghdr *msg, size_t len, int flags); | |
c3c7ea65 | 215 | uint bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait); |
3241ad82 | 216 | int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); |
1da177e4 LT |
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 */ | |
00e3112c | 224 | struct l2cap_ctrl { |
c3c7ea65 GP |
225 | unsigned int sframe:1, |
226 | poll:1, | |
227 | final:1, | |
228 | fcs:1, | |
229 | sar:2, | |
230 | super:2; | |
00e3112c MM |
231 | __u16 reqseq; |
232 | __u16 txseq; | |
233 | __u8 retries; | |
234 | }; | |
235 | ||
1da177e4 | 236 | struct bt_skb_cb { |
0d48d939 MH |
237 | __u8 pkt_type; |
238 | __u8 incoming; | |
33e882a5 | 239 | __u16 expect; |
14b12d0b | 240 | __u8 force_active; |
00e3112c | 241 | struct l2cap_ctrl control; |
1da177e4 | 242 | }; |
1c2acffb | 243 | #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb)) |
1da177e4 | 244 | |
dd0fc66f | 245 | static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how) |
1da177e4 LT |
246 | { |
247 | struct sk_buff *skb; | |
248 | ||
c3c7ea65 GP |
249 | skb = alloc_skb(len + BT_SKB_RESERVE, how); |
250 | if (skb) { | |
1da177e4 LT |
251 | skb_reserve(skb, BT_SKB_RESERVE); |
252 | bt_cb(skb)->incoming = 0; | |
253 | } | |
254 | return skb; | |
255 | } | |
256 | ||
5a9d0a3f WR |
257 | static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk, |
258 | unsigned long len, int nb, int *err) | |
1da177e4 LT |
259 | { |
260 | struct sk_buff *skb; | |
261 | ||
c3c7ea65 GP |
262 | skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err); |
263 | if (skb) { | |
1da177e4 LT |
264 | skb_reserve(skb, BT_SKB_RESERVE); |
265 | bt_cb(skb)->incoming = 0; | |
266 | } | |
e454c844 GP |
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 | } | |
1da177e4 LT |
279 | |
280 | return skb; | |
e454c844 GP |
281 | |
282 | out: | |
283 | kfree_skb(skb); | |
284 | return NULL; | |
1da177e4 LT |
285 | } |
286 | ||
e175072f | 287 | int bt_to_errno(__u16 code); |
1da177e4 | 288 | |
6516455d | 289 | extern int hci_sock_init(void); |
04005dd9 | 290 | extern void hci_sock_cleanup(void); |
6516455d MH |
291 | |
292 | extern int bt_sysfs_init(void); | |
293 | extern void bt_sysfs_cleanup(void); | |
294 | ||
aef7d97c | 295 | extern struct dentry *bt_debugfs; |
be9d1227 | 296 | |
64274518 GP |
297 | int l2cap_init(void); |
298 | void l2cap_exit(void); | |
64274518 | 299 | |
64274518 GP |
300 | int sco_init(void); |
301 | void sco_exit(void); | |
64274518 | 302 | |
d22015aa OP |
303 | void bt_sock_reclassify_lock(struct sock *sk, int proto); |
304 | ||
1da177e4 | 305 | #endif /* __BLUETOOTH_H */ |