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