aboutsummaryrefslogtreecommitdiffstats
path: root/src/datenklo/datenklo.c
blob: 12d7ce45c73b6d36bff24a556fe32bed19256570 (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
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
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
/* osmo datenklo, the "datenklo" emulator
 *
 * (C) 2019 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <asm-generic/termbits.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <poll.h>
#include <pthread.h>
#include <ctype.h>
#include <fcntl.h>
#include "../libsample/sample.h"
#include "../libtimer/timer.h"
#include "../libfsk/fsk.h"
#include "../libsound/sound.h"
#include "../libwave/wave.h"
#include "../libdisplay/display.h"
#include "../libdebug/debug.h"
#include "device.h"
#include "am791x.h"
#include "uart.h"
#include "datenklo.h"

/* Put the state of FD into *TERMIOS_P.  */
extern int tcgetattr (int __fd, struct termios *__termios_p) __THROW;

/* Set the state of FD to *TERMIOS_P.
   Values for OPTIONAL_ACTIONS (TCSA*) are in <bits/termios.h>.  */
extern int tcsetattr (int __fd, int __optional_actions,
                      const struct termios *__termios_p) __THROW;

static int quit = 0;

pthread_mutex_t mutex;

static tcflag_t baud2cflag(double _baudrate)
{
	tcflag_t cflag;
	int baudrate = (int)_baudrate;

	switch (baudrate) {
	case 0:
		cflag = B0;
		break;
	case 50:
		cflag = B50;
		break;
	case 75:
		cflag = B75;
		break;
	case 110:
		cflag = B110;
		break;
	case 134:
		cflag = B134;
		break;
	case 150:
		cflag = B150;
		break;
	case 200:
		cflag = B200;
		break;
	case 300:
		cflag = B300;
		break;
	case 600:
		cflag = B600;
		break;
	default:
		cflag = B1200;
	}

	return cflag;
}

static double cflag2baud(tcflag_t cflag)
{
	double baudrate;

	switch ((cflag & CBAUD)) {
	case B0:
		baudrate = 0;
		break;
	case B50:
		baudrate = 50;
		break;
	case B75:
		baudrate = 75;
		break;
	case B110:
		baudrate = 110;
		break;
	case B134:
		baudrate = 134.5;
		break;
	case B150:
		baudrate = 150;
		break;
	case B200:
		baudrate = 200;
		break;
	case B300:
		baudrate = 300;
		break;
	case B600:
		baudrate = 600;
		break;
	default:
		baudrate = 1200;
	}

	return baudrate;
}

static int cflag2databits(tcflag_t cflag)
{
	int databits;

	switch ((cflag & CSIZE)) {
	case CS5:
		databits = 5;
		break;
	case CS6:
		databits = 6;
		break;
	case CS7:
		databits = 7;
		break;
	default:
		databits = 8;
	}

	return databits;
}

static enum uart_parity cflag2parity(tcflag_t cflag)
{
	enum uart_parity parity;

	if (!(cflag & PARENB))
		parity = UART_PARITY_NONE;
	else
	if (!(cflag & PARODD)) {
		if (!(cflag & CMSPAR))
			parity = UART_PARITY_EVEN;
		else
			parity = UART_PARITY_SPACE;
	} else {
		if (!(cflag & CMSPAR))
			parity = UART_PARITY_ODD;
		else
			parity = UART_PARITY_MARK;
	}

	return parity;
}

static char parity2char(enum uart_parity parity)
{
	switch (parity) {
	case UART_PARITY_NONE:
		return 'N';
	case UART_PARITY_EVEN:
		return 'E';
	case UART_PARITY_ODD:
		return 'O';
	case UART_PARITY_MARK:
		return 'M';
	case UART_PARITY_SPACE:
		return 'S';
	}
	return ' ';
}

static int cflag2stopbits(tcflag_t cflag)
{
	int stopbits;

	if ((cflag & CSTOPB))
		stopbits = 2;
	else
		stopbits = 1;

	return stopbits;
}

/* modem changes CTS state */
static void cts(void *inst, int cts)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (datenklo->tx_back)
		return;

	if (datenklo->auto_rts) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Received CTS=%d in Automatic RTS Mode.\n", cts);
		datenklo->auto_rts_cts = cts;
		return;
	}

	if (cts)
		datenklo->lines |= TIOCM_CTS;
	else
		datenklo->lines &= ~TIOCM_CTS;
	PDEBUG(DDATENKLO, DEBUG_INFO, "Indicating to terminal that CTS is %s\n", (cts) ? "on" : "off");
}

/* modem changes CTS state (back channel) */
static void bcts(void *inst, int cts)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (!datenklo->tx_back)
		return;

	if (datenklo->auto_rts) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Received BCTS=%d in Automatic RTS Mode.\n", cts);
		datenklo->auto_rts_cts = cts;
		return;
	}

	if (cts)
		datenklo->lines |= TIOCM_CTS;
	else
		datenklo->lines &= ~TIOCM_CTS;
	PDEBUG(DDATENKLO, DEBUG_INFO, "Indicating to terminal that BCTS is %s\n", (cts) ? "on" : "off");
}

/* modem changes CD state */
static void cd(void *inst, int cd)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (datenklo->rx_back)
		return;

	if (datenklo->auto_rts) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Received CD=%d in Automatic RTS Mode.\n", cd);
		datenklo->auto_rts_cd = cd;
		return;
	}

	if (cd)
		datenklo->lines |= TIOCM_CD;
	else
		datenklo->lines &= ~TIOCM_CD;
	PDEBUG(DDATENKLO, DEBUG_INFO, "Indicating to terminal that CD is %s\n", (cd) ? "on" : "off");
}

/* modem changes CD state (back channel) */
static void bcd(void *inst, int cd)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (!datenklo->rx_back)
		return;

	if (datenklo->auto_rts) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Received BCD=%d in Automatic RTS Mode.\n", cd);
		datenklo->auto_rts_cd = cd;
		return;
	}

	if (cd)
		datenklo->lines |= TIOCM_CD;
	else
		datenklo->lines &= ~TIOCM_CD;
	PDEBUG(DDATENKLO, DEBUG_INFO, "Indicating to terminal that BCD is %s\n", (cd) ? "on" : "off");
}

/* modem request bit */
static int td(void *inst)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (datenklo->tx_back)
		return 1;

	if (!uart_is_tx(&datenklo->uart)) {
		if (datenklo->break_bits) {
			--datenklo->break_bits;
			return 0;
		}
		if (datenklo->break_on) {
			return 0;
		}
	}

	return uart_tx_bit(&datenklo->uart);
}

/* modem request bit (back channel) */
static int btd(void *inst)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (!datenklo->tx_back)
		return 1;

	if (!uart_is_tx(&datenklo->uart)) {
		if (datenklo->break_bits) {
			--datenklo->break_bits;
			return 0;
		}
		if (datenklo->break_on) {
			return 0;
		}
	}

	return uart_tx_bit(&datenklo->uart);
}

