summaryrefslogtreecommitdiff
path: root/sys/dev/usb/ubt.c
blob: d9d1058ba54ce7d7f56382289ad5318aedfbc34b (plain)
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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
/* $OpenBSD: ubt.c,v 1.10 2007/11/09 18:05:51 miod Exp $ */

/*-
 * Copyright (c) 2006 Itronix Inc.
 * All rights reserved.
 *
 * Written by Iain Hibbert for Itronix Inc.
 *
 * 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.
 * 3. The name of Itronix Inc. may not be used to endorse
 *    or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``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 ITRONIX INC. 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.
 */
/*
 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) and
 * David Sainty (David.Sainty@dtsp.co.nz).
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */
/*
 * This driver originally written by Lennart Augustsson and David Sainty,
 * but was mostly rewritten for the NetBSD Bluetooth protocol stack by
 * Iain Hibbert for Itronix, Inc using the FreeBSD ng_ubt.c driver as a
 * reference.
 */

#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/systm.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>

#include <netbt/bluetooth.h>
#include <netbt/hci.h>

/*******************************************************************************
 *
 *	debugging stuff
 */
#undef DPRINTF
#undef DPRINTFN

#ifdef UBT_DEBUG
int	ubt_debug = UBT_DEBUG;

#define DPRINTF(fmt, args...)		do {		\
	if (ubt_debug)					\
		printf("%s: "fmt, __func__ , ##args);	\
} while (/* CONSTCOND */0)

#define DPRINTFN(n, fmt, args...)	do {		\
	if (ubt_debug > (n))				\
		printf("%s: "fmt, __func__ , ##args);	\
} while (/* CONSTCOND */0)

#else
#define DPRINTF(...)
#define DPRINTFN(...)
#endif

/*******************************************************************************
 *
 *	ubt softc structure
 *
 */

/* buffer sizes */
/*
 * NB: although ACL packets can extend to 65535 bytes, most devices
 * have max_acl_size at much less (largest I have seen is 384)
 */
#define UBT_BUFSIZ_CMD		(HCI_CMD_PKT_SIZE - 1)
#define UBT_BUFSIZ_ACL		(2048 - 1)
#define UBT_BUFSIZ_EVENT	(HCI_EVENT_PKT_SIZE - 1)

/* Transmit timeouts */
#define UBT_CMD_TIMEOUT		USBD_DEFAULT_TIMEOUT
#define UBT_ACL_TIMEOUT		USBD_DEFAULT_TIMEOUT

/*
 * ISOC transfers
 *
 * xfer buffer size depends on the frame size, and the number
 * of frames per transfer is fixed, as each frame should be
 * 1ms worth of data. This keeps the rate that xfers complete
 * fairly constant. We use multiple xfers to keep the hardware
 * busy
 */
#define UBT_NXFERS		3	/* max xfers to queue */
#define UBT_NFRAMES		10	/* frames per xfer */

struct ubt_isoc_xfer {
	struct ubt_softc	*softc;
	usbd_xfer_handle	 xfer;
	uint8_t			*buf;
	uint16_t		 size[UBT_NFRAMES];
	int			 busy;
};

struct ubt_softc {
	struct device		 sc_dev;
	usbd_device_handle	 sc_udev;
	int			 sc_refcnt;
	int			 sc_dying;

	/* Control Interface */
	usbd_interface_handle	 sc_iface0;

	/* Commands (control) */
	usbd_xfer_handle	 sc_cmd_xfer;
	uint8_t			*sc_cmd_buf;

	/* Events (interrupt) */
	int			 sc_evt_addr;	/* endpoint address */
	usbd_pipe_handle	 sc_evt_pipe;
	uint8_t			*sc_evt_buf;

	/* ACL data (in) */
	int			 sc_aclrd_addr;	/* endpoint address */
	usbd_pipe_handle	 sc_aclrd_pipe;	/* read pipe */
	usbd_xfer_handle	 sc_aclrd_xfer;	/* read xfer */
	uint8_t			*sc_aclrd_buf;	/* read buffer */
	int			 sc_aclrd_busy;	/* reading */

	/* ACL data (out) */
	int			 sc_aclwr_addr;	/* endpoint address */
	usbd_pipe_handle	 sc_aclwr_pipe;	/* write pipe */
	usbd_xfer_handle	 sc_aclwr_xfer;	/* write xfer */
	uint8_t			*sc_aclwr_buf;	/* write buffer */

	/* ISOC interface */
	usbd_interface_handle	 sc_iface1;	/* ISOC interface */
	struct sysctllog	*sc_log;	/* sysctl log */
	int			 sc_config;	/* current config no */
	int			 sc_alt_config;	/* no of alternates */

	/* SCO data (in) */
	int			 sc_scord_addr;	/* endpoint address */
	usbd_pipe_handle	 sc_scord_pipe;	/* read pipe */
	int			 sc_scord_size;	/* frame length */
	struct ubt_isoc_xfer	 sc_scord[UBT_NXFERS];
	struct mbuf		*sc_scord_mbuf;	/* current packet */

