2 * linux/drivers/char/riscom.c -- RISCom/8 multiport serial driver.
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
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.
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.
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.
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.
30 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31 * - get rid of check_region and several cleanups
34 #include <linux/module.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>
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>
53 #include <linux/uaccess.h>
56 #include "riscom8_reg.h"
58 /* Am I paranoid or not ? ;-) */
59 #define RISCOM_PARANOIA_CHECK
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.
67 #define RISCOM_BRAIN_DAMAGED_CTS
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.
74 #undef RC_REPORT_OVERRUN
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)
82 static struct tty_driver *riscom_driver;
84 static DEFINE_SPINLOCK(riscom_lock);
86 static struct riscom_board rc_board[RC_NBOARD] = {
101 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
103 /* RISCom/8 I/O ports addresses (without address translation) */
104 static unsigned short rc_ioport[] = {
106 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
108 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
109 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
110 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
113 #define RC_NIOPORT ARRAY_SIZE(rc_ioport)
116 static int rc_paranoia_check(struct riscom_port const *port,
117 char *name, const char *routine)
119 #ifdef RISCOM_PARANOIA_CHECK
120 static const char badmagic[] = KERN_INFO
121 "rc: Warning: bad riscom port magic number for device %s in %s\n";
122 static const char badinfo[] = KERN_INFO
123 "rc: Warning: null riscom port for device %s in %s\n";
126 printk(badinfo, name, routine);
129 if (port->magic != RISCOM8_MAGIC) {
130 printk(badmagic, name, routine);
139 * Service functions for RISCom/8 driver.
143 /* Get board number from pointer */
144 static inline int board_No(struct riscom_board const *bp)
146 return bp - rc_board;
149 /* Get port number from pointer */
150 static inline int port_No(struct riscom_port const *port)
152 return RC_PORT(port - rc_port);
155 /* Get pointer to board from pointer to port */
156 static inline struct riscom_board *port_Board(struct riscom_port const *port)
158 return &rc_board[RC_BOARD(port - rc_port)];
161 /* Input Byte from CL CD180 register */
162 static inline unsigned char rc_in(struct riscom_board const *bp,
165 return inb(bp->base + RC_TO_ISA(reg));
168 /* Output Byte to CL CD180 register */
169 static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
172 outb(val, bp->base + RC_TO_ISA(reg));
175 /* Wait for Channel Command Register ready */
176 static void rc_wait_CCR(struct riscom_board const *bp)
180 /* FIXME: need something more descriptive then 100000 :) */
181 for (delay = 100000; delay; delay--)
182 if (!rc_in(bp, CD180_CCR))
185 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
189 * RISCom/8 probe functions.
192 static int rc_request_io_range(struct riscom_board * const bp)
196 for (i = 0; i < RC_NIOPORT; i++)
197 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
203 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
204 board_No(bp), bp->base);
206 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
210 static void rc_release_io_range(struct riscom_board * const bp)
214 for (i = 0; i < RC_NIOPORT; i++)
215 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
218 /* Reset and setup CD180 chip */
219 static void __init rc_init_CD180(struct riscom_board const *bp)
223 spin_lock_irqsave(&riscom_lock, flags);
225 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
226 rc_wait_CCR(bp); /* Wait for CCR ready */
227 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
228 spin_unlock_irqrestore(&riscom_lock, flags);
229 msleep(50); /* Delay 0.05 sec */
230 spin_lock_irqsave(&riscom_lock, flags);
231 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
232 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
233 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
234 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for tx intr */
235 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for rx intr */
237 /* Setting up prescaler. We need 4 ticks per 1 ms */
238 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
239 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
241 spin_unlock_irqrestore(&riscom_lock, flags);
244 /* Main probing routine, also sets irq. */
245 static int __init rc_probe(struct riscom_board *bp)
247 unsigned char val1, val2;
253 if (rc_request_io_range(bp))
256 /* Are the I/O ports here ? */
257 rc_out(bp, CD180_PPRL, 0x5a);
259 val1 = rc_in(bp, CD180_PPRL);
260 rc_out(bp, CD180_PPRL, 0xa5);
262 val2 = rc_in(bp, CD180_PPRL);
264 if ((val1 != 0x5a) || (val2 != 0xa5)) {
265 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
266 board_No(bp), bp->base);
270 /* It's time to find IRQ for this board */
271 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
272 irqs = probe_irq_on();
273 rc_init_CD180(bp); /* Reset CD180 chip */
274 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
276 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
277 rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr */
279 irqs = probe_irq_off(irqs);
280 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
281 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
282 rc_init_CD180(bp); /* Reset CD180 again */
284 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
285 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
286 "found.\n", board_No(bp), bp->base);
292 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
293 "at 0x%03x.\n", board_No(bp), bp->base);
297 bp->flags |= RC_BOARD_PRESENT;
299 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
302 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
307 rc_release_io_range(bp);
313 * Interrupt processing routines.
317 static struct riscom_port *rc_get_port(struct riscom_board const *bp,
318 unsigned char const *what)
320 unsigned char channel;
321 struct riscom_port *port;
323 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
324 if (channel < CD180_NCH) {
325 port = &rc_port[board_No(bp) * RC_NPORT + channel];
326 if (port->port.flags & ASYNC_INITIALIZED)
329 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
330 board_No(bp), what, channel);
334 static void rc_receive_exc(struct riscom_board const *bp)
336 struct riscom_port *port;
337 struct tty_struct *tty;
338 unsigned char status;
339 unsigned char ch, flag;
341 port = rc_get_port(bp, "Receive");
345 tty = port->port.tty;
347 #ifdef RC_REPORT_OVERRUN
348 status = rc_in(bp, CD180_RCSR);
349 if (status & RCSR_OE)
351 status &= port->mark_mask;
353 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
355 ch = rc_in(bp, CD180_RDR);
358 if (status & RCSR_TOUT) {
359 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
360 "Hardware problems ?\n",
361 board_No(bp), port_No(port));
364 } else if (status & RCSR_BREAK) {
365 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
366 board_No(bp), port_No(port));
368 if (port->port.flags & ASYNC_SAK)
371 } else if (status & RCSR_PE)
374 else if (status & RCSR_FE)
377 else if (status & RCSR_OE)
382 tty_insert_flip_char(tty, ch, flag);
383 tty_flip_buffer_push(tty);
386 static void rc_receive(struct riscom_board const *bp)
388 struct riscom_port *port;
389 struct tty_struct *tty;
392 port = rc_get_port(bp, "Receive");
396 tty = port->port.tty;
398 count = rc_in(bp, CD180_RDCR);
400 #ifdef RC_REPORT_FIFO
401 port->hits[count > 8 ? 9 : count]++;
405 if (tty_buffer_request_room(tty, 1) == 0) {
406 printk(KERN_WARNING "rc%d: port %d: Working around "
407 "flip buffer overflow.\n",
408 board_No(bp), port_No(port));
411 tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
413 tty_flip_buffer_push(tty);
416 static void rc_transmit(struct riscom_board const *bp)
418 struct riscom_port *port;
419 struct tty_struct *tty;
422 port = rc_get_port(bp, "Transmit");
426 tty = port->port.tty;
428 if (port->IER & IER_TXEMPTY) {
430 rc_out(bp, CD180_CAR, port_No(port));
431 port->IER &= ~IER_TXEMPTY;
432 rc_out(bp, CD180_IER, port->IER);
436 if ((port->xmit_cnt <= 0 && !port->break_length)
437 || tty->stopped || tty->hw_stopped) {
438 rc_out(bp, CD180_CAR, port_No(port));
439 port->IER &= ~IER_TXRDY;
440 rc_out(bp, CD180_IER, port->IER);
444 if (port->break_length) {
445 if (port->break_length > 0) {
446 if (port->COR2 & COR2_ETC) {
447 rc_out(bp, CD180_TDR, CD180_C_ESC);
448 rc_out(bp, CD180_TDR, CD180_C_SBRK);
449 port->COR2 &= ~COR2_ETC;
451 count = min_t(int, port->break_length, 0xff);
452 rc_out(bp, CD180_TDR, CD180_C_ESC);
453 rc_out(bp, CD180_TDR, CD180_C_DELAY);
454 rc_out(bp, CD180_TDR, count);
455 port->break_length -= count;
456 if (port->break_length == 0)
457 port->break_length--;
459 rc_out(bp, CD180_TDR, CD180_C_ESC);
460 rc_out(bp, CD180_TDR, CD180_C_EBRK);
461 rc_out(bp, CD180_COR2, port->COR2);
463 rc_out(bp, CD180_CCR, CCR_CORCHG2);
464 port->break_length = 0;
471 rc_out(bp, CD180_TDR, port->port.xmit_buf[port->xmit_tail++]);
472 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
473 if (--port->xmit_cnt <= 0)
475 } while (--count > 0);
477 if (port->xmit_cnt <= 0) {
478 rc_out(bp, CD180_CAR, port_No(port));
479 port->IER &= ~IER_TXRDY;
480 rc_out(bp, CD180_IER, port->IER);
482 if (port->xmit_cnt <= port->wakeup_chars)
486 static void rc_check_modem(struct riscom_board const *bp)
488 struct riscom_port *port;
489 struct tty_struct *tty;
492 port = rc_get_port(bp, "Modem");
496 tty = port->port.tty;
498 mcr = rc_in(bp, CD180_MCR);
499 if (mcr & MCR_CDCHG) {
500 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
501 wake_up_interruptible(&port->port.open_wait);
506 #ifdef RISCOM_BRAIN_DAMAGED_CTS
507 if (mcr & MCR_CTSCHG) {
508 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
510 port->IER |= IER_TXRDY;
511 if (port->xmit_cnt <= port->wakeup_chars)
515 port->IER &= ~IER_TXRDY;
517 rc_out(bp, CD180_IER, port->IER);
519 if (mcr & MCR_DSRCHG) {
520 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
522 port->IER |= IER_TXRDY;
523 if (port->xmit_cnt <= port->wakeup_chars)
527 port->IER &= ~IER_TXRDY;
529 rc_out(bp, CD180_IER, port->IER);
531 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
533 /* Clear change bits */
534 rc_out(bp, CD180_MCR, 0);
537 /* The main interrupt processing routine */
538 static irqreturn_t rc_interrupt(int dummy, void *dev_id)
540 unsigned char status;
542 struct riscom_board *bp = dev_id;
543 unsigned long loop = 0;
546 if (!(bp->flags & RC_BOARD_ACTIVE))
549 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
550 (RC_BSR_TOUT | RC_BSR_TINT |
551 RC_BSR_MINT | RC_BSR_RINT))) {
553 if (status & RC_BSR_TOUT)
554 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
555 "error?\n", board_No(bp));
556 else if (status & RC_BSR_RINT) {
557 ack = rc_in(bp, RC_ACK_RINT);
558 if (ack == (RC_ID | GIVR_IT_RCV))
560 else if (ack == (RC_ID | GIVR_IT_REXC))
563 printk(KERN_WARNING "rc%d: Bad receive ack "
566 } else if (status & RC_BSR_TINT) {
567 ack = rc_in(bp, RC_ACK_TINT);
568 if (ack == (RC_ID | GIVR_IT_TX))
571 printk(KERN_WARNING "rc%d: Bad transmit ack "
574 } else /* if (status & RC_BSR_MINT) */ {
575 ack = rc_in(bp, RC_ACK_MINT);
576 if (ack == (RC_ID | GIVR_IT_MODEM))
579 printk(KERN_WARNING "rc%d: Bad modem ack "
583 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
584 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
586 return IRQ_RETVAL(handled);
590 * Routines for open & close processing.
593 /* Called with disabled interrupts */
594 static int rc_setup_board(struct riscom_board *bp)
598 if (bp->flags & RC_BOARD_ACTIVE)
601 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
606 rc_out(bp, RC_CTOUT, 0); /* Just in case */
608 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
610 bp->flags |= RC_BOARD_ACTIVE;
615 /* Called with disabled interrupts */
616 static void rc_shutdown_board(struct riscom_board *bp)
618 if (!(bp->flags & RC_BOARD_ACTIVE))
621 bp->flags &= ~RC_BOARD_ACTIVE;
623 free_irq(bp->irq, NULL);
626 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
631 * Setting up port characteristics.
632 * Must be called with disabled interrupts
634 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
636 struct tty_struct *tty = port->port.tty;
639 unsigned char cor1 = 0, cor3 = 0;
640 unsigned char mcor1 = 0, mcor2 = 0;
644 port->MSVR = MSVR_RTS;
646 baud = tty_get_baud_rate(tty);
648 /* Select port on the board */
649 rc_out(bp, CD180_CAR, port_No(port));
652 /* Drop DTR & exit */
653 bp->DTR |= (1u << port_No(port));
654 rc_out(bp, RC_DTR, bp->DTR);
658 bp->DTR &= ~(1u << port_No(port));
659 rc_out(bp, RC_DTR, bp->DTR);
663 * Now we must calculate some speed depended things
666 /* Set baud rate for port */
667 tmp = (((RC_OSCFREQ + baud/2) / baud +
668 CD180_TPC/2) / CD180_TPC);
670 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
671 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
672 rc_out(bp, CD180_RBPRL, tmp & 0xff);
673 rc_out(bp, CD180_TBPRL, tmp & 0xff);
675 baud = (baud + 5) / 10; /* Estimated CPS */
677 /* Two timer ticks seems enough to wakeup something like SLIP driver */
678 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
679 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
680 SERIAL_XMIT_SIZE - 1 : tmp);
682 /* Receiver timeout will be transmission time for 1.5 chars */
683 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
684 tmp = (tmp > 0xff) ? 0xff : tmp;
685 rc_out(bp, CD180_RTPR, tmp);
687 switch (C_CSIZE(tty)) {
706 cor1 |= COR1_NORMPAR;
710 cor1 &= ~COR1_IGNORE;
712 /* Set marking of some errors */
713 port->mark_mask = RCSR_OE | RCSR_TOUT;
715 port->mark_mask |= RCSR_FE | RCSR_PE;
716 if (I_BRKINT(tty) || I_PARMRK(tty))
717 port->mark_mask |= RCSR_BREAK;
719 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
721 port->mark_mask &= ~RCSR_BREAK;
723 /* Real raw mode. Ignore all */
724 port->mark_mask &= ~RCSR_OE;
726 /* Enable Hardware Flow Control */
727 if (C_CRTSCTS(tty)) {
728 #ifdef RISCOM_BRAIN_DAMAGED_CTS
729 port->IER |= IER_DSR | IER_CTS;
730 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
731 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
732 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
733 (MSVR_CTS|MSVR_DSR));
735 port->COR2 |= COR2_CTSAE;
738 /* Enable Software Flow Control. FIXME: I'm not sure about this */
739 /* Some people reported that it works, but I still doubt */
741 port->COR2 |= COR2_TXIBE;
742 cor3 |= (COR3_FCT | COR3_SCDE);
744 port->COR2 |= COR2_IXM;
745 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
746 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
747 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
748 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
750 if (!C_CLOCAL(tty)) {
751 /* Enable CD check */
758 /* Enable receiver */
759 port->IER |= IER_RXD;
761 /* Set input FIFO size (1-8 bytes) */
762 cor3 |= RISCOM_RXFIFO;
763 /* Setting up CD180 channel registers */
764 rc_out(bp, CD180_COR1, cor1);
765 rc_out(bp, CD180_COR2, port->COR2);
766 rc_out(bp, CD180_COR3, cor3);
767 /* Make CD180 know about registers change */
769 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
770 /* Setting up modem option registers */
771 rc_out(bp, CD180_MCOR1, mcor1);
772 rc_out(bp, CD180_MCOR2, mcor2);
773 /* Enable CD180 transmitter & receiver */
775 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
776 /* Enable interrupts */
777 rc_out(bp, CD180_IER, port->IER);
778 /* And finally set RTS on */
779 rc_out(bp, CD180_MSVR, port->MSVR);
782 /* Must be called with interrupts enabled */
783 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
787 if (port->port.flags & ASYNC_INITIALIZED)
790 if (tty_port_alloc_xmit_buf(&port->port) < 0)
793 spin_lock_irqsave(&riscom_lock, flags);
795 clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
796 if (port->port.count == 1)
798 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
799 rc_change_speed(bp, port);
800 port->port.flags |= ASYNC_INITIALIZED;
802 spin_unlock_irqrestore(&riscom_lock, flags);
806 /* Must be called with interrupts disabled */
807 static void rc_shutdown_port(struct tty_struct *tty,
808 struct riscom_board *bp, struct riscom_port *port)
810 if (!(port->port.flags & ASYNC_INITIALIZED))
813 #ifdef RC_REPORT_OVERRUN
814 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
815 board_No(bp), port_No(port), port->overrun);
817 #ifdef RC_REPORT_FIFO
821 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
822 board_No(bp), port_No(port));
823 for (i = 0; i < 10; i++)
824 printk("%ld ", port->hits[i]);
828 tty_port_free_xmit_buf(&port->port);
831 bp->DTR |= (1u << port_No(port));
832 rc_out(bp, RC_DTR, bp->DTR);
836 rc_out(bp, CD180_CAR, port_No(port));
839 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
840 /* Disable all interrupts from this port */
842 rc_out(bp, CD180_IER, port->IER);
844 set_bit(TTY_IO_ERROR, &tty->flags);
845 port->port.flags &= ~ASYNC_INITIALIZED;
847 if (--bp->count < 0) {
848 printk(KERN_INFO "rc%d: rc_shutdown_port: "
849 "bad board count: %d\n",
850 board_No(bp), bp->count);
854 * If this is the last opened port on the board
855 * shutdown whole board
858 rc_shutdown_board(bp);
861 static int carrier_raised(struct tty_port *port)
863 struct riscom_port *p = container_of(port, struct riscom_port, port);
864 struct riscom_board *bp = port_Board(p);
868 spin_lock_irqsave(&riscom_lock, flags);
869 rc_out(bp, CD180_CAR, port_No(p));
870 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
871 rc_out(bp, CD180_MSVR, MSVR_RTS);
872 bp->DTR &= ~(1u << port_No(p));
873 rc_out(bp, RC_DTR, bp->DTR);
874 spin_unlock_irqrestore(&riscom_lock, flags);
878 static int rc_open(struct tty_struct *tty, struct file *filp)
882 struct riscom_port *port;
883 struct riscom_board *bp;
885 board = RC_BOARD(tty->index);
886 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
889 bp = &rc_board[board];
890 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
891 if (rc_paranoia_check(port, tty->name, "rc_open"))
894 error = rc_setup_board(bp);
899 tty->driver_data = port;
900 port->port.tty = tty;
902 error = rc_setup_port(bp, port);
904 error = tty_port_block_til_ready(&port->port, tty, filp);
908 static void rc_flush_buffer(struct tty_struct *tty)
910 struct riscom_port *port = tty->driver_data;
913 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
916 spin_lock_irqsave(&riscom_lock, flags);
917 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
918 spin_unlock_irqrestore(&riscom_lock, flags);
923 static void rc_close(struct tty_struct *tty, struct file *filp)
925 struct riscom_port *port = tty->driver_data;
926 struct riscom_board *bp;
928 unsigned long timeout;
930 if (!port || rc_paranoia_check(port, tty->name, "close"))
933 bp = port_Board(port);
935 if (tty_port_close_start(&port->port, tty, filp) == 0)
939 * At this point we stop accepting input. To do this, we
940 * disable the receive line status interrupts, and tell the
941 * interrupt driver to stop checking the data ready bit in the
942 * line status register.
945 spin_lock_irqsave(&riscom_lock, flags);
946 port->IER &= ~IER_RXD;
947 if (port->port.flags & ASYNC_INITIALIZED) {
948 port->IER &= ~IER_TXRDY;
949 port->IER |= IER_TXEMPTY;
950 rc_out(bp, CD180_CAR, port_No(port));
951 rc_out(bp, CD180_IER, port->IER);
953 * Before we drop DTR, make sure the UART transmitter
954 * has completely drained; this is especially
955 * important if there is a transmit FIFO!
957 timeout = jiffies + HZ;
958 while (port->IER & IER_TXEMPTY) {
959 spin_unlock_irqrestore(&riscom_lock, flags);
960 msleep_interruptible(jiffies_to_msecs(port->timeout));
961 spin_lock_irqsave(&riscom_lock, flags);
962 if (time_after(jiffies, timeout))
966 rc_shutdown_port(tty, bp, port);
967 rc_flush_buffer(tty);
968 spin_unlock_irqrestore(&riscom_lock, flags);
970 tty_port_close_end(&port->port, tty);
973 static int rc_write(struct tty_struct *tty,
974 const unsigned char *buf, int count)
976 struct riscom_port *port = tty->driver_data;
977 struct riscom_board *bp;
981 if (rc_paranoia_check(port, tty->name, "rc_write"))
984 bp = port_Board(port);
987 spin_lock_irqsave(&riscom_lock, flags);
989 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
990 SERIAL_XMIT_SIZE - port->xmit_head));
992 break; /* lock continues to be held */
994 memcpy(port->port.xmit_buf + port->xmit_head, buf, c);
995 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
998 spin_unlock_irqrestore(&riscom_lock, flags);
1005 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1006 !(port->IER & IER_TXRDY)) {
1007 port->IER |= IER_TXRDY;
1008 rc_out(bp, CD180_CAR, port_No(port));
1009 rc_out(bp, CD180_IER, port->IER);
1012 spin_unlock_irqrestore(&riscom_lock, flags);
1017 static int rc_put_char(struct tty_struct *tty, unsigned char ch)
1019 struct riscom_port *port = tty->driver_data;
1020 unsigned long flags;
1023 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1026 spin_lock_irqsave(&riscom_lock, flags);
1028 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1031 port->port.xmit_buf[port->xmit_head++] = ch;
1032 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1037 spin_unlock_irqrestore(&riscom_lock, flags);
1041 static void rc_flush_chars(struct tty_struct *tty)
1043 struct riscom_port *port = tty->driver_data;
1044 unsigned long flags;
1046 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1049 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped)
1052 spin_lock_irqsave(&riscom_lock, flags);
1054 port->IER |= IER_TXRDY;
1055 rc_out(port_Board(port), CD180_CAR, port_No(port));
1056 rc_out(port_Board(port), CD180_IER, port->IER);
1058 spin_unlock_irqrestore(&riscom_lock, flags);
1061 static int rc_write_room(struct tty_struct *tty)
1063 struct riscom_port *port = tty->driver_data;
1066 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1069 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1075 static int rc_chars_in_buffer(struct tty_struct *tty)
1077 struct riscom_port *port = tty->driver_data;
1079 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1082 return port->xmit_cnt;
1085 static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1087 struct riscom_port *port = tty->driver_data;
1088 struct riscom_board *bp;
1089 unsigned char status;
1090 unsigned int result;
1091 unsigned long flags;
1093 if (rc_paranoia_check(port, tty->name, __func__))
1096 bp = port_Board(port);
1098 spin_lock_irqsave(&riscom_lock, flags);
1100 rc_out(bp, CD180_CAR, port_No(port));
1101 status = rc_in(bp, CD180_MSVR);
1102 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1104 spin_unlock_irqrestore(&riscom_lock, flags);
1106 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1107 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1108 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1109 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1110 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1114 static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1115 unsigned int set, unsigned int clear)
1117 struct riscom_port *port = tty->driver_data;
1118 unsigned long flags;
1119 struct riscom_board *bp;
1121 if (rc_paranoia_check(port, tty->name, __func__))
1124 bp = port_Board(port);
1126 spin_lock_irqsave(&riscom_lock, flags);
1128 if (set & TIOCM_RTS)
1129 port->MSVR |= MSVR_RTS;
1130 if (set & TIOCM_DTR)
1131 bp->DTR &= ~(1u << port_No(port));
1133 if (clear & TIOCM_RTS)
1134 port->MSVR &= ~MSVR_RTS;
1135 if (clear & TIOCM_DTR)
1136 bp->DTR |= (1u << port_No(port));
1138 rc_out(bp, CD180_CAR, port_No(port));
1139 rc_out(bp, CD180_MSVR, port->MSVR);
1140 rc_out(bp, RC_DTR, bp->DTR);
1142 spin_unlock_irqrestore(&riscom_lock, flags);
1147 static int rc_send_break(struct tty_struct *tty, int length)
1149 struct riscom_port *port = tty->driver_data;
1150 struct riscom_board *bp = port_Board(port);
1151 unsigned long flags;
1153 if (length == 0 || length == -1)
1156 spin_lock_irqsave(&riscom_lock, flags);
1158 port->break_length = RISCOM_TPS / HZ * length;
1159 port->COR2 |= COR2_ETC;
1160 port->IER |= IER_TXRDY;
1161 rc_out(bp, CD180_CAR, port_No(port));
1162 rc_out(bp, CD180_COR2, port->COR2);
1163 rc_out(bp, CD180_IER, port->IER);
1165 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1168 spin_unlock_irqrestore(&riscom_lock, flags);
1172 static int rc_set_serial_info(struct riscom_port *port,
1173 struct serial_struct __user *newinfo)
1175 struct serial_struct tmp;
1176 struct riscom_board *bp = port_Board(port);
1179 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1183 if ((tmp.irq != bp->irq) ||
1184 (tmp.port != bp->base) ||
1185 (tmp.type != PORT_CIRRUS) ||
1186 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1187 (tmp.custom_divisor != 0) ||
1188 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1189 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1193 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1194 (tmp.flags & ASYNC_SPD_MASK));
1196 if (!capable(CAP_SYS_ADMIN)) {
1197 if ((tmp.close_delay != port->port.close_delay) ||
1198 (tmp.closing_wait != port->port.closing_wait) ||
1199 ((tmp.flags & ~ASYNC_USR_MASK) !=
1200 (port->port.flags & ~ASYNC_USR_MASK)))
1202 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1203 (tmp.flags & ASYNC_USR_MASK));
1205 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1206 (tmp.flags & ASYNC_FLAGS));
1207 port->port.close_delay = tmp.close_delay;
1208 port->port.closing_wait = tmp.closing_wait;
1211 unsigned long flags;
1213 spin_lock_irqsave(&riscom_lock, flags);
1214 rc_change_speed(bp, port);
1215 spin_unlock_irqrestore(&riscom_lock, flags);
1220 static int rc_get_serial_info(struct riscom_port *port,
1221 struct serial_struct __user *retinfo)
1223 struct serial_struct tmp;
1224 struct riscom_board *bp = port_Board(port);
1226 memset(&tmp, 0, sizeof(tmp));
1227 tmp.type = PORT_CIRRUS;
1228 tmp.line = port - rc_port;
1229 tmp.port = bp->base;
1231 tmp.flags = port->port.flags;
1232 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1233 tmp.close_delay = port->port.close_delay * HZ/100;
1234 tmp.closing_wait = port->port.closing_wait * HZ/100;
1235 tmp.xmit_fifo_size = CD180_NFIFO;
1236 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1239 static int rc_ioctl(struct tty_struct *tty, struct file *filp,
1240 unsigned int cmd, unsigned long arg)
1242 struct riscom_port *port = tty->driver_data;
1243 void __user *argp = (void __user *)arg;
1246 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1252 retval = rc_get_serial_info(port, argp);
1257 retval = rc_set_serial_info(port, argp);
1261 retval = -ENOIOCTLCMD;
1266 static void rc_throttle(struct tty_struct *tty)
1268 struct riscom_port *port = tty->driver_data;
1269 struct riscom_board *bp;
1270 unsigned long flags;
1272 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1274 bp = port_Board(port);
1276 spin_lock_irqsave(&riscom_lock, flags);
1277 port->MSVR &= ~MSVR_RTS;
1278 rc_out(bp, CD180_CAR, port_No(port));
1281 rc_out(bp, CD180_CCR, CCR_SSCH2);
1284 rc_out(bp, CD180_MSVR, port->MSVR);
1285 spin_unlock_irqrestore(&riscom_lock, flags);
1288 static void rc_unthrottle(struct tty_struct *tty)
1290 struct riscom_port *port = tty->driver_data;
1291 struct riscom_board *bp;
1292 unsigned long flags;
1294 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1296 bp = port_Board(port);
1298 spin_lock_irqsave(&riscom_lock, flags);
1299 port->MSVR |= MSVR_RTS;
1300 rc_out(bp, CD180_CAR, port_No(port));
1303 rc_out(bp, CD180_CCR, CCR_SSCH1);
1306 rc_out(bp, CD180_MSVR, port->MSVR);
1307 spin_unlock_irqrestore(&riscom_lock, flags);
1310 static void rc_stop(struct tty_struct *tty)
1312 struct riscom_port *port = tty->driver_data;
1313 struct riscom_board *bp;
1314 unsigned long flags;
1316 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1319 bp = port_Board(port);
1321 spin_lock_irqsave(&riscom_lock, flags);
1322 port->IER &= ~IER_TXRDY;
1323 rc_out(bp, CD180_CAR, port_No(port));
1324 rc_out(bp, CD180_IER, port->IER);
1325 spin_unlock_irqrestore(&riscom_lock, flags);
1328 static void rc_start(struct tty_struct *tty)
1330 struct riscom_port *port = tty->driver_data;
1331 struct riscom_board *bp;
1332 unsigned long flags;
1334 if (rc_paranoia_check(port, tty->name, "rc_start"))
1337 bp = port_Board(port);
1339 spin_lock_irqsave(&riscom_lock, flags);
1341 if (port->xmit_cnt && port->port.xmit_buf && !(port->IER & IER_TXRDY)) {
1342 port->IER |= IER_TXRDY;
1343 rc_out(bp, CD180_CAR, port_No(port));
1344 rc_out(bp, CD180_IER, port->IER);
1346 spin_unlock_irqrestore(&riscom_lock, flags);
1349 static void rc_hangup(struct tty_struct *tty)
1351 struct riscom_port *port = tty->driver_data;
1352 struct riscom_board *bp;
1353 unsigned long flags;
1355 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1358 bp = port_Board(port);
1360 rc_shutdown_port(tty, bp, port);
1361 spin_lock_irqsave(&port->port.lock, flags);
1362 port->port.count = 0;
1363 port->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1364 port->port.tty = NULL;
1365 wake_up_interruptible(&port->port.open_wait);
1366 spin_unlock_irqrestore(&port->port.lock, flags);
1369 static void rc_set_termios(struct tty_struct *tty,
1370 struct ktermios *old_termios)
1372 struct riscom_port *port = tty->driver_data;
1373 unsigned long flags;
1375 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1378 spin_lock_irqsave(&riscom_lock, flags);
1379 rc_change_speed(port_Board(port), port);
1380 spin_unlock_irqrestore(&riscom_lock, flags);
1382 if ((old_termios->c_cflag & CRTSCTS) &&
1383 !(tty->termios->c_cflag & CRTSCTS)) {
1384 tty->hw_stopped = 0;
1389 static const struct tty_operations riscom_ops = {
1393 .put_char = rc_put_char,
1394 .flush_chars = rc_flush_chars,
1395 .write_room = rc_write_room,
1396 .chars_in_buffer = rc_chars_in_buffer,
1397 .flush_buffer = rc_flush_buffer,
1399 .throttle = rc_throttle,
1400 .unthrottle = rc_unthrottle,
1401 .set_termios = rc_set_termios,
1404 .hangup = rc_hangup,
1405 .tiocmget = rc_tiocmget,
1406 .tiocmset = rc_tiocmset,
1407 .break_ctl = rc_send_break,
1410 static const struct tty_port_operations riscom_port_ops = {
1411 .carrier_raised = carrier_raised,
1415 static int __init rc_init_drivers(void)
1420 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1424 riscom_driver->owner = THIS_MODULE;
1425 riscom_driver->name = "ttyL";
1426 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1427 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1428 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1429 riscom_driver->init_termios = tty_std_termios;
1430 riscom_driver->init_termios.c_cflag =
1431 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1432 riscom_driver->init_termios.c_ispeed = 9600;
1433 riscom_driver->init_termios.c_ospeed = 9600;
1434 riscom_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
1435 tty_set_operations(riscom_driver, &riscom_ops);
1436 error = tty_register_driver(riscom_driver);
1438 put_tty_driver(riscom_driver);
1439 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1440 "error = %d\n", error);
1443 memset(rc_port, 0, sizeof(rc_port));
1444 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1445 tty_port_init(&rc_port[i].port);
1446 rc_port[i].port.ops = &riscom_port_ops;
1447 rc_port[i].magic = RISCOM8_MAGIC;
1452 static void rc_release_drivers(void)
1454 tty_unregister_driver(riscom_driver);
1455 put_tty_driver(riscom_driver);
1460 * Called at boot time.
1462 * You can specify IO base for up to RC_NBOARD cards,
1463 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1464 * Note that there will be no probing at default
1465 * addresses in this case.
1468 static int __init riscom8_setup(char *str)
1470 int ints[RC_NBOARD];
1473 str = get_options(str, ARRAY_SIZE(ints), ints);
1475 for (i = 0; i < RC_NBOARD; i++) {
1477 rc_board[i].base = ints[i+1];
1479 rc_board[i].base = 0;
1484 __setup("riscom8=", riscom8_setup);
1487 static char banner[] __initdata =
1488 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1490 static char no_boards_msg[] __initdata =
1491 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1494 * This routine must be called by kernel at boot time
1496 static int __init riscom8_init(void)
1503 if (rc_init_drivers())
1506 for (i = 0; i < RC_NBOARD; i++)
1507 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1510 rc_release_drivers();
1511 printk(no_boards_msg);
1522 module_param(iobase, int, 0);
1523 module_param(iobase1, int, 0);
1524 module_param(iobase2, int, 0);
1525 module_param(iobase3, int, 0);
1527 MODULE_LICENSE("GPL");
1528 MODULE_ALIAS_CHARDEV_MAJOR(RISCOM8_NORMAL_MAJOR);
1532 * You can setup up to 4 boards (current value of RC_NBOARD)
1533 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1536 static int __init riscom8_init_module(void)
1541 if (iobase || iobase1 || iobase2 || iobase3) {
1542 for (i = 0; i < RC_NBOARD; i++)
1543 rc_board[i].base = 0;
1547 rc_board[0].base = iobase;
1549 rc_board[1].base = iobase1;
1551 rc_board[2].base = iobase2;
1553 rc_board[3].base = iobase3;
1556 return riscom8_init();
1559 static void __exit riscom8_exit_module(void)
1563 rc_release_drivers();
1564 for (i = 0; i < RC_NBOARD; i++)
1565 if (rc_board[i].flags & RC_BOARD_PRESENT)
1566 rc_release_io_range(&rc_board[i]);
1570 module_init(riscom8_init_module);
1571 module_exit(riscom8_exit_module);