File: /usr/src/linux/drivers/char/stallion.c

1     /*****************************************************************************/
2     
3     /*
4      *	stallion.c  -- stallion multiport serial driver.
5      *
6      *	Copyright (C) 1996-1999  Stallion Technologies (support@stallion.oz.au).
7      *	Copyright (C) 1994-1996  Greg Ungerer.
8      *
9      *	This code is loosely based on the Linux serial driver, written by
10      *	Linus Torvalds, Theodore T'so and others.
11      *
12      *	This program is free software; you can redistribute it and/or modify
13      *	it under the terms of the GNU General Public License as published by
14      *	the Free Software Foundation; either version 2 of the License, or
15      *	(at your option) any later version.
16      *
17      *	This program is distributed in the hope that it will be useful,
18      *	but WITHOUT ANY WARRANTY; without even the implied warranty of
19      *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20      *	GNU General Public License for more details.
21      *
22      *	You should have received a copy of the GNU General Public License
23      *	along with this program; if not, write to the Free Software
24      *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25      */
26     
27     /*****************************************************************************/
28     
29     #include <linux/config.h>
30     #include <linux/module.h>
31     #include <linux/version.h> /* for linux/stallion.h */
32     #include <linux/slab.h>
33     #include <linux/interrupt.h>
34     #include <linux/tty.h>
35     #include <linux/tty_flip.h>
36     #include <linux/serial.h>
37     #include <linux/cd1400.h>
38     #include <linux/sc26198.h>
39     #include <linux/comstats.h>
40     #include <linux/stallion.h>
41     #include <linux/ioport.h>
42     #include <linux/init.h>
43     #include <linux/smp_lock.h>
44     #include <linux/devfs_fs_kernel.h>
45     
46     #include <asm/io.h>
47     #include <asm/uaccess.h>
48     
49     #ifdef CONFIG_PCI
50     #include <linux/pci.h>
51     #endif
52     
53     /*****************************************************************************/
54     
55     /*
56      *	Define different board types. Use the standard Stallion "assigned"
57      *	board numbers. Boards supported in this driver are abbreviated as
58      *	EIO = EasyIO and ECH = EasyConnection 8/32.
59      */
60     #define	BRD_EASYIO	20
61     #define	BRD_ECH		21
62     #define	BRD_ECHMC	22
63     #define	BRD_ECHPCI	26
64     #define	BRD_ECH64PCI	27
65     #define	BRD_EASYIOPCI	28
66     
67     /*
68      *	Define a configuration structure to hold the board configuration.
69      *	Need to set this up in the code (for now) with the boards that are
70      *	to be configured into the system. This is what needs to be modified
71      *	when adding/removing/modifying boards. Each line entry in the
72      *	stl_brdconf[] array is a board. Each line contains io/irq/memory
73      *	ranges for that board (as well as what type of board it is).
74      *	Some examples:
75      *		{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
76      *	This line would configure an EasyIO board (4 or 8, no difference),
77      *	at io address 2a0 and irq 10.
78      *	Another example:
79      *		{ BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
80      *	This line will configure an EasyConnection 8/32 board at primary io
81      *	address 2a8, secondary io address 280 and irq 12.
82      *	Enter as many lines into this array as you want (only the first 4
83      *	will actually be used!). Any combination of EasyIO and EasyConnection
84      *	boards can be specified. EasyConnection 8/32 boards can share their
85      *	secondary io addresses between each other.
86      *
87      *	NOTE: there is no need to put any entries in this table for PCI
88      *	boards. They will be found automatically by the driver - provided
89      *	PCI BIOS32 support is compiled into the kernel.
90      */
91     
92     typedef struct {
93     	int		brdtype;
94     	int		ioaddr1;
95     	int		ioaddr2;
96     	unsigned long	memaddr;
97     	int		irq;
98     	int		irqtype;
99     } stlconf_t;
100     
101     static stlconf_t	stl_brdconf[] = {
102     	/*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
103     };
104     
105     static int	stl_nrbrds = sizeof(stl_brdconf) / sizeof(stlconf_t);
106     
107     /*****************************************************************************/
108     
109     /*
110      *	Define some important driver characteristics. Device major numbers
111      *	allocated as per Linux Device Registry.
112      */
113     #ifndef	STL_SIOMEMMAJOR
114     #define	STL_SIOMEMMAJOR		28
115     #endif
116     #ifndef	STL_SERIALMAJOR
117     #define	STL_SERIALMAJOR		24
118     #endif
119     #ifndef	STL_CALLOUTMAJOR
120     #define	STL_CALLOUTMAJOR	25
121     #endif
122     
123     #define	STL_DRVTYPSERIAL	1
124     #define	STL_DRVTYPCALLOUT	2
125     
126     /*
127      *	Set the TX buffer size. Bigger is better, but we don't want
128      *	to chew too much memory with buffers!
129      */
130     #define	STL_TXBUFLOW		512
131     #define	STL_TXBUFSIZE		4096
132     
133     /*****************************************************************************/
134     
135     /*
136      *	Define our local driver identity first. Set up stuff to deal with
137      *	all the local structures required by a serial tty driver.
138      */
139     static char	*stl_drvtitle = "Stallion Multiport Serial Driver";
140     static char	*stl_drvname = "stallion";
141     static char	*stl_drvversion = "5.6.0";
142     #ifdef CONFIG_DEVFS_FS
143     static char	*stl_serialname = "tts/E%d";
144     static char	*stl_calloutname = "cua/E%d";
145     #else
146     static char	*stl_serialname = "ttyE";
147     static char	*stl_calloutname = "cue";
148     #endif
149     
150     static struct tty_driver	stl_serial;
151     static struct tty_driver	stl_callout;
152     static struct tty_struct	*stl_ttys[STL_MAXDEVS];
153     static struct termios		*stl_termios[STL_MAXDEVS];
154     static struct termios		*stl_termioslocked[STL_MAXDEVS];
155     static int			stl_refcount;
156     
157     /*
158      *	We will need to allocate a temporary write buffer for chars that
159      *	come direct from user space. The problem is that a copy from user
160      *	space might cause a page fault (typically on a system that is
161      *	swapping!). All ports will share one buffer - since if the system
162      *	is already swapping a shared buffer won't make things any worse.
163      */
164     static char			*stl_tmpwritebuf;
165     static DECLARE_MUTEX(stl_tmpwritesem);
166     
167     /*
168      *	Define a local default termios struct. All ports will be created
169      *	with this termios initially. Basically all it defines is a raw port
170      *	at 9600, 8 data bits, 1 stop bit.
171      */
172     static struct termios		stl_deftermios = {
173     	c_cflag:	(B9600 | CS8 | CREAD | HUPCL | CLOCAL),
174     	c_cc:		INIT_C_CC,
175     };
176     
177     /*
178      *	Define global stats structures. Not used often, and can be
179      *	re-used for each stats call.
180      */
181     static comstats_t	stl_comstats;
182     static combrd_t		stl_brdstats;
183     static stlbrd_t		stl_dummybrd;
184     static stlport_t	stl_dummyport;
185     
186     /*
187      *	Define global place to put buffer overflow characters.
188      */
189     static char		stl_unwanted[SC26198_RXFIFOSIZE];
190     
191     /*
192      *	Keep track of what interrupts we have requested for us.
193      *	We don't need to request an interrupt twice if it is being
194      *	shared with another Stallion board.
195      */
196     static int	stl_gotintrs[STL_MAXBRDS];
197     static int	stl_numintrs;
198     
199     /*****************************************************************************/
200     
201     static stlbrd_t		*stl_brds[STL_MAXBRDS];
202     
203     /*
204      *	Per board state flags. Used with the state field of the board struct.
205      *	Not really much here!
206      */
207     #define	BRD_FOUND	0x1
208     
209     /*
210      *	Define the port structure istate flags. These set of flags are
211      *	modified at interrupt time - so setting and reseting them needs
212      *	to be atomic. Use the bit clear/setting routines for this.
213      */
214     #define	ASYI_TXBUSY	1
215     #define	ASYI_TXLOW	2
216     #define	ASYI_DCDCHANGE	3
217     #define	ASYI_TXFLOWED	4
218     
219     /*
220      *	Define an array of board names as printable strings. Handy for
221      *	referencing boards when printing trace and stuff.
222      */
223     static char	*stl_brdnames[] = {
224     	(char *) NULL,
225     	(char *) NULL,
226     	(char *) NULL,
227     	(char *) NULL,
228     	(char *) NULL,
229     	(char *) NULL,
230     	(char *) NULL,
231     	(char *) NULL,
232     	(char *) NULL,
233     	(char *) NULL,
234     	(char *) NULL,
235     	(char *) NULL,
236     	(char *) NULL,
237     	(char *) NULL,
238     	(char *) NULL,
239     	(char *) NULL,
240     	(char *) NULL,
241     	(char *) NULL,
242     	(char *) NULL,
243     	(char *) NULL,
244     	"EasyIO",
245     	"EC8/32-AT",
246     	"EC8/32-MC",
247     	(char *) NULL,
248     	(char *) NULL,
249     	(char *) NULL,
250     	"EC8/32-PCI",
251     	"EC8/64-PCI",
252     	"EasyIO-PCI",
253     };
254     
255     /*****************************************************************************/
256     
257     #ifdef MODULE
258     /*
259      *	Define some string labels for arguments passed from the module
260      *	load line. These allow for easy board definitions, and easy
261      *	modification of the io, memory and irq resoucres.
262      */
263     
264     static char	*board0[4];
265     static char	*board1[4];
266     static char	*board2[4];
267     static char	*board3[4];
268     
269     static char	**stl_brdsp[] = {
270     	(char **) &board0,
271     	(char **) &board1,
272     	(char **) &board2,
273     	(char **) &board3
274     };
275     
276     /*
277      *	Define a set of common board names, and types. This is used to
278      *	parse any module arguments.
279      */
280     
281     typedef struct stlbrdtype {
282     	char	*name;
283     	int	type;
284     } stlbrdtype_t;
285     
286     static stlbrdtype_t	stl_brdstr[] = {
287     	{ "easyio", BRD_EASYIO },
288     	{ "eio", BRD_EASYIO },
289     	{ "20", BRD_EASYIO },
290     	{ "ec8/32", BRD_ECH },
291     	{ "ec8/32-at", BRD_ECH },
292     	{ "ec8/32-isa", BRD_ECH },
293     	{ "ech", BRD_ECH },
294     	{ "echat", BRD_ECH },
295     	{ "21", BRD_ECH },
296     	{ "ec8/32-mc", BRD_ECHMC },
297     	{ "ec8/32-mca", BRD_ECHMC },
298     	{ "echmc", BRD_ECHMC },
299     	{ "echmca", BRD_ECHMC },
300     	{ "22", BRD_ECHMC },
301     	{ "ec8/32-pc", BRD_ECHPCI },
302     	{ "ec8/32-pci", BRD_ECHPCI },
303     	{ "26", BRD_ECHPCI },
304     	{ "ec8/64-pc", BRD_ECH64PCI },
305     	{ "ec8/64-pci", BRD_ECH64PCI },
306     	{ "ech-pci", BRD_ECH64PCI },
307     	{ "echpci", BRD_ECH64PCI },
308     	{ "echpc", BRD_ECH64PCI },
309     	{ "27", BRD_ECH64PCI },
310     	{ "easyio-pc", BRD_EASYIOPCI },
311     	{ "easyio-pci", BRD_EASYIOPCI },
312     	{ "eio-pci", BRD_EASYIOPCI },
313     	{ "eiopci", BRD_EASYIOPCI },
314     	{ "28", BRD_EASYIOPCI },
315     };
316     
317     /*
318      *	Define the module agruments.
319      */
320     MODULE_AUTHOR("Greg Ungerer");
321     MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
322     MODULE_LICENSE("GPL");
323     
324     MODULE_PARM(board0, "1-4s");
325     MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
326     MODULE_PARM(board1, "1-4s");
327     MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
328     MODULE_PARM(board2, "1-4s");
329     MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
330     MODULE_PARM(board3, "1-4s");
331     MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
332     
333     #endif
334     
335     /*****************************************************************************/
336     
337     /*
338      *	Hardware ID bits for the EasyIO and ECH boards. These defines apply
339      *	to the directly accessible io ports of these boards (not the uarts -
340      *	they are in cd1400.h and sc26198.h).
341      */
342     #define	EIO_8PORTRS	0x04
343     #define	EIO_4PORTRS	0x05
344     #define	EIO_8PORTDI	0x00
345     #define	EIO_8PORTM	0x06
346     #define	EIO_MK3		0x03
347     #define	EIO_IDBITMASK	0x07
348     
349     #define	EIO_BRDMASK	0xf0
350     #define	ID_BRD4		0x10
351     #define	ID_BRD8		0x20
352     #define	ID_BRD16	0x30
353     
354     #define	EIO_INTRPEND	0x08
355     #define	EIO_INTEDGE	0x00
356     #define	EIO_INTLEVEL	0x08
357     #define	EIO_0WS		0x10
358     
359     #define	ECH_ID		0xa0
360     #define	ECH_IDBITMASK	0xe0
361     #define	ECH_BRDENABLE	0x08
362     #define	ECH_BRDDISABLE	0x00
363     #define	ECH_INTENABLE	0x01
364     #define	ECH_INTDISABLE	0x00
365     #define	ECH_INTLEVEL	0x02
366     #define	ECH_INTEDGE	0x00
367     #define	ECH_INTRPEND	0x01
368     #define	ECH_BRDRESET	0x01
369     
370     #define	ECHMC_INTENABLE	0x01
371     #define	ECHMC_BRDRESET	0x02
372     
373     #define	ECH_PNLSTATUS	2
374     #define	ECH_PNL16PORT	0x20
375     #define	ECH_PNLIDMASK	0x07
376     #define	ECH_PNLXPID	0x40
377     #define	ECH_PNLINTRPEND	0x80
378     
379     #define	ECH_ADDR2MASK	0x1e0
380     
381     /*
382      *	Define the vector mapping bits for the programmable interrupt board
383      *	hardware. These bits encode the interrupt for the board to use - it
384      *	is software selectable (except the EIO-8M).
385      */
386     static unsigned char	stl_vecmap[] = {
387     	0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
388     	0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
389     };
390     
391     /*
392      *	Set up enable and disable macros for the ECH boards. They require
393      *	the secondary io address space to be activated and deactivated.
394      *	This way all ECH boards can share their secondary io region.
395      *	If this is an ECH-PCI board then also need to set the page pointer
396      *	to point to the correct page.
397      */
398     #define	BRDENABLE(brdnr,pagenr)						\
399     	if (stl_brds[(brdnr)]->brdtype == BRD_ECH)			\
400     		outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE),	\
401     			stl_brds[(brdnr)]->ioctrl);			\
402     	else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)		\
403     		outb((pagenr), stl_brds[(brdnr)]->ioctrl);
404     
405     #define	BRDDISABLE(brdnr)						\
406     	if (stl_brds[(brdnr)]->brdtype == BRD_ECH)			\
407     		outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE),	\
408     			stl_brds[(brdnr)]->ioctrl);
409     
410     #define	STL_CD1400MAXBAUD	230400
411     #define	STL_SC26198MAXBAUD	460800
412     
413     #define	STL_BAUDBASE		115200
414     #define	STL_CLOSEDELAY		(5 * HZ / 10)
415     
416     /*****************************************************************************/
417     
418     #ifdef CONFIG_PCI
419     
420     /*
421      *	Define the Stallion PCI vendor and device IDs.
422      */
423     #ifndef	PCI_VENDOR_ID_STALLION
424     #define	PCI_VENDOR_ID_STALLION		0x124d
425     #endif
426     #ifndef PCI_DEVICE_ID_ECHPCI832
427     #define	PCI_DEVICE_ID_ECHPCI832		0x0000
428     #endif
429     #ifndef PCI_DEVICE_ID_ECHPCI864
430     #define	PCI_DEVICE_ID_ECHPCI864		0x0002
431     #endif
432     #ifndef PCI_DEVICE_ID_EIOPCI
433     #define	PCI_DEVICE_ID_EIOPCI		0x0003
434     #endif
435     
436     /*
437      *	Define structure to hold all Stallion PCI boards.
438      */
439     typedef struct stlpcibrd {
440     	unsigned short		vendid;
441     	unsigned short		devid;
442     	int			brdtype;
443     } stlpcibrd_t;
444     
445     static stlpcibrd_t	stl_pcibrds[] = {
446     	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
447     	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
448     	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
449     	{ PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
450     };
451     
452     static int	stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
453     
454     #endif
455     
456     /*****************************************************************************/
457     
458     /*
459      *	Define macros to extract a brd/port number from a minor number.
460      */
461     #define	MINOR2BRD(min)		(((min) & 0xc0) >> 6)
462     #define	MINOR2PORT(min)		((min) & 0x3f)
463     
464     /*
465      *	Define a baud rate table that converts termios baud rate selector
466      *	into the actual baud rate value. All baud rate calculations are
467      *	based on the actual baud rate required.
468      */
469     static unsigned int	stl_baudrates[] = {
470     	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
471     	9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
472     };
473     
474     /*
475      *	Define some handy local macros...
476      */
477     #undef	MIN
478     #define	MIN(a,b)	(((a) <= (b)) ? (a) : (b))
479     
480     #undef	TOLOWER
481     #define	TOLOWER(x)	((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
482     
483     /*****************************************************************************/
484     
485     /*
486      *	Declare all those functions in this driver!
487      */
488     
489     #ifdef MODULE
490     int		init_module(void);
491     void		cleanup_module(void);
492     static void	stl_argbrds(void);
493     static int	stl_parsebrd(stlconf_t *confp, char **argp);
494     
495     static unsigned long stl_atol(char *str);
496     #endif
497     
498     int		stl_init(void);
499     static int	stl_open(struct tty_struct *tty, struct file *filp);
500     static void	stl_close(struct tty_struct *tty, struct file *filp);
501     static int	stl_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
502     static void	stl_putchar(struct tty_struct *tty, unsigned char ch);
503     static void	stl_flushchars(struct tty_struct *tty);
504     static int	stl_writeroom(struct tty_struct *tty);
505     static int	stl_charsinbuffer(struct tty_struct *tty);
506     static int	stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
507     static void	stl_settermios(struct tty_struct *tty, struct termios *old);
508     static void	stl_throttle(struct tty_struct *tty);
509     static void	stl_unthrottle(struct tty_struct *tty);
510     static void	stl_stop(struct tty_struct *tty);
511     static void	stl_start(struct tty_struct *tty);
512     static void	stl_flushbuffer(struct tty_struct *tty);
513     static void	stl_breakctl(struct tty_struct *tty, int state);
514     static void	stl_waituntilsent(struct tty_struct *tty, int timeout);
515     static void	stl_sendxchar(struct tty_struct *tty, char ch);
516     static void	stl_hangup(struct tty_struct *tty);
517     static int	stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
518     static int	stl_portinfo(stlport_t *portp, int portnr, char *pos);
519     static int	stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
520     
521     static int	stl_brdinit(stlbrd_t *brdp);
522     static int	stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
523     static int	stl_mapirq(int irq, char *name);
524     static void	stl_getserial(stlport_t *portp, struct serial_struct *sp);
525     static int	stl_setserial(stlport_t *portp, struct serial_struct *sp);
526     static int	stl_getbrdstats(combrd_t *bp);
527     static int	stl_getportstats(stlport_t *portp, comstats_t *cp);
528     static int	stl_clrportstats(stlport_t *portp, comstats_t *cp);
529     static int	stl_getportstruct(unsigned long arg);
530     static int	stl_getbrdstruct(unsigned long arg);
531     static int	stl_waitcarrier(stlport_t *portp, struct file *filp);
532     static void	stl_delay(int len);
533     static void	stl_intr(int irq, void *dev_id, struct pt_regs *regs);
534     static void	stl_eiointr(stlbrd_t *brdp);
535     static void	stl_echatintr(stlbrd_t *brdp);
536     static void	stl_echmcaintr(stlbrd_t *brdp);
537     static void	stl_echpciintr(stlbrd_t *brdp);
538     static void	stl_echpci64intr(stlbrd_t *brdp);
539     static void	stl_offintr(void *private);
540     static void	*stl_memalloc(int len);
541     static stlbrd_t *stl_allocbrd(void);
542     static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
543     
544     static inline int	stl_initbrds(void);
545     static inline int	stl_initeio(stlbrd_t *brdp);
546     static inline int	stl_initech(stlbrd_t *brdp);
547     static inline int	stl_getbrdnr(void);
548     
549     #ifdef	CONFIG_PCI
550     static inline int	stl_findpcibrds(void);
551     static inline int	stl_initpcibrd(int brdtype, struct pci_dev *devp);
552     #endif
553     
554     /*
555      *	CD1400 uart specific handling functions.
556      */
557     static void	stl_cd1400setreg(stlport_t *portp, int regnr, int value);
558     static int	stl_cd1400getreg(stlport_t *portp, int regnr);
559     static int	stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
560     static int	stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
561     static void	stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
562     static void	stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
563     static int	stl_cd1400getsignals(stlport_t *portp);
564     static void	stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
565     static void	stl_cd1400ccrwait(stlport_t *portp);
566     static void	stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
567     static void	stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
568     static void	stl_cd1400disableintrs(stlport_t *portp);
569     static void	stl_cd1400sendbreak(stlport_t *portp, int len);
570     static void	stl_cd1400flowctrl(stlport_t *portp, int state);
571     static void	stl_cd1400sendflow(stlport_t *portp, int state);
572     static void	stl_cd1400flush(stlport_t *portp);
573     static int	stl_cd1400datastate(stlport_t *portp);
574     static void	stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
575     static void	stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
576     static void	stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
577     static void	stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
578     static void	stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
579     
580     static inline int	stl_cd1400breakisr(stlport_t *portp, int ioaddr);
581     
582     /*
583      *	SC26198 uart specific handling functions.
584      */
585     static void	stl_sc26198setreg(stlport_t *portp, int regnr, int value);
586     static int	stl_sc26198getreg(stlport_t *portp, int regnr);
587     static int	stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
588     static int	stl_sc26198getglobreg(stlport_t *portp, int regnr);
589     static int	stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
590     static void	stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
591     static void	stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
592     static int	stl_sc26198getsignals(stlport_t *portp);
593     static void	stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
594     static void	stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
595     static void	stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
596     static void	stl_sc26198disableintrs(stlport_t *portp);
597     static void	stl_sc26198sendbreak(stlport_t *portp, int len);
598     static void	stl_sc26198flowctrl(stlport_t *portp, int state);
599     static void	stl_sc26198sendflow(stlport_t *portp, int state);
600     static void	stl_sc26198flush(stlport_t *portp);
601     static int	stl_sc26198datastate(stlport_t *portp);
602     static void	stl_sc26198wait(stlport_t *portp);
603     static void	stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
604     static void	stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
605     static void	stl_sc26198txisr(stlport_t *port);
606     static void	stl_sc26198rxisr(stlport_t *port, unsigned int iack);
607     static void	stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
608     static void	stl_sc26198rxbadchars(stlport_t *portp);
609     static void	stl_sc26198otherisr(stlport_t *port, unsigned int iack);
610     
611     /*****************************************************************************/
612     
613     /*
614      *	Generic UART support structure.
615      */
616     typedef struct uart {
617     	int	(*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
618     	void	(*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
619     	void	(*setport)(stlport_t *portp, struct termios *tiosp);
620     	int	(*getsignals)(stlport_t *portp);
621     	void	(*setsignals)(stlport_t *portp, int dtr, int rts);
622     	void	(*enablerxtx)(stlport_t *portp, int rx, int tx);
623     	void	(*startrxtx)(stlport_t *portp, int rx, int tx);
624     	void	(*disableintrs)(stlport_t *portp);
625     	void	(*sendbreak)(stlport_t *portp, int len);
626     	void	(*flowctrl)(stlport_t *portp, int state);
627     	void	(*sendflow)(stlport_t *portp, int state);
628     	void	(*flush)(stlport_t *portp);
629     	int	(*datastate)(stlport_t *portp);
630     	void	(*intr)(stlpanel_t *panelp, unsigned int iobase);
631     } uart_t;
632     
633     /*
634      *	Define some macros to make calling these functions nice and clean.
635      */
636     #define	stl_panelinit		(* ((uart_t *) panelp->uartp)->panelinit)
637     #define	stl_portinit		(* ((uart_t *) portp->uartp)->portinit)
638     #define	stl_setport		(* ((uart_t *) portp->uartp)->setport)
639     #define	stl_getsignals		(* ((uart_t *) portp->uartp)->getsignals)
640     #define	stl_setsignals		(* ((uart_t *) portp->uartp)->setsignals)
641     #define	stl_enablerxtx		(* ((uart_t *) portp->uartp)->enablerxtx)
642     #define	stl_startrxtx		(* ((uart_t *) portp->uartp)->startrxtx)
643     #define	stl_disableintrs	(* ((uart_t *) portp->uartp)->disableintrs)
644     #define	stl_sendbreak		(* ((uart_t *) portp->uartp)->sendbreak)
645     #define	stl_flowctrl		(* ((uart_t *) portp->uartp)->flowctrl)
646     #define	stl_sendflow		(* ((uart_t *) portp->uartp)->sendflow)
647     #define	stl_flush		(* ((uart_t *) portp->uartp)->flush)
648     #define	stl_datastate		(* ((uart_t *) portp->uartp)->datastate)
649     
650     /*****************************************************************************/
651     
652     /*
653      *	CD1400 UART specific data initialization.
654      */
655     static uart_t stl_cd1400uart = {
656     	stl_cd1400panelinit,
657     	stl_cd1400portinit,
658     	stl_cd1400setport,
659     	stl_cd1400getsignals,
660     	stl_cd1400setsignals,
661     	stl_cd1400enablerxtx,
662     	stl_cd1400startrxtx,
663     	stl_cd1400disableintrs,
664     	stl_cd1400sendbreak,
665     	stl_cd1400flowctrl,
666     	stl_cd1400sendflow,
667     	stl_cd1400flush,
668     	stl_cd1400datastate,
669     	stl_cd1400eiointr
670     };
671     
672     /*
673      *	Define the offsets within the register bank of a cd1400 based panel.
674      *	These io address offsets are common to the EasyIO board as well.
675      */
676     #define	EREG_ADDR	0
677     #define	EREG_DATA	4
678     #define	EREG_RXACK	5
679     #define	EREG_TXACK	6
680     #define	EREG_MDACK	7
681     
682     #define	EREG_BANKSIZE	8
683     
684     #define	CD1400_CLK	25000000
685     #define	CD1400_CLK8M	20000000
686     
687     /*
688      *	Define the cd1400 baud rate clocks. These are used when calculating
689      *	what clock and divisor to use for the required baud rate. Also
690      *	define the maximum baud rate allowed, and the default base baud.
691      */
692     static int	stl_cd1400clkdivs[] = {
693     	CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
694     };
695     
696     /*****************************************************************************/
697     
698     /*
699      *	SC26198 UART specific data initization.
700      */
701     static uart_t stl_sc26198uart = {
702     	stl_sc26198panelinit,
703     	stl_sc26198portinit,
704     	stl_sc26198setport,
705     	stl_sc26198getsignals,
706     	stl_sc26198setsignals,
707     	stl_sc26198enablerxtx,
708     	stl_sc26198startrxtx,
709     	stl_sc26198disableintrs,
710     	stl_sc26198sendbreak,
711     	stl_sc26198flowctrl,
712     	stl_sc26198sendflow,
713     	stl_sc26198flush,
714     	stl_sc26198datastate,
715     	stl_sc26198intr
716     };
717     
718     /*
719      *	Define the offsets within the register bank of a sc26198 based panel.
720      */
721     #define	XP_DATA		0
722     #define	XP_ADDR		1
723     #define	XP_MODID	2
724     #define	XP_STATUS	2
725     #define	XP_IACK		3
726     
727     #define	XP_BANKSIZE	4
728     
729     /*
730      *	Define the sc26198 baud rate table. Offsets within the table
731      *	represent the actual baud rate selector of sc26198 registers.
732      */
733     static unsigned int	sc26198_baudtable[] = {
734     	50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
735     	4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
736     	230400, 460800, 921600
737     };
738     
739     #define	SC26198_NRBAUDS		(sizeof(sc26198_baudtable) / sizeof(unsigned int))
740     
741     /*****************************************************************************/
742     
743     /*
744      *	Define the driver info for a user level control device. Used mainly
745      *	to get at port stats - only not using the port device itself.
746      */
747     static struct file_operations	stl_fsiomem = {
748     	owner:		THIS_MODULE,
749     	ioctl:		stl_memioctl,
750     };
751     
752     /*****************************************************************************/
753     
754     static devfs_handle_t devfs_handle;
755     
756     #ifdef MODULE
757     
758     /*
759      *	Loadable module initialization stuff.
760      */
761     
762     int init_module()
763     {
764     	unsigned long	flags;
765     
766     #if DEBUG
767     	printk("init_module()\n");
768     #endif
769     
770     	save_flags(flags);
771     	cli();
772     	stl_init();
773     	restore_flags(flags);
774     
775     	return(0);
776     }
777     
778     /*****************************************************************************/
779     
780     void cleanup_module()
781     {
782     	stlbrd_t	*brdp;
783     	stlpanel_t	*panelp;
784     	stlport_t	*portp;
785     	unsigned long	flags;
786     	int		i, j, k;
787     
788     #if DEBUG
789     	printk("cleanup_module()\n");
790     #endif
791     
792     	printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
793     		stl_drvversion);
794     
795     	save_flags(flags);
796     	cli();
797     
798     /*
799      *	Free up all allocated resources used by the ports. This includes
800      *	memory and interrupts. As part of this process we will also do
801      *	a hangup on every open port - to try to flush out any processes
802      *	hanging onto ports.
803      */
804     	i = tty_unregister_driver(&stl_serial);
805     	j = tty_unregister_driver(&stl_callout);
806     	if (i || j) {
807     		printk("STALLION: failed to un-register tty driver, "
808     			"errno=%d,%d\n", -i, -j);
809     		restore_flags(flags);
810     		return;
811     	}
812     	devfs_unregister (devfs_handle);
813     	if ((i = devfs_unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
814     		printk("STALLION: failed to un-register serial memory device, "
815     			"errno=%d\n", -i);
816     
817     	if (stl_tmpwritebuf != (char *) NULL)
818     		kfree(stl_tmpwritebuf);
819     
820     	for (i = 0; (i < stl_nrbrds); i++) {
821     		if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
822     			continue;
823     		for (j = 0; (j < STL_MAXPANELS); j++) {
824     			panelp = brdp->panels[j];
825     			if (panelp == (stlpanel_t *) NULL)
826     				continue;
827     			for (k = 0; (k < STL_PORTSPERPANEL); k++) {
828     				portp = panelp->ports[k];
829     				if (portp == (stlport_t *) NULL)
830     					continue;
831     				if (portp->tty != (struct tty_struct *) NULL)
832     					stl_hangup(portp->tty);
833     				if (portp->tx.buf != (char *) NULL)
834     					kfree(portp->tx.buf);
835     				kfree(portp);
836     			}
837     			kfree(panelp);
838     		}
839     
840     		release_region(brdp->ioaddr1, brdp->iosize1);
841     		if (brdp->iosize2 > 0)
842     			release_region(brdp->ioaddr2, brdp->iosize2);
843     
844     		kfree(brdp);
845     		stl_brds[i] = (stlbrd_t *) NULL;
846     	}
847     
848     	for (i = 0; (i < stl_numintrs); i++)
849     		free_irq(stl_gotintrs[i], NULL);
850     
851     	restore_flags(flags);
852     }
853     
854     /*****************************************************************************/
855     
856     /*
857      *	Check for any arguments passed in on the module load command line.
858      */
859     
860     static void stl_argbrds()
861     {
862     	stlconf_t	conf;
863     	stlbrd_t	*brdp;
864     	int		nrargs, i;
865     
866     #if DEBUG
867     	printk("stl_argbrds()\n");
868     #endif
869     
870     	nrargs = sizeof(stl_brdsp) / sizeof(char **);
871     
872     	for (i = stl_nrbrds; (i < nrargs); i++) {
873     		memset(&conf, 0, sizeof(conf));
874     		if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
875     			continue;
876     		if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
877     			continue;
878     		stl_nrbrds = i + 1;
879     		brdp->brdnr = i;
880     		brdp->brdtype = conf.brdtype;
881     		brdp->ioaddr1 = conf.ioaddr1;
882     		brdp->ioaddr2 = conf.ioaddr2;
883     		brdp->irq = conf.irq;
884     		brdp->irqtype = conf.irqtype;
885     		stl_brdinit(brdp);
886     	}
887     }
888     
889     /*****************************************************************************/
890     
891     /*
892      *	Convert an ascii string number into an unsigned long.
893      */
894     
895     static unsigned long stl_atol(char *str)
896     {
897     	unsigned long	val;
898     	int		base, c;
899     	char		*sp;
900     
901     	val = 0;
902     	sp = str;
903     	if ((*sp == '0') && (*(sp+1) == 'x')) {
904     		base = 16;
905     		sp += 2;
906     	} else if (*sp == '0') {
907     		base = 8;
908     		sp++;
909     	} else {
910     		base = 10;
911     	}
912     
913     	for (; (*sp != 0); sp++) {
914     		c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
915     		if ((c < 0) || (c >= base)) {
916     			printk("STALLION: invalid argument %s\n", str);
917     			val = 0;
918     			break;
919     		}
920     		val = (val * base) + c;
921     	}
922     	return(val);
923     }
924     
925     /*****************************************************************************/
926     
927     /*
928      *	Parse the supplied argument string, into the board conf struct.
929      */
930     
931     static int stl_parsebrd(stlconf_t *confp, char **argp)
932     {
933     	char	*sp;
934     	int	nrbrdnames, i;
935     
936     #if DEBUG
937     	printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
938     #endif
939     
940     	if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
941     		return(0);
942     
943     	for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
944     		*sp = TOLOWER(*sp);
945     
946     	nrbrdnames = sizeof(stl_brdstr) / sizeof(stlbrdtype_t);
947     	for (i = 0; (i < nrbrdnames); i++) {
948     		if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
949     			break;
950     	}
951     	if (i >= nrbrdnames) {
952     		printk("STALLION: unknown board name, %s?\n", argp[0]);
953     		return(0);
954     	}
955     
956     	confp->brdtype = stl_brdstr[i].type;
957     
958     	i = 1;
959     	if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
960     		confp->ioaddr1 = stl_atol(argp[i]);
961     	i++;
962     	if (confp->brdtype == BRD_ECH) {
963     		if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
964     			confp->ioaddr2 = stl_atol(argp[i]);
965     		i++;
966     	}
967     	if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
968     		confp->irq = stl_atol(argp[i]);
969     	return(1);
970     }
971     
972     #endif
973     
974     /*****************************************************************************/
975     
976     /*
977      *	Local driver kernel memory allocation routine.
978      */
979     
980     static void *stl_memalloc(int len)
981     {
982     	return((void *) kmalloc(len, GFP_KERNEL));
983     }
984     
985     /*****************************************************************************/
986     
987     /*
988      *	Allocate a new board structure. Fill out the basic info in it.
989      */
990     
991     static stlbrd_t *stl_allocbrd()
992     {
993     	stlbrd_t	*brdp;
994     
995     	brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
996     	if (brdp == (stlbrd_t *) NULL) {
997     		printk("STALLION: failed to allocate memory (size=%d)\n",
998     			sizeof(stlbrd_t));
999     		return((stlbrd_t *) NULL);
1000     	}
1001     
1002     	memset(brdp, 0, sizeof(stlbrd_t));
1003     	brdp->magic = STL_BOARDMAGIC;
1004     	return(brdp);
1005     }
1006     
1007     /*****************************************************************************/
1008     
1009     static int stl_open(struct tty_struct *tty, struct file *filp)
1010     {
1011     	stlport_t	*portp;
1012     	stlbrd_t	*brdp;
1013     	unsigned int	minordev;
1014     	int		brdnr, panelnr, portnr, rc;
1015     
1016     #if DEBUG
1017     	printk("stl_open(tty=%x,filp=%x): device=%x\n", (int) tty,
1018     		(int) filp, tty->device);
1019     #endif
1020     
1021     	minordev = MINOR(tty->device);
1022     	brdnr = MINOR2BRD(minordev);
1023     	if (brdnr >= stl_nrbrds)
1024     		return(-ENODEV);
1025     	brdp = stl_brds[brdnr];
1026     	if (brdp == (stlbrd_t *) NULL)
1027     		return(-ENODEV);
1028     	minordev = MINOR2PORT(minordev);
1029     	for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
1030     		if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
1031     			break;
1032     		if (minordev < brdp->panels[panelnr]->nrports) {
1033     			portnr = minordev;
1034     			break;
1035     		}
1036     		minordev -= brdp->panels[panelnr]->nrports;
1037     	}
1038     	if (portnr < 0)
1039     		return(-ENODEV);
1040     
1041     	portp = brdp->panels[panelnr]->ports[portnr];
1042     	if (portp == (stlport_t *) NULL)
1043     		return(-ENODEV);
1044     
1045     	MOD_INC_USE_COUNT;
1046     
1047     /*
1048      *	On the first open of the device setup the port hardware, and
1049      *	initialize the per port data structure.
1050      */
1051     	portp->tty = tty;
1052     	tty->driver_data = portp;
1053     	portp->refcount++;
1054     
1055     	if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1056     		if (portp->tx.buf == (char *) NULL) {
1057     			portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
1058     			if (portp->tx.buf == (char *) NULL)
1059     				return(-ENOMEM);
1060     			portp->tx.head = portp->tx.buf;
1061     			portp->tx.tail = portp->tx.buf;
1062     		}
1063     		stl_setport(portp, tty->termios);
1064     		portp->sigs = stl_getsignals(portp);
1065     		stl_setsignals(portp, 1, 1);
1066     		stl_enablerxtx(portp, 1, 1);
1067     		stl_startrxtx(portp, 1, 0);
1068     		clear_bit(TTY_IO_ERROR, &tty->flags);
1069     		portp->flags |= ASYNC_INITIALIZED;
1070     	}
1071     
1072     /*
1073      *	Check if this port is in the middle of closing. If so then wait
1074      *	until it is closed then return error status, based on flag settings.
1075      *	The sleep here does not need interrupt protection since the wakeup
1076      *	for it is done with the same context.
1077      */
1078     	if (portp->flags & ASYNC_CLOSING) {
1079     		interruptible_sleep_on(&portp->close_wait);
1080     		if (portp->flags & ASYNC_HUP_NOTIFY)
1081     			return(-EAGAIN);
1082     		return(-ERESTARTSYS);
1083     	}
1084     
1085     /*
1086      *	Based on type of open being done check if it can overlap with any
1087      *	previous opens still in effect. If we are a normal serial device
1088      *	then also we might have to wait for carrier.
1089      */
1090     	if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
1091     		if (portp->flags & ASYNC_NORMAL_ACTIVE)
1092     			return(-EBUSY);
1093     		if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1094     			if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
1095     			    (portp->session != current->session))
1096     				return(-EBUSY);
1097     			if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
1098     			    (portp->pgrp != current->pgrp))
1099     				return(-EBUSY);
1100     		}
1101     		portp->flags |= ASYNC_CALLOUT_ACTIVE;
1102     	} else {
1103     		if (filp->f_flags & O_NONBLOCK) {
1104     			if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1105     				return(-EBUSY);
1106     		} else {
1107     			if ((rc = stl_waitcarrier(portp, filp)) != 0)
1108     				return(rc);
1109     		}
1110     		portp->flags |= ASYNC_NORMAL_ACTIVE;
1111     	}
1112     
1113     	if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
1114     		if (tty->driver.subtype == STL_DRVTYPSERIAL)
1115     			*tty->termios = portp->normaltermios;
1116     		else
1117     			*tty->termios = portp->callouttermios;
1118     		stl_setport(portp, tty->termios);
1119     	}
1120     
1121     	portp->session = current->session;
1122     	portp->pgrp = current->pgrp;
1123     	return(0);
1124     }
1125     
1126     /*****************************************************************************/
1127     
1128     /*
1129      *	Possibly need to wait for carrier (DCD signal) to come high. Say
1130      *	maybe because if we are clocal then we don't need to wait...
1131      */
1132     
1133     static int stl_waitcarrier(stlport_t *portp, struct file *filp)
1134     {
1135     	unsigned long	flags;
1136     	int		rc, doclocal;
1137     
1138     #if DEBUG
1139     	printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
1140     #endif
1141     
1142     	rc = 0;
1143     	doclocal = 0;
1144     
1145     	if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1146     		if (portp->normaltermios.c_cflag & CLOCAL)
1147     			doclocal++;
1148     	} else {
1149     		if (portp->tty->termios->c_cflag & CLOCAL)
1150     			doclocal++;
1151     	}
1152     
1153     	save_flags(flags);
1154     	cli();
1155     	portp->openwaitcnt++;
1156     	if (! tty_hung_up_p(filp))
1157     		portp->refcount--;
1158     
1159     	for (;;) {
1160     		if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0)
1161     			stl_setsignals(portp, 1, 1);
1162     		if (tty_hung_up_p(filp) ||
1163     		    ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1164     			if (portp->flags & ASYNC_HUP_NOTIFY)
1165     				rc = -EBUSY;
1166     			else
1167     				rc = -ERESTARTSYS;
1168     			break;
1169     		}
1170     		if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
1171     		    ((portp->flags & ASYNC_CLOSING) == 0) &&
1172     		    (doclocal || (portp->sigs & TIOCM_CD))) {
1173     			break;
1174     		}
1175     		if (signal_pending(current)) {
1176     			rc = -ERESTARTSYS;
1177     			break;
1178     		}
1179     		interruptible_sleep_on(&portp->open_wait);
1180     	}
1181     
1182     	if (! tty_hung_up_p(filp))
1183     		portp->refcount++;
1184     	portp->openwaitcnt--;
1185     	restore_flags(flags);
1186     
1187     	return(rc);
1188     }
1189     
1190     /*****************************************************************************/
1191     
1192     static void stl_close(struct tty_struct *tty, struct file *filp)
1193     {
1194     	stlport_t	*portp;
1195     	unsigned long	flags;
1196     
1197     #if DEBUG
1198     	printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1199     #endif
1200     
1201     	portp = tty->driver_data;
1202     	if (portp == (stlport_t *) NULL)
1203     		return;
1204     
1205     	save_flags(flags);
1206     	cli();
1207     	if (tty_hung_up_p(filp)) {
1208     		MOD_DEC_USE_COUNT;
1209     		restore_flags(flags);
1210     		return;
1211     	}
1212     	if ((tty->count == 1) && (portp->refcount != 1))
1213     		portp->refcount = 1;
1214     	if (portp->refcount-- > 1) {
1215     		MOD_DEC_USE_COUNT;
1216     		restore_flags(flags);
1217     		return;
1218     	}
1219     
1220     	portp->refcount = 0;
1221     	portp->flags |= ASYNC_CLOSING;
1222     
1223     	if (portp->flags & ASYNC_NORMAL_ACTIVE)
1224     		portp->normaltermios = *tty->termios;
1225     	if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1226     		portp->callouttermios = *tty->termios;
1227     
1228     /*
1229      *	May want to wait for any data to drain before closing. The BUSY
1230      *	flag keeps track of whether we are still sending or not - it is
1231      *	very accurate for the cd1400, not quite so for the sc26198.
1232      *	(The sc26198 has no "end-of-data" interrupt only empty FIFO)
1233      */
1234     	tty->closing = 1;
1235     	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1236     		tty_wait_until_sent(tty, portp->closing_wait);
1237     	stl_waituntilsent(tty, (HZ / 2));
1238     
1239     	portp->flags &= ~ASYNC_INITIALIZED;
1240     	stl_disableintrs(portp);
1241     	if (tty->termios->c_cflag & HUPCL)
1242     		stl_setsignals(portp, 0, 0);
1243     	stl_enablerxtx(portp, 0, 0);
1244     	stl_flushbuffer(tty);
1245     	portp->istate = 0;
1246     	if (portp->tx.buf != (char *) NULL) {
1247     		kfree(portp->tx.buf);
1248     		portp->tx.buf = (char *) NULL;
1249     		portp->tx.head = (char *) NULL;
1250     		portp->tx.tail = (char *) NULL;
1251     	}
1252     	set_bit(TTY_IO_ERROR, &tty->flags);
1253     	if (tty->ldisc.flush_buffer)
1254     		(tty->ldisc.flush_buffer)(tty);
1255     
1256     	tty->closing = 0;
1257     	portp->tty = (struct tty_struct *) NULL;
1258     
1259     	if (portp->openwaitcnt) {
1260     		if (portp->close_delay)
1261     			stl_delay(portp->close_delay);
1262     		wake_up_interruptible(&portp->open_wait);
1263     	}
1264     
1265     	portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE |
1266     		ASYNC_CLOSING);
1267     	wake_up_interruptible(&portp->close_wait);
1268     	MOD_DEC_USE_COUNT;
1269     	restore_flags(flags);
1270     }
1271     
1272     /*****************************************************************************/
1273     
1274     /*
1275      *	Wait for a specified delay period, this is not a busy-loop. It will
1276      *	give up the processor while waiting. Unfortunately this has some
1277      *	rather intimate knowledge of the process management stuff.
1278      */
1279     
1280     static void stl_delay(int len)
1281     {
1282     #if DEBUG
1283     	printk("stl_delay(len=%d)\n", len);
1284     #endif
1285     	if (len > 0) {
1286     		current->state = TASK_INTERRUPTIBLE;
1287     		schedule_timeout(len);
1288     		current->state = TASK_RUNNING;
1289     	}
1290     }
1291     
1292     /*****************************************************************************/
1293     
1294     /*
1295      *	Write routine. Take data and stuff it in to the TX ring queue.
1296      *	If transmit interrupts are not running then start them.
1297      */
1298     
1299     static int stl_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1300     {
1301     	stlport_t	*portp;
1302     	unsigned int	len, stlen;
1303     	unsigned char	*chbuf;
1304     	char		*head, *tail;
1305     
1306     #if DEBUG
1307     	printk("stl_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
1308     		(int) tty, from_user, (int) buf, count);
1309     #endif
1310     
1311     	if ((tty == (struct tty_struct *) NULL) ||
1312     	    (stl_tmpwritebuf == (char *) NULL))
1313     		return(0);
1314     	portp = tty->driver_data;
1315     	if (portp == (stlport_t *) NULL)
1316     		return(0);
1317     	if (portp->tx.buf == (char *) NULL)
1318     		return(0);
1319     
1320     /*
1321      *	If copying direct from user space we must cater for page faults,
1322      *	causing us to "sleep" here for a while. To handle this copy in all
1323      *	the data we need now, into a local buffer. Then when we got it all
1324      *	copy it into the TX buffer.
1325      */
1326     	chbuf = (unsigned char *) buf;
1327     	if (from_user) {
1328     		head = portp->tx.head;
1329     		tail = portp->tx.tail;
1330     		len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) :
1331     			(tail - head - 1);
1332     		count = MIN(len, count);
1333     		
1334     		down(&stl_tmpwritesem);
1335     		copy_from_user(stl_tmpwritebuf, chbuf, count);
1336     		chbuf = &stl_tmpwritebuf[0];
1337     	}
1338     
1339     	head = portp->tx.head;
1340     	tail = portp->tx.tail;
1341     	if (head >= tail) {
1342     		len = STL_TXBUFSIZE - (head - tail) - 1;
1343     		stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
1344     	} else {
1345     		len = tail - head - 1;
1346     		stlen = len;
1347     	}
1348     
1349     	len = MIN(len, count);
1350     	count = 0;
1351     	while (len > 0) {
1352     		stlen = MIN(len, stlen);
1353     		memcpy(head, chbuf, stlen);
1354     		len -= stlen;
1355     		chbuf += stlen;
1356     		count += stlen;
1357     		head += stlen;
1358     		if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
1359     			head = portp->tx.buf;
1360     			stlen = tail - head;
1361     		}
1362     	}
1363     	portp->tx.head = head;
1364     
1365     	clear_bit(ASYI_TXLOW, &portp->istate);
1366     	stl_startrxtx(portp, -1, 1);
1367     
1368     	if (from_user)
1369     		up(&stl_tmpwritesem);
1370     
1371     	return(count);
1372     }
1373     
1374     /*****************************************************************************/
1375     
1376     static void stl_putchar(struct tty_struct *tty, unsigned char ch)
1377     {
1378     	stlport_t	*portp;
1379     	unsigned int	len;
1380     	char		*head, *tail;
1381     
1382     #if DEBUG
1383     	printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1384     #endif
1385     
1386     	if (tty == (struct tty_struct *) NULL)
1387     		return;
1388     	portp = tty->driver_data;
1389     	if (portp == (stlport_t *) NULL)
1390     		return;
1391     	if (portp->tx.buf == (char *) NULL)
1392     		return;
1393     
1394     	head = portp->tx.head;
1395     	tail = portp->tx.tail;
1396     
1397     	len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
1398     	len--;
1399     
1400     	if (len > 0) {
1401     		*head++ = ch;
1402     		if (head >= (portp->tx.buf + STL_TXBUFSIZE))
1403     			head = portp->tx.buf;
1404     	}	
1405     	portp->tx.head = head;
1406     }
1407     
1408     /*****************************************************************************/
1409     
1410     /*
1411      *	If there are any characters in the buffer then make sure that TX
1412      *	interrupts are on and get'em out. Normally used after the putchar
1413      *	routine has been called.
1414      */
1415     
1416     static void stl_flushchars(struct tty_struct *tty)
1417     {
1418     	stlport_t	*portp;
1419     
1420     #if DEBUG
1421     	printk("stl_flushchars(tty=%x)\n", (int) tty);
1422     #endif
1423     
1424     	if (tty == (struct tty_struct *) NULL)
1425     		return;
1426     	portp = tty->driver_data;
1427     	if (portp == (stlport_t *) NULL)
1428     		return;
1429     	if (portp->tx.buf == (char *) NULL)
1430     		return;
1431     
1432     #if 0
1433     	if (tty->stopped || tty->hw_stopped ||
1434     	    (portp->tx.head == portp->tx.tail))
1435     		return;
1436     #endif
1437     	stl_startrxtx(portp, -1, 1);
1438     }
1439     
1440     /*****************************************************************************/
1441     
1442     static int stl_writeroom(struct tty_struct *tty)
1443     {
1444     	stlport_t	*portp;
1445     	char		*head, *tail;
1446     
1447     #if DEBUG
1448     	printk("stl_writeroom(tty=%x)\n", (int) tty);
1449     #endif
1450     
1451     	if (tty == (struct tty_struct *) NULL)
1452     		return(0);
1453     	portp = tty->driver_data;
1454     	if (portp == (stlport_t *) NULL)
1455     		return(0);
1456     	if (portp->tx.buf == (char *) NULL)
1457     		return(0);
1458     
1459     	head = portp->tx.head;
1460     	tail = portp->tx.tail;
1461     	return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
1462     }
1463     
1464     /*****************************************************************************/
1465     
1466     /*
1467      *	Return number of chars in the TX buffer. Normally we would just
1468      *	calculate the number of chars in the buffer and return that, but if
1469      *	the buffer is empty and TX interrupts are still on then we return
1470      *	that the buffer still has 1 char in it. This way whoever called us
1471      *	will not think that ALL chars have drained - since the UART still
1472      *	must have some chars in it (we are busy after all).
1473      */
1474     
1475     static int stl_charsinbuffer(struct tty_struct *tty)
1476     {
1477     	stlport_t	*portp;
1478     	unsigned int	size;
1479     	char		*head, *tail;
1480     
1481     #if DEBUG
1482     	printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
1483     #endif
1484     
1485     	if (tty == (struct tty_struct *) NULL)
1486     		return(0);
1487     	portp = tty->driver_data;
1488     	if (portp == (stlport_t *) NULL)
1489     		return(0);
1490     	if (portp->tx.buf == (char *) NULL)
1491     		return(0);
1492     
1493     	head = portp->tx.head;
1494     	tail = portp->tx.tail;
1495     	size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1496     	if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1497     		size = 1;
1498     	return(size);
1499     }
1500     
1501     /*****************************************************************************/
1502     
1503     /*
1504      *	Generate the serial struct info.
1505      */
1506     
1507     static void stl_getserial(stlport_t *portp, struct serial_struct *sp)
1508     {
1509     	struct serial_struct	sio;
1510     	stlbrd_t		*brdp;
1511     
1512     #if DEBUG
1513     	printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1514     #endif
1515     
1516     	memset(&sio, 0, sizeof(struct serial_struct));
1517     	sio.line = portp->portnr;
1518     	sio.port = portp->ioaddr;
1519     	sio.flags = portp->flags;
1520     	sio.baud_base = portp->baud_base;
1521     	sio.close_delay = portp->close_delay;
1522     	sio.closing_wait = portp->closing_wait;
1523     	sio.custom_divisor = portp->custom_divisor;
1524     	sio.hub6 = 0;
1525     	if (portp->uartp == &stl_cd1400uart) {
1526     		sio.type = PORT_CIRRUS;
1527     		sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1528     	} else {
1529     		sio.type = PORT_UNKNOWN;
1530     		sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1531     	}
1532     
1533     	brdp = stl_brds[portp->brdnr];
1534     	if (brdp != (stlbrd_t *) NULL)
1535     		sio.irq = brdp->irq;
1536     
1537     	copy_to_user(sp, &sio, sizeof(struct serial_struct));
1538     }
1539     
1540     /*****************************************************************************/
1541     
1542     /*
1543      *	Set port according to the serial struct info.
1544      *	At this point we do not do any auto-configure stuff, so we will
1545      *	just quietly ignore any requests to change irq, etc.
1546      */
1547     
1548     static int stl_setserial(stlport_t *portp, struct serial_struct *sp)
1549     {
1550     	struct serial_struct	sio;
1551     
1552     #if DEBUG
1553     	printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1554     #endif
1555     
1556     	copy_from_user(&sio, sp, sizeof(struct serial_struct));
1557     	if (!capable(CAP_SYS_ADMIN)) {
1558     		if ((sio.baud_base != portp->baud_base) ||
1559     		    (sio.close_delay != portp->close_delay) ||
1560     		    ((sio.flags & ~ASYNC_USR_MASK) !=
1561     		    (portp->flags & ~ASYNC_USR_MASK)))
1562     			return(-EPERM);
1563     	} 
1564     
1565     	portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1566     		(sio.flags & ASYNC_USR_MASK);
1567     	portp->baud_base = sio.baud_base;
1568     	portp->close_delay = sio.close_delay;
1569     	portp->closing_wait = sio.closing_wait;
1570     	portp->custom_divisor = sio.custom_divisor;
1571     	stl_setport(portp, portp->tty->termios);
1572     	return(0);
1573     }
1574     
1575     /*****************************************************************************/
1576     
1577     static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1578     {
1579     	stlport_t	*portp;
1580     	unsigned int	ival;
1581     	int		rc;
1582     
1583     #if DEBUG
1584     	printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
1585     		(int) tty, (int) file, cmd, (int) arg);
1586     #endif
1587     
1588     	if (tty == (struct tty_struct *) NULL)
1589     		return(-ENODEV);
1590     	portp = tty->driver_data;
1591     	if (portp == (stlport_t *) NULL)
1592     		return(-ENODEV);
1593     
1594     	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1595      	    (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1596     		if (tty->flags & (1 << TTY_IO_ERROR))
1597     			return(-EIO);
1598     	}
1599     
1600     	rc = 0;
1601     
1602     	switch (cmd) {
1603     	case TIOCGSOFTCAR:
1604     		rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1605     			(unsigned int *) arg);
1606     		break;
1607     	case TIOCSSOFTCAR:
1608     		if ((rc = verify_area(VERIFY_READ, (void *) arg,
1609     		    sizeof(int))) == 0) {
1610     			get_user(ival, (unsigned int *) arg);
1611     			tty->termios->c_cflag =
1612     				(tty->termios->c_cflag & ~CLOCAL) |
1613     				(ival ? CLOCAL : 0);
1614     		}
1615     		break;
1616     	case TIOCMGET:
1617     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1618     		    sizeof(unsigned int))) == 0) {
1619     			ival = stl_getsignals(portp);
1620     			put_user(ival, (unsigned int *) arg);
1621     		}
1622     		break;
1623     	case TIOCMBIS:
1624     		if ((rc = verify_area(VERIFY_READ, (void *) arg,
1625     		    sizeof(unsigned int))) == 0) {
1626     			get_user(ival, (unsigned int *) arg);
1627     			stl_setsignals(portp, ((ival & TIOCM_DTR) ? 1 : -1),
1628     				((ival & TIOCM_RTS) ? 1 : -1));
1629     		}
1630     		break;
1631     	case TIOCMBIC:
1632     		if ((rc = verify_area(VERIFY_READ, (void *) arg,
1633     		    sizeof(unsigned int))) == 0) {
1634     			get_user(ival, (unsigned int *) arg);
1635     			stl_setsignals(portp, ((ival & TIOCM_DTR) ? 0 : -1),
1636     				((ival & TIOCM_RTS) ? 0 : -1));
1637     		}
1638     		break;
1639     	case TIOCMSET:
1640     		if ((rc = verify_area(VERIFY_READ, (void *) arg,
1641     		    sizeof(unsigned int))) == 0) {
1642     			get_user(ival, (unsigned int *) arg);
1643     			stl_setsignals(portp, ((ival & TIOCM_DTR) ? 1 : 0),
1644     				((ival & TIOCM_RTS) ? 1 : 0));
1645     		}
1646     		break;
1647     	case TIOCGSERIAL:
1648     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1649     		    sizeof(struct serial_struct))) == 0)
1650     			stl_getserial(portp, (struct serial_struct *) arg);
1651     		break;
1652     	case TIOCSSERIAL:
1653     		if ((rc = verify_area(VERIFY_READ, (void *) arg,
1654     		    sizeof(struct serial_struct))) == 0)
1655     			rc = stl_setserial(portp, (struct serial_struct *) arg);
1656     		break;
1657     	case COM_GETPORTSTATS:
1658     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1659     		    sizeof(comstats_t))) == 0)
1660     			rc = stl_getportstats(portp, (comstats_t *) arg);
1661     		break;
1662     	case COM_CLRPORTSTATS:
1663     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1664     		    sizeof(comstats_t))) == 0)
1665     			rc = stl_clrportstats(portp, (comstats_t *) arg);
1666     		break;
1667     	case TIOCSERCONFIG:
1668     	case TIOCSERGWILD:
1669     	case TIOCSERSWILD:
1670     	case TIOCSERGETLSR:
1671     	case TIOCSERGSTRUCT:
1672     	case TIOCSERGETMULTI:
1673     	case TIOCSERSETMULTI:
1674     	default:
1675     		rc = -ENOIOCTLCMD;
1676     		break;
1677     	}
1678     
1679     	return(rc);
1680     }
1681     
1682     /*****************************************************************************/
1683     
1684     static void stl_settermios(struct tty_struct *tty, struct termios *old)
1685     {
1686     	stlport_t	*portp;
1687     	struct termios	*tiosp;
1688     
1689     #if DEBUG
1690     	printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1691     #endif
1692     
1693     	if (tty == (struct tty_struct *) NULL)
1694     		return;
1695     	portp = tty->driver_data;
1696     	if (portp == (stlport_t *) NULL)
1697     		return;
1698     
1699     	tiosp = tty->termios;
1700     	if ((tiosp->c_cflag == old->c_cflag) &&
1701     	    (tiosp->c_iflag == old->c_iflag))
1702     		return;
1703     
1704     	stl_setport(portp, tiosp);
1705     	stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1706     		-1);
1707     	if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1708     		tty->hw_stopped = 0;
1709     		stl_start(tty);
1710     	}
1711     	if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1712     		wake_up_interruptible(&portp->open_wait);
1713     }
1714     
1715     /*****************************************************************************/
1716     
1717     /*
1718      *	Attempt to flow control who ever is sending us data. Based on termios
1719      *	settings use software or/and hardware flow control.
1720      */
1721     
1722     static void stl_throttle(struct tty_struct *tty)
1723     {
1724     	stlport_t	*portp;
1725     
1726     #if DEBUG
1727     	printk("stl_throttle(tty=%x)\n", (int) tty);
1728     #endif
1729     
1730     	if (tty == (struct tty_struct *) NULL)
1731     		return;
1732     	portp = tty->driver_data;
1733     	if (portp == (stlport_t *) NULL)
1734     		return;
1735     	stl_flowctrl(portp, 0);
1736     }
1737     
1738     /*****************************************************************************/
1739     
1740     /*
1741      *	Unflow control the device sending us data...
1742      */
1743     
1744     static void stl_unthrottle(struct tty_struct *tty)
1745     {
1746     	stlport_t	*portp;
1747     
1748     #if DEBUG
1749     	printk("stl_unthrottle(tty=%x)\n", (int) tty);
1750     #endif
1751     
1752     	if (tty == (struct tty_struct *) NULL)
1753     		return;
1754     	portp = tty->driver_data;
1755     	if (portp == (stlport_t *) NULL)
1756     		return;
1757     	stl_flowctrl(portp, 1);
1758     }
1759     
1760     /*****************************************************************************/
1761     
1762     /*
1763      *	Stop the transmitter. Basically to do this we will just turn TX
1764      *	interrupts off.
1765      */
1766     
1767     static void stl_stop(struct tty_struct *tty)
1768     {
1769     	stlport_t	*portp;
1770     
1771     #if DEBUG
1772     	printk("stl_stop(tty=%x)\n", (int) tty);
1773     #endif
1774     
1775     	if (tty == (struct tty_struct *) NULL)
1776     		return;
1777     	portp = tty->driver_data;
1778     	if (portp == (stlport_t *) NULL)
1779     		return;
1780     	stl_startrxtx(portp, -1, 0);
1781     }
1782     
1783     /*****************************************************************************/
1784     
1785     /*
1786      *	Start the transmitter again. Just turn TX interrupts back on.
1787      */
1788     
1789     static void stl_start(struct tty_struct *tty)
1790     {
1791     	stlport_t	*portp;
1792     
1793     #if DEBUG
1794     	printk("stl_start(tty=%x)\n", (int) tty);
1795     #endif
1796     
1797     	if (tty == (struct tty_struct *) NULL)
1798     		return;
1799     	portp = tty->driver_data;
1800     	if (portp == (stlport_t *) NULL)
1801     		return;
1802     	stl_startrxtx(portp, -1, 1);
1803     }
1804     
1805     /*****************************************************************************/
1806     
1807     /*
1808      *	Hangup this port. This is pretty much like closing the port, only
1809      *	a little more brutal. No waiting for data to drain. Shutdown the
1810      *	port and maybe drop signals.
1811      */
1812     
1813     static void stl_hangup(struct tty_struct *tty)
1814     {
1815     	stlport_t	*portp;
1816     
1817     #if DEBUG
1818     	printk("stl_hangup(tty=%x)\n", (int) tty);
1819     #endif
1820     
1821     	if (tty == (struct tty_struct *) NULL)
1822     		return;
1823     	portp = tty->driver_data;
1824     	if (portp == (stlport_t *) NULL)
1825     		return;
1826     
1827     	portp->flags &= ~ASYNC_INITIALIZED;
1828     	stl_disableintrs(portp);
1829     	if (tty->termios->c_cflag & HUPCL)
1830     		stl_setsignals(portp, 0, 0);
1831     	stl_enablerxtx(portp, 0, 0);
1832     	stl_flushbuffer(tty);
1833     	portp->istate = 0;
1834     	set_bit(TTY_IO_ERROR, &tty->flags);
1835     	if (portp->tx.buf != (char *) NULL) {
1836     		kfree(portp->tx.buf);
1837     		portp->tx.buf = (char *) NULL;
1838     		portp->tx.head = (char *) NULL;
1839     		portp->tx.tail = (char *) NULL;
1840     	}
1841     	portp->tty = (struct tty_struct *) NULL;
1842     	portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1843     	portp->refcount = 0;
1844     	wake_up_interruptible(&portp->open_wait);
1845     }
1846     
1847     /*****************************************************************************/
1848     
1849     static void stl_flushbuffer(struct tty_struct *tty)
1850     {
1851     	stlport_t	*portp;
1852     
1853     #if DEBUG
1854     	printk("stl_flushbuffer(tty=%x)\n", (int) tty);
1855     #endif
1856     
1857     	if (tty == (struct tty_struct *) NULL)
1858     		return;
1859     	portp = tty->driver_data;
1860     	if (portp == (stlport_t *) NULL)
1861     		return;
1862     
1863     	stl_flush(portp);
1864     	wake_up_interruptible(&tty->write_wait);
1865     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1866     	    tty->ldisc.write_wakeup)
1867     		(tty->ldisc.write_wakeup)(tty);
1868     }
1869     
1870     /*****************************************************************************/
1871     
1872     static void stl_breakctl(struct tty_struct *tty, int state)
1873     {
1874     	stlport_t	*portp;
1875     
1876     #if DEBUG
1877     	printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
1878     #endif
1879     
1880     	if (tty == (struct tty_struct *) NULL)
1881     		return;
1882     	portp = tty->driver_data;
1883     	if (portp == (stlport_t *) NULL)
1884     		return;
1885     
1886     	stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1887     }
1888     
1889     /*****************************************************************************/
1890     
1891     static void stl_waituntilsent(struct tty_struct *tty, int timeout)
1892     {
1893     	stlport_t	*portp;
1894     	unsigned long	tend;
1895     
1896     #if DEBUG
1897     	printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
1898     #endif
1899     
1900     	if (tty == (struct tty_struct *) NULL)
1901     		return;
1902     	portp = tty->driver_data;
1903     	if (portp == (stlport_t *) NULL)
1904     		return;
1905     
1906     	if (timeout == 0)
1907     		timeout = HZ;
1908     	tend = jiffies + timeout;
1909     
1910     	while (stl_datastate(portp)) {
1911     		if (signal_pending(current))
1912     			break;
1913     		stl_delay(2);
1914     		if (time_after_eq(jiffies, tend))
1915     			break;
1916     	}
1917     }
1918     
1919     /*****************************************************************************/
1920     
1921     static void stl_sendxchar(struct tty_struct *tty, char ch)
1922     {
1923     	stlport_t	*portp;
1924     
1925     #if DEBUG
1926     	printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
1927     #endif
1928     
1929     	if (tty == (struct tty_struct *) NULL)
1930     		return;
1931     	portp = tty->driver_data;
1932     	if (portp == (stlport_t *) NULL)
1933     		return;
1934     
1935     	if (ch == STOP_CHAR(tty))
1936     		stl_sendflow(portp, 0);
1937     	else if (ch == START_CHAR(tty))
1938     		stl_sendflow(portp, 1);
1939     	else
1940     		stl_putchar(tty, ch);
1941     }
1942     
1943     /*****************************************************************************/
1944     
1945     #define	MAXLINE		80
1946     
1947     /*
1948      *	Format info for a specified port. The line is deliberately limited
1949      *	to 80 characters. (If it is too long it will be truncated, if too
1950      *	short then padded with spaces).
1951      */
1952     
1953     static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
1954     {
1955     	char	*sp;
1956     	int	sigs, cnt;
1957     
1958     	sp = pos;
1959     	sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
1960     		portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1961     		(int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1962     
1963     	if (portp->stats.rxframing)
1964     		sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
1965     	if (portp->stats.rxparity)
1966     		sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
1967     	if (portp->stats.rxbreaks)
1968     		sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
1969     	if (portp->stats.rxoverrun)
1970     		sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);
1971     
1972     	sigs = stl_getsignals(portp);
1973     	cnt = sprintf(sp, "%s%s%s%s%s ",
1974     		(sigs & TIOCM_RTS) ? "|RTS" : "",
1975     		(sigs & TIOCM_CTS) ? "|CTS" : "",
1976     		(sigs & TIOCM_DTR) ? "|DTR" : "",
1977     		(sigs & TIOCM_CD) ? "|DCD" : "",
1978     		(sigs & TIOCM_DSR) ? "|DSR" : "");
1979     	*sp = ' ';
1980     	sp += cnt;
1981     
1982     	for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
1983     		*sp++ = ' ';
1984     	if (cnt >= MAXLINE)
1985     		pos[(MAXLINE - 2)] = '+';
1986     	pos[(MAXLINE - 1)] = '\n';
1987     
1988     	return(MAXLINE);
1989     }
1990     
1991     /*****************************************************************************/
1992     
1993     /*
1994      *	Port info, read from the /proc file system.
1995      */
1996     
1997     static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
1998     {
1999     	stlbrd_t	*brdp;
2000     	stlpanel_t	*panelp;
2001     	stlport_t	*portp;
2002     	int		brdnr, panelnr, portnr, totalport;
2003     	int		curoff, maxoff;
2004     	char		*pos;
2005     
2006     #if DEBUG
2007     	printk("stl_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
2008     		"data=%x\n", (int) page, (int) start, (int) off, count,
2009     		(int) eof, (int) data);
2010     #endif
2011     
2012     	pos = page;
2013     	totalport = 0;
2014     	curoff = 0;
2015     
2016     	if (off == 0) {
2017     		pos += sprintf(pos, "%s: version %s", stl_drvtitle,
2018     			stl_drvversion);
2019     		while (pos < (page + MAXLINE - 1))
2020     			*pos++ = ' ';
2021     		*pos++ = '\n';
2022     	}
2023     	curoff =  MAXLINE;
2024     
2025     /*
2026      *	We scan through for each board, panel and port. The offset is
2027      *	calculated on the fly, and irrelevant ports are skipped.
2028      */
2029     	for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
2030     		brdp = stl_brds[brdnr];
2031     		if (brdp == (stlbrd_t *) NULL)
2032     			continue;
2033     		if (brdp->state == 0)
2034     			continue;
2035     
2036     		maxoff = curoff + (brdp->nrports * MAXLINE);
2037     		if (off >= maxoff) {
2038     			curoff = maxoff;
2039     			continue;
2040     		}
2041     
2042     		totalport = brdnr * STL_MAXPORTS;
2043     		for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
2044     			panelp = brdp->panels[panelnr];
2045     			if (panelp == (stlpanel_t *) NULL)
2046     				continue;
2047     
2048     			maxoff = curoff + (panelp->nrports * MAXLINE);
2049     			if (off >= maxoff) {
2050     				curoff = maxoff;
2051     				totalport += panelp->nrports;
2052     				continue;
2053     			}
2054     
2055     			for (portnr = 0; (portnr < panelp->nrports); portnr++,
2056     			    totalport++) {
2057     				portp = panelp->ports[portnr];
2058     				if (portp == (stlport_t *) NULL)
2059     					continue;
2060     				if (off >= (curoff += MAXLINE))
2061     					continue;
2062     				if ((pos - page + MAXLINE) > count)
2063     					goto stl_readdone;
2064     				pos += stl_portinfo(portp, totalport, pos);
2065     			}
2066     		}
2067     	}
2068     
2069     	*eof = 1;
2070     
2071     stl_readdone:
2072     	*start = page;
2073     	return(pos - page);
2074     }
2075     
2076     /*****************************************************************************/
2077     
2078     /*
2079      *	All board interrupts are vectored through here first. This code then
2080      *	calls off to the approrpriate board interrupt handlers.
2081      */
2082     
2083     static void stl_intr(int irq, void *dev_id, struct pt_regs *regs)
2084     {
2085     	stlbrd_t	*brdp;
2086     	int		i;
2087     
2088     #if DEBUG
2089     	printk("stl_intr(irq=%d,regs=%x)\n", irq, (int) regs);
2090     #endif
2091     
2092     	for (i = 0; (i < stl_nrbrds); i++) {
2093     		if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
2094     			continue;
2095     		if (brdp->state == 0)
2096     			continue;
2097     		(* brdp->isr)(brdp);
2098     	}
2099     }
2100     
2101     /*****************************************************************************/
2102     
2103     /*
2104      *	Interrupt service routine for EasyIO board types.
2105      */
2106     
2107     static void stl_eiointr(stlbrd_t *brdp)
2108     {
2109     	stlpanel_t	*panelp;
2110     	unsigned int	iobase;
2111     
2112     	panelp = brdp->panels[0];
2113     	iobase = panelp->iobase;
2114     	while (inb(brdp->iostatus) & EIO_INTRPEND)
2115     		(* panelp->isr)(panelp, iobase);
2116     }
2117     
2118     /*****************************************************************************/
2119     
2120     /*
2121      *	Interrupt service routine for ECH-AT board types.
2122      */
2123     
2124     static void stl_echatintr(stlbrd_t *brdp)
2125     {
2126     	stlpanel_t	*panelp;
2127     	unsigned int	ioaddr;
2128     	int		bnknr;
2129     
2130     	outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2131     
2132     	while (inb(brdp->iostatus) & ECH_INTRPEND) {
2133     		for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2134     			ioaddr = brdp->bnkstataddr[bnknr];
2135     			if (inb(ioaddr) & ECH_PNLINTRPEND) {
2136     				panelp = brdp->bnk2panel[bnknr];
2137     				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
2138     			}
2139     		}
2140     	}
2141     
2142     	outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2143     }
2144     
2145     /*****************************************************************************/
2146     
2147     /*
2148      *	Interrupt service routine for ECH-MCA board types.
2149      */
2150     
2151     static void stl_echmcaintr(stlbrd_t *brdp)
2152     {
2153     	stlpanel_t	*panelp;
2154     	unsigned int	ioaddr;
2155     	int		bnknr;
2156     
2157     	while (inb(brdp->iostatus) & ECH_INTRPEND) {
2158     		for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2159     			ioaddr = brdp->bnkstataddr[bnknr];
2160     			if (inb(ioaddr) & ECH_PNLINTRPEND) {
2161     				panelp = brdp->bnk2panel[bnknr];
2162     				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
2163     			}
2164     		}
2165     	}
2166     }
2167     
2168     /*****************************************************************************/
2169     
2170     /*
2171      *	Interrupt service routine for ECH-PCI board types.
2172      */
2173     
2174     static void stl_echpciintr(stlbrd_t *brdp)
2175     {
2176     	stlpanel_t	*panelp;
2177     	unsigned int	ioaddr;
2178     	int		bnknr, recheck;
2179     
2180     	while (1) {
2181     		recheck = 0;
2182     		for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2183     			outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
2184     			ioaddr = brdp->bnkstataddr[bnknr];
2185     			if (inb(ioaddr) & ECH_PNLINTRPEND) {
2186     				panelp = brdp->bnk2panel[bnknr];
2187     				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
2188     				recheck++;
2189     			}
2190     		}
2191     		if (! recheck)
2192     			break;
2193     	}
2194     }
2195     
2196     /*****************************************************************************/
2197     
2198     /*
2199      *	Interrupt service routine for ECH-8/64-PCI board types.
2200      */
2201     
2202     static void stl_echpci64intr(stlbrd_t *brdp)
2203     {
2204     	stlpanel_t	*panelp;
2205     	unsigned int	ioaddr;
2206     	int		bnknr;
2207     
2208     	while (inb(brdp->ioctrl) & 0x1) {
2209     		for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2210     			ioaddr = brdp->bnkstataddr[bnknr];
2211     			if (inb(ioaddr) & ECH_PNLINTRPEND) {
2212     				panelp = brdp->bnk2panel[bnknr];
2213     				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
2214     			}
2215     		}
2216     	}
2217     }
2218     
2219     /*****************************************************************************/
2220     
2221     /*
2222      *	Service an off-level request for some channel.
2223      */
2224     static void stl_offintr(void *private)
2225     {
2226     	stlport_t		*portp;
2227     	struct tty_struct	*tty;
2228     	unsigned int		oldsigs;
2229     
2230     	portp = private;
2231     
2232     #if DEBUG
2233     	printk("stl_offintr(portp=%x)\n", (int) portp);
2234     #endif
2235     
2236     	if (portp == (stlport_t *) NULL)
2237     		goto out;
2238     
2239     	tty = portp->tty;
2240     	if (tty == (struct tty_struct *) NULL)
2241     		goto out;
2242     
2243     	lock_kernel();
2244     	if (test_bit(ASYI_TXLOW, &portp->istate)) {
2245     		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2246     		    tty->ldisc.write_wakeup)
2247     			(tty->ldisc.write_wakeup)(tty);
2248     		wake_up_interruptible(&tty->write_wait);
2249     	}
2250     	if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
2251     		clear_bit(ASYI_DCDCHANGE, &portp->istate);
2252     		oldsigs = portp->sigs;
2253     		portp->sigs = stl_getsignals(portp);
2254     		if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
2255     			wake_up_interruptible(&portp->open_wait);
2256     		if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
2257     			if (portp->flags & ASYNC_CHECK_CD) {
2258     				if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
2259     				    (portp->flags & ASYNC_CALLOUT_NOHUP))) {
2260     					tty_hangup(tty);	/* FIXME: module removal race here - AKPM */
2261     				}
2262     			}
2263     		}
2264     	}
2265     	unlock_kernel();
2266     out:
2267     	MOD_DEC_USE_COUNT;
2268     }
2269     
2270     /*****************************************************************************/
2271     
2272     /*
2273      *	Map in interrupt vector to this driver. Check that we don't
2274      *	already have this vector mapped, we might be sharing this
2275      *	interrupt across multiple boards.
2276      */
2277     
2278     static int __init stl_mapirq(int irq, char *name)
2279     {
2280     	int	rc, i;
2281     
2282     #if DEBUG
2283     	printk("stl_mapirq(irq=%d,name=%s)\n", irq, name);
2284     #endif
2285     
2286     	rc = 0;
2287     	for (i = 0; (i < stl_numintrs); i++) {
2288     		if (stl_gotintrs[i] == irq)
2289     			break;
2290     	}
2291     	if (i >= stl_numintrs) {
2292     		if (request_irq(irq, stl_intr, SA_SHIRQ, name, NULL) != 0) {
2293     			printk("STALLION: failed to register interrupt "
2294     				"routine for %s irq=%d\n", name, irq);
2295     			rc = -ENODEV;
2296     		} else {
2297     			stl_gotintrs[stl_numintrs++] = irq;
2298     		}
2299     	}
2300     	return(rc);
2301     }
2302     
2303     /*****************************************************************************/
2304     
2305     /*
2306      *	Initialize all the ports on a panel.
2307      */
2308     
2309     static int __init stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2310     {
2311     	stlport_t	*portp;
2312     	int		chipmask, i;
2313     
2314     #if DEBUG
2315     	printk("stl_initports(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
2316     #endif
2317     
2318     	chipmask = stl_panelinit(brdp, panelp);
2319     
2320     /*
2321      *	All UART's are initialized (if found!). Now go through and setup
2322      *	each ports data structures.
2323      */
2324     	for (i = 0; (i < panelp->nrports); i++) {
2325     		portp = (stlport_t *) stl_memalloc(sizeof(stlport_t));
2326     		if (portp == (stlport_t *) NULL) {
2327     			printk("STALLION: failed to allocate memory "
2328     				"(size=%d)\n", sizeof(stlport_t));
2329     			break;
2330     		}
2331     		memset(portp, 0, sizeof(stlport_t));
2332     
2333     		portp->magic = STL_PORTMAGIC;
2334     		portp->portnr = i;
2335     		portp->brdnr = panelp->brdnr;
2336     		portp->panelnr = panelp->panelnr;
2337     		portp->uartp = panelp->uartp;
2338     		portp->clk = brdp->clk;
2339     		portp->baud_base = STL_BAUDBASE;
2340     		portp->close_delay = STL_CLOSEDELAY;
2341     		portp->closing_wait = 30 * HZ;
2342     		portp->normaltermios = stl_deftermios;
2343     		portp->callouttermios = stl_deftermios;
2344     		portp->tqueue.routine = stl_offintr;
2345     		portp->tqueue.data = portp;
2346     		init_waitqueue_head(&portp->open_wait);
2347     		init_waitqueue_head(&portp->close_wait);
2348     		portp->stats.brd = portp->brdnr;
2349     		portp->stats.panel = portp->panelnr;
2350     		portp->stats.port = portp->portnr;
2351     		panelp->ports[i] = portp;
2352     		stl_portinit(brdp, panelp, portp);
2353     	}
2354     
2355     	return(0);
2356     }
2357     
2358     /*****************************************************************************/
2359     
2360     /*
2361      *	Try to find and initialize an EasyIO board.
2362      */
2363     
2364     static inline int stl_initeio(stlbrd_t *brdp)
2365     {
2366     	stlpanel_t	*panelp;
2367     	unsigned int	status;
2368     	char		*name;
2369     	int		rc;
2370     
2371     #if DEBUG
2372     	printk("stl_initeio(brdp=%x)\n", (int) brdp);
2373     #endif
2374     
2375     	brdp->ioctrl = brdp->ioaddr1 + 1;
2376     	brdp->iostatus = brdp->ioaddr1 + 2;
2377     
2378     	status = inb(brdp->iostatus);
2379     	if ((status & EIO_IDBITMASK) == EIO_MK3)
2380     		brdp->ioctrl++;
2381     
2382     /*
2383      *	Handle board specific stuff now. The real difference is PCI
2384      *	or not PCI.
2385      */
2386     	if (brdp->brdtype == BRD_EASYIOPCI) {
2387     		brdp->iosize1 = 0x80;
2388     		brdp->iosize2 = 0x80;
2389     		name = "serial(EIO-PCI)";
2390     		outb(0x41, (brdp->ioaddr2 + 0x4c));
2391     	} else {
2392     		brdp->iosize1 = 8;
2393     		name = "serial(EIO)";
2394     		if ((brdp->irq < 0) || (brdp->irq > 15) ||
2395     		    (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2396     			printk("STALLION: invalid irq=%d for brd=%d\n",
2397     				brdp->irq, brdp->brdnr);
2398     			return(-EINVAL);
2399     		}
2400     		outb((stl_vecmap[brdp->irq] | EIO_0WS |
2401     			((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
2402     			brdp->ioctrl);
2403     	}
2404     
2405     	if (check_region(brdp->ioaddr1, brdp->iosize1)) {
2406     		printk("STALLION: Warning, board %d I/O address %x conflicts "
2407     			"with another device\n", brdp->brdnr, brdp->ioaddr1);
2408     	}
2409     	if (brdp->iosize2 > 0) {
2410     		if (check_region(brdp->ioaddr2, brdp->iosize2)) {
2411     			printk("STALLION: Warning, board %d I/O address %x "
2412     				"conflicts with another device\n",
2413     				brdp->brdnr, brdp->ioaddr2);
2414     		}
2415     	}
2416      
2417     /*
2418      *	Everything looks OK, so let's go ahead and probe for the hardware.
2419      */
2420     	brdp->clk = CD1400_CLK;
2421     	brdp->isr = stl_eiointr;
2422     
2423     	switch (status & EIO_IDBITMASK) {
2424     	case EIO_8PORTM:
2425     		brdp->clk = CD1400_CLK8M;
2426     		/* fall thru */
2427     	case EIO_8PORTRS:
2428     	case EIO_8PORTDI:
2429     		brdp->nrports = 8;
2430     		break;
2431     	case EIO_4PORTRS:
2432     		brdp->nrports = 4;
2433     		break;
2434     	case EIO_MK3:
2435     		switch (status & EIO_BRDMASK) {
2436     		case ID_BRD4:
2437     			brdp->nrports = 4;
2438     			break;
2439     		case ID_BRD8:
2440     			brdp->nrports = 8;
2441     			break;
2442     		case ID_BRD16:
2443     			brdp->nrports = 16;
2444     			break;
2445     		default:
2446     			return(-ENODEV);
2447     		}
2448     		break;
2449     	default:
2450     		return(-ENODEV);
2451     	}
2452     
2453     /*
2454      *	We have verfied that the board is actually present, so now we
2455      *	can complete the setup.
2456      */
2457     	request_region(brdp->ioaddr1, brdp->iosize1, name);
2458     	if (brdp->iosize2 > 0)
2459     		request_region(brdp->ioaddr2, brdp->iosize2, name);
2460     
2461     	panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2462     	if (panelp == (stlpanel_t *) NULL) {
2463     		printk("STALLION: failed to allocate memory (size=%d)\n",
2464     			sizeof(stlpanel_t));
2465     		return(-ENOMEM);
2466     	}
2467     	memset(panelp, 0, sizeof(stlpanel_t));
2468     
2469     	panelp->magic = STL_PANELMAGIC;
2470     	panelp->brdnr = brdp->brdnr;
2471     	panelp->panelnr = 0;
2472     	panelp->nrports = brdp->nrports;
2473     	panelp->iobase = brdp->ioaddr1;
2474     	panelp->hwid = status;
2475     	if ((status & EIO_IDBITMASK) == EIO_MK3) {
2476     		panelp->uartp = (void *) &stl_sc26198uart;
2477     		panelp->isr = stl_sc26198intr;
2478     	} else {
2479     		panelp->uartp = (void *) &stl_cd1400uart;
2480     		panelp->isr = stl_cd1400eiointr;
2481     	}
2482     
2483     	brdp->panels[0] = panelp;
2484     	brdp->nrpanels = 1;
2485     	brdp->state |= BRD_FOUND;
2486     	brdp->hwid = status;
2487     	rc = stl_mapirq(brdp->irq, name);
2488     	return(rc);
2489     }
2490     
2491     /*****************************************************************************/
2492     
2493     /*
2494      *	Try to find an ECH board and initialize it. This code is capable of
2495      *	dealing with all types of ECH board.
2496      */
2497     
2498     static int inline stl_initech(stlbrd_t *brdp)
2499     {
2500     	stlpanel_t	*panelp;
2501     	unsigned int	status, nxtid, ioaddr, conflict;
2502     	int		panelnr, banknr, i;
2503     	char		*name;
2504     
2505     #if DEBUG
2506     	printk("stl_initech(brdp=%x)\n", (int) brdp);
2507     #endif
2508     
2509     	status = 0;
2510     	conflict = 0;
2511     
2512     /*
2513      *	Set up the initial board register contents for boards. This varies a
2514      *	bit between the different board types. So we need to handle each
2515      *	separately. Also do a check that the supplied IRQ is good.
2516      */
2517     	switch (brdp->brdtype) {
2518     
2519     	case BRD_ECH:
2520     		brdp->isr = stl_echatintr;
2521     		brdp->ioctrl = brdp->ioaddr1 + 1;
2522     		brdp->iostatus = brdp->ioaddr1 + 1;
2523     		status = inb(brdp->iostatus);
2524     		if ((status & ECH_IDBITMASK) != ECH_ID)
2525     			return(-ENODEV);
2526     		if ((brdp->irq < 0) || (brdp->irq > 15) ||
2527     		    (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2528     			printk("STALLION: invalid irq=%d for brd=%d\n",
2529     				brdp->irq, brdp->brdnr);
2530     			return(-EINVAL);
2531     		}
2532     		status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2533     		status |= (stl_vecmap[brdp->irq] << 1);
2534     		outb((status | ECH_BRDRESET), brdp->ioaddr1);
2535     		brdp->ioctrlval = ECH_INTENABLE |
2536     			((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2537     		for (i = 0; (i < 10); i++)
2538     			outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2539     		brdp->iosize1 = 2;
2540     		brdp->iosize2 = 32;
2541     		name = "serial(EC8/32)";
2542     		outb(status, brdp->ioaddr1);
2543     		break;
2544     
2545     	case BRD_ECHMC:
2546     		brdp->isr = stl_echmcaintr;
2547     		brdp->ioctrl = brdp->ioaddr1 + 0x20;
2548     		brdp->iostatus = brdp->ioctrl;
2549     		status = inb(brdp->iostatus);
2550     		if ((status & ECH_IDBITMASK) != ECH_ID)
2551     			return(-ENODEV);
2552     		if ((brdp->irq < 0) || (brdp->irq > 15) ||
2553     		    (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2554     			printk("STALLION: invalid irq=%d for brd=%d\n",
2555     				brdp->irq, brdp->brdnr);
2556     			return(-EINVAL);
2557     		}
2558     		outb(ECHMC_BRDRESET, brdp->ioctrl);
2559     		outb(ECHMC_INTENABLE, brdp->ioctrl);
2560     		brdp->iosize1 = 64;
2561     		name = "serial(EC8/32-MC)";
2562     		break;
2563     
2564     	case BRD_ECHPCI:
2565     		brdp->isr = stl_echpciintr;
2566     		brdp->ioctrl = brdp->ioaddr1 + 2;
2567     		brdp->iosize1 = 4;
2568     		brdp->iosize2 = 8;
2569     		name = "serial(EC8/32-PCI)";
2570     		break;
2571     
2572     	case BRD_ECH64PCI:
2573     		brdp->isr = stl_echpci64intr;
2574     		brdp->ioctrl = brdp->ioaddr2 + 0x40;
2575     		outb(0x43, (brdp->ioaddr1 + 0x4c));
2576     		brdp->iosize1 = 0x80;
2577     		brdp->iosize2 = 0x80;
2578     		name = "serial(EC8/64-PCI)";
2579     		break;
2580     
2581     	default:
2582     		printk("STALLION: unknown board type=%d\n", brdp->brdtype);
2583     		return(-EINVAL);
2584     		break;
2585     	}
2586     
2587     /*
2588      *	Check boards for possible IO address conflicts. We won't actually
2589      *	do anything about it here, just issue a warning...
2590      */
2591     	conflict = check_region(brdp->ioaddr1, brdp->iosize1) ?
2592     		brdp->ioaddr1 : 0;
2593     	if ((conflict == 0) && (brdp->iosize2 > 0))
2594     		conflict = check_region(brdp->ioaddr2, brdp->iosize2) ?
2595     			brdp->ioaddr2 : 0;
2596     	if (conflict) {
2597     		printk("STALLION: Warning, board %d I/O address %x conflicts "
2598     			"with another device\n", brdp->brdnr, conflict);
2599     	}
2600     
2601     	request_region(brdp->ioaddr1, brdp->iosize1, name);
2602     	if (brdp->iosize2 > 0)
2603     		request_region(brdp->ioaddr2, brdp->iosize2, name);
2604     
2605     /*
2606      *	Scan through the secondary io address space looking for panels.
2607      *	As we find'em allocate and initialize panel structures for each.
2608      */
2609     	brdp->clk = CD1400_CLK;
2610     	brdp->hwid = status;
2611     
2612     	ioaddr = brdp->ioaddr2;
2613     	banknr = 0;
2614     	panelnr = 0;
2615     	nxtid = 0;
2616     
2617     	for (i = 0; (i < STL_MAXPANELS); i++) {
2618     		if (brdp->brdtype == BRD_ECHPCI) {
2619     			outb(nxtid, brdp->ioctrl);
2620     			ioaddr = brdp->ioaddr2;
2621     		}
2622     		status = inb(ioaddr + ECH_PNLSTATUS);
2623     		if ((status & ECH_PNLIDMASK) != nxtid)
2624     			break;
2625     		panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2626     		if (panelp == (stlpanel_t *) NULL) {
2627     			printk("STALLION: failed to allocate memory "
2628     				"(size=%d)\n", sizeof(stlpanel_t));
2629     			break;
2630     		}
2631     		memset(panelp, 0, sizeof(stlpanel_t));
2632     		panelp->magic = STL_PANELMAGIC;
2633     		panelp->brdnr = brdp->brdnr;
2634     		panelp->panelnr = panelnr;
2635     		panelp->iobase = ioaddr;
2636     		panelp->pagenr = nxtid;
2637     		panelp->hwid = status;
2638     		brdp->bnk2panel[banknr] = panelp;
2639     		brdp->bnkpageaddr[banknr] = nxtid;
2640     		brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2641     
2642     		if (status & ECH_PNLXPID) {
2643     			panelp->uartp = (void *) &stl_sc26198uart;
2644     			panelp->isr = stl_sc26198intr;
2645     			if (status & ECH_PNL16PORT) {
2646     				panelp->nrports = 16;
2647     				brdp->bnk2panel[banknr] = panelp;
2648     				brdp->bnkpageaddr[banknr] = nxtid;
2649     				brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2650     					ECH_PNLSTATUS;
2651     			} else {
2652     				panelp->nrports = 8;
2653     			}
2654     		} else {
2655     			panelp->uartp = (void *) &stl_cd1400uart;
2656     			panelp->isr = stl_cd1400echintr;
2657     			if (status & ECH_PNL16PORT) {
2658     				panelp->nrports = 16;
2659     				panelp->ackmask = 0x80;
2660     				if (brdp->brdtype != BRD_ECHPCI)
2661     					ioaddr += EREG_BANKSIZE;
2662     				brdp->bnk2panel[banknr] = panelp;
2663     				brdp->bnkpageaddr[banknr] = ++nxtid;
2664     				brdp->bnkstataddr[banknr++] = ioaddr +
2665     					ECH_PNLSTATUS;
2666     			} else {
2667     				panelp->nrports = 8;
2668     				panelp->ackmask = 0xc0;
2669     			}
2670     		}
2671     
2672     		nxtid++;
2673     		ioaddr += EREG_BANKSIZE;
2674     		brdp->nrports += panelp->nrports;
2675     		brdp->panels[panelnr++] = panelp;
2676     		if ((brdp->brdtype != BRD_ECHPCI) &&
2677     		    (ioaddr >= (brdp->ioaddr2 + brdp->iosize2)))
2678     			break;
2679     	}
2680     
2681     	brdp->nrpanels = panelnr;
2682     	brdp->nrbnks = banknr;
2683     	if (brdp->brdtype == BRD_ECH)
2684     		outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2685     
2686     	brdp->state |= BRD_FOUND;
2687     	i = stl_mapirq(brdp->irq, name);
2688     	return(i);
2689     }
2690     
2691     /*****************************************************************************/
2692     
2693     /*
2694      *	Initialize and configure the specified board.
2695      *	Scan through all the boards in the configuration and see what we
2696      *	can find. Handle EIO and the ECH boards a little differently here
2697      *	since the initial search and setup is very different.
2698      */
2699     
2700     static int __init stl_brdinit(stlbrd_t *brdp)
2701     {
2702     	int	i;
2703     
2704     #if DEBUG
2705     	printk("stl_brdinit(brdp=%x)\n", (int) brdp);
2706     #endif
2707     
2708     	switch (brdp->brdtype) {
2709     	case BRD_EASYIO:
2710     	case BRD_EASYIOPCI:
2711     		stl_initeio(brdp);
2712     		break;
2713     	case BRD_ECH:
2714     	case BRD_ECHMC:
2715     	case BRD_ECHPCI:
2716     	case BRD_ECH64PCI:
2717     		stl_initech(brdp);
2718     		break;
2719     	default:
2720     		printk("STALLION: board=%d is unknown board type=%d\n",
2721     			brdp->brdnr, brdp->brdtype);
2722     		return(ENODEV);
2723     	}
2724     
2725     	stl_brds[brdp->brdnr] = brdp;
2726     	if ((brdp->state & BRD_FOUND) == 0) {
2727     		printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2728     			stl_brdnames[brdp->brdtype], brdp->brdnr,
2729     			brdp->ioaddr1, brdp->irq);
2730     		return(ENODEV);
2731     	}
2732     
2733     	for (i = 0; (i < STL_MAXPANELS); i++)
2734     		if (brdp->panels[i] != (stlpanel_t *) NULL)
2735     			stl_initports(brdp, brdp->panels[i]);
2736     
2737     	printk("STALLION: %s found, board=%d io=%x irq=%d "
2738     		"nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2739     		brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2740     		brdp->nrports);
2741     	return(0);
2742     }
2743     
2744     /*****************************************************************************/
2745     
2746     /*
2747      *	Find the next available board number that is free.
2748      */
2749     
2750     static inline int stl_getbrdnr()
2751     {
2752     	int	i;
2753     
2754     	for (i = 0; (i < STL_MAXBRDS); i++) {
2755     		if (stl_brds[i] == (stlbrd_t *) NULL) {
2756     			if (i >= stl_nrbrds)
2757     				stl_nrbrds = i + 1;
2758     			return(i);
2759     		}
2760     	}
2761     	return(-1);
2762     }
2763     
2764     /*****************************************************************************/
2765     
2766     #ifdef	CONFIG_PCI
2767     
2768     /*
2769      *	We have a Stallion board. Allocate a board structure and
2770      *	initialize it. Read its IO and IRQ resources from PCI
2771      *	configuration space.
2772      */
2773     
2774     static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp)
2775     {
2776     	stlbrd_t	*brdp;
2777     
2778     #if DEBUG
2779     	printk("stl_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n", brdtype,
2780     		devp->bus->number, devp->devfn);
2781     #endif
2782     
2783     	if (pci_enable_device(devp))
2784     		return(-EIO);
2785     	if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2786     		return(-ENOMEM);
2787     	if ((brdp->brdnr = stl_getbrdnr()) < 0) {
2788     		printk("STALLION: too many boards found, "
2789     			"maximum supported %d\n", STL_MAXBRDS);
2790     		return(0);
2791     	}
2792     	brdp->brdtype = brdtype;
2793     
2794     /*
2795      *	Different Stallion boards use the BAR registers in different ways,
2796      *	so set up io addresses based on board type.
2797      */
2798     #if DEBUG
2799     	printk("%s(%d): BAR[]=%x,%x,%x,%x IRQ=%x\n", __FILE__, __LINE__,
2800     		pci_resource_start(devp, 0), pci_resource_start(devp, 1),
2801     		pci_resource_start(devp, 2), pci_resource_start(devp, 3), devp->irq);
2802     #endif
2803     
2804     /*
2805      *	We have all resources from the board, so let's setup the actual
2806      *	board structure now.
2807      */
2808     	switch (brdtype) {
2809     	case BRD_ECHPCI:
2810     		brdp->ioaddr2 = pci_resource_start(devp, 0);
2811     		brdp->ioaddr1 = pci_resource_start(devp, 1);
2812     		break;
2813     	case BRD_ECH64PCI:
2814     		brdp->ioaddr2 = pci_resource_start(devp, 2);
2815     		brdp->ioaddr1 = pci_resource_start(devp, 1);
2816     		break;
2817     	case BRD_EASYIOPCI:
2818     		brdp->ioaddr1 = pci_resource_start(devp, 2);
2819     		brdp->ioaddr2 = pci_resource_start(devp, 1);
2820     		break;
2821     	default:
2822     		printk("STALLION: unknown PCI board type=%d\n", brdtype);
2823     		break;
2824     	}
2825     
2826     	brdp->irq = devp->irq;
2827     	stl_brdinit(brdp);
2828     
2829     	return(0);
2830     }
2831     
2832     /*****************************************************************************/
2833     
2834     /*
2835      *	Find all Stallion PCI boards that might be installed. Initialize each
2836      *	one as it is found.
2837      */
2838     
2839     
2840     static inline int stl_findpcibrds()
2841     {
2842     	struct pci_dev	*dev = NULL;
2843     	int		i, rc;
2844     
2845     #if DEBUG
2846     	printk("stl_findpcibrds()\n");
2847     #endif
2848     
2849     	if (! pci_present())
2850     		return(0);
2851     
2852     	for (i = 0; (i < stl_nrpcibrds); i++)
2853     		while ((dev = pci_find_device(stl_pcibrds[i].vendid,
2854     		    stl_pcibrds[i].devid, dev))) {
2855     
2856     /*
2857      *			Found a device on the PCI bus that has our vendor and
2858      *			device ID. Need to check now that it is really us.
2859      */
2860     			if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2861     				continue;
2862     
2863     			rc = stl_initpcibrd(stl_pcibrds[i].brdtype, dev);
2864     			if (rc)
2865     				return(rc);
2866     		}
2867     
2868     	return(0);
2869     }
2870     
2871     #endif
2872     
2873     /*****************************************************************************/
2874     
2875     /*
2876      *	Scan through all the boards in the configuration and see what we
2877      *	can find. Handle EIO and the ECH boards a little differently here
2878      *	since the initial search and setup is too different.
2879      */
2880     
2881     static inline int stl_initbrds()
2882     {
2883     	stlbrd_t	*brdp;
2884     	stlconf_t	*confp;
2885     	int		i;
2886     
2887     #if DEBUG
2888     	printk("stl_initbrds()\n");
2889     #endif
2890     
2891     	if (stl_nrbrds > STL_MAXBRDS) {
2892     		printk("STALLION: too many boards in configuration table, "
2893     			"truncating to %d\n", STL_MAXBRDS);
2894     		stl_nrbrds = STL_MAXBRDS;
2895     	}
2896     
2897     /*
2898      *	Firstly scan the list of static boards configured. Allocate
2899      *	resources and initialize the boards as found.
2900      */
2901     	for (i = 0; (i < stl_nrbrds); i++) {
2902     		confp = &stl_brdconf[i];
2903     #ifdef MODULE
2904     		stl_parsebrd(confp, stl_brdsp[i]);
2905     #endif
2906     		if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2907     			return(-ENOMEM);
2908     		brdp->brdnr = i;
2909     		brdp->brdtype = confp->brdtype;
2910     		brdp->ioaddr1 = confp->ioaddr1;
2911     		brdp->ioaddr2 = confp->ioaddr2;
2912     		brdp->irq = confp->irq;
2913     		brdp->irqtype = confp->irqtype;
2914     		stl_brdinit(brdp);
2915     	}
2916     
2917     /*
2918      *	Find any dynamically supported boards. That is via module load
2919      *	line options or auto-detected on the PCI bus.
2920      */
2921     #ifdef MODULE
2922     	stl_argbrds();
2923     #endif
2924     #ifdef CONFIG_PCI
2925     	stl_findpcibrds();
2926     #endif
2927     
2928     	return(0);
2929     }
2930     
2931     /*****************************************************************************/
2932     
2933     /*
2934      *	Return the board stats structure to user app.
2935      */
2936     
2937     static int stl_getbrdstats(combrd_t *bp)
2938     {
2939     	stlbrd_t	*brdp;
2940     	stlpanel_t	*panelp;
2941     	int		i;
2942     
2943     	copy_from_user(&stl_brdstats, bp, sizeof(combrd_t));
2944     	if (stl_brdstats.brd >= STL_MAXBRDS)
2945     		return(-ENODEV);
2946     	brdp = stl_brds[stl_brdstats.brd];
2947     	if (brdp == (stlbrd_t *) NULL)
2948     		return(-ENODEV);
2949     
2950     	memset(&stl_brdstats, 0, sizeof(combrd_t));
2951     	stl_brdstats.brd = brdp->brdnr;
2952     	stl_brdstats.type = brdp->brdtype;
2953     	stl_brdstats.hwid = brdp->hwid;
2954     	stl_brdstats.state = brdp->state;
2955     	stl_brdstats.ioaddr = brdp->ioaddr1;
2956     	stl_brdstats.ioaddr2 = brdp->ioaddr2;
2957     	stl_brdstats.irq = brdp->irq;
2958     	stl_brdstats.nrpanels = brdp->nrpanels;
2959     	stl_brdstats.nrports = brdp->nrports;
2960     	for (i = 0; (i < brdp->nrpanels); i++) {
2961     		panelp = brdp->panels[i];
2962     		stl_brdstats.panels[i].panel = i;
2963     		stl_brdstats.panels[i].hwid = panelp->hwid;
2964     		stl_brdstats.panels[i].nrports = panelp->nrports;
2965     	}
2966     
2967     	copy_to_user(bp, &stl_brdstats, sizeof(combrd_t));
2968     	return(0);
2969     }
2970     
2971     /*****************************************************************************/
2972     
2973     /*
2974      *	Resolve the referenced port number into a port struct pointer.
2975      */
2976     
2977     static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2978     {
2979     	stlbrd_t	*brdp;
2980     	stlpanel_t	*panelp;
2981     
2982     	if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2983     		return((stlport_t *) NULL);
2984     	brdp = stl_brds[brdnr];
2985     	if (brdp == (stlbrd_t *) NULL)
2986     		return((stlport_t *) NULL);
2987     	if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2988     		return((stlport_t *) NULL);
2989     	panelp = brdp->panels[panelnr];
2990     	if (panelp == (stlpanel_t *) NULL)
2991     		return((stlport_t *) NULL);
2992     	if ((portnr < 0) || (portnr >= panelp->nrports))
2993     		return((stlport_t *) NULL);
2994     	return(panelp->ports[portnr]);
2995     }
2996     
2997     /*****************************************************************************/
2998     
2999     /*
3000      *	Return the port stats structure to user app. A NULL port struct
3001      *	pointer passed in means that we need to find out from the app
3002      *	what port to get stats for (used through board control device).
3003      */
3004     
3005     static int stl_getportstats(stlport_t *portp, comstats_t *cp)
3006     {
3007     	unsigned char	*head, *tail;
3008     	unsigned long	flags;
3009     
3010     	if (portp == (stlport_t *) NULL) {
3011     		copy_from_user(&stl_comstats, cp, sizeof(comstats_t));
3012     		portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3013     			stl_comstats.port);
3014     		if (portp == (stlport_t *) NULL)
3015     			return(-ENODEV);
3016     	}
3017     
3018     	portp->stats.state = portp->istate;
3019     	portp->stats.flags = portp->flags;
3020     	portp->stats.hwid = portp->hwid;
3021     
3022     	portp->stats.ttystate = 0;
3023     	portp->stats.cflags = 0;
3024     	portp->stats.iflags = 0;
3025     	portp->stats.oflags = 0;
3026     	portp->stats.lflags = 0;
3027     	portp->stats.rxbuffered = 0;
3028     
3029     	save_flags(flags);
3030     	cli();
3031     	if (portp->tty != (struct tty_struct *) NULL) {
3032     		if (portp->tty->driver_data == portp) {
3033     			portp->stats.ttystate = portp->tty->flags;
3034     			portp->stats.rxbuffered = portp->tty->flip.count;
3035     			if (portp->tty->termios != (struct termios *) NULL) {
3036     				portp->stats.cflags = portp->tty->termios->c_cflag;
3037     				portp->stats.iflags = portp->tty->termios->c_iflag;
3038     				portp->stats.oflags = portp->tty->termios->c_oflag;
3039     				portp->stats.lflags = portp->tty->termios->c_lflag;
3040     			}
3041     		}
3042     	}
3043     	restore_flags(flags);
3044     
3045     	head = portp->tx.head;
3046     	tail = portp->tx.tail;
3047     	portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
3048     		(STL_TXBUFSIZE - (tail - head)));
3049     
3050     	portp->stats.signals = (unsigned long) stl_getsignals(portp);
3051     
3052     	copy_to_user(cp, &portp->stats, sizeof(comstats_t));
3053     	return(0);
3054     }
3055     
3056     /*****************************************************************************/
3057     
3058     /*
3059      *	Clear the port stats structure. We also return it zeroed out...
3060      */
3061     
3062     static int stl_clrportstats(stlport_t *portp, comstats_t *cp)
3063     {
3064     	if (portp == (stlport_t *) NULL) {
3065     		copy_from_user(&stl_comstats, cp, sizeof(comstats_t));
3066     		portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3067     			stl_comstats.port);
3068     		if (portp == (stlport_t *) NULL)
3069     			return(-ENODEV);
3070     	}
3071     
3072     	memset(&portp->stats, 0, sizeof(comstats_t));
3073     	portp->stats.brd = portp->brdnr;
3074     	portp->stats.panel = portp->panelnr;
3075     	portp->stats.port = portp->portnr;
3076     	copy_to_user(cp, &portp->stats, sizeof(comstats_t));
3077     	return(0);
3078     }
3079     
3080     /*****************************************************************************/
3081     
3082     /*
3083      *	Return the entire driver ports structure to a user app.
3084      */
3085     
3086     static int stl_getportstruct(unsigned long arg)
3087     {
3088     	stlport_t	*portp;
3089     
3090     	copy_from_user(&stl_dummyport, (void *) arg, sizeof(stlport_t));
3091     	portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
3092     		 stl_dummyport.portnr);
3093     	if (portp == (stlport_t *) NULL)
3094     		return(-ENODEV);
3095     	copy_to_user((void *) arg, portp, sizeof(stlport_t));
3096     	return(0);
3097     }
3098     
3099     /*****************************************************************************/
3100     
3101     /*
3102      *	Return the entire driver board structure to a user app.
3103      */
3104     
3105     static int stl_getbrdstruct(unsigned long arg)
3106     {
3107     	stlbrd_t	*brdp;
3108     
3109     	copy_from_user(&stl_dummybrd, (void *) arg, sizeof(stlbrd_t));
3110     	if ((stl_dummybrd.brdnr < 0) || (stl_dummybrd.brdnr >= STL_MAXBRDS))
3111     		return(-ENODEV);
3112     	brdp = stl_brds[stl_dummybrd.brdnr];
3113     	if (brdp == (stlbrd_t *) NULL)
3114     		return(-ENODEV);
3115     	copy_to_user((void *) arg, brdp, sizeof(stlbrd_t));
3116     	return(0);
3117     }
3118     
3119     /*****************************************************************************/
3120     
3121     /*
3122      *	The "staliomem" device is also required to do some special operations
3123      *	on the board and/or ports. In this driver it is mostly used for stats
3124      *	collection.
3125      */
3126     
3127     static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
3128     {
3129     	int	brdnr, rc;
3130     
3131     #if DEBUG
3132     	printk("stl_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip,
3133     		(int) fp, cmd, (int) arg);
3134     #endif
3135     
3136     	brdnr = MINOR(ip->i_rdev);
3137     	if (brdnr >= STL_MAXBRDS)
3138     		return(-ENODEV);
3139     	rc = 0;
3140     
3141     	switch (cmd) {
3142     	case COM_GETPORTSTATS:
3143     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3144     		    sizeof(comstats_t))) == 0)
3145     			rc = stl_getportstats((stlport_t *) NULL,
3146     				(comstats_t *) arg);
3147     		break;
3148     	case COM_CLRPORTSTATS:
3149     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3150     		    sizeof(comstats_t))) == 0)
3151     			rc = stl_clrportstats((stlport_t *) NULL,
3152     				(comstats_t *) arg);
3153     		break;
3154     	case COM_GETBRDSTATS:
3155     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3156     		    sizeof(combrd_t))) == 0)
3157     			rc = stl_getbrdstats((combrd_t *) arg);
3158     		break;
3159     	case COM_READPORT:
3160     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3161     		    sizeof(stlport_t))) == 0)
3162     			rc = stl_getportstruct(arg);
3163     		break;
3164     	case COM_READBOARD:
3165     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
3166     		    sizeof(stlbrd_t))) == 0)
3167     			rc = stl_getbrdstruct(arg);
3168     		break;
3169     	default:
3170     		rc = -ENOIOCTLCMD;
3171     		break;
3172     	}
3173     
3174     	return(rc);
3175     }
3176     
3177     /*****************************************************************************/
3178     
3179     int __init stl_init(void)
3180     {
3181     	printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
3182     
3183     	stl_initbrds();
3184     
3185     /*
3186      *	Allocate a temporary write buffer.
3187      */
3188     	stl_tmpwritebuf = (char *) stl_memalloc(STL_TXBUFSIZE);
3189     	if (stl_tmpwritebuf == (char *) NULL)
3190     		printk("STALLION: failed to allocate memory (size=%d)\n",
3191     			STL_TXBUFSIZE);
3192     
3193     /*
3194      *	Set up a character driver for per board stuff. This is mainly used
3195      *	to do stats ioctls on the ports.
3196      */
3197     	if (devfs_register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
3198     		printk("STALLION: failed to register serial board device\n");
3199     	devfs_handle = devfs_mk_dir (NULL, "staliomem", NULL);
3200     	devfs_register_series (devfs_handle, "%u", 4, DEVFS_FL_DEFAULT,
3201     			       STL_SIOMEMMAJOR, 0,
3202     			       S_IFCHR | S_IRUSR | S_IWUSR,
3203     			       &stl_fsiomem, NULL);
3204     
3205     /*
3206      *	Set up the tty driver structure and register us as a driver.
3207      *	Also setup the callout tty device.
3208      */
3209     	memset(&stl_serial, 0, sizeof(struct tty_driver));
3210     	stl_serial.magic = TTY_DRIVER_MAGIC;
3211     	stl_serial.driver_name = stl_drvname;
3212     	stl_serial.name = stl_serialname;
3213     	stl_serial.major = STL_SERIALMAJOR;
3214     	stl_serial.minor_start = 0;
3215     	stl_serial.num = STL_MAXBRDS * STL_MAXPORTS;
3216     	stl_serial.type = TTY_DRIVER_TYPE_SERIAL;
3217     	stl_serial.subtype = STL_DRVTYPSERIAL;
3218     	stl_serial.init_termios = stl_deftermios;
3219     	stl_serial.flags = TTY_DRIVER_REAL_RAW;
3220     	stl_serial.refcount = &stl_refcount;
3221     	stl_serial.table = stl_ttys;
3222     	stl_serial.termios = stl_termios;
3223     	stl_serial.termios_locked = stl_termioslocked;
3224     	
3225     	stl_serial.open = stl_open;
3226     	stl_serial.close = stl_close;
3227     	stl_serial.write = stl_write;
3228     	stl_serial.put_char = stl_putchar;
3229     	stl_serial.flush_chars = stl_flushchars;
3230     	stl_serial.write_room = stl_writeroom;
3231     	stl_serial.chars_in_buffer = stl_charsinbuffer;
3232     	stl_serial.ioctl = stl_ioctl;
3233     	stl_serial.set_termios = stl_settermios;
3234     	stl_serial.throttle = stl_throttle;
3235     	stl_serial.unthrottle = stl_unthrottle;
3236     	stl_serial.stop = stl_stop;
3237     	stl_serial.start = stl_start;
3238     	stl_serial.hangup = stl_hangup;
3239     	stl_serial.flush_buffer = stl_flushbuffer;
3240     	stl_serial.break_ctl = stl_breakctl;
3241     	stl_serial.wait_until_sent = stl_waituntilsent;
3242     	stl_serial.send_xchar = stl_sendxchar;
3243     	stl_serial.read_proc = stl_readproc;
3244     
3245     	stl_callout = stl_serial;
3246     	stl_callout.name = stl_calloutname;
3247     	stl_callout.major = STL_CALLOUTMAJOR;
3248     	stl_callout.subtype = STL_DRVTYPCALLOUT;
3249     	stl_callout.read_proc = 0;
3250     
3251     	if (tty_register_driver(&stl_serial))
3252     		printk("STALLION: failed to register serial driver\n");
3253     	if (tty_register_driver(&stl_callout))
3254     		printk("STALLION: failed to register callout driver\n");
3255     
3256     	return(0);
3257     }
3258     
3259     /*****************************************************************************/
3260     /*                       CD1400 HARDWARE FUNCTIONS                           */
3261     /*****************************************************************************/
3262     
3263     /*
3264      *	These functions get/set/update the registers of the cd1400 UARTs.
3265      *	Access to the cd1400 registers is via an address/data io port pair.
3266      *	(Maybe should make this inline...)
3267      */
3268     
3269     static int stl_cd1400getreg(stlport_t *portp, int regnr)
3270     {
3271     	outb((regnr + portp->uartaddr), portp->ioaddr);
3272     	return(inb(portp->ioaddr + EREG_DATA));
3273     }
3274     
3275     static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
3276     {
3277     	outb((regnr + portp->uartaddr), portp->ioaddr);
3278     	outb(value, portp->ioaddr + EREG_DATA);
3279     }
3280     
3281     static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
3282     {
3283     	outb((regnr + portp->uartaddr), portp->ioaddr);
3284     	if (inb(portp->ioaddr + EREG_DATA) != value) {
3285     		outb(value, portp->ioaddr + EREG_DATA);
3286     		return(1);
3287     	}
3288     	return(0);
3289     }
3290     
3291     /*****************************************************************************/
3292     
3293     /*
3294      *	Inbitialize the UARTs in a panel. We don't care what sort of board
3295      *	these ports are on - since the port io registers are almost
3296      *	identical when dealing with ports.
3297      */
3298     
3299     static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3300     {
3301     	unsigned int	gfrcr;
3302     	int		chipmask, i, j;
3303     	int		nrchips, uartaddr, ioaddr;
3304     
3305     #if DEBUG
3306     	printk("stl_panelinit(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
3307     #endif
3308     
3309     	BRDENABLE(panelp->brdnr, panelp->pagenr);
3310     
3311     /*
3312      *	Check that each chip is present and started up OK.
3313      */
3314     	chipmask = 0;
3315     	nrchips = panelp->nrports / CD1400_PORTS;
3316     	for (i = 0; (i < nrchips); i++) {
3317     		if (brdp->brdtype == BRD_ECHPCI) {
3318     			outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3319     			ioaddr = panelp->iobase;
3320     		} else {
3321     			ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3322     		}
3323     		uartaddr = (i & 0x01) ? 0x080 : 0;
3324     		outb((GFRCR + uartaddr), ioaddr);
3325     		outb(0, (ioaddr + EREG_DATA));
3326     		outb((CCR + uartaddr), ioaddr);
3327     		outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3328     		outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3329     		outb((GFRCR + uartaddr), ioaddr);
3330     		for (j = 0; (j < CCR_MAXWAIT); j++) {
3331     			if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3332     				break;
3333     		}
3334     		if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3335     			printk("STALLION: cd1400 not responding, "
3336     				"brd=%d panel=%d chip=%d\n",
3337     				panelp->brdnr, panelp->panelnr, i);
3338     			continue;
3339     		}
3340     		chipmask |= (0x1 << i);
3341     		outb((PPR + uartaddr), ioaddr);
3342     		outb(PPR_SCALAR, (ioaddr + EREG_DATA));
3343     	}
3344     
3345     	BRDDISABLE(panelp->brdnr);
3346     	return(chipmask);
3347     }
3348     
3349     /*****************************************************************************/
3350     
3351     /*
3352      *	Initialize hardware specific port registers.
3353      */
3354     
3355     static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3356     {
3357     #if DEBUG
3358     	printk("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3359     		(int) brdp, (int) panelp, (int) portp);
3360     #endif
3361     
3362     	if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
3363     	    (portp == (stlport_t *) NULL))
3364     		return;
3365     
3366     	portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3367     		(portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3368     	portp->uartaddr = (portp->portnr & 0x04) << 5;
3369     	portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3370     
3371     	BRDENABLE(portp->brdnr, portp->pagenr);
3372     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3373     	stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3374     	portp->hwid = stl_cd1400getreg(portp, GFRCR);
3375     	BRDDISABLE(portp->brdnr);
3376     }
3377     
3378     /*****************************************************************************/
3379     
3380     /*
3381      *	Wait for the command register to be ready. We will poll this,
3382      *	since it won't usually take too long to be ready.
3383      */
3384     
3385     static void stl_cd1400ccrwait(stlport_t *portp)
3386     {
3387     	int	i;
3388     
3389     	for (i = 0; (i < CCR_MAXWAIT); i++) {
3390     		if (stl_cd1400getreg(portp, CCR) == 0) {
3391     			return;
3392     		}
3393     	}
3394     
3395     	printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
3396     		portp->portnr, portp->panelnr, portp->brdnr);
3397     }
3398     
3399     /*****************************************************************************/
3400     
3401     /*
3402      *	Set up the cd1400 registers for a port based on the termios port
3403      *	settings.
3404      */
3405     
3406     static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
3407     {
3408     	stlbrd_t	*brdp;
3409     	unsigned long	flags;
3410     	unsigned int	clkdiv, baudrate;
3411     	unsigned char	cor1, cor2, cor3;
3412     	unsigned char	cor4, cor5, ccr;
3413     	unsigned char	srer, sreron, sreroff;
3414     	unsigned char	mcor1, mcor2, rtpr;
3415     	unsigned char	clk, div;
3416     
3417     	cor1 = 0;
3418     	cor2 = 0;
3419     	cor3 = 0;
3420     	cor4 = 0;
3421     	cor5 = 0;
3422     	ccr = 0;
3423     	rtpr = 0;
3424     	clk = 0;
3425     	div = 0;
3426     	mcor1 = 0;
3427     	mcor2 = 0;
3428     	sreron = 0;
3429     	sreroff = 0;
3430     
3431     	brdp = stl_brds[portp->brdnr];
3432     	if (brdp == (stlbrd_t *) NULL)
3433     		return;
3434     
3435     /*
3436      *	Set up the RX char ignore mask with those RX error types we
3437      *	can ignore. We can get the cd1400 to help us out a little here,
3438      *	it will ignore parity errors and breaks for us.
3439      */
3440     	portp->rxignoremsk = 0;
3441     	if (tiosp->c_iflag & IGNPAR) {
3442     		portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
3443     		cor1 |= COR1_PARIGNORE;
3444     	}
3445     	if (tiosp->c_iflag & IGNBRK) {
3446     		portp->rxignoremsk |= ST_BREAK;
3447     		cor4 |= COR4_IGNBRK;
3448     	}
3449     
3450     	portp->rxmarkmsk = ST_OVERRUN;
3451     	if (tiosp->c_iflag & (INPCK | PARMRK))
3452     		portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
3453     	if (tiosp->c_iflag & BRKINT)
3454     		portp->rxmarkmsk |= ST_BREAK;
3455     
3456     /*
3457      *	Go through the char size, parity and stop bits and set all the
3458      *	option register appropriately.
3459      */
3460     	switch (tiosp->c_cflag & CSIZE) {
3461     	case CS5:
3462     		cor1 |= COR1_CHL5;
3463     		break;
3464     	case CS6:
3465     		cor1 |= COR1_CHL6;
3466     		break;
3467     	case CS7:
3468     		cor1 |= COR1_CHL7;
3469     		break;
3470     	default:
3471     		cor1 |= COR1_CHL8;
3472     		break;
3473     	}
3474     
3475     	if (tiosp->c_cflag & CSTOPB)
3476     		cor1 |= COR1_STOP2;
3477     	else
3478     		cor1 |= COR1_STOP1;
3479     
3480     	if (tiosp->c_cflag & PARENB) {
3481     		if (tiosp->c_cflag & PARODD)
3482     			cor1 |= (COR1_PARENB | COR1_PARODD);
3483     		else
3484     			cor1 |= (COR1_PARENB | COR1_PAREVEN);
3485     	} else {
3486     		cor1 |= COR1_PARNONE;
3487     	}
3488     
3489     /*
3490      *	Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
3491      *	space for hardware flow control and the like. This should be set to
3492      *	VMIN. Also here we will set the RX data timeout to 10ms - this should
3493      *	really be based on VTIME.
3494      */
3495     	cor3 |= FIFO_RXTHRESHOLD;
3496     	rtpr = 2;
3497     
3498     /*
3499      *	Calculate the baud rate timers. For now we will just assume that
3500      *	the input and output baud are the same. Could have used a baud
3501      *	table here, but this way we can generate virtually any baud rate
3502      *	we like!
3503      */
3504     	baudrate = tiosp->c_cflag & CBAUD;
3505     	if (baudrate & CBAUDEX) {
3506     		baudrate &= ~CBAUDEX;
3507     		if ((baudrate < 1) || (baudrate > 4))
3508     			tiosp->c_cflag &= ~CBAUDEX;
3509     		else
3510     			baudrate += 15;
3511     	}
3512     	baudrate = stl_baudrates[baudrate];
3513     	if ((tiosp->c_cflag & CBAUD) == B38400) {
3514     		if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3515     			baudrate = 57600;
3516     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3517     			baudrate = 115200;
3518     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3519     			baudrate = 230400;
3520     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3521     			baudrate = 460800;
3522     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3523     			baudrate = (portp->baud_base / portp->custom_divisor);
3524     	}
3525     	if (baudrate > STL_CD1400MAXBAUD)
3526     		baudrate = STL_CD1400MAXBAUD;
3527     
3528     	if (baudrate > 0) {
3529     		for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3530     			clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) / baudrate);
3531     			if (clkdiv < 0x100)
3532     				break;
3533     		}
3534     		div = (unsigned char) clkdiv;
3535     	}
3536     
3537     /*
3538      *	Check what form of modem signaling is required and set it up.
3539      */
3540     	if ((tiosp->c_cflag & CLOCAL) == 0) {
3541     		mcor1 |= MCOR1_DCD;
3542     		mcor2 |= MCOR2_DCD;
3543     		sreron |= SRER_MODEM;
3544     		portp->flags |= ASYNC_CHECK_CD;
3545     	} else {
3546     		portp->flags &= ~ASYNC_CHECK_CD;
3547     	}
3548     
3549     /*
3550      *	Setup cd1400 enhanced modes if we can. In particular we want to
3551      *	handle as much of the flow control as possible automatically. As
3552      *	well as saving a few CPU cycles it will also greatly improve flow
3553      *	control reliability.
3554      */
3555     	if (tiosp->c_iflag & IXON) {
3556     		cor2 |= COR2_TXIBE;
3557     		cor3 |= COR3_SCD12;
3558     		if (tiosp->c_iflag & IXANY)
3559     			cor2 |= COR2_IXM;
3560     	}
3561     
3562     	if (tiosp->c_cflag & CRTSCTS) {
3563     		cor2 |= COR2_CTSAE;
3564     		mcor1 |= FIFO_RTSTHRESHOLD;
3565     	}
3566     
3567     /*
3568      *	All cd1400 register values calculated so go through and set
3569      *	them all up.
3570      */
3571     
3572     #if DEBUG
3573     	printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3574     		portp->portnr, portp->panelnr, portp->brdnr);
3575     	printk("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
3576     		cor1, cor2, cor3, cor4, cor5);
3577     	printk("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3578     		mcor1, mcor2, rtpr, sreron, sreroff);
3579     	printk("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3580     	printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3581     		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3582     		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3583     #endif
3584     
3585     	save_flags(flags);
3586     	cli();
3587     	BRDENABLE(portp->brdnr, portp->pagenr);
3588     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3589     	srer = stl_cd1400getreg(portp, SRER);
3590     	stl_cd1400setreg(portp, SRER, 0);
3591     	if (stl_cd1400updatereg(portp, COR1, cor1))
3592     		ccr = 1;
3593     	if (stl_cd1400updatereg(portp, COR2, cor2))
3594     		ccr = 1;
3595     	if (stl_cd1400updatereg(portp, COR3, cor3))
3596     		ccr = 1;
3597     	if (ccr) {
3598     		stl_cd1400ccrwait(portp);
3599     		stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3600     	}
3601     	stl_cd1400setreg(portp, COR4, cor4);
3602     	stl_cd1400setreg(portp, COR5, cor5);
3603     	stl_cd1400setreg(portp, MCOR1, mcor1);
3604     	stl_cd1400setreg(portp, MCOR2, mcor2);
3605     	if (baudrate > 0) {
3606     		stl_cd1400setreg(portp, TCOR, clk);
3607     		stl_cd1400setreg(portp, TBPR, div);
3608     		stl_cd1400setreg(portp, RCOR, clk);
3609     		stl_cd1400setreg(portp, RBPR, div);
3610     	}
3611     	stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3612     	stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3613     	stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3614     	stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3615     	stl_cd1400setreg(portp, RTPR, rtpr);
3616     	mcor1 = stl_cd1400getreg(portp, MSVR1);
3617     	if (mcor1 & MSVR1_DCD)
3618     		portp->sigs |= TIOCM_CD;
3619     	else
3620     		portp->sigs &= ~TIOCM_CD;
3621     	stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3622     	BRDDISABLE(portp->brdnr);
3623     	restore_flags(flags);
3624     }
3625     
3626     /*****************************************************************************/
3627     
3628     /*
3629      *	Set the state of the DTR and RTS signals.
3630      */
3631     
3632     static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3633     {
3634     	unsigned char	msvr1, msvr2;
3635     	unsigned long	flags;
3636     
3637     #if DEBUG
3638     	printk("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n",
3639     		(int) portp, dtr, rts);
3640     #endif
3641     
3642     	msvr1 = 0;
3643     	msvr2 = 0;
3644     	if (dtr > 0)
3645     		msvr1 = MSVR1_DTR;
3646     	if (rts > 0)
3647     		msvr2 = MSVR2_RTS;
3648     
3649     	save_flags(flags);
3650     	cli();
3651     	BRDENABLE(portp->brdnr, portp->pagenr);
3652     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3653     	if (rts >= 0)
3654     		stl_cd1400setreg(portp, MSVR2, msvr2);
3655     	if (dtr >= 0)
3656     		stl_cd1400setreg(portp, MSVR1, msvr1);
3657     	BRDDISABLE(portp->brdnr);
3658     	restore_flags(flags);
3659     }
3660     
3661     /*****************************************************************************/
3662     
3663     /*
3664      *	Return the state of the signals.
3665      */
3666     
3667     static int stl_cd1400getsignals(stlport_t *portp)
3668     {
3669     	unsigned char	msvr1, msvr2;
3670     	unsigned long	flags;
3671     	int		sigs;
3672     
3673     #if DEBUG
3674     	printk("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3675     #endif
3676     
3677     	save_flags(flags);
3678     	cli();
3679     	BRDENABLE(portp->brdnr, portp->pagenr);
3680     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3681     	msvr1 = stl_cd1400getreg(portp, MSVR1);
3682     	msvr2 = stl_cd1400getreg(portp, MSVR2);
3683     	BRDDISABLE(portp->brdnr);
3684     	restore_flags(flags);
3685     
3686     	sigs = 0;
3687     	sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3688     	sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3689     	sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3690     	sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3691     #if 0
3692     	sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3693     	sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3694     #else
3695     	sigs |= TIOCM_DSR;
3696     #endif
3697     	return(sigs);
3698     }
3699     
3700     /*****************************************************************************/
3701     
3702     /*
3703      *	Enable/Disable the Transmitter and/or Receiver.
3704      */
3705     
3706     static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3707     {
3708     	unsigned char	ccr;
3709     	unsigned long	flags;
3710     
3711     #if DEBUG
3712     	printk("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3713     		(int) portp, rx, tx);
3714     #endif
3715     	ccr = 0;
3716     
3717     	if (tx == 0)
3718     		ccr |= CCR_TXDISABLE;
3719     	else if (tx > 0)
3720     		ccr |= CCR_TXENABLE;
3721     	if (rx == 0)
3722     		ccr |= CCR_RXDISABLE;
3723     	else if (rx > 0)
3724     		ccr |= CCR_RXENABLE;
3725     
3726     	save_flags(flags);
3727     	cli();
3728     	BRDENABLE(portp->brdnr, portp->pagenr);
3729     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3730     	stl_cd1400ccrwait(portp);
3731     	stl_cd1400setreg(portp, CCR, ccr);
3732     	stl_cd1400ccrwait(portp);
3733     	BRDDISABLE(portp->brdnr);
3734     	restore_flags(flags);
3735     }
3736     
3737     /*****************************************************************************/
3738     
3739     /*
3740      *	Start/stop the Transmitter and/or Receiver.
3741      */
3742     
3743     static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3744     {
3745     	unsigned char	sreron, sreroff;
3746     	unsigned long	flags;
3747     
3748     #if DEBUG
3749     	printk("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3750     		(int) portp, rx, tx);
3751     #endif
3752     
3753     	sreron = 0;
3754     	sreroff = 0;
3755     	if (tx == 0)
3756     		sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3757     	else if (tx == 1)
3758     		sreron |= SRER_TXDATA;
3759     	else if (tx >= 2)
3760     		sreron |= SRER_TXEMPTY;
3761     	if (rx == 0)
3762     		sreroff |= SRER_RXDATA;
3763     	else if (rx > 0)
3764     		sreron |= SRER_RXDATA;
3765     
3766     	save_flags(flags);
3767     	cli();
3768     	BRDENABLE(portp->brdnr, portp->pagenr);
3769     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3770     	stl_cd1400setreg(portp, SRER,
3771     		((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3772     	BRDDISABLE(portp->brdnr);
3773     	if (tx > 0)
3774     		set_bit(ASYI_TXBUSY, &portp->istate);
3775     	restore_flags(flags);
3776     }
3777     
3778     /*****************************************************************************/
3779     
3780     /*
3781      *	Disable all interrupts from this port.
3782      */
3783     
3784     static void stl_cd1400disableintrs(stlport_t *portp)
3785     {
3786     	unsigned long	flags;
3787     
3788     #if DEBUG
3789     	printk("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3790     #endif
3791     	save_flags(flags);
3792     	cli();
3793     	BRDENABLE(portp->brdnr, portp->pagenr);
3794     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3795     	stl_cd1400setreg(portp, SRER, 0);
3796     	BRDDISABLE(portp->brdnr);
3797     	restore_flags(flags);
3798     }
3799     
3800     /*****************************************************************************/
3801     
3802     static void stl_cd1400sendbreak(stlport_t *portp, int len)
3803     {
3804     	unsigned long	flags;
3805     
3806     #if DEBUG
3807     	printk("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp, len);
3808     #endif
3809     
3810     	save_flags(flags);
3811     	cli();
3812     	BRDENABLE(portp->brdnr, portp->pagenr);
3813     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3814     	stl_cd1400setreg(portp, SRER,
3815     		((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3816     		SRER_TXEMPTY));
3817     	BRDDISABLE(portp->brdnr);
3818     	portp->brklen = len;
3819     	if (len == 1)
3820     		portp->stats.txbreaks++;
3821     	restore_flags(flags);
3822     }
3823     
3824     /*****************************************************************************/
3825     
3826     /*
3827      *	Take flow control actions...
3828      */
3829     
3830     static void stl_cd1400flowctrl(stlport_t *portp, int state)
3831     {
3832     	struct tty_struct	*tty;
3833     	unsigned long		flags;
3834     
3835     #if DEBUG
3836     	printk("stl_cd1400flowctrl(portp=%x,state=%x)\n", (int) portp, state);
3837     #endif
3838     
3839     	if (portp == (stlport_t *) NULL)
3840     		return;
3841     	tty = portp->tty;
3842     	if (tty == (struct tty_struct *) NULL)
3843     		return;
3844     
3845     	save_flags(flags);
3846     	cli();
3847     	BRDENABLE(portp->brdnr, portp->pagenr);
3848     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3849     
3850     	if (state) {
3851     		if (tty->termios->c_iflag & IXOFF) {
3852     			stl_cd1400ccrwait(portp);
3853     			stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3854     			portp->stats.rxxon++;
3855     			stl_cd1400ccrwait(portp);
3856     		}
3857     /*
3858      *		Question: should we return RTS to what it was before? It may
3859      *		have been set by an ioctl... Suppose not, since if you have
3860      *		hardware flow control set then it is pretty silly to go and
3861      *		set the RTS line by hand.
3862      */
3863     		if (tty->termios->c_cflag & CRTSCTS) {
3864     			stl_cd1400setreg(portp, MCOR1,
3865     				(stl_cd1400getreg(portp, MCOR1) |
3866     				FIFO_RTSTHRESHOLD));
3867     			stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3868     			portp->stats.rxrtson++;
3869     		}
3870     	} else {
3871     		if (tty->termios->c_iflag & IXOFF) {
3872     			stl_cd1400ccrwait(portp);
3873     			stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3874     			portp->stats.rxxoff++;
3875     			stl_cd1400ccrwait(portp);
3876     		}
3877     		if (tty->termios->c_cflag & CRTSCTS) {
3878     			stl_cd1400setreg(portp, MCOR1,
3879     				(stl_cd1400getreg(portp, MCOR1) & 0xf0));
3880     			stl_cd1400setreg(portp, MSVR2, 0);
3881     			portp->stats.rxrtsoff++;
3882     		}
3883     	}
3884     
3885     	BRDDISABLE(portp->brdnr);
3886     	restore_flags(flags);
3887     }
3888     
3889     /*****************************************************************************/
3890     
3891     /*
3892      *	Send a flow control character...
3893      */
3894     
3895     static void stl_cd1400sendflow(stlport_t *portp, int state)
3896     {
3897     	struct tty_struct	*tty;
3898     	unsigned long		flags;
3899     
3900     #if DEBUG
3901     	printk("stl_cd1400sendflow(portp=%x,state=%x)\n", (int) portp, state);
3902     #endif
3903     
3904     	if (portp == (stlport_t *) NULL)
3905     		return;
3906     	tty = portp->tty;
3907     	if (tty == (struct tty_struct *) NULL)
3908     		return;
3909     
3910     	save_flags(flags);
3911     	cli();
3912     	BRDENABLE(portp->brdnr, portp->pagenr);
3913     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3914     	if (state) {
3915     		stl_cd1400ccrwait(portp);
3916     		stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3917     		portp->stats.rxxon++;
3918     		stl_cd1400ccrwait(portp);
3919     	} else {
3920     		stl_cd1400ccrwait(portp);
3921     		stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3922     		portp->stats.rxxoff++;
3923     		stl_cd1400ccrwait(portp);
3924     	}
3925     	BRDDISABLE(portp->brdnr);
3926     	restore_flags(flags);
3927     }
3928     
3929     /*****************************************************************************/
3930     
3931     static void stl_cd1400flush(stlport_t *portp)
3932     {
3933     	unsigned long	flags;
3934     
3935     #if DEBUG
3936     	printk("stl_cd1400flush(portp=%x)\n", (int) portp);
3937     #endif
3938     
3939     	if (portp == (stlport_t *) NULL)
3940     		return;
3941     
3942     	save_flags(flags);
3943     	cli();
3944     	BRDENABLE(portp->brdnr, portp->pagenr);
3945     	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3946     	stl_cd1400ccrwait(portp);
3947     	stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3948     	stl_cd1400ccrwait(portp);
3949     	portp->tx.tail = portp->tx.head;
3950     	BRDDISABLE(portp->brdnr);
3951     	restore_flags(flags);
3952     }
3953     
3954     /*****************************************************************************/
3955     
3956     /*
3957      *	Return the current state of data flow on this port. This is only
3958      *	really interresting when determining if data has fully completed
3959      *	transmission or not... This is easy for the cd1400, it accurately
3960      *	maintains the busy port flag.
3961      */
3962     
3963     static int stl_cd1400datastate(stlport_t *portp)
3964     {
3965     #if DEBUG
3966     	printk("stl_cd1400datastate(portp=%x)\n", (int) portp);
3967     #endif
3968     
3969     	if (portp == (stlport_t *) NULL)
3970     		return(0);
3971     
3972     	return(test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0);
3973     }
3974     
3975     /*****************************************************************************/
3976     
3977     /*
3978      *	Interrupt service routine for cd1400 EasyIO boards.
3979      */
3980     
3981     static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
3982     {
3983     	unsigned char	svrtype;
3984     
3985     #if DEBUG
3986     	printk("stl_cd1400eiointr(panelp=%x,iobase=%x)\n",
3987     		(int) panelp, iobase);
3988     #endif
3989     
3990     	outb(SVRR, iobase);
3991     	svrtype = inb(iobase + EREG_DATA);
3992     	if (panelp->nrports > 4) {
3993     		outb((SVRR + 0x80), iobase);
3994     		svrtype |= inb(iobase + EREG_DATA);
3995     	}
3996     
3997     	if (svrtype & SVRR_RX)
3998     		stl_cd1400rxisr(panelp, iobase);
3999     	else if (svrtype & SVRR_TX)
4000     		stl_cd1400txisr(panelp, iobase);
4001     	else if (svrtype & SVRR_MDM)
4002     		stl_cd1400mdmisr(panelp, iobase);
4003     }
4004     
4005     /*****************************************************************************/
4006     
4007     /*
4008      *	Interrupt service routine for cd1400 panels.
4009      */
4010     
4011     static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
4012     {
4013     	unsigned char	svrtype;
4014     
4015     #if DEBUG
4016     	printk("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
4017     		iobase);
4018     #endif
4019     
4020     	outb(SVRR, iobase);
4021     	svrtype = inb(iobase + EREG_DATA);
4022     	outb((SVRR + 0x80), iobase);
4023     	svrtype |= inb(iobase + EREG_DATA);
4024     	if (svrtype & SVRR_RX)
4025     		stl_cd1400rxisr(panelp, iobase);
4026     	else if (svrtype & SVRR_TX)
4027     		stl_cd1400txisr(panelp, iobase);
4028     	else if (svrtype & SVRR_MDM)
4029     		stl_cd1400mdmisr(panelp, iobase);
4030     }
4031     
4032     
4033     /*****************************************************************************/
4034     
4035     /*
4036      *	Unfortunately we need to handle breaks in the TX data stream, since
4037      *	this is the only way to generate them on the cd1400.
4038      */
4039     
4040     static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr)
4041     {
4042     	if (portp->brklen == 1) {
4043     		outb((COR2 + portp->uartaddr), ioaddr);
4044     		outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
4045     			(ioaddr + EREG_DATA));
4046     		outb((TDR + portp->uartaddr), ioaddr);
4047     		outb(ETC_CMD, (ioaddr + EREG_DATA));
4048     		outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
4049     		outb((SRER + portp->uartaddr), ioaddr);
4050     		outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
4051     			(ioaddr + EREG_DATA));
4052     		return(1);
4053     	} else if (portp->brklen > 1) {
4054     		outb((TDR + portp->uartaddr), ioaddr);
4055     		outb(ETC_CMD, (ioaddr + EREG_DATA));
4056     		outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
4057     		portp->brklen = -1;
4058     		return(1);
4059     	} else {
4060     		outb((COR2 + portp->uartaddr), ioaddr);
4061     		outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
4062     			(ioaddr + EREG_DATA));
4063     		portp->brklen = 0;
4064     	}
4065     	return(0);
4066     }
4067     
4068     /*****************************************************************************/
4069     
4070     /*
4071      *	Transmit interrupt handler. This has gotta be fast!  Handling TX
4072      *	chars is pretty simple, stuff as many as possible from the TX buffer
4073      *	into the cd1400 FIFO. Must also handle TX breaks here, since they
4074      *	are embedded as commands in the data stream. Oh no, had to use a goto!
4075      *	This could be optimized more, will do when I get time...
4076      *	In practice it is possible that interrupts are enabled but that the
4077      *	port has been hung up. Need to handle not having any TX buffer here,
4078      *	this is done by using the side effect that head and tail will also
4079      *	be NULL if the buffer has been freed.
4080      */
4081     
4082     static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
4083     {
4084     	stlport_t	*portp;
4085     	int		len, stlen;
4086     	char		*head, *tail;
4087     	unsigned char	ioack, srer;
4088     
4089     #if DEBUG
4090     	printk("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
4091     #endif
4092     
4093     	ioack = inb(ioaddr + EREG_TXACK);
4094     	if (((ioack & panelp->ackmask) != 0) ||
4095     	    ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
4096     		printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
4097     		return;
4098     	}
4099     	portp = panelp->ports[(ioack >> 3)];
4100     
4101     /*
4102      *	Unfortunately we need to handle breaks in the data stream, since
4103      *	this is the only way to generate them on the cd1400. Do it now if
4104      *	a break is to be sent.
4105      */
4106     	if (portp->brklen != 0)
4107     		if (stl_cd1400breakisr(portp, ioaddr))
4108     			goto stl_txalldone;
4109     
4110     	head = portp->tx.head;
4111     	tail = portp->tx.tail;
4112     	len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4113     	if ((len == 0) || ((len < STL_TXBUFLOW) &&
4114     	    (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4115     		set_bit(ASYI_TXLOW, &portp->istate);
4116     		MOD_INC_USE_COUNT;
4117     		if (schedule_task(&portp->tqueue) == 0)
4118     			MOD_DEC_USE_COUNT;
4119     	}
4120     
4121     	if (len == 0) {
4122     		outb((SRER + portp->uartaddr), ioaddr);
4123     		srer = inb(ioaddr + EREG_DATA);
4124     		if (srer & SRER_TXDATA) {
4125     			srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
4126     		} else {
4127     			srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
4128     			clear_bit(ASYI_TXBUSY, &portp->istate);
4129     		}
4130     		outb(srer, (ioaddr + EREG_DATA));
4131     	} else {
4132     		len = MIN(len, CD1400_TXFIFOSIZE);
4133     		portp->stats.txtotal += len;
4134     		stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
4135     		outb((TDR + portp->uartaddr), ioaddr);
4136     		outsb((ioaddr + EREG_DATA), tail, stlen);
4137     		len -= stlen;
4138     		tail += stlen;
4139     		if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4140     			tail = portp->tx.buf;
4141     		if (len > 0) {
4142     			outsb((ioaddr + EREG_DATA), tail, len);
4143     			tail += len;
4144     		}
4145     		portp->tx.tail = tail;
4146     	}
4147     
4148     stl_txalldone:
4149     	outb((EOSRR + portp->uartaddr), ioaddr);
4150     	outb(0, (ioaddr + EREG_DATA));
4151     }
4152     
4153     /*****************************************************************************/
4154     
4155     /*
4156      *	Receive character interrupt handler. Determine if we have good chars
4157      *	or bad chars and then process appropriately. Good chars are easy
4158      *	just shove the lot into the RX buffer and set all status byte to 0.
4159      *	If a bad RX char then process as required. This routine needs to be
4160      *	fast!  In practice it is possible that we get an interrupt on a port
4161      *	that is closed. This can happen on hangups - since they completely
4162      *	shutdown a port not in user context. Need to handle this case.
4163      */
4164     
4165     static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
4166     {
4167     	stlport_t		*portp;
4168     	struct tty_struct	*tty;
4169     	unsigned int		ioack, len, buflen;
4170     	unsigned char		status;
4171     	char			ch;
4172     
4173     #if DEBUG
4174     	printk("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
4175     #endif
4176     
4177     	ioack = inb(ioaddr + EREG_RXACK);
4178     	if ((ioack & panelp->ackmask) != 0) {
4179     		printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4180     		return;
4181     	}
4182     	portp = panelp->ports[(ioack >> 3)];
4183     	tty = portp->tty;
4184     
4185     	if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
4186     		outb((RDCR + portp->uartaddr), ioaddr);
4187     		len = inb(ioaddr + EREG_DATA);
4188     		if ((tty == (struct tty_struct *) NULL) ||
4189     		    (tty->flip.char_buf_ptr == (char *) NULL) ||
4190     		    ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
4191     			len = MIN(len, sizeof(stl_unwanted));
4192     			outb((RDSR + portp->uartaddr), ioaddr);
4193     			insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
4194     			portp->stats.rxlost += len;
4195     			portp->stats.rxtotal += len;
4196     		} else {
4197     			len = MIN(len, buflen);
4198     			if (len > 0) {
4199     				outb((RDSR + portp->uartaddr), ioaddr);
4200     				insb((ioaddr + EREG_DATA), tty->flip.char_buf_ptr, len);
4201     				memset(tty->flip.flag_buf_ptr, 0, len);
4202     				tty->flip.flag_buf_ptr += len;
4203     				tty->flip.char_buf_ptr += len;
4204     				tty->flip.count += len;
4205     				tty_schedule_flip(tty);
4206     				portp->stats.rxtotal += len;
4207     			}
4208     		}
4209     	} else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
4210     		outb((RDSR + portp->uartaddr), ioaddr);
4211     		status = inb(ioaddr + EREG_DATA);
4212     		ch = inb(ioaddr + EREG_DATA);
4213     		if (status & ST_PARITY)
4214     			portp->stats.rxparity++;
4215     		if (status & ST_FRAMING)
4216     			portp->stats.rxframing++;
4217     		if (status & ST_OVERRUN)
4218     			portp->stats.rxoverrun++;
4219     		if (status & ST_BREAK)
4220     			portp->stats.rxbreaks++;
4221     		if (status & ST_SCHARMASK) {
4222     			if ((status & ST_SCHARMASK) == ST_SCHAR1)
4223     				portp->stats.txxon++;
4224     			if ((status & ST_SCHARMASK) == ST_SCHAR2)
4225     				portp->stats.txxoff++;
4226     			goto stl_rxalldone;
4227     		}
4228     		if ((tty != (struct tty_struct *) NULL) &&
4229     		    ((portp->rxignoremsk & status) == 0)) {
4230     			if (portp->rxmarkmsk & status) {
4231     				if (status & ST_BREAK) {
4232     					status = TTY_BREAK;
4233     					if (portp->flags & ASYNC_SAK) {
4234     						do_SAK(tty);
4235     						BRDENABLE(portp->brdnr, portp->pagenr);
4236     					}
4237     				} else if (status & ST_PARITY) {
4238     					status = TTY_PARITY;
4239     				} else if (status & ST_FRAMING) {
4240     					status = TTY_FRAME;
4241     				} else if(status & ST_OVERRUN) {
4242     					status = TTY_OVERRUN;
4243     				} else {
4244     					status = 0;
4245     				}
4246     			} else {
4247     				status = 0;
4248     			}
4249     			if (tty->flip.char_buf_ptr != (char *) NULL) {
4250     				if (tty->flip.count < TTY_FLIPBUF_SIZE) {
4251     					*tty->flip.flag_buf_ptr++ = status;
4252     					*tty->flip.char_buf_ptr++ = ch;
4253     					tty->flip.count++;
4254     				}
4255     				tty_schedule_flip(tty);
4256     			}
4257     		}
4258     	} else {
4259     		printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4260     		return;
4261     	}
4262     
4263     stl_rxalldone:
4264     	outb((EOSRR + portp->uartaddr), ioaddr);
4265     	outb(0, (ioaddr + EREG_DATA));
4266     }
4267     
4268     /*****************************************************************************/
4269     
4270     /*
4271      *	Modem interrupt handler. The is called when the modem signal line
4272      *	(DCD) has changed state. Leave most of the work to the off-level
4273      *	processing routine.
4274      */
4275     
4276     static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
4277     {
4278     	stlport_t	*portp;
4279     	unsigned int	ioack;
4280     	unsigned char	misr;
4281     
4282     #if DEBUG
4283     	printk("stl_cd1400mdmisr(panelp=%x)\n", (int) panelp);
4284     #endif
4285     
4286     	ioack = inb(ioaddr + EREG_MDACK);
4287     	if (((ioack & panelp->ackmask) != 0) ||
4288     	    ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
4289     		printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
4290     		return;
4291     	}
4292     	portp = panelp->ports[(ioack >> 3)];
4293     
4294     	outb((MISR + portp->uartaddr), ioaddr);
4295     	misr = inb(ioaddr + EREG_DATA);
4296     	if (misr & MISR_DCD) {
4297     		set_bit(ASYI_DCDCHANGE, &portp->istate);
4298     		MOD_INC_USE_COUNT;
4299     		if (schedule_task(&portp->tqueue) == 0)
4300     			MOD_DEC_USE_COUNT;
4301     		portp->stats.modem++;
4302     	}
4303     
4304     	outb((EOSRR + portp->uartaddr), ioaddr);
4305     	outb(0, (ioaddr + EREG_DATA));
4306     }
4307     
4308     /*****************************************************************************/
4309     /*                      SC26198 HARDWARE FUNCTIONS                           */
4310     /*****************************************************************************/
4311     
4312     /*
4313      *	These functions get/set/update the registers of the sc26198 UARTs.
4314      *	Access to the sc26198 registers is via an address/data io port pair.
4315      *	(Maybe should make this inline...)
4316      */
4317     
4318     static int stl_sc26198getreg(stlport_t *portp, int regnr)
4319     {
4320     	outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4321     	return(inb(portp->ioaddr + XP_DATA));
4322     }
4323     
4324     static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
4325     {
4326     	outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4327     	outb(value, (portp->ioaddr + XP_DATA));
4328     }
4329     
4330     static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
4331     {
4332     	outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4333     	if (inb(portp->ioaddr + XP_DATA) != value) {
4334     		outb(value, (portp->ioaddr + XP_DATA));
4335     		return(1);
4336     	}
4337     	return(0);
4338     }
4339     
4340     /*****************************************************************************/
4341     
4342     /*
4343      *	Functions to get and set the sc26198 global registers.
4344      */
4345     
4346     static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
4347     {
4348     	outb(regnr, (portp->ioaddr + XP_ADDR));
4349     	return(inb(portp->ioaddr + XP_DATA));
4350     }
4351     
4352     #if 0
4353     static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
4354     {
4355     	outb(regnr, (portp->ioaddr + XP_ADDR));
4356     	outb(value, (portp->ioaddr + XP_DATA));
4357     }
4358     #endif
4359     
4360     /*****************************************************************************/
4361     
4362     /*
4363      *	Inbitialize the UARTs in a panel. We don't care what sort of board
4364      *	these ports are on - since the port io registers are almost
4365      *	identical when dealing with ports.
4366      */
4367     
4368     static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
4369     {
4370     	int	chipmask, i;
4371     	int	nrchips, ioaddr;
4372     
4373     #if DEBUG
4374     	printk("stl_sc26198panelinit(brdp=%x,panelp=%x)\n",
4375     		(int) brdp, (int) panelp);
4376     #endif
4377     
4378     	BRDENABLE(panelp->brdnr, panelp->pagenr);
4379     
4380     /*
4381      *	Check that each chip is present and started up OK.
4382      */
4383     	chipmask = 0;
4384     	nrchips = (panelp->nrports + 4) / SC26198_PORTS;
4385     	if (brdp->brdtype == BRD_ECHPCI)
4386     		outb(panelp->pagenr, brdp->ioctrl);
4387     
4388     	for (i = 0; (i < nrchips); i++) {
4389     		ioaddr = panelp->iobase + (i * 4); 
4390     		outb(SCCR, (ioaddr + XP_ADDR));
4391     		outb(CR_RESETALL, (ioaddr + XP_DATA));
4392     		outb(TSTR, (ioaddr + XP_ADDR));
4393     		if (inb(ioaddr + XP_DATA) != 0) {
4394     			printk("STALLION: sc26198 not responding, "
4395     				"brd=%d panel=%d chip=%d\n",
4396     				panelp->brdnr, panelp->panelnr, i);
4397     			continue;
4398     		}
4399     		chipmask |= (0x1 << i);
4400     		outb(GCCR, (ioaddr + XP_ADDR));
4401     		outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
4402     		outb(WDTRCR, (ioaddr + XP_ADDR));
4403     		outb(0xff, (ioaddr + XP_DATA));
4404     	}
4405     
4406     	BRDDISABLE(panelp->brdnr);
4407     	return(chipmask);
4408     }
4409     
4410     /*****************************************************************************/
4411     
4412     /*
4413      *	Initialize hardware specific port registers.
4414      */
4415     
4416     static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
4417     {
4418     #if DEBUG
4419     	printk("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
4420     		(int) brdp, (int) panelp, (int) portp);
4421     #endif
4422     
4423     	if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
4424     	    (portp == (stlport_t *) NULL))
4425     		return;
4426     
4427     	portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
4428     	portp->uartaddr = (portp->portnr & 0x07) << 4;
4429     	portp->pagenr = panelp->pagenr;
4430     	portp->hwid = 0x1;
4431     
4432     	BRDENABLE(portp->brdnr, portp->pagenr);
4433     	stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
4434     	BRDDISABLE(portp->brdnr);
4435     }
4436     
4437     /*****************************************************************************/
4438     
4439     /*
4440      *	Set up the sc26198 registers for a port based on the termios port
4441      *	settings.
4442      */
4443     
4444     static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
4445     {
4446     	stlbrd_t	*brdp;
4447     	unsigned long	flags;
4448     	unsigned int	baudrate;
4449     	unsigned char	mr0, mr1, mr2, clk;
4450     	unsigned char	imron, imroff, iopr, ipr;
4451     
4452     	mr0 = 0;
4453     	mr1 = 0;
4454     	mr2 = 0;
4455     	clk = 0;
4456     	iopr = 0;
4457     	imron = 0;
4458     	imroff = 0;
4459     
4460     	brdp = stl_brds[portp->brdnr];
4461     	if (brdp == (stlbrd_t *) NULL)
4462     		return;
4463     
4464     /*
4465      *	Set up the RX char ignore mask with those RX error types we
4466      *	can ignore.
4467      */
4468     	portp->rxignoremsk = 0;
4469     	if (tiosp->c_iflag & IGNPAR)
4470     		portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
4471     			SR_RXOVERRUN);
4472     	if (tiosp->c_iflag & IGNBRK)
4473     		portp->rxignoremsk |= SR_RXBREAK;
4474     
4475     	portp->rxmarkmsk = SR_RXOVERRUN;
4476     	if (tiosp->c_iflag & (INPCK | PARMRK))
4477     		portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
4478     	if (tiosp->c_iflag & BRKINT)
4479     		portp->rxmarkmsk |= SR_RXBREAK;
4480     
4481     /*
4482      *	Go through the char size, parity and stop bits and set all the
4483      *	option register appropriately.
4484      */
4485     	switch (tiosp->c_cflag & CSIZE) {
4486     	case CS5:
4487     		mr1 |= MR1_CS5;
4488     		break;
4489     	case CS6:
4490     		mr1 |= MR1_CS6;
4491     		break;
4492     	case CS7:
4493     		mr1 |= MR1_CS7;
4494     		break;
4495     	default:
4496     		mr1 |= MR1_CS8;
4497     		break;
4498     	}
4499     
4500     	if (tiosp->c_cflag & CSTOPB)
4501     		mr2 |= MR2_STOP2;
4502     	else
4503     		mr2 |= MR2_STOP1;
4504     
4505     	if (tiosp->c_cflag & PARENB) {
4506     		if (tiosp->c_cflag & PARODD)
4507     			mr1 |= (MR1_PARENB | MR1_PARODD);
4508     		else
4509     			mr1 |= (MR1_PARENB | MR1_PAREVEN);
4510     	} else {
4511     		mr1 |= MR1_PARNONE;
4512     	}
4513     
4514     	mr1 |= MR1_ERRBLOCK;
4515     
4516     /*
4517      *	Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
4518      *	space for hardware flow control and the like. This should be set to
4519      *	VMIN.
4520      */
4521     	mr2 |= MR2_RXFIFOHALF;
4522     
4523     /*
4524      *	Calculate the baud rate timers. For now we will just assume that
4525      *	the input and output baud are the same. The sc26198 has a fixed
4526      *	baud rate table, so only discrete baud rates possible.
4527      */
4528     	baudrate = tiosp->c_cflag & CBAUD;
4529     	if (baudrate & CBAUDEX) {
4530     		baudrate &= ~CBAUDEX;
4531     		if ((baudrate < 1) || (baudrate > 4))
4532     			tiosp->c_cflag &= ~CBAUDEX;
4533     		else
4534     			baudrate += 15;
4535     	}
4536     	baudrate = stl_baudrates[baudrate];
4537     	if ((tiosp->c_cflag & CBAUD) == B38400) {
4538     		if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
4539     			baudrate = 57600;
4540     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
4541     			baudrate = 115200;
4542     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
4543     			baudrate = 230400;
4544     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
4545     			baudrate = 460800;
4546     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
4547     			baudrate = (portp->baud_base / portp->custom_divisor);
4548     	}
4549     	if (baudrate > STL_SC26198MAXBAUD)
4550     		baudrate = STL_SC26198MAXBAUD;
4551     
4552     	if (baudrate > 0) {
4553     		for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
4554     			if (baudrate <= sc26198_baudtable[clk])
4555     				break;
4556     		}
4557     	}
4558     
4559     /*
4560      *	Check what form of modem signaling is required and set it up.
4561      */
4562     	if (tiosp->c_cflag & CLOCAL) {
4563     		portp->flags &= ~ASYNC_CHECK_CD;
4564     	} else {
4565     		iopr |= IOPR_DCDCOS;
4566     		imron |= IR_IOPORT;
4567     		portp->flags |= ASYNC_CHECK_CD;
4568     	}
4569     
4570     /*
4571      *	Setup sc26198 enhanced modes if we can. In particular we want to
4572      *	handle as much of the flow control as possible automatically. As
4573      *	well as saving a few CPU cycles it will also greatly improve flow
4574      *	control reliability.
4575      */
4576     	if (tiosp->c_iflag & IXON) {
4577     		mr0 |= MR0_SWFTX | MR0_SWFT;
4578     		imron |= IR_XONXOFF;
4579     	} else {
4580     		imroff |= IR_XONXOFF;
4581     	}
4582     	if (tiosp->c_iflag & IXOFF)
4583     		mr0 |= MR0_SWFRX;
4584     
4585     	if (tiosp->c_cflag & CRTSCTS) {
4586     		mr2 |= MR2_AUTOCTS;
4587     		mr1 |= MR1_AUTORTS;
4588     	}
4589     
4590     /*
4591      *	All sc26198 register values calculated so go through and set
4592      *	them all up.
4593      */
4594     
4595     #if DEBUG
4596     	printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
4597     		portp->portnr, portp->panelnr, portp->brdnr);
4598     	printk("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
4599     	printk("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
4600     	printk("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
4601     		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
4602     		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
4603     #endif
4604     
4605     	save_flags(flags);
4606     	cli();
4607     	BRDENABLE(portp->brdnr, portp->pagenr);
4608     	stl_sc26198setreg(portp, IMR, 0);
4609     	stl_sc26198updatereg(portp, MR0, mr0);
4610     	stl_sc26198updatereg(portp, MR1, mr1);
4611     	stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
4612     	stl_sc26198updatereg(portp, MR2, mr2);
4613     	stl_sc26198updatereg(portp, IOPIOR,
4614     		((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
4615     
4616     	if (baudrate > 0) {
4617     		stl_sc26198setreg(portp, TXCSR, clk);
4618     		stl_sc26198setreg(portp, RXCSR, clk);
4619     	}
4620     
4621     	stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
4622     	stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
4623     
4624     	ipr = stl_sc26198getreg(portp, IPR);
4625     	if (ipr & IPR_DCD)
4626     		portp->sigs &= ~TIOCM_CD;
4627     	else
4628     		portp->sigs |= TIOCM_CD;
4629     
4630     	portp->imr = (portp->imr & ~imroff) | imron;
4631     	stl_sc26198setreg(portp, IMR, portp->imr);
4632     	BRDDISABLE(portp->brdnr);
4633     	restore_flags(flags);
4634     }
4635     
4636     /*****************************************************************************/
4637     
4638     /*
4639      *	Set the state of the DTR and RTS signals.
4640      */
4641     
4642     static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
4643     {
4644     	unsigned char	iopioron, iopioroff;
4645     	unsigned long	flags;
4646     
4647     #if DEBUG
4648     	printk("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
4649     		(int) portp, dtr, rts);
4650     #endif
4651     
4652     	iopioron = 0;
4653     	iopioroff = 0;
4654     	if (dtr == 0)
4655     		iopioroff |= IPR_DTR;
4656     	else if (dtr > 0)
4657     		iopioron |= IPR_DTR;
4658     	if (rts == 0)
4659     		iopioroff |= IPR_RTS;
4660     	else if (rts > 0)
4661     		iopioron |= IPR_RTS;
4662     
4663     	save_flags(flags);
4664     	cli();
4665     	BRDENABLE(portp->brdnr, portp->pagenr);
4666     	stl_sc26198setreg(portp, IOPIOR,
4667     		((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4668     	BRDDISABLE(portp->brdnr);
4669     	restore_flags(flags);
4670     }
4671     
4672     /*****************************************************************************/
4673     
4674     /*
4675      *	Return the state of the signals.
4676      */
4677     
4678     static int stl_sc26198getsignals(stlport_t *portp)
4679     {
4680     	unsigned char	ipr;
4681     	unsigned long	flags;
4682     	int		sigs;
4683     
4684     #if DEBUG
4685     	printk("stl_sc26198getsignals(portp=%x)\n", (int) portp);
4686     #endif
4687     
4688     	save_flags(flags);
4689     	cli();
4690     	BRDENABLE(portp->brdnr, portp->pagenr);
4691     	ipr = stl_sc26198getreg(portp, IPR);
4692     	BRDDISABLE(portp->brdnr);
4693     	restore_flags(flags);
4694     
4695     	sigs = 0;
4696     	sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4697     	sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4698     	sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4699     	sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4700     	sigs |= TIOCM_DSR;
4701     	return(sigs);
4702     }
4703     
4704     /*****************************************************************************/
4705     
4706     /*
4707      *	Enable/Disable the Transmitter and/or Receiver.
4708      */
4709     
4710     static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
4711     {
4712     	unsigned char	ccr;
4713     	unsigned long	flags;
4714     
4715     #if DEBUG
4716     	printk("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
4717     		(int) portp, rx, tx);
4718     #endif
4719     
4720     	ccr = portp->crenable;
4721     	if (tx == 0)
4722     		ccr &= ~CR_TXENABLE;
4723     	else if (tx > 0)
4724     		ccr |= CR_TXENABLE;
4725     	if (rx == 0)
4726     		ccr &= ~CR_RXENABLE;
4727     	else if (rx > 0)
4728     		ccr |= CR_RXENABLE;
4729     
4730     	save_flags(flags);
4731     	cli();
4732     	BRDENABLE(portp->brdnr, portp->pagenr);
4733     	stl_sc26198setreg(portp, SCCR, ccr);
4734     	BRDDISABLE(portp->brdnr);
4735     	portp->crenable = ccr;
4736     	restore_flags(flags);
4737     }
4738     
4739     /*****************************************************************************/
4740     
4741     /*
4742      *	Start/stop the Transmitter and/or Receiver.
4743      */
4744     
4745     static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
4746     {
4747     	unsigned char	imr;
4748     	unsigned long	flags;
4749     
4750     #if DEBUG
4751     	printk("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
4752     		(int) portp, rx, tx);
4753     #endif
4754     
4755     	imr = portp->imr;
4756     	if (tx == 0)
4757     		imr &= ~IR_TXRDY;
4758     	else if (tx == 1)
4759     		imr |= IR_TXRDY;
4760     	if (rx == 0)
4761     		imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4762     	else if (rx > 0)
4763     		imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4764     
4765     	save_flags(flags);
4766     	cli();
4767     	BRDENABLE(portp->brdnr, portp->pagenr);
4768     	stl_sc26198setreg(portp, IMR, imr);
4769     	BRDDISABLE(portp->brdnr);
4770     	portp->imr = imr;
4771     	if (tx > 0)
4772     		set_bit(ASYI_TXBUSY, &portp->istate);
4773     	restore_flags(flags);
4774     }
4775     
4776     /*****************************************************************************/
4777     
4778     /*
4779      *	Disable all interrupts from this port.
4780      */
4781     
4782     static void stl_sc26198disableintrs(stlport_t *portp)
4783     {
4784     	unsigned long	flags;
4785     
4786     #if DEBUG
4787     	printk("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
4788     #endif
4789     
4790     	save_flags(flags);
4791     	cli();
4792     	BRDENABLE(portp->brdnr, portp->pagenr);
4793     	portp->imr = 0;
4794     	stl_sc26198setreg(portp, IMR, 0);
4795     	BRDDISABLE(portp->brdnr);
4796     	restore_flags(flags);
4797     }
4798     
4799     /*****************************************************************************/
4800     
4801     static void stl_sc26198sendbreak(stlport_t *portp, int len)
4802     {
4803     	unsigned long	flags;
4804     
4805     #if DEBUG
4806     	printk("stl_sc26198sendbreak(portp=%x,len=%d)\n", (int) portp, len);
4807     #endif
4808     
4809     	save_flags(flags);
4810     	cli();
4811     	BRDENABLE(portp->brdnr, portp->pagenr);
4812     	if (len == 1) {
4813     		stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4814     		portp->stats.txbreaks++;
4815     	} else {
4816     		stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4817     	}
4818     	BRDDISABLE(portp->brdnr);
4819     	restore_flags(flags);
4820     }
4821     
4822     /*****************************************************************************/
4823     
4824     /*
4825      *	Take flow control actions...
4826      */
4827     
4828     static void stl_sc26198flowctrl(stlport_t *portp, int state)
4829     {
4830     	struct tty_struct	*tty;
4831     	unsigned long		flags;
4832     	unsigned char		mr0;
4833     
4834     #if DEBUG
4835     	printk("stl_sc26198flowctrl(portp=%x,state=%x)\n", (int) portp, state);
4836     #endif
4837     
4838     	if (portp == (stlport_t *) NULL)
4839     		return;
4840     	tty = portp->tty;
4841     	if (tty == (struct tty_struct *) NULL)
4842     		return;
4843     
4844     	save_flags(flags);
4845     	cli();
4846     	BRDENABLE(portp->brdnr, portp->pagenr);
4847     
4848     	if (state) {
4849     		if (tty->termios->c_iflag & IXOFF) {
4850     			mr0 = stl_sc26198getreg(portp, MR0);
4851     			stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4852     			stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4853     			mr0 |= MR0_SWFRX;
4854     			portp->stats.rxxon++;
4855     			stl_sc26198wait(portp);
4856     			stl_sc26198setreg(portp, MR0, mr0);
4857     		}
4858     /*
4859      *		Question: should we return RTS to what it was before? It may
4860      *		have been set by an ioctl... Suppose not, since if you have
4861      *		hardware flow control set then it is pretty silly to go and
4862      *		set the RTS line by hand.
4863      */
4864     		if (tty->termios->c_cflag & CRTSCTS) {
4865     			stl_sc26198setreg(portp, MR1,
4866     				(stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4867     			stl_sc26198setreg(portp, IOPIOR,
4868     				(stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4869     			portp->stats.rxrtson++;
4870     		}
4871     	} else {
4872     		if (tty->termios->c_iflag & IXOFF) {
4873     			mr0 = stl_sc26198getreg(portp, MR0);
4874     			stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4875     			stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4876     			mr0 &= ~MR0_SWFRX;
4877     			portp->stats.rxxoff++;
4878     			stl_sc26198wait(portp);
4879     			stl_sc26198setreg(portp, MR0, mr0);
4880     		}
4881     		if (tty->termios->c_cflag & CRTSCTS) {
4882     			stl_sc26198setreg(portp, MR1,
4883     				(stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4884     			stl_sc26198setreg(portp, IOPIOR,
4885     				(stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4886     			portp->stats.rxrtsoff++;
4887     		}
4888     	}
4889     
4890     	BRDDISABLE(portp->brdnr);
4891     	restore_flags(flags);
4892     }
4893     
4894     /*****************************************************************************/
4895     
4896     /*
4897      *	Send a flow control character.
4898      */
4899     
4900     static void stl_sc26198sendflow(stlport_t *portp, int state)
4901     {
4902     	struct tty_struct	*tty;
4903     	unsigned long		flags;
4904     	unsigned char		mr0;
4905     
4906     #if DEBUG
4907     	printk("stl_sc26198sendflow(portp=%x,state=%x)\n", (int) portp, state);
4908     #endif
4909     
4910     	if (portp == (stlport_t *) NULL)
4911     		return;
4912     	tty = portp->tty;
4913     	if (tty == (struct tty_struct *) NULL)
4914     		return;
4915     
4916     	save_flags(flags);
4917     	cli();
4918     	BRDENABLE(portp->brdnr, portp->pagenr);
4919     	if (state) {
4920     		mr0 = stl_sc26198getreg(portp, MR0);
4921     		stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4922     		stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4923     		mr0 |= MR0_SWFRX;
4924     		portp->stats.rxxon++;
4925     		stl_sc26198wait(portp);
4926     		stl_sc26198setreg(portp, MR0, mr0);
4927     	} else {
4928     		mr0 = stl_sc26198getreg(portp, MR0);
4929     		stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4930     		stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4931     		mr0 &= ~MR0_SWFRX;
4932     		portp->stats.rxxoff++;
4933     		stl_sc26198wait(portp);
4934     		stl_sc26198setreg(portp, MR0, mr0);
4935     	}
4936     	BRDDISABLE(portp->brdnr);
4937     	restore_flags(flags);
4938     }
4939     
4940     /*****************************************************************************/
4941     
4942     static void stl_sc26198flush(stlport_t *portp)
4943     {
4944     	unsigned long	flags;
4945     
4946     #if DEBUG
4947     	printk("stl_sc26198flush(portp=%x)\n", (int) portp);
4948     #endif
4949     
4950     	if (portp == (stlport_t *) NULL)
4951     		return;
4952     
4953     	save_flags(flags);
4954     	cli();
4955     	BRDENABLE(portp->brdnr, portp->pagenr);
4956     	stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4957     	stl_sc26198setreg(portp, SCCR, portp->crenable);
4958     	BRDDISABLE(portp->brdnr);
4959     	portp->tx.tail = portp->tx.head;
4960     	restore_flags(flags);
4961     }
4962     
4963     /*****************************************************************************/
4964     
4965     /*
4966      *	Return the current state of data flow on this port. This is only
4967      *	really interresting when determining if data has fully completed
4968      *	transmission or not... The sc26198 interrupt scheme cannot
4969      *	determine when all data has actually drained, so we need to
4970      *	check the port statusy register to be sure.
4971      */
4972     
4973     static int stl_sc26198datastate(stlport_t *portp)
4974     {
4975     	unsigned long	flags;
4976     	unsigned char	sr;
4977     
4978     #if DEBUG
4979     	printk("stl_sc26198datastate(portp=%x)\n", (int) portp);
4980     #endif
4981     
4982     	if (portp == (stlport_t *) NULL)
4983     		return(0);
4984     	if (test_bit(ASYI_TXBUSY, &portp->istate))
4985     		return(1);
4986     
4987     	save_flags(flags);
4988     	cli();
4989     	BRDENABLE(portp->brdnr, portp->pagenr);
4990     	sr = stl_sc26198getreg(portp, SR);
4991     	BRDDISABLE(portp->brdnr);
4992     	restore_flags(flags);
4993     
4994     	return((sr & SR_TXEMPTY) ? 0 : 1);
4995     }
4996     
4997     /*****************************************************************************/
4998     
4999     /*
5000      *	Delay for a small amount of time, to give the sc26198 a chance
5001      *	to process a command...
5002      */
5003     
5004     static void stl_sc26198wait(stlport_t *portp)
5005     {
5006     	int	i;
5007     
5008     #if DEBUG
5009     	printk("stl_sc26198wait(portp=%x)\n", (int) portp);
5010     #endif
5011     
5012     	if (portp == (stlport_t *) NULL)
5013     		return;
5014     
5015     	for (i = 0; (i < 20); i++)
5016     		stl_sc26198getglobreg(portp, TSTR);
5017     }
5018     
5019     /*****************************************************************************/
5020     
5021     /*
5022      *	If we are TX flow controlled and in IXANY mode then we may
5023      *	need to unflow control here. We gotta do this because of the
5024      *	automatic flow control modes of the sc26198.
5025      */
5026     
5027     static inline void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty)
5028     {
5029     	unsigned char	mr0;
5030     
5031     	mr0 = stl_sc26198getreg(portp, MR0);
5032     	stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
5033     	stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
5034     	stl_sc26198wait(portp);
5035     	stl_sc26198setreg(portp, MR0, mr0);
5036     	clear_bit(ASYI_TXFLOWED, &portp->istate);
5037     }
5038     
5039     /*****************************************************************************/
5040     
5041     /*
5042      *	Interrupt service routine for sc26198 panels.
5043      */
5044     
5045     static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
5046     {
5047     	stlport_t	*portp;
5048     	unsigned int	iack;
5049     
5050     /* 
5051      *	Work around bug in sc26198 chip... Cannot have A6 address
5052      *	line of UART high, else iack will be returned as 0.
5053      */
5054     	outb(0, (iobase + 1));
5055     
5056     	iack = inb(iobase + XP_IACK);
5057     	portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
5058     
5059     	if (iack & IVR_RXDATA)
5060     		stl_sc26198rxisr(portp, iack);
5061     	else if (iack & IVR_TXDATA)
5062     		stl_sc26198txisr(portp);
5063     	else
5064     		stl_sc26198otherisr(portp, iack);
5065     }
5066     
5067     /*****************************************************************************/
5068     
5069     /*
5070      *	Transmit interrupt handler. This has gotta be fast!  Handling TX
5071      *	chars is pretty simple, stuff as many as possible from the TX buffer
5072      *	into the sc26198 FIFO.
5073      *	In practice it is possible that interrupts are enabled but that the
5074      *	port has been hung up. Need to handle not having any TX buffer here,
5075      *	this is done by using the side effect that head and tail will also
5076      *	be NULL if the buffer has been freed.
5077      */
5078     
5079     static void stl_sc26198txisr(stlport_t *portp)
5080     {
5081     	unsigned int	ioaddr;
5082     	unsigned char	mr0;
5083     	int		len, stlen;
5084     	char		*head, *tail;
5085     
5086     #if DEBUG
5087     	printk("stl_sc26198txisr(portp=%x)\n", (int) portp);
5088     #endif
5089     
5090     	ioaddr = portp->ioaddr;
5091     	head = portp->tx.head;
5092     	tail = portp->tx.tail;
5093     	len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
5094     	if ((len == 0) || ((len < STL_TXBUFLOW) &&
5095     	    (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
5096     		set_bit(ASYI_TXLOW, &portp->istate);
5097     		MOD_INC_USE_COUNT;
5098     		if (schedule_task(&portp->tqueue) == 0)
5099     			MOD_DEC_USE_COUNT;
5100     	}
5101     
5102     	if (len == 0) {
5103     		outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
5104     		mr0 = inb(ioaddr + XP_DATA);
5105     		if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
5106     			portp->imr &= ~IR_TXRDY;
5107     			outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
5108     			outb(portp->imr, (ioaddr + XP_DATA));
5109     			clear_bit(ASYI_TXBUSY, &portp->istate);
5110     		} else {
5111     			mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
5112     			outb(mr0, (ioaddr + XP_DATA));
5113     		}
5114     	} else {
5115     		len = MIN(len, SC26198_TXFIFOSIZE);
5116     		portp->stats.txtotal += len;
5117     		stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
5118     		outb(GTXFIFO, (ioaddr + XP_ADDR));
5119     		outsb((ioaddr + XP_DATA), tail, stlen);
5120     		len -= stlen;
5121     		tail += stlen;
5122     		if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
5123     			tail = portp->tx.buf;
5124     		if (len > 0) {
5125     			outsb((ioaddr + XP_DATA), tail, len);
5126     			tail += len;
5127     		}
5128     		portp->tx.tail = tail;
5129     	}
5130     }
5131     
5132     /*****************************************************************************/
5133     
5134     /*
5135      *	Receive character interrupt handler. Determine if we have good chars
5136      *	or bad chars and then process appropriately. Good chars are easy
5137      *	just shove the lot into the RX buffer and set all status byte to 0.
5138      *	If a bad RX char then process as required. This routine needs to be
5139      *	fast!  In practice it is possible that we get an interrupt on a port
5140      *	that is closed. This can happen on hangups - since they completely
5141      *	shutdown a port not in user context. Need to handle this case.
5142      */
5143     
5144     static void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
5145     {
5146     	struct tty_struct	*tty;
5147     	unsigned int		len, buflen, ioaddr;
5148     
5149     #if DEBUG
5150     	printk("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
5151     #endif
5152     
5153     	tty = portp->tty;
5154     	ioaddr = portp->ioaddr;
5155     	outb(GIBCR, (ioaddr + XP_ADDR));
5156     	len = inb(ioaddr + XP_DATA) + 1;
5157     
5158     	if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
5159     		if ((tty == (struct tty_struct *) NULL) ||
5160     		    (tty->flip.char_buf_ptr == (char *) NULL) ||
5161     		    ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
5162     			len = MIN(len, sizeof(stl_unwanted));
5163     			outb(GRXFIFO, (ioaddr + XP_ADDR));
5164     			insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
5165     			portp->stats.rxlost += len;
5166     			portp->stats.rxtotal += len;
5167     		} else {
5168     			len = MIN(len, buflen);
5169     			if (len > 0) {
5170     				outb(GRXFIFO, (ioaddr + XP_ADDR));
5171     				insb((ioaddr + XP_DATA), tty->flip.char_buf_ptr, len);
5172     				memset(tty->flip.flag_buf_ptr, 0, len);
5173     				tty->flip.flag_buf_ptr += len;
5174     				tty->flip.char_buf_ptr += len;
5175     				tty->flip.count += len;
5176     				tty_schedule_flip(tty);
5177     				portp->stats.rxtotal += len;
5178     			}
5179     		}
5180     	} else {
5181     		stl_sc26198rxbadchars(portp);
5182     	}
5183     
5184     /*
5185      *	If we are TX flow controlled and in IXANY mode then we may need
5186      *	to unflow control here. We gotta do this because of the automatic
5187      *	flow control modes of the sc26198.
5188      */
5189     	if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
5190     		if ((tty != (struct tty_struct *) NULL) &&
5191     		    (tty->termios != (struct termios *) NULL) &&
5192     		    (tty->termios->c_iflag & IXANY)) {
5193     			stl_sc26198txunflow(portp, tty);
5194     		}
5195     	}
5196     }
5197     
5198     /*****************************************************************************/
5199     
5200     /*
5201      *	Process an RX bad character.
5202      */
5203     
5204     static void inline stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch)
5205     {
5206     	struct tty_struct	*tty;
5207     	unsigned int		ioaddr;
5208     
5209     	tty = portp->tty;
5210     	ioaddr = portp->ioaddr;
5211     
5212     	if (status & SR_RXPARITY)
5213     		portp->stats.rxparity++;
5214     	if (status & SR_RXFRAMING)
5215     		portp->stats.rxframing++;
5216     	if (status & SR_RXOVERRUN)
5217     		portp->stats.rxoverrun++;
5218     	if (status & SR_RXBREAK)
5219     		portp->stats.rxbreaks++;
5220     
5221     	if ((tty != (struct tty_struct *) NULL) &&
5222     	    ((portp->rxignoremsk & status) == 0)) {
5223     		if (portp->rxmarkmsk & status) {
5224     			if (status & SR_RXBREAK) {
5225     				status = TTY_BREAK;
5226     				if (portp->flags & ASYNC_SAK) {
5227     					do_SAK(tty);
5228     					BRDENABLE(portp->brdnr, portp->pagenr);
5229     				}
5230     			} else if (status & SR_RXPARITY) {
5231     				status = TTY_PARITY;
5232     			} else if (status & SR_RXFRAMING) {
5233     				status = TTY_FRAME;
5234     			} else if(status & SR_RXOVERRUN) {
5235     				status = TTY_OVERRUN;
5236     			} else {
5237     				status = 0;
5238     			}
5239     		} else {
5240     			status = 0;
5241     		}
5242     
5243     		if (tty->flip.char_buf_ptr != (char *) NULL) {
5244     			if (tty->flip.count < TTY_FLIPBUF_SIZE) {
5245     				*tty->flip.flag_buf_ptr++ = status;
5246     				*tty->flip.char_buf_ptr++ = ch;
5247     				tty->flip.count++;
5248     			}
5249     			tty_schedule_flip(tty);
5250     		}
5251     
5252     		if (status == 0)
5253     			portp->stats.rxtotal++;
5254     	}
5255     }
5256     
5257     /*****************************************************************************/
5258     
5259     /*
5260      *	Process all characters in the RX FIFO of the UART. Check all char
5261      *	status bytes as well, and process as required. We need to check
5262      *	all bytes in the FIFO, in case some more enter the FIFO while we
5263      *	are here. To get the exact character error type we need to switch
5264      *	into CHAR error mode (that is why we need to make sure we empty
5265      *	the FIFO).
5266      */
5267     
5268     static void stl_sc26198rxbadchars(stlport_t *portp)
5269     {
5270     	unsigned char	status, mr1;
5271     	char		ch;
5272     
5273     /*
5274      *	To get the precise error type for each character we must switch
5275      *	back into CHAR error mode.
5276      */
5277     	mr1 = stl_sc26198getreg(portp, MR1);
5278     	stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
5279     
5280     	while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
5281     		stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
5282     		ch = stl_sc26198getreg(portp, RXFIFO);
5283     		stl_sc26198rxbadch(portp, status, ch);
5284     	}
5285     
5286     /*
5287      *	To get correct interrupt class we must switch back into BLOCK
5288      *	error mode.
5289      */
5290     	stl_sc26198setreg(portp, MR1, mr1);
5291     }
5292     
5293     /*****************************************************************************/
5294     
5295     /*
5296      *	Other interrupt handler. This includes modem signals, flow
5297      *	control actions, etc. Most stuff is left to off-level interrupt
5298      *	processing time.
5299      */
5300     
5301     static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
5302     {
5303     	unsigned char	cir, ipr, xisr;
5304     
5305     #if DEBUG
5306     	printk("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
5307     #endif
5308     
5309     	cir = stl_sc26198getglobreg(portp, CIR);
5310     
5311     	switch (cir & CIR_SUBTYPEMASK) {
5312     	case CIR_SUBCOS:
5313     		ipr = stl_sc26198getreg(portp, IPR);
5314     		if (ipr & IPR_DCDCHANGE) {
5315     			set_bit(ASYI_DCDCHANGE, &portp->istate);
5316     			MOD_INC_USE_COUNT;
5317     			if (schedule_task(&portp->tqueue) == 0)
5318     				MOD_DEC_USE_COUNT;
5319     			portp->stats.modem++;
5320     		}
5321     		break;
5322     	case CIR_SUBXONXOFF:
5323     		xisr = stl_sc26198getreg(portp, XISR);
5324     		if (xisr & XISR_RXXONGOT) {
5325     			set_bit(ASYI_TXFLOWED, &portp->istate);
5326     			portp->stats.txxoff++;
5327     		}
5328     		if (xisr & XISR_RXXOFFGOT) {
5329     			clear_bit(ASYI_TXFLOWED, &portp->istate);
5330     			portp->stats.txxon++;
5331     		}
5332     		break;
5333     	case CIR_SUBBREAK:
5334     		stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
5335     		stl_sc26198rxbadchars(portp);
5336     		break;
5337     	default:
5338     		break;
5339     	}
5340     }
5341     
5342     /*****************************************************************************/
5343