aboutsummaryrefslogtreecommitdiff
path: root/sys/netgraph/bluetooth/hci/ng_hci_cmds.c
blob: cf192bfb346908f6d6cec6f193d3402b61b5ef4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
 * ng_hci_cmds.c
 */

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: ng_hci_cmds.c,v 1.4 2003/09/08 18:57:51 max Exp $
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/endian.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/bluetooth/include/ng_bluetooth.h>
#include <netgraph/bluetooth/include/ng_hci.h>
#include <netgraph/bluetooth/hci/ng_hci_var.h>
#include <netgraph/bluetooth/hci/ng_hci_cmds.h>
#include <netgraph/bluetooth/hci/ng_hci_evnt.h>
#include <netgraph/bluetooth/hci/ng_hci_ulpi.h>
#include <netgraph/bluetooth/hci/ng_hci_misc.h>

/******************************************************************************
 ******************************************************************************
 **                     HCI commands processing module
 ******************************************************************************
 ******************************************************************************/

#undef	min
#define	min(a, b)	((a) < (b))? (a) : (b)

static int  complete_command (ng_hci_unit_p, int, struct mbuf **);

static int process_link_control_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
static int process_link_policy_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
static int process_hc_baseband_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
static int process_info_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
static int process_status_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
static int process_testing_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);
static int process_le_params
	(ng_hci_unit_p, u_int16_t, struct mbuf *, struct mbuf *);

static int process_link_control_status
	(ng_hci_unit_p, ng_hci_command_status_ep *, struct mbuf *);
static int process_link_policy_status
	(ng_hci_unit_p, ng_hci_command_status_ep *, struct mbuf *);
static int process_le_status
	(ng_hci_unit_p, ng_hci_command_status_ep *, struct mbuf *);

/*
 * Send HCI command to the driver.
 */

int
ng_hci_send_command(ng_hci_unit_p unit)
{
	struct mbuf	*m0 = NULL, *m = NULL;
	int		 free, error = 0;

	/* Check if other command is pending */
	if (unit->state & NG_HCI_UNIT_COMMAND_PENDING)
		return (0);

	/* Check if unit can accept our command */
	NG_HCI_BUFF_CMD_GET(unit->buffer, free);
	if (free == 0)
		return (0);

	/* Check if driver hook is still ok */
	if (unit->drv == NULL || NG_HOOK_NOT_VALID(unit->drv)) {
		NG_HCI_WARN(
"%s: %s - hook \"%s\" is not connected or valid\n",
			__func__, NG_NODE_NAME(unit->node), NG_HCI_HOOK_DRV);

		NG_BT_MBUFQ_DRAIN(&unit->cmdq);

		return (ENOTCONN);
	}

	/* 
	 * Get first command from queue, give it to RAW hook then 
	 * make copy of it and send it to the driver
	 */

	m0 = NG_BT_MBUFQ_FIRST(&unit->cmdq);
	if (m0 == NULL)
		return (0);

	ng_hci_mtap(unit, m0);

	m = m_dup(m0, M_NOWAIT);
	if (m != NULL)
		NG_SEND_DATA_ONLY(error, unit->drv, m);
	else
		error = ENOBUFS;

	if (error != 0)
		NG_HCI_ERR(
"%s: %s - could not send HCI command, error=%d\n",
			__func__, NG_NODE_NAME(unit->node), error);

	/*
	 * Even if we were not able to send command we still pretend
	 * that everything is OK and let timeout handle that.
	 */

	NG_HCI_BUFF_CMD_USE(unit->buffer, 1);
	NG_HCI_STAT_CMD_SENT(unit->stat);
	NG_HCI_STAT_BYTES_SENT(unit->stat, m0->m_pkthdr.len);

	/*
	 * Note: ng_hci_command_timeout() will set 
	 * NG_HCI_UNIT_COMMAND_PENDING flag
	 */

	ng_hci_command_timeout(unit);

	return (0);
} /* ng_hci_send_command */

/*
 * Process HCI Command_Compete event. Complete HCI command, and do post 
 * processing on the command parameters (cp) and command return parameters
 * (e) if required (for example adjust state).
 */