/* modem received bit */
static void rd(void *inst, int bit, double quality, double level)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (datenklo->rx_back)
		return;

	/* only show level+quality when bit has not changed */
	if (datenklo->last_bit == bit) {
		display_measurements_update(datenklo->dmp_level, level, 0.0);
		display_measurements_update(datenklo->dmp_quality, quality, 0.0);
	}
	datenklo->last_bit = bit;

	uart_rx_bit(&datenklo->uart, bit);
}

/* modem received bit (back channel) */
static void brd(void *inst, int bit, double quality, double level)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (!datenklo->rx_back)
		return;

	/* only show level+quality when bit has not changed */
	if (datenklo->last_bit == bit) {
		display_measurements_update(datenklo->dmp_level, level, 0.0);
		display_measurements_update(datenklo->dmp_quality, quality, 0.0);
	}
	datenklo->last_bit = bit;

	uart_rx_bit(&datenklo->uart, bit);
}

static void set_termios(datenklo_t *datenklo, const void *buf);

/* helper to flush tx buffer and all tx states */
static void flush_tx(datenklo_t *datenklo)
{
	datenklo->tx_fifo_out = datenklo->tx_fifo_in;
	datenklo->onlcr_char = 0;
}

/* helper to flush rx buffer */
static void flush_rx(datenklo_t *datenklo)
{
	datenklo->rx_fifo_out = datenklo->rx_fifo_in;
}

/* UART requests byte to transmit */
static int tx(void *inst)
{
	datenklo_t *datenklo = (datenklo_t *)inst;
	size_t fill;
	int data;

	if (datenklo->output_off)
		return -1;

	if (!(datenklo->lines & TIOCM_RTS) || !(datenklo->lines & TIOCM_CTS))
		return -1;

	if (datenklo->auto_rts && !datenklo->auto_rts_cts)
		return -1;

	/* nl -> cr+nl mode */
	if (datenklo->onlcr_char) {
		datenklo->onlcr_char = 0;
		data = '\n';
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "ONLCR: sending NL\n");
		goto out;
	}

again:
	fill = (datenklo->tx_fifo_in - datenklo->tx_fifo_out + datenklo->tx_fifo_size) % datenklo->tx_fifo_size;

	if (fill == (size_t)datenklo->tx_fifo_full) {
		/* tell cuse to write again */
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Set POLLOUT!\n");
		datenklo->revents |= POLLOUT;
		device_set_poll_events(datenklo->device, datenklo->revents);
	}

	if (!fill) {
		if (datenklo->auto_rts)
			datenklo->auto_rts_on = 0;

		if (datenklo->tcsetsw) {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Transmission finished, applying termios now.\n");
			memcpy(&datenklo->termios, &datenklo->tcsetsw_termios, sizeof(datenklo->termios));
			if (datenklo->tcsetsw == 2) {
				flush_rx(datenklo);
				if ((datenklo->revents & POLLIN)) {
					PDEBUG(DDATENKLO, DEBUG_DEBUG, "Reset POLLIN (flushed)\n");
					datenklo->revents &= ~POLLIN;
					device_set_poll_events(datenklo->device, datenklo->revents);
				}
			}
			datenklo->tcsetsw = 0;
			set_termios(datenklo,  &datenklo->tcsetsw_termios);
		}
		return -1;
	}

	data = datenklo->tx_fifo[datenklo->tx_fifo_out++];
	datenklo->tx_fifo_out %= datenklo->tx_fifo_size;
	fill--;

	/* in case of blocking: check if there is enough space to write */
	device_write_available(datenklo->device);

	/* process output features */
	if (datenklo->opost) {
		if (datenklo->olcuc) {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "OLCUC: 0x%02x -> 0x%02x\n", data, toupper(data));
			data = toupper(data);
		}
		if (datenklo->onlret && data == '\r') {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "ONLRET: ignore CR\n");
			goto again;
		}
		if (datenklo->ocrnl && data == '\r') {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "OCRNL: CR -> NL\n");
			data = '\n';
		}
		if (datenklo->onlcr && data == '\n') {
			datenklo->onlcr_char = 1;
			data = '\r';
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "ONLCR: sending CR\n");
		}
	}

out:
	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Transmitting byte 0x%02x to UART.\n", data);

	return data;
}

/* UART receives complete byte */
static void rx(void *inst, int data, uint32_t __attribute__((unused)) flags)
{
	datenklo_t *datenklo = (datenklo_t *)inst;
	size_t space;

	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Received byte 0x%02x ('%c') from UART.\n", data, (data >= 32 && data <= 126) ? data : '.');

	/* process input features */
	if (datenklo->ignbrk && (flags & UART_BREAK)) {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "IGNBRK: ignore BREAK\n");
		return;
	}
	if (datenklo->istrip && (data & 0x80)) {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "ISTRIP: 0x%02x -> 0x%02x\n", data, data & 0x7f);
		data &= 0x7f;
	}
	if (datenklo->inlcr && data == '\n') {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "INLCR: NL -> CR\n");
		data = '\r';
	}
	if (datenklo->igncr && data == '\r') {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "IGNCR: ignore CR\n");
		return;
	}
	if (datenklo->icrnl && data == '\r') {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "ICRNL: CR -> NL\n");
		data = '\n';
	}
	if (datenklo->iuclc) {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "IUCLC: 0x%02x -> 0x%02x\n", data, tolower(data));
		data = tolower(data);
	}
	if (datenklo->echo) {
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "ECHO: write to output\n");
		space = (datenklo->tx_fifo_out - datenklo->tx_fifo_in - 1 + datenklo->tx_fifo_size) % datenklo->tx_fifo_size;
		if (space) {
			datenklo->tx_fifo[datenklo->tx_fifo_in++] = data;
			datenklo->tx_fifo_in %= datenklo->tx_fifo_size;
		}
	}

	/* empty buffer gets data */
	if (datenklo->rx_fifo_out == datenklo->rx_fifo_in) {
		/* tell cuse to read again */
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Set POLLIN!\n");
		datenklo->revents |= POLLIN;
		device_set_poll_events(datenklo->device, datenklo->revents);
	}

	space = (datenklo->rx_fifo_out - datenklo->rx_fifo_in - 1 + datenklo->rx_fifo_size) % datenklo->rx_fifo_size;

	if (!space) {
		err_overflow:
		PDEBUG(DDATENKLO, DEBUG_NOTICE, "RX buffer overflow, dropping!\n");
		return;
	}

	if (datenklo->parmrk) {
		if ((flags & (UART_BREAK | UART_PARITY_ERROR))) {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "PARMRK: 0x%02x -> 0xff,0x00,0x%02x\n", data, data);
			if (space < 3)
				goto err_overflow;
			datenklo->rx_fifo[datenklo->rx_fifo_in++] = 0xff;
			datenklo->rx_fifo_in %= datenklo->rx_fifo_size;
			space--;
			datenklo->rx_fifo[datenklo->rx_fifo_in++] = 0x00;
			datenklo->rx_fifo_in %= datenklo->rx_fifo_size;
			space--;
		} else if (data == 0xff) {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "PARMRK: 0xff -> 0xff,0xff\n");
			if (space < 2)
				goto err_overflow;
			datenklo->rx_fifo[datenklo->rx_fifo_in++] = 0xff;
			datenklo->rx_fifo_in %= datenklo->rx_fifo_size;
			space--;
		}
	}

	datenklo->rx_fifo[datenklo->rx_fifo_in++] = data;
	datenklo->rx_fifo_in %= datenklo->rx_fifo_size;
	space--;

	/* in case of blocking: check if there is enough data to read */
	device_read_available(datenklo->device);
}

