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