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