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
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
|
/* $OpenBSD: ubt.c,v 1.26 2013/12/15 14:28:35 pirofti Exp $ */
/* $NetBSD: ubt.c,v 1.35 2008/07/28 14:19:26 drochner 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.
*
* 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/param.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/systm.h>
#include <sys/timeout.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
#define UBT_DEBUG 0
#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;
struct usbd_xfer *xfer;
uint8_t *buf;
uint16_t size[UBT_NFRAMES];
int busy;
};
struct ubt_softc {
struct device sc_dev;
struct usbd_device *sc_udev;
int sc_refcnt;
int sc_enabled;
/* Control Interface */
struct usbd_interface *sc_iface0;
/* Commands (control) */
struct usbd_xfer *sc_cmd_xfer;
uint8_t *sc_cmd_buf;
int sc_cmd_busy; /* write active */
struct ifqueue sc_cmd_queue; /* output queue */
/* Events (interrupt) */
int sc_evt_addr; /* endpoint address */
struct usbd_pipe *sc_evt_pipe;
uint8_t *sc_evt_buf;
/* ACL data (in) */
int sc_aclrd_addr; /* endpoint address */
struct usbd_pipe *sc_aclrd_pipe; /* read pipe */
struct usbd_xfer *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 */
struct usbd_pipe *sc_aclwr_pipe; /* write pipe */
struct usbd_xfer *sc_aclwr_xfer; /* write xfer */
uint8_t *sc_aclwr_buf; /* write buffer */
int sc_aclwr_busy; /* write active */
struct ifqueue sc_aclwr_queue;/* output queue */
/* ISOC interface */
struct usbd_interface *sc_iface1; /* ISOC interface */
int sc_config; /* current config no */
int sc_alt_config; /* no of alternates */
/* SCO data (in) */
int sc_scord_addr; /* endpoint address */
struct usbd_pipe *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 */
struct usbd_pipe *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 */
int sc_scowr_busy; /* write active */
struct ifqueue sc_scowr_queue;/* output queue */
/* Protocol structure */
struct hci_unit *sc_unit;
struct bt_stats sc_stats;
/* Successfully attached */
int sc_ok;
};
/*
* Bluetooth unit/USB callback routines
*/
int ubt_enable(struct device *);
void ubt_disable(struct device *);
void ubt_xmit_cmd(struct device *, struct mbuf *);
void ubt_xmit_cmd_start(struct ubt_softc *);
void ubt_xmit_cmd_complete(struct usbd_xfer *, void *, usbd_status);
void ubt_xmit_acl(struct device *, struct mbuf *);
void ubt_xmit_acl_start(struct ubt_softc *);
void ubt_xmit_acl_complete(struct usbd_xfer *, void *, usbd_status);
void ubt_xmit_sco(struct device *, struct mbuf *);
void ubt_xmit_sco_start(struct ubt_softc *);
void ubt_xmit_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
void ubt_xmit_sco_complete(struct usbd_xfer *, void *, usbd_status);
void ubt_recv_event(struct usbd_xfer *, void *, usbd_status);
void ubt_recv_acl_start(struct ubt_softc *);
void ubt_recv_acl_complete(struct usbd_xfer *, void *, usbd_status);
void ubt_recv_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
void ubt_recv_sco_complete(struct usbd_xfer *, void *, usbd_status);
void ubt_stats(struct device *, struct bt_stats *, int);
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 *, int);
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,
};
const struct hci_if ubt_hci = {
.enable = ubt_enable,
.disable = ubt_disable,
.output_cmd = ubt_xmit_cmd,
.output_acl = ubt_xmit_acl,
.output_sco = ubt_xmit_sco,
.get_stats = ubt_stats,
.ipl = IPL_USB, /* IPL_SOFTUSB ??? */
};
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_BCM2033NF }
};
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;
#ifndef __OpenBSD__ /* ??? */
MBUFQ_INIT(&sc->sc_cmd_queue);
MBUFQ_INIT(&sc->sc_aclwr_queue);
MBUFQ_INIT(&sc->sc_scowr_queue);
#endif
/*
* 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_attach(&ubt_hci, &sc->sc_dev, 0);
sc->sc_ok = 1;
/* XXX pmf_device_deregister in NetBSD (power hook) */
}
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);
/* XXX pmf_device_deregister in NetBSD (power hook) */
if (!sc->sc_ok)
return 0;
/* Detach HCI interface */
if (sc->sc_unit) {
hci_detach(sc->sc_unit);
sc->sc_unit = NULL;
}
/*
* 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);
DPRINTFN(1, "driver detached\n");
return 0;
}
int
ubt_activate(struct device *self, int act)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
switch (act) {
case DVACT_DEACTIVATE:
usbd_deactivate(sc->sc_udev);
break;
}
return (0);
}
/* 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;
}
/* Empty mbuf queues */
IF_PURGE(&sc->sc_cmd_queue);
IF_PURGE(&sc->sc_aclwr_queue);
IF_PURGE(&sc->sc_scowr_queue);
}
/*******************************************************************************
*
* Bluetooth Unit/USB callbacks
*
*/
int
ubt_enable(struct device *self)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
usbd_status err;
int s, i, error;
DPRINTFN(1, "sc=%p\n", sc);
if (sc->sc_enabled)
return 0;
s = splusb();
/* 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;
}
sc->sc_cmd_busy = 0;
/* 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;
}
sc->sc_aclwr_busy = 0;
/* 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;
}
sc->sc_scowr_busy = 0;
}
sc->sc_enabled = 1;
splx(s);
return 0;
bad:
ubt_abortdealloc(sc);
splx(s);
return error;
}
void
ubt_disable(struct device *self)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
int s;
DPRINTFN(1, "sc=%p\n", sc);
if (sc->sc_enabled == 0)
return;
s = splusb();
ubt_abortdealloc(sc);
sc->sc_enabled = 0;
splx(s);
}
void
ubt_xmit_cmd(struct device *self, struct mbuf *m)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
int s;
KASSERT(sc->sc_enabled);
s = splusb();
IF_ENQUEUE(&sc->sc_cmd_queue, m);
if (sc->sc_cmd_busy == 0)
ubt_xmit_cmd_start(sc);
splx(s);
}
void
ubt_xmit_cmd_start(struct ubt_softc *sc)
{
usb_device_request_t req;
usbd_status status;
struct mbuf *m;
int len;
if (usbd_is_dying(sc->sc_udev))
return;
if (IF_IS_EMPTY(&sc->sc_cmd_queue))
return;
IF_DEQUEUE(&sc->sc_cmd_queue, m);
KASSERT(m != NULL);
DPRINTFN(15, "%s: xmit CMD packet (%d bytes)\n",
&sc->sc_dev, m->m_pkthdr.len);
sc->sc_refcnt++;
sc->sc_cmd_busy = 1;
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,
sc,
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--;
sc->sc_cmd_busy = 0;
}
}
void
ubt_xmit_cmd_complete(struct usbd_xfer *xfer, void *h, usbd_status status)
{
struct ubt_softc *sc = h;
uint32_t count;
DPRINTFN(15, "%s: CMD complete status=%s (%d)\n",
sc->sc_dev.dv_xname, usbd_errstr(status), status);
sc->sc_cmd_busy = 0;
if (--sc->sc_refcnt < 0) {
DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
usb_detach_wakeup(&sc->sc_dev);
return;
}
if (usbd_is_dying(sc->sc_udev)) {
DPRINTF("dying\n");
return;
}
if (status != USBD_NORMAL_COMPLETION) {
DPRINTF("status=%s (%d)\n",
usbd_errstr(status), status);
sc->sc_stats.err_tx++;
return;
}
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
sc->sc_stats.cmd_tx++;
sc->sc_stats.byte_tx += count;
ubt_xmit_cmd_start(sc);
}
void
ubt_xmit_acl(struct device *self, struct mbuf *m)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
int s;
KASSERT(sc->sc_enabled);
s = splusb();
IF_ENQUEUE(&sc->sc_aclwr_queue, m);
if (sc->sc_aclwr_busy == 0)
ubt_xmit_acl_start(sc);
splx(s);
}
void
ubt_xmit_acl_start(struct ubt_softc *sc)
{
struct mbuf *m;
usbd_status status;
int len;
if (usbd_is_dying(sc->sc_udev))
return;
if (IF_IS_EMPTY(&sc->sc_aclwr_queue))
return;
sc->sc_refcnt++;
sc->sc_aclwr_busy = 1;
IF_DEQUEUE(&sc->sc_aclwr_queue, m);
KASSERT(m != NULL);
DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
sc->sc_dev.dv_xname, m->m_pkthdr.len);
len = m->m_pkthdr.len - 1;
if (len > UBT_BUFSIZ_ACL) {
DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
sc->sc_dev.dv_xname, len, UBT_BUFSIZ_ACL);
len = UBT_BUFSIZ_ACL;
}
m_copydata(m, 1, len, sc->sc_aclwr_buf);
m_freem(m);
sc->sc_stats.acl_tx++;
sc->sc_stats.byte_tx += len;
usbd_setup_xfer(sc->sc_aclwr_xfer,
sc->sc_aclwr_pipe,
sc,
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--;
sc->sc_aclwr_busy = 0;
}
}
void
ubt_xmit_acl_complete(struct usbd_xfer *xfer, void *h, usbd_status status)
{
struct ubt_softc *sc = h;
DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
sc->sc_dev.dv_xname, usbd_errstr(status), status);
sc->sc_aclwr_busy = 0;
if (--sc->sc_refcnt < 0) {
usb_detach_wakeup(&sc->sc_dev);
return;
}
if (usbd_is_dying(sc->sc_udev))
return;
if (status != USBD_NORMAL_COMPLETION) {
DPRINTF("status=%s (%d)\n",
usbd_errstr(status), status);
sc->sc_stats.err_tx++;
if (status == USBD_STALLED)
usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
else
return;
}
ubt_xmit_acl_start(sc);
}
void
ubt_xmit_sco(struct device *self, struct mbuf *m)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
int s;
KASSERT(sc->sc_enabled);
s = splusb();
IF_ENQUEUE(&sc->sc_scowr_queue, m);
if (sc->sc_scowr_busy == 0)
ubt_xmit_sco_start(sc);
splx(s);
}
void
ubt_xmit_sco_start(struct ubt_softc *sc)
{
int i;
if (usbd_is_dying(sc->sc_udev) || 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_scowr_queue, 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_stats.sco_tx++;
if (!hci_complete_sco(sc->sc_unit, m))
sc->sc_stats.err_tx++;
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_scowr_busy = 1;
sc->sc_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(struct usbd_xfer *xfer, void *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_scowr_busy = 0;
break;
}
if (sc->sc_scowr[i].busy)
break;
}
if (--sc->sc_refcnt < 0) {
usb_detach_wakeup(&sc->sc_dev);
return;
}
if (usbd_is_dying(sc->sc_udev))
return;
if (status != USBD_NORMAL_COMPLETION) {
DPRINTF("status=%s (%d)\n",
usbd_errstr(status), status);
sc->sc_stats.err_tx++;
if (status == USBD_STALLED)
usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
else
return;
}
ubt_xmit_sco_start(sc);
}
/*
* 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, M_NOWAIT); /* extends if this doesn't fail */
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(struct usbd_xfer *xfer, void *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 || usbd_is_dying(sc->sc_udev))
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_stats.err_rx++;
return;
}
sc->sc_stats.evt_rx++;
sc->sc_stats.byte_rx += count;
m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
if (m == NULL || !hci_input_event(sc->sc_unit, m))
sc->sc_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) {
DPRINTF("sc_aclrd_busy=%d\n", sc->sc_aclrd_busy);
return;
}
if (usbd_is_dying(sc->sc_udev)) {
DPRINTF("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(struct usbd_xfer *xfer, void *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 (usbd_is_dying(sc->sc_udev)) {
DPRINTF("dying\n");
return;
}
if (status != USBD_NORMAL_COMPLETION) {
DPRINTF("status=%s (%d)\n",
usbd_errstr(status), status);
sc->sc_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_stats.err_rx++;
} else {
sc->sc_stats.acl_rx++;
sc->sc_stats.byte_rx += count;
m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
if (m == NULL || !hci_input_acl(sc->sc_unit, m))
sc->sc_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 (usbd_is_dying(sc->sc_udev)) {
DPRINTF("dying");
return;
}
if (isoc->busy || sc->sc_scord_size == 0) {
DPRINTF("%s%s\n",
isoc->busy ? " busy" : "",
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(struct usbd_xfer *xfer, void *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 (usbd_is_dying(sc->sc_udev)) {
DPRINTF("dying\n");
return;
}
if (status != USBD_NORMAL_COMPLETION) {
DPRINTF("status=%s (%d)\n",
usbd_errstr(status), status);
sc->sc_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_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_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_stats.sco_rx++;
if (!hci_input_sco(sc->sc_unit, m))
sc->sc_stats.err_rx++;
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);
}
void
ubt_stats(struct device *self, struct bt_stats *dest, int flush)
{
struct ubt_softc *sc = (struct ubt_softc *)self;
int s;
s = splusb();
memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
if (flush)
memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
splx(s);
}
|