📄 scc.c
字号:
#define RCS_ID "$Id: scc.c,v 1.75 1998/11/04 15:15:01 jreuter Exp jreuter $"#define VERSION "3.0"/* * Please use z8530drv-utils-3.0 with this version. * ------------------ * * You can find a subset of the documentation in * linux/Documentation/networking/z8530drv.txt. *//* ******************************************************************** * SCC.C - Linux driver for Z8530 based HDLC cards for AX.25 * ******************************************************************** ******************************************************************** Copyright (c) 1993, 2000 Joerg Reuter DL1BKE portions (c) 1993 Guido ten Dolle PE1NNZ ******************************************************************** The driver and the programs in the archive are UNDER CONSTRUCTION. The code is likely to fail, and so your kernel could --- even a whole network. This driver is intended for Amateur Radio use. If you are running it for commercial purposes, please drop me a note. I am nosy... ...BUT: ! You m u s t recognize the appropriate legislations of your country ! ! before you connect a radio to the SCC board and start to transmit or ! ! receive. The GPL allows you to use the d r i v e r, NOT the RADIO! ! For non-Amateur-Radio use please note that you might need a special allowance/licence from the designer of the SCC Board and/or the MODEM. This program is free software; you can redistribute it and/or modify it under the terms of the (modified) GNU General Public License delivered with the Linux kernel source. 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 find a copy of the GNU General Public License in /usr/src/linux/COPYING; ******************************************************************** Incomplete history of z8530drv: ------------------------------- 1994-09-13 started to write the driver, rescued most of my own code (and Hans Alblas' memory buffer pool concept) from an earlier project "sccdrv" which was initiated by Guido ten Dolle. Not much of the old driver survived, though. The first version I put my hands on was sccdrv1.3 from August 1993. The memory buffer pool concept appeared in an unauthorized sccdrv version (1.5) from August 1994. 1995-01-31 changed copyright notice to GPL without limitations. . . <SNIP> . 1996-10-05 New semester, new driver... * KISS TNC emulator removed (TTY driver) * Source moved to drivers/net/ * Includes Z8530 defines from drivers/net/z8530.h * Uses sk_buffer memory management * Reduced overhead of /proc/net/z8530drv output * Streamlined quite a lot things * Invents brand new bugs... ;-) The move to version number 3.0 reflects theses changes. You can use 'kissbridge' if you need a KISS TNC emulator. 1996-12-13 Fixed for Linux networking changes. (G4KLX) 1997-01-08 Fixed the remaining problems. 1997-04-02 Hopefully fixed the problems with the new *_timer() routines, added calibration code. 1997-10-12 Made SCC_DELAY a CONFIG option, added CONFIG_SCC_TRXECHO 1998-01-29 Small fix to avoid lock-up on initialization 1998-09-29 Fixed the "grouping" bugs, tx_inhibit works again, using dev->tx_queue_len now instead of MAXQUEUE now. 1998-10-21 Postponed the spinlock changes, would need a lot of testing I currently don't have the time to. Softdcd doesn't work. 1998-11-04 Softdcd does not work correctly in DPLL mode, in fact it never did. The DPLL locks on noise, the SYNC unit sees flags that aren't... Restarting the DPLL does not help either, it resynchronizes too slow and the first received frame gets lost. 2000-02-13 Fixed for new network driver interface changes, still does TX timeouts itself since it uses its own queue scheme. Thanks to all who contributed to this driver with ideas and bug reports! 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 "v3.0.dl1bke" with your own. Just to avoid confusion... If you want to add your modification to the linux distribution please (!) contact me first. New versions of the driver will be announced on the linux-hams mailing list on vger.kernel.org. To subscribe send an e-mail to majordomo@vger.kernel.org with the following line in the body of the mail: subscribe linux-hams The content of the "Subject" field will be ignored. vy 73, Joerg Reuter ampr-net: dl1bke@db0pra.ampr.org AX-25 : DL1BKE @ DB0ABH.#BAY.DEU.EU Internet: jreuter@yaina.de www : http://yaina.de/jreuter*//* ----------------------------------------------------------------------- */#undef SCC_LDELAY /* slow it even a bit more down */#undef SCC_DONT_CHECK /* don't look if the SCCs you specified are available */#define SCC_MAXCHIPS 4 /* number of max. supported chips */#define SCC_BUFSIZE 384 /* must not exceed 4096 */#undef SCC_DEBUG#define SCC_DEFAULT_CLOCK 4915200 /* default pclock if nothing is specified *//* ----------------------------------------------------------------------- */#include <linux/config.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/signal.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/interrupt.h>#include <linux/ioport.h>#include <linux/string.h>#include <linux/in.h>#include <linux/fcntl.h>#include <linux/ptrace.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/if_ether.h>#include <linux/if_arp.h>#include <linux/socket.h>#include <linux/init.h>#include <linux/scc.h>#include "z8530.h"#include <net/ax25.h>#include <asm/irq.h>#include <asm/system.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/bitops.h>#include <linux/ctype.h>#include <linux/kernel.h>#include <linux/proc_fs.h>static char banner[] __initdata = KERN_INFO "AX.25: Z8530 SCC driver version "VERSION".dl1bke\n";static void t_dwait(unsigned long);static void t_txdelay(unsigned long);static void t_tail(unsigned long);static void t_busy(unsigned long);static void t_maxkeyup(unsigned long);static void t_idle(unsigned long);static void scc_tx_done(struct scc_channel *);static void scc_start_tx_timer(struct scc_channel *, void (*)(unsigned long), unsigned long);static void scc_start_maxkeyup(struct scc_channel *);static void scc_start_defer(struct scc_channel *);static void z8530_init(void);static void init_channel(struct scc_channel *scc);static void scc_key_trx (struct scc_channel *scc, char tx);static void scc_isr(int irq, void *dev_id, struct pt_regs *regs);static void scc_init_timer(struct scc_channel *scc);static int scc_net_setup(struct scc_channel *scc, unsigned char *name, int addev);static int scc_net_init(struct net_device *dev);static int scc_net_open(struct net_device *dev);static int scc_net_close(struct net_device *dev);static void scc_net_rx(struct scc_channel *scc, struct sk_buff *skb);static int scc_net_tx(struct sk_buff *skb, struct net_device *dev);static int scc_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);static int scc_net_set_mac_address(struct net_device *dev, void *addr);static struct net_device_stats * scc_net_get_stats(struct net_device *dev);static unsigned char SCC_DriverName[] = "scc";static struct irqflags { unsigned char used : 1; } Ivec[NR_IRQS]; static struct scc_channel SCC_Info[2 * SCC_MAXCHIPS]; /* information per channel */static struct scc_ctrl { io_port chan_A; io_port chan_B; int irq;} SCC_ctrl[SCC_MAXCHIPS+1];static unsigned char Driver_Initialized;static int Nchips;static io_port Vector_Latch;/* ******************************************************************** *//* * Port Access Functions * *//* ******************************************************************** *//* These provide interrupt save 2-step access to the Z8530 registers */static inline unsigned char InReg(io_port port, unsigned char reg){ unsigned long flags; unsigned char r; save_flags(flags); cli();#ifdef SCC_LDELAY Outb(port, reg); udelay(SCC_LDELAY); r=Inb(port); udelay(SCC_LDELAY);#else Outb(port, reg); r=Inb(port);#endif restore_flags(flags); return r;}static inline void OutReg(io_port port, unsigned char reg, unsigned char val){ unsigned long flags; save_flags(flags); cli();#ifdef SCC_LDELAY Outb(port, reg); udelay(SCC_LDELAY); Outb(port, val); udelay(SCC_LDELAY);#else Outb(port, reg); Outb(port, val);#endif restore_flags(flags);}static inline void wr(struct scc_channel *scc, unsigned char reg, unsigned char val){ OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));}static inline void or(struct scc_channel *scc, unsigned char reg, unsigned char val){ OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));}static inline void cl(struct scc_channel *scc, unsigned char reg, unsigned char val){ OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));}/* ******************************************************************** *//* * Some useful macros * *//* ******************************************************************** */static inline void scc_discard_buffers(struct scc_channel *scc){ unsigned long flags; save_flags(flags); cli(); if (scc->tx_buff != NULL) { dev_kfree_skb(scc->tx_buff); scc->tx_buff = NULL; } while (skb_queue_len(&scc->tx_queue)) dev_kfree_skb(skb_dequeue(&scc->tx_queue)); restore_flags(flags);}/* ******************************************************************** *//* * Interrupt Service Routines * *//* ******************************************************************** *//* ----> subroutines for the interrupt handlers <---- */static inline void scc_notify(struct scc_channel *scc, int event){ struct sk_buff *skb; char *bp; if (scc->kiss.fulldup != KISS_DUPLEX_OPTIMA) return; skb = dev_alloc_skb(2); if (skb != NULL) { bp = skb_put(skb, 2); *bp++ = PARAM_HWEVENT; *bp++ = event; scc_net_rx(scc, skb); } else scc->stat.nospace++;}static inline void flush_rx_FIFO(struct scc_channel *scc){ int k; for (k=0; k<3; k++) Inb(scc->data); if(scc->rx_buff != NULL) /* did we receive something? */ { scc->stat.rxerrs++; /* then count it as an error */ dev_kfree_skb_irq(scc->rx_buff); scc->rx_buff = NULL; }}static void start_hunt(struct scc_channel *scc){ 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 */}/* ----> four different interrupt handlers for Tx, Rx, changing of *//* DCD/CTS and Rx/Tx errors *//* Transmitter interrupt handler */static inline void scc_txint(struct scc_channel *scc){ struct sk_buff *skb; scc->stat.txints++; skb = scc->tx_buff; /* send first octet */ if (skb == NULL) { skb = skb_dequeue(&scc->tx_queue); scc->tx_buff = skb; netif_wake_queue(scc->dev); if (skb == NULL) { scc_tx_done(scc); Outb(scc->ctrl, RES_Tx_P); return; } if (skb->len == 0) /* Paranoia... */ { dev_kfree_skb_irq(skb); scc->tx_buff = NULL; scc_tx_done(scc); Outb(scc->ctrl, RES_Tx_P); return; } scc->stat.tx_state = TXS_ACTIVE; OutReg(scc->ctrl, R0, RES_Tx_CRC); /* reset CRC generator */ or(scc,R10,ABUNDER); /* re-install underrun protection */ Outb(scc->data,*skb->data); /* send byte */ skb_pull(skb, 1); if (!scc->enhanced) /* reset EOM latch */ Outb(scc->ctrl,RES_EOM_L); return; } /* End Of Frame... */ if (skb->len == 0) { Outb(scc->ctrl, RES_Tx_P); /* reset pending int */ cl(scc, R10, ABUNDER); /* send CRC */ dev_kfree_skb_irq(skb); scc->tx_buff = NULL; scc->stat.tx_state = TXS_NEWFRAME; /* next frame... */ return; } /* send octet */ Outb(scc->data,*skb->data); skb_pull(skb, 1);}/* External/Status interrupt handler */static inline void scc_exint(struct scc_channel *scc){ 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_rx_FIFO(scc); /* HUNT: software DCD; on = waiting for SYNC, off = receiving frame */ if ((changes & SYNC_HUNT) && scc->kiss.softdcd) { if (status & SYNC_HUNT) { scc->dcd = 0; flush_rx_FIFO(scc); if ((scc->modem.clocksrc != CLK_EXTERNAL)) OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */ } else { scc->dcd = 1; } scc_notify(scc, scc->dcd? HWEV_DCD_OFF:HWEV_DCD_ON); } /* DCD: on = start to receive packet, off = ABORT condition */ /* (a successfully received packet generates a special condition int) */ if((changes & DCD) && !scc->kiss.softdcd) /* DCD input changed state */ { if(status & DCD) /* DCD is now ON */ { start_hunt(scc); scc->dcd = 1; } else { /* DCD is now OFF */ cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */ flush_rx_FIFO(scc); scc->dcd = 0; } scc_notify(scc, scc->dcd? HWEV_DCD_ON:HWEV_DCD_OFF); }#ifdef notdef /* CTS: use external TxDelay (what's that good for?!) * Anyway: If we _could_ use it (BayCom USCC uses CTS for * own purposes) we _should_ use the "autoenable" feature * of the Z8530 and not this interrupt... */ if (chg_and_stat & CTS) /* CTS is now ON */ { if (scc->kiss.txdelay == 0) /* zero TXDELAY = wait for CTS */ scc_start_tx_timer(scc, t_txdelay, 0); }#endif if (scc->stat.tx_state == TXS_ACTIVE && (status & TxEOM)) { scc->stat.tx_under++; /* oops, an underrun! count 'em */ Outb(scc->ctrl, RES_EXT_INT); /* reset ext/status interrupts */ if (scc->tx_buff != NULL) { dev_kfree_skb_irq(scc->tx_buff); scc->tx_buff = NULL; } or(scc,R10,ABUNDER); scc_start_tx_timer(scc, t_txdelay, 0); /* restart transmission */ } scc->status = status; Outb(scc->ctrl,RES_EXT_INT);}/* Receiver interrupt handler */static inline void scc_rxint(struct scc_channel *scc){ struct sk_buff *skb; scc->stat.rxints++; if((scc->wreg[5] & RTS) && scc->kiss.fulldup == KISS_DUPLEX_HALF) { Inb(scc->data); /* discard char */ or(scc,R3,ENT_HM); /* enter hunt mode for next flag */ return; } skb = scc->rx_buff; if (skb == NULL) { skb = dev_alloc_skb(scc->stat.bufsize); if (skb == NULL) { scc->dev_stat.rx_dropped++; scc->stat.nospace++; Inb(scc->data); or(scc, R3, ENT_HM); return; } scc->rx_buff = skb; *(skb_put(skb, 1)) = 0; /* KISS data */ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -