specialix.c 56.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 *      specialix.c  -- specialix IO8+ multiport serial driver.
 *
 *      Copyright (C) 1997  Roger Wolff (R.E.Wolff@BitWizard.nl)
 *      Copyright (C) 1994-1996  Dmitry Gorodchanin (pgmdsg@ibi.com)
 *
 *      Specialix pays for the development and support of this driver.
 *      Please DO contact io8-linux@specialix.co.uk if you require
 *      support. But please read the documentation (specialix.txt)
 *      first.
 *
 *      This driver was developped in the BitWizard linux device
 *      driver service. If you require a linux device driver for your
 *      product, please contact devices@BitWizard.nl for a quote.
 *
 *      This code is firmly based on the riscom/8 serial driver,
 *      written by Dmitry Gorodchanin. The specialix IO8+ card
 *      programming information was obtained from the CL-CD1865 Data
 *      Book, and Specialix document number 6200059: IO8+ Hardware
 *      Functional Specification.
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License as
 *      published by the Free Software Foundation; either version 2 of
 *      the License, or (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be
 *      useful, but WITHOUT ANY WARRANTY; without even the implied
 *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *      PURPOSE.  See the GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public
 *      License along with this program; if not, write to the Free
 *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *      USA.
 *
 * Revision history:
 *
 * Revision 1.0:  April 1st 1997.
 *                Initial release for alpha testing.
41 42
 * Revision 1.1:  April 14th 1997.
 *                Incorporated Richard Hudsons suggestions,
L
Linus Torvalds 已提交
43 44 45
 *                removed some debugging printk's.
 * Revision 1.2:  April 15th 1997.
 *                Ported to 2.1.x kernels.
46 47
 * Revision 1.3:  April 17th 1997
 *                Backported to 2.0. (Compatibility macros).
L
Linus Torvalds 已提交
48
 * Revision 1.4:  April 18th 1997
49 50
 *                Fixed DTR/RTS bug that caused the card to indicate
 *                "don't send data" to a modem after the password prompt.
L
Linus Torvalds 已提交
51 52
 *                Fixed bug for premature (fake) interrupts.
 * Revision 1.5:  April 19th 1997
53
 *                fixed a minor typo in the header file, cleanup a little.
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62
 *                performance warnings are now MAXed at once per minute.
 * Revision 1.6:  May 23 1997
 *                Changed the specialix=... format to include interrupt.
 * Revision 1.7:  May 27 1997
 *                Made many more debug printk's a compile time option.
 * Revision 1.8:  Jul 1  1997
 *                port to linux-2.1.43 kernel.
 * Revision 1.9:  Oct 9  1998
 *                Added stuff for the IO8+/PCI version.
63 64
 * Revision 1.10: Oct 22  1999 / Jan 21 2000.
 *                Added stuff for setserial.
L
Linus Torvalds 已提交
65
 *                Nicolas Mailhot (Nicolas.Mailhot@email.enst.fr)
66
 *
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74
 */

#define VERSION "1.11"


/*
 * There is a bunch of documentation about the card, jumpers, config
 * settings, restrictions, cables, device names and numbers in
75
 * Documentation/serial/specialix.txt
L
Linus Torvalds 已提交
76 77 78 79
 */

#include <linux/module.h>

A
Alan Cox 已提交
80
#include <linux/io.h>
L
Linus Torvalds 已提交
81 82 83 84 85 86
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/tty.h>
A
Alan Cox 已提交
87
#include <linux/tty_flip.h>
L
Linus Torvalds 已提交
88 89 90 91 92 93 94
#include <linux/mm.h>
#include <linux/serial.h>
#include <linux/fcntl.h>
#include <linux/major.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/init.h>
A
Alan Cox 已提交
95
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

#include "specialix_io8.h"
#include "cd1865.h"


/*
   This driver can spew a whole lot of debugging output at you. If you
   need maximum performance, you should disable the DEBUG define. To
   aid in debugging in the field, I'm leaving the compile-time debug
   features enabled, and disable them "runtime". That allows me to
   instruct people with problems to enable debugging without requiring
   them to recompile...
*/
#define DEBUG

static int sx_debug;
static int sx_rxfifo = SPECIALIX_RXFIFO;
A
Alan Cox 已提交
113
static int sx_rtscts;
L
Linus Torvalds 已提交
114 115

#ifdef DEBUG
A
Alan Cox 已提交
116
#define dprintk(f, str...) if (sx_debug & f) printk(str)
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#else
#define dprintk(f, str...) /* nothing */
#endif

#define SX_DEBUG_FLOW    0x0001
#define SX_DEBUG_DATA    0x0002
#define SX_DEBUG_PROBE   0x0004
#define SX_DEBUG_CHAN    0x0008
#define SX_DEBUG_INIT    0x0010
#define SX_DEBUG_RX      0x0020
#define SX_DEBUG_TX      0x0040
#define SX_DEBUG_IRQ     0x0080
#define SX_DEBUG_OPEN    0x0100
#define SX_DEBUG_TERMIOS 0x0200
#define SX_DEBUG_SIGNALS 0x0400
#define SX_DEBUG_FIFO    0x0800


A
Alan Cox 已提交
135 136
#define func_enter() dprintk(SX_DEBUG_FLOW, "io8: enter %s\n", __func__)
#define func_exit()  dprintk(SX_DEBUG_FLOW, "io8: exit  %s\n", __func__)
L
Linus Torvalds 已提交
137 138 139 140 141 142 143


/* Configurable options: */

/* Am I paranoid or not ? ;-) */
#define SPECIALIX_PARANOIA_CHECK

144
/*
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
 * The following defines are mostly for testing purposes. But if you need
 * some nice reporting in your syslog, you can define them also.
 */
#undef SX_REPORT_FIFO
#undef SX_REPORT_OVERRUN




#define SPECIALIX_LEGAL_FLAGS \
	(ASYNC_HUP_NOTIFY   | ASYNC_SAK          | ASYNC_SPLIT_TERMIOS   | \
	 ASYNC_SPD_HI       | ASYNC_SPEED_VHI    | ASYNC_SESSION_LOCKOUT | \
	 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)

static struct tty_driver *specialix_driver;

static struct specialix_board sx_board[SX_NBOARD] =  {
	{ 0, SX_IOBASE1,  9, },
	{ 0, SX_IOBASE2, 11, },
	{ 0, SX_IOBASE3, 12, },
	{ 0, SX_IOBASE4, 15, },
};

static struct specialix_port sx_port[SX_NBOARD * SX_NPORT];


A
Alan Cox 已提交
171
static int sx_paranoia_check(struct specialix_port const *port,
L
Linus Torvalds 已提交
172 173 174
				    char *name, const char *routine)
{
#ifdef SPECIALIX_PARANOIA_CHECK
A
Alan Cox 已提交
175 176 177 178
	static const char *badmagic = KERN_ERR
	  "sx: Warning: bad specialix port magic number for device %s in %s\n";
	static const char *badinfo = KERN_ERR
	  "sx: Warning: null specialix port for device %s in %s\n";
179

L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193
	if (!port) {
		printk(badinfo, name, routine);
		return 1;
	}
	if (port->magic != SPECIALIX_MAGIC) {
		printk(badmagic, name, routine);
		return 1;
	}
#endif
	return 0;
}


/*
194
 *
L
Linus Torvalds 已提交
195
 *  Service functions for specialix IO8+ driver.
196
 *
L
Linus Torvalds 已提交
197 198 199
 */

/* Get board number from pointer */
A
Alan Cox 已提交
200
static inline int board_No(struct specialix_board *bp)
L
Linus Torvalds 已提交
201 202 203 204 205 206
{
	return bp - sx_board;
}


/* Get port number from pointer */
A
Alan Cox 已提交
207
static inline int port_No(struct specialix_port const *port)
L
Linus Torvalds 已提交
208
{
209
	return SX_PORT(port - sx_port);
L
Linus Torvalds 已提交
210 211 212 213
}


/* Get pointer to board from pointer to port */
A
Alan Cox 已提交
214 215
static inline struct specialix_board *port_Board(
					struct specialix_port const *port)
L
Linus Torvalds 已提交
216 217 218 219 220 221
{
	return &sx_board[SX_BOARD(port - sx_port)];
}


/* Input Byte from CL CD186x register */
A
Alan Cox 已提交
222 223
static inline unsigned char sx_in(struct specialix_board *bp,
							unsigned short reg)
L
Linus Torvalds 已提交
224 225
{
	bp->reg = reg | 0x80;
A
Alan Cox 已提交
226 227
	outb(reg | 0x80, bp->base + SX_ADDR_REG);
	return inb(bp->base + SX_DATA_REG);
L
Linus Torvalds 已提交
228 229 230 231
}


/* Output Byte to CL CD186x register */
A
Alan Cox 已提交
232
static inline void sx_out(struct specialix_board *bp, unsigned short reg,
L
Linus Torvalds 已提交
233 234 235
			  unsigned char val)
{
	bp->reg = reg | 0x80;
A
Alan Cox 已提交
236 237
	outb(reg | 0x80, bp->base + SX_ADDR_REG);
	outb(val, bp->base + SX_DATA_REG);
L
Linus Torvalds 已提交
238 239 240 241
}


/* Input Byte from CL CD186x register */
A
Alan Cox 已提交
242 243
static inline unsigned char sx_in_off(struct specialix_board *bp,
				unsigned short reg)
L
Linus Torvalds 已提交
244 245
{
	bp->reg = reg;
A
Alan Cox 已提交
246 247
	outb(reg, bp->base + SX_ADDR_REG);
	return inb(bp->base + SX_DATA_REG);
L
Linus Torvalds 已提交
248 249 250 251
}


/* Output Byte to CL CD186x register */
A
Alan Cox 已提交
252 253
static inline void sx_out_off(struct specialix_board  *bp,
				unsigned short reg, unsigned char val)
L
Linus Torvalds 已提交
254 255
{
	bp->reg = reg;
A
Alan Cox 已提交
256 257
	outb(reg, bp->base + SX_ADDR_REG);
	outb(val, bp->base + SX_DATA_REG);
L
Linus Torvalds 已提交
258 259 260 261
}


/* Wait for Channel Command Register ready */
A
Alan Cox 已提交
262
static void sx_wait_CCR(struct specialix_board  *bp)
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272
{
	unsigned long delay, flags;
	unsigned char ccr;

	for (delay = SX_CCR_TIMEOUT; delay; delay--) {
		spin_lock_irqsave(&bp->lock, flags);
		ccr = sx_in(bp, CD186x_CCR);
		spin_unlock_irqrestore(&bp->lock, flags);
		if (!ccr)
			return;
A
Alan Cox 已提交
273
		udelay(1);
L
Linus Torvalds 已提交
274
	}
275

L
Linus Torvalds 已提交
276 277 278 279 280
	printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
}


/* Wait for Channel Command Register ready */
A
Alan Cox 已提交
281
static void sx_wait_CCR_off(struct specialix_board  *bp)
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292
{
	unsigned long delay;
	unsigned char crr;
	unsigned long flags;

	for (delay = SX_CCR_TIMEOUT; delay; delay--) {
		spin_lock_irqsave(&bp->lock, flags);
		crr = sx_in_off(bp, CD186x_CCR);
		spin_unlock_irqrestore(&bp->lock, flags);
		if (!crr)
			return;
A
Alan Cox 已提交
293
		udelay(1);
L
Linus Torvalds 已提交
294
	}
295

L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303
	printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
}


/*
 *  specialix IO8+ IO range functions.
 */

A
Alan Cox 已提交
304
static int sx_request_io_range(struct specialix_board *bp)
L
Linus Torvalds 已提交
305
{
306 307 308
	return request_region(bp->base,
		bp->flags & SX_BOARD_IS_PCI ? SX_PCI_IO_SPACE : SX_IO_SPACE,
		"specialix IO8+") == NULL;
L
Linus Torvalds 已提交
309 310 311
}


A
Alan Cox 已提交
312
static void sx_release_io_range(struct specialix_board *bp)
L
Linus Torvalds 已提交
313
{
A
Alan Cox 已提交
314 315
	release_region(bp->base, bp->flags & SX_BOARD_IS_PCI ?
					SX_PCI_IO_SPACE : SX_IO_SPACE);
L
Linus Torvalds 已提交
316 317
}

318

L
Linus Torvalds 已提交
319
/* Set the IRQ using the RTS lines that run to the PAL on the board.... */
A
Alan Cox 已提交
320
static int sx_set_irq(struct specialix_board *bp)
L
Linus Torvalds 已提交
321 322 323 324 325
{
	int virq;
	int i;
	unsigned long flags;

326
	if (bp->flags & SX_BOARD_IS_PCI)
L
Linus Torvalds 已提交
327 328 329
		return 1;
	switch (bp->irq) {
	/* In the same order as in the docs... */
A
Alan Cox 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	case 15:
		virq = 0;
		break;
	case 12:
		virq = 1;
		break;
	case 11:
		virq = 2;
		break;
	case 9:
		virq = 3;
		break;
	default:printk(KERN_ERR
			    "Speclialix: cannot set irq to %d.\n", bp->irq);
		return 0;
L
Linus Torvalds 已提交
345 346
	}
	spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
347
	for (i = 0; i < 2; i++) {
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356
		sx_out(bp, CD186x_CAR, i);
		sx_out(bp, CD186x_MSVRTS, ((virq >> i) & 0x1)? MSVR_RTS:0);
	}
	spin_unlock_irqrestore(&bp->lock, flags);
	return 1;
}


/* Reset and setup CD186x chip */
A
Alan Cox 已提交
357
static int sx_init_CD186x(struct specialix_board  *bp)
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367
{
	unsigned long flags;
	int scaler;
	int rv = 1;

	func_enter();
	sx_wait_CCR_off(bp);			   /* Wait for CCR ready        */
	spin_lock_irqsave(&bp->lock, flags);
	sx_out_off(bp, CD186x_CCR, CCR_HARDRESET);      /* Reset CD186x chip          */
	spin_unlock_irqrestore(&bp->lock, flags);
368
	msleep(50);					/* Delay 0.05 sec            */
L
Linus Torvalds 已提交
369 370 371 372 373 374 375
	spin_lock_irqsave(&bp->lock, flags);
	sx_out_off(bp, CD186x_GIVR, SX_ID);             /* Set ID for this chip      */
	sx_out_off(bp, CD186x_GICR, 0);                 /* Clear all bits            */
	sx_out_off(bp, CD186x_PILR1, SX_ACK_MINT);      /* Prio for modem intr       */
	sx_out_off(bp, CD186x_PILR2, SX_ACK_TINT);      /* Prio for transmitter intr */
	sx_out_off(bp, CD186x_PILR3, SX_ACK_RINT);      /* Prio for receiver intr    */
	/* Set RegAckEn */
A
Alan Cox 已提交
376
	sx_out_off(bp, CD186x_SRCR, sx_in(bp, CD186x_SRCR) | SRCR_REGACKEN);
377

L
Linus Torvalds 已提交
378 379 380 381 382 383 384
	/* Setting up prescaler. We need 4 ticks per 1 ms */
	scaler =  SX_OSCFREQ/SPECIALIX_TPS;

	sx_out_off(bp, CD186x_PPRH, scaler >> 8);
	sx_out_off(bp, CD186x_PPRL, scaler & 0xff);
	spin_unlock_irqrestore(&bp->lock, flags);

A
Alan Cox 已提交
385
	if (!sx_set_irq(bp)) {
L
Linus Torvalds 已提交
386
		/* Figure out how to pass this along... */
A
Alan Cox 已提交
387
		printk(KERN_ERR "Cannot set irq to %d.\n", bp->irq);
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395
		rv = 0;
	}

	func_exit();
	return rv;
}


A
Alan Cox 已提交
396
static int read_cross_byte(struct specialix_board *bp, int reg, int bit)
L
Linus Torvalds 已提交
397 398 399 400 401 402
{
	int i;
	int t;
	unsigned long flags;

	spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
403 404 405
	for (i = 0, t = 0; i < 8; i++) {
		sx_out_off(bp, CD186x_CAR, i);
		if (sx_in_off(bp, reg) & bit)
L
Linus Torvalds 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
			t |= 1 << i;
	}
	spin_unlock_irqrestore(&bp->lock, flags);

	return t;
}


/* Main probing routine, also sets irq. */
static int sx_probe(struct specialix_board *bp)
{
	unsigned char val1, val2;
	int rev;
	int chip;

	func_enter();

423
	if (sx_request_io_range(bp)) {
L
Linus Torvalds 已提交
424 425 426 427 428 429
		func_exit();
		return 1;
	}

	/* Are the I/O ports here ? */
	sx_out_off(bp, CD186x_PPRL, 0x5a);
A
Alan Cox 已提交
430
	udelay(1);
L
Linus Torvalds 已提交
431 432 433
	val1 = sx_in_off(bp, CD186x_PPRL);

	sx_out_off(bp, CD186x_PPRL, 0xa5);
A
Alan Cox 已提交
434
	udelay(1);
L
Linus Torvalds 已提交
435 436
	val2 = sx_in_off(bp, CD186x_PPRL);

437

A
Alan Cox 已提交
438
	if (val1 != 0x5a || val2 != 0xa5) {
A
Alan Cox 已提交
439 440 441
		printk(KERN_INFO
			"sx%d: specialix IO8+ Board at 0x%03x not found.\n",
						board_No(bp), bp->base);
442
		sx_release_io_range(bp);
L
Linus Torvalds 已提交
443 444 445 446
		func_exit();
		return 1;
	}

447
	/* Check the DSR lines that Specialix uses as board
L
Linus Torvalds 已提交
448
	   identification */
A
Alan Cox 已提交
449 450 451 452 453
	val1 = read_cross_byte(bp, CD186x_MSVR, MSVR_DSR);
	val2 = read_cross_byte(bp, CD186x_MSVR, MSVR_RTS);
	dprintk(SX_DEBUG_INIT,
			"sx%d: DSR lines are: %02x, rts lines are: %02x\n",
					board_No(bp), val1, val2);
L
Linus Torvalds 已提交
454 455 456 457 458 459 460

	/* They managed to switch the bit order between the docs and
	   the IO8+ card. The new PCI card now conforms to old docs.
	   They changed the PCI docs to reflect the situation on the
	   old card. */
	val2 = (bp->flags & SX_BOARD_IS_PCI)?0x4d : 0xb2;
	if (val1 != val2) {
A
Alan Cox 已提交
461 462
		printk(KERN_INFO
		  "sx%d: specialix IO8+ ID %02x at 0x%03x not found (%02x).\n",
L
Linus Torvalds 已提交
463
		       board_No(bp), val2, bp->base, val1);
464
		sx_release_io_range(bp);
L
Linus Torvalds 已提交
465 466 467 468 469 470 471
		func_exit();
		return 1;
	}


	/* Reset CD186x again  */
	if (!sx_init_CD186x(bp)) {
472
		sx_release_io_range(bp);
L
Linus Torvalds 已提交
473
		func_exit();
474
		return 1;
L
Linus Torvalds 已提交
475 476 477 478
	}

	sx_request_io_range(bp);
	bp->flags |= SX_BOARD_PRESENT;
479

L
Linus Torvalds 已提交
480
	/* Chip           revcode   pkgtype
A
Alan Cox 已提交
481
			  GFRCR     SRCR bit 7
L
Linus Torvalds 已提交
482 483 484
	   CD180 rev B    0x81      0
	   CD180 rev C    0x82      0
	   CD1864 rev A   0x82      1
485
	   CD1865 rev A   0x83      1  -- Do not use!!! Does not work.
L
Linus Torvalds 已提交
486 487 488 489 490
	   CD1865 rev B   0x84      1
	 -- Thanks to Gwen Wang, Cirrus Logic.
	 */

	switch (sx_in_off(bp, CD186x_GFRCR)) {
A
Alan Cox 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	case 0x82:
		chip = 1864;
		rev = 'A';
		break;
	case 0x83:
		chip = 1865;
		rev = 'A';
		break;
	case 0x84:
		chip = 1865;
		rev = 'B';
		break;
	case 0x85:
		chip = 1865;
		rev = 'C';
		break; /* Does not exist at this time */
	default:
		chip = -1;
		rev = 'x';
L
Linus Torvalds 已提交
510 511
	}

A
Alan Cox 已提交
512
	dprintk(SX_DEBUG_INIT, " GFCR = 0x%02x\n", sx_in_off(bp, CD186x_GFRCR));
L
Linus Torvalds 已提交
513

A
Alan Cox 已提交
514 515 516
	printk(KERN_INFO
    "sx%d: specialix IO8+ board detected at 0x%03x, IRQ %d, CD%d Rev. %c.\n",
				board_No(bp), bp->base, bp->irq, chip, rev);
L
Linus Torvalds 已提交
517 518 519 520 521

	func_exit();
	return 0;
}

522 523
/*
 *
L
Linus Torvalds 已提交
524 525 526
 *  Interrupt processing routines.
 * */

A
Alan Cox 已提交
527
static struct specialix_port *sx_get_port(struct specialix_board *bp,
A
Alan Cox 已提交
528
					       unsigned char const *what)
L
Linus Torvalds 已提交
529 530
{
	unsigned char channel;
A
Alan Cox 已提交
531
	struct specialix_port *port = NULL;
L
Linus Torvalds 已提交
532 533

	channel = sx_in(bp, CD186x_GICR) >> GICR_CHAN_OFF;
A
Alan Cox 已提交
534
	dprintk(SX_DEBUG_CHAN, "channel: %d\n", channel);
L
Linus Torvalds 已提交
535 536
	if (channel < CD186x_NCH) {
		port = &sx_port[board_No(bp) * SX_NPORT + channel];
A
Alan Cox 已提交
537 538 539
		dprintk(SX_DEBUG_CHAN, "port: %d %p flags: 0x%lx\n",
			board_No(bp) * SX_NPORT + channel,  port,
			port->port.flags & ASYNC_INITIALIZED);
L
Linus Torvalds 已提交
540

A
Alan Cox 已提交
541
		if (port->port.flags & ASYNC_INITIALIZED) {
A
Alan Cox 已提交
542
			dprintk(SX_DEBUG_CHAN, "port: %d %p\n", channel, port);
L
Linus Torvalds 已提交
543 544 545 546
			func_exit();
			return port;
		}
	}
547
	printk(KERN_INFO "sx%d: %s interrupt from invalid port %d\n",
L
Linus Torvalds 已提交
548 549 550 551 552
	       board_No(bp), what, channel);
	return NULL;
}


A
Alan Cox 已提交
553
static void sx_receive_exc(struct specialix_board *bp)
L
Linus Torvalds 已提交
554 555 556 557
{
	struct specialix_port *port;
	struct tty_struct *tty;
	unsigned char status;
A
Alan Cox 已提交
558
	unsigned char ch, flag;
L
Linus Torvalds 已提交
559 560 561 562 563

	func_enter();

	port = sx_get_port(bp, "Receive");
	if (!port) {
A
Alan Cox 已提交
564
		dprintk(SX_DEBUG_RX, "Hmm, couldn't find port.\n");
L
Linus Torvalds 已提交
565 566 567
		func_exit();
		return;
	}
A
Alan Cox 已提交
568
	tty = port->port.tty;
569

L
Linus Torvalds 已提交
570 571
	status = sx_in(bp, CD186x_RCSR);

A
Alan Cox 已提交
572
	dprintk(SX_DEBUG_RX, "status: 0x%x\n", status);
L
Linus Torvalds 已提交
573 574
	if (status & RCSR_OE) {
		port->overrun++;
A
Alan Cox 已提交
575 576 577
		dprintk(SX_DEBUG_FIFO,
			"sx%d: port %d: Overrun. Total %ld overruns.\n",
				board_No(bp), port_No(port), port->overrun);
L
Linus Torvalds 已提交
578 579 580 581 582
	}
	status &= port->mark_mask;

	/* This flip buffer check needs to be below the reading of the
	   status register to reset the chip's IRQ.... */
A
Alan Cox 已提交
583
	if (tty_buffer_request_room(tty, 1) == 0) {
A
Alan Cox 已提交
584 585 586
		dprintk(SX_DEBUG_FIFO,
		    "sx%d: port %d: Working around flip buffer overflow.\n",
					board_No(bp), port_No(port));
L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595 596
		func_exit();
		return;
	}

	ch = sx_in(bp, CD186x_RDR);
	if (!status) {
		func_exit();
		return;
	}
	if (status & RCSR_TOUT) {
A
Alan Cox 已提交
597 598 599
		printk(KERN_INFO
		    "sx%d: port %d: Receiver timeout. Hardware problems ?\n",
					board_No(bp), port_No(port));
L
Linus Torvalds 已提交
600 601
		func_exit();
		return;
602

L
Linus Torvalds 已提交
603 604 605
	} else if (status & RCSR_BREAK) {
		dprintk(SX_DEBUG_RX, "sx%d: port %d: Handling break...\n",
		       board_No(bp), port_No(port));
A
Alan Cox 已提交
606
		flag = TTY_BREAK;
A
Alan Cox 已提交
607
		if (port->port.flags & ASYNC_SAK)
L
Linus Torvalds 已提交
608
			do_SAK(tty);
609 610

	} else if (status & RCSR_PE)
A
Alan Cox 已提交
611
		flag = TTY_PARITY;
612 613

	else if (status & RCSR_FE)
A
Alan Cox 已提交
614
		flag = TTY_FRAME;
615

L
Linus Torvalds 已提交
616
	else if (status & RCSR_OE)
A
Alan Cox 已提交
617
		flag = TTY_OVERRUN;
618

L
Linus Torvalds 已提交
619
	else
A
Alan Cox 已提交
620
		flag = TTY_NORMAL;
L
Linus Torvalds 已提交
621

A
Alan Cox 已提交
622
	if (tty_insert_flip_char(tty, ch, flag))
A
Alan Cox 已提交
623
		tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
624 625 626 627
	func_exit();
}


A
Alan Cox 已提交
628
static void sx_receive(struct specialix_board *bp)
L
Linus Torvalds 已提交
629 630 631 632 633 634
{
	struct specialix_port *port;
	struct tty_struct *tty;
	unsigned char count;

	func_enter();
635

A
Alan Cox 已提交
636 637 638
	port = sx_get_port(bp, "Receive");
	if (port == NULL) {
		dprintk(SX_DEBUG_RX, "Hmm, couldn't find port.\n");
L
Linus Torvalds 已提交
639 640 641
		func_exit();
		return;
	}
A
Alan Cox 已提交
642
	tty = port->port.tty;
643

L
Linus Torvalds 已提交
644
	count = sx_in(bp, CD186x_RDCR);
A
Alan Cox 已提交
645
	dprintk(SX_DEBUG_RX, "port: %p: count: %d\n", port, count);
L
Linus Torvalds 已提交
646
	port->hits[count > 8 ? 9 : count]++;
647

A
Alan Cox 已提交
648
	tty_buffer_request_room(tty, count);
L
Linus Torvalds 已提交
649

A
Alan Cox 已提交
650 651 652
	while (count--)
		tty_insert_flip_char(tty, sx_in(bp, CD186x_RDR), TTY_NORMAL);
	tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
653 654 655 656
	func_exit();
}


A
Alan Cox 已提交
657
static void sx_transmit(struct specialix_board *bp)
L
Linus Torvalds 已提交
658 659 660 661 662 663
{
	struct specialix_port *port;
	struct tty_struct *tty;
	unsigned char count;

	func_enter();
A
Alan Cox 已提交
664 665
	port = sx_get_port(bp, "Transmit");
	if (port == NULL) {
L
Linus Torvalds 已提交
666 667 668
		func_exit();
		return;
	}
A
Alan Cox 已提交
669
	dprintk(SX_DEBUG_TX, "port: %p\n", port);
A
Alan Cox 已提交
670
	tty = port->port.tty;
671

L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679
	if (port->IER & IER_TXEMPTY) {
		/* FIFO drained */
		sx_out(bp, CD186x_CAR, port_No(port));
		port->IER &= ~IER_TXEMPTY;
		sx_out(bp, CD186x_IER, port->IER);
		func_exit();
		return;
	}
680

L
Linus Torvalds 已提交
681 682 683 684 685 686 687 688
	if ((port->xmit_cnt <= 0 && !port->break_length)
	    || tty->stopped || tty->hw_stopped) {
		sx_out(bp, CD186x_CAR, port_No(port));
		port->IER &= ~IER_TXRDY;
		sx_out(bp, CD186x_IER, port->IER);
		func_exit();
		return;
	}
689

L
Linus Torvalds 已提交
690 691 692 693 694 695 696 697 698 699 700
	if (port->break_length) {
		if (port->break_length > 0) {
			if (port->COR2 & COR2_ETC) {
				sx_out(bp, CD186x_TDR, CD186x_C_ESC);
				sx_out(bp, CD186x_TDR, CD186x_C_SBRK);
				port->COR2 &= ~COR2_ETC;
			}
			count = min_t(int, port->break_length, 0xff);
			sx_out(bp, CD186x_TDR, CD186x_C_ESC);
			sx_out(bp, CD186x_TDR, CD186x_C_DELAY);
			sx_out(bp, CD186x_TDR, count);
A
Alan Cox 已提交
701 702
			port->break_length -= count;
			if (port->break_length == 0)
L
Linus Torvalds 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715
				port->break_length--;
		} else {
			sx_out(bp, CD186x_TDR, CD186x_C_ESC);
			sx_out(bp, CD186x_TDR, CD186x_C_EBRK);
			sx_out(bp, CD186x_COR2, port->COR2);
			sx_wait_CCR(bp);
			sx_out(bp, CD186x_CCR, CCR_CORCHG2);
			port->break_length = 0;
		}

		func_exit();
		return;
	}
716

L
Linus Torvalds 已提交
717 718 719 720 721 722 723
	count = CD186x_NFIFO;
	do {
		sx_out(bp, CD186x_TDR, port->xmit_buf[port->xmit_tail++]);
		port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
		if (--port->xmit_cnt <= 0)
			break;
	} while (--count > 0);
724

L
Linus Torvalds 已提交
725 726 727 728 729 730
	if (port->xmit_cnt <= 0) {
		sx_out(bp, CD186x_CAR, port_No(port));
		port->IER &= ~IER_TXRDY;
		sx_out(bp, CD186x_IER, port->IER);
	}
	if (port->xmit_cnt <= port->wakeup_chars)
A
Alan Cox 已提交
731
		tty_wakeup(tty);
L
Linus Torvalds 已提交
732 733 734 735 736

	func_exit();
}


A
Alan Cox 已提交
737
static void sx_check_modem(struct specialix_board *bp)
L
Linus Torvalds 已提交
738 739 740 741 742 743
{
	struct specialix_port *port;
	struct tty_struct *tty;
	unsigned char mcr;
	int msvr_cd;

A
Alan Cox 已提交
744 745 746
	dprintk(SX_DEBUG_SIGNALS, "Modem intr. ");
	port = sx_get_port(bp, "Modem");
	if (port == NULL)
L
Linus Torvalds 已提交
747
		return;
748

A
Alan Cox 已提交
749
	tty = port->port.tty;
750

L
Linus Torvalds 已提交
751 752 753
	mcr = sx_in(bp, CD186x_MCR);

	if ((mcr & MCR_CDCHG)) {
A
Alan Cox 已提交
754
		dprintk(SX_DEBUG_SIGNALS, "CD just changed... ");
L
Linus Torvalds 已提交
755 756
		msvr_cd = sx_in(bp, CD186x_MSVR) & MSVR_CD;
		if (msvr_cd) {
A
Alan Cox 已提交
757
			dprintk(SX_DEBUG_SIGNALS, "Waking up guys in open.\n");
A
Alan Cox 已提交
758
			wake_up_interruptible(&port->port.open_wait);
L
Linus Torvalds 已提交
759
		} else {
A
Alan Cox 已提交
760
			dprintk(SX_DEBUG_SIGNALS, "Sending HUP.\n");
761
			tty_hangup(tty);
L
Linus Torvalds 已提交
762 763
		}
	}
764

L
Linus Torvalds 已提交
765 766 767 768 769 770
#ifdef SPECIALIX_BRAIN_DAMAGED_CTS
	if (mcr & MCR_CTSCHG) {
		if (sx_in(bp, CD186x_MSVR) & MSVR_CTS) {
			tty->hw_stopped = 0;
			port->IER |= IER_TXRDY;
			if (port->xmit_cnt <= port->wakeup_chars)
771
				tty_wakeup(tty);
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782
		} else {
			tty->hw_stopped = 1;
			port->IER &= ~IER_TXRDY;
		}
		sx_out(bp, CD186x_IER, port->IER);
	}
	if (mcr & MCR_DSSXHG) {
		if (sx_in(bp, CD186x_MSVR) & MSVR_DSR) {
			tty->hw_stopped = 0;
			port->IER |= IER_TXRDY;
			if (port->xmit_cnt <= port->wakeup_chars)
783
				tty_wakeup(tty);
L
Linus Torvalds 已提交
784 785 786 787 788 789 790
		} else {
			tty->hw_stopped = 1;
			port->IER &= ~IER_TXRDY;
		}
		sx_out(bp, CD186x_IER, port->IER);
	}
#endif /* SPECIALIX_BRAIN_DAMAGED_CTS */
791

L
Linus Torvalds 已提交
792 793 794 795 796 797
	/* Clear change bits */
	sx_out(bp, CD186x_MCR, 0);
}


/* The main interrupt processing routine */
798
static irqreturn_t sx_interrupt(int dummy, void *dev_id)
L
Linus Torvalds 已提交
799 800 801
{
	unsigned char status;
	unsigned char ack;
802
	struct specialix_board *bp = dev_id;
L
Linus Torvalds 已提交
803 804 805 806 807 808 809 810
	unsigned long loop = 0;
	int saved_reg;
	unsigned long flags;

	func_enter();

	spin_lock_irqsave(&bp->lock, flags);

A
Alan Cox 已提交
811 812 813
	dprintk(SX_DEBUG_FLOW, "enter %s port %d room: %ld\n", __func__,
		port_No(sx_get_port(bp, "INT")),
		SERIAL_XMIT_SIZE - sx_get_port(bp, "ITN")->xmit_cnt - 1);
814
	if (!(bp->flags & SX_BOARD_ACTIVE)) {
A
Alan Cox 已提交
815 816
		dprintk(SX_DEBUG_IRQ, "sx: False interrupt. irq %d.\n",
								bp->irq);
L
Linus Torvalds 已提交
817 818 819 820 821 822 823
		spin_unlock_irqrestore(&bp->lock, flags);
		func_exit();
		return IRQ_NONE;
	}

	saved_reg = bp->reg;

A
Alan Cox 已提交
824 825 826 827 828
	while (++loop < 16) {
		status = sx_in(bp, CD186x_SRSR) &
				(SRSR_RREQint | SRSR_TREQint | SRSR_MREQint);
		if (status == 0)
			break;
L
Linus Torvalds 已提交
829 830 831 832 833 834 835 836
		if (status & SRSR_RREQint) {
			ack = sx_in(bp, CD186x_RRAR);

			if (ack == (SX_ID | GIVR_IT_RCV))
				sx_receive(bp);
			else if (ack == (SX_ID | GIVR_IT_REXC))
				sx_receive_exc(bp);
			else
A
Alan Cox 已提交
837 838 839
				printk(KERN_ERR
				"sx%d: status: 0x%x Bad receive ack 0x%02x.\n",
						board_No(bp), status, ack);
840

L
Linus Torvalds 已提交
841 842 843 844 845 846 847
		} else if (status & SRSR_TREQint) {
			ack = sx_in(bp, CD186x_TRAR);

			if (ack == (SX_ID | GIVR_IT_TX))
				sx_transmit(bp);
			else
				printk(KERN_ERR "sx%d: status: 0x%x Bad transmit ack 0x%02x. port: %d\n",
A
Alan Cox 已提交
848 849
					board_No(bp), status, ack,
					port_No(sx_get_port(bp, "Int")));
L
Linus Torvalds 已提交
850 851 852
		} else if (status & SRSR_MREQint) {
			ack = sx_in(bp, CD186x_MRAR);

853
			if (ack == (SX_ID | GIVR_IT_MODEM))
L
Linus Torvalds 已提交
854 855
				sx_check_modem(bp);
			else
A
Alan Cox 已提交
856 857
				printk(KERN_ERR
				  "sx%d: status: 0x%x Bad modem ack 0x%02x.\n",
L
Linus Torvalds 已提交
858
				       board_No(bp), status, ack);
859 860

		}
L
Linus Torvalds 已提交
861 862 863 864

		sx_out(bp, CD186x_EOIR, 0);   /* Mark end of interrupt */
	}
	bp->reg = saved_reg;
A
Alan Cox 已提交
865
	outb(bp->reg, bp->base + SX_ADDR_REG);
L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873 874 875
	spin_unlock_irqrestore(&bp->lock, flags);
	func_exit();
	return IRQ_HANDLED;
}


/*
 *  Routines for open & close processing.
 */

A
Alan Cox 已提交
876
static void turn_ints_off(struct specialix_board *bp)
L
Linus Torvalds 已提交
877 878 879 880 881
{
	unsigned long flags;

	func_enter();
	spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
882
	(void) sx_in_off(bp, 0); /* Turn off interrupts. */
L
Linus Torvalds 已提交
883 884 885 886 887
	spin_unlock_irqrestore(&bp->lock, flags);

	func_exit();
}

A
Alan Cox 已提交
888
static void turn_ints_on(struct specialix_board *bp)
L
Linus Torvalds 已提交
889 890 891 892 893 894
{
	unsigned long flags;

	func_enter();

	spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
895
	(void) sx_in(bp, 0); /* Turn ON interrupts. */
L
Linus Torvalds 已提交
896 897 898 899 900 901 902
	spin_unlock_irqrestore(&bp->lock, flags);

	func_exit();
}


/* Called with disabled interrupts */
A
Alan Cox 已提交
903
static int sx_setup_board(struct specialix_board *bp)
L
Linus Torvalds 已提交
904 905 906
{
	int error;

907
	if (bp->flags & SX_BOARD_ACTIVE)
L
Linus Torvalds 已提交
908 909 910
		return 0;

	if (bp->flags & SX_BOARD_IS_PCI)
A
Alan Cox 已提交
911 912
		error = request_irq(bp->irq, sx_interrupt,
			IRQF_DISABLED | IRQF_SHARED, "specialix IO8+", bp);
L
Linus Torvalds 已提交
913
	else
A
Alan Cox 已提交
914 915
		error = request_irq(bp->irq, sx_interrupt,
			IRQF_DISABLED, "specialix IO8+", bp);
L
Linus Torvalds 已提交
916

917
	if (error)
L
Linus Torvalds 已提交
918 919
		return error;

A
Alan Cox 已提交
920
	turn_ints_on(bp);
L
Linus Torvalds 已提交
921 922 923 924 925 926 927
	bp->flags |= SX_BOARD_ACTIVE;

	return 0;
}


/* Called with disabled interrupts */
A
Alan Cox 已提交
928
static void sx_shutdown_board(struct specialix_board *bp)
L
Linus Torvalds 已提交
929 930 931 932 933 934 935 936 937
{
	func_enter();

	if (!(bp->flags & SX_BOARD_ACTIVE)) {
		func_exit();
		return;
	}

	bp->flags &= ~SX_BOARD_ACTIVE;
938

A
Alan Cox 已提交
939 940
	dprintk(SX_DEBUG_IRQ, "Freeing IRQ%d for board %d.\n",
		 bp->irq, board_No(bp));
L
Linus Torvalds 已提交
941
	free_irq(bp->irq, bp);
A
Alan Cox 已提交
942
	turn_ints_off(bp);
L
Linus Torvalds 已提交
943 944 945
	func_exit();
}

A
Alan Cox 已提交
946 947 948 949 950 951
static unsigned int sx_crtscts(struct tty_struct *tty)
{
	if (sx_rtscts)
		return C_CRTSCTS(tty);
	return 1;
}
L
Linus Torvalds 已提交
952 953

/*
954
 * Setting up port characteristics.
L
Linus Torvalds 已提交
955 956
 * Must be called with disabled interrupts
 */
A
Alan Cox 已提交
957 958
static void sx_change_speed(struct specialix_board *bp,
						struct specialix_port *port)
L
Linus Torvalds 已提交
959 960 961 962 963 964 965 966 967 968 969
{
	struct tty_struct *tty;
	unsigned long baud;
	long tmp;
	unsigned char cor1 = 0, cor3 = 0;
	unsigned char mcor1 = 0, mcor2 = 0;
	static unsigned long again;
	unsigned long flags;

	func_enter();

A
Alan Cox 已提交
970 971
	tty = port->port.tty;
	if (!tty || !tty->termios) {
L
Linus Torvalds 已提交
972 973 974 975 976 977 978 979 980 981 982 983
		func_exit();
		return;
	}

	port->IER  = 0;
	port->COR2 = 0;
	/* Select port on the board */
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));

	/* The Specialix board doens't implement the RTS lines.
	   They are used to set the IRQ level. Don't touch them. */
A
Alan Cox 已提交
984
	if (sx_crtscts(tty))
L
Linus Torvalds 已提交
985 986 987 988
		port->MSVR = MSVR_DTR | (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
	else
		port->MSVR =  (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
	spin_unlock_irqrestore(&bp->lock, flags);
A
Alan Cox 已提交
989
	dprintk(SX_DEBUG_TERMIOS, "sx: got MSVR=%02x.\n", port->MSVR);
990
	baud = tty_get_baud_rate(tty);
991

992
	if (baud == 38400) {
A
Alan Cox 已提交
993
		if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
994
			baud = 57600;
A
Alan Cox 已提交
995
		if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
996
			baud = 115200;
L
Linus Torvalds 已提交
997
	}
998

999
	if (!baud) {
L
Linus Torvalds 已提交
1000
		/* Drop DTR & exit */
A
Alan Cox 已提交
1001
		dprintk(SX_DEBUG_TERMIOS, "Dropping DTR...  Hmm....\n");
A
Alan Cox 已提交
1002
		if (!sx_crtscts(tty)) {
A
Alan Cox 已提交
1003
			port->MSVR &= ~MSVR_DTR;
L
Linus Torvalds 已提交
1004
			spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
1005
			sx_out(bp, CD186x_MSVR, port->MSVR);
L
Linus Torvalds 已提交
1006
			spin_unlock_irqrestore(&bp->lock, flags);
A
Alan Cox 已提交
1007 1008
		} else
			dprintk(SX_DEBUG_TERMIOS, "Can't drop DTR: no DTR.\n");
L
Linus Torvalds 已提交
1009 1010 1011
		return;
	} else {
		/* Set DTR on */
A
Alan Cox 已提交
1012
		if (!sx_crtscts(tty))
A
Alan Cox 已提交
1013
			port->MSVR |= MSVR_DTR;
L
Linus Torvalds 已提交
1014
	}
1015

L
Linus Torvalds 已提交
1016
	/*
1017
	 * Now we must calculate some speed depended things
L
Linus Torvalds 已提交
1018 1019 1020 1021
	 */

	/* Set baud rate for port */
	tmp = port->custom_divisor ;
A
Alan Cox 已提交
1022 1023 1024 1025 1026
	if (tmp)
		printk(KERN_INFO
			"sx%d: Using custom baud rate divisor %ld. \n"
			"This is an untested option, please be careful.\n",
							port_No(port), tmp);
L
Linus Torvalds 已提交
1027
	else
A
Alan Cox 已提交
1028 1029
		tmp = (((SX_OSCFREQ + baud/2) / baud + CD186x_TPC/2) /
								CD186x_TPC);
L
Linus Torvalds 已提交
1030

A
Alan Cox 已提交
1031
	if (tmp < 0x10 && time_before(again, jiffies)) {
L
Linus Torvalds 已提交
1032 1033 1034
		again = jiffies + HZ * 60;
		/* Page 48 of version 2.0 of the CL-CD1865 databook */
		if (tmp >= 12) {
A
Alan Cox 已提交
1035 1036 1037 1038
			printk(KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
				"Performance degradation is possible.\n"
				"Read specialix.txt for more info.\n",
						port_No(port), tmp);
L
Linus Torvalds 已提交
1039
		} else {
A
Alan Cox 已提交
1040 1041 1042
			printk(KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
		"Warning: overstressing Cirrus chip. This might not work.\n"
		"Read specialix.txt for more info.\n", port_No(port), tmp);
L
Linus Torvalds 已提交
1043 1044 1045
		}
	}
	spin_lock_irqsave(&bp->lock, flags);
1046 1047 1048
	sx_out(bp, CD186x_RBPRH, (tmp >> 8) & 0xff);
	sx_out(bp, CD186x_TBPRH, (tmp >> 8) & 0xff);
	sx_out(bp, CD186x_RBPRL, tmp & 0xff);
L
Linus Torvalds 已提交
1049 1050
	sx_out(bp, CD186x_TBPRL, tmp & 0xff);
	spin_unlock_irqrestore(&bp->lock, flags);
1051
	if (port->custom_divisor)
A
Alan Cox 已提交
1052 1053
		baud = (SX_OSCFREQ + port->custom_divisor/2) /
							port->custom_divisor;
1054
	baud = (baud + 5) / 10;		/* Estimated CPS */
L
Linus Torvalds 已提交
1055 1056

	/* Two timer ticks seems enough to wakeup something like SLIP driver */
1057
	tmp = ((baud + HZ/2) / HZ) * 2 - CD186x_NFIFO;
L
Linus Torvalds 已提交
1058 1059
	port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
					      SERIAL_XMIT_SIZE - 1 : tmp);
1060

L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067
	/* Receiver timeout will be transmission time for 1.5 chars */
	tmp = (SPECIALIX_TPS + SPECIALIX_TPS/2 + baud/2) / baud;
	tmp = (tmp > 0xff) ? 0xff : tmp;
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_RTPR, tmp);
	spin_unlock_irqrestore(&bp->lock, flags);
	switch (C_CSIZE(tty)) {
A
Alan Cox 已提交
1068
	case CS5:
L
Linus Torvalds 已提交
1069 1070
		cor1 |= COR1_5BITS;
		break;
A
Alan Cox 已提交
1071
	case CS6:
L
Linus Torvalds 已提交
1072 1073
		cor1 |= COR1_6BITS;
		break;
A
Alan Cox 已提交
1074
	case CS7:
L
Linus Torvalds 已提交
1075 1076
		cor1 |= COR1_7BITS;
		break;
A
Alan Cox 已提交
1077
	case CS8:
L
Linus Torvalds 已提交
1078 1079 1080
		cor1 |= COR1_8BITS;
		break;
	}