/* helper to set line states of modem */
static void set_lines(datenklo_t *datenklo, int new)
{
	int old = datenklo->lines;

	if (!(old & TIOCM_DTR) && (new & TIOCM_DTR)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal turns DTR on\n");
		flush_tx(datenklo);
		flush_rx(datenklo);
		am791x_dtr(&datenklo->am791x, 1);
	}
	if ((old & TIOCM_DTR) && !(new & TIOCM_DTR)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal turns DTR off\n");
		am791x_dtr(&datenklo->am791x, 0);
	}

	if (!(old & TIOCM_RTS) && (new & TIOCM_RTS)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal turns RTS on\n");
		if (datenklo->auto_rts)
			new |= TIOCM_CTS | TIOCM_CD;
		else {
			if (!datenklo->tx_back)
				am791x_rts(&datenklo->am791x, 1);
			else
				am791x_brts(&datenklo->am791x, 1);
		}
	}
	if ((old & TIOCM_RTS) && !(new & TIOCM_RTS)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal turns RTS off\n");
		if (datenklo->auto_rts)
			new &= ~(TIOCM_CTS | TIOCM_CD);
		else {
			if (!datenklo->tx_back)
				am791x_rts(&datenklo->am791x, 0);
			else
				am791x_brts(&datenklo->am791x, 0);
		}
	}

	datenklo->lines = new;
}

		/* process Auto RTS */
static void process_auto_rts(datenklo_t *datenklo)
{
	if (!datenklo->auto_rts)
		return;
	if (datenklo->auto_rts_on && !datenklo->auto_rts_rts && !datenklo->auto_rts_cd) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Automatically raising RTS.\n");
		datenklo->auto_rts_rts = 1;
		if (!datenklo->tx_back)
			am791x_rts(&datenklo->am791x, 1);
		else
			am791x_brts(&datenklo->am791x, 1);
	}
	if (!datenklo->auto_rts_on && datenklo->auto_rts_rts) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Automatically dropping RTS.\n");
		datenklo->auto_rts_rts = 0;
		if (!datenklo->tx_back)
			am791x_rts(&datenklo->am791x, 0);
		else
			am791x_brts(&datenklo->am791x, 0);
	}
}

/* tty performs all IOCTLs that requests states */
static ssize_t dk_ioctl_get(void *inst, int cmd, void *buf, size_t out_bufsz)
{
	datenklo_t *datenklo = (datenklo_t *)inst;
	int status;
	ssize_t rc = 0;

#ifdef HEAVY_DEBUG
	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Device has been read for ioctl (cmd = %d, size = %zu).\n", cmd, out_bufsz);
#endif

	switch (cmd) {
	case TCGETS:
		rc = sizeof(datenklo->termios);
		if (!out_bufsz)
			break;
#ifdef HEAVY_DEBUG
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal requests termios.\n");
#endif
		memcpy(buf, &datenklo->termios, rc);
		break;
	case TIOCMGET:
		rc = sizeof(status);
		if (!out_bufsz)
			break;
#ifdef HEAVY_DEBUG
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal requests line states.\n");
#endif
		status = datenklo->lines | TIOCM_LE | TIOCM_DSR;
		memcpy(buf, &status, rc);
		break;
	case TIOCGWINSZ:
		rc = sizeof(struct winsize);
		if (!out_bufsz)
			break;
#ifdef HEAVY_DEBUG
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal requests window size.\n");
#endif
		struct winsize *winsize = (struct winsize *)buf;
		winsize->ws_row = 25;
		winsize->ws_col = 80;
		winsize->ws_xpixel = 640;
		winsize->ws_ypixel = 200;
		break;
	case FIONREAD:
		rc = sizeof(status);
		if (!out_bufsz)
			break;
		status = (datenklo->rx_fifo_in - datenklo->rx_fifo_out + datenklo->rx_fifo_size) % datenklo->rx_fifo_size;
		memcpy(buf, &status, rc);
#ifdef HEAVY_DEBUG
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal requests RX buffer fill states.\n");
#endif
		break;
	case TIOCOUTQ:
		rc = sizeof(status);
		if (!out_bufsz)
			break;
#ifdef HEAVY_DEBUG
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal requests TX buffer fill states.\n");
#endif
		status = (datenklo->tx_fifo_in - datenklo->tx_fifo_out + datenklo->tx_fifo_size) % datenklo->tx_fifo_size;
		memcpy(buf, &status, rc);
		break;
	default:
		rc = -EINVAL;
	}

	return rc;
}

static double tx_baud_rate(datenklo_t *datenklo)
{
	double baudrate;

	if (datenklo->force_tx_baud)
		baudrate = datenklo->force_tx_baud;
	else
		baudrate = datenklo->baudrate;
	if (baudrate > datenklo->max_baud)
		baudrate = datenklo->max_baud;

	return baudrate;
}

static double rx_baud_rate(datenklo_t *datenklo)
{ 
	double baudrate;

	if (datenklo->force_rx_baud)
		baudrate = datenklo->force_rx_baud;
	else
		baudrate = datenklo->baudrate;
	if (baudrate > datenklo->max_baud)
		baudrate = datenklo->max_baud;

	return baudrate;
}

