stallion.c 123.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*****************************************************************************/

/*
 *	stallion.c  -- stallion multiport serial driver.
 *
 *	Copyright (C) 1996-1999  Stallion Technologies
 *	Copyright (C) 1994-1996  Greg Ungerer.
 *
 *	This code is loosely based on the Linux serial driver, written by
 *	Linus Torvalds, Theodore T'so and others.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/cd1400.h>
#include <linux/sc26198.h>
#include <linux/comstats.h>
#include <linux/stallion.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/smp_lock.h>
#include <linux/device.h>
#include <linux/delay.h>
44
#include <linux/ctype.h>
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

#include <asm/io.h>
#include <asm/uaccess.h>

#include <linux/pci.h>

/*****************************************************************************/

/*
 *	Define different board types. Use the standard Stallion "assigned"
 *	board numbers. Boards supported in this driver are abbreviated as
 *	EIO = EasyIO and ECH = EasyConnection 8/32.
 */
#define	BRD_EASYIO	20
#define	BRD_ECH		21
#define	BRD_ECHMC	22
#define	BRD_ECHPCI	26
#define	BRD_ECH64PCI	27
#define	BRD_EASYIOPCI	28

65
struct stlconf {
66
	unsigned int	brdtype;
L
Linus Torvalds 已提交
67 68 69 70 71 72 73
	int		ioaddr1;
	int		ioaddr2;
	unsigned long	memaddr;
	int		irq;
	int		irqtype;
};

74
static unsigned int stl_nrbrds;
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

/*****************************************************************************/

/*
 *	Define some important driver characteristics. Device major numbers
 *	allocated as per Linux Device Registry.
 */
#ifndef	STL_SIOMEMMAJOR
#define	STL_SIOMEMMAJOR		28
#endif
#ifndef	STL_SERIALMAJOR
#define	STL_SERIALMAJOR		24
#endif
#ifndef	STL_CALLOUTMAJOR
#define	STL_CALLOUTMAJOR	25
#endif

/*
 *	Set the TX buffer size. Bigger is better, but we don't want
 *	to chew too much memory with buffers!
 */
#define	STL_TXBUFLOW		512
#define	STL_TXBUFSIZE		4096

/*****************************************************************************/

/*
 *	Define our local driver identity first. Set up stuff to deal with
 *	all the local structures required by a serial tty driver.
 */
static char	*stl_drvtitle = "Stallion Multiport Serial Driver";
static char	*stl_drvname = "stallion";
static char	*stl_drvversion = "5.6.0";

static struct tty_driver	*stl_serial;

/*
 *	Define a local default termios struct. All ports will be created
 *	with this termios initially. Basically all it defines is a raw port
 *	at 9600, 8 data bits, 1 stop bit.
 */
A
Alan Cox 已提交
116
static struct ktermios		stl_deftermios = {
L
Linus Torvalds 已提交
117 118
	.c_cflag	= (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
	.c_cc		= INIT_C_CC,
A
Alan Cox 已提交
119 120
	.c_ispeed	= 9600,
	.c_ospeed	= 9600,
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129
};

/*
 *	Define global place to put buffer overflow characters.
 */
static char		stl_unwanted[SC26198_RXFIFOSIZE];

/*****************************************************************************/

130
static DEFINE_MUTEX(stl_brdslock);
J
Jiri Slaby 已提交
131
static struct stlbrd		*stl_brds[STL_MAXBRDS];
L
Linus Torvalds 已提交
132 133 134 135 136 137

/*
 *	Per board state flags. Used with the state field of the board struct.
 *	Not really much here!
 */
#define	BRD_FOUND	0x1
138 139
#define STL_PROBED	0x2

L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

/*
 *	Define the port structure istate flags. These set of flags are
 *	modified at interrupt time - so setting and reseting them needs
 *	to be atomic. Use the bit clear/setting routines for this.
 */
#define	ASYI_TXBUSY	1
#define	ASYI_TXLOW	2
#define	ASYI_DCDCHANGE	3
#define	ASYI_TXFLOWED	4

/*
 *	Define an array of board names as printable strings. Handy for
 *	referencing boards when printing trace and stuff.
 */
static char	*stl_brdnames[] = {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
L
Linus Torvalds 已提交
176 177 178
	"EasyIO",
	"EC8/32-AT",
	"EC8/32-MC",
179 180 181
	NULL,
	NULL,
	NULL,
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189 190 191 192 193
	"EC8/32-PCI",
	"EC8/64-PCI",
	"EasyIO-PCI",
};

/*****************************************************************************/

/*
 *	Define some string labels for arguments passed from the module
 *	load line. These allow for easy board definitions, and easy
 *	modification of the io, memory and irq resoucres.
 */
194
static unsigned int stl_nargs;
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static char	*board0[4];
static char	*board1[4];
static char	*board2[4];
static char	*board3[4];

static char	**stl_brdsp[] = {
	(char **) &board0,
	(char **) &board1,
	(char **) &board2,
	(char **) &board3
};

/*
 *	Define a set of common board names, and types. This is used to
 *	parse any module arguments.
 */

J
Jiri Slaby 已提交
212
static struct {
L
Linus Torvalds 已提交
213 214
	char	*name;
	int	type;
J
Jiri Slaby 已提交
215
} stl_brdstr[] = {
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	{ "easyio", BRD_EASYIO },
	{ "eio", BRD_EASYIO },
	{ "20", BRD_EASYIO },
	{ "ec8/32", BRD_ECH },
	{ "ec8/32-at", BRD_ECH },
	{ "ec8/32-isa", BRD_ECH },
	{ "ech", BRD_ECH },
	{ "echat", BRD_ECH },
	{ "21", BRD_ECH },
	{ "ec8/32-mc", BRD_ECHMC },
	{ "ec8/32-mca", BRD_ECHMC },
	{ "echmc", BRD_ECHMC },
	{ "echmca", BRD_ECHMC },
	{ "22", BRD_ECHMC },
	{ "ec8/32-pc", BRD_ECHPCI },
	{ "ec8/32-pci", BRD_ECHPCI },
	{ "26", BRD_ECHPCI },
	{ "ec8/64-pc", BRD_ECH64PCI },
	{ "ec8/64-pci", BRD_ECH64PCI },
	{ "ech-pci", BRD_ECH64PCI },
	{ "echpci", BRD_ECH64PCI },
	{ "echpc", BRD_ECH64PCI },
	{ "27", BRD_ECH64PCI },
	{ "easyio-pc", BRD_EASYIOPCI },
	{ "easyio-pci", BRD_EASYIOPCI },
	{ "eio-pci", BRD_EASYIOPCI },
	{ "eiopci", BRD_EASYIOPCI },
	{ "28", BRD_EASYIOPCI },
};

/*
 *	Define the module agruments.
 */

module_param_array(board0, charp, &stl_nargs, 0);
MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
module_param_array(board1, charp, &stl_nargs, 0);
MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
module_param_array(board2, charp, &stl_nargs, 0);
MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
module_param_array(board3, charp, &stl_nargs, 0);
MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");

/*****************************************************************************/

/*
 *	Hardware ID bits for the EasyIO and ECH boards. These defines apply
 *	to the directly accessible io ports of these boards (not the uarts -
 *	they are in cd1400.h and sc26198.h).
 */
#define	EIO_8PORTRS	0x04
#define	EIO_4PORTRS	0x05
#define	EIO_8PORTDI	0x00
#define	EIO_8PORTM	0x06
#define	EIO_MK3		0x03
#define	EIO_IDBITMASK	0x07

#define	EIO_BRDMASK	0xf0
#define	ID_BRD4		0x10
#define	ID_BRD8		0x20
#define	ID_BRD16	0x30

#define	EIO_INTRPEND	0x08
#define	EIO_INTEDGE	0x00
#define	EIO_INTLEVEL	0x08
#define	EIO_0WS		0x10

#define	ECH_ID		0xa0
#define	ECH_IDBITMASK	0xe0
#define	ECH_BRDENABLE	0x08
#define	ECH_BRDDISABLE	0x00
#define	ECH_INTENABLE	0x01
#define	ECH_INTDISABLE	0x00
#define	ECH_INTLEVEL	0x02
#define	ECH_INTEDGE	0x00
#define	ECH_INTRPEND	0x01
#define	ECH_BRDRESET	0x01

#define	ECHMC_INTENABLE	0x01
#define	ECHMC_BRDRESET	0x02

#define	ECH_PNLSTATUS	2
#define	ECH_PNL16PORT	0x20
#define	ECH_PNLIDMASK	0x07
#define	ECH_PNLXPID	0x40
#define	ECH_PNLINTRPEND	0x80

#define	ECH_ADDR2MASK	0x1e0

/*
 *	Define the vector mapping bits for the programmable interrupt board
 *	hardware. These bits encode the interrupt for the board to use - it
 *	is software selectable (except the EIO-8M).
 */
static unsigned char	stl_vecmap[] = {
	0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
	0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
};

A
Alan Cox 已提交
315 316 317 318 319 320 321 322
/*
 *	Lock ordering is that you may not take stallion_lock holding
 *	brd_lock.
 */

static spinlock_t brd_lock; 		/* Guard the board mapping */
static spinlock_t stallion_lock;	/* Guard the tty driver */

L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
 *	Set up enable and disable macros for the ECH boards. They require
 *	the secondary io address space to be activated and deactivated.
 *	This way all ECH boards can share their secondary io region.
 *	If this is an ECH-PCI board then also need to set the page pointer
 *	to point to the correct page.
 */
#define	BRDENABLE(brdnr,pagenr)						\
	if (stl_brds[(brdnr)]->brdtype == BRD_ECH)			\
		outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE),	\
			stl_brds[(brdnr)]->ioctrl);			\
	else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)		\
		outb((pagenr), stl_brds[(brdnr)]->ioctrl);

#define	BRDDISABLE(brdnr)						\
	if (stl_brds[(brdnr)]->brdtype == BRD_ECH)			\
		outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE),	\
			stl_brds[(brdnr)]->ioctrl);

#define	STL_CD1400MAXBAUD	230400
#define	STL_SC26198MAXBAUD	460800

#define	STL_BAUDBASE		115200
#define	STL_CLOSEDELAY		(5 * HZ / 10)

/*****************************************************************************/

/*
 *	Define the Stallion PCI vendor and device IDs.
 */
#ifndef	PCI_VENDOR_ID_STALLION
#define	PCI_VENDOR_ID_STALLION		0x124d
#endif
#ifndef PCI_DEVICE_ID_ECHPCI832
#define	PCI_DEVICE_ID_ECHPCI832		0x0000
#endif
#ifndef PCI_DEVICE_ID_ECHPCI864
#define	PCI_DEVICE_ID_ECHPCI864		0x0002
#endif
#ifndef PCI_DEVICE_ID_EIOPCI
#define	PCI_DEVICE_ID_EIOPCI		0x0003
#endif

/*
 *	Define structure to hold all Stallion PCI boards.
 */

370 371 372 373 374 375 376 377 378 379 380 381
static struct pci_device_id stl_pcibrds[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864),
		.driver_data = BRD_ECH64PCI },
	{ PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI),
		.driver_data = BRD_EASYIOPCI },
	{ PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832),
		.driver_data = BRD_ECHPCI },
	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410),
		.driver_data = BRD_ECHPCI },
	{ }
};
MODULE_DEVICE_TABLE(pci, stl_pcibrds);
L
Linus Torvalds 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

/*****************************************************************************/

/*
 *	Define macros to extract a brd/port number from a minor number.
 */
#define	MINOR2BRD(min)		(((min) & 0xc0) >> 6)
#define	MINOR2PORT(min)		((min) & 0x3f)

/*
 *	Define a baud rate table that converts termios baud rate selector
 *	into the actual baud rate value. All baud rate calculations are
 *	based on the actual baud rate required.
 */
static unsigned int	stl_baudrates[] = {
	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
	9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
};

/*****************************************************************************/

/*
 *	Declare all those functions in this driver!
 */

static int	stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
J
Jiri Slaby 已提交
408 409 410 411
static int	stl_brdinit(struct stlbrd *brdp);
static int	stl_getportstats(struct stlport *portp, comstats_t __user *cp);
static int	stl_clrportstats(struct stlport *portp, comstats_t __user *cp);
static int	stl_waitcarrier(struct stlport *portp, struct file *filp);
L
Linus Torvalds 已提交
412 413 414 415

/*
 *	CD1400 uart specific handling functions.
 */
J
Jiri Slaby 已提交
416 417 418 419 420
static void	stl_cd1400setreg(struct stlport *portp, int regnr, int value);
static int	stl_cd1400getreg(struct stlport *portp, int regnr);
static int	stl_cd1400updatereg(struct stlport *portp, int regnr, int value);
static int	stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
static void	stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
A
Alan Cox 已提交
421
static void	stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp);
J
Jiri Slaby 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
static int	stl_cd1400getsignals(struct stlport *portp);
static void	stl_cd1400setsignals(struct stlport *portp, int dtr, int rts);
static void	stl_cd1400ccrwait(struct stlport *portp);
static void	stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx);
static void	stl_cd1400startrxtx(struct stlport *portp, int rx, int tx);
static void	stl_cd1400disableintrs(struct stlport *portp);
static void	stl_cd1400sendbreak(struct stlport *portp, int len);
static void	stl_cd1400flowctrl(struct stlport *portp, int state);
static void	stl_cd1400sendflow(struct stlport *portp, int state);
static void	stl_cd1400flush(struct stlport *portp);
static int	stl_cd1400datastate(struct stlport *portp);
static void	stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase);
static void	stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase);
static void	stl_cd1400txisr(struct stlpanel *panelp, int ioaddr);
static void	stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr);
static void	stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr);

static inline int	stl_cd1400breakisr(struct stlport *portp, int ioaddr);
L
Linus Torvalds 已提交
440 441 442 443

/*
 *	SC26198 uart specific handling functions.
 */
J
Jiri Slaby 已提交
444 445 446 447 448 449
static void	stl_sc26198setreg(struct stlport *portp, int regnr, int value);
static int	stl_sc26198getreg(struct stlport *portp, int regnr);
static int	stl_sc26198updatereg(struct stlport *portp, int regnr, int value);
static int	stl_sc26198getglobreg(struct stlport *portp, int regnr);
static int	stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
static void	stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
A
Alan Cox 已提交
450
static void	stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp);
J
Jiri Slaby 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static int	stl_sc26198getsignals(struct stlport *portp);
static void	stl_sc26198setsignals(struct stlport *portp, int dtr, int rts);
static void	stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx);
static void	stl_sc26198startrxtx(struct stlport *portp, int rx, int tx);
static void	stl_sc26198disableintrs(struct stlport *portp);
static void	stl_sc26198sendbreak(struct stlport *portp, int len);
static void	stl_sc26198flowctrl(struct stlport *portp, int state);
static void	stl_sc26198sendflow(struct stlport *portp, int state);
static void	stl_sc26198flush(struct stlport *portp);
static int	stl_sc26198datastate(struct stlport *portp);
static void	stl_sc26198wait(struct stlport *portp);
static void	stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty);
static void	stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase);
static void	stl_sc26198txisr(struct stlport *port);
static void	stl_sc26198rxisr(struct stlport *port, unsigned int iack);
static void	stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch);
static void	stl_sc26198rxbadchars(struct stlport *portp);
static void	stl_sc26198otherisr(struct stlport *port, unsigned int iack);
L
Linus Torvalds 已提交
469 470 471 472 473 474 475

/*****************************************************************************/

/*
 *	Generic UART support structure.
 */
typedef struct uart {
J
Jiri Slaby 已提交
476 477
	int	(*panelinit)(struct stlbrd *brdp, struct stlpanel *panelp);
	void	(*portinit)(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
A
Alan Cox 已提交
478
	void	(*setport)(struct stlport *portp, struct ktermios *tiosp);
J
Jiri Slaby 已提交
479 480 481 482 483 484 485 486 487 488 489
	int	(*getsignals)(struct stlport *portp);
	void	(*setsignals)(struct stlport *portp, int dtr, int rts);
	void	(*enablerxtx)(struct stlport *portp, int rx, int tx);
	void	(*startrxtx)(struct stlport *portp, int rx, int tx);
	void	(*disableintrs)(struct stlport *portp);
	void	(*sendbreak)(struct stlport *portp, int len);
	void	(*flowctrl)(struct stlport *portp, int state);
	void	(*sendflow)(struct stlport *portp, int state);
	void	(*flush)(struct stlport *portp);
	int	(*datastate)(struct stlport *portp);
	void	(*intr)(struct stlpanel *panelp, unsigned int iobase);
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
} uart_t;

/*
 *	Define some macros to make calling these functions nice and clean.
 */
#define	stl_panelinit		(* ((uart_t *) panelp->uartp)->panelinit)
#define	stl_portinit		(* ((uart_t *) portp->uartp)->portinit)
#define	stl_setport		(* ((uart_t *) portp->uartp)->setport)
#define	stl_getsignals		(* ((uart_t *) portp->uartp)->getsignals)
#define	stl_setsignals		(* ((uart_t *) portp->uartp)->setsignals)
#define	stl_enablerxtx		(* ((uart_t *) portp->uartp)->enablerxtx)
#define	stl_startrxtx		(* ((uart_t *) portp->uartp)->startrxtx)
#define	stl_disableintrs	(* ((uart_t *) portp->uartp)->disableintrs)
#define	stl_sendbreak		(* ((uart_t *) portp->uartp)->sendbreak)
#define	stl_flowctrl		(* ((uart_t *) portp->uartp)->flowctrl)
#define	stl_sendflow		(* ((uart_t *) portp->uartp)->sendflow)
#define	stl_flush		(* ((uart_t *) portp->uartp)->flush)
#define	stl_datastate		(* ((uart_t *) portp->uartp)->datastate)

/*****************************************************************************/

/*
 *	CD1400 UART specific data initialization.
 */
static uart_t stl_cd1400uart = {
	stl_cd1400panelinit,
	stl_cd1400portinit,
	stl_cd1400setport,
	stl_cd1400getsignals,
	stl_cd1400setsignals,
	stl_cd1400enablerxtx,
	stl_cd1400startrxtx,
	stl_cd1400disableintrs,
	stl_cd1400sendbreak,
	stl_cd1400flowctrl,
	stl_cd1400sendflow,
	stl_cd1400flush,
	stl_cd1400datastate,
	stl_cd1400eiointr
};

/*
 *	Define the offsets within the register bank of a cd1400 based panel.
 *	These io address offsets are common to the EasyIO board as well.
 */
#define	EREG_ADDR	0
#define	EREG_DATA	4
#define	EREG_RXACK	5
#define	EREG_TXACK	6
#define	EREG_MDACK	7

#define	EREG_BANKSIZE	8

#define	CD1400_CLK	25000000
#define	CD1400_CLK8M	20000000

/*
 *	Define the cd1400 baud rate clocks. These are used when calculating
 *	what clock and divisor to use for the required baud rate. Also
 *	define the maximum baud rate allowed, and the default base baud.
 */
static int	stl_cd1400clkdivs[] = {
	CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
};

/*****************************************************************************/

/*
 *	SC26198 UART specific data initization.
 */
static uart_t stl_sc26198uart = {
	stl_sc26198panelinit,
	stl_sc26198portinit,
	stl_sc26198setport,
	stl_sc26198getsignals,
	stl_sc26198setsignals,
	stl_sc26198enablerxtx,
	stl_sc26198startrxtx,
	stl_sc26198disableintrs,
	stl_sc26198sendbreak,
	stl_sc26198flowctrl,
	stl_sc26198sendflow,
	stl_sc26198flush,
	stl_sc26198datastate,
	stl_sc26198intr
};

/*
 *	Define the offsets within the register bank of a sc26198 based panel.
 */
#define	XP_DATA		0
#define	XP_ADDR		1
#define	XP_MODID	2
#define	XP_STATUS	2
#define	XP_IACK		3

#define	XP_BANKSIZE	4

/*
 *	Define the sc26198 baud rate table. Offsets within the table
 *	represent the actual baud rate selector of sc26198 registers.
 */
static unsigned int	sc26198_baudtable[] = {
	50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
	4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
	230400, 460800, 921600
};

598
#define	SC26198_NRBAUDS		ARRAY_SIZE(sc26198_baudtable)
L
Linus Torvalds 已提交
599 600 601 602 603 604 605

/*****************************************************************************/

/*
 *	Define the driver info for a user level control device. Used mainly
 *	to get at port stats - only not using the port device itself.
 */
606
static const struct file_operations	stl_fsiomem = {
L
Linus Torvalds 已提交
607 608 609 610
	.owner		= THIS_MODULE,
	.ioctl		= stl_memioctl,
};

611
static struct class *stallion_class;
L
Linus Torvalds 已提交
612 613 614 615 616 617 618 619 620 621 622

/*
 *	Check for any arguments passed in on the module load command line.
 */

/*****************************************************************************/

/*
 *	Parse the supplied argument string, into the board conf struct.
 */

623
static int __init stl_parsebrd(struct stlconf *confp, char **argp)
L
Linus Torvalds 已提交
624 625
{
	char	*sp;
626
	unsigned int i;
L
Linus Torvalds 已提交
627

628
	pr_debug("stl_parsebrd(confp=%p,argp=%p)\n", confp, argp);
L
Linus Torvalds 已提交
629

630
	if ((argp[0] == NULL) || (*argp[0] == 0))
631
		return 0;
L
Linus Torvalds 已提交
632

633
	for (sp = argp[0], i = 0; (*sp != 0) && (i < 25); sp++, i++)
634
		*sp = tolower(*sp);
L
Linus Torvalds 已提交
635

636
	for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++)
L
Linus Torvalds 已提交
637 638
		if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
			break;
639

640
	if (i == ARRAY_SIZE(stl_brdstr)) {
L
Linus Torvalds 已提交
641
		printk("STALLION: unknown board name, %s?\n", argp[0]);
642
		return 0;
L
Linus Torvalds 已提交
643 644 645 646 647
	}

	confp->brdtype = stl_brdstr[i].type;

	i = 1;
648
	if ((argp[i] != NULL) && (*argp[i] != 0))
649
		confp->ioaddr1 = simple_strtoul(argp[i], NULL, 0);
L
Linus Torvalds 已提交
650 651
	i++;
	if (confp->brdtype == BRD_ECH) {
652
		if ((argp[i] != NULL) && (*argp[i] != 0))
653
			confp->ioaddr2 = simple_strtoul(argp[i], NULL, 0);
L
Linus Torvalds 已提交
654 655
		i++;
	}
656
	if ((argp[i] != NULL) && (*argp[i] != 0))
657
		confp->irq = simple_strtoul(argp[i], NULL, 0);
658
	return 1;
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666
}

