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
/* $Id: l3dss1.c,v 2.7 1998/02/12 23:08:01 keil Exp $

 * EURO/DSS1 D-channel protocol
 *
 * Author       Karsten Keil (keil@temic-ech.spacenet.de)
 *              based on the teles driver from Jan den Ouden
 *
 * Thanks to    Jan den Ouden
 *              Fritz Elfert
 *
 * $Log: l3dss1.c,v $
 * Revision 2.7  1998/02/12 23:08:01  keil
 * change for 2.1.86 (removing FREE_READ/FREE_WRITE from [dev]_kfree_skb()
 *
 * Revision 2.6  1998/02/03 23:26:35  keil
 * V110 extensions from Thomas Pfeiffer
 *
 * Revision 2.5  1998/02/02 13:34:28  keil
 * Support australian Microlink net and german AOCD
 *
 * Revision 2.4  1997/11/06 17:12:25  keil
 * KERN_NOTICE --> KERN_INFO
 *
 * Revision 2.3  1997/10/29 19:03:01  keil
 * changes for 2.1
 *
 * Revision 2.2  1997/08/07 17:44:36  keil
 * Fix RESTART
 *
 * Revision 2.1  1997/08/03 14:36:33  keil
 * Implement RESTART procedure
 *
 * Revision 2.0  1997/07/27 21:15:43  keil
 * New Callref based layer3
 *
 * Revision 1.17  1997/06/26 11:11:46  keil
 * SET_SKBFREE now on creation of a SKB
 *
 * Revision 1.15  1997/04/17 11:50:48  keil
 * pa->loc was undefined, if it was not send by the exchange
 *
 * Old log removed /KKe
 *
 */

#define __NO_VERSION__
#include "hisax.h"
#include "isdnl3.h"
#include "l3dss1.h"
#include <linux/ctype.h>

extern char *HiSax_getrev(const char *revision);
const char *dss1_revision = "$Revision: 2.7 $";

#define EXT_BEARER_CAPS 1

#define	MsgHead(ptr, cref, mty) \
	*ptr++ = 0x8; \
	*ptr++ = 0x1; \
	*ptr++ = cref^0x80; \
	*ptr++ = mty


#ifdef HISAX_DE_AOC
static void
l3dss1_parse_facility(struct l3_process *pc, u_char *p)
{
	int qd_len = 0;
	char tmp[32];

	p++;
	qd_len = *p++;
	if (qd_len == 0) {
		l3_debug(pc->st, "qd_len == 0");
		return;
	}
	if((*p & 0x1F) != 0x11) {	/* Service discriminator, supplementary service */
		l3_debug(pc->st, "supplementary service != 0x11");
		return;
	}
	while(qd_len > 0 && !(*p & 0x80)) {	/* extension ? */
		p++; qd_len--;
	} 
	if(qd_len < 2) {
		l3_debug(pc->st, "qd_len < 2");
		return;
	}
	p++; qd_len--;
	if((*p & 0xE0) != 0xA0) {	/* class and form */
		l3_debug(pc->st, "class and form != 0xA0");
		return;
	}
	switch(*p & 0x1F) {		/* component tag */
	    case 1: /* invoke */
		{
		    unsigned char nlen, ilen;
		    int ident;
    
		    p++; qd_len--;
		    if(qd_len < 1) {
			    l3_debug(pc->st, "qd_len < 1");
			    break;
		    }
		    if(*p & 0x80) { /* length format */
			    l3_debug(pc->st, "*p & 0x80 length format");
			    break;
		    }
		    nlen = *p++; qd_len--;
		    if(qd_len < nlen) {
			    l3_debug(pc->st, "qd_len < nlen");
			    return;
		    }
		    qd_len -= nlen;
    
		    if(nlen < 2) {
			    l3_debug(pc->st, "nlen < 2");
			    return;
		    }
		    if(*p != 0x02) {	/* invoke identifier tag */
			    l3_debug(pc->st, "invoke identifier tag !=0x02");
			    return;
		    }
		    p++; nlen--;
		    if(*p & 0x80) { /* length format */
			    l3_debug(pc->st, "*p & 0x80 length format 2");
			    break;
		    }
		    ilen = *p++; nlen--;
		    if(ilen > nlen || ilen == 0) {
			    l3_debug(pc->st, "ilen > nlen || ilen == 0");
			    return;
		    }
		    nlen -= ilen;
		    ident = 0;
		    while(ilen > 0) {
			    ident = (ident << 8) | (*p++ & 0xFF);	/* invoke identifier */
			    ilen--;
		    }
    
		    if(nlen < 2) {
			    l3_debug(pc->st, "nlen < 2 22");
			    return;
		    }
		    if(*p != 0x02)	{	/* operation value */ 
			    l3_debug(pc->st, "operation value !=0x02");
			    return;
		    }
		    p++; nlen--;
		    ilen = *p++; nlen--;
		    if(ilen > nlen || ilen == 0) {
			    l3_debug(pc->st, "ilen > nlen || ilen == 0 22");
			    return;
		    }
		    nlen -= ilen;
		    ident = 0;
		    while(ilen > 0) {
			    ident = (ident << 8) | (*p++ & 0xFF);
			    ilen--;
		    }
    
    #define FOO1(s,a,b) \
	    while(nlen > 1) {		\
		    int ilen = p[1];	\
		    if(nlen < ilen+2) {	\
			    l3_debug(pc->st, "FOO1  nlen < ilen+2"); \
			    return;		\
		    }			\
		    nlen -= ilen+2;		\
		    if((*p & 0xFF) == (a)) {	\
			    int nlen = ilen;	\
			    p += 2;		\
			    b;		\
		    } else {		\
			    p += ilen+2;	\
		    }			\
	    }
			    
		    switch(ident) {
		    default:
			    break;
		    case 0x22: /* during */
			    FOO1("1A",0x30,FOO1("1C",0xA1,FOO1("1D",0x30,FOO1("1E",0x02,({
				    ident = 0;
				    while(ilen > 0) {
					    ident = (ident<<8) | *p++;
					    ilen--;
				    }
				    if (ident > pc->para.chargeinfo) {
					    pc->para.chargeinfo = ident;
					    pc->st->l3.l3l4(pc, CC_INFO_CHARGE, NULL);
				    }
				    if (pc->st->l3.debug & L3_DEB_CHARGE) {
					    if (*(p+2) == 0) {
						    sprintf(tmp, "charging info during %d", pc->para.chargeinfo);
						    l3_debug(pc->st, tmp);
					    }
					    else {
					    sprintf(tmp, "charging info final %d", pc->para.chargeinfo);
					    l3_debug(pc->st, tmp);
					    }
				    }
			    })))))
			    break;
		    case 0x24: /* final */
			    FOO1("2A",0x30,FOO1("2B",0x30,FOO1("2C",0xA1,FOO1("2D",0x30,FOO1("2E",0x02,({
				    ident = 0;
				    while(ilen > 0) {
					    ident = (ident<<8) | *p++;
					    ilen--;
				    }
				    if (ident > pc->para.chargeinfo) {
					    pc->para.chargeinfo = ident;
					    pc->st->l3.l3l4(pc, CC_INFO_CHARGE, NULL);
				    }
				    if (pc->st->l3.debug & L3_DEB_CHARGE) {
					    sprintf(tmp, "charging info final %d", pc->para.chargeinfo);
					    l3_debug(pc->st, tmp);
				    }
			    }))))))
		    break;
		    }
    #undef FOO1
    
		}
	    break;
	    case 2: /* return result */
		    l3_debug(pc->st, "return result break");
		    break;
	    case 3: /* return error */
		    l3_debug(pc->st, "return error break");
		    break;
	    default:
		    l3_debug(pc->st, "default break");
		    break;
	}
}
#endif	

