Char: riscom8, change rc_init_drivers prototype
[linux-2.6/linux-loongson.git] / drivers / char / riscom8.c
blob9b19abf29a2b1ad1f22db65d31a3524f4c98d777
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>
50 #include <linux/spinlock.h>
52 #include <asm/uaccess.h>
54 #include "riscom8.h"
55 #include "riscom8_reg.h"
57 /* Am I paranoid or not ? ;-) */
58 #define RISCOM_PARANOIA_CHECK
60 /*
61 * Crazy InteliCom/8 boards sometimes has swapped CTS & DSR signals.
62 * You can slightly speed up things by #undefing the following option,
63 * if you are REALLY sure that your board is correct one.
66 #define RISCOM_BRAIN_DAMAGED_CTS
68 /*
69 * The following defines are mostly for testing purposes. But if you need
70 * some nice reporting in your syslog, you can define them also.
72 #undef RC_REPORT_FIFO
73 #undef RC_REPORT_OVERRUN
76 #define RISCOM_LEGAL_FLAGS \
77 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
78 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
79 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
81 #define RS_EVENT_WRITE_WAKEUP 0
83 static struct tty_driver *riscom_driver;
85 static DEFINE_SPINLOCK(riscom_lock);
87 static struct riscom_board rc_board[RC_NBOARD] = {
89 .base = RC_IOBASE1,
92 .base = RC_IOBASE2,
95 .base = RC_IOBASE3,
98 .base = RC_IOBASE4,
102 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
104 /* RISCom/8 I/O ports addresses (without address translation) */
105 static unsigned short rc_ioport[] = {
106 #if 1
107 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
108 #else
109 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
110 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
111 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
112 #endif
114 #define RC_NIOPORT ARRAY_SIZE(rc_ioport)
117 static inline int rc_paranoia_check(struct riscom_port const * port,
118 char *name, const char *routine)
120 #ifdef RISCOM_PARANOIA_CHECK
121 static const char badmagic[] = KERN_INFO
122 "rc: Warning: bad riscom port magic number for device %s in %s\n";
123 static const char badinfo[] = KERN_INFO
124 "rc: Warning: null riscom port for device %s in %s\n";
126 if (!port) {
127 printk(badinfo, name, routine);
128 return 1;
130 if (port->magic != RISCOM8_MAGIC) {
131 printk(badmagic, name, routine);
132 return 1;
134 #endif
135 return 0;
140 * Service functions for RISCom/8 driver.
144 /* Get board number from pointer */
145 static inline int board_No (struct riscom_board const * bp)
147 return bp - rc_board;
150 /* Get port number from pointer */
151 static inline int port_No (struct riscom_port const * port)
153 return RC_PORT(port - rc_port);
156 /* Get pointer to board from pointer to port */
157 static inline struct riscom_board * port_Board(struct riscom_port const * port)
159 return &rc_board[RC_BOARD(port - rc_port)];
162 /* Input Byte from CL CD180 register */
163 static inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
165 return inb(bp->base + RC_TO_ISA(reg));
168 /* Output Byte to CL CD180 register */
169 static inline void rc_out(struct riscom_board const * bp, unsigned short reg,
170 unsigned char val)
172 outb(val, bp->base + RC_TO_ISA(reg));
175 /* Wait for Channel Command Register ready */
176 static inline void rc_wait_CCR(struct riscom_board const * bp)
178 unsigned long delay;
180 /* FIXME: need something more descriptive then 100000 :) */
181 for (delay = 100000; delay; delay--)
182 if (!rc_in(bp, CD180_CCR))
183 return;
185 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
189 * RISCom/8 probe functions.
192 static inline int rc_request_io_range(struct riscom_board * const bp)
194 int i;
196 for (i = 0; i < RC_NIOPORT; i++)
197 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
198 "RISCom/8")) {
199 goto out_release;
201 return 0;
202 out_release:
203 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
204 board_No(bp), bp->base);
205 while(--i >= 0)
206 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
207 return 1;
210 static inline void rc_release_io_range(struct riscom_board * const bp)
212 int i;
214 for (i = 0; i < RC_NIOPORT; i++)
215 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
218 /* Reset and setup CD180 chip */
219 static void __init rc_init_CD180(struct riscom_board const * bp)
221 unsigned long flags;
223 spin_lock_irqsave(&riscom_lock, flags);
225 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
226 rc_wait_CCR(bp); /* Wait for CCR ready */
227 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
228 spin_unlock_irqrestore(&riscom_lock, flags);
229 msleep(50); /* Delay 0.05 sec */
230 spin_lock_irqsave(&riscom_lock, flags);
231 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
232 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
233 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
234 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for transmitter intr */
235 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for receiver intr */
237 /* Setting up prescaler. We need 4 ticks per 1 ms */
238 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
239 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
241 spin_unlock_irqrestore(&riscom_lock, flags);
244 /* Main probing routine, also sets irq. */
245 static int __init rc_probe(struct riscom_board *bp)
247 unsigned char val1, val2;
248 int irqs = 0;
249 int retries;
251 bp->irq = 0;
253 if (rc_request_io_range(bp))
254 return 1;
256 /* Are the I/O ports here ? */
257 rc_out(bp, CD180_PPRL, 0x5a);
258 outb(0xff, 0x80);
259 val1 = rc_in(bp, CD180_PPRL);
260 rc_out(bp, CD180_PPRL, 0xa5);
261 outb(0x00, 0x80);
262 val2 = rc_in(bp, CD180_PPRL);
264 if ((val1 != 0x5a) || (val2 != 0xa5)) {
265 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
266 board_No(bp), bp->base);
267 goto out_release;
270 /* It's time to find IRQ for this board */
271 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
272 irqs = probe_irq_on();
273 rc_init_CD180(bp); /* Reset CD180 chip */
274 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
275 rc_wait_CCR(bp);
276 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
277 rc_out(bp, CD180_IER, IER_TXRDY); /* Enable tx empty intr */
278 msleep(50);
279 irqs = probe_irq_off(irqs);
280 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
281 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
282 rc_init_CD180(bp); /* Reset CD180 again */
284 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
285 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
286 "found.\n", board_No(bp), bp->base);
287 goto out_release;
291 if (irqs <= 0) {
292 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
293 "at 0x%03x.\n", board_No(bp), bp->base);
294 goto out_release;
296 bp->irq = irqs;
297 bp->flags |= RC_BOARD_PRESENT;
299 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
300 "0x%03x, IRQ %d.\n",
301 board_No(bp),
302 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
303 bp->base, bp->irq);
305 return 0;
306 out_release:
307 rc_release_io_range(bp);
308 return 1;
313 * Interrupt processing routines.
317 static inline void rc_mark_event(struct riscom_port * port, int event)
319 set_bit(event, &port->event);
320 schedule_work(&port->tqueue);
323 static inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
324 unsigned char const * what)
326 unsigned char channel;
327 struct riscom_port * port;
329 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
330 if (channel < CD180_NCH) {
331 port = &rc_port[board_No(bp) * RC_NPORT + channel];
332 if (port->flags & ASYNC_INITIALIZED) {
333 return port;
336 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
337 board_No(bp), what, channel);
338 return NULL;
341 static inline void rc_receive_exc(struct riscom_board const * bp)
343 struct riscom_port *port;
344 struct tty_struct *tty;
345 unsigned char status;
346 unsigned char ch, flag;
348 if (!(port = rc_get_port(bp, "Receive")))
349 return;
351 tty = port->tty;
353 #ifdef RC_REPORT_OVERRUN
354 status = rc_in(bp, CD180_RCSR);
355 if (status & RCSR_OE)
356 port->overrun++;
357 status &= port->mark_mask;
358 #else
359 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
360 #endif
361 ch = rc_in(bp, CD180_RDR);
362 if (!status) {
363 return;
365 if (status & RCSR_TOUT) {
366 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
367 "Hardware problems ?\n",
368 board_No(bp), port_No(port));
369 return;
371 } else if (status & RCSR_BREAK) {
372 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
373 board_No(bp), port_No(port));
374 flag = TTY_BREAK;
375 if (port->flags & ASYNC_SAK)
376 do_SAK(tty);
378 } else if (status & RCSR_PE)
379 flag = TTY_PARITY;
381 else if (status & RCSR_FE)
382 flag = TTY_FRAME;
384 else if (status & RCSR_OE)
385 flag = TTY_OVERRUN;
387 else
388 flag = TTY_NORMAL;
390 tty_insert_flip_char(tty, ch, flag);
391 tty_flip_buffer_push(tty);
394 static inline void rc_receive(struct riscom_board const * bp)
396 struct riscom_port *port;
397 struct tty_struct *tty;
398 unsigned char count;
400 if (!(port = rc_get_port(bp, "Receive")))
401 return;
403 tty = port->tty;
405 count = rc_in(bp, CD180_RDCR);
407 #ifdef RC_REPORT_FIFO
408 port->hits[count > 8 ? 9 : count]++;
409 #endif
411 while (count--) {
412 if (tty_buffer_request_room(tty, 1) == 0) {
413 printk(KERN_WARNING "rc%d: port %d: Working around "
414 "flip buffer overflow.\n",
415 board_No(bp), port_No(port));
416 break;
418 tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
420 tty_flip_buffer_push(tty);
423 static inline void rc_transmit(struct riscom_board const * bp)
425 struct riscom_port *port;
426 struct tty_struct *tty;
427 unsigned char count;
430 if (!(port = rc_get_port(bp, "Transmit")))
431 return;
433 tty = port->tty;
435 if (port->IER & IER_TXEMPTY) {
436 /* FIFO drained */
437 rc_out(bp, CD180_CAR, port_No(port));
438 port->IER &= ~IER_TXEMPTY;
439 rc_out(bp, CD180_IER, port->IER);
440 return;
443 if ((port->xmit_cnt <= 0 && !port->break_length)
444 || tty->stopped || tty->hw_stopped) {
445 rc_out(bp, CD180_CAR, port_No(port));
446 port->IER &= ~IER_TXRDY;
447 rc_out(bp, CD180_IER, port->IER);
448 return;
451 if (port->break_length) {
452 if (port->break_length > 0) {
453 if (port->COR2 & COR2_ETC) {
454 rc_out(bp, CD180_TDR, CD180_C_ESC);
455 rc_out(bp, CD180_TDR, CD180_C_SBRK);
456 port->COR2 &= ~COR2_ETC;
458 count = min_t(int, port->break_length, 0xff);
459 rc_out(bp, CD180_TDR, CD180_C_ESC);
460 rc_out(bp, CD180_TDR, CD180_C_DELAY);
461 rc_out(bp, CD180_TDR, count);
462 if (!(port->break_length -= count))
463 port->break_length--;
464 } else {
465 rc_out(bp, CD180_TDR, CD180_C_ESC);
466 rc_out(bp, CD180_TDR, CD180_C_EBRK);
467 rc_out(bp, CD180_COR2, port->COR2);
468 rc_wait_CCR(bp);
469 rc_out(bp, CD180_CCR, CCR_CORCHG2);
470 port->break_length = 0;
472 return;
475 count = CD180_NFIFO;
476 do {
477 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
478 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
479 if (--port->xmit_cnt <= 0)
480 break;
481 } while (--count > 0);
483 if (port->xmit_cnt <= 0) {
484 rc_out(bp, CD180_CAR, port_No(port));
485 port->IER &= ~IER_TXRDY;
486 rc_out(bp, CD180_IER, port->IER);
488 if (port->xmit_cnt <= port->wakeup_chars)
489 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
492 static inline void rc_check_modem(struct riscom_board const * bp)
494 struct riscom_port *port;
495 struct tty_struct *tty;
496 unsigned char mcr;
498 if (!(port = rc_get_port(bp, "Modem")))
499 return;
501 tty = port->tty;
503 mcr = rc_in(bp, CD180_MCR);
504 if (mcr & MCR_CDCHG) {
505 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
506 wake_up_interruptible(&port->open_wait);
507 else
508 schedule_work(&port->tqueue_hangup);
511 #ifdef RISCOM_BRAIN_DAMAGED_CTS
512 if (mcr & MCR_CTSCHG) {
513 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
514 tty->hw_stopped = 0;
515 port->IER |= IER_TXRDY;
516 if (port->xmit_cnt <= port->wakeup_chars)
517 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
518 } else {
519 tty->hw_stopped = 1;
520 port->IER &= ~IER_TXRDY;
522 rc_out(bp, CD180_IER, port->IER);
524 if (mcr & MCR_DSRCHG) {
525 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
526 tty->hw_stopped = 0;
527 port->IER |= IER_TXRDY;
528 if (port->xmit_cnt <= port->wakeup_chars)
529 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
530 } else {
531 tty->hw_stopped = 1;
532 port->IER &= ~IER_TXRDY;
534 rc_out(bp, CD180_IER, port->IER);
536 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
538 /* Clear change bits */
539 rc_out(bp, CD180_MCR, 0);
542 /* The main interrupt processing routine */
543 static irqreturn_t rc_interrupt(int dummy, void * dev_id)
545 unsigned char status;
546 unsigned char ack;
547 struct riscom_board *bp = dev_id;
548 unsigned long loop = 0;
549 int handled = 0;
551 if (!(bp->flags & RC_BOARD_ACTIVE))
552 return IRQ_NONE;
554 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
555 (RC_BSR_TOUT | RC_BSR_TINT |
556 RC_BSR_MINT | RC_BSR_RINT))) {
557 handled = 1;
558 if (status & RC_BSR_TOUT)
559 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
560 "error?\n", board_No(bp));
562 else if (status & RC_BSR_RINT) {
563 ack = rc_in(bp, RC_ACK_RINT);
565 if (ack == (RC_ID | GIVR_IT_RCV))
566 rc_receive(bp);
567 else if (ack == (RC_ID | GIVR_IT_REXC))
568 rc_receive_exc(bp);
569 else
570 printk(KERN_WARNING "rc%d: Bad receive ack "
571 "0x%02x.\n",
572 board_No(bp), ack);
574 } else if (status & RC_BSR_TINT) {
575 ack = rc_in(bp, RC_ACK_TINT);
577 if (ack == (RC_ID | GIVR_IT_TX))
578 rc_transmit(bp);
579 else
580 printk(KERN_WARNING "rc%d: Bad transmit ack "
581 "0x%02x.\n",
582 board_No(bp), ack);
584 } else /* if (status & RC_BSR_MINT) */ {
585 ack = rc_in(bp, RC_ACK_MINT);
587 if (ack == (RC_ID | GIVR_IT_MODEM))
588 rc_check_modem(bp);
589 else
590 printk(KERN_WARNING "rc%d: Bad modem ack "
591 "0x%02x.\n",
592 board_No(bp), ack);
596 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
597 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
599 return IRQ_RETVAL(handled);
603 * Routines for open & close processing.
606 /* Called with disabled interrupts */
607 static int rc_setup_board(struct riscom_board * bp)
609 int error;
611 if (bp->flags & RC_BOARD_ACTIVE)
612 return 0;
614 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
615 "RISCom/8", bp);
616 if (error)
617 return error;
619 rc_out(bp, RC_CTOUT, 0); /* Just in case */
620 bp->DTR = ~0;
621 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
623 bp->flags |= RC_BOARD_ACTIVE;
625 return 0;
628 /* Called with disabled interrupts */
629 static void rc_shutdown_board(struct riscom_board *bp)
631 if (!(bp->flags & RC_BOARD_ACTIVE))
632 return;
634 bp->flags &= ~RC_BOARD_ACTIVE;
636 free_irq(bp->irq, NULL);
638 bp->DTR = ~0;
639 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
644 * Setting up port characteristics.
645 * Must be called with disabled interrupts
647 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
649 struct tty_struct *tty;
650 unsigned long baud;
651 long tmp;
652 unsigned char cor1 = 0, cor3 = 0;
653 unsigned char mcor1 = 0, mcor2 = 0;
655 if (!(tty = port->tty) || !tty->termios)
656 return;
658 port->IER = 0;
659 port->COR2 = 0;
660 port->MSVR = MSVR_RTS;
662 baud = tty_get_baud_rate(tty);
664 /* Select port on the board */
665 rc_out(bp, CD180_CAR, port_No(port));
667 if (!baud) {
668 /* Drop DTR & exit */
669 bp->DTR |= (1u << port_No(port));
670 rc_out(bp, RC_DTR, bp->DTR);
671 return;
672 } else {
673 /* Set DTR on */
674 bp->DTR &= ~(1u << port_No(port));
675 rc_out(bp, RC_DTR, bp->DTR);
679 * Now we must calculate some speed depended things
682 /* Set baud rate for port */
683 tmp = (((RC_OSCFREQ + baud/2) / baud +
684 CD180_TPC/2) / CD180_TPC);
686 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
687 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
688 rc_out(bp, CD180_RBPRL, tmp & 0xff);
689 rc_out(bp, CD180_TBPRL, tmp & 0xff);
691 baud = (baud + 5) / 10; /* Estimated CPS */
693 /* Two timer ticks seems enough to wakeup something like SLIP driver */
694 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
695 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
696 SERIAL_XMIT_SIZE - 1 : tmp);
698 /* Receiver timeout will be transmission time for 1.5 chars */
699 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
700 tmp = (tmp > 0xff) ? 0xff : tmp;
701 rc_out(bp, CD180_RTPR, tmp);
703 switch (C_CSIZE(tty)) {
704 case CS5:
705 cor1 |= COR1_5BITS;
706 break;
707 case CS6:
708 cor1 |= COR1_6BITS;
709 break;
710 case CS7:
711 cor1 |= COR1_7BITS;
712 break;
713 case CS8:
714 cor1 |= COR1_8BITS;
715 break;
718 if (C_CSTOPB(tty))
719 cor1 |= COR1_2SB;
721 cor1 |= COR1_IGNORE;
722 if (C_PARENB(tty)) {
723 cor1 |= COR1_NORMPAR;
724 if (C_PARODD(tty))
725 cor1 |= COR1_ODDP;
726 if (I_INPCK(tty))
727 cor1 &= ~COR1_IGNORE;
729 /* Set marking of some errors */
730 port->mark_mask = RCSR_OE | RCSR_TOUT;
731 if (I_INPCK(tty))
732 port->mark_mask |= RCSR_FE | RCSR_PE;
733 if (I_BRKINT(tty) || I_PARMRK(tty))
734 port->mark_mask |= RCSR_BREAK;
735 if (I_IGNPAR(tty))
736 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
737 if (I_IGNBRK(tty)) {
738 port->mark_mask &= ~RCSR_BREAK;
739 if (I_IGNPAR(tty))
740 /* Real raw mode. Ignore all */
741 port->mark_mask &= ~RCSR_OE;
743 /* Enable Hardware Flow Control */
744 if (C_CRTSCTS(tty)) {
745 #ifdef RISCOM_BRAIN_DAMAGED_CTS
746 port->IER |= IER_DSR | IER_CTS;
747 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
748 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
749 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
750 #else
751 port->COR2 |= COR2_CTSAE;
752 #endif
754 /* Enable Software Flow Control. FIXME: I'm not sure about this */
755 /* Some people reported that it works, but I still doubt */
756 if (I_IXON(tty)) {
757 port->COR2 |= COR2_TXIBE;
758 cor3 |= (COR3_FCT | COR3_SCDE);
759 if (I_IXANY(tty))
760 port->COR2 |= COR2_IXM;
761 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
762 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
763 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
764 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
766 if (!C_CLOCAL(tty)) {
767 /* Enable CD check */
768 port->IER |= IER_CD;
769 mcor1 |= MCOR1_CDZD;
770 mcor2 |= MCOR2_CDOD;
773 if (C_CREAD(tty))
774 /* Enable receiver */
775 port->IER |= IER_RXD;
777 /* Set input FIFO size (1-8 bytes) */
778 cor3 |= RISCOM_RXFIFO;
779 /* Setting up CD180 channel registers */
780 rc_out(bp, CD180_COR1, cor1);
781 rc_out(bp, CD180_COR2, port->COR2);
782 rc_out(bp, CD180_COR3, cor3);
783 /* Make CD180 know about registers change */
784 rc_wait_CCR(bp);
785 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
786 /* Setting up modem option registers */
787 rc_out(bp, CD180_MCOR1, mcor1);
788 rc_out(bp, CD180_MCOR2, mcor2);
789 /* Enable CD180 transmitter & receiver */
790 rc_wait_CCR(bp);
791 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
792 /* Enable interrupts */
793 rc_out(bp, CD180_IER, port->IER);
794 /* And finally set RTS on */
795 rc_out(bp, CD180_MSVR, port->MSVR);
798 /* Must be called with interrupts enabled */
799 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
801 unsigned long flags;
803 if (port->flags & ASYNC_INITIALIZED)
804 return 0;
806 if (!port->xmit_buf) {
807 /* We may sleep in get_zeroed_page() */
808 unsigned long tmp;
810 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
811 return -ENOMEM;
813 if (port->xmit_buf) {
814 free_page(tmp);
815 return -ERESTARTSYS;
817 port->xmit_buf = (unsigned char *) tmp;
820 spin_lock_irqsave(&riscom_lock, flags);
822 if (port->tty)
823 clear_bit(TTY_IO_ERROR, &port->tty->flags);
825 if (port->count == 1)
826 bp->count++;
828 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
829 rc_change_speed(bp, port);
830 port->flags |= ASYNC_INITIALIZED;
832 spin_unlock_irqrestore(&riscom_lock, flags);
833 return 0;
836 /* Must be called with interrupts disabled */
837 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
839 struct tty_struct *tty;
841 if (!(port->flags & ASYNC_INITIALIZED))
842 return;
844 #ifdef RC_REPORT_OVERRUN
845 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
846 board_No(bp), port_No(port), port->overrun);
847 #endif
848 #ifdef RC_REPORT_FIFO
850 int i;
852 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
853 board_No(bp), port_No(port));
854 for (i = 0; i < 10; i++) {
855 printk("%ld ", port->hits[i]);
857 printk("].\n");
859 #endif
860 if (port->xmit_buf) {
861 free_page((unsigned long) port->xmit_buf);
862 port->xmit_buf = NULL;
865 if (!(tty = port->tty) || C_HUPCL(tty)) {
866 /* Drop DTR */
867 bp->DTR |= (1u << port_No(port));
868 rc_out(bp, RC_DTR, bp->DTR);
871 /* Select port */
872 rc_out(bp, CD180_CAR, port_No(port));
873 /* Reset port */
874 rc_wait_CCR(bp);
875 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
876 /* Disable all interrupts from this port */
877 port->IER = 0;
878 rc_out(bp, CD180_IER, port->IER);
880 if (tty)
881 set_bit(TTY_IO_ERROR, &tty->flags);
882 port->flags &= ~ASYNC_INITIALIZED;
884 if (--bp->count < 0) {
885 printk(KERN_INFO "rc%d: rc_shutdown_port: "
886 "bad board count: %d\n",
887 board_No(bp), bp->count);
888 bp->count = 0;
892 * If this is the last opened port on the board
893 * shutdown whole board
895 if (!bp->count)
896 rc_shutdown_board(bp);
900 static int block_til_ready(struct tty_struct *tty, struct file * filp,
901 struct riscom_port *port)
903 DECLARE_WAITQUEUE(wait, current);
904 struct riscom_board *bp = port_Board(port);
905 int retval;
906 int do_clocal = 0;
907 int CD;
908 unsigned long flags;
911 * If the device is in the middle of being closed, then block
912 * until it's done, and then try again.
914 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
915 interruptible_sleep_on(&port->close_wait);
916 if (port->flags & ASYNC_HUP_NOTIFY)
917 return -EAGAIN;
918 else
919 return -ERESTARTSYS;
923 * If non-blocking mode is set, or the port is not enabled,
924 * then make the check up front and then exit.
926 if ((filp->f_flags & O_NONBLOCK) ||
927 (tty->flags & (1 << TTY_IO_ERROR))) {
928 port->flags |= ASYNC_NORMAL_ACTIVE;
929 return 0;
932 if (C_CLOCAL(tty))
933 do_clocal = 1;
936 * Block waiting for the carrier detect and the line to become
937 * free (i.e., not in use by the callout). While we are in
938 * this loop, info->count is dropped by one, so that
939 * rs_close() knows when to free things. We restore it upon
940 * exit, either normal or abnormal.
942 retval = 0;
943 add_wait_queue(&port->open_wait, &wait);
945 spin_lock_irqsave(&riscom_lock, flags);
947 if (!tty_hung_up_p(filp))
948 port->count--;
950 spin_unlock_irqrestore(&riscom_lock, flags);
952 port->blocked_open++;
953 while (1) {
954 spin_lock_irqsave(&riscom_lock, flags);
956 rc_out(bp, CD180_CAR, port_No(port));
957 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
958 rc_out(bp, CD180_MSVR, MSVR_RTS);
959 bp->DTR &= ~(1u << port_No(port));
960 rc_out(bp, RC_DTR, bp->DTR);
962 spin_unlock_irqrestore(&riscom_lock, flags);
964 set_current_state(TASK_INTERRUPTIBLE);
965 if (tty_hung_up_p(filp) ||
966 !(port->flags & ASYNC_INITIALIZED)) {
967 if (port->flags & ASYNC_HUP_NOTIFY)
968 retval = -EAGAIN;
969 else
970 retval = -ERESTARTSYS;
971 break;
973 if (!(port->flags & ASYNC_CLOSING) &&
974 (do_clocal || CD))
975 break;
976 if (signal_pending(current)) {
977 retval = -ERESTARTSYS;
978 break;
980 schedule();
982 __set_current_state(TASK_RUNNING);
983 remove_wait_queue(&port->open_wait, &wait);
984 if (!tty_hung_up_p(filp))
985 port->count++;
986 port->blocked_open--;
987 if (retval)
988 return retval;
990 port->flags |= ASYNC_NORMAL_ACTIVE;
991 return 0;
994 static int rc_open(struct tty_struct * tty, struct file * filp)
996 int board;
997 int error;
998 struct riscom_port * port;
999 struct riscom_board * bp;
1001 board = RC_BOARD(tty->index);
1002 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
1003 return -ENODEV;
1005 bp = &rc_board[board];
1006 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
1007 if (rc_paranoia_check(port, tty->name, "rc_open"))
1008 return -ENODEV;
1010 if ((error = rc_setup_board(bp)))
1011 return error;
1013 port->count++;
1014 tty->driver_data = port;
1015 port->tty = tty;
1017 if ((error = rc_setup_port(bp, port)))
1018 return error;
1020 if ((error = block_til_ready(tty, filp, port)))
1021 return error;
1023 return 0;
1026 static void rc_close(struct tty_struct * tty, struct file * filp)
1028 struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1029 struct riscom_board *bp;
1030 unsigned long flags;
1031 unsigned long timeout;
1033 if (!port || rc_paranoia_check(port, tty->name, "close"))
1034 return;
1036 spin_lock_irqsave(&riscom_lock, flags);
1038 if (tty_hung_up_p(filp))
1039 goto out;
1041 bp = port_Board(port);
1042 if ((tty->count == 1) && (port->count != 1)) {
1043 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1044 " tty->count is 1, port count is %d\n",
1045 board_No(bp), port->count);
1046 port->count = 1;
1048 if (--port->count < 0) {
1049 printk(KERN_INFO "rc%d: rc_close: bad port count "
1050 "for tty%d: %d\n",
1051 board_No(bp), port_No(port), port->count);
1052 port->count = 0;
1054 if (port->count)
1055 goto out;
1056 port->flags |= ASYNC_CLOSING;
1058 * Now we wait for the transmit buffer to clear; and we notify
1059 * the line discipline to only process XON/XOFF characters.
1061 tty->closing = 1;
1062 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1063 tty_wait_until_sent(tty, port->closing_wait);
1065 * At this point we stop accepting input. To do this, we
1066 * disable the receive line status interrupts, and tell the
1067 * interrupt driver to stop checking the data ready bit in the
1068 * line status register.
1070 port->IER &= ~IER_RXD;
1071 if (port->flags & ASYNC_INITIALIZED) {
1072 port->IER &= ~IER_TXRDY;
1073 port->IER |= IER_TXEMPTY;
1074 rc_out(bp, CD180_CAR, port_No(port));
1075 rc_out(bp, CD180_IER, port->IER);
1077 * Before we drop DTR, make sure the UART transmitter
1078 * has completely drained; this is especially
1079 * important if there is a transmit FIFO!
1081 timeout = jiffies+HZ;
1082 while(port->IER & IER_TXEMPTY) {
1083 msleep_interruptible(jiffies_to_msecs(port->timeout));
1084 if (time_after(jiffies, timeout))
1085 break;
1088 rc_shutdown_port(bp, port);
1089 if (tty->driver->flush_buffer)
1090 tty->driver->flush_buffer(tty);
1091 tty_ldisc_flush(tty);
1093 tty->closing = 0;
1094 port->event = 0;
1095 port->tty = NULL;
1096 if (port->blocked_open) {
1097 if (port->close_delay) {
1098 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1100 wake_up_interruptible(&port->open_wait);
1102 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1103 wake_up_interruptible(&port->close_wait);
1105 out:
1106 spin_unlock_irqrestore(&riscom_lock, flags);
1109 static int rc_write(struct tty_struct * tty,
1110 const unsigned char *buf, int count)
1112 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1113 struct riscom_board *bp;
1114 int c, total = 0;
1115 unsigned long flags;
1117 if (rc_paranoia_check(port, tty->name, "rc_write"))
1118 return 0;
1120 bp = port_Board(port);
1122 if (!tty || !port->xmit_buf)
1123 return 0;
1125 while (1) {
1126 spin_lock_irqsave(&riscom_lock, flags);
1128 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1129 SERIAL_XMIT_SIZE - port->xmit_head));
1130 if (c <= 0)
1131 break; /* lock continues to be held */
1133 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1134 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1135 port->xmit_cnt += c;
1137 spin_unlock_irqrestore(&riscom_lock, flags);
1139 buf += c;
1140 count -= c;
1141 total += c;
1144 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1145 !(port->IER & IER_TXRDY)) {
1146 port->IER |= IER_TXRDY;
1147 rc_out(bp, CD180_CAR, port_No(port));
1148 rc_out(bp, CD180_IER, port->IER);
1151 spin_unlock_irqrestore(&riscom_lock, flags);
1153 return total;
1156 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1158 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1159 unsigned long flags;
1161 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1162 return;
1164 if (!tty || !port->xmit_buf)
1165 return;
1167 spin_lock_irqsave(&riscom_lock, flags);
1169 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1170 goto out;
1172 port->xmit_buf[port->xmit_head++] = ch;
1173 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1174 port->xmit_cnt++;
1176 out:
1177 spin_unlock_irqrestore(&riscom_lock, flags);
1180 static void rc_flush_chars(struct tty_struct * tty)
1182 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1183 unsigned long flags;
1185 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1186 return;
1188 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1189 !port->xmit_buf)
1190 return;
1192 spin_lock_irqsave(&riscom_lock, flags);
1194 port->IER |= IER_TXRDY;
1195 rc_out(port_Board(port), CD180_CAR, port_No(port));
1196 rc_out(port_Board(port), CD180_IER, port->IER);
1198 spin_unlock_irqrestore(&riscom_lock, flags);
1201 static int rc_write_room(struct tty_struct * tty)
1203 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1204 int ret;
1206 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1207 return 0;
1209 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1210 if (ret < 0)
1211 ret = 0;
1212 return ret;
1215 static int rc_chars_in_buffer(struct tty_struct *tty)
1217 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1219 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1220 return 0;
1222 return port->xmit_cnt;
1225 static void rc_flush_buffer(struct tty_struct *tty)
1227 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1228 unsigned long flags;
1230 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1231 return;
1233 spin_lock_irqsave(&riscom_lock, flags);
1235 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1237 spin_unlock_irqrestore(&riscom_lock, flags);
1239 tty_wakeup(tty);
1242 static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1244 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1245 struct riscom_board * bp;
1246 unsigned char status;
1247 unsigned int result;
1248 unsigned long flags;
1250 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1251 return -ENODEV;
1253 bp = port_Board(port);
1255 spin_lock_irqsave(&riscom_lock, flags);
1257 rc_out(bp, CD180_CAR, port_No(port));
1258 status = rc_in(bp, CD180_MSVR);
1259 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1261 spin_unlock_irqrestore(&riscom_lock, flags);
1263 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1264 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1265 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1266 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1267 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1268 return result;
1271 static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1272 unsigned int set, unsigned int clear)
1274 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1275 unsigned long flags;
1276 struct riscom_board *bp;
1278 if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1279 return -ENODEV;
1281 bp = port_Board(port);
1283 spin_lock_irqsave(&riscom_lock, flags);
1285 if (set & TIOCM_RTS)
1286 port->MSVR |= MSVR_RTS;
1287 if (set & TIOCM_DTR)
1288 bp->DTR &= ~(1u << port_No(port));
1290 if (clear & TIOCM_RTS)
1291 port->MSVR &= ~MSVR_RTS;
1292 if (clear & TIOCM_DTR)
1293 bp->DTR |= (1u << port_No(port));
1295 rc_out(bp, CD180_CAR, port_No(port));
1296 rc_out(bp, CD180_MSVR, port->MSVR);
1297 rc_out(bp, RC_DTR, bp->DTR);
1299 spin_unlock_irqrestore(&riscom_lock, flags);
1301 return 0;
1304 static inline void rc_send_break(struct riscom_port * port, unsigned long length)
1306 struct riscom_board *bp = port_Board(port);
1307 unsigned long flags;
1309 spin_lock_irqsave(&riscom_lock, flags);
1311 port->break_length = RISCOM_TPS / HZ * length;
1312 port->COR2 |= COR2_ETC;
1313 port->IER |= IER_TXRDY;
1314 rc_out(bp, CD180_CAR, port_No(port));
1315 rc_out(bp, CD180_COR2, port->COR2);
1316 rc_out(bp, CD180_IER, port->IER);
1317 rc_wait_CCR(bp);
1318 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1319 rc_wait_CCR(bp);
1321 spin_unlock_irqrestore(&riscom_lock, flags);
1324 static inline int rc_set_serial_info(struct riscom_port * port,
1325 struct serial_struct __user * newinfo)
1327 struct serial_struct tmp;
1328 struct riscom_board *bp = port_Board(port);
1329 int change_speed;
1331 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1332 return -EFAULT;
1334 #if 0
1335 if ((tmp.irq != bp->irq) ||
1336 (tmp.port != bp->base) ||
1337 (tmp.type != PORT_CIRRUS) ||
1338 (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1339 (tmp.custom_divisor != 0) ||
1340 (tmp.xmit_fifo_size != CD180_NFIFO) ||
1341 (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1342 return -EINVAL;
1343 #endif
1345 change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1346 (tmp.flags & ASYNC_SPD_MASK));
1348 if (!capable(CAP_SYS_ADMIN)) {
1349 if ((tmp.close_delay != port->close_delay) ||
1350 (tmp.closing_wait != port->closing_wait) ||
1351 ((tmp.flags & ~ASYNC_USR_MASK) !=
1352 (port->flags & ~ASYNC_USR_MASK)))
1353 return -EPERM;
1354 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1355 (tmp.flags & ASYNC_USR_MASK));
1356 } else {
1357 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1358 (tmp.flags & ASYNC_FLAGS));
1359 port->close_delay = tmp.close_delay;
1360 port->closing_wait = tmp.closing_wait;
1362 if (change_speed) {
1363 unsigned long flags;
1365 spin_lock_irqsave(&riscom_lock, flags);
1366 rc_change_speed(bp, port);
1367 spin_unlock_irqrestore(&riscom_lock, flags);
1369 return 0;
1372 static inline int rc_get_serial_info(struct riscom_port * port,
1373 struct serial_struct __user *retinfo)
1375 struct serial_struct tmp;
1376 struct riscom_board *bp = port_Board(port);
1378 memset(&tmp, 0, sizeof(tmp));
1379 tmp.type = PORT_CIRRUS;
1380 tmp.line = port - rc_port;
1381 tmp.port = bp->base;
1382 tmp.irq = bp->irq;
1383 tmp.flags = port->flags;
1384 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1385 tmp.close_delay = port->close_delay * HZ/100;
1386 tmp.closing_wait = port->closing_wait * HZ/100;
1387 tmp.xmit_fifo_size = CD180_NFIFO;
1388 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1391 static int rc_ioctl(struct tty_struct * tty, struct file * filp,
1392 unsigned int cmd, unsigned long arg)
1395 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1396 void __user *argp = (void __user *)arg;
1397 int retval;
1399 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1400 return -ENODEV;
1402 switch (cmd) {
1403 case TCSBRK: /* SVID version: non-zero arg --> no break */
1404 retval = tty_check_change(tty);
1405 if (retval)
1406 return retval;
1407 tty_wait_until_sent(tty, 0);
1408 if (!arg)
1409 rc_send_break(port, HZ/4); /* 1/4 second */
1410 break;
1411 case TCSBRKP: /* support for POSIX tcsendbreak() */
1412 retval = tty_check_change(tty);
1413 if (retval)
1414 return retval;
1415 tty_wait_until_sent(tty, 0);
1416 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1417 break;
1418 case TIOCGSOFTCAR:
1419 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned __user *)argp);
1420 case TIOCSSOFTCAR:
1421 if (get_user(arg,(unsigned __user *) argp))
1422 return -EFAULT;
1423 tty->termios->c_cflag =
1424 ((tty->termios->c_cflag & ~CLOCAL) |
1425 (arg ? CLOCAL : 0));
1426 break;
1427 case TIOCGSERIAL:
1428 return rc_get_serial_info(port, argp);
1429 case TIOCSSERIAL:
1430 return rc_set_serial_info(port, argp);
1431 default:
1432 return -ENOIOCTLCMD;
1434 return 0;
1437 static void rc_throttle(struct tty_struct * tty)
1439 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1440 struct riscom_board *bp;
1441 unsigned long flags;
1443 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1444 return;
1446 bp = port_Board(port);
1448 spin_lock_irqsave(&riscom_lock, flags);
1450 port->MSVR &= ~MSVR_RTS;
1451 rc_out(bp, CD180_CAR, port_No(port));
1452 if (I_IXOFF(tty)) {
1453 rc_wait_CCR(bp);
1454 rc_out(bp, CD180_CCR, CCR_SSCH2);
1455 rc_wait_CCR(bp);
1457 rc_out(bp, CD180_MSVR, port->MSVR);
1459 spin_unlock_irqrestore(&riscom_lock, flags);
1462 static void rc_unthrottle(struct tty_struct * tty)
1464 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1465 struct riscom_board *bp;
1466 unsigned long flags;
1468 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1469 return;
1471 bp = port_Board(port);
1473 spin_lock_irqsave(&riscom_lock, flags);
1475 port->MSVR |= MSVR_RTS;
1476 rc_out(bp, CD180_CAR, port_No(port));
1477 if (I_IXOFF(tty)) {
1478 rc_wait_CCR(bp);
1479 rc_out(bp, CD180_CCR, CCR_SSCH1);
1480 rc_wait_CCR(bp);
1482 rc_out(bp, CD180_MSVR, port->MSVR);
1484 spin_unlock_irqrestore(&riscom_lock, flags);
1487 static void rc_stop(struct tty_struct * tty)
1489 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1490 struct riscom_board *bp;
1491 unsigned long flags;
1493 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1494 return;
1496 bp = port_Board(port);
1498 spin_lock_irqsave(&riscom_lock, flags);
1500 port->IER &= ~IER_TXRDY;
1501 rc_out(bp, CD180_CAR, port_No(port));
1502 rc_out(bp, CD180_IER, port->IER);
1504 spin_unlock_irqrestore(&riscom_lock, flags);
1507 static void rc_start(struct tty_struct * tty)
1509 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1510 struct riscom_board *bp;
1511 unsigned long flags;
1513 if (rc_paranoia_check(port, tty->name, "rc_start"))
1514 return;
1516 bp = port_Board(port);
1518 spin_lock_irqsave(&riscom_lock, flags);
1520 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
1521 port->IER |= IER_TXRDY;
1522 rc_out(bp, CD180_CAR, port_No(port));
1523 rc_out(bp, CD180_IER, port->IER);
1526 spin_unlock_irqrestore(&riscom_lock, flags);
1530 * This routine is called from the work queue when the interrupt
1531 * routine has signalled that a hangup has occurred. The path of
1532 * hangup processing is:
1534 * serial interrupt routine -> (workqueue) ->
1535 * do_rc_hangup() -> tty->hangup() -> rc_hangup()
1538 static void do_rc_hangup(struct work_struct *ugly_api)
1540 struct riscom_port *port = container_of(ugly_api, struct riscom_port, tqueue_hangup);
1541 struct tty_struct *tty;
1543 tty = port->tty;
1544 if (tty)
1545 tty_hangup(tty); /* FIXME: module removal race still here */
1548 static void rc_hangup(struct tty_struct * tty)
1550 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1551 struct riscom_board *bp;
1553 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1554 return;
1556 bp = port_Board(port);
1558 rc_shutdown_port(bp, port);
1559 port->event = 0;
1560 port->count = 0;
1561 port->flags &= ~ASYNC_NORMAL_ACTIVE;
1562 port->tty = NULL;
1563 wake_up_interruptible(&port->open_wait);
1566 static void rc_set_termios(struct tty_struct * tty, struct ktermios * old_termios)
1568 struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1569 unsigned long flags;
1571 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1572 return;
1574 if (tty->termios->c_cflag == old_termios->c_cflag &&
1575 tty->termios->c_iflag == old_termios->c_iflag)
1576 return;
1578 spin_lock_irqsave(&riscom_lock, flags);
1579 rc_change_speed(port_Board(port), port);
1580 spin_unlock_irqrestore(&riscom_lock, flags);
1582 if ((old_termios->c_cflag & CRTSCTS) &&
1583 !(tty->termios->c_cflag & CRTSCTS)) {
1584 tty->hw_stopped = 0;
1585 rc_start(tty);
1589 static void do_softint(struct work_struct *ugly_api)
1591 struct riscom_port *port = container_of(ugly_api, struct riscom_port, tqueue);
1592 struct tty_struct *tty;
1594 if(!(tty = port->tty))
1595 return;
1597 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event))
1598 tty_wakeup(tty);
1601 static const struct tty_operations riscom_ops = {
1602 .open = rc_open,
1603 .close = rc_close,
1604 .write = rc_write,
1605 .put_char = rc_put_char,
1606 .flush_chars = rc_flush_chars,
1607 .write_room = rc_write_room,
1608 .chars_in_buffer = rc_chars_in_buffer,
1609 .flush_buffer = rc_flush_buffer,
1610 .ioctl = rc_ioctl,
1611 .throttle = rc_throttle,
1612 .unthrottle = rc_unthrottle,
1613 .set_termios = rc_set_termios,
1614 .stop = rc_stop,
1615 .start = rc_start,
1616 .hangup = rc_hangup,
1617 .tiocmget = rc_tiocmget,
1618 .tiocmset = rc_tiocmset,
1621 static int __init rc_init_drivers(void)
1623 int error;
1624 int i;
1626 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1627 if (!riscom_driver)
1628 return -ENOMEM;
1630 riscom_driver->owner = THIS_MODULE;
1631 riscom_driver->name = "ttyL";
1632 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1633 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1634 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1635 riscom_driver->init_termios = tty_std_termios;
1636 riscom_driver->init_termios.c_cflag =
1637 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1638 riscom_driver->init_termios.c_ispeed = 9600;
1639 riscom_driver->init_termios.c_ospeed = 9600;
1640 riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1641 tty_set_operations(riscom_driver, &riscom_ops);
1642 if ((error = tty_register_driver(riscom_driver))) {
1643 put_tty_driver(riscom_driver);
1644 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1645 "error = %d\n",
1646 error);
1647 return 1;
1650 memset(rc_port, 0, sizeof(rc_port));
1651 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1652 rc_port[i].magic = RISCOM8_MAGIC;
1653 INIT_WORK(&rc_port[i].tqueue, do_softint);
1654 INIT_WORK(&rc_port[i].tqueue_hangup, do_rc_hangup);
1655 rc_port[i].close_delay = 50 * HZ/100;
1656 rc_port[i].closing_wait = 3000 * HZ/100;
1657 init_waitqueue_head(&rc_port[i].open_wait);
1658 init_waitqueue_head(&rc_port[i].close_wait);
1661 return 0;
1664 static void rc_release_drivers(void)
1666 unsigned long flags;
1668 spin_lock_irqsave(&riscom_lock, flags);
1670 tty_unregister_driver(riscom_driver);
1671 put_tty_driver(riscom_driver);
1673 spin_unlock_irqrestore(&riscom_lock, flags);
1676 #ifndef MODULE
1678 * Called at boot time.
1680 * You can specify IO base for up to RC_NBOARD cards,
1681 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1682 * Note that there will be no probing at default
1683 * addresses in this case.
1686 static int __init riscom8_setup(char *str)
1688 int ints[RC_NBOARD];
1689 int i;
1691 str = get_options(str, ARRAY_SIZE(ints), ints);
1693 for (i = 0; i < RC_NBOARD; i++) {
1694 if (i < ints[0])
1695 rc_board[i].base = ints[i+1];
1696 else
1697 rc_board[i].base = 0;
1699 return 1;
1702 __setup("riscom8=", riscom8_setup);
1703 #endif
1705 static char banner[] __initdata =
1706 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1707 "1994-1996.\n";
1708 static char no_boards_msg[] __initdata =
1709 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1712 * This routine must be called by kernel at boot time
1714 static int __init riscom8_init(void)
1716 int i;
1717 int found = 0;
1719 printk(banner);
1721 if (rc_init_drivers())
1722 return -EIO;
1724 for (i = 0; i < RC_NBOARD; i++)
1725 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1726 found++;
1728 if (!found) {
1729 rc_release_drivers();
1730 printk(no_boards_msg);
1731 return -EIO;
1733 return 0;
1736 #ifdef MODULE
1737 static int iobase;
1738 static int iobase1;
1739 static int iobase2;
1740 static int iobase3;
1741 module_param(iobase, int, 0);
1742 module_param(iobase1, int, 0);
1743 module_param(iobase2, int, 0);
1744 module_param(iobase3, int, 0);
1746 MODULE_LICENSE("GPL");
1747 #endif /* MODULE */
1750 * You can setup up to 4 boards (current value of RC_NBOARD)
1751 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1754 static int __init riscom8_init_module (void)
1756 #ifdef MODULE
1757 int i;
1759 if (iobase || iobase1 || iobase2 || iobase3) {
1760 for(i = 0; i < RC_NBOARD; i++)
1761 rc_board[0].base = 0;
1764 if (iobase)
1765 rc_board[0].base = iobase;
1766 if (iobase1)
1767 rc_board[1].base = iobase1;
1768 if (iobase2)
1769 rc_board[2].base = iobase2;
1770 if (iobase3)
1771 rc_board[3].base = iobase3;
1772 #endif /* MODULE */
1774 return riscom8_init();
1777 static void __exit riscom8_exit_module (void)
1779 int i;
1781 rc_release_drivers();
1782 for (i = 0; i < RC_NBOARD; i++)
1783 if (rc_board[i].flags & RC_BOARD_PRESENT)
1784 rc_release_io_range(&rc_board[i]);
1788 module_init(riscom8_init_module);
1789 module_exit(riscom8_exit_module);