summaryrefslogtreecommitdiffstats
path: root/drivers/char/scc.c
blob: 81cbd9e5e1ed62e8fd1b9f278d58fcf835fa3cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
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
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
#include <linux/autoconf.h>	/* fastest method */
#ifdef CONFIG_SCC

#define RCS_ID "$Id: scc.c,v 1.17 1995/03/15 23:28:12 JReuter Exp JReuter $"

#define BANNER "Z8530 SCC driver v1.8.dl1bke (beta) by dl1bke\n"

/* ******************************************************************** */
/* *   SCC.C - Linux driver for Z8530 based HDLC cards for AX.25      *	*/ 
/* ******************************************************************** */

/* ********************************************************************

	(c) 1995 by Joerg Reuter DL1BKE

	portions (c) 1994 Hans Alblas PE1AYX 
	and      (c) 1993 Guido ten Dolle PE1NNZ

	for the complete copyright notice see >> Copying.Z8530DRV <<

   ********************************************************************	*/   

/*

   940913	- started to rewrite the drive
   950131	- changed copyright notice to GPL without limitations.
   950228	- (hopefully) fixed the reason for kernel panics in
                  chk_rcv_queue() [stupid error]
   950304       - fixed underrun/zcount handling
   950305	- the driver registers port addresses now
   950314	- fixed underrun interrupt handling again
   
   Thanks to:
   
   PE1CHL Rob	- for a lot of good ideas from his SCC driver for DOS
   PE1NNZ Guido - for his port of the original driver to Linux
   KA9Q   Phil  - from whom we stole the mbuf-structure
   PA3AYX Hans  - who rewrote the memory management and some minor,
                  but nevertheless useful changes
   DL8MBT Flori - for support
   DG0FT  Rene  - for the BayCom USCC support
   PA3AOU Harry - for ESCC testing, information supply and support
   
   PE1KOX Rob, DG1RTF Thomas, ON5QK Roland, 
   
   and all who sent me bug reports and ideas... 
   
   
   NB -- if you find errors, change something, please let me know
      	 first before you distribute it... And please don't touch
   	 the version number. Just replace my callsign in
   	 "v1.8.dl1bke" with your own. Just to avoid confusion...
   	  
   Jörg Reuter DL1BKE
   
*/


#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/tqueue.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/termios.h>
#include <linux/serial.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/config.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/malloc.h>
#include <linux/scc.h>
#include <linux/delay.h>
#include "scc_config.h"

#include <asm/system.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <asm/bitops.h>

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <linux/kernel.h>


long scc_init(long kmem_start);

int scc_open(struct tty_struct *tty, struct file *filp);
static void scc_close(struct tty_struct *tty, struct file *filp);
int scc_write(struct tty_struct *tty, int from_user, unsigned char *buf, int count);
static void scc_put_char(struct tty_struct *tty, unsigned char ch);
static void scc_flush_chars(struct tty_struct *tty);
static int scc_write_room(struct tty_struct *tty);
static int scc_chars_in_buffer(struct tty_struct *tty);
static void scc_flush_buffer(struct tty_struct *tty);
static int scc_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
static void scc_set_termios(struct tty_struct *tty, struct termios *old_termios);
static void scc_throttle(struct tty_struct *tty);
static void scc_unthrottle(struct tty_struct *tty);
static void scc_start(struct tty_struct *tty);
static void scc_stop(struct tty_struct *tty);

static void z8530_init(void);

static void scc_change_speed(struct scc_channel *scc);
static void kiss_encode(struct scc_channel *scc);

static void init_channel(struct scc_channel *scc);
static void scc_key_trx (struct scc_channel *scc, char tx);
static void scc_txint(register struct scc_channel *scc);
static void scc_exint(register struct scc_channel *scc);
static void scc_rxint(register struct scc_channel *scc);
static void scc_spint(register struct scc_channel *scc);
static void scc_isr(int irq, struct pt_regs *regs);
static void scc_timer(void);
static void scc_init_timer(struct scc_channel *scc);

/* from serial.c */

static int baud_table[] = {
	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
	9600, 19200, 38400, 57600, 115200, 0 };


struct tty_driver scc_driver;		    /* new in 1.1.xx */
static int scc_refcount;
static struct tty_struct *scc_table[2*MAXSCC];
static struct termios scc_termios[2 * MAXSCC];
static struct termios scc_termios_locked[2 * MAXSCC];
	
struct scc_channel SCC_Info[2 * MAXSCC];         /* information per channel */

unsigned char Random = 0;		/* random number for p-persist */
unsigned char Driver_Initialized = 0;
static struct sccbuf *sccfreelist[MAX_IBUFS] = {0};
static int allocated_ibufs = 0;


/* ******************************************************************** */
/* *			Port Access Functions			      * */
/* ******************************************************************** */

static inline unsigned char
InReg(register io_port port, register unsigned char reg)
{
#ifdef SCC_LDELAY
	register unsigned char r;
	Outb(port, reg);
	udelay(5);
	r=Inb(port);
	udelay(5);
	return r;
#else
	Outb(port, reg);
	return Inb(port);
#endif
}

static inline void
OutReg(register io_port port, register unsigned char reg, register unsigned char val)
{
#ifdef SCC_LDELAY
	Outb(port, reg); udelay(5);
	Outb(port, val); udelay(5);
#else
	Outb(port, reg);
	Outb(port, val);
#endif
}

static inline void
wr(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
{
	OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));
}

static inline void
or(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
{
	OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));
}

static inline void
cl(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
{
	OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));
}

/* ******************************************************************** */
/* * 			Memory Buffer Management			*/
/* ******************************************************************** */

/* mbuf concept lent from KA9Q. Tnx PE1AYX for the buffer pool concept	*/
/* (sorry, do you have any better ideas?) */


/* allocate memory for the interrupt buffer pool */

void scc_alloc_buffer_pool(void)
{
	int i;
	struct sccbuf *sccb;
	struct mbuf   *bp;
	
   	for (i = 0 ; i < MAX_IBUFS ; i++)
   	{
   		sccb = (struct sccbuf *)kmalloc(sizeof(struct sccbuf), GFP_ATOMIC);
   		bp = (struct mbuf *)kmalloc(sizeof(struct mbuf), GFP_ATOMIC);
   		
		if ( !(sccb && bp) )
		{
			allocated_ibufs = --i;
			
			if (allocated_ibufs < 0)
				panic("scc_alloc_buffer_pool() - can't get buffer space");
			else
				printk("Warning: scc_alloc_buffer_pool() - allocated only %i buffers\n",i);
				
			return;
		}
		
		sccfreelist[i] = sccb;
		sccfreelist[i]->bp = bp;
		memset(sccfreelist[i]->bp ,0,sizeof(struct mbuf));
		sccfreelist[i]->inuse = 0;
		sccfreelist[i]->bp->type = 0;
		sccfreelist[i]->bp->refcnt = 0;
		sccfreelist[i]->bp->size = BUFSIZE;
	}
	allocated_ibufs = MAX_IBUFS;
}

unsigned int scc_count_used_buffers(unsigned int * rx, unsigned int * tx)
{
	unsigned int i, used = 0;
	
	if (rx) *rx = 0;
	if (tx) *tx = 0;
	
	for (i = 0 ; i < allocated_ibufs ; i++)
	{
		if (sccfreelist[i]->inuse)
		{
			switch (sccfreelist[i]->bp->type)
			{
				case BT_RECEIVE:
					if (rx) (*rx)++; break;
				case BT_TRANSMIT:
					if (tx) (*tx)++; break;
			}
			
			used++;
		}
	}
	
	return used;
}


/* Allocate mbuf */
struct mbuf *
scc_get_buffer(char type)
{
	int i;
	unsigned long flags;

	save_flags(flags); cli();	/* just to be sure */

	for (i = 0 ; i < allocated_ibufs ; i++)
	{
		if(sccfreelist[i]->inuse == 0)
		{
			sccfreelist[i]->inuse = 1;
			sccfreelist[i]->bp->type = type;
			sccfreelist[i]->bp->next = NULLBUF;
			sccfreelist[i]->bp->anext = NULLBUF;
			sccfreelist[i]->bp->dup = NULLBUF;
			sccfreelist[i]->bp->size = BUFSIZE;
			sccfreelist[i]->bp->refcnt = 1;
			sccfreelist[i]->bp->cnt = 0;
			sccfreelist[i]->bp->in_use = 0;
		
			restore_flags(flags);
			return sccfreelist[i]->bp;
		}
	}
	
	printk("\nSCC scc_get_buffer(): buffer pool empty\n");	/* should never happen */
	restore_flags(flags);
	return NULLBUF;
}


