istallion.c 122.8 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
/*****************************************************************************/

/*
 *	istallion.c  -- stallion intelligent 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.
 *
 */

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

#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/cdk.h>
#include <linux/comstats.h>
#include <linux/istallion.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/wait.h>
A
Alan Cox 已提交
35
#include <linux/eisa.h>
36
#include <linux/ctype.h>
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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

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

#include <linux/pci.h>

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

/*
 *	Define different board types. Not all of the following board types
 *	are supported by this driver. But I will use the standard "assigned"
 *	board numbers. Currently supported boards are abbreviated as:
 *	ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
 *	STAL = Stallion.
 */
#define	BRD_UNKNOWN	0
#define	BRD_STALLION	1
#define	BRD_BRUMBY4	2
#define	BRD_ONBOARD2	3
#define	BRD_ONBOARD	4
#define	BRD_ONBOARDE	7
#define	BRD_ECP		23
#define BRD_ECPE	24
#define	BRD_ECPMC	25
#define	BRD_ECPPCI	29

#define	BRD_BRUMBY	BRD_BRUMBY4

/*
 *	Define a configuration structure to hold the board configuration.
 *	Need to set this up in the code (for now) with the boards that are
 *	to be configured into the system. This is what needs to be modified
 *	when adding/removing/modifying boards. Each line entry in the
 *	stli_brdconf[] array is a board. Each line contains io/irq/memory
 *	ranges for that board (as well as what type of board it is).
 *	Some examples:
 *		{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
 *	This line will configure an EasyConnection 8/64 at io address 2a0,
 *	and shared memory address of cc000. Multiple EasyConnection 8/64
 *	boards can share the same shared memory address space. No interrupt
 *	is required for this board type.
 *	Another example:
 *		{ BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
 *	This line will configure an EasyConnection 8/64 EISA in slot 5 and
 *	shared memory address of 0x80000000 (2 GByte). Multiple
 *	EasyConnection 8/64 EISA boards can share the same shared memory
 *	address space. No interrupt is required for this board type.
 *	Another example:
 *		{ BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
 *	This line will configure an ONboard (ISA type) at io address 240,
 *	and shared memory address of d0000. Multiple ONboards can share
 *	the same shared memory address space. No interrupt required.
 *	Another example:
 *		{ BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
 *	This line will configure a Brumby board (any number of ports!) at
 *	io address 360 and shared memory address of c8000. All Brumby boards
 *	configured into a system must have their own separate io and memory
 *	addresses. No interrupt is required.
 *	Another example:
 *		{ BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
 *	This line will configure an original Stallion board at io address 330
 *	and shared memory address d0000 (this would only be valid for a "V4.0"
 *	or Rev.O Stallion board). All Stallion boards configured into the
 *	system must have their own separate io and memory addresses. No
 *	interrupt is required.
 */

104
struct stlconf {
L
Linus Torvalds 已提交
105 106 107 108 109 110
	int		brdtype;
	int		ioaddr1;
	int		ioaddr2;
	unsigned long	memaddr;
	int		irq;
	int		irqtype;
111
};
L
Linus Torvalds 已提交
112

113
static unsigned int stli_nrbrds;
L
Linus Torvalds 已提交
114

A
Alan Cox 已提交
115 116 117 118
/* stli_lock must NOT be taken holding brd_lock */
static spinlock_t stli_lock;	/* TTY logic lock */
static spinlock_t brd_lock;	/* Board logic lock */

L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/*
 *	There is some experimental EISA board detection code in this driver.
 *	By default it is disabled, but for those that want to try it out,
 *	then set the define below to be 1.
 */
#define	STLI_EISAPROBE	0

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

/*
 *	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

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

/*
 *	Define our local driver identity first. Set up stuff to deal with
 *	all the local structures required by a serial tty driver.
 */
static char	*stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
static char	*stli_drvname = "istallion";
static char	*stli_drvversion = "5.6.0";
static char	*stli_serialname = "ttyE";

static struct tty_driver	*stli_serial;


#define	STLI_TXBUFSIZE		4096

/*
 *	Use a fast local buffer for cooked characters. Typically a whole
 *	bunch of cooked characters come in for a port, 1 at a time. So we
 *	save those up into a local buffer, then write out the whole lot
 *	with a large memcpy. Just use 1 buffer for all ports, since its
 *	use it is only need for short periods of time by each port.
 */
static char			*stli_txcookbuf;
static int			stli_txcooksize;
static int			stli_txcookrealsize;
static struct tty_struct	*stli_txcooktty;

/*
 *	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 baud, 8 data bits, no parity, 1 stop bit.
 */
A
Alan Cox 已提交
175
static struct ktermios		stli_deftermios = {
L
Linus Torvalds 已提交
176 177
	.c_cflag	= (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
	.c_cc		= INIT_C_CC,
A
Alan Cox 已提交
178 179
	.c_ispeed	= 9600,
	.c_ospeed	= 9600,
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187
};

/*
 *	Define global stats structures. Not used often, and can be
 *	re-used for each stats call.
 */
static comstats_t	stli_comstats;
static combrd_t		stli_brdstats;
188
static struct asystats	stli_cdkstats;
L
Linus Torvalds 已提交
189 190 191

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

192
static DEFINE_MUTEX(stli_brdslock);
193
static struct stlibrd	*stli_brds[STL_MAXBRDS];
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204

static int		stli_shared;

/*
 *	Per board state flags. Used with the state field of the board struct.
 *	Not really much here... All we need to do is keep track of whether
 *	the board has been detected, and whether it is actually running a slave
 *	or not.
 */
#define	BST_FOUND	0x1
#define	BST_STARTED	0x2
205
#define	BST_PROBED	0x4
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

/*
 *	Define the set of port state flags. These are marked for internal
 *	state purposes only, usually to do with the state of communications
 *	with the slave. Most of them need to be updated atomically, so always
 *	use the bit setting operations (unless protected by cli/sti).
 */
#define	ST_INITIALIZING	1
#define	ST_OPENING	2
#define	ST_CLOSING	3
#define	ST_CMDING	4
#define	ST_TXBUSY	5
#define	ST_RXING	6
#define	ST_DOFLUSHRX	7
#define	ST_DOFLUSHTX	8
#define	ST_DOSIGS	9
#define	ST_RXSTOP	10
#define	ST_GETSIGS	11

/*
 *	Define an array of board names as printable strings. Handy for
 *	referencing boards when printing trace and stuff.
 */
static char	*stli_brdnames[] = {
	"Unknown",
	"Stallion",
	"Brumby",
	"ONboard-MC",
	"ONboard",
	"Brumby",
	"Brumby",
	"ONboard-EI",
238
	NULL,
L
Linus Torvalds 已提交
239 240 241
	"ONboard",
	"ONboard-MC",
	"ONboard-MC",
242 243 244 245 246 247 248 249
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
L
Linus Torvalds 已提交
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
	"EasyIO",
	"EC8/32-AT",
	"EC8/32-MC",
	"EC8/64-AT",
	"EC8/64-EI",
	"EC8/64-MC",
	"EC8/32-PCI",
	"EC8/64-PCI",
	"EasyIO-PCI",
	"EC/RA-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.
 */

static char	*board0[8];
static char	*board1[8];
static char	*board2[8];
static char	*board3[8];

static char	**stli_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.
 */

287
static struct stlibrdtype {
L
Linus Torvalds 已提交
288 289
	char	*name;
	int	type;
290
} stli_brdstr[] = {
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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
	{ "stallion", BRD_STALLION },
	{ "1", BRD_STALLION },
	{ "brumby", BRD_BRUMBY },
	{ "brumby4", BRD_BRUMBY },
	{ "brumby/4", BRD_BRUMBY },
	{ "brumby-4", BRD_BRUMBY },
	{ "brumby8", BRD_BRUMBY },
	{ "brumby/8", BRD_BRUMBY },
	{ "brumby-8", BRD_BRUMBY },
	{ "brumby16", BRD_BRUMBY },
	{ "brumby/16", BRD_BRUMBY },
	{ "brumby-16", BRD_BRUMBY },
	{ "2", BRD_BRUMBY },
	{ "onboard2", BRD_ONBOARD2 },
	{ "onboard-2", BRD_ONBOARD2 },
	{ "onboard/2", BRD_ONBOARD2 },
	{ "onboard-mc", BRD_ONBOARD2 },
	{ "onboard/mc", BRD_ONBOARD2 },
	{ "onboard-mca", BRD_ONBOARD2 },
	{ "onboard/mca", BRD_ONBOARD2 },
	{ "3", BRD_ONBOARD2 },
	{ "onboard", BRD_ONBOARD },
	{ "onboardat", BRD_ONBOARD },
	{ "4", BRD_ONBOARD },
	{ "onboarde", BRD_ONBOARDE },
	{ "onboard-e", BRD_ONBOARDE },
	{ "onboard/e", BRD_ONBOARDE },
	{ "onboard-ei", BRD_ONBOARDE },
	{ "onboard/ei", BRD_ONBOARDE },
	{ "7", BRD_ONBOARDE },
	{ "ecp", BRD_ECP },
	{ "ecpat", BRD_ECP },
	{ "ec8/64", BRD_ECP },
	{ "ec8/64-at", BRD_ECP },
	{ "ec8/64-isa", BRD_ECP },
	{ "23", BRD_ECP },
	{ "ecpe", BRD_ECPE },
	{ "ecpei", BRD_ECPE },
	{ "ec8/64-e", BRD_ECPE },
	{ "ec8/64-ei", BRD_ECPE },
	{ "24", BRD_ECPE },
	{ "ecpmc", BRD_ECPMC },
	{ "ec8/64-mc", BRD_ECPMC },
	{ "ec8/64-mca", BRD_ECPMC },
	{ "25", BRD_ECPMC },
	{ "ecppci", BRD_ECPPCI },
	{ "ec/ra", BRD_ECPPCI },
	{ "ec/ra-pc", BRD_ECPPCI },
	{ "ec/ra-pci", BRD_ECPPCI },
	{ "29", BRD_ECPPCI },
};

/*
 *	Define the module agruments.
 */
MODULE_AUTHOR("Greg Ungerer");
MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
MODULE_LICENSE("GPL");


R
Rusty Russell 已提交
351
module_param_array(board0, charp, NULL, 0);
L
Linus Torvalds 已提交
352
MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
R
Rusty Russell 已提交
353
module_param_array(board1, charp, NULL, 0);
L
Linus Torvalds 已提交
354
MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
R
Rusty Russell 已提交
355
module_param_array(board2, charp, NULL, 0);
L
Linus Torvalds 已提交
356
MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
R
Rusty Russell 已提交
357
module_param_array(board3, charp, NULL, 0);
L
Linus Torvalds 已提交
358 359
MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");

360
#if STLI_EISAPROBE != 0
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
/*
 *	Set up a default memory address table for EISA board probing.
 *	The default addresses are all bellow 1Mbyte, which has to be the
 *	case anyway. They should be safe, since we only read values from
 *	them, and interrupts are disabled while we do it. If the higher
 *	memory support is compiled in then we also try probing around
 *	the 1Gb, 2Gb and 3Gb areas as well...
 */
static unsigned long	stli_eisamemprobeaddrs[] = {
	0xc0000,    0xd0000,    0xe0000,    0xf0000,
	0x80000000, 0x80010000, 0x80020000, 0x80030000,
	0x40000000, 0x40010000, 0x40020000, 0x40030000,
	0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
	0xff000000, 0xff010000, 0xff020000, 0xff030000,
};

377
static int	stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs);
378
#endif
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387

/*
 *	Define the Stallion PCI vendor and device IDs.
 */
#ifndef PCI_DEVICE_ID_ECRA
#define	PCI_DEVICE_ID_ECRA		0x0004
#endif

static struct pci_device_id istallion_pci_tbl[] = {
A
Alan Cox 已提交
388
	{ PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), },
L
Linus Torvalds 已提交
389 390 391 392
	{ 0 }
};
MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);

393
static struct pci_driver stli_pcidriver;
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 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

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

/*
 *	Hardware configuration info for ECP boards. These defines apply
 *	to the directly accessible io ports of the ECP. There is a set of
 *	defines for each ECP board type, ISA, EISA, MCA and PCI.
 */
#define	ECP_IOSIZE	4

#define	ECP_MEMSIZE	(128 * 1024)
#define	ECP_PCIMEMSIZE	(256 * 1024)

#define	ECP_ATPAGESIZE	(4 * 1024)
#define	ECP_MCPAGESIZE	(4 * 1024)
#define	ECP_EIPAGESIZE	(64 * 1024)
#define	ECP_PCIPAGESIZE	(64 * 1024)

#define	STL_EISAID	0x8c4e

/*
 *	Important defines for the ISA class of ECP board.
 */
#define	ECP_ATIREG	0
#define	ECP_ATCONFR	1
#define	ECP_ATMEMAR	2
#define	ECP_ATMEMPR	3
#define	ECP_ATSTOP	0x1
#define	ECP_ATINTENAB	0x10
#define	ECP_ATENABLE	0x20
#define	ECP_ATDISABLE	0x00
#define	ECP_ATADDRMASK	0x3f000
#define	ECP_ATADDRSHFT	12

/*
 *	Important defines for the EISA class of ECP board.
 */
#define	ECP_EIIREG	0
#define	ECP_EIMEMARL	1
#define	ECP_EICONFR	2
#define	ECP_EIMEMARH	3
#define	ECP_EIENABLE	0x1
#define	ECP_EIDISABLE	0x0
#define	ECP_EISTOP	0x4
#define	ECP_EIEDGE	0x00
#define	ECP_EILEVEL	0x80
#define	ECP_EIADDRMASKL	0x00ff0000
#define	ECP_EIADDRSHFTL	16
#define	ECP_EIADDRMASKH	0xff000000
#define	ECP_EIADDRSHFTH	24
#define	ECP_EIBRDENAB	0xc84

#define	ECP_EISAID	0x4

/*
 *	Important defines for the Micro-channel class of ECP board.
 *	(It has a lot in common with the ISA boards.)
 */
#define	ECP_MCIREG	0
#define	ECP_MCCONFR	1
#define	ECP_MCSTOP	0x20
#define	ECP_MCENABLE	0x80
#define	ECP_MCDISABLE	0x00

/*
 *	Important defines for the PCI class of ECP board.
 *	(It has a lot in common with the other ECP boards.)
 */
#define	ECP_PCIIREG	0
#define	ECP_PCICONFR	1
#define	ECP_PCISTOP	0x01

/*
 *	Hardware configuration info for ONboard and Brumby boards. These
 *	defines apply to the directly accessible io ports of these boards.
 */
#define	ONB_IOSIZE	16
#define	ONB_MEMSIZE	(64 * 1024)
#define	ONB_ATPAGESIZE	(64 * 1024)
#define	ONB_MCPAGESIZE	(64 * 1024)
#define	ONB_EIMEMSIZE	(128 * 1024)
#define	ONB_EIPAGESIZE	(64 * 1024)

/*
 *	Important defines for the ISA class of ONboard board.
 */
#define	ONB_ATIREG	0
#define	ONB_ATMEMAR	1
#define	ONB_ATCONFR	2
#define	ONB_ATSTOP	0x4
#define	ONB_ATENABLE	0x01
#define	ONB_ATDISABLE	0x00
#define	ONB_ATADDRMASK	0xff0000
#define	ONB_ATADDRSHFT	16

#define	ONB_MEMENABLO	0
#define	ONB_MEMENABHI	0x02

/*
 *	Important defines for the EISA class of ONboard board.
 */
#define	ONB_EIIREG	0
#define	ONB_EIMEMARL	1
#define	ONB_EICONFR	2
#define	ONB_EIMEMARH	3
#define	ONB_EIENABLE	0x1
#define	ONB_EIDISABLE	0x0
#define	ONB_EISTOP	0x4
#define	ONB_EIEDGE	0x00
#define	ONB_EILEVEL	0x80
#define	ONB_EIADDRMASKL	0x00ff0000
#define	ONB_EIADDRSHFTL	16
#define	ONB_EIADDRMASKH	0xff000000
#define	ONB_EIADDRSHFTH	24
#define	ONB_EIBRDENAB	0xc84

#define	ONB_EISAID	0x1

/*
 *	Important defines for the Brumby boards. They are pretty simple,
 *	there is not much that is programmably configurable.
 */
#define	BBY_IOSIZE	16
#define	BBY_MEMSIZE	(64 * 1024)
#define	BBY_PAGESIZE	(16 * 1024)

#define	BBY_ATIREG	0
#define	BBY_ATCONFR	1
#define	BBY_ATSTOP	0x4

/*
 *	Important defines for the Stallion boards. They are pretty simple,
 *	there is not much that is programmably configurable.
 */
#define	STAL_IOSIZE	16
#define	STAL_MEMSIZE	(64 * 1024)
#define	STAL_PAGESIZE	(64 * 1024)

/*
 *	Define the set of status register values for EasyConnection panels.
 *	The signature will return with the status value for each panel. From
 *	this we can determine what is attached to the board - before we have
 *	actually down loaded any code to it.
 */
#define	ECH_PNLSTATUS	2
#define	ECH_PNL16PORT	0x20
#define	ECH_PNLIDMASK	0x07
#define	ECH_PNLXPID	0x40
#define	ECH_PNLINTRPEND	0x80

/*
 *	Define some macros to do things to the board. Even those these boards
 *	are somewhat related there is often significantly different ways of
 *	doing some operation on it (like enable, paging, reset, etc). So each
 *	board class has a set of functions which do the commonly required
 *	operations. The macros below basically just call these functions,
 *	generally checking for a NULL function - which means that the board
 *	needs nothing done to it to achieve this operation!
 */
#define	EBRDINIT(brdp)						\
	if (brdp->init != NULL)					\
		(* brdp->init)(brdp)

#define	EBRDENABLE(brdp)					\
	if (brdp->enable != NULL)				\
		(* brdp->enable)(brdp);

#define	EBRDDISABLE(brdp)					\
	if (brdp->disable != NULL)				\
		(* brdp->disable)(brdp);

#define	EBRDINTR(brdp)						\
	if (brdp->intr != NULL)					\
		(* brdp->intr)(brdp);

#define	EBRDRESET(brdp)						\
	if (brdp->reset != NULL)				\
		(* brdp->reset)(brdp);

#define	EBRDGETMEMPTR(brdp,offset)				\
	(* brdp->getmemptr)(brdp, offset, __LINE__)

/*
 *	Define the maximal baud rate, and the default baud base for ports.
 */
#define	STL_MAXBAUD	460800
#define	STL_BAUDBASE	115200
#define	STL_CLOSEDELAY	(5 * HZ / 10)

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

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

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

/*
 *	Prototype all functions in this driver!
 */

597
static int	stli_parsebrd(struct stlconf *confp, char **argp);
L
Linus Torvalds 已提交
598 599 600
static int	stli_open(struct tty_struct *tty, struct file *filp);
static void	stli_close(struct tty_struct *tty, struct file *filp);
static int	stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
W
Wang Chen 已提交
601
static int	stli_putchar(struct tty_struct *tty, unsigned char ch);
L
Linus Torvalds 已提交
602 603 604 605
static void	stli_flushchars(struct tty_struct *tty);
static int	stli_writeroom(struct tty_struct *tty);
static int	stli_charsinbuffer(struct tty_struct *tty);
static int	stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
A
Alan Cox 已提交
606
static void	stli_settermios(struct tty_struct *tty, struct ktermios *old);
L
Linus Torvalds 已提交
607 608 609 610 611
static void	stli_throttle(struct tty_struct *tty);
static void	stli_unthrottle(struct tty_struct *tty);
static void	stli_stop(struct tty_struct *tty);
static void	stli_start(struct tty_struct *tty);
static void	stli_flushbuffer(struct tty_struct *tty);
A
Alan Cox 已提交
612
static int	stli_breakctl(struct tty_struct *tty, int state);
L
Linus Torvalds 已提交
613 614 615
static void	stli_waituntilsent(struct tty_struct *tty, int timeout);
static void	stli_sendxchar(struct tty_struct *tty, char ch);
static void	stli_hangup(struct tty_struct *tty);
616
static int	stli_portinfo(struct stlibrd *brdp, struct stliport *portp, int portnr, char *pos);
L
Linus Torvalds 已提交
617

