Import 2.1.37pre7
[davej-history.git] / drivers / char / riscom8.c
blobdfe150e0723a3aa0d84853273daa593fa54bd390
1 /*
2 * linux/drivers/char/riscom.c -- RISCom/8 multiport serial driver.
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (begemot@bgm.rosprint.net)
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 DECLARE_TASK_QUEUE(tq_riscom);
80 #define RISCOM_TYPE_NORMAL 1
81 #define RISCOM_TYPE_CALLOUT 2
83 static struct riscom_board * IRQ_to_board[16] = { NULL, } ;
84 static struct tty_driver riscom_driver, riscom_callout_driver;
85 static int riscom_refcount = 0;
86 static struct tty_struct * riscom_table[RC_NBOARD * RC_NPORT] = { NULL, };
87 static struct termios * riscom_termios[RC_NBOARD * RC_NPORT] = { NULL, };
88 static struct termios * riscom_termios_locked[RC_NBOARD * RC_NPORT] = { NULL, };
89 static unsigned char * tmp_buf = NULL;
90 static struct semaphore tmp_buf_sem = MUTEX;
92 static unsigned long baud_table[] = {
93 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
94 9600, 19200, 38400, 57600, 76800, 0,
97 static struct riscom_board rc_board[RC_NBOARD] = {
98 { 0, RC_IOBASE1, 0, },
99 { 0, RC_IOBASE2, 0, },
100 { 0, RC_IOBASE3, 0, },
101 { 0, RC_IOBASE4, 0, },
104 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT] = {
105 { 0, },
108 /* RISCom/8 I/O ports addresses (without address translation) */
109 static unsigned short rc_ioport[] = {
110 #if 1
111 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
112 #else
113 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
114 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
115 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
116 #endif
118 #define RC_NIOPORT (sizeof(rc_ioport) / sizeof(rc_ioport[0]))
121 static inline int rc_paranoia_check(struct riscom_port const * port,
122 kdev_t device, const char *routine)
124 #ifdef RISCOM_PARANOIA_CHECK
125 static const char *badmagic =
126 "rc: Warning: bad riscom port magic number for device %s in %s\n";
127 static const char *badinfo =
128 "rc: Warning: null riscom port for device %s in %s\n";
130 if (!port) {
131 printk(badinfo, kdevname(device), routine);
132 return 1;
134 if (port->magic != RISCOM8_MAGIC) {
135 printk(badmagic, kdevname(device), routine);
136 return 1;
138 #endif
139 return 0;
144 * Service functions for RISCom/8 driver.
148 /* Get board number from pointer */
149 extern inline int board_No (struct riscom_board const * bp)
151 return bp - rc_board;
154 /* Get port number from pointer */
155 extern inline int port_No (struct riscom_port const * port)
157 return RC_PORT(port - rc_port);
160 /* Get pointer to board from pointer to port */
161 extern inline struct riscom_board * port_Board(struct riscom_port const * port)
163 return &rc_board[RC_BOARD(port - rc_port)];
166 /* Input Byte from CL CD180 register */
167 extern inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
169 return inb(bp->base + RC_TO_ISA(reg));
172 /* Output Byte to CL CD180 register */
173 extern inline void rc_out(struct riscom_board const * bp, unsigned short reg,
174 unsigned char val)
176 outb(val, bp->base + RC_TO_ISA(reg));
179 /* Wait for Channel Command Register ready */
180 extern inline void rc_wait_CCR(struct riscom_board const * bp)
182 unsigned long delay;
184 /* FIXME: need something more descriptive then 100000 :) */
185 for (delay = 100000; delay; delay--)
186 if (!rc_in(bp, CD180_CCR))
187 return;
189 printk("rc%d: Timeout waiting for CCR.\n", board_No(bp));
193 * RISCom/8 probe functions.
196 extern inline int rc_check_io_range(struct riscom_board * const bp)
198 int i;
200 for (i = 0; i < RC_NIOPORT; i++)
201 if (check_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1)) {
202 printk("rc%d: Skipping probe at 0x%03x. I/O address in use.\n",
203 board_No(bp), bp->base);
204 return 1;
206 return 0;
209 extern inline void rc_request_io_range(struct riscom_board * const bp)
211 int i;
213 for (i = 0; i < RC_NIOPORT; i++)
214 request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1, "RISCom/8" );
217 extern inline void rc_release_io_range(struct riscom_board * const bp)
219 int i;
221 for (i = 0; i < RC_NIOPORT; i++)
222 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
226 /* Must be called with enabled interrupts */
227 extern inline void rc_long_delay(unsigned long delay)
229 unsigned long i;
231 for (i = jiffies + delay; i > jiffies; ) ;
234 /* Reset and setup CD180 chip */
235 __initfunc(static void rc_init_CD180(struct riscom_board const * bp))
237 unsigned long flags;
239 save_flags(flags); cli();
240 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
241 rc_wait_CCR(bp); /* Wait for CCR ready */
242 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
243 sti();
244 rc_long_delay(HZ/20); /* Delay 0.05 sec */
245 cli();
246 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
247 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
248 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
249 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
250 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
252 /* Setting up prescaler. We need 4 ticks per 1 ms */
253 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
254 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
256 restore_flags(flags);
259 /* Main probing routine, also sets irq. */
260 __initfunc(static int rc_probe(struct riscom_board *bp))
262 unsigned char val1, val2;
263 int irqs = 0;
264 int retries;
266 bp->irq = 0;
268 if (rc_check_io_range(bp))
269 return 1;
271 /* Are the I/O ports here ? */
272 rc_out(bp, CD180_PPRL, 0x5a);
273 outb(0xff, 0x80);
274 val1 = rc_in(bp, CD180_PPRL);
275 rc_out(bp, CD180_PPRL, 0xa5);
276 outb(0x00, 0x80);
277 val2 = rc_in(bp, CD180_PPRL);
279 if ((val1 != 0x5a) || (val2 != 0xa5)) {
280 printk("rc%d: RISCom/8 Board at 0x%03x not found.\n",
281 board_No(bp), bp->base);
282 return 1;
285 /* It's time to find IRQ for this board */
286 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
287 irqs = probe_irq_on();
288 rc_init_CD180(bp); /* Reset CD180 chip */
289 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
290 rc_wait_CCR(bp);
291 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
292 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
293 rc_long_delay(HZ/20);
294 irqs = probe_irq_off(irqs);
295 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
296 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
297 rc_init_CD180(bp); /* Reset CD180 again */
299 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
300 printk("rc%d: RISCom/8 Board at 0x%03x not found.\n",
301 board_No(bp), bp->base);
302 return 1;
306 if (irqs <= 0) {
307 printk("rc%d: Can't find IRQ for RISCom/8 board at 0x%03x.\n",
308 board_No(bp), bp->base);
309 return 1;
311 rc_request_io_range(bp);
312 bp->irq = irqs;
313 bp->flags |= RC_BOARD_PRESENT;
315 printk("rc%d: RISCom/8 Rev. %c board detected at 0x%03x, IRQ %d.\n",
316 board_No(bp),
317 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
318 bp->base, bp->irq);
320 return 0;
325 * Interrupt processing routines.
329 extern inline void rc_mark_event(struct riscom_port * port, int event)
332 * I'm not quite happy with current scheme all serial
333 * drivers use their own BH routine.
334 * It seems this easily can be done with one BH routine
335 * serving for all serial drivers.
336 * For now I must introduce another one - RISCOM8_BH.
337 * Still hope this will be changed in near future.
339 set_bit(event, &port->event);
340 queue_task_irq_off(&port->tqueue, &tq_riscom);
341 mark_bh(RISCOM8_BH);
344 extern inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
345 unsigned char const * what)
347 unsigned char channel;
348 struct riscom_port * port;
350 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
351 if (channel < CD180_NCH) {
352 port = &rc_port[board_No(bp) * RC_NPORT + channel];
353 if (port->flags & ASYNC_INITIALIZED) {
354 return port;
357 printk("rc%d: %s interrupt from invalid port %d\n",
358 board_No(bp), what, channel);
359 return NULL;
362 extern inline void rc_receive_exc(struct riscom_board const * bp)
364 struct riscom_port *port;
365 struct tty_struct *tty;
366 unsigned char status;
367 unsigned char ch;
369 if (!(port = rc_get_port(bp, "Receive")))
370 return;
372 tty = port->tty;
373 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
374 printk("rc%d: port %d: Working around flip buffer overflow.\n",
375 board_No(bp), port_No(port));
376 return;
379 #ifdef RC_REPORT_OVERRUN
380 status = rc_in(bp, CD180_RCSR);
381 if (status & RCSR_OE) {
382 port->overrun++;
383 #if 0
384 printk("rc%d: port %d: Overrun. Total %ld overruns.\n",
385 board_No(bp), port_No(port), port->overrun);
386 #endif
388 status &= port->mark_mask;
389 #else
390 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
391 #endif
392 ch = rc_in(bp, CD180_RDR);
393 if (!status) {
394 return;
396 if (status & RCSR_TOUT) {
397 printk("rc%d: port %d: Receiver timeout. Hardware problems ?\n",
398 board_No(bp), port_No(port));
399 return;
401 } else if (status & RCSR_BREAK) {
402 printk("rc%d: port %d: Handling break...\n",
403 board_No(bp), port_No(port));
404 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
405 if (port->flags & ASYNC_SAK)
406 do_SAK(tty);
408 } else if (status & RCSR_PE)
409 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
411 else if (status & RCSR_FE)
412 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
414 else if (status & RCSR_OE)
415 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
417 else
418 *tty->flip.flag_buf_ptr++ = 0;
420 *tty->flip.char_buf_ptr++ = ch;
421 tty->flip.count++;
422 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
425 extern inline void rc_receive(struct riscom_board const * bp)
427 struct riscom_port *port;
428 struct tty_struct *tty;
429 unsigned char count;
431 if (!(port = rc_get_port(bp, "Receive")))
432 return;
434 tty = port->tty;
436 count = rc_in(bp, CD180_RDCR);
438 #ifdef RC_REPORT_FIFO
439 port->hits[count > 8 ? 9 : count]++;
440 #endif
442 while (count--) {
443 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
444 printk("rc%d: port %d: Working around flip buffer overflow.\n",
445 board_No(bp), port_No(port));
446 break;
448 *tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);
449 *tty->flip.flag_buf_ptr++ = 0;
450 tty->flip.count++;
452 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
455 extern inline void rc_transmit(struct riscom_board const * bp)
457 struct riscom_port *port;
458 struct tty_struct *tty;
459 unsigned char count;
462 if (!(port = rc_get_port(bp, "Transmit")))
463 return;
465 tty = port->tty;
467 if (port->IER & IER_TXEMPTY) {
468 /* FIFO drained */
469 rc_out(bp, CD180_CAR, port_No(port));
470 port->IER &= ~IER_TXEMPTY;
471 rc_out(bp, CD180_IER, port->IER);
472 return;
475 if ((port->xmit_cnt <= 0 && !port->break_length)
476 || tty->stopped || tty->hw_stopped) {
477 rc_out(bp, CD180_CAR, port_No(port));
478 port->IER &= ~IER_TXRDY;
479 rc_out(bp, CD180_IER, port->IER);
480 return;
483 if (port->break_length) {
484 if (port->break_length > 0) {
485 if (port->COR2 & COR2_ETC) {
486 rc_out(bp, CD180_TDR, CD180_C_ESC);
487 rc_out(bp, CD180_TDR, CD180_C_SBRK);
488 port->COR2 &= ~COR2_ETC;
490 count = MIN(port->break_length, 0xff);
491 rc_out(bp, CD180_TDR, CD180_C_ESC);
492 rc_out(bp, CD180_TDR, CD180_C_DELAY);
493 rc_out(bp, CD180_TDR, count);
494 if (!(port->break_length -= count))
495 port->break_length--;
496 } else {
497 rc_out(bp, CD180_TDR, CD180_C_ESC);
498 rc_out(bp, CD180_TDR, CD180_C_EBRK);
499 rc_out(bp, CD180_COR2, port->COR2);
500 rc_wait_CCR(bp);
501 rc_out(bp, CD180_CCR, CCR_CORCHG2);
502 port->break_length = 0;
504 return;
507 count = CD180_NFIFO;
508 do {
509 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
510 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
511 if (--port->xmit_cnt <= 0)
512 break;
513 } while (--count > 0);
515 if (port->xmit_cnt <= 0) {
516 rc_out(bp, CD180_CAR, port_No(port));
517 port->IER &= ~IER_TXRDY;
518 rc_out(bp, CD180_IER, port->IER);
520 if (port->xmit_cnt <= port->wakeup_chars)
521 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
524 extern inline void rc_check_modem(struct riscom_board const * bp)
526 struct riscom_port *port;
527 struct tty_struct *tty;
528 unsigned char mcr;
530 if (!(port = rc_get_port(bp, "Modem")))
531 return;
533 tty = port->tty;
535 mcr = rc_in(bp, CD180_MCR);
536 if (mcr & MCR_CDCHG) {
537 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
538 wake_up_interruptible(&port->open_wait);
539 else if (!((port->flags & ASYNC_CALLOUT_ACTIVE) &&
540 (port->flags & ASYNC_CALLOUT_NOHUP)))
541 queue_task_irq_off(&port->tqueue_hangup,
542 &tq_scheduler);
545 #ifdef RISCOM_BRAIN_DAMAGED_CTS
546 if (mcr & MCR_CTSCHG) {
547 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
548 tty->hw_stopped = 0;
549 port->IER |= IER_TXRDY;
550 if (port->xmit_cnt <= port->wakeup_chars)
551 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
552 } else {
553 tty->hw_stopped = 1;
554 port->IER &= ~IER_TXRDY;
556 rc_out(bp, CD180_IER, port->IER);
558 if (mcr & MCR_DSRCHG) {
559 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
560 tty->hw_stopped = 0;
561 port->IER |= IER_TXRDY;
562 if (port->xmit_cnt <= port->wakeup_chars)
563 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
564 } else {
565 tty->hw_stopped = 1;
566 port->IER &= ~IER_TXRDY;
568 rc_out(bp, CD180_IER, port->IER);
570 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
572 /* Clear change bits */
573 rc_out(bp, CD180_MCR, 0);
576 /* The main interrupt processing routine */
577 static void rc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
579 unsigned char status;
580 unsigned char ack;
581 struct riscom_board *bp;
582 unsigned long loop = 0;
584 bp = IRQ_to_board[irq];
586 if (!bp || !(bp->flags & RC_BOARD_ACTIVE)) {
587 return;
590 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
591 (RC_BSR_TOUT | RC_BSR_TINT |
592 RC_BSR_MINT | RC_BSR_RINT))) {
594 if (status & RC_BSR_TOUT)
595 printk("rc%d: Got timeout. Hardware error ?\n", board_No(bp));
597 else if (status & RC_BSR_RINT) {
598 ack = rc_in(bp, RC_ACK_RINT);
600 if (ack == (RC_ID | GIVR_IT_RCV))
601 rc_receive(bp);
602 else if (ack == (RC_ID | GIVR_IT_REXC))
603 rc_receive_exc(bp);
604 else
605 printk("rc%d: Bad receive ack 0x%02x.\n",
606 board_No(bp), ack);
608 } else if (status & RC_BSR_TINT) {
609 ack = rc_in(bp, RC_ACK_TINT);
611 if (ack == (RC_ID | GIVR_IT_TX))
612 rc_transmit(bp);
613 else
614 printk("rc%d: Bad transmit ack 0x%02x.\n",
615 board_No(bp), ack);
617 } else /* if (status & RC_BSR_MINT) */ {
618 ack = rc_in(bp, RC_ACK_MINT);
620 if (ack == (RC_ID | GIVR_IT_MODEM))
621 rc_check_modem(bp);
622 else
623 printk("rc%d: Bad modem ack 0x%02x.\n",
624 board_No(bp), ack);
628 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
629 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
634 * Routines for open & close processing.
637 /* Called with disabled interrupts */
638 extern inline int rc_setup_board(struct riscom_board * bp)
640 int error;
642 if (bp->flags & RC_BOARD_ACTIVE)
643 return 0;
645 error = request_irq(bp->irq, rc_interrupt, SA_INTERRUPT, "RISCom/8", NULL);
646 if (error)
647 return error;
649 rc_out(bp, RC_CTOUT, 0); /* Just in case */
650 bp->DTR = ~0;
651 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
653 IRQ_to_board[bp->irq] = bp;
654 bp->flags |= RC_BOARD_ACTIVE;
656 MOD_INC_USE_COUNT;
657 return 0;
660 /* Called with disabled interrupts */
661 extern inline void rc_shutdown_board(struct riscom_board *bp)
663 if (!(bp->flags & RC_BOARD_ACTIVE))
664 return;
666 bp->flags &= ~RC_BOARD_ACTIVE;
668 free_irq(bp->irq, NULL);
669 IRQ_to_board[bp->irq] = NULL;
671 bp->DTR = ~0;
672 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
674 MOD_DEC_USE_COUNT;
678 * Setting up port characteristics.
679 * Must be called with disabled interrupts
681 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
683 struct tty_struct *tty;
684 unsigned long baud;
685 long tmp;
686 unsigned char cor1 = 0, cor3 = 0;
687 unsigned char mcor1 = 0, mcor2 = 0;
689 if (!(tty = port->tty) || !tty->termios)
690 return;
692 port->IER = 0;
693 port->COR2 = 0;
694 port->MSVR = MSVR_RTS;
696 baud = C_BAUD(tty);
698 if (baud & CBAUDEX) {
699 baud &= ~CBAUDEX;
700 if (baud < 1 || baud > 2)
701 port->tty->termios->c_cflag &= ~CBAUDEX;
702 else
703 baud += 15;
705 if (baud == 15) {
706 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
707 baud ++;
708 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
709 baud += 2;
712 /* Select port on the board */
713 rc_out(bp, CD180_CAR, port_No(port));
715 if (!baud_table[baud]) {
716 /* Drop DTR & exit */
717 bp->DTR |= (1u << port_No(port));
718 rc_out(bp, RC_DTR, bp->DTR);
719 return;
720 } else {
721 /* Set DTR on */
722 bp->DTR &= ~(1u << port_No(port));
723 rc_out(bp, RC_DTR, bp->DTR);
727 * Now we must calculate some speed depended things
730 /* Set baud rate for port */
731 tmp = (((RC_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +
732 CD180_TPC/2) / CD180_TPC);
734 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
735 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
736 rc_out(bp, CD180_RBPRL, tmp & 0xff);
737 rc_out(bp, CD180_TBPRL, tmp & 0xff);
739 baud = (baud_table[baud] + 5) / 10; /* Estimated CPS */
741 /* Two timer ticks seems enough to wakeup something like SLIP driver */
742 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
743 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
744 SERIAL_XMIT_SIZE - 1 : tmp);
746 /* Receiver timeout will be transmission time for 1.5 chars */
747 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
748 tmp = (tmp > 0xff) ? 0xff : tmp;
749 rc_out(bp, CD180_RTPR, tmp);
751 switch (C_CSIZE(tty)) {
752 case CS5:
753 cor1 |= COR1_5BITS;
754 break;
755 case CS6:
756 cor1 |= COR1_6BITS;
757 break;
758 case CS7:
759 cor1 |= COR1_7BITS;
760 break;
761 case CS8:
762 cor1 |= COR1_8BITS;
763 break;
766 if (C_CSTOPB(tty))
767 cor1 |= COR1_2SB;
769 cor1 |= COR1_IGNORE;
770 if (C_PARENB(tty)) {
771 cor1 |= COR1_NORMPAR;
772 if (C_PARODD(tty))
773 cor1 |= COR1_ODDP;
774 if (I_INPCK(tty))
775 cor1 &= ~COR1_IGNORE;
777 /* Set marking of some errors */
778 port->mark_mask = RCSR_OE | RCSR_TOUT;
779 if (I_INPCK(tty))
780 port->mark_mask |= RCSR_FE | RCSR_PE;
781 if (I_BRKINT(tty) || I_PARMRK(tty))
782 port->mark_mask |= RCSR_BREAK;
783 if (I_IGNPAR(tty))
784 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
785 if (I_IGNBRK(tty)) {
786 port->mark_mask &= ~RCSR_BREAK;
787 if (I_IGNPAR(tty))
788 /* Real raw mode. Ignore all */
789 port->mark_mask &= ~RCSR_OE;
791 /* Enable Hardware Flow Control */
792 if (C_CRTSCTS(tty)) {
793 #ifdef RISCOM_BRAIN_DAMAGED_CTS
794 port->IER |= IER_DSR | IER_CTS;
795 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
796 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
797 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
798 #else
799 port->COR2 |= COR2_CTSAE;
800 #endif
802 /* Enable Software Flow Control. FIXME: I'm not sure about this */
803 /* Some people reported that it works, but I still doubt */
804 if (I_IXON(tty)) {
805 port->COR2 |= COR2_TXIBE;
806 cor3 |= (COR3_FCT | COR3_SCDE);
807 if (I_IXANY(tty))
808 port->COR2 |= COR2_IXM;
809 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
810 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
811 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
812 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
814 if (!C_CLOCAL(tty)) {
815 /* Enable CD check */
816 port->IER |= IER_CD;
817 mcor1 |= MCOR1_CDZD;
818 mcor2 |= MCOR2_CDOD;
821 if (C_CREAD(tty))
822 /* Enable receiver */
823 port->IER |= IER_RXD;
825 /* Set input FIFO size (1-8 bytes) */
826 cor3 |= RISCOM_RXFIFO;
827 /* Setting up CD180 channel registers */
828 rc_out(bp, CD180_COR1, cor1);
829 rc_out(bp, CD180_COR2, port->COR2);
830 rc_out(bp, CD180_COR3, cor3);
831 /* Make CD180 know about registers change */
832 rc_wait_CCR(bp);
833 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
834 /* Setting up modem option registers */
835 rc_out(bp, CD180_MCOR1, mcor1);
836 rc_out(bp, CD180_MCOR2, mcor2);
837 /* Enable CD180 transmitter & receiver */
838 rc_wait_CCR(bp);
839 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
840 /* Enable interrupts */
841 rc_out(bp, CD180_IER, port->IER);
842 /* And finally set RTS on */
843 rc_out(bp, CD180_MSVR, port->MSVR);
846 /* Must be called with interrupts enabled */
847 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
849 unsigned long flags;
851 if (port->flags & ASYNC_INITIALIZED)
852 return 0;
854 if (!port->xmit_buf) {
855 /* We may sleep in get_free_page() */
856 unsigned long tmp;
858 if (!(tmp = get_free_page(GFP_KERNEL)))
859 return -ENOMEM;
861 if (port->xmit_buf) {
862 free_page(tmp);
863 return -ERESTARTSYS;
865 port->xmit_buf = (unsigned char *) tmp;
868 save_flags(flags); cli();
870 if (port->tty)
871 clear_bit(TTY_IO_ERROR, &port->tty->flags);
873 if (port->count == 1)
874 bp->count++;
876 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
877 rc_change_speed(bp, port);
878 port->flags |= ASYNC_INITIALIZED;
880 restore_flags(flags);
881 return 0;
884 /* Must be called with interrupts disabled */
885 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
887 struct tty_struct *tty;
889 if (!(port->flags & ASYNC_INITIALIZED))
890 return;
892 #ifdef RC_REPORT_OVERRUN
893 printk("rc%d: port %d: Total %ld overruns were detected.\n",
894 board_No(bp), port_No(port), port->overrun);
895 #endif
896 #ifdef RC_REPORT_FIFO
898 int i;
900 printk("rc%d: port %d: FIFO hits [ ",
901 board_No(bp), port_No(port));
902 for (i = 0; i < 10; i++) {
903 printk("%ld ", port->hits[i]);
905 printk("].\n");
907 #endif
908 if (port->xmit_buf) {
909 free_page((unsigned long) port->xmit_buf);
910 port->xmit_buf = NULL;
913 if (!(tty = port->tty) || C_HUPCL(tty)) {
914 /* Drop DTR */
915 bp->DTR |= (1u << port_No(port));
916 rc_out(bp, RC_DTR, bp->DTR);
919 /* Select port */
920 rc_out(bp, CD180_CAR, port_No(port));
921 /* Reset port */
922 rc_wait_CCR(bp);
923 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
924 /* Disable all interrupts from this port */
925 port->IER = 0;
926 rc_out(bp, CD180_IER, port->IER);
928 if (tty)
929 set_bit(TTY_IO_ERROR, &tty->flags);
930 port->flags &= ~ASYNC_INITIALIZED;
932 if (--bp->count < 0) {
933 printk("rc%d: rc_shutdown_port: bad board count: %d\n",
934 board_No(bp), bp->count);
935 bp->count = 0;
939 * If this is the last opened port on the board
940 * shutdown whole board
942 if (!bp->count)
943 rc_shutdown_board(bp);
947 static int block_til_ready(struct tty_struct *tty, struct file * filp,
948 struct riscom_port *port)
950 struct wait_queue wait = { current, NULL };
951 struct riscom_board *bp = port_Board(port);
952 int retval;
953 int do_clocal = 0;
954 int CD;
957 * If the device is in the middle of being closed, then block
958 * until it's done, and then try again.
960 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
961 interruptible_sleep_on(&port->close_wait);
962 if (port->flags & ASYNC_HUP_NOTIFY)
963 return -EAGAIN;
964 else
965 return -ERESTARTSYS;
969 * If this is a callout device, then just make sure the normal
970 * device isn't being used.
972 if (tty->driver.subtype == RISCOM_TYPE_CALLOUT) {
973 if (port->flags & ASYNC_NORMAL_ACTIVE)
974 return -EBUSY;
975 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
976 (port->flags & ASYNC_SESSION_LOCKOUT) &&
977 (port->session != current->session))
978 return -EBUSY;
979 if ((port->flags & ASYNC_CALLOUT_ACTIVE) &&
980 (port->flags & ASYNC_PGRP_LOCKOUT) &&
981 (port->pgrp != current->pgrp))
982 return -EBUSY;
983 port->flags |= ASYNC_CALLOUT_ACTIVE;
984 return 0;
988 * If non-blocking mode is set, or the port is not enabled,
989 * then make the check up front and then exit.
991 if ((filp->f_flags & O_NONBLOCK) ||
992 (tty->flags & (1 << TTY_IO_ERROR))) {
993 if (port->flags & ASYNC_CALLOUT_ACTIVE)
994 return -EBUSY;
995 port->flags |= ASYNC_NORMAL_ACTIVE;
996 return 0;
999 if (port->flags & ASYNC_CALLOUT_ACTIVE) {
1000 if (port->normal_termios.c_cflag & CLOCAL)
1001 do_clocal = 1;
1002 } else {
1003 if (C_CLOCAL(tty))
1004 do_clocal = 1;
1008 * Block waiting for the carrier detect and the line to become
1009 * free (i.e., not in use by the callout). While we are in
1010 * this loop, info->count is dropped by one, so that
1011 * rs_close() knows when to free things. We restore it upon
1012 * exit, either normal or abnormal.
1014 retval = 0;
1015 add_wait_queue(&port->open_wait, &wait);
1016 cli();
1017 if (!tty_hung_up_p(filp))
1018 port->count--;
1019 sti();
1020 port->blocked_open++;
1021 while (1) {
1022 cli();
1023 rc_out(bp, CD180_CAR, port_No(port));
1024 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
1025 if (!(port->flags & ASYNC_CALLOUT_ACTIVE)) {
1026 rc_out(bp, CD180_MSVR, MSVR_RTS);
1027 bp->DTR &= ~(1u << port_No(port));
1028 rc_out(bp, RC_DTR, bp->DTR);
1030 sti();
1031 current->state = TASK_INTERRUPTIBLE;
1032 if (tty_hung_up_p(filp) ||
1033 !(port->flags & ASYNC_INITIALIZED)) {
1034 if (port->flags & ASYNC_HUP_NOTIFY)
1035 retval = -EAGAIN;
1036 else
1037 retval = -ERESTARTSYS;
1038 break;
1040 if (!(port->flags & ASYNC_CALLOUT_ACTIVE) &&
1041 !(port->flags & ASYNC_CLOSING) &&
1042 (do_clocal || CD))
1043 break;
1044 if (current->signal & ~current->blocked) {
1045 retval = -ERESTARTSYS;
1046 break;
1048 schedule();
1050 current->state = TASK_RUNNING;
1051 remove_wait_queue(&port->open_wait, &wait);
1052 if (!tty_hung_up_p(filp))
1053 port->count++;
1054 port->blocked_open--;
1055 if (retval)
1056 return retval;
1058 port->flags |= ASYNC_NORMAL_ACTIVE;
1059 return 0;
1062 static int rc_open(struct tty_struct * tty, struct file * filp)
1064 int board;
1065 int error;
1066 struct riscom_port * port;
1067 struct riscom_board * bp;
1068 unsigned long flags;
1070 board = RC_BOARD(MINOR(tty->device));
1071 if (board > RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
1072 return -ENODEV;
1074 bp = &rc_board[board];
1075 port = rc_port + board * RC_NPORT + RC_PORT(MINOR(tty->device));
1076 if (rc_paranoia_check(port, tty->device, "rc_open"))
1077 return -ENODEV;
1079 if ((error = rc_setup_board(bp)))
1080 return error;
1082 port->count++;
1083 tty->driver_data = port;
1084 port->tty = tty;
1086 if ((error = rc_setup_port(bp, port)))
1087 return error;
1089 if ((error = block_til_ready(tty, filp, port)))
1090 return error;
1092 if ((port->count == 1) && (port->flags & ASYNC_SPLIT_TERMIOS)) {
1093 if (tty->driver.subtype == RISCOM_TYPE_NORMAL)
1094 *tty->termios = port->normal_termios;
1095 else
1096 *tty->termios = port->callout_termios;
1097 save_flags(flags); cli();
1098 rc_change_speed(bp, port);
1099 restore_flags(flags);
1102 port->session = current->session;
1103 port->pgrp = current->pgrp;
1105 return 0;
1108 static void rc_close(struct tty_struct * tty, struct file * filp)
1110 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1111 struct riscom_board *bp;
1112 unsigned long flags;
1113 unsigned long timeout;
1115 if (!port || rc_paranoia_check(port, tty->device, "close"))
1116 return;
1118 save_flags(flags); cli();
1119 if (tty_hung_up_p(filp)) {
1120 restore_flags(flags);
1121 return;
1124 bp = port_Board(port);
1125 if ((tty->count == 1) && (port->count != 1)) {
1126 printk("rc%d: rc_close: bad port count;"
1127 " tty->count is 1, port count is %d\n",
1128 board_No(bp), port->count);
1129 port->count = 1;
1131 if (--port->count < 0) {
1132 printk("rc%d: rc_close: bad port count for tty%d: %d\n",
1133 board_No(bp), port_No(port), port->count);
1134 port->count = 0;
1136 if (port->count) {
1137 restore_flags(flags);
1138 return;
1140 port->flags |= ASYNC_CLOSING;
1142 * Save the termios structure, since this port may have
1143 * separate termios for callout and dialin.
1145 if (port->flags & ASYNC_NORMAL_ACTIVE)
1146 port->normal_termios = *tty->termios;
1147 if (port->flags & ASYNC_CALLOUT_ACTIVE)
1148 port->callout_termios = *tty->termios;
1150 * Now we wait for the transmit buffer to clear; and we notify
1151 * the line discipline to only process XON/XOFF characters.
1153 tty->closing = 1;
1154 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1155 tty_wait_until_sent(tty, port->closing_wait);
1157 * At this point we stop accepting input. To do this, we
1158 * disable the receive line status interrupts, and tell the
1159 * interrupt driver to stop checking the data ready bit in the
1160 * line status register.
1162 port->IER &= ~IER_RXD;
1163 if (port->flags & ASYNC_INITIALIZED) {
1164 port->IER &= ~IER_TXRDY;
1165 port->IER |= IER_TXEMPTY;
1166 rc_out(bp, CD180_CAR, port_No(port));
1167 rc_out(bp, CD180_IER, port->IER);
1169 * Before we drop DTR, make sure the UART transmitter
1170 * has completely drained; this is especially
1171 * important if there is a transmit FIFO!
1173 timeout = jiffies+HZ;
1174 while(port->IER & IER_TXEMPTY) {
1175 current->state = TASK_INTERRUPTIBLE;
1176 current->timeout = jiffies + port->timeout;
1177 schedule();
1178 if (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 current->timeout = jiffies + port->close_delay;
1194 schedule();
1196 wake_up_interruptible(&port->open_wait);
1198 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1199 ASYNC_CLOSING);
1200 wake_up_interruptible(&port->close_wait);
1201 restore_flags(flags);
1204 static int rc_write(struct tty_struct * tty, int from_user,
1205 const unsigned char *buf, int count)
1207 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1208 struct riscom_board *bp;
1209 int c, total = 0;
1210 unsigned long flags;
1212 if (rc_paranoia_check(port, tty->device, "rc_write"))
1213 return 0;
1215 bp = port_Board(port);
1217 if (!tty || !port->xmit_buf || !tmp_buf)
1218 return 0;
1220 if (from_user)
1221 down(&tmp_buf_sem);
1223 save_flags(flags);
1224 while (1) {
1225 cli();
1226 c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1227 SERIAL_XMIT_SIZE - port->xmit_head));
1228 if (c <= 0)
1229 break;
1231 if (from_user) {
1232 copy_from_user(tmp_buf, buf, c);
1233 c = MIN(c, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1234 SERIAL_XMIT_SIZE - port->xmit_head));
1235 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1236 } else
1237 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1238 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1239 port->xmit_cnt += c;
1240 restore_flags(flags);
1241 buf += c;
1242 count -= c;
1243 total += c;
1245 if (from_user)
1246 up(&tmp_buf_sem);
1247 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1248 !(port->IER & IER_TXRDY)) {
1249 port->IER |= IER_TXRDY;
1250 rc_out(bp, CD180_CAR, port_No(port));
1251 rc_out(bp, CD180_IER, port->IER);
1253 restore_flags(flags);
1254 return total;
1257 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1259 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1260 unsigned long flags;
1262 if (rc_paranoia_check(port, tty->device, "rc_put_char"))
1263 return;
1265 if (!tty || !port->xmit_buf)
1266 return;
1268 save_flags(flags); cli();
1270 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1271 restore_flags(flags);
1272 return;
1275 port->xmit_buf[port->xmit_head++] = ch;
1276 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1277 port->xmit_cnt++;
1278 restore_flags(flags);
1281 static void rc_flush_chars(struct tty_struct * tty)
1283 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1284 unsigned long flags;
1286 if (rc_paranoia_check(port, tty->device, "rc_flush_chars"))
1287 return;
1289 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1290 !port->xmit_buf)
1291 return;
1293 save_flags(flags); cli();
1294 port->IER |= IER_TXRDY;
1295 rc_out(port_Board(port), CD180_CAR, port_No(port));
1296 rc_out(port_Board(port), CD180_IER, port->IER);
1297 restore_flags(flags);
1300 static int rc_write_room(struct tty_struct * tty)
1302 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1303 int ret;
1305 if (rc_paranoia_check(port, tty->device, "rc_write_room"))
1306 return 0;
1308 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1309 if (ret < 0)
1310 ret = 0;
1311 return ret;
1314 static int rc_chars_in_buffer(struct tty_struct *tty)
1316 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1318 if (rc_paranoia_check(port, tty->device, "rc_chars_in_buffer"))
1319 return 0;
1321 return port->xmit_cnt;
1324 static void rc_flush_buffer(struct tty_struct *tty)
1326 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1327 unsigned long flags;
1329 if (rc_paranoia_check(port, tty->device, "rc_flush_buffer"))
1330 return;
1332 save_flags(flags); cli();
1333 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1334 restore_flags(flags);
1336 wake_up_interruptible(&tty->write_wait);
1337 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1338 tty->ldisc.write_wakeup)
1339 (tty->ldisc.write_wakeup)(tty);
1342 static int rc_get_modem_info(struct riscom_port * port, unsigned int *value)
1344 struct riscom_board * bp;
1345 unsigned char status;
1346 unsigned int result;
1347 unsigned long flags;
1349 bp = port_Board(port);
1350 save_flags(flags); cli();
1351 rc_out(bp, CD180_CAR, port_No(port));
1352 status = rc_in(bp, CD180_MSVR);
1353 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1354 restore_flags(flags);
1355 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1356 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1357 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1358 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1359 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1360 put_user(result, value);
1361 return 0;
1364 static int rc_set_modem_info(struct riscom_port * port, unsigned int cmd,
1365 unsigned int *value)
1367 int error;
1368 unsigned int arg;
1369 unsigned long flags;
1370 struct riscom_board *bp = port_Board(port);
1372 error = get_user(arg, value);
1373 if (error)
1374 return error;
1375 switch (cmd) {
1376 case TIOCMBIS:
1377 if (arg & TIOCM_RTS)
1378 port->MSVR |= MSVR_RTS;
1379 if (arg & TIOCM_DTR)
1380 bp->DTR &= ~(1u << port_No(port));
1381 break;
1382 case TIOCMBIC:
1383 if (arg & TIOCM_RTS)
1384 port->MSVR &= ~MSVR_RTS;
1385 if (arg & TIOCM_DTR)
1386 bp->DTR |= (1u << port_No(port));
1387 break;
1388 case TIOCMSET:
1389 port->MSVR = (arg & TIOCM_RTS) ? (port->MSVR | MSVR_RTS) :
1390 (port->MSVR & ~MSVR_RTS);
1391 bp->DTR = arg & TIOCM_DTR ? (bp->DTR &= ~(1u << port_No(port))) :
1392 (bp->DTR |= (1u << port_No(port)));
1393 break;
1394 default:
1395 return -EINVAL;
1397 save_flags(flags); cli();
1398 rc_out(bp, CD180_CAR, port_No(port));
1399 rc_out(bp, CD180_MSVR, port->MSVR);
1400 rc_out(bp, RC_DTR, bp->DTR);
1401 restore_flags(flags);
1402 return 0;
1405 extern inline void rc_send_break(struct riscom_port * port, unsigned long length)
1407 struct riscom_board *bp = port_Board(port);
1408 unsigned long flags;
1410 save_flags(flags); cli();
1411 port->break_length = RISCOM_TPS / HZ * length;
1412 port->COR2 |= COR2_ETC;
1413 port->IER |= IER_TXRDY;
1414 rc_out(bp, CD180_CAR, port_No(port));
1415 rc_out(bp, CD180_COR2, port->COR2);
1416 rc_out(bp, CD180_IER, port->IER);
1417 rc_wait_CCR(bp);
1418 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1419 rc_wait_CCR(bp);
1420 restore_flags(flags);
1423 extern inline int rc_set_serial_info(struct riscom_port * port,
1424 struct serial_struct * newinfo)
1426 struct serial_struct tmp;
1427 struct riscom_board *bp = port_Board(port);
1428 int change_speed;
1429 unsigned long flags;
1430 int error;
1432 error = verify_area(VERIFY_READ, (void *) newinfo, sizeof(tmp));
1433 if (error)
1434 return error;
1435 copy_from_user(&tmp, newinfo, sizeof(tmp));
1437 #if 0
1438 if ((tmp.irq != bp->irq) ||
1439 (tmp.port != bp->base) ||
1440 (tmp.type != PORT_CIRRUS) ||
1441 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1442 (tmp.custom_divisor != 0) ||
1443 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1444 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1445 return -EINVAL;
1446 #endif
1448 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1449 (tmp.flags & ASYNC_SPD_MASK));
1451 if (!suser()) {
1452 if ((tmp.close_delay != port->close_delay) ||
1453 (tmp.closing_wait != port->closing_wait) ||
1454 ((tmp.flags & ~ASYNC_USR_MASK) !=
1455 (port->flags & ~ASYNC_USR_MASK)))
1456 return -EPERM;
1457 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1458 (tmp.flags & ASYNC_USR_MASK));
1459 } else {
1460 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1461 (tmp.flags & ASYNC_FLAGS));
1462 port->close_delay = tmp.close_delay;
1463 port->closing_wait = tmp.closing_wait;
1465 if (change_speed) {
1466 save_flags(flags); cli();
1467 rc_change_speed(bp, port);
1468 restore_flags(flags);
1470 return 0;
1473 extern inline int rc_get_serial_info(struct riscom_port * port,
1474 struct serial_struct * retinfo)
1476 struct serial_struct tmp;
1477 struct riscom_board *bp = port_Board(port);
1478 int error;
1480 error = verify_area(VERIFY_WRITE, (void *) retinfo, sizeof(tmp));
1481 if (error)
1482 return error;
1484 memset(&tmp, 0, sizeof(tmp));
1485 tmp.type = PORT_CIRRUS;
1486 tmp.line = port - rc_port;
1487 tmp.port = bp->base;
1488 tmp.irq = bp->irq;
1489 tmp.flags = port->flags;
1490 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1491 tmp.close_delay = port->close_delay * HZ/100;
1492 tmp.closing_wait = port->closing_wait * HZ/100;
1493 tmp.xmit_fifo_size = CD180_NFIFO;
1494 copy_to_user(retinfo, &tmp, sizeof(tmp));
1495 return 0;
1498 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1499 unsigned int cmd, unsigned long arg)
1502 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1503 int error;
1504 int retval;
1506 if (rc_paranoia_check(port, tty->device, "rc_ioctl"))
1507 return -ENODEV;
1509 switch (cmd) {
1510 case TCSBRK: /* SVID version: non-zero arg --> no break */
1511 retval = tty_check_change(tty);
1512 if (retval)
1513 return retval;
1514 tty_wait_until_sent(tty, 0);
1515 if (!arg)
1516 rc_send_break(port, HZ/4); /* 1/4 second */
1517 return 0;
1518 case TCSBRKP: /* support for POSIX tcsendbreak() */
1519 retval = tty_check_change(tty);
1520 if (retval)
1521 return retval;
1522 tty_wait_until_sent(tty, 0);
1523 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1524 return 0;
1525 case TIOCGSOFTCAR:
1526 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned int *) arg);
1527 case TIOCSSOFTCAR:
1528 retval = get_user(arg,(unsigned int *) arg);
1529 if (retval)
1530 return retval;
1531 tty->termios->c_cflag =
1532 ((tty->termios->c_cflag & ~CLOCAL) |
1533 (arg ? CLOCAL : 0));
1534 return 0;
1535 case TIOCMGET:
1536 error = verify_area(VERIFY_WRITE, (void *) arg,
1537 sizeof(unsigned int));
1538 if (error)
1539 return error;
1540 return rc_get_modem_info(port, (unsigned int *) arg);
1541 case TIOCMBIS:
1542 case TIOCMBIC:
1543 case TIOCMSET:
1544 return rc_set_modem_info(port, cmd, (unsigned int *) arg);
1545 case TIOCGSERIAL:
1546 return rc_get_serial_info(port, (struct serial_struct *) arg);
1547 case TIOCSSERIAL:
1548 return rc_set_serial_info(port, (struct serial_struct *) arg);
1549 default:
1550 return -ENOIOCTLCMD;
1552 return 0;
1555 static void rc_throttle(struct tty_struct * tty)
1557 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1558 struct riscom_board *bp;
1559 unsigned long flags;
1561 if (rc_paranoia_check(port, tty->device, "rc_throttle"))
1562 return;
1564 bp = port_Board(port);
1566 save_flags(flags); cli();
1567 port->MSVR &= ~MSVR_RTS;
1568 rc_out(bp, CD180_CAR, port_No(port));
1569 if (I_IXOFF(tty)) {
1570 rc_wait_CCR(bp);
1571 rc_out(bp, CD180_CCR, CCR_SSCH2);
1572 rc_wait_CCR(bp);
1574 rc_out(bp, CD180_MSVR, port->MSVR);
1575 restore_flags(flags);
1578 static void rc_unthrottle(struct tty_struct * tty)
1580 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1581 struct riscom_board *bp;
1582 unsigned long flags;
1584 if (rc_paranoia_check(port, tty->device, "rc_unthrottle"))
1585 return;
1587 bp = port_Board(port);
1589 save_flags(flags); cli();
1590 port->MSVR |= MSVR_RTS;
1591 rc_out(bp, CD180_CAR, port_No(port));
1592 if (I_IXOFF(tty)) {
1593 rc_wait_CCR(bp);
1594 rc_out(bp, CD180_CCR, CCR_SSCH1);
1595 rc_wait_CCR(bp);
1597 rc_out(bp, CD180_MSVR, port->MSVR);
1598 restore_flags(flags);
1601 static void rc_stop(struct tty_struct * tty)
1603 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1604 struct riscom_board *bp;
1605 unsigned long flags;
1607 if (rc_paranoia_check(port, tty->device, "rc_stop"))
1608 return;
1610 bp = port_Board(port);
1612 save_flags(flags); cli();
1613 port->IER &= ~IER_TXRDY;
1614 rc_out(bp, CD180_CAR, port_No(port));
1615 rc_out(bp, CD180_IER, port->IER);
1616 restore_flags(flags);
1619 static void rc_start(struct tty_struct * tty)
1621 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1622 struct riscom_board *bp;
1623 unsigned long flags;
1625 if (rc_paranoia_check(port, tty->device, "rc_start"))
1626 return;
1628 bp = port_Board(port);
1630 save_flags(flags); cli();
1631 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1632 port->IER |= IER_TXRDY;
1633 rc_out(bp, CD180_CAR, port_No(port));
1634 rc_out(bp, CD180_IER, port->IER);
1636 restore_flags(flags);
1640 * This routine is called from the scheduler tqueue when the interrupt
1641 * routine has signalled that a hangup has occurred. The path of
1642 * hangup processing is:
1644 * serial interrupt routine -> (scheduler tqueue) ->
1645 * do_rc_hangup() -> tty->hangup() -> rc_hangup()
1648 static void do_rc_hangup(void *private_)
1650 struct riscom_port *port = (struct riscom_port *) private_;
1651 struct tty_struct *tty;
1653 tty = port->tty;
1654 if (!tty)
1655 return;
1657 tty_hangup(tty);
1660 static void rc_hangup(struct tty_struct * tty)
1662 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1663 struct riscom_board *bp;
1665 if (rc_paranoia_check(port, tty->device, "rc_hangup"))
1666 return;
1668 bp = port_Board(port);
1670 rc_shutdown_port(bp, port);
1671 port->event = 0;
1672 port->count = 0;
1673 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1674 port->tty = 0;
1675 wake_up_interruptible(&port->open_wait);
1678 static void rc_set_termios(struct tty_struct * tty, struct termios * old_termios)
1680 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1681 unsigned long flags;
1683 if (rc_paranoia_check(port, tty->device, "rc_set_termios"))
1684 return;
1686 if (tty->termios->c_cflag == old_termios->c_cflag &&
1687 tty->termios->c_iflag == old_termios->c_iflag)
1688 return;
1690 save_flags(flags); cli();
1691 rc_change_speed(port_Board(port), port);
1692 restore_flags(flags);
1694 if ((old_termios->c_cflag & CRTSCTS) &&
1695 !(tty->termios->c_cflag & CRTSCTS)) {
1696 tty->hw_stopped = 0;
1697 rc_start(tty);
1701 static void do_riscom_bh(void)
1703 run_task_queue(&tq_riscom);
1706 static void do_softint(void *private_)
1708 struct riscom_port *port = (struct riscom_port *) private_;
1709 struct tty_struct *tty;
1711 if(!(tty = port->tty))
1712 return;
1714 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
1715 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1716 tty->ldisc.write_wakeup)
1717 (tty->ldisc.write_wakeup)(tty);
1718 wake_up_interruptible(&tty->write_wait);
1722 static inline int rc_init_drivers(void)
1724 int error;
1725 int i;
1728 if (!(tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL))) {
1729 printk("rc: Couldn't get free page.\n");
1730 return 1;
1732 init_bh(RISCOM8_BH, do_riscom_bh);
1733 memset(IRQ_to_board, 0, sizeof(IRQ_to_board));
1734 memset(&riscom_driver, 0, sizeof(riscom_driver));
1735 riscom_driver.magic = TTY_DRIVER_MAGIC;
1736 riscom_driver.name = "ttyL";
1737 riscom_driver.major = RISCOM8_NORMAL_MAJOR;
1738 riscom_driver.num = RC_NBOARD * RC_NPORT;
1739 riscom_driver.type = TTY_DRIVER_TYPE_SERIAL;
1740 riscom_driver.subtype = RISCOM_TYPE_NORMAL;
1741 riscom_driver.init_termios = tty_std_termios;
1742 riscom_driver.init_termios.c_cflag =
1743 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1744 riscom_driver.flags = TTY_DRIVER_REAL_RAW;
1745 riscom_driver.refcount = &riscom_refcount;
1746 riscom_driver.table = riscom_table;
1747 riscom_driver.termios = riscom_termios;
1748 riscom_driver.termios_locked = riscom_termios_locked;
1750 riscom_driver.open = rc_open;
1751 riscom_driver.close = rc_close;
1752 riscom_driver.write = rc_write;
1753 riscom_driver.put_char = rc_put_char;
1754 riscom_driver.flush_chars = rc_flush_chars;
1755 riscom_driver.write_room = rc_write_room;
1756 riscom_driver.chars_in_buffer = rc_chars_in_buffer;
1757 riscom_driver.flush_buffer = rc_flush_buffer;
1758 riscom_driver.ioctl = rc_ioctl;
1759 riscom_driver.throttle = rc_throttle;
1760 riscom_driver.unthrottle = rc_unthrottle;
1761 riscom_driver.set_termios = rc_set_termios;
1762 riscom_driver.stop = rc_stop;
1763 riscom_driver.start = rc_start;
1764 riscom_driver.hangup = rc_hangup;
1766 riscom_callout_driver = riscom_driver;
1767 riscom_callout_driver.name = "cul";
1768 riscom_callout_driver.major = RISCOM8_CALLOUT_MAJOR;
1769 riscom_callout_driver.subtype = RISCOM_TYPE_CALLOUT;
1771 if ((error = tty_register_driver(&riscom_driver))) {
1772 free_page((unsigned long)tmp_buf);
1773 printk("rc: Couldn't register RISCom/8 driver, error = %d\n",
1774 error);
1775 return 1;
1777 if ((error = tty_register_driver(&riscom_callout_driver))) {
1778 free_page((unsigned long)tmp_buf);
1779 tty_unregister_driver(&riscom_driver);
1780 printk("rc: Couldn't register RISCom/8 callout driver, error = %d\n",
1781 error);
1782 return 1;
1785 memset(rc_port, 0, sizeof(rc_port));
1786 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1787 rc_port[i].callout_termios = riscom_callout_driver.init_termios;
1788 rc_port[i].normal_termios = riscom_driver.init_termios;
1789 rc_port[i].magic = RISCOM8_MAGIC;
1790 rc_port[i].tqueue.routine = do_softint;
1791 rc_port[i].tqueue.data = &rc_port[i];
1792 rc_port[i].tqueue_hangup.routine = do_rc_hangup;
1793 rc_port[i].tqueue_hangup.data = &rc_port[i];
1794 rc_port[i].close_delay = 50 * HZ/100;
1795 rc_port[i].closing_wait = 3000 * HZ/100;
1798 return 0;
1801 static void rc_release_drivers(void)
1803 unsigned long flags;
1805 save_flags(flags);
1806 cli();
1807 remove_bh(RISCOM8_BH);
1808 free_page((unsigned long)tmp_buf);
1809 tty_unregister_driver(&riscom_driver);
1810 tty_unregister_driver(&riscom_callout_driver);
1811 restore_flags(flags);
1814 #ifndef MODULE
1816 * Called at boot time.
1818 * You can specify IO base for up to RC_NBOARD cards,
1819 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1820 * Note that there will be no probing at default
1821 * addresses in this case.
1824 __initfunc(void riscom8_setup(char *str, int * ints))
1826 int i;
1828 for (i = 0; i < RC_NBOARD; i++) {
1829 if (i < ints[0])
1830 rc_board[i].base = ints[i+1];
1831 else
1832 rc_board[i].base = 0;
1835 #endif
1838 * This routine must be called by kernel at boot time
1840 __initfunc(int riscom8_init(void))
1842 int i;
1843 int found = 0;
1845 printk("rc: SDL RISCom/8 card driver v1.0, (c) D.Gorodchanin 1994-1996.\n");
1847 if (rc_init_drivers())
1848 return -EIO;
1850 for (i = 0; i < RC_NBOARD; i++)
1851 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1852 found++;
1854 if (!found) {
1855 rc_release_drivers();
1856 printk("rc: No RISCom/8 boards detected.\n");
1857 return -EIO;
1859 return 0;
1862 #ifdef MODULE
1863 int iobase = 0;
1864 int iobase1 = 0;
1865 int iobase2 = 0;
1866 int iobase3 = 0;
1867 MODULE_PARM(iobase, "i");
1868 MODULE_PARM(iobase1, "i");
1869 MODULE_PARM(iobase2, "i");
1870 MODULE_PARM(iobase3, "i");
1873 * You can setup up to 4 boards (current value of RC_NBOARD)
1874 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1877 int init_module(void)
1879 int i;
1881 if (iobase || iobase1 || iobase2 || iobase3) {
1882 for(i = 0; i < RC_NBOARD; i++)
1883 rc_board[0].base = 0;
1886 if (iobase)
1887 rc_board[0].base = iobase;
1888 if (iobase1)
1889 rc_board[1].base = iobase1;
1890 if (iobase2)
1891 rc_board[2].base = iobase2;
1892 if (iobase3)
1893 rc_board[3].base = iobase3;
1895 return riscom8_init();
1898 void cleanup_module(void)
1900 int i;
1902 rc_release_drivers();
1903 for (i = 0; i < RC_NBOARD; i++)
1904 if (rc_board[i].flags & RC_BOARD_PRESENT)
1905 rc_release_io_range(&rc_board[i]);
1908 #endif /* MODULE */