1135332Sglebius#define RCS_ID "$Id: scc.c,v 1.75 1998/11/04 15:15:01 jreuter Exp jreuter $"
2219182Sglebius
3143923Sglebius#define VERSION "3.0"
4135332Sglebius
5135332Sglebius/*
6135332Sglebius * Please use z8530drv-utils-3.0 with this version.
7135332Sglebius *            ------------------
8135332Sglebius *
9135332Sglebius * You can find a subset of the documentation in
10135332Sglebius * Documentation/networking/device_drivers/hamradio/z8530drv.rst.
11135332Sglebius */
12135332Sglebius
13135332Sglebius/*
14135332Sglebius   ********************************************************************
15135332Sglebius   *   SCC.C - Linux driver for Z8530 based HDLC cards for AX.25      *
16135332Sglebius   ********************************************************************
17135332Sglebius
18135332Sglebius
19135332Sglebius   ********************************************************************
20135332Sglebius
21135332Sglebius	Copyright (c) 1993, 2000 Joerg Reuter DL1BKE
22135332Sglebius
23135332Sglebius	portions (c) 1993 Guido ten Dolle PE1NNZ
24135332Sglebius
25135332Sglebius   ********************************************************************
26135332Sglebius
27135332Sglebius   The driver and the programs in the archive are UNDER CONSTRUCTION.
28135332Sglebius   The code is likely to fail, and so your kernel could --- even
29135332Sglebius   a whole network.
30135332Sglebius
31260278Sdim   This driver is intended for Amateur Radio use. If you are running it
32260278Sdim   for commercial purposes, please drop me a note. I am nosy...
33135332Sglebius
34219182Sglebius   ...BUT:
35219182Sglebius
36219182Sglebius   ! You  m u s t  recognize the appropriate legislations of your country !
37135332Sglebius   ! before you connect a radio to the SCC board and start to transmit or !
38135332Sglebius   ! receive. The GPL allows you to use the  d r i v e r,  NOT the RADIO! !
39135332Sglebius
40167990Sglebius   For non-Amateur-Radio use please note that you might need a special
41135332Sglebius   allowance/licence from the designer of the SCC Board and/or the
42135332Sglebius   MODEM.
43140511Sglebius
44135332Sglebius   This program is free software; you can redistribute it and/or modify
45135332Sglebius   it under the terms of the (modified) GNU General Public License
46135332Sglebius   delivered with the Linux kernel source.
47135332Sglebius
48219182Sglebius   This program is distributed in the hope that it will be useful,
49135332Sglebius   but WITHOUT ANY WARRANTY; without even the implied warranty of
50135332Sglebius   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51163247Sglebius   GNU General Public License for more details.
52135332Sglebius
53135332Sglebius   You should find a copy of the GNU General Public License in
54135332Sglebius   /usr/src/linux/COPYING;
55135332Sglebius
56219182Sglebius   ********************************************************************
57143923Sglebius
58143923Sglebius
59219182Sglebius   Incomplete history of z8530drv:
60135332Sglebius   -------------------------------
61135332Sglebius
62135332Sglebius   1994-09-13	started to write the driver, rescued most of my own
63135332Sglebius		code (and Hans Alblas' memory buffer pool concept) from
64135332Sglebius		an earlier project "sccdrv" which was initiated by
65219182Sglebius		Guido ten Dolle. Not much of the old driver survived,
66135332Sglebius		though. The first version I put my hands on was sccdrv1.3
67135332Sglebius		from August 1993. The memory buffer pool concept
68135332Sglebius		appeared in an unauthorized sccdrv version (1.5) from
69135332Sglebius		August 1994.
70135332Sglebius
71135332Sglebius   1995-01-31	changed copyright notice to GPL without limitations.
72135332Sglebius
73135332Sglebius     .
74135332Sglebius     .	<SNIP>
75135332Sglebius     .
76135332Sglebius
77135332Sglebius   1996-10-05	New semester, new driver...
78135332Sglebius
79135332Sglebius   		  * KISS TNC emulator removed (TTY driver)
80135332Sglebius   		  * Source moved to drivers/net/
81135332Sglebius   		  * Includes Z8530 defines from drivers/net/z8530.h
82135332Sglebius   		  * Uses sk_buffer memory management
83135332Sglebius   		  * Reduced overhead of /proc/net/z8530drv output
84135332Sglebius   		  * Streamlined quite a lot things
85135332Sglebius   		  * Invents brand new bugs... ;-)
86135332Sglebius
87135332Sglebius   		  The move to version number 3.0 reflects theses changes.
88135332Sglebius   		  You can use 'kissbridge' if you need a KISS TNC emulator.
89135332Sglebius
90135332Sglebius   1996-12-13	Fixed for Linux networking changes. (G4KLX)
91135332Sglebius   1997-01-08	Fixed the remaining problems.
92135332Sglebius   1997-04-02	Hopefully fixed the problems with the new *_timer()
93135332Sglebius   		routines, added calibration code.
94135332Sglebius   1997-10-12	Made SCC_DELAY a CONFIG option, added CONFIG_SCC_TRXECHO
95135332Sglebius   1998-01-29	Small fix to avoid lock-up on initialization
96135332Sglebius   1998-09-29	Fixed the "grouping" bugs, tx_inhibit works again,
97135332Sglebius   		using dev->tx_queue_len now instead of MAXQUEUE now.
98135332Sglebius   1998-10-21	Postponed the spinlock changes, would need a lot of
99135332Sglebius   		testing I currently don't have the time to. Softdcd doesn't
100135332Sglebius   		work.
101135332Sglebius   1998-11-04	Softdcd does not work correctly in DPLL mode, in fact it
102135332Sglebius   		never did. The DPLL locks on noise, the SYNC unit sees
103135332Sglebius   		flags that aren't... Restarting the DPLL does not help
104135332Sglebius   		either, it resynchronizes too slow and the first received
105135332Sglebius   		frame gets lost.
106135332Sglebius   2000-02-13	Fixed for new network driver interface changes, still
107135332Sglebius   		does TX timeouts itself since it uses its own queue
108135332Sglebius   		scheme.
109135332Sglebius
110135332Sglebius   Thanks to all who contributed to this driver with ideas and bug
111135332Sglebius   reports!
112135332Sglebius
113135332Sglebius   NB -- if you find errors, change something, please let me know
114135332Sglebius      	 first before you distribute it... And please don't touch
115135332Sglebius   	 the version number. Just replace my callsign in
116135332Sglebius   	 "v3.0.dl1bke" with your own. Just to avoid confusion...
117183693Smav
118183693Smav   If you want to add your modification to the linux distribution
119183693Smav   please (!) contact me first.
120183693Smav
121183693Smav   New versions of the driver will be announced on the linux-hams
122183693Smav   mailing list on vger.kernel.org. To subscribe send an e-mail
123183693Smav   to majordomo@vger.kernel.org with the following line in
124183693Smav   the body of the mail:
125219182Sglebius
126219182Sglebius	   subscribe linux-hams
127219182Sglebius
128219182Sglebius   The content of the "Subject" field will be ignored.
129219182Sglebius
130219182Sglebius   vy 73,
131219182Sglebius   Joerg Reuter	ampr-net: dl1bke@db0pra.ampr.org
132219182Sglebius		AX-25   : DL1BKE @ DB0ABH.#BAY.DEU.EU
133219182Sglebius		Internet: jreuter@yaina.de
134219182Sglebius		www     : http://yaina.de/jreuter
135219182Sglebius*/
136219182Sglebius
137219182Sglebius/* ----------------------------------------------------------------------- */
138219182Sglebius
139219182Sglebius#undef  SCC_LDELAY		/* slow it even a bit more down */
140219182Sglebius#undef  SCC_DONT_CHECK		/* don't look if the SCCs you specified are available */
141241446Smelifaro
142241446Smelifaro#define SCC_MAXCHIPS	4       /* number of max. supported chips */
143241446Smelifaro#define SCC_BUFSIZE	384     /* must not exceed 4096 */
144241446Smelifaro#undef	SCC_DEBUG
145241446Smelifaro
146241446Smelifaro#define SCC_DEFAULT_CLOCK	4915200
147241446Smelifaro				/* default pclock if nothing is specified */
148241446Smelifaro
149135332Sglebius/* ----------------------------------------------------------------------- */
150135332Sglebius
151135332Sglebius#include <linux/compat.h>
152135332Sglebius#include <linux/module.h>
153135332Sglebius#include <linux/errno.h>
154135332Sglebius#include <linux/signal.h>
155135332Sglebius#include <linux/timer.h>
156135332Sglebius#include <linux/interrupt.h>
157135332Sglebius#include <linux/ioport.h>
158135332Sglebius#include <linux/string.h>
159135332Sglebius#include <linux/in.h>
160135332Sglebius#include <linux/fcntl.h>
161135332Sglebius#include <linux/ptrace.h>
162138392Sglebius#include <linux/delay.h>
163135332Sglebius#include <linux/skbuff.h>
164135332Sglebius#include <linux/netdevice.h>
165135332Sglebius#include <linux/rtnetlink.h>
166135332Sglebius#include <linux/if_ether.h>
167135332Sglebius#include <linux/if_arp.h>
168135332Sglebius#include <linux/socket.h>
169135332Sglebius#include <linux/init.h>
170135332Sglebius#include <linux/scc.h>
171135332Sglebius#include <linux/ctype.h>
172135332Sglebius#include <linux/kernel.h>
173135332Sglebius#include <linux/proc_fs.h>
174135332Sglebius#include <linux/seq_file.h>
175135332Sglebius#include <linux/bitops.h>
176135332Sglebius
177135332Sglebius#include <net/net_namespace.h>
178135332Sglebius#include <net/ax25.h>
179135332Sglebius
180135332Sglebius#include <asm/irq.h>
181135332Sglebius#include <asm/io.h>
182135332Sglebius#include <linux/uaccess.h>
183135332Sglebius
184135332Sglebius#include "z8530.h"
185135332Sglebius
186183693Smavstatic const char banner[] __initconst = KERN_INFO \
187183693Smav	"AX.25: Z8530 SCC driver version "VERSION".dl1bke\n";
188183693Smav
189183693Smavstatic void t_dwait(struct timer_list *t);
190183693Smavstatic void t_txdelay(struct timer_list *t);
191183693Smavstatic void t_tail(struct timer_list *t);
192183693Smavstatic void t_busy(struct timer_list *);
193219182Sglebiusstatic void t_maxkeyup(struct timer_list *);
194219182Sglebiusstatic void t_idle(struct timer_list *t);
195219182Sglebiusstatic void scc_tx_done(struct scc_channel *);
196219182Sglebiusstatic void scc_start_tx_timer(struct scc_channel *,
197219182Sglebius			       void (*)(struct timer_list *), unsigned long);
198219182Sglebiusstatic void scc_start_maxkeyup(struct scc_channel *);
199219182Sglebiusstatic void scc_start_defer(struct scc_channel *);
200219182Sglebius
201219182Sglebiusstatic void z8530_init(void);
202219182Sglebius
203219182Sglebiusstatic void init_channel(struct scc_channel *scc);
204219182Sglebiusstatic void scc_key_trx (struct scc_channel *scc, char tx);
205219182Sglebiusstatic void scc_init_timer(struct scc_channel *scc);
206219182Sglebius
207241446Smelifarostatic int scc_net_alloc(const char *name, struct scc_channel *scc);
208241446Smelifarostatic void scc_net_setup(struct net_device *dev);
209241446Smelifarostatic int scc_net_open(struct net_device *dev);
210241446Smelifarostatic int scc_net_close(struct net_device *dev);
211241446Smelifarostatic void scc_net_rx(struct scc_channel *scc, struct sk_buff *skb);
212241446Smelifarostatic netdev_tx_t scc_net_tx(struct sk_buff *skb,
213241446Smelifaro			      struct net_device *dev);
214135332Sglebiusstatic int scc_net_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
215135332Sglebius				  void __user *data, int cmd);
216135332Sglebiusstatic int scc_net_set_mac_address(struct net_device *dev, void *addr);
217135332Sglebiusstatic struct net_device_stats * scc_net_get_stats(struct net_device *dev);
218135332Sglebius
219135332Sglebiusstatic unsigned char SCC_DriverName[] = "scc";
220135332Sglebius
221135332Sglebiusstatic struct irqflags { unsigned char used : 1; } Ivec[NR_IRQS];
222135332Sglebius
223135332Sglebiusstatic struct scc_channel SCC_Info[2 * SCC_MAXCHIPS];	/* information per channel */
224135332Sglebius
225135332Sglebiusstatic struct scc_ctrl {
226135332Sglebius	io_port chan_A;
227135332Sglebius	io_port chan_B;
228135332Sglebius	int irq;
229135332Sglebius} SCC_ctrl[SCC_MAXCHIPS+1];
230135332Sglebius
231135332Sglebiusstatic unsigned char Driver_Initialized;
232135332Sglebiusstatic int Nchips;
233135332Sglebiusstatic io_port Vector_Latch;
234135332Sglebius
235146092Sglebius
236135332Sglebius/* ******************************************************************** */
237135332Sglebius/* *			Port Access Functions			      * */
238220769Sglebius/* ******************************************************************** */
239135332Sglebius
240135332Sglebius/* These provide interrupt save 2-step access to the Z8530 registers */
241220768Sglebius
242135332Sglebiusstatic DEFINE_SPINLOCK(iolock);	/* Guards paired accesses */
243232921Smelifaro
244232921Smelifarostatic inline unsigned char InReg(io_port port, unsigned char reg)
245232921Smelifaro{
246232921Smelifaro	unsigned long flags;
247232921Smelifaro	unsigned char r;
248135332Sglebius
249135332Sglebius	spin_lock_irqsave(&iolock, flags);
250135332Sglebius#ifdef SCC_LDELAY
251135332Sglebius	Outb(port, reg);
252135332Sglebius	udelay(SCC_LDELAY);
253135332Sglebius	r=Inb(port);
254135332Sglebius	udelay(SCC_LDELAY);
255135332Sglebius#else
256183693Smav	Outb(port, reg);
257183693Smav	r=Inb(port);
258183693Smav#endif
259183693Smav	spin_unlock_irqrestore(&iolock, flags);
260135332Sglebius	return r;
261178250Skris}
262135332Sglebius
263135332Sglebiusstatic inline void OutReg(io_port port, unsigned char reg, unsigned char val)
264220769Sglebius{
265135332Sglebius	unsigned long flags;
266135332Sglebius
267135332Sglebius	spin_lock_irqsave(&iolock, flags);
268135332Sglebius#ifdef SCC_LDELAY
269135332Sglebius	Outb(port, reg); udelay(SCC_LDELAY);
270135332Sglebius	Outb(port, val); udelay(SCC_LDELAY);
271135332Sglebius#else
272135332Sglebius	Outb(port, reg);
273135332Sglebius	Outb(port, val);
274135332Sglebius#endif
275135332Sglebius	spin_unlock_irqrestore(&iolock, flags);
276135332Sglebius}
277135332Sglebius
278135332Sglebiusstatic inline void wr(struct scc_channel *scc, unsigned char reg,
279135332Sglebius	unsigned char val)
280135332Sglebius{
281135332Sglebius	OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));
282135332Sglebius}
283135332Sglebius
284135332Sglebiusstatic inline void or(struct scc_channel *scc, unsigned char reg, unsigned char val)
285135332Sglebius{
286135332Sglebius	OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));
287135332Sglebius}
288135332Sglebius
289135332Sglebiusstatic inline void cl(struct scc_channel *scc, unsigned char reg, unsigned char val)
290135332Sglebius{
291135332Sglebius	OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));
292135332Sglebius}
293135332Sglebius
294135332Sglebius/* ******************************************************************** */
295135332Sglebius/* *			Some useful macros			      * */
296135332Sglebius/* ******************************************************************** */
297135332Sglebius
298135332Sglebiusstatic inline void scc_discard_buffers(struct scc_channel *scc)
299135332Sglebius{
300135332Sglebius	unsigned long flags;
301135332Sglebius
302135332Sglebius	spin_lock_irqsave(&scc->lock, flags);
303135332Sglebius	if (scc->tx_buff != NULL)
304135332Sglebius	{
305135332Sglebius		dev_kfree_skb_irq(scc->tx_buff);
306135332Sglebius		scc->tx_buff = NULL;
307135332Sglebius	}
308135332Sglebius
309135332Sglebius	while (!skb_queue_empty(&scc->tx_queue))
310135332Sglebius		dev_kfree_skb_irq(skb_dequeue(&scc->tx_queue));
311143988Sglebius
312143988Sglebius	spin_unlock_irqrestore(&scc->lock, flags);
313143988Sglebius}
314143988Sglebius
315143988Sglebius
316143988Sglebius
317143988Sglebius/* ******************************************************************** */
318143988Sglebius/* *			Interrupt Service Routines		      * */
319143988Sglebius/* ******************************************************************** */
320143988Sglebius
321143988Sglebius
322143988Sglebius/* ----> subroutines for the interrupt handlers <---- */
323143988Sglebius
324143988Sglebiusstatic inline void scc_notify(struct scc_channel *scc, int event)
325143988Sglebius{
326143988Sglebius	struct sk_buff *skb;
327143988Sglebius	char *bp;
328143988Sglebius
329143988Sglebius        if (scc->kiss.fulldup != KISS_DUPLEX_OPTIMA)
330143988Sglebius		return;
331143988Sglebius
332143988Sglebius	skb = dev_alloc_skb(2);
333143988Sglebius	if (skb != NULL)
334143988Sglebius	{
335143988Sglebius		bp = skb_put(skb, 2);
336135332Sglebius		*bp++ = PARAM_HWEVENT;
337135332Sglebius		*bp++ = event;
338135332Sglebius		scc_net_rx(scc, skb);
339135332Sglebius	} else
340135332Sglebius		scc->stat.nospace++;
341219182Sglebius}
342219182Sglebius
343219182Sglebiusstatic inline void flush_rx_FIFO(struct scc_channel *scc)
344219182Sglebius{
345135332Sglebius	int k;
346135332Sglebius
347175934Smav	for (k=0; k<3; k++)
348175934Smav		Inb(scc->data);
349175934Smav
350219182Sglebius	if(scc->rx_buff != NULL)		/* did we receive something? */
351219182Sglebius	{
352219182Sglebius		scc->stat.rxerrs++;  /* then count it as an error */
353219182Sglebius		dev_kfree_skb_irq(scc->rx_buff);
354219182Sglebius		scc->rx_buff = NULL;
355219182Sglebius	}
356219182Sglebius}
357219182Sglebius
358219182Sglebiusstatic void start_hunt(struct scc_channel *scc)
359219182Sglebius{
360135332Sglebius	if ((scc->modem.clocksrc != CLK_EXTERNAL))
361135332Sglebius		OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
362135332Sglebius	or(scc,R3,ENT_HM|RxENABLE);  /* enable the receiver, hunt mode */
363135332Sglebius}
364135332Sglebius
365135332Sglebius/* ----> four different interrupt handlers for Tx, Rx, changing of	*/
366135332Sglebius/*       DCD/CTS and Rx/Tx errors					*/
367135332Sglebius
368135332Sglebius/* Transmitter interrupt handler */
369135332Sglebiusstatic inline void scc_txint(struct scc_channel *scc)
370135332Sglebius{
371135332Sglebius	struct sk_buff *skb;
372135332Sglebius
373135332Sglebius	scc->stat.txints++;
374135332Sglebius	skb = scc->tx_buff;
375135332Sglebius
376135332Sglebius	/* send first octet */
377135332Sglebius
378135332Sglebius	if (skb == NULL)
379135332Sglebius	{
380135332Sglebius		skb = skb_dequeue(&scc->tx_queue);
381135332Sglebius		scc->tx_buff = skb;
382248724Sglebius		netif_wake_queue(scc->dev);
383135332Sglebius
384135332Sglebius		if (skb == NULL)
385135332Sglebius		{
386135332Sglebius			scc_tx_done(scc);
387135332Sglebius			Outb(scc->ctrl, RES_Tx_P);
388135332Sglebius			return;
389135332Sglebius		}
390135332Sglebius
391248724Sglebius		if (skb->len == 0)		/* Paranoia... */
392135332Sglebius		{
393248724Sglebius			dev_kfree_skb_irq(skb);
394135332Sglebius			scc->tx_buff = NULL;
395138392Sglebius			scc_tx_done(scc);
396135332Sglebius			Outb(scc->ctrl, RES_Tx_P);
397138392Sglebius			return;
398135332Sglebius		}
399135332Sglebius
400138392Sglebius		scc->stat.tx_state = TXS_ACTIVE;
401154354Sglebius
402138392Sglebius		OutReg(scc->ctrl, R0, RES_Tx_CRC);
403135332Sglebius						/* reset CRC generator */
404135332Sglebius		or(scc,R10,ABUNDER);		/* re-install underrun protection */
405135332Sglebius		Outb(scc->data,*skb->data);	/* send byte */
406135332Sglebius		skb_pull(skb, 1);
407135332Sglebius
408135332Sglebius		if (!scc->enhanced)		/* reset EOM latch */
409135332Sglebius			Outb(scc->ctrl,RES_EOM_L);
410135332Sglebius		return;
411135332Sglebius	}
412135332Sglebius
413135332Sglebius	/* End Of Frame... */
414135332Sglebius
415248724Sglebius	if (skb->len == 0)
416135332Sglebius	{
417248724Sglebius		Outb(scc->ctrl, RES_Tx_P);	/* reset pending int */
418135332Sglebius		cl(scc, R10, ABUNDER);		/* send CRC */
419135332Sglebius		dev_kfree_skb_irq(skb);
420135332Sglebius		scc->tx_buff = NULL;
421248724Sglebius		scc->stat.tx_state = TXS_NEWFRAME; /* next frame... */
422248724Sglebius		return;
423135332Sglebius	}
424135332Sglebius
425135332Sglebius	/* send octet */
426154354Sglebius
427138392Sglebius	Outb(scc->data,*skb->data);
428135332Sglebius	skb_pull(skb, 1);
429135332Sglebius}
430135332Sglebius
431135332Sglebius
432135332Sglebius/* External/Status interrupt handler */
433135332Sglebiusstatic inline void scc_exint(struct scc_channel *scc)
434135332Sglebius{
435135332Sglebius	unsigned char status,changes,chg_and_stat;
436135332Sglebius
437135332Sglebius	scc->stat.exints++;
438135332Sglebius
439135332Sglebius	status = InReg(scc->ctrl,R0);
440135332Sglebius	changes = status ^ scc->status;
441135332Sglebius	chg_and_stat = changes & status;
442135332Sglebius
443135332Sglebius	/* ABORT: generated whenever DCD drops while receiving */
444135332Sglebius
445248724Sglebius	if (chg_and_stat & BRK_ABRT)		/* Received an ABORT */
446135332Sglebius		flush_rx_FIFO(scc);
447248724Sglebius
448135332Sglebius	/* HUNT: software DCD; on = waiting for SYNC, off = receiving frame */
449135332Sglebius
450135332Sglebius	if ((changes & SYNC_HUNT) && scc->kiss.softdcd)
451248724Sglebius	{
452248724Sglebius		if (status & SYNC_HUNT)
453135332Sglebius		{
454135332Sglebius			scc->dcd = 0;
455135332Sglebius			flush_rx_FIFO(scc);
456154354Sglebius			if ((scc->modem.clocksrc != CLK_EXTERNAL))
457138392Sglebius				OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
458135332Sglebius		} else {
459135332Sglebius			scc->dcd = 1;
460135332Sglebius		}
461135332Sglebius
462135332Sglebius		scc_notify(scc, scc->dcd? HWEV_DCD_OFF:HWEV_DCD_ON);
463135332Sglebius	}
464135332Sglebius
465135332Sglebius	/* DCD: on = start to receive packet, off = ABORT condition */
466135332Sglebius	/* (a successfully received packet generates a special condition int) */
467248724Sglebius
468135332Sglebius	if((changes & DCD) && !scc->kiss.softdcd) /* DCD input changed state */
469248724Sglebius	{
470135332Sglebius		if(status & DCD)                /* DCD is now ON */
471135332Sglebius		{
472248724Sglebius			start_hunt(scc);
473248724Sglebius			scc->dcd = 1;
474135332Sglebius		} else {                        /* DCD is now OFF */
475135332Sglebius			cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
476135332Sglebius			flush_rx_FIFO(scc);
477135332Sglebius			scc->dcd = 0;
478135332Sglebius		}
479135332Sglebius
480135332Sglebius		scc_notify(scc, scc->dcd? HWEV_DCD_ON:HWEV_DCD_OFF);
481135332Sglebius	}
482248724Sglebius
483183693Smav#ifdef notdef
484248724Sglebius	/* CTS: use external TxDelay (what's that good for?!)
485183693Smav	 * Anyway: If we _could_ use it (BayCom USCC uses CTS for
486183693Smav	 * own purposes) we _should_ use the "autoenable" feature
487248724Sglebius	 * of the Z8530 and not this interrupt...
488248724Sglebius	 */
489183693Smav
490183693Smav	if (chg_and_stat & CTS)			/* CTS is now ON */
491183693Smav	{
492183693Smav		if (scc->kiss.txdelay == 0)	/* zero TXDELAY = wait for CTS */
493183693Smav			scc_start_tx_timer(scc, t_txdelay, 0);
494183693Smav	}
495183693Smav#endif
496183693Smav
497183693Smav	if (scc->stat.tx_state == TXS_ACTIVE && (status & TxEOM))
498183693Smav	{
499248724Sglebius		scc->stat.tx_under++;	  /* oops, an underrun! count 'em */
500219182Sglebius		Outb(scc->ctrl, RES_EXT_INT);	/* reset ext/status interrupts */
501248724Sglebius
502219182Sglebius		if (scc->tx_buff != NULL)
503219182Sglebius		{
504248724Sglebius			dev_kfree_skb_irq(scc->tx_buff);
505248724Sglebius			scc->tx_buff = NULL;
506219182Sglebius		}
507219182Sglebius
508219182Sglebius		or(scc,R10,ABUNDER);
509219182Sglebius		scc_start_tx_timer(scc, t_txdelay, 0);	/* restart transmission */
510219182Sglebius	}
511219182Sglebius
512219182Sglebius	scc->status = status;
513219182Sglebius	Outb(scc->ctrl,RES_EXT_INT);
514248724Sglebius}
515219182Sglebius
516248724Sglebius
517219182Sglebius/* Receiver interrupt handler */
518219182Sglebiusstatic inline void scc_rxint(struct scc_channel *scc)
519248724Sglebius{
520248724Sglebius	struct sk_buff *skb;
521219182Sglebius
522219182Sglebius	scc->stat.rxints++;
523219182Sglebius
524219182Sglebius	if((scc->wreg[5] & RTS) && scc->kiss.fulldup == KISS_DUPLEX_HALF)
525219182Sglebius	{
526219182Sglebius		Inb(scc->data);		/* discard char */
527219182Sglebius		or(scc,R3,ENT_HM);	/* enter hunt mode for next flag */
528219182Sglebius		return;
529219182Sglebius	}
530248724Sglebius
531135332Sglebius	skb = scc->rx_buff;
532248724Sglebius
533248724Sglebius	if (skb == NULL)
534135332Sglebius	{
535135332Sglebius		skb = dev_alloc_skb(scc->stat.bufsize);
536135332Sglebius		if (skb == NULL)
537135332Sglebius		{
538135332Sglebius			scc->dev_stat.rx_dropped++;
539135332Sglebius			scc->stat.nospace++;
540135332Sglebius			Inb(scc->data);
541223787Sglebius			or(scc, R3, ENT_HM);
542223787Sglebius			return;
543223787Sglebius		}
544135332Sglebius
545223787Sglebius		scc->rx_buff = skb;
546223787Sglebius		skb_put_u8(skb, 0);	/* KISS data */
547223787Sglebius	}
548135332Sglebius
549241446Smelifaro	if (skb->len >= scc->stat.bufsize)
550248724Sglebius	{
551241446Smelifaro#ifdef notdef
552241446Smelifaro		printk(KERN_DEBUG "z8530drv: oops, scc_rxint() received huge frame...\n");
553248724Sglebius#endif
554248724Sglebius		dev_kfree_skb_irq(skb);
555241446Smelifaro		scc->rx_buff = NULL;
556241446Smelifaro		Inb(scc->data);
557241446Smelifaro		or(scc, R3, ENT_HM);
558241446Smelifaro		return;
559248724Sglebius	}
560135332Sglebius
561135332Sglebius	skb_put_u8(skb, Inb(scc->data));
562135332Sglebius}
563135332Sglebius
564135332Sglebius
565135332Sglebius/* Receive Special Condition interrupt handler */
566135332Sglebiusstatic inline void scc_spint(struct scc_channel *scc)
567135332Sglebius{
568135332Sglebius	unsigned char status;
569135332Sglebius	struct sk_buff *skb;
570135332Sglebius
571135332Sglebius	scc->stat.spints++;
572135332Sglebius
573135332Sglebius	status = InReg(scc->ctrl,R1);		/* read receiver status */
574135332Sglebius
575135332Sglebius	Inb(scc->data);				/* throw away Rx byte */
576135332Sglebius	skb = scc->rx_buff;
577135332Sglebius
578135332Sglebius	if(status & Rx_OVR)			/* receiver overrun */
579135332Sglebius	{
580135332Sglebius		scc->stat.rx_over++;             /* count them */
581135332Sglebius		or(scc,R3,ENT_HM);               /* enter hunt mode for next flag */
582135332Sglebius
583135332Sglebius		if (skb != NULL)
584135332Sglebius			dev_kfree_skb_irq(skb);
585135332Sglebius		scc->rx_buff = skb = NULL;
586135332Sglebius	}
587135332Sglebius
588183693Smav	if(status & END_FR && skb != NULL)	/* end of frame */
589219182Sglebius	{
590219182Sglebius		/* CRC okay, frame ends on 8 bit boundary and received something ? */
591219182Sglebius
592183693Smav		if (!(status & CRC_ERR) && (status & 0xe) == RES8 && skb->len > 0)
593219182Sglebius		{
594237227Smelifaro			/* ignore last received byte (first of the CRC bytes) */
595237226Smelifaro			skb_trim(skb, skb->len-1);
596183693Smav			scc_net_rx(scc, skb);
597219182Sglebius			scc->rx_buff = NULL;
598219182Sglebius			scc->stat.rxframes++;
599219182Sglebius		} else {				/* a bad frame */
600135332Sglebius			dev_kfree_skb_irq(skb);
601219182Sglebius			scc->rx_buff = NULL;
602135332Sglebius			scc->stat.rxerrs++;
603135332Sglebius		}
604135332Sglebius	}
605135332Sglebius
606140511Sglebius	Outb(scc->ctrl,ERR_RES);
607135332Sglebius}
608135332Sglebius
609135332Sglebius
610183693Smav/* ----> interrupt service routine for the Z8530 <---- */
611183693Smav
612183693Smavstatic void scc_isr_dispatch(struct scc_channel *scc, int vector)
613183693Smav{
614183693Smav	spin_lock(&scc->lock);
615183693Smav	switch (vector & VECTOR_MASK)
616183693Smav	{
617183693Smav		case TXINT: scc_txint(scc); break;
618219182Sglebius		case EXINT: scc_exint(scc); break;
619183693Smav		case RXINT: scc_rxint(scc); break;
620219182Sglebius		case SPINT: scc_spint(scc); break;
621248724Sglebius	}
622248724Sglebius	spin_unlock(&scc->lock);
623183693Smav}
624183693Smav
625183693Smav/* If the card has a latch for the interrupt vector (like the PA0HZP card)
626183693Smav   use it to get the number of the chip that generated the int.
627183693Smav   If not: poll all defined chips.
628183693Smav */
629183693Smav
630183693Smav#define SCC_IRQTIMEOUT 30000
631183693Smav
632183693Smavstatic irqreturn_t scc_isr(int irq, void *dev_id)
633183693Smav{
634183693Smav	int chip_irq = (long) dev_id;
635183693Smav	unsigned char vector;
636183693Smav	struct scc_channel *scc;
637183693Smav	struct scc_ctrl *ctrl;
638143988Sglebius	int k;
639143988Sglebius
640183693Smav	if (Vector_Latch)
641143988Sglebius	{
642143988Sglebius	    	for(k=0; k < SCC_IRQTIMEOUT; k++)
643183693Smav    		{
644248724Sglebius			Outb(Vector_Latch, 0);      /* Generate INTACK */
645248724Sglebius
646183693Smav			/* Read the vector */
647183693Smav			if((vector=Inb(Vector_Latch)) >= 16 * Nchips) break;
648183693Smav			if (vector & 0x01) break;
649183693Smav
650183693Smav		        scc=&SCC_Info[vector >> 3 ^ 0x01];
651183693Smav			if (!scc->dev) break;
652183693Smav
653143988Sglebius			scc_isr_dispatch(scc, vector);
654237227Smelifaro
655237227Smelifaro			OutReg(scc->ctrl,R0,RES_H_IUS);              /* Reset Highest IUS */
656237227Smelifaro		}
657143988Sglebius
658219182Sglebius		if (k == SCC_IRQTIMEOUT)
659143988Sglebius			printk(KERN_WARNING "z8530drv: endless loop in scc_isr()?\n");
660143924Sglebius
661135332Sglebius		return IRQ_HANDLED;
662135332Sglebius	}
663143924Sglebius
664143924Sglebius	/* Find the SCC generating the interrupt by polling all attached SCCs
665143924Sglebius	 * reading RR3A (the interrupt pending register)
666143924Sglebius	 */
667143924Sglebius
668143924Sglebius	ctrl = SCC_ctrl;
669143924Sglebius	while (ctrl->chan_A)
670143924Sglebius	{
671143924Sglebius		if (ctrl->irq != chip_irq)
672143924Sglebius		{
673143924Sglebius			ctrl++;
674143924Sglebius			continue;
675143924Sglebius		}
676219182Sglebius
677219182Sglebius		scc = NULL;
678143924Sglebius		for (k = 0; InReg(ctrl->chan_A,R3) && k < SCC_IRQTIMEOUT; k++)
679143988Sglebius		{
680143924Sglebius			vector=InReg(ctrl->chan_B,R2);	/* Read the vector */
681143924Sglebius			if (vector & 0x01) break;
682143924Sglebius
683143924Sglebius			scc = &SCC_Info[vector >> 3 ^ 0x01];
684143924Sglebius		        if (!scc->dev) break;
685143924Sglebius
686143924Sglebius			scc_isr_dispatch(scc, vector);
687143924Sglebius		}
688135332Sglebius
689135332Sglebius		if (k == SCC_IRQTIMEOUT)
690143924Sglebius		{
691135332Sglebius			printk(KERN_WARNING "z8530drv: endless loop in scc_isr()?!\n");
692135332Sglebius			break;
693135332Sglebius		}
694143924Sglebius
695135332Sglebius		/* This looks weird and it is. At least the BayCom USCC doesn't
696135332Sglebius		 * use the Interrupt Daisy Chain, thus we'll have to start
697143924Sglebius		 * all over again to be sure not to miss an interrupt from
698135332Sglebius		 * (any of) the other chip(s)...
699135332Sglebius		 * Honestly, the situation *is* braindamaged...
700135332Sglebius		 */
701143924Sglebius
702143924Sglebius		if (scc != NULL)
703143924Sglebius		{
704237226Smelifaro			OutReg(scc->ctrl,R0,RES_H_IUS);
705135332Sglebius			ctrl = SCC_ctrl;
706219182Sglebius		} else
707219182Sglebius			ctrl++;
708219182Sglebius	}
709219182Sglebius	return IRQ_HANDLED;
710248724Sglebius}
711248724Sglebius
712219182Sglebius
713219182Sglebius
714219182Sglebius/* ******************************************************************** */
715219182Sglebius/* *			Init Channel					*/
716237226Smelifaro/* ******************************************************************** */
717219182Sglebius
718219182Sglebius
719163247Sglebius/* ----> set SCC channel speed <---- */
720163247Sglebius
721163247Sglebiusstatic inline void set_brg(struct scc_channel *scc, unsigned int tc)
722163247Sglebius{
723163247Sglebius	cl(scc,R14,BRENABL);		/* disable baudrate generator */
724163247Sglebius	wr(scc,R12,tc & 255);		/* brg rate LOW */
725163247Sglebius	wr(scc,R13,tc >> 8);   		/* brg rate HIGH */
726219182Sglebius	or(scc,R14,BRENABL);		/* enable baudrate generator */
727237226Smelifaro}
728219182Sglebius
729219182Sglebiusstatic inline void set_speed(struct scc_channel *scc)
730163247Sglebius{
731163247Sglebius	unsigned long flags;
732163247Sglebius	spin_lock_irqsave(&scc->lock, flags);
733219182Sglebius
734219182Sglebius	if (scc->modem.speed > 0)	/* paranoia... */
735219182Sglebius		set_brg(scc, (unsigned) (scc->clock / (scc->modem.speed * 64)) - 2);
736219182Sglebius
737219182Sglebius	spin_unlock_irqrestore(&scc->lock, flags);
738219182Sglebius}
739163247Sglebius
740163247Sglebius
741135332Sglebius/* ----> initialize a SCC channel <---- */
742143988Sglebius
743135332Sglebiusstatic inline void init_brg(struct scc_channel *scc)
744143924Sglebius{
745143924Sglebius	wr(scc, R14, BRSRC);				/* BRG source = PCLK */
746143924Sglebius	OutReg(scc->ctrl, R14, SSBR|scc->wreg[R14]);	/* DPLL source = BRG */
747143924Sglebius	OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]);	/* DPLL NRZI mode */
748143924Sglebius}
749237226Smelifaro
750219182Sglebius/*
751248724Sglebius * Initialization according to the Z8530 manual (SGS-Thomson's version):
752248724Sglebius *
753248724Sglebius * 1. Modes and constants
754248724Sglebius *
755219182Sglebius * WR9	11000000	chip reset
756219182Sglebius * WR4	XXXXXXXX	Tx/Rx control, async or sync mode
757237162Smelifaro * WR1	0XX00X00	select W/REQ (optional)
758219182Sglebius * WR2	XXXXXXXX	program interrupt vector
759219182Sglebius * WR3	XXXXXXX0	select Rx control
760219182Sglebius * WR5	XXXX0XXX	select Tx control
761143924Sglebius * WR6	XXXXXXXX	sync character
762143924Sglebius * WR7	XXXXXXXX	sync character
763143988Sglebius * WR9	000X0XXX	select interrupt control
764143924Sglebius * WR10	XXXXXXXX	miscellaneous control (optional)
765143924Sglebius * WR11	XXXXXXXX	clock control
766135332Sglebius * WR12	XXXXXXXX	time constant lower byte (optional)
767219182Sglebius * WR13	XXXXXXXX	time constant upper byte (optional)
768219182Sglebius * WR14	XXXXXXX0	miscellaneous control
769219182Sglebius * WR14	XXXSSSSS	commands (optional)
770219182Sglebius *
771219182Sglebius * 2. Enables
772219182Sglebius *
773148091Sglebius * WR14	000SSSS1	baud rate enable
774219182Sglebius * WR3	SSSSSSS1	Rx enable
775148091Sglebius * WR5	SSSS1SSS	Tx enable
776148091Sglebius * WR0	10000000	reset Tx CRG (optional)
777219182Sglebius * WR1	XSS00S00	DMA enable (optional)
778143924Sglebius *
779219182Sglebius * 3. Interrupt status
780219182Sglebius *
781219182Sglebius * WR15	XXXXXXXX	enable external/status
782219182Sglebius * WR0	00010000	reset external status
783219182Sglebius * WR0	00010000	reset external status twice
784248724Sglebius * WR1	SSSXXSXX	enable Rx, Tx and Ext/status
785248724Sglebius * WR9	000SXSSS	enable master interrupt enable
786219182Sglebius *
787148091Sglebius * 1 = set to one, 0 = reset to zero
788148091Sglebius * X = user defined, S = same as previous init
789148091Sglebius *
790148091Sglebius *
791148091Sglebius * Note that the implementation differs in some points from above scheme.
792148091Sglebius *
793148091Sglebius */
794219182Sglebius
795219182Sglebiusstatic void init_channel(struct scc_channel *scc)
796219182Sglebius{
797148091Sglebius	del_timer(&scc->tx_t);
798219182Sglebius	del_timer(&scc->tx_wdog);
799248724Sglebius
800248724Sglebius	disable_irq(scc->irq);
801248724Sglebius
802248724Sglebius	wr(scc,R4,X1CLK|SDLC);		/* *1 clock, SDLC mode */
803237227Smelifaro	wr(scc,R1,0);			/* no W/REQ operation */
804219182Sglebius	wr(scc,R3,Rx8|RxCRC_ENAB);	/* RX 8 bits/char, CRC, disabled */
805219182Sglebius	wr(scc,R5,Tx8|DTR|TxCRC_ENAB);	/* TX 8 bits/char, disabled, DTR */
806219182Sglebius	wr(scc,R6,0);			/* SDLC address zero (not used) */
807219182Sglebius	wr(scc,R7,FLAG);		/* SDLC flag value */
808219182Sglebius	wr(scc,R9,VIS);			/* vector includes status */
809219182Sglebius	wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER); /* abort on underrun, preset CRC generator, NRZ(I) */
810219182Sglebius	wr(scc,R14, 0);
811219182Sglebius
812219182Sglebius
813143924Sglebius/* set clock sources:
814248724Sglebius
815248724Sglebius   CLK_DPLL: normal halfduplex operation
816248724Sglebius
817248724Sglebius		RxClk: use DPLL
818219182Sglebius		TxClk: use DPLL
819219182Sglebius		TRxC mode DPLL output
820163247Sglebius
821219182Sglebius   CLK_EXTERNAL: external clocking (G3RUH or DF9IC modem)
822219182Sglebius
823219182Sglebius  	        BayCom: 		others:
824248724Sglebius
825248724Sglebius  	        TxClk = pin RTxC	TxClk = pin TRxC
826248724Sglebius  	        RxClk = pin TRxC 	RxClk = pin RTxC
827248724Sglebius
828248724Sglebius
829219182Sglebius   CLK_DIVIDER:
830219182Sglebius   		RxClk = use DPLL
831248724Sglebius   		TxClk = pin RTxC
832248724Sglebius
833219182Sglebius   		BayCom:			others:
834219182Sglebius   		pin TRxC = DPLL		pin TRxC = BRG
835219182Sglebius   		(RxClk * 1)		(RxClk * 32)
836219182Sglebius*/
837219182Sglebius
838219182Sglebius
839219182Sglebius	switch(scc->modem.clocksrc)
840219182Sglebius	{
841219182Sglebius		case CLK_DPLL:
842219182Sglebius			wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
843219182Sglebius			init_brg(scc);
844219182Sglebius			break;
845219182Sglebius
846219182Sglebius		case CLK_DIVIDER:
847219182Sglebius			wr(scc, R11, ((scc->brand & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
848219182Sglebius			init_brg(scc);
849248724Sglebius			break;
850248724Sglebius
851219182Sglebius		case CLK_EXTERNAL:
852219182Sglebius			wr(scc, R11, (scc->brand & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
853219182Sglebius			OutReg(scc->ctrl, R14, DISDPLL);
854219182Sglebius			break;
855219182Sglebius
856219182Sglebius	}
857219182Sglebius
858248724Sglebius	set_speed(scc);			/* set baudrate */
859248724Sglebius
860219182Sglebius	if(scc->enhanced)
861219182Sglebius	{
862219182Sglebius		or(scc,R15,SHDLCE|FIFOE);	/* enable FIFO, SDLC/HDLC Enhancements (From now R7 is R7') */
863219182Sglebius		wr(scc,R7,AUTOEOM);
864219182Sglebius	}
865219182Sglebius
866248724Sglebius	if(scc->kiss.softdcd || (InReg(scc->ctrl,R0) & DCD))
867248724Sglebius						/* DCD is now ON */
868219182Sglebius	{
869219182Sglebius		start_hunt(scc);
870219182Sglebius	}
871237227Smelifaro
872219182Sglebius	/* enable ABORT, DCD & SYNC/HUNT interrupts */
873219182Sglebius
874219182Sglebius	wr(scc,R15, BRKIE|TxUIE|(scc->kiss.softdcd? SYNCIE:DCDIE));
875219182Sglebius
876219182Sglebius	Outb(scc->ctrl,RES_EXT_INT);	/* reset ext/status interrupts */
877219182Sglebius	Outb(scc->ctrl,RES_EXT_INT);	/* must be done twice */
878237226Smelifaro
879241369Skevlo	or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
880237226Smelifaro
881237226Smelifaro	scc->status = InReg(scc->ctrl,R0);	/* read initial status */
882219182Sglebius
883219182Sglebius	or(scc,R9,MIE);			/* master interrupt enable */
884219182Sglebius
885219182Sglebius	scc_init_timer(scc);
886219182Sglebius
887219182Sglebius	enable_irq(scc->irq);
888163247Sglebius}
889219182Sglebius
890135332Sglebius
891143924Sglebius
892135332Sglebius
893219182Sglebius/* ******************************************************************** */
894219182Sglebius/* *			SCC timer functions			      * */
895219182Sglebius/* ******************************************************************** */
896219182Sglebius
897219182Sglebius
898219182Sglebius/* ----> scc_key_trx sets the time constant for the baudrate
899237226Smelifaro         generator and keys the transmitter		     <---- */
900237226Smelifaro
901237226Smelifarostatic void scc_key_trx(struct scc_channel *scc, char tx)
902237226Smelifaro{
903237226Smelifaro	unsigned int time_const;
904219182Sglebius
905219182Sglebius	if (scc->brand & PRIMUS)
906219182Sglebius		Outb(scc->ctrl + 4, scc->option | (tx? 0x80 : 0));
907219182Sglebius
908183693Smav	if (scc->modem.speed < 300)
909183693Smav		scc->modem.speed = 1200;
910183693Smav
911183693Smav	time_const = (unsigned) (scc->clock / (scc->modem.speed * (tx? 2:64))) - 2;
912183693Smav
913183693Smav	disable_irq(scc->irq);
914183693Smav
915219182Sglebius	if (tx)
916219182Sglebius	{
917219182Sglebius		or(scc, R1, TxINT_ENAB);	/* t_maxkeyup may have reset these */
918232921Smelifaro		or(scc, R15, TxUIE);
919232921Smelifaro	}
920232921Smelifaro
921232921Smelifaro	if (scc->modem.clocksrc == CLK_DPLL)
922219182Sglebius	{				/* force simplex operation */
923219182Sglebius		if (tx)
924135332Sglebius		{
925219182Sglebius#ifdef CONFIG_SCC_TRXECHO
926219182Sglebius			cl(scc, R3, RxENABLE|ENT_HM);	/* switch off receiver */
927219182Sglebius			cl(scc, R15, DCDIE|SYNCIE);	/* No DCD changes, please */
928219182Sglebius#endif
929219182Sglebius			set_brg(scc, time_const);	/* reprogram baudrate generator */
930219182Sglebius
931183693Smav			/* DPLL -> Rx clk, BRG -> Tx CLK, TRxC mode output, TRxC = BRG */
932219182Sglebius			wr(scc, R11, RCDPLL|TCBR|TRxCOI|TRxCBR);
933219182Sglebius
934219182Sglebius			/* By popular demand: tx_inhibit */
935219182Sglebius			if (scc->kiss.tx_inhibit)
936248724Sglebius			{
937248724Sglebius				or(scc,R5, TxENAB);
938219182Sglebius				scc->wreg[R5] |= RTS;
939219182Sglebius			} else {
940248724Sglebius				or(scc,R5,RTS|TxENAB);	/* set the RTS line and enable TX */
941248724Sglebius			}
942219182Sglebius		} else {
943219182Sglebius			cl(scc,R5,RTS|TxENAB);
944219182Sglebius
945219182Sglebius			set_brg(scc, time_const);	/* reprogram baudrate generator */
946219182Sglebius
947143988Sglebius			/* DPLL -> Rx clk, DPLL -> Tx CLK, TRxC mode output, TRxC = DPLL */
948183693Smav			wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
949219182Sglebius
950219182Sglebius#ifndef CONFIG_SCC_TRXECHO
951219182Sglebius			if (scc->kiss.softdcd)
952248724Sglebius#endif
953248724Sglebius			{
954219182Sglebius				or(scc,R15, scc->kiss.softdcd? SYNCIE:DCDIE);
955219182Sglebius				start_hunt(scc);
956248724Sglebius			}
957248724Sglebius		}
958219182Sglebius	} else {
959219182Sglebius		if (tx)
960219182Sglebius		{
961219182Sglebius#ifdef CONFIG_SCC_TRXECHO
962143988Sglebius			if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
963183693Smav			{
964143988Sglebius				cl(scc, R3, RxENABLE);
965143988Sglebius				cl(scc, R15, DCDIE|SYNCIE);
966135332Sglebius			}
967143912Sglebius#endif
968143912Sglebius
969143912Sglebius			if (scc->kiss.tx_inhibit)
970135332Sglebius			{
971135332Sglebius				or(scc,R5, TxENAB);
972135332Sglebius				scc->wreg[R5] |= RTS;
973135332Sglebius			} else {
974135332Sglebius				or(scc,R5,RTS|TxENAB);	/* enable tx */
975135332Sglebius			}
976135332Sglebius		} else {
977135332Sglebius			cl(scc,R5,RTS|TxENAB);		/* disable tx */
978135332Sglebius
979135332Sglebius			if ((scc->kiss.fulldup == KISS_DUPLEX_HALF) &&
980135332Sglebius#ifndef CONFIG_SCC_TRXECHO
981135332Sglebius			    scc->kiss.softdcd)
982135332Sglebius#else
983135332Sglebius			    1)
984135332Sglebius#endif
985135332Sglebius			{
986135332Sglebius				or(scc, R15, scc->kiss.softdcd? SYNCIE:DCDIE);
987135332Sglebius				start_hunt(scc);
988135332Sglebius			}
989135332Sglebius		}
990135332Sglebius	}
991135332Sglebius
992135332Sglebius	enable_irq(scc->irq);
993135332Sglebius}
994135332Sglebius
995135332Sglebius
996232921Smelifaro/* ----> SCC timer interrupt handler and friends. <---- */
997184205Sdes
998135332Sglebiusstatic void __scc_start_tx_timer(struct scc_channel *scc,
999135332Sglebius				 void (*handler)(struct timer_list *t),
1000135332Sglebius				 unsigned long when)
1001135332Sglebius{
1002135332Sglebius	del_timer(&scc->tx_t);
1003135332Sglebius
1004135332Sglebius	if (when == 0)
1005135332Sglebius	{
1006135332Sglebius		handler(&scc->tx_t);
1007135332Sglebius	} else
1008135332Sglebius	if (when != TIMER_OFF)
1009135332Sglebius	{
1010153791Sglebius		scc->tx_t.function = handler;
1011153791Sglebius		scc->tx_t.expires = jiffies + (when*HZ)/100;
1012153791Sglebius		add_timer(&scc->tx_t);
1013153791Sglebius	}
1014153791Sglebius}
1015153791Sglebius
1016135332Sglebiusstatic void scc_start_tx_timer(struct scc_channel *scc,
1017175934Smav			       void (*handler)(struct timer_list *t),
1018175934Smav			       unsigned long when)
1019219182Sglebius{
1020219182Sglebius	unsigned long flags;
1021135332Sglebius
1022175934Smav	spin_lock_irqsave(&scc->lock, flags);
1023135332Sglebius	__scc_start_tx_timer(scc, handler, when);
1024219182Sglebius	spin_unlock_irqrestore(&scc->lock, flags);
1025219182Sglebius}
1026219182Sglebius
1027219182Sglebiusstatic void scc_start_defer(struct scc_channel *scc)
1028219182Sglebius{
1029219182Sglebius	unsigned long flags;
1030135332Sglebius
1031135332Sglebius	spin_lock_irqsave(&scc->lock, flags);
1032135332Sglebius	del_timer(&scc->tx_wdog);
1033135332Sglebius
1034135332Sglebius	if (scc->kiss.maxdefer != 0 && scc->kiss.maxdefer != TIMER_OFF)
1035135332Sglebius	{
1036		scc->tx_wdog.function = t_busy;
1037		scc->tx_wdog.expires = jiffies + HZ*scc->kiss.maxdefer;
1038		add_timer(&scc->tx_wdog);
1039	}
1040	spin_unlock_irqrestore(&scc->lock, flags);
1041}
1042
1043static void scc_start_maxkeyup(struct scc_channel *scc)
1044{
1045	unsigned long flags;
1046
1047	spin_lock_irqsave(&scc->lock, flags);
1048	del_timer(&scc->tx_wdog);
1049
1050	if (scc->kiss.maxkeyup != 0 && scc->kiss.maxkeyup != TIMER_OFF)
1051	{
1052		scc->tx_wdog.function = t_maxkeyup;
1053		scc->tx_wdog.expires = jiffies + HZ*scc->kiss.maxkeyup;
1054		add_timer(&scc->tx_wdog);
1055	}
1056	spin_unlock_irqrestore(&scc->lock, flags);
1057}
1058
1059/*
1060 * This is called from scc_txint() when there are no more frames to send.
1061 * Not exactly a timer function, but it is a close friend of the family...
1062 */
1063
1064static void scc_tx_done(struct scc_channel *scc)
1065{
1066	/*
1067	 * trx remains keyed in fulldup mode 2 until t_idle expires.
1068	 */
1069
1070	switch (scc->kiss.fulldup)
1071	{
1072		case KISS_DUPLEX_LINK:
1073			scc->stat.tx_state = TXS_IDLE2;
1074			if (scc->kiss.idletime != TIMER_OFF)
1075				scc_start_tx_timer(scc, t_idle,
1076						   scc->kiss.idletime*100);
1077			break;
1078		case KISS_DUPLEX_OPTIMA:
1079			scc_notify(scc, HWEV_ALL_SENT);
1080			break;
1081		default:
1082			scc->stat.tx_state = TXS_BUSY;
1083			scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
1084	}
1085
1086	netif_wake_queue(scc->dev);
1087}
1088
1089
1090static unsigned char Rand = 17;
1091
1092static inline int is_grouped(struct scc_channel *scc)
1093{
1094	int k;
1095	struct scc_channel *scc2;
1096	unsigned char grp1, grp2;
1097
1098	grp1 = scc->kiss.group;
1099
1100	for (k = 0; k < (Nchips * 2); k++)
1101	{
1102		scc2 = &SCC_Info[k];
1103		grp2 = scc2->kiss.group;
1104
1105		if (scc2 == scc || !(scc2->dev && grp2))
1106			continue;
1107
1108		if ((grp1 & 0x3f) == (grp2 & 0x3f))
1109		{
1110			if ( (grp1 & TXGROUP) && (scc2->wreg[R5] & RTS) )
1111				return 1;
1112
1113			if ( (grp1 & RXGROUP) && scc2->dcd )
1114				return 1;
1115		}
1116	}
1117	return 0;
1118}
1119
1120/* DWAIT and SLOTTIME expired
1121 *
1122 * fulldup == 0:  DCD is active or Rand > P-persistence: start t_busy timer
1123 *                else key trx and start txdelay
1124 * fulldup == 1:  key trx and start txdelay
1125 * fulldup == 2:  mintime expired, reset status or key trx and start txdelay
1126 */
1127
1128static void t_dwait(struct timer_list *t)
1129{
1130	struct scc_channel *scc = from_timer(scc, t, tx_t);
1131
1132	if (scc->stat.tx_state == TXS_WAIT)	/* maxkeyup or idle timeout */
1133	{
1134		if (skb_queue_empty(&scc->tx_queue)) {	/* nothing to send */
1135			scc->stat.tx_state = TXS_IDLE;
1136			netif_wake_queue(scc->dev);	/* t_maxkeyup locked it. */
1137			return;
1138		}
1139
1140		scc->stat.tx_state = TXS_BUSY;
1141	}
1142
1143	if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
1144	{
1145		Rand = Rand * 17 + 31;
1146
1147		if (scc->dcd || (scc->kiss.persist) < Rand || (scc->kiss.group && is_grouped(scc)) )
1148		{
1149			scc_start_defer(scc);
1150			scc_start_tx_timer(scc, t_dwait, scc->kiss.slottime);
1151			return ;
1152		}
1153	}
1154
1155	if ( !(scc->wreg[R5] & RTS) )
1156	{
1157		scc_key_trx(scc, TX_ON);
1158		scc_start_tx_timer(scc, t_txdelay, scc->kiss.txdelay);
1159	} else {
1160		scc_start_tx_timer(scc, t_txdelay, 0);
1161	}
1162}
1163
1164
1165/* TXDELAY expired
1166 *
1167 * kick transmission by a fake scc_txint(scc), start 'maxkeyup' watchdog.
1168 */
1169
1170static void t_txdelay(struct timer_list *t)
1171{
1172	struct scc_channel *scc = from_timer(scc, t, tx_t);
1173
1174	scc_start_maxkeyup(scc);
1175
1176	if (scc->tx_buff == NULL)
1177	{
1178		disable_irq(scc->irq);
1179		scc_txint(scc);
1180		enable_irq(scc->irq);
1181	}
1182}
1183
1184
1185/* TAILTIME expired
1186 *
1187 * switch off transmitter. If we were stopped by Maxkeyup restart
1188 * transmission after 'mintime' seconds
1189 */
1190
1191static void t_tail(struct timer_list *t)
1192{
1193	struct scc_channel *scc = from_timer(scc, t, tx_t);
1194	unsigned long flags;
1195
1196	spin_lock_irqsave(&scc->lock, flags);
1197	del_timer(&scc->tx_wdog);
1198	scc_key_trx(scc, TX_OFF);
1199	spin_unlock_irqrestore(&scc->lock, flags);
1200
1201	if (scc->stat.tx_state == TXS_TIMEOUT)		/* we had a timeout? */
1202	{
1203		scc->stat.tx_state = TXS_WAIT;
1204		scc_start_tx_timer(scc, t_dwait, scc->kiss.mintime*100);
1205		return;
1206	}
1207
1208	scc->stat.tx_state = TXS_IDLE;
1209	netif_wake_queue(scc->dev);
1210}
1211
1212
1213/* BUSY timeout
1214 *
1215 * throw away send buffers if DCD remains active too long.
1216 */
1217
1218static void t_busy(struct timer_list *t)
1219{
1220	struct scc_channel *scc = from_timer(scc, t, tx_wdog);
1221
1222	del_timer(&scc->tx_t);
1223	netif_stop_queue(scc->dev);	/* don't pile on the wabbit! */
1224
1225	scc_discard_buffers(scc);
1226	scc->stat.txerrs++;
1227	scc->stat.tx_state = TXS_IDLE;
1228
1229	netif_wake_queue(scc->dev);
1230}
1231
1232/* MAXKEYUP timeout
1233 *
1234 * this is our watchdog.
1235 */
1236
1237static void t_maxkeyup(struct timer_list *t)
1238{
1239	struct scc_channel *scc = from_timer(scc, t, tx_wdog);
1240	unsigned long flags;
1241
1242	spin_lock_irqsave(&scc->lock, flags);
1243	/*
1244	 * let things settle down before we start to
1245	 * accept new data.
1246	 */
1247
1248	netif_stop_queue(scc->dev);
1249	scc_discard_buffers(scc);
1250
1251	del_timer(&scc->tx_t);
1252
1253	cl(scc, R1, TxINT_ENAB);	/* force an ABORT, but don't */
1254	cl(scc, R15, TxUIE);		/* count it. */
1255	OutReg(scc->ctrl, R0, RES_Tx_P);
1256
1257	spin_unlock_irqrestore(&scc->lock, flags);
1258
1259	scc->stat.txerrs++;
1260	scc->stat.tx_state = TXS_TIMEOUT;
1261	scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
1262}
1263
1264/* IDLE timeout
1265 *
1266 * in fulldup mode 2 it keys down the transmitter after 'idle' seconds
1267 * of inactivity. We will not restart transmission before 'mintime'
1268 * expires.
1269 */
1270
1271static void t_idle(struct timer_list *t)
1272{
1273	struct scc_channel *scc = from_timer(scc, t, tx_t);
1274
1275	del_timer(&scc->tx_wdog);
1276
1277	scc_key_trx(scc, TX_OFF);
1278	if(scc->kiss.mintime)
1279		scc_start_tx_timer(scc, t_dwait, scc->kiss.mintime*100);
1280	scc->stat.tx_state = TXS_WAIT;
1281}
1282
1283static void scc_init_timer(struct scc_channel *scc)
1284{
1285	unsigned long flags;
1286
1287	spin_lock_irqsave(&scc->lock, flags);
1288	scc->stat.tx_state = TXS_IDLE;
1289	spin_unlock_irqrestore(&scc->lock, flags);
1290}
1291
1292
1293/* ******************************************************************** */
1294/* *			Set/get L1 parameters			      * */
1295/* ******************************************************************** */
1296
1297
1298/*
1299 * this will set the "hardware" parameters through KISS commands or ioctl()
1300 */
1301
1302#define CAST(x) (unsigned long)(x)
1303
1304static unsigned int scc_set_param(struct scc_channel *scc, unsigned int cmd, unsigned int arg)
1305{
1306	switch (cmd)
1307	{
1308		case PARAM_TXDELAY:	scc->kiss.txdelay=arg;		break;
1309		case PARAM_PERSIST:	scc->kiss.persist=arg;		break;
1310		case PARAM_SLOTTIME:	scc->kiss.slottime=arg;		break;
1311		case PARAM_TXTAIL:	scc->kiss.tailtime=arg;		break;
1312		case PARAM_FULLDUP:	scc->kiss.fulldup=arg;		break;
1313		case PARAM_DTR:		break; /* does someone need this? */
1314		case PARAM_GROUP:	scc->kiss.group=arg;		break;
1315		case PARAM_IDLE:	scc->kiss.idletime=arg;		break;
1316		case PARAM_MIN:		scc->kiss.mintime=arg;		break;
1317		case PARAM_MAXKEY:	scc->kiss.maxkeyup=arg;		break;
1318		case PARAM_WAIT:	scc->kiss.waittime=arg;		break;
1319		case PARAM_MAXDEFER:	scc->kiss.maxdefer=arg;		break;
1320		case PARAM_TX:		scc->kiss.tx_inhibit=arg;	break;
1321
1322		case PARAM_SOFTDCD:
1323			scc->kiss.softdcd=arg;
1324			if (arg)
1325			{
1326				or(scc, R15, SYNCIE);
1327				cl(scc, R15, DCDIE);
1328				start_hunt(scc);
1329			} else {
1330				or(scc, R15, DCDIE);
1331				cl(scc, R15, SYNCIE);
1332			}
1333			break;
1334
1335		case PARAM_SPEED:
1336			if (arg < 256)
1337				scc->modem.speed=arg*100;
1338			else
1339				scc->modem.speed=arg;
1340
1341			if (scc->stat.tx_state == 0)	/* only switch baudrate on rx... ;-) */
1342				set_speed(scc);
1343			break;
1344
1345		case PARAM_RTS:
1346			if ( !(scc->wreg[R5] & RTS) )
1347			{
1348				if (arg != TX_OFF) {
1349					scc_key_trx(scc, TX_ON);
1350					scc_start_tx_timer(scc, t_txdelay, scc->kiss.txdelay);
1351				}
1352			} else {
1353				if (arg == TX_OFF)
1354				{
1355					scc->stat.tx_state = TXS_BUSY;
1356					scc_start_tx_timer(scc, t_tail, scc->kiss.tailtime);
1357				}
1358			}
1359			break;
1360
1361		case PARAM_HWEVENT:
1362			scc_notify(scc, scc->dcd? HWEV_DCD_ON:HWEV_DCD_OFF);
1363			break;
1364
1365		default:		return -EINVAL;
1366	}
1367
1368	return 0;
1369}
1370
1371
1372
1373static unsigned long scc_get_param(struct scc_channel *scc, unsigned int cmd)
1374{
1375	switch (cmd)
1376	{
1377		case PARAM_TXDELAY:	return CAST(scc->kiss.txdelay);
1378		case PARAM_PERSIST:	return CAST(scc->kiss.persist);
1379		case PARAM_SLOTTIME:	return CAST(scc->kiss.slottime);
1380		case PARAM_TXTAIL:	return CAST(scc->kiss.tailtime);
1381		case PARAM_FULLDUP:	return CAST(scc->kiss.fulldup);
1382		case PARAM_SOFTDCD:	return CAST(scc->kiss.softdcd);
1383		case PARAM_DTR:		return CAST((scc->wreg[R5] & DTR)? 1:0);
1384		case PARAM_RTS:		return CAST((scc->wreg[R5] & RTS)? 1:0);
1385		case PARAM_SPEED:	return CAST(scc->modem.speed);
1386		case PARAM_GROUP:	return CAST(scc->kiss.group);
1387		case PARAM_IDLE:	return CAST(scc->kiss.idletime);
1388		case PARAM_MIN:		return CAST(scc->kiss.mintime);
1389		case PARAM_MAXKEY:	return CAST(scc->kiss.maxkeyup);
1390		case PARAM_WAIT:	return CAST(scc->kiss.waittime);
1391		case PARAM_MAXDEFER:	return CAST(scc->kiss.maxdefer);
1392		case PARAM_TX:		return CAST(scc->kiss.tx_inhibit);
1393		default:		return NO_SUCH_PARAM;
1394	}
1395
1396}
1397
1398#undef CAST
1399
1400/* ******************************************************************* */
1401/* *			Send calibration pattern		     * */
1402/* ******************************************************************* */
1403
1404static void scc_stop_calibrate(struct timer_list *t)
1405{
1406	struct scc_channel *scc = from_timer(scc, t, tx_wdog);
1407	unsigned long flags;
1408
1409	spin_lock_irqsave(&scc->lock, flags);
1410	del_timer(&scc->tx_wdog);
1411	scc_key_trx(scc, TX_OFF);
1412	wr(scc, R6, 0);
1413	wr(scc, R7, FLAG);
1414	Outb(scc->ctrl,RES_EXT_INT);	/* reset ext/status interrupts */
1415	Outb(scc->ctrl,RES_EXT_INT);
1416
1417	netif_wake_queue(scc->dev);
1418	spin_unlock_irqrestore(&scc->lock, flags);
1419}
1420
1421
1422static void
1423scc_start_calibrate(struct scc_channel *scc, int duration, unsigned char pattern)
1424{
1425	unsigned long flags;
1426
1427	spin_lock_irqsave(&scc->lock, flags);
1428	netif_stop_queue(scc->dev);
1429	scc_discard_buffers(scc);
1430
1431	del_timer(&scc->tx_wdog);
1432
1433	scc->tx_wdog.function = scc_stop_calibrate;
1434	scc->tx_wdog.expires = jiffies + HZ*duration;
1435	add_timer(&scc->tx_wdog);
1436
1437	/* This doesn't seem to work. Why not? */
1438	wr(scc, R6, 0);
1439	wr(scc, R7, pattern);
1440
1441	/*
1442	 * Don't know if this works.
1443	 * Damn, where is my Z8530 programming manual...?
1444	 */
1445
1446	Outb(scc->ctrl,RES_EXT_INT);	/* reset ext/status interrupts */
1447	Outb(scc->ctrl,RES_EXT_INT);
1448
1449	scc_key_trx(scc, TX_ON);
1450	spin_unlock_irqrestore(&scc->lock, flags);
1451}
1452
1453/* ******************************************************************* */
1454/* *		Init channel structures, special HW, etc...	     * */
1455/* ******************************************************************* */
1456
1457/*
1458 * Reset the Z8530s and setup special hardware
1459 */
1460
1461static void z8530_init(void)
1462{
1463	struct scc_channel *scc;
1464	int chip, k;
1465	unsigned long flags;
1466	char *flag;
1467
1468
1469	printk(KERN_INFO "Init Z8530 driver: %u channels, IRQ", Nchips*2);
1470
1471	flag=" ";
1472	for (k = 0; k < nr_irqs; k++)
1473		if (Ivec[k].used)
1474		{
1475			printk("%s%d", flag, k);
1476			flag=",";
1477		}
1478	printk("\n");
1479
1480
1481	/* reset and pre-init all chips in the system */
1482	for (chip = 0; chip < Nchips; chip++)
1483	{
1484		scc=&SCC_Info[2*chip];
1485		if (!scc->ctrl) continue;
1486
1487		/* Special SCC cards */
1488
1489		if(scc->brand & EAGLE)			/* this is an EAGLE card */
1490			Outb(scc->special,0x08);	/* enable interrupt on the board */
1491
1492		if(scc->brand & (PC100 | PRIMUS))	/* this is a PC100/PRIMUS card */
1493			Outb(scc->special,scc->option);	/* set the MODEM mode (0x22) */
1494
1495
1496		/* Reset and pre-init Z8530 */
1497
1498		spin_lock_irqsave(&scc->lock, flags);
1499
1500		Outb(scc->ctrl, 0);
1501		OutReg(scc->ctrl,R9,FHWRES);		/* force hardware reset */
1502		udelay(100);				/* give it 'a bit' more time than required */
1503		wr(scc, R2, chip*16);			/* interrupt vector */
1504		wr(scc, R9, VIS);			/* vector includes status */
1505		spin_unlock_irqrestore(&scc->lock, flags);
1506        }
1507
1508
1509	Driver_Initialized = 1;
1510}
1511
1512/*
1513 * Allocate device structure, err, instance, and register driver
1514 */
1515
1516static int scc_net_alloc(const char *name, struct scc_channel *scc)
1517{
1518	int err;
1519	struct net_device *dev;
1520
1521	dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, scc_net_setup);
1522	if (!dev)
1523		return -ENOMEM;
1524
1525	dev->ml_priv = scc;
1526	scc->dev = dev;
1527	spin_lock_init(&scc->lock);
1528	timer_setup(&scc->tx_t, NULL, 0);
1529	timer_setup(&scc->tx_wdog, NULL, 0);
1530
1531	err = register_netdevice(dev);
1532	if (err) {
1533		printk(KERN_ERR "%s: can't register network device (%d)\n",
1534		       name, err);
1535		free_netdev(dev);
1536		scc->dev = NULL;
1537		return err;
1538	}
1539
1540	return 0;
1541}
1542
1543
1544
1545/* ******************************************************************** */
1546/* *			    Network driver methods		      * */
1547/* ******************************************************************** */
1548
1549static const struct net_device_ops scc_netdev_ops = {
1550	.ndo_open            = scc_net_open,
1551	.ndo_stop	     = scc_net_close,
1552	.ndo_start_xmit	     = scc_net_tx,
1553	.ndo_set_mac_address = scc_net_set_mac_address,
1554	.ndo_get_stats       = scc_net_get_stats,
1555	.ndo_siocdevprivate  = scc_net_siocdevprivate,
1556};
1557
1558/* ----> Initialize device <----- */
1559
1560static void scc_net_setup(struct net_device *dev)
1561{
1562	dev->tx_queue_len    = 16;	/* should be enough... */
1563
1564	dev->netdev_ops	     = &scc_netdev_ops;
1565	dev->header_ops      = &ax25_header_ops;
1566
1567	dev->flags      = 0;
1568
1569	dev->type = ARPHRD_AX25;
1570	dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
1571	dev->mtu = AX25_DEF_PACLEN;
1572	dev->addr_len = AX25_ADDR_LEN;
1573
1574	memcpy(dev->broadcast, &ax25_bcast,  AX25_ADDR_LEN);
1575	dev_addr_set(dev, (u8 *)&ax25_defaddr);
1576}
1577
1578/* ----> open network device <---- */
1579
1580static int scc_net_open(struct net_device *dev)
1581{
1582	struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
1583
1584	if (!scc->init)
1585		return -EINVAL;
1586
1587	scc->tx_buff = NULL;
1588	skb_queue_head_init(&scc->tx_queue);
1589
1590	init_channel(scc);
1591
1592	netif_start_queue(dev);
1593	return 0;
1594}
1595
1596/* ----> close network device <---- */
1597
1598static int scc_net_close(struct net_device *dev)
1599{
1600	struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
1601	unsigned long flags;
1602
1603	netif_stop_queue(dev);
1604
1605	spin_lock_irqsave(&scc->lock, flags);
1606	Outb(scc->ctrl,0);		/* Make sure pointer is written */
1607	wr(scc,R1,0);			/* disable interrupts */
1608	wr(scc,R3,0);
1609	spin_unlock_irqrestore(&scc->lock, flags);
1610
1611	del_timer_sync(&scc->tx_t);
1612	del_timer_sync(&scc->tx_wdog);
1613
1614	scc_discard_buffers(scc);
1615
1616	return 0;
1617}
1618
1619/* ----> receive frame, called from scc_rxint() <---- */
1620
1621static void scc_net_rx(struct scc_channel *scc, struct sk_buff *skb)
1622{
1623	if (skb->len == 0) {
1624		dev_kfree_skb_irq(skb);
1625		return;
1626	}
1627
1628	scc->dev_stat.rx_packets++;
1629	scc->dev_stat.rx_bytes += skb->len;
1630
1631	skb->protocol = ax25_type_trans(skb, scc->dev);
1632
1633	netif_rx(skb);
1634}
1635
1636/* ----> transmit frame <---- */
1637
1638static netdev_tx_t scc_net_tx(struct sk_buff *skb, struct net_device *dev)
1639{
1640	struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
1641	unsigned long flags;
1642	char kisscmd;
1643
1644	if (skb->protocol == htons(ETH_P_IP))
1645		return ax25_ip_xmit(skb);
1646
1647	if (skb->len > scc->stat.bufsize || skb->len < 2) {
1648		scc->dev_stat.tx_dropped++;	/* bogus frame */
1649		dev_kfree_skb(skb);
1650		return NETDEV_TX_OK;
1651	}
1652
1653	scc->dev_stat.tx_packets++;
1654	scc->dev_stat.tx_bytes += skb->len;
1655	scc->stat.txframes++;
1656
1657	kisscmd = *skb->data & 0x1f;
1658	skb_pull(skb, 1);
1659
1660	if (kisscmd) {
1661		scc_set_param(scc, kisscmd, *skb->data);
1662		dev_kfree_skb(skb);
1663		return NETDEV_TX_OK;
1664	}
1665
1666	spin_lock_irqsave(&scc->lock, flags);
1667
1668	if (skb_queue_len(&scc->tx_queue) > scc->dev->tx_queue_len) {
1669		struct sk_buff *skb_del;
1670		skb_del = skb_dequeue(&scc->tx_queue);
1671		dev_kfree_skb_irq(skb_del);
1672	}
1673	skb_queue_tail(&scc->tx_queue, skb);
1674	netif_trans_update(dev);
1675
1676
1677	/*
1678	 * Start transmission if the trx state is idle or
1679	 * t_idle hasn't expired yet. Use dwait/persistence/slottime
1680	 * algorithm for normal halfduplex operation.
1681	 */
1682
1683	if(scc->stat.tx_state == TXS_IDLE || scc->stat.tx_state == TXS_IDLE2) {
1684		scc->stat.tx_state = TXS_BUSY;
1685		if (scc->kiss.fulldup == KISS_DUPLEX_HALF)
1686			__scc_start_tx_timer(scc, t_dwait, scc->kiss.waittime);
1687		else
1688			__scc_start_tx_timer(scc, t_dwait, 0);
1689	}
1690	spin_unlock_irqrestore(&scc->lock, flags);
1691	return NETDEV_TX_OK;
1692}
1693
1694/* ----> ioctl functions <---- */
1695
1696/*
1697 * SIOCSCCCFG		- configure driver	arg: (struct scc_hw_config *) arg
1698 * SIOCSCCINI		- initialize driver	arg: ---
1699 * SIOCSCCCHANINI	- initialize channel	arg: (struct scc_modem *) arg
1700 * SIOCSCCSMEM		- set memory		arg: (struct scc_mem_config *) arg
1701 * SIOCSCCGKISS		- get level 1 parameter	arg: (struct scc_kiss_cmd *) arg
1702 * SIOCSCCSKISS		- set level 1 parameter arg: (struct scc_kiss_cmd *) arg
1703 * SIOCSCCGSTAT		- get driver status	arg: (struct scc_stat *) arg
1704 * SIOCSCCCAL		- send calib. pattern	arg: (struct scc_calibrate *) arg
1705 */
1706
1707static int scc_net_siocdevprivate(struct net_device *dev,
1708				  struct ifreq *ifr, void __user *arg, int cmd)
1709{
1710	struct scc_kiss_cmd kiss_cmd;
1711	struct scc_mem_config memcfg;
1712	struct scc_hw_config hwcfg;
1713	struct scc_calibrate cal;
1714	struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
1715	int chan;
1716	unsigned char device_name[IFNAMSIZ];
1717
1718	if (!Driver_Initialized)
1719	{
1720		if (cmd == SIOCSCCCFG)
1721		{
1722			int found = 1;
1723
1724			if (!capable(CAP_SYS_RAWIO)) return -EPERM;
1725			if (in_compat_syscall())
1726				return -EOPNOTSUPP;
1727
1728			if (!arg) return -EFAULT;
1729
1730			if (Nchips >= SCC_MAXCHIPS)
1731				return -EINVAL;
1732
1733			if (copy_from_user(&hwcfg, arg, sizeof(hwcfg)))
1734				return -EFAULT;
1735
1736			if (hwcfg.irq == 2) hwcfg.irq = 9;
1737
1738			if (hwcfg.irq < 0 || hwcfg.irq >= nr_irqs)
1739				return -EINVAL;
1740
1741			if (!Ivec[hwcfg.irq].used && hwcfg.irq)
1742			{
1743				if (request_irq(hwcfg.irq, scc_isr,
1744						0, "AX.25 SCC",
1745						(void *)(long) hwcfg.irq))
1746					printk(KERN_WARNING "z8530drv: warning, cannot get IRQ %d\n", hwcfg.irq);
1747				else
1748					Ivec[hwcfg.irq].used = 1;
1749			}
1750
1751			if (hwcfg.vector_latch && !Vector_Latch) {
1752				if (!request_region(hwcfg.vector_latch, 1, "scc vector latch"))
1753					printk(KERN_WARNING "z8530drv: warning, cannot reserve vector latch port 0x%lx\n, disabled.", hwcfg.vector_latch);
1754				else
1755					Vector_Latch = hwcfg.vector_latch;
1756			}
1757
1758			if (hwcfg.clock == 0)
1759				hwcfg.clock = SCC_DEFAULT_CLOCK;
1760
1761#ifndef SCC_DONT_CHECK
1762
1763			if(request_region(hwcfg.ctrl_a, 1, "scc-probe"))
1764			{
1765				disable_irq(hwcfg.irq);
1766				Outb(hwcfg.ctrl_a, 0);
1767				OutReg(hwcfg.ctrl_a, R9, FHWRES);
1768				udelay(100);
1769				OutReg(hwcfg.ctrl_a,R13,0x55);		/* is this chip really there? */
1770				udelay(5);
1771
1772				if (InReg(hwcfg.ctrl_a,R13) != 0x55)
1773					found = 0;
1774				enable_irq(hwcfg.irq);
1775				release_region(hwcfg.ctrl_a, 1);
1776			}
1777			else
1778				found = 0;
1779#endif
1780
1781			if (found)
1782			{
1783				SCC_Info[2*Nchips  ].ctrl = hwcfg.ctrl_a;
1784				SCC_Info[2*Nchips  ].data = hwcfg.data_a;
1785				SCC_Info[2*Nchips  ].irq  = hwcfg.irq;
1786				SCC_Info[2*Nchips+1].ctrl = hwcfg.ctrl_b;
1787				SCC_Info[2*Nchips+1].data = hwcfg.data_b;
1788				SCC_Info[2*Nchips+1].irq  = hwcfg.irq;
1789
1790				SCC_ctrl[Nchips].chan_A = hwcfg.ctrl_a;
1791				SCC_ctrl[Nchips].chan_B = hwcfg.ctrl_b;
1792				SCC_ctrl[Nchips].irq    = hwcfg.irq;
1793			}
1794
1795
1796			for (chan = 0; chan < 2; chan++)
1797			{
1798				sprintf(device_name, "%s%i", SCC_DriverName, 2*Nchips+chan);
1799
1800				SCC_Info[2*Nchips+chan].special = hwcfg.special;
1801				SCC_Info[2*Nchips+chan].clock = hwcfg.clock;
1802				SCC_Info[2*Nchips+chan].brand = hwcfg.brand;
1803				SCC_Info[2*Nchips+chan].option = hwcfg.option;
1804				SCC_Info[2*Nchips+chan].enhanced = hwcfg.escc;
1805
1806#ifdef SCC_DONT_CHECK
1807				printk(KERN_INFO "%s: data port = 0x%3.3x  control port = 0x%3.3x\n",
1808					device_name,
1809					SCC_Info[2*Nchips+chan].data,
1810					SCC_Info[2*Nchips+chan].ctrl);
1811
1812#else
1813				printk(KERN_INFO "%s: data port = 0x%3.3lx  control port = 0x%3.3lx -- %s\n",
1814					device_name,
1815					chan? hwcfg.data_b : hwcfg.data_a,
1816					chan? hwcfg.ctrl_b : hwcfg.ctrl_a,
1817					found? "found" : "missing");
1818#endif
1819
1820				if (found)
1821				{
1822					request_region(SCC_Info[2*Nchips+chan].ctrl, 1, "scc ctrl");
1823					request_region(SCC_Info[2*Nchips+chan].data, 1, "scc data");
1824					if (Nchips+chan != 0 &&
1825					    scc_net_alloc(device_name,
1826							  &SCC_Info[2*Nchips+chan]))
1827					    return -EINVAL;
1828				}
1829			}
1830
1831			if (found) Nchips++;
1832
1833			return 0;
1834		}
1835
1836		if (cmd == SIOCSCCINI)
1837		{
1838			if (!capable(CAP_SYS_RAWIO))
1839				return -EPERM;
1840
1841			if (Nchips == 0)
1842				return -EINVAL;
1843
1844			z8530_init();
1845			return 0;
1846		}
1847
1848		return -EINVAL;	/* confuse the user */
1849	}
1850
1851	if (!scc->init)
1852	{
1853		if (cmd == SIOCSCCCHANINI)
1854		{
1855			if (!capable(CAP_NET_ADMIN)) return -EPERM;
1856			if (!arg) return -EINVAL;
1857
1858			scc->stat.bufsize   = SCC_BUFSIZE;
1859
1860			if (copy_from_user(&scc->modem, arg, sizeof(struct scc_modem)))
1861				return -EINVAL;
1862
1863			/* default KISS Params */
1864
1865			if (scc->modem.speed < 4800)
1866			{
1867				scc->kiss.txdelay = 36;		/* 360 ms */
1868				scc->kiss.persist = 42;		/* 25% persistence */			/* was 25 */
1869				scc->kiss.slottime = 16;	/* 160 ms */
1870				scc->kiss.tailtime = 4;		/* minimal reasonable value */
1871				scc->kiss.fulldup = 0;		/* CSMA */
1872				scc->kiss.waittime = 50;	/* 500 ms */
1873				scc->kiss.maxkeyup = 10;	/* 10 s */
1874				scc->kiss.mintime = 3;		/* 3 s */
1875				scc->kiss.idletime = 30;	/* 30 s */
1876				scc->kiss.maxdefer = 120;	/* 2 min */
1877				scc->kiss.softdcd = 0;		/* hardware dcd */
1878			} else {
1879				scc->kiss.txdelay = 10;		/* 100 ms */
1880				scc->kiss.persist = 64;		/* 25% persistence */			/* was 25 */
1881				scc->kiss.slottime = 8;		/* 160 ms */
1882				scc->kiss.tailtime = 1;		/* minimal reasonable value */
1883				scc->kiss.fulldup = 0;		/* CSMA */
1884				scc->kiss.waittime = 50;	/* 500 ms */
1885				scc->kiss.maxkeyup = 7;		/* 7 s */
1886				scc->kiss.mintime = 3;		/* 3 s */
1887				scc->kiss.idletime = 30;	/* 30 s */
1888				scc->kiss.maxdefer = 120;	/* 2 min */
1889				scc->kiss.softdcd = 0;		/* hardware dcd */
1890			}
1891
1892			scc->tx_buff = NULL;
1893			skb_queue_head_init(&scc->tx_queue);
1894			scc->init = 1;
1895
1896			return 0;
1897		}
1898
1899		return -EINVAL;
1900	}
1901
1902	switch(cmd)
1903	{
1904		case SIOCSCCRESERVED:
1905			return -ENOIOCTLCMD;
1906
1907		case SIOCSCCSMEM:
1908			if (!capable(CAP_SYS_RAWIO)) return -EPERM;
1909			if (!arg || copy_from_user(&memcfg, arg, sizeof(memcfg)))
1910				return -EINVAL;
1911			scc->stat.bufsize   = memcfg.bufsize;
1912			return 0;
1913
1914		case SIOCSCCGSTAT:
1915			if (!arg || copy_to_user(arg, &scc->stat, sizeof(scc->stat)))
1916				return -EINVAL;
1917			return 0;
1918
1919		case SIOCSCCGKISS:
1920			if (!arg || copy_from_user(&kiss_cmd, arg, sizeof(kiss_cmd)))
1921				return -EINVAL;
1922			kiss_cmd.param = scc_get_param(scc, kiss_cmd.command);
1923			if (copy_to_user(arg, &kiss_cmd, sizeof(kiss_cmd)))
1924				return -EINVAL;
1925			return 0;
1926
1927		case SIOCSCCSKISS:
1928			if (!capable(CAP_NET_ADMIN)) return -EPERM;
1929			if (!arg || copy_from_user(&kiss_cmd, arg, sizeof(kiss_cmd)))
1930				return -EINVAL;
1931			return scc_set_param(scc, kiss_cmd.command, kiss_cmd.param);
1932
1933		case SIOCSCCCAL:
1934			if (!capable(CAP_SYS_RAWIO)) return -EPERM;
1935			if (!arg || copy_from_user(&cal, arg, sizeof(cal)) || cal.time == 0)
1936				return -EINVAL;
1937
1938			scc_start_calibrate(scc, cal.time, cal.pattern);
1939			return 0;
1940
1941		default:
1942			return -ENOIOCTLCMD;
1943
1944	}
1945
1946	return -EINVAL;
1947}
1948
1949/* ----> set interface callsign <---- */
1950
1951static int scc_net_set_mac_address(struct net_device *dev, void *addr)
1952{
1953	struct sockaddr *sa = (struct sockaddr *) addr;
1954	dev_addr_set(dev, sa->sa_data);
1955	return 0;
1956}
1957
1958/* ----> get statistics <---- */
1959
1960static struct net_device_stats *scc_net_get_stats(struct net_device *dev)
1961{
1962	struct scc_channel *scc = (struct scc_channel *) dev->ml_priv;
1963
1964	scc->dev_stat.rx_errors = scc->stat.rxerrs + scc->stat.rx_over;
1965	scc->dev_stat.tx_errors = scc->stat.txerrs + scc->stat.tx_under;
1966	scc->dev_stat.rx_fifo_errors = scc->stat.rx_over;
1967	scc->dev_stat.tx_fifo_errors = scc->stat.tx_under;
1968
1969	return &scc->dev_stat;
1970}
1971
1972/* ******************************************************************** */
1973/* *		dump statistics to /proc/net/z8530drv		      * */
1974/* ******************************************************************** */
1975
1976#ifdef CONFIG_PROC_FS
1977
1978static inline struct scc_channel *scc_net_seq_idx(loff_t pos)
1979{
1980	int k;
1981
1982	for (k = 0; k < Nchips*2; ++k) {
1983		if (!SCC_Info[k].init)
1984			continue;
1985		if (pos-- == 0)
1986			return &SCC_Info[k];
1987	}
1988	return NULL;
1989}
1990
1991static void *scc_net_seq_start(struct seq_file *seq, loff_t *pos)
1992{
1993	return *pos ? scc_net_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1994
1995}
1996
1997static void *scc_net_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1998{
1999	unsigned k;
2000	struct scc_channel *scc = v;
2001	++*pos;
2002
2003	for (k = (v == SEQ_START_TOKEN) ? 0 : (scc - SCC_Info)+1;
2004	     k < Nchips*2; ++k) {
2005		if (SCC_Info[k].init)
2006			return &SCC_Info[k];
2007	}
2008	return NULL;
2009}
2010
2011static void scc_net_seq_stop(struct seq_file *seq, void *v)
2012{
2013}
2014
2015static int scc_net_seq_show(struct seq_file *seq, void *v)
2016{
2017	if (v == SEQ_START_TOKEN) {
2018		seq_puts(seq, "z8530drv-"VERSION"\n");
2019	} else if (!Driver_Initialized) {
2020		seq_puts(seq, "not initialized\n");
2021	} else if (!Nchips) {
2022		seq_puts(seq, "chips missing\n");
2023	} else {
2024		const struct scc_channel *scc = v;
2025		const struct scc_stat *stat = &scc->stat;
2026		const struct scc_kiss *kiss = &scc->kiss;
2027
2028
2029		/* dev	data ctrl irq clock brand enh vector special option
2030		 *	baud nrz clocksrc softdcd bufsize
2031		 *	rxints txints exints spints
2032		 *	rcvd rxerrs over / xmit txerrs under / nospace bufsize
2033		 *	txd pers slot tail ful wait min maxk idl defr txof grp
2034		 *	W ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
2035		 *	R ## ## XX ## ## ## ## ## XX ## ## ## ## ## ## ##
2036		 */
2037
2038		seq_printf(seq, "%s\t%3.3lx %3.3lx %d %lu %2.2x %d %3.3lx %3.3lx %d\n",
2039				scc->dev->name,
2040				scc->data, scc->ctrl, scc->irq, scc->clock, scc->brand,
2041				scc->enhanced, Vector_Latch, scc->special,
2042				scc->option);
2043		seq_printf(seq, "\t%lu %d %d %d %d\n",
2044				scc->modem.speed, scc->modem.nrz,
2045				scc->modem.clocksrc, kiss->softdcd,
2046				stat->bufsize);
2047		seq_printf(seq, "\t%lu %lu %lu %lu\n",
2048				stat->rxints, stat->txints, stat->exints, stat->spints);
2049		seq_printf(seq, "\t%lu %lu %d / %lu %lu %d / %d %d\n",
2050				stat->rxframes, stat->rxerrs, stat->rx_over,
2051				stat->txframes, stat->txerrs, stat->tx_under,
2052				stat->nospace,  stat->tx_state);
2053
2054#define K(x) kiss->x
2055		seq_printf(seq, "\t%d %d %d %d %d %d %d %d %d %d %d %d\n",
2056				K(txdelay), K(persist), K(slottime), K(tailtime),
2057				K(fulldup), K(waittime), K(mintime), K(maxkeyup),
2058				K(idletime), K(maxdefer), K(tx_inhibit), K(group));
2059#undef K
2060#ifdef SCC_DEBUG
2061		{
2062			int reg;
2063
2064		seq_printf(seq, "\tW ");
2065			for (reg = 0; reg < 16; reg++)
2066				seq_printf(seq, "%2.2x ", scc->wreg[reg]);
2067			seq_printf(seq, "\n");
2068
2069		seq_printf(seq, "\tR %2.2x %2.2x XX ", InReg(scc->ctrl,R0), InReg(scc->ctrl,R1));
2070			for (reg = 3; reg < 8; reg++)
2071				seq_printf(seq, "%2.2x ", InReg(scc->ctrl, reg));
2072			seq_printf(seq, "XX ");
2073			for (reg = 9; reg < 16; reg++)
2074				seq_printf(seq, "%2.2x ", InReg(scc->ctrl, reg));
2075			seq_printf(seq, "\n");
2076		}
2077#endif
2078		seq_putc(seq, '\n');
2079	}
2080
2081        return 0;
2082}
2083
2084static const struct seq_operations scc_net_seq_ops = {
2085	.start  = scc_net_seq_start,
2086	.next   = scc_net_seq_next,
2087	.stop   = scc_net_seq_stop,
2088	.show   = scc_net_seq_show,
2089};
2090#endif /* CONFIG_PROC_FS */
2091
2092
2093/* ******************************************************************** */
2094/* * 			Init SCC driver 			      * */
2095/* ******************************************************************** */
2096
2097static int __init scc_init_driver (void)
2098{
2099	char devname[IFNAMSIZ];
2100
2101	printk(banner);
2102
2103	sprintf(devname,"%s0", SCC_DriverName);
2104
2105	rtnl_lock();
2106	if (scc_net_alloc(devname, SCC_Info)) {
2107		rtnl_unlock();
2108		printk(KERN_ERR "z8530drv: cannot initialize module\n");
2109		return -EIO;
2110	}
2111	rtnl_unlock();
2112
2113	proc_create_seq("z8530drv", 0, init_net.proc_net, &scc_net_seq_ops);
2114
2115	return 0;
2116}
2117
2118static void __exit scc_cleanup_driver(void)
2119{
2120	io_port ctrl;
2121	int k;
2122	struct scc_channel *scc;
2123	struct net_device *dev;
2124
2125	if (Nchips == 0 && (dev = SCC_Info[0].dev))
2126	{
2127		unregister_netdev(dev);
2128		free_netdev(dev);
2129	}
2130
2131	/* Guard against chip prattle */
2132	local_irq_disable();
2133
2134	for (k = 0; k < Nchips; k++)
2135		if ( (ctrl = SCC_ctrl[k].chan_A) )
2136		{
2137			Outb(ctrl, 0);
2138			OutReg(ctrl,R9,FHWRES);	/* force hardware reset */
2139			udelay(50);
2140		}
2141
2142	/* To unload the port must be closed so no real IRQ pending */
2143	for (k = 0; k < nr_irqs ; k++)
2144		if (Ivec[k].used) free_irq(k, NULL);
2145
2146	local_irq_enable();
2147
2148	/* Now clean up */
2149	for (k = 0; k < Nchips*2; k++)
2150	{
2151		scc = &SCC_Info[k];
2152		if (scc->ctrl)
2153		{
2154			release_region(scc->ctrl, 1);
2155			release_region(scc->data, 1);
2156		}
2157		if (scc->dev)
2158		{
2159			unregister_netdev(scc->dev);
2160			free_netdev(scc->dev);
2161		}
2162	}
2163
2164
2165	if (Vector_Latch)
2166		release_region(Vector_Latch, 1);
2167
2168	remove_proc_entry("z8530drv", init_net.proc_net);
2169}
2170
2171MODULE_AUTHOR("Joerg Reuter <jreuter@yaina.de>");
2172MODULE_DESCRIPTION("AX.25 Device Driver for Z8530 based HDLC cards");
2173MODULE_LICENSE("GPL");
2174module_init(scc_init_driver);
2175module_exit(scc_cleanup_driver);
2176