riscom8.c 41.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 *      linux/drivers/char/riscom.c  -- RISCom/8 multiport serial driver.
 *
 *      Copyright (C) 1994-1996  Dmitry Gorodchanin (pgmdsg@ibi.com)
 *
 *      This code is loosely based on the Linux serial driver, written by
A
Alan Cox 已提交
7 8 9
 *      Linus Torvalds, Theodore T'so and others. The RISCom/8 card
 *      programming info was obtained from various drivers for other OSes
 *	(FreeBSD, ISC, etc), but no source code from those drivers were
L
Linus Torvalds 已提交
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
 *	directly included in this driver.
 *
 *
 *      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 1.1
 *
 *	ChangeLog:
 *	Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
 *	- get rid of check_region and several cleanups
 */

#include <linux/module.h>

A
Alan Cox 已提交
36
#include <linux/io.h>
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/mm.h>
#include <linux/serial.h>
#include <linux/fcntl.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/delay.h>
A
Alan Cox 已提交
49
#include <linux/tty_flip.h>
J
Jeff Garzik 已提交
50
#include <linux/spinlock.h>
L
Linus Torvalds 已提交
51

A
Alan Cox 已提交
52
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
53 54 55 56 57 58 59

#include "riscom8.h"
#include "riscom8_reg.h"

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

A
Alan Cox 已提交
60 61
/*
 * Crazy InteliCom/8 boards sometimes have swapped CTS & DSR signals.
L
Linus Torvalds 已提交
62
 * You can slightly speed up things by #undefing the following option,
A
Alan Cox 已提交
63
 * if you are REALLY sure that your board is correct one.
L
Linus Torvalds 已提交
64 65 66 67
 */

#define RISCOM_BRAIN_DAMAGED_CTS

A
Alan Cox 已提交
68
/*
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82
 * The following defines are mostly for testing purposes. But if you need
 * some nice reporting in your syslog, you can define them also.
 */
#undef RC_REPORT_FIFO
#undef RC_REPORT_OVERRUN


#define RISCOM_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 *riscom_driver;

J
Jeff Garzik 已提交
83 84
static DEFINE_SPINLOCK(riscom_lock);

L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static struct riscom_board rc_board[RC_NBOARD] =  {
	{
		.base	= RC_IOBASE1,
	},
	{
		.base	= RC_IOBASE2,
	},
	{
		.base	= RC_IOBASE3,
	},
	{
		.base	= RC_IOBASE4,
	},
};

static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];

/* RISCom/8 I/O ports addresses (without address translation) */
static unsigned short rc_ioport[] =  {
104
#if 1
L
Linus Torvalds 已提交
105
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
106
#else
L
Linus Torvalds 已提交
107 108 109
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
	0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
	0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
110
#endif
L
Linus Torvalds 已提交
111
};
112
#define RC_NIOPORT	ARRAY_SIZE(rc_ioport)
L
Linus Torvalds 已提交
113 114


A
Alan Cox 已提交
115
static int rc_paranoia_check(struct riscom_port const *port,
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
				    char *name, const char *routine)
{
#ifdef RISCOM_PARANOIA_CHECK
	static const char badmagic[] = KERN_INFO
		"rc: Warning: bad riscom port magic number for device %s in %s\n";
	static const char badinfo[] = KERN_INFO
		"rc: Warning: null riscom port for device %s in %s\n";

	if (!port) {
		printk(badinfo, name, routine);
		return 1;
	}
	if (port->magic != RISCOM8_MAGIC) {
		printk(badmagic, name, routine);
		return 1;
	}
#endif
	return 0;
}

/*
A
Alan Cox 已提交
137
 *
L
Linus Torvalds 已提交
138
 *  Service functions for RISCom/8 driver.
A
Alan Cox 已提交
139
 *
L
Linus Torvalds 已提交
140 141 142
 */

/* Get board number from pointer */
A
Alan Cox 已提交
143
static inline int board_No(struct riscom_board const *bp)
L
Linus Torvalds 已提交
144 145 146 147 148
{
	return bp - rc_board;
}

/* Get port number from pointer */
A
Alan Cox 已提交
149
static inline int port_No(struct riscom_port const *port)
L
Linus Torvalds 已提交
150
{
A
Alan Cox 已提交
151
	return RC_PORT(port - rc_port);
L
Linus Torvalds 已提交
152 153 154
}

/* Get pointer to board from pointer to port */
A
Alan Cox 已提交
155
static inline struct riscom_board *port_Board(struct riscom_port const *port)
L
Linus Torvalds 已提交
156 157 158 159 160
{
	return &rc_board[RC_BOARD(port - rc_port)];
}

/* Input Byte from CL CD180 register */
A
Alan Cox 已提交
161 162
static inline unsigned char rc_in(struct riscom_board const *bp,
							unsigned short reg)
L
Linus Torvalds 已提交
163 164 165 166 167
{
	return inb(bp->base + RC_TO_ISA(reg));
}

/* Output Byte to CL CD180 register */
A
Alan Cox 已提交
168
static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
L
Linus Torvalds 已提交
169 170 171 172 173 174
			  unsigned char val)
{
	outb(val, bp->base + RC_TO_ISA(reg));
}

/* Wait for Channel Command Register ready */
A
Alan Cox 已提交
175
static void rc_wait_CCR(struct riscom_board const *bp)
L
Linus Torvalds 已提交
176 177 178 179
{
	unsigned long delay;

	/* FIXME: need something more descriptive then 100000 :) */
A
Alan Cox 已提交
180
	for (delay = 100000; delay; delay--)
L
Linus Torvalds 已提交
181 182
		if (!rc_in(bp, CD180_CCR))
			return;
A
Alan Cox 已提交
183

L
Linus Torvalds 已提交
184 185 186 187 188 189 190
	printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
}

/*
 *  RISCom/8 probe functions.
 */

A
Alan Cox 已提交
191
static int rc_request_io_range(struct riscom_board * const bp)
L
Linus Torvalds 已提交
192 193
{
	int i;
A
Alan Cox 已提交
194 195

	for (i = 0; i < RC_NIOPORT; i++)
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203
		if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
				   "RISCom/8"))  {
			goto out_release;
		}
	return 0;
out_release:
	printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
			 board_No(bp), bp->base);
A
Alan Cox 已提交
204
	while (--i >= 0)
L
Linus Torvalds 已提交
205 206 207 208
		release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
	return 1;
}

A
Alan Cox 已提交
209
static void rc_release_io_range(struct riscom_board * const bp)
L
Linus Torvalds 已提交
210 211
{
	int i;
A
Alan Cox 已提交
212 213

	for (i = 0; i < RC_NIOPORT; i++)
L
Linus Torvalds 已提交
214 215
		release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
}
A
Alan Cox 已提交
216

L
Linus Torvalds 已提交
217
/* Reset and setup CD180 chip */
A
Alan Cox 已提交
218
static void __init rc_init_CD180(struct riscom_board const *bp)
L
Linus Torvalds 已提交
219 220
{
	unsigned long flags;
A
Alan Cox 已提交
221

J
Jeff Garzik 已提交
222 223
	spin_lock_irqsave(&riscom_lock, flags);

A
Alan Cox 已提交
224 225 226
	rc_out(bp, RC_CTOUT, 0);     	           /* Clear timeout        */
	rc_wait_CCR(bp);			   /* Wait for CCR ready   */
	rc_out(bp, CD180_CCR, CCR_HARDRESET);      /* Reset CD180 chip     */
J
Jeff Garzik 已提交
227
	spin_unlock_irqrestore(&riscom_lock, flags);
A
Alan Cox 已提交
228
	msleep(50);				   /* Delay 0.05 sec       */
J
Jeff Garzik 已提交
229
	spin_lock_irqsave(&riscom_lock, flags);
A
Alan Cox 已提交
230 231 232 233 234 235
	rc_out(bp, CD180_GIVR, RC_ID);             /* Set ID for this chip */
	rc_out(bp, CD180_GICR, 0);                 /* Clear all bits       */
	rc_out(bp, CD180_PILR1, RC_ACK_MINT);      /* Prio for modem intr  */
	rc_out(bp, CD180_PILR2, RC_ACK_TINT);      /* Prio for tx intr     */
	rc_out(bp, CD180_PILR3, RC_ACK_RINT);      /* Prio for rx intr	   */

L
Linus Torvalds 已提交
236 237 238
	/* Setting up prescaler. We need 4 ticks per 1 ms */
	rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
	rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
A
Alan Cox 已提交
239

J
Jeff Garzik 已提交
240
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248
}