static int 
l3dss1_check_messagetype_validity(int mt) {
/* verify if a message type exists */
	switch(mt) {
		case MT_ALERTING:
		case MT_CALL_PROCEEDING:
		case MT_CONNECT:
		case MT_CONNECT_ACKNOWLEDGE:
		case MT_PROGRESS:
		case MT_SETUP:
		case MT_SETUP_ACKNOWLEDGE:
		case MT_RESUME:
		case MT_RESUME_ACKNOWLEDGE:
		case MT_RESUME_REJECT:
		case MT_SUSPEND:
		case MT_SUSPEND_ACKNOWLEDGE:
		case MT_SUSPEND_REJECT:
		case MT_USER_INFORMATION:
		case MT_DISCONNECT:
		case MT_RELEASE:
		case MT_RELEASE_COMPLETE:
		case MT_RESTART:
		case MT_RESTART_ACKNOWLEDGE:
		case MT_SEGMENT:
		case MT_CONGESTION_CONTROL:
		case MT_INFORMATION:
		case MT_FACILITY:
		case MT_NOTIFY:
		case MT_STATUS:
		case MT_STATUS_ENQUIRY:
			return(1);
		default:
			return(0);
	}
	return(0);
}

static void
l3dss1_message(struct l3_process *pc, u_char mt)
{
	struct sk_buff *skb;
	u_char *p;

	if (!(skb = l3_alloc_skb(4)))
		return;
	p = skb_put(skb, 4);
	MsgHead(p, pc->callref, mt);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
}

static void
l3dss1_release_req(struct l3_process *pc, u_char pr, void *arg)
{
	StopAllL3Timer(pc);
	newl3state(pc, 19);
	l3dss1_message(pc, MT_RELEASE);
	L3AddTimer(&pc->timer, T308, CC_T308_1);
}

static void
l3dss1_release_cmpl(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;
	int cause = -1;

	p = skb->data;
	pc->para.loc = 0;
	if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
		p++;
		if (*p++ == 2)
			pc->para.loc = *p++;
		cause = *p & 0x7f;
	}
	dev_kfree_skb(skb);
	StopAllL3Timer(pc);
	pc->para.cause = cause;
	newl3state(pc, 0);
	pc->st->l3.l3l4(pc, CC_RELEASE_CNF, NULL);
	release_l3_process(pc);
}

#ifdef EXT_BEARER_CAPS

u_char *EncodeASyncParams(u_char *p, u_char si2)
{ // 7c 06 88  90 21 42 00 bb

  p[0] = p[1] = 0; p[2] = 0x80;
  if (si2 & 32) // 7 data bits
    p[2] += 16;
  else          // 8 data bits
    p[2] +=24;

  if (si2 & 16) // 2 stop bits
    p[2] += 96;
  else          // 1 stop bit
    p[2] = 32;

  if (si2 & 8)  // even parity
    p[2] += 2;
  else          // no parity
    p[2] += 3;

  switch (si2 & 0x07)
  {
    case 0:     p[0] = 66;      // 1200 bit/s
                break;
    case 1:     p[0] = 88;      // 1200/75 bit/s
                break;
    case 2:     p[0] = 87;      // 75/1200 bit/s
                break;
    case 3:     p[0] = 67;      // 2400 bit/s
                break;
    case 4:     p[0] = 69;      // 4800 bit/s
                break;
    case 5:     p[0] = 72;      // 9600 bit/s
                break;
    case 6:     p[0] = 73;      // 14400 bit/s
                break;
    case 7:     p[0] = 75;      // 19200 bit/s
                break;
  }
  return p+3;
}

u_char EncodeSyncParams(u_char si2, u_char ai)
{

  switch (si2)
  {
    case 0:     return ai + 2;  // 1200 bit/s
    case 1:     return ai + 24; // 1200/75 bit/s
    case 2:     return ai + 23; // 75/1200 bit/s
    case 3:     return ai + 3;  // 2400 bit/s
    case 4:     return ai + 5;  // 4800 bit/s
    case 5:     return ai + 8;  // 9600 bit/s
    case 6:     return ai + 9;  // 14400 bit/s
    case 7:     return ai + 11; // 19200 bit/s
    case 8:     return ai + 14; // 48000 bit/s
    case 9:     return ai + 15; // 56000 bit/s
    case 15:    return ai + 40; // negotiate bit/s
    default:    break;
  }
  return ai;
}


