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