v2.4.9.8 -> v2.4.9.9
[opensuse:kernel.git] / drivers / char / amiserial.c
1 /*
2  *  linux/drivers/char/amiserial.c
3  *
4  * Serial driver for the amiga builtin port.
5  *
6  * This code was created by taking serial.c version 4.30 from kernel
7  * release 2.3.22, replacing all hardware related stuff with the
8  * corresponding amiga hardware actions, and removing all irrelevant
9  * code. As a consequence, it uses many of the constants and names
10  * associated with the registers and bits of 16550 compatible UARTS -
11  * but only to keep track of status, etc in the state variables. It
12  * was done this was to make it easier to keep the code in line with
13  * (non hardware specific) changes to serial.c.
14  *
15  * The port is registered with the tty driver as minor device 64, and
16  * therefore other ports should should only use 65 upwards.
17  *
18  * Richard Lucock 28/12/99
19  *
20  *  Copyright (C) 1991, 1992  Linus Torvalds
21  *  Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 
22  *              1998, 1999  Theodore Ts'o
23  *
24  */
25
26 /*
27  * Serial driver configuration section.  Here are the various options:
28  *
29  * SERIAL_PARANOIA_CHECK
30  *              Check the magic number for the async_structure where
31  *              ever possible.
32  */
33
34 #include <linux/config.h>
35 #include <linux/version.h>
36
37 #undef SERIAL_PARANOIA_CHECK
38 #define SERIAL_DO_RESTART
39
40 /* Set of debugging defines */
41
42 #undef SERIAL_DEBUG_INTR
43 #undef SERIAL_DEBUG_OPEN
44 #undef SERIAL_DEBUG_FLOW
45 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
46
47 /* Sanity checks */
48
49 #define SERIAL_INLINE
50   
51 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
52 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
53  kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
54 #else
55 #define DBG_CNT(s)
56 #endif
57
58 /*
59  * End of serial driver configuration section.
60  */
61
62 #include <linux/module.h>
63
64 #include <linux/types.h>
65 #include <linux/serial.h>
66 #include <linux/serialP.h>
67 #include <linux/serial_reg.h>
68 static char *serial_version = "4.30";
69
70 #include <linux/errno.h>
71 #include <linux/signal.h>
72 #include <linux/sched.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/console.h>
79 #include <linux/major.h>
80 #include <linux/string.h>
81 #include <linux/fcntl.h>
82 #include <linux/ptrace.h>
83 #include <linux/ioport.h>
84 #include <linux/mm.h>
85 #include <linux/slab.h>
86 #include <linux/init.h>
87 #include <linux/delay.h>
88
89 #include <asm/setup.h>
90
91 #include <asm/system.h>
92 #include <asm/io.h>
93 #include <asm/irq.h>
94 #include <asm/bitops.h>
95
96 #include <asm/amigahw.h>
97 #include <asm/amigaints.h>
98
99 #ifdef SERIAL_INLINE
100 #define _INLINE_ inline
101 #endif
102
103 static char *serial_name = "Amiga-builtin serial driver";
104
105 static DECLARE_TASK_QUEUE(tq_serial);
106
107 static struct tty_driver serial_driver, callout_driver;
108 static int serial_refcount;
109
110 /* serial subtype definitions */
111 #ifndef SERIAL_TYPE_NORMAL
112 #define SERIAL_TYPE_NORMAL      1
113 #define SERIAL_TYPE_CALLOUT     2
114 #endif
115
116 /* number of characters left in xmit buffer before we ask for more */
117 #define WAKEUP_CHARS 256
118
119 static struct async_struct *IRQ_ports;
120
121 static unsigned char current_ctl_bits;
122
123 static void change_speed(struct async_struct *info, struct termios *old);
124 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
125
126
127 static struct serial_state rs_table[1];
128
129 #define NR_PORTS        (sizeof(rs_table)/sizeof(struct serial_state))
130
131
132 static struct tty_struct *serial_table[NR_PORTS];
133 static struct termios *serial_termios[NR_PORTS];
134 static struct termios *serial_termios_locked[NR_PORTS];
135
136 #ifndef MIN
137 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
138 #endif
139
140 /*
141  * tmp_buf is used as a temporary buffer by serial_write.  We need to
142  * lock it in case the copy_from_user blocks while swapping in a page,
143  * and some other program tries to do a serial write at the same time.
144  * Since the lock will only come under contention when the system is
145  * swapping and available memory is low, it makes sense to share one
146  * buffer across all the serial ports, since it significantly saves
147  * memory if large numbers of serial ports are open.
148  */
149 static unsigned char *tmp_buf;
150 static DECLARE_MUTEX(tmp_buf_sem);
151
152 #include <asm/uaccess.h>
153
154 #define serial_isroot() (capable(CAP_SYS_ADMIN))
155
156
157 static inline int serial_paranoia_check(struct async_struct *info,
158                                         kdev_t device, const char *routine)
159 {
160 #ifdef SERIAL_PARANOIA_CHECK
161         static const char *badmagic =
162                 "Warning: bad magic number for serial struct (%s) in %s\n";
163         static const char *badinfo =
164                 "Warning: null async_struct for (%s) in %s\n";
165
166         if (!info) {
167                 printk(badinfo, kdevname(device), routine);
168                 return 1;
169         }
170         if (info->magic != SERIAL_MAGIC) {
171                 printk(badmagic, kdevname(device), routine);
172                 return 1;
173         }
174 #endif
175         return 0;
176 }
177
178 /* some serial hardware definitions */
179 #define SDR_OVRUN   (1<<15)
180 #define SDR_RBF     (1<<14)
181 #define SDR_TBE     (1<<13)
182 #define SDR_TSRE    (1<<12)
183
184 #define SERPER_PARENB    (1<<15)
185
186 #define AC_SETCLR   (1<<15)
187 #define AC_UARTBRK  (1<<11)
188
189 #define SER_DTR     (1<<7)
190 #define SER_RTS     (1<<6)
191 #define SER_DCD     (1<<5)
192 #define SER_CTS     (1<<4)
193 #define SER_DSR     (1<<3)
194
195 static __inline__ void rtsdtr_ctrl(int bits)
196 {
197     ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
198 }
199
200 /*
201  * ------------------------------------------------------------
202  * rs_stop() and rs_start()
203  *
204  * This routines are called before setting or resetting tty->stopped.
205  * They enable or disable transmitter interrupts, as necessary.
206  * ------------------------------------------------------------
207  */
208 static void rs_stop(struct tty_struct *tty)
209 {
210         struct async_struct *info = (struct async_struct *)tty->driver_data;
211         unsigned long flags;
212
213         if (serial_paranoia_check(info, tty->device, "rs_stop"))
214                 return;
215
216         save_flags(flags); cli();
217         if (info->IER & UART_IER_THRI) {
218                 info->IER &= ~UART_IER_THRI;
219                 /* disable Tx interrupt and remove any pending interrupts */
220                 custom.intena = IF_TBE;
221                 mb();
222                 custom.intreq = IF_TBE;
223                 mb();
224         }
225         restore_flags(flags);
226 }
227
228 static void rs_start(struct tty_struct *tty)
229 {
230         struct async_struct *info = (struct async_struct *)tty->driver_data;
231         unsigned long flags;
232
233         if (serial_paranoia_check(info, tty->device, "rs_start"))
234                 return;
235
236         save_flags(flags); cli();
237         if (info->xmit.head != info->xmit.tail
238             && info->xmit.buf
239             && !(info->IER & UART_IER_THRI)) {
240                 info->IER |= UART_IER_THRI;
241                 custom.intena = IF_SETCLR | IF_TBE;
242                 mb();
243                 /* set a pending Tx Interrupt, transmitter should restart now */
244                 custom.intreq = IF_SETCLR | IF_TBE;
245                 mb();
246         }
247         restore_flags(flags);
248 }
249
250 /*
251  * ----------------------------------------------------------------------
252  *
253  * Here starts the interrupt handling routines.  All of the following
254  * subroutines are declared as inline and are folded into
255  * rs_interrupt().  They were separated out for readability's sake.
256  *
257  * Note: rs_interrupt() is a "fast" interrupt, which means that it
258  * runs with interrupts turned off.  People who may want to modify
259  * rs_interrupt() should try to keep the interrupt handler as fast as
260  * possible.  After you are done making modifications, it is not a bad
261  * idea to do:
262  * 
263  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
264  *
265  * and look at the resulting assemble code in serial.s.
266  *
267  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
268  * -----------------------------------------------------------------------
269  */
270
271 /*
272  * This routine is used by the interrupt handler to schedule
273  * processing in the software interrupt portion of the driver.
274  */
275 static _INLINE_ void rs_sched_event(struct async_struct *info,
276                                   int event)
277 {
278         info->event |= 1 << event;
279         queue_task(&info->tqueue, &tq_serial);
280         mark_bh(SERIAL_BH);
281 }
282
283 static _INLINE_ void receive_chars(struct async_struct *info)
284 {
285         int status;
286         int serdatr;
287         struct tty_struct *tty = info->tty;
288         unsigned char ch;
289         struct  async_icount *icount;
290
291         icount = &info->state->icount;
292
293         status = UART_LSR_DR; /* We obviously have a character! */
294         serdatr = custom.serdatr;
295         mb();
296         custom.intreq = IF_RBF;
297         mb();
298
299         if((serdatr & 0x1ff) == 0)
300             status |= UART_LSR_BI;
301         if(serdatr & SDR_OVRUN)
302             status |= UART_LSR_OE;
303
304         ch = serdatr & 0xff;
305         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
306           goto ignore_char;
307         *tty->flip.char_buf_ptr = ch;
308         icount->rx++;
309
310 #ifdef SERIAL_DEBUG_INTR
311         printk("DR%02x:%02x...", ch, status);
312 #endif
313         *tty->flip.flag_buf_ptr = 0;
314
315         /*
316          * We don't handle parity or frame errors - but I have left
317          * the code in, since I'm not sure that the errors can't be
318          * detected.
319          */
320
321         if (status & (UART_LSR_BI | UART_LSR_PE |
322                       UART_LSR_FE | UART_LSR_OE)) {
323           /*
324            * For statistics only
325            */
326           if (status & UART_LSR_BI) {
327             status &= ~(UART_LSR_FE | UART_LSR_PE);
328             icount->brk++;
329           } else if (status & UART_LSR_PE)
330             icount->parity++;
331           else if (status & UART_LSR_FE)
332             icount->frame++;
333           if (status & UART_LSR_OE)
334             icount->overrun++;
335
336           /*
337            * Now check to see if character should be
338            * ignored, and mask off conditions which
339            * should be ignored.
340            */
341           if (status & info->ignore_status_mask)
342             goto ignore_char;
343
344           status &= info->read_status_mask;
345
346           if (status & (UART_LSR_BI)) {
347 #ifdef SERIAL_DEBUG_INTR
348             printk("handling break....");
349 #endif
350             *tty->flip.flag_buf_ptr = TTY_BREAK;
351             if (info->flags & ASYNC_SAK)
352               do_SAK(tty);
353           } else if (status & UART_LSR_PE)
354             *tty->flip.flag_buf_ptr = TTY_PARITY;
355           else if (status & UART_LSR_FE)
356             *tty->flip.flag_buf_ptr = TTY_FRAME;
357           if (status & UART_LSR_OE) {
358             /*
359              * Overrun is special, since it's
360              * reported immediately, and doesn't
361              * affect the current character
362              */
363             if (tty->flip.count < TTY_FLIPBUF_SIZE) {
364               tty->flip.count++;
365               tty->flip.flag_buf_ptr++;
366               tty->flip.char_buf_ptr++;
367               *tty->flip.flag_buf_ptr = TTY_OVERRUN;
368             }
369           }
370         }
371         tty->flip.flag_buf_ptr++;
372         tty->flip.char_buf_ptr++;
373         tty->flip.count++;
374  ignore_char:
375
376         tty_flip_buffer_push(tty);
377 }
378
379 static _INLINE_ void transmit_chars(struct async_struct *info)
380 {
381         custom.intreq = IF_TBE;
382         mb();
383         if (info->x_char) {
384                 custom.serdat = info->x_char | 0x100;
385                 mb();
386                 info->state->icount.tx++;
387                 info->x_char = 0;
388                 return;
389         }
390         if (info->xmit.head == info->xmit.tail
391             || info->tty->stopped
392             || info->tty->hw_stopped) {
393                 info->IER &= ~UART_IER_THRI;
394                 custom.intena = IF_TBE;
395                 mb();
396                 return;
397         }
398
399         custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
400         mb();
401         info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
402         info->state->icount.tx++;
403
404         if (CIRC_CNT(info->xmit.head,
405                      info->xmit.tail,
406                      SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
407                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
408
409 #ifdef SERIAL_DEBUG_INTR
410         printk("THRE...");
411 #endif
412         if (info->xmit.head == info->xmit.tail) {
413                 custom.intena = IF_TBE;
414                 mb();
415                 info->IER &= ~UART_IER_THRI;
416         }
417 }
418
419 static _INLINE_ void check_modem_status(struct async_struct *info)
420 {
421         unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
422         unsigned char dstatus;
423         struct  async_icount *icount;
424
425         /* Determine bits that have changed */
426         dstatus = status ^ current_ctl_bits;
427         current_ctl_bits = status;
428
429         if (dstatus) {
430                 icount = &info->state->icount;
431                 /* update input line counters */
432                 if (dstatus & SER_DSR)
433                         icount->dsr++;
434                 if (dstatus & SER_DCD) {
435                         icount->dcd++;
436 #ifdef CONFIG_HARD_PPS
437                         if ((info->flags & ASYNC_HARDPPS_CD) &&
438                             !(status & SER_DCD))
439                                 hardpps();
440 #endif
441                 }
442                 if (dstatus & SER_CTS)
443                         icount->cts++;
444                 wake_up_interruptible(&info->delta_msr_wait);
445         }
446
447         if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
448 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
449                 printk("ttyS%02d CD now %s...", info->line,
450                        (!(status & SER_DCD)) ? "on" : "off");
451 #endif
452                 if (!(status & SER_DCD))
453                         wake_up_interruptible(&info->open_wait);
454                 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
455                            (info->flags & ASYNC_CALLOUT_NOHUP))) {
456 #ifdef SERIAL_DEBUG_OPEN
457                         printk("doing serial hangup...");
458 #endif
459                         if (info->tty)
460                                 tty_hangup(info->tty);
461                 }
462         }
463         if (info->flags & ASYNC_CTS_FLOW) {
464                 if (info->tty->hw_stopped) {
465                         if (!(status & SER_CTS)) {
466 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
467                                 printk("CTS tx start...");
468 #endif
469                                 info->tty->hw_stopped = 0;
470                                 info->IER |= UART_IER_THRI;
471                                 custom.intena = IF_SETCLR | IF_TBE;
472                                 mb();
473                                 /* set a pending Tx Interrupt, transmitter should restart now */
474                                 custom.intreq = IF_SETCLR | IF_TBE;
475                                 mb();
476                                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
477                                 return;
478                         }
479                 } else {
480                         if ((status & SER_CTS)) {
481 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
482                                 printk("CTS tx stop...");
483 #endif
484                                 info->tty->hw_stopped = 1;
485                                 info->IER &= ~UART_IER_THRI;
486                                 /* disable Tx interrupt and remove any pending interrupts */
487                                 custom.intena = IF_TBE;
488                                 mb();
489                                 custom.intreq = IF_TBE;
490                                 mb();
491                         }
492                 }
493         }
494 }
495
496 static void ser_vbl_int( int irq, void *data, struct pt_regs *regs)
497 {
498         /* vbl is just a periodic interrupt we tie into to update modem status */
499         struct async_struct * info = IRQ_ports;
500         /*
501          * TBD - is it better to unregister from this interrupt or to
502          * ignore it if MSI is clear ?
503          */
504         if(info->IER & UART_IER_MSI)
505           check_modem_status(info);
506 }
507
508 static void ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
509 {
510         struct async_struct * info;
511
512 #ifdef SERIAL_DEBUG_INTR
513         printk("ser_rx_int...");
514 #endif
515
516         info = IRQ_ports;
517         if (!info || !info->tty)
518                 return;
519
520         receive_chars(info);
521         info->last_active = jiffies;
522 #ifdef SERIAL_DEBUG_INTR
523         printk("end.\n");
524 #endif
525 }
526
527 static void ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
528 {
529         struct async_struct * info;
530
531         if (custom.serdatr & SDR_TBE) {
532 #ifdef SERIAL_DEBUG_INTR
533           printk("ser_tx_int...");
534 #endif
535
536           info = IRQ_ports;
537           if (!info || !info->tty)
538             return;
539
540           transmit_chars(info);
541           info->last_active = jiffies;
542 #ifdef SERIAL_DEBUG_INTR
543           printk("end.\n");
544 #endif
545         }
546 }
547
548 /*
549  * -------------------------------------------------------------------
550  * Here ends the serial interrupt routines.
551  * -------------------------------------------------------------------
552  */
553
554 /*
555  * This routine is used to handle the "bottom half" processing for the
556  * serial driver, known also the "software interrupt" processing.
557  * This processing is done at the kernel interrupt level, after the
558  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
559  * is where time-consuming activities which can not be done in the
560  * interrupt driver proper are done; the interrupt driver schedules
561  * them using rs_sched_event(), and they get done here.
562  */
563 static void do_serial_bh(void)
564 {
565         run_task_queue(&tq_serial);
566 }
567
568 static void do_softint(void *private_)
569 {
570         struct async_struct     *info = (struct async_struct *) private_;
571         struct tty_struct       *tty;
572
573         tty = info->tty;
574         if (!tty)
575                 return;
576
577         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
578                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
579                     tty->ldisc.write_wakeup)
580                         (tty->ldisc.write_wakeup)(tty);
581                 wake_up_interruptible(&tty->write_wait);
582         }
583 }
584
585 /*
586  * ---------------------------------------------------------------
587  * Low level utility subroutines for the serial driver:  routines to
588  * figure out the appropriate timeout for an interrupt chain, routines
589  * to initialize and startup a serial port, and routines to shutdown a
590  * serial port.  Useful stuff like that.
591  * ---------------------------------------------------------------
592  */
593
594 static int startup(struct async_struct * info)
595 {
596         unsigned long flags;
597         int     retval=0;
598         unsigned long page;
599
600         page = get_free_page(GFP_KERNEL);
601         if (!page)
602                 return -ENOMEM;
603
604         save_flags(flags); cli();
605
606         if (info->flags & ASYNC_INITIALIZED) {
607                 free_page(page);
608                 goto errout;
609         }
610
611         if (info->xmit.buf)
612                 free_page(page);
613         else
614                 info->xmit.buf = (unsigned char *) page;
615
616 #ifdef SERIAL_DEBUG_OPEN
617         printk("starting up ttys%d ...", info->line);
618 #endif
619
620         /* Clear anything in the input buffer */
621
622         custom.intreq = IF_RBF;
623         mb();
624
625         retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
626         if (retval) {
627           if (serial_isroot()) {
628             if (info->tty)
629               set_bit(TTY_IO_ERROR,
630                       &info->tty->flags);
631             retval = 0;
632           }
633           goto errout;
634         }
635
636         /* enable both Rx and Tx interrupts */
637         custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
638         mb();
639         info->IER = UART_IER_MSI;
640
641         /* remember current state of the DCD and CTS bits */
642         current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
643
644         IRQ_ports = info;
645
646         info->MCR = 0;
647         if (info->tty->termios->c_cflag & CBAUD)
648           info->MCR = SER_DTR | SER_RTS;
649         rtsdtr_ctrl(info->MCR);
650
651         if (info->tty)
652                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
653         info->xmit.head = info->xmit.tail = 0;
654
655         /*
656          * Set up the tty->alt_speed kludge
657          */
658         if (info->tty) {
659                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
660                         info->tty->alt_speed = 57600;
661                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
662                         info->tty->alt_speed = 115200;
663                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
664                         info->tty->alt_speed = 230400;
665                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
666                         info->tty->alt_speed = 460800;
667         }
668
669         /*
670          * and set the speed of the serial port
671          */
672         change_speed(info, 0);
673
674         info->flags |= ASYNC_INITIALIZED;
675         restore_flags(flags);
676         return 0;
677
678 errout:
679         restore_flags(flags);
680         return retval;
681 }
682
683 /*
684  * This routine will shutdown a serial port; interrupts are disabled, and
685  * DTR is dropped if the hangup on close termio flag is on.
686  */
687 static void shutdown(struct async_struct * info)
688 {
689         unsigned long   flags;
690         struct serial_state *state;
691
692         if (!(info->flags & ASYNC_INITIALIZED))
693                 return;
694
695         state = info->state;
696
697 #ifdef SERIAL_DEBUG_OPEN
698         printk("Shutting down serial port %d ....\n", info->line);
699 #endif
700
701         save_flags(flags); cli(); /* Disable interrupts */
702
703         /*
704          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
705          * here so the queue might never be waken up
706          */
707         wake_up_interruptible(&info->delta_msr_wait);
708
709         IRQ_ports = NULL;
710
711         /*
712          * Free the IRQ, if necessary
713          */
714         free_irq(IRQ_AMIGA_VERTB, info);
715
716         if (info->xmit.buf) {
717                 free_page((unsigned long) info->xmit.buf);
718                 info->xmit.buf = 0;
719         }
720
721         info->IER = 0;
722         custom.intena = IF_RBF | IF_TBE;
723         mb();
724
725         /* disable break condition */
726         custom.adkcon = AC_UARTBRK;
727         mb();
728
729         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
730                 info->MCR &= ~(SER_DTR|SER_RTS);
731         rtsdtr_ctrl(info->MCR);
732
733         if (info->tty)
734                 set_bit(TTY_IO_ERROR, &info->tty->flags);
735
736         info->flags &= ~ASYNC_INITIALIZED;
737         restore_flags(flags);
738 }
739
740
741 /*
742  * This routine is called to set the UART divisor registers to match
743  * the specified baud rate for a serial port.
744  */
745 static void change_speed(struct async_struct *info,
746                          struct termios *old_termios)
747 {
748         int     quot = 0, baud_base, baud;
749         unsigned cflag, cval = 0;
750         int     bits;
751         unsigned long   flags;
752
753         if (!info->tty || !info->tty->termios)
754                 return;
755         cflag = info->tty->termios->c_cflag;
756
757         /* Byte size is always 8 bits plus parity bit if requested */
758
759         cval = 3; bits = 10;
760         if (cflag & CSTOPB) {
761                 cval |= 0x04;
762                 bits++;
763         }
764         if (cflag & PARENB) {
765                 cval |= UART_LCR_PARITY;
766                 bits++;
767         }
768         if (!(cflag & PARODD))
769                 cval |= UART_LCR_EPAR;
770 #ifdef CMSPAR
771         if (cflag & CMSPAR)
772                 cval |= UART_LCR_SPAR;
773 #endif
774
775         /* Determine divisor based on baud rate */
776         baud = tty_get_baud_rate(info->tty);
777         if (!baud)
778                 baud = 9600;    /* B0 transition handled in rs_set_termios */
779         baud_base = info->state->baud_base;
780         if (baud == 38400 &&
781             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
782                 quot = info->state->custom_divisor;
783         else {
784                 if (baud == 134)
785                         /* Special case since 134 is really 134.5 */
786                         quot = (2*baud_base / 269);
787                 else if (baud)
788                         quot = baud_base / baud;
789         }
790         /* If the quotient is zero refuse the change */
791         if (!quot && old_termios) {
792                 info->tty->termios->c_cflag &= ~CBAUD;
793                 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
794                 baud = tty_get_baud_rate(info->tty);
795                 if (!baud)
796                         baud = 9600;
797                 if (baud == 38400 &&
798                     ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
799                         quot = info->state->custom_divisor;
800                 else {
801                         if (baud == 134)
802                                 /* Special case since 134 is really 134.5 */
803                                 quot = (2*baud_base / 269);
804                         else if (baud)
805                                 quot = baud_base / baud;
806                 }
807         }
808         /* As a last resort, if the quotient is zero, default to 9600 bps */
809         if (!quot)
810                 quot = baud_base / 9600;
811         info->quot = quot;
812         info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
813         info->timeout += HZ/50;         /* Add .02 seconds of slop */
814
815         /* CTS flow control flag and modem status interrupts */
816         info->IER &= ~UART_IER_MSI;
817         if (info->flags & ASYNC_HARDPPS_CD)
818                 info->IER |= UART_IER_MSI;
819         if (cflag & CRTSCTS) {
820                 info->flags |= ASYNC_CTS_FLOW;
821                 info->IER |= UART_IER_MSI;
822         } else
823                 info->flags &= ~ASYNC_CTS_FLOW;
824         if (cflag & CLOCAL)
825                 info->flags &= ~ASYNC_CHECK_CD;
826         else {
827                 info->flags |= ASYNC_CHECK_CD;
828                 info->IER |= UART_IER_MSI;
829         }
830         /* TBD:
831          * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
832          */
833
834         /*
835          * Set up parity check flag
836          */
837 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
838
839         info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
840         if (I_INPCK(info->tty))
841                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
842         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
843                 info->read_status_mask |= UART_LSR_BI;
844
845         /*
846          * Characters to ignore
847          */
848         info->ignore_status_mask = 0;
849         if (I_IGNPAR(info->tty))
850                 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
851         if (I_IGNBRK(info->tty)) {
852                 info->ignore_status_mask |= UART_LSR_BI;
853                 /*
854                  * If we're ignore parity and break indicators, ignore 
855                  * overruns too.  (For real raw support).
856                  */
857                 if (I_IGNPAR(info->tty))
858                         info->ignore_status_mask |= UART_LSR_OE;
859         }
860         /*
861          * !!! ignore all characters if CREAD is not set
862          */
863         if ((cflag & CREAD) == 0)
864                 info->ignore_status_mask |= UART_LSR_DR;
865         save_flags(flags); cli();
866
867         {
868           short serper;
869
870         /* Set up the baud rate */
871           serper = quot - 1;
872
873         /* Enable or disable parity bit */
874
875         if(cval & UART_LCR_PARITY)
876           serper |= (SERPER_PARENB);
877
878         custom.serper = serper;
879         mb();
880         }
881
882         info->LCR = cval;                               /* Save LCR */
883         restore_flags(flags);
884 }
885
886 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
887 {
888         struct async_struct *info = (struct async_struct *)tty->driver_data;
889         unsigned long flags;
890
891         if (serial_paranoia_check(info, tty->device, "rs_put_char"))
892                 return;
893
894         if (!tty || !info->xmit.buf)
895                 return;
896
897         save_flags(flags); cli();
898         if (CIRC_SPACE(info->xmit.head,
899                        info->xmit.tail,
900                        SERIAL_XMIT_SIZE) == 0) {
901                 restore_flags(flags);
902                 return;
903         }
904
905         info->xmit.buf[info->xmit.head++] = ch;
906         info->xmit.head &= SERIAL_XMIT_SIZE-1;
907         restore_flags(flags);
908 }
909
910 static void rs_flush_chars(struct tty_struct *tty)
911 {
912         struct async_struct *info = (struct async_struct *)tty->driver_data;
913         unsigned long flags;
914
915         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
916                 return;
917
918         if (info->xmit.head == info->xmit.tail
919             || tty->stopped
920             || tty->hw_stopped
921             || !info->xmit.buf)
922                 return;
923
924         save_flags(flags); cli();
925         info->IER |= UART_IER_THRI;
926         custom.intena = IF_SETCLR | IF_TBE;
927         mb();
928         /* set a pending Tx Interrupt, transmitter should restart now */
929         custom.intreq = IF_SETCLR | IF_TBE;
930         mb();
931         restore_flags(flags);
932 }
933
934 static int rs_write(struct tty_struct * tty, int from_user,
935                     const unsigned char *buf, int count)
936 {
937         int     c, ret = 0;
938         struct async_struct *info = (struct async_struct *)tty->driver_data;
939         unsigned long flags;
940
941         if (serial_paranoia_check(info, tty->device, "rs_write"))
942                 return 0;
943
944         if (!tty || !info->xmit.buf || !tmp_buf)
945                 return 0;
946
947         save_flags(flags);
948         if (from_user) {
949                 down(&tmp_buf_sem);
950                 while (1) {
951                         int c1;
952                         c = CIRC_SPACE_TO_END(info->xmit.head,
953                                               info->xmit.tail,
954                                               SERIAL_XMIT_SIZE);
955                         if (count < c)
956                                 c = count;
957
958                         c -= copy_from_user(tmp_buf, buf, c);
959                         if (!c) {
960                                 if (!ret)
961                                         ret = -EFAULT;
962                                 break;
963                         }
964                         cli();
965                         c1 = CIRC_SPACE_TO_END(info->xmit.head,
966                                                info->xmit.tail,
967                                                SERIAL_XMIT_SIZE);
968                         if (c1 < c)
969                                 c = c1;
970                         memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
971                         info->xmit.head = ((info->xmit.head + c) &
972                                            (SERIAL_XMIT_SIZE-1));
973                         restore_flags(flags);
974                         buf += c;
975                         count -= c;
976                         ret += c;
977                 }
978                 up(&tmp_buf_sem);
979         } else {
980                 cli();
981                 while (1) {
982                         c = CIRC_SPACE_TO_END(info->xmit.head,
983                                               info->xmit.tail,
984                                               SERIAL_XMIT_SIZE);
985                         if (count < c)
986                                 c = count;
987                         if (c <= 0) {
988                                 break;
989                         }
990                         memcpy(info->xmit.buf + info->xmit.head, buf, c);
991                         info->xmit.head = ((info->xmit.head + c) &
992                                            (SERIAL_XMIT_SIZE-1));
993                         buf += c;
994                         count -= c;
995                         ret += c;
996                 }
997                 restore_flags(flags);
998         }
999         if (info->xmit.head != info->xmit.tail
1000             && !tty->stopped
1001             && !tty->hw_stopped
1002             && !(info->IER & UART_IER_THRI)) {
1003                 info->IER |= UART_IER_THRI;
1004                 cli();
1005                 custom.intena = IF_SETCLR | IF_TBE;
1006                 mb();
1007                 /* set a pending Tx Interrupt, transmitter should restart now */
1008                 custom.intreq = IF_SETCLR | IF_TBE;
1009                 mb();
1010                 restore_flags(flags);
1011         }
1012         return ret;
1013 }
1014
1015 static int rs_write_room(struct tty_struct *tty)
1016 {
1017         struct async_struct *info = (struct async_struct *)tty->driver_data;
1018
1019         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1020                 return 0;
1021         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1022 }
1023
1024 static int rs_chars_in_buffer(struct tty_struct *tty)
1025 {
1026         struct async_struct *info = (struct async_struct *)tty->driver_data;
1027
1028         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1029                 return 0;
1030         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1031 }
1032
1033 static void rs_flush_buffer(struct tty_struct *tty)
1034 {
1035         struct async_struct *info = (struct async_struct *)tty->driver_data;
1036         unsigned long flags;
1037
1038         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1039                 return;
1040         save_flags(flags); cli();
1041         info->xmit.head = info->xmit.tail = 0;
1042         restore_flags(flags);
1043         wake_up_interruptible(&tty->write_wait);
1044         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1045             tty->ldisc.write_wakeup)
1046                 (tty->ldisc.write_wakeup)(tty);
1047 }
1048
1049 /*
1050  * This function is used to send a high-priority XON/XOFF character to
1051  * the device
1052  */
1053 static void rs_send_xchar(struct tty_struct *tty, char ch)
1054 {
1055         struct async_struct *info = (struct async_struct *)tty->driver_data;
1056         unsigned long flags;
1057
1058         if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1059                 return;
1060
1061         info->x_char = ch;
1062         if (ch) {
1063                 /* Make sure transmit interrupts are on */
1064
1065                 /* Check this ! */
1066                 save_flags(flags);
1067                 cli();
1068                 if(!(custom.intenar & IF_TBE)) {
1069                     custom.intena = IF_SETCLR | IF_TBE;
1070                     mb();
1071                     /* set a pending Tx Interrupt, transmitter should restart now */
1072                     custom.intreq = IF_SETCLR | IF_TBE;
1073                     mb();
1074                 }
1075                 restore_flags(flags);
1076
1077                 info->IER |= UART_IER_THRI;
1078         }
1079 }
1080
1081 /*
1082  * ------------------------------------------------------------
1083  * rs_throttle()
1084  * 
1085  * This routine is called by the upper-layer tty layer to signal that
1086  * incoming characters should be throttled.
1087  * ------------------------------------------------------------
1088  */
1089 static void rs_throttle(struct tty_struct * tty)
1090 {
1091         struct async_struct *info = (struct async_struct *)tty->driver_data;
1092         unsigned long flags;
1093 #ifdef SERIAL_DEBUG_THROTTLE
1094         char    buf[64];
1095
1096         printk("throttle %s: %d....\n", tty_name(tty, buf),
1097                tty->ldisc.chars_in_buffer(tty));
1098 #endif
1099
1100         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1101                 return;
1102
1103         if (I_IXOFF(tty))
1104                 rs_send_xchar(tty, STOP_CHAR(tty));
1105
1106         if (tty->termios->c_cflag & CRTSCTS)
1107                 info->MCR &= ~SER_RTS;
1108
1109         save_flags(flags); cli();
1110         rtsdtr_ctrl(info->MCR);
1111         restore_flags(flags);
1112 }
1113
1114 static void rs_unthrottle(struct tty_struct * tty)
1115 {
1116         struct async_struct *info = (struct async_struct *)tty->driver_data;
1117         unsigned long flags;
1118 #ifdef SERIAL_DEBUG_THROTTLE
1119         char    buf[64];
1120
1121         printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1122                tty->ldisc.chars_in_buffer(tty));
1123 #endif
1124
1125         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1126                 return;
1127
1128         if (I_IXOFF(tty)) {
1129                 if (info->x_char)
1130                         info->x_char = 0;
1131                 else
1132                         rs_send_xchar(tty, START_CHAR(tty));
1133         }
1134         if (tty->termios->c_cflag & CRTSCTS)
1135                 info->MCR |= SER_RTS;
1136         save_flags(flags); cli();
1137         rtsdtr_ctrl(info->MCR);
1138         restore_flags(flags);
1139 }
1140
1141 /*
1142  * ------------------------------------------------------------
1143  * rs_ioctl() and friends
1144  * ------------------------------------------------------------
1145  */
1146
1147 static int get_serial_info(struct async_struct * info,
1148                            struct serial_struct * retinfo)
1149 {
1150         struct serial_struct tmp;
1151         struct serial_state *state = info->state;
1152    
1153         if (!retinfo)
1154                 return -EFAULT;
1155         memset(&tmp, 0, sizeof(tmp));
1156         tmp.type = state->type;
1157         tmp.line = state->line;
1158         tmp.port = state->port;
1159         tmp.irq = state->irq;
1160         tmp.flags = state->flags;
1161         tmp.xmit_fifo_size = state->xmit_fifo_size;
1162         tmp.baud_base = state->baud_base;
1163         tmp.close_delay = state->close_delay;
1164         tmp.closing_wait = state->closing_wait;
1165         tmp.custom_divisor = state->custom_divisor;
1166         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1167                 return -EFAULT;
1168         return 0;
1169 }
1170
1171 static int set_serial_info(struct async_struct * info,
1172                            struct serial_struct * new_info)
1173 {
1174         struct serial_struct new_serial;
1175         struct serial_state old_state, *state;
1176         unsigned int            change_irq,change_port;
1177         int                     retval = 0;
1178
1179         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1180                 return -EFAULT;
1181         state = info->state;
1182         old_state = *state;
1183   
1184         change_irq = new_serial.irq != state->irq;
1185         change_port = (new_serial.port != state->port);
1186         if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1187           return -EINVAL;
1188   
1189         if (!serial_isroot()) {
1190                 if ((new_serial.baud_base != state->baud_base) ||
1191                     (new_serial.close_delay != state->close_delay) ||
1192                     (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1193                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1194                      (state->flags & ~ASYNC_USR_MASK)))
1195                         return -EPERM;
1196                 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1197                                (new_serial.flags & ASYNC_USR_MASK));
1198                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1199                                (new_serial.flags & ASYNC_USR_MASK));
1200                 state->custom_divisor = new_serial.custom_divisor;
1201                 goto check_and_exit;
1202         }
1203
1204         if (new_serial.baud_base < 9600)
1205                 return -EINVAL;
1206
1207         /*
1208          * OK, past this point, all the error checking has been done.
1209          * At this point, we start making changes.....
1210          */
1211
1212         state->baud_base = new_serial.baud_base;
1213         state->flags = ((state->flags & ~ASYNC_FLAGS) |
1214                         (new_serial.flags & ASYNC_FLAGS));
1215         info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1216                        (info->flags & ASYNC_INTERNAL_FLAGS));
1217         state->custom_divisor = new_serial.custom_divisor;
1218         state->close_delay = new_serial.close_delay * HZ/100;
1219         state->closing_wait = new_serial.closing_wait * HZ/100;
1220         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1221
1222 check_and_exit:
1223         if (info->flags & ASYNC_INITIALIZED) {
1224                 if (((old_state.flags & ASYNC_SPD_MASK) !=
1225                      (state->flags & ASYNC_SPD_MASK)) ||
1226                     (old_state.custom_divisor != state->custom_divisor)) {
1227                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1228                                 info->tty->alt_speed = 57600;
1229                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1230                                 info->tty->alt_speed = 115200;
1231                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1232                                 info->tty->alt_speed = 230400;
1233                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1234                                 info->tty->alt_speed = 460800;
1235                         change_speed(info, 0);
1236                 }
1237         } else
1238                 retval = startup(info);
1239         return retval;
1240 }
1241
1242
1243 /*
1244  * get_lsr_info - get line status register info
1245  *
1246  * Purpose: Let user call ioctl() to get info when the UART physically
1247  *          is emptied.  On bus types like RS485, the transmitter must
1248  *          release the bus after transmitting. This must be done when
1249  *          the transmit shift register is empty, not be done when the
1250  *          transmit holding register is empty.  This functionality
1251  *          allows an RS485 driver to be written in user space. 
1252  */
1253 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1254 {
1255         unsigned char status;
1256         unsigned int result;
1257         unsigned long flags;
1258
1259         save_flags(flags); cli();
1260         status = custom.serdatr;
1261         mb();
1262         restore_flags(flags);
1263         result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1264         if (copy_to_user(value, &result, sizeof(int)))
1265                 return -EFAULT;
1266         return 0;
1267 }
1268
1269
1270 static int get_modem_info(struct async_struct * info, unsigned int *value)
1271 {
1272         unsigned char control, status;
1273         unsigned int result;
1274         unsigned long flags;
1275
1276         control = info->MCR;
1277         save_flags(flags); cli();
1278         status = ciab.pra;
1279         restore_flags(flags);
1280         result =  ((control & SER_RTS) ? TIOCM_RTS : 0)
1281                 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1282                 | (!(status  & SER_DCD) ? TIOCM_CAR : 0)
1283                 | (!(status  & SER_DSR) ? TIOCM_DSR : 0)
1284                 | (!(status  & SER_CTS) ? TIOCM_CTS : 0);
1285         if (copy_to_user(value, &result, sizeof(int)))
1286                 return -EFAULT;
1287         return 0;
1288 }
1289
1290 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1291                           unsigned int *value)
1292 {
1293         unsigned int arg;
1294         unsigned long flags;
1295
1296         if (copy_from_user(&arg, value, sizeof(int)))
1297                 return -EFAULT;
1298
1299         switch (cmd) {
1300         case TIOCMBIS: 
1301                 if (arg & TIOCM_RTS)
1302                         info->MCR |= SER_RTS;
1303                 if (arg & TIOCM_DTR)
1304                         info->MCR |= SER_DTR;
1305                 break;
1306         case TIOCMBIC:
1307                 if (arg & TIOCM_RTS)
1308                         info->MCR &= ~SER_RTS;
1309                 if (arg & TIOCM_DTR)
1310                         info->MCR &= ~SER_DTR;
1311                 break;
1312         case TIOCMSET:
1313                 info->MCR = ((info->MCR & ~(SER_RTS | SER_DTR))
1314                              | ((arg & TIOCM_RTS) ? SER_RTS : 0)
1315                              | ((arg & TIOCM_DTR) ? SER_DTR : 0));
1316                 break;
1317         default:
1318                 return -EINVAL;
1319         }
1320         save_flags(flags); cli();
1321         rtsdtr_ctrl(info->MCR);
1322         restore_flags(flags);
1323         return 0;
1324 }
1325
1326 /*
1327  * rs_break() --- routine which turns the break handling on or off
1328  */
1329 static void rs_break(struct tty_struct *tty, int break_state)
1330 {
1331         struct async_struct * info = (struct async_struct *)tty->driver_data;
1332         unsigned long flags;
1333
1334         if (serial_paranoia_check(info, tty->device, "rs_break"))
1335                 return;
1336
1337         save_flags(flags); cli();
1338         if (break_state == -1)
1339           custom.adkcon = AC_SETCLR | AC_UARTBRK;
1340         else
1341           custom.adkcon = AC_UARTBRK;
1342         mb();
1343         restore_flags(flags);
1344 }
1345
1346
1347 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1348                     unsigned int cmd, unsigned long arg)
1349 {
1350         struct async_struct * info = (struct async_struct *)tty->driver_data;
1351         struct async_icount cprev, cnow;        /* kernel counter temps */
1352         struct serial_icounter_struct icount;
1353         unsigned long flags;
1354
1355         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1356                 return -ENODEV;
1357
1358         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1359             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1360             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1361                 if (tty->flags & (1 << TTY_IO_ERROR))
1362                     return -EIO;
1363         }
1364
1365         switch (cmd) {
1366                 case TIOCMGET:
1367                         return get_modem_info(info, (unsigned int *) arg);
1368                 case TIOCMBIS:
1369                 case TIOCMBIC:
1370                 case TIOCMSET:
1371                         return set_modem_info(info, cmd, (unsigned int *) arg);
1372                 case TIOCGSERIAL:
1373                         return get_serial_info(info,
1374                                                (struct serial_struct *) arg);
1375                 case TIOCSSERIAL:
1376                         return set_serial_info(info,
1377                                                (struct serial_struct *) arg);
1378                 case TIOCSERCONFIG:
1379                         return 0;
1380
1381                 case TIOCSERGETLSR: /* Get line status register */
1382                         return get_lsr_info(info, (unsigned int *) arg);
1383
1384                 case TIOCSERGSTRUCT:
1385                         if (copy_to_user((struct async_struct *) arg,
1386                                          info, sizeof(struct async_struct)))
1387                                 return -EFAULT;
1388                         return 0;
1389
1390                 /*
1391                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1392                  * - mask passed in arg for lines of interest
1393                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1394                  * Caller should use TIOCGICOUNT to see which one it was
1395                  */
1396                 case TIOCMIWAIT:
1397                         save_flags(flags); cli();
1398                         /* note the counters on entry */
1399                         cprev = info->state->icount;
1400                         restore_flags(flags);
1401                         while (1) {
1402                                 interruptible_sleep_on(&info->delta_msr_wait);
1403                                 /* see if a signal did it */
1404                                 if (signal_pending(current))
1405                                         return -ERESTARTSYS;
1406                                 save_flags(flags); cli();
1407                                 cnow = info->state->icount; /* atomic copy */
1408                                 restore_flags(flags);
1409                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
1410                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1411                                         return -EIO; /* no change => error */
1412                                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1413                                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1414                                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1415                                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1416                                         return 0;
1417                                 }
1418                                 cprev = cnow;
1419                         }
1420                         /* NOTREACHED */
1421
1422                 /* 
1423                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1424                  * Return: write counters to the user passed counter struct
1425                  * NB: both 1->0 and 0->1 transitions are counted except for
1426                  *     RI where only 0->1 is counted.
1427                  */
1428                 case TIOCGICOUNT:
1429                         save_flags(flags); cli();
1430                         cnow = info->state->icount;
1431                         restore_flags(flags);
1432                         icount.cts = cnow.cts;
1433                         icount.dsr = cnow.dsr;
1434                         icount.rng = cnow.rng;
1435                         icount.dcd = cnow.dcd;
1436                         icount.rx = cnow.rx;
1437                         icount.tx = cnow.tx;
1438                         icount.frame = cnow.frame;
1439                         icount.overrun = cnow.overrun;
1440                         icount.parity = cnow.parity;
1441                         icount.brk = cnow.brk;
1442                         icount.buf_overrun = cnow.buf_overrun;
1443
1444                         if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1445                                 return -EFAULT;
1446                         return 0;
1447                 case TIOCSERGWILD:
1448                 case TIOCSERSWILD:
1449                         /* "setserial -W" is called in Debian boot */
1450                         printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1451                         return 0;
1452
1453                 default:
1454                         return -ENOIOCTLCMD;
1455                 }
1456         return 0;
1457 }
1458
1459 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1460 {
1461         struct async_struct *info = (struct async_struct *)tty->driver_data;
1462         unsigned long flags;
1463         unsigned int cflag = tty->termios->c_cflag;
1464
1465         if (   (cflag == old_termios->c_cflag)
1466             && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1467                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1468           return;
1469
1470         change_speed(info, old_termios);
1471
1472         /* Handle transition to B0 status */
1473         if ((old_termios->c_cflag & CBAUD) &&
1474             !(cflag & CBAUD)) {
1475                 info->MCR &= ~(SER_DTR|SER_RTS);
1476                 save_flags(flags); cli();
1477                 rtsdtr_ctrl(info->MCR);
1478                 restore_flags(flags);
1479         }
1480
1481         /* Handle transition away from B0 status */
1482         if (!(old_termios->c_cflag & CBAUD) &&
1483             (cflag & CBAUD)) {
1484                 info->MCR |= SER_DTR;
1485                 if (!(tty->termios->c_cflag & CRTSCTS) || 
1486                     !test_bit(TTY_THROTTLED, &tty->flags)) {
1487                         info->MCR |= SER_RTS;
1488                 }
1489                 save_flags(flags); cli();
1490                 rtsdtr_ctrl(info->MCR);
1491                 restore_flags(flags);
1492         }
1493
1494         /* Handle turning off CRTSCTS */
1495         if ((old_termios->c_cflag & CRTSCTS) &&
1496             !(tty->termios->c_cflag & CRTSCTS)) {
1497                 tty->hw_stopped = 0;
1498                 rs_start(tty);
1499         }
1500
1501 #if 0
1502         /*
1503          * No need to wake up processes in open wait, since they
1504          * sample the CLOCAL flag once, and don't recheck it.
1505          * XXX  It's not clear whether the current behavior is correct
1506          * or not.  Hence, this may change.....
1507          */
1508         if (!(old_termios->c_cflag & CLOCAL) &&
1509             (tty->termios->c_cflag & CLOCAL))
1510                 wake_up_interruptible(&info->open_wait);
1511 #endif
1512 }
1513
1514 /*
1515  * ------------------------------------------------------------
1516  * rs_close()
1517  * 
1518  * This routine is called when the serial port gets closed.  First, we
1519  * wait for the last remaining data to be sent.  Then, we unlink its
1520  * async structure from the interrupt chain if necessary, and we free
1521  * that IRQ if nothing is left in the chain.
1522  * ------------------------------------------------------------
1523  */
1524 static void rs_close(struct tty_struct *tty, struct file * filp)
1525 {
1526         struct async_struct * info = (struct async_struct *)tty->driver_data;
1527         struct serial_state *state;
1528         unsigned long flags;
1529
1530         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1531                 return;
1532
1533         state = info->state;
1534
1535         save_flags(flags); cli();
1536
1537         if (tty_hung_up_p(filp)) {
1538                 DBG_CNT("before DEC-hung");
1539                 MOD_DEC_USE_COUNT;
1540                 restore_flags(flags);
1541                 return;
1542         }
1543
1544 #ifdef SERIAL_DEBUG_OPEN
1545         printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1546 #endif
1547         if ((tty->count == 1) && (state->count != 1)) {
1548                 /*
1549                  * Uh, oh.  tty->count is 1, which means that the tty
1550                  * structure will be freed.  state->count should always
1551                  * be one in these conditions.  If it's greater than
1552                  * one, we've got real problems, since it means the
1553                  * serial port won't be shutdown.
1554                  */
1555                 printk("rs_close: bad serial port count; tty->count is 1, "
1556                        "state->count is %d\n", state->count);
1557                 state->count = 1;
1558         }
1559         if (--state->count < 0) {
1560                 printk("rs_close: bad serial port count for ttys%d: %d\n",
1561                        info->line, state->count);
1562                 state->count = 0;
1563         }
1564         if (state->count) {
1565                 DBG_CNT("before DEC-2");
1566                 MOD_DEC_USE_COUNT;
1567                 restore_flags(flags);
1568                 return;
1569         }
1570         info->flags |= ASYNC_CLOSING;
1571         /*
1572          * Save the termios structure, since this port may have
1573          * separate termios for callout and dialin.
1574          */
1575         if (info->flags & ASYNC_NORMAL_ACTIVE)
1576                 info->state->normal_termios = *tty->termios;
1577         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1578                 info->state->callout_termios = *tty->termios;
1579         /*
1580          * Now we wait for the transmit buffer to clear; and we notify 
1581          * the line discipline to only process XON/XOFF characters.
1582          */
1583         tty->closing = 1;
1584         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1585                 tty_wait_until_sent(tty, info->closing_wait);
1586         /*
1587          * At this point we stop accepting input.  To do this, we
1588          * disable the receive line status interrupts, and tell the
1589          * interrupt driver to stop checking the data ready bit in the
1590          * line status register.
1591          */
1592         info->read_status_mask &= ~UART_LSR_DR;
1593         if (info->flags & ASYNC_INITIALIZED) {
1594                 /* disable receive interrupts */
1595                 custom.intena = IF_RBF;
1596                 mb();
1597                 /* clear any pending receive interrupt */
1598                 custom.intreq = IF_RBF;
1599                 mb();
1600
1601                 /*
1602                  * Before we drop DTR, make sure the UART transmitter
1603                  * has completely drained; this is especially
1604                  * important if there is a transmit FIFO!
1605                  */
1606                 rs_wait_until_sent(tty, info->timeout);
1607         }
1608         shutdown(info);
1609         if (tty->driver.flush_buffer)
1610                 tty->driver.flush_buffer(tty);
1611         if (tty->ldisc.flush_buffer)
1612                 tty->ldisc.flush_buffer(tty);
1613         tty->closing = 0;
1614         info->event = 0;
1615         info->tty = 0;
1616         if (info->blocked_open) {
1617                 if (info->close_delay) {
1618                         current->state = TASK_INTERRUPTIBLE;
1619                         schedule_timeout(info->close_delay);
1620                 }
1621                 wake_up_interruptible(&info->open_wait);
1622         }
1623         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1624                          ASYNC_CLOSING);
1625         wake_up_interruptible(&info->close_wait);
1626         MOD_DEC_USE_COUNT;
1627         restore_flags(flags);
1628 }
1629
1630 /*
1631  * rs_wait_until_sent() --- wait until the transmitter is empty
1632  */
1633 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1634 {
1635         struct async_struct * info = (struct async_struct *)tty->driver_data;
1636         unsigned long orig_jiffies, char_time;
1637         int lsr;
1638
1639         if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1640                 return;
1641
1642         if (info->xmit_fifo_size == 0)
1643                 return; /* Just in case.... */
1644
1645         orig_jiffies = jiffies;
1646         /*
1647          * Set the check interval to be 1/5 of the estimated time to
1648          * send a single character, and make it at least 1.  The check
1649          * interval should also be less than the timeout.
1650          * 
1651          * Note: we have to use pretty tight timings here to satisfy
1652          * the NIST-PCTS.
1653          */
1654         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1655         char_time = char_time / 5;
1656         if (char_time == 0)
1657                 char_time = 1;
1658         if (timeout)
1659           char_time = MIN(char_time, timeout);
1660         /*
1661          * If the transmitter hasn't cleared in twice the approximate
1662          * amount of time to send the entire FIFO, it probably won't
1663          * ever clear.  This assumes the UART isn't doing flow
1664          * control, which is currently the case.  Hence, if it ever
1665          * takes longer than info->timeout, this is probably due to a
1666          * UART bug of some kind.  So, we clamp the timeout parameter at
1667          * 2*info->timeout.
1668          */
1669         if (!timeout || timeout > 2*info->timeout)
1670                 timeout = 2*info->timeout;
1671 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1672         printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1673         printk("jiff=%lu...", jiffies);
1674 #endif
1675         while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1676 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1677                 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1678 #endif
1679                 current->state = TASK_INTERRUPTIBLE;
1680                 schedule_timeout(char_time);
1681                 if (signal_pending(current))
1682                         break;
1683                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1684                         break;
1685         }
1686         current->state = TASK_RUNNING;
1687 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1688         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1689 #endif
1690 }
1691
1692 /*
1693  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1694  */
1695 static void rs_hangup(struct tty_struct *tty)
1696 {
1697         struct async_struct * info = (struct async_struct *)tty->driver_data;
1698         struct serial_state *state = info->state;
1699
1700         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1701                 return;
1702
1703         state = info->state;
1704
1705         rs_flush_buffer(tty);
1706         shutdown(info);
1707         info->event = 0;
1708         state->count = 0;
1709         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1710         info->tty = 0;
1711         wake_up_interruptible(&info->open_wait);
1712 }
1713
1714 /*
1715  * ------------------------------------------------------------
1716  * rs_open() and friends
1717  * ------------------------------------------------------------
1718  */
1719 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1720                            struct async_struct *info)
1721 {
1722 #ifdef DECLARE_WAITQUEUE
1723         DECLARE_WAITQUEUE(wait, current);
1724 #else
1725         struct wait_queue wait = { current, NULL };
1726 #endif
1727         struct serial_state *state = info->state;
1728         int             retval;
1729         int             do_clocal = 0, extra_count = 0;
1730         unsigned long   flags;
1731
1732         /*
1733          * If the device is in the middle of being closed, then block
1734          * until it's done, and then try again.
1735          */
1736         if (tty_hung_up_p(filp) ||
1737             (info->flags & ASYNC_CLOSING)) {
1738                 if (info->flags & ASYNC_CLOSING)
1739                         interruptible_sleep_on(&info->close_wait);
1740 #ifdef SERIAL_DO_RESTART
1741                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1742                         -EAGAIN : -ERESTARTSYS);
1743 #else
1744                 return -EAGAIN;
1745 #endif
1746         }
1747
1748         /*
1749          * If this is a callout device, then just make sure the normal
1750          * device isn't being used.
1751          */
1752         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1753                 if (info->flags & ASYNC_NORMAL_ACTIVE)
1754                         return -EBUSY;
1755                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1756                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
1757                     (info->session != current->session))
1758                     return -EBUSY;
1759                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1760                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
1761                     (info->pgrp != current->pgrp))
1762                     return -EBUSY;
1763                 info->flags |= ASYNC_CALLOUT_ACTIVE;
1764                 return 0;
1765         }
1766
1767         /*
1768          * If non-blocking mode is set, or the port is not enabled,
1769          * then make the check up front and then exit.
1770          */
1771         if ((filp->f_flags & O_NONBLOCK) ||
1772             (tty->flags & (1 << TTY_IO_ERROR))) {
1773                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1774                         return -EBUSY;
1775                 info->flags |= ASYNC_NORMAL_ACTIVE;
1776                 return 0;
1777         }
1778
1779         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1780                 if (state->normal_termios.c_cflag & CLOCAL)
1781                         do_clocal = 1;
1782         } else {
1783                 if (tty->termios->c_cflag & CLOCAL)
1784                         do_clocal = 1;
1785         }
1786
1787         /*
1788          * Block waiting for the carrier detect and the line to become
1789          * free (i.e., not in use by the callout).  While we are in
1790          * this loop, state->count is dropped by one, so that
1791          * rs_close() knows when to free things.  We restore it upon
1792          * exit, either normal or abnormal.
1793          */
1794         retval = 0;
1795         add_wait_queue(&info->open_wait, &wait);
1796 #ifdef SERIAL_DEBUG_OPEN
1797         printk("block_til_ready before block: ttys%d, count = %d\n",
1798                state->line, state->count);
1799 #endif
1800         save_flags(flags); cli();
1801         if (!tty_hung_up_p(filp)) {
1802                 extra_count = 1;
1803                 state->count--;
1804         }
1805         restore_flags(flags);
1806         info->blocked_open++;
1807         while (1) {
1808                 save_flags(flags); cli();
1809                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1810                     (tty->termios->c_cflag & CBAUD))
1811                         rtsdtr_ctrl(SER_DTR|SER_RTS);
1812                 restore_flags(flags);
1813                 set_current_state(TASK_INTERRUPTIBLE);
1814                 if (tty_hung_up_p(filp) ||
1815                     !(info->flags & ASYNC_INITIALIZED)) {
1816 #ifdef SERIAL_DO_RESTART
1817                         if (info->flags & ASYNC_HUP_NOTIFY)
1818                                 retval = -EAGAIN;
1819                         else
1820                                 retval = -ERESTARTSYS;
1821 #else
1822                         retval = -EAGAIN;
1823 #endif
1824                         break;
1825                 }
1826                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1827                     !(info->flags & ASYNC_CLOSING) &&
1828                     (do_clocal || (!(ciab.pra & SER_DCD)) ))
1829                         break;
1830                 if (signal_pending(current)) {
1831                         retval = -ERESTARTSYS;
1832                         break;
1833                 }
1834 #ifdef SERIAL_DEBUG_OPEN
1835                 printk("block_til_ready blocking: ttys%d, count = %d\n",
1836                        info->line, state->count);
1837 #endif
1838                 schedule();
1839         }
1840         current->state = TASK_RUNNING;
1841         remove_wait_queue(&info->open_wait, &wait);
1842         if (extra_count)
1843                 state->count++;
1844         info->blocked_open--;
1845 #ifdef SERIAL_DEBUG_OPEN
1846         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1847                info->line, state->count);
1848 #endif
1849         if (retval)
1850                 return retval;
1851         info->flags |= ASYNC_NORMAL_ACTIVE;
1852         return 0;
1853 }
1854
1855 static int get_async_struct(int line, struct async_struct **ret_info)
1856 {
1857         struct async_struct *info;
1858         struct serial_state *sstate;
1859
1860         sstate = rs_table + line;
1861         sstate->count++;
1862         if (sstate->info) {
1863                 *ret_info = sstate->info;
1864                 return 0;
1865         }
1866         info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1867         if (!info) {
1868                 sstate->count--;
1869                 return -ENOMEM;
1870         }
1871         memset(info, 0, sizeof(struct async_struct));
1872 #ifdef DECLARE_WAITQUEUE
1873         init_waitqueue_head(&info->open_wait);
1874         init_waitqueue_head(&info->close_wait);
1875         init_waitqueue_head(&info->delta_msr_wait);
1876 #endif
1877         info->magic = SERIAL_MAGIC;
1878         info->port = sstate->port;
1879         info->flags = sstate->flags;
1880         info->xmit_fifo_size = sstate->xmit_fifo_size;
1881         info->line = line;
1882         info->tqueue.routine = do_softint;
1883         info->tqueue.data = info;
1884         info->state = sstate;
1885         if (sstate->info) {
1886                 kfree(info);
1887                 *ret_info = sstate->info;
1888                 return 0;
1889         }
1890         *ret_info = sstate->info = info;
1891         return 0;
1892 }
1893
1894 /*
1895  * This routine is called whenever a serial port is opened.  It
1896  * enables interrupts for a serial port, linking in its async structure into
1897  * the IRQ chain.   It also performs the serial-specific
1898  * initialization for the tty structure.
1899  */
1900 static int rs_open(struct tty_struct *tty, struct file * filp)
1901 {
1902         struct async_struct     *info;
1903         int                     retval, line;
1904         unsigned long           page;
1905
1906         MOD_INC_USE_COUNT;
1907         line = MINOR(tty->device) - tty->driver.minor_start;
1908         if ((line < 0) || (line >= NR_PORTS)) {
1909                 MOD_DEC_USE_COUNT;
1910                 return -ENODEV;
1911         }
1912         retval = get_async_struct(line, &info);
1913         if (retval) {
1914                 MOD_DEC_USE_COUNT;
1915                 return retval;
1916         }
1917         tty->driver_data = info;
1918         info->tty = tty;
1919         if (serial_paranoia_check(info, tty->device, "rs_open"))
1920                 return -ENODEV;
1921
1922 #ifdef SERIAL_DEBUG_OPEN
1923         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1924                info->state->count);
1925 #endif
1926         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1927
1928         if (!tmp_buf) {
1929                 page = get_free_page(GFP_KERNEL);
1930                 if (!page) {
1931                         return -ENOMEM;
1932                 }
1933                 if (tmp_buf)
1934                         free_page(page);
1935                 else
1936                         tmp_buf = (unsigned char *) page;
1937         }
1938
1939         /*
1940          * If the port is the middle of closing, bail out now
1941          */
1942         if (tty_hung_up_p(filp) ||
1943             (info->flags & ASYNC_CLOSING)) {
1944                 if (info->flags & ASYNC_CLOSING)
1945                         interruptible_sleep_on(&info->close_wait);
1946 #ifdef SERIAL_DO_RESTART
1947                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1948                         -EAGAIN : -ERESTARTSYS);
1949 #else
1950                 return -EAGAIN;
1951 #endif
1952         }
1953
1954         /*
1955          * Start up serial port
1956          */
1957         retval = startup(info);
1958         if (retval) {
1959                 return retval;
1960         }
1961
1962         retval = block_til_ready(tty, filp, info);
1963         if (retval) {
1964 #ifdef SERIAL_DEBUG_OPEN
1965                 printk("rs_open returning after block_til_ready with %d\n",
1966                        retval);
1967 #endif
1968                 return retval;
1969         }
1970
1971         if ((info->state->count == 1) &&
1972             (info->flags & ASYNC_SPLIT_TERMIOS)) {
1973                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1974                         *tty->termios = info->state->normal_termios;
1975                 else 
1976                         *tty->termios = info->state->callout_termios;
1977                 change_speed(info, 0);
1978         }
1979         info->session = current->session;
1980         info->pgrp = current->pgrp;
1981
1982 #ifdef SERIAL_DEBUG_OPEN
1983         printk("rs_open ttys%d successful...", info->line);
1984 #endif
1985         return 0;
1986 }
1987
1988 /*
1989  * /proc fs routines....
1990  */
1991
1992 static inline int line_info(char *buf, struct serial_state *state)
1993 {
1994         struct async_struct *info = state->info, scr_info;
1995         char    stat_buf[30], control, status;
1996         int     ret;
1997         unsigned long flags;
1998
1999         ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
2000
2001         /*
2002          * Figure out the current RS-232 lines
2003          */
2004         if (!info) {
2005                 info = &scr_info;       /* This is just for serial_{in,out} */
2006
2007                 info->magic = SERIAL_MAGIC;
2008                 info->flags = state->flags;
2009                 info->quot = 0;
2010                 info->tty = 0;
2011         }
2012         save_flags(flags); cli();
2013         status = ciab.pra;
2014         control = info ? info->MCR : status;
2015         restore_flags(flags); 
2016
2017         stat_buf[0] = 0;
2018         stat_buf[1] = 0;
2019         if(!(control & SER_RTS))
2020                 strcat(stat_buf, "|RTS");
2021         if(!(status & SER_CTS))
2022                 strcat(stat_buf, "|CTS");
2023         if(!(control & SER_DTR))
2024                 strcat(stat_buf, "|DTR");
2025         if(!(status & SER_DSR))
2026                 strcat(stat_buf, "|DSR");
2027         if(!(status & SER_DCD))
2028                 strcat(stat_buf, "|CD");
2029
2030         if (info->quot) {
2031                 ret += sprintf(buf+ret, " baud:%d",
2032                                state->baud_base / info->quot);
2033         }
2034
2035         ret += sprintf(buf+ret, " tx:%d rx:%d",
2036                       state->icount.tx, state->icount.rx);
2037
2038         if (state->icount.frame)
2039                 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2040
2041         if (state->icount.parity)
2042                 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2043
2044         if (state->icount.brk)
2045                 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2046
2047         if (state->icount.overrun)
2048                 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2049
2050         /*
2051          * Last thing is the RS-232 status lines
2052          */
2053         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2054         return ret;
2055 }
2056
2057 int rs_read_proc(char *page, char **start, off_t off, int count,
2058                  int *eof, void *data)
2059 {
2060         int len = 0, l;
2061         off_t   begin = 0;
2062
2063         len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2064         l = line_info(page + len, &rs_table[0]);
2065         len += l;
2066         if (len+begin > off+count)
2067           goto done;
2068         if (len+begin < off) {
2069           begin += len;
2070           len = 0;
2071         }
2072         *eof = 1;
2073 done:
2074         if (off >= len+begin)
2075                 return 0;
2076         *start = page + (off-begin);
2077         return ((count < begin+len-off) ? count : begin+len-off);
2078 }
2079
2080 /*
2081  * ---------------------------------------------------------------------
2082  * rs_init() and friends
2083  *
2084  * rs_init() is called at boot-time to initialize the serial driver.
2085  * ---------------------------------------------------------------------
2086  */
2087
2088 /*
2089  * This routine prints out the appropriate serial driver version
2090  * number, and identifies which options were configured into this
2091  * driver.
2092  */
2093 static _INLINE_ void show_serial_version(void)
2094 {
2095         printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2096 }
2097
2098
2099 int register_serial(struct serial_struct *req);
2100 void unregister_serial(int line);
2101
2102
2103 /*
2104  * The serial driver boot-time initialization code!
2105  */
2106 static int __init rs_init(void)
2107 {
2108         unsigned long flags;
2109         struct serial_state * state;
2110
2111         if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2112                 return -ENODEV;
2113
2114         /*
2115          *  We request SERDAT and SERPER only, because the serial registers are
2116          *  too spreaded over the custom register space
2117          */
2118         if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2119                 return -EBUSY;
2120
2121         init_bh(SERIAL_BH, do_serial_bh);
2122
2123         IRQ_ports = NULL;
2124
2125         show_serial_version();
2126
2127         /* Initialize the tty_driver structure */
2128
2129         memset(&serial_driver, 0, sizeof(struct tty_driver));
2130         serial_driver.magic = TTY_DRIVER_MAGIC;
2131         serial_driver.driver_name = "amiserial";
2132         serial_driver.name = "ttyS";
2133         serial_driver.major = TTY_MAJOR;
2134         serial_driver.minor_start = 64;
2135         serial_driver.num = 1;
2136         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2137         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2138         serial_driver.init_termios = tty_std_termios;
2139         serial_driver.init_termios.c_cflag =
2140                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2141         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2142         serial_driver.refcount = &serial_refcount;
2143         serial_driver.table = serial_table;
2144         serial_driver.termios = serial_termios;
2145         serial_driver.termios_locked = serial_termios_locked;
2146
2147         serial_driver.open = rs_open;
2148         serial_driver.close = rs_close;
2149         serial_driver.write = rs_write;
2150         serial_driver.put_char = rs_put_char;
2151         serial_driver.flush_chars = rs_flush_chars;
2152         serial_driver.write_room = rs_write_room;
2153         serial_driver.chars_in_buffer = rs_chars_in_buffer;
2154         serial_driver.flush_buffer = rs_flush_buffer;
2155         serial_driver.ioctl = rs_ioctl;
2156         serial_driver.throttle = rs_throttle;
2157         serial_driver.unthrottle = rs_unthrottle;
2158         serial_driver.set_termios = rs_set_termios;
2159         serial_driver.stop = rs_stop;
2160         serial_driver.start = rs_start;
2161         serial_driver.hangup = rs_hangup;
2162         serial_driver.break_ctl = rs_break;
2163         serial_driver.send_xchar = rs_send_xchar;
2164         serial_driver.wait_until_sent = rs_wait_until_sent;
2165         serial_driver.read_proc = rs_read_proc;
2166
2167         /*
2168          * The callout device is just like normal device except for
2169          * major number and the subtype code.
2170          */
2171         callout_driver = serial_driver;
2172         callout_driver.name = "cua";
2173         callout_driver.major = TTYAUX_MAJOR;
2174         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2175         callout_driver.read_proc = 0;
2176         callout_driver.proc_entry = 0;
2177
2178         if (tty_register_driver(&serial_driver))
2179                 panic("Couldn't register serial driver\n");
2180         if (tty_register_driver(&callout_driver))
2181                 panic("Couldn't register callout driver\n");
2182
2183         state = rs_table;
2184         state->magic = SSTATE_MAGIC;
2185         state->port = (int)&custom.serdatr; /* Just to give it a value */
2186         state->line = 0;
2187         state->custom_divisor = 0;
2188         state->close_delay = 5*HZ/10;
2189         state->closing_wait = 30*HZ;
2190         state->callout_termios = callout_driver.init_termios;
2191         state->normal_termios = serial_driver.init_termios;
2192         state->icount.cts = state->icount.dsr = 
2193           state->icount.rng = state->icount.dcd = 0;
2194         state->icount.rx = state->icount.tx = 0;
2195         state->icount.frame = state->icount.parity = 0;
2196         state->icount.overrun = state->icount.brk = 0;
2197         /*
2198         if(state->port && check_region(state->port,REGION_LENGTH(state)))
2199           continue;
2200         */
2201
2202         printk(KERN_INFO "ttyS%02d is the amiga builtin serial port\n",
2203                        state->line);
2204
2205         /* Hardware set up */
2206
2207         state->baud_base = amiga_colorclock;
2208         state->xmit_fifo_size = 1;
2209
2210         save_flags (flags);
2211         cli();
2212
2213         /* set ISRs, and then disable the rx interrupts */
2214         request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2215         request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2216
2217         /* turn off Rx and Tx interrupts */
2218         custom.intena = IF_RBF | IF_TBE;
2219         mb();
2220
2221         /* clear any pending interrupt */
2222         custom.intreq = IF_RBF | IF_TBE;
2223         mb();
2224
2225         restore_flags (flags);
2226
2227         /*
2228          * set the appropriate directions for the modem control flags,
2229          * and clear RTS and DTR
2230          */
2231         ciab.ddra |= (SER_DTR | SER_RTS);   /* outputs */
2232         ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR);  /* inputs */
2233
2234         return 0;
2235 }
2236
2237 static __exit void rs_exit(void) 
2238 {
2239         unsigned long flags;
2240         int e1, e2;
2241         struct async_struct *info;
2242
2243         /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2244         save_flags(flags);
2245         cli();
2246         remove_bh(SERIAL_BH);
2247         if ((e1 = tty_unregister_driver(&serial_driver)))
2248                 printk("SERIAL: failed to unregister serial driver (%d)\n",
2249                        e1);
2250         if ((e2 = tty_unregister_driver(&callout_driver)))
2251                 printk("SERIAL: failed to unregister callout driver (%d)\n", 
2252                        e2);
2253         restore_flags(flags);
2254
2255         info = rs_table[0].info;
2256         if (info) {
2257           rs_table[0].info = NULL;
2258           kfree(info);
2259         }
2260
2261         if (tmp_buf) {
2262                 free_page((unsigned long) tmp_buf);
2263                 tmp_buf = NULL;
2264         }
2265
2266         release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2267 }
2268
2269 module_init(rs_init)
2270 module_exit(rs_exit)
2271
2272
2273 /*
2274  * ------------------------------------------------------------
2275  * Serial console driver
2276  * ------------------------------------------------------------
2277  */
2278 #ifdef CONFIG_SERIAL_CONSOLE
2279
2280 static void amiga_serial_putc(char c)
2281 {
2282         custom.serdat = (unsigned char)c | 0x100;
2283         while (!(custom.serdatr & 0x2000))
2284                 barrier();
2285 }
2286
2287 /*
2288  *      Print a string to the serial port trying not to disturb
2289  *      any possible real use of the port...
2290  *
2291  *      The console_lock must be held when we get here.
2292  */
2293 static void serial_console_write(struct console *co, const char *s,
2294                                 unsigned count)
2295 {
2296         unsigned short intena = custom.intenar;
2297
2298         custom.intena = IF_TBE;
2299
2300         while (count--) {
2301                 if (*s == '\n')
2302                         amiga_serial_putc('\r');
2303                 amiga_serial_putc(*s++);
2304         }
2305
2306         custom.intena = IF_SETCLR | (intena & IF_TBE);
2307 }
2308
2309 /*
2310  *      Receive character from the serial port
2311  */
2312 static int serial_console_wait_key(struct console *co)
2313 {
2314         unsigned short intena = custom.intenar;
2315         int ch;
2316
2317         custom.intena = IF_RBF;
2318
2319         while (!(custom.intreqr & IF_RBF))
2320                 barrier();
2321         ch = custom.serdatr & 0xff;
2322         custom.intreq = IF_RBF;
2323
2324         custom.intena = IF_SETCLR | (intena & IF_RBF);
2325
2326         return ch;
2327 }
2328
2329 static kdev_t serial_console_device(struct console *c)
2330 {
2331         return MKDEV(TTY_MAJOR, 64);
2332 }
2333
2334 static struct console sercons = {
2335         "ttyS",
2336         serial_console_write,
2337         NULL,
2338         serial_console_device,
2339         serial_console_wait_key,
2340         NULL,
2341         NULL,
2342         CON_PRINTBUFFER,
2343         -1,
2344         0,
2345         NULL
2346 };
2347
2348 /*
2349  *      Register console.
2350  */
2351 void __init serial_console_init(void)
2352 {
2353         register_console(&sercons);
2354 }
2355 #endif
2356
2357 MODULE_LICENSE("GPL");