int
ng_hci_process_command_complete(ng_hci_unit_p unit, struct mbuf *e)
{
	ng_hci_command_compl_ep		*ep = NULL;
	struct mbuf			*cp = NULL;
	int				 error = 0;

	/* Get event packet and update command buffer info */
	NG_HCI_M_PULLUP(e, sizeof(*ep));
	if (e == NULL)
		return (ENOBUFS); /* XXX this is bad */

	ep = mtod(e, ng_hci_command_compl_ep *);
        NG_HCI_BUFF_CMD_SET(unit->buffer, ep->num_cmd_pkts);

	/* Check for special NOOP command */
	if (ep->opcode == 0x0000) {
		NG_FREE_M(e);
		goto out;
	}

	/* Try to match first command item in the queue */
	error = complete_command(unit, ep->opcode, &cp);
	if (error != 0) {
		NG_FREE_M(e);
		goto out;
	}

	/* 
	 * Perform post processing on command parameters and return parameters
	 * do it only if status is OK (status == 0). Status is the first byte
	 * of any command return parameters.
	 */

	ep->opcode = le16toh(ep->opcode);
	m_adj(e, sizeof(*ep));

	if (*mtod(e, u_int8_t *) == 0) { /* XXX m_pullup here? */
		switch (NG_HCI_OGF(ep->opcode)) {
		case NG_HCI_OGF_LINK_CONTROL:
			error = process_link_control_params(unit,
					NG_HCI_OCF(ep->opcode), cp, e);
			break;

		case NG_HCI_OGF_LINK_POLICY:
			error = process_link_policy_params(unit,
					NG_HCI_OCF(ep->opcode), cp, e);
			break;

		case NG_HCI_OGF_HC_BASEBAND:
			error = process_hc_baseband_params(unit,
					NG_HCI_OCF(ep->opcode), cp, e);
			break;

		case NG_HCI_OGF_INFO:
			error = process_info_params(unit,
					NG_HCI_OCF(ep->opcode), cp, e);
			break;

		case NG_HCI_OGF_STATUS:
			error = process_status_params(unit,
					NG_HCI_OCF(ep->opcode), cp, e);
			break;

		case NG_HCI_OGF_TESTING:
			error = process_testing_params(unit,
					NG_HCI_OCF(ep->opcode), cp, e);
			break;
		case NG_HCI_OGF_LE:
			error = process_le_params(unit,
					  NG_HCI_OCF(ep->opcode), cp, e);
			break;
		case NG_HCI_OGF_BT_LOGO:
		case NG_HCI_OGF_VENDOR:
			NG_FREE_M(cp);
			NG_FREE_M(e);
			break;

		default:
			NG_FREE_M(cp);
			NG_FREE_M(e);
			error = EINVAL;
			break;
		}
	} else {
		NG_HCI_ERR(
"%s: %s - HCI command failed, OGF=%#x, OCF=%#x, status=%#x\n",
			__func__, NG_NODE_NAME(unit->node),
			NG_HCI_OGF(ep->opcode), NG_HCI_OCF(ep->opcode), 
			*mtod(e, u_int8_t *));

		NG_FREE_M(cp);
		NG_FREE_M(e);
	}
out:
	ng_hci_send_command(unit);

	return (error);
} /* ng_hci_process_command_complete */

/*
 * Process HCI Command_Status event. Check the status (mst) and do post 
 * processing (if required).
 */

int
ng_hci_process_command_status(ng_hci_unit_p unit, struct mbuf *e)
{
	ng_hci_command_status_ep	*ep = NULL;
	struct mbuf			*cp = NULL;
	int				 error = 0;

	/* Update command buffer info */
	NG_HCI_M_PULLUP(e, sizeof(*ep));
	if (e == NULL)
		return (ENOBUFS); /* XXX this is bad */

	ep = mtod(e, ng_hci_command_status_ep *);
	NG_HCI_BUFF_CMD_SET(unit->buffer, ep->num_cmd_pkts);

	/* Check for special NOOP command */
	if (ep->opcode == 0x0000)
		goto out;

	/* Try to match first command item in the queue */
	error = complete_command(unit, ep->opcode, &cp);
        if (error != 0)
		goto out;

	/* 
	 * Perform post processing on HCI Command_Status event
	 */

	ep->opcode = le16toh(ep->opcode);

	switch (NG_HCI_OGF(ep->opcode)) {
	case NG_HCI_OGF_LINK_CONTROL:
		error = process_link_control_status(unit, ep, cp);
		break;

	case NG_HCI_OGF_LINK_POLICY:
		error = process_link_policy_status(unit, ep, cp);
		break;
	case NG_HCI_OGF_LE:
		error = process_le_status(unit, ep, cp);
		break;
	case NG_HCI_OGF_BT_LOGO:
	case NG_HCI_OGF_VENDOR:
		NG_FREE_M(cp);
		break;

	case NG_HCI_OGF_HC_BASEBAND:
	case NG_HCI_OGF_INFO:
	case NG_HCI_OGF_STATUS:
	case NG_HCI_OGF_TESTING:
	default:
		NG_FREE_M(cp);
		error = EINVAL;
		break;
	}
out:
	NG_FREE_M(e);
	ng_hci_send_command(unit);

	return (error);
} /* ng_hci_process_command_status */