/* Decrement the reference pointer in an mbuf. If it goes to zero,
 * free all resources associated with mbuf.
 * Return pointer to next mbuf in packet chain
 */
struct mbuf *
scc_return_buffer(register struct mbuf *bp, char type)
{
	struct mbuf *bpnext;
	int i;
	unsigned long flags;
	
	if(!bp)
		return NULLBUF;
		
	save_flags(flags); cli();
	bpnext = bp->next;
	
	if (bp->dup)
	{
		for(i = 0 ; i < allocated_ibufs ; i++)
		{
			if(sccfreelist[i]->bp == bp->dup)
			{
			      if (sccfreelist[i]->bp->type != type)
			      {
			      	   printk("scc_return_buffer(bp->dup, %i): wrong buffer type %i",
			      	   	  type,sccfreelist[i]->bp->type);
			      }
			      
			      sccfreelist[i]->bp->cnt = 0;
			      sccfreelist[i]->bp->refcnt = 0;
			      sccfreelist[i]->bp->in_use = 0;
			      sccfreelist[i]->inuse = 0;
			      bp->dup = NULLBUF;
			}
		}
	}
	
	/* Decrement reference count. If it has gone to zero, free it. */
	if(--bp->refcnt <= 0)
	{
		for(i = 0 ; i < allocated_ibufs ; i++)
		{
			if(sccfreelist[i]->bp == bp)
			{
				if (sccfreelist[i]->bp->type != type)
				{
					printk("scc_return_buffer(bp, %i): wrong buffer type %i",
						type,sccfreelist[i]->bp->type);
				}				
			     
				sccfreelist[i]->bp->cnt = 0;
				sccfreelist[i]->bp->refcnt = 0;
				sccfreelist[i]->inuse = 0;
				restore_flags(flags);
				return bpnext;
			}
		}
	}
		
	printk("\nscc_return_buffer(): bogus pointer %p\n",bp);
	restore_flags(flags);
	return bpnext;
} 


/* Free packet (a chain of mbufs). Return pointer to next packet on queue,
 * if any
 */
struct mbuf *
scc_free_chain(register struct mbuf *bp, char type)
{
	register struct mbuf *abp;
	unsigned long flags;

	if(!bp) 
		return NULLBUF;
		
	save_flags(flags); cli();	
	
	abp = bp->anext;
	while (bp) bp = scc_return_buffer(bp, type);
		
	restore_flags(flags); cli();
	return abp;
}


/* Append mbuf to end of mbuf chain */
void
scc_append_to_chain(struct mbuf **bph,struct mbuf *bp)
{
	register struct mbuf *p;
	unsigned long flags;

	if(bph == NULLBUFP || bp == NULLBUF)
		return;
	
	save_flags(flags); cli();
	
	if(*bph == NULLBUF)
	{
		/* First one on chain */
		*bph = bp;
	} else {
		for(p = *bph ; p->next != NULLBUF ; p = p->next)
			;
		p->next = bp;
	}
	
	restore_flags(flags);
}


/* Append packet (chain of mbufs) to end of packet queue */
void
scc_enqueue(struct mbuf **queue,struct mbuf *bp)
{
	register struct mbuf *p;
	unsigned long flags;

	if(queue == NULLBUFP || bp == NULLBUF)
		return;
		
	save_flags(flags); cli();
	
	if(*queue == NULLBUF)
	{
		/* List is empty, stick at front */
		*queue = bp;
	} else {
		for(p = *queue ; p->anext != NULLBUF ; p = p->anext)
			;
		p->anext = bp;
	}
	restore_flags(flags);
}


/* ******************************************************************** */
/* *			Interrupt Service Routines		      * */
/* ******************************************************************** */

/* ----> interrupt service routine for the 8530 <---- */

/* it's recommendet to keep this function "inline" ;-) */

static inline void
scc_isr_dispatch(register struct scc_channel *scc, register int vector)
{
	switch (vector & VECTOR_MASK)
	{
		case TXINT: scc_txint(scc); break;
		case EXINT: scc_exint(scc); break;
		case RXINT: scc_rxint(scc); break;
		case SPINT: scc_spint(scc); break;
		default	  : printk("scc_isr(): unknown interrupt status (addr %4.4x, state %2.2x)\n",scc->ctrl,vector);
	}
}





/* If the card has a latch for the interrupt vector (like the PA0HZP card)
   use it to get the number of the chip that generated the int.
   If not: poll all defined chips.
 */

static void
scc_isr(int irq, struct pt_regs *regs)
{
	register unsigned char vector;	
	register struct scc_channel *scc;
	register io_port q;
	register io_port *p;
	register int k;


	cli();
	
	if (Vector_Latch)
	{
	    	while(1)			   /* forever...? */
    		{ 
			Outb(Vector_Latch, 0);      /* Generate INTACK */
        
			/* Read the vector */
			if((vector=Inb(Vector_Latch)) >= 16 * Nchips) break; 
						/* ...not forever! */
          
			/* Extract channel number and status from vector. */
			/* Isolate channel nummer */
			/* Call handler */
	        
			if (vector & 0x01) break; 
        	 
		        scc=&SCC_Info[(((vector>>1)&0x7c)^0x04) >> 2];
        
			if (!scc->tty) break;

			scc_isr_dispatch(scc, vector);
			
			Outb(scc->ctrl,0x38);              /* Reset Highest IUS" opcode to WR0 */
		}  
	    	
		sti();	
		return;
	}
	
	
	/* Find the SCC generating the interrupt by polling all attached SCCs
	 * reading RR3A (the interrupt pending register)
	 */

	k = 0;
	p = SCC_ctrl;
	
	while (k++ < Nchips)
	{
		if (!(q=*p++)) break;
		
		Outb(q,3);
		
		if (Inb(q))
		{
			if (!(q=*p++)) break;
			
			Outb(q,2);
			vector=Inb(q);                  /* Read the vector */
        
		        /* Extract channel number and status from vector. */
		        /* Isolate channel nummer */
		        /* Call handler */
        

			if (vector & 1) break; 
        
		        scc = &SCC_Info[(((vector >> 1) & 0x7c) ^ 0x04) >> 2];
        
		        if (!scc->tty) break;

			/* Isolate status info from vector, call handler */
			
			scc_isr_dispatch(scc, vector);

			k = 0;
			p = SCC_ctrl;

	     	} else p++;
	}    
	
	sti();
}



/* ----> four different interrupt handlers for Tx, Rx, changing of	*/
/*       DCD/CTS and Rx/Tx errors					*/


static inline void prepare_next_txframe(register struct scc_channel *scc)
{
	if ((scc->tbp = scc->sndq))
	{
		scc->sndq = scc->sndq->anext;
		scc->stat.tx_state = TXS_NEWFRAME;

	} else {
		scc->stat.tx_state = TXS_BUSY;
		scc->t_tail = scc->kiss.tailtime;
	}
}


/* Transmitter interrupt handler */
static void
scc_txint(register struct scc_channel *scc)
{
	register struct mbuf *bp;

	scc->stat.txints++;

	bp = scc->tbp;
			
	while (bp && !bp->cnt)      /* find next buffer */
		bp = scc_return_buffer(bp, BT_TRANSMIT);
				
	if (bp == NULLBUF)			/* no more buffers in this frame */
	{
		if (--scc->stat.tx_queued < 0)
			scc->stat.tx_queued = 0;
			
		Outb(scc->ctrl,RES_Tx_P);       /* reset pending int */					
		cl(scc,R10,ABUNDER);            /* frame complete, allow CRC transmit */ 
		prepare_next_txframe(scc);
		
	} else {				/* okay, send byte */
	
		if (scc->stat.tx_state == TXS_NEWFRAME)
		{				/* first byte ? */
			Outb(scc->ctrl, RES_Tx_CRC);	/* reset CRC generator */
			or(scc,R10,ABUNDER);		/* re-install underrun protection */
			Outb(scc->data,bp->data[bp->in_use++]);
							/* send byte */
			if (!scc->enhanced)		/* reset EOM latch */
				Outb(scc->ctrl, RES_EOM_L);
				
			scc->stat.tx_state = TXS_ACTIVE;/* next byte... */
		} else {
			Outb(scc->data,bp->data[bp->in_use++]);
		}
		
		bp->cnt--;                      /* decrease byte count */
		scc->tbp=bp;			/* store buffer address */
	}
}

/* Throw away received mbuf(s) when an error occurred */