static u_char DecodeASyncParams(u_char si2, u_char *p)
{ u_char info;

  switch (p[5])
  {
    case 66: // 1200 bit/s
             break; // si2 bleibt gleich
    case 88: // 1200/75 bit/s
             si2 += 1;
             break;
    case 87: // 75/1200 bit/s
             si2 += 2;
             break;
    case 67: // 2400 bit/s
             si2 += 3;
             break;
    case 69: // 4800 bit/s
             si2 += 4;
             break;
    case 72: // 9600 bit/s
             si2 += 5;
             break;
    case 73: // 14400 bit/s
             si2 += 6;
             break;
    case 75: // 19200 bit/s
             si2 += 7;
             break;
  }

  info = p[7] & 0x7f;
  if ((info & 16) && (!(info & 8)))   // 7 data bits
    si2 += 32;                        // else 8 data bits
  if ((info & 96) == 96)              // 2 stop bits
    si2 += 16;                        // else 1 stop bit
  if ((info & 2) && (!(info & 1)))    // even parity
    si2 += 8;                         // else no parity

  return si2;
}


static u_char DecodeSyncParams(u_char si2, u_char info)
{
  info &= 0x7f;
  switch (info)
  {
    case 40: // bit/s aushandeln  --- hat nicht geklappt, ai wird 165 statt 175!
      return si2 + 15;
    case 15: // 56000 bit/s --- hat nicht geklappt, ai wird 0 statt 169 !
      return si2 + 9;
    case 14: // 48000 bit/s
      return si2 + 8;
    case 11: // 19200 bit/s
      return si2 + 7;
    case 9:  // 14400 bit/s
      return si2 + 6;
    case 8:  // 9600  bit/s
      return si2 + 5;
    case 5:  // 4800  bit/s
      return si2 + 4;
    case 3:  // 2400  bit/s
      return si2 + 3;
    case 23: // 75/1200 bit/s
      return si2 + 2;
    case 24: // 1200/75 bit/s
      return si2 + 1;
    default: // 1200 bit/s
      return si2;
  }
}

static u_char DecodeSI2(struct sk_buff *skb)
{ u_char *p; //, *pend=skb->data + skb->len;

        if ((p = findie(skb->data, skb->len, 0x7c, 0)))
        {
          switch (p[4] & 0x0f)
          {
            case 0x01:  if (p[1] == 0x04) // sync. Bitratenadaption
                          return DecodeSyncParams(160, p[5]); // V.110/X.30
                        else if (p[1] == 0x06) // async. Bitratenadaption
                          return DecodeASyncParams(192, p);   // V.110/X.30
                        break;
            case 0x08:  // if (p[5] == 0x02) // sync. Bitratenadaption
                          return DecodeSyncParams(176, p[5]); // V.120
                        break;
          }
        }
        return 0;
}

#endif


static void
l3dss1_setup_req(struct l3_process *pc, u_char pr,
		 void *arg)
{
	struct sk_buff *skb;
	u_char tmp[128];
	u_char *p = tmp;
	u_char channel = 0;
	u_char screen = 0x80;
	u_char *teln;
	u_char *msn;
	u_char *sub;
	u_char *sp;
	int l;

	MsgHead(p, pc->callref, MT_SETUP);

	/*
	 * Set Bearer Capability, Map info from 1TR6-convention to EDSS1
	 */
#ifdef HISAX_EURO_SENDCOMPLETE
	*p++ = 0xa1;		/* complete indicator */
#endif
	switch (pc->para.setup.si1) {
		case 1:	/* Telephony                               */
			*p++ = 0x4;	/* BC-IE-code                              */
			*p++ = 0x3;	/* Length                                  */
			*p++ = 0x90;	/* Coding Std. CCITT, 3.1 kHz audio     */
			*p++ = 0x90;	/* Circuit-Mode 64kbps                     */
			*p++ = 0xa3;	/* A-Law Audio                             */
			break;
		case 5:	/* Datatransmission 64k, BTX               */
		case 7:	/* Datatransmission 64k                    */
		default:
			*p++ = 0x4;	/* BC-IE-code                              */
			*p++ = 0x2;	/* Length                                  */
			*p++ = 0x88;	/* Coding Std. CCITT, unrestr. dig. Inform. */
			*p++ = 0x90;	/* Circuit-Mode 64kbps                      */
			break;
	}
	/*
	 * What about info2? Mapping to High-Layer-Compatibility?
	 */
	teln = pc->para.setup.phone;
	if (*teln) {
		/* parse number for special things */
		if (!isdigit(*teln)) {
			switch (0x5f & *teln) {
				case 'C':
					channel = 0x08;
				case 'P':
					channel |= 0x80;
					teln++;
					if (*teln == '1')
						channel |= 0x01;
					else
						channel |= 0x02;
					break;
				case 'R':
					screen = 0xA0;
					break;
				case 'D':
					screen = 0x80;
					break;
				default:
					if (pc->debug & L3_DEB_WARN)
						l3_debug(pc->st, "Wrong MSN Code");
					break;
			}
			teln++;
		}
	}
	if (channel) {
		*p++ = IE_CHANNEL_ID;
		*p++ = 1;
		*p++ = channel;
	}
	msn = pc->para.setup.eazmsn;
	sub = NULL;
	sp = msn;
	while (*sp) { 
		if ('.' == *sp) {
			sub = sp;
			*sp = 0;
		} else 
			sp++;
	}
	if (*msn) {
		*p++ = 0x6c;
		*p++ = strlen(msn) + (screen ? 2 : 1);
		/* Classify as AnyPref. */
		if (screen) {
			*p++ = 0x01;	/* Ext = '0'B, Type = '000'B, Plan = '0001'B. */
			*p++ = screen;
		} else
			*p++ = 0x81;	/* Ext = '1'B, Type = '000'B, Plan = '0001'B. */
		while (*msn)
			*p++ = *msn++ & 0x7f;
	}
	if (sub) {
		*sub++ = '.';
		*p++ = 0x6d; /* Calling party subaddress */
 		*p++ = strlen(sub) + 2;
		*p++ = 0x80;	/* NSAP coded */
		*p++ = 0x50;	/* local IDI format */
 		while (*sub)
			*p++ = *sub++ & 0x7f;
	}
	sub = NULL;
	sp = teln;
	while (*sp) { 
		if ('.' == *sp) {
			sub = sp;
			*sp = 0;
		} else 
			sp++;
	}
	*p++ = 0x70;
	*p++ = strlen(teln) + 1;
	/* Classify as AnyPref. */
	*p++ = 0x81;		/* Ext = '1'B, Type = '000'B, Plan = '0001'B. */
	while (*teln)
		*p++ = *teln++ & 0x7f;

	if (sub) {
		*sub++ = '.';
		*p++ = 0x71; /* Called party subaddress */
 		*p++ = strlen(sub) + 2;
		*p++ = 0x80;	/* NSAP coded */
		*p++ = 0x50;	/* local IDI format */
 		while (*sub)
			*p++ = *sub++ & 0x7f;
	}

#ifdef EXT_BEARER_CAPS
        if ((pc->para.setup.si2 >= 160) && (pc->para.setup.si2 <= 175))
        { // sync. Bitratenadaption, V.110/X.30
          *p++ = 0x7c; *p++ = 0x04; *p++ = 0x88; *p++ = 0x90; *p++ = 0x21;
          *p++ = EncodeSyncParams(pc->para.setup.si2 - 160, 0x80);
        }
        else if ((pc->para.setup.si2 >= 176) && (pc->para.setup.si2 <= 191))
        { // sync. Bitratenadaption, V.120
          *p++ = 0x7c; *p++ = 0x05; *p++ = 0x88; *p++ = 0x90; *p++ = 0x28;
          *p++ = EncodeSyncParams(pc->para.setup.si2 - 176, 0);
          *p++ = 0x82;
        }
        else if (pc->para.setup.si2 >= 192)
        { // async. Bitratenadaption, V.110/X.30
          *p++ = 0x7c; *p++ = 0x06; *p++ = 0x88; *p++ = 0x90; *p++ = 0x21;
          p = EncodeASyncParams(p, pc->para.setup.si2 - 192);
        }
#endif
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, T303, CC_T303);
	newl3state(pc, 1);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
}