/*****************************************************************************/

/*
 *	Allocate a new board structure. Fill out the basic info in it.
 */

J
Jiri Slaby 已提交
667
static struct stlbrd *stl_allocbrd(void)
L
Linus Torvalds 已提交
668
{
J
Jiri Slaby 已提交
669
	struct stlbrd	*brdp;
L
Linus Torvalds 已提交
670

J
Jiri Slaby 已提交
671
	brdp = kzalloc(sizeof(struct stlbrd), GFP_KERNEL);
672
	if (!brdp) {
A
Alan Cox 已提交
673
		printk("STALLION: failed to allocate memory (size=%Zd)\n",
J
Jiri Slaby 已提交
674
			sizeof(struct stlbrd));
675
		return NULL;
L
Linus Torvalds 已提交
676 677 678
	}

	brdp->magic = STL_BOARDMAGIC;
679
	return brdp;
L
Linus Torvalds 已提交
680 681 682 683 684 685
}

/*****************************************************************************/

static int stl_open(struct tty_struct *tty, struct file *filp)
{
J
Jiri Slaby 已提交
686 687
	struct stlport	*portp;
	struct stlbrd	*brdp;
688 689
	unsigned int	minordev, brdnr, panelnr;
	int		portnr, rc;
L
Linus Torvalds 已提交
690

691
	pr_debug("stl_open(tty=%p,filp=%p): device=%s\n", tty, filp, tty->name);
L
Linus Torvalds 已提交
692 693 694 695

	minordev = tty->index;
	brdnr = MINOR2BRD(minordev);
	if (brdnr >= stl_nrbrds)
696
		return -ENODEV;
L
Linus Torvalds 已提交
697
	brdp = stl_brds[brdnr];
698
	if (brdp == NULL)
699
		return -ENODEV;
L
Linus Torvalds 已提交
700
	minordev = MINOR2PORT(minordev);
701
	for (portnr = -1, panelnr = 0; panelnr < STL_MAXPANELS; panelnr++) {
702
		if (brdp->panels[panelnr] == NULL)
L
Linus Torvalds 已提交
703 704 705 706 707 708 709 710
			break;
		if (minordev < brdp->panels[panelnr]->nrports) {
			portnr = minordev;
			break;
		}
		minordev -= brdp->panels[panelnr]->nrports;
	}
	if (portnr < 0)
711
		return -ENODEV;
L
Linus Torvalds 已提交
712 713

	portp = brdp->panels[panelnr]->ports[portnr];
714
	if (portp == NULL)
715
		return -ENODEV;
L
Linus Torvalds 已提交
716 717 718 719 720 721 722 723 724 725

/*
 *	On the first open of the device setup the port hardware, and
 *	initialize the per port data structure.
 */
	portp->tty = tty;
	tty->driver_data = portp;
	portp->refcount++;

	if ((portp->flags & ASYNC_INITIALIZED) == 0) {
726 727 728
		if (!portp->tx.buf) {
			portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
			if (!portp->tx.buf)
729
				return -ENOMEM;
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
			portp->tx.head = portp->tx.buf;
			portp->tx.tail = portp->tx.buf;
		}
		stl_setport(portp, tty->termios);
		portp->sigs = stl_getsignals(portp);
		stl_setsignals(portp, 1, 1);
		stl_enablerxtx(portp, 1, 1);
		stl_startrxtx(portp, 1, 0);
		clear_bit(TTY_IO_ERROR, &tty->flags);
		portp->flags |= ASYNC_INITIALIZED;
	}

/*
 *	Check if this port is in the middle of closing. If so then wait
 *	until it is closed then return error status, based on flag settings.
 *	The sleep here does not need interrupt protection since the wakeup
 *	for it is done with the same context.
 */
	if (portp->flags & ASYNC_CLOSING) {
		interruptible_sleep_on(&portp->close_wait);
		if (portp->flags & ASYNC_HUP_NOTIFY)
751 752
			return -EAGAIN;
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
753 754 755 756 757 758 759
	}

/*
 *	Based on type of open being done check if it can overlap with any
 *	previous opens still in effect. If we are a normal serial device
 *	then also we might have to wait for carrier.
 */
760
	if (!(filp->f_flags & O_NONBLOCK))
L
Linus Torvalds 已提交
761
		if ((rc = stl_waitcarrier(portp, filp)) != 0)
762
			return rc;
763

L
Linus Torvalds 已提交
764 765
	portp->flags |= ASYNC_NORMAL_ACTIVE;

766
	return 0;
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775
}

/*****************************************************************************/

/*
 *	Possibly need to wait for carrier (DCD signal) to come high. Say
 *	maybe because if we are clocal then we don't need to wait...
 */

J
Jiri Slaby 已提交
776
static int stl_waitcarrier(struct stlport *portp, struct file *filp)
L
Linus Torvalds 已提交
777 778 779 780
{
	unsigned long	flags;
	int		rc, doclocal;

781
	pr_debug("stl_waitcarrier(portp=%p,filp=%p)\n", portp, filp);
L
Linus Torvalds 已提交
782 783 784 785

	rc = 0;
	doclocal = 0;

A
Alan Cox 已提交
786 787
	spin_lock_irqsave(&stallion_lock, flags);

L
Linus Torvalds 已提交
788 789 790 791 792 793 794 795
	if (portp->tty->termios->c_cflag & CLOCAL)
		doclocal++;

	portp->openwaitcnt++;
	if (! tty_hung_up_p(filp))
		portp->refcount--;

	for (;;) {
A
Alan Cox 已提交
796
		/* Takes brd_lock internally */
L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804 805 806
		stl_setsignals(portp, 1, 1);
		if (tty_hung_up_p(filp) ||
		    ((portp->flags & ASYNC_INITIALIZED) == 0)) {
			if (portp->flags & ASYNC_HUP_NOTIFY)
				rc = -EBUSY;
			else
				rc = -ERESTARTSYS;
			break;
		}
		if (((portp->flags & ASYNC_CLOSING) == 0) &&
807
		    (doclocal || (portp->sigs & TIOCM_CD)))
L
Linus Torvalds 已提交
808 809 810 811 812
			break;
		if (signal_pending(current)) {
			rc = -ERESTARTSYS;
			break;
		}
A
Alan Cox 已提交
813
		/* FIXME */
L
Linus Torvalds 已提交
814 815 816 817 818 819
		interruptible_sleep_on(&portp->open_wait);
	}

	if (! tty_hung_up_p(filp))
		portp->refcount++;
	portp->openwaitcnt--;
A
Alan Cox 已提交
820
	spin_unlock_irqrestore(&stallion_lock, flags);
L
Linus Torvalds 已提交
821

822
	return rc;
L
Linus Torvalds 已提交
823 824 825 826
}

/*****************************************************************************/

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
static void stl_flushbuffer(struct tty_struct *tty)
{
	struct stlport	*portp;

	pr_debug("stl_flushbuffer(tty=%p)\n", tty);

	if (tty == NULL)
		return;
	portp = tty->driver_data;
	if (portp == NULL)
		return;

	stl_flush(portp);
	tty_wakeup(tty);
}

/*****************************************************************************/

static void stl_waituntilsent(struct tty_struct *tty, int timeout)
{
	struct stlport	*portp;
	unsigned long	tend;

	pr_debug("stl_waituntilsent(tty=%p,timeout=%d)\n", tty, timeout);

	if (tty == NULL)
		return;
	portp = tty->driver_data;
	if (portp == NULL)
		return;

	if (timeout == 0)
		timeout = HZ;
	tend = jiffies + timeout;

	while (stl_datastate(portp)) {
		if (signal_pending(current))
			break;
		msleep_interruptible(20);
		if (time_after_eq(jiffies, tend))
			break;
	}
}

/*****************************************************************************/

L
Linus Torvalds 已提交
873 874
static void stl_close(struct tty_struct *tty, struct file *filp)
{
J
Jiri Slaby 已提交
875
	struct stlport	*portp;
L
Linus Torvalds 已提交
876 877
	unsigned long	flags;

878
	pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);
L
Linus Torvalds 已提交
879 880

	portp = tty->driver_data;
881
	if (portp == NULL)
L
Linus Torvalds 已提交
882 883
		return;

A
Alan Cox 已提交
884
	spin_lock_irqsave(&stallion_lock, flags);
L
Linus Torvalds 已提交
885
	if (tty_hung_up_p(filp)) {
A
Alan Cox 已提交
886
		spin_unlock_irqrestore(&stallion_lock, flags);
L
Linus Torvalds 已提交
887 888 889 890 891
		return;
	}
	if ((tty->count == 1) && (portp->refcount != 1))
		portp->refcount = 1;
	if (portp->refcount-- > 1) {
A
Alan Cox 已提交
892
		spin_unlock_irqrestore(&stallion_lock, flags);
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905
		return;
	}

	portp->refcount = 0;
	portp->flags |= ASYNC_CLOSING;

/*
 *	May want to wait for any data to drain before closing. The BUSY
 *	flag keeps track of whether we are still sending or not - it is
 *	very accurate for the cd1400, not quite so for the sc26198.
 *	(The sc26198 has no "end-of-data" interrupt only empty FIFO)
 */
	tty->closing = 1;
A
Alan Cox 已提交
906 907 908

	spin_unlock_irqrestore(&stallion_lock, flags);

L
Linus Torvalds 已提交
909 910 911 912
	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
		tty_wait_until_sent(tty, portp->closing_wait);
	stl_waituntilsent(tty, (HZ / 2));

A
Alan Cox 已提交
913 914

	spin_lock_irqsave(&stallion_lock, flags);
L
Linus Torvalds 已提交
915
	portp->flags &= ~ASYNC_INITIALIZED;
A
Alan Cox 已提交
916 917
	spin_unlock_irqrestore(&stallion_lock, flags);

L
Linus Torvalds 已提交
918 919 920 921 922 923
	stl_disableintrs(portp);
	if (tty->termios->c_cflag & HUPCL)
		stl_setsignals(portp, 0, 0);
	stl_enablerxtx(portp, 0, 0);
	stl_flushbuffer(tty);
	portp->istate = 0;
924
	if (portp->tx.buf != NULL) {
L
Linus Torvalds 已提交
925
		kfree(portp->tx.buf);
926 927 928
		portp->tx.buf = NULL;
		portp->tx.head = NULL;
		portp->tx.tail = NULL;
L
Linus Torvalds 已提交
929 930 931 932 933
	}
	set_bit(TTY_IO_ERROR, &tty->flags);
	tty_ldisc_flush(tty);

	tty->closing = 0;
934
	portp->tty = NULL;
L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954

	if (portp->openwaitcnt) {
		if (portp->close_delay)
			msleep_interruptible(jiffies_to_msecs(portp->close_delay));
		wake_up_interruptible(&portp->open_wait);
	}

	portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
	wake_up_interruptible(&portp->close_wait);
}

/*****************************************************************************/

/*
 *	Write routine. Take data and stuff it in to the TX ring queue.
 *	If transmit interrupts are not running then start them.
 */

static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
J
Jiri Slaby 已提交
955
	struct stlport	*portp;
L
Linus Torvalds 已提交
956 957 958 959
	unsigned int	len, stlen;
	unsigned char	*chbuf;
	char		*head, *tail;

960
	pr_debug("stl_write(tty=%p,buf=%p,count=%d)\n", tty, buf, count);
L
Linus Torvalds 已提交
961 962

	portp = tty->driver_data;
963
	if (portp == NULL)
964
		return 0;
965
	if (portp->tx.buf == NULL)
966
		return 0;
L
Linus Torvalds 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

/*
 *	If copying direct from user space we must cater for page faults,
 *	causing us to "sleep" here for a while. To handle this copy in all
 *	the data we need now, into a local buffer. Then when we got it all
 *	copy it into the TX buffer.
 */
	chbuf = (unsigned char *) buf;

	head = portp->tx.head;
	tail = portp->tx.tail;
	if (head >= tail) {
		len = STL_TXBUFSIZE - (head - tail) - 1;
		stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
	} else {
		len = tail - head - 1;
		stlen = len;
	}

986
	len = min(len, (unsigned int)count);
L
Linus Torvalds 已提交
987 988
	count = 0;
	while (len > 0) {
989
		stlen = min(len, stlen);
L
Linus Torvalds 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
		memcpy(head, chbuf, stlen);
		len -= stlen;
		chbuf += stlen;
		count += stlen;
		head += stlen;
		if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
			head = portp->tx.buf;
			stlen = tail - head;
		}
	}
	portp->tx.head = head;

	clear_bit(ASYI_TXLOW, &portp->istate);
	stl_startrxtx(portp, -1, 1);

1005
	return count;
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011
}

/*****************************************************************************/

static void stl_putchar(struct tty_struct *tty, unsigned char ch)
{
J
Jiri Slaby 已提交
1012
	struct stlport	*portp;
L
Linus Torvalds 已提交
1013 1014 1015
	unsigned int	len;
	char		*head, *tail;

1016
	pr_debug("stl_putchar(tty=%p,ch=%x)\n", tty, ch);
L
Linus Torvalds 已提交
1017

1018
	if (tty == NULL)
L
Linus Torvalds 已提交
1019 1020
		return;
	portp = tty->driver_data;
1021
	if (portp == NULL)
L
Linus Torvalds 已提交
1022
		return;
1023
	if (portp->tx.buf == NULL)
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
		return;

	head = portp->tx.head;
	tail = portp->tx.tail;

	len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
	len--;

	if (len > 0) {
		*head++ = ch;
		if (head >= (portp->tx.buf + STL_TXBUFSIZE))
			head = portp->tx.buf;
	}	
	portp->tx.head = head;
}

/*****************************************************************************/

/*
 *	If there are any characters in the buffer then make sure that TX
 *	interrupts are on and get'em out. Normally used after the putchar
 *	routine has been called.
 */

static void stl_flushchars(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1050
	struct stlport	*portp;
L
Linus Torvalds 已提交
1051

1052
	pr_debug("stl_flushchars(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1053

1054
	if (tty == NULL)
L
Linus Torvalds 已提交
1055 1056
		return;
	portp = tty->driver_data;
1057
	if (portp == NULL)
L
Linus Torvalds 已提交
1058
		return;
1059
	if (portp->tx.buf == NULL)
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068
		return;

	stl_startrxtx(portp, -1, 1);
}

/*****************************************************************************/

static int stl_writeroom(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1069
	struct stlport	*portp;
L
Linus Torvalds 已提交
1070 1071
	char		*head, *tail;

1072
	pr_debug("stl_writeroom(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1073

1074
	if (tty == NULL)
1075
		return 0;
L
Linus Torvalds 已提交
1076
	portp = tty->driver_data;
1077
	if (portp == NULL)
1078
		return 0;
1079
	if (portp->tx.buf == NULL)
1080
		return 0;
L
Linus Torvalds 已提交
1081 1082 1083

	head = portp->tx.head;
	tail = portp->tx.tail;
1084
	return (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1);
L
Linus Torvalds 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
}

/*****************************************************************************/

/*
 *	Return number of chars in the TX buffer. Normally we would just
 *	calculate the number of chars in the buffer and return that, but if
 *	the buffer is empty and TX interrupts are still on then we return
 *	that the buffer still has 1 char in it. This way whoever called us
 *	will not think that ALL chars have drained - since the UART still
 *	must have some chars in it (we are busy after all).
 */

static int stl_charsinbuffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1100
	struct stlport	*portp;
L
Linus Torvalds 已提交
1101 1102 1103
	unsigned int	size;
	char		*head, *tail;

1104
	pr_debug("stl_charsinbuffer(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1105

1106
	if (tty == NULL)
1107
		return 0;
L
Linus Torvalds 已提交
1108
	portp = tty->driver_data;
1109
	if (portp == NULL)
1110
		return 0;
1111
	if (portp->tx.buf == NULL)
1112
		return 0;
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117 1118

	head = portp->tx.head;
	tail = portp->tx.tail;
	size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
	if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
		size = 1;
1119
	return size;
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125 1126 1127
}

/*****************************************************************************/

/*
 *	Generate the serial struct info.
 */

J
Jiri Slaby 已提交
1128
static int stl_getserial(struct stlport *portp, struct serial_struct __user *sp)
L
Linus Torvalds 已提交
1129 1130
{
	struct serial_struct	sio;
J
Jiri Slaby 已提交
1131
	struct stlbrd		*brdp;
L
Linus Torvalds 已提交
1132

1133
	pr_debug("stl_getserial(portp=%p,sp=%p)\n", portp, sp);
L
Linus Torvalds 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	memset(&sio, 0, sizeof(struct serial_struct));
	sio.line = portp->portnr;
	sio.port = portp->ioaddr;
	sio.flags = portp->flags;
	sio.baud_base = portp->baud_base;
	sio.close_delay = portp->close_delay;
	sio.closing_wait = portp->closing_wait;
	sio.custom_divisor = portp->custom_divisor;
	sio.hub6 = 0;
	if (portp->uartp == &stl_cd1400uart) {
		sio.type = PORT_CIRRUS;
		sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
	} else {
		sio.type = PORT_UNKNOWN;
		sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
	}

	brdp = stl_brds[portp->brdnr];
1153
	if (brdp != NULL)
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		sio.irq = brdp->irq;

	return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
}

/*****************************************************************************/

/*
 *	Set port according to the serial struct info.
 *	At this point we do not do any auto-configure stuff, so we will
 *	just quietly ignore any requests to change irq, etc.
 */

J
Jiri Slaby 已提交
1167
static int stl_setserial(struct stlport *portp, struct serial_struct __user *sp)
L
Linus Torvalds 已提交
1168 1169 1170
{
	struct serial_struct	sio;

1171
	pr_debug("stl_setserial(portp=%p,sp=%p)\n", portp, sp);
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179

	if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
		return -EFAULT;
	if (!capable(CAP_SYS_ADMIN)) {
		if ((sio.baud_base != portp->baud_base) ||
		    (sio.close_delay != portp->close_delay) ||
		    ((sio.flags & ~ASYNC_USR_MASK) !=
		    (portp->flags & ~ASYNC_USR_MASK)))
1180
			return -EPERM;
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189
	} 

	portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
		(sio.flags & ASYNC_USR_MASK);
	portp->baud_base = sio.baud_base;
	portp->close_delay = sio.close_delay;
	portp->closing_wait = sio.closing_wait;
	portp->custom_divisor = sio.custom_divisor;
	stl_setport(portp, portp->tty->termios);
1190
	return 0;
L
Linus Torvalds 已提交
1191 1192 1193 1194 1195 1196
}

/*****************************************************************************/

static int stl_tiocmget(struct tty_struct *tty, struct file *file)
{
J
Jiri Slaby 已提交
1197
	struct stlport	*portp;
L
Linus Torvalds 已提交
1198

1199
	if (tty == NULL)
1200
		return -ENODEV;
L
Linus Torvalds 已提交
1201
	portp = tty->driver_data;
1202
	if (portp == NULL)
1203
		return -ENODEV;
L
Linus Torvalds 已提交
1204
	if (tty->flags & (1 << TTY_IO_ERROR))
1205
		return -EIO;
L
Linus Torvalds 已提交
1206 1207 1208 1209 1210 1211 1212

	return stl_getsignals(portp);
}

static int stl_tiocmset(struct tty_struct *tty, struct file *file,
			unsigned int set, unsigned int clear)
{
J
Jiri Slaby 已提交
1213
	struct stlport	*portp;
L
Linus Torvalds 已提交
1214 1215
	int rts = -1, dtr = -1;

1216
	if (tty == NULL)
1217
		return -ENODEV;
L
Linus Torvalds 已提交
1218
	portp = tty->driver_data;
1219
	if (portp == NULL)
1220
		return -ENODEV;
L
Linus Torvalds 已提交
1221
	if (tty->flags & (1 << TTY_IO_ERROR))
1222
		return -EIO;
L
Linus Torvalds 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238

	if (set & TIOCM_RTS)
		rts = 1;
	if (set & TIOCM_DTR)
		dtr = 1;
	if (clear & TIOCM_RTS)
		rts = 0;
	if (clear & TIOCM_DTR)
		dtr = 0;

	stl_setsignals(portp, dtr, rts);
	return 0;
}

static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
{
J
Jiri Slaby 已提交
1239
	struct stlport	*portp;
L
Linus Torvalds 已提交
1240 1241 1242 1243
	unsigned int	ival;
	int		rc;
	void __user *argp = (void __user *)arg;

1244 1245
	pr_debug("stl_ioctl(tty=%p,file=%p,cmd=%x,arg=%lx)\n", tty, file, cmd,
			arg);
L
Linus Torvalds 已提交
1246

1247
	if (tty == NULL)
1248
		return -ENODEV;
L
Linus Torvalds 已提交
1249
	portp = tty->driver_data;
1250
	if (portp == NULL)
1251
		return -ENODEV;
L
Linus Torvalds 已提交
1252 1253

	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1254
 	    (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS))
L
Linus Torvalds 已提交
1255
		if (tty->flags & (1 << TTY_IO_ERROR))
1256
			return -EIO;
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

	rc = 0;

	switch (cmd) {
	case TIOCGSOFTCAR:
		rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
			(unsigned __user *) argp);
		break;
	case TIOCSSOFTCAR:
		if (get_user(ival, (unsigned int __user *) arg))
			return -EFAULT;
		tty->termios->c_cflag =
				(tty->termios->c_cflag & ~CLOCAL) |
				(ival ? CLOCAL : 0);
		break;
	case TIOCGSERIAL:
		rc = stl_getserial(portp, argp);
		break;
	case TIOCSSERIAL:
		rc = stl_setserial(portp, argp);
		break;
	case COM_GETPORTSTATS:
		rc = stl_getportstats(portp, argp);
		break;
	case COM_CLRPORTSTATS:
		rc = stl_clrportstats(portp, argp);
		break;
	case TIOCSERCONFIG:
	case TIOCSERGWILD:
	case TIOCSERSWILD:
	case TIOCSERGETLSR:
	case TIOCSERGSTRUCT:
	case TIOCSERGETMULTI:
	case TIOCSERSETMULTI:
	default:
		rc = -ENOIOCTLCMD;
		break;
	}

1296
	return rc;
L
Linus Torvalds 已提交
1297 1298 1299 1300
}

