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