/* helper to set termios */
static void set_termios(datenklo_t *datenklo, const void *buf)
{
	double old_baud, new_baud;
	int old_databits, new_databits;
	enum uart_parity old_parity, new_parity;
	int old_stopbits, new_stopbits;
	int old_ignbrk, new_ignbrk;
	int old_parmrk, new_parmrk;
	int old_istrip, new_istrip;
	int old_inlcr, new_inlcr;
	int old_igncr, new_igncr;
	int old_icrnl, new_icrnl;
	int old_iuclc, new_iuclc;
	int old_opost, new_opost;
	int old_onlcr, new_onlcr;
	int old_ocrnl, new_ocrnl;
	int old_onlret, new_onlret;
	int old_olcuc, new_olcuc;
	int old_echo, new_echo;
	int rc;

	old_baud = cflag2baud(datenklo->termios.c_cflag & CBAUD);
	old_databits = cflag2databits(datenklo->termios.c_cflag);
	old_parity = cflag2parity(datenklo->termios.c_cflag);
	old_stopbits = cflag2stopbits(datenklo->termios.c_cflag);
	old_ignbrk = !!(datenklo->termios.c_iflag & IGNBRK);
	old_parmrk = !!(datenklo->termios.c_iflag & PARMRK);
	old_istrip = !!(datenklo->termios.c_iflag & ISTRIP);
	old_inlcr = !!(datenklo->termios.c_iflag & INLCR);
	old_igncr = !!(datenklo->termios.c_iflag & IGNCR);
	old_icrnl = !!(datenklo->termios.c_iflag & ICRNL);
	old_iuclc = !!(datenklo->termios.c_iflag & IUCLC);
	old_opost = !!(datenklo->termios.c_oflag & OPOST);
	old_onlcr = !!(datenklo->termios.c_oflag & ONLCR);
	old_ocrnl = !!(datenklo->termios.c_oflag & OCRNL);
	old_onlret = !!(datenklo->termios.c_oflag & ONLRET);
	old_olcuc = !!(datenklo->termios.c_oflag & OLCUC);
	old_echo = !!(datenklo->termios.c_lflag & ECHO);

	memcpy(&datenklo->termios, buf, sizeof(datenklo->termios));

	new_baud = cflag2baud(datenklo->termios.c_cflag & CBAUD);
	new_databits = cflag2databits(datenklo->termios.c_cflag);
	new_parity = cflag2parity(datenklo->termios.c_cflag);
	new_stopbits = cflag2stopbits(datenklo->termios.c_cflag);
	new_ignbrk = !!(datenklo->termios.c_iflag & IGNBRK);
	new_parmrk = !!(datenklo->termios.c_iflag & PARMRK);
	new_istrip = !!(datenklo->termios.c_iflag & ISTRIP);
	new_inlcr = !!(datenklo->termios.c_iflag & INLCR);
	new_igncr = !!(datenklo->termios.c_iflag & IGNCR);
	new_icrnl = !!(datenklo->termios.c_iflag & ICRNL);
	new_iuclc = !!(datenklo->termios.c_iflag & IUCLC);
	new_opost = !!(datenklo->termios.c_oflag & OPOST);
	new_onlcr = !!(datenklo->termios.c_oflag & ONLCR);
	new_ocrnl = !!(datenklo->termios.c_oflag & OCRNL);
	new_onlret = !!(datenklo->termios.c_oflag & ONLRET);
	new_olcuc = !!(datenklo->termios.c_oflag & OLCUC);
	new_echo = !!(datenklo->termios.c_lflag & ECHO);

	if (old_baud != new_baud && (!datenklo->force_tx_baud || !datenklo->force_rx_baud)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal changes baud rate to %.1f Baud.\n", new_baud);
		if ((datenklo->lines & TIOCM_DTR) && !new_baud) {
			PDEBUG(DDATENKLO, DEBUG_INFO, "Baudrate is set to 0, we drop DTR\n");
			am791x_dtr(&datenklo->am791x, 0);
		}
		datenklo->baudrate = new_baud;
		am791x_mc(&datenklo->am791x, datenklo->mc, datenklo->samplerate, tx_baud_rate(datenklo), rx_baud_rate(datenklo));
		if ((datenklo->lines & TIOCM_DTR) && !old_baud) {
			PDEBUG(DDATENKLO, DEBUG_INFO, "Baudrate is set from 0, we raise DTR\n");
			am791x_dtr(&datenklo->am791x, 1);
		}
	}

	if (old_databits != new_databits
	 || old_parity != new_parity
	 || old_stopbits != new_stopbits) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal changes serial mode to %d%c%d.\n", cflag2databits(datenklo->termios.c_cflag), parity2char(cflag2parity(datenklo->termios.c_cflag)), cflag2stopbits(datenklo->termios.c_cflag));
		rc = uart_init(&datenklo->uart, datenklo, cflag2databits(datenklo->termios.c_cflag), cflag2parity(datenklo->termios.c_cflag), cflag2stopbits(datenklo->termios.c_cflag), tx, rx);
		if (rc < 0)
			PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to initialize UART.\n");
	}

	if (old_stopbits != new_stopbits
	 || old_ignbrk != new_ignbrk
	 || old_parmrk != new_parmrk
	 || old_istrip != new_istrip
	 || old_inlcr != new_inlcr
	 || old_igncr != new_igncr
	 || old_icrnl != new_icrnl
	 || old_iuclc != new_iuclc
	 || old_opost != new_opost
	 || old_onlcr != new_onlcr
	 || old_ocrnl != new_ocrnl
	 || old_onlret != new_onlret
	 || old_olcuc != new_olcuc
	 || old_echo != new_echo) {
		datenklo->ignbrk = new_ignbrk;
		datenklo->parmrk = new_parmrk;
		datenklo->istrip = new_istrip;
		datenklo->inlcr = new_inlcr;
		datenklo->igncr = new_igncr;
		datenklo->icrnl = new_icrnl;
		datenklo->iuclc = new_iuclc;
		datenklo->opost = new_opost;
		datenklo->onlcr = new_onlcr;
		datenklo->ocrnl = new_ocrnl;
		datenklo->onlret = new_onlret;
		datenklo->olcuc = new_olcuc;
		datenklo->echo = new_echo;
		PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal sets serial flags:\n");
		PDEBUG(DDATENKLO, DEBUG_INFO, "%cignbrk %cparmrk %cistrip %cinlcr %cigncr %cicrnl %ciuclc %copost %conlcr %cocrnl %conlret %colcuc %cecho\n",
			(datenklo->ignbrk) ? '+' : '-',
			(datenklo->parmrk) ? '+' : '-',
			(datenklo->istrip) ? '+' : '-',
			(datenklo->inlcr) ? '+' : '-',
			(datenklo->igncr) ? '+' : '-',
			(datenklo->icrnl) ? '+' : '-',
			(datenklo->iuclc) ? '+' : '-',
			(datenklo->opost) ? '+' : '-',
			(datenklo->onlcr) ? '+' : '-',
			(datenklo->ocrnl) ? '+' : '-',
			(datenklo->onlret) ? '+' : '-',
			(datenklo->olcuc) ? '+' : '-',
			(datenklo->echo) ? '+' : '-');
	}
}

/* tty performs all IOCTLs that sets states or performs actions */
static ssize_t dk_ioctl_set(void *inst, int cmd, const void *buf, size_t in_bufsz)
{
	datenklo_t *datenklo = (datenklo_t *)inst;
	int status;
	ssize_t rc = 0;
	size_t space;

#ifdef HEAVY_DEBUG
	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Device has been written for ioctl (cmd = %d, size = %zu).\n", cmd, in_bufsz);
#endif

	switch (cmd) {
	case TCSETS:
		rc = sizeof(datenklo->termios);
		if (!in_bufsz)
			break;
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal sets termios now.\n");
		set_termios(datenklo, buf);
		break;
	case TCSETSW:
	case TCSETSF:
		rc = sizeof(datenklo->termios);
		if (!in_bufsz)
			break;
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal sets termios after draining output buffer.\n");
		if (1 || datenklo->tx_fifo_out == datenklo->tx_fifo_in) {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Output buffer empty, applying termios now.\n");
			set_termios(datenklo, buf);
			break;
		}
		memcpy(&datenklo->tcsetsw_termios, buf, rc);
		if (cmd == TCSETSW)
			datenklo->tcsetsw = 1;
		else
			datenklo->tcsetsw = 2;
		break;
	case TCFLSH:
		rc = sizeof(status);
		if (!in_bufsz)
			break;
		memcpy(&status, buf, rc);
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal flushes buffer (status = %d).\n", status);
		if (status == TCIOFLUSH || status == TCOFLUSH) {
			flush_tx(datenklo);
			if (!(datenklo->revents & POLLOUT)) {
				/* tell cuse to write again */
				PDEBUG(DDATENKLO, DEBUG_DEBUG, "Set POLLOUT (flushed)\n");
				datenklo->revents |= POLLOUT;
				device_set_poll_events(datenklo->device, datenklo->revents);
			}
		}
		if (status == TCIOFLUSH || status == TCIFLUSH) {
			flush_rx(datenklo);
			if ((datenklo->revents & POLLIN)) {
				PDEBUG(DDATENKLO, DEBUG_DEBUG, "Reset POLLIN (flushed)\n");
				datenklo->revents &= ~POLLIN;
				device_set_poll_events(datenklo->device, datenklo->revents);
			}
		}
		break;
	case TCSBRK:
		rc = 0;
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal sends break\n");
		datenklo->break_bits = tx_baud_rate(datenklo) * 3 / 10;
		break;
	case TCSBRKP:
		rc = sizeof(status);
		if (!in_bufsz)
			break;
		memcpy(&status, buf, rc);
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal sends break (duration = %d).\n", status);
		if (status == 0)
			status = 3;
		if (status > 30)
			status = 30;
		datenklo->break_bits = tx_baud_rate(datenklo) * status / 10;
		break;
	case TIOCSBRK:
		rc = 0;
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal turns break on\n");
		datenklo->break_on = 1;
		break;
	case TIOCCBRK:
		rc = 0;
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal turns break off\n");
		datenklo->break_on = 0;
		break;
	case TIOCMBIS:
		rc = sizeof(status);
		if (!in_bufsz)
			break;
		memcpy(&status, buf, rc);
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal sets line status (0x%x).\n", status);
		status = datenklo->lines | status;
		set_lines(datenklo, status);
		break;
	case TIOCMBIC:
		rc = sizeof(status);
		if (!in_bufsz)
			break;
		memcpy(&status, buf, rc);
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal clears line status (0x%x).\n", status);
		status = datenklo->lines & ~status;
		set_lines(datenklo, status);
		break;
	case TIOCMSET:
		rc = sizeof(status);
		if (!in_bufsz)
			break;
		memcpy(&status, buf, rc);
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal specifies line status (0x%x).\n", status);
		set_lines(datenklo, status);
		break;
	case TIOCGSID:
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "TIOGSID -> ENOTTY\n");
		rc = -ENOTTY;
		break;
	case TIOCGPGRP:
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "TIOCGPGRP -> ENOTTY\n");
		rc = -ENOTTY;
		break;
	case TIOCSCTTY:
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "TIOCSCTTY -> ENOTTY\n");
		rc = -ENOTTY;
		break;
	case TIOCSPGRP:
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "TIOCSPGRP -> ENOTTY\n");
		rc = -ENOTTY;
		break;
	case TIOCSWINSZ:
		rc = sizeof(struct winsize);
		if (!in_bufsz)
			break;
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal sets window size.\n");
		break;
	case TCXONC:
		rc = sizeof(status);
		if (!in_bufsz)
			break;
		memcpy(&status, buf, rc);
		switch (status) {
		case TCOOFF:
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal turns off output.\n");
			datenklo->output_off = 1;
			break;
		case TCOON:
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal turns on output.\n");
			datenklo->output_off = 1;
			break;
		case TCIOFF:
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal turns off input.\n");
			space = (datenklo->rx_fifo_out - datenklo->rx_fifo_in - 1 + datenklo->rx_fifo_size) % datenklo->rx_fifo_size;
			if (space < 1)
				break;
			datenklo->rx_fifo[datenklo->rx_fifo_in++] = datenklo->termios.c_cc[VSTOP];
			datenklo->rx_fifo_in %= datenklo->rx_fifo_size;
			break;
		case TCION:
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Terminal turns on input.\n");
			space = (datenklo->rx_fifo_out - datenklo->rx_fifo_in - 1 + datenklo->rx_fifo_size) % datenklo->rx_fifo_size;
			if (space < 1)
				break;
			datenklo->rx_fifo[datenklo->rx_fifo_in++] = datenklo->termios.c_cc[VSTART];
			datenklo->rx_fifo_in %= datenklo->rx_fifo_size;
			break;
		}
		break;
	default:
		rc = -EINVAL;
	}

	return rc;
}

/* tty has been opened */
static int dk_open(void *inst, int flags)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	if (datenklo->open_count) {
		PDEBUG(DDATENKLO, DEBUG_NOTICE, "Device is busy.\n");
		return -EBUSY;
	}
	datenklo->open_count++;
	datenklo->flags = flags;
	PDEBUG(DDATENKLO, DEBUG_INFO, "Device has been opened.\n");
	int status = datenklo->lines | TIOCM_DTR | TIOCM_RTS;
	set_lines(datenklo, status);

	return 0;
}

/* tty has been closed */
static void dk_close(void *inst)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	PDEBUG(DDATENKLO, DEBUG_INFO, "Device has been closed.\n");
	datenklo->open_count--;
	int status = datenklo->lines & ~(TIOCM_DTR | TIOCM_RTS);
	set_lines(datenklo, status);
	datenklo->output_off = 0;
}

/* helper to debug buffer content */
static void debug_data(const char *buf, int count)
{
	char text[41];
	size_t i;

	while (count) {
		for (i = 0; count && i < sizeof(text) - 1; i++) {
			text[i] = (*buf >= 32 && *buf <= 126) ? *buf : '.';
			buf++;
			count--;
		}
		text[i] = '\0';
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "  \"%s\"\n", text);
	}
}