/*****************************************************************************/

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
/*
 *	Start the transmitter again. Just turn TX interrupts back on.
 */

static void stl_start(struct tty_struct *tty)
{
	struct stlport	*portp;

	pr_debug("stl_start(tty=%p)\n", tty);

	if (tty == NULL)
		return;
	portp = tty->driver_data;
	if (portp == NULL)
		return;
	stl_startrxtx(portp, -1, 1);
}

/*****************************************************************************/

A
Alan Cox 已提交
1321
static void stl_settermios(struct tty_struct *tty, struct ktermios *old)
L
Linus Torvalds 已提交
1322
{
J
Jiri Slaby 已提交
1323
	struct stlport	*portp;
A
Alan Cox 已提交
1324
	struct ktermios	*tiosp;
L
Linus Torvalds 已提交
1325

1326
	pr_debug("stl_settermios(tty=%p,old=%p)\n", tty, old);
L
Linus Torvalds 已提交
1327

1328
	if (tty == NULL)
L
Linus Torvalds 已提交
1329 1330
		return;
	portp = tty->driver_data;
1331
	if (portp == NULL)
L
Linus Torvalds 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		return;

	tiosp = tty->termios;
	if ((tiosp->c_cflag == old->c_cflag) &&
	    (tiosp->c_iflag == old->c_iflag))
		return;

	stl_setport(portp, tiosp);
	stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
		-1);
	if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
		tty->hw_stopped = 0;
		stl_start(tty);
	}
	if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
		wake_up_interruptible(&portp->open_wait);
}

/*****************************************************************************/

/*
 *	Attempt to flow control who ever is sending us data. Based on termios
 *	settings use software or/and hardware flow control.
 */

static void stl_throttle(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1359
	struct stlport	*portp;
L
Linus Torvalds 已提交
1360

1361
	pr_debug("stl_throttle(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1362

1363
	if (tty == NULL)
L
Linus Torvalds 已提交
1364 1365
		return;
	portp = tty->driver_data;
1366
	if (portp == NULL)
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
		return;
	stl_flowctrl(portp, 0);
}

/*****************************************************************************/

/*
 *	Unflow control the device sending us data...
 */

static void stl_unthrottle(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1379
	struct stlport	*portp;
L
Linus Torvalds 已提交
1380

1381
	pr_debug("stl_unthrottle(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1382

1383
	if (tty == NULL)
L
Linus Torvalds 已提交
1384 1385
		return;
	portp = tty->driver_data;
1386
	if (portp == NULL)
L
Linus Torvalds 已提交
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
		return;
	stl_flowctrl(portp, 1);
}

/*****************************************************************************/

/*
 *	Stop the transmitter. Basically to do this we will just turn TX
 *	interrupts off.
 */

static void stl_stop(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1400
	struct stlport	*portp;
L
Linus Torvalds 已提交
1401

1402
	pr_debug("stl_stop(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1403

1404
	if (tty == NULL)
L
Linus Torvalds 已提交
1405 1406
		return;
	portp = tty->driver_data;
1407
	if (portp == NULL)
L
Linus Torvalds 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
		return;
	stl_startrxtx(portp, -1, 0);
}

/*****************************************************************************/

/*
 *	Hangup this port. This is pretty much like closing the port, only
 *	a little more brutal. No waiting for data to drain. Shutdown the
 *	port and maybe drop signals.
 */

static void stl_hangup(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1422
	struct stlport	*portp;
L
Linus Torvalds 已提交
1423

1424
	pr_debug("stl_hangup(tty=%p)\n", tty);
L
Linus Torvalds 已提交
1425

1426
	if (tty == NULL)
L
Linus Torvalds 已提交
1427 1428
		return;
	portp = tty->driver_data;
1429
	if (portp == NULL)
L
Linus Torvalds 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
		return;

	portp->flags &= ~ASYNC_INITIALIZED;
	stl_disableintrs(portp);
	if (tty->termios->c_cflag & HUPCL)
		stl_setsignals(portp, 0, 0);
	stl_enablerxtx(portp, 0, 0);
	stl_flushbuffer(tty);
	portp->istate = 0;
	set_bit(TTY_IO_ERROR, &tty->flags);
1440
	if (portp->tx.buf != NULL) {
L
Linus Torvalds 已提交
1441
		kfree(portp->tx.buf);
1442 1443 1444
		portp->tx.buf = NULL;
		portp->tx.head = NULL;
		portp->tx.tail = NULL;
L
Linus Torvalds 已提交
1445
	}
1446
	portp->tty = NULL;
L
Linus Torvalds 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455
	portp->flags &= ~ASYNC_NORMAL_ACTIVE;
	portp->refcount = 0;
	wake_up_interruptible(&portp->open_wait);
}

/*****************************************************************************/

static void stl_breakctl(struct tty_struct *tty, int state)
{
J
Jiri Slaby 已提交
1456
	struct stlport	*portp;
L
Linus Torvalds 已提交
1457

1458
	pr_debug("stl_breakctl(tty=%p,state=%d)\n", tty, state);
L
Linus Torvalds 已提交
1459

1460
	if (tty == NULL)
L
Linus Torvalds 已提交
1461 1462
		return;
	portp = tty->driver_data;
1463
	if (portp == NULL)
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472
		return;

	stl_sendbreak(portp, ((state == -1) ? 1 : 2));
}

/*****************************************************************************/

static void stl_sendxchar(struct tty_struct *tty, char ch)
{
J
Jiri Slaby 已提交
1473
	struct stlport	*portp;
L
Linus Torvalds 已提交
1474

1475
	pr_debug("stl_sendxchar(tty=%p,ch=%x)\n", tty, ch);
L
Linus Torvalds 已提交
1476

1477
	if (tty == NULL)
L
Linus Torvalds 已提交
1478 1479
		return;
	portp = tty->driver_data;
1480
	if (portp == NULL)
L
Linus Torvalds 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
		return;

	if (ch == STOP_CHAR(tty))
		stl_sendflow(portp, 0);
	else if (ch == START_CHAR(tty))
		stl_sendflow(portp, 1);
	else
		stl_putchar(tty, ch);
}

/*****************************************************************************/

#define	MAXLINE		80

/*
 *	Format info for a specified port. The line is deliberately limited
 *	to 80 characters. (If it is too long it will be truncated, if too
 *	short then padded with spaces).
 */

J
Jiri Slaby 已提交
1501
static int stl_portinfo(struct stlport *portp, int portnr, char *pos)
L
Linus Torvalds 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
{
	char	*sp;
	int	sigs, cnt;

	sp = pos;
	sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
		portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
		(int) portp->stats.txtotal, (int) portp->stats.rxtotal);

	if (portp->stats.rxframing)
		sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
	if (portp->stats.rxparity)
		sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
	if (portp->stats.rxbreaks)
		sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
	if (portp->stats.rxoverrun)
		sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);

	sigs = stl_getsignals(portp);
	cnt = sprintf(sp, "%s%s%s%s%s ",
		(sigs & TIOCM_RTS) ? "|RTS" : "",
		(sigs & TIOCM_CTS) ? "|CTS" : "",
		(sigs & TIOCM_DTR) ? "|DTR" : "",
		(sigs & TIOCM_CD) ? "|DCD" : "",
		(sigs & TIOCM_DSR) ? "|DSR" : "");
	*sp = ' ';
	sp += cnt;

1530
	for (cnt = sp - pos; cnt < (MAXLINE - 1); cnt++)
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535
		*sp++ = ' ';
	if (cnt >= MAXLINE)
		pos[(MAXLINE - 2)] = '+';
	pos[(MAXLINE - 1)] = '\n';

1536
	return MAXLINE;
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
}

/*****************************************************************************/

/*
 *	Port info, read from the /proc file system.
 */

static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
{
J
Jiri Slaby 已提交
1547 1548 1549
	struct stlbrd	*brdp;
	struct stlpanel	*panelp;
	struct stlport	*portp;
1550 1551
	unsigned int	brdnr, panelnr, portnr;
	int		totalport, curoff, maxoff;
L
Linus Torvalds 已提交
1552 1553
	char		*pos;

1554 1555
	pr_debug("stl_readproc(page=%p,start=%p,off=%lx,count=%d,eof=%p,"
		"data=%p\n", page, start, off, count, eof, data);
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573

	pos = page;
	totalport = 0;
	curoff = 0;

	if (off == 0) {
		pos += sprintf(pos, "%s: version %s", stl_drvtitle,
			stl_drvversion);
		while (pos < (page + MAXLINE - 1))
			*pos++ = ' ';
		*pos++ = '\n';
	}
	curoff =  MAXLINE;

/*
 *	We scan through for each board, panel and port. The offset is
 *	calculated on the fly, and irrelevant ports are skipped.
 */
1574
	for (brdnr = 0; brdnr < stl_nrbrds; brdnr++) {
L
Linus Torvalds 已提交
1575
		brdp = stl_brds[brdnr];
1576
		if (brdp == NULL)
L
Linus Torvalds 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
			continue;
		if (brdp->state == 0)
			continue;

		maxoff = curoff + (brdp->nrports * MAXLINE);
		if (off >= maxoff) {
			curoff = maxoff;
			continue;
		}

		totalport = brdnr * STL_MAXPORTS;
1588
		for (panelnr = 0; panelnr < brdp->nrpanels; panelnr++) {
L
Linus Torvalds 已提交
1589
			panelp = brdp->panels[panelnr];
1590
			if (panelp == NULL)
L
Linus Torvalds 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599
				continue;

			maxoff = curoff + (panelp->nrports * MAXLINE);
			if (off >= maxoff) {
				curoff = maxoff;
				totalport += panelp->nrports;
				continue;
			}

1600
			for (portnr = 0; portnr < panelp->nrports; portnr++,
L
Linus Torvalds 已提交
1601 1602
			    totalport++) {
				portp = panelp->ports[portnr];
1603
				if (portp == NULL)
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
					continue;
				if (off >= (curoff += MAXLINE))
					continue;
				if ((pos - page + MAXLINE) > count)
					goto stl_readdone;
				pos += stl_portinfo(portp, totalport, pos);
			}
		}
	}

	*eof = 1;

stl_readdone:
	*start = page;
1618
	return pos - page;
L
Linus Torvalds 已提交
1619 1620 1621 1622 1623 1624 1625 1626 1627
}

/*****************************************************************************/

/*
 *	All board interrupts are vectored through here first. This code then
 *	calls off to the approrpriate board interrupt handlers.
 */

1628
static irqreturn_t stl_intr(int irq, void *dev_id)
L
Linus Torvalds 已提交
1629
{
J
Jiri Slaby 已提交
1630
	struct stlbrd *brdp = dev_id;
L
Linus Torvalds 已提交
1631

1632
	pr_debug("stl_intr(brdp=%p,irq=%d)\n", brdp, irq);
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642

	return IRQ_RETVAL((* brdp->isr)(brdp));
}

/*****************************************************************************/

/*
 *	Interrupt service routine for EasyIO board types.
 */

J
Jiri Slaby 已提交
1643
static int stl_eiointr(struct stlbrd *brdp)
L
Linus Torvalds 已提交
1644
{
J
Jiri Slaby 已提交
1645
	struct stlpanel	*panelp;
L
Linus Torvalds 已提交
1646 1647 1648
	unsigned int	iobase;
	int		handled = 0;

A
Alan Cox 已提交
1649
	spin_lock(&brd_lock);
L
Linus Torvalds 已提交
1650 1651 1652 1653 1654 1655
	panelp = brdp->panels[0];
	iobase = panelp->iobase;
	while (inb(brdp->iostatus) & EIO_INTRPEND) {
		handled = 1;
		(* panelp->isr)(panelp, iobase);
	}
A
Alan Cox 已提交
1656
	spin_unlock(&brd_lock);
L
Linus Torvalds 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665
	return handled;
}

/*****************************************************************************/

/*
 *	Interrupt service routine for ECH-AT board types.
 */

J
Jiri Slaby 已提交
1666
static int stl_echatintr(struct stlbrd *brdp)
L
Linus Torvalds 已提交
1667
{
J
Jiri Slaby 已提交
1668
	struct stlpanel	*panelp;
1669
	unsigned int	ioaddr, bnknr;
L
Linus Torvalds 已提交
1670 1671 1672 1673 1674 1675
	int		handled = 0;

	outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);

	while (inb(brdp->iostatus) & ECH_INTRPEND) {
		handled = 1;
1676
		for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
L
Linus Torvalds 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
			ioaddr = brdp->bnkstataddr[bnknr];
			if (inb(ioaddr) & ECH_PNLINTRPEND) {
				panelp = brdp->bnk2panel[bnknr];
				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
			}
		}
	}

	outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);

	return handled;
}

/*****************************************************************************/

/*
 *	Interrupt service routine for ECH-MCA board types.
 */

J
Jiri Slaby 已提交
1696
static int stl_echmcaintr(struct stlbrd *brdp)
L
Linus Torvalds 已提交
1697
{
J
Jiri Slaby 已提交
1698
	struct stlpanel	*panelp;
1699
	unsigned int	ioaddr, bnknr;
L
Linus Torvalds 已提交
1700 1701 1702 1703
	int		handled = 0;

	while (inb(brdp->iostatus) & ECH_INTRPEND) {
		handled = 1;
1704
		for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
L
Linus Torvalds 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
			ioaddr = brdp->bnkstataddr[bnknr];
			if (inb(ioaddr) & ECH_PNLINTRPEND) {
				panelp = brdp->bnk2panel[bnknr];
				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
			}
		}
	}
	return handled;
}

/*****************************************************************************/

/*
 *	Interrupt service routine for ECH-PCI board types.
 */

J
Jiri Slaby 已提交
1721
static int stl_echpciintr(struct stlbrd *brdp)
L
Linus Torvalds 已提交
1722
{
J
Jiri Slaby 已提交
1723
	struct stlpanel	*panelp;
1724
	unsigned int	ioaddr, bnknr, recheck;
L
Linus Torvalds 已提交
1725 1726 1727 1728
	int		handled = 0;

	while (1) {
		recheck = 0;
1729
		for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
L
Linus Torvalds 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
			outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
			ioaddr = brdp->bnkstataddr[bnknr];
			if (inb(ioaddr) & ECH_PNLINTRPEND) {
				panelp = brdp->bnk2panel[bnknr];
				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
				recheck++;
				handled = 1;
			}
		}
		if (! recheck)
			break;
	}
	return handled;
}

/*****************************************************************************/

/*
 *	Interrupt service routine for ECH-8/64-PCI board types.
 */

J
Jiri Slaby 已提交
1751
static int stl_echpci64intr(struct stlbrd *brdp)
L
Linus Torvalds 已提交
1752
{
J
Jiri Slaby 已提交
1753
	struct stlpanel	*panelp;
1754
	unsigned int	ioaddr, bnknr;
L
Linus Torvalds 已提交
1755 1756 1757 1758
	int		handled = 0;

	while (inb(brdp->ioctrl) & 0x1) {
		handled = 1;
1759
		for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
L
Linus Torvalds 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
			ioaddr = brdp->bnkstataddr[bnknr];
			if (inb(ioaddr) & ECH_PNLINTRPEND) {
				panelp = brdp->bnk2panel[bnknr];
				(* panelp->isr)(panelp, (ioaddr & 0xfffc));
			}
		}
	}

	return handled;
}

/*****************************************************************************/

/*
 *	Service an off-level request for some channel.
 */
1776
static void stl_offintr(struct work_struct *work)
L
Linus Torvalds 已提交
1777
{
J
Jiri Slaby 已提交
1778
	struct stlport		*portp = container_of(work, struct stlport, tqueue);
L
Linus Torvalds 已提交
1779 1780 1781
	struct tty_struct	*tty;
	unsigned int		oldsigs;

1782
	pr_debug("stl_offintr(portp=%p)\n", portp);
L
Linus Torvalds 已提交
1783

1784
	if (portp == NULL)
L
Linus Torvalds 已提交
1785 1786 1787
		return;

	tty = portp->tty;
1788
	if (tty == NULL)
L
Linus Torvalds 已提交
1789 1790
		return;

1791
	if (test_bit(ASYI_TXLOW, &portp->istate))
L
Linus Torvalds 已提交
1792
		tty_wakeup(tty);
1793

L
Linus Torvalds 已提交
1794 1795 1796 1797 1798 1799
	if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
		clear_bit(ASYI_DCDCHANGE, &portp->istate);
		oldsigs = portp->sigs;
		portp->sigs = stl_getsignals(portp);
		if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
			wake_up_interruptible(&portp->open_wait);
1800
		if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0))
L
Linus Torvalds 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
			if (portp->flags & ASYNC_CHECK_CD)
				tty_hangup(tty);	/* FIXME: module removal race here - AKPM */
	}
}

/*****************************************************************************/

/*
 *	Initialize all the ports on a panel.
 */

1812
static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
L
Linus Torvalds 已提交
1813
{
1814 1815 1816
	struct stlport *portp;
	unsigned int i;
	int chipmask;
L
Linus Torvalds 已提交
1817

1818
	pr_debug("stl_initports(brdp=%p,panelp=%p)\n", brdp, panelp);
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825

	chipmask = stl_panelinit(brdp, panelp);

/*
 *	All UART's are initialized (if found!). Now go through and setup
 *	each ports data structures.
 */
1826
	for (i = 0; i < panelp->nrports; i++) {
J
Jiri Slaby 已提交
1827
		portp = kzalloc(sizeof(struct stlport), GFP_KERNEL);
1828
		if (!portp) {
L
Linus Torvalds 已提交
1829
			printk("STALLION: failed to allocate memory "
J
Jiri Slaby 已提交
1830
				"(size=%Zd)\n", sizeof(struct stlport));
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
			break;
		}

		portp->magic = STL_PORTMAGIC;
		portp->portnr = i;
		portp->brdnr = panelp->brdnr;
		portp->panelnr = panelp->panelnr;
		portp->uartp = panelp->uartp;
		portp->clk = brdp->clk;
		portp->baud_base = STL_BAUDBASE;
		portp->close_delay = STL_CLOSEDELAY;
		portp->closing_wait = 30 * HZ;
1843
		INIT_WORK(&portp->tqueue, stl_offintr);
L
Linus Torvalds 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852
		init_waitqueue_head(&portp->open_wait);
		init_waitqueue_head(&portp->close_wait);
		portp->stats.brd = portp->brdnr;
		portp->stats.panel = portp->panelnr;
		portp->stats.port = portp->portnr;
		panelp->ports[i] = portp;
		stl_portinit(brdp, panelp, portp);
	}

1853
	return 0;
L
Linus Torvalds 已提交
1854 1855
}

1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
static void stl_cleanup_panels(struct stlbrd *brdp)
{
	struct stlpanel *panelp;
	struct stlport *portp;
	unsigned int j, k;

	for (j = 0; j < STL_MAXPANELS; j++) {
		panelp = brdp->panels[j];
		if (panelp == NULL)
			continue;
		for (k = 0; k < STL_PORTSPERPANEL; k++) {
			portp = panelp->ports[k];
			if (portp == NULL)
				continue;
			if (portp->tty != NULL)
				stl_hangup(portp->tty);
			kfree(portp->tx.buf);
			kfree(portp);
		}
		kfree(panelp);
	}
}