618 619
static int	stli_brdinit(struct stlibrd *brdp);
static int	stli_startbrd(struct stlibrd *brdp);
L
Linus Torvalds 已提交
620 621 622
static ssize_t	stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
static ssize_t	stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
static int	stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
623
static void	stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp);
L
Linus Torvalds 已提交
624
static void	stli_poll(unsigned long arg);
625
static int	stli_hostcmd(struct stlibrd *brdp, struct stliport *portp);
A
Alan Cox 已提交
626
static int	stli_initopen(struct tty_struct *tty, struct stlibrd *brdp, struct stliport *portp);
627 628
static int	stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait);
static int	stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait);
A
Alan Cox 已提交
629 630 631
static int	stli_waitcarrier(struct tty_struct *tty, struct stlibrd *brdp,
				struct stliport *portp, struct file *filp);
static int	stli_setport(struct tty_struct *tty);
632 633 634 635
static int	stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
static void	stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
static void	__stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
static void	stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp);
A
Alan Cox 已提交
636
static void	stli_mkasyport(struct tty_struct *tty, struct stliport *portp, asyport_t *pp, struct ktermios *tiosp);
L
Linus Torvalds 已提交
637 638
static void	stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
static long	stli_mktiocm(unsigned long sigvalue);
639 640
static void	stli_read(struct stlibrd *brdp, struct stliport *portp);
static int	stli_getserial(struct stliport *portp, struct serial_struct __user *sp);
A
Alan Cox 已提交
641
static int	stli_setserial(struct tty_struct *tty, struct serial_struct __user *sp);
L
Linus Torvalds 已提交
642
static int	stli_getbrdstats(combrd_t __user *bp);
A
Alan Cox 已提交
643 644
static int	stli_getportstats(struct tty_struct *tty, struct stliport *portp, comstats_t __user *cp);
static int	stli_portcmdstats(struct tty_struct *tty, struct stliport *portp);
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
static int	stli_clrportstats(struct stliport *portp, comstats_t __user *cp);
static int	stli_getportstruct(struct stliport __user *arg);
static int	stli_getbrdstruct(struct stlibrd __user *arg);
static struct stlibrd *stli_allocbrd(void);

static void	stli_ecpinit(struct stlibrd *brdp);
static void	stli_ecpenable(struct stlibrd *brdp);
static void	stli_ecpdisable(struct stlibrd *brdp);
static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_ecpreset(struct stlibrd *brdp);
static void	stli_ecpintr(struct stlibrd *brdp);
static void	stli_ecpeiinit(struct stlibrd *brdp);
static void	stli_ecpeienable(struct stlibrd *brdp);
static void	stli_ecpeidisable(struct stlibrd *brdp);
static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_ecpeireset(struct stlibrd *brdp);
static void	stli_ecpmcenable(struct stlibrd *brdp);
static void	stli_ecpmcdisable(struct stlibrd *brdp);
static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_ecpmcreset(struct stlibrd *brdp);
static void	stli_ecppciinit(struct stlibrd *brdp);
static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_ecppcireset(struct stlibrd *brdp);

static void	stli_onbinit(struct stlibrd *brdp);
static void	stli_onbenable(struct stlibrd *brdp);
static void	stli_onbdisable(struct stlibrd *brdp);
static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_onbreset(struct stlibrd *brdp);
static void	stli_onbeinit(struct stlibrd *brdp);
static void	stli_onbeenable(struct stlibrd *brdp);
static void	stli_onbedisable(struct stlibrd *brdp);
static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_onbereset(struct stlibrd *brdp);
static void	stli_bbyinit(struct stlibrd *brdp);
static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_bbyreset(struct stlibrd *brdp);
static void	stli_stalinit(struct stlibrd *brdp);
static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
static void	stli_stalreset(struct stlibrd *brdp);

686
static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr, unsigned int portnr);
687 688 689

static int	stli_initecp(struct stlibrd *brdp);
static int	stli_initonb(struct stlibrd *brdp);
690
#if STLI_EISAPROBE != 0
691
static int	stli_eisamemprobe(struct stlibrd *brdp);
692
#endif
693
static int	stli_initports(struct stlibrd *brdp);
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701 702

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

/*
 *	Define the driver info for a user level shared memory device. This
 *	device will work sort of like the /dev/kmem device - except that it
 *	will give access to the shared memory on the Stallion intelligent
 *	board. This is also a very useful debugging tool.
 */
703
static const struct file_operations	stli_fsiomem = {
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717
	.owner		= THIS_MODULE,
	.read		= stli_memread,
	.write		= stli_memwrite,
	.ioctl		= stli_memioctl,
};

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

/*
 *	Define a timer_list entry for our poll routine. The slave board
 *	is polled every so often to see if anything needs doing. This is
 *	much cheaper on host cpu than using interrupts. It turns out to
 *	not increase character latency by much either...
 */
718
static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0);
L
Linus Torvalds 已提交
719 720 721 722 723 724 725 726 727 728

static int	stli_timeron;

/*
 *	Define the calculation for the timeout routine.
 */
#define	STLI_TIMEOUT	(jiffies + 1)

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

729
static struct class *istallion_class;
L
Linus Torvalds 已提交
730

731
static void stli_cleanup_ports(struct stlibrd *brdp)
732
{
733
	struct stliport *portp;
734
	unsigned int j;
A
Alan Cox 已提交
735
	struct tty_struct *tty;
736 737 738 739

	for (j = 0; j < STL_MAXPORTS; j++) {
		portp = brdp->ports[j];
		if (portp != NULL) {
A
Alan Cox 已提交
740 741 742 743 744
			tty = tty_port_tty_get(&portp->port);
			if (tty != NULL) {
				tty_hangup(tty);
				tty_kref_put(tty);
			}
745 746 747 748 749
			kfree(portp);
		}
	}
}

L
Linus Torvalds 已提交
750 751 752 753 754 755
/*****************************************************************************/

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

756
static int stli_parsebrd(struct stlconf *confp, char **argp)
L
Linus Torvalds 已提交
757
{
758
	unsigned int i;
A
Alan Cox 已提交
759
	char *sp;
L
Linus Torvalds 已提交
760

A
Alan Cox 已提交
761 762
	if (argp[0] == NULL || *argp[0] == 0)
		return 0;
L
Linus Torvalds 已提交
763 764

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

767
	for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) {
L
Linus Torvalds 已提交
768 769 770
		if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
			break;
	}
771
	if (i == ARRAY_SIZE(stli_brdstr)) {
L
Linus Torvalds 已提交
772
		printk("STALLION: unknown board name, %s?\n", argp[0]);
773
		return 0;
L
Linus Torvalds 已提交
774 775 776
	}

	confp->brdtype = stli_brdstr[i].type;
A
Alan Cox 已提交
777
	if (argp[1] != NULL && *argp[1] != 0)
778
		confp->ioaddr1 = simple_strtoul(argp[1], NULL, 0);
A
Alan Cox 已提交
779
	if (argp[2] !=  NULL && *argp[2] != 0)
780
		confp->memaddr = simple_strtoul(argp[2], NULL, 0);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787
	return(1);
}

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

static int stli_open(struct tty_struct *tty, struct file *filp)
{
788 789
	struct stlibrd *brdp;
	struct stliport *portp;
790 791
	unsigned int minordev, brdnr, portnr;
	int rc;
L
Linus Torvalds 已提交
792 793 794 795

	minordev = tty->index;
	brdnr = MINOR2BRD(minordev);
	if (brdnr >= stli_nrbrds)
A
Alan Cox 已提交
796
		return -ENODEV;
L
Linus Torvalds 已提交
797
	brdp = stli_brds[brdnr];
A
Alan Cox 已提交
798 799
	if (brdp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
800
	if ((brdp->state & BST_STARTED) == 0)
A
Alan Cox 已提交
801
		return -ENODEV;
L
Linus Torvalds 已提交
802
	portnr = MINOR2PORT(minordev);
803
	if (portnr > brdp->nrports)
A
Alan Cox 已提交
804
		return -ENODEV;
L
Linus Torvalds 已提交
805 806

	portp = brdp->ports[portnr];
A
Alan Cox 已提交
807 808
	if (portp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
809
	if (portp->devnr < 1)
A
Alan Cox 已提交
810
		return -ENODEV;
L
Linus Torvalds 已提交
811 812 813 814 815 816 817 818


/*
 *	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.
 */
A
Alan Cox 已提交
819 820 821
	if (portp->port.flags & ASYNC_CLOSING) {
		interruptible_sleep_on(&portp->port.close_wait);
		if (portp->port.flags & ASYNC_HUP_NOTIFY)
A
Alan Cox 已提交
822 823
			return -EAGAIN;
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831
	}

/*
 *	On the first open of the device setup the port hardware, and
 *	initialize the per port data structure. Since initializing the port
 *	requires several commands to the board we will need to wait for any
 *	other open that is already initializing the port.
 */
A
Alan Cox 已提交
832
	tty_port_tty_set(&portp->port, tty);
L
Linus Torvalds 已提交
833
	tty->driver_data = portp;
W
Wang Chen 已提交
834
	portp->port.count++;
L
Linus Torvalds 已提交
835 836 837 838

	wait_event_interruptible(portp->raw_wait,
			!test_bit(ST_INITIALIZING, &portp->state));
	if (signal_pending(current))
A
Alan Cox 已提交
839
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
840

A
Alan Cox 已提交
841
	if ((portp->port.flags & ASYNC_INITIALIZED) == 0) {
L
Linus Torvalds 已提交
842
		set_bit(ST_INITIALIZING, &portp->state);
A
Alan Cox 已提交
843
		if ((rc = stli_initopen(tty, brdp, portp)) >= 0) {
A
Alan Cox 已提交
844
			portp->port.flags |= ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
845 846 847 848 849
			clear_bit(TTY_IO_ERROR, &tty->flags);
		}
		clear_bit(ST_INITIALIZING, &portp->state);
		wake_up_interruptible(&portp->raw_wait);
		if (rc < 0)
A
Alan Cox 已提交
850
			return rc;
L
Linus Torvalds 已提交
851 852 853 854 855 856 857 858
	}

/*
 *	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.
 */
A
Alan Cox 已提交
859 860 861
	if (portp->port.flags & ASYNC_CLOSING) {
		interruptible_sleep_on(&portp->port.close_wait);
		if (portp->port.flags & ASYNC_HUP_NOTIFY)
A
Alan Cox 已提交
862 863
			return -EAGAIN;
		return -ERESTARTSYS;
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871
	}

/*
 *	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.
 */
	if (!(filp->f_flags & O_NONBLOCK)) {
A
Alan Cox 已提交
872
		if ((rc = stli_waitcarrier(tty, brdp, portp, filp)) != 0)
A
Alan Cox 已提交
873
			return rc;
L
Linus Torvalds 已提交
874
	}
A
Alan Cox 已提交
875
	portp->port.flags |= ASYNC_NORMAL_ACTIVE;
A
Alan Cox 已提交
876
	return 0;
L
Linus Torvalds 已提交
877 878 879 880 881 882
}

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

static void stli_close(struct tty_struct *tty, struct file *filp)
{
883 884
	struct stlibrd *brdp;
	struct stliport *portp;
A
Alan Cox 已提交
885
	unsigned long flags;
L
Linus Torvalds 已提交
886 887

	portp = tty->driver_data;
A
Alan Cox 已提交
888
	if (portp == NULL)
L
Linus Torvalds 已提交
889 890
		return;

A
Alan Cox 已提交
891
	spin_lock_irqsave(&stli_lock, flags);
L
Linus Torvalds 已提交
892
	if (tty_hung_up_p(filp)) {
A
Alan Cox 已提交
893
		spin_unlock_irqrestore(&stli_lock, flags);
L
Linus Torvalds 已提交
894 895
		return;
	}
W
Wang Chen 已提交
896 897 898
	if ((tty->count == 1) && (portp->port.count != 1))
		portp->port.count = 1;
	if (portp->port.count-- > 1) {
A
Alan Cox 已提交
899
		spin_unlock_irqrestore(&stli_lock, flags);
L
Linus Torvalds 已提交
900 901 902
		return;
	}

A
Alan Cox 已提交
903
	portp->port.flags |= ASYNC_CLOSING;
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912 913

/*
 *	May want to wait for data to drain before closing. The BUSY flag
 *	keeps track of whether we are still transmitting or not. It is
 *	updated by messages from the slave - indicating when all chars
 *	really have drained.
 */
	if (tty == stli_txcooktty)
		stli_flushchars(tty);
	tty->closing = 1;
A
Alan Cox 已提交
914 915
	spin_unlock_irqrestore(&stli_lock, flags);

L
Linus Torvalds 已提交
916 917 918
	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
		tty_wait_until_sent(tty, portp->closing_wait);

A
Alan Cox 已提交
919
	portp->port.flags &= ~ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932
	brdp = stli_brds[portp->brdnr];
	stli_rawclose(brdp, portp, 0, 0);
	if (tty->termios->c_cflag & HUPCL) {
		stli_mkasysigs(&portp->asig, 0, 0);
		if (test_bit(ST_CMDING, &portp->state))
			set_bit(ST_DOSIGS, &portp->state);
		else
			stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
				sizeof(asysigs_t), 0);
	}
	clear_bit(ST_TXBUSY, &portp->state);
	clear_bit(ST_RXSTOP, &portp->state);
	set_bit(TTY_IO_ERROR, &tty->flags);
A
Alan Cox 已提交
933
	tty_ldisc_flush(tty);
L
Linus Torvalds 已提交
934 935 936 937
	set_bit(ST_DOFLUSHRX, &portp->state);
	stli_flushbuffer(tty);

	tty->closing = 0;
A
Alan Cox 已提交
938
	tty_port_tty_set(&portp->port, NULL);
L
Linus Torvalds 已提交
939 940 941 942

	if (portp->openwaitcnt) {
		if (portp->close_delay)
			msleep_interruptible(jiffies_to_msecs(portp->close_delay));
A
Alan Cox 已提交
943
		wake_up_interruptible(&portp->port.open_wait);
L
Linus Torvalds 已提交
944 945
	}

A
Alan Cox 已提交
946 947
	portp->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
	wake_up_interruptible(&portp->port.close_wait);
L
Linus Torvalds 已提交
948 949 950 951 952 953 954 955 956 957 958 959
}

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

/*
 *	Carry out first open operations on a port. This involves a number of
 *	commands to be sent to the slave. We need to open the port, set the
 *	notification events, set the initial port settings, get and set the
 *	initial signal values. We sleep and wait in between each one. But
 *	this still all happens pretty quickly.
 */

A
Alan Cox 已提交
960 961
static int stli_initopen(struct tty_struct *tty,
				struct stlibrd *brdp, struct stliport *portp)
L
Linus Torvalds 已提交
962
{
A
Alan Cox 已提交
963 964 965
	asynotify_t nt;
	asyport_t aport;
	int rc;
L
Linus Torvalds 已提交
966 967

	if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
A
Alan Cox 已提交
968
		return rc;
L
Linus Torvalds 已提交
969 970 971 972 973 974

	memset(&nt, 0, sizeof(asynotify_t));
	nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
	nt.signal = SG_DCD;
	if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
	    sizeof(asynotify_t), 0)) < 0)
A
Alan Cox 已提交
975
		return rc;
L
Linus Torvalds 已提交
976

A
Alan Cox 已提交
977
	stli_mkasyport(tty, portp, &aport, tty->termios);
L
Linus Torvalds 已提交
978 979
	if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
	    sizeof(asyport_t), 0)) < 0)
A
Alan Cox 已提交
980
		return rc;
L
Linus Torvalds 已提交
981 982 983 984

	set_bit(ST_GETSIGS, &portp->state);
	if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
	    sizeof(asysigs_t), 1)) < 0)
A
Alan Cox 已提交
985
		return rc;
L
Linus Torvalds 已提交
986 987 988 989 990
	if (test_and_clear_bit(ST_GETSIGS, &portp->state))
		portp->sigs = stli_mktiocm(portp->asig.sigvalue);
	stli_mkasysigs(&portp->asig, 1, 1);
	if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
	    sizeof(asysigs_t), 0)) < 0)
A
Alan Cox 已提交
991
		return rc;
L
Linus Torvalds 已提交
992

A
Alan Cox 已提交
993
	return 0;
L
Linus Torvalds 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004
}

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

/*
 *	Send an open message to the slave. This will sleep waiting for the
 *	acknowledgement, so must have user context. We need to co-ordinate
 *	with close events here, since we don't want open and close events
 *	to overlap.
 */

1005
static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait)
L
Linus Torvalds 已提交
1006
{
A
Alan Cox 已提交
1007 1008 1009 1010 1011
	cdkhdr_t __iomem *hdrp;
	cdkctrl_t __iomem *cp;
	unsigned char __iomem *bits;
	unsigned long flags;
	int rc;
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

/*
 *	Send a message to the slave to open this port.
 */

/*
 *	Slave is already closing this port. This can happen if a hangup
 *	occurs on this port. So we must wait until it is complete. The
 *	order of opens and closes may not be preserved across shared
 *	memory, so we must wait until it is complete.
 */
	wait_event_interruptible(portp->raw_wait,
			!test_bit(ST_CLOSING, &portp->state));
	if (signal_pending(current)) {
		return -ERESTARTSYS;
	}

/*
 *	Everything is ready now, so write the open message into shared
 *	memory. Once the message is in set the service bits to say that
 *	this port wants service.
 */
A
Alan Cox 已提交
1034
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1035
	EBRDENABLE(brdp);
A
Alan Cox 已提交
1036 1037 1038 1039 1040
	cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
	writel(arg, &cp->openarg);
	writeb(1, &cp->open);
	hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
	bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
L
Linus Torvalds 已提交
1041
		portp->portidx;
A
Alan Cox 已提交
1042
	writeb(readb(bits) | portp->portbit, bits);
L
Linus Torvalds 已提交
1043 1044 1045
	EBRDDISABLE(brdp);

	if (wait == 0) {
A
Alan Cox 已提交
1046 1047
		spin_unlock_irqrestore(&brd_lock, flags);
		return 0;
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055
	}

/*
 *	Slave is in action, so now we must wait for the open acknowledgment
 *	to come back.
 */
	rc = 0;
	set_bit(ST_OPENING, &portp->state);
A
Alan Cox 已提交
1056 1057
	spin_unlock_irqrestore(&brd_lock, flags);

L
Linus Torvalds 已提交
1058 1059 1060 1061 1062 1063 1064
	wait_event_interruptible(portp->raw_wait,
			!test_bit(ST_OPENING, &portp->state));
	if (signal_pending(current))
		rc = -ERESTARTSYS;

	if ((rc == 0) && (portp->rc != 0))
		rc = -EIO;
A
Alan Cox 已提交
1065
	return rc;
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
}

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

/*
 *	Send a close message to the slave. Normally this will sleep waiting
 *	for the acknowledgement, but if wait parameter is 0 it will not. If
 *	wait is true then must have user context (to sleep).
 */

1076
static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait)
L
Linus Torvalds 已提交
1077
{
A
Alan Cox 已提交
1078 1079 1080 1081 1082
	cdkhdr_t __iomem *hdrp;
	cdkctrl_t __iomem *cp;
	unsigned char __iomem *bits;
	unsigned long flags;
	int rc;
L
Linus Torvalds 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

/*
 *	Slave is already closing this port. This can happen if a hangup
 *	occurs on this port.
 */
	if (wait) {
		wait_event_interruptible(portp->raw_wait,
				!test_bit(ST_CLOSING, &portp->state));
		if (signal_pending(current)) {
			return -ERESTARTSYS;
		}
	}

/*
 *	Write the close command into shared memory.
 */
A
Alan Cox 已提交
1099
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1100
	EBRDENABLE(brdp);
A
Alan Cox 已提交
1101 1102 1103 1104 1105
	cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
	writel(arg, &cp->closearg);
	writeb(1, &cp->close);
	hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
	bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
L
Linus Torvalds 已提交
1106
		portp->portidx;
A
Alan Cox 已提交
1107
	writeb(readb(bits) |portp->portbit, bits);
L
Linus Torvalds 已提交
1108 1109 1110
	EBRDDISABLE(brdp);

	set_bit(ST_CLOSING, &portp->state);
A
Alan Cox 已提交
1111 1112 1113 1114
	spin_unlock_irqrestore(&brd_lock, flags);

	if (wait == 0)
		return 0;
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

/*
 *	Slave is in action, so now we must wait for the open acknowledgment
 *	to come back.
 */
	rc = 0;
	wait_event_interruptible(portp->raw_wait,
			!test_bit(ST_CLOSING, &portp->state));
	if (signal_pending(current))
		rc = -ERESTARTSYS;

	if ((rc == 0) && (portp->rc != 0))
		rc = -EIO;
A
Alan Cox 已提交
1128
	return rc;
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
}

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