/* tty performs read */
static ssize_t dk_read(void *inst, char *buf, size_t size, int flags)
{
	datenklo_t *datenklo = (datenklo_t *)inst;
	size_t fill, space;
	size_t i, count;
	unsigned char vtime = datenklo->termios.c_cc[VTIME];
	unsigned char vmin = datenklo->termios.c_cc[VMIN];

	fill = (datenklo->rx_fifo_in - datenklo->rx_fifo_out + datenklo->rx_fifo_size) % datenklo->rx_fifo_size;
	space = (datenklo->tx_fifo_out - datenklo->tx_fifo_in - 1 + datenklo->tx_fifo_size) % datenklo->tx_fifo_size;

	/* both MIN and TIME are nonzero */
	if (vmin && vtime) {
		/* first: start timer */
		if (!datenklo->vtimeout)
			timer_start(&datenklo->vtimer, (double)vtime * 0.1);
		/* no data: block (in blocking IO) */
		if (fill == 0) {
			/* special value to tell device there is no data right now, we have to block */
			return -EAGAIN;
		}
		/* not enough data and no timeout and blocking IO: block */
		if (fill < vmin && !datenklo->vtimeout && !(flags & O_NONBLOCK)) {
			/* special value to tell device there is no data right now, we have to block */
			return -EAGAIN;
		}
		/* enough data or timeout or nonblocking IO: stop timer and return what we have */
		datenklo->vtimeout = 0;
		timer_stop(&datenklo->vtimer);
	}
	/* both MIN and TIME are zero */
	if (!vmin && !vtime) {
		/* no data: return 0 */
		if (fill == 0)
			return 0;
		/* data: return what we have */
	}
	/* MIN is zero, TIME is nonzero */
	if (!vmin && vtime) {
		/* first: start timer */
		if (!datenklo->vtimeout)
			timer_start(&datenklo->vtimer, (double)vtime * 0.1);
		if (fill == 0) {
			/* no data and no timeout: block (in blocking IO) */
			if (!datenklo->vtimeout) {
				/* special value to tell device there is no data right now, we have to block */
				return -EAGAIN;
			}
			/* no data and timeout: return 0 */
			datenklo->vtimeout = 0;
			return 0;
		}
		/* data: stop timer and return what we have */
		datenklo->vtimeout = 0;
		timer_stop(&datenklo->vtimer);
	}
	/* MIN is nonzero, TIME is zero */
	if (vmin && !vtime) {
		/* less data than vmin (or buffer full): block (in blocking IO) */
		if (fill < vmin || !space) {
			/* special value to tell device there is no data right now, we have to block */
			return -EAGAIN;
		}
		/* enough data in buffer: return what we have */
	}

	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Device has been read from. (fill = %zu)\n", fill);

	/* get data from fifo */
	count = 0;
	for (i = 0; i < size; i++) {
		if (!fill)
			break;
		fill--;
		buf[i] = datenklo->rx_fifo[datenklo->rx_fifo_out++];
		datenklo->rx_fifo_out %= datenklo->rx_fifo_size;
		count++;
	}

	debug_data(buf, count);

	if (!fill) {
		/* tell cuse not to read anymore */
		if ((datenklo->revents & POLLIN)) {
			PDEBUG(DDATENKLO, DEBUG_DEBUG, "Reset POLLIN (now empty)!\n");
			datenklo->revents &= ~POLLIN;
			device_set_poll_events(datenklo->device, datenklo->revents);
		}
	}

	return count;
}

/* tty performs write */
static ssize_t dk_write(void *inst, const char *buf, size_t size, int __attribute__((unused)) flags)
{
	datenklo_t *datenklo = (datenklo_t *)inst;
	size_t space, fill;
	size_t i;

	if (!(datenklo->lines & TIOCM_DTR)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Dropping data, DTR is off!\n");
		return -EIO;
	}

	if (!(datenklo->lines & TIOCM_RTS)) {
		PDEBUG(DDATENKLO, DEBUG_INFO, "Dropping data, RTS is off!\n");
		return -EIO;
	}

	if (size > (size_t)datenklo->tx_fifo_size - 1) {
		PDEBUG(DDATENKLO, DEBUG_NOTICE, "Device sends us too many data. (size = %zu)\n", size);
		return -EIO;
	}

	space = (datenklo->tx_fifo_out - datenklo->tx_fifo_in - 1 + datenklo->tx_fifo_size) % datenklo->tx_fifo_size;

	/* block if not enough space AND buffer is not completely empty */
	if (space < size && datenklo->tx_fifo_out != datenklo->tx_fifo_in) {
		/* special value to tell device there is no data right now, we have to block */
		return -EAGAIN;
	}

	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Device has been written to. (space = %zu)\n", space);
	debug_data(buf, size);

	if (datenklo->auto_rts)
		datenklo->auto_rts_on = 1;

	/* put data to fifo */
	for (i = 0; i < size; i++) {
		datenklo->tx_fifo[datenklo->tx_fifo_in++] = buf[i];
		datenklo->tx_fifo_in %= datenklo->tx_fifo_size;
	}

	fill = (datenklo->tx_fifo_in - datenklo->tx_fifo_out + datenklo->tx_fifo_size) % datenklo->tx_fifo_size;

	if ((datenklo->revents & POLLOUT) && fill >= (size_t)datenklo->tx_fifo_full) {
		/* tell cuse not to write */
		PDEBUG(DDATENKLO, DEBUG_DEBUG, "Reset POLLOUT (buffer full)\n");
		datenklo->revents &= ~POLLOUT;
		device_set_poll_events(datenklo->device, datenklo->revents);
	}

	return size;
}

static void dk_flush_tx(void *inst)
{
	datenklo_t *datenklo = (datenklo_t *)inst;

	PDEBUG(DDATENKLO, DEBUG_INFO, "Terminal sends interrupt while writing, flushing TX buffer\n");
	flush_tx(datenklo);
}

/* tty locks main thread to call our functions */
static void dk_lock(void)
{
	pthread_mutex_lock(&mutex);
}

/* tty unlocks main thread */
static void dk_unlock(void)
{
	pthread_mutex_unlock(&mutex);
}

/* signal handler to exit */
void sighandler(int sigset)
{
	if (sigset == SIGHUP)
		return;
	if (sigset == SIGPIPE)
		return;

	printf("Signal received: %d\n", sigset);

	quit = 1;
}

/* vtimer */
static void vtime_timeout(struct timer *timer)
{
        datenklo_t *datenklo = (datenklo_t *)timer->priv;

	/* check if there is enough data to read */
	datenklo->vtimeout = 1;
	device_read_available(datenklo->device);
}

/* global init is required for the mutex */
void datenklo_init_global(void)
{
	if (pthread_mutex_init(&mutex, NULL)) {
		PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to init mutex.\n");
		exit(0);
	}
}