	/* SCO data (out) */
	int			 sc_scowr_addr;	/* endpoint address */
	usbd_pipe_handle	 sc_scowr_pipe;	/* write pipe */
	int			 sc_scowr_size;	/* frame length */
	struct ubt_isoc_xfer	 sc_scowr[UBT_NXFERS];
	struct mbuf		*sc_scowr_mbuf;	/* current packet */

	/* Protocol structure */
	struct hci_unit		 sc_unit;

	/* Successfully attached */
	int			 sc_ok;
};

/*
 * Bluetooth unit/USB callback routines
 */
int ubt_enable(struct hci_unit *);
void ubt_disable(struct hci_unit *);

void ubt_xmit_cmd_start(struct hci_unit *);
void ubt_xmit_cmd_complete(usbd_xfer_handle,
				usbd_private_handle, usbd_status);

void ubt_xmit_acl_start(struct hci_unit *);
void ubt_xmit_acl_complete(usbd_xfer_handle,
				usbd_private_handle, usbd_status);

void ubt_xmit_sco_start(struct hci_unit *);
void ubt_xmit_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
void ubt_xmit_sco_complete(usbd_xfer_handle,
				usbd_private_handle, usbd_status);

void ubt_recv_event(usbd_xfer_handle,
				usbd_private_handle, usbd_status);

void ubt_recv_acl_start(struct ubt_softc *);
void ubt_recv_acl_complete(usbd_xfer_handle,
				usbd_private_handle, usbd_status);

void ubt_recv_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
void ubt_recv_sco_complete(usbd_xfer_handle,
				usbd_private_handle, usbd_status);

int ubt_match(struct device *, void *, void *); 
void ubt_attach(struct device *, struct device *, void *); 
int ubt_detach(struct device *, int); 
int ubt_activate(struct device *, enum devact); 

struct cfdriver ubt_cd = { 
	NULL, "ubt", DV_DULL 
}; 

const struct cfattach ubt_ca = { 
	sizeof(struct ubt_softc), 
	ubt_match, 
	ubt_attach, 
	ubt_detach, 
	ubt_activate, 
};

static int ubt_set_isoc_config(struct ubt_softc *);
static void ubt_abortdealloc(struct ubt_softc *);

/*
 * Match against the whole device, since we want to take
 * both interfaces. If a device should be ignored then add
 *
 *	{ VendorID, ProductID }
 *
 * to the ubt_ignore list.
 */
static const struct usb_devno ubt_ignore[] = {
	{ USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033 },
	{ USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033NF },
	{ 0, 0 }	/* end of list */
};

int
ubt_match(struct device *parent, void *match, void *aux)
{

	struct usb_attach_arg *uaa = aux;
	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);

	DPRINTFN(50, "ubt_match\n");

	if (usb_lookup(ubt_ignore, uaa->vendor, uaa->product))
		return UMATCH_NONE;

	if (dd->bDeviceClass == UDCLASS_WIRELESS
	    && dd->bDeviceSubClass == UDSUBCLASS_RF
	    && dd->bDeviceProtocol == UDPROTO_BLUETOOTH)
		return UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO;

	return UMATCH_NONE;
}