/* Main probing routine, also sets irq. */
static int __init rc_probe(struct riscom_board *bp)
{
	unsigned char val1, val2;
	int irqs = 0;
	int retries;
A
Alan Cox 已提交
249

L
Linus Torvalds 已提交
250 251 252 253
	bp->irq = 0;

	if (rc_request_io_range(bp))
		return 1;
A
Alan Cox 已提交
254

L
Linus Torvalds 已提交
255 256 257 258 259 260 261
	/* Are the I/O ports here ? */
	rc_out(bp, CD180_PPRL, 0x5a);
	outb(0xff, 0x80);
	val1 = rc_in(bp, CD180_PPRL);
	rc_out(bp, CD180_PPRL, 0xa5);
	outb(0x00, 0x80);
	val2 = rc_in(bp, CD180_PPRL);
A
Alan Cox 已提交
262

L
Linus Torvalds 已提交
263 264 265 266 267
	if ((val1 != 0x5a) || (val2 != 0xa5))  {
		printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
		       board_No(bp), bp->base);
		goto out_release;
	}
A
Alan Cox 已提交
268

L
Linus Torvalds 已提交
269
	/* It's time to find IRQ for this board */
A
Alan Cox 已提交
270
	for (retries = 0; retries < 5 && irqs <= 0; retries++) {
L
Linus Torvalds 已提交
271
		irqs = probe_irq_on();
A
Alan Cox 已提交
272 273
		rc_init_CD180(bp);		 /* Reset CD180 chip	     */
		rc_out(bp, CD180_CAR, 2);	 /* Select port 2	     */
L
Linus Torvalds 已提交
274
		rc_wait_CCR(bp);
A
Alan Cox 已提交
275 276
		rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter	     */
		rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr     */
J
Jiri Slaby 已提交
277
		msleep(50);
L
Linus Torvalds 已提交
278
		irqs = probe_irq_off(irqs);
A
Alan Cox 已提交
279 280 281 282
		val1 = rc_in(bp, RC_BSR);	/* Get Board Status reg	     */
		val2 = rc_in(bp, RC_ACK_TINT);  /* ACK interrupt	     */
		rc_init_CD180(bp);	       	/* Reset CD180 again	     */

L
Linus Torvalds 已提交
283 284 285 286 287 288
		if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX)))  {
			printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
					"found.\n", board_No(bp), bp->base);
			goto out_release;
		}
	}
A
Alan Cox 已提交
289

L
Linus Torvalds 已提交
290 291 292 293 294 295 296
	if (irqs <= 0)  {
		printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
				"at 0x%03x.\n", board_No(bp), bp->base);
		goto out_release;
	}
	bp->irq = irqs;
	bp->flags |= RC_BOARD_PRESENT;
A
Alan Cox 已提交
297

L
Linus Torvalds 已提交
298 299 300 301 302
	printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
			 "0x%03x, IRQ %d.\n",
	       board_No(bp),
	       (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A',   /* Board revision */
	       bp->base, bp->irq);
A
Alan Cox 已提交
303

L
Linus Torvalds 已提交
304 305 306 307 308 309
	return 0;
out_release:
	rc_release_io_range(bp);
	return 1;
}

A
Alan Cox 已提交
310 311
/*
 *
L
Linus Torvalds 已提交
312
 *  Interrupt processing routines.
A
Alan Cox 已提交
313
 *
L
Linus Torvalds 已提交
314 315
 */

A
Alan Cox 已提交
316 317
static struct riscom_port *rc_get_port(struct riscom_board const *bp,
					       unsigned char const *what)
L
Linus Torvalds 已提交
318 319
{
	unsigned char channel;
A
Alan Cox 已提交
320 321
	struct riscom_port *port;

L
Linus Torvalds 已提交
322 323 324
	channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
	if (channel < CD180_NCH)  {
		port = &rc_port[board_No(bp) * RC_NPORT + channel];
A
Alan Cox 已提交
325
		if (port->port.flags & ASYNC_INITIALIZED)
L
Linus Torvalds 已提交
326 327
			return port;
	}
A
Alan Cox 已提交
328
	printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
L
Linus Torvalds 已提交
329 330 331 332
	       board_No(bp), what, channel);
	return NULL;
}

A
Alan Cox 已提交
333
static void rc_receive_exc(struct riscom_board const *bp)
L
Linus Torvalds 已提交
334 335 336 337
{
	struct riscom_port *port;
	struct tty_struct *tty;
	unsigned char status;
A
Alan Cox 已提交
338
	unsigned char ch, flag;
A
Alan Cox 已提交
339 340 341

	port = rc_get_port(bp, "Receive");
	if (port == NULL)
L
Linus Torvalds 已提交
342 343
		return;

A
Alan Cox 已提交
344
	tty = port->port.tty;
A
Alan Cox 已提交
345 346

#ifdef RC_REPORT_OVERRUN
L
Linus Torvalds 已提交
347
	status = rc_in(bp, CD180_RCSR);
A
Alan Cox 已提交
348
	if (status & RCSR_OE)
L
Linus Torvalds 已提交
349 350
		port->overrun++;
	status &= port->mark_mask;
A
Alan Cox 已提交
351
#else
L
Linus Torvalds 已提交
352
	status = rc_in(bp, CD180_RCSR) & port->mark_mask;
A
Alan Cox 已提交
353
#endif
L
Linus Torvalds 已提交
354
	ch = rc_in(bp, CD180_RDR);
A
Alan Cox 已提交
355
	if (!status)
L
Linus Torvalds 已提交
356 357 358
		return;
	if (status & RCSR_TOUT)  {
		printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
A
Alan Cox 已提交
359
				    "Hardware problems ?\n",
L
Linus Torvalds 已提交
360 361
		       board_No(bp), port_No(port));
		return;
A
Alan Cox 已提交
362

L
Linus Torvalds 已提交
363 364 365
	} else if (status & RCSR_BREAK)  {
		printk(KERN_INFO "rc%d: port %d: Handling break...\n",
		       board_No(bp), port_No(port));
A
Alan Cox 已提交
366
		flag = TTY_BREAK;
A
Alan Cox 已提交
367
		if (port->port.flags & ASYNC_SAK)
L
Linus Torvalds 已提交
368
			do_SAK(tty);
A
Alan Cox 已提交
369 370

	} else if (status & RCSR_PE)
A
Alan Cox 已提交
371
		flag = TTY_PARITY;
A
Alan Cox 已提交
372 373

	else if (status & RCSR_FE)
A
Alan Cox 已提交
374
		flag = TTY_FRAME;
A
Alan Cox 已提交
375 376

	else if (status & RCSR_OE)
A
Alan Cox 已提交
377
		flag = TTY_OVERRUN;
L
Linus Torvalds 已提交
378
	else
A
Alan Cox 已提交
379
		flag = TTY_NORMAL;
A
Alan Cox 已提交
380

A
Alan Cox 已提交
381 382
	tty_insert_flip_char(tty, ch, flag);
	tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
383 384
}

A
Alan Cox 已提交
385
static void rc_receive(struct riscom_board const *bp)
L
Linus Torvalds 已提交
386 387 388 389
{
	struct riscom_port *port;
	struct tty_struct *tty;
	unsigned char count;
A
Alan Cox 已提交
390 391 392

	port = rc_get_port(bp, "Receive");
	if (port == NULL)
L
Linus Torvalds 已提交
393
		return;
A
Alan Cox 已提交
394

A
Alan Cox 已提交
395
	tty = port->port.tty;
A
Alan Cox 已提交
396

L
Linus Torvalds 已提交
397
	count = rc_in(bp, CD180_RDCR);
A
Alan Cox 已提交
398

L
Linus Torvalds 已提交
399 400
#ifdef RC_REPORT_FIFO
	port->hits[count > 8 ? 9 : count]++;
A
Alan Cox 已提交
401 402
#endif

L
Linus Torvalds 已提交
403
	while (count--)  {
A
Alan Cox 已提交
404
		if (tty_buffer_request_room(tty, 1) == 0)  {
L
Linus Torvalds 已提交
405 406 407 408 409
			printk(KERN_WARNING "rc%d: port %d: Working around "
					    "flip buffer overflow.\n",
			       board_No(bp), port_No(port));
			break;
		}
A
Alan Cox 已提交
410
		tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
L
Linus Torvalds 已提交
411
	}
A
Alan Cox 已提交
412
	tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
413 414
}