/* init function */
int datenklo_init(datenklo_t *datenklo, const char *dev_name, enum am791x_type am791x_type, uint8_t mc, int auto_rts, double force_tx_baud, double force_rx_baud, int samplerate, int loopback)
{
	int rc = 0;
	tcflag_t flag;
	cc_t *cc;

	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Creating Datenklo instance.\n");

	memset(datenklo, 0, sizeof(*datenklo));

	datenklo->samplerate = samplerate;
	datenklo->loopback = loopback;

	datenklo->mc = mc;
	datenklo->auto_rts = auto_rts;
	datenklo->max_baud = am791x_max_baud(datenklo->mc);
	datenklo->baudrate = datenklo->max_baud;
	datenklo->force_tx_baud = force_tx_baud;
	datenklo->force_rx_baud = force_rx_baud;
	if ((force_tx_baud && force_tx_baud <= 150) || datenklo->max_baud <= 150)
		datenklo->tx_back = 1;
	if ((force_rx_baud && force_rx_baud <= 150) || datenklo->max_baud <= 150)
		datenklo->rx_back = 1;

	/* default termios */
	flag = 0;
	flag |= baud2cflag(datenklo->baudrate);
	flag |= CS8;
	flag |= CREAD;
	datenklo->termios.c_cflag = flag;
	flag = 0;
	flag |= OPOST | ONLCR;
	datenklo->termios.c_oflag = flag;
	cc = datenklo->termios.c_cc;
	cc[VDISCARD] = 017;
//	cc[VDSUSP] = 031;
	cc[VEOF] = 004;
	cc[VEOL] = 0;
	cc[VEOL2] = 0;
	cc[VERASE] = 0177;
	cc[VINTR] = 003;
	cc[VKILL] = 025;
	cc[VLNEXT] = 026;
	cc[VMIN] = 0;
	cc[VQUIT] = 034;
	cc[VREPRINT] = 022;
	cc[VSTART] = 021;
//	cc[VSTATUS] = 024;
	cc[VSTOP] = 023;
	cc[VSUSP] = 032;
	cc[VSWTC] = 0;
	cc[VTIME] = 0;
	cc[VWERASE] = 027;

	datenklo->device = device_init(datenklo, dev_name, dk_open, dk_close, dk_read, dk_write, dk_ioctl_get, dk_ioctl_set, dk_flush_tx, dk_lock, dk_unlock);
	if (!datenklo->device) {
		PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to attach virtual device '%s' using cuse.\n", dev_name);
		rc = -errno;
		goto error;
	}
	datenklo->revents = POLLOUT;
	device_set_poll_events(datenklo->device, datenklo->revents);

	datenklo->baudrate = cflag2baud(datenklo->termios.c_cflag);
	rc = am791x_init(&datenklo->am791x, datenklo, am791x_type, datenklo->mc, datenklo->samplerate, tx_baud_rate(datenklo), rx_baud_rate(datenklo), cts, bcts, cd, bcd, td, btd, rd, brd);
	if (rc < 0) {
		PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to initialize AM791X modem chip.\n");
		goto error;
	}

	datenklo->tx_fifo_size = 4097;
	datenklo->tx_fifo_full = 4097; /* poll events disabled if same as size */
	datenklo->rx_fifo_size = 4097;
	datenklo->tx_fifo = calloc(datenklo->tx_fifo_size, 1);
	datenklo->rx_fifo = calloc(datenklo->rx_fifo_size, 1);
	if (!datenklo->tx_fifo || !datenklo->rx_fifo) {
		PDEBUG(DDATENKLO, DEBUG_ERROR, "No mem!\n");
		rc = -ENOMEM;
		goto error;
	}

	timer_init(&datenklo->vtimer, vtime_timeout, datenklo);

	rc = uart_init(&datenklo->uart, datenklo, cflag2databits(datenklo->termios.c_cflag), cflag2parity(datenklo->termios.c_cflag), cflag2stopbits(datenklo->termios.c_cflag), tx, rx);
	if (rc < 0) {
		PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to initialize UART.\n");
		goto error;
	}

	display_wave_init(&datenklo->dispwav, samplerate, dev_name);
	display_measurements_init(&datenklo->dispmeas, samplerate, dev_name);

	datenklo->dmp_level = display_measurements_add(&datenklo->dispmeas, "Level", "%.1f dBm", DISPLAY_MEAS_LAST, DISPLAY_MEAS_LEFT, -50.0, 5.0, 0.0);
	datenklo->dmp_quality = display_measurements_add(&datenklo->dispmeas, "Quality", "%.1f %%", DISPLAY_MEAS_LAST, DISPLAY_MEAS_LEFT, 0.0, 100.0, 100.0);

	return 0;

error:
	datenklo_exit(datenklo);
	return rc;
}

/* open audio device of one or two datenlo_t instance */
int datenklo_open_audio(datenklo_t *datenklo, const char *audiodev, int latency, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave)
{
	int channels = 1;
	int rc;

	/* stereo */
	if (datenklo->slave)
		channels = 2;

	/* latency of send buffer in samples */
	datenklo->latspl = datenklo->samplerate * latency / 1000;

#ifdef HAVE_ALSA
	/* init sound */
	datenklo->audio = sound_open(audiodev, NULL, NULL, channels, 0.0, datenklo->samplerate, datenklo->latspl, 1.0, 4000.0);
	if (!datenklo->audio) {
		PDEBUG(DDATENKLO, DEBUG_ERROR, "No sound device!\n");
		return -EIO;
	}
#endif

	if (write_rx_wave) {
		rc = wave_create_record(&datenklo->wave_rx_rec, write_rx_wave, datenklo->samplerate, channels, 1.0);
		if (rc < 0) {
			PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to create WAVE recoding instance!\n");
			return rc;
		}
	}
	if (write_tx_wave) {
		rc = wave_create_record(&datenklo->wave_tx_rec, write_tx_wave, datenklo->samplerate, channels, 1.0);
		if (rc < 0) {
			PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to create WAVE recoding instance!\n");
			return rc;
		}
	}
	if (read_rx_wave) {
		rc = wave_create_playback(&datenklo->wave_rx_play, read_rx_wave, &datenklo->samplerate, &channels, 1.0);
		if (rc < 0) {
			PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to create WAVE playback instance!\n");
			return rc;
		}
	}
	if (read_tx_wave) {
		rc = wave_create_playback(&datenklo->wave_tx_play, read_tx_wave, &datenklo->samplerate, &channels, 1.0);
		if (rc < 0) {
			PDEBUG(DDATENKLO, DEBUG_ERROR, "Failed to create WAVE playback instance!\n");
			return rc;
		}
	}

	return 0;
}

static int get_char()
{
	struct timeval tv = {0, 0};
	fd_set fds;
	char c = 0;
	int __attribute__((__unused__)) rc;

	FD_ZERO(&fds);
	FD_SET(0, &fds);
	select(0+1, &fds, NULL, NULL, &tv);
	if (FD_ISSET(0, &fds)) {
		rc = read(0, &c, 1);
		return c;
	} else
		return -1;
}

