Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / char / riscom8.c
blob3f9d0a9ac36d5988408f29a133e5a9c8fa1763ae
1 /*
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.
27 * Revision 1.1
29 * ChangeLog:
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>
36 #include <asm/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>
52 #include <asm/uaccess.h>
54 #include "riscom8.h"
55 #include "riscom8_reg.h"
57 /* Am I paranoid or not ? ;-) */
58 #define RISCOM_PARANOIA_CHECK
60 /*
61 * Crazy InteliCom/8 boards sometimes has swapped CTS & DSR signals.
62 * You can slightly speed up things by #undefing the following option,
63 * if you are REALLY sure that your board is correct one.
66 #define RISCOM_BRAIN_DAMAGED_CTS
68 /*
69 * The following defines are mostly for testing purposes. But if you need
70 * some nice reporting in your syslog, you can define them also.
72 #undef RC_REPORT_FIFO
73 #undef RC_REPORT_OVERRUN
76 #define RISCOM_LEGAL_FLAGS \
77 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
78 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
79 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
81 static struct tty_driver *riscom_driver;
83 static DEFINE_SPINLOCK(riscom_lock);
85 static struct riscom_board rc_board[RC_NBOARD] = {
87 .base = RC_IOBASE1,
90 .base = RC_IOBASE2,
93 .base = RC_IOBASE3,
96 .base = RC_IOBASE4,
100 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
102 /* RISCom/8 I/O ports addresses (without address translation) */
103 static unsigned short rc_ioport[] = {
104 #if 1
105 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
106 #else
107 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
108 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
109 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
110 #endif
112 #define RC_NIOPORT ARRAY_SIZE(rc_ioport)
115 static inline int rc_paranoia_check(struct riscom_port const * port,
116 char *name, const char *routine)
118 #ifdef RISCOM_PARANOIA_CHECK
119 static const char badmagic[] = KERN_INFO
120 "rc: Warning: bad riscom port magic number for device %s in %s\n";
121 static const char badinfo[] = KERN_INFO
122 "rc: Warning: null riscom port for device %s in %s\n";
124 if (!port) {
125 printk(badinfo, name, routine);
126 return 1;
128 if (port->magic != RISCOM8_MAGIC) {
129 printk(badmagic, name, routine);
130 return 1;
132 #endif
133 return 0;
138 * Service functions for RISCom/8 driver.
142 /* Get board number from pointer */
143 static inline int board_No (struct riscom_board const * bp)
145 return bp - rc_board;
148 /* Get port number from pointer */
149 static inline int port_No (struct riscom_port const * port)
151 return RC_PORT(port - rc_port);
154 /* Get pointer to board from pointer to port */
155 static inline struct riscom_board * port_Board(struct riscom_port const * port)
157 return &rc_board[RC_BOARD(port - rc_port)];
160 /* Input Byte from CL CD180 register */
161 static inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
163 return inb(bp->base + RC_TO_ISA(reg));
166 /* Output Byte to CL CD180 register */
167 static inline void rc_out(struct riscom_board const * bp, unsigned short reg,
168 unsigned char val)
170 outb(val, bp->base + RC_TO_ISA(reg));
173 /* Wait for Channel Command Register ready */
174 static inline void rc_wait_CCR(struct riscom_board const * bp)
176 unsigned long delay;
178 /* FIXME: need something more descriptive then 100000 :) */
179 for (delay = 100000; delay; delay--)
180 if (!rc_in(bp, CD180_CCR))
181 return;
183 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
187 * RISCom/8 probe functions.
190 static inline int rc_request_io_range(struct riscom_board * const bp)
192 int i;
194 for (i = 0; i < RC_NIOPORT; i++)
195 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
196 "RISCom/8")) {
197 goto out_release;
199 return 0;
200 out_release:
201 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
202 board_No(bp), bp->base);
203 while(--i >= 0)
204 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
205 return 1;
208 static inline void rc_release_io_range(struct riscom_board * const bp)
210 int i;
212 for (i = 0; i < RC_NIOPORT; i++)
213 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
216 /* Reset and setup CD180 chip */
217 static void __init rc_init_CD180(struct riscom_board const * bp)
219 unsigned long flags;
221 spin_lock_irqsave(&riscom_lock, flags);
223 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
224 rc_wait_CCR(bp); /* Wait for CCR ready */
225 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
226 spin_unlock_irqrestore(&riscom_lock, flags);
227 msleep(50); /* Delay 0.05 sec */
228 spin_lock_irqsave(&riscom_lock, flags);
229 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
230 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
231 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
232 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
233 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
235 /* Setting up prescaler. We need 4 ticks per 1 ms */
236 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
237 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
239 spin_unlock_irqrestore(&riscom_lock, flags);
242 /* Main probing routine, also sets irq. */
243 static int __init rc_probe(struct riscom_board *bp)
245 unsigned char val1, val2;
246 int irqs = 0;
247 int retries;
249 bp->irq = 0;
251 if (rc_request_io_range(bp))
252 return 1;
254 /* Are the I/O ports here ? */
255 rc_out(bp, CD180_PPRL, 0x5a);
256 outb(0xff, 0x80);
257 val1 = rc_in(bp, CD180_PPRL);
258 rc_out(bp, CD180_PPRL, 0xa5);
259 outb(0x00, 0x80);
260 val2 = rc_in(bp, CD180_PPRL);
262 if ((val1 != 0x5a) || (val2 != 0xa5)) {
263 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
264 board_No(bp), bp->base);
265 goto out_release;
268 /* It's time to find IRQ for this board */
269 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
270 irqs = probe_irq_on();
271 rc_init_CD180(bp); /* Reset CD180 chip */
272 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
273 rc_wait_CCR(bp);
274 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
275 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
276 msleep(50);
277 irqs = probe_irq_off(irqs);
278 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
279 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
280 rc_init_CD180(bp); /* Reset CD180 again */
282 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
283 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
284 "found.\n", board_No(bp), bp->base);
285 goto out_release;
289 if (irqs <= 0) {
290 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
291 "at 0x%03x.\n", board_No(bp), bp->base);
292 goto out_release;
294 bp->irq = irqs;
295 bp->flags |= RC_BOARD_PRESENT;
297 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
298 "0x%03x, IRQ %d.\n",
299 board_No(bp),
300 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
301 bp->base, bp->irq);
303 return 0;
304 out_release:
305 rc_release_io_range(bp);
306 return 1;
311 * Interrupt processing routines.
315 static inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
316 unsigned char const * what)
318 unsigned char channel;
319 struct riscom_port * port;
321 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
322 if (channel < CD180_NCH) {
323 port = &rc_port[board_No(bp) * RC_NPORT + channel];
324 if (port->flags & ASYNC_INITIALIZED) {
325 return port;
328 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
329 board_No(bp), what, channel);
330 return NULL;
333 static inline void rc_receive_exc(struct riscom_board const * bp)
335 struct riscom_port *port;
336 struct tty_struct *tty;
337 unsigned char status;
338 unsigned char ch, flag;
340 if (!(port = rc_get_port(bp, "Receive")))
341 return;
343 tty = port->tty;
345 #ifdef RC_REPORT_OVERRUN
346 status = rc_in(bp, CD180_RCSR);
347 if (status & RCSR_OE)
348 port->overrun++;
349 status &= port->mark_mask;
350 #else
351 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
352 #endif
353 ch = rc_in(bp, CD180_RDR);
354 if (!status) {
355 return;
357 if (status & RCSR_TOUT) {
358 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
359 "Hardware problems ?\n",
360 board_No(bp), port_No(port));
361 return;
363 } else if (status & RCSR_BREAK) {
364 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
365 board_No(bp), port_No(port));
366 flag = TTY_BREAK;
367 if (port->flags & ASYNC_SAK)
368 do_SAK(tty);
370 } else if (status & RCSR_PE)
371 flag = TTY_PARITY;
373 else if (status & RCSR_FE)
374 flag = TTY_FRAME;
376 else if (status & RCSR_OE)
377 flag = TTY_OVERRUN;
379 else
380 flag = TTY_NORMAL;
382 tty_insert_flip_char(tty, ch, flag);
383 tty_flip_buffer_push(tty);
386 static inline void rc_receive(struct riscom_board const * bp)
388 struct riscom_port *port;
389 struct tty_struct *tty;
390 unsigned char count;
392 if (!(port = rc_get_port(bp, "Receive")))
393 return;
395 tty = port->tty;
397 count = rc_in(bp, CD180_RDCR);
399 #ifdef RC_REPORT_FIFO
400 port->hits[count > 8 ? 9 : count]++;
401 #endif
403 while (count--) {
404 if (tty_buffer_request_room(tty, 1) == 0) {
405 printk(KERN_WARNING "rc%d: port %d: Working around "
406 "flip buffer overflow.\n",
407 board_No(bp), port_No(port));
408 break;
410 tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
412 tty_flip_buffer_push(tty);
415 static inline void rc_transmit(struct riscom_board const * bp)
417 struct riscom_port *port;
418 struct tty_struct *tty;
419 unsigned char count;
422 if (!(port = rc_get_port(bp, "Transmit")))
423 return;
425 tty = port->tty;
427 if (port->IER & IER_TXEMPTY) {
428 /* FIFO drained */
429 rc_out(bp, CD180_CAR, port_No(port));
430 port->IER &= ~IER_TXEMPTY;
431 rc_out(bp, CD180_IER, port->IER);
432 return;
435 if ((port->xmit_cnt <= 0 && !port->break_length)
436 || tty->stopped || tty->hw_stopped) {
437 rc_out(bp, CD180_CAR, port_No(port));
438 port->IER &= ~IER_TXRDY;
439 rc_out(bp, CD180_IER, port->IER);
440 return;
443 if (port->break_length) {
444 if (port->break_length > 0) {
445 if (port->COR2 & COR2_ETC) {
446 rc_out(bp, CD180_TDR, CD180_C_ESC);
447 rc_out(bp, CD180_TDR, CD180_C_SBRK);
448 port->COR2 &= ~COR2_ETC;
450 count = min_t(int, port->break_length, 0xff);
451 rc_out(bp, CD180_TDR, CD180_C_ESC);
452 rc_out(bp, CD180_TDR, CD180_C_DELAY);
453 rc_out(bp, CD180_TDR, count);
454 if (!(port->break_length -= count))
455 port->break_length--;
456 } else {
457 rc_out(bp, CD180_TDR, CD180_C_ESC);
458 rc_out(bp, CD180_TDR, CD180_C_EBRK);
459 rc_out(bp, CD180_COR2, port->COR2);
460 rc_wait_CCR(bp);
461 rc_out(bp, CD180_CCR, CCR_CORCHG2);
462 port->break_length = 0;
464 return;
467 count = CD180_NFIFO;
468 do {
469 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
470 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
471 if (--port->xmit_cnt <= 0)
472 break;
473 } while (--count > 0);
475 if (port->xmit_cnt <= 0) {
476 rc_out(bp, CD180_CAR, port_No(port));
477 port->IER &= ~IER_TXRDY;
478 rc_out(bp, CD180_IER, port->IER);
480 if (port->xmit_cnt <= port->wakeup_chars)
481 tty_wakeup(tty);
484 static inline void rc_check_modem(struct riscom_board const * bp)
486 struct riscom_port *port;
487 struct tty_struct *tty;
488 unsigned char mcr;
490 if (!(port = rc_get_port(bp, "Modem")))
491 return;
493 tty = port->tty;
495 mcr = rc_in(bp, CD180_MCR);
496 if (mcr & MCR_CDCHG) {
497 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
498 wake_up_interruptible(&port->open_wait);
499 else
500 tty_hangup(tty);
503 #ifdef RISCOM_BRAIN_DAMAGED_CTS
504 if (mcr & MCR_CTSCHG) {
505 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
506 tty->hw_stopped = 0;
507 port->IER |= IER_TXRDY;
508 if (port->xmit_cnt <= port->wakeup_chars)
509 tty_wakeup(tty);
510 } else {
511 tty->hw_stopped = 1;
512 port->IER &= ~IER_TXRDY;
514 rc_out(bp, CD180_IER, port->IER);
516 if (mcr & MCR_DSRCHG) {
517 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
518 tty->hw_stopped = 0;
519 port->IER |= IER_TXRDY;
520 if (port->xmit_cnt <= port->wakeup_chars)
521 tty_wakeup(tty);
522 } else {
523 tty->hw_stopped = 1;
524 port->IER &= ~IER_TXRDY;
526 rc_out(bp, CD180_IER, port->IER);
528 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
530 /* Clear change bits */
531 rc_out(bp, CD180_MCR, 0);
534 /* The main interrupt processing routine */
535 static irqreturn_t rc_interrupt(int dummy, void * dev_id)
537 unsigned char status;
538 unsigned char ack;
539 struct riscom_board *bp = dev_id;
540 unsigned long loop = 0;
541 int handled = 0;
543 if (!(bp->flags & RC_BOARD_ACTIVE))
544 return IRQ_NONE;
546 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
547 (RC_BSR_TOUT | RC_BSR_TINT |
548 RC_BSR_MINT | RC_BSR_RINT))) {
549 handled = 1;
550 if (status & RC_BSR_TOUT)
551 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
552 "error?\n", board_No(bp));
554 else if (status & RC_BSR_RINT) {
555 ack = rc_in(bp, RC_ACK_RINT);
557 if (ack == (RC_ID | GIVR_IT_RCV))
558 rc_receive(bp);
559 else if (ack == (RC_ID | GIVR_IT_REXC))
560 rc_receive_exc(bp);
561 else
562 printk(KERN_WARNING "rc%d: Bad receive ack "
563 "0x%02x.\n",
564 board_No(bp), ack);
566 } else if (status & RC_BSR_TINT) {
567 ack = rc_in(bp, RC_ACK_TINT);
569 if (ack == (RC_ID | GIVR_IT_TX))
570 rc_transmit(bp);
571 else
572 printk(KERN_WARNING "rc%d: Bad transmit ack "
573 "0x%02x.\n",
574 board_No(bp), ack);
576 } else /* if (status & RC_BSR_MINT) */ {
577 ack = rc_in(bp, RC_ACK_MINT);
579 if (ack == (RC_ID | GIVR_IT_MODEM))
580 rc_check_modem(bp);
581 else
582 printk(KERN_WARNING "rc%d: Bad modem ack "
583 "0x%02x.\n",
584 board_No(bp), ack);
588 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
589 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
591 return IRQ_RETVAL(handled);
595 * Routines for open & close processing.
598 /* Called with disabled interrupts */
599 static int rc_setup_board(struct riscom_board * bp)
601 int error;
603 if (bp->flags & RC_BOARD_ACTIVE)
604 return 0;
606 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
607 "RISCom/8", bp);
608 if (error)
609 return error;
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 */
615 bp->flags |= RC_BOARD_ACTIVE;
617 return 0;
620 /* Called with disabled interrupts */
621 static void rc_shutdown_board(struct riscom_board *bp)
623 if (!(bp->flags & RC_BOARD_ACTIVE))
624 return;
626 bp->flags &= ~RC_BOARD_ACTIVE;
628 free_irq(bp->irq, NULL);
630 bp->DTR = ~0;
631 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
636 * Setting up port characteristics.
637 * Must be called with disabled interrupts
639 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
641 struct tty_struct *tty;
642 unsigned long baud;
643 long tmp;
644 unsigned char cor1 = 0, cor3 = 0;
645 unsigned char mcor1 = 0, mcor2 = 0;
647 if (!(tty = port->tty) || !tty->termios)
648 return;
650 port->IER = 0;
651 port->COR2 = 0;
652 port->MSVR = MSVR_RTS;
654 baud = tty_get_baud_rate(tty);
656 /* Select port on the board */
657 rc_out(bp, CD180_CAR, port_No(port));
659 if (!baud) {
660 /* Drop DTR & exit */
661 bp->DTR |= (1u << port_No(port));
662 rc_out(bp, RC_DTR, bp->DTR);
663 return;
664 } else {
665 /* Set DTR on */
666 bp->DTR &= ~(1u << port_No(port));
667 rc_out(bp, RC_DTR, bp->DTR);
671 * Now we must calculate some speed depended things
674 /* Set baud rate for port */
675 tmp = (((RC_OSCFREQ + baud/2) / baud +
676 CD180_TPC/2) / CD180_TPC);
678 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
679 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
680 rc_out(bp, CD180_RBPRL, tmp & 0xff);
681 rc_out(bp, CD180_TBPRL, tmp & 0xff);
683 baud = (baud + 5) / 10; /* Estimated CPS */
685 /* Two timer ticks seems enough to wakeup something like SLIP driver */
686 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
687 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
688 SERIAL_XMIT_SIZE - 1 : tmp);
690 /* Receiver timeout will be transmission time for 1.5 chars */
691 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
692 tmp = (tmp > 0xff) ? 0xff : tmp;
693 rc_out(bp, CD180_RTPR, tmp);
695 switch (C_CSIZE(tty)) {
696 case CS5:
697 cor1 |= COR1_5BITS;
698 break;
699 case CS6:
700 cor1 |= COR1_6BITS;
701 break;
702 case CS7:
703 cor1 |= COR1_7BITS;
704 break;
705 case CS8:
706 cor1 |= COR1_8BITS;
707 break;
710 if (C_CSTOPB(tty))
711 cor1 |= COR1_2SB;
713 cor1 |= COR1_IGNORE;
714 if (C_PARENB(tty)) {
715 cor1 |= COR1_NORMPAR;
716 if (C_PARODD(tty))
717 cor1 |= COR1_ODDP;
718 if (I_INPCK(tty))
719 cor1 &= ~COR1_IGNORE;
721 /* Set marking of some errors */
722 port->mark_mask = RCSR_OE | RCSR_TOUT;
723 if (I_INPCK(tty))
724 port->mark_mask |= RCSR_FE | RCSR_PE;
725 if (I_BRKINT(tty) || I_PARMRK(tty))
726 port->mark_mask |= RCSR_BREAK;
727 if (I_IGNPAR(tty))
728 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
729 if (I_IGNBRK(tty)) {
730 port->mark_mask &= ~RCSR_BREAK;
731 if (I_IGNPAR(tty))
732 /* Real raw mode. Ignore all */
733 port->mark_mask &= ~RCSR_OE;
735 /* Enable Hardware Flow Control */
736 if (C_CRTSCTS(tty)) {
737 #ifdef RISCOM_BRAIN_DAMAGED_CTS
738 port->IER |= IER_DSR | IER_CTS;
739 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
740 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
741 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
742 #else
743 port->COR2 |= COR2_CTSAE;
744 #endif
746 /* Enable Software Flow Control. FIXME: I'm not sure about this */
747 /* Some people reported that it works, but I still doubt */
748 if (I_IXON(tty)) {
749 port->COR2 |= COR2_TXIBE;
750 cor3 |= (COR3_FCT | COR3_SCDE);
751 if (I_IXANY(tty))
752 port->COR2 |= COR2_IXM;
753 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
754 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
755 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
756 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
758 if (!C_CLOCAL(tty)) {
759 /* Enable CD check */
760 port->IER |= IER_CD;
761 mcor1 |= MCOR1_CDZD;
762 mcor2 |= MCOR2_CDOD;
765 if (C_CREAD(tty))
766 /* Enable receiver */
767 port->IER |= IER_RXD;
769 /* Set input FIFO size (1-8 bytes) */
770 cor3 |= RISCOM_RXFIFO;
771 /* Setting up CD180 channel registers */
772 rc_out(bp, CD180_COR1, cor1);
773 rc_out(bp, CD180_COR2, port->COR2);
774 rc_out(bp, CD180_COR3, cor3);
775 /* Make CD180 know about registers change */
776 rc_wait_CCR(bp);
777 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
778 /* Setting up modem option registers */
779 rc_out(bp, CD180_MCOR1, mcor1);
780 rc_out(bp, CD180_MCOR2, mcor2);
781 /* Enable CD180 transmitter & receiver */
782 rc_wait_CCR(bp);
783 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
784 /* Enable interrupts */
785 rc_out(bp, CD180_IER, port->IER);
786 /* And finally set RTS on */
787 rc_out(bp, CD180_MSVR, port->MSVR);
790 /* Must be called with interrupts enabled */
791 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
793 unsigned long flags;
795 if (port->flags & ASYNC_INITIALIZED)
796 return 0;
798 if (!port->xmit_buf) {
799 /* We may sleep in get_zeroed_page() */
800 unsigned long tmp;
802 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
803 return -ENOMEM;
805 if (port->xmit_buf) {
806 free_page(tmp);
807 return -ERESTARTSYS;
809 port->xmit_buf = (unsigned char *) tmp;
812 spin_lock_irqsave(&riscom_lock, flags);
814 if (port->tty)
815 clear_bit(TTY_IO_ERROR, &port->tty->flags);
817 if (port->count == 1)
818 bp->count++;
820 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
821 rc_change_speed(bp, port);
822 port->flags |= ASYNC_INITIALIZED;
824 spin_unlock_irqrestore(&riscom_lock, flags);
825 return 0;
828 /* Must be called with interrupts disabled */
829 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
831 struct tty_struct *tty;
833 if (!(port->flags & ASYNC_INITIALIZED))
834 return;
836 #ifdef RC_REPORT_OVERRUN
837 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
838 board_No(bp), port_No(port), port->overrun);
839 #endif
840 #ifdef RC_REPORT_FIFO
842 int i;
844 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
845 board_No(bp), port_No(port));
846 for (i = 0; i < 10; i++) {
847 printk("%ld ", port->hits[i]);
849 printk("].\n");
851 #endif
852 if (port->xmit_buf) {
853 free_page((unsigned long) port->xmit_buf);
854 port->xmit_buf = NULL;
857 if (!(tty = port->tty) || C_HUPCL(tty)) {
858 /* Drop DTR */
859 bp->DTR |= (1u << port_No(port));
860 rc_out(bp, RC_DTR, bp->DTR);
863 /* Select port */
864 rc_out(bp, CD180_CAR, port_No(port));
865 /* Reset port */
866 rc_wait_CCR(bp);
867 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
868 /* Disable all interrupts from this port */
869 port->IER = 0;
870 rc_out(bp, CD180_IER, port->IER);
872 if (tty)
873 set_bit(TTY_IO_ERROR, &tty->flags);
874 port->flags &= ~ASYNC_INITIALIZED;
876 if (--bp->count < 0) {
877 printk(KERN_INFO "rc%d: rc_shutdown_port: "
878 "bad board count: %d\n",
879 board_No(bp), bp->count);
880 bp->count = 0;
884 * If this is the last opened port on the board
885 * shutdown whole board
887 if (!bp->count)
888 rc_shutdown_board(bp);
892 static int block_til_ready(struct tty_struct *tty, struct file * filp,
893 struct riscom_port *port)
895 DECLARE_WAITQUEUE(wait, current);
896 struct riscom_board *bp = port_Board(port);
897 int retval;
898 int do_clocal = 0;
899 int CD;
900 unsigned long flags;
903 * If the device is in the middle of being closed, then block
904 * until it's done, and then try again.
906 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
907 interruptible_sleep_on(&port->close_wait);
908 if (port->flags & ASYNC_HUP_NOTIFY)
909 return -EAGAIN;
910 else
911 return -ERESTARTSYS;
915 * If non-blocking mode is set, or the port is not enabled,
916 * then make the check up front and then exit.
918 if ((filp->f_flags & O_NONBLOCK) ||
919 (tty->flags & (1 << TTY_IO_ERROR))) {
920 port->flags |= ASYNC_NORMAL_ACTIVE;
921 return 0;
924 if (C_CLOCAL(tty))
925 do_clocal = 1;
928 * Block waiting for the carrier detect and the line to become
929 * free (i.e., not in use by the callout). While we are in
930 * this loop, info->count is dropped by one, so that
931 * rs_close() knows when to free things. We restore it upon
932 * exit, either normal or abnormal.
934 retval = 0;
935 add_wait_queue(&port->open_wait, &wait);
937 spin_lock_irqsave(&riscom_lock, flags);
939 if (!tty_hung_up_p(filp))
940 port->count--;
942 spin_unlock_irqrestore(&riscom_lock, flags);
944 port->blocked_open++;
945 while (1) {
946 spin_lock_irqsave(&riscom_lock, flags);
948 rc_out(bp, CD180_CAR, port_No(port));
949 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
950 rc_out(bp, CD180_MSVR, MSVR_RTS);
951 bp->DTR &= ~(1u << port_No(port));
952 rc_out(bp, RC_DTR, bp->DTR);
954 spin_unlock_irqrestore(&riscom_lock, flags);
956 set_current_state(TASK_INTERRUPTIBLE);
957 if (tty_hung_up_p(filp) ||
958 !(port->flags & ASYNC_INITIALIZED)) {
959 if (port->flags & ASYNC_HUP_NOTIFY)
960 retval = -EAGAIN;
961 else
962 retval = -ERESTARTSYS;
963 break;
965 if (!(port->flags & ASYNC_CLOSING) &&
966 (do_clocal || CD))
967 break;
968 if (signal_pending(current)) {
969 retval = -ERESTARTSYS;
970 break;
972 schedule();
974 __set_current_state(TASK_RUNNING);
975 remove_wait_queue(&port->open_wait, &wait);
976 if (!tty_hung_up_p(filp))
977 port->count++;
978 port->blocked_open--;
979 if (retval)
980 return retval;
982 port->flags |= ASYNC_NORMAL_ACTIVE;
983 return 0;
986 static int rc_open(struct tty_struct * tty, struct file * filp)
988 int board;
989 int error;
990 struct riscom_port * port;
991 struct riscom_board * bp;
993 board = RC_BOARD(tty->index);
994 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
995 return -ENODEV;
997 bp = &rc_board[board];
998 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
999 if (rc_paranoia_check(port, tty->name, "rc_open"))
1000 return -ENODEV;
1002 if ((error = rc_setup_board(bp)))
1003 return error;
1005 port->count++;
1006 tty->driver_data = port;
1007 port->tty = tty;
1009 if ((error = rc_setup_port(bp, port)))
1010 return error;
1012 if ((error = block_til_ready(tty, filp, port)))
1013 return error;
1015 return 0;
1018 static void rc_close(struct tty_struct * tty, struct file * filp)
1020 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1021 struct riscom_board *bp;
1022 unsigned long flags;
1023 unsigned long timeout;
1025 if (!port || rc_paranoia_check(port, tty->name, "close"))
1026 return;
1028 spin_lock_irqsave(&riscom_lock, flags);
1030 if (tty_hung_up_p(filp))
1031 goto out;
1033 bp = port_Board(port);
1034 if ((tty->count == 1) && (port->count != 1)) {
1035 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1036 " tty->count is 1, port count is %d\n",
1037 board_No(bp), port->count);
1038 port->count = 1;
1040 if (--port->count < 0) {
1041 printk(KERN_INFO "rc%d: rc_close: bad port count "
1042 "for tty%d: %d\n",
1043 board_No(bp), port_No(port), port->count);
1044 port->count = 0;
1046 if (port->count)
1047 goto out;
1048 port->flags |= ASYNC_CLOSING;
1050 * Now we wait for the transmit buffer to clear; and we notify
1051 * the line discipline to only process XON/XOFF characters.
1053 tty->closing = 1;
1054 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1055 tty_wait_until_sent(tty, port->closing_wait);
1057 * At this point we stop accepting input. To do this, we
1058 * disable the receive line status interrupts, and tell the
1059 * interrupt driver to stop checking the data ready bit in the
1060 * line status register.
1062 port->IER &= ~IER_RXD;
1063 if (port->flags & ASYNC_INITIALIZED) {
1064 port->IER &= ~IER_TXRDY;
1065 port->IER |= IER_TXEMPTY;
1066 rc_out(bp, CD180_CAR, port_No(port));
1067 rc_out(bp, CD180_IER, port->IER);
1069 * Before we drop DTR, make sure the UART transmitter
1070 * has completely drained; this is especially
1071 * important if there is a transmit FIFO!
1073 timeout = jiffies+HZ;
1074 while(port->IER & IER_TXEMPTY) {
1075 msleep_interruptible(jiffies_to_msecs(port->timeout));
1076 if (time_after(jiffies, timeout))
1077 break;
1080 rc_shutdown_port(bp, port);
1081 if (tty->driver->flush_buffer)
1082 tty->driver->flush_buffer(tty);
1083 tty_ldisc_flush(tty);
1085 tty->closing = 0;
1086 port->tty = NULL;
1087 if (port->blocked_open) {
1088 if (port->close_delay) {
1089 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1091 wake_up_interruptible(&port->open_wait);
1093 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1094 wake_up_interruptible(&port->close_wait);
1096 out:
1097 spin_unlock_irqrestore(&riscom_lock, flags);
1100 static int rc_write(struct tty_struct * tty,
1101 const unsigned char *buf, int count)
1103 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1104 struct riscom_board *bp;
1105 int c, total = 0;
1106 unsigned long flags;
1108 if (rc_paranoia_check(port, tty->name, "rc_write"))
1109 return 0;
1111 bp = port_Board(port);
1113 if (!tty || !port->xmit_buf)
1114 return 0;
1116 while (1) {
1117 spin_lock_irqsave(&riscom_lock, flags);
1119 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1120 SERIAL_XMIT_SIZE - port->xmit_head));
1121 if (c <= 0)
1122 break; /* lock continues to be held */
1124 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1125 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1126 port->xmit_cnt += c;
1128 spin_unlock_irqrestore(&riscom_lock, flags);
1130 buf += c;
1131 count -= c;
1132 total += c;
1135 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1136 !(port->IER & IER_TXRDY)) {
1137 port->IER |= IER_TXRDY;
1138 rc_out(bp, CD180_CAR, port_No(port));
1139 rc_out(bp, CD180_IER, port->IER);
1142 spin_unlock_irqrestore(&riscom_lock, flags);
1144 return total;
1147 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1149 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1150 unsigned long flags;
1152 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1153 return;
1155 if (!tty || !port->xmit_buf)
1156 return;
1158 spin_lock_irqsave(&riscom_lock, flags);
1160 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1161 goto out;
1163 port->xmit_buf[port->xmit_head++] = ch;
1164 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1165 port->xmit_cnt++;
1167 out:
1168 spin_unlock_irqrestore(&riscom_lock, flags);
1171 static void rc_flush_chars(struct tty_struct * tty)
1173 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1174 unsigned long flags;
1176 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1177 return;
1179 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1180 !port->xmit_buf)
1181 return;
1183 spin_lock_irqsave(&riscom_lock, flags);
1185 port->IER |= IER_TXRDY;
1186 rc_out(port_Board(port), CD180_CAR, port_No(port));
1187 rc_out(port_Board(port), CD180_IER, port->IER);
1189 spin_unlock_irqrestore(&riscom_lock, flags);
1192 static int rc_write_room(struct tty_struct * tty)
1194 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1195 int ret;
1197 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1198 return 0;
1200 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1201 if (ret < 0)
1202 ret = 0;
1203 return ret;
1206 static int rc_chars_in_buffer(struct tty_struct *tty)
1208 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1210 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1211 return 0;
1213 return port->xmit_cnt;
1216 static void rc_flush_buffer(struct tty_struct *tty)
1218 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1219 unsigned long flags;
1221 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1222 return;
1224 spin_lock_irqsave(&riscom_lock, flags);
1226 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1228 spin_unlock_irqrestore(&riscom_lock, flags);
1230 tty_wakeup(tty);
1233 static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1235 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1236 struct riscom_board * bp;
1237 unsigned char status;
1238 unsigned int result;
1239 unsigned long flags;
1241 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1242 return -ENODEV;
1244 bp = port_Board(port);
1246 spin_lock_irqsave(&riscom_lock, flags);
1248 rc_out(bp, CD180_CAR, port_No(port));
1249 status = rc_in(bp, CD180_MSVR);
1250 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1252 spin_unlock_irqrestore(&riscom_lock, flags);
1254 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1255 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1256 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1257 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1258 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1259 return result;
1262 static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1263 unsigned int set, unsigned int clear)
1265 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1266 unsigned long flags;
1267 struct riscom_board *bp;
1269 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1270 return -ENODEV;
1272 bp = port_Board(port);
1274 spin_lock_irqsave(&riscom_lock, flags);
1276 if (set & TIOCM_RTS)
1277 port->MSVR |= MSVR_RTS;
1278 if (set & TIOCM_DTR)
1279 bp->DTR &= ~(1u << port_No(port));
1281 if (clear & TIOCM_RTS)
1282 port->MSVR &= ~MSVR_RTS;
1283 if (clear & TIOCM_DTR)
1284 bp->DTR |= (1u << port_No(port));
1286 rc_out(bp, CD180_CAR, port_No(port));
1287 rc_out(bp, CD180_MSVR, port->MSVR);
1288 rc_out(bp, RC_DTR, bp->DTR);
1290 spin_unlock_irqrestore(&riscom_lock, flags);
1292 return 0;
1295 static inline void rc_send_break(struct riscom_port * port, unsigned long length)
1297 struct riscom_board *bp = port_Board(port);
1298 unsigned long flags;
1300 spin_lock_irqsave(&riscom_lock, flags);
1302 port->break_length = RISCOM_TPS / HZ * length;
1303 port->COR2 |= COR2_ETC;
1304 port->IER |= IER_TXRDY;
1305 rc_out(bp, CD180_CAR, port_No(port));
1306 rc_out(bp, CD180_COR2, port->COR2);
1307 rc_out(bp, CD180_IER, port->IER);
1308 rc_wait_CCR(bp);
1309 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1310 rc_wait_CCR(bp);
1312 spin_unlock_irqrestore(&riscom_lock, flags);
1315 static inline int rc_set_serial_info(struct riscom_port * port,
1316 struct serial_struct __user * newinfo)
1318 struct serial_struct tmp;
1319 struct riscom_board *bp = port_Board(port);
1320 int change_speed;
1322 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1323 return -EFAULT;
1325 #if 0
1326 if ((tmp.irq != bp->irq) ||
1327 (tmp.port != bp->base) ||
1328 (tmp.type != PORT_CIRRUS) ||
1329 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1330 (tmp.custom_divisor != 0) ||
1331 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1332 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1333 return -EINVAL;
1334 #endif
1336 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1337 (tmp.flags & ASYNC_SPD_MASK));
1339 if (!capable(CAP_SYS_ADMIN)) {
1340 if ((tmp.close_delay != port->close_delay) ||
1341 (tmp.closing_wait != port->closing_wait) ||
1342 ((tmp.flags & ~ASYNC_USR_MASK) !=
1343 (port->flags & ~ASYNC_USR_MASK)))
1344 return -EPERM;
1345 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1346 (tmp.flags & ASYNC_USR_MASK));
1347 } else {
1348 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1349 (tmp.flags & ASYNC_FLAGS));
1350 port->close_delay = tmp.close_delay;
1351 port->closing_wait = tmp.closing_wait;
1353 if (change_speed) {
1354 unsigned long flags;
1356 spin_lock_irqsave(&riscom_lock, flags);
1357 rc_change_speed(bp, port);
1358 spin_unlock_irqrestore(&riscom_lock, flags);
1360 return 0;
1363 static inline int rc_get_serial_info(struct riscom_port * port,
1364 struct serial_struct __user *retinfo)
1366 struct serial_struct tmp;
1367 struct riscom_board *bp = port_Board(port);
1369 memset(&tmp, 0, sizeof(tmp));
1370 tmp.type = PORT_CIRRUS;
1371 tmp.line = port - rc_port;
1372 tmp.port = bp->base;
1373 tmp.irq = bp->irq;
1374 tmp.flags = port->flags;
1375 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1376 tmp.close_delay = port->close_delay * HZ/100;
1377 tmp.closing_wait = port->closing_wait * HZ/100;
1378 tmp.xmit_fifo_size = CD180_NFIFO;
1379 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1382 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1383 unsigned int cmd, unsigned long arg)
1386 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1387 void __user *argp = (void __user *)arg;
1388 int retval;
1390 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1391 return -ENODEV;
1393 switch (cmd) {
1394 case TCSBRK: /* SVID version: non-zero arg --> no break */
1395 retval = tty_check_change(tty);
1396 if (retval)
1397 return retval;
1398 tty_wait_until_sent(tty, 0);
1399 if (!arg)
1400 rc_send_break(port, HZ/4); /* 1/4 second */
1401 break;
1402 case TCSBRKP: /* support for POSIX tcsendbreak() */
1403 retval = tty_check_change(tty);
1404 if (retval)
1405 return retval;
1406 tty_wait_until_sent(tty, 0);
1407 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1408 break;
1409 case TIOCGSOFTCAR:
1410 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned __user *)argp);
1411 case TIOCSSOFTCAR:
1412 if (get_user(arg,(unsigned __user *) argp))
1413 return -EFAULT;
1414 tty->termios->c_cflag =
1415 ((tty->termios->c_cflag & ~CLOCAL) |
1416 (arg ? CLOCAL : 0));
1417 break;
1418 case TIOCGSERIAL:
1419 return rc_get_serial_info(port, argp);
1420 case TIOCSSERIAL:
1421 return rc_set_serial_info(port, argp);
1422 default:
1423 return -ENOIOCTLCMD;
1425 return 0;
1428 static void rc_throttle(struct tty_struct * tty)
1430 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1431 struct riscom_board *bp;
1432 unsigned long flags;
1434 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1435 return;
1437 bp = port_Board(port);
1439 spin_lock_irqsave(&riscom_lock, flags);
1441 port->MSVR &= ~MSVR_RTS;
1442 rc_out(bp, CD180_CAR, port_No(port));
1443 if (I_IXOFF(tty)) {
1444 rc_wait_CCR(bp);
1445 rc_out(bp, CD180_CCR, CCR_SSCH2);
1446 rc_wait_CCR(bp);
1448 rc_out(bp, CD180_MSVR, port->MSVR);
1450 spin_unlock_irqrestore(&riscom_lock, flags);
1453 static void rc_unthrottle(struct tty_struct * tty)
1455 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1456 struct riscom_board *bp;
1457 unsigned long flags;
1459 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1460 return;
1462 bp = port_Board(port);
1464 spin_lock_irqsave(&riscom_lock, flags);
1466 port->MSVR |= MSVR_RTS;
1467 rc_out(bp, CD180_CAR, port_No(port));
1468 if (I_IXOFF(tty)) {
1469 rc_wait_CCR(bp);
1470 rc_out(bp, CD180_CCR, CCR_SSCH1);
1471 rc_wait_CCR(bp);
1473 rc_out(bp, CD180_MSVR, port->MSVR);
1475 spin_unlock_irqrestore(&riscom_lock, flags);
1478 static void rc_stop(struct tty_struct * tty)
1480 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1481 struct riscom_board *bp;
1482 unsigned long flags;
1484 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1485 return;
1487 bp = port_Board(port);
1489 spin_lock_irqsave(&riscom_lock, flags);
1491 port->IER &= ~IER_TXRDY;
1492 rc_out(bp, CD180_CAR, port_No(port));
1493 rc_out(bp, CD180_IER, port->IER);
1495 spin_unlock_irqrestore(&riscom_lock, flags);
1498 static void rc_start(struct tty_struct * tty)
1500 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1501 struct riscom_board *bp;
1502 unsigned long flags;
1504 if (rc_paranoia_check(port, tty->name, "rc_start"))
1505 return;
1507 bp = port_Board(port);
1509 spin_lock_irqsave(&riscom_lock, flags);
1511 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1512 port->IER |= IER_TXRDY;
1513 rc_out(bp, CD180_CAR, port_No(port));
1514 rc_out(bp, CD180_IER, port->IER);
1517 spin_unlock_irqrestore(&riscom_lock, flags);
1520 static void rc_hangup(struct tty_struct * tty)
1522 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1523 struct riscom_board *bp;
1525 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1526 return;
1528 bp = port_Board(port);
1530 rc_shutdown_port(bp, port);
1531 port->count = 0;
1532 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1533 port->tty = NULL;
1534 wake_up_interruptible(&port->open_wait);
1537 static void rc_set_termios(struct tty_struct * tty, struct ktermios * old_termios)
1539 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1540 unsigned long flags;
1542 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1543 return;
1545 if (tty->termios->c_cflag == old_termios->c_cflag &&
1546 tty->termios->c_iflag == old_termios->c_iflag)
1547 return;
1549 spin_lock_irqsave(&riscom_lock, flags);
1550 rc_change_speed(port_Board(port), port);
1551 spin_unlock_irqrestore(&riscom_lock, flags);
1553 if ((old_termios->c_cflag & CRTSCTS) &&
1554 !(tty->termios->c_cflag & CRTSCTS)) {
1555 tty->hw_stopped = 0;
1556 rc_start(tty);
1560 static const struct tty_operations riscom_ops = {
1561 .open = rc_open,
1562 .close = rc_close,
1563 .write = rc_write,
1564 .put_char = rc_put_char,
1565 .flush_chars = rc_flush_chars,
1566 .write_room = rc_write_room,
1567 .chars_in_buffer = rc_chars_in_buffer,
1568 .flush_buffer = rc_flush_buffer,
1569 .ioctl = rc_ioctl,
1570 .throttle = rc_throttle,
1571 .unthrottle = rc_unthrottle,
1572 .set_termios = rc_set_termios,
1573 .stop = rc_stop,
1574 .start = rc_start,
1575 .hangup = rc_hangup,
1576 .tiocmget = rc_tiocmget,
1577 .tiocmset = rc_tiocmset,
1580 static int __init rc_init_drivers(void)
1582 int error;
1583 int i;
1585 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1586 if (!riscom_driver)
1587 return -ENOMEM;
1589 riscom_driver->owner = THIS_MODULE;
1590 riscom_driver->name = "ttyL";
1591 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1592 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1593 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1594 riscom_driver->init_termios = tty_std_termios;
1595 riscom_driver->init_termios.c_cflag =
1596 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1597 riscom_driver->init_termios.c_ispeed = 9600;
1598 riscom_driver->init_termios.c_ospeed = 9600;
1599 riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1600 tty_set_operations(riscom_driver, &riscom_ops);
1601 if ((error = tty_register_driver(riscom_driver))) {
1602 put_tty_driver(riscom_driver);
1603 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1604 "error = %d\n",
1605 error);
1606 return 1;
1609 memset(rc_port, 0, sizeof(rc_port));
1610 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1611 rc_port[i].magic = RISCOM8_MAGIC;
1612 rc_port[i].close_delay = 50 * HZ/100;
1613 rc_port[i].closing_wait = 3000 * HZ/100;
1614 init_waitqueue_head(&rc_port[i].open_wait);
1615 init_waitqueue_head(&rc_port[i].close_wait);
1618 return 0;
1621 static void rc_release_drivers(void)
1623 tty_unregister_driver(riscom_driver);
1624 put_tty_driver(riscom_driver);
1627 #ifndef MODULE
1629 * Called at boot time.
1631 * You can specify IO base for up to RC_NBOARD cards,
1632 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1633 * Note that there will be no probing at default
1634 * addresses in this case.
1637 static int __init riscom8_setup(char *str)
1639 int ints[RC_NBOARD];
1640 int i;
1642 str = get_options(str, ARRAY_SIZE(ints), ints);
1644 for (i = 0; i < RC_NBOARD; i++) {
1645 if (i < ints[0])
1646 rc_board[i].base = ints[i+1];
1647 else
1648 rc_board[i].base = 0;
1650 return 1;
1653 __setup("riscom8=", riscom8_setup);
1654 #endif
1656 static char banner[] __initdata =
1657 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1658 "1994-1996.\n";
1659 static char no_boards_msg[] __initdata =
1660 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1663 * This routine must be called by kernel at boot time
1665 static int __init riscom8_init(void)
1667 int i;
1668 int found = 0;
1670 printk(banner);
1672 if (rc_init_drivers())
1673 return -EIO;
1675 for (i = 0; i < RC_NBOARD; i++)
1676 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1677 found++;
1679 if (!found) {
1680 rc_release_drivers();
1681 printk(no_boards_msg);
1682 return -EIO;
1684 return 0;
1687 #ifdef MODULE
1688 static int iobase;
1689 static int iobase1;
1690 static int iobase2;
1691 static int iobase3;
1692 module_param(iobase, int, 0);
1693 module_param(iobase1, int, 0);
1694 module_param(iobase2, int, 0);
1695 module_param(iobase3, int, 0);
1697 MODULE_LICENSE("GPL");
1698 #endif /* MODULE */
1701 * You can setup up to 4 boards (current value of RC_NBOARD)
1702 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1705 static int __init riscom8_init_module (void)
1707 #ifdef MODULE
1708 int i;
1710 if (iobase || iobase1 || iobase2 || iobase3) {
1711 for(i = 0; i < RC_NBOARD; i++)
1712 rc_board[i].base = 0;
1715 if (iobase)
1716 rc_board[0].base = iobase;
1717 if (iobase1)
1718 rc_board[1].base = iobase1;
1719 if (iobase2)
1720 rc_board[2].base = iobase2;
1721 if (iobase3)
1722 rc_board[3].base = iobase3;
1723 #endif /* MODULE */
1725 return riscom8_init();
1728 static void __exit riscom8_exit_module (void)
1730 int i;
1732 rc_release_drivers();
1733 for (i = 0; i < RC_NBOARD; i++)
1734 if (rc_board[i].flags & RC_BOARD_PRESENT)
1735 rc_release_io_range(&rc_board[i]);
1739 module_init(riscom8_init_module);
1740 module_exit(riscom8_exit_module);