[WATCHDOG] s3c2410 wdt - add .owner field
[linux-2.6] / drivers / char / amiserial.c
1 /*
2  *  linux/drivers/char/amiserial.c
3  *
4  * Serial driver for the amiga builtin port.
5  *
6  * This code was created by taking serial.c version 4.30 from kernel
7  * release 2.3.22, replacing all hardware related stuff with the
8  * corresponding amiga hardware actions, and removing all irrelevant
9  * code. As a consequence, it uses many of the constants and names
10  * associated with the registers and bits of 16550 compatible UARTS -
11  * but only to keep track of status, etc in the state variables. It
12  * was done this was to make it easier to keep the code in line with
13  * (non hardware specific) changes to serial.c.
14  *
15  * The port is registered with the tty driver as minor device 64, and
16  * therefore other ports should should only use 65 upwards.
17  *
18  * Richard Lucock 28/12/99
19  *
20  *  Copyright (C) 1991, 1992  Linus Torvalds
21  *  Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 
22  *              1998, 1999  Theodore Ts'o
23  *
24  */
25
26 /*
27  * Serial driver configuration section.  Here are the various options:
28  *
29  * SERIAL_PARANOIA_CHECK
30  *              Check the magic number for the async_structure where
31  *              ever possible.
32  */
33
34 #include <linux/config.h>
35 #include <linux/delay.h>
36
37 #undef SERIAL_PARANOIA_CHECK
38 #define SERIAL_DO_RESTART
39
40 /* Set of debugging defines */
41
42 #undef SERIAL_DEBUG_INTR
43 #undef SERIAL_DEBUG_OPEN
44 #undef SERIAL_DEBUG_FLOW
45 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
46
47 /* Sanity checks */
48
49 #define SERIAL_INLINE
50   
51 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
52 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
53  tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
54 #else
55 #define DBG_CNT(s)
56 #endif
57
58 /*
59  * End of serial driver configuration section.
60  */
61
62 #include <linux/module.h>
63
64 #include <linux/types.h>
65 #include <linux/serial.h>
66 #include <linux/serialP.h>
67 #include <linux/serial_reg.h>
68 static char *serial_version = "4.30";
69
70 #include <linux/errno.h>
71 #include <linux/signal.h>
72 #include <linux/sched.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/console.h>
79 #include <linux/major.h>
80 #include <linux/string.h>
81 #include <linux/fcntl.h>
82 #include <linux/ptrace.h>
83 #include <linux/ioport.h>
84 #include <linux/mm.h>
85 #include <linux/slab.h>
86 #include <linux/init.h>
87 #include <linux/bitops.h>
88
89 #include <asm/setup.h>
90
91 #include <asm/system.h>
92
93 #include <asm/irq.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, NULL);
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 = NULL;
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;
865         unsigned long flags;
866
867         if (!tty)
868                 return;
869
870         info = tty->driver_data;
871
872         if (serial_paranoia_check(info, tty->name, "rs_put_char"))
873                 return;
874
875         if (!info->xmit.buf)
876                 return;
877
878         local_irq_save(flags);
879         if (CIRC_SPACE(info->xmit.head,
880                        info->xmit.tail,
881                        SERIAL_XMIT_SIZE) == 0) {
882                 local_irq_restore(flags);
883                 return;
884         }
885
886         info->xmit.buf[info->xmit.head++] = ch;
887         info->xmit.head &= SERIAL_XMIT_SIZE-1;
888         local_irq_restore(flags);
889 }
890
891 static void rs_flush_chars(struct tty_struct *tty)
892 {
893         struct async_struct *info = (struct async_struct *)tty->driver_data;
894         unsigned long flags;
895
896         if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
897                 return;
898
899         if (info->xmit.head == info->xmit.tail
900             || tty->stopped
901             || tty->hw_stopped
902             || !info->xmit.buf)
903                 return;
904
905         local_irq_save(flags);
906         info->IER |= UART_IER_THRI;
907         custom.intena = IF_SETCLR | IF_TBE;
908         mb();
909         /* set a pending Tx Interrupt, transmitter should restart now */
910         custom.intreq = IF_SETCLR | IF_TBE;
911         mb();
912         local_irq_restore(flags);
913 }
914
915 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
916 {
917         int     c, ret = 0;
918         struct async_struct *info;
919         unsigned long flags;
920
921         if (!tty)
922                 return 0;
923
924         info = tty->driver_data;
925
926         if (serial_paranoia_check(info, tty->name, "rs_write"))
927                 return 0;
928
929         if (!info->xmit.buf || !tmp_buf)
930                 return 0;
931
932         local_save_flags(flags);
933         local_irq_disable();
934         while (1) {
935                 c = CIRC_SPACE_TO_END(info->xmit.head,
936                                       info->xmit.tail,
937                                       SERIAL_XMIT_SIZE);
938                 if (count < c)
939                         c = count;
940                 if (c <= 0) {
941                         break;
942                 }
943                 memcpy(info->xmit.buf + info->xmit.head, buf, c);
944                 info->xmit.head = ((info->xmit.head + c) &
945                                    (SERIAL_XMIT_SIZE-1));
946                 buf += c;
947                 count -= c;
948                 ret += c;
949         }
950         local_irq_restore(flags);
951
952         if (info->xmit.head != info->xmit.tail
953             && !tty->stopped
954             && !tty->hw_stopped
955             && !(info->IER & UART_IER_THRI)) {
956                 info->IER |= UART_IER_THRI;
957                 local_irq_disable();
958                 custom.intena = IF_SETCLR | IF_TBE;
959                 mb();
960                 /* set a pending Tx Interrupt, transmitter should restart now */
961                 custom.intreq = IF_SETCLR | IF_TBE;
962                 mb();
963                 local_irq_restore(flags);
964         }
965         return ret;
966 }
967
968 static int rs_write_room(struct tty_struct *tty)
969 {
970         struct async_struct *info = (struct async_struct *)tty->driver_data;
971
972         if (serial_paranoia_check(info, tty->name, "rs_write_room"))
973                 return 0;
974         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
975 }
976
977 static int rs_chars_in_buffer(struct tty_struct *tty)
978 {
979         struct async_struct *info = (struct async_struct *)tty->driver_data;
980
981         if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
982                 return 0;
983         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
984 }
985
986 static void rs_flush_buffer(struct tty_struct *tty)
987 {
988         struct async_struct *info = (struct async_struct *)tty->driver_data;
989         unsigned long flags;
990
991         if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
992                 return;
993         local_irq_save(flags);
994         info->xmit.head = info->xmit.tail = 0;
995         local_irq_restore(flags);
996         wake_up_interruptible(&tty->write_wait);
997         tty_wakeup(tty);
998 }
999
1000 /*
1001  * This function is used to send a high-priority XON/XOFF character to
1002  * the device
1003  */
1004 static void rs_send_xchar(struct tty_struct *tty, char ch)
1005 {
1006         struct async_struct *info = (struct async_struct *)tty->driver_data;
1007         unsigned long flags;
1008
1009         if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1010                 return;
1011
1012         info->x_char = ch;
1013         if (ch) {
1014                 /* Make sure transmit interrupts are on */
1015
1016                 /* Check this ! */
1017                 local_irq_save(flags);
1018                 if(!(custom.intenar & IF_TBE)) {
1019                     custom.intena = IF_SETCLR | IF_TBE;
1020                     mb();
1021                     /* set a pending Tx Interrupt, transmitter should restart now */
1022                     custom.intreq = IF_SETCLR | IF_TBE;
1023                     mb();
1024                 }
1025                 local_irq_restore(flags);
1026
1027                 info->IER |= UART_IER_THRI;
1028         }
1029 }
1030
1031 /*
1032  * ------------------------------------------------------------
1033  * rs_throttle()
1034  * 
1035  * This routine is called by the upper-layer tty layer to signal that
1036  * incoming characters should be throttled.
1037  * ------------------------------------------------------------
1038  */
1039 static void rs_throttle(struct tty_struct * tty)
1040 {
1041         struct async_struct *info = (struct async_struct *)tty->driver_data;
1042         unsigned long flags;
1043 #ifdef SERIAL_DEBUG_THROTTLE
1044         char    buf[64];
1045
1046         printk("throttle %s: %d....\n", tty_name(tty, buf),
1047                tty->ldisc.chars_in_buffer(tty));
1048 #endif
1049
1050         if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1051                 return;
1052
1053         if (I_IXOFF(tty))
1054                 rs_send_xchar(tty, STOP_CHAR(tty));
1055
1056         if (tty->termios->c_cflag & CRTSCTS)
1057                 info->MCR &= ~SER_RTS;
1058
1059         local_irq_save(flags);
1060         rtsdtr_ctrl(info->MCR);
1061         local_irq_restore(flags);
1062 }
1063
1064 static void rs_unthrottle(struct tty_struct * tty)
1065 {
1066         struct async_struct *info = (struct async_struct *)tty->driver_data;
1067         unsigned long flags;
1068 #ifdef SERIAL_DEBUG_THROTTLE
1069         char    buf[64];
1070
1071         printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1072                tty->ldisc.chars_in_buffer(tty));
1073 #endif
1074
1075         if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1076                 return;
1077
1078         if (I_IXOFF(tty)) {
1079                 if (info->x_char)
1080                         info->x_char = 0;
1081                 else
1082                         rs_send_xchar(tty, START_CHAR(tty));
1083         }
1084         if (tty->termios->c_cflag & CRTSCTS)
1085                 info->MCR |= SER_RTS;
1086         local_irq_save(flags);
1087         rtsdtr_ctrl(info->MCR);
1088         local_irq_restore(flags);
1089 }
1090
1091 /*
1092  * ------------------------------------------------------------
1093  * rs_ioctl() and friends
1094  * ------------------------------------------------------------
1095  */
1096
1097 static int get_serial_info(struct async_struct * info,
1098                            struct serial_struct * retinfo)
1099 {
1100         struct serial_struct tmp;
1101         struct serial_state *state = info->state;
1102    
1103         if (!retinfo)
1104                 return -EFAULT;
1105         memset(&tmp, 0, sizeof(tmp));
1106         tmp.type = state->type;
1107         tmp.line = state->line;
1108         tmp.port = state->port;
1109         tmp.irq = state->irq;
1110         tmp.flags = state->flags;
1111         tmp.xmit_fifo_size = state->xmit_fifo_size;
1112         tmp.baud_base = state->baud_base;
1113         tmp.close_delay = state->close_delay;
1114         tmp.closing_wait = state->closing_wait;
1115         tmp.custom_divisor = state->custom_divisor;
1116         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1117                 return -EFAULT;
1118         return 0;
1119 }
1120
1121 static int set_serial_info(struct async_struct * info,
1122                            struct serial_struct * new_info)
1123 {
1124         struct serial_struct new_serial;
1125         struct serial_state old_state, *state;
1126         unsigned int            change_irq,change_port;
1127         int                     retval = 0;
1128
1129         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1130                 return -EFAULT;
1131         state = info->state;
1132         old_state = *state;
1133   
1134         change_irq = new_serial.irq != state->irq;
1135         change_port = (new_serial.port != state->port);
1136         if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1137           return -EINVAL;
1138   
1139         if (!serial_isroot()) {
1140                 if ((new_serial.baud_base != state->baud_base) ||
1141                     (new_serial.close_delay != state->close_delay) ||
1142                     (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1143                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1144                      (state->flags & ~ASYNC_USR_MASK)))
1145                         return -EPERM;
1146                 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1147                                (new_serial.flags & ASYNC_USR_MASK));
1148                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1149                                (new_serial.flags & ASYNC_USR_MASK));
1150                 state->custom_divisor = new_serial.custom_divisor;
1151                 goto check_and_exit;
1152         }
1153
1154         if (new_serial.baud_base < 9600)
1155                 return -EINVAL;
1156
1157         /*
1158          * OK, past this point, all the error checking has been done.
1159          * At this point, we start making changes.....
1160          */
1161
1162         state->baud_base = new_serial.baud_base;
1163         state->flags = ((state->flags & ~ASYNC_FLAGS) |
1164                         (new_serial.flags & ASYNC_FLAGS));
1165         info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1166                        (info->flags & ASYNC_INTERNAL_FLAGS));
1167         state->custom_divisor = new_serial.custom_divisor;
1168         state->close_delay = new_serial.close_delay * HZ/100;
1169         state->closing_wait = new_serial.closing_wait * HZ/100;
1170         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1171
1172 check_and_exit:
1173         if (info->flags & ASYNC_INITIALIZED) {
1174                 if (((old_state.flags & ASYNC_SPD_MASK) !=
1175                      (state->flags & ASYNC_SPD_MASK)) ||
1176                     (old_state.custom_divisor != state->custom_divisor)) {
1177                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1178                                 info->tty->alt_speed = 57600;
1179                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1180                                 info->tty->alt_speed = 115200;
1181                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1182                                 info->tty->alt_speed = 230400;
1183                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1184                                 info->tty->alt_speed = 460800;
1185                         change_speed(info, NULL);
1186                 }
1187         } else
1188                 retval = startup(info);
1189         return retval;
1190 }
1191
1192
1193 /*
1194  * get_lsr_info - get line status register info
1195  *
1196  * Purpose: Let user call ioctl() to get info when the UART physically
1197  *          is emptied.  On bus types like RS485, the transmitter must
1198  *          release the bus after transmitting. This must be done when
1199  *          the transmit shift register is empty, not be done when the
1200  *          transmit holding register is empty.  This functionality
1201  *          allows an RS485 driver to be written in user space. 
1202  */
1203 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1204 {
1205         unsigned char status;
1206         unsigned int result;
1207         unsigned long flags;
1208
1209         local_irq_save(flags);
1210         status = custom.serdatr;
1211         mb();
1212         local_irq_restore(flags);
1213         result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1214         if (copy_to_user(value, &result, sizeof(int)))
1215                 return -EFAULT;
1216         return 0;
1217 }
1218
1219
1220 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1221 {
1222         struct async_struct * info = (struct async_struct *)tty->driver_data;
1223         unsigned char control, status;
1224         unsigned long flags;
1225
1226         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1227                 return -ENODEV;
1228         if (tty->flags & (1 << TTY_IO_ERROR))
1229                 return -EIO;
1230
1231         control = info->MCR;
1232         local_irq_save(flags);
1233         status = ciab.pra;
1234         local_irq_restore(flags);
1235         return    ((control & SER_RTS) ? TIOCM_RTS : 0)
1236                 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1237                 | (!(status  & SER_DCD) ? TIOCM_CAR : 0)
1238                 | (!(status  & SER_DSR) ? TIOCM_DSR : 0)
1239                 | (!(status  & SER_CTS) ? TIOCM_CTS : 0);
1240 }
1241
1242 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1243                        unsigned int set, unsigned int clear)
1244 {
1245         struct async_struct * info = (struct async_struct *)tty->driver_data;
1246         unsigned long flags;
1247
1248         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1249                 return -ENODEV;
1250         if (tty->flags & (1 << TTY_IO_ERROR))
1251                 return -EIO;
1252
1253         local_irq_save(flags);
1254         if (set & TIOCM_RTS)
1255                 info->MCR |= SER_RTS;
1256         if (set & TIOCM_DTR)
1257                 info->MCR |= SER_DTR;
1258         if (clear & TIOCM_RTS)
1259                 info->MCR &= ~SER_RTS;
1260         if (clear & TIOCM_DTR)
1261                 info->MCR &= ~SER_DTR;
1262         rtsdtr_ctrl(info->MCR);
1263         local_irq_restore(flags);
1264         return 0;
1265 }
1266
1267 /*
1268  * rs_break() --- routine which turns the break handling on or off
1269  */
1270 static void rs_break(struct tty_struct *tty, int break_state)
1271 {
1272         struct async_struct * info = (struct async_struct *)tty->driver_data;
1273         unsigned long flags;
1274
1275         if (serial_paranoia_check(info, tty->name, "rs_break"))
1276                 return;
1277
1278         local_irq_save(flags);
1279         if (break_state == -1)
1280           custom.adkcon = AC_SETCLR | AC_UARTBRK;
1281         else
1282           custom.adkcon = AC_UARTBRK;
1283         mb();
1284         local_irq_restore(flags);
1285 }
1286
1287
1288 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1289                     unsigned int cmd, unsigned long arg)
1290 {
1291         struct async_struct * info = (struct async_struct *)tty->driver_data;
1292         struct async_icount cprev, cnow;        /* kernel counter temps */
1293         struct serial_icounter_struct icount;
1294         unsigned long flags;
1295
1296         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1297                 return -ENODEV;
1298
1299         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1300             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1301             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1302                 if (tty->flags & (1 << TTY_IO_ERROR))
1303                     return -EIO;
1304         }
1305
1306         switch (cmd) {
1307                 case TIOCGSERIAL:
1308                         return get_serial_info(info,
1309                                                (struct serial_struct *) arg);
1310                 case TIOCSSERIAL:
1311                         return set_serial_info(info,
1312                                                (struct serial_struct *) arg);
1313                 case TIOCSERCONFIG:
1314                         return 0;
1315
1316                 case TIOCSERGETLSR: /* Get line status register */
1317                         return get_lsr_info(info, (unsigned int *) arg);
1318
1319                 case TIOCSERGSTRUCT:
1320                         if (copy_to_user((struct async_struct *) arg,
1321                                          info, sizeof(struct async_struct)))
1322                                 return -EFAULT;
1323                         return 0;
1324
1325                 /*
1326                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1327                  * - mask passed in arg for lines of interest
1328                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1329                  * Caller should use TIOCGICOUNT to see which one it was
1330                  */
1331                 case TIOCMIWAIT:
1332                         local_irq_save(flags);
1333                         /* note the counters on entry */
1334                         cprev = info->state->icount;
1335                         local_irq_restore(flags);
1336                         while (1) {
1337                                 interruptible_sleep_on(&info->delta_msr_wait);
1338                                 /* see if a signal did it */
1339                                 if (signal_pending(current))
1340                                         return -ERESTARTSYS;
1341                                 local_irq_save(flags);
1342                                 cnow = info->state->icount; /* atomic copy */
1343                                 local_irq_restore(flags);
1344                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
1345                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1346                                         return -EIO; /* no change => error */
1347                                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1348                                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1349                                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1350                                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1351                                         return 0;
1352                                 }
1353                                 cprev = cnow;
1354                         }
1355                         /* NOTREACHED */
1356
1357                 /* 
1358                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1359                  * Return: write counters to the user passed counter struct
1360                  * NB: both 1->0 and 0->1 transitions are counted except for
1361                  *     RI where only 0->1 is counted.
1362                  */
1363                 case TIOCGICOUNT:
1364                         local_irq_save(flags);
1365                         cnow = info->state->icount;
1366                         local_irq_restore(flags);
1367                         icount.cts = cnow.cts;
1368                         icount.dsr = cnow.dsr;
1369                         icount.rng = cnow.rng;
1370                         icount.dcd = cnow.dcd;
1371                         icount.rx = cnow.rx;
1372                         icount.tx = cnow.tx;
1373                         icount.frame = cnow.frame;
1374                         icount.overrun = cnow.overrun;
1375                         icount.parity = cnow.parity;
1376                         icount.brk = cnow.brk;
1377                         icount.buf_overrun = cnow.buf_overrun;
1378
1379                         if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1380                                 return -EFAULT;
1381                         return 0;
1382                 case TIOCSERGWILD:
1383                 case TIOCSERSWILD:
1384                         /* "setserial -W" is called in Debian boot */
1385                         printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1386                         return 0;
1387
1388                 default:
1389                         return -ENOIOCTLCMD;
1390                 }
1391         return 0;
1392 }
1393
1394 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1395 {
1396         struct async_struct *info = (struct async_struct *)tty->driver_data;
1397         unsigned long flags;
1398         unsigned int cflag = tty->termios->c_cflag;
1399
1400         if (   (cflag == old_termios->c_cflag)
1401             && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1402                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1403           return;
1404
1405         change_speed(info, old_termios);
1406
1407         /* Handle transition to B0 status */
1408         if ((old_termios->c_cflag & CBAUD) &&
1409             !(cflag & CBAUD)) {
1410                 info->MCR &= ~(SER_DTR|SER_RTS);
1411                 local_irq_save(flags);
1412                 rtsdtr_ctrl(info->MCR);
1413                 local_irq_restore(flags);
1414         }
1415
1416         /* Handle transition away from B0 status */
1417         if (!(old_termios->c_cflag & CBAUD) &&
1418             (cflag & CBAUD)) {
1419                 info->MCR |= SER_DTR;
1420                 if (!(tty->termios->c_cflag & CRTSCTS) || 
1421                     !test_bit(TTY_THROTTLED, &tty->flags)) {
1422                         info->MCR |= SER_RTS;
1423                 }
1424                 local_irq_save(flags);
1425                 rtsdtr_ctrl(info->MCR);
1426                 local_irq_restore(flags);
1427         }
1428
1429         /* Handle turning off CRTSCTS */
1430         if ((old_termios->c_cflag & CRTSCTS) &&
1431             !(tty->termios->c_cflag & CRTSCTS)) {
1432                 tty->hw_stopped = 0;
1433                 rs_start(tty);
1434         }
1435
1436 #if 0
1437         /*
1438          * No need to wake up processes in open wait, since they
1439          * sample the CLOCAL flag once, and don't recheck it.
1440          * XXX  It's not clear whether the current behavior is correct
1441          * or not.  Hence, this may change.....
1442          */
1443         if (!(old_termios->c_cflag & CLOCAL) &&
1444             (tty->termios->c_cflag & CLOCAL))
1445                 wake_up_interruptible(&info->open_wait);
1446 #endif
1447 }
1448
1449 /*
1450  * ------------------------------------------------------------
1451  * rs_close()
1452  * 
1453  * This routine is called when the serial port gets closed.  First, we
1454  * wait for the last remaining data to be sent.  Then, we unlink its
1455  * async structure from the interrupt chain if necessary, and we free
1456  * that IRQ if nothing is left in the chain.
1457  * ------------------------------------------------------------
1458  */
1459 static void rs_close(struct tty_struct *tty, struct file * filp)
1460 {
1461         struct async_struct * info = (struct async_struct *)tty->driver_data;
1462         struct serial_state *state;
1463         unsigned long flags;
1464
1465         if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1466                 return;
1467
1468         state = info->state;
1469
1470         local_irq_save(flags);
1471
1472         if (tty_hung_up_p(filp)) {
1473                 DBG_CNT("before DEC-hung");
1474                 local_irq_restore(flags);
1475                 return;
1476         }
1477
1478 #ifdef SERIAL_DEBUG_OPEN
1479         printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1480 #endif
1481         if ((tty->count == 1) && (state->count != 1)) {
1482                 /*
1483                  * Uh, oh.  tty->count is 1, which means that the tty
1484                  * structure will be freed.  state->count should always
1485                  * be one in these conditions.  If it's greater than
1486                  * one, we've got real problems, since it means the
1487                  * serial port won't be shutdown.
1488                  */
1489                 printk("rs_close: bad serial port count; tty->count is 1, "
1490                        "state->count is %d\n", state->count);
1491                 state->count = 1;
1492         }
1493         if (--state->count < 0) {
1494                 printk("rs_close: bad serial port count for ttys%d: %d\n",
1495                        info->line, state->count);
1496                 state->count = 0;
1497         }
1498         if (state->count) {
1499                 DBG_CNT("before DEC-2");
1500                 local_irq_restore(flags);
1501                 return;
1502         }
1503         info->flags |= ASYNC_CLOSING;
1504         /*
1505          * Now we wait for the transmit buffer to clear; and we notify 
1506          * the line discipline to only process XON/XOFF characters.
1507          */
1508         tty->closing = 1;
1509         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1510                 tty_wait_until_sent(tty, info->closing_wait);
1511         /*
1512          * At this point we stop accepting input.  To do this, we
1513          * disable the receive line status interrupts, and tell the
1514          * interrupt driver to stop checking the data ready bit in the
1515          * line status register.
1516          */
1517         info->read_status_mask &= ~UART_LSR_DR;
1518         if (info->flags & ASYNC_INITIALIZED) {
1519                 /* disable receive interrupts */
1520                 custom.intena = IF_RBF;
1521                 mb();
1522                 /* clear any pending receive interrupt */
1523                 custom.intreq = IF_RBF;
1524                 mb();
1525
1526                 /*
1527                  * Before we drop DTR, make sure the UART transmitter
1528                  * has completely drained; this is especially
1529                  * important if there is a transmit FIFO!
1530                  */
1531                 rs_wait_until_sent(tty, info->timeout);
1532         }
1533         shutdown(info);
1534         if (tty->driver->flush_buffer)
1535                 tty->driver->flush_buffer(tty);
1536                 
1537         tty_ldisc_flush(tty);
1538         tty->closing = 0;
1539         info->event = 0;
1540         info->tty = NULL;
1541         if (info->blocked_open) {
1542                 if (info->close_delay) {
1543                         msleep_interruptible(jiffies_to_msecs(info->close_delay));
1544                 }
1545                 wake_up_interruptible(&info->open_wait);
1546         }
1547         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1548         wake_up_interruptible(&info->close_wait);
1549         local_irq_restore(flags);
1550 }
1551
1552 /*
1553  * rs_wait_until_sent() --- wait until the transmitter is empty
1554  */
1555 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1556 {
1557         struct async_struct * info = (struct async_struct *)tty->driver_data;
1558         unsigned long orig_jiffies, char_time;
1559         int lsr;
1560
1561         if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1562                 return;
1563
1564         if (info->xmit_fifo_size == 0)
1565                 return; /* Just in case.... */
1566
1567         orig_jiffies = jiffies;
1568         /*
1569          * Set the check interval to be 1/5 of the estimated time to
1570          * send a single character, and make it at least 1.  The check
1571          * interval should also be less than the timeout.
1572          * 
1573          * Note: we have to use pretty tight timings here to satisfy
1574          * the NIST-PCTS.
1575          */
1576         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1577         char_time = char_time / 5;
1578         if (char_time == 0)
1579                 char_time = 1;
1580         if (timeout)
1581           char_time = min_t(unsigned long, char_time, timeout);
1582         /*
1583          * If the transmitter hasn't cleared in twice the approximate
1584          * amount of time to send the entire FIFO, it probably won't
1585          * ever clear.  This assumes the UART isn't doing flow
1586          * control, which is currently the case.  Hence, if it ever
1587          * takes longer than info->timeout, this is probably due to a
1588          * UART bug of some kind.  So, we clamp the timeout parameter at
1589          * 2*info->timeout.
1590          */
1591         if (!timeout || timeout > 2*info->timeout)
1592                 timeout = 2*info->timeout;
1593 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1594         printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1595         printk("jiff=%lu...", jiffies);
1596 #endif
1597         while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1598 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1599                 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1600 #endif
1601                 msleep_interruptible(jiffies_to_msecs(char_time));
1602                 if (signal_pending(current))
1603                         break;
1604                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1605                         break;
1606         }
1607         current->state = TASK_RUNNING;
1608 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1609         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1610 #endif
1611 }
1612
1613 /*
1614  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1615  */
1616 static void rs_hangup(struct tty_struct *tty)
1617 {
1618         struct async_struct * info = (struct async_struct *)tty->driver_data;
1619         struct serial_state *state = info->state;
1620
1621         if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1622                 return;
1623
1624         state = info->state;
1625
1626         rs_flush_buffer(tty);
1627         shutdown(info);
1628         info->event = 0;
1629         state->count = 0;
1630         info->flags &= ~ASYNC_NORMAL_ACTIVE;
1631         info->tty = NULL;
1632         wake_up_interruptible(&info->open_wait);
1633 }
1634
1635 /*
1636  * ------------------------------------------------------------
1637  * rs_open() and friends
1638  * ------------------------------------------------------------
1639  */
1640 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1641                            struct async_struct *info)
1642 {
1643 #ifdef DECLARE_WAITQUEUE
1644         DECLARE_WAITQUEUE(wait, current);
1645 #else
1646         struct wait_queue wait = { current, NULL };
1647 #endif
1648         struct serial_state *state = info->state;
1649         int             retval;
1650         int             do_clocal = 0, extra_count = 0;
1651         unsigned long   flags;
1652
1653         /*
1654          * If the device is in the middle of being closed, then block
1655          * until it's done, and then try again.
1656          */
1657         if (tty_hung_up_p(filp) ||
1658             (info->flags & ASYNC_CLOSING)) {
1659                 if (info->flags & ASYNC_CLOSING)
1660                         interruptible_sleep_on(&info->close_wait);
1661 #ifdef SERIAL_DO_RESTART
1662                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1663                         -EAGAIN : -ERESTARTSYS);
1664 #else
1665                 return -EAGAIN;
1666 #endif
1667         }
1668
1669         /*
1670          * If non-blocking mode is set, or the port is not enabled,
1671          * then make the check up front and then exit.
1672          */
1673         if ((filp->f_flags & O_NONBLOCK) ||
1674             (tty->flags & (1 << TTY_IO_ERROR))) {
1675                 info->flags |= ASYNC_NORMAL_ACTIVE;
1676                 return 0;
1677         }
1678
1679         if (tty->termios->c_cflag & CLOCAL)
1680                 do_clocal = 1;
1681
1682         /*
1683          * Block waiting for the carrier detect and the line to become
1684          * free (i.e., not in use by the callout).  While we are in
1685          * this loop, state->count is dropped by one, so that
1686          * rs_close() knows when to free things.  We restore it upon
1687          * exit, either normal or abnormal.
1688          */
1689         retval = 0;
1690         add_wait_queue(&info->open_wait, &wait);
1691 #ifdef SERIAL_DEBUG_OPEN
1692         printk("block_til_ready before block: ttys%d, count = %d\n",
1693                state->line, state->count);
1694 #endif
1695         local_irq_save(flags);
1696         if (!tty_hung_up_p(filp)) {
1697                 extra_count = 1;
1698                 state->count--;
1699         }
1700         local_irq_restore(flags);
1701         info->blocked_open++;
1702         while (1) {
1703                 local_irq_save(flags);
1704                 if (tty->termios->c_cflag & CBAUD)
1705                         rtsdtr_ctrl(SER_DTR|SER_RTS);
1706                 local_irq_restore(flags);
1707                 set_current_state(TASK_INTERRUPTIBLE);
1708                 if (tty_hung_up_p(filp) ||
1709                     !(info->flags & ASYNC_INITIALIZED)) {
1710 #ifdef SERIAL_DO_RESTART
1711                         if (info->flags & ASYNC_HUP_NOTIFY)
1712                                 retval = -EAGAIN;
1713                         else
1714                                 retval = -ERESTARTSYS;
1715 #else
1716                         retval = -EAGAIN;
1717 #endif
1718                         break;
1719                 }
1720                 if (!(info->flags & ASYNC_CLOSING) &&
1721                     (do_clocal || (!(ciab.pra & SER_DCD)) ))
1722                         break;
1723                 if (signal_pending(current)) {
1724                         retval = -ERESTARTSYS;
1725                         break;
1726                 }
1727 #ifdef SERIAL_DEBUG_OPEN
1728                 printk("block_til_ready blocking: ttys%d, count = %d\n",
1729                        info->line, state->count);
1730 #endif
1731                 schedule();
1732         }
1733         current->state = TASK_RUNNING;
1734         remove_wait_queue(&info->open_wait, &wait);
1735         if (extra_count)
1736                 state->count++;
1737         info->blocked_open--;
1738 #ifdef SERIAL_DEBUG_OPEN
1739         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1740                info->line, state->count);
1741 #endif
1742         if (retval)
1743                 return retval;
1744         info->flags |= ASYNC_NORMAL_ACTIVE;
1745         return 0;
1746 }
1747
1748 static int get_async_struct(int line, struct async_struct **ret_info)
1749 {
1750         struct async_struct *info;
1751         struct serial_state *sstate;
1752
1753         sstate = rs_table + line;
1754         sstate->count++;
1755         if (sstate->info) {
1756                 *ret_info = sstate->info;
1757                 return 0;
1758         }
1759         info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1760         if (!info) {
1761                 sstate->count--;
1762                 return -ENOMEM;
1763         }
1764         memset(info, 0, sizeof(struct async_struct));
1765 #ifdef DECLARE_WAITQUEUE
1766         init_waitqueue_head(&info->open_wait);
1767         init_waitqueue_head(&info->close_wait);
1768         init_waitqueue_head(&info->delta_msr_wait);
1769 #endif
1770         info->magic = SERIAL_MAGIC;
1771         info->port = sstate->port;
1772         info->flags = sstate->flags;
1773         info->xmit_fifo_size = sstate->xmit_fifo_size;
1774         info->line = line;
1775         tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1776         info->state = sstate;
1777         if (sstate->info) {
1778                 kfree(info);
1779                 *ret_info = sstate->info;
1780                 return 0;
1781         }
1782         *ret_info = sstate->info = info;
1783         return 0;
1784 }
1785
1786 /*
1787  * This routine is called whenever a serial port is opened.  It
1788  * enables interrupts for a serial port, linking in its async structure into
1789  * the IRQ chain.   It also performs the serial-specific
1790  * initialization for the tty structure.
1791  */
1792 static int rs_open(struct tty_struct *tty, struct file * filp)
1793 {
1794         struct async_struct     *info;
1795         int                     retval, line;
1796         unsigned long           page;
1797
1798         line = tty->index;
1799         if ((line < 0) || (line >= NR_PORTS)) {
1800                 return -ENODEV;
1801         }
1802         retval = get_async_struct(line, &info);
1803         if (retval) {
1804                 return retval;
1805         }
1806         tty->driver_data = info;
1807         info->tty = tty;
1808         if (serial_paranoia_check(info, tty->name, "rs_open"))
1809                 return -ENODEV;
1810
1811 #ifdef SERIAL_DEBUG_OPEN
1812         printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1813 #endif
1814         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1815
1816         if (!tmp_buf) {
1817                 page = get_zeroed_page(GFP_KERNEL);
1818                 if (!page) {
1819                         return -ENOMEM;
1820                 }
1821                 if (tmp_buf)
1822                         free_page(page);
1823                 else
1824                         tmp_buf = (unsigned char *) page;
1825         }
1826
1827         /*
1828          * If the port is the middle of closing, bail out now
1829          */
1830         if (tty_hung_up_p(filp) ||
1831             (info->flags & ASYNC_CLOSING)) {
1832                 if (info->flags & ASYNC_CLOSING)
1833                         interruptible_sleep_on(&info->close_wait);
1834 #ifdef SERIAL_DO_RESTART
1835                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1836                         -EAGAIN : -ERESTARTSYS);
1837 #else
1838                 return -EAGAIN;
1839 #endif
1840         }
1841
1842         /*
1843          * Start up serial port
1844          */
1845         retval = startup(info);
1846         if (retval) {
1847                 return retval;
1848         }
1849
1850         retval = block_til_ready(tty, filp, info);
1851         if (retval) {
1852 #ifdef SERIAL_DEBUG_OPEN
1853                 printk("rs_open returning after block_til_ready with %d\n",
1854                        retval);
1855 #endif
1856                 return retval;
1857         }
1858
1859 #ifdef SERIAL_DEBUG_OPEN
1860         printk("rs_open %s successful...", tty->name);
1861 #endif
1862         return 0;
1863 }
1864
1865 /*
1866  * /proc fs routines....
1867  */
1868
1869 static inline int line_info(char *buf, struct serial_state *state)
1870 {
1871         struct async_struct *info = state->info, scr_info;
1872         char    stat_buf[30], control, status;
1873         int     ret;
1874         unsigned long flags;
1875
1876         ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
1877
1878         /*
1879          * Figure out the current RS-232 lines
1880          */
1881         if (!info) {
1882                 info = &scr_info;       /* This is just for serial_{in,out} */
1883
1884                 info->magic = SERIAL_MAGIC;
1885                 info->flags = state->flags;
1886                 info->quot = 0;
1887                 info->tty = NULL;
1888         }
1889         local_irq_save(flags);
1890         status = ciab.pra;
1891         control = info ? info->MCR : status;
1892         local_irq_restore(flags);
1893
1894         stat_buf[0] = 0;
1895         stat_buf[1] = 0;
1896         if(!(control & SER_RTS))
1897                 strcat(stat_buf, "|RTS");
1898         if(!(status & SER_CTS))
1899                 strcat(stat_buf, "|CTS");
1900         if(!(control & SER_DTR))
1901                 strcat(stat_buf, "|DTR");
1902         if(!(status & SER_DSR))
1903                 strcat(stat_buf, "|DSR");
1904         if(!(status & SER_DCD))
1905                 strcat(stat_buf, "|CD");
1906
1907         if (info->quot) {
1908                 ret += sprintf(buf+ret, " baud:%d",
1909                                state->baud_base / info->quot);
1910         }
1911
1912         ret += sprintf(buf+ret, " tx:%d rx:%d",
1913                       state->icount.tx, state->icount.rx);
1914
1915         if (state->icount.frame)
1916                 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
1917
1918         if (state->icount.parity)
1919                 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
1920
1921         if (state->icount.brk)
1922                 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
1923
1924         if (state->icount.overrun)
1925                 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
1926
1927         /*
1928          * Last thing is the RS-232 status lines
1929          */
1930         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1931         return ret;
1932 }
1933
1934 static int rs_read_proc(char *page, char **start, off_t off, int count,
1935                         int *eof, void *data)
1936 {
1937         int len = 0, l;
1938         off_t   begin = 0;
1939
1940         len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
1941         l = line_info(page + len, &rs_table[0]);
1942         len += l;
1943         if (len+begin > off+count)
1944           goto done;
1945         if (len+begin < off) {
1946           begin += len;
1947           len = 0;
1948         }
1949         *eof = 1;
1950 done:
1951         if (off >= len+begin)
1952                 return 0;
1953         *start = page + (off-begin);
1954         return ((count < begin+len-off) ? count : begin+len-off);
1955 }
1956
1957 /*
1958  * ---------------------------------------------------------------------
1959  * rs_init() and friends
1960  *
1961  * rs_init() is called at boot-time to initialize the serial driver.
1962  * ---------------------------------------------------------------------
1963  */
1964
1965 /*
1966  * This routine prints out the appropriate serial driver version
1967  * number, and identifies which options were configured into this
1968  * driver.
1969  */
1970 static _INLINE_ void show_serial_version(void)
1971 {
1972         printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1973 }
1974
1975
1976 static struct tty_operations serial_ops = {
1977         .open = rs_open,
1978         .close = rs_close,
1979         .write = rs_write,
1980         .put_char = rs_put_char,
1981         .flush_chars = rs_flush_chars,
1982         .write_room = rs_write_room,
1983         .chars_in_buffer = rs_chars_in_buffer,
1984         .flush_buffer = rs_flush_buffer,
1985         .ioctl = rs_ioctl,
1986         .throttle = rs_throttle,
1987         .unthrottle = rs_unthrottle,
1988         .set_termios = rs_set_termios,
1989         .stop = rs_stop,
1990         .start = rs_start,
1991         .hangup = rs_hangup,
1992         .break_ctl = rs_break,
1993         .send_xchar = rs_send_xchar,
1994         .wait_until_sent = rs_wait_until_sent,
1995         .read_proc = rs_read_proc,
1996         .tiocmget = rs_tiocmget,
1997         .tiocmset = rs_tiocmset,
1998 };
1999
2000 /*
2001  * The serial driver boot-time initialization code!
2002  */
2003 static int __init rs_init(void)
2004 {
2005         unsigned long flags;
2006         struct serial_state * state;
2007
2008         if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2009                 return -ENODEV;
2010
2011         serial_driver = alloc_tty_driver(1);
2012         if (!serial_driver)
2013                 return -ENOMEM;
2014
2015         /*
2016          *  We request SERDAT and SERPER only, because the serial registers are
2017          *  too spreaded over the custom register space
2018          */
2019         if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2020                 return -EBUSY;
2021
2022         IRQ_ports = NULL;
2023
2024         show_serial_version();
2025
2026         /* Initialize the tty_driver structure */
2027
2028         serial_driver->owner = THIS_MODULE;
2029         serial_driver->driver_name = "amiserial";
2030         serial_driver->name = "ttyS";
2031         serial_driver->major = TTY_MAJOR;
2032         serial_driver->minor_start = 64;
2033         serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2034         serial_driver->subtype = SERIAL_TYPE_NORMAL;
2035         serial_driver->init_termios = tty_std_termios;
2036         serial_driver->init_termios.c_cflag =
2037                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2038         serial_driver->flags = TTY_DRIVER_REAL_RAW;
2039         tty_set_operations(serial_driver, &serial_ops);
2040
2041         if (tty_register_driver(serial_driver))
2042                 panic("Couldn't register serial driver\n");
2043
2044         state = rs_table;
2045         state->magic = SSTATE_MAGIC;
2046         state->port = (int)&custom.serdatr; /* Just to give it a value */
2047         state->line = 0;
2048         state->custom_divisor = 0;
2049         state->close_delay = 5*HZ/10;
2050         state->closing_wait = 30*HZ;
2051         state->icount.cts = state->icount.dsr = 
2052           state->icount.rng = state->icount.dcd = 0;
2053         state->icount.rx = state->icount.tx = 0;
2054         state->icount.frame = state->icount.parity = 0;
2055         state->icount.overrun = state->icount.brk = 0;
2056
2057         printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2058                        state->line);
2059
2060         /* Hardware set up */
2061
2062         state->baud_base = amiga_colorclock;
2063         state->xmit_fifo_size = 1;
2064
2065         local_irq_save(flags);
2066
2067         /* set ISRs, and then disable the rx interrupts */
2068         request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2069         request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2070
2071         /* turn off Rx and Tx interrupts */
2072         custom.intena = IF_RBF | IF_TBE;
2073         mb();
2074
2075         /* clear any pending interrupt */
2076         custom.intreq = IF_RBF | IF_TBE;
2077         mb();
2078
2079         local_irq_restore(flags);
2080
2081         /*
2082          * set the appropriate directions for the modem control flags,
2083          * and clear RTS and DTR
2084          */
2085         ciab.ddra |= (SER_DTR | SER_RTS);   /* outputs */
2086         ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR);  /* inputs */
2087
2088         return 0;
2089 }
2090
2091 static __exit void rs_exit(void) 
2092 {
2093         int error;
2094         struct async_struct *info = rs_table[0].info;
2095
2096         /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2097         tasklet_kill(&info->tlet);
2098         if ((error = tty_unregister_driver(serial_driver)))
2099                 printk("SERIAL: failed to unregister serial driver (%d)\n",
2100                        error);
2101         put_tty_driver(serial_driver);
2102
2103         if (info) {
2104           rs_table[0].info = NULL;
2105           kfree(info);
2106         }
2107
2108         if (tmp_buf) {
2109                 free_page((unsigned long) tmp_buf);
2110                 tmp_buf = NULL;
2111         }
2112
2113         release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2114 }
2115
2116 module_init(rs_init)
2117 module_exit(rs_exit)
2118
2119
2120 /*
2121  * ------------------------------------------------------------
2122  * Serial console driver
2123  * ------------------------------------------------------------
2124  */
2125 #ifdef CONFIG_SERIAL_CONSOLE
2126
2127 static void amiga_serial_putc(char c)
2128 {
2129         custom.serdat = (unsigned char)c | 0x100;
2130         while (!(custom.serdatr & 0x2000))
2131                 barrier();
2132 }
2133
2134 /*
2135  *      Print a string to the serial port trying not to disturb
2136  *      any possible real use of the port...
2137  *
2138  *      The console must be locked when we get here.
2139  */
2140 static void serial_console_write(struct console *co, const char *s,
2141                                 unsigned count)
2142 {
2143         unsigned short intena = custom.intenar;
2144
2145         custom.intena = IF_TBE;
2146
2147         while (count--) {
2148                 if (*s == '\n')
2149                         amiga_serial_putc('\r');
2150                 amiga_serial_putc(*s++);
2151         }
2152
2153         custom.intena = IF_SETCLR | (intena & IF_TBE);
2154 }
2155
2156 static struct tty_driver *serial_console_device(struct console *c, int *index)
2157 {
2158         *index = 0;
2159         return serial_driver;
2160 }
2161
2162 static struct console sercons = {
2163         .name =         "ttyS",
2164         .write =        serial_console_write,
2165         .device =       serial_console_device,
2166         .flags =        CON_PRINTBUFFER,
2167         .index =        -1,
2168 };
2169
2170 /*
2171  *      Register console.
2172  */
2173 static int __init amiserial_console_init(void)
2174 {
2175         register_console(&sercons);
2176         return 0;
2177 }
2178 console_initcall(amiserial_console_init);
2179 #endif
2180
2181 MODULE_LICENSE("GPL");