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);
422 * TODO: Make these information
423 * Available from userland.
425 u_int8_t length_data;
427 event = m_pullup(event, sizeof(u_int8_t));
429 NG_HCI_WARN("%s: Event datasize Pullup Failed\n", __func__);
432 length_data = *mtod(event, u_int8_t *);
433 m_adj(event, sizeof(u_int8_t));
434 n->extinq_size = (length_data < NG_HCI_EXTINQ_MAX)?
435 length_data : NG_HCI_EXTINQ_MAX;
437 /*Advertizement data*/
438 event = m_pullup(event, n->extinq_size);
440 NG_HCI_WARN("%s: Event data pullup Failed\n", __func__);
443 m_copydata(event, 0, n->extinq_size, n->extinq_data);
444 m_adj(event, n->extinq_size);
445 event = m_pullup(event, sizeof(char ));
448 NG_HCI_WARN("%s: Event rssi pull up Failed\n", __func__);
452 n->page_scan_mode = *mtod(event, char *);
453 m_adj(event, sizeof(u_int8_t));
460 } /* inquiry_result */
462 static int le_connection_complete(ng_hci_unit_p unit, struct mbuf *event)
466 ng_hci_le_connection_complete_ep *ep = NULL;
467 ng_hci_unit_con_p con = NULL;
469 uint8_t uclass[3] = {0,0,0};//dummy uclass
471 NG_HCI_M_PULLUP(event, sizeof(*ep));
475 ep = mtod(event, ng_hci_le_connection_complete_ep *);
476 link_type = (ep->address_type)? NG_HCI_LINK_LE_RANDOM :
477 NG_HCI_LINK_LE_PUBLIC;
479 * Find the first connection descriptor that matches the following:
481 * 1) con->link_type == link_type
482 * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
483 * 3) con->bdaddr == ep->address
485 LIST_FOREACH(con, &unit->con_list, next)
486 if (con->link_type == link_type &&
487 con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
488 bcmp(&con->bdaddr, &ep->address, sizeof(bdaddr_t)) == 0)
492 * Two possible cases:
494 * 1) We have found connection descriptor. That means upper layer has
495 * requested this connection via LP_CON_REQ message. In this case
496 * connection must have timeout set. If ng_hci_con_untimeout() fails
497 * then timeout message already went into node's queue. In this case
498 * ignore Connection_Complete event and let timeout deal with it.
500 * 2) We do not have connection descriptor. That means upper layer
501 * nas not requested this connection , (less likely) we gave up
502 * on this connection (timeout) or as node act as slave role.
503 * The most likely scenario is that
504 * we have received LE_Create_Connection command
512 con = ng_hci_new_con(unit, link_type);
518 con->state = NG_HCI_CON_W4_LP_CON_RSP;
519 ng_hci_con_timeout(con);
521 bcopy(&ep->address, &con->bdaddr, sizeof(con->bdaddr));
522 error = ng_hci_lp_con_ind(con, uclass);
524 ng_hci_con_untimeout(con);
525 ng_hci_free_con(con);
528 } else if ((error = ng_hci_con_untimeout(con)) != 0)
532 * Update connection descriptor and send notification
533 * to the upper layers.
536 con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->handle));
537 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
539 ng_hci_lp_con_cfm(con, ep->status);
541 /* Adjust connection state */
543 ng_hci_free_con(con);
545 con->state = NG_HCI_CON_OPEN;
548 * Change link policy for the ACL connections. Enable all
549 * supported link modes. Enable Role switch as well if
550 * device supports it.
562 static int le_connection_update(ng_hci_unit_p unit, struct mbuf *event)
572 le_event(ng_hci_unit_p unit, struct mbuf *event)
577 NG_HCI_M_PULLUP(event, sizeof(*lep));
581 lep = mtod(event, ng_hci_le_ep *);
582 m_adj(event, sizeof(*lep));
583 switch(lep->subevent_code){
584 case NG_HCI_LEEV_CON_COMPL:
585 le_connection_complete(unit, event);
587 case NG_HCI_LEEV_ADVREP:
588 le_advertizing_report(unit, event);
590 case NG_HCI_LEEV_CON_UPDATE_COMPL:
591 le_connection_update(unit, event);
593 case NG_HCI_LEEV_READ_REMOTE_FEATURES_COMPL:
596 case NG_HCI_LEEV_LONG_TERM_KEY_REQUEST:
605 /* Inquiry result event */
607 inquiry_result(ng_hci_unit_p unit, struct mbuf *event)
609 ng_hci_inquiry_result_ep *ep = NULL;
610 ng_hci_neighbor_p n = NULL;
614 NG_HCI_M_PULLUP(event, sizeof(*ep));
618 ep = mtod(event, ng_hci_inquiry_result_ep *);
619 m_adj(event, sizeof(*ep));
621 for (; ep->num_responses > 0; ep->num_responses --) {
622 /* Get remote unit address */
623 m_copydata(event, 0, sizeof(bdaddr), (caddr_t) &bdaddr);
624 m_adj(event, sizeof(bdaddr));
626 /* Lookup entry in the cache */
627 n = ng_hci_get_neighbor(unit, &bdaddr, NG_HCI_LINK_ACL);
629 /* Create new entry */
630 n = ng_hci_new_neighbor(unit);
636 getmicrotime(&n->updated);
638 bcopy(&bdaddr, &n->bdaddr, sizeof(n->bdaddr));
639 n->addrtype = NG_HCI_LINK_ACL;
641 /* XXX call m_pullup here? */
643 n->page_scan_rep_mode = *mtod(event, u_int8_t *);
644 m_adj(event, sizeof(u_int8_t));
646 /* page_scan_period_mode */
647 m_adj(event, sizeof(u_int8_t));
649 n->page_scan_mode = *mtod(event, u_int8_t *);
650 m_adj(event, sizeof(u_int8_t));
653 m_adj(event, NG_HCI_CLASS_SIZE);
656 m_copydata(event, 0, sizeof(n->clock_offset),
657 (caddr_t) &n->clock_offset);
658 n->clock_offset = le16toh(n->clock_offset);
664 } /* inquiry_result */
666 /* Connection complete event */
668 con_compl(ng_hci_unit_p unit, struct mbuf *event)
670 ng_hci_con_compl_ep *ep = NULL;
671 ng_hci_unit_con_p con = NULL;
674 NG_HCI_M_PULLUP(event, sizeof(*ep));
678 ep = mtod(event, ng_hci_con_compl_ep *);
681 * Find the first connection descriptor that matches the following:
683 * 1) con->link_type == ep->link_type
684 * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
685 * 3) con->bdaddr == ep->bdaddr
688 LIST_FOREACH(con, &unit->con_list, next)
689 if (con->link_type == ep->link_type &&
690 con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
691 bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
695 * Two possible cases:
697 * 1) We have found connection descriptor. That means upper layer has
698 * requested this connection via LP_CON_REQ message. In this case
699 * connection must have timeout set. If ng_hci_con_untimeout() fails
700 * then timeout message already went into node's queue. In this case
701 * ignore Connection_Complete event and let timeout deal with it.
703 * 2) We do not have connection descriptor. That means upper layer
704 * nas not requested this connection or (less likely) we gave up
705 * on this connection (timeout). The most likely scenario is that
706 * we have received Create_Connection/Add_SCO_Connection command
714 con = ng_hci_new_con(unit, ep->link_type);
720 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
721 } else if ((error = ng_hci_con_untimeout(con)) != 0)
725 * Update connection descriptor and send notification
726 * to the upper layers.
729 con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
730 con->encryption_mode = ep->encryption_mode;
732 ng_hci_lp_con_cfm(con, ep->status);
734 /* Adjust connection state */
736 ng_hci_free_con(con);
738 con->state = NG_HCI_CON_OPEN;
741 * Change link policy for the ACL connections. Enable all
742 * supported link modes. Enable Role switch as well if
743 * device supports it.
746 if (ep->link_type == NG_HCI_LINK_ACL) {
747 struct __link_policy {
748 ng_hci_cmd_pkt_t hdr;
749 ng_hci_write_link_policy_settings_cp cp;
750 } __attribute__ ((packed)) *lp;
753 MGETHDR(m, M_NOWAIT, MT_DATA);
755 m->m_pkthdr.len = m->m_len = sizeof(*lp);
756 lp = mtod(m, struct __link_policy *);
758 lp->hdr.type = NG_HCI_CMD_PKT;
759 lp->hdr.opcode = htole16(NG_HCI_OPCODE(
760 NG_HCI_OGF_LINK_POLICY,
761 NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS));
762 lp->hdr.length = sizeof(lp->cp);
764 lp->cp.con_handle = ep->con_handle;
767 if ((unit->features[0] & NG_HCI_LMP_SWITCH) &&
769 lp->cp.settings |= 0x1;
770 if (unit->features[0] & NG_HCI_LMP_HOLD_MODE)
771 lp->cp.settings |= 0x2;
772 if (unit->features[0] & NG_HCI_LMP_SNIFF_MODE)
773 lp->cp.settings |= 0x4;
774 if (unit->features[1] & NG_HCI_LMP_PARK_MODE)
775 lp->cp.settings |= 0x8;
777 lp->cp.settings &= unit->link_policy_mask;
778 lp->cp.settings = htole16(lp->cp.settings);
780 NG_BT_MBUFQ_ENQUEUE(&unit->cmdq, m);
781 if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
782 ng_hci_send_command(unit);
792 /* Connection request event */
794 con_req(ng_hci_unit_p unit, struct mbuf *event)
796 ng_hci_con_req_ep *ep = NULL;
797 ng_hci_unit_con_p con = NULL;
800 NG_HCI_M_PULLUP(event, sizeof(*ep));
804 ep = mtod(event, ng_hci_con_req_ep *);
807 * Find the first connection descriptor that matches the following:
809 * 1) con->link_type == ep->link_type
811 * 2) con->state == NG_HCI_CON_W4_LP_CON_RSP ||
812 * con->state == NG_HCI_CON_W4_CONN_COMPL
814 * 3) con->bdaddr == ep->bdaddr
818 * 1) We do not have connection descriptor. This is simple. Create
819 * new fresh connection descriptor and send notification to the
820 * appropriate upstream hook (based on link_type).
822 * 2) We found connection handle. This is more complicated.
826 * Since only one ACL link can exist between each pair of
827 * units then we have a race. Our upper layer has requested
828 * an ACL connection to the remote unit, but we did not send
829 * command yet. At the same time the remote unit has requested
830 * an ACL connection from us. In this case we will ignore
831 * Connection_Request event. This probably will cause connect
832 * failure on both units.
836 * The spec on page 45 says :
838 * "The master can support up to three SCO links to the same
839 * slave or to different slaves. A slave can support up to
840 * three SCO links from the same master, or two SCO links if
841 * the links originate from different masters."
843 * The only problem is how to handle multiple SCO links between
844 * matster and slave. For now we will assume that multiple SCO
845 * links MUST be opened one after another.
848 LIST_FOREACH(con, &unit->con_list, next)
849 if (con->link_type == ep->link_type &&
850 (con->state == NG_HCI_CON_W4_LP_CON_RSP ||
851 con->state == NG_HCI_CON_W4_CONN_COMPLETE) &&
852 bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
856 con = ng_hci_new_con(unit, ep->link_type);
858 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
860 con->state = NG_HCI_CON_W4_LP_CON_RSP;
861 ng_hci_con_timeout(con);
863 error = ng_hci_lp_con_ind(con, ep->uclass);
865 ng_hci_con_untimeout(con);
866 ng_hci_free_con(con);
877 /* Disconnect complete event */
879 discon_compl(ng_hci_unit_p unit, struct mbuf *event)
881 ng_hci_discon_compl_ep *ep = NULL;
882 ng_hci_unit_con_p con = NULL;
886 NG_HCI_M_PULLUP(event, sizeof(*ep));
890 ep = mtod(event, ng_hci_discon_compl_ep *);
894 * Do we have to send notification if ep->status != 0?
895 * For now we will send notification for both ACL and SCO connections
896 * ONLY if ep->status == 0.
899 if (ep->status == 0) {
900 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
901 con = ng_hci_con_by_handle(unit, h);
903 error = ng_hci_lp_discon_ind(con, ep->reason);
905 /* Remove all timeouts (if any) */
906 if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
907 ng_hci_con_untimeout(con);
909 ng_hci_free_con(con);
912 "%s: %s - invalid connection handle=%d\n",
913 __func__, NG_NODE_NAME(unit->node), h);
923 /* Encryption change event */
925 encryption_change(ng_hci_unit_p unit, struct mbuf *event)
927 ng_hci_encryption_change_ep *ep = NULL;
928 ng_hci_unit_con_p con = NULL;
932 NG_HCI_M_PULLUP(event, sizeof(*ep));
936 ep = mtod(event, ng_hci_encryption_change_ep *);
937 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
938 con = ng_hci_con_by_handle(unit, h);
940 if (ep->status == 0) {
943 "%s: %s - invalid connection handle=%d\n",
944 __func__, NG_NODE_NAME(unit->node), h);
946 } else if (con->link_type == NG_HCI_LINK_SCO) {
948 "%s: %s - invalid link type=%d\n",
949 __func__, NG_NODE_NAME(unit->node),
952 } else if (ep->encryption_enable)
953 /* XXX is that true? */
954 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_P2P;
956 con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
959 "%s: %s - failed to change encryption mode, status=%d\n",
960 __func__, NG_NODE_NAME(unit->node), ep->status);
962 /*Anyway, propagete encryption status to upper layer*/
963 ng_hci_lp_enc_change(con, con->encryption_mode);
968 } /* encryption_change */
970 /* Read remote feature complete event */
972 read_remote_features_compl(ng_hci_unit_p unit, struct mbuf *event)
974 ng_hci_read_remote_features_compl_ep *ep = NULL;
975 ng_hci_unit_con_p con = NULL;
976 ng_hci_neighbor_p n = NULL;
980 NG_HCI_M_PULLUP(event, sizeof(*ep));
984 ep = mtod(event, ng_hci_read_remote_features_compl_ep *);
986 if (ep->status == 0) {
987 /* Check if we have this connection handle */
988 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
989 con = ng_hci_con_by_handle(unit, h);
992 "%s: %s - invalid connection handle=%d\n",
993 __func__, NG_NODE_NAME(unit->node), h);
998 /* Update cache entry */
999 n = ng_hci_get_neighbor(unit, &con->bdaddr, NG_HCI_LINK_ACL);
1001 n = ng_hci_new_neighbor(unit);
1007 bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1008 n->addrtype = NG_HCI_LINK_ACL;
1010 getmicrotime(&n->updated);
1012 bcopy(ep->features, n->features, sizeof(n->features));
1015 "%s: %s - failed to read remote unit features, status=%d\n",
1016 __func__, NG_NODE_NAME(unit->node), ep->status);
1021 } /* read_remote_features_compl */
1023 /* QoS setup complete event */
1025 qos_setup_compl(ng_hci_unit_p unit, struct mbuf *event)
1027 ng_hci_qos_setup_compl_ep *ep = NULL;
1028 ng_hci_unit_con_p con = NULL;
1032 NG_HCI_M_PULLUP(event, sizeof(*ep));
1036 ep = mtod(event, ng_hci_qos_setup_compl_ep *);
1038 /* Check if we have this connection handle */
1039 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1040 con = ng_hci_con_by_handle(unit, h);
1043 "%s: %s - invalid connection handle=%d\n",
1044 __func__, NG_NODE_NAME(unit->node), h);
1046 } else if (con->link_type != NG_HCI_LINK_ACL) {
1048 "%s: %s - invalid link type=%d, handle=%d\n",
1049 __func__, NG_NODE_NAME(unit->node), con->link_type, h);
1051 } else if (con->state != NG_HCI_CON_OPEN) {
1053 "%s: %s - invalid connection state=%d, handle=%d\n",
1054 __func__, NG_NODE_NAME(unit->node),
1057 } else /* Notify upper layer */
1058 error = ng_hci_lp_qos_cfm(con, ep->status);
1063 } /* qos_setup_compl */
1065 /* Hardware error event */
1067 hardware_error(ng_hci_unit_p unit, struct mbuf *event)
1070 "%s: %s - hardware error %#x\n",
1071 __func__, NG_NODE_NAME(unit->node), *mtod(event, u_int8_t *));
1076 } /* hardware_error */
1078 /* Role change event */
1080 role_change(ng_hci_unit_p unit, struct mbuf *event)
1082 ng_hci_role_change_ep *ep = NULL;
1083 ng_hci_unit_con_p con = NULL;
1085 NG_HCI_M_PULLUP(event, sizeof(*ep));
1089 ep = mtod(event, ng_hci_role_change_ep *);
1091 if (ep->status == 0) {
1092 /* XXX shoud we also change "role" for SCO connections? */
1093 con = ng_hci_con_by_bdaddr(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1095 con->role = ep->role;
1098 "%s: %s - ACL connection does not exist, bdaddr=%x:%x:%x:%x:%x:%x\n",
1099 __func__, NG_NODE_NAME(unit->node),
1100 ep->bdaddr.b[5], ep->bdaddr.b[4],
1101 ep->bdaddr.b[3], ep->bdaddr.b[2],
1102 ep->bdaddr.b[1], ep->bdaddr.b[0]);
1105 "%s: %s - failed to change role, status=%d, bdaddr=%x:%x:%x:%x:%x:%x\n",
1106 __func__, NG_NODE_NAME(unit->node), ep->status,
1107 ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
1108 ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
1115 /* Number of completed packets event */
1117 num_compl_pkts(ng_hci_unit_p unit, struct mbuf *event)
1119 ng_hci_num_compl_pkts_ep *ep = NULL;
1120 ng_hci_unit_con_p con = NULL;
1123 NG_HCI_M_PULLUP(event, sizeof(*ep));
1127 ep = mtod(event, ng_hci_num_compl_pkts_ep *);
1128 m_adj(event, sizeof(*ep));
1130 for (; ep->num_con_handles > 0; ep->num_con_handles --) {
1131 /* Get connection handle */
1132 m_copydata(event, 0, sizeof(h), (caddr_t) &h);
1133 m_adj(event, sizeof(h));
1134 h = NG_HCI_CON_HANDLE(le16toh(h));
1136 /* Get number of completed packets */
1137 m_copydata(event, 0, sizeof(p), (caddr_t) &p);
1138 m_adj(event, sizeof(p));
1141 /* Check if we have this connection handle */
1142 con = ng_hci_con_by_handle(unit, h);
1145 if (con->pending < 0) {
1147 "%s: %s - pending packet counter is out of sync! " \
1148 "handle=%d, pending=%d, ncp=%d\n", __func__, NG_NODE_NAME(unit->node),
1149 con->con_handle, con->pending, p);
1154 /* Update buffer descriptor */
1155 if (con->link_type != NG_HCI_LINK_SCO)
1156 NG_HCI_BUFF_ACL_FREE(unit->buffer, p);
1158 NG_HCI_BUFF_SCO_FREE(unit->buffer, p);
1161 "%s: %s - invalid connection handle=%d\n",
1162 __func__, NG_NODE_NAME(unit->node), h);
1167 /* Send more data */
1168 ng_hci_send_data(unit);
1171 } /* num_compl_pkts */
1173 /* Mode change event */
1175 mode_change(ng_hci_unit_p unit, struct mbuf *event)
1177 ng_hci_mode_change_ep *ep = NULL;
1178 ng_hci_unit_con_p con = NULL;
1181 NG_HCI_M_PULLUP(event, sizeof(*ep));
1185 ep = mtod(event, ng_hci_mode_change_ep *);
1187 if (ep->status == 0) {
1188 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1190 con = ng_hci_con_by_handle(unit, h);
1193 "%s: %s - invalid connection handle=%d\n",
1194 __func__, NG_NODE_NAME(unit->node), h);
1196 } else if (con->link_type != NG_HCI_LINK_ACL) {
1198 "%s: %s - invalid link type=%d\n",
1199 __func__, NG_NODE_NAME(unit->node),
1203 con->mode = ep->unit_mode;
1206 "%s: %s - failed to change mode, status=%d\n",
1207 __func__, NG_NODE_NAME(unit->node), ep->status);
1214 /* Data buffer overflow event */
1216 data_buffer_overflow(ng_hci_unit_p unit, struct mbuf *event)
1219 "%s: %s - %s data buffer overflow\n",
1220 __func__, NG_NODE_NAME(unit->node),
1221 (*mtod(event, u_int8_t *) == NG_HCI_LINK_ACL)? "ACL" : "SCO");
1226 } /* data_buffer_overflow */
1228 /* Read clock offset complete event */
1230 read_clock_offset_compl(ng_hci_unit_p unit, struct mbuf *event)
1232 ng_hci_read_clock_offset_compl_ep *ep = NULL;
1233 ng_hci_unit_con_p con = NULL;
1234 ng_hci_neighbor_p n = NULL;
1237 NG_HCI_M_PULLUP(event, sizeof(*ep));
1241 ep = mtod(event, ng_hci_read_clock_offset_compl_ep *);
1243 if (ep->status == 0) {
1244 u_int16_t h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1246 con = ng_hci_con_by_handle(unit, h);
1249 "%s: %s - invalid connection handle=%d\n",
1250 __func__, NG_NODE_NAME(unit->node), h);
1255 /* Update cache entry */
1256 n = ng_hci_get_neighbor(unit, &con->bdaddr, NG_HCI_LINK_ACL);
1258 n = ng_hci_new_neighbor(unit);
1264 bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1265 n->addrtype = NG_HCI_LINK_ACL;
1267 getmicrotime(&n->updated);
1269 n->clock_offset = le16toh(ep->clock_offset);
1272 "%s: %s - failed to Read Remote Clock Offset, status=%d\n",
1273 __func__, NG_NODE_NAME(unit->node), ep->status);
1278 } /* read_clock_offset_compl */
1280 /* QoS violation event */
1282 qos_violation(ng_hci_unit_p unit, struct mbuf *event)
1284 ng_hci_qos_violation_ep *ep = NULL;
1285 ng_hci_unit_con_p con = NULL;
1289 NG_HCI_M_PULLUP(event, sizeof(*ep));
1293 ep = mtod(event, ng_hci_qos_violation_ep *);
1295 /* Check if we have this connection handle */
1296 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1297 con = ng_hci_con_by_handle(unit, h);
1300 "%s: %s - invalid connection handle=%d\n",
1301 __func__, NG_NODE_NAME(unit->node), h);
1303 } else if (con->link_type != NG_HCI_LINK_ACL) {
1305 "%s: %s - invalid link type=%d\n",
1306 __func__, NG_NODE_NAME(unit->node), con->link_type);
1308 } else if (con->state != NG_HCI_CON_OPEN) {
1310 "%s: %s - invalid connection state=%d, handle=%d\n",
1311 __func__, NG_NODE_NAME(unit->node), con->state, h);
1313 } else /* Notify upper layer */
1314 error = ng_hci_lp_qos_ind(con);
1319 } /* qos_violation */
1321 /* Page scan mode change event */
1323 page_scan_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1325 ng_hci_page_scan_mode_change_ep *ep = NULL;
1326 ng_hci_neighbor_p n = NULL;
1329 NG_HCI_M_PULLUP(event, sizeof(*ep));
1333 ep = mtod(event, ng_hci_page_scan_mode_change_ep *);
1335 /* Update cache entry */
1336 n = ng_hci_get_neighbor(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1338 n = ng_hci_new_neighbor(unit);
1344 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1345 n->addrtype = NG_HCI_LINK_ACL;
1347 getmicrotime(&n->updated);
1349 n->page_scan_mode = ep->page_scan_mode;
1354 } /* page_scan_mode_change */
1356 /* Page scan repetition mode change event */
1358 page_scan_rep_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1360 ng_hci_page_scan_rep_mode_change_ep *ep = NULL;
1361 ng_hci_neighbor_p n = NULL;
1364 NG_HCI_M_PULLUP(event, sizeof(*ep));
1368 ep = mtod(event, ng_hci_page_scan_rep_mode_change_ep *);
1370 /* Update cache entry */
1371 n = ng_hci_get_neighbor(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1373 n = ng_hci_new_neighbor(unit);
1379 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1380 n->addrtype = NG_HCI_LINK_ACL;
1382 getmicrotime(&n->updated);
1384 n->page_scan_rep_mode = ep->page_scan_rep_mode;
1389 } /* page_scan_rep_mode_change */