static void
l3dss1_call_proc(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;

	L3DelTimer(&pc->timer);
	p = skb->data;
	if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
		pc->para.bchannel = p[2] & 0x3;
		if ((!pc->para.bchannel) && (pc->debug & L3_DEB_WARN))
			l3_debug(pc->st, "setup answer without bchannel");
	} else if (pc->debug & L3_DEB_WARN)
		l3_debug(pc->st, "setup answer without bchannel");
	dev_kfree_skb(skb);
	newl3state(pc, 3);
	L3AddTimer(&pc->timer, T310, CC_T310);
	pc->st->l3.l3l4(pc, CC_PROCEEDING_IND, NULL);
}

static void
l3dss1_setup_ack(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;

	L3DelTimer(&pc->timer);
	p = skb->data;
	if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
		pc->para.bchannel = p[2] & 0x3;
		if ((!pc->para.bchannel) && (pc->debug & L3_DEB_WARN))
			l3_debug(pc->st, "setup answer without bchannel");
	} else if (pc->debug & L3_DEB_WARN)
		l3_debug(pc->st, "setup answer without bchannel");
	dev_kfree_skb(skb);
	newl3state(pc, 2);
	L3AddTimer(&pc->timer, T304, CC_T304);
	pc->st->l3.l3l4(pc, CC_MORE_INFO, NULL);
}

static void
l3dss1_disconnect(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;
	int cause = -1;

	StopAllL3Timer(pc);
	p = skb->data;
	pc->para.loc = 0;
	if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
		p++;
		if (*p++ == 2)
			pc->para.loc = *p++;
		cause = *p & 0x7f;
	}
	dev_kfree_skb(skb);
	newl3state(pc, 12);
	pc->para.cause = cause;
	pc->st->l3.l3l4(pc, CC_DISCONNECT_IND, NULL);
}

static void
l3dss1_connect(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;

	dev_kfree_skb(skb);
	L3DelTimer(&pc->timer);	/* T310 */
	newl3state(pc, 10);
	pc->para.chargeinfo = 0;
	pc->st->l3.l3l4(pc, CC_SETUP_CNF, NULL);
}

static void
l3dss1_alerting(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;

	dev_kfree_skb(skb);
	L3DelTimer(&pc->timer);	/* T304 */
	newl3state(pc, 4);
	pc->st->l3.l3l4(pc, CC_ALERTING_IND, NULL);
}

static void
l3dss1_msg_without_setup(struct l3_process *pc, u_char pr, void *arg)
{
  /* This routine is called if here was no SETUP made (checks in dss1up and in
   * l3dss1_setup) and a RELEASE_COMPLETE have to be sent with an error code
   * It is called after it is veryfied that Layer2 is up.
   * The cause value is allready in pc->para.cause
   * MT_STATUS_ENQUIRE in the NULL state is handled too
   */
	u_char tmp[16];
	u_char *p=tmp;
	int l;
	struct sk_buff *skb;

	switch (pc->para.cause) {
	  case  81: /* 0x51 invalid callreference */
	  case  96: /* 0x60 mandory IE missing */
	  case 101: /* 0x65 incompatible Callstate */
	  	MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);
		*p++ = IE_CAUSE;
		*p++ = 0x2;
		*p++ = 0x80;
		*p++ = pc->para.cause | 0x80;
		break;
	  default:
	  	printk(KERN_ERR "HiSax internal error l3dss1_msg_without_setup\n");
	  	return;
	}	
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
	release_l3_process(pc);
}

