2 * linux/drivers/char/amiserial.c
4 * Serial driver for the amiga builtin port.
6 * This code was created by taking serial.c version 4.30 from kernel
7 * release 2.3.22, replacing all hardware related stuff with the
8 * corresponding amiga hardware actions, and removing all irrelevant
9 * code. As a consequence, it uses many of the constants and names
10 * associated with the registers and bits of 16550 compatible UARTS -
11 * but only to keep track of status, etc in the state variables. It
12 * was done this was to make it easier to keep the code in line with
13 * (non hardware specific) changes to serial.c.
15 * The port is registered with the tty driver as minor device 64, and
16 * therefore other ports should should only use 65 upwards.
18 * Richard Lucock 28/12/99
20 * Copyright (C) 1991, 1992 Linus Torvalds
21 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
22 * 1998, 1999 Theodore Ts'o
27 * Serial driver configuration section. Here are the various options:
29 * SERIAL_PARANOIA_CHECK
30 * Check the magic number for the async_structure where
34 #include <linux/config.h>
35 #include <linux/delay.h>
37 #undef SERIAL_PARANOIA_CHECK
38 #define SERIAL_DO_RESTART
40 /* Set of debugging defines */
42 #undef SERIAL_DEBUG_INTR
43 #undef SERIAL_DEBUG_OPEN
44 #undef SERIAL_DEBUG_FLOW
45 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
51 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
52 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
53 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
59 * End of serial driver configuration section.
62 #include <linux/module.h>
64 #include <linux/types.h>
65 #include <linux/serial.h>
66 #include <linux/serialP.h>
67 #include <linux/serial_reg.h>
68 static char *serial_version = "4.30";
70 #include <linux/errno.h>
71 #include <linux/signal.h>
72 #include <linux/sched.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/console.h>
79 #include <linux/major.h>
80 #include <linux/string.h>
81 #include <linux/fcntl.h>
82 #include <linux/ptrace.h>
83 #include <linux/ioport.h>
85 #include <linux/slab.h>
86 #include <linux/init.h>
87 #include <linux/bitops.h>
89 #include <asm/setup.h>
91 #include <asm/system.h>
95 #include <asm/amigahw.h>
96 #include <asm/amigaints.h>
99 #define _INLINE_ inline
102 static char *serial_name = "Amiga-builtin serial driver";
104 static struct tty_driver *serial_driver;
106 /* number of characters left in xmit buffer before we ask for more */
107 #define WAKEUP_CHARS 256
109 static struct async_struct *IRQ_ports;
111 static unsigned char current_ctl_bits;
113 static void change_speed(struct async_struct *info, struct termios *old);
114 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
117 static struct serial_state rs_table[1];
119 #define NR_PORTS ARRAY_SIZE(rs_table)
122 * tmp_buf is used as a temporary buffer by serial_write. We need to
123 * lock it in case the copy_from_user blocks while swapping in a page,
124 * and some other program tries to do a serial write at the same time.
125 * Since the lock will only come under contention when the system is
126 * swapping and available memory is low, it makes sense to share one
127 * buffer across all the serial ports, since it significantly saves
128 * memory if large numbers of serial ports are open.
130 static unsigned char *tmp_buf;
131 static DECLARE_MUTEX(tmp_buf_sem);
133 #include <asm/uaccess.h>
135 #define serial_isroot() (capable(CAP_SYS_ADMIN))
138 static inline int serial_paranoia_check(struct async_struct *info,
139 char *name, const char *routine)
141 #ifdef SERIAL_PARANOIA_CHECK
142 static const char *badmagic =
143 "Warning: bad magic number for serial struct (%s) in %s\n";
144 static const char *badinfo =
145 "Warning: null async_struct for (%s) in %s\n";
148 printk(badinfo, name, routine);
151 if (info->magic != SERIAL_MAGIC) {
152 printk(badmagic, name, routine);
159 /* some serial hardware definitions */
160 #define SDR_OVRUN (1<<15)
161 #define SDR_RBF (1<<14)
162 #define SDR_TBE (1<<13)
163 #define SDR_TSRE (1<<12)
165 #define SERPER_PARENB (1<<15)
167 #define AC_SETCLR (1<<15)
168 #define AC_UARTBRK (1<<11)
170 #define SER_DTR (1<<7)
171 #define SER_RTS (1<<6)
172 #define SER_DCD (1<<5)
173 #define SER_CTS (1<<4)
174 #define SER_DSR (1<<3)
176 static __inline__ void rtsdtr_ctrl(int bits)
178 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
182 * ------------------------------------------------------------
183 * rs_stop() and rs_start()
185 * This routines are called before setting or resetting tty->stopped.
186 * They enable or disable transmitter interrupts, as necessary.
187 * ------------------------------------------------------------
189 static void rs_stop(struct tty_struct *tty)
191 struct async_struct *info = (struct async_struct *)tty->driver_data;
194 if (serial_paranoia_check(info, tty->name, "rs_stop"))
197 local_irq_save(flags);
198 if (info->IER & UART_IER_THRI) {
199 info->IER &= ~UART_IER_THRI;
200 /* disable Tx interrupt and remove any pending interrupts */
201 custom.intena = IF_TBE;
203 custom.intreq = IF_TBE;
206 local_irq_restore(flags);
209 static void rs_start(struct tty_struct *tty)
211 struct async_struct *info = (struct async_struct *)tty->driver_data;
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
217 local_irq_save(flags);
218 if (info->xmit.head != info->xmit.tail
220 && !(info->IER & UART_IER_THRI)) {
221 info->IER |= UART_IER_THRI;
222 custom.intena = IF_SETCLR | IF_TBE;
224 /* set a pending Tx Interrupt, transmitter should restart now */
225 custom.intreq = IF_SETCLR | IF_TBE;
228 local_irq_restore(flags);
232 * ----------------------------------------------------------------------
234 * Here starts the interrupt handling routines. All of the following
235 * subroutines are declared as inline and are folded into
236 * rs_interrupt(). They were separated out for readability's sake.
238 * Note: rs_interrupt() is a "fast" interrupt, which means that it
239 * runs with interrupts turned off. People who may want to modify
240 * rs_interrupt() should try to keep the interrupt handler as fast as
241 * possible. After you are done making modifications, it is not a bad
244 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
246 * and look at the resulting assemble code in serial.s.
248 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
249 * -----------------------------------------------------------------------
253 * This routine is used by the interrupt handler to schedule
254 * processing in the software interrupt portion of the driver.
256 static _INLINE_ void rs_sched_event(struct async_struct *info,
259 info->event |= 1 << event;
260 tasklet_schedule(&info->tlet);
263 static _INLINE_ void receive_chars(struct async_struct *info)
267 struct tty_struct *tty = info->tty;
268 unsigned char ch, flag;
269 struct async_icount *icount;
272 icount = &info->state->icount;
274 status = UART_LSR_DR; /* We obviously have a character! */
275 serdatr = custom.serdatr;
277 custom.intreq = IF_RBF;
280 if((serdatr & 0x1ff) == 0)
281 status |= UART_LSR_BI;
282 if(serdatr & SDR_OVRUN)
283 status |= UART_LSR_OE;
288 #ifdef SERIAL_DEBUG_INTR
289 printk("DR%02x:%02x...", ch, status);
294 * We don't handle parity or frame errors - but I have left
295 * the code in, since I'm not sure that the errors can't be
299 if (status & (UART_LSR_BI | UART_LSR_PE |
300 UART_LSR_FE | UART_LSR_OE)) {
302 * For statistics only
304 if (status & UART_LSR_BI) {
305 status &= ~(UART_LSR_FE | UART_LSR_PE);
307 } else if (status & UART_LSR_PE)
309 else if (status & UART_LSR_FE)
311 if (status & UART_LSR_OE)
315 * Now check to see if character should be
316 * ignored, and mask off conditions which
319 if (status & info->ignore_status_mask)
322 status &= info->read_status_mask;
324 if (status & (UART_LSR_BI)) {
325 #ifdef SERIAL_DEBUG_INTR
326 printk("handling break....");
329 if (info->flags & ASYNC_SAK)
331 } else if (status & UART_LSR_PE)
333 else if (status & UART_LSR_FE)
335 if (status & UART_LSR_OE) {
337 * Overrun is special, since it's
338 * reported immediately, and doesn't
339 * affect the current character
344 tty_insert_flip_char(tty, ch, flag);
346 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
347 tty_flip_buffer_push(tty);
352 static _INLINE_ void transmit_chars(struct async_struct *info)
354 custom.intreq = IF_TBE;
357 custom.serdat = info->x_char | 0x100;
359 info->state->icount.tx++;
363 if (info->xmit.head == info->xmit.tail
364 || info->tty->stopped
365 || info->tty->hw_stopped) {
366 info->IER &= ~UART_IER_THRI;
367 custom.intena = IF_TBE;
372 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
374 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
375 info->state->icount.tx++;
377 if (CIRC_CNT(info->xmit.head,
379 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
380 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
382 #ifdef SERIAL_DEBUG_INTR
385 if (info->xmit.head == info->xmit.tail) {
386 custom.intena = IF_TBE;
388 info->IER &= ~UART_IER_THRI;
392 static _INLINE_ void check_modem_status(struct async_struct *info)
394 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
395 unsigned char dstatus;
396 struct async_icount *icount;
398 /* Determine bits that have changed */
399 dstatus = status ^ current_ctl_bits;
400 current_ctl_bits = status;
403 icount = &info->state->icount;
404 /* update input line counters */
405 if (dstatus & SER_DSR)
407 if (dstatus & SER_DCD) {
409 #ifdef CONFIG_HARD_PPS
410 if ((info->flags & ASYNC_HARDPPS_CD) &&
415 if (dstatus & SER_CTS)
417 wake_up_interruptible(&info->delta_msr_wait);
420 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
421 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
422 printk("ttyS%d CD now %s...", info->line,
423 (!(status & SER_DCD)) ? "on" : "off");
425 if (!(status & SER_DCD))
426 wake_up_interruptible(&info->open_wait);
428 #ifdef SERIAL_DEBUG_OPEN
429 printk("doing serial hangup...");
432 tty_hangup(info->tty);
435 if (info->flags & ASYNC_CTS_FLOW) {
436 if (info->tty->hw_stopped) {
437 if (!(status & SER_CTS)) {
438 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
439 printk("CTS tx start...");
441 info->tty->hw_stopped = 0;
442 info->IER |= UART_IER_THRI;
443 custom.intena = IF_SETCLR | IF_TBE;
445 /* set a pending Tx Interrupt, transmitter should restart now */
446 custom.intreq = IF_SETCLR | IF_TBE;
448 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
452 if ((status & SER_CTS)) {
453 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
454 printk("CTS tx stop...");
456 info->tty->hw_stopped = 1;
457 info->IER &= ~UART_IER_THRI;
458 /* disable Tx interrupt and remove any pending interrupts */
459 custom.intena = IF_TBE;
461 custom.intreq = IF_TBE;
468 static irqreturn_t ser_vbl_int( int irq, void *data, struct pt_regs *regs)
470 /* vbl is just a periodic interrupt we tie into to update modem status */
471 struct async_struct * info = IRQ_ports;
473 * TBD - is it better to unregister from this interrupt or to
474 * ignore it if MSI is clear ?
476 if(info->IER & UART_IER_MSI)
477 check_modem_status(info);
481 static irqreturn_t ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
483 struct async_struct * info;
485 #ifdef SERIAL_DEBUG_INTR
486 printk("ser_rx_int...");
490 if (!info || !info->tty)
494 info->last_active = jiffies;
495 #ifdef SERIAL_DEBUG_INTR
501 static irqreturn_t ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
503 struct async_struct * info;
505 if (custom.serdatr & SDR_TBE) {
506 #ifdef SERIAL_DEBUG_INTR
507 printk("ser_tx_int...");
511 if (!info || !info->tty)
514 transmit_chars(info);
515 info->last_active = jiffies;
516 #ifdef SERIAL_DEBUG_INTR
524 * -------------------------------------------------------------------
525 * Here ends the serial interrupt routines.
526 * -------------------------------------------------------------------
530 * This routine is used to handle the "bottom half" processing for the
531 * serial driver, known also the "software interrupt" processing.
532 * This processing is done at the kernel interrupt level, after the
533 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
534 * is where time-consuming activities which can not be done in the
535 * interrupt driver proper are done; the interrupt driver schedules
536 * them using rs_sched_event(), and they get done here.
539 static void do_softint(unsigned long private_)
541 struct async_struct *info = (struct async_struct *) private_;
542 struct tty_struct *tty;
548 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
550 wake_up_interruptible(&tty->write_wait);
555 * ---------------------------------------------------------------
556 * Low level utility subroutines for the serial driver: routines to
557 * figure out the appropriate timeout for an interrupt chain, routines
558 * to initialize and startup a serial port, and routines to shutdown a
559 * serial port. Useful stuff like that.
560 * ---------------------------------------------------------------
563 static int startup(struct async_struct * info)
569 page = get_zeroed_page(GFP_KERNEL);
573 local_irq_save(flags);
575 if (info->flags & ASYNC_INITIALIZED) {
583 info->xmit.buf = (unsigned char *) page;
585 #ifdef SERIAL_DEBUG_OPEN
586 printk("starting up ttys%d ...", info->line);
589 /* Clear anything in the input buffer */
591 custom.intreq = IF_RBF;
594 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
596 if (serial_isroot()) {
598 set_bit(TTY_IO_ERROR,
605 /* enable both Rx and Tx interrupts */
606 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
608 info->IER = UART_IER_MSI;
610 /* remember current state of the DCD and CTS bits */
611 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
616 if (info->tty->termios->c_cflag & CBAUD)
617 info->MCR = SER_DTR | SER_RTS;
618 rtsdtr_ctrl(info->MCR);
621 clear_bit(TTY_IO_ERROR, &info->tty->flags);
622 info->xmit.head = info->xmit.tail = 0;
625 * Set up the tty->alt_speed kludge
628 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
629 info->tty->alt_speed = 57600;
630 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
631 info->tty->alt_speed = 115200;
632 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
633 info->tty->alt_speed = 230400;
634 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
635 info->tty->alt_speed = 460800;
639 * and set the speed of the serial port
641 change_speed(info, NULL);
643 info->flags |= ASYNC_INITIALIZED;
644 local_irq_restore(flags);
648 local_irq_restore(flags);
653 * This routine will shutdown a serial port; interrupts are disabled, and
654 * DTR is dropped if the hangup on close termio flag is on.
656 static void shutdown(struct async_struct * info)
659 struct serial_state *state;
661 if (!(info->flags & ASYNC_INITIALIZED))
666 #ifdef SERIAL_DEBUG_OPEN
667 printk("Shutting down serial port %d ....\n", info->line);
670 local_irq_save(flags); /* Disable interrupts */
673 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
674 * here so the queue might never be waken up
676 wake_up_interruptible(&info->delta_msr_wait);
681 * Free the IRQ, if necessary
683 free_irq(IRQ_AMIGA_VERTB, info);
685 if (info->xmit.buf) {
686 free_page((unsigned long) info->xmit.buf);
687 info->xmit.buf = NULL;
691 custom.intena = IF_RBF | IF_TBE;
694 /* disable break condition */
695 custom.adkcon = AC_UARTBRK;
698 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
699 info->MCR &= ~(SER_DTR|SER_RTS);
700 rtsdtr_ctrl(info->MCR);
703 set_bit(TTY_IO_ERROR, &info->tty->flags);
705 info->flags &= ~ASYNC_INITIALIZED;
706 local_irq_restore(flags);
711 * This routine is called to set the UART divisor registers to match
712 * the specified baud rate for a serial port.
714 static void change_speed(struct async_struct *info,
715 struct termios *old_termios)
717 int quot = 0, baud_base, baud;
718 unsigned cflag, cval = 0;
722 if (!info->tty || !info->tty->termios)
724 cflag = info->tty->termios->c_cflag;
726 /* Byte size is always 8 bits plus parity bit if requested */
729 if (cflag & CSTOPB) {
733 if (cflag & PARENB) {
734 cval |= UART_LCR_PARITY;
737 if (!(cflag & PARODD))
738 cval |= UART_LCR_EPAR;
741 cval |= UART_LCR_SPAR;
744 /* Determine divisor based on baud rate */
745 baud = tty_get_baud_rate(info->tty);
747 baud = 9600; /* B0 transition handled in rs_set_termios */
748 baud_base = info->state->baud_base;
750 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
751 quot = info->state->custom_divisor;
754 /* Special case since 134 is really 134.5 */
755 quot = (2*baud_base / 269);
757 quot = baud_base / baud;
759 /* If the quotient is zero refuse the change */
760 if (!quot && old_termios) {
761 info->tty->termios->c_cflag &= ~CBAUD;
762 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
763 baud = tty_get_baud_rate(info->tty);
767 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
768 quot = info->state->custom_divisor;
771 /* Special case since 134 is really 134.5 */
772 quot = (2*baud_base / 269);
774 quot = baud_base / baud;
777 /* As a last resort, if the quotient is zero, default to 9600 bps */
779 quot = baud_base / 9600;
781 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
782 info->timeout += HZ/50; /* Add .02 seconds of slop */
784 /* CTS flow control flag and modem status interrupts */
785 info->IER &= ~UART_IER_MSI;
786 if (info->flags & ASYNC_HARDPPS_CD)
787 info->IER |= UART_IER_MSI;
788 if (cflag & CRTSCTS) {
789 info->flags |= ASYNC_CTS_FLOW;
790 info->IER |= UART_IER_MSI;
792 info->flags &= ~ASYNC_CTS_FLOW;
794 info->flags &= ~ASYNC_CHECK_CD;
796 info->flags |= ASYNC_CHECK_CD;
797 info->IER |= UART_IER_MSI;
800 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
804 * Set up parity check flag
806 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
808 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
809 if (I_INPCK(info->tty))
810 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
811 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
812 info->read_status_mask |= UART_LSR_BI;
815 * Characters to ignore
817 info->ignore_status_mask = 0;
818 if (I_IGNPAR(info->tty))
819 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
820 if (I_IGNBRK(info->tty)) {
821 info->ignore_status_mask |= UART_LSR_BI;
823 * If we're ignore parity and break indicators, ignore
824 * overruns too. (For real raw support).
826 if (I_IGNPAR(info->tty))
827 info->ignore_status_mask |= UART_LSR_OE;
830 * !!! ignore all characters if CREAD is not set
832 if ((cflag & CREAD) == 0)
833 info->ignore_status_mask |= UART_LSR_DR;
834 local_irq_save(flags);
839 /* Set up the baud rate */
842 /* Enable or disable parity bit */
844 if(cval & UART_LCR_PARITY)
845 serper |= (SERPER_PARENB);
847 custom.serper = serper;
851 info->LCR = cval; /* Save LCR */
852 local_irq_restore(flags);
855 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
857 struct async_struct *info;
863 info = tty->driver_data;
865 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
871 local_irq_save(flags);
872 if (CIRC_SPACE(info->xmit.head,
874 SERIAL_XMIT_SIZE) == 0) {
875 local_irq_restore(flags);
879 info->xmit.buf[info->xmit.head++] = ch;
880 info->xmit.head &= SERIAL_XMIT_SIZE-1;
881 local_irq_restore(flags);
884 static void rs_flush_chars(struct tty_struct *tty)
886 struct async_struct *info = (struct async_struct *)tty->driver_data;
889 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
892 if (info->xmit.head == info->xmit.tail
898 local_irq_save(flags);
899 info->IER |= UART_IER_THRI;
900 custom.intena = IF_SETCLR | IF_TBE;
902 /* set a pending Tx Interrupt, transmitter should restart now */
903 custom.intreq = IF_SETCLR | IF_TBE;
905 local_irq_restore(flags);
908 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
911 struct async_struct *info;
917 info = tty->driver_data;
919 if (serial_paranoia_check(info, tty->name, "rs_write"))
922 if (!info->xmit.buf || !tmp_buf)
925 local_save_flags(flags);
928 c = CIRC_SPACE_TO_END(info->xmit.head,
936 memcpy(info->xmit.buf + info->xmit.head, buf, c);
937 info->xmit.head = ((info->xmit.head + c) &
938 (SERIAL_XMIT_SIZE-1));
943 local_irq_restore(flags);
945 if (info->xmit.head != info->xmit.tail
948 && !(info->IER & UART_IER_THRI)) {
949 info->IER |= UART_IER_THRI;
951 custom.intena = IF_SETCLR | IF_TBE;
953 /* set a pending Tx Interrupt, transmitter should restart now */
954 custom.intreq = IF_SETCLR | IF_TBE;
956 local_irq_restore(flags);
961 static int rs_write_room(struct tty_struct *tty)
963 struct async_struct *info = (struct async_struct *)tty->driver_data;
965 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
967 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
970 static int rs_chars_in_buffer(struct tty_struct *tty)
972 struct async_struct *info = (struct async_struct *)tty->driver_data;
974 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
976 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
979 static void rs_flush_buffer(struct tty_struct *tty)
981 struct async_struct *info = (struct async_struct *)tty->driver_data;
984 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
986 local_irq_save(flags);
987 info->xmit.head = info->xmit.tail = 0;
988 local_irq_restore(flags);
989 wake_up_interruptible(&tty->write_wait);
994 * This function is used to send a high-priority XON/XOFF character to
997 static void rs_send_xchar(struct tty_struct *tty, char ch)
999 struct async_struct *info = (struct async_struct *)tty->driver_data;
1000 unsigned long flags;
1002 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1007 /* Make sure transmit interrupts are on */
1010 local_irq_save(flags);
1011 if(!(custom.intenar & IF_TBE)) {
1012 custom.intena = IF_SETCLR | IF_TBE;
1014 /* set a pending Tx Interrupt, transmitter should restart now */
1015 custom.intreq = IF_SETCLR | IF_TBE;
1018 local_irq_restore(flags);
1020 info->IER |= UART_IER_THRI;
1025 * ------------------------------------------------------------
1028 * This routine is called by the upper-layer tty layer to signal that
1029 * incoming characters should be throttled.
1030 * ------------------------------------------------------------
1032 static void rs_throttle(struct tty_struct * tty)
1034 struct async_struct *info = (struct async_struct *)tty->driver_data;
1035 unsigned long flags;
1036 #ifdef SERIAL_DEBUG_THROTTLE
1039 printk("throttle %s: %d....\n", tty_name(tty, buf),
1040 tty->ldisc.chars_in_buffer(tty));
1043 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1047 rs_send_xchar(tty, STOP_CHAR(tty));
1049 if (tty->termios->c_cflag & CRTSCTS)
1050 info->MCR &= ~SER_RTS;
1052 local_irq_save(flags);
1053 rtsdtr_ctrl(info->MCR);
1054 local_irq_restore(flags);
1057 static void rs_unthrottle(struct tty_struct * tty)
1059 struct async_struct *info = (struct async_struct *)tty->driver_data;
1060 unsigned long flags;
1061 #ifdef SERIAL_DEBUG_THROTTLE
1064 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1065 tty->ldisc.chars_in_buffer(tty));
1068 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1075 rs_send_xchar(tty, START_CHAR(tty));
1077 if (tty->termios->c_cflag & CRTSCTS)
1078 info->MCR |= SER_RTS;
1079 local_irq_save(flags);
1080 rtsdtr_ctrl(info->MCR);
1081 local_irq_restore(flags);
1085 * ------------------------------------------------------------
1086 * rs_ioctl() and friends
1087 * ------------------------------------------------------------
1090 static int get_serial_info(struct async_struct * info,
1091 struct serial_struct * retinfo)
1093 struct serial_struct tmp;
1094 struct serial_state *state = info->state;
1098 memset(&tmp, 0, sizeof(tmp));
1099 tmp.type = state->type;
1100 tmp.line = state->line;
1101 tmp.port = state->port;
1102 tmp.irq = state->irq;
1103 tmp.flags = state->flags;
1104 tmp.xmit_fifo_size = state->xmit_fifo_size;
1105 tmp.baud_base = state->baud_base;
1106 tmp.close_delay = state->close_delay;
1107 tmp.closing_wait = state->closing_wait;
1108 tmp.custom_divisor = state->custom_divisor;
1109 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1114 static int set_serial_info(struct async_struct * info,
1115 struct serial_struct * new_info)
1117 struct serial_struct new_serial;
1118 struct serial_state old_state, *state;
1119 unsigned int change_irq,change_port;
1122 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1124 state = info->state;
1127 change_irq = new_serial.irq != state->irq;
1128 change_port = (new_serial.port != state->port);
1129 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1132 if (!serial_isroot()) {
1133 if ((new_serial.baud_base != state->baud_base) ||
1134 (new_serial.close_delay != state->close_delay) ||
1135 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1136 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1137 (state->flags & ~ASYNC_USR_MASK)))
1139 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1140 (new_serial.flags & ASYNC_USR_MASK));
1141 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1142 (new_serial.flags & ASYNC_USR_MASK));
1143 state->custom_divisor = new_serial.custom_divisor;
1144 goto check_and_exit;
1147 if (new_serial.baud_base < 9600)
1151 * OK, past this point, all the error checking has been done.
1152 * At this point, we start making changes.....
1155 state->baud_base = new_serial.baud_base;
1156 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1157 (new_serial.flags & ASYNC_FLAGS));
1158 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1159 (info->flags & ASYNC_INTERNAL_FLAGS));
1160 state->custom_divisor = new_serial.custom_divisor;
1161 state->close_delay = new_serial.close_delay * HZ/100;
1162 state->closing_wait = new_serial.closing_wait * HZ/100;
1163 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1166 if (info->flags & ASYNC_INITIALIZED) {
1167 if (((old_state.flags & ASYNC_SPD_MASK) !=
1168 (state->flags & ASYNC_SPD_MASK)) ||
1169 (old_state.custom_divisor != state->custom_divisor)) {
1170 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1171 info->tty->alt_speed = 57600;
1172 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1173 info->tty->alt_speed = 115200;
1174 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1175 info->tty->alt_speed = 230400;
1176 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1177 info->tty->alt_speed = 460800;
1178 change_speed(info, NULL);
1181 retval = startup(info);
1187 * get_lsr_info - get line status register info
1189 * Purpose: Let user call ioctl() to get info when the UART physically
1190 * is emptied. On bus types like RS485, the transmitter must
1191 * release the bus after transmitting. This must be done when
1192 * the transmit shift register is empty, not be done when the
1193 * transmit holding register is empty. This functionality
1194 * allows an RS485 driver to be written in user space.
1196 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1198 unsigned char status;
1199 unsigned int result;
1200 unsigned long flags;
1202 local_irq_save(flags);
1203 status = custom.serdatr;
1205 local_irq_restore(flags);
1206 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1207 if (copy_to_user(value, &result, sizeof(int)))
1213 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1215 struct async_struct * info = (struct async_struct *)tty->driver_data;
1216 unsigned char control, status;
1217 unsigned long flags;
1219 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1221 if (tty->flags & (1 << TTY_IO_ERROR))
1224 control = info->MCR;
1225 local_irq_save(flags);
1227 local_irq_restore(flags);
1228 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1229 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1230 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1231 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1232 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1235 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1236 unsigned int set, unsigned int clear)
1238 struct async_struct * info = (struct async_struct *)tty->driver_data;
1239 unsigned long flags;
1241 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1243 if (tty->flags & (1 << TTY_IO_ERROR))
1246 local_irq_save(flags);
1247 if (set & TIOCM_RTS)
1248 info->MCR |= SER_RTS;
1249 if (set & TIOCM_DTR)
1250 info->MCR |= SER_DTR;
1251 if (clear & TIOCM_RTS)
1252 info->MCR &= ~SER_RTS;
1253 if (clear & TIOCM_DTR)
1254 info->MCR &= ~SER_DTR;
1255 rtsdtr_ctrl(info->MCR);
1256 local_irq_restore(flags);
1261 * rs_break() --- routine which turns the break handling on or off
1263 static void rs_break(struct tty_struct *tty, int break_state)
1265 struct async_struct * info = (struct async_struct *)tty->driver_data;
1266 unsigned long flags;
1268 if (serial_paranoia_check(info, tty->name, "rs_break"))
1271 local_irq_save(flags);
1272 if (break_state == -1)
1273 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1275 custom.adkcon = AC_UARTBRK;
1277 local_irq_restore(flags);
1281 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1282 unsigned int cmd, unsigned long arg)
1284 struct async_struct * info = (struct async_struct *)tty->driver_data;
1285 struct async_icount cprev, cnow; /* kernel counter temps */
1286 struct serial_icounter_struct icount;
1287 unsigned long flags;
1289 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1292 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1293 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1294 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1295 if (tty->flags & (1 << TTY_IO_ERROR))
1301 return get_serial_info(info,
1302 (struct serial_struct *) arg);
1304 return set_serial_info(info,
1305 (struct serial_struct *) arg);
1309 case TIOCSERGETLSR: /* Get line status register */
1310 return get_lsr_info(info, (unsigned int *) arg);
1312 case TIOCSERGSTRUCT:
1313 if (copy_to_user((struct async_struct *) arg,
1314 info, sizeof(struct async_struct)))
1319 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1320 * - mask passed in arg for lines of interest
1321 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1322 * Caller should use TIOCGICOUNT to see which one it was
1325 local_irq_save(flags);
1326 /* note the counters on entry */
1327 cprev = info->state->icount;
1328 local_irq_restore(flags);
1330 interruptible_sleep_on(&info->delta_msr_wait);
1331 /* see if a signal did it */
1332 if (signal_pending(current))
1333 return -ERESTARTSYS;
1334 local_irq_save(flags);
1335 cnow = info->state->icount; /* atomic copy */
1336 local_irq_restore(flags);
1337 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1338 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1339 return -EIO; /* no change => error */
1340 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1341 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1342 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1343 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1351 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1352 * Return: write counters to the user passed counter struct
1353 * NB: both 1->0 and 0->1 transitions are counted except for
1354 * RI where only 0->1 is counted.
1357 local_irq_save(flags);
1358 cnow = info->state->icount;
1359 local_irq_restore(flags);
1360 icount.cts = cnow.cts;
1361 icount.dsr = cnow.dsr;
1362 icount.rng = cnow.rng;
1363 icount.dcd = cnow.dcd;
1364 icount.rx = cnow.rx;
1365 icount.tx = cnow.tx;
1366 icount.frame = cnow.frame;
1367 icount.overrun = cnow.overrun;
1368 icount.parity = cnow.parity;
1369 icount.brk = cnow.brk;
1370 icount.buf_overrun = cnow.buf_overrun;
1372 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1377 /* "setserial -W" is called in Debian boot */
1378 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1382 return -ENOIOCTLCMD;
1387 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1389 struct async_struct *info = (struct async_struct *)tty->driver_data;
1390 unsigned long flags;
1391 unsigned int cflag = tty->termios->c_cflag;
1393 if ( (cflag == old_termios->c_cflag)
1394 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1395 == RELEVANT_IFLAG(old_termios->c_iflag)))
1398 change_speed(info, old_termios);
1400 /* Handle transition to B0 status */
1401 if ((old_termios->c_cflag & CBAUD) &&
1403 info->MCR &= ~(SER_DTR|SER_RTS);
1404 local_irq_save(flags);
1405 rtsdtr_ctrl(info->MCR);
1406 local_irq_restore(flags);
1409 /* Handle transition away from B0 status */
1410 if (!(old_termios->c_cflag & CBAUD) &&
1412 info->MCR |= SER_DTR;
1413 if (!(tty->termios->c_cflag & CRTSCTS) ||
1414 !test_bit(TTY_THROTTLED, &tty->flags)) {
1415 info->MCR |= SER_RTS;
1417 local_irq_save(flags);
1418 rtsdtr_ctrl(info->MCR);
1419 local_irq_restore(flags);
1422 /* Handle turning off CRTSCTS */
1423 if ((old_termios->c_cflag & CRTSCTS) &&
1424 !(tty->termios->c_cflag & CRTSCTS)) {
1425 tty->hw_stopped = 0;
1431 * No need to wake up processes in open wait, since they
1432 * sample the CLOCAL flag once, and don't recheck it.
1433 * XXX It's not clear whether the current behavior is correct
1434 * or not. Hence, this may change.....
1436 if (!(old_termios->c_cflag & CLOCAL) &&
1437 (tty->termios->c_cflag & CLOCAL))
1438 wake_up_interruptible(&info->open_wait);
1443 * ------------------------------------------------------------
1446 * This routine is called when the serial port gets closed. First, we
1447 * wait for the last remaining data to be sent. Then, we unlink its
1448 * async structure from the interrupt chain if necessary, and we free
1449 * that IRQ if nothing is left in the chain.
1450 * ------------------------------------------------------------
1452 static void rs_close(struct tty_struct *tty, struct file * filp)
1454 struct async_struct * info = (struct async_struct *)tty->driver_data;
1455 struct serial_state *state;
1456 unsigned long flags;
1458 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1461 state = info->state;
1463 local_irq_save(flags);
1465 if (tty_hung_up_p(filp)) {
1466 DBG_CNT("before DEC-hung");
1467 local_irq_restore(flags);
1471 #ifdef SERIAL_DEBUG_OPEN
1472 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1474 if ((tty->count == 1) && (state->count != 1)) {
1476 * Uh, oh. tty->count is 1, which means that the tty
1477 * structure will be freed. state->count should always
1478 * be one in these conditions. If it's greater than
1479 * one, we've got real problems, since it means the
1480 * serial port won't be shutdown.
1482 printk("rs_close: bad serial port count; tty->count is 1, "
1483 "state->count is %d\n", state->count);
1486 if (--state->count < 0) {
1487 printk("rs_close: bad serial port count for ttys%d: %d\n",
1488 info->line, state->count);
1492 DBG_CNT("before DEC-2");
1493 local_irq_restore(flags);
1496 info->flags |= ASYNC_CLOSING;
1498 * Now we wait for the transmit buffer to clear; and we notify
1499 * the line discipline to only process XON/XOFF characters.
1502 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1503 tty_wait_until_sent(tty, info->closing_wait);
1505 * At this point we stop accepting input. To do this, we
1506 * disable the receive line status interrupts, and tell the
1507 * interrupt driver to stop checking the data ready bit in the
1508 * line status register.
1510 info->read_status_mask &= ~UART_LSR_DR;
1511 if (info->flags & ASYNC_INITIALIZED) {
1512 /* disable receive interrupts */
1513 custom.intena = IF_RBF;
1515 /* clear any pending receive interrupt */
1516 custom.intreq = IF_RBF;
1520 * Before we drop DTR, make sure the UART transmitter
1521 * has completely drained; this is especially
1522 * important if there is a transmit FIFO!
1524 rs_wait_until_sent(tty, info->timeout);
1527 if (tty->driver->flush_buffer)
1528 tty->driver->flush_buffer(tty);
1530 tty_ldisc_flush(tty);
1534 if (info->blocked_open) {
1535 if (info->close_delay) {
1536 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1538 wake_up_interruptible(&info->open_wait);
1540 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1541 wake_up_interruptible(&info->close_wait);
1542 local_irq_restore(flags);
1546 * rs_wait_until_sent() --- wait until the transmitter is empty
1548 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1550 struct async_struct * info = (struct async_struct *)tty->driver_data;
1551 unsigned long orig_jiffies, char_time;
1554 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1557 if (info->xmit_fifo_size == 0)
1558 return; /* Just in case.... */
1560 orig_jiffies = jiffies;
1562 * Set the check interval to be 1/5 of the estimated time to
1563 * send a single character, and make it at least 1. The check
1564 * interval should also be less than the timeout.
1566 * Note: we have to use pretty tight timings here to satisfy
1569 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1570 char_time = char_time / 5;
1574 char_time = min_t(unsigned long, char_time, timeout);
1576 * If the transmitter hasn't cleared in twice the approximate
1577 * amount of time to send the entire FIFO, it probably won't
1578 * ever clear. This assumes the UART isn't doing flow
1579 * control, which is currently the case. Hence, if it ever
1580 * takes longer than info->timeout, this is probably due to a
1581 * UART bug of some kind. So, we clamp the timeout parameter at
1584 if (!timeout || timeout > 2*info->timeout)
1585 timeout = 2*info->timeout;
1586 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1587 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1588 printk("jiff=%lu...", jiffies);
1590 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1591 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1592 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1594 msleep_interruptible(jiffies_to_msecs(char_time));
1595 if (signal_pending(current))
1597 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1600 current->state = TASK_RUNNING;
1601 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1602 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1607 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1609 static void rs_hangup(struct tty_struct *tty)
1611 struct async_struct * info = (struct async_struct *)tty->driver_data;
1612 struct serial_state *state = info->state;
1614 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1617 state = info->state;
1619 rs_flush_buffer(tty);
1623 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1625 wake_up_interruptible(&info->open_wait);
1629 * ------------------------------------------------------------
1630 * rs_open() and friends
1631 * ------------------------------------------------------------
1633 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1634 struct async_struct *info)
1636 #ifdef DECLARE_WAITQUEUE
1637 DECLARE_WAITQUEUE(wait, current);
1639 struct wait_queue wait = { current, NULL };
1641 struct serial_state *state = info->state;
1643 int do_clocal = 0, extra_count = 0;
1644 unsigned long flags;
1647 * If the device is in the middle of being closed, then block
1648 * until it's done, and then try again.
1650 if (tty_hung_up_p(filp) ||
1651 (info->flags & ASYNC_CLOSING)) {
1652 if (info->flags & ASYNC_CLOSING)
1653 interruptible_sleep_on(&info->close_wait);
1654 #ifdef SERIAL_DO_RESTART
1655 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1656 -EAGAIN : -ERESTARTSYS);
1663 * If non-blocking mode is set, or the port is not enabled,
1664 * then make the check up front and then exit.
1666 if ((filp->f_flags & O_NONBLOCK) ||
1667 (tty->flags & (1 << TTY_IO_ERROR))) {
1668 info->flags |= ASYNC_NORMAL_ACTIVE;
1672 if (tty->termios->c_cflag & CLOCAL)
1676 * Block waiting for the carrier detect and the line to become
1677 * free (i.e., not in use by the callout). While we are in
1678 * this loop, state->count is dropped by one, so that
1679 * rs_close() knows when to free things. We restore it upon
1680 * exit, either normal or abnormal.
1683 add_wait_queue(&info->open_wait, &wait);
1684 #ifdef SERIAL_DEBUG_OPEN
1685 printk("block_til_ready before block: ttys%d, count = %d\n",
1686 state->line, state->count);
1688 local_irq_save(flags);
1689 if (!tty_hung_up_p(filp)) {
1693 local_irq_restore(flags);
1694 info->blocked_open++;
1696 local_irq_save(flags);
1697 if (tty->termios->c_cflag & CBAUD)
1698 rtsdtr_ctrl(SER_DTR|SER_RTS);
1699 local_irq_restore(flags);
1700 set_current_state(TASK_INTERRUPTIBLE);
1701 if (tty_hung_up_p(filp) ||
1702 !(info->flags & ASYNC_INITIALIZED)) {
1703 #ifdef SERIAL_DO_RESTART
1704 if (info->flags & ASYNC_HUP_NOTIFY)
1707 retval = -ERESTARTSYS;
1713 if (!(info->flags & ASYNC_CLOSING) &&
1714 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1716 if (signal_pending(current)) {
1717 retval = -ERESTARTSYS;
1720 #ifdef SERIAL_DEBUG_OPEN
1721 printk("block_til_ready blocking: ttys%d, count = %d\n",
1722 info->line, state->count);
1726 current->state = TASK_RUNNING;
1727 remove_wait_queue(&info->open_wait, &wait);
1730 info->blocked_open--;
1731 #ifdef SERIAL_DEBUG_OPEN
1732 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1733 info->line, state->count);
1737 info->flags |= ASYNC_NORMAL_ACTIVE;
1741 static int get_async_struct(int line, struct async_struct **ret_info)
1743 struct async_struct *info;
1744 struct serial_state *sstate;
1746 sstate = rs_table + line;
1749 *ret_info = sstate->info;
1752 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1757 memset(info, 0, sizeof(struct async_struct));
1758 #ifdef DECLARE_WAITQUEUE
1759 init_waitqueue_head(&info->open_wait);
1760 init_waitqueue_head(&info->close_wait);
1761 init_waitqueue_head(&info->delta_msr_wait);
1763 info->magic = SERIAL_MAGIC;
1764 info->port = sstate->port;
1765 info->flags = sstate->flags;
1766 info->xmit_fifo_size = sstate->xmit_fifo_size;
1768 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1769 info->state = sstate;
1772 *ret_info = sstate->info;
1775 *ret_info = sstate->info = info;
1780 * This routine is called whenever a serial port is opened. It
1781 * enables interrupts for a serial port, linking in its async structure into
1782 * the IRQ chain. It also performs the serial-specific
1783 * initialization for the tty structure.
1785 static int rs_open(struct tty_struct *tty, struct file * filp)
1787 struct async_struct *info;
1792 if ((line < 0) || (line >= NR_PORTS)) {
1795 retval = get_async_struct(line, &info);
1799 tty->driver_data = info;
1801 if (serial_paranoia_check(info, tty->name, "rs_open"))
1804 #ifdef SERIAL_DEBUG_OPEN
1805 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1807 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1810 page = get_zeroed_page(GFP_KERNEL);
1817 tmp_buf = (unsigned char *) page;
1821 * If the port is the middle of closing, bail out now
1823 if (tty_hung_up_p(filp) ||
1824 (info->flags & ASYNC_CLOSING)) {
1825 if (info->flags & ASYNC_CLOSING)
1826 interruptible_sleep_on(&info->close_wait);
1827 #ifdef SERIAL_DO_RESTART
1828 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1829 -EAGAIN : -ERESTARTSYS);
1836 * Start up serial port
1838 retval = startup(info);
1843 retval = block_til_ready(tty, filp, info);
1845 #ifdef SERIAL_DEBUG_OPEN
1846 printk("rs_open returning after block_til_ready with %d\n",
1852 #ifdef SERIAL_DEBUG_OPEN
1853 printk("rs_open %s successful...", tty->name);
1859 * /proc fs routines....
1862 static inline int line_info(char *buf, struct serial_state *state)
1864 struct async_struct *info = state->info, scr_info;
1865 char stat_buf[30], control, status;
1867 unsigned long flags;
1869 ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
1872 * Figure out the current RS-232 lines
1875 info = &scr_info; /* This is just for serial_{in,out} */
1877 info->magic = SERIAL_MAGIC;
1878 info->flags = state->flags;
1882 local_irq_save(flags);
1884 control = info ? info->MCR : status;
1885 local_irq_restore(flags);
1889 if(!(control & SER_RTS))
1890 strcat(stat_buf, "|RTS");
1891 if(!(status & SER_CTS))
1892 strcat(stat_buf, "|CTS");
1893 if(!(control & SER_DTR))
1894 strcat(stat_buf, "|DTR");
1895 if(!(status & SER_DSR))
1896 strcat(stat_buf, "|DSR");
1897 if(!(status & SER_DCD))
1898 strcat(stat_buf, "|CD");
1901 ret += sprintf(buf+ret, " baud:%d",
1902 state->baud_base / info->quot);
1905 ret += sprintf(buf+ret, " tx:%d rx:%d",
1906 state->icount.tx, state->icount.rx);
1908 if (state->icount.frame)
1909 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
1911 if (state->icount.parity)
1912 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
1914 if (state->icount.brk)
1915 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
1917 if (state->icount.overrun)
1918 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
1921 * Last thing is the RS-232 status lines
1923 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1927 static int rs_read_proc(char *page, char **start, off_t off, int count,
1928 int *eof, void *data)
1933 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
1934 l = line_info(page + len, &rs_table[0]);
1936 if (len+begin > off+count)
1938 if (len+begin < off) {
1944 if (off >= len+begin)
1946 *start = page + (off-begin);
1947 return ((count < begin+len-off) ? count : begin+len-off);
1951 * ---------------------------------------------------------------------
1952 * rs_init() and friends
1954 * rs_init() is called at boot-time to initialize the serial driver.
1955 * ---------------------------------------------------------------------
1959 * This routine prints out the appropriate serial driver version
1960 * number, and identifies which options were configured into this
1963 static _INLINE_ void show_serial_version(void)
1965 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1969 static struct tty_operations serial_ops = {
1973 .put_char = rs_put_char,
1974 .flush_chars = rs_flush_chars,
1975 .write_room = rs_write_room,
1976 .chars_in_buffer = rs_chars_in_buffer,
1977 .flush_buffer = rs_flush_buffer,
1979 .throttle = rs_throttle,
1980 .unthrottle = rs_unthrottle,
1981 .set_termios = rs_set_termios,
1984 .hangup = rs_hangup,
1985 .break_ctl = rs_break,
1986 .send_xchar = rs_send_xchar,
1987 .wait_until_sent = rs_wait_until_sent,
1988 .read_proc = rs_read_proc,
1989 .tiocmget = rs_tiocmget,
1990 .tiocmset = rs_tiocmset,
1994 * The serial driver boot-time initialization code!
1996 static int __init rs_init(void)
1998 unsigned long flags;
1999 struct serial_state * state;
2001 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2004 serial_driver = alloc_tty_driver(1);
2009 * We request SERDAT and SERPER only, because the serial registers are
2010 * too spreaded over the custom register space
2012 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2017 show_serial_version();
2019 /* Initialize the tty_driver structure */
2021 serial_driver->owner = THIS_MODULE;
2022 serial_driver->driver_name = "amiserial";
2023 serial_driver->name = "ttyS";
2024 serial_driver->major = TTY_MAJOR;
2025 serial_driver->minor_start = 64;
2026 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2027 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2028 serial_driver->init_termios = tty_std_termios;
2029 serial_driver->init_termios.c_cflag =
2030 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2031 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2032 tty_set_operations(serial_driver, &serial_ops);
2034 if (tty_register_driver(serial_driver))
2035 panic("Couldn't register serial driver\n");
2038 state->magic = SSTATE_MAGIC;
2039 state->port = (int)&custom.serdatr; /* Just to give it a value */
2041 state->custom_divisor = 0;
2042 state->close_delay = 5*HZ/10;
2043 state->closing_wait = 30*HZ;
2044 state->icount.cts = state->icount.dsr =
2045 state->icount.rng = state->icount.dcd = 0;
2046 state->icount.rx = state->icount.tx = 0;
2047 state->icount.frame = state->icount.parity = 0;
2048 state->icount.overrun = state->icount.brk = 0;
2050 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2053 /* Hardware set up */
2055 state->baud_base = amiga_colorclock;
2056 state->xmit_fifo_size = 1;
2058 local_irq_save(flags);
2060 /* set ISRs, and then disable the rx interrupts */
2061 request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2062 request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2064 /* turn off Rx and Tx interrupts */
2065 custom.intena = IF_RBF | IF_TBE;
2068 /* clear any pending interrupt */
2069 custom.intreq = IF_RBF | IF_TBE;
2072 local_irq_restore(flags);
2075 * set the appropriate directions for the modem control flags,
2076 * and clear RTS and DTR
2078 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2079 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2084 static __exit void rs_exit(void)
2087 struct async_struct *info = rs_table[0].info;
2089 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2090 tasklet_kill(&info->tlet);
2091 if ((error = tty_unregister_driver(serial_driver)))
2092 printk("SERIAL: failed to unregister serial driver (%d)\n",
2094 put_tty_driver(serial_driver);
2097 rs_table[0].info = NULL;
2102 free_page((unsigned long) tmp_buf);
2106 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2109 module_init(rs_init)
2110 module_exit(rs_exit)
2114 * ------------------------------------------------------------
2115 * Serial console driver
2116 * ------------------------------------------------------------
2118 #ifdef CONFIG_SERIAL_CONSOLE
2120 static void amiga_serial_putc(char c)
2122 custom.serdat = (unsigned char)c | 0x100;
2123 while (!(custom.serdatr & 0x2000))
2128 * Print a string to the serial port trying not to disturb
2129 * any possible real use of the port...
2131 * The console must be locked when we get here.
2133 static void serial_console_write(struct console *co, const char *s,
2136 unsigned short intena = custom.intenar;
2138 custom.intena = IF_TBE;
2142 amiga_serial_putc('\r');
2143 amiga_serial_putc(*s++);
2146 custom.intena = IF_SETCLR | (intena & IF_TBE);
2149 static struct tty_driver *serial_console_device(struct console *c, int *index)
2152 return serial_driver;
2155 static struct console sercons = {
2157 .write = serial_console_write,
2158 .device = serial_console_device,
2159 .flags = CON_PRINTBUFFER,
2166 static int __init amiserial_console_init(void)
2168 register_console(&sercons);
2171 console_initcall(amiserial_console_init);
2174 MODULE_LICENSE("GPL");