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