]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/bluetooth/hci/ng_hci_evnt.c
MFV r359442: bmake: import -fno-common fix build back from upstream
[FreeBSD/FreeBSD.git] / sys / netgraph / bluetooth / hci / ng_hci_evnt.c
1 /*
2  * ng_hci_evnt.c
3  */
4
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: ng_hci_evnt.c,v 1.6 2003/09/08 18:57:51 max Exp $
33  * $FreeBSD$
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/queue.h>
43 #include <netgraph/ng_message.h>
44 #include <netgraph/netgraph.h>
45 #include <netgraph/bluetooth/include/ng_bluetooth.h>
46 #include <netgraph/bluetooth/include/ng_hci.h>
47 #include <netgraph/bluetooth/hci/ng_hci_var.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>
52
53 /******************************************************************************
54  ******************************************************************************
55  **                     HCI event processing module
56  ******************************************************************************
57  ******************************************************************************/
58
59 /* 
60  * Event processing routines 
61  */
62
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);
81 static int le_event                   (ng_hci_unit_p, struct mbuf *);
82
83 /*
84  * Process HCI event packet
85  */
86  
87 int
88 ng_hci_process_event(ng_hci_unit_p unit, struct mbuf *event)
89 {
90         ng_hci_event_pkt_t      *hdr = NULL;
91         int                      error = 0;
92
93         /* Get event packet header */
94         NG_HCI_M_PULLUP(event, sizeof(*hdr));
95         if (event == NULL)
96                 return (ENOBUFS);
97
98         hdr = mtod(event, ng_hci_event_pkt_t *);
99
100         NG_HCI_INFO(
101 "%s: %s - got HCI event=%#x, length=%d\n",
102                 __func__, NG_NODE_NAME(unit->node), hdr->event, hdr->length);
103
104         /* Get rid of event header and process event */
105         m_adj(event, sizeof(*hdr));
106
107         switch (hdr->event) {
108         case NG_HCI_EVENT_INQUIRY_COMPL:
109         case NG_HCI_EVENT_RETURN_LINK_KEYS:
110         case NG_HCI_EVENT_PIN_CODE_REQ:
111         case NG_HCI_EVENT_LINK_KEY_REQ:
112         case NG_HCI_EVENT_LINK_KEY_NOTIFICATION:
113         case NG_HCI_EVENT_LOOPBACK_COMMAND:
114         case NG_HCI_EVENT_AUTH_COMPL:
115         case NG_HCI_EVENT_CHANGE_CON_LINK_KEY_COMPL:
116         case NG_HCI_EVENT_MASTER_LINK_KEY_COMPL:
117         case NG_HCI_EVENT_FLUSH_OCCUR:  /* XXX Do we have to handle it? */
118         case NG_HCI_EVENT_MAX_SLOT_CHANGE:
119         case NG_HCI_EVENT_CON_PKT_TYPE_CHANGED:
120         case NG_HCI_EVENT_BT_LOGO:
121         case NG_HCI_EVENT_VENDOR:
122         case NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL:
123         case NG_HCI_EVENT_READ_REMOTE_VER_INFO_COMPL:
124                 /* These do not need post processing */
125                 NG_FREE_M(event);
126                 break;
127         case NG_HCI_EVENT_LE:
128                 error = le_event(unit, event);
129                 break;
130
131         case NG_HCI_EVENT_INQUIRY_RESULT:
132                 error = inquiry_result(unit, event);
133                 break;
134
135         case NG_HCI_EVENT_CON_COMPL:
136                 error = con_compl(unit, event);
137                 break;
138
139         case NG_HCI_EVENT_CON_REQ:
140                 error = con_req(unit, event);
141                 break;
142
143         case NG_HCI_EVENT_DISCON_COMPL:
144                 error = discon_compl(unit, event);
145                 break;
146
147         case NG_HCI_EVENT_ENCRYPTION_CHANGE:
148                 error = encryption_change(unit, event);
149                 break;
150
151         case NG_HCI_EVENT_READ_REMOTE_FEATURES_COMPL:
152                 error = read_remote_features_compl(unit, event);
153                 break;
154
155         case NG_HCI_EVENT_QOS_SETUP_COMPL:
156                 error = qos_setup_compl(unit, event);
157                 break;
158
159         case NG_HCI_EVENT_COMMAND_COMPL:
160                 error = ng_hci_process_command_complete(unit, event);
161                 break;
162
163         case NG_HCI_EVENT_COMMAND_STATUS:
164                 error = ng_hci_process_command_status(unit, event);
165                 break;
166
167         case NG_HCI_EVENT_HARDWARE_ERROR:
168                 error = hardware_error(unit, event);
169                 break;
170
171         case NG_HCI_EVENT_ROLE_CHANGE:
172                 error = role_change(unit, event);
173                 break;
174
175         case NG_HCI_EVENT_NUM_COMPL_PKTS:
176                 error = num_compl_pkts(unit, event);
177                 break;
178
179         case NG_HCI_EVENT_MODE_CHANGE:
180                 error = mode_change(unit, event);
181                 break;
182
183         case NG_HCI_EVENT_DATA_BUFFER_OVERFLOW:
184                 error = data_buffer_overflow(unit, event);
185                 break;
186
187         case NG_HCI_EVENT_READ_CLOCK_OFFSET_COMPL:
188                 error = read_clock_offset_compl(unit, event);
189                 break;
190
191         case NG_HCI_EVENT_QOS_VIOLATION:
192                 error = qos_violation(unit, event);
193                 break;
194
195         case NG_HCI_EVENT_PAGE_SCAN_MODE_CHANGE:
196                 error = page_scan_mode_change(unit, event);
197                 break;
198
199         case NG_HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE:
200                 error = page_scan_rep_mode_change(unit, event);
201                 break;
202
203         default:
204                 NG_FREE_M(event);
205                 error = EINVAL;
206                 break;
207         }
208
209         return (error);
210 } /* ng_hci_process_event */
211
212 /*
213  * Send ACL and/or SCO data to the unit driver
214  */
215
216 void
217 ng_hci_send_data(ng_hci_unit_p unit)
218 {
219         int     count;
220
221         /* Send ACL data */
222         NG_HCI_BUFF_ACL_AVAIL(unit->buffer, count);
223
224         NG_HCI_INFO(
225 "%s: %s - sending ACL data packets, count=%d\n",
226                 __func__, NG_NODE_NAME(unit->node), count);
227
228         if (count > 0) {
229                 count = send_data_packets(unit, NG_HCI_LINK_ACL, count);
230                 NG_HCI_STAT_ACL_SENT(unit->stat, count);
231                 NG_HCI_BUFF_ACL_USE(unit->buffer, count);
232         }
233
234         /* Send SCO data */
235         NG_HCI_BUFF_SCO_AVAIL(unit->buffer, count);
236
237         NG_HCI_INFO(
238 "%s: %s - sending SCO data packets, count=%d\n",
239                 __func__, NG_NODE_NAME(unit->node), count);
240
241         if (count > 0) {
242                 count = send_data_packets(unit, NG_HCI_LINK_SCO, count);
243                 NG_HCI_STAT_SCO_SENT(unit->stat, count);
244                 NG_HCI_BUFF_SCO_USE(unit->buffer, count);
245         }
246 } /* ng_hci_send_data */
247
248 /*
249  * Send data packets to the lower layer.
250  */
251
252 static int
253 send_data_packets(ng_hci_unit_p unit, int link_type, int limit)
254 {
255         ng_hci_unit_con_p       con = NULL, winner = NULL;
256         int                     reallink_type;
257         item_p                  item = NULL;
258         int                     min_pending, total_sent, sent, error, v;
259
260         for (total_sent = 0; limit > 0; ) {
261                 min_pending = 0x0fffffff;
262                 winner = NULL;
263
264                 /*
265                  * Find the connection that has has data to send 
266                  * and the smallest number of pending packets
267                  */
268
269                 LIST_FOREACH(con, &unit->con_list, next) {
270                         reallink_type = (con->link_type == NG_HCI_LINK_SCO)?
271                                 NG_HCI_LINK_SCO: NG_HCI_LINK_ACL;
272                         if (reallink_type != link_type){
273                                 continue;
274                         }
275                         if (NG_BT_ITEMQ_LEN(&con->conq) == 0)
276                                 continue;
277         
278                         if (con->pending < min_pending) {
279                                 winner = con;
280                                 min_pending = con->pending;
281                         }
282                 }
283
284                 if (winner == NULL)
285                         break;
286
287                 /* 
288                  * OK, we have a winner now send as much packets as we can
289                  * Count the number of packets we have sent and then sync
290                  * winner connection queue.
291                  */
292
293                 for (sent = 0; limit > 0; limit --, total_sent ++, sent ++) {
294                         NG_BT_ITEMQ_DEQUEUE(&winner->conq, item);
295                         if (item == NULL)
296                                 break;
297                 
298                         NG_HCI_INFO(
299 "%s: %s - sending data packet, handle=%d, len=%d\n",
300                                 __func__, NG_NODE_NAME(unit->node), 
301                                 winner->con_handle, NGI_M(item)->m_pkthdr.len);
302
303                         /* Check if driver hook still there */
304                         v = (unit->drv != NULL && NG_HOOK_IS_VALID(unit->drv));
305                         if (!v || (unit->state & NG_HCI_UNIT_READY) != 
306                                         NG_HCI_UNIT_READY) {
307                                 NG_HCI_ERR(
308 "%s: %s - could not send data. Hook \"%s\" is %svalid, state=%#x\n",
309                                         __func__, NG_NODE_NAME(unit->node),
310                                         NG_HCI_HOOK_DRV, ((v)? "" : "not "),
311                                         unit->state);
312
313                                 NG_FREE_ITEM(item);
314                                 error = ENOTCONN;
315                         } else {
316                                 v = NGI_M(item)->m_pkthdr.len;
317
318                                 /* Give packet to raw hook */
319                                 ng_hci_mtap(unit, NGI_M(item));
320
321                                 /* ... and forward item to the driver */
322                                 NG_FWD_ITEM_HOOK(error, item, unit->drv);
323                         }
324
325                         if (error != 0) {
326                                 NG_HCI_ERR(
327 "%s: %s - could not send data packet, handle=%d, error=%d\n",
328                                         __func__, NG_NODE_NAME(unit->node),
329                                         winner->con_handle, error);
330                                 break;
331                         }
332
333                         winner->pending ++;
334                         NG_HCI_STAT_BYTES_SENT(unit->stat, v);
335                 }
336
337                 /*
338                  * Sync connection queue for the winner
339                  */
340                 sync_con_queue(unit, winner, sent);
341         }
342
343         return (total_sent);
344 } /* send_data_packets */
345
346 /*
347  * Send flow control messages to the upper layer
348  */
349
350 static int
351 sync_con_queue(ng_hci_unit_p unit, ng_hci_unit_con_p con, int completed)
352 {
353         hook_p                           hook = NULL;
354         struct ng_mesg                  *msg = NULL;
355         ng_hci_sync_con_queue_ep        *state = NULL;
356         int                              error;
357
358         hook = (con->link_type != NG_HCI_LINK_SCO)? unit->acl : unit->sco;
359         if (hook == NULL || NG_HOOK_NOT_VALID(hook))
360                 return (ENOTCONN);
361
362         NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_SYNC_CON_QUEUE,
363                 sizeof(*state), M_NOWAIT);
364         if (msg == NULL)
365                 return (ENOMEM);
366
367         state = (ng_hci_sync_con_queue_ep *)(msg->data);
368         state->con_handle = con->con_handle;
369         state->completed = completed;
370
371         NG_SEND_MSG_HOOK(error, unit->node, msg, hook, 0);
372
373         return (error);
374 } /* sync_con_queue */
375 /* le meta event */
376 /* Inquiry result event */
377 static int
378 le_advertizing_report(ng_hci_unit_p unit, struct mbuf *event)
379 {
380         ng_hci_le_advertising_report_ep *ep = NULL;
381         ng_hci_neighbor_p                n = NULL;
382         bdaddr_t                         bdaddr;
383         int                              error = 0;
384         u_int8_t event_type;
385         u_int8_t addr_type;
386
387         NG_HCI_M_PULLUP(event, sizeof(*ep));
388         if (event == NULL)
389                 return (ENOBUFS);
390
391         ep = mtod(event, ng_hci_le_advertising_report_ep *);
392         m_adj(event, sizeof(*ep));
393
394         for (; ep->num_reports > 0; ep->num_reports --) {
395                 /* Get remote unit address */
396                 NG_HCI_M_PULLUP(event, sizeof(u_int8_t));
397                 event_type = *mtod(event, u_int8_t *);
398                 m_adj(event, sizeof(u_int8_t));
399                 NG_HCI_M_PULLUP(event, sizeof(u_int8_t));
400                 addr_type = *mtod(event, u_int8_t *);
401                 m_adj(event, sizeof(u_int8_t));
402
403                 m_copydata(event, 0, sizeof(bdaddr), (caddr_t) &bdaddr);
404                 m_adj(event, sizeof(bdaddr));
405                 
406                 /* Lookup entry in the cache */
407                 n = ng_hci_get_neighbor(unit, &bdaddr, (addr_type) ? NG_HCI_LINK_LE_RANDOM:NG_HCI_LINK_LE_PUBLIC);
408                 if (n == NULL) {
409                         /* Create new entry */
410                         n = ng_hci_new_neighbor(unit);
411                         if (n == NULL) {
412                                 error = ENOMEM;
413                                 break;
414                         }
415                         bcopy(&bdaddr, &n->bdaddr, sizeof(n->bdaddr));
416                         n->addrtype = (addr_type)? NG_HCI_LINK_LE_RANDOM :
417                           NG_HCI_LINK_LE_PUBLIC;
418                         
419                 } else
420                         getmicrotime(&n->updated);
421                 
422                 {
423                         /* 
424                          * TODO: Make these information 
425                          * Available from userland.
426                          */
427                         u_int8_t length_data;
428                         
429                         event = m_pullup(event, sizeof(u_int8_t));
430                         if(event == NULL){
431                                 NG_HCI_WARN("%s: Event datasize Pullup Failed\n", __func__);
432                                 goto out;
433                         }
434                         length_data = *mtod(event, u_int8_t *);
435                         m_adj(event, sizeof(u_int8_t));
436                         n->extinq_size = (length_data < NG_HCI_EXTINQ_MAX)?
437                                 length_data : NG_HCI_EXTINQ_MAX;
438                         
439                         /*Advertizement data*/
440                         event = m_pullup(event, n->extinq_size);
441                         if(event == NULL){
442                                 NG_HCI_WARN("%s: Event data pullup Failed\n", __func__);
443                                 goto out;
444                         }
445                         m_copydata(event, 0, n->extinq_size, n->extinq_data);
446                         m_adj(event, n->extinq_size);
447                         event = m_pullup(event, sizeof(char ));
448                         /*Get RSSI*/
449                         if(event == NULL){
450                                 NG_HCI_WARN("%s: Event rssi pull up Failed\n", __func__);
451                                 
452                                 goto out;
453                         }                               
454                         n->page_scan_mode = *mtod(event, char *);
455                         m_adj(event, sizeof(u_int8_t));
456                 }
457         }
458  out:
459         NG_FREE_M(event);
460
461         return (error);
462 } /* inquiry_result */
463
464 static int le_connection_complete(ng_hci_unit_p unit, struct mbuf *event)
465 {
466         int                      error = 0;
467
468         ng_hci_le_connection_complete_ep        *ep = NULL;
469         ng_hci_unit_con_p        con = NULL;
470         int link_type;
471         uint8_t uclass[3] = {0,0,0};//dummy uclass
472
473         NG_HCI_M_PULLUP(event, sizeof(*ep));
474         if (event == NULL)
475                 return (ENOBUFS);
476
477         ep = mtod(event, ng_hci_le_connection_complete_ep *);
478         link_type = (ep->address_type)? NG_HCI_LINK_LE_RANDOM :
479           NG_HCI_LINK_LE_PUBLIC;
480         /*
481          * Find the first connection descriptor that matches the following:
482          *
483          * 1) con->link_type == link_type
484          * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
485          * 3) con->bdaddr == ep->address
486          */
487         LIST_FOREACH(con, &unit->con_list, next)
488                 if (con->link_type == link_type &&
489                     con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
490                     bcmp(&con->bdaddr, &ep->address, sizeof(bdaddr_t)) == 0)
491                         break;
492
493         /*
494          * Two possible cases:
495          *
496          * 1) We have found connection descriptor. That means upper layer has
497          *    requested this connection via LP_CON_REQ message. In this case
498          *    connection must have timeout set. If ng_hci_con_untimeout() fails
499          *    then timeout message already went into node's queue. In this case
500          *    ignore Connection_Complete event and let timeout deal with it.
501          *
502          * 2) We do not have connection descriptor. That means upper layer
503          *    nas not requested this connection , (less likely) we gave up
504          *    on this connection (timeout) or as node act as slave role.
505          *    The most likely scenario is that
506          *    we have received LE_Create_Connection command 
507          *    from the RAW hook
508          */
509
510         if (con == NULL) {
511                 if (ep->status != 0)
512                         goto out;
513
514                 con = ng_hci_new_con(unit, link_type);
515                 if (con == NULL) {
516                         error = ENOMEM;
517                         goto out;
518                 }
519
520                 con->state = NG_HCI_CON_W4_LP_CON_RSP;
521                 ng_hci_con_timeout(con);
522
523                 bcopy(&ep->address, &con->bdaddr, sizeof(con->bdaddr));
524                 error = ng_hci_lp_con_ind(con, uclass);
525                 if (error != 0) {
526                         ng_hci_con_untimeout(con);
527                         ng_hci_free_con(con);
528                 }
529
530         } else if ((error = ng_hci_con_untimeout(con)) != 0)
531                         goto out;
532
533         /*
534          * Update connection descriptor and send notification 
535          * to the upper layers.
536          */
537
538         con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->handle));
539         con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
540
541         ng_hci_lp_con_cfm(con, ep->status);
542
543         /* Adjust connection state */
544         if (ep->status != 0)
545                 ng_hci_free_con(con);
546         else {
547                 con->state = NG_HCI_CON_OPEN;
548
549                 /*      
550                  * Change link policy for the ACL connections. Enable all 
551                  * supported link modes. Enable Role switch as well if
552                  * device supports it.
553                  */
554
555         }
556
557 out:
558         NG_FREE_M(event);
559
560         return (error);
561
562 }
563
564 static int le_connection_update(ng_hci_unit_p unit, struct mbuf *event)
565 {
566         int error = 0;
567         /*TBD*/
568         
569         NG_FREE_M(event);
570         return error;
571
572 }
573 static int
574 le_event(ng_hci_unit_p unit, struct mbuf *event)
575 {
576         int error = 0;
577         ng_hci_le_ep *lep;
578
579         NG_HCI_M_PULLUP(event, sizeof(*lep));
580         if(event ==NULL){
581                 return ENOBUFS;
582         }
583         lep = mtod(event, ng_hci_le_ep *);
584         m_adj(event, sizeof(*lep));
585         switch(lep->subevent_code){
586         case NG_HCI_LEEV_CON_COMPL:
587                 le_connection_complete(unit, event);
588                 break;
589         case NG_HCI_LEEV_ADVREP:
590                 le_advertizing_report(unit, event);
591                 break;
592         case NG_HCI_LEEV_CON_UPDATE_COMPL:
593                 le_connection_update(unit, event);
594                 break;
595         case NG_HCI_LEEV_READ_REMOTE_FEATURES_COMPL:
596                 //TBD
597           /*FALLTHROUGH*/
598         case NG_HCI_LEEV_LONG_TERM_KEY_REQUEST:
599                 //TBD
600           /*FALLTHROUGH*/
601         default:
602                 NG_FREE_M(event);
603         }
604         return error;
605 }
606
607 /* Inquiry result event */
608 static int
609 inquiry_result(ng_hci_unit_p unit, struct mbuf *event)
610 {
611         ng_hci_inquiry_result_ep        *ep = NULL;
612         ng_hci_neighbor_p                n = NULL;
613         bdaddr_t                         bdaddr;
614         int                              error = 0;
615
616         NG_HCI_M_PULLUP(event, sizeof(*ep));
617         if (event == NULL)
618                 return (ENOBUFS);
619
620         ep = mtod(event, ng_hci_inquiry_result_ep *);
621         m_adj(event, sizeof(*ep));
622
623         for (; ep->num_responses > 0; ep->num_responses --) {
624                 /* Get remote unit address */
625                 m_copydata(event, 0, sizeof(bdaddr), (caddr_t) &bdaddr);
626                 m_adj(event, sizeof(bdaddr));
627
628                 /* Lookup entry in the cache */
629                 n = ng_hci_get_neighbor(unit, &bdaddr, NG_HCI_LINK_ACL);
630                 if (n == NULL) {
631                         /* Create new entry */
632                         n = ng_hci_new_neighbor(unit);
633                         if (n == NULL) {
634                                 error = ENOMEM;
635                                 break;
636                         }
637                 } else
638                         getmicrotime(&n->updated);
639
640                 bcopy(&bdaddr, &n->bdaddr, sizeof(n->bdaddr));
641                 n->addrtype = NG_HCI_LINK_ACL;
642
643                 /* XXX call m_pullup here? */
644
645                 n->page_scan_rep_mode = *mtod(event, u_int8_t *);
646                 m_adj(event, sizeof(u_int8_t));
647
648                 /* page_scan_period_mode */
649                 m_adj(event, sizeof(u_int8_t));
650
651                 n->page_scan_mode = *mtod(event, u_int8_t *);
652                 m_adj(event, sizeof(u_int8_t));
653
654                 /* class */
655                 m_adj(event, NG_HCI_CLASS_SIZE);
656
657                 /* clock offset */
658                 m_copydata(event, 0, sizeof(n->clock_offset), 
659                         (caddr_t) &n->clock_offset);
660                 n->clock_offset = le16toh(n->clock_offset);
661         }
662
663         NG_FREE_M(event);
664
665         return (error);
666 } /* inquiry_result */
667
668 /* Connection complete event */
669 static int
670 con_compl(ng_hci_unit_p unit, struct mbuf *event)
671 {
672         ng_hci_con_compl_ep     *ep = NULL;
673         ng_hci_unit_con_p        con = NULL;
674         int                      error = 0;
675
676         NG_HCI_M_PULLUP(event, sizeof(*ep));
677         if (event == NULL)
678                 return (ENOBUFS);
679
680         ep = mtod(event, ng_hci_con_compl_ep *);
681
682         /*
683          * Find the first connection descriptor that matches the following:
684          *
685          * 1) con->link_type == ep->link_type
686          * 2) con->state == NG_HCI_CON_W4_CONN_COMPLETE
687          * 3) con->bdaddr == ep->bdaddr
688          */
689
690         LIST_FOREACH(con, &unit->con_list, next)
691                 if (con->link_type == ep->link_type &&
692                     con->state == NG_HCI_CON_W4_CONN_COMPLETE &&
693                     bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
694                         break;
695
696         /*
697          * Two possible cases:
698          *
699          * 1) We have found connection descriptor. That means upper layer has
700          *    requested this connection via LP_CON_REQ message. In this case
701          *    connection must have timeout set. If ng_hci_con_untimeout() fails
702          *    then timeout message already went into node's queue. In this case
703          *    ignore Connection_Complete event and let timeout deal with it.
704          *
705          * 2) We do not have connection descriptor. That means upper layer
706          *    nas not requested this connection or (less likely) we gave up
707          *    on this connection (timeout). The most likely scenario is that
708          *    we have received Create_Connection/Add_SCO_Connection command 
709          *    from the RAW hook
710          */
711
712         if (con == NULL) {
713                 if (ep->status != 0)
714                         goto out;
715
716                 con = ng_hci_new_con(unit, ep->link_type);
717                 if (con == NULL) {
718                         error = ENOMEM;
719                         goto out;
720                 }
721
722                 bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
723         } else if ((error = ng_hci_con_untimeout(con)) != 0)
724                         goto out;
725
726         /*
727          * Update connection descriptor and send notification 
728          * to the upper layers.
729          */
730
731         con->con_handle = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
732         con->encryption_mode = ep->encryption_mode;
733
734         ng_hci_lp_con_cfm(con, ep->status);
735
736         /* Adjust connection state */
737         if (ep->status != 0)
738                 ng_hci_free_con(con);
739         else {
740                 con->state = NG_HCI_CON_OPEN;
741
742                 /*      
743                  * Change link policy for the ACL connections. Enable all 
744                  * supported link modes. Enable Role switch as well if
745                  * device supports it.
746                  */
747
748                 if (ep->link_type == NG_HCI_LINK_ACL) {
749                         struct __link_policy {
750                                 ng_hci_cmd_pkt_t                         hdr;
751                                 ng_hci_write_link_policy_settings_cp     cp;
752                         } __attribute__ ((packed))                      *lp;
753                         struct mbuf                                     *m;
754
755                         MGETHDR(m, M_NOWAIT, MT_DATA);
756                         if (m != NULL) {
757                                 m->m_pkthdr.len = m->m_len = sizeof(*lp);
758                                 lp = mtod(m, struct __link_policy *);
759
760                                 lp->hdr.type = NG_HCI_CMD_PKT;
761                                 lp->hdr.opcode = htole16(NG_HCI_OPCODE(
762                                         NG_HCI_OGF_LINK_POLICY,
763                                         NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS));
764                                 lp->hdr.length = sizeof(lp->cp);
765
766                                 lp->cp.con_handle = ep->con_handle;
767
768                                 lp->cp.settings = 0;
769                                 if ((unit->features[0] & NG_HCI_LMP_SWITCH) &&
770                                     unit->role_switch)
771                                         lp->cp.settings |= 0x1;
772                                 if (unit->features[0] & NG_HCI_LMP_HOLD_MODE)
773                                         lp->cp.settings |= 0x2;
774                                 if (unit->features[0] & NG_HCI_LMP_SNIFF_MODE)
775                                         lp->cp.settings |= 0x4;
776                                 if (unit->features[1] & NG_HCI_LMP_PARK_MODE)
777                                         lp->cp.settings |= 0x8;
778
779                                 lp->cp.settings &= unit->link_policy_mask;
780                                 lp->cp.settings = htole16(lp->cp.settings);
781
782                                 NG_BT_MBUFQ_ENQUEUE(&unit->cmdq, m);
783                                 if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING))
784                                         ng_hci_send_command(unit);
785                         }
786                 }
787         }
788 out:
789         NG_FREE_M(event);
790
791         return (error);
792 } /* con_compl */
793
794 /* Connection request event */
795 static int
796 con_req(ng_hci_unit_p unit, struct mbuf *event)
797 {
798         ng_hci_con_req_ep       *ep = NULL;
799         ng_hci_unit_con_p        con = NULL;
800         int                      error = 0;
801
802         NG_HCI_M_PULLUP(event, sizeof(*ep));
803         if (event == NULL)
804                 return (ENOBUFS);
805
806         ep = mtod(event, ng_hci_con_req_ep *);
807
808         /*
809          * Find the first connection descriptor that matches the following:
810          *
811          * 1) con->link_type == ep->link_type
812          *
813          * 2) con->state == NG_HCI_CON_W4_LP_CON_RSP ||
814          *    con->state == NG_HCI_CON_W4_CONN_COMPL
815          * 
816          * 3) con->bdaddr == ep->bdaddr
817          *
818          * Possible cases:
819          *
820          * 1) We do not have connection descriptor. This is simple. Create
821          *    new fresh connection descriptor and send notification to the
822          *    appropriate upstream hook (based on link_type).
823          *
824          * 2) We found connection handle. This is more complicated.
825          * 
826          * 2.1) ACL links
827          *
828          *      Since only one ACL link can exist between each pair of
829          *      units then we have a race. Our upper layer has requested 
830          *      an ACL connection to the remote unit, but we did not send 
831          *      command yet. At the same time the remote unit has requested
832          *      an ACL connection from us. In this case we will ignore 
833          *      Connection_Request event. This probably will cause connect
834          *      failure on both units.
835          *
836          * 2.2) SCO links
837          *
838          *      The spec on page 45 says :
839          *
840          *      "The master can support up to three SCO links to the same 
841          *       slave or to different slaves. A slave can support up to 
842          *       three SCO links from the same master, or two SCO links if 
843          *       the links originate from different masters."
844          *
845          *      The only problem is how to handle multiple SCO links between
846          *      matster and slave. For now we will assume that multiple SCO
847          *      links MUST be opened one after another. 
848          */
849
850         LIST_FOREACH(con, &unit->con_list, next)
851                 if (con->link_type == ep->link_type &&
852                     (con->state == NG_HCI_CON_W4_LP_CON_RSP ||
853                      con->state == NG_HCI_CON_W4_CONN_COMPLETE) &&
854                     bcmp(&con->bdaddr, &ep->bdaddr, sizeof(bdaddr_t)) == 0)
855                         break;
856
857         if (con == NULL) {
858                 con = ng_hci_new_con(unit, ep->link_type);
859                 if (con != NULL) {
860                         bcopy(&ep->bdaddr, &con->bdaddr, sizeof(con->bdaddr));
861
862                         con->state = NG_HCI_CON_W4_LP_CON_RSP;
863                         ng_hci_con_timeout(con);
864
865                         error = ng_hci_lp_con_ind(con, ep->uclass);
866                         if (error != 0) {
867                                 ng_hci_con_untimeout(con);
868                                 ng_hci_free_con(con);
869                         }
870                 } else
871                         error = ENOMEM;
872         }
873
874         NG_FREE_M(event);
875
876         return (error);
877 } /* con_req */
878
879 /* Disconnect complete event */
880 static int
881 discon_compl(ng_hci_unit_p unit, struct mbuf *event)
882 {
883         ng_hci_discon_compl_ep  *ep = NULL;
884         ng_hci_unit_con_p        con = NULL;
885         int                      error = 0;
886         u_int16_t                h;
887
888         NG_HCI_M_PULLUP(event, sizeof(*ep));
889         if (event == NULL)
890                 return (ENOBUFS);
891
892         ep = mtod(event, ng_hci_discon_compl_ep *);
893
894         /* 
895          * XXX 
896          * Do we have to send notification if ep->status != 0? 
897          * For now we will send notification for both ACL and SCO connections
898          * ONLY if ep->status == 0.
899          */
900
901         if (ep->status == 0) {
902                 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
903                 con = ng_hci_con_by_handle(unit, h);
904                 if (con != NULL) {
905                         error = ng_hci_lp_discon_ind(con, ep->reason);
906
907                         /* Remove all timeouts (if any) */
908                         if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
909                                 ng_hci_con_untimeout(con);
910
911                         ng_hci_free_con(con);
912                 } else {
913                         NG_HCI_ALERT(
914 "%s: %s - invalid connection handle=%d\n",
915                                 __func__, NG_NODE_NAME(unit->node), h);
916                         error = ENOENT;
917                 }
918         }
919
920         NG_FREE_M(event);
921
922         return (error);
923 } /* discon_compl */
924
925 /* Encryption change event */
926 static int
927 encryption_change(ng_hci_unit_p unit, struct mbuf *event)
928 {
929         ng_hci_encryption_change_ep     *ep = NULL;
930         ng_hci_unit_con_p                con = NULL;
931         int                              error = 0;
932         u_int16_t       h;
933
934         NG_HCI_M_PULLUP(event, sizeof(*ep));
935         if (event == NULL)
936                 return (ENOBUFS);
937
938         ep = mtod(event, ng_hci_encryption_change_ep *);
939         h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
940         con = ng_hci_con_by_handle(unit, h);
941
942         if (ep->status == 0) {
943                 if (con == NULL) {
944                         NG_HCI_ALERT(
945 "%s: %s - invalid connection handle=%d\n",
946                                 __func__, NG_NODE_NAME(unit->node), h);
947                         error = ENOENT;
948                 } else if (con->link_type == NG_HCI_LINK_SCO) {
949                         NG_HCI_ALERT(
950 "%s: %s - invalid link type=%d\n",
951                                 __func__, NG_NODE_NAME(unit->node), 
952                                 con->link_type);
953                         error = EINVAL;
954                 } else if (ep->encryption_enable)
955                         /* XXX is that true? */
956                         con->encryption_mode = NG_HCI_ENCRYPTION_MODE_P2P;
957                 else
958                         con->encryption_mode = NG_HCI_ENCRYPTION_MODE_NONE;
959         } else
960                 NG_HCI_ERR(
961 "%s: %s - failed to change encryption mode, status=%d\n",
962                         __func__, NG_NODE_NAME(unit->node), ep->status);
963
964         /*Anyway, propagete encryption status to upper layer*/
965         ng_hci_lp_enc_change(con, con->encryption_mode);
966
967         NG_FREE_M(event);
968
969         return (error);
970 } /* encryption_change */
971
972 /* Read remote feature complete event */
973 static int
974 read_remote_features_compl(ng_hci_unit_p unit, struct mbuf *event)
975 {
976         ng_hci_read_remote_features_compl_ep    *ep = NULL;
977         ng_hci_unit_con_p                        con = NULL;
978         ng_hci_neighbor_p                        n = NULL;
979         u_int16_t                                h;
980         int                                      error = 0;
981
982         NG_HCI_M_PULLUP(event, sizeof(*ep));
983         if (event == NULL)
984                 return (ENOBUFS);
985
986         ep = mtod(event, ng_hci_read_remote_features_compl_ep *);
987
988         if (ep->status == 0) {
989                 /* Check if we have this connection handle */
990                 h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
991                 con = ng_hci_con_by_handle(unit, h);
992                 if (con == NULL) {
993                         NG_HCI_ALERT(
994 "%s: %s - invalid connection handle=%d\n",
995                                 __func__, NG_NODE_NAME(unit->node), h);
996                         error = ENOENT;
997                         goto out;
998                 }
999
1000                 /* Update cache entry */
1001                 n = ng_hci_get_neighbor(unit, &con->bdaddr, NG_HCI_LINK_ACL);
1002                 if (n == NULL) {
1003                         n = ng_hci_new_neighbor(unit);
1004                         if (n == NULL) {
1005                                 error = ENOMEM;
1006                                 goto out;
1007                         }
1008
1009                         bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1010                         n->addrtype = NG_HCI_LINK_ACL;
1011                 } else
1012                         getmicrotime(&n->updated);
1013
1014                 bcopy(ep->features, n->features, sizeof(n->features));
1015         } else
1016                 NG_HCI_ERR(
1017 "%s: %s - failed to read remote unit features, status=%d\n",
1018                         __func__, NG_NODE_NAME(unit->node), ep->status);
1019 out:
1020         NG_FREE_M(event);
1021
1022         return (error);
1023 } /* read_remote_features_compl */
1024
1025 /* QoS setup complete event */
1026 static int
1027 qos_setup_compl(ng_hci_unit_p unit, struct mbuf *event)
1028 {
1029         ng_hci_qos_setup_compl_ep       *ep = NULL;
1030         ng_hci_unit_con_p                con = NULL;
1031         u_int16_t                        h;
1032         int                              error = 0;
1033
1034         NG_HCI_M_PULLUP(event, sizeof(*ep));
1035         if (event == NULL)
1036                 return (ENOBUFS);
1037
1038         ep = mtod(event, ng_hci_qos_setup_compl_ep *);
1039
1040         /* Check if we have this connection handle */
1041         h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1042         con = ng_hci_con_by_handle(unit, h);
1043         if (con == NULL) {
1044                 NG_HCI_ALERT(
1045 "%s: %s - invalid connection handle=%d\n",
1046                         __func__, NG_NODE_NAME(unit->node), h);
1047                 error = ENOENT;
1048         } else if (con->link_type != NG_HCI_LINK_ACL) {
1049                 NG_HCI_ALERT(
1050 "%s: %s - invalid link type=%d, handle=%d\n",
1051                         __func__, NG_NODE_NAME(unit->node), con->link_type, h);
1052                 error = EINVAL;
1053         } else if (con->state != NG_HCI_CON_OPEN) {
1054                 NG_HCI_ALERT(
1055 "%s: %s - invalid connection state=%d, handle=%d\n",
1056                         __func__, NG_NODE_NAME(unit->node), 
1057                         con->state, h);
1058                 error = EINVAL;
1059         } else /* Notify upper layer */
1060                 error = ng_hci_lp_qos_cfm(con, ep->status);
1061
1062         NG_FREE_M(event);
1063
1064         return (error);
1065 } /* qos_setup_compl */
1066
1067 /* Hardware error event */
1068 static int
1069 hardware_error(ng_hci_unit_p unit, struct mbuf *event)
1070 {
1071         NG_HCI_ALERT(
1072 "%s: %s - hardware error %#x\n",
1073                 __func__, NG_NODE_NAME(unit->node), *mtod(event, u_int8_t *));
1074
1075         NG_FREE_M(event);
1076
1077         return (0);
1078 } /* hardware_error */
1079
1080 /* Role change event */
1081 static int
1082 role_change(ng_hci_unit_p unit, struct mbuf *event)
1083 {
1084         ng_hci_role_change_ep   *ep = NULL;
1085         ng_hci_unit_con_p        con = NULL;
1086
1087         NG_HCI_M_PULLUP(event, sizeof(*ep));
1088         if (event == NULL)
1089                 return (ENOBUFS);
1090
1091         ep = mtod(event, ng_hci_role_change_ep *);
1092
1093         if (ep->status == 0) {
1094                 /* XXX shoud we also change "role" for SCO connections? */
1095                 con = ng_hci_con_by_bdaddr(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1096                 if (con != NULL)
1097                         con->role = ep->role;
1098                 else
1099                         NG_HCI_ALERT(
1100 "%s: %s - ACL connection does not exist, bdaddr=%x:%x:%x:%x:%x:%x\n",
1101                                 __func__, NG_NODE_NAME(unit->node),
1102                                 ep->bdaddr.b[5], ep->bdaddr.b[4], 
1103                                 ep->bdaddr.b[3], ep->bdaddr.b[2], 
1104                                 ep->bdaddr.b[1], ep->bdaddr.b[0]);
1105         } else
1106                 NG_HCI_ERR(
1107 "%s: %s - failed to change role, status=%d, bdaddr=%x:%x:%x:%x:%x:%x\n",
1108                         __func__, NG_NODE_NAME(unit->node), ep->status,
1109                         ep->bdaddr.b[5], ep->bdaddr.b[4], ep->bdaddr.b[3],
1110                         ep->bdaddr.b[2], ep->bdaddr.b[1], ep->bdaddr.b[0]);
1111
1112         NG_FREE_M(event);
1113
1114         return (0);
1115 } /* role_change */
1116
1117 /* Number of completed packets event */
1118 static int
1119 num_compl_pkts(ng_hci_unit_p unit, struct mbuf *event)
1120 {
1121         ng_hci_num_compl_pkts_ep        *ep = NULL;
1122         ng_hci_unit_con_p                con = NULL;
1123         u_int16_t                        h, p;
1124
1125         NG_HCI_M_PULLUP(event, sizeof(*ep));
1126         if (event == NULL)
1127                 return (ENOBUFS);
1128
1129         ep = mtod(event, ng_hci_num_compl_pkts_ep *);
1130         m_adj(event, sizeof(*ep));
1131
1132         for (; ep->num_con_handles > 0; ep->num_con_handles --) {
1133                 /* Get connection handle */
1134                 m_copydata(event, 0, sizeof(h), (caddr_t) &h);
1135                 m_adj(event, sizeof(h));
1136                 h = NG_HCI_CON_HANDLE(le16toh(h));
1137
1138                 /* Get number of completed packets */
1139                 m_copydata(event, 0, sizeof(p), (caddr_t) &p);
1140                 m_adj(event, sizeof(p));
1141                 p = le16toh(p);
1142
1143                 /* Check if we have this connection handle */
1144                 con = ng_hci_con_by_handle(unit, h);
1145                 if (con != NULL) {
1146                         con->pending -= p;
1147                         if (con->pending < 0) {
1148                                 NG_HCI_WARN(
1149 "%s: %s - pending packet counter is out of sync! " \
1150 "handle=%d, pending=%d, ncp=%d\n",      __func__, NG_NODE_NAME(unit->node), 
1151                                         con->con_handle, con->pending, p);
1152
1153                                 con->pending = 0;
1154                         }
1155
1156                         /* Update buffer descriptor */
1157                         if (con->link_type != NG_HCI_LINK_SCO)
1158                                 NG_HCI_BUFF_ACL_FREE(unit->buffer, p);
1159                         else 
1160                                 NG_HCI_BUFF_SCO_FREE(unit->buffer, p);
1161                 } else
1162                         NG_HCI_ALERT(
1163 "%s: %s - invalid connection handle=%d\n",
1164                                 __func__, NG_NODE_NAME(unit->node), h);
1165         }
1166
1167         NG_FREE_M(event);
1168
1169         /* Send more data */
1170         ng_hci_send_data(unit);
1171
1172         return (0);
1173 } /* num_compl_pkts */
1174
1175 /* Mode change event */
1176 static int
1177 mode_change(ng_hci_unit_p unit, struct mbuf *event)
1178 {
1179         ng_hci_mode_change_ep   *ep = NULL;
1180         ng_hci_unit_con_p        con = NULL;
1181         int                      error = 0;
1182         
1183         NG_HCI_M_PULLUP(event, sizeof(*ep));
1184         if (event == NULL)
1185                 return (ENOBUFS);
1186
1187         ep = mtod(event, ng_hci_mode_change_ep *);
1188
1189         if (ep->status == 0) {
1190                 u_int16_t       h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1191
1192                 con = ng_hci_con_by_handle(unit, h);
1193                 if (con == NULL) {
1194                         NG_HCI_ALERT(
1195 "%s: %s - invalid connection handle=%d\n",
1196                                 __func__, NG_NODE_NAME(unit->node), h);
1197                         error = ENOENT;
1198                 } else if (con->link_type != NG_HCI_LINK_ACL) {
1199                         NG_HCI_ALERT(
1200 "%s: %s - invalid link type=%d\n",
1201                                 __func__, NG_NODE_NAME(unit->node), 
1202                                 con->link_type);
1203                         error = EINVAL;
1204                 } else
1205                         con->mode = ep->unit_mode;
1206         } else
1207                 NG_HCI_ERR(
1208 "%s: %s - failed to change mode, status=%d\n",
1209                         __func__, NG_NODE_NAME(unit->node), ep->status);
1210
1211         NG_FREE_M(event);
1212
1213         return (error);
1214 } /* mode_change */
1215
1216 /* Data buffer overflow event */
1217 static int
1218 data_buffer_overflow(ng_hci_unit_p unit, struct mbuf *event)
1219 {
1220         NG_HCI_ALERT(
1221 "%s: %s - %s data buffer overflow\n",
1222                 __func__, NG_NODE_NAME(unit->node),
1223                 (*mtod(event, u_int8_t *) == NG_HCI_LINK_ACL)? "ACL" : "SCO");
1224
1225         NG_FREE_M(event);
1226
1227         return (0);
1228 } /* data_buffer_overflow */
1229
1230 /* Read clock offset complete event */
1231 static int
1232 read_clock_offset_compl(ng_hci_unit_p unit, struct mbuf *event)
1233 {
1234         ng_hci_read_clock_offset_compl_ep       *ep = NULL;
1235         ng_hci_unit_con_p                        con = NULL;
1236         ng_hci_neighbor_p                        n = NULL;
1237         int                                      error = 0;
1238
1239         NG_HCI_M_PULLUP(event, sizeof(*ep));
1240         if (event == NULL)
1241                 return (ENOBUFS);
1242
1243         ep = mtod(event, ng_hci_read_clock_offset_compl_ep *);
1244
1245         if (ep->status == 0) {
1246                 u_int16_t       h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1247
1248                 con = ng_hci_con_by_handle(unit, h);
1249                 if (con == NULL) {
1250                         NG_HCI_ALERT(
1251 "%s: %s - invalid connection handle=%d\n",
1252                                 __func__, NG_NODE_NAME(unit->node), h);
1253                         error = ENOENT;
1254                         goto out;
1255                 }
1256
1257                 /* Update cache entry */
1258                 n = ng_hci_get_neighbor(unit, &con->bdaddr, NG_HCI_LINK_ACL);
1259                 if (n == NULL) {
1260                         n = ng_hci_new_neighbor(unit);
1261                         if (n == NULL) {
1262                                 error = ENOMEM;
1263                                 goto out;
1264                         }
1265
1266                         bcopy(&con->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1267                         n->addrtype = NG_HCI_LINK_ACL;
1268                 } else
1269                         getmicrotime(&n->updated);
1270
1271                 n->clock_offset = le16toh(ep->clock_offset);
1272         } else
1273                 NG_HCI_ERR(
1274 "%s: %s - failed to Read Remote Clock Offset, status=%d\n",
1275                         __func__, NG_NODE_NAME(unit->node), ep->status);
1276 out:
1277         NG_FREE_M(event);
1278
1279         return (error);
1280 } /* read_clock_offset_compl */
1281
1282 /* QoS violation event */
1283 static int
1284 qos_violation(ng_hci_unit_p unit, struct mbuf *event)
1285 {
1286         ng_hci_qos_violation_ep *ep = NULL;
1287         ng_hci_unit_con_p        con = NULL;
1288         u_int16_t                h;
1289         int                      error = 0;
1290
1291         NG_HCI_M_PULLUP(event, sizeof(*ep));
1292         if (event == NULL)
1293                 return (ENOBUFS);
1294
1295         ep = mtod(event, ng_hci_qos_violation_ep *);
1296
1297         /* Check if we have this connection handle */
1298         h = NG_HCI_CON_HANDLE(le16toh(ep->con_handle));
1299         con = ng_hci_con_by_handle(unit, h);
1300         if (con == NULL) {
1301                 NG_HCI_ALERT(
1302 "%s: %s - invalid connection handle=%d\n",
1303                         __func__, NG_NODE_NAME(unit->node), h);
1304                 error = ENOENT;
1305         } else if (con->link_type != NG_HCI_LINK_ACL) {
1306                 NG_HCI_ALERT(
1307 "%s: %s - invalid link type=%d\n",
1308                         __func__, NG_NODE_NAME(unit->node), con->link_type);
1309                 error = EINVAL;
1310         } else if (con->state != NG_HCI_CON_OPEN) {
1311                 NG_HCI_ALERT(
1312 "%s: %s - invalid connection state=%d, handle=%d\n",
1313                         __func__, NG_NODE_NAME(unit->node), con->state, h);
1314                 error = EINVAL;
1315         } else /* Notify upper layer */
1316                 error = ng_hci_lp_qos_ind(con); 
1317
1318         NG_FREE_M(event);
1319
1320         return (error);
1321 } /* qos_violation */
1322
1323 /* Page scan mode change event */
1324 static int
1325 page_scan_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1326 {
1327         ng_hci_page_scan_mode_change_ep *ep = NULL;
1328         ng_hci_neighbor_p                n = NULL;
1329         int                              error = 0;
1330
1331         NG_HCI_M_PULLUP(event, sizeof(*ep));
1332         if (event == NULL)
1333                 return (ENOBUFS);
1334
1335         ep = mtod(event, ng_hci_page_scan_mode_change_ep *);
1336
1337         /* Update cache entry */
1338         n = ng_hci_get_neighbor(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1339         if (n == NULL) {
1340                 n = ng_hci_new_neighbor(unit);
1341                 if (n == NULL) {
1342                         error = ENOMEM;
1343                         goto out;
1344                 }
1345
1346                 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1347                 n->addrtype = NG_HCI_LINK_ACL;
1348         } else
1349                 getmicrotime(&n->updated);
1350
1351         n->page_scan_mode = ep->page_scan_mode;
1352 out:
1353         NG_FREE_M(event);
1354
1355         return (error);
1356 } /* page_scan_mode_change */
1357
1358 /* Page scan repetition mode change event */
1359 static int
1360 page_scan_rep_mode_change(ng_hci_unit_p unit, struct mbuf *event)
1361 {
1362         ng_hci_page_scan_rep_mode_change_ep     *ep = NULL;
1363         ng_hci_neighbor_p                        n = NULL;
1364         int                                      error = 0;
1365
1366         NG_HCI_M_PULLUP(event, sizeof(*ep));
1367         if (event == NULL)
1368                 return (ENOBUFS);
1369
1370         ep = mtod(event, ng_hci_page_scan_rep_mode_change_ep *);
1371
1372         /* Update cache entry */
1373         n = ng_hci_get_neighbor(unit, &ep->bdaddr, NG_HCI_LINK_ACL);
1374         if (n == NULL) {
1375                 n = ng_hci_new_neighbor(unit);
1376                 if (n == NULL) {
1377                         error = ENOMEM;
1378                         goto out;
1379                 }
1380
1381                 bcopy(&ep->bdaddr, &n->bdaddr, sizeof(n->bdaddr));
1382                 n->addrtype = NG_HCI_LINK_ACL;
1383         } else
1384                 getmicrotime(&n->updated);
1385
1386         n->page_scan_rep_mode = ep->page_scan_rep_mode;
1387 out:
1388         NG_FREE_M(event);
1389
1390         return (error);
1391 } /* page_scan_rep_mode_change */
1392