1 #define RCS_ID "$Id: scc.c,v 1.75 1998/11/04 15:15:01 jreuter Exp jreuter $"
140 #undef SCC_DONT_CHECK
142 #define SCC_MAXCHIPS 4
143 #define SCC_BUFSIZE 384
146 #define SCC_DEFAULT_CLOCK 4915200
151 #include <linux/module.h>
152 #include <linux/errno.h>
153 #include <linux/signal.h>
157 #include <linux/string.h>
158 #include <linux/in.h>
159 #include <linux/fcntl.h>
160 #include <linux/ptrace.h>
163 #include <linux/netdevice.h>
164 #include <linux/rtnetlink.h>
165 #include <linux/if_ether.h>
166 #include <linux/if_arp.h>
167 #include <linux/socket.h>
169 #include <linux/scc.h>
170 #include <linux/ctype.h>
171 #include <linux/kernel.h>
174 #include <linux/bitops.h>
181 #include <asm/uaccess.h>
186 "AX.25: Z8530 SCC driver version "VERSION".dl1bke\n";
188 static void t_dwait(
unsigned long);
189 static void t_txdelay(
unsigned long);
190 static void t_tail(
unsigned long);
191 static void t_busy(
unsigned long);
192 static void t_maxkeyup(
unsigned long);
193 static void t_idle(
unsigned long);
195 static void scc_start_tx_timer(
struct scc_channel *,
void (*)(
unsigned long),
unsigned long);
196 static void scc_start_maxkeyup(
struct scc_channel *);
199 static void z8530_init(
void);
216 static unsigned char SCC_DriverName[] =
"scc";
218 static struct irqflags {
unsigned char used : 1; } Ivec[
NR_IRQS];
222 static struct scc_ctrl {
228 static unsigned char Driver_Initialized;
241 static inline unsigned char InReg(
io_port port,
unsigned char reg)
256 spin_unlock_irqrestore(&iolock, flags);
272 spin_unlock_irqrestore(&iolock, flags);
278 OutReg(scc->
ctrl, reg, (scc->
wreg[reg] = val));
283 OutReg(scc->
ctrl, reg, (scc->
wreg[reg] |= val));
288 OutReg(scc->
ctrl, reg, (scc->
wreg[reg] &= ~val));
306 while (!skb_queue_empty(&scc->
tx_queue))
309 spin_unlock_irqrestore(&scc->
lock, flags);
329 skb = dev_alloc_skb(2);
335 scc_net_rx(scc, skb);
340 static inline void flush_rx_FIFO(
struct scc_channel *scc)
366 static inline void scc_txint(
struct scc_channel *scc)
379 netif_wake_queue(scc->
dev);
430 static inline void scc_exint(
struct scc_channel *scc)
432 unsigned char status,changes,chg_and_stat;
436 status = InReg(scc->
ctrl,
R0);
437 changes = status ^ scc->
status;
438 chg_and_stat = changes &
status;
449 if (status & SYNC_HUNT)
465 if((changes &
DCD) && !scc->
kiss.softdcd)
487 if (chg_and_stat &
CTS)
489 if (scc->
kiss.txdelay == 0)
490 scc_start_tx_timer(scc, t_txdelay, 0);
496 scc->
stat.tx_under++;
506 scc_start_tx_timer(scc, t_txdelay, 0);
515 static inline void scc_rxint(
struct scc_channel *scc)
532 skb = dev_alloc_skb(scc->
stat.bufsize);
546 if (skb->
len >= scc->
stat.bufsize)
563 static inline void scc_spint(
struct scc_channel *scc)
570 status = InReg(scc->
ctrl,
R1);
593 scc_net_rx(scc, skb);
595 scc->
stat.rxframes++;
611 spin_lock(&scc->
lock);
614 case TXINT: scc_txint(scc);
break;
615 case EXINT: scc_exint(scc);
break;
616 case RXINT: scc_rxint(scc);
break;
617 case SPINT: scc_spint(scc);
break;
619 spin_unlock(&scc->
lock);
627 #define SCC_IRQTIMEOUT 30000
631 int chip_irq = (
long) dev_id;
634 struct scc_ctrl *
ctrl;
641 Outb(Vector_Latch, 0);
644 if((vector=
Inb(Vector_Latch)) >= 16 * Nchips)
break;
645 if (vector & 0x01)
break;
647 scc=&SCC_Info[vector >> 3 ^ 0x01];
648 if (!scc->
dev)
break;
650 scc_isr_dispatch(scc, vector);
655 if (k == SCC_IRQTIMEOUT)
668 if (ctrl->irq != chip_irq)
677 vector=InReg(ctrl->chan_B,
R2);
678 if (vector & 0x01)
break;
680 scc = &SCC_Info[vector >> 3 ^ 0x01];
681 if (!scc->
dev)
break;
683 scc_isr_dispatch(scc, vector);
686 if (k == SCC_IRQTIMEOUT)
718 static inline void set_brg(
struct scc_channel *scc,
unsigned int tc)
721 wr(scc,
R12,tc & 255);
726 static inline void set_speed(
struct scc_channel *scc)
731 if (scc->
modem.speed > 0)
732 set_brg(scc, (
unsigned) (scc->
clock / (scc->
modem.speed * 64)) - 2);
734 spin_unlock_irqrestore(&scc->
lock, flags);
740 static inline void init_brg(
struct scc_channel *scc)
836 switch(scc->
modem.clocksrc)
863 if(scc->
kiss.softdcd || (InReg(scc->
ctrl,
R0) & DCD))
900 unsigned int time_const;
905 if (scc->
modem.speed < 300)
906 scc->
modem.speed = 1200;
908 time_const = (unsigned) (scc->
clock / (scc->
modem.speed * (tx? 2:64))) - 2;
922 #ifdef CONFIG_SCC_TRXECHO
926 set_brg(scc, time_const);
932 if (scc->
kiss.tx_inhibit)
942 set_brg(scc, time_const);
947 #ifndef CONFIG_SCC_TRXECHO
948 if (scc->
kiss.softdcd)
958 #ifdef CONFIG_SCC_TRXECHO
966 if (scc->
kiss.tx_inhibit)
977 #ifndef CONFIG_SCC_TRXECHO
995 static void __scc_start_tx_timer(
struct scc_channel *scc,
void (*handler)(
unsigned long),
unsigned long when)
1005 scc->
tx_t.data = (
unsigned long) scc;
1012 static void scc_start_tx_timer(
struct scc_channel *scc,
void (*handler)(
unsigned long),
unsigned long when)
1014 unsigned long flags;
1017 __scc_start_tx_timer(scc, handler, when);
1018 spin_unlock_irqrestore(&scc->
lock, flags);
1021 static void scc_start_defer(
struct scc_channel *scc)
1023 unsigned long flags;
1031 scc->
tx_wdog.function = t_busy;
1035 spin_unlock_irqrestore(&scc->
lock, flags);
1038 static void scc_start_maxkeyup(
struct scc_channel *scc)
1040 unsigned long flags;
1048 scc->
tx_wdog.function = t_maxkeyup;
1052 spin_unlock_irqrestore(&scc->
lock, flags);
1066 switch (scc->
kiss.fulldup)
1071 scc_start_tx_timer(scc, t_idle,
1072 scc->
kiss.idletime*100);
1079 scc_start_tx_timer(scc, t_tail, scc->
kiss.tailtime);
1082 netif_wake_queue(scc->
dev);
1086 static unsigned char Rand = 17;
1088 static inline int is_grouped(
struct scc_channel *scc)
1092 unsigned char grp1, grp2;
1094 grp1 = scc->
kiss.group;
1096 for (k = 0; k < (Nchips * 2); k++)
1098 scc2 = &SCC_Info[
k];
1099 grp2 = scc2->
kiss.group;
1101 if (scc2 == scc || !(scc2->
dev && grp2))
1104 if ((grp1 & 0x3f) == (grp2 & 0x3f))
1124 static void t_dwait(
unsigned long channel)
1130 if (skb_queue_empty(&scc->
tx_queue)) {
1132 netif_wake_queue(scc->
dev);
1141 Rand = Rand * 17 + 31;
1143 if (scc->
dcd || (scc->
kiss.persist) < Rand || (scc->
kiss.group && is_grouped(scc)) )
1145 scc_start_defer(scc);
1146 scc_start_tx_timer(scc, t_dwait, scc->
kiss.slottime);
1153 scc_key_trx(scc,
TX_ON);
1154 scc_start_tx_timer(scc, t_txdelay, scc->
kiss.txdelay);
1156 scc_start_tx_timer(scc, t_txdelay, 0);
1166 static void t_txdelay(
unsigned long channel)
1170 scc_start_maxkeyup(scc);
1187 static void t_tail(
unsigned long channel)
1190 unsigned long flags;
1194 scc_key_trx(scc,
TX_OFF);
1195 spin_unlock_irqrestore(&scc->
lock, flags);
1200 scc_start_tx_timer(scc, t_dwait, scc->
kiss.mintime*100);
1205 netif_wake_queue(scc->
dev);
1214 static void t_busy(
unsigned long channel)
1219 netif_stop_queue(scc->
dev);
1221 scc_discard_buffers(scc);
1225 netif_wake_queue(scc->
dev);
1233 static void t_maxkeyup(
unsigned long channel)
1236 unsigned long flags;
1244 netif_stop_queue(scc->
dev);
1245 scc_discard_buffers(scc);
1253 spin_unlock_irqrestore(&scc->
lock, flags);
1257 scc_start_tx_timer(scc, t_tail, scc->
kiss.tailtime);
1267 static void t_idle(
unsigned long channel)
1273 scc_key_trx(scc,
TX_OFF);
1274 if(scc->
kiss.mintime)
1275 scc_start_tx_timer(scc, t_dwait, scc->
kiss.mintime*100);
1279 static void scc_init_timer(
struct scc_channel *scc)
1281 unsigned long flags;
1285 spin_unlock_irqrestore(&scc->
lock, flags);
1298 #define CAST(x) (unsigned long)(x)
1300 static unsigned int scc_set_param(
struct scc_channel *scc,
unsigned int cmd,
unsigned int arg)
1333 scc->
modem.speed=arg*100;
1337 if (scc->
stat.tx_state == 0)
1345 scc_key_trx(scc,
TX_ON);
1346 scc_start_tx_timer(scc, t_txdelay, scc->
kiss.txdelay);
1352 scc_start_tx_timer(scc, t_tail, scc->
kiss.tailtime);
1369 static unsigned long scc_get_param(
struct scc_channel *scc,
unsigned int cmd)
1400 static void scc_stop_calibrate(
unsigned long channel)
1403 unsigned long flags;
1407 scc_key_trx(scc,
TX_OFF);
1413 netif_wake_queue(scc->
dev);
1414 spin_unlock_irqrestore(&scc->
lock, flags);
1421 unsigned long flags;
1424 netif_stop_queue(scc->
dev);
1425 scc_discard_buffers(scc);
1430 scc->
tx_wdog.function = scc_stop_calibrate;
1436 wr(scc,
R7, pattern);
1446 scc_key_trx(scc,
TX_ON);
1447 spin_unlock_irqrestore(&scc->
lock, flags);
1458 static void z8530_init(
void)
1462 unsigned long flags;
1479 for (chip = 0; chip < Nchips; chip++)
1481 scc=&SCC_Info[2*
chip];
1482 if (!scc->
ctrl)
continue;
1500 wr(scc,
R2, chip*16);
1502 spin_unlock_irqrestore(&scc->
lock, flags);
1506 Driver_Initialized = 1;
1547 .ndo_open = scc_net_open,
1548 .ndo_stop = scc_net_close,
1549 .ndo_start_xmit = scc_net_tx,
1550 .ndo_set_mac_address = scc_net_set_mac_address,
1551 .ndo_get_stats = scc_net_get_stats,
1552 .ndo_do_ioctl = scc_net_ioctl,
1557 static void scc_net_setup(
struct net_device *dev)
1578 static int scc_net_open(
struct net_device *dev)
1586 skb_queue_head_init(&scc->
tx_queue);
1590 netif_start_queue(dev);
1596 static int scc_net_close(
struct net_device *dev)
1599 unsigned long flags;
1601 netif_stop_queue(dev);
1607 spin_unlock_irqrestore(&scc->
lock, flags);
1612 scc_discard_buffers(scc);
1621 if (skb->
len == 0) {
1639 unsigned long flags;
1650 scc->
stat.txframes++;
1652 kisscmd = *skb->
data & 0x1f;
1656 scc_set_param(scc, kisscmd, *skb->
data);
1663 if (skb_queue_len(&scc->
tx_queue) > scc->
dev->tx_queue_len) {
1666 dev_kfree_skb(skb_del);
1681 __scc_start_tx_timer(scc, t_dwait, scc->
kiss.waittime);
1683 __scc_start_tx_timer(scc, t_dwait, 0);
1685 spin_unlock_irqrestore(&scc->
lock, flags);
1702 static int scc_net_ioctl(
struct net_device *dev,
struct ifreq *ifr,
int cmd)
1711 void __user *
arg = ifr->ifr_data;
1714 if (!Driver_Initialized)
1729 if (hwcfg.irq == 2) hwcfg.irq = 9;
1731 if (hwcfg.irq < 0 || hwcfg.irq >= nr_irqs)
1734 if (!Ivec[hwcfg.irq].used && hwcfg.irq)
1738 (
void *)(
long) hwcfg.irq))
1741 Ivec[hwcfg.irq].used = 1;
1744 if (hwcfg.vector_latch && !Vector_Latch) {
1746 printk(
KERN_WARNING "z8530drv: warning, cannot reserve vector latch port 0x%lx\n, disabled.", hwcfg.vector_latch);
1748 Vector_Latch = hwcfg.vector_latch;
1751 if (hwcfg.clock == 0)
1754 #ifndef SCC_DONT_CHECK
1759 Outb(hwcfg.ctrl_a, 0);
1762 OutReg(hwcfg.ctrl_a,
R13,0x55);
1765 if (InReg(hwcfg.ctrl_a,
R13) != 0x55)
1776 SCC_Info[2*Nchips ].ctrl = hwcfg.ctrl_a;
1777 SCC_Info[2*Nchips ].data = hwcfg.data_a;
1778 SCC_Info[2*Nchips ].irq = hwcfg.irq;
1779 SCC_Info[2*Nchips+1].ctrl = hwcfg.ctrl_b;
1780 SCC_Info[2*Nchips+1].data = hwcfg.data_b;
1781 SCC_Info[2*Nchips+1].irq = hwcfg.irq;
1783 SCC_ctrl[Nchips].chan_A = hwcfg.ctrl_a;
1784 SCC_ctrl[Nchips].chan_B = hwcfg.ctrl_b;
1785 SCC_ctrl[Nchips].irq = hwcfg.irq;
1793 SCC_Info[2*Nchips+
chan].special = hwcfg.special;
1794 SCC_Info[2*Nchips+
chan].clock = hwcfg.clock;
1795 SCC_Info[2*Nchips+
chan].brand = hwcfg.brand;
1796 SCC_Info[2*Nchips+
chan].option = hwcfg.option;
1797 SCC_Info[2*Nchips+
chan].enhanced = hwcfg.escc;
1799 #ifdef SCC_DONT_CHECK
1806 printk(
KERN_INFO "%s: data port = 0x%3.3lx control port = 0x%3.3lx -- %s\n",
1808 chan? hwcfg.data_b : hwcfg.data_a,
1809 chan? hwcfg.ctrl_b : hwcfg.ctrl_a,
1810 found?
"found" :
"missing");
1817 if (Nchips+
chan != 0 &&
1819 &SCC_Info[2*Nchips+
chan]))
1824 if (found) Nchips++;
1858 if (scc->
modem.speed < 4800)
1860 scc->
kiss.txdelay = 36;
1861 scc->
kiss.persist = 42;
1862 scc->
kiss.slottime = 16;
1863 scc->
kiss.tailtime = 4;
1864 scc->
kiss.fulldup = 0;
1865 scc->
kiss.waittime = 50;
1866 scc->
kiss.maxkeyup = 10;
1867 scc->
kiss.mintime = 3;
1868 scc->
kiss.idletime = 30;
1869 scc->
kiss.maxdefer = 120;
1870 scc->
kiss.softdcd = 0;
1872 scc->
kiss.txdelay = 10;
1873 scc->
kiss.persist = 64;
1874 scc->
kiss.slottime = 8;
1875 scc->
kiss.tailtime = 1;
1876 scc->
kiss.fulldup = 0;
1877 scc->
kiss.waittime = 50;
1878 scc->
kiss.maxkeyup = 7;
1879 scc->
kiss.mintime = 3;
1880 scc->
kiss.idletime = 30;
1881 scc->
kiss.maxdefer = 120;
1882 scc->
kiss.softdcd = 0;
1886 skb_queue_head_init(&scc->
tx_queue);
1904 scc->
stat.bufsize = memcfg.bufsize;
1915 kiss_cmd.param = scc_get_param(scc, kiss_cmd.command);
1924 return scc_set_param(scc, kiss_cmd.command, kiss_cmd.param);
1931 scc_start_calibrate(scc, cal.time, cal.pattern);
1944 static int scc_net_set_mac_address(
struct net_device *dev,
void *
addr)
1969 #ifdef CONFIG_PROC_FS
1975 for (k = 0; k < Nchips*2; ++
k) {
1976 if (!SCC_Info[k].
init)
1979 return &SCC_Info[
k];
1984 static void *scc_net_seq_start(
struct seq_file *seq, loff_t *pos)
1990 static void *scc_net_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
1997 k < Nchips*2; ++
k) {
1998 if (SCC_Info[k].
init)
1999 return &SCC_Info[
k];
2004 static void scc_net_seq_stop(
struct seq_file *seq,
void *v)
2008 static int scc_net_seq_show(
struct seq_file *seq,
void *v)
2012 }
else if (!Driver_Initialized) {
2013 seq_puts(seq,
"not initialized\n");
2014 }
else if (!Nchips) {
2031 seq_printf(seq,
"%s\t%3.3lx %3.3lx %d %lu %2.2x %d %3.3lx %3.3lx %d\n",
2042 seq_printf(seq,
"\t%lu %lu %d / %lu %lu %d / %d %d\n",
2047 #define K(x) kiss->x
2048 seq_printf(seq,
"\t%d %d %d %d %d %d %d %d %d %d %d %d\n",
2058 for (reg = 0; reg < 16; reg++)
2063 for (reg = 3; reg < 8; reg++)
2066 for (reg = 9; reg < 16; reg++)
2078 .
start = scc_net_seq_start,
2079 .next = scc_net_seq_next,
2080 .stop = scc_net_seq_stop,
2081 .show = scc_net_seq_show,
2087 return seq_open(file, &scc_net_seq_ops);
2092 .open = scc_net_seq_open,
2105 static int __init scc_init_driver (
void)
2111 sprintf(devname,
"%s0", SCC_DriverName);
2114 if (scc_net_alloc(devname, SCC_Info)) {
2126 static void __exit scc_cleanup_driver(
void)
2133 if (Nchips == 0 && (dev = SCC_Info[0].dev))
2142 for (k = 0; k < Nchips; k++)
2143 if ( (ctrl = SCC_ctrl[k].chan_A) )
2151 for (k = 0; k <
nr_irqs ; k++)
2157 for (k = 0; k < Nchips*2; k++)