L
Linus Torvalds 已提交
1879 1880 1881 1882 1883 1884
/*****************************************************************************/

/*
 *	Try to find and initialize an EasyIO board.
 */

1885
static int __devinit stl_initeio(struct stlbrd *brdp)
L
Linus Torvalds 已提交
1886
{
J
Jiri Slaby 已提交
1887
	struct stlpanel	*panelp;
L
Linus Torvalds 已提交
1888 1889
	unsigned int	status;
	char		*name;
1890
	int		retval;
L
Linus Torvalds 已提交
1891

1892
	pr_debug("stl_initeio(brdp=%p)\n", brdp);
L
Linus Torvalds 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916

	brdp->ioctrl = brdp->ioaddr1 + 1;
	brdp->iostatus = brdp->ioaddr1 + 2;

	status = inb(brdp->iostatus);
	if ((status & EIO_IDBITMASK) == EIO_MK3)
		brdp->ioctrl++;

/*
 *	Handle board specific stuff now. The real difference is PCI
 *	or not PCI.
 */
	if (brdp->brdtype == BRD_EASYIOPCI) {
		brdp->iosize1 = 0x80;
		brdp->iosize2 = 0x80;
		name = "serial(EIO-PCI)";
		outb(0x41, (brdp->ioaddr2 + 0x4c));
	} else {
		brdp->iosize1 = 8;
		name = "serial(EIO)";
		if ((brdp->irq < 0) || (brdp->irq > 15) ||
		    (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
			printk("STALLION: invalid irq=%d for brd=%d\n",
				brdp->irq, brdp->brdnr);
1917 1918
			retval = -EINVAL;
			goto err;
L
Linus Torvalds 已提交
1919 1920 1921 1922 1923 1924
		}
		outb((stl_vecmap[brdp->irq] | EIO_0WS |
			((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
			brdp->ioctrl);
	}

1925
	retval = -EBUSY;
L
Linus Torvalds 已提交
1926 1927 1928 1929
	if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
		printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
			"%x conflicts with another device\n", brdp->brdnr, 
			brdp->ioaddr1);
1930
		goto err;
L
Linus Torvalds 已提交
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
	}
	
	if (brdp->iosize2 > 0)
		if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
			printk(KERN_WARNING "STALLION: Warning, board %d I/O "
				"address %x conflicts with another device\n",
				brdp->brdnr, brdp->ioaddr2);
			printk(KERN_WARNING "STALLION: Warning, also "
				"releasing board %d I/O address %x \n", 
				brdp->brdnr, brdp->ioaddr1);
1941
			goto err_rel1;
L
Linus Torvalds 已提交
1942 1943 1944 1945 1946 1947 1948 1949
		}

/*
 *	Everything looks OK, so let's go ahead and probe for the hardware.
 */
	brdp->clk = CD1400_CLK;
	brdp->isr = stl_eiointr;

1950
	retval = -ENODEV;
L
Linus Torvalds 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
	switch (status & EIO_IDBITMASK) {
	case EIO_8PORTM:
		brdp->clk = CD1400_CLK8M;
		/* fall thru */
	case EIO_8PORTRS:
	case EIO_8PORTDI:
		brdp->nrports = 8;
		break;
	case EIO_4PORTRS:
		brdp->nrports = 4;
		break;
	case EIO_MK3:
		switch (status & EIO_BRDMASK) {
		case ID_BRD4:
			brdp->nrports = 4;
			break;
		case ID_BRD8:
			brdp->nrports = 8;
			break;
		case ID_BRD16:
			brdp->nrports = 16;
			break;
		default:
1974
			goto err_rel2;
L
Linus Torvalds 已提交
1975 1976 1977
		}
		break;
	default:
1978
		goto err_rel2;
L
Linus Torvalds 已提交
1979 1980 1981 1982 1983 1984 1985
	}

/*
 *	We have verified that the board is actually present, so now we
 *	can complete the setup.
 */

J
Jiri Slaby 已提交
1986
	panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
1987
	if (!panelp) {
L
Linus Torvalds 已提交
1988
		printk(KERN_WARNING "STALLION: failed to allocate memory "
J
Jiri Slaby 已提交
1989
			"(size=%Zd)\n", sizeof(struct stlpanel));
1990 1991
		retval = -ENOMEM;
		goto err_rel2;
L
Linus Torvalds 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000
	}

	panelp->magic = STL_PANELMAGIC;
	panelp->brdnr = brdp->brdnr;
	panelp->panelnr = 0;
	panelp->nrports = brdp->nrports;
	panelp->iobase = brdp->ioaddr1;
	panelp->hwid = status;
	if ((status & EIO_IDBITMASK) == EIO_MK3) {
2001
		panelp->uartp = &stl_sc26198uart;
L
Linus Torvalds 已提交
2002 2003
		panelp->isr = stl_sc26198intr;
	} else {
2004
		panelp->uartp = &stl_cd1400uart;
L
Linus Torvalds 已提交
2005 2006 2007 2008 2009 2010 2011
		panelp->isr = stl_cd1400eiointr;
	}

	brdp->panels[0] = panelp;
	brdp->nrpanels = 1;
	brdp->state |= BRD_FOUND;
	brdp->hwid = status;
2012
	if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
L
Linus Torvalds 已提交
2013 2014
		printk("STALLION: failed to register interrupt "
		    "routine for %s irq=%d\n", name, brdp->irq);
2015 2016
		retval = -ENODEV;
		goto err_fr;
L
Linus Torvalds 已提交
2017
	}
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028

	return 0;
err_fr:
	stl_cleanup_panels(brdp);
err_rel2:
	if (brdp->iosize2 > 0)
		release_region(brdp->ioaddr2, brdp->iosize2);
err_rel1:
	release_region(brdp->ioaddr1, brdp->iosize1);
err:
	return retval;
L
Linus Torvalds 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037
}

/*****************************************************************************/

/*
 *	Try to find an ECH board and initialize it. This code is capable of
 *	dealing with all types of ECH board.
 */

2038
static int __devinit stl_initech(struct stlbrd *brdp)
L
Linus Torvalds 已提交
2039
{
J
Jiri Slaby 已提交
2040
	struct stlpanel	*panelp;
2041 2042
	unsigned int	status, nxtid, ioaddr, conflict, panelnr, banknr, i;
	int		retval;
L
Linus Torvalds 已提交
2043 2044
	char		*name;

2045
	pr_debug("stl_initech(brdp=%p)\n", brdp);
L
Linus Torvalds 已提交
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

	status = 0;
	conflict = 0;

/*
 *	Set up the initial board register contents for boards. This varies a
 *	bit between the different board types. So we need to handle each
 *	separately. Also do a check that the supplied IRQ is good.
 */
	switch (brdp->brdtype) {

	case BRD_ECH:
		brdp->isr = stl_echatintr;
		brdp->ioctrl = brdp->ioaddr1 + 1;
		brdp->iostatus = brdp->ioaddr1 + 1;
		status = inb(brdp->iostatus);
2062 2063 2064 2065
		if ((status & ECH_IDBITMASK) != ECH_ID) {
			retval = -ENODEV;
			goto err;
		}
L
Linus Torvalds 已提交
2066 2067 2068 2069
		if ((brdp->irq < 0) || (brdp->irq > 15) ||
		    (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
			printk("STALLION: invalid irq=%d for brd=%d\n",
				brdp->irq, brdp->brdnr);
2070 2071
			retval = -EINVAL;
			goto err;
L
Linus Torvalds 已提交
2072 2073 2074 2075 2076 2077
		}
		status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
		status |= (stl_vecmap[brdp->irq] << 1);
		outb((status | ECH_BRDRESET), brdp->ioaddr1);
		brdp->ioctrlval = ECH_INTENABLE |
			((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2078
		for (i = 0; i < 10; i++)
L
Linus Torvalds 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
			outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
		brdp->iosize1 = 2;
		brdp->iosize2 = 32;
		name = "serial(EC8/32)";
		outb(status, brdp->ioaddr1);
		break;

	case BRD_ECHMC:
		brdp->isr = stl_echmcaintr;
		brdp->ioctrl = brdp->ioaddr1 + 0x20;
		brdp->iostatus = brdp->ioctrl;
		status = inb(brdp->iostatus);
2091 2092 2093 2094
		if ((status & ECH_IDBITMASK) != ECH_ID) {
			retval = -ENODEV;
			goto err;
		}
L
Linus Torvalds 已提交
2095 2096 2097 2098
		if ((brdp->irq < 0) || (brdp->irq > 15) ||
		    (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
			printk("STALLION: invalid irq=%d for brd=%d\n",
				brdp->irq, brdp->brdnr);
2099 2100
			retval = -EINVAL;
			goto err;
L
Linus Torvalds 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
		}
		outb(ECHMC_BRDRESET, brdp->ioctrl);
		outb(ECHMC_INTENABLE, brdp->ioctrl);
		brdp->iosize1 = 64;
		name = "serial(EC8/32-MC)";
		break;

	case BRD_ECHPCI:
		brdp->isr = stl_echpciintr;
		brdp->ioctrl = brdp->ioaddr1 + 2;
		brdp->iosize1 = 4;
		brdp->iosize2 = 8;
		name = "serial(EC8/32-PCI)";
		break;

	case BRD_ECH64PCI:
		brdp->isr = stl_echpci64intr;
		brdp->ioctrl = brdp->ioaddr2 + 0x40;
		outb(0x43, (brdp->ioaddr1 + 0x4c));
		brdp->iosize1 = 0x80;
		brdp->iosize2 = 0x80;
		name = "serial(EC8/64-PCI)";
		break;

	default:
		printk("STALLION: unknown board type=%d\n", brdp->brdtype);
2127 2128
		retval = -EINVAL;
		goto err;
L
Linus Torvalds 已提交
2129 2130 2131 2132 2133 2134
	}

/*
 *	Check boards for possible IO address conflicts and return fail status 
 * 	if an IO conflict found.
 */
2135
	retval = -EBUSY;
L
Linus Torvalds 已提交
2136 2137 2138 2139
	if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
		printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
			"%x conflicts with another device\n", brdp->brdnr, 
			brdp->ioaddr1);
2140
		goto err;
L
Linus Torvalds 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
	}
	
	if (brdp->iosize2 > 0)
		if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
			printk(KERN_WARNING "STALLION: Warning, board %d I/O "
				"address %x conflicts with another device\n",
				brdp->brdnr, brdp->ioaddr2);
			printk(KERN_WARNING "STALLION: Warning, also "
				"releasing board %d I/O address %x \n", 
				brdp->brdnr, brdp->ioaddr1);
2151
			goto err_rel1;
L
Linus Torvalds 已提交
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
		}

/*
 *	Scan through the secondary io address space looking for panels.
 *	As we find'em allocate and initialize panel structures for each.
 */
	brdp->clk = CD1400_CLK;
	brdp->hwid = status;

	ioaddr = brdp->ioaddr2;
	banknr = 0;
	panelnr = 0;
	nxtid = 0;

2166
	for (i = 0; i < STL_MAXPANELS; i++) {
L
Linus Torvalds 已提交
2167 2168 2169 2170 2171 2172
		if (brdp->brdtype == BRD_ECHPCI) {
			outb(nxtid, brdp->ioctrl);
			ioaddr = brdp->ioaddr2;
		}
		status = inb(ioaddr + ECH_PNLSTATUS);
		if ((status & ECH_PNLIDMASK) != nxtid)
2173
			break;
J
Jiri Slaby 已提交
2174
		panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
2175
		if (!panelp) {
L
Linus Torvalds 已提交
2176
			printk("STALLION: failed to allocate memory "
J
Jiri Slaby 已提交
2177
				"(size=%Zd)\n", sizeof(struct stlpanel));
2178
			retval = -ENOMEM;
2179
			goto err_fr;
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
		}
		panelp->magic = STL_PANELMAGIC;
		panelp->brdnr = brdp->brdnr;
		panelp->panelnr = panelnr;
		panelp->iobase = ioaddr;
		panelp->pagenr = nxtid;
		panelp->hwid = status;
		brdp->bnk2panel[banknr] = panelp;
		brdp->bnkpageaddr[banknr] = nxtid;
		brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;

		if (status & ECH_PNLXPID) {
2192
			panelp->uartp = &stl_sc26198uart;
L
Linus Torvalds 已提交
2193 2194 2195 2196 2197 2198 2199
			panelp->isr = stl_sc26198intr;
			if (status & ECH_PNL16PORT) {
				panelp->nrports = 16;
				brdp->bnk2panel[banknr] = panelp;
				brdp->bnkpageaddr[banknr] = nxtid;
				brdp->bnkstataddr[banknr++] = ioaddr + 4 +
					ECH_PNLSTATUS;
2200
			} else
L
Linus Torvalds 已提交
2201 2202
				panelp->nrports = 8;
		} else {
2203
			panelp->uartp = &stl_cd1400uart;
L
Linus Torvalds 已提交
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
			panelp->isr = stl_cd1400echintr;
			if (status & ECH_PNL16PORT) {
				panelp->nrports = 16;
				panelp->ackmask = 0x80;
				if (brdp->brdtype != BRD_ECHPCI)
					ioaddr += EREG_BANKSIZE;
				brdp->bnk2panel[banknr] = panelp;
				brdp->bnkpageaddr[banknr] = ++nxtid;
				brdp->bnkstataddr[banknr++] = ioaddr +
					ECH_PNLSTATUS;
			} else {
				panelp->nrports = 8;
				panelp->ackmask = 0xc0;
			}
		}

		nxtid++;
		ioaddr += EREG_BANKSIZE;
		brdp->nrports += panelp->nrports;
		brdp->panels[panelnr++] = panelp;
		if ((brdp->brdtype != BRD_ECHPCI) &&
2225 2226
		    (ioaddr >= (brdp->ioaddr2 + brdp->iosize2))) {
			retval = -EINVAL;
2227
			goto err_fr;
2228
		}
L
Linus Torvalds 已提交
2229 2230 2231 2232 2233 2234 2235 2236
	}

	brdp->nrpanels = panelnr;
	brdp->nrbnks = banknr;
	if (brdp->brdtype == BRD_ECH)
		outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);

	brdp->state |= BRD_FOUND;
2237
	if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
L
Linus Torvalds 已提交
2238 2239
		printk("STALLION: failed to register interrupt "
		    "routine for %s irq=%d\n", name, brdp->irq);
2240 2241
		retval = -ENODEV;
		goto err_fr;
L
Linus Torvalds 已提交
2242 2243
	}

2244 2245 2246 2247 2248 2249 2250 2251 2252
	return 0;
err_fr:
	stl_cleanup_panels(brdp);
	if (brdp->iosize2 > 0)
		release_region(brdp->ioaddr2, brdp->iosize2);
err_rel1:
	release_region(brdp->ioaddr1, brdp->iosize1);
err:
	return retval;
L
Linus Torvalds 已提交
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
}

/*****************************************************************************/

/*
 *	Initialize and configure the specified board.
 *	Scan through all the boards in the configuration and see what we
 *	can find. Handle EIO and the ECH boards a little differently here
 *	since the initial search and setup is very different.
 */

2264
static int __devinit stl_brdinit(struct stlbrd *brdp)
L
Linus Torvalds 已提交
2265
{
2266
	int i, retval;
L
Linus Torvalds 已提交
2267

2268
	pr_debug("stl_brdinit(brdp=%p)\n", brdp);
L
Linus Torvalds 已提交
2269 2270 2271 2272

	switch (brdp->brdtype) {
	case BRD_EASYIO:
	case BRD_EASYIOPCI:
2273 2274 2275
		retval = stl_initeio(brdp);
		if (retval)
			goto err;
L
Linus Torvalds 已提交
2276 2277 2278 2279 2280
		break;
	case BRD_ECH:
	case BRD_ECHMC:
	case BRD_ECHPCI:
	case BRD_ECH64PCI:
2281 2282 2283
		retval = stl_initech(brdp);
		if (retval)
			goto err;
L
Linus Torvalds 已提交
2284 2285 2286 2287
		break;
	default:
		printk("STALLION: board=%d is unknown board type=%d\n",
			brdp->brdnr, brdp->brdtype);
2288 2289
		retval = -ENODEV;
		goto err;
L
Linus Torvalds 已提交
2290 2291 2292 2293 2294 2295
	}

	if ((brdp->state & BRD_FOUND) == 0) {
		printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
			stl_brdnames[brdp->brdtype], brdp->brdnr,
			brdp->ioaddr1, brdp->irq);
2296
		goto err_free;
L
Linus Torvalds 已提交
2297 2298
	}

2299
	for (i = 0; i < STL_MAXPANELS; i++)
2300
		if (brdp->panels[i] != NULL)
L
Linus Torvalds 已提交
2301 2302 2303 2304 2305 2306
			stl_initports(brdp, brdp->panels[i]);

	printk("STALLION: %s found, board=%d io=%x irq=%d "
		"nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
		brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
		brdp->nrports);
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318

	return 0;
err_free:
	free_irq(brdp->irq, brdp);

	stl_cleanup_panels(brdp);

	release_region(brdp->ioaddr1, brdp->iosize1);
	if (brdp->iosize2 > 0)
		release_region(brdp->ioaddr2, brdp->iosize2);
err:
	return retval;
L
Linus Torvalds 已提交
2319 2320 2321 2322 2323 2324 2325 2326
}

/*****************************************************************************/

/*
 *	Find the next available board number that is free.
 */

2327
static int __devinit stl_getbrdnr(void)
L
Linus Torvalds 已提交
2328
{
2329
	unsigned int i;
L
Linus Torvalds 已提交
2330

2331
	for (i = 0; i < STL_MAXBRDS; i++)
2332
		if (stl_brds[i] == NULL) {
L
Linus Torvalds 已提交
2333 2334
			if (i >= stl_nrbrds)
				stl_nrbrds = i + 1;
2335
			return i;
L
Linus Torvalds 已提交
2336
		}
2337 2338

	return -1;
L
Linus Torvalds 已提交
2339 2340
}

2341
/*****************************************************************************/
L
Linus Torvalds 已提交
2342 2343 2344 2345 2346 2347
/*
 *	We have a Stallion board. Allocate a board structure and
 *	initialize it. Read its IO and IRQ resources from PCI
 *	configuration space.
 */

2348 2349
static int __devinit stl_pciprobe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
L
Linus Torvalds 已提交
2350
{
2351
	struct stlbrd *brdp;
2352
	unsigned int i, brdtype = ent->driver_data;
2353
	int brdnr, retval = -ENODEV;
L
Linus Torvalds 已提交
2354

2355
	if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2356
		goto err;
2357

2358 2359 2360 2361 2362 2363 2364 2365
	retval = pci_enable_device(pdev);
	if (retval)
		goto err;
	brdp = stl_allocbrd();
	if (brdp == NULL) {
		retval = -ENOMEM;
		goto err;
	}
2366
	mutex_lock(&stl_brdslock);
2367 2368
	brdnr = stl_getbrdnr();
	if (brdnr < 0) {
2369
		dev_err(&pdev->dev, "too many boards found, "
L
Linus Torvalds 已提交
2370
			"maximum supported %d\n", STL_MAXBRDS);
2371
		mutex_unlock(&stl_brdslock);
2372
		retval = -ENODEV;
2373
		goto err_fr;
L
Linus Torvalds 已提交
2374
	}
2375
	brdp->brdnr = (unsigned int)brdnr;
2376 2377 2378
	stl_brds[brdp->brdnr] = brdp;
	mutex_unlock(&stl_brdslock);

L
Linus Torvalds 已提交
2379
	brdp->brdtype = brdtype;
2380
	brdp->state |= STL_PROBED;
L
Linus Torvalds 已提交
2381 2382 2383 2384 2385 2386 2387

/*
 *	We have all resources from the board, so let's setup the actual
 *	board structure now.
 */
	switch (brdtype) {
	case BRD_ECHPCI:
2388 2389
		brdp->ioaddr2 = pci_resource_start(pdev, 0);
		brdp->ioaddr1 = pci_resource_start(pdev, 1);
L
Linus Torvalds 已提交
2390 2391
		break;
	case BRD_ECH64PCI:
2392 2393
		brdp->ioaddr2 = pci_resource_start(pdev, 2);
		brdp->ioaddr1 = pci_resource_start(pdev, 1);
L
Linus Torvalds 已提交
2394 2395
		break;
	case BRD_EASYIOPCI:
2396 2397
		brdp->ioaddr1 = pci_resource_start(pdev, 2);
		brdp->ioaddr2 = pci_resource_start(pdev, 1);
L
Linus Torvalds 已提交
2398 2399
		break;
	default:
2400
		dev_err(&pdev->dev, "unknown PCI board type=%u\n", brdtype);
L
Linus Torvalds 已提交
2401 2402 2403
		break;
	}

2404
	brdp->irq = pdev->irq;
2405 2406
	retval = stl_brdinit(brdp);
	if (retval)
2407
		goto err_null;
L
Linus Torvalds 已提交
2408

2409 2410
	pci_set_drvdata(pdev, brdp);

2411 2412 2413 2414
	for (i = 0; i < brdp->nrports; i++)
		tty_register_device(stl_serial,
				brdp->brdnr * STL_MAXPORTS + i, &pdev->dev);

2415
	return 0;
2416 2417
err_null:
	stl_brds[brdp->brdnr] = NULL;
2418 2419 2420 2421
err_fr:
	kfree(brdp);
err:
	return retval;
L
Linus Torvalds 已提交
2422 2423
}