A
Alan Cox 已提交
415
static void rc_transmit(struct riscom_board const *bp)
L
Linus Torvalds 已提交
416 417 418 419
{
	struct riscom_port *port;
	struct tty_struct *tty;
	unsigned char count;
A
Alan Cox 已提交
420 421 422

	port = rc_get_port(bp, "Transmit");
	if (port == NULL)
L
Linus Torvalds 已提交
423
		return;
A
Alan Cox 已提交
424

A
Alan Cox 已提交
425
	tty = port->port.tty;
A
Alan Cox 已提交
426 427

	if (port->IER & IER_TXEMPTY) {
L
Linus Torvalds 已提交
428 429 430 431 432 433
		/* FIFO drained */
		rc_out(bp, CD180_CAR, port_No(port));
		port->IER &= ~IER_TXEMPTY;
		rc_out(bp, CD180_IER, port->IER);
		return;
	}
A
Alan Cox 已提交
434

L
Linus Torvalds 已提交
435 436 437 438 439 440 441
	if ((port->xmit_cnt <= 0 && !port->break_length)
	    || tty->stopped || tty->hw_stopped)  {
		rc_out(bp, CD180_CAR, port_No(port));
		port->IER &= ~IER_TXRDY;
		rc_out(bp, CD180_IER, port->IER);
		return;
	}
A
Alan Cox 已提交
442

L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453
	if (port->break_length)  {
		if (port->break_length > 0)  {
			if (port->COR2 & COR2_ETC)  {
				rc_out(bp, CD180_TDR, CD180_C_ESC);
				rc_out(bp, CD180_TDR, CD180_C_SBRK);
				port->COR2 &= ~COR2_ETC;
			}
			count = min_t(int, port->break_length, 0xff);
			rc_out(bp, CD180_TDR, CD180_C_ESC);
			rc_out(bp, CD180_TDR, CD180_C_DELAY);
			rc_out(bp, CD180_TDR, count);
A
Alan Cox 已提交
454 455
			port->break_length -= count;
			if (port->break_length == 0)
L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466
				port->break_length--;
		} else  {
			rc_out(bp, CD180_TDR, CD180_C_ESC);
			rc_out(bp, CD180_TDR, CD180_C_EBRK);
			rc_out(bp, CD180_COR2, port->COR2);
			rc_wait_CCR(bp);
			rc_out(bp, CD180_CCR, CCR_CORCHG2);
			port->break_length = 0;
		}
		return;
	}
A
Alan Cox 已提交
467

L
Linus Torvalds 已提交
468 469
	count = CD180_NFIFO;
	do {
A
Alan Cox 已提交
470
		rc_out(bp, CD180_TDR, port->port.xmit_buf[port->xmit_tail++]);
L
Linus Torvalds 已提交
471 472 473 474
		port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
		if (--port->xmit_cnt <= 0)
			break;
	} while (--count > 0);
A
Alan Cox 已提交
475

L
Linus Torvalds 已提交
476 477 478 479 480 481
	if (port->xmit_cnt <= 0)  {
		rc_out(bp, CD180_CAR, port_No(port));
		port->IER &= ~IER_TXRDY;
		rc_out(bp, CD180_IER, port->IER);
	}
	if (port->xmit_cnt <= port->wakeup_chars)
482
		tty_wakeup(tty);
L
Linus Torvalds 已提交
483 484
}

A
Alan Cox 已提交
485
static void rc_check_modem(struct riscom_board const *bp)
L
Linus Torvalds 已提交
486 487 488 489
{
	struct riscom_port *port;
	struct tty_struct *tty;
	unsigned char mcr;
A
Alan Cox 已提交
490 491 492

	port = rc_get_port(bp, "Modem");
	if (port == NULL)
L
Linus Torvalds 已提交
493
		return;
A
Alan Cox 已提交
494

A
Alan Cox 已提交
495
	tty = port->port.tty;
A
Alan Cox 已提交
496

L
Linus Torvalds 已提交
497
	mcr = rc_in(bp, CD180_MCR);
A
Alan Cox 已提交
498 499
	if (mcr & MCR_CDCHG) {
		if (rc_in(bp, CD180_MSVR) & MSVR_CD)
A
Alan Cox 已提交
500
			wake_up_interruptible(&port->port.open_wait);
L
Linus Torvalds 已提交
501
		else
502
			tty_hangup(tty);
L
Linus Torvalds 已提交
503
	}
A
Alan Cox 已提交
504

L
Linus Torvalds 已提交
505 506 507 508 509 510
#ifdef RISCOM_BRAIN_DAMAGED_CTS
	if (mcr & MCR_CTSCHG)  {
		if (rc_in(bp, CD180_MSVR) & MSVR_CTS)  {
			tty->hw_stopped = 0;
			port->IER |= IER_TXRDY;
			if (port->xmit_cnt <= port->wakeup_chars)
511
				tty_wakeup(tty);
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521 522
		} else  {
			tty->hw_stopped = 1;
			port->IER &= ~IER_TXRDY;
		}
		rc_out(bp, CD180_IER, port->IER);
	}
	if (mcr & MCR_DSRCHG)  {
		if (rc_in(bp, CD180_MSVR) & MSVR_DSR)  {
			tty->hw_stopped = 0;
			port->IER |= IER_TXRDY;
			if (port->xmit_cnt <= port->wakeup_chars)
523
				tty_wakeup(tty);
L
Linus Torvalds 已提交
524 525 526 527 528 529 530
		} else  {
			tty->hw_stopped = 1;
			port->IER &= ~IER_TXRDY;
		}
		rc_out(bp, CD180_IER, port->IER);
	}
#endif /* RISCOM_BRAIN_DAMAGED_CTS */
A
Alan Cox 已提交
531

L
Linus Torvalds 已提交
532 533 534 535 536
	/* Clear change bits */
	rc_out(bp, CD180_MCR, 0);
}

/* The main interrupt processing routine */
A
Alan Cox 已提交
537
static irqreturn_t rc_interrupt(int dummy, void *dev_id)
L
Linus Torvalds 已提交
538 539 540
{
	unsigned char status;
	unsigned char ack;
541
	struct riscom_board *bp = dev_id;
L
Linus Torvalds 已提交
542 543 544
	unsigned long loop = 0;
	int handled = 0;

545
	if (!(bp->flags & RC_BOARD_ACTIVE))
L
Linus Torvalds 已提交
546
		return IRQ_NONE;
547

L
Linus Torvalds 已提交
548 549 550 551
	while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
				 (RC_BSR_TOUT | RC_BSR_TINT |
				  RC_BSR_MINT | RC_BSR_RINT))) {
		handled = 1;
A
Alan Cox 已提交
552
		if (status & RC_BSR_TOUT)
L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
			printk(KERN_WARNING "rc%d: Got timeout. Hardware "
					    "error?\n", board_No(bp));
		else if (status & RC_BSR_RINT) {
			ack = rc_in(bp, RC_ACK_RINT);
			if (ack == (RC_ID | GIVR_IT_RCV))
				rc_receive(bp);
			else if (ack == (RC_ID | GIVR_IT_REXC))
				rc_receive_exc(bp);
			else
				printk(KERN_WARNING "rc%d: Bad receive ack "
						    "0x%02x.\n",
				       board_No(bp), ack);
		} else if (status & RC_BSR_TINT) {
			ack = rc_in(bp, RC_ACK_TINT);
			if (ack == (RC_ID | GIVR_IT_TX))
				rc_transmit(bp);
			else
				printk(KERN_WARNING "rc%d: Bad transmit ack "
						    "0x%02x.\n",
				       board_No(bp), ack);
		} else /* if (status & RC_BSR_MINT) */ {
			ack = rc_in(bp, RC_ACK_MINT);
A
Alan Cox 已提交
575
			if (ack == (RC_ID | GIVR_IT_MODEM))
L
Linus Torvalds 已提交
576 577 578 579 580
				rc_check_modem(bp);
			else
				printk(KERN_WARNING "rc%d: Bad modem ack "
						    "0x%02x.\n",
				       board_No(bp), ack);
A
Alan Cox 已提交
581
		}
L
Linus Torvalds 已提交
582 583 584 585 586 587 588 589 590 591 592
		rc_out(bp, CD180_EOIR, 0);   /* Mark end of interrupt */
		rc_out(bp, RC_CTOUT, 0);     /* Clear timeout flag    */
	}
	return IRQ_RETVAL(handled);
}

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

/* Called with disabled interrupts */
A
Alan Cox 已提交
593
static int rc_setup_board(struct riscom_board *bp)
L
Linus Torvalds 已提交
594 595 596
{
	int error;

A
Alan Cox 已提交
597
	if (bp->flags & RC_BOARD_ACTIVE)
L
Linus Torvalds 已提交
598
		return 0;
A
Alan Cox 已提交
599

600
	error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
601
			    "RISCom/8", bp);
A
Alan Cox 已提交
602
	if (error)
L
Linus Torvalds 已提交
603
		return error;
A
Alan Cox 已提交
604

L
Linus Torvalds 已提交
605 606 607
	rc_out(bp, RC_CTOUT, 0);       		/* Just in case         */
	bp->DTR = ~0;
	rc_out(bp, RC_DTR, bp->DTR);	        /* Drop DTR on all ports */
A
Alan Cox 已提交
608

L
Linus Torvalds 已提交
609
	bp->flags |= RC_BOARD_ACTIVE;
A
Alan Cox 已提交
610

L
Linus Torvalds 已提交
611 612 613 614
	return 0;
}

/* Called with disabled interrupts */
615
static void rc_shutdown_board(struct riscom_board *bp)
L
Linus Torvalds 已提交
616 617 618
{
	if (!(bp->flags & RC_BOARD_ACTIVE))
		return;
A
Alan Cox 已提交
619

L
Linus Torvalds 已提交
620
	bp->flags &= ~RC_BOARD_ACTIVE;
A
Alan Cox 已提交
621

L
Linus Torvalds 已提交
622
	free_irq(bp->irq, NULL);
A
Alan Cox 已提交
623

L
Linus Torvalds 已提交
624 625
	bp->DTR = ~0;
	rc_out(bp, RC_DTR, bp->DTR);	       /* Drop DTR on all ports */
A
Alan Cox 已提交
626

L
Linus Torvalds 已提交
627 628 629
}