1081 1082

	if (C_CSTOPB(tty))
L
Linus Torvalds 已提交
1083
		cor1 |= COR1_2SB;
1084

L
Linus Torvalds 已提交
1085 1086 1087
	cor1 |= COR1_IGNORE;
	if (C_PARENB(tty)) {
		cor1 |= COR1_NORMPAR;
1088
		if (C_PARODD(tty))
L
Linus Torvalds 已提交
1089
			cor1 |= COR1_ODDP;
1090
		if (I_INPCK(tty))
L
Linus Torvalds 已提交
1091 1092 1093 1094
			cor1 &= ~COR1_IGNORE;
	}
	/* Set marking of some errors */
	port->mark_mask = RCSR_OE | RCSR_TOUT;
1095
	if (I_INPCK(tty))
L
Linus Torvalds 已提交
1096
		port->mark_mask |= RCSR_FE | RCSR_PE;
1097
	if (I_BRKINT(tty) || I_PARMRK(tty))
L
Linus Torvalds 已提交
1098
		port->mark_mask |= RCSR_BREAK;
1099
	if (I_IGNPAR(tty))
L
Linus Torvalds 已提交
1100 1101 1102
		port->mark_mask &= ~(RCSR_FE | RCSR_PE);
	if (I_IGNBRK(tty)) {
		port->mark_mask &= ~RCSR_BREAK;
1103
		if (I_IGNPAR(tty))
L
Linus Torvalds 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			/* Real raw mode. Ignore all */
			port->mark_mask &= ~RCSR_OE;
	}
	/* Enable Hardware Flow Control */
	if (C_CRTSCTS(tty)) {
#ifdef SPECIALIX_BRAIN_DAMAGED_CTS
		port->IER |= IER_DSR | IER_CTS;
		mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
		mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
		spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
1114 1115
		tty->hw_stopped = !(sx_in(bp, CD186x_MSVR) &
							(MSVR_CTS|MSVR_DSR));
L
Linus Torvalds 已提交
1116 1117
		spin_unlock_irqrestore(&bp->lock, flags);
#else
1118
		port->COR2 |= COR2_CTSAE;
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
#endif
	}
	/* Enable Software Flow Control. FIXME: I'm not sure about this */
	/* Some people reported that it works, but I still doubt it */
	if (I_IXON(tty)) {
		port->COR2 |= COR2_TXIBE;
		cor3 |= (COR3_FCT | COR3_SCDE);
		if (I_IXANY(tty))
			port->COR2 |= COR2_IXM;
		spin_lock_irqsave(&bp->lock, flags);
		sx_out(bp, CD186x_SCHR1, START_CHAR(tty));
		sx_out(bp, CD186x_SCHR2, STOP_CHAR(tty));
		sx_out(bp, CD186x_SCHR3, START_CHAR(tty));
		sx_out(bp, CD186x_SCHR4, STOP_CHAR(tty));
		spin_unlock_irqrestore(&bp->lock, flags);
	}
	if (!C_CLOCAL(tty)) {
		/* Enable CD check */
		port->IER |= IER_CD;
		mcor1 |= MCOR1_CDZD;
		mcor2 |= MCOR2_CDOD;
	}
