2 * Serial driver for the amiga builtin port.
4 * This code was created by taking serial.c version 4.30 from kernel
5 * release 2.3.22, replacing all hardware related stuff with the
6 * corresponding amiga hardware actions, and removing all irrelevant
7 * code. As a consequence, it uses many of the constants and names
8 * associated with the registers and bits of 16550 compatible UARTS -
9 * but only to keep track of status, etc in the state variables. It
10 * was done this was to make it easier to keep the code in line with
11 * (non hardware specific) changes to serial.c.
13 * The port is registered with the tty driver as minor device 64, and
14 * therefore other ports should should only use 65 upwards.
16 * Richard Lucock 28/12/99
18 * Copyright (C) 1991, 1992 Linus Torvalds
19 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
20 * 1998, 1999 Theodore Ts'o
25 * Serial driver configuration section. Here are the various options:
27 * SERIAL_PARANOIA_CHECK
28 * Check the magic number for the async_structure where
32 #include <linux/delay.h>
34 #undef SERIAL_PARANOIA_CHECK
35 #define SERIAL_DO_RESTART
37 /* Set of debugging defines */
39 #undef SERIAL_DEBUG_INTR
40 #undef SERIAL_DEBUG_OPEN
41 #undef SERIAL_DEBUG_FLOW
42 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
46 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
48 tty->name, (info->state->flags), serial_driver->refcount,info->count,tty->count,s)
54 * End of serial driver configuration section.
57 #include <linux/module.h>
59 #include <linux/types.h>
60 #include <linux/serial.h>
61 #include <linux/serialP.h>
62 #include <linux/serial_reg.h>
63 static char *serial_version = "4.30";
65 #include <linux/errno.h>
66 #include <linux/signal.h>
67 #include <linux/sched.h>
68 #include <linux/kernel.h>
69 #include <linux/timer.h>
70 #include <linux/interrupt.h>
71 #include <linux/tty.h>
72 #include <linux/tty_flip.h>
73 #include <linux/console.h>
74 #include <linux/major.h>
75 #include <linux/string.h>
76 #include <linux/fcntl.h>
77 #include <linux/ptrace.h>
78 #include <linux/ioport.h>
80 #include <linux/seq_file.h>
81 #include <linux/slab.h>
82 #include <linux/init.h>
83 #include <linux/bitops.h>
84 #include <linux/platform_device.h>
86 #include <asm/setup.h>
88 #include <asm/system.h>
92 #include <asm/amigahw.h>
93 #include <asm/amigaints.h>
95 #define custom amiga_custom
96 static char *serial_name = "Amiga-builtin serial driver";
98 static struct tty_driver *serial_driver;
100 /* number of characters left in xmit buffer before we ask for more */
101 #define WAKEUP_CHARS 256
103 static unsigned char current_ctl_bits;
105 static void change_speed(struct async_struct *info, struct ktermios *old);
106 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
109 static struct serial_state rs_table[1];
111 #define NR_PORTS ARRAY_SIZE(rs_table)
113 #include <asm/uaccess.h>
115 #define serial_isroot() (capable(CAP_SYS_ADMIN))
118 static inline int serial_paranoia_check(struct async_struct *info,
119 char *name, const char *routine)
121 #ifdef SERIAL_PARANOIA_CHECK
122 static const char *badmagic =
123 "Warning: bad magic number for serial struct (%s) in %s\n";
124 static const char *badinfo =
125 "Warning: null async_struct for (%s) in %s\n";
128 printk(badinfo, name, routine);
131 if (info->magic != SERIAL_MAGIC) {
132 printk(badmagic, name, routine);
139 /* some serial hardware definitions */
140 #define SDR_OVRUN (1<<15)
141 #define SDR_RBF (1<<14)
142 #define SDR_TBE (1<<13)
143 #define SDR_TSRE (1<<12)
145 #define SERPER_PARENB (1<<15)
147 #define AC_SETCLR (1<<15)
148 #define AC_UARTBRK (1<<11)
150 #define SER_DTR (1<<7)
151 #define SER_RTS (1<<6)
152 #define SER_DCD (1<<5)
153 #define SER_CTS (1<<4)
154 #define SER_DSR (1<<3)
156 static __inline__ void rtsdtr_ctrl(int bits)
158 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
162 * ------------------------------------------------------------
163 * rs_stop() and rs_start()
165 * This routines are called before setting or resetting tty->stopped.
166 * They enable or disable transmitter interrupts, as necessary.
167 * ------------------------------------------------------------
169 static void rs_stop(struct tty_struct *tty)
171 struct async_struct *info = tty->driver_data;
174 if (serial_paranoia_check(info, tty->name, "rs_stop"))
177 local_irq_save(flags);
178 if (info->IER & UART_IER_THRI) {
179 info->IER &= ~UART_IER_THRI;
180 /* disable Tx interrupt and remove any pending interrupts */
181 custom.intena = IF_TBE;
183 custom.intreq = IF_TBE;
186 local_irq_restore(flags);
189 static void rs_start(struct tty_struct *tty)
191 struct async_struct *info = tty->driver_data;
194 if (serial_paranoia_check(info, tty->name, "rs_start"))
197 local_irq_save(flags);
198 if (info->xmit.head != info->xmit.tail
200 && !(info->IER & UART_IER_THRI)) {
201 info->IER |= UART_IER_THRI;
202 custom.intena = IF_SETCLR | IF_TBE;
204 /* set a pending Tx Interrupt, transmitter should restart now */
205 custom.intreq = IF_SETCLR | IF_TBE;
208 local_irq_restore(flags);
212 * ----------------------------------------------------------------------
214 * Here starts the interrupt handling routines. All of the following
215 * subroutines are declared as inline and are folded into
216 * rs_interrupt(). They were separated out for readability's sake.
218 * Note: rs_interrupt() is a "fast" interrupt, which means that it
219 * runs with interrupts turned off. People who may want to modify
220 * rs_interrupt() should try to keep the interrupt handler as fast as
221 * possible. After you are done making modifications, it is not a bad
224 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
226 * and look at the resulting assemble code in serial.s.
228 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
229 * -----------------------------------------------------------------------
232 static void receive_chars(struct async_struct *info)
236 struct tty_struct *tty = info->tty;
237 unsigned char ch, flag;
238 struct async_icount *icount;
241 icount = &info->state->icount;
243 status = UART_LSR_DR; /* We obviously have a character! */
244 serdatr = custom.serdatr;
246 custom.intreq = IF_RBF;
249 if((serdatr & 0x1ff) == 0)
250 status |= UART_LSR_BI;
251 if(serdatr & SDR_OVRUN)
252 status |= UART_LSR_OE;
257 #ifdef SERIAL_DEBUG_INTR
258 printk("DR%02x:%02x...", ch, status);
263 * We don't handle parity or frame errors - but I have left
264 * the code in, since I'm not sure that the errors can't be
268 if (status & (UART_LSR_BI | UART_LSR_PE |
269 UART_LSR_FE | UART_LSR_OE)) {
271 * For statistics only
273 if (status & UART_LSR_BI) {
274 status &= ~(UART_LSR_FE | UART_LSR_PE);
276 } else if (status & UART_LSR_PE)
278 else if (status & UART_LSR_FE)
280 if (status & UART_LSR_OE)
284 * Now check to see if character should be
285 * ignored, and mask off conditions which
288 if (status & info->ignore_status_mask)
291 status &= info->read_status_mask;
293 if (status & (UART_LSR_BI)) {
294 #ifdef SERIAL_DEBUG_INTR
295 printk("handling break....");
298 if (info->state->flags & ASYNC_SAK)
300 } else if (status & UART_LSR_PE)
302 else if (status & UART_LSR_FE)
304 if (status & UART_LSR_OE) {
306 * Overrun is special, since it's
307 * reported immediately, and doesn't
308 * affect the current character
313 tty_insert_flip_char(tty, ch, flag);
315 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
316 tty_flip_buffer_push(tty);
321 static void transmit_chars(struct async_struct *info)
323 custom.intreq = IF_TBE;
326 custom.serdat = info->x_char | 0x100;
328 info->state->icount.tx++;
332 if (info->xmit.head == info->xmit.tail
333 || info->tty->stopped
334 || info->tty->hw_stopped) {
335 info->IER &= ~UART_IER_THRI;
336 custom.intena = IF_TBE;
341 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
343 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
344 info->state->icount.tx++;
346 if (CIRC_CNT(info->xmit.head,
348 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
349 tty_wakeup(info->tty);
351 #ifdef SERIAL_DEBUG_INTR
354 if (info->xmit.head == info->xmit.tail) {
355 custom.intena = IF_TBE;
357 info->IER &= ~UART_IER_THRI;
361 static void check_modem_status(struct async_struct *info)
363 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
364 unsigned char dstatus;
365 struct async_icount *icount;
367 /* Determine bits that have changed */
368 dstatus = status ^ current_ctl_bits;
369 current_ctl_bits = status;
372 icount = &info->state->icount;
373 /* update input line counters */
374 if (dstatus & SER_DSR)
376 if (dstatus & SER_DCD) {
378 #ifdef CONFIG_HARD_PPS
379 if ((info->state->flags & ASYNC_HARDPPS_CD) &&
384 if (dstatus & SER_CTS)
386 wake_up_interruptible(&info->delta_msr_wait);
389 if ((info->state->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
390 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
391 printk("ttyS%d CD now %s...", info->line,
392 (!(status & SER_DCD)) ? "on" : "off");
394 if (!(status & SER_DCD))
395 wake_up_interruptible(&info->open_wait);
397 #ifdef SERIAL_DEBUG_OPEN
398 printk("doing serial hangup...");
401 tty_hangup(info->tty);
404 if (info->state->flags & ASYNC_CTS_FLOW) {
405 if (info->tty->hw_stopped) {
406 if (!(status & SER_CTS)) {
407 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
408 printk("CTS tx start...");
410 info->tty->hw_stopped = 0;
411 info->IER |= UART_IER_THRI;
412 custom.intena = IF_SETCLR | IF_TBE;
414 /* set a pending Tx Interrupt, transmitter should restart now */
415 custom.intreq = IF_SETCLR | IF_TBE;
417 tty_wakeup(info->tty);
421 if ((status & SER_CTS)) {
422 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
423 printk("CTS tx stop...");
425 info->tty->hw_stopped = 1;
426 info->IER &= ~UART_IER_THRI;
427 /* disable Tx interrupt and remove any pending interrupts */
428 custom.intena = IF_TBE;
430 custom.intreq = IF_TBE;
437 static irqreturn_t ser_vbl_int( int irq, void *data)
439 /* vbl is just a periodic interrupt we tie into to update modem status */
440 struct async_struct *info = data;
442 * TBD - is it better to unregister from this interrupt or to
443 * ignore it if MSI is clear ?
445 if(info->IER & UART_IER_MSI)
446 check_modem_status(info);
450 static irqreturn_t ser_rx_int(int irq, void *dev_id)
452 struct serial_state *state = dev_id;
453 struct async_struct *info = state->info;
455 #ifdef SERIAL_DEBUG_INTR
456 printk("ser_rx_int...");
459 if (!info || !info->tty)
463 #ifdef SERIAL_DEBUG_INTR
469 static irqreturn_t ser_tx_int(int irq, void *dev_id)
471 struct serial_state *state = dev_id;
472 struct async_struct *info = state->info;
474 if (custom.serdatr & SDR_TBE) {
475 #ifdef SERIAL_DEBUG_INTR
476 printk("ser_tx_int...");
479 if (!info || !info->tty)
482 transmit_chars(info);
483 #ifdef SERIAL_DEBUG_INTR
491 * -------------------------------------------------------------------
492 * Here ends the serial interrupt routines.
493 * -------------------------------------------------------------------
497 * ---------------------------------------------------------------
498 * Low level utility subroutines for the serial driver: routines to
499 * figure out the appropriate timeout for an interrupt chain, routines
500 * to initialize and startup a serial port, and routines to shutdown a
501 * serial port. Useful stuff like that.
502 * ---------------------------------------------------------------
505 static int startup(struct async_struct * info)
511 page = get_zeroed_page(GFP_KERNEL);
515 local_irq_save(flags);
517 if (info->state->flags & ASYNC_INITIALIZED) {
525 info->xmit.buf = (unsigned char *) page;
527 #ifdef SERIAL_DEBUG_OPEN
528 printk("starting up ttys%d ...", info->line);
531 /* Clear anything in the input buffer */
533 custom.intreq = IF_RBF;
536 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
538 if (serial_isroot()) {
540 set_bit(TTY_IO_ERROR,
547 /* enable both Rx and Tx interrupts */
548 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
550 info->IER = UART_IER_MSI;
552 /* remember current state of the DCD and CTS bits */
553 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
556 if (info->tty->termios->c_cflag & CBAUD)
557 info->MCR = SER_DTR | SER_RTS;
558 rtsdtr_ctrl(info->MCR);
561 clear_bit(TTY_IO_ERROR, &info->tty->flags);
562 info->xmit.head = info->xmit.tail = 0;
565 * Set up the tty->alt_speed kludge
568 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
569 info->tty->alt_speed = 57600;
570 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
571 info->tty->alt_speed = 115200;
572 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
573 info->tty->alt_speed = 230400;
574 if ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
575 info->tty->alt_speed = 460800;
579 * and set the speed of the serial port
581 change_speed(info, NULL);
583 info->state->flags |= ASYNC_INITIALIZED;
584 local_irq_restore(flags);
588 local_irq_restore(flags);
593 * This routine will shutdown a serial port; interrupts are disabled, and
594 * DTR is dropped if the hangup on close termio flag is on.
596 static void shutdown(struct async_struct * info)
599 struct serial_state *state;
601 if (!(info->state->flags & ASYNC_INITIALIZED))
606 #ifdef SERIAL_DEBUG_OPEN
607 printk("Shutting down serial port %d ....\n", info->line);
610 local_irq_save(flags); /* Disable interrupts */
613 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
614 * here so the queue might never be waken up
616 wake_up_interruptible(&info->delta_msr_wait);
619 * Free the IRQ, if necessary
621 free_irq(IRQ_AMIGA_VERTB, info);
623 if (info->xmit.buf) {
624 free_page((unsigned long) info->xmit.buf);
625 info->xmit.buf = NULL;
629 custom.intena = IF_RBF | IF_TBE;
632 /* disable break condition */
633 custom.adkcon = AC_UARTBRK;
636 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
637 info->MCR &= ~(SER_DTR|SER_RTS);
638 rtsdtr_ctrl(info->MCR);
641 set_bit(TTY_IO_ERROR, &info->tty->flags);
643 info->state->flags &= ~ASYNC_INITIALIZED;
644 local_irq_restore(flags);
649 * This routine is called to set the UART divisor registers to match
650 * the specified baud rate for a serial port.
652 static void change_speed(struct async_struct *info,
653 struct ktermios *old_termios)
655 int quot = 0, baud_base, baud;
656 unsigned cflag, cval = 0;
660 if (!info->tty || !info->tty->termios)
662 cflag = info->tty->termios->c_cflag;
664 /* Byte size is always 8 bits plus parity bit if requested */
667 if (cflag & CSTOPB) {
671 if (cflag & PARENB) {
672 cval |= UART_LCR_PARITY;
675 if (!(cflag & PARODD))
676 cval |= UART_LCR_EPAR;
679 cval |= UART_LCR_SPAR;
682 /* Determine divisor based on baud rate */
683 baud = tty_get_baud_rate(info->tty);
685 baud = 9600; /* B0 transition handled in rs_set_termios */
686 baud_base = info->state->baud_base;
688 ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
689 quot = info->state->custom_divisor;
692 /* Special case since 134 is really 134.5 */
693 quot = (2*baud_base / 269);
695 quot = baud_base / baud;
697 /* If the quotient is zero refuse the change */
698 if (!quot && old_termios) {
699 /* FIXME: Will need updating for new tty in the end */
700 info->tty->termios->c_cflag &= ~CBAUD;
701 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
702 baud = tty_get_baud_rate(info->tty);
706 ((info->state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
707 quot = info->state->custom_divisor;
710 /* Special case since 134 is really 134.5 */
711 quot = (2*baud_base / 269);
713 quot = baud_base / baud;
716 /* As a last resort, if the quotient is zero, default to 9600 bps */
718 quot = baud_base / 9600;
720 info->timeout = ((info->state->xmit_fifo_size*HZ*bits*quot) / baud_base);
721 info->timeout += HZ/50; /* Add .02 seconds of slop */
723 /* CTS flow control flag and modem status interrupts */
724 info->IER &= ~UART_IER_MSI;
725 if (info->state->flags & ASYNC_HARDPPS_CD)
726 info->IER |= UART_IER_MSI;
727 if (cflag & CRTSCTS) {
728 info->state->flags |= ASYNC_CTS_FLOW;
729 info->IER |= UART_IER_MSI;
731 info->state->flags &= ~ASYNC_CTS_FLOW;
733 info->state->flags &= ~ASYNC_CHECK_CD;
735 info->state->flags |= ASYNC_CHECK_CD;
736 info->IER |= UART_IER_MSI;
739 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
743 * Set up parity check flag
746 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
747 if (I_INPCK(info->tty))
748 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
749 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
750 info->read_status_mask |= UART_LSR_BI;
753 * Characters to ignore
755 info->ignore_status_mask = 0;
756 if (I_IGNPAR(info->tty))
757 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
758 if (I_IGNBRK(info->tty)) {
759 info->ignore_status_mask |= UART_LSR_BI;
761 * If we're ignore parity and break indicators, ignore
762 * overruns too. (For real raw support).
764 if (I_IGNPAR(info->tty))
765 info->ignore_status_mask |= UART_LSR_OE;
768 * !!! ignore all characters if CREAD is not set
770 if ((cflag & CREAD) == 0)
771 info->ignore_status_mask |= UART_LSR_DR;
772 local_irq_save(flags);
777 /* Set up the baud rate */
780 /* Enable or disable parity bit */
782 if(cval & UART_LCR_PARITY)
783 serper |= (SERPER_PARENB);
785 custom.serper = serper;
789 local_irq_restore(flags);
792 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
794 struct async_struct *info;
797 info = tty->driver_data;
799 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
805 local_irq_save(flags);
806 if (CIRC_SPACE(info->xmit.head,
808 SERIAL_XMIT_SIZE) == 0) {
809 local_irq_restore(flags);
813 info->xmit.buf[info->xmit.head++] = ch;
814 info->xmit.head &= SERIAL_XMIT_SIZE-1;
815 local_irq_restore(flags);
819 static void rs_flush_chars(struct tty_struct *tty)
821 struct async_struct *info = tty->driver_data;
824 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
827 if (info->xmit.head == info->xmit.tail
833 local_irq_save(flags);
834 info->IER |= UART_IER_THRI;
835 custom.intena = IF_SETCLR | IF_TBE;
837 /* set a pending Tx Interrupt, transmitter should restart now */
838 custom.intreq = IF_SETCLR | IF_TBE;
840 local_irq_restore(flags);
843 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
846 struct async_struct *info;
849 info = tty->driver_data;
851 if (serial_paranoia_check(info, tty->name, "rs_write"))
857 local_irq_save(flags);
859 c = CIRC_SPACE_TO_END(info->xmit.head,
867 memcpy(info->xmit.buf + info->xmit.head, buf, c);
868 info->xmit.head = ((info->xmit.head + c) &
869 (SERIAL_XMIT_SIZE-1));
874 local_irq_restore(flags);
876 if (info->xmit.head != info->xmit.tail
879 && !(info->IER & UART_IER_THRI)) {
880 info->IER |= UART_IER_THRI;
882 custom.intena = IF_SETCLR | IF_TBE;
884 /* set a pending Tx Interrupt, transmitter should restart now */
885 custom.intreq = IF_SETCLR | IF_TBE;
887 local_irq_restore(flags);
892 static int rs_write_room(struct tty_struct *tty)
894 struct async_struct *info = tty->driver_data;
896 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
898 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
901 static int rs_chars_in_buffer(struct tty_struct *tty)
903 struct async_struct *info = tty->driver_data;
905 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
907 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
910 static void rs_flush_buffer(struct tty_struct *tty)
912 struct async_struct *info = tty->driver_data;
915 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
917 local_irq_save(flags);
918 info->xmit.head = info->xmit.tail = 0;
919 local_irq_restore(flags);
924 * This function is used to send a high-priority XON/XOFF character to
927 static void rs_send_xchar(struct tty_struct *tty, char ch)
929 struct async_struct *info = tty->driver_data;
932 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
937 /* Make sure transmit interrupts are on */
940 local_irq_save(flags);
941 if(!(custom.intenar & IF_TBE)) {
942 custom.intena = IF_SETCLR | IF_TBE;
944 /* set a pending Tx Interrupt, transmitter should restart now */
945 custom.intreq = IF_SETCLR | IF_TBE;
948 local_irq_restore(flags);
950 info->IER |= UART_IER_THRI;
955 * ------------------------------------------------------------
958 * This routine is called by the upper-layer tty layer to signal that
959 * incoming characters should be throttled.
960 * ------------------------------------------------------------
962 static void rs_throttle(struct tty_struct * tty)
964 struct async_struct *info = tty->driver_data;
966 #ifdef SERIAL_DEBUG_THROTTLE
969 printk("throttle %s: %d....\n", tty_name(tty, buf),
970 tty->ldisc.chars_in_buffer(tty));
973 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
977 rs_send_xchar(tty, STOP_CHAR(tty));
979 if (tty->termios->c_cflag & CRTSCTS)
980 info->MCR &= ~SER_RTS;
982 local_irq_save(flags);
983 rtsdtr_ctrl(info->MCR);
984 local_irq_restore(flags);
987 static void rs_unthrottle(struct tty_struct * tty)
989 struct async_struct *info = tty->driver_data;
991 #ifdef SERIAL_DEBUG_THROTTLE
994 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
995 tty->ldisc.chars_in_buffer(tty));
998 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1005 rs_send_xchar(tty, START_CHAR(tty));
1007 if (tty->termios->c_cflag & CRTSCTS)
1008 info->MCR |= SER_RTS;
1009 local_irq_save(flags);
1010 rtsdtr_ctrl(info->MCR);
1011 local_irq_restore(flags);
1015 * ------------------------------------------------------------
1016 * rs_ioctl() and friends
1017 * ------------------------------------------------------------
1020 static int get_serial_info(struct async_struct * info,
1021 struct serial_struct __user * retinfo)
1023 struct serial_struct tmp;
1024 struct serial_state *state = info->state;
1028 memset(&tmp, 0, sizeof(tmp));
1030 tmp.type = state->type;
1031 tmp.line = state->line;
1032 tmp.port = state->port;
1033 tmp.irq = state->irq;
1034 tmp.flags = state->flags;
1035 tmp.xmit_fifo_size = state->xmit_fifo_size;
1036 tmp.baud_base = state->baud_base;
1037 tmp.close_delay = state->close_delay;
1038 tmp.closing_wait = state->closing_wait;
1039 tmp.custom_divisor = state->custom_divisor;
1041 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1046 static int set_serial_info(struct async_struct * info,
1047 struct serial_struct __user * new_info)
1049 struct serial_struct new_serial;
1050 struct serial_state old_state, *state;
1051 unsigned int change_irq,change_port;
1054 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1058 state = info->state;
1061 change_irq = new_serial.irq != state->irq;
1062 change_port = (new_serial.port != state->port);
1063 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1068 if (!serial_isroot()) {
1069 if ((new_serial.baud_base != state->baud_base) ||
1070 (new_serial.close_delay != state->close_delay) ||
1071 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1072 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1073 (state->flags & ~ASYNC_USR_MASK)))
1075 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1076 (new_serial.flags & ASYNC_USR_MASK));
1077 state->custom_divisor = new_serial.custom_divisor;
1078 goto check_and_exit;
1081 if (new_serial.baud_base < 9600) {
1087 * OK, past this point, all the error checking has been done.
1088 * At this point, we start making changes.....
1091 state->baud_base = new_serial.baud_base;
1092 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1093 (new_serial.flags & ASYNC_FLAGS));
1094 state->custom_divisor = new_serial.custom_divisor;
1095 state->close_delay = new_serial.close_delay * HZ/100;
1096 state->closing_wait = new_serial.closing_wait * HZ/100;
1097 info->tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1100 if (state->flags & ASYNC_INITIALIZED) {
1101 if (((old_state.flags & ASYNC_SPD_MASK) !=
1102 (state->flags & ASYNC_SPD_MASK)) ||
1103 (old_state.custom_divisor != state->custom_divisor)) {
1104 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1105 info->tty->alt_speed = 57600;
1106 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1107 info->tty->alt_speed = 115200;
1108 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1109 info->tty->alt_speed = 230400;
1110 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1111 info->tty->alt_speed = 460800;
1112 change_speed(info, NULL);
1115 retval = startup(info);
1122 * get_lsr_info - get line status register info
1124 * Purpose: Let user call ioctl() to get info when the UART physically
1125 * is emptied. On bus types like RS485, the transmitter must
1126 * release the bus after transmitting. This must be done when
1127 * the transmit shift register is empty, not be done when the
1128 * transmit holding register is empty. This functionality
1129 * allows an RS485 driver to be written in user space.
1131 static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
1133 unsigned char status;
1134 unsigned int result;
1135 unsigned long flags;
1137 local_irq_save(flags);
1138 status = custom.serdatr;
1140 local_irq_restore(flags);
1141 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1142 if (copy_to_user(value, &result, sizeof(int)))
1148 static int rs_tiocmget(struct tty_struct *tty)
1150 struct async_struct * info = tty->driver_data;
1151 unsigned char control, status;
1152 unsigned long flags;
1154 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1156 if (tty->flags & (1 << TTY_IO_ERROR))
1159 control = info->MCR;
1160 local_irq_save(flags);
1162 local_irq_restore(flags);
1163 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1164 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1165 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1166 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1167 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1170 static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1173 struct async_struct * info = tty->driver_data;
1174 unsigned long flags;
1176 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1178 if (tty->flags & (1 << TTY_IO_ERROR))
1181 local_irq_save(flags);
1182 if (set & TIOCM_RTS)
1183 info->MCR |= SER_RTS;
1184 if (set & TIOCM_DTR)
1185 info->MCR |= SER_DTR;
1186 if (clear & TIOCM_RTS)
1187 info->MCR &= ~SER_RTS;
1188 if (clear & TIOCM_DTR)
1189 info->MCR &= ~SER_DTR;
1190 rtsdtr_ctrl(info->MCR);
1191 local_irq_restore(flags);
1196 * rs_break() --- routine which turns the break handling on or off
1198 static int rs_break(struct tty_struct *tty, int break_state)
1200 struct async_struct * info = tty->driver_data;
1201 unsigned long flags;
1203 if (serial_paranoia_check(info, tty->name, "rs_break"))
1206 local_irq_save(flags);
1207 if (break_state == -1)
1208 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1210 custom.adkcon = AC_UARTBRK;
1212 local_irq_restore(flags);
1217 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1218 * Return: write counters to the user passed counter struct
1219 * NB: both 1->0 and 0->1 transitions are counted except for
1220 * RI where only 0->1 is counted.
1222 static int rs_get_icount(struct tty_struct *tty,
1223 struct serial_icounter_struct *icount)
1225 struct async_struct *info = tty->driver_data;
1226 struct async_icount cnow;
1227 unsigned long flags;
1229 local_irq_save(flags);
1230 cnow = info->state->icount;
1231 local_irq_restore(flags);
1232 icount->cts = cnow.cts;
1233 icount->dsr = cnow.dsr;
1234 icount->rng = cnow.rng;
1235 icount->dcd = cnow.dcd;
1236 icount->rx = cnow.rx;
1237 icount->tx = cnow.tx;
1238 icount->frame = cnow.frame;
1239 icount->overrun = cnow.overrun;
1240 icount->parity = cnow.parity;
1241 icount->brk = cnow.brk;
1242 icount->buf_overrun = cnow.buf_overrun;
1247 static int rs_ioctl(struct tty_struct *tty,
1248 unsigned int cmd, unsigned long arg)
1250 struct async_struct * info = tty->driver_data;
1251 struct async_icount cprev, cnow; /* kernel counter temps */
1252 void __user *argp = (void __user *)arg;
1253 unsigned long flags;
1255 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1258 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1259 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1260 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1261 if (tty->flags & (1 << TTY_IO_ERROR))
1267 return get_serial_info(info, argp);
1269 return set_serial_info(info, argp);
1273 case TIOCSERGETLSR: /* Get line status register */
1274 return get_lsr_info(info, argp);
1276 case TIOCSERGSTRUCT:
1277 if (copy_to_user(argp,
1278 info, sizeof(struct async_struct)))
1283 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1284 * - mask passed in arg for lines of interest
1285 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1286 * Caller should use TIOCGICOUNT to see which one it was
1289 local_irq_save(flags);
1290 /* note the counters on entry */
1291 cprev = info->state->icount;
1292 local_irq_restore(flags);
1294 interruptible_sleep_on(&info->delta_msr_wait);
1295 /* see if a signal did it */
1296 if (signal_pending(current))
1297 return -ERESTARTSYS;
1298 local_irq_save(flags);
1299 cnow = info->state->icount; /* atomic copy */
1300 local_irq_restore(flags);
1301 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1302 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1303 return -EIO; /* no change => error */
1304 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1305 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1306 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1307 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1316 /* "setserial -W" is called in Debian boot */
1317 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1321 return -ENOIOCTLCMD;
1326 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1328 struct async_struct *info = tty->driver_data;
1329 unsigned long flags;
1330 unsigned int cflag = tty->termios->c_cflag;
1332 change_speed(info, old_termios);
1334 /* Handle transition to B0 status */
1335 if ((old_termios->c_cflag & CBAUD) &&
1337 info->MCR &= ~(SER_DTR|SER_RTS);
1338 local_irq_save(flags);
1339 rtsdtr_ctrl(info->MCR);
1340 local_irq_restore(flags);
1343 /* Handle transition away from B0 status */
1344 if (!(old_termios->c_cflag & CBAUD) &&
1346 info->MCR |= SER_DTR;
1347 if (!(tty->termios->c_cflag & CRTSCTS) ||
1348 !test_bit(TTY_THROTTLED, &tty->flags)) {
1349 info->MCR |= SER_RTS;
1351 local_irq_save(flags);
1352 rtsdtr_ctrl(info->MCR);
1353 local_irq_restore(flags);
1356 /* Handle turning off CRTSCTS */
1357 if ((old_termios->c_cflag & CRTSCTS) &&
1358 !(tty->termios->c_cflag & CRTSCTS)) {
1359 tty->hw_stopped = 0;
1365 * No need to wake up processes in open wait, since they
1366 * sample the CLOCAL flag once, and don't recheck it.
1367 * XXX It's not clear whether the current behavior is correct
1368 * or not. Hence, this may change.....
1370 if (!(old_termios->c_cflag & CLOCAL) &&
1371 (tty->termios->c_cflag & CLOCAL))
1372 wake_up_interruptible(&info->open_wait);
1377 * ------------------------------------------------------------
1380 * This routine is called when the serial port gets closed. First, we
1381 * wait for the last remaining data to be sent. Then, we unlink its
1382 * async structure from the interrupt chain if necessary, and we free
1383 * that IRQ if nothing is left in the chain.
1384 * ------------------------------------------------------------
1386 static void rs_close(struct tty_struct *tty, struct file * filp)
1388 struct async_struct * info = tty->driver_data;
1389 struct serial_state *state;
1390 unsigned long flags;
1392 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1395 state = info->state;
1397 local_irq_save(flags);
1399 if (tty_hung_up_p(filp)) {
1400 DBG_CNT("before DEC-hung");
1401 local_irq_restore(flags);
1405 #ifdef SERIAL_DEBUG_OPEN
1406 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1408 if ((tty->count == 1) && (state->count != 1)) {
1410 * Uh, oh. tty->count is 1, which means that the tty
1411 * structure will be freed. state->count should always
1412 * be one in these conditions. If it's greater than
1413 * one, we've got real problems, since it means the
1414 * serial port won't be shutdown.
1416 printk("rs_close: bad serial port count; tty->count is 1, "
1417 "state->count is %d\n", state->count);
1420 if (--state->count < 0) {
1421 printk("rs_close: bad serial port count for ttys%d: %d\n",
1422 state->line, state->count);
1426 DBG_CNT("before DEC-2");
1427 local_irq_restore(flags);
1430 state->flags |= ASYNC_CLOSING;
1432 * Now we wait for the transmit buffer to clear; and we notify
1433 * the line discipline to only process XON/XOFF characters.
1436 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1437 tty_wait_until_sent(tty, state->closing_wait);
1439 * At this point we stop accepting input. To do this, we
1440 * disable the receive line status interrupts, and tell the
1441 * interrupt driver to stop checking the data ready bit in the
1442 * line status register.
1444 info->read_status_mask &= ~UART_LSR_DR;
1445 if (state->flags & ASYNC_INITIALIZED) {
1446 /* disable receive interrupts */
1447 custom.intena = IF_RBF;
1449 /* clear any pending receive interrupt */
1450 custom.intreq = IF_RBF;
1454 * Before we drop DTR, make sure the UART transmitter
1455 * has completely drained; this is especially
1456 * important if there is a transmit FIFO!
1458 rs_wait_until_sent(tty, info->timeout);
1461 rs_flush_buffer(tty);
1463 tty_ldisc_flush(tty);
1466 if (info->blocked_open) {
1467 if (state->close_delay) {
1468 msleep_interruptible(jiffies_to_msecs(state->close_delay));
1470 wake_up_interruptible(&info->open_wait);
1472 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1473 wake_up_interruptible(&info->close_wait);
1474 local_irq_restore(flags);
1478 * rs_wait_until_sent() --- wait until the transmitter is empty
1480 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1482 struct async_struct * info = tty->driver_data;
1483 unsigned long orig_jiffies, char_time;
1486 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1489 if (info->state->xmit_fifo_size == 0)
1490 return; /* Just in case.... */
1492 orig_jiffies = jiffies;
1495 * Set the check interval to be 1/5 of the estimated time to
1496 * send a single character, and make it at least 1. The check
1497 * interval should also be less than the timeout.
1499 * Note: we have to use pretty tight timings here to satisfy
1502 char_time = (info->timeout - HZ/50) / info->state->xmit_fifo_size;
1503 char_time = char_time / 5;
1507 char_time = min_t(unsigned long, char_time, timeout);
1509 * If the transmitter hasn't cleared in twice the approximate
1510 * amount of time to send the entire FIFO, it probably won't
1511 * ever clear. This assumes the UART isn't doing flow
1512 * control, which is currently the case. Hence, if it ever
1513 * takes longer than info->timeout, this is probably due to a
1514 * UART bug of some kind. So, we clamp the timeout parameter at
1517 if (!timeout || timeout > 2*info->timeout)
1518 timeout = 2*info->timeout;
1519 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1520 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1521 printk("jiff=%lu...", jiffies);
1523 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1524 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1525 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1527 msleep_interruptible(jiffies_to_msecs(char_time));
1528 if (signal_pending(current))
1530 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1533 __set_current_state(TASK_RUNNING);
1535 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1536 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1541 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1543 static void rs_hangup(struct tty_struct *tty)
1545 struct async_struct * info = tty->driver_data;
1546 struct serial_state *state = info->state;
1548 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1551 state = info->state;
1553 rs_flush_buffer(tty);
1556 state->flags &= ~ASYNC_NORMAL_ACTIVE;
1558 wake_up_interruptible(&info->open_wait);
1562 * ------------------------------------------------------------
1563 * rs_open() and friends
1564 * ------------------------------------------------------------
1566 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1567 struct async_struct *info)
1569 #ifdef DECLARE_WAITQUEUE
1570 DECLARE_WAITQUEUE(wait, current);
1572 struct wait_queue wait = { current, NULL };
1574 struct serial_state *state = info->state;
1576 int do_clocal = 0, extra_count = 0;
1577 unsigned long flags;
1580 * If the device is in the middle of being closed, then block
1581 * until it's done, and then try again.
1583 if (tty_hung_up_p(filp) ||
1584 (state->flags & ASYNC_CLOSING)) {
1585 if (state->flags & ASYNC_CLOSING)
1586 interruptible_sleep_on(&info->close_wait);
1587 #ifdef SERIAL_DO_RESTART
1588 return ((state->flags & ASYNC_HUP_NOTIFY) ?
1589 -EAGAIN : -ERESTARTSYS);
1596 * If non-blocking mode is set, or the port is not enabled,
1597 * then make the check up front and then exit.
1599 if ((filp->f_flags & O_NONBLOCK) ||
1600 (tty->flags & (1 << TTY_IO_ERROR))) {
1601 state->flags |= ASYNC_NORMAL_ACTIVE;
1605 if (tty->termios->c_cflag & CLOCAL)
1609 * Block waiting for the carrier detect and the line to become
1610 * free (i.e., not in use by the callout). While we are in
1611 * this loop, state->count is dropped by one, so that
1612 * rs_close() knows when to free things. We restore it upon
1613 * exit, either normal or abnormal.
1616 add_wait_queue(&info->open_wait, &wait);
1617 #ifdef SERIAL_DEBUG_OPEN
1618 printk("block_til_ready before block: ttys%d, count = %d\n",
1619 state->line, state->count);
1621 local_irq_save(flags);
1622 if (!tty_hung_up_p(filp)) {
1626 local_irq_restore(flags);
1627 info->blocked_open++;
1629 local_irq_save(flags);
1630 if (tty->termios->c_cflag & CBAUD)
1631 rtsdtr_ctrl(SER_DTR|SER_RTS);
1632 local_irq_restore(flags);
1633 set_current_state(TASK_INTERRUPTIBLE);
1634 if (tty_hung_up_p(filp) ||
1635 !(state->flags & ASYNC_INITIALIZED)) {
1636 #ifdef SERIAL_DO_RESTART
1637 if (state->flags & ASYNC_HUP_NOTIFY)
1640 retval = -ERESTARTSYS;
1646 if (!(state->flags & ASYNC_CLOSING) &&
1647 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1649 if (signal_pending(current)) {
1650 retval = -ERESTARTSYS;
1653 #ifdef SERIAL_DEBUG_OPEN
1654 printk("block_til_ready blocking: ttys%d, count = %d\n",
1655 info->line, state->count);
1661 __set_current_state(TASK_RUNNING);
1662 remove_wait_queue(&info->open_wait, &wait);
1665 info->blocked_open--;
1666 #ifdef SERIAL_DEBUG_OPEN
1667 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1668 info->line, state->count);
1672 state->flags |= ASYNC_NORMAL_ACTIVE;
1676 static int get_async_struct(int line, struct async_struct **ret_info)
1678 struct async_struct *info;
1679 struct serial_state *sstate;
1681 sstate = rs_table + line;
1684 *ret_info = sstate->info;
1687 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
1692 #ifdef DECLARE_WAITQUEUE
1693 init_waitqueue_head(&info->open_wait);
1694 init_waitqueue_head(&info->close_wait);
1695 init_waitqueue_head(&info->delta_msr_wait);
1697 info->state = sstate;
1700 *ret_info = sstate->info;
1703 *ret_info = sstate->info = info;
1708 * This routine is called whenever a serial port is opened. It
1709 * enables interrupts for a serial port, linking in its async structure into
1710 * the IRQ chain. It also performs the serial-specific
1711 * initialization for the tty structure.
1713 static int rs_open(struct tty_struct *tty, struct file * filp)
1715 struct async_struct *info;
1718 retval = get_async_struct(tty->index, &info);
1722 tty->driver_data = info;
1724 if (serial_paranoia_check(info, tty->name, "rs_open"))
1727 #ifdef SERIAL_DEBUG_OPEN
1728 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1730 info->tty->low_latency = (info->state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1733 * If the port is the middle of closing, bail out now
1735 if (tty_hung_up_p(filp) ||
1736 (info->state->flags & ASYNC_CLOSING)) {
1737 if (info->state->flags & ASYNC_CLOSING)
1738 interruptible_sleep_on(&info->close_wait);
1739 #ifdef SERIAL_DO_RESTART
1740 return ((info->state->flags & ASYNC_HUP_NOTIFY) ?
1741 -EAGAIN : -ERESTARTSYS);
1748 * Start up serial port
1750 retval = startup(info);
1755 retval = block_til_ready(tty, filp, info);
1757 #ifdef SERIAL_DEBUG_OPEN
1758 printk("rs_open returning after block_til_ready with %d\n",
1764 #ifdef SERIAL_DEBUG_OPEN
1765 printk("rs_open %s successful...", tty->name);
1771 * /proc fs routines....
1774 static inline void line_info(struct seq_file *m, struct serial_state *state)
1776 struct async_struct *info = state->info, scr_info;
1777 char stat_buf[30], control, status;
1778 unsigned long flags;
1780 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1783 * Figure out the current RS-232 lines
1786 info = &scr_info; /* This is just for serial_{in,out} */
1791 local_irq_save(flags);
1793 control = info ? info->MCR : status;
1794 local_irq_restore(flags);
1798 if(!(control & SER_RTS))
1799 strcat(stat_buf, "|RTS");
1800 if(!(status & SER_CTS))
1801 strcat(stat_buf, "|CTS");
1802 if(!(control & SER_DTR))
1803 strcat(stat_buf, "|DTR");
1804 if(!(status & SER_DSR))
1805 strcat(stat_buf, "|DSR");
1806 if(!(status & SER_DCD))
1807 strcat(stat_buf, "|CD");
1810 seq_printf(m, " baud:%d", state->baud_base / info->quot);
1813 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1815 if (state->icount.frame)
1816 seq_printf(m, " fe:%d", state->icount.frame);
1818 if (state->icount.parity)
1819 seq_printf(m, " pe:%d", state->icount.parity);
1821 if (state->icount.brk)
1822 seq_printf(m, " brk:%d", state->icount.brk);
1824 if (state->icount.overrun)
1825 seq_printf(m, " oe:%d", state->icount.overrun);
1828 * Last thing is the RS-232 status lines
1830 seq_printf(m, " %s\n", stat_buf+1);
1833 static int rs_proc_show(struct seq_file *m, void *v)
1835 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1836 line_info(m, &rs_table[0]);
1840 static int rs_proc_open(struct inode *inode, struct file *file)
1842 return single_open(file, rs_proc_show, NULL);
1845 static const struct file_operations rs_proc_fops = {
1846 .owner = THIS_MODULE,
1847 .open = rs_proc_open,
1849 .llseek = seq_lseek,
1850 .release = single_release,
1854 * ---------------------------------------------------------------------
1855 * rs_init() and friends
1857 * rs_init() is called at boot-time to initialize the serial driver.
1858 * ---------------------------------------------------------------------
1862 * This routine prints out the appropriate serial driver version
1863 * number, and identifies which options were configured into this
1866 static void show_serial_version(void)
1868 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1872 static const struct tty_operations serial_ops = {
1876 .put_char = rs_put_char,
1877 .flush_chars = rs_flush_chars,
1878 .write_room = rs_write_room,
1879 .chars_in_buffer = rs_chars_in_buffer,
1880 .flush_buffer = rs_flush_buffer,
1882 .throttle = rs_throttle,
1883 .unthrottle = rs_unthrottle,
1884 .set_termios = rs_set_termios,
1887 .hangup = rs_hangup,
1888 .break_ctl = rs_break,
1889 .send_xchar = rs_send_xchar,
1890 .wait_until_sent = rs_wait_until_sent,
1891 .tiocmget = rs_tiocmget,
1892 .tiocmset = rs_tiocmset,
1893 .get_icount = rs_get_icount,
1894 .proc_fops = &rs_proc_fops,
1898 * The serial driver boot-time initialization code!
1900 static int __init amiga_serial_probe(struct platform_device *pdev)
1902 unsigned long flags;
1903 struct serial_state * state;
1906 serial_driver = alloc_tty_driver(NR_PORTS);
1910 show_serial_version();
1912 /* Initialize the tty_driver structure */
1914 serial_driver->driver_name = "amiserial";
1915 serial_driver->name = "ttyS";
1916 serial_driver->major = TTY_MAJOR;
1917 serial_driver->minor_start = 64;
1918 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1919 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1920 serial_driver->init_termios = tty_std_termios;
1921 serial_driver->init_termios.c_cflag =
1922 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1923 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1924 tty_set_operations(serial_driver, &serial_ops);
1926 error = tty_register_driver(serial_driver);
1928 goto fail_put_tty_driver;
1931 state->port = (int)&custom.serdatr; /* Just to give it a value */
1933 state->custom_divisor = 0;
1934 state->close_delay = 5*HZ/10;
1935 state->closing_wait = 30*HZ;
1936 state->icount.cts = state->icount.dsr =
1937 state->icount.rng = state->icount.dcd = 0;
1938 state->icount.rx = state->icount.tx = 0;
1939 state->icount.frame = state->icount.parity = 0;
1940 state->icount.overrun = state->icount.brk = 0;
1942 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1945 /* Hardware set up */
1947 state->baud_base = amiga_colorclock;
1948 state->xmit_fifo_size = 1;
1950 /* set ISRs, and then disable the rx interrupts */
1951 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1953 goto fail_unregister;
1955 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
1956 "serial RX", state);
1960 local_irq_save(flags);
1962 /* turn off Rx and Tx interrupts */
1963 custom.intena = IF_RBF | IF_TBE;
1966 /* clear any pending interrupt */
1967 custom.intreq = IF_RBF | IF_TBE;
1970 local_irq_restore(flags);
1973 * set the appropriate directions for the modem control flags,
1974 * and clear RTS and DTR
1976 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1977 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1979 platform_set_drvdata(pdev, state);
1984 free_irq(IRQ_AMIGA_TBE, state);
1986 tty_unregister_driver(serial_driver);
1987 fail_put_tty_driver:
1988 put_tty_driver(serial_driver);
1992 static int __exit amiga_serial_remove(struct platform_device *pdev)
1995 struct serial_state *state = platform_get_drvdata(pdev);
1996 struct async_struct *info = state->info;
1998 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
1999 if ((error = tty_unregister_driver(serial_driver)))
2000 printk("SERIAL: failed to unregister serial driver (%d)\n",
2002 put_tty_driver(serial_driver);
2004 rs_table[0].info = NULL;
2007 free_irq(IRQ_AMIGA_TBE, rs_table);
2008 free_irq(IRQ_AMIGA_RBF, rs_table);
2010 platform_set_drvdata(pdev, NULL);
2015 static struct platform_driver amiga_serial_driver = {
2016 .remove = __exit_p(amiga_serial_remove),
2018 .name = "amiga-serial",
2019 .owner = THIS_MODULE,
2023 static int __init amiga_serial_init(void)
2025 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
2028 module_init(amiga_serial_init);
2030 static void __exit amiga_serial_exit(void)
2032 platform_driver_unregister(&amiga_serial_driver);
2035 module_exit(amiga_serial_exit);
2038 #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2041 * ------------------------------------------------------------
2042 * Serial console driver
2043 * ------------------------------------------------------------
2046 static void amiga_serial_putc(char c)
2048 custom.serdat = (unsigned char)c | 0x100;
2049 while (!(custom.serdatr & 0x2000))
2054 * Print a string to the serial port trying not to disturb
2055 * any possible real use of the port...
2057 * The console must be locked when we get here.
2059 static void serial_console_write(struct console *co, const char *s,
2062 unsigned short intena = custom.intenar;
2064 custom.intena = IF_TBE;
2068 amiga_serial_putc('\r');
2069 amiga_serial_putc(*s++);
2072 custom.intena = IF_SETCLR | (intena & IF_TBE);
2075 static struct tty_driver *serial_console_device(struct console *c, int *index)
2078 return serial_driver;
2081 static struct console sercons = {
2083 .write = serial_console_write,
2084 .device = serial_console_device,
2085 .flags = CON_PRINTBUFFER,
2092 static int __init amiserial_console_init(void)
2094 register_console(&sercons);
2097 console_initcall(amiserial_console_init);
2099 #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2101 MODULE_LICENSE("GPL");
2102 MODULE_ALIAS("platform:amiga-serial");