PageRenderTime 137ms CodeModel.GetById 18ms app.highlight 102ms RepoModel.GetById 2ms app.codeStats 1ms

/drivers/char/istallion.c

https://bitbucket.org/evzijst/gittest
C | 5276 lines | 3590 code | 777 blank | 909 comment | 755 complexity | 35cbe6dab5652e5cf384df0804d64b1d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*****************************************************************************/
   2
   3/*
   4 *	istallion.c  -- stallion intelligent multiport serial driver.
   5 *
   6 *	Copyright (C) 1996-1999  Stallion Technologies
   7 *	Copyright (C) 1994-1996  Greg Ungerer.
   8 *
   9 *	This code is loosely based on the Linux serial driver, written by
  10 *	Linus Torvalds, Theodore T'so and others.
  11 *
  12 *	This program is free software; you can redistribute it and/or modify
  13 *	it under the terms of the GNU General Public License as published by
  14 *	the Free Software Foundation; either version 2 of the License, or
  15 *	(at your option) any later version.
  16 *
  17 *	This program is distributed in the hope that it will be useful,
  18 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 *	GNU General Public License for more details.
  21 *
  22 *	You should have received a copy of the GNU General Public License
  23 *	along with this program; if not, write to the Free Software
  24 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25 */
  26
  27/*****************************************************************************/
  28
  29#include <linux/config.h>
  30#include <linux/module.h>
  31#include <linux/slab.h>
  32#include <linux/interrupt.h>
  33#include <linux/tty.h>
  34#include <linux/tty_flip.h>
  35#include <linux/serial.h>
  36#include <linux/cdk.h>
  37#include <linux/comstats.h>
  38#include <linux/istallion.h>
  39#include <linux/ioport.h>
  40#include <linux/delay.h>
  41#include <linux/init.h>
  42#include <linux/devfs_fs_kernel.h>
  43#include <linux/device.h>
  44#include <linux/wait.h>
  45
  46#include <asm/io.h>
  47#include <asm/uaccess.h>
  48
  49#ifdef CONFIG_PCI
  50#include <linux/pci.h>
  51#endif
  52
  53/*****************************************************************************/
  54
  55/*
  56 *	Define different board types. Not all of the following board types
  57 *	are supported by this driver. But I will use the standard "assigned"
  58 *	board numbers. Currently supported boards are abbreviated as:
  59 *	ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
  60 *	STAL = Stallion.
  61 */
  62#define	BRD_UNKNOWN	0
  63#define	BRD_STALLION	1
  64#define	BRD_BRUMBY4	2
  65#define	BRD_ONBOARD2	3
  66#define	BRD_ONBOARD	4
  67#define	BRD_BRUMBY8	5
  68#define	BRD_BRUMBY16	6
  69#define	BRD_ONBOARDE	7
  70#define	BRD_ONBOARD32	9
  71#define	BRD_ONBOARD2_32	10
  72#define	BRD_ONBOARDRS	11
  73#define	BRD_EASYIO	20
  74#define	BRD_ECH		21
  75#define	BRD_ECHMC	22
  76#define	BRD_ECP		23
  77#define BRD_ECPE	24
  78#define	BRD_ECPMC	25
  79#define	BRD_ECHPCI	26
  80#define	BRD_ECH64PCI	27
  81#define	BRD_EASYIOPCI	28
  82#define	BRD_ECPPCI	29
  83
  84#define	BRD_BRUMBY	BRD_BRUMBY4
  85
  86/*
  87 *	Define a configuration structure to hold the board configuration.
  88 *	Need to set this up in the code (for now) with the boards that are
  89 *	to be configured into the system. This is what needs to be modified
  90 *	when adding/removing/modifying boards. Each line entry in the
  91 *	stli_brdconf[] array is a board. Each line contains io/irq/memory
  92 *	ranges for that board (as well as what type of board it is).
  93 *	Some examples:
  94 *		{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
  95 *	This line will configure an EasyConnection 8/64 at io address 2a0,
  96 *	and shared memory address of cc000. Multiple EasyConnection 8/64
  97 *	boards can share the same shared memory address space. No interrupt
  98 *	is required for this board type.
  99 *	Another example:
 100 *		{ BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
 101 *	This line will configure an EasyConnection 8/64 EISA in slot 5 and
 102 *	shared memory address of 0x80000000 (2 GByte). Multiple
 103 *	EasyConnection 8/64 EISA boards can share the same shared memory
 104 *	address space. No interrupt is required for this board type.
 105 *	Another example:
 106 *		{ BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
 107 *	This line will configure an ONboard (ISA type) at io address 240,
 108 *	and shared memory address of d0000. Multiple ONboards can share
 109 *	the same shared memory address space. No interrupt required.
 110 *	Another example:
 111 *		{ BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
 112 *	This line will configure a Brumby board (any number of ports!) at
 113 *	io address 360 and shared memory address of c8000. All Brumby boards
 114 *	configured into a system must have their own separate io and memory
 115 *	addresses. No interrupt is required.
 116 *	Another example:
 117 *		{ BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
 118 *	This line will configure an original Stallion board at io address 330
 119 *	and shared memory address d0000 (this would only be valid for a "V4.0"
 120 *	or Rev.O Stallion board). All Stallion boards configured into the
 121 *	system must have their own separate io and memory addresses. No
 122 *	interrupt is required.
 123 */
 124
 125typedef struct {
 126	int		brdtype;
 127	int		ioaddr1;
 128	int		ioaddr2;
 129	unsigned long	memaddr;
 130	int		irq;
 131	int		irqtype;
 132} stlconf_t;
 133
 134static stlconf_t	stli_brdconf[] = {
 135	/*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
 136};
 137
 138static int	stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
 139
 140/*
 141 *	There is some experimental EISA board detection code in this driver.
 142 *	By default it is disabled, but for those that want to try it out,
 143 *	then set the define below to be 1.
 144 */
 145#define	STLI_EISAPROBE	0
 146
 147/*****************************************************************************/
 148
 149/*
 150 *	Define some important driver characteristics. Device major numbers
 151 *	allocated as per Linux Device Registry.
 152 */
 153#ifndef	STL_SIOMEMMAJOR
 154#define	STL_SIOMEMMAJOR		28
 155#endif
 156#ifndef	STL_SERIALMAJOR
 157#define	STL_SERIALMAJOR		24
 158#endif
 159#ifndef	STL_CALLOUTMAJOR
 160#define	STL_CALLOUTMAJOR	25
 161#endif
 162
 163/*****************************************************************************/
 164
 165/*
 166 *	Define our local driver identity first. Set up stuff to deal with
 167 *	all the local structures required by a serial tty driver.
 168 */
 169static char	*stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
 170static char	*stli_drvname = "istallion";
 171static char	*stli_drvversion = "5.6.0";
 172static char	*stli_serialname = "ttyE";
 173
 174static struct tty_driver	*stli_serial;
 175
 176/*
 177 *	We will need to allocate a temporary write buffer for chars that
 178 *	come direct from user space. The problem is that a copy from user
 179 *	space might cause a page fault (typically on a system that is
 180 *	swapping!). All ports will share one buffer - since if the system
 181 *	is already swapping a shared buffer won't make things any worse.
 182 */
 183static char			*stli_tmpwritebuf;
 184static DECLARE_MUTEX(stli_tmpwritesem);
 185
 186#define	STLI_TXBUFSIZE		4096
 187
 188/*
 189 *	Use a fast local buffer for cooked characters. Typically a whole
 190 *	bunch of cooked characters come in for a port, 1 at a time. So we
 191 *	save those up into a local buffer, then write out the whole lot
 192 *	with a large memcpy. Just use 1 buffer for all ports, since its
 193 *	use it is only need for short periods of time by each port.
 194 */
 195static char			*stli_txcookbuf;
 196static int			stli_txcooksize;
 197static int			stli_txcookrealsize;
 198static struct tty_struct	*stli_txcooktty;
 199
 200/*
 201 *	Define a local default termios struct. All ports will be created
 202 *	with this termios initially. Basically all it defines is a raw port
 203 *	at 9600 baud, 8 data bits, no parity, 1 stop bit.
 204 */
 205static struct termios		stli_deftermios = {
 206	.c_cflag	= (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
 207	.c_cc		= INIT_C_CC,
 208};
 209
 210/*
 211 *	Define global stats structures. Not used often, and can be
 212 *	re-used for each stats call.
 213 */
 214static comstats_t	stli_comstats;
 215static combrd_t		stli_brdstats;
 216static asystats_t	stli_cdkstats;
 217static stlibrd_t	stli_dummybrd;
 218static stliport_t	stli_dummyport;
 219
 220/*****************************************************************************/
 221
 222static stlibrd_t	*stli_brds[STL_MAXBRDS];
 223
 224static int		stli_shared;
 225
 226/*
 227 *	Per board state flags. Used with the state field of the board struct.
 228 *	Not really much here... All we need to do is keep track of whether
 229 *	the board has been detected, and whether it is actually running a slave
 230 *	or not.
 231 */
 232#define	BST_FOUND	0x1
 233#define	BST_STARTED	0x2
 234
 235/*
 236 *	Define the set of port state flags. These are marked for internal
 237 *	state purposes only, usually to do with the state of communications
 238 *	with the slave. Most of them need to be updated atomically, so always
 239 *	use the bit setting operations (unless protected by cli/sti).
 240 */
 241#define	ST_INITIALIZING	1
 242#define	ST_OPENING	2
 243#define	ST_CLOSING	3
 244#define	ST_CMDING	4
 245#define	ST_TXBUSY	5
 246#define	ST_RXING	6
 247#define	ST_DOFLUSHRX	7
 248#define	ST_DOFLUSHTX	8
 249#define	ST_DOSIGS	9
 250#define	ST_RXSTOP	10
 251#define	ST_GETSIGS	11
 252
 253/*
 254 *	Define an array of board names as printable strings. Handy for
 255 *	referencing boards when printing trace and stuff.
 256 */
 257static char	*stli_brdnames[] = {
 258	"Unknown",
 259	"Stallion",
 260	"Brumby",
 261	"ONboard-MC",
 262	"ONboard",
 263	"Brumby",
 264	"Brumby",
 265	"ONboard-EI",
 266	(char *) NULL,
 267	"ONboard",
 268	"ONboard-MC",
 269	"ONboard-MC",
 270	(char *) NULL,
 271	(char *) NULL,
 272	(char *) NULL,
 273	(char *) NULL,
 274	(char *) NULL,
 275	(char *) NULL,
 276	(char *) NULL,
 277	(char *) NULL,
 278	"EasyIO",
 279	"EC8/32-AT",
 280	"EC8/32-MC",
 281	"EC8/64-AT",
 282	"EC8/64-EI",
 283	"EC8/64-MC",
 284	"EC8/32-PCI",
 285	"EC8/64-PCI",
 286	"EasyIO-PCI",
 287	"EC/RA-PCI",
 288};
 289
 290/*****************************************************************************/
 291
 292#ifdef MODULE
 293/*
 294 *	Define some string labels for arguments passed from the module
 295 *	load line. These allow for easy board definitions, and easy
 296 *	modification of the io, memory and irq resoucres.
 297 */
 298
 299static char	*board0[8];
 300static char	*board1[8];
 301static char	*board2[8];
 302static char	*board3[8];
 303
 304static char	**stli_brdsp[] = {
 305	(char **) &board0,
 306	(char **) &board1,
 307	(char **) &board2,
 308	(char **) &board3
 309};
 310
 311/*
 312 *	Define a set of common board names, and types. This is used to
 313 *	parse any module arguments.
 314 */
 315
 316typedef struct stlibrdtype {
 317	char	*name;
 318	int	type;
 319} stlibrdtype_t;
 320
 321static stlibrdtype_t	stli_brdstr[] = {
 322	{ "stallion", BRD_STALLION },
 323	{ "1", BRD_STALLION },
 324	{ "brumby", BRD_BRUMBY },
 325	{ "brumby4", BRD_BRUMBY },
 326	{ "brumby/4", BRD_BRUMBY },
 327	{ "brumby-4", BRD_BRUMBY },
 328	{ "brumby8", BRD_BRUMBY },
 329	{ "brumby/8", BRD_BRUMBY },
 330	{ "brumby-8", BRD_BRUMBY },
 331	{ "brumby16", BRD_BRUMBY },
 332	{ "brumby/16", BRD_BRUMBY },
 333	{ "brumby-16", BRD_BRUMBY },
 334	{ "2", BRD_BRUMBY },
 335	{ "onboard2", BRD_ONBOARD2 },
 336	{ "onboard-2", BRD_ONBOARD2 },
 337	{ "onboard/2", BRD_ONBOARD2 },
 338	{ "onboard-mc", BRD_ONBOARD2 },
 339	{ "onboard/mc", BRD_ONBOARD2 },
 340	{ "onboard-mca", BRD_ONBOARD2 },
 341	{ "onboard/mca", BRD_ONBOARD2 },
 342	{ "3", BRD_ONBOARD2 },
 343	{ "onboard", BRD_ONBOARD },
 344	{ "onboardat", BRD_ONBOARD },
 345	{ "4", BRD_ONBOARD },
 346	{ "onboarde", BRD_ONBOARDE },
 347	{ "onboard-e", BRD_ONBOARDE },
 348	{ "onboard/e", BRD_ONBOARDE },
 349	{ "onboard-ei", BRD_ONBOARDE },
 350	{ "onboard/ei", BRD_ONBOARDE },
 351	{ "7", BRD_ONBOARDE },
 352	{ "ecp", BRD_ECP },
 353	{ "ecpat", BRD_ECP },
 354	{ "ec8/64", BRD_ECP },
 355	{ "ec8/64-at", BRD_ECP },
 356	{ "ec8/64-isa", BRD_ECP },
 357	{ "23", BRD_ECP },
 358	{ "ecpe", BRD_ECPE },
 359	{ "ecpei", BRD_ECPE },
 360	{ "ec8/64-e", BRD_ECPE },
 361	{ "ec8/64-ei", BRD_ECPE },
 362	{ "24", BRD_ECPE },
 363	{ "ecpmc", BRD_ECPMC },
 364	{ "ec8/64-mc", BRD_ECPMC },
 365	{ "ec8/64-mca", BRD_ECPMC },
 366	{ "25", BRD_ECPMC },
 367	{ "ecppci", BRD_ECPPCI },
 368	{ "ec/ra", BRD_ECPPCI },
 369	{ "ec/ra-pc", BRD_ECPPCI },
 370	{ "ec/ra-pci", BRD_ECPPCI },
 371	{ "29", BRD_ECPPCI },
 372};
 373
 374/*
 375 *	Define the module agruments.
 376 */
 377MODULE_AUTHOR("Greg Ungerer");
 378MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
 379MODULE_LICENSE("GPL");
 380
 381
 382MODULE_PARM(board0, "1-3s");
 383MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
 384MODULE_PARM(board1, "1-3s");
 385MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
 386MODULE_PARM(board2, "1-3s");
 387MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
 388MODULE_PARM(board3, "1-3s");
 389MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
 390
 391#endif
 392
 393/*
 394 *	Set up a default memory address table for EISA board probing.
 395 *	The default addresses are all bellow 1Mbyte, which has to be the
 396 *	case anyway. They should be safe, since we only read values from
 397 *	them, and interrupts are disabled while we do it. If the higher
 398 *	memory support is compiled in then we also try probing around
 399 *	the 1Gb, 2Gb and 3Gb areas as well...
 400 */
 401static unsigned long	stli_eisamemprobeaddrs[] = {
 402	0xc0000,    0xd0000,    0xe0000,    0xf0000,
 403	0x80000000, 0x80010000, 0x80020000, 0x80030000,
 404	0x40000000, 0x40010000, 0x40020000, 0x40030000,
 405	0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
 406	0xff000000, 0xff010000, 0xff020000, 0xff030000,
 407};
 408
 409static int	stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
 410int		stli_eisaprobe = STLI_EISAPROBE;
 411
 412/*
 413 *	Define the Stallion PCI vendor and device IDs.
 414 */
 415#ifdef CONFIG_PCI
 416#ifndef	PCI_VENDOR_ID_STALLION
 417#define	PCI_VENDOR_ID_STALLION		0x124d
 418#endif
 419#ifndef PCI_DEVICE_ID_ECRA
 420#define	PCI_DEVICE_ID_ECRA		0x0004
 421#endif
 422
 423static struct pci_device_id istallion_pci_tbl[] = {
 424	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
 425	{ 0 }
 426};
 427MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
 428
 429#endif /* CONFIG_PCI */
 430
 431/*****************************************************************************/
 432
 433/*
 434 *	Hardware configuration info for ECP boards. These defines apply
 435 *	to the directly accessible io ports of the ECP. There is a set of
 436 *	defines for each ECP board type, ISA, EISA, MCA and PCI.
 437 */
 438#define	ECP_IOSIZE	4
 439
 440#define	ECP_MEMSIZE	(128 * 1024)
 441#define	ECP_PCIMEMSIZE	(256 * 1024)
 442
 443#define	ECP_ATPAGESIZE	(4 * 1024)
 444#define	ECP_MCPAGESIZE	(4 * 1024)
 445#define	ECP_EIPAGESIZE	(64 * 1024)
 446#define	ECP_PCIPAGESIZE	(64 * 1024)
 447
 448#define	STL_EISAID	0x8c4e
 449
 450/*
 451 *	Important defines for the ISA class of ECP board.
 452 */
 453#define	ECP_ATIREG	0
 454#define	ECP_ATCONFR	1
 455#define	ECP_ATMEMAR	2
 456#define	ECP_ATMEMPR	3
 457#define	ECP_ATSTOP	0x1
 458#define	ECP_ATINTENAB	0x10
 459#define	ECP_ATENABLE	0x20
 460#define	ECP_ATDISABLE	0x00
 461#define	ECP_ATADDRMASK	0x3f000
 462#define	ECP_ATADDRSHFT	12
 463
 464/*
 465 *	Important defines for the EISA class of ECP board.
 466 */
 467#define	ECP_EIIREG	0
 468#define	ECP_EIMEMARL	1
 469#define	ECP_EICONFR	2
 470#define	ECP_EIMEMARH	3
 471#define	ECP_EIENABLE	0x1
 472#define	ECP_EIDISABLE	0x0
 473#define	ECP_EISTOP	0x4
 474#define	ECP_EIEDGE	0x00
 475#define	ECP_EILEVEL	0x80
 476#define	ECP_EIADDRMASKL	0x00ff0000
 477#define	ECP_EIADDRSHFTL	16
 478#define	ECP_EIADDRMASKH	0xff000000
 479#define	ECP_EIADDRSHFTH	24
 480#define	ECP_EIBRDENAB	0xc84
 481
 482#define	ECP_EISAID	0x4
 483
 484/*
 485 *	Important defines for the Micro-channel class of ECP board.
 486 *	(It has a lot in common with the ISA boards.)
 487 */
 488#define	ECP_MCIREG	0
 489#define	ECP_MCCONFR	1
 490#define	ECP_MCSTOP	0x20
 491#define	ECP_MCENABLE	0x80
 492#define	ECP_MCDISABLE	0x00
 493
 494/*
 495 *	Important defines for the PCI class of ECP board.
 496 *	(It has a lot in common with the other ECP boards.)
 497 */
 498#define	ECP_PCIIREG	0
 499#define	ECP_PCICONFR	1
 500#define	ECP_PCISTOP	0x01
 501
 502/*
 503 *	Hardware configuration info for ONboard and Brumby boards. These
 504 *	defines apply to the directly accessible io ports of these boards.
 505 */
 506#define	ONB_IOSIZE	16
 507#define	ONB_MEMSIZE	(64 * 1024)
 508#define	ONB_ATPAGESIZE	(64 * 1024)
 509#define	ONB_MCPAGESIZE	(64 * 1024)
 510#define	ONB_EIMEMSIZE	(128 * 1024)
 511#define	ONB_EIPAGESIZE	(64 * 1024)
 512
 513/*
 514 *	Important defines for the ISA class of ONboard board.
 515 */
 516#define	ONB_ATIREG	0
 517#define	ONB_ATMEMAR	1
 518#define	ONB_ATCONFR	2
 519#define	ONB_ATSTOP	0x4
 520#define	ONB_ATENABLE	0x01
 521#define	ONB_ATDISABLE	0x00
 522#define	ONB_ATADDRMASK	0xff0000
 523#define	ONB_ATADDRSHFT	16
 524
 525#define	ONB_MEMENABLO	0
 526#define	ONB_MEMENABHI	0x02
 527
 528/*
 529 *	Important defines for the EISA class of ONboard board.
 530 */
 531#define	ONB_EIIREG	0
 532#define	ONB_EIMEMARL	1
 533#define	ONB_EICONFR	2
 534#define	ONB_EIMEMARH	3
 535#define	ONB_EIENABLE	0x1
 536#define	ONB_EIDISABLE	0x0
 537#define	ONB_EISTOP	0x4
 538#define	ONB_EIEDGE	0x00
 539#define	ONB_EILEVEL	0x80
 540#define	ONB_EIADDRMASKL	0x00ff0000
 541#define	ONB_EIADDRSHFTL	16
 542#define	ONB_EIADDRMASKH	0xff000000
 543#define	ONB_EIADDRSHFTH	24
 544#define	ONB_EIBRDENAB	0xc84
 545
 546#define	ONB_EISAID	0x1
 547
 548/*
 549 *	Important defines for the Brumby boards. They are pretty simple,
 550 *	there is not much that is programmably configurable.
 551 */
 552#define	BBY_IOSIZE	16
 553#define	BBY_MEMSIZE	(64 * 1024)
 554#define	BBY_PAGESIZE	(16 * 1024)
 555
 556#define	BBY_ATIREG	0
 557#define	BBY_ATCONFR	1
 558#define	BBY_ATSTOP	0x4
 559
 560/*
 561 *	Important defines for the Stallion boards. They are pretty simple,
 562 *	there is not much that is programmably configurable.
 563 */
 564#define	STAL_IOSIZE	16
 565#define	STAL_MEMSIZE	(64 * 1024)
 566#define	STAL_PAGESIZE	(64 * 1024)
 567
 568/*
 569 *	Define the set of status register values for EasyConnection panels.
 570 *	The signature will return with the status value for each panel. From
 571 *	this we can determine what is attached to the board - before we have
 572 *	actually down loaded any code to it.
 573 */
 574#define	ECH_PNLSTATUS	2
 575#define	ECH_PNL16PORT	0x20
 576#define	ECH_PNLIDMASK	0x07
 577#define	ECH_PNLXPID	0x40
 578#define	ECH_PNLINTRPEND	0x80
 579
 580/*
 581 *	Define some macros to do things to the board. Even those these boards
 582 *	are somewhat related there is often significantly different ways of
 583 *	doing some operation on it (like enable, paging, reset, etc). So each
 584 *	board class has a set of functions which do the commonly required
 585 *	operations. The macros below basically just call these functions,
 586 *	generally checking for a NULL function - which means that the board
 587 *	needs nothing done to it to achieve this operation!
 588 */
 589#define	EBRDINIT(brdp)						\
 590	if (brdp->init != NULL)					\
 591		(* brdp->init)(brdp)
 592
 593#define	EBRDENABLE(brdp)					\
 594	if (brdp->enable != NULL)				\
 595		(* brdp->enable)(brdp);
 596
 597#define	EBRDDISABLE(brdp)					\
 598	if (brdp->disable != NULL)				\
 599		(* brdp->disable)(brdp);
 600
 601#define	EBRDINTR(brdp)						\
 602	if (brdp->intr != NULL)					\
 603		(* brdp->intr)(brdp);
 604
 605#define	EBRDRESET(brdp)						\
 606	if (brdp->reset != NULL)				\
 607		(* brdp->reset)(brdp);
 608
 609#define	EBRDGETMEMPTR(brdp,offset)				\
 610	(* brdp->getmemptr)(brdp, offset, __LINE__)
 611
 612/*
 613 *	Define the maximal baud rate, and the default baud base for ports.
 614 */
 615#define	STL_MAXBAUD	460800
 616#define	STL_BAUDBASE	115200
 617#define	STL_CLOSEDELAY	(5 * HZ / 10)
 618
 619/*****************************************************************************/
 620
 621/*
 622 *	Define macros to extract a brd or port number from a minor number.
 623 */
 624#define	MINOR2BRD(min)		(((min) & 0xc0) >> 6)
 625#define	MINOR2PORT(min)		((min) & 0x3f)
 626
 627/*
 628 *	Define a baud rate table that converts termios baud rate selector
 629 *	into the actual baud rate value. All baud rate calculations are based
 630 *	on the actual baud rate required.
 631 */
 632static unsigned int	stli_baudrates[] = {
 633	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 634	9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
 635};
 636
 637/*****************************************************************************/
 638
 639/*
 640 *	Define some handy local macros...
 641 */
 642#undef MIN
 643#define	MIN(a,b)	(((a) <= (b)) ? (a) : (b))
 644
 645#undef	TOLOWER
 646#define	TOLOWER(x)	((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
 647
 648/*****************************************************************************/
 649
 650/*
 651 *	Prototype all functions in this driver!
 652 */
 653
 654#ifdef MODULE
 655static void	stli_argbrds(void);
 656static int	stli_parsebrd(stlconf_t *confp, char **argp);
 657
 658static unsigned long	stli_atol(char *str);
 659#endif
 660
 661int		stli_init(void);
 662static int	stli_open(struct tty_struct *tty, struct file *filp);
 663static void	stli_close(struct tty_struct *tty, struct file *filp);
 664static int	stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
 665static void	stli_putchar(struct tty_struct *tty, unsigned char ch);
 666static void	stli_flushchars(struct tty_struct *tty);
 667static int	stli_writeroom(struct tty_struct *tty);
 668static int	stli_charsinbuffer(struct tty_struct *tty);
 669static int	stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 670static void	stli_settermios(struct tty_struct *tty, struct termios *old);
 671static void	stli_throttle(struct tty_struct *tty);
 672static void	stli_unthrottle(struct tty_struct *tty);
 673static void	stli_stop(struct tty_struct *tty);
 674static void	stli_start(struct tty_struct *tty);
 675static void	stli_flushbuffer(struct tty_struct *tty);
 676static void	stli_breakctl(struct tty_struct *tty, int state);
 677static void	stli_waituntilsent(struct tty_struct *tty, int timeout);
 678static void	stli_sendxchar(struct tty_struct *tty, char ch);
 679static void	stli_hangup(struct tty_struct *tty);
 680static int	stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
 681
 682static int	stli_brdinit(stlibrd_t *brdp);
 683static int	stli_startbrd(stlibrd_t *brdp);
 684static ssize_t	stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
 685static ssize_t	stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
 686static int	stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
 687static void	stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
 688static void	stli_poll(unsigned long arg);
 689static int	stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
 690static int	stli_initopen(stlibrd_t *brdp, stliport_t *portp);
 691static int	stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
 692static int	stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
 693static int	stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
 694static void	stli_dohangup(void *arg);
 695static int	stli_setport(stliport_t *portp);
 696static int	stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
 697static void	stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
 698static void	stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
 699static void	stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
 700static void	stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
 701static long	stli_mktiocm(unsigned long sigvalue);
 702static void	stli_read(stlibrd_t *brdp, stliport_t *portp);
 703static int	stli_getserial(stliport_t *portp, struct serial_struct __user *sp);
 704static int	stli_setserial(stliport_t *portp, struct serial_struct __user *sp);
 705static int	stli_getbrdstats(combrd_t __user *bp);
 706static int	stli_getportstats(stliport_t *portp, comstats_t __user *cp);
 707static int	stli_portcmdstats(stliport_t *portp);
 708static int	stli_clrportstats(stliport_t *portp, comstats_t __user *cp);
 709static int	stli_getportstruct(stliport_t __user *arg);
 710static int	stli_getbrdstruct(stlibrd_t __user *arg);
 711static void	*stli_memalloc(int len);
 712static stlibrd_t *stli_allocbrd(void);
 713
 714static void	stli_ecpinit(stlibrd_t *brdp);
 715static void	stli_ecpenable(stlibrd_t *brdp);
 716static void	stli_ecpdisable(stlibrd_t *brdp);
 717static char	*stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 718static void	stli_ecpreset(stlibrd_t *brdp);
 719static void	stli_ecpintr(stlibrd_t *brdp);
 720static void	stli_ecpeiinit(stlibrd_t *brdp);
 721static void	stli_ecpeienable(stlibrd_t *brdp);
 722static void	stli_ecpeidisable(stlibrd_t *brdp);
 723static char	*stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 724static void	stli_ecpeireset(stlibrd_t *brdp);
 725static void	stli_ecpmcenable(stlibrd_t *brdp);
 726static void	stli_ecpmcdisable(stlibrd_t *brdp);
 727static char	*stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 728static void	stli_ecpmcreset(stlibrd_t *brdp);
 729static void	stli_ecppciinit(stlibrd_t *brdp);
 730static char	*stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 731static void	stli_ecppcireset(stlibrd_t *brdp);
 732
 733static void	stli_onbinit(stlibrd_t *brdp);
 734static void	stli_onbenable(stlibrd_t *brdp);
 735static void	stli_onbdisable(stlibrd_t *brdp);
 736static char	*stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 737static void	stli_onbreset(stlibrd_t *brdp);
 738static void	stli_onbeinit(stlibrd_t *brdp);
 739static void	stli_onbeenable(stlibrd_t *brdp);
 740static void	stli_onbedisable(stlibrd_t *brdp);
 741static char	*stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 742static void	stli_onbereset(stlibrd_t *brdp);
 743static void	stli_bbyinit(stlibrd_t *brdp);
 744static char	*stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 745static void	stli_bbyreset(stlibrd_t *brdp);
 746static void	stli_stalinit(stlibrd_t *brdp);
 747static char	*stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
 748static void	stli_stalreset(stlibrd_t *brdp);
 749
 750static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
 751
 752static int	stli_initecp(stlibrd_t *brdp);
 753static int	stli_initonb(stlibrd_t *brdp);
 754static int	stli_eisamemprobe(stlibrd_t *brdp);
 755static int	stli_initports(stlibrd_t *brdp);
 756
 757#ifdef	CONFIG_PCI
 758static int	stli_initpcibrd(int brdtype, struct pci_dev *devp);
 759#endif
 760
 761/*****************************************************************************/
 762
 763/*
 764 *	Define the driver info for a user level shared memory device. This
 765 *	device will work sort of like the /dev/kmem device - except that it
 766 *	will give access to the shared memory on the Stallion intelligent
 767 *	board. This is also a very useful debugging tool.
 768 */
 769static struct file_operations	stli_fsiomem = {
 770	.owner		= THIS_MODULE,
 771	.read		= stli_memread,
 772	.write		= stli_memwrite,
 773	.ioctl		= stli_memioctl,
 774};
 775
 776/*****************************************************************************/
 777
 778/*
 779 *	Define a timer_list entry for our poll routine. The slave board
 780 *	is polled every so often to see if anything needs doing. This is
 781 *	much cheaper on host cpu than using interrupts. It turns out to
 782 *	not increase character latency by much either...
 783 */
 784static struct timer_list stli_timerlist = TIMER_INITIALIZER(stli_poll, 0, 0);
 785
 786static int	stli_timeron;
 787
 788/*
 789 *	Define the calculation for the timeout routine.
 790 */
 791#define	STLI_TIMEOUT	(jiffies + 1)
 792
 793/*****************************************************************************/
 794
 795static struct class_simple *istallion_class;
 796
 797#ifdef MODULE
 798
 799/*
 800 *	Loadable module initialization stuff.
 801 */
 802
 803static int __init istallion_module_init(void)
 804{
 805	unsigned long	flags;
 806
 807#ifdef DEBUG
 808	printk("init_module()\n");
 809#endif
 810
 811	save_flags(flags);
 812	cli();
 813	stli_init();
 814	restore_flags(flags);
 815
 816	return(0);
 817}
 818
 819/*****************************************************************************/
 820
 821static void __exit istallion_module_exit(void)
 822{
 823	stlibrd_t	*brdp;
 824	stliport_t	*portp;
 825	unsigned long	flags;
 826	int		i, j;
 827
 828#ifdef DEBUG
 829	printk("cleanup_module()\n");
 830#endif
 831
 832	printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
 833		stli_drvversion);
 834
 835	save_flags(flags);
 836	cli();
 837
 838/*
 839 *	Free up all allocated resources used by the ports. This includes
 840 *	memory and interrupts.
 841 */
 842	if (stli_timeron) {
 843		stli_timeron = 0;
 844		del_timer(&stli_timerlist);
 845	}
 846
 847	i = tty_unregister_driver(stli_serial);
 848	if (i) {
 849		printk("STALLION: failed to un-register tty driver, "
 850			"errno=%d\n", -i);
 851		restore_flags(flags);
 852		return;
 853	}
 854	put_tty_driver(stli_serial);
 855	for (i = 0; i < 4; i++) {
 856		devfs_remove("staliomem/%d", i);
 857		class_simple_device_remove(MKDEV(STL_SIOMEMMAJOR, i));
 858	}
 859	devfs_remove("staliomem");
 860	class_simple_destroy(istallion_class);
 861	if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
 862		printk("STALLION: failed to un-register serial memory device, "
 863			"errno=%d\n", -i);
 864	if (stli_tmpwritebuf != (char *) NULL)
 865		kfree(stli_tmpwritebuf);
 866	if (stli_txcookbuf != (char *) NULL)
 867		kfree(stli_txcookbuf);
 868
 869	for (i = 0; (i < stli_nrbrds); i++) {
 870		if ((brdp = stli_brds[i]) == (stlibrd_t *) NULL)
 871			continue;
 872		for (j = 0; (j < STL_MAXPORTS); j++) {
 873			portp = brdp->ports[j];
 874			if (portp != (stliport_t *) NULL) {
 875				if (portp->tty != (struct tty_struct *) NULL)
 876					tty_hangup(portp->tty);
 877				kfree(portp);
 878			}
 879		}
 880
 881		iounmap(brdp->membase);
 882		if (brdp->iosize > 0)
 883			release_region(brdp->iobase, brdp->iosize);
 884		kfree(brdp);
 885		stli_brds[i] = (stlibrd_t *) NULL;
 886	}
 887
 888	restore_flags(flags);
 889}
 890
 891module_init(istallion_module_init);
 892module_exit(istallion_module_exit);
 893
 894/*****************************************************************************/
 895
 896/*
 897 *	Check for any arguments passed in on the module load command line.
 898 */
 899
 900static void stli_argbrds(void)
 901{
 902	stlconf_t	conf;
 903	stlibrd_t	*brdp;
 904	int		nrargs, i;
 905
 906#ifdef DEBUG
 907	printk("stli_argbrds()\n");
 908#endif
 909
 910	nrargs = sizeof(stli_brdsp) / sizeof(char **);
 911
 912	for (i = stli_nrbrds; (i < nrargs); i++) {
 913		memset(&conf, 0, sizeof(conf));
 914		if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
 915			continue;
 916		if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
 917			continue;
 918		stli_nrbrds = i + 1;
 919		brdp->brdnr = i;
 920		brdp->brdtype = conf.brdtype;
 921		brdp->iobase = conf.ioaddr1;
 922		brdp->memaddr = conf.memaddr;
 923		stli_brdinit(brdp);
 924	}
 925}
 926
 927/*****************************************************************************/
 928
 929/*
 930 *	Convert an ascii string number into an unsigned long.
 931 */
 932
 933static unsigned long stli_atol(char *str)
 934{
 935	unsigned long	val;
 936	int		base, c;
 937	char		*sp;
 938
 939	val = 0;
 940	sp = str;
 941	if ((*sp == '0') && (*(sp+1) == 'x')) {
 942		base = 16;
 943		sp += 2;
 944	} else if (*sp == '0') {
 945		base = 8;
 946		sp++;
 947	} else {
 948		base = 10;
 949	}
 950
 951	for (; (*sp != 0); sp++) {
 952		c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
 953		if ((c < 0) || (c >= base)) {
 954			printk("STALLION: invalid argument %s\n", str);
 955			val = 0;
 956			break;
 957		}
 958		val = (val * base) + c;
 959	}
 960	return(val);
 961}
 962
 963/*****************************************************************************/
 964
 965/*
 966 *	Parse the supplied argument string, into the board conf struct.
 967 */
 968
 969static int stli_parsebrd(stlconf_t *confp, char **argp)
 970{
 971	char	*sp;
 972	int	nrbrdnames, i;
 973
 974#ifdef DEBUG
 975	printk("stli_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
 976#endif
 977
 978	if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
 979		return(0);
 980
 981	for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
 982		*sp = TOLOWER(*sp);
 983
 984	nrbrdnames = sizeof(stli_brdstr) / sizeof(stlibrdtype_t);
 985	for (i = 0; (i < nrbrdnames); i++) {
 986		if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
 987			break;
 988	}
 989	if (i >= nrbrdnames) {
 990		printk("STALLION: unknown board name, %s?\n", argp[0]);
 991		return(0);
 992	}
 993
 994	confp->brdtype = stli_brdstr[i].type;
 995	if ((argp[1] != (char *) NULL) && (*argp[1] != 0))
 996		confp->ioaddr1 = stli_atol(argp[1]);
 997	if ((argp[2] != (char *) NULL) && (*argp[2] != 0))
 998		confp->memaddr = stli_atol(argp[2]);
 999	return(1);
1000}
1001
1002#endif
1003
1004/*****************************************************************************/
1005
1006/*
1007 *	Local driver kernel malloc routine.
1008 */
1009
1010static void *stli_memalloc(int len)
1011{
1012	return((void *) kmalloc(len, GFP_KERNEL));
1013}
1014
1015/*****************************************************************************/
1016
1017static int stli_open(struct tty_struct *tty, struct file *filp)
1018{
1019	stlibrd_t	*brdp;
1020	stliport_t	*portp;
1021	unsigned int	minordev;
1022	int		brdnr, portnr, rc;
1023
1024#ifdef DEBUG
1025	printk("stli_open(tty=%x,filp=%x): device=%s\n", (int) tty,
1026		(int) filp, tty->name);
1027#endif
1028
1029	minordev = tty->index;
1030	brdnr = MINOR2BRD(minordev);
1031	if (brdnr >= stli_nrbrds)
1032		return(-ENODEV);
1033	brdp = stli_brds[brdnr];
1034	if (brdp == (stlibrd_t *) NULL)
1035		return(-ENODEV);
1036	if ((brdp->state & BST_STARTED) == 0)
1037		return(-ENODEV);
1038	portnr = MINOR2PORT(minordev);
1039	if ((portnr < 0) || (portnr > brdp->nrports))
1040		return(-ENODEV);
1041
1042	portp = brdp->ports[portnr];
1043	if (portp == (stliport_t *) NULL)
1044		return(-ENODEV);
1045	if (portp->devnr < 1)
1046		return(-ENODEV);
1047
1048
1049/*
1050 *	Check if this port is in the middle of closing. If so then wait
1051 *	until it is closed then return error status based on flag settings.
1052 *	The sleep here does not need interrupt protection since the wakeup
1053 *	for it is done with the same context.
1054 */
1055	if (portp->flags & ASYNC_CLOSING) {
1056		interruptible_sleep_on(&portp->close_wait);
1057		if (portp->flags & ASYNC_HUP_NOTIFY)
1058			return(-EAGAIN);
1059		return(-ERESTARTSYS);
1060	}
1061
1062/*
1063 *	On the first open of the device setup the port hardware, and
1064 *	initialize the per port data structure. Since initializing the port
1065 *	requires several commands to the board we will need to wait for any
1066 *	other open that is already initializing the port.
1067 */
1068	portp->tty = tty;
1069	tty->driver_data = portp;
1070	portp->refcount++;
1071
1072	wait_event_interruptible(portp->raw_wait,
1073			!test_bit(ST_INITIALIZING, &portp->state));
1074	if (signal_pending(current))
1075		return(-ERESTARTSYS);
1076
1077	if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1078		set_bit(ST_INITIALIZING, &portp->state);
1079		if ((rc = stli_initopen(brdp, portp)) >= 0) {
1080			portp->flags |= ASYNC_INITIALIZED;
1081			clear_bit(TTY_IO_ERROR, &tty->flags);
1082		}
1083		clear_bit(ST_INITIALIZING, &portp->state);
1084		wake_up_interruptible(&portp->raw_wait);
1085		if (rc < 0)
1086			return(rc);
1087	}
1088
1089/*
1090 *	Check if this port is in the middle of closing. If so then wait
1091 *	until it is closed then return error status, based on flag settings.
1092 *	The sleep here does not need interrupt protection since the wakeup
1093 *	for it is done with the same context.
1094 */
1095	if (portp->flags & ASYNC_CLOSING) {
1096		interruptible_sleep_on(&portp->close_wait);
1097		if (portp->flags & ASYNC_HUP_NOTIFY)
1098			return(-EAGAIN);
1099		return(-ERESTARTSYS);
1100	}
1101
1102/*
1103 *	Based on type of open being done check if it can overlap with any
1104 *	previous opens still in effect. If we are a normal serial device
1105 *	then also we might have to wait for carrier.
1106 */
1107	if (!(filp->f_flags & O_NONBLOCK)) {
1108		if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
1109			return(rc);
1110	}
1111	portp->flags |= ASYNC_NORMAL_ACTIVE;
1112	return(0);
1113}
1114
1115/*****************************************************************************/
1116
1117static void stli_close(struct tty_struct *tty, struct file *filp)
1118{
1119	stlibrd_t	*brdp;
1120	stliport_t	*portp;
1121	unsigned long	flags;
1122
1123#ifdef DEBUG
1124	printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1125#endif
1126
1127	portp = tty->driver_data;
1128	if (portp == (stliport_t *) NULL)
1129		return;
1130
1131	save_flags(flags);
1132	cli();
1133	if (tty_hung_up_p(filp)) {
1134		restore_flags(flags);
1135		return;
1136	}
1137	if ((tty->count == 1) && (portp->refcount != 1))
1138		portp->refcount = 1;
1139	if (portp->refcount-- > 1) {
1140		restore_flags(flags);
1141		return;
1142	}
1143
1144	portp->flags |= ASYNC_CLOSING;
1145
1146/*
1147 *	May want to wait for data to drain before closing. The BUSY flag
1148 *	keeps track of whether we are still transmitting or not. It is
1149 *	updated by messages from the slave - indicating when all chars
1150 *	really have drained.
1151 */
1152	if (tty == stli_txcooktty)
1153		stli_flushchars(tty);
1154	tty->closing = 1;
1155	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1156		tty_wait_until_sent(tty, portp->closing_wait);
1157
1158	portp->flags &= ~ASYNC_INITIALIZED;
1159	brdp = stli_brds[portp->brdnr];
1160	stli_rawclose(brdp, portp, 0, 0);
1161	if (tty->termios->c_cflag & HUPCL) {
1162		stli_mkasysigs(&portp->asig, 0, 0);
1163		if (test_bit(ST_CMDING, &portp->state))
1164			set_bit(ST_DOSIGS, &portp->state);
1165		else
1166			stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1167				sizeof(asysigs_t), 0);
1168	}
1169	clear_bit(ST_TXBUSY, &portp->state);
1170	clear_bit(ST_RXSTOP, &portp->state);
1171	set_bit(TTY_IO_ERROR, &tty->flags);
1172	if (tty->ldisc.flush_buffer)
1173		(tty->ldisc.flush_buffer)(tty);
1174	set_bit(ST_DOFLUSHRX, &portp->state);
1175	stli_flushbuffer(tty);
1176
1177	tty->closing = 0;
1178	portp->tty = (struct tty_struct *) NULL;
1179
1180	if (portp->openwaitcnt) {
1181		if (portp->close_delay)
1182			msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1183		wake_up_interruptible(&portp->open_wait);
1184	}
1185
1186	portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1187	wake_up_interruptible(&portp->close_wait);
1188	restore_flags(flags);
1189}
1190
1191/*****************************************************************************/
1192
1193/*
1194 *	Carry out first open operations on a port. This involves a number of
1195 *	commands to be sent to the slave. We need to open the port, set the
1196 *	notification events, set the initial port settings, get and set the
1197 *	initial signal values. We sleep and wait in between each one. But
1198 *	this still all happens pretty quickly.
1199 */
1200
1201static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1202{
1203	struct tty_struct	*tty;
1204	asynotify_t		nt;
1205	asyport_t		aport;
1206	int			rc;
1207
1208#ifdef DEBUG
1209	printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
1210#endif
1211
1212	if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1213		return(rc);
1214
1215	memset(&nt, 0, sizeof(asynotify_t));
1216	nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1217	nt.signal = SG_DCD;
1218	if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1219	    sizeof(asynotify_t), 0)) < 0)
1220		return(rc);
1221
1222	tty = portp->tty;
1223	if (tty == (struct tty_struct *) NULL)
1224		return(-ENODEV);
1225	stli_mkasyport(portp, &aport, tty->termios);
1226	if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1227	    sizeof(asyport_t), 0)) < 0)
1228		return(rc);
1229
1230	set_bit(ST_GETSIGS, &portp->state);
1231	if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1232	    sizeof(asysigs_t), 1)) < 0)
1233		return(rc);
1234	if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1235		portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1236	stli_mkasysigs(&portp->asig, 1, 1);
1237	if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1238	    sizeof(asysigs_t), 0)) < 0)
1239		return(rc);
1240
1241	return(0);
1242}
1243
1244/*****************************************************************************/
1245
1246/*
1247 *	Send an open message to the slave. This will sleep waiting for the
1248 *	acknowledgement, so must have user context. We need to co-ordinate
1249 *	with close events here, since we don't want open and close events
1250 *	to overlap.
1251 */
1252
1253static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1254{
1255	volatile cdkhdr_t	*hdrp;
1256	volatile cdkctrl_t	*cp;
1257	volatile unsigned char	*bits;
1258	unsigned long		flags;
1259	int			rc;
1260
1261#ifdef DEBUG
1262	printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1263		(int) brdp, (int) portp, (int) arg, wait);
1264#endif
1265
1266/*
1267 *	Send a message to the slave to open this port.
1268 */
1269	save_flags(flags);
1270	cli();
1271
1272/*
1273 *	Slave is already closing this port. This can happen if a hangup
1274 *	occurs on this port. So we must wait until it is complete. The
1275 *	order of opens and closes may not be preserved across shared
1276 *	memory, so we must wait until it is complete.
1277 */
1278	wait_event_interruptible(portp->raw_wait,
1279			!test_bit(ST_CLOSING, &portp->state));
1280	if (signal_pending(current)) {
1281		restore_flags(flags);
1282		return -ERESTARTSYS;
1283	}
1284
1285/*
1286 *	Everything is ready now, so write the open message into shared
1287 *	memory. Once the message is in set the service bits to say that
1288 *	this port wants service.
1289 */
1290	EBRDENABLE(brdp);
1291	cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1292	cp->openarg = arg;
1293	cp->open = 1;
1294	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1295	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1296		portp->portidx;
1297	*bits |= portp->portbit;
1298	EBRDDISABLE(brdp);
1299
1300	if (wait == 0) {
1301		restore_flags(flags);
1302		return(0);
1303	}
1304
1305/*
1306 *	Slave is in action, so now we must wait for the open acknowledgment
1307 *	to come back.
1308 */
1309	rc = 0;
1310	set_bit(ST_OPENING, &portp->state);
1311	wait_event_interruptible(portp->raw_wait,
1312			!test_bit(ST_OPENING, &portp->state));
1313	if (signal_pending(current))
1314		rc = -ERESTARTSYS;
1315	restore_flags(flags);
1316
1317	if ((rc == 0) && (portp->rc != 0))
1318		rc = -EIO;
1319	return(rc);
1320}
1321
1322/*****************************************************************************/
1323
1324/*
1325 *	Send a close message to the slave. Normally this will sleep waiting
1326 *	for the acknowledgement, but if wait parameter is 0 it will not. If
1327 *	wait is true then must have user context (to sleep).
1328 */
1329
1330static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1331{
1332	volatile cdkhdr_t	*hdrp;
1333	volatile cdkctrl_t	*cp;
1334	volatile unsigned char	*bits;
1335	unsigned long		flags;
1336	int			rc;
1337
1338#ifdef DEBUG
1339	printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1340		(int) brdp, (int) portp, (int) arg, wait);
1341#endif
1342
1343	save_flags(flags);
1344	cli();
1345
1346/*
1347 *	Slave is already closing this port. This can happen if a hangup
1348 *	occurs on this port.
1349 */
1350	if (wait) {
1351		wait_event_interruptible(portp->raw_wait,
1352				!test_bit(ST_CLOSING, &portp->state));
1353		if (signal_pending(current)) {
1354			restore_flags(flags);
1355			return -ERESTARTSYS;
1356		}
1357	}
1358
1359/*
1360 *	Write the close command into shared memory.
1361 */
1362	EBRDENABLE(brdp);
1363	cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1364	cp->closearg = arg;
1365	cp->close = 1;
1366	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1367	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1368		portp->portidx;
1369	*bits |= portp->portbit;
1370	EBRDDISABLE(brdp);
1371
1372	set_bit(ST_CLOSING, &portp->state);
1373	if (wait == 0) {
1374		restore_flags(flags);
1375		return(0);
1376	}
1377
1378/*
1379 *	Slave is in action, so now we must wait for the open acknowledgment
1380 *	to come back.
1381 */
1382	rc = 0;
1383	wait_event_interruptible(portp->raw_wait,
1384			!test_bit(ST_CLOSING, &portp->state));
1385	if (signal_pending(current))
1386		rc = -ERESTARTSYS;
1387	restore_flags(flags);
1388
1389	if ((rc == 0) && (portp->rc != 0))
1390		rc = -EIO;
1391	return(rc);
1392}
1393
1394/*****************************************************************************/
1395
1396/*
1397 *	Send a command to the slave and wait for the response. This must
1398 *	have user context (it sleeps). This routine is generic in that it
1399 *	can send any type of command. Its purpose is to wait for that command
1400 *	to complete (as opposed to initiating the command then returning).
1401 */
1402
1403static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1404{
1405	unsigned long	flags;
1406
1407#ifdef DEBUG
1408	printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1409		"copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1410		(int) arg, size, copyback);
1411#endif
1412
1413	save_flags(flags);
1414	cli();
1415	wait_event_interruptible(portp->raw_wait,
1416			!test_bit(ST_CMDING, &portp->state));
1417	if (signal_pending(current)) {
1418		restore_flags(flags);
1419		return -ERESTARTSYS;
1420	}
1421
1422	stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1423
1424	wait_event_interruptible(portp->raw_wait,
1425			!test_bit(ST_CMDING, &portp->state));
1426	if (signal_pending(current)) {
1427		restore_flags(flags);
1428		return -ERESTARTSYS;
1429	}
1430	restore_flags(flags);
1431
1432	if (portp->rc != 0)
1433		return(-EIO);
1434	return(0);
1435}
1436
1437/*****************************************************************************/
1438
1439/*
1440 *	Send the termios settings for this port to the slave. This sleeps
1441 *	waiting for the command to complete - so must have user context.
1442 */
1443
1444static int stli_setport(stliport_t *portp)
1445{
1446	stlibrd_t	*brdp;
1447	asyport_t	aport;
1448
1449#ifdef DEBUG
1450	printk("stli_setport(portp=%x)\n", (int) portp);
1451#endif
1452
1453	if (portp == (stliport_t *) NULL)
1454		return(-ENODEV);
1455	if (portp->tty == (struct tty_struct *) NULL)
1456		return(-ENODEV);
1457	if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
1458		return(-ENODEV);
1459	brdp = stli_brds[portp->brdnr];
1460	if (brdp == (stlibrd_t *) NULL)
1461		return(-ENODEV);
1462
1463	stli_mkasyport(portp, &aport, portp->tty->termios);
1464	return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1465}
1466
1467/*****************************************************************************/
1468
1469/*
1470 *	Possibly need to wait for carrier (DCD signal) to come high. Say
1471 *	maybe because if we are clocal then we don't need to wait...
1472 */
1473
1474static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1475{
1476	unsigned long	flags;
1477	int		rc, doclocal;
1478
1479#ifdef DEBUG
1480	printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n",
1481		(int) brdp, (int) portp, (int) filp);
1482#endif
1483
1484	rc = 0;
1485	doclocal = 0;
1486
1487	if (portp->tty->termios->c_cflag & CLOCAL)
1488		doclocal++;
1489
1490	save_flags(flags);
1491	cli();
1492	portp->openwaitcnt++;
1493	if (! tty_hung_up_p(filp))
1494		portp->refcount--;
1495
1496	for (;;) {
1497		stli_mkasysigs(&portp->asig, 1, 1);
1498		if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1499		    &portp->asig, sizeof(asysigs_t), 0)) < 0)
1500			break;
1501		if (tty_hung_up_p(filp) ||
1502		    ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1503			if (portp->flags & ASYNC_HUP_NOTIFY)
1504				rc = -EBUSY;
1505			else
1506				rc = -ERESTARTSYS;
1507			break;
1508		}
1509		if (((portp->flags & ASYNC_CLOSING) == 0) &&
1510		    (doclocal || (portp->sigs & TIOCM_CD))) {
1511			break;
1512		}
1513		if (signal_pending(current)) {
1514			rc = -ERESTARTSYS;
1515			break;
1516		}
1517		interruptible_sleep_on(&portp->open_wait);
1518	}
1519
1520	if (! tty_hung_up_p(filp))
1521		portp->refcount++;
1522	portp->openwaitcnt--;
1523	restore_flags(flags);
1524
1525	return(rc);
1526}
1527
1528/*****************************************************************************/
1529
1530/*
1531 *	Write routine. Take the data and put it in the shared memory ring
1532 *	queue. If port is not already sending chars then need to mark the
1533 *	service bits for this port.
1534 */
1535
1536static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
1537{
1538	volatile cdkasy_t	*ap;
1539	volatile cdkhdr_t	*hdrp;
1540	volatile unsigned char	*bits;
1541	unsigned char		*shbuf, *chbuf;
1542	stliport_t		*portp;
1543	stlibrd_t		*brdp;
1544	unsigned int		len, stlen, head, tail, size;
1545	unsigned long		flags;
1546
1547#ifdef DEBUG
1548	printk("stli_write(tty=%x,buf=%x,count=%d)\n",
1549		(int) tty, (int) buf, count);
1550#endif
1551
1552	if ((tty == (struct tty_struct *) NULL) ||
1553	    (stli_tmpwritebuf == (char *) NULL))
1554		return(0);
1555	if (tty == stli_txcooktty)
1556		stli_flushchars(tty);
1557	portp = tty->driver_data;
1558	if (portp == (stliport_t *) NULL)
1559		return(0);
1560	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1561		return(0);
1562	brdp = stli_brds[portp->brdnr];
1563	if (brdp == (stlibrd_t *) NULL)
1564		return(0);
1565	chbuf = (unsigned char *) buf;
1566
1567/*
1568 *	All data is now local, shove as much as possible into shared memory.
1569 */
1570	save_flags(flags);
1571	cli();
1572	EBRDENABLE(brdp);
1573	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1574	head = (unsigned int) ap->txq.head;
1575	tail = (unsigned int) ap->txq.tail;
1576	if (tail != ((unsigned int) ap->txq.tail))
1577		tail = (unsigned int) ap->txq.tail;
1578	size = portp->txsize;
1579	if (head >= tail) {
1580		len = size - (head - tail) - 1;
1581		stlen = size - head;
1582	} else {
1583		len = tail - head - 1;
1584		stlen = len;
1585	}
1586
1587	len = MIN(len, count);
1588	count = 0;
1589	shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1590
1591	while (len > 0) {
1592		stlen = MIN(len, stlen);
1593		memcpy((shbuf + head), chbuf, stlen);
1594		chbuf += stlen;
1595		len -= stlen;
1596		count += stlen;
1597		head += stlen;
1598		if (head >= size) {
1599			head = 0;
1600			stlen = tail;
1601		}
1602	}
1603
1604	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1605	ap->txq.head = head;
1606	if (test_bit(ST_TXBUSY, &portp->state)) {
1607		if (ap->changed.data & DT_TXEMPTY)
1608			ap->changed.data &= ~DT_TXEMPTY;
1609	}
1610	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1611	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1612		portp->portidx;
1613	*bits |= portp->portbit;
1614	set_bit(ST_TXBUSY, &portp->state);
1615	EBRDDISABLE(brdp);
1616
1617	restore_flags(flags);
1618
1619	return(count);
1620}
1621
1622/*****************************************************************************/
1623
1624/*
1625 *	Output a single character. We put it into a temporary local buffer
1626 *	(for speed) then write out that buffer when the flushchars routine
1627 *	is called. There is a safety catch here so that if some other port
1628 *	writes chars before the current buffer has been, then we write them
1629 *	first them do the new ports.
1630 */
1631
1632static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1633{
1634#ifdef DEBUG
1635	printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1636#endif
1637
1638	if (tty == (struct tty_struct *) NULL)
1639		return;
1640	if (tty != stli_txcooktty) {
1641		if (stli_txcooktty != (struct tty_struct *) NULL)
1642			stli_flushchars(stli_txcooktty);
1643		stli_txcooktty = tty;
1644	}
1645
1646	stli_txcookbuf[stli_txcooksize++] = ch;
1647}
1648
1649/*****************************************************************************/
1650
1651/*
1652 *	Transfer characters from the local TX cooking buffer to the board.
1653 *	We sort of ignore the tty that gets passed in here. We rely on the
1654 *	info stored with the TX cook buffer to tell us which port to flush
1655 *	the data on. In any case we clean out the TX cook buffer, for re-use
1656 *	by someone else.
1657 */
1658
1659static void stli_flushchars(struct tty_struct *tty)
1660{
1661	volatile cdkhdr_t	*hdrp;
1662	volatile unsigned char	*bits;
1663	volatile cdkasy_t	*ap;
1664	struct tty_struct	*cooktty;
1665	stliport_t		*portp;
1666	stlibrd_t		*brdp;
1667	unsigned int		len, stlen, head, tail, size, count, cooksize;
1668	unsigned char		*buf, *shbuf;
1669	unsigned long		flags;
1670
1671#ifdef DEBUG
1672	printk("stli_flushchars(tty=%x)\n", (int) tty);
1673#endif
1674
1675	cooksize = stli_txcooksize;
1676	cooktty = stli_txcooktty;
1677	stli_txcooksize = 0;
1678	stli_txcookrealsize = 0;
1679	stli_txcooktty = (struct tty_struct *) NULL;
1680
1681	if (tty == (struct tty_struct *) NULL)
1682		return;
1683	if (cooktty == (struct tty_struct *) NULL)
1684		return;
1685	if (tty != cooktty)
1686		tty = cooktty;
1687	if (cooksize == 0)
1688		return;
1689
1690	portp = tty->driver_data;
1691	if (portp == (stliport_t *) NULL)
1692		return;
1693	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1694		return;
1695	brdp = stli_brds[portp->brdnr];
1696	if (brdp == (stlibrd_t *) NULL)
1697		return;
1698
1699	save_flags(flags);
1700	cli();
1701	EBRDENABLE(brdp);
1702
1703	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1704	head = (unsigned int) ap->txq.head;
1705	tail = (unsigned int) ap->txq.tail;
1706	if (tail != ((unsigned int) ap->txq.tail))
1707		tail = (unsigned int) ap->txq.tail;
1708	size = portp->txsize;
1709	if (head >= tail) {
1710		len = size - (head - tail) - 1;
1711		stlen = size - head;
1712	} else {
1713		len = tail - head - 1;
1714		stlen = len;
1715	}
1716
1717	len = MIN(len, cooksize);
1718	count = 0;
1719	shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1720	buf = stli_txcookbuf;
1721
1722	while (len > 0) {
1723		stlen = MIN(len, stlen);
1724		memcpy((shbuf + head), buf, stlen);
1725		buf += stlen;
1726		len -= stlen;
1727		count += stlen;
1728		head += stlen;
1729		if (head >= size) {
1730			head = 0;
1731			stlen = tail;
1732		}
1733	}
1734
1735	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1736	ap->txq.head = head;
1737
1738	if (test_bit(ST_TXBUSY, &portp->state)) {
1739		if (ap->changed.data & DT_TXEMPTY)
1740			ap->changed.data &= ~DT_TXEMPTY;
1741	}
1742	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1743	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1744		portp->portidx;
1745	*bits |= portp->portbit;
1746	set_bit(ST_TXBUSY, &portp->state);
1747
1748	EBRDDISABLE(brdp);
1749	restore_flags(flags);
1750}
1751
1752/*****************************************************************************/
1753
1754static int stli_writeroom(struct tty_struct *tty)
1755{
1756	volatile cdkasyrq_t	*rp;
1757	stliport_t		*portp;
1758	stlibrd_t		*brdp;
1759	unsigned int		head, tail, len;
1760	unsigned long		flags;
1761
1762#ifdef DEBUG
1763	printk("stli_writeroom(tty=%x)\n", (int) tty);
1764#endif
1765
1766	if (tty == (struct tty_struct *) NULL)
1767		return(0);
1768	if (tty == stli_txcooktty) {
1769		if (stli_txcookrealsize != 0) {
1770			len = stli_txcookrealsize - stli_txcooksize;
1771			return(len);
1772		}
1773	}
1774
1775	portp = tty->driver_data;
1776	if (portp == (stliport_t *) NULL)
1777		return(0);
1778	if ((portp->brdnr < 

Large files files are truncated, but you can click here to view the full file