1141 1142

	if (C_CREAD(tty))
L
Linus Torvalds 已提交
1143 1144
		/* Enable receiver */
		port->IER |= IER_RXD;
1145

L
Linus Torvalds 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
	/* Set input FIFO size (1-8 bytes) */
	cor3 |= sx_rxfifo;
	/* Setting up CD186x channel registers */
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_COR1, cor1);
	sx_out(bp, CD186x_COR2, port->COR2);
	sx_out(bp, CD186x_COR3, cor3);
	spin_unlock_irqrestore(&bp->lock, flags);
	/* Make CD186x know about registers change */
	sx_wait_CCR(bp);
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
	/* Setting up modem option registers */
A
Alan Cox 已提交
1159 1160
	dprintk(SX_DEBUG_TERMIOS, "Mcor1 = %02x, mcor2 = %02x.\n",
								mcor1, mcor2);
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	sx_out(bp, CD186x_MCOR1, mcor1);
	sx_out(bp, CD186x_MCOR2, mcor2);
	spin_unlock_irqrestore(&bp->lock, flags);
	/* Enable CD186x transmitter & receiver */
	sx_wait_CCR(bp);
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CCR, CCR_TXEN | CCR_RXEN);
	/* Enable interrupts */
	sx_out(bp, CD186x_IER, port->IER);
	/* And finally set the modem lines... */
	sx_out(bp, CD186x_MSVR, port->MSVR);
	spin_unlock_irqrestore(&bp->lock, flags);

	func_exit();
}


