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