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_evnt.c,v 1.6 2003/09/08 18:57:51 max Exp $
31 * $FreeBSD: src/sys/netgraph/bluetooth/hci/ng_hci_evnt.c,v 1.8 2005/01/07 01:45:43 imp 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 <sys/refcount.h>
42 #include <netgraph7/ng_message.h>
43 #include <netgraph7/netgraph.h>
44 #include <netgraph7/netgraph2.h>
45 #include <netgraph7/bluetooth/include/ng_bluetooth.h>
46 #include <netgraph7/bluetooth/include/ng_hci.h>
47 #include <netgraph7/bluetooth/hci/ng_hci_var.h>
48 #include <netgraph7/bluetooth/hci/ng_hci_cmds.h>
49 #include <netgraph7/bluetooth/hci/ng_hci_evnt.h>
50 #include <netgraph7/bluetooth/hci/ng_hci_ulpi.h>
51 #include <netgraph7/bluetooth/hci/ng_hci_misc.h>
53 /******************************************************************************
54 ******************************************************************************
55 ** HCI event processing module
56 ******************************************************************************
57 ******************************************************************************/
60 * Event processing routines
63 static int inquiry_result (ng_hci_unit_p, struct mbuf *);
64 static int con_compl (ng_hci_unit_p, struct mbuf *);
65 static int con_req (ng_hci_unit_p, struct mbuf *);
66 static int discon_compl (ng_hci_unit_p, struct mbuf *);
67 static int encryption_change (ng_hci_unit_p, struct mbuf *);
68 static int read_remote_features_compl (ng_hci_unit_p, struct mbuf *);
69 static int qos_setup_compl (ng_hci_unit_p, struct mbuf *);
70 static int hardware_error (ng_hci_unit_p, struct mbuf *);
71 static int role_change (ng_hci_unit_p, struct mbuf *);
72 static int num_compl_pkts (ng_hci_unit_p, struct mbuf *);
73 static int mode_change (ng_hci_unit_p, struct mbuf *);
74 static int data_buffer_overflow (ng_hci_unit_p, struct mbuf *);
75 static int read_clock_offset_compl (ng_hci_unit_p, struct mbuf *);
76 static int qos_violation (ng_hci_unit_p, struct mbuf *);
77 static int page_scan_mode_change (ng_hci_unit_p, struct mbuf *);
78 static int page_scan_rep_mode_change (ng_hci_unit_p, struct mbuf *);
79 static int sync_con_queue (ng_hci_unit_p, ng_hci_unit_con_p, int);
80 static int send_data_packets (ng_hci_unit_p, int, int);
83 * Process HCI event packet
87 ng_hci_process_event(ng_hci_unit_p unit, struct mbuf *event)
89 ng_hci_event_pkt_t *hdr = NULL;
92 /* Get event packet header */
93 NG_HCI_M_PULLUP(event, sizeof(*hdr));
97 hdr = mtod(event, ng_hci_event_pkt_t *);
100 "%s: %s - got HCI event=%#x, length=%d\n",
101 __func__, NG_NODE_NAME(unit->node), hdr->event, hdr->length);
103 /* Get rid of event header and process event */
104 m_adj(event, sizeof(*hdr));
106 switch (hdr->event) {
107 case NG_HCI_EVENT_INQUIRY_COMPL:
108 case NG_HCI_EVENT_RETURN_LINK_KEYS:
109 case NG_HCI_EVENT_PIN_CODE_REQ:
110 case NG_HCI_EVENT_LINK_KEY_REQ:
111 case NG_HCI_EVENT_LINK_KEY_NOTIFICATION:
112 case NG_HCI_EVENT_LOOPBACK_COMMAND:
113 case NG_HCI_EVENT_AUTH_COMPL:
114 case NG_HCI_EVENT_CHANGE_CON_LINK_KEY_COMPL:
115 case NG_HCI_EVENT_MASTER_LINK_KEY_COMPL:
116 case NG_HCI_EVENT_FLUSH_OCCUR: /* XXX Do we have to handle it? */
117 case NG_HCI_EVENT_MAX_SLOT_CHANGE:
118 case NG_HCI_EVENT_CON_PKT_TYPE_CHANGED:
119 case NG_HCI_EVENT_BT_LOGO:
120 case NG_HCI_EVENT_VENDOR:
121 case NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL:
122 case NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL:
123 /* These do not need post processing */
127 case NG_HCI_EVENT_INQUIRY_RESULT:
128 error = inquiry_result(unit, event);
131 case NG_HCI_EVENT_CON_COMPL:
132 error = con_compl(unit, event);
135 case NG_HCI_EVENT_CON_REQ:
136 error = con_req(unit, event);
139 case NG_HCI_EVENT_DISCON_COMPL:
140 error = discon_compl(unit, event);
143 case NG_HCI_EVENT_ENCRYPTION_CHANGE:
144 error = encryption_change(unit, event);
147 case NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL:
148 error = read_remote_features_compl(unit, event);
151 case NG_HCI_EVENT_QOS_SETUP_COMPL:
152 error = qos_setup_compl(unit, event);
155 case NG_HCI_EVENT_COMMAND_COMPL:
156 error = ng_hci_process_command_complete(unit, event);
159 case NG_HCI_EVENT_COMMAND_STATUS:
160 error = ng_hci_process_command_status(unit, event);
163 case NG_HCI_EVENT_HARDWARE_ERROR:
164 error = hardware_error(unit, event);
167 case NG_HCI_EVENT_ROLE_CHANGE:
168 error = role_change(unit, event);
171 case NG_HCI_EVENT_NUM_COMPL_PKTS:
172 error = num_compl_pkts(unit, event);
175 case NG_HCI_EVENT_MODE_CHANGE:
176 error = mode_change(unit, event);
179 case NG_HCI_EVENT_DATA_BUFFER_OVERFLOW:
180 error = data_buffer_overflow(unit, event);
183 case NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL:
184 error = read_clock_offset_compl(unit, event);
187 case NG_HCI_EVENT_QOS_VIOLATION:
188 error = qos_violation(unit, event);
191 case NG_HCI_EVENT_PAGE_SCAN_MODE_CHANGE:
192 error = page_scan_mode_change(unit, event);
195 case NG_HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE:
196 error = page_scan_rep_mode_change(unit, event);
206 } /* ng_hci_process_event */
209 * Send ACL and/or SCO data to the unit driver
213 ng_hci_send_data(ng_hci_unit_p unit)
218 NG_HCI_BUFF_ACL_AVAIL(unit->buffer, count);
221 "%s: %s - sending ACL data packets, count=%d\n",
222 __func__, NG_NODE_NAME(unit->node), count);
225 count = send_data_packets(unit, NG_HCI_LINK_ACL, count);
226 NG_HCI_STAT_ACL_SENT(unit->stat, count);
227 NG_HCI_BUFF_ACL_USE(unit->buffer, count);
231 NG_HCI_BUFF_SCO_AVAIL(unit->buffer, count);
234 "%s: %s - sending SCO data packets, count=%d\n",
235 __func__, NG_NODE_NAME(unit->node), count);
238 count = send_data_packets(unit, NG_HCI_LINK_SCO, count);
239 NG_HCI_STAT_SCO_SENT(unit->stat, count);
240 NG_HCI_BUFF_SCO_USE(unit->buffer, count);
242 } /* ng_hci_send_data */
245 * Send data packets to the lower layer.
249 send_data_packets(ng_hci_unit_p unit, int link_type, int limit)
251 ng_hci_unit_con_p con = NULL, winner = NULL;
252 item_p item = NULL, item2;
253 int min_pending, total_sent, sent, error, v;
255 for (total_sent = 0; limit > 0; ) {
256 min_pending = 0x0fffffff;
260 * Find the connection that has has data to send
261 * and the smallest number of pending packets
264 LIST_FOREACH(con, &unit->con_list, next) {
265 if (con->link_type != link_type)
267 if (NG_BT_ITEMQ_LEN(&con->conq) == 0)
270 if (con->pending < min_pending) {
272 min_pending = con->pending;
280 * OK, we have a winner now send as much packets as we can
281 * Count the number of packets we have sent and then sync
282 * winner connection queue.
285 for (sent = 0; limit > 0; limit --, total_sent ++, sent ++) {
286 NG_BT_ITEMQ_DEQUEUE(&winner->conq, item);
292 "%s: %s - sending data packet, handle=%d, len=%d\n",
293 __func__, NG_NODE_NAME(unit->node),
294 winner->con_handle, NGI_M(item)->m_pkthdr.len);
296 /* Check if driver hook still there */
297 v = (unit->drv != NULL && NG_HOOK_IS_VALID(unit->drv));
298 if (!v || (unit->state & NG_HCI_UNIT_READY) !=
301 "%s: %s - could not send data. Hook \"%s\" is %svalid, state=%#x\n",
302 __func__, NG_NODE_NAME(unit->node),
303 NG_HCI_HOOK_DRV, ((v)? "" : "not "),
309 v = NGI_M(item)->m_pkthdr.len;
311 /* Give packet to raw hook */
312 ng_hci_mtap(unit, NGI_M(item));
314 /* ... and forward item to the driver */
315 NG_FWD_ITEM_HOOK(error, item, unit->drv);
317 ng_unref_item(item2, error);
321 "%s: %s - could not send data packet, handle=%d, error=%d\n",
322 __func__, NG_NODE_NAME(unit->node),
323 winner->con_handle, error);
328 NG_HCI_STAT_BYTES_SENT(unit->stat, v);
332 * Sync connection queue for the winner
335 sync_con_queue(unit, winner, sent);
339 } /* send_data_packets */
342 * Send flow control messages to the upper layer
346 sync_con_queue(ng_hci_unit_p unit, ng_hci_unit_con_p con, int completed)
349 struct ng_mesg *msg = NULL;
350 ng_hci_sync_con_queue_ep *state = NULL;
353 hook = (con->link_type == NG_HCI_LINK_ACL)? unit->acl : unit->sco;
354 if (hook == NULL || NG_HOOK_NOT_VALID(hook))
357 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_SYNC_CON_QUEUE,
358 sizeof(*state), M_WAITOK | M_NULLOK);
362 state = (ng_hci_sync_con_queue_ep *)(msg->data);
363 state->con_handle = con->con_handle;
364 state->completed = completed;
366 NG_SEND_MSG_HOOK(error, unit->node, msg, hook, 0);
369 } /* sync_con_queue */
371 /* Inquiry result event */
373 inquiry_result(ng_hci_unit_p unit, struct mbuf *event)
375 ng_hci_inquiry_result_ep *ep = NULL;
376 ng_hci_neighbor_p n = NULL;
380 NG_HCI_M_PULLUP(event, sizeof(*ep));
384 ep = mtod(event, ng_hci_inquiry_result_ep *);
385 m_adj(event, sizeof(*ep));
387 for (; ep->num_responses > 0; ep->num_responses --) {
388 /* Get remote unit address */
389 m_copydata(event, 0, sizeof(bdaddr), (caddr_t) &bdaddr);
390 m_adj(event, sizeof(bdaddr));
392 /* Lookup entry in the cache */
393 n = ng_hci_get_neighbor(unit, &bdaddr);
395 /* Create new entry */
396 n = ng_hci_new_neighbor(unit);
402 getmicrotime(&n->updated);
404 bcopy(&bdaddr, &n->bdaddr, sizeof(n->bdaddr));
406 /* XXX call m_pullup here? */
408 n->page_scan_rep_mode = *mtod(event, u_int8_t *);
409 m_adj(event, sizeof(u_int8_t));
411 /* page_scan_period_mode */
412 m_adj(event, sizeof(u_int8_t));
414 n->page_scan_mode = *mtod(event, u_int8_t *);
415 m_adj(event, sizeof(u_int8_t));
418 m_adj(event, NG_HCI_CLASS_SIZE);
421 m_copydata(event, 0, sizeof(n->clock_offset),
422 (caddr_t) &n->clock_offset);
423 n->clock_offset = le16toh(n->clock_offset);
429 } /* inquiry_result */
431 /* Connection complete event */
433 con_compl(ng_hci_unit_p unit, struct mbuf *event)
435 ng_hci_con_compl_ep *ep = NULL;
436 ng_hci_unit_con_p con = NULL;
439 NG_HCI_M_PULLUP(event, sizeof(*ep));
443 ep = mtod(event, ng_hci_con_compl_ep *);
446 * Find the first connection descriptor that matches the following:
448 * 1) con->link_type == ep->link_type
449 * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
450 * 3) con->bdaddr == ep->bdaddr
453 LIST_FOREACH(con, &unit->con_list, next)
454 if (con->link_type == ep->link_type &&
455 con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
456 bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
460 * Two possible cases:
462 * 1) We have found connection descriptor. That means upper layer has
463 * requested this connection via LP_CON_REQ message. In this case
464 * connection must have timeout set. If ng_hci_con_untimeout() fails
465 * then timeout message already went into node's queue. In this case
466 * ignore Connection_Complete event and let timeout deal with it.
468 * 2) We do not have connection descriptor. That means upper layer
469 * nas not requested this connection or (less likely) we gave up
470 * on this connection (timeout). The most likely scenario is that
471 * we have received Create_Connection/Add_SCO_Connection command
479 con = ng_hci_new_con(unit, ep->link_type);
485 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
486 } else if ((error = ng_hci_con_untimeout(con)) != 0)
490 * Update connection descriptor and send notification
491 * to the upper layers.
494 con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
495 con->encryption_mode = ep->encryption_mode;
497 ng_hci_lp_con_cfm(con, ep->status);
499 /* Adjust connection state */
501 ng_hci_free_con(con);
503 con->state = NG_HCI_CON_OPEN;
506 * Change link policy for the ACL connections. Enable all
507 * supported link modes. Enable Role switch as well if
508 * device supports it.
511 if (ep->link_type == NG_HCI_LINK_ACL) {
512 struct __link_policy {
513 ng_hci_cmd_pkt_t hdr;
514 ng_hci_write_link_policy_settings_cp cp;
515 } __attribute__ ((packed)) *lp;
518 MGETHDR(m, M_NOWAIT, MT_DATA);
520 m->m_pkthdr.len = m->m_len = sizeof(*lp);
521 lp = mtod(m, struct __link_policy *);
523 lp->hdr.type = NG_HCI_CMD_PKT;
524 lp->hdr.opcode = htole16(NG_HCI_OPCODE(
525 NG_HCI_OGF_LINK_POLICY,
526 NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS));
527 lp->hdr.length = sizeof(lp->cp);
529 lp->cp.con_handle = ep->con_handle;
532 if ((unit->features[0] & NG_HCI_LMP_SWITCH) &&
534 lp->cp.settings |= 0x1;
535 if (unit->features[0] & NG_HCI_LMP_HOLD_MODE)
536 lp->cp.settings |= 0x2;
537 if (unit->features[0] & NG_HCI_LMP_SNIFF_MODE)
538 lp->cp.settings |= 0x4;
539 if (unit->features[1] & NG_HCI_LMP_PARK_MODE)
540 lp->cp.settings |= 0x8;
542 lp->cp.settings &= unit->link_policy_mask;
543 lp->cp.settings = htole16(lp->cp.settings);
545 NG_BT_MBUFQ_ENQUEUE(&unit->cmdq, m);
546 if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
547 ng_hci_send_command(unit);
557 /* Connection request event */
559 con_req(ng_hci_unit_p unit, struct mbuf *event)
561 ng_hci_con_req_ep *ep = NULL;
562 ng_hci_unit_con_p con = NULL;
565 NG_HCI_M_PULLUP(event, sizeof(*ep));
569 ep = mtod(event, ng_hci_con_req_ep *);
572 * Find the first connection descriptor that matches the following:
574 * 1) con->link_type == ep->link_type
576 * 2) con->state == NG_HCI_CON_W4_LP_CON_RSP ||
577 * con->state == NG_HCI_CON_W4_CONN_COMPL
579 * 3) con->bdaddr == ep->bdaddr
583 * 1) We do not have connection descriptor. This is simple. Create
584 * new fresh connection descriptor and send notification to the
585 * appropriate upstream hook (based on link_type).
587 * 2) We found connection handle. This is more complicated.
591 * Since only one ACL link can exist between each pair of
592 * units then we have a race. Our upper layer has requested
593 * an ACL connection to the remote unit, but we did not send
594 * command yet. At the same time the remote unit has requested
595 * an ACL connection from us. In this case we will ignore
596 * Connection_Request event. This probably will cause connect
597 * failure on both units.
601 * The spec on page 45 says :
603 * "The master can support up to three SCO links to the same
604 * slave or to different slaves. A slave can support up to
605 * three SCO links from the same master, or two SCO links if
606 * the links originate from different masters."
608 * The only problem is how to handle multiple SCO links between
609 * matster and slave. For now we will assume that multiple SCO
610 * links MUST be opened one after another.
613 LIST_FOREACH(con, &unit->con_list, next)
614 if (con->link_type == ep->link_type &&
615 (con->state == NG_HCI_CON_W4_LP_CON_RSP ||
616 con->state == NG_HCI_CON_W4_CONN_COMPLETE) &&
617 bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
621 con = ng_hci_new_con(unit, ep->link_type);
623 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
625 con->state = NG_HCI_CON_W4_LP_CON_RSP;
626 ng_hci_con_timeout(con);
628 error = ng_hci_lp_con_ind(con, ep->uclass);
630 ng_hci_con_untimeout(con);
631 ng_hci_free_con(con);
642 /* Disconnect complete event */
644 discon_compl(ng_hci_unit_p unit, struct mbuf *event)
646 ng_hci_discon_compl_ep *ep = NULL;
647 ng_hci_unit_con_p con = NULL;
651 NG_HCI_M_PULLUP(event, sizeof(*ep));
655 ep = mtod(event, ng_hci_discon_compl_ep *);
659 * Do we have to send notification if ep->status != 0?
660 * For now we will send notification for both ACL and SCO connections
661 * ONLY if ep->status == 0.
664 if (ep->status == 0) {
665 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
666 con = ng_hci_con_by_handle(unit, h);
668 error = ng_hci_lp_discon_ind(con, ep->reason);
670 /* Remove all timeouts (if any) */
671 if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
672 ng_hci_con_untimeout(con);
674 ng_hci_free_con(con);
677 "%s: %s - invalid connection handle=%d\n",
678 __func__, NG_NODE_NAME(unit->node), h);
688 /* Encryption change event */
690 encryption_change(ng_hci_unit_p unit, struct mbuf *event)
692 ng_hci_encryption_change_ep *ep = NULL;
693 ng_hci_unit_con_p con = NULL;
696 NG_HCI_M_PULLUP(event, sizeof(*ep));
700 ep = mtod(event, ng_hci_encryption_change_ep *);
702 if (ep->status == 0) {
703 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
705 con = ng_hci_con_by_handle(unit, h);
708 "%s: %s - invalid connection handle=%d\n",
709 __func__, NG_NODE_NAME(unit->node), h);
711 } else if (con->link_type != NG_HCI_LINK_ACL) {
713 "%s: %s - invalid link type=%d\n",
714 __func__, NG_NODE_NAME(unit->node),
717 } else if (ep->encryption_enable)
718 /* XXX is that true? */
719 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_P2P;
721 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
724 "%s: %s - failed to change encryption mode, status=%d\n",
725 __func__, NG_NODE_NAME(unit->node), ep->status);
730 } /* encryption_change */
732 /* Read remote feature complete event */
734 read_remote_features_compl(ng_hci_unit_p unit, struct mbuf *event)
736 ng_hci_read_remote_features_compl_ep *ep = NULL;
737 ng_hci_unit_con_p con = NULL;
738 ng_hci_neighbor_p n = NULL;
742 NG_HCI_M_PULLUP(event, sizeof(*ep));
746 ep = mtod(event, ng_hci_read_remote_features_compl_ep *);
748 if (ep->status == 0) {
749 /* Check if we have this connection handle */
750 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
751 con = ng_hci_con_by_handle(unit, h);
754 "%s: %s - invalid connection handle=%d\n",
755 __func__, NG_NODE_NAME(unit->node), h);
760 /* Update cache entry */
761 n = ng_hci_get_neighbor(unit, &con->bdaddr);
763 n = ng_hci_new_neighbor(unit);
769 bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
771 getmicrotime(&n->updated);
773 bcopy(ep->features, n->features, sizeof(n->features));
776 "%s: %s - failed to read remote unit features, status=%d\n",
777 __func__, NG_NODE_NAME(unit->node), ep->status);
782 } /* read_remote_features_compl */
784 /* QoS setup complete event */
786 qos_setup_compl(ng_hci_unit_p unit, struct mbuf *event)
788 ng_hci_qos_setup_compl_ep *ep = NULL;
789 ng_hci_unit_con_p con = NULL;
793 NG_HCI_M_PULLUP(event, sizeof(*ep));
797 ep = mtod(event, ng_hci_qos_setup_compl_ep *);
799 /* Check if we have this connection handle */
800 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
801 con = ng_hci_con_by_handle(unit, h);
804 "%s: %s - invalid connection handle=%d\n",
805 __func__, NG_NODE_NAME(unit->node), h);
807 } else if (con->link_type != NG_HCI_LINK_ACL) {
809 "%s: %s - invalid link type=%d, handle=%d\n",
810 __func__, NG_NODE_NAME(unit->node), con->link_type, h);
812 } else if (con->state != NG_HCI_CON_OPEN) {
814 "%s: %s - invalid connection state=%d, handle=%d\n",
815 __func__, NG_NODE_NAME(unit->node),
818 } else /* Notify upper layer */
819 error = ng_hci_lp_qos_cfm(con, ep->status);
824 } /* qos_setup_compl */
826 /* Hardware error event */
828 hardware_error(ng_hci_unit_p unit, struct mbuf *event)
831 "%s: %s - hardware error %#x\n",
832 __func__, NG_NODE_NAME(unit->node), *mtod(event, u_int8_t *));
837 } /* hardware_error */
839 /* Role change event */
841 role_change(ng_hci_unit_p unit, struct mbuf *event)
843 ng_hci_role_change_ep *ep = NULL;
844 ng_hci_unit_con_p con = NULL;
846 NG_HCI_M_PULLUP(event, sizeof(*ep));
850 ep = mtod(event, ng_hci_role_change_ep *);
852 if (ep->status == 0) {
853 /* XXX shoud we also change "role" for SCO connections? */
854 con = ng_hci_con_by_bdaddr(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
856 con->role = ep->role;
859 "%s: %s - ACL connection does not exist, bdaddr=%x:%x:%x:%x:%x:%x\n",
860 __func__, NG_NODE_NAME(unit->node),
861 ep->bdaddr.b[5], ep->bdaddr.b[4],
862 ep->bdaddr.b[3], ep->bdaddr.b[2],
863 ep->bdaddr.b[1], ep->bdaddr.b[0]);
866 "%s: %s - failed to change role, status=%d, bdaddr=%x:%x:%x:%x:%x:%x\n",
867 __func__, NG_NODE_NAME(unit->node), ep->status,
868 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
869 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
876 /* Number of completed packets event */
878 num_compl_pkts(ng_hci_unit_p unit, struct mbuf *event)
880 ng_hci_num_compl_pkts_ep *ep = NULL;
881 ng_hci_unit_con_p con = NULL;
884 NG_HCI_M_PULLUP(event, sizeof(*ep));
888 ep = mtod(event, ng_hci_num_compl_pkts_ep *);
889 m_adj(event, sizeof(*ep));
891 for (; ep->num_con_handles > 0; ep->num_con_handles --) {
892 /* Get connection handle */
893 m_copydata(event, 0, sizeof(h), (caddr_t) &h);
894 m_adj(event, sizeof(h));
895 h = NG_HCI_CON_HANDLE(le16toh(h));
897 /* Get number of completed packets */
898 m_copydata(event, 0, sizeof(p), (caddr_t) &p);
899 m_adj(event, sizeof(p));
902 /* Check if we have this connection handle */
903 con = ng_hci_con_by_handle(unit, h);
906 if (con->pending < 0) {
908 "%s: %s - pending packet counter is out of sync! " \
909 "handle=%d, pending=%d, ncp=%d\n", __func__, NG_NODE_NAME(unit->node),
910 con->con_handle, con->pending, p);
915 /* Update buffer descriptor */
916 if (con->link_type == NG_HCI_LINK_ACL)
917 NG_HCI_BUFF_ACL_FREE(unit->buffer, p);
919 NG_HCI_BUFF_SCO_FREE(unit->buffer, p);
922 "%s: %s - invalid connection handle=%d\n",
923 __func__, NG_NODE_NAME(unit->node), h);
929 ng_hci_send_data(unit);
932 } /* num_compl_pkts */
934 /* Mode change event */
936 mode_change(ng_hci_unit_p unit, struct mbuf *event)
938 ng_hci_mode_change_ep *ep = NULL;
939 ng_hci_unit_con_p con = NULL;
942 NG_HCI_M_PULLUP(event, sizeof(*ep));
946 ep = mtod(event, ng_hci_mode_change_ep *);
948 if (ep->status == 0) {
949 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
951 con = ng_hci_con_by_handle(unit, h);
954 "%s: %s - invalid connection handle=%d\n",
955 __func__, NG_NODE_NAME(unit->node), h);
957 } else if (con->link_type != NG_HCI_LINK_ACL) {
959 "%s: %s - invalid link type=%d\n",
960 __func__, NG_NODE_NAME(unit->node),
964 con->mode = ep->unit_mode;
967 "%s: %s - failed to change mode, status=%d\n",
968 __func__, NG_NODE_NAME(unit->node), ep->status);
975 /* Data buffer overflow event */
977 data_buffer_overflow(ng_hci_unit_p unit, struct mbuf *event)
980 "%s: %s - %s data buffer overflow\n",
981 __func__, NG_NODE_NAME(unit->node),
982 (*mtod(event, u_int8_t *) == NG_HCI_LINK_ACL)? "ACL" : "SCO");
987 } /* data_buffer_overflow */
989 /* Read clock offset complete event */
991 read_clock_offset_compl(ng_hci_unit_p unit, struct mbuf *event)
993 ng_hci_read_clock_offset_compl_ep *ep = NULL;
994 ng_hci_unit_con_p con = NULL;
995 ng_hci_neighbor_p n = NULL;
998 NG_HCI_M_PULLUP(event, sizeof(*ep));
1002 ep = mtod(event, ng_hci_read_clock_offset_compl_ep *);
1004 if (ep->status == 0) {
1005 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1007 con = ng_hci_con_by_handle(unit, h);
1010 "%s: %s - invalid connection handle=%d\n",
1011 __func__, NG_NODE_NAME(unit->node), h);
1016 /* Update cache entry */
1017 n = ng_hci_get_neighbor(unit, &con->bdaddr);
1019 n = ng_hci_new_neighbor(unit);
1025 bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1027 getmicrotime(&n->updated);
1029 n->clock_offset = le16toh(ep->clock_offset);
1032 "%s: %s - failed to Read Remote Clock Offset, status=%d\n",
1033 __func__, NG_NODE_NAME(unit->node), ep->status);
1038 } /* read_clock_offset_compl */
1040 /* QoS violation event */
1042 qos_violation(ng_hci_unit_p unit, struct mbuf *event)
1044 ng_hci_qos_violation_ep *ep = NULL;
1045 ng_hci_unit_con_p con = NULL;
1049 NG_HCI_M_PULLUP(event, sizeof(*ep));
1053 ep = mtod(event, ng_hci_qos_violation_ep *);
1055 /* Check if we have this connection handle */
1056 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1057 con = ng_hci_con_by_handle(unit, h);
1060 "%s: %s - invalid connection handle=%d\n",
1061 __func__, NG_NODE_NAME(unit->node), h);
1063 } else if (con->link_type != NG_HCI_LINK_ACL) {
1065 "%s: %s - invalid link type=%d\n",
1066 __func__, NG_NODE_NAME(unit->node), con->link_type);
1068 } else if (con->state != NG_HCI_CON_OPEN) {
1070 "%s: %s - invalid connection state=%d, handle=%d\n",
1071 __func__, NG_NODE_NAME(unit->node), con->state, h);
1073 } else /* Notify upper layer */
1074 error = ng_hci_lp_qos_ind(con);
1079 } /* qos_violation */
1081 /* Page scan mode change event */
1083 page_scan_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1085 ng_hci_page_scan_mode_change_ep *ep = NULL;
1086 ng_hci_neighbor_p n = NULL;
1089 NG_HCI_M_PULLUP(event, sizeof(*ep));
1093 ep = mtod(event, ng_hci_page_scan_mode_change_ep *);
1095 /* Update cache entry */
1096 n = ng_hci_get_neighbor(unit, &ep->bdaddr);
1098 n = ng_hci_new_neighbor(unit);
1104 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1106 getmicrotime(&n->updated);
1108 n->page_scan_mode = ep->page_scan_mode;
1113 } /* page_scan_mode_change */
1115 /* Page scan repetition mode change event */
1117 page_scan_rep_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1119 ng_hci_page_scan_rep_mode_change_ep *ep = NULL;
1120 ng_hci_neighbor_p n = NULL;
1123 NG_HCI_M_PULLUP(event, sizeof(*ep));
1127 ep = mtod(event, ng_hci_page_scan_rep_mode_change_ep *);
1129 /* Update cache entry */
1130 n = ng_hci_get_neighbor(unit, &ep->bdaddr);
1132 n = ng_hci_new_neighbor(unit);
1138 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1140 getmicrotime(&n->updated);
1142 n->page_scan_rep_mode = ep->page_scan_rep_mode;
1147 } /* page_scan_rep_mode_change */