ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / net / bluetooth / rfcomm.h
1 /* 
2    RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3    Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4    Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License version 2 as
8    published by the Free Software Foundation;
9
10    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
15    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
16    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
17    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
20    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
21    SOFTWARE IS DISCLAIMED.
22 */
23
24 /* 
25    RPN support    -    Dirk Husemann <hud@zurich.ibm.com>
26 */
27
28 /*
29  * $Id: rfcomm.h,v 1.29 2002/10/02 20:26:17 maxk Exp $
30  */
31
32 #ifndef __RFCOMM_H
33 #define __RFCOMM_H
34
35 #define RFCOMM_PSM 3
36
37 #define RFCOMM_CONN_TIMEOUT (HZ * 30)
38 #define RFCOMM_DISC_TIMEOUT (HZ * 20)
39
40 #define RFCOMM_DEFAULT_MTU      127
41 #define RFCOMM_DEFAULT_CREDITS  7
42
43 #define RFCOMM_MAX_L2CAP_MTU    1024
44 #define RFCOMM_MAX_CREDITS      40
45
46 #define RFCOMM_SKB_HEAD_RESERVE 8
47 #define RFCOMM_SKB_TAIL_RESERVE 2
48 #define RFCOMM_SKB_RESERVE  (RFCOMM_SKB_HEAD_RESERVE + RFCOMM_SKB_TAIL_RESERVE)
49
50 #define RFCOMM_SABM     0x2f
51 #define RFCOMM_DISC     0x43
52 #define RFCOMM_UA       0x63
53 #define RFCOMM_DM       0x0f
54 #define RFCOMM_UIH      0xef
55
56 #define RFCOMM_TEST     0x08
57 #define RFCOMM_FCON     0x28
58 #define RFCOMM_FCOFF    0x18
59 #define RFCOMM_MSC      0x38
60 #define RFCOMM_RPN      0x24
61 #define RFCOMM_RLS      0x14
62 #define RFCOMM_PN       0x20
63 #define RFCOMM_NSC      0x04
64
65 #define RFCOMM_V24_FC   0x02
66 #define RFCOMM_V24_RTC  0x04
67 #define RFCOMM_V24_RTR  0x08
68 #define RFCOMM_V24_IC   0x40
69 #define RFCOMM_V24_DV   0x80
70
71 #define RFCOMM_RPN_BR_2400      0x0
72 #define RFCOMM_RPN_BR_4800      0x1
73 #define RFCOMM_RPN_BR_7200      0x2
74 #define RFCOMM_RPN_BR_9600      0x3
75 #define RFCOMM_RPN_BR_19200     0x4
76 #define RFCOMM_RPN_BR_38400     0x5
77 #define RFCOMM_RPN_BR_57600     0x6
78 #define RFCOMM_RPN_BR_115200    0x7
79 #define RFCOMM_RPN_BR_230400    0x8
80
81 #define RFCOMM_RPN_DATA_5       0x0
82 #define RFCOMM_RPN_DATA_6       0x1
83 #define RFCOMM_RPN_DATA_7       0x2
84 #define RFCOMM_RPN_DATA_8       0x3
85
86 #define RFCOMM_RPN_STOP_1       0
87 #define RFCOMM_RPN_STOP_15      1
88
89 #define RFCOMM_RPN_PARITY_NONE  0x0
90 #define RFCOMM_RPN_PARITY_ODD   0x4
91 #define RFCOMM_RPN_PARITY_EVEN  0x5
92 #define RFCOMM_RPN_PARITY_MARK  0x6
93 #define RFCOMM_RPN_PARITY_SPACE 0x7
94
95 #define RFCOMM_RPN_FLOW_NONE    0x00
96
97 #define RFCOMM_RPN_XON_CHAR     0x11
98 #define RFCOMM_RPN_XOFF_CHAR    0x13
99
100 #define RFCOMM_RPN_PM_BITRATE           0x0001
101 #define RFCOMM_RPN_PM_DATA              0x0002
102 #define RFCOMM_RPN_PM_STOP              0x0004
103 #define RFCOMM_RPN_PM_PARITY            0x0008
104 #define RFCOMM_RPN_PM_PARITY_TYPE       0x0010
105 #define RFCOMM_RPN_PM_XON               0x0020
106 #define RFCOMM_RPN_PM_XOFF              0x0040
107 #define RFCOMM_RPN_PM_FLOW              0x3F00
108
109 #define RFCOMM_RPN_PM_ALL               0x3F7F
110
111 struct rfcomm_hdr {
112         u8 addr;
113         u8 ctrl;
114         u8 len;    // Actual size can be 2 bytes
115 } __attribute__ ((packed));
116
117 struct rfcomm_cmd {
118         u8 addr;
119         u8 ctrl;
120         u8 len;
121         u8 fcs;
122 } __attribute__ ((packed));
123
124 struct rfcomm_mcc {
125         u8 type;
126         u8 len;
127 } __attribute__ ((packed));
128
129 struct rfcomm_pn {
130         u8  dlci;
131         u8  flow_ctrl;
132         u8  priority;
133         u8  ack_timer;
134         u16 mtu;
135         u8  max_retrans;
136         u8  credits;
137 } __attribute__ ((packed));
138
139 struct rfcomm_rpn {
140         u8  dlci;
141         u8  bit_rate;
142         u8  line_settings;
143         u8  flow_ctrl;
144         u8  xon_char;
145         u8  xoff_char;
146         u16 param_mask;
147 } __attribute__ ((packed));
148
149 struct rfcomm_rls {
150         u8  dlci;
151         u8  status;
152 } __attribute__ ((packed));
153
154 struct rfcomm_msc {
155         u8  dlci;
156         u8  v24_sig;
157 } __attribute__ ((packed));
158
159 /* ---- Core structures, flags etc ---- */
160
161 struct rfcomm_session {
162         struct list_head list;
163         struct socket   *sock;
164         unsigned long    state;
165         unsigned long    flags;
166         atomic_t         refcnt;
167         int              initiator;
168
169         /* Default DLC parameters */
170         int    cfc;
171         uint   mtu;
172
173         struct list_head dlcs;
174 };
175
176 struct rfcomm_dlc {
177         struct list_head      list;
178         struct rfcomm_session *session;
179         struct sk_buff_head   tx_queue;
180         struct timer_list     timer;
181
182         spinlock_t    lock;
183         unsigned long state;
184         unsigned long flags;
185         atomic_t      refcnt;
186         u8            dlci;
187         u8            addr;
188         u8            priority;
189         u8            v24_sig;
190         u8            mscex;
191
192         uint          mtu;
193         uint          cfc;
194         uint          rx_credits;
195         uint          tx_credits;
196
197         void          *owner;
198
199         void (*data_ready)(struct rfcomm_dlc *d, struct sk_buff *skb);
200         void (*state_change)(struct rfcomm_dlc *d, int err);
201         void (*modem_status)(struct rfcomm_dlc *d, u8 v24_sig);
202 };
203
204 /* DLC and session flags */
205 #define RFCOMM_RX_THROTTLED 0
206 #define RFCOMM_TX_THROTTLED 1
207 #define RFCOMM_MSC_PENDING  2
208 #define RFCOMM_TIMED_OUT    3
209
210 /* Scheduling flags and events */
211 #define RFCOMM_SCHED_STATE  0
212 #define RFCOMM_SCHED_RX     1
213 #define RFCOMM_SCHED_TX     2
214 #define RFCOMM_SCHED_TIMEO  3
215 #define RFCOMM_SCHED_WAKEUP 31
216
217 /* MSC exchange flags */
218 #define RFCOMM_MSCEX_TX     1
219 #define RFCOMM_MSCEX_RX     2
220 #define RFCOMM_MSCEX_OK     (RFCOMM_MSCEX_TX + RFCOMM_MSCEX_RX)
221
222 /* CFC states */
223 #define RFCOMM_CFC_UNKNOWN  -1
224 #define RFCOMM_CFC_DISABLED 0
225 #define RFCOMM_CFC_ENABLED  RFCOMM_MAX_CREDITS
226
227 extern struct task_struct *rfcomm_thread;
228 extern unsigned long rfcomm_event;
229
230 static inline void rfcomm_schedule(uint event)
231 {
232         if (!rfcomm_thread)
233                 return;
234         //set_bit(event, &rfcomm_event);
235         set_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event);
236         wake_up_process(rfcomm_thread);
237 }
238
239 extern struct semaphore rfcomm_sem;
240 #define rfcomm_lock()   down(&rfcomm_sem);
241 #define rfcomm_unlock() up(&rfcomm_sem);
242
243 /* ---- RFCOMM DLCs (channels) ---- */
244 struct rfcomm_dlc *rfcomm_dlc_alloc(int prio);
245 void rfcomm_dlc_free(struct rfcomm_dlc *d);
246 int  rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel);
247 int  rfcomm_dlc_close(struct rfcomm_dlc *d, int reason);
248 int  rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb);
249 int  rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig);
250 int  rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig);
251
252 #define rfcomm_dlc_lock(d)     spin_lock(&d->lock)
253 #define rfcomm_dlc_unlock(d)   spin_unlock(&d->lock)
254
255 static inline void rfcomm_dlc_hold(struct rfcomm_dlc *d)
256 {
257         atomic_inc(&d->refcnt);
258 }
259
260 static inline void rfcomm_dlc_put(struct rfcomm_dlc *d)
261 {
262         if (atomic_dec_and_test(&d->refcnt))
263                 rfcomm_dlc_free(d);
264 }
265
266 extern void FASTCALL(__rfcomm_dlc_throttle(struct rfcomm_dlc *d));
267 extern void FASTCALL(__rfcomm_dlc_unthrottle(struct rfcomm_dlc *d));
268
269 static inline void rfcomm_dlc_throttle(struct rfcomm_dlc *d)
270 {
271         if (!test_and_set_bit(RFCOMM_RX_THROTTLED, &d->flags))
272                 __rfcomm_dlc_throttle(d);
273 }
274
275 static inline void rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
276 {
277         if (test_and_clear_bit(RFCOMM_RX_THROTTLED, &d->flags))
278                 __rfcomm_dlc_unthrottle(d);
279 }
280
281 /* ---- RFCOMM sessions ---- */
282 struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state);
283 struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);
284 struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, bdaddr_t *dst, int *err);
285 void   rfcomm_session_del(struct rfcomm_session *s);
286 void   rfcomm_session_close(struct rfcomm_session *s, int err);
287 void   rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst);
288
289 static inline void rfcomm_session_hold(struct rfcomm_session *s)
290 {
291         atomic_inc(&s->refcnt);
292 }
293
294 static inline void rfcomm_session_put(struct rfcomm_session *s)
295 {
296         if (atomic_dec_and_test(&s->refcnt))
297                 rfcomm_session_del(s);
298 }
299
300 /* ---- RFCOMM chechsum ---- */
301 extern u8 rfcomm_crc_table[];
302
303 /* ---- RFCOMM sockets ---- */
304 struct sockaddr_rc {
305         sa_family_t rc_family;
306         bdaddr_t    rc_bdaddr;
307         u8          rc_channel;
308 };
309
310 #define rfcomm_pi(sk)   ((struct rfcomm_pinfo *)sk->sk_protinfo)
311
312 struct rfcomm_pinfo {
313         struct rfcomm_dlc   *dlc;
314         u8     channel;
315 };
316
317 int  rfcomm_init_sockets(void);
318 void rfcomm_cleanup_sockets(void);
319
320 int  rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d);
321
322 /* ---- RFCOMM TTY ---- */
323 #define RFCOMM_MAX_DEV  256
324
325 #define RFCOMMCREATEDEV         _IOW('R', 200, int)
326 #define RFCOMMRELEASEDEV        _IOW('R', 201, int)
327 #define RFCOMMGETDEVLIST        _IOR('R', 210, int)
328 #define RFCOMMGETDEVINFO        _IOR('R', 211, int)
329 #define RFCOMMSTEALDLC          _IOW('R', 220, int)
330
331 #define RFCOMM_REUSE_DLC      0
332 #define RFCOMM_RELEASE_ONHUP  1
333 #define RFCOMM_HANGUP_NOW     2
334 #define RFCOMM_TTY_ATTACHED   3
335
336 struct rfcomm_dev_req {
337         s16      dev_id;
338         u32      flags;
339         bdaddr_t src;
340         bdaddr_t dst;
341         u8       channel;
342         
343 };
344
345 struct rfcomm_dev_info {
346         s16      id;
347         u32      flags;
348         u16      state;
349         bdaddr_t src;
350         bdaddr_t dst;
351         u8       channel;
352 };
353
354 struct rfcomm_dev_list_req {
355         u16      dev_num;
356         struct   rfcomm_dev_info dev_info[0];
357 };
358
359 int  rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg);
360 int  rfcomm_init_ttys(void);
361 void rfcomm_cleanup_ttys(void);
362
363 extern struct proc_dir_entry *proc_bt_rfcomm;
364
365 #endif /* __RFCOMM_H */