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