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