/*
A
Alan Cox 已提交
630
 * Setting up port characteristics.
L
Linus Torvalds 已提交
631 632 633 634
 * Must be called with disabled interrupts
 */
static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
{
A
Alan Cox 已提交
635
	struct tty_struct *tty = port->port.tty;
L
Linus Torvalds 已提交
636 637 638 639
	unsigned long baud;
	long tmp;
	unsigned char cor1 = 0, cor3 = 0;
	unsigned char mcor1 = 0, mcor2 = 0;
A
Alan Cox 已提交
640

L
Linus Torvalds 已提交
641 642 643
	port->IER  = 0;
	port->COR2 = 0;
	port->MSVR = MSVR_RTS;
A
Alan Cox 已提交
644

645
	baud = tty_get_baud_rate(tty);
A
Alan Cox 已提交
646

L
Linus Torvalds 已提交
647 648
	/* Select port on the board */
	rc_out(bp, CD180_CAR, port_No(port));
A
Alan Cox 已提交
649

650
	if (!baud)  {
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659
		/* Drop DTR & exit */
		bp->DTR |= (1u << port_No(port));
		rc_out(bp, RC_DTR, bp->DTR);
		return;
	} else  {
		/* Set DTR on */
		bp->DTR &= ~(1u << port_No(port));
		rc_out(bp, RC_DTR, bp->DTR);
	}
A
Alan Cox 已提交
660

L
Linus Torvalds 已提交
661
	/*
A
Alan Cox 已提交
662
	 * Now we must calculate some speed depended things
L
Linus Torvalds 已提交
663
	 */
A
Alan Cox 已提交
664

L
Linus Torvalds 已提交
665
	/* Set baud rate for port */
666
	tmp = (((RC_OSCFREQ + baud/2) / baud +
L
Linus Torvalds 已提交
667 668
		CD180_TPC/2) / CD180_TPC);

A
Alan Cox 已提交
669 670 671
	rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
	rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
	rc_out(bp, CD180_RBPRL, tmp & 0xff);
L
Linus Torvalds 已提交
672
	rc_out(bp, CD180_TBPRL, tmp & 0xff);
A
Alan Cox 已提交
673

674
	baud = (baud + 5) / 10;   /* Estimated CPS */
A
Alan Cox 已提交
675

L
Linus Torvalds 已提交
676
	/* Two timer ticks seems enough to wakeup something like SLIP driver */
A
Alan Cox 已提交
677
	tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
L
Linus Torvalds 已提交
678 679
	port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
					      SERIAL_XMIT_SIZE - 1 : tmp);
A
Alan Cox 已提交
680

L
Linus Torvalds 已提交
681 682 683 684
	/* Receiver timeout will be transmission time for 1.5 chars */
	tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
	tmp = (tmp > 0xff) ? 0xff : tmp;
	rc_out(bp, CD180_RTPR, tmp);
A
Alan Cox 已提交
685 686 687

	switch (C_CSIZE(tty)) {
	case CS5:
L
Linus Torvalds 已提交
688 689
		cor1 |= COR1_5BITS;
		break;
A
Alan Cox 已提交
690
	case CS6:
L
Linus Torvalds 已提交
691 692
		cor1 |= COR1_6BITS;
		break;
A
Alan Cox 已提交
693
	case CS7:
L
Linus Torvalds 已提交
694 695
		cor1 |= COR1_7BITS;
		break;
A
Alan Cox 已提交
696
	case CS8:
L
Linus Torvalds 已提交
697 698 699
		cor1 |= COR1_8BITS;
		break;
	}
A
Alan Cox 已提交
700
	if (C_CSTOPB(tty))
L
Linus Torvalds 已提交
701
		cor1 |= COR1_2SB;
A
Alan Cox 已提交
702

L
Linus Torvalds 已提交
703
	cor1 |= COR1_IGNORE;
A
Alan Cox 已提交
704
	if (C_PARENB(tty)) {
L
Linus Torvalds 已提交
705
		cor1 |= COR1_NORMPAR;
A
Alan Cox 已提交
706
		if (C_PARODD(tty))
L
Linus Torvalds 已提交
707
			cor1 |= COR1_ODDP;
A
Alan Cox 已提交
708
		if (I_INPCK(tty))
L
Linus Torvalds 已提交
709 710 711 712
			cor1 &= ~COR1_IGNORE;
	}
	/* Set marking of some errors */
	port->mark_mask = RCSR_OE | RCSR_TOUT;
A
Alan Cox 已提交
713
	if (I_INPCK(tty))
L
Linus Torvalds 已提交
714
		port->mark_mask |= RCSR_FE | RCSR_PE;
A
Alan Cox 已提交
715
	if (I_BRKINT(tty) || I_PARMRK(tty))
L
Linus Torvalds 已提交
716
		port->mark_mask |= RCSR_BREAK;
A
Alan Cox 已提交
717
	if (I_IGNPAR(tty))
L
Linus Torvalds 已提交
718
		port->mark_mask &= ~(RCSR_FE | RCSR_PE);
A
Alan Cox 已提交
719
	if (I_IGNBRK(tty)) {
L
Linus Torvalds 已提交
720
		port->mark_mask &= ~RCSR_BREAK;
A
Alan Cox 已提交
721
		if (I_IGNPAR(tty))
L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729 730
			/* Real raw mode. Ignore all */
			port->mark_mask &= ~RCSR_OE;
	}
	/* Enable Hardware Flow Control */
	if (C_CRTSCTS(tty))  {
#ifdef RISCOM_BRAIN_DAMAGED_CTS
		port->IER |= IER_DSR | IER_CTS;
		mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
		mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
A
Alan Cox 已提交
731 732
		tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
						(MSVR_CTS|MSVR_DSR));
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
#else
		port->COR2 |= COR2_CTSAE;
#endif
	}
	/* Enable Software Flow Control. FIXME: I'm not sure about this */
	/* Some people reported that it works, but I still doubt */
	if (I_IXON(tty))  {
		port->COR2 |= COR2_TXIBE;
		cor3 |= (COR3_FCT | COR3_SCDE);
		if (I_IXANY(tty))
			port->COR2 |= COR2_IXM;
		rc_out(bp, CD180_SCHR1, START_CHAR(tty));
		rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
		rc_out(bp, CD180_SCHR3, START_CHAR(tty));
		rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
	}
	if (!C_CLOCAL(tty))  {
		/* Enable CD check */
		port->IER |= IER_CD;
		mcor1 |= MCOR1_CDZD;
		mcor2 |= MCOR2_CDOD;
	}
A
Alan Cox 已提交
755 756

	if (C_CREAD(tty))
L
Linus Torvalds 已提交
757 758
		/* Enable receiver */
		port->IER |= IER_RXD;
A
Alan Cox 已提交
759

L
Linus Torvalds 已提交
760
	/* Set input FIFO size (1-8 bytes) */
A
Alan Cox 已提交
761
	cor3 |= RISCOM_RXFIFO;
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
	/* Setting up CD180 channel registers */
	rc_out(bp, CD180_COR1, cor1);
	rc_out(bp, CD180_COR2, port->COR2);
	rc_out(bp, CD180_COR3, cor3);
	/* Make CD180 know about registers change */
	rc_wait_CCR(bp);
	rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
	/* Setting up modem option registers */
	rc_out(bp, CD180_MCOR1, mcor1);
	rc_out(bp, CD180_MCOR2, mcor2);
	/* Enable CD180 transmitter & receiver */
	rc_wait_CCR(bp);
	rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
	/* Enable interrupts */
	rc_out(bp, CD180_IER, port->IER);
	/* And finally set RTS on */
	rc_out(bp, CD180_MSVR, port->MSVR);
}

/* Must be called with interrupts enabled */
static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
{
	unsigned long flags;
A
Alan Cox 已提交
785

A
Alan Cox 已提交
786
	if (port->port.flags & ASYNC_INITIALIZED)
L
Linus Torvalds 已提交
787
		return 0;
A
Alan Cox 已提交
788

A
Alan Cox 已提交
789 790 791
	if (tty_port_alloc_xmit_buf(&port->port) < 0)
		return -ENOMEM;

J
Jeff Garzik 已提交
792 793
	spin_lock_irqsave(&riscom_lock, flags);

A
Alan Cox 已提交
794
	clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
A
Alan Cox 已提交
795
	if (port->port.count == 1)
L
Linus Torvalds 已提交
796 797 798
		bp->count++;
	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
	rc_change_speed(bp, port);
A
Alan Cox 已提交
799
	port->port.flags |= ASYNC_INITIALIZED;
A
Alan Cox 已提交
800

J
Jeff Garzik 已提交
801
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
802 803 804 805
	return 0;
}

/* Must be called with interrupts disabled */
A
Alan Cox 已提交
806 807
static void rc_shutdown_port(struct tty_struct *tty,
			struct riscom_board *bp, struct riscom_port *port)
