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 struct async_struct *IRQ_ports;
105 static unsigned char current_ctl_bits;
107 static void change_speed(struct async_struct *info, struct ktermios *old);
108 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
111 static struct serial_state rs_table[1];
113 #define NR_PORTS ARRAY_SIZE(rs_table)
115 #include <asm/uaccess.h>
117 #define serial_isroot() (capable(CAP_SYS_ADMIN))
120 static inline int serial_paranoia_check(struct async_struct *info,
121 char *name, const char *routine)
123 #ifdef SERIAL_PARANOIA_CHECK
124 static const char *badmagic =
125 "Warning: bad magic number for serial struct (%s) in %s\n";
126 static const char *badinfo =
127 "Warning: null async_struct for (%s) in %s\n";
130 printk(badinfo, name, routine);
133 if (info->magic != SERIAL_MAGIC) {
134 printk(badmagic, name, routine);
141 /* some serial hardware definitions */
142 #define SDR_OVRUN (1<<15)
143 #define SDR_RBF (1<<14)
144 #define SDR_TBE (1<<13)
145 #define SDR_TSRE (1<<12)
147 #define SERPER_PARENB (1<<15)
149 #define AC_SETCLR (1<<15)
150 #define AC_UARTBRK (1<<11)
152 #define SER_DTR (1<<7)
153 #define SER_RTS (1<<6)
154 #define SER_DCD (1<<5)
155 #define SER_CTS (1<<4)
156 #define SER_DSR (1<<3)
158 static __inline__ void rtsdtr_ctrl(int bits)
160 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
164 * ------------------------------------------------------------
165 * rs_stop() and rs_start()
167 * This routines are called before setting or resetting tty->stopped.
168 * They enable or disable transmitter interrupts, as necessary.
169 * ------------------------------------------------------------
171 static void rs_stop(struct tty_struct *tty)
173 struct async_struct *info = tty->driver_data;
176 if (serial_paranoia_check(info, tty->name, "rs_stop"))
179 local_irq_save(flags);
180 if (info->IER & UART_IER_THRI) {
181 info->IER &= ~UART_IER_THRI;
182 /* disable Tx interrupt and remove any pending interrupts */
183 custom.intena = IF_TBE;
185 custom.intreq = IF_TBE;
188 local_irq_restore(flags);
191 static void rs_start(struct tty_struct *tty)
193 struct async_struct *info = tty->driver_data;
196 if (serial_paranoia_check(info, tty->name, "rs_start"))
199 local_irq_save(flags);
200 if (info->xmit.head != info->xmit.tail
202 && !(info->IER & UART_IER_THRI)) {
203 info->IER |= UART_IER_THRI;
204 custom.intena = IF_SETCLR | IF_TBE;
206 /* set a pending Tx Interrupt, transmitter should restart now */
207 custom.intreq = IF_SETCLR | IF_TBE;
210 local_irq_restore(flags);
214 * ----------------------------------------------------------------------
216 * Here starts the interrupt handling routines. All of the following
217 * subroutines are declared as inline and are folded into
218 * rs_interrupt(). They were separated out for readability's sake.
220 * Note: rs_interrupt() is a "fast" interrupt, which means that it
221 * runs with interrupts turned off. People who may want to modify
222 * rs_interrupt() should try to keep the interrupt handler as fast as
223 * possible. After you are done making modifications, it is not a bad
226 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
228 * and look at the resulting assemble code in serial.s.
230 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
231 * -----------------------------------------------------------------------
234 static void receive_chars(struct async_struct *info)
238 struct tty_struct *tty = info->tty;
239 unsigned char ch, flag;
240 struct async_icount *icount;
243 icount = &info->state->icount;
245 status = UART_LSR_DR; /* We obviously have a character! */
246 serdatr = custom.serdatr;
248 custom.intreq = IF_RBF;
251 if((serdatr & 0x1ff) == 0)
252 status |= UART_LSR_BI;
253 if(serdatr & SDR_OVRUN)
254 status |= UART_LSR_OE;
259 #ifdef SERIAL_DEBUG_INTR
260 printk("DR%02x:%02x...", ch, status);
265 * We don't handle parity or frame errors - but I have left
266 * the code in, since I'm not sure that the errors can't be
270 if (status & (UART_LSR_BI | UART_LSR_PE |
271 UART_LSR_FE | UART_LSR_OE)) {
273 * For statistics only
275 if (status & UART_LSR_BI) {
276 status &= ~(UART_LSR_FE | UART_LSR_PE);
278 } else if (status & UART_LSR_PE)
280 else if (status & UART_LSR_FE)
282 if (status & UART_LSR_OE)
286 * Now check to see if character should be
287 * ignored, and mask off conditions which
290 if (status & info->ignore_status_mask)
293 status &= info->read_status_mask;
295 if (status & (UART_LSR_BI)) {
296 #ifdef SERIAL_DEBUG_INTR
297 printk("handling break....");
300 if (info->flags & ASYNC_SAK)
302 } else if (status & UART_LSR_PE)
304 else if (status & UART_LSR_FE)
306 if (status & UART_LSR_OE) {
308 * Overrun is special, since it's
309 * reported immediately, and doesn't
310 * affect the current character
315 tty_insert_flip_char(tty, ch, flag);
317 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
318 tty_flip_buffer_push(tty);
323 static void transmit_chars(struct async_struct *info)
325 custom.intreq = IF_TBE;
328 custom.serdat = info->x_char | 0x100;
330 info->state->icount.tx++;
334 if (info->xmit.head == info->xmit.tail
335 || info->tty->stopped
336 || info->tty->hw_stopped) {
337 info->IER &= ~UART_IER_THRI;
338 custom.intena = IF_TBE;
343 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
345 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
346 info->state->icount.tx++;
348 if (CIRC_CNT(info->xmit.head,
350 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
351 tty_wakeup(info->tty);
353 #ifdef SERIAL_DEBUG_INTR
356 if (info->xmit.head == info->xmit.tail) {
357 custom.intena = IF_TBE;
359 info->IER &= ~UART_IER_THRI;
363 static void check_modem_status(struct async_struct *info)
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->state->icount;
375 /* update input line counters */
376 if (dstatus & SER_DSR)
378 if (dstatus & SER_DCD) {
380 #ifdef CONFIG_HARD_PPS
381 if ((info->flags & ASYNC_HARDPPS_CD) &&
386 if (dstatus & SER_CTS)
388 wake_up_interruptible(&info->delta_msr_wait);
391 if ((info->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(&info->open_wait);
399 #ifdef SERIAL_DEBUG_OPEN
400 printk("doing serial hangup...");
403 tty_hangup(info->tty);
406 if (info->flags & ASYNC_CTS_FLOW) {
407 if (info->tty->hw_stopped) {
408 if (!(status & SER_CTS)) {
409 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
410 printk("CTS tx start...");
412 info->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(info->tty);
423 if ((status & SER_CTS)) {
424 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
425 printk("CTS tx stop...");
427 info->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 async_struct * info = IRQ_ports;
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 async_struct * info;
456 #ifdef SERIAL_DEBUG_INTR
457 printk("ser_rx_int...");
461 if (!info || !info->tty)
465 #ifdef SERIAL_DEBUG_INTR
471 static irqreturn_t ser_tx_int(int irq, void *dev_id)
473 struct async_struct * info;
475 if (custom.serdatr & SDR_TBE) {
476 #ifdef SERIAL_DEBUG_INTR
477 printk("ser_tx_int...");
481 if (!info || !info->tty)
484 transmit_chars(info);
485 #ifdef SERIAL_DEBUG_INTR
493 * -------------------------------------------------------------------
494 * Here ends the serial interrupt routines.
495 * -------------------------------------------------------------------
499 * ---------------------------------------------------------------
500 * Low level utility subroutines for the serial driver: routines to
501 * figure out the appropriate timeout for an interrupt chain, routines
502 * to initialize and startup a serial port, and routines to shutdown a
503 * serial port. Useful stuff like that.
504 * ---------------------------------------------------------------
507 static int startup(struct async_struct * info)
513 page = get_zeroed_page(GFP_KERNEL);
517 local_irq_save(flags);
519 if (info->flags & ASYNC_INITIALIZED) {
527 info->xmit.buf = (unsigned char *) page;
529 #ifdef SERIAL_DEBUG_OPEN
530 printk("starting up ttys%d ...", info->line);
533 /* Clear anything in the input buffer */
535 custom.intreq = IF_RBF;
538 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
540 if (serial_isroot()) {
542 set_bit(TTY_IO_ERROR,
549 /* enable both Rx and Tx interrupts */
550 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
552 info->IER = UART_IER_MSI;
554 /* remember current state of the DCD and CTS bits */
555 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
560 if (info->tty->termios->c_cflag & CBAUD)
561 info->MCR = SER_DTR | SER_RTS;
562 rtsdtr_ctrl(info->MCR);
565 clear_bit(TTY_IO_ERROR, &info->tty->flags);
566 info->xmit.head = info->xmit.tail = 0;
569 * Set up the tty->alt_speed kludge
572 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
573 info->tty->alt_speed = 57600;
574 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
575 info->tty->alt_speed = 115200;
576 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
577 info->tty->alt_speed = 230400;
578 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
579 info->tty->alt_speed = 460800;
583 * and set the speed of the serial port
585 change_speed(info, NULL);
587 info->flags |= ASYNC_INITIALIZED;
588 local_irq_restore(flags);
592 local_irq_restore(flags);
597 * This routine will shutdown a serial port; interrupts are disabled, and
598 * DTR is dropped if the hangup on close termio flag is on.
600 static void shutdown(struct async_struct * info)
603 struct serial_state *state;
605 if (!(info->flags & ASYNC_INITIALIZED))
610 #ifdef SERIAL_DEBUG_OPEN
611 printk("Shutting down serial port %d ....\n", info->line);
614 local_irq_save(flags); /* Disable interrupts */
617 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
618 * here so the queue might never be waken up
620 wake_up_interruptible(&info->delta_msr_wait);
625 * Free the IRQ, if necessary
627 free_irq(IRQ_AMIGA_VERTB, info);
629 if (info->xmit.buf) {
630 free_page((unsigned long) info->xmit.buf);
631 info->xmit.buf = NULL;
635 custom.intena = IF_RBF | IF_TBE;
638 /* disable break condition */
639 custom.adkcon = AC_UARTBRK;
642 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
643 info->MCR &= ~(SER_DTR|SER_RTS);
644 rtsdtr_ctrl(info->MCR);
647 set_bit(TTY_IO_ERROR, &info->tty->flags);
649 info->flags &= ~ASYNC_INITIALIZED;
650 local_irq_restore(flags);
655 * This routine is called to set the UART divisor registers to match
656 * the specified baud rate for a serial port.
658 static void change_speed(struct async_struct *info,
659 struct ktermios *old_termios)
661 int quot = 0, baud_base, baud;
662 unsigned cflag, cval = 0;
666 if (!info->tty || !info->tty->termios)
668 cflag = info->tty->termios->c_cflag;
670 /* Byte size is always 8 bits plus parity bit if requested */
673 if (cflag & CSTOPB) {
677 if (cflag & PARENB) {
678 cval |= UART_LCR_PARITY;
681 if (!(cflag & PARODD))
682 cval |= UART_LCR_EPAR;
685 cval |= UART_LCR_SPAR;
688 /* Determine divisor based on baud rate */
689 baud = tty_get_baud_rate(info->tty);
691 baud = 9600; /* B0 transition handled in rs_set_termios */
692 baud_base = info->state->baud_base;
694 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
695 quot = info->state->custom_divisor;
698 /* Special case since 134 is really 134.5 */
699 quot = (2*baud_base / 269);
701 quot = baud_base / baud;
703 /* If the quotient is zero refuse the change */
704 if (!quot && old_termios) {
705 /* FIXME: Will need updating for new tty in the end */
706 info->tty->termios->c_cflag &= ~CBAUD;
707 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
708 baud = tty_get_baud_rate(info->tty);
712 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
713 quot = info->state->custom_divisor;
716 /* Special case since 134 is really 134.5 */
717 quot = (2*baud_base / 269);
719 quot = baud_base / baud;
722 /* As a last resort, if the quotient is zero, default to 9600 bps */
724 quot = baud_base / 9600;
726 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
727 info->timeout += HZ/50; /* Add .02 seconds of slop */
729 /* CTS flow control flag and modem status interrupts */
730 info->IER &= ~UART_IER_MSI;
731 if (info->flags & ASYNC_HARDPPS_CD)
732 info->IER |= UART_IER_MSI;
733 if (cflag & CRTSCTS) {
734 info->flags |= ASYNC_CTS_FLOW;
735 info->IER |= UART_IER_MSI;
737 info->flags &= ~ASYNC_CTS_FLOW;
739 info->flags &= ~ASYNC_CHECK_CD;
741 info->flags |= ASYNC_CHECK_CD;
742 info->IER |= UART_IER_MSI;
745 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
749 * Set up parity check flag
752 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
753 if (I_INPCK(info->tty))
754 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
755 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
756 info->read_status_mask |= UART_LSR_BI;
759 * Characters to ignore
761 info->ignore_status_mask = 0;
762 if (I_IGNPAR(info->tty))
763 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
764 if (I_IGNBRK(info->tty)) {
765 info->ignore_status_mask |= UART_LSR_BI;
767 * If we're ignore parity and break indicators, ignore
768 * overruns too. (For real raw support).
770 if (I_IGNPAR(info->tty))
771 info->ignore_status_mask |= UART_LSR_OE;
774 * !!! ignore all characters if CREAD is not set
776 if ((cflag & CREAD) == 0)
777 info->ignore_status_mask |= UART_LSR_DR;
778 local_irq_save(flags);
783 /* Set up the baud rate */
786 /* Enable or disable parity bit */
788 if(cval & UART_LCR_PARITY)
789 serper |= (SERPER_PARENB);
791 custom.serper = serper;
795 local_irq_restore(flags);
798 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
800 struct async_struct *info;
803 info = tty->driver_data;
805 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
811 local_irq_save(flags);
812 if (CIRC_SPACE(info->xmit.head,
814 SERIAL_XMIT_SIZE) == 0) {
815 local_irq_restore(flags);
819 info->xmit.buf[info->xmit.head++] = ch;
820 info->xmit.head &= SERIAL_XMIT_SIZE-1;
821 local_irq_restore(flags);
825 static void rs_flush_chars(struct tty_struct *tty)
827 struct async_struct *info = tty->driver_data;
830 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
833 if (info->xmit.head == info->xmit.tail
839 local_irq_save(flags);
840 info->IER |= UART_IER_THRI;
841 custom.intena = IF_SETCLR | IF_TBE;
843 /* set a pending Tx Interrupt, transmitter should restart now */
844 custom.intreq = IF_SETCLR | IF_TBE;
846 local_irq_restore(flags);
849 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
852 struct async_struct *info;
855 info = tty->driver_data;
857 if (serial_paranoia_check(info, tty->name, "rs_write"))
863 local_irq_save(flags);
865 c = CIRC_SPACE_TO_END(info->xmit.head,
873 memcpy(info->xmit.buf + info->xmit.head, buf, c);
874 info->xmit.head = ((info->xmit.head + c) &
875 (SERIAL_XMIT_SIZE-1));
880 local_irq_restore(flags);
882 if (info->xmit.head != info->xmit.tail
885 && !(info->IER & UART_IER_THRI)) {
886 info->IER |= UART_IER_THRI;
888 custom.intena = IF_SETCLR | IF_TBE;
890 /* set a pending Tx Interrupt, transmitter should restart now */
891 custom.intreq = IF_SETCLR | IF_TBE;
893 local_irq_restore(flags);
898 static int rs_write_room(struct tty_struct *tty)
900 struct async_struct *info = tty->driver_data;
902 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
904 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
907 static int rs_chars_in_buffer(struct tty_struct *tty)
909 struct async_struct *info = tty->driver_data;
911 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
913 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
916 static void rs_flush_buffer(struct tty_struct *tty)
918 struct async_struct *info = tty->driver_data;
921 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
923 local_irq_save(flags);
924 info->xmit.head = info->xmit.tail = 0;
925 local_irq_restore(flags);
930 * This function is used to send a high-priority XON/XOFF character to
933 static void rs_send_xchar(struct tty_struct *tty, char ch)
935 struct async_struct *info = tty->driver_data;
938 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
943 /* Make sure transmit interrupts are on */
946 local_irq_save(flags);
947 if(!(custom.intenar & IF_TBE)) {
948 custom.intena = IF_SETCLR | IF_TBE;
950 /* set a pending Tx Interrupt, transmitter should restart now */
951 custom.intreq = IF_SETCLR | IF_TBE;
954 local_irq_restore(flags);
956 info->IER |= UART_IER_THRI;
961 * ------------------------------------------------------------
964 * This routine is called by the upper-layer tty layer to signal that
965 * incoming characters should be throttled.
966 * ------------------------------------------------------------
968 static void rs_throttle(struct tty_struct * tty)
970 struct async_struct *info = tty->driver_data;
972 #ifdef SERIAL_DEBUG_THROTTLE
975 printk("throttle %s: %d....\n", tty_name(tty, buf),
976 tty->ldisc.chars_in_buffer(tty));
979 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
983 rs_send_xchar(tty, STOP_CHAR(tty));
985 if (tty->termios->c_cflag & CRTSCTS)
986 info->MCR &= ~SER_RTS;
988 local_irq_save(flags);
989 rtsdtr_ctrl(info->MCR);
990 local_irq_restore(flags);
993 static void rs_unthrottle(struct tty_struct * tty)
995 struct async_struct *info = tty->driver_data;
997 #ifdef SERIAL_DEBUG_THROTTLE
1000 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1001 tty->ldisc.chars_in_buffer(tty));
1004 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1011 rs_send_xchar(tty, START_CHAR(tty));
1013 if (tty->termios->c_cflag & CRTSCTS)
1014 info->MCR |= SER_RTS;
1015 local_irq_save(flags);
1016 rtsdtr_ctrl(info->MCR);
1017 local_irq_restore(flags);
1021 * ------------------------------------------------------------
1022 * rs_ioctl() and friends
1023 * ------------------------------------------------------------
1026 static int get_serial_info(struct async_struct * info,
1027 struct serial_struct __user * retinfo)
1029 struct serial_struct tmp;
1030 struct serial_state *state = info->state;
1034 memset(&tmp, 0, sizeof(tmp));
1036 tmp.type = state->type;
1037 tmp.line = state->line;
1038 tmp.port = state->port;
1039 tmp.irq = state->irq;
1040 tmp.flags = state->flags;
1041 tmp.xmit_fifo_size = state->xmit_fifo_size;
1042 tmp.baud_base = state->baud_base;
1043 tmp.close_delay = state->close_delay;
1044 tmp.closing_wait = state->closing_wait;
1045 tmp.custom_divisor = state->custom_divisor;
1047 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1052 static int set_serial_info(struct async_struct * info,
1053 struct serial_struct __user * new_info)
1055 struct serial_struct new_serial;
1056 struct serial_state old_state, *state;
1057 unsigned int change_irq,change_port;
1060 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1064 state = info->state;
1067 change_irq = new_serial.irq != state->irq;
1068 change_port = (new_serial.port != state->port);
1069 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1074 if (!serial_isroot()) {
1075 if ((new_serial.baud_base != state->baud_base) ||
1076 (new_serial.close_delay != state->close_delay) ||
1077 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1078 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1079 (state->flags & ~ASYNC_USR_MASK)))
1081 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1082 (new_serial.flags & ASYNC_USR_MASK));
1083 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1084 (new_serial.flags & ASYNC_USR_MASK));
1085 state->custom_divisor = new_serial.custom_divisor;
1086 goto check_and_exit;
1089 if (new_serial.baud_base < 9600) {
1095 * OK, past this point, all the error checking has been done.
1096 * At this point, we start making changes.....
1099 state->baud_base = new_serial.baud_base;
1100 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1101 (new_serial.flags & ASYNC_FLAGS));
1102 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1103 (info->flags & ASYNC_INTERNAL_FLAGS));
1104 state->custom_divisor = new_serial.custom_divisor;
1105 state->close_delay = new_serial.close_delay * HZ/100;
1106 state->closing_wait = new_serial.closing_wait * HZ/100;
1107 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1110 if (info->flags & ASYNC_INITIALIZED) {
1111 if (((old_state.flags & ASYNC_SPD_MASK) !=
1112 (state->flags & ASYNC_SPD_MASK)) ||
1113 (old_state.custom_divisor != state->custom_divisor)) {
1114 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1115 info->tty->alt_speed = 57600;
1116 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1117 info->tty->alt_speed = 115200;
1118 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1119 info->tty->alt_speed = 230400;
1120 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1121 info->tty->alt_speed = 460800;
1122 change_speed(info, NULL);
1125 retval = startup(info);
1132 * get_lsr_info - get line status register info
1134 * Purpose: Let user call ioctl() to get info when the UART physically
1135 * is emptied. On bus types like RS485, the transmitter must
1136 * release the bus after transmitting. This must be done when
1137 * the transmit shift register is empty, not be done when the
1138 * transmit holding register is empty. This functionality
1139 * allows an RS485 driver to be written in user space.
1141 static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
1143 unsigned char status;
1144 unsigned int result;
1145 unsigned long flags;
1147 local_irq_save(flags);
1148 status = custom.serdatr;
1150 local_irq_restore(flags);
1151 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1152 if (copy_to_user(value, &result, sizeof(int)))
1158 static int rs_tiocmget(struct tty_struct *tty)
1160 struct async_struct * info = tty->driver_data;
1161 unsigned char control, status;
1162 unsigned long flags;
1164 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1166 if (tty->flags & (1 << TTY_IO_ERROR))
1169 control = info->MCR;
1170 local_irq_save(flags);
1172 local_irq_restore(flags);
1173 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1174 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1175 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1176 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1177 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1180 static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1183 struct async_struct * info = tty->driver_data;
1184 unsigned long flags;
1186 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1188 if (tty->flags & (1 << TTY_IO_ERROR))
1191 local_irq_save(flags);
1192 if (set & TIOCM_RTS)
1193 info->MCR |= SER_RTS;
1194 if (set & TIOCM_DTR)
1195 info->MCR |= SER_DTR;
1196 if (clear & TIOCM_RTS)
1197 info->MCR &= ~SER_RTS;
1198 if (clear & TIOCM_DTR)
1199 info->MCR &= ~SER_DTR;
1200 rtsdtr_ctrl(info->MCR);
1201 local_irq_restore(flags);
1206 * rs_break() --- routine which turns the break handling on or off
1208 static int rs_break(struct tty_struct *tty, int break_state)
1210 struct async_struct * info = tty->driver_data;
1211 unsigned long flags;
1213 if (serial_paranoia_check(info, tty->name, "rs_break"))
1216 local_irq_save(flags);
1217 if (break_state == -1)
1218 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1220 custom.adkcon = AC_UARTBRK;
1222 local_irq_restore(flags);
1227 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1228 * Return: write counters to the user passed counter struct
1229 * NB: both 1->0 and 0->1 transitions are counted except for
1230 * RI where only 0->1 is counted.
1232 static int rs_get_icount(struct tty_struct *tty,
1233 struct serial_icounter_struct *icount)
1235 struct async_struct *info = tty->driver_data;
1236 struct async_icount cnow;
1237 unsigned long flags;
1239 local_irq_save(flags);
1240 cnow = info->state->icount;
1241 local_irq_restore(flags);
1242 icount->cts = cnow.cts;
1243 icount->dsr = cnow.dsr;
1244 icount->rng = cnow.rng;
1245 icount->dcd = cnow.dcd;
1246 icount->rx = cnow.rx;
1247 icount->tx = cnow.tx;
1248 icount->frame = cnow.frame;
1249 icount->overrun = cnow.overrun;
1250 icount->parity = cnow.parity;
1251 icount->brk = cnow.brk;
1252 icount->buf_overrun = cnow.buf_overrun;
1257 static int rs_ioctl(struct tty_struct *tty,
1258 unsigned int cmd, unsigned long arg)
1260 struct async_struct * info = tty->driver_data;
1261 struct async_icount cprev, cnow; /* kernel counter temps */
1262 void __user *argp = (void __user *)arg;
1263 unsigned long flags;
1265 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1268 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1269 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1270 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1271 if (tty->flags & (1 << TTY_IO_ERROR))
1277 return get_serial_info(info, argp);
1279 return set_serial_info(info, argp);
1283 case TIOCSERGETLSR: /* Get line status register */
1284 return get_lsr_info(info, argp);
1286 case TIOCSERGSTRUCT:
1287 if (copy_to_user(argp,
1288 info, sizeof(struct async_struct)))
1293 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1294 * - mask passed in arg for lines of interest
1295 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1296 * Caller should use TIOCGICOUNT to see which one it was
1299 local_irq_save(flags);
1300 /* note the counters on entry */
1301 cprev = info->state->icount;
1302 local_irq_restore(flags);
1304 interruptible_sleep_on(&info->delta_msr_wait);
1305 /* see if a signal did it */
1306 if (signal_pending(current))
1307 return -ERESTARTSYS;
1308 local_irq_save(flags);
1309 cnow = info->state->icount; /* atomic copy */
1310 local_irq_restore(flags);
1311 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1312 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1313 return -EIO; /* no change => error */
1314 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1315 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1316 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1317 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1326 /* "setserial -W" is called in Debian boot */
1327 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1331 return -ENOIOCTLCMD;
1336 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1338 struct async_struct *info = tty->driver_data;
1339 unsigned long flags;
1340 unsigned int cflag = tty->termios->c_cflag;
1342 change_speed(info, old_termios);
1344 /* Handle transition to B0 status */
1345 if ((old_termios->c_cflag & CBAUD) &&
1347 info->MCR &= ~(SER_DTR|SER_RTS);
1348 local_irq_save(flags);
1349 rtsdtr_ctrl(info->MCR);
1350 local_irq_restore(flags);
1353 /* Handle transition away from B0 status */
1354 if (!(old_termios->c_cflag & CBAUD) &&
1356 info->MCR |= SER_DTR;
1357 if (!(tty->termios->c_cflag & CRTSCTS) ||
1358 !test_bit(TTY_THROTTLED, &tty->flags)) {
1359 info->MCR |= SER_RTS;
1361 local_irq_save(flags);
1362 rtsdtr_ctrl(info->MCR);
1363 local_irq_restore(flags);
1366 /* Handle turning off CRTSCTS */
1367 if ((old_termios->c_cflag & CRTSCTS) &&
1368 !(tty->termios->c_cflag & CRTSCTS)) {
1369 tty->hw_stopped = 0;
1375 * No need to wake up processes in open wait, since they
1376 * sample the CLOCAL flag once, and don't recheck it.
1377 * XXX It's not clear whether the current behavior is correct
1378 * or not. Hence, this may change.....
1380 if (!(old_termios->c_cflag & CLOCAL) &&
1381 (tty->termios->c_cflag & CLOCAL))
1382 wake_up_interruptible(&info->open_wait);
1387 * ------------------------------------------------------------
1390 * This routine is called when the serial port gets closed. First, we
1391 * wait for the last remaining data to be sent. Then, we unlink its
1392 * async structure from the interrupt chain if necessary, and we free
1393 * that IRQ if nothing is left in the chain.
1394 * ------------------------------------------------------------
1396 static void rs_close(struct tty_struct *tty, struct file * filp)
1398 struct async_struct * info = tty->driver_data;
1399 struct serial_state *state;
1400 unsigned long flags;
1402 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1405 state = info->state;
1407 local_irq_save(flags);
1409 if (tty_hung_up_p(filp)) {
1410 DBG_CNT("before DEC-hung");
1411 local_irq_restore(flags);
1415 #ifdef SERIAL_DEBUG_OPEN
1416 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1418 if ((tty->count == 1) && (state->count != 1)) {
1420 * Uh, oh. tty->count is 1, which means that the tty
1421 * structure will be freed. state->count should always
1422 * be one in these conditions. If it's greater than
1423 * one, we've got real problems, since it means the
1424 * serial port won't be shutdown.
1426 printk("rs_close: bad serial port count; tty->count is 1, "
1427 "state->count is %d\n", state->count);
1430 if (--state->count < 0) {
1431 printk("rs_close: bad serial port count for ttys%d: %d\n",
1432 info->line, state->count);
1436 DBG_CNT("before DEC-2");
1437 local_irq_restore(flags);
1440 info->flags |= ASYNC_CLOSING;
1442 * Now we wait for the transmit buffer to clear; and we notify
1443 * the line discipline to only process XON/XOFF characters.
1446 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1447 tty_wait_until_sent(tty, info->closing_wait);
1449 * At this point we stop accepting input. To do this, we
1450 * disable the receive line status interrupts, and tell the
1451 * interrupt driver to stop checking the data ready bit in the
1452 * line status register.
1454 info->read_status_mask &= ~UART_LSR_DR;
1455 if (info->flags & ASYNC_INITIALIZED) {
1456 /* disable receive interrupts */
1457 custom.intena = IF_RBF;
1459 /* clear any pending receive interrupt */
1460 custom.intreq = IF_RBF;
1464 * Before we drop DTR, make sure the UART transmitter
1465 * has completely drained; this is especially
1466 * important if there is a transmit FIFO!
1468 rs_wait_until_sent(tty, info->timeout);
1471 rs_flush_buffer(tty);
1473 tty_ldisc_flush(tty);
1476 if (info->blocked_open) {
1477 if (info->close_delay) {
1478 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1480 wake_up_interruptible(&info->open_wait);
1482 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1483 wake_up_interruptible(&info->close_wait);
1484 local_irq_restore(flags);
1488 * rs_wait_until_sent() --- wait until the transmitter is empty
1490 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1492 struct async_struct * info = tty->driver_data;
1493 unsigned long orig_jiffies, char_time;
1496 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1499 if (info->xmit_fifo_size == 0)
1500 return; /* Just in case.... */
1502 orig_jiffies = jiffies;
1505 * Set the check interval to be 1/5 of the estimated time to
1506 * send a single character, and make it at least 1. The check
1507 * interval should also be less than the timeout.
1509 * Note: we have to use pretty tight timings here to satisfy
1512 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1513 char_time = char_time / 5;
1517 char_time = min_t(unsigned long, char_time, timeout);
1519 * If the transmitter hasn't cleared in twice the approximate
1520 * amount of time to send the entire FIFO, it probably won't
1521 * ever clear. This assumes the UART isn't doing flow
1522 * control, which is currently the case. Hence, if it ever
1523 * takes longer than info->timeout, this is probably due to a
1524 * UART bug of some kind. So, we clamp the timeout parameter at
1527 if (!timeout || timeout > 2*info->timeout)
1528 timeout = 2*info->timeout;
1529 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1530 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1531 printk("jiff=%lu...", jiffies);
1533 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1534 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1535 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1537 msleep_interruptible(jiffies_to_msecs(char_time));
1538 if (signal_pending(current))
1540 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1543 __set_current_state(TASK_RUNNING);
1545 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1546 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1551 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1553 static void rs_hangup(struct tty_struct *tty)
1555 struct async_struct * info = tty->driver_data;
1556 struct serial_state *state = info->state;
1558 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1561 state = info->state;
1563 rs_flush_buffer(tty);
1566 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1568 wake_up_interruptible(&info->open_wait);
1572 * ------------------------------------------------------------
1573 * rs_open() and friends
1574 * ------------------------------------------------------------
1576 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1577 struct async_struct *info)
1579 #ifdef DECLARE_WAITQUEUE
1580 DECLARE_WAITQUEUE(wait, current);
1582 struct wait_queue wait = { current, NULL };
1584 struct serial_state *state = info->state;
1586 int do_clocal = 0, extra_count = 0;
1587 unsigned long flags;
1590 * If the device is in the middle of being closed, then block
1591 * until it's done, and then try again.
1593 if (tty_hung_up_p(filp) ||
1594 (info->flags & ASYNC_CLOSING)) {
1595 if (info->flags & ASYNC_CLOSING)
1596 interruptible_sleep_on(&info->close_wait);
1597 #ifdef SERIAL_DO_RESTART
1598 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1599 -EAGAIN : -ERESTARTSYS);
1606 * If non-blocking mode is set, or the port is not enabled,
1607 * then make the check up front and then exit.
1609 if ((filp->f_flags & O_NONBLOCK) ||
1610 (tty->flags & (1 << TTY_IO_ERROR))) {
1611 info->flags |= ASYNC_NORMAL_ACTIVE;
1615 if (tty->termios->c_cflag & CLOCAL)
1619 * Block waiting for the carrier detect and the line to become
1620 * free (i.e., not in use by the callout). While we are in
1621 * this loop, state->count is dropped by one, so that
1622 * rs_close() knows when to free things. We restore it upon
1623 * exit, either normal or abnormal.
1626 add_wait_queue(&info->open_wait, &wait);
1627 #ifdef SERIAL_DEBUG_OPEN
1628 printk("block_til_ready before block: ttys%d, count = %d\n",
1629 state->line, state->count);
1631 local_irq_save(flags);
1632 if (!tty_hung_up_p(filp)) {
1636 local_irq_restore(flags);
1637 info->blocked_open++;
1639 local_irq_save(flags);
1640 if (tty->termios->c_cflag & CBAUD)
1641 rtsdtr_ctrl(SER_DTR|SER_RTS);
1642 local_irq_restore(flags);
1643 set_current_state(TASK_INTERRUPTIBLE);
1644 if (tty_hung_up_p(filp) ||
1645 !(info->flags & ASYNC_INITIALIZED)) {
1646 #ifdef SERIAL_DO_RESTART
1647 if (info->flags & ASYNC_HUP_NOTIFY)
1650 retval = -ERESTARTSYS;
1656 if (!(info->flags & ASYNC_CLOSING) &&
1657 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1659 if (signal_pending(current)) {
1660 retval = -ERESTARTSYS;
1663 #ifdef SERIAL_DEBUG_OPEN
1664 printk("block_til_ready blocking: ttys%d, count = %d\n",
1665 info->line, state->count);
1671 __set_current_state(TASK_RUNNING);
1672 remove_wait_queue(&info->open_wait, &wait);
1675 info->blocked_open--;
1676 #ifdef SERIAL_DEBUG_OPEN
1677 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1678 info->line, state->count);
1682 info->flags |= ASYNC_NORMAL_ACTIVE;
1686 static int get_async_struct(int line, struct async_struct **ret_info)
1688 struct async_struct *info;
1689 struct serial_state *sstate;
1691 sstate = rs_table + line;
1694 *ret_info = sstate->info;
1697 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
1702 #ifdef DECLARE_WAITQUEUE
1703 init_waitqueue_head(&info->open_wait);
1704 init_waitqueue_head(&info->close_wait);
1705 init_waitqueue_head(&info->delta_msr_wait);
1707 info->port = sstate->port;
1708 info->flags = sstate->flags;
1709 info->xmit_fifo_size = sstate->xmit_fifo_size;
1711 info->state = sstate;
1714 *ret_info = sstate->info;
1717 *ret_info = sstate->info = info;
1722 * This routine is called whenever a serial port is opened. It
1723 * enables interrupts for a serial port, linking in its async structure into
1724 * the IRQ chain. It also performs the serial-specific
1725 * initialization for the tty structure.
1727 static int rs_open(struct tty_struct *tty, struct file * filp)
1729 struct async_struct *info;
1732 retval = get_async_struct(tty->index, &info);
1736 tty->driver_data = info;
1738 if (serial_paranoia_check(info, tty->name, "rs_open"))
1741 #ifdef SERIAL_DEBUG_OPEN
1742 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1744 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1747 * If the port is the middle of closing, bail out now
1749 if (tty_hung_up_p(filp) ||
1750 (info->flags & ASYNC_CLOSING)) {
1751 if (info->flags & ASYNC_CLOSING)
1752 interruptible_sleep_on(&info->close_wait);
1753 #ifdef SERIAL_DO_RESTART
1754 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1755 -EAGAIN : -ERESTARTSYS);
1762 * Start up serial port
1764 retval = startup(info);
1769 retval = block_til_ready(tty, filp, info);
1771 #ifdef SERIAL_DEBUG_OPEN
1772 printk("rs_open returning after block_til_ready with %d\n",
1778 #ifdef SERIAL_DEBUG_OPEN
1779 printk("rs_open %s successful...", tty->name);
1785 * /proc fs routines....
1788 static inline void line_info(struct seq_file *m, struct serial_state *state)
1790 struct async_struct *info = state->info, scr_info;
1791 char stat_buf[30], control, status;
1792 unsigned long flags;
1794 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1797 * Figure out the current RS-232 lines
1800 info = &scr_info; /* This is just for serial_{in,out} */
1802 info->flags = state->flags;
1806 local_irq_save(flags);
1808 control = info ? info->MCR : status;
1809 local_irq_restore(flags);
1813 if(!(control & SER_RTS))
1814 strcat(stat_buf, "|RTS");
1815 if(!(status & SER_CTS))
1816 strcat(stat_buf, "|CTS");
1817 if(!(control & SER_DTR))
1818 strcat(stat_buf, "|DTR");
1819 if(!(status & SER_DSR))
1820 strcat(stat_buf, "|DSR");
1821 if(!(status & SER_DCD))
1822 strcat(stat_buf, "|CD");
1825 seq_printf(m, " baud:%d", state->baud_base / info->quot);
1828 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1830 if (state->icount.frame)
1831 seq_printf(m, " fe:%d", state->icount.frame);
1833 if (state->icount.parity)
1834 seq_printf(m, " pe:%d", state->icount.parity);
1836 if (state->icount.brk)
1837 seq_printf(m, " brk:%d", state->icount.brk);
1839 if (state->icount.overrun)
1840 seq_printf(m, " oe:%d", state->icount.overrun);
1843 * Last thing is the RS-232 status lines
1845 seq_printf(m, " %s\n", stat_buf+1);
1848 static int rs_proc_show(struct seq_file *m, void *v)
1850 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1851 line_info(m, &rs_table[0]);
1855 static int rs_proc_open(struct inode *inode, struct file *file)
1857 return single_open(file, rs_proc_show, NULL);
1860 static const struct file_operations rs_proc_fops = {
1861 .owner = THIS_MODULE,
1862 .open = rs_proc_open,
1864 .llseek = seq_lseek,
1865 .release = single_release,
1869 * ---------------------------------------------------------------------
1870 * rs_init() and friends
1872 * rs_init() is called at boot-time to initialize the serial driver.
1873 * ---------------------------------------------------------------------
1877 * This routine prints out the appropriate serial driver version
1878 * number, and identifies which options were configured into this
1881 static void show_serial_version(void)
1883 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1887 static const struct tty_operations serial_ops = {
1891 .put_char = rs_put_char,
1892 .flush_chars = rs_flush_chars,
1893 .write_room = rs_write_room,
1894 .chars_in_buffer = rs_chars_in_buffer,
1895 .flush_buffer = rs_flush_buffer,
1897 .throttle = rs_throttle,
1898 .unthrottle = rs_unthrottle,
1899 .set_termios = rs_set_termios,
1902 .hangup = rs_hangup,
1903 .break_ctl = rs_break,
1904 .send_xchar = rs_send_xchar,
1905 .wait_until_sent = rs_wait_until_sent,
1906 .tiocmget = rs_tiocmget,
1907 .tiocmset = rs_tiocmset,
1908 .get_icount = rs_get_icount,
1909 .proc_fops = &rs_proc_fops,
1913 * The serial driver boot-time initialization code!
1915 static int __init amiga_serial_probe(struct platform_device *pdev)
1917 unsigned long flags;
1918 struct serial_state * state;
1921 serial_driver = alloc_tty_driver(NR_PORTS);
1927 show_serial_version();
1929 /* Initialize the tty_driver structure */
1931 serial_driver->driver_name = "amiserial";
1932 serial_driver->name = "ttyS";
1933 serial_driver->major = TTY_MAJOR;
1934 serial_driver->minor_start = 64;
1935 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1936 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1937 serial_driver->init_termios = tty_std_termios;
1938 serial_driver->init_termios.c_cflag =
1939 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1940 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1941 tty_set_operations(serial_driver, &serial_ops);
1943 error = tty_register_driver(serial_driver);
1945 goto fail_put_tty_driver;
1948 state->port = (int)&custom.serdatr; /* Just to give it a value */
1950 state->custom_divisor = 0;
1951 state->close_delay = 5*HZ/10;
1952 state->closing_wait = 30*HZ;
1953 state->icount.cts = state->icount.dsr =
1954 state->icount.rng = state->icount.dcd = 0;
1955 state->icount.rx = state->icount.tx = 0;
1956 state->icount.frame = state->icount.parity = 0;
1957 state->icount.overrun = state->icount.brk = 0;
1959 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
1962 /* Hardware set up */
1964 state->baud_base = amiga_colorclock;
1965 state->xmit_fifo_size = 1;
1967 /* set ISRs, and then disable the rx interrupts */
1968 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
1970 goto fail_unregister;
1972 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
1973 "serial RX", state);
1977 local_irq_save(flags);
1979 /* turn off Rx and Tx interrupts */
1980 custom.intena = IF_RBF | IF_TBE;
1983 /* clear any pending interrupt */
1984 custom.intreq = IF_RBF | IF_TBE;
1987 local_irq_restore(flags);
1990 * set the appropriate directions for the modem control flags,
1991 * and clear RTS and DTR
1993 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
1994 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
1996 platform_set_drvdata(pdev, state);
2001 free_irq(IRQ_AMIGA_TBE, state);
2003 tty_unregister_driver(serial_driver);
2004 fail_put_tty_driver:
2005 put_tty_driver(serial_driver);
2009 static int __exit amiga_serial_remove(struct platform_device *pdev)
2012 struct serial_state *state = platform_get_drvdata(pdev);
2013 struct async_struct *info = state->info;
2015 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2016 if ((error = tty_unregister_driver(serial_driver)))
2017 printk("SERIAL: failed to unregister serial driver (%d)\n",
2019 put_tty_driver(serial_driver);
2021 rs_table[0].info = NULL;
2024 free_irq(IRQ_AMIGA_TBE, rs_table);
2025 free_irq(IRQ_AMIGA_RBF, rs_table);
2027 platform_set_drvdata(pdev, NULL);
2032 static struct platform_driver amiga_serial_driver = {
2033 .remove = __exit_p(amiga_serial_remove),
2035 .name = "amiga-serial",
2036 .owner = THIS_MODULE,
2040 static int __init amiga_serial_init(void)
2042 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
2045 module_init(amiga_serial_init);
2047 static void __exit amiga_serial_exit(void)
2049 platform_driver_unregister(&amiga_serial_driver);
2052 module_exit(amiga_serial_exit);
2055 #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2058 * ------------------------------------------------------------
2059 * Serial console driver
2060 * ------------------------------------------------------------
2063 static void amiga_serial_putc(char c)
2065 custom.serdat = (unsigned char)c | 0x100;
2066 while (!(custom.serdatr & 0x2000))
2071 * Print a string to the serial port trying not to disturb
2072 * any possible real use of the port...
2074 * The console must be locked when we get here.
2076 static void serial_console_write(struct console *co, const char *s,
2079 unsigned short intena = custom.intenar;
2081 custom.intena = IF_TBE;
2085 amiga_serial_putc('\r');
2086 amiga_serial_putc(*s++);
2089 custom.intena = IF_SETCLR | (intena & IF_TBE);
2092 static struct tty_driver *serial_console_device(struct console *c, int *index)
2095 return serial_driver;
2098 static struct console sercons = {
2100 .write = serial_console_write,
2101 .device = serial_console_device,
2102 .flags = CON_PRINTBUFFER,
2109 static int __init amiserial_console_init(void)
2111 register_console(&sercons);
2114 console_initcall(amiserial_console_init);
2116 #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2118 MODULE_LICENSE("GPL");
2119 MODULE_ALIAS("platform:amiga-serial");