summaryrefslogtreecommitdiffstats
path: root/drivers/net/hamradio/pt.c
blob: 86ed83d3bfb4019012fb0b89d790f45ce98d784d (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
#undef PT_DEBUG 1
/*
 * pt.c: Linux device driver for the Gracilis PackeTwin.
 * Copyright (c) 1995 Craig Small VK2XLZ (vk2xlz@vk2xlz.ampr.org.)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge MA 02139, USA.
 *
 * This driver is largely based upon the PI driver by David Perry.
 *
 * Revision History
 * 23/02/95 cs  Started again on driver, last one scrapped
 * 27/02/95 cs  Program works, we have chan A only.  Tx stays on
 * 28/02/95 cs  Fix Tx problem (& TxUIE instead of | )
 *		Fix Chan B Tx timer problem, used TMR2 instead of TMR1
 * 03/03/95 cs  Painfully found out (after 3 days) SERIAL_CFG is write only
 *              created image of it and DMA_CFG
 * 21/06/95 cs  Upgraded to suit PI driver 0.8 ALPHA
 * 22/08/95 cs	Changed it all around to make it like pi driver
 * 23/08/95 cs  It now works, got caught again by TMR2 and we must have
 *				auto-enables for daughter boards.
 * 07/10/95 cs  Fixed for 1.3.30 (hopefully)
 * 26/11/95 cs  Fixed for 1.3.43, ala 29/10 for pi2.c by ac
 * 21/12/95 cs  Got rid of those nasty warnings when compiling, for 1.3.48
 * 08/08/96 jsn Convert to use as a module. Removed send_kiss, empty_scc and
 *		pt_loopback functions - they were unused.
 * 13/12/96 jsn Fixed to match Linux networking changes.
 */

/*
 * default configuration of the PackeTwin,
 * ie What Craig uses his PT for.
 */
#define PT_DMA 3

#define DEF_A_SPEED	4800		/* 4800 baud */
#define DEF_A_TXDELAY	350		/* 350 mS */
#define DEF_A_PERSIST	64		/* 25% persistence */
#define DEF_A_SLOTIME	10		/* 10 mS */
#define DEF_A_SQUELDELAY 30		/* 30 mS */
#define DEF_A_CLOCKMODE	0		/* Normal clock mode */
#define DEF_A_NRZI		1		/* NRZI mode */

#define DEF_B_SPEED	0		/* 0 means external clock */
#define DEF_B_TXDELAY	250		/* 250 mS */
#define DEF_B_PERSIST	64		/* 25% */
#define DEF_B_SLOTIME	10		/* 10 mS */
#define DEF_B_SQUELDELAY 30		/* 30 mS */
#define DEF_B_CLOCKMODE 0 		/* Normal clock mode ?!? */
#define DEF_B_NRZI		1		/* NRZI mode */


#define	PARAM_TXDELAY	1
#define	PARAM_PERSIST	2
#define	PARAM_SLOTTIME	3
#define	PARAM_FULLDUP	5
#define	PARAM_HARDWARE	6
#define	PARAM_RETURN	255

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/uaccess.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/timer.h>
#include <linux/if_arp.h>
#include <linux/pt.h>
#include <linux/init.h>
#include "z8530.h"
#include <net/ax25.h>

struct mbuf {
    struct mbuf *next;
    int cnt;
    char data[0];
};

/*
 * The actual PT devices we will use
 */
static int pt0_preprobe(struct device *dev) {return 0;} /* Dummy probe function */
static struct device pt0a = { "pt0a", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, pt0_preprobe };
static struct device pt0b = { "pt0b", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, pt0_preprobe };

/* Ok, they shouldn't be here, but both channels share them */
/* The Images of the Serial and DMA config registers */
static unsigned char pt_sercfg = 0;
static unsigned char pt_dmacfg = 0;

/* The number of IO ports used by the card */
#define PT_TOTAL_SIZE   16

/* Index to functions, as function prototypes. */

static int pt_probe(struct device *dev);
static int pt_open(struct device *dev);
static int pt_send_packet(struct sk_buff *skb, struct device *dev);
static void pt_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static int pt_close(struct device *dev);
static int pt_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
static struct net_device_stats *pt_get_stats(struct device *dev);
static void pt_rts(struct pt_local *lp, int x);
static void pt_rxisr(struct device *dev);
static void pt_txisr(struct pt_local *lp);
static void pt_exisr(struct pt_local *lp);
static void pt_tmrisr(struct pt_local *lp);
static char *get_dma_buffer(unsigned long *mem_ptr);
static int valid_dma_page(unsigned long addr, unsigned long dev_buffsize);
static int hw_probe(int ioaddr);
static void tdelay(struct pt_local *lp, int time);
static void chipset_init(struct device *dev);

static char ax25_bcast[7] =
{'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1};
static char ax25_test[7] =
{'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1};



static int ext2_secrm_seed = 152;

static inline unsigned char random(void)
{
    return (unsigned char) (ext2_secrm_seed = ext2_secrm_seed * 60691 + 1);
}

static inline void wrtscc(int cbase, int ctl, int sccreg, unsigned char val)
{
    outb_p(sccreg, ctl);        /* Select register */
    outb_p(val, ctl);           /* Output value */
}

static inline unsigned char rdscc(int cbase, int ctl, int sccreg)
{
    unsigned char retval;

    outb_p(sccreg, ctl);        /* Select register */
    retval = inb_p(ctl);
    return retval;
}

static void switchbuffers(struct pt_local *lp)
{
    if (lp->rcvbuf == lp->rxdmabuf1)
	lp->rcvbuf = lp->rxdmabuf2;
    else
	lp->rcvbuf = lp->rxdmabuf1;
}

static void hardware_send_packet(struct pt_local *lp, struct sk_buff *skb)
{
	char kickflag;
	unsigned long flags;
	char *ptr;
	struct device *dev;

	/* First, let's see if this packet is actually a KISS packet */
	ptr = skb->data;
	if (ptr[0] != 0 && skb->len >= 2)
	{
#ifdef PT_DEBUG
		printk(KERN_DEBUG "PT: Rx KISS... Control = %d, value = %d.\n", ptr[0], (skb->len > 1? ptr[1] : -1));
#endif
		/* Kludge to get device */
		if ((struct pt_local*)(&pt0b.priv) == lp)
			dev = &pt0b;
		else
			dev = &pt0a;
		switch(ptr[0])
		{

			case PARAM_TXDELAY:
				/*TxDelay is in 10mS increments */
				lp->txdelay = ptr[1] * 10;
				break;
			case PARAM_PERSIST:
				lp->persist = ptr[1];
				break;
			case PARAM_SLOTTIME:
				lp->slotime = ptr[1];
				break;
			case PARAM_FULLDUP:
				/* Yeah right, you wish!  Fullduplex is a little while to
				 * go folks, but this is how you fire it up
				 */
				break;
			/* Perhaps we should have txtail here?? */
		} /*switch */
		return;
	}

	lp->stats.tx_packets++;
	lp->stats.tx_bytes+=skb->len;
	save_flags(flags);
	cli();
	kickflag = (skb_peek(&lp->sndq) == NULL) && (lp->sndbuf == NULL);
	restore_flags(flags);

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: hardware_send_packet(): kickflag = %d (%d).\n", kickflag, lp->base & CHANA);
#endif
	skb_queue_tail(&lp->sndq, skb);
	if (kickflag) 
	{
        /* Simulate interrupt to transmit */
        	if (lp->dmachan)
			pt_txisr(lp);
		else 
		{
            		save_flags(flags);
	           	cli();
            		if (lp->tstate == IDLE)
                		pt_txisr(lp);
            		restore_flags(flags);
		}
	}
} /* hardware_send_packet() */

static void setup_rx_dma(struct pt_local *lp)
{
	unsigned long flags;
	int cmd;
	unsigned long dma_abs;
	unsigned char dmachan;

	save_flags(flags);
	cli();

	dma_abs = (unsigned long) (lp->rcvbuf->data);
	dmachan = lp->dmachan;
	cmd = lp->base + CTL;

	if(!valid_dma_page(dma_abs, DMA_BUFF_SIZE + sizeof(struct mbuf)))
		panic("PI: RX buffer violates DMA boundary!");

	/* Get ready for RX DMA */
	wrtscc(lp->cardbase, cmd, R1, WT_FN_RDYFN | WT_RDY_RT | INT_ERR_Rx | EXT_INT_ENAB);

	disable_dma(dmachan);
	clear_dma_ff(dmachan);

	/*
	 *	Set DMA mode register to single transfers, incrementing address,
	 *	auto init, writes
	 */

	set_dma_mode(dmachan, DMA_MODE_READ | 0x10);
	set_dma_addr(dmachan, dma_abs);
	set_dma_count(dmachan, lp->bufsiz);
	enable_dma(dmachan);

	/*
	 *	If a packet is already coming in, this line is supposed to
	 *	avoid receiving a partial packet.
	 */

	wrtscc(lp->cardbase, cmd, R0, RES_Rx_CRC);

	/* Enable RX dma */
	wrtscc(lp->cardbase, cmd, R1,
		WT_RDY_ENAB | WT_FN_RDYFN | WT_RDY_RT | INT_ERR_Rx | EXT_INT_ENAB);

	restore_flags(flags);
}

static void setup_tx_dma(struct pt_local *lp, int length)
{
    unsigned long dma_abs;
    unsigned long flags;
    unsigned long dmachan;

    save_flags(flags);
    cli();

    dmachan = lp->dmachan;
    dma_abs = (unsigned long) (lp->txdmabuf);

    if(!valid_dma_page(dma_abs, DMA_BUFF_SIZE + sizeof(struct mbuf)))
	panic("PT: TX buffer violates DMA boundary!");

    disable_dma(dmachan);
    /* Set DMA mode register to single transfers, incrementing address,
     *  no auto init, reads
     */
    set_dma_mode(dmachan, DMA_MODE_WRITE);
    clear_dma_ff(dmachan);
    set_dma_addr(dmachan, dma_abs);
    /* output byte count */
    set_dma_count(dmachan, length);

    restore_flags(flags);
}

/*
 * This sets up all the registers in the SCC for the given channel
 * based upon tsync_hwint()
 */
static void scc_init(struct device *dev)
{
	unsigned long flags;
	struct pt_local *lp = (struct pt_local*) dev->priv;
	register int cmd = lp->base + CTL;
	int tc, br;

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: scc_init(): (%d).\n", lp->base & CHANA);
#endif
	save_flags(flags);
	cli();

	/* We may put something here to enable_escc */

	if (cmd & CHANA)
	{
	        wrtscc(lp->cardbase, cmd, R9, CHRA);	/* Reset channel A */
	        wrtscc(lp->cardbase, cmd, R2, 0xff);	/* Initialise interrupt vector */
	}
	else
	    	wrtscc(lp->cardbase, cmd, R9, CHRB);	/* Reset channel B */

	/* Deselect all Rx and Tx interrupts */
	wrtscc(lp->cardbase, cmd, R1, 0);

	/* Turn off external interrupts (like CTS/CD) */
	wrtscc(lp->cardbase, cmd, R15, 0);

	/* X1 clock, SDLC mode */
	wrtscc(lp->cardbase, cmd, R4, SDLC | X1CLK);

	/* Preset CRC and set mode */
	if (lp->nrzi)
	    	/* Preset Tx CRC, put into NRZI mode */
		wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI);
	else
		/* Preset Tx CRC, put into NRZ mode */
		wrtscc(lp->cardbase, cmd, R10, CRCPS);

	/* Tx/Rx parameters */
	if (lp->speed)		/* Use internal clocking */
	       /* Tx Clk from BRG. Rx Clk form DPLL, TRxC pin outputs DPLL */
	       wrtscc(lp->cardbase, cmd, R11, TCBR | RCDPLL | TRxCDP | TRxCOI);
	else	/* Use external clocking */
	{
		/* Tx Clk from TRxCL. Rx Clk from RTxCL, TRxC pin if input */
		wrtscc(lp->cardbase, cmd, R11, TCTRxCP | RCRTxCP | TRxCBR);
        	wrtscc(lp->cardbase,cmd, R14, 0);	/* wiz1 */
	}

	/* Null out SDLC start address */
	wrtscc(lp->cardbase, cmd, R6, 0);

	/* SDLC flag */
	wrtscc(lp->cardbase, cmd, R7, FLAG);

	/* Setup Tx but don't enable it */
	wrtscc(lp->cardbase, cmd, R5, Tx8 | DTR);

	/* Setup Rx */
	wrtscc(lp->cardbase, cmd, R3, AUTO_ENAB | Rx8);

	/* Setup the BRG, turn it off first */
	wrtscc(lp->cardbase, cmd, R14, BRSRC);

	/* set the 32x time constant for the BRG in Rx mode */
	if (lp->speed)
	{
		br = lp->speed;
		tc = ((lp->xtal / 32) / (br * 2)) - 2;
		wrtscc(lp->cardbase, cmd, R12, tc & 0xff);		/* lower byte */
   		wrtscc(lp->cardbase, cmd, R13, (tc >> 8) & 0xff);	/* upper byte */
	}

	/* Turn transmitter off, to setup stuff */
   	pt_rts(lp, OFF);

	/* External clocking */
	if (lp->speed)
	{
		/* DPLL frm BRG, BRG src PCLK */
		wrtscc(lp->cardbase, cmd, R14, BRSRC | SSBR);
		wrtscc(lp->cardbase, cmd, R14, BRSRC | SEARCH);	/* SEARCH mode, keep BRG src */
		wrtscc(lp->cardbase, cmd, R14, BRSRC | BRENABL);	/* Enable the BRG */

	    /* Turn off external clock port */
		if (lp->base & CHANA)
			outb_p( (pt_sercfg &= ~PT_EXTCLKA), (lp->cardbase + SERIAL_CFG) );
		else
			outb_p( (pt_sercfg &= ~PT_EXTCLKB), (lp->cardbase + SERIAL_CFG) );
	}
	else
	{
		/* DPLL frm rtxc,BRG src PCLK */
		/* Turn on external clock port */
		if (lp->base & CHANA)
			outb_p( (pt_sercfg |= PT_EXTCLKA), (lp->cardbase + SERIAL_CFG) );
		else
			outb_p( (pt_sercfg |= PT_EXTCLKB), (lp->cardbase + SERIAL_CFG) );
	}

	if (!lp->dmachan)
		wrtscc(lp->cardbase, cmd, R1, (INT_ALL_Rx | EXT_INT_ENAB));

	wrtscc(lp->cardbase, cmd, R15, BRKIE);	/* ABORT int */

	/* Turn on the DTR to tell modem we're alive */
	if (lp->base & CHANA)
		outb_p( (pt_sercfg |= PT_DTRA_ON), (lp->cardbase + SERIAL_CFG) );
	else
	    	outb_p( (pt_sercfg |= PT_DTRB_ON), (lp->cardbase + SERIAL_CFG) );

	/* Now, turn on the receiver and hunt for a flag */
	wrtscc(lp->cardbase, cmd, R3, RxENABLE | RxCRC_ENAB | AUTO_ENAB | Rx8 );

	restore_flags(flags);

} /* scc_init() */

/* Resets the given channel and whole SCC if both channels off */
static void chipset_init(struct device *dev)
{

	struct pt_local *lp = (struct pt_local*) dev->priv;
#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: chipset_init(): pt0a tstate = %d.\n", ((struct pt_local*)pt0a.priv)->tstate);
	printk(KERN_DEBUG "PT: chipset_init(): pt0b tstate = %d.\n", ((struct pt_local*)pt0b.priv)->tstate);
#endif
	/* Reset SCC if both channels are to be canned */
	if ( ((lp->base & CHANA) && !(pt_sercfg & PT_DTRB_ON)) ||
			(!(lp->base & CHANA) && !(pt_sercfg & PT_DTRA_ON)) )
	{
		wrtscc(lp->cardbase, lp->base + CTL, R9, FHWRES);
        	/* Reset int and dma registers */
	        outb_p((pt_sercfg = 0), lp->cardbase + SERIAL_CFG);
	        outb_p((pt_dmacfg = 0), lp->cardbase + DMA_CFG);
#ifdef PT_DEBUG
		printk(KERN_DEBUG "PT: chipset_init() Resetting SCC, called by ch (%d).\n", lp->base & CHANA);
#endif
	}
	/* Reset individual channel */
    	if (lp->base & CHANA) {
        	wrtscc(lp->cardbase, lp->base + CTL, R9, MIE | DLC | NV | CHRA);
        	outb_p( (pt_sercfg &= ~PT_DTRA_ON), lp->cardbase + SERIAL_CFG);
    	} else {
        	wrtscc(lp->cardbase, lp->base + CTL, R9, MIE | DLC | NV | CHRB);
			outb_p( (pt_sercfg &= ~PT_DTRB_ON), lp->cardbase + SERIAL_CFG);
	}
} /* chipset_init() */



__initfunc(int pt_init(void))
{
    int *port;
    int ioaddr = 0;
    int card_type = 0;
    int ports[] =
    { 0x230, 0x240, 0x250, 0x260, 0x270, 0x280, 0x290, 0x2a0,
      0x2b0, 0x300, 0x330, 0x3f0,  0};

    printk(KERN_INFO "PT: 0.41 ALPHA 07 October 1995 Craig Small (csmall@small.dropbear.id.au)\n");

    for (port = &ports[0]; *port && !card_type; port++) {
        ioaddr = *port;

        if (check_region(ioaddr, PT_TOTAL_SIZE) == 0) {
            printk(KERN_INFO "PT: Probing for card at address %#3x\n", ioaddr);
            card_type = hw_probe(ioaddr);
        }
    }
    if (card_type) {
        printk(KERN_INFO "PT: Found a PT at address %#3x\n",ioaddr);
    } else {
        printk(KERN_ERR "PT: ERROR: No card found.\n");
        return -EIO;
    }

    /*
     * Link a couple of device structures into the chain
     *
     * For the A port
     * Allocate space for 4 buffers even though we only need 3,
     * because one of them may cross a DMA page boundary and
     * be rejected by get_dma_buffer().
     */
    register_netdev(&pt0a);

    pt0a.priv= kmalloc(sizeof(struct pt_local) + (DMA_BUFF_SIZE + sizeof(struct mbuf)) * 4, GFP_KERNEL | GFP_DMA);

    pt0a.dma = 0;	/* wizzer - no dma yet */
    pt0a.base_addr = ioaddr + CHANA;
    pt0a.irq = 0;

    /* And B port */
    register_netdev(&pt0b);

    pt0b.priv= kmalloc(sizeof(struct pt_local) + (DMA_BUFF_SIZE + sizeof(struct mbuf)) * 4, GFP_KERNEL | GFP_DMA);

    pt0b.base_addr = ioaddr + CHANB;
    pt0b.irq = 0;

    /* Now initialise them */
    pt_probe(&pt0a);
    pt_probe(&pt0b);

    pt0b.irq = pt0a.irq;	/* IRQ is shared */

    return 0;
} /* pt_init() */

/*
 * Probe for PT card.  Also initialises the timers
 */
__initfunc(static int hw_probe(int ioaddr))
{
    int time = 1000;		/* Number of milliseconds to test */
    int a = 1;
    int b = 1;
    unsigned long start_time, end_time;

    inb_p(ioaddr + TMR1CLR);
    inb_p(ioaddr + TMR2CLR);

    /* Timer counter channel 0, 1mS period */
    outb_p(SC0 | LSB_MSB | MODE3, ioaddr + TMRCMD);
    outb_p(0x00, ioaddr + TMR0);
    outb_p(0x18, ioaddr + TMR0);

    /* Setup timer control word for timer 1 */
    outb_p(SC1 | LSB_MSB | MODE0, ioaddr + TMRCMD);
    outb_p((time << 1) & 0xff, ioaddr + TMR1);
    outb_p((time >> 7) & 0xff, ioaddr + TMR1);

    /* wait until counter reg is loaded */
    do {
        /* Latch count for reading */
        outb_p(SC1, ioaddr + TMRCMD);
        a = inb_p(ioaddr + TMR1);
        b = inb_p(ioaddr + TMR1);
    } while (b == 0);
    start_time = jiffies;
    while(b != 0)
    {
        /* Latch count for reading */
        outb_p(SC1, ioaddr + TMRCMD);
        a = inb_p(ioaddr + TMR1);
        b = inb_p(ioaddr + TMR1);
        end_time = jiffies;
        /* Don't wait forever - there may be no card here */
        if ((end_time - start_time) > 200)
        {
        	inb_p(ioaddr + TMR1CLR);
            return 0;
        }
    }

    /* Now fix the timers up for general operation */

    /* Clear the timers */
    inb_p(ioaddr + TMR1CLR);
    inb_p(ioaddr + TMR2CLR);

    outb_p(SC1 | LSB_MSB | MODE0, ioaddr + TMRCMD);
    inb_p(ioaddr + TMR1CLR);

    outb_p(SC2 | LSB_MSB | MODE0, ioaddr + TMRCMD);
    /* Should this be tmr1 or tmr2? wiz3*/
    inb_p(ioaddr + TMR1CLR);

    return 1;
} /* hw_probe() */


static void pt_rts(struct pt_local *lp, int x)
{
	int tc;
	long br;
	int cmd = lp->base + CTL;
#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_rts(): Transmitter status will be %d (%d).\n", x, lp->base & CHANA);
#endif
	if (x == ON) {
	    /* Ex ints off to avoid int */
	    wrtscc(lp->cardbase, cmd, R15, 0);
	    wrtscc(lp->cardbase, cmd, R3, AUTO_ENAB | Rx8);	/* Rx off */
	    lp->rstate = IDLE;

	    if(lp->dmachan)
	    {
	        /* Setup for Tx DMA */
	        wrtscc(lp->cardbase, cmd, R1, WT_FN_RDYFN | EXT_INT_ENAB);
	    } else {
	        /* No interrupts */
	        wrtscc(lp->cardbase, cmd, R1, 0);
	    }

            if (!lp->clockmode)
            {
                if (lp->speed)
                {
                    br = lp->speed;
                    tc = (lp->xtal / (br * 2)) - 2;
                    wrtscc(lp->cardbase, cmd, R12, tc & 0xff);
                    wrtscc(lp->cardbase, cmd, R13, (tc >> 8) & 0xff);
                }
            }
            /* Turn on Tx by raising RTS */
            wrtscc(lp->cardbase, cmd, R5, TxCRC_ENAB | RTS | TxENAB | Tx8 | DTR);
            /* Transmitter on now */
        } else {		/* turning off Tx */
            lp->tstate = IDLE;

            /* Turn off Tx by dropping RTS */
            wrtscc(lp->cardbase, cmd, R5, Tx8 | DTR);
            if (!lp->clockmode)
            {
                if (lp->speed)		/* internally clocked */
                {
                    /* Reprogram BRG from 32x clock for Rx DPLL */
                    /* BRG off, keep PClk source */
                    wrtscc(lp->cardbase, cmd, R14, BRSRC);
                    br = lp->speed;
                    tc = ((lp->xtal / 32) / (br * 2)) - 2;
                    wrtscc(lp->cardbase, cmd, R12, tc & 0xff);
                    wrtscc(lp->cardbase, cmd, R13, (tc >> 8) & 0xff);

                    /* SEARCH mode, BRG source */
                    wrtscc(lp->cardbase, cmd, R14, BRSRC | SEARCH);
                    /* Enable the BRG */
                    wrtscc(lp->cardbase, cmd, R14, BRSRC | BRENABL);
                }
            }
            /* Flush Rx fifo */
            /* Turn Rx off */
            wrtscc(lp->cardbase, cmd, R3, AUTO_ENAB | Rx8);

            /* Reset error latch */
            wrtscc(lp->cardbase, cmd, R0, ERR_RES);

            /* get status byte from R1 */
            (void) rdscc(lp->cardbase, cmd, R1);

            /* Read and dump data in queue */
            (void) rdscc(lp->cardbase, cmd, R8);
            (void) rdscc(lp->cardbase, cmd, R8);
            (void) rdscc(lp->cardbase, cmd, R8);

            /* Now, turn on Rx and hunt for a flag */
              wrtscc(lp->cardbase, cmd, R3, RxENABLE | AUTO_ENAB | Rx8 );

            lp->rstate = ACTIVE;

            if (lp->dmachan)
            {
                setup_rx_dma(lp);
            } else {
                /* Reset buffer pointers */
                lp->rcp = lp->rcvbuf->data;
                lp->rcvbuf->cnt = 0;
                /* Allow aborts to interrupt us */
                wrtscc(lp->cardbase, cmd, R1, INT_ALL_Rx | EXT_INT_ENAB);

	}
	wrtscc(lp->cardbase, cmd, R15, BRKIE );
    }
} /* pt_rts() */


static int valid_dma_page(unsigned long addr, unsigned long dev_bufsize)
{
    if (((addr & 0xffff) + dev_bufsize) <= 0x10000)
        return 1;
    else
        return 0;
}

static int pt_set_mac_address(struct device *dev, void *addr)
{
	struct sockaddr *sa = (struct sockaddr *)addr;
	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);		/* addr is an AX.25 shifted ASCII */
	return 0;		/* mac address */
}


/* Allocate a buffer which does not cross a DMA page boundary */
static char * get_dma_buffer(unsigned long *mem_ptr)
{
    char *ret;

    ret = (char *) *mem_ptr;

    if (!valid_dma_page(*mem_ptr, DMA_BUFF_SIZE + sizeof(struct mbuf))) {
        *mem_ptr += (DMA_BUFF_SIZE + sizeof(struct mbuf));
        ret = (char *) *mem_ptr;
    }
    *mem_ptr += (DMA_BUFF_SIZE + sizeof(struct mbuf));
    return (ret);
} /* get_dma_buffer() */


/*
 * Sets up all the structures for the PT device
 */
static int pt_probe(struct device *dev)
{
    short ioaddr;
    struct pt_local *lp;
    unsigned long flags;
    unsigned long mem_ptr;

    ioaddr = dev->base_addr;

    /*
     * Initialise the device structure.
     * Must be done before chipset_init()
     * Make sure data structures used by  the PT are aligned
     */
    dev->priv = (void *) (((int) dev->priv + 7) & ~7);
    lp = (struct pt_local*) dev->priv;

    memset(dev->priv, 0, sizeof(struct pt_local));

    /* Allocate some buffers which do not cross DMA boundaries */
    mem_ptr = (unsigned long) dev->priv + sizeof(struct pt_local);
    lp->txdmabuf = get_dma_buffer(&mem_ptr);
    lp->rxdmabuf1 = (struct mbuf *) get_dma_buffer(&mem_ptr);
    lp->rxdmabuf2 = (struct mbuf *) get_dma_buffer(&mem_ptr);

    /* Initialise the Rx buffer */
    lp->rcvbuf = lp->rxdmabuf1;
    lp->rcp = lp->rcvbuf->data;
    lp->rcvbuf->cnt = 0;

    /* Initialise the transmit queue head structure */
    skb_queue_head_init(&lp->sndq);

    lp->base = dev->base_addr;
    lp->cardbase = dev->base_addr & 0x3f0;

    /* These need to be initialised before scc_init() is called.
     */
    lp->xtal = XTAL;

    if (dev->base_addr & CHANA) {
        lp->speed = DEF_A_SPEED;
        lp->txdelay = DEF_A_TXDELAY;
        lp->persist = DEF_A_PERSIST;
        lp->slotime = DEF_A_SLOTIME;
        lp->squeldelay = DEF_A_SQUELDELAY;
        lp->clockmode = DEF_A_CLOCKMODE;
        lp->nrzi = DEF_A_NRZI;
    } else {
        lp->speed = DEF_B_SPEED;
        lp->txdelay = DEF_B_TXDELAY;
        lp->persist = DEF_B_PERSIST;
        lp->slotime = DEF_B_SLOTIME;
        lp->squeldelay = DEF_B_SQUELDELAY;
        lp->clockmode = DEF_B_CLOCKMODE;
        lp->nrzi = DEF_B_NRZI;
    }
    lp->bufsiz = DMA_BUFF_SIZE;
    lp->tstate = IDLE;

    chipset_init(dev);

    if (dev->base_addr & CHANA) {
        /* Note that a single IRQ services 2 devices (A and B channels)
        */

	/*
	 * We disable the dma for a while, we have to get ints working
	 * properly first!!
	 */
	lp->dmachan = 0;

        if (dev->irq < 2) {
            autoirq_setup(0);

            /* Turn on PT interrupts */
            save_flags(flags);
            cli();
            outb_p( pt_sercfg |= PT_EI, lp->cardbase + INT_CFG);
            restore_flags(flags);

            /* Set a timer interrupt */
            tdelay(lp, 1);
            dev->irq = autoirq_report(20);

	    /* Turn off PT interrupts */
	    save_flags(flags);
	    cli();
            outb_p( (pt_sercfg  &= ~ PT_EI), lp->cardbase + INT_CFG);
            restore_flags(flags);

            if (!dev->irq) {
                printk(KERN_ERR "PT: ERROR: Failed to detect IRQ line, assuming IRQ7.\n");
            }
        }

        printk(KERN_INFO "PT: Autodetected IRQ %d, assuming DMA %d\n", dev->irq, dev->dma);

        /* This board has jumpered interrupts. Snarf the interrupt vector
         * now.  There is no point in waiting since no other device can use
         * the interrupt, and this marks the 'irqaction' as busy.
         */
        {
            int irqval = request_irq(dev->irq, &pt_interrupt,0, "pt", dev);
            if (irqval) {
                printk(KERN_ERR "PT: ERROR: Unable to get IRQ %d (irqval = %d).\n",
                    dev->irq, irqval);
                return EAGAIN;
            }
        }

        /* Grab the region */
        request_region(ioaddr & 0x3f0, PT_TOTAL_SIZE, "pt" );
    } /* A port */
    dev->open = pt_open;
    dev->stop = pt_close;
    dev->do_ioctl = pt_ioctl;
    dev->hard_start_xmit = pt_send_packet;
    dev->get_stats = pt_get_stats;

    /* Fill in the fields of the device structure */
    dev_init_buffers(dev);

#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
    dev->hard_header    = ax25_encapsulate;
    dev->rebuild_header = ax25_rebuild_header;
#endif

    dev->set_mac_address = pt_set_mac_address;

    dev->type = ARPHRD_AX25;            /* AF_AX25 device */
    dev->hard_header_len = 73;      /* We do digipeaters now */
    dev->mtu = 1500;                /* eth_mtu is default */
    dev->addr_len = 7;               /* sizeof an ax.25 address */
    memcpy(dev->broadcast, ax25_bcast, 7);
    memcpy(dev->dev_addr, ax25_test, 7);

    /* New style flags */
    dev->flags = 0;

    return 0;
} /* pt_probe() */


/* Open/initialise the board.  This is called (in the current kernel)
 * sometime after booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that 'should' only be set once at boot, so that there is
 * a non-reboot way to recover if something goes wrong.
 * derived from last half of tsync_attach()
 */
static int pt_open(struct device *dev)
{
    unsigned long flags;
    struct pt_local *lp = dev->priv;
    static first_time = 1;

    if (dev->base_addr & CHANA)
    {
        if (first_time)
        {
            if (request_dma(dev->dma, "pt"))
            {
                free_irq(dev->irq, dev);
                return -EAGAIN;
            }
        }

         /* Reset hardware */
         chipset_init(dev);
     }
     lp->tstate = IDLE;

     if (dev->base_addr & CHANA)
     {
         scc_init(dev);
         scc_init(dev->next);
     }
     /* Save a copy of register RR0 for comparing with later on */
     /* We always put 0 in zero count */
     lp->saved_RR0 = rdscc(lp->cardbase, lp->base + CTL, R0) & ~ZCOUNT;

    /* master interrupt enable */
    save_flags(flags);
    cli();
    wrtscc(lp->cardbase, lp->base + CTL, R9, MIE | NV);
    outb_p( pt_sercfg |= PT_EI, lp->cardbase + INT_CFG);
    restore_flags(flags);

    lp->open_time = jiffies;

    dev->tbusy = 0;
    dev->interrupt = 0;
    dev->start = 1;
    first_time = 0;

    MOD_INC_USE_COUNT;

    return 0;
} /* pt_open() */

static int pt_send_packet(struct sk_buff *skb, struct device *dev)
{
	struct pt_local *lp = (struct pt_local *) dev->priv;

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_send_packet(): (%d)\n", lp->base & CHANA);
#endif
	hardware_send_packet(lp, skb);
	dev->trans_start = jiffies;

	return 0;
}



/* The inverse routine to pt_open() */
static int pt_close(struct device *dev)
{
	unsigned long flags;
	struct pt_local *lp = dev->priv;
	struct sk_buff *ptr = NULL;
	int cmd;

	cmd = lp->base + CTL;

	save_flags(flags);
	cli();

	/* Reset SCC or channel */
	chipset_init(dev);
	disable_dma(lp->dmachan);

	lp->open_time = 0;
	dev->tbusy = 1;
	dev->start = 0;

	/* Free any buffers left in the hardware transmit queue */
	while ((ptr = skb_dequeue(&lp->sndq)) != NULL)
		kfree_skb(ptr);

	restore_flags(flags);

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_close(): Closing down channel (%d).\n", lp->base & CHANA);
#endif

	MOD_DEC_USE_COUNT;

	return 0;
} /* pt_close() */


static int pt_ioctl(struct device *dev, struct ifreq *ifr, int cmd)
{
    unsigned long flags;
    struct pt_req rq;
    struct pt_local *lp = (struct pt_local *) dev->priv;

    int ret = verify_area(VERIFY_WRITE, ifr->ifr_data, sizeof(struct pt_req));
    if (ret)
	return ret;

    if (cmd != SIOCDEVPRIVATE)
        return -EINVAL;

    copy_from_user(&rq, ifr->ifr_data, sizeof(struct pt_req));

    switch (rq.cmd) {
    case SIOCSPIPARAM:

	if (!suser())
	    return -EPERM;
	save_flags(flags);
	cli();
	lp->txdelay = rq.txdelay;
	lp->persist = rq.persist;
	lp->slotime = rq.slotime;
	lp->squeldelay = rq.squeldelay;
	lp->clockmode = rq.clockmode;
	lp->speed = rq.speed;
	pt_open(&pt0a);
	restore_flags(flags);
	ret = 0;
	break;

    case SIOCSPIDMA:

	if (!suser())
	    return -EPERM;
	ret = 0;
	if (dev->base_addr & CHANA) {   /* if A channel */
	   if (rq.dmachan < 1 || rq.dmachan > 3)
		return -EINVAL;
	   save_flags(flags);
	   cli();
	   pt_close(dev);
	   free_dma(lp->dmachan);
	   dev->dma = lp->dmachan = rq.dmachan;
	   if (request_dma(lp->dmachan,"pt"))
		ret = -EAGAIN;
	   pt_open(dev);
	   restore_flags(flags);
	}
	break;

    case SIOCSPIIRQ:
	ret = -EINVAL;      /* add this later */
	break;

    case SIOCGPIPARAM:
    case SIOCGPIDMA:
    case SIOCGPIIRQ:

	rq.speed = lp->speed;
	rq.txdelay = lp->txdelay;
	rq.persist = lp->persist;
	rq.slotime = lp->slotime;
	rq.squeldelay = lp->squeldelay;
	rq.clockmode = lp->clockmode;
	rq.dmachan = lp->dmachan;
	rq.irq = dev->irq;
	copy_to_user(ifr->ifr_data, &rq, sizeof(struct pt_req));
	ret = 0;
	break;

    default:
	ret = -EINVAL;
    }
    return ret;
}

/*
 *	Get the current statistics.
 *	This may be called with the card open or closed. 
 */
 
static struct net_device_stats *pt_get_stats(struct device *dev)
{
	struct pt_local *lp = (struct pt_local *) dev->priv;
	return &lp->stats;
}


/*
 * Local variables:
 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c skeleton.c"
 *  version-control: t
 *  kept-new-versions: 5
 *  tab-width: 4
 * End:
 */


static void tdelay(struct pt_local *lp, int time)
{
	/* For some reason, we turn off the Tx interrupts here! */
	if (!lp->dmachan)
		wrtscc(lp->cardbase, lp->base + CTL, R1, INT_ALL_Rx | EXT_INT_ENAB);

	if (lp->base & CHANA) 
	{
		outb_p(time & 0xff, lp->cardbase + TMR1);
		outb_p((time >> 8)&0xff, lp->cardbase + TMR1);
	}
	else
	{
		outb_p(time & 0xff, lp->cardbase + TMR2);
		outb_p((time >> 8)&0xff, lp->cardbase + TMR2);
	}
} /* tdelay */


static void pt_txisr(struct pt_local *lp)
{
	unsigned long flags;
	int cmd;
	unsigned char c;

	save_flags(flags);
	cli();
	cmd = lp->base + CTL;

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_txisr(): tstate = %d (%d).\n", lp->tstate, lp->base & CHANA);
#endif

	switch (lp->tstate)
	{
	case CRCOUT:
	    lp->tstate = FLAGOUT;
	    tdelay(lp, lp->squeldelay);
	    restore_flags(flags);
	    return;

	case IDLE:
	    /* Transmitter idle. Find a frame for transmission */
	    if ((lp->sndbuf = skb_dequeue(&lp->sndq)) == NULL)
	    {
	        /* Nothing to send - return to receive mode
	         * Tx off now - flag should have gone
	         */
	        pt_rts(lp, OFF);

	        restore_flags(flags);
	        return;
	    }
	    if (!lp->dmachan)
	    {
		    lp->txptr = lp->sndbuf->data;
		    lp->txptr++;		/* Ignore KISS control byte */
		    lp->txcnt = (int) lp->sndbuf->len - 1;
		}
	    /* If a buffer to send, drop though here */

	case DEFER:
	    /* Check DCD - debounce it */
	    /* See Intel Microcommunications Handbook p2-308 */
	    wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
	    wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
	    if ((rdscc(lp->cardbase, cmd, R0) & DCD) != 0)
	    {
	        lp->tstate = DEFER;
	        tdelay(lp, 100);
	        /* DEFER until DCD transition or timeout */
	        wrtscc(lp->cardbase, cmd, R15, DCDIE);
	        restore_flags(flags);
	        return;
	    }
	    if (random() > lp->persist)
	    {
	        lp->tstate = DEFER;
	        tdelay(lp, lp->slotime);
	        restore_flags(flags);
	        return;
	    }
	    pt_rts(lp, ON);		/* Tx on */
	    if (lp->dmachan)
	    	wrtscc(lp->cardbase, cmd, R5, TxCRC_ENAB | RTS | Tx8);
	    lp->tstate = ST_TXDELAY;
	    tdelay(lp, lp->txdelay);
	    restore_flags(flags);
	    return;

	case ACTIVE:
	    /* Here we are actively sending a frame */
	    if (lp->txcnt--)
	    {
	        /* XLZ - checkout Gracilis PT code to see if the while
	         * loop is better or not.
	         */
	        c = *lp->txptr++;
	        /* next char is gone */
	        wrtscc(lp->cardbase, cmd, R8, c);
	        /* stuffing a char satisfies interrupt condition */
	    } else {
	        /* No more to send */
	        kfree_skb(lp->sndbuf);
	        lp->sndbuf = NULL;
	        if ((rdscc(lp->cardbase, cmd, R0) & TxEOM))
	        {
	            /* Did we underrun */
	            lp->stats.tx_errors++;
	            lp->stats.tx_fifo_errors++;
	            wrtscc(lp->cardbase, cmd, R0, SEND_ABORT);
	            lp->tstate = FLAGOUT;
	            tdelay(lp, lp->squeldelay);
	            restore_flags(flags);
	            return;
	        }
	        lp->tstate = UNDERRUN;
	        /* Send flags on underrun */
	       if (lp->nrzi)
	       {
	           wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI);
	       } else {
	           wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZ);
	       }
	       /* Reset Tx interrupt pending */
	       wrtscc(lp->cardbase, cmd, R0, RES_Tx_P);
	   }
	   restore_flags(flags);
	   return;
	default:
		printk(KERN_ERR "PT: pt_txisr(): Invalid tstate (%d) for chan %s.\n", lp->tstate, (cmd & CHANA? "A": "B") );
		pt_rts(lp, OFF);
		lp->tstate = IDLE;
		break;
    } 				/*switch */
    restore_flags(flags);
}