static void
l3dss1_setup(struct l3_process *pc, u_char pr, void *arg)
{
        u_char *p, *ptmp[8];
	int i;
	int bcfound = 0;
	char tmp[80];
	struct sk_buff *skb = arg;

	/* ETS 300-104 1.3.4 and 1.3.5
	 * we need to detect unknown inform. element from 0 to 7
	 */	
	p = skb->data;
	for(i = 0; i < 8; i++)
		ptmp[i] = skb->data;
	if (findie(ptmp[1], skb->len, 0x01, 0)
	    || findie(ptmp[2], skb->len, 0x02, 0)
	    || findie(ptmp[3], skb->len, 0x03, 0)
	    || findie(ptmp[5], skb->len, 0x05, 0)
	    || findie(ptmp[6], skb->len, 0x06, 0)
	    || findie(ptmp[7], skb->len, 0x07, 0)) {
	  	/* if ie is < 8 and not 0 nor 4, send RELEASE_COMPLETE 
	  	 * cause 0x60
	  	 */
	  	pc->para.cause = 0x60;
		dev_kfree_skb(skb);
		if (pc->state == 0)
			pc->st->l3.l3l4(pc, CC_ESTABLISH, NULL);
		else
			l3dss1_msg_without_setup(pc, pr, NULL);
		return;
	}

	/*
	 * Channel Identification
	 */
	p = skb->data;
	if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
		pc->para.bchannel = p[2] & 0x3;
		if (pc->para.bchannel)
			bcfound++;
		else if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "setup without bchannel");
	} else if (pc->debug & L3_DEB_WARN)
		l3_debug(pc->st, "setup without bchannel");

	/*
	   * Bearer Capabilities
	 */
	p = skb->data;
	if ((p = findie(p, skb->len, 0x04, 0))) {
		pc->para.setup.si2 = 0;
		switch (p[2] & 0x1f) {
			case 0x00:
				/* Speech */
			case 0x10:
				/* 3.1 Khz audio */
				pc->para.setup.si1 = 1;
				break;
			case 0x08:
				/* Unrestricted digital information */
				pc->para.setup.si1 = 7;
/* JIM, 05.11.97 I wanna set service indicator 2 */
#ifdef EXT_BEARER_CAPS
                                pc->para.setup.si2 = DecodeSI2(skb);
                                printk(KERN_DEBUG "HiSax: SI=%d, AI=%d\n",
                                       pc->para.setup.si1, pc->para.setup.si2);
#endif
				break;
			case 0x09:
				/* Restricted digital information */
				pc->para.setup.si1 = 2;
				break;
			case 0x11:
				/* Unrestr. digital information  with tones/announcements */
				pc->para.setup.si1 = 3;
				break;
			case 0x18:
				/* Video */
				pc->para.setup.si1 = 4;
				break;
			default:
				pc->para.setup.si1 = 0;
		}
	} else {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "setup without bearer capabilities");
		/* ETS 300-104 1.3.3 */
	  	pc->para.cause = 0x60;
		dev_kfree_skb(skb);
		if (pc->state == 0)
			pc->st->l3.l3l4(pc, CC_ESTABLISH, NULL);
		else
			l3dss1_msg_without_setup(pc, pr, NULL);
		return;
	}

	p = skb->data;
	if ((p = findie(p, skb->len, 0x70, 0)))
		iecpy(pc->para.setup.eazmsn, p, 1);
	else
		pc->para.setup.eazmsn[0] = 0;

	p = skb->data;
	if ((p = findie(p, skb->len, 0x71, 0))) {
		/* Called party subaddress */
		if ((p[1]>=2) && (p[2]==0x80) && (p[3]==0x50)) {
			tmp[0]='.';
			iecpy(&tmp[1], p, 2);
			strcat(pc->para.setup.eazmsn, tmp);
		} else if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "wrong called subaddress");
	}
	p = skb->data;
	if ((p = findie(p, skb->len, 0x6c, 0))) {
		pc->para.setup.plan = p[2];
		if (p[2] & 0x80) {
			iecpy(pc->para.setup.phone, p, 1);
			pc->para.setup.screen = 0;
		} else {
			iecpy(pc->para.setup.phone, p, 2);
			pc->para.setup.screen = p[3];
		}
	} else {
		pc->para.setup.phone[0] = 0;
		pc->para.setup.plan = 0;
		pc->para.setup.screen = 0;
	}
	p = skb->data;
	if ((p = findie(p, skb->len, 0x6d, 0))) {
		/* Calling party subaddress */
		if ((p[1]>=2) && (p[2]==0x80) && (p[3]==0x50)) {
			tmp[0]='.';
			iecpy(&tmp[1], p, 2);
			strcat(pc->para.setup.phone, tmp);
		} else if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "wrong calling subaddress");
	}

	dev_kfree_skb(skb);

	if (bcfound) {
		if ((pc->para.setup.si1 != 7) && (pc->debug & L3_DEB_WARN)) {
			sprintf(tmp, "non-digital call: %s -> %s",
				pc->para.setup.phone, pc->para.setup.eazmsn);
			l3_debug(pc->st, tmp);
		}
		newl3state(pc, 6);
		pc->st->l3.l3l4(pc, CC_SETUP_IND, NULL);
	} else
		release_l3_process(pc);
}

static void
l3dss1_reset(struct l3_process *pc, u_char pr, void *arg)
{
	release_l3_process(pc);
}

static void
l3dss1_setup_rsp(struct l3_process *pc, u_char pr,
		 void *arg)
{
	newl3state(pc, 8);
	l3dss1_message(pc, MT_CONNECT);
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, T313, CC_T313);
}

static void
l3dss1_connect_ack(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;

	dev_kfree_skb(skb);
	newl3state(pc, 10);
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc, CC_SETUP_COMPLETE_IND, NULL);
}

static void
l3dss1_disconnect_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	u_char cause = 0x10;

	if (pc->para.cause > 0)
		cause = pc->para.cause;

	StopAllL3Timer(pc);

	MsgHead(p, pc->callref, MT_DISCONNECT);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause | 0x80;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	newl3state(pc, 11);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
	L3AddTimer(&pc->timer, T305, CC_T305);
}

static void
l3dss1_reject_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	u_char cause = 0x95;

	if (pc->para.cause > 0)
		cause = pc->para.cause;

	MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
	pc->st->l3.l3l4(pc, CC_RELEASE_IND, NULL);
	newl3state(pc, 0);
	release_l3_process(pc);
}

static void
l3dss1_release(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;
	int cause = -1;

	p = skb->data;
	if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
		p++;
		if (*p++ == 2)
			pc->para.loc = *p++;
		cause = *p & 0x7f;
	}
	p = skb->data;
	if ((p = findie(p, skb->len, IE_FACILITY, 0))) {
#ifdef HISAX_DE_AOC
	    l3dss1_parse_facility(pc,p);
#else
		p = NULL;
#endif
	}
	dev_kfree_skb(skb);
	StopAllL3Timer(pc);
	pc->para.cause = cause;
	l3dss1_message(pc, MT_RELEASE_COMPLETE);
	pc->st->l3.l3l4(pc, CC_RELEASE_IND, NULL);
	newl3state(pc, 0);
	release_l3_process(pc);
}

static void
l3dss1_alert_req(struct l3_process *pc, u_char pr,
		 void *arg)
{
	newl3state(pc, 7);
	l3dss1_message(pc, MT_ALERTING);
}

static void
l3dss1_status_enq(struct l3_process *pc, u_char pr, void *arg)
{
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	struct sk_buff *skb = arg;

	dev_kfree_skb(skb);

	MsgHead(p, pc->callref, MT_STATUS);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = 0x9E;		/* answer status enquire */

	*p++ = 0x14;		/* CallState */
	*p++ = 0x1;
	*p++ = pc->state & 0x3f;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
}