/*
 * Complete queued HCI command. 
 */

static int
complete_command(ng_hci_unit_p unit, int opcode, struct mbuf **cp)
{
	struct mbuf	*m = NULL;

	/* Check unit state */
	if (!(unit->state & NG_HCI_UNIT_COMMAND_PENDING)) {
		NG_HCI_ALERT(
"%s: %s - no pending command, state=%#x\n",
			__func__, NG_NODE_NAME(unit->node), unit->state);

		return (EINVAL);
	}

	/* Get first command in the queue */
	m = NG_BT_MBUFQ_FIRST(&unit->cmdq);
	if (m == NULL) {
		NG_HCI_ALERT(
"%s: %s - empty command queue?!\n", __func__, NG_NODE_NAME(unit->node));

		return (EINVAL);
	}

	/*
	 * Match command opcode, if does not match - do nothing and 
	 * let timeout handle that.
	 */

	if (mtod(m, ng_hci_cmd_pkt_t *)->opcode != opcode) {
		NG_HCI_ALERT(
"%s: %s - command queue is out of sync\n", __func__, NG_NODE_NAME(unit->node));

		return (EINVAL);
	}

	/* 
	 * Now we can remove command timeout, dequeue completed command
	 * and return command parameters. ng_hci_command_untimeout will
	 * drop NG_HCI_UNIT_COMMAND_PENDING flag.
	 * Note: if ng_hci_command_untimeout() fails (returns non-zero)
	 * then timeout already happened and timeout message went info node
	 * queue. In this case we ignore command completion and pretend
	 * there is a timeout.
	 */

	if (ng_hci_command_untimeout(unit) != 0)
		return (ETIMEDOUT);

	NG_BT_MBUFQ_DEQUEUE(&unit->cmdq, *cp);
	m_adj(*cp, sizeof(ng_hci_cmd_pkt_t));

	return (0);
} /* complete_command */

/*
 * Process HCI command timeout
 */

void
ng_hci_process_command_timeout(node_p node, hook_p hook, void *arg1, int arg2)
{
	ng_hci_unit_p	 unit = NULL;
	struct mbuf	*m = NULL;
	u_int16_t	 opcode;

	if (NG_NODE_NOT_VALID(node)) {
		printf("%s: Netgraph node is not valid\n", __func__);
		return;
	}

	unit = (ng_hci_unit_p) NG_NODE_PRIVATE(node);

	if (unit->state & NG_HCI_UNIT_COMMAND_PENDING) {
		unit->state &= ~NG_HCI_UNIT_COMMAND_PENDING;

		NG_BT_MBUFQ_DEQUEUE(&unit->cmdq, m);
		if (m == NULL) {
			NG_HCI_ALERT(
"%s: %s - command queue is out of sync!\n", __func__, NG_NODE_NAME(unit->node));

			return;
		}

		opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
		NG_FREE_M(m);

		NG_HCI_ERR(
"%s: %s - unable to complete HCI command OGF=%#x, OCF=%#x. Timeout\n",
			__func__, NG_NODE_NAME(unit->node), NG_HCI_OGF(opcode),
			NG_HCI_OCF(opcode));

		/* Try to send more commands */
 		NG_HCI_BUFF_CMD_SET(unit->buffer, 1);
		ng_hci_send_command(unit);
	} else
		NG_HCI_ALERT(
"%s: %s - no pending command\n", __func__, NG_NODE_NAME(unit->node));
} /* ng_hci_process_command_timeout */

/* 
 * Process link command return parameters
 */

static int
process_link_control_params(ng_hci_unit_p unit, u_int16_t ocf, 
		struct mbuf *mcp, struct mbuf *mrp)
{
	int	error  = 0;