2424
static void __devexit stl_pciremove(struct pci_dev *pdev)
L
Linus Torvalds 已提交
2425
{
2426
	struct stlbrd *brdp = pci_get_drvdata(pdev);
2427
	unsigned int i;
L
Linus Torvalds 已提交
2428

2429
	free_irq(brdp->irq, brdp);
L
Linus Torvalds 已提交
2430

2431
	stl_cleanup_panels(brdp);
L
Linus Torvalds 已提交
2432

2433 2434 2435
	release_region(brdp->ioaddr1, brdp->iosize1);
	if (brdp->iosize2 > 0)
		release_region(brdp->ioaddr2, brdp->iosize2);
L
Linus Torvalds 已提交
2436

2437 2438 2439 2440
	for (i = 0; i < brdp->nrports; i++)
		tty_unregister_device(stl_serial,
				brdp->brdnr * STL_MAXPORTS + i);

2441 2442
	stl_brds[brdp->brdnr] = NULL;
	kfree(brdp);
L
Linus Torvalds 已提交
2443 2444
}

2445 2446 2447 2448 2449 2450
static struct pci_driver stl_pcidriver = {
	.name = "stallion",
	.id_table = stl_pcibrds,
	.probe = stl_pciprobe,
	.remove = __devexit_p(stl_pciremove)
};
L
Linus Torvalds 已提交
2451 2452 2453 2454 2455 2456 2457 2458 2459

/*****************************************************************************/

/*
 *	Return the board stats structure to user app.
 */

static int stl_getbrdstats(combrd_t __user *bp)
{
2460
	combrd_t	stl_brdstats;
J
Jiri Slaby 已提交
2461 2462
	struct stlbrd	*brdp;
	struct stlpanel	*panelp;
2463
	unsigned int i;
L
Linus Torvalds 已提交
2464 2465 2466 2467

	if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
		return -EFAULT;
	if (stl_brdstats.brd >= STL_MAXBRDS)
2468
		return -ENODEV;
L
Linus Torvalds 已提交
2469
	brdp = stl_brds[stl_brdstats.brd];
2470
	if (brdp == NULL)
2471
		return -ENODEV;
L
Linus Torvalds 已提交
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482

	memset(&stl_brdstats, 0, sizeof(combrd_t));
	stl_brdstats.brd = brdp->brdnr;
	stl_brdstats.type = brdp->brdtype;
	stl_brdstats.hwid = brdp->hwid;
	stl_brdstats.state = brdp->state;
	stl_brdstats.ioaddr = brdp->ioaddr1;
	stl_brdstats.ioaddr2 = brdp->ioaddr2;
	stl_brdstats.irq = brdp->irq;
	stl_brdstats.nrpanels = brdp->nrpanels;
	stl_brdstats.nrports = brdp->nrports;
2483
	for (i = 0; i < brdp->nrpanels; i++) {
L
Linus Torvalds 已提交
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
		panelp = brdp->panels[i];
		stl_brdstats.panels[i].panel = i;
		stl_brdstats.panels[i].hwid = panelp->hwid;
		stl_brdstats.panels[i].nrports = panelp->nrports;
	}

	return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
}

/*****************************************************************************/

/*
 *	Resolve the referenced port number into a port struct pointer.
 */

J
Jiri Slaby 已提交
2499
static struct stlport *stl_getport(int brdnr, int panelnr, int portnr)
L
Linus Torvalds 已提交
2500
{
J
Jiri Slaby 已提交
2501 2502
	struct stlbrd	*brdp;
	struct stlpanel	*panelp;
L
Linus Torvalds 已提交
2503

2504 2505
	if (brdnr < 0 || brdnr >= STL_MAXBRDS)
		return NULL;
L
Linus Torvalds 已提交
2506
	brdp = stl_brds[brdnr];
2507
	if (brdp == NULL)
2508
		return NULL;
2509
	if (panelnr < 0 || (unsigned int)panelnr >= brdp->nrpanels)
2510
		return NULL;
L
Linus Torvalds 已提交
2511
	panelp = brdp->panels[panelnr];
2512
	if (panelp == NULL)
2513
		return NULL;
2514
	if (portnr < 0 || (unsigned int)portnr >= panelp->nrports)
2515 2516
		return NULL;
	return panelp->ports[portnr];
L
Linus Torvalds 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
}

/*****************************************************************************/

/*
 *	Return the port stats structure to user app. A NULL port struct
 *	pointer passed in means that we need to find out from the app
 *	what port to get stats for (used through board control device).
 */

J
Jiri Slaby 已提交
2527
static int stl_getportstats(struct stlport *portp, comstats_t __user *cp)
L
Linus Torvalds 已提交
2528
{
2529
	comstats_t	stl_comstats;
L
Linus Torvalds 已提交
2530 2531 2532 2533 2534 2535 2536 2537
	unsigned char	*head, *tail;
	unsigned long	flags;

	if (!portp) {
		if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
			return -EFAULT;
		portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
			stl_comstats.port);
2538
		if (portp == NULL)
2539
			return -ENODEV;
L
Linus Torvalds 已提交
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
	}

	portp->stats.state = portp->istate;
	portp->stats.flags = portp->flags;
	portp->stats.hwid = portp->hwid;

	portp->stats.ttystate = 0;
	portp->stats.cflags = 0;
	portp->stats.iflags = 0;
	portp->stats.oflags = 0;
	portp->stats.lflags = 0;
	portp->stats.rxbuffered = 0;

A
Alan Cox 已提交
2553
	spin_lock_irqsave(&stallion_lock, flags);
2554
	if (portp->tty != NULL)
L
Linus Torvalds 已提交
2555 2556
		if (portp->tty->driver_data == portp) {
			portp->stats.ttystate = portp->tty->flags;
A
Alan Cox 已提交
2557 2558
			/* No longer available as a statistic */
			portp->stats.rxbuffered = 1; /*portp->tty->flip.count; */
2559
			if (portp->tty->termios != NULL) {
L
Linus Torvalds 已提交
2560 2561 2562 2563 2564 2565
				portp->stats.cflags = portp->tty->termios->c_cflag;
				portp->stats.iflags = portp->tty->termios->c_iflag;
				portp->stats.oflags = portp->tty->termios->c_oflag;
				portp->stats.lflags = portp->tty->termios->c_lflag;
			}
		}
A
Alan Cox 已提交
2566
	spin_unlock_irqrestore(&stallion_lock, flags);
L
Linus Torvalds 已提交
2567 2568 2569

	head = portp->tx.head;
	tail = portp->tx.tail;
2570 2571
	portp->stats.txbuffered = (head >= tail) ? (head - tail) :
		(STL_TXBUFSIZE - (tail - head));
L
Linus Torvalds 已提交
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584

	portp->stats.signals = (unsigned long) stl_getsignals(portp);

	return copy_to_user(cp, &portp->stats,
			    sizeof(comstats_t)) ? -EFAULT : 0;
}

/*****************************************************************************/

/*
 *	Clear the port stats structure. We also return it zeroed out...
 */

J
Jiri Slaby 已提交
2585
static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
L
Linus Torvalds 已提交
2586
{
2587 2588
	comstats_t	stl_comstats;

L
Linus Torvalds 已提交
2589 2590 2591 2592 2593
	if (!portp) {
		if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
			return -EFAULT;
		portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
			stl_comstats.port);
2594
		if (portp == NULL)
2595
			return -ENODEV;
L
Linus Torvalds 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
	}

	memset(&portp->stats, 0, sizeof(comstats_t));
	portp->stats.brd = portp->brdnr;
	portp->stats.panel = portp->panelnr;
	portp->stats.port = portp->portnr;
	return copy_to_user(cp, &portp->stats,
			    sizeof(comstats_t)) ? -EFAULT : 0;
}

/*****************************************************************************/

/*
 *	Return the entire driver ports structure to a user app.
 */

J
Jiri Slaby 已提交
2612
static int stl_getportstruct(struct stlport __user *arg)
L
Linus Torvalds 已提交
2613
{
2614
	struct stlport	stl_dummyport;
J
Jiri Slaby 已提交
2615
	struct stlport	*portp;
L
Linus Torvalds 已提交
2616

J
Jiri Slaby 已提交
2617
	if (copy_from_user(&stl_dummyport, arg, sizeof(struct stlport)))
L
Linus Torvalds 已提交
2618 2619 2620 2621 2622
		return -EFAULT;
	portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
		 stl_dummyport.portnr);
	if (!portp)
		return -ENODEV;
J
Jiri Slaby 已提交
2623
	return copy_to_user(arg, portp, sizeof(struct stlport)) ? -EFAULT : 0;
L
Linus Torvalds 已提交
2624 2625 2626 2627 2628 2629 2630 2631
}

/*****************************************************************************/

/*
 *	Return the entire driver board structure to a user app.
 */

J
Jiri Slaby 已提交
2632
static int stl_getbrdstruct(struct stlbrd __user *arg)
L
Linus Torvalds 已提交
2633
{
2634
	struct stlbrd	stl_dummybrd;
J
Jiri Slaby 已提交
2635
	struct stlbrd	*brdp;
L
Linus Torvalds 已提交
2636

J
Jiri Slaby 已提交
2637
	if (copy_from_user(&stl_dummybrd, arg, sizeof(struct stlbrd)))
L
Linus Torvalds 已提交
2638
		return -EFAULT;
2639
	if (stl_dummybrd.brdnr >= STL_MAXBRDS)
L
Linus Torvalds 已提交
2640 2641 2642
		return -ENODEV;
	brdp = stl_brds[stl_dummybrd.brdnr];
	if (!brdp)
2643
		return -ENODEV;
J
Jiri Slaby 已提交
2644
	return copy_to_user(arg, brdp, sizeof(struct stlbrd)) ? -EFAULT : 0;
L
Linus Torvalds 已提交
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
}

/*****************************************************************************/

/*
 *	The "staliomem" device is also required to do some special operations
 *	on the board and/or ports. In this driver it is mostly used for stats
 *	collection.
 */

static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
{
	int	brdnr, rc;
	void __user *argp = (void __user *)arg;

2660
	pr_debug("stl_memioctl(ip=%p,fp=%p,cmd=%x,arg=%lx)\n", ip, fp, cmd,arg);
L
Linus Torvalds 已提交
2661 2662 2663

	brdnr = iminor(ip);
	if (brdnr >= STL_MAXBRDS)
2664
		return -ENODEV;
L
Linus Torvalds 已提交
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
	rc = 0;

	switch (cmd) {
	case COM_GETPORTSTATS:
		rc = stl_getportstats(NULL, argp);
		break;
	case COM_CLRPORTSTATS:
		rc = stl_clrportstats(NULL, argp);
		break;
	case COM_GETBRDSTATS:
		rc = stl_getbrdstats(argp);
		break;
	case COM_READPORT:
		rc = stl_getportstruct(argp);
		break;
	case COM_READBOARD:
		rc = stl_getbrdstruct(argp);
		break;
	default:
		rc = -ENOIOCTLCMD;
		break;
	}

2688
	return rc;
L
Linus Torvalds 已提交
2689 2690
}

J
Jeff Dike 已提交
2691
static const struct tty_operations stl_ops = {
L
Linus Torvalds 已提交
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
	.open = stl_open,
	.close = stl_close,
	.write = stl_write,
	.put_char = stl_putchar,
	.flush_chars = stl_flushchars,
	.write_room = stl_writeroom,
	.chars_in_buffer = stl_charsinbuffer,
	.ioctl = stl_ioctl,
	.set_termios = stl_settermios,
	.throttle = stl_throttle,
	.unthrottle = stl_unthrottle,
	.stop = stl_stop,
	.start = stl_start,
	.hangup = stl_hangup,
	.flush_buffer = stl_flushbuffer,
	.break_ctl = stl_breakctl,
	.wait_until_sent = stl_waituntilsent,
	.send_xchar = stl_sendxchar,
	.read_proc = stl_readproc,
	.tiocmget = stl_tiocmget,
	.tiocmset = stl_tiocmset,
};

/*****************************************************************************/
/*                       CD1400 HARDWARE FUNCTIONS                           */
/*****************************************************************************/

/*
 *	These functions get/set/update the registers of the cd1400 UARTs.
 *	Access to the cd1400 registers is via an address/data io port pair.
 *	(Maybe should make this inline...)
 */

J
Jiri Slaby 已提交
2725
static int stl_cd1400getreg(struct stlport *portp, int regnr)
L
Linus Torvalds 已提交
2726 2727
{
	outb((regnr + portp->uartaddr), portp->ioaddr);
2728
	return inb(portp->ioaddr + EREG_DATA);
L
Linus Torvalds 已提交
2729 2730
}

J
Jiri Slaby 已提交
2731
static void stl_cd1400setreg(struct stlport *portp, int regnr, int value)
L
Linus Torvalds 已提交
2732
{
2733
	outb(regnr + portp->uartaddr, portp->ioaddr);
L
Linus Torvalds 已提交
2734 2735 2736
	outb(value, portp->ioaddr + EREG_DATA);
}

J
Jiri Slaby 已提交
2737
static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value)
L
Linus Torvalds 已提交
2738
{
2739
	outb(regnr + portp->uartaddr, portp->ioaddr);
L
Linus Torvalds 已提交
2740 2741
	if (inb(portp->ioaddr + EREG_DATA) != value) {
		outb(value, portp->ioaddr + EREG_DATA);
2742
		return 1;
L
Linus Torvalds 已提交
2743
	}
2744
	return 0;
L
Linus Torvalds 已提交
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
}

/*****************************************************************************/

/*
 *	Inbitialize the UARTs in a panel. We don't care what sort of board
 *	these ports are on - since the port io registers are almost
 *	identical when dealing with ports.
 */

J
Jiri Slaby 已提交
2755
static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
L
Linus Torvalds 已提交
2756 2757 2758 2759
{
	unsigned int	gfrcr;
	int		chipmask, i, j;
	int		nrchips, uartaddr, ioaddr;
A
Alan Cox 已提交
2760
	unsigned long   flags;
L
Linus Torvalds 已提交
2761

2762
	pr_debug("stl_panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
L
Linus Torvalds 已提交
2763

A
Alan Cox 已提交
2764
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
2765 2766 2767 2768 2769 2770 2771
	BRDENABLE(panelp->brdnr, panelp->pagenr);

/*
 *	Check that each chip is present and started up OK.
 */
	chipmask = 0;
	nrchips = panelp->nrports / CD1400_PORTS;
2772
	for (i = 0; i < nrchips; i++) {
L
Linus Torvalds 已提交
2773 2774 2775
		if (brdp->brdtype == BRD_ECHPCI) {
			outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
			ioaddr = panelp->iobase;
2776
		} else
L
Linus Torvalds 已提交
2777 2778 2779 2780 2781 2782 2783 2784
			ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
		uartaddr = (i & 0x01) ? 0x080 : 0;
		outb((GFRCR + uartaddr), ioaddr);
		outb(0, (ioaddr + EREG_DATA));
		outb((CCR + uartaddr), ioaddr);
		outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
		outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
		outb((GFRCR + uartaddr), ioaddr);
2785
		for (j = 0; j < CCR_MAXWAIT; j++)
L
Linus Torvalds 已提交
2786 2787
			if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
				break;
2788

L
Linus Torvalds 已提交
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
		if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
			printk("STALLION: cd1400 not responding, "
				"brd=%d panel=%d chip=%d\n",
				panelp->brdnr, panelp->panelnr, i);
			continue;
		}
		chipmask |= (0x1 << i);
		outb((PPR + uartaddr), ioaddr);
		outb(PPR_SCALAR, (ioaddr + EREG_DATA));
	}

	BRDDISABLE(panelp->brdnr);
A
Alan Cox 已提交
2801
	spin_unlock_irqrestore(&brd_lock, flags);
2802
	return chipmask;
L
Linus Torvalds 已提交
2803 2804 2805 2806 2807 2808 2809 2810
}

/*****************************************************************************/

/*
 *	Initialize hardware specific port registers.
 */

J
Jiri Slaby 已提交
2811
static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
L
Linus Torvalds 已提交
2812
{
A
Alan Cox 已提交
2813
	unsigned long flags;
2814 2815
	pr_debug("stl_cd1400portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
			panelp, portp);
L
Linus Torvalds 已提交
2816

2817 2818
	if ((brdp == NULL) || (panelp == NULL) ||
	    (portp == NULL))
L
Linus Torvalds 已提交
2819 2820
		return;

A
Alan Cox 已提交
2821
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
	portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
		(portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
	portp->uartaddr = (portp->portnr & 0x04) << 5;
	portp->pagenr = panelp->pagenr + (portp->portnr >> 3);

	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
	portp->hwid = stl_cd1400getreg(portp, GFRCR);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
2832
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
2833 2834 2835 2836 2837 2838 2839 2840 2841
}

/*****************************************************************************/

/*
 *	Wait for the command register to be ready. We will poll this,
 *	since it won't usually take too long to be ready.
 */

J
Jiri Slaby 已提交
2842
static void stl_cd1400ccrwait(struct stlport *portp)
L
Linus Torvalds 已提交
2843 2844 2845
{
	int	i;

2846 2847
	for (i = 0; i < CCR_MAXWAIT; i++)
		if (stl_cd1400getreg(portp, CCR) == 0)
L
Linus Torvalds 已提交
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
			return;

	printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
		portp->portnr, portp->panelnr, portp->brdnr);
}

/*****************************************************************************/

/*
 *	Set up the cd1400 registers for a port based on the termios port
 *	settings.
 */

A
Alan Cox 已提交
2861
static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp)
L
Linus Torvalds 已提交
2862
{
J
Jiri Slaby 已提交
2863
	struct stlbrd	*brdp;
L
Linus Torvalds 已提交
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
	unsigned long	flags;
	unsigned int	clkdiv, baudrate;
	unsigned char	cor1, cor2, cor3;
	unsigned char	cor4, cor5, ccr;
	unsigned char	srer, sreron, sreroff;
	unsigned char	mcor1, mcor2, rtpr;
	unsigned char	clk, div;

	cor1 = 0;
	cor2 = 0;
	cor3 = 0;
	cor4 = 0;
	cor5 = 0;
	ccr = 0;
	rtpr = 0;
	clk = 0;
	div = 0;
	mcor1 = 0;
	mcor2 = 0;
	sreron = 0;
	sreroff = 0;

	brdp = stl_brds[portp->brdnr];
2887
	if (brdp == NULL)
L
Linus Torvalds 已提交
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
		return;

/*
 *	Set up the RX char ignore mask with those RX error types we
 *	can ignore. We can get the cd1400 to help us out a little here,
 *	it will ignore parity errors and breaks for us.
 */
	portp->rxignoremsk = 0;
	if (tiosp->c_iflag & IGNPAR) {
		portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
		cor1 |= COR1_PARIGNORE;
	}
	if (tiosp->c_iflag & IGNBRK) {
		portp->rxignoremsk |= ST_BREAK;
		cor4 |= COR4_IGNBRK;
	}

	portp->rxmarkmsk = ST_OVERRUN;
	if (tiosp->c_iflag & (INPCK | PARMRK))
		portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
	if (tiosp->c_iflag & BRKINT)
		portp->rxmarkmsk |= ST_BREAK;

/*
 *	Go through the char size, parity and stop bits and set all the
 *	option register appropriately.
 */
	switch (tiosp->c_cflag & CSIZE) {
	case CS5:
		cor1 |= COR1_CHL5;
		break;
	case CS6:
		cor1 |= COR1_CHL6;
		break;
	case CS7:
		cor1 |= COR1_CHL7;
		break;
	default:
		cor1 |= COR1_CHL8;
		break;
	}

	if (tiosp->c_cflag & CSTOPB)
		cor1 |= COR1_STOP2;
	else
		cor1 |= COR1_STOP1;

	if (tiosp->c_cflag & PARENB) {
		if (tiosp->c_cflag & PARODD)
			cor1 |= (COR1_PARENB | COR1_PARODD);
		else
			cor1 |= (COR1_PARENB | COR1_PAREVEN);
	} else {
		cor1 |= COR1_PARNONE;
	}

/*
 *	Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
 *	space for hardware flow control and the like. This should be set to
 *	VMIN. Also here we will set the RX data timeout to 10ms - this should
 *	really be based on VTIME.
 */
	cor3 |= FIFO_RXTHRESHOLD;
	rtpr = 2;

/*
 *	Calculate the baud rate timers. For now we will just assume that
 *	the input and output baud are the same. Could have used a baud
 *	table here, but this way we can generate virtually any baud rate
 *	we like!
 */
	baudrate = tiosp->c_cflag & CBAUD;
	if (baudrate & CBAUDEX) {
		baudrate &= ~CBAUDEX;
		if ((baudrate < 1) || (baudrate > 4))
			tiosp->c_cflag &= ~CBAUDEX;
		else
			baudrate += 15;
	}
	baudrate = stl_baudrates[baudrate];
	if ((tiosp->c_cflag & CBAUD) == B38400) {
		if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
			baudrate = 57600;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
			baudrate = 115200;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
			baudrate = 230400;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
			baudrate = 460800;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
			baudrate = (portp->baud_base / portp->custom_divisor);
	}
	if (baudrate > STL_CD1400MAXBAUD)
		baudrate = STL_CD1400MAXBAUD;

	if (baudrate > 0) {
2984 2985
		for (clk = 0; clk < CD1400_NUMCLKS; clk++) {
			clkdiv = (portp->clk / stl_cd1400clkdivs[clk]) / baudrate;
L
Linus Torvalds 已提交
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
			if (clkdiv < 0x100)
				break;
		}
		div = (unsigned char) clkdiv;
	}

/*
 *	Check what form of modem signaling is required and set it up.
 */
	if ((tiosp->c_cflag & CLOCAL) == 0) {
		mcor1 |= MCOR1_DCD;
		mcor2 |= MCOR2_DCD;
		sreron |= SRER_MODEM;
		portp->flags |= ASYNC_CHECK_CD;
3000
	} else
L
Linus Torvalds 已提交
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025
		portp->flags &= ~ASYNC_CHECK_CD;

/*
 *	Setup cd1400 enhanced modes if we can. In particular we want to
 *	handle as much of the flow control as possible automatically. As
 *	well as saving a few CPU cycles it will also greatly improve flow
 *	control reliability.
 */
	if (tiosp->c_iflag & IXON) {
		cor2 |= COR2_TXIBE;
		cor3 |= COR3_SCD12;
		if (tiosp->c_iflag & IXANY)
			cor2 |= COR2_IXM;
	}

	if (tiosp->c_cflag & CRTSCTS) {
		cor2 |= COR2_CTSAE;
		mcor1 |= FIFO_RTSTHRESHOLD;
	}

/*
 *	All cd1400 register values calculated so go through and set
 *	them all up.
 */

3026
	pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
L
Linus Torvalds 已提交
3027
		portp->portnr, portp->panelnr, portp->brdnr);