/* Must be called with interrupts enabled */
A
Alan Cox 已提交
1179 1180
static int sx_setup_port(struct specialix_board *bp,
						struct specialix_port *port)
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185
{
	unsigned long flags;

	func_enter();

A
Alan Cox 已提交
1186
	if (port->port.flags & ASYNC_INITIALIZED) {
L
Linus Torvalds 已提交
1187 1188 1189
		func_exit();
		return 0;
	}
1190

L
Linus Torvalds 已提交
1191 1192 1193
	if (!port->xmit_buf) {
		/* We may sleep in get_zeroed_page() */
		unsigned long tmp;
1194

A
Alan Cox 已提交
1195 1196
		tmp = get_zeroed_page(GFP_KERNEL);
		if (tmp == 0L) {
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
			func_exit();
			return -ENOMEM;
		}

		if (port->xmit_buf) {
			free_page(tmp);
			func_exit();
			return -ERESTARTSYS;
		}
		port->xmit_buf = (unsigned char *) tmp;
	}
1208

L
Linus Torvalds 已提交
1209 1210
	spin_lock_irqsave(&port->lock, flags);

A
Alan Cox 已提交
1211 1212
	if (port->port.tty)
		clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
L
Linus Torvalds 已提交
1213 1214 1215

	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
	sx_change_speed(bp, port);
A
Alan Cox 已提交
1216
	port->port.flags |= ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
1217 1218 1219

	spin_unlock_irqrestore(&port->lock, flags);

1220

L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226
	func_exit();
	return 0;
}


/* Must be called with interrupts disabled */
A
Alan Cox 已提交
1227 1228
static void sx_shutdown_port(struct specialix_board *bp,
						struct specialix_port *port)
L
Linus Torvalds 已提交
1229 1230 1231 1232
{
	struct tty_struct *tty;
	int i;
	unsigned long flags;
1233

L
Linus Torvalds 已提交
1234 1235
	func_enter();

A
Alan Cox 已提交
1236
	if (!(port->port.flags & ASYNC_INITIALIZED)) {
L
Linus Torvalds 已提交
1237 1238 1239
		func_exit();
		return;
	}
1240

L
Linus Torvalds 已提交
1241
	if (sx_debug & SX_DEBUG_FIFO) {
A
Alan Cox 已提交
1242 1243 1244 1245
		dprintk(SX_DEBUG_FIFO,
			"sx%d: port %d: %ld overruns, FIFO hits [ ",
				board_No(bp), port_No(port), port->overrun);
		for (i = 0; i < 10; i++)
L
Linus Torvalds 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
			dprintk(SX_DEBUG_FIFO, "%ld ", port->hits[i]);
		dprintk(SX_DEBUG_FIFO, "].\n");
	}

	if (port->xmit_buf) {
		free_page((unsigned long) port->xmit_buf);
		port->xmit_buf = NULL;
	}

	/* Select port */
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));

A
Alan Cox 已提交
1259 1260
	tty = port->port.tty;
	if (tty == NULL || C_HUPCL(tty)) {
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
		/* Drop DTR */
		sx_out(bp, CD186x_MSVDTR, 0);
	}
	spin_unlock_irqrestore(&bp->lock, flags);
	/* Reset port */
	sx_wait_CCR(bp);
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CCR, CCR_SOFTRESET);
	/* Disable all interrupts from this port */
	port->IER = 0;
	sx_out(bp, CD186x_IER, port->IER);
	spin_unlock_irqrestore(&bp->lock, flags);
	if (tty)
		set_bit(TTY_IO_ERROR, &tty->flags);
A
Alan Cox 已提交
1275
	port->port.flags &= ~ASYNC_INITIALIZED;
1276 1277

	if (!bp->count)
L
Linus Torvalds 已提交
1278 1279 1280 1281
		sx_shutdown_board(bp);
	func_exit();
}

1282

A
Alan Cox 已提交
1283 1284
static int block_til_ready(struct tty_struct *tty, struct file *filp,
						struct specialix_port *port)
L
Linus Torvalds 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
{
	DECLARE_WAITQUEUE(wait,  current);
	struct specialix_board *bp = port_Board(port);
	int    retval;
	int    do_clocal = 0;
	int    CD;
	unsigned long flags;

	func_enter();

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
A
Alan Cox 已提交
1299 1300 1301
	if (tty_hung_up_p(filp) || port->port.flags & ASYNC_CLOSING) {
		interruptible_sleep_on(&port->port.close_wait);
		if (port->port.flags & ASYNC_HUP_NOTIFY) {
L
Linus Torvalds 已提交
1302 1303 1304 1305 1306 1307 1308
			func_exit();
			return -EAGAIN;
		} else {
			func_exit();
			return -ERESTARTSYS;
		}
	}
1309

L
Linus Torvalds 已提交
1310 1311 1312 1313 1314 1315
	/*
	 * If non-blocking mode is set, or the port is not enabled,
	 * then make the check up front and then exit.
	 */
	if ((filp->f_flags & O_NONBLOCK) ||
	    (tty->flags & (1 << TTY_IO_ERROR))) {
A
Alan Cox 已提交
1316
		port->port.flags |= ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
		func_exit();
		return 0;
	}

	if (C_CLOCAL(tty))
		do_clocal = 1;

	/*
	 * Block waiting for the carrier detect and the line to become
	 * free (i.e., not in use by the callout).  While we are in
	 * this loop, info->count is dropped by one, so that
	 * rs_close() knows when to free things.  We restore it upon
	 * exit, either normal or abnormal.
	 */
	retval = 0;
A
Alan Cox 已提交
1332
	add_wait_queue(&port->port.open_wait, &wait);
L
Linus Torvalds 已提交
1333
	spin_lock_irqsave(&port->lock, flags);
A
Alan Cox 已提交
1334
	if (!tty_hung_up_p(filp))
A
Alan Cox 已提交
1335
		port->port.count--;
L
Linus Torvalds 已提交
1336
	spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
1337
	port->port.blocked_open++;
L
Linus Torvalds 已提交
1338 1339 1340 1341
	while (1) {
		spin_lock_irqsave(&bp->lock, flags);
		sx_out(bp, CD186x_CAR, port_No(port));
		CD = sx_in(bp, CD186x_MSVR) & MSVR_CD;
A
Alan Cox 已提交
1342
		if (sx_crtscts(tty)) {
L
Linus Torvalds 已提交
1343 1344
			/* Activate RTS */
			port->MSVR |= MSVR_DTR;		/* WTF? */
A
Alan Cox 已提交
1345
			sx_out(bp, CD186x_MSVR, port->MSVR);
L
Linus Torvalds 已提交
1346 1347 1348
		} else {
			/* Activate DTR */
			port->MSVR |= MSVR_DTR;
A
Alan Cox 已提交
1349
			sx_out(bp, CD186x_MSVR, port->MSVR);
L
Linus Torvalds 已提交
1350 1351 1352 1353
		}
		spin_unlock_irqrestore(&bp->lock, flags);
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
A
Alan Cox 已提交
1354 1355
		    !(port->port.flags & ASYNC_INITIALIZED)) {
			if (port->port.flags & ASYNC_HUP_NOTIFY)
L
Linus Torvalds 已提交
1356 1357
				retval = -EAGAIN;
			else
1358
				retval = -ERESTARTSYS;
L
Linus Torvalds 已提交
1359 1360
			break;
		}
A
Alan Cox 已提交
1361
		if (!(port->port.flags & ASYNC_CLOSING) &&
L
Linus Torvalds 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
		    (do_clocal || CD))
			break;
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
		schedule();
	}

	set_current_state(TASK_RUNNING);
A
Alan Cox 已提交
1372
	remove_wait_queue(&port->port.open_wait, &wait);
L
Linus Torvalds 已提交
1373
	spin_lock_irqsave(&port->lock, flags);
A
Alan Cox 已提交
1374
	if (!tty_hung_up_p(filp))
A
Alan Cox 已提交
1375 1376
		port->port.count++;
	port->port.blocked_open--;
L
Linus Torvalds 已提交
1377 1378 1379 1380 1381 1382
	spin_unlock_irqrestore(&port->lock, flags);
	if (retval) {
		func_exit();
		return retval;
	}

A
Alan Cox 已提交
1383
	port->port.flags |= ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
1384 1385
	func_exit();
	return 0;
1386
}
L
Linus Torvalds 已提交
1387 1388


A
Alan Cox 已提交
1389
static int sx_open(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
1390 1391 1392
{
	int board;
	int error;
A
Alan Cox 已提交
1393 1394
	struct specialix_port *port;
	struct specialix_board *bp;
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	int i;
	unsigned long flags;

	func_enter();

	board = SX_BOARD(tty->index);

	if (board >= SX_NBOARD || !(sx_board[board].flags & SX_BOARD_PRESENT)) {
		func_exit();
		return -ENODEV;
	}
1406

L
Linus Torvalds 已提交
1407 1408 1409 1410
	bp = &sx_board[board];
	port = sx_port + board * SX_NPORT + SX_PORT(tty->index);
	port->overrun = 0;
	for (i = 0; i < 10; i++)
A
Alan Cox 已提交
1411
		port->hits[i] = 0;
L
Linus Torvalds 已提交
1412

A
Alan Cox 已提交
1413 1414 1415
	dprintk(SX_DEBUG_OPEN,
			"Board = %d, bp = %p, port = %p, portno = %d.\n",
				 board, bp, port, SX_PORT(tty->index));
L
Linus Torvalds 已提交
1416 1417 1418 1419 1420 1421

	if (sx_paranoia_check(port, tty->name, "sx_open")) {
		func_enter();
		return -ENODEV;
	}

A
Alan Cox 已提交
1422 1423
	error = sx_setup_board(bp);
	if (error) {
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428
		func_exit();
		return error;
	}

	spin_lock_irqsave(&bp->lock, flags);
A
Alan Cox 已提交
1429
	port->port.count++;
L
Linus Torvalds 已提交
1430 1431
	bp->count++;
	tty->driver_data = port;
A
Alan Cox 已提交
1432
	port->port.tty = tty;
L
Linus Torvalds 已提交
1433 1434
	spin_unlock_irqrestore(&bp->lock, flags);

A
Alan Cox 已提交
1435 1436
	error = sx_setup_port(bp, port);
	if (error) {
L
Linus Torvalds 已提交
1437 1438 1439
		func_enter();
		return error;
	}
1440

A
Alan Cox 已提交
1441 1442
	error = block_til_ready(tty, filp, port);
	if (error) {
L
Linus Torvalds 已提交
1443 1444 1445 1446 1447 1448 1449 1450
		func_enter();
		return error;
	}

	func_exit();
	return 0;
}

1451 1452
static void sx_flush_buffer(struct tty_struct *tty)
{
A
Alan Cox 已提交
1453
	struct specialix_port *port = tty->driver_data;
1454
	unsigned long flags;
A
Alan Cox 已提交
1455
	struct specialix_board  *bp;
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_flush_buffer")) {
		func_exit();
		return;
	}

	bp = port_Board(port);
	spin_lock_irqsave(&port->lock, flags);
	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
	spin_unlock_irqrestore(&port->lock, flags);
	tty_wakeup(tty);

	func_exit();
}
L
Linus Torvalds 已提交
1472