static void
l3dss1_status_req(struct l3_process *pc, u_char pr, void *arg)
{
  /* ETS 300-104 7.4.1, 8.4.1, 10.3.1, 11.4.1, 12.4.1, 13.4.1, 14.4.1...
     if setup has been made and a non expected message type is received, we must send MT_STATUS cause 0x62  */
        u_char tmp[16];
	u_char *p = tmp;
	int l;
	struct sk_buff *skb = arg;

	dev_kfree_skb(skb);

	MsgHead(p, pc->callref, MT_STATUS);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = 0x62 | 0x80;		/* status sending */

	*p++ = 0x14;		/* CallState */
	*p++ = 0x1;
	*p++ = pc->state & 0x3f;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
}

static void
l3dss1_release_ind(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;
	int callState = 0;
	p = skb->data;

	if ((p = findie(p, skb->len, IE_CALL_STATE, 0))) {
		p++;
		if (1== *p++)
			callState = *p;
	}
	if(callState == 0) {
		/* ETS 300-104 7.6.1, 8.6.1, 10.6.1... and 16.1
		 * set down layer 3 without sending any message
		 */
		pc->st->l3.l3l4(pc, CC_RELEASE_IND, NULL);
		newl3state(pc, 0);
		release_l3_process(pc);
	} else {
		pc->st->l3.l3l4(pc, CC_IGNORE, NULL);
	}
}

static void
l3dss1_t303(struct l3_process *pc, u_char pr, void *arg)
{
	if (pc->N303 > 0) {
		pc->N303--;
		L3DelTimer(&pc->timer);
		l3dss1_setup_req(pc, pr, arg);
	} else {
		L3DelTimer(&pc->timer);
		pc->st->l3.l3l4(pc, CC_NOSETUP_RSP_ERR, NULL);
		release_l3_process(pc);
	}
}

static void
l3dss1_t304(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.cause = 0xE6;
	l3dss1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc, CC_SETUP_ERR, NULL);

}

static void
l3dss1_t305(struct l3_process *pc, u_char pr, void *arg)
{
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	struct sk_buff *skb;
	u_char cause = 0x90;

	L3DelTimer(&pc->timer);
	if (pc->para.cause > 0)
		cause = pc->para.cause | 0x80;

	MsgHead(p, pc->callref, MT_RELEASE);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	newl3state(pc, 19);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
	L3AddTimer(&pc->timer, T308, CC_T308_1);
}

static void
l3dss1_t310(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.cause = 0xE6;
	l3dss1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc, CC_SETUP_ERR, NULL);
}

static void
l3dss1_t313(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.cause = 0xE6;
	l3dss1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc, CC_CONNECT_ERR, NULL);
}

static void
l3dss1_t308_1(struct l3_process *pc, u_char pr, void *arg)
{
	newl3state(pc, 19);
	L3DelTimer(&pc->timer);
	l3dss1_message(pc, MT_RELEASE);
	L3AddTimer(&pc->timer, T308, CC_T308_2);
}

static void
l3dss1_t308_2(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc, CC_RELEASE_ERR, NULL);
	release_l3_process(pc);
}

static void
l3dss1_restart(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc, CC_DLRL, NULL);
	release_l3_process(pc);
}

static void
l3dss1_status(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	char tmp[64], *t;
	int l;
	struct sk_buff *skb = arg;
	int cause, callState;

	cause = callState = -1;
	p = skb->data;
	t = tmp;
	if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
		p++;
		l = *p++;
		t += sprintf(t,"Status CR %x Cause:", pc->callref);
		while (l--) {
		        cause = *p;
			t += sprintf(t," %2x",*p++);
		}
	} else
		sprintf(t,"Status CR %x no Cause", pc->callref);
	l3_debug(pc->st, tmp);
	p = skb->data;
	t = tmp;
	t += sprintf(t,"Status state %x ", pc->state);
	if ((p = findie(p, skb->len, IE_CALL_STATE, 0))) {
		p++;
		if (1== *p++) {
		        callState = *p;
			t += sprintf(t,"peer state %x" , *p);
		}
		else
			t += sprintf(t,"peer state len error");
	} else
		sprintf(t,"no peer state");
	l3_debug(pc->st, tmp);
	if(((cause & 0x7f) == 0x6f) && (callState == 0)) {
		/* ETS 300-104 7.6.1, 8.6.1, 10.6.1... 
		 * if received MT_STATUS with cause == 0x6f and call 
		 * state == 0, then we must set down layer 3
		 */
		l3dss1_release_ind(pc, pr, arg);
	} else
		dev_kfree_skb(skb);
}

static void
l3dss1_facility(struct l3_process *pc, u_char pr, void *arg)
{
        u_char *p;
	struct sk_buff *skb = arg;

	p = skb->data;
	if ((p = findie(p, skb->len, IE_FACILITY, 0))) {
#ifdef HISAX_DE_AOC
	    l3dss1_parse_facility(pc,p);
#else
		p = NULL;
#endif
	}
}


	
static void
l3dss1_global_restart(struct l3_process *pc, u_char pr, void *arg)
{
	u_char tmp[32];
	u_char *p;
	u_char ri, chan=0;
	int l;
	struct sk_buff *skb = arg;
	struct l3_process *up;
	
	newl3state(pc, 2);
	L3DelTimer(&pc->timer);
	p = skb->data;
	if ((p = findie(p, skb->len, IE_RESTART_IND, 0))) {
	        ri = p[2];
	        sprintf(tmp, "Restart %x", ri);
	} else {
		sprintf(tmp, "Restart without restart IE");
		ri = 0x86;
	}
	l3_debug(pc->st, tmp);
	p = skb->data;
	if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
		chan = p[2] & 3;
		sprintf(tmp, "Restart for channel %d", chan);
		l3_debug(pc->st, tmp);
	}
	dev_kfree_skb(skb);
	newl3state(pc, 2);
	up = pc->st->l3.proc;
	while (up) {
		if ((ri & 7)==7)
			up->st->lli.l4l3(up->st, CC_RESTART, up);
		else if (up->para.bchannel == chan)
				up->st->lli.l4l3(up->st, CC_RESTART, up);
		up = up->next;
	}
	p = tmp;
	MsgHead(p, pc->callref, MT_RESTART_ACKNOWLEDGE);
	if (chan) {
		*p++ = IE_CHANNEL_ID;
		*p++ = 1;
		*p++ = chan | 0x80;
	}
	*p++ = 0x79; /* RESTART Ind */
	*p++ = 1;
	*p++ = ri;
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	newl3state(pc, 0);
	pc->st->l3.l3l2(pc->st, DL_DATA, skb);
}