	switch (ocf) {
	case NG_HCI_OCF_INQUIRY_CANCEL:
	case NG_HCI_OCF_PERIODIC_INQUIRY:
	case NG_HCI_OCF_EXIT_PERIODIC_INQUIRY:
	case NG_HCI_OCF_LINK_KEY_REP:
	case NG_HCI_OCF_LINK_KEY_NEG_REP: 
	case NG_HCI_OCF_PIN_CODE_REP:
	case NG_HCI_OCF_PIN_CODE_NEG_REP:
		/* These do not need post processing */
		break;

	case NG_HCI_OCF_INQUIRY:
	case NG_HCI_OCF_CREATE_CON:
	case NG_HCI_OCF_DISCON:
	case NG_HCI_OCF_ADD_SCO_CON:
	case NG_HCI_OCF_ACCEPT_CON:
	case NG_HCI_OCF_REJECT_CON:
	case NG_HCI_OCF_CHANGE_CON_PKT_TYPE:
	case NG_HCI_OCF_AUTH_REQ:
	case NG_HCI_OCF_SET_CON_ENCRYPTION:
	case NG_HCI_OCF_CHANGE_CON_LINK_KEY:
	case NG_HCI_OCF_MASTER_LINK_KEY:
	case NG_HCI_OCF_REMOTE_NAME_REQ:
	case NG_HCI_OCF_READ_REMOTE_FEATURES:
	case NG_HCI_OCF_READ_REMOTE_VER_INFO:
	case NG_HCI_OCF_READ_CLOCK_OFFSET:
	default:

		/*
		 * None of these command was supposed to generate 
		 * Command_Complete event. Instead Command_Status event 
		 * should have been generated and then appropriate event 
		 * should have been sent to indicate the final result.
		 */

		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);
} /* process_link_control_params */

/* 
 * Process link policy command return parameters
 */

static int
process_link_policy_params(ng_hci_unit_p unit, u_int16_t ocf,
		struct mbuf *mcp, struct mbuf *mrp)
{
	int	error = 0;

	switch (ocf){
	case NG_HCI_OCF_ROLE_DISCOVERY: {
		ng_hci_role_discovery_rp	*rp = NULL;
		ng_hci_unit_con_t		*con = NULL;
		u_int16_t			 h;

		NG_HCI_M_PULLUP(mrp, sizeof(*rp));
		if (mrp != NULL) {
			rp = mtod(mrp, ng_hci_role_discovery_rp *);

			h = NG_HCI_CON_HANDLE(le16toh(rp->con_handle));
			con = ng_hci_con_by_handle(unit, h);
			if (con == NULL) {
				NG_HCI_ALERT(
"%s: %s - invalid connection handle=%d\n",
					__func__, NG_NODE_NAME(unit->node), h); 
				error = ENOENT;
			} else if (con->link_type != NG_HCI_LINK_ACL) {
				NG_HCI_ALERT(
"%s: %s - invalid link type=%d\n", __func__, NG_NODE_NAME(unit->node),
					con->link_type);
				error = EINVAL;
			} else
				con->role = rp->role;
		} else
			error = ENOBUFS;
		} break;

	case NG_HCI_OCF_READ_LINK_POLICY_SETTINGS:
	case NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS:
		/* These do not need post processing */
		break;

	case NG_HCI_OCF_HOLD_MODE:
	case NG_HCI_OCF_SNIFF_MODE:
	case NG_HCI_OCF_EXIT_SNIFF_MODE:
	case NG_HCI_OCF_PARK_MODE:
	case NG_HCI_OCF_EXIT_PARK_MODE:
	case NG_HCI_OCF_QOS_SETUP:
	case NG_HCI_OCF_SWITCH_ROLE:
	default:

		/*
		 * None of these command was supposed to generate 
		 * Command_Complete event. Instead Command_Status event 
		 * should have been generated and then appropriate event
		 * should have been sent to indicate the final result.
		 */

		error = EINVAL;
		break;
	} 

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);
} /* process_link_policy_params */

/* 
 * Process HC and baseband command return parameters
 */

int
process_hc_baseband_params(ng_hci_unit_p unit, u_int16_t ocf, 
		struct mbuf *mcp, struct mbuf *mrp)
{
	int	error = 0;