void
ubt_attach(struct device *parent, struct device *self, void *aux)
{
	struct ubt_softc *sc = (struct ubt_softc *)self;
	struct usb_attach_arg *uaa = aux;
	usb_config_descriptor_t *cd;
	usb_endpoint_descriptor_t *ed;
	int err;
	uint8_t count, i;

	DPRINTFN(50, "ubt_attach: sc=%p\n", sc);

	sc->sc_udev = uaa->device;

	/*
	 * Move the device into the configured state
	 */
	err = usbd_set_config_index(sc->sc_udev, 0, 1);
	if (err) {
		printf("%s: failed to set configuration idx 0: %s\n",
		    sc->sc_dev.dv_xname, usbd_errstr(err));

		return;
	}

	/*
	 * Interface 0 must have 3 endpoints
	 *	1) Interrupt endpoint to receive HCI events
	 *	2) Bulk IN endpoint to receive ACL data
	 *	3) Bulk OUT endpoint to send ACL data
	 */
	err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
	if (err) {
		printf("%s: Could not get interface 0 handle %s (%d)\n",
				sc->sc_dev.dv_xname, usbd_errstr(err), err);

		return;
	}

	sc->sc_evt_addr = -1;
	sc->sc_aclrd_addr = -1;
	sc->sc_aclwr_addr = -1;

	count = 0;
	(void)usbd_endpoint_count(sc->sc_iface0, &count);

	for (i = 0 ; i < count ; i++) {
		int dir, type;

		ed = usbd_interface2endpoint_descriptor(sc->sc_iface0, i);
		if (ed == NULL) {
			printf("%s: could not read endpoint descriptor %d\n",
			    sc->sc_dev.dv_xname, i);

			return;
		}

		dir = UE_GET_DIR(ed->bEndpointAddress);
		type = UE_GET_XFERTYPE(ed->bmAttributes);

		if (dir == UE_DIR_IN && type == UE_INTERRUPT)
			sc->sc_evt_addr = ed->bEndpointAddress;
		else if (dir == UE_DIR_IN && type == UE_BULK)
			sc->sc_aclrd_addr = ed->bEndpointAddress;
		else if (dir == UE_DIR_OUT && type == UE_BULK)
			sc->sc_aclwr_addr = ed->bEndpointAddress;
	}

	if (sc->sc_evt_addr == -1) {
		printf("%s: missing INTERRUPT endpoint on interface 0\n",
				sc->sc_dev.dv_xname);

		return;
	}
	if (sc->sc_aclrd_addr == -1) {
		printf("%s: missing BULK IN endpoint on interface 0\n",
				sc->sc_dev.dv_xname);

		return;
	}
	if (sc->sc_aclwr_addr == -1) {
		printf("%s: missing BULK OUT endpoint on interface 0\n",
				sc->sc_dev.dv_xname);

		return;
	}

	/*
	 * Interface 1 must have 2 endpoints
	 *	1) Isochronous IN endpoint to receive SCO data
	 *	2) Isochronous OUT endpoint to send SCO data
	 *
	 * and will have several configurations, which can be selected
	 * via a sysctl variable. We select config 0 to start, which
	 * means that no SCO data will be available.
	 */
	err = usbd_device2interface_handle(sc->sc_udev, 1, &sc->sc_iface1);
	if (err) {
		printf("%s: Could not get interface 1 handle %s (%d)\n",
				sc->sc_dev.dv_xname, usbd_errstr(err), err);

		return;
	}

	cd = usbd_get_config_descriptor(sc->sc_udev);
	if (cd == NULL) {
		printf("%s: could not get config descriptor\n",
			sc->sc_dev.dv_xname);

		return;
	}

	sc->sc_alt_config = usbd_get_no_alts(cd, 1);

	/* set initial config */
	err = ubt_set_isoc_config(sc);
	if (err) {
		printf("%s: ISOC config failed\n",
			sc->sc_dev.dv_xname);

		return;
	}

	/* Attach HCI */
	sc->sc_unit.hci_softc = self;
	sc->sc_unit.hci_devname = sc->sc_dev.dv_xname;
	sc->sc_unit.hci_enable = ubt_enable;
	sc->sc_unit.hci_disable = ubt_disable;
	sc->sc_unit.hci_start_cmd = ubt_xmit_cmd_start;
	sc->sc_unit.hci_start_acl = ubt_xmit_acl_start;
	sc->sc_unit.hci_start_sco = ubt_xmit_sco_start;
	sc->sc_unit.hci_ipl = IPL_USB; /* XXX: IPL_SOFTUSB ?? */
	hci_attach(&sc->sc_unit);

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
			   &sc->sc_dev);

	sc->sc_ok = 1;

	return;
}

int
ubt_detach(struct device *self, int flags)
{
	struct ubt_softc *sc = (struct ubt_softc *)self;
	int s;

	DPRINTF("sc=%p flags=%d\n", sc, flags);

	sc->sc_dying = 1;

	if (!sc->sc_ok)
		return 0;

	/* Detach HCI interface */
	hci_detach(&sc->sc_unit);

	/*
	 * Abort all pipes. Causes processes waiting for transfer to wake.
	 *
	 * Actually, hci_detach() above will call ubt_disable() which may
	 * call ubt_abortdealloc(), but lets be sure since doing it twice
	 * wont cause an error.
	 */
	ubt_abortdealloc(sc);

	/* wait for all processes to finish */
	s = splusb();
	if (sc->sc_refcnt-- > 0)
		usb_detach_wait(&sc->sc_dev);

	splx(s);

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
			   &sc->sc_dev);

	DPRINTFN(1, "driver detached\n");

	return 0;
}

int
ubt_activate(struct device *self, enum devact act)
{
	struct ubt_softc *sc = (struct ubt_softc *)self;
	int error = 0;

	DPRINTFN(1, "ubt_activate: sc=%p, act=%d\n", sc, act);

	switch (act) {
	case DVACT_ACTIVATE:
		return EOPNOTSUPP;
		break;

	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		break;
	}
	return error;
}

/* set ISOC configuration */
int
ubt_set_isoc_config(struct ubt_softc *sc)
{
	usb_endpoint_descriptor_t *ed;
	int rd_addr, wr_addr, rd_size, wr_size;
	uint8_t count, i;
	int err;

	err = usbd_set_interface(sc->sc_iface1, sc->sc_config);
	if (err != USBD_NORMAL_COMPLETION) {
		printf(
		    "%s: Could not set config %d on ISOC interface. %s (%d)\n",
		    sc->sc_dev.dv_xname, sc->sc_config, usbd_errstr(err), err);

		return err == USBD_IN_USE ? EBUSY : EIO;
	}

	/*
	 * We wont get past the above if there are any pipes open, so no
	 * need to worry about buf/xfer/pipe deallocation. If we get an
	 * error after this, the frame quantities will be 0 and no SCO
	 * data will be possible.
	 */

	sc->sc_scord_size = rd_size = 0;
	sc->sc_scord_addr = rd_addr = -1;

	sc->sc_scowr_size = wr_size = 0;
	sc->sc_scowr_addr = wr_addr = -1;

	count = 0;
	(void)usbd_endpoint_count(sc->sc_iface1, &count);

	for (i = 0 ; i < count ; i++) {
		ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i);
		if (ed == NULL) {
			printf("%s: could not read endpoint descriptor %d\n",
			    sc->sc_dev.dv_xname, i);

			return EIO;
		}

		DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n",
			sc->sc_dev.dv_xname,
			UE_GET_XFERTYPE(ed->bmAttributes),
			UE_GET_ISO_TYPE(ed->bmAttributes),
			ed->bEndpointAddress,
			UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out");

		if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
			continue;

		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
			rd_addr = ed->bEndpointAddress;
			rd_size = UGETW(ed->wMaxPacketSize);
		} else {
			wr_addr = ed->bEndpointAddress;
			wr_size = UGETW(ed->wMaxPacketSize);
		}
	}

	if (rd_addr == -1) {
		printf(
		    "%s: missing ISOC IN endpoint on interface config %d\n",
		    sc->sc_dev.dv_xname, sc->sc_config);

		return ENOENT;
	}
	if (wr_addr == -1) {
		printf(
		    "%s: missing ISOC OUT endpoint on interface config %d\n",
		    sc->sc_dev.dv_xname, sc->sc_config);

		return ENOENT;
	}

