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