	switch (ocf) {
	case NG_HCI_OCF_SET_EVENT_MASK:
	case NG_HCI_OCF_SET_EVENT_FILTER:
	case NG_HCI_OCF_FLUSH:	/* XXX Do we need to handle that? */
	case NG_HCI_OCF_READ_PIN_TYPE:
	case NG_HCI_OCF_WRITE_PIN_TYPE:
	case NG_HCI_OCF_CREATE_NEW_UNIT_KEY:
	case NG_HCI_OCF_WRITE_STORED_LINK_KEY:
	case NG_HCI_OCF_WRITE_CON_ACCEPT_TIMO:
	case NG_HCI_OCF_WRITE_PAGE_TIMO:
	case NG_HCI_OCF_READ_SCAN_ENABLE:
	case NG_HCI_OCF_WRITE_SCAN_ENABLE:
	case NG_HCI_OCF_WRITE_PAGE_SCAN_ACTIVITY:
	case NG_HCI_OCF_WRITE_INQUIRY_SCAN_ACTIVITY:
	case NG_HCI_OCF_READ_AUTH_ENABLE:
	case NG_HCI_OCF_WRITE_AUTH_ENABLE:
	case NG_HCI_OCF_READ_ENCRYPTION_MODE:
	case NG_HCI_OCF_WRITE_ENCRYPTION_MODE:
	case NG_HCI_OCF_WRITE_VOICE_SETTINGS:
	case NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS:
	case NG_HCI_OCF_WRITE_NUM_BROADCAST_RETRANS:
	case NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY:
	case NG_HCI_OCF_WRITE_HOLD_MODE_ACTIVITY:
	case NG_HCI_OCF_READ_SCO_FLOW_CONTROL:
	case NG_HCI_OCF_WRITE_SCO_FLOW_CONTROL:
	case NG_HCI_OCF_H2HC_FLOW_CONTROL: /* XXX Not supported this time */
	case NG_HCI_OCF_HOST_BUFFER_SIZE:
	case NG_HCI_OCF_READ_IAC_LAP:
	case NG_HCI_OCF_WRITE_IAC_LAP:
	case NG_HCI_OCF_READ_PAGE_SCAN_PERIOD:
	case NG_HCI_OCF_WRITE_PAGE_SCAN_PERIOD:
	case NG_HCI_OCF_READ_PAGE_SCAN:
	case NG_HCI_OCF_WRITE_PAGE_SCAN:
	case NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO:
	case NG_HCI_OCF_WRITE_LINK_SUPERVISION_TIMO:
	case NG_HCI_OCF_READ_SUPPORTED_IAC_NUM:
	case NG_HCI_OCF_READ_STORED_LINK_KEY:
	case NG_HCI_OCF_DELETE_STORED_LINK_KEY:
	case NG_HCI_OCF_READ_CON_ACCEPT_TIMO:
	case NG_HCI_OCF_READ_PAGE_TIMO:
	case NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY:
	case NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY:
	case NG_HCI_OCF_READ_VOICE_SETTINGS:
	case NG_HCI_OCF_READ_AUTO_FLUSH_TIMO:
	case NG_HCI_OCF_WRITE_AUTO_FLUSH_TIMO:
	case NG_HCI_OCF_READ_XMIT_LEVEL:
	case NG_HCI_OCF_HOST_NUM_COMPL_PKTS:	/* XXX Can get here? */
	case NG_HCI_OCF_CHANGE_LOCAL_NAME:
	case NG_HCI_OCF_READ_LOCAL_NAME:
	case NG_HCI_OCF_READ_UNIT_CLASS:
	case NG_HCI_OCF_WRITE_UNIT_CLASS:
	case NG_HCI_OCF_READ_LE_HOST_SUPPORTED:
	case NG_HCI_OCF_WRITE_LE_HOST_SUPPORTED:
		/* These do not need post processing */
		break;

	case NG_HCI_OCF_RESET: {
		ng_hci_unit_con_p	con = NULL;
		int			size;

		/*
		 * XXX 
		 *
		 * After RESET command unit goes into standby mode
		 * and all operational state is lost. Host controller
		 * will revert to default values for all parameters.
		 * 
		 * For now we shall terminate all connections and drop
		 * inited bit. After RESET unit must be re-initialized.
		 */

		while (!LIST_EMPTY(&unit->con_list)) {
			con = LIST_FIRST(&unit->con_list);

			/* Remove all timeouts (if any) */
			if (con->flags & NG_HCI_CON_TIMEOUT_PENDING)
				ng_hci_con_untimeout(con);

			/* Connection terminated by local host */
			ng_hci_lp_discon_ind(con, 0x16);
			ng_hci_free_con(con);
		}

		NG_HCI_BUFF_ACL_TOTAL(unit->buffer, size);
		NG_HCI_BUFF_ACL_FREE(unit->buffer, size);

		NG_HCI_BUFF_SCO_TOTAL(unit->buffer, size);
		NG_HCI_BUFF_SCO_FREE(unit->buffer, size);

		unit->state &= ~NG_HCI_UNIT_INITED;
		} break;

	default:
		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);
} /* process_hc_baseband_params */