#ifdef DIAGNOSTIC
	if (rd_size > MLEN) {
		printf("%s: rd_size=%d exceeds MLEN\n",
		    sc->sc_dev.dv_xname, rd_size);

		return EOVERFLOW;
	}

	if (wr_size > MLEN) {
		printf("%s: wr_size=%d exceeds MLEN\n",
		    sc->sc_dev.dv_xname, wr_size);

		return EOVERFLOW;
	}
#endif

	sc->sc_scord_size = rd_size;
	sc->sc_scord_addr = rd_addr;

	sc->sc_scowr_size = wr_size;
	sc->sc_scowr_addr = wr_addr;

	return 0;
}

void
ubt_abortdealloc(struct ubt_softc *sc)
{
	int i;

	DPRINTFN(1, "sc=%p\n", sc);

	/* Abort all pipes */
	if (sc->sc_evt_pipe != NULL) {
		usbd_abort_pipe(sc->sc_evt_pipe);
		usbd_close_pipe(sc->sc_evt_pipe);
		sc->sc_evt_pipe = NULL;
	}

	if (sc->sc_aclrd_pipe != NULL) {
		usbd_abort_pipe(sc->sc_aclrd_pipe);
		usbd_close_pipe(sc->sc_aclrd_pipe);
		sc->sc_aclrd_pipe = NULL;
	}

	if (sc->sc_aclwr_pipe != NULL) {
		usbd_abort_pipe(sc->sc_aclwr_pipe);
		usbd_close_pipe(sc->sc_aclwr_pipe);
		sc->sc_aclwr_pipe = NULL;
	}

	if (sc->sc_scord_pipe != NULL) {
		usbd_abort_pipe(sc->sc_scord_pipe);
		usbd_close_pipe(sc->sc_scord_pipe);
		sc->sc_scord_pipe = NULL;
	}

	if (sc->sc_scowr_pipe != NULL) {
		usbd_abort_pipe(sc->sc_scowr_pipe);
		usbd_close_pipe(sc->sc_scowr_pipe);
		sc->sc_scowr_pipe = NULL;
	}

	/* Free event buffer */
	if (sc->sc_evt_buf != NULL) {
		free(sc->sc_evt_buf, M_USBDEV);
		sc->sc_evt_buf = NULL;
	}

	/* Free all xfers and xfer buffers (implicit) */
	if (sc->sc_cmd_xfer != NULL) {
		usbd_free_xfer(sc->sc_cmd_xfer);
		sc->sc_cmd_xfer = NULL;
		sc->sc_cmd_buf = NULL;
	}

	if (sc->sc_aclrd_xfer != NULL) {
		usbd_free_xfer(sc->sc_aclrd_xfer);
		sc->sc_aclrd_xfer = NULL;
		sc->sc_aclrd_buf = NULL;
	}

	if (sc->sc_aclwr_xfer != NULL) {
		usbd_free_xfer(sc->sc_aclwr_xfer);
		sc->sc_aclwr_xfer = NULL;
		sc->sc_aclwr_buf = NULL;
	}

	for (i = 0 ; i < UBT_NXFERS ; i++) {
		if (sc->sc_scord[i].xfer != NULL) {
			usbd_free_xfer(sc->sc_scord[i].xfer);
			sc->sc_scord[i].xfer = NULL;
			sc->sc_scord[i].buf = NULL;
		}

		if (sc->sc_scowr[i].xfer != NULL) {
			usbd_free_xfer(sc->sc_scowr[i].xfer);
			sc->sc_scowr[i].xfer = NULL;
			sc->sc_scowr[i].buf = NULL;
		}
	}

	/* Free partial SCO packets */
	if (sc->sc_scord_mbuf != NULL) {
		m_freem(sc->sc_scord_mbuf);
		sc->sc_scord_mbuf = NULL;
	}

	if (sc->sc_scowr_mbuf != NULL) {
		m_freem(sc->sc_scowr_mbuf);
		sc->sc_scowr_mbuf = NULL;
	}
}

/*******************************************************************************
 *
 * Bluetooth Unit/USB callbacks
 *
 * All of this will be called at the IPL_ we specified above
 */