/*
 *	Send a command to the slave and wait for the response. This must
 *	have user context (it sleeps). This routine is generic in that it
 *	can send any type of command. Its purpose is to wait for that command
 *	to complete (as opposed to initiating the command then returning).
 */

1140
static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
L
Linus Torvalds 已提交
1141 1142 1143
{
	wait_event_interruptible(portp->raw_wait,
			!test_bit(ST_CMDING, &portp->state));
A
Alan Cox 已提交
1144
	if (signal_pending(current))
L
Linus Torvalds 已提交
1145 1146 1147 1148 1149 1150
		return -ERESTARTSYS;

	stli_sendcmd(brdp, portp, cmd, arg, size, copyback);

	wait_event_interruptible(portp->raw_wait,
			!test_bit(ST_CMDING, &portp->state));
A
Alan Cox 已提交
1151
	if (signal_pending(current))
L
Linus Torvalds 已提交
1152 1153 1154
		return -ERESTARTSYS;

	if (portp->rc != 0)
A
Alan Cox 已提交
1155 1156
		return -EIO;
	return 0;
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165
}

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

/*
 *	Send the termios settings for this port to the slave. This sleeps
 *	waiting for the command to complete - so must have user context.
 */

A
Alan Cox 已提交
1166
static int stli_setport(struct tty_struct *tty)
L
Linus Torvalds 已提交
1167
{
A
Alan Cox 已提交
1168
	struct stliport *portp = tty->driver_data;
1169
	struct stlibrd *brdp;
A
Alan Cox 已提交
1170
	asyport_t aport;
L
Linus Torvalds 已提交
1171

A
Alan Cox 已提交
1172 1173
	if (portp == NULL)
		return -ENODEV;
1174
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1175
		return -ENODEV;
L
Linus Torvalds 已提交
1176
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1177 1178
	if (brdp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
1179

A
Alan Cox 已提交
1180
	stli_mkasyport(tty, portp, &aport, tty->termios);
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
}

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

/*
 *	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...
 */

A
Alan Cox 已提交
1191 1192
static int stli_waitcarrier(struct tty_struct *tty, struct stlibrd *brdp,
				struct stliport *portp, struct file *filp)
L
Linus Torvalds 已提交
1193
{
A
Alan Cox 已提交
1194 1195
	unsigned long flags;
	int rc, doclocal;
L
Linus Torvalds 已提交
1196 1197 1198 1199

	rc = 0;
	doclocal = 0;

A
Alan Cox 已提交
1200
	if (tty->termios->c_cflag & CLOCAL)
L
Linus Torvalds 已提交
1201 1202
		doclocal++;

A
Alan Cox 已提交
1203
	spin_lock_irqsave(&stli_lock, flags);
L
Linus Torvalds 已提交
1204 1205
	portp->openwaitcnt++;
	if (! tty_hung_up_p(filp))
W
Wang Chen 已提交
1206
		portp->port.count--;
A
Alan Cox 已提交
1207
	spin_unlock_irqrestore(&stli_lock, flags);
L
Linus Torvalds 已提交
1208 1209 1210 1211 1212 1213 1214

	for (;;) {
		stli_mkasysigs(&portp->asig, 1, 1);
		if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
		    &portp->asig, sizeof(asysigs_t), 0)) < 0)
			break;
		if (tty_hung_up_p(filp) ||
A
Alan Cox 已提交
1215 1216
		    ((portp->port.flags & ASYNC_INITIALIZED) == 0)) {
			if (portp->port.flags & ASYNC_HUP_NOTIFY)
L
Linus Torvalds 已提交
1217 1218 1219 1220 1221
				rc = -EBUSY;
			else
				rc = -ERESTARTSYS;
			break;
		}
A
Alan Cox 已提交
1222
		if (((portp->port.flags & ASYNC_CLOSING) == 0) &&
L
Linus Torvalds 已提交
1223 1224 1225 1226 1227 1228 1229
		    (doclocal || (portp->sigs & TIOCM_CD))) {
			break;
		}
		if (signal_pending(current)) {
			rc = -ERESTARTSYS;
			break;
		}
A
Alan Cox 已提交
1230
		interruptible_sleep_on(&portp->port.open_wait);
L
Linus Torvalds 已提交
1231 1232
	}

A
Alan Cox 已提交
1233
	spin_lock_irqsave(&stli_lock, flags);
L
Linus Torvalds 已提交
1234
	if (! tty_hung_up_p(filp))
W
Wang Chen 已提交
1235
		portp->port.count++;
L
Linus Torvalds 已提交
1236
	portp->openwaitcnt--;
A
Alan Cox 已提交
1237
	spin_unlock_irqrestore(&stli_lock, flags);
L
Linus Torvalds 已提交
1238

A
Alan Cox 已提交
1239
	return rc;
L
Linus Torvalds 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
}

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

/*
 *	Write routine. Take the data and put it in the shared memory ring
 *	queue. If port is not already sending chars then need to mark the
 *	service bits for this port.
 */

static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
A
Alan Cox 已提交
1252 1253 1254 1255 1256
	cdkasy_t __iomem *ap;
	cdkhdr_t __iomem *hdrp;
	unsigned char __iomem *bits;
	unsigned char __iomem *shbuf;
	unsigned char *chbuf;
1257 1258
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1259 1260
	unsigned int len, stlen, head, tail, size;
	unsigned long flags;
L
Linus Torvalds 已提交
1261 1262 1263 1264

	if (tty == stli_txcooktty)
		stli_flushchars(tty);
	portp = tty->driver_data;
A
Alan Cox 已提交
1265 1266
	if (portp == NULL)
		return 0;
1267
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1268
		return 0;
L
Linus Torvalds 已提交
1269
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1270 1271
	if (brdp == NULL)
		return 0;
L
Linus Torvalds 已提交
1272 1273 1274 1275 1276
	chbuf = (unsigned char *) buf;

/*
 *	All data is now local, shove as much as possible into shared memory.
 */
A
Alan Cox 已提交
1277
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1278
	EBRDENABLE(brdp);
A
Alan Cox 已提交
1279 1280 1281 1282 1283
	ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
	head = (unsigned int) readw(&ap->txq.head);
	tail = (unsigned int) readw(&ap->txq.tail);
	if (tail != ((unsigned int) readw(&ap->txq.tail)))
		tail = (unsigned int) readw(&ap->txq.tail);
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292
	size = portp->txsize;
	if (head >= tail) {
		len = size - (head - tail) - 1;
		stlen = size - head;
	} else {
		len = tail - head - 1;
		stlen = len;
	}

1293
	len = min(len, (unsigned int)count);
L
Linus Torvalds 已提交
1294
	count = 0;
A
Alan Cox 已提交
1295
	shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset);
L
Linus Torvalds 已提交
1296 1297

	while (len > 0) {
1298
		stlen = min(len, stlen);
A
Alan Cox 已提交
1299
		memcpy_toio(shbuf + head, chbuf, stlen);
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
		chbuf += stlen;
		len -= stlen;
		count += stlen;
		head += stlen;
		if (head >= size) {
			head = 0;
			stlen = tail;
		}
	}

A
Alan Cox 已提交
1310 1311
	ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
	writew(head, &ap->txq.head);
L
Linus Torvalds 已提交
1312
	if (test_bit(ST_TXBUSY, &portp->state)) {
A
Alan Cox 已提交
1313 1314
		if (readl(&ap->changed.data) & DT_TXEMPTY)
			writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
L
Linus Torvalds 已提交
1315
	}
A
Alan Cox 已提交
1316 1317
	hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
	bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
L
Linus Torvalds 已提交
1318
		portp->portidx;
A
Alan Cox 已提交
1319
	writeb(readb(bits) | portp->portbit, bits);
L
Linus Torvalds 已提交
1320 1321
	set_bit(ST_TXBUSY, &portp->state);
	EBRDDISABLE(brdp);
A
Alan Cox 已提交
1322
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

	return(count);
}

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

/*
 *	Output a single character. We put it into a temporary local buffer
 *	(for speed) then write out that buffer when the flushchars routine
 *	is called. There is a safety catch here so that if some other port
 *	writes chars before the current buffer has been, then we write them
 *	first them do the new ports.
 */

W
Wang Chen 已提交
1337
static int stli_putchar(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
1338 1339
{
	if (tty != stli_txcooktty) {
A
Alan Cox 已提交
1340
		if (stli_txcooktty != NULL)
L
Linus Torvalds 已提交
1341 1342 1343 1344 1345
			stli_flushchars(stli_txcooktty);
		stli_txcooktty = tty;
	}

	stli_txcookbuf[stli_txcooksize++] = ch;
W
Wang Chen 已提交
1346
	return 0;
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
}

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

/*
 *	Transfer characters from the local TX cooking buffer to the board.
 *	We sort of ignore the tty that gets passed in here. We rely on the
 *	info stored with the TX cook buffer to tell us which port to flush
 *	the data on. In any case we clean out the TX cook buffer, for re-use
 *	by someone else.
 */

static void stli_flushchars(struct tty_struct *tty)
{
A
Alan Cox 已提交
1361 1362 1363 1364
	cdkhdr_t __iomem *hdrp;
	unsigned char __iomem *bits;
	cdkasy_t __iomem *ap;
	struct tty_struct *cooktty;
1365 1366
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1367 1368 1369 1370
	unsigned int len, stlen, head, tail, size, count, cooksize;
	unsigned char *buf;
	unsigned char __iomem *shbuf;
	unsigned long flags;
L
Linus Torvalds 已提交
1371 1372 1373 1374 1375

	cooksize = stli_txcooksize;
	cooktty = stli_txcooktty;
	stli_txcooksize = 0;
	stli_txcookrealsize = 0;
A
Alan Cox 已提交
1376
	stli_txcooktty = NULL;
L
Linus Torvalds 已提交
1377

A
Alan Cox 已提交
1378
	if (cooktty == NULL)
L
Linus Torvalds 已提交
1379 1380 1381 1382 1383 1384 1385
		return;
	if (tty != cooktty)
		tty = cooktty;
	if (cooksize == 0)
		return;

	portp = tty->driver_data;
A
Alan Cox 已提交
1386
	if (portp == NULL)
L
Linus Torvalds 已提交
1387
		return;
1388
	if (portp->brdnr >= stli_nrbrds)
L
Linus Torvalds 已提交
1389 1390
		return;
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1391
	if (brdp == NULL)
L
Linus Torvalds 已提交
1392 1393
		return;

A
Alan Cox 已提交
1394
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1395 1396
	EBRDENABLE(brdp);

A
Alan Cox 已提交
1397 1398 1399 1400 1401
	ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
	head = (unsigned int) readw(&ap->txq.head);
	tail = (unsigned int) readw(&ap->txq.tail);
	if (tail != ((unsigned int) readw(&ap->txq.tail)))
		tail = (unsigned int) readw(&ap->txq.tail);
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410
	size = portp->txsize;
	if (head >= tail) {
		len = size - (head - tail) - 1;
		stlen = size - head;
	} else {
		len = tail - head - 1;
		stlen = len;
	}

1411
	len = min(len, cooksize);
L
Linus Torvalds 已提交
1412
	count = 0;
1413
	shbuf = EBRDGETMEMPTR(brdp, portp->txoffset);
L
Linus Torvalds 已提交
1414 1415 1416
	buf = stli_txcookbuf;

	while (len > 0) {
1417
		stlen = min(len, stlen);
A
Alan Cox 已提交
1418
		memcpy_toio(shbuf + head, buf, stlen);
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
		buf += stlen;
		len -= stlen;
		count += stlen;
		head += stlen;
		if (head >= size) {
			head = 0;
			stlen = tail;
		}
	}

A
Alan Cox 已提交
1429 1430
	ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
	writew(head, &ap->txq.head);
L
Linus Torvalds 已提交
1431 1432

	if (test_bit(ST_TXBUSY, &portp->state)) {
A
Alan Cox 已提交
1433 1434
		if (readl(&ap->changed.data) & DT_TXEMPTY)
			writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
L
Linus Torvalds 已提交
1435
	}
A
Alan Cox 已提交
1436 1437
	hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
	bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
L
Linus Torvalds 已提交
1438
		portp->portidx;
A
Alan Cox 已提交
1439
	writeb(readb(bits) | portp->portbit, bits);
L
Linus Torvalds 已提交
1440 1441 1442
	set_bit(ST_TXBUSY, &portp->state);

	EBRDDISABLE(brdp);
A
Alan Cox 已提交
1443
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449
}

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

static int stli_writeroom(struct tty_struct *tty)
{
A
Alan Cox 已提交
1450
	cdkasyrq_t __iomem *rp;
1451 1452
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1453 1454
	unsigned int head, tail, len;
	unsigned long flags;
L
Linus Torvalds 已提交
1455 1456 1457 1458

	if (tty == stli_txcooktty) {
		if (stli_txcookrealsize != 0) {
			len = stli_txcookrealsize - stli_txcooksize;
A
Alan Cox 已提交
1459
			return len;
L
Linus Torvalds 已提交
1460 1461 1462 1463
		}
	}

	portp = tty->driver_data;
A
Alan Cox 已提交
1464 1465
	if (portp == NULL)
		return 0;
1466
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1467
		return 0;
L
Linus Torvalds 已提交
1468
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1469 1470
	if (brdp == NULL)
		return 0;
L
Linus Torvalds 已提交
1471

A
Alan Cox 已提交
1472
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1473
	EBRDENABLE(brdp);
A
Alan Cox 已提交
1474 1475 1476 1477 1478
	rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
	head = (unsigned int) readw(&rp->head);
	tail = (unsigned int) readw(&rp->tail);
	if (tail != ((unsigned int) readw(&rp->tail)))
		tail = (unsigned int) readw(&rp->tail);
L
Linus Torvalds 已提交
1479 1480 1481
	len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
	len--;
	EBRDDISABLE(brdp);
A
Alan Cox 已提交
1482
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487

	if (tty == stli_txcooktty) {
		stli_txcookrealsize = len;
		len -= stli_txcooksize;
	}
A
Alan Cox 已提交
1488
	return len;
L
Linus Torvalds 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
}

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

/*
 *	Return the number of characters in the transmit buffer. Normally we
 *	will return the number of chars in the shared memory ring queue.
 *	We need to kludge around the case where the shared memory buffer is
 *	empty but not all characters have drained yet, for this case just
 *	return that there is 1 character in the buffer!
 */

static int stli_charsinbuffer(struct tty_struct *tty)
{
A
Alan Cox 已提交
1503
	cdkasyrq_t __iomem *rp;
1504 1505
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1506 1507
	unsigned int head, tail, len;
	unsigned long flags;
L
Linus Torvalds 已提交
1508 1509 1510 1511

	if (tty == stli_txcooktty)
		stli_flushchars(tty);
	portp = tty->driver_data;
A
Alan Cox 已提交
1512 1513
	if (portp == NULL)
		return 0;
1514
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1515
		return 0;
L
Linus Torvalds 已提交
1516
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1517 1518
	if (brdp == NULL)
		return 0;
L
Linus Torvalds 已提交
1519

A
Alan Cox 已提交
1520
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1521
	EBRDENABLE(brdp);
A
Alan Cox 已提交
1522 1523 1524 1525 1526
	rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
	head = (unsigned int) readw(&rp->head);
	tail = (unsigned int) readw(&rp->tail);
	if (tail != ((unsigned int) readw(&rp->tail)))
		tail = (unsigned int) readw(&rp->tail);
L
Linus Torvalds 已提交
1527 1528 1529 1530
	len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
	if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
		len = 1;
	EBRDDISABLE(brdp);
A
Alan Cox 已提交
1531
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
1532

A
Alan Cox 已提交
1533
	return len;
L
Linus Torvalds 已提交
1534 1535 1536 1537 1538 1539 1540 1541
}

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

/*
 *	Generate the serial struct info.
 */

1542
static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp)
L
Linus Torvalds 已提交
1543
{
A
Alan Cox 已提交
1544
	struct serial_struct sio;
1545
	struct stlibrd *brdp;
L
Linus Torvalds 已提交
1546 1547 1548 1549 1550

	memset(&sio, 0, sizeof(struct serial_struct));
	sio.type = PORT_UNKNOWN;
	sio.line = portp->portnr;
	sio.irq = 0;
A
Alan Cox 已提交
1551
	sio.flags = portp->port.flags;
L
Linus Torvalds 已提交
1552 1553 1554 1555 1556 1557 1558 1559
	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.xmit_fifo_size = 0;
	sio.hub6 = 0;

	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1560
	if (brdp != NULL)
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
		sio.port = brdp->iobase;
		
	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.
 */

A
Alan Cox 已提交
1575
static int stli_setserial(struct tty_struct *tty, struct serial_struct __user *sp)
L
Linus Torvalds 已提交
1576
{
A
Alan Cox 已提交
1577 1578
	struct serial_struct sio;
	int rc;
A
Alan Cox 已提交
1579
	struct stliport *portp = tty->driver_data;
L
Linus Torvalds 已提交
1580 1581 1582 1583 1584 1585 1586

	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) !=
A
Alan Cox 已提交
1587
		    (portp->port.flags & ~ASYNC_USR_MASK)))
A
Alan Cox 已提交
1588
			return -EPERM;
L
Linus Torvalds 已提交
1589 1590
	} 

A
Alan Cox 已提交
1591
	portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) |
L
Linus Torvalds 已提交
1592 1593 1594 1595 1596 1597
		(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;

A
Alan Cox 已提交
1598
	if ((rc = stli_setport(tty)) < 0)
A
Alan Cox 已提交
1599 1600
		return rc;
	return 0;
L
Linus Torvalds 已提交
1601 1602 1603 1604 1605 1606
}

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

static int stli_tiocmget(struct tty_struct *tty, struct file *file)
{
1607 1608
	struct stliport *portp = tty->driver_data;
	struct stlibrd *brdp;
L
Linus Torvalds 已提交
1609 1610
	int rc;

A
Alan Cox 已提交
1611 1612
	if (portp == NULL)
		return -ENODEV;
1613
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1614
		return 0;
L
Linus Torvalds 已提交
1615
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1616 1617
	if (brdp == NULL)
		return 0;
L
Linus Torvalds 已提交
1618
	if (tty->flags & (1 << TTY_IO_ERROR))
A
Alan Cox 已提交
1619
		return -EIO;
L
Linus Torvalds 已提交
1620 1621 1622

	if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
			       &portp->asig, sizeof(asysigs_t), 1)) < 0)
A
Alan Cox 已提交
1623
		return rc;
L
Linus Torvalds 已提交
1624 1625 1626 1627 1628 1629 1630

	return stli_mktiocm(portp->asig.sigvalue);
}

static int stli_tiocmset(struct tty_struct *tty, struct file *file,
			 unsigned int set, unsigned int clear)
{
1631 1632
	struct stliport *portp = tty->driver_data;
	struct stlibrd *brdp;
L
Linus Torvalds 已提交
1633 1634
	int rts = -1, dtr = -1;

A
Alan Cox 已提交
1635 1636
	if (portp == NULL)
		return -ENODEV;
1637
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1638
		return 0;
L
Linus Torvalds 已提交
1639
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1640 1641
	if (brdp == NULL)
		return 0;
L
Linus Torvalds 已提交
1642
	if (tty->flags & (1 << TTY_IO_ERROR))
A
Alan Cox 已提交
1643
		return -EIO;
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

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

	stli_mkasysigs(&portp->asig, dtr, rts);

	return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
			    sizeof(asysigs_t), 0);
}

static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
{
1662 1663
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1664
	int rc;
L
Linus Torvalds 已提交
1665 1666 1667
	void __user *argp = (void __user *)arg;

	portp = tty->driver_data;
A
Alan Cox 已提交
1668 1669
	if (portp == NULL)
		return -ENODEV;
1670
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1671
		return 0;
L
Linus Torvalds 已提交
1672
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1673 1674
	if (brdp == NULL)
		return 0;
L
Linus Torvalds 已提交
1675 1676 1677 1678

	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
 	    (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
		if (tty->flags & (1 << TTY_IO_ERROR))
A
Alan Cox 已提交
1679
			return -EIO;
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688
	}

	rc = 0;

	switch (cmd) {
	case TIOCGSERIAL:
		rc = stli_getserial(portp, argp);
		break;
	case TIOCSSERIAL:
A
Alan Cox 已提交
1689
		rc = stli_setserial(tty, argp);
L
Linus Torvalds 已提交
1690 1691 1692 1693 1694 1695
		break;
	case STL_GETPFLAG:
		rc = put_user(portp->pflag, (unsigned __user *)argp);
		break;
	case STL_SETPFLAG:
		if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
A
Alan Cox 已提交
1696
			stli_setport(tty);
L
Linus Torvalds 已提交
1697 1698
		break;
	case COM_GETPORTSTATS:
A
Alan Cox 已提交
1699
		rc = stli_getportstats(tty, portp, argp);
L
Linus Torvalds 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
		break;
	case COM_CLRPORTSTATS:
		rc = stli_clrportstats(portp, argp);
		break;
	case TIOCSERCONFIG:
	case TIOCSERGWILD:
	case TIOCSERSWILD:
	case TIOCSERGETLSR:
	case TIOCSERGSTRUCT:
	case TIOCSERGETMULTI:
	case TIOCSERSETMULTI:
	default:
		rc = -ENOIOCTLCMD;
		break;
	}

A
Alan Cox 已提交
1716
	return rc;
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725
}

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

/*
 *	This routine assumes that we have user context and can sleep.
 *	Looks like it is true for the current ttys implementation..!!
 */

A
Alan Cox 已提交
1726
static void stli_settermios(struct tty_struct *tty, struct ktermios *old)
L
Linus Torvalds 已提交
1727
{
1728 1729
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1730
	struct ktermios *tiosp;
A
Alan Cox 已提交
1731
	asyport_t aport;
L
Linus Torvalds 已提交
1732 1733

	portp = tty->driver_data;
A
Alan Cox 已提交
1734
	if (portp == NULL)
L
Linus Torvalds 已提交
1735
		return;
1736
	if (portp->brdnr >= stli_nrbrds)
L
Linus Torvalds 已提交
1737 1738
		return;
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1739
	if (brdp == NULL)
L
Linus Torvalds 已提交
1740 1741 1742 1743
		return;

	tiosp = tty->termios;

A
Alan Cox 已提交
1744
	stli_mkasyport(tty, portp, &aport, tiosp);
L
Linus Torvalds 已提交
1745 1746 1747 1748 1749 1750 1751
	stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
	stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
	stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
		sizeof(asysigs_t), 0);
	if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
		tty->hw_stopped = 0;
	if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
A
Alan Cox 已提交
1752
		wake_up_interruptible(&portp->port.open_wait);
L
Linus Torvalds 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
}

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

/*
 *	Attempt to flow control who ever is sending us data. We won't really
 *	do any flow control action here. We can't directly, and even if we
 *	wanted to we would have to send a command to the slave. The slave
 *	knows how to flow control, and will do so when its buffers reach its
 *	internal high water marks. So what we will do is set a local state
 *	bit that will stop us sending any RX data up from the poll routine
 *	(which is the place where RX data from the slave is handled).
 */

static void stli_throttle(struct tty_struct *tty)
{
1769
	struct stliport	*portp = tty->driver_data;
A
Alan Cox 已提交
1770
	if (portp == NULL)
L
Linus Torvalds 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
		return;
	set_bit(ST_RXSTOP, &portp->state);
}

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

/*
 *	Unflow control the device sending us data... That means that all
 *	we have to do is clear the RXSTOP state bit. The next poll call
 *	will then be able to pass the RX data back up.
 */

static void stli_unthrottle(struct tty_struct *tty)
{
1785
	struct stliport	*portp = tty->driver_data;
A
Alan Cox 已提交
1786
	if (portp == NULL)
L
Linus Torvalds 已提交
1787 1788 1789 1790 1791 1792 1793
		return;
	clear_bit(ST_RXSTOP, &portp->state);
}

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

/*
A
Alan Cox 已提交
1794
 *	Stop the transmitter.
L
Linus Torvalds 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803
 */

static void stli_stop(struct tty_struct *tty)
{
}

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

/*
A
Alan Cox 已提交
1804
 *	Start the transmitter again.
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
 */

static void stli_start(struct tty_struct *tty)
{
}

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

/*
 *	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. This is rather tricky really. We want
 *	to close the port as well.
 */

static void stli_hangup(struct tty_struct *tty)
{
1822 1823
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1824
	unsigned long flags;
L
Linus Torvalds 已提交
1825 1826

	portp = tty->driver_data;
A
Alan Cox 已提交
1827
	if (portp == NULL)
L
Linus Torvalds 已提交
1828
		return;
1829
	if (portp->brdnr >= stli_nrbrds)
L
Linus Torvalds 已提交
1830 1831
		return;
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1832
	if (brdp == NULL)
L
Linus Torvalds 已提交
1833 1834
		return;

A
Alan Cox 已提交
1835
	portp->port.flags &= ~ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
1836

A
Alan Cox 已提交
1837
	if (!test_bit(ST_CLOSING, &portp->state))
L
Linus Torvalds 已提交
1838
		stli_rawclose(brdp, portp, 0, 0);
A
Alan Cox 已提交
1839 1840

	spin_lock_irqsave(&stli_lock, flags);
L
Linus Torvalds 已提交
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
	if (tty->termios->c_cflag & HUPCL) {
		stli_mkasysigs(&portp->asig, 0, 0);
		if (test_bit(ST_CMDING, &portp->state)) {
			set_bit(ST_DOSIGS, &portp->state);
			set_bit(ST_DOFLUSHTX, &portp->state);
			set_bit(ST_DOFLUSHRX, &portp->state);
		} else {
			stli_sendcmd(brdp, portp, A_SETSIGNALSF,
				&portp->asig, sizeof(asysigs_t), 0);
		}
	}

	clear_bit(ST_TXBUSY, &portp->state);
	clear_bit(ST_RXSTOP, &portp->state);
	set_bit(TTY_IO_ERROR, &tty->flags);
A
Alan Cox 已提交
1856
	tty_port_tty_set(&portp->port, NULL);
A
Alan Cox 已提交
1857
	portp->port.flags &= ~ASYNC_NORMAL_ACTIVE;
W
Wang Chen 已提交
1858
	portp->port.count = 0;
A
Alan Cox 已提交
1859 1860
	spin_unlock_irqrestore(&stli_lock, flags);

A
Alan Cox 已提交
1861
	wake_up_interruptible(&portp->port.open_wait);
L
Linus Torvalds 已提交
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
}

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

/*
 *	Flush characters from the lower buffer. We may not have user context
 *	so we cannot sleep waiting for it to complete. Also we need to check
 *	if there is chars for this port in the TX cook buffer, and flush them
 *	as well.
 */

static void stli_flushbuffer(struct tty_struct *tty)
{
1875 1876
	struct stliport *portp;
	struct stlibrd *brdp;
A
Alan Cox 已提交
1877
	unsigned long ftype, flags;
L
Linus Torvalds 已提交
1878 1879

	portp = tty->driver_data;
A
Alan Cox 已提交
1880
	if (portp == NULL)
L
Linus Torvalds 已提交
1881
		return;
1882
	if (portp->brdnr >= stli_nrbrds)
L
Linus Torvalds 已提交
1883 1884
		return;
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1885
	if (brdp == NULL)
L
Linus Torvalds 已提交
1886 1887
		return;

A
Alan Cox 已提交
1888
	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
1889
	if (tty == stli_txcooktty) {
A
Alan Cox 已提交
1890
		stli_txcooktty = NULL;
L
Linus Torvalds 已提交
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
		stli_txcooksize = 0;
		stli_txcookrealsize = 0;
	}
	if (test_bit(ST_CMDING, &portp->state)) {
		set_bit(ST_DOFLUSHTX, &portp->state);
	} else {
		ftype = FLUSHTX;
		if (test_bit(ST_DOFLUSHRX, &portp->state)) {
			ftype |= FLUSHRX;
			clear_bit(ST_DOFLUSHRX, &portp->state);
		}
A
Alan Cox 已提交
1902
		__stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
L
Linus Torvalds 已提交
1903
	}
A
Alan Cox 已提交
1904 1905
	spin_unlock_irqrestore(&brd_lock, flags);
	tty_wakeup(tty);
L
Linus Torvalds 已提交
1906 1907 1908 1909
}

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

A
Alan Cox 已提交
1910
static int stli_breakctl(struct tty_struct *tty, int state)
L
Linus Torvalds 已提交
1911
{
1912 1913
	struct stlibrd	*brdp;
	struct stliport	*portp;
L
Linus Torvalds 已提交
1914 1915 1916
	long		arg;

	portp = tty->driver_data;
A
Alan Cox 已提交
1917
	if (portp == NULL)
A
Alan Cox 已提交
1918
		return -EINVAL;
1919
	if (portp->brdnr >= stli_nrbrds)
A
Alan Cox 已提交
1920
		return -EINVAL;
L
Linus Torvalds 已提交
1921
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1922
	if (brdp == NULL)
A
Alan Cox 已提交
1923
		return -EINVAL;
L
Linus Torvalds 已提交
1924 1925 1926

	arg = (state == -1) ? BREAKON : BREAKOFF;
	stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
A
Alan Cox 已提交
1927
	return 0;
L
Linus Torvalds 已提交
1928 1929 1930 1931 1932 1933
}

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

static void stli_waituntilsent(struct tty_struct *tty, int timeout)
{
1934
	struct stliport *portp;
A
Alan Cox 已提交
1935
	unsigned long tend;
L
Linus Torvalds 已提交
1936 1937

	portp = tty->driver_data;
A
Alan Cox 已提交
1938
	if (portp == NULL)
L
Linus Torvalds 已提交
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
		return;

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

	while (test_bit(ST_TXBUSY, &portp->state)) {
		if (signal_pending(current))
			break;
		msleep_interruptible(20);
		if (time_after_eq(jiffies, tend))
			break;
	}
}

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

static void stli_sendxchar(struct tty_struct *tty, char ch)
{
1958 1959
	struct stlibrd	*brdp;
	struct stliport	*portp;
L
Linus Torvalds 已提交
1960 1961 1962
	asyctrl_t	actrl;

	portp = tty->driver_data;
A
Alan Cox 已提交
1963
	if (portp == NULL)
L
Linus Torvalds 已提交
1964
		return;
1965
	if (portp->brdnr >= stli_nrbrds)
L
Linus Torvalds 已提交
1966 1967
		return;
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
1968
	if (brdp == NULL)
L
Linus Torvalds 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
		return;

	memset(&actrl, 0, sizeof(asyctrl_t));
	if (ch == STOP_CHAR(tty)) {
		actrl.rxctrl = CT_STOPFLOW;
	} else if (ch == START_CHAR(tty)) {
		actrl.rxctrl = CT_STARTFLOW;
	} else {
		actrl.txctrl = CT_SENDCHR;
		actrl.tximdch = ch;
	}
	stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
}

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

#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).
 */

1993
static int stli_portinfo(struct stlibrd *brdp, struct stliport *portp, int portnr, char *pos)
L
Linus Torvalds 已提交
1994
{
A
Alan Cox 已提交
1995 1996
	char *sp, *uart;
	int rc, cnt;
L
Linus Torvalds 已提交
1997

A
Alan Cox 已提交
1998
	rc = stli_portcmdstats(NULL, portp);
L
Linus Torvalds 已提交
1999 2000 2001 2002

	uart = "UNKNOWN";
	if (brdp->state & BST_STARTED) {
		switch (stli_comstats.hwid) {
A
Alan Cox 已提交
2003 2004 2005
		case 0:	uart = "2681"; break;
		case 1:	uart = "SC26198"; break;
		default:uart = "CD1400"; break;
L
Linus Torvalds 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
		}
	}

	sp = pos;
	sp += sprintf(sp, "%d: uart:%s ", portnr, uart);

	if ((brdp->state & BST_STARTED) && (rc >= 0)) {
		sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
			(int) stli_comstats.rxtotal);

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

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

	for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
		*sp++ = ' ';
	if (cnt >= MAXLINE)
		pos[(MAXLINE - 2)] = '+';
	pos[(MAXLINE - 1)] = '\n';

	return(MAXLINE);
}

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

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

static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
{
2056 2057
	struct stlibrd *brdp;
	struct stliport *portp;
2058
	unsigned int brdnr, portnr, totalport;
A
Alan Cox 已提交
2059 2060
	int curoff, maxoff;
	char *pos;
L
Linus Torvalds 已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080

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

	if (off == 0) {
		pos += sprintf(pos, "%s: version %s", stli_drvtitle,
			stli_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.
 */
	for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
		brdp = stli_brds[brdnr];
A
Alan Cox 已提交
2081
		if (brdp == NULL)
L
Linus Torvalds 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
			continue;
		if (brdp->state == 0)
			continue;

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

		totalport = brdnr * STL_MAXPORTS;
		for (portnr = 0; (portnr < brdp->nrports); portnr++,
		    totalport++) {
			portp = brdp->ports[portnr];
A
Alan Cox 已提交
2096
			if (portp == NULL)
L
Linus Torvalds 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
				continue;
			if (off >= (curoff += MAXLINE))
				continue;
			if ((pos - page + MAXLINE) > count)
				goto stli_readdone;
			pos += stli_portinfo(brdp, portp, totalport, pos);
		}
	}

	*eof = 1;

stli_readdone:
	*start = page;
	return(pos - page);
}

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

/*
 *	Generic send command routine. This will send a message to the slave,
 *	of the specified type with the specified argument. Must be very
 *	careful of data that will be copied out from shared memory -
 *	containing command results. The command completion is all done from
 *	a poll routine that does not have user context. Therefore you cannot
 *	copy back directly into user space, or to the kernel stack of a
 *	process. This routine does not sleep, so can be called from anywhere.
A
Alan Cox 已提交
2123 2124 2125
 *
 *	The caller must hold the brd_lock (see also stli_sendcmd the usual
 *	entry point)
L
Linus Torvalds 已提交
2126 2127
 */

2128
static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
L
Linus Torvalds 已提交
2129
{
A
Alan Cox 已提交
2130 2131 2132
	cdkhdr_t __iomem *hdrp;
	cdkctrl_t __iomem *cp;
	unsigned char __iomem *bits;
L
Linus Torvalds 已提交
2133 2134 2135 2136 2137 2138 2139 2140

	if (test_bit(ST_CMDING, &portp->state)) {
		printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
				(int) cmd);
		return;
	}

	EBRDENABLE(brdp);
A
Alan Cox 已提交
2141
	cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
L
Linus Torvalds 已提交
2142
	if (size > 0) {
A
Alan Cox 已提交
2143
		memcpy_toio((void __iomem *) &(cp->args[0]), arg, size);
L
Linus Torvalds 已提交
2144 2145 2146 2147 2148
		if (copyback) {
			portp->argp = arg;
			portp->argsize = size;
		}
	}
A
Alan Cox 已提交
2149 2150 2151 2152
	writel(0, &cp->status);
	writel(cmd, &cp->cmd);
	hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
	bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
L
Linus Torvalds 已提交
2153
		portp->portidx;
A
Alan Cox 已提交
2154
	writeb(readb(bits) | portp->portbit, bits);
L
Linus Torvalds 已提交
2155 2156
	set_bit(ST_CMDING, &portp->state);
	EBRDDISABLE(brdp);
A
Alan Cox 已提交
2157 2158
}

2159
static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
A
Alan Cox 已提交
2160 2161 2162 2163 2164 2165
{
	unsigned long		flags;

	spin_lock_irqsave(&brd_lock, flags);
	__stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
}

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

/*
 *	Read data from shared memory. This assumes that the shared memory
 *	is enabled and that interrupts are off. Basically we just empty out
 *	the shared memory buffer into the tty buffer. Must be careful to
 *	handle the case where we fill up the tty buffer, but still have
 *	more chars to unload.
 */

2178
static void stli_read(struct stlibrd *brdp, struct stliport *portp)
L
Linus Torvalds 已提交
2179
{
A
Alan Cox 已提交
2180 2181
	cdkasyrq_t __iomem *rp;
	char __iomem *shbuf;
L
Linus Torvalds 已提交
2182
	struct tty_struct	*tty;
A
Alan Cox 已提交
2183 2184
	unsigned int head, tail, size;
	unsigned int len, stlen;
L
Linus Torvalds 已提交
2185 2186 2187

	if (test_bit(ST_RXSTOP, &portp->state))
		return;
A
Alan Cox 已提交
2188
	tty = tty_port_tty_get(&portp->port);
A
Alan Cox 已提交
2189
	if (tty == NULL)
L
Linus Torvalds 已提交
2190 2191
		return;

A
Alan Cox 已提交
2192 2193 2194 2195 2196
	rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
	head = (unsigned int) readw(&rp->head);
	if (head != ((unsigned int) readw(&rp->head)))
		head = (unsigned int) readw(&rp->head);
	tail = (unsigned int) readw(&rp->tail);
L
Linus Torvalds 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205
	size = portp->rxsize;
	if (head >= tail) {
		len = head - tail;
		stlen = len;
	} else {
		len = size - (tail - head);
		stlen = size - tail;
	}

A
Alan Cox 已提交
2206
	len = tty_buffer_request_room(tty, len);
A
Alan Cox 已提交
2207 2208

	shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset);
L
Linus Torvalds 已提交
2209 2210

	while (len > 0) {
A
Alan Cox 已提交
2211 2212
		unsigned char *cptr;

2213
		stlen = min(len, stlen);
A
Alan Cox 已提交
2214 2215
		tty_prepare_flip_string(tty, &cptr, stlen);
		memcpy_fromio(cptr, shbuf + tail, stlen);
L
Linus Torvalds 已提交
2216 2217 2218 2219 2220 2221 2222
		len -= stlen;
		tail += stlen;
		if (tail >= size) {
			tail = 0;
			stlen = head;
		}
	}
A
Alan Cox 已提交
2223 2224
	rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
	writew(tail, &rp->tail);
L
Linus Torvalds 已提交
2225 2226 2227 2228 2229

	if (head != tail)
		set_bit(ST_RXING, &portp->state);

	tty_schedule_flip(tty);
A
Alan Cox 已提交
2230
	tty_kref_put(tty);
L
Linus Torvalds 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
}

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

/*
 *	Set up and carry out any delayed commands. There is only a small set
 *	of slave commands that can be done "off-level". So it is not too
 *	difficult to deal with them here.
 */

2241
static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp)
L
Linus Torvalds 已提交
2242
{
A
Alan Cox 已提交
2243
	int cmd;
L
Linus Torvalds 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257

	if (test_bit(ST_DOSIGS, &portp->state)) {
		if (test_bit(ST_DOFLUSHTX, &portp->state) &&
		    test_bit(ST_DOFLUSHRX, &portp->state))
			cmd = A_SETSIGNALSF;
		else if (test_bit(ST_DOFLUSHTX, &portp->state))
			cmd = A_SETSIGNALSFTX;
		else if (test_bit(ST_DOFLUSHRX, &portp->state))
			cmd = A_SETSIGNALSFRX;
		else
			cmd = A_SETSIGNALS;
		clear_bit(ST_DOFLUSHTX, &portp->state);
		clear_bit(ST_DOFLUSHRX, &portp->state);
		clear_bit(ST_DOSIGS, &portp->state);
A
Alan Cox 已提交
2258
		memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &portp->asig,
L
Linus Torvalds 已提交
2259
			sizeof(asysigs_t));
A
Alan Cox 已提交
2260 2261
		writel(0, &cp->status);
		writel(cmd, &cp->cmd);
L
Linus Torvalds 已提交
2262 2263 2264 2265 2266 2267 2268
		set_bit(ST_CMDING, &portp->state);
	} else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
	    test_bit(ST_DOFLUSHRX, &portp->state)) {
		cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
		cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
		clear_bit(ST_DOFLUSHTX, &portp->state);
		clear_bit(ST_DOFLUSHRX, &portp->state);
A
Alan Cox 已提交
2269 2270 2271
		memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &cmd, sizeof(int));
		writel(0, &cp->status);
		writel(A_FLUSH, &cp->cmd);
