Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
/*
 * Audio crossconnecting/conferrencing (hardware level).
 *
 * Copyright 2002 by Andreas Eversberg (jolly@eversberg.eu)
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 */

/*
 * The process of adding and removing parties to/from a conference:
 *
 * There is a chain of struct dsp_conf which has one or more members in a chain
 * of struct dsp_conf_member.
 *
 * After a party is added, the conference is checked for hardware capability.
 * Also if a party is removed, the conference is checked again.
 *
 * There are 3 different solutions: -1 = software, 0 = hardware-crossconnect
 * 1-n = hardware-conference. The n will give the conference number.
 *
 * Depending on the change after removal or insertion of a party, hardware
 * commands are given.
 *
 * The current solution is stored within the struct dsp_conf entry.
 */

/*
 * HOW THE CMX WORKS:
 *
 * There are 3 types of interaction: One member is alone, in this case only
 * data flow from upper to lower layer is done.
 * Two members will also exchange their data so they are crossconnected.
 * Three or more members will be added in a conference and will hear each
 * other but will not receive their own speech (echo) if not enabled.
 *
 * Features of CMX are:
 *  - Crossconnecting or even conference, if more than two members are together.
 *  - Force mixing of transmit data with other crossconnect/conference members.
 *  - Echo generation to benchmark the delay of audio processing.
 *  - Use hardware to minimize cpu load, disable FIFO load and minimize delay.
 *  - Dejittering and clock generation.
 *
 * There are 2 buffers:
 *
 *
 * RX-Buffer
 *                 R             W
 *                 |             |
 * ----------------+-------------+-------------------
 *
 * The rx-buffer is a ring buffer used to store the received data for each
 * individual member. This is only the case if data needs to be dejittered
 * or in case of a conference where different clocks require reclocking.
 * The transmit-clock (R) will read the buffer.
 * If the clock overruns the write-pointer, we will have a buffer underrun.
 * If the write pointer always has a certain distance from the transmit-
 * clock, we will have a delay. The delay will dynamically be increased and
 * reduced.
 *
 *
 * TX-Buffer
 *                  R        W
 *                  |        |
 * -----------------+--------+-----------------------
 *
 * The tx-buffer is a ring buffer to queue the transmit data from user space
 * until it will be mixed or sent. There are two pointers, R and W. If the write
 * pointer W would reach or overrun R, the buffer would overrun. In this case
 * (some) data is dropped so that it will not overrun.
 * Additionally a dynamic dejittering can be enabled. this allows data from
 * user space that have jitter and different clock source.
 *
 *
 * Clock:
 *
 * A Clock is not required, if the data source has exactly one clock. In this
 * case the data source is forwarded to the destination.
 *
 * A Clock is required, because the data source
 *  - has multiple clocks.
 *  - has no usable clock due to jitter or packet loss (VoIP).
 * In this case the system's clock is used. The clock resolution depends on
 * the jiffie resolution.
 *
 * If a member joins a conference:
 *
 * - If a member joins, its rx_buff is set to silence and change read pointer
 *   to transmit clock.
 *
 * The procedure of received data from card is explained in cmx_receive.
 * The procedure of received data from user space is explained in cmx_transmit.
 * The procedure of transmit data to card is cmx_send.
 *
 *
 * Interaction with other features:
 *
 * DTMF:
 * DTMF decoding is done before the data is crossconnected.
 *
 * Volume change:
 * Changing rx-volume is done before the data is crossconnected. The tx-volume
 * must be changed whenever data is transmitted to the card by the cmx.
 *
 * Tones:
 * If a tone is enabled, it will be processed whenever data is transmitted to
 * the card. It will replace the tx-data from the user space.
 * If tones are generated by hardware, this conference member is removed for
 * this time.
 *
 * Disable rx-data:
 * If cmx is realized in hardware, rx data will be disabled if requested by
 * the upper layer. If dtmf decoding is done by software and enabled, rx data
 * will not be disabled but blocked to the upper layer.
 *
 * HFC conference engine:
 * If it is possible to realize all features using hardware, hardware will be
 * used if not forbidden by control command. Disabling rx-data provides
 * absolutely traffic free audio processing. (except for the quick 1-frame
 * upload of a tone loop, only once for a new tone)
 *
 */

/* delay.h is required for hw_lock.h */

#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/mISDNif.h>
#include <linux/mISDNdsp.h>
#include "core.h"
#include "dsp.h"
/*
 * debugging of multi party conference,
 * by using conference even with two members
 */

/* #define CMX_CONF_DEBUG */

/*#define CMX_DEBUG * massive read/write pointer output */
/*#define CMX_DELAY_DEBUG * gives rx-buffer delay overview */
/*#define CMX_TX_DEBUG * massive read/write on tx-buffer with content */

static inline int
count_list_member(struct list_head *head)
{
	int			cnt = 0;
	struct list_head	*m;

	list_for_each(m, head)
		cnt++;
	return cnt;
}

/*
 * debug cmx memory structure
 */
void
dsp_cmx_debug(struct dsp *dsp)
{
	struct dsp_conf	*conf;
	struct dsp_conf_member	*member;
	struct dsp		*odsp;

	printk(KERN_DEBUG "-----Current DSP\n");
	list_for_each_entry(odsp, &dsp_ilist, list) {
		printk(KERN_DEBUG "* %s hardecho=%d softecho=%d txmix=%d",
		       odsp->name, odsp->echo.hardware, odsp->echo.software,
		       odsp->tx_mix);
		if (odsp->conf)
			printk(" (Conf %d)", odsp->conf->id);
		if (dsp == odsp)
			printk(" *this*");
		printk("\n");
	}
	printk(KERN_DEBUG "-----Current Conf:\n");
	list_for_each_entry(conf, &conf_ilist, list) {
		printk(KERN_DEBUG "* Conf %d (%p)\n", conf->id, conf);
		list_for_each_entry(member, &conf->mlist, list) {
			printk(KERN_DEBUG
			       "  - member = %s (slot_tx %d, bank_tx %d, "
			       "slot_rx %d, bank_rx %d hfc_conf %d "
			       "tx_data %d rx_is_off %d)%s\n",
			       member->dsp->name, member->dsp->pcm_slot_tx,
			       member->dsp->pcm_bank_tx, member->dsp->pcm_slot_rx,
			       member->dsp->pcm_bank_rx, member->dsp->hfc_conf,
			       member->dsp->tx_data, member->dsp->rx_is_off,
			       (member->dsp == dsp) ? " *this*" : "");
		}
	}
	printk(KERN_DEBUG "-----end\n");
}

/*
 * search conference
 */
static struct dsp_conf *
dsp_cmx_search_conf(u32 id)
{
	struct dsp_conf *conf;

	if (!id) {
		printk(KERN_WARNING "%s: conference ID is 0.\n", __func__);
		return NULL;
	}

	/* search conference */
	list_for_each_entry(conf, &conf_ilist, list)
		if (conf->id == id)
			return conf;

	return NULL;
}


/*
 * add member to conference
 */
static int
dsp_cmx_add_conf_member(struct dsp *dsp, struct dsp_conf *conf)
{
	struct dsp_conf_member *member;

	if (!conf || !dsp) {
		printk(KERN_WARNING "%s: conf or dsp is 0.\n", __func__);
		return -EINVAL;
	}
	if (dsp->member) {
		printk(KERN_WARNING "%s: dsp is already member in a conf.\n",
		       __func__);
		return -EINVAL;
	}

	if (dsp->conf) {
		printk(KERN_WARNING "%s: dsp is already in a conf.\n",
		       __func__);
		return -EINVAL;
	}

	member = kzalloc(sizeof(struct dsp_conf_member), GFP_ATOMIC);
	if (!member) {
		printk(KERN_ERR "kzalloc struct dsp_conf_member failed\n");
		return -ENOMEM;
	}
	member->dsp = dsp;
	/* clear rx buffer */
	memset(dsp->rx_buff, dsp_silence, sizeof(dsp->rx_buff));
	dsp->rx_init = 1; /* rx_W and rx_R will be adjusted on first frame */
	dsp->rx_W = 0;
	dsp->rx_R = 0;

	list_add_tail(&member->list, &conf->mlist);

	dsp->conf = conf;
	dsp->member = member;

	return 0;
}