3028
	pr_debug("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
L
Linus Torvalds 已提交
3029
		cor1, cor2, cor3, cor4, cor5);
3030
	pr_debug("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
L
Linus Torvalds 已提交
3031
		mcor1, mcor2, rtpr, sreron, sreroff);
3032 3033
	pr_debug("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
	pr_debug("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
L
Linus Torvalds 已提交
3034 3035 3036
		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);

A
Alan Cox 已提交
3037
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
	srer = stl_cd1400getreg(portp, SRER);
	stl_cd1400setreg(portp, SRER, 0);
	if (stl_cd1400updatereg(portp, COR1, cor1))
		ccr = 1;
	if (stl_cd1400updatereg(portp, COR2, cor2))
		ccr = 1;
	if (stl_cd1400updatereg(portp, COR3, cor3))
		ccr = 1;
	if (ccr) {
		stl_cd1400ccrwait(portp);
		stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
	}
	stl_cd1400setreg(portp, COR4, cor4);
	stl_cd1400setreg(portp, COR5, cor5);
	stl_cd1400setreg(portp, MCOR1, mcor1);
	stl_cd1400setreg(portp, MCOR2, mcor2);
	if (baudrate > 0) {
		stl_cd1400setreg(portp, TCOR, clk);
		stl_cd1400setreg(portp, TBPR, div);
		stl_cd1400setreg(portp, RCOR, clk);
		stl_cd1400setreg(portp, RBPR, div);
	}
	stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
	stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
	stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
	stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
	stl_cd1400setreg(portp, RTPR, rtpr);
	mcor1 = stl_cd1400getreg(portp, MSVR1);
	if (mcor1 & MSVR1_DCD)
		portp->sigs |= TIOCM_CD;
	else
		portp->sigs &= ~TIOCM_CD;
	stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3074
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3075 3076 3077 3078 3079 3080 3081 3082
}

/*****************************************************************************/

/*
 *	Set the state of the DTR and RTS signals.
 */

J
Jiri Slaby 已提交
3083
static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts)
L
Linus Torvalds 已提交
3084 3085 3086 3087
{
	unsigned char	msvr1, msvr2;
	unsigned long	flags;

3088 3089
	pr_debug("stl_cd1400setsignals(portp=%p,dtr=%d,rts=%d)\n",
			portp, dtr, rts);
L
Linus Torvalds 已提交
3090 3091 3092 3093 3094 3095 3096 3097

	msvr1 = 0;
	msvr2 = 0;
	if (dtr > 0)
		msvr1 = MSVR1_DTR;
	if (rts > 0)
		msvr2 = MSVR2_RTS;

A
Alan Cox 已提交
3098
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3099 3100 3101 3102 3103 3104 3105
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	if (rts >= 0)
		stl_cd1400setreg(portp, MSVR2, msvr2);
	if (dtr >= 0)
		stl_cd1400setreg(portp, MSVR1, msvr1);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3106
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3107 3108 3109 3110 3111 3112 3113 3114
}

/*****************************************************************************/

/*
 *	Return the state of the signals.
 */

J
Jiri Slaby 已提交
3115
static int stl_cd1400getsignals(struct stlport *portp)
L
Linus Torvalds 已提交
3116 3117 3118 3119 3120
{
	unsigned char	msvr1, msvr2;
	unsigned long	flags;
	int		sigs;

3121
	pr_debug("stl_cd1400getsignals(portp=%p)\n", portp);
L
Linus Torvalds 已提交
3122

A
Alan Cox 已提交
3123
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3124 3125 3126 3127 3128
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	msvr1 = stl_cd1400getreg(portp, MSVR1);
	msvr2 = stl_cd1400getreg(portp, MSVR2);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3129
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141

	sigs = 0;
	sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
	sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
	sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
	sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
#if 0
	sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
	sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
#else
	sigs |= TIOCM_DSR;
#endif
3142
	return sigs;
L
Linus Torvalds 已提交
3143 3144 3145 3146 3147 3148 3149 3150
}

/*****************************************************************************/

/*
 *	Enable/Disable the Transmitter and/or Receiver.
 */

J
Jiri Slaby 已提交
3151
static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx)
L
Linus Torvalds 已提交
3152 3153 3154 3155
{
	unsigned char	ccr;
	unsigned long	flags;

3156 3157
	pr_debug("stl_cd1400enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);

L
Linus Torvalds 已提交
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
	ccr = 0;

	if (tx == 0)
		ccr |= CCR_TXDISABLE;
	else if (tx > 0)
		ccr |= CCR_TXENABLE;
	if (rx == 0)
		ccr |= CCR_RXDISABLE;
	else if (rx > 0)
		ccr |= CCR_RXENABLE;

A
Alan Cox 已提交
3169
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3170 3171 3172 3173 3174 3175
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	stl_cd1400ccrwait(portp);
	stl_cd1400setreg(portp, CCR, ccr);
	stl_cd1400ccrwait(portp);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3176
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3177 3178 3179 3180 3181 3182 3183 3184
}

/*****************************************************************************/

/*
 *	Start/stop the Transmitter and/or Receiver.
 */

J
Jiri Slaby 已提交
3185
static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx)
L
Linus Torvalds 已提交
3186 3187 3188 3189
{
	unsigned char	sreron, sreroff;
	unsigned long	flags;

3190
	pr_debug("stl_cd1400startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
L
Linus Torvalds 已提交
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204

	sreron = 0;
	sreroff = 0;
	if (tx == 0)
		sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
	else if (tx == 1)
		sreron |= SRER_TXDATA;
	else if (tx >= 2)
		sreron |= SRER_TXEMPTY;
	if (rx == 0)
		sreroff |= SRER_RXDATA;
	else if (rx > 0)
		sreron |= SRER_RXDATA;

A
Alan Cox 已提交
3205
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3206 3207 3208 3209 3210 3211 3212
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	stl_cd1400setreg(portp, SRER,
		((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
	BRDDISABLE(portp->brdnr);
	if (tx > 0)
		set_bit(ASYI_TXBUSY, &portp->istate);
A
Alan Cox 已提交
3213
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3214 3215 3216 3217 3218 3219 3220 3221
}

/*****************************************************************************/

/*
 *	Disable all interrupts from this port.
 */

J
Jiri Slaby 已提交
3222
static void stl_cd1400disableintrs(struct stlport *portp)
L
Linus Torvalds 已提交
3223 3224 3225
{
	unsigned long	flags;

3226 3227
	pr_debug("stl_cd1400disableintrs(portp=%p)\n", portp);

A
Alan Cox 已提交
3228
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3229 3230 3231 3232
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	stl_cd1400setreg(portp, SRER, 0);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3233
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3234 3235 3236 3237
}

/*****************************************************************************/

J
Jiri Slaby 已提交
3238
static void stl_cd1400sendbreak(struct stlport *portp, int len)
L
Linus Torvalds 已提交
3239 3240 3241
{
	unsigned long	flags;

3242
	pr_debug("stl_cd1400sendbreak(portp=%p,len=%d)\n", portp, len);
L
Linus Torvalds 已提交
3243

A
Alan Cox 已提交
3244
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3245 3246 3247 3248 3249 3250 3251 3252 3253
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	stl_cd1400setreg(portp, SRER,
		((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
		SRER_TXEMPTY));
	BRDDISABLE(portp->brdnr);
	portp->brklen = len;
	if (len == 1)
		portp->stats.txbreaks++;
A
Alan Cox 已提交
3254
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3255 3256 3257 3258 3259 3260 3261 3262
}

/*****************************************************************************/

/*
 *	Take flow control actions...
 */

J
Jiri Slaby 已提交
3263
static void stl_cd1400flowctrl(struct stlport *portp, int state)
L
Linus Torvalds 已提交
3264 3265 3266 3267
{
	struct tty_struct	*tty;
	unsigned long		flags;

3268
	pr_debug("stl_cd1400flowctrl(portp=%p,state=%x)\n", portp, state);
L
Linus Torvalds 已提交
3269

3270
	if (portp == NULL)
L
Linus Torvalds 已提交
3271 3272
		return;
	tty = portp->tty;
3273
	if (tty == NULL)
L
Linus Torvalds 已提交
3274 3275
		return;

A
Alan Cox 已提交
3276
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));

	if (state) {
		if (tty->termios->c_iflag & IXOFF) {
			stl_cd1400ccrwait(portp);
			stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
			portp->stats.rxxon++;
			stl_cd1400ccrwait(portp);
		}
/*
 *		Question: should we return RTS to what it was before? It may
 *		have been set by an ioctl... Suppose not, since if you have
 *		hardware flow control set then it is pretty silly to go and
 *		set the RTS line by hand.
 */
		if (tty->termios->c_cflag & CRTSCTS) {
			stl_cd1400setreg(portp, MCOR1,
				(stl_cd1400getreg(portp, MCOR1) |
				FIFO_RTSTHRESHOLD));
			stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
			portp->stats.rxrtson++;
		}
	} else {
		if (tty->termios->c_iflag & IXOFF) {
			stl_cd1400ccrwait(portp);
			stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
			portp->stats.rxxoff++;
			stl_cd1400ccrwait(portp);
		}
		if (tty->termios->c_cflag & CRTSCTS) {
			stl_cd1400setreg(portp, MCOR1,
				(stl_cd1400getreg(portp, MCOR1) & 0xf0));
			stl_cd1400setreg(portp, MSVR2, 0);
			portp->stats.rxrtsoff++;
		}
	}

	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3316
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3317 3318 3319 3320 3321 3322 3323 3324
}

/*****************************************************************************/

/*
 *	Send a flow control character...
 */

J
Jiri Slaby 已提交
3325
static void stl_cd1400sendflow(struct stlport *portp, int state)
L
Linus Torvalds 已提交
3326 3327 3328 3329
{
	struct tty_struct	*tty;
	unsigned long		flags;

3330
	pr_debug("stl_cd1400sendflow(portp=%p,state=%x)\n", portp, state);
L
Linus Torvalds 已提交
3331

3332
	if (portp == NULL)
L
Linus Torvalds 已提交
3333 3334
		return;
	tty = portp->tty;
3335
	if (tty == NULL)
L
Linus Torvalds 已提交
3336 3337
		return;

A
Alan Cox 已提交
3338
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	if (state) {
		stl_cd1400ccrwait(portp);
		stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
		portp->stats.rxxon++;
		stl_cd1400ccrwait(portp);
	} else {
		stl_cd1400ccrwait(portp);
		stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
		portp->stats.rxxoff++;
		stl_cd1400ccrwait(portp);
	}
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3353
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3354 3355 3356 3357
}

/*****************************************************************************/

J
Jiri Slaby 已提交
3358
static void stl_cd1400flush(struct stlport *portp)
L
Linus Torvalds 已提交
3359 3360 3361
{
	unsigned long	flags;

3362
	pr_debug("stl_cd1400flush(portp=%p)\n", portp);
L
Linus Torvalds 已提交
3363

3364
	if (portp == NULL)
L
Linus Torvalds 已提交
3365 3366
		return;

A
Alan Cox 已提交
3367
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3368 3369 3370 3371 3372 3373 3374
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
	stl_cd1400ccrwait(portp);
	stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
	stl_cd1400ccrwait(portp);
	portp->tx.tail = portp->tx.head;
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
3375
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
}

/*****************************************************************************/

/*
 *	Return the current state of data flow on this port. This is only
 *	really interresting when determining if data has fully completed
 *	transmission or not... This is easy for the cd1400, it accurately
 *	maintains the busy port flag.
 */

J
Jiri Slaby 已提交
3387
static int stl_cd1400datastate(struct stlport *portp)
L
Linus Torvalds 已提交
3388
{
3389
	pr_debug("stl_cd1400datastate(portp=%p)\n", portp);
L
Linus Torvalds 已提交
3390

3391
	if (portp == NULL)
3392
		return 0;
L
Linus Torvalds 已提交
3393

3394
	return test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0;
L
Linus Torvalds 已提交
3395 3396 3397 3398 3399 3400 3401 3402
}

/*****************************************************************************/

/*
 *	Interrupt service routine for cd1400 EasyIO boards.
 */

J
Jiri Slaby 已提交
3403
static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase)
L
Linus Torvalds 已提交
3404 3405 3406
{
	unsigned char	svrtype;

3407
	pr_debug("stl_cd1400eiointr(panelp=%p,iobase=%x)\n", panelp, iobase);
L
Linus Torvalds 已提交
3408

A
Alan Cox 已提交
3409
	spin_lock(&brd_lock);
L
Linus Torvalds 已提交
3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
	outb(SVRR, iobase);
	svrtype = inb(iobase + EREG_DATA);
	if (panelp->nrports > 4) {
		outb((SVRR + 0x80), iobase);
		svrtype |= inb(iobase + EREG_DATA);
	}

	if (svrtype & SVRR_RX)
		stl_cd1400rxisr(panelp, iobase);
	else if (svrtype & SVRR_TX)
		stl_cd1400txisr(panelp, iobase);
	else if (svrtype & SVRR_MDM)
		stl_cd1400mdmisr(panelp, iobase);
A
Alan Cox 已提交
3423 3424

	spin_unlock(&brd_lock);
L
Linus Torvalds 已提交
3425 3426 3427 3428 3429 3430 3431 3432
}

/*****************************************************************************/

/*
 *	Interrupt service routine for cd1400 panels.
 */

J
Jiri Slaby 已提交
3433
static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase)
L
Linus Torvalds 已提交
3434 3435 3436
{
	unsigned char	svrtype;

3437
	pr_debug("stl_cd1400echintr(panelp=%p,iobase=%x)\n", panelp, iobase);
L
Linus Torvalds 已提交
3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458

	outb(SVRR, iobase);
	svrtype = inb(iobase + EREG_DATA);
	outb((SVRR + 0x80), iobase);
	svrtype |= inb(iobase + EREG_DATA);
	if (svrtype & SVRR_RX)
		stl_cd1400rxisr(panelp, iobase);
	else if (svrtype & SVRR_TX)
		stl_cd1400txisr(panelp, iobase);
	else if (svrtype & SVRR_MDM)
		stl_cd1400mdmisr(panelp, iobase);
}


/*****************************************************************************/

/*
 *	Unfortunately we need to handle breaks in the TX data stream, since
 *	this is the only way to generate them on the cd1400.
 */

3459
static int stl_cd1400breakisr(struct stlport *portp, int ioaddr)
L
Linus Torvalds 已提交
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
{
	if (portp->brklen == 1) {
		outb((COR2 + portp->uartaddr), ioaddr);
		outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
			(ioaddr + EREG_DATA));
		outb((TDR + portp->uartaddr), ioaddr);
		outb(ETC_CMD, (ioaddr + EREG_DATA));
		outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
		outb((SRER + portp->uartaddr), ioaddr);
		outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
			(ioaddr + EREG_DATA));
3471
		return 1;
L
Linus Torvalds 已提交
3472 3473 3474 3475 3476
	} else if (portp->brklen > 1) {
		outb((TDR + portp->uartaddr), ioaddr);
		outb(ETC_CMD, (ioaddr + EREG_DATA));
		outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
		portp->brklen = -1;
3477
		return 1;
L
Linus Torvalds 已提交
3478 3479 3480 3481 3482 3483
	} else {
		outb((COR2 + portp->uartaddr), ioaddr);
		outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
			(ioaddr + EREG_DATA));
		portp->brklen = 0;
	}
3484
	return 0;
L
Linus Torvalds 已提交
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
}

/*****************************************************************************/

/*
 *	Transmit interrupt handler. This has gotta be fast!  Handling TX
 *	chars is pretty simple, stuff as many as possible from the TX buffer
 *	into the cd1400 FIFO. Must also handle TX breaks here, since they
 *	are embedded as commands in the data stream. Oh no, had to use a goto!
 *	This could be optimized more, will do when I get time...
 *	In practice it is possible that interrupts are enabled but that the
 *	port has been hung up. Need to handle not having any TX buffer here,
 *	this is done by using the side effect that head and tail will also
 *	be NULL if the buffer has been freed.
 */

