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
|
/* $NetBSD: btuart.c,v 1.10 2007/11/03 17:41:04 plunky Exp $ */
/*
* Copyright (c) 2006, 2007 KIYOHARA Takashi
* 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 ``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 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: btuart.c,v 1.10 2007/11/03 17:41:04 plunky Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/syslimits.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/bus.h>
#include <sys/intr.h>
#include <netbt/bluetooth.h>
#include <netbt/hci.h>
#include <dev/bluetooth/btuart.h>
#include <dev/firmload.h>
#include "ioconf.h"
#ifdef BTUART_DEBUG
int btuart_debug = 1;
#endif
struct btuart_softc;
struct bth4hci {
int type;
int init_baud;
#define FLOW_CTL 1
int flags;
int (*init)(struct btuart_softc *);
};
struct btuart_softc {
device_t sc_dev;
struct tty *sc_tp;
struct hci_unit sc_unit; /* Bluetooth HCI Unit */
struct bth4hci sc_bth4hci;
int sc_baud;
int sc_state; /* receive state */
#define BTUART_RECV_PKT_TYPE 0 /* packet type */
#define BTUART_RECV_ACL_HDR 1 /* acl header */
#define BTUART_RECV_SCO_HDR 2 /* sco header */
#define BTUART_RECV_EVENT_HDR 3 /* event header */
#define BTUART_RECV_ACL_DATA 4 /* acl packet data */
#define BTUART_RECV_SCO_DATA 5 /* sco packet data */
#define BTUART_RECV_EVENT_DATA 6 /* event packet data */
int sc_want; /* how much we want */
struct mbuf *sc_rxp; /* incoming packet */
struct mbuf *sc_txp; /* outgoing packet */
void (*sc_input_acl)(struct hci_unit *, struct mbuf *);
void (*sc_input_sco)(struct hci_unit *, struct mbuf *);
void (*sc_input_event)(struct hci_unit *, struct mbuf *);
};
void btuartattach(int);
static int btuart_match(device_t, struct cfdata *, void *);
static void btuart_attach(device_t, device_t, void *);
static int btuart_detach(device_t, int);
static int bth4_waitresp(struct btuart_softc *, struct mbuf **, uint16_t);
static int bth4_firmload(struct btuart_softc *, char *,
int (*)(struct btuart_softc *, int, char *));
static int init_ericsson(struct btuart_softc *);
static int init_digi(struct btuart_softc *);
static int init_texas(struct btuart_softc *);
static int init_csr(struct btuart_softc *);
static int init_swave(struct btuart_softc *);
static int init_st(struct btuart_softc *);
static int firmload_stlc2500(struct btuart_softc *, int, char *);
static int init_stlc2500(struct btuart_softc *);
static int init_bcm2035(struct btuart_softc *);
static int bth4init(struct btuart_softc *);
static void bth4init_input(struct hci_unit *, struct mbuf *);
static int bth4open(dev_t, struct tty *);
static int bth4close(struct tty *, int);
static int bth4ioctl(struct tty *, u_long, void *, int, struct lwp *);
static int bth4input(int, struct tty *);
static int bth4start(struct tty *);
static int bth4_enable(struct hci_unit *);
static void bth4_disable(struct hci_unit *);
static void bth4_start(struct hci_unit *);
/*
* It doesn't need to be exported, as only btuartattach() uses it,
* but there's no "official" way to make it static.
*/
CFATTACH_DECL_NEW(btuart, sizeof(struct btuart_softc),
btuart_match, btuart_attach, btuart_detach, NULL);
static struct linesw bth4_disc = {
.l_name = "btuart",
.l_open = bth4open,
.l_close = bth4close,
.l_read = ttyerrio,
.l_write = ttyerrio,
.l_ioctl = bth4ioctl,
.l_rint = bth4input,
.l_start = bth4start,
.l_modem = ttymodem,
.l_poll = ttyerrpoll
};
static struct bth4hci bth4hci[] = {
{ BTUART_HCITYPE_ANY, B0, FLOW_CTL, NULL },
{ BTUART_HCITYPE_ERICSSON, B57600, FLOW_CTL, init_ericsson },
{ BTUART_HCITYPE_DIGI, B9600, FLOW_CTL, init_digi },
{ BTUART_HCITYPE_TEXAS, B115200, FLOW_CTL, init_texas },
/* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
{ BTUART_HCITYPE_CSR, B115200, FLOW_CTL, init_csr },
/* Silicon Wave kits */
{ BTUART_HCITYPE_SWAVE, B115200, FLOW_CTL, init_swave },
/* ST Microelectronics minikits based on STLC2410/STLC2415 */
{ BTUART_HCITYPE_ST, B57600, FLOW_CTL, init_st },
/* ST Microelectronics minikits based on STLC2500 */
{ BTUART_HCITYPE_STLC2500, B115200, FLOW_CTL, init_stlc2500 },
/* AmbiCom BT2000C Bluetooth PC/CF Card */
{ BTUART_HCITYPE_BT2000C, B57600, FLOW_CTL, init_csr },
/* Broadcom BCM2035 */
{ BTUART_HCITYPE_BCM2035, B115200, 0, init_bcm2035 },
{ -1, B0, 0, NULL }
};
/* ARGSUSED */
void
btuartattach(int num __unused)
{
int error;
error = ttyldisc_attach(&bth4_disc);
if (error) {
aprint_error("%s: unable to register line discipline, "
"error = %d\n", btuart_cd.cd_name, error);
return;
}
error = config_cfattach_attach(btuart_cd.cd_name, &btuart_ca);
if (error) {
aprint_error("%s: unable to register cfattach, error = %d\n",
btuart_cd.cd_name, error);
config_cfdriver_detach(&btuart_cd);
(void) ttyldisc_detach(&bth4_disc);
}
}
/*
* Autoconf match routine.
*
* XXX: unused: config_attach_pseudo(9) does not call ca_match.
*/
/* ARGSUSED */
static int
btuart_match(device_t self __unused,
struct cfdata *cfdata __unused, void *arg __unused)
{
/* pseudo-device; always present */
return 1;
}
/*
* Autoconf attach routine. Called by config_attach_pseudo(9) when we
* open the line discipline.
*/
/* ARGSUSED */
static void
btuart_attach(device_t parent __unused,
device_t self, void *aux __unused)
{
struct btuart_softc *sc = device_private(self);
int i;
sc->sc_dev = self;
aprint_normal("\n");
aprint_naive("\n");
sc->sc_input_acl = bth4init_input;
sc->sc_input_sco = bth4init_input;
sc->sc_input_event = bth4init_input;
/* Copy default type */
for (i = 0; bth4hci[i].type != BTUART_HCITYPE_ANY; i++);
memcpy(&sc->sc_bth4hci, &bth4hci[i], sizeof(struct bth4hci));
/* Attach Bluetooth unit */
sc->sc_unit.hci_softc = sc;
sc->sc_unit.hci_devname = device_xname(self);
sc->sc_unit.hci_enable = bth4_enable;
sc->sc_unit.hci_disable = bth4_disable;
sc->sc_unit.hci_start_cmd = bth4_start;
sc->sc_unit.hci_start_acl = bth4_start;
sc->sc_unit.hci_start_sco = bth4_start;
sc->sc_unit.hci_ipl = makeiplcookie(IPL_TTY);
hci_attach(&sc->sc_unit);
}
/*
* Autoconf detach routine. Called when we close the line discipline.
*/
static int
btuart_detach(device_t self, int flags __unused)
{
struct btuart_softc *sc = device_private(self);
hci_detach(&sc->sc_unit);
return 0;
}
static int
bth4_waitresp(struct btuart_softc *sc, struct mbuf **mp, uint16_t opcode)
{
struct hci_unit *unit = &sc->sc_unit;
hci_event_hdr_t *e;
int status = 0, rv;
*mp = NULL;
while (1 /* CONSTCOND */) {
if ((rv =
tsleep(&unit->hci_eventq, PCATCH, "bth4init", 0)) != 0)
return rv;
MBUFQ_DEQUEUE(&unit->hci_eventq, *mp);
unit->hci_eventqlen--;
KASSERT(*mp != NULL);
e = mtod(*mp, hci_event_hdr_t *);
if (e->event == HCI_EVENT_COMMAND_COMPL) {
hci_command_compl_ep *ep;
ep = (hci_command_compl_ep *)(e + 1);
if (ep->opcode == opcode) {
status = *(char *)(ep + 1);
break;
}
} else if (e->event == HCI_EVENT_COMMAND_STATUS) {
hci_command_status_ep *ep;
ep = (hci_command_status_ep *)(e + 1);
if (ep->opcode == opcode) {
status = ep->status;
break;
}
} else if (e->event == HCI_EVENT_VENDOR)
break;
}
return status;
}
static int
bth4_firmload(struct btuart_softc *sc, char *filename,
int (*func_firmload)(struct btuart_softc *, int, char *))
{
const cfdriver_t cd = device_cfdriver(sc->sc_dev);
firmware_handle_t fh = NULL;
int error, size;
char *buf;
if ((error = firmware_open(cd->cd_name, filename, &fh)) != 0) {
printf("firmware_open failed\n");
return error;
}
size = firmware_get_size(fh);
if ((buf = firmware_malloc(size)) != NULL) {
printf("firmware_malloc failed\n");
firmware_close(fh);
return ENOMEM;
}
if ((error = firmware_read(fh, 0, buf, size)) != 0)
printf("firmware_read failed\n");
if (error == 0)
error = (*func_firmload)(sc, size, buf);
firmware_close(fh);
firmware_free(buf, size);
return error;
}
/*
* LSI initialize functions.
*/
static int
init_ericsson(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
int i, error = 0;
const uint16_t opcode = htole16(HCI_CMD_ERICSSON_SET_UART_BAUD_RATE);
static struct {
int baud;
uint8_t param;
} ericsson_baudtbl[] = {
{ B460800, 0x00 },
{ B230400, 0x01 },
{ B115200, 0x02 },
{ B57600, 0x03 },
{ B28800, 0x04 },
{ B14400, 0x05 },
{ B7200, 0x06 },
#if defined(B3600)
{ B3600, 0x07 },
#endif
{ B1800, 0x08 },
#if defined(B900)
{ B900, 0x09 },
#endif
#if defined(B153600)
{ B153600, 0x10 },
#endif
{ B76800, 0x11 },
{ B38400, 0x12 },
{ B19200, 0x13 },
{ B9600, 0x14 },
{ B4800, 0x15 },
{ B2400, 0x16 },
{ B1200, 0x17 },
{ B600, 0x18 },
{ B300, 0x19 },
{ B921600, 0x20 },
{ B0, 0xff }
};
for (i = 0; ericsson_baudtbl[i].baud != sc->sc_baud; i++)
if (ericsson_baudtbl[i].baud == B0)
return EINVAL;
m = m_gethdr(M_WAIT, MT_DATA);
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = sizeof(ericsson_baudtbl[0].param);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length,
&ericsson_baudtbl[i].param);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
#if 0
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: Ericsson_Set_UART_Baud_Rate failed:"
" Status 0x%02x\n", device_xname(sc->sc_dev), error);
error = EFAULT;
}
m_freem(m);
}
#else
/*
* XXXX: We cannot correctly receive this response perhaps. Wait
* until the transmission of the data of 5 bytes * 10 bit is completed.
* 1000000usec * 10bit * 5byte / baud
*/
delay(50000000 / sc->sc_bth4hci.init_baud);
#endif
return error;
}
static int
init_digi(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
uint8_t param;
/* XXXX */
switch (sc->sc_baud) {
case B57600:
param = 0x08;
break;
case B115200:
param = 0x09;
break;
default:
return EINVAL;
}
m = m_gethdr(M_WAIT, MT_DATA);
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
#define HCI_CMD_DIGIANSWER_SET_UART_BAUD_RATE 0xfc07 /* XXXX */
p->opcode = htole16(HCI_CMD_DIGIANSWER_SET_UART_BAUD_RATE);
p->length = sizeof(param);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, ¶m);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
/*
* XXXX
* Wait until the transmission of the data of 5 bytes * 10 bit is
* completed.
* 1000000usec * 10bit * 5byte / baud
*/
delay(50000000 / sc->sc_bth4hci.init_baud);
return 0;
}
static int
init_texas(struct btuart_softc *sc)
{
/* XXXX: Should we obtain the version of LMP? */
return 0;
}
static int
init_csr(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
int error;
const uint16_t opcode = htole16(HCI_CMD_CSR_EXTN);
struct {
uint8_t last :1;
uint8_t first :1;
#define CSR_BCCMD_CHANID_BCCMD 2
#define CSR_BCCMD_CHANID_HQ 3
#define CSR_BCCMD_CHANID_DEVMGRLIB 4
#define CSR_BCCMD_CHANID_L2CAPLIB 8
#define CSR_BCCMD_CHANID_RFCOMMLIB 9
#define CSR_BCCMD_CHANID_SDPLIB 10
#define CSR_BCCMD_CHANID_DFU 12
#define CSR_BCCMD_CHANID_VM 13
#define CSR_BCCMD_CHANID_LMDEBUG 20
uint8_t chanid :6;
struct {
#define CSR_BCCMD_MESSAGE_TYPE_GETREQ 0x0000
#define CSR_BCCMD_MESSAGE_TYPE_GETRESP 0x0001
#define CSR_BCCMD_MESSAGE_TYPE_SETREQ 0x0002
uint16_t type;
uint16_t length;
uint16_t seqno;
#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART 0x6802
#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_STOPB 0x2000
#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARENB 0x4000
#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARODD 0x8000
uint16_t varid;
#define CSR_BCCMD_MESSAGE_STATUS_OK 0x0000
#define CSR_BCCMD_MESSAGE_STATUS_NO_SUCH_VARID 0x0001
#define CSR_BCCMD_MESSAGE_STATUS_TOO_BIG 0x0002
#define CSR_BCCMD_MESSAGE_STATUS_NO_VALUE 0x0003
#define CSR_BCCMD_MESSAGE_STATUS_BAD_REQ 0x0004
#define CSR_BCCMD_MESSAGE_STATUS_NO_ACCESS 0x0005
#define CSR_BCCMD_MESSAGE_STATUS_READ_ONLY 0x0006
#define CSR_BCCMD_MESSAGE_STATUS_WRITE_ONLY 0x0007
#define CSR_BCCMD_MESSAGE_STATUS_ERROR 0x0008
#define CSR_BCCMD_MESSAGE_STATUS_PERMISION_DENIED 0x0009
uint16_t status;
uint16_t payload[4];
} message;
} bccmd;
m = m_gethdr(M_WAIT, MT_DATA);
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = sizeof(bccmd);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
/* setup BCSP command packet */
bccmd.last = 1;
bccmd.first = 1;
bccmd.chanid = CSR_BCCMD_CHANID_BCCMD;
bccmd.message.type = htole16(CSR_BCCMD_MESSAGE_TYPE_SETREQ);
bccmd.message.length = htole16(sizeof(bccmd.message) >> 1);
bccmd.message.seqno = htole16(0);
bccmd.message.varid = htole16(CSR_BCCMD_MESSAGE_VARID_CONFIG_UART);
bccmd.message.status = htole16(CSR_BCCMD_MESSAGE_STATUS_OK);
memset(bccmd.message.payload, 0, sizeof(bccmd.message.payload));
/* Value = (baud rate / 244.140625) | no parity | 1 stop bit. */
bccmd.message.payload[0] = htole16((sc->sc_baud * 64 + 7812) / 15625);
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &bccmd);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
/*
* XXXX:
* We will have to check the HCI_EVENT_VENDOR packet. For
* instance, it might be a different HCI_EVENT_VENDOR packet.
*/
if (error != 0) {
printf("%s: CSR set UART speed failed: Status 0x%02x\n",
device_xname(sc->sc_dev), error);
error = EFAULT;
}
m_freem(m);
}
return error;
}
static int
init_swave(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
hci_event_hdr_t *e;
int i, error;
#define HCI_CMD_SWAVE_SET_UART_BAUD_RATE 0xfc0b /* XXXX */
const uint16_t opcode = htole16(HCI_CMD_SWAVE_SET_UART_BAUD_RATE);
char param[6], *resp;
static struct { /* XXXX */
int baud;
uint8_t param;
} swave_baudtbl[] = {
{ B19200, 0x03 },
{ B38400, 0x02 },
{ B57600, 0x01 },
{ B115200, 0x00 },
{ B0, 0xff }
};
for (i = 0; swave_baudtbl[i].baud != sc->sc_baud; i++)
if (swave_baudtbl[i].baud == B0)
return EINVAL;
m = m_gethdr(M_WAIT, MT_DATA);
/* first send 'param access set' command. */
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = sizeof(param);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
/* XXXX */
param[0] = 0x01; /* param sub command */
param[1] = 0x11; /* HCI Tranport Params */
param[2] = 0x03; /* length of the parameter following */
param[3] = 0x01; /* HCI Transport flow control enable */
param[4] = 0x01; /* HCI Transport Type = UART */
param[5] = swave_baudtbl[i].param;
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, ¶m);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
while(1 /* CONSTCOND */) {
error = bth4_waitresp(sc, &m, opcode);
if (error != 0) {
if (m != NULL)
m_freem(m);
printf("%s: swave set baud rate command failed:"
" error 0x%02x\n", device_xname(sc->sc_dev), error);
return error;
}
if (m != NULL) {
e = mtod(m, hci_event_hdr_t *);
resp = (char *)(e + 1);
if (e->length == 7 && *resp == 0xb &&
memcmp(resp + 1, param, sizeof(param)) == 0)
break;
m_freem(m);
}
}
/* send 'reset' command consecutively. */
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = htole16(HCI_CMD_RESET);
p->length = 0;
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
/*
* XXXX
* Wait until the transmission of the data of 4 bytes * 10 bit is
* completed.
* 1000000usec * 10bit * 4byte / baud
*/
delay(40000000 / sc->sc_bth4hci.init_baud);
return 0;
}
static int
init_st(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
int i;
static struct { /* XXXX */
int baud;
uint8_t param;
} st_baudtbl[] = {
{ B9600, 0x09 },
{ B19200, 0x0b },
{ B38400, 0x0d },
{ B57600, 0x0e },
{ B115200, 0x10 },
{ B230400, 0x12 },
{ B460800, 0x13 },
{ B921600, 0x14 },
{ B0, 0xff }
};
for (i = 0; st_baudtbl[i].baud != sc->sc_baud; i++)
if (st_baudtbl[i].baud == B0)
return EINVAL;
m = m_gethdr(M_WAIT, MT_DATA);
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
#define HCI_CMD_ST_SET_UART_BAUD_RATE 0xfc46 /* XXXX */
p->opcode = htole16(HCI_CMD_ST_SET_UART_BAUD_RATE);
p->length = sizeof(st_baudtbl[0].param);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &st_baudtbl[i].param);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
/*
* XXXX
* Wait until the transmission of the data of 5 bytes * 10 bit is
* completed.
* 1000000usec * 10bit * 5byte / baud
*/
delay(50000000 / sc->sc_bth4hci.init_baud);
return 0;
}
static int
firmload_stlc2500(struct btuart_softc *sc, int size, char *buf)
{
struct hci_unit *unit = &sc->sc_unit;
struct mbuf *m;
hci_cmd_hdr_t *p;
int error, offset, n;
uint16_t opcode = htole16(0xfc2e); /* XXXX */
uint8_t seq;
m = m_gethdr(M_WAIT, MT_DATA);
seq = 0;
offset = 0;
error = 0;
while (offset < size) {
n = size - offset < 254 ? size - offset : 254;
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = n;
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
*(char *)(p + 1) = seq;
m_copyback(m,
sizeof(hci_cmd_hdr_t) + 1, p->length, buf + offset);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: stlc2500 firmware load failed:"
" Status 0x%02x\n",
device_xname(sc->sc_dev), error);
error = EFAULT;
break;
}
}
seq++;
offset += n;
}
m_freem(m);
return error;
}
static int
init_stlc2500(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
hci_event_hdr_t *e;
hci_read_local_ver_rp *lv;
int error, revision, i;
uint16_t opcode;
char filename[NAME_MAX], param[8];
static const char filenametmpl[] = "STLC2500_R%d_%02d%s";
const char *suffix[] = { ".ptc", ".ssf", NULL };
m = m_gethdr(M_WAIT, MT_DATA);
p = mtod(m, hci_cmd_hdr_t *);
opcode = htole16(HCI_CMD_READ_LOCAL_VER);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = 0;
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: HCI_Read_Local_Version_Information failed:"
" Status 0x%02x\n", device_xname(sc->sc_dev), error);
error = EFAULT;
m_freem(m);
}
}
if (error != 0)
return error;
e = mtod(m, hci_event_hdr_t *);
lv = (hci_read_local_ver_rp *)(e + 1);
revision = le16toh(lv->hci_revision);
opcode = htole16(HCI_CMD_RESET);
for (i = 0; suffix[i] != NULL; i++) {
/* send firmware */
snprintf(filename, sizeof(filename), filenametmpl,
(uint8_t)(revision >> 8), (uint8_t)revision, suffix[i]);
bth4_firmload(sc, filename, firmload_stlc2500);
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = 0;
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: HCI_Reset (%d) failed:"
" Status 0x%02x\n",
device_xname(sc->sc_dev), i, error);
error = EFAULT;
m_freem(m);
}
}
if (error != 0)
return error;
}
/* XXXX: We will obtain the character string. But I don't know... */
p = mtod(m, hci_cmd_hdr_t *);
opcode = htole16(0xfc0f); /* XXXXX ?? */
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = 0;
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: failed: opcode 0xfc0f Status 0x%02x\n",
device_xname(sc->sc_dev), error);
error = EFAULT;
m_freem(m);
}
}
if (error != 0)
return error;
/*
* XXXX:
* We do not know the beginning point of this character string.
* Because it doesn't know the event of this packet.
*
* printf("%s: %s\n", device_xname(sc->sc_dev), ???);
*/
p = mtod(m, hci_cmd_hdr_t *);
opcode = htole16(0xfc22); /* XXXXX ?? */
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = sizeof(param);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
/* XXXX */
param[0] = 0xfe;
param[1] = 0x06;
param[2] = 0xba;
param[3] = 0xab;
param[4] = 0x00;
param[5] = 0xe1;
param[6] = 0x80;
param[7] = 0x00;
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, param);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: failed: opcode 0xfc0f Status 0x%02x\n",
device_xname(sc->sc_dev), error);
error = EFAULT;
m_freem(m);
}
}
if (error != 0)
return error;
opcode = htole16(HCI_CMD_RESET);
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = 0;
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: HCI_Reset failed: Status 0x%02x\n",
device_xname(sc->sc_dev), error);
error = EFAULT;
m_freem(m);
}
}
return error;
}
static int
init_bcm2035(struct btuart_softc *sc)
{
struct mbuf *m;
struct hci_unit *unit = &sc->sc_unit;
hci_cmd_hdr_t *p;
int i, error;
#define HCI_CMD_BCM2035_SET_UART_BAUD_RATE 0xfc18 /* XXXX */
const uint16_t opcode = htole16(HCI_CMD_BCM2035_SET_UART_BAUD_RATE);
static struct { /* XXXX */
int baud;
uint16_t param;
} bcm2035_baudtbl[] = {
{ B57600, 0xe600 },
{ B230400, 0xfa22 },
{ B460800, 0xfd11 },
{ B921600, 0xff65 },
{ B0, 0xffff }
};
for (i = 0; bcm2035_baudtbl[i].baud != sc->sc_baud; i++)
if (bcm2035_baudtbl[i].baud == -1)
return EINVAL;
m = m_gethdr(M_WAIT, MT_DATA);
/*
* XXXX: Should we send some commands?
* HCI_CMD_RESET and HCI_CMD_READ_LOCAL_VER and
* HCI_CMD_READ_LOCAL_COMMANDS
*/
p = mtod(m, hci_cmd_hdr_t *);
p->type = HCI_CMD_PKT;
p->opcode = opcode;
p->length = sizeof(bcm2035_baudtbl[0].param);
m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
m_copyback(m, sizeof(hci_cmd_hdr_t), p->length,
&bcm2035_baudtbl[i].param);
MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
bth4_start(unit);
error = bth4_waitresp(sc, &m, opcode);
if (m != NULL) {
if (error != 0) {
printf("%s: bcm2035 set baud rate failed:"
" Status 0x%02x\n", device_xname(sc->sc_dev), error);
error = EFAULT;
}
m_freem(m);
}
return error;
}
static int
bth4init(struct btuart_softc *sc)
{
struct tty *tp = sc->sc_tp;
struct termios t;
int error = 0, s;
sc->sc_baud = tp->t_ospeed;
t.c_cflag = tp->t_cflag;
t.c_ispeed = 0;
t.c_ospeed = tp->t_ospeed;
if ((tp->t_cflag & CRTSCTS) && !(sc->sc_bth4hci.flags & FLOW_CTL))
t.c_cflag &= ~CRTSCTS;
if (sc->sc_bth4hci.init_baud != 0 &&
tp->t_ospeed != sc->sc_bth4hci.init_baud)
t.c_ospeed = sc->sc_bth4hci.init_baud;
if (t.c_ospeed != tp->t_ospeed || t.c_cflag != tp->t_cflag)
error = (*tp->t_param)(tp, &t);
if (error == 0 && sc->sc_bth4hci.init != NULL)
error = (*sc->sc_bth4hci.init)(sc);
s = splserial();
sc->sc_input_acl = hci_input_acl;
sc->sc_input_sco = hci_input_sco;
sc->sc_input_event = hci_input_event;
splx(s);
if (sc->sc_bth4hci.init_baud != 0 &&
sc->sc_bth4hci.init_baud != sc->sc_baud) {
t.c_ospeed = sc->sc_baud;
t.c_cflag = tp->t_cflag;
error = (*tp->t_param)(tp, &t);
}
return error;
}
static void
bth4init_input(struct hci_unit *unit, struct mbuf *m)
{
int i;
uint8_t *rptr = mtod(m, uint8_t *);
const char *pktstr = NULL;
switch (*rptr) {
case HCI_ACL_DATA_PKT:
pktstr = "acl data";
break;
case HCI_SCO_DATA_PKT:
pktstr = "sco data";
break;
case HCI_EVENT_PKT:
break;
default:
pktstr = "unknown";
break;
}
if (pktstr != NULL)
printf("%s: %s packet was received in initialization phase\n",
unit->hci_devname, pktstr);
if (
#ifdef BTUART_DEBUG
btuart_debug ||
#endif
pktstr != NULL) {
printf("%s: %s:", __FUNCTION__, unit->hci_devname);
for (i = 0; i < m->m_len; i++)
printf(" %02x", *(rptr + i));
printf("\n");
}
if (*rptr == HCI_EVENT_PKT)
if (unit->hci_eventqlen <= hci_eventq_max) {
unit->hci_eventqlen++;
MBUFQ_ENQUEUE(&unit->hci_eventq, m);
m = NULL;
wakeup(&unit->hci_eventq);
}
if (m != NULL)
m_freem(m);
}
/*
* Line discipline functions.
*/
/* ARGSUSED */
static int
bth4open(dev_t device __unused, struct tty *tp)
{
struct btuart_softc *sc;
struct cfdata *cfdata;
struct lwp *l = curlwp; /* XXX */
int error, unit, s;
static char name[] = "btuart";
if ((error = kauth_authorize_device_tty(l->l_cred,
KAUTH_GENERIC_ISSUSER, tp)) != 0)
return error;
s = spltty();
if (tp->t_linesw == &bth4_disc) {
sc = (struct btuart_softc *)tp->t_sc;
if (sc != NULL) {
splx(s);
return EBUSY;
}
}
KASSERT(tp->t_oproc != NULL);
cfdata = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
for (unit = 0; unit < btuart_cd.cd_ndevs; unit++)
if (btuart_cd.cd_devs[unit] == NULL)
break;
cfdata->cf_name = name;
cfdata->cf_atname = name;
cfdata->cf_unit = unit;
cfdata->cf_fstate = FSTATE_STAR;
printf("%s%d at tty major %d minor %d",
name, unit, major(tp->t_dev), minor(tp->t_dev));
sc = (struct btuart_softc *)config_attach_pseudo(cfdata);
if (sc == NULL) {
splx(s);
return EIO;
}
tp->t_sc = sc;
sc->sc_tp = tp;
ttyflush(tp, FREAD | FWRITE);
splx(s);
return 0;
}
/* ARGSUSED */
static int
bth4close(struct tty *tp, int flag __unused)
{
struct btuart_softc *sc;
struct cfdata *cfdata;
int s, baud;
sc = tp->t_sc;
/* reset to initial speed */
if (sc->sc_bth4hci.init != NULL) {
baud = sc->sc_baud;
sc->sc_baud = sc->sc_bth4hci.init_baud;
sc->sc_bth4hci.init_baud = baud;
s = splserial();
sc->sc_input_acl = bth4init_input;
sc->sc_input_sco = bth4init_input;
sc->sc_input_event = bth4init_input;
splx(s);
if ((*sc->sc_bth4hci.init)(sc) != 0)
printf("%s: reset speed fail\n", device_xname(sc->sc_dev));
}
s = spltty();
ttyflush(tp, FREAD | FWRITE);
ttyldisc_release(tp->t_linesw);
tp->t_linesw = ttyldisc_default();
if (sc != NULL) {
tp->t_sc = NULL;
if (sc->sc_tp == tp) {
cfdata = device_cfdata(sc->sc_dev);
config_detach(sc->sc_dev, 0);
free(cfdata, M_DEVBUF);
}
}
splx(s);
return 0;
}
/* ARGSUSED */
static int
bth4ioctl(struct tty *tp, u_long cmd, void *data,
int flag __unused, struct lwp *l __unused)
{
struct btuart_softc *sc = (struct btuart_softc *)tp->t_sc;
int error, i;
if (sc == NULL || tp != sc->sc_tp)
return EPASSTHROUGH;
error = 0;
switch (cmd) {
case BTUART_HCITYPE:
for (i = 0; bth4hci[i].type != -1; i++)
if (bth4hci[i].type == *(uint32_t *)data)
break;
if (bth4hci[i].type != -1)
memcpy(&sc->sc_bth4hci, &bth4hci[i],
sizeof(struct bth4hci));
else
error = EINVAL;
break;
case BTUART_INITSPEED:
sc->sc_bth4hci.init_baud = *(uint32_t *)data;
break;
case BTUART_START:
error = bth4init(sc);
break;
default:
error = EPASSTHROUGH;
break;
}
return error;
}
static int
bth4input(int c, struct tty *tp)
{
struct btuart_softc *sc = (struct btuart_softc *)tp->t_sc;
struct mbuf *m = sc->sc_rxp;
int space = 0;
c &= TTY_CHARMASK;
/* If we already started a packet, find the trailing end of it. */
if (m) {
while (m->m_next)
m = m->m_next;
space = M_TRAILINGSPACE(m);
}
if (space == 0) {
if (m == NULL) {
/* new packet */
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
printf("%s: out of memory\n",
device_xname(sc->sc_dev));
++sc->sc_unit.hci_stats.err_rx;
return 0; /* (lost sync) */
}
sc->sc_rxp = m;
m->m_pkthdr.len = m->m_len = 0;
space = MHLEN;
sc->sc_state = BTUART_RECV_PKT_TYPE;
sc->sc_want = 1;
} else {
/* extend mbuf */
MGET(m->m_next, M_DONTWAIT, MT_DATA);
if (m->m_next == NULL) {
printf("%s: out of memory\n",
device_xname(sc->sc_dev));
++sc->sc_unit.hci_stats.err_rx;
return 0; /* (lost sync) */
}
m = m->m_next;
m->m_len = 0;
space = MLEN;
if (sc->sc_want > MINCLSIZE) {
MCLGET(m, M_DONTWAIT);
if (m->m_flags & M_EXT)
space = MCLBYTES;
}
}
}
mtod(m, uint8_t *)[m->m_len++] = c;
sc->sc_rxp->m_pkthdr.len++;
sc->sc_unit.hci_stats.byte_rx++;
sc->sc_want--;
if (sc->sc_want > 0)
return 0; /* want more */
switch (sc->sc_state) {
case BTUART_RECV_PKT_TYPE: /* Got packet type */
switch (c) {
case HCI_ACL_DATA_PKT:
sc->sc_state = BTUART_RECV_ACL_HDR;
sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
break;
case HCI_SCO_DATA_PKT:
sc->sc_state = BTUART_RECV_SCO_HDR;
sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
break;
case HCI_EVENT_PKT:
sc->sc_state = BTUART_RECV_EVENT_HDR;
sc->sc_want = sizeof(hci_event_hdr_t) - 1;
break;
default:
printf("%s: Unknown packet type=%#x!\n",
device_xname(sc->sc_dev), c);
sc->sc_unit.hci_stats.err_rx++;
m_freem(sc->sc_rxp);
sc->sc_rxp = NULL;
return 0; /* (lost sync) */
}
break;
/*
* we assume (correctly of course :) that the packet headers all fit
* into a single pkthdr mbuf
*/
case BTUART_RECV_ACL_HDR: /* Got ACL Header */
sc->sc_state = BTUART_RECV_ACL_DATA;
sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
sc->sc_want = le16toh(sc->sc_want);
break;
case BTUART_RECV_SCO_HDR: /* Got SCO Header */
sc->sc_state = BTUART_RECV_SCO_DATA;
sc->sc_want = mtod(m, hci_scodata_hdr_t *)->length;
break;
case BTUART_RECV_EVENT_HDR: /* Got Event Header */
sc->sc_state = BTUART_RECV_EVENT_DATA;
sc->sc_want = mtod(m, hci_event_hdr_t *)->length;
break;
case BTUART_RECV_ACL_DATA: /* ACL Packet Complete */
(*sc->sc_input_acl)(&sc->sc_unit, sc->sc_rxp);
sc->sc_unit.hci_stats.acl_rx++;
sc->sc_rxp = m = NULL;
break;
case BTUART_RECV_SCO_DATA: /* SCO Packet Complete */
(*sc->sc_input_sco)(&sc->sc_unit, sc->sc_rxp);
sc->sc_unit.hci_stats.sco_rx++;
sc->sc_rxp = m = NULL;
break;
case BTUART_RECV_EVENT_DATA: /* Event Packet Complete */
sc->sc_unit.hci_stats.evt_rx++;
(*sc->sc_input_event)(&sc->sc_unit, sc->sc_rxp);
sc->sc_rxp = m = NULL;
break;
default:
panic("%s: invalid state %d!\n",
device_xname(sc->sc_dev), sc->sc_state);
}
return 0;
}
static int
bth4start(struct tty *tp)
{
struct btuart_softc *sc = (struct btuart_softc *)tp->t_sc;
struct mbuf *m;
int count, rlen;
uint8_t *rptr;
m = sc->sc_txp;
if (m == NULL) {
sc->sc_unit.hci_flags &= ~BTF_XMIT;
bth4_start(&sc->sc_unit);
return 0;
}
count = 0;
rlen = 0;
rptr = mtod(m, uint8_t *);
for(;;) {
if (rlen >= m->m_len) {
m = m->m_next;
if (m == NULL) {
m = sc->sc_txp;
sc->sc_txp = NULL;
if (M_GETCTX(m, void *) == NULL)
m_freem(m);
else
hci_complete_sco(&sc->sc_unit, m);
break;
}
rlen = 0;
rptr = mtod(m, uint8_t *);
continue;
}
if (putc(*rptr++, &tp->t_outq) < 0) {
m_adj(m, rlen);
break;
}
rlen++;
count++;
}
sc->sc_unit.hci_stats.byte_tx += count;
if (tp->t_outq.c_cc != 0)
(*tp->t_oproc)(tp);
return 0;
}
/*
* HCI UART (H4) functions.
*/
static int
bth4_enable(struct hci_unit *unit)
{
if (unit->hci_flags & BTF_RUNNING)
return 0;
unit->hci_flags |= BTF_RUNNING;
unit->hci_flags &= ~BTF_XMIT;
return 0;
}
static void
bth4_disable(struct hci_unit *unit)
{
struct btuart_softc *sc = unit->hci_softc;
if ((unit->hci_flags & BTF_RUNNING) == 0)
return;
if (sc->sc_rxp) {
m_freem(sc->sc_rxp);
sc->sc_rxp = NULL;
}
if (sc->sc_txp) {
m_freem(sc->sc_txp);
sc->sc_txp = NULL;
}
unit->hci_flags &= ~BTF_RUNNING;
}
static void
bth4_start(struct hci_unit *unit)
{
struct btuart_softc *sc = unit->hci_softc;
struct mbuf *m;
KASSERT((unit->hci_flags & BTF_XMIT) == 0);
KASSERT(sc->sc_txp == NULL);
if (MBUFQ_FIRST(&unit->hci_cmdq)) {
MBUFQ_DEQUEUE(&unit->hci_cmdq, m);
unit->hci_stats.cmd_tx++;
M_SETCTX(m, NULL);
goto start;
}
if (MBUFQ_FIRST(&unit->hci_scotxq)) {
MBUFQ_DEQUEUE(&unit->hci_scotxq, m);
unit->hci_stats.sco_tx++;
goto start;
}
if (MBUFQ_FIRST(&unit->hci_acltxq)) {
MBUFQ_DEQUEUE(&unit->hci_acltxq, m);
unit->hci_stats.acl_tx++;
M_SETCTX(m, NULL);
goto start;
}
/* Nothing to send */
return;
start:
sc->sc_txp = m;
unit->hci_flags |= BTF_XMIT;
bth4start(sc->sc_tp);
}
|