/* *INDENT-OFF* */
static struct stateentry downstatelist[] =
{
	{SBIT(0),
	 CC_ESTABLISH, l3dss1_msg_without_setup},
	{SBIT(0),
	 CC_SETUP_REQ, l3dss1_setup_req},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(6) | SBIT(7) | SBIT(8) | SBIT(10),
	 CC_DISCONNECT_REQ, l3dss1_disconnect_req},
	{SBIT(12),
	 CC_RELEASE_REQ, l3dss1_release_req},
	{ALL_STATES,
	 CC_DLRL, l3dss1_reset},
	{ALL_STATES,
	 CC_RESTART, l3dss1_restart},
	{SBIT(6),
	 CC_IGNORE, l3dss1_reset},
	{SBIT(6),
	 CC_REJECT_REQ, l3dss1_reject_req},
	{SBIT(6),
	 CC_ALERTING_REQ, l3dss1_alert_req},
	{SBIT(6) | SBIT(7),
	 CC_SETUP_RSP, l3dss1_setup_rsp},
	{SBIT(1),
	 CC_T303, l3dss1_t303},
	{SBIT(2),
	 CC_T304, l3dss1_t304},
	{SBIT(3),
	 CC_T310, l3dss1_t310},
	{SBIT(8),
	 CC_T313, l3dss1_t313},
	{SBIT(11),
	 CC_T305, l3dss1_t305},
	{SBIT(19),
	 CC_T308_1, l3dss1_t308_1},
	{SBIT(19),
	 CC_T308_2, l3dss1_t308_2},
};

static int downsllen = sizeof(downstatelist) /
sizeof(struct stateentry);

static struct stateentry datastatelist[] =
{
	{ALL_STATES,
	 MT_STATUS_ENQUIRY, l3dss1_status_enq},
	{ALL_STATES,
	 MT_FACILITY, l3dss1_facility},
	{SBIT(19),
	 MT_STATUS, l3dss1_release_ind},
	{ALL_STATES,
	 MT_STATUS, l3dss1_status},
	{SBIT(0) | SBIT(6),
	 MT_SETUP, l3dss1_setup},
	{SBIT(1) | SBIT(2),
	 MT_CALL_PROCEEDING, l3dss1_call_proc},
	{SBIT(3) | SBIT(4) | SBIT(8) | SBIT(10) | SBIT(11) | SBIT(19),
	 MT_CALL_PROCEEDING, l3dss1_status_req},
	{SBIT(1),
	 MT_SETUP_ACKNOWLEDGE, l3dss1_setup_ack},
	{SBIT(2) | SBIT(3) | SBIT(4) | SBIT(8) | SBIT(10) | SBIT(11) | SBIT(19),
	 MT_SETUP_ACKNOWLEDGE, l3dss1_status_req},
	{SBIT(1) | SBIT(2) | SBIT(3),
	 MT_ALERTING, l3dss1_alerting},
	{SBIT(4) | SBIT(8) | SBIT(10) | SBIT(11) | SBIT(19),
	 MT_ALERTING, l3dss1_status_req},
	{SBIT(0) | SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(10) |
	 SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(19),
	 MT_RELEASE_COMPLETE, l3dss1_release_cmpl},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(10) |
	 SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) /*| SBIT(19)*/,
	 MT_RELEASE, l3dss1_release},
	{SBIT(19),  MT_RELEASE, l3dss1_release_ind},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(10),
	 MT_DISCONNECT, l3dss1_disconnect},
	{SBIT(11),
	 MT_DISCONNECT, l3dss1_release_req},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4),
	 MT_CONNECT, l3dss1_connect},
	{SBIT(8) | SBIT(10) | SBIT(11) | SBIT(19),
	 MT_CONNECT, l3dss1_status_req},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(11) | SBIT(19),
	 MT_CONNECT_ACKNOWLEDGE, l3dss1_status_req},
	{SBIT(8),
	 MT_CONNECT_ACKNOWLEDGE, l3dss1_connect_ack},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(8) | SBIT(10) | SBIT(11) | SBIT(19),
	 MT_INVALID, l3dss1_status_req},
};

static int datasllen = sizeof(datastatelist) / sizeof(struct stateentry);

static struct stateentry globalmes_list[] =
{
	{ALL_STATES,
         MT_STATUS, l3dss1_status},
	{SBIT(0),
	 MT_RESTART, l3dss1_global_restart},
/*	{SBIT(1),
	 MT_RESTART_ACKNOWLEDGE, l3dss1_restart_ack},                                  
*/
};
static int globalm_len = sizeof(globalmes_list) / sizeof(struct stateentry);

#if 0
static struct stateentry globalcmd_list[] =
{
	{ALL_STATES,
         CC_STATUS, l3dss1_status_req},
	{SBIT(0),
	 CC_RESTART, l3dss1_restart_req},
};

static int globalc_len = sizeof(globalcmd_list) / sizeof(struct stateentry);
#endif
/* *INDENT-ON* */

static void
global_handler(struct PStack *st, int mt, struct sk_buff *skb)
{
	int i;
	char tmp[64];
	struct l3_process *proc = st->l3.global;
	
	for (i = 0; i < globalm_len; i++)
		if ((mt == globalmes_list[i].primitive) &&
		    ((1 << proc->state) & globalmes_list[i].state))
			break;
	if (i == globalm_len) {
		dev_kfree_skb(skb);
		if (st->l3.debug & L3_DEB_STATE) {
			sprintf(tmp, "dss1 global state %d mt %x unhandled",
				proc->state, mt);
			l3_debug(st, tmp);
		}
		return;
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			sprintf(tmp, "dss1 global %d mt %x",
				proc->state, mt);
			l3_debug(st, tmp);
		}
		globalmes_list[i].rout(proc, mt, skb);
	}
}