L
Linus Torvalds 已提交
808
{
A
Alan Cox 已提交
809
	if (!(port->port.flags & ASYNC_INITIALIZED))
L
Linus Torvalds 已提交
810
		return;
A
Alan Cox 已提交
811

L
Linus Torvalds 已提交
812 813 814
#ifdef RC_REPORT_OVERRUN
	printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
	       board_No(bp), port_No(port), port->overrun);
A
Alan Cox 已提交
815
#endif
L
Linus Torvalds 已提交
816 817 818
#ifdef RC_REPORT_FIFO
	{
		int i;
A
Alan Cox 已提交
819

L
Linus Torvalds 已提交
820 821
		printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
		       board_No(bp), port_No(port));
A
Alan Cox 已提交
822
		for (i = 0; i < 10; i++)
L
Linus Torvalds 已提交
823 824 825
			printk("%ld ", port->hits[i]);
		printk("].\n");
	}
A
Alan Cox 已提交
826
#endif
A
Alan Cox 已提交
827
	tty_port_free_xmit_buf(&port->port);
A
Alan Cox 已提交
828
	if (C_HUPCL(tty)) {
L
Linus Torvalds 已提交
829 830 831 832
		/* Drop DTR */
		bp->DTR |= (1u << port_No(port));
		rc_out(bp, RC_DTR, bp->DTR);
	}
A
Alan Cox 已提交
833 834

	/* Select port */
L
Linus Torvalds 已提交
835 836 837 838 839 840 841
	rc_out(bp, CD180_CAR, port_No(port));
	/* Reset port */
	rc_wait_CCR(bp);
	rc_out(bp, CD180_CCR, CCR_SOFTRESET);
	/* Disable all interrupts from this port */
	port->IER = 0;
	rc_out(bp, CD180_IER, port->IER);
A
Alan Cox 已提交
842

A
Alan Cox 已提交
843
	set_bit(TTY_IO_ERROR, &tty->flags);
A
Alan Cox 已提交
844
	port->port.flags &= ~ASYNC_INITIALIZED;
A
Alan Cox 已提交
845

L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854 855
	if (--bp->count < 0)  {
		printk(KERN_INFO "rc%d: rc_shutdown_port: "
				 "bad board count: %d\n",
		       board_No(bp), bp->count);
		bp->count = 0;
	}
	/*
	 * If this is the last opened port on the board
	 * shutdown whole board
	 */
A
Alan Cox 已提交
856
	if (!bp->count)
L
Linus Torvalds 已提交
857 858 859
		rc_shutdown_board(bp);
}

A
Alan Cox 已提交
860
static int block_til_ready(struct tty_struct *tty, struct file *filp,
L
Linus Torvalds 已提交
861 862 863 864 865 866 867
			   struct riscom_port *port)
{
	DECLARE_WAITQUEUE(wait, current);
	struct riscom_board *bp = port_Board(port);
	int    retval;
	int    do_clocal = 0;
	int    CD;
J
Jeff Garzik 已提交
868
	unsigned long flags;
L
Linus Torvalds 已提交
869 870 871 872 873

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
A
Alan Cox 已提交
874 875 876
	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 已提交
877 878 879 880 881 882 883 884 885 886 887
			return -EAGAIN;
		else
			return -ERESTARTSYS;
	}

	/*
	 * 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 已提交
888
		port->port.flags |= ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
889 890 891
		return 0;
	}

A
Alan Cox 已提交
892
	if (C_CLOCAL(tty))
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902
		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 已提交
903
	add_wait_queue(&port->port.open_wait, &wait);
J
Jeff Garzik 已提交
904 905 906

	spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
907
	if (!tty_hung_up_p(filp))
A
Alan Cox 已提交
908
		port->port.count--;
J
Jeff Garzik 已提交
909 910 911

	spin_unlock_irqrestore(&riscom_lock, flags);

A
Alan Cox 已提交
912
	port->port.blocked_open++;
L
Linus Torvalds 已提交
913
	while (1) {
J
Jeff Garzik 已提交
914 915
		spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
916 917 918 919 920
		rc_out(bp, CD180_CAR, port_No(port));
		CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
		rc_out(bp, CD180_MSVR, MSVR_RTS);
		bp->DTR &= ~(1u << port_No(port));
		rc_out(bp, RC_DTR, bp->DTR);
J
Jeff Garzik 已提交
921 922 923

		spin_unlock_irqrestore(&riscom_lock, flags);

L
Linus Torvalds 已提交
924 925
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
A
Alan Cox 已提交
926 927
		    !(port->port.flags & ASYNC_INITIALIZED)) {
			if (port->port.flags & ASYNC_HUP_NOTIFY)
L
Linus Torvalds 已提交
928 929
				retval = -EAGAIN;
			else
A
Alan Cox 已提交
930
				retval = -ERESTARTSYS;
L
Linus Torvalds 已提交
931 932
			break;
		}
A
Alan Cox 已提交
933
		if (!(port->port.flags & ASYNC_CLOSING) &&
L
Linus Torvalds 已提交
934 935 936 937 938 939 940 941
		    (do_clocal || CD))
			break;
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
		schedule();
	}
942
	__set_current_state(TASK_RUNNING);
A
Alan Cox 已提交
943
	remove_wait_queue(&port->port.open_wait, &wait);
L
Linus Torvalds 已提交
944
	if (!tty_hung_up_p(filp))
A
Alan Cox 已提交
945 946
		port->port.count++;
	port->port.blocked_open--;
L
Linus Torvalds 已提交
947 948
	if (retval)
		return retval;
A
Alan Cox 已提交
949

A
Alan Cox 已提交
950
	port->port.flags |= ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
951
	return 0;
A
Alan Cox 已提交
952
}
L
Linus Torvalds 已提交
953

A
Alan Cox 已提交
954
static int rc_open(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
955 956 957
{
	int board;
	int error;
A
Alan Cox 已提交
958 959 960
	struct riscom_port *port;
	struct riscom_board *bp;

L
Linus Torvalds 已提交
961 962 963
	board = RC_BOARD(tty->index);
	if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
		return -ENODEV;
A
Alan Cox 已提交
964

L
Linus Torvalds 已提交
965 966 967 968
	bp = &rc_board[board];
	port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
	if (rc_paranoia_check(port, tty->name, "rc_open"))
		return -ENODEV;
A
Alan Cox 已提交
969 970 971

	error = rc_setup_board(bp);
	if (error)
L
Linus Torvalds 已提交
972
		return error;
A
Alan Cox 已提交
973

A
Alan Cox 已提交
974
	port->port.count++;
L
Linus Torvalds 已提交
975
	tty->driver_data = port;
A
Alan Cox 已提交
976
	port->port.tty = tty;
A
Alan Cox 已提交
977 978 979 980 981

	error = rc_setup_port(bp, port);
	if (error == 0)
		error = block_til_ready(tty, filp, port);
	return error;
L
Linus Torvalds 已提交
982 983
}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
static void rc_flush_buffer(struct tty_struct *tty)
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	unsigned long flags;

	if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
		return;

	spin_lock_irqsave(&riscom_lock, flags);
	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
	spin_unlock_irqrestore(&riscom_lock, flags);

	tty_wakeup(tty);
}

A
Alan Cox 已提交
999
static void rc_close(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004
{
	struct riscom_port *port = (struct riscom_port *) tty->driver_data;
	struct riscom_board *bp;
	unsigned long flags;
	unsigned long timeout;
A
Alan Cox 已提交
1005

L
Linus Torvalds 已提交
1006 1007
	if (!port || rc_paranoia_check(port, tty->name, "close"))
		return;
J
Jeff Garzik 已提交
1008 1009 1010

	spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
1011 1012
	if (tty_hung_up_p(filp))
		goto out;
A
Alan Cox 已提交
1013

L
Linus Torvalds 已提交
1014
	bp = port_Board(port);
A
Alan Cox 已提交
1015
	if ((tty->count == 1) && (port->port.count != 1))  {
L
Linus Torvalds 已提交
1016 1017
		printk(KERN_INFO "rc%d: rc_close: bad port count;"
		       " tty->count is 1, port count is %d\n",
A
Alan Cox 已提交
1018 1019
		       board_No(bp), port->port.count);
		port->port.count = 1;
L
Linus Torvalds 已提交
1020
	}
A
Alan Cox 已提交
1021
	if (--port->port.count < 0)  {
L
Linus Torvalds 已提交
1022 1023
		printk(KERN_INFO "rc%d: rc_close: bad port count "
				 "for tty%d: %d\n",
A
Alan Cox 已提交
1024 1025
		       board_No(bp), port_No(port), port->port.count);
		port->port.count = 0;
L
Linus Torvalds 已提交
1026
	}
A
Alan Cox 已提交
1027
	if (port->port.count)
L
Linus Torvalds 已提交
1028
		goto out;
A
Alan Cox 已提交
1029
	port->port.flags |= ASYNC_CLOSING;
L
Linus Torvalds 已提交
1030
	/*
A
Alan Cox 已提交
1031
	 * Now we wait for the transmit buffer to clear; and we notify
L
Linus Torvalds 已提交
1032 1033 1034
	 * the line discipline to only process XON/XOFF characters.
	 */
	tty->closing = 1;