static void pt_rxisr(struct device *dev)
{
    struct pt_local *lp = (struct pt_local*) dev->priv;
    int cmd = lp->base + CTL;
    int bytecount;
    unsigned long flags;
    char rse;
    struct sk_buff *skb;
    int sksize, pkt_len;
    struct mbuf *cur_buf = NULL;
    unsigned char *cfix;

    save_flags(flags);
    cli();

    /* Get status byte from R1 */
    rse = rdscc(lp->cardbase, cmd, R1);

#ifdef PT_DEBUG
    printk(KERN_DEBUG "PT: pt_rxisr(): R1 = %#3x. (%d)\n", rse, lp->base & CHANA);
#endif

	if (lp->dmachan && (rse & Rx_OVR))
		lp->rstate = RXERROR;

    if (rdscc(lp->cardbase, cmd, R0) & Rx_CH_AV && !lp->dmachan)
    {
        /* There is a char to be stored
         * Read special condition bits before reading the data char
         */
        if (rse & Rx_OVR)
        {
             /* Rx overrun - toss buffer */
             /* wind back the pointers */
             lp->rcp = lp->rcvbuf->data;
             lp->rcvbuf->cnt = 0;
             lp->rstate = RXERROR;
             lp->stats.rx_errors++;
             lp->stats.rx_fifo_errors++;
         } else if (lp->rcvbuf->cnt >= lp->bufsiz)
             {
                 /* Too large packet
                  * wind back Rx buffer pointers
                  */
                 lp->rcp = lp->rcvbuf->data;
                 lp->rcvbuf->cnt = 0;
                 lp->rstate = TOOBIG;
             }
         /* ok, we can store the Rx char if no errors */
         if (lp->rstate == ACTIVE)
         {
             *lp->rcp++ = rdscc(lp->cardbase, cmd, R8);
             lp->rcvbuf->cnt++;
         } else {
             /* we got an error, dump the FIFO */
             (void) rdscc(lp->cardbase, cmd, R8);
             (void) rdscc(lp->cardbase, cmd, R8);
             (void) rdscc(lp->cardbase, cmd, R8);

             /* Reset error latch */
             wrtscc(lp->cardbase, cmd, R0, ERR_RES);
             lp->rstate = ACTIVE;

             /* Resync the SCC */
             wrtscc(lp->cardbase, cmd, R3, RxENABLE | ENT_HM | AUTO_ENAB | Rx8);

         }
     }

     if (rse & END_FR)
     {
#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_rxisr() Got end of a %u byte frame.\n", lp->rcvbuf->cnt);
#endif
		if (lp->dmachan)
		{
			clear_dma_ff(lp->dmachan);
			bytecount = lp->bufsiz - get_dma_residue(lp->dmachan);
		} else {
			bytecount = lp->rcvbuf->cnt;
		}

         /* END OF FRAME - Make sure Rx was active */
         if (lp->rcvbuf->cnt > 0 || lp->dmachan)
         {
             if ((rse & CRC_ERR) || (lp->rstate > ACTIVE) || (bytecount < 10))
             {
                 if ((bytecount >= 10) && (rse & CRC_ERR))
                 {
                     lp->stats.rx_crc_errors++;
                 }
                 if (lp->dmachan)
                 {
                 	if (lp->rstate == RXERROR)
                 	{
                 		lp->stats.rx_errors++;
                 		lp->stats.rx_over_errors++;
                 	}
                 	lp->rstate = ACTIVE;
                 	setup_rx_dma(lp);
                 } else {
	                 /* wind back Rx buffer pointers */
    	             lp->rcp = lp->rcvbuf->data;
        	         lp->rcvbuf->cnt = 0;

					/* Re-sync the SCC */
					wrtscc(lp->cardbase, cmd, R3, RxENABLE | ENT_HM | AUTO_ENAB | Rx8);

        	     }
#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_rxisr() %s error.\n", (rse & CRC_ERR)? "CRC" : "state");
#endif
             } else {
                 /* We have a valid frame */
                 if (lp->dmachan)
                 {
                 	pkt_len = lp->rcvbuf->cnt = bytecount - 2 +1;
					/* Get buffer for next frame */
                 	cur_buf = lp->rcvbuf;
                 	switchbuffers(lp);
                 	setup_rx_dma(lp);
                 } else {
	                 pkt_len = lp->rcvbuf->cnt -= 2;  /* Toss 2 CRC bytes */
    	             pkt_len += 1;	/* make room for KISS control byte */
        		}

                 /* Malloc up new buffer */
                 sksize = pkt_len;
                 skb = dev_alloc_skb(sksize);
                 if (skb == NULL)
                 {
                     printk(KERN_ERR "PT: %s: Memory squeeze, dropping packet.\n", dev->name);
                     lp->stats.rx_dropped++;
                     restore_flags(flags);
                     return;
                 }
                 skb->dev = dev;

                 /* KISS kludge = prefix with a 0 byte */
                 cfix=skb_put(skb,pkt_len);
                 *cfix++=0;
                 /* skb->data points to the start of sk_buff area */
                 if (lp->dmachan)
                 	memcpy(cfix, (char*)cur_buf->data, pkt_len - 1);
                 else
                    memcpy(cfix, lp->rcvbuf->data, pkt_len - 1);
                 skb->protocol = ntohs(ETH_P_AX25);
                 skb->mac.raw=skb->data;
                 lp->stats.rx_bytes+=skb->len;
                 netif_rx(skb);
                 lp->stats.rx_packets++;
                 if (!lp->dmachan)
                 {
	                 /* packet queued - wind back buffer for next frame */
    	             lp->rcp = lp->rcvbuf->data;
	                 lp->rcvbuf->cnt = 0;
	             }
             } /* good frame */
         } /* check active Rx */
         /* Clear error status */
         lp->rstate = ACTIVE;
         /* Reset error latch */
     } /* end EOF check */
     wrtscc(lp->cardbase, cmd, R0, ERR_RES);
     restore_flags(flags);
} /* pt_rxisr() */