static void
dss1up(struct PStack *st, int pr, void *arg)
{
	int i, mt, cr, cause, callState;
	char *ptr;
	struct sk_buff *skb = arg;
	struct l3_process *proc;
	char tmp[80];

	if (skb->data[0] != PROTO_DIS_EURO) {
		if (st->l3.debug & L3_DEB_PROTERR) {
			sprintf(tmp, "dss1up%sunexpected discriminator %x message len %d",
				(pr == DL_DATA) ? " " : "(broadcast) ",
				skb->data[0], skb->len);
			l3_debug(st, tmp);
		}
		dev_kfree_skb(skb);
		return;
	}
	cr = getcallref(skb->data);
	mt = skb->data[skb->data[1] + 2];
	if (!cr) {				/* Global CallRef */
		global_handler(st, mt, skb);
		return;
	} else if (cr == -1) {			/* Dummy Callref */
		dev_kfree_skb(skb);
		return;
	} else if (!(proc = getl3proc(st, cr))) {
		/* No transaction process exist, that means no call with
		 * this callreference is active
		 */
		if (mt == MT_SETUP) {
		/* Setup creates a new transaction process */
			if (!(proc = new_l3_process(st, cr))) {
				/* May be to answer with RELEASE_COMPLETE and
				 * CAUSE 0x2f "Resource unavailable", but this
				 * need a new_l3_process too ... arghh
				 */
				dev_kfree_skb(skb);
				return;
			}
		} else if (mt == MT_STATUS) {
			cause = 0;
			if((ptr = findie(skb->data, skb->len, IE_CAUSE, 0)) != NULL) {
				  ptr++;
				  if (*ptr++ == 2)
				  	ptr++;
				  cause = *ptr & 0x7f;
			}
			callState = 0;
			if((ptr = findie(skb->data, skb->len, IE_CALL_STATE, 0)) != NULL) {
				ptr++;
				if (*ptr++ == 2)
					ptr++;
				callState = *ptr;
			}
			if (callState == 0) {
				/* ETS 300-104 part 2.4.1
				 * if setup has not been made and a message type
				 * MT_STATUS is received with call state == 0,
				 * we must send nothing
				 */
				dev_kfree_skb(skb);
				return;
			} else {
				/* ETS 300-104 part 2.4.2
				 * if setup has not been made and a message type 
				 * MT_STATUS is received with call state != 0,
				 * we must send MT_RELEASE_COMPLETE cause 101
				 */
				dev_kfree_skb(skb);
				if ((proc = new_l3_process(st, cr))) {
					proc->para.cause = 0x65; /* 101 */
					proc->st->l3.l3l4(proc, CC_ESTABLISH, NULL);
				}
				return;
			}
		} else if (mt == MT_RELEASE_COMPLETE){
			dev_kfree_skb(skb);
			return;
		} else {
			/* ETS 300-104 part 2
			 * if setup has not been made and a message type 
			 * (except MT_SETUP and RELEASE_COMPLETE) is received,
			 * we must send MT_RELEASE_COMPLETE cause 81 */
			dev_kfree_skb(skb);
			if ((proc = new_l3_process(st, cr))) {
				proc->para.cause = 0x51; /* 81 */
				proc->st->l3.l3l4(proc, CC_ESTABLISH, NULL);
			}
			return;
		}
	} else if (!l3dss1_check_messagetype_validity(mt)) {
		/* ETS 300-104 7.4.2, 8.4.2, 10.3.2, 11.4.2, 12.4.2, 13.4.2,
		 * 14.4.2...
		 * if setup has been made and invalid message type is received,
		 * we must send MT_STATUS cause 0x62
		 */
		mt = MT_INVALID;  /* sorry, not clean, but do the right thing ;-) */
	}

	for (i = 0; i < datasllen; i++)
		if ((mt == datastatelist[i].primitive) &&
		    ((1 << proc->state) & datastatelist[i].state))
			break;
	if (i == datasllen) {
		dev_kfree_skb(skb);
		if (st->l3.debug & L3_DEB_STATE) {
			sprintf(tmp, "dss1up%sstate %d mt %x unhandled",
				(pr == DL_DATA) ? " " : "(broadcast) ",
				proc->state, mt);
			l3_debug(st, tmp);
		}
		return;
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			sprintf(tmp, "dss1up%sstate %d mt %x",
				(pr == DL_DATA) ? " " : "(broadcast) ",
				proc->state, mt);
			l3_debug(st, tmp);
		}
		datastatelist[i].rout(proc, pr, skb);
	}
}

static void
dss1down(struct PStack *st, int pr, void *arg)
{
	int i, cr;
	struct l3_process *proc;
	struct Channel *chan;
	char tmp[80];

	if (CC_SETUP_REQ == pr) {
		chan = arg;
		cr = newcallref();
		cr |= 0x80;
		if ((proc = new_l3_process(st, cr))) {
			proc->chan = chan;
			chan->proc = proc;
			proc->para.setup = chan->setup;
			proc->callref = cr;
		}
	} else {
		proc = arg;
	}
	if (!proc) {
		printk(KERN_ERR "HiSax internal error dss1down without proc\n");
		return;
	}
	for (i = 0; i < downsllen; i++)
		if ((pr == downstatelist[i].primitive) &&
		    ((1 << proc->state) & downstatelist[i].state))
			break;
	if (i == downsllen) {
		if (st->l3.debug & L3_DEB_STATE) {
			sprintf(tmp, "dss1down state %d prim %d unhandled",
				proc->state, pr);
			l3_debug(st, tmp);
		}
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			sprintf(tmp, "dss1down state %d prim %d",
				proc->state, pr);
			l3_debug(st, tmp);
		}
		downstatelist[i].rout(proc, pr, arg);
	}
}

void
setstack_dss1(struct PStack *st)
{
	char tmp[64];

	st->lli.l4l3 = dss1down;
	st->l2.l2l3 = dss1up;
	st->l3.N303 = 1;
	if (!(st->l3.global = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
		printk(KERN_ERR "HiSax can't get memory for dss1 global CR\n");
	} else {
		st->l3.global->state = 0;
		st->l3.global->callref = 0;
		st->l3.global->next = NULL;
		st->l3.global->debug = L3_DEB_WARN;
		st->l3.global->st = st;
		st->l3.global->N303 = 1;
		L3InitTimer(st->l3.global, &st->l3.global->timer);
	}
	strcpy(tmp, dss1_revision);
	printk(KERN_INFO "HiSax: DSS1 Rev. %s\n", HiSax_getrev(tmp));
}