xref: /freebsd/sys/netgraph/bluetooth/common/ng_bluetooth.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*
2  * bluetooth.c
3  *
4  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: ng_bluetooth.c,v 1.3 2003/04/26 22:37:31 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
38 
39 #include <netgraph/bluetooth/include/ng_bluetooth.h>
40 
41 /*
42  * Bluetooth stack sysctl globals
43  */
44 
45 static u_int32_t	bluetooth_hci_command_timeout_value  = 5;   /* sec */
46 static u_int32_t	bluetooth_hci_connect_timeout_value  = 60;  /* sec */
47 static u_int32_t	bluetooth_hci_max_neighbor_age_value = 600; /* sec */
48 static u_int32_t	bluetooth_l2cap_rtx_timeout_value    = 60;  /* sec */
49 static u_int32_t	bluetooth_l2cap_ertx_timeout_value   = 300; /* sec */
50 
51 /*
52  * Define sysctl tree that shared by other parts of Bluetooth stack
53  */
54 
55 SYSCTL_NODE(_net, OID_AUTO, bluetooth, CTLFLAG_RW, 0, "Bluetooth family");
56 SYSCTL_INT(_net_bluetooth, OID_AUTO, version,
57 	CTLFLAG_RD, 0, NG_BLUETOOTH_VERSION, "");
58 
59 /*
60  * HCI
61  */
62 
63 SYSCTL_NODE(_net_bluetooth, OID_AUTO, hci, CTLFLAG_RW,
64 	0, "Bluetooth HCI family");
65 
66 static int
67 bluetooth_set_hci_command_timeout_value(SYSCTL_HANDLER_ARGS)
68 {
69 	u_int32_t	value;
70 	int		error;
71 
72 	value = bluetooth_hci_command_timeout_value;
73 	error = sysctl_handle_int(oidp, &value, sizeof(value), req);
74 	if (error == 0 && req->newptr != NULL) {
75 		if (value > 0)
76 			bluetooth_hci_command_timeout_value = value;
77 		else
78 			error = EINVAL;
79 	}
80 
81 	return (error);
82 } /* bluetooth_set_hci_command_timeout_value */
83 
84 SYSCTL_PROC(_net_bluetooth_hci, OID_AUTO, command_timeout,
85 	CTLTYPE_INT | CTLFLAG_RW,
86 	&bluetooth_hci_command_timeout_value, 5,
87 	bluetooth_set_hci_command_timeout_value,
88 	"I", "HCI command timeout (sec)");
89 
90 static int
91 bluetooth_set_hci_connect_timeout_value(SYSCTL_HANDLER_ARGS)
92 {
93 	u_int32_t	value;
94 	int		error;
95 
96 	value = bluetooth_hci_connect_timeout_value;
97 	error = sysctl_handle_int(oidp, &value, sizeof(value), req);
98 	if (error == 0 && req->newptr != NULL) {
99 		if (0 < value && value <= bluetooth_l2cap_rtx_timeout_value)
100 			bluetooth_hci_connect_timeout_value = value;
101 		else
102 			error = EINVAL;
103 	}
104 
105 	return (error);
106 } /* bluetooth_set_hci_connect_timeout_value */
107 
108 SYSCTL_PROC(_net_bluetooth_hci, OID_AUTO, connection_timeout,
109 	CTLTYPE_INT | CTLFLAG_RW,
110 	&bluetooth_hci_connect_timeout_value, 60,
111 	bluetooth_set_hci_connect_timeout_value,
112 	"I", "HCI connect timeout (sec)");
113 
114 SYSCTL_INT(_net_bluetooth_hci, OID_AUTO, max_neighbor_age, CTLFLAG_RW,
115 	&bluetooth_hci_max_neighbor_age_value, 600,
116 	"Maximal HCI neighbor cache entry age (sec)");
117 
118 /*
119  * L2CAP
120  */
121 
122 SYSCTL_NODE(_net_bluetooth, OID_AUTO, l2cap, CTLFLAG_RW,
123 	0, "Bluetooth L2CAP family");
124 
125 static int
126 bluetooth_set_l2cap_rtx_timeout_value(SYSCTL_HANDLER_ARGS)
127 {
128 	u_int32_t	value;
129 	int		error;
130 
131 	value = bluetooth_l2cap_rtx_timeout_value;
132 	error = sysctl_handle_int(oidp, &value, sizeof(value), req);
133 	if (error == 0 && req->newptr != NULL) {
134 		if (bluetooth_hci_connect_timeout_value <= value &&
135 		    value <= bluetooth_l2cap_ertx_timeout_value)
136 			bluetooth_l2cap_rtx_timeout_value = value;
137 		else
138 			error = EINVAL;
139 	}
140 
141 	return (error);
142 } /* bluetooth_set_l2cap_rtx_timeout_value */
143 
144 SYSCTL_PROC(_net_bluetooth_l2cap, OID_AUTO, rtx_timeout,
145 	CTLTYPE_INT | CTLFLAG_RW,
146 	&bluetooth_l2cap_rtx_timeout_value, 60,
147 	bluetooth_set_l2cap_rtx_timeout_value,
148 	"I", "L2CAP RTX timeout (sec)");
149 
150 static int
151 bluetooth_set_l2cap_ertx_timeout_value(SYSCTL_HANDLER_ARGS)
152 {
153 	u_int32_t	value;
154 	int		error;
155 
156 	value = bluetooth_l2cap_ertx_timeout_value;
157 	error = sysctl_handle_int(oidp, &value, sizeof(value), req);
158 	if (error == 0 && req->newptr != NULL) {
159 		if (value >= bluetooth_l2cap_rtx_timeout_value)
160 			bluetooth_l2cap_ertx_timeout_value = value;
161 		else
162 			error = EINVAL;
163 	}
164 
165 	return (error);
166 } /* bluetooth_set_l2cap_ertx_timeout_value */
167 
168 SYSCTL_PROC(_net_bluetooth_l2cap, OID_AUTO, ertx_timeout,
169 	CTLTYPE_INT | CTLFLAG_RW,
170 	&bluetooth_l2cap_ertx_timeout_value, 300,
171 	bluetooth_set_l2cap_ertx_timeout_value,
172 	"I", "L2CAP ERTX timeout (sec)");
173 
174 /*
175  * Return various sysctl values
176  */
177 
178 u_int32_t
179 bluetooth_hci_command_timeout(void)
180 {
181 	return (bluetooth_hci_command_timeout_value * hz);
182 } /* bluetooth_hci_command_timeout */
183 
184 u_int32_t
185 bluetooth_hci_connect_timeout(void)
186 {
187 	return (bluetooth_hci_connect_timeout_value * hz);
188 } /* bluetooth_hci_connect_timeout */
189 
190 u_int32_t
191 bluetooth_hci_max_neighbor_age(void)
192 {
193 	return (bluetooth_hci_max_neighbor_age_value);
194 } /* bluetooth_hci_max_neighbor_age */
195 
196 u_int32_t
197 bluetooth_l2cap_rtx_timeout(void)
198 {
199 	return (bluetooth_l2cap_rtx_timeout_value * hz);
200 } /* bluetooth_l2cap_rtx_timeout */
201 
202 u_int32_t
203 bluetooth_l2cap_ertx_timeout(void)
204 {
205 	return (bluetooth_l2cap_ertx_timeout_value * hz);
206 } /* bluetooth_l2cap_ertx_timeout */
207 
208 /*
209  * RFCOMM
210  */
211 
212 SYSCTL_NODE(_net_bluetooth, OID_AUTO, rfcomm, CTLFLAG_RW,
213 	0, "Bluetooth RFCOMM family");
214 
215 /*
216  * Handle loading and unloading for this code.
217  */
218 
219 static int
220 bluetooth_modevent(module_t mod, int event, void *data)
221 {
222 	int	error = 0;
223 
224 	switch (event) {
225 	case MOD_LOAD:
226 		break;
227 
228 	case MOD_UNLOAD:
229 		break;
230 
231 	default:
232 		error = EOPNOTSUPP;
233 		break;
234 	}
235 
236 	return (error);
237 } /* bluetooth_modevent */
238 
239 /*
240  * Module
241  */
242 
243 static moduledata_t	bluetooth_mod = {
244 	"bluetooth",
245 	bluetooth_modevent,
246 	NULL
247 };
248 
249 DECLARE_MODULE(ng_bluetooth, bluetooth_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
250 MODULE_VERSION(ng_bluetooth, NG_BLUETOOTH_VERSION);
251 
252