A
Alan Cox 已提交
1473
static void sx_close(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
1474
{
A
Alan Cox 已提交
1475
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1476 1477 1478
	struct specialix_board *bp;
	unsigned long flags;
	unsigned long timeout;
1479

L
Linus Torvalds 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	func_enter();
	if (!port || sx_paranoia_check(port, tty->name, "close")) {
		func_exit();
		return;
	}
	spin_lock_irqsave(&port->lock, flags);

	if (tty_hung_up_p(filp)) {
		spin_unlock_irqrestore(&port->lock, flags);
		func_exit();
		return;
	}
1492

L
Linus Torvalds 已提交
1493
	bp = port_Board(port);
A
Alan Cox 已提交
1494
	if (tty->count == 1 && port->port.count != 1) {
L
Linus Torvalds 已提交
1495 1496
		printk(KERN_ERR "sx%d: sx_close: bad port count;"
		       " tty->count is 1, port count is %d\n",
A
Alan Cox 已提交
1497 1498
		       board_No(bp), port->port.count);
		port->port.count = 1;
L
Linus Torvalds 已提交
1499 1500
	}

A
Alan Cox 已提交
1501 1502
	if (port->port.count > 1) {
		port->port.count--;
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508 1509
		bp->count--;

		spin_unlock_irqrestore(&port->lock, flags);

		func_exit();
		return;
	}
A
Alan Cox 已提交
1510
	port->port.flags |= ASYNC_CLOSING;
L
Linus Torvalds 已提交
1511
	/*
1512
	 * Now we wait for the transmit buffer to clear; and we notify
L
Linus Torvalds 已提交
1513 1514 1515 1516
	 * the line discipline to only process XON/XOFF characters.
	 */
	tty->closing = 1;
	spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
1517 1518
	dprintk(SX_DEBUG_OPEN, "Closing\n");
	if (port->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
A
Alan Cox 已提交
1519
		tty_wait_until_sent(tty, port->port.closing_wait);
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525
	/*
	 * At this point we stop accepting input.  To do this, we
	 * disable the receive line status interrupts, and tell the
	 * interrupt driver to stop checking the data ready bit in the
	 * line status register.
	 */
A
Alan Cox 已提交
1526
	dprintk(SX_DEBUG_OPEN, "Closed\n");
L
Linus Torvalds 已提交
1527
	port->IER &= ~IER_RXD;
A
Alan Cox 已提交
1528
	if (port->port.flags & ASYNC_INITIALIZED) {
L
Linus Torvalds 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
		port->IER &= ~IER_TXRDY;
		port->IER |= IER_TXEMPTY;
		spin_lock_irqsave(&bp->lock, flags);
		sx_out(bp, CD186x_CAR, port_No(port));
		sx_out(bp, CD186x_IER, port->IER);
		spin_unlock_irqrestore(&bp->lock, flags);
		/*
		 * Before we drop DTR, make sure the UART transmitter
		 * has completely drained; this is especially
		 * important if there is a transmit FIFO!
		 */
		timeout = jiffies+HZ;
A
Alan Cox 已提交
1541 1542
		while (port->IER & IER_TXEMPTY) {
			set_current_state(TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
1543 1544
			msleep_interruptible(jiffies_to_msecs(port->timeout));
			if (time_after(jiffies, timeout)) {
A
Alan Cox 已提交
1545
				printk(KERN_INFO "Timeout waiting for close\n");
L
Linus Torvalds 已提交
1546 1547 1548 1549 1550 1551 1552
				break;
			}
		}

	}

	if (--bp->count < 0) {
A
Alan Cox 已提交
1553 1554 1555
		printk(KERN_ERR
		    "sx%d: sx_shutdown_port: bad board count: %d port: %d\n",
				board_No(bp), bp->count, tty->index);
L
Linus Torvalds 已提交
1556 1557
		bp->count = 0;
	}
A
Alan Cox 已提交
1558
	if (--port->port.count < 0) {
A
Alan Cox 已提交
1559 1560 1561
		printk(KERN_ERR
			"sx%d: sx_close: bad port count for tty%d: %d\n",
				board_No(bp), port_No(port), port->port.count);
A
Alan Cox 已提交
1562
		port->port.count = 0;
L
Linus Torvalds 已提交
1563 1564 1565
	}

	sx_shutdown_port(bp, port);
1566
	sx_flush_buffer(tty);
L
Linus Torvalds 已提交
1567 1568 1569
	tty_ldisc_flush(tty);
	spin_lock_irqsave(&port->lock, flags);
	tty->closing = 0;
A
Alan Cox 已提交
1570
	port->port.tty = NULL;
L
Linus Torvalds 已提交
1571
	spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
1572
	if (port->port.blocked_open) {
A
Alan Cox 已提交
1573 1574 1575
		if (port->port.close_delay)
			msleep_interruptible(
				jiffies_to_msecs(port->port.close_delay));
A
Alan Cox 已提交
1576
		wake_up_interruptible(&port->port.open_wait);
L
Linus Torvalds 已提交
1577
	}
A
Alan Cox 已提交
1578 1579
	port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
	wake_up_interruptible(&port->port.close_wait);
L
Linus Torvalds 已提交
1580 1581 1582 1583 1584

	func_exit();
}


A
Alan Cox 已提交
1585 1586
static int sx_write(struct tty_struct *tty,
					const unsigned char *buf, int count)
L
Linus Torvalds 已提交
1587
{
A
Alan Cox 已提交
1588
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1589 1590 1591 1592 1593 1594 1595 1596 1597
	struct specialix_board *bp;
	int c, total = 0;
	unsigned long flags;

	func_enter();
	if (sx_paranoia_check(port, tty->name, "sx_write")) {
		func_exit();
		return 0;
	}
1598

L
Linus Torvalds 已提交
1599 1600
	bp = port_Board(port);

1601
	if (!port->xmit_buf) {
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
		func_exit();
		return 0;
	}

	while (1) {
		spin_lock_irqsave(&port->lock, flags);
		c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
				   SERIAL_XMIT_SIZE - port->xmit_head));
		if (c <= 0) {
			spin_unlock_irqrestore(&port->lock, flags);
			break;
		}
		memcpy(port->xmit_buf + port->xmit_head, buf, c);
		port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
		port->xmit_cnt += c;
		spin_unlock_irqrestore(&port->lock, flags);

		buf += c;
		count -= c;
		total += c;
	}

	spin_lock_irqsave(&bp->lock, flags);
	if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
	    !(port->IER & IER_TXRDY)) {
		port->IER |= IER_TXRDY;
		sx_out(bp, CD186x_CAR, port_No(port));
		sx_out(bp, CD186x_IER, port->IER);
	}
	spin_unlock_irqrestore(&bp->lock, flags);
	func_exit();

	return total;
}


A
Alan Cox 已提交
1638
static int sx_put_char(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
1639
{
A
Alan Cox 已提交
1640
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1641
	unsigned long flags;
A
Alan Cox 已提交
1642
	struct specialix_board  *bp;
L
Linus Torvalds 已提交
1643 1644 1645 1646 1647

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_put_char")) {
		func_exit();
1648
		return 0;
L
Linus Torvalds 已提交
1649
	}
A
Alan Cox 已提交
1650
	dprintk(SX_DEBUG_TX, "check tty: %p %p\n", tty, port->xmit_buf);
1651
	if (!port->xmit_buf) {
L
Linus Torvalds 已提交
1652
		func_exit();
1653
		return 0;
L
Linus Torvalds 已提交
1654 1655 1656 1657
	}
	bp = port_Board(port);
	spin_lock_irqsave(&port->lock, flags);

A
Alan Cox 已提交
1658 1659 1660
	dprintk(SX_DEBUG_TX, "xmit_cnt: %d xmit_buf: %p\n",
					port->xmit_cnt, port->xmit_buf);
	if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1 || !port->xmit_buf) {
L
Linus Torvalds 已提交
1661
		spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
1662
		dprintk(SX_DEBUG_TX, "Exit size\n");
L
Linus Torvalds 已提交
1663
		func_exit();
1664
		return 0;
L
Linus Torvalds 已提交
1665
	}
A
Alan Cox 已提交
1666
	dprintk(SX_DEBUG_TX, "Handle xmit: %p %p\n", port, port->xmit_buf);
L
Linus Torvalds 已提交
1667 1668 1669 1670 1671 1672
	port->xmit_buf[port->xmit_head++] = ch;
	port->xmit_head &= SERIAL_XMIT_SIZE - 1;
	port->xmit_cnt++;
	spin_unlock_irqrestore(&port->lock, flags);

	func_exit();
1673
	return 1;
L
Linus Torvalds 已提交
1674 1675 1676
}


A
Alan Cox 已提交
1677
static void sx_flush_chars(struct tty_struct *tty)
L
Linus Torvalds 已提交
1678
{
A
Alan Cox 已提交
1679
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1680
	unsigned long flags;
A
Alan Cox 已提交
1681
	struct specialix_board  *bp = port_Board(port);
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_flush_chars")) {
		func_exit();
		return;
	}
	if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
	    !port->xmit_buf) {
		func_exit();
		return;
	}
	spin_lock_irqsave(&bp->lock, flags);
	port->IER |= IER_TXRDY;
	sx_out(port_Board(port), CD186x_CAR, port_No(port));
	sx_out(port_Board(port), CD186x_IER, port->IER);
	spin_unlock_irqrestore(&bp->lock, flags);

	func_exit();
}


A
Alan Cox 已提交
1704
static int sx_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1705
{
A
Alan Cox 已提交
1706
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
	int	ret;

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_write_room")) {
		func_exit();
		return 0;
	}

	ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
	if (ret < 0)
		ret = 0;

	func_exit();
	return ret;
}


static int sx_chars_in_buffer(struct tty_struct *tty)
{
A
Alan Cox 已提交
1727
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1728 1729

	func_enter();
1730

L
Linus Torvalds 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
	if (sx_paranoia_check(port, tty->name, "sx_chars_in_buffer")) {
		func_exit();
		return 0;
	}
	func_exit();
	return port->xmit_cnt;
}

static int sx_tiocmget(struct tty_struct *tty, struct file *file)
{
A
Alan Cox 已提交
1741
	struct specialix_port *port = tty->driver_data;
A
Alan Cox 已提交
1742
	struct specialix_board *bp;
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748
	unsigned char status;
	unsigned int result;
	unsigned long flags;

	func_enter();

1749
	if (sx_paranoia_check(port, tty->name, __func__)) {
L
Linus Torvalds 已提交
1750 1751 1752 1753 1754
		func_exit();
		return -ENODEV;
	}

	bp = port_Board(port);
A
Alan Cox 已提交
1755
	spin_lock_irqsave(&bp->lock, flags);
L
Linus Torvalds 已提交
1756 1757 1758
	sx_out(bp, CD186x_CAR, port_No(port));
	status = sx_in(bp, CD186x_MSVR);
	spin_unlock_irqrestore(&bp->lock, flags);
A
Alan Cox 已提交
1759 1760 1761
	dprintk(SX_DEBUG_INIT, "Got msvr[%d] = %02x, car = %d.\n",
			port_No(port), status, sx_in(bp, CD186x_CAR));
	dprintk(SX_DEBUG_INIT, "sx_port = %p, port = %p\n", sx_port, port);
A
Alan Cox 已提交
1762 1763
	if (sx_crtscts(port->port.tty)) {
		result  = TIOCM_DTR | TIOCM_DSR
A
Alan Cox 已提交
1764 1765 1766
			  |   ((status & MSVR_DTR) ? TIOCM_RTS : 0)
			  |   ((status & MSVR_CD)  ? TIOCM_CAR : 0)
			  |   ((status & MSVR_CTS) ? TIOCM_CTS : 0);
L
Linus Torvalds 已提交
1767
	} else {
A
Alan Cox 已提交
1768
		result  = TIOCM_RTS | TIOCM_DSR
A
Alan Cox 已提交
1769 1770 1771
			  |   ((status & MSVR_DTR) ? TIOCM_DTR : 0)
			  |   ((status & MSVR_CD)  ? TIOCM_CAR : 0)
			  |   ((status & MSVR_CTS) ? TIOCM_CTS : 0);
L
Linus Torvalds 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
	}

	func_exit();

	return result;
}


