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