int
ubt_enable(struct hci_unit *unit)
{
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;
	usbd_status err;
	int i, error;

	DPRINTFN(1, "sc=%p\n", sc);

	if (unit->hci_flags & BTF_RUNNING)
		return 0;

	/* Events */
	sc->sc_evt_buf = malloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
	if (sc->sc_evt_buf == NULL) {
		error = ENOMEM;
		goto bad;
	}
	err = usbd_open_pipe_intr(sc->sc_iface0,
				  sc->sc_evt_addr,
				  USBD_SHORT_XFER_OK,
				  &sc->sc_evt_pipe,
				  sc,
				  sc->sc_evt_buf,
				  UBT_BUFSIZ_EVENT,
				  ubt_recv_event,
				  USBD_DEFAULT_INTERVAL);
	if (err != USBD_NORMAL_COMPLETION) {
		error = EIO;
		goto bad;
	}

	/* Commands */
	sc->sc_cmd_xfer = usbd_alloc_xfer(sc->sc_udev);
	if (sc->sc_cmd_xfer == NULL) {
		error = ENOMEM;
		goto bad;
	}
	sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
	if (sc->sc_cmd_buf == NULL) {
		error = ENOMEM;
		goto bad;
	}

	/* ACL read */
	err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
				USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
	if (err != USBD_NORMAL_COMPLETION) {
		error = EIO;
		goto bad;
	}
	sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
	if (sc->sc_aclrd_xfer == NULL) {
		error = ENOMEM;
		goto bad;
	}
	sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
	if (sc->sc_aclrd_buf == NULL) {
		error = ENOMEM;
		goto bad;
	}
	sc->sc_aclrd_busy = 0;
	ubt_recv_acl_start(sc);

	/* ACL write */
	err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
				USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
	if (err != USBD_NORMAL_COMPLETION) {
		error = EIO;
		goto bad;
	}
	sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
	if (sc->sc_aclwr_xfer == NULL) {
		error = ENOMEM;
		goto bad;
	}
	sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
	if (sc->sc_aclwr_buf == NULL) {
		error = ENOMEM;
		goto bad;
	}

	/* SCO read */
	if (sc->sc_scord_size > 0) {
		err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
					USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
		if (err != USBD_NORMAL_COMPLETION) {
			error = EIO;
			goto bad;
		}

		for (i = 0 ; i < UBT_NXFERS ; i++) {
			sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev);
			if (sc->sc_scord[i].xfer == NULL) {
				error = ENOMEM;
				goto bad;
			}
			sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
						sc->sc_scord_size * UBT_NFRAMES);
			if (sc->sc_scord[i].buf == NULL) {
				error = ENOMEM;
				goto bad;
			}
			sc->sc_scord[i].softc = sc;
			sc->sc_scord[i].busy = 0;
			ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
		}
	}

	/* SCO write */
	if (sc->sc_scowr_size > 0) {
		err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
					USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
		if (err != USBD_NORMAL_COMPLETION) {
			error = EIO;
			goto bad;
		}

		for (i = 0 ; i < UBT_NXFERS ; i++) {
			sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev);
			if (sc->sc_scowr[i].xfer == NULL) {
				error = ENOMEM;
				goto bad;
			}
			sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
						sc->sc_scowr_size * UBT_NFRAMES);
			if (sc->sc_scowr[i].buf == NULL) {
				error = ENOMEM;
				goto bad;
			}
			sc->sc_scowr[i].softc = sc;
			sc->sc_scowr[i].busy = 0;
		}
	}

	unit->hci_flags &= ~BTF_XMIT;
	unit->hci_flags |= BTF_RUNNING;
	return 0;

bad:
	ubt_abortdealloc(sc);
	return error;
}

void
ubt_disable(struct hci_unit *unit)
{
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;

	DPRINTFN(1, "sc=%p\n", sc);

	if ((unit->hci_flags & BTF_RUNNING) == 0)
		return;

	ubt_abortdealloc(sc);

	unit->hci_flags &= ~BTF_RUNNING;
}

void
ubt_xmit_cmd_start(struct hci_unit *unit)
{
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;
	usb_device_request_t req;
	usbd_status status;
	struct mbuf *m;
	int len;

	if (sc->sc_dying)
		return;

	if (IF_IS_EMPTY(&unit->hci_cmdq))
		return;

	IF_DEQUEUE(&unit->hci_cmdq, m);

	DPRINTFN(15, "%s: xmit CMD packet (%d bytes)\n",
			unit->hci_devname, m->m_pkthdr.len);

	sc->sc_refcnt++;
	unit->hci_flags |= BTF_XMIT_CMD;

	len = m->m_pkthdr.len - 1;
	m_copydata(m, 1, len, sc->sc_cmd_buf);
	m_freem(m);

	memset(&req, 0, sizeof(req));
	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
	USETW(req.wLength, len);

	usbd_setup_default_xfer(sc->sc_cmd_xfer,
				sc->sc_udev,
				unit,
				UBT_CMD_TIMEOUT,
				&req,
				sc->sc_cmd_buf,
				len,
				USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
				ubt_xmit_cmd_complete);

	status = usbd_transfer(sc->sc_cmd_xfer);

	KASSERT(status != USBD_NORMAL_COMPLETION);

	if (status != USBD_IN_PROGRESS) {
		DPRINTF("usbd_transfer status=%s (%d)\n",
			usbd_errstr(status), status);

		sc->sc_refcnt--;
		unit->hci_flags &= ~BTF_XMIT_CMD;
	}
}

