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