/*
 * This handles the two timer interrupts.
 * This is a real bugger, cause you have to rip it out of the pi's
 * external status code.  They use the CTS line or something.
 */
static void pt_tmrisr(struct pt_local *lp)
{
    unsigned long flags;

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: pt_tmrisr(): tstate = %d (%d).\n", lp->tstate, lp->base & CHANA);
#endif

    save_flags(flags);
    cli();


    switch (lp->tstate)
    {
    /* Most of this stuff is in pt_exisr() */
    case FLAGOUT:
    case ST_TXDELAY:
    case DEFER:
/*    case ACTIVE:
    case UNDERRUN:*/
        pt_exisr(lp);
        break;

    default:
	if (lp->base & CHANA)
 	    printk(KERN_ERR "PT: pt_tmrisr(): Invalid tstate %d for Channel A\n", lp->tstate);
	else
	    printk(KERN_ERR "PT: pt_tmrisr(): Invalid tstate %d for Channel B\n", lp->tstate);
	break;
    } /* end switch */
    restore_flags(flags);
} /* pt_tmrisr() */


/*
 * This routine is called by the kernel when there is an interrupt for the
 * PT.
 */
static void pt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
    /* It's a tad dodgy here, but we assume pt0a until proven otherwise */
    struct device *dev = &pt0a;
    struct pt_local *lp = dev->priv;
    unsigned char intreg;
    unsigned char st;
    register int cbase = dev->base_addr & 0x3f0;
    unsigned long flags;

    /* Read the PT's interrupt register, this is not the SCC one! */
    intreg = inb_p(cbase + INT_REG);
    while(( intreg & 0x07) != 0x07) {
        /* Read interrupt register pending from Channel A */
        while ((st = rdscc(cbase, cbase + CHANA + CTL, R3)) != 0)
        {
        	/* Read interrupt vector from R2, channel B */
#ifdef PT_DEBUG
		printk(KERN_DEBUG "PT: pt_interrupt(): R3 = %#3x", st);
#endif
/*        	st = rdscc(lp->cardbase, cbase + CHANB + CTL, R2) & 0x0e;*/
#ifdef PT_DEBUG
		printk(KERN_DEBUG "PI: R2 = %#3x.\n", st);
#endif
			if (st & CHARxIP) {
			    /* Channel A Rx */
	            lp = (struct pt_local*)pt0a.priv;
	            pt_rxisr(&pt0a);
	        } else if (st & CHATxIP) {
	            /* Channel A Tx */
	            lp = (struct pt_local*)pt0a.priv;
	            pt_txisr(lp);
	        } else if (st & CHAEXT) {
	        	/* Channel A External Status */
	        	lp = (struct pt_local*)pt0a.priv;
	        	pt_exisr(lp);
	        } else if (st & CHBRxIP) {
	        	/* Channel B Rx */
	            lp= (struct pt_local*)pt0b.priv;
	            pt_rxisr(&pt0b);
	        } else if (st & CHBTxIP) {
            	/* Channel B Tx */
	            lp = (struct pt_local*)pt0b.priv;
	            pt_txisr(lp);
			} else if (st & CHBEXT) {
	        	/* Channel B External Status */
	        	lp = (struct pt_local*)pt0b.priv;
	        	pt_exisr(lp);
	        }
            /* Reset highest interrupt under service */
            save_flags(flags);
            cli();
            wrtscc(lp->cardbase, lp->base + CTL, R0, RES_H_IUS);
            restore_flags(flags);
        }  /* end of SCC ints */

        if (!(intreg & PT_TMR1_MSK))
        {
            /* Clear timer 1 */
            inb_p(cbase + TMR1CLR);

            pt_tmrisr( (struct pt_local*)pt0a.priv);
        }

        if (!(intreg & PT_TMR2_MSK))
        {
            /* Clear timer 2 */
            inb_p(cbase + TMR2CLR);

            pt_tmrisr( (struct pt_local*)pt0b.priv);
        }

        /* Get the next PT interrupt vector */
        intreg = inb_p(cbase + INT_REG);
    } /* while (intreg) */
} /* pt_interrupt() */