J
Jiri Slaby 已提交
3501
static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr)
L
Linus Torvalds 已提交
3502
{
J
Jiri Slaby 已提交
3503
	struct stlport	*portp;
L
Linus Torvalds 已提交
3504 3505 3506 3507
	int		len, stlen;
	char		*head, *tail;
	unsigned char	ioack, srer;

3508
	pr_debug("stl_cd1400txisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
L
Linus Torvalds 已提交
3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546

	ioack = inb(ioaddr + EREG_TXACK);
	if (((ioack & panelp->ackmask) != 0) ||
	    ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
		printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
		return;
	}
	portp = panelp->ports[(ioack >> 3)];

/*
 *	Unfortunately we need to handle breaks in the data stream, since
 *	this is the only way to generate them on the cd1400. Do it now if
 *	a break is to be sent.
 */
	if (portp->brklen != 0)
		if (stl_cd1400breakisr(portp, ioaddr))
			goto stl_txalldone;

	head = portp->tx.head;
	tail = portp->tx.tail;
	len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
	if ((len == 0) || ((len < STL_TXBUFLOW) &&
	    (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
		set_bit(ASYI_TXLOW, &portp->istate);
		schedule_work(&portp->tqueue);
	}

	if (len == 0) {
		outb((SRER + portp->uartaddr), ioaddr);
		srer = inb(ioaddr + EREG_DATA);
		if (srer & SRER_TXDATA) {
			srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
		} else {
			srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
			clear_bit(ASYI_TXBUSY, &portp->istate);
		}
		outb(srer, (ioaddr + EREG_DATA));
	} else {
3547
		len = min(len, CD1400_TXFIFOSIZE);
L
Linus Torvalds 已提交
3548
		portp->stats.txtotal += len;
3549
		stlen = min(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
L
Linus Torvalds 已提交
3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579
		outb((TDR + portp->uartaddr), ioaddr);
		outsb((ioaddr + EREG_DATA), tail, stlen);
		len -= stlen;
		tail += stlen;
		if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
			tail = portp->tx.buf;
		if (len > 0) {
			outsb((ioaddr + EREG_DATA), tail, len);
			tail += len;
		}
		portp->tx.tail = tail;
	}

stl_txalldone:
	outb((EOSRR + portp->uartaddr), ioaddr);
	outb(0, (ioaddr + EREG_DATA));
}

/*****************************************************************************/

/*
 *	Receive character interrupt handler. Determine if we have good chars
 *	or bad chars and then process appropriately. Good chars are easy
 *	just shove the lot into the RX buffer and set all status byte to 0.
 *	If a bad RX char then process as required. This routine needs to be
 *	fast!  In practice it is possible that we get an interrupt on a port
 *	that is closed. This can happen on hangups - since they completely
 *	shutdown a port not in user context. Need to handle this case.
 */

J
Jiri Slaby 已提交
3580
static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr)
L
Linus Torvalds 已提交
3581
{
J
Jiri Slaby 已提交
3582
	struct stlport		*portp;
L
Linus Torvalds 已提交
3583 3584 3585 3586 3587
	struct tty_struct	*tty;
	unsigned int		ioack, len, buflen;
	unsigned char		status;
	char			ch;

3588
	pr_debug("stl_cd1400rxisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
L
Linus Torvalds 已提交
3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600

	ioack = inb(ioaddr + EREG_RXACK);
	if ((ioack & panelp->ackmask) != 0) {
		printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
		return;
	}
	portp = panelp->ports[(ioack >> 3)];
	tty = portp->tty;

	if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
		outb((RDCR + portp->uartaddr), ioaddr);
		len = inb(ioaddr + EREG_DATA);
A
Alan Cox 已提交
3601
		if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
3602
			len = min(len, sizeof(stl_unwanted));
L
Linus Torvalds 已提交
3603 3604 3605 3606 3607
			outb((RDSR + portp->uartaddr), ioaddr);
			insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
			portp->stats.rxlost += len;
			portp->stats.rxtotal += len;
		} else {
3608
			len = min(len, buflen);
L
Linus Torvalds 已提交
3609
			if (len > 0) {
A
Alan Cox 已提交
3610
				unsigned char *ptr;
L
Linus Torvalds 已提交
3611
				outb((RDSR + portp->uartaddr), ioaddr);
A
Alan Cox 已提交
3612 3613
				tty_prepare_flip_string(tty, &ptr, len);
				insb((ioaddr + EREG_DATA), ptr, len);
L
Linus Torvalds 已提交
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
				tty_schedule_flip(tty);
				portp->stats.rxtotal += len;
			}
		}
	} else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
		outb((RDSR + portp->uartaddr), ioaddr);
		status = inb(ioaddr + EREG_DATA);
		ch = inb(ioaddr + EREG_DATA);
		if (status & ST_PARITY)
			portp->stats.rxparity++;
		if (status & ST_FRAMING)
			portp->stats.rxframing++;
		if (status & ST_OVERRUN)
			portp->stats.rxoverrun++;
		if (status & ST_BREAK)
			portp->stats.rxbreaks++;
		if (status & ST_SCHARMASK) {
			if ((status & ST_SCHARMASK) == ST_SCHAR1)
				portp->stats.txxon++;
			if ((status & ST_SCHARMASK) == ST_SCHAR2)
				portp->stats.txxoff++;
			goto stl_rxalldone;
		}
A
Alan Cox 已提交
3637
		if (tty != NULL && (portp->rxignoremsk & status) == 0) {
L
Linus Torvalds 已提交
3638 3639 3640 3641 3642 3643 3644
			if (portp->rxmarkmsk & status) {
				if (status & ST_BREAK) {
					status = TTY_BREAK;
					if (portp->flags & ASYNC_SAK) {
						do_SAK(tty);
						BRDENABLE(portp->brdnr, portp->pagenr);
					}
3645
				} else if (status & ST_PARITY)
L
Linus Torvalds 已提交
3646
					status = TTY_PARITY;
3647
				else if (status & ST_FRAMING)
L
Linus Torvalds 已提交
3648
					status = TTY_FRAME;
3649
				else if(status & ST_OVERRUN)
L
Linus Torvalds 已提交
3650
					status = TTY_OVERRUN;
3651
				else
L
Linus Torvalds 已提交
3652
					status = 0;
3653
			} else
L
Linus Torvalds 已提交
3654
				status = 0;
A
Alan Cox 已提交
3655 3656
			tty_insert_flip_char(tty, ch, status);
			tty_schedule_flip(tty);
L
Linus Torvalds 已提交
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
		}
	} else {
		printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
		return;
	}

stl_rxalldone:
	outb((EOSRR + portp->uartaddr), ioaddr);
	outb(0, (ioaddr + EREG_DATA));
}

/*****************************************************************************/

/*
 *	Modem interrupt handler. The is called when the modem signal line
 *	(DCD) has changed state. Leave most of the work to the off-level
 *	processing routine.
 */

J
Jiri Slaby 已提交
3676
static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr)
L
Linus Torvalds 已提交
3677
{
J
Jiri Slaby 已提交
3678
	struct stlport	*portp;
L
Linus Torvalds 已提交
3679 3680 3681
	unsigned int	ioack;
	unsigned char	misr;

3682
	pr_debug("stl_cd1400mdmisr(panelp=%p)\n", panelp);
L
Linus Torvalds 已提交
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713

	ioack = inb(ioaddr + EREG_MDACK);
	if (((ioack & panelp->ackmask) != 0) ||
	    ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
		printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
		return;
	}
	portp = panelp->ports[(ioack >> 3)];

	outb((MISR + portp->uartaddr), ioaddr);
	misr = inb(ioaddr + EREG_DATA);
	if (misr & MISR_DCD) {
		set_bit(ASYI_DCDCHANGE, &portp->istate);
		schedule_work(&portp->tqueue);
		portp->stats.modem++;
	}

	outb((EOSRR + portp->uartaddr), ioaddr);
	outb(0, (ioaddr + EREG_DATA));
}

/*****************************************************************************/
/*                      SC26198 HARDWARE FUNCTIONS                           */
/*****************************************************************************/

/*
 *	These functions get/set/update the registers of the sc26198 UARTs.
 *	Access to the sc26198 registers is via an address/data io port pair.
 *	(Maybe should make this inline...)
 */

J
Jiri Slaby 已提交
3714
static int stl_sc26198getreg(struct stlport *portp, int regnr)
L
Linus Torvalds 已提交
3715 3716
{
	outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3717
	return inb(portp->ioaddr + XP_DATA);
L
Linus Torvalds 已提交
3718 3719
}

J
Jiri Slaby 已提交
3720
static void stl_sc26198setreg(struct stlport *portp, int regnr, int value)
L
Linus Torvalds 已提交
3721 3722 3723 3724 3725
{
	outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
	outb(value, (portp->ioaddr + XP_DATA));
}

J
Jiri Slaby 已提交
3726
static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value)
L
Linus Torvalds 已提交
3727 3728 3729 3730
{
	outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
	if (inb(portp->ioaddr + XP_DATA) != value) {
		outb(value, (portp->ioaddr + XP_DATA));
3731
		return 1;
L
Linus Torvalds 已提交
3732
	}
3733
	return 0;
L
Linus Torvalds 已提交
3734 3735 3736 3737 3738 3739 3740 3741
}

/*****************************************************************************/

/*
 *	Functions to get and set the sc26198 global registers.
 */

J
Jiri Slaby 已提交
3742
static int stl_sc26198getglobreg(struct stlport *portp, int regnr)
L
Linus Torvalds 已提交
3743 3744
{
	outb(regnr, (portp->ioaddr + XP_ADDR));
3745
	return inb(portp->ioaddr + XP_DATA);
L
Linus Torvalds 已提交
3746 3747 3748
}

#if 0
J
Jiri Slaby 已提交
3749
static void stl_sc26198setglobreg(struct stlport *portp, int regnr, int value)
L
Linus Torvalds 已提交
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763
{
	outb(regnr, (portp->ioaddr + XP_ADDR));
	outb(value, (portp->ioaddr + XP_DATA));
}
#endif

/*****************************************************************************/

/*
 *	Inbitialize the UARTs in a panel. We don't care what sort of board
 *	these ports are on - since the port io registers are almost
 *	identical when dealing with ports.
 */

J
Jiri Slaby 已提交
3764
static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
L
Linus Torvalds 已提交
3765 3766 3767 3768
{
	int	chipmask, i;
	int	nrchips, ioaddr;

3769
	pr_debug("stl_sc26198panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
L
Linus Torvalds 已提交
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780

	BRDENABLE(panelp->brdnr, panelp->pagenr);

/*
 *	Check that each chip is present and started up OK.
 */
	chipmask = 0;
	nrchips = (panelp->nrports + 4) / SC26198_PORTS;
	if (brdp->brdtype == BRD_ECHPCI)
		outb(panelp->pagenr, brdp->ioctrl);

3781
	for (i = 0; i < nrchips; i++) {
L
Linus Torvalds 已提交
3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
		ioaddr = panelp->iobase + (i * 4); 
		outb(SCCR, (ioaddr + XP_ADDR));
		outb(CR_RESETALL, (ioaddr + XP_DATA));
		outb(TSTR, (ioaddr + XP_ADDR));
		if (inb(ioaddr + XP_DATA) != 0) {
			printk("STALLION: sc26198 not responding, "
				"brd=%d panel=%d chip=%d\n",
				panelp->brdnr, panelp->panelnr, i);
			continue;
		}
		chipmask |= (0x1 << i);
		outb(GCCR, (ioaddr + XP_ADDR));
		outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
		outb(WDTRCR, (ioaddr + XP_ADDR));
		outb(0xff, (ioaddr + XP_DATA));
	}

	BRDDISABLE(panelp->brdnr);
3800
	return chipmask;
L
Linus Torvalds 已提交
3801 3802 3803 3804 3805 3806 3807 3808
}

/*****************************************************************************/

/*
 *	Initialize hardware specific port registers.
 */

J
Jiri Slaby 已提交
3809
static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
L
Linus Torvalds 已提交
3810
{
3811 3812
	pr_debug("stl_sc26198portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
			panelp, portp);
L
Linus Torvalds 已提交
3813

3814 3815
	if ((brdp == NULL) || (panelp == NULL) ||
	    (portp == NULL))
L
Linus Torvalds 已提交
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
		return;

	portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
	portp->uartaddr = (portp->portnr & 0x07) << 4;
	portp->pagenr = panelp->pagenr;
	portp->hwid = 0x1;

	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
	BRDDISABLE(portp->brdnr);
}

/*****************************************************************************/

/*
 *	Set up the sc26198 registers for a port based on the termios port
 *	settings.
 */

A
Alan Cox 已提交
3835
static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
L
Linus Torvalds 已提交
3836
{
J
Jiri Slaby 已提交
3837
	struct stlbrd	*brdp;
L
Linus Torvalds 已提交
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851
	unsigned long	flags;
	unsigned int	baudrate;
	unsigned char	mr0, mr1, mr2, clk;
	unsigned char	imron, imroff, iopr, ipr;

	mr0 = 0;
	mr1 = 0;
	mr2 = 0;
	clk = 0;
	iopr = 0;
	imron = 0;
	imroff = 0;

	brdp = stl_brds[portp->brdnr];
3852
	if (brdp == NULL)
L
Linus Torvalds 已提交
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900
		return;

/*
 *	Set up the RX char ignore mask with those RX error types we
 *	can ignore.
 */
	portp->rxignoremsk = 0;
	if (tiosp->c_iflag & IGNPAR)
		portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
			SR_RXOVERRUN);
	if (tiosp->c_iflag & IGNBRK)
		portp->rxignoremsk |= SR_RXBREAK;

	portp->rxmarkmsk = SR_RXOVERRUN;
	if (tiosp->c_iflag & (INPCK | PARMRK))
		portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
	if (tiosp->c_iflag & BRKINT)
		portp->rxmarkmsk |= SR_RXBREAK;

/*
 *	Go through the char size, parity and stop bits and set all the
 *	option register appropriately.
 */
	switch (tiosp->c_cflag & CSIZE) {
	case CS5:
		mr1 |= MR1_CS5;
		break;
	case CS6:
		mr1 |= MR1_CS6;
		break;
	case CS7:
		mr1 |= MR1_CS7;
		break;
	default:
		mr1 |= MR1_CS8;
		break;
	}

	if (tiosp->c_cflag & CSTOPB)
		mr2 |= MR2_STOP2;
	else
		mr2 |= MR2_STOP1;

	if (tiosp->c_cflag & PARENB) {
		if (tiosp->c_cflag & PARODD)
			mr1 |= (MR1_PARENB | MR1_PARODD);
		else
			mr1 |= (MR1_PARENB | MR1_PAREVEN);
3901
	} else
L
Linus Torvalds 已提交
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941
		mr1 |= MR1_PARNONE;

	mr1 |= MR1_ERRBLOCK;

/*
 *	Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
 *	space for hardware flow control and the like. This should be set to
 *	VMIN.
 */
	mr2 |= MR2_RXFIFOHALF;

/*
 *	Calculate the baud rate timers. For now we will just assume that
 *	the input and output baud are the same. The sc26198 has a fixed
 *	baud rate table, so only discrete baud rates possible.
 */
	baudrate = tiosp->c_cflag & CBAUD;
	if (baudrate & CBAUDEX) {
		baudrate &= ~CBAUDEX;
		if ((baudrate < 1) || (baudrate > 4))
			tiosp->c_cflag &= ~CBAUDEX;
		else
			baudrate += 15;
	}
	baudrate = stl_baudrates[baudrate];
	if ((tiosp->c_cflag & CBAUD) == B38400) {
		if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
			baudrate = 57600;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
			baudrate = 115200;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
			baudrate = 230400;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
			baudrate = 460800;
		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
			baudrate = (portp->baud_base / portp->custom_divisor);
	}
	if (baudrate > STL_SC26198MAXBAUD)
		baudrate = STL_SC26198MAXBAUD;

3942 3943
	if (baudrate > 0)
		for (clk = 0; clk < SC26198_NRBAUDS; clk++)
L
Linus Torvalds 已提交
3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
			if (baudrate <= sc26198_baudtable[clk])
				break;

/*
 *	Check what form of modem signaling is required and set it up.
 */
	if (tiosp->c_cflag & CLOCAL) {
		portp->flags &= ~ASYNC_CHECK_CD;
	} else {
		iopr |= IOPR_DCDCOS;
		imron |= IR_IOPORT;
		portp->flags |= ASYNC_CHECK_CD;
	}

/*
 *	Setup sc26198 enhanced modes if we can. In particular we want to
 *	handle as much of the flow control as possible automatically. As
 *	well as saving a few CPU cycles it will also greatly improve flow
 *	control reliability.
 */
	if (tiosp->c_iflag & IXON) {
		mr0 |= MR0_SWFTX | MR0_SWFT;
		imron |= IR_XONXOFF;
3967
	} else
L
Linus Torvalds 已提交
3968
		imroff |= IR_XONXOFF;
3969

L
Linus Torvalds 已提交
3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982
	if (tiosp->c_iflag & IXOFF)
		mr0 |= MR0_SWFRX;

	if (tiosp->c_cflag & CRTSCTS) {
		mr2 |= MR2_AUTOCTS;
		mr1 |= MR1_AUTORTS;
	}

/*
 *	All sc26198 register values calculated so go through and set
 *	them all up.
 */

3983
	pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
L
Linus Torvalds 已提交
3984
		portp->portnr, portp->panelnr, portp->brdnr);