/* 
 * Process info command return parameters
 */

static int
process_info_params(ng_hci_unit_p unit, u_int16_t ocf, struct mbuf *mcp,
		struct mbuf *mrp)
{
	int	error = 0, len;

	switch (ocf) {
	case NG_HCI_OCF_READ_LOCAL_VER:
	case NG_HCI_OCF_READ_COUNTRY_CODE:
		break;

	case NG_HCI_OCF_READ_LOCAL_FEATURES:
		m_adj(mrp, sizeof(u_int8_t));
		len = min(mrp->m_pkthdr.len, sizeof(unit->features));
		m_copydata(mrp, 0, len, (caddr_t) unit->features);
		break;

	case NG_HCI_OCF_READ_BUFFER_SIZE: {
		ng_hci_read_buffer_size_rp	*rp = NULL;

		/* Do not update buffer descriptor if node was initialized */
		if ((unit->state & NG_HCI_UNIT_READY) == NG_HCI_UNIT_READY)
			break;

		NG_HCI_M_PULLUP(mrp, sizeof(*rp));
		if (mrp != NULL) {
			rp = mtod(mrp, ng_hci_read_buffer_size_rp *);

			NG_HCI_BUFF_ACL_SET(
				unit->buffer,
				le16toh(rp->num_acl_pkt),  /* number */
				le16toh(rp->max_acl_size), /* size */
				le16toh(rp->num_acl_pkt)   /* free */
			);

			NG_HCI_BUFF_SCO_SET(
				unit->buffer,
				le16toh(rp->num_sco_pkt), /* number */
				rp->max_sco_size,         /* size */
				le16toh(rp->num_sco_pkt)  /* free */
			);

			/* Let upper layers know */
			ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
			ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
		} else
			error = ENOBUFS;
		} break;

	case NG_HCI_OCF_READ_BDADDR:
		/* Do not update BD_ADDR if node was initialized */
		if ((unit->state & NG_HCI_UNIT_READY) == NG_HCI_UNIT_READY)
			break;

		m_adj(mrp, sizeof(u_int8_t));
		len = min(mrp->m_pkthdr.len, sizeof(unit->bdaddr));
		m_copydata(mrp, 0, len, (caddr_t) &unit->bdaddr);

		/* Let upper layers know */
		ng_hci_node_is_up(unit->node, unit->acl, NULL, 0);
		ng_hci_node_is_up(unit->node, unit->sco, NULL, 0);
		break;

	default:
		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);
} /* process_info_params */

/* 
 * Process status command return parameters
 */

static int
process_status_params(ng_hci_unit_p unit, u_int16_t ocf, struct mbuf *mcp,
		struct mbuf *mrp)
{
	int	error = 0;

	switch (ocf) {
	case NG_HCI_OCF_READ_FAILED_CONTACT_CNTR:
	case NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR:
	case NG_HCI_OCF_GET_LINK_QUALITY:
	case NG_HCI_OCF_READ_RSSI:
		/* These do not need post processing */
		break;

	default:
		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);
} /* process_status_params */

/* 
 * Process testing command return parameters
 */

int
process_testing_params(ng_hci_unit_p unit, u_int16_t ocf, struct mbuf *mcp,
		struct mbuf *mrp)
{
	int	error = 0;

	switch (ocf) {
	/*
	 * XXX FIXME
	 * We do not support these features at this time. However,
	 * HCI node could support this and do something smart. At least
	 * node can change unit state.
	 */

	case NG_HCI_OCF_READ_LOOPBACK_MODE:
	case NG_HCI_OCF_WRITE_LOOPBACK_MODE:
	case NG_HCI_OCF_ENABLE_UNIT_UNDER_TEST:
		break;

	default:
		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);
} /* process_testing_params */

/* 
 * Process LE command return parameters
 */

static int
process_le_params(ng_hci_unit_p unit, u_int16_t ocf,
		struct mbuf *mcp, struct mbuf *mrp)
{
	int	error = 0;

