Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / char / amiserial.c
blob1dc4259213a69ec15c747927d714e9d7c5a61d41
1 /*
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
31 * ever possible.
34 #include <linux/config.h>
35 #include <linux/delay.h>
37 #undef SERIAL_PARANOIA_CHECK
38 #define SERIAL_DO_RESTART
40 /* Set of debugging defines */
42 #undef SERIAL_DEBUG_INTR
43 #undef SERIAL_DEBUG_OPEN
44 #undef SERIAL_DEBUG_FLOW
45 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
47 /* Sanity checks */
49 #define SERIAL_INLINE
51 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
52 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
53 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
54 #else
55 #define DBG_CNT(s)
56 #endif
59 * End of serial driver configuration section.
62 #include <linux/module.h>
64 #include <linux/types.h>
65 #include <linux/serial.h>
66 #include <linux/serialP.h>
67 #include <linux/serial_reg.h>
68 static char *serial_version = "4.30";
70 #include <linux/errno.h>
71 #include <linux/signal.h>
72 #include <linux/sched.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/console.h>
79 #include <linux/major.h>
80 #include <linux/string.h>
81 #include <linux/fcntl.h>
82 #include <linux/ptrace.h>
83 #include <linux/ioport.h>
84 #include <linux/mm.h>
85 #include <linux/slab.h>
86 #include <linux/init.h>
87 #include <linux/bitops.h>
89 #include <asm/setup.h>
91 #include <asm/system.h>
93 #include <asm/irq.h>
95 #include <asm/amigahw.h>
96 #include <asm/amigaints.h>
98 #ifdef SERIAL_INLINE
99 #define _INLINE_ inline
100 #endif
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 * tmp_buf is used as a temporary buffer by serial_write. We need to
123 * lock it in case the copy_from_user blocks while swapping in a page,
124 * and some other program tries to do a serial write at the same time.
125 * Since the lock will only come under contention when the system is
126 * swapping and available memory is low, it makes sense to share one
127 * buffer across all the serial ports, since it significantly saves
128 * memory if large numbers of serial ports are open.
130 static unsigned char *tmp_buf;
131 static DECLARE_MUTEX(tmp_buf_sem);
133 #include <asm/uaccess.h>
135 #define serial_isroot() (capable(CAP_SYS_ADMIN))
138 static inline int serial_paranoia_check(struct async_struct *info,
139 char *name, const char *routine)
141 #ifdef SERIAL_PARANOIA_CHECK
142 static const char *badmagic =
143 "Warning: bad magic number for serial struct (%s) in %s\n";
144 static const char *badinfo =
145 "Warning: null async_struct for (%s) in %s\n";
147 if (!info) {
148 printk(badinfo, name, routine);
149 return 1;
151 if (info->magic != SERIAL_MAGIC) {
152 printk(badmagic, name, routine);
153 return 1;
155 #endif
156 return 0;
159 /* some serial hardware definitions */
160 #define SDR_OVRUN (1<<15)
161 #define SDR_RBF (1<<14)
162 #define SDR_TBE (1<<13)
163 #define SDR_TSRE (1<<12)
165 #define SERPER_PARENB (1<<15)
167 #define AC_SETCLR (1<<15)
168 #define AC_UARTBRK (1<<11)
170 #define SER_DTR (1<<7)
171 #define SER_RTS (1<<6)
172 #define SER_DCD (1<<5)
173 #define SER_CTS (1<<4)
174 #define SER_DSR (1<<3)
176 static __inline__ void rtsdtr_ctrl(int bits)
178 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
182 * ------------------------------------------------------------
183 * rs_stop() and rs_start()
185 * This routines are called before setting or resetting tty->stopped.
186 * They enable or disable transmitter interrupts, as necessary.
187 * ------------------------------------------------------------
189 static void rs_stop(struct tty_struct *tty)
191 struct async_struct *info = (struct async_struct *)tty->driver_data;
192 unsigned long flags;
194 if (serial_paranoia_check(info, tty->name, "rs_stop"))
195 return;
197 local_irq_save(flags);
198 if (info->IER & UART_IER_THRI) {
199 info->IER &= ~UART_IER_THRI;
200 /* disable Tx interrupt and remove any pending interrupts */
201 custom.intena = IF_TBE;
202 mb();
203 custom.intreq = IF_TBE;
204 mb();
206 local_irq_restore(flags);
209 static void rs_start(struct tty_struct *tty)
211 struct async_struct *info = (struct async_struct *)tty->driver_data;
212 unsigned long flags;
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 return;
217 local_irq_save(flags);
218 if (info->xmit.head != info->xmit.tail
219 && info->xmit.buf
220 && !(info->IER & UART_IER_THRI)) {
221 info->IER |= UART_IER_THRI;
222 custom.intena = IF_SETCLR | IF_TBE;
223 mb();
224 /* set a pending Tx Interrupt, transmitter should restart now */
225 custom.intreq = IF_SETCLR | IF_TBE;
226 mb();
228 local_irq_restore(flags);
232 * ----------------------------------------------------------------------
234 * Here starts the interrupt handling routines. All of the following
235 * subroutines are declared as inline and are folded into
236 * rs_interrupt(). They were separated out for readability's sake.
238 * Note: rs_interrupt() is a "fast" interrupt, which means that it
239 * runs with interrupts turned off. People who may want to modify
240 * rs_interrupt() should try to keep the interrupt handler as fast as
241 * possible. After you are done making modifications, it is not a bad
242 * idea to do:
244 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
246 * and look at the resulting assemble code in serial.s.
248 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
249 * -----------------------------------------------------------------------
253 * This routine is used by the interrupt handler to schedule
254 * processing in the software interrupt portion of the driver.
256 static _INLINE_ void rs_sched_event(struct async_struct *info,
257 int event)
259 info->event |= 1 << event;
260 tasklet_schedule(&info->tlet);
263 static _INLINE_ void receive_chars(struct async_struct *info)
265 int status;
266 int serdatr;
267 struct tty_struct *tty = info->tty;
268 unsigned char ch;
269 struct async_icount *icount;
271 icount = &info->state->icount;
273 status = UART_LSR_DR; /* We obviously have a character! */
274 serdatr = custom.serdatr;
275 mb();
276 custom.intreq = IF_RBF;
277 mb();
279 if((serdatr & 0x1ff) == 0)
280 status |= UART_LSR_BI;
281 if(serdatr & SDR_OVRUN)
282 status |= UART_LSR_OE;
284 ch = serdatr & 0xff;
285 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
286 goto ignore_char;
287 *tty->flip.char_buf_ptr = ch;
288 icount->rx++;
290 #ifdef SERIAL_DEBUG_INTR
291 printk("DR%02x:%02x...", ch, status);
292 #endif
293 *tty->flip.flag_buf_ptr = 0;
296 * We don't handle parity or frame errors - but I have left
297 * the code in, since I'm not sure that the errors can't be
298 * detected.
301 if (status & (UART_LSR_BI | UART_LSR_PE |
302 UART_LSR_FE | UART_LSR_OE)) {
304 * For statistics only
306 if (status & UART_LSR_BI) {
307 status &= ~(UART_LSR_FE | UART_LSR_PE);
308 icount->brk++;
309 } else if (status & UART_LSR_PE)
310 icount->parity++;
311 else if (status & UART_LSR_FE)
312 icount->frame++;
313 if (status & UART_LSR_OE)
314 icount->overrun++;
317 * Now check to see if character should be
318 * ignored, and mask off conditions which
319 * should be ignored.
321 if (status & info->ignore_status_mask)
322 goto ignore_char;
324 status &= info->read_status_mask;
326 if (status & (UART_LSR_BI)) {
327 #ifdef SERIAL_DEBUG_INTR
328 printk("handling break....");
329 #endif
330 *tty->flip.flag_buf_ptr = TTY_BREAK;
331 if (info->flags & ASYNC_SAK)
332 do_SAK(tty);
333 } else if (status & UART_LSR_PE)
334 *tty->flip.flag_buf_ptr = TTY_PARITY;
335 else if (status & UART_LSR_FE)
336 *tty->flip.flag_buf_ptr = TTY_FRAME;
337 if (status & UART_LSR_OE) {
339 * Overrun is special, since it's
340 * reported immediately, and doesn't
341 * affect the current character
343 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
344 tty->flip.count++;
345 tty->flip.flag_buf_ptr++;
346 tty->flip.char_buf_ptr++;
347 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
351 tty->flip.flag_buf_ptr++;
352 tty->flip.char_buf_ptr++;
353 tty->flip.count++;
354 ignore_char:
356 tty_flip_buffer_push(tty);
359 static _INLINE_ void transmit_chars(struct async_struct *info)
361 custom.intreq = IF_TBE;
362 mb();
363 if (info->x_char) {
364 custom.serdat = info->x_char | 0x100;
365 mb();
366 info->state->icount.tx++;
367 info->x_char = 0;
368 return;
370 if (info->xmit.head == info->xmit.tail
371 || info->tty->stopped
372 || info->tty->hw_stopped) {
373 info->IER &= ~UART_IER_THRI;
374 custom.intena = IF_TBE;
375 mb();
376 return;
379 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
380 mb();
381 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
382 info->state->icount.tx++;
384 if (CIRC_CNT(info->xmit.head,
385 info->xmit.tail,
386 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
387 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
389 #ifdef SERIAL_DEBUG_INTR
390 printk("THRE...");
391 #endif
392 if (info->xmit.head == info->xmit.tail) {
393 custom.intena = IF_TBE;
394 mb();
395 info->IER &= ~UART_IER_THRI;
399 static _INLINE_ void check_modem_status(struct async_struct *info)
401 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
402 unsigned char dstatus;
403 struct async_icount *icount;
405 /* Determine bits that have changed */
406 dstatus = status ^ current_ctl_bits;
407 current_ctl_bits = status;
409 if (dstatus) {
410 icount = &info->state->icount;
411 /* update input line counters */
412 if (dstatus & SER_DSR)
413 icount->dsr++;
414 if (dstatus & SER_DCD) {
415 icount->dcd++;
416 #ifdef CONFIG_HARD_PPS
417 if ((info->flags & ASYNC_HARDPPS_CD) &&
418 !(status & SER_DCD))
419 hardpps();
420 #endif
422 if (dstatus & SER_CTS)
423 icount->cts++;
424 wake_up_interruptible(&info->delta_msr_wait);
427 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
428 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
429 printk("ttyS%d CD now %s...", info->line,
430 (!(status & SER_DCD)) ? "on" : "off");
431 #endif
432 if (!(status & SER_DCD))
433 wake_up_interruptible(&info->open_wait);
434 else {
435 #ifdef SERIAL_DEBUG_OPEN
436 printk("doing serial hangup...");
437 #endif
438 if (info->tty)
439 tty_hangup(info->tty);
442 if (info->flags & ASYNC_CTS_FLOW) {
443 if (info->tty->hw_stopped) {
444 if (!(status & SER_CTS)) {
445 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
446 printk("CTS tx start...");
447 #endif
448 info->tty->hw_stopped = 0;
449 info->IER |= UART_IER_THRI;
450 custom.intena = IF_SETCLR | IF_TBE;
451 mb();
452 /* set a pending Tx Interrupt, transmitter should restart now */
453 custom.intreq = IF_SETCLR | IF_TBE;
454 mb();
455 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
456 return;
458 } else {
459 if ((status & SER_CTS)) {
460 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
461 printk("CTS tx stop...");
462 #endif
463 info->tty->hw_stopped = 1;
464 info->IER &= ~UART_IER_THRI;
465 /* disable Tx interrupt and remove any pending interrupts */
466 custom.intena = IF_TBE;
467 mb();
468 custom.intreq = IF_TBE;
469 mb();
475 static irqreturn_t ser_vbl_int( int irq, void *data, struct pt_regs *regs)
477 /* vbl is just a periodic interrupt we tie into to update modem status */
478 struct async_struct * info = IRQ_ports;
480 * TBD - is it better to unregister from this interrupt or to
481 * ignore it if MSI is clear ?
483 if(info->IER & UART_IER_MSI)
484 check_modem_status(info);
485 return IRQ_HANDLED;
488 static irqreturn_t ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
490 struct async_struct * info;
492 #ifdef SERIAL_DEBUG_INTR
493 printk("ser_rx_int...");
494 #endif
496 info = IRQ_ports;
497 if (!info || !info->tty)
498 return IRQ_NONE;
500 receive_chars(info);
501 info->last_active = jiffies;
502 #ifdef SERIAL_DEBUG_INTR
503 printk("end.\n");
504 #endif
505 return IRQ_HANDLED;
508 static irqreturn_t ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
510 struct async_struct * info;
512 if (custom.serdatr & SDR_TBE) {
513 #ifdef SERIAL_DEBUG_INTR
514 printk("ser_tx_int...");
515 #endif
517 info = IRQ_ports;
518 if (!info || !info->tty)
519 return IRQ_NONE;
521 transmit_chars(info);
522 info->last_active = jiffies;
523 #ifdef SERIAL_DEBUG_INTR
524 printk("end.\n");
525 #endif
527 return IRQ_HANDLED;
531 * -------------------------------------------------------------------
532 * Here ends the serial interrupt routines.
533 * -------------------------------------------------------------------
537 * This routine is used to handle the "bottom half" processing for the
538 * serial driver, known also the "software interrupt" processing.
539 * This processing is done at the kernel interrupt level, after the
540 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
541 * is where time-consuming activities which can not be done in the
542 * interrupt driver proper are done; the interrupt driver schedules
543 * them using rs_sched_event(), and they get done here.
546 static void do_softint(unsigned long private_)
548 struct async_struct *info = (struct async_struct *) private_;
549 struct tty_struct *tty;
551 tty = info->tty;
552 if (!tty)
553 return;
555 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
556 tty_wakeup(tty);
557 wake_up_interruptible(&tty->write_wait);
562 * ---------------------------------------------------------------
563 * Low level utility subroutines for the serial driver: routines to
564 * figure out the appropriate timeout for an interrupt chain, routines
565 * to initialize and startup a serial port, and routines to shutdown a
566 * serial port. Useful stuff like that.
567 * ---------------------------------------------------------------
570 static int startup(struct async_struct * info)
572 unsigned long flags;
573 int retval=0;
574 unsigned long page;
576 page = get_zeroed_page(GFP_KERNEL);
577 if (!page)
578 return -ENOMEM;
580 local_irq_save(flags);
582 if (info->flags & ASYNC_INITIALIZED) {
583 free_page(page);
584 goto errout;
587 if (info->xmit.buf)
588 free_page(page);
589 else
590 info->xmit.buf = (unsigned char *) page;
592 #ifdef SERIAL_DEBUG_OPEN
593 printk("starting up ttys%d ...", info->line);
594 #endif
596 /* Clear anything in the input buffer */
598 custom.intreq = IF_RBF;
599 mb();
601 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
602 if (retval) {
603 if (serial_isroot()) {
604 if (info->tty)
605 set_bit(TTY_IO_ERROR,
606 &info->tty->flags);
607 retval = 0;
609 goto errout;
612 /* enable both Rx and Tx interrupts */
613 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
614 mb();
615 info->IER = UART_IER_MSI;
617 /* remember current state of the DCD and CTS bits */
618 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
620 IRQ_ports = info;
622 info->MCR = 0;
623 if (info->tty->termios->c_cflag & CBAUD)
624 info->MCR = SER_DTR | SER_RTS;
625 rtsdtr_ctrl(info->MCR);
627 if (info->tty)
628 clear_bit(TTY_IO_ERROR, &info->tty->flags);
629 info->xmit.head = info->xmit.tail = 0;
632 * Set up the tty->alt_speed kludge
634 if (info->tty) {
635 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
636 info->tty->alt_speed = 57600;
637 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
638 info->tty->alt_speed = 115200;
639 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
640 info->tty->alt_speed = 230400;
641 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
642 info->tty->alt_speed = 460800;
646 * and set the speed of the serial port
648 change_speed(info, NULL);
650 info->flags |= ASYNC_INITIALIZED;
651 local_irq_restore(flags);
652 return 0;
654 errout:
655 local_irq_restore(flags);
656 return retval;
660 * This routine will shutdown a serial port; interrupts are disabled, and
661 * DTR is dropped if the hangup on close termio flag is on.
663 static void shutdown(struct async_struct * info)
665 unsigned long flags;
666 struct serial_state *state;
668 if (!(info->flags & ASYNC_INITIALIZED))
669 return;
671 state = info->state;
673 #ifdef SERIAL_DEBUG_OPEN
674 printk("Shutting down serial port %d ....\n", info->line);
675 #endif
677 local_irq_save(flags); /* Disable interrupts */
680 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
681 * here so the queue might never be waken up
683 wake_up_interruptible(&info->delta_msr_wait);
685 IRQ_ports = NULL;
688 * Free the IRQ, if necessary
690 free_irq(IRQ_AMIGA_VERTB, info);
692 if (info->xmit.buf) {
693 free_page((unsigned long) info->xmit.buf);
694 info->xmit.buf = NULL;
697 info->IER = 0;
698 custom.intena = IF_RBF | IF_TBE;
699 mb();
701 /* disable break condition */
702 custom.adkcon = AC_UARTBRK;
703 mb();
705 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
706 info->MCR &= ~(SER_DTR|SER_RTS);
707 rtsdtr_ctrl(info->MCR);
709 if (info->tty)
710 set_bit(TTY_IO_ERROR, &info->tty->flags);
712 info->flags &= ~ASYNC_INITIALIZED;
713 local_irq_restore(flags);
718 * This routine is called to set the UART divisor registers to match
719 * the specified baud rate for a serial port.
721 static void change_speed(struct async_struct *info,
722 struct termios *old_termios)
724 int quot = 0, baud_base, baud;
725 unsigned cflag, cval = 0;
726 int bits;
727 unsigned long flags;
729 if (!info->tty || !info->tty->termios)
730 return;
731 cflag = info->tty->termios->c_cflag;
733 /* Byte size is always 8 bits plus parity bit if requested */
735 cval = 3; bits = 10;
736 if (cflag & CSTOPB) {
737 cval |= 0x04;
738 bits++;
740 if (cflag & PARENB) {
741 cval |= UART_LCR_PARITY;
742 bits++;
744 if (!(cflag & PARODD))
745 cval |= UART_LCR_EPAR;
746 #ifdef CMSPAR
747 if (cflag & CMSPAR)
748 cval |= UART_LCR_SPAR;
749 #endif
751 /* Determine divisor based on baud rate */
752 baud = tty_get_baud_rate(info->tty);
753 if (!baud)
754 baud = 9600; /* B0 transition handled in rs_set_termios */
755 baud_base = info->state->baud_base;
756 if (baud == 38400 &&
757 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
758 quot = info->state->custom_divisor;
759 else {
760 if (baud == 134)
761 /* Special case since 134 is really 134.5 */
762 quot = (2*baud_base / 269);
763 else if (baud)
764 quot = baud_base / baud;
766 /* If the quotient is zero refuse the change */
767 if (!quot && old_termios) {
768 info->tty->termios->c_cflag &= ~CBAUD;
769 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
770 baud = tty_get_baud_rate(info->tty);
771 if (!baud)
772 baud = 9600;
773 if (baud == 38400 &&
774 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
775 quot = info->state->custom_divisor;
776 else {
777 if (baud == 134)
778 /* Special case since 134 is really 134.5 */
779 quot = (2*baud_base / 269);
780 else if (baud)
781 quot = baud_base / baud;
784 /* As a last resort, if the quotient is zero, default to 9600 bps */
785 if (!quot)
786 quot = baud_base / 9600;
787 info->quot = quot;
788 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
789 info->timeout += HZ/50; /* Add .02 seconds of slop */
791 /* CTS flow control flag and modem status interrupts */
792 info->IER &= ~UART_IER_MSI;
793 if (info->flags & ASYNC_HARDPPS_CD)
794 info->IER |= UART_IER_MSI;
795 if (cflag & CRTSCTS) {
796 info->flags |= ASYNC_CTS_FLOW;
797 info->IER |= UART_IER_MSI;
798 } else
799 info->flags &= ~ASYNC_CTS_FLOW;
800 if (cflag & CLOCAL)
801 info->flags &= ~ASYNC_CHECK_CD;
802 else {
803 info->flags |= ASYNC_CHECK_CD;
804 info->IER |= UART_IER_MSI;
806 /* TBD:
807 * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
811 * Set up parity check flag
813 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
815 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
816 if (I_INPCK(info->tty))
817 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
818 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
819 info->read_status_mask |= UART_LSR_BI;
822 * Characters to ignore
824 info->ignore_status_mask = 0;
825 if (I_IGNPAR(info->tty))
826 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
827 if (I_IGNBRK(info->tty)) {
828 info->ignore_status_mask |= UART_LSR_BI;
830 * If we're ignore parity and break indicators, ignore
831 * overruns too. (For real raw support).
833 if (I_IGNPAR(info->tty))
834 info->ignore_status_mask |= UART_LSR_OE;
837 * !!! ignore all characters if CREAD is not set
839 if ((cflag & CREAD) == 0)
840 info->ignore_status_mask |= UART_LSR_DR;
841 local_irq_save(flags);
844 short serper;
846 /* Set up the baud rate */
847 serper = quot - 1;
849 /* Enable or disable parity bit */
851 if(cval & UART_LCR_PARITY)
852 serper |= (SERPER_PARENB);
854 custom.serper = serper;
855 mb();
858 info->LCR = cval; /* Save LCR */
859 local_irq_restore(flags);
862 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
864 struct async_struct *info = (struct async_struct *)tty->driver_data;
865 unsigned long flags;
867 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
868 return;
870 if (!tty || !info->xmit.buf)
871 return;
873 local_irq_save(flags);
874 if (CIRC_SPACE(info->xmit.head,
875 info->xmit.tail,
876 SERIAL_XMIT_SIZE) == 0) {
877 local_irq_restore(flags);
878 return;
881 info->xmit.buf[info->xmit.head++] = ch;
882 info->xmit.head &= SERIAL_XMIT_SIZE-1;
883 local_irq_restore(flags);
886 static void rs_flush_chars(struct tty_struct *tty)
888 struct async_struct *info = (struct async_struct *)tty->driver_data;
889 unsigned long flags;
891 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
892 return;
894 if (info->xmit.head == info->xmit.tail
895 || tty->stopped
896 || tty->hw_stopped
897 || !info->xmit.buf)
898 return;
900 local_irq_save(flags);
901 info->IER |= UART_IER_THRI;
902 custom.intena = IF_SETCLR | IF_TBE;
903 mb();
904 /* set a pending Tx Interrupt, transmitter should restart now */
905 custom.intreq = IF_SETCLR | IF_TBE;
906 mb();
907 local_irq_restore(flags);
910 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
912 int c, ret = 0;
913 struct async_struct *info = (struct async_struct *)tty->driver_data;
914 unsigned long flags;
916 if (serial_paranoia_check(info, tty->name, "rs_write"))
917 return 0;
919 if (!tty || !info->xmit.buf || !tmp_buf)
920 return 0;
922 local_save_flags(flags);
923 local_irq_disable();
924 while (1) {
925 c = CIRC_SPACE_TO_END(info->xmit.head,
926 info->xmit.tail,
927 SERIAL_XMIT_SIZE);
928 if (count < c)
929 c = count;
930 if (c <= 0) {
931 break;
933 memcpy(info->xmit.buf + info->xmit.head, buf, c);
934 info->xmit.head = ((info->xmit.head + c) &
935 (SERIAL_XMIT_SIZE-1));
936 buf += c;
937 count -= c;
938 ret += c;
940 local_irq_restore(flags);
942 if (info->xmit.head != info->xmit.tail
943 && !tty->stopped
944 && !tty->hw_stopped
945 && !(info->IER & UART_IER_THRI)) {
946 info->IER |= UART_IER_THRI;
947 local_irq_disable();
948 custom.intena = IF_SETCLR | IF_TBE;
949 mb();
950 /* set a pending Tx Interrupt, transmitter should restart now */
951 custom.intreq = IF_SETCLR | IF_TBE;
952 mb();
953 local_irq_restore(flags);
955 return ret;
958 static int rs_write_room(struct tty_struct *tty)
960 struct async_struct *info = (struct async_struct *)tty->driver_data;
962 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
963 return 0;
964 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
967 static int rs_chars_in_buffer(struct tty_struct *tty)
969 struct async_struct *info = (struct async_struct *)tty->driver_data;
971 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
972 return 0;
973 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
976 static void rs_flush_buffer(struct tty_struct *tty)
978 struct async_struct *info = (struct async_struct *)tty->driver_data;
979 unsigned long flags;
981 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
982 return;
983 local_irq_save(flags);
984 info->xmit.head = info->xmit.tail = 0;
985 local_irq_restore(flags);
986 wake_up_interruptible(&tty->write_wait);
987 tty_wakeup(tty);
991 * This function is used to send a high-priority XON/XOFF character to
992 * the device
994 static void rs_send_xchar(struct tty_struct *tty, char ch)
996 struct async_struct *info = (struct async_struct *)tty->driver_data;
997 unsigned long flags;
999 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1000 return;
1002 info->x_char = ch;
1003 if (ch) {
1004 /* Make sure transmit interrupts are on */
1006 /* Check this ! */
1007 local_irq_save(flags);
1008 if(!(custom.intenar & IF_TBE)) {
1009 custom.intena = IF_SETCLR | IF_TBE;
1010 mb();
1011 /* set a pending Tx Interrupt, transmitter should restart now */
1012 custom.intreq = IF_SETCLR | IF_TBE;
1013 mb();
1015 local_irq_restore(flags);
1017 info->IER |= UART_IER_THRI;
1022 * ------------------------------------------------------------
1023 * rs_throttle()
1025 * This routine is called by the upper-layer tty layer to signal that
1026 * incoming characters should be throttled.
1027 * ------------------------------------------------------------
1029 static void rs_throttle(struct tty_struct * tty)
1031 struct async_struct *info = (struct async_struct *)tty->driver_data;
1032 unsigned long flags;
1033 #ifdef SERIAL_DEBUG_THROTTLE
1034 char buf[64];
1036 printk("throttle %s: %d....\n", tty_name(tty, buf),
1037 tty->ldisc.chars_in_buffer(tty));
1038 #endif
1040 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1041 return;
1043 if (I_IXOFF(tty))
1044 rs_send_xchar(tty, STOP_CHAR(tty));
1046 if (tty->termios->c_cflag & CRTSCTS)
1047 info->MCR &= ~SER_RTS;
1049 local_irq_save(flags);
1050 rtsdtr_ctrl(info->MCR);
1051 local_irq_restore(flags);
1054 static void rs_unthrottle(struct tty_struct * tty)
1056 struct async_struct *info = (struct async_struct *)tty->driver_data;
1057 unsigned long flags;
1058 #ifdef SERIAL_DEBUG_THROTTLE
1059 char buf[64];
1061 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1062 tty->ldisc.chars_in_buffer(tty));
1063 #endif
1065 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1066 return;
1068 if (I_IXOFF(tty)) {
1069 if (info->x_char)
1070 info->x_char = 0;
1071 else
1072 rs_send_xchar(tty, START_CHAR(tty));
1074 if (tty->termios->c_cflag & CRTSCTS)
1075 info->MCR |= SER_RTS;
1076 local_irq_save(flags);
1077 rtsdtr_ctrl(info->MCR);
1078 local_irq_restore(flags);
1082 * ------------------------------------------------------------
1083 * rs_ioctl() and friends
1084 * ------------------------------------------------------------
1087 static int get_serial_info(struct async_struct * info,
1088 struct serial_struct * retinfo)
1090 struct serial_struct tmp;
1091 struct serial_state *state = info->state;
1093 if (!retinfo)
1094 return -EFAULT;
1095 memset(&tmp, 0, sizeof(tmp));
1096 tmp.type = state->type;
1097 tmp.line = state->line;
1098 tmp.port = state->port;
1099 tmp.irq = state->irq;
1100 tmp.flags = state->flags;
1101 tmp.xmit_fifo_size = state->xmit_fifo_size;
1102 tmp.baud_base = state->baud_base;
1103 tmp.close_delay = state->close_delay;
1104 tmp.closing_wait = state->closing_wait;
1105 tmp.custom_divisor = state->custom_divisor;
1106 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1107 return -EFAULT;
1108 return 0;
1111 static int set_serial_info(struct async_struct * info,
1112 struct serial_struct * new_info)
1114 struct serial_struct new_serial;
1115 struct serial_state old_state, *state;
1116 unsigned int change_irq,change_port;
1117 int retval = 0;
1119 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1120 return -EFAULT;
1121 state = info->state;
1122 old_state = *state;
1124 change_irq = new_serial.irq != state->irq;
1125 change_port = (new_serial.port != state->port);
1126 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
1127 return -EINVAL;
1129 if (!serial_isroot()) {
1130 if ((new_serial.baud_base != state->baud_base) ||
1131 (new_serial.close_delay != state->close_delay) ||
1132 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1133 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1134 (state->flags & ~ASYNC_USR_MASK)))
1135 return -EPERM;
1136 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1137 (new_serial.flags & ASYNC_USR_MASK));
1138 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1139 (new_serial.flags & ASYNC_USR_MASK));
1140 state->custom_divisor = new_serial.custom_divisor;
1141 goto check_and_exit;
1144 if (new_serial.baud_base < 9600)
1145 return -EINVAL;
1148 * OK, past this point, all the error checking has been done.
1149 * At this point, we start making changes.....
1152 state->baud_base = new_serial.baud_base;
1153 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1154 (new_serial.flags & ASYNC_FLAGS));
1155 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1156 (info->flags & ASYNC_INTERNAL_FLAGS));
1157 state->custom_divisor = new_serial.custom_divisor;
1158 state->close_delay = new_serial.close_delay * HZ/100;
1159 state->closing_wait = new_serial.closing_wait * HZ/100;
1160 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1162 check_and_exit:
1163 if (info->flags & ASYNC_INITIALIZED) {
1164 if (((old_state.flags & ASYNC_SPD_MASK) !=
1165 (state->flags & ASYNC_SPD_MASK)) ||
1166 (old_state.custom_divisor != state->custom_divisor)) {
1167 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1168 info->tty->alt_speed = 57600;
1169 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1170 info->tty->alt_speed = 115200;
1171 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1172 info->tty->alt_speed = 230400;
1173 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1174 info->tty->alt_speed = 460800;
1175 change_speed(info, NULL);
1177 } else
1178 retval = startup(info);
1179 return retval;
1184 * get_lsr_info - get line status register info
1186 * Purpose: Let user call ioctl() to get info when the UART physically
1187 * is emptied. On bus types like RS485, the transmitter must
1188 * release the bus after transmitting. This must be done when
1189 * the transmit shift register is empty, not be done when the
1190 * transmit holding register is empty. This functionality
1191 * allows an RS485 driver to be written in user space.
1193 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1195 unsigned char status;
1196 unsigned int result;
1197 unsigned long flags;
1199 local_irq_save(flags);
1200 status = custom.serdatr;
1201 mb();
1202 local_irq_restore(flags);
1203 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1204 if (copy_to_user(value, &result, sizeof(int)))
1205 return -EFAULT;
1206 return 0;
1210 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1212 struct async_struct * info = (struct async_struct *)tty->driver_data;
1213 unsigned char control, status;
1214 unsigned long flags;
1216 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1217 return -ENODEV;
1218 if (tty->flags & (1 << TTY_IO_ERROR))
1219 return -EIO;
1221 control = info->MCR;
1222 local_irq_save(flags);
1223 status = ciab.pra;
1224 local_irq_restore(flags);
1225 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1226 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1227 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1228 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1229 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1232 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1233 unsigned int set, unsigned int clear)
1235 struct async_struct * info = (struct async_struct *)tty->driver_data;
1236 unsigned long flags;
1238 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1239 return -ENODEV;
1240 if (tty->flags & (1 << TTY_IO_ERROR))
1241 return -EIO;
1243 local_irq_save(flags);
1244 if (set & TIOCM_RTS)
1245 info->MCR |= SER_RTS;
1246 if (set & TIOCM_DTR)
1247 info->MCR |= SER_DTR;
1248 if (clear & TIOCM_RTS)
1249 info->MCR &= ~SER_RTS;
1250 if (clear & TIOCM_DTR)
1251 info->MCR &= ~SER_DTR;
1252 rtsdtr_ctrl(info->MCR);
1253 local_irq_restore(flags);
1254 return 0;
1258 * rs_break() --- routine which turns the break handling on or off
1260 static void rs_break(struct tty_struct *tty, int break_state)
1262 struct async_struct * info = (struct async_struct *)tty->driver_data;
1263 unsigned long flags;
1265 if (serial_paranoia_check(info, tty->name, "rs_break"))
1266 return;
1268 local_irq_save(flags);
1269 if (break_state == -1)
1270 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1271 else
1272 custom.adkcon = AC_UARTBRK;
1273 mb();
1274 local_irq_restore(flags);
1278 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1279 unsigned int cmd, unsigned long arg)
1281 struct async_struct * info = (struct async_struct *)tty->driver_data;
1282 struct async_icount cprev, cnow; /* kernel counter temps */
1283 struct serial_icounter_struct icount;
1284 unsigned long flags;
1286 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1287 return -ENODEV;
1289 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1290 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1291 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1292 if (tty->flags & (1 << TTY_IO_ERROR))
1293 return -EIO;
1296 switch (cmd) {
1297 case TIOCGSERIAL:
1298 return get_serial_info(info,
1299 (struct serial_struct *) arg);
1300 case TIOCSSERIAL:
1301 return set_serial_info(info,
1302 (struct serial_struct *) arg);
1303 case TIOCSERCONFIG:
1304 return 0;
1306 case TIOCSERGETLSR: /* Get line status register */
1307 return get_lsr_info(info, (unsigned int *) arg);
1309 case TIOCSERGSTRUCT:
1310 if (copy_to_user((struct async_struct *) arg,
1311 info, sizeof(struct async_struct)))
1312 return -EFAULT;
1313 return 0;
1316 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1317 * - mask passed in arg for lines of interest
1318 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1319 * Caller should use TIOCGICOUNT to see which one it was
1321 case TIOCMIWAIT:
1322 local_irq_save(flags);
1323 /* note the counters on entry */
1324 cprev = info->state->icount;
1325 local_irq_restore(flags);
1326 while (1) {
1327 interruptible_sleep_on(&info->delta_msr_wait);
1328 /* see if a signal did it */
1329 if (signal_pending(current))
1330 return -ERESTARTSYS;
1331 local_irq_save(flags);
1332 cnow = info->state->icount; /* atomic copy */
1333 local_irq_restore(flags);
1334 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1335 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1336 return -EIO; /* no change => error */
1337 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1338 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1339 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1340 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1341 return 0;
1343 cprev = cnow;
1345 /* NOTREACHED */
1348 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1349 * Return: write counters to the user passed counter struct
1350 * NB: both 1->0 and 0->1 transitions are counted except for
1351 * RI where only 0->1 is counted.
1353 case TIOCGICOUNT:
1354 local_irq_save(flags);
1355 cnow = info->state->icount;
1356 local_irq_restore(flags);
1357 icount.cts = cnow.cts;
1358 icount.dsr = cnow.dsr;
1359 icount.rng = cnow.rng;
1360 icount.dcd = cnow.dcd;
1361 icount.rx = cnow.rx;
1362 icount.tx = cnow.tx;
1363 icount.frame = cnow.frame;
1364 icount.overrun = cnow.overrun;
1365 icount.parity = cnow.parity;
1366 icount.brk = cnow.brk;
1367 icount.buf_overrun = cnow.buf_overrun;
1369 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1370 return -EFAULT;
1371 return 0;
1372 case TIOCSERGWILD:
1373 case TIOCSERSWILD:
1374 /* "setserial -W" is called in Debian boot */
1375 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1376 return 0;
1378 default:
1379 return -ENOIOCTLCMD;
1381 return 0;
1384 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1386 struct async_struct *info = (struct async_struct *)tty->driver_data;
1387 unsigned long flags;
1388 unsigned int cflag = tty->termios->c_cflag;
1390 if ( (cflag == old_termios->c_cflag)
1391 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1392 == RELEVANT_IFLAG(old_termios->c_iflag)))
1393 return;
1395 change_speed(info, old_termios);
1397 /* Handle transition to B0 status */
1398 if ((old_termios->c_cflag & CBAUD) &&
1399 !(cflag & CBAUD)) {
1400 info->MCR &= ~(SER_DTR|SER_RTS);
1401 local_irq_save(flags);
1402 rtsdtr_ctrl(info->MCR);
1403 local_irq_restore(flags);
1406 /* Handle transition away from B0 status */
1407 if (!(old_termios->c_cflag & CBAUD) &&
1408 (cflag & CBAUD)) {
1409 info->MCR |= SER_DTR;
1410 if (!(tty->termios->c_cflag & CRTSCTS) ||
1411 !test_bit(TTY_THROTTLED, &tty->flags)) {
1412 info->MCR |= SER_RTS;
1414 local_irq_save(flags);
1415 rtsdtr_ctrl(info->MCR);
1416 local_irq_restore(flags);
1419 /* Handle turning off CRTSCTS */
1420 if ((old_termios->c_cflag & CRTSCTS) &&
1421 !(tty->termios->c_cflag & CRTSCTS)) {
1422 tty->hw_stopped = 0;
1423 rs_start(tty);
1426 #if 0
1428 * No need to wake up processes in open wait, since they
1429 * sample the CLOCAL flag once, and don't recheck it.
1430 * XXX It's not clear whether the current behavior is correct
1431 * or not. Hence, this may change.....
1433 if (!(old_termios->c_cflag & CLOCAL) &&
1434 (tty->termios->c_cflag & CLOCAL))
1435 wake_up_interruptible(&info->open_wait);
1436 #endif
1440 * ------------------------------------------------------------
1441 * rs_close()
1443 * This routine is called when the serial port gets closed. First, we
1444 * wait for the last remaining data to be sent. Then, we unlink its
1445 * async structure from the interrupt chain if necessary, and we free
1446 * that IRQ if nothing is left in the chain.
1447 * ------------------------------------------------------------
1449 static void rs_close(struct tty_struct *tty, struct file * filp)
1451 struct async_struct * info = (struct async_struct *)tty->driver_data;
1452 struct serial_state *state;
1453 unsigned long flags;
1455 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1456 return;
1458 state = info->state;
1460 local_irq_save(flags);
1462 if (tty_hung_up_p(filp)) {
1463 DBG_CNT("before DEC-hung");
1464 local_irq_restore(flags);
1465 return;
1468 #ifdef SERIAL_DEBUG_OPEN
1469 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1470 #endif
1471 if ((tty->count == 1) && (state->count != 1)) {
1473 * Uh, oh. tty->count is 1, which means that the tty
1474 * structure will be freed. state->count should always
1475 * be one in these conditions. If it's greater than
1476 * one, we've got real problems, since it means the
1477 * serial port won't be shutdown.
1479 printk("rs_close: bad serial port count; tty->count is 1, "
1480 "state->count is %d\n", state->count);
1481 state->count = 1;
1483 if (--state->count < 0) {
1484 printk("rs_close: bad serial port count for ttys%d: %d\n",
1485 info->line, state->count);
1486 state->count = 0;
1488 if (state->count) {
1489 DBG_CNT("before DEC-2");
1490 local_irq_restore(flags);
1491 return;
1493 info->flags |= ASYNC_CLOSING;
1495 * Now we wait for the transmit buffer to clear; and we notify
1496 * the line discipline to only process XON/XOFF characters.
1498 tty->closing = 1;
1499 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1500 tty_wait_until_sent(tty, info->closing_wait);
1502 * At this point we stop accepting input. To do this, we
1503 * disable the receive line status interrupts, and tell the
1504 * interrupt driver to stop checking the data ready bit in the
1505 * line status register.
1507 info->read_status_mask &= ~UART_LSR_DR;
1508 if (info->flags & ASYNC_INITIALIZED) {
1509 /* disable receive interrupts */
1510 custom.intena = IF_RBF;
1511 mb();
1512 /* clear any pending receive interrupt */
1513 custom.intreq = IF_RBF;
1514 mb();
1517 * Before we drop DTR, make sure the UART transmitter
1518 * has completely drained; this is especially
1519 * important if there is a transmit FIFO!
1521 rs_wait_until_sent(tty, info->timeout);
1523 shutdown(info);
1524 if (tty->driver->flush_buffer)
1525 tty->driver->flush_buffer(tty);
1527 tty_ldisc_flush(tty);
1528 tty->closing = 0;
1529 info->event = 0;
1530 info->tty = NULL;
1531 if (info->blocked_open) {
1532 if (info->close_delay) {
1533 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1535 wake_up_interruptible(&info->open_wait);
1537 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1538 wake_up_interruptible(&info->close_wait);
1539 local_irq_restore(flags);
1543 * rs_wait_until_sent() --- wait until the transmitter is empty
1545 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1547 struct async_struct * info = (struct async_struct *)tty->driver_data;
1548 unsigned long orig_jiffies, char_time;
1549 int lsr;
1551 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1552 return;
1554 if (info->xmit_fifo_size == 0)
1555 return; /* Just in case.... */
1557 orig_jiffies = jiffies;
1559 * Set the check interval to be 1/5 of the estimated time to
1560 * send a single character, and make it at least 1. The check
1561 * interval should also be less than the timeout.
1563 * Note: we have to use pretty tight timings here to satisfy
1564 * the NIST-PCTS.
1566 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1567 char_time = char_time / 5;
1568 if (char_time == 0)
1569 char_time = 1;
1570 if (timeout)
1571 char_time = min_t(unsigned long, char_time, timeout);
1573 * If the transmitter hasn't cleared in twice the approximate
1574 * amount of time to send the entire FIFO, it probably won't
1575 * ever clear. This assumes the UART isn't doing flow
1576 * control, which is currently the case. Hence, if it ever
1577 * takes longer than info->timeout, this is probably due to a
1578 * UART bug of some kind. So, we clamp the timeout parameter at
1579 * 2*info->timeout.
1581 if (!timeout || timeout > 2*info->timeout)
1582 timeout = 2*info->timeout;
1583 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1584 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1585 printk("jiff=%lu...", jiffies);
1586 #endif
1587 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1588 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1589 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1590 #endif
1591 msleep_interruptible(jiffies_to_msecs(char_time));
1592 if (signal_pending(current))
1593 break;
1594 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1595 break;
1597 current->state = TASK_RUNNING;
1598 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1599 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1600 #endif
1604 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1606 static void rs_hangup(struct tty_struct *tty)
1608 struct async_struct * info = (struct async_struct *)tty->driver_data;
1609 struct serial_state *state = info->state;
1611 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1612 return;
1614 state = info->state;
1616 rs_flush_buffer(tty);
1617 shutdown(info);
1618 info->event = 0;
1619 state->count = 0;
1620 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1621 info->tty = NULL;
1622 wake_up_interruptible(&info->open_wait);
1626 * ------------------------------------------------------------
1627 * rs_open() and friends
1628 * ------------------------------------------------------------
1630 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1631 struct async_struct *info)
1633 #ifdef DECLARE_WAITQUEUE
1634 DECLARE_WAITQUEUE(wait, current);
1635 #else
1636 struct wait_queue wait = { current, NULL };
1637 #endif
1638 struct serial_state *state = info->state;
1639 int retval;
1640 int do_clocal = 0, extra_count = 0;
1641 unsigned long flags;
1644 * If the device is in the middle of being closed, then block
1645 * until it's done, and then try again.
1647 if (tty_hung_up_p(filp) ||
1648 (info->flags & ASYNC_CLOSING)) {
1649 if (info->flags & ASYNC_CLOSING)
1650 interruptible_sleep_on(&info->close_wait);
1651 #ifdef SERIAL_DO_RESTART
1652 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1653 -EAGAIN : -ERESTARTSYS);
1654 #else
1655 return -EAGAIN;
1656 #endif
1660 * If non-blocking mode is set, or the port is not enabled,
1661 * then make the check up front and then exit.
1663 if ((filp->f_flags & O_NONBLOCK) ||
1664 (tty->flags & (1 << TTY_IO_ERROR))) {
1665 info->flags |= ASYNC_NORMAL_ACTIVE;
1666 return 0;
1669 if (tty->termios->c_cflag & CLOCAL)
1670 do_clocal = 1;
1673 * Block waiting for the carrier detect and the line to become
1674 * free (i.e., not in use by the callout). While we are in
1675 * this loop, state->count is dropped by one, so that
1676 * rs_close() knows when to free things. We restore it upon
1677 * exit, either normal or abnormal.
1679 retval = 0;
1680 add_wait_queue(&info->open_wait, &wait);
1681 #ifdef SERIAL_DEBUG_OPEN
1682 printk("block_til_ready before block: ttys%d, count = %d\n",
1683 state->line, state->count);
1684 #endif
1685 local_irq_save(flags);
1686 if (!tty_hung_up_p(filp)) {
1687 extra_count = 1;
1688 state->count--;
1690 local_irq_restore(flags);
1691 info->blocked_open++;
1692 while (1) {
1693 local_irq_save(flags);
1694 if (tty->termios->c_cflag & CBAUD)
1695 rtsdtr_ctrl(SER_DTR|SER_RTS);
1696 local_irq_restore(flags);
1697 set_current_state(TASK_INTERRUPTIBLE);
1698 if (tty_hung_up_p(filp) ||
1699 !(info->flags & ASYNC_INITIALIZED)) {
1700 #ifdef SERIAL_DO_RESTART
1701 if (info->flags & ASYNC_HUP_NOTIFY)
1702 retval = -EAGAIN;
1703 else
1704 retval = -ERESTARTSYS;
1705 #else
1706 retval = -EAGAIN;
1707 #endif
1708 break;
1710 if (!(info->flags & ASYNC_CLOSING) &&
1711 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1712 break;
1713 if (signal_pending(current)) {
1714 retval = -ERESTARTSYS;
1715 break;
1717 #ifdef SERIAL_DEBUG_OPEN
1718 printk("block_til_ready blocking: ttys%d, count = %d\n",
1719 info->line, state->count);
1720 #endif
1721 schedule();
1723 current->state = TASK_RUNNING;
1724 remove_wait_queue(&info->open_wait, &wait);
1725 if (extra_count)
1726 state->count++;
1727 info->blocked_open--;
1728 #ifdef SERIAL_DEBUG_OPEN
1729 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1730 info->line, state->count);
1731 #endif
1732 if (retval)
1733 return retval;
1734 info->flags |= ASYNC_NORMAL_ACTIVE;
1735 return 0;
1738 static int get_async_struct(int line, struct async_struct **ret_info)
1740 struct async_struct *info;
1741 struct serial_state *sstate;
1743 sstate = rs_table + line;
1744 sstate->count++;
1745 if (sstate->info) {
1746 *ret_info = sstate->info;
1747 return 0;
1749 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1750 if (!info) {
1751 sstate->count--;
1752 return -ENOMEM;
1754 memset(info, 0, sizeof(struct async_struct));
1755 #ifdef DECLARE_WAITQUEUE
1756 init_waitqueue_head(&info->open_wait);
1757 init_waitqueue_head(&info->close_wait);
1758 init_waitqueue_head(&info->delta_msr_wait);
1759 #endif
1760 info->magic = SERIAL_MAGIC;
1761 info->port = sstate->port;
1762 info->flags = sstate->flags;
1763 info->xmit_fifo_size = sstate->xmit_fifo_size;
1764 info->line = line;
1765 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1766 info->state = sstate;
1767 if (sstate->info) {
1768 kfree(info);
1769 *ret_info = sstate->info;
1770 return 0;
1772 *ret_info = sstate->info = info;
1773 return 0;
1777 * This routine is called whenever a serial port is opened. It
1778 * enables interrupts for a serial port, linking in its async structure into
1779 * the IRQ chain. It also performs the serial-specific
1780 * initialization for the tty structure.
1782 static int rs_open(struct tty_struct *tty, struct file * filp)
1784 struct async_struct *info;
1785 int retval, line;
1786 unsigned long page;
1788 line = tty->index;
1789 if ((line < 0) || (line >= NR_PORTS)) {
1790 return -ENODEV;
1792 retval = get_async_struct(line, &info);
1793 if (retval) {
1794 return retval;
1796 tty->driver_data = info;
1797 info->tty = tty;
1798 if (serial_paranoia_check(info, tty->name, "rs_open"))
1799 return -ENODEV;
1801 #ifdef SERIAL_DEBUG_OPEN
1802 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1803 #endif
1804 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1806 if (!tmp_buf) {
1807 page = get_zeroed_page(GFP_KERNEL);
1808 if (!page) {
1809 return -ENOMEM;
1811 if (tmp_buf)
1812 free_page(page);
1813 else
1814 tmp_buf = (unsigned char *) page;
1818 * If the port is the middle of closing, bail out now
1820 if (tty_hung_up_p(filp) ||
1821 (info->flags & ASYNC_CLOSING)) {
1822 if (info->flags & ASYNC_CLOSING)
1823 interruptible_sleep_on(&info->close_wait);
1824 #ifdef SERIAL_DO_RESTART
1825 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1826 -EAGAIN : -ERESTARTSYS);
1827 #else
1828 return -EAGAIN;
1829 #endif
1833 * Start up serial port
1835 retval = startup(info);
1836 if (retval) {
1837 return retval;
1840 retval = block_til_ready(tty, filp, info);
1841 if (retval) {
1842 #ifdef SERIAL_DEBUG_OPEN
1843 printk("rs_open returning after block_til_ready with %d\n",
1844 retval);
1845 #endif
1846 return retval;
1849 #ifdef SERIAL_DEBUG_OPEN
1850 printk("rs_open %s successful...", tty->name);
1851 #endif
1852 return 0;
1856 * /proc fs routines....
1859 static inline int line_info(char *buf, struct serial_state *state)
1861 struct async_struct *info = state->info, scr_info;
1862 char stat_buf[30], control, status;
1863 int ret;
1864 unsigned long flags;
1866 ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
1869 * Figure out the current RS-232 lines
1871 if (!info) {
1872 info = &scr_info; /* This is just for serial_{in,out} */
1874 info->magic = SERIAL_MAGIC;
1875 info->flags = state->flags;
1876 info->quot = 0;
1877 info->tty = NULL;
1879 local_irq_save(flags);
1880 status = ciab.pra;
1881 control = info ? info->MCR : status;
1882 local_irq_restore(flags);
1884 stat_buf[0] = 0;
1885 stat_buf[1] = 0;
1886 if(!(control & SER_RTS))
1887 strcat(stat_buf, "|RTS");
1888 if(!(status & SER_CTS))
1889 strcat(stat_buf, "|CTS");
1890 if(!(control & SER_DTR))
1891 strcat(stat_buf, "|DTR");
1892 if(!(status & SER_DSR))
1893 strcat(stat_buf, "|DSR");
1894 if(!(status & SER_DCD))
1895 strcat(stat_buf, "|CD");
1897 if (info->quot) {
1898 ret += sprintf(buf+ret, " baud:%d",
1899 state->baud_base / info->quot);
1902 ret += sprintf(buf+ret, " tx:%d rx:%d",
1903 state->icount.tx, state->icount.rx);
1905 if (state->icount.frame)
1906 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
1908 if (state->icount.parity)
1909 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
1911 if (state->icount.brk)
1912 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
1914 if (state->icount.overrun)
1915 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
1918 * Last thing is the RS-232 status lines
1920 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
1921 return ret;
1924 static int rs_read_proc(char *page, char **start, off_t off, int count,
1925 int *eof, void *data)
1927 int len = 0, l;
1928 off_t begin = 0;
1930 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
1931 l = line_info(page + len, &rs_table[0]);
1932 len += l;
1933 if (len+begin > off+count)
1934 goto done;
1935 if (len+begin < off) {
1936 begin += len;
1937 len = 0;
1939 *eof = 1;
1940 done:
1941 if (off >= len+begin)
1942 return 0;
1943 *start = page + (off-begin);
1944 return ((count < begin+len-off) ? count : begin+len-off);
1948 * ---------------------------------------------------------------------
1949 * rs_init() and friends
1951 * rs_init() is called at boot-time to initialize the serial driver.
1952 * ---------------------------------------------------------------------
1956 * This routine prints out the appropriate serial driver version
1957 * number, and identifies which options were configured into this
1958 * driver.
1960 static _INLINE_ void show_serial_version(void)
1962 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1966 int register_serial(struct serial_struct *req);
1967 void unregister_serial(int line);
1970 static struct tty_operations serial_ops = {
1971 .open = rs_open,
1972 .close = rs_close,
1973 .write = rs_write,
1974 .put_char = rs_put_char,
1975 .flush_chars = rs_flush_chars,
1976 .write_room = rs_write_room,
1977 .chars_in_buffer = rs_chars_in_buffer,
1978 .flush_buffer = rs_flush_buffer,
1979 .ioctl = rs_ioctl,
1980 .throttle = rs_throttle,
1981 .unthrottle = rs_unthrottle,
1982 .set_termios = rs_set_termios,
1983 .stop = rs_stop,
1984 .start = rs_start,
1985 .hangup = rs_hangup,
1986 .break_ctl = rs_break,
1987 .send_xchar = rs_send_xchar,
1988 .wait_until_sent = rs_wait_until_sent,
1989 .read_proc = rs_read_proc,
1990 .tiocmget = rs_tiocmget,
1991 .tiocmset = rs_tiocmset,
1995 * The serial driver boot-time initialization code!
1997 static int __init rs_init(void)
1999 unsigned long flags;
2000 struct serial_state * state;
2002 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
2003 return -ENODEV;
2005 serial_driver = alloc_tty_driver(1);
2006 if (!serial_driver)
2007 return -ENOMEM;
2010 * We request SERDAT and SERPER only, because the serial registers are
2011 * too spreaded over the custom register space
2013 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
2014 return -EBUSY;
2016 IRQ_ports = NULL;
2018 show_serial_version();
2020 /* Initialize the tty_driver structure */
2022 serial_driver->owner = THIS_MODULE;
2023 serial_driver->driver_name = "amiserial";
2024 serial_driver->name = "ttyS";
2025 serial_driver->major = TTY_MAJOR;
2026 serial_driver->minor_start = 64;
2027 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2028 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2029 serial_driver->init_termios = tty_std_termios;
2030 serial_driver->init_termios.c_cflag =
2031 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2032 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2033 tty_set_operations(serial_driver, &serial_ops);
2035 if (tty_register_driver(serial_driver))
2036 panic("Couldn't register serial driver\n");
2038 state = rs_table;
2039 state->magic = SSTATE_MAGIC;
2040 state->port = (int)&custom.serdatr; /* Just to give it a value */
2041 state->line = 0;
2042 state->custom_divisor = 0;
2043 state->close_delay = 5*HZ/10;
2044 state->closing_wait = 30*HZ;
2045 state->icount.cts = state->icount.dsr =
2046 state->icount.rng = state->icount.dcd = 0;
2047 state->icount.rx = state->icount.tx = 0;
2048 state->icount.frame = state->icount.parity = 0;
2049 state->icount.overrun = state->icount.brk = 0;
2051 if(state->port && check_region(state->port,REGION_LENGTH(state)))
2052 continue;
2055 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2056 state->line);
2058 /* Hardware set up */
2060 state->baud_base = amiga_colorclock;
2061 state->xmit_fifo_size = 1;
2063 local_irq_save(flags);
2065 /* set ISRs, and then disable the rx interrupts */
2066 request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2067 request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
2069 /* turn off Rx and Tx interrupts */
2070 custom.intena = IF_RBF | IF_TBE;
2071 mb();
2073 /* clear any pending interrupt */
2074 custom.intreq = IF_RBF | IF_TBE;
2075 mb();
2077 local_irq_restore(flags);
2080 * set the appropriate directions for the modem control flags,
2081 * and clear RTS and DTR
2083 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2084 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2086 return 0;
2089 static __exit void rs_exit(void)
2091 int error;
2092 struct async_struct *info = rs_table[0].info;
2094 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2095 tasklet_kill(&info->tlet);
2096 if ((error = tty_unregister_driver(serial_driver)))
2097 printk("SERIAL: failed to unregister serial driver (%d)\n",
2098 error);
2099 put_tty_driver(serial_driver);
2101 if (info) {
2102 rs_table[0].info = NULL;
2103 kfree(info);
2106 if (tmp_buf) {
2107 free_page((unsigned long) tmp_buf);
2108 tmp_buf = NULL;
2111 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2114 module_init(rs_init)
2115 module_exit(rs_exit)
2119 * ------------------------------------------------------------
2120 * Serial console driver
2121 * ------------------------------------------------------------
2123 #ifdef CONFIG_SERIAL_CONSOLE
2125 static void amiga_serial_putc(char c)
2127 custom.serdat = (unsigned char)c | 0x100;
2128 while (!(custom.serdatr & 0x2000))
2129 barrier();
2133 * Print a string to the serial port trying not to disturb
2134 * any possible real use of the port...
2136 * The console must be locked when we get here.
2138 static void serial_console_write(struct console *co, const char *s,
2139 unsigned count)
2141 unsigned short intena = custom.intenar;
2143 custom.intena = IF_TBE;
2145 while (count--) {
2146 if (*s == '\n')
2147 amiga_serial_putc('\r');
2148 amiga_serial_putc(*s++);
2151 custom.intena = IF_SETCLR | (intena & IF_TBE);
2154 static struct tty_driver *serial_console_device(struct console *c, int *index)
2156 *index = 0;
2157 return serial_driver;
2160 static struct console sercons = {
2161 .name = "ttyS",
2162 .write = serial_console_write,
2163 .device = serial_console_device,
2164 .flags = CON_PRINTBUFFER,
2165 .index = -1,
2169 * Register console.
2171 static int __init amiserial_console_init(void)
2173 register_console(&sercons);
2174 return 0;
2176 console_initcall(amiserial_console_init);
2177 #endif
2179 MODULE_LICENSE("GPL");