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>
36 #undef SERIAL_PARANOIA_CHECK
37 #define SERIAL_DO_RESTART
39 /* Set of debugging defines */
41 #undef SERIAL_DEBUG_INTR
42 #undef SERIAL_DEBUG_OPEN
43 #undef SERIAL_DEBUG_FLOW
44 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
50 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
51 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
52 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
58 * End of serial driver configuration section.
61 #include <linux/module.h>
63 #include <linux/types.h>
64 #include <linux/serial.h>
65 #include <linux/serialP.h>
66 #include <linux/serial_reg.h>
67 static char *serial_version = "4.30";
69 #include <linux/errno.h>
70 #include <linux/signal.h>
71 #include <linux/sched.h>
72 #include <linux/kernel.h>
73 #include <linux/timer.h>
74 #include <linux/interrupt.h>
75 #include <linux/tty.h>
76 #include <linux/tty_flip.h>
77 #include <linux/console.h>
78 #include <linux/major.h>
79 #include <linux/string.h>
80 #include <linux/fcntl.h>
81 #include <linux/ptrace.h>
82 #include <linux/ioport.h>
84 #include <linux/slab.h>
85 #include <linux/init.h>
86 #include <linux/delay.h>
88 #include <asm/setup.h>
90 #include <asm/system.h>
93 #include <asm/bitops.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 (sizeof(rs_table)/sizeof(struct serial_state))
122 #define MIN(a,b) ((a) < (b) ? (a) : (b))
126 * tmp_buf is used as a temporary buffer by serial_write. We need to
127 * lock it in case the copy_from_user blocks while swapping in a page,
128 * and some other program tries to do a serial write at the same time.
129 * Since the lock will only come under contention when the system is
130 * swapping and available memory is low, it makes sense to share one
131 * buffer across all the serial ports, since it significantly saves
132 * memory if large numbers of serial ports are open.
134 static unsigned char *tmp_buf;
135 static DECLARE_MUTEX(tmp_buf_sem);
137 #include <asm/uaccess.h>
139 #define serial_isroot() (capable(CAP_SYS_ADMIN))
142 static inline int serial_paranoia_check(struct async_struct *info,
143 char *name, const char *routine)
145 #ifdef SERIAL_PARANOIA_CHECK
146 static const char *badmagic =
147 "Warning: bad magic number for serial struct (%s) in %s\n";
148 static const char *badinfo =
149 "Warning: null async_struct for (%s) in %s\n";
152 printk(badinfo, name, routine);
155 if (info->magic != SERIAL_MAGIC) {
156 printk(badmagic, name, routine);
163 /* some serial hardware definitions */
164 #define SDR_OVRUN (1<<15)
165 #define SDR_RBF (1<<14)
166 #define SDR_TBE (1<<13)
167 #define SDR_TSRE (1<<12)
169 #define SERPER_PARENB (1<<15)
171 #define AC_SETCLR (1<<15)
172 #define AC_UARTBRK (1<<11)
174 #define SER_DTR (1<<7)
175 #define SER_RTS (1<<6)
176 #define SER_DCD (1<<5)
177 #define SER_CTS (1<<4)
178 #define SER_DSR (1<<3)
180 static __inline__ void rtsdtr_ctrl(int bits)
182 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
186 * ------------------------------------------------------------
187 * rs_stop() and rs_start()
189 * This routines are called before setting or resetting tty->stopped.
190 * They enable or disable transmitter interrupts, as necessary.
191 * ------------------------------------------------------------
193 static void rs_stop(struct tty_struct *tty)
195 struct async_struct *info = (struct async_struct *)tty->driver_data;
198 if (serial_paranoia_check(info, tty->name, "rs_stop"))
201 local_irq_save(flags);
202 if (info->IER & UART_IER_THRI) {
203 info->IER &= ~UART_IER_THRI;
204 /* disable Tx interrupt and remove any pending interrupts */
205 custom.intena = IF_TBE;
207 custom.intreq = IF_TBE;
210 local_irq_restore(flags);
213 static void rs_start(struct tty_struct *tty)
215 struct async_struct *info = (struct async_struct *)tty->driver_data;
218 if (serial_paranoia_check(info, tty->name, "rs_start"))
221 local_irq_save(flags);
222 if (info->xmit.head != info->xmit.tail
224 && !(info->IER & UART_IER_THRI)) {
225 info->IER |= UART_IER_THRI;
226 custom.intena = IF_SETCLR | IF_TBE;
228 /* set a pending Tx Interrupt, transmitter should restart now */
229 custom.intreq = IF_SETCLR | IF_TBE;
232 local_irq_restore(flags);
236 * ----------------------------------------------------------------------
238 * Here starts the interrupt handling routines. All of the following
239 * subroutines are declared as inline and are folded into
240 * rs_interrupt(). They were separated out for readability's sake.
242 * Note: rs_interrupt() is a "fast" interrupt, which means that it
243 * runs with interrupts turned off. People who may want to modify
244 * rs_interrupt() should try to keep the interrupt handler as fast as
245 * possible. After you are done making modifications, it is not a bad
248 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
250 * and look at the resulting assemble code in serial.s.
252 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
253 * -----------------------------------------------------------------------
257 * This routine is used by the interrupt handler to schedule
258 * processing in the software interrupt portion of the driver.
260 static _INLINE_ void rs_sched_event(struct async_struct *info,
263 info->event |= 1 << event;
264 tasklet_schedule(&info->tlet);
267 static _INLINE_ void receive_chars(struct async_struct *info)
271 struct tty_struct *tty = info->tty;
273 struct async_icount *icount;
275 icount = &info->state->icount;
277 status = UART_LSR_DR; /* We obviously have a character! */
278 serdatr = custom.serdatr;
280 custom.intreq = IF_RBF;
283 if((serdatr & 0x1ff) == 0)
284 status |= UART_LSR_BI;
285 if(serdatr & SDR_OVRUN)
286 status |= UART_LSR_OE;
289 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
291 *tty->flip.char_buf_ptr = ch;
294 #ifdef SERIAL_DEBUG_INTR
295 printk("DR%02x:%02x...", ch, status);
297 *tty->flip.flag_buf_ptr = 0;
300 * We don't handle parity or frame errors - but I have left
301 * the code in, since I'm not sure that the errors can't be
305 if (status & (UART_LSR_BI | UART_LSR_PE |
306 UART_LSR_FE | UART_LSR_OE)) {
308 * For statistics only
310 if (status & UART_LSR_BI) {
311 status &= ~(UART_LSR_FE | UART_LSR_PE);
313 } else if (status & UART_LSR_PE)
315 else if (status & UART_LSR_FE)
317 if (status & UART_LSR_OE)
321 * Now check to see if character should be
322 * ignored, and mask off conditions which
325 if (status & info->ignore_status_mask)
328 status &= info->read_status_mask;
330 if (status & (UART_LSR_BI)) {
331 #ifdef SERIAL_DEBUG_INTR
332 printk("handling break....");
334 *tty->flip.flag_buf_ptr = TTY_BREAK;
335 if (info->flags & ASYNC_SAK)
337 } else if (status & UART_LSR_PE)
338 *tty->flip.flag_buf_ptr = TTY_PARITY;
339 else if (status & UART_LSR_FE)
340 *tty->flip.flag_buf_ptr = TTY_FRAME;
341 if (status & UART_LSR_OE) {
343 * Overrun is special, since it's
344 * reported immediately, and doesn't
345 * affect the current character
347 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
349 tty->flip.flag_buf_ptr++;
350 tty->flip.char_buf_ptr++;
351 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
355 tty->flip.flag_buf_ptr++;
356 tty->flip.char_buf_ptr++;
360 tty_flip_buffer_push(tty);
363 static _INLINE_ void transmit_chars(struct async_struct *info)
365 custom.intreq = IF_TBE;
368 custom.serdat = info->x_char | 0x100;
370 info->state->icount.tx++;
374 if (info->xmit.head == info->xmit.tail
375 || info->tty->stopped
376 || info->tty->hw_stopped) {
377 info->IER &= ~UART_IER_THRI;
378 custom.intena = IF_TBE;
383 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
385 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
386 info->state->icount.tx++;
388 if (CIRC_CNT(info->xmit.head,
390 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
391 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
393 #ifdef SERIAL_DEBUG_INTR
396 if (info->xmit.head == info->xmit.tail) {
397 custom.intena = IF_TBE;
399 info->IER &= ~UART_IER_THRI;
403 static _INLINE_ void check_modem_status(struct async_struct *info)
405 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
406 unsigned char dstatus;
407 struct async_icount *icount;
409 /* Determine bits that have changed */
410 dstatus = status ^ current_ctl_bits;
411 current_ctl_bits = status;
414 icount = &info->state->icount;
415 /* update input line counters */
416 if (dstatus & SER_DSR)
418 if (dstatus & SER_DCD) {
420 #ifdef CONFIG_HARD_PPS
421 if ((info->flags & ASYNC_HARDPPS_CD) &&
426 if (dstatus & SER_CTS)
428 wake_up_interruptible(&info->delta_msr_wait);
431 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
432 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
433 printk("ttyS%02d CD now %s...", info->line,
434 (!(status & SER_DCD)) ? "on" : "off");
436 if (!(status & SER_DCD))
437 wake_up_interruptible(&info->open_wait);
439 #ifdef SERIAL_DEBUG_OPEN
440 printk("doing serial hangup...");
443 tty_hangup(info->tty);
446 if (info->flags & ASYNC_CTS_FLOW) {
447 if (info->tty->hw_stopped) {
448 if (!(status & SER_CTS)) {
449 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
450 printk("CTS tx start...");
452 info->tty->hw_stopped = 0;
453 info->IER |= UART_IER_THRI;
454 custom.intena = IF_SETCLR | IF_TBE;
456 /* set a pending Tx Interrupt, transmitter should restart now */
457 custom.intreq = IF_SETCLR | IF_TBE;
459 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
463 if ((status & SER_CTS)) {
464 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
465 printk("CTS tx stop...");
467 info->tty->hw_stopped = 1;
468 info->IER &= ~UART_IER_THRI;
469 /* disable Tx interrupt and remove any pending interrupts */
470 custom.intena = IF_TBE;
472 custom.intreq = IF_TBE;
479 static irqreturn_t ser_vbl_int( int irq, void *data, struct pt_regs *regs)
481 /* vbl is just a periodic interrupt we tie into to update modem status */
482 struct async_struct * info = IRQ_ports;
484 * TBD - is it better to unregister from this interrupt or to
485 * ignore it if MSI is clear ?
487 if(info->IER & UART_IER_MSI)
488 check_modem_status(info);
492 static irqreturn_t ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
494 struct async_struct * info;
496 #ifdef SERIAL_DEBUG_INTR
497 printk("ser_rx_int...");
501 if (!info || !info->tty)
505 info->last_active = jiffies;
506 #ifdef SERIAL_DEBUG_INTR
512 static irqreturn_t ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
514 struct async_struct * info;
516 if (custom.serdatr & SDR_TBE) {
517 #ifdef SERIAL_DEBUG_INTR
518 printk("ser_tx_int...");
522 if (!info || !info->tty)
525 transmit_chars(info);
526 info->last_active = jiffies;
527 #ifdef SERIAL_DEBUG_INTR
535 * -------------------------------------------------------------------
536 * Here ends the serial interrupt routines.
537 * -------------------------------------------------------------------
541 * This routine is used to handle the "bottom half" processing for the
542 * serial driver, known also the "software interrupt" processing.
543 * This processing is done at the kernel interrupt level, after the
544 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
545 * is where time-consuming activities which can not be done in the
546 * interrupt driver proper are done; the interrupt driver schedules
547 * them using rs_sched_event(), and they get done here.
550 static void do_softint(unsigned long private_)
552 struct async_struct *info = (struct async_struct *) private_;
553 struct tty_struct *tty;
559 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
560 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
561 tty->ldisc.write_wakeup)
562 (tty->ldisc.write_wakeup)(tty);
563 wake_up_interruptible(&tty->write_wait);
568 * ---------------------------------------------------------------
569 * Low level utility subroutines for the serial driver: routines to
570 * figure out the appropriate timeout for an interrupt chain, routines
571 * to initialize and startup a serial port, and routines to shutdown a
572 * serial port. Useful stuff like that.
573 * ---------------------------------------------------------------
576 static int startup(struct async_struct * info)
582 page = get_zeroed_page(GFP_KERNEL);
586 local_irq_save(flags);
588 if (info->flags & ASYNC_INITIALIZED) {
596 info->xmit.buf = (unsigned char *) page;
598 #ifdef SERIAL_DEBUG_OPEN
599 printk("starting up ttys%d ...", info->line);
602 /* Clear anything in the input buffer */
604 custom.intreq = IF_RBF;
607 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
609 if (serial_isroot()) {
611 set_bit(TTY_IO_ERROR,
618 /* enable both Rx and Tx interrupts */
619 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
621 info->IER = UART_IER_MSI;
623 /* remember current state of the DCD and CTS bits */
624 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
629 if (info->tty->termios->c_cflag & CBAUD)
630 info->MCR = SER_DTR | SER_RTS;
631 rtsdtr_ctrl(info->MCR);
634 clear_bit(TTY_IO_ERROR, &info->tty->flags);
635 info->xmit.head = info->xmit.tail = 0;
638 * Set up the tty->alt_speed kludge
641 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
642 info->tty->alt_speed = 57600;
643 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
644 info->tty->alt_speed = 115200;
645 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
646 info->tty->alt_speed = 230400;
647 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
648 info->tty->alt_speed = 460800;
652 * and set the speed of the serial port
654 change_speed(info, 0);
656 info->flags |= ASYNC_INITIALIZED;
657 local_irq_restore(flags);
661 local_irq_restore(flags);
666 * This routine will shutdown a serial port; interrupts are disabled, and
667 * DTR is dropped if the hangup on close termio flag is on.
669 static void shutdown(struct async_struct * info)
672 struct serial_state *state;
674 if (!(info->flags & ASYNC_INITIALIZED))
679 #ifdef SERIAL_DEBUG_OPEN
680 printk("Shutting down serial port %d ....\n", info->line);
683 local_irq_save(flags); /* Disable interrupts */
686 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
687 * here so the queue might never be waken up
689 wake_up_interruptible(&info->delta_msr_wait);
694 * Free the IRQ, if necessary
696 free_irq(IRQ_AMIGA_VERTB, info);
698 if (info->xmit.buf) {
699 free_page((unsigned long) info->xmit.buf);
704 custom.intena = IF_RBF | IF_TBE;
707 /* disable break condition */
708 custom.adkcon = AC_UARTBRK;
711 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
712 info->MCR &= ~(SER_DTR|SER_RTS);
713 rtsdtr_ctrl(info->MCR);
716 set_bit(TTY_IO_ERROR, &info->tty->flags);
718 info->flags &= ~ASYNC_INITIALIZED;
719 local_irq_restore(flags);
724 * This routine is called to set the UART divisor registers to match
725 * the specified baud rate for a serial port.
727 static void change_speed(struct async_struct *info,
728 struct termios *old_termios)
730 int quot = 0, baud_base, baud;
731 unsigned cflag, cval = 0;
735 if (!info->tty || !info->tty->termios)
737 cflag = info->tty->termios->c_cflag;
739 /* Byte size is always 8 bits plus parity bit if requested */
742 if (cflag & CSTOPB) {
746 if (cflag & PARENB) {
747 cval |= UART_LCR_PARITY;
750 if (!(cflag & PARODD))
751 cval |= UART_LCR_EPAR;
754 cval |= UART_LCR_SPAR;
757 /* Determine divisor based on baud rate */
758 baud = tty_get_baud_rate(info->tty);
760 baud = 9600; /* B0 transition handled in rs_set_termios */
761 baud_base = info->state->baud_base;
763 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
764 quot = info->state->custom_divisor;
767 /* Special case since 134 is really 134.5 */
768 quot = (2*baud_base / 269);
770 quot = baud_base / baud;
772 /* If the quotient is zero refuse the change */
773 if (!quot && old_termios) {
774 info->tty->termios->c_cflag &= ~CBAUD;
775 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
776 baud = tty_get_baud_rate(info->tty);
780 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
781 quot = info->state->custom_divisor;
784 /* Special case since 134 is really 134.5 */
785 quot = (2*baud_base / 269);
787 quot = baud_base / baud;
790 /* As a last resort, if the quotient is zero, default to 9600 bps */
792 quot = baud_base / 9600;
794 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
795 info->timeout += HZ/50; /* Add .02 seconds of slop */
797 /* CTS flow control flag and modem status interrupts */
798 info->IER &= ~UART_IER_MSI;
799 if (info->flags & ASYNC_HARDPPS_CD)
800 info->IER |= UART_IER_MSI;
801 if (cflag & CRTSCTS) {
802 info->flags |= ASYNC_CTS_FLOW;
803 info->IER |= UART_IER_MSI;
805 info->flags &= ~ASYNC_CTS_FLOW;
807 info->flags &= ~ASYNC_CHECK_CD;
809 info->flags |= ASYNC_CHECK_CD;
810 info->IER |= UART_IER_MSI;
813 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
817 * Set up parity check flag
819 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
821 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
822 if (I_INPCK(info->tty))
823 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
824 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
825 info->read_status_mask |= UART_LSR_BI;
828 * Characters to ignore
830 info->ignore_status_mask = 0;
831 if (I_IGNPAR(info->tty))
832 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
833 if (I_IGNBRK(info->tty)) {
834 info->ignore_status_mask |= UART_LSR_BI;
836 * If we're ignore parity and break indicators, ignore
837 * overruns too. (For real raw support).
839 if (I_IGNPAR(info->tty))
840 info->ignore_status_mask |= UART_LSR_OE;
843 * !!! ignore all characters if CREAD is not set
845 if ((cflag & CREAD) == 0)
846 info->ignore_status_mask |= UART_LSR_DR;
847 local_irq_save(flags);
852 /* Set up the baud rate */
855 /* Enable or disable parity bit */
857 if(cval & UART_LCR_PARITY)
858 serper |= (SERPER_PARENB);
860 custom.serper = serper;
864 info->LCR = cval; /* Save LCR */
865 local_irq_restore(flags);
868 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
870 struct async_struct *info = (struct async_struct *)tty->driver_data;
873 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
876 if (!tty || !info->xmit.buf)
879 local_irq_save(flags);
880 if (CIRC_SPACE(info->xmit.head,
882 SERIAL_XMIT_SIZE) == 0) {
883 local_irq_restore(flags);
887 info->xmit.buf[info->xmit.head++] = ch;
888 info->xmit.head &= SERIAL_XMIT_SIZE-1;
889 local_irq_restore(flags);
892 static void rs_flush_chars(struct tty_struct *tty)
894 struct async_struct *info = (struct async_struct *)tty->driver_data;
897 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
900 if (info->xmit.head == info->xmit.tail
906 local_irq_save(flags);
907 info->IER |= UART_IER_THRI;
908 custom.intena = IF_SETCLR | IF_TBE;
910 /* set a pending Tx Interrupt, transmitter should restart now */
911 custom.intreq = IF_SETCLR | IF_TBE;
913 local_irq_restore(flags);
916 static int rs_write(struct tty_struct * tty, int from_user,
917 const unsigned char *buf, int count)
920 struct async_struct *info = (struct async_struct *)tty->driver_data;
923 if (serial_paranoia_check(info, tty->name, "rs_write"))
926 if (!tty || !info->xmit.buf || !tmp_buf)
929 local_save_flags(flags);
934 c = CIRC_SPACE_TO_END(info->xmit.head,
940 c -= copy_from_user(tmp_buf, buf, c);
947 c1 = CIRC_SPACE_TO_END(info->xmit.head,
952 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
953 info->xmit.head = ((info->xmit.head + c) &
954 (SERIAL_XMIT_SIZE-1));
955 local_irq_restore(flags);
964 c = CIRC_SPACE_TO_END(info->xmit.head,
972 memcpy(info->xmit.buf + info->xmit.head, buf, c);
973 info->xmit.head = ((info->xmit.head + c) &
974 (SERIAL_XMIT_SIZE-1));
979 local_irq_restore(flags);
981 if (info->xmit.head != info->xmit.tail
984 && !(info->IER & UART_IER_THRI)) {
985 info->IER |= UART_IER_THRI;
987 custom.intena = IF_SETCLR | IF_TBE;
989 /* set a pending Tx Interrupt, transmitter should restart now */
990 custom.intreq = IF_SETCLR | IF_TBE;
992 local_irq_restore(flags);
997 static int rs_write_room(struct tty_struct *tty)
999 struct async_struct *info = (struct async_struct *)tty->driver_data;
1001 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1003 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1006 static int rs_chars_in_buffer(struct tty_struct *tty)
1008 struct async_struct *info = (struct async_struct *)tty->driver_data;
1010 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1012 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1015 static void rs_flush_buffer(struct tty_struct *tty)
1017 struct async_struct *info = (struct async_struct *)tty->driver_data;
1018 unsigned long flags;
1020 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1022 local_irq_save(flags);
1023 info->xmit.head = info->xmit.tail = 0;
1024 local_irq_restore(flags);
1025 wake_up_interruptible(&tty->write_wait);
1026 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1027 tty->ldisc.write_wakeup)
1028 (tty->ldisc.write_wakeup)(tty);
1032 * This function is used to send a high-priority XON/XOFF character to
1035 static void rs_send_xchar(struct tty_struct *tty, char ch)
1037 struct async_struct *info = (struct async_struct *)tty->driver_data;
1038 unsigned long flags;
1040 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1045 /* Make sure transmit interrupts are on */
1048 local_irq_save(flags);
1049 if(!(custom.intenar & IF_TBE)) {
1050 custom.intena = IF_SETCLR | IF_TBE;
1052 /* set a pending Tx Interrupt, transmitter should restart now */
1053 custom.intreq = IF_SETCLR | IF_TBE;
1056 local_irq_restore(flags);
1058 info->IER |= UART_IER_THRI;
1063 * ------------------------------------------------------------
1066 * This routine is called by the upper-layer tty layer to signal that
1067 * incoming characters should be throttled.
1068 * ------------------------------------------------------------
1070 static void rs_throttle(struct tty_struct * tty)
1072 struct async_struct *info = (struct async_struct *)tty->driver_data;
1073 unsigned long flags;
1074 #ifdef SERIAL_DEBUG_THROTTLE
1077 printk("throttle %s: %d....\n", tty_name(tty, buf),
1078 tty->ldisc.chars_in_buffer(tty));
1081 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1085 rs_send_xchar(tty, STOP_CHAR(tty));
1087 if (tty->termios->c_cflag & CRTSCTS)
1088 info->MCR &= ~SER_RTS;
1090 local_irq_save(flags);
1091 rtsdtr_ctrl(info->MCR);
1092 local_irq_restore(flags);
1095 static void rs_unthrottle(struct tty_struct * tty)
1097 struct async_struct *info = (struct async_struct *)tty->driver_data;
1098 unsigned long flags;
1099 #ifdef SERIAL_DEBUG_THROTTLE
1102 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1103 tty->ldisc.chars_in_buffer(tty));
1106 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1113 rs_send_xchar(tty, START_CHAR(tty));
1115 if (tty->termios->c_cflag & CRTSCTS)
1116 info->MCR |= SER_RTS;
1117 local_irq_save(flags);
1118 rtsdtr_ctrl(info->MCR);
1119 local_irq_restore(flags);
1123 * ------------------------------------------------------------
1124 * rs_ioctl() and friends
1125 * ------------------------------------------------------------
1128 static int get_serial_info(struct async_struct * info,
1129 struct serial_struct * retinfo)
1131 struct serial_struct tmp;
1132 struct serial_state *state = info->state;
1136 memset(&tmp, 0, sizeof(tmp));
1137 tmp.type = state->type;
1138 tmp.line = state->line;
1139 tmp.port = state->port;
1140 tmp.irq = state->irq;
1141 tmp.flags = state->flags;
1142 tmp.xmit_fifo_size = state->xmit_fifo_size;
1143 tmp.baud_base = state->baud_base;
1144 tmp.close_delay = state->close_delay;
1145 tmp.closing_wait = state->closing_wait;
1146 tmp.custom_divisor = state->custom_divisor;
1147 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1152 static int set_serial_info(struct async_struct * info,
1153 struct serial_struct * new_info)
1155 struct serial_struct new_serial;
1156 struct serial_state old_state, *state;
1157 unsigned int change_irq,change_port;
1160 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1162 state = info->state;
1165 change_irq = new_serial.irq != state->irq;
1166 change_port = (new_serial.port != state->port);
1167 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1170 if (!serial_isroot()) {
1171 if ((new_serial.baud_base != state->baud_base) ||
1172 (new_serial.close_delay != state->close_delay) ||
1173 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1174 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1175 (state->flags & ~ASYNC_USR_MASK)))
1177 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1178 (new_serial.flags & ASYNC_USR_MASK));
1179 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1180 (new_serial.flags & ASYNC_USR_MASK));
1181 state->custom_divisor = new_serial.custom_divisor;
1182 goto check_and_exit;
1185 if (new_serial.baud_base < 9600)
1189 * OK, past this point, all the error checking has been done.
1190 * At this point, we start making changes.....
1193 state->baud_base = new_serial.baud_base;
1194 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1195 (new_serial.flags & ASYNC_FLAGS));
1196 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1197 (info->flags & ASYNC_INTERNAL_FLAGS));
1198 state->custom_divisor = new_serial.custom_divisor;
1199 state->close_delay = new_serial.close_delay * HZ/100;
1200 state->closing_wait = new_serial.closing_wait * HZ/100;
1201 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1204 if (info->flags & ASYNC_INITIALIZED) {
1205 if (((old_state.flags & ASYNC_SPD_MASK) !=
1206 (state->flags & ASYNC_SPD_MASK)) ||
1207 (old_state.custom_divisor != state->custom_divisor)) {
1208 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1209 info->tty->alt_speed = 57600;
1210 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1211 info->tty->alt_speed = 115200;
1212 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1213 info->tty->alt_speed = 230400;
1214 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1215 info->tty->alt_speed = 460800;
1216 change_speed(info, 0);
1219 retval = startup(info);
1225 * get_lsr_info - get line status register info
1227 * Purpose: Let user call ioctl() to get info when the UART physically
1228 * is emptied. On bus types like RS485, the transmitter must
1229 * release the bus after transmitting. This must be done when
1230 * the transmit shift register is empty, not be done when the
1231 * transmit holding register is empty. This functionality
1232 * allows an RS485 driver to be written in user space.
1234 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1236 unsigned char status;
1237 unsigned int result;
1238 unsigned long flags;
1240 local_irq_save(flags);
1241 status = custom.serdatr;
1243 local_irq_restore(flags);
1244 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1245 if (copy_to_user(value, &result, sizeof(int)))
1251 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1253 struct async_struct * info = (struct async_struct *)tty->driver_data;
1254 unsigned char control, status;
1255 unsigned long flags;
1257 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1259 if (tty->flags & (1 << TTY_IO_ERROR))
1262 control = info->MCR;
1263 local_irq_save(flags);
1265 local_irq_restore(flags);
1266 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1267 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1268 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1269 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1270 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1273 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1274 unsigned int set, unsigned int clear)
1276 struct async_struct * info = (struct async_struct *)tty->driver_data;
1277 unsigned long flags;
1279 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1281 if (tty->flags & (1 << TTY_IO_ERROR))
1284 local_irq_save(flags);
1285 if (set & TIOCM_RTS)
1286 info->MCR |= SER_RTS;
1287 if (set & TIOCM_DTR)
1288 info->MCR |= SER_DTR;
1289 if (clear & TIOCM_RTS)
1290 info->MCR &= ~SER_RTS;
1291 if (clear & TIOCM_DTR)
1292 info->MCR &= ~SER_DTR;
1293 rtsdtr_ctrl(info->MCR);
1294 local_irq_restore(flags);
1299 * rs_break() --- routine which turns the break handling on or off
1301 static void rs_break(struct tty_struct *tty, int break_state)
1303 struct async_struct * info = (struct async_struct *)tty->driver_data;
1304 unsigned long flags;
1306 if (serial_paranoia_check(info, tty->name, "rs_break"))
1309 local_irq_save(flags);
1310 if (break_state == -1)
1311 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1313 custom.adkcon = AC_UARTBRK;
1315 local_irq_restore(flags);
1319 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1320 unsigned int cmd, unsigned long arg)
1322 struct async_struct * info = (struct async_struct *)tty->driver_data;
1323 struct async_icount cprev, cnow; /* kernel counter temps */
1324 struct serial_icounter_struct icount;
1325 unsigned long flags;
1327 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1330 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1331 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1332 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1333 if (tty->flags & (1 << TTY_IO_ERROR))
1339 return get_serial_info(info,
1340 (struct serial_struct *) arg);
1342 return set_serial_info(info,
1343 (struct serial_struct *) arg);
1347 case TIOCSERGETLSR: /* Get line status register */
1348 return get_lsr_info(info, (unsigned int *) arg);
1350 case TIOCSERGSTRUCT:
1351 if (copy_to_user((struct async_struct *) arg,
1352 info, sizeof(struct async_struct)))
1357 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1358 * - mask passed in arg for lines of interest
1359 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1360 * Caller should use TIOCGICOUNT to see which one it was
1363 local_irq_save(flags);
1364 /* note the counters on entry */
1365 cprev = info->state->icount;
1366 local_irq_restore(flags);
1368 interruptible_sleep_on(&info->delta_msr_wait);
1369 /* see if a signal did it */
1370 if (signal_pending(current))
1371 return -ERESTARTSYS;
1372 local_irq_save(flags);
1373 cnow = info->state->icount; /* atomic copy */
1374 local_irq_restore(flags);
1375 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1376 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1377 return -EIO; /* no change => error */
1378 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1379 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1380 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1381 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1389 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1390 * Return: write counters to the user passed counter struct
1391 * NB: both 1->0 and 0->1 transitions are counted except for
1392 * RI where only 0->1 is counted.
1395 local_irq_save(flags);
1396 cnow = info->state->icount;
1397 local_irq_restore(flags);
1398 icount.cts = cnow.cts;
1399 icount.dsr = cnow.dsr;
1400 icount.rng = cnow.rng;
1401 icount.dcd = cnow.dcd;
1402 icount.rx = cnow.rx;
1403 icount.tx = cnow.tx;
1404 icount.frame = cnow.frame;
1405 icount.overrun = cnow.overrun;
1406 icount.parity = cnow.parity;
1407 icount.brk = cnow.brk;
1408 icount.buf_overrun = cnow.buf_overrun;
1410 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1415 /* "setserial -W" is called in Debian boot */
1416 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1420 return -ENOIOCTLCMD;
1425 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1427 struct async_struct *info = (struct async_struct *)tty->driver_data;
1428 unsigned long flags;
1429 unsigned int cflag = tty->termios->c_cflag;
1431 if ( (cflag == old_termios->c_cflag)
1432 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1433 == RELEVANT_IFLAG(old_termios->c_iflag)))
1436 change_speed(info, old_termios);
1438 /* Handle transition to B0 status */
1439 if ((old_termios->c_cflag & CBAUD) &&
1441 info->MCR &= ~(SER_DTR|SER_RTS);
1442 local_irq_save(flags);
1443 rtsdtr_ctrl(info->MCR);
1444 local_irq_restore(flags);
1447 /* Handle transition away from B0 status */
1448 if (!(old_termios->c_cflag & CBAUD) &&
1450 info->MCR |= SER_DTR;
1451 if (!(tty->termios->c_cflag & CRTSCTS) ||
1452 !test_bit(TTY_THROTTLED, &tty->flags)) {
1453 info->MCR |= SER_RTS;
1455 local_irq_save(flags);
1456 rtsdtr_ctrl(info->MCR);
1457 local_irq_restore(flags);
1460 /* Handle turning off CRTSCTS */
1461 if ((old_termios->c_cflag & CRTSCTS) &&
1462 !(tty->termios->c_cflag & CRTSCTS)) {
1463 tty->hw_stopped = 0;
1469 * No need to wake up processes in open wait, since they
1470 * sample the CLOCAL flag once, and don't recheck it.
1471 * XXX It's not clear whether the current behavior is correct
1472 * or not. Hence, this may change.....
1474 if (!(old_termios->c_cflag & CLOCAL) &&
1475 (tty->termios->c_cflag & CLOCAL))
1476 wake_up_interruptible(&info->open_wait);
1481 * ------------------------------------------------------------
1484 * This routine is called when the serial port gets closed. First, we
1485 * wait for the last remaining data to be sent. Then, we unlink its
1486 * async structure from the interrupt chain if necessary, and we free
1487 * that IRQ if nothing is left in the chain.
1488 * ------------------------------------------------------------
1490 static void rs_close(struct tty_struct *tty, struct file * filp)
1492 struct async_struct * info = (struct async_struct *)tty->driver_data;
1493 struct serial_state *state;
1494 unsigned long flags;
1496 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1499 state = info->state;
1501 local_irq_save(flags);
1503 if (tty_hung_up_p(filp)) {
1504 DBG_CNT("before DEC-hung");
1505 local_irq_restore(flags);
1509 #ifdef SERIAL_DEBUG_OPEN
1510 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1512 if ((tty->count == 1) && (state->count != 1)) {
1514 * Uh, oh. tty->count is 1, which means that the tty
1515 * structure will be freed. state->count should always
1516 * be one in these conditions. If it's greater than
1517 * one, we've got real problems, since it means the
1518 * serial port won't be shutdown.
1520 printk("rs_close: bad serial port count; tty->count is 1, "
1521 "state->count is %d\n", state->count);
1524 if (--state->count < 0) {
1525 printk("rs_close: bad serial port count for ttys%d: %d\n",
1526 info->line, state->count);
1530 DBG_CNT("before DEC-2");
1531 local_irq_restore(flags);
1534 info->flags |= ASYNC_CLOSING;
1536 * Now we wait for the transmit buffer to clear; and we notify
1537 * the line discipline to only process XON/XOFF characters.
1540 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1541 tty_wait_until_sent(tty, info->closing_wait);
1543 * At this point we stop accepting input. To do this, we
1544 * disable the receive line status interrupts, and tell the
1545 * interrupt driver to stop checking the data ready bit in the
1546 * line status register.
1548 info->read_status_mask &= ~UART_LSR_DR;
1549 if (info->flags & ASYNC_INITIALIZED) {
1550 /* disable receive interrupts */
1551 custom.intena = IF_RBF;
1553 /* clear any pending receive interrupt */
1554 custom.intreq = IF_RBF;
1558 * Before we drop DTR, make sure the UART transmitter
1559 * has completely drained; this is especially
1560 * important if there is a transmit FIFO!
1562 rs_wait_until_sent(tty, info->timeout);
1565 if (tty->driver->flush_buffer)
1566 tty->driver->flush_buffer(tty);
1567 if (tty->ldisc.flush_buffer)
1568 tty->ldisc.flush_buffer(tty);
1572 if (info->blocked_open) {
1573 if (info->close_delay) {
1574 current->state = TASK_INTERRUPTIBLE;
1575 schedule_timeout(info->close_delay);
1577 wake_up_interruptible(&info->open_wait);
1579 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1580 wake_up_interruptible(&info->close_wait);
1581 local_irq_restore(flags);
1585 * rs_wait_until_sent() --- wait until the transmitter is empty
1587 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1589 struct async_struct * info = (struct async_struct *)tty->driver_data;
1590 unsigned long orig_jiffies, char_time;
1593 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1596 if (info->xmit_fifo_size == 0)
1597 return; /* Just in case.... */
1599 orig_jiffies = jiffies;
1601 * Set the check interval to be 1/5 of the estimated time to
1602 * send a single character, and make it at least 1. The check
1603 * interval should also be less than the timeout.
1605 * Note: we have to use pretty tight timings here to satisfy
1608 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1609 char_time = char_time / 5;
1613 char_time = MIN(char_time, timeout);
1615 * If the transmitter hasn't cleared in twice the approximate
1616 * amount of time to send the entire FIFO, it probably won't
1617 * ever clear. This assumes the UART isn't doing flow
1618 * control, which is currently the case. Hence, if it ever
1619 * takes longer than info->timeout, this is probably due to a
1620 * UART bug of some kind. So, we clamp the timeout parameter at
1623 if (!timeout || timeout > 2*info->timeout)
1624 timeout = 2*info->timeout;
1625 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1626 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1627 printk("jiff=%lu...", jiffies);
1629 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1630 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1631 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1633 current->state = TASK_INTERRUPTIBLE;
1634 schedule_timeout(char_time);
1635 if (signal_pending(current))
1637 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1640 current->state = TASK_RUNNING;
1641 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1642 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1647 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1649 static void rs_hangup(struct tty_struct *tty)
1651 struct async_struct * info = (struct async_struct *)tty->driver_data;
1652 struct serial_state *state = info->state;
1654 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1657 state = info->state;
1659 rs_flush_buffer(tty);
1663 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1665 wake_up_interruptible(&info->open_wait);
1669 * ------------------------------------------------------------
1670 * rs_open() and friends
1671 * ------------------------------------------------------------
1673 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1674 struct async_struct *info)
1676 #ifdef DECLARE_WAITQUEUE
1677 DECLARE_WAITQUEUE(wait, current);
1679 struct wait_queue wait = { current, NULL };
1681 struct serial_state *state = info->state;
1683 int do_clocal = 0, extra_count = 0;
1684 unsigned long flags;
1687 * If the device is in the middle of being closed, then block
1688 * until it's done, and then try again.
1690 if (tty_hung_up_p(filp) ||
1691 (info->flags & ASYNC_CLOSING)) {
1692 if (info->flags & ASYNC_CLOSING)
1693 interruptible_sleep_on(&info->close_wait);
1694 #ifdef SERIAL_DO_RESTART
1695 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1696 -EAGAIN : -ERESTARTSYS);
1703 * If non-blocking mode is set, or the port is not enabled,
1704 * then make the check up front and then exit.
1706 if ((filp->f_flags & O_NONBLOCK) ||
1707 (tty->flags & (1 << TTY_IO_ERROR))) {
1708 info->flags |= ASYNC_NORMAL_ACTIVE;
1712 if (tty->termios->c_cflag & CLOCAL)
1716 * Block waiting for the carrier detect and the line to become
1717 * free (i.e., not in use by the callout). While we are in
1718 * this loop, state->count is dropped by one, so that
1719 * rs_close() knows when to free things. We restore it upon
1720 * exit, either normal or abnormal.
1723 add_wait_queue(&info->open_wait, &wait);
1724 #ifdef SERIAL_DEBUG_OPEN
1725 printk("block_til_ready before block: ttys%d, count = %d\n",
1726 state->line, state->count);
1728 local_irq_save(flags);
1729 if (!tty_hung_up_p(filp)) {
1733 local_irq_restore(flags);
1734 info->blocked_open++;
1736 local_irq_save(flags);
1737 if (tty->termios->c_cflag & CBAUD)
1738 rtsdtr_ctrl(SER_DTR|SER_RTS);
1739 local_irq_restore(flags);
1740 set_current_state(TASK_INTERRUPTIBLE);
1741 if (tty_hung_up_p(filp) ||
1742 !(info->flags & ASYNC_INITIALIZED)) {
1743 #ifdef SERIAL_DO_RESTART
1744 if (info->flags & ASYNC_HUP_NOTIFY)
1747 retval = -ERESTARTSYS;
1753 if (!(info->flags & ASYNC_CLOSING) &&
1754 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1756 if (signal_pending(current)) {
1757 retval = -ERESTARTSYS;
1760 #ifdef SERIAL_DEBUG_OPEN
1761 printk("block_til_ready blocking: ttys%d, count = %d\n",
1762 info->line, state->count);
1766 current->state = TASK_RUNNING;
1767 remove_wait_queue(&info->open_wait, &wait);
1770 info->blocked_open--;
1771 #ifdef SERIAL_DEBUG_OPEN
1772 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1773 info->line, state->count);
1777 info->flags |= ASYNC_NORMAL_ACTIVE;
1781 static int get_async_struct(int line, struct async_struct **ret_info)
1783 struct async_struct *info;
1784 struct serial_state *sstate;
1786 sstate = rs_table + line;
1789 *ret_info = sstate->info;
1792 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1797 memset(info, 0, sizeof(struct async_struct));
1798 #ifdef DECLARE_WAITQUEUE
1799 init_waitqueue_head(&info->open_wait);
1800 init_waitqueue_head(&info->close_wait);
1801 init_waitqueue_head(&info->delta_msr_wait);
1803 info->magic = SERIAL_MAGIC;
1804 info->port = sstate->port;
1805 info->flags = sstate->flags;
1806 info->xmit_fifo_size = sstate->xmit_fifo_size;
1808 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1809 info->state = sstate;
1812 *ret_info = sstate->info;
1815 *ret_info = sstate->info = info;
1820 * This routine is called whenever a serial port is opened. It
1821 * enables interrupts for a serial port, linking in its async structure into
1822 * the IRQ chain. It also performs the serial-specific
1823 * initialization for the tty structure.
1825 static int rs_open(struct tty_struct *tty, struct file * filp)
1827 struct async_struct *info;
1832 if ((line < 0) || (line >= NR_PORTS)) {
1835 retval = get_async_struct(line, &info);
1839 tty->driver_data = info;
1841 if (serial_paranoia_check(info, tty->name, "rs_open"))
1844 #ifdef SERIAL_DEBUG_OPEN
1845 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1847 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1850 page = get_zeroed_page(GFP_KERNEL);
1857 tmp_buf = (unsigned char *) page;
1861 * If the port is the middle of closing, bail out now
1863 if (tty_hung_up_p(filp) ||
1864 (info->flags & ASYNC_CLOSING)) {
1865 if (info->flags & ASYNC_CLOSING)
1866 interruptible_sleep_on(&info->close_wait);
1867 #ifdef SERIAL_DO_RESTART
1868 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1869 -EAGAIN : -ERESTARTSYS);
1876 * Start up serial port
1878 retval = startup(info);
1883 retval = block_til_ready(tty, filp, info);
1885 #ifdef SERIAL_DEBUG_OPEN
1886 printk("rs_open returning after block_til_ready with %d\n",
1892 #ifdef SERIAL_DEBUG_OPEN
1893 printk("rs_open %s successful...", tty->name);
1899 * /proc fs routines....
1902 static inline int line_info(char *buf, struct serial_state *state)
1904 struct async_struct *info = state->info, scr_info;
1905 char stat_buf[30], control, status;
1907 unsigned long flags;
1909 ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
1912 * Figure out the current RS-232 lines
1915 info = &scr_info; /* This is just for serial_{in,out} */
1917 info->magic = SERIAL_MAGIC;
1918 info->flags = state->flags;
1922 local_irq_save(flags);
1924 control = info ? info->MCR : status;
1925 local_irq_restore(flags);
1929 if(!(control & SER_RTS))
1930 strcat(stat_buf, "|RTS");
1931 if(!(status & SER_CTS))
1932 strcat(stat_buf, "|CTS");
1933 if(!(control & SER_DTR))
1934 strcat(stat_buf, "|DTR");
1935 if(!(status & SER_DSR))
1936 strcat(stat_buf, "|DSR");
1937 if(!(status & SER_DCD))
1938 strcat(stat_buf, "|CD");
1941 ret += sprintf(buf+ret, " baud:%d",
1942 state->baud_base / info->quot);
1945 ret += sprintf(buf+ret, " tx:%d rx:%d",
1946 state->icount.tx, state->icount.rx);
1948 if (state->icount.frame)
1949 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
1951 if (state->icount.parity)
1952 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
1954 if (state->icount.brk)
1955 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
1957 if (state->icount.overrun)
1958 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
1961 * Last thing is the RS-232 status lines
1963 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1967 static int rs_read_proc(char *page, char **start, off_t off, int count,
1968 int *eof, void *data)
1973 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
1974 l = line_info(page + len, &rs_table[0]);
1976 if (len+begin > off+count)
1978 if (len+begin < off) {
1984 if (off >= len+begin)
1986 *start = page + (off-begin);
1987 return ((count < begin+len-off) ? count : begin+len-off);
1991 * ---------------------------------------------------------------------
1992 * rs_init() and friends
1994 * rs_init() is called at boot-time to initialize the serial driver.
1995 * ---------------------------------------------------------------------
1999 * This routine prints out the appropriate serial driver version
2000 * number, and identifies which options were configured into this
2003 static _INLINE_ void show_serial_version(void)
2005 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2009 int register_serial(struct serial_struct *req);
2010 void unregister_serial(int line);
2013 static struct tty_operations serial_ops = {
2017 .put_char = rs_put_char,
2018 .flush_chars = rs_flush_chars,
2019 .write_room = rs_write_room,
2020 .chars_in_buffer = rs_chars_in_buffer,
2021 .flush_buffer = rs_flush_buffer,
2023 .throttle = rs_throttle,
2024 .unthrottle = rs_unthrottle,
2025 .set_termios = rs_set_termios,
2028 .hangup = rs_hangup,
2029 .break_ctl = rs_break,
2030 .send_xchar = rs_send_xchar,
2031 .wait_until_sent = rs_wait_until_sent,
2032 .read_proc = rs_read_proc,
2033 .tiocmget = rs_tiocmget,
2034 .tiocmset = rs_tiocmset,
2038 * The serial driver boot-time initialization code!
2040 static int __init rs_init(void)
2042 unsigned long flags;
2043 struct serial_state * state;
2045 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2048 serial_driver = alloc_tty_driver(1);
2053 * We request SERDAT and SERPER only, because the serial registers are
2054 * too spreaded over the custom register space
2056 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2061 show_serial_version();
2063 /* Initialize the tty_driver structure */
2065 serial_driver->owner = THIS_MODULE;
2066 serial_driver->driver_name = "amiserial";
2067 serial_driver->name = "ttyS";
2068 serial_driver->major = TTY_MAJOR;
2069 serial_driver->minor_start = 64;
2070 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2071 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2072 serial_driver->init_termios = tty_std_termios;
2073 serial_driver->init_termios.c_cflag =
2074 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2075 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2076 tty_set_operations(serial_driver, &serial_ops);
2078 if (tty_register_driver(serial_driver))
2079 panic("Couldn't register serial driver\n");
2082 state->magic = SSTATE_MAGIC;
2083 state->port = (int)&custom.serdatr; /* Just to give it a value */
2085 state->custom_divisor = 0;
2086 state->close_delay = 5*HZ/10;
2087 state->closing_wait = 30*HZ;
2088 state->icount.cts = state->icount.dsr =
2089 state->icount.rng = state->icount.dcd = 0;
2090 state->icount.rx = state->icount.tx = 0;
2091 state->icount.frame = state->icount.parity = 0;
2092 state->icount.overrun = state->icount.brk = 0;
2094 if(state->port && check_region(state->port,REGION_LENGTH(state)))
2098 printk(KERN_INFO "ttyS%02d is the amiga builtin serial port\n",
2101 /* Hardware set up */
2103 state->baud_base = amiga_colorclock;
2104 state->xmit_fifo_size = 1;
2106 local_irq_save(flags);
2108 /* set ISRs, and then disable the rx interrupts */
2109 request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2110 request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2112 /* turn off Rx and Tx interrupts */
2113 custom.intena = IF_RBF | IF_TBE;
2116 /* clear any pending interrupt */
2117 custom.intreq = IF_RBF | IF_TBE;
2120 local_irq_restore(flags);
2123 * set the appropriate directions for the modem control flags,
2124 * and clear RTS and DTR
2126 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2127 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2132 static __exit void rs_exit(void)
2135 struct async_struct *info = rs_table[0].info;
2137 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2138 tasklet_kill(&info->tlet);
2139 if ((error = tty_unregister_driver(serial_driver)))
2140 printk("SERIAL: failed to unregister serial driver (%d)\n",
2142 put_tty_driver(serial_driver);
2145 rs_table[0].info = NULL;
2150 free_page((unsigned long) tmp_buf);
2154 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2157 module_init(rs_init)
2158 module_exit(rs_exit)
2162 * ------------------------------------------------------------
2163 * Serial console driver
2164 * ------------------------------------------------------------
2166 #ifdef CONFIG_SERIAL_CONSOLE
2168 static void amiga_serial_putc(char c)
2170 custom.serdat = (unsigned char)c | 0x100;
2171 while (!(custom.serdatr & 0x2000))
2176 * Print a string to the serial port trying not to disturb
2177 * any possible real use of the port...
2179 * The console must be locked when we get here.
2181 static void serial_console_write(struct console *co, const char *s,
2184 unsigned short intena = custom.intenar;
2186 custom.intena = IF_TBE;
2190 amiga_serial_putc('\r');
2191 amiga_serial_putc(*s++);
2194 custom.intena = IF_SETCLR | (intena & IF_TBE);
2197 static struct tty_driver *serial_console_device(struct console *c, int *index)
2200 return serial_driver;
2203 static struct console sercons = {
2205 .write = serial_console_write,
2206 .device = serial_console_device,
2207 .flags = CON_PRINTBUFFER,
2214 static int __init amiserial_console_init(void)
2216 register_console(&sercons);
2219 console_initcall(amiserial_console_init);
2222 MODULE_LICENSE("GPL");