xref: /freebsd/sys/netgraph/bluetooth/hci/ng_hci_misc.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*
2  * ng_hci_misc.c
3  *
4  * Copyright (c) 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_hci_misc.c,v 1.5 2003/09/08 18:57:51 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/queue.h>
38 #include <netgraph/ng_message.h>
39 #include <netgraph/netgraph.h>
40 #include <netgraph/bluetooth/include/ng_bluetooth.h>
41 #include <netgraph/bluetooth/include/ng_hci.h>
42 #include <netgraph/bluetooth/hci/ng_hci_var.h>
43 #include <netgraph/bluetooth/hci/ng_hci_cmds.h>
44 #include <netgraph/bluetooth/hci/ng_hci_evnt.h>
45 #include <netgraph/bluetooth/hci/ng_hci_ulpi.h>
46 #include <netgraph/bluetooth/hci/ng_hci_misc.h>
47 
48 /******************************************************************************
49  ******************************************************************************
50  **                              Utility routines
51  ******************************************************************************
52  ******************************************************************************/
53 
54 /*
55  * Give packet to RAW hook
56  * Assumes input mbuf is read only.
57  */
58 
59 void
60 ng_hci_mtap(ng_hci_unit_p unit, struct mbuf *m0)
61 {
62 	struct mbuf	*m = NULL;
63 	int		 error = 0;
64 
65 	if (unit->raw != NULL && NG_HOOK_IS_VALID(unit->raw)) {
66 		m = m_dup(m0, M_DONTWAIT);
67 		if (m != NULL)
68 			NG_SEND_DATA_ONLY(error, unit->raw, m);
69 
70 		if (error != 0)
71 			NG_HCI_INFO(
72 "%s: %s - Could not forward packet, error=%d\n",
73 				__func__, NG_NODE_NAME(unit->node), error);
74 	}
75 } /* ng_hci_mtap */
76 
77 /*
78  * Send notification to the upper layer's
79  */
80 
81 void
82 ng_hci_node_is_up(node_p node, hook_p hook, void *arg1, int arg2)
83 {
84 	ng_hci_unit_p		 unit = NULL;
85 	struct ng_mesg		*msg = NULL;
86 	ng_hci_node_up_ep	*ep = NULL;
87 	int			 error;
88 
89 	if (node == NULL || NG_NODE_NOT_VALID(node) ||
90 	    hook == NULL || NG_HOOK_NOT_VALID(hook))
91 		return;
92 
93 	unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
94 	if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY)
95 		return;
96 
97 	if (hook != unit->acl && hook != unit->sco)
98 		return;
99 
100 	NG_MKMESSAGE(msg,NGM_HCI_COOKIE,NGM_HCI_NODE_UP,sizeof(*ep),M_NOWAIT);
101 	if (msg != NULL) {
102 		ep = (ng_hci_node_up_ep *)(msg->data);
103 
104 		if (hook == unit->acl) {
105 			NG_HCI_BUFF_ACL_SIZE(unit->buffer, ep->pkt_size);
106 			NG_HCI_BUFF_ACL_TOTAL(unit->buffer, ep->num_pkts);
107 		} else {
108 			NG_HCI_BUFF_SCO_SIZE(unit->buffer, ep->pkt_size);
109 			NG_HCI_BUFF_SCO_TOTAL(unit->buffer, ep->num_pkts);
110 		}
111 
112 		bcopy(&unit->bdaddr, &ep->bdaddr, sizeof(ep->bdaddr));
113 
114 		NG_SEND_MSG_HOOK(error, node, msg, hook, 0);
115 	} else
116 		error = ENOMEM;
117 
118 	if (error != 0)
119 		NG_HCI_INFO(
120 "%s: %s - failed to send NODE_UP message to hook \"%s\", error=%d\n",
121 			__func__, NG_NODE_NAME(unit->node),
122 			NG_HOOK_NAME(hook), error);
123 } /* ng_hci_node_is_up */
124 
125 /*
126  * Clean unit (helper)
127  */
128 
129 void
130 ng_hci_unit_clean(ng_hci_unit_p unit, int reason)
131 {
132 	int	size;
133 
134 	/* Drain command queue */
135 	if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)
136 		ng_hci_command_untimeout(unit);
137 
138 	NG_BT_MBUFQ_DRAIN(&unit->cmdq);
139 	NG_HCI_BUFF_CMD_SET(unit->buffer, 1);
140 
141 	/* Clean up connection list */
142 	while (!LIST_EMPTY(&unit->con_list)) {
143 		ng_hci_unit_con_p	con = LIST_FIRST(&unit->con_list);
144 
145 		/* Remove all timeouts (if any) */
146 		if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
147 			ng_hci_con_untimeout(con);
148 
149 		/*
150 		 * Notify upper layer protocol and destroy connection
151 		 * descriptor. Do not really care about the result.
152 		 */
153 
154 		ng_hci_lp_discon_ind(con, reason);
155 		ng_hci_free_con(con);
156 	}
157 
158 	NG_HCI_BUFF_ACL_TOTAL(unit->buffer, size);
159 	NG_HCI_BUFF_ACL_FREE(unit->buffer, size);
160 
161 	NG_HCI_BUFF_SCO_TOTAL(unit->buffer, size);
162 	NG_HCI_BUFF_SCO_FREE(unit->buffer, size);
163 
164 	/* Clean up neighbors list */
165 	ng_hci_flush_neighbor_cache(unit);
166 } /* ng_hci_unit_clean */
167 
168 /*
169  * Allocate and link new unit neighbor cache entry
170  */
171 
172 ng_hci_neighbor_p
173 ng_hci_new_neighbor(ng_hci_unit_p unit)
174 {
175 	ng_hci_neighbor_p	n = NULL;
176 
177 	MALLOC(n, ng_hci_neighbor_p, sizeof(*n), M_NETGRAPH_HCI,
178 		M_NOWAIT | M_ZERO);
179 	if (n != NULL) {
180 		getmicrotime(&n->updated);
181 		LIST_INSERT_HEAD(&unit->neighbors, n, next);
182 	}
183 
184 	return (n);
185 } /* ng_hci_new_neighbor */
186 
187 /*
188  * Free unit neighbor cache entry
189  */
190 
191 void
192 ng_hci_free_neighbor(ng_hci_neighbor_p n)
193 {
194 	LIST_REMOVE(n, next);
195 	bzero(n, sizeof(*n));
196 	FREE(n, M_NETGRAPH_HCI);
197 } /* ng_hci_free_neighbor */
198 
199 /*
200  * Flush neighbor cache
201  */
202 
203 void
204 ng_hci_flush_neighbor_cache(ng_hci_unit_p unit)
205 {
206 	while (!LIST_EMPTY(&unit->neighbors))
207 		ng_hci_free_neighbor(LIST_FIRST(&unit->neighbors));
208 } /* ng_hci_flush_neighbor_cache */
209 
210 /*
211  * Lookup unit in neighbor cache
212  */
213 
214 ng_hci_neighbor_p
215 ng_hci_get_neighbor(ng_hci_unit_p unit, bdaddr_p bdaddr)
216 {
217 	ng_hci_neighbor_p	n = NULL;
218 
219 	for (n = LIST_FIRST(&unit->neighbors); n != NULL; ) {
220 		ng_hci_neighbor_p	nn = LIST_NEXT(n, next);
221 
222 		if (!ng_hci_neighbor_stale(n)) {
223 			if (bcmp(&n->bdaddr, bdaddr, sizeof(*bdaddr)) == 0)
224 				break;
225 		} else
226 			ng_hci_free_neighbor(n); /* remove old entry */
227 
228 		n = nn;
229 	}
230 
231 	return (n);
232 } /* ng_hci_get_neighbor */
233 
234 /*
235  * Check if neighbor entry is stale
236  */
237 
238 int
239 ng_hci_neighbor_stale(ng_hci_neighbor_p n)
240 {
241 	struct timeval	now;
242 
243 	getmicrotime(&now);
244 
245 	return (now.tv_sec - n->updated.tv_sec > bluetooth_hci_max_neighbor_age());
246 } /* ng_hci_neighbor_stale */
247 
248 /*
249  * Allocate and link new connection descriptor
250  */
251 
252 ng_hci_unit_con_p
253 ng_hci_new_con(ng_hci_unit_p unit, int link_type)
254 {
255 	ng_hci_unit_con_p	con = NULL;
256 	int			num_pkts;
257 	static int		fake_con_handle = 0x0f00;
258 
259 	MALLOC(con, ng_hci_unit_con_p, sizeof(*con), M_NETGRAPH_HCI,
260 		M_NOWAIT | M_ZERO);
261 	if (con != NULL) {
262 		con->unit = unit;
263 		con->state = NG_HCI_CON_CLOSED;
264 
265 		/*
266 		 * XXX
267 		 *
268 		 * Assign fake connection handle to the connection descriptor.
269 		 * Bluetooth specification marks 0x0f00 - 0x0fff connection
270 		 * handles as reserved. We need this fake connection handles
271 		 * for timeouts. Connection handle will be passed as argument
272 		 * to timeout so when timeout happens we can find the right
273 		 * connection descriptor. We can not pass pointers, because
274 		 * timeouts are external (to Netgraph) events and there might
275 		 * be a race when node/hook goes down and timeout event already
276 		 * went into node's queue
277 		 */
278 
279 		con->con_handle = fake_con_handle ++;
280 		if (fake_con_handle > 0x0fff)
281 			fake_con_handle = 0x0f00;
282 
283 		con->link_type = link_type;
284 
285 		if (con->link_type == NG_HCI_LINK_ACL)
286 			NG_HCI_BUFF_ACL_TOTAL(unit->buffer, num_pkts);
287 		else
288 			NG_HCI_BUFF_SCO_TOTAL(unit->buffer, num_pkts);
289 
290 		NG_BT_ITEMQ_INIT(&con->conq, num_pkts);
291 
292 		callout_handle_init(&con->con_timo);
293 
294 		LIST_INSERT_HEAD(&unit->con_list, con, next);
295 	}
296 
297 	return (con);
298 } /* ng_hci_new_con */
299 
300 /*
301  * Free connection descriptor
302  */
303 
304 void
305 ng_hci_free_con(ng_hci_unit_con_p con)
306 {
307 	LIST_REMOVE(con, next);
308 
309 	/*
310 	 * If we have pending packets then assume that Host Controller has
311 	 * flushed these packets and we can free them too
312 	 */
313 
314 	if (con->link_type == NG_HCI_LINK_ACL)
315 		NG_HCI_BUFF_ACL_FREE(con->unit->buffer, con->pending);
316 	else
317 		NG_HCI_BUFF_SCO_FREE(con->unit->buffer, con->pending);
318 
319 	NG_BT_ITEMQ_DESTROY(&con->conq);
320 
321 	bzero(con, sizeof(*con));
322 	FREE(con, M_NETGRAPH_HCI);
323 } /* ng_hci_free_con */
324 
325 /*
326  * Lookup connection for given unit and connection handle.
327  */
328 
329 ng_hci_unit_con_p
330 ng_hci_con_by_handle(ng_hci_unit_p unit, int con_handle)
331 {
332 	ng_hci_unit_con_p	con = NULL;
333 
334 	LIST_FOREACH(con, &unit->con_list, next)
335 		if (con->con_handle == con_handle)
336 			break;
337 
338 	return (con);
339 } /* ng_hci_con_by_handle */
340 
341 /*
342  * Lookup connection for given unit, link type and remove unit address
343  */
344 
345 ng_hci_unit_con_p
346 ng_hci_con_by_bdaddr(ng_hci_unit_p unit, bdaddr_p bdaddr, int link_type)
347 {
348 	ng_hci_unit_con_p	con = NULL;
349 
350 	LIST_FOREACH(con, &unit->con_list, next)
351 		if (con->link_type == link_type &&
352 		    bcmp(&con->bdaddr, bdaddr, sizeof(bdaddr_t)) == 0)
353 			break;
354 
355 	return (con);
356 } /* ng_hci_con_by_bdaddr */
357 
358 /*
359  * Set HCI command timeout
360  * XXX FIXME: check unit->cmd_timo.callout != NULL
361  */
362 
363 int
364 ng_hci_command_timeout(ng_hci_unit_p unit)
365 {
366 	if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)
367 		panic(
368 "%s: %s - Duplicated command timeout!\n", __func__, NG_NODE_NAME(unit->node));
369 
370 	unit->state |= NG_HCI_UNIT_COMMAND_PENDING;
371 	unit->cmd_timo = ng_timeout(unit->node, NULL,
372 				bluetooth_hci_command_timeout(),
373 				ng_hci_process_command_timeout, NULL, 0);
374 
375 	return (0);
376 } /* ng_hci_command_timeout */
377 
378 /*
379  * Unset HCI command timeout
380  */
381 
382 int
383 ng_hci_command_untimeout(ng_hci_unit_p unit)
384 {
385 	if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
386 		panic(
387 "%s: %s - No command timeout!\n", __func__, NG_NODE_NAME(unit->node));
388 
389 	if (ng_untimeout(unit->cmd_timo, unit->node) == 0)
390 		return (ETIMEDOUT);
391 
392 	unit->state &= ~NG_HCI_UNIT_COMMAND_PENDING;
393 
394 	return (0);
395 } /* ng_hci_command_untimeout */
396 
397 /*
398  * Set HCI connection timeout
399  * XXX FIXME: check unit->cmd_timo.callout != NULL
400  */
401 
402 int
403 ng_hci_con_timeout(ng_hci_unit_con_p con)
404 {
405 	if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
406 		panic(
407 "%s: %s - Duplicated connection timeout!\n",
408 			__func__, NG_NODE_NAME(con->unit->node));
409 
410 	con->flags |= NG_HCI_CON_TIMEOUT_PENDING;
411 	con->con_timo = ng_timeout(con->unit->node, NULL,
412 				bluetooth_hci_connect_timeout(),
413 				ng_hci_process_con_timeout, NULL,
414 				con->con_handle);
415 
416 	return (0);
417 } /* ng_hci_con_timeout */
418 
419 /*
420  * Unset HCI connection timeout
421  */
422 
423 int
424 ng_hci_con_untimeout(ng_hci_unit_con_p con)
425 {
426 	if (!(con->flags & NG_HCI_CON_TIMEOUT_PENDING))
427 		panic(
428 "%s: %s - No connection timeout!\n", __func__, NG_NODE_NAME(con->unit->node));
429 
430 	if (ng_untimeout(con->con_timo, con->unit->node) == 0)
431 		return (ETIMEDOUT);
432 
433 	con->flags &= ~NG_HCI_CON_TIMEOUT_PENDING;
434 
435 	return (0);
436 } /* ng_hci_con_untimeout */
437 
438 #if 0
439 /*
440  * Convert numeric error code/reason to a string
441  */
442 
443 char const * const
444 ng_hci_str_error(u_int16_t code)
445 {
446 #define	LAST_ERROR_CODE			((sizeof(s)/sizeof(s[0]))-1)
447 	static char const * const	s[] = {
448 	/* 0x00 */ "No error",
449 	/* 0x01 */ "Unknown HCI command",
450 	/* 0x02 */ "No connection",
451 	/* 0x03 */ "Hardware failure",
452 	/* 0x04 */ "Page timeout",
453 	/* 0x05 */ "Authentication failure",
454 	/* 0x06 */ "Key missing",
455 	/* 0x07 */ "Memory full",
456 	/* 0x08 */ "Connection timeout",
457 	/* 0x09 */ "Max number of connections",
458 	/* 0x0a */ "Max number of SCO connections to a unit",
459 	/* 0x0b */ "ACL connection already exists",
460 	/* 0x0c */ "Command disallowed",
461 	/* 0x0d */ "Host rejected due to limited resources",
462 	/* 0x0e */ "Host rejected due to securiity reasons",
463 	/* 0x0f */ "Host rejected due to remote unit is a personal unit",
464 	/* 0x10 */ "Host timeout",
465 	/* 0x11 */ "Unsupported feature or parameter value",
466 	/* 0x12 */ "Invalid HCI command parameter",
467 	/* 0x13 */ "Other end terminated connection: User ended connection",
468 	/* 0x14 */ "Other end terminated connection: Low resources",
469 	/* 0x15 */ "Other end terminated connection: About to power off",
470 	/* 0x16 */ "Connection terminated by local host",
471 	/* 0x17 */ "Repeated attempts",
472 	/* 0x18 */ "Pairing not allowed",
473 	/* 0x19 */ "Unknown LMP PDU",
474 	/* 0x1a */ "Unsupported remote feature",
475 	/* 0x1b */ "SCO offset rejected",
476 	/* 0x1c */ "SCO interval rejected",
477 	/* 0x1d */ "SCO air mode rejected",
478 	/* 0x1e */ "Invalid LMP parameters",
479 	/* 0x1f */ "Unspecified error",
480 	/* 0x20 */ "Unsupported LMP parameter value",
481 	/* 0x21 */ "Role change not allowed",
482 	/* 0x22 */ "LMP response timeout",
483 	/* 0x23 */ "LMP error transaction collision",
484 	/* 0x24 */ "LMP PSU not allowed",
485 	/* 0x25 */ "Encryption mode not acceptable",
486 	/* 0x26 */ "Unit key used",
487 	/* 0x27 */ "QoS is not supported",
488 	/* 0x28 */ "Instant passed",
489 	/* 0x29 */ "Paring with unit key not supported",
490 	/* SHOULD ALWAYS BE LAST */ "Unknown error"
491 	};
492 
493 	return ((code >= LAST_ERROR_CODE)? s[LAST_ERROR_CODE] : s[code]);
494 } /* ng_hci_str_error */
495 #endif
496 
497