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 $
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/bluetooth/include/ng_bluetooth.h>
44 #include <netgraph/bluetooth/include/ng_hci.h>
45 #include <netgraph/bluetooth/hci/ng_hci_var.h>
46 #include <netgraph/bluetooth/hci/ng_hci_cmds.h>
47 #include <netgraph/bluetooth/hci/ng_hci_evnt.h>
48 #include <netgraph/bluetooth/hci/ng_hci_ulpi.h>
49 #include <netgraph/bluetooth/hci/ng_hci_misc.h>
51 /******************************************************************************
52 ******************************************************************************
53 ** HCI event processing module
54 ******************************************************************************
55 ******************************************************************************/
58 * Event processing routines
61 static int inquiry_result (ng_hci_unit_p, struct mbuf *);
62 static int con_compl (ng_hci_unit_p, struct mbuf *);
63 static int con_req (ng_hci_unit_p, struct mbuf *);
64 static int discon_compl (ng_hci_unit_p, struct mbuf *);
65 static int encryption_change (ng_hci_unit_p, struct mbuf *);
66 static int read_remote_features_compl (ng_hci_unit_p, struct mbuf *);
67 static int qos_setup_compl (ng_hci_unit_p, struct mbuf *);
68 static int hardware_error (ng_hci_unit_p, struct mbuf *);
69 static int role_change (ng_hci_unit_p, struct mbuf *);
70 static int num_compl_pkts (ng_hci_unit_p, struct mbuf *);
71 static int mode_change (ng_hci_unit_p, struct mbuf *);
72 static int data_buffer_overflow (ng_hci_unit_p, struct mbuf *);
73 static int read_clock_offset_compl (ng_hci_unit_p, struct mbuf *);
74 static int qos_violation (ng_hci_unit_p, struct mbuf *);
75 static int page_scan_mode_change (ng_hci_unit_p, struct mbuf *);
76 static int page_scan_rep_mode_change (ng_hci_unit_p, struct mbuf *);
77 static int sync_con_queue (ng_hci_unit_p, ng_hci_unit_con_p, int);
78 static int send_data_packets (ng_hci_unit_p, int, int);
79 static int le_event (ng_hci_unit_p, struct mbuf *);
82 * Process HCI event packet
86 ng_hci_process_event(ng_hci_unit_p unit, struct mbuf *event)
88 ng_hci_event_pkt_t *hdr = NULL;
91 /* Get event packet header */
92 NG_HCI_M_PULLUP(event, sizeof(*hdr));
96 hdr = mtod(event, ng_hci_event_pkt_t *);
99 "%s: %s - got HCI event=%#x, length=%d\n",
100 __func__, NG_NODE_NAME(unit->node), hdr->event, hdr->length);
102 /* Get rid of event header and process event */
103 m_adj(event, sizeof(*hdr));
105 switch (hdr->event) {
106 case NG_HCI_EVENT_INQUIRY_COMPL:
107 case NG_HCI_EVENT_RETURN_LINK_KEYS:
108 case NG_HCI_EVENT_PIN_CODE_REQ:
109 case NG_HCI_EVENT_LINK_KEY_REQ:
110 case NG_HCI_EVENT_LINK_KEY_NOTIFICATION:
111 case NG_HCI_EVENT_LOOPBACK_COMMAND:
112 case NG_HCI_EVENT_AUTH_COMPL:
113 case NG_HCI_EVENT_CHANGE_CON_LINK_KEY_COMPL:
114 case NG_HCI_EVENT_MASTER_LINK_KEY_COMPL:
115 case NG_HCI_EVENT_FLUSH_OCCUR: /* XXX Do we have to handle it? */
116 case NG_HCI_EVENT_MAX_SLOT_CHANGE:
117 case NG_HCI_EVENT_CON_PKT_TYPE_CHANGED:
118 case NG_HCI_EVENT_BT_LOGO:
119 case NG_HCI_EVENT_VENDOR:
120 case NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL:
121 case NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL:
122 /* These do not need post processing */
125 case NG_HCI_EVENT_LE:
126 error = le_event(unit, event);
129 case NG_HCI_EVENT_INQUIRY_RESULT:
130 error = inquiry_result(unit, event);
133 case NG_HCI_EVENT_CON_COMPL:
134 error = con_compl(unit, event);
137 case NG_HCI_EVENT_CON_REQ:
138 error = con_req(unit, event);
141 case NG_HCI_EVENT_DISCON_COMPL:
142 error = discon_compl(unit, event);
145 case NG_HCI_EVENT_ENCRYPTION_CHANGE:
146 error = encryption_change(unit, event);
149 case NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL:
150 error = read_remote_features_compl(unit, event);
153 case NG_HCI_EVENT_QOS_SETUP_COMPL:
154 error = qos_setup_compl(unit, event);
157 case NG_HCI_EVENT_COMMAND_COMPL:
158 error = ng_hci_process_command_complete(unit, event);
161 case NG_HCI_EVENT_COMMAND_STATUS:
162 error = ng_hci_process_command_status(unit, event);
165 case NG_HCI_EVENT_HARDWARE_ERROR:
166 error = hardware_error(unit, event);
169 case NG_HCI_EVENT_ROLE_CHANGE:
170 error = role_change(unit, event);
173 case NG_HCI_EVENT_NUM_COMPL_PKTS:
174 error = num_compl_pkts(unit, event);
177 case NG_HCI_EVENT_MODE_CHANGE:
178 error = mode_change(unit, event);
181 case NG_HCI_EVENT_DATA_BUFFER_OVERFLOW:
182 error = data_buffer_overflow(unit, event);
185 case NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL:
186 error = read_clock_offset_compl(unit, event);
189 case NG_HCI_EVENT_QOS_VIOLATION:
190 error = qos_violation(unit, event);
193 case NG_HCI_EVENT_PAGE_SCAN_MODE_CHANGE:
194 error = page_scan_mode_change(unit, event);
197 case NG_HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE:
198 error = page_scan_rep_mode_change(unit, event);
208 } /* ng_hci_process_event */
211 * Send ACL and/or SCO data to the unit driver
215 ng_hci_send_data(ng_hci_unit_p unit)
220 NG_HCI_BUFF_ACL_AVAIL(unit->buffer, count);
223 "%s: %s - sending ACL data packets, count=%d\n",
224 __func__, NG_NODE_NAME(unit->node), count);
227 count = send_data_packets(unit, NG_HCI_LINK_ACL, count);
228 NG_HCI_STAT_ACL_SENT(unit->stat, count);
229 NG_HCI_BUFF_ACL_USE(unit->buffer, count);
233 NG_HCI_BUFF_SCO_AVAIL(unit->buffer, count);
236 "%s: %s - sending SCO data packets, count=%d\n",
237 __func__, NG_NODE_NAME(unit->node), count);
240 count = send_data_packets(unit, NG_HCI_LINK_SCO, count);
241 NG_HCI_STAT_SCO_SENT(unit->stat, count);
242 NG_HCI_BUFF_SCO_USE(unit->buffer, count);
244 } /* ng_hci_send_data */
247 * Send data packets to the lower layer.
251 send_data_packets(ng_hci_unit_p unit, int link_type, int limit)
253 ng_hci_unit_con_p con = NULL, winner = NULL;
256 int min_pending, total_sent, sent, error, v;
258 for (total_sent = 0; limit > 0; ) {
259 min_pending = 0x0fffffff;
263 * Find the connection that has has data to send
264 * and the smallest number of pending packets
267 LIST_FOREACH(con, &unit->con_list, next) {
268 reallink_type = (con->link_type == NG_HCI_LINK_SCO)?
269 NG_HCI_LINK_SCO: NG_HCI_LINK_ACL;
270 if (reallink_type != link_type){
273 if (NG_BT_ITEMQ_LEN(&con->conq) == 0)
276 if (con->pending < min_pending) {
278 min_pending = con->pending;
286 * OK, we have a winner now send as much packets as we can
287 * Count the number of packets we have sent and then sync
288 * winner connection queue.
291 for (sent = 0; limit > 0; limit --, total_sent ++, sent ++) {
292 NG_BT_ITEMQ_DEQUEUE(&winner->conq, item);
297 "%s: %s - sending data packet, handle=%d, len=%d\n",
298 __func__, NG_NODE_NAME(unit->node),
299 winner->con_handle, NGI_M(item)->m_pkthdr.len);
301 /* Check if driver hook still there */
302 v = (unit->drv != NULL && NG_HOOK_IS_VALID(unit->drv));
303 if (!v || (unit->state & NG_HCI_UNIT_READY) !=
306 "%s: %s - could not send data. Hook \"%s\" is %svalid, state=%#x\n",
307 __func__, NG_NODE_NAME(unit->node),
308 NG_HCI_HOOK_DRV, ((v)? "" : "not "),
314 v = NGI_M(item)->m_pkthdr.len;
316 /* Give packet to raw hook */
317 ng_hci_mtap(unit, NGI_M(item));
319 /* ... and forward item to the driver */
320 NG_FWD_ITEM_HOOK(error, item, unit->drv);
325 "%s: %s - could not send data packet, handle=%d, error=%d\n",
326 __func__, NG_NODE_NAME(unit->node),
327 winner->con_handle, error);
332 NG_HCI_STAT_BYTES_SENT(unit->stat, v);
336 * Sync connection queue for the winner
338 sync_con_queue(unit, winner, sent);
342 } /* send_data_packets */
345 * Send flow control messages to the upper layer
349 sync_con_queue(ng_hci_unit_p unit, ng_hci_unit_con_p con, int completed)
352 struct ng_mesg *msg = NULL;
353 ng_hci_sync_con_queue_ep *state = NULL;
356 hook = (con->link_type != NG_HCI_LINK_SCO)? unit->acl : unit->sco;
357 if (hook == NULL || NG_HOOK_NOT_VALID(hook))
360 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_SYNC_CON_QUEUE,
361 sizeof(*state), M_NOWAIT);
365 state = (ng_hci_sync_con_queue_ep *)(msg->data);
366 state->con_handle = con->con_handle;
367 state->completed = completed;
369 NG_SEND_MSG_HOOK(error, unit->node, msg, hook, 0);
372 } /* sync_con_queue */
374 /* Inquiry result event */
376 le_advertizing_report(ng_hci_unit_p unit, struct mbuf *event)
378 ng_hci_le_advertising_report_ep *ep = NULL;
379 ng_hci_neighbor_p n = NULL;
385 NG_HCI_M_PULLUP(event, sizeof(*ep));
389 ep = mtod(event, ng_hci_le_advertising_report_ep *);
390 m_adj(event, sizeof(*ep));
392 for (; ep->num_reports > 0; ep->num_reports --) {
393 /* Get remote unit address */
394 NG_HCI_M_PULLUP(event, sizeof(u_int8_t));
395 event_type = *mtod(event, u_int8_t *);
396 m_adj(event, sizeof(u_int8_t));
397 NG_HCI_M_PULLUP(event, sizeof(u_int8_t));
398 addr_type = *mtod(event, u_int8_t *);
399 m_adj(event, sizeof(u_int8_t));
401 m_copydata(event, 0, sizeof(bdaddr), (caddr_t) &bdaddr);
402 m_adj(event, sizeof(bdaddr));
404 /* Lookup entry in the cache */
405 n = ng_hci_get_neighbor(unit, &bdaddr, (addr_type) ? NG_HCI_LINK_LE_RANDOM:NG_HCI_LINK_LE_PUBLIC);
407 /* Create new entry */
408 n = ng_hci_new_neighbor(unit);
413 bcopy(&bdaddr, &n->bdaddr, sizeof(n->bdaddr));
414 n->addrtype = (addr_type)? NG_HCI_LINK_LE_RANDOM :
415 NG_HCI_LINK_LE_PUBLIC;
418 getmicrotime(&n->updated);
423 * TODO: Make these information
424 * Available from userland.
426 u_int8_t length_data;
430 NG_HCI_M_PULLUP(event, sizeof(u_int8_t));
431 length_data = *mtod(event, u_int8_t *);
432 m_adj(event, sizeof(u_int8_t));
433 /*Advertizement data*/
434 NG_HCI_M_PULLUP(event, length_data);
435 m_adj(event, length_data);
436 NG_HCI_M_PULLUP(event, sizeof(char ));
438 rssi = mtod(event, char *);
439 m_adj(event, sizeof(u_int8_t));
446 } /* inquiry_result */
448 static int le_connection_complete(ng_hci_unit_p unit, struct mbuf *event)
452 ng_hci_le_connection_complete_ep *ep = NULL;
453 ng_hci_unit_con_p con = NULL;
455 uint8_t uclass[3] = {0,0,0};//dummy uclass
457 NG_HCI_M_PULLUP(event, sizeof(*ep));
461 ep = mtod(event, ng_hci_le_connection_complete_ep *);
462 link_type = (ep->address_type)? NG_HCI_LINK_LE_RANDOM :
463 NG_HCI_LINK_LE_PUBLIC;
465 * Find the first connection descriptor that matches the following:
467 * 1) con->link_type == link_type
468 * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
469 * 3) con->bdaddr == ep->address
471 LIST_FOREACH(con, &unit->con_list, next)
472 if (con->link_type == link_type &&
473 con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
474 bcmp(&con->bdaddr, &ep->address, sizeof(bdaddr_t)) == 0)
478 * Two possible cases:
480 * 1) We have found connection descriptor. That means upper layer has
481 * requested this connection via LP_CON_REQ message. In this case
482 * connection must have timeout set. If ng_hci_con_untimeout() fails
483 * then timeout message already went into node's queue. In this case
484 * ignore Connection_Complete event and let timeout deal with it.
486 * 2) We do not have connection descriptor. That means upper layer
487 * nas not requested this connection , (less likely) we gave up
488 * on this connection (timeout) or as node act as slave role.
489 * The most likely scenario is that
490 * we have received LE_Create_Connection command
498 con = ng_hci_new_con(unit, link_type);
504 con->state = NG_HCI_CON_W4_LP_CON_RSP;
505 ng_hci_con_timeout(con);
507 bcopy(&ep->address, &con->bdaddr, sizeof(con->bdaddr));
508 error = ng_hci_lp_con_ind(con, uclass);
510 ng_hci_con_untimeout(con);
511 ng_hci_free_con(con);
514 } else if ((error = ng_hci_con_untimeout(con)) != 0)
518 * Update connection descriptor and send notification
519 * to the upper layers.
522 con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->handle));
523 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
525 ng_hci_lp_con_cfm(con, ep->status);
527 /* Adjust connection state */
529 ng_hci_free_con(con);
531 con->state = NG_HCI_CON_OPEN;
534 * Change link policy for the ACL connections. Enable all
535 * supported link modes. Enable Role switch as well if
536 * device supports it.
548 static int le_connection_update(ng_hci_unit_p unit, struct mbuf *event)
558 le_event(ng_hci_unit_p unit, struct mbuf *event)
563 NG_HCI_M_PULLUP(event, sizeof(*lep));
567 lep = mtod(event, ng_hci_le_ep *);
568 m_adj(event, sizeof(*lep));
569 switch(lep->subevent_code){
570 case NG_HCI_LEEV_CON_COMPL:
571 le_connection_complete(unit, event);
573 case NG_HCI_LEEV_ADVREP:
574 le_advertizing_report(unit, event);
576 case NG_HCI_LEEV_CON_UPDATE_COMPL:
577 le_connection_update(unit, event);
579 case NG_HCI_LEEV_READ_REMOTE_FEATURES_COMPL:
582 case NG_HCI_LEEV_LONG_TERM_KEY_REQUEST:
591 /* Inquiry result event */
593 inquiry_result(ng_hci_unit_p unit, struct mbuf *event)
595 ng_hci_inquiry_result_ep *ep = NULL;
596 ng_hci_neighbor_p n = NULL;
600 NG_HCI_M_PULLUP(event, sizeof(*ep));
604 ep = mtod(event, ng_hci_inquiry_result_ep *);
605 m_adj(event, sizeof(*ep));
607 for (; ep->num_responses > 0; ep->num_responses --) {
608 /* Get remote unit address */
609 m_copydata(event, 0, sizeof(bdaddr), (caddr_t) &bdaddr);
610 m_adj(event, sizeof(bdaddr));
612 /* Lookup entry in the cache */
613 n = ng_hci_get_neighbor(unit, &bdaddr, NG_HCI_LINK_ACL);
615 /* Create new entry */
616 n = ng_hci_new_neighbor(unit);
622 getmicrotime(&n->updated);
624 bcopy(&bdaddr, &n->bdaddr, sizeof(n->bdaddr));
625 n->addrtype = NG_HCI_LINK_ACL;
627 /* XXX call m_pullup here? */
629 n->page_scan_rep_mode = *mtod(event, u_int8_t *);
630 m_adj(event, sizeof(u_int8_t));
632 /* page_scan_period_mode */
633 m_adj(event, sizeof(u_int8_t));
635 n->page_scan_mode = *mtod(event, u_int8_t *);
636 m_adj(event, sizeof(u_int8_t));
639 m_adj(event, NG_HCI_CLASS_SIZE);
642 m_copydata(event, 0, sizeof(n->clock_offset),
643 (caddr_t) &n->clock_offset);
644 n->clock_offset = le16toh(n->clock_offset);
650 } /* inquiry_result */
652 /* Connection complete event */
654 con_compl(ng_hci_unit_p unit, struct mbuf *event)
656 ng_hci_con_compl_ep *ep = NULL;
657 ng_hci_unit_con_p con = NULL;
660 NG_HCI_M_PULLUP(event, sizeof(*ep));
664 ep = mtod(event, ng_hci_con_compl_ep *);
667 * Find the first connection descriptor that matches the following:
669 * 1) con->link_type == ep->link_type
670 * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
671 * 3) con->bdaddr == ep->bdaddr
674 LIST_FOREACH(con, &unit->con_list, next)
675 if (con->link_type == ep->link_type &&
676 con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
677 bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
681 * Two possible cases:
683 * 1) We have found connection descriptor. That means upper layer has
684 * requested this connection via LP_CON_REQ message. In this case
685 * connection must have timeout set. If ng_hci_con_untimeout() fails
686 * then timeout message already went into node's queue. In this case
687 * ignore Connection_Complete event and let timeout deal with it.
689 * 2) We do not have connection descriptor. That means upper layer
690 * nas not requested this connection or (less likely) we gave up
691 * on this connection (timeout). The most likely scenario is that
692 * we have received Create_Connection/Add_SCO_Connection command
700 con = ng_hci_new_con(unit, ep->link_type);
706 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
707 } else if ((error = ng_hci_con_untimeout(con)) != 0)
711 * Update connection descriptor and send notification
712 * to the upper layers.
715 con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
716 con->encryption_mode = ep->encryption_mode;
718 ng_hci_lp_con_cfm(con, ep->status);
720 /* Adjust connection state */
722 ng_hci_free_con(con);
724 con->state = NG_HCI_CON_OPEN;
727 * Change link policy for the ACL connections. Enable all
728 * supported link modes. Enable Role switch as well if
729 * device supports it.
732 if (ep->link_type == NG_HCI_LINK_ACL) {
733 struct __link_policy {
734 ng_hci_cmd_pkt_t hdr;
735 ng_hci_write_link_policy_settings_cp cp;
736 } __attribute__ ((packed)) *lp;
739 MGETHDR(m, M_NOWAIT, MT_DATA);
741 m->m_pkthdr.len = m->m_len = sizeof(*lp);
742 lp = mtod(m, struct __link_policy *);
744 lp->hdr.type = NG_HCI_CMD_PKT;
745 lp->hdr.opcode = htole16(NG_HCI_OPCODE(
746 NG_HCI_OGF_LINK_POLICY,
747 NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS));
748 lp->hdr.length = sizeof(lp->cp);
750 lp->cp.con_handle = ep->con_handle;
753 if ((unit->features[0] & NG_HCI_LMP_SWITCH) &&
755 lp->cp.settings |= 0x1;
756 if (unit->features[0] & NG_HCI_LMP_HOLD_MODE)
757 lp->cp.settings |= 0x2;
758 if (unit->features[0] & NG_HCI_LMP_SNIFF_MODE)
759 lp->cp.settings |= 0x4;
760 if (unit->features[1] & NG_HCI_LMP_PARK_MODE)
761 lp->cp.settings |= 0x8;
763 lp->cp.settings &= unit->link_policy_mask;
764 lp->cp.settings = htole16(lp->cp.settings);
766 NG_BT_MBUFQ_ENQUEUE(&unit->cmdq, m);
767 if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
768 ng_hci_send_command(unit);
778 /* Connection request event */
780 con_req(ng_hci_unit_p unit, struct mbuf *event)
782 ng_hci_con_req_ep *ep = NULL;
783 ng_hci_unit_con_p con = NULL;
786 NG_HCI_M_PULLUP(event, sizeof(*ep));
790 ep = mtod(event, ng_hci_con_req_ep *);
793 * Find the first connection descriptor that matches the following:
795 * 1) con->link_type == ep->link_type
797 * 2) con->state == NG_HCI_CON_W4_LP_CON_RSP ||
798 * con->state == NG_HCI_CON_W4_CONN_COMPL
800 * 3) con->bdaddr == ep->bdaddr
804 * 1) We do not have connection descriptor. This is simple. Create
805 * new fresh connection descriptor and send notification to the
806 * appropriate upstream hook (based on link_type).
808 * 2) We found connection handle. This is more complicated.
812 * Since only one ACL link can exist between each pair of
813 * units then we have a race. Our upper layer has requested
814 * an ACL connection to the remote unit, but we did not send
815 * command yet. At the same time the remote unit has requested
816 * an ACL connection from us. In this case we will ignore
817 * Connection_Request event. This probably will cause connect
818 * failure on both units.
822 * The spec on page 45 says :
824 * "The master can support up to three SCO links to the same
825 * slave or to different slaves. A slave can support up to
826 * three SCO links from the same master, or two SCO links if
827 * the links originate from different masters."
829 * The only problem is how to handle multiple SCO links between
830 * matster and slave. For now we will assume that multiple SCO
831 * links MUST be opened one after another.
834 LIST_FOREACH(con, &unit->con_list, next)
835 if (con->link_type == ep->link_type &&
836 (con->state == NG_HCI_CON_W4_LP_CON_RSP ||
837 con->state == NG_HCI_CON_W4_CONN_COMPLETE) &&
838 bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
842 con = ng_hci_new_con(unit, ep->link_type);
844 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
846 con->state = NG_HCI_CON_W4_LP_CON_RSP;
847 ng_hci_con_timeout(con);
849 error = ng_hci_lp_con_ind(con, ep->uclass);
851 ng_hci_con_untimeout(con);
852 ng_hci_free_con(con);
863 /* Disconnect complete event */
865 discon_compl(ng_hci_unit_p unit, struct mbuf *event)
867 ng_hci_discon_compl_ep *ep = NULL;
868 ng_hci_unit_con_p con = NULL;
872 NG_HCI_M_PULLUP(event, sizeof(*ep));
876 ep = mtod(event, ng_hci_discon_compl_ep *);
880 * Do we have to send notification if ep->status != 0?
881 * For now we will send notification for both ACL and SCO connections
882 * ONLY if ep->status == 0.
885 if (ep->status == 0) {
886 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
887 con = ng_hci_con_by_handle(unit, h);
889 error = ng_hci_lp_discon_ind(con, ep->reason);
891 /* Remove all timeouts (if any) */
892 if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
893 ng_hci_con_untimeout(con);
895 ng_hci_free_con(con);
898 "%s: %s - invalid connection handle=%d\n",
899 __func__, NG_NODE_NAME(unit->node), h);
909 /* Encryption change event */
911 encryption_change(ng_hci_unit_p unit, struct mbuf *event)
913 ng_hci_encryption_change_ep *ep = NULL;
914 ng_hci_unit_con_p con = NULL;
918 NG_HCI_M_PULLUP(event, sizeof(*ep));
922 ep = mtod(event, ng_hci_encryption_change_ep *);
923 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
924 con = ng_hci_con_by_handle(unit, h);
926 if (ep->status == 0) {
929 "%s: %s - invalid connection handle=%d\n",
930 __func__, NG_NODE_NAME(unit->node), h);
932 } else if (con->link_type == NG_HCI_LINK_SCO) {
934 "%s: %s - invalid link type=%d\n",
935 __func__, NG_NODE_NAME(unit->node),
938 } else if (ep->encryption_enable)
939 /* XXX is that true? */
940 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_P2P;
942 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
945 "%s: %s - failed to change encryption mode, status=%d\n",
946 __func__, NG_NODE_NAME(unit->node), ep->status);
948 /*Anyway, propagete encryption status to upper layer*/
949 ng_hci_lp_enc_change(con, con->encryption_mode);
954 } /* encryption_change */
956 /* Read remote feature complete event */
958 read_remote_features_compl(ng_hci_unit_p unit, struct mbuf *event)
960 ng_hci_read_remote_features_compl_ep *ep = NULL;
961 ng_hci_unit_con_p con = NULL;
962 ng_hci_neighbor_p n = NULL;
966 NG_HCI_M_PULLUP(event, sizeof(*ep));
970 ep = mtod(event, ng_hci_read_remote_features_compl_ep *);
972 if (ep->status == 0) {
973 /* Check if we have this connection handle */
974 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
975 con = ng_hci_con_by_handle(unit, h);
978 "%s: %s - invalid connection handle=%d\n",
979 __func__, NG_NODE_NAME(unit->node), h);
984 /* Update cache entry */
985 n = ng_hci_get_neighbor(unit, &con->bdaddr, NG_HCI_LINK_ACL);
987 n = ng_hci_new_neighbor(unit);
993 bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
994 n->addrtype = NG_HCI_LINK_ACL;
996 getmicrotime(&n->updated);
998 bcopy(ep->features, n->features, sizeof(n->features));
1001 "%s: %s - failed to read remote unit features, status=%d\n",
1002 __func__, NG_NODE_NAME(unit->node), ep->status);
1007 } /* read_remote_features_compl */
1009 /* QoS setup complete event */
1011 qos_setup_compl(ng_hci_unit_p unit, struct mbuf *event)
1013 ng_hci_qos_setup_compl_ep *ep = NULL;
1014 ng_hci_unit_con_p con = NULL;
1018 NG_HCI_M_PULLUP(event, sizeof(*ep));
1022 ep = mtod(event, ng_hci_qos_setup_compl_ep *);
1024 /* Check if we have this connection handle */
1025 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1026 con = ng_hci_con_by_handle(unit, h);
1029 "%s: %s - invalid connection handle=%d\n",
1030 __func__, NG_NODE_NAME(unit->node), h);
1032 } else if (con->link_type != NG_HCI_LINK_ACL) {
1034 "%s: %s - invalid link type=%d, handle=%d\n",
1035 __func__, NG_NODE_NAME(unit->node), con->link_type, h);
1037 } else if (con->state != NG_HCI_CON_OPEN) {
1039 "%s: %s - invalid connection state=%d, handle=%d\n",
1040 __func__, NG_NODE_NAME(unit->node),
1043 } else /* Notify upper layer */
1044 error = ng_hci_lp_qos_cfm(con, ep->status);
1049 } /* qos_setup_compl */
1051 /* Hardware error event */
1053 hardware_error(ng_hci_unit_p unit, struct mbuf *event)
1056 "%s: %s - hardware error %#x\n",
1057 __func__, NG_NODE_NAME(unit->node), *mtod(event, u_int8_t *));
1062 } /* hardware_error */
1064 /* Role change event */
1066 role_change(ng_hci_unit_p unit, struct mbuf *event)
1068 ng_hci_role_change_ep *ep = NULL;
1069 ng_hci_unit_con_p con = NULL;
1071 NG_HCI_M_PULLUP(event, sizeof(*ep));
1075 ep = mtod(event, ng_hci_role_change_ep *);
1077 if (ep->status == 0) {
1078 /* XXX shoud we also change "role" for SCO connections? */
1079 con = ng_hci_con_by_bdaddr(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1081 con->role = ep->role;
1084 "%s: %s - ACL connection does not exist, bdaddr=%x:%x:%x:%x:%x:%x\n",
1085 __func__, NG_NODE_NAME(unit->node),
1086 ep->bdaddr.b[5], ep->bdaddr.b[4],
1087 ep->bdaddr.b[3], ep->bdaddr.b[2],
1088 ep->bdaddr.b[1], ep->bdaddr.b[0]);
1091 "%s: %s - failed to change role, status=%d, bdaddr=%x:%x:%x:%x:%x:%x\n",
1092 __func__, NG_NODE_NAME(unit->node), ep->status,
1093 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
1094 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
1101 /* Number of completed packets event */
1103 num_compl_pkts(ng_hci_unit_p unit, struct mbuf *event)
1105 ng_hci_num_compl_pkts_ep *ep = NULL;
1106 ng_hci_unit_con_p con = NULL;
1109 NG_HCI_M_PULLUP(event, sizeof(*ep));
1113 ep = mtod(event, ng_hci_num_compl_pkts_ep *);
1114 m_adj(event, sizeof(*ep));
1116 for (; ep->num_con_handles > 0; ep->num_con_handles --) {
1117 /* Get connection handle */
1118 m_copydata(event, 0, sizeof(h), (caddr_t) &h);
1119 m_adj(event, sizeof(h));
1120 h = NG_HCI_CON_HANDLE(le16toh(h));
1122 /* Get number of completed packets */
1123 m_copydata(event, 0, sizeof(p), (caddr_t) &p);
1124 m_adj(event, sizeof(p));
1127 /* Check if we have this connection handle */
1128 con = ng_hci_con_by_handle(unit, h);
1131 if (con->pending < 0) {
1133 "%s: %s - pending packet counter is out of sync! " \
1134 "handle=%d, pending=%d, ncp=%d\n", __func__, NG_NODE_NAME(unit->node),
1135 con->con_handle, con->pending, p);
1140 /* Update buffer descriptor */
1141 if (con->link_type != NG_HCI_LINK_SCO)
1142 NG_HCI_BUFF_ACL_FREE(unit->buffer, p);
1144 NG_HCI_BUFF_SCO_FREE(unit->buffer, p);
1147 "%s: %s - invalid connection handle=%d\n",
1148 __func__, NG_NODE_NAME(unit->node), h);
1153 /* Send more data */
1154 ng_hci_send_data(unit);
1157 } /* num_compl_pkts */
1159 /* Mode change event */
1161 mode_change(ng_hci_unit_p unit, struct mbuf *event)
1163 ng_hci_mode_change_ep *ep = NULL;
1164 ng_hci_unit_con_p con = NULL;
1167 NG_HCI_M_PULLUP(event, sizeof(*ep));
1171 ep = mtod(event, ng_hci_mode_change_ep *);
1173 if (ep->status == 0) {
1174 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1176 con = ng_hci_con_by_handle(unit, h);
1179 "%s: %s - invalid connection handle=%d\n",
1180 __func__, NG_NODE_NAME(unit->node), h);
1182 } else if (con->link_type != NG_HCI_LINK_ACL) {
1184 "%s: %s - invalid link type=%d\n",
1185 __func__, NG_NODE_NAME(unit->node),
1189 con->mode = ep->unit_mode;
1192 "%s: %s - failed to change mode, status=%d\n",
1193 __func__, NG_NODE_NAME(unit->node), ep->status);
1200 /* Data buffer overflow event */
1202 data_buffer_overflow(ng_hci_unit_p unit, struct mbuf *event)
1205 "%s: %s - %s data buffer overflow\n",
1206 __func__, NG_NODE_NAME(unit->node),
1207 (*mtod(event, u_int8_t *) == NG_HCI_LINK_ACL)? "ACL" : "SCO");
1212 } /* data_buffer_overflow */
1214 /* Read clock offset complete event */
1216 read_clock_offset_compl(ng_hci_unit_p unit, struct mbuf *event)
1218 ng_hci_read_clock_offset_compl_ep *ep = NULL;
1219 ng_hci_unit_con_p con = NULL;
1220 ng_hci_neighbor_p n = NULL;
1223 NG_HCI_M_PULLUP(event, sizeof(*ep));
1227 ep = mtod(event, ng_hci_read_clock_offset_compl_ep *);
1229 if (ep->status == 0) {
1230 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1232 con = ng_hci_con_by_handle(unit, h);
1235 "%s: %s - invalid connection handle=%d\n",
1236 __func__, NG_NODE_NAME(unit->node), h);
1241 /* Update cache entry */
1242 n = ng_hci_get_neighbor(unit, &con->bdaddr, NG_HCI_LINK_ACL);
1244 n = ng_hci_new_neighbor(unit);
1250 bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1251 n->addrtype = NG_HCI_LINK_ACL;
1253 getmicrotime(&n->updated);
1255 n->clock_offset = le16toh(ep->clock_offset);
1258 "%s: %s - failed to Read Remote Clock Offset, status=%d\n",
1259 __func__, NG_NODE_NAME(unit->node), ep->status);
1264 } /* read_clock_offset_compl */
1266 /* QoS violation event */
1268 qos_violation(ng_hci_unit_p unit, struct mbuf *event)
1270 ng_hci_qos_violation_ep *ep = NULL;
1271 ng_hci_unit_con_p con = NULL;
1275 NG_HCI_M_PULLUP(event, sizeof(*ep));
1279 ep = mtod(event, ng_hci_qos_violation_ep *);
1281 /* Check if we have this connection handle */
1282 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1283 con = ng_hci_con_by_handle(unit, h);
1286 "%s: %s - invalid connection handle=%d\n",
1287 __func__, NG_NODE_NAME(unit->node), h);
1289 } else if (con->link_type != NG_HCI_LINK_ACL) {
1291 "%s: %s - invalid link type=%d\n",
1292 __func__, NG_NODE_NAME(unit->node), con->link_type);
1294 } else if (con->state != NG_HCI_CON_OPEN) {
1296 "%s: %s - invalid connection state=%d, handle=%d\n",
1297 __func__, NG_NODE_NAME(unit->node), con->state, h);
1299 } else /* Notify upper layer */
1300 error = ng_hci_lp_qos_ind(con);
1305 } /* qos_violation */
1307 /* Page scan mode change event */
1309 page_scan_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1311 ng_hci_page_scan_mode_change_ep *ep = NULL;
1312 ng_hci_neighbor_p n = NULL;
1315 NG_HCI_M_PULLUP(event, sizeof(*ep));
1319 ep = mtod(event, ng_hci_page_scan_mode_change_ep *);
1321 /* Update cache entry */
1322 n = ng_hci_get_neighbor(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1324 n = ng_hci_new_neighbor(unit);
1330 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1331 n->addrtype = NG_HCI_LINK_ACL;
1333 getmicrotime(&n->updated);
1335 n->page_scan_mode = ep->page_scan_mode;
1340 } /* page_scan_mode_change */
1342 /* Page scan repetition mode change event */
1344 page_scan_rep_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1346 ng_hci_page_scan_rep_mode_change_ep *ep = NULL;
1347 ng_hci_neighbor_p n = NULL;
1350 NG_HCI_M_PULLUP(event, sizeof(*ep));
1354 ep = mtod(event, ng_hci_page_scan_rep_mode_change_ep *);
1356 /* Update cache entry */
1357 n = ng_hci_get_neighbor(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1359 n = ng_hci_new_neighbor(unit);
1365 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1366 n->addrtype = NG_HCI_LINK_ACL;
1368 getmicrotime(&n->updated);
1370 n->page_scan_rep_mode = ep->page_scan_rep_mode;
1375 } /* page_scan_rep_mode_change */