m68knommu: remove local gettimeofday code
[wrt350n-kernel.git] / drivers / char / riscom8.c
blob102ece4c4e0e8050efe26b36745db83e45069e4b
1 /*
2 * linux/drivers/char/riscom.c -- RISCom/8 multiport serial driver.
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others. The RISCom/8 card
8 * programming info was obtained from various drivers for other OSes
9 * (FreeBSD, ISC, etc), but no source code from those drivers were
10 * directly included in this driver.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Revision 1.1
29 * ChangeLog:
30 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31 * - get rid of check_region and several cleanups
34 #include <linux/module.h>
36 #include <asm/io.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/ioport.h>
40 #include <linux/interrupt.h>
41 #include <linux/errno.h>
42 #include <linux/tty.h>
43 #include <linux/mm.h>
44 #include <linux/serial.h>
45 #include <linux/fcntl.h>
46 #include <linux/major.h>
47 #include <linux/init.h>
48 #include <linux/delay.h>
49 #include <linux/tty_flip.h>
51 #include <asm/uaccess.h>
53 #include "riscom8.h"
54 #include "riscom8_reg.h"
56 /* Am I paranoid or not ? ;-) */
57 #define RISCOM_PARANOIA_CHECK
59 /*
60 * Crazy InteliCom/8 boards sometimes has swapped CTS & DSR signals.
61 * You can slightly speed up things by #undefing the following option,
62 * if you are REALLY sure that your board is correct one.
65 #define RISCOM_BRAIN_DAMAGED_CTS
67 /*
68 * The following defines are mostly for testing purposes. But if you need
69 * some nice reporting in your syslog, you can define them also.
71 #undef RC_REPORT_FIFO
72 #undef RC_REPORT_OVERRUN
75 #define RISCOM_LEGAL_FLAGS \
76 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
77 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
78 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
80 #define RS_EVENT_WRITE_WAKEUP 0
82 static struct tty_driver *riscom_driver;
84 static struct riscom_board rc_board[RC_NBOARD] = {
86 .base = RC_IOBASE1,
89 .base = RC_IOBASE2,
92 .base = RC_IOBASE3,
95 .base = RC_IOBASE4,
99 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
101 /* RISCom/8 I/O ports addresses (without address translation) */
102 static unsigned short rc_ioport[] = {
103 #if 1
104 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
105 #else
106 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
107 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
108 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
109 #endif
111 #define RC_NIOPORT ARRAY_SIZE(rc_ioport)
114 static inline int rc_paranoia_check(struct riscom_port const * port,
115 char *name, const char *routine)
117 #ifdef RISCOM_PARANOIA_CHECK
118 static const char badmagic[] = KERN_INFO
119 "rc: Warning: bad riscom port magic number for device %s in %s\n";
120 static const char badinfo[] = KERN_INFO
121 "rc: Warning: null riscom port for device %s in %s\n";
123 if (!port) {
124 printk(badinfo, name, routine);
125 return 1;
127 if (port->magic != RISCOM8_MAGIC) {
128 printk(badmagic, name, routine);
129 return 1;
131 #endif
132 return 0;
137 * Service functions for RISCom/8 driver.
141 /* Get board number from pointer */
142 static inline int board_No (struct riscom_board const * bp)
144 return bp - rc_board;
147 /* Get port number from pointer */
148 static inline int port_No (struct riscom_port const * port)
150 return RC_PORT(port - rc_port);
153 /* Get pointer to board from pointer to port */
154 static inline struct riscom_board * port_Board(struct riscom_port const * port)
156 return &rc_board[RC_BOARD(port - rc_port)];
159 /* Input Byte from CL CD180 register */
160 static inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
162 return inb(bp->base + RC_TO_ISA(reg));
165 /* Output Byte to CL CD180 register */
166 static inline void rc_out(struct riscom_board const * bp, unsigned short reg,
167 unsigned char val)
169 outb(val, bp->base + RC_TO_ISA(reg));
172 /* Wait for Channel Command Register ready */
173 static inline void rc_wait_CCR(struct riscom_board const * bp)
175 unsigned long delay;
177 /* FIXME: need something more descriptive then 100000 :) */
178 for (delay = 100000; delay; delay--)
179 if (!rc_in(bp, CD180_CCR))
180 return;
182 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
186 * RISCom/8 probe functions.
189 static inline int rc_request_io_range(struct riscom_board * const bp)
191 int i;
193 for (i = 0; i < RC_NIOPORT; i++)
194 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
195 "RISCom/8")) {
196 goto out_release;
198 return 0;
199 out_release:
200 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
201 board_No(bp), bp->base);
202 while(--i >= 0)
203 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
204 return 1;
207 static inline void rc_release_io_range(struct riscom_board * const bp)
209 int i;
211 for (i = 0; i < RC_NIOPORT; i++)
212 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
215 /* Reset and setup CD180 chip */
216 static void __init rc_init_CD180(struct riscom_board const * bp)
218 unsigned long flags;
220 save_flags(flags); cli();
221 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
222 rc_wait_CCR(bp); /* Wait for CCR ready */
223 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
224 sti();
225 msleep(50); /* Delay 0.05 sec */
226 cli();
227 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
228 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
229 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
230 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
231 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
233 /* Setting up prescaler. We need 4 ticks per 1 ms */
234 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
235 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
237 restore_flags(flags);
240 /* Main probing routine, also sets irq. */
241 static int __init rc_probe(struct riscom_board *bp)
243 unsigned char val1, val2;
244 int irqs = 0;
245 int retries;
247 bp->irq = 0;
249 if (rc_request_io_range(bp))
250 return 1;
252 /* Are the I/O ports here ? */
253 rc_out(bp, CD180_PPRL, 0x5a);
254 outb(0xff, 0x80);
255 val1 = rc_in(bp, CD180_PPRL);
256 rc_out(bp, CD180_PPRL, 0xa5);
257 outb(0x00, 0x80);
258 val2 = rc_in(bp, CD180_PPRL);
260 if ((val1 != 0x5a) || (val2 != 0xa5)) {
261 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
262 board_No(bp), bp->base);
263 goto out_release;
266 /* It's time to find IRQ for this board */
267 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
268 irqs = probe_irq_on();
269 rc_init_CD180(bp); /* Reset CD180 chip */
270 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
271 rc_wait_CCR(bp);
272 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
273 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
274 msleep(50);
275 irqs = probe_irq_off(irqs);
276 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
277 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
278 rc_init_CD180(bp); /* Reset CD180 again */
280 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
281 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
282 "found.\n", board_No(bp), bp->base);
283 goto out_release;
287 if (irqs <= 0) {
288 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
289 "at 0x%03x.\n", board_No(bp), bp->base);
290 goto out_release;
292 bp->irq = irqs;
293 bp->flags |= RC_BOARD_PRESENT;
295 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
296 "0x%03x, IRQ %d.\n",
297 board_No(bp),
298 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
299 bp->base, bp->irq);
301 return 0;
302 out_release:
303 rc_release_io_range(bp);
304 return 1;
309 * Interrupt processing routines.
313 static inline void rc_mark_event(struct riscom_port * port, int event)
315 set_bit(event, &port->event);
316 schedule_work(&port->tqueue);
319 static inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
320 unsigned char const * what)
322 unsigned char channel;
323 struct riscom_port * port;
325 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
326 if (channel < CD180_NCH) {
327 port = &rc_port[board_No(bp) * RC_NPORT + channel];
328 if (port->flags & ASYNC_INITIALIZED) {
329 return port;
332 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
333 board_No(bp), what, channel);
334 return NULL;
337 static inline void rc_receive_exc(struct riscom_board const * bp)
339 struct riscom_port *port;
340 struct tty_struct *tty;
341 unsigned char status;
342 unsigned char ch, flag;
344 if (!(port = rc_get_port(bp, "Receive")))
345 return;
347 tty = port->tty;
349 #ifdef RC_REPORT_OVERRUN
350 status = rc_in(bp, CD180_RCSR);
351 if (status & RCSR_OE)
352 port->overrun++;
353 status &= port->mark_mask;
354 #else
355 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
356 #endif
357 ch = rc_in(bp, CD180_RDR);
358 if (!status) {
359 return;
361 if (status & RCSR_TOUT) {
362 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
363 "Hardware problems ?\n",
364 board_No(bp), port_No(port));
365 return;
367 } else if (status & RCSR_BREAK) {
368 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
369 board_No(bp), port_No(port));
370 flag = TTY_BREAK;
371 if (port->flags & ASYNC_SAK)
372 do_SAK(tty);
374 } else if (status & RCSR_PE)
375 flag = TTY_PARITY;
377 else if (status & RCSR_FE)
378 flag = TTY_FRAME;
380 else if (status & RCSR_OE)
381 flag = TTY_OVERRUN;
383 else
384 flag = TTY_NORMAL;
386 tty_insert_flip_char(tty, ch, flag);
387 tty_flip_buffer_push(tty);
390 static inline void rc_receive(struct riscom_board const * bp)
392 struct riscom_port *port;
393 struct tty_struct *tty;
394 unsigned char count;
396 if (!(port = rc_get_port(bp, "Receive")))
397 return;
399 tty = port->tty;
401 count = rc_in(bp, CD180_RDCR);
403 #ifdef RC_REPORT_FIFO
404 port->hits[count > 8 ? 9 : count]++;
405 #endif
407 while (count--) {
408 if (tty_buffer_request_room(tty, 1) == 0) {
409 printk(KERN_WARNING "rc%d: port %d: Working around "
410 "flip buffer overflow.\n",
411 board_No(bp), port_No(port));
412 break;
414 tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
416 tty_flip_buffer_push(tty);
419 static inline void rc_transmit(struct riscom_board const * bp)
421 struct riscom_port *port;
422 struct tty_struct *tty;
423 unsigned char count;
426 if (!(port = rc_get_port(bp, "Transmit")))
427 return;
429 tty = port->tty;
431 if (port->IER & IER_TXEMPTY) {
432 /* FIFO drained */
433 rc_out(bp, CD180_CAR, port_No(port));
434 port->IER &= ~IER_TXEMPTY;
435 rc_out(bp, CD180_IER, port->IER);
436 return;
439 if ((port->xmit_cnt <= 0 && !port->break_length)
440 || tty->stopped || tty->hw_stopped) {
441 rc_out(bp, CD180_CAR, port_No(port));
442 port->IER &= ~IER_TXRDY;
443 rc_out(bp, CD180_IER, port->IER);
444 return;
447 if (port->break_length) {
448 if (port->break_length > 0) {
449 if (port->COR2 & COR2_ETC) {
450 rc_out(bp, CD180_TDR, CD180_C_ESC);
451 rc_out(bp, CD180_TDR, CD180_C_SBRK);
452 port->COR2 &= ~COR2_ETC;
454 count = min_t(int, port->break_length, 0xff);
455 rc_out(bp, CD180_TDR, CD180_C_ESC);
456 rc_out(bp, CD180_TDR, CD180_C_DELAY);
457 rc_out(bp, CD180_TDR, count);
458 if (!(port->break_length -= count))
459 port->break_length--;
460 } else {
461 rc_out(bp, CD180_TDR, CD180_C_ESC);
462 rc_out(bp, CD180_TDR, CD180_C_EBRK);
463 rc_out(bp, CD180_COR2, port->COR2);
464 rc_wait_CCR(bp);
465 rc_out(bp, CD180_CCR, CCR_CORCHG2);
466 port->break_length = 0;
468 return;
471 count = CD180_NFIFO;
472 do {
473 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
474 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
475 if (--port->xmit_cnt <= 0)
476 break;
477 } while (--count > 0);
479 if (port->xmit_cnt <= 0) {
480 rc_out(bp, CD180_CAR, port_No(port));
481 port->IER &= ~IER_TXRDY;
482 rc_out(bp, CD180_IER, port->IER);
484 if (port->xmit_cnt <= port->wakeup_chars)
485 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
488 static inline void rc_check_modem(struct riscom_board const * bp)
490 struct riscom_port *port;
491 struct tty_struct *tty;
492 unsigned char mcr;
494 if (!(port = rc_get_port(bp, "Modem")))
495 return;
497 tty = port->tty;
499 mcr = rc_in(bp, CD180_MCR);
500 if (mcr & MCR_CDCHG) {
501 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
502 wake_up_interruptible(&port->open_wait);
503 else
504 schedule_work(&port->tqueue_hangup);
507 #ifdef RISCOM_BRAIN_DAMAGED_CTS
508 if (mcr & MCR_CTSCHG) {
509 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
510 tty->hw_stopped = 0;
511 port->IER |= IER_TXRDY;
512 if (port->xmit_cnt <= port->wakeup_chars)
513 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
514 } else {
515 tty->hw_stopped = 1;
516 port->IER &= ~IER_TXRDY;
518 rc_out(bp, CD180_IER, port->IER);
520 if (mcr & MCR_DSRCHG) {
521 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
522 tty->hw_stopped = 0;
523 port->IER |= IER_TXRDY;
524 if (port->xmit_cnt <= port->wakeup_chars)
525 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
526 } else {
527 tty->hw_stopped = 1;
528 port->IER &= ~IER_TXRDY;
530 rc_out(bp, CD180_IER, port->IER);
532 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
534 /* Clear change bits */
535 rc_out(bp, CD180_MCR, 0);
538 /* The main interrupt processing routine */
539 static irqreturn_t rc_interrupt(int dummy, void * dev_id)
541 unsigned char status;
542 unsigned char ack;
543 struct riscom_board *bp = dev_id;
544 unsigned long loop = 0;
545 int handled = 0;
547 if (!(bp->flags & RC_BOARD_ACTIVE))
548 return IRQ_NONE;
550 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
551 (RC_BSR_TOUT | RC_BSR_TINT |
552 RC_BSR_MINT | RC_BSR_RINT))) {
553 handled = 1;
554 if (status & RC_BSR_TOUT)
555 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
556 "error?\n", board_No(bp));
558 else if (status & RC_BSR_RINT) {
559 ack = rc_in(bp, RC_ACK_RINT);
561 if (ack == (RC_ID | GIVR_IT_RCV))
562 rc_receive(bp);
563 else if (ack == (RC_ID | GIVR_IT_REXC))
564 rc_receive_exc(bp);
565 else
566 printk(KERN_WARNING "rc%d: Bad receive ack "
567 "0x%02x.\n",
568 board_No(bp), ack);
570 } else if (status & RC_BSR_TINT) {
571 ack = rc_in(bp, RC_ACK_TINT);
573 if (ack == (RC_ID | GIVR_IT_TX))
574 rc_transmit(bp);
575 else
576 printk(KERN_WARNING "rc%d: Bad transmit ack "
577 "0x%02x.\n",
578 board_No(bp), ack);
580 } else /* if (status & RC_BSR_MINT) */ {
581 ack = rc_in(bp, RC_ACK_MINT);
583 if (ack == (RC_ID | GIVR_IT_MODEM))
584 rc_check_modem(bp);
585 else
586 printk(KERN_WARNING "rc%d: Bad modem ack "
587 "0x%02x.\n",
588 board_No(bp), ack);
592 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
593 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
595 return IRQ_RETVAL(handled);
599 * Routines for open & close processing.
602 /* Called with disabled interrupts */
603 static int rc_setup_board(struct riscom_board * bp)
605 int error;
607 if (bp->flags & RC_BOARD_ACTIVE)
608 return 0;
610 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
611 "RISCom/8", bp);
612 if (error)
613 return error;
615 rc_out(bp, RC_CTOUT, 0); /* Just in case */
616 bp->DTR = ~0;
617 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
619 bp->flags |= RC_BOARD_ACTIVE;
621 return 0;
624 /* Called with disabled interrupts */
625 static void rc_shutdown_board(struct riscom_board *bp)
627 if (!(bp->flags & RC_BOARD_ACTIVE))
628 return;
630 bp->flags &= ~RC_BOARD_ACTIVE;
632 free_irq(bp->irq, NULL);
634 bp->DTR = ~0;
635 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
640 * Setting up port characteristics.
641 * Must be called with disabled interrupts
643 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
645 struct tty_struct *tty;
646 unsigned long baud;
647 long tmp;
648 unsigned char cor1 = 0, cor3 = 0;
649 unsigned char mcor1 = 0, mcor2 = 0;
651 if (!(tty = port->tty) || !tty->termios)
652 return;
654 port->IER = 0;
655 port->COR2 = 0;
656 port->MSVR = MSVR_RTS;
658 baud = tty_get_baud_rate(tty);
660 /* Select port on the board */
661 rc_out(bp, CD180_CAR, port_No(port));
663 if (!baud) {
664 /* Drop DTR & exit */
665 bp->DTR |= (1u << port_No(port));
666 rc_out(bp, RC_DTR, bp->DTR);
667 return;
668 } else {
669 /* Set DTR on */
670 bp->DTR &= ~(1u << port_No(port));
671 rc_out(bp, RC_DTR, bp->DTR);
675 * Now we must calculate some speed depended things
678 /* Set baud rate for port */
679 tmp = (((RC_OSCFREQ + baud/2) / baud +
680 CD180_TPC/2) / CD180_TPC);
682 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
683 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
684 rc_out(bp, CD180_RBPRL, tmp & 0xff);
685 rc_out(bp, CD180_TBPRL, tmp & 0xff);
687 baud = (baud + 5) / 10; /* Estimated CPS */
689 /* Two timer ticks seems enough to wakeup something like SLIP driver */
690 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
691 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
692 SERIAL_XMIT_SIZE - 1 : tmp);
694 /* Receiver timeout will be transmission time for 1.5 chars */
695 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
696 tmp = (tmp > 0xff) ? 0xff : tmp;
697 rc_out(bp, CD180_RTPR, tmp);
699 switch (C_CSIZE(tty)) {
700 case CS5:
701 cor1 |= COR1_5BITS;
702 break;
703 case CS6:
704 cor1 |= COR1_6BITS;
705 break;
706 case CS7:
707 cor1 |= COR1_7BITS;
708 break;
709 case CS8:
710 cor1 |= COR1_8BITS;
711 break;
714 if (C_CSTOPB(tty))
715 cor1 |= COR1_2SB;
717 cor1 |= COR1_IGNORE;
718 if (C_PARENB(tty)) {
719 cor1 |= COR1_NORMPAR;
720 if (C_PARODD(tty))
721 cor1 |= COR1_ODDP;
722 if (I_INPCK(tty))
723 cor1 &= ~COR1_IGNORE;
725 /* Set marking of some errors */
726 port->mark_mask = RCSR_OE | RCSR_TOUT;
727 if (I_INPCK(tty))
728 port->mark_mask |= RCSR_FE | RCSR_PE;
729 if (I_BRKINT(tty) || I_PARMRK(tty))
730 port->mark_mask |= RCSR_BREAK;
731 if (I_IGNPAR(tty))
732 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
733 if (I_IGNBRK(tty)) {
734 port->mark_mask &= ~RCSR_BREAK;
735 if (I_IGNPAR(tty))
736 /* Real raw mode. Ignore all */
737 port->mark_mask &= ~RCSR_OE;
739 /* Enable Hardware Flow Control */
740 if (C_CRTSCTS(tty)) {
741 #ifdef RISCOM_BRAIN_DAMAGED_CTS
742 port->IER |= IER_DSR | IER_CTS;
743 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
744 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
745 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
746 #else
747 port->COR2 |= COR2_CTSAE;
748 #endif
750 /* Enable Software Flow Control. FIXME: I'm not sure about this */
751 /* Some people reported that it works, but I still doubt */
752 if (I_IXON(tty)) {
753 port->COR2 |= COR2_TXIBE;
754 cor3 |= (COR3_FCT | COR3_SCDE);
755 if (I_IXANY(tty))
756 port->COR2 |= COR2_IXM;
757 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
758 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
759 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
760 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
762 if (!C_CLOCAL(tty)) {
763 /* Enable CD check */
764 port->IER |= IER_CD;
765 mcor1 |= MCOR1_CDZD;
766 mcor2 |= MCOR2_CDOD;
769 if (C_CREAD(tty))
770 /* Enable receiver */
771 port->IER |= IER_RXD;
773 /* Set input FIFO size (1-8 bytes) */
774 cor3 |= RISCOM_RXFIFO;
775 /* Setting up CD180 channel registers */
776 rc_out(bp, CD180_COR1, cor1);
777 rc_out(bp, CD180_COR2, port->COR2);
778 rc_out(bp, CD180_COR3, cor3);
779 /* Make CD180 know about registers change */
780 rc_wait_CCR(bp);
781 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
782 /* Setting up modem option registers */
783 rc_out(bp, CD180_MCOR1, mcor1);
784 rc_out(bp, CD180_MCOR2, mcor2);
785 /* Enable CD180 transmitter & receiver */
786 rc_wait_CCR(bp);
787 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
788 /* Enable interrupts */
789 rc_out(bp, CD180_IER, port->IER);
790 /* And finally set RTS on */
791 rc_out(bp, CD180_MSVR, port->MSVR);
794 /* Must be called with interrupts enabled */
795 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
797 unsigned long flags;
799 if (port->flags & ASYNC_INITIALIZED)
800 return 0;
802 if (!port->xmit_buf) {
803 /* We may sleep in get_zeroed_page() */
804 unsigned long tmp;
806 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
807 return -ENOMEM;
809 if (port->xmit_buf) {
810 free_page(tmp);
811 return -ERESTARTSYS;
813 port->xmit_buf = (unsigned char *) tmp;
816 save_flags(flags); cli();
818 if (port->tty)
819 clear_bit(TTY_IO_ERROR, &port->tty->flags);
821 if (port->count == 1)
822 bp->count++;
824 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
825 rc_change_speed(bp, port);
826 port->flags |= ASYNC_INITIALIZED;
828 restore_flags(flags);
829 return 0;
832 /* Must be called with interrupts disabled */
833 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
835 struct tty_struct *tty;
837 if (!(port->flags & ASYNC_INITIALIZED))
838 return;
840 #ifdef RC_REPORT_OVERRUN
841 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
842 board_No(bp), port_No(port), port->overrun);
843 #endif
844 #ifdef RC_REPORT_FIFO
846 int i;
848 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
849 board_No(bp), port_No(port));
850 for (i = 0; i < 10; i++) {
851 printk("%ld ", port->hits[i]);
853 printk("].\n");
855 #endif
856 if (port->xmit_buf) {
857 free_page((unsigned long) port->xmit_buf);
858 port->xmit_buf = NULL;
861 if (!(tty = port->tty) || C_HUPCL(tty)) {
862 /* Drop DTR */
863 bp->DTR |= (1u << port_No(port));
864 rc_out(bp, RC_DTR, bp->DTR);
867 /* Select port */
868 rc_out(bp, CD180_CAR, port_No(port));
869 /* Reset port */
870 rc_wait_CCR(bp);
871 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
872 /* Disable all interrupts from this port */
873 port->IER = 0;
874 rc_out(bp, CD180_IER, port->IER);
876 if (tty)
877 set_bit(TTY_IO_ERROR, &tty->flags);
878 port->flags &= ~ASYNC_INITIALIZED;
880 if (--bp->count < 0) {
881 printk(KERN_INFO "rc%d: rc_shutdown_port: "
882 "bad board count: %d\n",
883 board_No(bp), bp->count);
884 bp->count = 0;
888 * If this is the last opened port on the board
889 * shutdown whole board
891 if (!bp->count)
892 rc_shutdown_board(bp);
896 static int block_til_ready(struct tty_struct *tty, struct file * filp,
897 struct riscom_port *port)
899 DECLARE_WAITQUEUE(wait, current);
900 struct riscom_board *bp = port_Board(port);
901 int retval;
902 int do_clocal = 0;
903 int CD;
906 * If the device is in the middle of being closed, then block
907 * until it's done, and then try again.
909 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
910 interruptible_sleep_on(&port->close_wait);
911 if (port->flags & ASYNC_HUP_NOTIFY)
912 return -EAGAIN;
913 else
914 return -ERESTARTSYS;
918 * If non-blocking mode is set, or the port is not enabled,
919 * then make the check up front and then exit.
921 if ((filp->f_flags & O_NONBLOCK) ||
922 (tty->flags & (1 << TTY_IO_ERROR))) {
923 port->flags |= ASYNC_NORMAL_ACTIVE;
924 return 0;
927 if (C_CLOCAL(tty))
928 do_clocal = 1;
931 * Block waiting for the carrier detect and the line to become
932 * free (i.e., not in use by the callout). While we are in
933 * this loop, info->count is dropped by one, so that
934 * rs_close() knows when to free things. We restore it upon
935 * exit, either normal or abnormal.
937 retval = 0;
938 add_wait_queue(&port->open_wait, &wait);
939 cli();
940 if (!tty_hung_up_p(filp))
941 port->count--;
942 sti();
943 port->blocked_open++;
944 while (1) {
945 cli();
946 rc_out(bp, CD180_CAR, port_No(port));
947 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
948 rc_out(bp, CD180_MSVR, MSVR_RTS);
949 bp->DTR &= ~(1u << port_No(port));
950 rc_out(bp, RC_DTR, bp->DTR);
951 sti();
952 set_current_state(TASK_INTERRUPTIBLE);
953 if (tty_hung_up_p(filp) ||
954 !(port->flags & ASYNC_INITIALIZED)) {
955 if (port->flags & ASYNC_HUP_NOTIFY)
956 retval = -EAGAIN;
957 else
958 retval = -ERESTARTSYS;
959 break;
961 if (!(port->flags & ASYNC_CLOSING) &&
962 (do_clocal || CD))
963 break;
964 if (signal_pending(current)) {
965 retval = -ERESTARTSYS;
966 break;
968 schedule();
970 __set_current_state(TASK_RUNNING);
971 remove_wait_queue(&port->open_wait, &wait);
972 if (!tty_hung_up_p(filp))
973 port->count++;
974 port->blocked_open--;
975 if (retval)
976 return retval;
978 port->flags |= ASYNC_NORMAL_ACTIVE;
979 return 0;
982 static int rc_open(struct tty_struct * tty, struct file * filp)
984 int board;
985 int error;
986 struct riscom_port * port;
987 struct riscom_board * bp;
989 board = RC_BOARD(tty->index);
990 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
991 return -ENODEV;
993 bp = &rc_board[board];
994 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
995 if (rc_paranoia_check(port, tty->name, "rc_open"))
996 return -ENODEV;
998 if ((error = rc_setup_board(bp)))
999 return error;
1001 port->count++;
1002 tty->driver_data = port;
1003 port->tty = tty;
1005 if ((error = rc_setup_port(bp, port)))
1006 return error;
1008 if ((error = block_til_ready(tty, filp, port)))
1009 return error;
1011 return 0;
1014 static void rc_close(struct tty_struct * tty, struct file * filp)
1016 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1017 struct riscom_board *bp;
1018 unsigned long flags;
1019 unsigned long timeout;
1021 if (!port || rc_paranoia_check(port, tty->name, "close"))
1022 return;
1024 save_flags(flags); cli();
1025 if (tty_hung_up_p(filp))
1026 goto out;
1028 bp = port_Board(port);
1029 if ((tty->count == 1) && (port->count != 1)) {
1030 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1031 " tty->count is 1, port count is %d\n",
1032 board_No(bp), port->count);
1033 port->count = 1;
1035 if (--port->count < 0) {
1036 printk(KERN_INFO "rc%d: rc_close: bad port count "
1037 "for tty%d: %d\n",
1038 board_No(bp), port_No(port), port->count);
1039 port->count = 0;
1041 if (port->count)
1042 goto out;
1043 port->flags |= ASYNC_CLOSING;
1045 * Now we wait for the transmit buffer to clear; and we notify
1046 * the line discipline to only process XON/XOFF characters.
1048 tty->closing = 1;
1049 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1050 tty_wait_until_sent(tty, port->closing_wait);
1052 * At this point we stop accepting input. To do this, we
1053 * disable the receive line status interrupts, and tell the
1054 * interrupt driver to stop checking the data ready bit in the
1055 * line status register.
1057 port->IER &= ~IER_RXD;
1058 if (port->flags & ASYNC_INITIALIZED) {
1059 port->IER &= ~IER_TXRDY;
1060 port->IER |= IER_TXEMPTY;
1061 rc_out(bp, CD180_CAR, port_No(port));
1062 rc_out(bp, CD180_IER, port->IER);
1064 * Before we drop DTR, make sure the UART transmitter
1065 * has completely drained; this is especially
1066 * important if there is a transmit FIFO!
1068 timeout = jiffies+HZ;
1069 while(port->IER & IER_TXEMPTY) {
1070 msleep_interruptible(jiffies_to_msecs(port->timeout));
1071 if (time_after(jiffies, timeout))
1072 break;
1075 rc_shutdown_port(bp, port);
1076 if (tty->driver->flush_buffer)
1077 tty->driver->flush_buffer(tty);
1078 tty_ldisc_flush(tty);
1080 tty->closing = 0;
1081 port->event = 0;
1082 port->tty = NULL;
1083 if (port->blocked_open) {
1084 if (port->close_delay) {
1085 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1087 wake_up_interruptible(&port->open_wait);
1089 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1090 wake_up_interruptible(&port->close_wait);
1091 out: restore_flags(flags);
1094 static int rc_write(struct tty_struct * tty,
1095 const unsigned char *buf, int count)
1097 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1098 struct riscom_board *bp;
1099 int c, total = 0;
1100 unsigned long flags;
1102 if (rc_paranoia_check(port, tty->name, "rc_write"))
1103 return 0;
1105 bp = port_Board(port);
1107 if (!tty || !port->xmit_buf)
1108 return 0;
1110 save_flags(flags);
1111 while (1) {
1112 cli();
1113 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1114 SERIAL_XMIT_SIZE - port->xmit_head));
1115 if (c <= 0) {
1116 restore_flags(flags);
1117 break;
1120 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1121 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1122 port->xmit_cnt += c;
1123 restore_flags(flags);
1125 buf += c;
1126 count -= c;
1127 total += c;
1130 cli();
1131 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1132 !(port->IER & IER_TXRDY)) {
1133 port->IER |= IER_TXRDY;
1134 rc_out(bp, CD180_CAR, port_No(port));
1135 rc_out(bp, CD180_IER, port->IER);
1137 restore_flags(flags);
1139 return total;
1142 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1144 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1145 unsigned long flags;
1147 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1148 return;
1150 if (!tty || !port->xmit_buf)
1151 return;
1153 save_flags(flags); cli();
1155 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1156 goto out;
1158 port->xmit_buf[port->xmit_head++] = ch;
1159 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1160 port->xmit_cnt++;
1161 out: restore_flags(flags);
1164 static void rc_flush_chars(struct tty_struct * tty)
1166 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1167 unsigned long flags;
1169 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1170 return;
1172 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1173 !port->xmit_buf)
1174 return;
1176 save_flags(flags); cli();
1177 port->IER |= IER_TXRDY;
1178 rc_out(port_Board(port), CD180_CAR, port_No(port));
1179 rc_out(port_Board(port), CD180_IER, port->IER);
1180 restore_flags(flags);
1183 static int rc_write_room(struct tty_struct * tty)
1185 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1186 int ret;
1188 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1189 return 0;
1191 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1192 if (ret < 0)
1193 ret = 0;
1194 return ret;
1197 static int rc_chars_in_buffer(struct tty_struct *tty)
1199 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1201 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1202 return 0;
1204 return port->xmit_cnt;
1207 static void rc_flush_buffer(struct tty_struct *tty)
1209 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1210 unsigned long flags;
1212 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1213 return;
1215 save_flags(flags); cli();
1216 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1217 restore_flags(flags);
1219 tty_wakeup(tty);
1222 static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1224 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1225 struct riscom_board * bp;
1226 unsigned char status;
1227 unsigned int result;
1228 unsigned long flags;
1230 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1231 return -ENODEV;
1233 bp = port_Board(port);
1234 save_flags(flags); cli();
1235 rc_out(bp, CD180_CAR, port_No(port));
1236 status = rc_in(bp, CD180_MSVR);
1237 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1238 restore_flags(flags);
1239 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1240 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1241 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1242 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1243 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1244 return result;
1247 static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1248 unsigned int set, unsigned int clear)
1250 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1251 unsigned long flags;
1252 struct riscom_board *bp;
1254 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1255 return -ENODEV;
1257 bp = port_Board(port);
1259 save_flags(flags); cli();
1260 if (set & TIOCM_RTS)
1261 port->MSVR |= MSVR_RTS;
1262 if (set & TIOCM_DTR)
1263 bp->DTR &= ~(1u << port_No(port));
1265 if (clear & TIOCM_RTS)
1266 port->MSVR &= ~MSVR_RTS;
1267 if (clear & TIOCM_DTR)
1268 bp->DTR |= (1u << port_No(port));
1270 rc_out(bp, CD180_CAR, port_No(port));
1271 rc_out(bp, CD180_MSVR, port->MSVR);
1272 rc_out(bp, RC_DTR, bp->DTR);
1273 restore_flags(flags);
1274 return 0;
1277 static inline void rc_send_break(struct riscom_port * port, unsigned long length)
1279 struct riscom_board *bp = port_Board(port);
1280 unsigned long flags;
1282 save_flags(flags); cli();
1283 port->break_length = RISCOM_TPS / HZ * length;
1284 port->COR2 |= COR2_ETC;
1285 port->IER |= IER_TXRDY;
1286 rc_out(bp, CD180_CAR, port_No(port));
1287 rc_out(bp, CD180_COR2, port->COR2);
1288 rc_out(bp, CD180_IER, port->IER);
1289 rc_wait_CCR(bp);
1290 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1291 rc_wait_CCR(bp);
1292 restore_flags(flags);
1295 static inline int rc_set_serial_info(struct riscom_port * port,
1296 struct serial_struct __user * newinfo)
1298 struct serial_struct tmp;
1299 struct riscom_board *bp = port_Board(port);
1300 int change_speed;
1301 unsigned long flags;
1303 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1304 return -EFAULT;
1306 #if 0
1307 if ((tmp.irq != bp->irq) ||
1308 (tmp.port != bp->base) ||
1309 (tmp.type != PORT_CIRRUS) ||
1310 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1311 (tmp.custom_divisor != 0) ||
1312 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1313 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1314 return -EINVAL;
1315 #endif
1317 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1318 (tmp.flags & ASYNC_SPD_MASK));
1320 if (!capable(CAP_SYS_ADMIN)) {
1321 if ((tmp.close_delay != port->close_delay) ||
1322 (tmp.closing_wait != port->closing_wait) ||
1323 ((tmp.flags & ~ASYNC_USR_MASK) !=
1324 (port->flags & ~ASYNC_USR_MASK)))
1325 return -EPERM;
1326 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1327 (tmp.flags & ASYNC_USR_MASK));
1328 } else {
1329 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1330 (tmp.flags & ASYNC_FLAGS));
1331 port->close_delay = tmp.close_delay;
1332 port->closing_wait = tmp.closing_wait;
1334 if (change_speed) {
1335 save_flags(flags); cli();
1336 rc_change_speed(bp, port);
1337 restore_flags(flags);
1339 return 0;
1342 static inline int rc_get_serial_info(struct riscom_port * port,
1343 struct serial_struct __user *retinfo)
1345 struct serial_struct tmp;
1346 struct riscom_board *bp = port_Board(port);
1348 memset(&tmp, 0, sizeof(tmp));
1349 tmp.type = PORT_CIRRUS;
1350 tmp.line = port - rc_port;
1351 tmp.port = bp->base;
1352 tmp.irq = bp->irq;
1353 tmp.flags = port->flags;
1354 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1355 tmp.close_delay = port->close_delay * HZ/100;
1356 tmp.closing_wait = port->closing_wait * HZ/100;
1357 tmp.xmit_fifo_size = CD180_NFIFO;
1358 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1361 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1362 unsigned int cmd, unsigned long arg)
1365 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1366 void __user *argp = (void __user *)arg;
1367 int retval;
1369 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1370 return -ENODEV;
1372 switch (cmd) {
1373 case TCSBRK: /* SVID version: non-zero arg --> no break */
1374 retval = tty_check_change(tty);
1375 if (retval)
1376 return retval;
1377 tty_wait_until_sent(tty, 0);
1378 if (!arg)
1379 rc_send_break(port, HZ/4); /* 1/4 second */
1380 break;
1381 case TCSBRKP: /* support for POSIX tcsendbreak() */
1382 retval = tty_check_change(tty);
1383 if (retval)
1384 return retval;
1385 tty_wait_until_sent(tty, 0);
1386 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1387 break;
1388 case TIOCGSOFTCAR:
1389 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned __user *)argp);
1390 case TIOCSSOFTCAR:
1391 if (get_user(arg,(unsigned __user *) argp))
1392 return -EFAULT;
1393 tty->termios->c_cflag =
1394 ((tty->termios->c_cflag & ~CLOCAL) |
1395 (arg ? CLOCAL : 0));
1396 break;
1397 case TIOCGSERIAL:
1398 return rc_get_serial_info(port, argp);
1399 case TIOCSSERIAL:
1400 return rc_set_serial_info(port, argp);
1401 default:
1402 return -ENOIOCTLCMD;
1404 return 0;
1407 static void rc_throttle(struct tty_struct * tty)
1409 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1410 struct riscom_board *bp;
1411 unsigned long flags;
1413 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1414 return;
1416 bp = port_Board(port);
1418 save_flags(flags); cli();
1419 port->MSVR &= ~MSVR_RTS;
1420 rc_out(bp, CD180_CAR, port_No(port));
1421 if (I_IXOFF(tty)) {
1422 rc_wait_CCR(bp);
1423 rc_out(bp, CD180_CCR, CCR_SSCH2);
1424 rc_wait_CCR(bp);
1426 rc_out(bp, CD180_MSVR, port->MSVR);
1427 restore_flags(flags);
1430 static void rc_unthrottle(struct tty_struct * tty)
1432 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1433 struct riscom_board *bp;
1434 unsigned long flags;
1436 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1437 return;
1439 bp = port_Board(port);
1441 save_flags(flags); cli();
1442 port->MSVR |= MSVR_RTS;
1443 rc_out(bp, CD180_CAR, port_No(port));
1444 if (I_IXOFF(tty)) {
1445 rc_wait_CCR(bp);
1446 rc_out(bp, CD180_CCR, CCR_SSCH1);
1447 rc_wait_CCR(bp);
1449 rc_out(bp, CD180_MSVR, port->MSVR);
1450 restore_flags(flags);
1453 static void rc_stop(struct tty_struct * tty)
1455 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1456 struct riscom_board *bp;
1457 unsigned long flags;
1459 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1460 return;
1462 bp = port_Board(port);
1464 save_flags(flags); cli();
1465 port->IER &= ~IER_TXRDY;
1466 rc_out(bp, CD180_CAR, port_No(port));
1467 rc_out(bp, CD180_IER, port->IER);
1468 restore_flags(flags);
1471 static void rc_start(struct tty_struct * tty)
1473 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1474 struct riscom_board *bp;
1475 unsigned long flags;
1477 if (rc_paranoia_check(port, tty->name, "rc_start"))
1478 return;
1480 bp = port_Board(port);
1482 save_flags(flags); cli();
1483 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1484 port->IER |= IER_TXRDY;
1485 rc_out(bp, CD180_CAR, port_No(port));
1486 rc_out(bp, CD180_IER, port->IER);
1488 restore_flags(flags);
1492 * This routine is called from the work queue when the interrupt
1493 * routine has signalled that a hangup has occurred. The path of
1494 * hangup processing is:
1496 * serial interrupt routine -> (workqueue) ->
1497 * do_rc_hangup() -> tty->hangup() -> rc_hangup()
1500 static void do_rc_hangup(struct work_struct *ugly_api)
1502 struct riscom_port *port = container_of(ugly_api, struct riscom_port, tqueue_hangup);
1503 struct tty_struct *tty;
1505 tty = port->tty;
1506 if (tty)
1507 tty_hangup(tty); /* FIXME: module removal race still here */
1510 static void rc_hangup(struct tty_struct * tty)
1512 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1513 struct riscom_board *bp;
1515 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1516 return;
1518 bp = port_Board(port);
1520 rc_shutdown_port(bp, port);
1521 port->event = 0;
1522 port->count = 0;
1523 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1524 port->tty = NULL;
1525 wake_up_interruptible(&port->open_wait);
1528 static void rc_set_termios(struct tty_struct * tty, struct ktermios * old_termios)
1530 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1531 unsigned long flags;
1533 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1534 return;
1536 if (tty->termios->c_cflag == old_termios->c_cflag &&
1537 tty->termios->c_iflag == old_termios->c_iflag)
1538 return;
1540 save_flags(flags); cli();
1541 rc_change_speed(port_Board(port), port);
1542 restore_flags(flags);
1544 if ((old_termios->c_cflag & CRTSCTS) &&
1545 !(tty->termios->c_cflag & CRTSCTS)) {
1546 tty->hw_stopped = 0;
1547 rc_start(tty);
1551 static void do_softint(struct work_struct *ugly_api)
1553 struct riscom_port *port = container_of(ugly_api, struct riscom_port, tqueue);
1554 struct tty_struct *tty;
1556 if(!(tty = port->tty))
1557 return;
1559 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event))
1560 tty_wakeup(tty);
1563 static const struct tty_operations riscom_ops = {
1564 .open = rc_open,
1565 .close = rc_close,
1566 .write = rc_write,
1567 .put_char = rc_put_char,
1568 .flush_chars = rc_flush_chars,
1569 .write_room = rc_write_room,
1570 .chars_in_buffer = rc_chars_in_buffer,
1571 .flush_buffer = rc_flush_buffer,
1572 .ioctl = rc_ioctl,
1573 .throttle = rc_throttle,
1574 .unthrottle = rc_unthrottle,
1575 .set_termios = rc_set_termios,
1576 .stop = rc_stop,
1577 .start = rc_start,
1578 .hangup = rc_hangup,
1579 .tiocmget = rc_tiocmget,
1580 .tiocmset = rc_tiocmset,
1583 static inline int rc_init_drivers(void)
1585 int error;
1586 int i;
1588 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1589 if (!riscom_driver)
1590 return -ENOMEM;
1592 riscom_driver->owner = THIS_MODULE;
1593 riscom_driver->name = "ttyL";
1594 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1595 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1596 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1597 riscom_driver->init_termios = tty_std_termios;
1598 riscom_driver->init_termios.c_cflag =
1599 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1600 riscom_driver->init_termios.c_ispeed = 9600;
1601 riscom_driver->init_termios.c_ospeed = 9600;
1602 riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1603 tty_set_operations(riscom_driver, &riscom_ops);
1604 if ((error = tty_register_driver(riscom_driver))) {
1605 put_tty_driver(riscom_driver);
1606 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1607 "error = %d\n",
1608 error);
1609 return 1;
1612 memset(rc_port, 0, sizeof(rc_port));
1613 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1614 rc_port[i].magic = RISCOM8_MAGIC;
1615 INIT_WORK(&rc_port[i].tqueue, do_softint);
1616 INIT_WORK(&rc_port[i].tqueue_hangup, do_rc_hangup);
1617 rc_port[i].close_delay = 50 * HZ/100;
1618 rc_port[i].closing_wait = 3000 * HZ/100;
1619 init_waitqueue_head(&rc_port[i].open_wait);
1620 init_waitqueue_head(&rc_port[i].close_wait);
1623 return 0;
1626 static void rc_release_drivers(void)
1628 unsigned long flags;
1630 save_flags(flags);
1631 cli();
1632 tty_unregister_driver(riscom_driver);
1633 put_tty_driver(riscom_driver);
1634 restore_flags(flags);
1637 #ifndef MODULE
1639 * Called at boot time.
1641 * You can specify IO base for up to RC_NBOARD cards,
1642 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1643 * Note that there will be no probing at default
1644 * addresses in this case.
1647 static int __init riscom8_setup(char *str)
1649 int ints[RC_NBOARD];
1650 int i;
1652 str = get_options(str, ARRAY_SIZE(ints), ints);
1654 for (i = 0; i < RC_NBOARD; i++) {
1655 if (i < ints[0])
1656 rc_board[i].base = ints[i+1];
1657 else
1658 rc_board[i].base = 0;
1660 return 1;
1663 __setup("riscom8=", riscom8_setup);
1664 #endif
1666 static char banner[] __initdata =
1667 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1668 "1994-1996.\n";
1669 static char no_boards_msg[] __initdata =
1670 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1673 * This routine must be called by kernel at boot time
1675 static int __init riscom8_init(void)
1677 int i;
1678 int found = 0;
1680 printk(banner);
1682 if (rc_init_drivers())
1683 return -EIO;
1685 for (i = 0; i < RC_NBOARD; i++)
1686 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1687 found++;
1689 if (!found) {
1690 rc_release_drivers();
1691 printk(no_boards_msg);
1692 return -EIO;
1694 return 0;
1697 #ifdef MODULE
1698 static int iobase;
1699 static int iobase1;
1700 static int iobase2;
1701 static int iobase3;
1702 module_param(iobase, int, 0);
1703 module_param(iobase1, int, 0);
1704 module_param(iobase2, int, 0);
1705 module_param(iobase3, int, 0);
1707 MODULE_LICENSE("GPL");
1708 #endif /* MODULE */
1711 * You can setup up to 4 boards (current value of RC_NBOARD)
1712 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1715 static int __init riscom8_init_module (void)
1717 #ifdef MODULE
1718 int i;
1720 if (iobase || iobase1 || iobase2 || iobase3) {
1721 for(i = 0; i < RC_NBOARD; i++)
1722 rc_board[0].base = 0;
1725 if (iobase)
1726 rc_board[0].base = iobase;
1727 if (iobase1)
1728 rc_board[1].base = iobase1;
1729 if (iobase2)
1730 rc_board[2].base = iobase2;
1731 if (iobase3)
1732 rc_board[3].base = iobase3;
1733 #endif /* MODULE */
1735 return riscom8_init();
1738 static void __exit riscom8_exit_module (void)
1740 int i;
1742 rc_release_drivers();
1743 for (i = 0; i < RC_NBOARD; i++)
1744 if (rc_board[i].flags & RC_BOARD_PRESENT)
1745 rc_release_io_range(&rc_board[i]);
1749 module_init(riscom8_init_module);
1750 module_exit(riscom8_exit_module);