6 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: ng_hci_main.c,v 1.2 2003/03/18 00:09:36 max Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <netgraph/ng_message.h>
42 #include <netgraph/netgraph.h>
43 #include <netgraph/ng_parse.h>
44 #include <netgraph/bluetooth/include/ng_bluetooth.h>
45 #include <netgraph/bluetooth/include/ng_hci.h>
46 #include <netgraph/bluetooth/hci/ng_hci_var.h>
47 #include <netgraph/bluetooth/hci/ng_hci_prse.h>
48 #include <netgraph/bluetooth/hci/ng_hci_cmds.h>
49 #include <netgraph/bluetooth/hci/ng_hci_evnt.h>
50 #include <netgraph/bluetooth/hci/ng_hci_ulpi.h>
51 #include <netgraph/bluetooth/hci/ng_hci_misc.h>
53 /******************************************************************************
54 ******************************************************************************
55 ** This node implements Bluetooth Host Controller Interface (HCI)
56 ******************************************************************************
57 ******************************************************************************/
60 #ifdef NG_SEPARATE_MALLOC
61 MALLOC_DEFINE(M_NETGRAPH_HCI, "netgraph_hci", "Netgraph Bluetooth HCI node");
63 #define M_NETGRAPH_HCI M_NETGRAPH
64 #endif /* NG_SEPARATE_MALLOC */
66 /* Netgraph node methods */
67 static ng_constructor_t ng_hci_constructor;
68 static ng_shutdown_t ng_hci_shutdown;
69 static ng_newhook_t ng_hci_newhook;
70 static ng_connect_t ng_hci_connect;
71 static ng_disconnect_t ng_hci_disconnect;
72 static ng_rcvmsg_t ng_hci_default_rcvmsg;
73 static ng_rcvmsg_t ng_hci_upper_rcvmsg;
74 static ng_rcvdata_t ng_hci_drv_rcvdata;
75 static ng_rcvdata_t ng_hci_acl_rcvdata;
76 static ng_rcvdata_t ng_hci_sco_rcvdata;
77 static ng_rcvdata_t ng_hci_raw_rcvdata;
79 /* Netgraph node type descriptor */
80 static struct ng_type typestruct = {
81 .version = NG_ABI_VERSION,
82 .name = NG_HCI_NODE_TYPE,
83 .constructor = ng_hci_constructor,
84 .rcvmsg = ng_hci_default_rcvmsg,
85 .shutdown = ng_hci_shutdown,
86 .newhook = ng_hci_newhook,
87 .connect = ng_hci_connect,
88 .rcvdata = ng_hci_drv_rcvdata,
89 .disconnect = ng_hci_disconnect,
90 .cmdlist = ng_hci_cmdlist,
92 NETGRAPH_INIT(hci, &typestruct);
93 MODULE_VERSION(ng_hci, NG_BLUETOOTH_VERSION);
94 MODULE_DEPEND(ng_hci, ng_bluetooth, NG_BLUETOOTH_VERSION,
95 NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
97 /*****************************************************************************
98 *****************************************************************************
99 ** Netgraph methods implementation
100 *****************************************************************************
101 *****************************************************************************/
104 * Create new instance of HCI node (new unit)
108 ng_hci_constructor(node_p node)
110 ng_hci_unit_p unit = NULL;
112 unit = malloc(sizeof(*unit), M_NETGRAPH_HCI,
118 unit->debug = NG_HCI_WARN_LEVEL;
120 unit->link_policy_mask = 0xffff; /* Enable all supported modes */
121 unit->packet_mask = 0xffff; /* Enable all packet types */
122 unit->role_switch = 1; /* Enable role switch (if device supports it) */
125 * Set default buffer info
128 * One ACL packet with max. size of 17 bytes (1 DM1 packet)
129 * One SCO packet with max. size of 10 bytes (1 HV1 packet)
132 NG_HCI_BUFF_CMD_SET(unit->buffer, 1);
133 NG_HCI_BUFF_ACL_SET(unit->buffer, 1, 17, 1);
134 NG_HCI_BUFF_SCO_SET(unit->buffer, 1, 10, 1);
136 /* Init command queue & command timeout handler */
137 ng_callout_init(&unit->cmd_timo);
138 NG_BT_MBUFQ_INIT(&unit->cmdq, NG_HCI_CMD_QUEUE_LEN);
141 LIST_INIT(&unit->con_list);
142 LIST_INIT(&unit->neighbors);
145 * This node has to be a WRITER because both data and messages
146 * can change node state.
149 NG_NODE_FORCE_WRITER(node);
150 NG_NODE_SET_PRIVATE(node, unit);
153 } /* ng_hci_constructor */
160 ng_hci_shutdown(node_p node)
162 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
164 NG_NODE_SET_PRIVATE(node, NULL);
168 ng_hci_unit_clean(unit, 0x16 /* Connection terminated by local host */);
170 NG_BT_MBUFQ_DESTROY(&unit->cmdq);
172 bzero(unit, sizeof(*unit));
173 free(unit, M_NETGRAPH_HCI);
176 } /* ng_hci_shutdown */
179 * Give our OK for a hook to be added. Unit driver is connected to the driver
180 * (NG_HCI_HOOK_DRV) hook. Upper layer protocols are connected to appropriate
181 * (NG_HCI_HOOK_ACL or NG_HCI_HOOK_SCO) hooks.
185 ng_hci_newhook(node_p node, hook_p hook, char const *name)
187 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
190 if (strcmp(name, NG_HCI_HOOK_DRV) == 0)
192 else if (strcmp(name, NG_HCI_HOOK_ACL) == 0)
194 else if (strcmp(name, NG_HCI_HOOK_SCO) == 0)
196 else if (strcmp(name, NG_HCI_HOOK_RAW) == 0)
207 } /* ng_hci_newhook */
210 * Give our final OK to connect hook
214 ng_hci_connect(hook_p hook)
216 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
218 if (hook != unit->drv) {
219 if (hook == unit->acl) {
220 NG_HOOK_SET_RCVMSG(hook, ng_hci_upper_rcvmsg);
221 NG_HOOK_SET_RCVDATA(hook, ng_hci_acl_rcvdata);
222 } else if (hook == unit->sco) {
223 NG_HOOK_SET_RCVMSG(hook, ng_hci_upper_rcvmsg);
224 NG_HOOK_SET_RCVDATA(hook, ng_hci_sco_rcvdata);
226 NG_HOOK_SET_RCVDATA(hook, ng_hci_raw_rcvdata);
228 /* Send delayed notification to the upper layers */
229 if (hook != unit->raw)
230 ng_send_fn(unit->node, hook, ng_hci_node_is_up, NULL,0);
232 unit->state |= NG_HCI_UNIT_CONNECTED;
235 } /* ng_hci_connect */
238 * Disconnect the hook
242 ng_hci_disconnect(hook_p hook)
244 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
246 if (hook == unit->acl)
248 else if (hook == unit->sco)
250 else if (hook == unit->raw)
252 else if (hook == unit->drv) {
255 /* Connection terminated by local host */
256 ng_hci_unit_clean(unit, 0x16);
257 unit->state &= ~(NG_HCI_UNIT_CONNECTED|NG_HCI_UNIT_INITED);
261 /* Shutdown when all hooks are disconnected */
262 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
263 (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
264 ng_rmnode_self(NG_HOOK_NODE(hook));
267 } /* ng_hci_disconnect */
270 * Default control message processing routine. Control message could be:
272 * 1) GENERIC Netgraph messages
274 * 2) Control message directed to the node itself.
278 ng_hci_default_rcvmsg(node_p node, item_p item, hook_p lasthook)
280 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
281 struct ng_mesg *msg = NULL, *rsp = NULL;
284 NGI_GET_MSG(item, msg);
286 switch (msg->header.typecookie) {
287 case NGM_GENERIC_COOKIE:
288 switch (msg->header.cmd) {
289 case NGM_TEXT_STATUS: {
291 acl_total, acl_avail, acl_size,
292 sco_total, sco_avail, sco_size;
294 NG_MKRESPONSE(rsp, msg, NG_TEXTRESPONSE, M_NOWAIT);
300 NG_HCI_BUFF_CMD_GET(unit->buffer, cmd_avail);
302 NG_HCI_BUFF_ACL_AVAIL(unit->buffer, acl_avail);
303 NG_HCI_BUFF_ACL_TOTAL(unit->buffer, acl_total);
304 NG_HCI_BUFF_ACL_SIZE(unit->buffer, acl_size);
306 NG_HCI_BUFF_SCO_AVAIL(unit->buffer, sco_avail);
307 NG_HCI_BUFF_SCO_TOTAL(unit->buffer, sco_total);
308 NG_HCI_BUFF_SCO_SIZE(unit->buffer, sco_size);
310 snprintf(rsp->data, NG_TEXTRESPONSE,
311 "bdaddr %x:%x:%x:%x:%x:%x\n" \
312 "Hooks %s %s %s %s\n" \
315 "Buffer cmd:%d,acl:%d,%d,%d,sco:%d,%d,%d",
316 unit->bdaddr.b[5], unit->bdaddr.b[4],
317 unit->bdaddr.b[3], unit->bdaddr.b[2],
318 unit->bdaddr.b[1], unit->bdaddr.b[0],
319 (unit->drv != NULL)? NG_HCI_HOOK_DRV : "",
320 (unit->acl != NULL)? NG_HCI_HOOK_ACL : "",
321 (unit->sco != NULL)? NG_HCI_HOOK_SCO : "",
322 (unit->raw != NULL)? NG_HCI_HOOK_RAW : "",
324 NG_BT_MBUFQ_LEN(&unit->cmdq),
326 acl_avail, acl_total, acl_size,
327 sco_avail, sco_total, sco_size);
337 switch (msg->header.cmd) {
338 /* Get current node state */
339 case NGM_HCI_NODE_GET_STATE:
340 NG_MKRESPONSE(rsp, msg, sizeof(unit->state), M_NOWAIT);
346 *((ng_hci_node_state_ep *)(rsp->data)) = unit->state;
349 /* Turn INITED bit - node initialized */
350 case NGM_HCI_NODE_INIT:
351 if (bcmp(&unit->bdaddr, NG_HCI_BDADDR_ANY,
352 sizeof(bdaddr_t)) == 0) {
357 unit->state |= NG_HCI_UNIT_INITED;
359 ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
360 ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
363 /* Get node debug level */
364 case NGM_HCI_NODE_GET_DEBUG:
365 NG_MKRESPONSE(rsp, msg, sizeof(unit->debug), M_NOWAIT);
371 *((ng_hci_node_debug_ep *)(rsp->data)) = unit->debug;
374 /* Set node debug level */
375 case NGM_HCI_NODE_SET_DEBUG:
376 if (msg->header.arglen != sizeof(ng_hci_node_debug_ep)){
381 unit->debug = *((ng_hci_node_debug_ep *)(msg->data));
384 /* Get buffer info */
385 case NGM_HCI_NODE_GET_BUFFER: {
386 ng_hci_node_buffer_ep *ep = NULL;
388 NG_MKRESPONSE(rsp, msg, sizeof(ng_hci_node_buffer_ep),
395 ep = (ng_hci_node_buffer_ep *)(rsp->data);
397 NG_HCI_BUFF_CMD_GET(unit->buffer, ep->cmd_free);
398 NG_HCI_BUFF_ACL_AVAIL(unit->buffer, ep->acl_free);
399 NG_HCI_BUFF_ACL_TOTAL(unit->buffer, ep->acl_pkts);
400 NG_HCI_BUFF_ACL_SIZE(unit->buffer, ep->acl_size);
401 NG_HCI_BUFF_SCO_AVAIL(unit->buffer, ep->sco_free);
402 NG_HCI_BUFF_SCO_TOTAL(unit->buffer, ep->sco_pkts);
403 NG_HCI_BUFF_SCO_SIZE(unit->buffer, ep->sco_size);
407 case NGM_HCI_NODE_GET_BDADDR:
408 NG_MKRESPONSE(rsp, msg, sizeof(bdaddr_t), M_NOWAIT);
414 bcopy(&unit->bdaddr, rsp->data, sizeof(bdaddr_t));
418 case NGM_HCI_NODE_GET_FEATURES:
419 NG_MKRESPONSE(rsp,msg,sizeof(unit->features),M_NOWAIT);
425 bcopy(&unit->features,rsp->data,sizeof(unit->features));
429 case NGM_HCI_NODE_GET_STAT:
430 NG_MKRESPONSE(rsp, msg, sizeof(unit->stat), M_NOWAIT);
436 bcopy(&unit->stat, rsp->data, sizeof(unit->stat));
440 case NGM_HCI_NODE_RESET_STAT:
441 NG_HCI_STAT_RESET(unit->stat);
444 /* Clean up neighbors list */
445 case NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE:
446 ng_hci_flush_neighbor_cache(unit);
449 /* Get neighbor cache entries */
450 case NGM_HCI_NODE_GET_NEIGHBOR_CACHE: {
451 ng_hci_neighbor_p n = NULL;
452 ng_hci_node_get_neighbor_cache_ep *e1 = NULL;
453 ng_hci_node_neighbor_cache_entry_ep *e2 = NULL;
456 /* Look for the fresh entries in the cache */
457 for (n = LIST_FIRST(&unit->neighbors); n != NULL; ) {
458 ng_hci_neighbor_p nn = LIST_NEXT(n, next);
460 if (ng_hci_neighbor_stale(n))
461 ng_hci_free_neighbor(n);
467 if (s > NG_HCI_MAX_NEIGHBOR_NUM)
468 s = NG_HCI_MAX_NEIGHBOR_NUM;
470 /* Prepare response */
471 NG_MKRESPONSE(rsp, msg, sizeof(*e1) + s * sizeof(*e2),
478 e1 = (ng_hci_node_get_neighbor_cache_ep *)(rsp->data);
479 e2 = (ng_hci_node_neighbor_cache_entry_ep *)(e1 + 1);
483 LIST_FOREACH(n, &unit->neighbors, next) {
484 e2->page_scan_rep_mode = n->page_scan_rep_mode;
485 e2->page_scan_mode = n->page_scan_mode;
486 e2->clock_offset = n->clock_offset;
487 bcopy(&n->bdaddr, &e2->bdaddr,
489 bcopy(&n->features, &e2->features,
490 sizeof(e2->features));
498 /* Get connection list */
499 case NGM_HCI_NODE_GET_CON_LIST: {
500 ng_hci_unit_con_p c = NULL;
501 ng_hci_node_con_list_ep *e1 = NULL;
502 ng_hci_node_con_ep *e2 = NULL;
505 /* Count number of connections in the list */
506 LIST_FOREACH(c, &unit->con_list, next)
508 if (s > NG_HCI_MAX_CON_NUM)
509 s = NG_HCI_MAX_CON_NUM;
511 /* Prepare response */
512 NG_MKRESPONSE(rsp, msg, sizeof(*e1) + s * sizeof(*e2),
519 e1 = (ng_hci_node_con_list_ep *)(rsp->data);
520 e2 = (ng_hci_node_con_ep *)(e1 + 1);
522 e1->num_connections = s;
524 LIST_FOREACH(c, &unit->con_list, next) {
525 e2->link_type = c->link_type;
526 e2->encryption_mode= c->encryption_mode;
530 e2->state = c->state;
532 e2->pending = c->pending;
533 e2->queue_len = NG_BT_ITEMQ_LEN(&c->conq);
535 e2->con_handle = c->con_handle;
536 bcopy(&c->bdaddr, &e2->bdaddr,
545 /* Get link policy settings mask */
546 case NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK:
547 NG_MKRESPONSE(rsp, msg, sizeof(unit->link_policy_mask),
554 *((ng_hci_node_link_policy_mask_ep *)(rsp->data)) =
555 unit->link_policy_mask;
558 /* Set link policy settings mask */
559 case NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK:
560 if (msg->header.arglen !=
561 sizeof(ng_hci_node_link_policy_mask_ep)) {
566 unit->link_policy_mask =
567 *((ng_hci_node_link_policy_mask_ep *)
571 /* Get packet mask */
572 case NGM_HCI_NODE_GET_PACKET_MASK:
573 NG_MKRESPONSE(rsp, msg, sizeof(unit->packet_mask),
580 *((ng_hci_node_packet_mask_ep *)(rsp->data)) =
584 /* Set packet mask */
585 case NGM_HCI_NODE_SET_PACKET_MASK:
586 if (msg->header.arglen !=
587 sizeof(ng_hci_node_packet_mask_ep)) {
593 *((ng_hci_node_packet_mask_ep *)(msg->data));
596 /* Get role switch */
597 case NGM_HCI_NODE_GET_ROLE_SWITCH:
598 NG_MKRESPONSE(rsp, msg, sizeof(unit->role_switch),
605 *((ng_hci_node_role_switch_ep *)(rsp->data)) =
609 /* Set role switch */
610 case NGM_HCI_NODE_SET_ROLE_SWITCH:
611 if (msg->header.arglen !=
612 sizeof(ng_hci_node_role_switch_ep)) {
618 *((ng_hci_node_role_switch_ep *)(msg->data));
632 /* NG_RESPOND_MSG should take care of "item" and "rsp" */
633 NG_RESPOND_MSG(error, node, item, rsp);
637 } /* ng_hci_default_rcvmsg */
640 * Process control message from upstream hooks (ACL and SCO).
641 * Handle LP_xxx messages here, give everything else to default routine.
645 ng_hci_upper_rcvmsg(node_p node, item_p item, hook_p lasthook)
647 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);
650 switch (NGI_MSG(item)->header.typecookie) {
652 switch (NGI_MSG(item)->header.cmd) {
653 case NGM_HCI_LP_CON_REQ:
654 error = ng_hci_lp_con_req(unit, item, lasthook);
657 case NGM_HCI_LP_DISCON_REQ: /* XXX not defined by specs */
658 error = ng_hci_lp_discon_req(unit, item, lasthook);
661 case NGM_HCI_LP_CON_RSP:
662 error = ng_hci_lp_con_rsp(unit, item, lasthook);
665 case NGM_HCI_LP_QOS_REQ:
666 error = ng_hci_lp_qos_req(unit, item, lasthook);
670 error = ng_hci_default_rcvmsg(node, item, lasthook);
676 error = ng_hci_default_rcvmsg(node, item, lasthook);
681 } /* ng_hci_upper_rcvmsg */
684 * Process data packet from the driver hook.
685 * We expect HCI events, ACL or SCO data packets.
689 ng_hci_drv_rcvdata(hook_p hook, item_p item)
691 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
692 struct mbuf *m = NULL;
696 m = NGI_M(item); /* item still has mbuf, just peeking */
697 m->m_flags |= M_PROTO1; /* mark as incoming packet */
699 NG_HCI_STAT_BYTES_RECV(unit->stat, m->m_pkthdr.len);
701 /* Give copy packet to RAW hook */
702 ng_hci_mtap(unit, m);
706 * Lower layer drivers MUST NOT send mbuf chain with empty mbuf at
707 * the beginning of the chain. HCI layer WILL NOT call m_pullup() here.
710 switch (*mtod(m, u_int8_t *)) {
711 case NG_HCI_ACL_DATA_PKT:
712 NG_HCI_STAT_ACL_RECV(unit->stat);
714 if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY ||
715 unit->acl == NULL || NG_HOOK_NOT_VALID(unit->acl)) {
717 "%s: %s - could not forward HCI ACL data packet, state=%#x, hook=%p\n",
718 __func__, NG_NODE_NAME(unit->node),
719 unit->state, unit->acl);
723 NG_FWD_ITEM_HOOK(error, item, unit->acl);
726 case NG_HCI_SCO_DATA_PKT:
727 NG_HCI_STAT_SCO_RECV(unit->stat);
729 if ((unit->state & NG_HCI_UNIT_READY) != NG_HCI_UNIT_READY ||
730 unit->sco == NULL || NG_HOOK_NOT_VALID(unit->sco)) {
732 "%s: %s - could not forward HCI SCO data packet, state=%#x, hook=%p\n",
733 __func__, NG_NODE_NAME(unit->node),
734 unit->state, unit->sco);
738 NG_FWD_ITEM_HOOK(error, item, unit->sco);
741 case NG_HCI_EVENT_PKT:
742 NG_HCI_STAT_EVNT_RECV(unit->stat);
744 /* Detach mbuf, discard item and process event */
748 error = ng_hci_process_event(unit, m);
753 "%s: %s - got unknown HCI packet type=%#x\n",
754 __func__, NG_NODE_NAME(unit->node),
755 *mtod(m, u_int8_t *));
764 } /* ng_hci_drv_rcvdata */
767 * Process data packet from ACL upstream hook.
768 * We expect valid HCI ACL data packets.
772 ng_hci_acl_rcvdata(hook_p hook, item_p item)
774 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
775 struct mbuf *m = NULL;
776 ng_hci_unit_con_p con = NULL;
777 u_int16_t con_handle;
780 NG_HCI_BUFF_ACL_SIZE(unit->buffer, size);
785 if (*mtod(m, u_int8_t *) != NG_HCI_ACL_DATA_PKT) {
787 "%s: %s - invalid HCI data packet type=%#x\n",
788 __func__, NG_NODE_NAME(unit->node),
789 *mtod(m, u_int8_t *));
795 if (m->m_pkthdr.len < sizeof(ng_hci_acldata_pkt_t) ||
796 m->m_pkthdr.len > sizeof(ng_hci_acldata_pkt_t) + size) {
798 "%s: %s - invalid HCI ACL data packet, len=%d, mtu=%d\n",
799 __func__, NG_NODE_NAME(unit->node),
800 m->m_pkthdr.len, size);
806 NG_HCI_M_PULLUP(m, sizeof(ng_hci_acldata_pkt_t));
812 con_handle = NG_HCI_CON_HANDLE(le16toh(
813 mtod(m, ng_hci_acldata_pkt_t *)->con_handle));
814 size = le16toh(mtod(m, ng_hci_acldata_pkt_t *)->length);
816 if (m->m_pkthdr.len != sizeof(ng_hci_acldata_pkt_t) + size) {
818 "%s: %s - invalid HCI ACL data packet size, len=%d, length=%d\n",
819 __func__, NG_NODE_NAME(unit->node),
820 m->m_pkthdr.len, size);
827 con = ng_hci_con_by_handle(unit, con_handle);
830 "%s: %s - unexpected HCI ACL data packet. Connection does not exists, " \
831 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node), con_handle);
837 if (con->link_type != NG_HCI_LINK_ACL) {
839 "%s: %s - unexpected HCI ACL data packet. Not ACL link, con_handle=%d, " \
840 "link_type=%d\n", __func__, NG_NODE_NAME(unit->node),
841 con_handle, con->link_type);
847 if (con->state != NG_HCI_CON_OPEN) {
849 "%s: %s - unexpected HCI ACL data packet. Invalid connection state=%d, " \
850 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node),
851 con->state, con_handle);
857 if (NG_BT_ITEMQ_FULL(&con->conq)) {
859 "%s: %s - dropping HCI ACL data packet, con_handle=%d, len=%d, queue_len=%d\n",
860 __func__, NG_NODE_NAME(unit->node), con_handle,
861 m->m_pkthdr.len, NG_BT_ITEMQ_LEN(&con->conq));
863 NG_BT_ITEMQ_DROP(&con->conq);
869 /* Queue item and schedule data transfer */
871 NG_BT_ITEMQ_ENQUEUE(&con->conq, item);
875 ng_hci_send_data(unit);
880 NG_FREE_M(m); /* NG_FREE_M() checks for m != NULL */
883 } /* ng_hci_acl_rcvdata */
886 * Process data packet from SCO upstream hook.
887 * We expect valid HCI SCO data packets
891 ng_hci_sco_rcvdata(hook_p hook, item_p item)
893 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
894 struct mbuf *m = NULL;
895 ng_hci_unit_con_p con = NULL;
896 u_int16_t con_handle;
899 NG_HCI_BUFF_SCO_SIZE(unit->buffer, size);
904 if (*mtod(m, u_int8_t *) != NG_HCI_SCO_DATA_PKT) {
906 "%s: %s - invalid HCI data packet type=%#x\n",
907 __func__, NG_NODE_NAME(unit->node),
908 *mtod(m, u_int8_t *));
914 if (m->m_pkthdr.len < sizeof(ng_hci_scodata_pkt_t) ||
915 m->m_pkthdr.len > sizeof(ng_hci_scodata_pkt_t) + size) {
917 "%s: %s - invalid HCI SCO data packet, len=%d, mtu=%d\n",
918 __func__, NG_NODE_NAME(unit->node),
919 m->m_pkthdr.len, size);
925 NG_HCI_M_PULLUP(m, sizeof(ng_hci_scodata_pkt_t));
931 con_handle = NG_HCI_CON_HANDLE(le16toh(
932 mtod(m, ng_hci_scodata_pkt_t *)->con_handle));
933 size = mtod(m, ng_hci_scodata_pkt_t *)->length;
935 if (m->m_pkthdr.len != sizeof(ng_hci_scodata_pkt_t) + size) {
937 "%s: %s - invalid HCI SCO data packet size, len=%d, length=%d\n",
938 __func__, NG_NODE_NAME(unit->node),
939 m->m_pkthdr.len, size);
946 con = ng_hci_con_by_handle(unit, con_handle);
949 "%s: %s - unexpected HCI SCO data packet. Connection does not exists, " \
950 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node), con_handle);
956 if (con->link_type != NG_HCI_LINK_SCO) {
958 "%s: %s - unexpected HCI SCO data packet. Not SCO link, con_handle=%d, " \
959 "link_type=%d\n", __func__, NG_NODE_NAME(unit->node),
960 con_handle, con->link_type);
966 if (con->state != NG_HCI_CON_OPEN) {
968 "%s: %s - unexpected HCI SCO data packet. Invalid connection state=%d, " \
969 "con_handle=%d\n", __func__, NG_NODE_NAME(unit->node),
970 con->state, con_handle);
976 if (NG_BT_ITEMQ_FULL(&con->conq)) {
978 "%s: %s - dropping HCI SCO data packet, con_handle=%d, len=%d, queue_len=%d\n",
979 __func__, NG_NODE_NAME(unit->node), con_handle,
980 m->m_pkthdr.len, NG_BT_ITEMQ_LEN(&con->conq));
982 NG_BT_ITEMQ_DROP(&con->conq);
988 /* Queue item and schedule data transfer */
990 NG_BT_ITEMQ_ENQUEUE(&con->conq, item);
994 ng_hci_send_data(unit);
999 NG_FREE_M(m); /* NG_FREE_M() checks for m != NULL */
1002 } /* ng_hci_sco_rcvdata */
1005 * Process data packet from uptream RAW hook.
1006 * We expect valid HCI command packets.
1010 ng_hci_raw_rcvdata(hook_p hook, item_p item)
1012 ng_hci_unit_p unit = (ng_hci_unit_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1013 struct mbuf *m = NULL;
1020 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1022 "%s: %s - invalid HCI command packet type=%#x\n",
1023 __func__, NG_NODE_NAME(unit->node),
1024 *mtod(m, u_int8_t *));
1030 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t)) {
1032 "%s: %s - invalid HCI command packet len=%d\n",
1033 __func__, NG_NODE_NAME(unit->node), m->m_pkthdr.len);
1039 NG_HCI_M_PULLUP(m, sizeof(ng_hci_cmd_pkt_t));
1045 if (m->m_pkthdr.len !=
1046 mtod(m, ng_hci_cmd_pkt_t *)->length + sizeof(ng_hci_cmd_pkt_t)) {
1048 "%s: %s - invalid HCI command packet size, len=%d, length=%d\n",
1049 __func__, NG_NODE_NAME(unit->node), m->m_pkthdr.len,
1050 mtod(m, ng_hci_cmd_pkt_t *)->length);
1056 if (mtod(m, ng_hci_cmd_pkt_t *)->opcode == 0) {
1058 "%s: %s - invalid HCI command opcode\n",
1059 __func__, NG_NODE_NAME(unit->node));
1065 if (NG_BT_MBUFQ_FULL(&unit->cmdq)) {
1067 "%s: %s - dropping HCI command packet, len=%d, queue_len=%d\n",
1068 __func__, NG_NODE_NAME(unit->node), m->m_pkthdr.len,
1069 NG_BT_MBUFQ_LEN(&unit->cmdq));
1071 NG_BT_MBUFQ_DROP(&unit->cmdq);
1077 /* Queue and send command */
1078 NG_BT_MBUFQ_ENQUEUE(&unit->cmdq, m);
1081 if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
1082 error = ng_hci_send_command(unit);
1084 NG_FREE_M(m); /* NG_FREE_M() checks for m != NULL */
1087 } /* ng_hci_raw_rcvdata */