/*
 * del member from conference
 */
int
dsp_cmx_del_conf_member(struct dsp *dsp)
{
	struct dsp_conf_member *member;

	if (!dsp) {
		printk(KERN_WARNING "%s: dsp is 0.\n",
		       __func__);
		return -EINVAL;
	}

	if (!dsp->conf) {
		printk(KERN_WARNING "%s: dsp is not in a conf.\n",
		       __func__);
		return -EINVAL;
	}

	if (list_empty(&dsp->conf->mlist)) {
		printk(KERN_WARNING "%s: dsp has linked an empty conf.\n",
		       __func__);
		return -EINVAL;
	}

	/* find us in conf */
	list_for_each_entry(member, &dsp->conf->mlist, list) {
		if (member->dsp == dsp) {
			list_del(&member->list);
			dsp->conf = NULL;
			dsp->member = NULL;
			kfree(member);
			return 0;
		}
	}
	printk(KERN_WARNING
	       "%s: dsp is not present in its own conf_meber list.\n",
	       __func__);

	return -EINVAL;
}


/*
 * new conference
 */
static struct dsp_conf
*dsp_cmx_new_conf(u32 id)
{
	struct dsp_conf *conf;

	if (!id) {
		printk(KERN_WARNING "%s: id is 0.\n",
		       __func__);
		return NULL;
	}

	conf = kzalloc(sizeof(struct dsp_conf), GFP_ATOMIC);
	if (!conf) {
		printk(KERN_ERR "kzalloc struct dsp_conf failed\n");
		return NULL;
	}
	INIT_LIST_HEAD(&conf->mlist);
	conf->id = id;

	list_add_tail(&conf->list, &conf_ilist);

	return conf;
}


/*
 * del conference
 */
int
dsp_cmx_del_conf(struct dsp_conf *conf)
{
	if (!conf) {
		printk(KERN_WARNING "%s: conf is null.\n",
		       __func__);
		return -EINVAL;
	}

	if (!list_empty(&conf->mlist)) {
		printk(KERN_WARNING "%s: conf not empty.\n",
		       __func__);
		return -EINVAL;
	}
	list_del(&conf->list);
	kfree(conf);

	return 0;
}


/*
 * send HW message to hfc card
 */
static void
dsp_cmx_hw_message(struct dsp *dsp, u32 message, u32 param1, u32 param2,
		   u32 param3, u32 param4)
{
	struct mISDN_ctrl_req cq;

	memset(&cq, 0, sizeof(cq));
	cq.op = message;
	cq.p1 = param1 | (param2 << 8);
	cq.p2 = param3 | (param4 << 8);
	if (dsp->ch.peer)
		dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq);
}


/*
 * do hardware update and set the software/hardware flag
 *
 * either a conference or a dsp instance can be given
 * if only dsp instance is given, the instance is not associated with a conf
 * and therefore removed. if a conference is given, the dsp is expected to
 * be member of that conference.
 */
