• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/char/
1/*
2 *      linux/drivers/char/riscom.c  -- RISCom/8 multiport serial driver.
3 *
4 *      Copyright (C) 1994-1996  Dmitry Gorodchanin (pgmdsg@ibi.com)
5 *
6 *      This code is loosely based on the Linux serial driver, written by
7 *      Linus Torvalds, Theodore T'so and others. The RISCom/8 card
8 *      programming info was obtained from various drivers for other OSes
9 *	(FreeBSD, ISC, etc), but no source code from those drivers were
10 *	directly included in this driver.
11 *
12 *
13 *      This program is free software; you can redistribute it and/or modify
14 *      it under the terms of the GNU General Public License as published by
15 *      the Free Software Foundation; either version 2 of the License, or
16 *      (at your option) any later version.
17 *
18 *      This program is distributed in the hope that it will be useful,
19 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *      GNU General Public License for more details.
22 *
23 *      You should have received a copy of the GNU General Public License
24 *      along with this program; if not, write to the Free Software
25 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *	Revision 1.1
28 *
29 *	ChangeLog:
30 *	Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31 *	- get rid of check_region and several cleanups
32 */
33
34#include <linux/module.h>
35
36#include <linux/io.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/ioport.h>
40#include <linux/interrupt.h>
41#include <linux/errno.h>
42#include <linux/tty.h>
43#include <linux/mm.h>
44#include <linux/serial.h>
45#include <linux/fcntl.h>
46#include <linux/major.h>
47#include <linux/init.h>
48#include <linux/delay.h>
49#include <linux/tty_flip.h>
50#include <linux/spinlock.h>
51#include <linux/device.h>
52
53#include <linux/uaccess.h>
54
55#include "riscom8.h"
56#include "riscom8_reg.h"
57
58/* Am I paranoid or not ? ;-) */
59#define RISCOM_PARANOIA_CHECK
60
61/*
62 * Crazy InteliCom/8 boards sometimes have swapped CTS & DSR signals.
63 * You can slightly speed up things by #undefing the following option,
64 * if you are REALLY sure that your board is correct one.
65 */
66
67#define RISCOM_BRAIN_DAMAGED_CTS
68
69/*
70 * The following defines are mostly for testing purposes. But if you need
71 * some nice reporting in your syslog, you can define them also.
72 */
73#undef RC_REPORT_FIFO
74#undef RC_REPORT_OVERRUN
75
76
77#define RISCOM_LEGAL_FLAGS \
78	(ASYNC_HUP_NOTIFY   | ASYNC_SAK          | ASYNC_SPLIT_TERMIOS   | \
79	 ASYNC_SPD_HI       | ASYNC_SPEED_VHI    | ASYNC_SESSION_LOCKOUT | \
80	 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
81
82static struct tty_driver *riscom_driver;
83
84static DEFINE_SPINLOCK(riscom_lock);
85
86static struct riscom_board rc_board[RC_NBOARD] =  {
87	{
88		.base	= RC_IOBASE1,
89	},
90	{
91		.base	= RC_IOBASE2,
92	},
93	{
94		.base	= RC_IOBASE3,
95	},
96	{
97		.base	= RC_IOBASE4,
98	},
99};
100
101static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
102
103/* RISCom/8 I/O ports addresses (without address translation) */
104static unsigned short rc_ioport[] =  {
105	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
106};
107#define RC_NIOPORT	ARRAY_SIZE(rc_ioport)
108
109
110static int rc_paranoia_check(struct riscom_port const *port,
111				    char *name, const char *routine)
112{
113#ifdef RISCOM_PARANOIA_CHECK
114	static const char badmagic[] = KERN_INFO
115		"rc: Warning: bad riscom port magic number for device %s in %s\n";
116	static const char badinfo[] = KERN_INFO
117		"rc: Warning: null riscom port for device %s in %s\n";
118
119	if (!port) {
120		printk(badinfo, name, routine);
121		return 1;
122	}
123	if (port->magic != RISCOM8_MAGIC) {
124		printk(badmagic, name, routine);
125		return 1;
126	}
127#endif
128	return 0;
129}
130
131/*
132 *
133 *  Service functions for RISCom/8 driver.
134 *
135 */
136
137/* Get board number from pointer */
138static inline int board_No(struct riscom_board const *bp)
139{
140	return bp - rc_board;
141}
142
143/* Get port number from pointer */
144static inline int port_No(struct riscom_port const *port)
145{
146	return RC_PORT(port - rc_port);
147}
148
149/* Get pointer to board from pointer to port */
150static inline struct riscom_board *port_Board(struct riscom_port const *port)
151{
152	return &rc_board[RC_BOARD(port - rc_port)];
153}
154
155/* Input Byte from CL CD180 register */
156static inline unsigned char rc_in(struct riscom_board const *bp,
157							unsigned short reg)
158{
159	return inb(bp->base + RC_TO_ISA(reg));
160}
161
162/* Output Byte to CL CD180 register */
163static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
164			  unsigned char val)
165{
166	outb(val, bp->base + RC_TO_ISA(reg));
167}
168
169/* Wait for Channel Command Register ready */
170static void rc_wait_CCR(struct riscom_board const *bp)
171{
172	unsigned long delay;
173
174	for (delay = 100000; delay; delay--)
175		if (!rc_in(bp, CD180_CCR))
176			return;
177
178	printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
179}
180
181/*
182 *  RISCom/8 probe functions.
183 */
184
185static int rc_request_io_range(struct riscom_board * const bp)
186{
187	int i;
188
189	for (i = 0; i < RC_NIOPORT; i++)
190		if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
191				   "RISCom/8"))  {
192			goto out_release;
193		}
194	return 0;
195out_release:
196	printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
197			 board_No(bp), bp->base);
198	while (--i >= 0)
199		release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
200	return 1;
201}
202
203static void rc_release_io_range(struct riscom_board * const bp)
204{
205	int i;
206
207	for (i = 0; i < RC_NIOPORT; i++)
208		release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
209}
210
211/* Reset and setup CD180 chip */
212static void __init rc_init_CD180(struct riscom_board const *bp)
213{
214	unsigned long flags;
215
216	spin_lock_irqsave(&riscom_lock, flags);
217
218	rc_out(bp, RC_CTOUT, 0);     	           /* Clear timeout        */
219	rc_wait_CCR(bp);			   /* Wait for CCR ready   */
220	rc_out(bp, CD180_CCR, CCR_HARDRESET);      /* Reset CD180 chip     */
221	spin_unlock_irqrestore(&riscom_lock, flags);
222	msleep(50);				   /* Delay 0.05 sec       */
223	spin_lock_irqsave(&riscom_lock, flags);
224	rc_out(bp, CD180_GIVR, RC_ID);             /* Set ID for this chip */
225	rc_out(bp, CD180_GICR, 0);                 /* Clear all bits       */
226	rc_out(bp, CD180_PILR1, RC_ACK_MINT);      /* Prio for modem intr  */
227	rc_out(bp, CD180_PILR2, RC_ACK_TINT);      /* Prio for tx intr     */
228	rc_out(bp, CD180_PILR3, RC_ACK_RINT);      /* Prio for rx intr	   */
229
230	/* Setting up prescaler. We need 4 ticks per 1 ms */
231	rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
232	rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
233
234	spin_unlock_irqrestore(&riscom_lock, flags);
235}
236
237/* Main probing routine, also sets irq. */
238static int __init rc_probe(struct riscom_board *bp)
239{
240	unsigned char val1, val2;
241	int irqs = 0;
242	int retries;
243
244	bp->irq = 0;
245
246	if (rc_request_io_range(bp))
247		return 1;
248
249	/* Are the I/O ports here ? */
250	rc_out(bp, CD180_PPRL, 0x5a);
251	outb(0xff, 0x80);
252	val1 = rc_in(bp, CD180_PPRL);
253	rc_out(bp, CD180_PPRL, 0xa5);
254	outb(0x00, 0x80);
255	val2 = rc_in(bp, CD180_PPRL);
256
257	if ((val1 != 0x5a) || (val2 != 0xa5))  {
258		printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
259		       board_No(bp), bp->base);
260		goto out_release;
261	}
262
263	/* It's time to find IRQ for this board */
264	for (retries = 0; retries < 5 && irqs <= 0; retries++) {
265		irqs = probe_irq_on();
266		rc_init_CD180(bp);		 /* Reset CD180 chip	     */
267		rc_out(bp, CD180_CAR, 2);	 /* Select port 2	     */
268		rc_wait_CCR(bp);
269		rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter	     */
270		rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr     */
271		msleep(50);
272		irqs = probe_irq_off(irqs);
273		val1 = rc_in(bp, RC_BSR);	/* Get Board Status reg	     */
274		val2 = rc_in(bp, RC_ACK_TINT);  /* ACK interrupt	     */
275		rc_init_CD180(bp);	       	/* Reset CD180 again	     */
276
277		if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX)))  {
278			printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
279					"found.\n", board_No(bp), bp->base);
280			goto out_release;
281		}
282	}
283
284	if (irqs <= 0)  {
285		printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
286				"at 0x%03x.\n", board_No(bp), bp->base);
287		goto out_release;
288	}
289	bp->irq = irqs;
290	bp->flags |= RC_BOARD_PRESENT;
291
292	printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
293			 "0x%03x, IRQ %d.\n",
294	       board_No(bp),
295	       (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A',   /* Board revision */
296	       bp->base, bp->irq);
297
298	return 0;
299out_release:
300	rc_release_io_range(bp);
301	return 1;
302}
303
304/*
305 *
306 *  Interrupt processing routines.
307 *
308 */
309
310static struct riscom_port *rc_get_port(struct riscom_board const *bp,
311					       unsigned char const *what)
312{
313	unsigned char channel;
314	struct riscom_port *port;
315
316	channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
317	if (channel < CD180_NCH)  {
318		port = &rc_port[board_No(bp) * RC_NPORT + channel];
319		if (port->port.flags & ASYNC_INITIALIZED)
320			return port;
321	}
322	printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
323	       board_No(bp), what, channel);
324	return NULL;
325}
326
327static void rc_receive_exc(struct riscom_board const *bp)
328{
329	struct riscom_port *port;
330	struct tty_struct *tty;
331	unsigned char status;
332	unsigned char ch, flag;
333
334	port = rc_get_port(bp, "Receive");
335	if (port == NULL)
336		return;
337
338	tty = tty_port_tty_get(&port->port);
339
340#ifdef RC_REPORT_OVERRUN
341	status = rc_in(bp, CD180_RCSR);
342	if (status & RCSR_OE)
343		port->overrun++;
344	status &= port->mark_mask;
345#else
346	status = rc_in(bp, CD180_RCSR) & port->mark_mask;
347#endif
348	ch = rc_in(bp, CD180_RDR);
349	if (!status)
350		goto out;
351	if (status & RCSR_TOUT)  {
352		printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
353				    "Hardware problems ?\n",
354		       board_No(bp), port_No(port));
355		goto out;
356
357	} else if (status & RCSR_BREAK)  {
358		printk(KERN_INFO "rc%d: port %d: Handling break...\n",
359		       board_No(bp), port_No(port));
360		flag = TTY_BREAK;
361		if (tty && (port->port.flags & ASYNC_SAK))
362			do_SAK(tty);
363
364	} else if (status & RCSR_PE)
365		flag = TTY_PARITY;
366
367	else if (status & RCSR_FE)
368		flag = TTY_FRAME;
369
370	else if (status & RCSR_OE)
371		flag = TTY_OVERRUN;
372	else
373		flag = TTY_NORMAL;
374
375	if (tty) {
376		tty_insert_flip_char(tty, ch, flag);
377		tty_flip_buffer_push(tty);
378	}
379out:
380	tty_kref_put(tty);
381}
382
383static void rc_receive(struct riscom_board const *bp)
384{
385	struct riscom_port *port;
386	struct tty_struct *tty;
387	unsigned char count;
388
389	port = rc_get_port(bp, "Receive");
390	if (port == NULL)
391		return;
392
393	tty = tty_port_tty_get(&port->port);
394
395	count = rc_in(bp, CD180_RDCR);
396
397#ifdef RC_REPORT_FIFO
398	port->hits[count > 8 ? 9 : count]++;
399#endif
400
401	while (count--)  {
402		u8 ch = rc_in(bp, CD180_RDR);
403		if (tty)
404			tty_insert_flip_char(tty, ch, TTY_NORMAL);
405	}
406	if (tty) {
407		tty_flip_buffer_push(tty);
408		tty_kref_put(tty);
409	}
410}
411
412static void rc_transmit(struct riscom_board const *bp)
413{
414	struct riscom_port *port;
415	struct tty_struct *tty;
416	unsigned char count;
417
418	port = rc_get_port(bp, "Transmit");
419	if (port == NULL)
420		return;
421
422	tty = tty_port_tty_get(&port->port);
423
424	if (port->IER & IER_TXEMPTY) {
425		/* FIFO drained */
426		rc_out(bp, CD180_CAR, port_No(port));
427		port->IER &= ~IER_TXEMPTY;
428		rc_out(bp, CD180_IER, port->IER);
429		goto out;
430	}
431
432	if ((port->xmit_cnt <= 0 && !port->break_length)
433	    || (tty && (tty->stopped || tty->hw_stopped)))  {
434		rc_out(bp, CD180_CAR, port_No(port));
435		port->IER &= ~IER_TXRDY;
436		rc_out(bp, CD180_IER, port->IER);
437		goto out;
438	}
439
440	if (port->break_length)  {
441		if (port->break_length > 0)  {
442			if (port->COR2 & COR2_ETC)  {
443				rc_out(bp, CD180_TDR, CD180_C_ESC);
444				rc_out(bp, CD180_TDR, CD180_C_SBRK);
445				port->COR2 &= ~COR2_ETC;
446			}
447			count = min_t(int, port->break_length, 0xff);
448			rc_out(bp, CD180_TDR, CD180_C_ESC);
449			rc_out(bp, CD180_TDR, CD180_C_DELAY);
450			rc_out(bp, CD180_TDR, count);
451			port->break_length -= count;
452			if (port->break_length == 0)
453				port->break_length--;
454		} else  {
455			rc_out(bp, CD180_TDR, CD180_C_ESC);
456			rc_out(bp, CD180_TDR, CD180_C_EBRK);
457			rc_out(bp, CD180_COR2, port->COR2);
458			rc_wait_CCR(bp);
459			rc_out(bp, CD180_CCR, CCR_CORCHG2);
460			port->break_length = 0;
461		}
462		goto out;
463	}
464
465	count = CD180_NFIFO;
466	do {
467		rc_out(bp, CD180_TDR, port->port.xmit_buf[port->xmit_tail++]);
468		port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
469		if (--port->xmit_cnt <= 0)
470			break;
471	} while (--count > 0);
472
473	if (port->xmit_cnt <= 0)  {
474		rc_out(bp, CD180_CAR, port_No(port));
475		port->IER &= ~IER_TXRDY;
476		rc_out(bp, CD180_IER, port->IER);
477	}
478	if (tty && port->xmit_cnt <= port->wakeup_chars)
479		tty_wakeup(tty);
480out:
481	tty_kref_put(tty);
482}
483
484static void rc_check_modem(struct riscom_board const *bp)
485{
486	struct riscom_port *port;
487	struct tty_struct *tty;
488	unsigned char mcr;
489
490	port = rc_get_port(bp, "Modem");
491	if (port == NULL)
492		return;
493
494	tty = tty_port_tty_get(&port->port);
495
496	mcr = rc_in(bp, CD180_MCR);
497	if (mcr & MCR_CDCHG) {
498		if (rc_in(bp, CD180_MSVR) & MSVR_CD)
499			wake_up_interruptible(&port->port.open_wait);
500		else if (tty)
501			tty_hangup(tty);
502	}
503
504#ifdef RISCOM_BRAIN_DAMAGED_CTS
505	if (mcr & MCR_CTSCHG)  {
506		if (rc_in(bp, CD180_MSVR) & MSVR_CTS)  {
507			port->IER |= IER_TXRDY;
508			if (tty) {
509				tty->hw_stopped = 0;
510				if (port->xmit_cnt <= port->wakeup_chars)
511					tty_wakeup(tty);
512			}
513		} else  {
514			if (tty)
515				tty->hw_stopped = 1;
516			port->IER &= ~IER_TXRDY;
517		}
518		rc_out(bp, CD180_IER, port->IER);
519	}
520	if (mcr & MCR_DSRCHG)  {
521		if (rc_in(bp, CD180_MSVR) & MSVR_DSR)  {
522			port->IER |= IER_TXRDY;
523			if (tty) {
524				tty->hw_stopped = 0;
525				if (port->xmit_cnt <= port->wakeup_chars)
526					tty_wakeup(tty);
527			}
528		} else  {
529			if (tty)
530				tty->hw_stopped = 1;
531			port->IER &= ~IER_TXRDY;
532		}
533		rc_out(bp, CD180_IER, port->IER);
534	}
535#endif /* RISCOM_BRAIN_DAMAGED_CTS */
536
537	/* Clear change bits */
538	rc_out(bp, CD180_MCR, 0);
539	tty_kref_put(tty);
540}
541
542/* The main interrupt processing routine */
543static irqreturn_t rc_interrupt(int dummy, void *dev_id)
544{
545	unsigned char status;
546	unsigned char ack;
547	struct riscom_board *bp = dev_id;
548	unsigned long loop = 0;
549	int handled = 0;
550
551	if (!(bp->flags & RC_BOARD_ACTIVE))
552		return IRQ_NONE;
553
554	while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
555				 (RC_BSR_TOUT | RC_BSR_TINT |
556				  RC_BSR_MINT | RC_BSR_RINT))) {
557		handled = 1;
558		if (status & RC_BSR_TOUT)
559			printk(KERN_WARNING "rc%d: Got timeout. Hardware "
560					    "error?\n", board_No(bp));
561		else if (status & RC_BSR_RINT) {
562			ack = rc_in(bp, RC_ACK_RINT);
563			if (ack == (RC_ID | GIVR_IT_RCV))
564				rc_receive(bp);
565			else if (ack == (RC_ID | GIVR_IT_REXC))
566				rc_receive_exc(bp);
567			else
568				printk(KERN_WARNING "rc%d: Bad receive ack "
569						    "0x%02x.\n",
570				       board_No(bp), ack);
571		} else if (status & RC_BSR_TINT) {
572			ack = rc_in(bp, RC_ACK_TINT);
573			if (ack == (RC_ID | GIVR_IT_TX))
574				rc_transmit(bp);
575			else
576				printk(KERN_WARNING "rc%d: Bad transmit ack "
577						    "0x%02x.\n",
578				       board_No(bp), ack);
579		} else /* if (status & RC_BSR_MINT) */ {
580			ack = rc_in(bp, RC_ACK_MINT);
581			if (ack == (RC_ID | GIVR_IT_MODEM))
582				rc_check_modem(bp);
583			else
584				printk(KERN_WARNING "rc%d: Bad modem ack "
585						    "0x%02x.\n",
586				       board_No(bp), ack);
587		}
588		rc_out(bp, CD180_EOIR, 0);   /* Mark end of interrupt */
589		rc_out(bp, RC_CTOUT, 0);     /* Clear timeout flag    */
590	}
591	return IRQ_RETVAL(handled);
592}
593
594/*
595 *  Routines for open & close processing.
596 */
597
598/* Called with disabled interrupts */
599static int rc_setup_board(struct riscom_board *bp)
600{
601	int error;
602
603	if (bp->flags & RC_BOARD_ACTIVE)
604		return 0;
605
606	error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
607			    "RISCom/8", bp);
608	if (error)
609		return error;
610
611	rc_out(bp, RC_CTOUT, 0);       		/* Just in case         */
612	bp->DTR = ~0;
613	rc_out(bp, RC_DTR, bp->DTR);	        /* Drop DTR on all ports */
614
615	bp->flags |= RC_BOARD_ACTIVE;
616
617	return 0;
618}
619
620/* Called with disabled interrupts */
621static void rc_shutdown_board(struct riscom_board *bp)
622{
623	if (!(bp->flags & RC_BOARD_ACTIVE))
624		return;
625
626	bp->flags &= ~RC_BOARD_ACTIVE;
627
628	free_irq(bp->irq, NULL);
629
630	bp->DTR = ~0;
631	rc_out(bp, RC_DTR, bp->DTR);	       /* Drop DTR on all ports */
632
633}
634
635/*
636 * Setting up port characteristics.
637 * Must be called with disabled interrupts
638 */
639static void rc_change_speed(struct tty_struct *tty, struct riscom_board *bp,
640						struct riscom_port *port)
641{
642	unsigned long baud;
643	long tmp;
644	unsigned char cor1 = 0, cor3 = 0;
645	unsigned char mcor1 = 0, mcor2 = 0;
646
647	port->IER  = 0;
648	port->COR2 = 0;
649	port->MSVR = MSVR_RTS;
650
651	baud = tty_get_baud_rate(tty);
652
653	/* Select port on the board */
654	rc_out(bp, CD180_CAR, port_No(port));
655
656	if (!baud)  {
657		/* Drop DTR & exit */
658		bp->DTR |= (1u << port_No(port));
659		rc_out(bp, RC_DTR, bp->DTR);
660		return;
661	} else  {
662		/* Set DTR on */
663		bp->DTR &= ~(1u << port_No(port));
664		rc_out(bp, RC_DTR, bp->DTR);
665	}
666
667	/*
668	 * Now we must calculate some speed depended things
669	 */
670
671	/* Set baud rate for port */
672	tmp = (((RC_OSCFREQ + baud/2) / baud +
673		CD180_TPC/2) / CD180_TPC);
674
675	rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
676	rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
677	rc_out(bp, CD180_RBPRL, tmp & 0xff);
678	rc_out(bp, CD180_TBPRL, tmp & 0xff);
679
680	baud = (baud + 5) / 10;   /* Estimated CPS */
681
682	/* Two timer ticks seems enough to wakeup something like SLIP driver */
683	tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
684	port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
685					      SERIAL_XMIT_SIZE - 1 : tmp);
686
687	/* Receiver timeout will be transmission time for 1.5 chars */
688	tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
689	tmp = (tmp > 0xff) ? 0xff : tmp;
690	rc_out(bp, CD180_RTPR, tmp);
691
692	switch (C_CSIZE(tty)) {
693	case CS5:
694		cor1 |= COR1_5BITS;
695		break;
696	case CS6:
697		cor1 |= COR1_6BITS;
698		break;
699	case CS7:
700		cor1 |= COR1_7BITS;
701		break;
702	case CS8:
703		cor1 |= COR1_8BITS;
704		break;
705	}
706	if (C_CSTOPB(tty))
707		cor1 |= COR1_2SB;
708
709	cor1 |= COR1_IGNORE;
710	if (C_PARENB(tty)) {
711		cor1 |= COR1_NORMPAR;
712		if (C_PARODD(tty))
713			cor1 |= COR1_ODDP;
714		if (I_INPCK(tty))
715			cor1 &= ~COR1_IGNORE;
716	}
717	/* Set marking of some errors */
718	port->mark_mask = RCSR_OE | RCSR_TOUT;
719	if (I_INPCK(tty))
720		port->mark_mask |= RCSR_FE | RCSR_PE;
721	if (I_BRKINT(tty) || I_PARMRK(tty))
722		port->mark_mask |= RCSR_BREAK;
723	if (I_IGNPAR(tty))
724		port->mark_mask &= ~(RCSR_FE | RCSR_PE);
725	if (I_IGNBRK(tty)) {
726		port->mark_mask &= ~RCSR_BREAK;
727		if (I_IGNPAR(tty))
728			/* Real raw mode. Ignore all */
729			port->mark_mask &= ~RCSR_OE;
730	}
731	/* Enable Hardware Flow Control */
732	if (C_CRTSCTS(tty))  {
733#ifdef RISCOM_BRAIN_DAMAGED_CTS
734		port->IER |= IER_DSR | IER_CTS;
735		mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
736		mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
737		tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
738						(MSVR_CTS|MSVR_DSR));
739#else
740		port->COR2 |= COR2_CTSAE;
741#endif
742	}
743	/* Some people reported that it works, but I still doubt */
744	if (I_IXON(tty))  {
745		port->COR2 |= COR2_TXIBE;
746		cor3 |= (COR3_FCT | COR3_SCDE);
747		if (I_IXANY(tty))
748			port->COR2 |= COR2_IXM;
749		rc_out(bp, CD180_SCHR1, START_CHAR(tty));
750		rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
751		rc_out(bp, CD180_SCHR3, START_CHAR(tty));
752		rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
753	}
754	if (!C_CLOCAL(tty))  {
755		/* Enable CD check */
756		port->IER |= IER_CD;
757		mcor1 |= MCOR1_CDZD;
758		mcor2 |= MCOR2_CDOD;
759	}
760
761	if (C_CREAD(tty))
762		/* Enable receiver */
763		port->IER |= IER_RXD;
764
765	/* Set input FIFO size (1-8 bytes) */
766	cor3 |= RISCOM_RXFIFO;
767	/* Setting up CD180 channel registers */
768	rc_out(bp, CD180_COR1, cor1);
769	rc_out(bp, CD180_COR2, port->COR2);
770	rc_out(bp, CD180_COR3, cor3);
771	/* Make CD180 know about registers change */
772	rc_wait_CCR(bp);
773	rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
774	/* Setting up modem option registers */
775	rc_out(bp, CD180_MCOR1, mcor1);
776	rc_out(bp, CD180_MCOR2, mcor2);
777	/* Enable CD180 transmitter & receiver */
778	rc_wait_CCR(bp);
779	rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
780	/* Enable interrupts */
781	rc_out(bp, CD180_IER, port->IER);
782	/* And finally set RTS on */
783	rc_out(bp, CD180_MSVR, port->MSVR);
784}
785
786/* Must be called with interrupts enabled */
787static int rc_activate_port(struct tty_port *port, struct tty_struct *tty)
788{
789	struct riscom_port *rp = container_of(port, struct riscom_port, port);
790	struct riscom_board *bp = port_Board(rp);
791	unsigned long flags;
792
793	if (tty_port_alloc_xmit_buf(port) < 0)
794		return -ENOMEM;
795
796	spin_lock_irqsave(&riscom_lock, flags);
797
798	clear_bit(TTY_IO_ERROR, &tty->flags);
799	bp->count++;
800	rp->xmit_cnt = rp->xmit_head = rp->xmit_tail = 0;
801	rc_change_speed(tty, bp, rp);
802	spin_unlock_irqrestore(&riscom_lock, flags);
803	return 0;
804}
805
806/* Must be called with interrupts disabled */
807static void rc_shutdown_port(struct tty_struct *tty,
808			struct riscom_board *bp, struct riscom_port *port)
809{
810#ifdef RC_REPORT_OVERRUN
811	printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
812	       board_No(bp), port_No(port), port->overrun);
813#endif
814#ifdef RC_REPORT_FIFO
815	{
816		int i;
817
818		printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
819		       board_No(bp), port_No(port));
820		for (i = 0; i < 10; i++)
821			printk("%ld ", port->hits[i]);
822		printk("].\n");
823	}
824#endif
825	tty_port_free_xmit_buf(&port->port);
826
827	/* Select port */
828	rc_out(bp, CD180_CAR, port_No(port));
829	/* Reset port */
830	rc_wait_CCR(bp);
831	rc_out(bp, CD180_CCR, CCR_SOFTRESET);
832	/* Disable all interrupts from this port */
833	port->IER = 0;
834	rc_out(bp, CD180_IER, port->IER);
835
836	set_bit(TTY_IO_ERROR, &tty->flags);
837
838	if (--bp->count < 0)  {
839		printk(KERN_INFO "rc%d: rc_shutdown_port: "
840				 "bad board count: %d\n",
841		       board_No(bp), bp->count);
842		bp->count = 0;
843	}
844	/*
845	 * If this is the last opened port on the board
846	 * shutdown whole board
847	 */
848	if (!bp->count)
849		rc_shutdown_board(bp);
850}
851
852static int carrier_raised(struct tty_port *port)
853{
854	struct riscom_port *p = container_of(port, struct riscom_port, port);
855	struct riscom_board *bp = port_Board(p);
856	unsigned long flags;
857	int CD;
858
859	spin_lock_irqsave(&riscom_lock, flags);
860	rc_out(bp, CD180_CAR, port_No(p));
861	CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
862	rc_out(bp, CD180_MSVR, MSVR_RTS);
863	bp->DTR &= ~(1u << port_No(p));
864	rc_out(bp, RC_DTR, bp->DTR);
865	spin_unlock_irqrestore(&riscom_lock, flags);
866	return CD;
867}
868
869static void dtr_rts(struct tty_port *port, int onoff)
870{
871	struct riscom_port *p = container_of(port, struct riscom_port, port);
872	struct riscom_board *bp = port_Board(p);
873	unsigned long flags;
874
875	spin_lock_irqsave(&riscom_lock, flags);
876	bp->DTR &= ~(1u << port_No(p));
877	if (onoff == 0)
878		bp->DTR |= (1u << port_No(p));
879	rc_out(bp, RC_DTR, bp->DTR);
880	spin_unlock_irqrestore(&riscom_lock, flags);
881}
882
883static int rc_open(struct tty_struct *tty, struct file *filp)
884{
885	int board;
886	int error;
887	struct riscom_port *port;
888	struct riscom_board *bp;
889
890	board = RC_BOARD(tty->index);
891	if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
892		return -ENODEV;
893
894	bp = &rc_board[board];
895	port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
896	if (rc_paranoia_check(port, tty->name, "rc_open"))
897		return -ENODEV;
898
899	error = rc_setup_board(bp);
900	if (error)
901		return error;
902
903	tty->driver_data = port;
904	return tty_port_open(&port->port, tty, filp);
905}
906
907static void rc_flush_buffer(struct tty_struct *tty)
908{
909	struct riscom_port *port = tty->driver_data;
910	unsigned long flags;
911
912	if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
913		return;
914
915	spin_lock_irqsave(&riscom_lock, flags);
916	port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
917	spin_unlock_irqrestore(&riscom_lock, flags);
918
919	tty_wakeup(tty);
920}
921
922static void rc_close_port(struct tty_port *port)
923{
924	unsigned long flags;
925	struct riscom_port *rp = container_of(port, struct riscom_port, port);
926	struct riscom_board *bp = port_Board(rp);
927	unsigned long timeout;
928
929	/*
930	 * At this point we stop accepting input.  To do this, we
931	 * disable the receive line status interrupts, and tell the
932	 * interrupt driver to stop checking the data ready bit in the
933	 * line status register.
934	 */
935
936	spin_lock_irqsave(&riscom_lock, flags);
937	rp->IER &= ~IER_RXD;
938
939	rp->IER &= ~IER_TXRDY;
940	rp->IER |= IER_TXEMPTY;
941	rc_out(bp, CD180_CAR, port_No(rp));
942	rc_out(bp, CD180_IER, rp->IER);
943	/*
944	 * Before we drop DTR, make sure the UART transmitter
945	 * has completely drained; this is especially
946	 * important if there is a transmit FIFO!
947	 */
948	timeout = jiffies + HZ;
949	while (rp->IER & IER_TXEMPTY) {
950		spin_unlock_irqrestore(&riscom_lock, flags);
951		msleep_interruptible(jiffies_to_msecs(rp->timeout));
952		spin_lock_irqsave(&riscom_lock, flags);
953		if (time_after(jiffies, timeout))
954			break;
955	}
956	rc_shutdown_port(port->tty, bp, rp);
957	spin_unlock_irqrestore(&riscom_lock, flags);
958}
959
960static void rc_close(struct tty_struct *tty, struct file *filp)
961{
962	struct riscom_port *port = tty->driver_data;
963
964	if (!port || rc_paranoia_check(port, tty->name, "close"))
965		return;
966	tty_port_close(&port->port, tty, filp);
967}
968
969static int rc_write(struct tty_struct *tty,
970		    const unsigned char *buf, int count)
971{
972	struct riscom_port *port = tty->driver_data;
973	struct riscom_board *bp;
974	int c, total = 0;
975	unsigned long flags;
976
977	if (rc_paranoia_check(port, tty->name, "rc_write"))
978		return 0;
979
980	bp = port_Board(port);
981
982	while (1) {
983		spin_lock_irqsave(&riscom_lock, flags);
984
985		c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
986					  SERIAL_XMIT_SIZE - port->xmit_head));
987		if (c <= 0)
988			break;	/* lock continues to be held */
989
990		memcpy(port->port.xmit_buf + port->xmit_head, buf, c);
991		port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
992		port->xmit_cnt += c;
993
994		spin_unlock_irqrestore(&riscom_lock, flags);
995
996		buf += c;
997		count -= c;
998		total += c;
999	}
1000
1001	if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1002	    !(port->IER & IER_TXRDY)) {
1003		port->IER |= IER_TXRDY;
1004		rc_out(bp, CD180_CAR, port_No(port));
1005		rc_out(bp, CD180_IER, port->IER);
1006	}
1007
1008	spin_unlock_irqrestore(&riscom_lock, flags);
1009
1010	return total;
1011}
1012
1013static int rc_put_char(struct tty_struct *tty, unsigned char ch)
1014{
1015	struct riscom_port *port = tty->driver_data;
1016	unsigned long flags;
1017	int ret = 0;
1018
1019	if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1020		return 0;
1021
1022	spin_lock_irqsave(&riscom_lock, flags);
1023
1024	if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1025		goto out;
1026
1027	port->port.xmit_buf[port->xmit_head++] = ch;
1028	port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1029	port->xmit_cnt++;
1030	ret = 1;
1031
1032out:
1033	spin_unlock_irqrestore(&riscom_lock, flags);
1034	return ret;
1035}
1036
1037static void rc_flush_chars(struct tty_struct *tty)
1038{
1039	struct riscom_port *port = tty->driver_data;
1040	unsigned long flags;
1041
1042	if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1043		return;
1044
1045	if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped)
1046		return;
1047
1048	spin_lock_irqsave(&riscom_lock, flags);
1049
1050	port->IER |= IER_TXRDY;
1051	rc_out(port_Board(port), CD180_CAR, port_No(port));
1052	rc_out(port_Board(port), CD180_IER, port->IER);
1053
1054	spin_unlock_irqrestore(&riscom_lock, flags);
1055}
1056
1057static int rc_write_room(struct tty_struct *tty)
1058{
1059	struct riscom_port *port = tty->driver_data;
1060	int	ret;
1061
1062	if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1063		return 0;
1064
1065	ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1066	if (ret < 0)
1067		ret = 0;
1068	return ret;
1069}
1070
1071static int rc_chars_in_buffer(struct tty_struct *tty)
1072{
1073	struct riscom_port *port = tty->driver_data;
1074
1075	if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1076		return 0;
1077
1078	return port->xmit_cnt;
1079}
1080
1081static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1082{
1083	struct riscom_port *port = tty->driver_data;
1084	struct riscom_board *bp;
1085	unsigned char status;
1086	unsigned int result;
1087	unsigned long flags;
1088
1089	if (rc_paranoia_check(port, tty->name, __func__))
1090		return -ENODEV;
1091
1092	bp = port_Board(port);
1093
1094	spin_lock_irqsave(&riscom_lock, flags);
1095
1096	rc_out(bp, CD180_CAR, port_No(port));
1097	status = rc_in(bp, CD180_MSVR);
1098	result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1099
1100	spin_unlock_irqrestore(&riscom_lock, flags);
1101
1102	result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1103		| ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1104		| ((status & MSVR_CD)  ? TIOCM_CAR : 0)
1105		| ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1106		| ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1107	return result;
1108}
1109
1110static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1111		       unsigned int set, unsigned int clear)
1112{
1113	struct riscom_port *port = tty->driver_data;
1114	unsigned long flags;
1115	struct riscom_board *bp;
1116
1117	if (rc_paranoia_check(port, tty->name, __func__))
1118		return -ENODEV;
1119
1120	bp = port_Board(port);
1121
1122	spin_lock_irqsave(&riscom_lock, flags);
1123
1124	if (set & TIOCM_RTS)
1125		port->MSVR |= MSVR_RTS;
1126	if (set & TIOCM_DTR)
1127		bp->DTR &= ~(1u << port_No(port));
1128
1129	if (clear & TIOCM_RTS)
1130		port->MSVR &= ~MSVR_RTS;
1131	if (clear & TIOCM_DTR)
1132		bp->DTR |= (1u << port_No(port));
1133
1134	rc_out(bp, CD180_CAR, port_No(port));
1135	rc_out(bp, CD180_MSVR, port->MSVR);
1136	rc_out(bp, RC_DTR, bp->DTR);
1137
1138	spin_unlock_irqrestore(&riscom_lock, flags);
1139
1140	return 0;
1141}
1142
1143static int rc_send_break(struct tty_struct *tty, int length)
1144{
1145	struct riscom_port *port = tty->driver_data;
1146	struct riscom_board *bp = port_Board(port);
1147	unsigned long flags;
1148
1149	if (length == 0 || length == -1)
1150		return -EOPNOTSUPP;
1151
1152	spin_lock_irqsave(&riscom_lock, flags);
1153
1154	port->break_length = RISCOM_TPS / HZ * length;
1155	port->COR2 |= COR2_ETC;
1156	port->IER  |= IER_TXRDY;
1157	rc_out(bp, CD180_CAR, port_No(port));
1158	rc_out(bp, CD180_COR2, port->COR2);
1159	rc_out(bp, CD180_IER, port->IER);
1160	rc_wait_CCR(bp);
1161	rc_out(bp, CD180_CCR, CCR_CORCHG2);
1162	rc_wait_CCR(bp);
1163
1164	spin_unlock_irqrestore(&riscom_lock, flags);
1165	return 0;
1166}
1167
1168static int rc_set_serial_info(struct tty_struct *tty, struct riscom_port *port,
1169				     struct serial_struct __user *newinfo)
1170{
1171	struct serial_struct tmp;
1172	struct riscom_board *bp = port_Board(port);
1173	int change_speed;
1174
1175	if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1176		return -EFAULT;
1177
1178	mutex_lock(&port->port.mutex);
1179	change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1180			(tmp.flags & ASYNC_SPD_MASK));
1181
1182	if (!capable(CAP_SYS_ADMIN)) {
1183		if ((tmp.close_delay != port->port.close_delay) ||
1184		    (tmp.closing_wait != port->port.closing_wait) ||
1185		    ((tmp.flags & ~ASYNC_USR_MASK) !=
1186		     (port->port.flags & ~ASYNC_USR_MASK))) {
1187			mutex_unlock(&port->port.mutex);
1188			return -EPERM;
1189		}
1190		port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1191			       (tmp.flags & ASYNC_USR_MASK));
1192	} else  {
1193		port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1194			       (tmp.flags & ASYNC_FLAGS));
1195		port->port.close_delay = tmp.close_delay;
1196		port->port.closing_wait = tmp.closing_wait;
1197	}
1198	if (change_speed)  {
1199		unsigned long flags;
1200
1201		spin_lock_irqsave(&riscom_lock, flags);
1202		rc_change_speed(tty, bp, port);
1203		spin_unlock_irqrestore(&riscom_lock, flags);
1204	}
1205	mutex_unlock(&port->port.mutex);
1206	return 0;
1207}
1208
1209static int rc_get_serial_info(struct riscom_port *port,
1210				     struct serial_struct __user *retinfo)
1211{
1212	struct serial_struct tmp;
1213	struct riscom_board *bp = port_Board(port);
1214
1215	memset(&tmp, 0, sizeof(tmp));
1216	tmp.type = PORT_CIRRUS;
1217	tmp.line = port - rc_port;
1218
1219	mutex_lock(&port->port.mutex);
1220	tmp.port = bp->base;
1221	tmp.irq  = bp->irq;
1222	tmp.flags = port->port.flags;
1223	tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1224	tmp.close_delay = port->port.close_delay * HZ/100;
1225	tmp.closing_wait = port->port.closing_wait * HZ/100;
1226	mutex_unlock(&port->port.mutex);
1227	tmp.xmit_fifo_size = CD180_NFIFO;
1228	return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1229}
1230
1231static int rc_ioctl(struct tty_struct *tty, struct file *filp,
1232		    unsigned int cmd, unsigned long arg)
1233{
1234	struct riscom_port *port = tty->driver_data;
1235	void __user *argp = (void __user *)arg;
1236	int retval;
1237
1238	if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1239		return -ENODEV;
1240
1241	switch (cmd) {
1242	case TIOCGSERIAL:
1243		retval = rc_get_serial_info(port, argp);
1244		break;
1245	case TIOCSSERIAL:
1246		retval = rc_set_serial_info(tty, port, argp);
1247		break;
1248	default:
1249		retval = -ENOIOCTLCMD;
1250	}
1251	return retval;
1252}
1253
1254static void rc_throttle(struct tty_struct *tty)
1255{
1256	struct riscom_port *port = tty->driver_data;
1257	struct riscom_board *bp;
1258	unsigned long flags;
1259
1260	if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1261		return;
1262	bp = port_Board(port);
1263
1264	spin_lock_irqsave(&riscom_lock, flags);
1265	port->MSVR &= ~MSVR_RTS;
1266	rc_out(bp, CD180_CAR, port_No(port));
1267	if (I_IXOFF(tty)) {
1268		rc_wait_CCR(bp);
1269		rc_out(bp, CD180_CCR, CCR_SSCH2);
1270		rc_wait_CCR(bp);
1271	}
1272	rc_out(bp, CD180_MSVR, port->MSVR);
1273	spin_unlock_irqrestore(&riscom_lock, flags);
1274}
1275
1276static void rc_unthrottle(struct tty_struct *tty)
1277{
1278	struct riscom_port *port = tty->driver_data;
1279	struct riscom_board *bp;
1280	unsigned long flags;
1281
1282	if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1283		return;
1284	bp = port_Board(port);
1285
1286	spin_lock_irqsave(&riscom_lock, flags);
1287	port->MSVR |= MSVR_RTS;
1288	rc_out(bp, CD180_CAR, port_No(port));
1289	if (I_IXOFF(tty))  {
1290		rc_wait_CCR(bp);
1291		rc_out(bp, CD180_CCR, CCR_SSCH1);
1292		rc_wait_CCR(bp);
1293	}
1294	rc_out(bp, CD180_MSVR, port->MSVR);
1295	spin_unlock_irqrestore(&riscom_lock, flags);
1296}
1297
1298static void rc_stop(struct tty_struct *tty)
1299{
1300	struct riscom_port *port = tty->driver_data;
1301	struct riscom_board *bp;
1302	unsigned long flags;
1303
1304	if (rc_paranoia_check(port, tty->name, "rc_stop"))
1305		return;
1306
1307	bp = port_Board(port);
1308
1309	spin_lock_irqsave(&riscom_lock, flags);
1310	port->IER &= ~IER_TXRDY;
1311	rc_out(bp, CD180_CAR, port_No(port));
1312	rc_out(bp, CD180_IER, port->IER);
1313	spin_unlock_irqrestore(&riscom_lock, flags);
1314}
1315
1316static void rc_start(struct tty_struct *tty)
1317{
1318	struct riscom_port *port = tty->driver_data;
1319	struct riscom_board *bp;
1320	unsigned long flags;
1321
1322	if (rc_paranoia_check(port, tty->name, "rc_start"))
1323		return;
1324
1325	bp = port_Board(port);
1326
1327	spin_lock_irqsave(&riscom_lock, flags);
1328
1329	if (port->xmit_cnt && port->port.xmit_buf && !(port->IER & IER_TXRDY)) {
1330		port->IER |= IER_TXRDY;
1331		rc_out(bp, CD180_CAR, port_No(port));
1332		rc_out(bp, CD180_IER, port->IER);
1333	}
1334	spin_unlock_irqrestore(&riscom_lock, flags);
1335}
1336
1337static void rc_hangup(struct tty_struct *tty)
1338{
1339	struct riscom_port *port = tty->driver_data;
1340
1341	if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1342		return;
1343
1344	tty_port_hangup(&port->port);
1345}
1346
1347static void rc_set_termios(struct tty_struct *tty,
1348					struct ktermios *old_termios)
1349{
1350	struct riscom_port *port = tty->driver_data;
1351	unsigned long flags;
1352
1353	if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1354		return;
1355
1356	spin_lock_irqsave(&riscom_lock, flags);
1357	rc_change_speed(tty, port_Board(port), port);
1358	spin_unlock_irqrestore(&riscom_lock, flags);
1359
1360	if ((old_termios->c_cflag & CRTSCTS) &&
1361	    !(tty->termios->c_cflag & CRTSCTS)) {
1362		tty->hw_stopped = 0;
1363		rc_start(tty);
1364	}
1365}
1366
1367static const struct tty_operations riscom_ops = {
1368	.open  = rc_open,
1369	.close = rc_close,
1370	.write = rc_write,
1371	.put_char = rc_put_char,
1372	.flush_chars = rc_flush_chars,
1373	.write_room = rc_write_room,
1374	.chars_in_buffer = rc_chars_in_buffer,
1375	.flush_buffer = rc_flush_buffer,
1376	.ioctl = rc_ioctl,
1377	.throttle = rc_throttle,
1378	.unthrottle = rc_unthrottle,
1379	.set_termios = rc_set_termios,
1380	.stop = rc_stop,
1381	.start = rc_start,
1382	.hangup = rc_hangup,
1383	.tiocmget = rc_tiocmget,
1384	.tiocmset = rc_tiocmset,
1385	.break_ctl = rc_send_break,
1386};
1387
1388static const struct tty_port_operations riscom_port_ops = {
1389	.carrier_raised = carrier_raised,
1390	.dtr_rts = dtr_rts,
1391	.shutdown = rc_close_port,
1392	.activate = rc_activate_port,
1393};
1394
1395
1396static int __init rc_init_drivers(void)
1397{
1398	int error;
1399	int i;
1400
1401	riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1402	if (!riscom_driver)
1403		return -ENOMEM;
1404
1405	riscom_driver->owner = THIS_MODULE;
1406	riscom_driver->name = "ttyL";
1407	riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1408	riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1409	riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1410	riscom_driver->init_termios = tty_std_termios;
1411	riscom_driver->init_termios.c_cflag =
1412		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1413	riscom_driver->init_termios.c_ispeed = 9600;
1414	riscom_driver->init_termios.c_ospeed = 9600;
1415	riscom_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
1416	tty_set_operations(riscom_driver, &riscom_ops);
1417	error = tty_register_driver(riscom_driver);
1418	if (error != 0) {
1419		put_tty_driver(riscom_driver);
1420		printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1421				"error = %d\n", error);
1422		return 1;
1423	}
1424	memset(rc_port, 0, sizeof(rc_port));
1425	for (i = 0; i < RC_NPORT * RC_NBOARD; i++)  {
1426		tty_port_init(&rc_port[i].port);
1427		rc_port[i].port.ops = &riscom_port_ops;
1428		rc_port[i].magic = RISCOM8_MAGIC;
1429	}
1430	return 0;
1431}
1432
1433static void rc_release_drivers(void)
1434{
1435	tty_unregister_driver(riscom_driver);
1436	put_tty_driver(riscom_driver);
1437}
1438
1439#ifndef MODULE
1440/*
1441 * Called at boot time.
1442 *
1443 * You can specify IO base for up to RC_NBOARD cards,
1444 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1445 * Note that there will be no probing at default
1446 * addresses in this case.
1447 *
1448 */
1449static int __init riscom8_setup(char *str)
1450{
1451	int ints[RC_NBOARD];
1452	int i;
1453
1454	str = get_options(str, ARRAY_SIZE(ints), ints);
1455
1456	for (i = 0; i < RC_NBOARD; i++) {
1457		if (i < ints[0])
1458			rc_board[i].base = ints[i+1];
1459		else
1460			rc_board[i].base = 0;
1461	}
1462	return 1;
1463}
1464
1465__setup("riscom8=", riscom8_setup);
1466#endif
1467
1468static char banner[] __initdata =
1469	KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1470		  "1994-1996.\n";
1471static char no_boards_msg[] __initdata =
1472	KERN_INFO "rc: No RISCom/8 boards detected.\n";
1473
1474/*
1475 * This routine must be called by kernel at boot time
1476 */
1477static int __init riscom8_init(void)
1478{
1479	int i;
1480	int found = 0;
1481
1482	printk(banner);
1483
1484	if (rc_init_drivers())
1485		return -EIO;
1486
1487	for (i = 0; i < RC_NBOARD; i++)
1488		if (rc_board[i].base && !rc_probe(&rc_board[i]))
1489			found++;
1490	if (!found)  {
1491		rc_release_drivers();
1492		printk(no_boards_msg);
1493		return -EIO;
1494	}
1495	return 0;
1496}
1497
1498#ifdef MODULE
1499static int iobase;
1500static int iobase1;
1501static int iobase2;
1502static int iobase3;
1503module_param(iobase, int, 0);
1504module_param(iobase1, int, 0);
1505module_param(iobase2, int, 0);
1506module_param(iobase3, int, 0);
1507
1508MODULE_LICENSE("GPL");
1509MODULE_ALIAS_CHARDEV_MAJOR(RISCOM8_NORMAL_MAJOR);
1510#endif /* MODULE */
1511
1512/*
1513 * You can setup up to 4 boards (current value of RC_NBOARD)
1514 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1515 *
1516 */
1517static int __init riscom8_init_module(void)
1518{
1519#ifdef MODULE
1520	int i;
1521
1522	if (iobase || iobase1 || iobase2 || iobase3) {
1523		for (i = 0; i < RC_NBOARD; i++)
1524			rc_board[i].base = 0;
1525	}
1526
1527	if (iobase)
1528		rc_board[0].base = iobase;
1529	if (iobase1)
1530		rc_board[1].base = iobase1;
1531	if (iobase2)
1532		rc_board[2].base = iobase2;
1533	if (iobase3)
1534		rc_board[3].base = iobase3;
1535#endif /* MODULE */
1536
1537	return riscom8_init();
1538}
1539
1540static void __exit riscom8_exit_module(void)
1541{
1542	int i;
1543
1544	rc_release_drivers();
1545	for (i = 0; i < RC_NBOARD; i++)
1546		if (rc_board[i].flags & RC_BOARD_PRESENT)
1547			rc_release_io_range(&rc_board[i]);
1548
1549}
1550
1551module_init(riscom8_init_module);
1552module_exit(riscom8_exit_module);
1553