void
ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
			usbd_private_handle h, usbd_status status)
{
	struct hci_unit *unit = h;
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;
	uint32_t count;

	DPRINTFN(15, "%s: CMD complete status=%s (%d)\n",
			unit->hci_devname, usbd_errstr(status), status);

	unit->hci_flags &= ~BTF_XMIT_CMD;

	if (--sc->sc_refcnt < 0) {
		DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
		usb_detach_wakeup(&sc->sc_dev);
		return;
	}

	if (sc->sc_dying) {
		DPRINTF("sc_dying\n");
		return;
	}

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF("status=%s (%d)\n",
			usbd_errstr(status), status);

		unit->hci_stats.err_tx++;
		return;
	}

	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
	unit->hci_stats.cmd_tx++;
	unit->hci_stats.byte_tx += count;

	ubt_xmit_cmd_start(unit);
}

void
ubt_xmit_acl_start(struct hci_unit *unit)
{
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;
	struct mbuf *m;
	usbd_status status;
	int len;

	if (sc->sc_dying)
		return;

	if (IF_IS_EMPTY(&unit->hci_acltxq))
		return;

	sc->sc_refcnt++;
	unit->hci_flags |= BTF_XMIT_ACL;

	IF_DEQUEUE(&unit->hci_acltxq, m);

	DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
			unit->hci_devname, m->m_pkthdr.len);

	len = m->m_pkthdr.len - 1;
	if (len > UBT_BUFSIZ_ACL) {
		DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
			unit->hci_devname, len, UBT_BUFSIZ_ACL);

		len = UBT_BUFSIZ_ACL;
	}

	m_copydata(m, 1, len, sc->sc_aclwr_buf);
	m_freem(m);

	unit->hci_stats.acl_tx++;
	unit->hci_stats.byte_tx += len;

	usbd_setup_xfer(sc->sc_aclwr_xfer,
			sc->sc_aclwr_pipe,
			unit,
			sc->sc_aclwr_buf,
			len,
			USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
			UBT_ACL_TIMEOUT,
			ubt_xmit_acl_complete);

	status = usbd_transfer(sc->sc_aclwr_xfer);

	KASSERT(status != USBD_NORMAL_COMPLETION);

	if (status != USBD_IN_PROGRESS) {
		DPRINTF("usbd_transfer status=%s (%d)\n",
			usbd_errstr(status), status);

		sc->sc_refcnt--;
		unit->hci_flags &= ~BTF_XMIT_ACL;
	}
}

void
ubt_xmit_acl_complete(usbd_xfer_handle xfer,
		usbd_private_handle h, usbd_status status)
{
	struct hci_unit *unit = h;
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;

	DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
		unit->hci_devname, usbd_errstr(status), status);

	unit->hci_flags &= ~BTF_XMIT_ACL;

	if (--sc->sc_refcnt < 0) {
		usb_detach_wakeup(&sc->sc_dev);
		return;
	}

	if (sc->sc_dying)
		return;

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF("status=%s (%d)\n",
			usbd_errstr(status), status);

		unit->hci_stats.err_tx++;

		if (status == USBD_STALLED)
			usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
		else
			return;
	}

	ubt_xmit_acl_start(unit);
}

void
ubt_xmit_sco_start(struct hci_unit *unit)
{
	struct ubt_softc *sc = (struct ubt_softc *)unit->hci_softc;
	int i;

	if (sc->sc_dying || sc->sc_scowr_size == 0)
		return;

	for (i = 0 ; i < UBT_NXFERS ; i++) {
		if (sc->sc_scowr[i].busy)
			continue;

		ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
	}
}

void
ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
{
	struct mbuf *m;
	uint8_t *buf;
	int num, len, size, space;

	space = sc->sc_scowr_size * UBT_NFRAMES;
	buf = isoc->buf;
	len = 0;

	/*
	 * Fill the request buffer with data from the queue,
	 * keeping any leftover packet on our private hook.
	 *
	 * Complete packets are passed back up to the stack
	 * for disposal, since we can't rely on the controller
	 * to tell us when it has finished with them.
	 */

	m = sc->sc_scowr_mbuf;
	while (space > 0) {
		if (m == NULL) {
			IF_DEQUEUE(&sc->sc_unit.hci_scotxq, m);
			if (m == NULL)
				break;

			m_adj(m, 1);	/* packet type */
		}

		if (m->m_pkthdr.len > 0) {
			size = MIN(m->m_pkthdr.len, space);

			m_copydata(m, 0, size, buf);
			m_adj(m, size);

			buf += size;
			len += size;
			space -= size;
		}

		if (m->m_pkthdr.len == 0) {
			sc->sc_unit.hci_stats.sco_tx++;
			hci_complete_sco(&sc->sc_unit, m);
			m = NULL;
		}
	}
	sc->sc_scowr_mbuf = m;

	DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);

	if (len == 0)	/* nothing to send */
		return;

	sc->sc_refcnt++;
	sc->sc_unit.hci_flags |= BTF_XMIT_SCO;
	sc->sc_unit.hci_stats.byte_tx += len;
	isoc->busy = 1;

	/*
	 * calculate number of isoc frames and sizes
	 */

	for (num = 0 ; len > 0 ; num++) {
		size = MIN(sc->sc_scowr_size, len);

		isoc->size[num] = size;
		len -= size;
	}

	usbd_setup_isoc_xfer(isoc->xfer,
			     sc->sc_scowr_pipe,
			     isoc,
			     isoc->size,
			     num,
			     USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
			     ubt_xmit_sco_complete);

	usbd_transfer(isoc->xfer);
}