static inline void
scc_toss_buffer(register struct scc_channel *scc)
{
	register struct mbuf *bp;
	
	if((bp = scc->rbp) != NULLBUF)
	{
		scc_free_chain(bp->next, BT_RECEIVE);
		bp->next = NULLBUF;
		scc->rbp1 = bp;         /* Don't throw this one away */
		bp->cnt = 0;            /* Simply rewind it */
		bp->in_use = 0;
	}
}

static inline void
flush_FIFO(register struct scc_channel *scc)
{
	register int k;
	
	for (k=0; k<3; k++)
		Inb(scc->data);
		
	if(scc->rbp != NULLBUF)	/* did we receive something? */
	{
		if(scc->rbp->next != NULLBUF || scc->rbp->cnt > 0)
			scc->stat.rxerrs++;  /* then count it as an error */
			
		scc_toss_buffer(scc);         /* throw away buffer */
	}
}



/* External/Status interrupt handler */
static void
scc_exint(register struct scc_channel *scc)
{
	register unsigned char status,changes,chg_and_stat;

	scc->stat.exints++;

	status = InReg(scc->ctrl,R0);
	changes = status ^ scc->status;
	chg_and_stat = changes & status;
	
	/* ABORT: generated whenever DCD drops while receiving */

	if (chg_and_stat & BRK_ABRT)		/* Received an ABORT */
		flush_FIFO(scc);
			
		
	/* DCD: on = start to receive packet, off = ABORT condition */
	/* (a successfully received packet generates a special condition int) */
	
	if(changes & DCD)                       /* DCD input changed state */
	{
		if(status & DCD)                /* DCD is now ON */
		{
			if (scc->modem.clocksrc != CLK_EXTERNAL)
				OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
				
			or(scc,R3,ENT_HM|RxENABLE); /* enable the receiver, hunt mode */
		} else {                        /* DCD is now OFF */
			cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
			flush_FIFO(scc);
		}
	}


	/* CTS: use external TxDelay (what's that good for?!) */
	
	if (chg_and_stat & CTS)			/* CTS is now ON */
	{			
		if (!Running(t_txdel) && scc->kiss.txdelay == 0) /* zero TXDELAY = wait for CTS */
			scc->t_txdel = 0;	/* kick it! */		
		
	}
	
	if ((scc->stat.tx_state == TXS_ACTIVE) && (status & TxEOM))
	{
		scc->stat.tx_under++;	  /* oops, an underrun! count 'em */
		Outb(scc->ctrl, RES_Tx_P);
		Outb(scc->ctrl, RES_EXT_INT);	/* reset ext/status interrupts */
		scc->t_maxk = 1;
		scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
		
		if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
		or(scc,R10,ABUNDER);
	}
		
	if (status & ZCOUNT)		   /* Oops? */
	{
		scc->stat.tx_under = 9999;  /* errr... yes. */
		Outb(scc->ctrl, RES_Tx_P); /* just to be sure */
		scc->t_maxk = 1;
		scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
		if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
		scc->kiss.tx_inhibit = 1;	/* don't try it again! */
	}
		
	
	scc->status = status;
	Outb(scc->ctrl,RES_EXT_INT);
}


/* Receiver interrupt handler */
static void
scc_rxint(register struct scc_channel *scc)
{
	register struct mbuf *bp;

	scc->stat.rxints++;

	if( Running(t_maxk) && !(scc->kiss.fulldup))
	{
		Inb(scc->data);		/* discard char */
		or(scc,R3,ENT_HM);	/* enter hunt mode for next flag */
		return;
	}

	if ((bp = scc->rbp1) == NULLBUF || bp->cnt >= bp->size)	
	{ 				/* no buffer available or buffer full */
		if (scc->rbp == NULLBUF)
		{
			if ((bp = scc_get_buffer(BT_RECEIVE)) != NULLBUF)
				scc->rbp = scc->rbp1 = bp;
				
		}
		else if ((bp = scc_get_buffer(BT_RECEIVE)))
		{
			scc_append_to_chain(&scc->rbp, bp);
			scc->rbp1 = bp;
		}
		
		if (bp == NULLBUF)		/* no buffer available? */
		{
			Inb(scc->data);		/* discard character */
			or(scc,R3,ENT_HM);      /* enter hunt mode */
			scc_toss_buffer(scc);	/* throw away buffers */
			scc->stat.nospace++;    /* and count this error */
			return;
		}
	}

	/* now, we have a buffer. read character and store it */
	bp->data[bp->cnt++] = Inb(scc->data);
}


/* Receive Special Condition interrupt handler */
static void
scc_spint(register struct scc_channel *scc)
{
	register unsigned char status;
	register struct mbuf *bp;

	scc->stat.spints++;

	status = InReg(scc->ctrl,R1);		/* read receiver status */
	
	Inb(scc->data);				/* throw away Rx byte */

	if(status & Rx_OVR)			/* receiver overrun */
	{
		scc->stat.rx_over++;                /* count them */
		or(scc,R3,ENT_HM);              /* enter hunt mode for next flag */
		scc_toss_buffer(scc);                 /* rewind the buffer and toss */
	}
	
	if(status & END_FR && scc->rbp != NULLBUF)	/* end of frame */
	{
		/* CRC okay, frame ends on 8 bit boundary and received something ? */
		
		if (!(status & CRC_ERR) && (status & 0xe) == RES8 && scc->rbp->cnt)
		{
			/* ignore last received byte (first of the CRC bytes) */
			
			for (bp = scc->rbp; bp->next != NULLBUF; bp = bp->next) ;
				bp->cnt--;              /* last byte is first CRC byte */
				
			scc_enqueue(&scc->rcvq,scc->rbp);
			scc->rbp = scc->rbp1 = NULLBUF;
			scc->stat.rxframes++;
			scc->stat.rx_queued++;
		} else {				/* a bad frame */
			scc_toss_buffer(scc);		/* throw away frame */
			scc->stat.rxerrs++;
		}
	}
	
	Outb(scc->ctrl,ERR_RES);
}


/* ******************************************************************** */
/* *			Init Channel					*/
/* ******************************************************************** */


/* ----> set SCC channel speed <---- */

static inline void set_brg(register struct scc_channel *scc, unsigned int tc)
{
	unsigned long flags;

 	save_flags(flags); cli();      	/* 2-step register accesses... */

	cl(scc,R14,BRENABL);		/* disable baudrate generator */
	wr(scc,R12,tc & 255);		/* brg rate LOW */
	wr(scc,R13,tc >> 8);   		/* brg rate HIGH */
	or(scc,R14,BRENABL);		/* enable baudrate generator */

	restore_flags(flags);
}

static inline void set_speed(register struct scc_channel *scc)
{
	set_brg(scc, (unsigned) (Clock / (scc->modem.speed * 64)) - 2);
}


/* ----> initialize a SCC channel <---- */

static inline void init_brg(register struct scc_channel *scc)
{
	wr(scc, R14, BRSRC);				/* BRG source = PCLK */
	OutReg(scc->ctrl, R14, SSBR|scc->wreg[R14]);	/* DPLL source = BRG */
	OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]);	/* DPLL NRZI mode */
}

static void
init_channel(register struct scc_channel *scc)
{
	unsigned long flags;

	save_flags(flags); cli();

	wr(scc,R1,0);			/* no W/REQ operation */
	wr(scc,R3,Rx8|RxCRC_ENAB);	/* RX 8 bits/char, CRC, disabled */	
	wr(scc,R4,X1CLK|SDLC);		/* *1 clock, SDLC mode */
	wr(scc,R5,Tx8|DTR|TxCRC_ENAB);	/* TX 8 bits/char, disabled, DTR */
	wr(scc,R6,0);			/* SDLC address zero (not used) */
	wr(scc,R7,FLAG);		/* SDLC flag value */
	wr(scc,R9,VIS);			/* vector includes status */
	wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER); /* abort on underrun, preset CRC generator, NRZ(I) */
	wr(scc,R14, 0);


