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/version.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 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
59 * End of serial driver configuration section.
63 #include <linux/modversions.h>
65 #include <linux/module.h>
67 #include <linux/types.h>
68 #include <linux/serial.h>
69 #include <linux/serialP.h>
70 #include <linux/serial_reg.h>
71 static char *serial_version = "4.30";
73 #include <linux/errno.h>
74 #include <linux/signal.h>
75 #include <linux/sched.h>
76 #include <linux/kernel.h>
77 #include <linux/timer.h>
78 #include <linux/interrupt.h>
79 #include <linux/tty.h>
80 #include <linux/tty_flip.h>
81 #include <linux/console.h>
82 #include <linux/major.h>
83 #include <linux/string.h>
84 #include <linux/fcntl.h>
85 #include <linux/ptrace.h>
86 #include <linux/ioport.h>
88 #include <linux/malloc.h>
89 #include <linux/init.h>
90 #include <linux/delay.h>
92 #include <asm/setup.h>
94 #include <asm/system.h>
97 #include <asm/bitops.h>
99 #include <asm/amigahw.h>
100 #include <asm/amigaints.h>
103 #define _INLINE_ inline
106 static char *serial_name = "Amiga-builtin serial driver";
108 static DECLARE_TASK_QUEUE(tq_serial);
110 static struct tty_driver serial_driver, callout_driver;
111 static int serial_refcount;
113 /* serial subtype definitions */
114 #ifndef SERIAL_TYPE_NORMAL
115 #define SERIAL_TYPE_NORMAL 1
116 #define SERIAL_TYPE_CALLOUT 2
119 /* number of characters left in xmit buffer before we ask for more */
120 #define WAKEUP_CHARS 256
122 static struct async_struct *IRQ_ports;
124 static unsigned char current_ctl_bits;
126 static void change_speed(struct async_struct *info, struct termios *old);
127 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
130 static struct serial_state rs_table[1];
132 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
135 static struct tty_struct *serial_table[NR_PORTS];
136 static struct termios *serial_termios[NR_PORTS];
137 static struct termios *serial_termios_locked[NR_PORTS];
140 #define MIN(a,b) ((a) < (b) ? (a) : (b))
144 * tmp_buf is used as a temporary buffer by serial_write. We need to
145 * lock it in case the copy_from_user blocks while swapping in a page,
146 * and some other program tries to do a serial write at the same time.
147 * Since the lock will only come under contention when the system is
148 * swapping and available memory is low, it makes sense to share one
149 * buffer across all the serial ports, since it significantly saves
150 * memory if large numbers of serial ports are open.
152 static unsigned char *tmp_buf;
153 static DECLARE_MUTEX(tmp_buf_sem);
155 #include <asm/uaccess.h>
157 #define serial_isroot() (capable(CAP_SYS_ADMIN))
160 static inline int serial_paranoia_check(struct async_struct *info,
161 kdev_t device, const char *routine)
163 #ifdef SERIAL_PARANOIA_CHECK
164 static const char *badmagic =
165 "Warning: bad magic number for serial struct (%s) in %s\n";
166 static const char *badinfo =
167 "Warning: null async_struct for (%s) in %s\n";
170 printk(badinfo, kdevname(device), routine);
173 if (info->magic != SERIAL_MAGIC) {
174 printk(badmagic, kdevname(device), routine);
181 /* some serial hardware definitions */
182 #define SDR_OVRUN (1<<15)
183 #define SDR_RBF (1<<14)
184 #define SDR_TBE (1<<13)
185 #define SDR_TSRE (1<<12)
187 #define SERPER_PARENB (1<<15)
189 #define AC_SETCLR (1<<15)
190 #define AC_UARTBRK (1<<11)
192 #define SER_DTR (1<<7)
193 #define SER_RTS (1<<6)
194 #define SER_DCD (1<<5)
195 #define SER_CTS (1<<4)
196 #define SER_DSR (1<<3)
198 static __inline__ void rtsdtr_ctrl(int bits)
200 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
204 * ------------------------------------------------------------
205 * rs_stop() and rs_start()
207 * This routines are called before setting or resetting tty->stopped.
208 * They enable or disable transmitter interrupts, as necessary.
209 * ------------------------------------------------------------
211 static void rs_stop(struct tty_struct *tty)
213 struct async_struct *info = (struct async_struct *)tty->driver_data;
216 if (serial_paranoia_check(info, tty->device, "rs_stop"))
219 save_flags(flags); cli();
220 if (info->IER & UART_IER_THRI) {
221 info->IER &= ~UART_IER_THRI;
222 /* disable Tx interrupt and remove any pending interrupts */
223 custom.intena = IF_TBE;
225 custom.intreq = IF_TBE;
228 restore_flags(flags);
231 static void rs_start(struct tty_struct *tty)
233 struct async_struct *info = (struct async_struct *)tty->driver_data;
236 if (serial_paranoia_check(info, tty->device, "rs_start"))
239 save_flags(flags); cli();
240 if (info->xmit.head != info->xmit.tail
242 && !(info->IER & UART_IER_THRI)) {
243 info->IER |= UART_IER_THRI;
244 custom.intena = IF_SETCLR | IF_TBE;
246 /* set a pending Tx Interrupt, transmitter should restart now */
247 custom.intreq = IF_SETCLR | IF_TBE;
250 restore_flags(flags);
254 * ----------------------------------------------------------------------
256 * Here starts the interrupt handling routines. All of the following
257 * subroutines are declared as inline and are folded into
258 * rs_interrupt(). They were separated out for readability's sake.
260 * Note: rs_interrupt() is a "fast" interrupt, which means that it
261 * runs with interrupts turned off. People who may want to modify
262 * rs_interrupt() should try to keep the interrupt handler as fast as
263 * possible. After you are done making modifications, it is not a bad
266 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
268 * and look at the resulting assemble code in serial.s.
270 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
271 * -----------------------------------------------------------------------
275 * This routine is used by the interrupt handler to schedule
276 * processing in the software interrupt portion of the driver.
278 static _INLINE_ void rs_sched_event(struct async_struct *info,
281 info->event |= 1 << event;
282 queue_task(&info->tqueue, &tq_serial);
286 static _INLINE_ void receive_chars(struct async_struct *info)
290 struct tty_struct *tty = info->tty;
292 struct async_icount *icount;
294 icount = &info->state->icount;
296 status = UART_LSR_DR; /* We obviously have a character! */
297 serdatr = custom.serdatr;
299 custom.intreq = IF_RBF;
302 if((serdatr & 0x1ff) == 0)
303 status |= UART_LSR_BI;
304 if(serdatr & SDR_OVRUN)
305 status |= UART_LSR_OE;
308 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
310 *tty->flip.char_buf_ptr = ch;
313 #ifdef SERIAL_DEBUG_INTR
314 printk("DR%02x:%02x...", ch, status);
316 *tty->flip.flag_buf_ptr = 0;
319 * We don't handle parity or frame errors - but I have left
320 * the code in, since I'm not sure that the errors can't be
324 if (status & (UART_LSR_BI | UART_LSR_PE |
325 UART_LSR_FE | UART_LSR_OE)) {
327 * For statistics only
329 if (status & UART_LSR_BI) {
330 status &= ~(UART_LSR_FE | UART_LSR_PE);
332 } else if (status & UART_LSR_PE)
334 else if (status & UART_LSR_FE)
336 if (status & UART_LSR_OE)
340 * Now check to see if character should be
341 * ignored, and mask off conditions which
344 if (status & info->ignore_status_mask)
347 status &= info->read_status_mask;
349 if (status & (UART_LSR_BI)) {
350 #ifdef SERIAL_DEBUG_INTR
351 printk("handling break....");
353 *tty->flip.flag_buf_ptr = TTY_BREAK;
354 if (info->flags & ASYNC_SAK)
356 } else if (status & UART_LSR_PE)
357 *tty->flip.flag_buf_ptr = TTY_PARITY;
358 else if (status & UART_LSR_FE)
359 *tty->flip.flag_buf_ptr = TTY_FRAME;
360 if (status & UART_LSR_OE) {
362 * Overrun is special, since it's
363 * reported immediately, and doesn't
364 * affect the current character
366 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
368 tty->flip.flag_buf_ptr++;
369 tty->flip.char_buf_ptr++;
370 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
374 tty->flip.flag_buf_ptr++;
375 tty->flip.char_buf_ptr++;
379 tty_flip_buffer_push(tty);
382 static _INLINE_ void transmit_chars(struct async_struct *info)
384 custom.intreq = IF_TBE;
387 custom.serdat = info->x_char | 0x100;
389 info->state->icount.tx++;
393 if (info->xmit.head == info->xmit.tail
394 || info->tty->stopped
395 || info->tty->hw_stopped) {
396 info->IER &= ~UART_IER_THRI;
397 custom.intena = IF_TBE;
402 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
404 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
405 info->state->icount.tx++;
407 if (CIRC_CNT(info->xmit.head,
409 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
410 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
412 #ifdef SERIAL_DEBUG_INTR
415 if (info->xmit.head == info->xmit.tail) {
416 custom.intena = IF_TBE;
418 info->IER &= ~UART_IER_THRI;
422 static _INLINE_ void check_modem_status(struct async_struct *info)
424 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
425 unsigned char dstatus;
426 struct async_icount *icount;
428 /* Determine bits that have changed */
429 dstatus = status ^ current_ctl_bits;
430 current_ctl_bits = status;
433 icount = &info->state->icount;
434 /* update input line counters */
435 if (dstatus & SER_DSR)
437 if (dstatus & SER_DCD) {
439 #ifdef CONFIG_HARD_PPS
440 if ((info->flags & ASYNC_HARDPPS_CD) &&
445 if (dstatus & SER_CTS)
447 wake_up_interruptible(&info->delta_msr_wait);
450 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
451 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
452 printk("ttyS%02d CD now %s...", info->line,
453 (!(status & SER_DCD)) ? "on" : "off");
455 if (!(status & SER_DCD))
456 wake_up_interruptible(&info->open_wait);
457 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
458 (info->flags & ASYNC_CALLOUT_NOHUP))) {
459 #ifdef SERIAL_DEBUG_OPEN
460 printk("doing serial hangup...");
463 tty_hangup(info->tty);
466 if (info->flags & ASYNC_CTS_FLOW) {
467 if (info->tty->hw_stopped) {
468 if (!(status & SER_CTS)) {
469 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
470 printk("CTS tx start...");
472 info->tty->hw_stopped = 0;
473 info->IER |= UART_IER_THRI;
474 custom.intena = IF_SETCLR | IF_TBE;
476 /* set a pending Tx Interrupt, transmitter should restart now */
477 custom.intreq = IF_SETCLR | IF_TBE;
479 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
483 if ((status & SER_CTS)) {
484 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
485 printk("CTS tx stop...");
487 info->tty->hw_stopped = 1;
488 info->IER &= ~UART_IER_THRI;
489 /* disable Tx interrupt and remove any pending interrupts */
490 custom.intena = IF_TBE;
492 custom.intreq = IF_TBE;
499 static void ser_vbl_int( int irq, void *data, struct pt_regs *regs)
501 /* vbl is just a periodic interrupt we tie into to update modem status */
502 struct async_struct * info = IRQ_ports;
504 * TBD - is it better to unregister from this interrupt or to
505 * ignore it if MSI is clear ?
507 if(info->IER & UART_IER_MSI)
508 check_modem_status(info);
511 static void ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
513 struct async_struct * info;
515 #ifdef SERIAL_DEBUG_INTR
516 printk("ser_rx_int...");
520 if (!info || !info->tty)
524 info->last_active = jiffies;
525 #ifdef SERIAL_DEBUG_INTR
530 static void ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
532 struct async_struct * info;
534 if (custom.serdatr & SDR_TBE) {
535 #ifdef SERIAL_DEBUG_INTR
536 printk("ser_tx_int...");
540 if (!info || !info->tty)
543 transmit_chars(info);
544 info->last_active = jiffies;
545 #ifdef SERIAL_DEBUG_INTR
552 * -------------------------------------------------------------------
553 * Here ends the serial interrupt routines.
554 * -------------------------------------------------------------------
558 * This routine is used to handle the "bottom half" processing for the
559 * serial driver, known also the "software interrupt" processing.
560 * This processing is done at the kernel interrupt level, after the
561 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
562 * is where time-consuming activities which can not be done in the
563 * interrupt driver proper are done; the interrupt driver schedules
564 * them using rs_sched_event(), and they get done here.
566 static void do_serial_bh(void)
568 run_task_queue(&tq_serial);
571 static void do_softint(void *private_)
573 struct async_struct *info = (struct async_struct *) private_;
574 struct tty_struct *tty;
580 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
581 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
582 tty->ldisc.write_wakeup)
583 (tty->ldisc.write_wakeup)(tty);
584 wake_up_interruptible(&tty->write_wait);
589 * ---------------------------------------------------------------
590 * Low level utility subroutines for the serial driver: routines to
591 * figure out the appropriate timeout for an interrupt chain, routines
592 * to initialize and startup a serial port, and routines to shutdown a
593 * serial port. Useful stuff like that.
594 * ---------------------------------------------------------------
597 static int startup(struct async_struct * info)
603 page = get_free_page(GFP_KERNEL);
607 save_flags(flags); cli();
609 if (info->flags & ASYNC_INITIALIZED) {
617 info->xmit.buf = (unsigned char *) page;
619 #ifdef SERIAL_DEBUG_OPEN
620 printk("starting up ttys%d ...", info->line);
623 /* Clear anything in the input buffer */
625 custom.intreq = IF_RBF;
628 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
630 if (serial_isroot()) {
632 set_bit(TTY_IO_ERROR,
639 /* enable both Rx and Tx interrupts */
640 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
642 info->IER = UART_IER_MSI;
644 /* remember current state of the DCD and CTS bits */
645 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
650 if (info->tty->termios->c_cflag & CBAUD)
651 info->MCR = SER_DTR | SER_RTS;
652 rtsdtr_ctrl(info->MCR);
655 clear_bit(TTY_IO_ERROR, &info->tty->flags);
656 info->xmit.head = info->xmit.tail = 0;
659 * Set up the tty->alt_speed kludge
662 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
663 info->tty->alt_speed = 57600;
664 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
665 info->tty->alt_speed = 115200;
666 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
667 info->tty->alt_speed = 230400;
668 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
669 info->tty->alt_speed = 460800;
673 * and set the speed of the serial port
675 change_speed(info, 0);
677 info->flags |= ASYNC_INITIALIZED;
678 restore_flags(flags);
682 restore_flags(flags);
687 * This routine will shutdown a serial port; interrupts are disabled, and
688 * DTR is dropped if the hangup on close termio flag is on.
690 static void shutdown(struct async_struct * info)
693 struct serial_state *state;
695 if (!(info->flags & ASYNC_INITIALIZED))
700 #ifdef SERIAL_DEBUG_OPEN
701 printk("Shutting down serial port %d ....\n", info->line);
704 save_flags(flags); cli(); /* Disable interrupts */
707 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
708 * here so the queue might never be waken up
710 wake_up_interruptible(&info->delta_msr_wait);
715 * Free the IRQ, if necessary
717 free_irq(IRQ_AMIGA_VERTB, info);
719 if (info->xmit.buf) {
720 free_page((unsigned long) info->xmit.buf);
725 custom.intena = IF_RBF | IF_TBE;
728 /* disable break condition */
729 custom.adkcon = AC_UARTBRK;
732 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
733 info->MCR &= ~(SER_DTR|SER_RTS);
734 rtsdtr_ctrl(info->MCR);
737 set_bit(TTY_IO_ERROR, &info->tty->flags);
739 info->flags &= ~ASYNC_INITIALIZED;
740 restore_flags(flags);
745 * This routine is called to set the UART divisor registers to match
746 * the specified baud rate for a serial port.
748 static void change_speed(struct async_struct *info,
749 struct termios *old_termios)
751 int quot = 0, baud_base, baud;
752 unsigned cflag, cval = 0;
756 if (!info->tty || !info->tty->termios)
758 cflag = info->tty->termios->c_cflag;
760 /* Byte size is always 8 bits plus parity bit if requested */
763 if (cflag & CSTOPB) {
767 if (cflag & PARENB) {
768 cval |= UART_LCR_PARITY;
771 if (!(cflag & PARODD))
772 cval |= UART_LCR_EPAR;
775 cval |= UART_LCR_SPAR;
778 /* Determine divisor based on baud rate */
779 baud = tty_get_baud_rate(info->tty);
781 baud = 9600; /* B0 transition handled in rs_set_termios */
782 baud_base = info->state->baud_base;
784 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
785 quot = info->state->custom_divisor;
788 /* Special case since 134 is really 134.5 */
789 quot = (2*baud_base / 269);
791 quot = baud_base / baud;
793 /* If the quotient is zero refuse the change */
794 if (!quot && old_termios) {
795 info->tty->termios->c_cflag &= ~CBAUD;
796 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
797 baud = tty_get_baud_rate(info->tty);
801 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
802 quot = info->state->custom_divisor;
805 /* Special case since 134 is really 134.5 */
806 quot = (2*baud_base / 269);
808 quot = baud_base / baud;
811 /* As a last resort, if the quotient is zero, default to 9600 bps */
813 quot = baud_base / 9600;
815 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
816 info->timeout += HZ/50; /* Add .02 seconds of slop */
818 /* CTS flow control flag and modem status interrupts */
819 info->IER &= ~UART_IER_MSI;
820 if (info->flags & ASYNC_HARDPPS_CD)
821 info->IER |= UART_IER_MSI;
822 if (cflag & CRTSCTS) {
823 info->flags |= ASYNC_CTS_FLOW;
824 info->IER |= UART_IER_MSI;
826 info->flags &= ~ASYNC_CTS_FLOW;
828 info->flags &= ~ASYNC_CHECK_CD;
830 info->flags |= ASYNC_CHECK_CD;
831 info->IER |= UART_IER_MSI;
834 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
838 * Set up parity check flag
840 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
842 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
843 if (I_INPCK(info->tty))
844 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
845 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
846 info->read_status_mask |= UART_LSR_BI;
849 * Characters to ignore
851 info->ignore_status_mask = 0;
852 if (I_IGNPAR(info->tty))
853 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
854 if (I_IGNBRK(info->tty)) {
855 info->ignore_status_mask |= UART_LSR_BI;
857 * If we're ignore parity and break indicators, ignore
858 * overruns too. (For real raw support).
860 if (I_IGNPAR(info->tty))
861 info->ignore_status_mask |= UART_LSR_OE;
864 * !!! ignore all characters if CREAD is not set
866 if ((cflag & CREAD) == 0)
867 info->ignore_status_mask |= UART_LSR_DR;
868 save_flags(flags); cli();
873 /* Set up the baud rate */
876 /* Enable or disable parity bit */
878 if(cval & UART_LCR_PARITY)
879 serper |= (SERPER_PARENB);
881 custom.serper = serper;
885 info->LCR = cval; /* Save LCR */
886 restore_flags(flags);
889 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
891 struct async_struct *info = (struct async_struct *)tty->driver_data;
894 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
897 if (!tty || !info->xmit.buf)
900 save_flags(flags); cli();
901 if (CIRC_SPACE(info->xmit.head,
903 SERIAL_XMIT_SIZE) == 0) {
904 restore_flags(flags);
908 info->xmit.buf[info->xmit.head++] = ch;
909 info->xmit.head &= SERIAL_XMIT_SIZE-1;
910 restore_flags(flags);
913 static void rs_flush_chars(struct tty_struct *tty)
915 struct async_struct *info = (struct async_struct *)tty->driver_data;
918 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
921 if (info->xmit.head == info->xmit.tail
927 save_flags(flags); cli();
928 info->IER |= UART_IER_THRI;
929 custom.intena = IF_SETCLR | IF_TBE;
931 /* set a pending Tx Interrupt, transmitter should restart now */
932 custom.intreq = IF_SETCLR | IF_TBE;
934 restore_flags(flags);
937 static int rs_write(struct tty_struct * tty, int from_user,
938 const unsigned char *buf, int count)
941 struct async_struct *info = (struct async_struct *)tty->driver_data;
944 if (serial_paranoia_check(info, tty->device, "rs_write"))
947 if (!tty || !info->xmit.buf || !tmp_buf)
955 c = CIRC_SPACE_TO_END(info->xmit.head,
961 c -= copy_from_user(tmp_buf, buf, c);
968 c1 = CIRC_SPACE_TO_END(info->xmit.head,
973 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
974 info->xmit.head = ((info->xmit.head + c) &
975 (SERIAL_XMIT_SIZE-1));
976 restore_flags(flags);
985 c = CIRC_SPACE_TO_END(info->xmit.head,
993 memcpy(info->xmit.buf + info->xmit.head, buf, c);
994 info->xmit.head = ((info->xmit.head + c) &
995 (SERIAL_XMIT_SIZE-1));
1000 restore_flags(flags);
1002 if (info->xmit.head != info->xmit.tail
1005 && !(info->IER & UART_IER_THRI)) {
1006 info->IER |= UART_IER_THRI;
1008 custom.intena = IF_SETCLR | IF_TBE;
1010 /* set a pending Tx Interrupt, transmitter should restart now */
1011 custom.intreq = IF_SETCLR | IF_TBE;
1013 restore_flags(flags);
1018 static int rs_write_room(struct tty_struct *tty)
1020 struct async_struct *info = (struct async_struct *)tty->driver_data;
1022 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1024 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1027 static int rs_chars_in_buffer(struct tty_struct *tty)
1029 struct async_struct *info = (struct async_struct *)tty->driver_data;
1031 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1033 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1036 static void rs_flush_buffer(struct tty_struct *tty)
1038 struct async_struct *info = (struct async_struct *)tty->driver_data;
1039 unsigned long flags;
1041 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1043 save_flags(flags); cli();
1044 info->xmit.head = info->xmit.tail = 0;
1045 restore_flags(flags);
1046 wake_up_interruptible(&tty->write_wait);
1047 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1048 tty->ldisc.write_wakeup)
1049 (tty->ldisc.write_wakeup)(tty);
1053 * This function is used to send a high-priority XON/XOFF character to
1056 static void rs_send_xchar(struct tty_struct *tty, char ch)
1058 struct async_struct *info = (struct async_struct *)tty->driver_data;
1059 unsigned long flags;
1061 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1066 /* Make sure transmit interrupts are on */
1071 if(!(custom.intenar & IF_TBE)) {
1072 custom.intena = IF_SETCLR | IF_TBE;
1074 /* set a pending Tx Interrupt, transmitter should restart now */
1075 custom.intreq = IF_SETCLR | IF_TBE;
1078 restore_flags(flags);
1080 info->IER |= UART_IER_THRI;
1085 * ------------------------------------------------------------
1088 * This routine is called by the upper-layer tty layer to signal that
1089 * incoming characters should be throttled.
1090 * ------------------------------------------------------------
1092 static void rs_throttle(struct tty_struct * tty)
1094 struct async_struct *info = (struct async_struct *)tty->driver_data;
1095 unsigned long flags;
1096 #ifdef SERIAL_DEBUG_THROTTLE
1099 printk("throttle %s: %d....\n", tty_name(tty, buf),
1100 tty->ldisc.chars_in_buffer(tty));
1103 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1107 rs_send_xchar(tty, STOP_CHAR(tty));
1109 if (tty->termios->c_cflag & CRTSCTS)
1110 info->MCR &= ~SER_RTS;
1112 save_flags(flags); cli();
1113 rtsdtr_ctrl(info->MCR);
1114 restore_flags(flags);
1117 static void rs_unthrottle(struct tty_struct * tty)
1119 struct async_struct *info = (struct async_struct *)tty->driver_data;
1120 unsigned long flags;
1121 #ifdef SERIAL_DEBUG_THROTTLE
1124 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1125 tty->ldisc.chars_in_buffer(tty));
1128 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1135 rs_send_xchar(tty, START_CHAR(tty));
1137 if (tty->termios->c_cflag & CRTSCTS)
1138 info->MCR |= SER_RTS;
1139 save_flags(flags); cli();
1140 rtsdtr_ctrl(info->MCR);
1141 restore_flags(flags);
1145 * ------------------------------------------------------------
1146 * rs_ioctl() and friends
1147 * ------------------------------------------------------------
1150 static int get_serial_info(struct async_struct * info,
1151 struct serial_struct * retinfo)
1153 struct serial_struct tmp;
1154 struct serial_state *state = info->state;
1158 memset(&tmp, 0, sizeof(tmp));
1159 tmp.type = state->type;
1160 tmp.line = state->line;
1161 tmp.port = state->port;
1162 tmp.irq = state->irq;
1163 tmp.flags = state->flags;
1164 tmp.xmit_fifo_size = state->xmit_fifo_size;
1165 tmp.baud_base = state->baud_base;
1166 tmp.close_delay = state->close_delay;
1167 tmp.closing_wait = state->closing_wait;
1168 tmp.custom_divisor = state->custom_divisor;
1169 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1174 static int set_serial_info(struct async_struct * info,
1175 struct serial_struct * new_info)
1177 struct serial_struct new_serial;
1178 struct serial_state old_state, *state;
1179 unsigned int change_irq,change_port;
1182 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1184 state = info->state;
1187 change_irq = new_serial.irq != state->irq;
1188 change_port = (new_serial.port != state->port);
1189 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1192 if (!serial_isroot()) {
1193 if ((new_serial.baud_base != state->baud_base) ||
1194 (new_serial.close_delay != state->close_delay) ||
1195 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1196 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1197 (state->flags & ~ASYNC_USR_MASK)))
1199 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1200 (new_serial.flags & ASYNC_USR_MASK));
1201 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1202 (new_serial.flags & ASYNC_USR_MASK));
1203 state->custom_divisor = new_serial.custom_divisor;
1204 goto check_and_exit;
1207 if (new_serial.baud_base < 9600)
1211 * OK, past this point, all the error checking has been done.
1212 * At this point, we start making changes.....
1215 state->baud_base = new_serial.baud_base;
1216 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1217 (new_serial.flags & ASYNC_FLAGS));
1218 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1219 (info->flags & ASYNC_INTERNAL_FLAGS));
1220 state->custom_divisor = new_serial.custom_divisor;
1221 state->close_delay = new_serial.close_delay * HZ/100;
1222 state->closing_wait = new_serial.closing_wait * HZ/100;
1223 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1226 if (info->flags & ASYNC_INITIALIZED) {
1227 if (((old_state.flags & ASYNC_SPD_MASK) !=
1228 (state->flags & ASYNC_SPD_MASK)) ||
1229 (old_state.custom_divisor != state->custom_divisor)) {
1230 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1231 info->tty->alt_speed = 57600;
1232 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1233 info->tty->alt_speed = 115200;
1234 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1235 info->tty->alt_speed = 230400;
1236 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1237 info->tty->alt_speed = 460800;
1238 change_speed(info, 0);
1241 retval = startup(info);
1247 * get_lsr_info - get line status register info
1249 * Purpose: Let user call ioctl() to get info when the UART physically
1250 * is emptied. On bus types like RS485, the transmitter must
1251 * release the bus after transmitting. This must be done when
1252 * the transmit shift register is empty, not be done when the
1253 * transmit holding register is empty. This functionality
1254 * allows an RS485 driver to be written in user space.
1256 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1258 unsigned char status;
1259 unsigned int result;
1260 unsigned long flags;
1262 save_flags(flags); cli();
1263 status = custom.serdatr;
1265 restore_flags(flags);
1266 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1267 if (copy_to_user(value, &result, sizeof(int)))
1273 static int get_modem_info(struct async_struct * info, unsigned int *value)
1275 unsigned char control, status;
1276 unsigned int result;
1277 unsigned long flags;
1279 control = info->MCR;
1280 save_flags(flags); cli();
1282 restore_flags(flags);
1283 result = ((control & SER_RTS) ? TIOCM_RTS : 0)
1284 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1285 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1286 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1287 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1288 if (copy_to_user(value, &result, sizeof(int)))
1293 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1294 unsigned int *value)
1297 unsigned long flags;
1299 if (copy_from_user(&arg, value, sizeof(int)))
1304 if (arg & TIOCM_RTS)
1305 info->MCR |= SER_RTS;
1306 if (arg & TIOCM_DTR)
1307 info->MCR |= SER_DTR;
1310 if (arg & TIOCM_RTS)
1311 info->MCR &= ~SER_RTS;
1312 if (arg & TIOCM_DTR)
1313 info->MCR &= ~SER_DTR;
1316 info->MCR = ((info->MCR & ~(SER_RTS | SER_DTR))
1317 | ((arg & TIOCM_RTS) ? SER_RTS : 0)
1318 | ((arg & TIOCM_DTR) ? SER_DTR : 0));
1323 save_flags(flags); cli();
1324 rtsdtr_ctrl(info->MCR);
1325 restore_flags(flags);
1330 * rs_break() --- routine which turns the break handling on or off
1332 static void rs_break(struct tty_struct *tty, int break_state)
1334 struct async_struct * info = (struct async_struct *)tty->driver_data;
1335 unsigned long flags;
1337 if (serial_paranoia_check(info, tty->device, "rs_break"))
1340 save_flags(flags); cli();
1341 if (break_state == -1)
1342 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1344 custom.adkcon = AC_UARTBRK;
1346 restore_flags(flags);
1350 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1351 unsigned int cmd, unsigned long arg)
1353 struct async_struct * info = (struct async_struct *)tty->driver_data;
1354 struct async_icount cprev, cnow; /* kernel counter temps */
1355 struct serial_icounter_struct icount;
1356 unsigned long flags;
1358 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1361 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1362 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1363 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1364 if (tty->flags & (1 << TTY_IO_ERROR))
1370 return get_modem_info(info, (unsigned int *) arg);
1374 return set_modem_info(info, cmd, (unsigned int *) arg);
1376 return get_serial_info(info,
1377 (struct serial_struct *) arg);
1379 return set_serial_info(info,
1380 (struct serial_struct *) arg);
1384 case TIOCSERGETLSR: /* Get line status register */
1385 return get_lsr_info(info, (unsigned int *) arg);
1387 case TIOCSERGSTRUCT:
1388 if (copy_to_user((struct async_struct *) arg,
1389 info, sizeof(struct async_struct)))
1394 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1395 * - mask passed in arg for lines of interest
1396 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1397 * Caller should use TIOCGICOUNT to see which one it was
1400 save_flags(flags); cli();
1401 /* note the counters on entry */
1402 cprev = info->state->icount;
1403 restore_flags(flags);
1405 interruptible_sleep_on(&info->delta_msr_wait);
1406 /* see if a signal did it */
1407 if (signal_pending(current))
1408 return -ERESTARTSYS;
1409 save_flags(flags); cli();
1410 cnow = info->state->icount; /* atomic copy */
1411 restore_flags(flags);
1412 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1413 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1414 return -EIO; /* no change => error */
1415 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1416 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1417 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1418 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1426 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1427 * Return: write counters to the user passed counter struct
1428 * NB: both 1->0 and 0->1 transitions are counted except for
1429 * RI where only 0->1 is counted.
1432 save_flags(flags); cli();
1433 cnow = info->state->icount;
1434 restore_flags(flags);
1435 icount.cts = cnow.cts;
1436 icount.dsr = cnow.dsr;
1437 icount.rng = cnow.rng;
1438 icount.dcd = cnow.dcd;
1439 icount.rx = cnow.rx;
1440 icount.tx = cnow.tx;
1441 icount.frame = cnow.frame;
1442 icount.overrun = cnow.overrun;
1443 icount.parity = cnow.parity;
1444 icount.brk = cnow.brk;
1445 icount.buf_overrun = cnow.buf_overrun;
1447 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1452 /* "setserial -W" is called in Debian boot */
1453 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1457 return -ENOIOCTLCMD;
1462 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1464 struct async_struct *info = (struct async_struct *)tty->driver_data;
1465 unsigned long flags;
1466 unsigned int cflag = tty->termios->c_cflag;
1468 if ( (cflag == old_termios->c_cflag)
1469 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1470 == RELEVANT_IFLAG(old_termios->c_iflag)))
1473 change_speed(info, old_termios);
1475 /* Handle transition to B0 status */
1476 if ((old_termios->c_cflag & CBAUD) &&
1478 info->MCR &= ~(SER_DTR|SER_RTS);
1479 save_flags(flags); cli();
1480 rtsdtr_ctrl(info->MCR);
1481 restore_flags(flags);
1484 /* Handle transition away from B0 status */
1485 if (!(old_termios->c_cflag & CBAUD) &&
1487 info->MCR |= SER_DTR;
1488 if (!(tty->termios->c_cflag & CRTSCTS) ||
1489 !test_bit(TTY_THROTTLED, &tty->flags)) {
1490 info->MCR |= SER_RTS;
1492 save_flags(flags); cli();
1493 rtsdtr_ctrl(info->MCR);
1494 restore_flags(flags);
1497 /* Handle turning off CRTSCTS */
1498 if ((old_termios->c_cflag & CRTSCTS) &&
1499 !(tty->termios->c_cflag & CRTSCTS)) {
1500 tty->hw_stopped = 0;
1506 * No need to wake up processes in open wait, since they
1507 * sample the CLOCAL flag once, and don't recheck it.
1508 * XXX It's not clear whether the current behavior is correct
1509 * or not. Hence, this may change.....
1511 if (!(old_termios->c_cflag & CLOCAL) &&
1512 (tty->termios->c_cflag & CLOCAL))
1513 wake_up_interruptible(&info->open_wait);
1518 * ------------------------------------------------------------
1521 * This routine is called when the serial port gets closed. First, we
1522 * wait for the last remaining data to be sent. Then, we unlink its
1523 * async structure from the interrupt chain if necessary, and we free
1524 * that IRQ if nothing is left in the chain.
1525 * ------------------------------------------------------------
1527 static void rs_close(struct tty_struct *tty, struct file * filp)
1529 struct async_struct * info = (struct async_struct *)tty->driver_data;
1530 struct serial_state *state;
1531 unsigned long flags;
1533 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1536 state = info->state;
1538 save_flags(flags); cli();
1540 if (tty_hung_up_p(filp)) {
1541 DBG_CNT("before DEC-hung");
1543 restore_flags(flags);
1547 #ifdef SERIAL_DEBUG_OPEN
1548 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1550 if ((tty->count == 1) && (state->count != 1)) {
1552 * Uh, oh. tty->count is 1, which means that the tty
1553 * structure will be freed. state->count should always
1554 * be one in these conditions. If it's greater than
1555 * one, we've got real problems, since it means the
1556 * serial port won't be shutdown.
1558 printk("rs_close: bad serial port count; tty->count is 1, "
1559 "state->count is %d\n", state->count);
1562 if (--state->count < 0) {
1563 printk("rs_close: bad serial port count for ttys%d: %d\n",
1564 info->line, state->count);
1568 DBG_CNT("before DEC-2");
1570 restore_flags(flags);
1573 info->flags |= ASYNC_CLOSING;
1575 * Save the termios structure, since this port may have
1576 * separate termios for callout and dialin.
1578 if (info->flags & ASYNC_NORMAL_ACTIVE)
1579 info->state->normal_termios = *tty->termios;
1580 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1581 info->state->callout_termios = *tty->termios;
1583 * Now we wait for the transmit buffer to clear; and we notify
1584 * the line discipline to only process XON/XOFF characters.
1587 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1588 tty_wait_until_sent(tty, info->closing_wait);
1590 * At this point we stop accepting input. To do this, we
1591 * disable the receive line status interrupts, and tell the
1592 * interrupt driver to stop checking the data ready bit in the
1593 * line status register.
1595 info->read_status_mask &= ~UART_LSR_DR;
1596 if (info->flags & ASYNC_INITIALIZED) {
1597 /* disable receive interrupts */
1598 custom.intena = IF_RBF;
1600 /* clear any pending receive interrupt */
1601 custom.intreq = IF_RBF;
1605 * Before we drop DTR, make sure the UART transmitter
1606 * has completely drained; this is especially
1607 * important if there is a transmit FIFO!
1609 rs_wait_until_sent(tty, info->timeout);
1612 if (tty->driver.flush_buffer)
1613 tty->driver.flush_buffer(tty);
1614 if (tty->ldisc.flush_buffer)
1615 tty->ldisc.flush_buffer(tty);
1619 if (info->blocked_open) {
1620 if (info->close_delay) {
1621 current->state = TASK_INTERRUPTIBLE;
1622 schedule_timeout(info->close_delay);
1624 wake_up_interruptible(&info->open_wait);
1626 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1628 wake_up_interruptible(&info->close_wait);
1630 restore_flags(flags);
1634 * rs_wait_until_sent() --- wait until the transmitter is empty
1636 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1638 struct async_struct * info = (struct async_struct *)tty->driver_data;
1639 unsigned long orig_jiffies, char_time;
1642 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1645 if (info->xmit_fifo_size == 0)
1646 return; /* Just in case.... */
1648 orig_jiffies = jiffies;
1650 * Set the check interval to be 1/5 of the estimated time to
1651 * send a single character, and make it at least 1. The check
1652 * interval should also be less than the timeout.
1654 * Note: we have to use pretty tight timings here to satisfy
1657 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1658 char_time = char_time / 5;
1662 char_time = MIN(char_time, timeout);
1664 * If the transmitter hasn't cleared in twice the approximate
1665 * amount of time to send the entire FIFO, it probably won't
1666 * ever clear. This assumes the UART isn't doing flow
1667 * control, which is currently the case. Hence, if it ever
1668 * takes longer than info->timeout, this is probably due to a
1669 * UART bug of some kind. So, we clamp the timeout parameter at
1672 if (!timeout || timeout > 2*info->timeout)
1673 timeout = 2*info->timeout;
1674 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1675 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1676 printk("jiff=%lu...", jiffies);
1678 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1679 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1680 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1682 current->state = TASK_INTERRUPTIBLE;
1683 schedule_timeout(char_time);
1684 if (signal_pending(current))
1686 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1689 current->state = TASK_RUNNING;
1690 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1691 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1696 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1698 static void rs_hangup(struct tty_struct *tty)
1700 struct async_struct * info = (struct async_struct *)tty->driver_data;
1701 struct serial_state *state = info->state;
1703 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1706 state = info->state;
1708 rs_flush_buffer(tty);
1712 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1714 wake_up_interruptible(&info->open_wait);
1718 * ------------------------------------------------------------
1719 * rs_open() and friends
1720 * ------------------------------------------------------------
1722 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1723 struct async_struct *info)
1725 #ifdef DECLARE_WAITQUEUE
1726 DECLARE_WAITQUEUE(wait, current);
1728 struct wait_queue wait = { current, NULL };
1730 struct serial_state *state = info->state;
1732 int do_clocal = 0, extra_count = 0;
1733 unsigned long flags;
1736 * If the device is in the middle of being closed, then block
1737 * until it's done, and then try again.
1739 if (tty_hung_up_p(filp) ||
1740 (info->flags & ASYNC_CLOSING)) {
1741 if (info->flags & ASYNC_CLOSING)
1742 interruptible_sleep_on(&info->close_wait);
1743 #ifdef SERIAL_DO_RESTART
1744 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1745 -EAGAIN : -ERESTARTSYS);
1752 * If this is a callout device, then just make sure the normal
1753 * device isn't being used.
1755 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1756 if (info->flags & ASYNC_NORMAL_ACTIVE)
1758 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1759 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1760 (info->session != current->session))
1762 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1763 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1764 (info->pgrp != current->pgrp))
1766 info->flags |= ASYNC_CALLOUT_ACTIVE;
1771 * If non-blocking mode is set, or the port is not enabled,
1772 * then make the check up front and then exit.
1774 if ((filp->f_flags & O_NONBLOCK) ||
1775 (tty->flags & (1 << TTY_IO_ERROR))) {
1776 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1778 info->flags |= ASYNC_NORMAL_ACTIVE;
1782 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1783 if (state->normal_termios.c_cflag & CLOCAL)
1786 if (tty->termios->c_cflag & CLOCAL)
1791 * Block waiting for the carrier detect and the line to become
1792 * free (i.e., not in use by the callout). While we are in
1793 * this loop, state->count is dropped by one, so that
1794 * rs_close() knows when to free things. We restore it upon
1795 * exit, either normal or abnormal.
1798 add_wait_queue(&info->open_wait, &wait);
1799 #ifdef SERIAL_DEBUG_OPEN
1800 printk("block_til_ready before block: ttys%d, count = %d\n",
1801 state->line, state->count);
1803 save_flags(flags); cli();
1804 if (!tty_hung_up_p(filp)) {
1808 restore_flags(flags);
1809 info->blocked_open++;
1811 save_flags(flags); cli();
1812 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1813 (tty->termios->c_cflag & CBAUD))
1814 rtsdtr_ctrl(SER_DTR|SER_RTS);
1815 restore_flags(flags);
1816 set_current_state(TASK_INTERRUPTIBLE);
1817 if (tty_hung_up_p(filp) ||
1818 !(info->flags & ASYNC_INITIALIZED)) {
1819 #ifdef SERIAL_DO_RESTART
1820 if (info->flags & ASYNC_HUP_NOTIFY)
1823 retval = -ERESTARTSYS;
1829 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1830 !(info->flags & ASYNC_CLOSING) &&
1831 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1833 if (signal_pending(current)) {
1834 retval = -ERESTARTSYS;
1837 #ifdef SERIAL_DEBUG_OPEN
1838 printk("block_til_ready blocking: ttys%d, count = %d\n",
1839 info->line, state->count);
1843 current->state = TASK_RUNNING;
1844 remove_wait_queue(&info->open_wait, &wait);
1847 info->blocked_open--;
1848 #ifdef SERIAL_DEBUG_OPEN
1849 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1850 info->line, state->count);
1854 info->flags |= ASYNC_NORMAL_ACTIVE;
1858 static int get_async_struct(int line, struct async_struct **ret_info)
1860 struct async_struct *info;
1861 struct serial_state *sstate;
1863 sstate = rs_table + line;
1866 *ret_info = sstate->info;
1869 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1874 memset(info, 0, sizeof(struct async_struct));
1875 #ifdef DECLARE_WAITQUEUE
1876 init_waitqueue_head(&info->open_wait);
1877 init_waitqueue_head(&info->close_wait);
1878 init_waitqueue_head(&info->delta_msr_wait);
1880 info->magic = SERIAL_MAGIC;
1881 info->port = sstate->port;
1882 info->flags = sstate->flags;
1883 info->xmit_fifo_size = sstate->xmit_fifo_size;
1885 info->tqueue.routine = do_softint;
1886 info->tqueue.data = info;
1887 info->state = sstate;
1890 *ret_info = sstate->info;
1893 *ret_info = sstate->info = info;
1898 * This routine is called whenever a serial port is opened. It
1899 * enables interrupts for a serial port, linking in its async structure into
1900 * the IRQ chain. It also performs the serial-specific
1901 * initialization for the tty structure.
1903 static int rs_open(struct tty_struct *tty, struct file * filp)
1905 struct async_struct *info;
1910 line = MINOR(tty->device) - tty->driver.minor_start;
1911 if ((line < 0) || (line >= NR_PORTS)) {
1915 retval = get_async_struct(line, &info);
1920 tty->driver_data = info;
1922 if (serial_paranoia_check(info, tty->device, "rs_open"))
1925 #ifdef SERIAL_DEBUG_OPEN
1926 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1927 info->state->count);
1929 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1932 page = get_free_page(GFP_KERNEL);
1939 tmp_buf = (unsigned char *) page;
1943 * If the port is the middle of closing, bail out now
1945 if (tty_hung_up_p(filp) ||
1946 (info->flags & ASYNC_CLOSING)) {
1947 if (info->flags & ASYNC_CLOSING)
1948 interruptible_sleep_on(&info->close_wait);
1949 #ifdef SERIAL_DO_RESTART
1950 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1951 -EAGAIN : -ERESTARTSYS);
1958 * Start up serial port
1960 retval = startup(info);
1965 retval = block_til_ready(tty, filp, info);
1967 #ifdef SERIAL_DEBUG_OPEN
1968 printk("rs_open returning after block_til_ready with %d\n",
1974 if ((info->state->count == 1) &&
1975 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1976 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1977 *tty->termios = info->state->normal_termios;
1979 *tty->termios = info->state->callout_termios;
1980 change_speed(info, 0);
1982 info->session = current->session;
1983 info->pgrp = current->pgrp;
1985 #ifdef SERIAL_DEBUG_OPEN
1986 printk("rs_open ttys%d successful...", info->line);
1992 * /proc fs routines....
1995 static inline int line_info(char *buf, struct serial_state *state)
1997 struct async_struct *info = state->info, scr_info;
1998 char stat_buf[30], control, status;
2000 unsigned long flags;
2002 ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
2005 * Figure out the current RS-232 lines
2008 info = &scr_info; /* This is just for serial_{in,out} */
2010 info->magic = SERIAL_MAGIC;
2011 info->flags = state->flags;
2015 save_flags(flags); cli();
2017 control = info ? info->MCR : status;
2018 restore_flags(flags);
2022 if(!(control & SER_RTS))
2023 strcat(stat_buf, "|RTS");
2024 if(!(status & SER_CTS))
2025 strcat(stat_buf, "|CTS");
2026 if(!(control & SER_DTR))
2027 strcat(stat_buf, "|DTR");
2028 if(!(status & SER_DSR))
2029 strcat(stat_buf, "|DSR");
2030 if(!(status & SER_DCD))
2031 strcat(stat_buf, "|CD");
2034 ret += sprintf(buf+ret, " baud:%d",
2035 state->baud_base / info->quot);
2038 ret += sprintf(buf+ret, " tx:%d rx:%d",
2039 state->icount.tx, state->icount.rx);
2041 if (state->icount.frame)
2042 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2044 if (state->icount.parity)
2045 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2047 if (state->icount.brk)
2048 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2050 if (state->icount.overrun)
2051 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2054 * Last thing is the RS-232 status lines
2056 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2060 int rs_read_proc(char *page, char **start, off_t off, int count,
2061 int *eof, void *data)
2066 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2067 l = line_info(page + len, &rs_table[0]);
2069 if (len+begin > off+count)
2071 if (len+begin < off) {
2077 if (off >= len+begin)
2079 *start = page + (off-begin);
2080 return ((count < begin+len-off) ? count : begin+len-off);
2084 * ---------------------------------------------------------------------
2085 * rs_init() and friends
2087 * rs_init() is called at boot-time to initialize the serial driver.
2088 * ---------------------------------------------------------------------
2092 * This routine prints out the appropriate serial driver version
2093 * number, and identifies which options were configured into this
2096 static _INLINE_ void show_serial_version(void)
2098 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2102 int register_serial(struct serial_struct *req);
2103 void unregister_serial(int line);
2107 * The serial driver boot-time initialization code!
2109 static int __init rs_init(void)
2111 unsigned long flags;
2112 struct serial_state * state;
2114 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2118 * We request SERDAT and SERPER only, because the serial registers are
2119 * too spreaded over the custom register space
2121 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2124 init_bh(SERIAL_BH, do_serial_bh);
2128 show_serial_version();
2130 /* Initialize the tty_driver structure */
2132 memset(&serial_driver, 0, sizeof(struct tty_driver));
2133 serial_driver.magic = TTY_DRIVER_MAGIC;
2134 serial_driver.driver_name = "amiserial";
2135 serial_driver.name = "ttyS";
2136 serial_driver.major = TTY_MAJOR;
2137 serial_driver.minor_start = 64;
2138 serial_driver.num = 1;
2139 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2140 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2141 serial_driver.init_termios = tty_std_termios;
2142 serial_driver.init_termios.c_cflag =
2143 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2144 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2145 serial_driver.refcount = &serial_refcount;
2146 serial_driver.table = serial_table;
2147 serial_driver.termios = serial_termios;
2148 serial_driver.termios_locked = serial_termios_locked;
2150 serial_driver.open = rs_open;
2151 serial_driver.close = rs_close;
2152 serial_driver.write = rs_write;
2153 serial_driver.put_char = rs_put_char;
2154 serial_driver.flush_chars = rs_flush_chars;
2155 serial_driver.write_room = rs_write_room;
2156 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2157 serial_driver.flush_buffer = rs_flush_buffer;
2158 serial_driver.ioctl = rs_ioctl;
2159 serial_driver.throttle = rs_throttle;
2160 serial_driver.unthrottle = rs_unthrottle;
2161 serial_driver.set_termios = rs_set_termios;
2162 serial_driver.stop = rs_stop;
2163 serial_driver.start = rs_start;
2164 serial_driver.hangup = rs_hangup;
2165 serial_driver.break_ctl = rs_break;
2166 serial_driver.send_xchar = rs_send_xchar;
2167 serial_driver.wait_until_sent = rs_wait_until_sent;
2168 serial_driver.read_proc = rs_read_proc;
2171 * The callout device is just like normal device except for
2172 * major number and the subtype code.
2174 callout_driver = serial_driver;
2175 callout_driver.name = "cua";
2176 callout_driver.major = TTYAUX_MAJOR;
2177 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2178 callout_driver.read_proc = 0;
2179 callout_driver.proc_entry = 0;
2181 if (tty_register_driver(&serial_driver))
2182 panic("Couldn't register serial driver\n");
2183 if (tty_register_driver(&callout_driver))
2184 panic("Couldn't register callout driver\n");
2187 state->magic = SSTATE_MAGIC;
2188 state->port = (int)&custom.serdatr; /* Just to give it a value */
2190 state->custom_divisor = 0;
2191 state->close_delay = 5*HZ/10;
2192 state->closing_wait = 30*HZ;
2193 state->callout_termios = callout_driver.init_termios;
2194 state->normal_termios = serial_driver.init_termios;
2195 state->icount.cts = state->icount.dsr =
2196 state->icount.rng = state->icount.dcd = 0;
2197 state->icount.rx = state->icount.tx = 0;
2198 state->icount.frame = state->icount.parity = 0;
2199 state->icount.overrun = state->icount.brk = 0;
2201 if(state->port && check_region(state->port,REGION_LENGTH(state)))
2205 printk(KERN_INFO "ttyS%02d is the amiga builtin serial port\n",
2208 /* Hardware set up */
2210 state->baud_base = amiga_colorclock;
2211 state->xmit_fifo_size = 1;
2216 /* set ISRs, and then disable the rx interrupts */
2217 request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2218 request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2220 /* turn off Rx and Tx interrupts */
2221 custom.intena = IF_RBF | IF_TBE;
2224 /* clear any pending interrupt */
2225 custom.intreq = IF_RBF | IF_TBE;
2228 restore_flags (flags);
2231 * set the appropriate directions for the modem control flags,
2232 * and clear RTS and DTR
2234 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2235 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2240 static __exit void rs_exit(void)
2242 unsigned long flags;
2244 struct async_struct *info;
2246 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2249 remove_bh(SERIAL_BH);
2250 if ((e1 = tty_unregister_driver(&serial_driver)))
2251 printk("SERIAL: failed to unregister serial driver (%d)\n",
2253 if ((e2 = tty_unregister_driver(&callout_driver)))
2254 printk("SERIAL: failed to unregister callout driver (%d)\n",
2256 restore_flags(flags);
2258 info = rs_table[0].info;
2260 rs_table[0].info = NULL;
2265 free_page((unsigned long) tmp_buf);
2269 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2272 module_init(rs_init)
2273 module_exit(rs_exit)
2277 * ------------------------------------------------------------
2278 * Serial console driver
2279 * ------------------------------------------------------------
2281 #ifdef CONFIG_SERIAL_CONSOLE
2283 static void amiga_serial_putc(char c)
2285 custom.serdat = (unsigned char)c | 0x100;
2286 while (!(custom.serdatr & 0x2000))
2291 * Print a string to the serial port trying not to disturb
2292 * any possible real use of the port...
2294 * The console_lock must be held when we get here.
2296 static void serial_console_write(struct console *co, const char *s,
2299 unsigned short intena = custom.intenar;
2301 custom.intena = IF_TBE;
2305 amiga_serial_putc('\r');
2306 amiga_serial_putc(*s++);
2309 custom.intena = IF_SETCLR | (intena & IF_TBE);
2313 * Receive character from the serial port
2315 static int serial_console_wait_key(struct console *co)
2317 unsigned short intena = custom.intenar;
2320 custom.intena = IF_RBF;
2322 while (!(custom.intreqr & IF_RBF))
2324 ch = custom.serdatr & 0xff;
2325 custom.intreq = IF_RBF;
2327 custom.intena = IF_SETCLR | (intena & IF_RBF);
2332 static kdev_t serial_console_device(struct console *c)
2334 return MKDEV(TTY_MAJOR, 64);
2337 static struct console sercons = {
2339 serial_console_write,
2341 serial_console_device,
2342 serial_console_wait_key,
2354 void __init serial_console_init(void)
2356 register_console(&sercons);