void
dsp_cmx_hardware(struct dsp_conf *conf, struct dsp *dsp)
{
	struct dsp_conf_member	*member, *nextm;
	struct dsp		*finddsp;
	int		memb = 0, i, ii, i1, i2;
	int		freeunits[8];
	u_char		freeslots[256];
	int		same_hfc = -1, same_pcm = -1, current_conf = -1,
		all_conf = 1, tx_data = 0;

	/* dsp gets updated (no conf) */
	if (!conf) {
		if (!dsp)
			return;
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG "%s checking dsp %s\n",
			       __func__, dsp->name);
	one_member:
		/* remove HFC conference if enabled */
		if (dsp->hfc_conf >= 0) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s removing %s from HFC conf %d "
				       "because dsp is split\n", __func__,
				       dsp->name, dsp->hfc_conf);
			dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_CONF_SPLIT,
					   0, 0, 0, 0);
			dsp->hfc_conf = -1;
		}
		/* process hw echo */
		if (dsp->features.pcm_banks < 1)
			return;
		if (!dsp->echo.software && !dsp->echo.hardware) {
			/* NO ECHO: remove PCM slot if assigned */
			if (dsp->pcm_slot_tx >= 0 || dsp->pcm_slot_rx >= 0) {
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG "%s removing %s from"
					       " PCM slot %d (TX) %d (RX) because"
					       " dsp is split (no echo)\n",
					       __func__, dsp->name,
					       dsp->pcm_slot_tx, dsp->pcm_slot_rx);
				dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_PCM_DISC,
						   0, 0, 0, 0);
				dsp->pcm_slot_tx = -1;
				dsp->pcm_bank_tx = -1;
				dsp->pcm_slot_rx = -1;
				dsp->pcm_bank_rx = -1;
			}
			return;
		}
		/* echo is enabled, find out if we use soft or hardware */
		dsp->echo.software = dsp->tx_data;
		dsp->echo.hardware = 0;
		/* ECHO: already echo */
		if (dsp->pcm_slot_tx >= 0 && dsp->pcm_slot_rx < 0 &&
		    dsp->pcm_bank_tx == 2 && dsp->pcm_bank_rx == 2) {
			dsp->echo.hardware = 1;
			return;
		}
		/* ECHO: if slot already assigned */
		if (dsp->pcm_slot_tx >= 0) {
			dsp->pcm_slot_rx = dsp->pcm_slot_tx;
			dsp->pcm_bank_tx = 2; /* 2 means loop */
			dsp->pcm_bank_rx = 2;
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s refresh %s for echo using slot %d\n",
				       __func__, dsp->name,
				       dsp->pcm_slot_tx);
			dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_PCM_CONN,
					   dsp->pcm_slot_tx, 2, dsp->pcm_slot_rx, 2);
			dsp->echo.hardware = 1;
			return;
		}
		/* ECHO: find slot */
		dsp->pcm_slot_tx = -1;
		dsp->pcm_slot_rx = -1;
		memset(freeslots, 1, sizeof(freeslots));
		list_for_each_entry(finddsp, &dsp_ilist, list) {
			if (finddsp->features.pcm_id == dsp->features.pcm_id) {
				if (finddsp->pcm_slot_rx >= 0 &&
				    finddsp->pcm_slot_rx < sizeof(freeslots))
					freeslots[finddsp->pcm_slot_rx] = 0;
				if (finddsp->pcm_slot_tx >= 0 &&
				    finddsp->pcm_slot_tx < sizeof(freeslots))
					freeslots[finddsp->pcm_slot_tx] = 0;
			}
		}
		i = 0;
		ii = dsp->features.pcm_slots;
		while (i < ii) {
			if (freeslots[i])
				break;
			i++;
		}
		if (i == ii) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s no slot available for echo\n",
				       __func__);
			/* no more slots available */
			dsp->echo.software = 1;
			return;
		}
		/* assign free slot */
		dsp->pcm_slot_tx = i;
		dsp->pcm_slot_rx = i;
		dsp->pcm_bank_tx = 2; /* loop */
		dsp->pcm_bank_rx = 2;
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG
			       "%s assign echo for %s using slot %d\n",
			       __func__, dsp->name, dsp->pcm_slot_tx);
		dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_PCM_CONN,
				   dsp->pcm_slot_tx, 2, dsp->pcm_slot_rx, 2);
		dsp->echo.hardware = 1;
		return;
	}

	/* conf gets updated (all members) */
	if (dsp_debug & DEBUG_DSP_CMX)
		printk(KERN_DEBUG "%s checking conference %d\n",
		       __func__, conf->id);

	if (list_empty(&conf->mlist)) {
		printk(KERN_ERR "%s: conference whithout members\n",
		       __func__);
		return;
	}
	member = list_entry(conf->mlist.next, struct dsp_conf_member, list);
	same_hfc = member->dsp->features.hfc_id;
	same_pcm = member->dsp->features.pcm_id;
	/* check all members in our conference */
	list_for_each_entry(member, &conf->mlist, list) {
		/* check if member uses mixing */
		if (member->dsp->tx_mix) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "tx_mix is turned on\n", __func__,
				       member->dsp->name);
		conf_software:
			list_for_each_entry(member, &conf->mlist, list) {
				dsp = member->dsp;
				/* remove HFC conference if enabled */
				if (dsp->hfc_conf >= 0) {
					if (dsp_debug & DEBUG_DSP_CMX)
						printk(KERN_DEBUG
						       "%s removing %s from HFC "
						       "conf %d because not "
						       "possible with hardware\n",
						       __func__,
						       dsp->name,
						       dsp->hfc_conf);
					dsp_cmx_hw_message(dsp,
							   MISDN_CTRL_HFC_CONF_SPLIT,
							   0, 0, 0, 0);
					dsp->hfc_conf = -1;
				}
				/* remove PCM slot if assigned */
				if (dsp->pcm_slot_tx >= 0 ||
				    dsp->pcm_slot_rx >= 0) {
					if (dsp_debug & DEBUG_DSP_CMX)
						printk(KERN_DEBUG "%s removing "
						       "%s from PCM slot %d (TX)"
						       " slot %d (RX) because not"
						       " possible with hardware\n",
						       __func__,
						       dsp->name,
						       dsp->pcm_slot_tx,
						       dsp->pcm_slot_rx);
					dsp_cmx_hw_message(dsp,
							   MISDN_CTRL_HFC_PCM_DISC,
							   0, 0, 0, 0);
					dsp->pcm_slot_tx = -1;
					dsp->pcm_bank_tx = -1;
					dsp->pcm_slot_rx = -1;
					dsp->pcm_bank_rx = -1;
				}
			}
			conf->hardware = 0;
			conf->software = 1;
			return;
		}
		/* check if member has echo turned on */
		if (member->dsp->echo.hardware || member->dsp->echo.software) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "echo is turned on\n", __func__,
				       member->dsp->name);
			goto conf_software;
		}
		/* check if member has tx_mix turned on */
		if (member->dsp->tx_mix) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "tx_mix is turned on\n",
				       __func__, member->dsp->name);
			goto conf_software;
		}
		/* check if member changes volume at an not suppoted level */
		if (member->dsp->tx_volume) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "tx_volume is changed\n",
				       __func__, member->dsp->name);
			goto conf_software;
		}
		if (member->dsp->rx_volume) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "rx_volume is changed\n",
				       __func__, member->dsp->name);
			goto conf_software;
		}
		/* check if tx-data turned on */
		if (member->dsp->tx_data) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s tx_data is turned on\n",
				       __func__, member->dsp->name);
			tx_data = 1;
		}
		/* check if pipeline exists */
		if (member->dsp->pipeline.inuse) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "pipeline exists\n", __func__,
				       member->dsp->name);
			goto conf_software;
		}
		/* check if encryption is enabled */
		if (member->dsp->bf_enable) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG "%s dsp %s cannot form a "
				       "conf, because encryption is enabled\n",
				       __func__, member->dsp->name);
			goto conf_software;
		}
		/* check if member is on a card with PCM support */
		if (member->dsp->features.pcm_id < 0) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "dsp has no PCM bus\n",
				       __func__, member->dsp->name);
			goto conf_software;
		}
		/* check if relations are on the same PCM bus */
		if (member->dsp->features.pcm_id != same_pcm) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s dsp %s cannot form a conf, because "
				       "dsp is on a different PCM bus than the "
				       "first dsp\n",
				       __func__, member->dsp->name);
			goto conf_software;
		}
		/* determine if members are on the same hfc chip */
		if (same_hfc != member->dsp->features.hfc_id)
			same_hfc = -1;
		/* if there are members already in a conference */
		if (current_conf < 0 && member->dsp->hfc_conf >= 0)
			current_conf = member->dsp->hfc_conf;
		/* if any member is not in a conference */
		if (member->dsp->hfc_conf < 0)
			all_conf = 0;

		memb++;
	}

	/* if no member, this is an error */
	if (memb < 1)
		return;

	/* one member */
	if (memb == 1) {
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG
			       "%s conf %d cannot form a HW conference, "
			       "because dsp is alone\n", __func__, conf->id);
		conf->hardware = 0;
		conf->software = 0;
		member = list_entry(conf->mlist.next, struct dsp_conf_member,
				    list);
		dsp = member->dsp;
		goto one_member;
	}

	/*
	 * ok, now we are sure that all members are on the same pcm.
	 * now we will see if we have only two members, so we can do
	 * crossconnections, which don't have any limitations.
	 */

	/* if we have only two members */
	if (memb == 2) {
		member = list_entry(conf->mlist.next, struct dsp_conf_member,
				    list);
		nextm = list_entry(member->list.next, struct dsp_conf_member,
				   list);
		/* remove HFC conference if enabled */
		if (member->dsp->hfc_conf >= 0) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s removing %s from HFC conf %d because "
				       "two parties require only a PCM slot\n",
				       __func__, member->dsp->name,
				       member->dsp->hfc_conf);
			dsp_cmx_hw_message(member->dsp,
					   MISDN_CTRL_HFC_CONF_SPLIT, 0, 0, 0, 0);
			member->dsp->hfc_conf = -1;
		}
		if (nextm->dsp->hfc_conf >= 0) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s removing %s from HFC conf %d because "
				       "two parties require only a PCM slot\n",
				       __func__, nextm->dsp->name,
				       nextm->dsp->hfc_conf);
			dsp_cmx_hw_message(nextm->dsp,
					   MISDN_CTRL_HFC_CONF_SPLIT, 0, 0, 0, 0);
			nextm->dsp->hfc_conf = -1;
		}
		/* if members have two banks (and not on the same chip) */
		if (member->dsp->features.pcm_banks > 1 &&
		    nextm->dsp->features.pcm_banks > 1 &&
		    member->dsp->features.hfc_id !=
		    nextm->dsp->features.hfc_id) {
			/* if both members have same slots with crossed banks */
			if (member->dsp->pcm_slot_tx >= 0 &&
			    member->dsp->pcm_slot_rx >= 0 &&
			    nextm->dsp->pcm_slot_tx >= 0 &&
			    nextm->dsp->pcm_slot_rx >= 0 &&
			    nextm->dsp->pcm_slot_tx ==
			    member->dsp->pcm_slot_rx &&
			    nextm->dsp->pcm_slot_rx ==
			    member->dsp->pcm_slot_tx &&
			    nextm->dsp->pcm_slot_tx ==
			    member->dsp->pcm_slot_tx &&
			    member->dsp->pcm_bank_tx !=
			    member->dsp->pcm_bank_rx &&
			    nextm->dsp->pcm_bank_tx !=
			    nextm->dsp->pcm_bank_rx) {
				/* all members have same slot */
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG
					       "%s dsp %s & %s stay joined on "
					       "PCM slot %d bank %d (TX) bank %d "
					       "(RX) (on different chips)\n",
					       __func__,
					       member->dsp->name,
					       nextm->dsp->name,
					       member->dsp->pcm_slot_tx,
					       member->dsp->pcm_bank_tx,
					       member->dsp->pcm_bank_rx);
				conf->hardware = 1;
				conf->software = tx_data;
				return;
			}
			/* find a new slot */
			memset(freeslots, 1, sizeof(freeslots));
			list_for_each_entry(dsp, &dsp_ilist, list) {
				if (dsp != member->dsp &&
				    dsp != nextm->dsp &&
				    member->dsp->features.pcm_id ==
				    dsp->features.pcm_id) {
					if (dsp->pcm_slot_rx >= 0 &&
					    dsp->pcm_slot_rx <
					    sizeof(freeslots))
						freeslots[dsp->pcm_slot_rx] = 0;
					if (dsp->pcm_slot_tx >= 0 &&
					    dsp->pcm_slot_tx <
					    sizeof(freeslots))
						freeslots[dsp->pcm_slot_tx] = 0;
				}
			}
			i = 0;
			ii = member->dsp->features.pcm_slots;
			while (i < ii) {
				if (freeslots[i])
					break;
				i++;
			}
			if (i == ii) {
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG
					       "%s no slot available for "
					       "%s & %s\n", __func__,
					       member->dsp->name,
					       nextm->dsp->name);
				/* no more slots available */
				goto conf_software;
			}
			/* assign free slot */
			member->dsp->pcm_slot_tx = i;
			member->dsp->pcm_slot_rx = i;
			nextm->dsp->pcm_slot_tx = i;
			nextm->dsp->pcm_slot_rx = i;
			member->dsp->pcm_bank_rx = 0;
			member->dsp->pcm_bank_tx = 1;
			nextm->dsp->pcm_bank_rx = 1;
			nextm->dsp->pcm_bank_tx = 0;
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s adding %s & %s to new PCM slot %d "
				       "(TX and RX on different chips) because "
				       "both members have not same slots\n",
				       __func__,
				       member->dsp->name,
				       nextm->dsp->name,
				       member->dsp->pcm_slot_tx);
			dsp_cmx_hw_message(member->dsp, MISDN_CTRL_HFC_PCM_CONN,
					   member->dsp->pcm_slot_tx, member->dsp->pcm_bank_tx,
					   member->dsp->pcm_slot_rx, member->dsp->pcm_bank_rx);
			dsp_cmx_hw_message(nextm->dsp, MISDN_CTRL_HFC_PCM_CONN,
					   nextm->dsp->pcm_slot_tx, nextm->dsp->pcm_bank_tx,
					   nextm->dsp->pcm_slot_rx, nextm->dsp->pcm_bank_rx);
			conf->hardware = 1;
			conf->software = tx_data;
			return;
			/* if members have one bank (or on the same chip) */
		} else {
			/* if both members have different crossed slots */
			if (member->dsp->pcm_slot_tx >= 0 &&
			    member->dsp->pcm_slot_rx >= 0 &&
			    nextm->dsp->pcm_slot_tx >= 0 &&
			    nextm->dsp->pcm_slot_rx >= 0 &&
			    nextm->dsp->pcm_slot_tx ==
			    member->dsp->pcm_slot_rx &&
			    nextm->dsp->pcm_slot_rx ==
			    member->dsp->pcm_slot_tx &&
			    member->dsp->pcm_slot_tx !=
			    member->dsp->pcm_slot_rx &&
			    member->dsp->pcm_bank_tx == 0 &&
			    member->dsp->pcm_bank_rx == 0 &&
			    nextm->dsp->pcm_bank_tx == 0 &&
			    nextm->dsp->pcm_bank_rx == 0) {
				/* all members have same slot */
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG
					       "%s dsp %s & %s stay joined on PCM "
					       "slot %d (TX) %d (RX) on same chip "
					       "or one bank PCM)\n", __func__,
					       member->dsp->name,
					       nextm->dsp->name,
					       member->dsp->pcm_slot_tx,
					       member->dsp->pcm_slot_rx);
				conf->hardware = 1;
				conf->software = tx_data;
				return;
			}
			/* find two new slot */
			memset(freeslots, 1, sizeof(freeslots));
			list_for_each_entry(dsp, &dsp_ilist, list) {
				if (dsp != member->dsp &&
				    dsp != nextm->dsp &&
				    member->dsp->features.pcm_id ==
				    dsp->features.pcm_id) {
					if (dsp->pcm_slot_rx >= 0 &&
					    dsp->pcm_slot_rx <
					    sizeof(freeslots))
						freeslots[dsp->pcm_slot_rx] = 0;
					if (dsp->pcm_slot_tx >= 0 &&
					    dsp->pcm_slot_tx <
					    sizeof(freeslots))
						freeslots[dsp->pcm_slot_tx] = 0;
				}
			}
			i1 = 0;
			ii = member->dsp->features.pcm_slots;
			while (i1 < ii) {
				if (freeslots[i1])
					break;
				i1++;
			}
			if (i1 == ii) {
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG
					       "%s no slot available "
					       "for %s & %s\n", __func__,
					       member->dsp->name,
					       nextm->dsp->name);
				/* no more slots available */
				goto conf_software;
			}
			i2 = i1 + 1;
			while (i2 < ii) {
				if (freeslots[i2])
					break;
				i2++;
			}
			if (i2 == ii) {
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG
					       "%s no slot available "
					       "for %s & %s\n",
					       __func__,
					       member->dsp->name,
					       nextm->dsp->name);
				/* no more slots available */
				goto conf_software;
			}
			/* assign free slots */
			member->dsp->pcm_slot_tx = i1;
			member->dsp->pcm_slot_rx = i2;
			nextm->dsp->pcm_slot_tx = i2;
			nextm->dsp->pcm_slot_rx = i1;
			member->dsp->pcm_bank_rx = 0;
			member->dsp->pcm_bank_tx = 0;
			nextm->dsp->pcm_bank_rx = 0;
			nextm->dsp->pcm_bank_tx = 0;
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s adding %s & %s to new PCM slot %d "
				       "(TX) %d (RX) on same chip or one bank "
				       "PCM, because both members have not "
				       "crossed slots\n", __func__,
				       member->dsp->name,
				       nextm->dsp->name,
				       member->dsp->pcm_slot_tx,
				       member->dsp->pcm_slot_rx);
			dsp_cmx_hw_message(member->dsp, MISDN_CTRL_HFC_PCM_CONN,
					   member->dsp->pcm_slot_tx, member->dsp->pcm_bank_tx,
					   member->dsp->pcm_slot_rx, member->dsp->pcm_bank_rx);
			dsp_cmx_hw_message(nextm->dsp, MISDN_CTRL_HFC_PCM_CONN,
					   nextm->dsp->pcm_slot_tx, nextm->dsp->pcm_bank_tx,
					   nextm->dsp->pcm_slot_rx, nextm->dsp->pcm_bank_rx);
			conf->hardware = 1;
			conf->software = tx_data;
			return;
		}
	}

	/*
	 * if we have more than two, we may check if we have a conference
	 * unit available on the chip. also all members must be on the same
	 */

	/* if not the same HFC chip */
	if (same_hfc < 0) {
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG
			       "%s conference %d cannot be formed, because "
			       "members are on different chips or not "
			       "on HFC chip\n",
			       __func__, conf->id);
		goto conf_software;
	}

	/* for more than two members.. */

	/* if all members already have the same conference */
	if (all_conf) {
		conf->hardware = 1;
		conf->software = tx_data;
		return;
	}

	/*
	 * if there is an existing conference, but not all members have joined
	 */
	if (current_conf >= 0) {
	join_members:
		list_for_each_entry(member, &conf->mlist, list) {
			/* if no conference engine on our chip, change to
			 * software */
			if (!member->dsp->features.hfc_conf)
				goto conf_software;
			/* in case of hdlc, change to software */
			if (member->dsp->hdlc)
				goto conf_software;
			/* join to current conference */
			if (member->dsp->hfc_conf == current_conf)
				continue;
			/* get a free timeslot first */
			memset(freeslots, 1, sizeof(freeslots));
			list_for_each_entry(dsp, &dsp_ilist, list) {
				/*
				 * not checking current member, because
				 * slot will be overwritten.
				 */
				if (
					dsp != member->dsp &&
					/* dsp must be on the same PCM */
					member->dsp->features.pcm_id ==
					dsp->features.pcm_id) {
					/* dsp must be on a slot */
					if (dsp->pcm_slot_tx >= 0 &&
					    dsp->pcm_slot_tx <
					    sizeof(freeslots))
						freeslots[dsp->pcm_slot_tx] = 0;
					if (dsp->pcm_slot_rx >= 0 &&
					    dsp->pcm_slot_rx <
					    sizeof(freeslots))
						freeslots[dsp->pcm_slot_rx] = 0;
				}
			}
			i = 0;
			ii = member->dsp->features.pcm_slots;
			while (i < ii) {
				if (freeslots[i])
					break;
				i++;
			}
			if (i == ii) {
				/* no more slots available */
				if (dsp_debug & DEBUG_DSP_CMX)
					printk(KERN_DEBUG
					       "%s conference %d cannot be formed,"
					       " because no slot free\n",
					       __func__, conf->id);
				goto conf_software;
			}
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "%s changing dsp %s to HW conference "
				       "%d slot %d\n", __func__,
				       member->dsp->name, current_conf, i);
			/* assign free slot & set PCM & join conf */
			member->dsp->pcm_slot_tx = i;
			member->dsp->pcm_slot_rx = i;
			member->dsp->pcm_bank_tx = 2; /* loop */
			member->dsp->pcm_bank_rx = 2;
			member->dsp->hfc_conf = current_conf;
			dsp_cmx_hw_message(member->dsp, MISDN_CTRL_HFC_PCM_CONN,
					   i, 2, i, 2);
			dsp_cmx_hw_message(member->dsp,
					   MISDN_CTRL_HFC_CONF_JOIN, current_conf, 0, 0, 0);
		}
		conf->hardware = 1;
		conf->software = tx_data;
		return;
	}

	/*
	 * no member is in a conference yet, so we find a free one
	 */
	memset(freeunits, 1, sizeof(freeunits));
	list_for_each_entry(dsp, &dsp_ilist, list) {
		/* dsp must be on the same chip */
		if (dsp->features.hfc_id == same_hfc &&
		    /* dsp must have joined a HW conference */
		    dsp->hfc_conf >= 0 &&
		    /* slot must be within range */
		    dsp->hfc_conf < 8)
			freeunits[dsp->hfc_conf] = 0;
	}
	i = 0;
	ii = 8;
	while (i < ii) {
		if (freeunits[i])
			break;
		i++;
	}
	if (i == ii) {
		/* no more conferences available */
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG
			       "%s conference %d cannot be formed, because "
			       "no conference number free\n",
			       __func__, conf->id);
		goto conf_software;
	}
	/* join all members */
	current_conf = i;
	goto join_members;
}


