Virtio_Balloon implementation for DragonFly
[dragonfly.git] / sys / netbt / rfcomm.h
1 /* $DragonFly: src/sys/netbt/rfcomm.h,v 1.2 2008/03/18 13:41:42 hasso Exp $ */
2 /* $OpenBSD: src/sys/netbt/rfcomm.h,v 1.2 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: rfcomm.h,v 1.6 2007/11/20 20:25:58 plunky Exp $ */
4
5 /*-
6  * Copyright (c) 2006 Itronix Inc.
7  * All rights reserved.
8  *
9  * Written by Iain Hibbert for Itronix Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of Itronix Inc. may not be used to endorse
20  *    or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*-
36  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  * $Id: rfcomm.h,v 1.6 2007/11/20 20:25:58 plunky Exp $
61  * $FreeBSD: src/sys/netgraph/bluetooth/include/ng_btsocket_rfcomm.h,v 1.4 2005/01/11 01:39:53 emax Exp $
62  */
63
64 #ifndef _NETBT_RFCOMM_H_
65 #define _NETBT_RFCOMM_H_
66
67 #include <sys/types.h>
68
69 /*************************************************************************
70  *************************************************************************
71  **                             RFCOMM                                  **
72  *************************************************************************
73  *************************************************************************/
74
75 #define RFCOMM_MTU_MAX                  32767
76 #define RFCOMM_MTU_MIN                  23
77 #define RFCOMM_MTU_DEFAULT              127
78
79 #define RFCOMM_CREDITS_MAX              255     /* in any single packet */
80 #define RFCOMM_CREDITS_DEFAULT          7       /* default initial value */
81
82 #define RFCOMM_CHANNEL_ANY              0
83 #define RFCOMM_CHANNEL_MIN              1
84 #define RFCOMM_CHANNEL_MAX              30
85
86 /* RFCOMM frame types */
87 #define RFCOMM_FRAME_SABM               0x2f
88 #define RFCOMM_FRAME_DISC               0x43
89 #define RFCOMM_FRAME_UA                 0x63
90 #define RFCOMM_FRAME_DM                 0x0f
91 #define RFCOMM_FRAME_UIH                0xef
92
93 /* RFCOMM MCC commands */
94 #define RFCOMM_MCC_TEST                 0x08    /* Test */
95 #define RFCOMM_MCC_FCON                 0x28    /* Flow Control on */
96 #define RFCOMM_MCC_FCOFF                0x18    /* Flow Control off */
97 #define RFCOMM_MCC_MSC                  0x38    /* Modem Status Command */
98 #define RFCOMM_MCC_RPN                  0x24    /* Remote Port Negotiation */
99 #define RFCOMM_MCC_RLS                  0x14    /* Remote Line Status */
100 #define RFCOMM_MCC_PN                   0x20    /* Port Negotiation */
101 #define RFCOMM_MCC_NSC                  0x04    /* Non Supported Command */
102
103 /* RFCOMM modem signals */
104 #define RFCOMM_MSC_FC                   0x02    /* Flow Control asserted */
105 #define RFCOMM_MSC_RTC                  0x04    /* Ready To Communicate */
106 #define RFCOMM_MSC_RTR                  0x08    /* Ready To Receive */
107 #define RFCOMM_MSC_IC                   0x40    /* Incomming Call (RING) */
108 #define RFCOMM_MSC_DV                   0x80    /* Data Valid */
109
110 /* RPN parameters - baud rate */
111 #define RFCOMM_RPN_BR_2400              0x0
112 #define RFCOMM_RPN_BR_4800              0x1
113 #define RFCOMM_RPN_BR_7200              0x2
114 #define RFCOMM_RPN_BR_9600              0x3
115 #define RFCOMM_RPN_BR_19200             0x4
116 #define RFCOMM_RPN_BR_38400             0x5
117 #define RFCOMM_RPN_BR_57600             0x6
118 #define RFCOMM_RPN_BR_115200            0x7
119 #define RFCOMM_RPN_BR_230400            0x8
120
121 /* RPN parameters - data bits */
122 #define RFCOMM_RPN_DATA_5               0x0
123 #define RFCOMM_RPN_DATA_6               0x1
124 #define RFCOMM_RPN_DATA_7               0x2
125 #define RFCOMM_RPN_DATA_8               0x3
126
127 /* RPN parameters - stop bit */
128 #define RFCOMM_RPN_STOP_1               0
129 #define RFCOMM_RPN_STOP_15              1
130
131 /* RPN parameters - parity enable */
132 #define RFCOMM_RPN_PARITY_NONE          0x0
133
134 /* RPN parameters - parity type */
135 #define RFCOMM_RPN_PARITY_ODD           0x0
136 #define RFCOMM_RPN_PARITY_EVEN          0x1
137 #define RFCOMM_RPN_PARITY_MARK          0x2
138 #define RFCOMM_RPN_PARITY_SPACE         0x3
139
140 /* RPN parameters - default line_setting */
141 #define RFCOMM_RPN_8_N_1                0x03
142
143 /* RPN parameters - flow control */
144 #define RFCOMM_RPN_XON_CHAR             0x11
145 #define RFCOMM_RPN_XOFF_CHAR            0x13
146 #define RFCOMM_RPN_FLOW_NONE            0x00
147
148 /* RPN parameters - mask */
149 #define RFCOMM_RPN_PM_RATE              0x0001
150 #define RFCOMM_RPN_PM_DATA              0x0002
151 #define RFCOMM_RPN_PM_STOP              0x0004
152 #define RFCOMM_RPN_PM_PARITY            0x0008
153 #define RFCOMM_RPN_PM_PTYPE             0x0010
154 #define RFCOMM_RPN_PM_XON               0x0020
155 #define RFCOMM_RPN_PM_XOFF              0x0040
156
157 #define RFCOMM_RPN_PM_FLOW              0x3f00
158
159 #define RFCOMM_RPN_PM_ALL               0x3f7f
160
161 /* RFCOMM command frame header */
162 struct rfcomm_cmd_hdr
163 {
164         uint8_t         address;
165         uint8_t         control;
166         uint8_t         length;
167         uint8_t         fcs;
168 } __attribute__ ((__packed__));
169
170 /* RFCOMM MSC command */
171 struct rfcomm_mcc_msc
172 {
173         uint8_t         address;
174         uint8_t         modem;
175         uint8_t         brk;
176 } __attribute__ ((__packed__));
177
178 /* RFCOMM RPN command */
179 struct rfcomm_mcc_rpn
180 {
181         uint8_t         dlci;
182         uint8_t         bit_rate;
183         uint8_t         line_settings;
184         uint8_t         flow_control;
185         uint8_t         xon_char;
186         uint8_t         xoff_char;
187         uint16_t        param_mask;
188 } __attribute__ ((__packed__));
189
190 /* RFCOMM RLS command */
191 struct rfcomm_mcc_rls
192 {
193         uint8_t         address;
194         uint8_t         status;
195 } __attribute__ ((__packed__));
196
197 /* RFCOMM PN command */
198 struct rfcomm_mcc_pn
199 {
200         uint8_t         dlci;
201         uint8_t         flow_control;
202         uint8_t         priority;
203         uint8_t         ack_timer;
204         uint16_t        mtu;
205         uint8_t         max_retrans;
206         uint8_t         credits;
207 } __attribute__ ((__packed__));
208
209 /* RFCOMM frame parsing macros */
210 #define RFCOMM_DLCI(b)                  (((b) & 0xfc) >> 2)
211 #define RFCOMM_TYPE(b)                  (((b) & 0xef))
212
213 #define RFCOMM_EA(b)                    (((b) & 0x01))
214 #define RFCOMM_CR(b)                    (((b) & 0x02) >> 1)
215 #define RFCOMM_PF(b)                    (((b) & 0x10) >> 4)
216
217 #define RFCOMM_CHANNEL(dlci)            (((dlci) >> 1) & 0x2f)
218 #define RFCOMM_DIRECTION(dlci)          ((dlci) & 0x1)
219
220 #define RFCOMM_MKADDRESS(cr, dlci) \
221         ((((dlci) & 0x3f) << 2) | ((cr) << 1) | 0x01)
222
223 #define RFCOMM_MKCONTROL(type, pf)      ((((type) & 0xef) | ((pf) << 4)))
224 #define RFCOMM_MKDLCI(dir, channel)     ((((channel) & 0x1f) << 1) | (dir))
225
226 /* RFCOMM MCC macros */
227 #define RFCOMM_MCC_TYPE(b)              (((b) & 0xfc) >> 2)
228 #define RFCOMM_MCC_LENGTH(b)            (((b) & 0xfe) >> 1)
229 #define RFCOMM_MKMCC_TYPE(cr, type)     ((((type) << 2) | ((cr) << 1) | 0x01))
230
231 /* RPN macros */
232 #define RFCOMM_RPN_DATA_BITS(line)      ((line) & 0x3)
233 #define RFCOMM_RPN_STOP_BITS(line)      (((line) >> 2) & 0x1)
234 #define RFCOMM_RPN_PARITY(line)         (((line) >> 3) & 0x1)
235
236 /*************************************************************************
237  *************************************************************************
238  **                     SOCK_STREAM RFCOMM sockets                      **
239  *************************************************************************
240  *************************************************************************/
241
242 /* Socket options */
243 #define SO_RFCOMM_MTU           1       /* mtu */
244 #define SO_RFCOMM_FC_INFO       2       /* flow control info (below) */
245 #define SO_RFCOMM_LM            3       /* link mode */
246
247 /* Flow control information */
248 struct rfcomm_fc_info {
249         uint8_t         lmodem;         /* modem signals (local) */
250         uint8_t         rmodem;         /* modem signals (remote) */
251         uint8_t         tx_cred;        /* TX credits */
252         uint8_t         rx_cred;        /* RX credits */
253         uint8_t         cfc;            /* credit flow control */
254         uint8_t         reserved;
255 };
256
257 /* RFCOMM link mode flags */
258 #define RFCOMM_LM_AUTH          (1<<0)  /* want authentication */
259 #define RFCOMM_LM_ENCRYPT       (1<<1)  /* want encryption */
260 #define RFCOMM_LM_SECURE        (1<<2)  /* want secured link */
261
262 #ifdef _KERNEL
263
264 /* sysctl variables */
265 extern int rfcomm_sendspace;
266 extern int rfcomm_recvspace;
267 extern int rfcomm_mtu_default;
268 extern int rfcomm_ack_timeout;
269 extern int rfcomm_mcc_timeout;
270
271 /*
272  * Bluetooth RFCOMM session data
273  * One L2CAP connection == one RFCOMM session
274  */
275
276 /* Credit note */
277 struct rfcomm_credit {
278         struct rfcomm_dlc               *rc_dlc;        /* owner */
279         uint16_t                         rc_len;        /* length */
280         STAILQ_ENTRY(rfcomm_credit)      rc_next;       /* next credit */
281 };
282
283 /* RFCOMM session data (one L2CAP channel) */
284 struct rfcomm_session {
285         struct l2cap_channel            *rs_l2cap;      /* L2CAP pointer */
286         uint16_t                         rs_flags;      /* session flags */
287         uint16_t                         rs_state;      /* session state */
288         uint16_t                         rs_mtu;        /* default MTU */
289
290         STAILQ_HEAD(,rfcomm_credit)      rs_credits;    /* credit notes */
291         LIST_HEAD(,rfcomm_dlc)           rs_dlcs;       /* DLC list */
292
293         struct callout                   rs_timeout;    /* timeout */
294
295         LIST_ENTRY(rfcomm_session)       rs_next;       /* next session */
296 };
297
298 LIST_HEAD(rfcomm_session_list, rfcomm_session);
299 extern struct rfcomm_session_list rfcomm_session_active;
300 extern struct rfcomm_session_list rfcomm_session_listen;
301
302 /* Session state */
303 #define RFCOMM_SESSION_CLOSED           0
304 #define RFCOMM_SESSION_WAIT_CONNECT     1
305 #define RFCOMM_SESSION_OPEN             2
306 #define RFCOMM_SESSION_WAIT_DISCONNECT  3
307 #define RFCOMM_SESSION_LISTEN           4
308
309 /* Session flags */
310 #define RFCOMM_SESSION_INITIATOR        (1 << 0) /* we are initiator */
311 #define RFCOMM_SESSION_CFC              (1 << 1) /* credit flow control */
312 #define RFCOMM_SESSION_LFC              (1 << 2) /* local flow control */
313 #define RFCOMM_SESSION_RFC              (1 << 3) /* remote flow control */
314 #define RFCOMM_SESSION_FREE             (1 << 4) /* self lock out for free */
315
316 #define IS_INITIATOR(rs)        ((rs)->rs_flags & RFCOMM_SESSION_INITIATOR)
317
318 /* Bluetooth RFCOMM DLC data (connection) */
319 struct rfcomm_dlc {
320         struct rfcomm_session   *rd_session; /* RFCOMM session */
321         uint8_t                  rd_dlci;    /* RFCOMM DLCI */
322
323         uint16_t                 rd_flags;   /* DLC flags */
324         uint16_t                 rd_state;   /* DLC state */
325         uint16_t                 rd_mtu;     /* MTU */
326         int                      rd_mode;    /* link mode */
327
328         struct sockaddr_bt       rd_laddr;   /* local address */
329         struct sockaddr_bt       rd_raddr;   /* remote address */
330
331         uint8_t                  rd_lmodem;  /* local modem signls */
332         uint8_t                  rd_rmodem;  /* remote modem signals */
333
334         int                      rd_rxcred;  /* receive credits (sent) */
335         size_t                   rd_rxsize;  /* receive buffer (bytes, avail) */
336         int                      rd_txcred;  /* transmit credits (unused) */
337         int                      rd_pending; /* packets sent but not complete */
338
339         struct callout           rd_timeout; /* timeout */
340         struct mbuf             *rd_txbuf;   /* transmit buffer */
341
342         const struct btproto    *rd_proto;   /* upper layer callbacks */
343         void                    *rd_upper;   /* upper layer argument */
344
345         LIST_ENTRY(rfcomm_dlc)   rd_next;    /* next dlc on session */
346 };
347
348 /*
349  * Credit Flow Control works in the following way.
350  *
351  * txcred is how many packets we can send. Received credit
352  * is added to this value, and it is decremented each time
353  * we send a packet.
354  *
355  * rxsize is the number of bytes that are available in the
356  * upstream receive buffer.
357  *
358  * rxcred is the number of credits that we have previously
359  * sent that are still unused. This value will be decreased
360  * for each packet we receive and we will add to it when we
361  * send credits. We calculate the amount of credits to send
362  * by the cunning formula "(space / mtu) - sent" so that if
363  * we get a bunch of small packets, we can continue sending
364  * credits without risking buffer overflow.
365  */
366
367 /* DLC flags */
368 #define RFCOMM_DLC_DETACH               (1 << 0) /* DLC to be detached */
369 #define RFCOMM_DLC_SHUTDOWN             (1 << 1) /* DLC to be shutdown */
370
371 /* DLC state */
372 #define RFCOMM_DLC_CLOSED               0       /* no session */
373 #define RFCOMM_DLC_WAIT_SESSION         1       /* waiting for session */
374 #define RFCOMM_DLC_WAIT_CONNECT         2       /* waiting for connect */
375 #define RFCOMM_DLC_WAIT_SEND_SABM       3       /* waiting to send SABM */
376 #define RFCOMM_DLC_WAIT_SEND_UA         4       /* waiting to send UA */
377 #define RFCOMM_DLC_WAIT_RECV_UA         5       /* waiting to receive UA */
378 #define RFCOMM_DLC_OPEN                 6       /* can send/receive */
379 #define RFCOMM_DLC_WAIT_DISCONNECT      7       /* waiting for disconnect */
380 #define RFCOMM_DLC_LISTEN               8       /* listening DLC */
381
382 /*
383  * Bluetooth RFCOMM socket kernel prototypes
384  */
385
386 extern struct pr_usrreqs rfcomm_usrreqs;
387 struct socket;
388 union netmsg;
389
390 /* rfcomm_dlc.c */
391 struct rfcomm_dlc *rfcomm_dlc_lookup(struct rfcomm_session *, int);
392 struct rfcomm_dlc *rfcomm_dlc_newconn(struct rfcomm_session *, int);
393 void rfcomm_dlc_close(struct rfcomm_dlc *, int);
394 void rfcomm_dlc_timeout(void *);
395 int rfcomm_dlc_setmode(struct rfcomm_dlc *);
396 int rfcomm_dlc_connect(struct rfcomm_dlc *);
397 int rfcomm_dlc_open(struct rfcomm_dlc *);
398 void rfcomm_dlc_start(struct rfcomm_dlc *);
399
400 /* rfcomm_session.c */
401 void rfcomm_init(void);
402 struct rfcomm_session *rfcomm_session_alloc(struct rfcomm_session_list *, struct sockaddr_bt *);
403 struct rfcomm_session *rfcomm_session_lookup(struct sockaddr_bt *, struct sockaddr_bt *);
404 void rfcomm_session_free(struct rfcomm_session *);
405 int rfcomm_session_send_frame(struct rfcomm_session *, int, int);
406 int rfcomm_session_send_uih(struct rfcomm_session *, struct rfcomm_dlc *, int, struct mbuf *);
407 int rfcomm_session_send_mcc(struct rfcomm_session *, int, uint8_t, void *, int);
408 void rfcomm_session_timeout(void *);
409
410 /* rfcomm_socket.c */
411 int rfcomm_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *);
412 void rfcomm_ctloutput(union netmsg *);
413
414 /* rfcomm_upper.c */
415 int rfcomm_attach(struct rfcomm_dlc **, const struct btproto *, void *);
416 int rfcomm_bind(struct rfcomm_dlc *, struct sockaddr_bt *);
417 int rfcomm_sockaddr(struct rfcomm_dlc *, struct sockaddr_bt *);
418 int rfcomm_connect(struct rfcomm_dlc *, struct sockaddr_bt *);
419 int rfcomm_peeraddr(struct rfcomm_dlc *, struct sockaddr_bt *);
420 int rfcomm_disconnect(struct rfcomm_dlc *, int);
421 int rfcomm_detach(struct rfcomm_dlc **);
422 int rfcomm_listen(struct rfcomm_dlc *);
423 int rfcomm_send(struct rfcomm_dlc *, struct mbuf *);
424 int rfcomm_rcvd(struct rfcomm_dlc *, size_t);
425 int rfcomm_setopt(struct rfcomm_dlc *, int, void *);
426 int rfcomm_setopt2(struct rfcomm_dlc *, int, struct socket *, struct sockopt *);
427 int rfcomm_getopt(struct rfcomm_dlc *, int, void *);
428
429 extern vm_zone_t rfcomm_credit_pool;
430 #endif /* _KERNEL */
431
432 #endif /* _NETBT_RFCOMM_H_ */