xref: /dragonfly/sys/netbt/rfcomm.h (revision 0cfebe3d)
1 /* $OpenBSD: rfcomm.h,v 1.1 2007/06/01 02:46:11 uwe Exp $ */
2 /* $NetBSD: rfcomm.h,v 1.3 2007/04/21 06:15:23 plunky Exp $ */
3 /* $DragonFly: src/sys/netbt/rfcomm.h,v 1.1 2007/12/30 20:02:56 hasso 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.1.1.1 2007/11/20 11:56:11 griffin 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_MIN		1
83 #define RFCOMM_CHANNEL_MAX		30
84 
85 /* RFCOMM frame types */
86 #define RFCOMM_FRAME_SABM		0x2f
87 #define RFCOMM_FRAME_DISC		0x43
88 #define RFCOMM_FRAME_UA			0x63
89 #define RFCOMM_FRAME_DM			0x0f
90 #define RFCOMM_FRAME_UIH		0xef
91 
92 /* RFCOMM MCC commands */
93 #define RFCOMM_MCC_TEST			0x08	/* Test */
94 #define RFCOMM_MCC_FCON			0x28	/* Flow Control on */
95 #define RFCOMM_MCC_FCOFF		0x18	/* Flow Control off */
96 #define RFCOMM_MCC_MSC			0x38	/* Modem Status Command */
97 #define RFCOMM_MCC_RPN			0x24	/* Remote Port Negotiation */
98 #define RFCOMM_MCC_RLS			0x14	/* Remote Line Status */
99 #define RFCOMM_MCC_PN			0x20	/* Port Negotiation */
100 #define RFCOMM_MCC_NSC			0x04	/* Non Supported Command */
101 
102 /* RFCOMM modem signals */
103 #define RFCOMM_MSC_FC			0x02	/* Flow Control asserted */
104 #define RFCOMM_MSC_RTC			0x04	/* Ready To Communicate */
105 #define RFCOMM_MSC_RTR			0x08	/* Ready To Receive */
106 #define RFCOMM_MSC_IC			0x40	/* Incomming Call (RING) */
107 #define RFCOMM_MSC_DV			0x80	/* Data Valid */
108 
109 /* RPN parameters - baud rate */
110 #define RFCOMM_RPN_BR_2400		0x0
111 #define RFCOMM_RPN_BR_4800		0x1
112 #define RFCOMM_RPN_BR_7200		0x2
113 #define RFCOMM_RPN_BR_9600		0x3
114 #define RFCOMM_RPN_BR_19200		0x4
115 #define RFCOMM_RPN_BR_38400		0x5
116 #define RFCOMM_RPN_BR_57600		0x6
117 #define RFCOMM_RPN_BR_115200		0x7
118 #define RFCOMM_RPN_BR_230400		0x8
119 
120 /* RPN parameters - data bits */
121 #define RFCOMM_RPN_DATA_5		0x0
122 #define RFCOMM_RPN_DATA_6		0x1
123 #define RFCOMM_RPN_DATA_7		0x2
124 #define RFCOMM_RPN_DATA_8		0x3
125 
126 /* RPN parameters - stop bit */
127 #define RFCOMM_RPN_STOP_1		0
128 #define RFCOMM_RPN_STOP_15		1
129 
130 /* RPN parameters - parity enable */
131 #define RFCOMM_RPN_PARITY_NONE		0x0
132 
133 /* RPN parameters - parity type */
134 #define RFCOMM_RPN_PARITY_ODD		0x0
135 #define RFCOMM_RPN_PARITY_EVEN		0x1
136 #define RFCOMM_RPN_PARITY_MARK		0x2
137 #define RFCOMM_RPN_PARITY_SPACE		0x3
138 
139 /* RPN parameters - default line_setting */
140 #define RFCOMM_RPN_8_N_1		0x03
141 
142 /* RPN parameters - flow control */
143 #define RFCOMM_RPN_XON_CHAR		0x11
144 #define RFCOMM_RPN_XOFF_CHAR		0x13
145 #define RFCOMM_RPN_FLOW_NONE		0x00
146 
147 /* RPN parameters - mask */
148 #define RFCOMM_RPN_PM_RATE		0x0001
149 #define RFCOMM_RPN_PM_DATA		0x0002
150 #define RFCOMM_RPN_PM_STOP		0x0004
151 #define RFCOMM_RPN_PM_PARITY		0x0008
152 #define RFCOMM_RPN_PM_PTYPE		0x0010
153 #define RFCOMM_RPN_PM_XON		0x0020
154 #define RFCOMM_RPN_PM_XOFF		0x0040
155 
156 #define RFCOMM_RPN_PM_FLOW		0x3f00
157 
158 #define RFCOMM_RPN_PM_ALL		0x3f7f
159 
160 /* RFCOMM command frame header */
161 struct rfcomm_cmd_hdr
162 {
163 	uint8_t		address;
164 	uint8_t		control;
165 	uint8_t		length;
166 	uint8_t		fcs;
167 } __attribute__ ((__packed__));
168 
169 /* RFCOMM MSC command */
170 struct rfcomm_mcc_msc
171 {
172 	uint8_t		address;
173 	uint8_t		modem;
174 	uint8_t		brk;
175 } __attribute__ ((__packed__));
176 
177 /* RFCOMM RPN command */
178 struct rfcomm_mcc_rpn
179 {
180 	uint8_t		dlci;
181 	uint8_t		bit_rate;
182 	uint8_t		line_settings;
183 	uint8_t		flow_control;
184 	uint8_t		xon_char;
185 	uint8_t		xoff_char;
186 	uint16_t	param_mask;
187 } __attribute__ ((__packed__));
188 
189 /* RFCOMM RLS command */
190 struct rfcomm_mcc_rls
191 {
192 	uint8_t		address;
193 	uint8_t		status;
194 } __attribute__ ((__packed__));
195 
196 /* RFCOMM PN command */
197 struct rfcomm_mcc_pn
198 {
199 	uint8_t		dlci;
200 	uint8_t		flow_control;
201 	uint8_t		priority;
202 	uint8_t		ack_timer;
203 	uint16_t	mtu;
204 	uint8_t		max_retrans;
205 	uint8_t		credits;
206 } __attribute__ ((__packed__));
207 
208 /* RFCOMM frame parsing macros */
209 #define RFCOMM_DLCI(b)			(((b) & 0xfc) >> 2)
210 #define RFCOMM_TYPE(b)			(((b) & 0xef))
211 
212 #define RFCOMM_EA(b)			(((b) & 0x01))
213 #define RFCOMM_CR(b)			(((b) & 0x02) >> 1)
214 #define RFCOMM_PF(b)			(((b) & 0x10) >> 4)
215 
216 #define RFCOMM_CHANNEL(dlci)		(((dlci) >> 1) & 0x2f)
217 #define RFCOMM_DIRECTION(dlci)		((dlci) & 0x1)
218 
219 #define RFCOMM_MKADDRESS(cr, dlci) \
220 	((((dlci) & 0x3f) << 2) | ((cr) << 1) | 0x01)
221 
222 #define RFCOMM_MKCONTROL(type, pf)	((((type) & 0xef) | ((pf) << 4)))
223 #define RFCOMM_MKDLCI(dir, channel)	((((channel) & 0x1f) << 1) | (dir))
224 
225 /* RFCOMM MCC macros */
226 #define RFCOMM_MCC_TYPE(b)		(((b) & 0xfc) >> 2)
227 #define RFCOMM_MCC_LENGTH(b)		(((b) & 0xfe) >> 1)
228 #define RFCOMM_MKMCC_TYPE(cr, type)	((((type) << 2) | ((cr) << 1) | 0x01))
229 
230 /* RPN macros */
231 #define RFCOMM_RPN_DATA_BITS(line)	((line) & 0x3)
232 #define RFCOMM_RPN_STOP_BITS(line)	(((line) >> 2) & 0x1)
233 #define RFCOMM_RPN_PARITY(line)		(((line) >> 3) & 0x1)
234 
235 /*************************************************************************
236  *************************************************************************
237  **			SOCK_STREAM RFCOMM sockets			**
238  *************************************************************************
239  *************************************************************************/
240 
241 /* Socket options */
242 #define SO_RFCOMM_MTU		1	/* mtu */
243 #define SO_RFCOMM_FC_INFO	2	/* flow control info (below) */
244 #define SO_RFCOMM_LM		3	/* link mode */
245 
246 /* Flow control information */
247 struct rfcomm_fc_info {
248 	uint8_t		lmodem;		/* modem signals (local) */
249 	uint8_t		rmodem;		/* modem signals (remote) */
250 	uint8_t		tx_cred;	/* TX credits */
251 	uint8_t		rx_cred;	/* RX credits */
252 	uint8_t		cfc;		/* credit flow control */
253 	uint8_t		reserved;
254 };
255 
256 /* RFCOMM link mode flags */
257 #define RFCOMM_LM_AUTH		(1<<0)	/* want authentication */
258 #define RFCOMM_LM_ENCRYPT	(1<<1)	/* want encryption */
259 #define RFCOMM_LM_SECURE	(1<<2)	/* want secured link */
260 
261 #ifdef _KERNEL
262 
263 /* sysctl variables */
264 extern int rfcomm_sendspace;
265 extern int rfcomm_recvspace;
266 extern int rfcomm_mtu_default;
267 extern int rfcomm_ack_timeout;
268 extern int rfcomm_mcc_timeout;
269 
270 /*
271  * Bluetooth RFCOMM session data
272  * One L2CAP connection == one RFCOMM session
273  */
274 
275 /* Credit note */
276 struct rfcomm_credit {
277 	struct rfcomm_dlc		*rc_dlc;	/* owner */
278 	uint16_t			 rc_len;	/* length */
279 	STAILQ_ENTRY(rfcomm_credit)	 rc_next;	/* next credit */
280 };
281 
282 /* RFCOMM session data (one L2CAP channel) */
283 struct rfcomm_session {
284 	struct l2cap_channel		*rs_l2cap;	/* L2CAP pointer */
285 	uint16_t			 rs_flags;	/* session flags */
286 	uint16_t			 rs_state;	/* session state */
287 	uint16_t			 rs_mtu;	/* default MTU */
288 
289 	STAILQ_HEAD(,rfcomm_credit)	 rs_credits;	/* credit notes */
290 	LIST_HEAD(,rfcomm_dlc)		 rs_dlcs;	/* DLC list */
291 
292 	struct callout			 rs_timeout;	/* timeout */
293 
294 	LIST_ENTRY(rfcomm_session)	 rs_next;	/* next session */
295 };
296 
297 LIST_HEAD(rfcomm_session_list, rfcomm_session);
298 extern struct rfcomm_session_list rfcomm_session_active;
299 extern struct rfcomm_session_list rfcomm_session_listen;
300 
301 /* Session state */
302 #define RFCOMM_SESSION_CLOSED		0
303 #define RFCOMM_SESSION_WAIT_CONNECT	1
304 #define RFCOMM_SESSION_OPEN		2
305 #define RFCOMM_SESSION_WAIT_DISCONNECT	3
306 #define RFCOMM_SESSION_LISTEN		4
307 
308 /* Session flags */
309 #define RFCOMM_SESSION_INITIATOR	(1 << 0) /* we are initiator */
310 #define RFCOMM_SESSION_CFC		(1 << 1) /* credit flow control */
311 #define RFCOMM_SESSION_LFC		(1 << 2) /* local flow control */
312 #define RFCOMM_SESSION_RFC		(1 << 3) /* remote flow control */
313 #define RFCOMM_SESSION_FREE		(1 << 4) /* self lock out for free */
314 
315 #define IS_INITIATOR(rs)	((rs)->rs_flags & RFCOMM_SESSION_INITIATOR)
316 
317 /* Bluetooth RFCOMM DLC data (connection) */
318 struct rfcomm_dlc {
319 	struct rfcomm_session	*rd_session; /* RFCOMM session */
320 	uint8_t			 rd_dlci;    /* RFCOMM DLCI */
321 
322 	uint16_t		 rd_flags;   /* DLC flags */
323 	uint16_t		 rd_state;   /* DLC state */
324 	uint16_t		 rd_mtu;     /* MTU */
325 	int			 rd_mode;    /* link mode */
326 
327 	struct sockaddr_bt	 rd_laddr;   /* local address */
328 	struct sockaddr_bt	 rd_raddr;   /* remote address */
329 
330 	uint8_t			 rd_lmodem;  /* local modem signls */
331 	uint8_t			 rd_rmodem;  /* remote modem signals */
332 
333 	int			 rd_rxcred;  /* receive credits (sent) */
334 	size_t			 rd_rxsize;  /* receive buffer (bytes, avail) */
335 	int			 rd_txcred;  /* transmit credits (unused) */
336 	int			 rd_pending; /* packets sent but not complete */
337 
338 	struct callout		 rd_timeout; /* timeout */
339 	struct mbuf		*rd_txbuf;   /* transmit buffer */
340 
341 	const struct btproto	*rd_proto;   /* upper layer callbacks */
342 	void			*rd_upper;   /* upper layer argument */
343 
344 	LIST_ENTRY(rfcomm_dlc)	 rd_next;    /* next dlc on session */
345 };
346 
347 /*
348  * Credit Flow Control works in the following way.
349  *
350  * txcred is how many packets we can send. Received credit
351  * is added to this value, and it is decremented each time
352  * we send a packet.
353  *
354  * rxsize is the number of bytes that are available in the
355  * upstream receive buffer.
356  *
357  * rxcred is the number of credits that we have previously
358  * sent that are still unused. This value will be decreased
359  * for each packet we receive and we will add to it when we
360  * send credits. We calculate the amount of credits to send
361  * by the cunning formula "(space / mtu) - sent" so that if
362  * we get a bunch of small packets, we can continue sending
363  * credits without risking buffer overflow.
364  */
365 
366 /* DLC flags */
367 #define RFCOMM_DLC_DETACH		(1 << 0) /* DLC to be detached */
368 #define RFCOMM_DLC_SHUTDOWN		(1 << 1) /* DLC to be shutdown */
369 
370 /* DLC state */
371 #define RFCOMM_DLC_CLOSED		0	/* no session */
372 #define RFCOMM_DLC_WAIT_SESSION		1	/* waiting for session */
373 #define RFCOMM_DLC_WAIT_CONNECT		2	/* waiting for connect */
374 #define RFCOMM_DLC_WAIT_SEND_SABM	3	/* waiting to send SABM */
375 #define RFCOMM_DLC_WAIT_SEND_UA		4	/* waiting to send UA */
376 #define RFCOMM_DLC_WAIT_RECV_UA		5	/* waiting to receive UA */
377 #define RFCOMM_DLC_OPEN			6	/* can send/receive */
378 #define RFCOMM_DLC_WAIT_DISCONNECT	7	/* waiting for disconnect */
379 #define RFCOMM_DLC_LISTEN		8	/* listening DLC */
380 
381 /*
382  * Bluetooth RFCOMM socket kernel prototypes
383  */
384 
385 extern struct pr_usrreqs rfcomm_usrreqs;
386 struct socket;
387 
388 /* rfcomm_dlc.c */
389 struct rfcomm_dlc *rfcomm_dlc_lookup(struct rfcomm_session *, int);
390 struct rfcomm_dlc *rfcomm_dlc_newconn(struct rfcomm_session *, int);
391 void rfcomm_dlc_close(struct rfcomm_dlc *, int);
392 void rfcomm_dlc_timeout(void *);
393 int rfcomm_dlc_setmode(struct rfcomm_dlc *);
394 int rfcomm_dlc_connect(struct rfcomm_dlc *);
395 int rfcomm_dlc_open(struct rfcomm_dlc *);
396 void rfcomm_dlc_start(struct rfcomm_dlc *);
397 
398 /* rfcomm_session.c */
399 void rfcomm_init(void);
400 struct rfcomm_session *rfcomm_session_alloc(struct rfcomm_session_list *, struct sockaddr_bt *);
401 struct rfcomm_session *rfcomm_session_lookup(struct sockaddr_bt *, struct sockaddr_bt *);
402 void rfcomm_session_free(struct rfcomm_session *);
403 int rfcomm_session_send_frame(struct rfcomm_session *, int, int);
404 int rfcomm_session_send_uih(struct rfcomm_session *, struct rfcomm_dlc *, int, struct mbuf *);
405 int rfcomm_session_send_mcc(struct rfcomm_session *, int, uint8_t, void *, int);
406 void rfcomm_session_timeout(void *);
407 
408 /* rfcomm_socket.c */
409 int rfcomm_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *);
410 int rfcomm_ctloutput(struct socket *so, struct sockopt *sopt);
411 
412 /* rfcomm_upper.c */
413 int rfcomm_attach(struct rfcomm_dlc **, const struct btproto *, void *);
414 int rfcomm_bind(struct rfcomm_dlc *, struct sockaddr_bt *);
415 int rfcomm_sockaddr(struct rfcomm_dlc *, struct sockaddr_bt *);
416 int rfcomm_connect(struct rfcomm_dlc *, struct sockaddr_bt *);
417 int rfcomm_peeraddr(struct rfcomm_dlc *, struct sockaddr_bt *);
418 int rfcomm_disconnect(struct rfcomm_dlc *, int);
419 int rfcomm_detach(struct rfcomm_dlc **);
420 int rfcomm_listen(struct rfcomm_dlc *);
421 int rfcomm_send(struct rfcomm_dlc *, struct mbuf *);
422 int rfcomm_rcvd(struct rfcomm_dlc *, size_t);
423 int rfcomm_setopt(struct rfcomm_dlc *, int, void *);
424 int rfcomm_setopt2(struct rfcomm_dlc *, int, struct socket *, struct sockopt *);
425 int rfcomm_getopt(struct rfcomm_dlc *, int, void *);
426 
427 extern vm_zone_t rfcomm_credit_pool;
428 #endif /* _KERNEL */
429 
430 #endif /* _NETBT_RFCOMM_H_ */
431