Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / riscom8.c
blob34e1dd51ce069adbfa5554edb5a74d30c64af9f2
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.0
30 #include <linux/module.h>
32 #include <asm/io.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/ioport.h>
36 #include <linux/interrupt.h>
37 #include <linux/errno.h>
38 #include <linux/tty.h>
39 #include <linux/mm.h>
40 #include <linux/serial.h>
41 #include <linux/fcntl.h>
42 #include <linux/major.h>
43 #include <linux/init.h>
45 #include <asm/uaccess.h>
47 #include "riscom8.h"
48 #include "riscom8_reg.h"
50 /* Am I paranoid or not ? ;-) */
51 #define RISCOM_PARANOIA_CHECK
53 /*
54 * Crazy InteliCom/8 boards sometimes has swapped CTS & DSR signals.
55 * You can slightly speed up things by #undefing the following option,
56 * if you are REALLY sure that your board is correct one.
59 #define RISCOM_BRAIN_DAMAGED_CTS
61 /*
62 * The following defines are mostly for testing purposes. But if you need
63 * some nice reporting in your syslog, you can define them also.
65 #undef RC_REPORT_FIFO
66 #undef RC_REPORT_OVERRUN
69 #define RISCOM_LEGAL_FLAGS \
70 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
71 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
72 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
74 #ifndef MIN
75 #define MIN(a,b) ((a) < (b) ? (a) : (b))
76 #endif
78 #define RS_EVENT_WRITE_WAKEUP 0
80 DECLARE_TASK_QUEUE(tq_riscom);
82 #define RISCOM_TYPE_NORMAL 1
83 #define RISCOM_TYPE_CALLOUT 2
85 static struct riscom_board * IRQ_to_board[16] = { NULL, } ;
86 static struct tty_driver riscom_driver, riscom_callout_driver;
87 static int riscom_refcount = 0;
88 static struct tty_struct * riscom_table[RC_NBOARD * RC_NPORT] = { NULL, };
89 static struct termios * riscom_termios[RC_NBOARD * RC_NPORT] = { NULL, };
90 static struct termios * riscom_termios_locked[RC_NBOARD * RC_NPORT] = { NULL, };
91 static unsigned char * tmp_buf = NULL;
92 static DECLARE_MUTEX(tmp_buf_sem);
94 static unsigned long baud_table[] = {
95 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
96 9600, 19200, 38400, 57600, 76800, 0,
99 static struct riscom_board rc_board[RC_NBOARD] = {
100 { 0, RC_IOBASE1, 0, },
101 { 0, RC_IOBASE2, 0, },
102 { 0, RC_IOBASE3, 0, },
103 { 0, RC_IOBASE4, 0, },
106 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT] = {
107 { 0, },
110 /* RISCom/8 I/O ports addresses (without address translation) */
111 static unsigned short rc_ioport[] = {
112 #if 1
113 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
114 #else
115 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
116 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
117 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
118 #endif
120 #define RC_NIOPORT (sizeof(rc_ioport) / sizeof(rc_ioport[0]))
123 static inline int rc_paranoia_check(struct riscom_port const * port,
124 kdev_t device, const char *routine)
126 #ifdef RISCOM_PARANOIA_CHECK
127 static const char *badmagic =
128 "rc: Warning: bad riscom port magic number for device %s in %s\n";
129 static const char *badinfo =
130 "rc: Warning: null riscom port for device %s in %s\n";
132 if (!port) {
133 printk(badinfo, kdevname(device), routine);
134 return 1;
136 if (port->magic != RISCOM8_MAGIC) {
137 printk(badmagic, kdevname(device), routine);
138 return 1;
140 #endif
141 return 0;
146 * Service functions for RISCom/8 driver.
150 /* Get board number from pointer */
151 extern inline int board_No (struct riscom_board const * bp)
153 return bp - rc_board;
156 /* Get port number from pointer */
157 extern inline int port_No (struct riscom_port const * port)
159 return RC_PORT(port - rc_port);
162 /* Get pointer to board from pointer to port */
163 extern inline struct riscom_board * port_Board(struct riscom_port const * port)
165 return &rc_board[RC_BOARD(port - rc_port)];
168 /* Input Byte from CL CD180 register */
169 extern inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
171 return inb(bp->base + RC_TO_ISA(reg));
174 /* Output Byte to CL CD180 register */
175 extern inline void rc_out(struct riscom_board const * bp, unsigned short reg,
176 unsigned char val)
178 outb(val, bp->base + RC_TO_ISA(reg));
181 /* Wait for Channel Command Register ready */
182 extern inline void rc_wait_CCR(struct riscom_board const * bp)
184 unsigned long delay;
186 /* FIXME: need something more descriptive then 100000 :) */
187 for (delay = 100000; delay; delay--)
188 if (!rc_in(bp, CD180_CCR))
189 return;
191 printk("rc%d: Timeout waiting for CCR.\n", board_No(bp));
195 * RISCom/8 probe functions.
198 extern inline int rc_check_io_range(struct riscom_board * const bp)
200 int i;
202 for (i = 0; i < RC_NIOPORT; i++)
203 if (check_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1)) {
204 printk("rc%d: Skipping probe at 0x%03x. I/O address in use.\n",
205 board_No(bp), bp->base);
206 return 1;
208 return 0;
211 extern inline void rc_request_io_range(struct riscom_board * const bp)
213 int i;
215 for (i = 0; i < RC_NIOPORT; i++)
216 request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1, "RISCom/8" );
219 extern inline void rc_release_io_range(struct riscom_board * const bp)
221 int i;
223 for (i = 0; i < RC_NIOPORT; i++)
224 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
228 /* Must be called with enabled interrupts */
229 extern inline void rc_long_delay(unsigned long delay)
231 unsigned long i;
233 for (i = jiffies + delay; time_after(i,jiffies); ) ;
236 /* Reset and setup CD180 chip */
237 static void __init rc_init_CD180(struct riscom_board const * bp)
239 unsigned long flags;
241 save_flags(flags); cli();
242 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
243 rc_wait_CCR(bp); /* Wait for CCR ready */
244 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
245 sti();
246 rc_long_delay(HZ/20); /* Delay 0.05 sec */
247 cli();
248 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
249 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
250 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
251 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
252 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
254 /* Setting up prescaler. We need 4 ticks per 1 ms */
255 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
256 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
258 restore_flags(flags);
261 /* Main probing routine, also sets irq. */
262 static int __init rc_probe(struct riscom_board *bp)
264 unsigned char val1, val2;
265 int irqs = 0;
266 int retries;
268 bp->irq = 0;
270 if (rc_check_io_range(bp))
271 return 1;
273 /* Are the I/O ports here ? */
274 rc_out(bp, CD180_PPRL, 0x5a);
275 outb(0xff, 0x80);
276 val1 = rc_in(bp, CD180_PPRL);
277 rc_out(bp, CD180_PPRL, 0xa5);
278 outb(0x00, 0x80);
279 val2 = rc_in(bp, CD180_PPRL);
281 if ((val1 != 0x5a) || (val2 != 0xa5)) {
282 printk("rc%d: RISCom/8 Board at 0x%03x not found.\n",
283 board_No(bp), bp->base);
284 return 1;
287 /* It's time to find IRQ for this board */
288 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
289 irqs = probe_irq_on();
290 rc_init_CD180(bp); /* Reset CD180 chip */
291 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
292 rc_wait_CCR(bp);
293 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
294 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
295 rc_long_delay(HZ/20);
296 irqs = probe_irq_off(irqs);
297 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
298 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
299 rc_init_CD180(bp); /* Reset CD180 again */
301 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
302 printk("rc%d: RISCom/8 Board at 0x%03x not found.\n",
303 board_No(bp), bp->base);
304 return 1;
308 if (irqs <= 0) {
309 printk("rc%d: Can't find IRQ for RISCom/8 board at 0x%03x.\n",
310 board_No(bp), bp->base);
311 return 1;
313 rc_request_io_range(bp);
314 bp->irq = irqs;
315 bp->flags |= RC_BOARD_PRESENT;
317 printk("rc%d: RISCom/8 Rev. %c board detected at 0x%03x, IRQ %d.\n",
318 board_No(bp),
319 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
320 bp->base, bp->irq);
322 return 0;
327 * Interrupt processing routines.
331 extern inline void rc_mark_event(struct riscom_port * port, int event)
334 * I'm not quite happy with current scheme all serial
335 * drivers use their own BH routine.
336 * It seems this easily can be done with one BH routine
337 * serving for all serial drivers.
338 * For now I must introduce another one - RISCOM8_BH.
339 * Still hope this will be changed in near future.
341 set_bit(event, &port->event);
342 queue_task(&port->tqueue, &tq_riscom);
343 mark_bh(RISCOM8_BH);
346 extern inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
347 unsigned char const * what)
349 unsigned char channel;
350 struct riscom_port * port;
352 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
353 if (channel < CD180_NCH) {
354 port = &rc_port[board_No(bp) * RC_NPORT + channel];
355 if (port->flags & ASYNC_INITIALIZED) {
356 return port;
359 printk("rc%d: %s interrupt from invalid port %d\n",
360 board_No(bp), what, channel);
361 return NULL;
364 extern inline void rc_receive_exc(struct riscom_board const * bp)
366 struct riscom_port *port;
367 struct tty_struct *tty;
368 unsigned char status;
369 unsigned char ch;
371 if (!(port = rc_get_port(bp, "Receive")))
372 return;
374 tty = port->tty;
375 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
376 printk("rc%d: port %d: Working around flip buffer overflow.\n",
377 board_No(bp), port_No(port));
378 return;
381 #ifdef RC_REPORT_OVERRUN
382 status = rc_in(bp, CD180_RCSR);
383 if (status & RCSR_OE) {
384 port->overrun++;
385 #if 0
386 printk("rc%d: port %d: Overrun. Total %ld overruns.\n",
387 board_No(bp), port_No(port), port->overrun);
388 #endif
390 status &= port->mark_mask;
391 #else
392 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
393 #endif
394 ch = rc_in(bp, CD180_RDR);
395 if (!status) {
396 return;
398 if (status & RCSR_TOUT) {
399 printk("rc%d: port %d: Receiver timeout. Hardware problems ?\n",
400 board_No(bp), port_No(port));
401 return;
403 } else if (status & RCSR_BREAK) {
404 printk("rc%d: port %d: Handling break...\n",
405 board_No(bp), port_No(port));
406 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
407 if (port->flags & ASYNC_SAK)
408 do_SAK(tty);
410 } else if (status & RCSR_PE)
411 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
413 else if (status & RCSR_FE)
414 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
416 else if (status & RCSR_OE)
417 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
419 else
420 *tty->flip.flag_buf_ptr++ = 0;
422 *tty->flip.char_buf_ptr++ = ch;
423 tty->flip.count++;
424 queue_task(&tty->flip.tqueue, &tq_timer);
427 extern inline void rc_receive(struct riscom_board const * bp)
429 struct riscom_port *port;
430 struct tty_struct *tty;
431 unsigned char count;
433 if (!(port = rc_get_port(bp, "Receive")))
434 return;
436 tty = port->tty;
438 count = rc_in(bp, CD180_RDCR);
440 #ifdef RC_REPORT_FIFO
441 port->hits[count > 8 ? 9 : count]++;
442 #endif
444 while (count--) {
445 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
446 printk("rc%d: port %d: Working around flip buffer overflow.\n",
447 board_No(bp), port_No(port));
448 break;
450 *tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);
451 *tty->flip.flag_buf_ptr++ = 0;
452 tty->flip.count++;
454 queue_task(&tty->flip.tqueue, &tq_timer);
457 extern inline void rc_transmit(struct riscom_board const * bp)
459 struct riscom_port *port;
460 struct tty_struct *tty;
461 unsigned char count;
464 if (!(port = rc_get_port(bp, "Transmit")))
465 return;
467 tty = port->tty;
469 if (port->IER & IER_TXEMPTY) {
470 /* FIFO drained */
471 rc_out(bp, CD180_CAR, port_No(port));
472 port->IER &= ~IER_TXEMPTY;
473 rc_out(bp, CD180_IER, port->IER);
474 return;
477 if ((port->xmit_cnt <= 0 && !port->break_length)
478 || tty->stopped || tty->hw_stopped) {
479 rc_out(bp, CD180_CAR, port_No(port));
480 port->IER &= ~IER_TXRDY;
481 rc_out(bp, CD180_IER, port->IER);
482 return;
485 if (port->break_length) {
486 if (port->break_length > 0) {
487 if (port->COR2 & COR2_ETC) {
488 rc_out(bp, CD180_TDR, CD180_C_ESC);
489 rc_out(bp, CD180_TDR, CD180_C_SBRK);
490 port->COR2 &= ~COR2_ETC;
492 count = MIN(port->break_length, 0xff);
493 rc_out(bp, CD180_TDR, CD180_C_ESC);
494 rc_out(bp, CD180_TDR, CD180_C_DELAY);
495 rc_out(bp, CD180_TDR, count);
496 if (!(port->break_length -= count))
497 port->break_length--;
498 } else {
499 rc_out(bp, CD180_TDR, CD180_C_ESC);
500 rc_out(bp, CD180_TDR, CD180_C_EBRK);
501 rc_out(bp, CD180_COR2, port->COR2);
502 rc_wait_CCR(bp);
503 rc_out(bp, CD180_CCR, CCR_CORCHG2);
504 port->break_length = 0;
506 return;
509 count = CD180_NFIFO;
510 do {
511 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
512 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
513 if (--port->xmit_cnt <= 0)
514 break;
515 } while (--count > 0);
517 if (port->xmit_cnt <= 0) {
518 rc_out(bp, CD180_CAR, port_No(port));
519 port->IER &= ~IER_TXRDY;
520 rc_out(bp, CD180_IER, port->IER);
522 if (port->xmit_cnt <= port->wakeup_chars)
523 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
526 extern inline void rc_check_modem(struct riscom_board const * bp)
528 struct riscom_port *port;
529 struct tty_struct *tty;
530 unsigned char mcr;
532 if (!(port = rc_get_port(bp, "Modem")))
533 return;
535 tty = port->tty;
537 mcr = rc_in(bp, CD180_MCR);
538 if (mcr & MCR_CDCHG) {
539 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
540 wake_up_interruptible(&port->open_wait);
541 else if (!((port->flags & ASYNC_CALLOUT_ACTIVE) &&
542 (port->flags & ASYNC_CALLOUT_NOHUP)))
543 queue_task(&port->tqueue_hangup, &tq_scheduler);
546 #ifdef RISCOM_BRAIN_DAMAGED_CTS
547 if (mcr & MCR_CTSCHG) {
548 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
549 tty->hw_stopped = 0;
550 port->IER |= IER_TXRDY;
551 if (port->xmit_cnt <= port->wakeup_chars)
552 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
553 } else {
554 tty->hw_stopped = 1;
555 port->IER &= ~IER_TXRDY;
557 rc_out(bp, CD180_IER, port->IER);
559 if (mcr & MCR_DSRCHG) {
560 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
561 tty->hw_stopped = 0;
562 port->IER |= IER_TXRDY;
563 if (port->xmit_cnt <= port->wakeup_chars)
564 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
565 } else {
566 tty->hw_stopped = 1;
567 port->IER &= ~IER_TXRDY;
569 rc_out(bp, CD180_IER, port->IER);
571 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
573 /* Clear change bits */
574 rc_out(bp, CD180_MCR, 0);
577 /* The main interrupt processing routine */
578 static void rc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
580 unsigned char status;
581 unsigned char ack;
582 struct riscom_board *bp;
583 unsigned long loop = 0;
585 bp = IRQ_to_board[irq];
587 if (!bp || !(bp->flags & RC_BOARD_ACTIVE)) {
588 return;
591 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
592 (RC_BSR_TOUT | RC_BSR_TINT |
593 RC_BSR_MINT | RC_BSR_RINT))) {
595 if (status & RC_BSR_TOUT)
596 printk("rc%d: Got timeout. Hardware error ?\n", board_No(bp));
598 else if (status & RC_BSR_RINT) {
599 ack = rc_in(bp, RC_ACK_RINT);
601 if (ack == (RC_ID | GIVR_IT_RCV))
602 rc_receive(bp);
603 else if (ack == (RC_ID | GIVR_IT_REXC))
604 rc_receive_exc(bp);
605 else
606 printk("rc%d: Bad receive ack 0x%02x.\n",
607 board_No(bp), ack);
609 } else if (status & RC_BSR_TINT) {
610 ack = rc_in(bp, RC_ACK_TINT);
612 if (ack == (RC_ID | GIVR_IT_TX))
613 rc_transmit(bp);
614 else
615 printk("rc%d: Bad transmit ack 0x%02x.\n",
616 board_No(bp), ack);
618 } else /* if (status & RC_BSR_MINT) */ {
619 ack = rc_in(bp, RC_ACK_MINT);
621 if (ack == (RC_ID | GIVR_IT_MODEM))
622 rc_check_modem(bp);
623 else
624 printk("rc%d: Bad modem ack 0x%02x.\n",
625 board_No(bp), ack);
629 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
630 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
635 * Routines for open & close processing.
638 /* Called with disabled interrupts */
639 extern inline int rc_setup_board(struct riscom_board * bp)
641 int error;
643 if (bp->flags & RC_BOARD_ACTIVE)
644 return 0;
646 error = request_irq(bp->irq, rc_interrupt, SA_INTERRUPT, "RISCom/8", NULL);
647 if (error)
648 return error;
650 rc_out(bp, RC_CTOUT, 0); /* Just in case */
651 bp->DTR = ~0;
652 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
654 IRQ_to_board[bp->irq] = bp;
655 bp->flags |= RC_BOARD_ACTIVE;
657 MOD_INC_USE_COUNT;
658 return 0;
661 /* Called with disabled interrupts */
662 extern inline void rc_shutdown_board(struct riscom_board *bp)
664 if (!(bp->flags & RC_BOARD_ACTIVE))
665 return;
667 bp->flags &= ~RC_BOARD_ACTIVE;
669 free_irq(bp->irq, NULL);
670 IRQ_to_board[bp->irq] = NULL;
672 bp->DTR = ~0;
673 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
675 MOD_DEC_USE_COUNT;
679 * Setting up port characteristics.
680 * Must be called with disabled interrupts
682 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
684 struct tty_struct *tty;
685 unsigned long baud;
686 long tmp;
687 unsigned char cor1 = 0, cor3 = 0;
688 unsigned char mcor1 = 0, mcor2 = 0;
690 if (!(tty = port->tty) || !tty->termios)
691 return;
693 port->IER = 0;
694 port->COR2 = 0;
695 port->MSVR = MSVR_RTS;
697 baud = C_BAUD(tty);
699 if (baud & CBAUDEX) {
700 baud &= ~CBAUDEX;
701 if (baud < 1 || baud > 2)
702 port->tty->termios->c_cflag &= ~CBAUDEX;
703 else
704 baud += 15;
706 if (baud == 15) {
707 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
708 baud ++;
709 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
710 baud += 2;
713 /* Select port on the board */
714 rc_out(bp, CD180_CAR, port_No(port));
716 if (!baud_table[baud]) {
717 /* Drop DTR & exit */
718 bp->DTR |= (1u << port_No(port));
719 rc_out(bp, RC_DTR, bp->DTR);
720 return;
721 } else {
722 /* Set DTR on */
723 bp->DTR &= ~(1u << port_No(port));
724 rc_out(bp, RC_DTR, bp->DTR);
728 * Now we must calculate some speed depended things
731 /* Set baud rate for port */
732 tmp = (((RC_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +
733 CD180_TPC/2) / CD180_TPC);
735 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
736 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
737 rc_out(bp, CD180_RBPRL, tmp & 0xff);
738 rc_out(bp, CD180_TBPRL, tmp & 0xff);
740 baud = (baud_table[baud] + 5) / 10; /* Estimated CPS */
742 /* Two timer ticks seems enough to wakeup something like SLIP driver */
743 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
744 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
745 SERIAL_XMIT_SIZE - 1 : tmp);
747 /* Receiver timeout will be transmission time for 1.5 chars */
748 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
749 tmp = (tmp > 0xff) ? 0xff : tmp;
750 rc_out(bp, CD180_RTPR, tmp);
752 switch (C_CSIZE(tty)) {
753 case CS5:
754 cor1 |= COR1_5BITS;
755 break;
756 case CS6:
757 cor1 |= COR1_6BITS;
758 break;
759 case CS7:
760 cor1 |= COR1_7BITS;
761 break;
762 case CS8:
763 cor1 |= COR1_8BITS;
764 break;
767 if (C_CSTOPB(tty))
768 cor1 |= COR1_2SB;
770 cor1 |= COR1_IGNORE;
771 if (C_PARENB(tty)) {
772 cor1 |= COR1_NORMPAR;
773 if (C_PARODD(tty))
774 cor1 |= COR1_ODDP;
775 if (I_INPCK(tty))
776 cor1 &= ~COR1_IGNORE;
778 /* Set marking of some errors */
779 port->mark_mask = RCSR_OE | RCSR_TOUT;
780 if (I_INPCK(tty))
781 port->mark_mask |= RCSR_FE | RCSR_PE;
782 if (I_BRKINT(tty) || I_PARMRK(tty))
783 port->mark_mask |= RCSR_BREAK;
784 if (I_IGNPAR(tty))
785 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
786 if (I_IGNBRK(tty)) {
787 port->mark_mask &= ~RCSR_BREAK;
788 if (I_IGNPAR(tty))
789 /* Real raw mode. Ignore all */
790 port->mark_mask &= ~RCSR_OE;
792 /* Enable Hardware Flow Control */
793 if (C_CRTSCTS(tty)) {
794 #ifdef RISCOM_BRAIN_DAMAGED_CTS
795 port->IER |= IER_DSR | IER_CTS;
796 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
797 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
798 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
799 #else
800 port->COR2 |= COR2_CTSAE;
801 #endif
803 /* Enable Software Flow Control. FIXME: I'm not sure about this */
804 /* Some people reported that it works, but I still doubt */
805 if (I_IXON(tty)) {
806 port->COR2 |= COR2_TXIBE;
807 cor3 |= (COR3_FCT | COR3_SCDE);
808 if (I_IXANY(tty))
809 port->COR2 |= COR2_IXM;
810 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
811 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
812 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
813 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
815 if (!C_CLOCAL(tty)) {
816 /* Enable CD check */
817 port->IER |= IER_CD;
818 mcor1 |= MCOR1_CDZD;
819 mcor2 |= MCOR2_CDOD;
822 if (C_CREAD(tty))
823 /* Enable receiver */
824 port->IER |= IER_RXD;
826 /* Set input FIFO size (1-8 bytes) */
827 cor3 |= RISCOM_RXFIFO;
828 /* Setting up CD180 channel registers */
829 rc_out(bp, CD180_COR1, cor1);
830 rc_out(bp, CD180_COR2, port->COR2);
831 rc_out(bp, CD180_COR3, cor3);
832 /* Make CD180 know about registers change */
833 rc_wait_CCR(bp);
834 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
835 /* Setting up modem option registers */
836 rc_out(bp, CD180_MCOR1, mcor1);
837 rc_out(bp, CD180_MCOR2, mcor2);
838 /* Enable CD180 transmitter & receiver */
839 rc_wait_CCR(bp);
840 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
841 /* Enable interrupts */
842 rc_out(bp, CD180_IER, port->IER);
843 /* And finally set RTS on */
844 rc_out(bp, CD180_MSVR, port->MSVR);
847 /* Must be called with interrupts enabled */
848 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
850 unsigned long flags;
852 if (port->flags & ASYNC_INITIALIZED)
853 return 0;
855 if (!port->xmit_buf) {
856 /* We may sleep in get_free_page() */
857 unsigned long tmp;
859 if (!(tmp = get_free_page(GFP_KERNEL)))
860 return -ENOMEM;
862 if (port->xmit_buf) {
863 free_page(tmp);
864 return -ERESTARTSYS;
866 port->xmit_buf = (unsigned char *) tmp;
869 save_flags(flags); cli();
871 if (port->tty)
872 clear_bit(TTY_IO_ERROR, &port->tty->flags);
874 if (port->count == 1)
875 bp->count++;
877 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
878 rc_change_speed(bp, port);
879 port->flags |= ASYNC_INITIALIZED;
881 restore_flags(flags);
882 return 0;
885 /* Must be called with interrupts disabled */
886 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
888 struct tty_struct *tty;
890 if (!(port->flags & ASYNC_INITIALIZED))
891 return;
893 #ifdef RC_REPORT_OVERRUN
894 printk("rc%d: port %d: Total %ld overruns were detected.\n",
895 board_No(bp), port_No(port), port->overrun);
896 #endif
897 #ifdef RC_REPORT_FIFO
899 int i;
901 printk("rc%d: port %d: FIFO hits [ ",
902 board_No(bp), port_No(port));
903 for (i = 0; i < 10; i++) {
904 printk("%ld ", port->hits[i]);
906 printk("].\n");
908 #endif
909 if (port->xmit_buf) {
910 free_page((unsigned long) port->xmit_buf);
911 port->xmit_buf = NULL;
914 if (!(tty = port->tty) || C_HUPCL(tty)) {
915 /* Drop DTR */
916 bp->DTR |= (1u << port_No(port));
917 rc_out(bp, RC_DTR, bp->DTR);
920 /* Select port */
921 rc_out(bp, CD180_CAR, port_No(port));
922 /* Reset port */
923 rc_wait_CCR(bp);
924 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
925 /* Disable all interrupts from this port */
926 port->IER = 0;
927 rc_out(bp, CD180_IER, port->IER);
929 if (tty)
930 set_bit(TTY_IO_ERROR, &tty->flags);
931 port->flags &= ~ASYNC_INITIALIZED;
933 if (--bp->count < 0) {
934 printk("rc%d: rc_shutdown_port: bad board count: %d\n",
935 board_No(bp), bp->count);
936 bp->count = 0;
940 * If this is the last opened port on the board
941 * shutdown whole board
943 if (!bp->count)
944 rc_shutdown_board(bp);
948 static int block_til_ready(struct tty_struct *tty, struct file * filp,
949 struct riscom_port *port)
951 DECLARE_WAITQUEUE(wait, current);
952 struct riscom_board *bp = port_Board(port);
953 int retval;
954 int do_clocal = 0;
955 int CD;
958 * If the device is in the middle of being closed, then block
959 * until it's done, and then try again.
961 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
962 interruptible_sleep_on(&port->close_wait);
963 if (port->flags & ASYNC_HUP_NOTIFY)
964 return -EAGAIN;
965 else
966 return -ERESTARTSYS;
970 * If this is a callout device, then just make sure the normal
971 * device isn't being used.
973 if (tty->driver.subtype == RISCOM_TYPE_CALLOUT) {
974 if (port->flags & ASYNC_NORMAL_ACTIVE)
975 return -EBUSY;
976 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
977 (port->flags & ASYNC_SESSION_LOCKOUT) &&
978 (port->session != current->session))
979 return -EBUSY;
980 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
981 (port->flags & ASYNC_PGRP_LOCKOUT) &&
982 (port->pgrp != current->pgrp))
983 return -EBUSY;
984 port->flags |= ASYNC_CALLOUT_ACTIVE;
985 return 0;
989 * If non-blocking mode is set, or the port is not enabled,
990 * then make the check up front and then exit.
992 if ((filp->f_flags & O_NONBLOCK) ||
993 (tty->flags & (1 << TTY_IO_ERROR))) {
994 if (port->flags & ASYNC_CALLOUT_ACTIVE)
995 return -EBUSY;
996 port->flags |= ASYNC_NORMAL_ACTIVE;
997 return 0;
1000 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
1001 if (port->normal_termios.c_cflag & CLOCAL)
1002 do_clocal = 1;
1003 } else {
1004 if (C_CLOCAL(tty))
1005 do_clocal = 1;
1009 * Block waiting for the carrier detect and the line to become
1010 * free (i.e., not in use by the callout). While we are in
1011 * this loop, info->count is dropped by one, so that
1012 * rs_close() knows when to free things. We restore it upon
1013 * exit, either normal or abnormal.
1015 retval = 0;
1016 add_wait_queue(&port->open_wait, &wait);
1017 cli();
1018 if (!tty_hung_up_p(filp))
1019 port->count--;
1020 sti();
1021 port->blocked_open++;
1022 while (1) {
1023 cli();
1024 rc_out(bp, CD180_CAR, port_No(port));
1025 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
1026 if (!(port->flags & ASYNC_CALLOUT_ACTIVE)) {
1027 rc_out(bp, CD180_MSVR, MSVR_RTS);
1028 bp->DTR &= ~(1u << port_No(port));
1029 rc_out(bp, RC_DTR, bp->DTR);
1031 sti();
1032 set_current_state(TASK_INTERRUPTIBLE);
1033 if (tty_hung_up_p(filp) ||
1034 !(port->flags & ASYNC_INITIALIZED)) {
1035 if (port->flags & ASYNC_HUP_NOTIFY)
1036 retval = -EAGAIN;
1037 else
1038 retval = -ERESTARTSYS;
1039 break;
1041 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
1042 !(port->flags & ASYNC_CLOSING) &&
1043 (do_clocal || CD))
1044 break;
1045 if (signal_pending(current)) {
1046 retval = -ERESTARTSYS;
1047 break;
1049 schedule();
1051 current->state = TASK_RUNNING;
1052 remove_wait_queue(&port->open_wait, &wait);
1053 if (!tty_hung_up_p(filp))
1054 port->count++;
1055 port->blocked_open--;
1056 if (retval)
1057 return retval;
1059 port->flags |= ASYNC_NORMAL_ACTIVE;
1060 return 0;
1063 static int rc_open(struct tty_struct * tty, struct file * filp)
1065 int board;
1066 int error;
1067 struct riscom_port * port;
1068 struct riscom_board * bp;
1069 unsigned long flags;
1071 board = RC_BOARD(MINOR(tty->device));
1072 if (board > RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
1073 return -ENODEV;
1075 bp = &rc_board[board];
1076 port = rc_port + board * RC_NPORT + RC_PORT(MINOR(tty->device));
1077 if (rc_paranoia_check(port, tty->device, "rc_open"))
1078 return -ENODEV;
1080 if ((error = rc_setup_board(bp)))
1081 return error;
1083 port->count++;
1084 tty->driver_data = port;
1085 port->tty = tty;
1087 if ((error = rc_setup_port(bp, port)))
1088 return error;
1090 if ((error = block_til_ready(tty, filp, port)))
1091 return error;
1093 if ((port->count == 1) && (port->flags & ASYNC_SPLIT_TERMIOS)) {
1094 if (tty->driver.subtype == RISCOM_TYPE_NORMAL)
1095 *tty->termios = port->normal_termios;
1096 else
1097 *tty->termios = port->callout_termios;
1098 save_flags(flags); cli();
1099 rc_change_speed(bp, port);
1100 restore_flags(flags);
1103 port->session = current->session;
1104 port->pgrp = current->pgrp;
1106 return 0;
1109 static void rc_close(struct tty_struct * tty, struct file * filp)
1111 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1112 struct riscom_board *bp;
1113 unsigned long flags;
1114 unsigned long timeout;
1116 if (!port || rc_paranoia_check(port, tty->device, "close"))
1117 return;
1119 save_flags(flags); cli();
1120 if (tty_hung_up_p(filp)) {
1121 restore_flags(flags);
1122 return;
1125 bp = port_Board(port);
1126 if ((tty->count == 1) && (port->count != 1)) {
1127 printk("rc%d: rc_close: bad port count;"
1128 " tty->count is 1, port count is %d\n",
1129 board_No(bp), port->count);
1130 port->count = 1;
1132 if (--port->count < 0) {
1133 printk("rc%d: rc_close: bad port count for tty%d: %d\n",
1134 board_No(bp), port_No(port), port->count);
1135 port->count = 0;
1137 if (port->count) {
1138 restore_flags(flags);
1139 return;
1141 port->flags |= ASYNC_CLOSING;
1143 * Save the termios structure, since this port may have
1144 * separate termios for callout and dialin.
1146 if (port->flags & ASYNC_NORMAL_ACTIVE)
1147 port->normal_termios = *tty->termios;
1148 if (port->flags & ASYNC_CALLOUT_ACTIVE)
1149 port->callout_termios = *tty->termios;
1151 * Now we wait for the transmit buffer to clear; and we notify
1152 * the line discipline to only process XON/XOFF characters.
1154 tty->closing = 1;
1155 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1156 tty_wait_until_sent(tty, port->closing_wait);
1158 * At this point we stop accepting input. To do this, we
1159 * disable the receive line status interrupts, and tell the
1160 * interrupt driver to stop checking the data ready bit in the
1161 * line status register.
1163 port->IER &= ~IER_RXD;
1164 if (port->flags & ASYNC_INITIALIZED) {
1165 port->IER &= ~IER_TXRDY;
1166 port->IER |= IER_TXEMPTY;
1167 rc_out(bp, CD180_CAR, port_No(port));
1168 rc_out(bp, CD180_IER, port->IER);
1170 * Before we drop DTR, make sure the UART transmitter
1171 * has completely drained; this is especially
1172 * important if there is a transmit FIFO!
1174 timeout = jiffies+HZ;
1175 while(port->IER & IER_TXEMPTY) {
1176 current->state = TASK_INTERRUPTIBLE;
1177 schedule_timeout(port->timeout);
1178 if (time_after(jiffies, timeout))
1179 break;
1182 rc_shutdown_port(bp, port);
1183 if (tty->driver.flush_buffer)
1184 tty->driver.flush_buffer(tty);
1185 if (tty->ldisc.flush_buffer)
1186 tty->ldisc.flush_buffer(tty);
1187 tty->closing = 0;
1188 port->event = 0;
1189 port->tty = 0;
1190 if (port->blocked_open) {
1191 if (port->close_delay) {
1192 current->state = TASK_INTERRUPTIBLE;
1193 schedule_timeout(port->close_delay);
1195 wake_up_interruptible(&port->open_wait);
1197 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1198 ASYNC_CLOSING);
1199 wake_up_interruptible(&port->close_wait);
1200 restore_flags(flags);
1203 static int rc_write(struct tty_struct * tty, int from_user,
1204 const unsigned char *buf, int count)
1206 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1207 struct riscom_board *bp;
1208 int c, total = 0;
1209 unsigned long flags;
1211 if (rc_paranoia_check(port, tty->device, "rc_write"))
1212 return 0;
1214 bp = port_Board(port);
1216 if (!tty || !port->xmit_buf || !tmp_buf)
1217 return 0;
1219 if (from_user)
1220 down(&tmp_buf_sem);
1222 save_flags(flags);
1223 while (1) {
1224 cli();
1225 c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1226 SERIAL_XMIT_SIZE - port->xmit_head));
1227 if (c <= 0)
1228 break;
1230 if (from_user) {
1231 copy_from_user(tmp_buf, buf, c);
1232 c = MIN(c, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1233 SERIAL_XMIT_SIZE - port->xmit_head));
1234 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1235 } else
1236 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1237 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1238 port->xmit_cnt += c;
1239 restore_flags(flags);
1240 buf += c;
1241 count -= c;
1242 total += c;
1244 if (from_user)
1245 up(&tmp_buf_sem);
1246 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1247 !(port->IER & IER_TXRDY)) {
1248 port->IER |= IER_TXRDY;
1249 rc_out(bp, CD180_CAR, port_No(port));
1250 rc_out(bp, CD180_IER, port->IER);
1252 restore_flags(flags);
1253 return total;
1256 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1258 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1259 unsigned long flags;
1261 if (rc_paranoia_check(port, tty->device, "rc_put_char"))
1262 return;
1264 if (!tty || !port->xmit_buf)
1265 return;
1267 save_flags(flags); cli();
1269 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1270 restore_flags(flags);
1271 return;
1274 port->xmit_buf[port->xmit_head++] = ch;
1275 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1276 port->xmit_cnt++;
1277 restore_flags(flags);
1280 static void rc_flush_chars(struct tty_struct * tty)
1282 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1283 unsigned long flags;
1285 if (rc_paranoia_check(port, tty->device, "rc_flush_chars"))
1286 return;
1288 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1289 !port->xmit_buf)
1290 return;
1292 save_flags(flags); cli();
1293 port->IER |= IER_TXRDY;
1294 rc_out(port_Board(port), CD180_CAR, port_No(port));
1295 rc_out(port_Board(port), CD180_IER, port->IER);
1296 restore_flags(flags);
1299 static int rc_write_room(struct tty_struct * tty)
1301 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1302 int ret;
1304 if (rc_paranoia_check(port, tty->device, "rc_write_room"))
1305 return 0;
1307 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1308 if (ret < 0)
1309 ret = 0;
1310 return ret;
1313 static int rc_chars_in_buffer(struct tty_struct *tty)
1315 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1317 if (rc_paranoia_check(port, tty->device, "rc_chars_in_buffer"))
1318 return 0;
1320 return port->xmit_cnt;
1323 static void rc_flush_buffer(struct tty_struct *tty)
1325 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1326 unsigned long flags;
1328 if (rc_paranoia_check(port, tty->device, "rc_flush_buffer"))
1329 return;
1331 save_flags(flags); cli();
1332 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1333 restore_flags(flags);
1335 wake_up_interruptible(&tty->write_wait);
1336 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1337 tty->ldisc.write_wakeup)
1338 (tty->ldisc.write_wakeup)(tty);
1341 static int rc_get_modem_info(struct riscom_port * port, unsigned int *value)
1343 struct riscom_board * bp;
1344 unsigned char status;
1345 unsigned int result;
1346 unsigned long flags;
1348 bp = port_Board(port);
1349 save_flags(flags); cli();
1350 rc_out(bp, CD180_CAR, port_No(port));
1351 status = rc_in(bp, CD180_MSVR);
1352 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1353 restore_flags(flags);
1354 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1355 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1356 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1357 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1358 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1359 put_user(result, value);
1360 return 0;
1363 static int rc_set_modem_info(struct riscom_port * port, unsigned int cmd,
1364 unsigned int *value)
1366 int error;
1367 unsigned int arg;
1368 unsigned long flags;
1369 struct riscom_board *bp = port_Board(port);
1371 error = get_user(arg, value);
1372 if (error)
1373 return error;
1374 switch (cmd) {
1375 case TIOCMBIS:
1376 if (arg & TIOCM_RTS)
1377 port->MSVR |= MSVR_RTS;
1378 if (arg & TIOCM_DTR)
1379 bp->DTR &= ~(1u << port_No(port));
1380 break;
1381 case TIOCMBIC:
1382 if (arg & TIOCM_RTS)
1383 port->MSVR &= ~MSVR_RTS;
1384 if (arg & TIOCM_DTR)
1385 bp->DTR |= (1u << port_No(port));
1386 break;
1387 case TIOCMSET:
1388 port->MSVR = (arg & TIOCM_RTS) ? (port->MSVR | MSVR_RTS) :
1389 (port->MSVR & ~MSVR_RTS);
1390 bp->DTR = arg & TIOCM_DTR ? (bp->DTR &= ~(1u << port_No(port))) :
1391 (bp->DTR |= (1u << port_No(port)));
1392 break;
1393 default:
1394 return -EINVAL;
1396 save_flags(flags); cli();
1397 rc_out(bp, CD180_CAR, port_No(port));
1398 rc_out(bp, CD180_MSVR, port->MSVR);
1399 rc_out(bp, RC_DTR, bp->DTR);
1400 restore_flags(flags);
1401 return 0;
1404 extern inline void rc_send_break(struct riscom_port * port, unsigned long length)
1406 struct riscom_board *bp = port_Board(port);
1407 unsigned long flags;
1409 save_flags(flags); cli();
1410 port->break_length = RISCOM_TPS / HZ * length;
1411 port->COR2 |= COR2_ETC;
1412 port->IER |= IER_TXRDY;
1413 rc_out(bp, CD180_CAR, port_No(port));
1414 rc_out(bp, CD180_COR2, port->COR2);
1415 rc_out(bp, CD180_IER, port->IER);
1416 rc_wait_CCR(bp);
1417 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1418 rc_wait_CCR(bp);
1419 restore_flags(flags);
1422 extern inline int rc_set_serial_info(struct riscom_port * port,
1423 struct serial_struct * newinfo)
1425 struct serial_struct tmp;
1426 struct riscom_board *bp = port_Board(port);
1427 int change_speed;
1428 unsigned long flags;
1429 int error;
1431 error = verify_area(VERIFY_READ, (void *) newinfo, sizeof(tmp));
1432 if (error)
1433 return error;
1434 copy_from_user(&tmp, newinfo, sizeof(tmp));
1436 #if 0
1437 if ((tmp.irq != bp->irq) ||
1438 (tmp.port != bp->base) ||
1439 (tmp.type != PORT_CIRRUS) ||
1440 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1441 (tmp.custom_divisor != 0) ||
1442 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1443 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1444 return -EINVAL;
1445 #endif
1447 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1448 (tmp.flags & ASYNC_SPD_MASK));
1450 if (!capable(CAP_SYS_ADMIN)) {
1451 if ((tmp.close_delay != port->close_delay) ||
1452 (tmp.closing_wait != port->closing_wait) ||
1453 ((tmp.flags & ~ASYNC_USR_MASK) !=
1454 (port->flags & ~ASYNC_USR_MASK)))
1455 return -EPERM;
1456 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1457 (tmp.flags & ASYNC_USR_MASK));
1458 } else {
1459 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1460 (tmp.flags & ASYNC_FLAGS));
1461 port->close_delay = tmp.close_delay;
1462 port->closing_wait = tmp.closing_wait;
1464 if (change_speed) {
1465 save_flags(flags); cli();
1466 rc_change_speed(bp, port);
1467 restore_flags(flags);
1469 return 0;
1472 extern inline int rc_get_serial_info(struct riscom_port * port,
1473 struct serial_struct * retinfo)
1475 struct serial_struct tmp;
1476 struct riscom_board *bp = port_Board(port);
1477 int error;
1479 error = verify_area(VERIFY_WRITE, (void *) retinfo, sizeof(tmp));
1480 if (error)
1481 return error;
1483 memset(&tmp, 0, sizeof(tmp));
1484 tmp.type = PORT_CIRRUS;
1485 tmp.line = port - rc_port;
1486 tmp.port = bp->base;
1487 tmp.irq = bp->irq;
1488 tmp.flags = port->flags;
1489 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1490 tmp.close_delay = port->close_delay * HZ/100;
1491 tmp.closing_wait = port->closing_wait * HZ/100;
1492 tmp.xmit_fifo_size = CD180_NFIFO;
1493 copy_to_user(retinfo, &tmp, sizeof(tmp));
1494 return 0;
1497 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1498 unsigned int cmd, unsigned long arg)
1501 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1502 int error;
1503 int retval;
1505 if (rc_paranoia_check(port, tty->device, "rc_ioctl"))
1506 return -ENODEV;
1508 switch (cmd) {
1509 case TCSBRK: /* SVID version: non-zero arg --> no break */
1510 retval = tty_check_change(tty);
1511 if (retval)
1512 return retval;
1513 tty_wait_until_sent(tty, 0);
1514 if (!arg)
1515 rc_send_break(port, HZ/4); /* 1/4 second */
1516 return 0;
1517 case TCSBRKP: /* support for POSIX tcsendbreak() */
1518 retval = tty_check_change(tty);
1519 if (retval)
1520 return retval;
1521 tty_wait_until_sent(tty, 0);
1522 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1523 return 0;
1524 case TIOCGSOFTCAR:
1525 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned int *) arg);
1526 case TIOCSSOFTCAR:
1527 retval = get_user(arg,(unsigned int *) arg);
1528 if (retval)
1529 return retval;
1530 tty->termios->c_cflag =
1531 ((tty->termios->c_cflag & ~CLOCAL) |
1532 (arg ? CLOCAL : 0));
1533 return 0;
1534 case TIOCMGET:
1535 error = verify_area(VERIFY_WRITE, (void *) arg,
1536 sizeof(unsigned int));
1537 if (error)
1538 return error;
1539 return rc_get_modem_info(port, (unsigned int *) arg);
1540 case TIOCMBIS:
1541 case TIOCMBIC:
1542 case TIOCMSET:
1543 return rc_set_modem_info(port, cmd, (unsigned int *) arg);
1544 case TIOCGSERIAL:
1545 return rc_get_serial_info(port, (struct serial_struct *) arg);
1546 case TIOCSSERIAL:
1547 return rc_set_serial_info(port, (struct serial_struct *) arg);
1548 default:
1549 return -ENOIOCTLCMD;
1551 return 0;
1554 static void rc_throttle(struct tty_struct * tty)
1556 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1557 struct riscom_board *bp;
1558 unsigned long flags;
1560 if (rc_paranoia_check(port, tty->device, "rc_throttle"))
1561 return;
1563 bp = port_Board(port);
1565 save_flags(flags); cli();
1566 port->MSVR &= ~MSVR_RTS;
1567 rc_out(bp, CD180_CAR, port_No(port));
1568 if (I_IXOFF(tty)) {
1569 rc_wait_CCR(bp);
1570 rc_out(bp, CD180_CCR, CCR_SSCH2);
1571 rc_wait_CCR(bp);
1573 rc_out(bp, CD180_MSVR, port->MSVR);
1574 restore_flags(flags);
1577 static void rc_unthrottle(struct tty_struct * tty)
1579 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1580 struct riscom_board *bp;
1581 unsigned long flags;
1583 if (rc_paranoia_check(port, tty->device, "rc_unthrottle"))
1584 return;
1586 bp = port_Board(port);
1588 save_flags(flags); cli();
1589 port->MSVR |= MSVR_RTS;
1590 rc_out(bp, CD180_CAR, port_No(port));
1591 if (I_IXOFF(tty)) {
1592 rc_wait_CCR(bp);
1593 rc_out(bp, CD180_CCR, CCR_SSCH1);
1594 rc_wait_CCR(bp);
1596 rc_out(bp, CD180_MSVR, port->MSVR);
1597 restore_flags(flags);
1600 static void rc_stop(struct tty_struct * tty)
1602 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1603 struct riscom_board *bp;
1604 unsigned long flags;
1606 if (rc_paranoia_check(port, tty->device, "rc_stop"))
1607 return;
1609 bp = port_Board(port);
1611 save_flags(flags); cli();
1612 port->IER &= ~IER_TXRDY;
1613 rc_out(bp, CD180_CAR, port_No(port));
1614 rc_out(bp, CD180_IER, port->IER);
1615 restore_flags(flags);
1618 static void rc_start(struct tty_struct * tty)
1620 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1621 struct riscom_board *bp;
1622 unsigned long flags;
1624 if (rc_paranoia_check(port, tty->device, "rc_start"))
1625 return;
1627 bp = port_Board(port);
1629 save_flags(flags); cli();
1630 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1631 port->IER |= IER_TXRDY;
1632 rc_out(bp, CD180_CAR, port_No(port));
1633 rc_out(bp, CD180_IER, port->IER);
1635 restore_flags(flags);
1639 * This routine is called from the scheduler tqueue when the interrupt
1640 * routine has signalled that a hangup has occurred. The path of
1641 * hangup processing is:
1643 * serial interrupt routine -> (scheduler tqueue) ->
1644 * do_rc_hangup() -> tty->hangup() -> rc_hangup()
1647 static void do_rc_hangup(void *private_)
1649 struct riscom_port *port = (struct riscom_port *) private_;
1650 struct tty_struct *tty;
1652 tty = port->tty;
1653 if (!tty)
1654 return;
1656 tty_hangup(tty);
1659 static void rc_hangup(struct tty_struct * tty)
1661 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1662 struct riscom_board *bp;
1664 if (rc_paranoia_check(port, tty->device, "rc_hangup"))
1665 return;
1667 bp = port_Board(port);
1669 rc_shutdown_port(bp, port);
1670 port->event = 0;
1671 port->count = 0;
1672 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1673 port->tty = 0;
1674 wake_up_interruptible(&port->open_wait);
1677 static void rc_set_termios(struct tty_struct * tty, struct termios * old_termios)
1679 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1680 unsigned long flags;
1682 if (rc_paranoia_check(port, tty->device, "rc_set_termios"))
1683 return;
1685 if (tty->termios->c_cflag == old_termios->c_cflag &&
1686 tty->termios->c_iflag == old_termios->c_iflag)
1687 return;
1689 save_flags(flags); cli();
1690 rc_change_speed(port_Board(port), port);
1691 restore_flags(flags);
1693 if ((old_termios->c_cflag & CRTSCTS) &&
1694 !(tty->termios->c_cflag & CRTSCTS)) {
1695 tty->hw_stopped = 0;
1696 rc_start(tty);
1700 static void do_riscom_bh(void)
1702 run_task_queue(&tq_riscom);
1705 static void do_softint(void *private_)
1707 struct riscom_port *port = (struct riscom_port *) private_;
1708 struct tty_struct *tty;
1710 if(!(tty = port->tty))
1711 return;
1713 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
1714 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1715 tty->ldisc.write_wakeup)
1716 (tty->ldisc.write_wakeup)(tty);
1717 wake_up_interruptible(&tty->write_wait);
1721 static inline int rc_init_drivers(void)
1723 int error;
1724 int i;
1727 if (!(tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL))) {
1728 printk("rc: Couldn't get free page.\n");
1729 return 1;
1731 init_bh(RISCOM8_BH, do_riscom_bh);
1732 memset(IRQ_to_board, 0, sizeof(IRQ_to_board));
1733 memset(&riscom_driver, 0, sizeof(riscom_driver));
1734 riscom_driver.magic = TTY_DRIVER_MAGIC;
1735 riscom_driver.name = "ttyL";
1736 riscom_driver.major = RISCOM8_NORMAL_MAJOR;
1737 riscom_driver.num = RC_NBOARD * RC_NPORT;
1738 riscom_driver.type = TTY_DRIVER_TYPE_SERIAL;
1739 riscom_driver.subtype = RISCOM_TYPE_NORMAL;
1740 riscom_driver.init_termios = tty_std_termios;
1741 riscom_driver.init_termios.c_cflag =
1742 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1743 riscom_driver.flags = TTY_DRIVER_REAL_RAW;
1744 riscom_driver.refcount = &riscom_refcount;
1745 riscom_driver.table = riscom_table;
1746 riscom_driver.termios = riscom_termios;
1747 riscom_driver.termios_locked = riscom_termios_locked;
1749 riscom_driver.open = rc_open;
1750 riscom_driver.close = rc_close;
1751 riscom_driver.write = rc_write;
1752 riscom_driver.put_char = rc_put_char;
1753 riscom_driver.flush_chars = rc_flush_chars;
1754 riscom_driver.write_room = rc_write_room;
1755 riscom_driver.chars_in_buffer = rc_chars_in_buffer;
1756 riscom_driver.flush_buffer = rc_flush_buffer;
1757 riscom_driver.ioctl = rc_ioctl;
1758 riscom_driver.throttle = rc_throttle;
1759 riscom_driver.unthrottle = rc_unthrottle;
1760 riscom_driver.set_termios = rc_set_termios;
1761 riscom_driver.stop = rc_stop;
1762 riscom_driver.start = rc_start;
1763 riscom_driver.hangup = rc_hangup;
1765 riscom_callout_driver = riscom_driver;
1766 riscom_callout_driver.name = "cul";
1767 riscom_callout_driver.major = RISCOM8_CALLOUT_MAJOR;
1768 riscom_callout_driver.subtype = RISCOM_TYPE_CALLOUT;
1770 if ((error = tty_register_driver(&riscom_driver))) {
1771 free_page((unsigned long)tmp_buf);
1772 printk("rc: Couldn't register RISCom/8 driver, error = %d\n",
1773 error);
1774 return 1;
1776 if ((error = tty_register_driver(&riscom_callout_driver))) {
1777 free_page((unsigned long)tmp_buf);
1778 tty_unregister_driver(&riscom_driver);
1779 printk("rc: Couldn't register RISCom/8 callout driver, error = %d\n",
1780 error);
1781 return 1;
1784 memset(rc_port, 0, sizeof(rc_port));
1785 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1786 rc_port[i].callout_termios = riscom_callout_driver.init_termios;
1787 rc_port[i].normal_termios = riscom_driver.init_termios;
1788 rc_port[i].magic = RISCOM8_MAGIC;
1789 rc_port[i].tqueue.routine = do_softint;
1790 rc_port[i].tqueue.data = &rc_port[i];
1791 rc_port[i].tqueue_hangup.routine = do_rc_hangup;
1792 rc_port[i].tqueue_hangup.data = &rc_port[i];
1793 rc_port[i].close_delay = 50 * HZ/100;
1794 rc_port[i].closing_wait = 3000 * HZ/100;
1795 init_waitqueue_head(&rc_port[i].open_wait);
1796 init_waitqueue_head(&rc_port[i].close_wait);
1799 return 0;
1802 static void rc_release_drivers(void)
1804 unsigned long flags;
1806 save_flags(flags);
1807 cli();
1808 remove_bh(RISCOM8_BH);
1809 free_page((unsigned long)tmp_buf);
1810 tty_unregister_driver(&riscom_driver);
1811 tty_unregister_driver(&riscom_callout_driver);
1812 restore_flags(flags);
1815 #ifndef MODULE
1817 * Called at boot time.
1819 * You can specify IO base for up to RC_NBOARD cards,
1820 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1821 * Note that there will be no probing at default
1822 * addresses in this case.
1825 void __init riscom8_setup(char *str, int * ints)
1827 int i;
1829 for (i = 0; i < RC_NBOARD; i++) {
1830 if (i < ints[0])
1831 rc_board[i].base = ints[i+1];
1832 else
1833 rc_board[i].base = 0;
1836 #endif
1839 * This routine must be called by kernel at boot time
1841 int __init riscom8_init(void)
1843 int i;
1844 int found = 0;
1846 printk("rc: SDL RISCom/8 card driver v1.0, (c) D.Gorodchanin 1994-1996.\n");
1848 if (rc_init_drivers())
1849 return -EIO;
1851 for (i = 0; i < RC_NBOARD; i++)
1852 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1853 found++;
1855 if (!found) {
1856 rc_release_drivers();
1857 printk("rc: No RISCom/8 boards detected.\n");
1858 return -EIO;
1860 return 0;
1863 #ifdef MODULE
1864 int iobase = 0;
1865 int iobase1 = 0;
1866 int iobase2 = 0;
1867 int iobase3 = 0;
1868 MODULE_PARM(iobase, "i");
1869 MODULE_PARM(iobase1, "i");
1870 MODULE_PARM(iobase2, "i");
1871 MODULE_PARM(iobase3, "i");
1874 * You can setup up to 4 boards (current value of RC_NBOARD)
1875 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1878 int init_module(void)
1880 int i;
1882 if (iobase || iobase1 || iobase2 || iobase3) {
1883 for(i = 0; i < RC_NBOARD; i++)
1884 rc_board[0].base = 0;
1887 if (iobase)
1888 rc_board[0].base = iobase;
1889 if (iobase1)
1890 rc_board[1].base = iobase1;
1891 if (iobase2)
1892 rc_board[2].base = iobase2;
1893 if (iobase3)
1894 rc_board[3].base = iobase3;
1896 return riscom8_init();
1899 void cleanup_module(void)
1901 int i;
1903 rc_release_drivers();
1904 for (i = 0; i < RC_NBOARD; i++)
1905 if (rc_board[i].flags & RC_BOARD_PRESENT)
1906 rc_release_io_range(&rc_board[i]);
1909 #endif /* MODULE */