/* set clock sources:

   CLK_DPLL: normal halfduplex operation
   
		RxClk: use DPLL
		TxClk: use DPLL
		TRxC mode DPLL output
		
   CLK_EXTERNAL: external clocking (G3RUH or DF9IC modem)
   
  	        BayCom: 		others:
  	        
  	        TxClk = pin RTxC	TxClk = pin TRxC
  	        RxClk = pin TRxC 	RxClk = pin RTxC
  	     

   CLK_DIVIDER:
   		RxClk = use DPLL
   		TxClk = pin RTxC
   		
   		BayCom:			others:
   		pin TRxC = DPLL		pin TRxC = BRG
   		(RxClk * 1)		(RxClk * 32)
*/  

   		
	switch(scc->modem.clocksrc)
	{
		case CLK_DPLL:
			wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
			init_brg(scc);
			break;

		case CLK_DIVIDER:
			wr(scc, R11, ((Board & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
			init_brg(scc);
			break;

		case CLK_EXTERNAL:
			wr(scc, R11, (Board & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
			OutReg(scc->ctrl, R14, DISDPLL);
			break;

	}
	
	/* enable CTS (not for Baycom), ABORT & DCD interrupts */
	wr(scc,R15,((Board & BAYCOM) ? 0 : CTSIE)|BRKIE|DCDIE|TxUIE);

	if(scc->enhanced)
	{
		or(scc,R15,SHDLCE|FIFOE);	/* enable FIFO, SDLC/HDLC Enhancements (From now R7 is R7') */
		wr(scc,R7,AUTOEOM);
	}

	if((InReg(scc->ctrl,R0)) & DCD)		/* DCD is now ON */
	{
		if (scc->modem.clocksrc != CLK_EXTERNAL)
			or(scc,R14, SEARCH);
			
		or(scc,R3,ENT_HM|RxENABLE);	/* enable the receiver, hunt mode */
	}
	
	Outb(scc->ctrl,RES_EXT_INT);	/* reset ext/status interrupts */
	Outb(scc->ctrl,RES_EXT_INT);	/* must be done twice */
	
	scc->status = InReg(scc->ctrl,R0);	/* read initial status */

	or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
	or(scc,R9,MIE);			/* master interrupt enable */
			
	restore_flags(flags);
	
	set_speed(scc); 
}




/* ******************************************************************** */
/* *			SCC timer functions			      * */
/* ******************************************************************** */


/* ----> scc_key_trx sets the time constant for the baudrate 
         generator and keys the transmitter		     <---- */

static void
scc_key_trx(struct scc_channel *scc, char tx)
{
	unsigned int time_const;

	if (scc->modem.speed < baud_table[1]) 
		scc->modem.speed = 1200;
		
	if (Board & PRIMUS)
		Outb(scc->ctrl + 4, Option | (tx? 0x80 : 0));
	
	time_const = (unsigned) (Clock / (scc->modem.speed * (tx? 2:64))) - 2;
	
	if (scc->modem.clocksrc == CLK_DPLL)
	{				/* simplex operation */
		if (tx)
		{
			cl(scc,R3,RxENABLE|ENT_HM);	/* then switch off receiver */
			
			set_brg(scc, time_const);	/* reprogram baudrate generator */

			/* DPLL -> Rx clk, BRG -> Tx CLK, TRxC mode output, TRxC = BRG */
			wr(scc, R11, RCDPLL|TCBR|TRxCOI|TRxCBR);
			
			or(scc,R5,RTS|TxENAB);		/* set the RTS line and enable TX */
		} else {
			cl(scc,R5,RTS|TxENAB);
			
			set_brg(scc, time_const);	/* reprogram baudrate generator */
			
			/* DPLL -> Rx clk, DPLL -> Tx CLK, TRxC mode output, TRxC = DPLL */
			wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
			
			or(scc,R3,RxENABLE|ENT_HM);
		}
	} else {
		if (tx)
			or(scc,R5,RTS|TxENAB);		/* enable tx */
		else
			cl(scc,R5,RTS|TxENAB);		/* disable tx */
	}
}


/* ----> SCC timer interrupt handler and friends. Will be called every 1/TPS s <---- */

static unsigned char Rand = 17;

static inline int is_grouped(register struct scc_channel *scc)
{
	int k;
	struct scc_channel *scc2;
	unsigned char grp1, grp2;

	grp1 = scc->kiss.group;
	
	for (k = 0; k < (Nchips * 2); k++)
	{
		scc2 = &SCC_Info[k];
		grp2 = scc2->kiss.group;
		
		if (scc2 == scc || !(scc2->tty && grp2)) 
			return 0;
		
		if (grp1 & 0x3f == grp2 & 0x3f)
		{
			if ( (grp1 & TXGROUP) && (scc2->wreg[R5] & RTS) )
				return 1;
			
			if ( (grp1 & RXGROUP) && (scc2->status & DCD) )
				return 1;
		}
	}
	return 0;
}
		

static inline void dw_slot_timeout(register struct scc_channel *scc)
{
	scc->t_dwait = TIMER_STOPPED;
	scc->t_slot = TIMER_STOPPED;
	
	if (!scc->kiss.fulldup)
	{
		Rand = Rand * 17 + 31;
		
		if ( (scc->kiss.softdcd? !(scc->status & SYNC_HUNT):(scc->status & DCD))  || (scc->kiss.persist) < Rand || (scc->kiss.group && is_grouped(scc)) )
		{
			if (scc->t_mbusy == TIMER_STOPPED)
				scc->t_mbusy = TPS * scc->kiss.maxdefer;
				
			scc->t_slot = scc->kiss.slottime;
			return ;
			
		}
	}
	
	if ( !(scc->wreg[R5] & RTS) )
	{
		scc->t_txdel = scc->kiss.txdelay;
		scc_key_trx(scc, TX_ON);
	} else {
		scc->t_txdel = 0;
	}
}




static inline void txdel_timeout(register struct scc_channel *scc)
{
	scc->t_txdel = TIMER_STOPPED;
	
	scc->t_maxk = TPS * scc->kiss.maxkeyup;
	prepare_next_txframe(scc);
	
	if (scc->stat.tx_state != TXS_BUSY)
		scc_txint(scc);		
}
	


static inline void tail_timeout(register struct scc_channel *scc)
{
	scc->t_tail = TIMER_STOPPED;
	
	/* when fulldup is 0 or 1, switch off the transmitter.
	 * when frames are still queued (because of transmit time limit),
	 * restart the procedure to get the channel after MINTIME.
	 * when fulldup is 2, the transmitter remains keyed and we
	 * continue sending after waiting for waittime. IDLETIME is an 
	 * idle timeout in this case.
	 */ 
	 
	 if (scc->kiss.fulldup < 2)
	 {
	 	if (scc->sndq)		/* we had a timeout? */
	 	{
	 		scc->stat.tx_state = TXS_BUSY;
	 		scc->t_dwait = TPS * scc->kiss.mintime; /* try again */
	 	}
	 	
	 	scc->stat.tx_state = TXS_IDLE;
		scc->t_maxk = TIMER_STOPPED;
	 	scc_key_trx(scc, TX_OFF);
	 	return;
	 }
	 
	 if (scc->sndq)			/* maxkeyup expired */ /* ?! */
	 {
	 	scc->stat.tx_state = TXS_BUSY;
	 	scc->t_txdel = TPS * scc->kiss.waittime;
	 }
	 else
	 
	 	scc->t_idle = TPS * scc->kiss.idletime;
}


static inline void busy_timeout(register struct scc_channel *scc)
{
#ifdef	THROW_AWAY_AFTER_BUSY_TIMEOUT
	register struct mbuf *bp;		/* not tested */

	bp = scc->sndq;
	
	while (bp) bp = scc_free_chain(bp, BT_TRANSMIT);
	
	scc->sndq = NULLBUF;
	scc->stat.tx_state = TXS_IDLE;
	
#else
	scc->t_txdel = scc->kiss.txdelay;	/* brute force ... */
#endif
	scc->t_mbusy = TIMER_STOPPED;
	
}


static inline void maxk_idle_timeout(register struct scc_channel *scc)
{
	scc->t_maxk = TIMER_STOPPED;
	scc->t_idle = TIMER_STOPPED;
	
	scc->stat.tx_state = TXS_BUSY;
	scc->t_tail = scc->kiss.tailtime;
}

static inline void check_rcv_queue(register struct scc_channel *scc)
{
	register struct mbuf *bp;
	
	if (scc->stat.rx_queued > QUEUE_THRES)
	{
		if (scc->rcvq == NULLBUF)
		{
			printk("z8530drv: Warning - scc->stat.rx_queued shows overflow"
			       " (%d) but queue is empty\n", scc->stat.rx_queued);
			       
			scc->stat.rx_queued = 0;	/* correct it */
			scc->stat.nospace = 12345;	/* draw attention to it */
			return;
		}
			
		bp = scc->rcvq->anext;	/* don't use the one we currently use */
		
		while (bp && (scc->stat.rx_queued > QUEUE_HYST))
		{
			bp = scc_free_chain(bp, BT_RECEIVE);
			scc->stat.rx_queued--;
			scc->stat.nospace++;
		}
		
		scc->rcvq->anext = bp;
	}
}

static void
scc_timer(void)
{
	register struct scc_channel *scc;
	register int chan;
	unsigned long flags;
		
	save_flags(flags); cli();
	
	for (chan = 0; chan < (Nchips * 2); chan++)
	{
		scc = &SCC_Info[chan];
		
		if (scc->tty && scc->init)
		{
			kiss_encode(scc);
			check_rcv_queue(scc);
			
			/* KISS-TNC emulation */
			
			if (Expired(t_dwait)) dw_slot_timeout(scc)	; else
			if (Expired(t_slot))  dw_slot_timeout(scc)	; else
			if (Expired(t_txdel)) txdel_timeout(scc)   	; else
			if (Expired(t_tail))  tail_timeout(scc)	   	;
			
			/* watchdogs */
			
			if (Expired(t_mbusy)) busy_timeout(scc);
			if (Expired(t_maxk))  maxk_idle_timeout(scc);
			if (Expired(t_idle))  maxk_idle_timeout(scc);
		}
	}
	
	timer_table[SCC_TIMER].fn = scc_timer;
	timer_table[SCC_TIMER].expires = jiffies + HZ/TPS;
	timer_active |= 1 << SCC_TIMER; 
	
	restore_flags(flags);
}
			

static void
scc_init_timer(struct scc_channel *scc)
{
	unsigned long flags;
	
	save_flags(flags); cli();
	
	Stop_Timer(t_dwait);
	Stop_Timer(t_slot);
	Stop_Timer(t_txdel);
	Stop_Timer(t_tail);
	Stop_Timer(t_mbusy);
	Stop_Timer(t_maxk);
	Stop_Timer(t_idle);
	scc->stat.tx_state = TXS_IDLE;
	
	restore_flags(flags);
}



/* ******************************************************************** */
/* *			KISS interpreter			      * */
/* ******************************************************************** */


/*
 * this will set the "kiss" parameters through kiss itself
 */
 
static void
kiss_set_param(struct scc_channel *scc,char cmd, unsigned int val)
{

#define  VAL val=val*TPS/100
#define SVAL val? val:TIMER_STOPPED

	switch(cmd){
	case PARAM_TXDELAY:
		scc->kiss.txdelay = VAL; break;
	case PARAM_PERSIST:
		scc->kiss.persist = val; break;
	case PARAM_SLOTTIME:
		scc->kiss.slottime = VAL; break;
	case PARAM_TXTAIL:
		scc->kiss.tailtime = VAL; break;
	case PARAM_FULLDUP:
		scc->kiss.fulldup = val; break;
	case PARAM_WAIT:
		scc->kiss.waittime = VAL; break;
	case PARAM_MAXKEY:
		scc->kiss.maxkeyup = SVAL; break;
	case PARAM_MIN:
		scc->kiss.mintime = SVAL; break;
	case PARAM_IDLE:
		scc->kiss.idletime = val; break;
	case PARAM_MAXDEFER:
		scc->kiss.maxdefer = SVAL; break;
	case PARAM_GROUP:
		scc->kiss.group = val;  break;
	case PARAM_TX:
		scc->kiss.tx_inhibit = val;
	case PARAM_SOFTDCD:
		scc->kiss.softdcd = val;
	}
	return;

#undef  VAL
#undef SVAL
}


/* interpret frame: strip CRC and decode KISS */

static void kiss_interpret_frame(struct scc_channel * scc)
{
	unsigned char kisscmd;
	unsigned long flags;

	if (scc->sndq1->cnt < 2)
	{
		if (scc->sndq1) 
			scc_free_chain(scc->sndq1, BT_TRANSMIT);
		else
			scc->sndq1 = NULLBUF;
			
		scc->sndq2 = NULLBUF;
		return;
	}
	
	
	
	if (scc->kiss.not_slip)
	{
		kisscmd = scc->sndq1->data[scc->sndq1->in_use++];
		scc->sndq1->cnt--;
	} else {
		kisscmd = 0;
	}

	if (kisscmd & 0xa0)
	{
		if (scc->sndq1->cnt > 2)
			scc->sndq1->cnt -= 2;
		else
		{
			scc_free_chain(scc->sndq1, BT_TRANSMIT);
			scc->sndq2 = NULLBUF;
			return;
		}
	}
	
	
	kisscmd &= 0x1f;
	
		
	if (kisscmd)
	{
		kiss_set_param(scc, kisscmd, scc->sndq1->data[scc->sndq1->in_use]);
		scc->sndq1->cnt=0;
		scc->sndq1->in_use=0;
					
		scc_free_chain(scc->sndq1, BT_TRANSMIT);
		scc->sndq2 = NULLBUF;
		return;
	}
	
	scc_enqueue(&scc->sndq,scc->sndq1); /* scc_enqueue packet */
	scc->stat.txframes++;
	scc->stat.tx_queued++;
	scc->sndq2 = NULLBUF;		/* acquire a new buffer next time */

	save_flags(flags); cli();

	if(scc->stat.tx_state == TXS_IDLE)
	{      				/* when transmitter is idle */
		scc_init_timer(scc);
		scc->stat.tx_state = TXS_BUSY;
		scc->t_dwait = (scc->kiss.fulldup? 0:scc->kiss.waittime);
	}
	
	restore_flags(flags);
}

static void kiss_store_byte(struct scc_channel *scc, unsigned char ch)
{
	if (scc->sndq2 == NULLBUF) return;
	
	if(scc->sndq2->cnt == scc->sndq2->size)		/* buffer full? */
	{
		if((scc->sndq2 = scc_get_buffer(BT_TRANSMIT)) == NULLBUF)
		{
			printk("\nsccdrv: running out of memory\n");
			return;
		}
		scc_append_to_chain(&scc->sndq1,scc->sndq2);         /* add buffer */
	}
	
	scc->sndq2->data[scc->sndq2->cnt++] = ch;
}

static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
{
	switch (scc->stat.tx_kiss_state) 
	{
		case KISS_IDLE:
			if (ch == FEND)
			{
				if (!(scc->sndq2 = scc->sndq1 = scc_get_buffer(BT_TRANSMIT)))
					return 0;
					
				scc->stat.tx_kiss_state = KISS_DATA;
			} else scc->stat.txerrs++;
			break;
					
		case KISS_DATA:
			if (ch == FESC)
				scc->stat.tx_kiss_state = KISS_ESCAPE;
			else if (ch == FEND)
			{
				kiss_interpret_frame(scc);	
				scc->stat.tx_kiss_state = KISS_IDLE;
			}
			else kiss_store_byte(scc, ch);
			break;
					
		case KISS_ESCAPE:
			if (ch == TFEND)
			{
				kiss_store_byte(scc, FEND);
				scc->stat.tx_kiss_state = KISS_DATA;
			}
			else if (ch == TFESC)
			{
				kiss_store_byte(scc, FESC);
				scc->stat.tx_kiss_state = KISS_DATA;
			}
			else
			{
				scc_free_chain(scc->sndq1, BT_TRANSMIT);
				scc->sndq2 = NULLBUF;
				scc->stat.txerrs++;
				scc->stat.tx_kiss_state = KISS_IDLE;
			}
			break;
	} /* switch */
	
	return 0;
	
}

/* ----> Encode received data and write it to the flip-buffer  <---- */

/* receive raw frame from SCC. used for AX.25 */
static void
kiss_encode(register struct scc_channel *scc)
{
	struct mbuf *bp,*bp2;
	struct tty_struct * tty = scc->tty;
	unsigned long flags; 
	unsigned char ch;

	if(!scc->rcvq)
	{
		scc->stat.rx_kiss_state = KISS_IDLE;
		return;
	}
	
	/* worst case: FEND 0 FESC TFEND -> 4 bytes */
	
	while(tty->flip.count < TTY_FLIPBUF_SIZE-3)
	{ 
		if (scc->rcvq->cnt)
		{
			bp = scc->rcvq;
			
			if (scc->stat.rx_kiss_state == KISS_IDLE)
			{
				tty_insert_flip_char(tty, FEND, 0);
				
				if (scc->kiss.not_slip)
					tty_insert_flip_char(tty, 0, 0);
					
				scc->stat.rx_kiss_state = KISS_RXFRAME;
			}
				
			switch(ch = bp->data[bp->in_use++])
			{
				case FEND:
					tty_insert_flip_char(tty, FESC, 0);
					tty_insert_flip_char(tty, TFEND, 0);
					break;
				case FESC:
					tty_insert_flip_char(tty, FESC, 0);
					tty_insert_flip_char(tty, TFESC, 0);
					break;
				default:
					tty_insert_flip_char(tty, ch, 0);
			}
			
			bp->cnt--;
			
 		} else {
			save_flags(flags); cli();
			
			while (!scc->rcvq->cnt)
	 		{				 /* buffer empty? */
				bp  = scc->rcvq->next;  /* next buffer */
				bp2 = scc->rcvq->anext; /* next packet */
				
				
				scc_return_buffer(scc->rcvq, BT_RECEIVE);
				
				if (!bp)	/* end of frame ? */
				{
					scc->rcvq = bp2;
					
					if (--scc->stat.rx_queued < 0)
						scc->stat.rx_queued = 0;
					
					if (scc->stat.rx_kiss_state == KISS_RXFRAME)	/* new packet? */
					{
						tty_insert_flip_char(tty, FEND, 0); /* send FEND for old frame */
						scc->stat.rx_kiss_state = KISS_IDLE; /* generate FEND for new frame */
					}
					
					restore_flags(flags);
					queue_task(&tty->flip.tqueue, &tq_timer);
					return;
					
				} else scc->rcvq = bp; /* next buffer */
			}
			
			restore_flags(flags);
		}						
		
	}
	
 	queue_task(&tty->flip.tqueue, &tq_timer); /* kick it... */
}


/* ******************************************************************* */
/* *		Init channel structures, special HW, etc...	     * */
/* ******************************************************************* */


static void
z8530_init(void)
{
	struct scc_channel *scc;
	int chip,chan;
	unsigned long flags;
    	int k;

	/* reset and pre-init all chips in the system */
	for (chip = 0; chip < Nchips; chip++)
	{
		/* Special SCC cards */

		if(Board & EAGLE)			/* this is an EAGLE card */
			Outb(Special_Port,0x08);	/* enable interrupt on the board */
			
		if(Board & (PC100 | PRIMUS))		/* this is a PC100/EAGLE card */
			Outb(Special_Port,Option);	/* set the MODEM mode (22H normally) */
			
		/* Init SCC */
		
		scc=&SCC_Info[2*chip];
		if (!scc->ctrl) continue;
		
		save_flags(flags); cli();
		
		Outb(scc->ctrl, 0);
		OutReg(scc->ctrl,R9,FHWRES);		/* force hardware reset */
		for (k=1; k < 1000; k++) ;
		
		for (chan = 0; chan < 2; chan++)
		{
			scc=&SCC_Info[2*chip+chan];
			
			wr(scc,R1, 0);
			wr(scc,R2, chip*16);		/* No of chip is vector */
			wr(scc,R9,VIS);			/* vector includes status */
		}
 
        	restore_flags(flags);
        }

	if (Ivec == 2) Ivec = 9;			/* this f... IBM AT-design! */
	request_irq(Ivec, scc_isr,   SA_INTERRUPT, "AX.25 SCC");
 
	Driver_Initialized = 1;
}


/* ******************************************************************** */
/* * 	Filesystem Routines: open, close, ioctl, settermios, etc      * */
/* ******************************************************************** */



/* scc_paranoia_check(): warn user if something went wrong		*/

static inline int scc_paranoia_check(struct scc_channel *scc, dev_t device, const char *routine)
{
#ifdef SCC_PARANOIA_CHECK
	static const char *badmagic =
		"Warning: bad magic number for Z8530 SCC struct (%d, %d) in %s\n";
        static const char *badinfo =
                "Warning: Z8530 not found for (%d, %d) in %s\n";
       
	if (!scc->init) 
	{
        	printk(badinfo, MAJOR(device), MINOR(device), routine);
                return 1;
        }
        if (scc->magic != SCC_MAGIC) {
        	printk(badmagic, MAJOR(device), MINOR(device), routine);
                return 1;
        }
#endif

	return 0;
}


/* ----> this one is called whenever you open the device <---- */

int scc_open(struct tty_struct *tty, struct file * filp)
{
	struct scc_channel *scc;
	int chan;
	
        chan = MINOR(tty->device) - tty->driver.minor_start;
        if ((chan < 0) || (chan >= (Nchips * 2)))
                return -ENODEV;
 
 	scc = &SCC_Info[chan];
 	
	tty->driver_data = scc;
 	tty->termios->c_cflag &= ~CBAUD; 
 	
	if (!Driver_Initialized)
		return 0;
	
	if (scc->magic != SCC_MAGIC)
	{
		printk("ERROR: scc_open(): bad magic number for device (%d, %d)", MAJOR(tty->device), MINOR(tty->device));
		return -ENODEV;
	}		
	
	if(scc->tty != NULL)
	{
		scc->tty_opened++;
		return 0;
	}

 	if(!scc->init) return 0;
 	 	 	 
  	scc->tty = tty;
	init_channel(scc);

	scc->stat.tx_kiss_state = KISS_IDLE;	/* don't change this... */
	scc->stat.rx_kiss_state = KISS_IDLE;	/* ...or this */
	
	scc_init_timer(scc);
	
	timer_table[SCC_TIMER].fn = scc_timer;
	timer_table[SCC_TIMER].expires = 0;	/* now! */
	timer_active |= 1 << SCC_TIMER;
	
	return 0;
}


/* ----> and this whenever you close the device <---- */

static void
scc_close(struct tty_struct *tty, struct file * filp)
{
	struct scc_channel *scc = tty->driver_data;
	unsigned long flags;

        if (!scc || (scc->magic != SCC_MAGIC))
                return;
	
	if(scc->tty_opened)
	{
		scc->tty_opened--;
		return;
	}
	
	tty->driver_data = NULLBUF;
	
	save_flags(flags); cli();
	
	Outb(scc->ctrl,0);		/* Make sure pointer is written */
	wr(scc,R1,0);			/* disable interrupts */
	wr(scc,R3,0);
	
	scc->tty = NULL;
	
	restore_flags(flags);
	tty->stopped = 0;		
}




/*
 * change scc_speed
 */
 
static void
scc_change_speed(struct scc_channel * scc)
{
	if (scc->tty == NULL)
		return;
		
	scc->modem.speed = baud_table[scc->tty->termios->c_cflag & CBAUD];
	
	if (scc->stat.tx_state == 0)	/* only switch baudrate on rx... ;-) */
		set_speed(scc);
}


/* ----> ioctl-routine of the driver <---- */

/* perform ioctl on SCC (sdlc) channel
 * this is used for AX.25 mode, and will set the "kiss" parameters
 */
 
/* TIOCMGET 	- get modem status	arg: (unsigned long *) arg
 * TIOCMBIS 	- set PTT		arg: ---
 * TIOCMBIC 	- reset PTT		arg: ---
 * TIOCMBIC 	- set PTT		arg: ---
 * TIOCSCCINI	- initialize driver	arg: ---
 * TIOCCHANINI	- initialize channel	arg: (struct scc_modem *) arg
 * TIOCGKISS	- get level 1 parameter	arg: (struct ioctl_command *) arg
 * TIOCSKISS	- set level 1 parameter arg: (struct ioctl_command *) arg
 * TIOCSCCSTAT  - get driver status	arg: (struct scc_stat *) arg
 */
 

static int
scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
{
	struct scc_channel * scc = tty->driver_data;
	unsigned long flags, r;
	unsigned int result;
	unsigned int value;
	struct ioctl_command kiss_cmd;
	int error;

        if (scc->magic != SCC_MAGIC) 
        {
		printk("ERROR: scc_ioctl(): bad magic number for device %d,%d", 
			MAJOR(tty->device), MINOR(tty->device));
			
                return -ENODEV;
        }
	                                                 
	r = NO_SUCH_PARAM;
	
	if (!Driver_Initialized)
	{
		if (cmd == TIOCSCCINI)
		{
			if (!suser())
				return -EPERM;
			
			scc_alloc_buffer_pool();
			z8530_init();
			return 0;
		}
		
		return -EINVAL;	/* confuse the user */
	}
	
	if (!scc->init)
	{

		if (cmd == TIOCCHANINI)
		{
			if (!arg)
				return -EFAULT;
				
			if (!suser())
				return -EPERM;
			
			memcpy_fromfs(&scc->modem, (void *) arg, sizeof(struct scc_modem));
			
			/* default KISS Params */
		
			if (scc->modem.speed < 4800)
			{
				scc->kiss.txdelay = 36*TPS/100;    /* 360 ms */
				scc->kiss.persist = 42;            /* 25% persistence */			/* was 25 */
				scc->kiss.slottime = 16*TPS/100;   /* 160 ms */
				scc->kiss.tailtime = 4;            /* minimal reasonable value */
				scc->kiss.fulldup = 0;             /* CSMA */
				scc->kiss.waittime = 50*TPS/100;   /* 500 ms */
				scc->kiss.maxkeyup = 10;           /* 10 s */
				scc->kiss.mintime = 3;             /* 3 s */
				scc->kiss.idletime = 30;           /* 30 s */
				scc->kiss.maxdefer = 120;	   /* 2 min */
				scc->kiss.not_slip = 1;		   /* KISS mode */
				scc->kiss.softdcd = 0;		   /* hardware dcd */
			} else {
				scc->kiss.txdelay = 10*TPS/100;    /* 100 ms */
				scc->kiss.persist = 64;            /* 25% persistence */			/* was 25 */
				scc->kiss.slottime = 8*TPS/100;    /* 160 ms */
				scc->kiss.tailtime = 1;            /* minimal reasonable value */
				scc->kiss.fulldup = 0;             /* CSMA */
				scc->kiss.waittime = 50*TPS/100;   /* 500 ms */
				scc->kiss.maxkeyup = 7;            /* 7 s */
				scc->kiss.mintime = 3;             /* 3 s */
				scc->kiss.idletime = 30;           /* 30 s */
				scc->kiss.maxdefer = 120;	   /* 2 min */
				scc->kiss.not_slip = 1;		   /* KISS mode */
				scc->kiss.softdcd = 0;		   /* hardware dcd */
			}
			
			scc->init = 1;			
			
			return 0;
		}
		
		return -EINVAL;
	}

	switch(cmd){
	case TCSBRK:
		return 0;
	case TIOCMGET:
		error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(unsigned int *));
		if (error)
			return error;

		save_flags(flags); cli();
		
		result =  ((scc->wreg[R5] & RTS) ? TIOCM_RTS : 0)
			| ((scc->wreg[R5] & DTR) ? TIOCM_DTR : 0)
			| ((InReg(scc->ctrl,R0) & DCD)  ? TIOCM_CAR : 0)
			| ((InReg(scc->ctrl,R0) & CTS)  ? TIOCM_CTS : 0);
		
		restore_flags(flags);
			
		put_fs_long(result,(unsigned long *) arg);
		return 0;
	case TIOCMBIS:
	case TIOCMBIC:
	case TIOCMSET:
		switch (cmd) {
		case TIOCMBIS:
			scc->wreg[R5] |= DTR;
			scc->wreg[R5] |= RTS;
			break;
		case TIOCMBIC:
			scc->wreg[R5] &= ~DTR;
			scc->wreg[R5] &= ~RTS;
			break;
		case TIOCMSET:
			value = get_fs_long((unsigned long *) arg);
			
			if(value & TIOCM_DTR)
				scc->wreg[R5] |= DTR;
			else
				scc->wreg[R5] &= ~DTR;
			if(value & TIOCM_RTS)
				scc->wreg[R5] |= RTS;
			else
				scc->wreg[R5] &= ~RTS;
			break;
		}
		
		save_flags(flags); cli();
		
		if(scc->stat.tx_state == TXS_IDLE && !Running(t_idle))
			maxk_idle_timeout(scc);
			
		restore_flags(flags);
		
		return 0;
		
	case TCGETS:
		error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct termios));
		if (error)
			return error;
		if (!arg) 
			return -EFAULT;
			
		memcpy_tofs((void *) arg, scc->tty->termios, sizeof(struct termios));
		return 0;
		
	case TCSETS:
	case TCSETSF:		/* should flush first, but... */
	case TCSETSW:		/* should wait 'till flush, but... */
		if (!suser())
			return -EPERM;
		if (!arg)
			return -EFAULT;
		
		memcpy_fromfs(scc->tty->termios, (void *) arg, sizeof(struct termios));
		scc_change_speed(scc);
		return 0;
		
		
	case TIOCSCCSTAT:
		error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct scc_stat));
		if (error)
			return error;
		
		if (!arg)
			return -EFAULT;
			
		scc->stat.used_buf = scc_count_used_buffers(&scc->stat.rx_alloc, 
							    &scc->stat.tx_alloc);
			
		memcpy_tofs((void *) arg, &scc->stat, sizeof(struct scc_stat));
		return 0;
		
#define TICKS (100/TPS)
#define CAST(x) (unsigned long)(x)
#define  Val	kiss_cmd.param
#define  VAL	kiss_cmd.param*TPS/100
#define SVAL	kiss_cmd.param? kiss_cmd.param:TIMER_STOPPED

	case TIOCGKISS:
		error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct ioctl_command));
		if (error)
			return error;
			
		if (!arg)
			return -EFAULT;
			
		memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));

		switch (kiss_cmd.command)
		{
			case PARAM_TXDELAY:	r = CAST(scc->kiss.txdelay*TICKS);	break;
			case PARAM_PERSIST:	r = CAST(scc->kiss.persist); 		break;
			case PARAM_SLOTTIME:	r = CAST(scc->kiss.slottime*TICKS);	break;
			case PARAM_TXTAIL:	r = CAST(scc->kiss.tailtime*TICKS);	break;
			case PARAM_FULLDUP:	r = CAST(scc->kiss.fulldup); 		break;
			case PARAM_SOFTDCD:	r = CAST(scc->kiss.softdcd);		break;
			case PARAM_DTR:		r = CAST((scc->wreg[R5] & DTR)? 1:0); break;
			case PARAM_RTS:		r = CAST((scc->wreg[R5] & RTS)? 1:0); break;
			case PARAM_SPEED:	r = CAST(scc->modem.speed);	break;
			case PARAM_GROUP:	r = CAST(scc->kiss.group); 		break;
			case PARAM_IDLE:	r = CAST(scc->kiss.idletime);		break;
			case PARAM_MIN:		r = CAST(scc->kiss.mintime);		break;
			case PARAM_MAXKEY:	r = CAST(scc->kiss.maxkeyup);		break;
			case PARAM_WAIT:	r = CAST(scc->kiss.waittime);		break;
			case PARAM_MAXDEFER:	r = CAST(scc->kiss.maxdefer);		break;
			case PARAM_TX:		r = CAST(scc->kiss.tx_inhibit);	break;
			case PARAM_SLIP:	r = CAST(!scc->kiss.not_slip);		break;
			default:		r = NO_SUCH_PARAM;
		}
		
		kiss_cmd.param = r;
		
		memcpy_tofs((void *) arg, &kiss_cmd, sizeof(struct ioctl_command));
		return 0;
		break;
		
	case TIOCSKISS:
		if (!arg)
			return -EFAULT;

		if (!suser())
			return -EPERM;
			
		memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));
		
		switch (kiss_cmd.command)
		{
			case PARAM_TXDELAY:	scc->kiss.txdelay=VAL;		break;
			case PARAM_PERSIST:	scc->kiss.persist=Val;		break;
			case PARAM_SLOTTIME:	scc->kiss.slottime=VAL;		break;
			case PARAM_TXTAIL:	scc->kiss.tailtime=VAL;		break;
			case PARAM_FULLDUP:	scc->kiss.fulldup=Val;		break;
			case PARAM_SOFTDCD:	scc->kiss.softdcd=Val;		break;
			case PARAM_DTR:		break; /* does someone need this? */
			case PARAM_RTS:		break; /* or this? */
			case PARAM_SPEED:	scc->modem.speed=Val;		break;
			case PARAM_GROUP:	scc->kiss.group=Val;		break;
			case PARAM_IDLE:	scc->kiss.idletime=Val;		break;
			case PARAM_MIN:		scc->kiss.mintime=SVAL;		break;
			case PARAM_MAXKEY:	scc->kiss.maxkeyup=SVAL;	break;
			case PARAM_WAIT:	scc->kiss.waittime=Val;		break;
			case PARAM_MAXDEFER:	scc->kiss.maxdefer=SVAL;	break;
			case PARAM_TX:		scc->kiss.tx_inhibit=Val;	break;
			case PARAM_SLIP:	scc->kiss.not_slip=!Val;	break;
			default:		return -ENOIOCTLCMD;
		}
		
		return 0;
		break;
