MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / riscom8.c
bloba35ea03a3227777156776e98ea3138e7fec4d43a
1 /*
2 * linux/drivers/char/riscom.c -- RISCom/8 multiport serial driver.
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others. The RISCom/8 card
8 * programming info was obtained from various drivers for other OSes
9 * (FreeBSD, ISC, etc), but no source code from those drivers were
10 * directly included in this driver.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Revision 1.1
29 * ChangeLog:
30 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31 * - get rid of check_region and several cleanups
34 #include <linux/module.h>
36 #include <asm/io.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/ioport.h>
40 #include <linux/interrupt.h>
41 #include <linux/errno.h>
42 #include <linux/tty.h>
43 #include <linux/mm.h>
44 #include <linux/serial.h>
45 #include <linux/fcntl.h>
46 #include <linux/major.h>
47 #include <linux/init.h>
49 #include <asm/uaccess.h>
51 #include "riscom8.h"
52 #include "riscom8_reg.h"
54 /* Am I paranoid or not ? ;-) */
55 #define RISCOM_PARANOIA_CHECK
57 /*
58 * Crazy InteliCom/8 boards sometimes has swapped CTS & DSR signals.
59 * You can slightly speed up things by #undefing the following option,
60 * if you are REALLY sure that your board is correct one.
63 #define RISCOM_BRAIN_DAMAGED_CTS
65 /*
66 * The following defines are mostly for testing purposes. But if you need
67 * some nice reporting in your syslog, you can define them also.
69 #undef RC_REPORT_FIFO
70 #undef RC_REPORT_OVERRUN
73 #define RISCOM_LEGAL_FLAGS \
74 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
75 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
76 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
78 #define RS_EVENT_WRITE_WAKEUP 0
80 static struct riscom_board * IRQ_to_board[16];
81 static struct tty_driver *riscom_driver;
82 static unsigned char * tmp_buf;
83 static DECLARE_MUTEX(tmp_buf_sem);
85 static unsigned long baud_table[] = {
86 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
87 9600, 19200, 38400, 57600, 76800, 0,
90 static struct riscom_board rc_board[RC_NBOARD] = {
92 .base = RC_IOBASE1,
95 .base = RC_IOBASE2,
98 .base = RC_IOBASE3,
101 .base = RC_IOBASE4,
105 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
107 /* RISCom/8 I/O ports addresses (without address translation) */
108 static unsigned short rc_ioport[] = {
109 #if 1
110 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
111 #else
112 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
113 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
114 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
115 #endif
117 #define RC_NIOPORT (sizeof(rc_ioport) / sizeof(rc_ioport[0]))
120 static inline int rc_paranoia_check(struct riscom_port const * port,
121 char *name, const char *routine)
123 #ifdef RISCOM_PARANOIA_CHECK
124 static const char badmagic[] = KERN_INFO
125 "rc: Warning: bad riscom port magic number for device %s in %s\n";
126 static const char badinfo[] = KERN_INFO
127 "rc: Warning: null riscom port for device %s in %s\n";
129 if (!port) {
130 printk(badinfo, name, routine);
131 return 1;
133 if (port->magic != RISCOM8_MAGIC) {
134 printk(badmagic, name, routine);
135 return 1;
137 #endif
138 return 0;
143 * Service functions for RISCom/8 driver.
147 /* Get board number from pointer */
148 static inline int board_No (struct riscom_board const * bp)
150 return bp - rc_board;
153 /* Get port number from pointer */
154 static inline int port_No (struct riscom_port const * port)
156 return RC_PORT(port - rc_port);
159 /* Get pointer to board from pointer to port */
160 static inline struct riscom_board * port_Board(struct riscom_port const * port)
162 return &rc_board[RC_BOARD(port - rc_port)];
165 /* Input Byte from CL CD180 register */
166 static inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
168 return inb(bp->base + RC_TO_ISA(reg));
171 /* Output Byte to CL CD180 register */
172 static inline void rc_out(struct riscom_board const * bp, unsigned short reg,
173 unsigned char val)
175 outb(val, bp->base + RC_TO_ISA(reg));
178 /* Wait for Channel Command Register ready */
179 static inline void rc_wait_CCR(struct riscom_board const * bp)
181 unsigned long delay;
183 /* FIXME: need something more descriptive then 100000 :) */
184 for (delay = 100000; delay; delay--)
185 if (!rc_in(bp, CD180_CCR))
186 return;
188 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
192 * RISCom/8 probe functions.
195 static inline int rc_request_io_range(struct riscom_board * const bp)
197 int i;
199 for (i = 0; i < RC_NIOPORT; i++)
200 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
201 "RISCom/8")) {
202 goto out_release;
204 return 0;
205 out_release:
206 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
207 board_No(bp), bp->base);
208 while(--i >= 0)
209 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
210 return 1;
213 static inline void rc_release_io_range(struct riscom_board * const bp)
215 int i;
217 for (i = 0; i < RC_NIOPORT; i++)
218 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
221 /* Must be called with enabled interrupts */
222 static inline void rc_long_delay(unsigned long delay)
224 unsigned long i;
226 for (i = jiffies + delay; time_after(i,jiffies); ) ;
229 /* Reset and setup CD180 chip */
230 static void __init rc_init_CD180(struct riscom_board const * bp)
232 unsigned long flags;
234 save_flags(flags); cli();
235 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
236 rc_wait_CCR(bp); /* Wait for CCR ready */
237 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
238 sti();
239 rc_long_delay(HZ/20); /* Delay 0.05 sec */
240 cli();
241 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
242 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
243 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
244 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
245 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
247 /* Setting up prescaler. We need 4 ticks per 1 ms */
248 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
249 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
251 restore_flags(flags);
254 /* Main probing routine, also sets irq. */
255 static int __init rc_probe(struct riscom_board *bp)
257 unsigned char val1, val2;
258 int irqs = 0;
259 int retries;
261 bp->irq = 0;
263 if (rc_request_io_range(bp))
264 return 1;
266 /* Are the I/O ports here ? */
267 rc_out(bp, CD180_PPRL, 0x5a);
268 outb(0xff, 0x80);
269 val1 = rc_in(bp, CD180_PPRL);
270 rc_out(bp, CD180_PPRL, 0xa5);
271 outb(0x00, 0x80);
272 val2 = rc_in(bp, CD180_PPRL);
274 if ((val1 != 0x5a) || (val2 != 0xa5)) {
275 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
276 board_No(bp), bp->base);
277 goto out_release;
280 /* It's time to find IRQ for this board */
281 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
282 irqs = probe_irq_on();
283 rc_init_CD180(bp); /* Reset CD180 chip */
284 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
285 rc_wait_CCR(bp);
286 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
287 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
288 rc_long_delay(HZ/20);
289 irqs = probe_irq_off(irqs);
290 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
291 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
292 rc_init_CD180(bp); /* Reset CD180 again */
294 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
295 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
296 "found.\n", board_No(bp), bp->base);
297 goto out_release;
301 if (irqs <= 0) {
302 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
303 "at 0x%03x.\n", board_No(bp), bp->base);
304 goto out_release;
306 bp->irq = irqs;
307 bp->flags |= RC_BOARD_PRESENT;
309 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
310 "0x%03x, IRQ %d.\n",
311 board_No(bp),
312 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
313 bp->base, bp->irq);
315 return 0;
316 out_release:
317 rc_release_io_range(bp);
318 return 1;
323 * Interrupt processing routines.
327 static inline void rc_mark_event(struct riscom_port * port, int event)
329 set_bit(event, &port->event);
330 schedule_work(&port->tqueue);
333 static inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
334 unsigned char const * what)
336 unsigned char channel;
337 struct riscom_port * port;
339 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
340 if (channel < CD180_NCH) {
341 port = &rc_port[board_No(bp) * RC_NPORT + channel];
342 if (port->flags & ASYNC_INITIALIZED) {
343 return port;
346 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
347 board_No(bp), what, channel);
348 return NULL;
351 static inline void rc_receive_exc(struct riscom_board const * bp)
353 struct riscom_port *port;
354 struct tty_struct *tty;
355 unsigned char status;
356 unsigned char ch;
358 if (!(port = rc_get_port(bp, "Receive")))
359 return;
361 tty = port->tty;
362 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
363 printk(KERN_WARNING "rc%d: port %d: Working around flip "
364 "buffer overflow.\n",
365 board_No(bp), port_No(port));
366 return;
369 #ifdef RC_REPORT_OVERRUN
370 status = rc_in(bp, CD180_RCSR);
371 if (status & RCSR_OE) {
372 port->overrun++;
373 #if 0
374 printk(KERN_ERR "rc%d: port %d: Overrun. Total %ld overruns\n",
375 board_No(bp), port_No(port), port->overrun);
376 #endif
378 status &= port->mark_mask;
379 #else
380 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
381 #endif
382 ch = rc_in(bp, CD180_RDR);
383 if (!status) {
384 return;
386 if (status & RCSR_TOUT) {
387 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
388 "Hardware problems ?\n",
389 board_No(bp), port_No(port));
390 return;
392 } else if (status & RCSR_BREAK) {
393 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
394 board_No(bp), port_No(port));
395 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
396 if (port->flags & ASYNC_SAK)
397 do_SAK(tty);
399 } else if (status & RCSR_PE)
400 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
402 else if (status & RCSR_FE)
403 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
405 else if (status & RCSR_OE)
406 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
408 else
409 *tty->flip.flag_buf_ptr++ = 0;
411 *tty->flip.char_buf_ptr++ = ch;
412 tty->flip.count++;
413 schedule_delayed_work(&tty->flip.work, 1);
416 static inline void rc_receive(struct riscom_board const * bp)
418 struct riscom_port *port;
419 struct tty_struct *tty;
420 unsigned char count;
422 if (!(port = rc_get_port(bp, "Receive")))
423 return;
425 tty = port->tty;
427 count = rc_in(bp, CD180_RDCR);
429 #ifdef RC_REPORT_FIFO
430 port->hits[count > 8 ? 9 : count]++;
431 #endif
433 while (count--) {
434 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
435 printk(KERN_WARNING "rc%d: port %d: Working around "
436 "flip buffer overflow.\n",
437 board_No(bp), port_No(port));
438 break;
440 *tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);
441 *tty->flip.flag_buf_ptr++ = 0;
442 tty->flip.count++;
444 schedule_delayed_work(&tty->flip.work, 1);
447 static inline void rc_transmit(struct riscom_board const * bp)
449 struct riscom_port *port;
450 struct tty_struct *tty;
451 unsigned char count;
454 if (!(port = rc_get_port(bp, "Transmit")))
455 return;
457 tty = port->tty;
459 if (port->IER & IER_TXEMPTY) {
460 /* FIFO drained */
461 rc_out(bp, CD180_CAR, port_No(port));
462 port->IER &= ~IER_TXEMPTY;
463 rc_out(bp, CD180_IER, port->IER);
464 return;
467 if ((port->xmit_cnt <= 0 && !port->break_length)
468 || tty->stopped || tty->hw_stopped) {
469 rc_out(bp, CD180_CAR, port_No(port));
470 port->IER &= ~IER_TXRDY;
471 rc_out(bp, CD180_IER, port->IER);
472 return;
475 if (port->break_length) {
476 if (port->break_length > 0) {
477 if (port->COR2 & COR2_ETC) {
478 rc_out(bp, CD180_TDR, CD180_C_ESC);
479 rc_out(bp, CD180_TDR, CD180_C_SBRK);
480 port->COR2 &= ~COR2_ETC;
482 count = min_t(int, port->break_length, 0xff);
483 rc_out(bp, CD180_TDR, CD180_C_ESC);
484 rc_out(bp, CD180_TDR, CD180_C_DELAY);
485 rc_out(bp, CD180_TDR, count);
486 if (!(port->break_length -= count))
487 port->break_length--;
488 } else {
489 rc_out(bp, CD180_TDR, CD180_C_ESC);
490 rc_out(bp, CD180_TDR, CD180_C_EBRK);
491 rc_out(bp, CD180_COR2, port->COR2);
492 rc_wait_CCR(bp);
493 rc_out(bp, CD180_CCR, CCR_CORCHG2);
494 port->break_length = 0;
496 return;
499 count = CD180_NFIFO;
500 do {
501 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
502 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
503 if (--port->xmit_cnt <= 0)
504 break;
505 } while (--count > 0);
507 if (port->xmit_cnt <= 0) {
508 rc_out(bp, CD180_CAR, port_No(port));
509 port->IER &= ~IER_TXRDY;
510 rc_out(bp, CD180_IER, port->IER);
512 if (port->xmit_cnt <= port->wakeup_chars)
513 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
516 static inline void rc_check_modem(struct riscom_board const * bp)
518 struct riscom_port *port;
519 struct tty_struct *tty;
520 unsigned char mcr;
522 if (!(port = rc_get_port(bp, "Modem")))
523 return;
525 tty = port->tty;
527 mcr = rc_in(bp, CD180_MCR);
528 if (mcr & MCR_CDCHG) {
529 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
530 wake_up_interruptible(&port->open_wait);
531 else
532 schedule_work(&port->tqueue_hangup);
535 #ifdef RISCOM_BRAIN_DAMAGED_CTS
536 if (mcr & MCR_CTSCHG) {
537 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
538 tty->hw_stopped = 0;
539 port->IER |= IER_TXRDY;
540 if (port->xmit_cnt <= port->wakeup_chars)
541 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
542 } else {
543 tty->hw_stopped = 1;
544 port->IER &= ~IER_TXRDY;
546 rc_out(bp, CD180_IER, port->IER);
548 if (mcr & MCR_DSRCHG) {
549 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
550 tty->hw_stopped = 0;
551 port->IER |= IER_TXRDY;
552 if (port->xmit_cnt <= port->wakeup_chars)
553 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
554 } else {
555 tty->hw_stopped = 1;
556 port->IER &= ~IER_TXRDY;
558 rc_out(bp, CD180_IER, port->IER);
560 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
562 /* Clear change bits */
563 rc_out(bp, CD180_MCR, 0);
566 /* The main interrupt processing routine */
567 static irqreturn_t rc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
569 unsigned char status;
570 unsigned char ack;
571 struct riscom_board *bp;
572 unsigned long loop = 0;
573 int handled = 0;
575 bp = IRQ_to_board[irq];
577 if (!bp || !(bp->flags & RC_BOARD_ACTIVE)) {
578 return IRQ_NONE;
581 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
582 (RC_BSR_TOUT | RC_BSR_TINT |
583 RC_BSR_MINT | RC_BSR_RINT))) {
584 handled = 1;
585 if (status & RC_BSR_TOUT)
586 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
587 "error?\n", board_No(bp));
589 else if (status & RC_BSR_RINT) {
590 ack = rc_in(bp, RC_ACK_RINT);
592 if (ack == (RC_ID | GIVR_IT_RCV))
593 rc_receive(bp);
594 else if (ack == (RC_ID | GIVR_IT_REXC))
595 rc_receive_exc(bp);
596 else
597 printk(KERN_WARNING "rc%d: Bad receive ack "
598 "0x%02x.\n",
599 board_No(bp), ack);
601 } else if (status & RC_BSR_TINT) {
602 ack = rc_in(bp, RC_ACK_TINT);
604 if (ack == (RC_ID | GIVR_IT_TX))
605 rc_transmit(bp);
606 else
607 printk(KERN_WARNING "rc%d: Bad transmit ack "
608 "0x%02x.\n",
609 board_No(bp), ack);
611 } else /* if (status & RC_BSR_MINT) */ {
612 ack = rc_in(bp, RC_ACK_MINT);
614 if (ack == (RC_ID | GIVR_IT_MODEM))
615 rc_check_modem(bp);
616 else
617 printk(KERN_WARNING "rc%d: Bad modem ack "
618 "0x%02x.\n",
619 board_No(bp), ack);
623 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
624 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
626 return IRQ_RETVAL(handled);
630 * Routines for open & close processing.
633 /* Called with disabled interrupts */
634 static inline int rc_setup_board(struct riscom_board * bp)
636 int error;
638 if (bp->flags & RC_BOARD_ACTIVE)
639 return 0;
641 error = request_irq(bp->irq, rc_interrupt, SA_INTERRUPT,
642 "RISCom/8", NULL);
643 if (error)
644 return error;
646 rc_out(bp, RC_CTOUT, 0); /* Just in case */
647 bp->DTR = ~0;
648 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
650 IRQ_to_board[bp->irq] = bp;
651 bp->flags |= RC_BOARD_ACTIVE;
653 return 0;
656 /* Called with disabled interrupts */
657 static inline void rc_shutdown_board(struct riscom_board *bp)
659 if (!(bp->flags & RC_BOARD_ACTIVE))
660 return;
662 bp->flags &= ~RC_BOARD_ACTIVE;
664 free_irq(bp->irq, NULL);
665 IRQ_to_board[bp->irq] = NULL;
667 bp->DTR = ~0;
668 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
673 * Setting up port characteristics.
674 * Must be called with disabled interrupts
676 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
678 struct tty_struct *tty;
679 unsigned long baud;
680 long tmp;
681 unsigned char cor1 = 0, cor3 = 0;
682 unsigned char mcor1 = 0, mcor2 = 0;
684 if (!(tty = port->tty) || !tty->termios)
685 return;
687 port->IER = 0;
688 port->COR2 = 0;
689 port->MSVR = MSVR_RTS;
691 baud = C_BAUD(tty);
693 if (baud & CBAUDEX) {
694 baud &= ~CBAUDEX;
695 if (baud < 1 || baud > 2)
696 port->tty->termios->c_cflag &= ~CBAUDEX;
697 else
698 baud += 15;
700 if (baud == 15) {
701 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
702 baud ++;
703 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
704 baud += 2;
707 /* Select port on the board */
708 rc_out(bp, CD180_CAR, port_No(port));
710 if (!baud_table[baud]) {
711 /* Drop DTR & exit */
712 bp->DTR |= (1u << port_No(port));
713 rc_out(bp, RC_DTR, bp->DTR);
714 return;
715 } else {
716 /* Set DTR on */
717 bp->DTR &= ~(1u << port_No(port));
718 rc_out(bp, RC_DTR, bp->DTR);
722 * Now we must calculate some speed depended things
725 /* Set baud rate for port */
726 tmp = (((RC_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +
727 CD180_TPC/2) / CD180_TPC);
729 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
730 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
731 rc_out(bp, CD180_RBPRL, tmp & 0xff);
732 rc_out(bp, CD180_TBPRL, tmp & 0xff);
734 baud = (baud_table[baud] + 5) / 10; /* Estimated CPS */
736 /* Two timer ticks seems enough to wakeup something like SLIP driver */
737 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
738 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
739 SERIAL_XMIT_SIZE - 1 : tmp);
741 /* Receiver timeout will be transmission time for 1.5 chars */
742 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
743 tmp = (tmp > 0xff) ? 0xff : tmp;
744 rc_out(bp, CD180_RTPR, tmp);
746 switch (C_CSIZE(tty)) {
747 case CS5:
748 cor1 |= COR1_5BITS;
749 break;
750 case CS6:
751 cor1 |= COR1_6BITS;
752 break;
753 case CS7:
754 cor1 |= COR1_7BITS;
755 break;
756 case CS8:
757 cor1 |= COR1_8BITS;
758 break;
761 if (C_CSTOPB(tty))
762 cor1 |= COR1_2SB;
764 cor1 |= COR1_IGNORE;
765 if (C_PARENB(tty)) {
766 cor1 |= COR1_NORMPAR;
767 if (C_PARODD(tty))
768 cor1 |= COR1_ODDP;
769 if (I_INPCK(tty))
770 cor1 &= ~COR1_IGNORE;
772 /* Set marking of some errors */
773 port->mark_mask = RCSR_OE | RCSR_TOUT;
774 if (I_INPCK(tty))
775 port->mark_mask |= RCSR_FE | RCSR_PE;
776 if (I_BRKINT(tty) || I_PARMRK(tty))
777 port->mark_mask |= RCSR_BREAK;
778 if (I_IGNPAR(tty))
779 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
780 if (I_IGNBRK(tty)) {
781 port->mark_mask &= ~RCSR_BREAK;
782 if (I_IGNPAR(tty))
783 /* Real raw mode. Ignore all */
784 port->mark_mask &= ~RCSR_OE;
786 /* Enable Hardware Flow Control */
787 if (C_CRTSCTS(tty)) {
788 #ifdef RISCOM_BRAIN_DAMAGED_CTS
789 port->IER |= IER_DSR | IER_CTS;
790 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
791 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
792 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
793 #else
794 port->COR2 |= COR2_CTSAE;
795 #endif
797 /* Enable Software Flow Control. FIXME: I'm not sure about this */
798 /* Some people reported that it works, but I still doubt */
799 if (I_IXON(tty)) {
800 port->COR2 |= COR2_TXIBE;
801 cor3 |= (COR3_FCT | COR3_SCDE);
802 if (I_IXANY(tty))
803 port->COR2 |= COR2_IXM;
804 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
805 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
806 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
807 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
809 if (!C_CLOCAL(tty)) {
810 /* Enable CD check */
811 port->IER |= IER_CD;
812 mcor1 |= MCOR1_CDZD;
813 mcor2 |= MCOR2_CDOD;
816 if (C_CREAD(tty))
817 /* Enable receiver */
818 port->IER |= IER_RXD;
820 /* Set input FIFO size (1-8 bytes) */
821 cor3 |= RISCOM_RXFIFO;
822 /* Setting up CD180 channel registers */
823 rc_out(bp, CD180_COR1, cor1);
824 rc_out(bp, CD180_COR2, port->COR2);
825 rc_out(bp, CD180_COR3, cor3);
826 /* Make CD180 know about registers change */
827 rc_wait_CCR(bp);
828 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
829 /* Setting up modem option registers */
830 rc_out(bp, CD180_MCOR1, mcor1);
831 rc_out(bp, CD180_MCOR2, mcor2);
832 /* Enable CD180 transmitter & receiver */
833 rc_wait_CCR(bp);
834 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
835 /* Enable interrupts */
836 rc_out(bp, CD180_IER, port->IER);
837 /* And finally set RTS on */
838 rc_out(bp, CD180_MSVR, port->MSVR);
841 /* Must be called with interrupts enabled */
842 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
844 unsigned long flags;
846 if (port->flags & ASYNC_INITIALIZED)
847 return 0;
849 if (!port->xmit_buf) {
850 /* We may sleep in get_zeroed_page() */
851 unsigned long tmp;
853 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
854 return -ENOMEM;
856 if (port->xmit_buf) {
857 free_page(tmp);
858 return -ERESTARTSYS;
860 port->xmit_buf = (unsigned char *) tmp;
863 save_flags(flags); cli();
865 if (port->tty)
866 clear_bit(TTY_IO_ERROR, &port->tty->flags);
868 if (port->count == 1)
869 bp->count++;
871 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
872 rc_change_speed(bp, port);
873 port->flags |= ASYNC_INITIALIZED;
875 restore_flags(flags);
876 return 0;
879 /* Must be called with interrupts disabled */
880 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
882 struct tty_struct *tty;
884 if (!(port->flags & ASYNC_INITIALIZED))
885 return;
887 #ifdef RC_REPORT_OVERRUN
888 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
889 board_No(bp), port_No(port), port->overrun);
890 #endif
891 #ifdef RC_REPORT_FIFO
893 int i;
895 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
896 board_No(bp), port_No(port));
897 for (i = 0; i < 10; i++) {
898 printk("%ld ", port->hits[i]);
900 printk("].\n");
902 #endif
903 if (port->xmit_buf) {
904 free_page((unsigned long) port->xmit_buf);
905 port->xmit_buf = NULL;
908 if (!(tty = port->tty) || C_HUPCL(tty)) {
909 /* Drop DTR */
910 bp->DTR |= (1u << port_No(port));
911 rc_out(bp, RC_DTR, bp->DTR);
914 /* Select port */
915 rc_out(bp, CD180_CAR, port_No(port));
916 /* Reset port */
917 rc_wait_CCR(bp);
918 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
919 /* Disable all interrupts from this port */
920 port->IER = 0;
921 rc_out(bp, CD180_IER, port->IER);
923 if (tty)
924 set_bit(TTY_IO_ERROR, &tty->flags);
925 port->flags &= ~ASYNC_INITIALIZED;
927 if (--bp->count < 0) {
928 printk(KERN_INFO "rc%d: rc_shutdown_port: "
929 "bad board count: %d\n",
930 board_No(bp), bp->count);
931 bp->count = 0;
935 * If this is the last opened port on the board
936 * shutdown whole board
938 if (!bp->count)
939 rc_shutdown_board(bp);
943 static int block_til_ready(struct tty_struct *tty, struct file * filp,
944 struct riscom_port *port)
946 DECLARE_WAITQUEUE(wait, current);
947 struct riscom_board *bp = port_Board(port);
948 int retval;
949 int do_clocal = 0;
950 int CD;
953 * If the device is in the middle of being closed, then block
954 * until it's done, and then try again.
956 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
957 interruptible_sleep_on(&port->close_wait);
958 if (port->flags & ASYNC_HUP_NOTIFY)
959 return -EAGAIN;
960 else
961 return -ERESTARTSYS;
965 * If non-blocking mode is set, or the port is not enabled,
966 * then make the check up front and then exit.
968 if ((filp->f_flags & O_NONBLOCK) ||
969 (tty->flags & (1 << TTY_IO_ERROR))) {
970 port->flags |= ASYNC_NORMAL_ACTIVE;
971 return 0;
974 if (C_CLOCAL(tty))
975 do_clocal = 1;
978 * Block waiting for the carrier detect and the line to become
979 * free (i.e., not in use by the callout). While we are in
980 * this loop, info->count is dropped by one, so that
981 * rs_close() knows when to free things. We restore it upon
982 * exit, either normal or abnormal.
984 retval = 0;
985 add_wait_queue(&port->open_wait, &wait);
986 cli();
987 if (!tty_hung_up_p(filp))
988 port->count--;
989 sti();
990 port->blocked_open++;
991 while (1) {
992 cli();
993 rc_out(bp, CD180_CAR, port_No(port));
994 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
995 rc_out(bp, CD180_MSVR, MSVR_RTS);
996 bp->DTR &= ~(1u << port_No(port));
997 rc_out(bp, RC_DTR, bp->DTR);
998 sti();
999 set_current_state(TASK_INTERRUPTIBLE);
1000 if (tty_hung_up_p(filp) ||
1001 !(port->flags & ASYNC_INITIALIZED)) {
1002 if (port->flags & ASYNC_HUP_NOTIFY)
1003 retval = -EAGAIN;
1004 else
1005 retval = -ERESTARTSYS;
1006 break;
1008 if (!(port->flags & ASYNC_CLOSING) &&
1009 (do_clocal || CD))
1010 break;
1011 if (signal_pending(current)) {
1012 retval = -ERESTARTSYS;
1013 break;
1015 schedule();
1017 current->state = TASK_RUNNING;
1018 remove_wait_queue(&port->open_wait, &wait);
1019 if (!tty_hung_up_p(filp))
1020 port->count++;
1021 port->blocked_open--;
1022 if (retval)
1023 return retval;
1025 port->flags |= ASYNC_NORMAL_ACTIVE;
1026 return 0;
1029 static int rc_open(struct tty_struct * tty, struct file * filp)
1031 int board;
1032 int error;
1033 struct riscom_port * port;
1034 struct riscom_board * bp;
1036 board = RC_BOARD(tty->index);
1037 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
1038 return -ENODEV;
1040 bp = &rc_board[board];
1041 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
1042 if (rc_paranoia_check(port, tty->name, "rc_open"))
1043 return -ENODEV;
1045 if ((error = rc_setup_board(bp)))
1046 return error;
1048 port->count++;
1049 tty->driver_data = port;
1050 port->tty = tty;
1052 if ((error = rc_setup_port(bp, port)))
1053 return error;
1055 if ((error = block_til_ready(tty, filp, port)))
1056 return error;
1058 return 0;
1061 static void rc_close(struct tty_struct * tty, struct file * filp)
1063 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1064 struct riscom_board *bp;
1065 unsigned long flags;
1066 unsigned long timeout;
1068 if (!port || rc_paranoia_check(port, tty->name, "close"))
1069 return;
1071 save_flags(flags); cli();
1072 if (tty_hung_up_p(filp))
1073 goto out;
1075 bp = port_Board(port);
1076 if ((tty->count == 1) && (port->count != 1)) {
1077 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1078 " tty->count is 1, port count is %d\n",
1079 board_No(bp), port->count);
1080 port->count = 1;
1082 if (--port->count < 0) {
1083 printk(KERN_INFO "rc%d: rc_close: bad port count "
1084 "for tty%d: %d\n",
1085 board_No(bp), port_No(port), port->count);
1086 port->count = 0;
1088 if (port->count)
1089 goto out;
1090 port->flags |= ASYNC_CLOSING;
1092 * Now we wait for the transmit buffer to clear; and we notify
1093 * the line discipline to only process XON/XOFF characters.
1095 tty->closing = 1;
1096 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1097 tty_wait_until_sent(tty, port->closing_wait);
1099 * At this point we stop accepting input. To do this, we
1100 * disable the receive line status interrupts, and tell the
1101 * interrupt driver to stop checking the data ready bit in the
1102 * line status register.
1104 port->IER &= ~IER_RXD;
1105 if (port->flags & ASYNC_INITIALIZED) {
1106 port->IER &= ~IER_TXRDY;
1107 port->IER |= IER_TXEMPTY;
1108 rc_out(bp, CD180_CAR, port_No(port));
1109 rc_out(bp, CD180_IER, port->IER);
1111 * Before we drop DTR, make sure the UART transmitter
1112 * has completely drained; this is especially
1113 * important if there is a transmit FIFO!
1115 timeout = jiffies+HZ;
1116 while(port->IER & IER_TXEMPTY) {
1117 current->state = TASK_INTERRUPTIBLE;
1118 schedule_timeout(port->timeout);
1119 if (time_after(jiffies, timeout))
1120 break;
1123 rc_shutdown_port(bp, port);
1124 if (tty->driver->flush_buffer)
1125 tty->driver->flush_buffer(tty);
1126 tty_ldisc_flush(tty);
1128 tty->closing = 0;
1129 port->event = 0;
1130 port->tty = NULL;
1131 if (port->blocked_open) {
1132 if (port->close_delay) {
1133 current->state = TASK_INTERRUPTIBLE;
1134 schedule_timeout(port->close_delay);
1136 wake_up_interruptible(&port->open_wait);
1138 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1139 wake_up_interruptible(&port->close_wait);
1140 out: restore_flags(flags);
1143 static int rc_write(struct tty_struct * tty, int from_user,
1144 const unsigned char *buf, int count)
1146 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1147 struct riscom_board *bp;
1148 int c, total = 0;
1149 unsigned long flags;
1151 if (rc_paranoia_check(port, tty->name, "rc_write"))
1152 return 0;
1154 bp = port_Board(port);
1156 if (!tty || !port->xmit_buf || !tmp_buf)
1157 return 0;
1159 save_flags(flags);
1160 if (from_user) {
1161 down(&tmp_buf_sem);
1162 while (1) {
1163 cli();
1164 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1165 SERIAL_XMIT_SIZE - port->xmit_head));
1166 if (c <= 0)
1167 break;
1169 c -= copy_from_user(tmp_buf, buf, c);
1170 if (!c) {
1171 if (!total)
1172 total = -EFAULT;
1173 break;
1176 cli();
1177 c = min_t(int, c, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1178 SERIAL_XMIT_SIZE - port->xmit_head));
1179 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1180 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1181 port->xmit_cnt += c;
1182 restore_flags(flags);
1184 buf += c;
1185 count -= c;
1186 total += c;
1188 up(&tmp_buf_sem);
1189 } else {
1190 while (1) {
1191 cli();
1192 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1193 SERIAL_XMIT_SIZE - port->xmit_head));
1194 if (c <= 0) {
1195 restore_flags(flags);
1196 break;
1199 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1200 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1201 port->xmit_cnt += c;
1202 restore_flags(flags);
1204 buf += c;
1205 count -= c;
1206 total += c;
1210 cli();
1211 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1212 !(port->IER & IER_TXRDY)) {
1213 port->IER |= IER_TXRDY;
1214 rc_out(bp, CD180_CAR, port_No(port));
1215 rc_out(bp, CD180_IER, port->IER);
1217 restore_flags(flags);
1219 return total;
1222 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1224 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1225 unsigned long flags;
1227 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1228 return;
1230 if (!tty || !port->xmit_buf)
1231 return;
1233 save_flags(flags); cli();
1235 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1236 goto out;
1238 port->xmit_buf[port->xmit_head++] = ch;
1239 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1240 port->xmit_cnt++;
1241 out: restore_flags(flags);
1244 static void rc_flush_chars(struct tty_struct * tty)
1246 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1247 unsigned long flags;
1249 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1250 return;
1252 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1253 !port->xmit_buf)
1254 return;
1256 save_flags(flags); cli();
1257 port->IER |= IER_TXRDY;
1258 rc_out(port_Board(port), CD180_CAR, port_No(port));
1259 rc_out(port_Board(port), CD180_IER, port->IER);
1260 restore_flags(flags);
1263 static int rc_write_room(struct tty_struct * tty)
1265 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1266 int ret;
1268 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1269 return 0;
1271 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1272 if (ret < 0)
1273 ret = 0;
1274 return ret;
1277 static int rc_chars_in_buffer(struct tty_struct *tty)
1279 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1281 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1282 return 0;
1284 return port->xmit_cnt;
1287 static void rc_flush_buffer(struct tty_struct *tty)
1289 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1290 unsigned long flags;
1292 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1293 return;
1295 save_flags(flags); cli();
1296 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1297 restore_flags(flags);
1299 wake_up_interruptible(&tty->write_wait);
1300 tty_wakeup(tty);
1303 static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1305 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1306 struct riscom_board * bp;
1307 unsigned char status;
1308 unsigned int result;
1309 unsigned long flags;
1311 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1312 return -ENODEV;
1314 bp = port_Board(port);
1315 save_flags(flags); cli();
1316 rc_out(bp, CD180_CAR, port_No(port));
1317 status = rc_in(bp, CD180_MSVR);
1318 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1319 restore_flags(flags);
1320 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1321 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1322 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1323 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1324 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1325 return result;
1328 static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1329 unsigned int set, unsigned int clear)
1331 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1332 unsigned long flags;
1333 struct riscom_board *bp;
1335 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1336 return -ENODEV;
1338 bp = port_Board(port);
1340 save_flags(flags); cli();
1341 if (set & TIOCM_RTS)
1342 port->MSVR |= MSVR_RTS;
1343 if (set & TIOCM_DTR)
1344 bp->DTR &= ~(1u << port_No(port));
1346 if (clear & TIOCM_RTS)
1347 port->MSVR &= ~MSVR_RTS;
1348 if (clear & TIOCM_DTR)
1349 bp->DTR |= (1u << port_No(port));
1351 rc_out(bp, CD180_CAR, port_No(port));
1352 rc_out(bp, CD180_MSVR, port->MSVR);
1353 rc_out(bp, RC_DTR, bp->DTR);
1354 restore_flags(flags);
1355 return 0;
1358 static inline void rc_send_break(struct riscom_port * port, unsigned long length)
1360 struct riscom_board *bp = port_Board(port);
1361 unsigned long flags;
1363 save_flags(flags); cli();
1364 port->break_length = RISCOM_TPS / HZ * length;
1365 port->COR2 |= COR2_ETC;
1366 port->IER |= IER_TXRDY;
1367 rc_out(bp, CD180_CAR, port_No(port));
1368 rc_out(bp, CD180_COR2, port->COR2);
1369 rc_out(bp, CD180_IER, port->IER);
1370 rc_wait_CCR(bp);
1371 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1372 rc_wait_CCR(bp);
1373 restore_flags(flags);
1376 static inline int rc_set_serial_info(struct riscom_port * port,
1377 struct serial_struct __user * newinfo)
1379 struct serial_struct tmp;
1380 struct riscom_board *bp = port_Board(port);
1381 int change_speed;
1382 unsigned long flags;
1384 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1385 return -EFAULT;
1387 #if 0
1388 if ((tmp.irq != bp->irq) ||
1389 (tmp.port != bp->base) ||
1390 (tmp.type != PORT_CIRRUS) ||
1391 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1392 (tmp.custom_divisor != 0) ||
1393 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1394 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1395 return -EINVAL;
1396 #endif
1398 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1399 (tmp.flags & ASYNC_SPD_MASK));
1401 if (!capable(CAP_SYS_ADMIN)) {
1402 if ((tmp.close_delay != port->close_delay) ||
1403 (tmp.closing_wait != port->closing_wait) ||
1404 ((tmp.flags & ~ASYNC_USR_MASK) !=
1405 (port->flags & ~ASYNC_USR_MASK)))
1406 return -EPERM;
1407 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1408 (tmp.flags & ASYNC_USR_MASK));
1409 } else {
1410 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1411 (tmp.flags & ASYNC_FLAGS));
1412 port->close_delay = tmp.close_delay;
1413 port->closing_wait = tmp.closing_wait;
1415 if (change_speed) {
1416 save_flags(flags); cli();
1417 rc_change_speed(bp, port);
1418 restore_flags(flags);
1420 return 0;
1423 static inline int rc_get_serial_info(struct riscom_port * port,
1424 struct serial_struct __user *retinfo)
1426 struct serial_struct tmp;
1427 struct riscom_board *bp = port_Board(port);
1429 memset(&tmp, 0, sizeof(tmp));
1430 tmp.type = PORT_CIRRUS;
1431 tmp.line = port - rc_port;
1432 tmp.port = bp->base;
1433 tmp.irq = bp->irq;
1434 tmp.flags = port->flags;
1435 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1436 tmp.close_delay = port->close_delay * HZ/100;
1437 tmp.closing_wait = port->closing_wait * HZ/100;
1438 tmp.xmit_fifo_size = CD180_NFIFO;
1439 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1442 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1443 unsigned int cmd, unsigned long arg)
1446 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1447 void __user *argp = (void __user *)arg;
1448 int retval;
1450 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1451 return -ENODEV;
1453 switch (cmd) {
1454 case TCSBRK: /* SVID version: non-zero arg --> no break */
1455 retval = tty_check_change(tty);
1456 if (retval)
1457 return retval;
1458 tty_wait_until_sent(tty, 0);
1459 if (!arg)
1460 rc_send_break(port, HZ/4); /* 1/4 second */
1461 break;
1462 case TCSBRKP: /* support for POSIX tcsendbreak() */
1463 retval = tty_check_change(tty);
1464 if (retval)
1465 return retval;
1466 tty_wait_until_sent(tty, 0);
1467 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1468 break;
1469 case TIOCGSOFTCAR:
1470 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned __user *)argp);
1471 case TIOCSSOFTCAR:
1472 if (get_user(arg,(unsigned __user *) argp))
1473 return -EFAULT;
1474 tty->termios->c_cflag =
1475 ((tty->termios->c_cflag & ~CLOCAL) |
1476 (arg ? CLOCAL : 0));
1477 break;
1478 case TIOCGSERIAL:
1479 return rc_get_serial_info(port, argp);
1480 case TIOCSSERIAL:
1481 return rc_set_serial_info(port, argp);
1482 default:
1483 return -ENOIOCTLCMD;
1485 return 0;
1488 static void rc_throttle(struct tty_struct * tty)
1490 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1491 struct riscom_board *bp;
1492 unsigned long flags;
1494 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1495 return;
1497 bp = port_Board(port);
1499 save_flags(flags); cli();
1500 port->MSVR &= ~MSVR_RTS;
1501 rc_out(bp, CD180_CAR, port_No(port));
1502 if (I_IXOFF(tty)) {
1503 rc_wait_CCR(bp);
1504 rc_out(bp, CD180_CCR, CCR_SSCH2);
1505 rc_wait_CCR(bp);
1507 rc_out(bp, CD180_MSVR, port->MSVR);
1508 restore_flags(flags);
1511 static void rc_unthrottle(struct tty_struct * tty)
1513 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1514 struct riscom_board *bp;
1515 unsigned long flags;
1517 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1518 return;
1520 bp = port_Board(port);
1522 save_flags(flags); cli();
1523 port->MSVR |= MSVR_RTS;
1524 rc_out(bp, CD180_CAR, port_No(port));
1525 if (I_IXOFF(tty)) {
1526 rc_wait_CCR(bp);
1527 rc_out(bp, CD180_CCR, CCR_SSCH1);
1528 rc_wait_CCR(bp);
1530 rc_out(bp, CD180_MSVR, port->MSVR);
1531 restore_flags(flags);
1534 static void rc_stop(struct tty_struct * tty)
1536 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1537 struct riscom_board *bp;
1538 unsigned long flags;
1540 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1541 return;
1543 bp = port_Board(port);
1545 save_flags(flags); cli();
1546 port->IER &= ~IER_TXRDY;
1547 rc_out(bp, CD180_CAR, port_No(port));
1548 rc_out(bp, CD180_IER, port->IER);
1549 restore_flags(flags);
1552 static void rc_start(struct tty_struct * tty)
1554 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1555 struct riscom_board *bp;
1556 unsigned long flags;
1558 if (rc_paranoia_check(port, tty->name, "rc_start"))
1559 return;
1561 bp = port_Board(port);
1563 save_flags(flags); cli();
1564 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1565 port->IER |= IER_TXRDY;
1566 rc_out(bp, CD180_CAR, port_No(port));
1567 rc_out(bp, CD180_IER, port->IER);
1569 restore_flags(flags);
1573 * This routine is called from the work queue when the interrupt
1574 * routine has signalled that a hangup has occurred. The path of
1575 * hangup processing is:
1577 * serial interrupt routine -> (workqueue) ->
1578 * do_rc_hangup() -> tty->hangup() -> rc_hangup()
1581 static void do_rc_hangup(void *private_)
1583 struct riscom_port *port = (struct riscom_port *) private_;
1584 struct tty_struct *tty;
1586 tty = port->tty;
1587 if (tty)
1588 tty_hangup(tty); /* FIXME: module removal race still here */
1591 static void rc_hangup(struct tty_struct * tty)
1593 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1594 struct riscom_board *bp;
1596 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1597 return;
1599 bp = port_Board(port);
1601 rc_shutdown_port(bp, port);
1602 port->event = 0;
1603 port->count = 0;
1604 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1605 port->tty = NULL;
1606 wake_up_interruptible(&port->open_wait);
1609 static void rc_set_termios(struct tty_struct * tty, struct termios * old_termios)
1611 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1612 unsigned long flags;
1614 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1615 return;
1617 if (tty->termios->c_cflag == old_termios->c_cflag &&
1618 tty->termios->c_iflag == old_termios->c_iflag)
1619 return;
1621 save_flags(flags); cli();
1622 rc_change_speed(port_Board(port), port);
1623 restore_flags(flags);
1625 if ((old_termios->c_cflag & CRTSCTS) &&
1626 !(tty->termios->c_cflag & CRTSCTS)) {
1627 tty->hw_stopped = 0;
1628 rc_start(tty);
1632 static void do_softint(void *private_)
1634 struct riscom_port *port = (struct riscom_port *) private_;
1635 struct tty_struct *tty;
1637 if(!(tty = port->tty))
1638 return;
1640 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
1641 tty_wakeup(tty);
1642 wake_up_interruptible(&tty->write_wait);
1646 static struct tty_operations riscom_ops = {
1647 .open = rc_open,
1648 .close = rc_close,
1649 .write = rc_write,
1650 .put_char = rc_put_char,
1651 .flush_chars = rc_flush_chars,
1652 .write_room = rc_write_room,
1653 .chars_in_buffer = rc_chars_in_buffer,
1654 .flush_buffer = rc_flush_buffer,
1655 .ioctl = rc_ioctl,
1656 .throttle = rc_throttle,
1657 .unthrottle = rc_unthrottle,
1658 .set_termios = rc_set_termios,
1659 .stop = rc_stop,
1660 .start = rc_start,
1661 .hangup = rc_hangup,
1662 .tiocmget = rc_tiocmget,
1663 .tiocmset = rc_tiocmset,
1666 static inline int rc_init_drivers(void)
1668 int error;
1669 int i;
1671 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1672 if (!riscom_driver)
1673 return -ENOMEM;
1675 if (!(tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL))) {
1676 printk(KERN_ERR "rc: Couldn't get free page.\n");
1677 put_tty_driver(riscom_driver);
1678 return 1;
1680 memset(IRQ_to_board, 0, sizeof(IRQ_to_board));
1681 riscom_driver->owner = THIS_MODULE;
1682 riscom_driver->name = "ttyL";
1683 riscom_driver->devfs_name = "tts/L";
1684 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1685 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1686 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1687 riscom_driver->init_termios = tty_std_termios;
1688 riscom_driver->init_termios.c_cflag =
1689 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1690 riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1691 tty_set_operations(riscom_driver, &riscom_ops);
1692 if ((error = tty_register_driver(riscom_driver))) {
1693 free_page((unsigned long)tmp_buf);
1694 put_tty_driver(riscom_driver);
1695 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1696 "error = %d\n",
1697 error);
1698 return 1;
1701 memset(rc_port, 0, sizeof(rc_port));
1702 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1703 rc_port[i].magic = RISCOM8_MAGIC;
1704 INIT_WORK(&rc_port[i].tqueue, do_softint, &rc_port[i]);
1705 INIT_WORK(&rc_port[i].tqueue_hangup, do_rc_hangup, &rc_port[i]);
1706 rc_port[i].close_delay = 50 * HZ/100;
1707 rc_port[i].closing_wait = 3000 * HZ/100;
1708 init_waitqueue_head(&rc_port[i].open_wait);
1709 init_waitqueue_head(&rc_port[i].close_wait);
1712 return 0;
1715 static void rc_release_drivers(void)
1717 unsigned long flags;
1719 save_flags(flags);
1720 cli();
1721 free_page((unsigned long)tmp_buf);
1722 tty_unregister_driver(riscom_driver);
1723 put_tty_driver(riscom_driver);
1724 restore_flags(flags);
1727 #ifndef MODULE
1729 * Called at boot time.
1731 * You can specify IO base for up to RC_NBOARD cards,
1732 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1733 * Note that there will be no probing at default
1734 * addresses in this case.
1737 static int __init riscom8_setup(char *str)
1739 int ints[RC_NBOARD];
1740 int i;
1742 str = get_options(str, ARRAY_SIZE(ints), ints);
1744 for (i = 0; i < RC_NBOARD; i++) {
1745 if (i < ints[0])
1746 rc_board[i].base = ints[i+1];
1747 else
1748 rc_board[i].base = 0;
1750 return 1;
1753 __setup("riscom8=", riscom8_setup);
1754 #endif
1756 static char banner[] __initdata =
1757 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1758 "1994-1996.\n";
1759 static char no_boards_msg[] __initdata =
1760 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1763 * This routine must be called by kernel at boot time
1765 static int __init riscom8_init(void)
1767 int i;
1768 int found = 0;
1770 printk(banner);
1772 if (rc_init_drivers())
1773 return -EIO;
1775 for (i = 0; i < RC_NBOARD; i++)
1776 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1777 found++;
1779 if (!found) {
1780 rc_release_drivers();
1781 printk(no_boards_msg);
1782 return -EIO;
1784 return 0;
1787 #ifdef MODULE
1788 static int iobase;
1789 static int iobase1;
1790 static int iobase2;
1791 static int iobase3;
1792 MODULE_PARM(iobase, "i");
1793 MODULE_PARM(iobase1, "i");
1794 MODULE_PARM(iobase2, "i");
1795 MODULE_PARM(iobase3, "i");
1797 MODULE_LICENSE("GPL");
1798 #endif /* MODULE */
1801 * You can setup up to 4 boards (current value of RC_NBOARD)
1802 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1805 static int __init riscom8_init_module (void)
1807 #ifdef MODULE
1808 int i;
1810 if (iobase || iobase1 || iobase2 || iobase3) {
1811 for(i = 0; i < RC_NBOARD; i++)
1812 rc_board[0].base = 0;
1815 if (iobase)
1816 rc_board[0].base = iobase;
1817 if (iobase1)
1818 rc_board[1].base = iobase1;
1819 if (iobase2)
1820 rc_board[2].base = iobase2;
1821 if (iobase3)
1822 rc_board[3].base = iobase3;
1823 #endif /* MODULE */
1825 return riscom8_init();
1828 static void __exit riscom8_exit_module (void)
1830 int i;
1832 rc_release_drivers();
1833 for (i = 0; i < RC_NBOARD; i++)
1834 if (rc_board[i].flags & RC_BOARD_PRESENT)
1835 rc_release_io_range(&rc_board[i]);
1839 module_init(riscom8_init_module);
1840 module_exit(riscom8_exit_module);