/*
 * conf_id != 0: join or change conference
 * conf_id == 0: split from conference if not already
 */
int
dsp_cmx_conf(struct dsp *dsp, u32 conf_id)
{
	int err;
	struct dsp_conf *conf;
	struct dsp_conf_member	*member;

	/* if conference doesn't change */
	if (dsp->conf_id == conf_id)
		return 0;

	/* first remove us from current conf */
	if (dsp->conf_id) {
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG "removing us from conference %d\n",
			       dsp->conf->id);
		/* remove us from conf */
		conf = dsp->conf;
		err = dsp_cmx_del_conf_member(dsp);
		if (err)
			return err;
		dsp->conf_id = 0;

		/* update hardware */
		dsp_cmx_hardware(NULL, dsp);

		/* conf now empty? */
		if (list_empty(&conf->mlist)) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "conference is empty, so we remove it.\n");
			err = dsp_cmx_del_conf(conf);
			if (err)
				return err;
		} else {
			/* update members left on conf */
			dsp_cmx_hardware(conf, NULL);
		}
	}

	/* if split */
	if (!conf_id)
		return 0;

	/* now add us to conf */
	if (dsp_debug & DEBUG_DSP_CMX)
		printk(KERN_DEBUG "searching conference %d\n",
		       conf_id);
	conf = dsp_cmx_search_conf(conf_id);
	if (!conf) {
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG
			       "conference doesn't exist yet, creating.\n");
		/* the conference doesn't exist, so we create */
		conf = dsp_cmx_new_conf(conf_id);
		if (!conf)
			return -EINVAL;
	} else if (!list_empty(&conf->mlist)) {
		member = list_entry(conf->mlist.next, struct dsp_conf_member,
				    list);
		if (dsp->hdlc && !member->dsp->hdlc) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "cannot join transparent conference.\n");
			return -EINVAL;
		}
		if (!dsp->hdlc && member->dsp->hdlc) {
			if (dsp_debug & DEBUG_DSP_CMX)
				printk(KERN_DEBUG
				       "cannot join hdlc conference.\n");
			return -EINVAL;
		}
	}
	/* add conference member */
	err = dsp_cmx_add_conf_member(dsp, conf);
	if (err)
		return err;
	dsp->conf_id = conf_id;

	/* if we are alone, we do nothing! */
	if (list_empty(&conf->mlist)) {
		if (dsp_debug & DEBUG_DSP_CMX)
			printk(KERN_DEBUG
			       "we are alone in this conference, so exit.\n");
		/* update hardware */
		dsp_cmx_hardware(NULL, dsp);
		return 0;
	}

	/* update members on conf */
	dsp_cmx_hardware(conf, NULL);

	return 0;
}