#undef TICKS
#undef CAST
#undef VAL
#undef SVAL
#undef Val
		
	default:
		return -ENOIOCTLCMD;
    }
}


/* ----- TERMIOS function ----- */

static void
scc_set_termios(struct tty_struct * tty, struct termios * old_termios)
{
	if (tty->termios->c_cflag == old_termios->c_cflag) 
		return;
	scc_change_speed(tty->driver_data);
}


static inline void check_tx_queue(register struct scc_channel *scc)
{
	register struct mbuf *bp;
	
	if (scc->stat.tx_queued > QUEUE_THRES)
	{
		if (scc->sndq1 == NULLBUF)
		{
			printk("z8530drv: Warning - scc->stat.tx_queued shows overflow"
			       " (%d) but queue is empty\n", scc->stat.tx_queued);
			       
			scc->stat.tx_queued = 0;	/* correct it */
			scc->stat.nospace = 54321;	/* draw attention to it */
			return;
		}
			
		bp = scc->sndq1->anext;	/* don't use the one we currently use */
		
		while (bp && (scc->stat.tx_queued > QUEUE_HYST))
		{
			bp = scc_free_chain(bp, BT_TRANSMIT);
			scc->stat.tx_queued--;
			scc->stat.nospace++;
		}
		
		scc->sndq1->anext = bp;
	}
}