/* main loop */
void datenklo_main(datenklo_t *datenklo, int loopback)
{
	int num_chan = 1;
	int interval = 1;
	double begin_time, now, sleep;
	struct termios term, term_orig;
	int c;
	int i;

	/* stereo */
	if (datenklo->slave)
		num_chan = 2;

	sample_t buff[num_chan][datenklo->latspl], *samples[num_chan];
	uint8_t pbuff[num_chan][datenklo->latspl], *power[num_chan];
	for (i = 0; i < num_chan; i++) {
		samples[i] = buff[i];
		power[i] = pbuff[i];
	}
	double rf_level_db[num_chan];
	int count;
	int __attribute__((unused)) rc;

	pthread_mutex_lock(&mutex);

	/* prepare terminal */
	tcgetattr(0, &term_orig);
	term = term_orig;
	term.c_lflag &= ~(ISIG|ICANON|ECHO);
	term.c_cc[VMIN]=1;
	term.c_cc[VTIME]=2;
	tcsetattr(0, TCSANOW, &term);

	/* catch signals */
	signal(SIGINT, sighandler);
	signal(SIGHUP, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGPIPE, sighandler);

	sound_start(datenklo->audio);

	while (!quit) {
		begin_time = get_time();

		process_auto_rts(datenklo);
		/* process Auto RTS */
		if (num_chan > 1)
			process_auto_rts(datenklo->slave);

		/* process timers */
		process_timer();

#ifdef HAVE_ALSA
		count = sound_read(datenklo->audio, samples, datenklo->latspl, num_chan, rf_level_db);
		if (count < 0) {
			PDEBUG(DDSP, DEBUG_ERROR, "Failed to read RX data from audio device (rc = %d)\n", count);
			if (count == -EPIPE) {
				PDEBUG(DDATENKLO, DEBUG_ERROR, "Trying to recover!\n");
				continue;
			}
			break;
		}
#endif

		/* record received audio to wave */
		if (datenklo->wave_rx_rec.fp)
			wave_write(&datenklo->wave_rx_rec, samples, count);

		/* replace received audio from wave */
		if (datenklo->wave_rx_play.fp)
			wave_read(&datenklo->wave_rx_play, samples, count);

		/* put audio into modem */
		if (!loopback) {
			am791x_receive(&datenklo->am791x, samples[0], count);
			display_wave(&datenklo->dispwav, samples[0], count, 1);
			if (num_chan > 1) {
				am791x_receive(&datenklo->slave->am791x, samples[1], count);
				display_wave(&datenklo->slave->dispwav, samples[1], count, 1);
			}
		}

#ifdef HAVE_ALSA
		count = sound_get_tosend(datenklo->audio, datenklo->latspl);
#else
		count = samplerate / 1000;
#endif
		if (count < 0) {
			PDEBUG(DDSP, DEBUG_ERROR, "Failed to get number of samples in buffer (rc = %d)!\n", count);
			if (count == -EPIPE) {
				PDEBUG(DDATENKLO, DEBUG_ERROR, "Trying to recover!\n");
				continue;
			}
			break;
		}

		/* get audio from modem */
		am791x_send(&datenklo->am791x, samples[0], count);
		if (num_chan > 1) {
			am791x_send(&datenklo->slave->am791x, samples[1], count);
		}
		if (loopback) {
			/* copy buffer to preserve original audio for later use */
			sample_t lbuff[num_chan][datenklo->latspl];
			memcpy(lbuff, buff, sizeof(lbuff));
			if (loopback == 2 && num_chan == 2) {
				/* swap */
				samples[0] = lbuff[1];
				samples[1] = lbuff[0];
			} else {
				samples[0] = lbuff[0];
				samples[1] = lbuff[1];
			}
			am791x_receive(&datenklo->am791x, samples[0], count);
			display_wave(&datenklo->dispwav, samples[0], count, 1);
			if (num_chan > 1) {
				am791x_receive(&datenklo->slave->am791x, samples[1], count);
				display_wave(&datenklo->slave->dispwav, samples[1], count, 1);
			}
			samples[0] = buff[0];
			samples[1] = buff[1];
		}
		memset(power[0], 1, count);

		/* write generated audio to wave */
		if (datenklo->wave_tx_rec.fp)
			wave_write(&datenklo->wave_tx_rec, samples, count);

		/* replace generated audio from wave */
		if (datenklo->wave_tx_play.fp)
			wave_read(&datenklo->wave_tx_play, samples, count);

#ifdef HAVE_ALSA
		/* write audio */
		rc = sound_write(datenklo->audio, samples, power, count, NULL, NULL, num_chan);
		if (rc < 0) {
			PDEBUG(DDSP, DEBUG_ERROR, "Failed to write TX data to audio device (rc = %d)\n", rc);
			if (rc == -EPIPE) {
				PDEBUG(DDATENKLO, DEBUG_ERROR, "Trying to recover!\n");
				continue;
			}
			break;
		}
#endif

next_char:
		c = get_char();
		switch (c) {
		case 3:
			/* quit */
			if (clear_console_text)
				clear_console_text();
			printf("CTRL+c received, quitting!\n");
			quit = 1;
			goto next_char;
		case 'w':
			/* toggle wave display */
			display_measurements_on(0);
			display_wave_on(-1);
			goto next_char;
		case 'm':
			/* toggle measurements display */
			display_wave_on(0);
			display_measurements_on(-1);
			goto next_char;
		}

		display_measurements((double)interval / 1000.0);

		now = get_time();

		/* sleep interval */
		sleep = ((double)interval / 1000.0) - (now - begin_time);

		pthread_mutex_unlock(&mutex);
		if (sleep > 0)
			usleep(sleep * 1000000.0);
		pthread_mutex_lock(&mutex);
	}

	/* get rid of last entry */
	if (clear_console_text)
		clear_console_text();

	/* reset terminal */
	tcsetattr(0, TCSANOW, &term_orig);

	/* reset signals */
	signal(SIGINT, SIG_DFL);
	signal(SIGHUP, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);

	pthread_mutex_unlock(&mutex);
}

/* cleanup function */
void datenklo_exit(datenklo_t *datenklo)
{
	PDEBUG(DDATENKLO, DEBUG_DEBUG, "Destroying Datenklo instance.\n");

	timer_exit(&datenklo->vtimer);

	/* exit device */
	if (datenklo->device)
		device_exit(datenklo->device);

#ifdef HAVE_ALSA
	/* exit sound */
	if (datenklo->audio)
		sound_close(datenklo->audio);
#endif

	wave_destroy_record(&datenklo->wave_rx_rec);
	wave_destroy_record(&datenklo->wave_tx_rec);
	wave_destroy_playback(&datenklo->wave_rx_play);
	wave_destroy_playback(&datenklo->wave_tx_play);
}