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->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 tty_struct *tty, struct serial_state *info,
106 struct ktermios *old);
107 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
110 static struct serial_state rs_table[1];
112 #define NR_PORTS ARRAY_SIZE(rs_table)
114 #include <asm/uaccess.h>
116 #define serial_isroot() (capable(CAP_SYS_ADMIN))
119 static inline int serial_paranoia_check(struct serial_state *info,
120 char *name, const char *routine)
122 #ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct (%s) in %s\n";
125 static const char *badinfo =
126 "Warning: null async_struct for (%s) in %s\n";
129 printk(badinfo, name, routine);
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
140 /* some serial hardware definitions */
141 #define SDR_OVRUN (1<<15)
142 #define SDR_RBF (1<<14)
143 #define SDR_TBE (1<<13)
144 #define SDR_TSRE (1<<12)
146 #define SERPER_PARENB (1<<15)
148 #define AC_SETCLR (1<<15)
149 #define AC_UARTBRK (1<<11)
151 #define SER_DTR (1<<7)
152 #define SER_RTS (1<<6)
153 #define SER_DCD (1<<5)
154 #define SER_CTS (1<<4)
155 #define SER_DSR (1<<3)
157 static __inline__ void rtsdtr_ctrl(int bits)
159 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
163 * ------------------------------------------------------------
164 * rs_stop() and rs_start()
166 * This routines are called before setting or resetting tty->stopped.
167 * They enable or disable transmitter interrupts, as necessary.
168 * ------------------------------------------------------------
170 static void rs_stop(struct tty_struct *tty)
172 struct serial_state *info = tty->driver_data;
175 if (serial_paranoia_check(info, tty->name, "rs_stop"))
178 local_irq_save(flags);
179 if (info->IER & UART_IER_THRI) {
180 info->IER &= ~UART_IER_THRI;
181 /* disable Tx interrupt and remove any pending interrupts */
182 custom.intena = IF_TBE;
184 custom.intreq = IF_TBE;
187 local_irq_restore(flags);
190 static void rs_start(struct tty_struct *tty)
192 struct serial_state *info = tty->driver_data;
195 if (serial_paranoia_check(info, tty->name, "rs_start"))
198 local_irq_save(flags);
199 if (info->xmit.head != info->xmit.tail
201 && !(info->IER & UART_IER_THRI)) {
202 info->IER |= UART_IER_THRI;
203 custom.intena = IF_SETCLR | IF_TBE;
205 /* set a pending Tx Interrupt, transmitter should restart now */
206 custom.intreq = IF_SETCLR | IF_TBE;
209 local_irq_restore(flags);
213 * ----------------------------------------------------------------------
215 * Here starts the interrupt handling routines. All of the following
216 * subroutines are declared as inline and are folded into
217 * rs_interrupt(). They were separated out for readability's sake.
219 * Note: rs_interrupt() is a "fast" interrupt, which means that it
220 * runs with interrupts turned off. People who may want to modify
221 * rs_interrupt() should try to keep the interrupt handler as fast as
222 * possible. After you are done making modifications, it is not a bad
225 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
227 * and look at the resulting assemble code in serial.s.
229 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
230 * -----------------------------------------------------------------------
233 static void receive_chars(struct serial_state *info)
237 struct tty_struct *tty = info->tport.tty;
238 unsigned char ch, flag;
239 struct async_icount *icount;
242 icount = &info->icount;
244 status = UART_LSR_DR; /* We obviously have a character! */
245 serdatr = custom.serdatr;
247 custom.intreq = IF_RBF;
250 if((serdatr & 0x1ff) == 0)
251 status |= UART_LSR_BI;
252 if(serdatr & SDR_OVRUN)
253 status |= UART_LSR_OE;
258 #ifdef SERIAL_DEBUG_INTR
259 printk("DR%02x:%02x...", ch, status);
264 * We don't handle parity or frame errors - but I have left
265 * the code in, since I'm not sure that the errors can't be
269 if (status & (UART_LSR_BI | UART_LSR_PE |
270 UART_LSR_FE | UART_LSR_OE)) {
272 * For statistics only
274 if (status & UART_LSR_BI) {
275 status &= ~(UART_LSR_FE | UART_LSR_PE);
277 } else if (status & UART_LSR_PE)
279 else if (status & UART_LSR_FE)
281 if (status & UART_LSR_OE)
285 * Now check to see if character should be
286 * ignored, and mask off conditions which
289 if (status & info->ignore_status_mask)
292 status &= info->read_status_mask;
294 if (status & (UART_LSR_BI)) {
295 #ifdef SERIAL_DEBUG_INTR
296 printk("handling break....");
299 if (info->flags & ASYNC_SAK)
301 } else if (status & UART_LSR_PE)
303 else if (status & UART_LSR_FE)
305 if (status & UART_LSR_OE) {
307 * Overrun is special, since it's
308 * reported immediately, and doesn't
309 * affect the current character
314 tty_insert_flip_char(tty, ch, flag);
316 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
317 tty_flip_buffer_push(tty);
322 static void transmit_chars(struct serial_state *info)
324 custom.intreq = IF_TBE;
327 custom.serdat = info->x_char | 0x100;
333 if (info->xmit.head == info->xmit.tail
334 || info->tport.tty->stopped
335 || info->tport.tty->hw_stopped) {
336 info->IER &= ~UART_IER_THRI;
337 custom.intena = IF_TBE;
342 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
344 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
347 if (CIRC_CNT(info->xmit.head,
349 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
350 tty_wakeup(info->tport.tty);
352 #ifdef SERIAL_DEBUG_INTR
355 if (info->xmit.head == info->xmit.tail) {
356 custom.intena = IF_TBE;
358 info->IER &= ~UART_IER_THRI;
362 static void check_modem_status(struct serial_state *info)
364 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
365 unsigned char dstatus;
366 struct async_icount *icount;
368 /* Determine bits that have changed */
369 dstatus = status ^ current_ctl_bits;
370 current_ctl_bits = status;
373 icount = &info->icount;
374 /* update input line counters */
375 if (dstatus & SER_DSR)
377 if (dstatus & SER_DCD) {
379 #ifdef CONFIG_HARD_PPS
380 if ((info->flags & ASYNC_HARDPPS_CD) &&
385 if (dstatus & SER_CTS)
387 wake_up_interruptible(&info->tport.delta_msr_wait);
390 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
391 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
392 printk("ttyS%d CD now %s...", info->line,
393 (!(status & SER_DCD)) ? "on" : "off");
395 if (!(status & SER_DCD))
396 wake_up_interruptible(&info->tport.open_wait);
398 #ifdef SERIAL_DEBUG_OPEN
399 printk("doing serial hangup...");
402 tty_hangup(info->tport.tty);
405 if (info->flags & ASYNC_CTS_FLOW) {
406 if (info->tport.tty->hw_stopped) {
407 if (!(status & SER_CTS)) {
408 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
409 printk("CTS tx start...");
411 info->tport.tty->hw_stopped = 0;
412 info->IER |= UART_IER_THRI;
413 custom.intena = IF_SETCLR | IF_TBE;
415 /* set a pending Tx Interrupt, transmitter should restart now */
416 custom.intreq = IF_SETCLR | IF_TBE;
418 tty_wakeup(info->tport.tty);
422 if ((status & SER_CTS)) {
423 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
424 printk("CTS tx stop...");
426 info->tport.tty->hw_stopped = 1;
427 info->IER &= ~UART_IER_THRI;
428 /* disable Tx interrupt and remove any pending interrupts */
429 custom.intena = IF_TBE;
431 custom.intreq = IF_TBE;
438 static irqreturn_t ser_vbl_int( int irq, void *data)
440 /* vbl is just a periodic interrupt we tie into to update modem status */
441 struct serial_state *info = data;
443 * TBD - is it better to unregister from this interrupt or to
444 * ignore it if MSI is clear ?
446 if(info->IER & UART_IER_MSI)
447 check_modem_status(info);
451 static irqreturn_t ser_rx_int(int irq, void *dev_id)
453 struct serial_state *info = dev_id;
455 #ifdef SERIAL_DEBUG_INTR
456 printk("ser_rx_int...");
459 if (!info->tport.tty)
463 #ifdef SERIAL_DEBUG_INTR
469 static irqreturn_t ser_tx_int(int irq, void *dev_id)
471 struct serial_state *info = dev_id;
473 if (custom.serdatr & SDR_TBE) {
474 #ifdef SERIAL_DEBUG_INTR
475 printk("ser_tx_int...");
478 if (!info->tport.tty)
481 transmit_chars(info);
482 #ifdef SERIAL_DEBUG_INTR
490 * -------------------------------------------------------------------
491 * Here ends the serial interrupt routines.
492 * -------------------------------------------------------------------
496 * ---------------------------------------------------------------
497 * Low level utility subroutines for the serial driver: routines to
498 * figure out the appropriate timeout for an interrupt chain, routines
499 * to initialize and startup a serial port, and routines to shutdown a
500 * serial port. Useful stuff like that.
501 * ---------------------------------------------------------------
504 static int startup(struct tty_struct *tty, struct serial_state *info)
510 page = get_zeroed_page(GFP_KERNEL);
514 local_irq_save(flags);
516 if (info->flags & ASYNC_INITIALIZED) {
524 info->xmit.buf = (unsigned char *) page;
526 #ifdef SERIAL_DEBUG_OPEN
527 printk("starting up ttys%d ...", info->line);
530 /* Clear anything in the input buffer */
532 custom.intreq = IF_RBF;
535 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
537 if (serial_isroot()) {
538 set_bit(TTY_IO_ERROR, &tty->flags);
544 /* enable both Rx and Tx interrupts */
545 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
547 info->IER = UART_IER_MSI;
549 /* remember current state of the DCD and CTS bits */
550 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
554 info->MCR = SER_DTR | SER_RTS;
555 rtsdtr_ctrl(info->MCR);
557 clear_bit(TTY_IO_ERROR, &tty->flags);
558 info->xmit.head = info->xmit.tail = 0;
561 * Set up the tty->alt_speed kludge
563 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
564 tty->alt_speed = 57600;
565 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
566 tty->alt_speed = 115200;
567 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
568 tty->alt_speed = 230400;
569 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
570 tty->alt_speed = 460800;
573 * and set the speed of the serial port
575 change_speed(tty, info, NULL);
577 info->flags |= ASYNC_INITIALIZED;
578 local_irq_restore(flags);
582 local_irq_restore(flags);
587 * This routine will shutdown a serial port; interrupts are disabled, and
588 * DTR is dropped if the hangup on close termio flag is on.
590 static void shutdown(struct tty_struct *tty, struct serial_state *info)
593 struct serial_state *state;
595 if (!(info->flags & ASYNC_INITIALIZED))
600 #ifdef SERIAL_DEBUG_OPEN
601 printk("Shutting down serial port %d ....\n", info->line);
604 local_irq_save(flags); /* Disable interrupts */
607 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
608 * here so the queue might never be waken up
610 wake_up_interruptible(&info->tport.delta_msr_wait);
613 * Free the IRQ, if necessary
615 free_irq(IRQ_AMIGA_VERTB, info);
617 if (info->xmit.buf) {
618 free_page((unsigned long) info->xmit.buf);
619 info->xmit.buf = NULL;
623 custom.intena = IF_RBF | IF_TBE;
626 /* disable break condition */
627 custom.adkcon = AC_UARTBRK;
630 if (tty->termios->c_cflag & HUPCL)
631 info->MCR &= ~(SER_DTR|SER_RTS);
632 rtsdtr_ctrl(info->MCR);
634 set_bit(TTY_IO_ERROR, &tty->flags);
636 info->flags &= ~ASYNC_INITIALIZED;
637 local_irq_restore(flags);
642 * This routine is called to set the UART divisor registers to match
643 * the specified baud rate for a serial port.
645 static void change_speed(struct tty_struct *tty, struct serial_state *info,
646 struct ktermios *old_termios)
648 int quot = 0, baud_base, baud;
649 unsigned cflag, cval = 0;
653 cflag = tty->termios->c_cflag;
655 /* Byte size is always 8 bits plus parity bit if requested */
658 if (cflag & CSTOPB) {
662 if (cflag & PARENB) {
663 cval |= UART_LCR_PARITY;
666 if (!(cflag & PARODD))
667 cval |= UART_LCR_EPAR;
670 cval |= UART_LCR_SPAR;
673 /* Determine divisor based on baud rate */
674 baud = tty_get_baud_rate(tty);
676 baud = 9600; /* B0 transition handled in rs_set_termios */
677 baud_base = info->baud_base;
679 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
680 quot = info->custom_divisor;
683 /* Special case since 134 is really 134.5 */
684 quot = (2*baud_base / 269);
686 quot = baud_base / baud;
688 /* If the quotient is zero refuse the change */
689 if (!quot && old_termios) {
690 /* FIXME: Will need updating for new tty in the end */
691 tty->termios->c_cflag &= ~CBAUD;
692 tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
693 baud = tty_get_baud_rate(tty);
697 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
698 quot = info->custom_divisor;
701 /* Special case since 134 is really 134.5 */
702 quot = (2*baud_base / 269);
704 quot = baud_base / baud;
707 /* As a last resort, if the quotient is zero, default to 9600 bps */
709 quot = baud_base / 9600;
711 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
712 info->timeout += HZ/50; /* Add .02 seconds of slop */
714 /* CTS flow control flag and modem status interrupts */
715 info->IER &= ~UART_IER_MSI;
716 if (info->flags & ASYNC_HARDPPS_CD)
717 info->IER |= UART_IER_MSI;
718 if (cflag & CRTSCTS) {
719 info->flags |= ASYNC_CTS_FLOW;
720 info->IER |= UART_IER_MSI;
722 info->flags &= ~ASYNC_CTS_FLOW;
724 info->flags &= ~ASYNC_CHECK_CD;
726 info->flags |= ASYNC_CHECK_CD;
727 info->IER |= UART_IER_MSI;
730 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
734 * Set up parity check flag
737 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
739 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
740 if (I_BRKINT(tty) || I_PARMRK(tty))
741 info->read_status_mask |= UART_LSR_BI;
744 * Characters to ignore
746 info->ignore_status_mask = 0;
748 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
750 info->ignore_status_mask |= UART_LSR_BI;
752 * If we're ignore parity and break indicators, ignore
753 * overruns too. (For real raw support).
756 info->ignore_status_mask |= UART_LSR_OE;
759 * !!! ignore all characters if CREAD is not set
761 if ((cflag & CREAD) == 0)
762 info->ignore_status_mask |= UART_LSR_DR;
763 local_irq_save(flags);
768 /* Set up the baud rate */
771 /* Enable or disable parity bit */
773 if(cval & UART_LCR_PARITY)
774 serper |= (SERPER_PARENB);
776 custom.serper = serper;
780 local_irq_restore(flags);
783 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
785 struct serial_state *info;
788 info = tty->driver_data;
790 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
796 local_irq_save(flags);
797 if (CIRC_SPACE(info->xmit.head,
799 SERIAL_XMIT_SIZE) == 0) {
800 local_irq_restore(flags);
804 info->xmit.buf[info->xmit.head++] = ch;
805 info->xmit.head &= SERIAL_XMIT_SIZE-1;
806 local_irq_restore(flags);
810 static void rs_flush_chars(struct tty_struct *tty)
812 struct serial_state *info = tty->driver_data;
815 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
818 if (info->xmit.head == info->xmit.tail
824 local_irq_save(flags);
825 info->IER |= UART_IER_THRI;
826 custom.intena = IF_SETCLR | IF_TBE;
828 /* set a pending Tx Interrupt, transmitter should restart now */
829 custom.intreq = IF_SETCLR | IF_TBE;
831 local_irq_restore(flags);
834 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
837 struct serial_state *info = tty->driver_data;
840 if (serial_paranoia_check(info, tty->name, "rs_write"))
846 local_irq_save(flags);
848 c = CIRC_SPACE_TO_END(info->xmit.head,
856 memcpy(info->xmit.buf + info->xmit.head, buf, c);
857 info->xmit.head = ((info->xmit.head + c) &
858 (SERIAL_XMIT_SIZE-1));
863 local_irq_restore(flags);
865 if (info->xmit.head != info->xmit.tail
868 && !(info->IER & UART_IER_THRI)) {
869 info->IER |= UART_IER_THRI;
871 custom.intena = IF_SETCLR | IF_TBE;
873 /* set a pending Tx Interrupt, transmitter should restart now */
874 custom.intreq = IF_SETCLR | IF_TBE;
876 local_irq_restore(flags);
881 static int rs_write_room(struct tty_struct *tty)
883 struct serial_state *info = tty->driver_data;
885 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
887 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
890 static int rs_chars_in_buffer(struct tty_struct *tty)
892 struct serial_state *info = tty->driver_data;
894 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
896 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
899 static void rs_flush_buffer(struct tty_struct *tty)
901 struct serial_state *info = tty->driver_data;
904 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
906 local_irq_save(flags);
907 info->xmit.head = info->xmit.tail = 0;
908 local_irq_restore(flags);
913 * This function is used to send a high-priority XON/XOFF character to
916 static void rs_send_xchar(struct tty_struct *tty, char ch)
918 struct serial_state *info = tty->driver_data;
921 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
926 /* Make sure transmit interrupts are on */
929 local_irq_save(flags);
930 if(!(custom.intenar & IF_TBE)) {
931 custom.intena = IF_SETCLR | IF_TBE;
933 /* set a pending Tx Interrupt, transmitter should restart now */
934 custom.intreq = IF_SETCLR | IF_TBE;
937 local_irq_restore(flags);
939 info->IER |= UART_IER_THRI;
944 * ------------------------------------------------------------
947 * This routine is called by the upper-layer tty layer to signal that
948 * incoming characters should be throttled.
949 * ------------------------------------------------------------
951 static void rs_throttle(struct tty_struct * tty)
953 struct serial_state *info = tty->driver_data;
955 #ifdef SERIAL_DEBUG_THROTTLE
958 printk("throttle %s: %d....\n", tty_name(tty, buf),
959 tty->ldisc.chars_in_buffer(tty));
962 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
966 rs_send_xchar(tty, STOP_CHAR(tty));
968 if (tty->termios->c_cflag & CRTSCTS)
969 info->MCR &= ~SER_RTS;
971 local_irq_save(flags);
972 rtsdtr_ctrl(info->MCR);
973 local_irq_restore(flags);
976 static void rs_unthrottle(struct tty_struct * tty)
978 struct serial_state *info = tty->driver_data;
980 #ifdef SERIAL_DEBUG_THROTTLE
983 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
984 tty->ldisc.chars_in_buffer(tty));
987 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
994 rs_send_xchar(tty, START_CHAR(tty));
996 if (tty->termios->c_cflag & CRTSCTS)
997 info->MCR |= SER_RTS;
998 local_irq_save(flags);
999 rtsdtr_ctrl(info->MCR);
1000 local_irq_restore(flags);
1004 * ------------------------------------------------------------
1005 * rs_ioctl() and friends
1006 * ------------------------------------------------------------
1009 static int get_serial_info(struct serial_state *state,
1010 struct serial_struct __user * retinfo)
1012 struct serial_struct tmp;
1016 memset(&tmp, 0, sizeof(tmp));
1018 tmp.type = state->type;
1019 tmp.line = state->line;
1020 tmp.port = state->port;
1021 tmp.irq = state->irq;
1022 tmp.flags = state->flags;
1023 tmp.xmit_fifo_size = state->xmit_fifo_size;
1024 tmp.baud_base = state->baud_base;
1025 tmp.close_delay = state->close_delay;
1026 tmp.closing_wait = state->closing_wait;
1027 tmp.custom_divisor = state->custom_divisor;
1029 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1034 static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
1035 struct serial_struct __user * new_info)
1037 struct serial_struct new_serial;
1041 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1045 change_spd = ((new_serial.flags ^ state->flags) & ASYNC_SPD_MASK) ||
1046 new_serial.custom_divisor != state->custom_divisor;
1047 if (new_serial.irq != state->irq || new_serial.port != state->port ||
1048 new_serial.xmit_fifo_size != state->xmit_fifo_size) {
1053 if (!serial_isroot()) {
1054 if ((new_serial.baud_base != state->baud_base) ||
1055 (new_serial.close_delay != state->close_delay) ||
1056 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1057 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1058 (state->flags & ~ASYNC_USR_MASK)))
1060 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1061 (new_serial.flags & ASYNC_USR_MASK));
1062 state->custom_divisor = new_serial.custom_divisor;
1063 goto check_and_exit;
1066 if (new_serial.baud_base < 9600) {
1072 * OK, past this point, all the error checking has been done.
1073 * At this point, we start making changes.....
1076 state->baud_base = new_serial.baud_base;
1077 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1078 (new_serial.flags & ASYNC_FLAGS));
1079 state->custom_divisor = new_serial.custom_divisor;
1080 state->close_delay = new_serial.close_delay * HZ/100;
1081 state->closing_wait = new_serial.closing_wait * HZ/100;
1082 tty->low_latency = (state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1085 if (state->flags & ASYNC_INITIALIZED) {
1087 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1088 tty->alt_speed = 57600;
1089 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1090 tty->alt_speed = 115200;
1091 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1092 tty->alt_speed = 230400;
1093 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1094 tty->alt_speed = 460800;
1095 change_speed(tty, state, NULL);
1098 retval = startup(tty, state);
1105 * get_lsr_info - get line status register info
1107 * Purpose: Let user call ioctl() to get info when the UART physically
1108 * is emptied. On bus types like RS485, the transmitter must
1109 * release the bus after transmitting. This must be done when
1110 * the transmit shift register is empty, not be done when the
1111 * transmit holding register is empty. This functionality
1112 * allows an RS485 driver to be written in user space.
1114 static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
1116 unsigned char status;
1117 unsigned int result;
1118 unsigned long flags;
1120 local_irq_save(flags);
1121 status = custom.serdatr;
1123 local_irq_restore(flags);
1124 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1125 if (copy_to_user(value, &result, sizeof(int)))
1131 static int rs_tiocmget(struct tty_struct *tty)
1133 struct serial_state *info = tty->driver_data;
1134 unsigned char control, status;
1135 unsigned long flags;
1137 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1139 if (tty->flags & (1 << TTY_IO_ERROR))
1142 control = info->MCR;
1143 local_irq_save(flags);
1145 local_irq_restore(flags);
1146 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1147 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1148 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1149 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1150 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1153 static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1156 struct serial_state *info = tty->driver_data;
1157 unsigned long flags;
1159 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1161 if (tty->flags & (1 << TTY_IO_ERROR))
1164 local_irq_save(flags);
1165 if (set & TIOCM_RTS)
1166 info->MCR |= SER_RTS;
1167 if (set & TIOCM_DTR)
1168 info->MCR |= SER_DTR;
1169 if (clear & TIOCM_RTS)
1170 info->MCR &= ~SER_RTS;
1171 if (clear & TIOCM_DTR)
1172 info->MCR &= ~SER_DTR;
1173 rtsdtr_ctrl(info->MCR);
1174 local_irq_restore(flags);
1179 * rs_break() --- routine which turns the break handling on or off
1181 static int rs_break(struct tty_struct *tty, int break_state)
1183 struct serial_state *info = tty->driver_data;
1184 unsigned long flags;
1186 if (serial_paranoia_check(info, tty->name, "rs_break"))
1189 local_irq_save(flags);
1190 if (break_state == -1)
1191 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1193 custom.adkcon = AC_UARTBRK;
1195 local_irq_restore(flags);
1200 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1201 * Return: write counters to the user passed counter struct
1202 * NB: both 1->0 and 0->1 transitions are counted except for
1203 * RI where only 0->1 is counted.
1205 static int rs_get_icount(struct tty_struct *tty,
1206 struct serial_icounter_struct *icount)
1208 struct serial_state *info = tty->driver_data;
1209 struct async_icount cnow;
1210 unsigned long flags;
1212 local_irq_save(flags);
1213 cnow = info->icount;
1214 local_irq_restore(flags);
1215 icount->cts = cnow.cts;
1216 icount->dsr = cnow.dsr;
1217 icount->rng = cnow.rng;
1218 icount->dcd = cnow.dcd;
1219 icount->rx = cnow.rx;
1220 icount->tx = cnow.tx;
1221 icount->frame = cnow.frame;
1222 icount->overrun = cnow.overrun;
1223 icount->parity = cnow.parity;
1224 icount->brk = cnow.brk;
1225 icount->buf_overrun = cnow.buf_overrun;
1230 static int rs_ioctl(struct tty_struct *tty,
1231 unsigned int cmd, unsigned long arg)
1233 struct serial_state *info = tty->driver_data;
1234 struct async_icount cprev, cnow; /* kernel counter temps */
1235 void __user *argp = (void __user *)arg;
1236 unsigned long flags;
1238 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1241 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1242 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1243 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1244 if (tty->flags & (1 << TTY_IO_ERROR))
1250 return get_serial_info(info, argp);
1252 return set_serial_info(tty, info, argp);
1256 case TIOCSERGETLSR: /* Get line status register */
1257 return get_lsr_info(info, argp);
1259 case TIOCSERGSTRUCT:
1260 if (copy_to_user(argp,
1261 info, sizeof(struct serial_state)))
1266 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1267 * - mask passed in arg for lines of interest
1268 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1269 * Caller should use TIOCGICOUNT to see which one it was
1272 local_irq_save(flags);
1273 /* note the counters on entry */
1274 cprev = info->icount;
1275 local_irq_restore(flags);
1277 interruptible_sleep_on(&info->tport.delta_msr_wait);
1278 /* see if a signal did it */
1279 if (signal_pending(current))
1280 return -ERESTARTSYS;
1281 local_irq_save(flags);
1282 cnow = info->icount; /* atomic copy */
1283 local_irq_restore(flags);
1284 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1285 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1286 return -EIO; /* no change => error */
1287 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1288 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1289 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1290 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1299 /* "setserial -W" is called in Debian boot */
1300 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1304 return -ENOIOCTLCMD;
1309 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1311 struct serial_state *info = tty->driver_data;
1312 unsigned long flags;
1313 unsigned int cflag = tty->termios->c_cflag;
1315 change_speed(tty, info, old_termios);
1317 /* Handle transition to B0 status */
1318 if ((old_termios->c_cflag & CBAUD) &&
1320 info->MCR &= ~(SER_DTR|SER_RTS);
1321 local_irq_save(flags);
1322 rtsdtr_ctrl(info->MCR);
1323 local_irq_restore(flags);
1326 /* Handle transition away from B0 status */
1327 if (!(old_termios->c_cflag & CBAUD) &&
1329 info->MCR |= SER_DTR;
1330 if (!(tty->termios->c_cflag & CRTSCTS) ||
1331 !test_bit(TTY_THROTTLED, &tty->flags)) {
1332 info->MCR |= SER_RTS;
1334 local_irq_save(flags);
1335 rtsdtr_ctrl(info->MCR);
1336 local_irq_restore(flags);
1339 /* Handle turning off CRTSCTS */
1340 if ((old_termios->c_cflag & CRTSCTS) &&
1341 !(tty->termios->c_cflag & CRTSCTS)) {
1342 tty->hw_stopped = 0;
1348 * No need to wake up processes in open wait, since they
1349 * sample the CLOCAL flag once, and don't recheck it.
1350 * XXX It's not clear whether the current behavior is correct
1351 * or not. Hence, this may change.....
1353 if (!(old_termios->c_cflag & CLOCAL) &&
1354 (tty->termios->c_cflag & CLOCAL))
1355 wake_up_interruptible(&info->open_wait);
1360 * ------------------------------------------------------------
1363 * This routine is called when the serial port gets closed. First, we
1364 * wait for the last remaining data to be sent. Then, we unlink its
1365 * async structure from the interrupt chain if necessary, and we free
1366 * that IRQ if nothing is left in the chain.
1367 * ------------------------------------------------------------
1369 static void rs_close(struct tty_struct *tty, struct file * filp)
1371 struct serial_state *state = tty->driver_data;
1372 unsigned long flags;
1374 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
1377 local_irq_save(flags);
1379 if (tty_hung_up_p(filp)) {
1380 DBG_CNT("before DEC-hung");
1381 local_irq_restore(flags);
1385 #ifdef SERIAL_DEBUG_OPEN
1386 printk("rs_close ttys%d, count = %d\n", state->line, state->count);
1388 if ((tty->count == 1) && (state->count != 1)) {
1390 * Uh, oh. tty->count is 1, which means that the tty
1391 * structure will be freed. state->count should always
1392 * be one in these conditions. If it's greater than
1393 * one, we've got real problems, since it means the
1394 * serial port won't be shutdown.
1396 printk("rs_close: bad serial port count; tty->count is 1, "
1397 "state->count is %d\n", state->count);
1400 if (--state->count < 0) {
1401 printk("rs_close: bad serial port count for ttys%d: %d\n",
1402 state->line, state->count);
1406 DBG_CNT("before DEC-2");
1407 local_irq_restore(flags);
1410 state->flags |= ASYNC_CLOSING;
1412 * Now we wait for the transmit buffer to clear; and we notify
1413 * the line discipline to only process XON/XOFF characters.
1416 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1417 tty_wait_until_sent(tty, state->closing_wait);
1419 * At this point we stop accepting input. To do this, we
1420 * disable the receive line status interrupts, and tell the
1421 * interrupt driver to stop checking the data ready bit in the
1422 * line status register.
1424 state->read_status_mask &= ~UART_LSR_DR;
1425 if (state->flags & ASYNC_INITIALIZED) {
1426 /* disable receive interrupts */
1427 custom.intena = IF_RBF;
1429 /* clear any pending receive interrupt */
1430 custom.intreq = IF_RBF;
1434 * Before we drop DTR, make sure the UART transmitter
1435 * has completely drained; this is especially
1436 * important if there is a transmit FIFO!
1438 rs_wait_until_sent(tty, state->timeout);
1440 shutdown(tty, state);
1441 rs_flush_buffer(tty);
1443 tty_ldisc_flush(tty);
1445 state->tport.tty = NULL;
1446 if (state->tport.blocked_open) {
1447 if (state->close_delay) {
1448 msleep_interruptible(jiffies_to_msecs(state->close_delay));
1450 wake_up_interruptible(&state->tport.open_wait);
1452 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1453 wake_up_interruptible(&state->tport.close_wait);
1454 local_irq_restore(flags);
1458 * rs_wait_until_sent() --- wait until the transmitter is empty
1460 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1462 struct serial_state *info = tty->driver_data;
1463 unsigned long orig_jiffies, char_time;
1466 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1469 if (info->xmit_fifo_size == 0)
1470 return; /* Just in case.... */
1472 orig_jiffies = jiffies;
1475 * Set the check interval to be 1/5 of the estimated time to
1476 * send a single character, and make it at least 1. The check
1477 * interval should also be less than the timeout.
1479 * Note: we have to use pretty tight timings here to satisfy
1482 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1483 char_time = char_time / 5;
1487 char_time = min_t(unsigned long, char_time, timeout);
1489 * If the transmitter hasn't cleared in twice the approximate
1490 * amount of time to send the entire FIFO, it probably won't
1491 * ever clear. This assumes the UART isn't doing flow
1492 * control, which is currently the case. Hence, if it ever
1493 * takes longer than info->timeout, this is probably due to a
1494 * UART bug of some kind. So, we clamp the timeout parameter at
1497 if (!timeout || timeout > 2*info->timeout)
1498 timeout = 2*info->timeout;
1499 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1500 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1501 printk("jiff=%lu...", jiffies);
1503 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1504 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1505 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1507 msleep_interruptible(jiffies_to_msecs(char_time));
1508 if (signal_pending(current))
1510 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1513 __set_current_state(TASK_RUNNING);
1515 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1516 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1521 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1523 static void rs_hangup(struct tty_struct *tty)
1525 struct serial_state *info = tty->driver_data;
1527 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1530 rs_flush_buffer(tty);
1531 shutdown(tty, info);
1533 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1534 info->tport.tty = NULL;
1535 wake_up_interruptible(&info->tport.open_wait);
1539 * ------------------------------------------------------------
1540 * rs_open() and friends
1541 * ------------------------------------------------------------
1543 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1544 struct serial_state *info)
1546 #ifdef DECLARE_WAITQUEUE
1547 DECLARE_WAITQUEUE(wait, current);
1549 struct wait_queue wait = { current, NULL };
1552 int do_clocal = 0, extra_count = 0;
1553 unsigned long flags;
1556 * If the device is in the middle of being closed, then block
1557 * until it's done, and then try again.
1559 if (tty_hung_up_p(filp) ||
1560 (info->flags & ASYNC_CLOSING)) {
1561 if (info->flags & ASYNC_CLOSING)
1562 interruptible_sleep_on(&info->tport.close_wait);
1563 #ifdef SERIAL_DO_RESTART
1564 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1565 -EAGAIN : -ERESTARTSYS);
1572 * If non-blocking mode is set, or the port is not enabled,
1573 * then make the check up front and then exit.
1575 if ((filp->f_flags & O_NONBLOCK) ||
1576 (tty->flags & (1 << TTY_IO_ERROR))) {
1577 info->flags |= ASYNC_NORMAL_ACTIVE;
1581 if (tty->termios->c_cflag & CLOCAL)
1585 * Block waiting for the carrier detect and the line to become
1586 * free (i.e., not in use by the callout). While we are in
1587 * this loop, info->count is dropped by one, so that
1588 * rs_close() knows when to free things. We restore it upon
1589 * exit, either normal or abnormal.
1592 add_wait_queue(&info->tport.open_wait, &wait);
1593 #ifdef SERIAL_DEBUG_OPEN
1594 printk("block_til_ready before block: ttys%d, count = %d\n",
1595 info->line, info->count);
1597 local_irq_save(flags);
1598 if (!tty_hung_up_p(filp)) {
1602 local_irq_restore(flags);
1603 info->tport.blocked_open++;
1605 local_irq_save(flags);
1606 if (tty->termios->c_cflag & CBAUD)
1607 rtsdtr_ctrl(SER_DTR|SER_RTS);
1608 local_irq_restore(flags);
1609 set_current_state(TASK_INTERRUPTIBLE);
1610 if (tty_hung_up_p(filp) ||
1611 !(info->flags & ASYNC_INITIALIZED)) {
1612 #ifdef SERIAL_DO_RESTART
1613 if (info->flags & ASYNC_HUP_NOTIFY)
1616 retval = -ERESTARTSYS;
1622 if (!(info->flags & ASYNC_CLOSING) &&
1623 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1625 if (signal_pending(current)) {
1626 retval = -ERESTARTSYS;
1629 #ifdef SERIAL_DEBUG_OPEN
1630 printk("block_til_ready blocking: ttys%d, count = %d\n",
1631 info->line, info->count);
1637 __set_current_state(TASK_RUNNING);
1638 remove_wait_queue(&info->tport.open_wait, &wait);
1641 info->tport.blocked_open--;
1642 #ifdef SERIAL_DEBUG_OPEN
1643 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1644 info->line, info->count);
1648 info->flags |= ASYNC_NORMAL_ACTIVE;
1653 * This routine is called whenever a serial port is opened. It
1654 * enables interrupts for a serial port, linking in its async structure into
1655 * the IRQ chain. It also performs the serial-specific
1656 * initialization for the tty structure.
1658 static int rs_open(struct tty_struct *tty, struct file * filp)
1660 struct serial_state *info = rs_table + tty->index;
1664 info->tport.tty = tty;
1665 tty->driver_data = info;
1666 tty->port = &info->tport;
1667 if (serial_paranoia_check(info, tty->name, "rs_open"))
1670 #ifdef SERIAL_DEBUG_OPEN
1671 printk("rs_open %s, count = %d\n", tty->name, info->count);
1673 tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1676 * If the port is the middle of closing, bail out now
1678 if (tty_hung_up_p(filp) ||
1679 (info->flags & ASYNC_CLOSING)) {
1680 if (info->flags & ASYNC_CLOSING)
1681 interruptible_sleep_on(&info->tport.close_wait);
1682 #ifdef SERIAL_DO_RESTART
1683 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1684 -EAGAIN : -ERESTARTSYS);
1691 * Start up serial port
1693 retval = startup(tty, info);
1698 retval = block_til_ready(tty, filp, info);
1700 #ifdef SERIAL_DEBUG_OPEN
1701 printk("rs_open returning after block_til_ready with %d\n",
1707 #ifdef SERIAL_DEBUG_OPEN
1708 printk("rs_open %s successful...", tty->name);
1714 * /proc fs routines....
1717 static inline void line_info(struct seq_file *m, struct serial_state *state)
1719 char stat_buf[30], control, status;
1720 unsigned long flags;
1722 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1724 local_irq_save(flags);
1726 control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status;
1727 local_irq_restore(flags);
1731 if(!(control & SER_RTS))
1732 strcat(stat_buf, "|RTS");
1733 if(!(status & SER_CTS))
1734 strcat(stat_buf, "|CTS");
1735 if(!(control & SER_DTR))
1736 strcat(stat_buf, "|DTR");
1737 if(!(status & SER_DSR))
1738 strcat(stat_buf, "|DSR");
1739 if(!(status & SER_DCD))
1740 strcat(stat_buf, "|CD");
1743 seq_printf(m, " baud:%d", state->baud_base / state->quot);
1745 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1747 if (state->icount.frame)
1748 seq_printf(m, " fe:%d", state->icount.frame);
1750 if (state->icount.parity)
1751 seq_printf(m, " pe:%d", state->icount.parity);
1753 if (state->icount.brk)
1754 seq_printf(m, " brk:%d", state->icount.brk);
1756 if (state->icount.overrun)
1757 seq_printf(m, " oe:%d", state->icount.overrun);
1760 * Last thing is the RS-232 status lines
1762 seq_printf(m, " %s\n", stat_buf+1);
1765 static int rs_proc_show(struct seq_file *m, void *v)
1767 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1768 line_info(m, &rs_table[0]);
1772 static int rs_proc_open(struct inode *inode, struct file *file)
1774 return single_open(file, rs_proc_show, NULL);
1777 static const struct file_operations rs_proc_fops = {
1778 .owner = THIS_MODULE,
1779 .open = rs_proc_open,
1781 .llseek = seq_lseek,
1782 .release = single_release,
1786 * ---------------------------------------------------------------------
1787 * rs_init() and friends
1789 * rs_init() is called at boot-time to initialize the serial driver.
1790 * ---------------------------------------------------------------------
1794 * This routine prints out the appropriate serial driver version
1795 * number, and identifies which options were configured into this
1798 static void show_serial_version(void)
1800 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1804 static const struct tty_operations serial_ops = {
1808 .put_char = rs_put_char,
1809 .flush_chars = rs_flush_chars,
1810 .write_room = rs_write_room,
1811 .chars_in_buffer = rs_chars_in_buffer,
1812 .flush_buffer = rs_flush_buffer,
1814 .throttle = rs_throttle,
1815 .unthrottle = rs_unthrottle,
1816 .set_termios = rs_set_termios,
1819 .hangup = rs_hangup,
1820 .break_ctl = rs_break,
1821 .send_xchar = rs_send_xchar,
1822 .wait_until_sent = rs_wait_until_sent,
1823 .tiocmget = rs_tiocmget,
1824 .tiocmset = rs_tiocmset,
1825 .get_icount = rs_get_icount,
1826 .proc_fops = &rs_proc_fops,
1830 * The serial driver boot-time initialization code!
1832 static int __init amiga_serial_probe(struct platform_device *pdev)
1834 unsigned long flags;
1835 struct serial_state * state;
1838 serial_driver = alloc_tty_driver(NR_PORTS);
1842 show_serial_version();
1844 /* Initialize the tty_driver structure */
1846 serial_driver->driver_name = "amiserial";
1847 serial_driver->name = "ttyS";
1848 serial_driver->major = TTY_MAJOR;
1849 serial_driver->minor_start = 64;
1850 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1851 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1852 serial_driver->init_termios = tty_std_termios;
1853 serial_driver->init_termios.c_cflag =
1854 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1855 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1856 tty_set_operations(serial_driver, &serial_ops);
1858 error = tty_register_driver(serial_driver);
1860 goto fail_put_tty_driver;
1863 state->port = (int)&custom.serdatr; /* Just to give it a value */
1865 state->custom_divisor = 0;
1866 state->close_delay = 5*HZ/10;
1867 state->closing_wait = 30*HZ;
1868 state->icount.cts = state->icount.dsr =
1869 state->icount.rng = state->icount.dcd = 0;
1870 state->icount.rx = state->icount.tx = 0;
1871 state->icount.frame = state->icount.parity = 0;
1872 state->icount.overrun = state->icount.brk = 0;
1873 tty_port_init(&state->tport);
1875 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1878 /* Hardware set up */
1880 state->baud_base = amiga_colorclock;
1881 state->xmit_fifo_size = 1;
1883 /* set ISRs, and then disable the rx interrupts */
1884 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1886 goto fail_unregister;
1888 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
1889 "serial RX", state);
1893 local_irq_save(flags);
1895 /* turn off Rx and Tx interrupts */
1896 custom.intena = IF_RBF | IF_TBE;
1899 /* clear any pending interrupt */
1900 custom.intreq = IF_RBF | IF_TBE;
1903 local_irq_restore(flags);
1906 * set the appropriate directions for the modem control flags,
1907 * and clear RTS and DTR
1909 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1910 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1912 platform_set_drvdata(pdev, state);
1917 free_irq(IRQ_AMIGA_TBE, state);
1919 tty_unregister_driver(serial_driver);
1920 fail_put_tty_driver:
1921 put_tty_driver(serial_driver);
1925 static int __exit amiga_serial_remove(struct platform_device *pdev)
1928 struct serial_state *state = platform_get_drvdata(pdev);
1930 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
1931 if ((error = tty_unregister_driver(serial_driver)))
1932 printk("SERIAL: failed to unregister serial driver (%d)\n",
1934 put_tty_driver(serial_driver);
1936 free_irq(IRQ_AMIGA_TBE, state);
1937 free_irq(IRQ_AMIGA_RBF, state);
1939 platform_set_drvdata(pdev, NULL);
1944 static struct platform_driver amiga_serial_driver = {
1945 .remove = __exit_p(amiga_serial_remove),
1947 .name = "amiga-serial",
1948 .owner = THIS_MODULE,
1952 static int __init amiga_serial_init(void)
1954 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1957 module_init(amiga_serial_init);
1959 static void __exit amiga_serial_exit(void)
1961 platform_driver_unregister(&amiga_serial_driver);
1964 module_exit(amiga_serial_exit);
1967 #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1970 * ------------------------------------------------------------
1971 * Serial console driver
1972 * ------------------------------------------------------------
1975 static void amiga_serial_putc(char c)
1977 custom.serdat = (unsigned char)c | 0x100;
1978 while (!(custom.serdatr & 0x2000))
1983 * Print a string to the serial port trying not to disturb
1984 * any possible real use of the port...
1986 * The console must be locked when we get here.
1988 static void serial_console_write(struct console *co, const char *s,
1991 unsigned short intena = custom.intenar;
1993 custom.intena = IF_TBE;
1997 amiga_serial_putc('\r');
1998 amiga_serial_putc(*s++);
2001 custom.intena = IF_SETCLR | (intena & IF_TBE);
2004 static struct tty_driver *serial_console_device(struct console *c, int *index)
2007 return serial_driver;
2010 static struct console sercons = {
2012 .write = serial_console_write,
2013 .device = serial_console_device,
2014 .flags = CON_PRINTBUFFER,
2021 static int __init amiserial_console_init(void)
2023 register_console(&sercons);
2026 console_initcall(amiserial_console_init);
2028 #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2030 MODULE_LICENSE("GPL");
2031 MODULE_ALIAS("platform:amiga-serial");