ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / riscom8.c
1 /*
2  *      linux/drivers/char/riscom.c  -- RISCom/8 multiport serial driver.
3  *
4  *      Copyright (C) 1994-1996  Dmitry Gorodchanin (pgmdsg@ibi.com)
5  *
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.
11  *
12  *
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.
17  *
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.
22  *
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.
26  *
27  *      Revision 1.1
28  *
29  *      ChangeLog:
30  *      Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31  *      - get rid of check_region and several cleanups
32  */
33
34 #include <linux/module.h>
35
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
49 #include <asm/uaccess.h>
50
51 #include "riscom8.h"
52 #include "riscom8_reg.h"
53
54 /* Am I paranoid or not ? ;-) */
55 #define RISCOM_PARANOIA_CHECK
56
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. 
61  */
62
63 #define RISCOM_BRAIN_DAMAGED_CTS
64
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.
68  */
69 #undef RC_REPORT_FIFO
70 #undef RC_REPORT_OVERRUN
71
72
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)
77
78 #ifndef MIN
79 #define MIN(a,b) ((a) < (b) ? (a) : (b))
80 #endif
81
82 #define RS_EVENT_WRITE_WAKEUP   0
83
84 static struct riscom_board * IRQ_to_board[16];
85 static struct tty_driver *riscom_driver;
86 static unsigned char * tmp_buf;
87 static DECLARE_MUTEX(tmp_buf_sem);
88
89 static unsigned long baud_table[] =  {
90         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
91         9600, 19200, 38400, 57600, 76800, 0, 
92 };
93
94 static struct riscom_board rc_board[RC_NBOARD] =  {
95         {
96                 .base   = RC_IOBASE1,
97         },
98         {
99                 .base   = RC_IOBASE2,
100         },
101         {
102                 .base   = RC_IOBASE3,
103         },
104         {
105                 .base   = RC_IOBASE4,
106         },
107 };
108
109 static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
110                 
111 /* RISCom/8 I/O ports addresses (without address translation) */
112 static unsigned short rc_ioport[] =  {
113 #if 1   
114         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
115 #else   
116         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
117         0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
118         0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
119 #endif  
120 };
121 #define RC_NIOPORT      (sizeof(rc_ioport) / sizeof(rc_ioport[0]))
122
123
124 static inline int rc_paranoia_check(struct riscom_port const * port,
125                                     char *name, const char *routine)
126 {
127 #ifdef RISCOM_PARANOIA_CHECK
128         static const char badmagic[] = KERN_INFO
129                 "rc: Warning: bad riscom port magic number for device %s in %s\n";
130         static const char badinfo[] = KERN_INFO
131                 "rc: Warning: null riscom port for device %s in %s\n";
132
133         if (!port) {
134                 printk(badinfo, name, routine);
135                 return 1;
136         }
137         if (port->magic != RISCOM8_MAGIC) {
138                 printk(badmagic, name, routine);
139                 return 1;
140         }
141 #endif
142         return 0;
143 }
144
145 /*
146  * 
147  *  Service functions for RISCom/8 driver.
148  * 
149  */
150
151 /* Get board number from pointer */
152 static inline int board_No (struct riscom_board const * bp)
153 {
154         return bp - rc_board;
155 }
156
157 /* Get port number from pointer */
158 static inline int port_No (struct riscom_port const * port)
159 {
160         return RC_PORT(port - rc_port); 
161 }
162
163 /* Get pointer to board from pointer to port */
164 static inline struct riscom_board * port_Board(struct riscom_port const * port)
165 {
166         return &rc_board[RC_BOARD(port - rc_port)];
167 }
168
169 /* Input Byte from CL CD180 register */
170 static inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg)
171 {
172         return inb(bp->base + RC_TO_ISA(reg));
173 }
174
175 /* Output Byte to CL CD180 register */
176 static inline void rc_out(struct riscom_board const * bp, unsigned short reg,
177                           unsigned char val)
178 {
179         outb(val, bp->base + RC_TO_ISA(reg));
180 }
181
182 /* Wait for Channel Command Register ready */
183 static inline void rc_wait_CCR(struct riscom_board const * bp)
184 {
185         unsigned long delay;
186
187         /* FIXME: need something more descriptive then 100000 :) */
188         for (delay = 100000; delay; delay--) 
189                 if (!rc_in(bp, CD180_CCR))
190                         return;
191         
192         printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
193 }
194
195 /*
196  *  RISCom/8 probe functions.
197  */
198
199 static inline int rc_request_io_range(struct riscom_board * const bp)
200 {
201         int i;
202         
203         for (i = 0; i < RC_NIOPORT; i++)  
204                 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
205                                    "RISCom/8"))  {
206                         goto out_release;
207                 }
208         return 0;
209 out_release:
210         printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
211                          board_No(bp), bp->base);
212         while(--i >= 0)
213                 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
214         return 1;
215 }
216
217 static inline void rc_release_io_range(struct riscom_board * const bp)
218 {
219         int i;
220         
221         for (i = 0; i < RC_NIOPORT; i++)  
222                 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
223 }
224         
225 /* Must be called with enabled interrupts */
226 static inline void rc_long_delay(unsigned long delay)
227 {
228         unsigned long i;
229         
230         for (i = jiffies + delay; time_after(i,jiffies); ) ;
231 }
232
233 /* Reset and setup CD180 chip */
234 static void __init rc_init_CD180(struct riscom_board const * bp)
235 {
236         unsigned long flags;
237         
238         save_flags(flags); cli();
239         rc_out(bp, RC_CTOUT, 0);                   /* Clear timeout             */
240         rc_wait_CCR(bp);                           /* Wait for CCR ready        */
241         rc_out(bp, CD180_CCR, CCR_HARDRESET);      /* Reset CD180 chip          */
242         sti();
243         rc_long_delay(HZ/20);                      /* Delay 0.05 sec            */
244         cli();
245         rc_out(bp, CD180_GIVR, RC_ID);             /* Set ID for this chip      */
246         rc_out(bp, CD180_GICR, 0);                 /* Clear all bits            */
247         rc_out(bp, CD180_PILR1, RC_ACK_MINT);      /* Prio for modem intr       */
248         rc_out(bp, CD180_PILR2, RC_ACK_TINT);      /* Prio for transmitter intr */
249         rc_out(bp, CD180_PILR3, RC_ACK_RINT);      /* Prio for receiver intr    */
250         
251         /* Setting up prescaler. We need 4 ticks per 1 ms */
252         rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
253         rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
254         
255         restore_flags(flags);
256 }
257
258 /* Main probing routine, also sets irq. */
259 static int __init rc_probe(struct riscom_board *bp)
260 {
261         unsigned char val1, val2;
262         int irqs = 0;
263         int retries;
264         
265         bp->irq = 0;
266
267         if (rc_request_io_range(bp))
268                 return 1;
269         
270         /* Are the I/O ports here ? */
271         rc_out(bp, CD180_PPRL, 0x5a);
272         outb(0xff, 0x80);
273         val1 = rc_in(bp, CD180_PPRL);
274         rc_out(bp, CD180_PPRL, 0xa5);
275         outb(0x00, 0x80);
276         val2 = rc_in(bp, CD180_PPRL);
277         
278         if ((val1 != 0x5a) || (val2 != 0xa5))  {
279                 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
280                        board_No(bp), bp->base);
281                 goto out_release;
282         }
283         
284         /* It's time to find IRQ for this board */
285         for (retries = 0; retries < 5 && irqs <= 0; retries++)  {
286                 irqs = probe_irq_on();
287                 rc_init_CD180(bp);                      /* Reset CD180 chip       */
288                 rc_out(bp, CD180_CAR, 2);               /* Select port 2          */
289                 rc_wait_CCR(bp);
290                 rc_out(bp, CD180_CCR, CCR_TXEN);        /* Enable transmitter     */
291                 rc_out(bp, CD180_IER, IER_TXRDY);       /* Enable tx empty intr   */
292                 rc_long_delay(HZ/20);                   
293                 irqs = probe_irq_off(irqs);
294                 val1 = rc_in(bp, RC_BSR);               /* Get Board Status reg   */
295                 val2 = rc_in(bp, RC_ACK_TINT);          /* ACK interrupt          */
296                 rc_init_CD180(bp);                      /* Reset CD180 again      */
297         
298                 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX)))  {
299                         printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
300                                         "found.\n", board_No(bp), bp->base);
301                         goto out_release;
302                 }
303         }
304         
305         if (irqs <= 0)  {
306                 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
307                                 "at 0x%03x.\n", board_No(bp), bp->base);
308                 goto out_release;
309         }
310         bp->irq = irqs;
311         bp->flags |= RC_BOARD_PRESENT;
312         
313         printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
314                          "0x%03x, IRQ %d.\n",
315                board_No(bp),
316                (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A',   /* Board revision */
317                bp->base, bp->irq);
318         
319         return 0;
320 out_release:
321         rc_release_io_range(bp);
322         return 1;
323 }
324
325 /* 
326  * 
327  *  Interrupt processing routines.
328  * 
329  */
330
331 static inline void rc_mark_event(struct riscom_port * port, int event)
332 {
333         set_bit(event, &port->event);
334         schedule_work(&port->tqueue);
335 }
336
337 static inline struct riscom_port * rc_get_port(struct riscom_board const * bp,
338                                                unsigned char const * what)
339 {
340         unsigned char channel;
341         struct riscom_port * port;
342         
343         channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
344         if (channel < CD180_NCH)  {
345                 port = &rc_port[board_No(bp) * RC_NPORT + channel];
346                 if (port->flags & ASYNC_INITIALIZED)  {
347                         return port;
348                 }
349         }
350         printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n", 
351                board_No(bp), what, channel);
352         return NULL;
353 }
354
355 static inline void rc_receive_exc(struct riscom_board const * bp)
356 {
357         struct riscom_port *port;
358         struct tty_struct *tty;
359         unsigned char status;
360         unsigned char ch;
361         
362         if (!(port = rc_get_port(bp, "Receive")))
363                 return;
364
365         tty = port->tty;
366         if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {
367                 printk(KERN_WARNING "rc%d: port %d: Working around flip "
368                                     "buffer overflow.\n",
369                        board_No(bp), port_No(port));
370                 return;
371         }
372         
373 #ifdef RC_REPORT_OVERRUN        
374         status = rc_in(bp, CD180_RCSR);
375         if (status & RCSR_OE)  {
376                 port->overrun++;
377 #if 0           
378                 printk(KERN_ERR "rc%d: port %d: Overrun. Total %ld overruns\n", 
379                        board_No(bp), port_No(port), port->overrun);
380 #endif          
381         }
382         status &= port->mark_mask;
383 #else   
384         status = rc_in(bp, CD180_RCSR) & port->mark_mask;
385 #endif  
386         ch = rc_in(bp, CD180_RDR);
387         if (!status)  {
388                 return;
389         }
390         if (status & RCSR_TOUT)  {
391                 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
392                                     "Hardware problems ?\n", 
393                        board_No(bp), port_No(port));
394                 return;
395                 
396         } else if (status & RCSR_BREAK)  {
397                 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
398                        board_No(bp), port_No(port));
399                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
400                 if (port->flags & ASYNC_SAK)
401                         do_SAK(tty);
402                 
403         } else if (status & RCSR_PE) 
404                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
405         
406         else if (status & RCSR_FE) 
407                 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
408         
409         else if (status & RCSR_OE)
410                 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
411         
412         else
413                 *tty->flip.flag_buf_ptr++ = 0;
414         
415         *tty->flip.char_buf_ptr++ = ch;
416         tty->flip.count++;
417         schedule_delayed_work(&tty->flip.work, 1);
418 }
419
420 static inline void rc_receive(struct riscom_board const * bp)
421 {
422         struct riscom_port *port;
423         struct tty_struct *tty;
424         unsigned char count;
425         
426         if (!(port = rc_get_port(bp, "Receive")))
427                 return;
428         
429         tty = port->tty;
430         
431         count = rc_in(bp, CD180_RDCR);
432         
433 #ifdef RC_REPORT_FIFO
434         port->hits[count > 8 ? 9 : count]++;
435 #endif  
436         
437         while (count--)  {
438                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {
439                         printk(KERN_WARNING "rc%d: port %d: Working around "
440                                             "flip buffer overflow.\n",
441                                board_No(bp), port_No(port));
442                         break;
443                 }
444                 *tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);
445                 *tty->flip.flag_buf_ptr++ = 0;
446                 tty->flip.count++;
447         }
448         schedule_delayed_work(&tty->flip.work, 1);
449 }
450
451 static inline void rc_transmit(struct riscom_board const * bp)
452 {
453         struct riscom_port *port;
454         struct tty_struct *tty;
455         unsigned char count;
456         
457         
458         if (!(port = rc_get_port(bp, "Transmit")))
459                 return;
460         
461         tty = port->tty;
462         
463         if (port->IER & IER_TXEMPTY)  {
464                 /* FIFO drained */
465                 rc_out(bp, CD180_CAR, port_No(port));
466                 port->IER &= ~IER_TXEMPTY;
467                 rc_out(bp, CD180_IER, port->IER);
468                 return;
469         }
470         
471         if ((port->xmit_cnt <= 0 && !port->break_length)
472             || tty->stopped || tty->hw_stopped)  {
473                 rc_out(bp, CD180_CAR, port_No(port));
474                 port->IER &= ~IER_TXRDY;
475                 rc_out(bp, CD180_IER, port->IER);
476                 return;
477         }
478         
479         if (port->break_length)  {
480                 if (port->break_length > 0)  {
481                         if (port->COR2 & COR2_ETC)  {
482                                 rc_out(bp, CD180_TDR, CD180_C_ESC);
483                                 rc_out(bp, CD180_TDR, CD180_C_SBRK);
484                                 port->COR2 &= ~COR2_ETC;
485                         }
486                         count = MIN(port->break_length, 0xff);
487                         rc_out(bp, CD180_TDR, CD180_C_ESC);
488                         rc_out(bp, CD180_TDR, CD180_C_DELAY);
489                         rc_out(bp, CD180_TDR, count);
490                         if (!(port->break_length -= count))
491                                 port->break_length--;
492                 } else  {
493                         rc_out(bp, CD180_TDR, CD180_C_ESC);
494                         rc_out(bp, CD180_TDR, CD180_C_EBRK);
495                         rc_out(bp, CD180_COR2, port->COR2);
496                         rc_wait_CCR(bp);
497                         rc_out(bp, CD180_CCR, CCR_CORCHG2);
498                         port->break_length = 0;
499                 }
500                 return;
501         }
502         
503         count = CD180_NFIFO;
504         do {
505                 rc_out(bp, CD180_TDR, port->xmit_buf[port->xmit_tail++]);
506                 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
507                 if (--port->xmit_cnt <= 0)
508                         break;
509         } while (--count > 0);
510         
511         if (port->xmit_cnt <= 0)  {
512                 rc_out(bp, CD180_CAR, port_No(port));
513                 port->IER &= ~IER_TXRDY;
514                 rc_out(bp, CD180_IER, port->IER);
515         }
516         if (port->xmit_cnt <= port->wakeup_chars)
517                 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
518 }
519
520 static inline void rc_check_modem(struct riscom_board const * bp)
521 {
522         struct riscom_port *port;
523         struct tty_struct *tty;
524         unsigned char mcr;
525         
526         if (!(port = rc_get_port(bp, "Modem")))
527                 return;
528         
529         tty = port->tty;
530         
531         mcr = rc_in(bp, CD180_MCR);
532         if (mcr & MCR_CDCHG)  {
533                 if (rc_in(bp, CD180_MSVR) & MSVR_CD) 
534                         wake_up_interruptible(&port->open_wait);
535                 else
536                         schedule_work(&port->tqueue_hangup);
537         }
538         
539 #ifdef RISCOM_BRAIN_DAMAGED_CTS
540         if (mcr & MCR_CTSCHG)  {
541                 if (rc_in(bp, CD180_MSVR) & MSVR_CTS)  {
542                         tty->hw_stopped = 0;
543                         port->IER |= IER_TXRDY;
544                         if (port->xmit_cnt <= port->wakeup_chars)
545                                 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
546                 } else  {
547                         tty->hw_stopped = 1;
548                         port->IER &= ~IER_TXRDY;
549                 }
550                 rc_out(bp, CD180_IER, port->IER);
551         }
552         if (mcr & MCR_DSRCHG)  {
553                 if (rc_in(bp, CD180_MSVR) & MSVR_DSR)  {
554                         tty->hw_stopped = 0;
555                         port->IER |= IER_TXRDY;
556                         if (port->xmit_cnt <= port->wakeup_chars)
557                                 rc_mark_event(port, RS_EVENT_WRITE_WAKEUP);
558                 } else  {
559                         tty->hw_stopped = 1;
560                         port->IER &= ~IER_TXRDY;
561                 }
562                 rc_out(bp, CD180_IER, port->IER);
563         }
564 #endif /* RISCOM_BRAIN_DAMAGED_CTS */
565         
566         /* Clear change bits */
567         rc_out(bp, CD180_MCR, 0);
568 }
569
570 /* The main interrupt processing routine */
571 static irqreturn_t rc_interrupt(int irq, void * dev_id, struct pt_regs * regs)
572 {
573         unsigned char status;
574         unsigned char ack;
575         struct riscom_board *bp;
576         unsigned long loop = 0;
577         int handled = 0;
578
579         bp = IRQ_to_board[irq];
580         
581         if (!bp || !(bp->flags & RC_BOARD_ACTIVE))  {
582                 return IRQ_NONE;
583         }
584         
585         while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
586                                  (RC_BSR_TOUT | RC_BSR_TINT |
587                                   RC_BSR_MINT | RC_BSR_RINT))) {
588                 handled = 1;
589                 if (status & RC_BSR_TOUT) 
590                         printk(KERN_WARNING "rc%d: Got timeout. Hardware "
591                                             "error?\n", board_No(bp));
592                 
593                 else if (status & RC_BSR_RINT) {
594                         ack = rc_in(bp, RC_ACK_RINT);
595                 
596                         if (ack == (RC_ID | GIVR_IT_RCV))
597                                 rc_receive(bp);
598                         else if (ack == (RC_ID | GIVR_IT_REXC))
599                                 rc_receive_exc(bp);
600                         else
601                                 printk(KERN_WARNING "rc%d: Bad receive ack "
602                                                     "0x%02x.\n",
603                                        board_No(bp), ack);
604                 
605                 } else if (status & RC_BSR_TINT) {
606                         ack = rc_in(bp, RC_ACK_TINT);
607                 
608                         if (ack == (RC_ID | GIVR_IT_TX))
609                                 rc_transmit(bp);
610                         else
611                                 printk(KERN_WARNING "rc%d: Bad transmit ack "
612                                                     "0x%02x.\n",
613                                        board_No(bp), ack);
614                 
615                 } else /* if (status & RC_BSR_MINT) */ {
616                         ack = rc_in(bp, RC_ACK_MINT);
617                 
618                         if (ack == (RC_ID | GIVR_IT_MODEM)) 
619                                 rc_check_modem(bp);
620                         else
621                                 printk(KERN_WARNING "rc%d: Bad modem ack "
622                                                     "0x%02x.\n",
623                                        board_No(bp), ack);
624                 
625                 } 
626
627                 rc_out(bp, CD180_EOIR, 0);   /* Mark end of interrupt */
628                 rc_out(bp, RC_CTOUT, 0);     /* Clear timeout flag    */
629         }
630         return IRQ_RETVAL(handled);
631 }
632
633 /*
634  *  Routines for open & close processing.
635  */
636
637 /* Called with disabled interrupts */
638 static inline int rc_setup_board(struct riscom_board * bp)
639 {
640         int error;
641
642         if (bp->flags & RC_BOARD_ACTIVE) 
643                 return 0;
644         
645         error = request_irq(bp->irq, rc_interrupt, SA_INTERRUPT,
646                             "RISCom/8", NULL);
647         if (error) 
648                 return error;
649         
650         rc_out(bp, RC_CTOUT, 0);                /* Just in case         */
651         bp->DTR = ~0;
652         rc_out(bp, RC_DTR, bp->DTR);            /* Drop DTR on all ports */
653         
654         IRQ_to_board[bp->irq] = bp;
655         bp->flags |= RC_BOARD_ACTIVE;
656         
657         return 0;
658 }
659
660 /* Called with disabled interrupts */
661 static inline void rc_shutdown_board(struct riscom_board *bp)
662 {
663         if (!(bp->flags & RC_BOARD_ACTIVE))
664                 return;
665         
666         bp->flags &= ~RC_BOARD_ACTIVE;
667         
668         free_irq(bp->irq, NULL);
669         IRQ_to_board[bp->irq] = NULL;
670         
671         bp->DTR = ~0;
672         rc_out(bp, RC_DTR, bp->DTR);           /* Drop DTR on all ports */
673         
674 }
675
676 /*
677  * Setting up port characteristics. 
678  * Must be called with disabled interrupts
679  */
680 static void rc_change_speed(struct riscom_board *bp, struct riscom_port *port)
681 {
682         struct tty_struct *tty;
683         unsigned long baud;
684         long tmp;
685         unsigned char cor1 = 0, cor3 = 0;
686         unsigned char mcor1 = 0, mcor2 = 0;
687         
688         if (!(tty = port->tty) || !tty->termios)
689                 return;
690
691         port->IER  = 0;
692         port->COR2 = 0;
693         port->MSVR = MSVR_RTS;
694         
695         baud = C_BAUD(tty);
696         
697         if (baud & CBAUDEX) {
698                 baud &= ~CBAUDEX;
699                 if (baud < 1 || baud > 2) 
700                         port->tty->termios->c_cflag &= ~CBAUDEX;
701                 else
702                         baud += 15;
703         }
704         if (baud == 15)  {
705                 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
706                         baud ++;
707                 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
708                         baud += 2;
709         }
710         
711         /* Select port on the board */
712         rc_out(bp, CD180_CAR, port_No(port));
713         
714         if (!baud_table[baud])  {
715                 /* Drop DTR & exit */
716                 bp->DTR |= (1u << port_No(port));
717                 rc_out(bp, RC_DTR, bp->DTR);
718                 return;
719         } else  {
720                 /* Set DTR on */
721                 bp->DTR &= ~(1u << port_No(port));
722                 rc_out(bp, RC_DTR, bp->DTR);
723         }
724         
725         /*
726          * Now we must calculate some speed depended things 
727          */
728         
729         /* Set baud rate for port */
730         tmp = (((RC_OSCFREQ + baud_table[baud]/2) / baud_table[baud] +
731                 CD180_TPC/2) / CD180_TPC);
732
733         rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff); 
734         rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff); 
735         rc_out(bp, CD180_RBPRL, tmp & 0xff); 
736         rc_out(bp, CD180_TBPRL, tmp & 0xff);
737         
738         baud = (baud_table[baud] + 5) / 10;   /* Estimated CPS */
739         
740         /* Two timer ticks seems enough to wakeup something like SLIP driver */
741         tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;           
742         port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
743                                               SERIAL_XMIT_SIZE - 1 : tmp);
744         
745         /* Receiver timeout will be transmission time for 1.5 chars */
746         tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
747         tmp = (tmp > 0xff) ? 0xff : tmp;
748         rc_out(bp, CD180_RTPR, tmp);
749         
750         switch (C_CSIZE(tty))  {
751          case CS5:
752                 cor1 |= COR1_5BITS;
753                 break;
754          case CS6:
755                 cor1 |= COR1_6BITS;
756                 break;
757          case CS7:
758                 cor1 |= COR1_7BITS;
759                 break;
760          case CS8:
761                 cor1 |= COR1_8BITS;
762                 break;
763         }
764         
765         if (C_CSTOPB(tty)) 
766                 cor1 |= COR1_2SB;
767         
768         cor1 |= COR1_IGNORE;
769         if (C_PARENB(tty))  {
770                 cor1 |= COR1_NORMPAR;
771                 if (C_PARODD(tty)) 
772                         cor1 |= COR1_ODDP;
773                 if (I_INPCK(tty)) 
774                         cor1 &= ~COR1_IGNORE;
775         }
776         /* Set marking of some errors */
777         port->mark_mask = RCSR_OE | RCSR_TOUT;
778         if (I_INPCK(tty)) 
779                 port->mark_mask |= RCSR_FE | RCSR_PE;
780         if (I_BRKINT(tty) || I_PARMRK(tty)) 
781                 port->mark_mask |= RCSR_BREAK;
782         if (I_IGNPAR(tty)) 
783                 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
784         if (I_IGNBRK(tty))  {
785                 port->mark_mask &= ~RCSR_BREAK;
786                 if (I_IGNPAR(tty)) 
787                         /* Real raw mode. Ignore all */
788                         port->mark_mask &= ~RCSR_OE;
789         }
790         /* Enable Hardware Flow Control */
791         if (C_CRTSCTS(tty))  {
792 #ifdef RISCOM_BRAIN_DAMAGED_CTS
793                 port->IER |= IER_DSR | IER_CTS;
794                 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
795                 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
796                 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
797 #else
798                 port->COR2 |= COR2_CTSAE;
799 #endif
800         }
801         /* Enable Software Flow Control. FIXME: I'm not sure about this */
802         /* Some people reported that it works, but I still doubt */
803         if (I_IXON(tty))  {
804                 port->COR2 |= COR2_TXIBE;
805                 cor3 |= (COR3_FCT | COR3_SCDE);
806                 if (I_IXANY(tty))
807                         port->COR2 |= COR2_IXM;
808                 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
809                 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
810                 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
811                 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
812         }
813         if (!C_CLOCAL(tty))  {
814                 /* Enable CD check */
815                 port->IER |= IER_CD;
816                 mcor1 |= MCOR1_CDZD;
817                 mcor2 |= MCOR2_CDOD;
818         }
819         
820         if (C_CREAD(tty)) 
821                 /* Enable receiver */
822                 port->IER |= IER_RXD;
823         
824         /* Set input FIFO size (1-8 bytes) */
825         cor3 |= RISCOM_RXFIFO; 
826         /* Setting up CD180 channel registers */
827         rc_out(bp, CD180_COR1, cor1);
828         rc_out(bp, CD180_COR2, port->COR2);
829         rc_out(bp, CD180_COR3, cor3);
830         /* Make CD180 know about registers change */
831         rc_wait_CCR(bp);
832         rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
833         /* Setting up modem option registers */
834         rc_out(bp, CD180_MCOR1, mcor1);
835         rc_out(bp, CD180_MCOR2, mcor2);
836         /* Enable CD180 transmitter & receiver */
837         rc_wait_CCR(bp);
838         rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
839         /* Enable interrupts */
840         rc_out(bp, CD180_IER, port->IER);
841         /* And finally set RTS on */
842         rc_out(bp, CD180_MSVR, port->MSVR);
843 }
844
845 /* Must be called with interrupts enabled */
846 static int rc_setup_port(struct riscom_board *bp, struct riscom_port *port)
847 {
848         unsigned long flags;
849         
850         if (port->flags & ASYNC_INITIALIZED)
851                 return 0;
852         
853         if (!port->xmit_buf) {
854                 /* We may sleep in get_zeroed_page() */
855                 unsigned long tmp;
856                 
857                 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
858                         return -ENOMEM;
859                     
860                 if (port->xmit_buf) {
861                         free_page(tmp);
862                         return -ERESTARTSYS;
863                 }
864                 port->xmit_buf = (unsigned char *) tmp;
865         }
866                 
867         save_flags(flags); cli();
868                 
869         if (port->tty) 
870                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
871                 
872         if (port->count == 1) 
873                 bp->count++;
874                 
875         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
876         rc_change_speed(bp, port);
877         port->flags |= ASYNC_INITIALIZED;
878                 
879         restore_flags(flags);
880         return 0;
881 }
882
883 /* Must be called with interrupts disabled */
884 static void rc_shutdown_port(struct riscom_board *bp, struct riscom_port *port)
885 {
886         struct tty_struct *tty;
887         
888         if (!(port->flags & ASYNC_INITIALIZED)) 
889                 return;
890         
891 #ifdef RC_REPORT_OVERRUN
892         printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
893                board_No(bp), port_No(port), port->overrun);
894 #endif  
895 #ifdef RC_REPORT_FIFO
896         {
897                 int i;
898                 
899                 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
900                        board_No(bp), port_No(port));
901                 for (i = 0; i < 10; i++)  {
902                         printk("%ld ", port->hits[i]);
903                 }
904                 printk("].\n");
905         }
906 #endif  
907         if (port->xmit_buf)  {
908                 free_page((unsigned long) port->xmit_buf);
909                 port->xmit_buf = NULL;
910         }
911
912         if (!(tty = port->tty) || C_HUPCL(tty))  {
913                 /* Drop DTR */
914                 bp->DTR |= (1u << port_No(port));
915                 rc_out(bp, RC_DTR, bp->DTR);
916         }
917         
918         /* Select port */
919         rc_out(bp, CD180_CAR, port_No(port));
920         /* Reset port */
921         rc_wait_CCR(bp);
922         rc_out(bp, CD180_CCR, CCR_SOFTRESET);
923         /* Disable all interrupts from this port */
924         port->IER = 0;
925         rc_out(bp, CD180_IER, port->IER);
926         
927         if (tty)  
928                 set_bit(TTY_IO_ERROR, &tty->flags);
929         port->flags &= ~ASYNC_INITIALIZED;
930         
931         if (--bp->count < 0)  {
932                 printk(KERN_INFO "rc%d: rc_shutdown_port: "
933                                  "bad board count: %d\n",
934                        board_No(bp), bp->count);
935                 bp->count = 0;
936         }
937         
938         /*
939          * If this is the last opened port on the board
940          * shutdown whole board
941          */
942         if (!bp->count) 
943                 rc_shutdown_board(bp);
944 }
945
946         
947 static int block_til_ready(struct tty_struct *tty, struct file * filp,
948                            struct riscom_port *port)
949 {
950         DECLARE_WAITQUEUE(wait, current);
951         struct riscom_board *bp = port_Board(port);
952         int    retval;
953         int    do_clocal = 0;
954         int    CD;
955
956         /*
957          * If the device is in the middle of being closed, then block
958          * until it's done, and then try again.
959          */
960         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
961                 interruptible_sleep_on(&port->close_wait);
962                 if (port->flags & ASYNC_HUP_NOTIFY)
963                         return -EAGAIN;
964                 else
965                         return -ERESTARTSYS;
966         }
967
968         /*
969          * If non-blocking mode is set, or the port is not enabled,
970          * then make the check up front and then exit.
971          */
972         if ((filp->f_flags & O_NONBLOCK) ||
973             (tty->flags & (1 << TTY_IO_ERROR))) {
974                 port->flags |= ASYNC_NORMAL_ACTIVE;
975                 return 0;
976         }
977
978         if (C_CLOCAL(tty))  
979                 do_clocal = 1;
980
981         /*
982          * Block waiting for the carrier detect and the line to become
983          * free (i.e., not in use by the callout).  While we are in
984          * this loop, info->count is dropped by one, so that
985          * rs_close() knows when to free things.  We restore it upon
986          * exit, either normal or abnormal.
987          */
988         retval = 0;
989         add_wait_queue(&port->open_wait, &wait);
990         cli();
991         if (!tty_hung_up_p(filp))
992                 port->count--;
993         sti();
994         port->blocked_open++;
995         while (1) {
996                 cli();
997                 rc_out(bp, CD180_CAR, port_No(port));
998                 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
999                 rc_out(bp, CD180_MSVR, MSVR_RTS);
1000                 bp->DTR &= ~(1u << port_No(port));
1001                 rc_out(bp, RC_DTR, bp->DTR);
1002                 sti();
1003                 set_current_state(TASK_INTERRUPTIBLE);
1004                 if (tty_hung_up_p(filp) ||
1005                     !(port->flags & ASYNC_INITIALIZED)) {
1006                         if (port->flags & ASYNC_HUP_NOTIFY)
1007                                 retval = -EAGAIN;
1008                         else
1009                                 retval = -ERESTARTSYS;  
1010                         break;
1011                 }
1012                 if (!(port->flags & ASYNC_CLOSING) &&
1013                     (do_clocal || CD))
1014                         break;
1015                 if (signal_pending(current)) {
1016                         retval = -ERESTARTSYS;
1017                         break;
1018                 }
1019                 schedule();
1020         }
1021         current->state = TASK_RUNNING;
1022         remove_wait_queue(&port->open_wait, &wait);
1023         if (!tty_hung_up_p(filp))
1024                 port->count++;
1025         port->blocked_open--;
1026         if (retval)
1027                 return retval;
1028         
1029         port->flags |= ASYNC_NORMAL_ACTIVE;
1030         return 0;
1031 }       
1032
1033 static int rc_open(struct tty_struct * tty, struct file * filp)
1034 {
1035         int board;
1036         int error;
1037         struct riscom_port * port;
1038         struct riscom_board * bp;
1039         
1040         board = RC_BOARD(tty->index);
1041         if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
1042                 return -ENODEV;
1043         
1044         bp = &rc_board[board];
1045         port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
1046         if (rc_paranoia_check(port, tty->name, "rc_open"))
1047                 return -ENODEV;
1048         
1049         if ((error = rc_setup_board(bp))) 
1050                 return error;
1051                 
1052         port->count++;
1053         tty->driver_data = port;
1054         port->tty = tty;
1055         
1056         if ((error = rc_setup_port(bp, port))) 
1057                 return error;
1058         
1059         if ((error = block_til_ready(tty, filp, port)))
1060                 return error;
1061         
1062         return 0;
1063 }
1064
1065 static void rc_close(struct tty_struct * tty, struct file * filp)
1066 {
1067         struct riscom_port *port = (struct riscom_port *) tty->driver_data;
1068         struct riscom_board *bp;
1069         unsigned long flags;
1070         unsigned long timeout;
1071         
1072         if (!port || rc_paranoia_check(port, tty->name, "close"))
1073                 return;
1074         
1075         save_flags(flags); cli();
1076         if (tty_hung_up_p(filp))
1077                 goto out;
1078         
1079         bp = port_Board(port);
1080         if ((tty->count == 1) && (port->count != 1))  {
1081                 printk(KERN_INFO "rc%d: rc_close: bad port count;"
1082                        " tty->count is 1, port count is %d\n",
1083                        board_No(bp), port->count);
1084                 port->count = 1;
1085         }
1086         if (--port->count < 0)  {
1087                 printk(KERN_INFO "rc%d: rc_close: bad port count "
1088                                  "for tty%d: %d\n",
1089                        board_No(bp), port_No(port), port->count);
1090                 port->count = 0;
1091         }
1092         if (port->count)
1093                 goto out;
1094         port->flags |= ASYNC_CLOSING;
1095         /*
1096          * Now we wait for the transmit buffer to clear; and we notify 
1097          * the line discipline to only process XON/XOFF characters.
1098          */
1099         tty->closing = 1;
1100         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1101                 tty_wait_until_sent(tty, port->closing_wait);
1102         /*
1103          * At this point we stop accepting input.  To do this, we
1104          * disable the receive line status interrupts, and tell the
1105          * interrupt driver to stop checking the data ready bit in the
1106          * line status register.
1107          */
1108         port->IER &= ~IER_RXD;
1109         if (port->flags & ASYNC_INITIALIZED) {
1110                 port->IER &= ~IER_TXRDY;
1111                 port->IER |= IER_TXEMPTY;
1112                 rc_out(bp, CD180_CAR, port_No(port));
1113                 rc_out(bp, CD180_IER, port->IER);
1114                 /*
1115                  * Before we drop DTR, make sure the UART transmitter
1116                  * has completely drained; this is especially
1117                  * important if there is a transmit FIFO!
1118                  */
1119                 timeout = jiffies+HZ;
1120                 while(port->IER & IER_TXEMPTY)  {
1121                         current->state = TASK_INTERRUPTIBLE;
1122                         schedule_timeout(port->timeout);
1123                         if (time_after(jiffies, timeout))
1124                                 break;
1125                 }
1126         }
1127         rc_shutdown_port(bp, port);
1128         if (tty->driver->flush_buffer)
1129                 tty->driver->flush_buffer(tty);
1130         if (tty->ldisc.flush_buffer)
1131                 tty->ldisc.flush_buffer(tty);
1132         tty->closing = 0;
1133         port->event = 0;
1134         port->tty = 0;
1135         if (port->blocked_open) {
1136                 if (port->close_delay) {
1137                         current->state = TASK_INTERRUPTIBLE;
1138                         schedule_timeout(port->close_delay);
1139                 }
1140                 wake_up_interruptible(&port->open_wait);
1141         }
1142         port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1143         wake_up_interruptible(&port->close_wait);
1144 out:    restore_flags(flags);
1145 }
1146
1147 static int rc_write(struct tty_struct * tty, int from_user, 
1148                     const unsigned char *buf, int count)
1149 {
1150         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1151         struct riscom_board *bp;
1152         int c, total = 0;
1153         unsigned long flags;
1154                                 
1155         if (rc_paranoia_check(port, tty->name, "rc_write"))
1156                 return 0;
1157         
1158         bp = port_Board(port);
1159
1160         if (!tty || !port->xmit_buf || !tmp_buf)
1161                 return 0;
1162
1163         save_flags(flags);
1164         if (from_user) {
1165                 down(&tmp_buf_sem);
1166                 while (1) {
1167                         cli();          
1168                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1169                                            SERIAL_XMIT_SIZE - port->xmit_head));
1170                         if (c <= 0)
1171                                 break;
1172
1173                         c -= copy_from_user(tmp_buf, buf, c);
1174                         if (!c) {
1175                                 if (!total)
1176                                         total = -EFAULT;
1177                                 break;
1178                         }
1179
1180                         cli();
1181                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1182                                        SERIAL_XMIT_SIZE - port->xmit_head));
1183                         memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1184                         port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1185                         port->xmit_cnt += c;
1186                         restore_flags(flags);
1187
1188                         buf += c;
1189                         count -= c;
1190                         total += c;
1191                 }
1192                 up(&tmp_buf_sem);
1193         } else {
1194                 while (1) {
1195                         cli();          
1196                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1197                                            SERIAL_XMIT_SIZE - port->xmit_head));
1198                         if (c <= 0) {
1199                                 restore_flags(flags);
1200                                 break;
1201                         }
1202
1203                         memcpy(port->xmit_buf + port->xmit_head, buf, c);
1204                         port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1205                         port->xmit_cnt += c;
1206                         restore_flags(flags);
1207
1208                         buf += c;
1209                         count -= c;
1210                         total += c;
1211                 }
1212         }
1213
1214         cli();
1215         if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1216             !(port->IER & IER_TXRDY)) {
1217                 port->IER |= IER_TXRDY;
1218                 rc_out(bp, CD180_CAR, port_No(port));
1219                 rc_out(bp, CD180_IER, port->IER);
1220         }
1221         restore_flags(flags);
1222
1223         return total;
1224 }
1225
1226 static void rc_put_char(struct tty_struct * tty, unsigned char ch)
1227 {
1228         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1229         unsigned long flags;
1230
1231         if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1232                 return;
1233
1234         if (!tty || !port->xmit_buf)
1235                 return;
1236
1237         save_flags(flags); cli();
1238         
1239         if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1240                 goto out;
1241
1242         port->xmit_buf[port->xmit_head++] = ch;
1243         port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1244         port->xmit_cnt++;
1245 out:    restore_flags(flags);
1246 }
1247
1248 static void rc_flush_chars(struct tty_struct * tty)
1249 {
1250         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1251         unsigned long flags;
1252                                 
1253         if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1254                 return;
1255         
1256         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1257             !port->xmit_buf)
1258                 return;
1259
1260         save_flags(flags); cli();
1261         port->IER |= IER_TXRDY;
1262         rc_out(port_Board(port), CD180_CAR, port_No(port));
1263         rc_out(port_Board(port), CD180_IER, port->IER);
1264         restore_flags(flags);
1265 }
1266
1267 static int rc_write_room(struct tty_struct * tty)
1268 {
1269         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1270         int     ret;
1271                                 
1272         if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1273                 return 0;
1274
1275         ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1276         if (ret < 0)
1277                 ret = 0;
1278         return ret;
1279 }
1280
1281 static int rc_chars_in_buffer(struct tty_struct *tty)
1282 {
1283         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1284                                 
1285         if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1286                 return 0;
1287         
1288         return port->xmit_cnt;
1289 }
1290
1291 static void rc_flush_buffer(struct tty_struct *tty)
1292 {
1293         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1294         unsigned long flags;
1295                                 
1296         if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
1297                 return;
1298
1299         save_flags(flags); cli();
1300         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1301         restore_flags(flags);
1302         
1303         wake_up_interruptible(&tty->write_wait);
1304         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1305             tty->ldisc.write_wakeup)
1306                 (tty->ldisc.write_wakeup)(tty);
1307 }
1308
1309 static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1310 {
1311         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1312         struct riscom_board * bp;
1313         unsigned char status;
1314         unsigned int result;
1315         unsigned long flags;
1316
1317         if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1318                 return -ENODEV;
1319
1320         bp = port_Board(port);
1321         save_flags(flags); cli();
1322         rc_out(bp, CD180_CAR, port_No(port));
1323         status = rc_in(bp, CD180_MSVR);
1324         result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1325         restore_flags(flags);
1326         result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1327                 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1328                 | ((status & MSVR_CD)  ? TIOCM_CAR : 0)
1329                 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1330                 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1331         return result;
1332 }
1333
1334 static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1335                        unsigned int set, unsigned int clear)
1336 {
1337         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1338         unsigned long flags;
1339         struct riscom_board *bp;
1340
1341         if (rc_paranoia_check(port, tty->name, __FUNCTION__))
1342                 return -ENODEV;
1343
1344         bp = port_Board(port);
1345
1346         save_flags(flags); cli();
1347         if (set & TIOCM_RTS)
1348                 port->MSVR |= MSVR_RTS;
1349         if (set & TIOCM_DTR)
1350                 bp->DTR &= ~(1u << port_No(port));
1351
1352         if (clear & TIOCM_RTS)
1353                 port->MSVR &= ~MSVR_RTS;
1354         if (clear & TIOCM_DTR)
1355                 bp->DTR |= (1u << port_No(port));
1356
1357         rc_out(bp, CD180_CAR, port_No(port));
1358         rc_out(bp, CD180_MSVR, port->MSVR);
1359         rc_out(bp, RC_DTR, bp->DTR);
1360         restore_flags(flags);
1361         return 0;
1362 }
1363
1364 static inline void rc_send_break(struct riscom_port * port, unsigned long length)
1365 {
1366         struct riscom_board *bp = port_Board(port);
1367         unsigned long flags;
1368         
1369         save_flags(flags); cli();
1370         port->break_length = RISCOM_TPS / HZ * length;
1371         port->COR2 |= COR2_ETC;
1372         port->IER  |= IER_TXRDY;
1373         rc_out(bp, CD180_CAR, port_No(port));
1374         rc_out(bp, CD180_COR2, port->COR2);
1375         rc_out(bp, CD180_IER, port->IER);
1376         rc_wait_CCR(bp);
1377         rc_out(bp, CD180_CCR, CCR_CORCHG2);
1378         rc_wait_CCR(bp);
1379         restore_flags(flags);
1380 }
1381
1382 static inline int rc_set_serial_info(struct riscom_port * port,
1383                                      struct serial_struct * newinfo)
1384 {
1385         struct serial_struct tmp;
1386         struct riscom_board *bp = port_Board(port);
1387         int change_speed;
1388         unsigned long flags;
1389         
1390         if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1391                 return -EFAULT;
1392         
1393 #if 0   
1394         if ((tmp.irq != bp->irq) ||
1395             (tmp.port != bp->base) ||
1396             (tmp.type != PORT_CIRRUS) ||
1397             (tmp.baud_base != (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC) ||
1398             (tmp.custom_divisor != 0) ||
1399             (tmp.xmit_fifo_size != CD180_NFIFO) ||
1400             (tmp.flags & ~RISCOM_LEGAL_FLAGS))
1401                 return -EINVAL;
1402 #endif  
1403         
1404         change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1405                         (tmp.flags & ASYNC_SPD_MASK));
1406         
1407         if (!capable(CAP_SYS_ADMIN)) {
1408                 if ((tmp.close_delay != port->close_delay) ||
1409                     (tmp.closing_wait != port->closing_wait) ||
1410                     ((tmp.flags & ~ASYNC_USR_MASK) !=
1411                      (port->flags & ~ASYNC_USR_MASK)))  
1412                         return -EPERM;
1413                 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1414                                (tmp.flags & ASYNC_USR_MASK));
1415         } else  {
1416                 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1417                                (tmp.flags & ASYNC_FLAGS));
1418                 port->close_delay = tmp.close_delay;
1419                 port->closing_wait = tmp.closing_wait;
1420         }
1421         if (change_speed)  {
1422                 save_flags(flags); cli();
1423                 rc_change_speed(bp, port);
1424                 restore_flags(flags);
1425         }
1426         return 0;
1427 }
1428
1429 static inline int rc_get_serial_info(struct riscom_port * port,
1430                                      struct serial_struct * retinfo)
1431 {
1432         struct serial_struct tmp;
1433         struct riscom_board *bp = port_Board(port);
1434         
1435         memset(&tmp, 0, sizeof(tmp));
1436         tmp.type = PORT_CIRRUS;
1437         tmp.line = port - rc_port;
1438         tmp.port = bp->base;
1439         tmp.irq  = bp->irq;
1440         tmp.flags = port->flags;
1441         tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1442         tmp.close_delay = port->close_delay * HZ/100;
1443         tmp.closing_wait = port->closing_wait * HZ/100;
1444         tmp.xmit_fifo_size = CD180_NFIFO;
1445         return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1446 }
1447
1448 static int rc_ioctl(struct tty_struct * tty, struct file * filp, 
1449                     unsigned int cmd, unsigned long arg)
1450                     
1451 {
1452         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1453         int retval;
1454                                 
1455         if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1456                 return -ENODEV;
1457         
1458         switch (cmd) {
1459          case TCSBRK:   /* SVID version: non-zero arg --> no break */
1460                 retval = tty_check_change(tty);
1461                 if (retval)
1462                         return retval;
1463                 tty_wait_until_sent(tty, 0);
1464                 if (!arg)
1465                         rc_send_break(port, HZ/4);      /* 1/4 second */
1466                 break;
1467          case TCSBRKP:  /* support for POSIX tcsendbreak() */
1468                 retval = tty_check_change(tty);
1469                 if (retval)
1470                         return retval;
1471                 tty_wait_until_sent(tty, 0);
1472                 rc_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1473                 break;
1474          case TIOCGSOFTCAR:
1475                 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned int *) arg);
1476          case TIOCSSOFTCAR:
1477                 if (get_user(arg,(unsigned int *) arg))
1478                         return -EFAULT;
1479                 tty->termios->c_cflag =
1480                         ((tty->termios->c_cflag & ~CLOCAL) |
1481                         (arg ? CLOCAL : 0));
1482                 break;
1483          case TIOCGSERIAL:      
1484                 return rc_get_serial_info(port, (struct serial_struct *) arg);
1485          case TIOCSSERIAL:      
1486                 return rc_set_serial_info(port, (struct serial_struct *) arg);
1487          default:
1488                 return -ENOIOCTLCMD;
1489         }
1490         return 0;
1491 }
1492
1493 static void rc_throttle(struct tty_struct * tty)
1494 {
1495         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1496         struct riscom_board *bp;
1497         unsigned long flags;
1498                                 
1499         if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1500                 return;
1501         
1502         bp = port_Board(port);
1503         
1504         save_flags(flags); cli();
1505         port->MSVR &= ~MSVR_RTS;
1506         rc_out(bp, CD180_CAR, port_No(port));
1507         if (I_IXOFF(tty))  {
1508                 rc_wait_CCR(bp);
1509                 rc_out(bp, CD180_CCR, CCR_SSCH2);
1510                 rc_wait_CCR(bp);
1511         }
1512         rc_out(bp, CD180_MSVR, port->MSVR);
1513         restore_flags(flags);
1514 }
1515
1516 static void rc_unthrottle(struct tty_struct * tty)
1517 {
1518         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1519         struct riscom_board *bp;
1520         unsigned long flags;
1521                                 
1522         if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1523                 return;
1524         
1525         bp = port_Board(port);
1526         
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_SSCH1);
1533                 rc_wait_CCR(bp);
1534         }
1535         rc_out(bp, CD180_MSVR, port->MSVR);
1536         restore_flags(flags);
1537 }
1538
1539 static void rc_stop(struct tty_struct * tty)
1540 {
1541         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1542         struct riscom_board *bp;
1543         unsigned long flags;
1544                                 
1545         if (rc_paranoia_check(port, tty->name, "rc_stop"))
1546                 return;
1547         
1548         bp = port_Board(port);
1549         
1550         save_flags(flags); cli();
1551         port->IER &= ~IER_TXRDY;
1552         rc_out(bp, CD180_CAR, port_No(port));
1553         rc_out(bp, CD180_IER, port->IER);
1554         restore_flags(flags);
1555 }
1556
1557 static void rc_start(struct tty_struct * tty)
1558 {
1559         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1560         struct riscom_board *bp;
1561         unsigned long flags;
1562                                 
1563         if (rc_paranoia_check(port, tty->name, "rc_start"))
1564                 return;
1565         
1566         bp = port_Board(port);
1567         
1568         save_flags(flags); cli();
1569         if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY))  {
1570                 port->IER |= IER_TXRDY;
1571                 rc_out(bp, CD180_CAR, port_No(port));
1572                 rc_out(bp, CD180_IER, port->IER);
1573         }
1574         restore_flags(flags);
1575 }
1576
1577 /*
1578  * This routine is called from the work queue when the interrupt
1579  * routine has signalled that a hangup has occurred.  The path of
1580  * hangup processing is:
1581  *
1582  *      serial interrupt routine -> (workqueue) ->
1583  *      do_rc_hangup() -> tty->hangup() -> rc_hangup()
1584  * 
1585  */
1586 static void do_rc_hangup(void *private_)
1587 {
1588         struct riscom_port      *port = (struct riscom_port *) private_;
1589         struct tty_struct       *tty;
1590         
1591         tty = port->tty;
1592         if (tty)
1593                 tty_hangup(tty);        /* FIXME: module removal race still here */
1594 }
1595
1596 static void rc_hangup(struct tty_struct * tty)
1597 {
1598         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1599         struct riscom_board *bp;
1600                                 
1601         if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1602                 return;
1603         
1604         bp = port_Board(port);
1605         
1606         rc_shutdown_port(bp, port);
1607         port->event = 0;
1608         port->count = 0;
1609         port->flags &= ~ASYNC_NORMAL_ACTIVE;
1610         port->tty = 0;
1611         wake_up_interruptible(&port->open_wait);
1612 }
1613
1614 static void rc_set_termios(struct tty_struct * tty, struct termios * old_termios)
1615 {
1616         struct riscom_port *port = (struct riscom_port *)tty->driver_data;
1617         unsigned long flags;
1618                                 
1619         if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1620                 return;
1621         
1622         if (tty->termios->c_cflag == old_termios->c_cflag &&
1623             tty->termios->c_iflag == old_termios->c_iflag)
1624                 return;
1625
1626         save_flags(flags); cli();
1627         rc_change_speed(port_Board(port), port);
1628         restore_flags(flags);
1629
1630         if ((old_termios->c_cflag & CRTSCTS) &&
1631             !(tty->termios->c_cflag & CRTSCTS)) {
1632                 tty->hw_stopped = 0;
1633                 rc_start(tty);
1634         }
1635 }
1636
1637 static void do_softint(void *private_)
1638 {
1639         struct riscom_port      *port = (struct riscom_port *) private_;
1640         struct tty_struct       *tty;
1641         
1642         if(!(tty = port->tty)) 
1643                 return;
1644
1645         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
1646                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1647                     tty->ldisc.write_wakeup)
1648                         (tty->ldisc.write_wakeup)(tty);
1649                 wake_up_interruptible(&tty->write_wait);
1650         }
1651 }
1652
1653 static struct tty_operations riscom_ops = {
1654         .open  = rc_open,
1655         .close = rc_close,
1656         .write = rc_write,
1657         .put_char = rc_put_char,
1658         .flush_chars = rc_flush_chars,
1659         .write_room = rc_write_room,
1660         .chars_in_buffer = rc_chars_in_buffer,
1661         .flush_buffer = rc_flush_buffer,
1662         .ioctl = rc_ioctl,
1663         .throttle = rc_throttle,
1664         .unthrottle = rc_unthrottle,
1665         .set_termios = rc_set_termios,
1666         .stop = rc_stop,
1667         .start = rc_start,
1668         .hangup = rc_hangup,
1669         .tiocmget = rc_tiocmget,
1670         .tiocmset = rc_tiocmset,
1671 };
1672
1673 static inline int rc_init_drivers(void)
1674 {
1675         int error;
1676         int i;
1677
1678         riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1679         if (!riscom_driver)     
1680                 return -ENOMEM;
1681         
1682         if (!(tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL))) {
1683                 printk(KERN_ERR "rc: Couldn't get free page.\n");
1684                 put_tty_driver(riscom_driver);
1685                 return 1;
1686         }
1687         memset(IRQ_to_board, 0, sizeof(IRQ_to_board));
1688         riscom_driver->owner = THIS_MODULE;
1689         riscom_driver->name = "ttyL";
1690         riscom_driver->devfs_name = "tts/L";
1691         riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1692         riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1693         riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1694         riscom_driver->init_termios = tty_std_termios;
1695         riscom_driver->init_termios.c_cflag =
1696                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1697         riscom_driver->flags = TTY_DRIVER_REAL_RAW;
1698         tty_set_operations(riscom_driver, &riscom_ops);
1699         if ((error = tty_register_driver(riscom_driver)))  {
1700                 free_page((unsigned long)tmp_buf);
1701                 put_tty_driver(riscom_driver);
1702                 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1703                                 "error = %d\n",
1704                        error);
1705                 return 1;
1706         }
1707
1708         memset(rc_port, 0, sizeof(rc_port));
1709         for (i = 0; i < RC_NPORT * RC_NBOARD; i++)  {
1710                 rc_port[i].magic = RISCOM8_MAGIC;
1711                 INIT_WORK(&rc_port[i].tqueue, do_softint, &rc_port[i]);
1712                 INIT_WORK(&rc_port[i].tqueue_hangup, do_rc_hangup, &rc_port[i]);
1713                 rc_port[i].close_delay = 50 * HZ/100;
1714                 rc_port[i].closing_wait = 3000 * HZ/100;
1715                 init_waitqueue_head(&rc_port[i].open_wait);
1716                 init_waitqueue_head(&rc_port[i].close_wait);
1717         }
1718         
1719         return 0;
1720 }
1721
1722 static void rc_release_drivers(void)
1723 {
1724         unsigned long flags;
1725
1726         save_flags(flags);
1727         cli();
1728         free_page((unsigned long)tmp_buf);
1729         tty_unregister_driver(riscom_driver);
1730         put_tty_driver(riscom_driver);
1731         restore_flags(flags);
1732 }
1733
1734 #ifndef MODULE
1735 /*
1736  * Called at boot time.
1737  * 
1738  * You can specify IO base for up to RC_NBOARD cards,
1739  * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1740  * Note that there will be no probing at default
1741  * addresses in this case.
1742  *
1743  */ 
1744 static int __init riscom8_setup(char *str)
1745 {
1746         int ints[RC_NBOARD];
1747         int i;
1748
1749         str = get_options(str, ARRAY_SIZE(ints), ints);
1750
1751         for (i = 0; i < RC_NBOARD; i++) {
1752                 if (i < ints[0])
1753                         rc_board[i].base = ints[i+1];
1754                 else 
1755                         rc_board[i].base = 0;
1756         }
1757         return 1;
1758 }
1759
1760 __setup("riscom8=", riscom8_setup);
1761 #endif
1762
1763 static char banner[] __initdata =
1764         KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1765                   "1994-1996.\n";
1766 static char no_boards_msg[] __initdata =
1767         KERN_INFO "rc: No RISCom/8 boards detected.\n";
1768
1769 /* 
1770  * This routine must be called by kernel at boot time 
1771  */
1772 static int __init riscom8_init(void)
1773 {
1774         int i;
1775         int found = 0;
1776
1777         printk(banner);
1778
1779         if (rc_init_drivers()) 
1780                 return -EIO;
1781
1782         for (i = 0; i < RC_NBOARD; i++) 
1783                 if (rc_board[i].base && !rc_probe(&rc_board[i]))  
1784                         found++;
1785         
1786         if (!found)  {
1787                 rc_release_drivers();
1788                 printk(no_boards_msg);
1789                 return -EIO;
1790         }
1791         return 0;
1792 }
1793
1794 #ifdef MODULE
1795 static int iobase;
1796 static int iobase1;
1797 static int iobase2;
1798 static int iobase3;
1799 MODULE_PARM(iobase, "i");
1800 MODULE_PARM(iobase1, "i");
1801 MODULE_PARM(iobase2, "i");
1802 MODULE_PARM(iobase3, "i");
1803
1804 MODULE_LICENSE("GPL");
1805 #endif /* MODULE */
1806
1807 /*
1808  * You can setup up to 4 boards (current value of RC_NBOARD)
1809  * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1810  *
1811  */
1812 static int __init riscom8_init_module (void)
1813 {
1814 #ifdef MODULE
1815         int i;
1816
1817         if (iobase || iobase1 || iobase2 || iobase3) {
1818                 for(i = 0; i < RC_NBOARD; i++)
1819                         rc_board[0].base = 0;
1820         }
1821
1822         if (iobase)
1823                 rc_board[0].base = iobase;
1824         if (iobase1)
1825                 rc_board[1].base = iobase1;
1826         if (iobase2)
1827                 rc_board[2].base = iobase2;
1828         if (iobase3)
1829                 rc_board[3].base = iobase3;
1830 #endif /* MODULE */
1831
1832         return riscom8_init();
1833 }
1834         
1835 static void __exit riscom8_exit_module (void)
1836 {
1837         int i;
1838         
1839         rc_release_drivers();
1840         for (i = 0; i < RC_NBOARD; i++)  
1841                 if (rc_board[i].flags & RC_BOARD_PRESENT) 
1842                         rc_release_io_range(&rc_board[i]);
1843         
1844 }
1845
1846 module_init(riscom8_init_module);
1847 module_exit(riscom8_exit_module);
1848