A
Alan Cox 已提交
1035 1036
	if (port->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
		tty_wait_until_sent(tty, port->port.closing_wait);
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042 1043
	/*
	 * 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.
	 */
	port->IER &= ~IER_RXD;
A
Alan Cox 已提交
1044
	if (port->port.flags & ASYNC_INITIALIZED) {
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053
		port->IER &= ~IER_TXRDY;
		port->IER |= IER_TXEMPTY;
		rc_out(bp, CD180_CAR, port_No(port));
		rc_out(bp, CD180_IER, port->IER);
		/*
		 * Before we drop DTR, make sure the UART transmitter
		 * has completely drained; this is especially
		 * important if there is a transmit FIFO!
		 */
A
Alan Cox 已提交
1054 1055
		timeout = jiffies + HZ;
		while (port->IER & IER_TXEMPTY) {
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060
			msleep_interruptible(jiffies_to_msecs(port->timeout));
			if (time_after(jiffies, timeout))
				break;
		}
	}
A
Alan Cox 已提交
1061
	rc_shutdown_port(tty, bp, port);
1062
	rc_flush_buffer(tty);
L
Linus Torvalds 已提交
1063 1064 1065
	tty_ldisc_flush(tty);

	tty->closing = 0;
A
Alan Cox 已提交
1066 1067
	port->port.tty = NULL;
	if (port->port.blocked_open) {
A
Alan Cox 已提交
1068 1069
		if (port->port.close_delay)
			msleep_interruptible(jiffies_to_msecs(port->port.close_delay));
A
Alan Cox 已提交
1070
		wake_up_interruptible(&port->port.open_wait);
L
Linus Torvalds 已提交
1071
	}
A
Alan Cox 已提交
1072 1073
	port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
	wake_up_interruptible(&port->port.close_wait);
J
Jeff Garzik 已提交
1074 1075 1076

out:
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1077 1078
}

A
Alan Cox 已提交
1079
static int rc_write(struct tty_struct *tty,
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085
		    const unsigned char *buf, int count)
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	struct riscom_board *bp;
	int c, total = 0;
	unsigned long flags;
A
Alan Cox 已提交
1086

L
Linus Torvalds 已提交
1087 1088
	if (rc_paranoia_check(port, tty->name, "rc_write"))
		return 0;
A
Alan Cox 已提交
1089

L
Linus Torvalds 已提交
1090 1091 1092
	bp = port_Board(port);

	while (1) {
J
Jeff Garzik 已提交
1093 1094
		spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
1095 1096
		c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
					  SERIAL_XMIT_SIZE - port->xmit_head));
J
Jeff Garzik 已提交
1097 1098
		if (c <= 0)
			break;	/* lock continues to be held */
L
Linus Torvalds 已提交
1099

A
Alan Cox 已提交
1100
		memcpy(port->port.xmit_buf + port->xmit_head, buf, c);
L
Linus Torvalds 已提交
1101 1102
		port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
		port->xmit_cnt += c;
J
Jeff Garzik 已提交
1103 1104

		spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

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

	if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
	    !(port->IER & IER_TXRDY)) {
		port->IER |= IER_TXRDY;
		rc_out(bp, CD180_CAR, port_No(port));
		rc_out(bp, CD180_IER, port->IER);
	}
J
Jeff Garzik 已提交
1117 1118

	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1119 1120 1121 1122

	return total;
}

A
Alan Cox 已提交
1123
static int rc_put_char(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
1124 1125 1126
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	unsigned long flags;
1127
	int ret = 0;
L
Linus Torvalds 已提交
1128 1129

	if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1130
		return 0;
L
Linus Torvalds 已提交
1131

J
Jeff Garzik 已提交
1132
	spin_lock_irqsave(&riscom_lock, flags);
A
Alan Cox 已提交
1133

L
Linus Torvalds 已提交
1134 1135 1136
	if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
		goto out;

A
Alan Cox 已提交
1137
	port->port.xmit_buf[port->xmit_head++] = ch;
L
Linus Torvalds 已提交
1138 1139
	port->xmit_head &= SERIAL_XMIT_SIZE - 1;
	port->xmit_cnt++;
1140
	ret = 1;
J
Jeff Garzik 已提交
1141 1142 1143

out:
	spin_unlock_irqrestore(&riscom_lock, flags);
1144
	return ret;
L
Linus Torvalds 已提交
1145 1146
}

A
Alan Cox 已提交
1147
static void rc_flush_chars(struct tty_struct *tty)
L
Linus Torvalds 已提交
1148 1149 1150
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	unsigned long flags;
A
Alan Cox 已提交
1151

L
Linus Torvalds 已提交
1152 1153
	if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
		return;
A
Alan Cox 已提交
1154

A
Alan Cox 已提交
1155
	if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped)
L
Linus Torvalds 已提交
1156 1157
		return;

J
Jeff Garzik 已提交
1158 1159
	spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
1160 1161 1162
	port->IER |= IER_TXRDY;
	rc_out(port_Board(port), CD180_CAR, port_No(port));
	rc_out(port_Board(port), CD180_IER, port->IER);
J
Jeff Garzik 已提交
1163 1164

	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1165 1166
}

A
Alan Cox 已提交
1167
static int rc_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1168 1169 1170
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	int	ret;
A
Alan Cox 已提交
1171

L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	if (rc_paranoia_check(port, tty->name, "rc_write_room"))
		return 0;

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

static int rc_chars_in_buffer(struct tty_struct *tty)
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
A
Alan Cox 已提交
1184

L
Linus Torvalds 已提交
1185 1186
	if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
		return 0;
A
Alan Cox 已提交
1187

L
Linus Torvalds 已提交
1188 1189 1190 1191 1192 1193
	return port->xmit_cnt;
}

static int rc_tiocmget(struct tty_struct *tty, struct file *file)
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
A
Alan Cox 已提交
1194
	struct riscom_board *bp;
L
Linus Torvalds 已提交
1195 1196 1197 1198
	unsigned char status;
	unsigned int result;
	unsigned long flags;

1199
	if (rc_paranoia_check(port, tty->name, __func__))
L
Linus Torvalds 已提交
1200 1201 1202
		return -ENODEV;

	bp = port_Board(port);
J
Jeff Garzik 已提交
1203 1204 1205

	spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
1206 1207 1208
	rc_out(bp, CD180_CAR, port_No(port));
	status = rc_in(bp, CD180_MSVR);
	result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
J
Jeff Garzik 已提交
1209 1210 1211

	spin_unlock_irqrestore(&riscom_lock, flags);

L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
		| ((status & MSVR_DTR) ? TIOCM_DTR : 0)
		| ((status & MSVR_CD)  ? TIOCM_CAR : 0)
		| ((status & MSVR_DSR) ? TIOCM_DSR : 0)
		| ((status & MSVR_CTS) ? TIOCM_CTS : 0);
	return result;
}

static int rc_tiocmset(struct tty_struct *tty, struct file *file,
		       unsigned int set, unsigned int clear)
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	unsigned long flags;
	struct riscom_board *bp;

1227
	if (rc_paranoia_check(port, tty->name, __func__))
L
Linus Torvalds 已提交
1228 1229 1230 1231
		return -ENODEV;

	bp = port_Board(port);

J
Jeff Garzik 已提交
1232 1233
	spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	if (set & TIOCM_RTS)
		port->MSVR |= MSVR_RTS;
	if (set & TIOCM_DTR)
		bp->DTR &= ~(1u << port_No(port));

	if (clear & TIOCM_RTS)
		port->MSVR &= ~MSVR_RTS;
	if (clear & TIOCM_DTR)
		bp->DTR |= (1u << port_No(port));

	rc_out(bp, CD180_CAR, port_No(port));
	rc_out(bp, CD180_MSVR, port->MSVR);
	rc_out(bp, RC_DTR, bp->DTR);
J
Jeff Garzik 已提交
1247 1248 1249

	spin_unlock_irqrestore(&riscom_lock, flags);

L
Linus Torvalds 已提交
1250 1251 1252
	return 0;
}

A
Alan Cox 已提交
1253
static void rc_send_break(struct riscom_port *port, unsigned long length)
L
Linus Torvalds 已提交
1254 1255 1256
{
	struct riscom_board *bp = port_Board(port);
	unsigned long flags;
A
Alan Cox 已提交
1257

J
Jeff Garzik 已提交
1258 1259
	spin_lock_irqsave(&riscom_lock, flags);

L
Linus Torvalds 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268
	port->break_length = RISCOM_TPS / HZ * length;
	port->COR2 |= COR2_ETC;
	port->IER  |= IER_TXRDY;
	rc_out(bp, CD180_CAR, port_No(port));
	rc_out(bp, CD180_COR2, port->COR2);
	rc_out(bp, CD180_IER, port->IER);
	rc_wait_CCR(bp);
	rc_out(bp, CD180_CCR, CCR_CORCHG2);
	rc_wait_CCR(bp);
J
Jeff Garzik 已提交
1269 1270

	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1271 1272
}