#ifdef CMX_DELAY_DEBUG
int delaycount;
static void
showdelay(struct dsp *dsp, int samples, int delay)
{
	char bar[] = "--------------------------------------------------|";
	int sdelay;

	delaycount += samples;
	if (delaycount < 8000)
		return;
	delaycount = 0;

	sdelay = delay * 50 / (dsp_poll << 2);

	printk(KERN_DEBUG "DELAY (%s) %3d >%s\n", dsp->name, delay,
	       sdelay > 50 ? "..." : bar + 50 - sdelay);
}
#endif

/*
 * audio data is received from card
 */
void
dsp_cmx_receive(struct dsp *dsp, struct sk_buff *skb)
{
	u8 *d, *p;
	int len = skb->len;
	struct mISDNhead *hh = mISDN_HEAD_P(skb);
	int w, i, ii;

	/* check if we have sompen */
	if (len < 1)
		return;

	/* half of the buffer should be larger than maximum packet size */
	if (len >= CMX_BUFF_HALF) {
		printk(KERN_ERR
		       "%s line %d: packet from card is too large (%d bytes). "
		       "please make card send smaller packets OR increase "
		       "CMX_BUFF_SIZE\n", __FILE__, __LINE__, len);
		return;
	}

	/*
	 * initialize pointers if not already -
	 * also add delay if requested by PH_SIGNAL
	 */
	if (dsp->rx_init) {
		dsp->rx_init = 0;
		if (dsp->features.unordered) {
			dsp->rx_R = (hh->id & CMX_BUFF_MASK);
			if (dsp->cmx_delay)
				dsp->rx_W = (dsp->rx_R + dsp->cmx_delay)
					& CMX_BUFF_MASK;
			else
				dsp->rx_W = (dsp->rx_R + (dsp_poll >> 1))
					& CMX_BUFF_MASK;
		} else {
			dsp->rx_R = 0;
			if (dsp->cmx_delay)
				dsp->rx_W = dsp->cmx_delay;
			else
				dsp->rx_W = dsp_poll >> 1;
		}
	}
	/* if frame contains time code, write directly */
	if (dsp->features.unordered) {
		dsp->rx_W = (hh->id & CMX_BUFF_MASK);
		/* printk(KERN_DEBUG "%s %08x\n", dsp->name, hh->id); */
	}
	/*
	 * if we underrun (or maybe overrun),
	 * we set our new read pointer, and write silence to buffer
	 */
	if (((dsp->rx_W-dsp->rx_R) & CMX_BUFF_MASK) >= CMX_BUFF_HALF) {
		if (dsp_debug & DEBUG_DSP_CLOCK)
			printk(KERN_DEBUG
			       "cmx_receive(dsp=%lx): UNDERRUN (or overrun the "
			       "maximum delay), adjusting read pointer! "
			       "(inst %s)\n", (u_long)dsp, dsp->name);
		/* flush rx buffer and set delay to dsp_poll / 2 */
		if (dsp->features.unordered) {
			dsp->rx_R = (hh->id & CMX_BUFF_MASK);
			if (dsp->cmx_delay)
				dsp->rx_W = (dsp->rx_R + dsp->cmx_delay)
					& CMX_BUFF_MASK;
			else
				dsp->rx_W = (dsp->rx_R + (dsp_poll >> 1))
					& CMX_BUFF_MASK;
		} else {
			dsp->rx_R = 0;
			if (dsp->cmx_delay)
				dsp->rx_W = dsp->cmx_delay;
			else
				dsp->rx_W = dsp_poll >> 1;
		}
		memset(dsp->rx_buff, dsp_silence, sizeof(dsp->rx_buff));
	}
	/* if we have reached double delay, jump back to middle */
	if (dsp->cmx_delay)
		if (((dsp->rx_W - dsp->rx_R) & CMX_BUFF_MASK) >=
		    (dsp->cmx_delay << 1)) {
			if (dsp_debug & DEBUG_DSP_CLOCK)
				printk(KERN_DEBUG
				       "cmx_receive(dsp=%lx): OVERRUN (because "
				       "twice the delay is reached), adjusting "
				       "read pointer! (inst %s)\n",
				       (u_long)dsp, dsp->name);
			/* flush buffer */
			if (dsp->features.unordered) {
				dsp->rx_R = (hh->id & CMX_BUFF_MASK);
				dsp->rx_W = (dsp->rx_R + dsp->cmx_delay)
					& CMX_BUFF_MASK;
			} else {
				dsp->rx_R = 0;
				dsp->rx_W = dsp->cmx_delay;
			}
			memset(dsp->rx_buff, dsp_silence, sizeof(dsp->rx_buff));
		}

	/* show where to write */
#ifdef CMX_DEBUG
	printk(KERN_DEBUG
	       "cmx_receive(dsp=%lx): rx_R(dsp)=%05x rx_W(dsp)=%05x len=%d %s\n",
	       (u_long)dsp, dsp->rx_R, dsp->rx_W, len, dsp->name);
#endif

	/* write data into rx_buffer */
	p = skb->data;
	d = dsp->rx_buff;
	w = dsp->rx_W;
	i = 0;
	ii = len;
	while (i < ii) {
		d[w++ & CMX_BUFF_MASK] = *p++;
		i++;
	}

	/* increase write-pointer */
	dsp->rx_W = ((dsp->rx_W + len) & CMX_BUFF_MASK);
#ifdef CMX_DELAY_DEBUG
	showdelay(dsp, len, (dsp->rx_W-dsp->rx_R) & CMX_BUFF_MASK);
#endif
}


