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->tport.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->tport.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 struct tty_port *port = &info->tport;
365 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
366 unsigned char dstatus;
367 struct async_icount *icount;
369 /* Determine bits that have changed */
370 dstatus = status ^ current_ctl_bits;
371 current_ctl_bits = status;
374 icount = &info->icount;
375 /* update input line counters */
376 if (dstatus & SER_DSR)
378 if (dstatus & SER_DCD) {
380 #ifdef CONFIG_HARD_PPS
381 if ((port->flags & ASYNC_HARDPPS_CD) &&
386 if (dstatus & SER_CTS)
388 wake_up_interruptible(&port->delta_msr_wait);
391 if ((port->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
392 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
393 printk("ttyS%d CD now %s...", info->line,
394 (!(status & SER_DCD)) ? "on" : "off");
396 if (!(status & SER_DCD))
397 wake_up_interruptible(&port->open_wait);
399 #ifdef SERIAL_DEBUG_OPEN
400 printk("doing serial hangup...");
403 tty_hangup(port->tty);
406 if (port->flags & ASYNC_CTS_FLOW) {
407 if (port->tty->hw_stopped) {
408 if (!(status & SER_CTS)) {
409 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
410 printk("CTS tx start...");
412 port->tty->hw_stopped = 0;
413 info->IER |= UART_IER_THRI;
414 custom.intena = IF_SETCLR | IF_TBE;
416 /* set a pending Tx Interrupt, transmitter should restart now */
417 custom.intreq = IF_SETCLR | IF_TBE;
419 tty_wakeup(port->tty);
423 if ((status & SER_CTS)) {
424 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
425 printk("CTS tx stop...");
427 port->tty->hw_stopped = 1;
428 info->IER &= ~UART_IER_THRI;
429 /* disable Tx interrupt and remove any pending interrupts */
430 custom.intena = IF_TBE;
432 custom.intreq = IF_TBE;
439 static irqreturn_t ser_vbl_int( int irq, void *data)
441 /* vbl is just a periodic interrupt we tie into to update modem status */
442 struct serial_state *info = data;
444 * TBD - is it better to unregister from this interrupt or to
445 * ignore it if MSI is clear ?
447 if(info->IER & UART_IER_MSI)
448 check_modem_status(info);
452 static irqreturn_t ser_rx_int(int irq, void *dev_id)
454 struct serial_state *info = dev_id;
456 #ifdef SERIAL_DEBUG_INTR
457 printk("ser_rx_int...");
460 if (!info->tport.tty)
464 #ifdef SERIAL_DEBUG_INTR
470 static irqreturn_t ser_tx_int(int irq, void *dev_id)
472 struct serial_state *info = dev_id;
474 if (custom.serdatr & SDR_TBE) {
475 #ifdef SERIAL_DEBUG_INTR
476 printk("ser_tx_int...");
479 if (!info->tport.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 tty_struct *tty, struct serial_state *info)
507 struct tty_port *port = &info->tport;
512 page = get_zeroed_page(GFP_KERNEL);
516 local_irq_save(flags);
518 if (port->flags & ASYNC_INITIALIZED) {
526 info->xmit.buf = (unsigned char *) page;
528 #ifdef SERIAL_DEBUG_OPEN
529 printk("starting up ttys%d ...", info->line);
532 /* Clear anything in the input buffer */
534 custom.intreq = IF_RBF;
537 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
539 if (serial_isroot()) {
540 set_bit(TTY_IO_ERROR, &tty->flags);
546 /* enable both Rx and Tx interrupts */
547 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
549 info->IER = UART_IER_MSI;
551 /* remember current state of the DCD and CTS bits */
552 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
556 info->MCR = SER_DTR | SER_RTS;
557 rtsdtr_ctrl(info->MCR);
559 clear_bit(TTY_IO_ERROR, &tty->flags);
560 info->xmit.head = info->xmit.tail = 0;
563 * Set up the tty->alt_speed kludge
565 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
566 tty->alt_speed = 57600;
567 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
568 tty->alt_speed = 115200;
569 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
570 tty->alt_speed = 230400;
571 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
572 tty->alt_speed = 460800;
575 * and set the speed of the serial port
577 change_speed(tty, info, NULL);
579 port->flags |= ASYNC_INITIALIZED;
580 local_irq_restore(flags);
584 local_irq_restore(flags);
589 * This routine will shutdown a serial port; interrupts are disabled, and
590 * DTR is dropped if the hangup on close termio flag is on.
592 static void shutdown(struct tty_struct *tty, struct serial_state *info)
595 struct serial_state *state;
597 if (!(info->tport.flags & ASYNC_INITIALIZED))
602 #ifdef SERIAL_DEBUG_OPEN
603 printk("Shutting down serial port %d ....\n", info->line);
606 local_irq_save(flags); /* Disable interrupts */
609 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
610 * here so the queue might never be waken up
612 wake_up_interruptible(&info->tport.delta_msr_wait);
615 * Free the IRQ, if necessary
617 free_irq(IRQ_AMIGA_VERTB, info);
619 if (info->xmit.buf) {
620 free_page((unsigned long) info->xmit.buf);
621 info->xmit.buf = NULL;
625 custom.intena = IF_RBF | IF_TBE;
628 /* disable break condition */
629 custom.adkcon = AC_UARTBRK;
632 if (tty->termios->c_cflag & HUPCL)
633 info->MCR &= ~(SER_DTR|SER_RTS);
634 rtsdtr_ctrl(info->MCR);
636 set_bit(TTY_IO_ERROR, &tty->flags);
638 info->tport.flags &= ~ASYNC_INITIALIZED;
639 local_irq_restore(flags);
644 * This routine is called to set the UART divisor registers to match
645 * the specified baud rate for a serial port.
647 static void change_speed(struct tty_struct *tty, struct serial_state *info,
648 struct ktermios *old_termios)
650 struct tty_port *port = &info->tport;
651 int quot = 0, baud_base, baud;
652 unsigned cflag, cval = 0;
656 cflag = tty->termios->c_cflag;
658 /* Byte size is always 8 bits plus parity bit if requested */
661 if (cflag & CSTOPB) {
665 if (cflag & PARENB) {
666 cval |= UART_LCR_PARITY;
669 if (!(cflag & PARODD))
670 cval |= UART_LCR_EPAR;
673 cval |= UART_LCR_SPAR;
676 /* Determine divisor based on baud rate */
677 baud = tty_get_baud_rate(tty);
679 baud = 9600; /* B0 transition handled in rs_set_termios */
680 baud_base = info->baud_base;
681 if (baud == 38400 && (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
682 quot = info->custom_divisor;
685 /* Special case since 134 is really 134.5 */
686 quot = (2*baud_base / 269);
688 quot = baud_base / baud;
690 /* If the quotient is zero refuse the change */
691 if (!quot && old_termios) {
692 /* FIXME: Will need updating for new tty in the end */
693 tty->termios->c_cflag &= ~CBAUD;
694 tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
695 baud = tty_get_baud_rate(tty);
699 (port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
700 quot = info->custom_divisor;
703 /* Special case since 134 is really 134.5 */
704 quot = (2*baud_base / 269);
706 quot = baud_base / baud;
709 /* As a last resort, if the quotient is zero, default to 9600 bps */
711 quot = baud_base / 9600;
713 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
714 info->timeout += HZ/50; /* Add .02 seconds of slop */
716 /* CTS flow control flag and modem status interrupts */
717 info->IER &= ~UART_IER_MSI;
718 if (port->flags & ASYNC_HARDPPS_CD)
719 info->IER |= UART_IER_MSI;
720 if (cflag & CRTSCTS) {
721 port->flags |= ASYNC_CTS_FLOW;
722 info->IER |= UART_IER_MSI;
724 port->flags &= ~ASYNC_CTS_FLOW;
726 port->flags &= ~ASYNC_CHECK_CD;
728 port->flags |= ASYNC_CHECK_CD;
729 info->IER |= UART_IER_MSI;
732 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
736 * Set up parity check flag
739 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
741 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
742 if (I_BRKINT(tty) || I_PARMRK(tty))
743 info->read_status_mask |= UART_LSR_BI;
746 * Characters to ignore
748 info->ignore_status_mask = 0;
750 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
752 info->ignore_status_mask |= UART_LSR_BI;
754 * If we're ignore parity and break indicators, ignore
755 * overruns too. (For real raw support).
758 info->ignore_status_mask |= UART_LSR_OE;
761 * !!! ignore all characters if CREAD is not set
763 if ((cflag & CREAD) == 0)
764 info->ignore_status_mask |= UART_LSR_DR;
765 local_irq_save(flags);
770 /* Set up the baud rate */
773 /* Enable or disable parity bit */
775 if(cval & UART_LCR_PARITY)
776 serper |= (SERPER_PARENB);
778 custom.serper = serper;
782 local_irq_restore(flags);
785 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
787 struct serial_state *info;
790 info = tty->driver_data;
792 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
798 local_irq_save(flags);
799 if (CIRC_SPACE(info->xmit.head,
801 SERIAL_XMIT_SIZE) == 0) {
802 local_irq_restore(flags);
806 info->xmit.buf[info->xmit.head++] = ch;
807 info->xmit.head &= SERIAL_XMIT_SIZE-1;
808 local_irq_restore(flags);
812 static void rs_flush_chars(struct tty_struct *tty)
814 struct serial_state *info = tty->driver_data;
817 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
820 if (info->xmit.head == info->xmit.tail
826 local_irq_save(flags);
827 info->IER |= UART_IER_THRI;
828 custom.intena = IF_SETCLR | IF_TBE;
830 /* set a pending Tx Interrupt, transmitter should restart now */
831 custom.intreq = IF_SETCLR | IF_TBE;
833 local_irq_restore(flags);
836 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
839 struct serial_state *info = tty->driver_data;
842 if (serial_paranoia_check(info, tty->name, "rs_write"))
848 local_irq_save(flags);
850 c = CIRC_SPACE_TO_END(info->xmit.head,
858 memcpy(info->xmit.buf + info->xmit.head, buf, c);
859 info->xmit.head = ((info->xmit.head + c) &
860 (SERIAL_XMIT_SIZE-1));
865 local_irq_restore(flags);
867 if (info->xmit.head != info->xmit.tail
870 && !(info->IER & UART_IER_THRI)) {
871 info->IER |= UART_IER_THRI;
873 custom.intena = IF_SETCLR | IF_TBE;
875 /* set a pending Tx Interrupt, transmitter should restart now */
876 custom.intreq = IF_SETCLR | IF_TBE;
878 local_irq_restore(flags);
883 static int rs_write_room(struct tty_struct *tty)
885 struct serial_state *info = tty->driver_data;
887 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
889 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
892 static int rs_chars_in_buffer(struct tty_struct *tty)
894 struct serial_state *info = tty->driver_data;
896 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
898 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
901 static void rs_flush_buffer(struct tty_struct *tty)
903 struct serial_state *info = tty->driver_data;
906 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
908 local_irq_save(flags);
909 info->xmit.head = info->xmit.tail = 0;
910 local_irq_restore(flags);
915 * This function is used to send a high-priority XON/XOFF character to
918 static void rs_send_xchar(struct tty_struct *tty, char ch)
920 struct serial_state *info = tty->driver_data;
923 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
928 /* Make sure transmit interrupts are on */
931 local_irq_save(flags);
932 if(!(custom.intenar & IF_TBE)) {
933 custom.intena = IF_SETCLR | IF_TBE;
935 /* set a pending Tx Interrupt, transmitter should restart now */
936 custom.intreq = IF_SETCLR | IF_TBE;
939 local_irq_restore(flags);
941 info->IER |= UART_IER_THRI;
946 * ------------------------------------------------------------
949 * This routine is called by the upper-layer tty layer to signal that
950 * incoming characters should be throttled.
951 * ------------------------------------------------------------
953 static void rs_throttle(struct tty_struct * tty)
955 struct serial_state *info = tty->driver_data;
957 #ifdef SERIAL_DEBUG_THROTTLE
960 printk("throttle %s: %d....\n", tty_name(tty, buf),
961 tty->ldisc.chars_in_buffer(tty));
964 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
968 rs_send_xchar(tty, STOP_CHAR(tty));
970 if (tty->termios->c_cflag & CRTSCTS)
971 info->MCR &= ~SER_RTS;
973 local_irq_save(flags);
974 rtsdtr_ctrl(info->MCR);
975 local_irq_restore(flags);
978 static void rs_unthrottle(struct tty_struct * tty)
980 struct serial_state *info = tty->driver_data;
982 #ifdef SERIAL_DEBUG_THROTTLE
985 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
986 tty->ldisc.chars_in_buffer(tty));
989 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
996 rs_send_xchar(tty, START_CHAR(tty));
998 if (tty->termios->c_cflag & CRTSCTS)
999 info->MCR |= SER_RTS;
1000 local_irq_save(flags);
1001 rtsdtr_ctrl(info->MCR);
1002 local_irq_restore(flags);
1006 * ------------------------------------------------------------
1007 * rs_ioctl() and friends
1008 * ------------------------------------------------------------
1011 static int get_serial_info(struct serial_state *state,
1012 struct serial_struct __user * retinfo)
1014 struct serial_struct tmp;
1018 memset(&tmp, 0, sizeof(tmp));
1020 tmp.type = state->type;
1021 tmp.line = state->line;
1022 tmp.port = state->port;
1023 tmp.irq = state->irq;
1024 tmp.flags = state->tport.flags;
1025 tmp.xmit_fifo_size = state->xmit_fifo_size;
1026 tmp.baud_base = state->baud_base;
1027 tmp.close_delay = state->tport.close_delay;
1028 tmp.closing_wait = state->tport.closing_wait;
1029 tmp.custom_divisor = state->custom_divisor;
1031 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1036 static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
1037 struct serial_struct __user * new_info)
1039 struct tty_port *port = &state->tport;
1040 struct serial_struct new_serial;
1044 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1048 change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) ||
1049 new_serial.custom_divisor != state->custom_divisor;
1050 if (new_serial.irq != state->irq || new_serial.port != state->port ||
1051 new_serial.xmit_fifo_size != state->xmit_fifo_size) {
1056 if (!serial_isroot()) {
1057 if ((new_serial.baud_base != state->baud_base) ||
1058 (new_serial.close_delay != port->close_delay) ||
1059 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1060 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1061 (port->flags & ~ASYNC_USR_MASK)))
1063 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1064 (new_serial.flags & ASYNC_USR_MASK));
1065 state->custom_divisor = new_serial.custom_divisor;
1066 goto check_and_exit;
1069 if (new_serial.baud_base < 9600) {
1075 * OK, past this point, all the error checking has been done.
1076 * At this point, we start making changes.....
1079 state->baud_base = new_serial.baud_base;
1080 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1081 (new_serial.flags & ASYNC_FLAGS));
1082 state->custom_divisor = new_serial.custom_divisor;
1083 port->close_delay = new_serial.close_delay * HZ/100;
1084 port->closing_wait = new_serial.closing_wait * HZ/100;
1085 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1088 if (port->flags & ASYNC_INITIALIZED) {
1090 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1091 tty->alt_speed = 57600;
1092 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1093 tty->alt_speed = 115200;
1094 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1095 tty->alt_speed = 230400;
1096 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1097 tty->alt_speed = 460800;
1098 change_speed(tty, state, NULL);
1101 retval = startup(tty, state);
1108 * get_lsr_info - get line status register info
1110 * Purpose: Let user call ioctl() to get info when the UART physically
1111 * is emptied. On bus types like RS485, the transmitter must
1112 * release the bus after transmitting. This must be done when
1113 * the transmit shift register is empty, not be done when the
1114 * transmit holding register is empty. This functionality
1115 * allows an RS485 driver to be written in user space.
1117 static int get_lsr_info(struct serial_state *info, unsigned int __user *value)
1119 unsigned char status;
1120 unsigned int result;
1121 unsigned long flags;
1123 local_irq_save(flags);
1124 status = custom.serdatr;
1126 local_irq_restore(flags);
1127 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1128 if (copy_to_user(value, &result, sizeof(int)))
1134 static int rs_tiocmget(struct tty_struct *tty)
1136 struct serial_state *info = tty->driver_data;
1137 unsigned char control, status;
1138 unsigned long flags;
1140 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1142 if (tty->flags & (1 << TTY_IO_ERROR))
1145 control = info->MCR;
1146 local_irq_save(flags);
1148 local_irq_restore(flags);
1149 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1150 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1151 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1152 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1153 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1156 static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1159 struct serial_state *info = tty->driver_data;
1160 unsigned long flags;
1162 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1164 if (tty->flags & (1 << TTY_IO_ERROR))
1167 local_irq_save(flags);
1168 if (set & TIOCM_RTS)
1169 info->MCR |= SER_RTS;
1170 if (set & TIOCM_DTR)
1171 info->MCR |= SER_DTR;
1172 if (clear & TIOCM_RTS)
1173 info->MCR &= ~SER_RTS;
1174 if (clear & TIOCM_DTR)
1175 info->MCR &= ~SER_DTR;
1176 rtsdtr_ctrl(info->MCR);
1177 local_irq_restore(flags);
1182 * rs_break() --- routine which turns the break handling on or off
1184 static int rs_break(struct tty_struct *tty, int break_state)
1186 struct serial_state *info = tty->driver_data;
1187 unsigned long flags;
1189 if (serial_paranoia_check(info, tty->name, "rs_break"))
1192 local_irq_save(flags);
1193 if (break_state == -1)
1194 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1196 custom.adkcon = AC_UARTBRK;
1198 local_irq_restore(flags);
1203 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1204 * Return: write counters to the user passed counter struct
1205 * NB: both 1->0 and 0->1 transitions are counted except for
1206 * RI where only 0->1 is counted.
1208 static int rs_get_icount(struct tty_struct *tty,
1209 struct serial_icounter_struct *icount)
1211 struct serial_state *info = tty->driver_data;
1212 struct async_icount cnow;
1213 unsigned long flags;
1215 local_irq_save(flags);
1216 cnow = info->icount;
1217 local_irq_restore(flags);
1218 icount->cts = cnow.cts;
1219 icount->dsr = cnow.dsr;
1220 icount->rng = cnow.rng;
1221 icount->dcd = cnow.dcd;
1222 icount->rx = cnow.rx;
1223 icount->tx = cnow.tx;
1224 icount->frame = cnow.frame;
1225 icount->overrun = cnow.overrun;
1226 icount->parity = cnow.parity;
1227 icount->brk = cnow.brk;
1228 icount->buf_overrun = cnow.buf_overrun;
1233 static int rs_ioctl(struct tty_struct *tty,
1234 unsigned int cmd, unsigned long arg)
1236 struct serial_state *info = tty->driver_data;
1237 struct async_icount cprev, cnow; /* kernel counter temps */
1238 void __user *argp = (void __user *)arg;
1239 unsigned long flags;
1241 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1244 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1245 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1246 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1247 if (tty->flags & (1 << TTY_IO_ERROR))
1253 return get_serial_info(info, argp);
1255 return set_serial_info(tty, info, argp);
1259 case TIOCSERGETLSR: /* Get line status register */
1260 return get_lsr_info(info, argp);
1262 case TIOCSERGSTRUCT:
1263 if (copy_to_user(argp,
1264 info, sizeof(struct serial_state)))
1269 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1270 * - mask passed in arg for lines of interest
1271 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1272 * Caller should use TIOCGICOUNT to see which one it was
1275 local_irq_save(flags);
1276 /* note the counters on entry */
1277 cprev = info->icount;
1278 local_irq_restore(flags);
1280 interruptible_sleep_on(&info->tport.delta_msr_wait);
1281 /* see if a signal did it */
1282 if (signal_pending(current))
1283 return -ERESTARTSYS;
1284 local_irq_save(flags);
1285 cnow = info->icount; /* atomic copy */
1286 local_irq_restore(flags);
1287 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1288 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1289 return -EIO; /* no change => error */
1290 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1291 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1292 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1293 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1302 /* "setserial -W" is called in Debian boot */
1303 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1307 return -ENOIOCTLCMD;
1312 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1314 struct serial_state *info = tty->driver_data;
1315 unsigned long flags;
1316 unsigned int cflag = tty->termios->c_cflag;
1318 change_speed(tty, info, old_termios);
1320 /* Handle transition to B0 status */
1321 if ((old_termios->c_cflag & CBAUD) &&
1323 info->MCR &= ~(SER_DTR|SER_RTS);
1324 local_irq_save(flags);
1325 rtsdtr_ctrl(info->MCR);
1326 local_irq_restore(flags);
1329 /* Handle transition away from B0 status */
1330 if (!(old_termios->c_cflag & CBAUD) &&
1332 info->MCR |= SER_DTR;
1333 if (!(tty->termios->c_cflag & CRTSCTS) ||
1334 !test_bit(TTY_THROTTLED, &tty->flags)) {
1335 info->MCR |= SER_RTS;
1337 local_irq_save(flags);
1338 rtsdtr_ctrl(info->MCR);
1339 local_irq_restore(flags);
1342 /* Handle turning off CRTSCTS */
1343 if ((old_termios->c_cflag & CRTSCTS) &&
1344 !(tty->termios->c_cflag & CRTSCTS)) {
1345 tty->hw_stopped = 0;
1351 * No need to wake up processes in open wait, since they
1352 * sample the CLOCAL flag once, and don't recheck it.
1353 * XXX It's not clear whether the current behavior is correct
1354 * or not. Hence, this may change.....
1356 if (!(old_termios->c_cflag & CLOCAL) &&
1357 (tty->termios->c_cflag & CLOCAL))
1358 wake_up_interruptible(&info->open_wait);
1363 * ------------------------------------------------------------
1366 * This routine is called when the serial port gets closed. First, we
1367 * wait for the last remaining data to be sent. Then, we unlink its
1368 * async structure from the interrupt chain if necessary, and we free
1369 * that IRQ if nothing is left in the chain.
1370 * ------------------------------------------------------------
1372 static void rs_close(struct tty_struct *tty, struct file * filp)
1374 struct serial_state *state = tty->driver_data;
1375 struct tty_port *port = &state->tport;
1376 unsigned long flags;
1378 if (!state || serial_paranoia_check(state, tty->name, "rs_close"))
1381 local_irq_save(flags);
1383 if (tty_hung_up_p(filp)) {
1384 DBG_CNT("before DEC-hung");
1385 local_irq_restore(flags);
1389 #ifdef SERIAL_DEBUG_OPEN
1390 printk("rs_close ttys%d, count = %d\n", state->line, port->count);
1392 if ((tty->count == 1) && (port->count != 1)) {
1394 * Uh, oh. tty->count is 1, which means that the tty
1395 * structure will be freed. port->count should always
1396 * be one in these conditions. If it's greater than
1397 * one, we've got real problems, since it means the
1398 * serial port won't be shutdown.
1400 printk("rs_close: bad serial port count; tty->count is 1, "
1401 "port->count is %d\n", state->tport.count);
1404 if (--port->count < 0) {
1405 printk("rs_close: bad serial port count for ttys%d: %d\n",
1406 state->line, port->count);
1410 DBG_CNT("before DEC-2");
1411 local_irq_restore(flags);
1414 port->flags |= ASYNC_CLOSING;
1416 * Now we wait for the transmit buffer to clear; and we notify
1417 * the line discipline to only process XON/XOFF characters.
1420 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1421 tty_wait_until_sent(tty, port->closing_wait);
1423 * At this point we stop accepting input. To do this, we
1424 * disable the receive line status interrupts, and tell the
1425 * interrupt driver to stop checking the data ready bit in the
1426 * line status register.
1428 state->read_status_mask &= ~UART_LSR_DR;
1429 if (port->flags & ASYNC_INITIALIZED) {
1430 /* disable receive interrupts */
1431 custom.intena = IF_RBF;
1433 /* clear any pending receive interrupt */
1434 custom.intreq = IF_RBF;
1438 * Before we drop DTR, make sure the UART transmitter
1439 * has completely drained; this is especially
1440 * important if there is a transmit FIFO!
1442 rs_wait_until_sent(tty, state->timeout);
1444 shutdown(tty, state);
1445 rs_flush_buffer(tty);
1447 tty_ldisc_flush(tty);
1450 if (port->blocked_open) {
1451 if (port->close_delay) {
1452 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1454 wake_up_interruptible(&port->open_wait);
1456 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1457 wake_up_interruptible(&port->close_wait);
1458 local_irq_restore(flags);
1462 * rs_wait_until_sent() --- wait until the transmitter is empty
1464 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1466 struct serial_state *info = tty->driver_data;
1467 unsigned long orig_jiffies, char_time;
1470 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1473 if (info->xmit_fifo_size == 0)
1474 return; /* Just in case.... */
1476 orig_jiffies = jiffies;
1479 * Set the check interval to be 1/5 of the estimated time to
1480 * send a single character, and make it at least 1. The check
1481 * interval should also be less than the timeout.
1483 * Note: we have to use pretty tight timings here to satisfy
1486 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1487 char_time = char_time / 5;
1491 char_time = min_t(unsigned long, char_time, timeout);
1493 * If the transmitter hasn't cleared in twice the approximate
1494 * amount of time to send the entire FIFO, it probably won't
1495 * ever clear. This assumes the UART isn't doing flow
1496 * control, which is currently the case. Hence, if it ever
1497 * takes longer than info->timeout, this is probably due to a
1498 * UART bug of some kind. So, we clamp the timeout parameter at
1501 if (!timeout || timeout > 2*info->timeout)
1502 timeout = 2*info->timeout;
1503 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1504 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1505 printk("jiff=%lu...", jiffies);
1507 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1508 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1509 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1511 msleep_interruptible(jiffies_to_msecs(char_time));
1512 if (signal_pending(current))
1514 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1517 __set_current_state(TASK_RUNNING);
1519 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1520 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1525 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1527 static void rs_hangup(struct tty_struct *tty)
1529 struct serial_state *info = tty->driver_data;
1531 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1534 rs_flush_buffer(tty);
1535 shutdown(tty, info);
1536 info->tport.count = 0;
1537 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
1538 info->tport.tty = NULL;
1539 wake_up_interruptible(&info->tport.open_wait);
1543 * ------------------------------------------------------------
1544 * rs_open() and friends
1545 * ------------------------------------------------------------
1547 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1548 struct serial_state *info)
1550 #ifdef DECLARE_WAITQUEUE
1551 DECLARE_WAITQUEUE(wait, current);
1553 struct wait_queue wait = { current, NULL };
1555 struct tty_port *port = &info->tport;
1557 int do_clocal = 0, extra_count = 0;
1558 unsigned long flags;
1561 * If the device is in the middle of being closed, then block
1562 * until it's done, and then try again.
1564 if (tty_hung_up_p(filp) ||
1565 (port->flags & ASYNC_CLOSING)) {
1566 if (port->flags & ASYNC_CLOSING)
1567 interruptible_sleep_on(&port->close_wait);
1568 #ifdef SERIAL_DO_RESTART
1569 return ((port->flags & ASYNC_HUP_NOTIFY) ?
1570 -EAGAIN : -ERESTARTSYS);
1577 * If non-blocking mode is set, or the port is not enabled,
1578 * then make the check up front and then exit.
1580 if ((filp->f_flags & O_NONBLOCK) ||
1581 (tty->flags & (1 << TTY_IO_ERROR))) {
1582 port->flags |= ASYNC_NORMAL_ACTIVE;
1586 if (tty->termios->c_cflag & CLOCAL)
1590 * Block waiting for the carrier detect and the line to become
1591 * free (i.e., not in use by the callout). While we are in
1592 * this loop, port->count is dropped by one, so that
1593 * rs_close() knows when to free things. We restore it upon
1594 * exit, either normal or abnormal.
1597 add_wait_queue(&port->open_wait, &wait);
1598 #ifdef SERIAL_DEBUG_OPEN
1599 printk("block_til_ready before block: ttys%d, count = %d\n",
1600 info->line, port->count);
1602 local_irq_save(flags);
1603 if (!tty_hung_up_p(filp)) {
1607 local_irq_restore(flags);
1608 port->blocked_open++;
1610 local_irq_save(flags);
1611 if (tty->termios->c_cflag & CBAUD)
1612 rtsdtr_ctrl(SER_DTR|SER_RTS);
1613 local_irq_restore(flags);
1614 set_current_state(TASK_INTERRUPTIBLE);
1615 if (tty_hung_up_p(filp) ||
1616 !(port->flags & ASYNC_INITIALIZED)) {
1617 #ifdef SERIAL_DO_RESTART
1618 if (port->flags & ASYNC_HUP_NOTIFY)
1621 retval = -ERESTARTSYS;
1627 if (!(port->flags & ASYNC_CLOSING) &&
1628 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1630 if (signal_pending(current)) {
1631 retval = -ERESTARTSYS;
1634 #ifdef SERIAL_DEBUG_OPEN
1635 printk("block_til_ready blocking: ttys%d, count = %d\n",
1636 info->line, port->count);
1642 __set_current_state(TASK_RUNNING);
1643 remove_wait_queue(&port->open_wait, &wait);
1646 port->blocked_open--;
1647 #ifdef SERIAL_DEBUG_OPEN
1648 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1649 info->line, port->count);
1653 port->flags |= ASYNC_NORMAL_ACTIVE;
1658 * This routine is called whenever a serial port is opened. It
1659 * enables interrupts for a serial port, linking in its async structure into
1660 * the IRQ chain. It also performs the serial-specific
1661 * initialization for the tty structure.
1663 static int rs_open(struct tty_struct *tty, struct file * filp)
1665 struct serial_state *info = rs_table + tty->index;
1666 struct tty_port *port = &info->tport;
1671 tty->driver_data = info;
1673 if (serial_paranoia_check(info, tty->name, "rs_open"))
1676 #ifdef SERIAL_DEBUG_OPEN
1677 printk("rs_open %s, count = %d\n", tty->name, info->count);
1679 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1682 * If the port is the middle of closing, bail out now
1684 if (tty_hung_up_p(filp) ||
1685 (port->flags & ASYNC_CLOSING)) {
1686 if (port->flags & ASYNC_CLOSING)
1687 interruptible_sleep_on(&port->close_wait);
1688 #ifdef SERIAL_DO_RESTART
1689 return ((port->flags & ASYNC_HUP_NOTIFY) ?
1690 -EAGAIN : -ERESTARTSYS);
1697 * Start up serial port
1699 retval = startup(tty, info);
1704 retval = block_til_ready(tty, filp, info);
1706 #ifdef SERIAL_DEBUG_OPEN
1707 printk("rs_open returning after block_til_ready with %d\n",
1713 #ifdef SERIAL_DEBUG_OPEN
1714 printk("rs_open %s successful...", tty->name);
1720 * /proc fs routines....
1723 static inline void line_info(struct seq_file *m, struct serial_state *state)
1725 char stat_buf[30], control, status;
1726 unsigned long flags;
1728 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1730 local_irq_save(flags);
1732 control = (state->tport.flags & ASYNC_INITIALIZED) ? state->MCR : status;
1733 local_irq_restore(flags);
1737 if(!(control & SER_RTS))
1738 strcat(stat_buf, "|RTS");
1739 if(!(status & SER_CTS))
1740 strcat(stat_buf, "|CTS");
1741 if(!(control & SER_DTR))
1742 strcat(stat_buf, "|DTR");
1743 if(!(status & SER_DSR))
1744 strcat(stat_buf, "|DSR");
1745 if(!(status & SER_DCD))
1746 strcat(stat_buf, "|CD");
1749 seq_printf(m, " baud:%d", state->baud_base / state->quot);
1751 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1753 if (state->icount.frame)
1754 seq_printf(m, " fe:%d", state->icount.frame);
1756 if (state->icount.parity)
1757 seq_printf(m, " pe:%d", state->icount.parity);
1759 if (state->icount.brk)
1760 seq_printf(m, " brk:%d", state->icount.brk);
1762 if (state->icount.overrun)
1763 seq_printf(m, " oe:%d", state->icount.overrun);
1766 * Last thing is the RS-232 status lines
1768 seq_printf(m, " %s\n", stat_buf+1);
1771 static int rs_proc_show(struct seq_file *m, void *v)
1773 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1774 line_info(m, &rs_table[0]);
1778 static int rs_proc_open(struct inode *inode, struct file *file)
1780 return single_open(file, rs_proc_show, NULL);
1783 static const struct file_operations rs_proc_fops = {
1784 .owner = THIS_MODULE,
1785 .open = rs_proc_open,
1787 .llseek = seq_lseek,
1788 .release = single_release,
1792 * ---------------------------------------------------------------------
1793 * rs_init() and friends
1795 * rs_init() is called at boot-time to initialize the serial driver.
1796 * ---------------------------------------------------------------------
1800 * This routine prints out the appropriate serial driver version
1801 * number, and identifies which options were configured into this
1804 static void show_serial_version(void)
1806 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1810 static const struct tty_operations serial_ops = {
1814 .put_char = rs_put_char,
1815 .flush_chars = rs_flush_chars,
1816 .write_room = rs_write_room,
1817 .chars_in_buffer = rs_chars_in_buffer,
1818 .flush_buffer = rs_flush_buffer,
1820 .throttle = rs_throttle,
1821 .unthrottle = rs_unthrottle,
1822 .set_termios = rs_set_termios,
1825 .hangup = rs_hangup,
1826 .break_ctl = rs_break,
1827 .send_xchar = rs_send_xchar,
1828 .wait_until_sent = rs_wait_until_sent,
1829 .tiocmget = rs_tiocmget,
1830 .tiocmset = rs_tiocmset,
1831 .get_icount = rs_get_icount,
1832 .proc_fops = &rs_proc_fops,
1836 * The serial driver boot-time initialization code!
1838 static int __init amiga_serial_probe(struct platform_device *pdev)
1840 unsigned long flags;
1841 struct serial_state * state;
1844 serial_driver = alloc_tty_driver(NR_PORTS);
1848 show_serial_version();
1850 /* Initialize the tty_driver structure */
1852 serial_driver->driver_name = "amiserial";
1853 serial_driver->name = "ttyS";
1854 serial_driver->major = TTY_MAJOR;
1855 serial_driver->minor_start = 64;
1856 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1857 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1858 serial_driver->init_termios = tty_std_termios;
1859 serial_driver->init_termios.c_cflag =
1860 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1861 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1862 tty_set_operations(serial_driver, &serial_ops);
1864 error = tty_register_driver(serial_driver);
1866 goto fail_put_tty_driver;
1869 state->port = (int)&custom.serdatr; /* Just to give it a value */
1871 state->custom_divisor = 0;
1872 state->icount.cts = state->icount.dsr =
1873 state->icount.rng = state->icount.dcd = 0;
1874 state->icount.rx = state->icount.tx = 0;
1875 state->icount.frame = state->icount.parity = 0;
1876 state->icount.overrun = state->icount.brk = 0;
1877 tty_port_init(&state->tport);
1879 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1882 /* Hardware set up */
1884 state->baud_base = amiga_colorclock;
1885 state->xmit_fifo_size = 1;
1887 /* set ISRs, and then disable the rx interrupts */
1888 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1890 goto fail_unregister;
1892 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
1893 "serial RX", state);
1897 local_irq_save(flags);
1899 /* turn off Rx and Tx interrupts */
1900 custom.intena = IF_RBF | IF_TBE;
1903 /* clear any pending interrupt */
1904 custom.intreq = IF_RBF | IF_TBE;
1907 local_irq_restore(flags);
1910 * set the appropriate directions for the modem control flags,
1911 * and clear RTS and DTR
1913 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1914 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1916 platform_set_drvdata(pdev, state);
1921 free_irq(IRQ_AMIGA_TBE, state);
1923 tty_unregister_driver(serial_driver);
1924 fail_put_tty_driver:
1925 put_tty_driver(serial_driver);
1929 static int __exit amiga_serial_remove(struct platform_device *pdev)
1932 struct serial_state *state = platform_get_drvdata(pdev);
1934 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
1935 if ((error = tty_unregister_driver(serial_driver)))
1936 printk("SERIAL: failed to unregister serial driver (%d)\n",
1938 put_tty_driver(serial_driver);
1940 free_irq(IRQ_AMIGA_TBE, state);
1941 free_irq(IRQ_AMIGA_RBF, state);
1943 platform_set_drvdata(pdev, NULL);
1948 static struct platform_driver amiga_serial_driver = {
1949 .remove = __exit_p(amiga_serial_remove),
1951 .name = "amiga-serial",
1952 .owner = THIS_MODULE,
1956 static int __init amiga_serial_init(void)
1958 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
1961 module_init(amiga_serial_init);
1963 static void __exit amiga_serial_exit(void)
1965 platform_driver_unregister(&amiga_serial_driver);
1968 module_exit(amiga_serial_exit);
1971 #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
1974 * ------------------------------------------------------------
1975 * Serial console driver
1976 * ------------------------------------------------------------
1979 static void amiga_serial_putc(char c)
1981 custom.serdat = (unsigned char)c | 0x100;
1982 while (!(custom.serdatr & 0x2000))
1987 * Print a string to the serial port trying not to disturb
1988 * any possible real use of the port...
1990 * The console must be locked when we get here.
1992 static void serial_console_write(struct console *co, const char *s,
1995 unsigned short intena = custom.intenar;
1997 custom.intena = IF_TBE;
2001 amiga_serial_putc('\r');
2002 amiga_serial_putc(*s++);
2005 custom.intena = IF_SETCLR | (intena & IF_TBE);
2008 static struct tty_driver *serial_console_device(struct console *c, int *index)
2011 return serial_driver;
2014 static struct console sercons = {
2016 .write = serial_console_write,
2017 .device = serial_console_device,
2018 .flags = CON_PRINTBUFFER,
2025 static int __init amiserial_console_init(void)
2027 register_console(&sercons);
2030 console_initcall(amiserial_console_init);
2032 #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2034 MODULE_LICENSE("GPL");
2035 MODULE_ALIAS("platform:amiga-serial");