A
Alan Cox 已提交
1273 1274
static int rc_set_serial_info(struct riscom_port *port,
				     struct serial_struct __user *newinfo)
L
Linus Torvalds 已提交
1275 1276 1277 1278
{
	struct serial_struct tmp;
	struct riscom_board *bp = port_Board(port);
	int change_speed;
A
Alan Cox 已提交
1279

L
Linus Torvalds 已提交
1280 1281
	if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
		return -EFAULT;
A
Alan Cox 已提交
1282 1283

#if 0
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290 1291
	if ((tmp.irq != bp->irq) ||
	    (tmp.port != bp->base) ||
	    (tmp.type != PORT_CIRRUS) ||
	    (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
	    (tmp.custom_divisor != 0) ||
	    (tmp.xmit_fifo_size != CD180_NFIFO) ||
	    (tmp.flags & ~RISCOM_LEGAL_FLAGS))
		return -EINVAL;
A
Alan Cox 已提交
1292 1293
#endif

A
Alan Cox 已提交
1294
	change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
L
Linus Torvalds 已提交
1295
			(tmp.flags & ASYNC_SPD_MASK));
A
Alan Cox 已提交
1296

L
Linus Torvalds 已提交
1297
	if (!capable(CAP_SYS_ADMIN)) {
A
Alan Cox 已提交
1298 1299
		if ((tmp.close_delay != port->port.close_delay) ||
		    (tmp.closing_wait != port->port.closing_wait) ||
L
Linus Torvalds 已提交
1300
		    ((tmp.flags & ~ASYNC_USR_MASK) !=
A
Alan Cox 已提交
1301
		     (port->port.flags & ~ASYNC_USR_MASK)))
L
Linus Torvalds 已提交
1302
			return -EPERM;
A
Alan Cox 已提交
1303
		port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
L
Linus Torvalds 已提交
1304 1305
			       (tmp.flags & ASYNC_USR_MASK));
	} else  {
A
Alan Cox 已提交
1306
		port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
L
Linus Torvalds 已提交
1307
			       (tmp.flags & ASYNC_FLAGS));
A
Alan Cox 已提交
1308 1309
		port->port.close_delay = tmp.close_delay;
		port->port.closing_wait = tmp.closing_wait;
L
Linus Torvalds 已提交
1310 1311
	}
	if (change_speed)  {
J
Jeff Garzik 已提交
1312 1313 1314
		unsigned long flags;

		spin_lock_irqsave(&riscom_lock, flags);
L
Linus Torvalds 已提交
1315
		rc_change_speed(bp, port);
J
Jeff Garzik 已提交
1316
		spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1317 1318 1319 1320
	}
	return 0;
}

A
Alan Cox 已提交
1321
static int rc_get_serial_info(struct riscom_port *port,
L
Linus Torvalds 已提交
1322 1323 1324 1325
				     struct serial_struct __user *retinfo)
{
	struct serial_struct tmp;
	struct riscom_board *bp = port_Board(port);
A
Alan Cox 已提交
1326

L
Linus Torvalds 已提交
1327 1328 1329 1330 1331
	memset(&tmp, 0, sizeof(tmp));
	tmp.type = PORT_CIRRUS;
	tmp.line = port - rc_port;
	tmp.port = bp->base;
	tmp.irq  = bp->irq;
A
Alan Cox 已提交
1332
	tmp.flags = port->port.flags;
L
Linus Torvalds 已提交
1333
	tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
A
Alan Cox 已提交
1334 1335
	tmp.close_delay = port->port.close_delay * HZ/100;
	tmp.closing_wait = port->port.closing_wait * HZ/100;
L
Linus Torvalds 已提交
1336 1337 1338 1339
	tmp.xmit_fifo_size = CD180_NFIFO;
	return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
}

A
Alan Cox 已提交
1340
static int rc_ioctl(struct tty_struct *tty, struct file *filp,
L
Linus Torvalds 已提交
1341 1342 1343 1344
		    unsigned int cmd, unsigned long arg)
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	void __user *argp = (void __user *)arg;
A
Alan Cox 已提交
1345
	int retval = 0;
A
Alan Cox 已提交
1346

L
Linus Torvalds 已提交
1347 1348
	if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
		return -ENODEV;
A
Alan Cox 已提交
1349

L
Linus Torvalds 已提交
1350
	switch (cmd) {
A
Alan Cox 已提交
1351
	case TCSBRK:	/* SVID version: non-zero arg --> no break */
L
Linus Torvalds 已提交
1352 1353 1354 1355 1356 1357 1358
		retval = tty_check_change(tty);
		if (retval)
			return retval;
		tty_wait_until_sent(tty, 0);
		if (!arg)
			rc_send_break(port, HZ/4);	/* 1/4 second */
		break;
A
Alan Cox 已提交
1359
	case TCSBRKP:	/* support for POSIX tcsendbreak() */
L
Linus Torvalds 已提交
1360 1361 1362 1363 1364 1365
		retval = tty_check_change(tty);
		if (retval)
			return retval;
		tty_wait_until_sent(tty, 0);
		rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
		break;
A
Alan Cox 已提交
1366 1367
	case TIOCGSERIAL:
		lock_kernel();
A
Alan Cox 已提交
1368 1369
		retval = rc_get_serial_info(port, argp);
		unlock_kernel();
L
Linus Torvalds 已提交
1370
		break;
A
Alan Cox 已提交
1371 1372
	case TIOCSSERIAL:
		lock_kernel();
A
Alan Cox 已提交
1373 1374 1375
		retval = rc_set_serial_info(port, argp);
		unlock_kernel();
		break;
A
Alan Cox 已提交
1376
	default:
A
Alan Cox 已提交
1377
		retval = -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1378
	}
A
Alan Cox 已提交
1379
	return retval;
L
Linus Torvalds 已提交
1380 1381
}

A
Alan Cox 已提交
1382
static void rc_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1383 1384 1385 1386
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	struct riscom_board *bp;
	unsigned long flags;
A
Alan Cox 已提交
1387

L
Linus Torvalds 已提交
1388 1389 1390
	if (rc_paranoia_check(port, tty->name, "rc_throttle"))
		return;
	bp = port_Board(port);
J
Jeff Garzik 已提交
1391 1392

	spin_lock_irqsave(&riscom_lock, flags);
L
Linus Torvalds 已提交
1393 1394
	port->MSVR &= ~MSVR_RTS;
	rc_out(bp, CD180_CAR, port_No(port));
J
Jeff Garzik 已提交
1395
	if (I_IXOFF(tty)) {
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400
		rc_wait_CCR(bp);
		rc_out(bp, CD180_CCR, CCR_SSCH2);
		rc_wait_CCR(bp);
	}
	rc_out(bp, CD180_MSVR, port->MSVR);
J
Jeff Garzik 已提交
1401
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1402 1403
}

A
Alan Cox 已提交
1404
static void rc_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1405 1406 1407 1408
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	struct riscom_board *bp;
	unsigned long flags;
A
Alan Cox 已提交
1409

L
Linus Torvalds 已提交
1410 1411 1412
	if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
		return;
	bp = port_Board(port);
J
Jeff Garzik 已提交
1413

A
Alan Cox 已提交
1414
	spin_lock_irqsave(&riscom_lock, flags);
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420 1421 1422
	port->MSVR |= MSVR_RTS;
	rc_out(bp, CD180_CAR, port_No(port));
	if (I_IXOFF(tty))  {
		rc_wait_CCR(bp);
		rc_out(bp, CD180_CCR, CCR_SSCH1);
		rc_wait_CCR(bp);
	}
	rc_out(bp, CD180_MSVR, port->MSVR);
J
Jeff Garzik 已提交
1423
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1424 1425
}

A
Alan Cox 已提交
1426
static void rc_stop(struct tty_struct *tty)
L
Linus Torvalds 已提交
1427 1428 1429 1430
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	struct riscom_board *bp;
	unsigned long flags;
A
Alan Cox 已提交
1431

L
Linus Torvalds 已提交
1432 1433
	if (rc_paranoia_check(port, tty->name, "rc_stop"))
		return;
A
Alan Cox 已提交
1434

L
Linus Torvalds 已提交
1435
	bp = port_Board(port);
J
Jeff Garzik 已提交
1436

A
Alan Cox 已提交
1437
	spin_lock_irqsave(&riscom_lock, flags);
L
Linus Torvalds 已提交
1438 1439 1440
	port->IER &= ~IER_TXRDY;
	rc_out(bp, CD180_CAR, port_No(port));
	rc_out(bp, CD180_IER, port->IER);
J
Jeff Garzik 已提交
1441
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1442 1443
}