/*
 * send (mixed) audio data to card and control jitter
 */
static void
dsp_cmx_send_member(struct dsp *dsp, int len, s32 *c, int members)
{
	struct dsp_conf *conf = dsp->conf;
	struct dsp *member, *other;
	register s32 sample;
	u8 *d, *p, *q, *o_q;
	struct sk_buff *nskb, *txskb;
	int r, rr, t, tt, o_r, o_rr;
	int preload = 0;
	struct mISDNhead *hh, *thh;
	int tx_data_only = 0;

	/* don't process if: */
	if (!dsp->b_active) { /* if not active */
		dsp->last_tx = 0;
		return;
	}
	if (((dsp->conf && dsp->conf->hardware) || /* hardware conf */
	     dsp->echo.hardware) && /* OR hardware echo */
	    dsp->tx_R == dsp->tx_W && /* AND no tx-data */
	    !(dsp->tone.tone && dsp->tone.software)) { /* AND not soft tones */
		if (!dsp->tx_data) { /* no tx_data for user space required */
			dsp->last_tx = 0;
			return;
		}
		if (dsp->conf && dsp->conf->software && dsp->conf->hardware)
			tx_data_only = 1;
		if (dsp->echo.software && dsp->echo.hardware)
			tx_data_only = 1;
	}

#ifdef CMX_DEBUG
	printk(KERN_DEBUG
	       "SEND members=%d dsp=%s, conf=%p, rx_R=%05x rx_W=%05x\n",
	       members, dsp->name, conf, dsp->rx_R, dsp->rx_W);
#endif

	/* preload if we have delay set */
	if (dsp->cmx_delay && !dsp->last_tx) {
		preload = len;
		if (preload < 128)
			preload = 128;
	}

	/* PREPARE RESULT */
	nskb = mI_alloc_skb(len + preload, GFP_ATOMIC);
	if (!nskb) {
		printk(KERN_ERR
		       "FATAL ERROR in mISDN_dsp.o: cannot alloc %d bytes\n",
		       len + preload);
		return;
	}
	hh = mISDN_HEAD_P(nskb);
	hh->prim = PH_DATA_REQ;
	hh->id = 0;
	dsp->last_tx = 1;

	/* set pointers, indexes and stuff */
	member = dsp;
	p = dsp->tx_buff; /* transmit data */
	q = dsp->rx_buff; /* received data */
	d = skb_put(nskb, preload + len); /* result */
	t = dsp->tx_R; /* tx-pointers */
	tt = dsp->tx_W;
	r = dsp->rx_R; /* rx-pointers */
	rr = (r + len) & CMX_BUFF_MASK;

	/* preload with silence, if required */
	if (preload) {
		memset(d, dsp_silence, preload);
		d += preload;
	}

	/* PROCESS TONES/TX-DATA ONLY */
	if (dsp->tone.tone && dsp->tone.software) {
		/* -> copy tone */
		dsp_tone_copy(dsp, d, len);
		dsp->tx_R = 0; /* clear tx buffer */
		dsp->tx_W = 0;
		goto send_packet;
	}
	/* if we have tx-data but do not use mixing */
	if (!dsp->tx_mix && t != tt) {
		/* -> send tx-data and continue when not enough */
#ifdef CMX_TX_DEBUG
		sprintf(debugbuf, "TX sending (%04x-%04x)%p: ", t, tt, p);
#endif
		while (r != rr && t != tt) {
#ifdef CMX_TX_DEBUG
			if (strlen(debugbuf) < 48)
				sprintf(debugbuf + strlen(debugbuf), " %02x",
					p[t]);
#endif
			*d++ = p[t]; /* write tx_buff */
			t = (t + 1) & CMX_BUFF_MASK;
			r = (r + 1) & CMX_BUFF_MASK;
		}
		if (r == rr) {
			dsp->tx_R = t;
#ifdef CMX_TX_DEBUG
			printk(KERN_DEBUG "%s\n", debugbuf);
#endif
			goto send_packet;
		}
	}
#ifdef CMX_TX_DEBUG
	printk(KERN_DEBUG "%s\n", debugbuf);
#endif

	/* PROCESS DATA (one member / no conf) */
	if (!conf || members <= 1) {
		/* -> if echo is NOT enabled */
		if (!dsp->echo.software) {
			/* -> send tx-data if available or use 0-volume */
			while (r != rr && t != tt) {
				*d++ = p[t]; /* write tx_buff */
				t = (t + 1) & CMX_BUFF_MASK;
				r = (r + 1) & CMX_BUFF_MASK;
			}
			if (r != rr) {
				if (dsp_debug & DEBUG_DSP_CLOCK)
					printk(KERN_DEBUG "%s: RX empty\n",
					       __func__);
				memset(d, dsp_silence, (rr - r) & CMX_BUFF_MASK);
			}
			/* -> if echo is enabled */
		} else {
			/*
			 * -> mix tx-data with echo if available,
			 * or use echo only
			 */
			while (r != rr && t != tt) {
				*d++ = dsp_audio_mix_law[(p[t] << 8) | q[r]];
				t = (t + 1) & CMX_BUFF_MASK;
				r = (r + 1) & CMX_BUFF_MASK;
			}
			while (r != rr) {
				*d++ = q[r]; /* echo */
				r = (r + 1) & CMX_BUFF_MASK;
			}
		}
		dsp->tx_R = t;
		goto send_packet;
	}
	/* PROCESS DATA (two members) */
#ifdef CMX_CONF_DEBUG
	if (0) {
#else
		if (members == 2) {
#endif
			/* "other" becomes other party */
			other = (list_entry(conf->mlist.next,
					    struct dsp_conf_member, list))->dsp;
			if (other == member)
				other = (list_entry(conf->mlist.prev,
						    struct dsp_conf_member, list))->dsp;
			o_q = other->rx_buff; /* received data */
			o_rr = (other->rx_R + len) & CMX_BUFF_MASK;
			/* end of rx-pointer */
			o_r = (o_rr - rr + r) & CMX_BUFF_MASK;
			/* start rx-pointer at current read position*/
			/* -> if echo is NOT enabled */
			if (!dsp->echo.software) {
				/*
				 * -> copy other member's rx-data,
				 * if tx-data is available, mix
				 */
				while (o_r != o_rr && t != tt) {
					*d++ = dsp_audio_mix_law[(p[t] << 8) | o_q[o_r]];
					t = (t + 1) & CMX_BUFF_MASK;
					o_r = (o_r + 1) & CMX_BUFF_MASK;
				}
				while (o_r != o_rr) {
					*d++ = o_q[o_r];
					o_r = (o_r + 1) & CMX_BUFF_MASK;
				}
				/* -> if echo is enabled */
			} else {
				/*
				 * -> mix other member's rx-data with echo,
				 * if tx-data is available, mix
				 */
				while (r != rr && t != tt) {
					sample = dsp_audio_law_to_s32[p[t]] +
						dsp_audio_law_to_s32[q[r]] +
						dsp_audio_law_to_s32[o_q[o_r]];
					if (sample < -32768)
						sample = -32768;
					else if (sample > 32767)
						sample = 32767;
					*d++ = dsp_audio_s16_to_law[sample & 0xffff];
					/* tx-data + rx_data + echo */
					t = (t + 1) & CMX_BUFF_MASK;
					r = (r + 1) & CMX_BUFF_MASK;
					o_r = (o_r + 1) & CMX_BUFF_MASK;
				}
				while (r != rr) {
					*d++ = dsp_audio_mix_law[(q[r] << 8) | o_q[o_r]];
					r = (r + 1) & CMX_BUFF_MASK;
					o_r = (o_r + 1) & CMX_BUFF_MASK;
				}
			}
			dsp->tx_R = t;
			goto send_packet;
		}
#ifdef DSP_NEVER_DEFINED
	}
#endif
	/* PROCESS DATA (three or more members) */
	/* -> if echo is NOT enabled */
	if (!dsp->echo.software) {
		/*
		 * -> subtract rx-data from conf-data,
		 * if tx-data is available, mix
		 */
		while (r != rr && t != tt) {
			sample = dsp_audio_law_to_s32[p[t]] + *c++ -
				dsp_audio_law_to_s32[q[r]];
			if (sample < -32768)
				sample = -32768;
			else if (sample > 32767)
				sample = 32767;
			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
			/* conf-rx+tx */
			r = (r + 1) & CMX_BUFF_MASK;
			t = (t + 1) & CMX_BUFF_MASK;
		}
		while (r != rr) {
			sample = *c++ - dsp_audio_law_to_s32[q[r]];
			if (sample < -32768)
				sample = -32768;
			else if (sample > 32767)
				sample = 32767;
			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
			/* conf-rx */
			r = (r + 1) & CMX_BUFF_MASK;
		}
		/* -> if echo is enabled */
	} else {
		/*
		 * -> encode conf-data, if tx-data
		 * is available, mix
		 */
		while (r != rr && t != tt) {
			sample = dsp_audio_law_to_s32[p[t]] + *c++;
			if (sample < -32768)
				sample = -32768;
			else if (sample > 32767)
				sample = 32767;
			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
			/* conf(echo)+tx */
			t = (t + 1) & CMX_BUFF_MASK;
			r = (r + 1) & CMX_BUFF_MASK;
		}
		while (r != rr) {
			sample = *c++;
			if (sample < -32768)
				sample = -32768;
			else if (sample > 32767)
				sample = 32767;
			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
			/* conf(echo) */
			r = (r + 1) & CMX_BUFF_MASK;
		}
	}
	dsp->tx_R = t;
	goto send_packet;

send_packet:
	/*
	 * send tx-data if enabled - don't filter,
	 * because we want what we send, not what we filtered
	 */
	if (dsp->tx_data) {
		if (tx_data_only) {
			hh->prim = DL_DATA_REQ;
			hh->id = 0;
			/* queue and trigger */
			skb_queue_tail(&dsp->sendq, nskb);
			schedule_work(&dsp->workq);
			/* exit because only tx_data is used */
			return;
		} else {
			txskb = mI_alloc_skb(len, GFP_ATOMIC);
			if (!txskb) {
				printk(KERN_ERR
				       "FATAL ERROR in mISDN_dsp.o: "
				       "cannot alloc %d bytes\n", len);
			} else {
				thh = mISDN_HEAD_P(txskb);
				thh->prim = DL_DATA_REQ;
				thh->id = 0;
				memcpy(skb_put(txskb, len), nskb->data + preload,
				       len);
				/* queue (trigger later) */
				skb_queue_tail(&dsp->sendq, txskb);
			}
		}
	}

	/* send data only to card, if we don't just calculated tx_data */
	/* adjust volume */
	if (dsp->tx_volume)
		dsp_change_volume(nskb, dsp->tx_volume);
	/* pipeline */
	if (dsp->pipeline.inuse)
		dsp_pipeline_process_tx(&dsp->pipeline, nskb->data,
					nskb->len);
	/* crypt */
	if (dsp->bf_enable)
		dsp_bf_encrypt(dsp, nskb->data, nskb->len);
	/* queue and trigger */
	skb_queue_tail(&dsp->sendq, nskb);
	schedule_work(&dsp->workq);
}