3985 3986 3987
	pr_debug("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
	pr_debug("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
	pr_debug("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
L
Linus Torvalds 已提交
3988 3989 3990
		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
		tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);

A
Alan Cox 已提交
3991
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_sc26198setreg(portp, IMR, 0);
	stl_sc26198updatereg(portp, MR0, mr0);
	stl_sc26198updatereg(portp, MR1, mr1);
	stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
	stl_sc26198updatereg(portp, MR2, mr2);
	stl_sc26198updatereg(portp, IOPIOR,
		((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));

	if (baudrate > 0) {
		stl_sc26198setreg(portp, TXCSR, clk);
		stl_sc26198setreg(portp, RXCSR, clk);
	}

	stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
	stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);

	ipr = stl_sc26198getreg(portp, IPR);
	if (ipr & IPR_DCD)
		portp->sigs &= ~TIOCM_CD;
	else
		portp->sigs |= TIOCM_CD;

	portp->imr = (portp->imr & ~imroff) | imron;
	stl_sc26198setreg(portp, IMR, portp->imr);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4018
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4019 4020 4021 4022 4023 4024 4025 4026
}

/*****************************************************************************/

/*
 *	Set the state of the DTR and RTS signals.
 */

J
Jiri Slaby 已提交
4027
static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts)
L
Linus Torvalds 已提交
4028 4029 4030 4031
{
	unsigned char	iopioron, iopioroff;
	unsigned long	flags;

4032 4033
	pr_debug("stl_sc26198setsignals(portp=%p,dtr=%d,rts=%d)\n", portp,
			dtr, rts);
L
Linus Torvalds 已提交
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045

	iopioron = 0;
	iopioroff = 0;
	if (dtr == 0)
		iopioroff |= IPR_DTR;
	else if (dtr > 0)
		iopioron |= IPR_DTR;
	if (rts == 0)
		iopioroff |= IPR_RTS;
	else if (rts > 0)
		iopioron |= IPR_RTS;

A
Alan Cox 已提交
4046
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4047 4048 4049 4050
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_sc26198setreg(portp, IOPIOR,
		((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4051
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4052 4053 4054 4055 4056 4057 4058 4059
}

/*****************************************************************************/

/*
 *	Return the state of the signals.
 */

J
Jiri Slaby 已提交
4060
static int stl_sc26198getsignals(struct stlport *portp)
L
Linus Torvalds 已提交
4061 4062 4063 4064 4065
{
	unsigned char	ipr;
	unsigned long	flags;
	int		sigs;

4066
	pr_debug("stl_sc26198getsignals(portp=%p)\n", portp);
L
Linus Torvalds 已提交
4067

A
Alan Cox 已提交
4068
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4069 4070 4071
	BRDENABLE(portp->brdnr, portp->pagenr);
	ipr = stl_sc26198getreg(portp, IPR);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4072
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4073 4074 4075 4076 4077 4078 4079

	sigs = 0;
	sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
	sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
	sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
	sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
	sigs |= TIOCM_DSR;
4080
	return sigs;
L
Linus Torvalds 已提交
4081 4082 4083 4084 4085 4086 4087 4088
}

/*****************************************************************************/

/*
 *	Enable/Disable the Transmitter and/or Receiver.
 */

J
Jiri Slaby 已提交
4089
static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx)
L
Linus Torvalds 已提交
4090 4091 4092 4093
{
	unsigned char	ccr;
	unsigned long	flags;

4094
	pr_debug("stl_sc26198enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx,tx);
L
Linus Torvalds 已提交
4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105

	ccr = portp->crenable;
	if (tx == 0)
		ccr &= ~CR_TXENABLE;
	else if (tx > 0)
		ccr |= CR_TXENABLE;
	if (rx == 0)
		ccr &= ~CR_RXENABLE;
	else if (rx > 0)
		ccr |= CR_RXENABLE;

A
Alan Cox 已提交
4106
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4107 4108 4109 4110
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_sc26198setreg(portp, SCCR, ccr);
	BRDDISABLE(portp->brdnr);
	portp->crenable = ccr;
A
Alan Cox 已提交
4111
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4112 4113 4114 4115 4116 4117 4118 4119
}

/*****************************************************************************/

/*
 *	Start/stop the Transmitter and/or Receiver.
 */

J
Jiri Slaby 已提交
4120
static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx)
L
Linus Torvalds 已提交
4121 4122 4123 4124
{
	unsigned char	imr;
	unsigned long	flags;

4125
	pr_debug("stl_sc26198startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
L
Linus Torvalds 已提交
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136

	imr = portp->imr;
	if (tx == 0)
		imr &= ~IR_TXRDY;
	else if (tx == 1)
		imr |= IR_TXRDY;
	if (rx == 0)
		imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
	else if (rx > 0)
		imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;

A
Alan Cox 已提交
4137
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4138 4139 4140 4141 4142 4143
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_sc26198setreg(portp, IMR, imr);
	BRDDISABLE(portp->brdnr);
	portp->imr = imr;
	if (tx > 0)
		set_bit(ASYI_TXBUSY, &portp->istate);
A
Alan Cox 已提交
4144
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4145 4146 4147 4148 4149 4150 4151 4152
}

/*****************************************************************************/

/*
 *	Disable all interrupts from this port.
 */

J
Jiri Slaby 已提交
4153
static void stl_sc26198disableintrs(struct stlport *portp)
L
Linus Torvalds 已提交
4154 4155 4156
{
	unsigned long	flags;

4157
	pr_debug("stl_sc26198disableintrs(portp=%p)\n", portp);
L
Linus Torvalds 已提交
4158

A
Alan Cox 已提交
4159
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4160 4161 4162 4163
	BRDENABLE(portp->brdnr, portp->pagenr);
	portp->imr = 0;
	stl_sc26198setreg(portp, IMR, 0);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4164
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4165 4166 4167 4168
}

/*****************************************************************************/

J
Jiri Slaby 已提交
4169
static void stl_sc26198sendbreak(struct stlport *portp, int len)
L
Linus Torvalds 已提交
4170 4171 4172
{
	unsigned long	flags;

4173
	pr_debug("stl_sc26198sendbreak(portp=%p,len=%d)\n", portp, len);
L
Linus Torvalds 已提交
4174

A
Alan Cox 已提交
4175
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4176 4177 4178 4179
	BRDENABLE(portp->brdnr, portp->pagenr);
	if (len == 1) {
		stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
		portp->stats.txbreaks++;
4180
	} else
L
Linus Torvalds 已提交
4181
		stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4182

L
Linus Torvalds 已提交
4183
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4184
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4185 4186 4187 4188 4189 4190 4191 4192
}

/*****************************************************************************/

/*
 *	Take flow control actions...
 */

J
Jiri Slaby 已提交
4193
static void stl_sc26198flowctrl(struct stlport *portp, int state)
L
Linus Torvalds 已提交
4194 4195 4196 4197 4198
{
	struct tty_struct	*tty;
	unsigned long		flags;
	unsigned char		mr0;

4199
	pr_debug("stl_sc26198flowctrl(portp=%p,state=%x)\n", portp, state);
L
Linus Torvalds 已提交
4200

4201
	if (portp == NULL)
L
Linus Torvalds 已提交
4202 4203
		return;
	tty = portp->tty;
4204
	if (tty == NULL)
L
Linus Torvalds 已提交
4205 4206
		return;

A
Alan Cox 已提交
4207
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252
	BRDENABLE(portp->brdnr, portp->pagenr);

	if (state) {
		if (tty->termios->c_iflag & IXOFF) {
			mr0 = stl_sc26198getreg(portp, MR0);
			stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
			stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
			mr0 |= MR0_SWFRX;
			portp->stats.rxxon++;
			stl_sc26198wait(portp);
			stl_sc26198setreg(portp, MR0, mr0);
		}
/*
 *		Question: should we return RTS to what it was before? It may
 *		have been set by an ioctl... Suppose not, since if you have
 *		hardware flow control set then it is pretty silly to go and
 *		set the RTS line by hand.
 */
		if (tty->termios->c_cflag & CRTSCTS) {
			stl_sc26198setreg(portp, MR1,
				(stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
			stl_sc26198setreg(portp, IOPIOR,
				(stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
			portp->stats.rxrtson++;
		}
	} else {
		if (tty->termios->c_iflag & IXOFF) {
			mr0 = stl_sc26198getreg(portp, MR0);
			stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
			stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
			mr0 &= ~MR0_SWFRX;
			portp->stats.rxxoff++;
			stl_sc26198wait(portp);
			stl_sc26198setreg(portp, MR0, mr0);
		}
		if (tty->termios->c_cflag & CRTSCTS) {
			stl_sc26198setreg(portp, MR1,
				(stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
			stl_sc26198setreg(portp, IOPIOR,
				(stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
			portp->stats.rxrtsoff++;
		}
	}

	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4253
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4254 4255 4256 4257 4258 4259 4260 4261
}

/*****************************************************************************/

/*
 *	Send a flow control character.
 */

J
Jiri Slaby 已提交
4262
static void stl_sc26198sendflow(struct stlport *portp, int state)
L
Linus Torvalds 已提交
4263 4264 4265 4266 4267
{
	struct tty_struct	*tty;
	unsigned long		flags;
	unsigned char		mr0;

4268
	pr_debug("stl_sc26198sendflow(portp=%p,state=%x)\n", portp, state);
L
Linus Torvalds 已提交
4269

4270
	if (portp == NULL)
L
Linus Torvalds 已提交
4271 4272
		return;
	tty = portp->tty;
4273
	if (tty == NULL)
L
Linus Torvalds 已提交
4274 4275
		return;

A
Alan Cox 已提交
4276
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295
	BRDENABLE(portp->brdnr, portp->pagenr);
	if (state) {
		mr0 = stl_sc26198getreg(portp, MR0);
		stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
		stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
		mr0 |= MR0_SWFRX;
		portp->stats.rxxon++;
		stl_sc26198wait(portp);
		stl_sc26198setreg(portp, MR0, mr0);
	} else {
		mr0 = stl_sc26198getreg(portp, MR0);
		stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
		stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
		mr0 &= ~MR0_SWFRX;
		portp->stats.rxxoff++;
		stl_sc26198wait(portp);
		stl_sc26198setreg(portp, MR0, mr0);
	}
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4296
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4297 4298 4299 4300
}

/*****************************************************************************/

J
Jiri Slaby 已提交
4301
static void stl_sc26198flush(struct stlport *portp)
L
Linus Torvalds 已提交
4302 4303 4304
{
	unsigned long	flags;

4305
	pr_debug("stl_sc26198flush(portp=%p)\n", portp);
L
Linus Torvalds 已提交
4306

4307
	if (portp == NULL)
L
Linus Torvalds 已提交
4308 4309
		return;

A
Alan Cox 已提交
4310
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4311 4312 4313 4314 4315
	BRDENABLE(portp->brdnr, portp->pagenr);
	stl_sc26198setreg(portp, SCCR, CR_TXRESET);
	stl_sc26198setreg(portp, SCCR, portp->crenable);
	BRDDISABLE(portp->brdnr);
	portp->tx.tail = portp->tx.head;
A
Alan Cox 已提交
4316
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328
}

/*****************************************************************************/

/*
 *	Return the current state of data flow on this port. This is only
 *	really interresting when determining if data has fully completed
 *	transmission or not... The sc26198 interrupt scheme cannot
 *	determine when all data has actually drained, so we need to
 *	check the port statusy register to be sure.
 */

J
Jiri Slaby 已提交
4329
static int stl_sc26198datastate(struct stlport *portp)
L
Linus Torvalds 已提交
4330 4331 4332 4333
{
	unsigned long	flags;
	unsigned char	sr;

4334
	pr_debug("stl_sc26198datastate(portp=%p)\n", portp);
L
Linus Torvalds 已提交
4335

4336
	if (portp == NULL)
4337
		return 0;
L
Linus Torvalds 已提交
4338
	if (test_bit(ASYI_TXBUSY, &portp->istate))
4339
		return 1;
L
Linus Torvalds 已提交
4340

A
Alan Cox 已提交
4341
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
4342 4343 4344
	BRDENABLE(portp->brdnr, portp->pagenr);
	sr = stl_sc26198getreg(portp, SR);
	BRDDISABLE(portp->brdnr);
A
Alan Cox 已提交
4345
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4346

4347
	return (sr & SR_TXEMPTY) ? 0 : 1;
L
Linus Torvalds 已提交
4348 4349 4350 4351 4352 4353 4354 4355 4356
}

/*****************************************************************************/

/*
 *	Delay for a small amount of time, to give the sc26198 a chance
 *	to process a command...
 */

J
Jiri Slaby 已提交
4357
static void stl_sc26198wait(struct stlport *portp)
L
Linus Torvalds 已提交
4358 4359 4360
{
	int	i;

4361
	pr_debug("stl_sc26198wait(portp=%p)\n", portp);
L
Linus Torvalds 已提交
4362

4363
	if (portp == NULL)
L
Linus Torvalds 已提交
4364 4365
		return;

4366
	for (i = 0; i < 20; i++)
L
Linus Torvalds 已提交
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377
		stl_sc26198getglobreg(portp, TSTR);
}

/*****************************************************************************/

/*
 *	If we are TX flow controlled and in IXANY mode then we may
 *	need to unflow control here. We gotta do this because of the
 *	automatic flow control modes of the sc26198.
 */

4378
static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty)
L
Linus Torvalds 已提交
4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395
{
	unsigned char	mr0;

	mr0 = stl_sc26198getreg(portp, MR0);
	stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
	stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
	stl_sc26198wait(portp);
	stl_sc26198setreg(portp, MR0, mr0);
	clear_bit(ASYI_TXFLOWED, &portp->istate);
}

/*****************************************************************************/

/*
 *	Interrupt service routine for sc26198 panels.
 */

J
Jiri Slaby 已提交
4396
static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase)
L
Linus Torvalds 已提交
4397
{
J
Jiri Slaby 已提交
4398
	struct stlport	*portp;
L
Linus Torvalds 已提交
4399 4400
	unsigned int	iack;

A
Alan Cox 已提交
4401 4402
	spin_lock(&brd_lock);

L
Linus Torvalds 已提交
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
/* 
 *	Work around bug in sc26198 chip... Cannot have A6 address
 *	line of UART high, else iack will be returned as 0.
 */
	outb(0, (iobase + 1));

	iack = inb(iobase + XP_IACK);
	portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];

	if (iack & IVR_RXDATA)
		stl_sc26198rxisr(portp, iack);
	else if (iack & IVR_TXDATA)
		stl_sc26198txisr(portp);
	else
		stl_sc26198otherisr(portp, iack);
A
Alan Cox 已提交
4418 4419

	spin_unlock(&brd_lock);
L
Linus Torvalds 已提交
4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
}

/*****************************************************************************/

/*
 *	Transmit interrupt handler. This has gotta be fast!  Handling TX
 *	chars is pretty simple, stuff as many as possible from the TX buffer
 *	into the sc26198 FIFO.
 *	In practice it is possible that interrupts are enabled but that the
 *	port has been hung up. Need to handle not having any TX buffer here,
 *	this is done by using the side effect that head and tail will also
 *	be NULL if the buffer has been freed.
 */

J
Jiri Slaby 已提交
4434
static void stl_sc26198txisr(struct stlport *portp)
L
Linus Torvalds 已提交
4435 4436 4437 4438 4439 4440
{
	unsigned int	ioaddr;
	unsigned char	mr0;
	int		len, stlen;
	char		*head, *tail;

4441
	pr_debug("stl_sc26198txisr(portp=%p)\n", portp);
L
Linus Torvalds 已提交
4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465

	ioaddr = portp->ioaddr;
	head = portp->tx.head;
	tail = portp->tx.tail;
	len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
	if ((len == 0) || ((len < STL_TXBUFLOW) &&
	    (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
		set_bit(ASYI_TXLOW, &portp->istate);
		schedule_work(&portp->tqueue); 
	}

	if (len == 0) {
		outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
		mr0 = inb(ioaddr + XP_DATA);
		if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
			portp->imr &= ~IR_TXRDY;
			outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
			outb(portp->imr, (ioaddr + XP_DATA));
			clear_bit(ASYI_TXBUSY, &portp->istate);
		} else {
			mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
			outb(mr0, (ioaddr + XP_DATA));
		}
	} else {
4466
		len = min(len, SC26198_TXFIFOSIZE);
L
Linus Torvalds 已提交
4467
		portp->stats.txtotal += len;
4468
		stlen = min(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
L
Linus Torvalds 已提交
4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
		outb(GTXFIFO, (ioaddr + XP_ADDR));
		outsb((ioaddr + XP_DATA), tail, stlen);
		len -= stlen;
		tail += stlen;
		if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
			tail = portp->tx.buf;
		if (len > 0) {
			outsb((ioaddr + XP_DATA), tail, len);
			tail += len;
		}
		portp->tx.tail = tail;
	}
}

/*****************************************************************************/

/*
 *	Receive character interrupt handler. Determine if we have good chars
 *	or bad chars and then process appropriately. Good chars are easy
 *	just shove the lot into the RX buffer and set all status byte to 0.
 *	If a bad RX char then process as required. This routine needs to be
 *	fast!  In practice it is possible that we get an interrupt on a port
 *	that is closed. This can happen on hangups - since they completely
 *	shutdown a port not in user context. Need to handle this case.
 */

J
Jiri Slaby 已提交
4495
static void stl_sc26198rxisr(struct stlport *portp, unsigned int iack)
L
Linus Torvalds 已提交
4496 4497 4498 4499
{
	struct tty_struct	*tty;
	unsigned int		len, buflen, ioaddr;

4500
	pr_debug("stl_sc26198rxisr(portp=%p,iack=%x)\n", portp, iack);
L
Linus Torvalds 已提交
4501 4502 4503 4504 4505 4506 4507

	tty = portp->tty;
	ioaddr = portp->ioaddr;
	outb(GIBCR, (ioaddr + XP_ADDR));
	len = inb(ioaddr + XP_DATA) + 1;

	if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
A
Alan Cox 已提交
4508
		if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
4509
			len = min(len, sizeof(stl_unwanted));
L
Linus Torvalds 已提交
4510 4511 4512 4513 4514
			outb(GRXFIFO, (ioaddr + XP_ADDR));
			insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
			portp->stats.rxlost += len;
			portp->stats.rxtotal += len;
		} else {
4515
			len = min(len, buflen);
L
Linus Torvalds 已提交
4516
			if (len > 0) {
A
Alan Cox 已提交
4517
				unsigned char *ptr;
L
Linus Torvalds 已提交
4518
				outb(GRXFIFO, (ioaddr + XP_ADDR));
A
Alan Cox 已提交
4519 4520
				tty_prepare_flip_string(tty, &ptr, len);
				insb((ioaddr + XP_DATA), ptr, len);
L
Linus Torvalds 已提交
4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534
				tty_schedule_flip(tty);
				portp->stats.rxtotal += len;
			}
		}
	} else {
		stl_sc26198rxbadchars(portp);
	}

/*
 *	If we are TX flow controlled and in IXANY mode then we may need
 *	to unflow control here. We gotta do this because of the automatic
 *	flow control modes of the sc26198.
 */
	if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
4535 4536
		if ((tty != NULL) &&
		    (tty->termios != NULL) &&
L
Linus Torvalds 已提交
4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548
		    (tty->termios->c_iflag & IXANY)) {
			stl_sc26198txunflow(portp, tty);
		}
	}
}

/*****************************************************************************/

/*
 *	Process an RX bad character.
 */

4549
static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch)
L
Linus Torvalds 已提交
4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565
{
	struct tty_struct	*tty;
	unsigned int		ioaddr;

	tty = portp->tty;
	ioaddr = portp->ioaddr;

	if (status & SR_RXPARITY)
		portp->stats.rxparity++;
	if (status & SR_RXFRAMING)
		portp->stats.rxframing++;
	if (status & SR_RXOVERRUN)
		portp->stats.rxoverrun++;
	if (status & SR_RXBREAK)
		portp->stats.rxbreaks++;

4566
	if ((tty != NULL) &&
L
Linus Torvalds 已提交
4567 4568 4569 4570 4571 4572 4573 4574
	    ((portp->rxignoremsk & status) == 0)) {
		if (portp->rxmarkmsk & status) {
			if (status & SR_RXBREAK) {
				status = TTY_BREAK;
				if (portp->flags & ASYNC_SAK) {
					do_SAK(tty);
					BRDENABLE(portp->brdnr, portp->pagenr);
				}
4575
			} else if (status & SR_RXPARITY)
L
Linus Torvalds 已提交
4576
				status = TTY_PARITY;
4577
			else if (status & SR_RXFRAMING)
L
Linus Torvalds 已提交
4578
				status = TTY_FRAME;
4579
			else if(status & SR_RXOVERRUN)
L
Linus Torvalds 已提交
4580
				status = TTY_OVERRUN;
4581
			else
L
Linus Torvalds 已提交
4582
				status = 0;
4583
		} else
L
Linus Torvalds 已提交
4584 4585
			status = 0;

A
Alan Cox 已提交
4586 4587
		tty_insert_flip_char(tty, ch, status);
		tty_schedule_flip(tty);
L
Linus Torvalds 已提交
4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604

		if (status == 0)
			portp->stats.rxtotal++;
	}
}

/*****************************************************************************/

/*
 *	Process all characters in the RX FIFO of the UART. Check all char
 *	status bytes as well, and process as required. We need to check
 *	all bytes in the FIFO, in case some more enter the FIFO while we
 *	are here. To get the exact character error type we need to switch
 *	into CHAR error mode (that is why we need to make sure we empty
 *	the FIFO).
 */

J
Jiri Slaby 已提交
4605
static void stl_sc26198rxbadchars(struct stlport *portp)
L
Linus Torvalds 已提交
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
{
	unsigned char	status, mr1;
	char		ch;

/*
 *	To get the precise error type for each character we must switch
 *	back into CHAR error mode.
 */
	mr1 = stl_sc26198getreg(portp, MR1);
	stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));

	while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
		stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
		ch = stl_sc26198getreg(portp, RXFIFO);
		stl_sc26198rxbadch(portp, status, ch);
	}

/*
 *	To get correct interrupt class we must switch back into BLOCK
 *	error mode.
 */
	stl_sc26198setreg(portp, MR1, mr1);
}

/*****************************************************************************/

/*
 *	Other interrupt handler. This includes modem signals, flow
 *	control actions, etc. Most stuff is left to off-level interrupt
 *	processing time.
 */

J
Jiri Slaby 已提交
4638
static void stl_sc26198otherisr(struct stlport *portp, unsigned int iack)
L
Linus Torvalds 已提交
4639 4640 4641
{
	unsigned char	cir, ipr, xisr;

4642
	pr_debug("stl_sc26198otherisr(portp=%p,iack=%x)\n", portp, iack);
L
Linus Torvalds 已提交
4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674

	cir = stl_sc26198getglobreg(portp, CIR);

	switch (cir & CIR_SUBTYPEMASK) {
	case CIR_SUBCOS:
		ipr = stl_sc26198getreg(portp, IPR);
		if (ipr & IPR_DCDCHANGE) {
			set_bit(ASYI_DCDCHANGE, &portp->istate);
			schedule_work(&portp->tqueue); 
			portp->stats.modem++;
		}
		break;
	case CIR_SUBXONXOFF:
		xisr = stl_sc26198getreg(portp, XISR);
		if (xisr & XISR_RXXONGOT) {
			set_bit(ASYI_TXFLOWED, &portp->istate);
			portp->stats.txxoff++;
		}
		if (xisr & XISR_RXXOFFGOT) {
			clear_bit(ASYI_TXFLOWED, &portp->istate);
			portp->stats.txxon++;
		}
		break;
	case CIR_SUBBREAK:
		stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
		stl_sc26198rxbadchars(portp);
		break;
	default:
		break;
	}
}

4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696
static void stl_free_isabrds(void)
{
	struct stlbrd *brdp;
	unsigned int i;

	for (i = 0; i < stl_nrbrds; i++) {
		if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
			continue;

		free_irq(brdp->irq, brdp);

		stl_cleanup_panels(brdp);

		release_region(brdp->ioaddr1, brdp->iosize1);
		if (brdp->iosize2 > 0)
			release_region(brdp->ioaddr2, brdp->iosize2);

		kfree(brdp);
		stl_brds[i] = NULL;
	}
}

4697 4698 4699 4700 4701
/*
 *	Loadable module initialization stuff.
 */
static int __init stallion_module_init(void)
{
4702 4703
	struct stlbrd	*brdp;
	struct stlconf	conf;
4704
	unsigned int i, j;
4705
	int retval;
4706 4707 4708 4709 4710 4711

	printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);

	spin_lock_init(&stallion_lock);
	spin_lock_init(&brd_lock);

4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
	stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
	if (!stl_serial) {
		retval = -ENOMEM;
		goto err;
	}

	stl_serial->owner = THIS_MODULE;
	stl_serial->driver_name = stl_drvname;
	stl_serial->name = "ttyE";
	stl_serial->major = STL_SERIALMAJOR;
	stl_serial->minor_start = 0;
	stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
	stl_serial->subtype = SERIAL_TYPE_NORMAL;
	stl_serial->init_termios = stl_deftermios;
	stl_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
	tty_set_operations(stl_serial, &stl_ops);

	retval = tty_register_driver(stl_serial);
	if (retval) {
		printk("STALLION: failed to register serial driver\n");
		goto err_frtty;
	}

4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750
/*
 *	Find any dynamically supported boards. That is via module load
 *	line options.
 */
	for (i = stl_nrbrds; i < stl_nargs; i++) {
		memset(&conf, 0, sizeof(conf));
		if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
			continue;
		if ((brdp = stl_allocbrd()) == NULL)
			continue;
		brdp->brdnr = i;
		brdp->brdtype = conf.brdtype;
		brdp->ioaddr1 = conf.ioaddr1;
		brdp->ioaddr2 = conf.ioaddr2;
		brdp->irq = conf.irq;
		brdp->irqtype = conf.irqtype;
4751 4752 4753
		stl_brds[brdp->brdnr] = brdp;
		if (stl_brdinit(brdp)) {
			stl_brds[brdp->brdnr] = NULL;
4754
			kfree(brdp);
4755
		} else {
4756 4757 4758
			for (j = 0; j < brdp->nrports; j++)
				tty_register_device(stl_serial,
					brdp->brdnr * STL_MAXPORTS + j, NULL);
4759
			stl_nrbrds = i + 1;
4760
		}
4761
	}
4762

4763
	/* this has to be _after_ isa finding because of locking */
4764
	retval = pci_register_driver(&stl_pcidriver);
4765 4766 4767
	if (retval && stl_nrbrds == 0) {
		printk(KERN_ERR "STALLION: can't register pci driver\n");
		goto err_unrtty;
4768
	}
4769 4770 4771 4772 4773 4774 4775 4776 4777

/*
 *	Set up a character driver for per board stuff. This is mainly used
 *	to do stats ioctls on the ports.
 */
	if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
		printk("STALLION: failed to register serial board device\n");

	stallion_class = class_create(THIS_MODULE, "staliomem");
4778 4779
	if (IS_ERR(stallion_class))
		printk("STALLION: failed to create class\n");
4780 4781 4782 4783 4784 4785
	for (i = 0; i < 4; i++)
		class_device_create(stallion_class, NULL,
				    MKDEV(STL_SIOMEMMAJOR, i), NULL,
				    "staliomem%d", i);

	return 0;
4786 4787 4788
err_unrtty:
	tty_unregister_driver(stl_serial);
err_frtty:
4789
	put_tty_driver(stl_serial);
4790 4791
err:
	return retval;
4792 4793 4794 4795
}

static void __exit stallion_module_exit(void)
{
4796 4797 4798
	struct stlbrd *brdp;
	unsigned int i, j;
	int retval;
4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810

	pr_debug("cleanup_module()\n");

	printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
		stl_drvversion);

/*
 *	Free up all allocated resources used by the ports. This includes
 *	memory and interrupts. As part of this process we will also do
 *	a hangup on every open port - to try to flush out any processes
 *	hanging onto ports.
 */
4811 4812 4813 4814 4815 4816 4817
	for (i = 0; i < stl_nrbrds; i++) {
		if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
			continue;
		for (j = 0; j < brdp->nrports; j++)
			tty_unregister_device(stl_serial,
				brdp->brdnr * STL_MAXPORTS + j);
	}
4818

4819 4820
	for (i = 0; i < 4; i++)
		class_device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
4821
	if ((retval = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
4822
		printk("STALLION: failed to un-register serial memory device, "
4823
			"errno=%d\n", -retval);
4824 4825
	class_destroy(stallion_class);

4826 4827
	pci_unregister_driver(&stl_pcidriver);

4828
	stl_free_isabrds();
4829 4830 4831

	tty_unregister_driver(stl_serial);
	put_tty_driver(stl_serial);
4832 4833 4834 4835 4836 4837 4838 4839
}

module_init(stallion_module_init);
module_exit(stallion_module_exit);

MODULE_AUTHOR("Greg Ungerer");
MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
MODULE_LICENSE("GPL");