Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / riscom8.c
blob86c03dd9f0f857c7d63742c19ab8a53c7f16200e
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 #ifndef MIN
79 #define MIN(a,b) ((a) < (b) ? (a) : (b))
80 #endif
82 #define RS_EVENT_WRITE_WAKEUP 0
84 static DECLARE_TASK_QUEUE(tq_riscom);
86 static struct riscom_board * IRQ_to_board[16];
87 static struct tty_driver *riscom_driver;
88 static unsigned char * tmp_buf;
89 static DECLARE_MUTEX(tmp_buf_sem);
91 static unsigned long baud_table[] = {
92 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
93 9600, 19200, 38400, 57600, 76800, 0,
96 static struct riscom_board rc_board[RC_NBOARD] = {
98 .base = RC_IOBASE1,
101 .base = RC_IOBASE2,
104 .base = RC_IOBASE3,
107 .base = RC_IOBASE4,
111 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
113 /* RISCom/8 I/O ports addresses (without address translation) */
114 static unsigned short rc_ioport[] = {
115 #if 1
116 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
117 #else
118 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
119 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
120 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
121 #endif
123 #define RC_NIOPORT (sizeof(rc_ioport) / sizeof(rc_ioport[0]))
126 static inline int rc_paranoia_check(struct riscom_port const * port,
127 char *name, const char *routine)
129 #ifdef RISCOM_PARANOIA_CHECK
130 static const char badmagic[] = KERN_INFO
131 "rc: Warning: bad riscom port magic number for device %s in %s\n";
132 static const char badinfo[] = KERN_INFO
133 "rc: Warning: null riscom port for device %s in %s\n";
135 if (!port) {
136 printk(badinfo, name, routine);
137 return 1;
139 if (port->magic != RISCOM8_MAGIC) {
140 printk(badmagic, name, routine);
141 return 1;
143 #endif
144 return 0;
149 * Service functions for RISCom/8 driver.
153 /* Get board number from pointer */
154 static inline int board_No (struct riscom_board const * bp)
156 return bp - rc_board;
159 /* Get port number from pointer */
160 static inline int port_No (struct riscom_port const * port)
162 return RC_PORT(port - rc_port);
165 /* Get pointer to board from pointer to port */
166 static inline struct riscom_board * port_Board(struct riscom_port const * port)
168 return &rc_board[RC_BOARD(port - rc_port)];
171 /* Input Byte from CL CD180 register */
172 static inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
174 return inb(bp->base + RC_TO_ISA(reg));
177 /* Output Byte to CL CD180 register */
178 static inline void rc_out(struct riscom_board const * bp, unsigned short reg,
179 unsigned char val)
181 outb(val, bp->base + RC_TO_ISA(reg));
184 /* Wait for Channel Command Register ready */
185 static inline void rc_wait_CCR(struct riscom_board const * bp)
187 unsigned long delay;
189 /* FIXME: need something more descriptive then 100000 :) */
190 for (delay = 100000; delay; delay--)
191 if (!rc_in(bp, CD180_CCR))
192 return;
194 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
198 * RISCom/8 probe functions.
201 static inline int rc_request_io_range(struct riscom_board * const bp)
203 int i;
205 for (i = 0; i < RC_NIOPORT; i++)
206 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
207 "RISCom/8")) {
208 goto out_release;
210 return 0;
211 out_release:
212 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
213 board_No(bp), bp->base);
214 while(--i >= 0)
215 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
216 return 1;
219 static inline void rc_release_io_range(struct riscom_board * const bp)
221 int i;
223 for (i = 0; i < RC_NIOPORT; i++)
224 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
227 /* Must be called with enabled interrupts */
228 static inline void rc_long_delay(unsigned long delay)
230 unsigned long i;
232 for (i = jiffies + delay; time_after(i,jiffies); ) ;
235 /* Reset and setup CD180 chip */
236 static void __init rc_init_CD180(struct riscom_board const * bp)
238 unsigned long flags;
240 save_flags(flags); cli();
241 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
242 rc_wait_CCR(bp); /* Wait for CCR ready */
243 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
244 sti();
245 rc_long_delay(HZ/20); /* Delay 0.05 sec */
246 cli();
247 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
248 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
249 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
250 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
251 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
253 /* Setting up prescaler. We need 4 ticks per 1 ms */
254 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
255 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
257 restore_flags(flags);
260 /* Main probing routine, also sets irq. */
261 static int __init rc_probe(struct riscom_board *bp)
263 unsigned char val1, val2;
264 int irqs = 0;
265 int retries;
267 bp->irq = 0;
269 if (rc_request_io_range(bp))
270 return 1;
272 /* Are the I/O ports here ? */
273 rc_out(bp, CD180_PPRL, 0x5a);
274 outb(0xff, 0x80);
275 val1 = rc_in(bp, CD180_PPRL);
276 rc_out(bp, CD180_PPRL, 0xa5);
277 outb(0x00, 0x80);
278 val2 = rc_in(bp, CD180_PPRL);
280 if ((val1 != 0x5a) || (val2 != 0xa5)) {
281 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
282 board_No(bp), bp->base);
283 goto out_release;
286 /* It's time to find IRQ for this board */
287 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
288 irqs = probe_irq_on();
289 rc_init_CD180(bp); /* Reset CD180 chip */
290 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
291 rc_wait_CCR(bp);
292 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
293 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
294 rc_long_delay(HZ/20);
295 irqs = probe_irq_off(irqs);
296 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
297 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
298 rc_init_CD180(bp); /* Reset CD180 again */
300 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
301 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
302 "found.\n", board_No(bp), bp->base);
303 goto out_release;
307 if (irqs <= 0) {
308 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
309 "at 0x%03x.\n", board_No(bp), bp->base);
310 goto out_release;
312 bp->irq = irqs;
313 bp->flags |= RC_BOARD_PRESENT;
315 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
316 "0x%03x, IRQ %d.\n",
317 board_No(bp),
318 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
319 bp->base, bp->irq);
321 return 0;
322 out_release:
323 rc_release_io_range(bp);
324 return 1;
329 * Interrupt processing routines.
333 static inline void rc_mark_event(struct riscom_port * port, int event)
336 * I'm not quite happy with current scheme all serial
337 * drivers use their own BH routine.
338 * It seems this easily can be done with one BH routine
339 * serving for all serial drivers.
340 * For now I must introduce another one - RISCOM8_BH.
341 * Still hope this will be changed in near future.
343 set_bit(event, &port->event);
344 queue_task(&port->tqueue, &tq_riscom);
345 mark_bh(RISCOM8_BH);
348 static inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
349 unsigned char const * what)
351 unsigned char channel;
352 struct riscom_port * port;
354 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
355 if (channel < CD180_NCH) {
356 port = &rc_port[board_No(bp) * RC_NPORT + channel];
357 if (port->flags & ASYNC_INITIALIZED) {
358 return port;
361 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
362 board_No(bp), what, channel);
363 return NULL;
366 static inline void rc_receive_exc(struct riscom_board const * bp)
368 struct riscom_port *port;
369 struct tty_struct *tty;
370 unsigned char status;
371 unsigned char ch;
373 if (!(port = rc_get_port(bp, "Receive")))
374 return;
376 tty = port->tty;
377 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
378 printk(KERN_WARNING "rc%d: port %d: Working around flip "
379 "buffer overflow.\n",
380 board_No(bp), port_No(port));
381 return;
384 #ifdef RC_REPORT_OVERRUN
385 status = rc_in(bp, CD180_RCSR);
386 if (status & RCSR_OE) {
387 port->overrun++;
388 #if 0
389 printk(KERN_ERR "rc%d: port %d: Overrun. Total %ld overruns\n",
390 board_No(bp), port_No(port), port->overrun);
391 #endif
393 status &= port->mark_mask;
394 #else
395 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
396 #endif
397 ch = rc_in(bp, CD180_RDR);
398 if (!status) {
399 return;
401 if (status & RCSR_TOUT) {
402 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
403 "Hardware problems ?\n",
404 board_No(bp), port_No(port));
405 return;
407 } else if (status & RCSR_BREAK) {
408 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
409 board_No(bp), port_No(port));
410 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
411 if (port->flags & ASYNC_SAK)
412 do_SAK(tty);
414 } else if (status & RCSR_PE)
415 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
417 else if (status & RCSR_FE)
418 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
420 else if (status & RCSR_OE)
421 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
423 else
424 *tty->flip.flag_buf_ptr++ = 0;
426 *tty->flip.char_buf_ptr++ = ch;
427 tty->flip.count++;
428 queue_task(&tty->flip.tqueue, &tq_timer);
431 static inline void rc_receive(struct riscom_board const * bp)
433 struct riscom_port *port;
434 struct tty_struct *tty;
435 unsigned char count;
437 if (!(port = rc_get_port(bp, "Receive")))
438 return;
440 tty = port->tty;
442 count = rc_in(bp, CD180_RDCR);
444 #ifdef RC_REPORT_FIFO
445 port->hits[count > 8 ? 9 : count]++;
446 #endif
448 while (count--) {
449 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
450 printk(KERN_WARNING "rc%d: port %d: Working around "
451 "flip buffer overflow.\n",
452 board_No(bp), port_No(port));
453 break;
455 *tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);
456 *tty->flip.flag_buf_ptr++ = 0;
457 tty->flip.count++;
459 queue_task(&tty->flip.tqueue, &tq_timer);
462 static inline void rc_transmit(struct riscom_board const * bp)
464 struct riscom_port *port;
465 struct tty_struct *tty;
466 unsigned char count;
469 if (!(port = rc_get_port(bp, "Transmit")))
470 return;
472 tty = port->tty;
474 if (port->IER & IER_TXEMPTY) {
475 /* FIFO drained */
476 rc_out(bp, CD180_CAR, port_No(port));
477 port->IER &= ~IER_TXEMPTY;
478 rc_out(bp, CD180_IER, port->IER);
479 return;
482 if ((port->xmit_cnt <= 0 && !port->break_length)
483 || tty->stopped || tty->hw_stopped) {
484 rc_out(bp, CD180_CAR, port_No(port));
485 port->IER &= ~IER_TXRDY;
486 rc_out(bp, CD180_IER, port->IER);
487 return;
490 if (port->break_length) {
491 if (port->break_length > 0) {
492 if (port->COR2 & COR2_ETC) {
493 rc_out(bp, CD180_TDR, CD180_C_ESC);
494 rc_out(bp, CD180_TDR, CD180_C_SBRK);
495 port->COR2 &= ~COR2_ETC;
497 count = MIN(port->break_length, 0xff);
498 rc_out(bp, CD180_TDR, CD180_C_ESC);
499 rc_out(bp, CD180_TDR, CD180_C_DELAY);
500 rc_out(bp, CD180_TDR, count);
501 if (!(port->break_length -= count))
502 port->break_length--;
503 } else {
504 rc_out(bp, CD180_TDR, CD180_C_ESC);
505 rc_out(bp, CD180_TDR, CD180_C_EBRK);
506 rc_out(bp, CD180_COR2, port->COR2);
507 rc_wait_CCR(bp);
508 rc_out(bp, CD180_CCR, CCR_CORCHG2);
509 port->break_length = 0;
511 return;
514 count = CD180_NFIFO;
515 do {
516 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
517 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
518 if (--port->xmit_cnt <= 0)
519 break;
520 } while (--count > 0);
522 if (port->xmit_cnt <= 0) {
523 rc_out(bp, CD180_CAR, port_No(port));
524 port->IER &= ~IER_TXRDY;
525 rc_out(bp, CD180_IER, port->IER);
527 if (port->xmit_cnt <= port->wakeup_chars)
528 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
531 static inline void rc_check_modem(struct riscom_board const * bp)
533 struct riscom_port *port;
534 struct tty_struct *tty;
535 unsigned char mcr;
537 if (!(port = rc_get_port(bp, "Modem")))
538 return;
540 tty = port->tty;
542 mcr = rc_in(bp, CD180_MCR);
543 if (mcr & MCR_CDCHG) {
544 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
545 wake_up_interruptible(&port->open_wait);
546 else
547 schedule_task(&port->tqueue_hangup);
550 #ifdef RISCOM_BRAIN_DAMAGED_CTS
551 if (mcr & MCR_CTSCHG) {
552 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
553 tty->hw_stopped = 0;
554 port->IER |= IER_TXRDY;
555 if (port->xmit_cnt <= port->wakeup_chars)
556 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
557 } else {
558 tty->hw_stopped = 1;
559 port->IER &= ~IER_TXRDY;
561 rc_out(bp, CD180_IER, port->IER);
563 if (mcr & MCR_DSRCHG) {
564 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
565 tty->hw_stopped = 0;
566 port->IER |= IER_TXRDY;
567 if (port->xmit_cnt <= port->wakeup_chars)
568 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
569 } else {
570 tty->hw_stopped = 1;
571 port->IER &= ~IER_TXRDY;
573 rc_out(bp, CD180_IER, port->IER);
575 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
577 /* Clear change bits */
578 rc_out(bp, CD180_MCR, 0);
581 /* The main interrupt processing routine */
582 static irqreturn_t rc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
584 unsigned char status;
585 unsigned char ack;
586 struct riscom_board *bp;
587 unsigned long loop = 0;
588 int handled = 0;
590 bp = IRQ_to_board[irq];
592 if (!bp || !(bp->flags & RC_BOARD_ACTIVE)) {
593 return IRQ_NONE;
596 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
597 (RC_BSR_TOUT | RC_BSR_TINT |
598 RC_BSR_MINT | RC_BSR_RINT))) {
599 handled = 1;
600 if (status & RC_BSR_TOUT)
601 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
602 "error?\n", board_No(bp));
604 else if (status & RC_BSR_RINT) {
605 ack = rc_in(bp, RC_ACK_RINT);
607 if (ack == (RC_ID | GIVR_IT_RCV))
608 rc_receive(bp);
609 else if (ack == (RC_ID | GIVR_IT_REXC))
610 rc_receive_exc(bp);
611 else
612 printk(KERN_WARNING "rc%d: Bad receive ack "
613 "0x%02x.\n",
614 board_No(bp), ack);
616 } else if (status & RC_BSR_TINT) {
617 ack = rc_in(bp, RC_ACK_TINT);
619 if (ack == (RC_ID | GIVR_IT_TX))
620 rc_transmit(bp);
621 else
622 printk(KERN_WARNING "rc%d: Bad transmit ack "
623 "0x%02x.\n",
624 board_No(bp), ack);
626 } else /* if (status & RC_BSR_MINT) */ {
627 ack = rc_in(bp, RC_ACK_MINT);
629 if (ack == (RC_ID | GIVR_IT_MODEM))
630 rc_check_modem(bp);
631 else
632 printk(KERN_WARNING "rc%d: Bad modem ack "
633 "0x%02x.\n",
634 board_No(bp), ack);
638 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
639 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
641 return IRQ_RETVAL(handled);
645 * Routines for open & close processing.
648 /* Called with disabled interrupts */
649 static inline int rc_setup_board(struct riscom_board * bp)
651 int error;
653 if (bp->flags & RC_BOARD_ACTIVE)
654 return 0;
656 error = request_irq(bp->irq, rc_interrupt, SA_INTERRUPT,
657 "RISCom/8", NULL);
658 if (error)
659 return error;
661 rc_out(bp, RC_CTOUT, 0); /* Just in case */
662 bp->DTR = ~0;
663 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
665 IRQ_to_board[bp->irq] = bp;
666 bp->flags |= RC_BOARD_ACTIVE;
668 return 0;
671 /* Called with disabled interrupts */
672 static inline void rc_shutdown_board(struct riscom_board *bp)
674 if (!(bp->flags & RC_BOARD_ACTIVE))
675 return;
677 bp->flags &= ~RC_BOARD_ACTIVE;
679 free_irq(bp->irq, NULL);
680 IRQ_to_board[bp->irq] = NULL;
682 bp->DTR = ~0;
683 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
688 * Setting up port characteristics.
689 * Must be called with disabled interrupts
691 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
693 struct tty_struct *tty;
694 unsigned long baud;
695 long tmp;
696 unsigned char cor1 = 0, cor3 = 0;
697 unsigned char mcor1 = 0, mcor2 = 0;
699 if (!(tty = port->tty) || !tty->termios)
700 return;
702 port->IER = 0;
703 port->COR2 = 0;
704 port->MSVR = MSVR_RTS;
706 baud = C_BAUD(tty);
708 if (baud & CBAUDEX) {
709 baud &= ~CBAUDEX;
710 if (baud < 1 || baud > 2)
711 port->tty->termios->c_cflag &= ~CBAUDEX;
712 else
713 baud += 15;
715 if (baud == 15) {
716 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
717 baud ++;
718 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
719 baud += 2;
722 /* Select port on the board */
723 rc_out(bp, CD180_CAR, port_No(port));
725 if (!baud_table[baud]) {
726 /* Drop DTR & exit */
727 bp->DTR |= (1u << port_No(port));
728 rc_out(bp, RC_DTR, bp->DTR);
729 return;
730 } else {
731 /* Set DTR on */
732 bp->DTR &= ~(1u << port_No(port));
733 rc_out(bp, RC_DTR, bp->DTR);
737 * Now we must calculate some speed depended things
740 /* Set baud rate for port */
741 tmp = (((RC_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +
742 CD180_TPC/2) / CD180_TPC);
744 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
745 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
746 rc_out(bp, CD180_RBPRL, tmp & 0xff);
747 rc_out(bp, CD180_TBPRL, tmp & 0xff);
749 baud = (baud_table[baud] + 5) / 10; /* Estimated CPS */
751 /* Two timer ticks seems enough to wakeup something like SLIP driver */
752 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
753 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
754 SERIAL_XMIT_SIZE - 1 : tmp);
756 /* Receiver timeout will be transmission time for 1.5 chars */
757 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
758 tmp = (tmp > 0xff) ? 0xff : tmp;
759 rc_out(bp, CD180_RTPR, tmp);
761 switch (C_CSIZE(tty)) {
762 case CS5:
763 cor1 |= COR1_5BITS;
764 break;
765 case CS6:
766 cor1 |= COR1_6BITS;
767 break;
768 case CS7:
769 cor1 |= COR1_7BITS;
770 break;
771 case CS8:
772 cor1 |= COR1_8BITS;
773 break;
776 if (C_CSTOPB(tty))
777 cor1 |= COR1_2SB;
779 cor1 |= COR1_IGNORE;
780 if (C_PARENB(tty)) {
781 cor1 |= COR1_NORMPAR;
782 if (C_PARODD(tty))
783 cor1 |= COR1_ODDP;
784 if (I_INPCK(tty))
785 cor1 &= ~COR1_IGNORE;
787 /* Set marking of some errors */
788 port->mark_mask = RCSR_OE | RCSR_TOUT;
789 if (I_INPCK(tty))
790 port->mark_mask |= RCSR_FE | RCSR_PE;
791 if (I_BRKINT(tty) || I_PARMRK(tty))
792 port->mark_mask |= RCSR_BREAK;
793 if (I_IGNPAR(tty))
794 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
795 if (I_IGNBRK(tty)) {
796 port->mark_mask &= ~RCSR_BREAK;
797 if (I_IGNPAR(tty))
798 /* Real raw mode. Ignore all */
799 port->mark_mask &= ~RCSR_OE;
801 /* Enable Hardware Flow Control */
802 if (C_CRTSCTS(tty)) {
803 #ifdef RISCOM_BRAIN_DAMAGED_CTS
804 port->IER |= IER_DSR | IER_CTS;
805 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
806 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
807 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
808 #else
809 port->COR2 |= COR2_CTSAE;
810 #endif
812 /* Enable Software Flow Control. FIXME: I'm not sure about this */
813 /* Some people reported that it works, but I still doubt */
814 if (I_IXON(tty)) {
815 port->COR2 |= COR2_TXIBE;
816 cor3 |= (COR3_FCT | COR3_SCDE);
817 if (I_IXANY(tty))
818 port->COR2 |= COR2_IXM;
819 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
820 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
821 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
822 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
824 if (!C_CLOCAL(tty)) {
825 /* Enable CD check */
826 port->IER |= IER_CD;
827 mcor1 |= MCOR1_CDZD;
828 mcor2 |= MCOR2_CDOD;
831 if (C_CREAD(tty))
832 /* Enable receiver */
833 port->IER |= IER_RXD;
835 /* Set input FIFO size (1-8 bytes) */
836 cor3 |= RISCOM_RXFIFO;
837 /* Setting up CD180 channel registers */
838 rc_out(bp, CD180_COR1, cor1);
839 rc_out(bp, CD180_COR2, port->COR2);
840 rc_out(bp, CD180_COR3, cor3);
841 /* Make CD180 know about registers change */
842 rc_wait_CCR(bp);
843 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
844 /* Setting up modem option registers */
845 rc_out(bp, CD180_MCOR1, mcor1);
846 rc_out(bp, CD180_MCOR2, mcor2);
847 /* Enable CD180 transmitter & receiver */
848 rc_wait_CCR(bp);
849 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
850 /* Enable interrupts */
851 rc_out(bp, CD180_IER, port->IER);
852 /* And finally set RTS on */
853 rc_out(bp, CD180_MSVR, port->MSVR);
856 /* Must be called with interrupts enabled */
857 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
859 unsigned long flags;
861 if (port->flags & ASYNC_INITIALIZED)
862 return 0;
864 if (!port->xmit_buf) {
865 /* We may sleep in get_zeroed_page() */
866 unsigned long tmp;
868 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
869 return -ENOMEM;
871 if (port->xmit_buf) {
872 free_page(tmp);
873 return -ERESTARTSYS;
875 port->xmit_buf = (unsigned char *) tmp;
878 save_flags(flags); cli();
880 if (port->tty)
881 clear_bit(TTY_IO_ERROR, &port->tty->flags);
883 if (port->count == 1)
884 bp->count++;
886 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
887 rc_change_speed(bp, port);
888 port->flags |= ASYNC_INITIALIZED;
890 restore_flags(flags);
891 return 0;
894 /* Must be called with interrupts disabled */
895 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
897 struct tty_struct *tty;
899 if (!(port->flags & ASYNC_INITIALIZED))
900 return;
902 #ifdef RC_REPORT_OVERRUN
903 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
904 board_No(bp), port_No(port), port->overrun);
905 #endif
906 #ifdef RC_REPORT_FIFO
908 int i;
910 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
911 board_No(bp), port_No(port));
912 for (i = 0; i < 10; i++) {
913 printk("%ld ", port->hits[i]);
915 printk("].\n");
917 #endif
918 if (port->xmit_buf) {
919 free_page((unsigned long) port->xmit_buf);
920 port->xmit_buf = NULL;
923 if (!(tty = port->tty) || C_HUPCL(tty)) {
924 /* Drop DTR */
925 bp->DTR |= (1u << port_No(port));
926 rc_out(bp, RC_DTR, bp->DTR);
929 /* Select port */
930 rc_out(bp, CD180_CAR, port_No(port));
931 /* Reset port */
932 rc_wait_CCR(bp);
933 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
934 /* Disable all interrupts from this port */
935 port->IER = 0;
936 rc_out(bp, CD180_IER, port->IER);
938 if (tty)
939 set_bit(TTY_IO_ERROR, &tty->flags);
940 port->flags &= ~ASYNC_INITIALIZED;
942 if (--bp->count < 0) {
943 printk(KERN_INFO "rc%d: rc_shutdown_port: "
944 "bad board count: %d\n",
945 board_No(bp), bp->count);
946 bp->count = 0;
950 * If this is the last opened port on the board
951 * shutdown whole board
953 if (!bp->count)
954 rc_shutdown_board(bp);
958 static int block_til_ready(struct tty_struct *tty, struct file * filp,
959 struct riscom_port *port)
961 DECLARE_WAITQUEUE(wait, current);
962 struct riscom_board *bp = port_Board(port);
963 int retval;
964 int do_clocal = 0;
965 int CD;
968 * If the device is in the middle of being closed, then block
969 * until it's done, and then try again.
971 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
972 interruptible_sleep_on(&port->close_wait);
973 if (port->flags & ASYNC_HUP_NOTIFY)
974 return -EAGAIN;
975 else
976 return -ERESTARTSYS;
980 * If non-blocking mode is set, or the port is not enabled,
981 * then make the check up front and then exit.
983 if ((filp->f_flags & O_NONBLOCK) ||
984 (tty->flags & (1 << TTY_IO_ERROR))) {
985 port->flags |= ASYNC_NORMAL_ACTIVE;
986 return 0;
989 if (C_CLOCAL(tty))
990 do_clocal = 1;
993 * Block waiting for the carrier detect and the line to become
994 * free (i.e., not in use by the callout). While we are in
995 * this loop, info->count is dropped by one, so that
996 * rs_close() knows when to free things. We restore it upon
997 * exit, either normal or abnormal.
999 retval = 0;
1000 add_wait_queue(&port->open_wait, &wait);
1001 cli();
1002 if (!tty_hung_up_p(filp))
1003 port->count--;
1004 sti();
1005 port->blocked_open++;
1006 while (1) {
1007 cli();
1008 rc_out(bp, CD180_CAR, port_No(port));
1009 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
1010 rc_out(bp, CD180_MSVR, MSVR_RTS);
1011 bp->DTR &= ~(1u << port_No(port));
1012 rc_out(bp, RC_DTR, bp->DTR);
1013 sti();
1014 set_current_state(TASK_INTERRUPTIBLE);
1015 if (tty_hung_up_p(filp) ||
1016 !(port->flags & ASYNC_INITIALIZED)) {
1017 if (port->flags & ASYNC_HUP_NOTIFY)
1018 retval = -EAGAIN;
1019 else
1020 retval = -ERESTARTSYS;
1021 break;
1023 if (!(port->flags & ASYNC_CLOSING) &&
1024 (do_clocal || CD))
1025 break;
1026 if (signal_pending(current)) {
1027 retval = -ERESTARTSYS;
1028 break;
1030 schedule();
1032 current->state = TASK_RUNNING;
1033 remove_wait_queue(&port->open_wait, &wait);
1034 if (!tty_hung_up_p(filp))
1035 port->count++;
1036 port->blocked_open--;
1037 if (retval)
1038 return retval;
1040 port->flags |= ASYNC_NORMAL_ACTIVE;
1041 return 0;
1044 static int rc_open(struct tty_struct * tty, struct file * filp)
1046 int board;
1047 int error;
1048 struct riscom_port * port;
1049 struct riscom_board * bp;
1050 unsigned long flags;
1052 board = RC_BOARD(tty->index);
1053 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
1054 return -ENODEV;
1056 bp = &rc_board[board];
1057 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
1058 if (rc_paranoia_check(port, tty->name, "rc_open"))
1059 return -ENODEV;
1061 if ((error = rc_setup_board(bp)))
1062 return error;
1064 port->count++;
1065 tty->driver_data = port;
1066 port->tty = tty;
1068 if ((error = rc_setup_port(bp, port)))
1069 return error;
1071 if ((error = block_til_ready(tty, filp, port)))
1072 return error;
1074 return 0;
1077 static void rc_close(struct tty_struct * tty, struct file * filp)
1079 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1080 struct riscom_board *bp;
1081 unsigned long flags;
1082 unsigned long timeout;
1084 if (!port || rc_paranoia_check(port, tty->name, "close"))
1085 return;
1087 save_flags(flags); cli();
1088 if (tty_hung_up_p(filp))
1089 goto out;
1091 bp = port_Board(port);
1092 if ((tty->count == 1) && (port->count != 1)) {
1093 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1094 " tty->count is 1, port count is %d\n",
1095 board_No(bp), port->count);
1096 port->count = 1;
1098 if (--port->count < 0) {
1099 printk(KERN_INFO "rc%d: rc_close: bad port count "
1100 "for tty%d: %d\n",
1101 board_No(bp), port_No(port), port->count);
1102 port->count = 0;
1104 if (port->count)
1105 goto out;
1106 port->flags |= ASYNC_CLOSING;
1108 * Now we wait for the transmit buffer to clear; and we notify
1109 * the line discipline to only process XON/XOFF characters.
1111 tty->closing = 1;
1112 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1113 tty_wait_until_sent(tty, port->closing_wait);
1115 * At this point we stop accepting input. To do this, we
1116 * disable the receive line status interrupts, and tell the
1117 * interrupt driver to stop checking the data ready bit in the
1118 * line status register.
1120 port->IER &= ~IER_RXD;
1121 if (port->flags & ASYNC_INITIALIZED) {
1122 port->IER &= ~IER_TXRDY;
1123 port->IER |= IER_TXEMPTY;
1124 rc_out(bp, CD180_CAR, port_No(port));
1125 rc_out(bp, CD180_IER, port->IER);
1127 * Before we drop DTR, make sure the UART transmitter
1128 * has completely drained; this is especially
1129 * important if there is a transmit FIFO!
1131 timeout = jiffies+HZ;
1132 while(port->IER & IER_TXEMPTY) {
1133 current->state = TASK_INTERRUPTIBLE;
1134 schedule_timeout(port->timeout);
1135 if (time_after(jiffies, timeout))
1136 break;
1139 rc_shutdown_port(bp, port);
1140 if (tty->driver->flush_buffer)
1141 tty->driver->flush_buffer(tty);
1142 if (tty->ldisc.flush_buffer)
1143 tty->ldisc.flush_buffer(tty);
1144 tty->closing = 0;
1145 port->event = 0;
1146 port->tty = 0;
1147 if (port->blocked_open) {
1148 if (port->close_delay) {
1149 current->state = TASK_INTERRUPTIBLE;
1150 schedule_timeout(port->close_delay);
1152 wake_up_interruptible(&port->open_wait);
1154 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1155 wake_up_interruptible(&port->close_wait);
1156 out: restore_flags(flags);
1159 static int rc_write(struct tty_struct * tty, int from_user,
1160 const unsigned char *buf, int count)
1162 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1163 struct riscom_board *bp;
1164 int c, total = 0;
1165 unsigned long flags;
1167 if (rc_paranoia_check(port, tty->name, "rc_write"))
1168 return 0;
1170 bp = port_Board(port);
1172 if (!tty || !port->xmit_buf || !tmp_buf)
1173 return 0;
1175 save_flags(flags);
1176 if (from_user) {
1177 down(&tmp_buf_sem);
1178 while (1) {
1179 cli();
1180 c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1181 SERIAL_XMIT_SIZE - port->xmit_head));
1182 if (c <= 0)
1183 break;
1185 c -= copy_from_user(tmp_buf, buf, c);
1186 if (!c) {
1187 if (!total)
1188 total = -EFAULT;
1189 break;
1192 cli();
1193 c = MIN(c, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1194 SERIAL_XMIT_SIZE - port->xmit_head));
1195 memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1196 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1197 port->xmit_cnt += c;
1198 restore_flags(flags);
1200 buf += c;
1201 count -= c;
1202 total += c;
1204 up(&tmp_buf_sem);
1205 } else {
1206 while (1) {
1207 cli();
1208 c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1209 SERIAL_XMIT_SIZE - port->xmit_head));
1210 if (c <= 0) {
1211 restore_flags(flags);
1212 break;
1215 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1216 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1217 port->xmit_cnt += c;
1218 restore_flags(flags);
1220 buf += c;
1221 count -= c;
1222 total += c;
1226 cli();
1227 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1228 !(port->IER & IER_TXRDY)) {
1229 port->IER |= IER_TXRDY;
1230 rc_out(bp, CD180_CAR, port_No(port));
1231 rc_out(bp, CD180_IER, port->IER);
1233 restore_flags(flags);
1235 return total;
1238 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1240 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1241 unsigned long flags;
1243 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1244 return;
1246 if (!tty || !port->xmit_buf)
1247 return;
1249 save_flags(flags); cli();
1251 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1252 goto out;
1254 port->xmit_buf[port->xmit_head++] = ch;
1255 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1256 port->xmit_cnt++;
1257 out: restore_flags(flags);
1260 static void rc_flush_chars(struct tty_struct * tty)
1262 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1263 unsigned long flags;
1265 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1266 return;
1268 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1269 !port->xmit_buf)
1270 return;
1272 save_flags(flags); cli();
1273 port->IER |= IER_TXRDY;
1274 rc_out(port_Board(port), CD180_CAR, port_No(port));
1275 rc_out(port_Board(port), CD180_IER, port->IER);
1276 restore_flags(flags);
1279 static int rc_write_room(struct tty_struct * tty)
1281 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1282 int ret;
1284 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1285 return 0;
1287 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1288 if (ret < 0)
1289 ret = 0;
1290 return ret;
1293 static int rc_chars_in_buffer(struct tty_struct *tty)
1295 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1297 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1298 return 0;
1300 return port->xmit_cnt;
1303 static void rc_flush_buffer(struct tty_struct *tty)
1305 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1306 unsigned long flags;
1308 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1309 return;
1311 save_flags(flags); cli();
1312 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1313 restore_flags(flags);
1315 wake_up_interruptible(&tty->write_wait);
1316 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1317 tty->ldisc.write_wakeup)
1318 (tty->ldisc.write_wakeup)(tty);
1321 static int rc_get_modem_info(struct riscom_port * port, unsigned int *value)
1323 struct riscom_board * bp;
1324 unsigned char status;
1325 unsigned int result;
1326 unsigned long flags;
1328 bp = port_Board(port);
1329 save_flags(flags); cli();
1330 rc_out(bp, CD180_CAR, port_No(port));
1331 status = rc_in(bp, CD180_MSVR);
1332 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1333 restore_flags(flags);
1334 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1335 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1336 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1337 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1338 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1339 return put_user(result, value);
1342 static int rc_set_modem_info(struct riscom_port * port, unsigned int cmd,
1343 unsigned int *value)
1345 unsigned int arg;
1346 unsigned long flags;
1347 struct riscom_board *bp = port_Board(port);
1349 if (get_user(arg, value))
1350 return -EFAULT;
1351 switch (cmd) {
1352 case TIOCMBIS:
1353 if (arg & TIOCM_RTS)
1354 port->MSVR |= MSVR_RTS;
1355 if (arg & TIOCM_DTR)
1356 bp->DTR &= ~(1u << port_No(port));
1357 break;
1358 case TIOCMBIC:
1359 if (arg & TIOCM_RTS)
1360 port->MSVR &= ~MSVR_RTS;
1361 if (arg & TIOCM_DTR)
1362 bp->DTR |= (1u << port_No(port));
1363 break;
1364 case TIOCMSET:
1365 port->MSVR = (arg & TIOCM_RTS) ? (port->MSVR | MSVR_RTS) :
1366 (port->MSVR & ~MSVR_RTS);
1367 bp->DTR = arg & TIOCM_DTR ? (bp->DTR &= ~(1u << port_No(port))) :
1368 (bp->DTR |= (1u << port_No(port)));
1369 break;
1370 default:
1371 return -EINVAL;
1373 save_flags(flags); cli();
1374 rc_out(bp, CD180_CAR, port_No(port));
1375 rc_out(bp, CD180_MSVR, port->MSVR);
1376 rc_out(bp, RC_DTR, bp->DTR);
1377 restore_flags(flags);
1378 return 0;
1381 static inline void rc_send_break(struct riscom_port * port, unsigned long length)
1383 struct riscom_board *bp = port_Board(port);
1384 unsigned long flags;
1386 save_flags(flags); cli();
1387 port->break_length = RISCOM_TPS / HZ * length;
1388 port->COR2 |= COR2_ETC;
1389 port->IER |= IER_TXRDY;
1390 rc_out(bp, CD180_CAR, port_No(port));
1391 rc_out(bp, CD180_COR2, port->COR2);
1392 rc_out(bp, CD180_IER, port->IER);
1393 rc_wait_CCR(bp);
1394 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1395 rc_wait_CCR(bp);
1396 restore_flags(flags);
1399 static inline int rc_set_serial_info(struct riscom_port * port,
1400 struct serial_struct * newinfo)
1402 struct serial_struct tmp;
1403 struct riscom_board *bp = port_Board(port);
1404 int change_speed;
1405 unsigned long flags;
1407 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1408 return -EFAULT;
1410 #if 0
1411 if ((tmp.irq != bp->irq) ||
1412 (tmp.port != bp->base) ||
1413 (tmp.type != PORT_CIRRUS) ||
1414 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1415 (tmp.custom_divisor != 0) ||
1416 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1417 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1418 return -EINVAL;
1419 #endif
1421 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1422 (tmp.flags & ASYNC_SPD_MASK));
1424 if (!capable(CAP_SYS_ADMIN)) {
1425 if ((tmp.close_delay != port->close_delay) ||
1426 (tmp.closing_wait != port->closing_wait) ||
1427 ((tmp.flags & ~ASYNC_USR_MASK) !=
1428 (port->flags & ~ASYNC_USR_MASK)))
1429 return -EPERM;
1430 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1431 (tmp.flags & ASYNC_USR_MASK));
1432 } else {
1433 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1434 (tmp.flags & ASYNC_FLAGS));
1435 port->close_delay = tmp.close_delay;
1436 port->closing_wait = tmp.closing_wait;
1438 if (change_speed) {
1439 save_flags(flags); cli();
1440 rc_change_speed(bp, port);
1441 restore_flags(flags);
1443 return 0;
1446 static inline int rc_get_serial_info(struct riscom_port * port,
1447 struct serial_struct * retinfo)
1449 struct serial_struct tmp;
1450 struct riscom_board *bp = port_Board(port);
1452 memset(&tmp, 0, sizeof(tmp));
1453 tmp.type = PORT_CIRRUS;
1454 tmp.line = port - rc_port;
1455 tmp.port = bp->base;
1456 tmp.irq = bp->irq;
1457 tmp.flags = port->flags;
1458 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1459 tmp.close_delay = port->close_delay * HZ/100;
1460 tmp.closing_wait = port->closing_wait * HZ/100;
1461 tmp.xmit_fifo_size = CD180_NFIFO;
1462 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1465 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1466 unsigned int cmd, unsigned long arg)
1469 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1470 int retval;
1472 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1473 return -ENODEV;
1475 switch (cmd) {
1476 case TCSBRK: /* SVID version: non-zero arg --> no break */
1477 retval = tty_check_change(tty);
1478 if (retval)
1479 return retval;
1480 tty_wait_until_sent(tty, 0);
1481 if (!arg)
1482 rc_send_break(port, HZ/4); /* 1/4 second */
1483 break;
1484 case TCSBRKP: /* support for POSIX tcsendbreak() */
1485 retval = tty_check_change(tty);
1486 if (retval)
1487 return retval;
1488 tty_wait_until_sent(tty, 0);
1489 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1490 break;
1491 case TIOCGSOFTCAR:
1492 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned int *) arg);
1493 case TIOCSSOFTCAR:
1494 if (get_user(arg,(unsigned int *) arg))
1495 return -EFAULT;
1496 tty->termios->c_cflag =
1497 ((tty->termios->c_cflag & ~CLOCAL) |
1498 (arg ? CLOCAL : 0));
1499 break;
1500 case TIOCMGET:
1501 return rc_get_modem_info(port, (unsigned int *) arg);
1502 case TIOCMBIS:
1503 case TIOCMBIC:
1504 case TIOCMSET:
1505 return rc_set_modem_info(port, cmd, (unsigned int *) arg);
1506 case TIOCGSERIAL:
1507 return rc_get_serial_info(port, (struct serial_struct *) arg);
1508 case TIOCSSERIAL:
1509 return rc_set_serial_info(port, (struct serial_struct *) arg);
1510 default:
1511 return -ENOIOCTLCMD;
1513 return 0;
1516 static void rc_throttle(struct tty_struct * tty)
1518 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1519 struct riscom_board *bp;
1520 unsigned long flags;
1522 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1523 return;
1525 bp = port_Board(port);
1527 save_flags(flags); cli();
1528 port->MSVR &= ~MSVR_RTS;
1529 rc_out(bp, CD180_CAR, port_No(port));
1530 if (I_IXOFF(tty)) {
1531 rc_wait_CCR(bp);
1532 rc_out(bp, CD180_CCR, CCR_SSCH2);
1533 rc_wait_CCR(bp);
1535 rc_out(bp, CD180_MSVR, port->MSVR);
1536 restore_flags(flags);
1539 static void rc_unthrottle(struct tty_struct * tty)
1541 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1542 struct riscom_board *bp;
1543 unsigned long flags;
1545 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1546 return;
1548 bp = port_Board(port);
1550 save_flags(flags); cli();
1551 port->MSVR |= MSVR_RTS;
1552 rc_out(bp, CD180_CAR, port_No(port));
1553 if (I_IXOFF(tty)) {
1554 rc_wait_CCR(bp);
1555 rc_out(bp, CD180_CCR, CCR_SSCH1);
1556 rc_wait_CCR(bp);
1558 rc_out(bp, CD180_MSVR, port->MSVR);
1559 restore_flags(flags);
1562 static void rc_stop(struct tty_struct * tty)
1564 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1565 struct riscom_board *bp;
1566 unsigned long flags;
1568 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1569 return;
1571 bp = port_Board(port);
1573 save_flags(flags); cli();
1574 port->IER &= ~IER_TXRDY;
1575 rc_out(bp, CD180_CAR, port_No(port));
1576 rc_out(bp, CD180_IER, port->IER);
1577 restore_flags(flags);
1580 static void rc_start(struct tty_struct * tty)
1582 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1583 struct riscom_board *bp;
1584 unsigned long flags;
1586 if (rc_paranoia_check(port, tty->name, "rc_start"))
1587 return;
1589 bp = port_Board(port);
1591 save_flags(flags); cli();
1592 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1593 port->IER |= IER_TXRDY;
1594 rc_out(bp, CD180_CAR, port_No(port));
1595 rc_out(bp, CD180_IER, port->IER);
1597 restore_flags(flags);
1601 * This routine is called from the scheduler tqueue when the interrupt
1602 * routine has signalled that a hangup has occurred. The path of
1603 * hangup processing is:
1605 * serial interrupt routine -> (scheduler tqueue) ->
1606 * do_rc_hangup() -> tty->hangup() -> rc_hangup()
1609 static void do_rc_hangup(void *private_)
1611 struct riscom_port *port = (struct riscom_port *) private_;
1612 struct tty_struct *tty;
1614 tty = port->tty;
1615 if (tty)
1616 tty_hangup(tty); /* FIXME: module removal race still here */
1619 static void rc_hangup(struct tty_struct * tty)
1621 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1622 struct riscom_board *bp;
1624 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1625 return;
1627 bp = port_Board(port);
1629 rc_shutdown_port(bp, port);
1630 port->event = 0;
1631 port->count = 0;
1632 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1633 port->tty = 0;
1634 wake_up_interruptible(&port->open_wait);
1637 static void rc_set_termios(struct tty_struct * tty, struct termios * old_termios)
1639 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1640 unsigned long flags;
1642 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1643 return;
1645 if (tty->termios->c_cflag == old_termios->c_cflag &&
1646 tty->termios->c_iflag == old_termios->c_iflag)
1647 return;
1649 save_flags(flags); cli();
1650 rc_change_speed(port_Board(port), port);
1651 restore_flags(flags);
1653 if ((old_termios->c_cflag & CRTSCTS) &&
1654 !(tty->termios->c_cflag & CRTSCTS)) {
1655 tty->hw_stopped = 0;
1656 rc_start(tty);
1660 static void do_riscom_bh(void)
1662 run_task_queue(&tq_riscom);
1665 static void do_softint(void *private_)
1667 struct riscom_port *port = (struct riscom_port *) private_;
1668 struct tty_struct *tty;
1670 if(!(tty = port->tty))
1671 return;
1673 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
1674 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1675 tty->ldisc.write_wakeup)
1676 (tty->ldisc.write_wakeup)(tty);
1677 wake_up_interruptible(&tty->write_wait);
1681 static struct tty_operations riscom_ops = {
1682 .open = rc_open,
1683 .close = rc_close,
1684 .write = rc_write,
1685 .put_char = rc_put_char,
1686 .flush_chars = rc_flush_chars,
1687 .write_room = rc_write_room,
1688 .chars_in_buffer = rc_chars_in_buffer,
1689 .flush_buffer = rc_flush_buffer,
1690 .ioctl = rc_ioctl,
1691 .throttle = rc_throttle,
1692 .unthrottle = rc_unthrottle,
1693 .set_termios = rc_set_termios,
1694 .stop = rc_stop,
1695 .start = rc_start,
1696 .hangup = rc_hangup,
1699 static inline int rc_init_drivers(void)
1701 int error;
1702 int i;
1704 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1705 if (!riscom_driver)
1706 return -ENOMEM;
1708 if (!(tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL))) {
1709 printk(KERN_ERR "rc: Couldn't get free page.\n");
1710 put_tty_driver(riscom_driver);
1711 return 1;
1713 init_bh(RISCOM8_BH, do_riscom_bh);
1714 memset(IRQ_to_board, 0, sizeof(IRQ_to_board));
1715 riscom_driver->owner = THIS_MODULE;
1716 riscom_driver->name = "ttyL";
1717 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1718 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1719 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1720 riscom_driver->init_termios = tty_std_termios;
1721 riscom_driver->init_termios.c_cflag =
1722 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1723 riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1724 tty_set_operations(riscom_driver, &riscom_ops);
1725 if ((error = tty_register_driver(riscom_driver))) {
1726 free_page((unsigned long)tmp_buf);
1727 put_tty_driver(riscom_driver);
1728 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1729 "error = %d\n",
1730 error);
1731 return 1;
1734 memset(rc_port, 0, sizeof(rc_port));
1735 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1736 rc_port[i].magic = RISCOM8_MAGIC;
1737 rc_port[i].tqueue.routine = do_softint;
1738 rc_port[i].tqueue.data = &rc_port[i];
1739 rc_port[i].tqueue_hangup.routine = do_rc_hangup;
1740 rc_port[i].tqueue_hangup.data = &rc_port[i];
1741 rc_port[i].close_delay = 50 * HZ/100;
1742 rc_port[i].closing_wait = 3000 * HZ/100;
1743 init_waitqueue_head(&rc_port[i].open_wait);
1744 init_waitqueue_head(&rc_port[i].close_wait);
1747 return 0;
1750 static void rc_release_drivers(void)
1752 unsigned long flags;
1754 save_flags(flags);
1755 cli();
1756 remove_bh(RISCOM8_BH);
1757 free_page((unsigned long)tmp_buf);
1758 tty_unregister_driver(riscom_driver);
1759 put_tty_driver(riscom_driver);
1760 restore_flags(flags);
1763 #ifndef MODULE
1765 * Called at boot time.
1767 * You can specify IO base for up to RC_NBOARD cards,
1768 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1769 * Note that there will be no probing at default
1770 * addresses in this case.
1773 static int __init riscom8_setup(char *str)
1775 int ints[RC_NBOARD];
1776 int i;
1778 str = get_options(str, ARRAY_SIZE(ints), ints);
1780 for (i = 0; i < RC_NBOARD; i++) {
1781 if (i < ints[0])
1782 rc_board[i].base = ints[i+1];
1783 else
1784 rc_board[i].base = 0;
1786 return 1;
1789 __setup("riscom8=", riscom8_setup);
1790 #endif
1792 static char banner[] __initdata =
1793 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1794 "1994-1996.\n";
1795 static char no_boards_msg[] __initdata =
1796 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1799 * This routine must be called by kernel at boot time
1801 static int __init riscom8_init(void)
1803 int i;
1804 int found = 0;
1806 printk(banner);
1808 if (rc_init_drivers())
1809 return -EIO;
1811 for (i = 0; i < RC_NBOARD; i++)
1812 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1813 found++;
1815 if (!found) {
1816 rc_release_drivers();
1817 printk(no_boards_msg);
1818 return -EIO;
1820 return 0;
1823 #ifdef MODULE
1824 static int iobase;
1825 static int iobase1;
1826 static int iobase2;
1827 static int iobase3;
1828 MODULE_PARM(iobase, "i");
1829 MODULE_PARM(iobase1, "i");
1830 MODULE_PARM(iobase2, "i");
1831 MODULE_PARM(iobase3, "i");
1833 MODULE_LICENSE("GPL");
1834 #endif /* MODULE */
1837 * You can setup up to 4 boards (current value of RC_NBOARD)
1838 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1841 static int __init riscom8_init_module (void)
1843 #ifdef MODULE
1844 int i;
1846 if (iobase || iobase1 || iobase2 || iobase3) {
1847 for(i = 0; i < RC_NBOARD; i++)
1848 rc_board[0].base = 0;
1851 if (iobase)
1852 rc_board[0].base = iobase;
1853 if (iobase1)
1854 rc_board[1].base = iobase1;
1855 if (iobase2)
1856 rc_board[2].base = iobase2;
1857 if (iobase3)
1858 rc_board[3].base = iobase3;
1859 #endif /* MODULE */
1861 return riscom8_init();
1864 static void __exit riscom8_exit_module (void)
1866 int i;
1868 rc_release_drivers();
1869 for (i = 0; i < RC_NBOARD; i++)
1870 if (rc_board[i].flags & RC_BOARD_PRESENT)
1871 rc_release_io_range(&rc_board[i]);
1875 module_init(riscom8_init_module);
1876 module_exit(riscom8_exit_module);