static u32	jittercount; /* counter for jitter check */
struct timer_list dsp_spl_tl;
unsigned long	dsp_spl_jiffies; /* calculate the next time to fire */
static u16	dsp_count; /* last sample count */
static int	dsp_count_valid; /* if we have last sample count */

void
dsp_cmx_send(void *arg)
{
	struct dsp_conf *conf;
	struct dsp_conf_member *member;
	struct dsp *dsp;
	int mustmix, members;
	static s32 mixbuffer[MAX_POLL + 100];
	s32 *c;
	u8 *p, *q;
	int r, rr;
	int jittercheck = 0, delay, i;
	u_long flags;
	u16 length, count;

	/* lock */
	spin_lock_irqsave(&dsp_lock, flags);

	if (!dsp_count_valid) {
		dsp_count = mISDN_clock_get();
		length = dsp_poll;
		dsp_count_valid = 1;
	} else {
		count = mISDN_clock_get();
		length = count - dsp_count;
		dsp_count = count;
	}
	if (length > MAX_POLL + 100)
		length = MAX_POLL + 100;
	/* printk(KERN_DEBUG "len=%d dsp_count=0x%x\n", length, dsp_count); */

	/*
	 * check if jitter needs to be checked (this is every second)
	 */
	jittercount += length;
	if (jittercount >= 8000) {
		jittercount -= 8000;
		jittercheck = 1;
	}

	/* loop all members that do not require conference mixing */
	list_for_each_entry(dsp, &dsp_ilist, list) {
		if (dsp->hdlc)
			continue;
		conf = dsp->conf;
		mustmix = 0;
		members = 0;
		if (conf) {
			members = count_list_member(&conf->mlist);
#ifdef CMX_CONF_DEBUG
			if (conf->software && members > 1)
#else
				if (conf->software && members > 2)
#endif
					mustmix = 1;
		}

		/* transmission required */
		if (!mustmix) {
			dsp_cmx_send_member(dsp, length, mixbuffer, members);

			/*
			 * unused mixbuffer is given to prevent a
			 * potential null-pointer-bug
			 */
		}
	}

	/* loop all members that require conference mixing */
	list_for_each_entry(conf, &conf_ilist, list) {
		/* count members and check hardware */
		members = count_list_member(&conf->mlist);
#ifdef CMX_CONF_DEBUG
		if (conf->software && members > 1) {
#else
			if (conf->software && members > 2) {
#endif
				/* check for hdlc conf */
				member = list_entry(conf->mlist.next,
						    struct dsp_conf_member, list);
				if (member->dsp->hdlc)
					continue;
				/* mix all data */
				memset(mixbuffer, 0, length * sizeof(s32));
				list_for_each_entry(member, &conf->mlist, list) {
					dsp = member->dsp;
					/* get range of data to mix */
					c = mixbuffer;
					q = dsp->rx_buff;
					r = dsp->rx_R;
					rr = (r + length) & CMX_BUFF_MASK;
					/* add member's data */
					while (r != rr) {
						*c++ += dsp_audio_law_to_s32[q[r]];
						r = (r + 1) & CMX_BUFF_MASK;
					}
				}

				/* process each member */
				list_for_each_entry(member, &conf->mlist, list) {
					/* transmission */
					dsp_cmx_send_member(member->dsp, length,
							    mixbuffer, members);
				}
			}
		}

		/* delete rx-data, increment buffers, change pointers */
		list_for_each_entry(dsp, &dsp_ilist, list) {
			if (dsp->hdlc)
				continue;
			p = dsp->rx_buff;
			q = dsp->tx_buff;
			r = dsp->rx_R;
			/* move receive pointer when receiving */
			if (!dsp->rx_is_off) {
				rr = (r + length) & CMX_BUFF_MASK;
				/* delete rx-data */
				while (r != rr) {
					p[r] = dsp_silence;
					r = (r + 1) & CMX_BUFF_MASK;
				}
				/* increment rx-buffer pointer */
				dsp->rx_R = r; /* write incremented read pointer */
			}

			/* check current rx_delay */
			delay = (dsp->rx_W-dsp->rx_R) & CMX_BUFF_MASK;
			if (delay >= CMX_BUFF_HALF)
				delay = 0; /* will be the delay before next write */
			/* check for lower delay */
			if (delay < dsp->rx_delay[0])
				dsp->rx_delay[0] = delay;
			/* check current tx_delay */
			delay = (dsp->tx_W-dsp->tx_R) & CMX_BUFF_MASK;
			if (delay >= CMX_BUFF_HALF)
				delay = 0; /* will be the delay before next write */
			/* check for lower delay */
			if (delay < dsp->tx_delay[0])
				dsp->tx_delay[0] = delay;
			if (jittercheck) {
				/* find the lowest of all rx_delays */
				delay = dsp->rx_delay[0];
				i = 1;
				while (i < MAX_SECONDS_JITTER_CHECK) {
					if (delay > dsp->rx_delay[i])
						delay = dsp->rx_delay[i];
					i++;
				}
				/*
				 * remove rx_delay only if we have delay AND we
				 * have not preset cmx_delay AND
				 * the delay is greater dsp_poll
				 */
				if (delay > dsp_poll && !dsp->cmx_delay) {
					if (dsp_debug & DEBUG_DSP_CLOCK)
						printk(KERN_DEBUG
						       "%s lowest rx_delay of %d bytes for"
						       " dsp %s are now removed.\n",
						       __func__, delay,
						       dsp->name);
					r = dsp->rx_R;
					rr = (r + delay - (dsp_poll >> 1))
						& CMX_BUFF_MASK;
					/* delete rx-data */
					while (r != rr) {
						p[r] = dsp_silence;
						r = (r + 1) & CMX_BUFF_MASK;
					}
					/* increment rx-buffer pointer */
					dsp->rx_R = r;
					/* write incremented read pointer */
				}
				/* find the lowest of all tx_delays */
				delay = dsp->tx_delay[0];
				i = 1;
				while (i < MAX_SECONDS_JITTER_CHECK) {
					if (delay > dsp->tx_delay[i])
						delay = dsp->tx_delay[i];
					i++;
				}
				/*
				 * remove delay only if we have delay AND we
				 * have enabled tx_dejitter
				 */
				if (delay > dsp_poll && dsp->tx_dejitter) {
					if (dsp_debug & DEBUG_DSP_CLOCK)
						printk(KERN_DEBUG
						       "%s lowest tx_delay of %d bytes for"
						       " dsp %s are now removed.\n",
						       __func__, delay,
						       dsp->name);
					r = dsp->tx_R;
					rr = (r + delay - (dsp_poll >> 1))
						& CMX_BUFF_MASK;
					/* delete tx-data */
					while (r != rr) {
						q[r] = dsp_silence;
						r = (r + 1) & CMX_BUFF_MASK;
					}
					/* increment rx-buffer pointer */
					dsp->tx_R = r;
					/* write incremented read pointer */
				}
				/* scroll up delays */
				i = MAX_SECONDS_JITTER_CHECK - 1;
				while (i) {
					dsp->rx_delay[i] = dsp->rx_delay[i - 1];
					dsp->tx_delay[i] = dsp->tx_delay[i - 1];
					i--;
				}
				dsp->tx_delay[0] = CMX_BUFF_HALF; /* (infinite) delay */
				dsp->rx_delay[0] = CMX_BUFF_HALF; /* (infinite) delay */
			}
		}

		/* if next event would be in the past ... */
		if ((s32)(dsp_spl_jiffies + dsp_tics-jiffies) <= 0)
			dsp_spl_jiffies = jiffies + 1;
		else
			dsp_spl_jiffies += dsp_tics;

		dsp_spl_tl.expires = dsp_spl_jiffies;
		add_timer(&dsp_spl_tl);

		/* unlock */
		spin_unlock_irqrestore(&dsp_lock, flags);
	}

/*
 * audio data is transmitted from upper layer to the dsp
 */
	void
		dsp_cmx_transmit(struct dsp *dsp, struct sk_buff *skb)
	{
		u_int w, ww;
		u8 *d, *p;
		int space; /* todo: , l = skb->len; */
#ifdef CMX_TX_DEBUG
		char debugbuf[256] = "";
#endif

		/* check if there is enough space, and then copy */
		w = dsp->tx_W;
		ww = dsp->tx_R;
		p = dsp->tx_buff;
		d = skb->data;
		space = (ww - w - 1) & CMX_BUFF_MASK;
		/* write-pointer should not overrun nor reach read pointer */
		if (space < skb->len) {
			/* write to the space we have left */
			ww = (ww - 1) & CMX_BUFF_MASK; /* end one byte prior tx_R */
			if (dsp_debug & DEBUG_DSP_CLOCK)
				printk(KERN_DEBUG "%s: TX overflow space=%d skb->len="
				       "%d, w=0x%04x, ww=0x%04x\n", __func__, space,
				       skb->len, w, ww);
		} else
			/* write until all byte are copied */
			ww = (w + skb->len) & CMX_BUFF_MASK;
		dsp->tx_W = ww;

		/* show current buffer */
#ifdef CMX_DEBUG
		printk(KERN_DEBUG
		       "cmx_transmit(dsp=%lx) %d bytes to 0x%x-0x%x. %s\n",
		       (u_long)dsp, (ww - w) & CMX_BUFF_MASK, w, ww, dsp->name);
#endif

		/* copy transmit data to tx-buffer */
#ifdef CMX_TX_DEBUG
		sprintf(debugbuf, "TX getting (%04x-%04x)%p: ", w, ww, p);
#endif
		while (w != ww) {
#ifdef CMX_TX_DEBUG
			if (strlen(debugbuf) < 48)
				sprintf(debugbuf + strlen(debugbuf), " %02x", *d);
#endif
			p[w] = *d++;
			w = (w + 1) & CMX_BUFF_MASK;
		}
#ifdef CMX_TX_DEBUG
		printk(KERN_DEBUG "%s\n", debugbuf);
#endif

	}

/*
 * hdlc data is received from card and sent to all members.
 */
	void
		dsp_cmx_hdlc(struct dsp *dsp, struct sk_buff *skb)
	{
		struct sk_buff *nskb = NULL;
		struct dsp_conf_member *member;
		struct mISDNhead *hh;

		/* not if not active */
		if (!dsp->b_active)
			return;

		/* check if we have sompen */
		if (skb->len < 1)
			return;

		/* no conf */
		if (!dsp->conf) {
			/* in case of software echo */
			if (dsp->echo.software) {
				nskb = skb_clone(skb, GFP_ATOMIC);
				if (nskb) {
					hh = mISDN_HEAD_P(nskb);
					hh->prim = PH_DATA_REQ;
					hh->id = 0;
					skb_queue_tail(&dsp->sendq, nskb);
					schedule_work(&dsp->workq);
				}
			}
			return;
		}
		/* in case of hardware conference */
		if (dsp->conf->hardware)
			return;
		list_for_each_entry(member, &dsp->conf->mlist, list) {
			if (dsp->echo.software || member->dsp != dsp) {
				nskb = skb_clone(skb, GFP_ATOMIC);
				if (nskb) {
					hh = mISDN_HEAD_P(nskb);
					hh->prim = PH_DATA_REQ;
					hh->id = 0;
					skb_queue_tail(&member->dsp->sendq, nskb);
					schedule_work(&member->dsp->workq);
				}
			}
		}
	}