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