static int sx_tiocmset(struct tty_struct *tty, struct file *file,
		       unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
1783
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1784 1785 1786 1787 1788
	unsigned long flags;
	struct specialix_board *bp;

	func_enter();

1789
	if (sx_paranoia_check(port, tty->name, __func__)) {
L
Linus Torvalds 已提交
1790 1791 1792 1793 1794 1795 1796
		func_exit();
		return -ENODEV;
	}

	bp = port_Board(port);

	spin_lock_irqsave(&port->lock, flags);
A
Alan Cox 已提交
1797
	if (sx_crtscts(port->port.tty)) {
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803
		if (set & TIOCM_RTS)
			port->MSVR |= MSVR_DTR;
	} else {
		if (set & TIOCM_DTR)
			port->MSVR |= MSVR_DTR;
	}
A
Alan Cox 已提交
1804
	if (sx_crtscts(port->port.tty)) {
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
		if (clear & TIOCM_RTS)
			port->MSVR &= ~MSVR_DTR;
	} else {
		if (clear & TIOCM_DTR)
			port->MSVR &= ~MSVR_DTR;
	}
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));
	sx_out(bp, CD186x_MSVR, port->MSVR);
	spin_unlock_irqrestore(&bp->lock, flags);
	spin_unlock_irqrestore(&port->lock, flags);
	func_exit();
	return 0;
}


1821
static int sx_send_break(struct tty_struct *tty, int length)
L
Linus Torvalds 已提交
1822
{
A
Alan Cox 已提交
1823
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1824 1825
	struct specialix_board *bp = port_Board(port);
	unsigned long flags;
1826

L
Linus Torvalds 已提交
1827
	func_enter();
1828 1829
	if (length == 0 || length == -1)
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1830

A
Alan Cox 已提交
1831
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
1832 1833 1834 1835 1836 1837 1838 1839
	port->break_length = SPECIALIX_TPS / HZ * length;
	port->COR2 |= COR2_ETC;
	port->IER  |= IER_TXRDY;
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));
	sx_out(bp, CD186x_COR2, port->COR2);
	sx_out(bp, CD186x_IER, port->IER);
	spin_unlock_irqrestore(&bp->lock, flags);
A
Alan Cox 已提交
1840
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1841 1842 1843 1844 1845 1846 1847
	sx_wait_CCR(bp);
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CCR, CCR_CORCHG2);
	spin_unlock_irqrestore(&bp->lock, flags);
	sx_wait_CCR(bp);

	func_exit();
1848
	return 0;
L
Linus Torvalds 已提交
1849 1850 1851
}


A
Alan Cox 已提交
1852
static int sx_set_serial_info(struct specialix_port *port,
A
Alan Cox 已提交
1853
					struct serial_struct __user *newinfo)
L
Linus Torvalds 已提交
1854 1855 1856 1857 1858 1859
{
	struct serial_struct tmp;
	struct specialix_board *bp = port_Board(port);
	int change_speed;

	func_enter();
A
Alan Cox 已提交
1860

L
Linus Torvalds 已提交
1861 1862 1863 1864
	if (copy_from_user(&tmp, newinfo, sizeof(tmp))) {
		func_enter();
		return -EFAULT;
	}
1865

A
Alan Cox 已提交
1866
	lock_kernel();
L
Linus Torvalds 已提交
1867

A
Alan Cox 已提交
1868
	change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
L
Linus Torvalds 已提交
1869 1870
			(tmp.flags & ASYNC_SPD_MASK));
	change_speed |= (tmp.custom_divisor != port->custom_divisor);
1871

L
Linus Torvalds 已提交
1872
	if (!capable(CAP_SYS_ADMIN)) {
A
Alan Cox 已提交
1873 1874
		if ((tmp.close_delay != port->port.close_delay) ||
		    (tmp.closing_wait != port->port.closing_wait) ||
L
Linus Torvalds 已提交
1875
		    ((tmp.flags & ~ASYNC_USR_MASK) !=
A
Alan Cox 已提交
1876
		     (port->port.flags & ~ASYNC_USR_MASK))) {
L
Linus Torvalds 已提交
1877
			func_exit();
A
Alan Cox 已提交
1878
			unlock_kernel();
L
Linus Torvalds 已提交
1879 1880
			return -EPERM;
		}
A
Alan Cox 已提交
1881
		port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
A
Alan Cox 已提交
1882
						(tmp.flags & ASYNC_USR_MASK));
L
Linus Torvalds 已提交
1883 1884
		port->custom_divisor = tmp.custom_divisor;
	} else {
A
Alan Cox 已提交
1885
		port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
A
Alan Cox 已提交
1886
						(tmp.flags & ASYNC_FLAGS));
A
Alan Cox 已提交
1887 1888
		port->port.close_delay = tmp.close_delay;
		port->port.closing_wait = tmp.closing_wait;
L
Linus Torvalds 已提交
1889 1890
		port->custom_divisor = tmp.custom_divisor;
	}
A
Alan Cox 已提交
1891
	if (change_speed)
L
Linus Torvalds 已提交
1892
		sx_change_speed(bp, port);
A
Alan Cox 已提交
1893

L
Linus Torvalds 已提交
1894
	func_exit();
A
Alan Cox 已提交
1895
	unlock_kernel();
L
Linus Torvalds 已提交
1896 1897 1898 1899
	return 0;
}


A
Alan Cox 已提交
1900
static int sx_get_serial_info(struct specialix_port *port,
L
Linus Torvalds 已提交
1901 1902 1903 1904
				     struct serial_struct __user *retinfo)
{
	struct serial_struct tmp;
	struct specialix_board *bp = port_Board(port);
1905

L
Linus Torvalds 已提交
1906 1907 1908
	func_enter();

	memset(&tmp, 0, sizeof(tmp));
A
Alan Cox 已提交
1909
	lock_kernel();
L
Linus Torvalds 已提交
1910 1911 1912 1913
	tmp.type = PORT_CIRRUS;
	tmp.line = port - sx_port;
	tmp.port = bp->base;
	tmp.irq  = bp->irq;
A
Alan Cox 已提交
1914
	tmp.flags = port->port.flags;
L
Linus Torvalds 已提交
1915
	tmp.baud_base = (SX_OSCFREQ + CD186x_TPC/2) / CD186x_TPC;
A
Alan Cox 已提交
1916 1917
	tmp.close_delay = port->port.close_delay * HZ/100;
	tmp.closing_wait = port->port.closing_wait * HZ/100;
L
Linus Torvalds 已提交
1918 1919
	tmp.custom_divisor =  port->custom_divisor;
	tmp.xmit_fifo_size = CD186x_NFIFO;
A
Alan Cox 已提交
1920
	unlock_kernel();
L
Linus Torvalds 已提交
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
	if (copy_to_user(retinfo, &tmp, sizeof(tmp))) {
		func_exit();
		return -EFAULT;
	}

	func_exit();
	return 0;
}


A
Alan Cox 已提交
1931 1932
static int sx_ioctl(struct tty_struct *tty, struct file *filp,
				unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1933
{
A
Alan Cox 已提交
1934
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1935 1936 1937 1938 1939 1940 1941 1942
	void __user *argp = (void __user *)arg;

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_ioctl")) {
		func_exit();
		return -ENODEV;
	}
1943

L
Linus Torvalds 已提交
1944
	switch (cmd) {
A
Alan Cox 已提交
1945
	case TIOCGSERIAL:
1946
		func_exit();
L
Linus Torvalds 已提交
1947
		return sx_get_serial_info(port, argp);
A
Alan Cox 已提交
1948
	case TIOCSSERIAL:
1949
		func_exit();
L
Linus Torvalds 已提交
1950
		return sx_set_serial_info(port, argp);
A
Alan Cox 已提交
1951
	default:
1952
		func_exit();
L
Linus Torvalds 已提交
1953 1954 1955 1956 1957 1958 1959
		return -ENOIOCTLCMD;
	}
	func_exit();
	return 0;
}


A
Alan Cox 已提交
1960
static void sx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1961
{
A
Alan Cox 已提交
1962
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971
	struct specialix_board *bp;
	unsigned long flags;

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_throttle")) {
		func_exit();
		return;
	}
1972

L
Linus Torvalds 已提交
1973
	bp = port_Board(port);
1974

L
Linus Torvalds 已提交
1975
	/* Use DTR instead of RTS ! */
A
Alan Cox 已提交
1976
	if (sx_crtscts(tty))
L
Linus Torvalds 已提交
1977 1978 1979 1980
		port->MSVR &= ~MSVR_DTR;
	else {
		/* Auch!!! I think the system shouldn't call this then. */
		/* Or maybe we're supposed (allowed?) to do our side of hw
1981
		   handshake anyway, even when hardware handshake is off.
L
Linus Torvalds 已提交
1982
		   When you see this in your logs, please report.... */
A
Alan Cox 已提交
1983 1984 1985
		printk(KERN_ERR
		   "sx%d: Need to throttle, but can't (hardware hs is off)\n",
							port_No(port));
L
Linus Torvalds 已提交
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
	}
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));
	spin_unlock_irqrestore(&bp->lock, flags);
	if (I_IXOFF(tty)) {
		sx_wait_CCR(bp);
		spin_lock_irqsave(&bp->lock, flags);
		sx_out(bp, CD186x_CCR, CCR_SSCH2);
		spin_unlock_irqrestore(&bp->lock, flags);
		sx_wait_CCR(bp);
	}
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_MSVR, port->MSVR);
	spin_unlock_irqrestore(&bp->lock, flags);

	func_exit();
}


A
Alan Cox 已提交
2005
static void sx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
2006
{
A
Alan Cox 已提交
2007
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2008 2009 2010 2011
	struct specialix_board *bp;
	unsigned long flags;

	func_enter();
2012

L
Linus Torvalds 已提交
2013 2014 2015 2016
	if (sx_paranoia_check(port, tty->name, "sx_unthrottle")) {
		func_exit();
		return;
	}
2017

L
Linus Torvalds 已提交
2018
	bp = port_Board(port);
2019

L
Linus Torvalds 已提交
2020 2021
	spin_lock_irqsave(&port->lock, flags);
	/* XXXX Use DTR INSTEAD???? */
A
Alan Cox 已提交
2022
	if (sx_crtscts(tty))
L
Linus Torvalds 已提交
2023
		port->MSVR |= MSVR_DTR;
A
Alan Cox 已提交
2024
	/* Else clause: see remark in "sx_throttle"... */
L
Linus Torvalds 已提交
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));
	spin_unlock_irqrestore(&bp->lock, flags);
	if (I_IXOFF(tty)) {
		spin_unlock_irqrestore(&port->lock, flags);
		sx_wait_CCR(bp);
		spin_lock_irqsave(&bp->lock, flags);
		sx_out(bp, CD186x_CCR, CCR_SSCH1);
		spin_unlock_irqrestore(&bp->lock, flags);
		sx_wait_CCR(bp);
		spin_lock_irqsave(&port->lock, flags);
	}
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_MSVR, port->MSVR);
	spin_unlock_irqrestore(&bp->lock, flags);
	spin_unlock_irqrestore(&port->lock, flags);

	func_exit();
}


A
Alan Cox 已提交
2046
static void sx_stop(struct tty_struct *tty)
L
Linus Torvalds 已提交
2047
{
A
Alan Cox 已提交
2048
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2049 2050 2051 2052
	struct specialix_board *bp;
	unsigned long flags;

	func_enter();
2053

L
Linus Torvalds 已提交
2054 2055 2056 2057 2058 2059
	if (sx_paranoia_check(port, tty->name, "sx_stop")) {
		func_exit();
		return;
	}

	bp = port_Board(port);
2060

L
Linus Torvalds 已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
	spin_lock_irqsave(&port->lock, flags);
	port->IER &= ~IER_TXRDY;
	spin_lock_irqsave(&bp->lock, flags);
	sx_out(bp, CD186x_CAR, port_No(port));
	sx_out(bp, CD186x_IER, port->IER);
	spin_unlock_irqrestore(&bp->lock, flags);
	spin_unlock_irqrestore(&port->lock, flags);

	func_exit();
}