void
ubt_xmit_sco_complete(usbd_xfer_handle xfer,
		usbd_private_handle h, usbd_status status)
{
	struct ubt_isoc_xfer *isoc = h;
	struct ubt_softc *sc;
	int i;

	KASSERT(xfer == isoc->xfer);
	sc = isoc->softc;

	DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
		isoc, usbd_errstr(status), status);

	isoc->busy = 0;

	for (i = 0 ; ; i++) {
		if (i == UBT_NXFERS) {
			sc->sc_unit.hci_flags &= ~BTF_XMIT_SCO;
			break;
		}

		if (sc->sc_scowr[i].busy)
			break;
	}

	if (--sc->sc_refcnt < 0) {
		usb_detach_wakeup(&sc->sc_dev);
		return;
	}

	if (sc->sc_dying)
		return;

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF("status=%s (%d)\n",
			usbd_errstr(status), status);

		sc->sc_unit.hci_stats.err_tx++;

		if (status == USBD_STALLED)
			usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
		else
			return;
	}

	ubt_xmit_sco_start(&sc->sc_unit);
}

/*
 * load incoming data into an mbuf with
 * leading type byte
 */
static struct mbuf *
ubt_mbufload(uint8_t *buf, int count, uint8_t type)
{
	struct mbuf *m;

	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m == NULL)
		return NULL;

	*mtod(m, uint8_t *) = type;
	m->m_pkthdr.len = m->m_len = MHLEN;
	m_copyback(m, 1, count, buf);	// (extends if necessary)
	if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
		m_free(m);
		return NULL;
	}

	m->m_pkthdr.len = count + 1;
	m->m_len = MIN(MHLEN, m->m_pkthdr.len);

	return m;
}

void
ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
{
	struct ubt_softc *sc = h;
	struct mbuf *m;
	uint32_t count;
	void *buf;

	DPRINTFN(15, "sc=%p status=%s (%d)\n",
		    sc, usbd_errstr(status), status);

	if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
		return;

	usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);

	if (count < sizeof(hci_event_hdr_t) - 1) {
		DPRINTF("dumped undersized event (count = %d)\n", count);
		sc->sc_unit.hci_stats.err_rx++;
		return;
	}

	sc->sc_unit.hci_stats.evt_rx++;
	sc->sc_unit.hci_stats.byte_rx += count;

	m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
	if (m != NULL)
		hci_input_event(&sc->sc_unit, m);
	else
		sc->sc_unit.hci_stats.err_rx++;
}

void
ubt_recv_acl_start(struct ubt_softc *sc)
{
	usbd_status status;

	DPRINTFN(15, "sc=%p\n", sc);

	if (sc->sc_aclrd_busy || sc->sc_dying) {
		DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
			sc->sc_aclrd_busy,
			sc->sc_dying);

		return;
	}

	sc->sc_refcnt++;
	sc->sc_aclrd_busy = 1;

	usbd_setup_xfer(sc->sc_aclrd_xfer,
			sc->sc_aclrd_pipe,
			sc,
			sc->sc_aclrd_buf,
			UBT_BUFSIZ_ACL,
			USBD_NO_COPY | USBD_SHORT_XFER_OK,
			USBD_NO_TIMEOUT,
			ubt_recv_acl_complete);

	status = usbd_transfer(sc->sc_aclrd_xfer);

	KASSERT(status != USBD_NORMAL_COMPLETION);

	if (status != USBD_IN_PROGRESS) {
		DPRINTF("usbd_transfer status=%s (%d)\n",
			usbd_errstr(status), status);

		sc->sc_refcnt--;
		sc->sc_aclrd_busy = 0;
	}
}

void
ubt_recv_acl_complete(usbd_xfer_handle xfer,
		usbd_private_handle h, usbd_status status)
{
	struct ubt_softc *sc = h;
	struct mbuf *m;
	uint32_t count;
	void *buf;

	DPRINTFN(15, "sc=%p status=%s (%d)\n",
			sc, usbd_errstr(status), status);

	sc->sc_aclrd_busy = 0;

	if (--sc->sc_refcnt < 0) {
		DPRINTF("refcnt = %d\n", sc->sc_refcnt);
		usb_detach_wakeup(&sc->sc_dev);
		return;
	}

	if (sc->sc_dying) {
		DPRINTF("sc_dying\n");
		return;
	}

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF("status=%s (%d)\n",
			usbd_errstr(status), status);

		sc->sc_unit.hci_stats.err_rx++;

		if (status == USBD_STALLED)
			usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
		else
			return;
	} else {
		usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);

		if (count < sizeof(hci_acldata_hdr_t) - 1) {
			DPRINTF("dumped undersized packet (%d)\n", count);
			sc->sc_unit.hci_stats.err_rx++;
		} else {
			sc->sc_unit.hci_stats.acl_rx++;
			sc->sc_unit.hci_stats.byte_rx += count;

			m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
			if (m != NULL)
				hci_input_acl(&sc->sc_unit, m);
			else
				sc->sc_unit.hci_stats.err_rx++;
		}
	}

	/* and restart */
	ubt_recv_acl_start(sc);
}