static void pt_exisr(struct pt_local *lp)
{
    unsigned long flags;
    int cmd = lp->base + CTL;
    unsigned char st;
    char c;
    int length;

    save_flags(flags);
    cli();

    /* Get external status */
    st = rdscc(lp->cardbase, cmd, R0);

#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: exisr(): R0 = %#3x tstate = %d (%d).\n", st, lp->tstate, lp->base & CHANA);
#endif
    /* Reset external status latch */
    wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);

    if ((lp->rstate >= ACTIVE) && (st & BRK_ABRT) && lp->dmachan)
    {
    	setup_rx_dma(lp);
    	lp->rstate = ACTIVE;
    }

    switch (lp->tstate)
    {
    case ACTIVE:		/* Unexpected underrun */
#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: exisr(): unexpected underrun detected.\n");
#endif
        kfree_skb(lp->sndbuf);
        lp->sndbuf = NULL;
        if (!lp->dmachan)
        {
	        wrtscc(lp->cardbase, cmd, R0, SEND_ABORT);
	        lp->stats.tx_errors++;
	        lp->stats.tx_fifo_errors++;
	    }
        lp->tstate = FLAGOUT;
        tdelay(lp, lp->squeldelay);
        restore_flags(flags);
        return;
    case UNDERRUN:
        lp->tstate = CRCOUT;
        restore_flags(flags);
        return;
    case FLAGOUT:
        /* squeldelay has timed out */
        /* Find a frame for transmission */
        if ((lp->sndbuf = skb_dequeue(&lp->sndq)) == NULL)
        {
            /* Nothing to send - return to Rx mode */
            pt_rts(lp, OFF);
            lp->tstate = IDLE;
            restore_flags(flags);
            return;
        }
        if (!lp->dmachan)
        {
	        lp->txptr = lp->sndbuf->data;
    	    lp->txptr++;		/* Ignore KISS control byte */
	        lp->txcnt = (int) lp->sndbuf->len - 1;
	    }
        /* Fall through if we have a packet */

    case ST_TXDELAY:
    	if (lp->dmachan)
    	{
    		/* Disable DMA chan */
    		disable_dma(lp->dmachan);

    		/* Set up for TX dma */
    		wrtscc(lp->cardbase, cmd, R1, WT_FN_RDYFN | EXT_INT_ENAB);

    		length = lp->sndbuf->len - 1;
    		memcpy(lp->txdmabuf, &lp->sndbuf->data[1], length);

    		/* Setup DMA controller for Tx */
    		setup_tx_dma(lp, length);

    		enable_dma(lp->dmachan);

    		/* Reset CRC, Txint pending */
    		wrtscc(lp->cardbase, cmd, R0, RES_Tx_CRC | RES_Tx_P);

    		/* Allow underrun only */
    		wrtscc(lp->cardbase, cmd, R15, TxUIE);

    		/* Enable TX DMA */
    		wrtscc(lp->cardbase, cmd, R1, WT_RDY_ENAB | WT_FN_RDYFN | EXT_INT_ENAB);

    		/* Send CRC on underrun */
    		wrtscc(lp->cardbase, cmd, R0, RES_EOM_L);

    		lp->tstate = ACTIVE;
    		break;
    	}
        /* Get first char to send */
        lp->txcnt--;
        c = *lp->txptr++;
        /* Reset CRC for next frame */
        wrtscc(lp->cardbase, cmd, R0, RES_Tx_CRC);

        /* send abort on underrun */
        if (lp->nrzi)
        {
            wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZI | ABUNDER);
        } else {
            wrtscc(lp->cardbase, cmd, R10, CRCPS | NRZ | ABUNDER);
        }
        /* send first char */
        wrtscc(lp->cardbase, cmd, R8, c);

        /* Reset end of message latch */
        wrtscc(lp->cardbase, cmd, R0, RES_EOM_L);

        /* stuff an extra one in */
