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