/* ----> tx routine: decode KISS data and scc_enqueue it <---- */

/* send raw frame to SCC. used for AX.25 */
int scc_write(struct tty_struct *tty, int from_user, unsigned char *buf, int count)
{
	struct scc_channel * scc = tty->driver_data;
	unsigned char tbuf[BUFSIZE], *p;
	int cnt, cnt2;
	
	if (!tty) return count;
	
	if (scc_paranoia_check(scc, tty->device, "scc_write"))
		return 0;

	if (scc->kiss.tx_inhibit) return count;
	
	check_tx_queue(scc);

	cnt2 = count;
	
	while (cnt2)
	{
		cnt   = cnt2 > BUFSIZE? BUFSIZE:cnt2;
		cnt2 -= cnt;
		
		if (from_user)
			memcpy_fromfs(tbuf, buf, cnt);
		else
			memcpy(tbuf, buf, cnt);
		
		buf += cnt;
			
		p=tbuf;
		
		while(cnt--) 
		  if (kiss_decode(scc, *p++))
		  {
		  	scc->stat.nospace++;
		  	return 0;
		  }
		  	
	} /* while cnt2 */
	
	return count;
}
				

/* put a single char into the buffer */

static void scc_put_char(struct tty_struct * tty, unsigned char ch)
{
	struct scc_channel *scc = tty->driver_data;
	unsigned char ch2;
	
	if (scc_paranoia_check(scc, tty->device, "scc_put_char"))
		return;
		
	ch2 = ch;
	scc_write(tty, 0, &ch2, 1);	/* that's all */
}