L
Linus Torvalds 已提交
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
		set_bit(ST_CMDING, &portp->state);
	}
}

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

/*
 *	Host command service checking. This handles commands or messages
 *	coming from the slave to the host. Must have board shared memory
 *	enabled and interrupts off when called. Notice that by servicing the
 *	read data last we don't need to change the shared memory pointer
 *	during processing (which is a slow IO operation).
 *	Return value indicates if this port is still awaiting actions from
 *	the slave (like open, command, or even TX data being sent). If 0
 *	then port is still busy, otherwise no longer busy.
 */

2289
static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp)
L
Linus Torvalds 已提交
2290
{
A
Alan Cox 已提交
2291 2292 2293 2294 2295 2296 2297 2298
	cdkasy_t __iomem *ap;
	cdkctrl_t __iomem *cp;
	struct tty_struct *tty;
	asynotify_t nt;
	unsigned long oldsigs;
	int rc, donerx;

	ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
L
Linus Torvalds 已提交
2299 2300 2301 2302 2303 2304
	cp = &ap->ctrl;

/*
 *	Check if we are waiting for an open completion message.
 */
	if (test_bit(ST_OPENING, &portp->state)) {
A
Alan Cox 已提交
2305 2306
		rc = readl(&cp->openarg);
		if (readb(&cp->open) == 0 && rc != 0) {
L
Linus Torvalds 已提交
2307 2308
			if (rc > 0)
				rc--;
A
Alan Cox 已提交
2309
			writel(0, &cp->openarg);
L
Linus Torvalds 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
			portp->rc = rc;
			clear_bit(ST_OPENING, &portp->state);
			wake_up_interruptible(&portp->raw_wait);
		}
	}

/*
 *	Check if we are waiting for a close completion message.
 */
	if (test_bit(ST_CLOSING, &portp->state)) {
A
Alan Cox 已提交
2320 2321
		rc = (int) readl(&cp->closearg);
		if (readb(&cp->close) == 0 && rc != 0) {
L
Linus Torvalds 已提交
2322 2323
			if (rc > 0)
				rc--;
A
Alan Cox 已提交
2324
			writel(0, &cp->closearg);
L
Linus Torvalds 已提交
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
			portp->rc = rc;
			clear_bit(ST_CLOSING, &portp->state);
			wake_up_interruptible(&portp->raw_wait);
		}
	}

/*
 *	Check if we are waiting for a command completion message. We may
 *	need to copy out the command results associated with this command.
 */
	if (test_bit(ST_CMDING, &portp->state)) {
A
Alan Cox 已提交
2336 2337
		rc = readl(&cp->status);
		if (readl(&cp->cmd) == 0 && rc != 0) {
L
Linus Torvalds 已提交
2338 2339
			if (rc > 0)
				rc--;
A
Alan Cox 已提交
2340 2341
			if (portp->argp != NULL) {
				memcpy_fromio(portp->argp, (void __iomem *) &(cp->args[0]),
L
Linus Torvalds 已提交
2342
					portp->argsize);
A
Alan Cox 已提交
2343
				portp->argp = NULL;
L
Linus Torvalds 已提交
2344
			}
A
Alan Cox 已提交
2345
			writel(0, &cp->status);
L
Linus Torvalds 已提交
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
			portp->rc = rc;
			clear_bit(ST_CMDING, &portp->state);
			stli_dodelaycmd(portp, cp);
			wake_up_interruptible(&portp->raw_wait);
		}
	}

/*
 *	Check for any notification messages ready. This includes lots of
 *	different types of events - RX chars ready, RX break received,
 *	TX data low or empty in the slave, modem signals changed state.
 */
	donerx = 0;

	if (ap->notify) {
		nt = ap->changed;
		ap->notify = 0;
A
Alan Cox 已提交
2363
		tty = tty_port_tty_get(&portp->port);
L
Linus Torvalds 已提交
2364 2365 2366 2367 2368 2369 2370

		if (nt.signal & SG_DCD) {
			oldsigs = portp->sigs;
			portp->sigs = stli_mktiocm(nt.sigvalue);
			clear_bit(ST_GETSIGS, &portp->state);
			if ((portp->sigs & TIOCM_CD) &&
			    ((oldsigs & TIOCM_CD) == 0))
A
Alan Cox 已提交
2371
				wake_up_interruptible(&portp->port.open_wait);
L
Linus Torvalds 已提交
2372 2373
			if ((oldsigs & TIOCM_CD) &&
			    ((portp->sigs & TIOCM_CD) == 0)) {
A
Alan Cox 已提交
2374
				if (portp->port.flags & ASYNC_CHECK_CD) {
L
Linus Torvalds 已提交
2375
					if (tty)
2376
						tty_hangup(tty);
L
Linus Torvalds 已提交
2377 2378 2379 2380 2381 2382 2383
				}
			}
		}

		if (nt.data & DT_TXEMPTY)
			clear_bit(ST_TXBUSY, &portp->state);
		if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
A
Alan Cox 已提交
2384 2385 2386
			if (tty != NULL) {
				tty_wakeup(tty);
				EBRDENABLE(brdp);
L
Linus Torvalds 已提交
2387 2388 2389 2390
			}
		}

		if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
A
Alan Cox 已提交
2391
			if (tty != NULL) {
A
Alan Cox 已提交
2392
				tty_insert_flip_char(tty, 0, TTY_BREAK);
A
Alan Cox 已提交
2393
				if (portp->port.flags & ASYNC_SAK) {
A
Alan Cox 已提交
2394 2395
					do_SAK(tty);
					EBRDENABLE(brdp);
L
Linus Torvalds 已提交
2396
				}
A
Alan Cox 已提交
2397
				tty_schedule_flip(tty);
L
Linus Torvalds 已提交
2398 2399
			}
		}
A
Alan Cox 已提交
2400
		tty_kref_put(tty);
L
Linus Torvalds 已提交
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435

		if (nt.data & DT_RXBUSY) {
			donerx++;
			stli_read(brdp, portp);
		}
	}

/*
 *	It might seem odd that we are checking for more RX chars here.
 *	But, we need to handle the case where the tty buffer was previously
 *	filled, but we had more characters to pass up. The slave will not
 *	send any more RX notify messages until the RX buffer has been emptied.
 *	But it will leave the service bits on (since the buffer is not empty).
 *	So from here we can try to process more RX chars.
 */
	if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
		clear_bit(ST_RXING, &portp->state);
		stli_read(brdp, portp);
	}

	return((test_bit(ST_OPENING, &portp->state) ||
		test_bit(ST_CLOSING, &portp->state) ||
		test_bit(ST_CMDING, &portp->state) ||
		test_bit(ST_TXBUSY, &portp->state) ||
		test_bit(ST_RXING, &portp->state)) ? 0 : 1);
}

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

/*
 *	Service all ports on a particular board. Assumes that the boards
 *	shared memory is enabled, and that the page pointer is pointed
 *	at the cdk header structure.
 */

2436
static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp)
L
Linus Torvalds 已提交
2437
{
2438
	struct stliport *portp;
A
Alan Cox 已提交
2439 2440 2441 2442 2443
	unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
	unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
	unsigned char __iomem *slavep;
	int bitpos, bitat, bitsize;
	int channr, nrdevs, slavebitchange;
L
Linus Torvalds 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454

	bitsize = brdp->bitsize;
	nrdevs = brdp->nrdevs;

/*
 *	Check if slave wants any service. Basically we try to do as
 *	little work as possible here. There are 2 levels of service
 *	bits. So if there is nothing to do we bail early. We check
 *	8 service bits at a time in the inner loop, so we can bypass
 *	the lot if none of them want service.
 */
A
Alan Cox 已提交
2455
	memcpy_fromio(&hostbits[0], (((unsigned char __iomem *) hdrp) + brdp->hostoffset),
L
Linus Torvalds 已提交
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
		bitsize);

	memset(&slavebits[0], 0, bitsize);
	slavebitchange = 0;

	for (bitpos = 0; (bitpos < bitsize); bitpos++) {
		if (hostbits[bitpos] == 0)
			continue;
		channr = bitpos * 8;
		for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
			if (hostbits[bitpos] & bitat) {
				portp = brdp->ports[(channr - 1)];
				if (stli_hostcmd(brdp, portp)) {
					slavebitchange++;
					slavebits[bitpos] |= bitat;
				}
			}
		}
	}

/*
 *	If any of the ports are no longer busy then update them in the
 *	slave request bits. We need to do this after, since a host port
 *	service may initiate more slave requests.
 */
	if (slavebitchange) {
A
Alan Cox 已提交
2482 2483
		hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
		slavep = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset;
L
Linus Torvalds 已提交
2484
		for (bitpos = 0; (bitpos < bitsize); bitpos++) {
A
Alan Cox 已提交
2485 2486
			if (readb(slavebits + bitpos))
				writeb(readb(slavep + bitpos) & ~slavebits[bitpos], slavebits + bitpos);
L
Linus Torvalds 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
		}
	}
}

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

/*
 *	Driver poll routine. This routine polls the boards in use and passes
 *	messages back up to host when necessary. This is actually very
 *	CPU efficient, since we will always have the kernel poll clock, it
 *	adds only a few cycles when idle (since board service can be
 *	determined very easily), but when loaded generates no interrupts
 *	(with their expensive associated context change).
 */

static void stli_poll(unsigned long arg)
{
A
Alan Cox 已提交
2504
	cdkhdr_t __iomem *hdrp;
2505
	struct stlibrd *brdp;
2506
	unsigned int brdnr;
L
Linus Torvalds 已提交
2507

2508
	mod_timer(&stli_timerlist, STLI_TIMEOUT);
L
Linus Torvalds 已提交
2509 2510 2511 2512 2513 2514

/*
 *	Check each board and do any servicing required.
 */
	for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
		brdp = stli_brds[brdnr];
A
Alan Cox 已提交
2515
		if (brdp == NULL)
L
Linus Torvalds 已提交
2516 2517 2518 2519
			continue;
		if ((brdp->state & BST_STARTED) == 0)
			continue;

A
Alan Cox 已提交
2520
		spin_lock(&brd_lock);
L
Linus Torvalds 已提交
2521
		EBRDENABLE(brdp);
A
Alan Cox 已提交
2522 2523
		hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
		if (readb(&hdrp->hostreq))
L
Linus Torvalds 已提交
2524 2525
			stli_brdpoll(brdp, hdrp);
		EBRDDISABLE(brdp);
A
Alan Cox 已提交
2526
		spin_unlock(&brd_lock);
L
Linus Torvalds 已提交
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
	}
}

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

/*
 *	Translate the termios settings into the port setting structure of
 *	the slave.
 */

A
Alan Cox 已提交
2537 2538
static void stli_mkasyport(struct tty_struct *tty, struct stliport *portp,
				asyport_t *pp, struct ktermios *tiosp)
L
Linus Torvalds 已提交
2539 2540 2541 2542 2543 2544
{
	memset(pp, 0, sizeof(asyport_t));

/*
 *	Start of by setting the baud, char size, parity and stop bit info.
 */
A
Alan Cox 已提交
2545
	pp->baudout = tty_get_baud_rate(tty);
L
Linus Torvalds 已提交
2546
	if ((tiosp->c_cflag & CBAUD) == B38400) {
A
Alan Cox 已提交
2547
		if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
L
Linus Torvalds 已提交
2548
			pp->baudout = 57600;
A
Alan Cox 已提交
2549
		else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
L
Linus Torvalds 已提交
2550
			pp->baudout = 115200;
A
Alan Cox 已提交
2551
		else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
L
Linus Torvalds 已提交
2552
			pp->baudout = 230400;
A
Alan Cox 已提交
2553
		else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
L
Linus Torvalds 已提交
2554
			pp->baudout = 460800;
A
Alan Cox 已提交
2555
		else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
L
Linus Torvalds 已提交
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
			pp->baudout = (portp->baud_base / portp->custom_divisor);
	}
	if (pp->baudout > STL_MAXBAUD)
		pp->baudout = STL_MAXBAUD;
	pp->baudin = pp->baudout;

	switch (tiosp->c_cflag & CSIZE) {
	case CS5:
		pp->csize = 5;
		break;
	case CS6:
		pp->csize = 6;
		break;
	case CS7:
		pp->csize = 7;
		break;
	default:
		pp->csize = 8;
		break;
	}

	if (tiosp->c_cflag & CSTOPB)
		pp->stopbs = PT_STOP2;
	else
		pp->stopbs = PT_STOP1;

	if (tiosp->c_cflag & PARENB) {
		if (tiosp->c_cflag & PARODD)
			pp->parity = PT_ODDPARITY;
		else
			pp->parity = PT_EVENPARITY;
	} else {
		pp->parity = PT_NOPARITY;
	}

/*
 *	Set up any flow control options enabled.
 */
	if (tiosp->c_iflag & IXON) {
		pp->flow |= F_IXON;
		if (tiosp->c_iflag & IXANY)
			pp->flow |= F_IXANY;
	}
	if (tiosp->c_cflag & CRTSCTS)
		pp->flow |= (F_RTSFLOW | F_CTSFLOW);

	pp->startin = tiosp->c_cc[VSTART];
	pp->stopin = tiosp->c_cc[VSTOP];
	pp->startout = tiosp->c_cc[VSTART];
	pp->stopout = tiosp->c_cc[VSTOP];

/*
 *	Set up the RX char marking mask with those RX error types we must
 *	catch. We can get the slave to help us out a little here, it will
 *	ignore parity errors and breaks for us, and mark parity errors in
 *	the data stream.
 */
	if (tiosp->c_iflag & IGNPAR)
		pp->iflag |= FI_IGNRXERRS;
	if (tiosp->c_iflag & IGNBRK)
		pp->iflag |= FI_IGNBREAK;

	portp->rxmarkmsk = 0;
	if (tiosp->c_iflag & (INPCK | PARMRK))
		pp->iflag |= FI_1MARKRXERRS;
	if (tiosp->c_iflag & BRKINT)
		portp->rxmarkmsk |= BRKINT;

/*
 *	Set up clocal processing as required.
 */
	if (tiosp->c_cflag & CLOCAL)
A
Alan Cox 已提交
2628
		portp->port.flags &= ~ASYNC_CHECK_CD;
L
Linus Torvalds 已提交
2629
	else
A
Alan Cox 已提交
2630
		portp->port.flags |= ASYNC_CHECK_CD;
L
Linus Torvalds 已提交
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669

/*
 *	Transfer any persistent flags into the asyport structure.
 */
	pp->pflag = (portp->pflag & 0xffff);
	pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
	pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
	pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
}

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

/*
 *	Construct a slave signals structure for setting the DTR and RTS
 *	signals as specified.
 */

static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
{
	memset(sp, 0, sizeof(asysigs_t));
	if (dtr >= 0) {
		sp->signal |= SG_DTR;
		sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
	}
	if (rts >= 0) {
		sp->signal |= SG_RTS;
		sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
	}
}

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

/*
 *	Convert the signals returned from the slave into a local TIOCM type
 *	signals value. We keep them locally in TIOCM format.
 */

static long stli_mktiocm(unsigned long sigvalue)
{
A
Alan Cox 已提交
2670
	long	tiocm = 0;
L
Linus Torvalds 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
	tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
	tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
	tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
	tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
	tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
	tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
	return(tiocm);
}

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

/*
 *	All panels and ports actually attached have been worked out. All
 *	we need to do here is set up the appropriate per port data structures.
 */

2687
static int stli_initports(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2688
{
2689
	struct stliport	*portp;
2690
	unsigned int i, panelnr, panelport;
L
Linus Torvalds 已提交
2691 2692

	for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
2693
		portp = kzalloc(sizeof(struct stliport), GFP_KERNEL);
2694
		if (!portp) {
L
Linus Torvalds 已提交
2695 2696 2697
			printk("STALLION: failed to allocate port structure\n");
			continue;
		}
A
Alan Cox 已提交
2698
		tty_port_init(&portp->port);
L
Linus Torvalds 已提交
2699 2700 2701 2702 2703 2704 2705
		portp->magic = STLI_PORTMAGIC;
		portp->portnr = i;
		portp->brdnr = brdp->brdnr;
		portp->panelnr = panelnr;
		portp->baud_base = STL_BAUDBASE;
		portp->close_delay = STL_CLOSEDELAY;
		portp->closing_wait = 30 * HZ;
A
Alan Cox 已提交
2706 2707
		init_waitqueue_head(&portp->port.open_wait);
		init_waitqueue_head(&portp->port.close_wait);
L
Linus Torvalds 已提交
2708 2709 2710 2711 2712 2713 2714 2715 2716
		init_waitqueue_head(&portp->raw_wait);
		panelport++;
		if (panelport >= brdp->panels[panelnr]) {
			panelport = 0;
			panelnr++;
		}
		brdp->ports[i] = portp;
	}

A
Alan Cox 已提交
2717
	return 0;
L
Linus Torvalds 已提交
2718 2719 2720 2721 2722 2723 2724 2725
}

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

/*
 *	All the following routines are board specific hardware operations.
 */

2726
static void stli_ecpinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
{
	unsigned long	memconf;

	outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
	udelay(10);
	outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
	udelay(100);

	memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
	outb(memconf, (brdp->iobase + ECP_ATMEMAR));
}

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

2741
static void stli_ecpenable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2742 2743 2744 2745 2746 2747
{	
	outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
}

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

2748
static void stli_ecpdisable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2749 2750 2751 2752 2753 2754
{	
	outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
}

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

2755
static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
2756
{	
2757
	void __iomem *ptr;
A
Alan Cox 已提交
2758
	unsigned char val;
L
Linus Torvalds 已提交
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775

	if (offset > brdp->memsize) {
		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
				"range at line=%d(%d), brd=%d\n",
			(int) offset, line, __LINE__, brdp->brdnr);
		ptr = NULL;
		val = 0;
	} else {
		ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
		val = (unsigned char) (offset / ECP_ATPAGESIZE);
	}
	outb(val, (brdp->iobase + ECP_ATMEMPR));
	return(ptr);
}

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

2776
static void stli_ecpreset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2777 2778 2779 2780 2781 2782 2783 2784 2785
{	
	outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
	udelay(10);
	outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
	udelay(500);
}

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

2786
static void stli_ecpintr(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
{	
	outb(0x1, brdp->iobase);
}

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

/*
 *	The following set of functions act on ECP EISA boards.
 */

2797
static void stli_ecpeiinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
{
	unsigned long	memconf;

	outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
	outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
	udelay(10);
	outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
	udelay(500);

	memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
	outb(memconf, (brdp->iobase + ECP_EIMEMARL));
	memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
	outb(memconf, (brdp->iobase + ECP_EIMEMARH));
}

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

2815
static void stli_ecpeienable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2816 2817 2818 2819 2820 2821
{	
	outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
}

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

2822
static void stli_ecpeidisable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2823 2824 2825 2826 2827 2828
{	
	outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
}

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

2829
static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
2830
{	
2831
	void __iomem *ptr;
L
Linus Torvalds 已提交
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
	unsigned char	val;

	if (offset > brdp->memsize) {
		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
				"range at line=%d(%d), brd=%d\n",
			(int) offset, line, __LINE__, brdp->brdnr);
		ptr = NULL;
		val = 0;
	} else {
		ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
		if (offset < ECP_EIPAGESIZE)
			val = ECP_EIENABLE;
		else
			val = ECP_EIENABLE | 0x40;
	}
	outb(val, (brdp->iobase + ECP_EICONFR));
	return(ptr);
}

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

2853
static void stli_ecpeireset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
{	
	outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
	udelay(10);
	outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
	udelay(500);
}

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

/*
 *	The following set of functions act on ECP MCA boards.
 */

2867
static void stli_ecpmcenable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2868 2869 2870 2871 2872 2873
{	
	outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
}

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

2874
static void stli_ecpmcdisable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2875 2876 2877 2878 2879 2880
{	
	outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
}

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

2881
static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
2882
{	
2883
	void __iomem *ptr;
A
Alan Cox 已提交
2884
	unsigned char val;
L
Linus Torvalds 已提交
2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901

	if (offset > brdp->memsize) {
		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
				"range at line=%d(%d), brd=%d\n",
			(int) offset, line, __LINE__, brdp->brdnr);
		ptr = NULL;
		val = 0;
	} else {
		ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
		val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
	}
	outb(val, (brdp->iobase + ECP_MCCONFR));
	return(ptr);
}

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

