6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: ng_hci_main.c,v 1.2 2003/03/18 00:09:36 max Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <netgraph/ng_message.h>
44 #include <netgraph/netgraph.h>
45 #include <netgraph/ng_parse.h>
46 #include <netgraph/bluetooth/include/ng_bluetooth.h>
47 #include <netgraph/bluetooth/include/ng_hci.h>
48 #include <netgraph/bluetooth/hci/ng_hci_var.h>
49 #include <netgraph/bluetooth/hci/ng_hci_prse.h>
50 #include <netgraph/bluetooth/hci/ng_hci_cmds.h>
51 #include <netgraph/bluetooth/hci/ng_hci_evnt.h>
52 #include <netgraph/bluetooth/hci/ng_hci_ulpi.h>
53 #include <netgraph/bluetooth/hci/ng_hci_misc.h>
55 /******************************************************************************
56 ******************************************************************************
57 ** This node implements Bluetooth Host Controller Interface (HCI)
58 ******************************************************************************
59 ******************************************************************************/
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_HCI, "netgraph_hci", "Netgraph Bluetooth HCI node");
65 #define M_NETGRAPH_HCI M_NETGRAPH
66 #endif /* NG_SEPARATE_MALLOC */
68 /* Netgraph node methods */
69 static ng_constructor_t ng_hci_constructor;
70 static ng_shutdown_t ng_hci_shutdown;
71 static ng_newhook_t ng_hci_newhook;
72 static ng_connect_t ng_hci_connect;
73 static ng_disconnect_t ng_hci_disconnect;
74 static ng_rcvmsg_t ng_hci_default_rcvmsg;
75 static ng_rcvmsg_t ng_hci_upper_rcvmsg;
76 static ng_rcvdata_t ng_hci_drv_rcvdata;
77 static ng_rcvdata_t ng_hci_acl_rcvdata;
78 static ng_rcvdata_t ng_hci_sco_rcvdata;
79 static ng_rcvdata_t ng_hci_raw_rcvdata;
81 /* Netgraph node type descriptor */
82 static struct ng_type typestruct = {
83 .version = NG_ABI_VERSION,
84 .name = NG_HCI_NODE_TYPE,
85 .constructor = ng_hci_constructor,
86 .rcvmsg = ng_hci_default_rcvmsg,
87 .shutdown = ng_hci_shutdown,
88 .newhook = ng_hci_newhook,
89 .connect = ng_hci_connect,
90 .rcvdata = ng_hci_drv_rcvdata,
91 .disconnect = ng_hci_disconnect,
92 .cmdlist = ng_hci_cmdlist,
94 NETGRAPH_INIT(hci, &typestruct);
95 MODULE_VERSION(ng_hci, NG_BLUETOOTH_VERSION);
96 MODULE_DEPEND(ng_hci, ng_bluetooth, NG_BLUETOOTH_VERSION,
97 NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
98 static int ng_hci_linktype_to_addrtype(int linktype);
100 static int ng_hci_linktype_to_addrtype(int linktype)
103 case NG_HCI_LINK_LE_PUBLIC:
104 return BDADDR_LE_PUBLIC;
105 case NG_HCI_LINK_LE_RANDOM:
106 return BDADDR_LE_RANDOM;
107 case NG_HCI_LINK_ACL:
114 /*****************************************************************************
115 *****************************************************************************
116 ** Netgraph methods implementation
117 *****************************************************************************
118 *****************************************************************************/
121 * Create new instance of HCI node (new unit)
125 ng_hci_constructor(node_p node)
127 ng_hci_unit_p unit = NULL;
129 unit = malloc(sizeof(*unit), M_NETGRAPH_HCI, M_WAITOK | M_ZERO);
132 unit->debug = NG_HCI_WARN_LEVEL;
134 unit->link_policy_mask = 0xffff; /* Enable all supported modes */
135 unit->packet_mask = 0xffff; /* Enable all packet types */
136 unit->role_switch = 1; /* Enable role switch (if device supports it) */
139 * Set default buffer info
142 * One ACL packet with max. size of 17 bytes (1 DM1 packet)
143 * One SCO packet with max. size of 10 bytes (1 HV1 packet)
146 NG_HCI_BUFF_CMD_SET(unit->buffer, 1);
147 NG_HCI_BUFF_ACL_SET(unit->buffer, 1, 17, 1);
148 NG_HCI_BUFF_SCO_SET(unit->buffer, 1, 10, 1);
150 /* Init command queue & command timeout handler */
151 ng_callout_init(&unit->cmd_timo);
152 NG_BT_MBUFQ_INIT(&unit->cmdq, NG_HCI_CMD_QUEUE_LEN);
155 LIST_INIT(&unit->con_list);
156 LIST_INIT(&unit->neighbors);
159 * This node has to be a WRITER because both data and messages
160 * can change node state.
163 NG_NODE_FORCE_WRITER(node);
164 NG_NODE_SET_PRIVATE(node, unit);
167 } /* ng_hci_constructor */
174 ng_hci_shutdown(node_p node)
176 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
178 NG_NODE_SET_PRIVATE(node, NULL);
182 ng_hci_unit_clean(unit, 0x16 /* Connection terminated by local host */);
184 NG_BT_MBUFQ_DESTROY(&unit->cmdq);
186 bzero(unit, sizeof(*unit));
187 free(unit, M_NETGRAPH_HCI);
190 } /* ng_hci_shutdown */
193 * Give our OK for a hook to be added. Unit driver is connected to the driver
194 * (NG_HCI_HOOK_DRV) hook. Upper layer protocols are connected to appropriate
195 * (NG_HCI_HOOK_ACL or NG_HCI_HOOK_SCO) hooks.
199 ng_hci_newhook(node_p node, hook_p hook, char const *name)
201 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
204 if (strcmp(name, NG_HCI_HOOK_DRV) == 0)
206 else if (strcmp(name, NG_HCI_HOOK_ACL) == 0)
208 else if (strcmp(name, NG_HCI_HOOK_SCO) == 0)
210 else if (strcmp(name, NG_HCI_HOOK_RAW) == 0)
221 } /* ng_hci_newhook */
224 * Give our final OK to connect hook
228 ng_hci_connect(hook_p hook)
230 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
232 if (hook != unit->drv) {
233 if (hook == unit->acl) {
234 NG_HOOK_SET_RCVMSG(hook, ng_hci_upper_rcvmsg);
235 NG_HOOK_SET_RCVDATA(hook, ng_hci_acl_rcvdata);
236 } else if (hook == unit->sco) {
237 NG_HOOK_SET_RCVMSG(hook, ng_hci_upper_rcvmsg);
238 NG_HOOK_SET_RCVDATA(hook, ng_hci_sco_rcvdata);
240 NG_HOOK_SET_RCVDATA(hook, ng_hci_raw_rcvdata);
242 /* Send delayed notification to the upper layers */
243 if (hook != unit->raw)
244 ng_send_fn(unit->node, hook, ng_hci_node_is_up, NULL,0);
246 unit->state |= NG_HCI_UNIT_CONNECTED;
249 } /* ng_hci_connect */
252 * Disconnect the hook
256 ng_hci_disconnect(hook_p hook)
258 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
260 if (hook == unit->acl)
262 else if (hook == unit->sco)
264 else if (hook == unit->raw)
266 else if (hook == unit->drv) {
269 /* Connection terminated by local host */
270 ng_hci_unit_clean(unit, 0x16);
271 unit->state &= ~(NG_HCI_UNIT_CONNECTED|NG_HCI_UNIT_INITED);
275 /* Shutdown when all hooks are disconnected */
276 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
277 (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
278 ng_rmnode_self(NG_HOOK_NODE(hook));
281 } /* ng_hci_disconnect */
284 * Default control message processing routine. Control message could be:
286 * 1) GENERIC Netgraph messages
288 * 2) Control message directed to the node itself.
292 ng_hci_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
294 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
295 struct ng_mesg *msg = NULL, *rsp = NULL;
298 NGI_GET_MSG(item, msg);
300 switch (msg->header.typecookie) {
301 case NGM_GENERIC_COOKIE:
302 switch (msg->header.cmd) {
303 case NGM_TEXT_STATUS: {
305 acl_total, acl_avail, acl_size,
306 sco_total, sco_avail, sco_size;
308 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
314 NG_HCI_BUFF_CMD_GET(unit->buffer, cmd_avail);
316 NG_HCI_BUFF_ACL_AVAIL(unit->buffer, acl_avail);
317 NG_HCI_BUFF_ACL_TOTAL(unit->buffer, acl_total);
318 NG_HCI_BUFF_ACL_SIZE(unit->buffer, acl_size);
320 NG_HCI_BUFF_SCO_AVAIL(unit->buffer, sco_avail);
321 NG_HCI_BUFF_SCO_TOTAL(unit->buffer, sco_total);
322 NG_HCI_BUFF_SCO_SIZE(unit->buffer, sco_size);
324 snprintf(rsp->data, NG_TEXTRESPONSE,
325 "bdaddr %x:%x:%x:%x:%x:%x\n" \
326 "Hooks %s %s %s %s\n" \
329 "Buffer cmd:%d,acl:%d,%d,%d,sco:%d,%d,%d",
330 unit->bdaddr.b[5], unit->bdaddr.b[4],
331 unit->bdaddr.b[3], unit->bdaddr.b[2],
332 unit->bdaddr.b[1], unit->bdaddr.b[0],
333 (unit->drv != NULL)? NG_HCI_HOOK_DRV : "",
334 (unit->acl != NULL)? NG_HCI_HOOK_ACL : "",
335 (unit->sco != NULL)? NG_HCI_HOOK_SCO : "",
336 (unit->raw != NULL)? NG_HCI_HOOK_RAW : "",
338 NG_BT_MBUFQ_LEN(&unit->cmdq),
340 acl_avail, acl_total, acl_size,
341 sco_avail, sco_total, sco_size);
351 switch (msg->header.cmd) {
352 /* Get current node state */
353 case NGM_HCI_NODE_GET_STATE:
354 NG_MKRESPONSE(rsp, msg, sizeof(unit->state), M_NOWAIT);
360 *((ng_hci_node_state_ep *)(rsp->data)) = unit->state;
363 /* Turn INITED bit - node initialized */
364 case NGM_HCI_NODE_INIT:
365 if (bcmp(&unit->bdaddr, NG_HCI_BDADDR_ANY,
366 sizeof(bdaddr_t)) == 0) {
371 unit->state |= NG_HCI_UNIT_INITED;
373 ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
374 ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
377 /* Get node debug level */
378 case NGM_HCI_NODE_GET_DEBUG:
379 NG_MKRESPONSE(rsp, msg, sizeof(unit->debug), M_NOWAIT);
385 *((ng_hci_node_debug_ep *)(rsp->data)) = unit->debug;
388 /* Set node debug level */
389 case NGM_HCI_NODE_SET_DEBUG:
390 if (msg->header.arglen != sizeof(ng_hci_node_debug_ep)){
395 unit->debug = *((ng_hci_node_debug_ep *)(msg->data));
398 /* Get buffer info */
399 case NGM_HCI_NODE_GET_BUFFER: {
400 ng_hci_node_buffer_ep *ep = NULL;
402 NG_MKRESPONSE(rsp, msg, sizeof(ng_hci_node_buffer_ep),
409 ep = (ng_hci_node_buffer_ep *)(rsp->data);
411 NG_HCI_BUFF_CMD_GET(unit->buffer, ep->cmd_free);
412 NG_HCI_BUFF_ACL_AVAIL(unit->buffer, ep->acl_free);
413 NG_HCI_BUFF_ACL_TOTAL(unit->buffer, ep->acl_pkts);
414 NG_HCI_BUFF_ACL_SIZE(unit->buffer, ep->acl_size);
415 NG_HCI_BUFF_SCO_AVAIL(unit->buffer, ep->sco_free);
416 NG_HCI_BUFF_SCO_TOTAL(unit->buffer, ep->sco_pkts);
417 NG_HCI_BUFF_SCO_SIZE(unit->buffer, ep->sco_size);
421 case NGM_HCI_NODE_GET_BDADDR:
422 NG_MKRESPONSE(rsp, msg, sizeof(bdaddr_t), M_NOWAIT);
428 bcopy(&unit->bdaddr, rsp->data, sizeof(bdaddr_t));
432 case NGM_HCI_NODE_GET_FEATURES:
433 NG_MKRESPONSE(rsp,msg,sizeof(unit->features),M_NOWAIT);
439 bcopy(&unit->features,rsp->data,sizeof(unit->features));
443 case NGM_HCI_NODE_GET_STAT:
444 NG_MKRESPONSE(rsp, msg, sizeof(unit->stat), M_NOWAIT);
450 bcopy(&unit->stat, rsp->data, sizeof(unit->stat));
454 case NGM_HCI_NODE_RESET_STAT:
455 NG_HCI_STAT_RESET(unit->stat);
458 /* Clean up neighbors list */
459 case NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE:
460 ng_hci_flush_neighbor_cache(unit);
463 /* Get neighbor cache entries */
464 case NGM_HCI_NODE_GET_NEIGHBOR_CACHE: {
465 ng_hci_neighbor_p n = NULL;
466 ng_hci_node_get_neighbor_cache_ep *e1 = NULL;
467 ng_hci_node_neighbor_cache_entry_ep *e2 = NULL;
470 /* Look for the fresh entries in the cache */
471 for (n = LIST_FIRST(&unit->neighbors); n != NULL; ) {
472 ng_hci_neighbor_p nn = LIST_NEXT(n, next);
474 if (ng_hci_neighbor_stale(n))
475 ng_hci_free_neighbor(n);
481 if (s > NG_HCI_MAX_NEIGHBOR_NUM)
482 s = NG_HCI_MAX_NEIGHBOR_NUM;
484 /* Prepare response */
485 NG_MKRESPONSE(rsp, msg, sizeof(*e1) + s * sizeof(*e2),
492 e1 = (ng_hci_node_get_neighbor_cache_ep *)(rsp->data);
493 e2 = (ng_hci_node_neighbor_cache_entry_ep *)(e1 + 1);
497 LIST_FOREACH(n, &unit->neighbors, next) {
498 e2->page_scan_rep_mode = n->page_scan_rep_mode;
499 e2->page_scan_mode = n->page_scan_mode;
500 e2->clock_offset = n->clock_offset;
502 ng_hci_linktype_to_addrtype(n->addrtype);
503 e2->extinq_size = n->extinq_size;
504 bcopy(&n->bdaddr, &e2->bdaddr,
506 bcopy(&n->features, &e2->features,
507 sizeof(e2->features));
508 bcopy(&n->extinq_data, &e2->extinq_data,
516 /* Get connection list */
517 case NGM_HCI_NODE_GET_CON_LIST: {
518 ng_hci_unit_con_p c = NULL;
519 ng_hci_node_con_list_ep *e1 = NULL;
520 ng_hci_node_con_ep *e2 = NULL;
523 /* Count number of connections in the list */
524 LIST_FOREACH(c, &unit->con_list, next)
526 if (s > NG_HCI_MAX_CON_NUM)
527 s = NG_HCI_MAX_CON_NUM;
529 /* Prepare response */
530 NG_MKRESPONSE(rsp, msg, sizeof(*e1) + s * sizeof(*e2),
537 e1 = (ng_hci_node_con_list_ep *)(rsp->data);
538 e2 = (ng_hci_node_con_ep *)(e1 + 1);
540 e1->num_connections = s;
542 LIST_FOREACH(c, &unit->con_list, next) {
543 e2->link_type = c->link_type;
544 e2->encryption_mode= c->encryption_mode;
548 e2->state = c->state;
550 e2->pending = c->pending;
551 e2->queue_len = NG_BT_ITEMQ_LEN(&c->conq);
553 e2->con_handle = c->con_handle;
554 bcopy(&c->bdaddr, &e2->bdaddr,
563 /* Get link policy settings mask */
564 case NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK:
565 NG_MKRESPONSE(rsp, msg, sizeof(unit->link_policy_mask),
572 *((ng_hci_node_link_policy_mask_ep *)(rsp->data)) =
573 unit->link_policy_mask;
576 /* Set link policy settings mask */
577 case NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK:
578 if (msg->header.arglen !=
579 sizeof(ng_hci_node_link_policy_mask_ep)) {
584 unit->link_policy_mask =
585 *((ng_hci_node_link_policy_mask_ep *)
589 /* Get packet mask */
590 case NGM_HCI_NODE_GET_PACKET_MASK:
591 NG_MKRESPONSE(rsp, msg, sizeof(unit->packet_mask),
598 *((ng_hci_node_packet_mask_ep *)(rsp->data)) =
602 /* Set packet mask */
603 case NGM_HCI_NODE_SET_PACKET_MASK:
604 if (msg->header.arglen !=
605 sizeof(ng_hci_node_packet_mask_ep)) {
611 *((ng_hci_node_packet_mask_ep *)(msg->data));
614 /* Get role switch */
615 case NGM_HCI_NODE_GET_ROLE_SWITCH:
616 NG_MKRESPONSE(rsp, msg, sizeof(unit->role_switch),
623 *((ng_hci_node_role_switch_ep *)(rsp->data)) =
627 /* Set role switch */
628 case NGM_HCI_NODE_SET_ROLE_SWITCH:
629 if (msg->header.arglen !=
630 sizeof(ng_hci_node_role_switch_ep)) {
636 *((ng_hci_node_role_switch_ep *)(msg->data));
650 /* NG_RESPOND_MSG should take care of "item" and "rsp" */
651 NG_RESPOND_MSG(error, node, item, rsp);
655 } /* ng_hci_default_rcvmsg */
658 * Process control message from upstream hooks (ACL and SCO).
659 * Handle LP_xxx messages here, give everything else to default routine.
663 ng_hci_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
665 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
668 switch (NGI_MSG(item)->header.typecookie) {
670 switch (NGI_MSG(item)->header.cmd) {
671 case NGM_HCI_LP_CON_REQ:
672 error = ng_hci_lp_con_req(unit, item, lasthook);
675 case NGM_HCI_LP_DISCON_REQ: /* XXX not defined by specs */
676 error = ng_hci_lp_discon_req(unit, item, lasthook);
679 case NGM_HCI_LP_CON_RSP:
680 error = ng_hci_lp_con_rsp(unit, item, lasthook);
683 case NGM_HCI_LP_QOS_REQ:
684 error = ng_hci_lp_qos_req(unit, item, lasthook);
688 error = ng_hci_default_rcvmsg(node, item, lasthook);
694 error = ng_hci_default_rcvmsg(node, item, lasthook);
699 } /* ng_hci_upper_rcvmsg */
702 * Process data packet from the driver hook.
703 * We expect HCI events, ACL or SCO data packets.
707 ng_hci_drv_rcvdata(hook_p hook, item_p item)
709 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
710 struct mbuf *m = NULL;
714 m = NGI_M(item); /* item still has mbuf, just peeking */
715 m->m_flags |= M_PROTO1; /* mark as incoming packet */
717 NG_HCI_STAT_BYTES_RECV(unit->stat, m->m_pkthdr.len);
719 /* Give copy packet to RAW hook */
720 ng_hci_mtap(unit, m);
724 * Lower layer drivers MUST NOT send mbuf chain with empty mbuf at
725 * the beginning of the chain. HCI layer WILL NOT call m_pullup() here.
728 switch (*mtod(m, u_int8_t *)) {
729 case NG_HCI_ACL_DATA_PKT:
730 NG_HCI_STAT_ACL_RECV(unit->stat);
732 if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY ||
733 unit->acl == NULL || NG_HOOK_NOT_VALID(unit->acl)) {
735 "%s: %s - could not forward HCI ACL data packet, state=%#x, hook=%p\n",
736 __func__, NG_NODE_NAME(unit->node),
737 unit->state, unit->acl);
741 NG_FWD_ITEM_HOOK(error, item, unit->acl);
744 case NG_HCI_SCO_DATA_PKT:
745 NG_HCI_STAT_SCO_RECV(unit->stat);
747 if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY ||
748 unit->sco == NULL || NG_HOOK_NOT_VALID(unit->sco)) {
750 "%s: %s - could not forward HCI SCO data packet, state=%#x, hook=%p\n",
751 __func__, NG_NODE_NAME(unit->node),
752 unit->state, unit->sco);
756 NG_FWD_ITEM_HOOK(error, item, unit->sco);
759 case NG_HCI_EVENT_PKT:
760 NG_HCI_STAT_EVNT_RECV(unit->stat);
762 /* Detach mbuf, discard item and process event */
766 error = ng_hci_process_event(unit, m);
771 "%s: %s - got unknown HCI packet type=%#x\n",
772 __func__, NG_NODE_NAME(unit->node),
773 *mtod(m, u_int8_t *));
782 } /* ng_hci_drv_rcvdata */
785 * Process data packet from ACL upstream hook.
786 * We expect valid HCI ACL data packets.
790 ng_hci_acl_rcvdata(hook_p hook, item_p item)
792 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
793 struct mbuf *m = NULL;
794 ng_hci_unit_con_p con = NULL;
795 u_int16_t con_handle;
798 NG_HCI_BUFF_ACL_SIZE(unit->buffer, size);
802 if (*mtod(m, u_int8_t *) != NG_HCI_ACL_DATA_PKT) {
804 "%s: %s - invalid HCI data packet type=%#x\n",
805 __func__, NG_NODE_NAME(unit->node),
806 *mtod(m, u_int8_t *));
811 if (m->m_pkthdr.len < sizeof(ng_hci_acldata_pkt_t) ||
812 m->m_pkthdr.len > sizeof(ng_hci_acldata_pkt_t) + size) {
814 "%s: %s - invalid HCI ACL data packet, len=%d, mtu=%d\n",
815 __func__, NG_NODE_NAME(unit->node),
816 m->m_pkthdr.len, size);
822 NG_HCI_M_PULLUP(m, sizeof(ng_hci_acldata_pkt_t));
828 con_handle = NG_HCI_CON_HANDLE(le16toh(
829 mtod(m, ng_hci_acldata_pkt_t *)->con_handle));
830 size = le16toh(mtod(m, ng_hci_acldata_pkt_t *)->length);
832 if (m->m_pkthdr.len != sizeof(ng_hci_acldata_pkt_t) + size) {
834 "%s: %s - invalid HCI ACL data packet size, len=%d, length=%d\n",
835 __func__, NG_NODE_NAME(unit->node),
836 m->m_pkthdr.len, size);
843 con = ng_hci_con_by_handle(unit, con_handle);
846 "%s: %s - unexpected HCI ACL data packet. Connection does not exists, " \
847 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node), con_handle);
853 if (con->link_type == NG_HCI_LINK_SCO) {
855 "%s: %s - unexpected HCI ACL data packet. Not ACL link, con_handle=%d, " \
856 "link_type=%d\n", __func__, NG_NODE_NAME(unit->node),
857 con_handle, con->link_type);
863 if (con->state != NG_HCI_CON_OPEN) {
865 "%s: %s - unexpected HCI ACL data packet. Invalid connection state=%d, " \
866 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node),
867 con->state, con_handle);
873 if (NG_BT_ITEMQ_FULL(&con->conq)) {
875 "%s: %s - dropping HCI ACL data packet, con_handle=%d, len=%d, queue_len=%d\n",
876 __func__, NG_NODE_NAME(unit->node), con_handle,
877 m->m_pkthdr.len, NG_BT_ITEMQ_LEN(&con->conq));
879 NG_BT_ITEMQ_DROP(&con->conq);
885 /* Queue item and schedule data transfer */
887 NG_BT_ITEMQ_ENQUEUE(&con->conq, item);
891 ng_hci_send_data(unit);
896 NG_FREE_M(m); /* NG_FREE_M() checks for m != NULL */
899 } /* ng_hci_acl_rcvdata */
902 * Process data packet from SCO upstream hook.
903 * We expect valid HCI SCO data packets
907 ng_hci_sco_rcvdata(hook_p hook, item_p item)
909 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
910 struct mbuf *m = NULL;
911 ng_hci_unit_con_p con = NULL;
912 u_int16_t con_handle;
915 NG_HCI_BUFF_SCO_SIZE(unit->buffer, size);
920 if (*mtod(m, u_int8_t *) != NG_HCI_SCO_DATA_PKT) {
922 "%s: %s - invalid HCI data packet type=%#x\n",
923 __func__, NG_NODE_NAME(unit->node),
924 *mtod(m, u_int8_t *));
930 if (m->m_pkthdr.len < sizeof(ng_hci_scodata_pkt_t) ||
931 m->m_pkthdr.len > sizeof(ng_hci_scodata_pkt_t) + size) {
933 "%s: %s - invalid HCI SCO data packet, len=%d, mtu=%d\n",
934 __func__, NG_NODE_NAME(unit->node),
935 m->m_pkthdr.len, size);
941 NG_HCI_M_PULLUP(m, sizeof(ng_hci_scodata_pkt_t));
947 con_handle = NG_HCI_CON_HANDLE(le16toh(
948 mtod(m, ng_hci_scodata_pkt_t *)->con_handle));
949 size = mtod(m, ng_hci_scodata_pkt_t *)->length;
951 if (m->m_pkthdr.len != sizeof(ng_hci_scodata_pkt_t) + size) {
953 "%s: %s - invalid HCI SCO data packet size, len=%d, length=%d\n",
954 __func__, NG_NODE_NAME(unit->node),
955 m->m_pkthdr.len, size);
962 con = ng_hci_con_by_handle(unit, con_handle);
965 "%s: %s - unexpected HCI SCO data packet. Connection does not exists, " \
966 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node), con_handle);
972 if (con->link_type != NG_HCI_LINK_SCO) {
974 "%s: %s - unexpected HCI SCO data packet. Not SCO link, con_handle=%d, " \
975 "link_type=%d\n", __func__, NG_NODE_NAME(unit->node),
976 con_handle, con->link_type);
982 if (con->state != NG_HCI_CON_OPEN) {
984 "%s: %s - unexpected HCI SCO data packet. Invalid connection state=%d, " \
985 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node),
986 con->state, con_handle);
992 if (NG_BT_ITEMQ_FULL(&con->conq)) {
994 "%s: %s - dropping HCI SCO data packet, con_handle=%d, len=%d, queue_len=%d\n",
995 __func__, NG_NODE_NAME(unit->node), con_handle,
996 m->m_pkthdr.len, NG_BT_ITEMQ_LEN(&con->conq));
998 NG_BT_ITEMQ_DROP(&con->conq);
1004 /* Queue item and schedule data transfer */
1006 NG_BT_ITEMQ_ENQUEUE(&con->conq, item);
1010 ng_hci_send_data(unit);
1015 NG_FREE_M(m); /* NG_FREE_M() checks for m != NULL */
1018 } /* ng_hci_sco_rcvdata */
1021 * Process data packet from uptream RAW hook.
1022 * We expect valid HCI command packets.
1026 ng_hci_raw_rcvdata(hook_p hook, item_p item)
1028 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1029 struct mbuf *m = NULL;
1036 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1038 "%s: %s - invalid HCI command packet type=%#x\n",
1039 __func__, NG_NODE_NAME(unit->node),
1040 *mtod(m, u_int8_t *));
1046 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t)) {
1048 "%s: %s - invalid HCI command packet len=%d\n",
1049 __func__, NG_NODE_NAME(unit->node), m->m_pkthdr.len);
1055 NG_HCI_M_PULLUP(m, sizeof(ng_hci_cmd_pkt_t));
1061 if (m->m_pkthdr.len !=
1062 mtod(m, ng_hci_cmd_pkt_t *)->length + sizeof(ng_hci_cmd_pkt_t)) {
1064 "%s: %s - invalid HCI command packet size, len=%d, length=%d\n",
1065 __func__, NG_NODE_NAME(unit->node), m->m_pkthdr.len,
1066 mtod(m, ng_hci_cmd_pkt_t *)->length);
1072 if (mtod(m, ng_hci_cmd_pkt_t *)->opcode == 0) {
1074 "%s: %s - invalid HCI command opcode\n",
1075 __func__, NG_NODE_NAME(unit->node));
1081 if (NG_BT_MBUFQ_FULL(&unit->cmdq)) {
1083 "%s: %s - dropping HCI command packet, len=%d, queue_len=%d\n",
1084 __func__, NG_NODE_NAME(unit->node), m->m_pkthdr.len,
1085 NG_BT_MBUFQ_LEN(&unit->cmdq));
1087 NG_BT_MBUFQ_DROP(&unit->cmdq);
1093 /* Queue and send command */
1094 NG_BT_MBUFQ_ENQUEUE(&unit->cmdq, m);
1097 if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
1098 error = ng_hci_send_command(unit);
1100 NG_FREE_M(m); /* NG_FREE_M() checks for m != NULL */
1103 } /* ng_hci_raw_rcvdata */