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