void
ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
{
	int i;

	DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);

	if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
		DPRINTF("%s%s%s\n",
			isoc->busy ? " busy" : "",
			sc->sc_dying ? " dying" : "",
			sc->sc_scord_size == 0 ? " size=0" : "");

		return;
	}

	sc->sc_refcnt++;
	isoc->busy = 1;

	for (i = 0 ; i < UBT_NFRAMES ; i++)
		isoc->size[i] = sc->sc_scord_size;

	usbd_setup_isoc_xfer(isoc->xfer,
			     sc->sc_scord_pipe,
			     isoc,
			     isoc->size,
			     UBT_NFRAMES,
			     USBD_NO_COPY | USBD_SHORT_XFER_OK,
			     ubt_recv_sco_complete);

	usbd_transfer(isoc->xfer);
}

void
ubt_recv_sco_complete(usbd_xfer_handle xfer,
		usbd_private_handle h, usbd_status status)
{
	struct ubt_isoc_xfer *isoc = h;
	struct ubt_softc *sc;
	struct mbuf *m;
	uint32_t count;
	uint8_t *ptr, *frame;
	int i, size, got, want;

	KASSERT(isoc != NULL);
	KASSERT(isoc->xfer == xfer);

	sc = isoc->softc;
	isoc->busy = 0;

	if (--sc->sc_refcnt < 0) {
		DPRINTF("refcnt=%d\n", sc->sc_refcnt);
		usb_detach_wakeup(&sc->sc_dev);
		return;
	}

	if (sc->sc_dying) {
		DPRINTF("sc_dying\n");
		return;
	}

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF("status=%s (%d)\n",
			usbd_errstr(status), status);

		sc->sc_unit.hci_stats.err_rx++;

		if (status == USBD_STALLED) {
			usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
			goto restart;
		}

		return;
	}

	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
	if (count == 0)
		goto restart;

	DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
			sc, isoc, count);

	sc->sc_unit.hci_stats.byte_rx += count;

	/*
	 * Extract SCO packets from ISOC frames. The way we have it,
	 * no SCO packet can be bigger than MHLEN. This is unlikely
	 * to actually happen, but if we ran out of mbufs and lost
	 * sync then we may get spurious data that makes it seem that
	 * way, so we discard data that wont fit. This doesnt really
	 * help with the lost sync situation alas.
	 */

	m = sc->sc_scord_mbuf;
	if (m != NULL) {
		sc->sc_scord_mbuf = NULL;
		ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
		got = m->m_pkthdr.len;
		want = sizeof(hci_scodata_hdr_t);
		if (got >= want)
			want += mtod(m, hci_scodata_hdr_t *)->length ;
	} else {
		ptr = NULL;
		got = 0;
		want = 0;
	}

	for (i = 0 ; i < UBT_NFRAMES ; i++) {
		frame = isoc->buf + (i * sc->sc_scord_size);

		while (isoc->size[i] > 0) {
			size = isoc->size[i];

			if (m == NULL) {
				MGETHDR(m, M_DONTWAIT, MT_DATA);
				if (m == NULL) {
					printf("%s: out of memory (xfer halted)\n",
						sc->sc_dev.dv_xname);

					sc->sc_unit.hci_stats.err_rx++;
					return;		/* lost sync */
				}

				ptr = mtod(m, uint8_t *);
				*ptr++ = HCI_SCO_DATA_PKT;
				got = 1;
				want = sizeof(hci_scodata_hdr_t);
			}

			if (got + size > want)
				size = want - got;

			if (got + size > MHLEN)
				memcpy(ptr, frame, MHLEN - got);
			else
				memcpy(ptr, frame, size);

			ptr += size;
			got += size;
			frame += size;

			if (got == want) {
				/*
				 * If we only got a header, add the packet
				 * length to our want count. Send complete
				 * packets up to protocol stack.
				 */
				if (want == sizeof(hci_scodata_hdr_t))
					want += mtod(m, hci_scodata_hdr_t *)->length;

				if (got == want) {
					m->m_pkthdr.len = m->m_len = got;
					sc->sc_unit.hci_stats.sco_rx++;
					hci_input_sco(&sc->sc_unit, m);
					m = NULL;
				}
			}

			isoc->size[i] -= size;
		}
	}

	if (m != NULL) {
		m->m_pkthdr.len = m->m_len = got;
		sc->sc_scord_mbuf = m;
	}

restart: /* and restart */
	ubt_recv_sco_start1(sc, isoc);
}