	switch (ocf){
	case NG_HCI_OCF_LE_SET_EVENT_MASK:
	case NG_HCI_OCF_LE_READ_BUFFER_SIZE:
	case NG_HCI_OCF_LE_READ_LOCAL_SUPPORTED_FEATURES:
	case NG_HCI_OCF_LE_SET_RANDOM_ADDRESS:
	case NG_HCI_OCF_LE_SET_ADVERTISING_PARAMETERS:
	case NG_HCI_OCF_LE_READ_ADVERTISING_CHANNEL_TX_POWER:
	case NG_HCI_OCF_LE_SET_ADVERTISING_DATA:
	case NG_HCI_OCF_LE_SET_SCAN_RESPONSE_DATA:
	case NG_HCI_OCF_LE_SET_ADVERTISE_ENABLE:
	case NG_HCI_OCF_LE_SET_SCAN_PARAMETERS:
	case NG_HCI_OCF_LE_SET_SCAN_ENABLE:
	case NG_HCI_OCF_LE_CREATE_CONNECTION_CANCEL:
	case NG_HCI_OCF_LE_CLEAR_WHITE_LIST:
	case NG_HCI_OCF_LE_READ_WHITE_LIST_SIZE:
	case NG_HCI_OCF_LE_ADD_DEVICE_TO_WHITE_LIST:
	case NG_HCI_OCF_LE_REMOVE_DEVICE_FROM_WHITE_LIST:
	case NG_HCI_OCF_LE_SET_HOST_CHANNEL_CLASSIFICATION:
	case NG_HCI_OCF_LE_READ_CHANNEL_MAP:
	case NG_HCI_OCF_LE_ENCRYPT:
	case NG_HCI_OCF_LE_RAND:
	case NG_HCI_OCF_LE_LONG_TERM_KEY_REQUEST_REPLY:
	case NG_HCI_OCF_LE_LONG_TERM_KEY_REQUEST_NEGATIVE_REPLY:
	case NG_HCI_OCF_LE_READ_SUPPORTED_STATES:
	case NG_HCI_OCF_LE_RECEIVER_TEST:
	case NG_HCI_OCF_LE_TRANSMITTER_TEST:
	case NG_HCI_OCF_LE_TEST_END:

		/* These do not need post processing */
		break;
	case NG_HCI_OCF_LE_CREATE_CONNECTION:
	case NG_HCI_OCF_LE_CONNECTION_UPDATE:
	case NG_HCI_OCF_LE_READ_REMOTE_USED_FEATURES:
	case NG_HCI_OCF_LE_START_ENCRYPTION:

	default:
		/*
		 * None of these command was supposed to generate 
		 * Command_Complete event. Instead Command_Status event 
		 * should have been generated and then appropriate event
		 * should have been sent to indicate the final result.
		 */

		error = EINVAL;
		break;
	} 

	NG_FREE_M(mcp);
	NG_FREE_M(mrp);

	return (error);

}

static int
process_le_status(ng_hci_unit_p unit,ng_hci_command_status_ep *ep,
		struct mbuf *mcp)
{
	int	error = 0;

	switch (NG_HCI_OCF(ep->opcode)){
	case NG_HCI_OCF_LE_CREATE_CONNECTION:
	case NG_HCI_OCF_LE_CONNECTION_UPDATE:
	case NG_HCI_OCF_LE_READ_REMOTE_USED_FEATURES:
	case NG_HCI_OCF_LE_START_ENCRYPTION:

		/* These do not need post processing */
		break;

	case NG_HCI_OCF_LE_SET_EVENT_MASK:
	case NG_HCI_OCF_LE_READ_BUFFER_SIZE:
	case NG_HCI_OCF_LE_READ_LOCAL_SUPPORTED_FEATURES:
	case NG_HCI_OCF_LE_SET_RANDOM_ADDRESS:
	case NG_HCI_OCF_LE_SET_ADVERTISING_PARAMETERS:
	case NG_HCI_OCF_LE_READ_ADVERTISING_CHANNEL_TX_POWER:
	case NG_HCI_OCF_LE_SET_ADVERTISING_DATA:
	case NG_HCI_OCF_LE_SET_SCAN_RESPONSE_DATA:
	case NG_HCI_OCF_LE_SET_ADVERTISE_ENABLE:
	case NG_HCI_OCF_LE_SET_SCAN_PARAMETERS:
	case NG_HCI_OCF_LE_SET_SCAN_ENABLE:
	case NG_HCI_OCF_LE_CREATE_CONNECTION_CANCEL:
	case NG_HCI_OCF_LE_CLEAR_WHITE_LIST:
	case NG_HCI_OCF_LE_READ_WHITE_LIST_SIZE:
	case NG_HCI_OCF_LE_ADD_DEVICE_TO_WHITE_LIST:
	case NG_HCI_OCF_LE_REMOVE_DEVICE_FROM_WHITE_LIST:
	case NG_HCI_OCF_LE_SET_HOST_CHANNEL_CLASSIFICATION:
	case NG_HCI_OCF_LE_READ_CHANNEL_MAP:
	case NG_HCI_OCF_LE_ENCRYPT:
	case NG_HCI_OCF_LE_RAND:
	case NG_HCI_OCF_LE_LONG_TERM_KEY_REQUEST_REPLY:
	case NG_HCI_OCF_LE_LONG_TERM_KEY_REQUEST_NEGATIVE_REPLY:
	case NG_HCI_OCF_LE_READ_SUPPORTED_STATES:
	case NG_HCI_OCF_LE_RECEIVER_TEST:
	case NG_HCI_OCF_LE_TRANSMITTER_TEST:
	case NG_HCI_OCF_LE_TEST_END:

	default:
		/*
		 * None of these command was supposed to generate 
		 * Command_Stutus event. Command Complete instead.
		 */

		error = EINVAL;
		break;
	} 