A
Alan Cox 已提交
1444
static void rc_start(struct tty_struct *tty)
L
Linus Torvalds 已提交
1445 1446 1447 1448
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	struct riscom_board *bp;
	unsigned long flags;
A
Alan Cox 已提交
1449

L
Linus Torvalds 已提交
1450 1451
	if (rc_paranoia_check(port, tty->name, "rc_start"))
		return;
A
Alan Cox 已提交
1452

L
Linus Torvalds 已提交
1453
	bp = port_Board(port);
A
Alan Cox 已提交
1454

J
Jeff Garzik 已提交
1455 1456
	spin_lock_irqsave(&riscom_lock, flags);

A
Alan Cox 已提交
1457
	if (port->xmit_cnt && port->port.xmit_buf && !(port->IER & IER_TXRDY)) {
L
Linus Torvalds 已提交
1458 1459 1460 1461
		port->IER |= IER_TXRDY;
		rc_out(bp, CD180_CAR, port_No(port));
		rc_out(bp, CD180_IER, port->IER);
	}
J
Jeff Garzik 已提交
1462
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1463 1464
}

A
Alan Cox 已提交
1465
static void rc_hangup(struct tty_struct *tty)
L
Linus Torvalds 已提交
1466 1467 1468
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	struct riscom_board *bp;
A
Alan Cox 已提交
1469

L
Linus Torvalds 已提交
1470 1471
	if (rc_paranoia_check(port, tty->name, "rc_hangup"))
		return;
A
Alan Cox 已提交
1472

L
Linus Torvalds 已提交
1473
	bp = port_Board(port);
A
Alan Cox 已提交
1474

A
Alan Cox 已提交
1475
	rc_shutdown_port(tty, bp, port);
A
Alan Cox 已提交
1476 1477 1478 1479
	port->port.count = 0;
	port->port.flags &= ~ASYNC_NORMAL_ACTIVE;
	port->port.tty = NULL;
	wake_up_interruptible(&port->port.open_wait);
L
Linus Torvalds 已提交
1480 1481
}

A
Alan Cox 已提交
1482 1483
static void rc_set_termios(struct tty_struct *tty,
					struct ktermios *old_termios)
L
Linus Torvalds 已提交
1484 1485 1486
{
	struct riscom_port *port = (struct riscom_port *)tty->driver_data;
	unsigned long flags;
A
Alan Cox 已提交
1487

L
Linus Torvalds 已提交
1488 1489 1490
	if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
		return;

J
Jeff Garzik 已提交
1491
	spin_lock_irqsave(&riscom_lock, flags);
L
Linus Torvalds 已提交
1492
	rc_change_speed(port_Board(port), port);
J
Jeff Garzik 已提交
1493
	spin_unlock_irqrestore(&riscom_lock, flags);
L
Linus Torvalds 已提交
1494 1495 1496 1497 1498 1499 1500 1501

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

J
Jeff Dike 已提交
1502
static const struct tty_operations riscom_ops = {
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	.open  = rc_open,
	.close = rc_close,
	.write = rc_write,
	.put_char = rc_put_char,
	.flush_chars = rc_flush_chars,
	.write_room = rc_write_room,
	.chars_in_buffer = rc_chars_in_buffer,
	.flush_buffer = rc_flush_buffer,
	.ioctl = rc_ioctl,
	.throttle = rc_throttle,
	.unthrottle = rc_unthrottle,
	.set_termios = rc_set_termios,
	.stop = rc_stop,
	.start = rc_start,
	.hangup = rc_hangup,
	.tiocmget = rc_tiocmget,
	.tiocmset = rc_tiocmset,
};

1522
static int __init rc_init_drivers(void)
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527
{
	int error;
	int i;

	riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
A
Alan Cox 已提交
1528
	if (!riscom_driver)
L
Linus Torvalds 已提交
1529
		return -ENOMEM;
A
Alan Cox 已提交
1530

L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538
	riscom_driver->owner = THIS_MODULE;
	riscom_driver->name = "ttyL";
	riscom_driver->major = RISCOM8_NORMAL_MAJOR;
	riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
	riscom_driver->subtype = SERIAL_TYPE_NORMAL;
	riscom_driver->init_termios = tty_std_termios;
	riscom_driver->init_termios.c_cflag =
		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
A
Alan Cox 已提交
1539 1540
	riscom_driver->init_termios.c_ispeed = 9600;
	riscom_driver->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
1541 1542
	riscom_driver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(riscom_driver, &riscom_ops);
A
Alan Cox 已提交
1543 1544
	error = tty_register_driver(riscom_driver);
	if (error != 0) {
L
Linus Torvalds 已提交
1545 1546
		put_tty_driver(riscom_driver);
		printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
A
Alan Cox 已提交
1547
				"error = %d\n", error);
L
Linus Torvalds 已提交
1548 1549 1550 1551
		return 1;
	}
	memset(rc_port, 0, sizeof(rc_port));
	for (i = 0; i < RC_NPORT * RC_NBOARD; i++)  {
A
Alan Cox 已提交
1552
		tty_port_init(&rc_port[i].port);
A
Alan Cox 已提交
1553
		rc_port[i].magic = RISCOM8_MAGIC;
L
Linus Torvalds 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	}
	return 0;
}

static void rc_release_drivers(void)
{
	tty_unregister_driver(riscom_driver);
	put_tty_driver(riscom_driver);
}

#ifndef MODULE
/*
 * Called at boot time.
A
Alan Cox 已提交
1567
 *
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572
 * You can specify IO base for up to RC_NBOARD cards,
 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
 * Note that there will be no probing at default
 * addresses in this case.
 *
A
Alan Cox 已提交
1573
 */
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
static int __init riscom8_setup(char *str)
{
	int ints[RC_NBOARD];
	int i;

	str = get_options(str, ARRAY_SIZE(ints), ints);

	for (i = 0; i < RC_NBOARD; i++) {
		if (i < ints[0])
			rc_board[i].base = ints[i+1];
A
Alan Cox 已提交
1584
		else
L
Linus Torvalds 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
			rc_board[i].base = 0;
	}
	return 1;
}

__setup("riscom8=", riscom8_setup);
#endif

static char banner[] __initdata =
	KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
		  "1994-1996.\n";
static char no_boards_msg[] __initdata =
	KERN_INFO "rc: No RISCom/8 boards detected.\n";

A
Alan Cox 已提交
1599 1600
/*
 * This routine must be called by kernel at boot time
L
Linus Torvalds 已提交
1601 1602 1603 1604 1605 1606 1607 1608
 */
static int __init riscom8_init(void)
{
	int i;
	int found = 0;

	printk(banner);

A
Alan Cox 已提交
1609
	if (rc_init_drivers())
L
Linus Torvalds 已提交
1610 1611
		return -EIO;

A
Alan Cox 已提交
1612 1613
	for (i = 0; i < RC_NBOARD; i++)
		if (rc_board[i].base && !rc_probe(&rc_board[i]))
L
Linus Torvalds 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
			found++;
	if (!found)  {
		rc_release_drivers();
		printk(no_boards_msg);
		return -EIO;
	}
	return 0;
}

#ifdef MODULE
static int iobase;
static int iobase1;
static int iobase2;
static int iobase3;
R
Rusty Russell 已提交
1628 1629 1630 1631
module_param(iobase, int, 0);
module_param(iobase1, int, 0);
module_param(iobase2, int, 0);
module_param(iobase3, int, 0);
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640

MODULE_LICENSE("GPL");
#endif /* MODULE */

/*
 * You can setup up to 4 boards (current value of RC_NBOARD)
 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
 *
 */
A
Alan Cox 已提交
1641
static int __init riscom8_init_module(void)
L
Linus Torvalds 已提交
1642 1643 1644 1645 1646
{
#ifdef MODULE
	int i;

	if (iobase || iobase1 || iobase2 || iobase3) {
A
Alan Cox 已提交
1647
		for (i = 0; i < RC_NBOARD; i++)
J
Jiri Slaby 已提交
1648
			rc_board[i].base = 0;
L
Linus Torvalds 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
	}

	if (iobase)
		rc_board[0].base = iobase;
	if (iobase1)
		rc_board[1].base = iobase1;
	if (iobase2)
		rc_board[2].base = iobase2;
	if (iobase3)
		rc_board[3].base = iobase3;
#endif /* MODULE */

	return riscom8_init();
}
A
Alan Cox 已提交
1663 1664

static void __exit riscom8_exit_module(void)
L
Linus Torvalds 已提交
1665 1666
{
	int i;
A
Alan Cox 已提交
1667

L
Linus Torvalds 已提交
1668
	rc_release_drivers();
A
Alan Cox 已提交
1669 1670
	for (i = 0; i < RC_NBOARD; i++)
		if (rc_board[i].flags & RC_BOARD_PRESENT)
L
Linus Torvalds 已提交
1671
			rc_release_io_range(&rc_board[i]);
A
Alan Cox 已提交
1672

L
Linus Torvalds 已提交
1673 1674 1675 1676
}

module_init(riscom8_init_module);
module_exit(riscom8_exit_module);