2902
static void stli_ecpmcreset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
{	
	outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
	udelay(10);
	outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
	udelay(500);
}

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

/*
 *	The following set of functions act on ECP PCI boards.
 */

2916
static void stli_ecppciinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2917 2918 2919 2920 2921 2922 2923 2924 2925
{
	outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
	udelay(10);
	outb(0, (brdp->iobase + ECP_PCICONFR));
	udelay(500);
}

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

2926
static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
2927
{	
2928
	void __iomem *ptr;
L
Linus Torvalds 已提交
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
	unsigned char	val;

	if (offset > brdp->memsize) {
		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
				"range at line=%d(%d), board=%d\n",
				(int) offset, line, __LINE__, brdp->brdnr);
		ptr = NULL;
		val = 0;
	} else {
		ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
		val = (offset / ECP_PCIPAGESIZE) << 1;
	}
	outb(val, (brdp->iobase + ECP_PCICONFR));
	return(ptr);
}

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

2947
static void stli_ecppcireset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
{	
	outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
	udelay(10);
	outb(0, (brdp->iobase + ECP_PCICONFR));
	udelay(500);
}

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

/*
 *	The following routines act on ONboards.
 */

2961
static void stli_onbinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977
{
	unsigned long	memconf;

	outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
	udelay(10);
	outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
	mdelay(1000);

	memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
	outb(memconf, (brdp->iobase + ONB_ATMEMAR));
	outb(0x1, brdp->iobase);
	mdelay(1);
}

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

2978
static void stli_onbenable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2979 2980 2981 2982 2983 2984
{	
	outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
}

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

2985
static void stli_onbdisable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
2986 2987 2988 2989 2990 2991
{	
	outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
}

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

2992
static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
2993
{	
2994
	void __iomem *ptr;
L
Linus Torvalds 已提交
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008

	if (offset > brdp->memsize) {
		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
				"range at line=%d(%d), brd=%d\n",
				(int) offset, line, __LINE__, brdp->brdnr);
		ptr = NULL;
	} else {
		ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
	}
	return(ptr);
}

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

3009
static void stli_onbreset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
{	
	outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
	udelay(10);
	outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
	mdelay(1000);
}

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

/*
 *	The following routines act on ONboard EISA.
 */

3023
static void stli_onbeinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
{
	unsigned long	memconf;

	outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
	outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
	udelay(10);
	outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
	mdelay(1000);

	memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
	outb(memconf, (brdp->iobase + ONB_EIMEMARL));
	memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
	outb(memconf, (brdp->iobase + ONB_EIMEMARH));
	outb(0x1, brdp->iobase);
	mdelay(1);
}

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

3043
static void stli_onbeenable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3044 3045 3046 3047 3048 3049
{	
	outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
}

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

3050
static void stli_onbedisable(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3051 3052 3053 3054 3055 3056
{	
	outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
}

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

3057
static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
3058
{	
3059
	void __iomem *ptr;
A
Alan Cox 已提交
3060
	unsigned char val;
L
Linus Torvalds 已提交
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080

	if (offset > brdp->memsize) {
		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
				"range at line=%d(%d), brd=%d\n",
			(int) offset, line, __LINE__, brdp->brdnr);
		ptr = NULL;
		val = 0;
	} else {
		ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
		if (offset < ONB_EIPAGESIZE)
			val = ONB_EIENABLE;
		else
			val = ONB_EIENABLE | 0x40;
	}
	outb(val, (brdp->iobase + ONB_EICONFR));
	return(ptr);
}

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

3081
static void stli_onbereset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
{	
	outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
	udelay(10);
	outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
	mdelay(1000);
}

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

/*
 *	The following routines act on Brumby boards.
 */

3095
static void stli_bbyinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
{
	outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
	udelay(10);
	outb(0, (brdp->iobase + BBY_ATCONFR));
	mdelay(1000);
	outb(0x1, brdp->iobase);
	mdelay(1);
}

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

3107
static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
3108
{	
3109
	void __iomem *ptr;
A
Alan Cox 已提交
3110
	unsigned char val;
L
Linus Torvalds 已提交
3111

A
Alan Cox 已提交
3112
	BUG_ON(offset > brdp->memsize);
L
Linus Torvalds 已提交
3113

A
Alan Cox 已提交
3114 3115
	ptr = brdp->membase + (offset % BBY_PAGESIZE);
	val = (unsigned char) (offset / BBY_PAGESIZE);
L
Linus Torvalds 已提交
3116 3117 3118 3119 3120 3121
	outb(val, (brdp->iobase + BBY_ATCONFR));
	return(ptr);
}

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

3122
static void stli_bbyreset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135
{	
	outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
	udelay(10);
	outb(0, (brdp->iobase + BBY_ATCONFR));
	mdelay(1000);
}

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

/*
 *	The following routines act on original old Stallion boards.
 */

3136
static void stli_stalinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3137 3138 3139 3140 3141 3142 3143
{
	outb(0x1, brdp->iobase);
	mdelay(1000);
}

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

3144
static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
L
Linus Torvalds 已提交
3145
{	
A
Alan Cox 已提交
3146 3147
	BUG_ON(offset > brdp->memsize);
	return brdp->membase + (offset % STAL_PAGESIZE);
L
Linus Torvalds 已提交
3148 3149 3150 3151
}

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