/*        while ((rdscc(lp->cardbase, cmd, R0) & Tx_BUF_EMP) && lp->txcnt)
        {
            lp->txcnt--;
            c = *lp->txptr++;
            wrtscc(lp->cardbase, cmd, R8, c);
        }*/

        /* select Tx interrupts to enable */
        /* Allow underrun int only */
        wrtscc(lp->cardbase, cmd, R15, TxUIE);

        /* Reset external interrupts */
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);

        /* Tx and Rx ints enabled */
        wrtscc(lp->cardbase, cmd, R1, TxINT_ENAB | EXT_INT_ENAB);

        lp->tstate = ACTIVE;
        restore_flags(flags);
        return;

        /* slotime has timed out */
    case DEFER:
        /* Check DCD - debounce it
         * see Intel Microcommunications Handbook, p2-308
         */
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
        wrtscc(lp->cardbase, cmd, R0, RES_EXT_INT);
        if ((rdscc(lp->cardbase, cmd, R0) & DCD) != 0)
        {
            lp->tstate = DEFER;
            tdelay(lp, 100);
            /* DEFER until DCD transition or timeout */
            wrtscc(lp->cardbase, cmd, R15, DCDIE);
            restore_flags(flags);
            return;
        }
        if (random() > lp->persist)
        {
            lp->tstate = DEFER;
            tdelay(lp, lp->slotime);
            restore_flags(flags);
            return;
        }
        if (lp->dmachan)
        	wrtscc(lp->cardbase, cmd, R5, TxCRC_ENAB | RTS | Tx8);
        pt_rts(lp, ON);			/* Tx on */
        lp->tstate = ST_TXDELAY;
        tdelay(lp, lp->txdelay);
        restore_flags(flags);
        return;

 	/* Only for int driven parts */
 	if (lp->dmachan)
 	{
 		restore_flags(flags);
 		return;
 	}

    } /* end switch */
    /*
     * Rx mode only
     * This triggers when hunt mode is entered, & since an ABORT
     * automatically enters hunt mode, we use that to clean up
     * any waiting garbage
     */
    if ((lp->rstate == ACTIVE) && (st & BRK_ABRT) )
    {
#ifdef PT_DEBUG
	printk(KERN_DEBUG "PT: exisr(): abort detected.\n");
#endif
  		/* read and dump all of SCC Rx FIFO */
        (void) rdscc(lp->cardbase, cmd, R8);
        (void) rdscc(lp->cardbase, cmd, R8);
        (void) rdscc(lp->cardbase, cmd, R8);

        lp->rcp = lp->rcvbuf->data;
        lp->rcvbuf->cnt = 0;

		/* Re-sync the SCC */
		wrtscc(lp->cardbase, cmd, R3, RxENABLE | ENT_HM | AUTO_ENAB | Rx8);

    }

    /* Check for DCD transitions */
    if ( (st & DCD) != (lp->saved_RR0 & DCD))
    {
#ifdef PT_DEBUG
        printk(KERN_DEBUG "PT: pt_exisr(): DCD is now %s.\n", (st & DCD)? "ON" : "OFF" );
#endif
		if (st & DCD)
		{
			/* Check that we don't already have some data */
			if (lp->rcvbuf->cnt > 0)
			{
#ifdef PT_DEBUG
				printk(KERN_DEBUG "PT: pt_exisr() dumping %u bytes from buffer.\n", lp->rcvbuf->cnt);
#endif
				/* wind back buffers */
				lp->rcp = lp->rcvbuf->data;
				lp->rcvbuf->cnt = 0;
			}
		} else {  /* DCD off */

			/* read and dump al SCC FIFO */
			(void)rdscc(lp->cardbase, cmd, R8);
			(void)rdscc(lp->cardbase, cmd, R8);
			(void)rdscc(lp->cardbase, cmd, R8);

			/* wind back buffers */
			lp->rcp = lp->rcvbuf->data;
			lp->rcvbuf->cnt = 0;

			/* Re-sync the SCC */
			wrtscc(lp->cardbase, cmd, R3, RxENABLE | ENT_HM | AUTO_ENAB | Rx8);
		}

    }
    /* Update the saved version of register RR) */
    lp->saved_RR0 = st &~ ZCOUNT;
    restore_flags(flags);

} /* pt_exisr() */

#ifdef MODULE
EXPORT_NO_SYMBOLS;

MODULE_AUTHOR("Craig Small VK2XLZ <vk2xlz@vk2xlz.ampr.org>");
MODULE_DESCRIPTION("AX.25 driver for the Gracillis PacketTwin HDLC card");

int init_module(void)
{
	return pt_init();
}

void cleanup_module(void)
{
	free_irq(pt0a.irq, &pt0a);	/* IRQs and IO Ports are shared */
	release_region(pt0a.base_addr & 0x3f0, PT_TOTAL_SIZE);

	kfree(pt0a.priv);
	pt0a.priv = NULL;
	unregister_netdev(&pt0a);

	kfree(pt0b.priv);
	pt0b.priv = NULL;
	unregister_netdev(&pt0b);
}
#endif