static void scc_flush_chars(struct tty_struct * tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	scc_paranoia_check(scc, tty->device, "scc_flush_chars"); /* just to annoy the user... */
	
	return;	/* no flush needed */
}

/* the kernel does NOT use this routine yet... */

static int scc_write_room(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc_paranoia_check(scc, tty->device, "scc_write_room"))
		return 0;
	
	if (scc->stat.tx_alloc >= QUEUE_THRES)
	{
		printk("scc_write_room(): buffer full (ignore)\n");
		return 0;
	}
		
	return BUFSIZE;
}

static int scc_chars_in_buffer(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc && scc->sndq2)
		return scc->sndq2->cnt;
	else
		return 0;
}

static void scc_flush_buffer(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc_paranoia_check(scc, tty->device, "scc_flush_buffer"))
		return;
		
	scc->stat.tx_kiss_state = KISS_IDLE;
	
	wake_up_interruptible(&tty->write_wait);
	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
	    tty->ldisc.write_wakeup)
		(tty->ldisc.write_wakeup)(tty);
}

static void scc_throttle(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc_paranoia_check(scc, tty->device, "scc_throttle"))
		return;
		
		
	/* dummy */
}

static void scc_unthrottle(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc_paranoia_check(scc, tty->device, "scc_unthrottle"))
		return;
		
	/* dummy */
}