3152
static void stli_stalreset(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3153
{	
A
Alan Cox 已提交
3154
	u32 __iomem *vecp;
L
Linus Torvalds 已提交
3155

A
Alan Cox 已提交
3156 3157
	vecp = (u32 __iomem *) (brdp->membase + 0x30);
	writel(0xffff0000, vecp);
L
Linus Torvalds 已提交
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
	outb(0, brdp->iobase);
	mdelay(1000);
}

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

/*
 *	Try to find an ECP board and initialize it. This handles only ECP
 *	board types.
 */

3169
static int stli_initecp(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3170
{
A
Alan Cox 已提交
3171 3172 3173 3174
	cdkecpsig_t sig;
	cdkecpsig_t __iomem *sigsp;
	unsigned int status, nxtid;
	char *name;
3175
	int retval, panelnr, nrports;
L
Linus Torvalds 已提交
3176

3177 3178 3179 3180 3181
	if ((brdp->iobase == 0) || (brdp->memaddr == 0)) {
		retval = -ENODEV;
		goto err;
	}

I
Ingo Korb 已提交
3182 3183
	brdp->iosize = ECP_IOSIZE;

3184 3185 3186
	if (!request_region(brdp->iobase, brdp->iosize, "istallion")) {
		retval = -EIO;
		goto err;
L
Linus Torvalds 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
	}

/*
 *	Based on the specific board type setup the common vars to access
 *	and enable shared memory. Set all board specific information now
 *	as well.
 */
	switch (brdp->brdtype) {
	case BRD_ECP:
		brdp->memsize = ECP_MEMSIZE;
		brdp->pagesize = ECP_ATPAGESIZE;
		brdp->init = stli_ecpinit;
		brdp->enable = stli_ecpenable;
		brdp->reenable = stli_ecpenable;
		brdp->disable = stli_ecpdisable;
		brdp->getmemptr = stli_ecpgetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_ecpreset;
		name = "serial(EC8/64)";
		break;

	case BRD_ECPE:
		brdp->memsize = ECP_MEMSIZE;
		brdp->pagesize = ECP_EIPAGESIZE;
		brdp->init = stli_ecpeiinit;
		brdp->enable = stli_ecpeienable;
		brdp->reenable = stli_ecpeienable;
		brdp->disable = stli_ecpeidisable;
		brdp->getmemptr = stli_ecpeigetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_ecpeireset;
		name = "serial(EC8/64-EI)";
		break;

	case BRD_ECPMC:
		brdp->memsize = ECP_MEMSIZE;
		brdp->pagesize = ECP_MCPAGESIZE;
		brdp->init = NULL;
		brdp->enable = stli_ecpmcenable;
		brdp->reenable = stli_ecpmcenable;
		brdp->disable = stli_ecpmcdisable;
		brdp->getmemptr = stli_ecpmcgetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_ecpmcreset;
		name = "serial(EC8/64-MCA)";
		break;

	case BRD_ECPPCI:
		brdp->memsize = ECP_PCIMEMSIZE;
		brdp->pagesize = ECP_PCIPAGESIZE;
		brdp->init = stli_ecppciinit;
		brdp->enable = NULL;
		brdp->reenable = NULL;
		brdp->disable = NULL;
		brdp->getmemptr = stli_ecppcigetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_ecppcireset;
		name = "serial(EC/RA-PCI)";
		break;

	default:
3248 3249
		retval = -EINVAL;
		goto err_reg;
L
Linus Torvalds 已提交
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259
	}

/*
 *	The per-board operations structure is all set up, so now let's go
 *	and get the board operational. Firstly initialize board configuration
 *	registers. Set the memory mapping info so we can get at the boards
 *	shared memory.
 */
	EBRDINIT(brdp);

3260
	brdp->membase = ioremap_nocache(brdp->memaddr, brdp->memsize);
3261 3262 3263
	if (brdp->membase == NULL) {
		retval = -ENOMEM;
		goto err_reg;
L
Linus Torvalds 已提交
3264 3265 3266 3267 3268 3269 3270 3271
	}

/*
 *	Now that all specific code is set up, enable the shared memory and
 *	look for the a signature area that will tell us exactly what board
 *	this is, and what it is connected to it.
 */
	EBRDENABLE(brdp);
A
Alan Cox 已提交
3272
	sigsp = (cdkecpsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3273
	memcpy_fromio(&sig, sigsp, sizeof(cdkecpsig_t));
L
Linus Torvalds 已提交
3274 3275
	EBRDDISABLE(brdp);

3276 3277 3278
	if (sig.magic != cpu_to_le32(ECP_MAGIC)) {
		retval = -ENODEV;
		goto err_unmap;
L
Linus Torvalds 已提交
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
	}

/*
 *	Scan through the signature looking at the panels connected to the
 *	board. Calculate the total number of ports as we go.
 */
	for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
		status = sig.panelid[nxtid];
		if ((status & ECH_PNLIDMASK) != nxtid)
			break;

		brdp->panelids[panelnr] = status;
		nrports = (status & ECH_PNL16PORT) ? 16 : 8;
		if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
			nxtid++;
		brdp->panels[panelnr] = nrports;
		brdp->nrports += nrports;
		nxtid++;
		brdp->nrpanels++;
	}


	brdp->state |= BST_FOUND;
A
Alan Cox 已提交
3302
	return 0;
3303 3304 3305 3306 3307 3308 3309
err_unmap:
	iounmap(brdp->membase);
	brdp->membase = NULL;
err_reg:
	release_region(brdp->iobase, brdp->iosize);
err:
	return retval;
L
Linus Torvalds 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318
}

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

/*
 *	Try to find an ONboard, Brumby or Stallion board and initialize it.
 *	This handles only these board types.
 */

3319
static int stli_initonb(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3320
{
A
Alan Cox 已提交
3321 3322 3323
	cdkonbsig_t sig;
	cdkonbsig_t __iomem *sigsp;
	char *name;
3324
	int i, retval;
L
Linus Torvalds 已提交
3325 3326 3327 3328

/*
 *	Do a basic sanity check on the IO and memory addresses.
 */
3329 3330 3331 3332
	if (brdp->iobase == 0 || brdp->memaddr == 0) {
		retval = -ENODEV;
		goto err;
	}
L
Linus Torvalds 已提交
3333 3334 3335

	brdp->iosize = ONB_IOSIZE;
	
3336 3337 3338 3339
	if (!request_region(brdp->iobase, brdp->iosize, "istallion")) {
		retval = -EIO;
		goto err;
	}
L
Linus Torvalds 已提交
3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404

/*
 *	Based on the specific board type setup the common vars to access
 *	and enable shared memory. Set all board specific information now
 *	as well.
 */
	switch (brdp->brdtype) {
	case BRD_ONBOARD:
	case BRD_ONBOARD2:
		brdp->memsize = ONB_MEMSIZE;
		brdp->pagesize = ONB_ATPAGESIZE;
		brdp->init = stli_onbinit;
		brdp->enable = stli_onbenable;
		brdp->reenable = stli_onbenable;
		brdp->disable = stli_onbdisable;
		brdp->getmemptr = stli_onbgetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_onbreset;
		if (brdp->memaddr > 0x100000)
			brdp->enabval = ONB_MEMENABHI;
		else
			brdp->enabval = ONB_MEMENABLO;
		name = "serial(ONBoard)";
		break;

	case BRD_ONBOARDE:
		brdp->memsize = ONB_EIMEMSIZE;
		brdp->pagesize = ONB_EIPAGESIZE;
		brdp->init = stli_onbeinit;
		brdp->enable = stli_onbeenable;
		brdp->reenable = stli_onbeenable;
		brdp->disable = stli_onbedisable;
		brdp->getmemptr = stli_onbegetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_onbereset;
		name = "serial(ONBoard/E)";
		break;

	case BRD_BRUMBY4:
		brdp->memsize = BBY_MEMSIZE;
		brdp->pagesize = BBY_PAGESIZE;
		brdp->init = stli_bbyinit;
		brdp->enable = NULL;
		brdp->reenable = NULL;
		brdp->disable = NULL;
		brdp->getmemptr = stli_bbygetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_bbyreset;
		name = "serial(Brumby)";
		break;

	case BRD_STALLION:
		brdp->memsize = STAL_MEMSIZE;
		brdp->pagesize = STAL_PAGESIZE;
		brdp->init = stli_stalinit;
		brdp->enable = NULL;
		brdp->reenable = NULL;
		brdp->disable = NULL;
		brdp->getmemptr = stli_stalgetmemptr;
		brdp->intr = stli_ecpintr;
		brdp->reset = stli_stalreset;
		name = "serial(Stallion)";
		break;

	default:
3405 3406
		retval = -EINVAL;
		goto err_reg;
L
Linus Torvalds 已提交
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
	}

/*
 *	The per-board operations structure is all set up, so now let's go
 *	and get the board operational. Firstly initialize board configuration
 *	registers. Set the memory mapping info so we can get at the boards
 *	shared memory.
 */
	EBRDINIT(brdp);

3417
	brdp->membase = ioremap_nocache(brdp->memaddr, brdp->memsize);
3418 3419 3420
	if (brdp->membase == NULL) {
		retval = -ENOMEM;
		goto err_reg;
L
Linus Torvalds 已提交
3421 3422 3423 3424 3425 3426 3427 3428
	}

/*
 *	Now that all specific code is set up, enable the shared memory and
 *	look for the a signature area that will tell us exactly what board
 *	this is, and how many ports.
 */
	EBRDENABLE(brdp);
A
Alan Cox 已提交
3429 3430
	sigsp = (cdkonbsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
	memcpy_fromio(&sig, sigsp, sizeof(cdkonbsig_t));
L
Linus Torvalds 已提交
3431 3432
	EBRDDISABLE(brdp);

A
Alan Cox 已提交
3433 3434 3435
	if (sig.magic0 != cpu_to_le16(ONB_MAGIC0) ||
	    sig.magic1 != cpu_to_le16(ONB_MAGIC1) ||
	    sig.magic2 != cpu_to_le16(ONB_MAGIC2) ||
3436 3437 3438
	    sig.magic3 != cpu_to_le16(ONB_MAGIC3)) {
		retval = -ENODEV;
		goto err_unmap;
L
Linus Torvalds 已提交
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458
	}

/*
 *	Scan through the signature alive mask and calculate how many ports
 *	there are on this board.
 */
	brdp->nrpanels = 1;
	if (sig.amask1) {
		brdp->nrports = 32;
	} else {
		for (i = 0; (i < 16); i++) {
			if (((sig.amask0 << i) & 0x8000) == 0)
				break;
		}
		brdp->nrports = i;
	}
	brdp->panels[0] = brdp->nrports;


	brdp->state |= BST_FOUND;
A
Alan Cox 已提交
3459
	return 0;
3460 3461 3462 3463 3464 3465 3466
err_unmap:
	iounmap(brdp->membase);
	brdp->membase = NULL;
err_reg:
	release_region(brdp->iobase, brdp->iosize);
err:
	return retval;
L
Linus Torvalds 已提交
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
}

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

/*
 *	Start up a running board. This routine is only called after the
 *	code has been down loaded to the board and is operational. It will
 *	read in the memory map, and get the show on the road...
 */

3477
static int stli_startbrd(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3478
{
A
Alan Cox 已提交
3479 3480 3481 3482
	cdkhdr_t __iomem *hdrp;
	cdkmem_t __iomem *memp;
	cdkasy_t __iomem *ap;
	unsigned long flags;
3483
	unsigned int portnr, nrdevs, i;
3484
	struct stliport *portp;
3485
	int rc = 0;
A
Alan Cox 已提交
3486 3487 3488
	u32 memoff;

	spin_lock_irqsave(&brd_lock, flags);
L
Linus Torvalds 已提交
3489
	EBRDENABLE(brdp);
A
Alan Cox 已提交
3490
	hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
L
Linus Torvalds 已提交
3491 3492 3493 3494 3495
	nrdevs = hdrp->nrdevs;

#if 0
	printk("%s(%d): CDK version %d.%d.%d --> "
		"nrdevs=%d memp=%x hostp=%x slavep=%x\n",
A
Alan Cox 已提交
3496 3497 3498
		 __FILE__, __LINE__, readb(&hdrp->ver_release), readb(&hdrp->ver_modification),
		 readb(&hdrp->ver_fix), nrdevs, (int) readl(&hdrp->memp), readl(&hdrp->hostp),
		 readl(&hdrp->slavep));
L
Linus Torvalds 已提交
3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509
#endif

	if (nrdevs < (brdp->nrports + 1)) {
		printk(KERN_ERR "STALLION: slave failed to allocate memory for "
				"all devices, devices=%d\n", nrdevs);
		brdp->nrports = nrdevs - 1;
	}
	brdp->nrdevs = nrdevs;
	brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
	brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
	brdp->bitsize = (nrdevs + 7) / 8;
A
Alan Cox 已提交
3510 3511
	memoff = readl(&hdrp->memp);
	if (memoff > brdp->memsize) {
L
Linus Torvalds 已提交
3512 3513 3514 3515
		printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
		rc = -EIO;
		goto stli_donestartup;
	}
A
Alan Cox 已提交
3516 3517
	memp = (cdkmem_t __iomem *) EBRDGETMEMPTR(brdp, memoff);
	if (readw(&memp->dtype) != TYP_ASYNCTRL) {
L
Linus Torvalds 已提交
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
		printk(KERN_ERR "STALLION: no slave control device found\n");
		goto stli_donestartup;
	}
	memp++;

/*
 *	Cycle through memory allocation of each port. We are guaranteed to
 *	have all ports inside the first page of slave window, so no need to
 *	change pages while reading memory map.
 */
	for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
A
Alan Cox 已提交
3529
		if (readw(&memp->dtype) != TYP_ASYNC)
L
Linus Torvalds 已提交
3530 3531
			break;
		portp = brdp->ports[portnr];
A
Alan Cox 已提交
3532
		if (portp == NULL)
L
Linus Torvalds 已提交
3533 3534
			break;
		portp->devnr = i;
A
Alan Cox 已提交
3535
		portp->addr = readl(&memp->offset);
L
Linus Torvalds 已提交
3536 3537 3538 3539 3540
		portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
		portp->portidx = (unsigned char) (i / 8);
		portp->portbit = (unsigned char) (0x1 << (i % 8));
	}

A
Alan Cox 已提交
3541
	writeb(0xff, &hdrp->slavereq);
L
Linus Torvalds 已提交
3542 3543 3544 3545 3546 3547 3548 3549

/*
 *	For each port setup a local copy of the RX and TX buffer offsets
 *	and sizes. We do this separate from the above, because we need to
 *	move the shared memory page...
 */
	for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
		portp = brdp->ports[portnr];
A
Alan Cox 已提交
3550
		if (portp == NULL)
L
Linus Torvalds 已提交
3551 3552 3553
			break;
		if (portp->addr == 0)
			break;
A
Alan Cox 已提交
3554 3555 3556 3557 3558 3559
		ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
		if (ap != NULL) {
			portp->rxsize = readw(&ap->rxq.size);
			portp->txsize = readw(&ap->txq.size);
			portp->rxoffset = readl(&ap->rxq.offset);
			portp->txoffset = readl(&ap->txq.offset);
L
Linus Torvalds 已提交
3560 3561 3562 3563 3564
		}
	}

stli_donestartup:
	EBRDDISABLE(brdp);
A
Alan Cox 已提交
3565
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
3566 3567 3568 3569 3570 3571

	if (rc == 0)
		brdp->state |= BST_STARTED;

	if (! stli_timeron) {
		stli_timeron++;
3572
		mod_timer(&stli_timerlist, STLI_TIMEOUT);
L
Linus Torvalds 已提交
3573 3574
	}

A
Alan Cox 已提交
3575
	return rc;
L
Linus Torvalds 已提交
3576 3577 3578 3579 3580 3581 3582 3583
}

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

/*
 *	Probe and initialize the specified board.
 */

3584
static int __devinit stli_brdinit(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3585
{
3586 3587
	int retval;

L
Linus Torvalds 已提交
3588 3589 3590 3591 3592
	switch (brdp->brdtype) {
	case BRD_ECP:
	case BRD_ECPE:
	case BRD_ECPMC:
	case BRD_ECPPCI:
3593
		retval = stli_initecp(brdp);
L
Linus Torvalds 已提交
3594 3595 3596 3597 3598 3599
		break;
	case BRD_ONBOARD:
	case BRD_ONBOARDE:
	case BRD_ONBOARD2:
	case BRD_BRUMBY4:
	case BRD_STALLION:
3600
		retval = stli_initonb(brdp);
L
Linus Torvalds 已提交
3601 3602 3603 3604
		break;
	default:
		printk(KERN_ERR "STALLION: board=%d is unknown board "
				"type=%d\n", brdp->brdnr, brdp->brdtype);
3605
		retval = -ENODEV;
L
Linus Torvalds 已提交
3606 3607
	}

3608 3609
	if (retval)
		return retval;
L
Linus Torvalds 已提交
3610 3611 3612 3613 3614 3615

	stli_initports(brdp);
	printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
		"nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
		brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
		brdp->nrpanels, brdp->nrports);
A
Alan Cox 已提交
3616
	return 0;
L
Linus Torvalds 已提交
3617 3618
}

3619
#if STLI_EISAPROBE != 0
L
Linus Torvalds 已提交
3620 3621 3622 3623 3624 3625 3626
/*****************************************************************************/

/*
 *	Probe around trying to find where the EISA boards shared memory
 *	might be. This is a bit if hack, but it is the best we can do.
 */

3627
static int stli_eisamemprobe(struct stlibrd *brdp)
L
Linus Torvalds 已提交
3628
{
A
Alan Cox 已提交
3629 3630
	cdkecpsig_t	ecpsig, __iomem *ecpsigp;
	cdkonbsig_t	onbsig, __iomem *onbsigp;
L
Linus Torvalds 已提交
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655
	int		i, foundit;

/*
 *	First up we reset the board, to get it into a known state. There
 *	is only 2 board types here we need to worry about. Don;t use the
 *	standard board init routine here, it programs up the shared
 *	memory address, and we don't know it yet...
 */
	if (brdp->brdtype == BRD_ECPE) {
		outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
		outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
		udelay(10);
		outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
		udelay(500);
		stli_ecpeienable(brdp);
	} else if (brdp->brdtype == BRD_ONBOARDE) {
		outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
		outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
		udelay(10);
		outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
		mdelay(100);
		outb(0x1, brdp->iobase);
		mdelay(1);
		stli_onbeenable(brdp);
	} else {
A
Alan Cox 已提交
3656
		return -ENODEV;
L
Linus Torvalds 已提交
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
	}

	foundit = 0;
	brdp->memsize = ECP_MEMSIZE;

/*
 *	Board shared memory is enabled, so now we have a poke around and
 *	see if we can find it.
 */
	for (i = 0; (i < stli_eisamempsize); i++) {
		brdp->memaddr = stli_eisamemprobeaddrs[i];
3668
		brdp->membase = ioremap_nocache(brdp->memaddr, brdp->memsize);
A
Alan Cox 已提交
3669
		if (brdp->membase == NULL)
L
Linus Torvalds 已提交
3670 3671 3672
			continue;

		if (brdp->brdtype == BRD_ECPE) {
3673
			ecpsigp = stli_ecpeigetmemptr(brdp,
L
Linus Torvalds 已提交
3674
				CDK_SIGADDR, __LINE__);
A
Alan Cox 已提交
3675 3676
			memcpy_fromio(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
			if (ecpsig.magic == cpu_to_le32(ECP_MAGIC))
L
Linus Torvalds 已提交
3677 3678
				foundit = 1;
		} else {
A
Alan Cox 已提交
3679
			onbsigp = (cdkonbsig_t __iomem *) stli_onbegetmemptr(brdp,
L
Linus Torvalds 已提交
3680
				CDK_SIGADDR, __LINE__);
A
Alan Cox 已提交
3681 3682 3683 3684 3685
			memcpy_fromio(&onbsig, onbsigp, sizeof(cdkonbsig_t));
			if ((onbsig.magic0 == cpu_to_le16(ONB_MAGIC0)) &&
			    (onbsig.magic1 == cpu_to_le16(ONB_MAGIC1)) &&
			    (onbsig.magic2 == cpu_to_le16(ONB_MAGIC2)) &&
			    (onbsig.magic3 == cpu_to_le16(ONB_MAGIC3)))
L
Linus Torvalds 已提交
3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708
				foundit = 1;
		}

		iounmap(brdp->membase);
		if (foundit)
			break;
	}

/*
 *	Regardless of whether we found the shared memory or not we must
 *	disable the region. After that return success or failure.
 */
	if (brdp->brdtype == BRD_ECPE)
		stli_ecpeidisable(brdp);
	else
		stli_onbedisable(brdp);

	if (! foundit) {
		brdp->memaddr = 0;
		brdp->membase = NULL;
		printk(KERN_ERR "STALLION: failed to probe shared memory "
				"region for %s in EISA slot=%d\n",
			stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
A
Alan Cox 已提交
3709
		return -ENODEV;
L
Linus Torvalds 已提交
3710
	}
A
Alan Cox 已提交
3711
	return 0;
L
Linus Torvalds 已提交
3712
}
3713
#endif
L
Linus Torvalds 已提交
3714 3715 3716

static int stli_getbrdnr(void)
{
3717
	unsigned int i;
L
Linus Torvalds 已提交
3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728

	for (i = 0; i < STL_MAXBRDS; i++) {
		if (!stli_brds[i]) {
			if (i >= stli_nrbrds)
				stli_nrbrds = i + 1;
			return i;
		}
	}
	return -1;
}

3729
#if STLI_EISAPROBE != 0
L
Linus Torvalds 已提交
3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743
/*****************************************************************************/

/*
 *	Probe around and try to find any EISA boards in system. The biggest
 *	problem here is finding out what memory address is associated with
 *	an EISA board after it is found. The registers of the ECPE and
 *	ONboardE are not readable - so we can't read them from there. We
 *	don't have access to the EISA CMOS (or EISA BIOS) so we don't
 *	actually have any way to find out the real value. The best we can
 *	do is go probing around in the usual places hoping we can find it.
 */

static int stli_findeisabrds(void)
{
3744
	struct stlibrd *brdp;
3745
	unsigned int iobase, eid, i;
3746
	int brdnr, found = 0;
L
Linus Torvalds 已提交
3747 3748

/*
A
Alan Cox 已提交
3749
 *	Firstly check if this is an EISA system.  If this is not an EISA system then
L
Linus Torvalds 已提交
3750 3751
 *	don't bother going any further!
 */
A
Alan Cox 已提交
3752 3753
	if (EISA_bus)
		return 0;
L
Linus Torvalds 已提交
3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770

/*
 *	Looks like an EISA system, so go searching for EISA boards.
 */
	for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
		outb(0xff, (iobase + 0xc80));
		eid = inb(iobase + 0xc80);
		eid |= inb(iobase + 0xc81) << 8;
		if (eid != STL_EISAID)
			continue;

/*
 *		We have found a board. Need to check if this board was
 *		statically configured already (just in case!).
 */
		for (i = 0; (i < STL_MAXBRDS); i++) {
			brdp = stli_brds[i];
A
Alan Cox 已提交
3771
			if (brdp == NULL)
L
Linus Torvalds 已提交
3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
				continue;
			if (brdp->iobase == iobase)
				break;
		}
		if (i < STL_MAXBRDS)
			continue;

/*
 *		We have found a Stallion board and it is not configured already.
 *		Allocate a board structure and initialize it.
 */
A
Alan Cox 已提交
3783
		if ((brdp = stli_allocbrd()) == NULL)
3784
			return found ? : -ENOMEM;
3785 3786
		brdnr = stli_getbrdnr();
		if (brdnr < 0)
3787
			return found ? : -ENOMEM;
3788
		brdp->brdnr = (unsigned int)brdnr;
L
Linus Torvalds 已提交
3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
		eid = inb(iobase + 0xc82);
		if (eid == ECP_EISAID)
			brdp->brdtype = BRD_ECPE;
		else if (eid == ONB_EISAID)
			brdp->brdtype = BRD_ONBOARDE;
		else
			brdp->brdtype = BRD_UNKNOWN;
		brdp->iobase = iobase;
		outb(0x1, (iobase + 0xc84));
		if (stli_eisamemprobe(brdp))
			outb(0, (iobase + 0xc84));
3800 3801 3802 3803 3804
		if (stli_brdinit(brdp) < 0) {
			kfree(brdp);
			continue;
		}

3805
		stli_brds[brdp->brdnr] = brdp;
3806
		found++;
3807 3808 3809 3810

		for (i = 0; i < brdp->nrports; i++)
			tty_register_device(stli_serial,
					brdp->brdnr * STL_MAXPORTS + i, NULL);
L
Linus Torvalds 已提交
3811 3812
	}

3813
	return found;
L
Linus Torvalds 已提交
3814
}
3815 3816 3817
#else
static inline int stli_findeisabrds(void) { return 0; }
#endif
L
Linus Torvalds 已提交
3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832

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

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

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

/*
 *	We have a Stallion board. Allocate a board structure and
 *	initialize it. Read its IO and MEMORY resources from PCI
 *	configuration space.
 */

3833 3834
static int __devinit stli_pciprobe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
L
Linus Torvalds 已提交
3835
{
3836
	struct stlibrd *brdp;
3837
	unsigned int i;
3838
	int brdnr, retval = -EIO;
3839 3840 3841 3842 3843 3844 3845 3846 3847

	retval = pci_enable_device(pdev);
	if (retval)
		goto err;
	brdp = stli_allocbrd();
	if (brdp == NULL) {
		retval = -ENOMEM;
		goto err;
	}
3848
	mutex_lock(&stli_brdslock);
3849
	brdnr = stli_getbrdnr();
3850
	if (brdnr < 0) {
L
Linus Torvalds 已提交
3851 3852
		printk(KERN_INFO "STALLION: too many boards found, "
			"maximum supported %d\n", STL_MAXBRDS);
3853
		mutex_unlock(&stli_brdslock);
3854 3855
		retval = -EIO;
		goto err_fr;
L
Linus Torvalds 已提交
3856
	}
3857
	brdp->brdnr = (unsigned int)brdnr;
3858 3859
	stli_brds[brdp->brdnr] = brdp;
	mutex_unlock(&stli_brdslock);
3860
	brdp->brdtype = BRD_ECPPCI;
L
Linus Torvalds 已提交
3861 3862 3863 3864
/*
 *	We have all resources from the board, so lets setup the actual
 *	board structure now.
 */
3865 3866 3867 3868
	brdp->iobase = pci_resource_start(pdev, 3);
	brdp->memaddr = pci_resource_start(pdev, 2);
	retval = stli_brdinit(brdp);
	if (retval)
3869
		goto err_null;
3870

3871
	brdp->state |= BST_PROBED;
3872
	pci_set_drvdata(pdev, brdp);
L
Linus Torvalds 已提交
3873

J
Jiri Slaby 已提交
3874 3875 3876 3877
	EBRDENABLE(brdp);
	brdp->enable = NULL;
	brdp->disable = NULL;

3878 3879 3880 3881
	for (i = 0; i < brdp->nrports; i++)
		tty_register_device(stli_serial, brdp->brdnr * STL_MAXPORTS + i,
				&pdev->dev);

A
Alan Cox 已提交
3882
	return 0;
3883 3884
err_null:
	stli_brds[brdp->brdnr] = NULL;
3885 3886 3887 3888
err_fr:
	kfree(brdp);
err:
	return retval;
L
Linus Torvalds 已提交
3889 3890
}

3891 3892
static void stli_pciremove(struct pci_dev *pdev)
{
3893
	struct stlibrd *brdp = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
3894

3895
	stli_cleanup_ports(brdp);
L
Linus Torvalds 已提交
3896

3897 3898 3899
	iounmap(brdp->membase);
	if (brdp->iosize > 0)
		release_region(brdp->iobase, brdp->iosize);
L
Linus Torvalds 已提交
3900

3901 3902
	stli_brds[brdp->brdnr] = NULL;
	kfree(brdp);
L
Linus Torvalds 已提交
3903 3904
}

3905 3906 3907 3908 3909 3910
static struct pci_driver stli_pcidriver = {
	.name = "istallion",
	.id_table = istallion_pci_tbl,
	.probe = stli_pciprobe,
	.remove = __devexit_p(stli_pciremove)
};
L
Linus Torvalds 已提交
3911 3912 3913 3914 3915 3916
/*****************************************************************************/

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

3917
static struct stlibrd *stli_allocbrd(void)
L
Linus Torvalds 已提交
3918
{
3919
	struct stlibrd *brdp;
L
Linus Torvalds 已提交
3920

3921
	brdp = kzalloc(sizeof(struct stlibrd), GFP_KERNEL);
3922
	if (!brdp) {
L
Linus Torvalds 已提交
3923
		printk(KERN_ERR "STALLION: failed to allocate memory "
3924
				"(size=%Zd)\n", sizeof(struct stlibrd));
3925
		return NULL;
L
Linus Torvalds 已提交
3926 3927
	}
	brdp->magic = STLI_BOARDMAGIC;
A
Alan Cox 已提交
3928
	return brdp;
L
Linus Torvalds 已提交
3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939
}

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

/*
 *	Scan through all the boards in the configuration and see what we
 *	can find.
 */

static int stli_initbrds(void)
{
3940 3941
	struct stlibrd *brdp, *nxtbrdp;
	struct stlconf conf;
3942
	unsigned int i, j, found = 0;
3943
	int retval;
L
Linus Torvalds 已提交
3944

3945 3946 3947 3948 3949
	for (stli_nrbrds = 0; stli_nrbrds < ARRAY_SIZE(stli_brdsp);
			stli_nrbrds++) {
		memset(&conf, 0, sizeof(conf));
		if (stli_parsebrd(&conf, stli_brdsp[stli_nrbrds]) == 0)
			continue;
A
Alan Cox 已提交
3950
		if ((brdp = stli_allocbrd()) == NULL)
3951 3952 3953 3954 3955
			continue;
		brdp->brdnr = stli_nrbrds;
		brdp->brdtype = conf.brdtype;
		brdp->iobase = conf.ioaddr1;
		brdp->memaddr = conf.memaddr;
3956 3957 3958 3959
		if (stli_brdinit(brdp) < 0) {
			kfree(brdp);
			continue;
		}
3960
		stli_brds[brdp->brdnr] = brdp;
3961
		found++;
3962 3963 3964 3965

		for (i = 0; i < brdp->nrports; i++)
			tty_register_device(stli_serial,
					brdp->brdnr * STL_MAXPORTS + i, NULL);
L
Linus Torvalds 已提交
3966 3967
	}

3968 3969 3970
	retval = stli_findeisabrds();
	if (retval > 0)
		found += retval;
3971

L
Linus Torvalds 已提交
3972 3973 3974 3975 3976 3977 3978 3979 3980
/*
 *	All found boards are initialized. Now for a little optimization, if
 *	no boards are sharing the "shared memory" regions then we can just
 *	leave them all enabled. This is in fact the usual case.
 */
	stli_shared = 0;
	if (stli_nrbrds > 1) {
		for (i = 0; (i < stli_nrbrds); i++) {
			brdp = stli_brds[i];
A
Alan Cox 已提交
3981
			if (brdp == NULL)
L
Linus Torvalds 已提交
3982 3983 3984
				continue;
			for (j = i + 1; (j < stli_nrbrds); j++) {
				nxtbrdp = stli_brds[j];
A
Alan Cox 已提交
3985
				if (nxtbrdp == NULL)
L
Linus Torvalds 已提交
3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
					continue;
				if ((brdp->membase >= nxtbrdp->membase) &&
				    (brdp->membase <= (nxtbrdp->membase +
				    nxtbrdp->memsize - 1))) {
					stli_shared++;
					break;
				}
			}
		}
	}

	if (stli_shared == 0) {
		for (i = 0; (i < stli_nrbrds); i++) {
			brdp = stli_brds[i];
A
Alan Cox 已提交
4000
			if (brdp == NULL)
L
Linus Torvalds 已提交
4001 4002 4003 4004 4005 4006 4007 4008 4009
				continue;
			if (brdp->state & BST_FOUND) {
				EBRDENABLE(brdp);
				brdp->enable = NULL;
				brdp->disable = NULL;
			}
		}
	}

J
Jiri Slaby 已提交
4010 4011 4012 4013 4014 4015 4016
	retval = pci_register_driver(&stli_pcidriver);
	if (retval && found == 0) {
		printk(KERN_ERR "Neither isa nor eisa cards found nor pci "
				"driver can be registered!\n");
		goto err;
	}

A
Alan Cox 已提交
4017
	return 0;
4018 4019
err:
	return retval;
L
Linus Torvalds 已提交
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
}

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

/*
 *	Code to handle an "staliomem" read operation. This device is the 
 *	contents of the board shared memory. It is used for down loading
 *	the slave image (and debugging :-)
 */

static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
{
A
Alan Cox 已提交
4032
	unsigned long flags;
4033
	void __iomem *memptr;
4034
	struct stlibrd *brdp;
4035 4036
	unsigned int brdnr;
	int size, n;
A
Alan Cox 已提交
4037 4038
	void *p;
	loff_t off = *offp;
L
Linus Torvalds 已提交
4039

4040
	brdnr = iminor(fp->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
4041
	if (brdnr >= stli_nrbrds)
A
Alan Cox 已提交
4042
		return -ENODEV;
L
Linus Torvalds 已提交
4043
	brdp = stli_brds[brdnr];
A
Alan Cox 已提交
4044 4045
	if (brdp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
4046
	if (brdp->state == 0)
A
Alan Cox 已提交
4047 4048 4049
		return -ENODEV;
	if (off >= brdp->memsize || off + count < off)
		return 0;
L
Linus Torvalds 已提交
4050

4051
	size = min(count, (size_t)(brdp->memsize - off));
A
Alan Cox 已提交
4052 4053 4054 4055 4056 4057 4058 4059

	/*
	 *	Copy the data a page at a time
	 */

	p = (void *)__get_free_page(GFP_KERNEL);
	if(p == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
4060 4061

	while (size > 0) {
A
Alan Cox 已提交
4062 4063
		spin_lock_irqsave(&brd_lock, flags);
		EBRDENABLE(brdp);
4064
		memptr = EBRDGETMEMPTR(brdp, off);
4065 4066
		n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
		n = min(n, (int)PAGE_SIZE);
A
Alan Cox 已提交
4067 4068 4069 4070
		memcpy_fromio(p, memptr, n);
		EBRDDISABLE(brdp);
		spin_unlock_irqrestore(&brd_lock, flags);
		if (copy_to_user(buf, p, n)) {
L
Linus Torvalds 已提交
4071 4072 4073
			count = -EFAULT;
			goto out;
		}
A
Alan Cox 已提交
4074
		off += n;
L
Linus Torvalds 已提交
4075 4076 4077 4078
		buf += n;
		size -= n;
	}
out:
A
Alan Cox 已提交
4079 4080 4081
	*offp = off;
	free_page((unsigned long)p);
	return count;
L
Linus Torvalds 已提交
4082 4083 4084 4085 4086 4087 4088 4089
}

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

/*
 *	Code to handle an "staliomem" write operation. This device is the 
 *	contents of the board shared memory. It is used for down loading
 *	the slave image (and debugging :-)
A
Alan Cox 已提交
4090 4091
 *
 *	FIXME: copy under lock
L
Linus Torvalds 已提交
4092 4093 4094 4095
 */

static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
{
A
Alan Cox 已提交
4096
	unsigned long flags;
4097
	void __iomem *memptr;
4098
	struct stlibrd *brdp;
A
Alan Cox 已提交
4099
	char __user *chbuf;
4100 4101
	unsigned int brdnr;
	int size, n;
A
Alan Cox 已提交
4102 4103
	void *p;
	loff_t off = *offp;
L
Linus Torvalds 已提交
4104

4105
	brdnr = iminor(fp->f_path.dentry->d_inode);
A
Alan Cox 已提交
4106

L
Linus Torvalds 已提交
4107
	if (brdnr >= stli_nrbrds)
A
Alan Cox 已提交
4108
		return -ENODEV;
L
Linus Torvalds 已提交
4109
	brdp = stli_brds[brdnr];
A
Alan Cox 已提交
4110 4111
	if (brdp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
4112
	if (brdp->state == 0)
A
Alan Cox 已提交
4113 4114 4115
		return -ENODEV;
	if (off >= brdp->memsize || off + count < off)
		return 0;
L
Linus Torvalds 已提交
4116 4117

	chbuf = (char __user *) buf;
4118
	size = min(count, (size_t)(brdp->memsize - off));
A
Alan Cox 已提交
4119 4120 4121 4122 4123 4124 4125 4126

	/*
	 *	Copy the data a page at a time
	 */

	p = (void *)__get_free_page(GFP_KERNEL);
	if(p == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
4127 4128

	while (size > 0) {
4129 4130
		n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
		n = min(n, (int)PAGE_SIZE);
A
Alan Cox 已提交
4131 4132 4133
		if (copy_from_user(p, chbuf, n)) {
			if (count == 0)
				count = -EFAULT;
L
Linus Torvalds 已提交
4134 4135
			goto out;
		}
A
Alan Cox 已提交
4136 4137
		spin_lock_irqsave(&brd_lock, flags);
		EBRDENABLE(brdp);
4138
		memptr = EBRDGETMEMPTR(brdp, off);
A
Alan Cox 已提交
4139 4140 4141 4142
		memcpy_toio(memptr, p, n);
		EBRDDISABLE(brdp);
		spin_unlock_irqrestore(&brd_lock, flags);
		off += n;
L
Linus Torvalds 已提交
4143 4144 4145 4146
		chbuf += n;
		size -= n;
	}
out:
A
Alan Cox 已提交
4147 4148 4149
	free_page((unsigned long) p);
	*offp = off;
	return count;
L
Linus Torvalds 已提交
4150 4151 4152 4153 4154 4155 4156 4157 4158 4159
}

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

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

static int stli_getbrdstats(combrd_t __user *bp)
{
4160
	struct stlibrd *brdp;
4161
	unsigned int i;
L
Linus Torvalds 已提交
4162 4163 4164 4165

	if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
		return -EFAULT;
	if (stli_brdstats.brd >= STL_MAXBRDS)
A
Alan Cox 已提交
4166
		return -ENODEV;
L
Linus Torvalds 已提交
4167
	brdp = stli_brds[stli_brdstats.brd];
A
Alan Cox 已提交
4168 4169
	if (brdp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187

	memset(&stli_brdstats, 0, sizeof(combrd_t));
	stli_brdstats.brd = brdp->brdnr;
	stli_brdstats.type = brdp->brdtype;
	stli_brdstats.hwid = 0;
	stli_brdstats.state = brdp->state;
	stli_brdstats.ioaddr = brdp->iobase;
	stli_brdstats.memaddr = brdp->memaddr;
	stli_brdstats.nrpanels = brdp->nrpanels;
	stli_brdstats.nrports = brdp->nrports;
	for (i = 0; (i < brdp->nrpanels); i++) {
		stli_brdstats.panels[i].panel = i;
		stli_brdstats.panels[i].hwid = brdp->panelids[i];
		stli_brdstats.panels[i].nrports = brdp->panels[i];
	}

	if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
		return -EFAULT;
A
Alan Cox 已提交
4188
	return 0;
L
Linus Torvalds 已提交
4189 4190 4191 4192 4193 4194 4195 4196
}

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

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

4197 4198
static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr,
		unsigned int portnr)
L
Linus Torvalds 已提交
4199
{
4200
	struct stlibrd *brdp;
4201
	unsigned int i;
L
Linus Torvalds 已提交
4202

4203
	if (brdnr >= STL_MAXBRDS)
A
Alan Cox 已提交
4204
		return NULL;
L
Linus Torvalds 已提交
4205
	brdp = stli_brds[brdnr];
A
Alan Cox 已提交
4206 4207
	if (brdp == NULL)
		return NULL;
L
Linus Torvalds 已提交
4208 4209
	for (i = 0; (i < panelnr); i++)
		portnr += brdp->panels[i];
4210
	if (portnr >= brdp->nrports)
A
Alan Cox 已提交
4211 4212
		return NULL;
	return brdp->ports[portnr];
L
Linus Torvalds 已提交
4213 4214 4215 4216 4217 4218 4219 4220 4221 4222
}

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

/*
 *	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).
 */

A
Alan Cox 已提交
4223
static int stli_portcmdstats(struct tty_struct *tty, struct stliport *portp)
L
Linus Torvalds 已提交
4224 4225
{
	unsigned long	flags;
4226
	struct stlibrd	*brdp;
L
Linus Torvalds 已提交
4227 4228 4229 4230
	int		rc;

	memset(&stli_comstats, 0, sizeof(comstats_t));

A
Alan Cox 已提交
4231 4232
	if (portp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
4233
	brdp = stli_brds[portp->brdnr];
A
Alan Cox 已提交
4234 4235
	if (brdp == NULL)
		return -ENODEV;
L
Linus Torvalds 已提交
4236 4237 4238 4239

	if (brdp->state & BST_STARTED) {
		if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
		    &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
A
Alan Cox 已提交
4240
			return rc;
L
Linus Torvalds 已提交
4241 4242 4243 4244 4245 4246 4247 4248
	} else {
		memset(&stli_cdkstats, 0, sizeof(asystats_t));
	}

	stli_comstats.brd = portp->brdnr;
	stli_comstats.panel = portp->panelnr;
	stli_comstats.port = portp->portnr;
	stli_comstats.state = portp->state;
W
Wang Chen 已提交
4249
	stli_comstats.flags = portp->port.flags;
L
Linus Torvalds 已提交
4250

A
Alan Cox 已提交
4251
	spin_lock_irqsave(&brd_lock, flags);
A
Alan Cox 已提交
4252 4253 4254
	if (tty != NULL) {
		if (portp->port.tty == tty) {
			stli_comstats.ttystate = tty->flags;
A
Alan Cox 已提交
4255
			stli_comstats.rxbuffered = -1;
A
Alan Cox 已提交
4256 4257 4258 4259 4260
			if (tty->termios != NULL) {
				stli_comstats.cflags = tty->termios->c_cflag;
				stli_comstats.iflags = tty->termios->c_iflag;
				stli_comstats.oflags = tty->termios->c_oflag;
				stli_comstats.lflags = tty->termios->c_lflag;
L
Linus Torvalds 已提交
4261 4262 4263
			}
		}
	}
A
Alan Cox 已提交
4264
	spin_unlock_irqrestore(&brd_lock, flags);
L
Linus Torvalds 已提交
4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285

	stli_comstats.txtotal = stli_cdkstats.txchars;
	stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
	stli_comstats.txbuffered = stli_cdkstats.txringq;
	stli_comstats.rxbuffered += stli_cdkstats.rxringq;
	stli_comstats.rxoverrun = stli_cdkstats.overruns;
	stli_comstats.rxparity = stli_cdkstats.parity;
	stli_comstats.rxframing = stli_cdkstats.framing;
	stli_comstats.rxlost = stli_cdkstats.ringover;
	stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
	stli_comstats.txbreaks = stli_cdkstats.txbreaks;
	stli_comstats.txxon = stli_cdkstats.txstart;
	stli_comstats.txxoff = stli_cdkstats.txstop;
	stli_comstats.rxxon = stli_cdkstats.rxstart;
	stli_comstats.rxxoff = stli_cdkstats.rxstop;
	stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
	stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
	stli_comstats.modem = stli_cdkstats.dcdcnt;
	stli_comstats.hwid = stli_cdkstats.hwid;
	stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);

A
Alan Cox 已提交
4286
	return 0;
L
Linus Torvalds 已提交
4287 4288 4289 4290 4291 4292 4293 4294 4295 4296
}

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

/*
 *	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).
 */

A
Alan Cox 已提交
4297 4298
static int stli_getportstats(struct tty_struct *tty, struct stliport *portp,
							comstats_t __user *cp)
L
Linus Torvalds 已提交
4299
{
4300
	struct stlibrd *brdp;
A
Alan Cox 已提交
4301
	int rc;
L
Linus Torvalds 已提交
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315

	if (!portp) {
		if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
			return -EFAULT;
		portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
			stli_comstats.port);
		if (!portp)
			return -ENODEV;
	}

	brdp = stli_brds[portp->brdnr];
	if (!brdp)
		return -ENODEV;

A
Alan Cox 已提交
4316
	if ((rc = stli_portcmdstats(tty, portp)) < 0)
L
Linus Torvalds 已提交
4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328
		return rc;

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

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

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

4329
static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp)
L
Linus Torvalds 已提交
4330
{
4331
	struct stlibrd *brdp;
A
Alan Cox 已提交
4332
	int rc;
L
Linus Torvalds 已提交
4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367

	if (!portp) {
		if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
			return -EFAULT;
		portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
			stli_comstats.port);
		if (!portp)
			return -ENODEV;
	}

	brdp = stli_brds[portp->brdnr];
	if (!brdp)
		return -ENODEV;

	if (brdp->state & BST_STARTED) {
		if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0)
			return rc;
	}

	memset(&stli_comstats, 0, sizeof(comstats_t));
	stli_comstats.brd = portp->brdnr;
	stli_comstats.panel = portp->panelnr;
	stli_comstats.port = portp->portnr;

	if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
		return -EFAULT;
	return 0;
}

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

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

4368
static int stli_getportstruct(struct stliport __user *arg)
L
Linus Torvalds 已提交
4369
{
4370
	struct stliport stli_dummyport;
4371
	struct stliport *portp;
L
Linus Torvalds 已提交
4372

4373
	if (copy_from_user(&stli_dummyport, arg, sizeof(struct stliport)))
L
Linus Torvalds 已提交
4374 4375 4376 4377 4378
		return -EFAULT;
	portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
		 stli_dummyport.portnr);
	if (!portp)
		return -ENODEV;
4379
	if (copy_to_user(arg, portp, sizeof(struct stliport)))
L
Linus Torvalds 已提交
4380 4381 4382 4383 4384 4385 4386 4387 4388 4389
		return -EFAULT;
	return 0;
}

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

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

4390
static int stli_getbrdstruct(struct stlibrd __user *arg)
L
Linus Torvalds 已提交
4391
{
4392
	struct stlibrd stli_dummybrd;
4393
	struct stlibrd *brdp;
L
Linus Torvalds 已提交
4394

4395
	if (copy_from_user(&stli_dummybrd, arg, sizeof(struct stlibrd)))
L
Linus Torvalds 已提交
4396
		return -EFAULT;
4397
	if (stli_dummybrd.brdnr >= STL_MAXBRDS)
L
Linus Torvalds 已提交
4398 4399 4400 4401
		return -ENODEV;
	brdp = stli_brds[stli_dummybrd.brdnr];
	if (!brdp)
		return -ENODEV;
4402
	if (copy_to_user(arg, brdp, sizeof(struct stlibrd)))
L
Linus Torvalds 已提交
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
		return -EFAULT;
	return 0;
}

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

/*
 *	The "staliomem" device is also required to do some special operations on
 *	the board. We need to be able to send an interrupt to the board,
 *	reset it, and start/stop it.
 */

static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
{
4417
	struct stlibrd *brdp;
A
Alan Cox 已提交
4418
	int brdnr, rc, done;
L
Linus Torvalds 已提交
4419 4420 4421 4422 4423 4424 4425 4426
	void __user *argp = (void __user *)arg;

/*
 *	First up handle the board independent ioctls.
 */
	done = 0;
	rc = 0;

4427 4428
	lock_kernel();

L
Linus Torvalds 已提交
4429 4430
	switch (cmd) {
	case COM_GETPORTSTATS:
A
Alan Cox 已提交
4431
		rc = stli_getportstats(NULL, NULL, argp);
L
Linus Torvalds 已提交
4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450
		done++;
		break;
	case COM_CLRPORTSTATS:
		rc = stli_clrportstats(NULL, argp);
		done++;
		break;
	case COM_GETBRDSTATS:
		rc = stli_getbrdstats(argp);
		done++;
		break;
	case COM_READPORT:
		rc = stli_getportstruct(argp);
		done++;
		break;
	case COM_READBOARD:
		rc = stli_getbrdstruct(argp);
		done++;
		break;
	}
4451
	unlock_kernel();
L
Linus Torvalds 已提交
4452 4453

	if (done)
A
Alan Cox 已提交
4454
		return rc;
L
Linus Torvalds 已提交
4455 4456 4457 4458 4459 4460 4461

/*
 *	Now handle the board specific ioctls. These all depend on the
 *	minor number of the device they were called from.
 */
	brdnr = iminor(ip);
	if (brdnr >= STL_MAXBRDS)
A
Alan Cox 已提交
4462
		return -ENODEV;
L
Linus Torvalds 已提交
4463 4464
	brdp = stli_brds[brdnr];
	if (!brdp)
A
Alan Cox 已提交
4465
		return -ENODEV;
L
Linus Torvalds 已提交
4466
	if (brdp->state == 0)
A
Alan Cox 已提交
4467
		return -ENODEV;
L
Linus Torvalds 已提交
4468

4469 4470
	lock_kernel();

L
Linus Torvalds 已提交
4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492
	switch (cmd) {
	case STL_BINTR:
		EBRDINTR(brdp);
		break;
	case STL_BSTART:
		rc = stli_startbrd(brdp);
		break;
	case STL_BSTOP:
		brdp->state &= ~BST_STARTED;
		break;
	case STL_BRESET:
		brdp->state &= ~BST_STARTED;
		EBRDRESET(brdp);
		if (stli_shared == 0) {
			if (brdp->reenable != NULL)
				(* brdp->reenable)(brdp);
		}
		break;
	default:
		rc = -ENOIOCTLCMD;
		break;
	}
4493
	unlock_kernel();
A
Alan Cox 已提交
4494
	return rc;
L
Linus Torvalds 已提交
4495 4496
}

J
Jeff Dike 已提交
4497
static const struct tty_operations stli_ops = {
L
Linus Torvalds 已提交
4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521
	.open = stli_open,
	.close = stli_close,
	.write = stli_write,
	.put_char = stli_putchar,
	.flush_chars = stli_flushchars,
	.write_room = stli_writeroom,
	.chars_in_buffer = stli_charsinbuffer,
	.ioctl = stli_ioctl,
	.set_termios = stli_settermios,
	.throttle = stli_throttle,
	.unthrottle = stli_unthrottle,
	.stop = stli_stop,
	.start = stli_start,
	.hangup = stli_hangup,
	.flush_buffer = stli_flushbuffer,
	.break_ctl = stli_breakctl,
	.wait_until_sent = stli_waituntilsent,
	.send_xchar = stli_sendxchar,
	.read_proc = stli_readproc,
	.tiocmget = stli_tiocmget,
	.tiocmset = stli_tiocmset,
};

/*****************************************************************************/
4522 4523 4524
/*
 *	Loadable module initialization stuff.
 */
L
Linus Torvalds 已提交
4525

4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
static void istallion_cleanup_isa(void)
{
	struct stlibrd	*brdp;
	unsigned int j;

	for (j = 0; (j < stli_nrbrds); j++) {
		if ((brdp = stli_brds[j]) == NULL || (brdp->state & BST_PROBED))
			continue;

		stli_cleanup_ports(brdp);

		iounmap(brdp->membase);
		if (brdp->iosize > 0)
			release_region(brdp->iobase, brdp->iosize);
		kfree(brdp);
		stli_brds[j] = NULL;
	}
}

4545
static int __init istallion_module_init(void)
L
Linus Torvalds 已提交
4546
{
4547 4548
	unsigned int i;
	int retval;
4549

L
Linus Torvalds 已提交
4550 4551
	printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);

A
Alan Cox 已提交
4552 4553 4554
	spin_lock_init(&stli_lock);
	spin_lock_init(&brd_lock);

4555
	stli_txcookbuf = kmalloc(STLI_TXBUFSIZE, GFP_KERNEL);
4556
	if (!stli_txcookbuf) {
L
Linus Torvalds 已提交
4557 4558
		printk(KERN_ERR "STALLION: failed to allocate memory "
				"(size=%d)\n", STLI_TXBUFSIZE);
4559 4560 4561
		retval = -ENOMEM;
		goto err;
	}
L
Linus Torvalds 已提交
4562

4563 4564 4565 4566 4567
	stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
	if (!stli_serial) {
		retval = -ENOMEM;
		goto err_free;
	}
L
Linus Torvalds 已提交
4568 4569 4570 4571 4572 4573 4574 4575 4576

	stli_serial->owner = THIS_MODULE;
	stli_serial->driver_name = stli_drvname;
	stli_serial->name = stli_serialname;
	stli_serial->major = STL_SERIALMAJOR;
	stli_serial->minor_start = 0;
	stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
	stli_serial->subtype = SERIAL_TYPE_NORMAL;
	stli_serial->init_termios = stli_deftermios;
4577
	stli_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
L
Linus Torvalds 已提交
4578 4579
	tty_set_operations(stli_serial, &stli_ops);

4580 4581
	retval = tty_register_driver(stli_serial);
	if (retval) {
L
Linus Torvalds 已提交
4582
		printk(KERN_ERR "STALLION: failed to register serial driver\n");
4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598
		goto err_ttyput;
	}

	retval = stli_initbrds();
	if (retval)
		goto err_ttyunr;

/*
 *	Set up a character driver for the shared memory region. We need this
 *	to down load the slave code image. Also it is a useful debugging tool.
 */
	retval = register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem);
	if (retval) {
		printk(KERN_ERR "STALLION: failed to register serial memory "
				"device\n");
		goto err_deinit;
L
Linus Torvalds 已提交
4599
	}
4600 4601 4602

	istallion_class = class_create(THIS_MODULE, "staliomem");
	for (i = 0; i < 4; i++)
4603 4604 4605
		device_create_drvdata(istallion_class, NULL,
				      MKDEV(STL_SIOMEMMAJOR, i),
				      NULL, "staliomem%d", i);
4606

A
Alan Cox 已提交
4607
	return 0;
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
err_deinit:
	pci_unregister_driver(&stli_pcidriver);
	istallion_cleanup_isa();
err_ttyunr:
	tty_unregister_driver(stli_serial);
err_ttyput:
	put_tty_driver(stli_serial);
err_free:
	kfree(stli_txcookbuf);
err:
	return retval;
L
Linus Torvalds 已提交
4619 4620 4621
}

/*****************************************************************************/
4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634

static void __exit istallion_module_exit(void)
{
	unsigned int j;

	printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
		stli_drvversion);

	if (stli_timeron) {
		stli_timeron = 0;
		del_timer_sync(&stli_timerlist);
	}

4635 4636
	unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");

4637
	for (j = 0; j < 4; j++)
4638
		device_destroy(istallion_class, MKDEV(STL_SIOMEMMAJOR, j));
4639 4640
	class_destroy(istallion_class);

4641 4642
	pci_unregister_driver(&stli_pcidriver);
	istallion_cleanup_isa();
4643

4644 4645
	tty_unregister_driver(stli_serial);
	put_tty_driver(stli_serial);
4646

4647
	kfree(stli_txcookbuf);
4648 4649 4650 4651
}

module_init(istallion_module_init);
module_exit(istallion_module_exit);