A
Alan Cox 已提交
2073
static void sx_start(struct tty_struct *tty)
L
Linus Torvalds 已提交
2074
{
A
Alan Cox 已提交
2075
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2076 2077 2078 2079
	struct specialix_board *bp;
	unsigned long flags;

	func_enter();
2080

L
Linus Torvalds 已提交
2081 2082 2083 2084
	if (sx_paranoia_check(port, tty->name, "sx_start")) {
		func_exit();
		return;
	}
2085

L
Linus Torvalds 已提交
2086
	bp = port_Board(port);
2087

L
Linus Torvalds 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
	spin_lock_irqsave(&port->lock, flags);
	if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
		port->IER |= IER_TXRDY;
		spin_lock_irqsave(&bp->lock, flags);
		sx_out(bp, CD186x_CAR, port_No(port));
		sx_out(bp, CD186x_IER, port->IER);
		spin_unlock_irqrestore(&bp->lock, flags);
	}
	spin_unlock_irqrestore(&port->lock, flags);

	func_exit();
}

A
Alan Cox 已提交
2101
static void sx_hangup(struct tty_struct *tty)
L
Linus Torvalds 已提交
2102
{
A
Alan Cox 已提交
2103
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112
	struct specialix_board *bp;
	unsigned long flags;

	func_enter();

	if (sx_paranoia_check(port, tty->name, "sx_hangup")) {
		func_exit();
		return;
	}
2113

L
Linus Torvalds 已提交
2114
	bp = port_Board(port);
2115

L
Linus Torvalds 已提交
2116 2117
	sx_shutdown_port(bp, port);
	spin_lock_irqsave(&port->lock, flags);
A
Alan Cox 已提交
2118
	bp->count -= port->port.count;
L
Linus Torvalds 已提交
2119
	if (bp->count < 0) {
A
Alan Cox 已提交
2120 2121 2122
		printk(KERN_ERR
			"sx%d: sx_hangup: bad board count: %d port: %d\n",
					board_No(bp), bp->count, tty->index);
L
Linus Torvalds 已提交
2123 2124
		bp->count = 0;
	}
A
Alan Cox 已提交
2125 2126 2127
	port->port.count = 0;
	port->port.flags &= ~ASYNC_NORMAL_ACTIVE;
	port->port.tty = NULL;
L
Linus Torvalds 已提交
2128
	spin_unlock_irqrestore(&port->lock, flags);
A
Alan Cox 已提交
2129
	wake_up_interruptible(&port->port.open_wait);
L
Linus Torvalds 已提交
2130 2131 2132 2133 2134

	func_exit();
}


A
Alan Cox 已提交
2135 2136
static void sx_set_termios(struct tty_struct *tty,
					struct ktermios *old_termios)
L
Linus Torvalds 已提交
2137
{
A
Alan Cox 已提交
2138
	struct specialix_port *port = tty->driver_data;
L
Linus Torvalds 已提交
2139
	unsigned long flags;
A
Alan Cox 已提交
2140
	struct specialix_board  *bp;
2141

L
Linus Torvalds 已提交
2142 2143
	if (sx_paranoia_check(port, tty->name, "sx_set_termios"))
		return;
2144

L
Linus Torvalds 已提交
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
	bp = port_Board(port);
	spin_lock_irqsave(&port->lock, flags);
	sx_change_speed(port_Board(port), port);
	spin_unlock_irqrestore(&port->lock, flags);

	if ((old_termios->c_cflag & CRTSCTS) &&
	    !(tty->termios->c_cflag & CRTSCTS)) {
		tty->hw_stopped = 0;
		sx_start(tty);
	}
}

J
Jeff Dike 已提交
2157
static const struct tty_operations sx_ops = {
L
Linus Torvalds 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	.open  = sx_open,
	.close = sx_close,
	.write = sx_write,
	.put_char = sx_put_char,
	.flush_chars = sx_flush_chars,
	.write_room = sx_write_room,
	.chars_in_buffer = sx_chars_in_buffer,
	.flush_buffer = sx_flush_buffer,
	.ioctl = sx_ioctl,
	.throttle = sx_throttle,
	.unthrottle = sx_unthrottle,
	.set_termios = sx_set_termios,
	.stop = sx_stop,
	.start = sx_start,
	.hangup = sx_hangup,
	.tiocmget = sx_tiocmget,
	.tiocmset = sx_tiocmset,
2175
	.break_ctl = sx_send_break,
L
Linus Torvalds 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
};

static int sx_init_drivers(void)
{
	int error;
	int i;

	func_enter();

	specialix_driver = alloc_tty_driver(SX_NBOARD * SX_NPORT);
	if (!specialix_driver) {
		printk(KERN_ERR "sx: Couldn't allocate tty_driver.\n");
		func_exit();
		return 1;
	}
2191

L
Linus Torvalds 已提交
2192 2193 2194 2195 2196 2197 2198 2199
	specialix_driver->owner = THIS_MODULE;
	specialix_driver->name = "ttyW";
	specialix_driver->major = SPECIALIX_NORMAL_MAJOR;
	specialix_driver->type = TTY_DRIVER_TYPE_SERIAL;
	specialix_driver->subtype = SERIAL_TYPE_NORMAL;
	specialix_driver->init_termios = tty_std_termios;
	specialix_driver->init_termios.c_cflag =
		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
A
Alan Cox 已提交
2200 2201
	specialix_driver->init_termios.c_ispeed = 9600;
	specialix_driver->init_termios.c_ospeed = 9600;
2202 2203
	specialix_driver->flags = TTY_DRIVER_REAL_RAW |
						TTY_DRIVER_HARDWARE_BREAK;
L
Linus Torvalds 已提交
2204 2205
	tty_set_operations(specialix_driver, &sx_ops);

A
Alan Cox 已提交
2206 2207
	error = tty_register_driver(specialix_driver);
	if (error) {
L
Linus Torvalds 已提交
2208
		put_tty_driver(specialix_driver);
A
Alan Cox 已提交
2209 2210 2211
		printk(KERN_ERR
		  "sx: Couldn't register specialix IO8+ driver, error = %d\n",
								error);
L
Linus Torvalds 已提交
2212 2213 2214 2215 2216 2217
		func_exit();
		return 1;
	}
	memset(sx_port, 0, sizeof(sx_port));
	for (i = 0; i < SX_NPORT * SX_NBOARD; i++) {
		sx_port[i].magic = SPECIALIX_MAGIC;
A
Alan Cox 已提交
2218
		tty_port_init(&sx_port[i].port);
L
Linus Torvalds 已提交
2219 2220
		spin_lock_init(&sx_port[i].lock);
	}
2221

L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
	func_exit();
	return 0;
}

static void sx_release_drivers(void)
{
	func_enter();

	tty_unregister_driver(specialix_driver);
	put_tty_driver(specialix_driver);
	func_exit();
}

2235 2236
/*
 * This routine must be called by kernel at boot time
L
Linus Torvalds 已提交
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
 */
static int __init specialix_init(void)
{
	int i;
	int found = 0;

	func_enter();

	printk(KERN_INFO "sx: Specialix IO8+ driver v" VERSION ", (c) R.E.Wolff 1997/1998.\n");
	printk(KERN_INFO "sx: derived from work (c) D.Gorodchanin 1994-1996.\n");
A
Alan Cox 已提交
2247 2248 2249 2250 2251
	if (sx_rtscts)
		printk(KERN_INFO
			"sx: DTR/RTS pin is RTS when CRTSCTS is on.\n");
	else
		printk(KERN_INFO "sx: DTR/RTS pin is always RTS.\n");
2252

L
Linus Torvalds 已提交
2253
	for (i = 0; i < SX_NBOARD; i++)
I
Ingo Molnar 已提交
2254
		spin_lock_init(&sx_board[i].lock);
L
Linus Torvalds 已提交
2255 2256 2257 2258 2259 2260

	if (sx_init_drivers()) {
		func_exit();
		return -EIO;
	}

2261
	for (i = 0; i < SX_NBOARD; i++)
L
Linus Torvalds 已提交
2262 2263 2264 2265 2266 2267 2268
		if (sx_board[i].base && !sx_probe(&sx_board[i]))
			found++;

#ifdef CONFIG_PCI
	{
		struct pci_dev *pdev = NULL;

A
Alan Cox 已提交
2269
		i = 0;
L
Linus Torvalds 已提交
2270 2271 2272 2273 2274
		while (i < SX_NBOARD) {
			if (sx_board[i].flags & SX_BOARD_PRESENT) {
				i++;
				continue;
			}
A
Alan Cox 已提交
2275 2276 2277 2278
			pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX,
					PCI_DEVICE_ID_SPECIALIX_IO8, pdev);
			if (!pdev)
				break;
L
Linus Torvalds 已提交
2279 2280 2281 2282 2283 2284

			if (pci_enable_device(pdev))
				continue;

			sx_board[i].irq = pdev->irq;

A
Alan Cox 已提交
2285
			sx_board[i].base = pci_resource_start(pdev, 2);
L
Linus Torvalds 已提交
2286 2287 2288

			sx_board[i].flags |= SX_BOARD_IS_PCI;
			if (!sx_probe(&sx_board[i]))
A
Alan Cox 已提交
2289
				found++;
L
Linus Torvalds 已提交
2290
		}
A
Alan Cox 已提交
2291 2292 2293
		/* May exit pci_get sequence early with lots of boards */
		if (pdev != NULL)
			pci_dev_put(pdev);
L
Linus Torvalds 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
	}
#endif

	if (!found) {
		sx_release_drivers();
		printk(KERN_INFO "sx: No specialix IO8+ boards detected.\n");
		func_exit();
		return -EIO;
	}

	func_exit();
	return 0;
}

static int iobase[SX_NBOARD]  = {0,};
A
Alan Cox 已提交
2309
static int irq[SX_NBOARD] = {0,};
L
Linus Torvalds 已提交
2310 2311 2312 2313

module_param_array(iobase, int, NULL, 0);
module_param_array(irq, int, NULL, 0);
module_param(sx_debug, int, 0);
A
Alan Cox 已提交
2314
module_param(sx_rtscts, int, 0);
L
Linus Torvalds 已提交
2315 2316 2317 2318 2319
module_param(sx_rxfifo, int, 0);

/*
 * You can setup up to 4 boards.
 * by specifying "iobase=0xXXX,0xXXX ..." as insmod parameter.
2320 2321
 * You should specify the IRQs too in that case "irq=....,...".
 *
L
Linus Torvalds 已提交
2322
 * More than 4 boards in one computer is not possible, as the card can
2323
 * only use 4 different interrupts.
L
Linus Torvalds 已提交
2324 2325 2326 2327 2328 2329 2330 2331 2332
 *
 */
static int __init specialix_init_module(void)
{
	int i;

	func_enter();

	if (iobase[0] || iobase[1] || iobase[2] || iobase[3]) {
A
Alan Cox 已提交
2333
		for (i = 0; i < SX_NBOARD; i++) {
L
Linus Torvalds 已提交
2334 2335
			sx_board[i].base = iobase[i];
			sx_board[i].irq = irq[i];
A
Alan Cox 已提交
2336
			sx_board[i].count = 0;
L
Linus Torvalds 已提交
2337 2338 2339 2340 2341 2342 2343
		}
	}

	func_exit();

	return specialix_init();
}
2344

L
Linus Torvalds 已提交
2345 2346 2347
static void __exit specialix_exit_module(void)
{
	int i;
2348

L
Linus Torvalds 已提交
2349 2350 2351 2352
	func_enter();

	sx_release_drivers();
	for (i = 0; i < SX_NBOARD; i++)
2353
		if (sx_board[i].flags & SX_BOARD_PRESENT)
L
Linus Torvalds 已提交
2354 2355 2356 2357
			sx_release_io_range(&sx_board[i]);
	func_exit();
}

2358 2359 2360 2361 2362 2363
static struct pci_device_id specialx_pci_tbl[] __devinitdata = {
	{ PCI_DEVICE(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_IO8) },
	{ }
};
MODULE_DEVICE_TABLE(pci, specialx_pci_tbl);

L
Linus Torvalds 已提交
2364 2365 2366 2367
module_init(specialix_init_module);
module_exit(specialix_exit_module);

MODULE_LICENSE("GPL");
2368
MODULE_ALIAS_CHARDEV_MAJOR(SPECIALIX_NORMAL_MAJOR);