	NG_FREE_M(mcp);

	return (error);

}

/*
 * Process link control command status
 */

static int
process_link_control_status(ng_hci_unit_p unit, ng_hci_command_status_ep *ep,
		struct mbuf *mcp)
{
	int	error = 0;

	switch (NG_HCI_OCF(ep->opcode)) {
	case NG_HCI_OCF_INQUIRY:
	case NG_HCI_OCF_DISCON:		/* XXX */
	case NG_HCI_OCF_REJECT_CON:	/* XXX */
	case NG_HCI_OCF_CHANGE_CON_PKT_TYPE:
	case NG_HCI_OCF_AUTH_REQ:
	case NG_HCI_OCF_SET_CON_ENCRYPTION:
	case NG_HCI_OCF_CHANGE_CON_LINK_KEY:
	case NG_HCI_OCF_MASTER_LINK_KEY:
	case NG_HCI_OCF_REMOTE_NAME_REQ:
	case NG_HCI_OCF_READ_REMOTE_FEATURES:
	case NG_HCI_OCF_READ_REMOTE_VER_INFO:
	case NG_HCI_OCF_READ_CLOCK_OFFSET:
		/* These do not need post processing */
		break;

	case NG_HCI_OCF_CREATE_CON:
		break;

	case NG_HCI_OCF_ADD_SCO_CON:
		break;

	case NG_HCI_OCF_ACCEPT_CON:
		break;

	case NG_HCI_OCF_INQUIRY_CANCEL:
	case NG_HCI_OCF_PERIODIC_INQUIRY:
	case NG_HCI_OCF_EXIT_PERIODIC_INQUIRY:
	case NG_HCI_OCF_LINK_KEY_REP:
	case NG_HCI_OCF_LINK_KEY_NEG_REP: 
	case NG_HCI_OCF_PIN_CODE_REP:
	case NG_HCI_OCF_PIN_CODE_NEG_REP:
	default:

		/*
		 * None of these command was supposed to generate 
		 * Command_Status event. Instead Command_Complete event 
		 * should have been sent.
		 */

		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);

	return (error);
} /* process_link_control_status */

/*
 * Process link policy command status
 */

static int
process_link_policy_status(ng_hci_unit_p unit, ng_hci_command_status_ep *ep,
		struct mbuf *mcp)
{
	int	error = 0;

	switch (NG_HCI_OCF(ep->opcode)) {
	case NG_HCI_OCF_HOLD_MODE:
	case NG_HCI_OCF_SNIFF_MODE:
	case NG_HCI_OCF_EXIT_SNIFF_MODE:
	case NG_HCI_OCF_PARK_MODE:
	case NG_HCI_OCF_EXIT_PARK_MODE:
	case NG_HCI_OCF_SWITCH_ROLE:
		/* These do not need post processing */
		break;

	case NG_HCI_OCF_QOS_SETUP:
		break;

	case NG_HCI_OCF_ROLE_DISCOVERY:
	case NG_HCI_OCF_READ_LINK_POLICY_SETTINGS:
	case NG_HCI_OCF_WRITE_LINK_POLICY_SETTINGS:
	default:

		/*
		 * None of these command was supposed to generate 
		 * Command_Status event. Instead Command_Complete event 
		 * should have been sent.
		 */

		error = EINVAL;
		break;
	}

	NG_FREE_M(mcp);

	return (error);
} /* process_link_policy_status */