static void scc_start(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc_paranoia_check(scc, tty->device, "scc_start"))
		return;
		
	/* dummy */
}
	                                            	

static void scc_stop(struct tty_struct *tty)
{
	struct scc_channel *scc = tty->driver_data;
	
	if (scc_paranoia_check(scc, tty->device, "scc_stop"))
		return;
		
	/* dummy */
}


/* ******************************************************************** */
/* * 			Init SCC driver 			      * */
/* ******************************************************************** */

long scc_init (long kmem_start)
{
	int chip, chan;
	register io_port ctrl;
	long flags;
	
	
        memset(&scc_driver, 0, sizeof(struct tty_driver));
        scc_driver.magic = TTY_DRIVER_MAGIC;
        scc_driver.name = "sc";
        scc_driver.major = TTY_MAJOR;		
        scc_driver.minor_start = 96;
        scc_driver.num = Nchips*2;
        scc_driver.type = TTY_DRIVER_TYPE_SERIAL;
        scc_driver.subtype = 0;			/* not needed */
        scc_driver.init_termios = tty_std_termios;
        scc_driver.init_termios.c_cflag = B9600	| CS8 | CREAD | HUPCL | CLOCAL;
        scc_driver.flags = TTY_DRIVER_REAL_RAW;
        scc_driver.refcount = &scc_refcount;	/* not needed yet */
        scc_driver.table = scc_table;
        scc_driver.termios = (struct termios **) scc_termios;
        scc_driver.termios_locked = (struct termios **) scc_termios_locked;
        scc_driver.open = scc_open;
        scc_driver.close = scc_close;
        scc_driver.write = scc_write;
        scc_driver.start = scc_start;
        scc_driver.stop = scc_stop;
        
        scc_driver.put_char = scc_put_char;
        scc_driver.flush_chars = scc_flush_chars;        
	scc_driver.write_room = scc_write_room;
	scc_driver.chars_in_buffer = scc_chars_in_buffer;
	scc_driver.flush_buffer = scc_flush_buffer;
	
	scc_driver.throttle = scc_throttle;
	scc_driver.unthrottle = scc_unthrottle;
        
        scc_driver.ioctl = scc_ioctl;
        scc_driver.set_termios = scc_set_termios;
        
        if (tty_register_driver(&scc_driver))
           panic("Couldn't register Z8530 SCC driver\n");
                                
	printk (BANNER);
	
	if (Nchips > MAXSCC) Nchips = MAXSCC;	/* to avoid the "DAU" (duemmster anzunehmender User) */

	/* reset and pre-init all chips in the system */
	
	for (chip = 0; chip < Nchips; chip++)
	{
		memset((char *) &SCC_Info[2*chip  ], 0, sizeof(struct scc_channel));
		memset((char *) &SCC_Info[2*chip+1], 0, sizeof(struct scc_channel));
		
		ctrl = SCC_ctrl[chip * 2];
		if (!ctrl) continue;

		save_flags(flags); cli();	/* because of 2-step accesses */
		

/* Hmm... this may fail on fast systems with cards who don't delay the INTACK */
/* If you are sure you specified the right port addresses and the driver doesn't */
/* recognize the chips, define DONT_CHECK in scc_config.h */

#ifndef DONT_CHECK
		check_region(ctrl, 1);

		Outb(ctrl, 0);
		OutReg(ctrl,R13,0x55);		/* is this chip realy there? */
		
		if (InReg(ctrl,R13) != 0x55 )
		{
			restore_flags(flags);
			continue;
		}
#endif
		
		SCC_Info[2*chip  ].magic    = SCC_MAGIC;
		SCC_Info[2*chip  ].ctrl     = SCC_ctrl[2*chip];
		SCC_Info[2*chip  ].data     = SCC_data[2*chip];
		SCC_Info[2*chip  ].enhanced = SCC_Enhanced[chip];
			
		SCC_Info[2*chip+1].magic    = SCC_MAGIC;
		SCC_Info[2*chip+1].ctrl     = SCC_ctrl[2*chip+1];
		SCC_Info[2*chip+1].data     = SCC_data[2*chip+1];
		SCC_Info[2*chip+1].enhanced = SCC_Enhanced[chip];

 
        	restore_flags(flags);
        }
        
#ifdef VERBOSE_BOOTMSG
	printk("Init Z8530 driver: %u channels, using irq %u\n",Nchips*2,Ivec);
	

	for (chan = 0; chan < Nchips * 2 ; chan++)
	{
		printk("/dev/%s%i: data port = 0x%3.3x  control port = 0x%3.3x -- %s\n",
			scc_driver.name, chan, SCC_data[chan], SCC_ctrl[chan],
			SCC_Info[chan].ctrl? "found"   : "missing");
			
		if (SCC_Info[chan].ctrl == 0) 
		{
			SCC_ctrl[chan] = 0;
		} else {
			request_region(SCC_ctrl[chan], 1, "scc ctrl");
			request_region(SCC_data[chan], 1, "scc data");
		}
	}
#else
	printk("Init Z8530 driver: %u channels\n",Nchips*2);
#endif

	
	return kmem_start;
}
#endif