PageRenderTime 139ms CodeModel.GetById 17ms app.highlight 105ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/char/stallion.c

https://bitbucket.org/evzijst/gittest
C | 5197 lines | 3602 code | 765 blank | 830 comment | 795 complexity | 61f0d2e5e8c11ce6db5bedb3fbc920ec MD5 | raw file

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

   1/*****************************************************************************/
   2
   3/*
   4 *	stallion.c  -- stallion multiport serial driver.
   5 *
   6 *	Copyright (C) 1996-1999  Stallion Technologies
   7 *	Copyright (C) 1994-1996  Greg Ungerer.
   8 *
   9 *	This code is loosely based on the Linux serial driver, written by
  10 *	Linus Torvalds, Theodore T'so and others.
  11 *
  12 *	This program is free software; you can redistribute it and/or modify
  13 *	it under the terms of the GNU General Public License as published by
  14 *	the Free Software Foundation; either version 2 of the License, or
  15 *	(at your option) any later version.
  16 *
  17 *	This program is distributed in the hope that it will be useful,
  18 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 *	GNU General Public License for more details.
  21 *
  22 *	You should have received a copy of the GNU General Public License
  23 *	along with this program; if not, write to the Free Software
  24 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25 */
  26
  27/*****************************************************************************/
  28
  29#include <linux/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/cd1400.h>
  37#include <linux/sc26198.h>
  38#include <linux/comstats.h>
  39#include <linux/stallion.h>
  40#include <linux/ioport.h>
  41#include <linux/init.h>
  42#include <linux/smp_lock.h>
  43#include <linux/devfs_fs_kernel.h>
  44#include <linux/device.h>
  45#include <linux/delay.h>
  46
  47#include <asm/io.h>
  48#include <asm/uaccess.h>
  49
  50#ifdef CONFIG_PCI
  51#include <linux/pci.h>
  52#endif
  53
  54/*****************************************************************************/
  55
  56/*
  57 *	Define different board types. Use the standard Stallion "assigned"
  58 *	board numbers. Boards supported in this driver are abbreviated as
  59 *	EIO = EasyIO and ECH = EasyConnection 8/32.
  60 */
  61#define	BRD_EASYIO	20
  62#define	BRD_ECH		21
  63#define	BRD_ECHMC	22
  64#define	BRD_ECHPCI	26
  65#define	BRD_ECH64PCI	27
  66#define	BRD_EASYIOPCI	28
  67
  68/*
  69 *	Define a configuration structure to hold the board configuration.
  70 *	Need to set this up in the code (for now) with the boards that are
  71 *	to be configured into the system. This is what needs to be modified
  72 *	when adding/removing/modifying boards. Each line entry in the
  73 *	stl_brdconf[] array is a board. Each line contains io/irq/memory
  74 *	ranges for that board (as well as what type of board it is).
  75 *	Some examples:
  76 *		{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
  77 *	This line would configure an EasyIO board (4 or 8, no difference),
  78 *	at io address 2a0 and irq 10.
  79 *	Another example:
  80 *		{ BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
  81 *	This line will configure an EasyConnection 8/32 board at primary io
  82 *	address 2a8, secondary io address 280 and irq 12.
  83 *	Enter as many lines into this array as you want (only the first 4
  84 *	will actually be used!). Any combination of EasyIO and EasyConnection
  85 *	boards can be specified. EasyConnection 8/32 boards can share their
  86 *	secondary io addresses between each other.
  87 *
  88 *	NOTE: there is no need to put any entries in this table for PCI
  89 *	boards. They will be found automatically by the driver - provided
  90 *	PCI BIOS32 support is compiled into the kernel.
  91 */
  92
  93typedef struct {
  94	int		brdtype;
  95	int		ioaddr1;
  96	int		ioaddr2;
  97	unsigned long	memaddr;
  98	int		irq;
  99	int		irqtype;
 100} stlconf_t;
 101
 102static stlconf_t	stl_brdconf[] = {
 103	/*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
 104};
 105
 106static int	stl_nrbrds = sizeof(stl_brdconf) / sizeof(stlconf_t);
 107
 108/*****************************************************************************/
 109
 110/*
 111 *	Define some important driver characteristics. Device major numbers
 112 *	allocated as per Linux Device Registry.
 113 */
 114#ifndef	STL_SIOMEMMAJOR
 115#define	STL_SIOMEMMAJOR		28
 116#endif
 117#ifndef	STL_SERIALMAJOR
 118#define	STL_SERIALMAJOR		24
 119#endif
 120#ifndef	STL_CALLOUTMAJOR
 121#define	STL_CALLOUTMAJOR	25
 122#endif
 123
 124/*
 125 *	Set the TX buffer size. Bigger is better, but we don't want
 126 *	to chew too much memory with buffers!
 127 */
 128#define	STL_TXBUFLOW		512
 129#define	STL_TXBUFSIZE		4096
 130
 131/*****************************************************************************/
 132
 133/*
 134 *	Define our local driver identity first. Set up stuff to deal with
 135 *	all the local structures required by a serial tty driver.
 136 */
 137static char	*stl_drvtitle = "Stallion Multiport Serial Driver";
 138static char	*stl_drvname = "stallion";
 139static char	*stl_drvversion = "5.6.0";
 140
 141static struct tty_driver	*stl_serial;
 142
 143/*
 144 *	We will need to allocate a temporary write buffer for chars that
 145 *	come direct from user space. The problem is that a copy from user
 146 *	space might cause a page fault (typically on a system that is
 147 *	swapping!). All ports will share one buffer - since if the system
 148 *	is already swapping a shared buffer won't make things any worse.
 149 */
 150static char			*stl_tmpwritebuf;
 151static DECLARE_MUTEX(stl_tmpwritesem);
 152
 153/*
 154 *	Define a local default termios struct. All ports will be created
 155 *	with this termios initially. Basically all it defines is a raw port
 156 *	at 9600, 8 data bits, 1 stop bit.
 157 */
 158static struct termios		stl_deftermios = {
 159	.c_cflag	= (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
 160	.c_cc		= INIT_C_CC,
 161};
 162
 163/*
 164 *	Define global stats structures. Not used often, and can be
 165 *	re-used for each stats call.
 166 */
 167static comstats_t	stl_comstats;
 168static combrd_t		stl_brdstats;
 169static stlbrd_t		stl_dummybrd;
 170static stlport_t	stl_dummyport;
 171
 172/*
 173 *	Define global place to put buffer overflow characters.
 174 */
 175static char		stl_unwanted[SC26198_RXFIFOSIZE];
 176
 177/*****************************************************************************/
 178
 179static stlbrd_t		*stl_brds[STL_MAXBRDS];
 180
 181/*
 182 *	Per board state flags. Used with the state field of the board struct.
 183 *	Not really much here!
 184 */
 185#define	BRD_FOUND	0x1
 186
 187/*
 188 *	Define the port structure istate flags. These set of flags are
 189 *	modified at interrupt time - so setting and reseting them needs
 190 *	to be atomic. Use the bit clear/setting routines for this.
 191 */
 192#define	ASYI_TXBUSY	1
 193#define	ASYI_TXLOW	2
 194#define	ASYI_DCDCHANGE	3
 195#define	ASYI_TXFLOWED	4
 196
 197/*
 198 *	Define an array of board names as printable strings. Handy for
 199 *	referencing boards when printing trace and stuff.
 200 */
 201static char	*stl_brdnames[] = {
 202	(char *) NULL,
 203	(char *) NULL,
 204	(char *) NULL,
 205	(char *) NULL,
 206	(char *) NULL,
 207	(char *) NULL,
 208	(char *) NULL,
 209	(char *) NULL,
 210	(char *) NULL,
 211	(char *) NULL,
 212	(char *) NULL,
 213	(char *) NULL,
 214	(char *) NULL,
 215	(char *) NULL,
 216	(char *) NULL,
 217	(char *) NULL,
 218	(char *) NULL,
 219	(char *) NULL,
 220	(char *) NULL,
 221	(char *) NULL,
 222	"EasyIO",
 223	"EC8/32-AT",
 224	"EC8/32-MC",
 225	(char *) NULL,
 226	(char *) NULL,
 227	(char *) NULL,
 228	"EC8/32-PCI",
 229	"EC8/64-PCI",
 230	"EasyIO-PCI",
 231};
 232
 233/*****************************************************************************/
 234
 235/*
 236 *	Define some string labels for arguments passed from the module
 237 *	load line. These allow for easy board definitions, and easy
 238 *	modification of the io, memory and irq resoucres.
 239 */
 240static int	stl_nargs = 0;
 241static char	*board0[4];
 242static char	*board1[4];
 243static char	*board2[4];
 244static char	*board3[4];
 245
 246static char	**stl_brdsp[] = {
 247	(char **) &board0,
 248	(char **) &board1,
 249	(char **) &board2,
 250	(char **) &board3
 251};
 252
 253/*
 254 *	Define a set of common board names, and types. This is used to
 255 *	parse any module arguments.
 256 */
 257
 258typedef struct stlbrdtype {
 259	char	*name;
 260	int	type;
 261} stlbrdtype_t;
 262
 263static stlbrdtype_t	stl_brdstr[] = {
 264	{ "easyio", BRD_EASYIO },
 265	{ "eio", BRD_EASYIO },
 266	{ "20", BRD_EASYIO },
 267	{ "ec8/32", BRD_ECH },
 268	{ "ec8/32-at", BRD_ECH },
 269	{ "ec8/32-isa", BRD_ECH },
 270	{ "ech", BRD_ECH },
 271	{ "echat", BRD_ECH },
 272	{ "21", BRD_ECH },
 273	{ "ec8/32-mc", BRD_ECHMC },
 274	{ "ec8/32-mca", BRD_ECHMC },
 275	{ "echmc", BRD_ECHMC },
 276	{ "echmca", BRD_ECHMC },
 277	{ "22", BRD_ECHMC },
 278	{ "ec8/32-pc", BRD_ECHPCI },
 279	{ "ec8/32-pci", BRD_ECHPCI },
 280	{ "26", BRD_ECHPCI },
 281	{ "ec8/64-pc", BRD_ECH64PCI },
 282	{ "ec8/64-pci", BRD_ECH64PCI },
 283	{ "ech-pci", BRD_ECH64PCI },
 284	{ "echpci", BRD_ECH64PCI },
 285	{ "echpc", BRD_ECH64PCI },
 286	{ "27", BRD_ECH64PCI },
 287	{ "easyio-pc", BRD_EASYIOPCI },
 288	{ "easyio-pci", BRD_EASYIOPCI },
 289	{ "eio-pci", BRD_EASYIOPCI },
 290	{ "eiopci", BRD_EASYIOPCI },
 291	{ "28", BRD_EASYIOPCI },
 292};
 293
 294/*
 295 *	Define the module agruments.
 296 */
 297MODULE_AUTHOR("Greg Ungerer");
 298MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
 299MODULE_LICENSE("GPL");
 300
 301module_param_array(board0, charp, &stl_nargs, 0);
 302MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
 303module_param_array(board1, charp, &stl_nargs, 0);
 304MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
 305module_param_array(board2, charp, &stl_nargs, 0);
 306MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
 307module_param_array(board3, charp, &stl_nargs, 0);
 308MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
 309
 310/*****************************************************************************/
 311
 312/*
 313 *	Hardware ID bits for the EasyIO and ECH boards. These defines apply
 314 *	to the directly accessible io ports of these boards (not the uarts -
 315 *	they are in cd1400.h and sc26198.h).
 316 */
 317#define	EIO_8PORTRS	0x04
 318#define	EIO_4PORTRS	0x05
 319#define	EIO_8PORTDI	0x00
 320#define	EIO_8PORTM	0x06
 321#define	EIO_MK3		0x03
 322#define	EIO_IDBITMASK	0x07
 323
 324#define	EIO_BRDMASK	0xf0
 325#define	ID_BRD4		0x10
 326#define	ID_BRD8		0x20
 327#define	ID_BRD16	0x30
 328
 329#define	EIO_INTRPEND	0x08
 330#define	EIO_INTEDGE	0x00
 331#define	EIO_INTLEVEL	0x08
 332#define	EIO_0WS		0x10
 333
 334#define	ECH_ID		0xa0
 335#define	ECH_IDBITMASK	0xe0
 336#define	ECH_BRDENABLE	0x08
 337#define	ECH_BRDDISABLE	0x00
 338#define	ECH_INTENABLE	0x01
 339#define	ECH_INTDISABLE	0x00
 340#define	ECH_INTLEVEL	0x02
 341#define	ECH_INTEDGE	0x00
 342#define	ECH_INTRPEND	0x01
 343#define	ECH_BRDRESET	0x01
 344
 345#define	ECHMC_INTENABLE	0x01
 346#define	ECHMC_BRDRESET	0x02
 347
 348#define	ECH_PNLSTATUS	2
 349#define	ECH_PNL16PORT	0x20
 350#define	ECH_PNLIDMASK	0x07
 351#define	ECH_PNLXPID	0x40
 352#define	ECH_PNLINTRPEND	0x80
 353
 354#define	ECH_ADDR2MASK	0x1e0
 355
 356/*
 357 *	Define the vector mapping bits for the programmable interrupt board
 358 *	hardware. These bits encode the interrupt for the board to use - it
 359 *	is software selectable (except the EIO-8M).
 360 */
 361static unsigned char	stl_vecmap[] = {
 362	0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
 363	0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
 364};
 365
 366/*
 367 *	Set up enable and disable macros for the ECH boards. They require
 368 *	the secondary io address space to be activated and deactivated.
 369 *	This way all ECH boards can share their secondary io region.
 370 *	If this is an ECH-PCI board then also need to set the page pointer
 371 *	to point to the correct page.
 372 */
 373#define	BRDENABLE(brdnr,pagenr)						\
 374	if (stl_brds[(brdnr)]->brdtype == BRD_ECH)			\
 375		outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE),	\
 376			stl_brds[(brdnr)]->ioctrl);			\
 377	else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)		\
 378		outb((pagenr), stl_brds[(brdnr)]->ioctrl);
 379
 380#define	BRDDISABLE(brdnr)						\
 381	if (stl_brds[(brdnr)]->brdtype == BRD_ECH)			\
 382		outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE),	\
 383			stl_brds[(brdnr)]->ioctrl);
 384
 385#define	STL_CD1400MAXBAUD	230400
 386#define	STL_SC26198MAXBAUD	460800
 387
 388#define	STL_BAUDBASE		115200
 389#define	STL_CLOSEDELAY		(5 * HZ / 10)
 390
 391/*****************************************************************************/
 392
 393#ifdef CONFIG_PCI
 394
 395/*
 396 *	Define the Stallion PCI vendor and device IDs.
 397 */
 398#ifndef	PCI_VENDOR_ID_STALLION
 399#define	PCI_VENDOR_ID_STALLION		0x124d
 400#endif
 401#ifndef PCI_DEVICE_ID_ECHPCI832
 402#define	PCI_DEVICE_ID_ECHPCI832		0x0000
 403#endif
 404#ifndef PCI_DEVICE_ID_ECHPCI864
 405#define	PCI_DEVICE_ID_ECHPCI864		0x0002
 406#endif
 407#ifndef PCI_DEVICE_ID_EIOPCI
 408#define	PCI_DEVICE_ID_EIOPCI		0x0003
 409#endif
 410
 411/*
 412 *	Define structure to hold all Stallion PCI boards.
 413 */
 414typedef struct stlpcibrd {
 415	unsigned short		vendid;
 416	unsigned short		devid;
 417	int			brdtype;
 418} stlpcibrd_t;
 419
 420static stlpcibrd_t	stl_pcibrds[] = {
 421	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
 422	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
 423	{ PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
 424	{ PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
 425};
 426
 427static int	stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
 428
 429#endif
 430
 431/*****************************************************************************/
 432
 433/*
 434 *	Define macros to extract a brd/port number from a minor number.
 435 */
 436#define	MINOR2BRD(min)		(((min) & 0xc0) >> 6)
 437#define	MINOR2PORT(min)		((min) & 0x3f)
 438
 439/*
 440 *	Define a baud rate table that converts termios baud rate selector
 441 *	into the actual baud rate value. All baud rate calculations are
 442 *	based on the actual baud rate required.
 443 */
 444static unsigned int	stl_baudrates[] = {
 445	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 446	9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
 447};
 448
 449/*
 450 *	Define some handy local macros...
 451 */
 452#undef	MIN
 453#define	MIN(a,b)	(((a) <= (b)) ? (a) : (b))
 454
 455#undef	TOLOWER
 456#define	TOLOWER(x)	((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
 457
 458/*****************************************************************************/
 459
 460/*
 461 *	Declare all those functions in this driver!
 462 */
 463
 464static void	stl_argbrds(void);
 465static int	stl_parsebrd(stlconf_t *confp, char **argp);
 466
 467static unsigned long stl_atol(char *str);
 468
 469int		stl_init(void);
 470static int	stl_open(struct tty_struct *tty, struct file *filp);
 471static void	stl_close(struct tty_struct *tty, struct file *filp);
 472static int	stl_write(struct tty_struct *tty, const unsigned char *buf, int count);
 473static void	stl_putchar(struct tty_struct *tty, unsigned char ch);
 474static void	stl_flushchars(struct tty_struct *tty);
 475static int	stl_writeroom(struct tty_struct *tty);
 476static int	stl_charsinbuffer(struct tty_struct *tty);
 477static int	stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
 478static void	stl_settermios(struct tty_struct *tty, struct termios *old);
 479static void	stl_throttle(struct tty_struct *tty);
 480static void	stl_unthrottle(struct tty_struct *tty);
 481static void	stl_stop(struct tty_struct *tty);
 482static void	stl_start(struct tty_struct *tty);
 483static void	stl_flushbuffer(struct tty_struct *tty);
 484static void	stl_breakctl(struct tty_struct *tty, int state);
 485static void	stl_waituntilsent(struct tty_struct *tty, int timeout);
 486static void	stl_sendxchar(struct tty_struct *tty, char ch);
 487static void	stl_hangup(struct tty_struct *tty);
 488static int	stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
 489static int	stl_portinfo(stlport_t *portp, int portnr, char *pos);
 490static int	stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
 491
 492static int	stl_brdinit(stlbrd_t *brdp);
 493static int	stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
 494static int	stl_getserial(stlport_t *portp, struct serial_struct __user *sp);
 495static int	stl_setserial(stlport_t *portp, struct serial_struct __user *sp);
 496static int	stl_getbrdstats(combrd_t __user *bp);
 497static int	stl_getportstats(stlport_t *portp, comstats_t __user *cp);
 498static int	stl_clrportstats(stlport_t *portp, comstats_t __user *cp);
 499static int	stl_getportstruct(stlport_t __user *arg);
 500static int	stl_getbrdstruct(stlbrd_t __user *arg);
 501static int	stl_waitcarrier(stlport_t *portp, struct file *filp);
 502static int	stl_eiointr(stlbrd_t *brdp);
 503static int	stl_echatintr(stlbrd_t *brdp);
 504static int	stl_echmcaintr(stlbrd_t *brdp);
 505static int	stl_echpciintr(stlbrd_t *brdp);
 506static int	stl_echpci64intr(stlbrd_t *brdp);
 507static void	stl_offintr(void *private);
 508static void	*stl_memalloc(int len);
 509static stlbrd_t *stl_allocbrd(void);
 510static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
 511
 512static inline int	stl_initbrds(void);
 513static inline int	stl_initeio(stlbrd_t *brdp);
 514static inline int	stl_initech(stlbrd_t *brdp);
 515static inline int	stl_getbrdnr(void);
 516
 517#ifdef	CONFIG_PCI
 518static inline int	stl_findpcibrds(void);
 519static inline int	stl_initpcibrd(int brdtype, struct pci_dev *devp);
 520#endif
 521
 522/*
 523 *	CD1400 uart specific handling functions.
 524 */
 525static void	stl_cd1400setreg(stlport_t *portp, int regnr, int value);
 526static int	stl_cd1400getreg(stlport_t *portp, int regnr);
 527static int	stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
 528static int	stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
 529static void	stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
 530static void	stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
 531static int	stl_cd1400getsignals(stlport_t *portp);
 532static void	stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
 533static void	stl_cd1400ccrwait(stlport_t *portp);
 534static void	stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
 535static void	stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
 536static void	stl_cd1400disableintrs(stlport_t *portp);
 537static void	stl_cd1400sendbreak(stlport_t *portp, int len);
 538static void	stl_cd1400flowctrl(stlport_t *portp, int state);
 539static void	stl_cd1400sendflow(stlport_t *portp, int state);
 540static void	stl_cd1400flush(stlport_t *portp);
 541static int	stl_cd1400datastate(stlport_t *portp);
 542static void	stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
 543static void	stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
 544static void	stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
 545static void	stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
 546static void	stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
 547
 548static inline int	stl_cd1400breakisr(stlport_t *portp, int ioaddr);
 549
 550/*
 551 *	SC26198 uart specific handling functions.
 552 */
 553static void	stl_sc26198setreg(stlport_t *portp, int regnr, int value);
 554static int	stl_sc26198getreg(stlport_t *portp, int regnr);
 555static int	stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
 556static int	stl_sc26198getglobreg(stlport_t *portp, int regnr);
 557static int	stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
 558static void	stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
 559static void	stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
 560static int	stl_sc26198getsignals(stlport_t *portp);
 561static void	stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
 562static void	stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
 563static void	stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
 564static void	stl_sc26198disableintrs(stlport_t *portp);
 565static void	stl_sc26198sendbreak(stlport_t *portp, int len);
 566static void	stl_sc26198flowctrl(stlport_t *portp, int state);
 567static void	stl_sc26198sendflow(stlport_t *portp, int state);
 568static void	stl_sc26198flush(stlport_t *portp);
 569static int	stl_sc26198datastate(stlport_t *portp);
 570static void	stl_sc26198wait(stlport_t *portp);
 571static void	stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
 572static void	stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
 573static void	stl_sc26198txisr(stlport_t *port);
 574static void	stl_sc26198rxisr(stlport_t *port, unsigned int iack);
 575static void	stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
 576static void	stl_sc26198rxbadchars(stlport_t *portp);
 577static void	stl_sc26198otherisr(stlport_t *port, unsigned int iack);
 578
 579/*****************************************************************************/
 580
 581/*
 582 *	Generic UART support structure.
 583 */
 584typedef struct uart {
 585	int	(*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
 586	void	(*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
 587	void	(*setport)(stlport_t *portp, struct termios *tiosp);
 588	int	(*getsignals)(stlport_t *portp);
 589	void	(*setsignals)(stlport_t *portp, int dtr, int rts);
 590	void	(*enablerxtx)(stlport_t *portp, int rx, int tx);
 591	void	(*startrxtx)(stlport_t *portp, int rx, int tx);
 592	void	(*disableintrs)(stlport_t *portp);
 593	void	(*sendbreak)(stlport_t *portp, int len);
 594	void	(*flowctrl)(stlport_t *portp, int state);
 595	void	(*sendflow)(stlport_t *portp, int state);
 596	void	(*flush)(stlport_t *portp);
 597	int	(*datastate)(stlport_t *portp);
 598	void	(*intr)(stlpanel_t *panelp, unsigned int iobase);
 599} uart_t;
 600
 601/*
 602 *	Define some macros to make calling these functions nice and clean.
 603 */
 604#define	stl_panelinit		(* ((uart_t *) panelp->uartp)->panelinit)
 605#define	stl_portinit		(* ((uart_t *) portp->uartp)->portinit)
 606#define	stl_setport		(* ((uart_t *) portp->uartp)->setport)
 607#define	stl_getsignals		(* ((uart_t *) portp->uartp)->getsignals)
 608#define	stl_setsignals		(* ((uart_t *) portp->uartp)->setsignals)
 609#define	stl_enablerxtx		(* ((uart_t *) portp->uartp)->enablerxtx)
 610#define	stl_startrxtx		(* ((uart_t *) portp->uartp)->startrxtx)
 611#define	stl_disableintrs	(* ((uart_t *) portp->uartp)->disableintrs)
 612#define	stl_sendbreak		(* ((uart_t *) portp->uartp)->sendbreak)
 613#define	stl_flowctrl		(* ((uart_t *) portp->uartp)->flowctrl)
 614#define	stl_sendflow		(* ((uart_t *) portp->uartp)->sendflow)
 615#define	stl_flush		(* ((uart_t *) portp->uartp)->flush)
 616#define	stl_datastate		(* ((uart_t *) portp->uartp)->datastate)
 617
 618/*****************************************************************************/
 619
 620/*
 621 *	CD1400 UART specific data initialization.
 622 */
 623static uart_t stl_cd1400uart = {
 624	stl_cd1400panelinit,
 625	stl_cd1400portinit,
 626	stl_cd1400setport,
 627	stl_cd1400getsignals,
 628	stl_cd1400setsignals,
 629	stl_cd1400enablerxtx,
 630	stl_cd1400startrxtx,
 631	stl_cd1400disableintrs,
 632	stl_cd1400sendbreak,
 633	stl_cd1400flowctrl,
 634	stl_cd1400sendflow,
 635	stl_cd1400flush,
 636	stl_cd1400datastate,
 637	stl_cd1400eiointr
 638};
 639
 640/*
 641 *	Define the offsets within the register bank of a cd1400 based panel.
 642 *	These io address offsets are common to the EasyIO board as well.
 643 */
 644#define	EREG_ADDR	0
 645#define	EREG_DATA	4
 646#define	EREG_RXACK	5
 647#define	EREG_TXACK	6
 648#define	EREG_MDACK	7
 649
 650#define	EREG_BANKSIZE	8
 651
 652#define	CD1400_CLK	25000000
 653#define	CD1400_CLK8M	20000000
 654
 655/*
 656 *	Define the cd1400 baud rate clocks. These are used when calculating
 657 *	what clock and divisor to use for the required baud rate. Also
 658 *	define the maximum baud rate allowed, and the default base baud.
 659 */
 660static int	stl_cd1400clkdivs[] = {
 661	CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
 662};
 663
 664/*****************************************************************************/
 665
 666/*
 667 *	SC26198 UART specific data initization.
 668 */
 669static uart_t stl_sc26198uart = {
 670	stl_sc26198panelinit,
 671	stl_sc26198portinit,
 672	stl_sc26198setport,
 673	stl_sc26198getsignals,
 674	stl_sc26198setsignals,
 675	stl_sc26198enablerxtx,
 676	stl_sc26198startrxtx,
 677	stl_sc26198disableintrs,
 678	stl_sc26198sendbreak,
 679	stl_sc26198flowctrl,
 680	stl_sc26198sendflow,
 681	stl_sc26198flush,
 682	stl_sc26198datastate,
 683	stl_sc26198intr
 684};
 685
 686/*
 687 *	Define the offsets within the register bank of a sc26198 based panel.
 688 */
 689#define	XP_DATA		0
 690#define	XP_ADDR		1
 691#define	XP_MODID	2
 692#define	XP_STATUS	2
 693#define	XP_IACK		3
 694
 695#define	XP_BANKSIZE	4
 696
 697/*
 698 *	Define the sc26198 baud rate table. Offsets within the table
 699 *	represent the actual baud rate selector of sc26198 registers.
 700 */
 701static unsigned int	sc26198_baudtable[] = {
 702	50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
 703	4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
 704	230400, 460800, 921600
 705};
 706
 707#define	SC26198_NRBAUDS		(sizeof(sc26198_baudtable) / sizeof(unsigned int))
 708
 709/*****************************************************************************/
 710
 711/*
 712 *	Define the driver info for a user level control device. Used mainly
 713 *	to get at port stats - only not using the port device itself.
 714 */
 715static struct file_operations	stl_fsiomem = {
 716	.owner		= THIS_MODULE,
 717	.ioctl		= stl_memioctl,
 718};
 719
 720/*****************************************************************************/
 721
 722static struct class_simple *stallion_class;
 723
 724/*
 725 *	Loadable module initialization stuff.
 726 */
 727
 728static int __init stallion_module_init(void)
 729{
 730	unsigned long	flags;
 731
 732#ifdef DEBUG
 733	printk("init_module()\n");
 734#endif
 735
 736	save_flags(flags);
 737	cli();
 738	stl_init();
 739	restore_flags(flags);
 740
 741	return(0);
 742}
 743
 744/*****************************************************************************/
 745
 746static void __exit stallion_module_exit(void)
 747{
 748	stlbrd_t	*brdp;
 749	stlpanel_t	*panelp;
 750	stlport_t	*portp;
 751	unsigned long	flags;
 752	int		i, j, k;
 753
 754#ifdef DEBUG
 755	printk("cleanup_module()\n");
 756#endif
 757
 758	printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
 759		stl_drvversion);
 760
 761	save_flags(flags);
 762	cli();
 763
 764/*
 765 *	Free up all allocated resources used by the ports. This includes
 766 *	memory and interrupts. As part of this process we will also do
 767 *	a hangup on every open port - to try to flush out any processes
 768 *	hanging onto ports.
 769 */
 770	i = tty_unregister_driver(stl_serial);
 771	put_tty_driver(stl_serial);
 772	if (i) {
 773		printk("STALLION: failed to un-register tty driver, "
 774			"errno=%d\n", -i);
 775		restore_flags(flags);
 776		return;
 777	}
 778	for (i = 0; i < 4; i++) {
 779		devfs_remove("staliomem/%d", i);
 780		class_simple_device_remove(MKDEV(STL_SIOMEMMAJOR, i));
 781	}
 782	devfs_remove("staliomem");
 783	if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
 784		printk("STALLION: failed to un-register serial memory device, "
 785			"errno=%d\n", -i);
 786	class_simple_destroy(stallion_class);
 787
 788	if (stl_tmpwritebuf != (char *) NULL)
 789		kfree(stl_tmpwritebuf);
 790
 791	for (i = 0; (i < stl_nrbrds); i++) {
 792		if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
 793			continue;
 794
 795		free_irq(brdp->irq, brdp);
 796
 797		for (j = 0; (j < STL_MAXPANELS); j++) {
 798			panelp = brdp->panels[j];
 799			if (panelp == (stlpanel_t *) NULL)
 800				continue;
 801			for (k = 0; (k < STL_PORTSPERPANEL); k++) {
 802				portp = panelp->ports[k];
 803				if (portp == (stlport_t *) NULL)
 804					continue;
 805				if (portp->tty != (struct tty_struct *) NULL)
 806					stl_hangup(portp->tty);
 807				if (portp->tx.buf != (char *) NULL)
 808					kfree(portp->tx.buf);
 809				kfree(portp);
 810			}
 811			kfree(panelp);
 812		}
 813
 814		release_region(brdp->ioaddr1, brdp->iosize1);
 815		if (brdp->iosize2 > 0)
 816			release_region(brdp->ioaddr2, brdp->iosize2);
 817
 818		kfree(brdp);
 819		stl_brds[i] = (stlbrd_t *) NULL;
 820	}
 821
 822	restore_flags(flags);
 823}
 824
 825module_init(stallion_module_init);
 826module_exit(stallion_module_exit);
 827
 828/*****************************************************************************/
 829
 830/*
 831 *	Check for any arguments passed in on the module load command line.
 832 */
 833
 834static void stl_argbrds(void)
 835{
 836	stlconf_t	conf;
 837	stlbrd_t	*brdp;
 838	int		i;
 839
 840#ifdef DEBUG
 841	printk("stl_argbrds()\n");
 842#endif
 843
 844	for (i = stl_nrbrds; (i < stl_nargs); i++) {
 845		memset(&conf, 0, sizeof(conf));
 846		if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
 847			continue;
 848		if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
 849			continue;
 850		stl_nrbrds = i + 1;
 851		brdp->brdnr = i;
 852		brdp->brdtype = conf.brdtype;
 853		brdp->ioaddr1 = conf.ioaddr1;
 854		brdp->ioaddr2 = conf.ioaddr2;
 855		brdp->irq = conf.irq;
 856		brdp->irqtype = conf.irqtype;
 857		stl_brdinit(brdp);
 858	}
 859}
 860
 861/*****************************************************************************/
 862
 863/*
 864 *	Convert an ascii string number into an unsigned long.
 865 */
 866
 867static unsigned long stl_atol(char *str)
 868{
 869	unsigned long	val;
 870	int		base, c;
 871	char		*sp;
 872
 873	val = 0;
 874	sp = str;
 875	if ((*sp == '0') && (*(sp+1) == 'x')) {
 876		base = 16;
 877		sp += 2;
 878	} else if (*sp == '0') {
 879		base = 8;
 880		sp++;
 881	} else {
 882		base = 10;
 883	}
 884
 885	for (; (*sp != 0); sp++) {
 886		c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
 887		if ((c < 0) || (c >= base)) {
 888			printk("STALLION: invalid argument %s\n", str);
 889			val = 0;
 890			break;
 891		}
 892		val = (val * base) + c;
 893	}
 894	return(val);
 895}
 896
 897/*****************************************************************************/
 898
 899/*
 900 *	Parse the supplied argument string, into the board conf struct.
 901 */
 902
 903static int stl_parsebrd(stlconf_t *confp, char **argp)
 904{
 905	char	*sp;
 906	int	nrbrdnames, i;
 907
 908#ifdef DEBUG
 909	printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
 910#endif
 911
 912	if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
 913		return(0);
 914
 915	for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
 916		*sp = TOLOWER(*sp);
 917
 918	nrbrdnames = sizeof(stl_brdstr) / sizeof(stlbrdtype_t);
 919	for (i = 0; (i < nrbrdnames); i++) {
 920		if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
 921			break;
 922	}
 923	if (i >= nrbrdnames) {
 924		printk("STALLION: unknown board name, %s?\n", argp[0]);
 925		return(0);
 926	}
 927
 928	confp->brdtype = stl_brdstr[i].type;
 929
 930	i = 1;
 931	if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
 932		confp->ioaddr1 = stl_atol(argp[i]);
 933	i++;
 934	if (confp->brdtype == BRD_ECH) {
 935		if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
 936			confp->ioaddr2 = stl_atol(argp[i]);
 937		i++;
 938	}
 939	if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
 940		confp->irq = stl_atol(argp[i]);
 941	return(1);
 942}
 943
 944/*****************************************************************************/
 945
 946/*
 947 *	Local driver kernel memory allocation routine.
 948 */
 949
 950static void *stl_memalloc(int len)
 951{
 952	return((void *) kmalloc(len, GFP_KERNEL));
 953}
 954
 955/*****************************************************************************/
 956
 957/*
 958 *	Allocate a new board structure. Fill out the basic info in it.
 959 */
 960
 961static stlbrd_t *stl_allocbrd(void)
 962{
 963	stlbrd_t	*brdp;
 964
 965	brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
 966	if (brdp == (stlbrd_t *) NULL) {
 967		printk("STALLION: failed to allocate memory (size=%d)\n",
 968			sizeof(stlbrd_t));
 969		return((stlbrd_t *) NULL);
 970	}
 971
 972	memset(brdp, 0, sizeof(stlbrd_t));
 973	brdp->magic = STL_BOARDMAGIC;
 974	return(brdp);
 975}
 976
 977/*****************************************************************************/
 978
 979static int stl_open(struct tty_struct *tty, struct file *filp)
 980{
 981	stlport_t	*portp;
 982	stlbrd_t	*brdp;
 983	unsigned int	minordev;
 984	int		brdnr, panelnr, portnr, rc;
 985
 986#ifdef DEBUG
 987	printk("stl_open(tty=%x,filp=%x): device=%s\n", (int) tty,
 988		(int) filp, tty->name);
 989#endif
 990
 991	minordev = tty->index;
 992	brdnr = MINOR2BRD(minordev);
 993	if (brdnr >= stl_nrbrds)
 994		return(-ENODEV);
 995	brdp = stl_brds[brdnr];
 996	if (brdp == (stlbrd_t *) NULL)
 997		return(-ENODEV);
 998	minordev = MINOR2PORT(minordev);
 999	for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
1000		if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
1001			break;
1002		if (minordev < brdp->panels[panelnr]->nrports) {
1003			portnr = minordev;
1004			break;
1005		}
1006		minordev -= brdp->panels[panelnr]->nrports;
1007	}
1008	if (portnr < 0)
1009		return(-ENODEV);
1010
1011	portp = brdp->panels[panelnr]->ports[portnr];
1012	if (portp == (stlport_t *) NULL)
1013		return(-ENODEV);
1014
1015/*
1016 *	On the first open of the device setup the port hardware, and
1017 *	initialize the per port data structure.
1018 */
1019	portp->tty = tty;
1020	tty->driver_data = portp;
1021	portp->refcount++;
1022
1023	if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1024		if (portp->tx.buf == (char *) NULL) {
1025			portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
1026			if (portp->tx.buf == (char *) NULL)
1027				return(-ENOMEM);
1028			portp->tx.head = portp->tx.buf;
1029			portp->tx.tail = portp->tx.buf;
1030		}
1031		stl_setport(portp, tty->termios);
1032		portp->sigs = stl_getsignals(portp);
1033		stl_setsignals(portp, 1, 1);
1034		stl_enablerxtx(portp, 1, 1);
1035		stl_startrxtx(portp, 1, 0);
1036		clear_bit(TTY_IO_ERROR, &tty->flags);
1037		portp->flags |= ASYNC_INITIALIZED;
1038	}
1039
1040/*
1041 *	Check if this port is in the middle of closing. If so then wait
1042 *	until it is closed then return error status, based on flag settings.
1043 *	The sleep here does not need interrupt protection since the wakeup
1044 *	for it is done with the same context.
1045 */
1046	if (portp->flags & ASYNC_CLOSING) {
1047		interruptible_sleep_on(&portp->close_wait);
1048		if (portp->flags & ASYNC_HUP_NOTIFY)
1049			return(-EAGAIN);
1050		return(-ERESTARTSYS);
1051	}
1052
1053/*
1054 *	Based on type of open being done check if it can overlap with any
1055 *	previous opens still in effect. If we are a normal serial device
1056 *	then also we might have to wait for carrier.
1057 */
1058	if (!(filp->f_flags & O_NONBLOCK)) {
1059		if ((rc = stl_waitcarrier(portp, filp)) != 0)
1060			return(rc);
1061	}
1062	portp->flags |= ASYNC_NORMAL_ACTIVE;
1063
1064	return(0);
1065}
1066
1067/*****************************************************************************/
1068
1069/*
1070 *	Possibly need to wait for carrier (DCD signal) to come high. Say
1071 *	maybe because if we are clocal then we don't need to wait...
1072 */
1073
1074static int stl_waitcarrier(stlport_t *portp, struct file *filp)
1075{
1076	unsigned long	flags;
1077	int		rc, doclocal;
1078
1079#ifdef DEBUG
1080	printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
1081#endif
1082
1083	rc = 0;
1084	doclocal = 0;
1085
1086	if (portp->tty->termios->c_cflag & CLOCAL)
1087		doclocal++;
1088
1089	save_flags(flags);
1090	cli();
1091	portp->openwaitcnt++;
1092	if (! tty_hung_up_p(filp))
1093		portp->refcount--;
1094
1095	for (;;) {
1096		stl_setsignals(portp, 1, 1);
1097		if (tty_hung_up_p(filp) ||
1098		    ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1099			if (portp->flags & ASYNC_HUP_NOTIFY)
1100				rc = -EBUSY;
1101			else
1102				rc = -ERESTARTSYS;
1103			break;
1104		}
1105		if (((portp->flags & ASYNC_CLOSING) == 0) &&
1106		    (doclocal || (portp->sigs & TIOCM_CD))) {
1107			break;
1108		}
1109		if (signal_pending(current)) {
1110			rc = -ERESTARTSYS;
1111			break;
1112		}
1113		interruptible_sleep_on(&portp->open_wait);
1114	}
1115
1116	if (! tty_hung_up_p(filp))
1117		portp->refcount++;
1118	portp->openwaitcnt--;
1119	restore_flags(flags);
1120
1121	return(rc);
1122}
1123
1124/*****************************************************************************/
1125
1126static void stl_close(struct tty_struct *tty, struct file *filp)
1127{
1128	stlport_t	*portp;
1129	unsigned long	flags;
1130
1131#ifdef DEBUG
1132	printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1133#endif
1134
1135	portp = tty->driver_data;
1136	if (portp == (stlport_t *) NULL)
1137		return;
1138
1139	save_flags(flags);
1140	cli();
1141	if (tty_hung_up_p(filp)) {
1142		restore_flags(flags);
1143		return;
1144	}
1145	if ((tty->count == 1) && (portp->refcount != 1))
1146		portp->refcount = 1;
1147	if (portp->refcount-- > 1) {
1148		restore_flags(flags);
1149		return;
1150	}
1151
1152	portp->refcount = 0;
1153	portp->flags |= ASYNC_CLOSING;
1154
1155/*
1156 *	May want to wait for any data to drain before closing. The BUSY
1157 *	flag keeps track of whether we are still sending or not - it is
1158 *	very accurate for the cd1400, not quite so for the sc26198.
1159 *	(The sc26198 has no "end-of-data" interrupt only empty FIFO)
1160 */
1161	tty->closing = 1;
1162	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1163		tty_wait_until_sent(tty, portp->closing_wait);
1164	stl_waituntilsent(tty, (HZ / 2));
1165
1166	portp->flags &= ~ASYNC_INITIALIZED;
1167	stl_disableintrs(portp);
1168	if (tty->termios->c_cflag & HUPCL)
1169		stl_setsignals(portp, 0, 0);
1170	stl_enablerxtx(portp, 0, 0);
1171	stl_flushbuffer(tty);
1172	portp->istate = 0;
1173	if (portp->tx.buf != (char *) NULL) {
1174		kfree(portp->tx.buf);
1175		portp->tx.buf = (char *) NULL;
1176		portp->tx.head = (char *) NULL;
1177		portp->tx.tail = (char *) NULL;
1178	}
1179	set_bit(TTY_IO_ERROR, &tty->flags);
1180	tty_ldisc_flush(tty);
1181
1182	tty->closing = 0;
1183	portp->tty = (struct tty_struct *) NULL;
1184
1185	if (portp->openwaitcnt) {
1186		if (portp->close_delay)
1187			msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1188		wake_up_interruptible(&portp->open_wait);
1189	}
1190
1191	portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1192	wake_up_interruptible(&portp->close_wait);
1193	restore_flags(flags);
1194}
1195
1196/*****************************************************************************/
1197
1198/*
1199 *	Write routine. Take data and stuff it in to the TX ring queue.
1200 *	If transmit interrupts are not running then start them.
1201 */
1202
1203static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
1204{
1205	stlport_t	*portp;
1206	unsigned int	len, stlen;
1207	unsigned char	*chbuf;
1208	char		*head, *tail;
1209
1210#ifdef DEBUG
1211	printk("stl_write(tty=%x,buf=%x,count=%d)\n",
1212		(int) tty, (int) buf, count);
1213#endif
1214
1215	if ((tty == (struct tty_struct *) NULL) ||
1216	    (stl_tmpwritebuf == (char *) NULL))
1217		return(0);
1218	portp = tty->driver_data;
1219	if (portp == (stlport_t *) NULL)
1220		return(0);
1221	if (portp->tx.buf == (char *) NULL)
1222		return(0);
1223
1224/*
1225 *	If copying direct from user space we must cater for page faults,
1226 *	causing us to "sleep" here for a while. To handle this copy in all
1227 *	the data we need now, into a local buffer. Then when we got it all
1228 *	copy it into the TX buffer.
1229 */
1230	chbuf = (unsigned char *) buf;
1231
1232	head = portp->tx.head;
1233	tail = portp->tx.tail;
1234	if (head >= tail) {
1235		len = STL_TXBUFSIZE - (head - tail) - 1;
1236		stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
1237	} else {
1238		len = tail - head - 1;
1239		stlen = len;
1240	}
1241
1242	len = MIN(len, count);
1243	count = 0;
1244	while (len > 0) {
1245		stlen = MIN(len, stlen);
1246		memcpy(head, chbuf, stlen);
1247		len -= stlen;
1248		chbuf += stlen;
1249		count += stlen;
1250		head += stlen;
1251		if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
1252			head = portp->tx.buf;
1253			stlen = tail - head;
1254		}
1255	}
1256	portp->tx.head = head;
1257
1258	clear_bit(ASYI_TXLOW, &portp->istate);
1259	stl_startrxtx(portp, -1, 1);
1260
1261	return(count);
1262}
1263
1264/*****************************************************************************/
1265
1266static void stl_putchar(struct tty_struct *tty, unsigned char ch)
1267{
1268	stlport_t	*portp;
1269	unsigned int	len;
1270	char		*head, *tail;
1271
1272#ifdef DEBUG
1273	printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1274#endif
1275
1276	if (tty == (struct tty_struct *) NULL)
1277		return;
1278	portp = tty->driver_data;
1279	if (portp == (stlport_t *) NULL)
1280		return;
1281	if (portp->tx.buf == (char *) NULL)
1282		return;
1283
1284	head = portp->tx.head;
1285	tail = portp->tx.tail;
1286
1287	len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
1288	len--;
1289
1290	if (len > 0) {
1291		*head++ = ch;
1292		if (head >= (portp->tx.buf + STL_TXBUFSIZE))
1293			head = portp->tx.buf;
1294	}	
1295	portp->tx.head = head;
1296}
1297
1298/*****************************************************************************/
1299
1300/*
1301 *	If there are any characters in the buffer then make sure that TX
1302 *	interrupts are on and get'em out. Normally used after the putchar
1303 *	routine has been called.
1304 */
1305
1306static void stl_flushchars(struct tty_struct *tty)
1307{
1308	stlport_t	*portp;
1309
1310#ifdef DEBUG
1311	printk("stl_flushchars(tty=%x)\n", (int) tty);
1312#endif
1313
1314	if (tty == (struct tty_struct *) NULL)
1315		return;
1316	portp = tty->driver_data;
1317	if (portp == (stlport_t *) NULL)
1318		return;
1319	if (portp->tx.buf == (char *) NULL)
1320		return;
1321
1322#if 0
1323	if (tty->stopped || tty->hw_stopped ||
1324	    (portp->tx.head == portp->tx.tail))
1325		return;
1326#endif
1327	stl_startrxtx(portp, -1, 1);
1328}
1329
1330/*****************************************************************************/
1331
1332static int stl_writeroom(struct tty_struct *tty)
1333{
1334	stlport_t	*portp;
1335	char		*head, *tail;
1336
1337#ifdef DEBUG
1338	printk("stl_writeroom(tty=%x)\n", (int) tty);
1339#endif
1340
1341	if (tty == (struct tty_struct *) NULL)
1342		return(0);
1343	portp = tty->driver_data;
1344	if (portp == (stlport_t *) NULL)
1345		return(0);
1346	if (portp->tx.buf == (char *) NULL)
1347		return(0);
1348
1349	head = portp->tx.head;
1350	tail = portp->tx.tail;
1351	return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
1352}
1353
1354/*****************************************************************************/
1355
1356/*
1357 *	Return number of chars in the TX buffer. Normally we would just
1358 *	calculate the number of chars in the buffer and return that, but if
1359 *	the buffer is empty and TX interrupts are still on then we return
1360 *	that the buffer still has 1 char in it. This way whoever called us
1361 *	will not think that ALL chars have drained - since the UART still
1362 *	must have some chars in it (we are busy after all).
1363 */
1364
1365static int stl_charsinbuffer(struct tty_struct *tty)
1366{
1367	stlport_t	*portp;
1368	unsigned int	size;
1369	char		*head, *tail;
1370
1371#ifdef DEBUG
1372	printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
1373#endif
1374
1375	if (tty == (struct tty_struct *) NULL)
1376		return(0);
1377	portp = tty->driver_data;
1378	if (portp == (stlport_t *) NULL)
1379		return(0);
1380	if (portp->tx.buf == (char *) NULL)
1381		return(0);
1382
1383	head = portp->tx.head;
1384	tail = portp->tx.tail;
1385	size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1386	if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1387		size = 1;
1388	return(size);
1389}
1390
1391/*****************************************************************************/
1392
1393/*
1394 *	Generate the serial struct info.
1395 */
1396
1397static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp)
1398{
1399	struct serial_struct	sio;
1400	stlbrd_t		*brdp;
1401
1402#ifdef DEBUG
1403	printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1404#endif
1405
1406	memset(&sio, 0, sizeof(struct serial_struct));
1407	sio.line = portp->portnr;
1408	sio.port = portp->ioaddr;
1409	sio.flags = portp->flags;
1410	sio.baud_base = portp->baud_base;
1411	sio.close_delay = portp->close_delay;
1412	sio.closing_wait = portp->closing_wait;
1413	sio.custom_divisor = portp->custom_divisor;
1414	sio.hub6 = 0;
1415	if (portp->uartp == &stl_cd1400uart) {
1416		sio.type = PORT_CIRRUS;
1417		sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1418	} else {
1419		sio.type = PORT_UNKNOWN;
1420		sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1421	}
1422
1423	brdp = stl_brds[portp->brdnr];
1424	if (brdp != (stlbrd_t *) NULL)
1425		sio.irq = brdp->irq;
1426
1427	return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1428}
1429
1430/*****************************************************************************/
1431
1432/*
1433 *	Set port according to the serial struct info.
1434 *	At this point we do not do any auto-configure stuff, so we will
1435 *	just quietly ignore any requests to change irq, etc.
1436 */
1437
1438static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp)
1439{
1440	struct serial_struct	sio;
1441
1442#ifdef DEBUG
1443	printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1444#endif
1445
1446	if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1447		return -EFAULT;
1448	if (!capable(CAP_SYS_ADMIN)) {
1449		if ((sio.baud_base != portp->baud_base) ||
1450		    (sio.close_delay != portp->close_delay) ||
1451		    ((sio.flags & ~ASYNC_USR_MASK) !=
1452		    (portp->flags & ~ASYNC_USR_MASK)))
1453			return(-EPERM);
1454	} 
1455
1456	portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1457		(sio.flags & ASYNC_USR_MASK);
1458	portp->baud_base = sio.baud_base;
1459	portp->close_delay = sio.close_delay;
1460	portp->closing_wait = sio.closing_wait;
1461	portp->custom_divisor = sio.custom_divisor;
1462	stl_setport(portp, portp->tty->termios);
1463	return(0);
1464}
1465
1466/*****************************************************************************/
1467
1468static int stl_tiocmget(struct tty_struct *tty, struct file *file)
1469{
1470	stlport_t	*portp;
1471
1472	if (tty == (struct tty_struct *) NULL)
1473		return(-ENODEV);
1474	portp = tty->driver_data;
1475	if (portp == (stlport_t *) NULL)
1476		return(-ENODEV);
1477	if (tty->flags & (1 << TTY_IO_ERROR))
1478		return(-EIO);
1479
1480	return stl_getsignals(portp);
1481}
1482
1483static int stl_tiocmset(struct tty_struct *tty, struct file *file,
1484			unsigned int set, unsigned int clear)
1485{
1486	stlport_t	*portp;
1487	int rts = -1, dtr = -1;
1488
1489	if (tty == (struct tty_struct *) NULL)
1490		return(-ENODEV);
1491	portp = tty->driver_data;
1492	if (portp == (stlport_t *) NULL)
1493		return(-ENODEV);
1494	if (tty->flags & (1 << TTY_IO_ERROR))
1495		return(-EIO);
1496
1497	if (set & TIOCM_RTS)
1498		rts = 1;
1499	if (set & TIOCM_DTR)
1500		dtr = 1;
1501	if (clear & TIOCM_RTS)
1502		rts = 0;
1503	if (clear & TIOCM_DTR)
1504		dtr = 0;
1505
1506	stl_setsignals(portp, dtr, rts);
1507	return 0;
1508}
1509
1510static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1511{
1512	stlport_t	*portp;
1513	unsigned int	ival;
1514	int		rc;
1515	void __user *argp = (void __user *)arg;
1516
1517#ifdef DEBUG
1518	printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
1519		(int) tty, (int) file, cmd, (int) arg);
1520#endif
1521
1522	if (tty == (struct tty_struct *) NULL)
1523		return(-ENODEV);
1524	portp = tty->driver_data;
1525	if (portp == (stlport_t *) NULL)
1526		return(-ENODEV);
1527
1528	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1529 	    (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1530		if (tty->flags & (1 << TTY_IO_ERROR))
1531			return(-EIO);
1532	}
1533
1534	rc = 0;
1535
1536	switch (cmd) {
1537	case TIOCGSOFTCAR:
1538		rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1539			(unsigned __user *) argp);
1540		break;
1541	case TIOCSSOFTCAR:
1542		if (get_user(ival, (unsigned int __user *) arg))
1543			return -EFAULT;
1544		tty->termios->c_cflag =
1545				(tty->termios->c_cflag & ~CLOCAL) |
1546				(ival ? CLOCAL : 0);
1547		break;
1548	case TIOCGSERIAL:
1549		rc = stl_getserial(portp, argp);
1550		break;
1551	case TIOCSSERIAL:
1552		rc = stl_setserial(portp, argp);
1553		break;
1554	case COM_GETPORTSTATS:
1555		rc = stl_getportstats(portp, argp);
1556		break;
1557	case COM_CLRPORTSTATS:
1558		rc = stl_clrportstats(portp, argp);
1559		break;
1560	case TIOCSERCONFIG:
1561	case TIOCSERGWILD:
1562	case TIOCSERSWILD:
1563	case TIOCSERGETLSR:
1564	case TIOCSERGSTRUCT:
1565	case TIOCSERGETMULTI:
1566	case TIOCSERSETMULTI:
1567	default:
1568		rc = -ENOIOCTLCMD;
1569		break;
1570	}
1571
1572	return(rc);
1573}
1574
1575/*****************************************************************************/
1576
1577static void stl_settermios(struct tty_struct *tty, struct termios *old)
1578{
1579	stlport_t	*portp;
1580	struct termios	*tiosp;
1581
1582#ifdef DEBUG
1583	printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1584#endif
1585
1586	if (tty == (struct tty_struct *) NULL)
1587		return;
1588	portp = tty->driver_data;
1589	if (portp == (stlport_t *) NULL)
1590		return;
1591
1592	tiosp = tty->termios;
1593	if ((tiosp->c_cflag == old->c_cflag) &&
1594	    (tiosp->c_iflag == old->c_iflag))
1595		return;
1596
1597	stl_setport(portp, tiosp);
1598	stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1599		-1);
1600	if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1601		tty->hw_stopped = 0;
1602		stl_start(tty);
1603	}
1604	if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1605		wake_up_interruptible(&portp->open_wait);
1606}
1607
1608/*****************************************************************************/
1609
1610/*
1611 *	Attempt to flow control who ever is sending us data. Based on termios
1612 *	settings use software or/and hardware flow control.
1613 */
1614
1615static void stl_throttle(struct tty_struct *tty)
1616{
1617	stlport_t	*portp;
1618
1619#ifdef DEBUG
1620	printk("stl_throttle(tty=%x)\n", (int) tty);
1621#endif
1622
1623	if (tty == (struct tty_struct *) NULL)
1624		return;
1625	portp = tty->driver_data;
1626	if (portp == (stlport_t *) NULL)
1627		return;
1628	stl_flowctrl(portp, 0);
1629}
1630
1631/*****************************************************************************/
1632
1633/*
1634 *	Unflow control the device sending us data...
1635 */
1636
1637static void stl_unthrottle(struct tty_struct *tty)
1638{
1639	stlport_t	*portp;
1640
1641#ifdef DEBUG
1642	printk("stl_unthrottle(tty=%x)\n", (int) tty);
1643#endif
1644
1645	if (tty == (struct tty_struct *) NULL)
1646		return;
1647	portp = tty->driver_data;
1648	if (portp == (stlport_t *) NULL)
1649		return;
1650	stl_flowctrl(portp, 1);
1651}
1652
1653/*****************************************************************************/
1654
1655/*
1656 *	Stop the transmitter. Basically to do this we will just turn TX
1657 *	interrupts off.
1658 */
1659
1660static void stl_stop(struct tty_struct *tty)
1661{
1662	stlport_t	*portp;
1663
1664#ifdef DEBUG
1665	printk("stl_stop(tty=%x)\n", (int) tty);
1666#endif
1667
1668	if (tty == (struct tty_struct *) NULL)
1669		return;
1670	portp = tty->driver_data;
1671	if (portp == (stlport_t *) NULL)
1672		return;
1673	stl_startrxtx(portp, -1, 0);
1674}
1675
1676/*****************************************************************************/
1677
1678/*
1679 *	Start the transmitter again. Just turn TX interrupts back on.
1680 */
1681
1682static void stl_start(struct tty_struct *tty)
1683{
1684	stlport_t	*portp;
1685
1686#ifdef DEBUG
1687	printk("stl_start(tty=%x)\n", (int) tty);
1688#endif
1689
1690	if (tty == (struct tty_struct *) NULL)
1691		return;
1692	portp = tty->driver_data;
1693	if (portp == (stlport_t *) NULL)
1694		return;
1695	stl_startrxtx(portp, -1, 1);
1696}
1697
1698/*****************************************************************************/
1699
1700/*
1701 *	Hangup this port. This is pretty much like closing the port, only
1702 *	a little more brutal. No waiting for data to drain. Shutdown the
1703 *	port and maybe drop signals.
1704 */
1705
1706static void stl_hangup(struct tty_struct *tty)
1707{
1708	stlport_t	*portp;
1709
1710#ifdef DEBUG
1711	printk("stl_hangup(tty=%x)\n", (int) tty);
1712#endif
1713
1714	if (tty == (struct tty_struct *) NULL)
1715		return;
1716	portp = tty->driver_data;
1717	if (portp == (stlport_t *) NULL)
1718		return;
1719
1720	portp->flags &= ~ASYNC_INITIALIZED;
1721	stl_disableintrs(portp);
1722	if (tty->termios->c_cflag & HUPCL)
1723		stl_setsignals(portp, 0, 0);
1724	stl_enablerxtx(portp, 0, 0);
1725	stl_flushbuffer(tty);
1726	portp->istate = 0;
1727	set_bit(TTY_IO_ERROR, &tty->flags);
1728	if (portp->tx.buf != (char *) NULL) {
1729		kfree(portp->tx.buf);
1730		portp->tx.buf = (char *) NULL;
1731		portp->tx.head = (char *) NULL;
1732		portp->tx.tail = (char *) NULL;
1733	}
1734	portp->tty = (struct tty_struct *) NULL;
1735	portp->flags &= ~ASYNC_NORMAL_ACTIVE;
1736	portp->refcount = 0;
1737	wake_up_interruptible(&portp->open_wait);
1738}
1739
1740/*****************************************************************************/
1741
1742static void stl_flushbuffer(struct tty_struct *tty)
1743{
1744	stlport_t	*portp;
1745
1746#ifdef DEBUG
1747	printk("stl_flushbuffer(tty=%x)\n", (int) tty);
1748#endif
1749
1750	if (tty == (struct tty_struct *) NULL)
1751		return;
1752	portp = tty->driver_data;
1753	if (portp == (stlport_t *) NULL)
1754		return;
1755
1756	stl_flush(portp);
1757	tty_wakeup(tty);
1758}
1759
1760/*****************************************************************************/
1761
1762static void stl_breakctl(struct tty_struct *tty, int state)
1763{
1764	stlport_t	*portp;
1765
1766#ifdef DEBUG
1767	printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
1768#endif
1769
1770	if (tty == (struct tty_struct *) NULL)
1771		return;
1772	portp = tty->driver_data;
1773	if (portp == (stlport_t *) NULL)
1774		return;
1775
1776	stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1777}
1778
1779/*****************************************************************************/
1780
1781static void stl_waituntilsent(struct tty_struct *tty, int timeout)
1782{
1783	stlport_t	*portp;
1784	unsigned long	tend;
1785
1786#ifdef DEBUG
1787	printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
1788#endif
1789
1790	if (tty == (struct tty_struct *) NULL)
1791		return;
1792	portp = tty->driver_data;
1793	if (portp == (stlport_t *) NULL)
1794		return;
1795
1796	if (timeout == 0)
1797		timeout = HZ;
1798	tend = jiffies + timeout;
1799
1800	while (stl_datastate(portp)) {
1801		if (signal_pending(current))
1802			break;
1803		msleep_interruptible(20);
1804		if (time_after_eq(jiffies, tend))
1805			break;
1806	}
1807}
1808
1809/*****************************************************************************/
1810
1811static void stl_sendxchar(struct tty_struct *tty, char ch)
1812{
1813	stlport_t	*portp;
1814
1815#ifdef DEBUG
1816	printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
1817#endif
1818
1819	if (tty == (struct tty_struct *) NULL)
1820		return;
1821	portp = tty->driver_data;
1822	if (portp == (stlport_t *) NULL)
1823		return;
1824
1825	if (ch == STOP_CHAR(tty))
1826		stl_sendflow(portp, 0);
1827	else if (ch == START_CHAR(tty))
1828		stl_sendflow(portp, 1);
1829	else
1830		stl_putchar(tty, ch);
1831}
1832
1833/*****************************************************************************/
1834
1835#define	MAXLINE		80
1836
1837/*
1838 *	Format info for a specified port. The line is deliberately limited
1839 *	to 80 characters. (If it is too long it will be truncated, if too
1840 *	short then padded with spaces).
1841 */
1842
1843static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
1844{
1845	char	*sp;
1846	int	sigs, cnt;
1847
1848	sp = pos;
1849	sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
1850		portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1851		(int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1852
1853	if (portp->stats.rxframing)
1854		sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
1855	if (portp->stats.rxparity)
1856		sp += spri

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